| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | DBD::XBase - DBI driver for XBase compatible database files | ||
| 5 | |||
| 6 | =cut | ||
| 7 | |||
| 8 | # ################################## | ||
| 9 | # Here starts the DBD::XBase package | ||
| 10 | |||
| 11 | package DBD::XBase; | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use DBI (); # we want DBI | ||
| 15 | use XBase; # and we want the basic XBase handling modules | ||
| 16 | use XBase::SQL; # including the SQL parsing routines | ||
| 17 | use Exporter; | ||
| 18 | |||
| 19 | use vars qw( $VERSION @ISA @EXPORT $err $errstr $drh $sqlstate ); | ||
| 20 | # a couple of global variables that may come handy | ||
| 21 | |||
| 22 | $VERSION = '1.06'; | ||
| 23 | |||
| 24 | $err = 0; | ||
| 25 | $errstr = ''; | ||
| 26 | $sqlstate = ''; | ||
| 27 | $drh = undef; | ||
| 28 | |||
| 29 | # The driver method creates the drivers instance; we store it in the | ||
| 30 | # global $drh variable to only load the driver once | ||
| 31 | sub driver { | ||
| 32 | return $drh if $drh; | ||
| 33 | my ($class, $attr) = @_; | ||
| 34 | $class .= '::dr'; | ||
| 35 | $drh = DBI::_new_drh($class, { | ||
| 36 | 'Name' => 'XBase', | ||
| 37 | 'Version' => $VERSION, | ||
| 38 | 'Err' => \$DBD::XBase::err, | ||
| 39 | 'Errstr' => \$DBD::XBase::errstr, | ||
| 40 | 'State' => \$DBD::XBase::sqlstate, | ||
| 41 | 'Attribution' => 'DBD::XBase by Jan Pazdziora', | ||
| 42 | }); | ||
| 43 | } | ||
| 44 | |||
| 45 | # The data_sources method should return list of possible "databases" | ||
| 46 | # for the driver. With DBD::XBase, the database is in fact a directory. | ||
| 47 | # So should we return all direcoties in current? Right now, we only | ||
| 48 | # return the current directory. | ||
| 49 | sub data_sources { | ||
| 50 | 'dbi:XBase:.'; | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | # ################## | ||
| 55 | # The driver package | ||
| 56 | |||
| 57 | package DBD::XBase::dr; | ||
| 58 | use strict; | ||
| 59 | $DBD::XBase::dr::imp_data_size = 0; | ||
| 60 | |||
| 61 | # The connect method returns a dbh; we require that the directory we | ||
| 62 | # want to search for tables exists. | ||
| 63 | sub connect { | ||
| 64 | my ($drh, $dsn) = @_; | ||
| 65 | $dsn = '.' if $dsn eq ''; | ||
| 66 | if (not -d $dsn) { | ||
| 67 | $drh->DBI::set_err(1, "Connect failed: directory `$dsn' doesn't exist"); | ||
| 68 | return; | ||
| 69 | } | ||
| 70 | DBI::_new_dbh($drh, { 'Name' => $dsn } ); | ||
| 71 | } | ||
| 72 | |||
| 73 | # We do not want to do anything upon disconnecting, but we might in | ||
| 74 | # the future (flush, close files) | ||
| 75 | sub disconnect_all { | ||
| 76 | 1; | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | # #################### | ||
| 81 | # The database package | ||
| 82 | |||
| 83 | package DBD::XBase::db; | ||
| 84 | use strict; | ||
| 85 | $DBD::XBase::db::imp_data_size = 0; | ||
| 86 | |||
| 87 | # The prepare method takes dbh and a SQL query and should return | ||
| 88 | # statement handler | ||
| 89 | sub prepare { | ||
| 90 | my ($dbh, $statement) = @_; | ||
| 91 | |||
| 92 | # we basically call XBase::SQL parsing and get an object | ||
| 93 | my $parsed_sql = parse XBase::SQL($statement); | ||
| 94 | ### use Data::Dumper; print Dumper $parsed_sql; | ||
| 95 | |||
| 96 | if (defined $parsed_sql->{'errstr'}) { | ||
| 97 | $dbh->DBI::set_err(2, $parsed_sql->{'errstr'}); | ||
| 98 | return; | ||
| 99 | } | ||
| 100 | |||
| 101 | # we create a new statement handler; the only thing the the | ||
| 102 | # specs requires us to do here (except parsing the query) is | ||
| 103 | # to se the number of bind parameters found, which we do; | ||
| 104 | # we do not set NUM_OF_FIELDS (which the specs doesn't require) | ||
| 105 | # since for select * we do not know the number yet, for example | ||
| 106 | DBI::_new_sth($dbh, { | ||
| 107 | 'Statement' => $statement, | ||
| 108 | 'xbase_parsed_sql' => $parsed_sql, | ||
| 109 | 'NUM_OF_PARAMS' => scalar(keys %{$parsed_sql->{'binds'}}), | ||
| 110 | }); | ||
| 111 | } | ||
| 112 | |||
| 113 | # Storing and fetching attributes in the database handler | ||
| 114 | sub STORE { | ||
| 115 | my ($dbh, $attrib, $value) = @_; | ||
| 116 | if ($attrib eq 'AutoCommit') { | ||
| 117 | unless ($value) { | ||
| 118 | die "Can't disable AutoCommit"; | ||
| 119 | } | ||
| 120 | return 1; | ||
| 121 | } elsif ($attrib =~ /^xbase_/) { | ||
| 122 | $dbh->{$attrib} = $value; return 1; | ||
| 123 | } | ||
| 124 | $dbh->DBD::_::db::STORE($attrib, $value); | ||
| 125 | } | ||
| 126 | sub FETCH { | ||
| 127 | my ($dbh, $attrib) = @_; | ||
| 128 | if ($attrib eq 'AutoCommit') { | ||
| 129 | return 1; | ||
| 130 | } elsif ($attrib =~ /^xbase_/) { | ||
| 131 | return $dbh->{$attrib}; | ||
| 132 | } | ||
| 133 | $dbh->DBD::_::db::FETCH($attrib); | ||
| 134 | } | ||
| 135 | |||
| 136 | # Method tables provides a list of tables in the directory | ||
| 137 | sub tables { | ||
| 138 | my $dbh = shift; | ||
| 139 | opendir DIR, $dbh->{'Name'} or return; | ||
| 140 | my @result = (); | ||
| 141 | while (defined(my $item = readdir DIR)) { | ||
| 142 | next unless $item =~ s/\.dbf$//i; | ||
| 143 | push @result, $item; | ||
| 144 | } | ||
| 145 | closedir DIR; | ||
| 146 | @result; | ||
| 147 | } | ||
| 148 | |||
| 149 | # Quoting method | ||
| 150 | sub quote { | ||
| 151 | my $text = $_[1]; | ||
| 152 | return 'NULL' unless defined $text; | ||
| 153 | $text =~ s/\\/\\\\/sg; | ||
| 154 | $text =~ s/\'/\\\'/sg; | ||
| 155 | return "'$text'"; | ||
| 156 | ### return "'\Q$text\E'"; | ||
| 157 | } | ||
| 158 | |||
| 159 | # Commit and rollback do not do anything usefull | ||
| 160 | sub commit { | ||
| 161 | warn "Commit ineffective while AutoCommit is on" | ||
| 162 | if $_[0]->FETCH('Warn'); | ||
| 163 | 1; | ||
| 164 | } | ||
| 165 | sub rollback { | ||
| 166 | warn "Rollback ineffective while AutoCommit is on" | ||
| 167 | if $_[0]->FETCH('Warn'); | ||
| 168 | 0; | ||
| 169 | } | ||
| 170 | |||
| 171 | # Upon disconnecting we close all tables | ||
| 172 | sub disconnect { | ||
| 173 | my $dbh = shift; | ||
| 174 | foreach my $table (keys %{$dbh->{'xbase_tables'}}) { | ||
| 175 | $dbh->{'xbase_tables'}->{$table}->close; | ||
| 176 | delete $dbh->{'xbase_tables'}{$table}; | ||
| 177 | } | ||
| 178 | 1; | ||
| 179 | } | ||
| 180 | |||
| 181 | # Table_info is a strange method that returns information about | ||
| 182 | # tables. There is not much we could say about the files so we only | ||
| 183 | # return list of them. | ||
| 184 | sub table_info { | ||
| 185 | my $dbh = shift; | ||
| 186 | my $sth = DBI::_new_sth($dbh, { 'xbase_lines' => | ||
| 187 | [ map { [ undef, undef, $_, 'TABLE', undef ] } $dbh->tables ] | ||
| 188 | } ); | ||
| 189 | $sth->STORE('NUM_OF_FIELDS', 5); | ||
| 190 | $sth->{'xbase_nondata_name'} = [ qw! TABLE_QUALIFIER TABLE_OWNER | ||
| 191 | TABLE_NAME TABLE_TYPE REMARKS !]; | ||
| 192 | $sth->execute and return $sth; | ||
| 193 | return; | ||
| 194 | } | ||
| 195 | |||
| 196 | # Very unreadable structure that the specs requires us to keep. It | ||
| 197 | # summarizes information about various data types we support. I do not | ||
| 198 | # hide the fact that this is not polished and probably not correct. | ||
| 199 | my @TYPE_INFO_ALL = ( | ||
| 200 | [ qw( TYPE_NAME DATA_TYPE PRECISION LITERAL_PREFIX LITERAL_SUFFIX CREATE_PARAMS NULLABLE CASE_SENSITIVE SEARCHABLE UNSIGNED_ATTRIBUTE MONEY AUTO_INCREMENT LOCAL_TYPE_NAME MINIMUM_SCALE MAXIMUM_SCALE ) ], | ||
| 201 | [ 'VARCHAR', DBI::SQL_VARCHAR, 65535, "'", "'", 'max length', 0, 1, 2, undef, 0, 0, undef, undef, undef ], | ||
| 202 | [ 'CHAR', DBI::SQL_CHAR, 65535, "'", "'", 'max length', 0, 1, 2, undef, 0, 0, undef, undef, undef ], | ||
| 203 | [ 'INTEGER', DBI::SQL_INTEGER, 0, '', '', 'number of digits', 1, 0, 2, 0, 0, 0, undef, 0, undef ], | ||
| 204 | [ 'FLOAT', DBI::SQL_FLOAT, 0, '', '', 'number of digits', 1, 0, 2, 0, 0, 0, undef, 0, undef ], | ||
| 205 | [ 'NUMERIC', DBI::SQL_NUMERIC, 0, '', '', 'number of digits', 1, 0, 2, 0, 0, 0, undef, 0, undef ], | ||
| 206 | [ 'BOOLEAN', DBI::SQL_BINARY, 0, '', '', undef, 1, 0, 2, 0, 0, 0, undef, 1, 1 ], | ||
| 207 | [ 'DATE', DBI::SQL_DATE, 0, '', '', 'number of digits', 1, 0, 2, 0, 0, 0, undef, 0, undef ], | ||
| 208 | [ 'TIME', DBI::SQL_TIME, 0, '', '', 'number of digits', 1, 0, 2, 0, 0, 0, undef, 0, undef ], | ||
| 209 | [ 'BLOB', DBI::SQL_LONGVARBINARY, 0, '', '', undef, 1, 0, 2, 0, 0, 0, undef, 0, undef ], | ||
| 210 | ); | ||
| 211 | |||
| 212 | my %TYPE_INFO_TYPES = map { ( $TYPE_INFO_ALL[$_][0] => $_ ) } ( 1 .. $#TYPE_INFO_ALL ); | ||
| 213 | my %REVTYPES = qw( C char N numeric F float L boolean D date M blob T time ); | ||
| 214 | my %REVSQLTYPES = map { ( $_ => $TYPE_INFO_ALL[ $TYPE_INFO_TYPES{ uc $REVTYPES{$_} } ][1] ) } keys %REVTYPES; | ||
| 215 | |||
| 216 | ### use Data::Dumper; print STDERR Dumper \@TYPE_INFO_ALL, \%TYPE_INFO_TYPES, \%REVTYPES, \%REVSQLTYPES; | ||
| 217 | |||
| 218 | sub type_info_all { | ||
| 219 | my $dbh = shift; | ||
| 220 | my $result = [ @TYPE_INFO_ALL ]; | ||
| 221 | my $i = 0; | ||
| 222 | my $hash = { map { ( $_ => $i++) } @{$result->[0]} }; | ||
| 223 | $result->[0] = $hash; | ||
| 224 | $result; | ||
| 225 | } | ||
| 226 | sub type_info { | ||
| 227 | my ($dbh, $type) = @_; | ||
| 228 | my @result = (); | ||
| 229 | for my $row ( 1 .. $#TYPE_INFO_ALL ) { | ||
| 230 | if ($type == DBI::SQL_ALL_TYPES or $type == $TYPE_INFO_ALL[$row][1]) | ||
| 231 | { push @result, { map { ( $TYPE_INFO_ALL[0][$_] => $TYPE_INFO_ALL[$row][$_] ) } ( 0 .. $#{$TYPE_INFO_ALL[0]} ) } } | ||
| 232 | } | ||
| 233 | @result; | ||
| 234 | } | ||
| 235 | |||
| 236 | sub DESTROY { | ||
| 237 | # To avoid autoloaded DESTROY | ||
| 238 | } | ||
| 239 | |||
| 240 | |||
| 241 | # ##################### | ||
| 242 | # The statement package | ||
| 243 | |||
| 244 | package DBD::XBase::st; | ||
| 245 | use strict; | ||
| 246 | $DBD::XBase::st::imp_data_size = 0; | ||
| 247 | |||
| 248 | # Binding of parameters: numbers are converted to :pnumber form, | ||
| 249 | # values are stored in the sth->{'xbase_bind_values'}->name of the | ||
| 250 | # parameter hash | ||
| 251 | sub bind_param { | ||
| 252 | my ($sth, $parameter) = (shift, shift); | ||
| 253 | if ($parameter =~ /^\d+$/) { $parameter = ':p'.$parameter; } | ||
| 254 | $sth->{'xbase_bind_values'}{$parameter} = shift; | ||
| 255 | 1; | ||
| 256 | } | ||
| 257 | |||
| 258 | # Returns number of rows fetched until now | ||
| 259 | sub rows { | ||
| 260 | defined $_[0]->{'xbase_rows'} ? $_[0]->{'xbase_rows'} : -1; | ||
| 261 | } | ||
| 262 | |||
| 263 | sub _set_rows { | ||
| 264 | my $sth = shift; | ||
| 265 | if (not @_ or not defined $_[0]) { | ||
| 266 | $sth->{'xbase_rows'} = undef; return -1; | ||
| 267 | } | ||
| 268 | $sth->{'xbase_rows'} = ( $_[0] ? $_[0] : '0E0' ); | ||
| 269 | } | ||
| 270 | # Execute the current statement, possibly binding parameters. For | ||
| 271 | # nonselect commands the actions needs to be done here, for select we | ||
| 272 | # just create the cursor and wait for fetchrows | ||
| 273 | sub execute { | ||
| 274 | my $sth = shift; | ||
| 275 | |||
| 276 | # the binds_order arrayref holds the conversion from the first | ||
| 277 | # occurence of the named parameter to its name; | ||
| 278 | # we bind the parameters here | ||
| 279 | my $parsed_sql = $sth->{'xbase_parsed_sql'}; | ||
| 280 | for (my $i = 0; $i < @_; $i++) { | ||
| 281 | $sth->bind_param($parsed_sql->{'binds_order'}[$i], $_[$i]); | ||
| 282 | } | ||
| 283 | |||
| 284 | # binded parameters | ||
| 285 | my $bind_values = $sth->{'xbase_bind_values'}; | ||
| 286 | |||
| 287 | if (defined $sth->{'xbase_lines'}) { return -1; } | ||
| 288 | |||
| 289 | # cancel the count of rows done in the previous run, this is a | ||
| 290 | # new execute | ||
| 291 | $sth->{'xbase_rows'} = undef; | ||
| 292 | |||
| 293 | # we'll nee dbh, table name and to command to do with them | ||
| 294 | my $dbh = $sth->{'Database'}; | ||
| 295 | my $table = $parsed_sql->{'table'}[0]; | ||
| 296 | my $command = $parsed_sql->{'command'}; | ||
| 297 | |||
| 298 | # create table first; we just create it and are done | ||
| 299 | if ($command eq 'create') { | ||
| 300 | my $filename = $dbh->{'Name'} . '/' . $table; | ||
| 301 | my %opts; | ||
| 302 | # get the name and the fields info | ||
| 303 | @opts{ qw( name field_names field_types field_lengths | ||
| 304 | field_decimals ) } = | ||
| 305 | ( $filename, @{$parsed_sql}{ qw( createfields | ||
| 306 | createtypes createlengths createdecimals ) } ); | ||
| 307 | # try to create the table (and memo automatically) | ||
| 308 | my $xbase = XBase->create(%opts) or do { | ||
| 309 | $sth->DBI::set_err(10, XBase->errstr()); | ||
| 310 | return; | ||
| 311 | }; | ||
| 312 | # keep the table open | ||
| 313 | $dbh->{'xbase_tables'}->{$table} = $xbase; | ||
| 314 | return $sth->DBD::XBase::st::_set_rows(0); # return true | ||
| 315 | } | ||
| 316 | |||
| 317 | # let's see if we've already opened the table | ||
| 318 | my $xbase = $dbh->{'xbase_tables'}->{$table}; | ||
| 319 | if (not defined $xbase) { | ||
| 320 | # if not, open the table now | ||
| 321 | my $filename = $dbh->{'Name'} . '/' . $table; | ||
| 322 | my %opts = ('name' => $filename); | ||
| 323 | $opts{'ignorememo'} = 1 if $dbh->{'xbase_ignorememo'}; | ||
| 324 | # try to open the table using XBase.pm | ||
| 325 | $xbase = new XBase(%opts) or do { | ||
| 326 | $sth->DBI::set_err(3, "Table $table not found: " | ||
| 327 | . XBase->errstr()); | ||
| 328 | return; | ||
| 329 | }; | ||
| 330 | $dbh->{'xbase_tables'}->{$table} = $xbase; | ||
| 331 | } | ||
| 332 | |||
| 333 | # the following is not multiple-statements safe -- I'd overwrite | ||
| 334 | # the attribute here; but I do not think anybody needs | ||
| 335 | # ChopBlanks = 0 anyway | ||
| 336 | if (defined $parsed_sql->{'ChopBlanks'}) { | ||
| 337 | $xbase->{'ChopBlanks'} = $parsed_sql->{'ChopBlanks'}; | ||
| 338 | } | ||
| 339 | $parsed_sql->{'ChopBlanks'} = \$xbase->{'ChopBlanks'}; | ||
| 340 | # I cannot see what I meant by this line -- never mind | ||
| 341 | |||
| 342 | # the array usedfields holds a list of field names that were | ||
| 343 | # explicitely mentioned somewhere in the SQL query -- select | ||
| 344 | # fields list, where clause, set clauses in update ... | ||
| 345 | # we'll try to make a list of those that do not exist in the table | ||
| 346 | my %nonexist; | ||
| 347 | for my $field (@{$parsed_sql->{'usedfields'}}) { | ||
| 348 | $nonexist{$field} = 1 unless defined $xbase->field_type($field); | ||
| 349 | } | ||
| 350 | if (keys %nonexist) { | ||
| 351 | $sth->DBI::set_err(4, | ||
| 352 | sprintf 'Field %s not found in table %s', | ||
| 353 | join(', ', sort keys %nonexist), $table); | ||
| 354 | return; | ||
| 355 | } | ||
| 356 | |||
| 357 | # inserting values means appending a new row with reasonable | ||
| 358 | # values; the insertfn function expects the TABLE object and | ||
| 359 | # the BIND hash (it doesn' make use of them at the moment, | ||
| 360 | # AFAIK, because only constants are supported), it returns list | ||
| 361 | # of values | ||
| 362 | if ($command eq 'insert') { | ||
| 363 | my $last = $xbase->last_record; | ||
| 364 | my @values = &{$parsed_sql->{'insertfn'}}($xbase, $bind_values); | ||
| 365 | |||
| 366 | ### here, we'd really need a check for too many or too | ||
| 367 | ### few values | ||
| 368 | if (defined $parsed_sql->{'insertfields'}) { | ||
| 369 | my %newval; | ||
| 370 | @newval{ @{$parsed_sql->{'insertfields'} } } = @values; | ||
| 371 | @values = @newval{ $xbase->field_names }; | ||
| 372 | } | ||
| 373 | $xbase->set_record($last + 1, @values) or do { | ||
| 374 | $sth->DBI::set_err(49,'Insert failed: '.$xbase->errstr); | ||
| 375 | return; | ||
| 376 | }; | ||
| 377 | return $sth->DBD::XBase::st::_set_rows(1); # we've added one row | ||
| 378 | } | ||
| 379 | |||
| 380 | |||
| 381 | # rows? what do we need rows here for? never mind. | ||
| 382 | my $rows; | ||
| 383 | |||
| 384 | # wherefn is defined if the statement had where clause; it | ||
| 385 | # should be called with $TABLE, $VALUES and $BIND parameters | ||
| 386 | my $wherefn = $parsed_sql->{'wherefn'}; | ||
| 387 | |||
| 388 | # we expand selectall to list of fields | ||
| 389 | if (defined $parsed_sql->{'selectall'}) { | ||
| 390 | $parsed_sql->{'selectnames'} = [ $xbase->field_names ]; | ||
| 391 | push @{$parsed_sql->{'usedfields'}}, $xbase->field_names; | ||
| 392 | $parsed_sql->{'selectfieldscount'} = scalar $xbase->field_names; | ||
| 393 | } | ||
| 394 | |||
| 395 | # we only set NUM_OF_FIELDS for select command -- which is | ||
| 396 | # exactly what selectfieldscount means | ||
| 397 | if (not $sth->FETCH('NUM_OF_FIELDS')) { | ||
| 398 | $sth->STORE('NUM_OF_FIELDS', $parsed_sql->{'selectfieldscount'}); | ||
| 399 | } | ||
| 400 | |||
| 401 | # this cursor will be needed, because both select and update and | ||
| 402 | # delete with where clause need to fetch the data first | ||
| 403 | my $cursor = $xbase->prepare_select(@{$parsed_sql->{'usedfields'}}); | ||
| 404 | |||
| 405 | |||
| 406 | # select with order by clause will be done using "substatement" | ||
| 407 | if ($command eq 'select' and defined $parsed_sql->{'orderfields'}) { | ||
| 408 | my @orderfields = @{$parsed_sql->{'orderfields'}}; | ||
| 409 | |||
| 410 | # make a copy of the $parsed_sql hash, but delete the | ||
| 411 | # orderfields value | ||
| 412 | my $subparsed_sql = { %$parsed_sql }; | ||
| 413 | delete $subparsed_sql->{'orderfields'}; | ||
| 414 | delete $subparsed_sql->{'selectall'}; | ||
| 415 | |||
| 416 | my $selectfn = $parsed_sql->{'selectfn'}; | ||
| 417 | $subparsed_sql->{'selectfn'} = sub { | ||
| 418 | my ($TABLE, $VALUES, $BINDS) = @_; | ||
| 419 | return map({ XBase::SQL::Expr->field($_, $TABLE, $VALUES)->value } @orderfields), &{$selectfn}($TABLE, $VALUES, $BINDS); | ||
| 420 | }; | ||
| 421 | ### use Data::Dumper; print STDERR Dumper $subparsed_sql; | ||
| 422 | $subparsed_sql->{'selectfieldscount'} += scalar(@orderfields); | ||
| 423 | |||
| 424 | # make new $sth | ||
| 425 | my $substh = DBI::_new_sth($dbh, { | ||
| 426 | 'Statement' => $sth->{'Statement'}, | ||
| 427 | 'xbase_parsed_sql' => $subparsed_sql, | ||
| 428 | }); | ||
| 429 | |||
| 430 | # bind all parameters in the substh | ||
| 431 | for my $key (keys %$bind_values) { | ||
| 432 | $substh->bind_param($key, $bind_values->{$key}); | ||
| 433 | } | ||
| 434 | |||
| 435 | # execute and fetch all rows | ||
| 436 | $substh->execute; | ||
| 437 | ### use Data::Dumper; print STDERR Dumper $substh->{'xbase_parsed_sql'}; | ||
| 438 | my $data = $substh->fetchall_arrayref; | ||
| 439 | |||
| 440 | my $sortfn = ''; | ||
| 441 | for (my $i = 0; $i < @orderfields; $i++) { | ||
| 442 | $sortfn .= ' or ' if $i > 0; | ||
| 443 | if ($xbase->field_type($orderfields[$i]) =~ /^[CML]$/) { | ||
| 444 | if (lc($parsed_sql->{'orderdescs'}[$i]) eq 'desc') { | ||
| 445 | $sortfn .= "\$_[1]->[$i] cmp \$_[0]->[$i]"; | ||
| 446 | } else { | ||
| 447 | $sortfn .= "\$_[0]->[$i] cmp \$_[1]->[$i]"; | ||
| 448 | } | ||
| 449 | } else { | ||
| 450 | if (lc($parsed_sql->{'orderdescs'}[$i]) eq 'desc') { | ||
| 451 | $sortfn .= "\$_[1]->[$i] <=> \$_[0]->[$i]"; | ||
| 452 | } else { | ||
| 453 | $sortfn .= "\$_[0]->[$i] <=> \$_[1]->[$i]"; | ||
| 454 | } | ||
| 455 | } | ||
| 456 | } | ||
| 457 | my $fn = eval "sub { $sortfn }"; | ||
| 458 | # sort them and store in xbase_lines | ||
| 459 | $sth->{'xbase_lines'} = | ||
| 460 | [ map { [ @{$_}[scalar(@orderfields) .. scalar(@$_) - 1 ] ] } | ||
| 461 | sort { &{$fn}($a, $b) } @$data ]; | ||
| 462 | } elsif ($command eq 'select') { | ||
| 463 | $sth->{'xbase_cursor'} = $cursor; | ||
| 464 | } elsif ($command eq 'delete') { | ||
| 465 | if (not defined $wherefn) { | ||
| 466 | my $last = $xbase->last_record; | ||
| 467 | for (my $i = 0; $i <= $last; $i++) { | ||
| 468 | if (not (($xbase->get_record_nf($i, 0))[0])) { | ||
| 469 | $xbase->delete_record($i); | ||
| 470 | $rows = 0 unless defined $rows; | ||
| 471 | $rows++; | ||
| 472 | } | ||
| 473 | } | ||
| 474 | } else { | ||
| 475 | my $values; | ||
| 476 | while (defined($values = $cursor->fetch_hashref)) { | ||
| 477 | next unless &{$wherefn}($xbase, $values, | ||
| 478 | $bind_values, 0); | ||
| 479 | $xbase->delete_record($cursor->last_fetched); | ||
| 480 | $rows = 0 unless defined $rows; | ||
| 481 | $rows++; | ||
| 482 | } | ||
| 483 | } | ||
| 484 | } elsif ($command eq 'update') { | ||
| 485 | my $values; | ||
| 486 | while (defined($values = $cursor->fetch_hashref)) { | ||
| 487 | next if defined $wherefn and not | ||
| 488 | &{$wherefn}($xbase, $values, $bind_values); | ||
| 489 | my %newval; | ||
| 490 | @newval{ @{$parsed_sql->{'updatefields'}} } = | ||
| 491 | &{$parsed_sql->{'updatefn'}}($xbase, $values, | ||
| 492 | $bind_values); | ||
| 493 | $xbase->update_record_hash($cursor->last_fetched, %newval); | ||
| 494 | $rows = 0 unless defined $rows; | ||
| 495 | $rows++; | ||
| 496 | } | ||
| 497 | } elsif ($command eq 'drop') { | ||
| 498 | # dropping the table is really easy | ||
| 499 | $xbase->drop or do { | ||
| 500 | $sth->DBI::set_err(60, "Dropping table $table failed: " | ||
| 501 | . $xbase->errstr); | ||
| 502 | return; | ||
| 503 | }; | ||
| 504 | delete $dbh->{'xbase_tables'}{$table}; | ||
| 505 | $rows = -1; | ||
| 506 | } | ||
| 507 | |||
| 508 | # finaly, set the number of rows (what if somebody will ask) and | ||
| 509 | # return it to curious crowds | ||
| 510 | return $sth->DBD::XBase::st::_set_rows($rows); | ||
| 511 | } | ||
| 512 | |||
| 513 | |||
| 514 | |||
| 515 | sub fetch { | ||
| 516 | my $sth = shift; | ||
| 517 | my $retarray; | ||
| 518 | if (defined $sth->{'xbase_lines'}) { | ||
| 519 | $retarray = shift @{$sth->{'xbase_lines'}}; | ||
| 520 | } elsif (defined $sth->{'xbase_cursor'}) { | ||
| 521 | my $cursor = $sth->{'xbase_cursor'}; | ||
| 522 | my $wherefn = $sth->{'xbase_parsed_sql'}{'wherefn'}; | ||
| 523 | |||
| 524 | my $xbase = $cursor->table; | ||
| 525 | my $values; | ||
| 526 | while (defined($values = $cursor->fetch_hashref)) { | ||
| 527 | ### use Data::Dumper; print Dumper $sth->{'xbase_bind_values'}; | ||
| 528 | next if defined $wherefn and not | ||
| 529 | &{$wherefn}($xbase, $values, | ||
| 530 | $sth->{'xbase_bind_values'}); | ||
| 531 | last; | ||
| 532 | } | ||
| 533 | $retarray = [ &{$sth->{'xbase_parsed_sql'}{'selectfn'}}($xbase, $values, $sth->{'xbase_bind_values'}) ] | ||
| 534 | if defined $values; | ||
| 535 | } | ||
| 536 | |||
| 537 | ### use Data::Dumper; print Dumper $retarray; | ||
| 538 | |||
| 539 | return unless defined $retarray; | ||
| 540 | |||
| 541 | ### print STDERR "sth->{'NUM_OF_FIELDS'}: $sth->{'NUM_OF_FIELDS'} sth->{'NUM_OF_PARAMS'}: $sth->{'NUM_OF_PARAMS'}\n"; | ||
| 542 | |||
| 543 | |||
| 544 | $sth->_set_fbav($retarray); return $retarray; | ||
| 545 | |||
| 546 | my $i = 0; | ||
| 547 | for my $ref ( @{$sth->{'xbase_bind_col'}} ) { | ||
| 548 | next unless defined $ref; | ||
| 549 | $$ref = $retarray->[$i]; | ||
| 550 | } continue { | ||
| 551 | $i++; | ||
| 552 | } | ||
| 553 | |||
| 554 | return $retarray; | ||
| 555 | } | ||
| 556 | *fetchrow_arrayref = \&fetch; | ||
| 557 | |||
| 558 | sub FETCH { | ||
| 559 | my ($sth, $attrib) = @_; | ||
| 560 | my $parsed_sql = $sth->{'xbase_parsed_sql'}; | ||
| 561 | if ($attrib =~ /^xbase_/) { | ||
| 562 | return $sth->{$attrib}; | ||
| 563 | } | ||
| 564 | if ($attrib eq 'NAME') { | ||
| 565 | if (defined $sth->{'xbase_nondata_name'}) { | ||
| 566 | return $sth->{'xbase_nondata_name'}; | ||
| 567 | } | ||
| 568 | return [ @{$parsed_sql->{'selectnames'}} ]; | ||
| 569 | } elsif ($attrib eq 'NULLABLE') { | ||
| 570 | return [ (1) x scalar(@{$parsed_sql->{'selectnames'}}) ]; | ||
| 571 | } elsif ($attrib eq 'TYPE') { | ||
| 572 | return [ map { ( $REVSQLTYPES{$_} or undef ) } | ||
| 573 | map { ( $sth->{'Database'}->{'xbase_tables'}->{$parsed_sql->{'table'}[0]}->field_type($_) or undef ) } | ||
| 574 | @{$parsed_sql->{'selectnames'}} ]; | ||
| 575 | } elsif ($attrib eq 'PRECISION') { | ||
| 576 | return [ map { $sth->{'Database'}->{'xbase_tables'}->{$parsed_sql->{'table'}[0]}->field_length($_) } | ||
| 577 | @{$parsed_sql->{'selectnames'}} ]; | ||
| 578 | } elsif ($attrib eq 'SCALE') { | ||
| 579 | return [ map { $sth->{'Database'}->{'xbase_tables'}->{$parsed_sql->{'table'}[0]}->field_decimal($_) } | ||
| 580 | @{$parsed_sql->{'selectnames'}} ]; | ||
| 581 | } elsif ($attrib eq 'ChopBlanks') { | ||
| 582 | return $parsed_sql->{'ChopBlanks'}; | ||
| 583 | } else { | ||
| 584 | return $sth->DBD::_::st::FETCH($attrib); | ||
| 585 | } | ||
| 586 | } | ||
| 587 | sub STORE { | ||
| 588 | my ($sth, $attrib, $value) = @_; | ||
| 589 | if ($attrib =~ /^xbase_/) { | ||
| 590 | $sth->{$attrib} = $value; | ||
| 591 | } | ||
| 592 | if ($attrib eq 'ChopBlanks') { | ||
| 593 | $sth->{'xbase_parsed_sql'}->{'ChopBlanks'} = $value; | ||
| 594 | } | ||
| 595 | return $sth->DBD::_::st::STORE($attrib, $value); | ||
| 596 | } | ||
| 597 | |||
| 598 | sub finish { 1; } | ||
| 599 | |||
| 600 | sub DESTROY { } | ||
| 601 | |||
| 602 | 1; | ||
| 603 | |||
| 604 | __END__ |