File Coverage

blib/lib/DBR/Config.pm
Criterion Covered Total %
statement 63 67 94.0
branch 18 32 56.2
condition 1 6 16.6
subroutine 8 8 100.0
pod 0 3 0.0
total 90 116 77.5


line stmt bran cond sub pod time code
1             # the contents of this file are Copyright (c) 2009 Daniel Norman
2             # This program is free software; you can redistribute it and/or
3             # modify it under the terms of the GNU General Public License as
4             # published by the Free Software Foundation.
5              
6             package DBR::Config;
7              
8 18     18   118 use strict;
  18         42  
  18         901  
9 18     18   205 use base 'DBR::Common';
  18         41  
  18         3040  
10 18     18   11696 use DBR::Config::Instance;
  18         69  
  18         739  
11 18     18   162 use DBR::Config::Schema;
  18         45  
  18         362  
12 18     18   116 use Carp;
  18         41  
  18         22650  
13              
14             my %LOADED_FILES;
15             sub new {
16 17     17 0 69 my( $package ) = shift;
17 17         66 my %params = @_;
18 17         85 my $self = {session => $params{session}};
19              
20 17 50       290 croak( 'session is required' ) unless $self->{session};
21              
22 17         63 bless( $self, $package );
23              
24 17         260 return( $self );
25             }
26              
27              
28             sub load_file{
29 17     17 0 88 my $self = shift;
30 17         97 my %params = @_;
31              
32 17 50       101 my $dbr = $params{'dbr'} or return $self->_error( 'dbr parameter is required' );
33 17 50       103 my $file = $params{'file'} or return $self->_error( 'file parameter is required' );
34 17 50       76 if ($LOADED_FILES{$file}){
35 0         0 $self->_logDebug2("skipping already loaded config file '$file'");
36 0         0 return 1;
37             }
38              
39 17         195 $self->_logDebug2("loading config file '$file'");
40 17         37 my @conf;
41 17         45 my $setcount = 0;
42 17 50       952 open (my $fh, '<', $file) || return $self->_error("Failed to open '$file'");
43              
44 17         511 while (my $row = <$fh>) {
45 119 100       660 if ($row =~ /^(.*?)\#/){ # strip everything after the first comment
46 85         235 $row = $1;
47             }
48              
49 119         942 $row =~ s/(^\s*|\s*$)//g;# strip leading and trailing spaces
50 119 100       2275 next unless length($row);
51              
52 17   50     179 $conf[$setcount] ||= {};
53 17 50       111 if($row =~ /^---/){ # section divider. increment the count and skip this iteration
54 0         0 $setcount++;
55 0         0 next;
56             }
57              
58 17         237 foreach my $part (split(/\s*\;\s*/,$row)){ # Semicolons are ok in lieu of newline cus I'm arbitrary like that.
59 85         607 my ($key,$val) = $part =~ /^(.*?)\s*=\s*(.*)$/;
60              
61 85         502 $conf[$setcount]->{lc($key)} = $val;
62             }
63             }
64 17         409 close $fh;
65              
66             # Filter blank sections
67 17         135 @conf = grep { scalar ( %{$_} ) } @conf;
  17         48  
  17         151  
68              
69 17         43 my $count;
70 17         50 foreach my $instspec (@conf){
71 17         41 $count++;
72              
73 17 50 0     239 my $instance = DBR::Config::Instance->register(
74             dbr => $dbr,
75             session => $self->{session},
76             spec => $instspec
77             ) or $self->_error("failed to load DBR conf file '$file' (stanza #$count)") && next;
78 17 50       129 if($instance->dbr_bootstrap){
79             #don't bail out here on error
80 17 50 0     200 $self->load_dbconf(
81             dbr => $dbr,
82             instance => $instance
83             ) || $self->_error("failed to load DBR config tables") && next;
84             }
85             }
86              
87 17         83 $LOADED_FILES{$file} = 1;
88              
89 17         287 return 1;
90              
91             }
92              
93             sub load_dbconf{
94 17     17 0 41 my $self = shift;
95 17         82 my %params = @_;
96              
97              
98              
99 17 50       101 my $dbr = $params{'dbr'} or return $self->_error( 'dbr parameter is required' );
100 17 50       85 my $parent_inst = $params{'instance'} or return $self->_error( 'instance parameter is required' );
101              
102              
103              
104 17 50       204 $self->_error("failed to create instance handles") unless
105             my $instances = DBR::Config::Instance->load_from_db(
106             session => $self->{session},
107             dbr => $dbr,
108             parent_inst => $parent_inst
109             );
110              
111 17         45 my %schema_ids;
112 17         51 map {$schema_ids{ $_->schema_id } = 1 } @$instances;
  17         97  
113              
114 17 50       81 if(%schema_ids){
115 17 50       254 $self->_error("failed to create schema handles") unless
116             my $schemas = DBR::Config::Schema->load(
117             session => $self->{session},
118             schema_id => [keys %schema_ids],
119             instance => $parent_inst,
120             );
121             }
122              
123 17         6992 return 1;
124             }
125              
126             1;