| 1 | package Class::PObject::Template; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # Template.pm,v 1.24 2005/02/20 18:05:00 sherzodr Exp | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | #use diagnostics; | ||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Log::Agent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use vars ('$VERSION'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use overload ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 17 | ▶ | '""' => sub { $_[0]->id }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | fallback=> 1 | |||||||||||||||||||||||||||
| 13 | 3 | ); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | $VERSION = '1.93'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 99 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 18 | 99 | my $class = shift; | |||||||||||||||||||||||||||
| 19 | 99 | ▶ | $class = ref($class) || $class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 99 | logtrc 2, "%s->new()", $class; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 99 | ▶ | croak "Odd number of arguments passed to new(). May result in corrupted data" if @_ % 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 99 | my $props = $class->__props(); | |||||||||||||||||||||||||||
| 26 | 99 | my $self = { | |||||||||||||||||||||||||||
| 27 | columns => { @_ }, # <-- holds key/value pairs | ||||||||||||||||||||||||||||
| 28 | _is_new => 1 | ||||||||||||||||||||||||||||
| 29 | }; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 99 | bless($self, $class); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # It's possible that new() was not given all the column/values. So we | ||||||||||||||||||||||||||||
| 34 | # detect the ones missing, and assign them 'undef' | ||||||||||||||||||||||||||||
| 35 | 99 | for my $colname ( @{$props->{columns}} ) { | |||||||||||||||||||||||||||
| 36 | 378 | ▶ | unless ( defined $self->{columns}->{$colname} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 203 | $self->{columns}->{$colname} = undef | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 99 | $self->pobject_init; | |||||||||||||||||||||||||||
| 42 | 99 | return $self | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # | ||||||||||||||||||||||||||||
| 47 | # Extra init. code should be defined in parent | ||||||||||||||||||||||||||||
| 48 | # | ||||||||||||||||||||||||||||
| 49 | 18 | ▶ | sub pobject_init { } | ||||||||||||||||||||||||||
|
Subroutine
pobject_init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 81 | ▶ | sub set_datasource { | ||||||||||||||||||||||||||
|
Subroutine
set_datasource: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 81 | ▶ | $_[0]->__props()->{"datasource"} = $_[1] if defined( $_[1] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 5 | ▶ | sub set_driver { | ||||||||||||||||||||||||||
|
Subroutine
set_driver: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 5 | ▶ | $_[0]->__props()->{'driver'} = $_[1] if defined( $_[1] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 37 | ▶ | sub set { | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 60 | 37 | my $self = shift; | |||||||||||||||||||||||||||
| 61 | 37 | my ($colname, $colvalue) = @_; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 37 | ▶ | croak "set(): called as class method" unless ref( $self ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 37 | ▶ | croak "set(): missing arguments" unless @_ == 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 37 | my $props = $self->__props(); | |||||||||||||||||||||||||||
| 67 | 37 | my ($typeclass, $args) = $props->{tmap}->{$colname} =~ m/^([a-zA-Z0-9_:]+)(?:\(([^\)]+)\))?$/; | |||||||||||||||||||||||||||
| 68 | 37 | logtrc 3, "col: %s, type: %s, args: %s", $colname, $typeclass, $args; | |||||||||||||||||||||||||||
| 69 | 37 | ▶ | if ( ref $colvalue eq $typeclass ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 2 | $self->{columns}->{$colname} = $colvalue; | |||||||||||||||||||||||||||
| 71 | } else { | ||||||||||||||||||||||||||||
| 72 | 35 | $self->{columns}->{$colname} = $typeclass->new(id=>$colvalue); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 160 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=6 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 160 | my ($self, $colname) = @_; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 160 | ▶ | croak "get(): called as class method" unless ref( $self ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 160 | ▶ | croak "get(): missing arguments" unless defined $colname; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 160 | my $colvalue = $self->{columns}->{$colname}; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # If the value is undef, we should return it as is, not to surprise anyone. | ||||||||||||||||||||||||||||
| 89 | # If we keep going, the user will end up with an object, | ||||||||||||||||||||||||||||
| 90 | # which may not appear as empty | ||||||||||||||||||||||||||||
| 91 | 160 | ▶ | return unless defined( $colvalue ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # If we already have this value in our cache, let's return it | ||||||||||||||||||||||||||||
| 94 | 150 | ▶ | return $colvalue if ref( $colvalue ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # If we come this far, this value is being inquired for the first time. So we should load() it. | ||||||||||||||||||||||||||||
| 97 | # To do this, we first need to identify its column type, to know how to inflate it. | ||||||||||||||||||||||||||||
| 98 | 82 | my $props = $self->__props(); | |||||||||||||||||||||||||||
| 99 | 82 | my ($typeclass, $args) = $props->{tmap}->{ $colname } =~ m/^([a-zA-Z0-9_:]+)(?:\(([^\)]+)\))?$/; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 82 | ▶ | croak "set(): couldn't detect type of column '$colname'" unless $typeclass; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # We should cache the loaded object in the column | ||||||||||||||||||||||||||||
| 104 | 82 | return $self->{columns}->{$colname} = $typeclass->load($colvalue); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 11 | ▶ | sub save { | ||||||||||||||||||||||||||
|
Subroutine
save: called CC=6 SCAR=18.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 110 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 111 | 11 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 11 | ▶ | croak "save(): called as class method" unless ref $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 11 | logtrc 2, "%s->save(%s)", $class, join ", ", @_; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 11 | my $props = $self->__props(); | |||||||||||||||||||||||||||
| 117 | 11 | my $driver_obj = $self->__driver(); | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 11 | my %columns = (); | |||||||||||||||||||||||||||
| 120 | 11 | ▶ | while ( my ($k, $v) = each %{ $self->{columns} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | # We should realize that column values are of Class::PObject::Type class, | ||||||||||||||||||||||||||||
| 122 | # so their values should be stringified before being passed to drivers' save() method. | ||||||||||||||||||||||||||||
| 123 | 42 | ▶ | $v = $v->id while ref $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 42 | $columns{$k} = $v | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # We call the driver's save() method, with the name of the class, all the props passed to pobject(), | ||||||||||||||||||||||||||||
| 128 | # and column values to be stored | ||||||||||||||||||||||||||||
| 129 | 11 | my $rv = $driver_obj->save($class, $props, \%columns); | |||||||||||||||||||||||||||
| 130 | 11 | ▶ | unless ( defined $rv ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | $self->errstr($driver_obj->errstr); | |||||||||||||||||||||||||||
| 132 | 0 | logerr $self->errstr; | |||||||||||||||||||||||||||
| 133 | return undef | ||||||||||||||||||||||||||||
| 134 | 0 | } | |||||||||||||||||||||||||||
| 135 | 11 | $self->id($rv); | |||||||||||||||||||||||||||
| 136 | 11 | return $rv | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 2 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 146 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 147 | 2 | ▶ | croak "fetch(): called as object method" if ref( $class ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 2 | my ($terms, $args) = @_; | |||||||||||||||||||||||||||
| 150 | 2 | ▶ | $terms ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$terms ||= {}
MC/DC100%1/1
$terms ||= {}
$terms
Truth table100%2/2
A
A= $terms
| |||||||||||||||||||||||||||||
| 151 | 2 | ▶ | $args ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args ||= {}
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 2 | logtrc 2, "%s->fetch()", $class; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 2 | my $props = $class->__props(); | |||||||||||||||||||||||||||
| 156 | 2 | my $driver = $class->__driver(); | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 2 | ▶ | while ( my ($k, $v) = each %$terms ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 1 | ▶ | $v = $v->id while ref $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 1 | $terms->{$k} = $v | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 2 | my $ids = $driver->load_ids($class, $props, $terms, $args); | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 2 | require Class::PObject::Iterator; | |||||||||||||||||||||||||||
| 165 | 2 | return Class::PObject::Iterator->new($class, $ids); | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 39 | ▶ | sub load { | ||||||||||||||||||||||||||
|
Subroutine
load: called CC=18 SCAR=29.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 176 | 39 | my $class = shift; | |||||||||||||||||||||||||||
| 177 | 39 | ▶ | croak "load(): called as object method" if ref($class); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 39 | my ($terms, $args) = @_; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | # | ||||||||||||||||||||||||||||
| 181 | # Initializing class attributes. This only makes difference if the class | ||||||||||||||||||||||||||||
| 182 | # if making use of pobject_init() | ||||||||||||||||||||||||||||
| 183 | # | ||||||||||||||||||||||||||||
| 184 | 39 | $class->new(); | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 39 | logtrc 2, "%s->load()", $class; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 39 | ▶ | $terms = {} unless defined $terms; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 39 | ▶ | $args = {} unless defined $args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # If we're called in void context, why bother? | ||||||||||||||||||||||||||||
| 192 | 39 | ▶ | return undef unless defined(wantarray); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 39 | ▶ | unless ( wantarray ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 22 | $args->{"limit"} = 1; | |||||||||||||||||||||||||||
| 196 | 22 | ▶ | $args->{"sort"} ||= 'id'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"sort"} ||= "id"
MC/DC100%1/1
$args->{"sort"} ||= "id"
$args->{"sort"}
Truth table100%2/2
A
A= $args->{"sort"}
| |||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 39 | my $props = $class->__props(); | |||||||||||||||||||||||||||
| 200 | 39 | my $driver_obj = $class->__driver(); | |||||||||||||||||||||||||||
| 201 | 39 | my $ids = []; # we first initialize an empty ID list | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # now, if we had a single argument, and that argument was not a HASH, | ||||||||||||||||||||||||||||
| 204 | # we assume we received an ID | ||||||||||||||||||||||||||||
| 205 | 39 | ▶ | if ( defined($terms) && (ref $terms ne 'HASH') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $terms and ref $terms ne "HASH"
MC/DC50%1/2
defined $terms and ref $terms ne "HASH"
defined $terms
ref $terms ne "HASH"
Truth table66%2/3
A and B
A= defined $termsB= ref $terms ne "HASH"
| |||||||||||||||||||||||||||||
| 206 | 18 | $ids = [ $terms ] | |||||||||||||||||||||||||||
| 207 | } else { | ||||||||||||||||||||||||||||
| 208 | 21 | ▶ | while ( my ($k, $v) = each %$terms ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 14 | ▶ | if ( $props->{tmap}->{$k} =~ m/^(MD5|ENCRYPT)$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | carp "cannot select by '$1' type columns (Yet!)" | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | # | ||||||||||||||||||||||||||||
| 213 | # Following trick will enable load(\%terms) syntax to work | ||||||||||||||||||||||||||||
| 214 | # by passing objects. | ||||||||||||||||||||||||||||
| 215 | # | ||||||||||||||||||||||||||||
| 216 | 14 | ▶ | $terms->{$k} = $terms->{$k}->id while ref $terms->{$k}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | 21 | ▶ | $ids = $driver_obj->load_ids($class, $props, $terms, $args) or return | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | 39 | ▶ | return () unless scalar(@$ids); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | # if called in array context, we return an array of objects: | ||||||||||||||||||||||||||||
| 222 | 38 | ▶ | if ( wantarray() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | 16 | my @data_set = (); | |||||||||||||||||||||||||||
| 224 | 16 | for my $id ( @$ids ) { | |||||||||||||||||||||||||||
| 225 | 28 | ▶ | my $row = $driver_obj->load($class, $props, $id) or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 28 | my $o = $class->new( %$row ); | |||||||||||||||||||||||||||
| 227 | 28 | $o->{_is_new} = 0; | |||||||||||||||||||||||||||
| 228 | 28 | push @data_set, $o | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | return @data_set | ||||||||||||||||||||||||||||
| 231 | 16 | } | |||||||||||||||||||||||||||
| 232 | # if we come this far, we're being called in scalar context | ||||||||||||||||||||||||||||
| 233 | 22 | ▶ | my $row = $driver_obj->load($class, $props, $ids->[0]) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | 21 | my $o = $class->new( %$row ); | |||||||||||||||||||||||||||
| 235 | 21 | $o->{_is_new} = 0; | |||||||||||||||||||||||||||
| 236 | 21 | return $o | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 1 | ▶ | sub remove { | ||||||||||||||||||||||||||
|
Subroutine
remove: called CC=4 SCAR=14.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 242 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 243 | 1 | ▶ | croak "remove(): called as class method" unless ref($self); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 1 | logtrc 2, "%s->remove()", ref $self; | |||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 1 | my $props = $self->__props(); | |||||||||||||||||||||||||||
| 248 | 1 | my $driver_obj = $self->__driver(); | |||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | # if 'id' field is missing, most likely it's because this particular object | ||||||||||||||||||||||||||||
| 251 | # hasn't been saved into disk yet | ||||||||||||||||||||||||||||
| 252 | 1 | ▶ | croak "remove(): object id is missing. Cannot remove" unless defined $self->id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 1 | my $rv = $driver_obj->remove( ref($self), $props, $self->id); | |||||||||||||||||||||||||||
| 255 | 1 | ▶ | unless ( defined $rv ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | 0 | $self->errstr($driver_obj->errstr); | |||||||||||||||||||||||||||
| 257 | return undef | ||||||||||||||||||||||||||||
| 258 | 0 | } | |||||||||||||||||||||||||||
| 259 | 1 | return $rv | |||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 5 | ▶ | sub remove_all { | ||||||||||||||||||||||||||
|
Subroutine
remove_all: called CC=6 SCAR=18.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 269 | 5 | my $class = shift; | |||||||||||||||||||||||||||
| 270 | 5 | my ($terms) = @_; | |||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 5 | ▶ | croak "remove_all(): called as object method" if ref($class); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | 5 | logtrc 2, "%s->remove_all()", $class; | |||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 5 | ▶ | $terms ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$terms ||= {}
MC/DC100%1/1
$terms ||= {}
$terms
Truth table100%2/2
A
A= $terms
| |||||||||||||||||||||||||||||
| 276 | 5 | my $props = $class->__props(); | |||||||||||||||||||||||||||
| 277 | 5 | my $driver_obj = $class->__driver(); | |||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 5 | ▶ | while ( my ($k, $v) = each %$terms ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | 1 | ▶ | $v = $v->id while ref $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 281 | 1 | $terms->{$k} = $v | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 5 | my $rv = $driver_obj->remove_all($class, $props, $terms); | |||||||||||||||||||||||||||
| 285 | 5 | ▶ | unless ( defined $rv ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | 0 | $class->errstr($driver_obj->errstr()); | |||||||||||||||||||||||||||
| 287 | return undef | ||||||||||||||||||||||||||||
| 288 | 0 | } | |||||||||||||||||||||||||||
| 289 | 5 | return 1 | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 5 | ▶ | sub drop_datasource { | ||||||||||||||||||||||||||
|
Subroutine
drop_datasource: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 296 | 5 | my $class = shift; | |||||||||||||||||||||||||||
| 297 | 5 | ▶ | croak "drop_datasource(): called as object method" if ref( $class ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 5 | logtrc 2, "%s->drop_datasource", $class; | |||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | 5 | my $props = $class->__props(); | |||||||||||||||||||||||||||
| 301 | 5 | my $driver_obj = $class->__driver(); | |||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | 5 | my $rv = $driver_obj->drop_datasource($class, $props); | |||||||||||||||||||||||||||
| 304 | 5 | ▶ | unless ( defined $rv ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 305 | 0 | $class->errstr( $driver_obj->errstr ); | |||||||||||||||||||||||||||
| 306 | return undef | ||||||||||||||||||||||||||||
| 307 | 0 | } | |||||||||||||||||||||||||||
| 308 | 5 | return 1 | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 15 | ▶ | sub count { | ||||||||||||||||||||||||||
|
Subroutine
count: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 317 | 15 | my ($class, $terms) = @_; | |||||||||||||||||||||||||||
| 318 | 15 | ▶ | croak "count(): called as object method" if ref ($class); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 319 | 15 | logtrc 2, "%s->count()", $class; | |||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 15 | ▶ | $terms ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$terms ||= {}
MC/DC100%1/1
$terms ||= {}
$terms
Truth table100%2/2
A
A= $terms
| |||||||||||||||||||||||||||||
| 322 | 15 | my $props = $class->__props(); | |||||||||||||||||||||||||||
| 323 | 15 | my $driver_obj = $class->__driver(); | |||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 15 | ▶ | while ( my ($k, $v) = each %$terms ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | 6 | ▶ | $v = $v->id while ref $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | 6 | $terms->{$k} = $v | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | 15 | return $driver_obj->count($class, $props, $terms) | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 7 | ▶ | sub errstr { | ||||||||||||||||||||||||||
|
Subroutine
errstr: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 335 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 336 | 7 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 339 | 7 | ▶ | if ( defined $_[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | 0 | ${ "$class\::errstr" } = $_[0] | |||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | 7 | return ${ "$class\::errstr" } | |||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 2 | ▶ | sub columns { | ||||||||||||||||||||||||||
|
Subroutine
columns: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 355 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 356 | 2 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 2 | logtrc 2, "%s->columns()", $class; | |||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 2 | my %columns = (); | |||||||||||||||||||||||||||
| 361 | 2 | ▶ | while ( my ($k, $v) = each %{$self->{columns}} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 362 | 7 | ▶ | $v = $v->id while ref $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 363 | 7 | $columns{$k} = $v; | |||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 2 | return \%columns | |||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 1 | ▶ | sub dump { | ||||||||||||||||||||||||||
|
Subroutine
dump: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 376 | 1 | my ($self, $indent) = @_; | |||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | 1 | require Data::Dumper; | |||||||||||||||||||||||||||
| 379 | 1 | my $d = Data::Dumper->new([$self], [ref $self]); | |||||||||||||||||||||||||||
| 380 | 1 | ▶ | $d->Indent($indent||2); | ||||||||||||||||||||||||||
|
Condition50%1/2
$indent || 2
MC/DC0%0/1
$indent || 2
$indent
Truth table50%1/2
A
A= $indent
| |||||||||||||||||||||||||||||
| 381 | 1 | $d->Deepcopy(1); | |||||||||||||||||||||||||||
| 382 | 1 | return $d->Dump() | |||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | 463 | ▶ | sub __props { | ||||||||||||||||||||||||||
|
Subroutine
__props: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 390 | 463 | my $class = shift; | |||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | # | ||||||||||||||||||||||||||||
| 393 | # Can be called either as class or object method | ||||||||||||||||||||||||||||
| 394 | # | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 397 | 463 | ▶ | return ${ (ref($class) || $class) . '::props' } | ||||||||||||||||||||||||||
|
Condition66%2/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table66%2/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | 80 | ▶ | sub __driver { | ||||||||||||||||||||||||||
|
Subroutine
__driver: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 403 | 80 | my $class = shift; | |||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | # | ||||||||||||||||||||||||||||
| 407 | # Can be called either as class or object method | ||||||||||||||||||||||||||||
| 408 | # | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | 80 | my $props = $class->__props(); | |||||||||||||||||||||||||||
| 411 | 80 | my $pm = "Class::PObject::Driver::" . $props->{driver}; | |||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | # closure for getting and setting driver object | ||||||||||||||||||||||||||||
| 414 | 83 | ▶ | my $get_set_driver = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 415 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 416 | 83 | ▶ | if ( defined $_[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 417 | 3 | ${ "$pm\::__O" } = $_[0] | |||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||
| 419 | 83 | return ${ "$pm\::__O" } | |||||||||||||||||||||||||||
| 420 | 80 | }; | |||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | 80 | my $driver_obj = $get_set_driver->(); | |||||||||||||||||||||||||||
| 423 | 80 | ▶ | return $driver_obj if defined $driver_obj; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | # | ||||||||||||||||||||||||||||
| 426 | # If we got this far, it's the first time the driver is | ||||||||||||||||||||||||||||
| 427 | # required. | ||||||||||||||||||||||||||||
| 428 | # | ||||||||||||||||||||||||||||
| 429 | 3 | eval "require $pm"; | |||||||||||||||||||||||||||
| 430 | 3 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 431 | 0 | logcroak $@ | |||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||
| 433 | 3 | $driver_obj = $pm->new(); | |||||||||||||||||||||||||||
| 434 | 3 | ▶ | unless ( defined $driver_obj ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 435 | 0 | $class->errstr($pm->errstr); | |||||||||||||||||||||||||||
| 436 | return undef | ||||||||||||||||||||||||||||
| 437 | 0 | } | |||||||||||||||||||||||||||
| 438 | 3 | $get_set_driver->($driver_obj); | |||||||||||||||||||||||||||
| 439 | 3 | return $driver_obj | |||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | package VARCHAR; | ||||||||||||||||||||||||||||
| 445 | 3 | ▶ | use vars ('@ISA'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 446 | 3 | require Class::PObject::Type::VARCHAR; | |||||||||||||||||||||||||||
| 447 | 3 | @ISA = ("Class::PObject::Type::VARCHAR"); | |||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | package CHAR; | ||||||||||||||||||||||||||||
| 451 | 3 | ▶ | use vars ('@ISA'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 452 | 3 | require Class::PObject::Type::CHAR; | |||||||||||||||||||||||||||
| 453 | 3 | @ISA = ("Class::PObject::Type::CHAR"); | |||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | package INTEGER; | ||||||||||||||||||||||||||||
| 457 | 3 | ▶ | use vars ('@ISA'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 458 | 3 | require Class::PObject::Type::INTEGER; | |||||||||||||||||||||||||||
| 459 | 3 | @ISA = ("Class::PObject::Type::INTEGER"); | |||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | package TEXT; | ||||||||||||||||||||||||||||
| 463 | 3 | ▶ | use vars ('@ISA'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 464 | 3 | require Class::PObject::Type::TEXT; | |||||||||||||||||||||||||||
| 465 | 3 | @ISA = ("Class::PObject::Type::TEXT"); | |||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | package ENCRYPT; | ||||||||||||||||||||||||||||
| 469 | 3 | ▶ | use vars ('@ISA'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 470 | 3 | require Class::PObject::Type::ENCRYPT; | |||||||||||||||||||||||||||
| 471 | 3 | @ISA = ("Class::PObject::Type::ENCRYPT"); | |||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | package MD5; | ||||||||||||||||||||||||||||
| 475 | 3 | ▶ | use vars ('@ISA'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 476 | 3 | require Class::PObject::Type::MD5; | |||||||||||||||||||||||||||
| 477 | 3 | @ISA = ("Class::PObject::Type::MD5"); | |||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | 3 | 1; | |||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | __END__; | ||||||||||||||||||||||||||||