| 1 | |||
| 2 | # #################### | ||
| 3 | # Parsing SQL commands | ||
| 4 | |||
| 5 | package XBase::SQL::Expr; | ||
| 6 | package XBase::SQL; | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use vars qw( $VERSION %COMMANDS ); | ||
| 10 | |||
| 11 | $VERSION = '1.06'; | ||
| 12 | |||
| 13 | # ################################# | ||
| 14 | # Type conversions for create table | ||
| 15 | my %TYPES = ( 'char' => 'C', 'varchar' => 'C', | ||
| 16 | 'num' => 'N', 'numeric' => 'N', 'int' => 'N', 'decimal' => 'N', | ||
| 17 | 'integer' => 'N', 'float' => 'F', 'boolean' => 'L', | ||
| 18 | 'blob' => 'M', 'memo' => 'M', 'date' => 'D', | ||
| 19 | 'time' => 'T', 'datetime' => 'T', | ||
| 20 | 'money' => 'Y' ); | ||
| 21 | |||
| 22 | # ################## | ||
| 23 | # Regexp definitions | ||
| 24 | |||
| 25 | %COMMANDS = ( | ||
| 26 | |||
| 27 | # Top level SQL commands | ||
| 28 | |||
| 29 | 'COMMANDS' => ' ( SELECT | INSERT | DELETE | UPDATE | CREATE | DROP ) [\\s|;]* ', | ||
| 30 | 'SELECT' => 'select ( SELECTALL | SELECTFIELDS ) from TABLE WHERE ? | ||
| 31 | ORDERBY ?', | ||
| 32 | 'INSERT' => 'insert into TABLE ( \( INSERTFIELDS \) ) ? values | ||
| 33 | \( INSERTCONSTANTS \)', | ||
| 34 | 'DELETE' => 'delete from TABLE WHERE ?', | ||
| 35 | 'UPDATE' => 'update TABLE set SETCOLUMNS WHERE ?', | ||
| 36 | 'CREATE' => 'create table TABLE \( COLUMNDEF ( , COLUMNDEF ) * \)', | ||
| 37 | 'DROP' => 'drop table TABLE', | ||
| 38 | |||
| 39 | # select fields | ||
| 40 | |||
| 41 | 'SELECTFIELDS' => 'SELECTFIELD ( , SELECTFIELD ) *', | ||
| 42 | 'SELECTFIELD' => 'SELECTEXPFIELD ( as ? FIELDNAMENOTFROM SELECTFIELDNAME ) ? ', | ||
| 43 | 'SELECTALL' => q'\*', | ||
| 44 | 'SELECTEXPFIELD' => 'ARITHMETIC', | ||
| 45 | 'FIELDNAMENOTFROM' => '(?!from)(?=\w)|(?=from\s+from\b)', | ||
| 46 | 'SELECTFIELDNAME' => 'STRING | [a-z_][a-z0-9_]*', | ||
| 47 | |||
| 48 | # insert definitions | ||
| 49 | |||
| 50 | 'INSERTFIELDS' => 'INSERTFIELDNAME ( , INSERTFIELDNAME ) *', | ||
| 51 | 'INSERTFIELDNAME' => 'FIELDNAME', | ||
| 52 | 'INSERTCONSTANTS' => 'CONSTANT ( , CONSTANT ) *', | ||
| 53 | |||
| 54 | # update definitions | ||
| 55 | |||
| 56 | 'SETCOLUMNS' => 'SETCOLUMN ( , SETCOLUMN ) *', | ||
| 57 | 'SETCOLUMN' => 'UPDATEFIELDNAME = UPDATEARITHMETIC', | ||
| 58 | 'UPDATEFIELDNAME' => 'FIELDNAME', | ||
| 59 | 'UPDATEARITHMETIC' => 'ARITHMETIC', | ||
| 60 | |||
| 61 | # create definitions | ||
| 62 | |||
| 63 | 'COLUMNDEF' => 'COLUMNKEY | COLUMNNAMETYPE ( not null ) ?', | ||
| 64 | 'COLUMNKEY' => 'primary key \( FIELDNAME \)', | ||
| 65 | 'COLUMNNAMETYPE' => 'FIELDNAME FIELDTYPE', | ||
| 66 | 'FIELDTYPE' => 'TYPECHAR | TYPENUM | TYPEBOOLEAN | TYPEMEMO | TYPEDATE | money ', | ||
| 67 | |||
| 68 | 'TYPECHAR' => ' ( varchar | char ) ( \( TYPELENGTH \) ) ?', | ||
| 69 | 'TYPENUM' => '( num | numeric | decimal | float | int | integer ) ( \( TYPELENGTH ( , TYPEDEC ) ? \) ) ?', | ||
| 70 | 'TYPEDEC' => '\d+', | ||
| 71 | |||
| 72 | 'TYPELENGTH' => '\d+', | ||
| 73 | 'TYPEBOOLEAN' => 'boolean | logical', | ||
| 74 | 'TYPEMEMO' => 'memo | blob', | ||
| 75 | 'TYPEDATE' => 'date | time | datetime', | ||
| 76 | |||
| 77 | # table, field name, number, string | ||
| 78 | |||
| 79 | 'TABLE' => '[^\s\(]+', | ||
| 80 | 'FIELDNAME' => '[a-z_][a-z0-9_.]*', | ||
| 81 | 'NUMBER' => q'-?\d*\.?\d+', | ||
| 82 | 'STRING' => q! \\" STRINGDBL \\" | \\' STRINGSGL \\' !, | ||
| 83 | 'STRINGDBL' => q' STRINGDBLPART ( \\\\. STRINGDBLPART ) * ', | ||
| 84 | 'STRINGSGL' => q' STRINGSGLPART ( \\\\. STRINGSGLPART ) * ', | ||
| 85 | 'STRINGDBLPART' => q' [^\\\\"]* ', | ||
| 86 | 'STRINGSGLPART' => q! [^\\\\']* !, | ||
| 87 | |||
| 88 | # where clause | ||
| 89 | |||
| 90 | 'WHERE' => 'where WHEREEXPR', | ||
| 91 | 'WHEREEXPR' => 'BOOLEAN', | ||
| 92 | |||
| 93 | 'BOOLEAN' => q'not BOOLEAN | ( \( BOOLEAN \) | RELATION ) ( ( AND | OR ) BOOLEAN ) *', | ||
| 94 | 'RELATION' => 'ARITHMETIC ( is not ? null | LIKE CONSTANT_NOT_NULL | RELOP ARITHMETIC )', | ||
| 95 | 'AND' => 'and', | ||
| 96 | 'OR' => 'or', | ||
| 97 | |||
| 98 | 'RELOP' => [ qw{ == | = | <= | >= | <> | != | < | > } ], | ||
| 99 | 'LIKE' => 'not ? like', | ||
| 100 | |||
| 101 | 'ARITHMETIC' => [ qw{ ( \( ARITHMETIC \) | ||
| 102 | | CONSTANT | FUNCTION | EXPFIELDNAME ) | ||
| 103 | ( ( \+ | \- | \* | \/ | \% | CONCATENATION ) ARITHMETIC ) ? } ], | ||
| 104 | 'EXPFIELDNAME' => 'FIELDNAME', | ||
| 105 | 'CONCATENATION' => '\|\|', | ||
| 106 | |||
| 107 | |||
| 108 | 'CONSTANT' => ' CONSTANT_NOT_NULL | NULL ', | ||
| 109 | 'CONSTANT_NOT_NULL' => ' BINDPARAM | NUMBER | STRING ', | ||
| 110 | 'BINDPARAM' => q'\? | : [a-z0-9]* ', | ||
| 111 | 'NULL' => 'null', | ||
| 112 | |||
| 113 | 'ARITHMETICLIST' => ' ARITHMETIC ( , ARITHMETICLIST ) * ', | ||
| 114 | 'FUNCTION' => ' FUNCTION1 | FUNCTION23 | FUNCTIONANY ', | ||
| 115 | 'FUNCTION1' => ' ( length | trim | ltrim | rtrim ) \( ARITHMETIC \) ', | ||
| 116 | 'FUNCTION23' => ' ( substr | substring ) \( ARITHMETIC , ARITHMETIC ( , ARITHMETIC ) ? \) ', | ||
| 117 | 'FUNCTIONANY' => ' concat \( ARITHMETICLIST \) ', | ||
| 118 | |||
| 119 | 'ORDERBY' => 'order by ORDERFIELDNAME ORDERDESC ? | ||
| 120 | ( , ORDERFIELDNAME ORDERDESC ? ) *', | ||
| 121 | 'ORDERDESC' => 'asc | desc', | ||
| 122 | 'ORDERFIELDNAME' => 'FIELDNAME', | ||
| 123 | ); | ||
| 124 | |||
| 125 | # ##################################### | ||
| 126 | # "Expected" messages for various types | ||
| 127 | my %ERRORS = ( | ||
| 128 | 'COMMANDS' => 'Unknown SQL command', | ||
| 129 | 'TABLE' => 'Table name expected', | ||
| 130 | 'RELATION' => 'Relation expected', | ||
| 131 | 'ARITHMETIC' => 'Arithmetic expression expected', | ||
| 132 | 'from' => 'From specification expected', | ||
| 133 | 'into' => 'Into specification expected', | ||
| 134 | 'values' => 'Values specification expected', | ||
| 135 | '\\(' => 'Left paren expected', | ||
| 136 | '\\)' => 'Right paren expected', | ||
| 137 | '\\*' => 'Star expected', | ||
| 138 | '\\"' => 'Double quote expected', | ||
| 139 | "\\'" => 'Single quote expected', | ||
| 140 | 'STRING' => 'String expected', | ||
| 141 | 'SELECTFIELDS' => 'Columns to select expected', | ||
| 142 | 'FIELDTYPE' => 'Field type expected', | ||
| 143 | ); | ||
| 144 | |||
| 145 | |||
| 146 | # ######### | ||
| 147 | # Callbacks to be called after everything is nicely matched | ||
| 148 | my %STORE = ( | ||
| 149 | 'SELECT' => sub { shift->{'command'} = 'select'; }, | ||
| 150 | 'SELECTALL' => sub { | ||
| 151 | my $self = shift; | ||
| 152 | $self->{'selectall'} = '*'; | ||
| 153 | $self->{'selectfn'} = sub { my ($TABLE, $VALUES, $BIND) = @_; map { XBase::SQL::Expr->field($_, $TABLE, $VALUES)->value } $TABLE->field_names; }; | ||
| 154 | undef; | ||
| 155 | }, | ||
| 156 | 'SELECTEXPFIELD' => 'fields', | ||
| 157 | 'SELECTFIELDS' => sub { | ||
| 158 | my $self = shift; | ||
| 159 | my $select_fn = 'sub { my ($TABLE, $VALUES, $BIND) = @_; map { $_->value } (' . join(', ', @{$self->{'fields'}} ) . ')}'; | ||
| 160 | ### print "Selectfn: $select_fn\n"; | ||
| 161 | my $fn = eval $select_fn; | ||
| 162 | if ($@) { $self->{'selecterror'} = $@; } | ||
| 163 | else { $self->{'selectfn'} = $fn; } | ||
| 164 | $self->{'selectfieldscount'} = scalar(@{$self->{'fields'}}); | ||
| 165 | undef; | ||
| 166 | }, | ||
| 167 | 'SELECTFIELDNAME' => sub { | ||
| 168 | my $self = shift; | ||
| 169 | my $fieldnum = @{$self->{'fields'}} - 1; | ||
| 170 | my $name = (get_strings(@_))[0]; | ||
| 171 | $self->{'selectnames'}[$fieldnum] = $name; | ||
| 172 | undef; | ||
| 173 | }, | ||
| 174 | |||
| 175 | 'INSERT' => sub { shift->{'command'} = 'insert'; }, | ||
| 176 | 'INSERTFIELDNAME' => 'insertfields', | ||
| 177 | 'INSERTCONSTANTS' => sub { my $self = shift; | ||
| 178 | my $insert_fn = 'sub { my ($TABLE, $BIND) = @_; map { | ||
| 179 | $_->value() } ' . join(' ', get_strings(@_)) . ' }'; | ||
| 180 | my $fn = eval $insert_fn; | ||
| 181 | ### print STDERR "Evalling insert_fn: $insert_fn\n"; | ||
| 182 | if ($@) { $self->{'inserterror'} = $@; } | ||
| 183 | else { $self->{'insertfn'} = $fn; } | ||
| 184 | undef; | ||
| 185 | }, | ||
| 186 | 'INSERTFIELDS' => sub { my ($self, @fields) = @_; | ||
| 187 | while (@fields) { push @{$self->{'fields'}}, shift @fields; shift @fields; }}, | ||
| 188 | |||
| 189 | 'DELETE' => sub { shift->{'command'} = 'delete'; }, | ||
| 190 | |||
| 191 | 'UPDATE' => sub { shift->{'command'} = 'update'; }, | ||
| 192 | 'UPDATEFIELDNAME' => 'updatefields', | ||
| 193 | 'UPDATEARITHMETIC' => 'updateexprs', | ||
| 194 | 'SETCOLUMNS' => sub { my $self = shift; | ||
| 195 | my $list = join ', ', @{$self->{'updateexprs'}};; | ||
| 196 | my $update_fn = 'sub { my ($TABLE, $VALUES, $BIND) = @_; map { $_->value() } (' . $list . ') }'; | ||
| 197 | my $fn = eval $update_fn; | ||
| 198 | ### print STDERR "Evalling update_fn: $update_fn\n"; | ||
| 199 | if ($@) { $self->{'updateerror'} = $@; } | ||
| 200 | else { $self->{'updatefn'} = $fn; } | ||
| 201 | undef; | ||
| 202 | }, | ||
| 203 | |||
| 204 | |||
| 205 | 'CREATE' => sub { shift->{'command'} = 'create'; }, | ||
| 206 | 'COLUMNNAMETYPE' => sub { my $self = shift; | ||
| 207 | my @results = get_strings(@_); | ||
| 208 | push @{$self->{'createfields'}}, $results[0]; | ||
| 209 | push @{$self->{'createtypes'}}, $TYPES{lc $results[1]}; | ||
| 210 | push @{$self->{'createlengths'}}, $results[3]; | ||
| 211 | push @{$self->{'createdecimals'}}, $results[5]; }, | ||
| 212 | |||
| 213 | 'DROP' => sub { shift->{'command'} = 'drop'; }, | ||
| 214 | |||
| 215 | 'TABLE' => sub { | ||
| 216 | my $self = shift; | ||
| 217 | my $table = (get_strings(@_))[0]; | ||
| 218 | push @{$self->{'table'}}, $table; | ||
| 219 | $table; | ||
| 220 | }, | ||
| 221 | |||
| 222 | |||
| 223 | 'FIELDNAME' => sub { | ||
| 224 | my $self = shift; | ||
| 225 | my $field = uc ((get_strings(@_))[0]); | ||
| 226 | $field =~ s/^.*\.//; | ||
| 227 | push @{$self->{'usedfields'}}, $field; | ||
| 228 | $field; | ||
| 229 | }, | ||
| 230 | 'EXPFIELDNAME' => sub { | ||
| 231 | my $self = shift; | ||
| 232 | my $e = (get_strings(@_))[0]; | ||
| 233 | "XBase::SQL::Expr->field('$e', \$TABLE, \$VALUES)"; | ||
| 234 | }, | ||
| 235 | |||
| 236 | 'BINDPARAM' => sub { | ||
| 237 | my $self = shift; | ||
| 238 | my $string = join '', get_strings(@_); | ||
| 239 | |||
| 240 | my $bindcount = keys %{$self->{'binds'}}; | ||
| 241 | $bindcount = 0 unless defined $bindcount; | ||
| 242 | |||
| 243 | if ($string eq '?') { | ||
| 244 | $string = ':p'.($bindcount+1); | ||
| 245 | } | ||
| 246 | $self->{'binds_order'}[$bindcount] = $string | ||
| 247 | unless exists $self->{'binds'}{$string}; | ||
| 248 | |||
| 249 | $self->{'binds'}{$string}++; | ||
| 250 | "XBase::SQL::Expr->string(\$BIND->{'$string'})"; | ||
| 251 | }, | ||
| 252 | |||
| 253 | 'FUNCTION' => sub { | ||
| 254 | my $self = shift; | ||
| 255 | my @params = get_strings(@_); | ||
| 256 | my $fn = uc shift @params; | ||
| 257 | "XBase::SQL::Expr->function('$fn', \$TABLE, \$VALUES, @params)"; | ||
| 258 | }, | ||
| 259 | |||
| 260 | 'ORDERFIELDNAME' => 'orderfields', | ||
| 261 | 'ORDERDESC' => 'orderdescs', | ||
| 262 | |||
| 263 | 'STRINGDBL' => sub { | ||
| 264 | my $self = shift; | ||
| 265 | join '', '"', get_strings(@_), '"'; | ||
| 266 | }, | ||
| 267 | 'STRINGSGL' => sub { | ||
| 268 | my $self = shift; | ||
| 269 | join '', '\'', get_strings(@_), '\''; | ||
| 270 | }, | ||
| 271 | 'STRING' => sub { | ||
| 272 | shift; | ||
| 273 | my $e = (get_strings(@_))[1]; | ||
| 274 | "XBase::SQL::Expr->string($e)"; | ||
| 275 | }, | ||
| 276 | 'NUMBER' => sub { | ||
| 277 | shift; | ||
| 278 | my $e = (get_strings(@_))[0]; | ||
| 279 | "XBase::SQL::Expr->number($e)"; | ||
| 280 | }, | ||
| 281 | 'NULL' => sub { 'XBase::SQL::Expr->null()' }, | ||
| 282 | 'AND' => sub { 'and' }, | ||
| 283 | 'OR' => sub { 'or' }, | ||
| 284 | 'LIKE' => sub { shift; join ' ', get_strings(@_); }, | ||
| 285 | 'CONCATENATION' => sub { ' . ' }, | ||
| 286 | |||
| 287 | |||
| 288 | |||
| 289 | |||
| 290 | 'WHEREEXPR' => sub { my $self = shift; | ||
| 291 | my $expr = join ' ', get_strings(@_); | ||
| 292 | ### print STDERR "Evalling: $expr\n"; | ||
| 293 | ### use Data::Dumper; | ||
| 294 | my $fn = eval ' | ||
| 295 | sub { | ||
| 296 | ### print Dumper @_; | ||
| 297 | my ($TABLE, $VALUES, $BIND) = @_; ' . $expr . '; }'; | ||
| 298 | if ($@) { $self->{'whereerror'} = $@; } | ||
| 299 | else { $self->{'wherefn'} = $fn; } | ||
| 300 | ''; | ||
| 301 | }, | ||
| 302 | |||
| 303 | |||
| 304 | |||
| 305 | 'RELOP' => sub { shift; my $e = (get_strings(@_))[0]; | ||
| 306 | if ($e eq '=') { $e = '=='; } | ||
| 307 | elsif ($e eq '<>') { $e = '!=';} $e; }, | ||
| 308 | 'ARITHMETIC' => sub { shift; join ' ', get_strings(@_); }, | ||
| 309 | 'RELATION' => sub { shift; my @values = get_strings(@_); | ||
| 310 | local $^W = 0; | ||
| 311 | my $testnull = join ' ', @values[1 .. 3]; | ||
| 312 | if ($testnull =~ /^is (not )?null ?$/i) | ||
| 313 | { return "not $1 defined(($values[0])->value)"; } | ||
| 314 | elsif ($values[1] =~ /^(not )?like$/i) | ||
| 315 | { return "$1(XBase::SQL::Expr->likematch($values[0], $values[2])) " } | ||
| 316 | else { return join ' ', @values; } }, | ||
| 317 | |||
| 318 | ); | ||
| 319 | |||
| 320 | sub find_verbatim_select_names { | ||
| 321 | my ($self, @result) = @_; | ||
| 322 | my $i = 0; | ||
| 323 | while ($i < @result) { | ||
| 324 | if ($result[$i] eq 'SELECTEXPFIELD') { | ||
| 325 | my @out = $self->get_verbatim_select_names(@result[$i, $i + 1]); | ||
| 326 | push @{$self->{'selectnames'}}, uc join '', @out; | ||
| 327 | } | ||
| 328 | elsif (ref $result[$i + 1] eq 'ARRAY') { | ||
| 329 | $self->find_verbatim_select_names(@{$result[$i + 1]}); | ||
| 330 | } | ||
| 331 | $i += 2; | ||
| 332 | } | ||
| 333 | } | ||
| 334 | sub get_verbatim_select_names { | ||
| 335 | my ($self, @result) = @_; | ||
| 336 | my $i = 1; | ||
| 337 | my @out = (); | ||
| 338 | while ($i < @result) { | ||
| 339 | if (ref $result[$i] eq 'ARRAY') { | ||
| 340 | push @out, $self->get_verbatim_select_names(@{$result[$i]}); | ||
| 341 | } else { | ||
| 342 | push @out, $result[$i]; | ||
| 343 | } | ||
| 344 | $i += 2; | ||
| 345 | } | ||
| 346 | @out; | ||
| 347 | } | ||
| 348 | |||
| 349 | ####### | ||
| 350 | |||
| 351 | # Parse is called with a string -- the whole SQL query. It should | ||
| 352 | # return the object with all properties filled, or errstr upon error | ||
| 353 | |||
| 354 | # First, we call match. Then, after we know that the match was | ||
| 355 | # successfull, we call store_results | ||
| 356 | sub parse { | ||
| 357 | $^W = 0; | ||
| 358 | my ($class, $string) = @_; | ||
| 359 | my $self = bless {}, $class; | ||
| 360 | |||
| 361 | # try to match the $string against $COMMANDS{'COMMANDS'} | ||
| 362 | # that's the top level starting point | ||
| 363 | my ($srest, $error, $errstr, @result) = match($string, 'COMMANDS'); | ||
| 364 | |||
| 365 | # after the parse, nothing should have left from the $string | ||
| 366 | # if it does, it's some rubbish | ||
| 367 | if ($srest ne '' and not $error) { | ||
| 368 | $error = 1; | ||
| 369 | $errstr = 'Extra characters in SQL command'; | ||
| 370 | } | ||
| 371 | |||
| 372 | # we want to have meaningfull error messages. if it heasn't | ||
| 373 | # been specified so far, let's just say Error | ||
| 374 | if ($error) { | ||
| 375 | if (not defined $errstr) { | ||
| 376 | $errstr = 'Error in SQL command'; | ||
| 377 | } | ||
| 378 | |||
| 379 | # and only show the relevant part of the SQL string | ||
| 380 | substr($srest, 40) = '...' if length $srest > 44; | ||
| 381 | if ($srest ne '') { | ||
| 382 | $self->{'errstr'} = "$errstr near `$srest'"; | ||
| 383 | } else { | ||
| 384 | $self->{'errstr'} = "$errstr at the end of query"; | ||
| 385 | } | ||
| 386 | } else { | ||
| 387 | # take the results and store them to $self | ||
| 388 | |||
| 389 | $self->find_verbatim_select_names(@result); | ||
| 390 | $self->store_results(\@result, \%STORE); | ||
| 391 | if (defined $self->{'whereerror'}) { | ||
| 392 | $self->{'errstr'} = "Some deeper problem: eval failed: $self->{'whereerror'}"; | ||
| 393 | } | ||
| 394 | ### use Data::Dumper; print STDERR "Parsed $string to\n", Dumper $self if $ENV{'SQL_DUMPER'}; | ||
| 395 | } | ||
| 396 | $self; | ||
| 397 | } | ||
| 398 | |||
| 399 | ########## | ||
| 400 | |||
| 401 | # Function match is called with a string and a list of regular | ||
| 402 | # expressions we need to match | ||
| 403 | sub match { | ||
| 404 | my $string = shift; | ||
| 405 | my @regexps = @_; | ||
| 406 | |||
| 407 | # we save the starting string, for case when we need to backtrack | ||
| 408 | my $origstring = $string; | ||
| 409 | |||
| 410 | # the title is the name of the goal (bigger entity) we now try | ||
| 411 | # to match; it's mainly used to find correct error message | ||
| 412 | my $title; | ||
| 413 | |||
| 414 | if (@regexps == 1 and defined $COMMANDS{$regexps[0]}) { | ||
| 415 | $title = $regexps[0]; | ||
| 416 | my $c = $COMMANDS{$regexps[0]}; | ||
| 417 | |||
| 418 | # if we are to match a thing in %COMMANDS, let's expand it | ||
| 419 | @regexps = expand( ( ref $c ) ? @$c : | ||
| 420 | grep { $_ ne '' } split /\s+/, $c); | ||
| 421 | } | ||
| 422 | |||
| 423 | # as the first element of the @regexp list, we might have got | ||
| 424 | # modifiers -- ? or * -- we will use them in cse of non-match | ||
| 425 | my $modif; | ||
| 426 | if (@regexps and $regexps[0] eq '?' or $regexps[0] eq '*') { | ||
| 427 | $modif = shift @regexps; | ||
| 428 | } | ||
| 429 | |||
| 430 | # let's walk through the @regexp list and see | ||
| 431 | my @result; | ||
| 432 | my $i = 0; | ||
| 433 | while ($i < @regexps) { | ||
| 434 | my $regexp = $regexps[$i]; | ||
| 435 | my ($error, $errstr, @r); | ||
| 436 | |||
| 437 | # if it's an array, call match recursivelly | ||
| 438 | if (ref $regexp) { | ||
| 439 | ($string, $error, $errstr, @r) = match($string, @$regexp); | ||
| 440 | } | ||
| 441 | # if it's a thing in COMMANDS, call match recursivelly | ||
| 442 | elsif (defined $COMMANDS{$regexp}) { | ||
| 443 | ($string, $error, $errstr, @r) = match($string, $regexp); | ||
| 444 | } | ||
| 445 | |||
| 446 | # if we've found |, it means that one alternative matched | ||
| 447 | # fine and we can leave the loop -- we use next to go | ||
| 448 | # through continue | ||
| 449 | elsif ($regexp eq '|') { | ||
| 450 | $i = $#regexps; next; | ||
| 451 | } | ||
| 452 | |||
| 453 | # otherwise do a regexp match | ||
| 454 | elsif ($string =~ s/^\s*?($regexp)(?:$|\b|(?=\W))//si) { | ||
| 455 | @r = $1; | ||
| 456 | } | ||
| 457 | |||
| 458 | # and yet otherwise we have a problem | ||
| 459 | else { | ||
| 460 | $error = 1; | ||
| 461 | } | ||
| 462 | |||
| 463 | # if we have a problem | ||
| 464 | if (defined $error) { | ||
| 465 | # if nothing has matched yet, try to find next | ||
| 466 | # alternative | ||
| 467 | if ($origstring eq $string) { | ||
| 468 | while ($i < @regexps) { | ||
| 469 | last if $regexps[$i] eq '|'; $i++; | ||
| 470 | } | ||
| 471 | next if $i < @regexps; | ||
| 472 | last if defined $modif; | ||
| 473 | } | ||
| 474 | |||
| 475 | # if we got here, we haven't found any alternative | ||
| 476 | # and no modifier was specified for this list | ||
| 477 | # so just form the errstr and return with shame | ||
| 478 | if (not defined $errstr) { | ||
| 479 | if (defined $ERRORS{$regexp}) { | ||
| 480 | $errstr = $ERRORS{$regexp}; | ||
| 481 | } elsif (defined $title and defined $ERRORS{$title}) { | ||
| 482 | $errstr = $ERRORS{$title}; | ||
| 483 | } | ||
| 484 | } | ||
| 485 | |||
| 486 | return ($string, 1, $errstr, @result); | ||
| 487 | } | ||
| 488 | |||
| 489 | # add result to @result | ||
| 490 | if (ref $regexp) { | ||
| 491 | push @result, @r; | ||
| 492 | } elsif (@r > 1) { | ||
| 493 | push @result, $regexp, [ @r ]; | ||
| 494 | } else { | ||
| 495 | push @result, $regexp, $r[0]; | ||
| 496 | } | ||
| 497 | } | ||
| 498 | continue { | ||
| 499 | $i++; | ||
| 500 | # if we hve *, let's try another round | ||
| 501 | if (defined $modif and $modif eq '*' and $i >= @regexps) { | ||
| 502 | $origstring = $string; $i = 0; | ||
| 503 | } | ||
| 504 | } | ||
| 505 | |||
| 506 | return ($string, undef, undef, @result); | ||
| 507 | } | ||
| 508 | |||
| 509 | sub expand { | ||
| 510 | my @result; | ||
| 511 | my $i = 0; | ||
| 512 | while ($i < @_) { | ||
| 513 | my $t = $_[$i]; | ||
| 514 | if ($t eq '(') { | ||
| 515 | $i++; | ||
| 516 | my $begin = $i; | ||
| 517 | my $nest = 1; | ||
| 518 | while ($i < @_ and $nest) { | ||
| 519 | my $t = $_[$i]; | ||
| 520 | if ($t eq '(') { $nest++; } | ||
| 521 | elsif ($t eq ')') { $nest--; } | ||
| 522 | $i++; | ||
| 523 | } | ||
| 524 | $i--; | ||
| 525 | push @result, [ expand(@_[$begin .. $i - 1]) ]; | ||
| 526 | } elsif ($t eq '?' or $t eq '*') { | ||
| 527 | my $prev = pop @result; | ||
| 528 | push @result, [ $t, ( ref $prev ? @$prev : $prev ) ]; | ||
| 529 | } else { | ||
| 530 | push @result, $t; | ||
| 531 | } | ||
| 532 | $i++; | ||
| 533 | } | ||
| 534 | @result; | ||
| 535 | } | ||
| 536 | # | ||
| 537 | # We run this method on the XBase::SQL object, with the tree structure | ||
| 538 | # in the $result arrayref | ||
| 539 | sub store_results { | ||
| 540 | my ($self, $result) = @_; | ||
| 541 | |||
| 542 | my $i = 0; | ||
| 543 | |||
| 544 | # Walk through the list | ||
| 545 | while ($i < @$result) { | ||
| 546 | # get the key and the value matched for the key | ||
| 547 | my ($key, $match) = @{$result}[$i, $i + 1]; | ||
| 548 | |||
| 549 | # if there is some structure below, process it | ||
| 550 | if (ref $match) { | ||
| 551 | $self->store_results($match); | ||
| 552 | } | ||
| 553 | |||
| 554 | # see what are we supposed to do for this key | ||
| 555 | my $store_value = $STORE{$key}; | ||
| 556 | |||
| 557 | if (defined $store_value) { | ||
| 558 | if (ref $store_value eq 'CODE') { | ||
| 559 | my @out = &{$store_value}($self, (ref $match ? @$match : $match)); | ||
| 560 | if (@out == 1) { | ||
| 561 | $result->[$i+1] = $out[0]; | ||
| 562 | } else { | ||
| 563 | $result->[$i+1] = [ @out ]; | ||
| 564 | } | ||
| 565 | } else { | ||
| 566 | push @{$self->{$store_value}}, get_strings($match); | ||
| 567 | } | ||
| 568 | } | ||
| 569 | |||
| 570 | =comment | ||
| 571 | |||
| 572 | if (defined $m) | ||
| 573 | { | ||
| 574 | my @result = (( ref $m eq 'CODE' ) ? &{$m}( ref $match ? @$match : $match) : $m); | ||
| 575 | if (@result == 1) | ||
| 576 | { $match = $result[0]; } | ||
| 577 | else | ||
| 578 | { $match = [ @result ]; } | ||
| 579 | $result->[$i + 1] = $match; | ||
| 580 | } | ||
| 581 | |||
| 582 | if (defined $stval) | ||
| 583 | { | ||
| 584 | my @result; | ||
| 585 | if (ref $match) { @result = get_strings($match); } | ||
| 586 | else { @result = $match; } | ||
| 587 | if (ref $stval eq 'CODE') | ||
| 588 | { &{$stval}($self, @result); } | ||
| 589 | else | ||
| 590 | { push @{$self->{$stval}}, @result; } | ||
| 591 | } | ||
| 592 | |||
| 593 | =cut | ||
| 594 | |||
| 595 | $i += 2; | ||
| 596 | } | ||
| 597 | } | ||
| 598 | # | ||
| 599 | # | ||
| 600 | sub get_strings { | ||
| 601 | my @strings = @_; | ||
| 602 | if (@strings == 1 and ref $strings[0]) { | ||
| 603 | @strings = @{$strings[0]}; | ||
| 604 | } | ||
| 605 | my @result; | ||
| 606 | my $i = 1; | ||
| 607 | while ($i < @strings) { | ||
| 608 | if (ref $strings[$i]) { | ||
| 609 | push @result, get_strings($strings[$i]); | ||
| 610 | } else { | ||
| 611 | push @result, $strings[$i]; | ||
| 612 | } | ||
| 613 | $i += 2; | ||
| 614 | } | ||
| 615 | @result; | ||
| 616 | } | ||
| 617 | sub print_result { | ||
| 618 | my $result = shift; | ||
| 619 | my @result = @$result; | ||
| 620 | my @before = @_; | ||
| 621 | my $i = 0; | ||
| 622 | while ($i < @result) { | ||
| 623 | my ($regexp, $string) = @result[$i, $i + 1]; | ||
| 624 | if (ref $string) { | ||
| 625 | print_result($string, @before, $regexp); | ||
| 626 | } else { | ||
| 627 | print "$string:\t @before $regexp\n"; | ||
| 628 | } | ||
| 629 | $i += 2; | ||
| 630 | } | ||
| 631 | } | ||
| 632 | |||
| 633 | |||
| 634 | # ####################################### | ||
| 635 | # Implementing methods in SQL expressions | ||
| 636 | |||
| 637 | package XBase::SQL::Expr; | ||
| 638 | |||
| 639 | use strict; | ||
| 640 | |||
| 641 | use overload | ||
| 642 | '+' => sub { XBase::SQL::Expr->number($_[0]->value + $_[1]->value); }, | ||
| 643 | '-' => sub { my $a = $_[0]->value - $_[1]->value; $a = -$a if $_[2]; | ||
| 644 | XBase::SQL::Expr->number($a); }, | ||
| 645 | '/' => sub { my $a = ( $_[2] ? $_[1]->value / $_[0]->value | ||
| 646 | : $_[0]->value / $_[1]->value ); | ||
| 647 | XBase::SQL::Expr->number($a); }, | ||
| 648 | '%' => sub { my $a = ( $_[2] ? $_[1]->value % $_[0]->value | ||
| 649 | : $_[0]->value % $_[1]->value ); | ||
| 650 | XBase::SQL::Expr->number($a); }, | ||
| 651 | '<' => \&less, | ||
| 652 | '<=' => \&lesseq, | ||
| 653 | '>' => sub { $_[1]->less(@_[0, 2]); }, | ||
| 654 | '>=' => sub { $_[1]->lesseq(@_[0, 2]); }, | ||
| 655 | '!=' => \¬equal, | ||
| 656 | '<>' => \¬equal, | ||
| 657 | '==' => sub { my $a = shift->notequal(@_); return ( $a ? 0 : 1); }, | ||
| 658 | '""' => sub { ref shift; }, | ||
| 659 | '.' => sub { XBase::SQL::Expr->string($_[0]->value . $_[1]->value); }, | ||
| 660 | '*' => sub { XBase::SQL::Expr->number($_[0]->value * $_[1]->value);}, | ||
| 661 | '!' => sub { not $_[0]->value }, | ||
| 662 | ; | ||
| 663 | |||
| 664 | sub new { bless {}, shift; } | ||
| 665 | sub value { shift->{'value'}; } | ||
| 666 | |||
| 667 | sub field { | ||
| 668 | my ($class, $field, $table, $values) = @_; | ||
| 669 | my $self = $class->new; | ||
| 670 | $self->{'field'} = $field; | ||
| 671 | $self->{'value'} = $values->{$field}; | ||
| 672 | |||
| 673 | my $type = $table->field_type($field); | ||
| 674 | if ($type eq 'N') { $self->{'number'} = 1; } | ||
| 675 | else { $self->{'string'} = 1; } | ||
| 676 | $self; | ||
| 677 | } | ||
| 678 | sub string { | ||
| 679 | my $self = shift->new; | ||
| 680 | $self->{'value'} = shift; | ||
| 681 | $self->{'string'} = 1; | ||
| 682 | $self; | ||
| 683 | } | ||
| 684 | sub number { | ||
| 685 | my $self = shift->new; | ||
| 686 | $self->{'value'} = shift; | ||
| 687 | $self->{'number'} = 1; | ||
| 688 | $self; | ||
| 689 | } | ||
| 690 | sub null { | ||
| 691 | my $self = shift->new; | ||
| 692 | $self->{'value'} = undef; | ||
| 693 | $self; | ||
| 694 | } | ||
| 695 | sub other { | ||
| 696 | my $class = shift; | ||
| 697 | my $other = shift; | ||
| 698 | $other; | ||
| 699 | } | ||
| 700 | sub function { | ||
| 701 | my ($class, $function, $table, $values, @params) = @_; | ||
| 702 | my $self = $class->new; | ||
| 703 | $self->{'string'} = 1; | ||
| 704 | if ($function eq 'LENGTH') { | ||
| 705 | $self->{'value'} = length($params[0]->value); | ||
| 706 | delete $self->{'string'}; | ||
| 707 | $self->{'number'} = 1; | ||
| 708 | } elsif ($function eq 'TRIM') { | ||
| 709 | ($self->{'value'} = $params[0]->value) =~ s/^\s+|\s+$//g; | ||
| 710 | } elsif ($function eq 'LTRIM') { | ||
| 711 | ($self->{'value'} = $params[0]->value) =~ s/^\s+//; | ||
| 712 | } elsif ($function eq 'RTRIM') { | ||
| 713 | ($self->{'value'} = $params[0]->value) =~ s/\s+$//; | ||
| 714 | } elsif ($function eq 'CONCAT') { | ||
| 715 | $self->{'value'} = join '', map { $_->value } @params; | ||
| 716 | } elsif ($function eq 'SUBSTR' or $function eq 'SUBSTRING') { | ||
| 717 | my ($string, $start, $length) = map { $_->value } @params; | ||
| 718 | if ($start == 0) { $start = 1; } | ||
| 719 | $self->{'value'} = substr($string, $start - 1, $length); | ||
| 720 | } | ||
| 721 | $self; | ||
| 722 | } | ||
| 723 | |||
| 724 | 1; | ||
| 725 | # | ||
| 726 | # Function working on Expr objects | ||
| 727 | # | ||
| 728 | sub less { | ||
| 729 | my ($self, $other, $reverse) = @_; | ||
| 730 | my $answer; | ||
| 731 | if (defined $self->{'string'} or defined $other->{'string'}) { | ||
| 732 | $answer = ($self->value lt $other->value); | ||
| 733 | } else { | ||
| 734 | $answer = ($self->value < $other->value); | ||
| 735 | } | ||
| 736 | return -$answer if $reverse; | ||
| 737 | $answer; | ||
| 738 | } | ||
| 739 | sub lesseq { | ||
| 740 | my ($self, $other, $reverse) = @_; | ||
| 741 | my $answer; | ||
| 742 | if (defined $self->{'string'} or defined $other->{'string'}) { | ||
| 743 | $answer = ($self->value le $other->value); | ||
| 744 | } else { | ||
| 745 | $answer = ($self->value <= $other->value); | ||
| 746 | } | ||
| 747 | return -$answer if $reverse; | ||
| 748 | $answer; | ||
| 749 | } | ||
| 750 | sub notequal { | ||
| 751 | my ($self, $other) = @_; | ||
| 752 | local $^W = 0; | ||
| 753 | if (defined $self->{'string'} or defined $other->{'string'}) { | ||
| 754 | ($self->value ne $other->value); | ||
| 755 | } else { | ||
| 756 | ($self->value != $other->value); | ||
| 757 | } | ||
| 758 | } | ||
| 759 | |||
| 760 | sub likematch { | ||
| 761 | my $class = shift; | ||
| 762 | my ($field, $string) = @_; | ||
| 763 | |||
| 764 | my $regexp = $string->value; | ||
| 765 | $regexp =~ s/(\\\\[%_]|.)/ ($1 eq '%') ? '.*' : ($1 eq '_') ? '.' : "\Q$1" /seg; | ||
| 766 | $field->value =~ /^$regexp$/si; | ||
| 767 | } | ||
| 768 | |||
| 769 | 1; | ||
| 770 |