File Coverage

blib/lib/DBIx/Wrapper/Config.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             # Creation date: 2005-10-23 19:43:33
2             # Authors: don
3             #
4             # Copyright (c) 2005 Don Owens . All rights reserved.
5              
6             # This is free software; you can redistribute it and/or modify it
7             # under the same terms as Perl itself. See perlartistic.
8              
9             # This program is distributed in the hope that it will be
10             # useful, but WITHOUT ANY WARRANTY; without even the implied
11             # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12             # PURPOSE.
13              
14             =pod
15              
16             =head1 NAME
17              
18             DBIx::Wrapper::Config - Config Module for DBIx::Wrapper
19              
20             =head1 SYNOPSIS
21              
22             use DBIx::Wrapper::Config;
23              
24             my $dbh = DBIx::Wrapper::Config->connect($db_key, $conf_path, \%dbix_wrapper_attrs);
25              
26             =head1 DESCRIPTION
27              
28             This module allows you to create a configuration file in XML
29             specifying information required to connect to databases using
30             DBIx::Wrapper. This way you can keep your database connection
31             specifications in one place. Each "db" element specifies a
32             key/name for the database connection, which should be passed as
33             the $db_key argument to connect() in order to connect to that
34             database. The "db" element's children specify the dsn,
35             authentication, and attribute information.
36              
37            
38            
39            
40             dbi:mysql:database=test_db;host=example.com;port=3306
41              
42            
45            
46              
47             test_user
48             test_pwd
49              
50            
51            
52            
53            
54              
55            
56            
57              
58             test_user
59             test_pwd
60              
61            
62            
63            
64              
65            
66              
67              
68             =cut
69              
70 1     1   8199 use strict;
  1         2  
  1         37  
71 1     1   6 use warnings;
  1         3  
  1         148  
72              
73 1     1   19 use 5.006_00;
  1         8  
  1         102  
74              
75             package DBIx::Wrapper::Config;
76              
77             our $VERSION = '0.02';
78              
79 1     1   1434 use DBIx::Wrapper;
  1         72680  
  1         8  
80 1     1   1755 use XML::Parser::Wrapper;
  0            
  0            
81              
82             sub new {
83             my $proto = shift;
84            
85             return $proto->connect(@_);
86             }
87              
88             =pod
89              
90             =head2 connect($db_key, $conf_path, \%dbix_wrapper_attrs)
91              
92             Return a DBIx::Wrapper object connected to the database
93             specified by $db_key in the file at $conf_path.
94             %dbix_wrapper_attrs is the optional 5th argument to
95             DBIx::Wrapper's connect() method, specifying handlers, etc.
96              
97             The file specified by $conf_path should be in the format
98             specified in the DESCRIPTION section of this document.
99              
100             =cut
101             sub connect {
102             my $self = shift;
103             my $db_key = shift;
104             my $conf_path = shift;
105             my $wrapper_attrs = shift;
106              
107             return unless $db_key;
108              
109             my $conf = $self->_read_conf($conf_path);
110             unless ($conf and %$conf) {
111             die "\n\nread conf failed";
112             return;
113             }
114              
115             my $conf_entry = $conf->{$db_key};
116            
117             unless ($conf_entry) {
118             die "no conf entry";
119             }
120              
121             return DBIx::Wrapper->connect($conf_entry->{dsn}, $conf_entry->{user},
122             $conf_entry->{password}, $conf_entry->{attributes},
123             $wrapper_attrs);
124             }
125              
126             sub _read_conf {
127             my $self = shift;
128             my $conf_path = shift;
129              
130             unless (defined($conf_path) and $conf_path ne '') {
131             $conf_path = '/etc/dbix.conf.xml';
132             }
133              
134             return unless -r $conf_path;
135              
136             my $root = XML::Parser::Wrapper->new({ file => $conf_path });
137             unless ($root->name eq 'config') {
138             # bad format
139             return;
140             }
141              
142             my $dbs = {};
143             my $db_tags = $root->kids('db');
144             return unless $db_tags and @$db_tags;
145              
146             foreach my $db_element (@$db_tags) {
147             my $name = $db_element->attr('name');
148             next unless defined $name;
149              
150             my $dsn_element = $db_element->kid('dsn');
151             next unless $dsn_element;
152              
153             my $dsn;
154             my $dsn_attrs = $dsn_element->attrs;
155             if ($dsn_attrs and %$dsn_attrs) {
156             my $driver = $dsn_attrs->{driver};
157             # unless (defined($driver)) {
158             # $driver = 'mysql';
159             # }
160             my @keys = sort grep { $_ ne 'driver' } keys %$dsn_attrs;
161            
162             $dsn = "dbi:$driver:"
163             . join(';', map { "$_=$dsn_attrs->{$_}" } @keys);
164             }
165             else {
166             $dsn = $dsn_element->text;
167             }
168              
169             my $this_db = { dsn => $dsn };
170             $dbs->{$name} = $this_db;
171             $this_db->{user} = $db_element->kid('user')->text;
172             $this_db->{password} = $db_element->kid('password')->text;
173              
174             my $attributes = {};
175             $this_db->{attributes} = $attributes;
176             my $attribute_list = $db_element->kids('attribute');
177             if ($attribute_list and @$attribute_list) {
178             foreach my $attribute_element (@$attribute_list) {
179             $attributes->{$attribute_element->attr('name')}
180             = $attribute_element->attr('value');
181             }
182             }
183             }
184              
185             return $dbs;
186             }
187              
188             =pod
189              
190             =head1 EXAMPLES
191              
192              
193             =head1 DEPENDENCIES
194              
195             DBIx::Wrapper, XML::Parser::Wrapper
196              
197             =head1 AUTHOR
198              
199             Don Owens
200              
201             =head1 LICENSE AND COPYRIGHT
202              
203             Copyright (c) 2005 Don Owens . All rights reserved.
204              
205             This is free software; you can redistribute it and/or modify it
206             under the same terms as Perl itself. See perlartistic.
207              
208             This program is distributed in the hope that it will be
209             useful, but WITHOUT ANY WARRANTY; without even the implied
210             warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
211             PURPOSE.
212              
213             =head1 SEE ALSO
214              
215             DBIx::Wrapper, DBI
216              
217             =head1 VERSION
218              
219             0.02
220              
221             =cut
222              
223             1;
224              
225             # Local Variables: #
226             # mode: perl #
227             # tab-width: 4 #
228             # indent-tabs-mode: nil #
229             # cperl-indent-level: 4 #
230             # perl-indent-level: 4 #
231             # End: #
232             # vim:set ai si et sta ts=4 sw=4 sts=4: