| 1 | =head1 NAME | ||
| 2 | |||
| 3 | DBD::Ovrimos - DBI Driver for Ovrimos (formerly Altera SQL Server) | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use DBI; | ||
| 8 | my $dbh=DBI->connect( | ||
| 9 | "dbi:Ovrimos:some.host.com:2500", | ||
| 10 | "user", | ||
| 11 | "passwd") | ||
| 12 | or die "Cannot connect\n"; | ||
| 13 | # more DBI calls... | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | DBI driver for Ovrimos (See L<DBI(3)> for details). This driver is essentially | ||
| 18 | a rename of DBD::Altera. Since DBI | ||
| 19 | is a moving target at the time of this writing, this driver should only be | ||
| 20 | assumed to work with DBI 0.93. | ||
| 21 | A standard notice in DBD drivers' man pages is that, since the DBI is not yet | ||
| 22 | stable, any DBD driver should be considered ALPHA software. So be it. | ||
| 23 | We will try to keep up with the changes, stay tuned at | ||
| 24 | <http://www.altera.gr/download.html> which is the primary download site for | ||
| 25 | this driver. | ||
| 26 | |||
| 27 | =head1 CURRENT VERSION | ||
| 28 | |||
| 29 | Release 0.12 Name change... | ||
| 30 | Previous release were: | ||
| 31 | Release 0.11 Essentially a bug-fix. | ||
| 32 | Release 0.10 (one hair short of 1.00). Main difference from previous version | ||
| 33 | 0.09 is minor alterations to permit use for AGI (Another Gateway Interface). | ||
| 34 | In other words, how can one use the same module to write both DBI programs | ||
| 35 | and stored procedures for Ovrimos. Also, stored procedures | ||
| 36 | are now supported, using the pseudo-SQL "call xxx ..." statement. See | ||
| 37 | the documentation of Ovrimos for details. | ||
| 38 | |||
| 39 | =head1 DRIVER-SPECIFIC BEHAVIOR | ||
| 40 | |||
| 41 | =head2 DATA-SOURCE NAME | ||
| 42 | |||
| 43 | The dsn string passed to DBI->connect must be of the following form: | ||
| 44 | |||
| 45 | dbi:Ovrimos:host:port | ||
| 46 | |||
| 47 | where host is a TCP/IP address in human-readable or dotted-decimal format, and | ||
| 48 | port is the TCP/IP port number to use (Ovrimos SQLPORT configuration | ||
| 49 | parameter). | ||
| 50 | |||
| 51 | =head2 CONNECTIONS, SESSIONS AND TRANSACTIONS | ||
| 52 | |||
| 53 | One can have multiple connections to an Ovrimos database, up to | ||
| 54 | the limit specified by one's User License. Keep in mind that what the License | ||
| 55 | calls 'sessions' amount to what are called separate statements in DBI. | ||
| 56 | Underlying the DBI is a protocol using the ODBC-equivalent 'connections' and | ||
| 57 | 'statements'. Sessions are kept live until commit/rollback, and that can | ||
| 58 | result in denial of service if you reach the License limit. The database | ||
| 59 | handle will reuse an inactive statement handle, so finish() often. | ||
| 60 | |||
| 61 | Commit/rollback finish()'es implicitly all open cursors (that's the answer | ||
| 62 | one asks ODBC with SQL_CURSOR_COMMIT_BEHAVIOR and SQL_CURSOR_ROLLBACK_BEHAVIOR). | ||
| 63 | |||
| 64 | Cached statements are not available. In the near future it is planned to | ||
| 65 | cache SQL statements internally at the SQL Server, so preparing the same | ||
| 66 | SQL statement as some time before will return a new $sth but without the | ||
| 67 | cost associated with preparing from scratch. | ||
| 68 | |||
| 69 | =head2 DATA TYPES | ||
| 70 | |||
| 71 | All ODBC 2.0 data types are supported. The format of time/date values is | ||
| 72 | as per the SQL/2 Standard, i.e.: S<'DATE YYYY-MM-DD'>, S<'TIME HH:MM:SS'> and | ||
| 73 | S<'TIMESTAMP YYYY-MM-DD HH:MM:SS'>. | ||
| 74 | |||
| 75 | Ovrimos supports some additional types that | ||
| 76 | are given below alongside their numerical value: | ||
| 77 | |||
| 78 | =over 4 | ||
| 79 | |||
| 80 | =item UNSIGNED SMALLINT = 20 | ||
| 81 | |||
| 82 | =item UNSIGNED INTEGER = 21 | ||
| 83 | |||
| 84 | =item UNSIGNED TINYINT = 22 | ||
| 85 | |||
| 86 | =item UNSIGNED BIGINT = 23 | ||
| 87 | |||
| 88 | =back | ||
| 89 | |||
| 90 | =head2 ERROR HANDLING | ||
| 91 | |||
| 92 | As it stands, the DBI does not support the notion of warnings. | ||
| 93 | Consequently, there are no diagnostics for successful calls. | ||
| 94 | There is no obstacle in adding this, but since perl code using | ||
| 95 | DBI will not check $h->errstr for successful operations, there is | ||
| 96 | not much incentive to actually do it. Diagnostics for failed calls | ||
| 97 | are inspected with the usual DBI calls. Do not pay any attention to | ||
| 98 | $h->err; it is dummy. Ovrimos returns Standard SQL | ||
| 99 | SQLSTATES and assorted messages, modelled principally after ODBC | ||
| 100 | use. Since many diagnostics can be accumulated by one call, the | ||
| 101 | diagnostics are merged, separated with newline. In that way, only | ||
| 102 | the first SQLSTATE in the queue is visible using $h->state. One | ||
| 103 | has to parse $h->errstr to find out the rest. | ||
| 104 | |||
| 105 | =head2 BLOBS | ||
| 106 | |||
| 107 | BLOBs are supported via the SQL2 types B<S<LONG VARCHAR>> and B<S<LONG | ||
| 108 | VARBINARY>>. These are not fetched with SQL queries and the LongReadLen and | ||
| 109 | LongTruncOk attributes are not honored. Instead, Ovrimos | ||
| 110 | presents a HTTP interface for retrieving BLOBS. Every BLOB has a Uniform | ||
| 111 | Resource Identifier that can be found using the built-in B<URI> function. | ||
| 112 | This makes for easy retrieval of BLOBs in CGI scripts, where the URI can | ||
| 113 | be embedded in HTML constructs like this: | ||
| 114 | |||
| 115 | my ($name,$uri1,$uri2); | ||
| 116 | $sth->bind_columns(undef,\($name,$uri1,$uri2)); | ||
| 117 | |||
| 118 | $sth->prepare('select name,uri(blob1),uri(blob2) from blobtest'); | ||
| 119 | $sth->execute; | ||
| 120 | |||
| 121 | while($sth->fetch) { | ||
| 122 | print '<A HREF="' . $uri1 .'">Click here!<A> '; | ||
| 123 | print '<IMG SRC="' . $uri2 . '" ALT="Image"><BR>', "\n"; | ||
| 124 | } | ||
| 125 | |||
| 126 | BLOBs are MIME-typed so the HTTP browser knows how to handle them. If one | ||
| 127 | needs to retrieve a BLOB in an arbitrary script, one can use HTTP facilities | ||
| 128 | like those in the libwww bundle (see CPAN, | ||
| 129 | <http://cpan.perl.org/CPAN.html#libwww>). Or, one can just lead a simple life | ||
| 130 | and do | ||
| 131 | |||
| 132 | require 5.002; | ||
| 133 | use strict; | ||
| 134 | use IO::Socket; | ||
| 135 | my $host; | ||
| 136 | my $file; | ||
| 137 | my $port=80; | ||
| 138 | if($uri =~ m[^http://(.*):(\d*)/(.*)]) { | ||
| 139 | ($host,$port,$file)=($1,$2,$3); | ||
| 140 | } elsif($uri =~ m[^http://(.*)/(.*)]) { | ||
| 141 | ($host,$file)=($1,$2); | ||
| 142 | } else { | ||
| 143 | die "horribly"; | ||
| 144 | } | ||
| 145 | my $so=IO::Socket::INET->new( Proto=>"tcp", PeerAddr=>$host, | ||
| 146 | PeerPort=>$port) or die "in pain"; | ||
| 147 | print $so "GET /$file HTTP/1.0\r\n\r\n"; | ||
| 148 | $so->flush() or die "in agony"; | ||
| 149 | |||
| 150 | One can then proceed to read from $so after skipping the reply header. | ||
| 151 | If the MIME type is required, it can be found in the 'Content-type:' | ||
| 152 | attribute of the reply header. | ||
| 153 | |||
| 154 | Maybe in a later release this functionality will be included in the driver. | ||
| 155 | |||
| 156 | =head2 DRIVER-SPECIFIC ATTRIBUTES | ||
| 157 | |||
| 158 | There are some additional attributes that the user can query a $sth for: | ||
| 159 | |||
| 160 | =over 4 | ||
| 161 | |||
| 162 | =item TYPE (also ovrimos_column_type) | ||
| 163 | |||
| 164 | Reference to an array of column types as per ODBC, plus the Ovrimos extended types. | ||
| 165 | TYPE is in capitals because the values returned conform to approved standards | ||
| 166 | (ODBC, X/Open). | ||
| 167 | |||
| 168 | =item ovrimos_column_precision | ||
| 169 | |||
| 170 | Reference to an array of column precisions. Has meaning only for vector types | ||
| 171 | (*CHAR, *BINARY) and NUMERIC/DECIMAL | ||
| 172 | |||
| 173 | =item ovrimos_column_scale | ||
| 174 | |||
| 175 | Reference to an array of column scales. Has meaning only for NUMERIC/DECIMAL. | ||
| 176 | |||
| 177 | =item ovrimos_execution_plan | ||
| 178 | |||
| 179 | It is a high-level explanation of the execution plan for the statement. The | ||
| 180 | format is highly version-dependent and not to be dependent upon, but a human | ||
| 181 | reader should be able to understand the access path for every range variable | ||
| 182 | used, the order of range variables, the indices used, which temporary tables | ||
| 183 | have been created et.c. | ||
| 184 | |||
| 185 | =item ovrimos_native_query | ||
| 186 | |||
| 187 | The query submitted, but in the form retained by the SQL Server. The SQL | ||
| 188 | Server applies transformations to the SQL source and disambiguates certain | ||
| 189 | constructs. The modified source can also be found in the execution plan (see | ||
| 190 | above). | ||
| 191 | |||
| 192 | =back | ||
| 193 | |||
| 194 | =head2 LOW-LEVEL LIBRARY | ||
| 195 | |||
| 196 | The entire low-level library that implements the Ovrimos protocol | ||
| 197 | is included. The DBI driver is based on this library, but one could conceivably | ||
| 198 | use the library on its own. It is the only way, for the time being, to use | ||
| 199 | scrollable cursors and bookmarks, since the DBI does not support them (yet?). | ||
| 200 | See the package C<DBD::Ovrimos::lowlevel> in C<Ovrimos.pm>. No documentation is | ||
| 201 | provided in this version about the low-level library. | ||
| 202 | |||
| 203 | =head1 COMFORMANCE | ||
| 204 | |||
| 205 | There is a particularity concerning transactions: see | ||
| 206 | L</CONNECTIONS, SESSIONS AND TRANSACTIONS>. | ||
| 207 | |||
| 208 | Cached statements don't exist. Not even the function prepare_cached exists. | ||
| 209 | Do not use it! You won't find any relevant attribute either. | ||
| 210 | |||
| 211 | =head1 KNOWN BUGS | ||
| 212 | |||
| 213 | There are no known bugs in the DBD Driver. | ||
| 214 | |||
| 215 | =head1 ACKNOWLEDGEMENTS | ||
| 216 | |||
| 217 | I would like to thank all the people on the DBI-DEV mailing list that | ||
| 218 | helped clear some misunderstandings. | ||
| 219 | |||
| 220 | =head1 SEE ALSO | ||
| 221 | |||
| 222 | DBI(3) | ||
| 223 | |||
| 224 | =head1 AUTHOR | ||
| 225 | |||
| 226 | Dimitrios Souflis dsouflis@altera.gr, | ||
| 227 | |||
| 228 | =head1 COPYRIGHT | ||
| 229 | |||
| 230 | (c) Altera Ltd, Greece http://www.altera.gr | ||
| 231 | |||
| 232 | Permission is granted to use this software library according to the | ||
| 233 | GNU Library General Public License (see <http://www.gnu.org>). | ||
| 234 | |||
| 235 | =cut | ||
| 236 | |||
| 237 | require 5.003; | ||
| 238 | use strict; | ||
| 239 | use IO::Socket; | ||
| 240 | |||
| 241 | package DBD::Ovrimos::lowlevel; | ||
| 242 | |||
| 243 | #Declarations for low-level functions and constants | ||
| 244 | #Essentially a Perl port of the C low-level library | ||
| 245 | |||
| 246 | sub _plain_mesg($$); | ||
| 247 | |||
| 248 | sub sqlConnect($$$$); | ||
| 249 | sub sqlConnectOutcome(); | ||
| 250 | sub sqlDisconnect($); | ||
| 251 | sub sqlAllocStmt($); | ||
| 252 | sub sqlFreeStmt($); | ||
| 253 | |||
| 254 | sub sqlSetConnIntOption($$$); | ||
| 255 | sub sqlGetConnIntOption($$); | ||
| 256 | sub sqlSetStmtIntOption($$$); | ||
| 257 | sub sqlGetStmtIntOption($$); | ||
| 258 | sub sqlSetRowsetSize($$); | ||
| 259 | sub sqlGetRowsetSize($); | ||
| 260 | |||
| 261 | sub sqlSetIntOption($$$$); | ||
| 262 | sub sqlGetIntOption($$$); | ||
| 263 | |||
| 264 | sub sqlExecDirect($$); | ||
| 265 | sub sqlPrepare($$); | ||
| 266 | sub sqlExec($); | ||
| 267 | sub sqlCloseCursor($); | ||
| 268 | sub sqlAsyncFinished($); | ||
| 269 | sub sqlCancel($); | ||
| 270 | sub sqlSetCursorName($$); | ||
| 271 | sub sqlGetCursorName($); | ||
| 272 | |||
| 273 | sub sqlNest($); | ||
| 274 | sub sqlCommit($); | ||
| 275 | sub sqlRollback($); | ||
| 276 | |||
| 277 | sub sqlGetConnPending($); | ||
| 278 | sub sqlGetStmtPending($); | ||
| 279 | |||
| 280 | sub sqlGetConnDiagnostics($); | ||
| 281 | sub sqlGetStmtDiagnostics($); | ||
| 282 | |||
| 283 | sub sqlGetExecutionPlan($); | ||
| 284 | sub sqlGetNativeQuery($); | ||
| 285 | sub sqlGetRowCount($); | ||
| 286 | |||
| 287 | sub sqlGetOutputColDescr($); | ||
| 288 | sub sqlGetOutputColNb($); | ||
| 289 | sub sqlGetOutputColName($$); | ||
| 290 | sub sqlGetOutputColType($$); | ||
| 291 | sub sqlGetOutputColLength($$); | ||
| 292 | sub sqlGetOutputColPrecision($$); | ||
| 293 | sub sqlGetOutputColScale($$); | ||
| 294 | sub sqlGetOutputColNullable($$); | ||
| 295 | |||
| 296 | sub sqlGetParamDescr($); | ||
| 297 | sub sqlGetParamNb($); | ||
| 298 | sub sqlGetParamType($$); | ||
| 299 | sub sqlGetParamLength($$); | ||
| 300 | sub sqlGetParamPrecision($$); | ||
| 301 | sub sqlGetParamScale($$); | ||
| 302 | |||
| 303 | sub sqlPutParam($$$); | ||
| 304 | sub sqlResetParams($); | ||
| 305 | |||
| 306 | sub sqlCursorThis($); | ||
| 307 | sub sqlCursorFirst($$); | ||
| 308 | sub sqlCursorNext($$); | ||
| 309 | sub sqlCursorLast($$); | ||
| 310 | sub sqlCursorPrev($$); | ||
| 311 | sub sqlCursorBookmark($$); | ||
| 312 | sub sqlCursorGetBookmark($); | ||
| 313 | sub sqlCursorMove($$$$); | ||
| 314 | |||
| 315 | sub sqlGotoRow($$); | ||
| 316 | sub sqlRowState($$); | ||
| 317 | sub sqlRowBookmark($$); | ||
| 318 | |||
| 319 | sub sqlColValue($$$); | ||
| 320 | sub sqlColIsNull($$$); | ||
| 321 | |||
| 322 | sub _type_size($); | ||
| 323 | sub _type_overhead($); | ||
| 324 | |||
| 325 | sub _byte_order(); | ||
| 326 | sub _column_def_len() {37}; | ||
| 327 | sub _MAXMESGLEN() { 1024*64 }; | ||
| 328 | sub _column_width($); | ||
| 329 | sub _column_pack_template($); | ||
| 330 | sub _collapse_null_ind($); | ||
| 331 | |||
| 332 | # Here we build a custom packing/unpacking facility to handle values | ||
| 333 | # Note that BIGINT and UNSIGNED BIGINT are kept in hex | ||
| 334 | sub _pack($$$); #_pack(endianity,template,ref array of values) -> string | ||
| 335 | sub _unpack($$$); #_unpack(endianity,template,string) -> array of values | ||
| 336 | sub _swapstring($); #_swapstring(string) -> string | ||
| 337 | sub _unpack_coldefs($$$); | ||
| 338 | sub make_date($$$); | ||
| 339 | sub make_time($$$); | ||
| 340 | sub break_date($); | ||
| 341 | sub break_time($); | ||
| 342 | |||
| 343 | # template characters: | ||
| 344 | # a/A sint8/uint8 | ||
| 345 | # b/B sint16/uint16 | ||
| 346 | # c/C sint32/uint32 | ||
| 347 | # d/D sint64/uint64 | ||
| 348 | # f/F float/double | ||
| 349 | # g date 'DATE YYYY-MM-DD' | ||
| 350 | # h time 'TIME HH:MM:SS' | ||
| 351 | # i timestamp 'TIMESTAMP YYYY-MM-DD HH:MM:SS' | ||
| 352 | # y99 <num> chars : fixed length BINARY | ||
| 353 | # Y99 VARBINARY <num> chars including padding preceded by uint16 actual len | ||
| 354 | # z zero-terminated string | ||
| 355 | # z99 zero-terminated string in field <num> chars wide (excluding null) | ||
| 356 | |||
| 357 | # Constants that indicate type of failure for sqlConnect | ||
| 358 | sub c_ok() {0} | ||
| 359 | sub c_conn_failed() {1} | ||
| 360 | sub c_trans_failed() {2} | ||
| 361 | sub c_auth_failed() {3} | ||
| 362 | |||
| 363 | # Options | ||
| 364 | sub OPTION_ASYNC() {0} | ||
| 365 | sub OPTION_SEND_BOOKMARKS() {1} | ||
| 366 | sub OPTION_ISOLATION() {2} | ||
| 367 | |||
| 368 | # Row status indicators | ||
| 369 | sub ROW_OK() {0} | ||
| 370 | sub ROW_INEXISTANT() {1} | ||
| 371 | sub ROW_ERROR() {2} | ||
| 372 | |||
| 373 | # Return codes | ||
| 374 | sub RET_OK() {0} | ||
| 375 | sub RET_STILL_EXEC() {1} | ||
| 376 | sub RET_ERROR() {2} | ||
| 377 | |||
| 378 | # Types | ||
| 379 | sub T_CHAR() {1} | ||
| 380 | sub T_VARCHAR() {12} | ||
| 381 | sub T_LONGVARCHAR() {-1} | ||
| 382 | sub T_DECIMAL() {3} | ||
| 383 | sub T_NUMERIC() {2} | ||
| 384 | sub T_SMALLINT() {5} | ||
| 385 | sub T_INTEGER() {4} | ||
| 386 | sub T_REAL() {7} | ||
| 387 | sub T_FLOAT() {6} | ||
| 388 | sub T_DOUBLE() {8} | ||
| 389 | sub T_BIT() {-7} | ||
| 390 | sub T_TINYINT() {-6} | ||
| 391 | sub T_BIGINT() {-5} | ||
| 392 | sub T_BINARY() {-2} | ||
| 393 | sub T_VARBINARY() {-3} | ||
| 394 | sub T_LONGVARBINARY() {-4} | ||
| 395 | sub T_DATE() {9} | ||
| 396 | sub T_TIME() {10} | ||
| 397 | sub T_TIMESTAMP() {11} | ||
| 398 | sub T_USMALLINT() {20} | ||
| 399 | sub T_UINTEGER() {21} | ||
| 400 | sub T_UTINYINT() {22} | ||
| 401 | sub T_UBIGINT() {23} | ||
| 402 | |||
| 403 | # Byte orders | ||
| 404 | sub BYTE_ORDER_LITTLE() {0} | ||
| 405 | sub BYTE_ORDER_BIG() {1} | ||
| 406 | |||
| 407 | # Messages | ||
| 408 | sub FUNC_LOGIN() {0} | ||
| 409 | sub FUNC_LOGOUT() {1} | ||
| 410 | sub FUNC_ALLOC_STMT() {2} | ||
| 411 | sub FUNC_FREE_STMT() {3} | ||
| 412 | sub FUNC_EXEC() {4} | ||
| 413 | sub FUNC_CURSOR_THIS() {5} | ||
| 414 | sub FUNC_OPTION_SET() {6} | ||
| 415 | sub FUNC_OPTION_GET() {7} | ||
| 416 | sub FUNC_STMT_OPTION_SET() {8} | ||
| 417 | sub FUNC_STMT_OPTION_GET() {9} | ||
| 418 | sub FUNC_STILL_EXEC() {10} | ||
| 419 | sub FUNC_GET_DIAGS() {11} | ||
| 420 | sub FUNC_GET_STMT_DIAGS() {12} | ||
| 421 | sub FUNC_GET_NATIVE_QUERY() {13} | ||
| 422 | sub FUNC_GET_EXEC_PLAN() {14} | ||
| 423 | sub FUNC_PUT_PARAM() {15} | ||
| 424 | sub FUNC_PREPARE() {16} | ||
| 425 | sub FUNC_EXEC_DIRECT() {17} | ||
| 426 | sub FUNC_DESCRIBE_PARAMS() {18} | ||
| 427 | sub FUNC_DESCRIBE_RES_COLS() {19} | ||
| 428 | sub FUNC_CURSOR_FIRST() {20} | ||
| 429 | sub FUNC_CURSOR_NEXT() {21} | ||
| 430 | sub FUNC_CURSOR_LAST() {22} | ||
| 431 | sub FUNC_CURSOR_PREV() {23} | ||
| 432 | sub FUNC_COMMIT() {24} | ||
| 433 | sub FUNC_ROLLBACK() {25} | ||
| 434 | sub FUNC_SET_NAME() {26} | ||
| 435 | sub FUNC_NEST() {27} | ||
| 436 | sub FUNC_RESET_PARAMS() {28} | ||
| 437 | sub FUNC_END_EXEC() {29} | ||
| 438 | sub FUNC_GET_NAME() {30} | ||
| 439 | sub FUNC_GET_ROW_COUNT() {31} | ||
| 440 | sub FUNC_CURSOR_GET_BM() {32} | ||
| 441 | sub FUNC_CURSOR_GOTO_BM() {33} | ||
| 442 | sub FUNC_CANCEL() {34} | ||
| 443 | sub FUNC_CALL() {35} | ||
| 444 | sub FUNC_BULK() {36} | ||
| 445 | |||
| 446 | |||
| 447 | # | ||
| 448 | sub _pack($$$) { | ||
| 449 | my $endianity=shift; | ||
| 450 | my $template=shift; | ||
| 451 | my $valuesref=shift; | ||
| 452 | my ($buf,$index); | ||
| 453 | my $len=scalar @$valuesref; | ||
| 454 | for($index=0;$index<$len;$index++) { | ||
| 455 | my $c=substr($template,0,1); | ||
| 456 | $template=substr($template,1); | ||
| 457 | my $val=$$valuesref[$index]; | ||
| 458 | my $bitstring; | ||
| 459 | if($c eq 'z' || $c eq 'y' || $c eq 'Y') { | ||
| 460 | my $vallen=length($val); | ||
| 461 | my $charlen=$vallen; | ||
| 462 | my $xlen=1; | ||
| 463 | if($c eq 'y') { $xlen=0; } | ||
| 464 | if($template =~ /(\d+)(.*)/) { | ||
| 465 | $charlen=$1; | ||
| 466 | $template=$2; | ||
| 467 | } | ||
| 468 | my $templ='a' . $charlen . 'x' . $xlen; | ||
| 469 | $bitstring=pack($templ, $val); | ||
| 470 | if($c eq 'Y') { | ||
| 471 | $bitstring=pack("B",length($val)) . $bitstring; | ||
| 472 | } | ||
| 473 | } elsif($c eq 'i') { | ||
| 474 | if($val =~ /^TIMESTAMP (\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/i) { | ||
| 475 | my $val1=make_date($1,$2,$3); | ||
| 476 | my $val2=make_time($4,$5,$6); | ||
| 477 | $bitstring=_pack($endianity,'hg',[$val2,$val1]); | ||
| 478 | } | ||
| 479 | } else { | ||
| 480 | if($c eq 'd' || $c eq 'D') { | ||
| 481 | if($val =~ /^0x(.*)/i) { | ||
| 482 | $val=$1; | ||
| 483 | } | ||
| 484 | } elsif($c eq 'g') { | ||
| 485 | if($val =~ /^DATE (\d\d\d\d)-(\d\d)-(\d\d)/i) { | ||
| 486 | $val=make_date($1,$2,$3); | ||
| 487 | } | ||
| 488 | } elsif($c eq 'h') { | ||
| 489 | if($val =~ /^TIME (\d\d):(\d\d):(\d\d)/i) { | ||
| 490 | $val=make_time($1,$2,$3); | ||
| 491 | } | ||
| 492 | } | ||
| 493 | my $templ=$DBD::Ovrimos::lowlevel::_pack_templates{$c}; | ||
| 494 | $bitstring=pack $templ,$val; | ||
| 495 | if($endianity!=$DBD::Ovrimos::lowlevel::_local_byte_order) { | ||
| 496 | $bitstring=_swapstring($bitstring); | ||
| 497 | } | ||
| 498 | } | ||
| 499 | $buf .= $bitstring; | ||
| 500 | } | ||
| 501 | $buf; | ||
| 502 | } | ||
| 503 | |||
| 504 | sub _unpack($$$) { | ||
| 505 | my ($endianity,$template,$buf)=@_; | ||
| 506 | my @values=(); | ||
| 507 | while(length($template)>0) { | ||
| 508 | my $c=substr($template,0,1); | ||
| 509 | $template=substr($template,1); | ||
| 510 | my $val; | ||
| 511 | if($c eq 'z' || $c eq 'y' || $c eq 'Y') { | ||
| 512 | my $len; | ||
| 513 | my $xlen=1; | ||
| 514 | my $keeplen; | ||
| 515 | if($c eq 'y') { $xlen=0; } | ||
| 516 | if($c eq 'Y') { | ||
| 517 | $keeplen=unpack($DBD::Ovrimos::lowlevel::_pack_templates{'B'},$buf); | ||
| 518 | $buf=substr($buf,2); | ||
| 519 | } | ||
| 520 | if($template =~ /(\d+)(.*)/) { | ||
| 521 | $len=$1; | ||
| 522 | $template=$2; | ||
| 523 | } else { | ||
| 524 | $len=index($buf,chr(0)); # 'z' only | ||
| 525 | } | ||
| 526 | $val=substr($buf,0,$len+$xlen); | ||
| 527 | if($c eq 'z') { | ||
| 528 | my $reallen=index($val,chr(0)); | ||
| 529 | if($reallen!=-1) { | ||
| 530 | $val=substr($val,0,$reallen); | ||
| 531 | } | ||
| 532 | } | ||
| 533 | $buf=substr($buf,$len+$xlen); | ||
| 534 | if($c eq 'Y') { | ||
| 535 | $val=substr($val,0,$keeplen); | ||
| 536 | } | ||
| 537 | } elsif($c eq 'i') { | ||
| 538 | my $bitstring=substr($buf,0,8); | ||
| 539 | $buf=substr($buf,8); | ||
| 540 | my ($time,$date)=_unpack($endianity,'hg',$bitstring); | ||
| 541 | $val='TIMESTAMP' . substr($date,4) . substr($time,4); | ||
| 542 | } else { | ||
| 543 | my $templ=$DBD::Ovrimos::lowlevel::_pack_templates{$c}; | ||
| 544 | my $len=$DBD::Ovrimos::lowlevel::_pack_lengths{$c}; | ||
| 545 | my $bitstring=substr($buf,0,$len); | ||
| 546 | $buf=substr($buf,$len); | ||
| 547 | if($endianity!=$DBD::Ovrimos::lowlevel::_local_byte_order) { | ||
| 548 | $bitstring=_swapstring($bitstring); | ||
| 549 | } | ||
| 550 | $val=unpack $templ,$bitstring; | ||
| 551 | if($c eq 'd' || $c eq 'D') { | ||
| 552 | $val='0x' . $val; | ||
| 553 | } elsif($c eq 'g') { | ||
| 554 | $val=break_date($val); | ||
| 555 | } elsif($c eq 'h') { | ||
| 556 | $val=break_time($val); | ||
| 557 | } | ||
| 558 | } | ||
| 559 | push @values,$val; | ||
| 560 | } | ||
| 561 | @values; | ||
| 562 | } | ||
| 563 | |||
| 564 | sub _swapstring($) { | ||
| 565 | my $str=shift; | ||
| 566 | my $len=length($str); | ||
| 567 | my $i; | ||
| 568 | for($i=0; $i<$len/2; $i++) { | ||
| 569 | my $t; | ||
| 570 | $t=substr($str,$i,1); | ||
| 571 | substr($str,$i,1)=substr($str,$len-$i-1,1); | ||
| 572 | substr($str,$len-$i-1,1)=$t; | ||
| 573 | } | ||
| 574 | $str; | ||
| 575 | } | ||
| 576 | |||
| 577 | sub make_date($$$) { | ||
| 578 | my ($yy,$mm,$dd)=@_; | ||
| 579 | $yy*2^16+$mm*256+$dd; | ||
| 580 | } | ||
| 581 | |||
| 582 | sub make_time($$$) { | ||
| 583 | my ($hh,$mm,$ss)=@_; | ||
| 584 | $hh*3600+$mm*60+$ss; | ||
| 585 | } | ||
| 586 | |||
| 587 | sub break_date($) { | ||
| 588 | my $num=shift; | ||
| 589 | my $dd=$num%256; | ||
| 590 | my $mm=($num>>8)%256; | ||
| 591 | my $yy=($num>>16); | ||
| 592 | if(wantarray) { | ||
| 593 | return ($yy,$mm,$dd); | ||
| 594 | } else { | ||
| 595 | return sprintf 'DATE %04d-%02d-%02d', $yy, $mm, $dd; | ||
| 596 | } | ||
| 597 | } | ||
| 598 | |||
| 599 | sub break_time($) { | ||
| 600 | my $num=shift; | ||
| 601 | my $hh=int($num/3600); | ||
| 602 | my $mm=int(($num%3600)/60); | ||
| 603 | my $ss=$num%60; | ||
| 604 | if(wantarray) { | ||
| 605 | return ($hh,$mm,$ss); | ||
| 606 | } else { | ||
| 607 | sprintf 'TIME %02d:%02d:%02d', $hh, $mm, $ss; | ||
| 608 | } | ||
| 609 | } | ||
| 610 | |||
| 611 | # Find out local byte order | ||
| 612 | sub _byte_order() { | ||
| 613 | my $local_short=pack 's',[300]; | ||
| 614 | my $big_endian_short=pack 'n',[300]; | ||
| 615 | if($local_short eq $big_endian_short) { | ||
| 616 | return BYTE_ORDER_BIG; | ||
| 617 | } else { | ||
| 618 | return BYTE_ORDER_LITTLE; | ||
| 619 | } | ||
| 620 | } | ||
| 621 | |||
| 622 | BEGIN { | ||
| 623 | $DBD::Ovrimos::lowlevel::_local_byte_order=DBD::Ovrimos::lowlevel::_byte_order(); | ||
| 624 | %DBD::Ovrimos::lowlevel::_pack_templates=( | ||
| 625 | a => 'c', | ||
| 626 | A => 'C', | ||
| 627 | b => 's', | ||
| 628 | B => 'S', | ||
| 629 | c => 'l', | ||
| 630 | C => 'L', | ||
| 631 | d => 'H16', | ||
| 632 | D => 'H16', | ||
| 633 | f => 'f', | ||
| 634 | F => 'd', | ||
| 635 | g => 'L', | ||
| 636 | h => 'L', | ||
| 637 | ); | ||
| 638 | %DBD::Ovrimos::lowlevel::_pack_lengths=( | ||
| 639 | a => 1, | ||
| 640 | A => 1, | ||
| 641 | b => 2, | ||
| 642 | B => 2, | ||
| 643 | c => 4, | ||
| 644 | C => 4, | ||
| 645 | d => 8, | ||
| 646 | D => 8, | ||
| 647 | f => 4, | ||
| 648 | F => 8, | ||
| 649 | g => 4, | ||
| 650 | h => 4, | ||
| 651 | ); | ||
| 652 | } | ||
| 653 | |||
| 654 | sub _plain_mesg($$) { | ||
| 655 | my $stmtref=shift; | ||
| 656 | my $func=shift; | ||
| 657 | my $connref=$$stmtref{'Database'}; | ||
| 658 | my @arg=(0,$$stmtref{stmt_handle},$func); | ||
| 659 | my $buf=_pack($$connref{endianity},"BBB",\@arg); | ||
| 660 | $$connref{osocket}->write($buf,length($buf)) or return undef; | ||
| 661 | $$connref{osocket}->flush() or return undef; | ||
| 662 | $$connref{isocket}->read($buf,6) or return undef; | ||
| 663 | my ($len,$ret,$pending)=_unpack($$connref{endianity},"BBB",$buf); | ||
| 664 | $$stmtref{pending}=$pending; | ||
| 665 | if($len!=0) { | ||
| 666 | $$connref{isocket}->read($buf,$len) or return undef; | ||
| 667 | return undef; | ||
| 668 | } | ||
| 669 | $ret==RET_OK; | ||
| 670 | } | ||
| 671 | |||
| 672 | sub sqlConnect($$$$) { | ||
| 673 | my ($server,$port,$username,$password) = @_; | ||
| 674 | my ($so,$endianity,$buf); | ||
| 675 | $DBD::Ovrimos::lowlevel::_outcome=c_conn_failed; | ||
| 676 | $so=IO::Socket::INET->new(Proto=>'tcp',PeerAddr=>$server,PeerPort=>$port); | ||
| 677 | return undef unless defined($so); | ||
| 678 | $DBD::Ovrimos::lowlevel::_outcome=c_trans_failed; | ||
| 679 | return undef unless 1==$so->read($endianity,1); | ||
| 680 | $endianity=ord($endianity); | ||
| 681 | my @arg= | ||
| 682 | (length($username)+1,$username,length($password)+1,$password); | ||
| 683 | $buf=_pack($endianity,"bzbz",\@arg); | ||
| 684 | $so->write($buf,length($buf)) or return undef; | ||
| 685 | $so->flush() or return undef; | ||
| 686 | $so->read($buf,2) or return undef; | ||
| 687 | $DBD::Ovrimos::lowlevel::_outcome=c_auth_failed; | ||
| 688 | my ($ret)=_unpack($endianity,"b",$buf); | ||
| 689 | return undef unless $ret==RET_OK; | ||
| 690 | $DBD::Ovrimos::lowlevel::_outcome=c_ok; | ||
| 691 | my @empty_array=(); | ||
| 692 | { | ||
| 693 | 'endianity' =>$endianity, | ||
| 694 | 'osocket' =>$so, | ||
| 695 | 'isocket' =>$so, | ||
| 696 | 'stmts' =>\@empty_array, | ||
| 697 | 'AutoCommit' =>1, | ||
| 698 | 'PrintError' =>1, | ||
| 699 | 'RaiseError' =>0, | ||
| 700 | 'Active' =>1, | ||
| 701 | 'AGI' =>0, | ||
| 702 | }; | ||
| 703 | } | ||
| 704 | |||
| 705 | sub sqlConnectOutcome() { | ||
| 706 | $DBD::Ovrimos::lowlevel::_outcome; | ||
| 707 | } | ||
| 708 | |||
| 709 | sub sqlDisconnect($) { | ||
| 710 | my $connref=shift; | ||
| 711 | $$connref{'Active'}=0; | ||
| 712 | my @arg=(0,0,FUNC_LOGOUT); | ||
| 713 | my $buf=_pack($$connref{endianity},"BBB",\@arg); | ||
| 714 | $$connref{osocket}->write($buf,length($buf)) or return undef; | ||
| 715 | $$connref{osocket}->flush() or return undef; | ||
| 716 | $$connref{isocket}->read($buf,6) or return undef; | ||
| 717 | my ($len,$ret,$pending)=_unpack($$connref{endianity},"BBB",$buf); | ||
| 718 | $$connref{pending}=$pending; | ||
| 719 | if($len!=0) { | ||
| 720 | $$connref{isocket}->read($buf,$len) or return undef; | ||
| 721 | return undef; | ||
| 722 | } | ||
| 723 | $$connref{osocket}->close() or return undef; | ||
| 724 | $ret==RET_OK; | ||
| 725 | } | ||
| 726 | |||
| 727 | sub sqlAllocStmt($) { | ||
| 728 | my $connref=shift; | ||
| 729 | unless(defined($connref)) { return undef; } | ||
| 730 | my @arg=(0,0,FUNC_ALLOC_STMT); | ||
| 731 | my $buf=_pack($$connref{endianity},"BBB",\@arg); | ||
| 732 | $$connref{osocket}->write($buf,length($buf)) or return undef; | ||
| 733 | $$connref{osocket}->flush() or return undef; | ||
| 734 | $$connref{isocket}->read($buf,6) or return undef; | ||
| 735 | my ($len,$ret,$pending)=_unpack($$connref{endianity},"BBB",$buf); | ||
| 736 | $$connref{isocket}->read($buf,$len) or return undef; | ||
| 737 | unless($len==2) { return undef; } | ||
| 738 | if ($ret==RET_ERROR) { return undef; } | ||
| 739 | my ($stmt)=_unpack($$connref{endianity},"B",$buf); | ||
| 740 | { | ||
| 741 | 'Database'=>$connref, | ||
| 742 | stmt_handle=>$stmt, | ||
| 743 | rowset_size=>1, | ||
| 744 | currrow=>0, | ||
| 745 | 'Active'=>1, | ||
| 746 | }; | ||
| 747 | } | ||
| 748 | |||
| 749 | sub sqlFreeStmt($) { | ||
| 750 | my $stmtref=shift; | ||
| 751 | my $ret=_plain_mesg($stmtref,FUNC_FREE_STMT); | ||
| 752 | $$stmtref{'Active'}=0; | ||
| 753 | $ret; | ||
| 754 | } | ||
| 755 | |||
| 756 | sub sqlAsyncFinished($) { | ||
| 757 | my $stmtref=shift; | ||
| 758 | _plain_mesg($stmtref,FUNC_STILL_EXEC); | ||
| 759 | } | ||
| 760 | |||
| 761 | sub sqlCancel($) { | ||
| 762 | my $stmtref=shift; | ||
| 763 | _plain_mesg($stmtref,FUNC_CANCEL); | ||
| 764 | } | ||
| 765 | |||
| 766 | sub sqlPutParam($$$) { | ||
| 767 | my $stmtref=shift; | ||
| 768 | my $num=shift; | ||
| 769 | my $val=shift; | ||
| 770 | if($num<0 || $num>=$$stmtref{paramcount}) { | ||
| 771 | return undef; | ||
| 772 | } | ||
| 773 | my $connref=$$stmtref{'Database'}; | ||
| 774 | my @arg; | ||
| 775 | my $buf; | ||
| 776 | if(defined($val)) { | ||
| 777 | @arg=(length($val)+2,0,FUNC_PUT_PARAM,$num,$val); | ||
| 778 | $buf=_pack($$connref{endianity},"BBBBy".length($val),\@arg); | ||
| 779 | } else { | ||
| 780 | @arg=(2,0,FUNC_PUT_PARAM,$num); | ||
| 781 | $buf=_pack($$connref{endianity},"BBBB",\@arg); | ||
| 782 | } | ||
| 783 | $$connref{osocket}->write($buf,length($buf)) or return undef; | ||
| 784 | $$connref{osocket}->flush() or return undef; | ||
| 785 | $$connref{isocket}->read($buf,6) or return undef; | ||
| 786 | my ($len,$ret,$pending)=_unpack($$connref{endianity},"BBB",$buf); | ||
| 787 | $$stmtref{pending}=$pending; | ||
| 788 | if($len!=0) { | ||
| 789 | $$connref{isocket}->read($buf,$len) or return undef; | ||
| 790 | return undef; | ||
| 791 | } | ||
| 792 | $ret==RET_OK; | ||
| 793 | } | ||
| 794 | |||
| 795 | sub sqlResetParams($) { | ||
| 796 | my $stmtref=shift; | ||
| 797 | _plain_mesg($stmtref,FUNC_RESET_PARAMS); | ||
| 798 | } | ||
| 799 | |||
| 800 | sub sqlPrepare($$) { | ||
| 801 | my $stmtref=shift; | ||
| 802 | my $cmd=shift; | ||
| 803 | my $connref=$$stmtref{'Database'}; | ||
| 804 | my @arg=(length($cmd)+1,$$stmtref{stmt_handle},FUNC_PREPARE,$cmd); | ||
| 805 | my $buf=_pack($$connref{endianity},"BBBz",\@arg); | ||
| 806 | $$connref{osocket}->write($buf,length($buf)) or return undef; | ||
| 807 | $$connref{osocket}->flush() or return undef; | ||
| 808 | $$connref{isocket}->read($buf,6) or return undef; | ||
| 809 | my ($len,$ret,$pending)=_unpack($$connref{endianity},"BBB",$buf); | ||
| 810 | $$stmtref{pending}=$pending; | ||
| 811 | if($len!=0) { | ||
| 812 | $$connref{isocket}->read($buf,$len) or return undef; | ||
| 813 | return undef; | ||
| 814 | } | ||
| 815 | $ret==RET_OK; | ||
| 816 | } | ||
| 817 | |||
| 818 | sub sqlExecDirect($$) { | ||
| 819 | my $stmtref=shift; | ||
| 820 | my $cmd=shift; | ||
| 821 | my $connref=$$stmtref{'Database'}; | ||
| 822 | my $func=FUNC_EXEC_DIRECT; | ||
| 823 | if($cmd=~/call (.*)/i) { | ||
| 824 | $func=FUNC_CALL; | ||
| 825 | $cmd=$1; | ||
| 826 | } | ||
| 827 | my @arg=(length($cmd)+1,$$stmtref{stmt_handle},$func,$cmd); | ||
| 828 | my $buf=_pack($$connref{endianity},"BBBz",\@arg); | ||
| 829 | $$connref{osocket}->write($buf,length($buf)) or return undef; | ||
| 830 | $$connref{osocket}->flush() or return undef; | ||
| 831 | $$connref{isocket}->read($buf,6) or return undef; | ||
| 832 | my ($len,$ret,$pending)=_unpack($$connref{endianity},"BBB",$buf); | ||
| 833 | $$stmtref{pending}=$pending; | ||
| 834 | if($len!=0) { | ||
| 835 | $$connref{isocket}->read($buf,$len) or return undef; | ||
| 836 | return undef; | ||
| 837 | } | ||
| 838 | $ret==RET_OK; | ||
| 839 | } | ||
| 840 | |||
| 841 | sub sqlExec($) { | ||
| 842 | my $stmtref=shift; | ||
| 843 | _plain_mesg($stmtref,FUNC_EXEC); | ||
| 844 | } | ||
| 845 | |||
| 846 | sub sqlCloseCursor($) { | ||
| 847 | my $stmtref=shift; | ||
| 848 | _plain_mesg($stmtref,FUNC_END_EXEC); | ||
| 849 | } | ||
| 850 | |||
| 851 | sub sqlSetCursorName($$) { | ||
| 852 | my $stmtref=shift; | ||
| 853 | my $cname=shift; | ||
| 854 | my $connref=$$stmtref{'Database'}; | ||
| 855 | my @arg=(length($cname)+1,$$stmtref{stmt_handle},FUNC_SET_NAME,$cname); | ||
| 856 | my $buf=_pack($$connref{endianity},"BBBz",\@arg); | ||
| 857 | $$connref{osocket}->write($buf,length($buf)) or return undef; | ||
| 858 | $$connref{osocket}->flush() or return undef; | ||
| 859 | $$connref{isocket}->read($buf,6) or return undef; | ||
| 860 | my ($len,$ret,$pending)=_unpack($$connref{endianity},"BBB",$buf); | ||
| 861 | $$stmtref{pending}=$pending; | ||
| 862 | if($len!=0) { | ||
| 863 | $$connref{isocket}->read($buf,$len) or return undef; | ||
| 864 | return undef; | ||
| 865 | } | ||
| 866 | $ret==RET_OK; | ||
| 867 | } | ||
| 868 | |||
| 869 | sub sqlGetCursorName($) { | ||
| 870 | my $stmtref=shift; | ||
| 871 | my $connref=$$stmtref{'Database'}; | ||
| 872 | my @arg=(0,$$stmtref{stmt_handle},FUNC_GET_NAME); | ||
| 873 | my $buf=_pack($$connref{endianity},"BBB",\@arg); | ||
| 874 | $$connref{osocket}->write($buf,length($buf)) or return undef; | ||
| 875 | $$connref{osocket}->flush() or return undef; | ||
| 876 | $$connref{isocket}->read($buf,6) or return undef; | ||
| 877 | my ($len,$ret,$pending)=_unpack($$connref{endianity},"BBB",$buf); | ||
| 878 | $$stmtref{pending}=$pending; | ||
| 879 | if($len!=0) { | ||
| 880 | $$connref{isocket}->read($buf,$len) or return undef; | ||
| 881 | my ($x)=_unpack($$connref{endianity},"z",$buf); | ||
| 882 | return $x; | ||
| 883 | } | ||
| 884 | undef; | ||
| 885 | } | ||
| 886 | |||
| 887 | sub sqlGetExecutionPlan($) { | ||
| 888 | my $stmtref=shift; | ||
| 889 | my $connref=$$stmtref{'Database'}; | ||
| 890 | my @arg=(0,$$stmtref{stmt_handle},FUNC_GET_EXEC_PLAN); | ||
| 891 | my $buf=_pack($$connref{endianity},"BBB",\@arg); | ||
| 892 | $$connref{osocket}->write($buf,length($buf)) or return undef; | ||
| 893 | $$connref{osocket}->flush() or return undef; | ||
| 894 | $$connref{isocket}->read($buf,6) or return undef; | ||
| 895 | my ($len,$ret,$pending)=_unpack($$connref{endianity},"BBB",$buf); | ||
| 896 | $$stmtref{pending}=$pending; | ||
| 897 | if($len!=0) { | ||
| 898 | $$connref{isocket}->read($buf,$len) or return undef; | ||
| 899 | my ($x)=_unpack($$connref{endianity},"z",$buf); | ||
| 900 | return $x; | ||
| 901 | } | ||
| 902 | undef; | ||
| 903 | } | ||
| 904 | |||
| 905 | sub sqlGetNativeQuery($) { | ||
| 906 | my $stmtref=shift; | ||
| 907 | my $connref=$$stmtref{'Database'}; | ||
| 908 | my @arg=(0,$$stmtref{stmt_handle},FUNC_GET_NATIVE_QUERY); | ||
| 909 | my $buf=_pack($$connref{endianity},"BBB",\@arg); | ||
| 910 | $$connref{osocket}->write($buf,length($buf)) or return undef; | ||
| 911 | $$connref{osocket}->flush() or return undef; | ||
| 912 | $$connref{isocket}->read($buf,6) or return undef; | ||
| 913 | my ($len,$ret,$pending)=_unpack($$connref{endianity},"BBB",$buf); | ||
| 914 | $$stmtref{pending}=$pending; | ||
| 915 | if($len!=0) { | ||
| 916 | $$connref{isocket}->read($buf,$len) or return undef; | ||
| 917 | my ($x)=_unpack($$connref{endianity},"z",$buf); | ||
| 918 | return $x; | ||
| 919 | } | ||
| 920 | undef; | ||
| 921 | } | ||
| 922 | |||
| 923 | sub sqlGetRowCount($) { | ||
| 924 | my $stmtref=shift; | ||
| 925 | my $connref=$$stmtref{'Database'}; | ||
| 926 | my @arg=(0,$$stmtref{stmt_handle},FUNC_GET_ROW_COUNT); | ||
| 927 | my $buf=_pack($$connref{endianity},"BBB",\@arg); | ||
| 928 | $$connref{osocket}->write($buf,length($buf)) or return undef; | ||
| 929 | $$connref{osocket}->flush() or return undef; | ||
| 930 | $$connref{isocket}->read($buf,6) or return undef; | ||
| 931 | my ($len,$ret,$pending)=_unpack($$connref{endianity},"BBB",$buf); | ||
| 932 | $$stmtref{pending}=$pending; | ||
| 933 | if($len!=0) { | ||
| 934 | $$connref{isocket}->read($buf,$len) or return undef; | ||
| 935 | return _unpack($$connref{endianity},"C",$buf); | ||
| 936 | } | ||
| 937 | undef; | ||
| 938 | } | ||
| 939 | |||
| 940 | sub sqlSetConnIntOption($$$) { | ||
| 941 | my $connref=shift; | ||
| 942 | my $option=shift; | ||
| 943 | my $value=shift; | ||
| 944 | sqlSetIntOption($connref,undef,$option,$value); | ||
| 945 | } | ||
| 946 | |||
| 947 | sub sqlGetConnIntOption($$) { | ||
| 948 | my $connref=shift; | ||
| 949 | my $option=shift; | ||
| 950 | sqlGetIntOption($connref,undef,$option); | ||
| 951 | } | ||
| 952 | |||
| 953 | sub sqlSetStmtIntOption($$$) { | ||
| 954 | my $stmtref=shift; | ||
| 955 | my $connref=$$stmtref{'Database'}; | ||
| 956 | my $option=shift; | ||
| 957 | my $value=shift; | ||
| 958 | sqlSetIntOption($connref,$stmtref,$option,$value); | ||
| 959 | } | ||
| 960 | |||
| 961 | sub sqlGetStmtIntOption($$) { | ||
| 962 | my $stmtref=shift; | ||
| 963 | my $connref=$$stmtref{'Database'}; | ||
| 964 | my $option=shift; | ||
| 965 | sqlGetIntOption($connref,$stmtref,$option); | ||
| 966 | } | ||
| 967 | |||
| 968 | sub sqlSetIntOption($$$$) { | ||
| 969 | my $connref=shift; | ||
| 970 | my $stmtref=shift; | ||
| 971 | my $option=shift; | ||
| 972 | my $value=shift; | ||
| 973 | my $func=FUNC_OPTION_SET; | ||
| 974 | my $stmt_handle=0; | ||
| 975 | if(defined($stmtref)) { | ||
| 976 | $func=FUNC_OPTION_SET; | ||
| 977 | $stmt_handle=$$stmtref{stmt_handle}; | ||
| 978 | } | ||
| 979 | my @arg=(0,$stmt_handle,$func,$option,$value); | ||
| 980 | my $buf=_pack($$connref{endianity},"BBBbc",\@arg); | ||
| 981 | $$connref{osocket}->write($buf,length($buf)) or return undef; | ||
| 982 | $$connref{osocket}->flush() or return undef; | ||
| 983 | $$connref{isocket}->read($buf,6) or return undef; | ||
| 984 | my ($len,$ret,$pending)=_unpack($$connref{endianity},"BBB",$buf); | ||
| 985 | if(defined($stmtref)) { | ||
| 986 | $$stmtref{pending}=$pending; | ||
| 987 | } else { | ||
| 988 | $$connref{pending}=$pending; | ||
| 989 | } | ||
| 990 | $ret==RET_OK; | ||
| 991 | } | ||
| 992 | |||
| 993 | sub sqlGetIntOption($$$) { | ||
| 994 | my $connref=shift; | ||
| 995 | my $stmtref=shift; | ||
| 996 | my $option=shift; | ||
| 997 | my $value=shift; | ||
| 998 | my $func=FUNC_OPTION_GET; | ||
| 999 | my $stmt_handle=0; | ||
| 1000 | if(defined($stmtref)) { | ||
| 1001 | $func=FUNC_OPTION_GET; | ||
| 1002 | $stmt_handle=$$stmtref{stmt_handle}; | ||
| 1003 | } | ||
| 1004 | my @arg=(0,$stmt_handle,$func,$option); | ||
| 1005 | my $buf=_pack($$connref{endianity},"BBBb",\@arg); | ||
| 1006 | $$connref{osocket}->write($buf,length($buf)) or return undef; | ||
| 1007 | $$connref{osocket}->flush() or return undef; | ||
| 1008 | $$connref{isocket}->read($buf,6) or return undef; | ||
| 1009 | my ($len,$ret,$pending)=_unpack($$connref{endianity},"BBB",$buf); | ||
| 1010 | if(defined($stmtref)) { | ||
| 1011 | $$stmtref{pending}=$pending; | ||
| 1012 | } else { | ||
| 1013 | $$connref{pending}=$pending; | ||
| 1014 | } | ||
| 1015 | if($len!=4) { | ||
| 1016 | return undef; | ||
| 1017 | } | ||
| 1018 | $$connref{isocket}->read($buf,$len) or return undef; | ||
| 1019 | _unpack($$connref{endianity},"c",$buf); | ||
| 1020 | } | ||
| 1021 | |||
| 1022 | sub sqlGetConnDiagnostics($) { | ||
| 1023 | my $connref=shift; | ||
| 1024 | sqlGetDiagnostics($connref,undef); | ||
| 1025 | } | ||
| 1026 | |||
| 1027 | sub sqlGetStmtDiagnostics($) { | ||
| 1028 | my $stmtref=shift; | ||
| 1029 | my $connref=$$stmtref{'Database'}; | ||
| 1030 | sqlGetDiagnostics($connref,$stmtref); | ||
| 1031 | } | ||
| 1032 | |||
| 1033 | sub sqlGetDiagnostics($$) { | ||
| 1034 | my $connref=shift; | ||
| 1035 | my $stmtref=shift; | ||
| 1036 | my $stmt_handle=0; | ||
| 1037 | my $func=FUNC_GET_DIAGS; | ||
| 1038 | if(defined($stmtref)) { | ||
| 1039 | $func=FUNC_GET_STMT_DIAGS; | ||
| 1040 | $stmt_handle=$$stmtref{stmt_handle}; | ||
| 1041 | } | ||
| 1042 | my @arg=(2,$stmt_handle,$func,64*1024-10); | ||
| 1043 | my $buf=_pack($$connref{endianity},"BBBB",\@arg); | ||
| 1044 | $$connref{osocket}->write($buf,length($buf)) or return undef; | ||
| 1045 | $$connref{osocket}->flush() or return undef; | ||
| 1046 | $$connref{isocket}->read($buf,6) or return undef; | ||
| 1047 | my ($len,$ret,$pending)=_unpack($$connref{endianity},"BBB",$buf); | ||
| 1048 | if($len==0) { | ||
| 1049 | if($pending!=0) { | ||
| 1050 | return undef; #oops! diagnostics that are not received? | ||
| 1051 | } | ||
| 1052 | return 1; #ok, no diagnostics | ||
| 1053 | } | ||
| 1054 | $$connref{isocket}->read($buf,$len) or return undef; | ||
| 1055 | my ($diags)=_unpack($$connref{endianity},"z",$buf); | ||
| 1056 | $diags; | ||
| 1057 | } | ||
| 1058 | |||
| 1059 | sub sqlNest($) { | ||
| 1060 | my $stmtref=shift; | ||
| 1061 | _plain_mesg($stmtref,FUNC_NEST); | ||
| 1062 | } | ||
| 1063 | |||
| 1064 | sub sqlCommit($) { | ||
| 1065 | my $stmtref=shift; | ||
| 1066 | _plain_mesg($stmtref,FUNC_COMMIT); | ||
| 1067 | } | ||
| 1068 | |||
| 1069 | sub sqlRollback($) { | ||
| 1070 | my $stmtref=shift; | ||
| 1071 | _plain_mesg($stmtref,FUNC_ROLLBACK); | ||
| 1072 | } | ||
| 1073 | |||
| 1074 | sub sqlGetConnPending($) { | ||
| 1075 | my $connref=shift; | ||
| 1076 | $$connref{pending}; | ||
| 1077 | } | ||
| 1078 | |||
| 1079 | sub sqlGetStmtPending($) { | ||
| 1080 | my $stmtref=shift; | ||
| 1081 | $$stmtref{pending}; | ||
| 1082 | } | ||
| 1083 | |||
| 1084 | sub _unpack_coldefs($$$) { | ||
| 1085 | my $endianity=shift; | ||
| 1086 | my $colnb=shift; | ||
| 1087 | my $buf=shift; | ||
| 1088 | my $i; | ||
| 1089 | my @res=(); | ||
| 1090 | for($i=0; $i<$colnb; $i++) { | ||
| 1091 | my ($name,$type,$length,$scale,$nullable)=_unpack($endianity,"z30abbA",$buf); | ||
| 1092 | $buf=substr($buf,_column_def_len); | ||
| 1093 | my %coldef=( | ||
| 1094 | name => $name, | ||
| 1095 | type => $type, | ||
| 1096 | len => $length, | ||
| 1097 | scale => $scale, | ||
| 1098 | nullable => $nullable, | ||
| 1099 | ); | ||
| 1100 | push(@res,\%coldef); | ||
| 1101 | } | ||
| 1102 | @res; | ||
| 1103 | } | ||
| 1104 | |||
| 1105 | sub sqlGetParamDescr($) { | ||
| 1106 | my $stmtref=shift; | ||
| 1107 | my $connref=$$stmtref{'Database'}; | ||
| 1108 | my @arg=(0,$$stmtref{stmt_handle},FUNC_DESCRIBE_PARAMS); | ||
| 1109 | my $buf=_pack($$connref{endianity},"BBB",\@arg); | ||
| 1110 | $$connref{osocket}->write($buf,length($buf)) or return undef; | ||
| 1111 | $$connref{osocket}->flush() or return undef; | ||
| 1112 | $$connref{isocket}->read($buf,6) or return undef; | ||
| 1113 | my ($len,$ret,$pending)=_unpack($$connref{endianity},"BBB",$buf); | ||
| 1114 | $$stmtref{pending}=$pending; | ||
| 1115 | if($len==0) { return undef; } | ||
| 1116 | $$connref{isocket}->read($buf,$len) or return undef; | ||
| 1117 | my ($colnb)=_unpack($$connref{endianity},"B",$buf); $buf=substr($buf,2); | ||
| 1118 | my @params=_unpack_coldefs($$connref{endianity},$colnb,$buf); | ||
| 1119 | $$stmtref{paramcount}=$colnb; | ||
| 1120 | $$stmtref{params}=\@params; | ||
| 1121 | $ret==RET_OK; | ||
| 1122 | } | ||
| 1123 | |||
| 1124 | sub sqlGetParamNb($) { | ||
| 1125 | my $stmtref=shift; | ||
| 1126 | $$stmtref{paramcount}; | ||
| 1127 | } | ||
| 1128 | |||
| 1129 | sub sqlGetParamType($$) { | ||
| 1130 | my $stmtref=shift; | ||
| 1131 | my $icol=shift; | ||
| 1132 | my $paramsc=$$stmtref{params}; | ||
| 1133 | my $coldef=$$paramsc[$icol]; | ||
| 1134 | $$coldef{type}; | ||
| 1135 | } | ||
| 1136 | |||
| 1137 | sub sqlGetParamLength($$) { | ||
| 1138 | my $stmtref=shift; | ||
| 1139 | my $icol=shift; | ||
| 1140 | my $paramsc=$$stmtref{params}; | ||
| 1141 | my $coldef=$$paramsc[$icol]; | ||
| 1142 | $$coldef{len}; | ||
| 1143 | } | ||
| 1144 | |||
| 1145 | sub sqlGetParamPrecision($$) { | ||
| 1146 | my $stmtref=shift; | ||
| 1147 | my $icol=shift; | ||
| 1148 | my $paramsc=$$stmtref{params}; | ||
| 1149 | my $coldef=$$paramsc[$icol]; | ||
| 1150 | $$coldef{len}; | ||
| 1151 | } | ||
| 1152 | |||
| 1153 | sub sqlGetParamScale($$) { | ||
| 1154 | my $stmtref=shift; | ||
| 1155 | my $icol=shift; | ||
| 1156 | my $paramsc=$$stmtref{params}; | ||
| 1157 | my $coldef=$$paramsc[$icol]; | ||
| 1158 | $$coldef{scale}; | ||
| 1159 | } | ||
| 1160 | |||
| 1161 | sub sqlGetOutputColDescr($) { | ||
| 1162 | my $stmtref=shift; | ||
| 1163 | my $connref=$$stmtref{'Database'}; | ||
| 1164 | my @arg=(0,$$stmtref{stmt_handle},FUNC_DESCRIBE_RES_COLS); | ||
| 1165 | my $buf=_pack($$connref{endianity},"BBB",\@arg); | ||
| 1166 | $$connref{osocket}->write($buf,length($buf)) or return undef; | ||
| 1167 | $$connref{osocket}->flush() or return undef; | ||
| 1168 | $$connref{isocket}->read($buf,6) or return undef; | ||
| 1169 | my ($len,$ret,$pending)=_unpack($$connref{endianity},"BBB",$buf); | ||
| 1170 | $$stmtref{pending}=$pending; | ||
| 1171 | if($len==0) { return undef; } | ||
| 1172 | $$connref{isocket}->read($buf,$len) or return undef; | ||
| 1173 | my ($colnb)=_unpack($$connref{endianity},"B",$buf); $buf=substr($buf,2); | ||
| 1174 | my @res=_unpack_coldefs($$connref{endianity},$colnb,$buf); | ||
| 1175 | $$stmtref{colnb}=$colnb; | ||
| 1176 | $$stmtref{res}=\@res; | ||
| 1177 | $$stmtref{row_width}=0; | ||
| 1178 | $$stmtref{row_template}=''; | ||
| 1179 | my $coldefref; | ||
| 1180 | foreach $coldefref (@res) { | ||
| 1181 | $$stmtref{row_width}+=_column_width($coldefref); | ||
| 1182 | $$stmtref{row_template}.=_column_pack_template($coldefref); | ||
| 1183 | } | ||
| 1184 | $ret==RET_OK; | ||
| 1185 | } | ||
| 1186 | |||
| 1187 | sub sqlGetOutputColNb($) { | ||
| 1188 | my $stmtref=shift; | ||
| 1189 | $$stmtref{colnb}; | ||
| 1190 | } | ||
| 1191 | |||
| 1192 | sub sqlGetOutputColName($$) { | ||
| 1193 | my $stmtref=shift; | ||
| 1194 | my $icol=shift; | ||
| 1195 | my $resc=$$stmtref{res}; | ||
| 1196 | my $coldef=$$resc[$icol]; | ||
| 1197 | $$coldef{name}; | ||
| 1198 | } | ||
| 1199 | |||
| 1200 | sub sqlGetOutputColType($$) { | ||
| 1201 | my $stmtref=shift; | ||
| 1202 | my $icol=shift; | ||
| 1203 | my $resc=$$stmtref{res}; | ||
| 1204 | my $coldef=$$resc[$icol]; | ||
| 1205 | $$coldef{type}; | ||
| 1206 | } | ||
| 1207 | |||
| 1208 | sub sqlGetOutputColLength($$) { | ||
| 1209 | my $stmtref=shift; | ||
| 1210 | my $icol=shift; | ||
| 1211 | my $resc=$$stmtref{res}; | ||
| 1212 | my $coldef=$$resc[$icol]; | ||
| 1213 | $$coldef{len}; | ||
| 1214 | } | ||
| 1215 | |||
| 1216 | sub sqlGetOutputColPrecision($$) { | ||
| 1217 | my $stmtref=shift; | ||
| 1218 | my $icol=shift; | ||
| 1219 | my $resc=$$stmtref{res}; | ||
| 1220 | my $coldef=$$resc[$icol]; | ||
| 1221 | $$coldef{len}; | ||
| 1222 | } | ||
| 1223 | |||
| 1224 | sub sqlGetOutputColScale($$) { | ||
| 1225 | my $stmtref=shift; | ||
| 1226 | my $icol=shift; | ||
| 1227 | my $resc=$$stmtref{res}; | ||
| 1228 | my $coldef=$$resc[$icol]; | ||
| 1229 | $$coldef{scale}; | ||
| 1230 | } | ||
| 1231 | |||
| 1232 | sub sqlGetOutputColNullable($$) { | ||
| 1233 | my $stmtref=shift; | ||
| 1234 | my $icol=shift; | ||
| 1235 | my $resc=$$stmtref{res}; | ||
| 1236 | my $coldef=$$resc[$icol]; | ||
| 1237 | $$coldef{nullable}; | ||
| 1238 | } | ||
| 1239 | |||
| 1240 | sub sqlGetRowsetSize($) { | ||
| 1241 | my $stmtref=shift; | ||
| 1242 | $$stmtref{rowset_size}; | ||
| 1243 | } | ||
| 1244 | |||
| 1245 | sub sqlSetRowsetSize($$) { | ||
| 1246 | my $stmtref=shift; | ||
| 1247 | my $sz=shift; | ||
| 1248 | my $row_width=$$stmtref{row_width}; | ||
| 1249 | my $max_sz=int((_MAXMESGLEN-2-6)/($row_width+6)); | ||
| 1250 | if($sz>$max_sz) { | ||
| 1251 | $sz=$max_sz; | ||
| 1252 | } | ||
| 1253 | $$stmtref{rowset_size}=$sz; | ||
| 1254 | } | ||
| 1255 | |||
| 1256 | sub _column_width($) { | ||
| 1257 | my $coldefref=shift; | ||
| 1258 | my $type=$$coldefref{type}; | ||
| 1259 | my $len=$$coldefref{len}; | ||
| 1260 | my $w; | ||
| 1261 | if($type==T_DECIMAL || $type==T_NUMERIC) { | ||
| 1262 | $w=_type_size($type); | ||
| 1263 | } else { | ||
| 1264 | $w=$len*_type_size($type)+_type_overhead($type); | ||
| 1265 | } | ||
| 1266 | $w+1; #plus null indicator | ||
| 1267 | } | ||
| 1268 | |||
| 1269 | sub _column_pack_template($) { | ||
| 1270 | my $coldefref=shift; | ||
| 1271 | my $t=$$coldefref{type}; | ||
| 1272 | my $len=$$coldefref{len}; | ||
| 1273 | if($t==T_BIGINT) { | ||
| 1274 | return "Ad" | ||
| 1275 | } elsif($t==T_TIMESTAMP) { | ||
| 1276 | return "Ai"; | ||
| 1277 | } elsif($t==T_UBIGINT) { | ||
| 1278 | return "AD" | ||
| 1279 | } elsif($t==T_DECIMAL || $t==T_NUMERIC || $t==T_DOUBLE || $t==T_FLOAT) { | ||
| 1280 | return "AF"; | ||
| 1281 | } elsif($t==T_INTEGER) { | ||
| 1282 | return "Ac"; | ||
| 1283 | } elsif($t==T_UINTEGER) { | ||
| 1284 | return "AC"; | ||
| 1285 | } elsif($t==T_TIME) { | ||
| 1286 | return "Ah"; | ||
| 1287 | } elsif($t==T_DATE) { | ||
| 1288 | return "Ag"; | ||
| 1289 | } elsif($t==T_REAL) { | ||
| 1290 | return "Af"; | ||
| 1291 | } elsif($t==T_SMALLINT) { | ||
| 1292 | return "Ab"; | ||
| 1293 | } elsif($t==T_USMALLINT) { | ||
| 1294 | return "AB"; | ||
| 1295 | } elsif($t==T_LONGVARCHAR || $t==T_LONGVARBINARY) { | ||
| 1296 | return "AC"; | ||
| 1297 | } elsif($t==T_TINYINT) { | ||
| 1298 | return "Aa"; | ||
| 1299 | } elsif($t==T_BIT || $t==T_UTINYINT) { | ||
| 1300 | return "AA"; | ||
| 1301 | } elsif($t==T_CHAR || T_VARCHAR) { | ||
| 1302 | return "Az" . $len; | ||
| 1303 | } elsif($t==T_BINARY) { | ||
| 1304 | return "Ay" . $len; | ||
| 1305 | } elsif($t==T_VARBINARY) { | ||
| 1306 | return "AY" . $len; | ||
| 1307 | } else { | ||
| 1308 | return undef; | ||
| 1309 | } | ||
| 1310 | } | ||
| 1311 | |||
| 1312 | sub _type_size($) { | ||
| 1313 | my $t=shift; | ||
| 1314 | if($t==T_TIMESTAMP || $t==T_BIGINT || $t==T_UBIGINT || | ||
| 1315 | $t==T_DECIMAL || $t==T_NUMERIC || $t==T_DOUBLE || $t==T_FLOAT) { | ||
| 1316 | return 8; | ||
| 1317 | } elsif($t==T_INTEGER || $t==T_UINTEGER || $t==T_TIME || $t==T_DATE || | ||
| 1318 | $t==T_REAL) { | ||
| 1319 | return 4; | ||
| 1320 | } elsif($t==T_SMALLINT || $t==T_USMALLINT) { | ||
| 1321 | return 2; | ||
| 1322 | } elsif($t==T_LONGVARCHAR || $t==T_LONGVARBINARY) { | ||
| 1323 | return 0; | ||
| 1324 | # so that 0*length+type_overhead=type_overhead | ||
| 1325 | } else { | ||
| 1326 | return 1; | ||
| 1327 | } | ||
| 1328 | } | ||
| 1329 | |||
| 1330 | sub _type_overhead($) { | ||
| 1331 | my $t=shift; | ||
| 1332 | if($t==T_CHAR || $t==T_VARCHAR) { | ||
| 1333 | return 1; | ||
| 1334 | } elsif($t==T_VARBINARY) { | ||
| 1335 | return 2; | ||
| 1336 | } elsif($t==T_LONGVARCHAR || $t==T_LONGVARBINARY) { | ||
| 1337 | return 4; | ||
| 1338 | } else { | ||
| 1339 | return 0; | ||
| 1340 | } | ||
| 1341 | } | ||
| 1342 | |||
| 1343 | sub sqlCursorMove($$$$) { | ||
| 1344 | my $stmtref=shift; | ||
| 1345 | my $irow=shift; | ||
| 1346 | my $func=shift; | ||
| 1347 | my $fetch=shift; | ||
| 1348 | my $connref=$$stmtref{'Database'}; | ||
| 1349 | my @arg=(6,$$stmtref{stmt_handle},$func,$fetch,$irow); | ||
| 1350 | my $buf=_pack($$connref{endianity},"BBBBC",\@arg); | ||
| 1351 | $$connref{osocket}->write($buf,length($buf)) or return undef; | ||
| 1352 | $$connref{osocket}->flush() or return undef; | ||
| 1353 | $$connref{isocket}->read($buf,6) or return undef; | ||
| 1354 | my ($len,$ret,$pending)=_unpack($$connref{endianity},"BBB",$buf); | ||
| 1355 | $$stmtref{pending}=$pending; | ||
| 1356 | if($len==0) { return undef; } | ||
| 1357 | $$connref{isocket}->read($buf,$len) or return undef; | ||
| 1358 | if($ret!=RET_OK) { | ||
| 1359 | return undef; | ||
| 1360 | } | ||
| 1361 | my ($w)=_unpack($$connref{endianity},"B",$buf); $buf=substr($buf,2); | ||
| 1362 | if($w != $$stmtref{row_width}) { | ||
| 1363 | return undef; | ||
| 1364 | } | ||
| 1365 | my @rows=(); | ||
| 1366 | my $i; | ||
| 1367 | for($i=0; $i<$fetch; $i++) { | ||
| 1368 | my ($st,$bm)=_unpack($$connref{endianity},"bC",$buf); $buf=substr($buf,6); | ||
| 1369 | my $rw=undef; | ||
| 1370 | if($st==ROW_OK) { | ||
| 1371 | my @x=_unpack($$connref{endianity},$$stmtref{row_template},$buf); | ||
| 1372 | $buf=substr($buf,$w); | ||
| 1373 | $rw=_collapse_null_ind(\@x); | ||
| 1374 | } | ||
| 1375 | my %rowdata=( state=> $st, bookmark=> $bm, data=>$rw); | ||
| 1376 | push(@rows,\%rowdata); | ||
| 1377 | } | ||
| 1378 | $$stmtref{rows}=\@rows; | ||
| 1379 | $$stmtref{currrow}=0; | ||
| 1380 | $ret==RET_OK; | ||
| 1381 | } | ||
| 1382 | |||
| 1383 | sub _collapse_null_ind($) { | ||
| 1384 | my $listref=shift; | ||
| 1385 | my @data=(); | ||
| 1386 | my $i; | ||
| 1387 | for($i=0; $i<scalar(@$listref); $i+=2) { | ||
| 1388 | if($$listref[$i]==0) { | ||
| 1389 | push(@data,$$listref[$i+1]); | ||
| 1390 | } else { | ||
| 1391 | push(@data,undef); | ||
| 1392 | } | ||
| 1393 | } | ||
| 1394 | \@data; | ||
| 1395 | } | ||
| 1396 | |||
| 1397 | sub sqlCursorThis($) { | ||
| 1398 | my $stmtref=shift; | ||
| 1399 | sqlCursorMove($stmtref,0,FUNC_CURSOR_THIS,$$stmtref{rowset_size}); | ||
| 1400 | } | ||
| 1401 | |||
| 1402 | sub sqlCursorFirst($$) { | ||
| 1403 | my $stmtref=shift; | ||
| 1404 | my $irow=shift; | ||
| 1405 | sqlCursorMove($stmtref,$irow,FUNC_CURSOR_FIRST,$$stmtref{rowset_size}); | ||
| 1406 | } | ||
| 1407 | |||
| 1408 | sub sqlCursorNext($$) { | ||
| 1409 | my $stmtref=shift; | ||
| 1410 | my $irow=shift; | ||
| 1411 | sqlCursorMove($stmtref,$irow,FUNC_CURSOR_NEXT,$$stmtref{rowset_size}); | ||
| 1412 | } | ||
| 1413 | |||
| 1414 | sub sqlCursorLast($$) { | ||
| 1415 | my $stmtref=shift; | ||
| 1416 | my $irow=shift; | ||
| 1417 | sqlCursorMove($stmtref,$irow,FUNC_CURSOR_LAST,$$stmtref{rowset_size}); | ||
| 1418 | } | ||
| 1419 | |||
| 1420 | sub sqlCursorPrev($$) { | ||
| 1421 | my $stmtref=shift; | ||
| 1422 | my $irow=shift; | ||
| 1423 | sqlCursorMove($stmtref,$irow,FUNC_CURSOR_PREV,$$stmtref{rowset_size}); | ||
| 1424 | } | ||
| 1425 | |||
| 1426 | sub sqlCursorBookmark($$) { | ||
| 1427 | my $stmtref=shift; | ||
| 1428 | my $bm=shift; | ||
| 1429 | sqlCursorMove($stmtref,$bm,FUNC_CURSOR_GOTO_BM,$$stmtref{rowset_size}); | ||
| 1430 | } | ||
| 1431 | |||
| 1432 | sub sqlColValue($$$) { | ||
| 1433 | my $stmtref=shift; | ||
| 1434 | my $icol=shift; | ||
| 1435 | my $irow=shift; | ||
| 1436 | my $rows=$$stmtref{rows}; | ||
| 1437 | my $row=$$rows[$irow]; | ||
| 1438 | my $data=$$row{data}; | ||
| 1439 | #$$$$$stmtref{rows}[$irow]{data}[$icol]; | ||
| 1440 | $$data[$icol]; | ||
| 1441 | } | ||
| 1442 | |||
| 1443 | sub sqlColIsNull($$$) { | ||
| 1444 | my $stmtref=shift; | ||
| 1445 | my $icol=shift; | ||
| 1446 | my $irow=shift; | ||
| 1447 | undefined(sqlColValue($stmtref,$icol,$irow)); | ||
| 1448 | } | ||
| 1449 | |||
| 1450 | sub sqlRowState($$) { | ||
| 1451 | my $stmtref=shift; | ||
| 1452 | my $irow=shift; | ||
| 1453 | my $rows=$$stmtref{rows}; | ||
| 1454 | my $row=$$rows[$irow]; | ||
| 1455 | $$row{state}; | ||
| 1456 | } | ||
| 1457 | |||
| 1458 | sub sqlRowBookmark($$) { | ||
| 1459 | my $stmtref=shift; | ||
| 1460 | my $irow=shift; | ||
| 1461 | my $rows=$$stmtref{rows}; | ||
| 1462 | my $row=$$rows[$irow]; | ||
| 1463 | $$row{bookmark}; | ||
| 1464 | } | ||
| 1465 | |||
| 1466 | package DBD::Ovrimos; | ||
| 1467 | |||
| 1468 | use vars qw($VERSION); #so that VERSION_FROM will work | ||
| 1469 | |||
| 1470 | $VERSION="0.11"; | ||
| 1471 | $DBD::Ovrimos::drh=undef; | ||
| 1472 | @DBD::Ovrimos::connections=(); | ||
| 1473 | $DBD::Ovrimos::err=''; | ||
| 1474 | $DBD::Ovrimos::errStr=''; | ||
| 1475 | |||
| 1476 | sub driver { | ||
| 1477 | return $DBD::Ovrimos::drh if $DBD::Ovrimos::drh; | ||
| 1478 | $DBD::Ovrimos::drh=DBI::_new_drh('DBD::Ovrimos::dr', | ||
| 1479 | { | ||
| 1480 | 'Name' => 'Ovrimos', | ||
| 1481 | 'Version' => $DBD::Ovrimos::VERSION, | ||
| 1482 | 'Err' => \$DBD::Ovrimos::err, | ||
| 1483 | 'Errstr' => \$DBD::Ovrimos::errStr, | ||
| 1484 | 'Atribution' => 'DBD::Ovrimos by Dimitrios Souflis', | ||
| 1485 | }); | ||
| 1486 | } | ||
| 1487 | |||
| 1488 | sub AGIdb() { | ||
| 1489 | my ($ofh,$ifh); | ||
| 1490 | $ofh=new IO::Handle; | ||
| 1491 | $ifh=new IO::Handle; | ||
| 1492 | if(!$ifh->fdopen(fileno(STDIN),"r")) { | ||
| 1493 | return undef; | ||
| 1494 | } | ||
| 1495 | if(!$ofh->fdopen(fileno(STDOUT),"w")) { | ||
| 1496 | return undef; | ||
| 1497 | } | ||
| 1498 | my @empty_array=(); | ||
| 1499 | bless { | ||
| 1500 | 'endianity' =>$DBD::Ovrimos::lowlevel::_local_byte_order, | ||
| 1501 | 'isocket' =>$ifh, | ||
| 1502 | 'osocket' =>$ofh, | ||
| 1503 | 'stmts' =>\@empty_array, | ||
| 1504 | 'AutoCommit' =>1, | ||
| 1505 | 'PrintError' =>0, | ||
| 1506 | 'RaiseError' =>0, | ||
| 1507 | 'Active' =>1, | ||
| 1508 | 'AGI' =>1, | ||
| 1509 | 'Err' => \$DBD::Ovrimos::err, | ||
| 1510 | 'Errstr' => \$DBD::Ovrimos::errStr, | ||
| 1511 | }, 'DBD::Ovrimos::db'; | ||
| 1512 | } | ||
| 1513 | |||
| 1514 | package DBD::Ovrimos::dr; | ||
| 1515 | |||
| 1516 | $DBD::Ovrimos::dr::imp_data_size=$DBD::Ovrimos::dr::imp_data_size=0; | ||
| 1517 | |||
| 1518 | sub errstr { | ||
| 1519 | my $self=shift; | ||
| 1520 | ${$self->{'Errstr'}}; | ||
| 1521 | } | ||
| 1522 | |||
| 1523 | sub state { | ||
| 1524 | my $self=shift; | ||
| 1525 | substr(${$self->{'Errstr'}},0,5); | ||
| 1526 | } | ||
| 1527 | |||
| 1528 | sub err { | ||
| 1529 | my $self=shift; | ||
| 1530 | if(${$self->{'Errstr'}} eq '') { | ||
| 1531 | return 0; | ||
| 1532 | } else { | ||
| 1533 | return 1; # arbitrary non-0 value | ||
| 1534 | } | ||
| 1535 | } | ||
| 1536 | |||
| 1537 | sub connect { | ||
| 1538 | my $driver=shift; | ||
| 1539 | my $dsn=shift; | ||
| 1540 | my $user=shift; | ||
| 1541 | my $pass=shift; | ||
| 1542 | my $attr=shift; | ||
| 1543 | my $host; | ||
| 1544 | my $port; | ||
| 1545 | $DBD::Ovrimos::errStr=''; | ||
| 1546 | if($dsn =~ /^(.*):(.*)/) { | ||
| 1547 | ($host,$port)=($1,$2); | ||
| 1548 | } else { | ||
| 1549 | $DBD::Ovrimos::errStr.="08001 Malformed dsn '$dsn'"; | ||
| 1550 | return undef; | ||
| 1551 | } | ||
| 1552 | my $connref=DBD::Ovrimos::lowlevel::sqlConnect($host,$port,$user,$pass); | ||
| 1553 | if(!defined($connref)) { | ||
| 1554 | my $o=DBD::Ovrimos::lowlevel::sqlConnectOutcome(); | ||
| 1555 | if($o==DBD::Ovrimos::lowlevel::c_conn_failed) { | ||
| 1556 | $DBD::Ovrimos::errStr.="08001 Connection to $host:$port impossible"; | ||
| 1557 | } elsif($o==DBD::Ovrimos::lowlevel::c_auth_failed) { | ||
| 1558 | $DBD::Ovrimos::errStr.="08004 Authentication as $user failed"; | ||
| 1559 | } else { | ||
| 1560 | $DBD::Ovrimos::errStr.="08S01 Connection to $host:$port failed"; | ||
| 1561 | } | ||
| 1562 | return undef; | ||
| 1563 | } | ||
| 1564 | my ($dbh,$h)=DBI::_new_dbh($DBD::Ovrimos::drh,$connref,undef); | ||
| 1565 | push(@DBD::Ovrimos::connections,$h); | ||
| 1566 | $dbh; | ||
| 1567 | } | ||
| 1568 | |||
| 1569 | sub DESTROY { | ||
| 1570 | disconnect_all(); | ||
| 1571 | } | ||
| 1572 | |||
| 1573 | sub disconnect_all { | ||
| 1574 | my $connref; | ||
| 1575 | for $connref (@DBD::Ovrimos::connections) { | ||
| 1576 | if(!DBD::Ovrimos::lowlevel::sqlDisconnect($connref)) { | ||
| 1577 | $DBD::Ovrimos::errStr.="01002 Disconnect error"; | ||
| 1578 | } | ||
| 1579 | } | ||
| 1580 | } | ||
| 1581 | |||
| 1582 | package DBD::Ovrimos::db; | ||
| 1583 | |||
| 1584 | $DBD::Ovrimos::db::imp_data_size=$DBD::Ovrimos::db::imp_data_size=0; | ||
| 1585 | |||
| 1586 | sub _w { | ||
| 1587 | my $self=shift; | ||
| 1588 | my $msg=shift; | ||
| 1589 | |||
| 1590 | if($$self{'PrintError'}) { | ||
| 1591 | warn $msg; | ||
| 1592 | } | ||
| 1593 | if($$self{'RaiseError'}) { | ||
| 1594 | die $msg; | ||
| 1595 | } | ||
| 1596 | if(${$self->{'Errstr'}} ne '') { | ||
| 1597 | ${$self->{'Errstr'}}.="\n"; | ||
| 1598 | } | ||
| 1599 | ${$self->{'Errstr'}}.=$msg; | ||
| 1600 | $msg; | ||
| 1601 | } | ||
| 1602 | |||
| 1603 | sub errstr { | ||
| 1604 | my $self=shift; | ||
| 1605 | ${$self->{'Errstr'}}; | ||
| 1606 | } | ||
| 1607 | |||
| 1608 | sub state { | ||
| 1609 | my $self=shift; | ||
| 1610 | substr(${$self->{'Errstr'}},0,5); | ||
| 1611 | } | ||
| 1612 | |||
| 1613 | sub err { | ||
| 1614 | my $self=shift; | ||
| 1615 | if(${$self->{'Errstr'}} eq '') { | ||
| 1616 | return 0; | ||
| 1617 | } else { | ||
| 1618 | return 1; # arbitrary non-0 value | ||
| 1619 | } | ||
| 1620 | } | ||
| 1621 | |||
| 1622 | sub DESTROY { | ||
| 1623 | my $self=shift; | ||
| 1624 | if($$self{'AGI'}) { | ||
| 1625 | return 1; | ||
| 1626 | } | ||
| 1627 | DBD::Ovrimos::lowlevel::sqlDisconnect($self); | ||
| 1628 | } | ||
| 1629 | |||
| 1630 | sub disconnect { | ||
| 1631 | my $self=shift; | ||
| 1632 | my $i; | ||
| 1633 | ${$self->{'Errstr'}}=''; | ||
| 1634 | for($i=0; $i<scalar(@DBD::Ovrimos::connections);$i++) { | ||
| 1635 | if($DBD::Ovrimos::connections[$i]->{osocket}==$self->{osocket}) { | ||
| 1636 | splice(@DBD::Ovrimos::connections,$i,1); | ||
| 1637 | last; | ||
| 1638 | } | ||
| 1639 | } | ||
| 1640 | if(!DBD::Ovrimos::lowlevel::sqlDisconnect($self)) { | ||
| 1641 | _w($self,'01002 Disconnect error'); | ||
| 1642 | } | ||
| 1643 | 1; | ||
| 1644 | } | ||
| 1645 | |||
| 1646 | sub do { | ||
| 1647 | my $self=shift; | ||
| 1648 | my $cmd=shift; | ||
| 1649 | my @params=@_; | ||
| 1650 | ${$self->{'Errstr'}}=''; | ||
| 1651 | my $stmtref=_reuse_stmt($self); | ||
| 1652 | if(!defined($stmtref)) { | ||
| 1653 | if($$self{'AGI'}) { | ||
| 1654 | return undef; | ||
| 1655 | } | ||
| 1656 | $stmtref=DBD::Ovrimos::lowlevel::sqlAllocStmt($self); | ||
| 1657 | if(!defined($stmtref)) { | ||
| 1658 | _w($self,DBD::Ovrimos::lowlevel::sqlGetConnDiagnostics($self)); | ||
| 1659 | return undef; | ||
| 1660 | } | ||
| 1661 | $$stmtref{'Database'}=$self; # must do it by hand here | ||
| 1662 | } | ||
| 1663 | # if params supplied, prepare, put params and then execute | ||
| 1664 | my $ret1; | ||
| 1665 | if(defined(@params)) { | ||
| 1666 | $ret1=DBD::Ovrimos::lowlevel::sqlPrepare($stmtref,$cmd); | ||
| 1667 | if(!$ret1) { | ||
| 1668 | _w($self,DBD::Ovrimos::lowlevel::sqlGetStmtDiagnostics($stmtref)); | ||
| 1669 | if(!$$self{'AGI'}) { | ||
| 1670 | DBD::Ovrimos::lowlevel::sqlFreeStmt($stmtref); | ||
| 1671 | } | ||
| 1672 | return undef; | ||
| 1673 | } | ||
| 1674 | my $parnb=DBD::Ovrimos::lowlevel::sqlGetParamNb($stmtref); | ||
| 1675 | if($parnb!=scalar(@params)) { | ||
| 1676 | _w($self,'07001 Wrong number of parameters'); | ||
| 1677 | if(!$$self{'AGI'}) { | ||
| 1678 | DBD::Ovrimos::lowlevel::sqlFreeStmt($stmtref); | ||
| 1679 | } | ||
| 1680 | return undef; | ||
| 1681 | } | ||
| 1682 | my $i; | ||
| 1683 | for($i=1; $i<=$parnb; $i++) { | ||
| 1684 | my $rv=bind_param($self,$i,$params[$i-1]); | ||
| 1685 | if(!$rv) { | ||
| 1686 | if(!$$self{'AGI'}) { | ||
| 1687 | DBD::Ovrimos::lowlevel::sqlFreeStmt($stmtref); | ||
| 1688 | } | ||
| 1689 | return undef; | ||
| 1690 | } | ||
| 1691 | } | ||
| 1692 | $ret1=DBD::Ovrimos::lowlevel::sqlExec($stmtref); | ||
| 1693 | } else { | ||
| 1694 | $ret1=DBD::Ovrimos::lowlevel::sqlExecDirect($stmtref,$cmd); | ||
| 1695 | } | ||
| 1696 | if(!$ret1) { | ||
| 1697 | _w($self,DBD::Ovrimos::lowlevel::sqlGetStmtDiagnostics($stmtref)); | ||
| 1698 | if(!$$self{'AGI'}) { | ||
| 1699 | DBD::Ovrimos::lowlevel::sqlFreeStmt($stmtref); | ||
| 1700 | } | ||
| 1701 | return undef; | ||
| 1702 | } | ||
| 1703 | my $rows=DBD::Ovrimos::lowlevel::sqlGetRowCount($stmtref); | ||
| 1704 | if($$self{'Autocommit'}) { | ||
| 1705 | if(!$$self{'AGI'}) { | ||
| 1706 | my $ret2=DBD::Ovrimos::lowlevel::sqlFreeStmt($stmtref); | ||
| 1707 | if(!$ret2) { | ||
| 1708 | _w($self,DBD::Ovrimos::lowlevel::sqlGetConnDiagnostics($self)); | ||
| 1709 | return undef; | ||
| 1710 | } | ||
| 1711 | } | ||
| 1712 | } else { | ||
| 1713 | my $stmts=$$self{stmts}; | ||
| 1714 | $$stmtref{'Active'}=0; | ||
| 1715 | push(@$stmts,$stmtref); # will be Free'd at _trans | ||
| 1716 | } | ||
| 1717 | $rows==0? '0E0' : $rows; | ||
| 1718 | } | ||
| 1719 | |||
| 1720 | sub prepare { | ||
| 1721 | my $self=shift; | ||
| 1722 | my $cmd=shift; | ||
| 1723 | my $attr=shift; | ||
| 1724 | ${$self->{'Errstr'}}=''; | ||
| 1725 | my $stmtref=_reuse_stmt($self); | ||
| 1726 | if(!defined($stmtref)) { | ||
| 1727 | if($$self{'AGI'}) { | ||
| 1728 | return undef; | ||
| 1729 | } | ||
| 1730 | $stmtref=DBD::Ovrimos::lowlevel::sqlAllocStmt($self); | ||
| 1731 | if(!defined($stmtref)) { | ||
| 1732 | _w($self,DBD::Ovrimos::lowlevel::sqlGetStmtDiagnostics($self)); | ||
| 1733 | return undef; | ||
| 1734 | } | ||
| 1735 | } | ||
| 1736 | my $ret=DBD::Ovrimos::lowlevel::sqlPrepare($stmtref,$cmd); | ||
| 1737 | if(!$ret) { | ||
| 1738 | _w($self,DBD::Ovrimos::lowlevel::sqlGetStmtDiagnostics($stmtref)); | ||
| 1739 | return undef; | ||
| 1740 | } | ||
| 1741 | if(!DBD::Ovrimos::lowlevel::sqlGetOutputColDescr($stmtref)) { | ||
| 1742 | _w($self,DBD::Ovrimos::lowlevel::sqlGetStmtDiagnostics($stmtref)); | ||
| 1743 | return undef; | ||
| 1744 | } | ||
| 1745 | #This demands caching of rows. Will actually set it at closest possible num | ||
| 1746 | DBD::Ovrimos::lowlevel::sqlSetRowsetSize($stmtref,10000); | ||
| 1747 | if(!DBD::Ovrimos::lowlevel::sqlGetParamDescr($stmtref)) { | ||
| 1748 | _w($self,DBD::Ovrimos::lowlevel::sqlGetStmtDiagnostics($stmtref)); | ||
| 1749 | return undef; | ||
| 1750 | } | ||
| 1751 | if(!$$self{'AGI'}) { | ||
| 1752 | my ($sth,$h)=DBI::_new_sth($self,$stmtref,undef); | ||
| 1753 | my $stmts=$$self{stmts}; | ||
| 1754 | push(@$stmts,$h); | ||
| 1755 | return $sth; | ||
| 1756 | } else { | ||
| 1757 | my $stmts=$$self{stmts}; | ||
| 1758 | push(@$stmts,$stmtref); | ||
| 1759 | return $stmtref; | ||
| 1760 | } | ||
| 1761 | } | ||
| 1762 | |||
| 1763 | sub commit { | ||
| 1764 | my $self=shift; | ||
| 1765 | _trans($self,\&DBD::Ovrimos::lowlevel::sqlCommit); | ||
| 1766 | } | ||
| 1767 | |||
| 1768 | sub rollback { | ||
| 1769 | my $self=shift; | ||
| 1770 | _trans($self,\&DBD::Ovrimos::lowlevel::sqlRollback); | ||
| 1771 | } | ||
| 1772 | |||
| 1773 | sub _trans { | ||
| 1774 | my $self=shift; | ||
| 1775 | my $func=shift; | ||
| 1776 | my $stmts=$$self{stmts}; | ||
| 1777 | my $i; | ||
| 1778 | my $ret=1; | ||
| 1779 | ${$self->{'Errstr'}}=''; | ||
| 1780 | for($i=0; $i<scalar(@$stmts); $i++) { | ||
| 1781 | my $target=$$stmts[$i]; | ||
| 1782 | my $ret2=&$func($target); | ||
| 1783 | if(!$ret2) { | ||
| 1784 | _w($self,DBD::Ovrimos::lowlevel::sqlGetStmtDiagnostics($target)); | ||
| 1785 | } | ||
| 1786 | $ret &&= $ret2; | ||
| 1787 | if(!$$self{'AGI'}) { | ||
| 1788 | $ret2=DBD::Ovrimos::lowlevel::sqlFreeStmt($target); | ||
| 1789 | _delete_stmt_handle($self,$$target{stmt_handle}); | ||
| 1790 | } | ||
| 1791 | if(!$ret2) { | ||
| 1792 | _w($self,DBD::Ovrimos::lowlevel::sqlGetStmtDiagnostics($target)); | ||
| 1793 | } | ||
| 1794 | $ret &&= $ret2; | ||
| 1795 | } | ||
| 1796 | $ret; | ||
| 1797 | } | ||
| 1798 | |||
| 1799 | sub _delete_stmt_handle { | ||
| 1800 | my $self=shift; | ||
| 1801 | my $stmt_handle=shift; | ||
| 1802 | my $stmts=$$self{stmts}; | ||
| 1803 | my $i; | ||
| 1804 | for($i=0; $i<scalar(@$stmts); $i++) { | ||
| 1805 | my $target=$$stmts[$i]; | ||
| 1806 | if($$target{stmt_handle}==$stmt_handle) { | ||
| 1807 | splice(@$stmts,$i,1); | ||
| 1808 | last; | ||
| 1809 | } | ||
| 1810 | } | ||
| 1811 | } | ||
| 1812 | |||
| 1813 | sub _reuse_stmt { | ||
| 1814 | my $self=shift; | ||
| 1815 | my $stmts=$$self{stmts}; | ||
| 1816 | my $i; | ||
| 1817 | if(scalar(@$stmts)==0 && $$self{'AGI'}==1) { | ||
| 1818 | return bless { | ||
| 1819 | 'Database'=>$self, | ||
| 1820 | stmt_handle=>0, # dummy handle | ||
| 1821 | rowset_size=>1, | ||
| 1822 | currrow=>0, | ||
| 1823 | 'Active'=>1, | ||
| 1824 | 'Err' => \$DBD::Ovrimos::err, | ||
| 1825 | 'Errstr' => \$DBD::Ovrimos::errStr, | ||
| 1826 | }, 'DBD::Ovrimos::st'; | ||
| 1827 | } | ||
| 1828 | for($i=0; $i<scalar(@$stmts); $i++) { | ||
| 1829 | my $target=$$stmts[$i]; | ||
| 1830 | if(!$$target{'Active'}) { | ||
| 1831 | splice(@$stmts,$i,1); | ||
| 1832 | return $target; | ||
| 1833 | } | ||
| 1834 | } | ||
| 1835 | undef; | ||
| 1836 | } | ||
| 1837 | |||
| 1838 | sub FETCH { | ||
| 1839 | my $self=shift; | ||
| 1840 | my $key=shift; | ||
| 1841 | if($key eq 'AutoCommit') { | ||
| 1842 | return $$self{'AutoCommit'}; | ||
| 1843 | } elsif($key eq 'Active') { | ||
| 1844 | return $$self{'Active'}; | ||
| 1845 | } elsif($key eq 'Kids') { | ||
| 1846 | return scalar(@{$self->{stmts}}); | ||
| 1847 | } elsif($key eq 'ActiveKids') { | ||
| 1848 | return scalar(grep { $_->{'Active'} } @{$self->{stmts}}); | ||
| 1849 | } | ||
| 1850 | undef; | ||
| 1851 | } | ||
| 1852 | |||
| 1853 | sub STORE { | ||
| 1854 | my $self=shift; | ||
| 1855 | my $key=shift; | ||
| 1856 | my $value=shift; | ||
| 1857 | if($key eq 'AutoCommit') { | ||
| 1858 | $$self{'AutoCommit'}=$value; | ||
| 1859 | } else { | ||
| 1860 | $self->DBD::_::db::STORE($key,$value); | ||
| 1861 | } | ||
| 1862 | } | ||
| 1863 | |||
| 1864 | package DBD::Ovrimos::st; | ||
| 1865 | |||
| 1866 | $DBD::Ovrimos::st::imp_data_size=$DBD::Ovrimos::st::imp_data_size=0; | ||
| 1867 | |||
| 1868 | sub _w { | ||
| 1869 | my $self=shift; | ||
| 1870 | my $msg=shift; | ||
| 1871 | |||
| 1872 | if($$self{'PrintError'}) { | ||
| 1873 | warn $msg; | ||
| 1874 | } | ||
| 1875 | if($$self{'RaiseError'}) { | ||
| 1876 | die $msg; | ||
| 1877 | } | ||
| 1878 | if(${$self->{'Errstr'}} ne '') { | ||
| 1879 | ${$self->{'Errstr'}}.="\n"; | ||
| 1880 | } | ||
| 1881 | ${$self->{'Errstr'}}.=$msg; | ||
| 1882 | $msg; | ||
| 1883 | } | ||
| 1884 | |||
| 1885 | sub errstr { | ||
| 1886 | my $self=shift; | ||
| 1887 | ${$self->{'Errstr'}}; | ||
| 1888 | } | ||
| 1889 | |||
| 1890 | sub state { | ||
| 1891 | my $self=shift; | ||
| 1892 | substr(${$self->{'Errstr'}},0,5); | ||
| 1893 | } | ||
| 1894 | |||
| 1895 | sub err { | ||
| 1896 | my $self=shift; | ||
| 1897 | if(${$self->{'Errstr'}} eq '') { | ||
| 1898 | return 0; | ||
| 1899 | } else { | ||
| 1900 | return 1; # arbitrary non-0 value | ||
| 1901 | } | ||
| 1902 | } | ||
| 1903 | |||
| 1904 | sub execute { | ||
| 1905 | my $self=shift; | ||
| 1906 | my @params=@_; | ||
| 1907 | ${$self->{'Errstr'}}=''; | ||
| 1908 | if(defined(@params)) { | ||
| 1909 | my $parnb=$$self{paramcount}; | ||
| 1910 | if($parnb!=scalar(@params)) { | ||
| 1911 | _w($self,'07001 Wrong number of parameters'); | ||
| 1912 | if(!$$self{'AGI'}) { | ||
| 1913 | DBD::Ovrimos::lowlevel::sqlFreeStmt($self); | ||
| 1914 | _delete_stmt_handle($$self{'Database'},$$self{stmt_handle}); | ||
| 1915 | } | ||
| 1916 | return undef; | ||
| 1917 | } | ||
| 1918 | my $i; | ||
| 1919 | for($i=1; $i<=$parnb; $i++) { | ||
| 1920 | my $rv=bind_param($self,$i,$params[$i-1]); | ||
| 1921 | if(!$rv && !$$self{'AGI'}) { | ||
| 1922 | DBD::Ovrimos::lowlevel::sqlFreeStmt($self); | ||
| 1923 | _delete_stmt_handle($$self{'Database'},$$self{stmt_handle}); | ||
| 1924 | return undef; | ||
| 1925 | } | ||
| 1926 | } | ||
| 1927 | } | ||
| 1928 | if(!DBD::Ovrimos::lowlevel::sqlExec($self)) { | ||
| 1929 | _w($self,DBD::Ovrimos::lowlevel::sqlGetStmtDiagnostics($self)); | ||
| 1930 | return undef; | ||
| 1931 | } | ||
| 1932 | my $rows=DBD::Ovrimos::lowlevel::sqlGetRowCount($self); | ||
| 1933 | if(!defined($rows)) { | ||
| 1934 | _w($self,DBD::Ovrimos::lowlevel::sqlGetStmtDiagnostics($self)); | ||
| 1935 | } | ||
| 1936 | $$self{rowcount}=$rows; | ||
| 1937 | $$self{start}=1; | ||
| 1938 | $rows==0? '0E0' : $rows; | ||
| 1939 | } | ||
| 1940 | |||
| 1941 | sub DESTROY { | ||
| 1942 | my $self=shift; | ||
| 1943 | my $connref=$$self{'Database'}; | ||
| 1944 | if($$connref{'AGI'}) { | ||
| 1945 | return 1; | ||
| 1946 | } | ||
| 1947 | $self->finish; | ||
| 1948 | } | ||
| 1949 | |||
| 1950 | sub finish { | ||
| 1951 | my $self=shift; | ||
| 1952 | if(!$$self{Active}) { | ||
| 1953 | return 1; # finished already | ||
| 1954 | } | ||
| 1955 | $$self{Active}=0; | ||
| 1956 | my $connref=$$self{'Database'}; | ||
| 1957 | my $stmts=$$connref{stmts}; | ||
| 1958 | ${$self->{'Errstr'}}=''; | ||
| 1959 | if(!DBD::Ovrimos::lowlevel::sqlCloseCursor($self)) { | ||
| 1960 | _w($self,DBD::Ovrimos::lowlevel::sqlGetStmtDiagnostics($self)); | ||
| 1961 | return undef; | ||
| 1962 | } | ||
| 1963 | 1; | ||
| 1964 | } | ||
| 1965 | |||
| 1966 | sub _advance { | ||
| 1967 | my $self=shift; | ||
| 1968 | my $rows=$$self{rows}; | ||
| 1969 | ${$self->{'Errstr'}}=''; | ||
| 1970 | if(!defined($rows) || $$self{currrow}>=scalar(@$rows)-1) { | ||
| 1971 | my $where; | ||
| 1972 | if($$self{start}) { | ||
| 1973 | $$self{start}=0; | ||
| 1974 | $where=0; | ||
| 1975 | } else { | ||
| 1976 | $where=$$self{rowset_size}-1; | ||
| 1977 | } | ||
| 1978 | my $ret=DBD::Ovrimos::lowlevel::sqlCursorNext($self,$where); | ||
| 1979 | if(!$ret) { | ||
| 1980 | _w($self,DBD::Ovrimos::lowlevel::sqlGetStmtDiagnostics($self)); | ||
| 1981 | return undef; | ||
| 1982 | } | ||
| 1983 | $$self{currrow}=0; | ||
| 1984 | } else { | ||
| 1985 | $$self{currrow}++; | ||
| 1986 | } | ||
| 1987 | my $row=$$rows[$$self{currrow}]; | ||
| 1988 | if($$row{'state'}==DBD::Ovrimos::lowlevel::ROW_INEXISTANT) { | ||
| 1989 | return undef; | ||
| 1990 | } | ||
| 1991 | 1; | ||
| 1992 | } | ||
| 1993 | |||
| 1994 | sub fetchrow_arrayref { | ||
| 1995 | my $self=shift; | ||
| 1996 | ${$self->{'Errstr'}}=''; | ||
| 1997 | if(!_advance($self)) { | ||
| 1998 | return undef; | ||
| 1999 | } | ||
| 2000 | my $rows=$$self{rows}; | ||
| 2001 | my $row=$$rows[$$self{currrow}]; | ||
| 2002 | my $rowdata=$$row{data}; | ||
| 2003 | if(exists($$self{bindings})) { | ||
| 2004 | my $i; | ||
| 2005 | for($i=0; $i<$$self{colnb}; $i++) { | ||
| 2006 | my $value=$$rowdata[$i]; | ||
| 2007 | my $ref=$$self{bindings}->[$i]; | ||
| 2008 | if(ref($ref)) { | ||
| 2009 | $$ref=$value; | ||
| 2010 | } | ||
| 2011 | } | ||
| 2012 | } | ||
| 2013 | $rowdata; | ||
| 2014 | } | ||
| 2015 | |||
| 2016 | sub fetch { | ||
| 2017 | my $self=shift; | ||
| 2018 | fetchrow_arrayref($self); | ||
| 2019 | } | ||
| 2020 | |||
| 2021 | sub fetchrow_array { | ||
| 2022 | my $self=shift; | ||
| 2023 | @$self->fetchrow_arrayref(); | ||
| 2024 | } | ||
| 2025 | |||
| 2026 | sub fetchrow_hashref { | ||
| 2027 | my $self=shift; | ||
| 2028 | my $rowdata=fetchrow_arrayref($self); | ||
| 2029 | return undef if !$rowdata; | ||
| 2030 | my $resref=$$self{res}; | ||
| 2031 | my $b=exists($$self{bindings}); | ||
| 2032 | my %h=(); | ||
| 2033 | my $i; | ||
| 2034 | for($i=0; $i<$$self{colnb}; $i++) { | ||
| 2035 | my $coldefref=$$resref[$i]; | ||
| 2036 | my $name=$$coldefref{name}; | ||
| 2037 | my $value=$$rowdata[$i]; | ||
| 2038 | $h{$name}=$value; | ||
| 2039 | if($b) { | ||
| 2040 | my $ref=$$self{bindings}->[$i]; | ||
| 2041 | if(ref($ref)) { | ||
| 2042 | $$ref=$value; | ||
| 2043 | } | ||
| 2044 | } | ||
| 2045 | } | ||
| 2046 | \%h; | ||
| 2047 | } | ||
| 2048 | |||
| 2049 | sub rows { | ||
| 2050 | my $self=shift; | ||
| 2051 | $$self{rowcount}; | ||
| 2052 | } | ||
| 2053 | |||
| 2054 | sub bind_columns { | ||
| 2055 | my $self=shift; | ||
| 2056 | my $attrs=shift; | ||
| 2057 | my @refs=@_; | ||
| 2058 | ${$self->{'Errstr'}}=''; | ||
| 2059 | if($$self{colnb}!=scalar(@refs)) { | ||
| 2060 | _w($self,'S1002 Invalid number of columns to bind'); | ||
| 2061 | return undef; | ||
| 2062 | } | ||
| 2063 | $$self{bindings}=\@refs; | ||
| 2064 | 1; | ||
| 2065 | } | ||
| 2066 | |||
| 2067 | sub bind_col { | ||
| 2068 | my $self=shift; | ||
| 2069 | my $num=shift; | ||
| 2070 | my $ref=shift; | ||
| 2071 | my $attrs=shift; | ||
| 2072 | ${$self->{'Errstr'}}=''; | ||
| 2073 | if($num<1 || $num>$$self{colnb}) { | ||
| 2074 | _w($self,'S1002 Invalid column number to bind'); | ||
| 2075 | return undef; | ||
| 2076 | } | ||
| 2077 | if(!exists($$self{bindings})) { | ||
| 2078 | $$self{bindings}=[]; | ||
| 2079 | } | ||
| 2080 | $$self{bindings}->[$num-1]=$ref; | ||
| 2081 | 1; | ||
| 2082 | } | ||
| 2083 | |||
| 2084 | sub FETCH { | ||
| 2085 | my $self=shift; | ||
| 2086 | my $key=shift; | ||
| 2087 | if($key eq 'NUM_OF_FIELDS') { | ||
| 2088 | return $$self{colnb}; | ||
| 2089 | } elsif($key eq 'NUM_OF_PARAMS') { | ||
| 2090 | return $$self{paramcount}; | ||
| 2091 | } elsif($key eq 'Active') { | ||
| 2092 | return $$self{'Active'}; | ||
| 2093 | } elsif($key eq 'NAME') { | ||
| 2094 | my @a=map { $_->{name} } @{$self->{res}}; | ||
| 2095 | return \@a; | ||
| 2096 | } elsif($key eq 'NULLABLE') { | ||
| 2097 | my @a=map { $_->{nullable} } @{$self->{res}}; | ||
| 2098 | return \@a; | ||
| 2099 | } elsif($key eq 'ovrimos_column_type' || $key eq 'TYPE') { | ||
| 2100 | my @a=map { $_->{type} } @{$self->{res}}; | ||
| 2101 | return \@a; | ||
| 2102 | } elsif($key eq 'ovrimos_column_precision') { | ||
| 2103 | my @a=map { $_->{len} } @{$self->{res}}; | ||
| 2104 | return \@a; | ||
| 2105 | } elsif($key eq 'ovrimos_column_scale') { | ||
| 2106 | my @a=map { $_->{scale} } @{$self->{res}}; | ||
| 2107 | return \@a; | ||
| 2108 | } elsif($key eq 'CursorName') { | ||
| 2109 | return DBD::Ovrimos::lowlevel::sqlGetCursorName($self); | ||
| 2110 | } elsif($key eq 'ovrimos_execution_plan') { | ||
| 2111 | return DBD::Ovrimos::lowlevel::sqlGetExecutionPlan($self); | ||
| 2112 | } elsif($key eq 'ovrimos_native_query') { | ||
| 2113 | return DBD::Ovrimos::lowlevel::sqlGetNativeQuery($self); | ||
| 2114 | } else { | ||
| 2115 | return undef; | ||
| 2116 | } | ||
| 2117 | } | ||
| 2118 | |||
| 2119 | sub bind_param { | ||
| 2120 | my $self=shift; | ||
| 2121 | my $num=shift; | ||
| 2122 | my $value=shift; | ||
| 2123 | my $attr=shift; | ||
| 2124 | my $connref=$$self{'Database'}; | ||
| 2125 | |||
| 2126 | ${$self->{'Errstr'}}=''; | ||
| 2127 | if($num<1 || $num>$$self{paramcount}) { | ||
| 2128 | _w($self,'S1093 Invalid parameter number to bind'); | ||
| 2129 | return undef; | ||
| 2130 | } | ||
| 2131 | |||
| 2132 | my $coldefref=$$self{params}->[$num-1]; | ||
| 2133 | my $templ=substr(DBD::Ovrimos::lowlevel::_column_pack_template($coldefref),1); #skip null ind/tor | ||
| 2134 | my $val=DBD::Ovrimos::lowlevel::_pack($$connref{endianity},$templ,[$value]); | ||
| 2135 | my $ret=DBD::Ovrimos::lowlevel::sqlPutParam($self,$num-1,$val); | ||
| 2136 | if(!$ret) { | ||
| 2137 | _w($self,DBD::Ovrimos::lowlevel::sqlGetStmtDiagnostics($self)); | ||
| 2138 | } | ||
| 2139 | $ret; | ||
| 2140 | } | ||
| 2141 | |||
| 2142 | 1; |