| 1 | package DBIx::ParseDSN; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use v5.8.8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 4 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 4 | ▶ | use Module::Load::Conditional qw/can_load/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 4 | ▶ | use Class::Load qw/is_class_loaded/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 4 | ▶ | use DBIx::ParseDSN::Default; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 4 | ▶ | use base 'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 4 | our @EXPORT = qw/parse_dsn/; | |||||||||
| 13 | |||||||||||
| 14 | 4 | ▶ | use version; our $VERSION = qv('0.9.3'); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | ## this is really a utility function, but there is no ::Util module | ||||||||||
| 17 | ## yet, so it's here for now | ||||||||||
| 18 | 15 | ▶ | sub _split_dsn { | ||||||||
|
Subroutine
_split_dsn: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 15 | my $dsn = shift; | |||||||||
| 21 | 15 | my @parts = split /:/, $dsn, 3; | |||||||||
| 22 | |||||||||||
| 23 | 15 | return @parts; | |||||||||
| 24 | |||||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | ## a method to check health status of parsers DSN | ||||||||||
| 28 | 0 | ▶ | sub _dsn_sanity_check { | ||||||||
|
Subroutine
_dsn_sanity_check: not called CC=3 SCAR=24.8
| |||||||||||
| 29 | |||||||||||
| 30 | 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 | ▶ | if ( not defined $self->dsn ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 0 | carp "DSN isn't set"; | |||||||||
| 41 | 0 | return; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 0 | ▶ | if ( _split_dsn($self->dsn) != 3 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 0 | carp "DSN does not contain the expected pattern with 2 separating colons."; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 2 | ▶ | sub _default_parser { | ||||||||
|
Subroutine
_default_parser: called CC=1 SCAR=0.0
| |||||||||||
| 51 | 2 | return __PACKAGE__ . "::Default"; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 4 | ▶ | sub parse_dsn { | ||||||||
|
Subroutine
parse_dsn: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 55 | |||||||||||
| 56 | 4 | my($dsn,$user,$pass,$attr) = @_; | |||||||||
| 57 | |||||||||||
| 58 | ## decide driver | ||||||||||
| 59 | 4 | my($scheme,$driver) = _split_dsn($dsn); | |||||||||
| 60 | |||||||||||
| 61 | 4 | my $parser_module = __PACKAGE__ . "::" . $driver; | |||||||||
| 62 | |||||||||||
| 63 | 4 | ▶ | if ( not is_class_loaded($parser_module) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | |||||||||||
| 65 | 3 | ▶ | if ( not can_load modules => { $parser_module=>0 } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 2 | $parser_module = _default_parser; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 4 | return $parser_module->new($dsn); | |||||||||
| 72 | |||||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 4 | 1; # Magic true value required at end of module | |||||||||
| 76 | __END__ | ||||||||||