Declarative.pm

filter: Statementstmt 78.1% 132 / 169 Branchbran 37.9% 22 / 58 Conditioncond 35.5% 16 / 45 MC/DCmcdc 4.0% 1 / 25 Subroutinesub 87.5% 28 / 32 Podpod 42.8% 3 / 7 totaltotal 60.1% 202 / 336 CC 51 SCAR 32.9
CC
51
Cov
62%
apply_method
132.0
schema
28.8
new
12.8
CRAP 26.9
« Declarative/Schema.pm ↑ Summary
1 package DBIx::ORM::Declarative;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use DBIx::ORM::Declarative::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use DBIx::ORM::Declarative::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use DBIx::ORM::Declarative::Join;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use DBIx::ORM::Declarative::Row;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use DBIx::ORM::Declarative::JRow;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 $VERSION = '0.22';
13  
14 1 ▶use constant BASE_CLASS => 'DBIx::ORM::Declarative';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use constant SCHEMA_CLASS => 'DBIx::ORM::Declarative::Schema';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use constant TABLE_CLASS => 'DBIx::ORM::Declarative::Table';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use constant JOIN_CLASS => 'DBIx::ORM::Declarative::Join';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use constant ROW_CLASS => 'DBIx::ORM::Declarative::Row';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use constant JROW_CLASS => 'DBIx::ORM::Declarative::JRow';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # Use this to /really/ supress warnings
22 1 ▶use constant w__noop => sub { };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: not called CC=1 SCAR=0.0
23  
24 # The error we return when we have an embarassment of riches
25 1 ▶use constant E_TOOMANYROWS => 'Database error: underdetermined data set';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # The error we return when we've lost the row we just inserted
28 1 ▶use constant E_NOROWSFOUND => 'Database error: inserted data not found';
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # We need to register table & join creation methods; otherwise, we
31 # may wind up blowing up when we try to deal with a table that has
32 # a column with the same name as the table itself
33 1 my %table_methods = ();
34 1 my %join_methods = ();
35  
36 sub table_method
37 5 ▶{
Subroutine table_method: called CC=3 SCAR=11.0
Pod undocumented
38 5 my ($self, $table, $method) = @_;
39 5 ▶ return $table_methods{$table} if $table_methods{$table} and not $method;
Branch: 1 branch
BranchTrueFalseTotal
if $table_methods{$table} and not $method 2 3 5
Condition66%2/3
$table_methods{$table} and not $method
!ll&&!rl&&r
302
MC/DC50%1/2
$table_methods{$table} and not $method
$table_methods{$table} not $method
Truth table66%2/3
A and not(B)
A= $table_methods{$table}B= not $method
ABresult
0X0
100
111
40 3 $table_methods{$table} = $method;
41 }
42  
43 sub join_method
44 1 ▶{
Subroutine join_method: called CC=3 SCAR=12.5
Pod undocumented
45 1 my ($self, $join, $method) = @_;
46 1 ▶ return $join_methods{$join} if $join_methods{$join} and not $method;
Branch: 1 branch
BranchTrueFalseTotal
if $join_methods{$join} and not $method 0 1 1
Condition33%1/3
$join_methods{$join} and not $method
!ll&&!rl&&r
100
MC/DC0%0/2
$join_methods{$join} and not $method
$join_methods{$join} not $method
Truth table33%1/3
A and not(B)
A= $join_methods{$join}B= not $method
ABresult
0X0
100
111
47 1 $join_methods{$join} = $method;
48 }
49  
50 # This applies a method by name - necessary for perl < 5.6
51 sub apply_method
52 0 ▶{
Subroutine apply_method: not called CC=11 SCAR=48.8
Pod undocumented
53 0 my ($obj, $method, $wantarray, @args) = @_;
54 # Check to see if we can apply it directly
55 0 my @rv;
56 eval
57 0 {
58 # We don't need any warnings here
59 0 local $SIG{__WARN__} = __PACKAGE__->w__noop;
60 0 ▶ if($wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 0 0 0
61 {
62 0 @rv = $obj->$method(@args);
63 }
64 else
65 {
66 0 $rv[0] = $obj->$method(@args);
67 }
68 } ;
69 0 ▶ if(not $@)
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
70 {
71 0 ▶ return $wantarray?@rv:$rv[0];
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
72 }
73 0 my $res = UNIVERSAL::can($obj, $method);
74 0 ▶ if($res)
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 0 0 0
75 {
76 0 ▶ return $wantarray?($res->($obj, @args)):scalar($res->($obj, @args));
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
77 }
78 0 $res = UNIVERSAL::can($obj, 'AUTOLOAD');
79 0 ▶ if($res)
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 0 0 0
80 {
81 # We can't directly use the result in $res, because we need to know
82 # which AUTOLOAD it found. Just use eval for now. *sigh*.
83 0 ▶ if($wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 0 0 0
84 {
85 0 eval "\@rv = \$obj->$method(\@args)";
86 }
87 else
88 {
89 0 eval "\$rv[0] = \$obj->$method(\@args)";
90 }
91 0 ▶ carp $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
92 0 ▶ return $wantarray?@rv:$rv[0];
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
93 }
94 0 ▶ my $class = ref $obj || $obj;
Condition0%0/3
ref $obj || $obj
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $obj || $obj
ref $obj $obj
Truth table0%0/3
A || B
A= ref $objB= $obj
ABresult
1X1
011
000
95 0 carp qq(Can't locate object method "$method" via package "$class");
96 }
97  
98 # Create a new DBIx::ORM::Declarative object
99 # Accepts args as a hash
100 # Recognized args are "handle" and "debug"
101 # Unrecognized args are ignored
102 # If used as an object method, copy the handle and debug status, if available
103 sub new
104 13 ▶{
Subroutine new: called CC=8 SCAR=25.5
Pod documented
105 13 my ($self, %args) = @_;
106 13 ▶ my $class = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
670
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
107 13 ▶ my $handle = exists $args{handle}?$args{handle}:$self->handle;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'handle'} ? : 0 13 13
108 13 ▶ my $debug = delete $args{debug} || $self->debug_level || 0;
Condition40%2/5
delete $args{'debug'} || $self->debug_level || 0
l!l
013
delete $args{"debug"} or $self->debug_level
l!l&&r!l&&!r
0013
MC/DC0%0/2
delete $args{'debug'} || $self->debug_level || 0
delete $args{"debug"} $self->debug_level
Truth table33%1/3
A or B
A= delete $args{"debug"}B= $self->debug_level
ABresult
1X1
011
000
109 13 ▶ if(not exists $args{handle} and DBI->can('connect') and $args{dsn})
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args{"handle"} and "DBI"->can("connect") and $args{"dsn"}) 0 13 13
Condition33%2/6
not exists $args{"handle"} and "DBI"->can("connect") and $args{"dsn"}
!ll&&!rl&&r
1300
not exists $args{"handle"} and "DBI"->can("connect")
!ll&&!rl&&r
0130
MC/DC0%0/3
not exists $args{"handle"} and "DBI"->can("connect") and $args{"dsn"}
not exists $args{"handle"} "DBI"->can("connect") $args{"dsn"}
Truth table25%1/4
not(A) and B and C
A= not exists $args{"handle"}B= "DBI"->can("connect")C= $args{"dsn"}
ABCresult
0XX0
10X0
1100
1111
110 {
111 0 $handle = DBI->connect(@args{qw(dsn username password)},
112 { RaiseError => 0, PrintError => 0, AutoCommit => 0 });
113 }
114 13 my $rv = bless { __handle => $handle, __debug_level => $debug }, $class;
115 13 return $rv;
116 }
117  
118 # Custom import method to create schemas during the "use" clause.
119 sub import
120 2 ▶{
Subroutine import: called CC=4 SCAR=14.3
121 2 my ($package, @args) = @_;
122 2 ▶ if(not ref $args[0])
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $args[0]) 1 1 2
123 {
124 1 $package->schema(@args);
125 1 return;
126 }
127 1 for my $arg (@args)
128 {
129 1 ▶ if(not ref $arg)
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $arg) 0 1 1
130 {
131 0 carp "Can't import '$arg' in '$package'";
132 0 next;
133 }
134 1 $package->schema(%$arg);
135 }
136 }
137  
138 # Get or set the DBI handle
139 sub handle
140 13 ▶{
Subroutine handle: called CC=5 SCAR=19.6
Pod documented
141 13 my $self = shift;
142 13 ▶ return unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 7 6 13
143 6 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 6 6
144 {
145 0 delete $self->{__handle};
146 0 ▶ $self->{__handle} = $_[0] if $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 0 0 0
147 0 return $self;
148 }
149 6 ▶ return unless exists $self->{__handle};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"__handle"} 0 6 6
150 6 return $self->{__handle};
151 }
152  
153 # Get or set the debug level
154 sub debug_level
155 13 ▶{
Subroutine debug_level: called CC=5 SCAR=19.4
Pod undocumented
156 13 my $self = shift;
157 13 ▶ return 0 unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 7 6 13
158 6 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 6 6
159 {
160 0 ▶ $self->{__debug_level} = $_[0] || 0;
Condition0%0/2
$_[0] || 0
l!l
00
MC/DC0%0/1
$_[0] || 0
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
161 0 return $self;
162 }
163 6 ▶ return $self->{__debug_level} || 0;
Condition50%1/2
$self->{'__debug_level'} || 0
l!l
06
MC/DC0%0/1
$self->{'__debug_level'} || 0
$self->{'__debug_level'}
Truth table50%1/2
A
A= $self->{'__debug_level'}
Aresult
11
00
164 }
165  
166 # Get the current schema name, or switch to a new schema, or create a
167 # new schema class.
168 sub schema
169 2 ▶{
Subroutine schema: called CC=18 SCAR=33.6
Pod documented
170 2 my ($self, @args) = @_;
171 2 ▶ if(@args<2)
Branch: 1 branch
BranchTrueFalseTotal
if (@args < 2) 1 1 2
172 {
173 1 ▶ if(@args==1)
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1) 0 1 1
174 {
175 0 my $schema = shift @args;
176 0 ▶ return $self->apply_method($schema,wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if $schema and $self->can($schema) 0 0 0
Condition0%0/3
$schema and $self->can($schema)
!ll&&!rl&&r
000
MC/DC0%0/2
$schema and $self->can($schema)
$schema $self->can($schema)
Truth table0%0/3
A and B
A= $schemaB= $self->can($schema)
ABresult
0X0
100
111
177 if $schema and $self->can($schema);
178 0 return $self;
179 }
180 1 my $schema;
181 1 eval { $schema = $self->_schema; };
182 1 return $schema;
183 }
184  
185 # Creating/updating a schema class - process the args
186 1 my %args = @args;
187 1 my $schema = delete $args{schema};
188 1 my $from_dual = delete $args{from_dual};
189 1 ▶ my $limit_clause = delete $args{limit_clause} || 'LIMIT %offset%,%count%';
Condition50%1/2
delete $args{'limit_clause'} || 'LIMIT %offset%,%count%'
l!l
01
MC/DC0%0/1
delete $args{'limit_clause'} || 'LIMIT %offset%,%count%'
delete $args{'limit_clause'}
Truth table50%1/2
A
A= delete $args{'limit_clause'}
Aresult
11
00
190  
191 1 ▶ carp "missing schema argument" and return unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 1 1
Condition25%1/4
$schema || carp('missing schema argument') && (return)
l!l
10
carp("missing schema argument") and return
!ll
00
MC/DC0%0/2
$schema || carp('missing schema argument') && (return)
$schema carp("missing schema argument")
Truth table33%1/3
A || B
A= $schemaB= carp("missing schema argument")
ABresult
1X1
011
000
192 1 my $schema_class = $self->SCHEMA_CLASS . "::$schema";
193  
194 # The meat of the declarations
195 1 ▶ my $tables = delete $args{tables} || [ ];
Condition50%1/2
delete $args{'tables'} || []
l!l
10
MC/DC0%0/1
delete $args{'tables'} || []
delete $args{'tables'}
Truth table50%1/2
A
A= delete $args{'tables'}
Aresult
11
00
196 1 ▶ my $joins = delete $args{joins} || [ ];
Condition50%1/2
delete $args{'joins'} || []
l!l
10
MC/DC0%0/1
delete $args{'joins'} || []
delete $args{'joins'}
Truth table50%1/2
A
A= delete $args{'joins'}
Aresult
11
00
197 1 ▶ my $aliases = delete $args{table_aliases} || { };
Condition50%1/2
delete $args{'table_aliases'} || {}
l!l
01
MC/DC0%0/1
delete $args{'table_aliases'} || {}
delete $args{'table_aliases'}
Truth table50%1/2
A
A= delete $args{'table_aliases'}
Aresult
11
00
198  
199 # We're gonna do a whole mess of symbolic references...
200 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
201 1 my $schema_method_name = $self->BASE_CLASS . "::$schema";
202 1 ▶ if(not @{$schema_class . '::ISA'})
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$schema_class . '::ISA';}) 1 0 1
203 {
204 # Create the class heirarchy
205 1 @{$schema_class . '::ISA'} = ($self->SCHEMA_CLASS);
206  
207 # Let's see if we're called from import...
208 1 my ($pkg, $file, $line, $sub) = caller(1);
209 1 ▶ if($sub eq __PACKAGE__ . '::import' and $pkg ne 'main')
Branch: 1 branch
BranchTrueFalseTotal
if ($sub eq "DBIx::ORM::Declarative::import" and $pkg ne "main") 0 1 1
Condition33%1/3
$sub eq "DBIx::ORM::Declarative::import" and $pkg ne "main"
!ll&&!rl&&r
010
MC/DC0%0/2
$sub eq "DBIx::ORM::Declarative::import" and $pkg ne "main"
$sub eq "DBIx::ORM::Declarative::import" $pkg ne "main"
Truth table33%1/3
A and B
A= $sub eq "DBIx::ORM::Declarative::import"B= $pkg ne "main"
ABresult
0X0
100
111
210 {
211 # Yep - insert ourselves in the upstream @ISA...
212 0 my $isaref = \@{$pkg . '::ISA'};
213 0 ▶ push @$isaref, $schema_class unless $pkg->isa($schema_class);
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg->isa($schema_class) 0 0 0
214 }
215  
216 # Information methods
217 1 ▶ *{$schema_class . '::_schema' } = sub { $schema; };
Subroutine __ANON__: called CC=1 SCAR=0.0
218 1 *{$schema_class . '::_schema_class' } =
219 1 ▶ *{$schema_class . '::_class' } = sub { $schema_class; };
Subroutine __ANON__: called CC=1 SCAR=0.0
220 0 ▶ *{$schema_class . '::_limit_clause' } = sub { $limit_clause; };
Subroutine __ANON__: not called CC=1 SCAR=0.4
221 0 ▶ *{$schema_class . '::_from_dual' } = sub { $from_dual; };
Subroutine __ANON__: not called CC=1 SCAR=0.4
222  
223 # A constructor/mutator
224 1 *{$schema_method_name} = sub
225 2 ▶ {
Subroutine __ANON__: called CC=2 SCAR=7.0
226 2 my ($self) = @_;
227 2 my $rv = $self->new;
228 2 ▶ bless $rv, $schema_class unless $rv->isa($schema_class);
Branch: 1 branch
BranchTrueFalseTotal
unless $rv->isa($schema_class) 2 0 2
229 2 return $rv;
230 1 } ;
231 }
232  
233 # Create the tables
234 1 $schema_class->table(%$_) foreach @$tables;
235  
236 # Create the aliases, if we have any
237 1 $schema_class->alias($_, $aliases->{$_}) foreach keys %$aliases;
238
239 # Create any joins we might have
240 1 $schema_class->join(%$_) foreach @$joins;
241  
242 1 return &{$schema_method_name}($self);
243 }
244  
245 1 1;
« Declarative/Schema.pm ↑ Summary