blib/lib/DBIx/Wrapper/Config.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 # Creation date: 2005-10-23 19:43:33
2 # Authors: don
3 #
4 # Copyright (c) 2005 Don Owens <don@regexguy.com>. 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 <?xml version="1.0" encoding="iso-8859-1"?>
38 <config>
39 <db name="test_db_key">
40 <dsn>dbi:mysql:database=test_db;host=example.com;port=3306</dsn>
41  
42 <!-- You can also use attributes to specify each part of the
43 dsn separately.
44 -->
45 <!-- <dsn driver="mysql" database="test_db" host="example.com" port="3306"/> -->
46  
47 <user>test_user</user>
48 <password>test_pwd</password>
49  
50 <!-- attributes to pass to DBIx::Wrapper (and ultimately to DBI) -->
51 <attribute name="RaiseError" value="0"/>
52 <attribute name="PrintError" value="1"/>
53 </db>
54  
55 <db name="test_db_key2">
56 <dsn driver="mysql" database="test_db" host="test.example.com" port="3306"/>
57  
58 <user>test_user</user>
59 <password>test_pwd</password>
60  
61 <attribute name="RaiseError" value="0"/>
62 <attribute name="PrintError" value="1"/>
63 </db>
64  
65 </config>
66  
67  
68 =cut
69  
70 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 1 ▶use 5.006_00;
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 package DBIx::Wrapper::Config;
76  
77 our $VERSION = '0.02';
78  
79 1 ▶use DBIx::Wrapper;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 0 ▶use XML::Parser::Wrapper;
Subroutine BEGIN: called CC=1 SCAR=2.6
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 <don@regexguy.com>
200  
201 =head1 LICENSE AND COPYRIGHT
202  
203 Copyright (c) 2005 Don Owens <don@regexguy.com>. 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:
↑ Summary