| 1 | =head1 NAME | ||
| 2 | |||
| 3 | SQL::Steno - Short hand for SQL and compact output | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | Type some short-hand, see the corresponding SQL and its output: | ||
| 8 | |||
| 9 | steno> TABLE1;somecolumn > 2 -- ; after tables means where | ||
| 10 | select * from TABLE1 where somecolumn > 2; | ||
| 11 | prepare: 0.000s execute: 0.073s rows: 14 | ||
| 12 | id|column1 |column2 | ||
| 13 | | | |column3 | ||
| 14 | | | | |somecolumn | ||
| 15 | ----|-------------------------------------------|----|-|-| | ||
| 16 | 27|foo | |a|7| | ||
| 17 | 49|bar |abcd|a|3| | ||
| 18 | 81|baz\nbazinga\nbazurka |jk |b|9| | ||
| 19 | 1984|bla bla bla bla bla bla bla bla bla bla bla|xyz |c|5| | ||
| 20 | ... | ||
| 21 | steno> /abc|foo/#TBL1;.socol > 2 -- /regexp/ grep, #tableabbrev, .columnabbrev | ||
| 22 | select * from TABLE1 where somecolumn > 2; | ||
| 23 | prepare: 0.000s execute: 0.039s rows: 14 | ||
| 24 | id|column1 | ||
| 25 | | |column2 | ||
| 26 | | | |[column3=a] | ||
| 27 | | | |somecolumn | ||
| 28 | --|---|----|-| | ||
| 29 | 27|foo| |7| | ||
| 30 | 49|bar|abcd|3| | ||
| 31 | steno> .c1,.c2,.some;#TE1#:ob2d3 -- ; before tables means from, 2nd # alias, :macro | ||
| 32 | select column1,column2,somecolumn from TABLE1 TE1 order by 2 desc, 3; | ||
| 33 | ... | ||
| 34 | steno> n(), yr(), cw(,1,2,3) -- functionabbrev before (, can have initial default arg | ||
| 35 | select count(*), year(now()), concat_ws(',',1,2,3); | ||
| 36 | ... | ||
| 37 | steno> .col1,.clm2,.sn;#TBL1:jTBL2 u(id);mydate :b :m+3d and :d-w -- :jTABLEABBREV and :+/- family | ||
| 38 | select column1,column2,somecolumn from TABLE1 join TABLE2 using(id) where mydate between date_format(now(),"%Y-%m-01")+interval 3 day and curdate()-interval 1 week; | ||
| 39 | ... | ||
| 40 | |||
| 41 | =head1 DESCRIPTION | ||
| 42 | |||
| 43 | You're the command-line type, but are tired of typing C<select * from TABLE | ||
| 44 | where CONDITION>, always forgetting the final C<;>? Output always seems far | ||
| 45 | too wide and at least mysql cli messes up the format when it includes | ||
| 46 | newlines? | ||
| 47 | |||
| 48 | This module consists of the function C<convert> which implements a | ||
| 49 | configurable ultra-compact language that maps to SQL. Then there is C<run> | ||
| 50 | which performs normal SQL queries but has various tricks for narrowing the | ||
| 51 | output. It can also grep on whole rows, rather than having to list all fields | ||
| 52 | that you expect to match. They get combined by the function C<shell> which | ||
| 53 | converts and runs in an endless loop. | ||
| 54 | |||
| 55 | This is work in progress, only recently isolated from a monolithic script. | ||
| 56 | Language elements and API may change as the need arises, e.g. C<:macro> used | ||
| 57 | to be C<@macro>, till the day I wanted to use an SQL-variable and noticed the | ||
| 58 | collision. In this early stage, you are more than welcome to propose | ||
| 59 | ammendments, especially if they make the language more powerful and/or more | ||
| 60 | consistent. Defaults are for MariaDB/MySQL, though the mechanism also works | ||
| 61 | with other DBs. | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | use v5.10; | ||
| 66 | |||
| 67 | package SQL::Steno; | ||
| 68 | our $VERSION = 'v0.1.2'; # quotes because of CPAN indexer bug | ||
| 69 | |||
| 70 | use strict; | ||
| 71 | use Time::HiRes qw(gettimeofday tv_interval); | ||
| 72 | |||
| 73 | binmode STDIN, ':utf8'; | ||
| 74 | binmode STDOUT, ':utf8'; | ||
| 75 | |||
| 76 | our $dbh; | ||
| 77 | our $perl_re = qr/(\{(?:(?>[^{}]+)|(?-1))*\})/; | ||
| 78 | our( %Table_Columns, $table_re ); | ||
| 79 | sub init { | ||
| 80 | die "\$dbh is undef\n" unless $dbh; | ||
| 81 | local @{$dbh}{qw(PrintWarn PrintError RaiseError)} = (0, 0, 0); # \todo is this right? views can barf because more restrictive. | ||
| 82 | for my $table ( @{$dbh->table_info->fetchall_arrayref} ) { | ||
| 83 | $Table_Columns{uc $table->[2]} = []; | ||
| 84 | splice @$table, 3, -1, '%'; | ||
| 85 | my $info = $dbh->column_info( @$table ) or next; | ||
| 86 | for my $column ( @{$info->fetchall_arrayref} ) { | ||
| 87 | push @{$Table_Columns{$table->[2]}}, uc $column->[3]; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | undef $table_re; # (re)create below | ||
| 91 | } | ||
| 92 | our $init_from_query = <<\SQL; | ||
| 93 | select ucase(TABLE_NAME), ucase(COLUMN_NAME) | ||
| 94 | from information_schema.COLUMNS | ||
| 95 | where TABLE_SCHEMA = schema() | ||
| 96 | SQL | ||
| 97 | sub init_from_query { | ||
| 98 | die "\$dbh is undef\n" unless $dbh; | ||
| 99 | local @{$dbh}{qw(PrintWarn PrintError RaiseError)} = (0, 0, 0); # \todo is this right? | ||
| 100 | my $sth = $dbh->prepare( $init_from_query ); | ||
| 101 | $sth->execute; | ||
| 102 | $sth->bind_columns( \my( $table, $column )); | ||
| 103 | push @{$Table_Columns{$table}}, $column while $sth->fetch; | ||
| 104 | undef $table_re; # (re)create below | ||
| 105 | } | ||
| 106 | my @keys_Table_Columns; | ||
| 107 | |||
| 108 | our( %Queries_help, %Queries ); | ||
| 109 | sub Queries(@) { | ||
| 110 | for( @_ ) { | ||
| 111 | $Queries_help{$_->[0]} = $_->[1]; | ||
| 112 | $Queries{$_->[0]} = $_->[2]; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | Queries | ||
| 116 | [ps => ' show processlist (without Sleep)', | ||
| 117 | '/^(?!.+\|Sleep)/=show processlist'], | ||
| 118 | |||
| 119 | [psf => ' show full processlist (without Sleep)', | ||
| 120 | '/^(?!.+\|Sleep)/=show full processlist'], | ||
| 121 | |||
| 122 | |||
| 123 | [s => 'var,value set @var = value', | ||
| 124 | 'set @$1=$2'], | ||
| 125 | |||
| 126 | [ss => 'var,value set @var = "value"', | ||
| 127 | 'set @$1=$"*"'], | ||
| 128 | |||
| 129 | [sd => 'var,value set @var = cast("value" as date)', | ||
| 130 | 'set @$1=cast($"*" as date)'], | ||
| 131 | |||
| 132 | [sdt => 'var,value set @var = cast("value" as datetime)', | ||
| 133 | 'set @$1=cast($"*" as datetime)'], | ||
| 134 | |||
| 135 | [st => 'var,value set @var = cast("value" as time)', | ||
| 136 | 'set @$1=cast($"*" as time)'], | ||
| 137 | |||
| 138 | [sy => ' set @a, @z yesterday is between @a and @z (see :baz)', | ||
| 139 | #'set @a=date(now())-interval 1 day, @z=date(now())-interval 1 second', | ||
| 140 | 'select @a:=date(now()-interval 1 day)`@a`, @z:=date(now())-interval 1 second`@z`']; | ||
| 141 | |||
| 142 | |||
| 143 | our $weekstart = 1; | ||
| 144 | my $timespec_re = qr/[yqmwdhMs]?/; | ||
| 145 | our %Join_clause; | ||
| 146 | our %Macros = | ||
| 147 | ( | ||
| 148 | b => ' between', | ||
| 149 | baz => ' between @a and @z', | ||
| 150 | d => ' distinct', | ||
| 151 | h => ' having', | ||
| 152 | j => ' join', | ||
| 153 | l => ' like', | ||
| 154 | lj => ' left join', | ||
| 155 | n => ' is null', | ||
| 156 | nb => ' not between', | ||
| 157 | nc => ' sql_no_cache', | ||
| 158 | nl => ' not like', | ||
| 159 | nn => ' is not null', | ||
| 160 | nr => ' not rlike', | ||
| 161 | od => ' on duplicate key update', | ||
| 162 | odku => ' on duplicate key update', | ||
| 163 | r => ' rlike', | ||
| 164 | u => ' union select', | ||
| 165 | ua => ' union all select', | ||
| 166 | wr => ' with rollup', | ||
| 167 | '' => sub { | ||
| 168 | my $join = 'for all #TBL matching TABLE'; | ||
| 169 | my $int = 'see :+ :- :y-m :q+0 :d+2h'; | ||
| 170 | my $gob = 'for 0 or more digits, optionally followed by a or d'; | ||
| 171 | return ([jTBL => $join], ['jTBL#' => $join], [ljTBL => $join], [1 => 'for all numbers'], | ||
| 172 | [gb147 => $gob], [ob2d5a9 => $gob], | ||
| 173 | ['+' => <<INT], ['-' => $int], ['d+2h' => $int], ['y-m' => $int], ['q+0' => $int]) | ||
| 174 | :B+/-NO this B(ase) +/- N(umber, 0 for none, default 1 if O given) O(ffset) | ||
| 175 | optional B, O is y(ear), q(uarter), m(onth), w(eek), d(ay), h(our), M(inute), s(econd) | ||
| 176 | INT | ||
| 177 | unless @_; # help | ||
| 178 | for( $_[0] ) { | ||
| 179 | return " limit $_" if /^\d+$/; | ||
| 180 | if( s/^([og])b(?=(?:\d[ad]?)*$)/ $1 eq 'g' ? ' group by ' : ' order by ' /e ) { | ||
| 181 | s/(?<! )(?=\d)/, /g; | ||
| 182 | s/a/ asc/g; s/(?<!r)d/ desc/g; | ||
| 183 | return $_; | ||
| 184 | } | ||
| 185 | if( s/^(l?)j/#/ ) { # (l)jtbl: j or lj with any #tbl | ||
| 186 | my $left = $1 ? ' left' : ''; | ||
| 187 | &convert_table_column; | ||
| 188 | /^(\w+)/; | ||
| 189 | return "$left join $_" . ($Join_clause{$1} || $Join_clause{''} || ''); | ||
| 190 | } | ||
| 191 | return $_ if | ||
| 192 | s(^($timespec_re)([+-])(\d*)($timespec_re)$) { | ||
| 193 | ({ y => ' date_format(now(),"%Y-01-01")', | ||
| 194 | q => ' date_format(now()-interval mod(month(now())+11,3) month,"%Y-%m-01")', | ||
| 195 | m => ' date_format(now(),"%Y-%m-01")', | ||
| 196 | w => ' curdate()-interval weekday(now())' . ($weekstart ? ' day' : '+1 day'), | ||
| 197 | d => ' curdate()', | ||
| 198 | h => ' date_format(now(),"%F %H:00")', | ||
| 199 | M => ' date_format(now(),"%F %H:%M")', | ||
| 200 | s => ' now()' }->{$1} || '') . | ||
| 201 | ($3 ne '0' && | ||
| 202 | "$2interval" . | ||
| 203 | ($3 ? " $3" : $4 ? ' 1' : '') . | ||
| 204 | ({ y => ' year', | ||
| 205 | q => ' quarter', | ||
| 206 | m => ' month', | ||
| 207 | w => ' week', | ||
| 208 | d => ' day', | ||
| 209 | h => ' hour', | ||
| 210 | M => ' minute', | ||
| 211 | s => ' second' }->{$4} || '')) | ||
| 212 | }eo; | ||
| 213 | } | ||
| 214 | }); | ||
| 215 | |||
| 216 | # \todo default arg n() -> n(*) time*(now()) | ||
| 217 | our %Functions = | ||
| 218 | ( | ||
| 219 | c => 'concat', | ||
| 220 | cw => 'concat_ws', | ||
| 221 | coa => 'coalesce', | ||
| 222 | gc => 'group_concat', | ||
| 223 | i => 'in', # not really fn, but ( follows | ||
| 224 | in => 'ifnull', | ||
| 225 | l => 'char_length', | ||
| 226 | lc => 'lcase', | ||
| 227 | m => 'min', | ||
| 228 | M => 'max', | ||
| 229 | n => 'count', | ||
| 230 | ni => 'not in', # -"- | ||
| 231 | s => 'substring', | ||
| 232 | u => 'using', # -"- | ||
| 233 | uc => 'ucase' | ||
| 234 | ); | ||
| 235 | |||
| 236 | # functions where the 1st argument can be now() | ||
| 237 | my @nowFunctions = qw( | ||
| 238 | adddate addtime convert_tz date date_add date_format date_sub datediff day | ||
| 239 | dayname dayofmonth dayofweek dayofyear hour last_day minute month | ||
| 240 | monthname quarter second subdate subtime time time_format time_to_sec | ||
| 241 | timediff timestamp to_days to_seconds week weekday weekofyear year | ||
| 242 | yearweek | ||
| 243 | ); | ||
| 244 | our @Functions = sort @nowFunctions, qw( | ||
| 245 | abs acos aes_decrypt aes_encrypt ascii asin atan avg benchmark bin bit_and | ||
| 246 | bit_count bit_length bit_or bit_xor cast ceiling char_length char | ||
| 247 | character_length charset coalesce coercibility collation compress | ||
| 248 | concat_ws concat connection_id conv cos cot count crc32 curdate | ||
| 249 | current_date current_time current_timestamp current_user curtime database | ||
| 250 | decode default degrees des_decrypt des_encrypt elt encode encrypt exp | ||
| 251 | export_set field find_in_set floor format found_rows from_days | ||
| 252 | from_unixtime get_format get_lock greatest group_concat hex if ifnull | ||
| 253 | inet_aton inet_ntoa insert instr interval is_free_lock is_used_lock isnull | ||
| 254 | last_insert_id lcase least left length ln load_file localtime | ||
| 255 | localtimestamp locate log10 log2 log lower lpad ltrim make_set makedate | ||
| 256 | maketime master_pos_wait max md5 microsecond mid min mod name_const now | ||
| 257 | nullif oct octet_length old_password ord password period_add period_diff | ||
| 258 | pi position power quote radians rand release_lock repeat replace reverse | ||
| 259 | right round row_count rpad rtrim schema sec_to_time session_user sha1 sign | ||
| 260 | sin sleep soundex space sqrt stddev stddev_pop stddev_samp str_to_date | ||
| 261 | strcmp substring_index substring sum sysdate system_user tan timestampadd | ||
| 262 | timestampdiff trim truncate ucase uncompress uncompressed_length unhex | ||
| 263 | unix_timestamp upper user utc_date utc_time utc_timestamp uuid values | ||
| 264 | var_pop var_samp variance | ||
| 265 | ); | ||
| 266 | |||
| 267 | our %DefaultArguments = ( | ||
| 268 | count => '*', | ||
| 269 | concat_ws => "','" | ||
| 270 | ); | ||
| 271 | $DefaultArguments{$_} = 'now()' for @nowFunctions; | ||
| 272 | |||
| 273 | |||
| 274 | our %Tables; | ||
| 275 | our %Columns; | ||
| 276 | |||
| 277 | sub regexp($$) { | ||
| 278 | my( $str, $type ) = @_; | ||
| 279 | if( $type < 2 ) { | ||
| 280 | return if $str !~ /_/; # Otherwise same as find sprintf cases | ||
| 281 | return ($type ? '' : '^') . join '.*?_', split /_/, $str; # 0 & 1 | ||
| 282 | } | ||
| 283 | my $expr = join '.*?', split //, $str; # 2, 3 & 4 | ||
| 284 | if( $type < 4 ) { | ||
| 285 | substr $expr, 0, 0, '^'; # 2 & 3 | ||
| 286 | $expr .= '$' if $type == 2; # 2 | ||
| 287 | } | ||
| 288 | $expr; | ||
| 289 | } | ||
| 290 | |||
| 291 | my $error; | ||
| 292 | my @simple = qw(^%s$ ^%s_ ^%s _%s$ _%s %s$ %s_ %s); | ||
| 293 | sub find($$$\%;\@) { | ||
| 294 | my( $str, $prefix, $suffix, $hash, $list ) = @_; | ||
| 295 | my $ret = $hash->{$str}; | ||
| 296 | return $ret if $ret; | ||
| 297 | |||
| 298 | $ret = $hash->{''}; | ||
| 299 | $ret = &$ret( $str ) if $ret; | ||
| 300 | return $ret if $ret; | ||
| 301 | |||
| 302 | if( $list ) { | ||
| 303 | for my $type ( 0..@simple+4 ) { # Try to find a more and more fuzzy match. | ||
| 304 | my $expr = $type < @simple ? | ||
| 305 | sprintf $simple[$type], $str : | ||
| 306 | regexp $str, $type - @simple; | ||
| 307 | next unless defined $expr; | ||
| 308 | my @res = grep /$expr/i, @$list; | ||
| 309 | if( @res ) { | ||
| 310 | return $res[0] if @res == 1; | ||
| 311 | warn "$prefix$str$suffix matches @res\n"; | ||
| 312 | $error = 1; | ||
| 313 | return ''; | ||
| 314 | } | ||
| 315 | } | ||
| 316 | } | ||
| 317 | # no special syntax for fields or functions, so don't fail on real one | ||
| 318 | return $str if ord $prefix == ord '.' or ord $suffix == ord '('; | ||
| 319 | |||
| 320 | warn "$prefix$str$suffix doesn't match\n"; | ||
| 321 | $error = 1; | ||
| 322 | } | ||
| 323 | |||
| 324 | sub convert_Queries($$) { | ||
| 325 | my $res = find $_[0], '&', '', %Queries; | ||
| 326 | local $_ = $_[1]; | ||
| 327 | &convert_table_column; | ||
| 328 | my @arg = split ','; | ||
| 329 | my @rest; | ||
| 330 | for my $i ( 1..@arg ) { | ||
| 331 | $res =~ s/\$$i/$arg[$i - 1]/g or $res =~ s/\$"$i"/"$arg[$i - 1]"/g or push @rest, $arg[$i - 1]; | ||
| 332 | } | ||
| 333 | $res =~ s!\$\?(.*?)\?(.*?)\?!@rest ? $1 : $2!e; | ||
| 334 | $res =~ s!\$"\*"!'"' . join( '","', @rest ) . '"'!ge or | ||
| 335 | $res =~ s!\$\*!join( ',', @rest )!ge; | ||
| 336 | $res; | ||
| 337 | } | ||
| 338 | |||
| 339 | sub convert_table_column { | ||
| 340 | # \todo .col. or .col.alias like #tbl# | ||
| 341 | @keys_Table_Columns = keys %Table_Columns unless @keys_Table_Columns; | ||
| 342 | s&(?<!\\)#(\w+)(?:#(\w*))?&find( $1, '#', '', %Tables, @keys_Table_Columns ) . ($2 ? " $2" : defined $2 ? " $1" : '')&eg unless $error; | ||
| 343 | |||
| 344 | unless( $table_re ) { | ||
| 345 | $table_re = join '|', keys %Table_Columns; | ||
| 346 | $table_re = $table_re ? qr/\b(?:$table_re)\b/ : qr/\s\b\s/; | ||
| 347 | } | ||
| 348 | unless( $error ) { | ||
| 349 | my %column; | ||
| 350 | for( grep /$table_re/io, split /\W+/ ) { | ||
| 351 | undef $column{$_} for @{$Table_Columns{$_}}; | ||
| 352 | } | ||
| 353 | my @column = keys %column; | ||
| 354 | s/(^|[-+\s(,;&|])?(?<!\\)\.([a-z]\w*)(?:\.(\w*))?/(defined $1 ? $1 : '.') . (find $2, '.', '', %Columns, @column) . ($3 ? " $3" : defined $3 ? " $2" : '')/egi; | ||
| 355 | } | ||
| 356 | } | ||
| 357 | |||
| 358 | |||
| 359 | =head2 convert | ||
| 360 | |||
| 361 | This function takes a short-hand query in C<$_> and transforms it to SQL. See | ||
| 362 | L</shell> for more run time oriented features. | ||
| 363 | |||
| 364 | First it looks for C<:macro>. These are mostly simple text-replacements | ||
| 365 | stored in C<%Macros>. There are also some dynamic macros. Those starting | ||
| 366 | with C<:j> (join) or C<:lj> (left join) may continue into a table spec without | ||
| 367 | the leading C<#>. E.g. C<:ljtbl#t> might expand to C<left join table t>. | ||
| 368 | |||
| 369 | Those starting with C<:gb> (group by) or C<:ob> (order by) may be followed by | ||
| 370 | result columns numbers from 1-9, each optionally followed by a or d for asc or | ||
| 371 | desc. | ||
| 372 | |||
| 373 | Then there are the time macros, where an optional leading letter indicates a | ||
| 374 | base time, and an optional trailing letter with an optional count means the | ||
| 375 | offset. The letters are: | ||
| 376 | |||
| 377 | =over | ||
| 378 | |||
| 379 | =item y | ||
| 380 | |||
| 381 | (this) year. E.g. C<:y+2m> is march this year. | ||
| 382 | |||
| 383 | =item q | ||
| 384 | |||
| 385 | (this) quarter. E.g. C<:q+0> is this quarter, C<:q+q> is next quarter. | ||
| 386 | |||
| 387 | =item m | ||
| 388 | |||
| 389 | (this) month. E.g. C<:-3m> is whatever precedes, minus 3 months. | ||
| 390 | |||
| 391 | =item w | ||
| 392 | |||
| 393 | (this) week (starting on C<$weekstart>). E.g. C<:w+3d> is this week thursday (or wednesday). | ||
| 394 | |||
| 395 | =item d | ||
| 396 | |||
| 397 | (this) day. E.g. C<:d-w> is midnight one week ago. | ||
| 398 | |||
| 399 | =item h | ||
| 400 | |||
| 401 | (this) hour. E.g. C<:h+30M> is half past current hour. | ||
| 402 | |||
| 403 | =item M | ||
| 404 | |||
| 405 | (this) minute. E.g. C<:+10M> is whatever precedes, plus 10min. | ||
| 406 | |||
| 407 | =item s | ||
| 408 | |||
| 409 | (this) second. E.g. C<:s-2h> is exactly 2h ago. | ||
| 410 | |||
| 411 | =back | ||
| 412 | |||
| 413 | Then it looks for C<#tbl>, C<#tbl#> or C<#tbl#alias>. Here tbl is a key of | ||
| 414 | C<%Tables> or any abbreviation of known tables in C<@Tables>. If followed by | ||
| 415 | C<#>, the abbreviation is used as an alias, unless an alias directly follows, | ||
| 416 | in which case that is used. | ||
| 417 | |||
| 418 | Then it looks for C<.col>, C<.col.> C<.col.alias>. Here col is a key of | ||
| 419 | C<%Columns> or any abbreviation of columns of any table recognized in the | ||
| 420 | query. If followed by C<.>, the abbreviation is used as an alias, unless an | ||
| 421 | alias directly follows, in which case that is used. It tries to be clever | ||
| 422 | about whether the 1st C<.> needs to be preserved, i.e. following a table name. | ||
| 423 | |||
| 424 | Finally it looks for C<func(>. Here func is a key of C<%Functions> or any | ||
| 425 | abbreviation of known functions in C<@Functions>, which includes words | ||
| 426 | typically followed by an opening parenthesis, such as C<u(> for C<using(>. | ||
| 427 | C<i(> is C<in(>, so that C<in(> is free for matching C<ifnull(>, but more | ||
| 428 | importantly, because it has a smart brother: C<I(> is also C<in(>, where | ||
| 429 | quoting is applied as necessary, and unless you give an initial comma C<I(,>, | ||
| 430 | the elements are separated on space. E.g. C<I(a 3 a's q"q)> and | ||
| 431 | C<I(,a,3,a's,q"q)> both give C<in("a",3,"a's",'q"q')>. If the 1st argument of | ||
| 432 | a function is empty and the abbrev or function is found in | ||
| 433 | C<%DefaultArguments> the value becomes the 1st argument. | ||
| 434 | |||
| 435 | |||
| 436 | Finally it picks on the structure of the statement: These keywords can be | ||
| 437 | abbreviated: C<se(lect)>, C<ins(ert)>, C<upd(ate)> or C<del(ete)>. If none of | ||
| 438 | these or C<set> is present, C<select> is assumed as default (more keywords | ||
| 439 | need to be recognized in the future). | ||
| 440 | |||
| 441 | For C<select>, semicolons are alternately replaced by C<from> (the 1st being | ||
| 442 | optional if it starts with a table name) and C<where>. If no result columns | ||
| 443 | are given, they default to C<*>, see L</SYNOPSIS>. For C<update>, semicolons | ||
| 444 | are frst replaced by C<set> and then C<where>. | ||
| 445 | |||
| 446 | =cut | ||
| 447 | |||
| 448 | sub convert { | ||
| 449 | # Handle I(str1 str2 str3) | ||
| 450 | s<\bI\((, ?)?(.+?)\)> { 'i('.join(',', map { /"/ ? "'$_'" : /^\d+$/ ? $_ : qq+"$_"+ } split $1||' +', $2, -1).')' }ge; | ||
| 451 | |||
| 452 | my @strings; # extract strings to prevent following replacements inside. | ||
| 453 | while( /\G.*?(['"])/gc ) { | ||
| 454 | my $quote = $1; | ||
| 455 | my $pos = pos; | ||
| 456 | while( /\G.*?([\\$quote])/gc ) { | ||
| 457 | if( $1 eq '\\' ) { | ||
| 458 | ++pos; # skip next | ||
| 459 | } elsif( ! /\G$quote/gc ) { # skip double quote | ||
| 460 | push @strings, substr $_, $pos - 1, 1 - $pos + pos, # get string | ||
| 461 | "\cA".@strings."\cB"; # and replace with counter | ||
| 462 | last; | ||
| 463 | } | ||
| 464 | } | ||
| 465 | } | ||
| 466 | |||
| 467 | until( $error ) { | ||
| 468 | s&:$perl_re&my $ret = eval $1; warn $@ if $@; $ret // ' NULL '&ego or | ||
| 469 | # \todo (?(?<=\w)\b) | ||
| 470 | s&:($timespec_re[+-]\d*$timespec_re(?(?<=\w)\b)|l?j\w+(?:#(\w*))|\w+)&find $1, ':', '', %Macros&ego or | ||
| 471 | last; | ||
| 472 | } | ||
| 473 | |||
| 474 | s&^(?=#)&;&; # Assume empty fieldlist before table name | ||
| 475 | &convert_table_column; | ||
| 476 | s&^(?=$table_re)&;&; # Assume empty fieldlist before table name | ||
| 477 | |||
| 478 | s&\b(\w+)\((?=\s*([,)])?)&my $fn = find $1, '', '(', %Functions, @Functions; ($fn || $1).'('.($2 and $DefaultArguments{$1} || $DefaultArguments{$fn} or '')&eg unless $error; | ||
| 479 | #s&\b(\w+)(?=\()&find $1, '', '(', %Functions, @Functions or $1&eg unless $error; | ||
| 480 | |||
| 481 | return if $error; | ||
| 482 | s/\A\s*;/*;/; | ||
| 483 | s/;\s*\Z//; | ||
| 484 | if( s/^upd(?:a(?:t(?:e)?)?)?\b/update/i ) { | ||
| 485 | s/(?<!\\);(?:\s*set\s*)?/ set / && s/(?<!\\);(?:\s*where\s*)?/ where /; | ||
| 486 | } else { | ||
| 487 | s/(?<!\\);(?:\s*where\s*)?/ where / while s/(?<!\\);(?:\s*from\s*)?/ from /; | ||
| 488 | s/^ins(?:e(?:r(?:t)?)?)?\b/insert/i || | ||
| 489 | s/^del(?:e(?:t(?:e)?)?)?\b/delete/i || | ||
| 490 | s/^(?!se(?:lec)?t)/select /i; | ||
| 491 | } | ||
| 492 | |||
| 493 | s/ {2,}/ /g; | ||
| 494 | s/\cA(\d+)\cB/$strings[$1]/g; # put back the strings | ||
| 495 | |||
| 496 | 1; | ||
| 497 | } | ||
| 498 | |||
| 499 | |||
| 500 | my %ctrl = | ||
| 501 | ("\t", '\t', | ||
| 502 | "\n", '\n', | ||
| 503 | "\r", '\r'); | ||
| 504 | my $lasttime = time; | ||
| 505 | |||
| 506 | sub run($;$$$$) { | ||
| 507 | my( $sql, $re, $sub, $fh, $csv ) = @_; | ||
| 508 | my $t0 = [gettimeofday]; | ||
| 509 | if( $DBI::err || $t0->[0] - $lasttime > 3600 and !$dbh->ping ) { | ||
| 510 | printf STDOUT "Inactive for %ds, ping failed after %.03fs, your session variables are lost.\n", | ||
| 511 | $t0->[0] - $lasttime, tv_interval $t0; | ||
| 512 | #$dbh->disconnect; | ||
| 513 | $dbh = $dbh->clone; # reconnect | ||
| 514 | $t0 = [gettimeofday]; | ||
| 515 | } | ||
| 516 | $lasttime = $t0->[0]; | ||
| 517 | if( my $sth = UNIVERSAL::isa( $sql, 'DBI::st' ) ? $sql : $dbh->prepare( $sql )) { | ||
| 518 | my $t1 = [gettimeofday]; | ||
| 519 | $sth->execute; | ||
| 520 | # \todo Steno time, optionally #rows grepped | ||
| 521 | printf STDOUT "prepare: %.03fs execute: %.03fs rows: %d\n", | ||
| 522 | tv_interval( $t0, $t1 ), tv_interval( $t1 ), $DBI::rows; | ||
| 523 | if( $sth->{Active} ) { | ||
| 524 | my @name = @{$sth->{NAME}}; | ||
| 525 | print join( ',', @name ) . "\n" if $csv; | ||
| 526 | my @len = (1) x @name; | ||
| 527 | my( @txt, @res, @comp ); | ||
| 528 | while( my @res1 = $sth->fetchrow_array ) { | ||
| 529 | next if $sub && !$sub->( \@res1, \@name ); | ||
| 530 | next if $re && join( '|', map $_ // '\@', @res1 ) !~ $re; | ||
| 531 | if( $csv ) { | ||
| 532 | for( @res1 ) { | ||
| 533 | $_ //= ''; | ||
| 534 | $_ = qq!"$_"! if | ||
| 535 | s/"/""/g or | ||
| 536 | tr/,\n// or | ||
| 537 | /\A=/; | ||
| 538 | utf8::decode $_; | ||
| 539 | } | ||
| 540 | print join( ',', @res1 ) . "\n"; | ||
| 541 | next; | ||
| 542 | } else { | ||
| 543 | for my $i ( 0..$#res1 ) { | ||
| 544 | if( !defined $res1[$i] ) { | ||
| 545 | $res1[$i] = '\@'; | ||
| 546 | } elsif( $res1[$i] !~ /^\d+(?:\.\d+)?$/ ) { | ||
| 547 | $txt[$i] = 1; | ||
| 548 | $res1[$i] =~ s/\r\n/\\R/g; | ||
| 549 | $res1[$i] =~ s/([\t\n\r])/$ctrl{$1}/g; | ||
| 550 | no warnings 'uninitialized'; | ||
| 551 | $res1[$i] =~ s/^(?:(0000-)00-00|(1970-)01-01)(?:( 00:)00:00)?$/$1$2$3/ or | ||
| 552 | $res1[$i] =~ s/^(\d{4}-\d\d-\d\d )?(?:00:00:00|23:59:5(9))$/$1 . ($2 ? '24:' : '00:')/e or | ||
| 553 | $res1[$i] =~ s/^((\d{4}-\d\d-\d\d )?\d\d:\d\d):00$/$1/; | ||
| 554 | utf8::decode $res1[$i]; | ||
| 555 | } | ||
| 556 | $txt[$i] = 0 if @txt < $i; | ||
| 557 | my $len = length $res1[$i]; | ||
| 558 | $len[$i] = $len if $len[$i] < $len; | ||
| 559 | } | ||
| 560 | if( @comp ) { | ||
| 561 | for my $i ( 0..$#comp ) { | ||
| 562 | undef $comp[$i] if defined $comp[$i] && $comp[$i] ne $res1[$i]; | ||
| 563 | } | ||
| 564 | } else { | ||
| 565 | @comp = @res1; | ||
| 566 | } | ||
| 567 | push @res, \@res1; | ||
| 568 | } | ||
| 569 | } | ||
| 570 | if( @res ) { | ||
| 571 | @comp = () if @res == 1; | ||
| 572 | my $fmt = ''; | ||
| 573 | for( my $i = 0; $i < @name; ++$i ) { | ||
| 574 | $name[$i] =~ s/\r\n/\\R/g; | ||
| 575 | $name[$i] =~ s/([\t\n\r])/$ctrl{$1}/g; | ||
| 576 | if( defined $comp[$i] ) { | ||
| 577 | my $more; | ||
| 578 | while( defined $comp[$i] ) { | ||
| 579 | printf $fmt, @name[0..$i-1] unless $more; | ||
| 580 | $more = 1; | ||
| 581 | printf "[%s=%s]", $name[$i], $comp[$i]; | ||
| 582 | @name[0..$i] = ('') x ($i+1); | ||
| 583 | for my $row ( \@comp, \@name, \@len, \@txt, @res ) { | ||
| 584 | splice @$row, $i, 1; | ||
| 585 | } | ||
| 586 | } | ||
| 587 | print "\n"; | ||
| 588 | --$i, next; | ||
| 589 | } | ||
| 590 | if( $len[$i] < length $name[$i] ) { | ||
| 591 | printf "$fmt%s\n", @name[0..$i]; | ||
| 592 | @name[0..$i] = ('') x ($i+1); | ||
| 593 | } | ||
| 594 | $fmt .= '%' . ($txt[$i] ? -$len[$i] : $len[$i]) . 's|'; | ||
| 595 | } | ||
| 596 | $fmt .= "\n"; | ||
| 597 | printf $fmt, @name if $name[-1]; | ||
| 598 | printf $fmt, map '-'x$_, @len; | ||
| 599 | my $cnt = 0; | ||
| 600 | my $i = 100; | ||
| 601 | for my $row ( @res ) { | ||
| 602 | printf $fmt, @$row; | ||
| 603 | ++$cnt; | ||
| 604 | if( !$fh && --$i <= 0 && $cnt < @res ) { | ||
| 605 | printf STDERR "How many more, or * for all? (%d of %d) [default: 100] ", | ||
| 606 | $cnt, scalar @res; | ||
| 607 | $i = <>; | ||
| 608 | if( defined $i ) { | ||
| 609 | $i =~ tr/ \t\n\r//d; | ||
| 610 | $i = (0 == length $i) ? 100 : | ||
| 611 | $i eq '*' ? ~0 : | ||
| 612 | $i == 0 ? last : | ||
| 613 | $i; | ||
| 614 | } else { | ||
| 615 | print "\n"; | ||
| 616 | last; | ||
| 617 | } | ||
| 618 | } | ||
| 619 | } | ||
| 620 | } | ||
| 621 | } | ||
| 622 | } | ||
| 623 | } | ||
| 624 | |||
| 625 | |||
| 626 | =head2 shell | ||
| 627 | |||
| 628 | This function reads, converts and (if C<$dbh> is set) runs in an end-less loop | ||
| 629 | (i.e. till end of file or C<^D>). Reading is a single line affair, unless you | ||
| 630 | request otherwise. This can happen either, as in Unix Shell, by using | ||
| 631 | continuation lines as long as you put a backslash at the end of your lines. | ||
| 632 | Or there is a special case, if the 1st line starts with C<\\>, then everything | ||
| 633 | up to C<\\> at the end of one of the next lines, constitutes one entry. | ||
| 634 | |||
| 635 | In addition to converting it offers a few extra features, performed in this | ||
| 636 | order (i.e. C<&xyz> can return C</regexp/=literal sql> etc.): | ||
| 637 | |||
| 638 | =over | ||
| 639 | |||
| 640 | =item &query arg, ... | ||
| 641 | |||
| 642 | =item &query( arg, ... ) following text | ||
| 643 | |||
| 644 | =item &{Perl code} following text | ||
| 645 | |||
| 646 | These allow canned entries and are more complex than macros, in that they take | ||
| 647 | arguments and replacement can depend on the argument. | ||
| 648 | |||
| 649 | Run I<Perl code>. It sees the I<following text> in C<$_> and may modify it. | ||
| 650 | If it returns undef this statement is skipped. If it returns a DBI statement | ||
| 651 | handle run that instead of this statement. Else replace with what it returns. | ||
| 652 | |||
| 653 | Reprocess result as a shell entry (i.e. it may return another C<&>query). | ||
| 654 | |||
| 655 | =item {Perl code}statement | ||
| 656 | |||
| 657 | Call I<Perl code> for every output row returned by the I<statement> with the | ||
| 658 | array of values as first and the array of column names as second argument. It | ||
| 659 | may modify individual values, but not the array. If it returns false, the row | ||
| 660 | is skipped. | ||
| 661 | |||
| 662 | =item /regexp/statement | ||
| 663 | |||
| 664 | This will treat the I<statement> normally, but will join each output row with | ||
| 665 | '|' characters (which need to be matched as C<\/>) and check that against the | ||
| 666 | regexp. Only matching rows are considered. You may combine S<C<{Perl | ||
| 667 | code}/regexp/>>. | ||
| 668 | |||
| 669 | =item =literal sql | ||
| 670 | |||
| 671 | A preceding C<=> prevents conversion, useful for hitherto untreated keywords | ||
| 672 | or where the conversion doesn't play well with your intention. | ||
| 673 | |||
| 674 | =item ? | ||
| 675 | |||
| 676 | Help prefix. Alone it will give an overview. You can follow up with any of | ||
| 677 | the special syntaxes, with or without an abbreviation. E.g. C<?(> will show | ||
| 678 | all function abbreviations, whereas C<?abbrev(> will show only those functions | ||
| 679 | matching abbrev or C<?#abbrev> only those tables matching abbrev. | ||
| 680 | |||
| 681 | =item ??statemment | ||
| 682 | |||
| 683 | Will convert and show, but not perform statement. If C<$dbh> is not set, this | ||
| 684 | is the default behaviour. | ||
| 685 | |||
| 686 | =item !System Shell code | ||
| 687 | |||
| 688 | Run it. | ||
| 689 | |||
| 690 | =item >filename | ||
| 691 | |||
| 692 | =item >>filename | ||
| 693 | |||
| 694 | Redirect or append next statement's output to I<filename>. If it has a suffix | ||
| 695 | C<.csv>, an according format is used. | ||
| 696 | |||
| 697 | =item |System Shell code | ||
| 698 | |||
| 699 | Pipe next statement's output through I<System Shell code>. | ||
| 700 | |||
| 701 | =back | ||
| 702 | |||
| 703 | =cut | ||
| 704 | |||
| 705 | our $prompt = 'steno> '; | ||
| 706 | sub shell() { | ||
| 707 | print STDERR $prompt; | ||
| 708 | my( $fh, $csv ); | ||
| 709 | while( <> ) { | ||
| 710 | undef $error; | ||
| 711 | goto NEXT unless /\S/; | ||
| 712 | if( s/^\s*\\\\\s*// ) { | ||
| 713 | s/\s*\Z/\n/s; | ||
| 714 | local $/ = "\\\\\n"; # leading \n gets chopped below | ||
| 715 | $_ .= <>; | ||
| 716 | chomp; | ||
| 717 | } else { | ||
| 718 | while( s/(?<!\\)((?:\\\\)*)\\(?=\n\Z)/$1/ ) { # join continuation lines | ||
| 719 | print STDERR '...> '; | ||
| 720 | $_ .= <>; | ||
| 721 | } | ||
| 722 | s/\A\s+//; | ||
| 723 | } | ||
| 724 | s/\s+\Z//; | ||
| 725 | |||
| 726 | until( $error ) { | ||
| 727 | last unless | ||
| 728 | s!^\&(\w+)(\(((?:(?>[^()]+)|(?2))*)\))!convert_Queries $1, $3!e | ||
| 729 | or s!^\&(\w+) *(.*)!convert_Queries $1, $2!e | ||
| 730 | or my $perl = s!^\&$perl_re!!; | ||
| 731 | if( $perl ) { | ||
| 732 | $perl = eval $1; | ||
| 733 | local $| = 1; # flush to avoid stderr prompt overtaking last output line. | ||
| 734 | warn $@ if $@; | ||
| 735 | if( UNIVERSAL::isa $perl, 'DBI::st' ) { | ||
| 736 | $_ = $perl; | ||
| 737 | goto RUN; | ||
| 738 | } elsif( defined $perl ) { | ||
| 739 | substr $_, 0, 0, $perl; | ||
| 740 | } else { | ||
| 741 | goto NEXT; | ||
| 742 | } | ||
| 743 | } | ||
| 744 | } | ||
| 745 | |||
| 746 | my $sub; | ||
| 747 | if( s/^\s*$perl_re// ) { | ||
| 748 | $sub = eval "sub $1"; | ||
| 749 | warn $@ if $@; | ||
| 750 | } | ||
| 751 | my $re = $1 ? qr/$2/i : qr/$2/ | ||
| 752 | if s!^(i?)/(.+?)/\s*!!; | ||
| 753 | goto RUN if s/^\s*=//; # run literally | ||
| 754 | |||
| 755 | my $skip = 0; | ||
| 756 | if( /^\s*\?\s*(?:([?&#.:])(\w*)|(\w*)\()?/ ) { # help | ||
| 757 | if( $1 && $1 eq '?' ) { | ||
| 758 | s/^\s*\?\s*\?//; | ||
| 759 | $skip = 1; | ||
| 760 | } else { | ||
| 761 | help( $1, $2, $3 ); | ||
| 762 | goto NEXT; | ||
| 763 | } | ||
| 764 | } | ||
| 765 | if( s/^\s*!// ) { | ||
| 766 | system $_; | ||
| 767 | if( $? == -1 ) { | ||
| 768 | print STDERR "failed to execute: $!\n"; | ||
| 769 | } elsif( my $exit = $? & 0b111_1111 ) { | ||
| 770 | printf STDERR "child died with signal %d, with%s coredump\n", | ||
| 771 | $exit, ($? & 0b1000_0000) ? '' : 'out'; | ||
| 772 | } else { | ||
| 773 | printf STDERR "child exited with value %d %d\n", $?, $? >> 8; | ||
| 774 | } | ||
| 775 | goto NEXT; | ||
| 776 | } | ||
| 777 | s/^\s*()//; # dummy because $1 survives loop iterations :-o | ||
| 778 | if( /\A(>{1,2})\s*(.+?(\.csv)?)\s*\Z/ ) { # redirect output | ||
| 779 | $csv = 1 if $3; | ||
| 780 | open $fh, "$1:utf8", (glob $2)[0]; | ||
| 781 | select $fh; | ||
| 782 | goto NEXT; | ||
| 783 | } elsif( /\A\|(.+)\Z/ ) { # pipe output | ||
| 784 | open $fh, '|-:utf8', $1; | ||
| 785 | select $fh; | ||
| 786 | goto NEXT; | ||
| 787 | } | ||
| 788 | |||
| 789 | undef $error; | ||
| 790 | |||
| 791 | goto NEXT unless $_ && &convert; | ||
| 792 | |||
| 793 | print STDOUT "$_;\n"; | ||
| 794 | goto NEXT if $skip; | ||
| 795 | |||
| 796 | RUN: | ||
| 797 | run $_, $re, $sub, $fh, $csv if $dbh; | ||
| 798 | if( $fh ) { | ||
| 799 | close; | ||
| 800 | select STDOUT; | ||
| 801 | undef $fh; | ||
| 802 | undef $csv; | ||
| 803 | } | ||
| 804 | NEXT: | ||
| 805 | print STDERR $prompt; | ||
| 806 | } | ||
| 807 | print STDERR "\n"; | ||
| 808 | } | ||
| 809 | |||
| 810 | |||
| 811 | |||
| 812 | sub helphashalt(\%@) { | ||
| 813 | my $hash = shift; | ||
| 814 | if( @_ ) { | ||
| 815 | my $ret = $hash->{''}; | ||
| 816 | print "for *ptr, *cr, *cp, ...:\n"; | ||
| 817 | printf "%-5s %s\n", $_, &$ret( $_ ) | ||
| 818 | for @_; | ||
| 819 | print "\n"; | ||
| 820 | } | ||
| 821 | $_ eq '' or printf "%-5s %s\n", $_, $hash->{$_} | ||
| 822 | for sort keys %$hash; | ||
| 823 | } | ||
| 824 | sub helphash($$$\%;\@) { | ||
| 825 | #my( $str, $prefix, $suffix, $hash, $list ) = @_; | ||
| 826 | if( $_[0] ) { | ||
| 827 | undef $error; | ||
| 828 | $error or printf "%-7s %s\n", "$_[1]$_[0]$_[2]", $_ if $_ = &find; | ||
| 829 | } else { | ||
| 830 | my %hash = %{$_[3]}; | ||
| 831 | if( my $sub = delete $hash{''} ) { | ||
| 832 | my @list = $sub->(); | ||
| 833 | for my $elt ( @list ) { | ||
| 834 | $hash{$elt->[0]} = $sub->( my $name = $elt->[0] ) . ' ' . $elt->[1]; | ||
| 835 | } | ||
| 836 | } | ||
| 837 | chomp %hash; | ||
| 838 | printf "%-7s %s\n", "$_[1]$_$_[2]", $hash{$_} | ||
| 839 | for sort { lc( $a ) cmp lc( $b ) or $a cmp $b } keys %hash; | ||
| 840 | return unless $_[4]; | ||
| 841 | my $i = 0; | ||
| 842 | my @list = sort { lc( $a ) cmp lc( $b ) or $a cmp $b } @{$_[4]}; | ||
| 843 | while( @list ) { | ||
| 844 | if( ($i += length $list[0]) < 80 ) { | ||
| 845 | print ' ', shift @list; | ||
| 846 | } else { | ||
| 847 | $i = 0; | ||
| 848 | print "\n"; | ||
| 849 | } | ||
| 850 | } | ||
| 851 | print "\n" if $i; | ||
| 852 | } | ||
| 853 | } | ||
| 854 | |||
| 855 | sub help { | ||
| 856 | if( defined $_[2] ) { | ||
| 857 | local $Functions{I} = 'in but quotes strings itself, splits on space, or with beginning if I(, ... or I(,...'; | ||
| 858 | helphash $_[2], '', '(', %Functions, @Functions; | ||
| 859 | } elsif( !$_[0] ) { | ||
| 860 | print <<\HELP; | ||
| 861 | All entries are single line unless \\wrapped at 1st bol and last eol\\ or continued.\ | ||
| 862 | Queries have the form: {{i}/regexp/}{=}query | ||
| 863 | The query has lots of short-hands expanded, unless it is prefixed by the optional =. | ||
| 864 | The fields joined with '|' are grepped if regexp is given, case-insensitively if i is given. | ||
| 865 | |||
| 866 | ??query Only shows massaged query. | ||
| 867 | !perl-code Runs perl-code. | ||
| 868 | >file Next query's output to file. In csv format if filename has .csv suffix. | ||
| 869 | |||
| 870 | Query has the form {select|update|insert|delete}{fieldlist};tablelist{;clause} or set ... | ||
| 871 | 'select' is prepended if none of these initial keywords. | ||
| 872 | fieldlist defaults to '*', also if Query starts with '#'. | ||
| 873 | ';' is alternately replaced by 'from' and 'where' except '\;'. | ||
| 874 | |||
| 875 | Abbreviations, more help with ?&{abbrev}, ?#{abbrev}, ?.{abbrev}, ?{abbrev}(, ?:{abbrev} | ||
| 876 | &query $1,$2,... # only at bol | ||
| 877 | &query($1,$2,...)... # only at bol, only replace upto ) | ||
| 878 | #table | ||
| 879 | .column # for any table recognized in statement | ||
| 880 | function( | ||
| 881 | :macro | ||
| 882 | |||
| 883 | Characters \t\n\r get masked in output, \r\n as \R. | ||
| 884 | Date or time 0000-00-00 -> 0000- 1970-01-01 -> 1970- 00:00:00 -> 00: 23:59:59 -> 24: | ||
| 885 | HELP | ||
| 886 | } elsif( $_[0] eq '#' ) { | ||
| 887 | @keys_Table_Columns = keys %Table_Columns unless @keys_Table_Columns; | ||
| 888 | helphash $_[1], '#', '', %Tables, @keys_Table_Columns; | ||
| 889 | } elsif( $_[0] eq '.' ) { | ||
| 890 | helphashalt %Columns, 'ptr' unless $_[1]; | ||
| 891 | $error or print "$_\n" if | ||
| 892 | $_[1] and $_ = find $_[1], '.', '', %Columns; # \todo, @column; | ||
| 893 | } elsif( $_[0] eq '&' ) { | ||
| 894 | helphash $_[1], '&', '', %Queries_help; | ||
| 895 | } else { | ||
| 896 | local $Tables{TBL} = 'TABLE'; | ||
| 897 | helphash $_[1], ':', '', %Macros; | ||
| 898 | } | ||
| 899 | } | ||
| 900 | |||
| 901 | =head1 YOUR SCRIPT | ||
| 902 | |||
| 903 | package SQL::Steno; # doesn't export yet, so get the functions easily | ||
| 904 | use SQL::Steno; | ||
| 905 | use DBI; | ||
| 906 | our $sql = DBI->connect( ... ); # preferably mysql, but other DBs should work (with limitations). | ||
| 907 | # If you want #tbl and .col to work, (only) one of: | ||
| 908 | init_from_query; # fast, defaults to mysql information_schema, for which you need read permission | ||
| 909 | init; # slow, using DBI dbh methods. | ||
| 910 | # Set any of the variables mentioned above to get you favourite abbreviations. | ||
| 911 | shell; | ||
| 912 | |||
| 913 | =head1 LICENSE | ||
| 914 | |||
| 915 | This program is free software; you may redistribute it and/or modify it under | ||
| 916 | the same terms as Perl itself. | ||
| 917 | |||
| 918 | =head1 SEE ALSO | ||
| 919 | |||
| 920 | L<DBI>, L<SQL::Interp>, L<SQL::Preproc>, L<SQL::Yapp>, L<Jade|http://jade-lang.com/> | ||
| 921 | |||
| 922 | =head1 AUTHOR | ||
| 923 | |||
| 924 | (C) 2015 by Daniel Pfeiffer <occitan@esperanto.org>. |