| 1 | package Fey::DBIManager; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | $Fey::DBIManager::VERSION = '0.16'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Fey::Exceptions qw( object_state_error param_error ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Fey::DBIManager::Source; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Moose 0.90; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use MooseX::SemiAffordanceAccessor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use MooseX::StrictConstructor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | has _sources => ( | ||||||||||||||||||||||||||||
| 20 | traits => ['Hash'], | ||||||||||||||||||||||||||||
| 21 | is => 'ro', | ||||||||||||||||||||||||||||
| 22 | isa => 'HashRef[Fey::DBIManager::Source]', | ||||||||||||||||||||||||||||
| 23 | 1 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | init_arg => undef, | |||||||||||||||||||||||||||
| 25 | handles => { | ||||||||||||||||||||||||||||
| 26 | get_source => 'get', | ||||||||||||||||||||||||||||
| 27 | add_source => 'set', | ||||||||||||||||||||||||||||
| 28 | remove_source => 'delete', | ||||||||||||||||||||||||||||
| 29 | _source_count => 'count', | ||||||||||||||||||||||||||||
| 30 | has_source => 'exists', | ||||||||||||||||||||||||||||
| 31 | sources => 'values', | ||||||||||||||||||||||||||||
| 32 | }, | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 4 | ▶ | around 'add_source' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.7
| |||||||||||||||||||||||||||||
| 36 | 4 | my $orig = shift; | |||||||||||||||||||||||||||
| 37 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 4 | my $source; | |||||||||||||||||||||||||||
| 40 | 4 | ▶ | if ( @_ > 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | $source = Fey::DBIManager::Source->new(@_); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | else { | ||||||||||||||||||||||||||||
| 44 | 4 | $source = shift; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 4 | my $name; | |||||||||||||||||||||||||||
| 48 | 4 | ▶ | if ( blessed $source && $source->can('name') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($source) and $source->can("name")
MC/DC0%0/2
builtin::blessed($source) and $source->can("name")
builtin::blessed($source)
$source->can("name")
Truth table33%1/3
A and B
A= builtin::blessed($source)B= $source->can("name")
| |||||||||||||||||||||||||||||
| 49 | 4 | $name = $source->name(); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 4 | ▶ | param_error qq{You already have a source named "$name".} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | if $self->has_source($name); | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 3 | my $return = $self->$orig( $name => $source ); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 3 | return $return; | |||||||||||||||||||||||||||
| 58 | 1 | }; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 9 | ▶ | sub default_source { | ||||||||||||||||||||||||||
|
Subroutine
default_source: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 9 | ▶ | if ( $self->_source_count() == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 64 | 1 | object_state_error | |||||||||||||||||||||||||||
| 65 | 'This manager has no default source because it has no sources at all.'; | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | elsif ( $self->_source_count() == 1 ) { | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # Need to force scalar context for the return value | ||||||||||||||||||||||||||||
| 70 | 1 | return ( $self->sources() )[0]; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | elsif ( my $source = $self->get_source('default') ) { | ||||||||||||||||||||||||||||
| 73 | 5 | return $source; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | else { | ||||||||||||||||||||||||||||
| 76 | 2 | object_state_error | |||||||||||||||||||||||||||
| 77 | 'This manager has multiple sources, but none are named "default".'; | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | return; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2 | ▶ | sub source_for_sql { | ||||||||||||||||||||||||||
|
Subroutine
source_for_sql: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2 | return $self->default_source(); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | __PACKAGE__->meta()->make_immutable(); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | 1; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # ABSTRACT: Manage a set of DBI handles | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =pod | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head1 NAME | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | Fey::DBIManager - Manage a set of DBI handles | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head1 VERSION | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | version 0.16 | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | my $manager = Fey::DBIManager->new(); | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | $manager->add_source( dsn => 'dbi:Pg:dbname=MyDB' ); | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | my $source = $manager->default_source(); | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | my $source = $manager->source_for_sql($select_sql); | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | C<Fey::DBIManager> manager a set of L<Fey::DBIManager::Source> | ||||||||||||||||||||||||||||
| 120 | objects, each of which in turn represents a single C<DBI> handle. | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | It's main purpose is to provide a single interface to one or more data | ||||||||||||||||||||||||||||
| 123 | sources, allowing you to easily define your database connections in | ||||||||||||||||||||||||||||
| 124 | one spot. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head1 METHODS | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | This class provides the following methods: | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head2 Fey::DBIManager->new() | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | Returns a new C<Fey::DBIManager> object. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head2 $manager->add_source(...) | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | This method adds a new L<Fey::DBIManager::Source> object to the | ||||||||||||||||||||||||||||
| 137 | manager. It can either accept an instantiated | ||||||||||||||||||||||||||||
| 138 | L<Fey::DBIManager::Source> object, or a set of parameters needed to | ||||||||||||||||||||||||||||
| 139 | create a new source. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Sources are identified by name, and if you try to add one that already | ||||||||||||||||||||||||||||
| 142 | exists in the manager, an error will be thrown. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head2 $manager->get_source($name) | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Given a source name, this returns the named source, if it exists in | ||||||||||||||||||||||||||||
| 147 | the manager. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head2 $manager->remove_source($name) | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Removes the named source, if it exists in the manager. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head2 $manager->has_source($name) | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | Returns true if the manager has the named source. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head2 $manager->sources() | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | Returns all of the source in the manager. | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head2 $manager->default_source() | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | This method returns the default source for the manager. If the manager | ||||||||||||||||||||||||||||
| 164 | has only one source, then this is the default. Otherwise it looks for | ||||||||||||||||||||||||||||
| 165 | a source named "default". If no such source exists, or if the manager | ||||||||||||||||||||||||||||
| 166 | has no sources at all, then an exception is thrown. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head2 $manager->source_for_sql($sql_object) | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | This method accepts a single C<Fey::SQL> object and returns an | ||||||||||||||||||||||||||||
| 171 | appropriate source. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | By default, this method simply returns the default source. It exists | ||||||||||||||||||||||||||||
| 174 | to provide a spot for subclasses which want to do something more | ||||||||||||||||||||||||||||
| 175 | clever, such as use one source for reads and another for writes. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head1 BUGS | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | Please report any bugs or feature requests to | ||||||||||||||||||||||||||||
| 180 | C<bug-fey-dbimanager@rt.cpan.org>, or through the web interface at | ||||||||||||||||||||||||||||
| 181 | L<http://rt.cpan.org>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 182 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | Dave Rolsky <autarch@urth.org> | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | This software is Copyright (c) 2011 by Dave Rolsky. | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =cut | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | __END__ | ||||||||||||||||||||||||||||