blib/lib/SQL/Steno.pm

filter: Statementstmt 76.4% 380 / 497 Branchbran 67.4% 263 / 390 Conditioncond 58.2% 99 / 170 MC/DCmcdc 30.9% 26 / 84 Subroutinesub 88.8% 24 / 27 Podpod 15.7% 3 / 19 totaltotal 66.9% 795 / 1187 SCAR 77.0
CC
271
Cov
70%
helphash
117.2
help
93.3
shell
71.6
CRAP 2214.8
↑ Summary
1 =encoding utf8
2  
3 =head1 NAME
4  
5 SQL::Steno - Short hand for SQL and compact output
6  
7 =head1 SYNOPSIS
8  
9 Type some short-hand, see the corresponding SQL and its output:
10  
11 steno> TABLE1;somecolumn > 2 -- ; after tables means where
12 select * from TABLE1 where somecolumn > 2;
13 prepare: 0.000s execute: 0.073s rows: 14
14 id|column1 |column2
15 | | |column3
16 | | | |somecolumn
17 ----|-------------------------------------------|----|-|-|
18 27|foo | |a|7|
19 49|bar |abcd|a|3|
20 81|baz\nbazinga\nbazurka |jk |b|9|
21 1984|bla bla bla bla bla bla bla bla bla bla bla|xyz |c|5|
22 ...
23 steno> /abc|foo/#TBL1;.socol > 2 -- /regexp/ grep, #tableabbrev, .columnabbrev
24 select * from TABLE1 where somecolumn > 2;
25 prepare: 0.000s execute: 0.039s rows: 14
26 id|column1
27 | |column2
28 | | |[column3=a]
29 | | |somecolumn
30 --|---|----|-|
31 27|foo| |7|
32 49|bar|abcd|3|
33 steno> .c1,.c2,.some;#TE1#:ob2d3 -- ; before tables means from, 2nd # alias, :macro
34 select column1,column2,somecolumn from TABLE1 TE1 order by 2 desc, 3;
35 ...
36 steno> n(), yr(), cw(,1,2,3) -- functionabbrev before (, can have initial default arg
37 select count(*), year(now()), concat_ws(',',1,2,3);
38 ...
39 steno> .col1,.clm2,.sn;#TBL1:jTBL2 u(id);mydate :b :m+3d and :d-w -- :jTABLEABBREV and :+/- family
40 select column1,column2,somecolumn from TABLE1 join TABLE2 using(id) where mydate
41 between date_format(now(),"%Y-%m-01")+interval 3 day and curdate()-interval 1 week;
42 ...
43  
44 =head1 DESCRIPTION
45  
46 You're the command-line type, but are tired of typing C<select * from TABLE
47 where CONDITION>, always forgetting the final C<;>? Output always seems far
48 too wide and at least mysql cli messes up the format when it includes
49 newlines?
50  
51 This module consists of the function C<convert> which implements a
52 configurable ultra-compact language that maps to SQL. Then there is C<run>
53 which performs normal SQL queries but has various tricks for narrowing the
54 output. It can also grep on whole rows, rather than having to list all fields
55 that you expect to match. They get combined by the function C<shell> which
56 converts and runs in an endless loop.
57  
58 This is work in progress, only recently isolated from a monolithic script.
59 Language elements and API may change as the need arises, e.g. C<:macro> used
60 to be C<@macro>, till the day I wanted to use an SQL-variable and noticed the
61 collision. In this early stage, you are more than welcome to propose
62 ammendments, especially if they make the language more powerful and/or more
63 consistent. Defaults are for MariaDB/MySQL, though the mechanism also works
64 with other DBs.
65  
66 =cut
67  
68 4 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 package SQL::Steno v0.3.0;
71  
72 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 4 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 4 binmode STDIN, ':utf8';
77 4 binmode STDOUT, ':utf8';
78  
79 4 our $dbh;
80 4 our $perl_re = qr/(\{(?:(?>[^{}]+)|(?-1))*\})/;
81 4 our( %Table_Columns, $table_re );
82 sub init {
83 0 ▶ die "\$dbh is undef\n" unless $dbh;
Subroutine init: not called CC=5 SCAR=34.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $SQL::Steno::dbh 0 0 0
84 0 local @{$dbh}{qw(PrintWarn PrintError RaiseError)} = (0, 0, 0); # \todo is this right? views can barf because more restrictive.
85 0 for my $table ( @{$dbh->table_info->fetchall_arrayref} ) {
86 0 $Table_Columns{uc $table->[2]} = [];
87 0 splice @$table, 3, -1, '%';
88 0 ▶ my $info = $dbh->column_info( @$table ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $info = $SQL::Steno::dbh->column_info(@$table) or next 0 0 0
89 0 for my $column ( @{$info->fetchall_arrayref} ) {
90 0 push @{$Table_Columns{$table->[2]}}, uc $column->[3];
91 }
92 }
93 0 undef $table_re; # (re)create below
94 }
95 4 our $init_from_query = <<\SQL;
96 select ucase(TABLE_NAME), ucase(COLUMN_NAME)
97 from information_schema.COLUMNS
98 where TABLE_SCHEMA = schema()
99 SQL
100 sub init_from_query {
101 0 ▶ die "\$dbh is undef\n" unless $dbh;
Subroutine init_from_query: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $SQL::Steno::dbh 0 0 0
102 0 local @{$dbh}{qw(PrintWarn PrintError RaiseError)} = (0, 0, 0); # \todo is this right?
103 0 my $sth = $dbh->prepare( $init_from_query );
104 0 $sth->execute;
105 0 $sth->bind_columns( \my( $table, $column ));
106 0 ▶ push @{$Table_Columns{$table}}, $column while $sth->fetch;
Branch: 1 branch
BranchTrueFalseTotal
$sth->fetch and do { push @{$SQL::Steno::Table_Columns{$table};}, $column } 0 0 0
107 0 undef $table_re; # (re)create below
108 }
109  
110  
111  
112 4 my %render =
113 (csv => \&render_csv,
114 table => \&render_table,
115 yaml => \&render_yaml,
116 yml => \&render_yaml);
117 4 my( $render, %opt );
118  
119 sub set_render($@) {
120 4 ▶ ($render, %opt) = ();
Subroutine set_render: called CC=3 SCAR=11.0
Pod undocumented
121 4 for( @_ ) {
122 7 ▶ if( defined $render ) { # all further args are opts
Branch: 1 branch
BranchTrueFalseTotal
if (defined $render) { } 3 4 7
123 3 tr/ \t//d;
124 3 undef $opt{$_}; # make it exist
125 } else {
126 4 $render = substr $_, 1;
127 }
128 }
129 4 $render = $render{$render};
130 4 ''; # For use as a query
131 }
132 4 our( %Queries_help, %Queries );
133  
134 4 for my $fmt ( keys %render ) {
135 16 $Queries_help{".$fmt"} = " output '&.$fmt() this' or next query as \U$fmt";
136 16 $Queries{".$fmt"} = \&set_render;
137 }
138 sub Query {
139 41 ▶ $Queries_help{$_[0]} = $_[1];
Subroutine Query: called CC=1 SCAR=0.0
Pod undocumented
140 41 $Queries{$_[0]} = $_[2];
141 41 undef;
142 }
143 4 Query @$_ for
144 ['-' => " output next query as YAML",
145 '&.yaml'],
146  
147  
148 [ps => ' show processlist (without Sleep)',
149 '{(${_}[7] // "") ne "Sleep"}=show processlist'],
150  
151 [psf => ' show full processlist (without Sleep)',
152 '{(${_}[7] // "") ne "Sleep"}=show full processlist'],
153  
154  
155 [t => 'unquotedtablename[,unquotedcolumnpattern] show fields',
156 'COLUMN_NAME,COLUMN_TYPE,IS_NULLABLE NUL,COLUMN_KEY `KEY`,COLUMN_COMMENT;information_schema.`COLUMNS`;TABLE_SCHEMA=schema() and TABLE_NAME=$\1$? and COLUMN_NAME like$\*??:ob ORDINAL_POSITION'],
157  
158  
159 [s => 'var;value set @var = value',
160 'set @$1=$2'],
161  
162 [ss => "var;value set \@var = 'value'",
163 'set @$1=$\2'],
164  
165 [sd => 'var;value set @var = cast("value" as date)',
166 'set @$1=cast($\2 as date)'],
167  
168 [sdt => 'var;value set @var = cast("value" as datetime)',
169 'set @$1=cast($\2 as datetime)'],
170  
171 [st => 'var;value set @var = cast("value" as time)',
172 'set @$1=cast($\2 as time)'],
173  
174 [sy => ' set @a, @z yesterday is between @a and @z (see :baz)',
175 #'set @a=date(now())-interval 1 day, @z=date(now())-interval 1 second',
176 'select @a:=date(now()-interval 1 day)`@a`, @z:=date(now())-interval 1 second`@z`'];
177  
178  
179 4 our $weekstart = 1; # Monday is the first day of the week as per ISO 8601.
180 4 my $timespec_re = qr/[yqmwdhMs]?/;
181 4 our %Join_clause;
182 our %Macros =
183 (
184 b => ' between',
185 baz => ' between @a and @z',
186 d => ' distinct',
187 h => ' having',
188 j => ' join',
189 l => ' like',
190 lj => ' left join',
191 n => ' is null',
192 nb => ' not between',
193 nc => ' sql_no_cache',
194 nl => ' not like',
195 nn => ' is not null',
196 nr => ' not rlike',
197 od => ' on duplicate key update',
198 odku => ' on duplicate key update',
199 r => ' rlike',
200 u => ' union select',
201 ua => ' union all select',
202 wr => ' with rollup',
203 '' => sub {
204 2 ▶ my $join = 'for all #TBL matching TABLE';
Subroutine __ANON__: called CC=17 SCAR=40.1
205 2 my $int = 'see :+ :- :y-m :q+0 :d+2h';
206 2 my $gob = 'for 0 or more digits, optionally followed by a or d';
207 2 ▶ return ([jTBL => $join], ['jTBL#' => $join], [ljTBL => $join], [1 => 'for all numbers'],
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 2 2
208 [gb147 => $gob], [ob2d5a9 => $gob],
209 ['+' => <<INT], ['-' => $int], ['d+2h' => $int], ['y-m' => $int], ['q+0' => $int])
210 :B+/-NO this B(ase) +/- N(umber, 0 for none, default 1 if O given) O(ffset)
211 optional B, O is y(ear), q(uarter), m(onth), w(eek), d(ay), h(our), M(inute), s(econd)
212 INT
213 unless @_; # help
214 2 for( $_[0] ) {
215 2 ▶ return " limit $_" if /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\d+$/u 0 2 2
216 2 ▶ if( s/^([og])b(?=(?:\d[ad]?)*$)/ $1 eq 'g' ? ' group by ' : ' order by ' /e ) {
Branch: 2 branches
BranchTrueFalseTotal
if (s/^([og])b(?=(?:\d[ad]?)*$)/$1 eq "g" ? " group by " : " order by ";/eu) 1 1 2
$1 eq 'g' ? : 0 1 1
217 1 s/(?<! )(?=\d)/, /g;
218 1 s/a/ asc/g; s/(?<!r)d/ desc/g;
219 1 return $_;
220 }
221 1 ▶ if( s/^(l?)j/#/ ) { # (l)jtbl: j or lj with any #tbl
Branch: 1 branch
BranchTrueFalseTotal
if (s/^(l?)j/#/u) 0 1 1
222 0 ▶ my $left = $1 ? ' left' : '';
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 0 0 0
223 0 &convert_table_column;
224 0 /^(\w+)/;
225 0 ▶ return "$left join $_" . ($Join_clause{$1} || $Join_clause{''} || '');
Condition0%0/5
$SQL::Steno::Join_clause{$1} || $SQL::Steno::Join_clause{''} || ''
l!l
00
$SQL::Steno::Join_clause{$1} or $SQL::Steno::Join_clause{""}
l!l&&r!l&&!r
000
MC/DC0%0/2
$SQL::Steno::Join_clause{$1} || $SQL::Steno::Join_clause{''} || ''
$SQL::Steno::Join_clause{$1} $SQL::Steno::Join_clause{""}
Truth table0%0/3
A or B
A= $SQL::Steno::Join_clause{$1}B= $SQL::Steno::Join_clause{""}
ABresult
1X1
011
000
226 }
227 1 ▶ return $_ if
Branch: 1 branch
BranchTrueFalseTotal
if s/^($timespec_re)([+-])(\d*)($timespec_re)$/({"y", " date_format(now(),\"%Y-01-01\")", "q", " date_format(now()-interval mod(month(now())+11,3) month,\"%Y-%m-01\")", "m", " date_format(now(),\"%Y-%m-01\")", "w", ' curdate()-interval weekday(now())' . ($SQL::Steno::weekstart ? " day" : "+1 day"), "d", " curdate()", "h", " date_format(now(),\"%F %H:00\")", "M", " date_format(now(),\"%F %H:%M\")", "s", " now()"}->{$1} || "") . ($3 ne 0 && " $2interval" . ($3 ? " $3" : ($4 ? " 1" : "")) . ({"y", " year", "q", " quarter", "m", " month", "w", " week", "d", " day", "h", " hour", "M", " minute", "s", " second"}->{$4} || ""));/eou 1 0 1
228 s(^($timespec_re)([+-])(\d*)($timespec_re)$) {
229 ({ y => ' date_format(now(),"%Y-01-01")',
230 q => ' date_format(now()-interval mod(month(now())+11,3) month,"%Y-%m-01")',
231 m => ' date_format(now(),"%Y-%m-01")',
232 w => ' curdate()-interval weekday(now())' . ($weekstart ? ' day' : '+1 day'),
233 d => ' curdate()',
234 h => ' date_format(now(),"%F %H:00")',
235 M => ' date_format(now(),"%F %H:%M")',
236 s => ' now()' }->{$1} || '') .
237 ($3 ne '0' &&
238 " $2interval" .
239 ($3 ? " $3" : $4 ? ' 1' : '') .
240 ({ y => ' year',
241 q => ' quarter',
242 m => ' month',
243 w => ' week',
244 d => ' day',
245 h => ' hour',
246 M => ' minute',
247 1 ▶ s => ' second' }->{$4} || ''))
Branch: 3 branches
BranchTrueFalseTotal
$SQL::Steno::weekstart ? : 1 0 1
$3 ? : 0 0 0
$4 ? : 0 0 0
Condition28%2/7
{'y', ' date_format(now(),"%Y-01-01")', 'q', ' date_format(now()-interval mod(month(now())+11,3) month,"%Y-%m-01")', 'm', ' date_format(now(),"%Y-%m-01")', 'w', ' curdate()-interval weekday(now())' . ($SQL::Steno::weekstart ? ' day' : '+1 day'), 'd', ' curdate()', 'h', ' date_format(now(),"%F %H:00")', 'M', ' date_format(now(),"%F %H:%M")', 's', ' now()'}->{$1} || ''
l!l
10
$3 ne '0' && " $2interval" . ($3 ? " $3" : ($4 ? ' 1' : '')) . ({'y', ' year', 'q', ' quarter', 'm', ' month', 'w', ' week', 'd', ' day', 'h', ' hour', 'M', ' minute', 's', ' second'}->{$4} || '')
!ll&&!rl&&r
100
{'y', ' year', 'q', ' quarter', 'm', ' month', 'w', ' week', 'd', ' day', 'h', ' hour', 'M', ' minute', 's', ' second'}->{$4} || ''
l!l
00
MC/DC0%0/1
{'y', ' date_format(now(),"%Y-01-01")', 'q', ' date_format(now()-interval mod(month(now())+11,3) month,"%Y-%m-01")', 'm', ' date_format(now(),"%Y-%m-01")', 'w', ' curdate()-interval weekday(now())' . ($SQL::Steno::weekstart ? ' day' : '+1 day'), 'd', ' curdate()', 'h', ' date_format(now(),"%F %H:00")', 'M', ' date_format(now(),"%F %H:%M")', 's', ' now()'}->{$1} || ''
{'y', ' date_format(now(),"%Y-01-01")', 'q', ' date_format(now()-interval mod(month(now())+11,3) month,"%Y-%m-01")', 'm', ' date_format(now(),"%Y-%m-01")', 'w', ' curdate()-interval weekday(now())' . ($SQL::Steno::weekstart ? ' day' : '+1 day'), 'd', ' curdate()', 'h', ' date_format(now(),"%F %H:00")', 'M', ' date_format(now(),"%F %H:%M")', 's', ' now()'}->{$1}
MC/DC0%0/2
$3 ne '0' && " $2interval" . ($3 ? " $3" : ($4 ? ' 1' : '')) . ({'y', ' year', 'q', ' quarter', 'm', ' month', 'w', ' week', 'd', ' day', 'h', ' hour', 'M', ' minute', 's', ' second'}->{$4} || '')
$3 ne '0' " $2interval" . ($3 ? " $3" : ($4 ? ' 1' : '')) . ({'y', ' year', 'q', ' quarter', 'm', ' month', 'w', ' week', 'd', ' day', 'h', ' hour', 'M', ' minute', 's', ' second'}->{$4} || '')
MC/DC0%0/1
{'y', ' year', 'q', ' quarter', 'm', ' month', 'w', ' week', 'd', ' day', 'h', ' hour', 'M', ' minute', 's', ' second'}->{$4} || ''
{'y', ' year', 'q', ' quarter', 'm', ' month', 'w', ' week', 'd', ' day', 'h', ' hour', 'M', ' minute', 's', ' second'}->{$4}
Truth table50%1/2
A
A= {'y', ' date_format(now(),"%Y-01-01")', 'q', ' date_format(now()-interval mod(month(now())+11,3) month,"%Y-%m-01")', 'm', ' date_format(now(),"%Y-%m-01")', 'w', ' curdate()-interval weekday(now())' . ($SQL::Steno::weekstart ? ' day' : '+1 day'), 'd', ' curdate()', 'h', ' date_format(now(),"%F %H:00")', 'M', ' date_format(now(),"%F %H:%M")', 's', ' now()'}->{$1}
Aresult
11
00
Truth table33%1/3
A && B
A= $3 ne '0'B= " $2interval" . ($3 ? " $3" : ($4 ? ' 1' : '')) . ({'y', ' year', 'q', ' quarter', 'm', ' month', 'w', ' week', 'd', ' day', 'h', ' hour', 'M', ' minute', 's', ' second'}->{$4} || '')
ABresult
0X0
100
111
Truth table0%0/2
A
A= {'y', ' year', 'q', ' quarter', 'm', ' month', 'w', ' week', 'd', ' day', 'h', ' hour', 'M', ' minute', 's', ' second'}->{$4}
Aresult
11
00
248 }eo;
249 }
250 4 });
251  
252 # \todo default arg n() -> n(*) time*(now())
253 4 our %Functions =
254 (
255 c => 'concat',
256 cw => 'concat_ws',
257 coa => 'coalesce',
258 gc => 'group_concat',
259 i => 'in', # not really fn, but ( follows
260 in => 'ifnull',
261 l => 'char_length',
262 lc => 'lcase',
263 m => 'min',
264 M => 'max',
265 n => 'count',
266 ni => 'not in', # -"-
267 s => 'substring',
268 u => 'using', # -"-
269 uc => 'ucase'
270 );
271  
272 # functions where the 1st argument can be now()
273 4 my @nowFunctions = qw(
274 adddate addtime convert_tz date date_add date_format date_sub datediff day
275 dayname dayofmonth dayofweek dayofyear hour last_day minute month
276 monthname quarter second subdate subtime time time_format time_to_sec
277 timediff timestamp to_days to_seconds week weekday weekofyear year
278 yearweek
279 );
280 4 our @Functions = sort @nowFunctions, qw(
281 abs acos aes_decrypt aes_encrypt ascii asin atan avg benchmark bin bit_and
282 bit_count bit_length bit_or bit_xor cast ceiling char_length char
283 character_length charset coalesce coercibility collation compress
284 concat_ws concat connection_id conv cos cot count crc32 curdate
285 current_date current_time current_timestamp current_user curtime database
286 decode default degrees des_decrypt des_encrypt elt encode encrypt exp
287 export_set field find_in_set floor format found_rows from_days
288 from_unixtime get_format get_lock greatest group_concat hex if ifnull
289 inet_aton inet_ntoa insert instr interval is_free_lock is_used_lock isnull
290 last_insert_id lcase least left length ln load_file localtime
291 localtimestamp locate log10 log2 log lower lpad ltrim make_set makedate
292 maketime master_pos_wait max md5 microsecond mid min mod name_const now
293 nullif oct octet_length old_password ord password period_add period_diff
294 pi position power quote radians rand release_lock repeat replace reverse
295 right round row_count rpad rtrim schema sec_to_time session_user sha1 sign
296 sin sleep soundex space sqrt stddev stddev_pop stddev_samp str_to_date
297 strcmp substring_index substring sum sysdate system_user tan timestampadd
298 timestampdiff trim truncate ucase uncompress uncompressed_length unhex
299 unix_timestamp upper user utc_date utc_time utc_timestamp uuid values
300 var_pop var_samp variance
301 );
302  
303 4 our %DefaultArguments = (
304 count => '*',
305 concat_ws => "','"
306 );
307 4 $DefaultArguments{$_} = 'now()' for @nowFunctions;
308  
309  
310 4 our %Tables;
311 4 our %Columns;
312  
313 sub regexp($$) {
314 5 ▶ my( $str, $type ) = @_;
Subroutine regexp: called CC=6 SCAR=18.5
Pod documented
315 5 ▶ if( $type < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type < 2) 2 3 5
316 2 ▶ return if $str !~ /_/; # Otherwise same as find sprintf cases
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /_/u 2 0 2
317 0 ▶ return ($type ? '' : '^') . join '.*?_', split /_/, $str; # 0 & 1
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 0 0 0
318 }
319 3 my $expr = join '.*?', split //, $str; # 2, 3 & 4
320 3 ▶ if( $type < 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type < 4) 2 1 3
321 2 substr $expr, 0, 0, '^'; # 2 & 3
322 2 ▶ $expr .= '$' if $type == 2; # 2
Branch: 1 branch
BranchTrueFalseTotal
if $type == 2 1 1 2
323 }
324 3 $expr;
325 }
326  
327 4 my $error;
328 4 my @simple = qw(^%s$ ^%s_ ^%s _%s$ _%s %s$ %s_ %s);
329 sub find($$$\%;\@) {
330 53 ▶ my( $str, $prefix, $suffix, $hash, $list ) = @_;
Subroutine find: called CC=12 SCAR=26.6
Pod undocumented
331 53 my $ret = $hash->{$str};
332 53 ▶ return $ret if $ret;
Branch: 1 branch
BranchTrueFalseTotal
if $ret 40 13 53
333  
334 13 $ret = $hash->{''};
335 13 ▶ $ret = &$ret( $str ) if $ret;
Branch: 1 branch
BranchTrueFalseTotal
if $ret 2 11 13
336 13 ▶ return $ret if $ret;
Branch: 1 branch
BranchTrueFalseTotal
if $ret 2 11 13
337  
338 11 ▶ if( $list ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list) 11 0 11
339 11 for my $type ( 0..@simple+4 ) { # Try to find a more and more fuzzy match.
340 23 ▶ my $expr = $type < @simple ?
Branch: 1 branch
BranchTrueFalseTotal
$type < @simple ? : 18 5 23
341 sprintf $simple[$type], $str :
342 regexp $str, $type - @simple;
343 23 ▶ next unless defined $expr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expr 2 21 23
344 21 my @res = grep /$expr/i, @$list;
345 21 ▶ if( @res ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@res) 10 11 21
346 10 ▶ return $res[0] if @res == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @res == 1 10 0 10
347 0 warn "$prefix$str$suffix matches @res\n";
348 0 $error = 1;
349 0 return '';
350 }
351 }
352 }
353 # no special syntax for fields or functions, so don't fail on real one
354 1 ▶ return $str if ord $prefix == ord '.' or ord $suffix == ord '(';
Branch: 1 branch
BranchTrueFalseTotal
if ord $prefix == 46 or ord $suffix == 40 1 0 1
Condition33%1/3
ord $prefix == 46 or ord $suffix == 40
l!l&&r!l&&!r
100
MC/DC0%0/2
ord $prefix == 46 or ord $suffix == 40
ord $prefix == 46 ord $suffix == 40
Truth table33%1/3
A or B
A= ord $prefix == 46B= ord $suffix == 40
ABresult
1X1
011
000
355  
356 0 warn "$prefix$str$suffix doesn't match\n";
357 0 $error = 1;
358 }
359  
360 4 my %rq = ('[', ']',
361 '{', '}');
362 4 ▶my $quote_re = qr(\\([-,:;./ #?\N{GREEK SMALL LETTER OMEGA}^\\\@!'"`[\]{}]*)(?:%(.+?))?);
Subroutine BEGIN: called CC=1 SCAR=0.0
363 sub quote($$@) {
364 19 ▶ (local $_, my $join) = splice @_, 0, 2;
Subroutine quote: called CC=19 SCAR=30.2
Pod undocumented
365 19 ▶ $join //= ',';
Condition100%2/2
$join //= ","
l!l
118
MC/DC100%1/1
$join //= ","
$join
Truth table100%2/2
A
A= $join
Aresult
11
00
366 19 ▶ return join $join, @_ unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 4 15 19
367 15 my @args = @_;
368 15 /(['"`[{])/;
369 15 my( $lq ) = /(['"`[{])/;
370 15 ▶ $lq ||= "'";
Condition100%2/2
$lq ||= "'"
l!l
105
MC/DC100%1/1
$lq ||= "'"
$lq
Truth table100%2/2
A
A= $lq
Aresult
11
00
371 15 ▶ my $rq = $rq{$lq} || $lq;
Condition66%2/3
$rq{$lq} || $lq
l!l&&r!l&&!r
690
MC/DC0%0/2
$rq{$lq} || $lq
$rq{$lq} $lq
Truth table66%2/3
A || B
A= $rq{$lq}B= $lq
ABresult
1X1
011
000
372 15 my( $noquote, $number, $boolean, $null, $space, $var ) =
373 (tr/!//, tr/#//, tr/?//, tr/\N{GREEK SMALL LETTER OMEGA}^//, tr/\\//, tr/@//);
374 15 ▶ my $split = tr/-// ? '-' : ''; # avoid range by putting - 1st
Branch: 1 branch
BranchTrueFalseTotal
tr/\-// ? : 0 15 15
375 15 ▶ $split .= tr/ // ? '\s' : ''; # space means any whitespace
Branch: 1 branch
BranchTrueFalseTotal
tr/ // ? : 3 12 15
376 15 $split .= join '', /([,:;.\/])/g;
377 15 ▶ $split ||= ',';
Condition100%2/2
$split ||= ","
l!l
411
MC/DC100%1/1
$split ||= ","
$split
Truth table100%2/2
A
A= $split
Aresult
11
00
378 15 ▶ $split = $space ? qr/[$split]/ : qr/\s*[$split]\s*/;
Branch: 1 branch
BranchTrueFalseTotal
$space ? : 1 14 15
379 join $join, map {
380 34 ▶ if( $noquote || $boolean && /^(?:true|false)$/i || $null && /^null$/i || $var && /^\@\w+$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($noquote or $boolean and /^(?:true|false)$/iu or $null and /^null$/iu or $var and /^\@\w+$/u) { } 3 31 34
elsif ($number and /^[-+]?(?:0b[01]+|0x[\da-f]+|(?=\.?\d)\d*\.?\d*(?:e[-+]?\d+)?)$/iu) { } 4 27 31
Condition50%9/18
$noquote or $boolean and /^(?:true|false)$/iu or $null and /^null$/iu or $var and /^\@\w+$/u
l!l&&r!l&&!r
3031
$noquote or $boolean and /^(?:true|false)$/iu or $null and /^null$/iu
l!l&&r!l&&!r
3031
$noquote or $boolean and /^(?:true|false)$/iu
l!l&&r!l&&!r
3031
$boolean and /^(?:true|false)$/iu
!ll&&!rl&&r
3100
$null and /^null$/iu
!ll&&!rl&&r
3100
$var and /^\@\w+$/u
!ll&&!rl&&r
3100
MC/DC14%1/7
$noquote or $boolean and /^(?:true|false)$/iu or $null and /^null$/iu or $var and /^\@\w+$/u
$noquote $boolean /^(?:true|false)$/iu $null /^null$/iu $var /^\@\w+$/u
Truth table12%2/16
A or B and C or D and E or F and G
A= $noquoteB= $booleanC= /^(?:true|false)$/iuD= $nullE= /^null$/iuF= $varG= /^\@\w+$/u
ABCDEFGresult
1XXXXXX1
011XXXX1
00X11XX1
01011XX1
00X0X111
00X10111
0100X111
01010111
00X0X0X0
00X0X100
00X100X0
00X10100
0100X0X0
0100X100
010100X0
01010100
381 3 $_;
382 ▶ } elsif( $number && /^[-+]?(?:0b[01]+|0x[\da-f]+|(?=\.?\d)\d*\.?\d*(?:e[-+]?\d+)?)$/i ) {
Condition100%3/3
$number and /^[-+]?(?:0b[01]+|0x[\da-f]+|(?=\.?\d)\d*\.?\d*(?:e[-+]?\d+)?)$/iu
!ll&&!rl&&r
15124
MC/DC100%2/2
$number and /^[-+]?(?:0b[01]+|0x[\da-f]+|(?=\.?\d)\d*\.?\d*(?:e[-+]?\d+)?)$/iu
$number /^[-+]?(?:0b[01]+|0x[\da-f]+|(?=\.?\d)\d*\.?\d*(?:e[-+]?\d+)?)$/iu
Truth table100%3/3
A and B
A= $numberB= /^[-+]?(?:0b[01]+|0x[\da-f]+|(?=\.?\d)\d*\.?\d*(?:e[-+]?\d+)?)$/iu
ABresult
0X0
100
111
383 4 $_;
384 } else {
385 27 s/$rq/$rq$rq/g;
386 27 "$lq$_$rq";
387 }
388 } map {
389 15 ▶ unless( $space ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($space) 19 1 20
390 19 s/\A\s*//;
391 19 s/\s*\Z//;
392 }
393 20 split $split, $_, -1;
394 } @args;
395 }
396  
397 sub convert_Query($$) {
398 14 ▶ my $name = $_[0];
Subroutine convert_Query: called CC=16 SCAR=27.7
Pod undocumented
399 14 my $res = find $name, '&', '', %Queries;
400 14 my $ref = ref $res;
401 14 my @arg;
402 14 ▶ for( $ref ? $_[1] : "$res\cA$_[1]" ) {
Branch: 1 branch
BranchTrueFalseTotal
$ref ? : 4 10 14
403 14 &convert_table_column;
404 14 ▶ ($res, $_) = split "\cA" unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 10 4 14
405 14 @arg = split ';';
406 }
407 14 ▶ return &$res( $name, @arg ) // '' if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 4 10 14
Condition50%1/2
&$res($name, @arg) // ""
l!l
40
MC/DC0%0/1
&$res($name, @arg) // ""
&$res($name, @arg)
Truth table50%1/2
A
A= &$res($name, @arg)
Aresult
11
00
408  
409 10 my( @var, %seen, @rest );
410 10 $res =~ s(\$$quote_re?(?:(\d+\b)|([*>_]))) {
411 16 ▶ if( $3 && $3 > @arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($3 and $3 > @arg) { } 2 14 16
Condition100%3/3
$3 and $3 > @arg
!ll&&!rl&&r
4102
MC/DC100%2/2
$3 and $3 > @arg
$3 $3 > @arg
Truth table100%3/3
A and B
A= $3B= $3 > @arg
ABresult
0X0
100
111
412 2 '';
413 } else {
414 14 ▶ push @var, [$1, $2, $4 ? (undef, $4) : $3-1];
Branch: 1 branch
BranchTrueFalseTotal
$4 ? : 3 11 14
415 14 ▶ undef $seen{$3-1} if $3; # make it exist
Branch: 1 branch
BranchTrueFalseTotal
if $3 10 4 14
416 14 "\cV$#var\cZ";
417 }
418 }eg;
419 10 ▶ if( @arg > keys %seen ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@arg > keys %seen) 1 9 10
420 1 @rest = @arg;
421 1 undef $rest[$_] for keys %seen;
422 1 @rest = grep defined(), @rest;
423 }
424 10 $res =~ s(\cV(\d+)\cZ) {
425 14 my @res = @{$var[$1]};
426 14 ▶ quote $res[0], $res[1],
Branch: 4 branches
BranchTrueFalseTotal
$res[3] ? : 3 11 14
$res[3] eq '*' ? : 1 2 3
$res[3] eq '>' ? : 2 0 2
$res[2] < 0 ? : 1 10 11
427 $res[3] ? ($res[3] eq '*' ? @arg :
428 $res[3] eq '>' ? @rest :
429 $_) :
430 $res[2] < 0 ? $name : $arg[$res[2]];
431 }eg;
432 10 $res;
433 }
434  
435 4 my @keys_Table_Columns;
436 sub convert_table_column {
437 30 ▶ @keys_Table_Columns = keys %Table_Columns unless @keys_Table_Columns;
Subroutine convert_table_column: called CC=13 SCAR=32.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless @keys_Table_Columns 30 0 30
438 30 ▶ s&(?<!\\)#(\w+)(?:#(\w*))?&find( $1, '#', '', %Tables, @keys_Table_Columns ) . ($2 ? " $2" : defined $2 ? " $1" : '')&eg unless $error;
Branch: 3 branches
BranchTrueFalseTotal
unless $error 30 0 30
$2 ? : 0 0 0
defined $2 ? : 0 0 0
439  
440 30 ▶ unless( $table_re ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($SQL::Steno::table_re) 2 28 30
441 2 $table_re = join '|', keys %Table_Columns;
442 2 ▶ $table_re = $table_re ? qr/\b(?:$table_re)\b/ : qr/\s\b\s/;
Branch: 1 branch
BranchTrueFalseTotal
$SQL::Steno::table_re ? : 0 2 2
443 }
444 30 ▶ unless( $error ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 30 0 30
445 30 my %column;
446 30 for( grep /$table_re/io, split /\W+/ ) {
447 0 undef $column{$_} for @{$Table_Columns{$_}};
448 }
449 30 my @column = keys %column;
450 30 ▶ s/(^|.&|[-+\s(,;|])?(?<!\\)\.([a-z]\w*)(?:\.(\w*))?/(defined $1 ? $1 : '.') . (find $2, '.', '', %Columns, @column) . ($3 ? " $3" : defined $3 ? " $2" : '')/egi;
Branch: 3 branches
BranchTrueFalseTotal
defined $1 ? : 0 1 1
$3 ? : 0 1 1
defined $3 ? : 0 1 1
451 }
452 }
453  
454  
455 =head2 convert
456  
457 This function takes a short-hand query in C<$_> and transforms it to SQL. See
458 L</shell> for more run time oriented features.
459  
460 =head3 C<:\I<spec>(I<strings>)> E<nbsp> or E<nbsp> C<:\I<spec>%I<join>(I<strings>)>
461  
462 This is a special macro that transforms odd lists to SQL syntax. It takes a
463 list of unquoted strings and quotes each one of them in various ways. The
464 syntax is inspired by the Shell single character quote and Perl's C<\(...)>
465 syntax. The I<strings> can get split on a variety of one or even
466 simultaneously various characters, which you can give in any order in the
467 I<spec>:
468  
469 =over
470  
471 =item C<\> (backslash)
472  
473 This one isn't a character to split on, but rather prevents trimming the
474 whitespace of both ends of the resulting strings.
475  
476 =item C<,> (comma), the default
477  
478 You only need to specify it, if you want to split on commas, as well as other
479 characters, at the same time.
480  
481 =item C<E<nbsp>> (space)
482  
483 This one stands for any single whitespace. Since strings are normally
484 trimmed, it's the equivalent of what the Shell does. But, if you combine it
485 with C<\>, which prevents trimming, you will get an empty string between each
486 of multiple whitespaces.
487  
488 =item C<-> (minus)
489  
490 =item C<:> (colon)
491  
492 =item C<;> (semicolon)
493  
494 =item C<.> (period)
495  
496 =item C</> (slash)
497  
498 =back
499  
500 The I<spec> can also contain several of these characters to prevent certain
501 strings from being quoted:
502  
503 =over
504  
505 =item C<#> (hash)
506  
507 All numbers, including signed, floats, binary and hexadecimal stay literal.
508 If you use C<-> as a separator, there can be no negative numbers.
509  
510 =item C<?> (question mark)
511  
512 The boolean values C<true> and C<false> stay literal.
513  
514 =item C<ω> (omega) E<nbsp> or E<nbsp> C<^> (caret)
515  
516 The value C<null> stays literal.
517  
518 =item C<@> (at sign)
519  
520 Variables C<@name> stay literal.
521  
522 =item C<!> (exclamation mark)
523  
524 Everything, presumably column names, stays literal.
525  
526 =back
527  
528 And you can give at most one I<spec> for the quotes to use:
529  
530 =over
531  
532 =item C<'> (quote), the default
533  
534 =item C<"> (double quote)
535  
536 =item C<`> (backquote)
537  
538 =item C<[]> (brackets)
539  
540 =item C<{}> (braces)
541  
542 In the latter two cases, the closing quotes are optional, for decoration only,
543 or if code completion adds them.
544  
545 =back
546  
547 The results are joined by comma, unless I<join> is given. E.g. C<:\(a,b,
548 c,NULL,true,-1.2)> gives C<'a','b','c','NULL','true','-1.2'>, while
549 C<:\:"/\#?0(a:b/ c:NULL:true/-1.2)> gives C<"a","b"," c",NULL,true,-1.2> and
550 C<:\ !%&&(a b E<nbsp>c)> gives C<a&&b&&c>.
551  
552  
553  
554 =head3 C<:I<macro>>
555  
556 These are mostly simple text-replacements stored in C<%Macros>. Unlike
557 L<C<:\>|/spec-strings-or-:-spec-join-strings> these do not take arguments.
558 There are also some dynamic macros. Those starting with C<:j> (join) or
559 C<:lj> (left join) may continue into a L<table spec|/tbl-or-tbl-or-tbl-alias>
560 without the leading C<#>. E.g. C<:ljtbl#t> might expand to C<left join table
561 t>.
562  
563 Those starting with C<:gb> (group by) or C<:ob> (order by) may be followed by
564 result columns numbers from 1-9, each optionally followed by a or d for asc or
565 desc. E.g. C<:ob2d3> gives C<order by 2 desc, 3>.
566  
567 =head3 C<:+I<interval>> E<nbsp> or E<nbsp> C<:I<time>+I<interval>> E<nbsp> or E<nbsp> C<:-I<interval>> E<nbsp> or E<nbsp> C<:I<time>-I<interval>>
568  
569 These are time calculation macros, where an optional leading letter indicates
570 a base time, and an optional trailing letter with an optional count means the
571 offset. The letters are:
572  
573 =over
574  
575 =item y
576  
577 (this) year(start). E.g. C<:y+2m> is march this year.
578  
579 =item q
580  
581 (this) quarter(start). E.g. C<:q+0> is this quarter, C<:q+q> is next quarter.
582  
583 =item m
584  
585 (this) month(start). E.g. C<:-3m> is whatever precedes, minus 3 months.
586  
587 =item w
588  
589 (this) week(start). E.g. C<:w+3d> is this week thursday (or wednesday if you
590 set C<$weekstart> to not follow ISO 8601 and the bible).
591  
592 =item d
593  
594 (this) day(start). E.g. C<:d-w> is midnight one week ago.
595  
596 =item h
597  
598 (this) hour(start). E.g. C<:h+30M> is half past current hour.
599  
600 =item M
601  
602 (this) minute(start). E.g. C<:+10M> is whatever precedes, plus 10min.
603  
604 =item s
605  
606 (this) second. E.g. C<:s-2h> is exactly 2h ago.
607  
608 =back
609  
610  
611 =head3 C<:{I<Perl code>}>
612  
613 This gets replaced by what it returns.
614  
615  
616 =head3 C<#I<tbl>> E<nbsp> or E<nbsp> C<#I<tbl>#> E<nbsp> or E<nbsp> C<#I<tbl>#I<alias>>
617  
618 Here I<tbl> is a key of C<%Tables> or any abbreviation of known tables in
619 C<@Tables>. If followed by C<#>, the abbreviation is used as an alias, unless
620 an I<alias> directly follows, in which case that is used.
621  
622  
623 =head3 C<.I<col>> E<nbsp> or E<nbsp> C<.I<col>.> E<nbsp> or E<nbsp> C<.I<col>.I<alias>>
624  
625 Here I<col> is a key of C<%Columns> or any abbreviation of columns of any table
626 recognized in the query. If followed by C<.>, the abbreviation is used as an
627 alias, unless an I<alias> directly follows, in which case that is used. It tries
628 to be clever about whether the 1st C<.> needs to be preserved, i.e. following
629 a table name.
630  
631 =head3 C<I<func>(> E<nbsp> or E<nbsp> C<I<func>\I<spec>(I<strings>)> E<nbsp>
632 or E<nbsp> C<I<func>\I<spec>%I<join>(I<strings>)>
633  
634 Here I<func> is a key of C<%Functions> or any
635 abbreviation of known functions in C<@Functions>, which includes words
636 typically followed by an opening parenthesis, such as C<u(> for C<using(>.
637 C<i(> becomes C<in(>, whereas C<in(> becomes C<ifnull(>.
638  
639 If the 2nd or 3rd form is used, the I<strings> inside of the parentheses are treated
640 just like C<L<:\I<spec>(I<strings>)|/spec-strings-or-:-spec-join-strings>>,
641 but in this case preserving the parentheses.
642  
643 If the 1st argument of a function is empty and the abbrev or function is found
644 in C<%DefaultArguments> the value becomes the 1st argument.
645 E.g. C<cw(,'a','b','c')> or C<cw(,:\(a,b,c))> both become
646 C<concat_ws(',','a','b','c')>.
647  
648 =head3 Abbreviated Keyword
649  
650 Finally it picks on the structure of the statement: These keywords can be
651 abbreviated: C<se(lect)>, C<ins(ert)>, C<upd(ate)> or C<del(ete)>. If none of
652 these or C<set> is present, C<select> is assumed as default (more keywords
653 need to be recognized in the future).
654  
655 For C<select>, semicolons are alternately replaced by C<from> (the 1st being
656 optional if it starts with a table name) and C<where>. If no result columns
657 are given, they default to C<*>, see L</SYNOPSIS>. For C<update>, semicolons
658 are frst replaced by C<set> and then C<where>.
659  
660 =cut
661  
662 sub convert {
663 # Handle :\...(str1, str2, str3)
664 16 ▶ s<(?:\b(\w+)|:)$quote_re\((.+?)\)> {
Subroutine convert: called CC=24 SCAR=36.9
Pod documented
665 5 ▶ ($1 ? "$1(" : '') .
Branch: 2 branches
BranchTrueFalseTotal
$1 ? : 3 2 5
$1 ? : 3 2 5
666 quote($2, $3, $4 ) .
667 ($1 ? ')' : '')
668 }ge;
669 16 my @strings; # extract strings to prevent following replacements inside.
670 16 ▶ while( /\G.*?(['"`[{])/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G.*?(['"`[{])/cgu) 44 16 60
671 44 ▶ my $rq = $rq{$1}||$1;
Condition66%2/3
$rq{$1} || $1
l!l&&r!l&&!r
20240
MC/DC0%0/2
$rq{$1} || $1
$rq{$1} $1
Truth table66%2/3
A || B
A= $rq{$1}B= $1
ABresult
1X1
011
000
672 44 my $pos = pos;
673 44 ▶ while( /\G.*?([$rq\\])/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G.*?([$rq\\])/cgu) 60 0 60
674 60 ▶ if( $1 eq '\\' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($1 eq '\\') { } 5 55 60
elsif (not /\G$rq/cgu) { } 44 11 55
675 5 ++pos; # skip next
676 } elsif( ! /\G$rq/gc ) { # skip doubled quote
677 44 push @strings,
678 substr $_, $pos - 1, 1 - $pos + pos, # get string
679 "\cS".@strings."\cZ"; # and replace with counter
680 44 last;
681 }
682 }
683 }
684  
685 16 ▶ until( $error ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 24 0 24
686 24 ▶ s&:$perl_re&my $ret = eval $1; warn $@ if $@; $ret // ' NULL '&ego or
Branch: 2 branches
BranchTrueFalseTotal
s[:$SQL::Steno::perl_re][my $ret = eval $1; warn $@ if $@; $ret // " NULL ";]egou or s/:(${timespec_re}[+-]\d*$timespec_re(?(?<=\w)\b)|l?j\w+(?:#(\w*))|\w+)/&find($1, ":", "", \%SQL::Steno::Macros);/egou or last 16 8 24
if $@ 0 0 0
Condition40%2/5
s[:$SQL::Steno::perl_re][my $ret = eval $1; warn $@ if $@; $ret // " NULL ";]egou or s/:(${timespec_re}[+-]\d*$timespec_re(?(?<=\w)\b)|l?j\w+(?:#(\w*))|\w+)/&find($1, ":", "", \%SQL::Steno::Macros);/egou
l!l&&r!l&&!r
0816
$ret // " NULL "
l!l
00
MC/DC50%1/2
s[:$SQL::Steno::perl_re][my $ret = eval $1; warn $@ if $@; $ret // " NULL ";]egou or s/:(${timespec_re}[+-]\d*$timespec_re(?(?<=\w)\b)|l?j\w+(?:#(\w*))|\w+)/&find($1, ":", "", \%SQL::Steno::Macros);/egou
s[:$SQL::Steno::perl_re][my $ret = eval $1; warn $@ if $@; $ret // " NULL ";]egou s/:(${timespec_re}[+-]\d*$timespec_re(?(?<=\w)\b)|l?j\w+(?:#(\w*))|\w+)/&find($1, ":", "", \%SQL::Steno::Macros);/egou
MC/DC0%0/1
$ret // " NULL "
$ret
Truth table66%2/3
A or B
A= s[:$SQL::Steno::perl_re][my $ret = eval $1; warn $@ if $@; $ret // " NULL ";]egouB= s/:(${timespec_re}[+-]\d*$timespec_re(?(?<=\w)\b)|l?j\w+(?:#(\w*))|\w+)/&find($1, ":", "", \%SQL::Steno::Macros);/egou
ABresult
1X1
011
000
Truth table0%0/2
A
A= $ret
Aresult
11
00
687 # \todo (?(?<=\w)\b)
688 23 s&:($timespec_re[+-]\d*$timespec_re(?(?<=\w)\b)|l?j\w+(?:#(\w*))|\w+)&find $1, ':', '', %Macros&ego or
689 last;
690 }
691  
692 16 s&^(?=#)&;&; # Assume empty fieldlist before table name
693 16 &convert_table_column;
694 16 s&^(?=$table_re)&;&; # Assume empty fieldlist before table name
695  
696 16 ▶ s&\b(\w+)\((?=\s*([,)])?)&my $fn = find $1, '', '(', %Functions, @Functions; ($fn || $1).'('.($2 and $DefaultArguments{$1} || $DefaultArguments{$fn} or '')&eg unless $error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 16 0 16
Condition45%5/11
$fn || $1
l!l&&r!l&&!r
1300
$2 && ($SQL::Steno::DefaultArguments{$1} || $SQL::Steno::DefaultArguments{$fn}) || ''
l!l
013
$2 and $SQL::Steno::DefaultArguments{$1} || $SQL::Steno::DefaultArguments{$fn}
!ll&&!rl&&r
940
$SQL::Steno::DefaultArguments{$1} or $SQL::Steno::DefaultArguments{$fn}
l!l&&r!l&&!r
004
MC/DC0%0/2
$fn || $1
$fn $1
MC/DC0%0/3
$2 && ($SQL::Steno::DefaultArguments{$1} || $SQL::Steno::DefaultArguments{$fn}) || ''
$2 $SQL::Steno::DefaultArguments{$1} $SQL::Steno::DefaultArguments{$fn}
Truth table33%1/3
A || B
A= $fnB= $1
ABresult
1X1
011
000
Truth table50%2/4
A and B or C
A= $2B= $SQL::Steno::DefaultArguments{$1}C= $SQL::Steno::DefaultArguments{$fn}
ABCresult
11X1
1011
0XX0
1000
697 #s&\b(\w+)(?=\()&find $1, '', '(', %Functions, @Functions or $1&eg unless $error;
698  
699 16 ▶ return if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 16 16
700 16 s/\A\s*;/*;/;
701 16 s/;\s*\Z//;
702 16 ▶ if( s/^upd(?:a(?:t(?:e)?)?)?\b/update/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^upd(?:a(?:t(?:e)?)?)?\b/update/iu) { } 0 16 16
703 0 ▶ s/(?<!\\);(?:\s*set\s*)?/ set / && s/(?<!\\);(?:\s*where\s*)?/ where /;
Branch: 1 branch
BranchTrueFalseTotal
s/(?<!\\);(?:\s*set\s*)?/ set /u and s/(?<!\\);(?:\s*where\s*)?/ where /u 0 0 0
704 } else {
705 16 ▶ s/(?<!\\);(?:\s*where\s*)?/ where / while s/(?<!\\);(?:\s*from\s*)?/ from /;
Branch: 1 branch
BranchTrueFalseTotal
s/(?<!\\);(?:\s*from\s*)?/ from /u and do { s/(?<!\\);(?:\s*where\s*)?/ where /u } 4 16 20
706 16 ▶ s/^ins(?:e(?:r(?:t)?)?)?\b/insert/i ||
Branch: 1 branch
BranchTrueFalseTotal
s/^ins(?:e(?:r(?:t)?)?)?\b/insert/iu or s/^del(?:e(?:t(?:e)?)?)?\b/delete/iu or s/^(?!se(?:lec)?t)/select /iu 16 0 16
Condition33%1/3
s/^ins(?:e(?:r(?:t)?)?)?\b/insert/iu or s/^del(?:e(?:t(?:e)?)?)?\b/delete/iu
l!l&&r!l&&!r
0016
MC/DC0%0/2
s/^ins(?:e(?:r(?:t)?)?)?\b/insert/iu or s/^del(?:e(?:t(?:e)?)?)?\b/delete/iu
s/^ins(?:e(?:r(?:t)?)?)?\b/insert/iu s/^del(?:e(?:t(?:e)?)?)?\b/delete/iu
Truth table33%1/3
A or B
A= s/^ins(?:e(?:r(?:t)?)?)?\b/insert/iuB= s/^del(?:e(?:t(?:e)?)?)?\b/delete/iu
ABresult
1X1
011
000
707 s/^del(?:e(?:t(?:e)?)?)?\b/delete/i ||
708 s/^(?!se(?:lec)?t)/select /i;
709 }
710  
711 16 s/ $//mg;
712 16 s/ {2,}/ /g;
713 16 s/\cS(\d+)\cZ/$strings[$1]/g; # put back the strings
714  
715 16 1;
716 }
717  
718  
719 # escape map for special replacement characters
720 4 ▶my %esc = map { $_ eq 'v' ? "\013" : eval( qq!"\\$_"! ), "\\$_" } qw'0 a b e f n r t v \ "';
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'v' ? : 4 40 44
721  
722 # With an argument of total number of rows, init output counting and return undef if it is to be skipped (not stdout).
723 # Without an argument, do the counting and return undef if no more rows wanted.
724 {
725 4 my( $total, $cnt, $i );
726 sub count(;$) {
727 16 ▶ if( @_ ) {
Subroutine count: called CC=9 SCAR=31.5
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 11 5 16
728 11 $total = $_[0];
729 11 $cnt = 0;
730 11 $i = 100;
731 11 ▶ return select eq 'main::STDOUT' ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
select() eq 'main::STDOUT' ? : 2 9 11
732 }
733 5 ++$cnt;
734 5 ▶ if( --$i <= 0 && $cnt < $total ) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$i <= 0 and $cnt < $total) 0 5 5
Condition33%1/3
--$i <= 0 and $cnt < $total
!ll&&!rl&&r
500
MC/DC0%0/2
--$i <= 0 and $cnt < $total
--$i <= 0 $cnt < $total
Truth table33%1/3
A and B
A= --$i <= 0B= $cnt < $total
ABresult
0X0
100
111
735 0 printf STDERR "How many more, * for all, or q to quit? (%d of %d) [default: 100] ",
736 $cnt, $total;
737 0 $i = <>;
738 0 ▶ if( defined $i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $i) { } 0 0 0
739 0 $i =~ tr/qQxX \t\n\r/0000/d;
740 0 ▶ $i = (0 == length $i) ? 100 :
Branch: 3 branches
BranchTrueFalseTotal
0 == length $i ? : 0 0 0
$i eq '*' ? : 0 0 0
$i == 0 ? : 0 0 0
741 $i eq '*' ? ~0 :
742 $i == 0 ? return :
743 $i;
744 } else {
745 0 print "\n";
746 0 return;
747 }
748 }
749 5 1;
750 }
751 }
752  
753 4 sub render_csv($;$$) {
754 6 ▶ my( $sth, $filter ) = @_;
Subroutine render_csv: called CC=15 SCAR=27.1
Pod undocumented
755 my( $semi, $tab ) =
756 (exists $_[2]{semi},
757 exists $_[2]{tab})
758 6 ▶ if $_[2];
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] 5 1 6
759 6 my $name = $sth->{NAME};
760 6 my @row = @$name;
761 6 while() {
762 18 for( @row ) {
763 162 ▶ if( defined ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) { } 156 6 162
764 156 ▶ $_ = qq!"$_"! if
Branch: 3 branches
BranchTrueFalseTotal
if /\A\Z/u or s/"/""/gu or $semi ? tr/\n;// : ($tab ? tr/\t\n// : tr/\n,//) or /\A=/u 15 132 147
$semi ? : 49 98 147
$tab ? : 49 49 98
Condition88%8/9
/\A\Z/u or s/"/""/gu or $semi ? tr/\n;// : ($tab ? tr/\t\n// : tr/\n,//) or /\A=/u
l!l&&r!l&&!r
240132
/\A\Z/u or s/"/""/gu or $semi ? tr/\n;// : ($tab ? tr/\t\n// : tr/\n,//)
l!l&&r!l&&!r
915132
/\A\Z/u or s/"/""/gu
l!l&&r!l&&!r
36147
MC/DC75%3/4
/\A\Z/u or s/"/""/gu or $semi ? tr/\n;// : ($tab ? tr/\t\n// : tr/\n,//) or /\A=/u
/\A\Z/u s/"/""/gu $semi ? tr/\n;// : ($tab ? tr/\t\n// : tr/\n,//) /\A=/u
Truth table80%4/5
A or B or C or D
A= /\A\Z/uB= s/"/""/guC= $semi ? tr/\n;// : ($tab ? tr/\t\n// : tr/\n,//)D= /\A=/u
ABCDresult
1XXX1
01XX1
001X1
00011
00000
765 /\A\Z/ or
766 s/"/""/g or
767 $semi ? tr/;\n// : $tab ? tr/\t\n// : tr/,\n// or
768 /\A=/;
769 } else {
770 6 $_ = '';
771 }
772 162 utf8::decode $_;
773 }
774 18 ▶ print join( $semi ? ';' : $tab ? "\t" : ',', @row ) . "\n";
Branch: 2 branches
BranchTrueFalseTotal
$semi ? : 6 12 18
$tab ? : 6 6 12
775  
776 21 ▶ FETCH:
Branch: 1 branch
BranchTrueFalseTotal
@row = $sth->fetchrow_array or last 6 15 21
777 @row = $sth->fetchrow_array
778 or last;
779 15 ▶ $filter->( $name, @row ) or goto FETCH if $filter;
Branch: 1 branch
BranchTrueFalseTotal
if $filter 6 9 15
Condition80%4/5
$filter && (&$filter($name, @row) || (goto FETCH))
!ll&&!rl&&r
960
&$filter($name, @row) or goto FETCH
l!l
33
MC/DC0%0/2
$filter && (&$filter($name, @row) || (goto FETCH))
$filter &$filter($name, @row)
Truth table66%2/3
A && B
A= $filterB= &$filter($name, @row)
ABresult
0X0
100
111
780 }
781 }
782  
783 4 our $NULL = 'ω';
784 4 utf8::decode $NULL;
785 4 my( $r1, $r2, $r3, $r5 ) = ('[01]\d', '[0-2]\d', '[0-3]\d', '[0-5]\d');
786 sub render_table($;$$) {
787 16 ▶ my( $sth, $filter ) = @_;
Subroutine render_table: called CC=49 SCAR=40.3
Pod undocumented
788 my( $null, $crlf, $date, $time ) =
789 exists $_[2]{all} ?
790 ('NULL', 1, 1, 1) :
791 (exists $_[2]{NULL} ? 'NULL' : exists $_[2]{null} ? 'null' : 0,
792 exists $_[2]{crlf},
793 exists $_[2]{date},
794 exists $_[2]{time})
795 16 ▶ if $_[2];
Branch: 4 branches
BranchTrueFalseTotal
if $_[2] 14 2 16
exists $_[2]{'all'} ? : 2 12 14
exists $_[2]{'NULL'} ? : 0 12 12
exists $_[2]{'null'} ? : 1 11 12
796 16 ▶ $null ||= $NULL;
Condition66%2/3
$null ||= $SQL::Steno::NULL
l!l&&r!l&&!r
3130
MC/DC0%0/2
$null ||= $SQL::Steno::NULL
$null $SQL::Steno::NULL
Truth table66%2/3
A ||= B
A= $nullB= $SQL::Steno::NULL
ABresult
1X1
011
000
797 16 my @name = @{$sth->{NAME}};
798 16 my @len = (0) x @name;
799 16 my( @txt, @res, @comp );
800 16 ▶ while( my @res1 = $sth->fetchrow_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@res1) = $sth->fetchrow_array) 80 16 96
801 80 ▶ $filter->( \@name, @res1 ) or next if $filter;
Branch: 1 branch
BranchTrueFalseTotal
if $filter 1 79 80
Condition60%3/5
$filter && (&$filter(\@name, @res1) || (next))
!ll&&!rl&&r
7910
&$filter(\@name, @res1) or next
l!l
10
MC/DC0%0/2
$filter && (&$filter(\@name, @res1) || (next))
$filter &$filter(\@name, @res1)
Truth table33%1/3
A && B
A= $filterB= &$filter(\@name, @res1)
ABresult
0X0
100
111
802 80 for my $i ( 0..$#res1 ) {
803 580 ▶ if( !defined $res1[$i] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $res1[$i]) { } 4 576 580
elsif (not $res1[$i] =~ /^\d+(?:\.\d+)?$/u) { } 537 39 576
804 4 $res1[$i] = $null;
805 } elsif( $res1[$i] !~ /^\d+(?:\.\d+)?$/ ) {
806 537 $txt[$i] = 1;
807 537 ▶ $res1[$i] =~ s/\r\n/\\R/g unless $crlf;
Branch: 1 branch
BranchTrueFalseTotal
unless $crlf 411 126 537
808 537 $res1[$i] =~ s/([\t\n\r])/$esc{$1}/g;
809 4 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
810 537 ▶ unless( $date ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($date) 215 322 537
811 215 ▶ if( $res1[$i] =~ s/^(\d{4}-)($r1)-0[01]([T ]$r2:$r5(?::$r5(?:[.,]\d{3})?)?(?:Z|[+-]$r2:$r5)?)?$/$1/o ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res1[$i] =~ s/^(\d{4}-)($r1)-0[01]([T ]$r2:$r5(?::$r5(?:[.,]\d{3})?)?(?:Z|[+-]$r2:$r5)?)?$/$1/ou) 109 106 215
812 109 ▶ $res1[$i] .= "$2-" if $2 > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $2 > 1 24 85 109
813 109 ▶ $res1[$i] .= $3 if $3;
Branch: 1 branch
BranchTrueFalseTotal
if $3 91 18 109
814 }
815 }
816 537 ▶ unless( $time ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($time) 215 322 537
817 215 ▶ if( $res1[$i] =~ s/^(\d{4}-(?:$r1-(?:$r3)?)?[T ])?($r2):($r5)(?::($r5)(?:([.,])(\d{3}))?)?(Z|[+-]$r2:$r5)?$/$1/o ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res1[$i] =~ s/^(\d{4}-(?:$r1-(?:$r3)?)?[T ])?($r2):($r5)(?::($r5)(?:([.,])(\d{3}))?)?(Z|[+-]$r2:$r5)?$/$1/ou) 170 45 215
818 170 ▶ $res1[$i] = $1 || '';
Condition100%2/2
$1 || ''
l!l
14228
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
819 170 ▶ if( $2 == 23 && $3 == 59 && ($4 // 59) == 59 && ($6 // 999) == 999 ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($2 == 23 and $3 == 59 and ($4 // 59) == 59 and ($6 // 999) == 999) { } 49 121 170
elsif ($6 > 0) { } 24 97 121
elsif ($4 > 0) { } 12 85 97
elsif ($3 > 0) { } 12 73 85
Condition76%10/13
$2 == 23 and $3 == 59 and ($4 // 59) == 59 and ($6 // 999) == 999
!ll&&!rl&&r
121049
$2 == 23 and $3 == 59 and ($4 // 59) == 59
!ll&&!rl&&r
121049
$2 == 23 and $3 == 59
!ll&&!rl&&r
121049
$4 // 59
l!l
3613
$6 // 999
l!l
2425
MC/DC50%2/4
$2 == 23 and $3 == 59 and ($4 // 59) == 59 and ($6 // 999) == 999
$2 == 23 $3 == 59 $4 $6
Truth table60%3/5
A and B and C and D
A= $2 == 23B= $3 == 59C= $4D= $6
ABCDresult
0XXX0
10XX0
110X0
11100
11111
820 49 $res1[$i] .= "24:";
821 } elsif( $6 > 0 ) {
822 24 $res1[$i] .= "$2:$3:$4$5$6";
823 } elsif( $4 > 0 ) {
824 12 $res1[$i] .= "$2:$3:$4";
825 } elsif( $3 > 0 ) {
826 12 $res1[$i] .= "$2:$3";
827 } else {
828 73 $res1[$i] .= "$2:";
829 }
830 170 ▶ ($res1[$i] .= $7) =~ s/:00$/:/
Branch: 1 branch
BranchTrueFalseTotal
if $7 84 86 170
831 if $7;
832 }
833 }
834 537 utf8::decode $res1[$i];
835 }
836 580 ▶ $txt[$i] = 0 if @txt < $i;
Branch: 1 branch
BranchTrueFalseTotal
if @txt < $i 10 570 580
837 580 my $len = length $res1[$i];
838 580 ▶ $len[$i] = $len if $len[$i] < $len;
Branch: 1 branch
BranchTrueFalseTotal
if $len[$i] < $len 184 396 580
839 }
840 80 ▶ if( @comp ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@comp) { } 71 9 80
841 71 for my $i ( 0..$#comp ) {
842 509 ▶ undef $comp[$i] if defined $comp[$i] && $comp[$i] ne $res1[$i];
Branch: 1 branch
BranchTrueFalseTotal
if defined $comp[$i] and $comp[$i] ne $res1[$i] 62 447 509
Condition100%3/3
defined $comp[$i] and $comp[$i] ne $res1[$i]
!ll&&!rl&&r
3965162
MC/DC100%2/2
defined $comp[$i] and $comp[$i] ne $res1[$i]
defined $comp[$i] $comp[$i] ne $res1[$i]
Truth table100%3/3
A and B
A= defined $comp[$i]B= $comp[$i] ne $res1[$i]
ABresult
0X0
100
111
843 }
844 } else {
845 9 @comp = @res1;
846 }
847 80 push @res, \@res1;
848 }
849 16 ▶ if( @res ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@res) 9 7 16
850 9 ▶ @comp = () if @res == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @res == 1 1 8 9
851 9 my $fmt = '';
852 9 ▶ for( my $i = 0; $i < @name; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @name) 71 9 80
853 71 $name[$i] =~ s/\r\n/\\R/g;
854 71 $name[$i] =~ s/([\t\n\r])/$esc{$1}/g;
855 71 ▶ if( defined $comp[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comp[$i]) 0 71 71
856 0 my $more;
857 0 ▶ while( defined $comp[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comp[$i]) 0 0 0
858 0 ▶ printf $fmt, @name[0..$i-1] unless $more;
Branch: 1 branch
BranchTrueFalseTotal
unless $more 0 0 0
859 0 $more = 1;
860 0 printf "[%s=%s]", $name[$i], $comp[$i];
861 0 @name[0..$i] = ('') x ($i+1);
862 0 for my $row ( \@comp, \@name, \@len, \@txt, @res ) {
863 0 splice @$row, $i, 1;
864 }
865 }
866 0 print "\n";
867 0 --$i, next;
868 }
869 71 ▶ if( $len[$i] < length $name[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len[$i] < length $name[$i]) 9 62 71
870 9 printf "$fmt%s\n", @name[0..$i];
871 9 @name[0..$i] = ('') x ($i+1);
872 }
873 71 ▶ $fmt .= '%' . ($txt[$i] ? -$len[$i] : $len[$i]) . 's|';
Branch: 1 branch
BranchTrueFalseTotal
$txt[$i] ? : 67 4 71
874 }
875 9 $fmt .= "\n";
876 9 ▶ printf $fmt, @name if $name[-1];
Branch: 1 branch
BranchTrueFalseTotal
if $name[-1] 8 1 9
877 9 printf $fmt, map '-'x$_, @len;
878 9 my $count = count @res; # init
879 9 for my $row ( @res ) {
880 80 printf $fmt, @$row;
881 80 ▶ defined count or last if defined $count;
Branch: 1 branch
BranchTrueFalseTotal
if defined $count 1 79 80
Condition60%3/5
defined $count && (defined &count() || (last))
!ll&&!rl&&r
7910
defined &count() or last
l!l
10
MC/DC0%0/2
defined $count && (defined &count() || (last))
defined $count defined &count()
Truth table33%1/3
A && B
A= defined $countB= defined &count()
ABresult
0X0
100
111
882 }
883 }
884 }
885  
886 4 my $yaml_re = join '', sort keys %esc;
887 4 $yaml_re =~ s!\\!\\\\!;
888 4 ▶my $tabsize = $ENV{TABSIZE} || 8;
Condition100%2/2
$ENV{'TABSIZE'} || 8
l!l
13
MC/DC100%1/1
$ENV{'TABSIZE'} || 8
$ENV{'TABSIZE'}
Truth table100%2/2
A
A= $ENV{'TABSIZE'}
Aresult
11
00
889 sub render_yaml($;$$) {
890 2 ▶ my( $sth, $filter ) = @_;
Subroutine render_yaml: called CC=23 SCAR=31.7
Pod undocumented
891 2 my @label; # Fill on 0th round with same transformation as data (but \n inline)
892 2 ▶ my $count = count $DBI::rows || 1; # init \todo don't know how many unfiltered
Condition100%2/2
&count($DBI::rows) || 1
l!l
11
MC/DC100%1/1
&count($DBI::rows) || 1
&count($DBI::rows)
Truth table100%2/2
A
A= &count($DBI::rows)
Aresult
11
00
893 2 my @row = @{$sth->{NAME}};
894 2 while() {
895 6 local $_;
896 6 my $i = 0;
897 6 for( @row ) {
898 54 ▶ if( !defined ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $_) { } 2 52 54
elsif (/^(?:y(?:es)?|no?|true|false|o(?:n|ff)|-?\.inf|\.nan)$/su) { } 0 52 52
elsif (tr/\-\000-\b\cK-\037!-#%-'*,:<-@[]`{-\237// or @label ? 0 : tr/\n//) { } 15 37 52
elsif (tr/\n//) { } 1 36 37
899 2 $_ = '~';
900 } elsif( /^(?:y(?:es)?|no?|true|false|o(?:n|ff)|-?\.inf|\.nan)$/s ) { # can only be string in Perl or DB
901 0 $_ = "'$_'";
902 ▶ } elsif( tr/][{},?:`'"|<>&*!%#@=~\0-\010\013-\037\177-\237-// or @label ? 0 : tr/\n// ) {
Branch: 1 branch
BranchTrueFalseTotal
@label ? : 25 13 38
Condition100%3/3
tr/\-\000-\b\cK-\037!-#%-'*,:<-@[]`{-\237// or @label ? 0 : tr/\n//
l!l&&r!l&&!r
14137
MC/DC100%2/2
tr/\-\000-\b\cK-\037!-#%-'*,:<-@[]`{-\237// or @label ? 0 : tr/\n//
tr/\-\000-\b\cK-\037!-#%-'*,:<-@[]`{-\237// @label ? 0 : tr/\n//
Truth table100%3/3
A or B
A= tr/\-\000-\b\cK-\037!-#%-'*,:<-@[]`{-\237//B= @label ? 0 : tr/\n//
ABresult
1X1
011
000
903 15 s/([$yaml_re])/$esc{$1}/go;
904 15 s/([\0-\010\013-\037\177-\237])/sprintf "\\x%02x", ord $1/ge;
905 15 $_ = qq!"$_"!;
906 } elsif( tr/\n// ) {
907 1 my $nl = chomp;
908 1 s/^/ /mg;
909 1 ▶ substr $_, 0, 0, $nl ? "|2\n" : "|2-\n";
Branch: 1 branch
BranchTrueFalseTotal
$nl ? : 0 1 1
910 }
911 54 ▶ printf "$label[$i++]$_\n" if @label;
Branch: 1 branch
BranchTrueFalseTotal
if @label 36 18 54
912 }
913 6 ▶ if( @label ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@label) { } 4 2 6
914 4 ▶ defined count or last if defined $count;
Branch: 1 branch
BranchTrueFalseTotal
if defined $count 4 0 4
Condition40%2/5
defined $count && (defined &count() || (last))
!ll&&!rl&&r
040
defined &count() or last
l!l
40
MC/DC0%0/2
defined $count && (defined &count() || (last))
defined $count defined &count()
Truth table0%0/3
A && B
A= defined $countB= defined &count()
ABresult
0X0
100
111
915 } else {
916 2 my $maxlen = 0;
917 2 for( @row ) {
918 18 ▶ substr $_, 0, 0, $maxlen ? ' ' : '- '; # 1st field if no maxlen yet
Branch: 1 branch
BranchTrueFalseTotal
$maxlen ? : 16 2 18
919 18 my $length = 0;
920 18 ▶ $length += $1 ? $tabsize - $length % $tabsize : length $2
Branch: 2 branches
BranchTrueFalseTotal
/\G(?:(\t)|([^\t]+))/cgu and do { $length += $1 ? $tabsize - $length % $tabsize : length $2 } 20 18 38
$1 ? : 1 19 20
921 while /\G(?:(\t)|([^\t]+))/gc;
922 18 $_ .= ": $length";
923 18 ▶ $maxlen = $length if $maxlen < $length;
Branch: 1 branch
BranchTrueFalseTotal
if $maxlen < $length 7 11 18
924 }
925 18 s/(\d+)\Z/' ' x ($maxlen - $1)/e
926 2 for @label = @row;
927 }
928 7 ▶ FETCH:
Branch: 1 branch
BranchTrueFalseTotal
@row = $sth->fetchrow_array or last 2 5 7
929 @row = $sth->fetchrow_array
930 or last;
931 5 ▶ $filter->( $sth->{NAME}, @row ) or goto FETCH if $filter;
Branch: 1 branch
BranchTrueFalseTotal
if $filter 2 3 5
Condition80%4/5
$filter && (&$filter($sth->{'NAME'}, @row) || (goto FETCH))
!ll&&!rl&&r
320
&$filter($sth->{"NAME"}, @row) or goto FETCH
l!l
11
MC/DC0%0/2
$filter && (&$filter($sth->{'NAME'}, @row) || (goto FETCH))
$filter &$filter($sth->{"NAME"}, @row)
Truth table66%2/3
A && B
A= $filterB= &$filter($sth->{"NAME"}, @row)
ABresult
0X0
100
111
932 }
933 }
934  
935  
936  
937 4 my $lasttime = time;
938 sub run($;$\%) {
939 12 ▶ my( $sql, $filter, $opt ) = @_;
Subroutine run: called CC=8 SCAR=23.9
Pod undocumented
940 12 my $t0 = [gettimeofday];
941 12 ▶ if( $DBI::err || $t0->[0] - $lasttime > 3600 and !$dbh->ping ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DBI::err || $t0->[0] - $lasttime > 3600 and not $SQL::Steno::dbh->ping) 0 12 12
Condition33%2/6
$DBI::err || $t0->[0] - $lasttime > 3600 and not $SQL::Steno::dbh->ping
!ll&&!rl&&r
1200
$DBI::err or $t0->[0] - $lasttime > 3600
l!l&&r!l&&!r
0012
MC/DC0%0/3
$DBI::err || $t0->[0] - $lasttime > 3600 and not $SQL::Steno::dbh->ping
$DBI::err $t0->[0] - $lasttime > 3600 not $SQL::Steno::dbh->ping
Truth table20%1/5
A or B and not(C)
A= $DBI::errB= $t0->[0] - $lasttime > 3600C= not $SQL::Steno::dbh->ping
ABCresult
00X0
1X00
0100
1X11
0111
942 0 printf STDOUT "Inactive for %ds, ping failed after %.03fs, your session variables are lost.\n",
943 $t0->[0] - $lasttime, tv_interval $t0;
944 #$dbh->disconnect;
945 0 $dbh = $dbh->clone; # reconnect
946 0 $t0 = [gettimeofday];
947 }
948 12 $lasttime = $t0->[0];
949 12 ▶ if( my $sth = UNIVERSAL::isa( $sql, 'DBI::st' ) ? $sql : $dbh->prepare( $sql )) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $sth = UNIVERSAL::isa($sql, "DBI::st") ? $sql : $SQL::Steno::dbh->prepare($sql)) 12 0 12
UNIVERSAL::isa($sql, 'DBI::st') ? : 0 12 12
950 12 my $t1 = [gettimeofday];
951 12 $sth->execute;
952 12 printf STDOUT "prepare: %.03fs execute: %.03fs rows: %d\n",
953 tv_interval( $t0, $t1 ), tv_interval( $t1 ), $DBI::rows;
954 12 ▶ if( $sth->{Active} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->{"Active"}) 12 0 12
955 12 ▶ if( $render ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($render) { } 4 8 12
956 4 &$render( $sth, $filter, $opt );
957 } else {
958 8 render_table $sth, $filter, $opt;
959 }
960 }
961 }
962 }
963  
964  
965 =head2 shell
966  
967 This function reads, converts and (if C<$dbh> is set) runs in an end-less loop
968 (i.e. till end of file or C<^D>). Reading is a single line affair, unless you
969 request otherwise. This can happen either, as in Unix Shell, by using
970 continuation lines as long as you put a backslash at the end of your lines.
971 Or there is a special case, if the 1st line starts with C<\\>, then everything
972 up to C<\\> at the end of one of the next lines, constitutes one entry.
973  
974 In addition to converting, it offers a few extra features, performed in this
975 order (i.e. C<&I<xyz>> can return C</I<regexp>/=I<literal sql>> etc.):
976  
977 =head3 C<&{I<Perl code>} I<following text>>
978  
979 Run I<Perl code>. It sees the optional I<following text> in C<$_> and may
980 modify it. If it returns C<undef>, this statement is skipped. If it returns
981 a DBI statement handle, run that instead of this statement. Else replace with
982 what it returns.
983  
984 Reprocess result as a shell entry (i.e. it may return another C<&I<query>>).
985  
986 =head3 C<&I<query arg>; ...> E<nbsp> or E<nbsp> C<&I<query>( I<arg>; ... ) I<following text>>
987  
988 These allow canned entries and are more complex than macros, in that they take
989 arguments and replacement can depend on the argument.
990  
991 Reprocess result as a shell entry (i.e. it may return another C<&I<query>>).
992  
993 You can define your own canned queries with:
994  
995 C< &{ Query I<name> =E<gt> 'I<doc>', 'I<query>' }>
996  
997 Here C<query> becomes the replacement string for C<&name>. It may contain
998 arguments a bit like the Shell: C<$0> (I<name>), C<$*> (all arguments), C<$1,
999 $2, ..., $10, ...> (individual arguments) and C<$E<gt>> (all arguments not
1000 adressed individually). They can become quoted like
1001 L<C<:\>|/spec-strings-or-:-spec-join-strings> as C<:\I<spec>I<arg>> or
1002 C<:\I<spec>%I<join>I<arg>>. Here I<arg> is C<*>, C<E<gt>> or a number
1003 directly tacked on to I<spec> or I<join>. E.g.: C<$\-"1> splits the 1st
1004 (semi-colon separated from the 2nd) argument itself on C<-> (minus), quotes
1005 the pieces with C<"> (double quote) and joins them with C<,> (comma). Putting
1006 the quotes inside the argument like this, eliminates them, if no argument is
1007 given.
1008  
1009 =head3 C</I<regexp>/ I<statement>> E<nbsp> or E<nbsp> C</I<regexp>/i I<statement>> E<nbsp> or E<nbsp> C<!/I<regexp>/ I<statement>> E<nbsp> or E<nbsp> C<!/I<regexp>/i I<statement>>
1010  
1011 This will treat the I<statement> normally, but will join each output row into
1012 a C<~> (tilde) separated string for matching. NULL fields are rendered as
1013 that string. E.g. to return only rows starting with a number 1-5, followed by
1014 a NULL field, you could write: C</^[1-5]~NULL~/>.
1015  
1016 With a suffix C<i>, matching becomes case insensitive. This is why the mostly
1017 optional space before I<statement> is shown above. Without an C<i>, but if
1018 the statement starts with the word C<i> (e.g. your first column name), you
1019 must separate it with a space. With an C<i>, if the statement starts with an
1020 alphanumeric caracter, you must separate it with a space.
1021  
1022 Only matching rows are considered unless there is a preceding C<!>
1023 (exclamation mark), in which case only non-matching rows are considered.
1024  
1025 You can provide your own formatting of the row by setting C<$regexp_fail> to a
1026 Perl sub that returns a Perl expression as a string. That expression takes
1027 the row in C<@_> and shall be true if the row fails to match.
1028  
1029 Caveat: the whole result set of the I<statement> gets generated and
1030 transferred to the client. This is definitely much more expensive than doing
1031 the equivalent filtering in the where clause. But it is not a big deal for
1032 tens or maybe hundreds of thousands or rows, probably still faster than
1033 writing the corresponding SQL. And Perl's regexps are so much more powerful.
1034  
1035 =head3 C<{I<Perl code>}I<statement>>
1036  
1037 Call I<Perl code> for every output row returned by the I<statement> with the
1038 array of column names as zeroth argument and the values after that (i.e.
1039 numbered from 1 like in SQL). It may modify individual values. If it returns
1040 false, the row is skipped.
1041  
1042 You may combine S<C</I<regexp>/{I<Perl code>}>> in any order and as many of them as
1043 you want.
1044  
1045 The same caveat as for regexps applies here. But again Perl is far more
1046 powerful than any SQL functions.
1047  
1048 =head3 C<=I<literal sql>>
1049  
1050 A preceding C<=> prevents conversion, useful for hitherto untreated keywords
1051 or where the conversion doesn't play well with your intention.
1052  
1053 =head3 C<?>
1054  
1055 Help prefix. Alone it will give an overview. You can follow up with any of
1056 the special syntaxes, with or without an abbreviation. E.g. C<?(> will show
1057 all function abbreviations, whereas C<?I<abbrev>(> will show only those functions
1058 matching abbrev or C<?#I<abbrev>> only those tables matching abbrev.
1059  
1060 =head3 C<??I<statement>>
1061  
1062 Will convert and show, but not perform I<statement>. If C<$dbh> is not set, this
1063 is the default behaviour.
1064  
1065 =head3 C<!I<System Shell code>>
1066  
1067 Run I<System Shell code>.
1068  
1069 =head3 C<E<gt>I<filename>> E<nbsp> or E<nbsp> C<E<gt>E<gt>I<filename>>
1070  
1071 Redirect or append next statement's output to I<filename>. For known
1072 suffixes and options, see the L<next section|/Output Formats>.
1073  
1074 =head3 C<|I<System Shell code>>
1075  
1076 Pipe next statement's output through I<System Shell code>.
1077  
1078 =head2 Output Formats
1079  
1080 The output format for the next SQL statement that is run, is chosen from the
1081 suffix of a redirection or a special suffix query. In both cases
1082 comma-separated options may be passed:
1083  
1084 =over
1085  
1086 =item >I<filename>.I<suffix>
1087  
1088 =item >I<filename>.I<suffix>( I<opt>; ... )
1089  
1090 =item >>I<filename>.I<suffix>
1091  
1092 =item >>I<filename>.I<suffix>( I<opt>; ... )
1093  
1094 =item &.I<suffix opt>; ...
1095  
1096 =item &.I<suffix>( I<opt>; ... ) following text
1097  
1098 =back
1099  
1100 The known suffixes and their respective options are:
1101  
1102 =over
1103  
1104 =item C<.csv>
1105  
1106 This writes Comma Separated Values with one subtle trick: NULL and empty
1107 strings are distinguished by quoting the latter. Some tools like Perl's file
1108 DB L<DBD::CSV> or rather its underlying L<Text::CSV_XS> can pick that up. CSV
1109 can take one of these options:
1110  
1111 =over
1112  
1113 =item semi
1114  
1115 Use a semicolon as a separator. This is a common format in environments where
1116 the comma is the decimal separator. However if you want decimal commas, you must
1117 provide such formatting yourself.
1118  
1119 =item tab
1120  
1121 Use tabulators as column separators. Apart from that you get the full CSV
1122 formatting, so this is not the primitive F<.tsv> format some tools may have.
1123  
1124 =back
1125  
1126  
1127 =item C<.table>
1128  
1129 This is the default table format. But you need to name it, if you want to set
1130 options.
1131  
1132 =over
1133  
1134 =item all
1135  
1136 This is a shorthand for outputting everything in the long form, equivalent to
1137 C<( NULL, crlf, date )>.
1138  
1139 =item crlf
1140  
1141 Do not shorten C<\r\n> to C<\R>.
1142  
1143 =item date
1144  
1145 Output ISO dates fully instead of shortening 0000-00-00 to 0000- and
1146 yyyy-01-01 to yyyy- or yyyy-mm-01 to yyyy-mm-.
1147  
1148 =item time
1149  
1150 Output times fully instead of shortening 23:59(:59) to 24: and hh:00(:00) to
1151 hh: or hh:mm(:00) to hh:mm.
1152  
1153 =item NULL
1154  
1155 =item null
1156  
1157 Output this keyword instead of the shorter C<ω> from DB theory (or whatever
1158 you assigned to C<$NULL>).
1159  
1160 =back
1161  
1162  
1163 =item C<.yaml>
1164  
1165 =item C<.yml>
1166  
1167 Format output as YAML. This format has no options. Because its every value
1168 on a new line format can be more readable, there is a shorthand query C<&->
1169 for it.
1170  
1171 =back
1172  
1173 =cut
1174  
1175 4 our $prompt = 'steno> ';
1176 4 our $contprompt = '...> ';
1177 4 our $echo;
1178 # Called for every leading re, 1st arg is the optional '!', 2nd arg '/re/' or '/re/i'. Expression shall be true for non-matching lines.
1179 3 ▶our $regexp_fail = sub($$) { 'join( "~", map ref() ? () : $_ // q!NULL!, @_ )' . ($_[0] ? '=~' : '!~') . $_[1] };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 1 2 3
1180 sub shell() {
1181 1 ▶ print STDERR $prompt;
Subroutine shell: called CC=35 SCAR=42.7
Pod documented
1182 1 my $fh;
1183 1 ▶ while( <> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ARGV)) 19 1 20
1184 19 undef $error;
1185 19 ▶ goto NEXT unless /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/u 2 17 19
1186 17 ▶ if( s/^\s*\\\\\s*// ) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^\s*\\\\\s*//u) { } 1 16 17
1187 1 s/\s*\Z/\n/s;
1188 1 local $/ = "\\\\\n"; # leading \n gets chopped below
1189 1 $_ .= <>;
1190 1 chomp;
1191 } else {
1192 16 ▶ while( s/(?<!\\)((?:\\\\)*)\\(?=\n\Z)/$1/ ) { # join continuation lines
Branch: 1 branch
BranchTrueFalseTotal
if (s/(?<!\\)((?:\\\\)*)\\(?=\n\Z)/$1/u) 1 16 17
1193 1 print STDERR $contprompt;
1194 1 $_ .= <>;
1195 }
1196 16 s/\A\s+//;
1197 }
1198 17 s/\s+\Z//;
1199 17 ▶ say if $echo;
Branch: 1 branch
BranchTrueFalseTotal
if $SQL::Steno::echo 17 0 17
1200 17 ▶ until( $error ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 32 0 32
1201 32 ▶ if( s!^&$perl_re!! ) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^&$SQL::Steno::perl_re//u) { } 2 30 32
1202 2 my $perl = eval $1;
1203 2 local $| = 1; # flush to avoid stderr prompt overtaking last output line.
1204 2 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
1205 2 ▶ if( UNIVERSAL::isa $perl, 'DBI::st' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($perl, 'DBI::st')) { } 0 2 2
elsif (defined $perl) { } 1 1 2
1206 0 $_ = $perl;
1207 0 goto RUN;
1208 } elsif( defined $perl ) {
1209 1 substr $_, 0, 0, $perl;
1210 } else {
1211 1 goto NEXT;
1212 }
1213 } else {
1214 last unless
1215 30 ▶ s!^&(\.?\w+|-)(\(((?:(?>[^()]+)|(?2))*)\))!convert_Query $1, $3!e
Branch: 1 branch
BranchTrueFalseTotal
unless s/^&(\.?\w+|-)(\(((?:(?>[^()]+)|(?2))*)\))/&convert_Query($1, $3);/eu or s/^&(\.?\w+|-) *(.*)/&convert_Query($1, $2);/eu 16 14 30
Condition100%3/3
s/^&(\.?\w+|-)(\(((?:(?>[^()]+)|(?2))*)\))/&convert_Query($1, $3);/eu or s/^&(\.?\w+|-) *(.*)/&convert_Query($1, $2);/eu
l!l&&r!l&&!r
31116
MC/DC100%2/2
s/^&(\.?\w+|-)(\(((?:(?>[^()]+)|(?2))*)\))/&convert_Query($1, $3);/eu or s/^&(\.?\w+|-) *(.*)/&convert_Query($1, $2);/eu
s/^&(\.?\w+|-)(\(((?:(?>[^()]+)|(?2))*)\))/&convert_Query($1, $3);/eu s/^&(\.?\w+|-) *(.*)/&convert_Query($1, $2);/eu
Truth table100%3/3
A or B
A= s/^&(\.?\w+|-)(\(((?:(?>[^()]+)|(?2))*)\))/&convert_Query($1, $3);/euB= s/^&(\.?\w+|-) *(.*)/&convert_Query($1, $2);/eu
ABresult
1X1
011
000
1216 11 or s!^&(\.?\w+|-) *(.*)!convert_Query $1, $2!e;
1217 }
1218 }
1219  
1220 16 my $filter = '';
1221 16 ▶ while( s/^\s*$perl_re// || s%^\s*(!?)(/.+?/(?:i\b)?)\s*%% ) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^\s*$SQL::Steno::perl_re//u or s[^\s*(!?)(/.+?/(?:i\b)?)\s*][]u) 9 16 25
Condition100%3/3
s/^\s*$SQL::Steno::perl_re//u or s[^\s*(!?)(/.+?/(?:i\b)?)\s*][]u
l!l&&r!l&&!r
6316
MC/DC100%2/2
s/^\s*$SQL::Steno::perl_re//u or s[^\s*(!?)(/.+?/(?:i\b)?)\s*][]u
s/^\s*$SQL::Steno::perl_re//u s[^\s*(!?)(/.+?/(?:i\b)?)\s*][]u
Truth table100%3/3
A or B
A= s/^\s*$SQL::Steno::perl_re//uB= s[^\s*(!?)(/.+?/(?:i\b)?)\s*][]u
ABresult
1X1
011
000
1222 9 ▶ if( defined $2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $2) { } 3 6 9
1223 3 $filter .= 'return if ' . $regexp_fail->( $1, $2 ) . ";\n";
1224 } else {
1225 6 $filter .= "return unless eval $1;\n";
1226 }
1227 }
1228 16 ▶ if( $filter ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) 7 9 16
1229 7 $filter = eval "sub {\n$filter 1; }";
1230 7 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
1231 }
1232 16 ▶ goto RUN if s/^\s*=//; # run literally
Branch: 1 branch
BranchTrueFalseTotal
if s/^\s*=//u 5 11 16
1233  
1234 11 my $skip = 0;
1235 11 ▶ if( /^\s*\?\s*(?:([?&#.:])(\w*)|(\w*)\()?/ ) { # help
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*\?\s*(?:([?&#.:])(\w*)|(\w*)\()?/u) 2 9 11
1236 2 ▶ if( $1 && $1 eq '?' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 and $1 eq '?') { } 0 2 2
Condition33%1/3
$1 and $1 eq "?"
!ll&&!rl&&r
020
MC/DC0%0/2
$1 and $1 eq "?"
$1 $1 eq "?"
Truth table33%1/3
A and B
A= $1B= $1 eq "?"
ABresult
0X0
100
111
1237 0 s/^\s*\?\s*\?//;
1238 0 $skip = 1;
1239 } else {
1240 2 help( $1, $2, $3 );
1241 2 goto NEXT;
1242 }
1243 }
1244 9 ▶ if( s/^\s*!// ) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^\s*!//u) 0 9 9
1245 0 system $_;
1246 0 ▶ if( $? == -1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($? == -1) { } 0 0 0
elsif (my $exit = $? & 127) { } 0 0 0
1247 0 print STDERR "failed to execute: $!\n";
1248 } elsif( my $exit = $? & 0b111_1111 ) {
1249 0 ▶ printf STDERR "child died with signal %d, with%s coredump\n",
Branch: 1 branch
BranchTrueFalseTotal
$? & 128 ? : 0 0 0
1250 $exit, ($? & 0b1000_0000) ? '' : 'out';
1251 } else {
1252 0 printf STDERR "child exited with value %d\n", $? >> 8;
1253 }
1254 0 goto NEXT;
1255 }
1256 9 s/^\s*()//; # dummy because $1 survives loop iterations :-o
1257 9 ▶ if( /\A(>{1,2})\s*(.+?(\.\w+)?)(?:\((.*)\))?\s*\Z/ ) { # redirect output
Branch: 2 branches
BranchTrueFalseTotal
if (/\A(>{1,2})\s*(.+?(\.\w+)?)(?:\((.*)\))?\s*\Z/u) { } 0 9 9
elsif (/\A\|(.+)\Z/u) { } 0 9 9
1258 0 ▶ set_render $3, $4 ? split ';', $4 : () if $3;
Branch: 2 branches
BranchTrueFalseTotal
if $3 0 0 0
$4 ? : 0 0 0
1259 0 open $fh, "$1:utf8", (glob $2)[0];
1260 0 select $fh;
1261 0 goto NEXT;
1262 } elsif( /\A\|(.+)\Z/ ) { # pipe output
1263 0 open $fh, '|-:utf8', $1;
1264 0 select $fh;
1265 0 goto NEXT;
1266 }
1267  
1268 9 undef $error;
1269  
1270 9 ▶ goto NEXT unless $_ && &convert;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ and &convert 2 7 9
Condition66%2/3
$_ and &convert
!ll&&!rl&&r
207
MC/DC50%1/2
$_ and &convert
$_ &convert
Truth table66%2/3
A and B
A= $_B= &convert
ABresult
0X0
100
111
1271  
1272 7 print STDOUT "$_;\n";
1273 7 ▶ goto NEXT if $skip;
Branch: 1 branch
BranchTrueFalseTotal
if $skip 0 7 7
1274  
1275 12 ▶ RUN:
Branch: 1 branch
BranchTrueFalseTotal
if $SQL::Steno::dbh 12 0 12
1276 run $_, $filter, %opt if $dbh;
1277 12 ($render, %opt) = ();
1278 12 ▶ if( $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh) 0 12 12
1279 0 close;
1280 0 select STDOUT;
1281 0 undef $fh;
1282 }
1283 NEXT:
1284 19 print STDERR $prompt;
1285 }
1286 1 print STDERR "\n";
1287 }
1288  
1289  
1290  
1291 sub helphashalt(\%@) {
1292 0 ▶ my $hash = shift;
Subroutine helphashalt: not called CC=5 SCAR=34.0
Pod undocumented
1293 0 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
1294 0 my $ret = $hash->{''};
1295 0 print "for *ptr, *cr, *cp, ...:\n";
1296 printf "%-5s %s\n", $_, &$ret( $_ )
1297 0 for @_;
1298 0 print "\n";
1299 }
1300 $_ eq '' or printf "%-5s %s\n", $_, $hash->{$_}
1301 0 ▶ for sort keys %$hash;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "" or printf "%-5s %s\n", $_, $hash->{$_} 0 0 0
1302 }
1303 sub helphash($$$\%;\@) {
1304 #my( $str, $prefix, $suffix, $hash, $list ) = @_;
1305 2 ▶ if( $_[0] ) {
Subroutine helphash: called CC=13 SCAR=47.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) { } 2 0 2
1306 2 undef $error;
1307 2 ▶ $error or printf "%-7s %s\n", "$_[1]$_[0]$_[2]", $_ if $_ = &find;
Branch: 1 branch
BranchTrueFalseTotal
if $_ = &find 2 0 2
Condition33%2/6
($_ = &find) && ($error || printf("%-7s %s\n", "$_[1]$_[0]$_[2]", $_))
!ll&&!rl&&r
020
$error or printf "%-7s %s\n", "$_[1]$_[0]$_[2]", $_
l!l&&r!l&&!r
020
MC/DC0%0/3
($_ = &find) && ($error || printf("%-7s %s\n", "$_[1]$_[0]$_[2]", $_))
($_ = &find) $error printf "%-7s %s\n", "$_[1]$_[0]$_[2]", $_
Truth table0%0/4
A && B or C
A= ($_ = &find)B= $errorC= printf "%-7s %s\n", "$_[1]$_[0]$_[2]", $_
ABCresult
0XX0
1000
11X1
1011
1308 } else {
1309 0 my %hash = %{$_[3]};
1310 0 ▶ if( my $sub = delete $hash{''} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = delete $hash{""}) 0 0 0
1311 0 my @list = $sub->();
1312 0 for my $elt ( @list ) {
1313 0 $hash{$elt->[0]} = $sub->( my $name = $elt->[0] ) . ' ' . $elt->[1];
1314 }
1315 }
1316 0 chomp %hash;
1317 printf "%-7s %s\n", "$_[1]$_$_[2]", $hash{$_}
1318 0 ▶ for sort { lc( $a ) cmp lc( $b ) or $a cmp $b } keys %hash;
Branch: 1 branch
BranchTrueFalseTotal
lc $a cmp lc $b or $a cmp $b 0 0 0
1319 0 ▶ return unless $_[4];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[4] 0 0 0
1320 0 my $i = 0;
1321 0 ▶ my @list = sort { lc( $a ) cmp lc( $b ) or $a cmp $b } @{$_[4]};
Branch: 1 branch
BranchTrueFalseTotal
lc $a cmp lc $b or $a cmp $b 0 0 0
1322 0 ▶ while( @list ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list) 0 0 0
1323 0 ▶ if( ($i += length $list[0]) < 80 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($i += length $list[0]) < 80) { } 0 0 0
1324 0 print ' ', shift @list;
1325 } else {
1326 0 $i = 0;
1327 0 print "\n";
1328 }
1329 }
1330 0 ▶ print "\n" if $i;
Branch: 1 branch
BranchTrueFalseTotal
if $i 0 0 0
1331 }
1332 }
1333  
1334 sub help {
1335 2 ▶ if( defined $_[2] ) {
Subroutine help: called CC=13 SCAR=45.4
Pod undocumented
Branch: 5 branches
BranchTrueFalseTotal
if (defined $_[2]) { } 0 2 2
elsif (not $_[0]) { } 0 2 2
elsif ($_[0] eq '#') { } 0 2 2
elsif ($_[0] eq '.') { } 0 2 2
elsif ($_[0] eq '&') { } 2 0 2
1336 0 helphash $_[2], '', '(', %Functions, @Functions;
1337 } elsif( !$_[0] ) {
1338 0 print <<\HELP;
1339 All entries are single line unless \\wrapped at 1st bol and last eol\\ or continued.\
1340 Queries have the form: {{!}/regexp/{i}}{=}query
1341 The query has lots of short-hands expanded, unless it is prefixed by the optional =.
1342 The fields joined with '~' are grepped if regexp is given, case-insensitively if i is given.
1343  
1344 ??query Only shows massaged query.
1345 !perl-code Runs perl-code.
1346 >file Next query's output to file. In csv or yaml format if filename has that suffix.
1347  
1348 Query has the form {select|update|insert|delete}{fieldlist};tablelist{;clause} or set ...
1349 'select' is prepended if none of these initial keywords.
1350 fieldlist defaults to '*', also if Query starts with '#'.
1351 ';' is alternately replaced by 'from' and 'where'.
1352  
1353 Abbreviations, more help with ?&{abbrev}, ?:{abbrev}, ?#{abbrev}, ?.{abbrev}, ?{abbrev}(
1354 &{Perl code}... # only at bol, if it returns undef then skip, else prepend to ...
1355 &query $1;$2;... # only at bol
1356 &query($1;$2;...)... # only at bol, only replace upto )
1357 #table #table#t
1358 .column .column.c # for any table recognized in statement
1359 function(
1360 :macro
1361 :{Perl code} # dynamic macro
1362  
1363 Characters \t\n\r get masked in output, \r\n as \R.
1364 Date or time 0000-00-00 -> 0000- 1970-01-01 -> 1970- 00:00:00 -> 00: 23:59:59 -> 24:
1365 HELP
1366 } elsif( $_[0] eq '#' ) {
1367 0 ▶ @keys_Table_Columns = keys %Table_Columns unless @keys_Table_Columns;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys_Table_Columns 0 0 0
1368 0 helphash $_[1], '#', '', %Tables, @keys_Table_Columns;
1369 } elsif( $_[0] eq '.' ) {
1370 0 ▶ helphashalt %Columns, 'ptr' unless $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 0 0 0
1371 0 ▶ $error or print "$_\n" if
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] and $_ = &find($_[1], ".", "", \%SQL::Steno::Columns) 0 0 0
Condition0%0/9
$_[1] && ($_ = &find($_[1], '.', '', \%SQL::Steno::Columns)) && ($error || print("$_\n"))
!ll&&!rl&&r
000
$_[1] and $_ = &find($_[1], ".", "", \%SQL::Steno::Columns)
!ll&&!rl&&r
000
$error or print "$_\n"
l!l&&r!l&&!r
000
MC/DC0%0/4
$_[1] && ($_ = &find($_[1], '.', '', \%SQL::Steno::Columns)) && ($error || print("$_\n"))
$_[1] $_ = &find($_[1], ".", "", \%SQL::Steno::Columns) $error print "$_\n"
Truth table0%0/5
A and B && C or D
A= $_[1]B= $_ = &find($_[1], ".", "", \%SQL::Steno::Columns)C= $errorD= print "$_\n"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
1372 $_[1] and $_ = find $_[1], '.', '', %Columns; # \todo, @column;
1373 } elsif( $_[0] eq '&' ) {
1374 2 ▶ print <<\HELP unless $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 0 2 2
1375 &{ Query name => 'doc', 'query' } define query &name on the fly
1376 query may contain arguments a bit like the Shell: $1, $2, ..., $*
1377 they can become quoted: $\1, $\"2, $\`*, $\[3, $\{}>
1378 $* means all args; $> the remaining args after using up the numbered ones
1379 if it is quoted, each arg gets quoted, separated by a comma
1380 $?arg?arg-replacement?no-arg-replacement? 1st if $arg has a value
1381 HELP
1382 2 helphash $_[1], '&', '', %Queries_help;
1383 } else {
1384 0 ▶ print <<\HELP unless $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 0 0 0
1385 :\(...) split arguments and quote in many ways
1386 HELP
1387 0 local $Tables{TBL} = 'TABLE';
1388 0 helphash $_[1], ':', '', %Macros;
1389 }
1390 }
1391  
1392 4 1;
1393  
1394 =head1 YOUR SCRIPT
1395  
1396 package SQL::Steno; # doesn't export yet, so get the functions easily
1397 use SQL::Steno;
1398 use DBI;
1399 our $dbh = DBI->connect( ... ); # preferably mysql, but other DBs should work (with limitations).
1400 # If you want #tbl and .col to work, (only) one of:
1401 init_from_query; # fast, defaults to mysql information_schema, for which you need read permission
1402 init; # slow, using DBI dbh methods.
1403 # Set any of the variables mentioned above to get you favourite abbreviations.
1404 shell;
1405  
1406 =head1 LICENSE
1407  
1408 This program is free software; you may redistribute it and/or modify it under
1409 the same terms as Perl itself.
1410  
1411 =head1 SEE ALSO
1412  
1413 L<DBI>, L<SQL::Interp>, L<SQL::Preproc>, L<SQL::Yapp>, L<Jade|http://jade-lang.com/>
1414  
1415 =head1 AUTHOR
1416  
1417 (C) 2015, 2016 by Daniel Pfeiffer <occitan@esperanto.org>.
↑ Summary