| 1 | #!/usr/local/bin/perl5 | ||
| 2 | |||
| 3 | ##++ | ||
| 4 | ## Sprite v3.21 | ||
| 5 | ## Last modified: April 21, 1998 | ||
| 6 | ## | ||
| 7 | ## Copyright (c) 1995-98, Shishir Gundavaram | ||
| 8 | ## All Rights Reserved | ||
| 9 | ## | ||
| 10 | ## E-Mail: shishir@ora.com | ||
| 11 | ## | ||
| 12 | ## Permission to use, copy, and distribute is hereby granted, | ||
| 13 | ## providing that the above copyright notice and this permission | ||
| 14 | ## appear in all copies and in supporting documentation. | ||
| 15 | ## | ||
| 16 | ## If you use Sprite for any cool (Web) applications, I would be | ||
| 17 | ## interested in hearing about them. So, drop me a line. Thanks! | ||
| 18 | ##-- | ||
| 19 | |||
| 20 | ############################################################################# | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | Sprite - Module to manipulate text delimited databases using SQL. | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | use Sprite; | ||
| 29 | |||
| 30 | $rdb = new Sprite; | ||
| 31 | |||
| 32 | $rdb->set_delimiter (-Read => '::') ## OR: ('Read', '::'); | ||
| 33 | $rdb->set_delimiter (-Write => '::') ## OR: ('Write', '::'); | ||
| 34 | |||
| 35 | $rdb->set_os ('Win95'); | ||
| 36 | |||
| 37 | ## Valid arguments (case insensitive) include: | ||
| 38 | ## | ||
| 39 | ## Unix, Win95, Windows95, MSDOS, NT, WinNT, OS2, VMS, | ||
| 40 | ## MacOS or Macintosh. Default determined by $^O. | ||
| 41 | |||
| 42 | $rdb->set_lock_file ('c:\win95\tmp\Sprite.lck', 10); | ||
| 43 | |||
| 44 | $rdb->set_db_dir ('Mac OS:Perl 5:Data') || die "Can't access dir!\n"; | ||
| 45 | |||
| 46 | $data = $rdb->sql (<<Query); ## OR: @data = $rdb->sql (<<Query); | ||
| 47 | . | ||
| 48 | . (SQL) | ||
| 49 | . | ||
| 50 | Query | ||
| 51 | |||
| 52 | foreach $row (@$data) { ## OR: foreach $row (@data) { | ||
| 53 | @columns = @$row; ## NO null delimited string -- v3.2 | ||
| 54 | } | ||
| 55 | |||
| 56 | $rdb->close; | ||
| 57 | $rdb->close ($database); ## To save updated database | ||
| 58 | |||
| 59 | =head1 DESCRIPTION | ||
| 60 | |||
| 61 | Here is a simple database where the fields are delimited by commas: | ||
| 62 | |||
| 63 | Player,Years,Points,Rebounds,Assists,Championships | ||
| 64 | ... | ||
| 65 | Larry Bird,13,25,11,7,3 | ||
| 66 | Michael Jordan,14,29,6,5,5 | ||
| 67 | Magic Johnson,13,22,7,11,5 | ||
| 68 | ... | ||
| 69 | |||
| 70 | I<Note:> The first line must contain the field names (case sensitive). | ||
| 71 | |||
| 72 | =head1 Supported SQL Commands | ||
| 73 | |||
| 74 | Here are a list of the SQL commands that are supported by Sprite: | ||
| 75 | |||
| 76 | =over 5 | ||
| 77 | |||
| 78 | =item I<select> - retrieves records that match specified criteria: | ||
| 79 | |||
| 80 | select col1 [,col2] from database | ||
| 81 | where (cond1 OPERATOR value1) | ||
| 82 | [and|or (cond2 OPERATOR value2) ...] | ||
| 83 | |||
| 84 | The '*' operator can be used to select all columns. | ||
| 85 | |||
| 86 | The I<database> is simply the file that contains the data. If the file | ||
| 87 | is not in the current directory, the path must be specified. | ||
| 88 | |||
| 89 | Sprite does I<not> support multiple tables (or commonly knows as "joins"). | ||
| 90 | |||
| 91 | Valid column names can be used where [cond1..n] and [value1..n] are expected, | ||
| 92 | such as: | ||
| 93 | |||
| 94 | I<Example 1>: | ||
| 95 | |||
| 96 | select Player, Points from my_db | ||
| 97 | where (Rebounds > Assists) | ||
| 98 | |||
| 99 | The following SQL operators can be used: =, <, >, <=, >=, <> as well as | ||
| 100 | Perl's special operators: =~ and !~. The =~ and !~ operators are used to | ||
| 101 | specify regular expressions, such as: | ||
| 102 | |||
| 103 | I<Example 2>: | ||
| 104 | |||
| 105 | select * from my_db | ||
| 106 | where (Name =~ /Bird$/i) | ||
| 107 | |||
| 108 | Selects records where the Name column ends with "Bird" (case insensitive). | ||
| 109 | For more information, look at a manual on regexps. | ||
| 110 | |||
| 111 | I<Note:> A path to a database can contain only the following characters: | ||
| 112 | |||
| 113 | \w, \x80-\xFF, -, /, \, ., : | ||
| 114 | |||
| 115 | If you have directories with spaces or other 'invalid' characters, you | ||
| 116 | need to use the I<set_db_dir> method. | ||
| 117 | |||
| 118 | =item I<update> - updates records that match specified criteria. | ||
| 119 | |||
| 120 | update database | ||
| 121 | set cond1 = (value1)[,cond2 = (value2) ...] | ||
| 122 | where (cond1 OPERATOR value1) | ||
| 123 | [and|or (cond2 OPERATOR value2) ...] | ||
| 124 | |||
| 125 | I<Example>: | ||
| 126 | |||
| 127 | update my_db | ||
| 128 | set Championships = (Championships + 1) | ||
| 129 | where (Player = 'Larry Bird') | ||
| 130 | |||
| 131 | update my_db | ||
| 132 | set Championships = (Championships + 1), | ||
| 133 | Years = (12) | ||
| 134 | where (Player = 'Larry Bird') | ||
| 135 | |||
| 136 | =item I<delete> - removes records that match specified criteria: | ||
| 137 | |||
| 138 | delete from database | ||
| 139 | where (cond1 OPERATOR value1) | ||
| 140 | [and|or (cond2 OPERATOR value2) ...] | ||
| 141 | |||
| 142 | I<Example>: | ||
| 143 | |||
| 144 | delete from my_db | ||
| 145 | where (Player =~ /Johnson$/i) or | ||
| 146 | (Years > 12) | ||
| 147 | |||
| 148 | =item I<alter> - simplified version of SQL-92 counterpart | ||
| 149 | |||
| 150 | Removes the specified column from the database. The other standard SQL | ||
| 151 | functions for alter table are not supported: | ||
| 152 | |||
| 153 | alter table database | ||
| 154 | drop column column-name | ||
| 155 | |||
| 156 | alter table database | ||
| 157 | add column column-name | ||
| 158 | |||
| 159 | I<Example>: | ||
| 160 | |||
| 161 | alter table my_db | ||
| 162 | drop column Years | ||
| 163 | |||
| 164 | alter table my_db | ||
| 165 | add column Legend | ||
| 166 | |||
| 167 | =item I<insert> - inserts a record into the database: | ||
| 168 | |||
| 169 | insert into database | ||
| 170 | (col1, col2, ... coln) | ||
| 171 | values | ||
| 172 | (val1, val2, ... valn) | ||
| 173 | |||
| 174 | I<Example>: | ||
| 175 | |||
| 176 | insert into my_db | ||
| 177 | (Player, Years, Points, Championships) | ||
| 178 | values | ||
| 179 | ('Kareem Abdul-Jabbar', 21, 26, 6) | ||
| 180 | |||
| 181 | You don't have to specify all of the fields in the database! Sprite also | ||
| 182 | does not require you to specify the fields in the same order as that of | ||
| 183 | the database. | ||
| 184 | |||
| 185 | I<Note:> You should make it a habit to quote strings. | ||
| 186 | |||
| 187 | =back | ||
| 188 | |||
| 189 | =head1 METHODS | ||
| 190 | |||
| 191 | Here are the available methods: | ||
| 192 | |||
| 193 | =over 5 | ||
| 194 | |||
| 195 | =item I<set_delimiter> | ||
| 196 | |||
| 197 | The set_delimiter function sets the read and write delimiter for the | ||
| 198 | database. The delimiter is not limited to one character; you can have | ||
| 199 | a string, and even a regexp (for reading only). | ||
| 200 | |||
| 201 | I<Return Value> | ||
| 202 | |||
| 203 | None | ||
| 204 | |||
| 205 | =item I<set_os> | ||
| 206 | |||
| 207 | The set_os function can be used to notify Sprite as to the operating | ||
| 208 | system that you're using. Default is determined by $^O. | ||
| 209 | |||
| 210 | I<Note:> If you're using Sprite on Windows 95/NT or on OS2, make sure | ||
| 211 | to use backslashes -- and NOT forward slashes -- when specifying a path | ||
| 212 | for a database or to the I<set_db_dir> or I<set_lock_file> methods! | ||
| 213 | |||
| 214 | I<Return Value> | ||
| 215 | |||
| 216 | None | ||
| 217 | |||
| 218 | =item I<set_lock_file> | ||
| 219 | |||
| 220 | For any O/S that doesn't support flock (i.e Mac, Windows 95 and VMS), this | ||
| 221 | method allows you to set a lock file to use and the number of tries that | ||
| 222 | Sprite should try to obtain a 'fake' lock. However, this method is NOT | ||
| 223 | fully reliable, but is better than no lock at all. | ||
| 224 | |||
| 225 | 'Sprite.lck' (either in the directory specified by I<set_db_dir> or in | ||
| 226 | the current directory) is used as the default lock file if one | ||
| 227 | is not specified. | ||
| 228 | |||
| 229 | I<Return Value> | ||
| 230 | |||
| 231 | None | ||
| 232 | |||
| 233 | =item I<set_db_dir> | ||
| 234 | |||
| 235 | A path to a database can contain only the following characters: | ||
| 236 | |||
| 237 | \w, \x80-\xFF, -, /, \, ., : | ||
| 238 | |||
| 239 | If your path contains other characters besides the ones listed above, | ||
| 240 | you can use this method to set a default directory. Here's an example: | ||
| 241 | |||
| 242 | $rdb->set_db_dir ("Mac OS:Perl 5:Data"); | ||
| 243 | |||
| 244 | $data = $rdb->sql ("select * from phone.db"); | ||
| 245 | |||
| 246 | Sprite will look for the file "Mac OS:Perl 5:Data:phone.db". Just to | ||
| 247 | note, the database filename cannot have any characters besides the one | ||
| 248 | listed above! | ||
| 249 | |||
| 250 | I<Return Value> | ||
| 251 | |||
| 252 | 0 - Failure | ||
| 253 | 1 - Success | ||
| 254 | |||
| 255 | =item I<sql> | ||
| 256 | |||
| 257 | The sql function is used to pass a SQL command to this module. All of the | ||
| 258 | SQL commands described above are supported. The I<select> SQL command | ||
| 259 | returns an array containing the data, where the first element is the status. | ||
| 260 | All of the other other SQL commands simply return a status. | ||
| 261 | |||
| 262 | I<Return Value> | ||
| 263 | 1 - Success | ||
| 264 | 0 - Error | ||
| 265 | |||
| 266 | =item I<close> | ||
| 267 | |||
| 268 | The close function closes the file, and destroys the database object. You | ||
| 269 | can pass a filename to the function, in which case Sprite will save the | ||
| 270 | database to that file; the directory set by I<set_db_dir> is used as | ||
| 271 | the default. | ||
| 272 | |||
| 273 | I<Return Value> | ||
| 274 | |||
| 275 | None | ||
| 276 | |||
| 277 | =back | ||
| 278 | |||
| 279 | =head1 NOTES | ||
| 280 | |||
| 281 | Sprite is not the solution to all your data manipulation needs. It's fine | ||
| 282 | for small databases (less than 1000 records), but anything over that, and | ||
| 283 | you'll have to sit there and twiddle your fingers while Sprite goes | ||
| 284 | chugging away ... and returns a few *seconds* or so later. | ||
| 285 | |||
| 286 | The main advantage of Sprite is that you can use Perl's regular expressions | ||
| 287 | to search through your data. Yippee! | ||
| 288 | |||
| 289 | =head1 SEE ALSO | ||
| 290 | |||
| 291 | Text::CSV, RDB | ||
| 292 | |||
| 293 | =head1 ACKNOWLEDGEMENTS | ||
| 294 | |||
| 295 | I would like to thank the following, especially Rod Whitby and Jim Esten, | ||
| 296 | for finding bugs and offering suggestions: | ||
| 297 | |||
| 298 | Rod Whitby (rwhitby@geocities.com) | ||
| 299 | Jim Esten (jesten@wdynamic.com) | ||
| 300 | Dave Moore (dmoore@videoactv.com) | ||
| 301 | Shane Hutchins (hutchins@ctron.com) | ||
| 302 | Josh Hochman (josh@bcdinc.com) | ||
| 303 | Barry Harrison (barryh@topnet.net) | ||
| 304 | Lisa Farley (lfarley@segue.com) | ||
| 305 | Loyd Gore (lgore@ascd.org) | ||
| 306 | Tanju Cataltepe (tanju@netlabs.net) | ||
| 307 | Haakon Norheim (hanorhei@online.no) | ||
| 308 | |||
| 309 | =head1 COPYRIGHT INFORMATION | ||
| 310 | |||
| 311 | Copyright (c) 1995-1998, Shishir Gundavaram | ||
| 312 | All Rights Reserved | ||
| 313 | |||
| 314 | Permission to use, copy, and distribute is hereby granted, | ||
| 315 | providing that the above copyright notice and this permission | ||
| 316 | appear in all copies and in supporting documentation. | ||
| 317 | |||
| 318 | =cut | ||
| 319 | |||
| 320 | ############################################################################### | ||
| 321 | |||
| 322 | package Sprite; | ||
| 323 | |||
| 324 | require 5.002; | ||
| 325 | |||
| 326 | use Cwd; | ||
| 327 | use Fcntl; | ||
| 328 | |||
| 329 | ##++ | ||
| 330 | ## Global Variables. Declare lock constants manually, instead of | ||
| 331 | ## importing them from Fcntl. | ||
| 332 | ## | ||
| 333 | ## use vars qw ($VERSION $LOCK_SH $LOCK_EX); | ||
| 334 | ##-- | ||
| 335 | |||
| 336 | $Sprite::VERSION = '3.21'; | ||
| 337 | $Sprite::LOCK_SH = 1; | ||
| 338 | $Sprite::LOCK_EX = 2; | ||
| 339 | |||
| 340 | ##++ | ||
| 341 | ## Public Methods and Constructor | ||
| 342 | ##-- | ||
| 343 | |||
| 344 | sub new | ||
| 345 | { | ||
| 346 | my $class = shift; | ||
| 347 | my $self; | ||
| 348 | |||
| 349 | $self = { | ||
| 350 | commands => 'select|update|delete|alter|insert', | ||
| 351 | column => '[A-Za-z\x80-\xFF][\w\x80-\xFF]+', | ||
| 352 | _select => '[\w\x80-\xFF\*,\s]+', | ||
| 353 | path => '[\w\x80-\xFF\-\/\.\:\\\\]+', | ||
| 354 | table => '', | ||
| 355 | file => '', | ||
| 356 | directory => '', | ||
| 357 | _read => ',', | ||
| 358 | _write => ',', | ||
| 359 | fields => {}, | ||
| 360 | order => [], | ||
| 361 | records => [], | ||
| 362 | platform => 'Unix', | ||
| 363 | fake_lock => 0, | ||
| 364 | default_lock => 'Sprite.lck', | ||
| 365 | lock_file => '', | ||
| 366 | lock_handle => '', | ||
| 367 | default_try => 10, | ||
| 368 | lock_try => '', | ||
| 369 | lock_sleep => 1, | ||
| 370 | errors => {} | ||
| 371 | }; | ||
| 372 | |||
| 373 | $self->{separator} = { Unix => '/', Mac => ':', | ||
| 374 | PC => '\\\\', VMS => '/' }; | ||
| 375 | |||
| 376 | bless $self, $class; | ||
| 377 | |||
| 378 | $self->initialize; | ||
| 379 | |||
| 380 | return $self; | ||
| 381 | } | ||
| 382 | |||
| 383 | sub initialize | ||
| 384 | { | ||
| 385 | my $self = shift; | ||
| 386 | |||
| 387 | $self->define_errors; | ||
| 388 | |||
| 389 | $self->set_os ($^O) if (defined $^O); | ||
| 390 | } | ||
| 391 | |||
| 392 | sub set_delimiter | ||
| 393 | { | ||
| 394 | my ($self, $type, $delimiter) = @_; | ||
| 395 | |||
| 396 | $type ||= 'other'; | ||
| 397 | $delimiter ||= $self->{_read} || $self->{_write}; | ||
| 398 | |||
| 399 | $type =~ s/^-//; | ||
| 400 | $type = lc $type; | ||
| 401 | |||
| 402 | if ($type eq 'read') { | ||
| 403 | $self->{_read} = $delimiter; | ||
| 404 | } elsif ($type eq 'write') { | ||
| 405 | $self->{_write} = $delimiter; | ||
| 406 | } else { | ||
| 407 | $self->{_read} = $self->{_write} = $delimiter; | ||
| 408 | } | ||
| 409 | |||
| 410 | return (1); | ||
| 411 | } | ||
| 412 | |||
| 413 | sub set_os | ||
| 414 | { | ||
| 415 | my ($self, $platform) = @_; | ||
| 416 | |||
| 417 | $platform = 'Unix', return unless ($platform); | ||
| 418 | |||
| 419 | $platform =~ s/\s//g; | ||
| 420 | |||
| 421 | if ($platform =~ /^(?:OS2|(?:Win)?NT|Win(?:dows)?95|(?:MS)?DOS)$/i) { | ||
| 422 | $self->{platform} = 'PC'; | ||
| 423 | } elsif ($platform =~ /^Mac(?:OS|intosh)?$/i) { | ||
| 424 | $self->{platform} = 'Mac'; | ||
| 425 | } elsif ($platform =~ /^VMS$/i) { | ||
| 426 | $self->{platform} = 'VMS'; | ||
| 427 | } else { | ||
| 428 | $self->{platform} = 'Unix'; | ||
| 429 | } | ||
| 430 | |||
| 431 | return (1); | ||
| 432 | } | ||
| 433 | |||
| 434 | sub set_db_dir | ||
| 435 | { | ||
| 436 | my ($self, $directory) = @_; | ||
| 437 | |||
| 438 | return (0) unless ($directory); | ||
| 439 | |||
| 440 | stat ($directory); | ||
| 441 | |||
| 442 | if ( (-d _) && (-e _) && (-r _) && (-w _) ) { | ||
| 443 | $self->{directory} = $directory; | ||
| 444 | return (1); | ||
| 445 | } else { | ||
| 446 | return (0); | ||
| 447 | } | ||
| 448 | } | ||
| 449 | |||
| 450 | sub get_path_info | ||
| 451 | { | ||
| 452 | my ($self, $file) = @_; | ||
| 453 | my ($separator, $path, $name, $full); | ||
| 454 | |||
| 455 | $separator = $self->{separator}->{ $self->{platform} }; | ||
| 456 | |||
| 457 | ($path, $name) = $file =~ m|(.*?)([^$separator]+)$|o; | ||
| 458 | |||
| 459 | if ($path) { | ||
| 460 | $full = $file; | ||
| 461 | } else { | ||
| 462 | $path = $self->{directory} || fastcwd; | ||
| 463 | $path .= $separator; | ||
| 464 | $full = $path . $name; | ||
| 465 | } | ||
| 466 | |||
| 467 | return wantarray ? ($path, $name) : $full; | ||
| 468 | } | ||
| 469 | |||
| 470 | sub set_lock_file | ||
| 471 | { | ||
| 472 | my ($self, $file, $lock_try) = @_; | ||
| 473 | |||
| 474 | if (!$file || !$lock_try) { | ||
| 475 | return (0); | ||
| 476 | } else { | ||
| 477 | $self->{lock_file} = $file; | ||
| 478 | $self->{lock_try} = $lock_try; | ||
| 479 | |||
| 480 | return (1); | ||
| 481 | } | ||
| 482 | } | ||
| 483 | |||
| 484 | sub lock | ||
| 485 | { | ||
| 486 | my $self = shift; | ||
| 487 | my $count; | ||
| 488 | |||
| 489 | $self->{lock_file} ||= $self->{default_lock}; | ||
| 490 | $self->{lock_file} = $self->get_path_info ($self->{lock_file}); | ||
| 491 | $self->{lock_try} ||= $self->{default_try}; | ||
| 492 | |||
| 493 | local *FILE; | ||
| 494 | |||
| 495 | $count = 0; | ||
| 496 | |||
| 497 | while (++$count <= $self->{lock_try}) { | ||
| 498 | if (sysopen (FILE, $self->{lock_file}, | ||
| 499 | O_WRONLY|O_EXCL|O_CREAT, 0644)) { | ||
| 500 | |||
| 501 | $self->{fake_lock} = 1; | ||
| 502 | $self->{lock_handle} = *FILE; | ||
| 503 | |||
| 504 | last; | ||
| 505 | } else { | ||
| 506 | select (undef, undef, undef, $self->{lock_sleep}); | ||
| 507 | } | ||
| 508 | } | ||
| 509 | |||
| 510 | return $self->{fake_lock}; | ||
| 511 | } | ||
| 512 | |||
| 513 | sub unlock | ||
| 514 | { | ||
| 515 | my $self = shift; | ||
| 516 | |||
| 517 | if ($self->{fake_lock}) { | ||
| 518 | |||
| 519 | close ($self->{lock_handle}) || return (0); | ||
| 520 | unlink ($self->{lock_file}) || return (0); | ||
| 521 | |||
| 522 | $self->{fake_lock} = 0; | ||
| 523 | $self->{lock_handle} = ''; | ||
| 524 | |||
| 525 | } | ||
| 526 | |||
| 527 | return (1); | ||
| 528 | } | ||
| 529 | |||
| 530 | sub sql | ||
| 531 | { | ||
| 532 | my ($self, $query) = @_; | ||
| 533 | my ($command, $status); | ||
| 534 | |||
| 535 | $self->display_error (-514) unless ($query); | ||
| 536 | |||
| 537 | $query =~ s/\n/ /gs; | ||
| 538 | $query =~ s/^\s*(.*?)\s*$/$1/; | ||
| 539 | $command = ''; | ||
| 540 | |||
| 541 | if ($query =~ /^($self->{commands})/io) { | ||
| 542 | $command = lc $1; | ||
| 543 | $status = $self->$command ($query); | ||
| 544 | |||
| 545 | if (ref ($status) eq 'ARRAY') { | ||
| 546 | unshift (@$status, 1); | ||
| 547 | |||
| 548 | return wantarray ? @$status : $status; | ||
| 549 | } else { | ||
| 550 | if ($status <= 0) { | ||
| 551 | $self->display_error ($status); | ||
| 552 | return (0); | ||
| 553 | } else { | ||
| 554 | return (1); | ||
| 555 | } | ||
| 556 | } | ||
| 557 | } else { | ||
| 558 | return (0); | ||
| 559 | } | ||
| 560 | } | ||
| 561 | |||
| 562 | sub display_error | ||
| 563 | { | ||
| 564 | my ($self, $status, @error) = @_; | ||
| 565 | my ($error, $other); | ||
| 566 | |||
| 567 | $error = (scalar @error) ? "\n>> " . join ("\n>> ", @error) : ''; | ||
| 568 | $other = ($@) ? "\n>> $@" : "\n"; | ||
| 569 | |||
| 570 | warn <<Error_Message; | ||
| 571 | Sprite Error: | ||
| 572 | |||
| 573 | >> $self->{errors}->{$status} $error $other | ||
| 574 | Error_Message | ||
| 575 | |||
| 576 | return (1); | ||
| 577 | } | ||
| 578 | |||
| 579 | sub close | ||
| 580 | { | ||
| 581 | my ($self, $file) = @_; | ||
| 582 | my ($status, $full_path); | ||
| 583 | |||
| 584 | $status = 1; | ||
| 585 | |||
| 586 | if ($file) { | ||
| 587 | $full_path = $self->get_path_info ($file); | ||
| 588 | $status = $self->write_file ($full_path); | ||
| 589 | |||
| 590 | $self->display_error ($status) if ($status <= 0); | ||
| 591 | } | ||
| 592 | |||
| 593 | ##++ | ||
| 594 | ## Destroy object! | ||
| 595 | ##-- | ||
| 596 | |||
| 597 | undef $self; | ||
| 598 | |||
| 599 | return $status; | ||
| 600 | } | ||
| 601 | |||
| 602 | ##++ | ||
| 603 | ## Private Methods | ||
| 604 | ##-- | ||
| 605 | |||
| 606 | sub define_errors | ||
| 607 | { | ||
| 608 | my $self = shift; | ||
| 609 | my $errors; | ||
| 610 | |||
| 611 | $errors = {}; | ||
| 612 | |||
| 613 | $errors->{'-501'} = 'Could not open specified database.'; | ||
| 614 | $errors->{'-502'} = 'Specified column(s) not found.'; | ||
| 615 | $errors->{'-503'} = 'Incorrect format in select.'; | ||
| 616 | $errors->{'-504'} = 'Incorrect format in update.'; | ||
| 617 | $errors->{'-505'} = 'Incorrect format in delete.'; | ||
| 618 | $errors->{'-506'} = 'Incorrect format in add/drop column.'; | ||
| 619 | $errors->{'-507'} = 'Incorrect format in alter table.'; | ||
| 620 | $errors->{'-508'} = 'Incorrect format in insert command.'; | ||
| 621 | $errors->{'-509'} = 'The no. of columns does not match no. of values.'; | ||
| 622 | $errors->{'-510'} = 'A severe error! Check your query carefully.'; | ||
| 623 | $errors->{'-511'} = 'Cannot write the database to output file.'; | ||
| 624 | $errors->{'-512'} = 'Unmatched quote in expression.'; | ||
| 625 | $errors->{'-513'} = 'Need to open the database first.'; | ||
| 626 | $errors->{'-514'} = 'Please specify a valid query.'; | ||
| 627 | $errors->{'-515'} = 'Cannot get lock on database file.'; | ||
| 628 | $errors->{'-516'} = 'Cannot delete temp. lock file.'; | ||
| 629 | |||
| 630 | $self->{errors} = $errors; | ||
| 631 | |||
| 632 | return (1); | ||
| 633 | } | ||
| 634 | |||
| 635 | sub parse_expression | ||
| 636 | { | ||
| 637 | my ($self, $query) = @_; | ||
| 638 | my ($column, @strings, %numopmap, %stropmap, $numops, $strops, $special); | ||
| 639 | |||
| 640 | return unless ($query); | ||
| 641 | |||
| 642 | $column = $self->{column}; | ||
| 643 | @strings = (); | ||
| 644 | |||
| 645 | %numopmap = ( '=' => 'eq', '==' => 'eq', '>=' => 'ge', '<=' => 'le', | ||
| 646 | '>' => 'gt', '<' => 'lt', '!=' => 'ne', '<>' => 'ne' ); | ||
| 647 | %stropmap = ( 'eq' => '==', 'ge' => '>=', 'le' => '<=', | ||
| 648 | 'gt' => '>', 'lt' => '<', 'ne' => '!='); | ||
| 649 | |||
| 650 | $numops = join '|', keys %numopmap; | ||
| 651 | $strops = join '|', keys %stropmap; | ||
| 652 | |||
| 653 | $special = "$strops|and|or"; | ||
| 654 | |||
| 655 | ##++ | ||
| 656 | ## A big thanks to the King of Regex, Jeffrey Friedl, for helping | ||
| 657 | ## me craft up this beauty: (...)((?:\\\1|(?!\1).)*)\1 - Thanks! | ||
| 658 | ##-- | ||
| 659 | |||
| 660 | $query =~ s{([!=]~)\s*(m?)([^\w;\s])((?:\\\3|(?!\3).)*)\3(i?)}{ | ||
| 661 | push (@strings, "$2$3$4$3$5"); "$1 *$#strings"; | ||
| 662 | }ge; | ||
| 663 | |||
| 664 | $query =~ s{(['"])((?:\\\1|(?!\1).)*)\1}{ | ||
| 665 | push (@strings, "$1$2$1"); "*$#strings"; | ||
| 666 | }ge; | ||
| 667 | |||
| 668 | $query =~ s|\b($column)\s*($numops)\s*\*|$1 $numopmap{$2} \*|go; | ||
| 669 | |||
| 670 | $query =~ s|\b($column)\s*=\s*(\d+)|$1 == $2|go; | ||
| 671 | |||
| 672 | $query =~ s|\b($column)\s+($strops)\s+(\d+)|$1 $stropmap{$2} $3|go; | ||
| 673 | |||
| 674 | $query =~ s|($column)| | ||
| 675 | my $match = $1; | ||
| 676 | ($match =~ /\b(?:$special)/io) ? "\L$match\E" : | ||
| 677 | "\$_->{$match}" | ||
| 678 | |geo; | ||
| 679 | |||
| 680 | $query =~ s|[;`]||g; | ||
| 681 | $query =~ s#\|\|#or#g; | ||
| 682 | $query =~ s#&&#and#g; | ||
| 683 | |||
| 684 | $query =~ s|\*(\d+)|$strings[$1]|g; | ||
| 685 | |||
| 686 | return $query; | ||
| 687 | } | ||
| 688 | |||
| 689 | sub check_columns | ||
| 690 | { | ||
| 691 | my ($self, $column_string) = @_; | ||
| 692 | my ($status, @columns, $column); | ||
| 693 | |||
| 694 | $status = 1; | ||
| 695 | @columns = split (/,/, $column_string); | ||
| 696 | |||
| 697 | foreach $column (@columns) { | ||
| 698 | $status = 0 unless ($self->{fields}->{$column}); | ||
| 699 | } | ||
| 700 | |||
| 701 | return $status; | ||
| 702 | } | ||
| 703 | |||
| 704 | sub parse_columns | ||
| 705 | { | ||
| 706 | my ($self, $command, $column_string, $condition, $values) = @_; | ||
| 707 | my ($status, $results, @columns, $single, $loop, $code, $column); | ||
| 708 | |||
| 709 | local $SIG{'__WARN__'} = sub { $status = -510 }; | ||
| 710 | local $^W = 0; | ||
| 711 | |||
| 712 | $status = 1; | ||
| 713 | $results = []; | ||
| 714 | @columns = split (/,/, $column_string); | ||
| 715 | $single = ($#columns) ? $columns[$[] : $column_string; | ||
| 716 | |||
| 717 | for ($loop=0; $loop < scalar @{ $self->{records} }; $loop++) { | ||
| 718 | $_ = $self->{records}->[$loop]; | ||
| 719 | |||
| 720 | if ( !$condition || (eval $condition) ) { | ||
| 721 | if ($command eq 'select') { | ||
| 722 | push (@$results, [ @$_{@columns} ]); | ||
| 723 | } elsif ($command eq 'update') { | ||
| 724 | |||
| 725 | $code = ''; | ||
| 726 | |||
| 727 | map { $code .= qq|\$_->{'$_'} = $values->{$_};| } @columns; | ||
| 728 | |||
| 729 | eval $code; | ||
| 730 | |||
| 731 | } elsif ($command eq 'add') { | ||
| 732 | $_->{$single} = ''; | ||
| 733 | |||
| 734 | } elsif ($command eq 'drop') { | ||
| 735 | delete $_->{$single}; | ||
| 736 | } | ||
| 737 | } | ||
| 738 | } | ||
| 739 | |||
| 740 | if ( ($status <= 0) || ($command ne 'select') ) { | ||
| 741 | return $status; | ||
| 742 | } else { | ||
| 743 | return $results; | ||
| 744 | } | ||
| 745 | } | ||
| 746 | |||
| 747 | sub check_for_reload | ||
| 748 | { | ||
| 749 | my ($self, $file) = @_; | ||
| 750 | my ($table, $path, $status); | ||
| 751 | |||
| 752 | return unless ($file); | ||
| 753 | |||
| 754 | ($path, $table) = $self->get_path_info ($file); | ||
| 755 | |||
| 756 | $file = $path . $table if ($table eq $file); | ||
| 757 | $status = 1; | ||
| 758 | |||
| 759 | if ( ($self->{table} ne $table) || ($self->{file} ne $file) ) { | ||
| 760 | stat ($file); | ||
| 761 | |||
| 762 | if ( (-e _) && (-T _) && (-s _) && (-r _) ) { | ||
| 763 | |||
| 764 | $self->{table} = $table; | ||
| 765 | $self->{file} = $file; | ||
| 766 | $status = $self->load_database ($file); | ||
| 767 | |||
| 768 | } else { | ||
| 769 | $status = 0; | ||
| 770 | } | ||
| 771 | } | ||
| 772 | |||
| 773 | return $status; | ||
| 774 | } | ||
| 775 | |||
| 776 | sub select | ||
| 777 | { | ||
| 778 | my ($self, $query) = @_; | ||
| 779 | my ($regex, $path, $columns, $table, $extra, $condition, $values_or_error); | ||
| 780 | |||
| 781 | $regex = $self->{_select}; | ||
| 782 | $path = $self->{path}; | ||
| 783 | |||
| 784 | if ($query =~ /^select\s+ # Keyword | ||
| 785 | ($regex)\s+ # Columns | ||
| 786 | from\s+ # 'from' | ||
| 787 | ($path)(.*)$/iox) { | ||
| 788 | |||
| 789 | ($columns, $table, $extra) = ($1, $2, $3); | ||
| 790 | |||
| 791 | if ($extra =~ /^\s+where\s+(.+)$/i) { | ||
| 792 | $condition = $self->parse_expression ($1); | ||
| 793 | } | ||
| 794 | |||
| 795 | $self->check_for_reload ($table) || return (-501); | ||
| 796 | |||
| 797 | $columns = join (',', @{ $self->{order} }) if ($columns eq '*'); | ||
| 798 | $columns =~ s/\s//g; | ||
| 799 | |||
| 800 | $self->check_columns ($columns) || return (-502); | ||
| 801 | |||
| 802 | $values_or_error = $self->parse_columns ('select', $columns, | ||
| 803 | $condition); | ||
| 804 | |||
| 805 | return $values_or_error; | ||
| 806 | } else { | ||
| 807 | return (-503); | ||
| 808 | } | ||
| 809 | } | ||
| 810 | |||
| 811 | sub update | ||
| 812 | { | ||
| 813 | my ($self, $query) = @_; | ||
| 814 | my ($path, $regex, $table, $extra, $condition, $all_columns, | ||
| 815 | $columns, $status); | ||
| 816 | |||
| 817 | ##++ | ||
| 818 | ## Hack to allow parenthesis to be escaped! | ||
| 819 | ##-- | ||
| 820 | |||
| 821 | $query =~ s/\\([()])/sprintf ("%%\0%d: ", ord ($1))/ge; | ||
| 822 | $path = $self->{path}; | ||
| 823 | $regex = $self->{column}; | ||
| 824 | |||
| 825 | if ($query =~ /^update\s+($path)\s+set\s+(.+)$/io) { | ||
| 826 | ($table, $extra) = ($1, $2); | ||
| 827 | |||
| 828 | $all_columns = {}; | ||
| 829 | $columns = ''; | ||
| 830 | |||
| 831 | $extra =~ s|($regex)\s*=\s*(\(.+?\))(?:\s*,\s*)?| | ||
| 832 | my ($key, $value) = ($1, $2); | ||
| 833 | |||
| 834 | $value =~ s/%\0(\d+): /pack ("C", $1)/ge; | ||
| 835 | $value = $self->parse_expression ($value); | ||
| 836 | |||
| 837 | $all_columns->{$key} = $value; | ||
| 838 | |||
| 839 | ''; | ||
| 840 | |goe; | ||
| 841 | |||
| 842 | $columns = join (',', keys %$all_columns); | ||
| 843 | $condition = ($extra =~ /^\s*where\s+(.+)$/i) ? $1 : ''; | ||
| 844 | |||
| 845 | $self->check_for_reload ($table) || return (-501); | ||
| 846 | $self->check_columns ($columns) || return (-502); | ||
| 847 | |||
| 848 | $condition = $self->parse_expression ($condition); | ||
| 849 | |||
| 850 | $status = $self->parse_columns ('update', $columns, | ||
| 851 | $condition, | ||
| 852 | $all_columns); | ||
| 853 | |||
| 854 | return ($status); | ||
| 855 | } else { | ||
| 856 | return (-504); | ||
| 857 | } | ||
| 858 | } | ||
| 859 | |||
| 860 | sub delete | ||
| 861 | { | ||
| 862 | my ($self, $query) = @_; | ||
| 863 | my ($path, $table, $condition, $status); | ||
| 864 | |||
| 865 | $path = $self->{path}; | ||
| 866 | |||
| 867 | if ($query =~ /^delete\s+from\s+($path)\s+where\s+(.+)$/io) { | ||
| 868 | $table = $1; | ||
| 869 | $condition = $self->parse_expression ($2); | ||
| 870 | |||
| 871 | $self->check_for_reload ($table) || return (-501); | ||
| 872 | |||
| 873 | $status = $self->delete_rows ($condition); | ||
| 874 | |||
| 875 | return $status; | ||
| 876 | } else { | ||
| 877 | return (-505); | ||
| 878 | } | ||
| 879 | } | ||
| 880 | |||
| 881 | sub delete_rows | ||
| 882 | { | ||
| 883 | my ($self, $condition) = @_; | ||
| 884 | my ($status, $loop); | ||
| 885 | |||
| 886 | local $SIG{'__WARN__'} = sub { $status = -510 }; | ||
| 887 | local $^W = 0; | ||
| 888 | |||
| 889 | $status = 1; | ||
| 890 | |||
| 891 | for ($loop=0; $loop < scalar @{ $self->{records} }; $loop++) { | ||
| 892 | $_ = $self->{records}->[$loop]; | ||
| 893 | |||
| 894 | $self->{records}->[$loop] = undef if (eval $condition); | ||
| 895 | } | ||
| 896 | |||
| 897 | return $status; | ||
| 898 | } | ||
| 899 | |||
| 900 | sub alter | ||
| 901 | { | ||
| 902 | my ($self, $query) = @_; | ||
| 903 | my ($path, $regex, $table, $extra, $type, $column, $count, $status); | ||
| 904 | |||
| 905 | $path = $self->{path}; | ||
| 906 | $regex = $self->{column}; | ||
| 907 | |||
| 908 | if ($query =~ /^alter\s+table\s+($path)\s+(.+)$/io) { | ||
| 909 | ($table, $extra) = ($1, $2); | ||
| 910 | |||
| 911 | if ($extra =~ /^(add|drop)\s+column\s+($regex)$/io) { | ||
| 912 | ($type, $column) = ($1, $2); | ||
| 913 | |||
| 914 | $self->check_for_reload ($table) || return (-501); | ||
| 915 | |||
| 916 | if ($type eq 'add') { | ||
| 917 | $self->{fields}->{$column} = 1; | ||
| 918 | push (@{ $self->{order} }, $column); | ||
| 919 | |||
| 920 | } else { | ||
| 921 | $self->check_columns ($column) || return (-502); | ||
| 922 | |||
| 923 | $count = -1; | ||
| 924 | |||
| 925 | foreach (@{ $self->{order} }) { | ||
| 926 | ++$count; | ||
| 927 | last if ($_ eq $column); | ||
| 928 | } | ||
| 929 | |||
| 930 | splice (@{ $self->{order} }, $count, 1); | ||
| 931 | delete $self->{fields}->{$column}; | ||
| 932 | } | ||
| 933 | |||
| 934 | $status = $self->parse_columns ("\L$type\E", $column); | ||
| 935 | |||
| 936 | return $status; | ||
| 937 | } else { | ||
| 938 | return (-506); | ||
| 939 | } | ||
| 940 | } else { | ||
| 941 | return (-507); | ||
| 942 | } | ||
| 943 | } | ||
| 944 | |||
| 945 | sub insert | ||
| 946 | { | ||
| 947 | my ($self, $query) = @_; | ||
| 948 | my ($path, $table, $columns, $values, $status); | ||
| 949 | |||
| 950 | $path = $self->{path}; | ||
| 951 | |||
| 952 | if ($query =~ /^insert\s+into\s+ # Keyword | ||
| 953 | ($path)\s+ # Table | ||
| 954 | \((.+?)\)\s+ # Keys | ||
| 955 | values\s+ # 'values' | ||
| 956 | \((.+)\)$/ixo) { | ||
| 957 | |||
| 958 | ($table, $columns, $values) = ($1, $2, $3); | ||
| 959 | |||
| 960 | $columns =~ s/\s//g; | ||
| 961 | |||
| 962 | $self->check_for_reload ($table) || return (-501); | ||
| 963 | $self->check_columns ($columns) || return (-502); | ||
| 964 | |||
| 965 | $status = $self->insert_data ($columns, $values); | ||
| 966 | |||
| 967 | return $status; | ||
| 968 | } else { | ||
| 969 | return (-508); | ||
| 970 | } | ||
| 971 | } | ||
| 972 | |||
| 973 | sub insert_data | ||
| 974 | { | ||
| 975 | my ($self, $column_string, $value_string) = @_; | ||
| 976 | my (@columns, @values, $hash, $loop, $column); | ||
| 977 | |||
| 978 | @columns = split (/,/, $column_string); | ||
| 979 | @values = $self->quotewords (',\s*', $value_string); | ||
| 980 | |||
| 981 | if ($#columns == $#values) { | ||
| 982 | $hash = {}; | ||
| 983 | |||
| 984 | for ($loop=0; $loop <= $#columns; $loop++) { | ||
| 985 | $column = $columns[$loop]; | ||
| 986 | |||
| 987 | if ($self->{fields}->{$column}) { | ||
| 988 | $hash->{$column} = $values[$loop]; | ||
| 989 | } | ||
| 990 | } | ||
| 991 | |||
| 992 | push @{ $self->{records} }, $hash; | ||
| 993 | |||
| 994 | return (1); | ||
| 995 | } else { | ||
| 996 | return (-509); | ||
| 997 | } | ||
| 998 | } | ||
| 999 | |||
| 1000 | sub write_file | ||
| 1001 | { | ||
| 1002 | my ($self, $new_file) = @_; | ||
| 1003 | my ($status, $write, $fields, $loop, $record, $record_string, | ||
| 1004 | $column, $value); | ||
| 1005 | |||
| 1006 | local (*FILE, $^W); | ||
| 1007 | |||
| 1008 | $^W = 0; | ||
| 1009 | $status = (scalar @{ $self->{records} }) ? 1 : -513; | ||
| 1010 | |||
| 1011 | if ( ($status >= 1) && (open (FILE, ">$new_file")) ) { | ||
| 1012 | eval { flock (FILE, $Sprite::LOCK_EX) || die }; | ||
| 1013 | |||
| 1014 | if ($@) { | ||
| 1015 | $self->lock || $self->display_error (-515); | ||
| 1016 | } | ||
| 1017 | |||
| 1018 | $write = $self->{_write}; | ||
| 1019 | $fields = join ($write, @{ $self->{order} }); | ||
| 1020 | |||
| 1021 | print FILE "$fields\n"; | ||
| 1022 | |||
| 1023 | for ($loop=0; $loop < scalar @{ $self->{records} }; $loop++) { | ||
| 1024 | $record = $self->{records}->[$loop]; | ||
| 1025 | |||
| 1026 | next unless (defined $record); | ||
| 1027 | |||
| 1028 | $record_string = ''; | ||
| 1029 | |||
| 1030 | foreach $column (@{ $self->{order} }) { | ||
| 1031 | $value = $record->{$column}; | ||
| 1032 | |||
| 1033 | if ($value =~ /(?:\Q$write\E)|(?:['"\\])/o) { | ||
| 1034 | $value =~ s/(["\\])/\\$1/g; | ||
| 1035 | $value = qq|"$value"|; | ||
| 1036 | } | ||
| 1037 | |||
| 1038 | $record_string .= "$write$value"; | ||
| 1039 | } | ||
| 1040 | |||
| 1041 | $record_string =~ s/^\Q$write\E//o; | ||
| 1042 | |||
| 1043 | print FILE "$record_string\n"; | ||
| 1044 | } | ||
| 1045 | |||
| 1046 | close (FILE); | ||
| 1047 | |||
| 1048 | $self->unlock || $self->display_error (-516); | ||
| 1049 | } else { | ||
| 1050 | $status = ($status < 1) ? $status : -511; | ||
| 1051 | } | ||
| 1052 | |||
| 1053 | return $status; | ||
| 1054 | } | ||
| 1055 | |||
| 1056 | sub load_database | ||
| 1057 | { | ||
| 1058 | my ($self, $file) = @_; | ||
| 1059 | my ($header, @fields, $no_fields, @record, $hash, $loop); | ||
| 1060 | |||
| 1061 | local (*FILE); | ||
| 1062 | |||
| 1063 | open (FILE, $file) || return (-501); | ||
| 1064 | |||
| 1065 | eval { flock (FILE, $Sprite::LOCK_SH) || die }; | ||
| 1066 | |||
| 1067 | if ($@) { | ||
| 1068 | $self->lock || $self->display_error (-515); | ||
| 1069 | } | ||
| 1070 | |||
| 1071 | $_ = <FILE>; | ||
| 1072 | |||
| 1073 | ($header) = /^ *(.*?) *$/; | ||
| 1074 | @fields = split (/$self->{_read}/o, $header); | ||
| 1075 | $no_fields = $#fields; | ||
| 1076 | |||
| 1077 | undef %{ $self->{fields} }; | ||
| 1078 | undef @{ $self->{order} }; | ||
| 1079 | |||
| 1080 | $self->{order} = [ @fields ]; | ||
| 1081 | |||
| 1082 | map { $self->{fields}->{$_} = 1 } @fields; | ||
| 1083 | undef @{ $self->{records} } if (scalar @{ $self->{records} }); | ||
| 1084 | |||
| 1085 | while (<FILE>) { | ||
| 1086 | chomp; | ||
| 1087 | |||
| 1088 | if (/['"\\]/) { | ||
| 1089 | s/((?!\\).)""/$1\\"/g; | ||
| 1090 | |||
| 1091 | @record = $self->quotewords ($self->{_read}, $_); | ||
| 1092 | } else { | ||
| 1093 | @record = split (/$self->{_read}/o, $_); | ||
| 1094 | } | ||
| 1095 | |||
| 1096 | next unless (scalar @record); | ||
| 1097 | |||
| 1098 | $hash = {}; | ||
| 1099 | |||
| 1100 | for ($loop=0; $loop <= $no_fields; $loop++) { | ||
| 1101 | $hash->{ $fields[$loop] } = $record[$loop]; | ||
| 1102 | } | ||
| 1103 | |||
| 1104 | push @{ $self->{records} }, $hash; | ||
| 1105 | } | ||
| 1106 | |||
| 1107 | close (FILE); | ||
| 1108 | |||
| 1109 | $self->unlock || $self->display_error (-516); | ||
| 1110 | |||
| 1111 | return (1); | ||
| 1112 | } | ||
| 1113 | |||
| 1114 | ##++ | ||
| 1115 | ## NOTE: Derived from lib/Text/ParseWords.pm. Thanks Hal! | ||
| 1116 | ##-- | ||
| 1117 | |||
| 1118 | sub quotewords | ||
| 1119 | { | ||
| 1120 | my ($self, $delim, $line) = @_; | ||
| 1121 | my (@words, $snippet, $field); | ||
| 1122 | |||
| 1123 | $_ = $line; | ||
| 1124 | |||
| 1125 | while (length) { | ||
| 1126 | $field = ''; | ||
| 1127 | |||
| 1128 | for (;;) { | ||
| 1129 | |||
| 1130 | $snippet = ''; | ||
| 1131 | |||
| 1132 | if (s/^"([^"\\]*(?:\\.[^"\\]*)*)"//) { | ||
| 1133 | $snippet = $1; | ||
| 1134 | } elsif (s/^'([^'\\]*(?:\\.[^'\\]*)*)'//) { | ||
| 1135 | $snippet = $1; | ||
| 1136 | } elsif (/^["']/) { # Don't bail out! | ||
| 1137 | $self->display_error (-512, $line); | ||
| 1138 | return; | ||
| 1139 | } elsif (s/^\\(.)//) { | ||
| 1140 | $snippet = $1; | ||
| 1141 | } elsif (!length || s/^$delim//) { | ||
| 1142 | last; | ||
| 1143 | } else { | ||
| 1144 | while (length && !(/^$delim/ || /^['"\\]/)) { | ||
| 1145 | $snippet .= substr ($_, 0, 1); | ||
| 1146 | s/^.//s; | ||
| 1147 | } | ||
| 1148 | } | ||
| 1149 | |||
| 1150 | $field .= $snippet; | ||
| 1151 | } | ||
| 1152 | |||
| 1153 | $field =~ s/\\(.)/$1/g; | ||
| 1154 | |||
| 1155 | push (@words, $field); | ||
| 1156 | } | ||
| 1157 | |||
| 1158 | return (@words); | ||
| 1159 | } | ||
| 1160 | |||
| 1161 | 1; | ||
| 1162 |