| 1 | package EntityModel::Query::Base; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 45 | $EntityModel::Query::Base::VERSION = '0.102'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | 45 | ▶ | use EntityModel::Class; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 45 | ▶ | no if $] >= 5.017011, warnings => "experimental::smartmatch"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | EntityModel::Query::Base - base class for L<EntityModel::Query>-derived components | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | version 0.102 | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | See L<Entitymodel::Query>. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | See L<Entitymodel::Query>. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 45 | my %ParseHandler; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head2 register | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | Register a parse handler for the given key(s). | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | Called from subclass ->import methods to hook into the configuration parser: | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | EntityModel::Query->new( | ||||||||||||||||||||||||||||
| 35 | x => [ ], | ||||||||||||||||||||||||||||
| 36 | y => [ ] | ||||||||||||||||||||||||||||
| 37 | ) | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | will call the registered parse_x and parse_y methods to handle the two directives, unless those methods are already available on the class. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =cut | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 195 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 195 | my $class = shift; | |||||||||||||||||||||||||||
| 45 | 195 | my %args = @_; | |||||||||||||||||||||||||||
| 46 | 195 | foreach my $k (keys %args) { | |||||||||||||||||||||||||||
| 47 | 195 | ▶ | die "Parse handler for $k already handled by " . $ParseHandler{$k}->{class} if exists $ParseHandler{$k}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | $ParseHandler{$k} = { | ||||||||||||||||||||||||||||
| 49 | class => $class, | ||||||||||||||||||||||||||||
| 50 | 195 | parser => $args{$k} | |||||||||||||||||||||||||||
| 51 | }; | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =head2 can_parse | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | If this class supports the parse_X method, or the given configuration key was registered by | ||||||||||||||||||||||||||||
| 58 | one of the subclasses, returns the appropriate parse handler. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | Returns undef if no handler was available. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 35 | ▶ | sub can_parse { | ||||||||||||||||||||||||||
|
Subroutine
can_parse: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 35 | my $self = shift; | |||||||||||||||||||||||||||
| 66 | 35 | my $k = shift; | |||||||||||||||||||||||||||
| 67 | # TODO should probably drop the 'exists', don't think it's needed | ||||||||||||||||||||||||||||
| 68 | 35 | ▶ | return $self->can("parse_$k") || (exists($ParseHandler{$k}) ? $ParseHandler{$k}->{parser} : undef); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->can("parse_$k") || (exists $ParseHandler{$k} ? $ParseHandler{$k}{'parser'} : undef)
MC/DC0%0/2
$self->can("parse_$k") || (exists $ParseHandler{$k} ? $ParseHandler{$k}{'parser'} : undef)
$self->can("parse_$k")
(exists $ParseHandler{$k} ? $ParseHandler{$k}{'parser'} : undef)
Truth table66%2/3
A || B
A= $self->can("parse_$k")B= (exists $ParseHandler{$k} ? $ParseHandler{$k}{'parser'} : undef)
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head2 inlineSQL | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =cut | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub inlineSQL { | ||||||||||||||||||||||||||
|
Subroutine
inlineSQL: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table0%0/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 79 | 0 | die "Virtual ->inlineSQL method from EntityModel::Query::Base called on $class."; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head2 normaliseInlineSQL | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | Merge adjacent plaintext sections in an inline SQL expression. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | This would for example convert the following: | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 'select', ' ', Entity::Field, ' ', 'from', ' ', Entity::Table | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | into: | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 'select ', Entity::Field, ' from ', Entity::Table | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =cut | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 16 | ▶ | sub normaliseInlineSQL { | ||||||||||||||||||||||||||
|
Subroutine
normaliseInlineSQL: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 16 | my $self = shift; | |||||||||||||||||||||||||||
| 98 | 16 | my @sql = @_; | |||||||||||||||||||||||||||
| 99 | 16 | my @text; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | my @out; | ||||||||||||||||||||||||||||
| 102 | 16 | ▶ | while(@sql) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 166 | my $next = shift(@sql); | |||||||||||||||||||||||||||
| 104 | 166 | ▶ | if(defined($next) && !ref($next)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $next and not ref $next
MC/DC50%1/2
defined $next and not ref $next
defined $next
not ref $next
Truth table66%2/3
A and not(B)
A= defined $nextB= not ref $next
| |||||||||||||||||||||||||||||
| 105 | 154 | push @text, $next; | |||||||||||||||||||||||||||
| 106 | } else { | ||||||||||||||||||||||||||||
| 107 | 12 | ▶ | push @out, join('', @text) if @text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 12 | push @out, $next; | |||||||||||||||||||||||||||
| 109 | 12 | @text = (); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 16 | ▶ | push @out, join('', @text) if @text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 16 | return \@out; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head2 decantValue | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | Extract a value. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =cut | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 13 | ▶ | sub decantValue { | ||||||||||||||||||||||||||
|
Subroutine
decantValue: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 124 | 13 | my $in = shift; | |||||||||||||||||||||||||||
| 125 | 13 | my $out; | |||||||||||||||||||||||||||
| 126 | 13 | my $type = ref $in; | |||||||||||||||||||||||||||
| 127 | 13 | given($type) { | |||||||||||||||||||||||||||
| 128 | 13 | when('SCALAR') { | |||||||||||||||||||||||||||
| 129 | 13 | $out = $$in; | |||||||||||||||||||||||||||
| 130 | 13 | ▶ | if(defined($out) && $out ~~ /^-?\d+(?:\.\d+)?$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $out and $out ~~ qr/^-?\d+(?:\.\d+)?$/
MC/DC50%1/2
defined $out and $out ~~ qr/^-?\d+(?:\.\d+)?$/
defined $out
$out ~~ qr/^-?\d+(?:\.\d+)?$/
Truth table66%2/3
A and B
A= defined $outB= $out ~~ qr/^-?\d+(?:\.\d+)?$/
| |||||||||||||||||||||||||||||
| 131 | 6 | $out = $out+0; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 0 | default { $out = 'unknown'; } | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 13 | return $out; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head2 decantQuotedValue | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Extract a quoted value suitable for use in direct SQL strings. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | The plain-string form of SQL query is only intended for debugging and tracing; regular queries should always use the prepared statement form | ||||||||||||||||||||||||||||
| 144 | provided by L<sqlAndParameters>. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =cut | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 13 | ▶ | sub decantQuotedValue { | ||||||||||||||||||||||||||
|
Subroutine
decantQuotedValue: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 150 | 13 | my $in = shift; | |||||||||||||||||||||||||||
| 151 | 13 | my $out; | |||||||||||||||||||||||||||
| 152 | 13 | my $type = ref $in; | |||||||||||||||||||||||||||
| 153 | 13 | given($type) { | |||||||||||||||||||||||||||
| 154 | 13 | when('SCALAR') { | |||||||||||||||||||||||||||
| 155 | 13 | $out = $$in; | |||||||||||||||||||||||||||
| 156 | 13 | ▶ | if(!defined($out)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 157 | 0 | $out = 'NULL'; | |||||||||||||||||||||||||||
| 158 | } elsif($out =~ /^-?\d+(?:\.\d+)?$/) { | ||||||||||||||||||||||||||||
| 159 | 6 | $out = $out+0; | |||||||||||||||||||||||||||
| 160 | } else { | ||||||||||||||||||||||||||||
| 161 | 7 | $out =~ s/'/''/g; | |||||||||||||||||||||||||||
| 162 | 7 | $out = "'$out'"; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 0 | default { $out = 'unknown'; } | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | 13 | return $out; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head2 sqlString | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =cut | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 11 | ▶ | sub sqlString { | ||||||||||||||||||||||||||
|
Subroutine
sqlString: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 175 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 176 | 11 | my @query = @{ $self->inlineSQL }; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 11 | my $sql = ''; | |||||||||||||||||||||||||||
| 179 | 11 | foreach my $part (@query) { | |||||||||||||||||||||||||||
| 180 | 54 | my $type = ref $part; | |||||||||||||||||||||||||||
| 181 | 54 | ▶ | if($type) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 13 | $sql .= $self->decantQuotedValue($part); | |||||||||||||||||||||||||||
| 183 | } else { | ||||||||||||||||||||||||||||
| 184 | 41 | $sql .= $part; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | 11 | return $sql; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =head2 sqlAndParameters | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =cut | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 11 | ▶ | sub sqlAndParameters { | ||||||||||||||||||||||||||
|
Subroutine
sqlAndParameters: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 195 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 196 | 11 | my @query = @{ $self->inlineSQL }; | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 11 | my $sql = ''; | |||||||||||||||||||||||||||
| 199 | 11 | my @bind; | |||||||||||||||||||||||||||
| 200 | 11 | my $id = 1; | |||||||||||||||||||||||||||
| 201 | 11 | foreach my $part (@query) { | |||||||||||||||||||||||||||
| 202 | 54 | my $type = ref $part; | |||||||||||||||||||||||||||
| 203 | 54 | ▶ | if($type) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | 13 | push @bind, $self->decantValue($part); | |||||||||||||||||||||||||||
| 205 | 13 | $sql .= '$'. $id++; | |||||||||||||||||||||||||||
| 206 | } else { | ||||||||||||||||||||||||||||
| 207 | 41 | $sql .= $part; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | 11 | ▶ | return wantarray ? ($sql, @bind) : $sql; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =head2 asString | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =cut | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 0 | ▶ | sub asString { shift->sqlString } | ||||||||||||||||||||||||||
|
Subroutine
asString: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 45 | 1; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | __END__ | ||||||||||||||||||||||||||||