DBI/Query.pm

filter: Statementstmt 20.5% 14 / 68 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 28.5% 4 / 14 Podpod 40.0% 2 / 5 totaltotal 14.8% 20 / 135 CC 19 SCAR 32.9
CC
19
Cov
13%
run
72.0
new
30.0
new
12.0
CRAP 26.8
« DBI/Relationship/HasMany.pm ↑ Summary DBI.pm »
1 package Class::DBI::Query::Base;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use base 'Class::Accessor';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Storable 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
9 0 my ($class, $fields) = @_;
10 0 my $self = $class->SUPER::new();
11 0 ▶ foreach my $key (keys %{ $fields || {} }) {
Condition0%0/2
$fields || {}
l!l
00
MC/DC0%0/1
$fields || {}
$fields
Truth table0%0/2
A
A= $fields
Aresult
11
00
12 0 $self->set($key => $fields->{$key});
13 }
14 0 $self;
15 }
16  
17 0 ▶sub get {
Subroutine get: not called CC=3 SCAR=24.8
18 0 my ($self, $key) = @_;
19 0 ▶ my @vals = @{ $self->{$key} || [] };
Condition0%0/2
$self->{$key} || []
l!l
00
MC/DC0%0/1
$self->{$key} || []
$self->{$key}
Truth table0%0/2
A
A= $self->{$key}
Aresult
11
00
20 0 ▶ return wantarray ? @vals : $vals[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
21 }
22  
23 0 ▶sub set {
Subroutine set: not called CC=2 SCAR=17.9
24 0 my ($self, $key, @args) = @_;
25 0 ▶ @args = map { ref $_ eq "ARRAY" ? @$_ : $_ } @args;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 0 0 0
26 0 $self->{$key} = [@args];
27 }
28  
29 0 ▶sub clone { dclone shift }
Subroutine clone: not called CC=1 SCAR=6.9
30  
31 package Class::DBI::Query;
32  
33 4 ▶use base 'Class::DBI::Query::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 4 __PACKAGE__->mk_accessors(
36 qw/
37 owner essential sqlname where_clause restrictions order_by kings
38 /
39 );
40  
41 =head1 NAME
42  
43 Class::DBI::Query - Deprecated SQL manager for Class::DBI
44  
45 =head1 SYNOPSIS
46  
47 my $sth = Class::DBI::Query
48 ->new({
49 owner => $class,
50 sqlname => $type,
51 essential => \@columns,
52 where_columns => \@where_cols,
53 })
54 ->run($val);
55  
56  
57 =head1 DESCRIPTION
58  
59 This abstracts away many of the details of the Class::DBI underlying SQL
60 mechanism. For the most part you probably don't want to be interfacing
61 directly with this.
62  
63 The underlying mechanisms are not yet stable, and are subject to change
64 at any time.
65  
66 =cut
67  
68 =head1 OPTIONS
69  
70 A Query can have many options set before executing. Most can either be
71 passed as an option to new(), or set later if you are building the query
72 up dynamically:
73  
74 =head2 owner
75  
76 The Class::DBI subclass that 'owns' this query. In the vast majority
77 of cases a query will return objects - the owner is the class of
78 which instances will be returned.
79  
80 =head2 sqlname
81  
82 This should be the name of a query set up using set_sql.
83  
84 =head2 where_clause
85  
86 This is the raw SQL that will substituted into the 'WHERE %s' in your
87 query. If you have multiple %s's in your query then you should supply
88 a listref of where_clauses. This SQL can include placeholders, which will be
89 used when you call run().
90  
91 =head2 essential
92  
93 When retrieving rows from the database that match the WHERE clause of
94 the query, these are the columns that we fetch back and pre-load the
95 resulting objects with. By default this is the Essential column group
96 of the owner class.
97  
98 =head1 METHODS
99  
100 =head2 where()
101  
102 $query->where($match, @columns);
103  
104 This will extend your 'WHERE' clause by adding a 'AND $column = ?' (or
105 whatever $match is, isntead of "=") for each column passed. If you have
106 multiple WHERE clauses this will extend the last one.
107  
108 =cut
109  
110 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
111 0 my ($class, $self) = @_;
112 0 require Carp;
113 0 Carp::carp "Class::DBI::Query deprecated";
114 0 ▶ $self->{owner} ||= caller;
Condition0%0/3
$self->{"owner"} ||= caller
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"owner"} ||= caller
$self->{"owner"} caller
Truth table0%0/3
A ||= B
A= $self->{"owner"}B= caller
ABresult
1X1
011
000
115 0 ▶ $self->{kings} ||= $self->{owner};
Condition0%0/3
$self->{"kings"} ||= $self->{"owner"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"kings"} ||= $self->{"owner"}
$self->{"kings"} $self->{"owner"}
Truth table0%0/3
A ||= B
A= $self->{"kings"}B= $self->{"owner"}
ABresult
1X1
011
000
116 0 ▶ $self->{essential} ||= [ $self->{owner}->_essential ];
Condition0%0/2
$self->{"essential"} ||= [$self->{"owner"}->_essential]
l!l
00
MC/DC0%0/1
$self->{"essential"} ||= [$self->{"owner"}->_essential]
$self->{"essential"}
Truth table0%0/2
A
A= $self->{"essential"}
Aresult
11
00
117 0 ▶ $self->{sqlname} ||= 'SearchSQL';
Condition0%0/2
$self->{"sqlname"} ||= "SearchSQL"
l!l
00
MC/DC0%0/1
$self->{"sqlname"} ||= "SearchSQL"
$self->{"sqlname"}
Truth table0%0/2
A
A= $self->{"sqlname"}
Aresult
11
00
118 0 return $class->SUPER::new($self);
119 }
120  
121 0 ▶sub _essential_string {
Subroutine _essential_string: not called CC=1 SCAR=6.9
122 0 my $self = shift;
123 0 my $table = $self->owner->table_alias;
124 0 join ", ", map "$table.$_", $self->essential;
125 }
126  
127 0 ▶sub where {
Subroutine where: not called CC=3 SCAR=24.8
Pod documented
128 0 my ($self, $type, @cols) = @_;
129 0 my @where = $self->where_clause;
130 0 ▶ my $last = pop @where || "";
Condition0%0/2
pop @where || ''
l!l
00
MC/DC0%0/1
pop @where || ''
pop @where
Truth table0%0/2
A
A= pop @where
Aresult
11
00
131 0 $last .= join " AND ", $self->restrictions;
132 0 ▶ $last .= " ORDER BY " . $self->order_by if $self->order_by;
Branch: 1 branch
BranchTrueFalseTotal
if $self->order_by 0 0 0
133 0 push @where, $last;
134 0 return @where;
135 }
136  
137 0 ▶sub add_restriction {
Subroutine add_restriction: not called CC=1 SCAR=6.9
Pod undocumented
138 0 my ($self, $sql) = @_;
139 0 $self->restrictions($self->restrictions, $sql);
140 }
141  
142 0 ▶sub tables {
Subroutine tables: not called CC=1 SCAR=6.9
Pod undocumented
143 0 my $self = shift;
144 0 join ", ", map { join " ", $_->table, $_->table_alias } $self->kings;
145 }
146  
147 # my $sth = $query->run(@vals);
148 # Runs the SQL set up in $sqlname, e.g.
149 #
150 # SELECT %s (Essential)
151 # FROM %s (Table)
152 # WHERE %s = ? (SelectCol = @vals)
153 #
154 # substituting the relevant values via sprintf, and then executing with $select_val.
155  
156 0 ▶sub run {
Subroutine run: not called CC=8 SCAR=42.8
Pod undocumented
157 0 my $self = shift;
158 0 ▶ my $owner = $self->owner or Class::DBI->_croak("Query has no owner");
Branch: 1 branch
BranchTrueFalseTotal
my $owner = $self->owner or "Class::DBI"->_croak("Query has no owner") 0 0 0
159 0 ▶ $owner = ref $owner || $owner;
Condition0%0/3
ref $owner || $owner
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $owner || $owner
ref $owner $owner
Truth table0%0/3
A || B
A= ref $ownerB= $owner
ABresult
1X1
011
000
160 0 ▶ $owner->can('db_Main') or $owner->_croak("No database connection defined");
Branch: 1 branch
BranchTrueFalseTotal
$owner->can("db_Main") or $owner->_croak("No database connection defined") 0 0 0
161 0 ▶ my $sql_name = $self->sqlname or $owner->_croak("Query has no SQL");
Branch: 1 branch
BranchTrueFalseTotal
my $sql_name = $self->sqlname or $owner->_croak("Query has no SQL") 0 0 0
162  
163 my @sel_vals = @_
164 0 ▶ ? ref $_[0] eq "ARRAY" ? @{ $_[0] } : (@_)
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
ref $_[0] eq 'ARRAY' ? : 0 0 0
165 : ();
166 0 my $sql_method = "sql_$sql_name";
167  
168 0 my $sth;
169 0 eval {
170 0 $sth =
171 $owner->$sql_method($self->_essential_string, $self->tables,
172 $self->where);
173 0 $sth->execute(@sel_vals);
174 };
175 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
176 0 $owner->_croak(
177 "Can't select for $owner using '$sth->{Statement}' ($sql_name): $@",
178 err => $@);
179 0 return;
180 }
181 0 return $sth;
182 }
183  
184 4 1;
« DBI/Relationship/HasMany.pm ↑ Summary DBI.pm »