| 1 | package Database::Wrapper; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use base qw(Exporter); | ||
| 6 | |||
| 7 | our $VERSION = "1.04"; | ||
| 8 | # $Id: Wrapper.pm,v 1.13 2005/11/26 00:37:34 incorpoc Exp $ | ||
| 9 | |||
| 10 | use DBI; | ||
| 11 | use lib qw(..); | ||
| 12 | |||
| 13 | # Documentation and examples are at the end of this file after '__END__' | ||
| 14 | |||
| 15 | our $ConnectionError = undef; # Only used for connection errors | ||
| 16 | our $WarnDifferentPrepare = 0; | ||
| 17 | |||
| 18 | use constant RETURNTYPE_NONE => 0; | ||
| 19 | use constant RETURNTYPE_SUCCESS => 1; | ||
| 20 | use constant RETURNTYPE_VALUE => 2; # The value of the first field of the first record | ||
| 21 | use constant RETURNTYPE_ALLVALUES => 7; # Array of all the first field of all records | ||
| 22 | use constant RETURNTYPE_HASHREF => 3; # The first record as a hash ref | ||
| 23 | use constant RETURNTYPE_ARRAYREF => 4; # The first record as an array ref | ||
| 24 | use constant RETURNTYPE_ARRAYREFOFHASHREFS => 5; # All records as hash refs | ||
| 25 | use constant RETURNTYPE_ARRAYREFOFARRAYREFS => 6; # All records as array refs | ||
| 26 | |||
| 27 | use constant CONNECTION_TYPE_MySQL => 1; | ||
| 28 | use constant CONNECTION_TYPE_ODBC => 2; | ||
| 29 | use constant CONNECTION_TYPE_Postgres => 3; | ||
| 30 | use constant CONNECTION_TYPE_SQLite => 4; | ||
| 31 | |||
| 32 | my @aReturnTypes = qw(RETURNTYPE_NONE RETURNTYPE_SUCCESS RETURNTYPE_VALUE RETURNTYPE_ALLVALUES RETURNTYPE_HASHREF RETURNTYPE_ARRAYREF RETURNTYPE_ARRAYREFOFHASHREFS RETURNTYPE_ARRAYREFOFARRAYREFS); | ||
| 33 | my @aConnectionTypes = qw(CONNECTION_TYPE_MySQL CONNECTION_TYPE_ODBC CONNECTION_TYPE_Postgres CONNECTION_TYPE_SQLite); | ||
| 34 | our @EXPORT_OK = (@aReturnTypes, @aConnectionTypes); | ||
| 35 | our %EXPORT_TAGS = (RETURN_TYPES => \@aReturnTypes, CONNECTION_TYPES => \@aConnectionTypes); | ||
| 36 | |||
| 37 | my $rhDriverNames = | ||
| 38 | { | ||
| 39 | CONNECTION_TYPE_MySQL() => "mysql", | ||
| 40 | CONNECTION_TYPE_ODBC() => "ODBC", | ||
| 41 | CONNECTION_TYPE_Postgres() => "Pg", | ||
| 42 | CONNECTION_TYPE_SQLite() => "SQLite2", | ||
| 43 | }; | ||
| 44 | |||
| 45 | sub new($$) | ||
| 46 | { | ||
| 47 | my ($class, $rhConnection) = (shift, shift); | ||
| 48 | |||
| 49 | $ConnectionError = ""; | ||
| 50 | if(not defined $rhConnection) | ||
| 51 | { | ||
| 52 | $ConnectionError = "Database::Wrapper::new(), '$rhConnection' parameter undefined"; | ||
| 53 | return undef; | ||
| 54 | } | ||
| 55 | if(ref($rhConnection) ne "HASH") | ||
| 56 | { | ||
| 57 | $ConnectionError = "Database::Wrapper::new(), '$rhConnection' must be hash reference"; | ||
| 58 | return undef; | ||
| 59 | } | ||
| 60 | |||
| 61 | my $nConnectionType = $rhConnection->{ConnectionType}; | ||
| 62 | my $sDatabaseName = $rhConnection->{DatabaseName}; | ||
| 63 | my $sUser = $rhConnection->{User}; | ||
| 64 | my $sPassword = $rhConnection->{Password}; | ||
| 65 | my $sHost = $rhConnection->{Host}; | ||
| 66 | my $rhAttribs = (exists $rhConnection->{Attribs})? $rhConnection->{Attribs} : {}; | ||
| 67 | $rhAttribs->{RaiseError} = defined($rhAttribs->{RaiseError})? $rhAttribs->{RaiseError} : 1; | ||
| 68 | $rhAttribs->{PrintError} = defined($rhAttribs->{PrintError})? $rhAttribs->{PrintError} : 0; | ||
| 69 | $rhAttribs->{AutoCommit} = defined($rhAttribs->{AutoCommit})? $rhAttribs->{AutoCommit} : 1; | ||
| 70 | |||
| 71 | my $sDbiConnectionString = _MakeDsn($nConnectionType, $sDatabaseName, $sHost); | ||
| 72 | if(not defined $sDbiConnectionString) | ||
| 73 | { | ||
| 74 | # No need to set '$ConnectionError', it is set by _MakeDsn() | ||
| 75 | return undef; | ||
| 76 | } | ||
| 77 | |||
| 78 | my $rhConnectionData = | ||
| 79 | { | ||
| 80 | ConnectionType => $nConnectionType, | ||
| 81 | DatabaseName => $sDatabaseName, | ||
| 82 | User => $sUser, | ||
| 83 | Password => $sPassword, | ||
| 84 | Host => $sHost, | ||
| 85 | Attribs => $rhAttribs, | ||
| 86 | DbiConnectionString => $sDbiConnectionString, | ||
| 87 | }; | ||
| 88 | my $dbh = _Connect($rhConnectionData); | ||
| 89 | return undef | ||
| 90 | if(not defined $dbh); | ||
| 91 | |||
| 92 | my $self = | ||
| 93 | { | ||
| 94 | %$rhConnectionData, | ||
| 95 | dbh => $dbh, | ||
| 96 | Queries => {}, | ||
| 97 | LastError => "", | ||
| 98 | }; | ||
| 99 | SWITCH: | ||
| 100 | { | ||
| 101 | if($nConnectionType == CONNECTION_TYPE_MySQL) | ||
| 102 | { | ||
| 103 | require Database::Wrapper::MySQL; | ||
| 104 | return Database::Wrapper::MySQL->_Create($self); | ||
| 105 | } | ||
| 106 | if($nConnectionType == CONNECTION_TYPE_ODBC) | ||
| 107 | { | ||
| 108 | require Database::Wrapper::ODBC; | ||
| 109 | return Database::Wrapper::ODBC->_Create($self); | ||
| 110 | } | ||
| 111 | if($nConnectionType == CONNECTION_TYPE_Postgres) | ||
| 112 | { | ||
| 113 | require Database::Wrapper::Postgres; | ||
| 114 | return Database::Wrapper::Postgres->_Create($self); | ||
| 115 | } | ||
| 116 | if($nConnectionType == CONNECTION_TYPE_SQLite) | ||
| 117 | { | ||
| 118 | require Database::Wrapper::SQLite; | ||
| 119 | return Database::Wrapper::SQLite->_Create($self); | ||
| 120 | } | ||
| 121 | $ConnectionError = "Database::Wrapper::new(), unknown 'ConnectionType' given: '$nConnectionType'"; | ||
| 122 | return undef; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | # Utility function for db connection | ||
| 127 | # Side effects: sets '$ConnectionError' | ||
| 128 | sub _Connect($) | ||
| 129 | { | ||
| 130 | my ($rhConnectionData) = (shift); | ||
| 131 | |||
| 132 | my $dbh = undef; | ||
| 133 | eval | ||
| 134 | { | ||
| 135 | $dbh = DBI->connect($rhConnectionData->{DbiConnectionString}, $rhConnectionData->{User}, $rhConnectionData->{Password}, $rhConnectionData->{Attribs}); | ||
| 136 | }; | ||
| 137 | if($@) | ||
| 138 | { | ||
| 139 | if(defined $DBI::errstr) | ||
| 140 | { | ||
| 141 | my $sDBIError = DBI->errstr; | ||
| 142 | $ConnectionError = "DBI->connect() failed with error '$sDBIError'."; | ||
| 143 | return undef; | ||
| 144 | } | ||
| 145 | else | ||
| 146 | { | ||
| 147 | $ConnectionError = "DBI->connect() failed with error '$@'."; | ||
| 148 | return undef; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | if(not defined $dbh) | ||
| 152 | { | ||
| 153 | # Is this case possible? | ||
| 154 | my $sUndefError = DBI->errstr; | ||
| 155 | $ConnectionError = "DBI->connect() returned undef with error '$sUndefError'."; | ||
| 156 | return undef; | ||
| 157 | } | ||
| 158 | |||
| 159 | return $dbh; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub GetLastError($) | ||
| 163 | { | ||
| 164 | my ($self) = (shift); | ||
| 165 | |||
| 166 | return $self->{LastError}; | ||
| 167 | } | ||
| 168 | |||
| 169 | sub DESTROY($) | ||
| 170 | { | ||
| 171 | my ($self) = (shift); | ||
| 172 | |||
| 173 | return | ||
| 174 | if(not exists $self->{dbh}); | ||
| 175 | return | ||
| 176 | if(not defined $self->{dbh}); | ||
| 177 | $self->{dbh}->disconnect(); | ||
| 178 | $self->{dbh} = undef; | ||
| 179 | delete $self->{dbh}; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub commit($) | ||
| 183 | { | ||
| 184 | my ($self) = (shift); | ||
| 185 | |||
| 186 | my $dbh = $self->{dbh}; | ||
| 187 | return | ||
| 188 | if(not defined $dbh); | ||
| 189 | eval | ||
| 190 | { | ||
| 191 | $dbh->commit(); | ||
| 192 | }; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub GetDriverName($) | ||
| 196 | { | ||
| 197 | my ($nConnectionType) = (shift); | ||
| 198 | |||
| 199 | return undef | ||
| 200 | if(not exists $rhDriverNames->{$nConnectionType}); | ||
| 201 | |||
| 202 | return $rhDriverNames->{$nConnectionType}; | ||
| 203 | } | ||
| 204 | |||
| 205 | # A standard function for all the data definition query functions | ||
| 206 | |||
| 207 | # Receives a reference to the calling sub's '@_' | ||
| 208 | sub _GetConnectionData | ||
| 209 | { | ||
| 210 | my $raArgs = shift; | ||
| 211 | if(scalar @$raArgs == 0) | ||
| 212 | { | ||
| 213 | warn("No parameters supplied"); | ||
| 214 | return undef; | ||
| 215 | } | ||
| 216 | my $par = shift @$raArgs; | ||
| 217 | if(not defined $par) | ||
| 218 | { | ||
| 219 | warn("First parameter undef"); | ||
| 220 | return undef; | ||
| 221 | } | ||
| 222 | |||
| 223 | my $rhConnectionData = {}; | ||
| 224 | SWITCH: | ||
| 225 | { | ||
| 226 | if(ref($par) eq "") | ||
| 227 | { | ||
| 228 | # Check for the deprecated call | ||
| 229 | $rhConnectionData->{ConnectionType} = $par; | ||
| 230 | my $rhAttribs = shift @$raArgs; | ||
| 231 | if(ref($rhAttribs) ne "HASH") | ||
| 232 | { | ||
| 233 | warn("Hash expected as second parameter"); | ||
| 234 | return undef; | ||
| 235 | } | ||
| 236 | $rhConnectionData->{User} = $rhAttribs->{user}; | ||
| 237 | $rhConnectionData->{Password} = $rhAttribs->{password}; | ||
| 238 | $rhConnectionData->{Host} = $rhAttribs->{host}; | ||
| 239 | $rhConnectionData->{Port} = $rhAttribs->{port}; | ||
| 240 | last; | ||
| 241 | } | ||
| 242 | if(ref($par) eq "HASH") | ||
| 243 | { | ||
| 244 | # Static call | ||
| 245 | my $rhConnection = $par; | ||
| 246 | $rhConnectionData->{ConnectionType} = $rhConnection->{ConnectionType}; | ||
| 247 | $rhConnectionData->{User} = $rhConnection->{User}; | ||
| 248 | $rhConnectionData->{Password} = $rhConnection->{Password}; | ||
| 249 | $rhConnectionData->{Host} = $rhConnection->{Host}; | ||
| 250 | $rhConnectionData->{Port} = $rhConnection->{Port}; | ||
| 251 | last; | ||
| 252 | } | ||
| 253 | if(UNIVERSAL::isa($par, "Database::Wrapper")) | ||
| 254 | { | ||
| 255 | my $dbw = $par; | ||
| 256 | $rhConnectionData->{ConnectionType} = $dbw->{ConnectionType}; | ||
| 257 | $rhConnectionData->{User} = $dbw->{User}; | ||
| 258 | $rhConnectionData->{Password} = $dbw->{Password}; | ||
| 259 | $rhConnectionData->{Host} = $dbw->{Host}; | ||
| 260 | $rhConnectionData->{Port} = $dbw->{Port}; | ||
| 261 | last; | ||
| 262 | } | ||
| 263 | warn("Incorrect parameters"); | ||
| 264 | return undef; | ||
| 265 | } | ||
| 266 | my $sDriver = GetDriverName($rhConnectionData->{ConnectionType}); | ||
| 267 | return undef | ||
| 268 | if(not defined $sDriver); | ||
| 269 | |||
| 270 | $rhConnectionData->{Driver} = $sDriver; | ||
| 271 | |||
| 272 | return $rhConnectionData; | ||
| 273 | } | ||
| 274 | |||
| 275 | sub GetDatabaseNames | ||
| 276 | { | ||
| 277 | my $rhConnectionData = _GetConnectionData(\@_); | ||
| 278 | return undef | ||
| 279 | if(not defined $rhConnectionData); | ||
| 280 | |||
| 281 | my $rasDatabases = undef; | ||
| 282 | SWITCH: | ||
| 283 | { | ||
| 284 | if($rhConnectionData->{ConnectionType} == CONNECTION_TYPE_MySQL) | ||
| 285 | { | ||
| 286 | my $rhAttribs = | ||
| 287 | { | ||
| 288 | user => $rhConnectionData->{User}, | ||
| 289 | password => $rhConnectionData->{Password}, | ||
| 290 | host => $rhConnectionData->{Host}, | ||
| 291 | port => $rhConnectionData->{Port}, | ||
| 292 | }; | ||
| 293 | $rasDatabases = [DBI->data_sources($rhConnectionData->{Driver}, $rhAttribs)]; | ||
| 294 | # Names are returned in the form 'dbi:DRIVER_NAME:DATABASE_NAME' | ||
| 295 | # Strip off 'dbi:DRIVER_NAME:' | ||
| 296 | grep{s/.*?\:([^\:]*$)/$1/go} @$rasDatabases; | ||
| 297 | if(not defined $rasDatabases) | ||
| 298 | { | ||
| 299 | warn("data_sources() returned undef"); | ||
| 300 | return undef; | ||
| 301 | } | ||
| 302 | last; | ||
| 303 | } | ||
| 304 | if($rhConnectionData->{ConnectionType} == CONNECTION_TYPE_ODBC) | ||
| 305 | { | ||
| 306 | $rasDatabases = [DBI->data_sources($rhConnectionData->{Driver})]; | ||
| 307 | grep{s/.*?\:([^\:]*$)/$1/go} @$rasDatabases; | ||
| 308 | if(not defined $rasDatabases) | ||
| 309 | { | ||
| 310 | warn("data_sources() returned undef"); | ||
| 311 | return undef; | ||
| 312 | } | ||
| 313 | last; | ||
| 314 | } | ||
| 315 | if($rhConnectionData->{ConnectionType} == CONNECTION_TYPE_Postgres) | ||
| 316 | { | ||
| 317 | # Aargh! Listing databases in Postgres requires a connection to 'template1'. | ||
| 318 | # If the connection requires authentication, the module offers no way of passing user and password. | ||
| 319 | # It will only look for them in the environment. Seems like a stroke of genius ;-) | ||
| 320 | local $ENV{DBI_USER} = $rhConnectionData->{User}; | ||
| 321 | local $ENV{DBI_PASS} = $rhConnectionData->{Password}; | ||
| 322 | $rasDatabases = [DBI->data_sources($rhConnectionData->{Driver})]; | ||
| 323 | if(not defined $rasDatabases) | ||
| 324 | { | ||
| 325 | warn("data_sources() returned undef"); | ||
| 326 | return undef; | ||
| 327 | } | ||
| 328 | grep{s/.*?:dbname=(.*)/$1/go} @$rasDatabases; | ||
| 329 | last; | ||
| 330 | } | ||
| 331 | warn("Unrecognised ConnectionType '$rhConnectionData->{ConnectionType}'"); | ||
| 332 | return undef; | ||
| 333 | } | ||
| 334 | return $rasDatabases; | ||
| 335 | } | ||
| 336 | |||
| 337 | sub DatabaseExists | ||
| 338 | { | ||
| 339 | my ($parOne, $sName, $parThree) = (shift, shift, shift); | ||
| 340 | |||
| 341 | my $raDatabases = GetDatabaseNames($parOne, $parThree); | ||
| 342 | my $bFound = grep {$_ eq $sName} @$raDatabases; | ||
| 343 | |||
| 344 | return $bFound; | ||
| 345 | } | ||
| 346 | |||
| 347 | sub GetTableNames($) | ||
| 348 | { | ||
| 349 | my ($self) = (shift); | ||
| 350 | |||
| 351 | my $raTables = [$self->{dbh}->tables]; | ||
| 352 | |||
| 353 | return $raTables; | ||
| 354 | } | ||
| 355 | |||
| 356 | sub TableExists($$) | ||
| 357 | { | ||
| 358 | my ($self, $sTableToFind) = (shift, shift); | ||
| 359 | my $raTables = $self->GetTableNames(); | ||
| 360 | foreach my $sTable (@$raTables) | ||
| 361 | { | ||
| 362 | return 1 | ||
| 363 | if($sTable eq $sTableToFind); | ||
| 364 | } | ||
| 365 | |||
| 366 | return 0; | ||
| 367 | } | ||
| 368 | |||
| 369 | sub GetUserNames | ||
| 370 | { | ||
| 371 | warn("GetUserNames() has not been implemented for this Database type"); | ||
| 372 | |||
| 373 | return undef; | ||
| 374 | } | ||
| 375 | |||
| 376 | sub PrepareQuery($$$$) | ||
| 377 | { | ||
| 378 | my ($self, $sName, $sqry, $nReturnType) = (shift, shift, shift, shift); | ||
| 379 | |||
| 380 | $self->{LastError} = ""; | ||
| 381 | |||
| 382 | if(not defined $sName || $sName eq "") | ||
| 383 | { | ||
| 384 | $self->{LastError} = "Query name '$sName' invalid."; | ||
| 385 | return 0; | ||
| 386 | } | ||
| 387 | |||
| 388 | if($self->QueryExists($sName)) | ||
| 389 | { | ||
| 390 | if($WarnDifferentPrepare) | ||
| 391 | { | ||
| 392 | my $sthExisiting = $self->{Queries}->{$sName}; | ||
| 393 | my $sqryPrepared = $sthExisiting->{Statement}; | ||
| 394 | warn("Attempt to redefine '$sName'. Original query '$sqryPrepared', new definition '$sqry'") | ||
| 395 | if($sqryPrepared ne $sqry); | ||
| 396 | } | ||
| 397 | # Return 'Success', as you can call PrepareQuery() more than once on any given query. | ||
| 398 | return 1; | ||
| 399 | } | ||
| 400 | |||
| 401 | my $sth = undef; | ||
| 402 | eval | ||
| 403 | { | ||
| 404 | $sth = $self->{dbh}->prepare($sqry); | ||
| 405 | }; | ||
| 406 | if($@) | ||
| 407 | { | ||
| 408 | $self->{LastError} = "PrepareQuery(): error during prepare('$sqry'): $@"; | ||
| 409 | return 0; | ||
| 410 | } | ||
| 411 | if(DBI::err) | ||
| 412 | { | ||
| 413 | $self->{LastError} = DBI::errstr; | ||
| 414 | return 0; | ||
| 415 | } | ||
| 416 | if(not defined $sth) | ||
| 417 | { | ||
| 418 | $self->{LastError} = "PrepareQuery(): prepare('$sqry') failed."; | ||
| 419 | return 0; | ||
| 420 | } | ||
| 421 | $self->{Queries}->{$sName} = | ||
| 422 | { | ||
| 423 | Handle => $sth, | ||
| 424 | ReturnType => $nReturnType || RETURNTYPE_NONE, | ||
| 425 | }; | ||
| 426 | return 1; | ||
| 427 | } | ||
| 428 | |||
| 429 | sub QueryExists($$) | ||
| 430 | { | ||
| 431 | my ($self, $sName) = (shift, shift); | ||
| 432 | |||
| 433 | return 0 | ||
| 434 | if(not defined $sName || $sName eq ""); | ||
| 435 | return exists $self->{Queries}->{$sName}; | ||
| 436 | } | ||
| 437 | |||
| 438 | sub RunSql | ||
| 439 | { | ||
| 440 | my ($self, $sql, $reSplit) = (shift, shift, shift); | ||
| 441 | |||
| 442 | my $dbh = $self->{dbh}; | ||
| 443 | |||
| 444 | $reSplit = (defined $reSplit)? $reSplit : qr(;[\r\n]+); | ||
| 445 | |||
| 446 | # Split into statements | ||
| 447 | my $raLines = [split($reSplit, $sql)]; | ||
| 448 | |||
| 449 | foreach my $sLine (@$raLines) | ||
| 450 | { | ||
| 451 | # Strip comments | ||
| 452 | $sLine =~ s/^--[^\r\n]*[\r\n]//gom; | ||
| 453 | # Strip empty lines | ||
| 454 | $sLine =~ s/^[\r\n]//gom; | ||
| 455 | next | ||
| 456 | if($sLine eq ""); | ||
| 457 | |||
| 458 | eval | ||
| 459 | { | ||
| 460 | $dbh->do($sLine); | ||
| 461 | }; | ||
| 462 | if($@) | ||
| 463 | { | ||
| 464 | if(defined $DBI::errstr) | ||
| 465 | { | ||
| 466 | $self->{LastError} = "RunSql(): " . DBI->errstr . "."; | ||
| 467 | return undef; | ||
| 468 | } | ||
| 469 | else | ||
| 470 | { | ||
| 471 | $self->{LastError} = "RunSql(): $@."; | ||
| 472 | return undef; | ||
| 473 | } | ||
| 474 | } | ||
| 475 | } | ||
| 476 | |||
| 477 | return 1; | ||
| 478 | } | ||
| 479 | |||
| 480 | =pod | ||
| 481 | |||
| 482 | Execute a stored query | ||
| 483 | |||
| 484 | =cut | ||
| 485 | |||
| 486 | sub ExecuteQuery($$$) | ||
| 487 | { | ||
| 488 | my ($self, $sName, $raParameters) = (shift, shift, shift); | ||
| 489 | |||
| 490 | $self->{LastError} = ""; | ||
| 491 | |||
| 492 | $raParameters = $raParameters || []; | ||
| 493 | if(not defined $sName) | ||
| 494 | { | ||
| 495 | $self->{LastError} = "ExecuteQuery(): No query name supplied."; | ||
| 496 | return undef; | ||
| 497 | } | ||
| 498 | if($sName eq "") | ||
| 499 | { | ||
| 500 | $self->{LastError} = "ExecuteQuery(): Query name supplied was empty string."; | ||
| 501 | return undef; | ||
| 502 | } | ||
| 503 | if(not exists $self->{Queries}->{$sName}) | ||
| 504 | { | ||
| 505 | $self->{LastError} = "ExecuteQuery(): Query '$sName' does not exist."; | ||
| 506 | return undef; | ||
| 507 | } | ||
| 508 | my $qry = $self->{Queries}->{$sName}; | ||
| 509 | my $sth = $qry->{Handle}; | ||
| 510 | if(not defined $sth) | ||
| 511 | { | ||
| 512 | $self->{LastError} = "ExecuteQuery(): Query handle for '$sName' not defined."; | ||
| 513 | return undef; | ||
| 514 | } | ||
| 515 | my $nReturnType = $qry->{ReturnType}; | ||
| 516 | return $self->_ExecuteQueryOnHandle($sth, $raParameters, $nReturnType); | ||
| 517 | } | ||
| 518 | |||
| 519 | sub ExecuteTemporaryQuery($$$$) | ||
| 520 | { | ||
| 521 | my ($self, $sqry, $raParameters, $nReturnType) = (shift, shift, shift, shift); | ||
| 522 | |||
| 523 | $self->{LastError} = ""; | ||
| 524 | |||
| 525 | my $sth = undef; | ||
| 526 | eval | ||
| 527 | { | ||
| 528 | $sth = $self->{dbh}->prepare($sqry); | ||
| 529 | }; | ||
| 530 | if(DBI::err) | ||
| 531 | { | ||
| 532 | $self->{LastError} = DBI::errstr; | ||
| 533 | return undef; | ||
| 534 | } | ||
| 535 | return $self->_ExecuteQueryOnHandle($sth, $raParameters, $nReturnType); | ||
| 536 | } | ||
| 537 | |||
| 538 | sub GetNewInsertId($$$) | ||
| 539 | { | ||
| 540 | my ($self, $sTableName, $nIdField) = (shift, shift, shift); | ||
| 541 | |||
| 542 | warn("GetNewInsertId() is deprecated, use GetMaxId()"); | ||
| 543 | |||
| 544 | return GetMaxId($self, $sTableName, $nIdField); | ||
| 545 | } | ||
| 546 | |||
| 547 | =pod | ||
| 548 | |||
| 549 | Return: | ||
| 550 | MaxId from table or $nDefault if there are no records. | ||
| 551 | |||
| 552 | =cut | ||
| 553 | |||
| 554 | sub GetMaxId($$$) | ||
| 555 | { | ||
| 556 | my ($self, $sTableName, $nIdField) = (shift, shift, shift); | ||
| 557 | my $nDefault = (scalar @_ > 0)? shift : undef; | ||
| 558 | |||
| 559 | my $sqry = "SELECT MAX($nIdField) AS MaxId FROM $sTableName;"; | ||
| 560 | my $ra = undef; | ||
| 561 | eval | ||
| 562 | { | ||
| 563 | my $sth = $self->{dbh}->prepare($sqry); | ||
| 564 | $sth->execute(); | ||
| 565 | $ra = $sth->fetchrow_arrayref(); | ||
| 566 | }; | ||
| 567 | if($@) | ||
| 568 | { | ||
| 569 | $self->{LastError} = "GetMaxId(): $@"; | ||
| 570 | return undef; | ||
| 571 | } | ||
| 572 | if(DBI::err) | ||
| 573 | { | ||
| 574 | $self->{LastError} = DBI::errstr; | ||
| 575 | return undef; | ||
| 576 | } | ||
| 577 | return $nDefault | ||
| 578 | if(not defined $ra); | ||
| 579 | my $nMaxId = $ra->[0]; | ||
| 580 | return $nDefault | ||
| 581 | if(not defined $nMaxId); | ||
| 582 | return $nMaxId; | ||
| 583 | } | ||
| 584 | |||
| 585 | sub _ExecuteQueryOnHandle($$$$) | ||
| 586 | { | ||
| 587 | my ($self, $sth, $raParameters, $nReturnType) = (shift, shift, shift, shift); | ||
| 588 | |||
| 589 | if(not defined $sth) | ||
| 590 | { | ||
| 591 | $self->{LastError} = "_ExecuteQueryOnHandle(): No query handle supplied."; | ||
| 592 | return undef; | ||
| 593 | } | ||
| 594 | $raParameters = (defined $raParameters && ref($raParameters) eq "ARRAY")? $raParameters : []; | ||
| 595 | $nReturnType = (defined $nReturnType)? $nReturnType : RETURNTYPE_NONE; | ||
| 596 | eval | ||
| 597 | { | ||
| 598 | $sth->execute(@$raParameters); | ||
| 599 | }; | ||
| 600 | if($@) | ||
| 601 | { | ||
| 602 | $self->{LastError} = "_ExecuteQueryOnHandle(): $@"; | ||
| 603 | return undef; | ||
| 604 | } | ||
| 605 | if(DBI::err) | ||
| 606 | { | ||
| 607 | $self->{LastError} = "_ExecuteQueryOnHandle(): " . DBI::errstr; | ||
| 608 | return undef; | ||
| 609 | } | ||
| 610 | my $ret = undef; | ||
| 611 | SWITCH: | ||
| 612 | { | ||
| 613 | if($nReturnType == RETURNTYPE_NONE) | ||
| 614 | { | ||
| 615 | last; | ||
| 616 | } | ||
| 617 | if($nReturnType == RETURNTYPE_SUCCESS) | ||
| 618 | { | ||
| 619 | $ret = 1; | ||
| 620 | last; | ||
| 621 | } | ||
| 622 | if($nReturnType == RETURNTYPE_VALUE) | ||
| 623 | { | ||
| 624 | my $ra = $sth->fetchrow_arrayref(); | ||
| 625 | if(not defined $ra) | ||
| 626 | { | ||
| 627 | $self->{LastError} = "_ExecuteQueryOnHandle(): fetchrow_arrayref() returned undef"; | ||
| 628 | last; | ||
| 629 | } | ||
| 630 | $ret = $ra->[0]; | ||
| 631 | last; | ||
| 632 | } | ||
| 633 | if($nReturnType == RETURNTYPE_ALLVALUES) | ||
| 634 | { | ||
| 635 | $ret = []; | ||
| 636 | my $ra = $sth->fetchrow_arrayref(); | ||
| 637 | while(defined $ra) | ||
| 638 | { | ||
| 639 | push @$ret, $ra->[0]; | ||
| 640 | $ra = $sth->fetchrow_arrayref(); | ||
| 641 | } | ||
| 642 | last; | ||
| 643 | } | ||
| 644 | if($nReturnType == RETURNTYPE_HASHREF) | ||
| 645 | { | ||
| 646 | my $rh = $sth->fetchrow_hashref(); | ||
| 647 | if(not defined $rh) | ||
| 648 | { | ||
| 649 | $self->{LastError} = "_ExecuteQueryOnHandle(): fetchrow_hashref() returned undef"; | ||
| 650 | last; | ||
| 651 | } | ||
| 652 | $ret = $rh; | ||
| 653 | last; | ||
| 654 | } | ||
| 655 | if($nReturnType == RETURNTYPE_ARRAYREF) | ||
| 656 | { | ||
| 657 | my $ra = $sth->fetchrow_arrayref(); | ||
| 658 | if(not defined $ra) | ||
| 659 | { | ||
| 660 | $self->{LastError} = "_ExecuteQueryOnHandle(): fetchrow_arrayref() returned undef"; | ||
| 661 | last; | ||
| 662 | } | ||
| 663 | # DBI(?) BUG workaround: fetchrow_arrayref hands back the SAME array ref each time, | ||
| 664 | # so it stomps on previous values in recursive calls. | ||
| 665 | # So, create a new array ref | ||
| 666 | $ret = [@$ra]; | ||
| 667 | last; | ||
| 668 | } | ||
| 669 | if($nReturnType == RETURNTYPE_ARRAYREFOFARRAYREFS) | ||
| 670 | { | ||
| 671 | my $ra = $sth->fetchall_arrayref(); | ||
| 672 | $ret = $ra; | ||
| 673 | last; | ||
| 674 | } | ||
| 675 | if($nReturnType == RETURNTYPE_ARRAYREFOFHASHREFS) | ||
| 676 | { | ||
| 677 | my $ra = $sth->fetchall_arrayref({}); | ||
| 678 | $ret = $ra; | ||
| 679 | last; | ||
| 680 | } | ||
| 681 | $self->{LastError} = "_ExecuteQueryOnHandle(): Unknown return type '$nReturnType'"; | ||
| 682 | return undef; | ||
| 683 | } | ||
| 684 | $sth->finish(); | ||
| 685 | return $ret; | ||
| 686 | } | ||
| 687 | |||
| 688 | sub _Create($$) | ||
| 689 | { | ||
| 690 | my ($proto, $self) = (shift, shift); | ||
| 691 | |||
| 692 | my $class = ref($proto) || $proto; | ||
| 693 | bless($self, $class); | ||
| 694 | |||
| 695 | return $self; | ||
| 696 | } | ||
| 697 | |||
| 698 | sub _MakeDsn | ||
| 699 | { | ||
| 700 | my ($nConnectionType, $sDatabaseName, $sHost) = (shift, shift, shift); | ||
| 701 | if(not defined $nConnectionType) | ||
| 702 | { | ||
| 703 | $ConnectionError = "ConnectionType undefined at " . __FILE__ . " line " . __LINE__ . "."; | ||
| 704 | return undef; | ||
| 705 | } | ||
| 706 | if(not defined $sDatabaseName) | ||
| 707 | { | ||
| 708 | $ConnectionError = "DatabaseName undefined at " . __FILE__ . " line " . __LINE__ . "."; | ||
| 709 | return undef; | ||
| 710 | } | ||
| 711 | my $sDriverName = GetDriverName($nConnectionType); | ||
| 712 | if(not defined $sDriverName) | ||
| 713 | { | ||
| 714 | $ConnectionError = "Can't get driver name for ConnectionType '$nConnectionType' at " . __FILE__ . " line " . __LINE__ . "."; | ||
| 715 | return undef; | ||
| 716 | } | ||
| 717 | my $sDsn; | ||
| 718 | SWITCH: | ||
| 719 | { | ||
| 720 | if($nConnectionType == CONNECTION_TYPE_MySQL) | ||
| 721 | { | ||
| 722 | $sDsn = "dbi:$sDriverName:$sDatabaseName"; | ||
| 723 | $sDsn .= ":$sHost" | ||
| 724 | if(defined $sHost); | ||
| 725 | last; | ||
| 726 | } | ||
| 727 | if($nConnectionType == CONNECTION_TYPE_ODBC) | ||
| 728 | { | ||
| 729 | $sDsn = "dbi:$sDriverName:$sDatabaseName"; | ||
| 730 | last; | ||
| 731 | } | ||
| 732 | if($nConnectionType == CONNECTION_TYPE_Postgres) | ||
| 733 | { | ||
| 734 | $sDsn = "dbi:$sDriverName:dbname=$sDatabaseName"; | ||
| 735 | $sDsn .= ":host=$sHost" | ||
| 736 | if(defined $sHost); | ||
| 737 | last; | ||
| 738 | } | ||
| 739 | if($nConnectionType == CONNECTION_TYPE_SQLite) | ||
| 740 | { | ||
| 741 | $sDsn = "dbi:$sDriverName:dbname=$sDatabaseName"; | ||
| 742 | last; | ||
| 743 | } | ||
| 744 | $ConnectionError = "Unknown ConnectionType '$nConnectionType' at " . __FILE__ . " line " . __LINE__ . "."; | ||
| 745 | $sDsn = undef; | ||
| 746 | } | ||
| 747 | |||
| 748 | return $sDsn; | ||
| 749 | } | ||
| 750 | |||
| 751 | =pod | ||
| 752 | |||
| 753 | Static functions | ||
| 754 | |||
| 755 | =cut | ||
| 756 | |||
| 757 | # Deprecated | ||
| 758 | sub GetMySQLDate | ||
| 759 | { | ||
| 760 | my ($nTime) = (shift); | ||
| 761 | warn("GetMySQLDate() is deprecated"); | ||
| 762 | |||
| 763 | $nTime = time | ||
| 764 | if(not defined $nTime); | ||
| 765 | my ($nSecond, $nMinute, $nHour, $nDay, $nMonth, $nYear, $wday, $yday, $isdst) = gmtime($nTime); | ||
| 766 | $nMonth++; | ||
| 767 | $nYear += 1900; | ||
| 768 | my $sMySQLDate = sprintf("%04u-%02u-%02u", $nYear, $nMonth, $nDay); | ||
| 769 | return $sMySQLDate; | ||
| 770 | } | ||
| 771 | |||
| 772 | # Deprecated | ||
| 773 | sub GetMySQLDateTime | ||
| 774 | { | ||
| 775 | my ($nTime) = (shift); | ||
| 776 | warn("GetMySQLDateTime() is deprecated"); | ||
| 777 | |||
| 778 | $nTime = time | ||
| 779 | if(not defined $nTime); | ||
| 780 | my ($nSecond, $nMinute, $nHour, $nDay, $nMonth, $nYear, $wday, $yday, $isdst) = gmtime($nTime); | ||
| 781 | $nMonth++; | ||
| 782 | $nYear += 1900; | ||
| 783 | my $sMySQLDate = sprintf("%04u-%02u-%02u %02u:%02u:%02u", $nYear, $nMonth, $nDay, $nHour, $nMinute, $nSecond); | ||
| 784 | return $sMySQLDate; | ||
| 785 | } | ||
| 786 | |||
| 787 | 1; | ||
| 788 | |||
| 789 | __END__ |