File Coverage

blib/lib/DBIx/ParseDSN.pm
Criterion Covered Total %
statement 38 44 86.3
branch 4 8 50.0
condition n/a
subroutine 12 13 92.3
pod 1 1 100.0
total 55 66 83.3


line stmt bran cond sub pod time code
1             package DBIx::ParseDSN;
2              
3 4     4   564313 use v5.8.8;
  4         17  
  4         201  
4              
5 4     4   20 use warnings;
  4         7  
  4         93  
6 4     4   19 use strict;
  4         11  
  4         114  
7 4     4   20 use Carp;
  4         6  
  4         247  
8 4     4   4161 use Module::Load::Conditional qw/can_load/;
  4         111983  
  4         260  
9 4     4   3657 use Class::Load qw/is_class_loaded/;
  4         154184  
  4         269  
10 4     4   2829 use DBIx::ParseDSN::Default;
  4         16  
  4         145  
11 4     4   233 use base 'Exporter';
  4         9  
  4         445  
12             our @EXPORT = qw/parse_dsn/;
13              
14 4     4   21 use version; our $VERSION = qv('0.9.3');
  4         8  
  4         35  
15              
16             ## this is really a utility function, but there is no ::Util module
17             ## yet, so it's here for now
18             sub _split_dsn {
19              
20 15     15   24890 my $dsn = shift;
21 15         70 my @parts = split /:/, $dsn, 3;
22              
23 15         101 return @parts;
24              
25             }
26              
27             ## a method to check health status of parsers DSN
28             sub _dsn_sanity_check {
29              
30 0     0   0 my $self = shift;
31              
32             ## it should have three groups, separated by two colons, ie:
33             ## group1:group2:group3
34             ##
35             ## group1 is probably only ever "dbi"
36             ## group2 will be the driver followed by attributes
37             ## group3 will be driver specific options. group3 may include colons
38              
39 0 0       0 if ( not defined $self->dsn ) {
40 0         0 carp "DSN isn't set";
41 0         0 return;
42             }
43              
44 0 0       0 if ( _split_dsn($self->dsn) != 3 ) {
45 0         0 carp "DSN does not contain the expected pattern with 2 separating colons.";
46             }
47              
48             }
49              
50             sub _default_parser {
51 2     2   6 return __PACKAGE__ . "::Default";
52             }
53              
54             sub parse_dsn {
55              
56 4     4 1 7913 my($dsn,$user,$pass,$attr) = @_;
57              
58             ## decide driver
59 4         17 my($scheme,$driver) = _split_dsn($dsn);
60              
61 4         22 my $parser_module = __PACKAGE__ . "::" . $driver;
62              
63 4 100       28 if ( not is_class_loaded($parser_module) ) {
64              
65 3 100       1236 if ( not can_load modules => { $parser_module=>0 } ) {
66 2         877 $parser_module = _default_parser;
67             }
68              
69             }
70              
71 4         3110 return $parser_module->new($dsn);
72              
73             }
74              
75             1; # Magic true value required at end of module
76             __END__
77              
78             =encoding utf8
79              
80             =head1 NAME
81              
82             DBIx::ParseDSN - Parse DSN's, DBI connection strings.
83              
84             =head1 VERSION
85              
86             This document describes DBIx::ParseDSN version 0.9.3
87              
88             =head1 SYNOPSIS
89              
90             use DBIx::ParseDSN;
91              
92             my $dsn = parse_dsn("dbi:SQLite:database=/var/foo.db");
93              
94             $dsn->scheme; ## dbi
95             $dsn->driver; ## SQLite
96             $dsn->database; ## /var/foo.db
97              
98             $dsn->dbd_driver; ## DBD::SQLite
99              
100             ## information in user string
101             my $dsn2 = parse_dsn( 'dbi:Oracle:host=foobar;port=1521', 'scott@DB/tiger' )
102             $dsn2->database; ## DB
103             $dsn2->port; ## 1521
104             $dsn2->host; ## foobar
105              
106             ## uri connector
107             my $dsn3 = parse_dsn( 'dbi:Oracle://myhost:1522/ORCL' )
108             $dsn3->database; ## ORCL
109             $dsn3->port; ## 1522
110             $dsn3->host; ## myhost
111              
112             =head1 DESCRIPTION
113              
114             Exports parse_dsn that parses a DSN. It returns a
115             L<DBIx::ParseDSN::Default> that has attributes from the dsn.
116              
117             This module looks for parser classes of the form DBIx::ParseDSN::Foo,
118             where Foo literally matches the DSN driver, ie the 2nd part of the DSN
119             string. Case sensitive.
120              
121             Example: dbi:SQLite:database=/foo/bar would look for
122             DBIx::ParseDSN::SQLite and use that as a parser, if found.
123              
124             If DBIx::ParseDSN::Foo is loaded, it uses that. If the module can be
125             loaded, it will load it.
126              
127             It falls back to L<DBIx::ParseDSN::Default> if no specific parser is
128             found.
129              
130             To implement not supported DBI driver strings, subclass
131             L<DBIx::ParseDSN::Default> and reimplement C<sub parse> or change for
132             example C<sub names_for_database> if database is just called something
133             else.
134              
135             =head1 INTERFACE
136              
137             =head2 parse_dsn( $dsn );
138              
139             Parses a DSN and returns a L<DBIx::ParseDSN::Default> object that has
140             properties reflecting the parameters found in the DSN.
141              
142             See L<DBIx::ParseDSN::Default/DSN ATTRIBUTES> for details.
143              
144             =head1 BUGS AND LIMITATIONS
145              
146             No bugs have been reported.
147              
148             Please report any bugs or feature requests to
149             C<bug-dbix-parsedsn@rt.cpan.org>, or through the web interface at
150             L<http://rt.cpan.org>.
151              
152             =head1 SEE ALSO
153              
154             L<DBI>
155             L<DBIx::ParseDSN::Default>
156              
157             =head1 AUTHOR
158              
159             Torbjørn Lindahl C<< <torbjorn.lindahl@gmail.com> >>
160              
161             =head1 LICENCE AND COPYRIGHT
162              
163             Copyright (c) 2014, Torbjørn Lindahl C<< <torbjorn.lindahl@gmail.com> >>. All rights reserved.
164              
165             This module is free software; you can redistribute it and/or
166             modify it under the same terms as Perl itself. See L<perlartistic>.
167              
168              
169             =head1 DISCLAIMER OF WARRANTY
170              
171             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
172             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
173             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
174             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
175             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
176             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
177             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
178             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
179             NECESSARY SERVICING, REPAIR, OR CORRECTION.
180              
181             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
182             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
183             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
184             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
185             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
186             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
187             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
188             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
189             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
190             SUCH DAMAGES.