| 1 | |||||||||||||||||||||||||||||||||||||||||||
| 2 | package Class::DBI::Lite; | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 17 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 17 | ▶ | use warnings 'all'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 17 | ▶ | use base 'Ima::DBI::Contextual'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 17 | ▶ | use Carp qw( cluck confess ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 17 | ▶ | use SQL::Abstract; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 17 | ▶ | use SQL::Abstract::Limit; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 17 | ▶ | use Class::DBI::Lite::Iterator; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 17 | ▶ | use Class::DBI::Lite::Pager; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 17 | ▶ | use Class::DBI::Lite::RootMeta; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 17 | ▶ | use Class::DBI::Lite::EntityMeta; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 17 | ▶ | use Digest::MD5 'md5_hex'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 17 | ▶ | use POSIX 'ceil'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | use overload | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 39 | ▶ | '""' => sub { eval { $_[0]->id } }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 320203 | ▶ | bool => sub { eval { $_[0]->id } }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 17 | fallback => 1; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 17 | our $VERSION = '1.034'; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 17 | our $meta; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 17 | our %DBI_OPTIONS = ( | |||||||||||||||||||||||||||||||||||||||||
| 25 | FetchHashKeyName => 'NAME_lc', | ||||||||||||||||||||||||||||||||||||||||||
| 26 | ShowErrorStatement => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | ChopBlanks => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 28 | AutoCommit => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 29 | RaiseError => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 17 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 17 | ▶ | use vars qw( $Weaken_Is_Available %Live_Objects ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 17 | $Weaken_Is_Available = 1; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 17 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 37 | 17 | require Scalar::Util; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 17 | import Scalar::Util qw(weaken isweak); | |||||||||||||||||||||||||||||||||||||||||
| 39 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 17 | ▶ | $Weaken_Is_Available = 0 if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | }# end BEGIN: | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # Abstract methods: | ||||||||||||||||||||||||||||||||||||||||||
| 46 | sub set_up_table; | ||||||||||||||||||||||||||||||||||||||||||
| 47 | sub get_last_insert_id; | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 51 | sub import | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 62 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 62 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 17 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 62 | $class->_load_class( ( @{$class.'::ISA'} )[0] ); | |||||||||||||||||||||||||||||||||||||||||
| 57 | 62 | ▶ | if( my $table = eval { ( @{$class.'::ISA'} )[0]->table } ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | { | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | $class->set_up_table( $table ); | |||||||||||||||||||||||||||||||||||||||||
| 60 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 61 | }# end import() | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 65 | sub clear_object_index | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 3 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clear_object_index: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 3 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 3 | ▶ | my $class = ref($s) ? ref($s) : $s; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 3 | my $key_starter = $s->root_meta->schema . ":" . $class; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 3 | map { delete($Live_Objects{$_}) } grep { m/^$key_starter\:\d+/o } keys(%Live_Objects); | |||||||||||||||||||||||||||||||||||||||||
| 72 | }# end clear_object_index() | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 76 | sub find_column | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 52 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_column: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 52 | my ($class, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 52 | ▶ | my ($col) = grep { $_ eq $name } $class->columns('All') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | or return; | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 16 | return $col; | |||||||||||||||||||||||||||||||||||||||||
| 83 | }# end find_column() | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 87 | sub construct | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 112613 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
construct: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 112613 | my ($s, $data, $is_void_context) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 112613 | ▶ | my $class = ref($s) ? ref($s) : $s; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 112613 | my $PK = $class->primary_column; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 112613 | my $key = join ':', grep { defined($_) } ( $s->root_meta->schema, $class, $data->{ $PK } ); | |||||||||||||||||||||||||||||||||||||||||
| 95 | 112613 | ▶ | return $Live_Objects{$key} if $Live_Objects{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 2590 | $data->{__id} = $data->{ $PK }; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 2590 | $data->{__Changed} = { }; | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 2590 | my $obj = bless $data, $class; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 2590 | ▶ | if( $Weaken_Is_Available && ! $is_void_context ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$Class::DBI::Lite::Weaken_Is_Available and not $is_void_context
MC/DC50%1/2
$Class::DBI::Lite::Weaken_Is_Available and not $is_void_context
$Class::DBI::Lite::Weaken_Is_Available
not $is_void_context
Truth table66%2/3
A and not(B)
A= $Class::DBI::Lite::Weaken_Is_AvailableB= not $is_void_context
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 2455 | $Live_Objects{$key} = $obj; | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 2455 | weaken( $Live_Objects{$key} ); | |||||||||||||||||||||||||||||||||||||||||
| 106 | 2455 | return $Live_Objects{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | else | ||||||||||||||||||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 135 | return $obj; | |||||||||||||||||||||||||||||||||||||||||
| 111 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 112 | }# end construct() | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 116 | sub deconstruct | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 203 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
deconstruct: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 203 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 203 | bless $s, 'Class::DBI::Lite::Object::Has::Been::Deleted'; | |||||||||||||||||||||||||||||||||||||||||
| 121 | }# end deconstruct() | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 2 | ▶ | sub schema { $_[0]->root_meta->schema } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
schema: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 2 | ▶ | sub dsn { $_[0]->root_meta->dsn } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dsn: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 11812 | ▶ | sub table { $_[0]->_meta->{table} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
table: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 4 | ▶ | sub triggers { @{ $_[0]->_meta->{triggers}->{ $_[1] } } } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
triggers: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 109 | ▶ | sub _meta { } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_meta: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 1 | ▶ | sub set_cache { my ($class, $cache) = @_; $class->_meta->{cache} = $cache } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_cache: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 200063 | ▶ | sub cache { shift->_meta->{cache} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cache: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 135 | sub _init_meta | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 51 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init_meta: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 51 | my ($class, $entity) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 17 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 17 | ▶ | no warnings qw( once redefine ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 51 | my $schema = $class->root_meta->schema; | |||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 51 | my $_class_meta = Class::DBI::Lite::EntityMeta->new( $class, $schema, $entity ); | |||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | # If we are re-initializing meta (i.e. changed schema) then remove accessors first: | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 50 | foreach my $col ( eval { $class->columns } ) | |||||||||||||||||||||||||||||||||||||||||
| 147 | { | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 3 | ▶ | local $SIG{__WARN__} = sub { }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 3 | *{"$class\::$col"} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 150 | }# end foreach() | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 50 | ▶ | *{"$class\::_meta"} = sub { $_class_meta }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 50 | my $pk = ($class->columns('Primary'))[0]; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 50 | ▶ | *{"$class\::primary_column"} = sub { $pk }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 50 | ▶ | *{"$class\::$pk"} = sub { $_[0]->{$pk} }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | # Install the column accessors: | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 50 | foreach my $col ( grep { $_ ne $pk } $class->columns ) | |||||||||||||||||||||||||||||||||||||||||
| 160 | { | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 104 | my $setter = "_set_$col"; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 104 | my $getter = "_get_$col"; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 104 | ▶ | *{"$class\::$setter"} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 1165 | my ($s, $newval) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 165 | 17 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 1165 | ▶ | return $newval if $newval eq $s->{$col}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 25 | $s->_call_triggers( "before_set_$col", $s->{$col}, $newval ); | |||||||||||||||||||||||||||||||||||||||||
| 168 | $s->{__Changed}->{$col} = { | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 25 | oldval => $s->{$col} | |||||||||||||||||||||||||||||||||||||||||
| 170 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 25 | return $s->{$col} = $newval; | |||||||||||||||||||||||||||||||||||||||||
| 172 | 104 | }; | |||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | 104 | ▶ | *{"$class\::$getter"} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 1282 | shift->{$col}; | |||||||||||||||||||||||||||||||||||||||||
| 176 | 104 | }; | |||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 104 | ▶ | *{"$class\::$col"} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 2447 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | 2447 | ▶ | exists($s->{$col}) or $s->_flesh_out; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 2447 | ▶ | @_ ? $s->$setter( @_ ) : $s->$getter( @_ ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | 104 | }; | |||||||||||||||||||||||||||||||||||||||||
| 184 | }# end foreach() | ||||||||||||||||||||||||||||||||||||||||||
| 185 | }# end _init_meta() | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 189 | sub connection | ||||||||||||||||||||||||||||||||||||||||||
| 190 | 18 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
connection: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | 18 | my ($class, @DSN) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | 18 | ▶ | return $class->db_Main unless @DSN; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | 18 | $class->set_master( @DSN ); | |||||||||||||||||||||||||||||||||||||||||
| 196 | 18 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 197 | }# end connection() | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | sub db_RO | ||||||||||||||||||||||||||||||||||||||||||
| 201 | 10275 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
db_RO: called CC=4 SCAR=17.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 10275 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | # If we're inside a transaction or don't have any slaves, return the master: | ||||||||||||||||||||||||||||||||||||||||||
| 205 | 10275 | ▶ | unless( $s->db_Main->{AutoCommit} && $s->root_meta->has_slaves ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$s->db_Main->{"AutoCommit"} and $s->root_meta->has_slaves
MC/DC0%0/2
$s->db_Main->{"AutoCommit"} and $s->root_meta->has_slaves
$s->db_Main->{"AutoCommit"}
$s->root_meta->has_slaves
Truth table66%2/3
A and B
A= $s->db_Main->{"AutoCommit"}B= $s->root_meta->has_slaves
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | { | ||||||||||||||||||||||||||||||||||||||||||
| 207 | 10275 | return $s->db_Main; | |||||||||||||||||||||||||||||||||||||||||
| 208 | }# end unless() | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | # Otherwise return the slave if we have any: | ||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | ▶ | $s->root_meta->has_slaves ? $s->db_Slave : $s->db_Main; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | }# end db_RO() | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | sub set_master | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 18 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_master: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 18 | my ($class, @DSN) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 218 | 18 | my $root = $class->root_meta; | |||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | 18 | $class->_mk_connection('Main', @DSN); | |||||||||||||||||||||||||||||||||||||||||
| 221 | }# end set_master() | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | sub set_slaves | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_slaves: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | my ($class, @connections) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | my $root = $class->root_meta; | |||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | $root->add_slave( $_ ) for grep { $_ } @connections; | |||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | # Select a connection at random and use it: | ||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | my $conn = $connections[ int(rand() * @connections) - 1 ]; | |||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | $class->_mk_connection('Slave', @$conn); | |||||||||||||||||||||||||||||||||||||||||
| 234 | }# end set_slaves() | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | sub switch_slave | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
switch_slave: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | my ($class, $trace) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | my $old_slave = $class->root_meta->slaves->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | $class->_mk_connection('Slave', @{ $class->root_meta->random_slave } ); | |||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | my $new_slave = $class->root_meta->slaves->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | warn "[Debug] Switched slave from $old_slave->[0] to $new_slave->[0]\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | if $trace; | ||||||||||||||||||||||||||||||||||||||||||
| 247 | }# end switch_slave() | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | 17 | our $root_metas = { }; | |||||||||||||||||||||||||||||||||||||||||
| 250 | sub _mk_connection | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 18 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mk_connection: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 18 | my ($class, $name, @DSN) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | # Set up the root meta: | ||||||||||||||||||||||||||||||||||||||||||
| 255 | 17 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 17 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | # unless( $class->_has_root_meta ) | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 18 | my $meta_key = join ':', ( $class ); | |||||||||||||||||||||||||||||||||||||||||
| 259 | 18 | ▶ | unless( $root_metas->{ $meta_key } ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | { | ||||||||||||||||||||||||||||||||||||||||||
| 261 | 17 | $root_metas->{$meta_key} = Class::DBI::Lite::RootMeta->new( | |||||||||||||||||||||||||||||||||||||||||
| 262 | \@DSN | ||||||||||||||||||||||||||||||||||||||||||
| 263 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 264 | 17 | my $caller = caller(2); | |||||||||||||||||||||||||||||||||||||||||
| 265 | 17 | ▶ | *{ "$caller\::root" } = sub { $caller }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 17 | ▶ | *{ $class->root . "::root_meta" } = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | # use Data::Dumper; warn Dumper($root_metas); | ||||||||||||||||||||||||||||||||||||||||||
| 268 | 223155 | return $root_metas->{$meta_key}; | |||||||||||||||||||||||||||||||||||||||||
| 269 | 17 | }; | |||||||||||||||||||||||||||||||||||||||||
| 270 | # ${ $class->root . "::_has_root_meta" } = 1; | ||||||||||||||||||||||||||||||||||||||||||
| 271 | }# end unless() | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | # Connect: | ||||||||||||||||||||||||||||||||||||||||||
| 274 | 18 | undef(%Live_Objects); | |||||||||||||||||||||||||||||||||||||||||
| 275 | 18 | local $^W = 0; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 18 | $class->set_db($name => @DSN, { | |||||||||||||||||||||||||||||||||||||||||
| 277 | RaiseError => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 278 | AutoCommit => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 279 | PrintError => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 280 | Taint => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 281 | # RootClass => "DBIx::ContextualFetch" | ||||||||||||||||||||||||||||||||||||||||||
| 282 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 283 | }# end _mk_connection() | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 287 | sub root | ||||||||||||||||||||||||||||||||||||||||||
| 288 | 16 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
root: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 16 | __PACKAGE__; | |||||||||||||||||||||||||||||||||||||||||
| 290 | }# end root() | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 294 | sub root_meta | ||||||||||||||||||||||||||||||||||||||||||
| 295 | 16 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
root_meta: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | 16 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | 17 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | 16 | my $root = $s->root; | |||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | 16 | ${"$root\::root_meta"}; | |||||||||||||||||||||||||||||||||||||||||
| 302 | }# end root_meta() | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | ▶ | sub _has_root_meta { no strict 'refs'; my $root = $_[0]->root; ${"$root\::_has_root_meta"} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
Subroutine
_has_root_meta: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 308 | sub id | ||||||||||||||||||||||||||||||||||||||||||
| 309 | 332036 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 310 | 332036 | $_[0]->{ $_[0]->primary_column }; | |||||||||||||||||||||||||||||||||||||||||
| 311 | }# end id() | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 315 | 17 | my %ok_types = ( | |||||||||||||||||||||||||||||||||||||||||
| 316 | All => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 317 | Essential => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 318 | Primary => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 319 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 320 | sub columns | ||||||||||||||||||||||||||||||||||||||||||
| 321 | 12982 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
columns: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | 12982 | my ($s) = shift; | |||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | 12982 | ▶ | if( my $type = shift(@_) ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | { | ||||||||||||||||||||||||||||||||||||||||||
| 327 | 12877 | ▶ | confess "Unknown column group '$type'" unless $ok_types{$type}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 328 | 12875 | ▶ | if( my @cols = @_ ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | { | ||||||||||||||||||||||||||||||||||||||||||
| 330 | 4 | $s->_meta->columns->{$type} = \@cols; | |||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||
| 332 | else | ||||||||||||||||||||||||||||||||||||||||||
| 333 | { | ||||||||||||||||||||||||||||||||||||||||||
| 334 | # Get: my ($PK) = $class->columns('Primary'); | ||||||||||||||||||||||||||||||||||||||||||
| 335 | 12871 | return @{ $s->_meta->columns->{$type} }; | |||||||||||||||||||||||||||||||||||||||||
| 336 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||||||||||||
| 338 | else | ||||||||||||||||||||||||||||||||||||||||||
| 339 | { | ||||||||||||||||||||||||||||||||||||||||||
| 340 | 105 | return @{ $s->_meta->columns->{All} }; | |||||||||||||||||||||||||||||||||||||||||
| 341 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | }# end columns() | ||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 347 | sub retrieve_all | ||||||||||||||||||||||||||||||||||||||||||
| 348 | 32 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
retrieve_all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 349 | 32 | my ($s) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | 32 | return $s->retrieve_from_sql( ); | |||||||||||||||||||||||||||||||||||||||||
| 352 | }# end retrieve_all() | ||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 356 | sub retrieve | ||||||||||||||||||||||||||||||||||||||||||
| 357 | 10122 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
retrieve: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 358 | 10122 | my ($s, $id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | 10122 | ▶ | if( my $data = $s->_call_triggers( before_retrieve => $id ) ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 361 | { | ||||||||||||||||||||||||||||||||||||||||||
| 362 | 0 | return $s->construct( $data ); | |||||||||||||||||||||||||||||||||||||||||
| 363 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | 10122 | my ($obj) = $s->retrieve_from_sql(<<"", $id); | |||||||||||||||||||||||||||||||||||||||||
| 366 | 10122 | @{[ $s->primary_column ]} = ? | |||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | 10122 | ▶ | return unless $obj; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 369 | 10122 | $obj->_call_triggers( after_retrieve => $obj ); | |||||||||||||||||||||||||||||||||||||||||
| 370 | 10122 | return $obj; | |||||||||||||||||||||||||||||||||||||||||
| 371 | }# end retrieve() | ||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 375 | sub create | ||||||||||||||||||||||||||||||||||||||||||
| 376 | 1257 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create: called CC=9 SCAR=22.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 377 | 1257 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||
| 379 | 1257 | ▶ | my $data = ref($_[0]) ? $_[0] : { @_ }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | 1257 | my $PK = $s->primary_column; | |||||||||||||||||||||||||||||||||||||||||
| 382 | 2527 | my %create_fields = map { $_ => $data->{$_} } | |||||||||||||||||||||||||||||||||||||||||
| 383 | 1257 | ▶ | grep { exists($data->{$_}) && $_ ne $PK } | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
exists $data->{$_} && $_ ne $PK
MC/DC100%2/2
exists $data->{$_} && $_ ne $PK
exists $data->{$_}
$_ ne $PK
Truth table100%3/3
A && B
A= exists $data->{$_}B= $_ ne $PK
| |||||||||||||||||||||||||||||||||||||||||||
| 384 | $s->columns('All'); | ||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | 1257 | ▶ | my $pre_obj = bless { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 387 | __id => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 388 | __Changed => { }, | ||||||||||||||||||||||||||||||||||||||||||
| 389 | %create_fields | ||||||||||||||||||||||||||||||||||||||||||
| 390 | }, ref($s) ? ref($s) : $s; | ||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||
| 392 | # Cal the "before" trigger: | ||||||||||||||||||||||||||||||||||||||||||
| 393 | 1257 | $pre_obj->_call_triggers( before_create => \%create_fields ); | |||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | # Changes may have happened to the original creation data (from the trigger(s)) - re-evaluate now: | ||||||||||||||||||||||||||||||||||||||||||
| 396 | 2525 | %create_fields = map { $_ => $pre_obj->{$_} } | |||||||||||||||||||||||||||||||||||||||||
| 397 | 1256 | ▶ | grep { defined($pre_obj->{$_}) && $_ ne $PK } | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $pre_obj->{$_} && $_ ne $PK
MC/DC50%1/2
defined $pre_obj->{$_} && $_ ne $PK
defined $pre_obj->{$_}
$_ ne $PK
Truth table66%2/3
A && B
A= defined $pre_obj->{$_}B= $_ ne $PK
| |||||||||||||||||||||||||||||||||||||||||||
| 398 | $pre_obj->columns('All'); | ||||||||||||||||||||||||||||||||||||||||||
| 399 | 1256 | $data = { %$pre_obj }; | |||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | 1256 | my @fields = map { $_ } sort grep { exists($data->{$_}) } keys(%create_fields); | |||||||||||||||||||||||||||||||||||||||||
| 402 | 1256 | my @vals = map { $data->{$_} } sort grep { exists($pre_obj->{$_}) } keys(%create_fields); | |||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||
| 404 | 1256 | my $sql = <<""; | |||||||||||||||||||||||||||||||||||||||||
| 405 | 1256 | INSERT INTO @{[ $s->table ]} ( | |||||||||||||||||||||||||||||||||||||||||
| 406 | 1256 | @{[ join ',', @fields ]} | |||||||||||||||||||||||||||||||||||||||||
| 407 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 408 | VALUES ( | ||||||||||||||||||||||||||||||||||||||||||
| 409 | 1256 | @{[ join ',', map {"?"} @vals ]} | |||||||||||||||||||||||||||||||||||||||||
| 410 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | 1256 | ▶ | if( $s->_meta->trace ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 413 | { | ||||||||||||||||||||||||||||||||||||||||||
| 414 | 0 | ▶ | my $class = ref($s) || $s; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $s || $s
MC/DC0%0/2
ref $s || $s
ref $s
$s
Truth table0%0/3
A || B
A= ref $sB= $s
| |||||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | cluck "$class: create($sql, values[" . join( ",", map {qq('$_')} @vals) . "])"; | |||||||||||||||||||||||||||||||||||||||||
| 416 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | 1256 | my $sth = $s->db_Main->prepare_cached( $sql ); | |||||||||||||||||||||||||||||||||||||||||
| 419 | 1256 | $sth->execute( @vals ); | |||||||||||||||||||||||||||||||||||||||||
| 420 | 1256 | ▶ | my $id = $s->get_last_insert_id | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 421 | or confess "ERROR - CANNOT get last insert id"; | ||||||||||||||||||||||||||||||||||||||||||
| 422 | 1256 | $sth->finish(); | |||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||
| 424 | 1256 | my $new_obj = $s->construct( { | |||||||||||||||||||||||||||||||||||||||||
| 425 | %$pre_obj, | ||||||||||||||||||||||||||||||||||||||||||
| 426 | $PK => $id, | ||||||||||||||||||||||||||||||||||||||||||
| 427 | }, defined wantarray ); | ||||||||||||||||||||||||||||||||||||||||||
| 428 | 1256 | $pre_obj->discard_changes; | |||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | 1256 | $new_obj->_call_triggers( after_create => $new_obj ); | |||||||||||||||||||||||||||||||||||||||||
| 431 | 1256 | ▶ | $new_obj->update if $new_obj->{__Changed}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 432 | 1256 | $new_obj; | |||||||||||||||||||||||||||||||||||||||||
| 433 | }# end create() | ||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||
| 436 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 437 | sub do_transaction | ||||||||||||||||||||||||||||||||||||||||||
| 438 | 20 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_transaction: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 439 | 20 | my ($s, $code) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||
| 441 | 20 | local $s->db_Main->{AutoCommit}; | |||||||||||||||||||||||||||||||||||||||||
| 442 | 20 | my ($res, @res); | |||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | ▶ | wantarray ? @res = eval { $code->( ) } : $res = eval { $code->( ) }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||
| 445 | 20 | ▶ | if( my $trans_error = $@ ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 446 | { | ||||||||||||||||||||||||||||||||||||||||||
| 447 | 1 | eval { $s->dbi_rollback }; | |||||||||||||||||||||||||||||||||||||||||
| 448 | 1 | ▶ | if( my $rollback_error = $@ ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 449 | { | ||||||||||||||||||||||||||||||||||||||||||
| 450 | 0 | confess join "\n\t", "Both transaction and rollback failed:", | |||||||||||||||||||||||||||||||||||||||||
| 451 | "Transaction error: $trans_error", | ||||||||||||||||||||||||||||||||||||||||||
| 452 | "Rollback Error: $rollback_error"; | ||||||||||||||||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||||||||||||||||
| 454 | else | ||||||||||||||||||||||||||||||||||||||||||
| 455 | { | ||||||||||||||||||||||||||||||||||||||||||
| 456 | 1 | confess join "\n\t", "Transaction failed but rollback succeeded:", | |||||||||||||||||||||||||||||||||||||||||
| 457 | "Transaction error: $trans_error"; | ||||||||||||||||||||||||||||||||||||||||||
| 458 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||||||||||||
| 460 | else | ||||||||||||||||||||||||||||||||||||||||||
| 461 | { | ||||||||||||||||||||||||||||||||||||||||||
| 462 | # Success: | ||||||||||||||||||||||||||||||||||||||||||
| 463 | 19 | $s->dbi_commit; | |||||||||||||||||||||||||||||||||||||||||
| 464 | 19 | ▶ | wantarray ? return @res : return $res; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 465 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 466 | }# end do_transaction() | ||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 470 | sub update | ||||||||||||||||||||||||||||||||||||||||||
| 471 | 1279 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update: called CC=7 SCAR=19.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 472 | 1279 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 473 | 1279 | ▶ | confess "$s\->update cannot be called without an object" unless ref($s); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||
| 475 | 1278 | ▶ | return 1 unless eval { keys(%{ $s->{__Changed} }) }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||
| 477 | 6 | $s->_call_triggers( before_update => $s ); | |||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | 5 | my $changed = $s->{__Changed}; | |||||||||||||||||||||||||||||||||||||||||
| 480 | 5 | foreach my $field ( grep { $changed->{$_} } sort keys(%$s) ) | |||||||||||||||||||||||||||||||||||||||||
| 481 | { | ||||||||||||||||||||||||||||||||||||||||||
| 482 | 5 | $s->_call_triggers( "before_update_$field", $changed->{$field}->{oldval}, $s->{$field} ); | |||||||||||||||||||||||||||||||||||||||||
| 483 | }# end foreach() | ||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||
| 486 | # Make our SQL: | ||||||||||||||||||||||||||||||||||||||||||
| 487 | 5 | my @fields = map { "$_ = ?" } grep { $changed->{$_} } sort keys(%$s); | |||||||||||||||||||||||||||||||||||||||||
| 488 | 5 | my @vals = map { $s->{$_} } grep { $changed->{$_} } sort keys(%$s); | |||||||||||||||||||||||||||||||||||||||||
| 489 | 5 | my $sql = <<""; | |||||||||||||||||||||||||||||||||||||||||
| 490 | 5 | UPDATE @{[ $s->table ]} SET | |||||||||||||||||||||||||||||||||||||||||
| 491 | 5 | @{[ join ', ', @fields ]} | |||||||||||||||||||||||||||||||||||||||||
| 492 | 5 | WHERE @{[ $s->primary_column ]} = ? | |||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||
| 494 | 5 | ▶ | if( $s->_meta->trace ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 495 | { | ||||||||||||||||||||||||||||||||||||||||||
| 496 | 0 | ▶ | my $class = ref($s) || $s; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $s || $s
MC/DC0%0/2
ref $s || $s
ref $s
$s
Truth table0%0/3
A || B
A= ref $sB= $s
| |||||||||||||||||||||||||||||||||||||||||||
| 497 | 0 | cluck "$class: update($sql, values[" . join( ",", map {qq('$_')} @vals) . "])"; | |||||||||||||||||||||||||||||||||||||||||
| 498 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 499 | 5 | my $sth = $s->db_Main->prepare_cached( $sql ); | |||||||||||||||||||||||||||||||||||||||||
| 500 | 5 | $sth->execute( @vals, $s->id ); | |||||||||||||||||||||||||||||||||||||||||
| 501 | 5 | $sth->finish(); | |||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||
| 503 | 5 | foreach my $field ( grep { $changed->{$_} } sort keys(%$s) ) | |||||||||||||||||||||||||||||||||||||||||
| 504 | { | ||||||||||||||||||||||||||||||||||||||||||
| 505 | 5 | my $old_val = $changed->{$field}->{oldval}; | |||||||||||||||||||||||||||||||||||||||||
| 506 | 5 | $s->_call_triggers( "after_update_$field", $old_val, $s->{$field} ); | |||||||||||||||||||||||||||||||||||||||||
| 507 | }# end foreach() | ||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||
| 509 | 5 | $s->{__Changed} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 510 | 5 | $s->_call_triggers( after_update => $s ); | |||||||||||||||||||||||||||||||||||||||||
| 511 | 5 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 512 | }# end update() | ||||||||||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||
| 515 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 516 | sub delete | ||||||||||||||||||||||||||||||||||||||||||
| 517 | 205 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 518 | 205 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||
| 520 | 205 | ▶ | confess "$s\->delete cannot be called without an object" unless ref($s); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||
| 522 | 204 | $s->_call_triggers( before_delete => $s ); | |||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||
| 524 | 203 | my $sql = <<""; | |||||||||||||||||||||||||||||||||||||||||
| 525 | 203 | DELETE FROM @{[ $s->table ]} | |||||||||||||||||||||||||||||||||||||||||
| 526 | 203 | WHERE @{[ $s->primary_column ]} = ? | |||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||
| 528 | 203 | ▶ | if( $s->_meta->trace ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 529 | { | ||||||||||||||||||||||||||||||||||||||||||
| 530 | 0 | ▶ | my $class = ref($s) || $s; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $s || $s
MC/DC0%0/2
ref $s || $s
ref $s
$s
Truth table0%0/3
A || B
A= ref $sB= $s
| |||||||||||||||||||||||||||||||||||||||||||
| 531 | 0 | cluck "$class: delete($sql, values[" . $s->id . "])\n"; | |||||||||||||||||||||||||||||||||||||||||
| 532 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 533 | 203 | my $sth = $s->db_Main->prepare_cached( $sql ); | |||||||||||||||||||||||||||||||||||||||||
| 534 | 203 | $sth->execute( $s->id ); | |||||||||||||||||||||||||||||||||||||||||
| 535 | 203 | $sth->finish(); | |||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||
| 537 | 203 | my $deleted = bless { $s->primary_column => $s->id }, ref($s); | |||||||||||||||||||||||||||||||||||||||||
| 538 | 203 | my $key = join ':', grep { defined($_) } ($s->root_meta->{schema}, ref($s), $s->id ); | |||||||||||||||||||||||||||||||||||||||||
| 539 | 203 | $s->_call_triggers( after_delete => $deleted ); | |||||||||||||||||||||||||||||||||||||||||
| 540 | 203 | delete($Live_Objects{$key}); | |||||||||||||||||||||||||||||||||||||||||
| 541 | 203 | undef(%$deleted); | |||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||
| 543 | 203 | undef(%$s); | |||||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||||||
| 545 | 203 | $s->deconstruct; | |||||||||||||||||||||||||||||||||||||||||
| 546 | }# end delete() | ||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||||||
| 549 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 550 | sub ad_hoc | ||||||||||||||||||||||||||||||||||||||||||
| 551 | 2 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ad_hoc: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 552 | 2 | my ($s, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||||
| 554 | 2 | my $sth = $s->db_RO->prepare( $args{sql} ); | |||||||||||||||||||||||||||||||||||||||||
| 555 | 2 | ▶ | $args{args} ||= [ ]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"args"} ||= []
MC/DC0%0/1
$args{"args"} ||= []
$args{"args"}
Truth table50%1/2
A
A= $args{"args"}
| |||||||||||||||||||||||||||||||||||||||||||
| 556 | 2 | ▶ | $args{isa} ||= 'Class::DBI::Lite'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{"isa"} ||= "Class::DBI::Lite"
MC/DC100%1/1
$args{"isa"} ||= "Class::DBI::Lite"
$args{"isa"}
Truth table100%2/2
A
A= $args{"isa"}
| |||||||||||||||||||||||||||||||||||||||||||
| 557 | 2 | $sth->execute( @{ $args{args} } ); | |||||||||||||||||||||||||||||||||||||||||
| 558 | 2 | my @data = ( ); | |||||||||||||||||||||||||||||||||||||||||
| 559 | 2 | require Class::DBI::Lite::AdHocEntity; | |||||||||||||||||||||||||||||||||||||||||
| 560 | 2 | ▶ | while( my $rec = $sth->fetchrow_hashref ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 561 | { | ||||||||||||||||||||||||||||||||||||||||||
| 562 | push @data, Class::DBI::Lite::AdHocEntity->new( | ||||||||||||||||||||||||||||||||||||||||||
| 563 | isa => $args{isa}, | ||||||||||||||||||||||||||||||||||||||||||
| 564 | sql => \$args{sql}, | ||||||||||||||||||||||||||||||||||||||||||
| 565 | args => $args{args}, | ||||||||||||||||||||||||||||||||||||||||||
| 566 | primary_key => $args{primary_key}, | ||||||||||||||||||||||||||||||||||||||||||
| 567 | 2 | data => $rec, | |||||||||||||||||||||||||||||||||||||||||
| 568 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 569 | }# end while() | ||||||||||||||||||||||||||||||||||||||||||
| 570 | 2 | $sth->finish(); | |||||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||||||
| 572 | 2 | ▶ | return wantarray ? @data : Class::DBI::Lite::Iterator->new( \@data ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 573 | }# end ad_hoc() | ||||||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||||||||||
| 576 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 577 | sub retrieve_from_sql | ||||||||||||||||||||||||||||||||||||||||||
| 578 | 10249 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
retrieve_from_sql: called CC=4 SCAR=15.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 579 | 10249 | my ($s, $sql, @bind) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||
| 581 | 10249 | $sql = "SELECT @{[ join ', ', $s->columns('Essential') ]} " . | |||||||||||||||||||||||||||||||||||||||||
| 582 | 10249 | ▶ | "FROM @{[ $s->table ]}" . ( $sql ? " WHERE $sql " : "" ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||||||||||||||||
| 584 | 10249 | ▶ | if( $s->_meta->trace ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 585 | { | ||||||||||||||||||||||||||||||||||||||||||
| 586 | 0 | ▶ | my $class = ref($s) || $s; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $s || $s
MC/DC0%0/2
ref $s || $s
ref $s
$s
Truth table0%0/3
A || B
A= ref $sB= $s
| |||||||||||||||||||||||||||||||||||||||||||
| 587 | 0 | cluck "$class: search*($sql, values[" . join( ",", map {qq('$_')} @bind) . "])"; | |||||||||||||||||||||||||||||||||||||||||
| 588 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 589 | SCOPE: { | ||||||||||||||||||||||||||||||||||||||||||
| 590 | 10249 | my $sth = $s->db_RO->prepare_cached( $sql ); | |||||||||||||||||||||||||||||||||||||||||
| 591 | 10249 | $sth->execute( @bind ); | |||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||
| 593 | 10249 | return $s->sth_to_objects( $sth, $sql ); | |||||||||||||||||||||||||||||||||||||||||
| 594 | } | ||||||||||||||||||||||||||||||||||||||||||
| 595 | }# end retrieve_from_sql() | ||||||||||||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||
| 598 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 599 | sub sth_to_objects | ||||||||||||||||||||||||||||||||||||||||||
| 600 | 10269 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sth_to_objects: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 601 | 10269 | my ($s, $sth, $sql) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||||||||||||
| 603 | 10269 | ▶ | my $class = ref($s) ? ref($s) : $s; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 604 | 10269 | my @vals; | |||||||||||||||||||||||||||||||||||||||||
| 605 | 10269 | ▶ | while( my $rec = $sth->fetchrow_hashref ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 606 | { | ||||||||||||||||||||||||||||||||||||||||||
| 607 | 11337 | push @vals, $rec; | |||||||||||||||||||||||||||||||||||||||||
| 608 | }# end while() | ||||||||||||||||||||||||||||||||||||||||||
| 609 | 10269 | $sth->finish(); | |||||||||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||||||||||
| 611 | 10269 | return $s->_prepare_result( @ vals ); | |||||||||||||||||||||||||||||||||||||||||
| 612 | }# end sth_to_objects() | ||||||||||||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||||||||||
| 615 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 616 | sub search | ||||||||||||||||||||||||||||||||||||||||||
| 617 | 100021 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
search: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 618 | 100021 | my ($s, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||||||||||||||||
| 620 | 100021 | my @cached = grep { $_ } $s->_call_triggers( before_search => \%args ); | |||||||||||||||||||||||||||||||||||||||||
| 621 | 100021 | ▶ | if( @cached ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 622 | { | ||||||||||||||||||||||||||||||||||||||||||
| 623 | 99980 | return $s->_prepare_result( @cached ); | |||||||||||||||||||||||||||||||||||||||||
| 624 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||||||||||||||||
| 626 | 41 | my $sql = ""; | |||||||||||||||||||||||||||||||||||||||||
| 627 | 41 | my @sql_parts = map { "$_ = ?" } sort keys(%args); | |||||||||||||||||||||||||||||||||||||||||
| 628 | 41 | my @sql_vals = map { $args{$_} } sort keys(%args); | |||||||||||||||||||||||||||||||||||||||||
| 629 | 41 | $sql .= join ' AND ', @sql_parts; | |||||||||||||||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||||||||||||||||
| 631 | 41 | my @vals = $s->retrieve_from_sql( $sql, @sql_vals ); | |||||||||||||||||||||||||||||||||||||||||
| 632 | 41 | $s->_call_triggers( after_search => ( \%args, \@vals ) ); | |||||||||||||||||||||||||||||||||||||||||
| 633 | 41 | return $s->_prepare_result( @vals ); | |||||||||||||||||||||||||||||||||||||||||
| 634 | }# end search() | ||||||||||||||||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||||||
| 637 | sub _prepare_result | ||||||||||||||||||||||||||||||||||||||||||
| 638 | 110290 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_prepare_result: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 639 | 110290 | my ($class, @vals) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 640 | 110290 | ▶ | if( wantarray ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 641 | { | ||||||||||||||||||||||||||||||||||||||||||
| 642 | 110214 | my @out = map { $class->construct( $_ ) } @vals; | |||||||||||||||||||||||||||||||||||||||||
| 643 | 110214 | return @out; | |||||||||||||||||||||||||||||||||||||||||
| 644 | } | ||||||||||||||||||||||||||||||||||||||||||
| 645 | else | ||||||||||||||||||||||||||||||||||||||||||
| 646 | { | ||||||||||||||||||||||||||||||||||||||||||
| 647 | my $iter = Class::DBI::Lite::Iterator->new( | ||||||||||||||||||||||||||||||||||||||||||
| 648 | [ | ||||||||||||||||||||||||||||||||||||||||||
| 649 | 76 | map { $class->construct( $_ ) } @vals | |||||||||||||||||||||||||||||||||||||||||
| 650 | ] | ||||||||||||||||||||||||||||||||||||||||||
| 651 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 652 | 76 | return $iter; | |||||||||||||||||||||||||||||||||||||||||
| 653 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 654 | }# end _prepare_result() | ||||||||||||||||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||||||||||||||||
| 657 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 658 | sub count_search | ||||||||||||||||||||||||||||||||||||||||||
| 659 | 2 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
count_search: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 660 | 2 | my ($s, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||||||||||||||||
| 662 | 2 | my $sql = "SELECT COUNT(*) FROM @{[ $s->table ]} WHERE "; | |||||||||||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||||||||||||
| 664 | 2 | my @sql_parts = map { "$_ = ?" } sort keys(%args); | |||||||||||||||||||||||||||||||||||||||||
| 665 | 2 | my @sql_vals = map { $args{$_} } sort keys(%args); | |||||||||||||||||||||||||||||||||||||||||
| 666 | 2 | $sql .= join ' AND ', @sql_parts; | |||||||||||||||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||||||||||||||||
| 668 | 2 | ▶ | if( $s->_meta->trace ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 669 | { | ||||||||||||||||||||||||||||||||||||||||||
| 670 | 0 | ▶ | my $class = ref($s) || $s; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $s || $s
MC/DC0%0/2
ref $s || $s
ref $s
$s
Truth table0%0/3
A || B
A= ref $sB= $s
| |||||||||||||||||||||||||||||||||||||||||||
| 671 | 0 | cluck "$class: count_search($sql, values[" . join( ",", map {qq('$_')} @sql_vals) . "])"; | |||||||||||||||||||||||||||||||||||||||||
| 672 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 673 | SCOPE: { | ||||||||||||||||||||||||||||||||||||||||||
| 674 | 2 | my $sth = $s->db_RO->prepare_cached( $sql ); | |||||||||||||||||||||||||||||||||||||||||
| 675 | 2 | $sth->execute( @sql_vals ); | |||||||||||||||||||||||||||||||||||||||||
| 676 | 2 | my ($count) = $sth->fetchrow; | |||||||||||||||||||||||||||||||||||||||||
| 677 | 2 | $sth->finish(); | |||||||||||||||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||||||||||||||||
| 679 | 2 | return $count; | |||||||||||||||||||||||||||||||||||||||||
| 680 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 681 | }# end count_search() | ||||||||||||||||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||||||||||||||||
| 684 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 685 | sub search_like | ||||||||||||||||||||||||||||||||||||||||||
| 686 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
search_like: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 687 | 1 | my ($s, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||||||||||||||||
| 689 | 1 | my $sql = ""; | |||||||||||||||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||||||||||||||||
| 691 | 1 | my @sql_parts = map { "$_ LIKE ?" } sort keys(%args); | |||||||||||||||||||||||||||||||||||||||||
| 692 | 1 | my @sql_vals = map { $args{$_} } sort keys(%args); | |||||||||||||||||||||||||||||||||||||||||
| 693 | 1 | $sql .= join ' AND ', @sql_parts; | |||||||||||||||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||||||||||||||||
| 695 | 1 | return $s->retrieve_from_sql( $sql, @sql_vals ); | |||||||||||||||||||||||||||||||||||||||||
| 696 | }# end search_like() | ||||||||||||||||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||||||||||||||||
| 699 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 700 | sub count_search_like | ||||||||||||||||||||||||||||||||||||||||||
| 701 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
count_search_like: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 702 | 1 | my ($s, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||||||||||||||||
| 704 | 1 | my $sql = "SELECT COUNT(*) FROM @{[ $s->table ]} WHERE "; | |||||||||||||||||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||||||||||||||||
| 706 | 1 | my @sql_parts = map { "$_ LIKE ?" } sort keys(%args); | |||||||||||||||||||||||||||||||||||||||||
| 707 | 1 | my @sql_vals = map { $args{$_} } sort keys(%args); | |||||||||||||||||||||||||||||||||||||||||
| 708 | 1 | $sql .= join ' AND ', @sql_parts; | |||||||||||||||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||||||||||||||||
| 710 | 1 | ▶ | if( $s->_meta->trace ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 711 | { | ||||||||||||||||||||||||||||||||||||||||||
| 712 | 0 | ▶ | my $class = ref($s) || $s; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $s || $s
MC/DC0%0/2
ref $s || $s
ref $s
$s
Truth table0%0/3
A || B
A= ref $sB= $s
| |||||||||||||||||||||||||||||||||||||||||||
| 713 | 0 | cluck "$class: count_search_like($sql, values[" . join( ",", map {qq('$_')} @sql_vals) . "])"; | |||||||||||||||||||||||||||||||||||||||||
| 714 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 715 | SCOPE: { | ||||||||||||||||||||||||||||||||||||||||||
| 716 | 1 | my $sth = $s->db_RO->prepare_cached( $sql ); | |||||||||||||||||||||||||||||||||||||||||
| 717 | 1 | $sth->execute( @sql_vals ); | |||||||||||||||||||||||||||||||||||||||||
| 718 | 1 | my ($count) = $sth->fetchrow; | |||||||||||||||||||||||||||||||||||||||||
| 719 | 1 | $sth->finish(); | |||||||||||||||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||||||||||||||||
| 721 | 1 | return $count; | |||||||||||||||||||||||||||||||||||||||||
| 722 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 723 | }# end count_search_like() | ||||||||||||||||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||||||||||||||||
| 726 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 727 | sub search_where | ||||||||||||||||||||||||||||||||||||||||||
| 728 | 53 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
search_where: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 729 | 53 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 730 | |||||||||||||||||||||||||||||||||||||||||||
| 731 | 53 | ▶ | my $where = (ref $_[0]) ? $_[0] : { @_ }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 732 | 53 | ▶ | my $attr = (ref $_[0]) ? $_[1] : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 733 | 53 | ▶ | my $order = ($attr) ? delete($attr->{order_by}) : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 734 | 53 | ▶ | my $limit = ($attr) ? delete($attr->{limit}) : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 735 | 53 | ▶ | my $offset = ($attr) ? delete($attr->{offset}) : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||||||||||||||||
| 737 | 53 | my $sql = SQL::Abstract::Limit->new(%$attr, limit_dialect => $s->db_Main ); | |||||||||||||||||||||||||||||||||||||||||
| 738 | 53 | my($phrase, @bind) = $sql->where($where, $order, $limit, $offset); | |||||||||||||||||||||||||||||||||||||||||
| 739 | 53 | $phrase =~ s/^\s*WHERE\s*//i; | |||||||||||||||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||||||||||||||||
| 741 | 53 | return $s->retrieve_from_sql($phrase, @bind); | |||||||||||||||||||||||||||||||||||||||||
| 742 | }# end search_where() | ||||||||||||||||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||||||||||||||||
| 745 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 746 | sub pager | ||||||||||||||||||||||||||||||||||||||||||
| 747 | 6 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pager: called CC=9 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 748 | 6 | my ($s, $where, $attr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 749 | |||||||||||||||||||||||||||||||||||||||||||
| 750 | 6 | ▶ | unless( $where && keys %$where ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$where and keys %$where
MC/DC0%0/2
$where and keys %$where
$where
keys %$where
Truth table33%1/3
A and B
A= $whereB= keys %$where
| |||||||||||||||||||||||||||||||||||||||||||
| 751 | { | ||||||||||||||||||||||||||||||||||||||||||
| 752 | 6 | $where = { 1 => 1 }; | |||||||||||||||||||||||||||||||||||||||||
| 753 | }# end unless() | ||||||||||||||||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||||||||||||||||
| 755 | 6 | foreach(qw( page_size page_number )) | |||||||||||||||||||||||||||||||||||||||||
| 756 | { | ||||||||||||||||||||||||||||||||||||||||||
| 757 | confess "Required attribute '$_' was not provided" | ||||||||||||||||||||||||||||||||||||||||||
| 758 | 12 | ▶ | unless $attr->{$_}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 759 | }# end foreach() | ||||||||||||||||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||||||||||||||||
| 761 | # Limits: | ||||||||||||||||||||||||||||||||||||||||||
| 762 | 6 | my $page_size = $attr->{page_size}; | |||||||||||||||||||||||||||||||||||||||||
| 763 | 6 | my $page_number = $attr->{page_number}; | |||||||||||||||||||||||||||||||||||||||||
| 764 | 6 | ▶ | my $offset = $page_number == 1 ? 0 : ($page_number - 1) * $page_size; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||||||||||||||||
| 766 | 6 | ▶ | my $order = $attr ? $attr->{order_by} : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 767 | 6 | my $sql = SQL::Abstract::Limit->new(%$attr, limit_dialect => $s->db_Main ); | |||||||||||||||||||||||||||||||||||||||||
| 768 | 6 | my($phrase, @bind) = $sql->where($where, $order); | |||||||||||||||||||||||||||||||||||||||||
| 769 | 6 | $phrase =~ s/^\s*WHERE\s*//i; | |||||||||||||||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||||||||||||||||
| 771 | 6 | my $total = $s->count_search_where( $where ); | |||||||||||||||||||||||||||||||||||||||||
| 772 | 6 | ▶ | my $total_pages = $total < $page_size ? 1 : POSIX::ceil($total / $page_size); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||||||||||||||||
| 774 | 6 | ▶ | return Class::DBI::Lite::Pager->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 775 | where => $where, | ||||||||||||||||||||||||||||||||||||||||||
| 776 | order_by => $order, | ||||||||||||||||||||||||||||||||||||||||||
| 777 | class => ref($s) ? ref($s) : $s, | ||||||||||||||||||||||||||||||||||||||||||
| 778 | page_number => $page_number, | ||||||||||||||||||||||||||||||||||||||||||
| 779 | page_size => $page_size, | ||||||||||||||||||||||||||||||||||||||||||
| 780 | total_pages => $total_pages, | ||||||||||||||||||||||||||||||||||||||||||
| 781 | total_items => $total, | ||||||||||||||||||||||||||||||||||||||||||
| 782 | start_item => $offset + 1, | ||||||||||||||||||||||||||||||||||||||||||
| 783 | stop_item => $offset + $page_size, | ||||||||||||||||||||||||||||||||||||||||||
| 784 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 785 | }# end pager() | ||||||||||||||||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||||||||||||||||
| 787 | |||||||||||||||||||||||||||||||||||||||||||
| 788 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 789 | sub sql_pager | ||||||||||||||||||||||||||||||||||||||||||
| 790 | 3 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql_pager: called CC=10 SCAR=23.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 791 | 3 | my ($s, $args, $attr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 792 | |||||||||||||||||||||||||||||||||||||||||||
| 793 | 3 | ▶ | confess "\$args is required" unless $args; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 794 | 3 | foreach( qw( data_sql count_sql ) ) | |||||||||||||||||||||||||||||||||||||||||
| 795 | { | ||||||||||||||||||||||||||||||||||||||||||
| 796 | 6 | ▶ | confess "\$args->{$_} is required" unless $args->{$_}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 797 | }# end foreach() | ||||||||||||||||||||||||||||||||||||||||||
| 798 | 3 | ▶ | $args->{sql_args} ||= [ ]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"sql_args"} ||= []
MC/DC100%1/1
$args->{"sql_args"} ||= []
$args->{"sql_args"}
Truth table100%2/2
A
A= $args->{"sql_args"}
| |||||||||||||||||||||||||||||||||||||||||||
| 799 | |||||||||||||||||||||||||||||||||||||||||||
| 800 | 3 | foreach(qw( page_size page_number )) | |||||||||||||||||||||||||||||||||||||||||
| 801 | { | ||||||||||||||||||||||||||||||||||||||||||
| 802 | confess "Required attribute '$_' was not provided" | ||||||||||||||||||||||||||||||||||||||||||
| 803 | 6 | ▶ | unless $attr->{$_}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 804 | }# end foreach() | ||||||||||||||||||||||||||||||||||||||||||
| 805 | |||||||||||||||||||||||||||||||||||||||||||
| 806 | # Limits: | ||||||||||||||||||||||||||||||||||||||||||
| 807 | 3 | my $page_size = $attr->{page_size}; | |||||||||||||||||||||||||||||||||||||||||
| 808 | 3 | my $page_number = $attr->{page_number}; | |||||||||||||||||||||||||||||||||||||||||
| 809 | 3 | ▶ | my $offset = $page_number == 1 ? 0 : ($page_number - 1) * $page_size; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||||||||||||||||
| 811 | # Get the total items: | ||||||||||||||||||||||||||||||||||||||||||
| 812 | 3 | my $sth = $s->db_RO->prepare( $args->{count_sql} ); | |||||||||||||||||||||||||||||||||||||||||
| 813 | 3 | $sth->execute( @{ $args->{sql_args} } ); | |||||||||||||||||||||||||||||||||||||||||
| 814 | 3 | my ($total) = $sth->fetchrow; | |||||||||||||||||||||||||||||||||||||||||
| 815 | 3 | $sth->finish; | |||||||||||||||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||||||||||||||||
| 817 | 3 | ▶ | my $total_pages = $total < $page_size ? 1 : POSIX::ceil($total / $page_size); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 818 | |||||||||||||||||||||||||||||||||||||||||||
| 819 | return Class::DBI::Lite::Pager->new( | ||||||||||||||||||||||||||||||||||||||||||
| 820 | data_sql => $args->{data_sql}, | ||||||||||||||||||||||||||||||||||||||||||
| 821 | count_sql => $args->{count_sql}, | ||||||||||||||||||||||||||||||||||||||||||
| 822 | sql_args => $args->{sql_args}, | ||||||||||||||||||||||||||||||||||||||||||
| 823 | 3 | ▶ | class => ref($s) ? ref($s) : $s, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 824 | page_number => $page_number, | ||||||||||||||||||||||||||||||||||||||||||
| 825 | page_size => $page_size, | ||||||||||||||||||||||||||||||||||||||||||
| 826 | total_pages => $total_pages, | ||||||||||||||||||||||||||||||||||||||||||
| 827 | total_items => $total, | ||||||||||||||||||||||||||||||||||||||||||
| 828 | start_item => $offset + 1, | ||||||||||||||||||||||||||||||||||||||||||
| 829 | stop_item => $offset + $page_size, | ||||||||||||||||||||||||||||||||||||||||||
| 830 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 831 | }# end sql_pager() | ||||||||||||||||||||||||||||||||||||||||||
| 832 | |||||||||||||||||||||||||||||||||||||||||||
| 833 | |||||||||||||||||||||||||||||||||||||||||||
| 834 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 835 | sub dataset | ||||||||||||||||||||||||||||||||||||||||||
| 836 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dataset: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 837 | 1 | my ($s) = shift; | |||||||||||||||||||||||||||||||||||||||||
| 838 | 1 | require Class::DBI::Lite::Dataset; | |||||||||||||||||||||||||||||||||||||||||
| 839 | 1 | Class::DBI::Lite::Dataset->new( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 840 | }# end dataset() | ||||||||||||||||||||||||||||||||||||||||||
| 841 | |||||||||||||||||||||||||||||||||||||||||||
| 842 | |||||||||||||||||||||||||||||||||||||||||||
| 843 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 844 | sub count_search_where | ||||||||||||||||||||||||||||||||||||||||||
| 845 | 17 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
count_search_where: called CC=7 SCAR=20.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 846 | 17 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 847 | |||||||||||||||||||||||||||||||||||||||||||
| 848 | 17 | ▶ | my $where = (ref $_[0]) ? $_[0] : { @_ }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 849 | 17 | my $phrase = ""; | |||||||||||||||||||||||||||||||||||||||||
| 850 | 17 | my @bind; | |||||||||||||||||||||||||||||||||||||||||
| 851 | 17 | ▶ | if( keys( %$where ) == 1 && (keys %$where)[0] eq '1' && (values %$where)[0] eq '1' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
keys %$where == 1 and (keys %$where)[0] eq 1 and (values %$where)[0] eq 1
keys %$where == 1 and (keys %$where)[0] eq 1
MC/DC33%1/3
keys %$where == 1 and (keys %$where)[0] eq 1 and (values %$where)[0] eq 1
keys %$where == 1
(keys %$where)[0] eq 1
(values %$where)[0] eq 1
Truth table50%2/4
A and B and C
A= keys %$where == 1B= (keys %$where)[0] eq 1C= (values %$where)[0] eq 1
| |||||||||||||||||||||||||||||||||||||||||||
| 852 | { | ||||||||||||||||||||||||||||||||||||||||||
| 853 | # No phrase: | ||||||||||||||||||||||||||||||||||||||||||
| 854 | } | ||||||||||||||||||||||||||||||||||||||||||
| 855 | else | ||||||||||||||||||||||||||||||||||||||||||
| 856 | { | ||||||||||||||||||||||||||||||||||||||||||
| 857 | 11 | my $abstract = SQL::Abstract::Limit->new(); | |||||||||||||||||||||||||||||||||||||||||
| 858 | 11 | ( $phrase, @bind ) = $abstract->where($where); | |||||||||||||||||||||||||||||||||||||||||
| 859 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 860 | |||||||||||||||||||||||||||||||||||||||||||
| 861 | 17 | my $sql = "SELECT COUNT(*) FROM @{[ $s->table ]} $phrase"; | |||||||||||||||||||||||||||||||||||||||||
| 862 | 17 | ▶ | if( $s->_meta->trace ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 863 | { | ||||||||||||||||||||||||||||||||||||||||||
| 864 | 0 | ▶ | my $class = ref($s) || $s; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $s || $s
MC/DC0%0/2
ref $s || $s
ref $s
$s
Truth table0%0/3
A || B
A= ref $sB= $s
| |||||||||||||||||||||||||||||||||||||||||||
| 865 | 0 | cluck "$class: count_search_where($sql, values[" . join( ",", map {qq('$_')} @bind) . "])"; | |||||||||||||||||||||||||||||||||||||||||
| 866 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 867 | SCOPE: { | ||||||||||||||||||||||||||||||||||||||||||
| 868 | 17 | my $sth = $s->db_RO->prepare_cached($sql); | |||||||||||||||||||||||||||||||||||||||||
| 869 | 17 | $sth->execute( @bind ); | |||||||||||||||||||||||||||||||||||||||||
| 870 | 17 | my ($count) = $sth->fetchrow; | |||||||||||||||||||||||||||||||||||||||||
| 871 | 17 | $sth->finish; | |||||||||||||||||||||||||||||||||||||||||
| 872 | |||||||||||||||||||||||||||||||||||||||||||
| 873 | 17 | return $count; | |||||||||||||||||||||||||||||||||||||||||
| 874 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 875 | }# end count_search_where() | ||||||||||||||||||||||||||||||||||||||||||
| 876 | |||||||||||||||||||||||||||||||||||||||||||
| 877 | |||||||||||||||||||||||||||||||||||||||||||
| 878 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 879 | sub find_or_create | ||||||||||||||||||||||||||||||||||||||||||
| 880 | 14 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_or_create: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 881 | 14 | my ($s, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 882 | |||||||||||||||||||||||||||||||||||||||||||
| 883 | 14 | my $result = eval { | |||||||||||||||||||||||||||||||||||||||||
| 884 | 14 | ▶ | $s->do_transaction(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 885 | |||||||||||||||||||||||||||||||||||||||||||
| 886 | 14 | ▶ | if( my ($obj) = $s->search( %args ) ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 887 | { | ||||||||||||||||||||||||||||||||||||||||||
| 888 | 2 | return $obj; | |||||||||||||||||||||||||||||||||||||||||
| 889 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 890 | |||||||||||||||||||||||||||||||||||||||||||
| 891 | 12 | my $obj = $s->create( %args ); | |||||||||||||||||||||||||||||||||||||||||
| 892 | 12 | return $obj; | |||||||||||||||||||||||||||||||||||||||||
| 893 | 14 | }); | |||||||||||||||||||||||||||||||||||||||||
| 894 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 895 | 14 | ▶ | if( $@ ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 896 | { | ||||||||||||||||||||||||||||||||||||||||||
| 897 | 0 | die $@; | |||||||||||||||||||||||||||||||||||||||||
| 898 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 899 | |||||||||||||||||||||||||||||||||||||||||||
| 900 | 14 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 901 | }# end find_or_create() | ||||||||||||||||||||||||||||||||||||||||||
| 902 | |||||||||||||||||||||||||||||||||||||||||||
| 903 | |||||||||||||||||||||||||||||||||||||||||||
| 904 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 905 | sub belongs_to | ||||||||||||||||||||||||||||||||||||||||||
| 906 | 31 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
belongs_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 907 | 31 | my ($class, $method, $otherClass, $fk) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 908 | |||||||||||||||||||||||||||||||||||||||||||
| 909 | 31 | $class->_load_class( $otherClass ); | |||||||||||||||||||||||||||||||||||||||||
| 910 | |||||||||||||||||||||||||||||||||||||||||||
| 911 | 31 | $class->_meta->{belongs_to_rels}->{$method} = { | |||||||||||||||||||||||||||||||||||||||||
| 912 | class => $otherClass, | ||||||||||||||||||||||||||||||||||||||||||
| 913 | fk => $fk | ||||||||||||||||||||||||||||||||||||||||||
| 914 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 915 | |||||||||||||||||||||||||||||||||||||||||||
| 916 | 17 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 917 | 31 | ▶ | *{"$class\::$method"} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 918 | 117 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 919 | |||||||||||||||||||||||||||||||||||||||||||
| 920 | 117 | $otherClass->retrieve( $s->$fk ); | |||||||||||||||||||||||||||||||||||||||||
| 921 | 31 | }; | |||||||||||||||||||||||||||||||||||||||||
| 922 | }# end belongs_to() | ||||||||||||||||||||||||||||||||||||||||||
| 923 | 17 | *has_a = \&belongs_to; | |||||||||||||||||||||||||||||||||||||||||
| 924 | |||||||||||||||||||||||||||||||||||||||||||
| 925 | |||||||||||||||||||||||||||||||||||||||||||
| 926 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 927 | sub has_many | ||||||||||||||||||||||||||||||||||||||||||
| 928 | 16 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_many: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 929 | 16 | my ($class, $method, $otherClass, $fk) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 930 | |||||||||||||||||||||||||||||||||||||||||||
| 931 | 16 | $class->_load_class( $otherClass ); | |||||||||||||||||||||||||||||||||||||||||
| 932 | 16 | $class->_meta->{has_many_rels}->{$method} = { | |||||||||||||||||||||||||||||||||||||||||
| 933 | class => $otherClass, | ||||||||||||||||||||||||||||||||||||||||||
| 934 | fk => $fk, | ||||||||||||||||||||||||||||||||||||||||||
| 935 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 936 | |||||||||||||||||||||||||||||||||||||||||||
| 937 | 17 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 938 | 16 | ▶ | *{"$class\::$method"} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 939 | 22 | my ($s, $args, $attrs) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 940 | 22 | ▶ | $args = { } unless $args; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 941 | 22 | $args->{ $fk } = $s->id; | |||||||||||||||||||||||||||||||||||||||||
| 942 | 22 | ▶ | $attrs ||= { }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$attrs ||= {}
MC/DC100%1/1
$attrs ||= {}
$attrs
Truth table100%2/2
A
A= $attrs
| |||||||||||||||||||||||||||||||||||||||||||
| 943 | 22 | $otherClass->search_where( $args, $attrs ); | |||||||||||||||||||||||||||||||||||||||||
| 944 | 16 | }; | |||||||||||||||||||||||||||||||||||||||||
| 945 | |||||||||||||||||||||||||||||||||||||||||||
| 946 | 16 | ▶ | *{"$class\::add_to_$method"} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 947 | 123 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 948 | 123 | ▶ | my %options = ref($_[0]) ? %{$_[0]} : @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 949 | 123 | $otherClass->create( | |||||||||||||||||||||||||||||||||||||||||
| 950 | %options, | ||||||||||||||||||||||||||||||||||||||||||
| 951 | $fk => $s->id, | ||||||||||||||||||||||||||||||||||||||||||
| 952 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 953 | 16 | }; | |||||||||||||||||||||||||||||||||||||||||
| 954 | }# end has_many() | ||||||||||||||||||||||||||||||||||||||||||
| 955 | |||||||||||||||||||||||||||||||||||||||||||
| 956 | |||||||||||||||||||||||||||||||||||||||||||
| 957 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 958 | sub has_one | ||||||||||||||||||||||||||||||||||||||||||
| 959 | 15 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_one: called CC=1 SCAR=0.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 960 | 15 | my ($class, $method, $otherClass, $fk) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 961 | |||||||||||||||||||||||||||||||||||||||||||
| 962 | 15 | $class->_load_class( $otherClass ); | |||||||||||||||||||||||||||||||||||||||||
| 963 | 15 | $class->_meta->{has_one_rels}->{$method} = { | |||||||||||||||||||||||||||||||||||||||||
| 964 | class => $otherClass, | ||||||||||||||||||||||||||||||||||||||||||
| 965 | fk => $fk, | ||||||||||||||||||||||||||||||||||||||||||
| 966 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 967 | |||||||||||||||||||||||||||||||||||||||||||
| 968 | 17 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 969 | 15 | ▶ | *{"$class\::$method"} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 970 | 2 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 971 | 2 | ▶ | my ($item) = $otherClass->search( $fk => $s->id ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 972 | or return; | ||||||||||||||||||||||||||||||||||||||||||
| 973 | 2 | return $item; | |||||||||||||||||||||||||||||||||||||||||
| 974 | 15 | }; | |||||||||||||||||||||||||||||||||||||||||
| 975 | |||||||||||||||||||||||||||||||||||||||||||
| 976 | 15 | ▶ | *{"$class\::set_$method"} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=15.1
| |||||||||||||||||||||||||||||||||||||||||||
| 977 | 0 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 978 | 0 | ▶ | my %options = ref($_[0]) ? %{$_[0]} : @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 979 | 0 | $otherClass->create( | |||||||||||||||||||||||||||||||||||||||||
| 980 | %options, | ||||||||||||||||||||||||||||||||||||||||||
| 981 | $fk => $s->id, | ||||||||||||||||||||||||||||||||||||||||||
| 982 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 983 | 15 | }; | |||||||||||||||||||||||||||||||||||||||||
| 984 | }# end has_one() | ||||||||||||||||||||||||||||||||||||||||||
| 985 | |||||||||||||||||||||||||||||||||||||||||||
| 986 | |||||||||||||||||||||||||||||||||||||||||||
| 987 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 988 | sub add_trigger | ||||||||||||||||||||||||||||||||||||||||||
| 989 | 65 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_trigger: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 990 | 65 | my ($s, $event, $handler) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 991 | |||||||||||||||||||||||||||||||||||||||||||
| 992 | 65 | ▶ | confess "add_trigger called but the handler is not a subref" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 993 | unless ref($handler) eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||
| 994 | |||||||||||||||||||||||||||||||||||||||||||
| 995 | 65 | ▶ | $s->_meta->{triggers}->{$event} ||= [ ]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$s->_meta->{"triggers"}{$event} ||= []
MC/DC100%1/1
$s->_meta->{"triggers"}{$event} ||= []
$s->_meta->{"triggers"}{$event}
Truth table100%2/2
A
A= $s->_meta->{"triggers"}{$event}
| |||||||||||||||||||||||||||||||||||||||||||
| 996 | 65 | my $handlers = $s->_meta->{triggers}->{$event}; | |||||||||||||||||||||||||||||||||||||||||
| 997 | 65 | ▶ | return if grep { $_ eq $handler } @$handlers; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 998 | |||||||||||||||||||||||||||||||||||||||||||
| 999 | 64 | push @$handlers, $handler; | |||||||||||||||||||||||||||||||||||||||||
| 1000 | }# end add_trigger() | ||||||||||||||||||||||||||||||||||||||||||
| 1001 | |||||||||||||||||||||||||||||||||||||||||||
| 1002 | |||||||||||||||||||||||||||||||||||||||||||
| 1003 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1004 | sub _call_triggers | ||||||||||||||||||||||||||||||||||||||||||
| 1005 | 123272 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_call_triggers: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1006 | 123272 | my ($s, $event) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1007 | |||||||||||||||||||||||||||||||||||||||||||
| 1008 | 123272 | ▶ | $s->_meta->{triggers}->{ $event } ||= [ ]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$s->_meta->{"triggers"}{$event} ||= []
MC/DC100%1/1
$s->_meta->{"triggers"}{$event} ||= []
$s->_meta->{"triggers"}{$event}
Truth table100%2/2
A
A= $s->_meta->{"triggers"}{$event}
| |||||||||||||||||||||||||||||||||||||||||||
| 1009 | 123272 | ▶ | return unless my @handlers = @{ $s->_meta->{triggers}->{ $event } }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1010 | 101216 | shift;shift; | |||||||||||||||||||||||||||||||||||||||||
| 1011 | 101216 | my @return_values; | |||||||||||||||||||||||||||||||||||||||||
| 1012 | my $return_value; | ||||||||||||||||||||||||||||||||||||||||||
| 1013 | 101216 | foreach my $handler ( @handlers ) | |||||||||||||||||||||||||||||||||||||||||
| 1014 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1015 | 101251 | ▶ | if( wantarray ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1016 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1017 | 100000 | ▶ | eval { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1018 | 100000 | my @val = $handler->( $s, @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 1019 | 100000 | ▶ | push @return_values, @val if @val; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1020 | 100000 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 1021 | } or confess $@; | ||||||||||||||||||||||||||||||||||||||||||
| 1022 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1023 | else | ||||||||||||||||||||||||||||||||||||||||||
| 1024 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1025 | 1251 | ▶ | eval { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1026 | 1251 | $return_value = $handler->( $s, @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 1027 | 1248 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 1028 | } or confess $@; | ||||||||||||||||||||||||||||||||||||||||||
| 1029 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 1030 | }# end foreach() | ||||||||||||||||||||||||||||||||||||||||||
| 1031 | |||||||||||||||||||||||||||||||||||||||||||
| 1032 | 101213 | ▶ | return wantarray ? @return_values : $return_value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1033 | }# end _call_triggers() | ||||||||||||||||||||||||||||||||||||||||||
| 1034 | |||||||||||||||||||||||||||||||||||||||||||
| 1035 | |||||||||||||||||||||||||||||||||||||||||||
| 1036 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1037 | sub dbi_commit | ||||||||||||||||||||||||||||||||||||||||||
| 1038 | 20 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dbi_commit: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 1039 | 20 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1040 | 20 | ▶ | return if $s->db_Main->{AutoCommit}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1041 | 19 | $s->db_Main->commit( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 1042 | }# end dbi_commit() | ||||||||||||||||||||||||||||||||||||||||||
| 1043 | |||||||||||||||||||||||||||||||||||||||||||
| 1044 | |||||||||||||||||||||||||||||||||||||||||||
| 1045 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1046 | sub remove_from_object_index | ||||||||||||||||||||||||||||||||||||||||||
| 1047 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove_from_object_index: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 1048 | 1 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1049 | 1 | my $obj = $Live_Objects{ $s->get_cache_key }; | |||||||||||||||||||||||||||||||||||||||||
| 1050 | 1 | delete($Live_Objects{ $s->get_cache_key }); | |||||||||||||||||||||||||||||||||||||||||
| 1051 | 1 | undef(%$obj); | |||||||||||||||||||||||||||||||||||||||||
| 1052 | }# end remove_from_object_index() | ||||||||||||||||||||||||||||||||||||||||||
| 1053 | |||||||||||||||||||||||||||||||||||||||||||
| 1054 | |||||||||||||||||||||||||||||||||||||||||||
| 1055 | sub get_cache_key | ||||||||||||||||||||||||||||||||||||||||||
| 1056 | 100022 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_cache_key: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 1057 | 100022 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1058 | 100022 | ▶ | if( my $id = shift ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1059 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1060 | 100020 | ▶ | my $class = ref($s) ? ref($s) : $s; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1061 | 100020 | return join ':', ( $s->root_meta->{schema}, $class, $id ); | |||||||||||||||||||||||||||||||||||||||||
| 1062 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1063 | else | ||||||||||||||||||||||||||||||||||||||||||
| 1064 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1065 | 2 | return $s->root_meta->{schema} . ':' . ref($s) . ':' . $s->id | |||||||||||||||||||||||||||||||||||||||||
| 1066 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 1067 | }# end get_cache_key() | ||||||||||||||||||||||||||||||||||||||||||
| 1068 | |||||||||||||||||||||||||||||||||||||||||||
| 1069 | |||||||||||||||||||||||||||||||||||||||||||
| 1070 | sub as_hashref | ||||||||||||||||||||||||||||||||||||||||||
| 1071 | 20 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_hashref: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 1072 | 20 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1073 | 20 | my %data = %$s; | |||||||||||||||||||||||||||||||||||||||||
| 1074 | 20 | delete( $data{__Changed} ); | |||||||||||||||||||||||||||||||||||||||||
| 1075 | 20 | delete( $data{__id} ); | |||||||||||||||||||||||||||||||||||||||||
| 1076 | 20 | \%data; | |||||||||||||||||||||||||||||||||||||||||
| 1077 | }# end as_hashref() | ||||||||||||||||||||||||||||||||||||||||||
| 1078 | |||||||||||||||||||||||||||||||||||||||||||
| 1079 | |||||||||||||||||||||||||||||||||||||||||||
| 1080 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1081 | sub dbi_rollback | ||||||||||||||||||||||||||||||||||||||||||
| 1082 | 2 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dbi_rollback: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 1083 | 2 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1084 | 2 | $s->db_Main->rollback( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 1085 | }# end dbi_rollback() | ||||||||||||||||||||||||||||||||||||||||||
| 1086 | |||||||||||||||||||||||||||||||||||||||||||
| 1087 | |||||||||||||||||||||||||||||||||||||||||||
| 1088 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1089 | sub discard_changes | ||||||||||||||||||||||||||||||||||||||||||
| 1090 | 1258 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
discard_changes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1091 | 1258 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1092 | |||||||||||||||||||||||||||||||||||||||||||
| 1093 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 1094 | $s->{$_} = $s->{__Changed}->{$_}->{oldval} | ||||||||||||||||||||||||||||||||||||||||||
| 1095 | 1258 | } keys(%{$s->{__Changed}}); | |||||||||||||||||||||||||||||||||||||||||
| 1096 | |||||||||||||||||||||||||||||||||||||||||||
| 1097 | 1258 | $s->{__Changed} = { }; | |||||||||||||||||||||||||||||||||||||||||
| 1098 | |||||||||||||||||||||||||||||||||||||||||||
| 1099 | 1258 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 1100 | }# end discard_changes() | ||||||||||||||||||||||||||||||||||||||||||
| 1101 | |||||||||||||||||||||||||||||||||||||||||||
| 1102 | |||||||||||||||||||||||||||||||||||||||||||
| 1103 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1104 | 17 | *_load_class = \&load_class; | |||||||||||||||||||||||||||||||||||||||||
| 1105 | sub load_class | ||||||||||||||||||||||||||||||||||||||||||
| 1106 | 124 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load_class: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 1107 | 124 | my (undef, $class) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1108 | |||||||||||||||||||||||||||||||||||||||||||
| 1109 | 124 | (my $file = "$class.pm") =~ s/::/\//g; | |||||||||||||||||||||||||||||||||||||||||
| 1110 | 124 | ▶ | unless( $INC{$file} ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1111 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1112 | 30 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 1113 | 30 | require $file; | |||||||||||||||||||||||||||||||||||||||||
| 1114 | 30 | $class->import; | |||||||||||||||||||||||||||||||||||||||||
| 1115 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 1116 | }# end unless(); | ||||||||||||||||||||||||||||||||||||||||||
| 1117 | }# end load_class() | ||||||||||||||||||||||||||||||||||||||||||
| 1118 | |||||||||||||||||||||||||||||||||||||||||||
| 1119 | |||||||||||||||||||||||||||||||||||||||||||
| 1120 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1121 | sub trace | ||||||||||||||||||||||||||||||||||||||||||
| 1122 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
trace: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1123 | 0 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1124 | 0 | $s->_meta->trace( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 1125 | }# end trace() | ||||||||||||||||||||||||||||||||||||||||||
| 1126 | |||||||||||||||||||||||||||||||||||||||||||
| 1127 | |||||||||||||||||||||||||||||||||||||||||||
| 1128 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1129 | sub _flesh_out | ||||||||||||||||||||||||||||||||||||||||||
| 1130 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_flesh_out: called CC=4 SCAR=14.8
| |||||||||||||||||||||||||||||||||||||||||||
| 1131 | 1 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1132 | |||||||||||||||||||||||||||||||||||||||||||
| 1133 | 1 | my @missing_fields = grep { ! exists($s->{$_}) } $s->columns('All'); | |||||||||||||||||||||||||||||||||||||||||
| 1134 | 1 | my $sql = <<""; | |||||||||||||||||||||||||||||||||||||||||
| 1135 | 1 | SELECT @{[ join ', ', @missing_fields ]} | |||||||||||||||||||||||||||||||||||||||||
| 1136 | 1 | FROM @{[ $s->table ]} | |||||||||||||||||||||||||||||||||||||||||
| 1137 | 1 | WHERE @{[ $s->primary_column ]} = ? | |||||||||||||||||||||||||||||||||||||||||
| 1138 | |||||||||||||||||||||||||||||||||||||||||||
| 1139 | 1 | my $sth = $s->db_RO->prepare($sql); | |||||||||||||||||||||||||||||||||||||||||
| 1140 | |||||||||||||||||||||||||||||||||||||||||||
| 1141 | 1 | ▶ | if( $s->_meta->trace ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1142 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1143 | 0 | ▶ | my $class = ref($s) || $s; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $s || $s
MC/DC0%0/2
ref $s || $s
ref $s
$s
Truth table0%0/3
A || B
A= ref $sB= $s
| |||||||||||||||||||||||||||||||||||||||||||
| 1144 | 0 | cluck "$class: flesh_out($sql, values[" . $s->id . "])"; | |||||||||||||||||||||||||||||||||||||||||
| 1145 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 1146 | 1 | $sth->execute( $s->id ); | |||||||||||||||||||||||||||||||||||||||||
| 1147 | 1 | my $rec = $sth->fetchrow_hashref; | |||||||||||||||||||||||||||||||||||||||||
| 1148 | 1 | $sth->finish(); | |||||||||||||||||||||||||||||||||||||||||
| 1149 | |||||||||||||||||||||||||||||||||||||||||||
| 1150 | 1 | $s->{$_} = $rec->{$_} foreach @missing_fields; | |||||||||||||||||||||||||||||||||||||||||
| 1151 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 1152 | }# end _flesh_out() | ||||||||||||||||||||||||||||||||||||||||||
| 1153 | |||||||||||||||||||||||||||||||||||||||||||
| 1154 | |||||||||||||||||||||||||||||||||||||||||||
| 1155 | #============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 1156 | sub DESTROY | ||||||||||||||||||||||||||||||||||||||||||
| 1157 | 3842 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 1158 | 3842 | my $s = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1159 | |||||||||||||||||||||||||||||||||||||||||||
| 1160 | 3842 | ▶ | if( $s->{__Changed} && keys(%{ $s->{__Changed} }) ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$s->{"__Changed"} and keys %{$$s{"__Changed"};}
MC/DC100%2/2
$s->{"__Changed"} and keys %{$$s{"__Changed"};}
$s->{"__Changed"}
keys %{$$s{"__Changed"};}
Truth table100%3/3
A and B
A= $s->{"__Changed"}B= keys %{$$s{"__Changed"};}
| |||||||||||||||||||||||||||||||||||||||||||
| 1161 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1162 | 1 | my $changed = join ', ', sort keys(%{ $s->{__Changed} }); | |||||||||||||||||||||||||||||||||||||||||
| 1163 | 1 | cluck ref($s) . " #$s->{__id} DESTROY'd without saving changes to $changed"; | |||||||||||||||||||||||||||||||||||||||||
| 1164 | }# end if() | ||||||||||||||||||||||||||||||||||||||||||
| 1165 | |||||||||||||||||||||||||||||||||||||||||||
| 1166 | 3842 | delete($s->{$_}) foreach keys(%$s); | |||||||||||||||||||||||||||||||||||||||||
| 1167 | }# end DESTROY() | ||||||||||||||||||||||||||||||||||||||||||
| 1168 | |||||||||||||||||||||||||||||||||||||||||||
| 1169 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1170 | # This is deleted-object-heaven: | ||||||||||||||||||||||||||||||||||||||||||
| 1171 | package | ||||||||||||||||||||||||||||||||||||||||||
| 1172 | Class::DBI::Lite::Object::Has::Been::Deleted; | ||||||||||||||||||||||||||||||||||||||||||
| 1173 | |||||||||||||||||||||||||||||||||||||||||||
| 1174 | use overload | ||||||||||||||||||||||||||||||||||||||||||
| 1175 | 0 | ▶ | '""' => sub { '' }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 1176 | 1 | ▶ | bool => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1177 | 17 | fallback => 1; | |||||||||||||||||||||||||||||||||||||||||
| 1178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1179 | |||||||||||||||||||||||||||||||||||||||||||
| 1180 | |||||||||||||||||||||||||||||||||||||||||||
| 1181 | 17 | sub lock_table; | |||||||||||||||||||||||||||||||||||||||||
| 1182 | sub unlock_table; | ||||||||||||||||||||||||||||||||||||||||||
| 1183 | |||||||||||||||||||||||||||||||||||||||||||
| 1184 | 17 | 1;# return true: | |||||||||||||||||||||||||||||||||||||||||
| 1185 | |||||||||||||||||||||||||||||||||||||||||||
| 1186 | |||||||||||||||||||||||||||||||||||||||||||
| 1187 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 1188 | |||||||||||||||||||||||||||||||||||||||||||
| 1189 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 1190 | |||||||||||||||||||||||||||||||||||||||||||
| 1191 | Class::DBI::Lite - Lightweight ORM for Perl | ||||||||||||||||||||||||||||||||||||||||||
| 1192 | |||||||||||||||||||||||||||||||||||||||||||
| 1193 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 1194 | |||||||||||||||||||||||||||||||||||||||||||
| 1195 | Please take a look at L<Class::DBI::Lite::Tutorial> for an introduction to using this module. | ||||||||||||||||||||||||||||||||||||||||||
| 1196 | |||||||||||||||||||||||||||||||||||||||||||
| 1197 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 1198 | |||||||||||||||||||||||||||||||||||||||||||
| 1199 | C<Class::DBI::Lite> offers a simple way to deal with databases in an object-oriented way. | ||||||||||||||||||||||||||||||||||||||||||
| 1200 | |||||||||||||||||||||||||||||||||||||||||||
| 1201 | One class (the B<Model> class) defines your connection to the database (eg: connectionstring, username and password) | ||||||||||||||||||||||||||||||||||||||||||
| 1202 | and your other classes define interaction with one table each (your B<entity> classes). | ||||||||||||||||||||||||||||||||||||||||||
| 1203 | |||||||||||||||||||||||||||||||||||||||||||
| 1204 | The Entity classes subclass the Model class and automatically inherit its connection. | ||||||||||||||||||||||||||||||||||||||||||
| 1205 | |||||||||||||||||||||||||||||||||||||||||||
| 1206 | C<Class::DBI::Lite> relies heavily on L<Ima::DBI::Contextual>, L<SQL::Abstract> and L<Scalar::Util>. | ||||||||||||||||||||||||||||||||||||||||||
| 1207 | |||||||||||||||||||||||||||||||||||||||||||
| 1208 | C<Class::DBI::Lite> does not leak memory and is well-suited for use within mod_perl, Fast CGI, CGI | ||||||||||||||||||||||||||||||||||||||||||
| 1209 | and anywhere else you might need it. | ||||||||||||||||||||||||||||||||||||||||||
| 1210 | |||||||||||||||||||||||||||||||||||||||||||
| 1211 | =head1 BACKGROUND | ||||||||||||||||||||||||||||||||||||||||||
| 1212 | |||||||||||||||||||||||||||||||||||||||||||
| 1213 | I used L<Class::DBI> for a few years, a few years ago, on a very large project, under mod_perl. | ||||||||||||||||||||||||||||||||||||||||||
| 1214 | This was back in 2002-2003 when the ORM (Object-Relational Mapper) scene was still fairly new. | ||||||||||||||||||||||||||||||||||||||||||
| 1215 | |||||||||||||||||||||||||||||||||||||||||||
| 1216 | While it saved me a great deal of typing, I was amazed at the complexity of C<Class::DBI>'s internal code. | ||||||||||||||||||||||||||||||||||||||||||
| 1217 | After some time I found myself spending more effort working around problems caused by C<Class::DBI> | ||||||||||||||||||||||||||||||||||||||||||
| 1218 | than I could stand. | ||||||||||||||||||||||||||||||||||||||||||
| 1219 | |||||||||||||||||||||||||||||||||||||||||||
| 1220 | Many people encountered the same problems I encountered (transactions, database connection sharing issues, performance, etc) | ||||||||||||||||||||||||||||||||||||||||||
| 1221 | and they all went and began writing L<DBIx::Class>. | ||||||||||||||||||||||||||||||||||||||||||
| 1222 | |||||||||||||||||||||||||||||||||||||||||||
| 1223 | L<DBIx::Class> went in a direction away from the database while I wanted to get closer to | ||||||||||||||||||||||||||||||||||||||||||
| 1224 | the database. As close as I could possibly get without wasting time. I also wanted | ||||||||||||||||||||||||||||||||||||||||||
| 1225 | to keep some simple logic in my Entity classes (those classes that represent individual tables). | ||||||||||||||||||||||||||||||||||||||||||
| 1226 | I didn't want my ORM to do too much magic, think too much or do anything not immediately apparent. | ||||||||||||||||||||||||||||||||||||||||||
| 1227 | I didn't care about many-to-many relationships or automatic SQL join clauses. Vendor-specific | ||||||||||||||||||||||||||||||||||||||||||
| 1228 | LIMIT expressions simply were not a concern of mine. | ||||||||||||||||||||||||||||||||||||||||||
| 1229 | |||||||||||||||||||||||||||||||||||||||||||
| 1230 | So...I reimplemented (most) of the C<Class::DBI> interface in a way that I preferred. I left out some | ||||||||||||||||||||||||||||||||||||||||||
| 1231 | things that didn't matter to me (eg: many-to-many relationships, column groups) and added some things | ||||||||||||||||||||||||||||||||||||||||||
| 1232 | I needed frequently (eg: transactions, single-field triggers, mod_perl compatibility). | ||||||||||||||||||||||||||||||||||||||||||
| 1233 | |||||||||||||||||||||||||||||||||||||||||||
| 1234 | =head1 PHILOSOPHY | ||||||||||||||||||||||||||||||||||||||||||
| 1235 | |||||||||||||||||||||||||||||||||||||||||||
| 1236 | C<Class::DBI::Lite> is intended to minimize the boiler-plate code typically written | ||||||||||||||||||||||||||||||||||||||||||
| 1237 | in most applications. It is not intended to completely insulate developers from | ||||||||||||||||||||||||||||||||||||||||||
| 1238 | interacting with the database directly. | ||||||||||||||||||||||||||||||||||||||||||
| 1239 | |||||||||||||||||||||||||||||||||||||||||||
| 1240 | C<Class::DBI::Lite> is not a way to avoid I<learning> SQL - it is a way to avoid I<writing> | ||||||||||||||||||||||||||||||||||||||||||
| 1241 | boring, repetitive, "boiler-plate" SQL. | ||||||||||||||||||||||||||||||||||||||||||
| 1242 | |||||||||||||||||||||||||||||||||||||||||||
| 1243 | =head1 PUBLIC PROPERTIES | ||||||||||||||||||||||||||||||||||||||||||
| 1244 | |||||||||||||||||||||||||||||||||||||||||||
| 1245 | =head2 connection( $dsn, $username, $password ) | ||||||||||||||||||||||||||||||||||||||||||
| 1246 | |||||||||||||||||||||||||||||||||||||||||||
| 1247 | Sets the DSN for your classes. | ||||||||||||||||||||||||||||||||||||||||||
| 1248 | |||||||||||||||||||||||||||||||||||||||||||
| 1249 | package App::db::model; | ||||||||||||||||||||||||||||||||||||||||||
| 1250 | |||||||||||||||||||||||||||||||||||||||||||
| 1251 | use base 'Class::DBI::Lite::mysql'; | ||||||||||||||||||||||||||||||||||||||||||
| 1252 | |||||||||||||||||||||||||||||||||||||||||||
| 1253 | __PACKAGE__->connection('DBI:mysql:dbname:localhost', 'username', 'password' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1254 | |||||||||||||||||||||||||||||||||||||||||||
| 1255 | =head2 db_Main | ||||||||||||||||||||||||||||||||||||||||||
| 1256 | |||||||||||||||||||||||||||||||||||||||||||
| 1257 | Returns the active database handle in use by the class. | ||||||||||||||||||||||||||||||||||||||||||
| 1258 | |||||||||||||||||||||||||||||||||||||||||||
| 1259 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1260 | |||||||||||||||||||||||||||||||||||||||||||
| 1261 | my $dbh = App::db::artist->db_Main; | ||||||||||||||||||||||||||||||||||||||||||
| 1262 | my $sth = $dbh->prepare("select * from artists"); | ||||||||||||||||||||||||||||||||||||||||||
| 1263 | $sth->execute(); | ||||||||||||||||||||||||||||||||||||||||||
| 1264 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 1265 | |||||||||||||||||||||||||||||||||||||||||||
| 1266 | =head2 table | ||||||||||||||||||||||||||||||||||||||||||
| 1267 | |||||||||||||||||||||||||||||||||||||||||||
| 1268 | Returns the name of the table that the class is assigned to. | ||||||||||||||||||||||||||||||||||||||||||
| 1269 | |||||||||||||||||||||||||||||||||||||||||||
| 1270 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1271 | |||||||||||||||||||||||||||||||||||||||||||
| 1272 | print App::db::artist->table; # 'artists' | ||||||||||||||||||||||||||||||||||||||||||
| 1273 | |||||||||||||||||||||||||||||||||||||||||||
| 1274 | =head2 columns | ||||||||||||||||||||||||||||||||||||||||||
| 1275 | |||||||||||||||||||||||||||||||||||||||||||
| 1276 | Returns a list of field names in the table that the class represents. | ||||||||||||||||||||||||||||||||||||||||||
| 1277 | |||||||||||||||||||||||||||||||||||||||||||
| 1278 | Given the following table: | ||||||||||||||||||||||||||||||||||||||||||
| 1279 | |||||||||||||||||||||||||||||||||||||||||||
| 1280 | create table artists ( | ||||||||||||||||||||||||||||||||||||||||||
| 1281 | artist_id integer unsigned not null primary key auto_increment, | ||||||||||||||||||||||||||||||||||||||||||
| 1282 | name varchar(100) not null, | ||||||||||||||||||||||||||||||||||||||||||
| 1283 | ) engine=innodb charset=utf8; | ||||||||||||||||||||||||||||||||||||||||||
| 1284 | |||||||||||||||||||||||||||||||||||||||||||
| 1285 | We get this: | ||||||||||||||||||||||||||||||||||||||||||
| 1286 | |||||||||||||||||||||||||||||||||||||||||||
| 1287 | print join ", ", App::db::artist->columns; | ||||||||||||||||||||||||||||||||||||||||||
| 1288 | # artist_id, name | ||||||||||||||||||||||||||||||||||||||||||
| 1289 | |||||||||||||||||||||||||||||||||||||||||||
| 1290 | =head2 trace( 1:0 ) | ||||||||||||||||||||||||||||||||||||||||||
| 1291 | |||||||||||||||||||||||||||||||||||||||||||
| 1292 | (New in version 1.018) | ||||||||||||||||||||||||||||||||||||||||||
| 1293 | |||||||||||||||||||||||||||||||||||||||||||
| 1294 | Setting C<trace> to 1 or 0 will turn on or off SQL logging to STDERR. | ||||||||||||||||||||||||||||||||||||||||||
| 1295 | |||||||||||||||||||||||||||||||||||||||||||
| 1296 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1297 | |||||||||||||||||||||||||||||||||||||||||||
| 1298 | # Start seeing all the SQL: | ||||||||||||||||||||||||||||||||||||||||||
| 1299 | App::db::artist->trace( 1 ); | ||||||||||||||||||||||||||||||||||||||||||
| 1300 | |||||||||||||||||||||||||||||||||||||||||||
| 1301 | # We will see some SQL when the next line is executed: | ||||||||||||||||||||||||||||||||||||||||||
| 1302 | my @users = App::db::artist->search_like( name => 'Rob%' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1303 | |||||||||||||||||||||||||||||||||||||||||||
| 1304 | # Turn it off again: | ||||||||||||||||||||||||||||||||||||||||||
| 1305 | App::db::artist->trace( 0 ); | ||||||||||||||||||||||||||||||||||||||||||
| 1306 | |||||||||||||||||||||||||||||||||||||||||||
| 1307 | By default, C<trace> is turned off. | ||||||||||||||||||||||||||||||||||||||||||
| 1308 | |||||||||||||||||||||||||||||||||||||||||||
| 1309 | =head1 STATIC METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 1310 | |||||||||||||||||||||||||||||||||||||||||||
| 1311 | =head2 create( %info ) | ||||||||||||||||||||||||||||||||||||||||||
| 1312 | |||||||||||||||||||||||||||||||||||||||||||
| 1313 | Creates a new object and returns it. | ||||||||||||||||||||||||||||||||||||||||||
| 1314 | |||||||||||||||||||||||||||||||||||||||||||
| 1315 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1316 | |||||||||||||||||||||||||||||||||||||||||||
| 1317 | my $artist = App::db::artist->create( name => 'Bob Marley' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1318 | |||||||||||||||||||||||||||||||||||||||||||
| 1319 | =head2 find_or_create( %info ) | ||||||||||||||||||||||||||||||||||||||||||
| 1320 | |||||||||||||||||||||||||||||||||||||||||||
| 1321 | Using C<%info> a search will be performed. If a matching result is found it is returned. Otherwise | ||||||||||||||||||||||||||||||||||||||||||
| 1322 | a new record will be created using C<%info> as arguments. | ||||||||||||||||||||||||||||||||||||||||||
| 1323 | |||||||||||||||||||||||||||||||||||||||||||
| 1324 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1325 | |||||||||||||||||||||||||||||||||||||||||||
| 1326 | my $artist = App::db::artist->find_or_create( name => 'Bob Marley' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1327 | |||||||||||||||||||||||||||||||||||||||||||
| 1328 | =head2 retrieve( $id ) | ||||||||||||||||||||||||||||||||||||||||||
| 1329 | |||||||||||||||||||||||||||||||||||||||||||
| 1330 | Given the id of a record in the database, returns that object. | ||||||||||||||||||||||||||||||||||||||||||
| 1331 | |||||||||||||||||||||||||||||||||||||||||||
| 1332 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1333 | |||||||||||||||||||||||||||||||||||||||||||
| 1334 | my $artist = App::db::artist->retrieve( 1 ); | ||||||||||||||||||||||||||||||||||||||||||
| 1335 | |||||||||||||||||||||||||||||||||||||||||||
| 1336 | Same as the following SQL: | ||||||||||||||||||||||||||||||||||||||||||
| 1337 | |||||||||||||||||||||||||||||||||||||||||||
| 1338 | SELECT * | ||||||||||||||||||||||||||||||||||||||||||
| 1339 | FROM artists | ||||||||||||||||||||||||||||||||||||||||||
| 1340 | WHERE artist_id = 1 | ||||||||||||||||||||||||||||||||||||||||||
| 1341 | |||||||||||||||||||||||||||||||||||||||||||
| 1342 | =head2 retrieve_all( ) | ||||||||||||||||||||||||||||||||||||||||||
| 1343 | |||||||||||||||||||||||||||||||||||||||||||
| 1344 | Returns all objects in the database table. | ||||||||||||||||||||||||||||||||||||||||||
| 1345 | |||||||||||||||||||||||||||||||||||||||||||
| 1346 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1347 | |||||||||||||||||||||||||||||||||||||||||||
| 1348 | my @artists = App::db::artist->retrieve_all; | ||||||||||||||||||||||||||||||||||||||||||
| 1349 | |||||||||||||||||||||||||||||||||||||||||||
| 1350 | Same as the following SQL: | ||||||||||||||||||||||||||||||||||||||||||
| 1351 | |||||||||||||||||||||||||||||||||||||||||||
| 1352 | SELECT * FROM artists | ||||||||||||||||||||||||||||||||||||||||||
| 1353 | |||||||||||||||||||||||||||||||||||||||||||
| 1354 | B<NOTE:> If you want to sort all of the records or do paging, use C<search_where> | ||||||||||||||||||||||||||||||||||||||||||
| 1355 | like this: | ||||||||||||||||||||||||||||||||||||||||||
| 1356 | |||||||||||||||||||||||||||||||||||||||||||
| 1357 | my @artists = App::db::artist->search_where({ 1 => 1}, {order_by => 'name DESC'}); | ||||||||||||||||||||||||||||||||||||||||||
| 1358 | |||||||||||||||||||||||||||||||||||||||||||
| 1359 | Same as the following SQL: | ||||||||||||||||||||||||||||||||||||||||||
| 1360 | |||||||||||||||||||||||||||||||||||||||||||
| 1361 | SELECT * | ||||||||||||||||||||||||||||||||||||||||||
| 1362 | FROM artists | ||||||||||||||||||||||||||||||||||||||||||
| 1363 | WHERE 1 = 1 | ||||||||||||||||||||||||||||||||||||||||||
| 1364 | ORDER BY name DESC | ||||||||||||||||||||||||||||||||||||||||||
| 1365 | |||||||||||||||||||||||||||||||||||||||||||
| 1366 | That "C<WHERE 1 = 1>" is a funny way of telling the database "give them all to me". | ||||||||||||||||||||||||||||||||||||||||||
| 1367 | |||||||||||||||||||||||||||||||||||||||||||
| 1368 | =head2 has_many( ... ) | ||||||||||||||||||||||||||||||||||||||||||
| 1369 | |||||||||||||||||||||||||||||||||||||||||||
| 1370 | Declares a "one-to-many" relationship between this two classes. | ||||||||||||||||||||||||||||||||||||||||||
| 1371 | |||||||||||||||||||||||||||||||||||||||||||
| 1372 | package App::db::artist; | ||||||||||||||||||||||||||||||||||||||||||
| 1373 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 1374 | __PACKAGE__->has_many( | ||||||||||||||||||||||||||||||||||||||||||
| 1375 | albums => | ||||||||||||||||||||||||||||||||||||||||||
| 1376 | 'App::db::album' => | ||||||||||||||||||||||||||||||||||||||||||
| 1377 | 'album_id' | ||||||||||||||||||||||||||||||||||||||||||
| 1378 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1379 | |||||||||||||||||||||||||||||||||||||||||||
| 1380 | The syntax is: | ||||||||||||||||||||||||||||||||||||||||||
| 1381 | |||||||||||||||||||||||||||||||||||||||||||
| 1382 | __PACKAGE__->has_many( | ||||||||||||||||||||||||||||||||||||||||||
| 1383 | $what_they_are_called => | ||||||||||||||||||||||||||||||||||||||||||
| 1384 | $the_class_name => | ||||||||||||||||||||||||||||||||||||||||||
| 1385 | $the_foreign_key_field_from_the_other_class | ||||||||||||||||||||||||||||||||||||||||||
| 1386 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1387 | |||||||||||||||||||||||||||||||||||||||||||
| 1388 | The result is this: | ||||||||||||||||||||||||||||||||||||||||||
| 1389 | |||||||||||||||||||||||||||||||||||||||||||
| 1390 | my @albums = $artist->albums; | ||||||||||||||||||||||||||||||||||||||||||
| 1391 | $artist->add_to_albums( name => 'Legend' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1392 | |||||||||||||||||||||||||||||||||||||||||||
| 1393 | That's the same as: | ||||||||||||||||||||||||||||||||||||||||||
| 1394 | |||||||||||||||||||||||||||||||||||||||||||
| 1395 | my @albums = App::db::artist->search( | ||||||||||||||||||||||||||||||||||||||||||
| 1396 | artist_id => $artist->id | ||||||||||||||||||||||||||||||||||||||||||
| 1397 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1398 | |||||||||||||||||||||||||||||||||||||||||||
| 1399 | =head2 belongs_to( ... ) | ||||||||||||||||||||||||||||||||||||||||||
| 1400 | |||||||||||||||||||||||||||||||||||||||||||
| 1401 | Declares that instances "this" class exists only as a feature of instances of another class. | ||||||||||||||||||||||||||||||||||||||||||
| 1402 | |||||||||||||||||||||||||||||||||||||||||||
| 1403 | For example, "songs" exist as features of "albums" - not the other way around. | ||||||||||||||||||||||||||||||||||||||||||
| 1404 | |||||||||||||||||||||||||||||||||||||||||||
| 1405 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1406 | |||||||||||||||||||||||||||||||||||||||||||
| 1407 | package App::db::album; | ||||||||||||||||||||||||||||||||||||||||||
| 1408 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 1409 | __PACKAGE__->belongs_to( | ||||||||||||||||||||||||||||||||||||||||||
| 1410 | artist => | ||||||||||||||||||||||||||||||||||||||||||
| 1411 | 'App::db::artist' => | ||||||||||||||||||||||||||||||||||||||||||
| 1412 | 'artist_id' | ||||||||||||||||||||||||||||||||||||||||||
| 1413 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1414 | |||||||||||||||||||||||||||||||||||||||||||
| 1415 | So that's: | ||||||||||||||||||||||||||||||||||||||||||
| 1416 | |||||||||||||||||||||||||||||||||||||||||||
| 1417 | __PACKAGE__->belongs_to( | ||||||||||||||||||||||||||||||||||||||||||
| 1418 | $the_method_name => | ||||||||||||||||||||||||||||||||||||||||||
| 1419 | $the_class_name => | ||||||||||||||||||||||||||||||||||||||||||
| 1420 | $my_foreign_key_field | ||||||||||||||||||||||||||||||||||||||||||
| 1421 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1422 | |||||||||||||||||||||||||||||||||||||||||||
| 1423 | =head2 construct( $hashref ) | ||||||||||||||||||||||||||||||||||||||||||
| 1424 | |||||||||||||||||||||||||||||||||||||||||||
| 1425 | Blesses the object into the given class, even if we don't have all the information | ||||||||||||||||||||||||||||||||||||||||||
| 1426 | about the object (as long as we get its primary field value). | ||||||||||||||||||||||||||||||||||||||||||
| 1427 | |||||||||||||||||||||||||||||||||||||||||||
| 1428 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1429 | |||||||||||||||||||||||||||||||||||||||||||
| 1430 | for( 1..5 ) { | ||||||||||||||||||||||||||||||||||||||||||
| 1431 | my $artist = App::db::artist->construct({ artist_id => $_ }); | ||||||||||||||||||||||||||||||||||||||||||
| 1432 | |||||||||||||||||||||||||||||||||||||||||||
| 1433 | # name is automatically "fleshed out": | ||||||||||||||||||||||||||||||||||||||||||
| 1434 | print $artist->name; | ||||||||||||||||||||||||||||||||||||||||||
| 1435 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1436 | |||||||||||||||||||||||||||||||||||||||||||
| 1437 | =head2 eval { do_transaction( sub { ... } ) } | ||||||||||||||||||||||||||||||||||||||||||
| 1438 | |||||||||||||||||||||||||||||||||||||||||||
| 1439 | Executes a block of code within the context of a transaction. | ||||||||||||||||||||||||||||||||||||||||||
| 1440 | |||||||||||||||||||||||||||||||||||||||||||
| 1441 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1442 | |||||||||||||||||||||||||||||||||||||||||||
| 1443 | # Safely update the name of every album: | ||||||||||||||||||||||||||||||||||||||||||
| 1444 | eval { | ||||||||||||||||||||||||||||||||||||||||||
| 1445 | App::db::artist->do_transaction( sub { | ||||||||||||||||||||||||||||||||||||||||||
| 1446 | |||||||||||||||||||||||||||||||||||||||||||
| 1447 | # Your transaction code goes here: | ||||||||||||||||||||||||||||||||||||||||||
| 1448 | my $artist = App::db::artist->retrieve( 1 ); | ||||||||||||||||||||||||||||||||||||||||||
| 1449 | foreach my $album ( $artist->albums ) { | ||||||||||||||||||||||||||||||||||||||||||
| 1450 | $album->name( $artist->name . ': ' . $album->name ); | ||||||||||||||||||||||||||||||||||||||||||
| 1451 | $album->update; | ||||||||||||||||||||||||||||||||||||||||||
| 1452 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1453 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1454 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 1455 | |||||||||||||||||||||||||||||||||||||||||||
| 1456 | if( $@ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 1457 | # There was an error: | ||||||||||||||||||||||||||||||||||||||||||
| 1458 | die $@; | ||||||||||||||||||||||||||||||||||||||||||
| 1459 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1460 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1461 | # Everything was OK: | ||||||||||||||||||||||||||||||||||||||||||
| 1462 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1463 | |||||||||||||||||||||||||||||||||||||||||||
| 1464 | =head2 search( %args ) | ||||||||||||||||||||||||||||||||||||||||||
| 1465 | |||||||||||||||||||||||||||||||||||||||||||
| 1466 | Returns any objects that match all elements in C<%args>. | ||||||||||||||||||||||||||||||||||||||||||
| 1467 | |||||||||||||||||||||||||||||||||||||||||||
| 1468 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1469 | |||||||||||||||||||||||||||||||||||||||||||
| 1470 | my @artists = App::db::artist->search( name => 'Bob Marley' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1471 | |||||||||||||||||||||||||||||||||||||||||||
| 1472 | my $artist_iterator = App::db::artist->search( name => 'Bob Marley' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1473 | |||||||||||||||||||||||||||||||||||||||||||
| 1474 | Returns an array in list context or a L<Class::DBI::Lite::Iterator> in scalar context. | ||||||||||||||||||||||||||||||||||||||||||
| 1475 | |||||||||||||||||||||||||||||||||||||||||||
| 1476 | =head2 search_like( %args ) | ||||||||||||||||||||||||||||||||||||||||||
| 1477 | |||||||||||||||||||||||||||||||||||||||||||
| 1478 | Returns any objects that match all elements in C<%args> using the C<LIKE> operator. | ||||||||||||||||||||||||||||||||||||||||||
| 1479 | |||||||||||||||||||||||||||||||||||||||||||
| 1480 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1481 | |||||||||||||||||||||||||||||||||||||||||||
| 1482 | my @artists = App::db::artist->search_like( name => 'Bob%' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1483 | |||||||||||||||||||||||||||||||||||||||||||
| 1484 | my $artist_iterator = App::db::artist->search_like( name => 'Bob%' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1485 | |||||||||||||||||||||||||||||||||||||||||||
| 1486 | Returns an array in list context or a L<Class::DBI::Lite::Iterator> in scalar context. | ||||||||||||||||||||||||||||||||||||||||||
| 1487 | |||||||||||||||||||||||||||||||||||||||||||
| 1488 | Both examples would execute the following SQL: | ||||||||||||||||||||||||||||||||||||||||||
| 1489 | |||||||||||||||||||||||||||||||||||||||||||
| 1490 | SELECT * FROM artists WHERE name LIKE 'Bob%' | ||||||||||||||||||||||||||||||||||||||||||
| 1491 | |||||||||||||||||||||||||||||||||||||||||||
| 1492 | =head2 search_where( \%args, [\%sort_and_limit] ) | ||||||||||||||||||||||||||||||||||||||||||
| 1493 | |||||||||||||||||||||||||||||||||||||||||||
| 1494 | Returns any objects that match all elements in C<%args> as specified by C<%sort_and_limit>. | ||||||||||||||||||||||||||||||||||||||||||
| 1495 | |||||||||||||||||||||||||||||||||||||||||||
| 1496 | Returns an array in list context or a L<Class::DBI::Lite::Iterator> in scalar context. | ||||||||||||||||||||||||||||||||||||||||||
| 1497 | |||||||||||||||||||||||||||||||||||||||||||
| 1498 | Example 1: | ||||||||||||||||||||||||||||||||||||||||||
| 1499 | |||||||||||||||||||||||||||||||||||||||||||
| 1500 | my @artists = App::db::artist->search_where({ | ||||||||||||||||||||||||||||||||||||||||||
| 1501 | name => 'Bob Marley' | ||||||||||||||||||||||||||||||||||||||||||
| 1502 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1503 | |||||||||||||||||||||||||||||||||||||||||||
| 1504 | Same as this SQL: | ||||||||||||||||||||||||||||||||||||||||||
| 1505 | |||||||||||||||||||||||||||||||||||||||||||
| 1506 | SELECT * | ||||||||||||||||||||||||||||||||||||||||||
| 1507 | FROM artists | ||||||||||||||||||||||||||||||||||||||||||
| 1508 | WHERE name = 'Bob Marley' | ||||||||||||||||||||||||||||||||||||||||||
| 1509 | |||||||||||||||||||||||||||||||||||||||||||
| 1510 | Example 2: | ||||||||||||||||||||||||||||||||||||||||||
| 1511 | |||||||||||||||||||||||||||||||||||||||||||
| 1512 | my @artists = App::db::artist->search_where({ | ||||||||||||||||||||||||||||||||||||||||||
| 1513 | name => 'Bob Marley' | ||||||||||||||||||||||||||||||||||||||||||
| 1514 | }, { | ||||||||||||||||||||||||||||||||||||||||||
| 1515 | order_by => 'name ASC LIMIT 0, 10' | ||||||||||||||||||||||||||||||||||||||||||
| 1516 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1517 | |||||||||||||||||||||||||||||||||||||||||||
| 1518 | Same as this SQL: | ||||||||||||||||||||||||||||||||||||||||||
| 1519 | |||||||||||||||||||||||||||||||||||||||||||
| 1520 | SELECT * | ||||||||||||||||||||||||||||||||||||||||||
| 1521 | FROM artists | ||||||||||||||||||||||||||||||||||||||||||
| 1522 | WHERE name = 'Bob Marley' | ||||||||||||||||||||||||||||||||||||||||||
| 1523 | ORDER BY name | ||||||||||||||||||||||||||||||||||||||||||
| 1524 | LIMIT 0, 10 | ||||||||||||||||||||||||||||||||||||||||||
| 1525 | |||||||||||||||||||||||||||||||||||||||||||
| 1526 | Example 3: | ||||||||||||||||||||||||||||||||||||||||||
| 1527 | |||||||||||||||||||||||||||||||||||||||||||
| 1528 | my @artists = App::db::artist->search_where([ | ||||||||||||||||||||||||||||||||||||||||||
| 1529 | name => { '!=' => 'Bob Marley' }, | ||||||||||||||||||||||||||||||||||||||||||
| 1530 | genre => 'Rock', | ||||||||||||||||||||||||||||||||||||||||||
| 1531 | ]); | ||||||||||||||||||||||||||||||||||||||||||
| 1532 | |||||||||||||||||||||||||||||||||||||||||||
| 1533 | Same as this SQL: | ||||||||||||||||||||||||||||||||||||||||||
| 1534 | |||||||||||||||||||||||||||||||||||||||||||
| 1535 | SELECT * | ||||||||||||||||||||||||||||||||||||||||||
| 1536 | FROM artists | ||||||||||||||||||||||||||||||||||||||||||
| 1537 | WHERE name != 'Bob Marley' | ||||||||||||||||||||||||||||||||||||||||||
| 1538 | OR genre = 'Rock' | ||||||||||||||||||||||||||||||||||||||||||
| 1539 | |||||||||||||||||||||||||||||||||||||||||||
| 1540 | Because C<search_where> uses L<SQL::Abstract> to generate the SQL for the database, | ||||||||||||||||||||||||||||||||||||||||||
| 1541 | you can look there for more detailed examples. | ||||||||||||||||||||||||||||||||||||||||||
| 1542 | |||||||||||||||||||||||||||||||||||||||||||
| 1543 | Specifying OrderBy, Limit and Offset separately: | ||||||||||||||||||||||||||||||||||||||||||
| 1544 | |||||||||||||||||||||||||||||||||||||||||||
| 1545 | my @artists = App::db::artist->search_where({ | ||||||||||||||||||||||||||||||||||||||||||
| 1546 | name => 'Bob Marley' | ||||||||||||||||||||||||||||||||||||||||||
| 1547 | }, { | ||||||||||||||||||||||||||||||||||||||||||
| 1548 | order_by => 'name ASC', | ||||||||||||||||||||||||||||||||||||||||||
| 1549 | limit => $how_many, | ||||||||||||||||||||||||||||||||||||||||||
| 1550 | offset => $start_where, | ||||||||||||||||||||||||||||||||||||||||||
| 1551 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1552 | |||||||||||||||||||||||||||||||||||||||||||
| 1553 | So if your C<$how_many> were 10, and your C<$start_where> were zero (C<0>) then that would be the same as: | ||||||||||||||||||||||||||||||||||||||||||
| 1554 | |||||||||||||||||||||||||||||||||||||||||||
| 1555 | SELECT * | ||||||||||||||||||||||||||||||||||||||||||
| 1556 | FROM artists | ||||||||||||||||||||||||||||||||||||||||||
| 1557 | WHERE name = 'Bob Marley' | ||||||||||||||||||||||||||||||||||||||||||
| 1558 | ORDER BY name ASC | ||||||||||||||||||||||||||||||||||||||||||
| 1559 | LIMIT 0, 10 | ||||||||||||||||||||||||||||||||||||||||||
| 1560 | |||||||||||||||||||||||||||||||||||||||||||
| 1561 | =head2 count_search( %args ) | ||||||||||||||||||||||||||||||||||||||||||
| 1562 | |||||||||||||||||||||||||||||||||||||||||||
| 1563 | Returns the number of records that match C<%args>. | ||||||||||||||||||||||||||||||||||||||||||
| 1564 | |||||||||||||||||||||||||||||||||||||||||||
| 1565 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1566 | |||||||||||||||||||||||||||||||||||||||||||
| 1567 | my $count = App::db::album->count_search( name => 'Greatest Hits' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1568 | |||||||||||||||||||||||||||||||||||||||||||
| 1569 | =head2 count_search_like( %args ) | ||||||||||||||||||||||||||||||||||||||||||
| 1570 | |||||||||||||||||||||||||||||||||||||||||||
| 1571 | Returns the number of records that match C<%args> using the C<LIKE> operator. | ||||||||||||||||||||||||||||||||||||||||||
| 1572 | |||||||||||||||||||||||||||||||||||||||||||
| 1573 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1574 | |||||||||||||||||||||||||||||||||||||||||||
| 1575 | my $count = App::db::artist->count_search_like( | ||||||||||||||||||||||||||||||||||||||||||
| 1576 | name => 'Bob%' | ||||||||||||||||||||||||||||||||||||||||||
| 1577 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1578 | |||||||||||||||||||||||||||||||||||||||||||
| 1579 | =head2 count_search_where( \%args ) | ||||||||||||||||||||||||||||||||||||||||||
| 1580 | |||||||||||||||||||||||||||||||||||||||||||
| 1581 | Returns the number of records that match C<\%args>. | ||||||||||||||||||||||||||||||||||||||||||
| 1582 | |||||||||||||||||||||||||||||||||||||||||||
| 1583 | Examples: | ||||||||||||||||||||||||||||||||||||||||||
| 1584 | |||||||||||||||||||||||||||||||||||||||||||
| 1585 | my $count = App::db::album->count_search_where({ | ||||||||||||||||||||||||||||||||||||||||||
| 1586 | name => { LIKE => 'Best Of%' } | ||||||||||||||||||||||||||||||||||||||||||
| 1587 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1588 | |||||||||||||||||||||||||||||||||||||||||||
| 1589 | my $count = App::db::album->count_search_where({ | ||||||||||||||||||||||||||||||||||||||||||
| 1590 | genre => { '!=' => 'Country/Western' } | ||||||||||||||||||||||||||||||||||||||||||
| 1591 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1592 | |||||||||||||||||||||||||||||||||||||||||||
| 1593 | As with C<search_where()>, the C<count_search_where()> class method uses L<SQL::Abstract> | ||||||||||||||||||||||||||||||||||||||||||
| 1594 | to generate the SQL for the database. | ||||||||||||||||||||||||||||||||||||||||||
| 1595 | |||||||||||||||||||||||||||||||||||||||||||
| 1596 | =head2 sth_to_objects( $sth ) | ||||||||||||||||||||||||||||||||||||||||||
| 1597 | |||||||||||||||||||||||||||||||||||||||||||
| 1598 | Takes a statement handle that is ready to fetch records from. Returns the results | ||||||||||||||||||||||||||||||||||||||||||
| 1599 | as objects. | ||||||||||||||||||||||||||||||||||||||||||
| 1600 | |||||||||||||||||||||||||||||||||||||||||||
| 1601 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1602 | |||||||||||||||||||||||||||||||||||||||||||
| 1603 | my $sth = App::db::artist->db_Main->prepare("SELECT * FROM artists"); | ||||||||||||||||||||||||||||||||||||||||||
| 1604 | $sth->execute(); | ||||||||||||||||||||||||||||||||||||||||||
| 1605 | my @artists = App::db::artist->sth_to_objects( $sth ); | ||||||||||||||||||||||||||||||||||||||||||
| 1606 | |||||||||||||||||||||||||||||||||||||||||||
| 1607 | This method is very useful for when your SQL query is too complicated for C<search_where()>. | ||||||||||||||||||||||||||||||||||||||||||
| 1608 | |||||||||||||||||||||||||||||||||||||||||||
| 1609 | =head2 add_trigger( $event => \&sub ) | ||||||||||||||||||||||||||||||||||||||||||
| 1610 | |||||||||||||||||||||||||||||||||||||||||||
| 1611 | Specifies a callback to be executed when a specific event happens. | ||||||||||||||||||||||||||||||||||||||||||
| 1612 | |||||||||||||||||||||||||||||||||||||||||||
| 1613 | Examples: | ||||||||||||||||||||||||||||||||||||||||||
| 1614 | |||||||||||||||||||||||||||||||||||||||||||
| 1615 | package App::db::artist; | ||||||||||||||||||||||||||||||||||||||||||
| 1616 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 1617 | __PACKAGE__->add_trigger( after_create => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 1618 | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 1619 | |||||||||||||||||||||||||||||||||||||||||||
| 1620 | warn "You just created a new artist: " . $self->name; | ||||||||||||||||||||||||||||||||||||||||||
| 1621 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1622 | |||||||||||||||||||||||||||||||||||||||||||
| 1623 | There are 6 main trigger points at the class level and 2 trigger points for | ||||||||||||||||||||||||||||||||||||||||||
| 1624 | every field: | ||||||||||||||||||||||||||||||||||||||||||
| 1625 | |||||||||||||||||||||||||||||||||||||||||||
| 1626 | =head3 Class Triggers | ||||||||||||||||||||||||||||||||||||||||||
| 1627 | |||||||||||||||||||||||||||||||||||||||||||
| 1628 | =head4 before_create( $self ) | ||||||||||||||||||||||||||||||||||||||||||
| 1629 | |||||||||||||||||||||||||||||||||||||||||||
| 1630 | Called just before a new record is created. C<$self> is a hashref blessed into | ||||||||||||||||||||||||||||||||||||||||||
| 1631 | the object's class and contains only the values that were provided for its creation. | ||||||||||||||||||||||||||||||||||||||||||
| 1632 | |||||||||||||||||||||||||||||||||||||||||||
| 1633 | So, given this trigger: | ||||||||||||||||||||||||||||||||||||||||||
| 1634 | |||||||||||||||||||||||||||||||||||||||||||
| 1635 | package App::db::album; | ||||||||||||||||||||||||||||||||||||||||||
| 1636 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 1637 | __PACKAGE__->add_trigger( before_create => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 1638 | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 1639 | |||||||||||||||||||||||||||||||||||||||||||
| 1640 | warn "ID = '$self->{album_id}', Name = '$self->{name}"; | ||||||||||||||||||||||||||||||||||||||||||
| 1641 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1642 | |||||||||||||||||||||||||||||||||||||||||||
| 1643 | If we ran this code: | ||||||||||||||||||||||||||||||||||||||||||
| 1644 | |||||||||||||||||||||||||||||||||||||||||||
| 1645 | my $album = App::db::album->create( name => 'Legend' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1646 | |||||||||||||||||||||||||||||||||||||||||||
| 1647 | We would see this output: | ||||||||||||||||||||||||||||||||||||||||||
| 1648 | |||||||||||||||||||||||||||||||||||||||||||
| 1649 | ID = '', Name = 'Legend' | ||||||||||||||||||||||||||||||||||||||||||
| 1650 | |||||||||||||||||||||||||||||||||||||||||||
| 1651 | Because the value for C<album_id> has not been assigned by the database it does | ||||||||||||||||||||||||||||||||||||||||||
| 1652 | not yet have a value. | ||||||||||||||||||||||||||||||||||||||||||
| 1653 | |||||||||||||||||||||||||||||||||||||||||||
| 1654 | =head4 after_create( $self ) | ||||||||||||||||||||||||||||||||||||||||||
| 1655 | |||||||||||||||||||||||||||||||||||||||||||
| 1656 | Called just after a new record is created. C<$self> is the new object itself. | ||||||||||||||||||||||||||||||||||||||||||
| 1657 | |||||||||||||||||||||||||||||||||||||||||||
| 1658 | So given this trigger: | ||||||||||||||||||||||||||||||||||||||||||
| 1659 | |||||||||||||||||||||||||||||||||||||||||||
| 1660 | package App::db::album; | ||||||||||||||||||||||||||||||||||||||||||
| 1661 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 1662 | __PACKAGE__->add_trigger( after_create => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 1663 | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 1664 | |||||||||||||||||||||||||||||||||||||||||||
| 1665 | warn "ID = '$self->{album_id}', Name = '$self->{name}"; | ||||||||||||||||||||||||||||||||||||||||||
| 1666 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1667 | |||||||||||||||||||||||||||||||||||||||||||
| 1668 | If we ran this code: | ||||||||||||||||||||||||||||||||||||||||||
| 1669 | |||||||||||||||||||||||||||||||||||||||||||
| 1670 | my $album = App::db::album->create( name => 'Legend' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1671 | |||||||||||||||||||||||||||||||||||||||||||
| 1672 | We would see this output: | ||||||||||||||||||||||||||||||||||||||||||
| 1673 | |||||||||||||||||||||||||||||||||||||||||||
| 1674 | ID = '1', Name = 'Legend' | ||||||||||||||||||||||||||||||||||||||||||
| 1675 | |||||||||||||||||||||||||||||||||||||||||||
| 1676 | =head4 before_update( $self ) | ||||||||||||||||||||||||||||||||||||||||||
| 1677 | |||||||||||||||||||||||||||||||||||||||||||
| 1678 | Called just before changes are saved to the database. C<$self> is the object | ||||||||||||||||||||||||||||||||||||||||||
| 1679 | to be updated. | ||||||||||||||||||||||||||||||||||||||||||
| 1680 | |||||||||||||||||||||||||||||||||||||||||||
| 1681 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1682 | |||||||||||||||||||||||||||||||||||||||||||
| 1683 | package App::db::album; | ||||||||||||||||||||||||||||||||||||||||||
| 1684 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 1685 | __PACKAGE__->add_trigger( before_update => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 1686 | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 1687 | |||||||||||||||||||||||||||||||||||||||||||
| 1688 | warn "About to update album " . $self->name; | ||||||||||||||||||||||||||||||||||||||||||
| 1689 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1690 | |||||||||||||||||||||||||||||||||||||||||||
| 1691 | =head4 after_update( $self ) | ||||||||||||||||||||||||||||||||||||||||||
| 1692 | |||||||||||||||||||||||||||||||||||||||||||
| 1693 | Called just after changes are saved to the database. C<$self> is the object | ||||||||||||||||||||||||||||||||||||||||||
| 1694 | that was updated. | ||||||||||||||||||||||||||||||||||||||||||
| 1695 | |||||||||||||||||||||||||||||||||||||||||||
| 1696 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1697 | |||||||||||||||||||||||||||||||||||||||||||
| 1698 | package App::db::album; | ||||||||||||||||||||||||||||||||||||||||||
| 1699 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 1700 | __PACKAGE__->add_trigger( after_update => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 1701 | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 1702 | |||||||||||||||||||||||||||||||||||||||||||
| 1703 | warn "Finished updating album " . $self->name; | ||||||||||||||||||||||||||||||||||||||||||
| 1704 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1705 | |||||||||||||||||||||||||||||||||||||||||||
| 1706 | B<NOTE:> If you make changes to C<$self> from within an C<after_update> you could | ||||||||||||||||||||||||||||||||||||||||||
| 1707 | enter into a recursive loop in which an update is made that causes an update to | ||||||||||||||||||||||||||||||||||||||||||
| 1708 | be made which causes an update to be made which causes an update to be made which causes an update to be made which | ||||||||||||||||||||||||||||||||||||||||||
| 1709 | causes an update to be made which causes an update to be made which causes an update to be made which | ||||||||||||||||||||||||||||||||||||||||||
| 1710 | causes an update to be made which causes an update to be made which causes an update to be made which | ||||||||||||||||||||||||||||||||||||||||||
| 1711 | causes an update to be made which causes an update to be made which causes an update to be made which | ||||||||||||||||||||||||||||||||||||||||||
| 1712 | causes an update to be made which causes an update to be made which causes an update to be made which | ||||||||||||||||||||||||||||||||||||||||||
| 1713 | causes an update to be made which causes an update to be made which causes an update to be made which...and so on. | ||||||||||||||||||||||||||||||||||||||||||
| 1714 | |||||||||||||||||||||||||||||||||||||||||||
| 1715 | B<DO NOT DO THIS>: | ||||||||||||||||||||||||||||||||||||||||||
| 1716 | |||||||||||||||||||||||||||||||||||||||||||
| 1717 | package App::db::album; | ||||||||||||||||||||||||||||||||||||||||||
| 1718 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 1719 | __PACKAGE__->add_trigger( after_update => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 1720 | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 1721 | |||||||||||||||||||||||||||||||||||||||||||
| 1722 | # This will cause problems: | ||||||||||||||||||||||||||||||||||||||||||
| 1723 | warn "Making a recursive problem:"; | ||||||||||||||||||||||||||||||||||||||||||
| 1724 | $self->name( 'Hello ' . rand() ); | ||||||||||||||||||||||||||||||||||||||||||
| 1725 | $self->update; | ||||||||||||||||||||||||||||||||||||||||||
| 1726 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1727 | |||||||||||||||||||||||||||||||||||||||||||
| 1728 | =head4 before_delete( $self ) | ||||||||||||||||||||||||||||||||||||||||||
| 1729 | |||||||||||||||||||||||||||||||||||||||||||
| 1730 | Called just before something is deleted. | ||||||||||||||||||||||||||||||||||||||||||
| 1731 | |||||||||||||||||||||||||||||||||||||||||||
| 1732 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1733 | |||||||||||||||||||||||||||||||||||||||||||
| 1734 | package App::db::album; | ||||||||||||||||||||||||||||||||||||||||||
| 1735 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 1736 | __PACKAGE__->add_trigger( before_delete => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 1737 | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 1738 | |||||||||||||||||||||||||||||||||||||||||||
| 1739 | warn "About to delete " . $self->name; | ||||||||||||||||||||||||||||||||||||||||||
| 1740 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1741 | |||||||||||||||||||||||||||||||||||||||||||
| 1742 | =head4 after_delete( {$primary_field => $id} ) | ||||||||||||||||||||||||||||||||||||||||||
| 1743 | |||||||||||||||||||||||||||||||||||||||||||
| 1744 | Called just after something is deleted. | ||||||||||||||||||||||||||||||||||||||||||
| 1745 | |||||||||||||||||||||||||||||||||||||||||||
| 1746 | B<NOTE:> Since the object itself is deleted from the database B<and> memory, all | ||||||||||||||||||||||||||||||||||||||||||
| 1747 | that is left is the id of the original object. | ||||||||||||||||||||||||||||||||||||||||||
| 1748 | |||||||||||||||||||||||||||||||||||||||||||
| 1749 | So, given this trigger... | ||||||||||||||||||||||||||||||||||||||||||
| 1750 | |||||||||||||||||||||||||||||||||||||||||||
| 1751 | package App::db::album; | ||||||||||||||||||||||||||||||||||||||||||
| 1752 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 1753 | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||||
| 1754 | __PACKAGE__->add_trigger( after_delete => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 1755 | my ($obj) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 1756 | |||||||||||||||||||||||||||||||||||||||||||
| 1757 | warn "Deleted an album: " . Dumper($obj); | ||||||||||||||||||||||||||||||||||||||||||
| 1758 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1759 | |||||||||||||||||||||||||||||||||||||||||||
| 1760 | ...we might see the following output: | ||||||||||||||||||||||||||||||||||||||||||
| 1761 | |||||||||||||||||||||||||||||||||||||||||||
| 1762 | Deleted an album: $VAR1 = { | ||||||||||||||||||||||||||||||||||||||||||
| 1763 | album_id => 123 | ||||||||||||||||||||||||||||||||||||||||||
| 1764 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 1765 | |||||||||||||||||||||||||||||||||||||||||||
| 1766 | =head3 Field Triggers | ||||||||||||||||||||||||||||||||||||||||||
| 1767 | |||||||||||||||||||||||||||||||||||||||||||
| 1768 | =head4 before_update_<field>( $self, $old_value, $new_value ) | ||||||||||||||||||||||||||||||||||||||||||
| 1769 | |||||||||||||||||||||||||||||||||||||||||||
| 1770 | Called just B<before> a field's value is updated. | ||||||||||||||||||||||||||||||||||||||||||
| 1771 | |||||||||||||||||||||||||||||||||||||||||||
| 1772 | So, given the following trigger... | ||||||||||||||||||||||||||||||||||||||||||
| 1773 | |||||||||||||||||||||||||||||||||||||||||||
| 1774 | package App::db::album; | ||||||||||||||||||||||||||||||||||||||||||
| 1775 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 1776 | __PACKAGE__->add_trigger( before_update_name => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 1777 | my ($self, $old_value, $new_value) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 1778 | |||||||||||||||||||||||||||||||||||||||||||
| 1779 | warn "About to change name from '$old_value' to '$new_value'"; | ||||||||||||||||||||||||||||||||||||||||||
| 1780 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1781 | |||||||||||||||||||||||||||||||||||||||||||
| 1782 | ...called with the following code... | ||||||||||||||||||||||||||||||||||||||||||
| 1783 | |||||||||||||||||||||||||||||||||||||||||||
| 1784 | my $artist = App::db::artist->create( name => 'Bob Marley' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1785 | my $album = $artist->add_to_albums( name => 'Legend' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1786 | |||||||||||||||||||||||||||||||||||||||||||
| 1787 | # Now change the name: | ||||||||||||||||||||||||||||||||||||||||||
| 1788 | $album->name( 'Greatest Hits' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1789 | $album->update; # <--- the trigger is called right here. | ||||||||||||||||||||||||||||||||||||||||||
| 1790 | |||||||||||||||||||||||||||||||||||||||||||
| 1791 | ...we would see the following output: | ||||||||||||||||||||||||||||||||||||||||||
| 1792 | |||||||||||||||||||||||||||||||||||||||||||
| 1793 | About to change the name from 'Legend' to 'Greatest Hits' | ||||||||||||||||||||||||||||||||||||||||||
| 1794 | |||||||||||||||||||||||||||||||||||||||||||
| 1795 | =head4 after_update_<field>( $self, $old_value, $new_value ) | ||||||||||||||||||||||||||||||||||||||||||
| 1796 | |||||||||||||||||||||||||||||||||||||||||||
| 1797 | Called just B<after> a field's value is updated. | ||||||||||||||||||||||||||||||||||||||||||
| 1798 | |||||||||||||||||||||||||||||||||||||||||||
| 1799 | So, given the following trigger... | ||||||||||||||||||||||||||||||||||||||||||
| 1800 | |||||||||||||||||||||||||||||||||||||||||||
| 1801 | package App::db::album; | ||||||||||||||||||||||||||||||||||||||||||
| 1802 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 1803 | __PACKAGE__->add_trigger( after_update_name => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 1804 | my ($self, $old_value, $new_value) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 1805 | |||||||||||||||||||||||||||||||||||||||||||
| 1806 | warn "Changed name from '$old_value' to '$new_value'"; | ||||||||||||||||||||||||||||||||||||||||||
| 1807 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1808 | |||||||||||||||||||||||||||||||||||||||||||
| 1809 | ...called with the following code... | ||||||||||||||||||||||||||||||||||||||||||
| 1810 | |||||||||||||||||||||||||||||||||||||||||||
| 1811 | my $artist = App::db::artist->create( name => 'Bob Marley' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1812 | my $album = $artist->add_to_albums( name => 'Legend' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1813 | |||||||||||||||||||||||||||||||||||||||||||
| 1814 | # Now change the name: | ||||||||||||||||||||||||||||||||||||||||||
| 1815 | $album->name( 'Greatest Hits' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1816 | $album->update; # <--- the trigger is called right here. | ||||||||||||||||||||||||||||||||||||||||||
| 1817 | |||||||||||||||||||||||||||||||||||||||||||
| 1818 | ...we would see the following output: | ||||||||||||||||||||||||||||||||||||||||||
| 1819 | |||||||||||||||||||||||||||||||||||||||||||
| 1820 | Changed the name from 'Legend' to 'Greatest Hits' | ||||||||||||||||||||||||||||||||||||||||||
| 1821 | |||||||||||||||||||||||||||||||||||||||||||
| 1822 | =head2 find_column( $name ) | ||||||||||||||||||||||||||||||||||||||||||
| 1823 | |||||||||||||||||||||||||||||||||||||||||||
| 1824 | Returns the name of the column, if the class has that column. | ||||||||||||||||||||||||||||||||||||||||||
| 1825 | |||||||||||||||||||||||||||||||||||||||||||
| 1826 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1827 | |||||||||||||||||||||||||||||||||||||||||||
| 1828 | if( App::db::artist->find_column('name') ) { | ||||||||||||||||||||||||||||||||||||||||||
| 1829 | warn "Artists have names!"; | ||||||||||||||||||||||||||||||||||||||||||
| 1830 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1831 | |||||||||||||||||||||||||||||||||||||||||||
| 1832 | =head2 get_table_info( ) | ||||||||||||||||||||||||||||||||||||||||||
| 1833 | |||||||||||||||||||||||||||||||||||||||||||
| 1834 | Returns a L<Class::DBI::Lite::TableInfo> object fully-populated with all of the | ||||||||||||||||||||||||||||||||||||||||||
| 1835 | information available about the table represented by a class. | ||||||||||||||||||||||||||||||||||||||||||
| 1836 | |||||||||||||||||||||||||||||||||||||||||||
| 1837 | So, given the following table structure: | ||||||||||||||||||||||||||||||||||||||||||
| 1838 | |||||||||||||||||||||||||||||||||||||||||||
| 1839 | create table artists ( | ||||||||||||||||||||||||||||||||||||||||||
| 1840 | artist_id integer unsigned not null primary key auto_increment, | ||||||||||||||||||||||||||||||||||||||||||
| 1841 | name varchar(100) not null | ||||||||||||||||||||||||||||||||||||||||||
| 1842 | ) engine=innodb charset=utf8; | ||||||||||||||||||||||||||||||||||||||||||
| 1843 | |||||||||||||||||||||||||||||||||||||||||||
| 1844 | Here is the example: | ||||||||||||||||||||||||||||||||||||||||||
| 1845 | |||||||||||||||||||||||||||||||||||||||||||
| 1846 | my $info = App::db::artist->get_table_info(); | ||||||||||||||||||||||||||||||||||||||||||
| 1847 | |||||||||||||||||||||||||||||||||||||||||||
| 1848 | my $column = $info->column('name'); | ||||||||||||||||||||||||||||||||||||||||||
| 1849 | warn $column->name; # 'name' | ||||||||||||||||||||||||||||||||||||||||||
| 1850 | warn $column->type; # varchar | ||||||||||||||||||||||||||||||||||||||||||
| 1851 | warn $column->length; # 100 | ||||||||||||||||||||||||||||||||||||||||||
| 1852 | warn $column->is_pk; # '0' (because it's not the Primary Key) | ||||||||||||||||||||||||||||||||||||||||||
| 1853 | warn $column->is_nullable; # 0 (because `not null` was specified on the table) | ||||||||||||||||||||||||||||||||||||||||||
| 1854 | warn $column->default_value; # undef because no default value was specified | ||||||||||||||||||||||||||||||||||||||||||
| 1855 | warn $column->key; # undef because not UNIQUE or PRIMARY KEY | ||||||||||||||||||||||||||||||||||||||||||
| 1856 | |||||||||||||||||||||||||||||||||||||||||||
| 1857 | foreach my $column ( $info->columns ) { | ||||||||||||||||||||||||||||||||||||||||||
| 1858 | warn $column->name; | ||||||||||||||||||||||||||||||||||||||||||
| 1859 | warn $column->type; | ||||||||||||||||||||||||||||||||||||||||||
| 1860 | warn $column->length; | ||||||||||||||||||||||||||||||||||||||||||
| 1861 | warn $column->is_pk; | ||||||||||||||||||||||||||||||||||||||||||
| 1862 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 1863 | # If the column is an 'enum' field: | ||||||||||||||||||||||||||||||||||||||||||
| 1864 | warn join ', ', @{ $column->enum_values }; | ||||||||||||||||||||||||||||||||||||||||||
| 1865 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1866 | |||||||||||||||||||||||||||||||||||||||||||
| 1867 | =head2 pager( \%where, { order_by => 'fields ASC', page_number => 1, page_size => 10 } ) | ||||||||||||||||||||||||||||||||||||||||||
| 1868 | |||||||||||||||||||||||||||||||||||||||||||
| 1869 | Returns a L<Class::DBI::Lite::Pager> object. | ||||||||||||||||||||||||||||||||||||||||||
| 1870 | |||||||||||||||||||||||||||||||||||||||||||
| 1871 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1872 | |||||||||||||||||||||||||||||||||||||||||||
| 1873 | # Step 1: Get our pager: | ||||||||||||||||||||||||||||||||||||||||||
| 1874 | my $pager = App::db::artist->pager({ | ||||||||||||||||||||||||||||||||||||||||||
| 1875 | name => { LIKE => 'Bob%' } | ||||||||||||||||||||||||||||||||||||||||||
| 1876 | }, { | ||||||||||||||||||||||||||||||||||||||||||
| 1877 | order_by => 'name ASC', | ||||||||||||||||||||||||||||||||||||||||||
| 1878 | page_number => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 1879 | page_size => 20, | ||||||||||||||||||||||||||||||||||||||||||
| 1880 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1881 | |||||||||||||||||||||||||||||||||||||||||||
| 1882 | # Step 2: Show the items in that recordset: | ||||||||||||||||||||||||||||||||||||||||||
| 1883 | foreach my $artist ( $pager->items ) { | ||||||||||||||||||||||||||||||||||||||||||
| 1884 | # Do stuff with $artist: | ||||||||||||||||||||||||||||||||||||||||||
| 1885 | print $artist->name; | ||||||||||||||||||||||||||||||||||||||||||
| 1886 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1887 | |||||||||||||||||||||||||||||||||||||||||||
| 1888 | See L<Class::DBI::Lite::Pager> for more details and examples. | ||||||||||||||||||||||||||||||||||||||||||
| 1889 | |||||||||||||||||||||||||||||||||||||||||||
| 1890 | =head2 sql_pager( { data_sql => $str, count_sql => $str, sql_args => \@array }, { page_number => 1, page_size => 10 } ) | ||||||||||||||||||||||||||||||||||||||||||
| 1891 | |||||||||||||||||||||||||||||||||||||||||||
| 1892 | Returns a L<Class::DBI::Lite::Pager> object. | ||||||||||||||||||||||||||||||||||||||||||
| 1893 | |||||||||||||||||||||||||||||||||||||||||||
| 1894 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1895 | |||||||||||||||||||||||||||||||||||||||||||
| 1896 | # Step 1: Get our pager: | ||||||||||||||||||||||||||||||||||||||||||
| 1897 | my $pager = App::db::artist->sql_pager({ | ||||||||||||||||||||||||||||||||||||||||||
| 1898 | data_sql => "SELECT * FROM artists WHERE name LIKE ?", | ||||||||||||||||||||||||||||||||||||||||||
| 1899 | count_sql => "SELECT COUNT(*) FROM artists WHERE name LIKE ?", | ||||||||||||||||||||||||||||||||||||||||||
| 1900 | sql_args => [ 'Bob%' ], | ||||||||||||||||||||||||||||||||||||||||||
| 1901 | }, { | ||||||||||||||||||||||||||||||||||||||||||
| 1902 | page_number => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 1903 | page_size => 20, | ||||||||||||||||||||||||||||||||||||||||||
| 1904 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 1905 | |||||||||||||||||||||||||||||||||||||||||||
| 1906 | # Step 2: Show the items in that recordset: | ||||||||||||||||||||||||||||||||||||||||||
| 1907 | foreach my $artist ( $pager->items ) { | ||||||||||||||||||||||||||||||||||||||||||
| 1908 | # Do stuff with $artist: | ||||||||||||||||||||||||||||||||||||||||||
| 1909 | print $artist->name; | ||||||||||||||||||||||||||||||||||||||||||
| 1910 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1911 | |||||||||||||||||||||||||||||||||||||||||||
| 1912 | See L<Class::DBI::Lite::Pager> for more details and examples. | ||||||||||||||||||||||||||||||||||||||||||
| 1913 | |||||||||||||||||||||||||||||||||||||||||||
| 1914 | =head1 OBJECT METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 1915 | |||||||||||||||||||||||||||||||||||||||||||
| 1916 | =head2 Field Methods | ||||||||||||||||||||||||||||||||||||||||||
| 1917 | |||||||||||||||||||||||||||||||||||||||||||
| 1918 | For each of the fields in your table, an "accessor" method will be created. | ||||||||||||||||||||||||||||||||||||||||||
| 1919 | |||||||||||||||||||||||||||||||||||||||||||
| 1920 | So, given the following table structure: | ||||||||||||||||||||||||||||||||||||||||||
| 1921 | |||||||||||||||||||||||||||||||||||||||||||
| 1922 | create table artists ( | ||||||||||||||||||||||||||||||||||||||||||
| 1923 | artist_id integer unsigned not null primary key auto_increment, | ||||||||||||||||||||||||||||||||||||||||||
| 1924 | name varchar(100) not null, | ||||||||||||||||||||||||||||||||||||||||||
| 1925 | ) engine=innodb charset=utf8; | ||||||||||||||||||||||||||||||||||||||||||
| 1926 | |||||||||||||||||||||||||||||||||||||||||||
| 1927 | And the following class: | ||||||||||||||||||||||||||||||||||||||||||
| 1928 | |||||||||||||||||||||||||||||||||||||||||||
| 1929 | package App::db::artist; | ||||||||||||||||||||||||||||||||||||||||||
| 1930 | |||||||||||||||||||||||||||||||||||||||||||
| 1931 | use strict; | ||||||||||||||||||||||||||||||||||||||||||
| 1932 | use warnings 'all'; | ||||||||||||||||||||||||||||||||||||||||||
| 1933 | use base 'My::Model'; | ||||||||||||||||||||||||||||||||||||||||||
| 1934 | |||||||||||||||||||||||||||||||||||||||||||
| 1935 | __PACKAGE__->set_up_table('artists'); | ||||||||||||||||||||||||||||||||||||||||||
| 1936 | |||||||||||||||||||||||||||||||||||||||||||
| 1937 | 1;# return true: | ||||||||||||||||||||||||||||||||||||||||||
| 1938 | |||||||||||||||||||||||||||||||||||||||||||
| 1939 | The C<App::db::artist> class would have the following methods created: | ||||||||||||||||||||||||||||||||||||||||||
| 1940 | |||||||||||||||||||||||||||||||||||||||||||
| 1941 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 1942 | |||||||||||||||||||||||||||||||||||||||||||
| 1943 | =item * artist_id | ||||||||||||||||||||||||||||||||||||||||||
| 1944 | |||||||||||||||||||||||||||||||||||||||||||
| 1945 | Returns the value of the C<artist_id> field the database. This value is read-only | ||||||||||||||||||||||||||||||||||||||||||
| 1946 | and cannot be changed. | ||||||||||||||||||||||||||||||||||||||||||
| 1947 | |||||||||||||||||||||||||||||||||||||||||||
| 1948 | =item * name | ||||||||||||||||||||||||||||||||||||||||||
| 1949 | |||||||||||||||||||||||||||||||||||||||||||
| 1950 | Gets or sets the value of the C<name> field the database. | ||||||||||||||||||||||||||||||||||||||||||
| 1951 | |||||||||||||||||||||||||||||||||||||||||||
| 1952 | To get the value of the C<name> field, do this: | ||||||||||||||||||||||||||||||||||||||||||
| 1953 | |||||||||||||||||||||||||||||||||||||||||||
| 1954 | my $value = $artist->name; | ||||||||||||||||||||||||||||||||||||||||||
| 1955 | |||||||||||||||||||||||||||||||||||||||||||
| 1956 | To set the value of the C<name> field, do this: | ||||||||||||||||||||||||||||||||||||||||||
| 1957 | |||||||||||||||||||||||||||||||||||||||||||
| 1958 | $artist->name( "New Name" ); | ||||||||||||||||||||||||||||||||||||||||||
| 1959 | |||||||||||||||||||||||||||||||||||||||||||
| 1960 | To save those changes to the database you must call C<update>: | ||||||||||||||||||||||||||||||||||||||||||
| 1961 | |||||||||||||||||||||||||||||||||||||||||||
| 1962 | $artist->update; | ||||||||||||||||||||||||||||||||||||||||||
| 1963 | |||||||||||||||||||||||||||||||||||||||||||
| 1964 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1965 | |||||||||||||||||||||||||||||||||||||||||||
| 1966 | =head2 Overriding Setters and Getters | ||||||||||||||||||||||||||||||||||||||||||
| 1967 | |||||||||||||||||||||||||||||||||||||||||||
| 1968 | The accessors/mutators ("setters" and "getters") can be individually overridden | ||||||||||||||||||||||||||||||||||||||||||
| 1969 | within your entity class by implementing C<_set_foo($self, $value)> or | ||||||||||||||||||||||||||||||||||||||||||
| 1970 | C<_get_foo($self)> methods. | ||||||||||||||||||||||||||||||||||||||||||
| 1971 | |||||||||||||||||||||||||||||||||||||||||||
| 1972 | B<NOTE:> In practice this may be more useful for the C<_get_*> methods, as the C<_set_*> | ||||||||||||||||||||||||||||||||||||||||||
| 1973 | methods are usually best left to triggers. | ||||||||||||||||||||||||||||||||||||||||||
| 1974 | |||||||||||||||||||||||||||||||||||||||||||
| 1975 | =head2 id | ||||||||||||||||||||||||||||||||||||||||||
| 1976 | |||||||||||||||||||||||||||||||||||||||||||
| 1977 | Always returns the value of the object's primary column. | ||||||||||||||||||||||||||||||||||||||||||
| 1978 | |||||||||||||||||||||||||||||||||||||||||||
| 1979 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1980 | |||||||||||||||||||||||||||||||||||||||||||
| 1981 | $album->id == $album->album_id; | ||||||||||||||||||||||||||||||||||||||||||
| 1982 | $artist->id == $artist->artist_id; | ||||||||||||||||||||||||||||||||||||||||||
| 1983 | |||||||||||||||||||||||||||||||||||||||||||
| 1984 | =head2 update() | ||||||||||||||||||||||||||||||||||||||||||
| 1985 | |||||||||||||||||||||||||||||||||||||||||||
| 1986 | Causes any changes to an object to be saved to the database. | ||||||||||||||||||||||||||||||||||||||||||
| 1987 | |||||||||||||||||||||||||||||||||||||||||||
| 1988 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1989 | |||||||||||||||||||||||||||||||||||||||||||
| 1990 | $artist->name( 'Big Bob' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1991 | $artist->update; | ||||||||||||||||||||||||||||||||||||||||||
| 1992 | |||||||||||||||||||||||||||||||||||||||||||
| 1993 | =head2 delete() | ||||||||||||||||||||||||||||||||||||||||||
| 1994 | |||||||||||||||||||||||||||||||||||||||||||
| 1995 | Deletes the object from the database. The object is then re-blessed into the special | ||||||||||||||||||||||||||||||||||||||||||
| 1996 | class C<Class::DBI::Lite::Object::Has::Been::Deleted>. | ||||||||||||||||||||||||||||||||||||||||||
| 1997 | |||||||||||||||||||||||||||||||||||||||||||
| 1998 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 1999 | |||||||||||||||||||||||||||||||||||||||||||
| 2000 | $album->delete; | ||||||||||||||||||||||||||||||||||||||||||
| 2001 | |||||||||||||||||||||||||||||||||||||||||||
| 2002 | =head2 discard_changes() | ||||||||||||||||||||||||||||||||||||||||||
| 2003 | |||||||||||||||||||||||||||||||||||||||||||
| 2004 | Causes any changes made to the object that have not been stored in the database | ||||||||||||||||||||||||||||||||||||||||||
| 2005 | to be forgotten. | ||||||||||||||||||||||||||||||||||||||||||
| 2006 | |||||||||||||||||||||||||||||||||||||||||||
| 2007 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 2008 | |||||||||||||||||||||||||||||||||||||||||||
| 2009 | my $artist = App::db::artist->create( name => 'Bob Marley' ); | ||||||||||||||||||||||||||||||||||||||||||
| 2010 | $artist->name( 'Big Bob' ); | ||||||||||||||||||||||||||||||||||||||||||
| 2011 | |||||||||||||||||||||||||||||||||||||||||||
| 2012 | $artist->discard_changes; | ||||||||||||||||||||||||||||||||||||||||||
| 2013 | |||||||||||||||||||||||||||||||||||||||||||
| 2014 | =head1 ADVANCED TOPICS | ||||||||||||||||||||||||||||||||||||||||||
| 2015 | |||||||||||||||||||||||||||||||||||||||||||
| 2016 | =head2 Master/Slave Configuration | ||||||||||||||||||||||||||||||||||||||||||
| 2017 | |||||||||||||||||||||||||||||||||||||||||||
| 2018 | In your My::db::model class: | ||||||||||||||||||||||||||||||||||||||||||
| 2019 | |||||||||||||||||||||||||||||||||||||||||||
| 2020 | Instead of: | ||||||||||||||||||||||||||||||||||||||||||
| 2021 | |||||||||||||||||||||||||||||||||||||||||||
| 2022 | __PACKAGE__->connection( $dsn, $user, $pass ); | ||||||||||||||||||||||||||||||||||||||||||
| 2023 | |||||||||||||||||||||||||||||||||||||||||||
| 2024 | Do this: | ||||||||||||||||||||||||||||||||||||||||||
| 2025 | |||||||||||||||||||||||||||||||||||||||||||
| 2026 | __PACKAGE__->set_master( $dsn, $user, $pass ); | ||||||||||||||||||||||||||||||||||||||||||
| 2027 | |||||||||||||||||||||||||||||||||||||||||||
| 2028 | __PACKAGE__->set_slaves( | ||||||||||||||||||||||||||||||||||||||||||
| 2029 | [ $dsn1, $user1, $pass1 ], | ||||||||||||||||||||||||||||||||||||||||||
| 2030 | [ $dsn2, $user2, $pass2 ], | ||||||||||||||||||||||||||||||||||||||||||
| 2031 | [ $dsn3, $user3, $pass3 ], | ||||||||||||||||||||||||||||||||||||||||||
| 2032 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 2033 | |||||||||||||||||||||||||||||||||||||||||||
| 2034 | Your slaves will be shuffled. | ||||||||||||||||||||||||||||||||||||||||||
| 2035 | |||||||||||||||||||||||||||||||||||||||||||
| 2036 | Writes will always* go to the master, reads will always go to the slaves. | ||||||||||||||||||||||||||||||||||||||||||
| 2037 | |||||||||||||||||||||||||||||||||||||||||||
| 2038 | *Unless you are inside of a transaction, in which case all reads will also go to the master. | ||||||||||||||||||||||||||||||||||||||||||
| 2039 | |||||||||||||||||||||||||||||||||||||||||||
| 2040 | If you want to switch to a different slave, call 'switch_slave' on your main model class: | ||||||||||||||||||||||||||||||||||||||||||
| 2041 | |||||||||||||||||||||||||||||||||||||||||||
| 2042 | My::db::model->switch_slave(); | ||||||||||||||||||||||||||||||||||||||||||
| 2043 | |||||||||||||||||||||||||||||||||||||||||||
| 2044 | In an ASP4 environment you could add a line like that to an ASP4::RequestFilter. | ||||||||||||||||||||||||||||||||||||||||||
| 2045 | |||||||||||||||||||||||||||||||||||||||||||
| 2046 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 2047 | |||||||||||||||||||||||||||||||||||||||||||
| 2048 | L<Class::DBI::Lite::Tutorial> | ||||||||||||||||||||||||||||||||||||||||||
| 2049 | |||||||||||||||||||||||||||||||||||||||||||
| 2050 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 2051 | |||||||||||||||||||||||||||||||||||||||||||
| 2052 | Copyright John Drago <jdrago_999@yahoo.com>. All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 2053 | |||||||||||||||||||||||||||||||||||||||||||
| 2054 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 2055 | |||||||||||||||||||||||||||||||||||||||||||
| 2056 | This software is B<Free> software and may be used and redistributed under the | ||||||||||||||||||||||||||||||||||||||||||
| 2057 | same terms as perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 2058 | |||||||||||||||||||||||||||||||||||||||||||
| 2059 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 2060 | |||||||||||||||||||||||||||||||||||||||||||