EntityModel/Query/ParseSQL.pm

filter: Statementstmt 13.7% 20 / 145 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 7.4% 5 / 67 Podpod 0.0% 0 / 15 totaltotal 8.9% 25 / 281 CC 19 SCAR 18.9
CC
19
Cov
11%
where_am_i
30.0
__ANON__
20.0
parse_statement
20.0
CRAP 6.6
« EntityModel/Query/SubQuery.pm ↑ Summary EntityModel/StorageClass/KVStore/Layer/Memcached.pm »
1 package EntityModel::Query::ParseSQL;
2 {
3 1 $EntityModel::Query::ParseSQL::VERSION = '0.102';
4 }
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use parent qw(Parser::MGC);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use constant KEYWORDS => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
12 select
13 insert
14 update
15 truncate
16 delete
17 from
18 where
19 group
20 by
21 limit
22 not
23 in
24 like
25 order
26 having
27 create
28 table
29 drop
30 1 );
31  
32 =head1 NAME
33  
34 EntityModel::Query::ParseSQL
35  
36 =head1 VERSION
37  
38 version 0.102
39  
40 =head1 SYNOPSIS
41  
42 use strict; use warnings;
43 use EntityModel::Query::ParseSQL;
44 my $parser = EntityModel::Query::ParseSQL->new;
45 print Dumper($parser->from_string('select id from tbl x inner join tbl y on x.idx = y.idx where x.idx < 14'));
46  
47 =head1 METHODS
48  
49 =cut
50  
51 0 ▶sub where_am_i { return unless DEBUG; my $self = shift;
Subroutine where_am_i: not called CC=5 SCAR=34.0
Pod undocumented
52 0 ▶ my $note = shift || (caller(1))[3];
Condition0%0/3
shift() || (caller 1)[3]
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || (caller 1)[3]
shift() (caller 1)[3]
Truth table0%0/3
A || B
A= shift()B= (caller 1)[3]
ABresult
1X1
011
000
53 0 my ( $lineno, $col, $text ) = $self->where;
54 0 my $len = length($text);
55 0 my $target_pos = $col;
56 0 ▶ $target_pos++ while $target_pos < length($text) && substr($text, $target_pos, 1) =~ /^\s/;
Branch: 1 branch
BranchTrueFalseTotal
$target_pos < length $text and substr($text, $target_pos, 1) =~ /^\s/ and do { ++$target_pos } 0 0 0
Condition0%0/3
$target_pos < length $text and substr($text, $target_pos, 1) =~ /^\s/
!ll&&!rl&&r
000
MC/DC0%0/2
$target_pos < length $text and substr($text, $target_pos, 1) =~ /^\s/
$target_pos < length $text substr($text, $target_pos, 1) =~ /^\s/
Truth table0%0/3
A and B
A= $target_pos < length $textB= substr($text, $target_pos, 1) =~ /^\s/
ABresult
0X0
100
111
57 0 $target_pos++;
58 0 ▶ substr $text, ($target_pos >= length($text) ? length($text) : $target_pos), 0, "\033[01;00m";
Branch: 1 branch
BranchTrueFalseTotal
$target_pos >= length $text ? : 0 0 0
59 0 substr $text, $col, 0, "\033[01;32m";
60 0 printf("%-80.80s %d,%d %d %s\n", $text, $col, $lineno, $len, $note);
61 }
62  
63 0 ▶sub parse {
Subroutine parse: not called CC=1 SCAR=6.9
Pod undocumented
64 0 my $self = shift;
65  
66 0 ▶ $self->list_of(';', sub { $self->parse_statement });
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 }
68  
69 0 ▶sub parse_select {
Subroutine parse_select: not called CC=1 SCAR=6.9
Pod undocumented
70 0 my $self = shift;
71 0 $self->where_am_i;
72 0 $self->token_kw('select');
73 }
74  
75 0 ▶sub token_lvalue {
Subroutine token_lvalue: not called CC=1 SCAR=6.9
Pod undocumented
76 0 my $self = shift;
77 0 $self->where_am_i;
78 $self->any_of(
79 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
80 0 ▶ join '', @{ $self->maybe(sub { [$self->token_ident, $self->expect('.')] }) || [] }, $self->token_ident;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/2
$self->maybe(sub { [$self->token_ident, $self->expect('.')]; } ) || []
l!l
00
MC/DC0%0/1
$self->maybe(sub { [$self->token_ident, $self->expect('.')]; } ) || []
$self->maybe(sub { [$self->token_ident, $self->expect('.')]; } )
Truth table0%0/2
A
A= $self->maybe(sub { [$self->token_ident, $self->expect('.')]; } )
Aresult
11
00
81 },
82 0 ▶ sub { $self->token_int },
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 0 ▶ sub { $self->token_float },
Subroutine __ANON__: not called CC=1 SCAR=6.9
84 0 ▶ sub { $self->token_string },
Subroutine __ANON__: not called CC=1 SCAR=6.9
85 0 );
86 }
87  
88 0 ▶sub token_rvalue {
Subroutine token_rvalue: not called CC=1 SCAR=6.9
Pod undocumented
89 0 my $self = shift;
90 0 $self->where_am_i;
91 $self->any_of(
92 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
93 0 ▶ join '', @{ $self->maybe(sub { [$self->token_ident, $self->expect('.')] }) || [] }, $self->token_ident;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/2
$self->maybe(sub { [$self->token_ident, $self->expect('.')]; } ) || []
l!l
00
MC/DC0%0/1
$self->maybe(sub { [$self->token_ident, $self->expect('.')]; } ) || []
$self->maybe(sub { [$self->token_ident, $self->expect('.')]; } )
Truth table0%0/2
A
A= $self->maybe(sub { [$self->token_ident, $self->expect('.')]; } )
Aresult
11
00
94 },
95 0 ▶ sub { $self->token_int },
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 0 ▶ sub { $self->token_float },
Subroutine __ANON__: not called CC=1 SCAR=6.9
97 0 ▶ sub { $self->token_string },
Subroutine __ANON__: not called CC=1 SCAR=6.9
98 0 );
99 }
100  
101  
102 0 ▶sub token_operator {
Subroutine token_operator: not called CC=1 SCAR=6.9
Pod undocumented
103 0 my $self = shift;
104 0 $self->where_am_i;
105 $self->any_of(
106 0 ▶ sub { $self->expect('=') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
107 0 ▶ sub { $self->expect('!=') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
108 0 ▶ sub { $self->expect('<=') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 0 ▶ sub { $self->expect('>=') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
110 0 ▶ sub { $self->expect('<') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
111 0 ▶ sub { $self->expect('>') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
112 0 ▶ sub { $self->expect('<>') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
113 0 ▶ sub { $self->expect('is') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
114 0 ▶ sub { $self->expect('in') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 0 ▶ sub { $self->expect('like') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
116 0 );
117 }
118  
119 1 my $ANY_KEYWORD_RE = qr(@{[ join '|', KEYWORDS ]});
120  
121 0 ▶sub check_keyword {
Subroutine check_keyword: not called CC=2 SCAR=17.9
Pod undocumented
122 0 my $self = shift;
123 0 $self->where_am_i;
124 0 $self->skip_ws;
125 0 ▶ if($self->{str} =~ m/\G($ANY_KEYWORD_RE)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"str"} =~ /\G($ANY_KEYWORD_RE)/cg) 0 0 0
126 0 $self->fail( "Had keyword $1" );
127 0 return 1;
128 }
129 0 return 0;
130 }
131  
132 0 ▶sub token_alias {
Subroutine token_alias: not called CC=2 SCAR=17.9
Pod undocumented
133 0 my $self = shift;
134 0 $self->where_am_i;
135 0 ▶ return if $self->check_keyword;
Branch: 1 branch
BranchTrueFalseTotal
if $self->check_keyword 0 0 0
136 0 $self->token_ident;
137 }
138  
139 0 ▶sub parse_join {
Subroutine parse_join: not called CC=1 SCAR=6.9
Pod undocumented
140 0 my $self = shift;
141 0 $self->where_am_i;
142 0 ▶ $self->sequence_of(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
143 0 $self->skip_ws;
144 0 $self->where_am_i('join seq');
145 [
146 0 ▶ @{ $self->maybe(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 0 $self->where_am_i('find join kw');
148 $self->any_of(
149 0 ▶ sub { [ $self->expect('full'), $self->expect('outer') ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
150 0 ▶ sub { [ $self->token_kw(qw(inner left right full cross hash)) ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
151 0 ▶ sub { [ $self->expect('left'), $self->expect('outer') ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
152 0 ▶ sub { [ $self->expect('right'), $self->expect('outer') ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
153 )
154 0 ▶ }) || [] },
Condition0%0/2
$self->maybe(sub { $self->where_am_i('find join kw'); $self->any_of(sub { [$self->expect('full'), $self->expect('outer')]; } , sub { [$self->token_kw('inner', 'left', 'right', 'full', 'cross', 'hash')]; } , sub { [$self->expect('left'), $self->expect('outer')]; } , sub { [$self->expect('right'), $self->expect('outer')]; } ); } ) || []
l!l
00
MC/DC0%0/1
$self->maybe(sub { $self->where_am_i('find join kw'); $self->any_of(sub { [$self->expect('full'), $self->expect('outer')]; } , sub { [$self->token_kw('inner', 'left', 'right', 'full', 'cross', 'hash')]; } , sub { [$self->expect('left'), $self->expect('outer')]; } , sub { [$self->expect('right'), $self->expect('outer')]; } ); } ) || []
$self->maybe(sub { $self->where_am_i('find join kw'); $self->any_of(sub { [$self->expect('full'), $self->expect('outer')]; } , sub { [$self->token_kw('inner', 'left', 'right', 'full', 'cross', 'hash')]; } , sub { [$self->expect('left'), $self->expect('outer')]; } , sub { [$self->expect('right'), $self->expect('outer')]; } ); } )
Truth table0%0/2
A
A= $self->maybe(sub { $self->where_am_i('find join kw'); $self->any_of(sub { [$self->expect('full'), $self->expect('outer')]; } , sub { [$self->token_kw('inner', 'left', 'right', 'full', 'cross', 'hash')]; } , sub { [$self->expect('left'), $self->expect('outer')]; } , sub { [$self->expect('right'), $self->expect('outer')]; } ); } )
Aresult
11
00
155 $self->expect('join'),
156 0 ▶ @{ $self->parse_table_or_query || [] },
Condition0%0/2
$self->parse_table_or_query || []
l!l
00
MC/DC0%0/1
$self->parse_table_or_query || []
$self->parse_table_or_query
Truth table0%0/2
A
A= $self->parse_table_or_query
Aresult
11
00
157 0 ▶ @{ $self->maybe(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 [ $self->expect('on'),
159 $self->any_of(
160 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
161 0 $self->where_am_i;
162 [
163 0 $self->token_lvalue,
164 $self->token_operator,
165 $self->token_rvalue
166 ]
167 },
168 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
169 0 $self->where_am_i;
170 [
171 0 $self->token_rvalue
172 ]
173 }
174 0 ) ]
175 0 ▶ }) || [] } ]
Condition0%0/2
$self->maybe(sub { [$self->expect('on'), $self->any_of(sub { $self->where_am_i; [$self->token_lvalue, $self->token_operator, $self->token_rvalue]; } , sub { $self->where_am_i; [$self->token_rvalue]; } )]; } ) || []
l!l
00
MC/DC0%0/1
$self->maybe(sub { [$self->expect('on'), $self->any_of(sub { $self->where_am_i; [$self->token_lvalue, $self->token_operator, $self->token_rvalue]; } , sub { $self->where_am_i; [$self->token_rvalue]; } )]; } ) || []
$self->maybe(sub { [$self->expect('on'), $self->any_of(sub { $self->where_am_i; [$self->token_lvalue, $self->token_operator, $self->token_rvalue]; } , sub { $self->where_am_i; [$self->token_rvalue]; } )]; } )
Truth table0%0/2
A
A= $self->maybe(sub { [$self->expect('on'), $self->any_of(sub { $self->where_am_i; [$self->token_lvalue, $self->token_operator, $self->token_rvalue]; } , sub { $self->where_am_i; [$self->token_rvalue]; } )]; } )
Aresult
11
00
176 0 });
177 }
178  
179 0 ▶sub parse_statement {
Subroutine parse_statement: not called CC=4 SCAR=30.0
Pod undocumented
180 0 my $self = shift;
181 0 $self->where_am_i;
182  
183 return [
184 # Query type
185 $self->any_of(
186 0 ▶ sub { $self->parse_select }
Subroutine __ANON__: not called CC=1 SCAR=6.9
187 ),
188 $self->parse_fields,
189 0 ▶ @{ $self->maybe(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
190 0 $self->parse_from;
191 0 ▶ }) || []},
Condition0%0/2
$self->maybe(sub { $self->parse_from; } ) || []
l!l
00
MC/DC0%0/1
$self->maybe(sub { $self->parse_from; } ) || []
$self->maybe(sub { $self->parse_from; } )
Truth table0%0/2
A
A= $self->maybe(sub { $self->parse_from; } )
Aresult
11
00
192 0 ▶ @{ $self->maybe(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
193 0 $self->parse_join;
194 0 ▶ }) || []},
Condition0%0/2
$self->maybe(sub { $self->parse_join; } ) || []
l!l
00
MC/DC0%0/1
$self->maybe(sub { $self->parse_join; } ) || []
$self->maybe(sub { $self->parse_join; } )
Truth table0%0/2
A
A= $self->maybe(sub { $self->parse_join; } )
Aresult
11
00
195 0 ▶ @{ $self->maybe(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
196 0 $self->parse_where;
197 0 ▶ }) || []},
Condition0%0/2
$self->maybe(sub { $self->parse_where; } ) || []
l!l
00
MC/DC0%0/1
$self->maybe(sub { $self->parse_where; } ) || []
$self->maybe(sub { $self->parse_where; } )
Truth table0%0/2
A
A= $self->maybe(sub { $self->parse_where; } )
Aresult
11
00
198 ];
199 }
200  
201 0 ▶sub token_keyword {
Subroutine token_keyword: not called CC=1 SCAR=6.9
Pod undocumented
202 0 my $self = shift;
203 0 ▶ $self->any_of(map { my $k = $_; sub { $self->expect($k) } } KEYWORDS);
Subroutine __ANON__: not called CC=1 SCAR=6.9
204 }
205  
206 0 ▶sub parse_from {
Subroutine parse_from: not called CC=2 SCAR=17.9
Pod undocumented
207 0 my $self = shift;
208 0 $self->where_am_i;
209 return [
210 $self->expect('from'),
211 0 ▶ @{ $self->parse_table_or_query || [] },
Condition0%0/2
$self->parse_table_or_query || []
l!l
00
MC/DC0%0/1
$self->parse_table_or_query || []
$self->parse_table_or_query
Truth table0%0/2
A
A= $self->parse_table_or_query
Aresult
11
00
212 ];
213 }
214  
215 0 ▶sub parse_table_or_query {
Subroutine parse_table_or_query: not called CC=4 SCAR=30.0
Pod undocumented
216 0 my $self = shift;
217 [
218 $self->any_of(
219 0 ▶ @{ $self->maybe(sub { [$self->token_ident, $self->expect('.')] }) || [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/2
$self->maybe(sub { [$self->token_ident, $self->expect('.')]; } ) || []
l!l
00
MC/DC0%0/1
$self->maybe(sub { [$self->token_ident, $self->expect('.')]; } ) || []
$self->maybe(sub { [$self->token_ident, $self->expect('.')]; } )
Truth table0%0/2
A
A= $self->maybe(sub { [$self->token_ident, $self->expect('.')]; } )
Aresult
11
00
220 0 ▶ sub { $self->token_ident },
Subroutine __ANON__: not called CC=1 SCAR=6.9
221 ),
222 0 ▶ @{ $self->maybe(sub { [$self->expect('as')] }) || [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/2
$self->maybe(sub { [$self->expect('as')]; } ) || []
l!l
00
MC/DC0%0/1
$self->maybe(sub { [$self->expect('as')]; } ) || []
$self->maybe(sub { [$self->expect('as')]; } )
Truth table0%0/2
A
A= $self->maybe(sub { [$self->expect('as')]; } )
Aresult
11
00
223 0 ▶ @{ $self->maybe(sub { [$self->token_alias ] }) || [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/2
$self->maybe(sub { [$self->token_alias]; } ) || []
l!l
00
MC/DC0%0/1
$self->maybe(sub { [$self->token_alias]; } ) || []
$self->maybe(sub { [$self->token_alias]; } )
Truth table0%0/2
A
A= $self->maybe(sub { [$self->token_alias]; } )
Aresult
11
00
224 ];
225 }
226  
227 0 ▶sub parse_where {
Subroutine parse_where: not called CC=1 SCAR=6.9
Pod undocumented
228 0 my $self = shift;
229 0 $self->where_am_i;
230 [$self->expect('where'), $self->sequence_of(
231 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
232 $self->any_of(
233 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
234 [
235 0 $self->token_lvalue,
236 $self->token_operator,
237 $self->token_rvalue
238 ]
239 },
240 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
241 [
242 0 $self->token_rvalue
243 ]
244 }
245 0 );
246 }
247 0 )];
248 }
249  
250 0 ▶sub parse_fields {
Subroutine parse_fields: not called CC=1 SCAR=6.9
Pod undocumented
251 0 my $self = shift;
252 0 $self->where_am_i;
253  
254 # Fields
255 0 ▶ $self->list_of(',', sub { $self->any_of(
Subroutine __ANON__: not called CC=1 SCAR=6.9
256 0 ▶ sub { $self->where_am_i('int field'); $self->token_int },
Subroutine __ANON__: not called CC=1 SCAR=6.9
257 0 ▶ sub { $self->where_am_i('ident field'); $self->token_ident },
Subroutine __ANON__: not called CC=1 SCAR=6.9
258 0 ▶ sub { $self->where_am_i('string field'); $self->token_string },
Subroutine __ANON__: not called CC=1 SCAR=6.9
259 0 ▶ sub { $self->where_am_i('nested fields'); $self->scope_of( "(", \&parse, ")" ) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
260 0 )});
261 }
262  
263 1 1;
264  
265 __END__
« EntityModel/Query/SubQuery.pm ↑ Summary EntityModel/StorageClass/KVStore/Layer/Memcached.pm »