| 1 | package EntityModel::Query; | ||||||||||||||||||
| 2 | { | ||||||||||||||||||
| 3 | 33 | $EntityModel::Query::VERSION = '0.102'; | |||||||||||||||||
| 4 | } | ||||||||||||||||||
| 5 | 33 | ▶ | use EntityModel::Class { | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | 33 | _isa => [qw{EntityModel::Query::Base}], | |||||||||||||||||
| 7 | 'forUpdate' => 'string', | ||||||||||||||||||
| 8 | 'limit' => 'int', | ||||||||||||||||||
| 9 | 'offset' => 'int', | ||||||||||||||||||
| 10 | 'field' => { type => 'array', subclass => 'EntityModel::Query::Field' }, | ||||||||||||||||||
| 11 | 'from' => { type => 'array', subclass => 'EntityModel::Query::FromTable' }, | ||||||||||||||||||
| 12 | 'join' => { type => 'array', subclass => 'EntityModel::Query::Join' }, | ||||||||||||||||||
| 13 | 'where' => { type => 'EntityModel::Query::Condition' }, | ||||||||||||||||||
| 14 | 'having' => { type => 'array', subclass => 'EntityModel::Query::Condition' }, | ||||||||||||||||||
| 15 | 'group' => { type => 'array', subclass => 'EntityModel::Query::GroupField' }, | ||||||||||||||||||
| 16 | 'order' => { type => 'array', subclass => 'EntityModel::Query::OrderField' }, | ||||||||||||||||||
| 17 | 'returning' => { type => 'array', subclass => 'EntityModel::Query::ReturningField' }, | ||||||||||||||||||
| 18 | 'db' => { type => 'EntityModel::DB', scope => 'private' }, | ||||||||||||||||||
| 19 | 'transaction' => { type => 'EntityModel::Transaction', scope => 'private' }, | ||||||||||||||||||
| 20 | 33 | }; | |||||||||||||||||
| 21 | 33 | ▶ | no if $] >= 5.017011, warnings => "experimental::smartmatch"; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | =head1 NAME | ||||||||||||||||||
| 24 | |||||||||||||||||||
| 25 | EntityModel::Query - handle SQL queries | ||||||||||||||||||
| 26 | |||||||||||||||||||
| 27 | =head1 VERSION | ||||||||||||||||||
| 28 | |||||||||||||||||||
| 29 | version 0.102 | ||||||||||||||||||
| 30 | |||||||||||||||||||
| 31 | =head1 SYNOPSIS | ||||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | use EntityModel::Query; | ||||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | # Provide a definition on instantiation: | ||||||||||||||||||
| 36 | my $query = EntityModel::Query->new( | ||||||||||||||||||
| 37 | select => [qw(id name)] | ||||||||||||||||||
| 38 | from => 'table', | ||||||||||||||||||
| 39 | where => [ created => { '<' => '2010-01-01' } ], | ||||||||||||||||||
| 40 | limit => 5 | ||||||||||||||||||
| 41 | ); | ||||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | # or using chained methods: | ||||||||||||||||||
| 44 | my $query = EntityModel::Query->new | ||||||||||||||||||
| 45 | ->select( qw(id name) ) | ||||||||||||||||||
| 46 | ->from( 'table' ) | ||||||||||||||||||
| 47 | ->where( | ||||||||||||||||||
| 48 | created => { '<' => '2010-01-01' } | ||||||||||||||||||
| 49 | ) | ||||||||||||||||||
| 50 | ->limit(5); | ||||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | # Extract query as SQL | ||||||||||||||||||
| 53 | my ($sql, @bind) = $query->sqlAndParameters; | ||||||||||||||||||
| 54 | my $sth = $dbh->prepare($sql); | ||||||||||||||||||
| 55 | $sth->execute(@bind); | ||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | =head1 DESCRIPTION | ||||||||||||||||||
| 58 | |||||||||||||||||||
| 59 | Provides an abstraction layer for building SQL queries programmatically. | ||||||||||||||||||
| 60 | |||||||||||||||||||
| 61 | When generating the query, each of the components is called in turn to get an "inline SQL" arrayref. This is an arrayref consisting of | ||||||||||||||||||
| 62 | SQL string fragments interspersed with refs for items such as L<EntityModel::Entity> names, direct scalar values, or L<EntityModel::Field> | ||||||||||||||||||
| 63 | names. | ||||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | As an example: | ||||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | [ 'select * from ', EntityModel::Entity->new('table'), ' where ', EntityModel::Field->new('something'), ' = ', \3 ] | ||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | This can then be used by L<sqlAndParameters> to generate: | ||||||||||||||||||
| 70 | |||||||||||||||||||
| 71 | 'select * from "table" where "something" = ?', 3 | ||||||||||||||||||
| 72 | |||||||||||||||||||
| 73 | or as a plain SQL string (perhaps for diagnostic purposes) from L<sqlString> as: | ||||||||||||||||||
| 74 | |||||||||||||||||||
| 75 | 'select * from "table" where "something" = 3' | ||||||||||||||||||
| 76 | |||||||||||||||||||
| 77 | =cut | ||||||||||||||||||
| 78 | |||||||||||||||||||
| 79 | # Need both of these available and we may be running without EntityModel behind us | ||||||||||||||||||
| 80 | 33 | ▶ | use EntityModel::Entity; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 81 | 33 | ▶ | use EntityModel::Field; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 82 | |||||||||||||||||||
| 83 | # All the query subtypes | ||||||||||||||||||
| 84 | 33 | ▶ | use EntityModel::Query::Select; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 85 | 33 | ▶ | use EntityModel::Query::Update; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 86 | 33 | ▶ | use EntityModel::Query::Insert; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 87 | 33 | ▶ | use EntityModel::Query::Delete; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 88 | |||||||||||||||||||
| 89 | # Components | ||||||||||||||||||
| 90 | 33 | ▶ | use EntityModel::Query::FromTable; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 91 | 33 | ▶ | use EntityModel::Query::Field; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 92 | 33 | ▶ | use EntityModel::Query::InsertField; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 93 | 33 | ▶ | use EntityModel::Query::UpdateField; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 94 | 33 | ▶ | use EntityModel::Query::OrderField; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 95 | 33 | ▶ | use EntityModel::Query::Join; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 96 | |||||||||||||||||||
| 97 | # PostgreSQL extension | ||||||||||||||||||
| 98 | 33 | ▶ | use EntityModel::Query::ReturningField; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 99 | |||||||||||||||||||
| 100 | # Multi-query pieces | ||||||||||||||||||
| 101 | 33 | ▶ | use EntityModel::Query::Union; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 102 | 33 | ▶ | use EntityModel::Query::UnionAll; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 103 | 33 | ▶ | use EntityModel::Query::Intersect; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 104 | 33 | ▶ | use EntityModel::Query::Except; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 105 | |||||||||||||||||||
| 106 | 33 | ▶ | use Carp qw/confess/; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 107 | |||||||||||||||||||
| 108 | =head1 METHODS | ||||||||||||||||||
| 109 | |||||||||||||||||||
| 110 | =cut | ||||||||||||||||||
| 111 | |||||||||||||||||||
| 112 | =head2 new | ||||||||||||||||||
| 113 | |||||||||||||||||||
| 114 | Construct a new L<EntityModel::Query>. Most of the work is passed off to L<parse_spec>. | ||||||||||||||||||
| 115 | |||||||||||||||||||
| 116 | =cut | ||||||||||||||||||
| 117 | |||||||||||||||||||
| 118 | 11 | ▶ | sub new { | ||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||
| 119 | 11 | my $class = shift; | |||||||||||||||||
| 120 | 11 | my $self = bless { }, $class; | |||||||||||||||||
| 121 | 11 | ▶ | $self->parse_spec(@_) if @_; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 122 | 11 | return $self; | |||||||||||||||||
| 123 | } | ||||||||||||||||||
| 124 | |||||||||||||||||||
| 125 | =head2 type | ||||||||||||||||||
| 126 | |||||||||||||||||||
| 127 | Returns the type of the current query. The query object will be reblessed into an appropriate | ||||||||||||||||||
| 128 | subclass depending on whether this is an insert, select, delete etc. A query that has not been | ||||||||||||||||||
| 129 | reblessed is invalid. | ||||||||||||||||||
| 130 | |||||||||||||||||||
| 131 | =cut | ||||||||||||||||||
| 132 | |||||||||||||||||||
| 133 | 0 | ▶ | sub type { confess "Virtual type - this is likely because you did not specify valid insert/select/delete criteria"; } | ||||||||||||||||
|
Subroutine
type: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 134 | |||||||||||||||||||
| 135 | =head2 parse_spec | ||||||||||||||||||
| 136 | |||||||||||||||||||
| 137 | Parse the specification we were given. | ||||||||||||||||||
| 138 | |||||||||||||||||||
| 139 | =cut | ||||||||||||||||||
| 140 | |||||||||||||||||||
| 141 | 11 | ▶ | sub parse_spec { | ||||||||||||||||
|
Subroutine
parse_spec: called CC=6 SCAR=19.5
Pod
documented
| |||||||||||||||||||
| 142 | 11 | my $self = shift; | |||||||||||||||||
| 143 | 11 | my @details = @_; | |||||||||||||||||
| 144 | SPEC: | ||||||||||||||||||
| 145 | 11 | ▶ | while(@details) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 146 | 35 | my $k = shift(@details); | |||||||||||||||||
| 147 | 35 | my $v = shift(@details); | |||||||||||||||||
| 148 | |||||||||||||||||||
| 149 | 35 | ▶ | next SPEC if $k eq 'key'; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 150 | |||||||||||||||||||
| 151 | # Queries such as 'insert into' => 'insert_into' | ||||||||||||||||||
| 152 | 35 | $k =~ s/\s+/_/g; | |||||||||||||||||
| 153 | |||||||||||||||||||
| 154 | # FIXME haxx | ||||||||||||||||||
| 155 | 35 | ▶ | if($k eq 'db') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 156 | 0 | $self->db($v); | |||||||||||||||||
| 157 | } elsif($k eq 'transaction') { | ||||||||||||||||||
| 158 | 0 | $self->transaction($v); | |||||||||||||||||
| 159 | } elsif(my $handler = $self->can_parse($k)) { | ||||||||||||||||||
| 160 | 35 | $handler->($self, $v); | |||||||||||||||||
| 161 | } else { | ||||||||||||||||||
| 162 | 0 | die "Could not find method for $k"; | |||||||||||||||||
| 163 | } | ||||||||||||||||||
| 164 | } | ||||||||||||||||||
| 165 | 11 | return $self; | |||||||||||||||||
| 166 | } | ||||||||||||||||||
| 167 | |||||||||||||||||||
| 168 | =head2 parse_base | ||||||||||||||||||
| 169 | |||||||||||||||||||
| 170 | Base method for parsing. | ||||||||||||||||||
| 171 | |||||||||||||||||||
| 172 | =cut | ||||||||||||||||||
| 173 | |||||||||||||||||||
| 174 | 29 | ▶ | sub parse_base { | ||||||||||||||||
|
Subroutine
parse_base: called CC=8 SCAR=21.5
Pod
documented
| |||||||||||||||||||
| 175 | 29 | my $self = shift; | |||||||||||||||||
| 176 | 29 | my $spec = shift; | |||||||||||||||||
| 177 | 29 | my %arg = @_; | |||||||||||||||||
| 178 | |||||||||||||||||||
| 179 | 29 | my $meth = delete $arg{method}; | |||||||||||||||||
| 180 | 29 | my $type = delete $arg{type}; | |||||||||||||||||
| 181 | |||||||||||||||||||
| 182 | # Capital letter means a class of some sort. Arbitrary but at least it's simple. | ||||||||||||||||||
| 183 | 29 | my $extType = ucfirst($type) eq $type; | |||||||||||||||||
| 184 | |||||||||||||||||||
| 185 | 29 | ▶ | if(ref $spec ~~ 'ARRAY') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 186 | 3 | ▶ | $self->$meth->push($extType ? $type->new($_) : $_) foreach @$spec; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 187 | } elsif(ref $spec ~~ 'HASH') { | ||||||||||||||||||
| 188 | # FIXME wrong if $extType is not set? | ||||||||||||||||||
| 189 | 8 | ▶ | $self->$meth->push($extType ? $type->new($spec) : $spec); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 190 | } elsif(ref $spec) { | ||||||||||||||||||
| 191 | 0 | $self->$meth->push($spec); | |||||||||||||||||
| 192 | # die "Don't know how to handle ref $spec for $meth"; | ||||||||||||||||||
| 193 | } else { | ||||||||||||||||||
| 194 | 18 | ▶ | $self->$meth->push($extType ? $type->new($spec) : $spec); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 195 | } | ||||||||||||||||||
| 196 | 29 | return $self; | |||||||||||||||||
| 197 | } | ||||||||||||||||||
| 198 | |||||||||||||||||||
| 199 | |||||||||||||||||||
| 200 | =head2 reclassify | ||||||||||||||||||
| 201 | |||||||||||||||||||
| 202 | Virtual method to allow subclass to perform any required updates after reblessing to an alternative class. | ||||||||||||||||||
| 203 | |||||||||||||||||||
| 204 | =cut | ||||||||||||||||||
| 205 | |||||||||||||||||||
| 206 | 11 | ▶ | sub reclassify { $_[0] } | ||||||||||||||||
|
Subroutine
reclassify: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 207 | |||||||||||||||||||
| 208 | |||||||||||||||||||
| 209 | =head2 upgradeTo | ||||||||||||||||||
| 210 | |||||||||||||||||||
| 211 | Upgrade an existing L<EntityModel::Query> object to a subclass. | ||||||||||||||||||
| 212 | |||||||||||||||||||
| 213 | =cut | ||||||||||||||||||
| 214 | |||||||||||||||||||
| 215 | 11 | ▶ | sub upgradeTo { | ||||||||||||||||
|
Subroutine
upgradeTo: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 216 | 11 | my $self = shift; | |||||||||||||||||
| 217 | 11 | my $class = shift; | |||||||||||||||||
| 218 | 11 | bless $self, $class; | |||||||||||||||||
| 219 | 11 | $self->reclassify; | |||||||||||||||||
| 220 | } | ||||||||||||||||||
| 221 | |||||||||||||||||||
| 222 | =head2 parse_limit | ||||||||||||||||||
| 223 | |||||||||||||||||||
| 224 | Handle a 'limit' directive. | ||||||||||||||||||
| 225 | |||||||||||||||||||
| 226 | =cut | ||||||||||||||||||
| 227 | |||||||||||||||||||
| 228 | 1 | ▶ | sub parse_limit { | ||||||||||||||||
|
Subroutine
parse_limit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 229 | 1 | my $self = shift; | |||||||||||||||||
| 230 | 1 | my $v = shift; | |||||||||||||||||
| 231 | 1 | $self->limit($v); | |||||||||||||||||
| 232 | 1 | return $self; | |||||||||||||||||
| 233 | } | ||||||||||||||||||
| 234 | |||||||||||||||||||
| 235 | =head2 parse_group | ||||||||||||||||||
| 236 | |||||||||||||||||||
| 237 | =cut | ||||||||||||||||||
| 238 | |||||||||||||||||||
| 239 | 4 | ▶ | sub parse_group { | ||||||||||||||||
|
Subroutine
parse_group: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 240 | 4 | my $self = shift; | |||||||||||||||||
| 241 | 4 | $self->parse_base( | |||||||||||||||||
| 242 | @_, | ||||||||||||||||||
| 243 | method => 'group', | ||||||||||||||||||
| 244 | type => 'EntityModel::Query::GroupField' | ||||||||||||||||||
| 245 | ); | ||||||||||||||||||
| 246 | } | ||||||||||||||||||
| 247 | |||||||||||||||||||
| 248 | =head2 parse_where | ||||||||||||||||||
| 249 | |||||||||||||||||||
| 250 | =cut | ||||||||||||||||||
| 251 | |||||||||||||||||||
| 252 | 6 | ▶ | sub parse_where { | ||||||||||||||||
|
Subroutine
parse_where: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 253 | 6 | my $self = shift; | |||||||||||||||||
| 254 | 6 | $self->where(EntityModel::Query::Condition->new(@_)); | |||||||||||||||||
| 255 | 6 | return $self; | |||||||||||||||||
| 256 | } | ||||||||||||||||||
| 257 | |||||||||||||||||||
| 258 | =head2 typeSQL | ||||||||||||||||||
| 259 | |||||||||||||||||||
| 260 | Proxy method for L<type>, returns the SQL string representation for the current query type (such as 'select' or 'insert into'). | ||||||||||||||||||
| 261 | |||||||||||||||||||
| 262 | =cut | ||||||||||||||||||
| 263 | |||||||||||||||||||
| 264 | 16 | ▶ | sub typeSQL { shift->type } | ||||||||||||||||
|
Subroutine
typeSQL: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 265 | |||||||||||||||||||
| 266 | =head2 fieldsSQL | ||||||||||||||||||
| 267 | |||||||||||||||||||
| 268 | Generate the SQL for fields. | ||||||||||||||||||
| 269 | |||||||||||||||||||
| 270 | =cut | ||||||||||||||||||
| 271 | |||||||||||||||||||
| 272 | 16 | ▶ | sub fieldsSQL { | ||||||||||||||||
|
Subroutine
fieldsSQL: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||
| 273 | 16 | my $self = shift; | |||||||||||||||||
| 274 | my $fields = join(', ', map { | ||||||||||||||||||
| 275 | 16 | ▶ | $_->asString . ($_->alias ? (' as ' . $_->alias) : ''); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 276 | } $self->field->list); | ||||||||||||||||||
| 277 | 16 | ▶ | return unless $fields; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 278 | 16 | return $fields; | |||||||||||||||||
| 279 | } | ||||||||||||||||||
| 280 | |||||||||||||||||||
| 281 | =head2 fromSQL | ||||||||||||||||||
| 282 | |||||||||||||||||||
| 283 | SQL for the 'from' clause. | ||||||||||||||||||
| 284 | |||||||||||||||||||
| 285 | =cut | ||||||||||||||||||
| 286 | |||||||||||||||||||
| 287 | 16 | ▶ | sub fromSQL { | ||||||||||||||||
|
Subroutine
fromSQL: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 288 | 16 | my $self = shift; | |||||||||||||||||
| 289 | 16 | my $from = join(', ', map { $_->asString } $self->from->list); | |||||||||||||||||
| 290 | 16 | ▶ | return unless $from; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 291 | 14 | logDebug("From " . $from); | |||||||||||||||||
| 292 | 14 | return 'from ' . $from; | |||||||||||||||||
| 293 | } | ||||||||||||||||||
| 294 | |||||||||||||||||||
| 295 | =head2 limitSQL | ||||||||||||||||||
| 296 | |||||||||||||||||||
| 297 | =cut | ||||||||||||||||||
| 298 | |||||||||||||||||||
| 299 | 16 | ▶ | sub limitSQL { | ||||||||||||||||
|
Subroutine
limitSQL: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||
| 300 | 16 | my $self = shift; | |||||||||||||||||
| 301 | 16 | ▶ | my $sql = (exists $self->{limit}) ? ('limit ' . $self->limit) : ''; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 302 | 16 | ▶ | return unless $sql; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 303 | 2 | return $sql; | |||||||||||||||||
| 304 | } | ||||||||||||||||||
| 305 | |||||||||||||||||||
| 306 | =head2 offsetSQL | ||||||||||||||||||
| 307 | |||||||||||||||||||
| 308 | =cut | ||||||||||||||||||
| 309 | |||||||||||||||||||
| 310 | 16 | ▶ | sub offsetSQL { | ||||||||||||||||
|
Subroutine
offsetSQL: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||
| 311 | 16 | my $self = shift; | |||||||||||||||||
| 312 | 16 | ▶ | my $sql = (exists $self->{offset}) ? ('offset ' . $self->offset) : ''; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 313 | 16 | ▶ | return unless $sql; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 314 | 0 | return $sql; | |||||||||||||||||
| 315 | } | ||||||||||||||||||
| 316 | |||||||||||||||||||
| 317 | =head2 orderSQL | ||||||||||||||||||
| 318 | |||||||||||||||||||
| 319 | =cut | ||||||||||||||||||
| 320 | |||||||||||||||||||
| 321 | 16 | ▶ | sub orderSQL { | ||||||||||||||||
|
Subroutine
orderSQL: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 322 | 16 | my $self = shift; | |||||||||||||||||
| 323 | 16 | my $sql = join(', ', map { $_->asString } $self->order->list); | |||||||||||||||||
| 324 | 16 | ▶ | return unless $sql; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 325 | 4 | return 'order by ' . $sql; | |||||||||||||||||
| 326 | } | ||||||||||||||||||
| 327 | |||||||||||||||||||
| 328 | =head2 groupSQL | ||||||||||||||||||
| 329 | |||||||||||||||||||
| 330 | =cut | ||||||||||||||||||
| 331 | |||||||||||||||||||
| 332 | 16 | ▶ | sub groupSQL { | ||||||||||||||||
|
Subroutine
groupSQL: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 333 | 16 | my $self = shift; | |||||||||||||||||
| 334 | 16 | my $sql = join(', ', map { $_->asString } $self->group->list); | |||||||||||||||||
| 335 | 16 | ▶ | return unless $sql; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 336 | 8 | return 'group by ' . $sql; | |||||||||||||||||
| 337 | } | ||||||||||||||||||
| 338 | |||||||||||||||||||
| 339 | =head2 havingSQL | ||||||||||||||||||
| 340 | |||||||||||||||||||
| 341 | =cut | ||||||||||||||||||
| 342 | |||||||||||||||||||
| 343 | 16 | ▶ | sub havingSQL { | ||||||||||||||||
|
Subroutine
havingSQL: called CC=2 SCAR=8.4
Pod
documented
| |||||||||||||||||||
| 344 | 16 | my $self = shift; | |||||||||||||||||
| 345 | 0 | my $sql = join(', ', map { $_->expr } $self->having->list); | |||||||||||||||||
| 346 | 16 | ▶ | return unless $sql; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 347 | 0 | return $sql; | |||||||||||||||||
| 348 | } | ||||||||||||||||||
| 349 | |||||||||||||||||||
| 350 | =head2 whereSQL | ||||||||||||||||||
| 351 | |||||||||||||||||||
| 352 | =cut | ||||||||||||||||||
| 353 | |||||||||||||||||||
| 354 | 18 | ▶ | sub whereSQL { | ||||||||||||||||
|
Subroutine
whereSQL: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 355 | 18 | my $self = shift; | |||||||||||||||||
| 356 | 18 | ▶ | return unless $self->where; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 357 | 12 | return [ "where ", @{ $self->where->inlineSQL } ]; | |||||||||||||||||
| 358 | } | ||||||||||||||||||
| 359 | |||||||||||||||||||
| 360 | =head2 joinSQL | ||||||||||||||||||
| 361 | |||||||||||||||||||
| 362 | =cut | ||||||||||||||||||
| 363 | |||||||||||||||||||
| 364 | 16 | ▶ | sub joinSQL { | ||||||||||||||||
|
Subroutine
joinSQL: called CC=2 SCAR=8.4
Pod
documented
| |||||||||||||||||||
| 365 | 16 | my $self = shift; | |||||||||||||||||
| 366 | 0 | my $join = join(' ', map { $_->asString } $self->join->list); | |||||||||||||||||
| 367 | 16 | ▶ | return unless $join; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 368 | 0 | return $join; | |||||||||||||||||
| 369 | } | ||||||||||||||||||
| 370 | |||||||||||||||||||
| 371 | =head2 keyword_order { qw{type fields from join where having group order offset limit}; | ||||||||||||||||||
| 372 | |||||||||||||||||||
| 373 | =cut | ||||||||||||||||||
| 374 | |||||||||||||||||||
| 375 | 0 | ▶ | sub keyword_order { qw{type fields from join where having group order offset limit}; } | ||||||||||||||||
|
Subroutine
keyword_order: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 376 | |||||||||||||||||||
| 377 | =head2 inlineSQL | ||||||||||||||||||
| 378 | |||||||||||||||||||
| 379 | =cut | ||||||||||||||||||
| 380 | |||||||||||||||||||
| 381 | 16 | ▶ | sub inlineSQL { | ||||||||||||||||
|
Subroutine
inlineSQL: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||
| 382 | 16 | my $self = shift; | |||||||||||||||||
| 383 | 16 | my @sql; | |||||||||||||||||
| 384 | 16 | my @items = $self->keyword_order; | |||||||||||||||||
| 385 | ITEM: | ||||||||||||||||||
| 386 | 16 | ▶ | while(@items) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 387 | 160 | my $m = shift(@items) . 'SQL'; | |||||||||||||||||
| 388 | 160 | my $entry = $self->$m; | |||||||||||||||||
| 389 | 160 | ▶ | next ITEM unless defined $entry; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 390 | 70 | ▶ | push @sql, ' ' if @sql; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 391 | 70 | ▶ | if(ref $entry eq 'ARRAY') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 392 | 10 | push @sql, @$entry; | |||||||||||||||||
| 393 | } else { | ||||||||||||||||||
| 394 | 60 | push @sql, $entry; | |||||||||||||||||
| 395 | } | ||||||||||||||||||
| 396 | } | ||||||||||||||||||
| 397 | 16 | ▶ | push @sql, ' for update' if $self->forUpdate; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 398 | 16 | return $self->normaliseInlineSQL(@sql); | |||||||||||||||||
| 399 | } | ||||||||||||||||||
| 400 | |||||||||||||||||||
| 401 | =head2 results | ||||||||||||||||||
| 402 | |||||||||||||||||||
| 403 | =cut | ||||||||||||||||||
| 404 | |||||||||||||||||||
| 405 | 0 | ▶ | sub results { | ||||||||||||||||
|
Subroutine
results: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||
| 406 | 0 | my $self = shift; | |||||||||||||||||
| 407 | 0 | logDebug("Running [%s]", $self->sqlString); | |||||||||||||||||
| 408 | |||||||||||||||||||
| 409 | 0 | ▶ | $self->db(EntityModel::DB->active_db) unless $self->db; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 410 | 0 | ▶ | die "No DB" unless $self->db; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 411 | |||||||||||||||||||
| 412 | 0 | my ($sql, @bind) = $self->sqlAndParameters; | |||||||||||||||||
| 413 | 0 | my $sth; | |||||||||||||||||
| 414 | 0 | eval { | |||||||||||||||||
| 415 | 0 | $sth = $self->dbh->prepare($sql); | |||||||||||||||||
| 416 | 0 | $sth->execute(@bind); | |||||||||||||||||
| 417 | }; | ||||||||||||||||||
| 418 | 0 | ▶ | if($@) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 419 | 0 | my $msg = $@; | |||||||||||||||||
| 420 | 0 | logError("Query [%s] failed: [%s]", $sql, $msg); | |||||||||||||||||
| 421 | 0 | return EntityModel::Error->new('SQL failed'); | |||||||||||||||||
| 422 | } | ||||||||||||||||||
| 423 | |||||||||||||||||||
| 424 | 0 | ▶ | return EntityModel::Error->new('Invalid handle') unless $sth->{Active}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 425 | |||||||||||||||||||
| 426 | 0 | my @rslt; | |||||||||||||||||
| 427 | 0 | ▶ | while(my $row = $sth->fetchrow_hashref) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 428 | 0 | ▶ | logDebug("Got " . join(',', map { $_ . ' => ' . ($row->{$_} // 'undef') } keys %$row)); | ||||||||||||||||
|
Condition0%0/2
$row->{$_} // "undef"
MC/DC0%0/1
$row->{$_} // "undef"
$row->{$_}
Truth table0%0/2
A
A= $row->{$_}
| |||||||||||||||||||
| 429 | 0 | push @rslt, $row; | |||||||||||||||||
| 430 | } | ||||||||||||||||||
| 431 | 0 | return @rslt; | |||||||||||||||||
| 432 | } | ||||||||||||||||||
| 433 | |||||||||||||||||||
| 434 | =head2 iterate | ||||||||||||||||||
| 435 | |||||||||||||||||||
| 436 | Calls the given method for each result returned from the current query. | ||||||||||||||||||
| 437 | |||||||||||||||||||
| 438 | =cut | ||||||||||||||||||
| 439 | |||||||||||||||||||
| 440 | 0 | ▶ | sub iterate { | ||||||||||||||||
|
Subroutine
iterate: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||
| 441 | 0 | my $self = shift; | |||||||||||||||||
| 442 | 0 | my $code = shift; | |||||||||||||||||
| 443 | 0 | logDebug("Running [%s]", $self->sqlString); | |||||||||||||||||
| 444 | |||||||||||||||||||
| 445 | 0 | my ($sql, @bind) = $self->sqlAndParameters; | |||||||||||||||||
| 446 | |||||||||||||||||||
| 447 | 0 | my $sth; | |||||||||||||||||
| 448 | 0 | eval { | |||||||||||||||||
| 449 | 0 | $sth = $self->dbh->prepare($sql); | |||||||||||||||||
| 450 | 0 | $sth->execute(@bind); | |||||||||||||||||
| 451 | }; | ||||||||||||||||||
| 452 | 0 | ▶ | if($@) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 453 | 0 | my $msg = $@; | |||||||||||||||||
| 454 | 0 | logError("Query [%s] failed: [%s]", $sql, $msg); | |||||||||||||||||
| 455 | 0 | return EntityModel::Error->new('SQL failed'); | |||||||||||||||||
| 456 | } | ||||||||||||||||||
| 457 | 0 | ▶ | return unless $sth->{Active}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 458 | |||||||||||||||||||
| 459 | 0 | ▶ | while(my $row = $sth->fetchrow_hashref) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 460 | 0 | ▶ | logDebug("Got " . join(',', map { $_ . ' => ' . ($row->{$_} // 'undef') } keys %$row)); | ||||||||||||||||
|
Condition0%0/2
$row->{$_} // "undef"
MC/DC0%0/1
$row->{$_} // "undef"
$row->{$_}
Truth table0%0/2
A
A= $row->{$_}
| |||||||||||||||||||
| 461 | 0 | $code->($row); | |||||||||||||||||
| 462 | } | ||||||||||||||||||
| 463 | 0 | return $self; | |||||||||||||||||
| 464 | } | ||||||||||||||||||
| 465 | |||||||||||||||||||
| 466 | =head2 dbh | ||||||||||||||||||
| 467 | |||||||||||||||||||
| 468 | =cut | ||||||||||||||||||
| 469 | |||||||||||||||||||
| 470 | 0 | ▶ | sub dbh { | ||||||||||||||||
|
Subroutine
dbh: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||
| 471 | 0 | my $self = shift; | |||||||||||||||||
| 472 | 0 | ▶ | $self->db(EntityModel::DB->active_db) unless $self->db; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 473 | 0 | return $self->db->dbh; | |||||||||||||||||
| 474 | } | ||||||||||||||||||
| 475 | |||||||||||||||||||
| 476 | 33 | 1; | |||||||||||||||||
| 477 | |||||||||||||||||||
| 478 | __END__ | ||||||||||||||||||