1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
|
/* KInterbasDB Python Package - Implementation of Cursor
*
* Version 3.3
*
* The following contributors hold Copyright (C) over their respective
* portions of code (see license.txt for details):
*
* [Original Author (maintained through version 2.0-0.3.1):]
* 1998-2001 [alex] Alexander Kuznetsov <alexan@users.sourceforge.net>
* [Maintainers (after version 2.0-0.3.1):]
* 2001-2002 [maz] Marek Isalski <kinterbasdb@maz.nu>
* 2002-2007 [dsr] David Rushby <woodsplitter@rocketmail.com>
* [Contributors:]
* 2001 [eac] Evgeny A. Cherkashin <eugeneai@icc.ru>
* 2001-2002 [janez] Janez Jere <janez.jere@void.si>
*/
/****************** DECLARATIONS:BEGIN *******************/
static int PreparedStatement_close_without_unlink(
PreparedStatement *, boolean
);
static int PSTracker_remove(PSTracker **, PreparedStatement *cont, boolean);
static int PreparedStatement_isc_close(PreparedStatement *, boolean);
static PyObject *_generic_single_item_isc_dsql_sql_info_request(
isc_stmt_handle *, ISC_STATUS *, const char, const short
);
static int _determine_statement_type(isc_stmt_handle *, ISC_STATUS *);
typedef int (*PSCacheMappedFunction)(
PSCache *, unsigned short, PreparedStatement *
);
#define PSCache_has_been_deleted(self) \
((self)->container == NULL)
/* Note that PS_STATE_CLOSED has a very different meaning from the "closed"
* states of most other objects: */
#define PreparedStatement_is_open(self) \
((self)->state == PS_STATE_OPEN || (self)->state == PS_STATE_CLOSED)
/* Python strings are immutable, and Python's garbage collector does not
* relocate memory, so if two PyObject pointers point to the same memory
* location, the two objects are certainly equal--in fact, they're IDentical
* (id(self->sql) == id(sql)).
*
* If the pointers refer to different memory locations, the two objects are
* still equal if their contents match. */
#define PreparedStatement_matches_sql(self_, sql_) \
( (self_)->sql == (sql_) \
|| PyObject_Compare((self_)->sql, (sql_)) == 0 \
)
/****************** DECLARATIONS:END *******************/
/*************** PSCache (PreparedStatementList) METHODS:BEGIN ***************/
#define PSCACHE_NULLIFY(self) \
(self)->container = NULL; \
(self)->capacity = 0; \
(self)->start = 0; \
(self)->most_recently_found = NULL;
static int PSCache_initialize(PSCache *self, unsigned short capacity) {
unsigned short i;
self->container = kimem_main_malloc(sizeof(PreparedStatement *) * capacity);
if (self->container == NULL) { return -1; }
self->capacity = capacity;
for (i = 0; i < capacity; i++) {
self->container[i] = NULL;
}
self->most_recently_found = NULL;
return 0;
} /* PSCache_initialize */
#if (PREP_STMT_CACHE_CAPACITY == 1)
#define _PSCache_index_next(i) 0
#define _PSCache_index_prev(i) 0
#else
#define _PSCache_index_next(i) \
((i + 1) % self->capacity)
#define _PSCache_index_prev(i) \
(i != 0 ? i - 1 : self->capacity - 1)
#endif
#define PSCache_first(self) \
_PSCache_index_prev(self->start)
#define PSCache_is_empty(self) \
(PSCache_first(self) == NULL)
static int PSCache_append(PSCache *self, PreparedStatement *ps) {
const unsigned short i = self->start;
assert (ps != NULL);
assert (PreparedStatement_is_open(ps));
/* Any PreparedStatement added to a Cursor's PSCache should be for internal
* user, so we can enforce specific expectations about its reference
* count: */
assert (ps->for_internal_use);
assert (ps->ob_refcnt == 1);
assert (ps->sql != NULL);
assert (OBJECT_IS_VALID_TYPE_FOR_PREPARED_STATEMENT_SQL(ps->sql));
assert (ps->cur != NULL);
if (Cursor_ensure_PSCache(ps->cur) != 0) { return -1; }
{
PreparedStatement *prev_occupant = self->container[i];
if (prev_occupant != NULL) {
/* We should never be appending to the cache a PreparedStatement that's
* already present. The following assertion isn't a robust check, of
* course. */
assert (prev_occupant != ps);
/* We mustn't delete a PreparedStatement, yet have it still referenced by
* the self->most_recently_found shortcut pointer. */
if (prev_occupant == self->most_recently_found) {
self->most_recently_found = NULL;
}
/* Since all PreparedStatements stored in a PSCache are for internal use,
* and internal use is tightly controllable, we know that removing
* prev_occupant from the cache will cause it to be deleted. */
assert (prev_occupant->ob_refcnt == 1);
Py_DECREF(prev_occupant);
}
}
Py_INCREF(ps);
self->container[i] = ps;
self->start = _PSCache_index_next(i);
return 0;
} /* PSCache_append */
static PreparedStatement *PSCache_find_prep_stmt_for_sql(PSCache *self,
PyObject *sql
)
{
/* Returns a borrowed reference to a PreparedStatement that matches sql, or
* NULL if there is no such PreparedStatement in the cache. */
assert (!PSCache_has_been_deleted(self));
assert (sql != NULL);
assert (OBJECT_IS_VALID_TYPE_FOR_PREPARED_STATEMENT_SQL(sql));
if (self->most_recently_found != NULL) {
assert (self->most_recently_found->sql != NULL);
if (PreparedStatement_matches_sql(self->most_recently_found, sql)) {
return self->most_recently_found;
}
}{
PreparedStatement *matching_ps = NULL;
/* self->start is the index where the next appended statement should be
* placed, but we want to start with the most recently appended statement and
* work backward from there. */
unsigned short i = _PSCache_index_prev(self->start);
const unsigned short i_orig = i;
do {
PreparedStatement *ps = self->container[i];
if (ps == NULL) { break; } /* None left to search. */
assert (OBJECT_IS_VALID_TYPE_FOR_PREPARED_STATEMENT_SQL(ps->sql));
if (PreparedStatement_matches_sql(ps, sql)) {
matching_ps = self->most_recently_found = ps;
break;
}
i = _PSCache_index_prev(i);
} while (i != i_orig);
return matching_ps;
}} /* PSCache_find_prep_stmt_for_sql */
static int PSCache_traverse(PSCache *self, PSCacheMappedFunction modifier) {
unsigned short i = _PSCache_index_prev(self->start);
const unsigned short i_orig = i;
assert (!PSCache_has_been_deleted(self));
do {
PreparedStatement *ps = self->container[i];
if (ps == NULL) { break; }{ /* None left to search. */
const int modifier_result = modifier(self, i, ps);
if (modifier_result != 0) { return modifier_result; }
i = _PSCache_index_prev(i);
}} while (i != i_orig);
return 0;
} /* PSCache_traverse */
static void PSCache_clear(PSCache *self) {
/* Empties the PSCache, but does not release any of the dynamically allocated
* memory of its members (we're talking about PSCache *structure members*,
* not *container elements*). */
unsigned short i;
assert (!PSCache_has_been_deleted(self));
i = self->start;
for (;;) {
i = _PSCache_index_prev(i);
{
PreparedStatement *ps = self->container[i];
if (ps == NULL) {
break;
}
/* Only internal PreparedStatements should be in the PSCache: */
assert (ps->for_internal_use);
/* At this point, each internal PreparedStatement should be referenced
* once by the tracker: */
assert (ps->ob_refcnt != 0);
/* But nowhere else: */
assert (ps->ob_refcnt == 1);
Py_DECREF(ps);
self->container[i] = NULL;
}
}
self->start = 0;
self->most_recently_found = NULL;
} /* PSCache_clear */
static void PSCache_delete(PSCache *self) {
/* Opposite of PSCache_initialize. */
assert (!PSCache_has_been_deleted(self));
assert (self->container != NULL);
PSCache_clear(self);
/* Must ensure that a dangling reference to a deleted member of ->container
* wasn't left behind. */
assert (self->most_recently_found == NULL);
kimem_main_free(self->container);
self->container = NULL;
self->capacity = 0;
} /* PSCache_delete */
/**************** PSCache (PreparedStatementList) METHODS:END ****************/
/********** PreparedStatement METHODS INACCESSIBLE TO PYTHON:BEGIN ***********/
#define PS_REQ_OPEN_WITH_FAILURE(ps, failure_action) \
if (_PreparedStatement_require_open(ps, NULL) != 0) { failure_action; }
#define PS_REQ_OPEN(ps) \
PS_REQ_OPEN_WITH_FAILURE(ps, return NULL)
#define PS_REQ_OPEN2(ps, failure_message) \
if (_PreparedStatement_require_open(ps, failure_message) != 0) { \
return NULL; \
}
static int _PreparedStatement_require_open(
PreparedStatement *self, char *failure_message
)
{
/* If self is not open, raises the supplied error message (or a default if
* no error message is supplied).
* Returns 0 if self was open; -1 if it was closed. */
if (PreparedStatement_is_open(self)) { return 0; }
if (self->state == PS_STATE_CONNECTION_TIMED_OUT) {
raise_exception(ConnectionTimedOut,
"This PreparedStatement's connection timed out, and"
" PreparedStatements cannot transparently survive a timeout."
);
} else {
raise_exception(ProgrammingError,
failure_message != NULL ? failure_message :
"The PreparedStatement must be OPEN to perform this"
" operation."
);
}
return -1;
} /* _PreparedStatement_require_open */
static void PreparedStatement_create_references_to_superiors(
PreparedStatement *self, const boolean for_internal_use,
Cursor *cur
)
{
assert (self != NULL);
assert (self->cur == NULL);
assert (cur != NULL);
/* The internal-use indicator that we've been provided should match the one
* set in the corresponding member of self: */
assert (for_internal_use == self->for_internal_use);
/* An non-internal (user-accessible) PreparedStatement owns a reference to
* its Cursor, while its Cursor does not own a reference to it. Reference
* ownership for an internal PreparedStatement is just the opposite. */
if (!for_internal_use) {
Py_INCREF(cur);
}
self->cur = cur;
} /* PreparedStatement_create_references_to_superiors */
static void PreparedStatement_clear_references_to_superiors(
PreparedStatement *self
)
{
assert (self != NULL);
{
Cursor *cur = self->cur;
assert (cur != NULL);
self->cur = NULL;
if (!self->for_internal_use) {
Py_DECREF(cur);
}
}
} /* PreparedStatement_clear_references_to_superiors */
static PreparedStatement *PreparedStatement_create(
Cursor *cur, boolean for_internal_use
)
{
PreparedStatement *self;
self = PyObject_New(PreparedStatement, &PreparedStatementType);
if (self == NULL) { goto fail; }
self->state = PS_STATE_CREATED;
self->for_internal_use = for_internal_use;
self->stmt_handle = NULL_STMT_HANDLE;
self->sql = NULL;
assert (cur != NULL);
self->cur = NULL;
PreparedStatement_create_references_to_superiors(
self, for_internal_use, cur
);
assert (self->cur == cur);
self->statement_type = NULL_STATEMENT_TYPE;
self->in_sqlda = NULL;
self->in_sqlda_sqlind_array = NULL;
self->out_sqlda = NULL;
self->in_var_orig_spec = NULL;
self->out_buffer = NULL;
self->description = NULL;
/* Notice that this constructor does not add self to any caches or trackers.
* It is the caller's responsibility to do so, if appropriate. */
return self;
fail:
Py_XDECREF(self);
return NULL;
} /* PreparedStatement_create */
static int PreparedStatement_open(PreparedStatement* self,
Cursor *cur, PyObject *sql
)
{
ISC_STATUS *sv;
Transaction *trans;
CConnection *con;
assert (cur != NULL);
sv = cur->status_vector;
trans = cur->trans;
assert (trans != NULL);
con = Transaction_get_con(trans);
assert (con != NULL);
CON_MUST_ALREADY_BE_ACTIVE(con);
/* The caller should've already validated sql; sql should now be a non-NULL
* str. */
assert (sql != NULL);
assert (OBJECT_IS_VALID_TYPE_FOR_PREPARED_STATEMENT_SQL(sql));
Py_INCREF(sql);
self->sql = sql;
/* Allocate new statement handle: */
assert(self->stmt_handle == NULL_STMT_HANDLE);
ENTER_GDAL
isc_dsql_allocate_statement(sv, &con->db_handle, &self->stmt_handle);
LEAVE_GDAL
if (DB_API_ERROR(sv)) {
raise_sql_exception(OperationalError, "isc_dsql_allocate_statement: ", sv);
goto fail;
}
assert(self->stmt_handle != NULL_STMT_HANDLE);
/* Allocate enough space for a default number of XSQLVARs in the OUTput
* XSQLDA. The XSQLDA must be allocated prior to calling isc_dsql_prepare
* below, and if there are too many output variables for the default size,
* it may need to be reallocated and re-initialized with isc_dsql_describe
* later. */
assert (self->out_sqlda == NULL);
if (reallocate_sqlda(&self->out_sqlda, FALSE, NULL) < 0) { goto fail; }
assert (self->out_sqlda != NULL);
/* Ask the database engine to compile the statement. */
{
/* Note that we call Transaction_get_handle_p while holding the GIL. */
isc_tr_handle *trans_handle_addr = Transaction_get_handle_p(trans);
char *sql_raw_buffer = PyString_AS_STRING(sql);
const Py_ssize_t sql_len = PyString_GET_SIZE(sql);
if (!_check_statement_length(sql_len)) { goto fail; }
ENTER_GDAL
isc_dsql_prepare(sv, trans_handle_addr, &self->stmt_handle,
(unsigned short) sql_len, sql_raw_buffer, con->dialect,
self->out_sqlda
);
LEAVE_GDAL
if (DB_API_ERROR(sv)) {
raise_sql_exception(ProgrammingError, "isc_dsql_prepare: ", sv);
goto fail;
}
}
/* Determine the database API's internal statement type code for the current
* statement and cache that code. */
assert (self->statement_type == NULL_STATEMENT_TYPE);
self->statement_type = _determine_statement_type(&self->stmt_handle, sv);
if (self->statement_type == -1) { goto fail; }
assert (self->statement_type != NULL_STATEMENT_TYPE);
/* Now that we know how many output variables there are, it might be
* necessary to resize the output XSQLDA to accomodate them. */
{
const int sqlda_realloc_res = reallocate_sqlda(&self->out_sqlda, FALSE, NULL);
if (sqlda_realloc_res == 0) {
/* No actual reallocation was necessary, so there's no need to rebind. */
} else if (sqlda_realloc_res == 1) {
/* The default number of XSQLVARs allocated earlier was insufficient, so
* the XSQLDA's parameter information must be rebound. */
ENTER_GDAL
isc_dsql_describe(sv, &self->stmt_handle, con->dialect,
self->out_sqlda /* OUTPUT */
);
LEAVE_GDAL
if (DB_API_ERROR(sv)) {
raise_sql_exception(OperationalError,
"isc_dsql_describe for OUTput params: ", sv
);
goto fail;
}
} else {
/* reallocate_sqlda raised an error. */
goto fail;
}
}
/* If there are any output variables, allocate a buffer to hold the raw
* values from the database, before they're converted to Python values. This
* buffer will be allocated to exactly the required size, and will then
* remain alive throughout the life of this PreparedStatement, accepting
* every row of raw output every fetched with the statement. */
assert (self->out_buffer == NULL);
if (self->out_sqlda->sqld > 0) {
self->out_buffer = allocate_output_buffer(self->out_sqlda);
if (self->out_buffer == NULL) { goto fail; }
}
/* Bind information about the INput XSQLDA's variables. */
/* Allocate enough space for a default number of XSQLVARs in the INput
* XSQLDA. The XSQLDA must be allocated prior to calling
* isc_dsql_describe_bind below, and it may need to be reallocated prior if
* the default number of XSQLVARs was insufficient. */
assert (self->in_sqlda == NULL);
if (reallocate_sqlda(&self->in_sqlda, TRUE, &self->in_sqlda_sqlind_array)
< 0
)
{ goto fail; }
assert (self->in_sqlda != NULL);
ENTER_GDAL
isc_dsql_describe_bind(sv, &self->stmt_handle, con->dialect,
self->in_sqlda /* INPUT */
);
LEAVE_GDAL
if (DB_API_ERROR(sv)) {
raise_sql_exception(OperationalError,
"isc_dsql_describe_bind for INput params: ", sv
);
goto fail;
}
{
const int sqlda_realloc_res = reallocate_sqlda(&self->in_sqlda, TRUE,
&self->in_sqlda_sqlind_array
);
if (sqlda_realloc_res == 0) {
/* No actual reallocation was necessary, so there's no need to rebind. */
} else if (sqlda_realloc_res == 1) {
/* The default number of XSQLVARs allocated earlier was insufficient, so
* the XSQLDA's parameter information must be rebound. */
ENTER_GDAL
isc_dsql_describe_bind(sv, &self->stmt_handle, con->dialect,
self->in_sqlda /* INPUT */
);
LEAVE_GDAL
if (DB_API_ERROR(sv)) {
raise_sql_exception(OperationalError,
"isc_dsql_describe_bind for INput params: ", sv
);
goto fail;
}
} else {
/* reallocate_sqlda raised an error. */
goto fail;
}
}
/* Record the original type and size information for input parameters so that
* information can be restored if implicit input parameter conversion from
* string is invoked. */
{
XSQLVAR *sqlvar;
OriginalXSQLVARSpecificationCache *spec_cache;
short var_no;
const short input_param_count = self->in_sqlda->sqld;
self->in_var_orig_spec = kimem_plain_malloc(
sizeof(OriginalXSQLVARSpecificationCache) * input_param_count
);
if (self->in_var_orig_spec == NULL) {
/* Weren't calling one of the Python-supplied memory allocators; need to
* set Python exception (MemoryError). */
PyErr_NoMemory();
goto fail;
}
for ( sqlvar = self->in_sqlda->sqlvar, var_no = 0,
spec_cache = self->in_var_orig_spec;
var_no < input_param_count;
sqlvar++, var_no++, spec_cache++
)
{
spec_cache->sqltype = sqlvar->sqltype;
spec_cache->sqllen = sqlvar->sqllen;
}
}
/* The description tuple should still be NULL; it will be computed lazily
* only if the client programmer asks for it. */
assert (self->description == NULL);
assert (!PyErr_Occurred());
self->state = PS_STATE_OPEN;
return 0;
fail:
assert (PyErr_Occurred());
/* The state flag should indicate that the PreparedStatement has been
* created but not successfully opened. */
assert (self->state == PS_STATE_CREATED);
return -1;
} /* PreparedStatement_open */
static int PreparedStatement_compare(
PreparedStatement *a, PreparedStatement *b
)
{
PyObject *a_sql = a->sql;
PyObject *b_sql = b->sql;
Cursor *a_cur = a->cur;
Cursor *b_cur = b->cur;
if ( (a_sql == NULL || b_sql == NULL)
|| (a_cur == NULL || b_cur == NULL)
|| (a_cur->trans != b_cur->trans)
)
{
/* Not equal. */
return -1;
} else {
/* Equality comes down to the equality of their SQL strings. */
return PyObject_Compare(a_sql, b_sql);
}
} /* PreparedStatement_compare */
static PyObject *PreparedStatement_description_tuple_get(
PreparedStatement *self
)
{
/* Lazily creates a Python DB API Cursor.description tuple for this
* PreparedStatement.
* Returns borrowed reference to the description tuple, or NULL on error. */
assert (self->out_sqlda != NULL);
assert (self->cur != NULL);
if (self->description == NULL) {
/* The object created by the following call will be DECREFed by
* PreparedStatement_clear_description_tuple, which may be called directly
* by the PreparedStatement destructor or indirectly by
* (Connection|Cursor).set_type_trans_out. */
self->description = XSQLDA2Description(self->out_sqlda, self->cur);
}
return self->description;
} /* PreparedStatement_description_tuple_get */
static PyObject *pyob_PreparedStatement_description_get(
PreparedStatement *self, void *closure
)
{
/* DB API description tuple read-only property. */
PS_REQ_OPEN(self);
{
PyObject *py_result = PreparedStatement_description_tuple_get(self);
/* PreparedStatement_description_tuple_get returns a *borrowed* reference
* to the description tuple; we must increment it (with Py_XINCREF, because
* it might be NULL) before returning it. */
Py_XINCREF(py_result);
return py_result;
}
} /* pyob_Cursor_description_get */
/* CLEAR-DESCRIPTION-TUPLE SUPPORT CODE : BEGIN */
/* The description tuple contains information about a statement's output
* variables that might change if (Connection|Cursor).set_type_trans_out is
* called. Therefore, set_type_trans_out was changed to trigger the clearing
* of the description tuple of every PreparedStatement subordinate to the
* object on which the method was invoked (a Connection or Cursor). This group
* of functions supports that operation.
*
* Note that the description tuples are cleared, not reconstructed. A given
* description tuple will only be reconstructed if the user actually requests
* it. */
static int PreparedStatement_clear_description_tuple(PreparedStatement *self) {
/* Clears an individual PreparedStatement's description tuple. */
if (self->description != NULL) {
Py_DECREF(self->description);
self->description = NULL;
}
return 0;
} /* PreparedStatement_clear_description_tuple */
static int PSCacheMapped_clear_description_tuple(
PSCache* cache, unsigned short cache_index, PreparedStatement *ps
)
{
/* Called on each element of a Cursor's PSCache's container. */
assert (ps != NULL);
return PreparedStatement_clear_description_tuple(ps);
} /* PSCacheMapped_clear_description_tuple */
static int PSTrackerMapped_clear_description_tuple(
PSTracker *node_prev, PSTracker *node_cur
)
{
/* Called on each element of a Cursor's PSTracker. */
PreparedStatement *ps;
assert (node_cur != NULL);
ps = node_cur->contained;
assert (ps != NULL);
return PreparedStatement_clear_description_tuple(ps);
} /* PSTrackerMapped_clear_description_tuple */
static int Cursor_clear_ps_description_tuples(Cursor *self) {
/* Clears the description tuple of every PreparedStatement subordinate to
* this Cursor. */
PSCache *psc = &self->ps_cache_internal;
if (!PSCache_has_been_deleted(psc)) {
if (PSCache_traverse(psc, PSCacheMapped_clear_description_tuple) != 0)
{ goto fail; }
}
if (self->ps_tracker != NULL) {
if (PSTracker_traverse(self->ps_tracker,
PSTrackerMapped_clear_description_tuple
) != 0
)
{ goto fail; }
}
return 0;
fail:
assert (PyErr_Occurred());
return -1;
} /* Cursor_clear_ps_description_tuples */
static int CConnection_clear_ps_description_tuples(
CConnection *self
)
{
/* Clears the description tuple of every PreparedStatement subordinate to
* this Connection. */
TransactionTracker *trans_node = self->transactions;
while (trans_node != NULL) {
Transaction *trans = trans_node->contained;
assert (trans != NULL);
{
CursorTracker *cur_node = trans->open_cursors;
while (cur_node != NULL) {
Cursor *cur = cur_node->contained;
assert (cur != NULL);
if (Cursor_clear_ps_description_tuples(cur) != 0) { return -1; }
cur_node = cur_node->next;
}
}
trans_node = trans_node->next;
}
return 0;
} /* CConnection_clear_ps_description_tuples */
/* CLEAR-DESCRIPTION-TUPLE SUPPORT CODE : END */
static int PreparedStatement_isc_close(PreparedStatement *self,
boolean allowed_to_raise
)
{
/* IB6 API Guide page 334: "A cursor need only be closed in this manner [with
* DSQL_close] if it was previously opened and associated with stmt_handle by
* isc_dsql_set_cursor_name()."
*
* That's not accurate. A statement handle also needs to be closed in this
* manner if it's about to be executed again. If it's going to be closed
* *permanently*, calling isc_dsql_free_statement with DSQL_drop (as is done
* in PreparedStatement_isc_drop) is sufficient. */
ISC_STATUS *sv;
assert (self->cur != NULL);
assert (self->cur->trans != NULL);
assert (Transaction_get_con(self->cur->trans) != NULL);
CON_MUST_ALREADY_BE_ACTIVE(Transaction_get_con(self->cur->trans));
sv = self->cur->status_vector;
assert (PreparedStatement_is_open(self));
ENTER_GDAL
isc_dsql_free_statement(sv, &self->stmt_handle,
/* DSQL_close means "close the open result set associated with this
* statement; we're clearing it for another execution." */
DSQL_close
);
LEAVE_GDAL
if (DB_API_ERROR(sv)) {
raise_sql_exception(OperationalError,
"Error while trying to close PreparedStatement's associated result"
" set: ", sv
);
if (allowed_to_raise) {
return -1;
} else {
SUPPRESS_EXCEPTION;
}
}
self->state = PS_STATE_CLOSED;
/* We "closed" this prepared statement in a sense that matters only to the
* server, not to the Python client programmer: */
assert (PreparedStatement_is_open(self));
assert (self->stmt_handle != NULL_STMT_HANDLE);
return 0;
} /* PreparedStatement_isc_close */
static int PreparedStatement_isc_drop(PreparedStatement *self,
boolean allowed_to_raise
)
{
ISC_STATUS *sv;
assert (self->cur != NULL);
assert (self->cur->trans != NULL);
#if (defined(ENABLE_CONNECTION_TIMEOUT) && !defined(NDEBUG))
{
CConnection *con = Transaction_get_con(self->cur->trans);
assert (con != NULL);
if (Connection_timeout_enabled(con)) {
assert (CURRENT_THREAD_OWNS_CON_TP(con));
if (RUNNING_IN_CONNECTION_TIMEOUT_THREAD) {
assert (con->timeout->state == CONOP_IDLE);
}
}
}
#endif
sv = self->cur->status_vector;
/* Notice that unlike in PreparedStatement_isc_close, there's no
* assert (PreparedStatement_is_open(self));
* statement here. That's because it's possible for a statement handle to
* be allocated, but for preparation to fail later in the process, as when
* there's a syntax error. In that case, self's state won't have been moved
* to PS_STATE_OPEN. */
{
/* This code can be reached when the CTT is timing out a connection. In
* that case, we want the GIL to remain held during the entire timeout
* operation. */
OPEN_LOCAL_GIL_MANIPULATION_INFRASTRUCTURE
#ifdef ENABLE_CONNECTION_TIMEOUT
const boolean should_manip_gil = NOT_RUNNING_IN_CONNECTION_TIMEOUT_THREAD;
if (should_manip_gil) {
#endif
LEAVE_GIL_WITHOUT_AFFECTING_DB_AND_WITHOUT_STARTING_CODE_BLOCK
#ifdef ENABLE_CONNECTION_TIMEOUT
}
#endif
ENTER_GDAL_WITHOUT_LEAVING_PYTHON
isc_dsql_free_statement(sv, &self->stmt_handle,
/* DSQL_drop means "free resources allocated for this statement; we're
* closing it permanently." */
DSQL_drop
);
LEAVE_GDAL_WITHOUT_ENTERING_PYTHON
#ifdef ENABLE_CONNECTION_TIMEOUT
if (should_manip_gil) {
#endif
ENTER_GIL_WITHOUT_AFFECTING_DB_AND_WITHOUT_ENDING_CODE_BLOCK
#ifdef ENABLE_CONNECTION_TIMEOUT
}
#endif
CLOSE_LOCAL_GIL_MANIPULATION_INFRASTRUCTURE
} /* end of lock manipulation scope */
if (DB_API_ERROR(sv)) {
raise_sql_exception(OperationalError,
"Error while trying to drop PreparedStatement's statement handle: ",
sv
);
if (allowed_to_raise) {
return -1;
} else {
SUPPRESS_EXCEPTION;
}
}
self->stmt_handle = NULL_STMT_HANDLE;
self->state = PS_STATE_DROPPED;
assert (!PreparedStatement_is_open(self));
return 0;
} /* PreparedStatement_isc_drop */
static int PreparedStatement_close_without_unlink(PreparedStatement *self,
boolean allowed_to_raise
)
{
if (self->sql != NULL) {
Py_DECREF(self->sql);
self->sql = NULL;
}
if (self->in_sqlda != NULL) {
kimem_xsqlda_free(self->in_sqlda);
self->in_sqlda = NULL;
}
if (self->in_sqlda_sqlind_array != NULL) {
kimem_main_free(self->in_sqlda_sqlind_array);
self->in_sqlda_sqlind_array = NULL;
}
if (self->out_sqlda != NULL) {
kimem_xsqlda_free(self->out_sqlda);
self->out_sqlda = NULL;
}
if (self->in_var_orig_spec != NULL) {
kimem_plain_free(self->in_var_orig_spec);
self->in_var_orig_spec = NULL;
}
if (self->out_buffer != NULL) {
kimem_main_free(self->out_buffer);
self->out_buffer = NULL;
}
PreparedStatement_clear_description_tuple(self);
/* Save the operations that might fail for last: */
if (self->cur != NULL) {
/* If self is the cursor's current PreparedStatement, need to ensure that
* the cursor realizes self has closed so it won't try to perform
* operations using self in the future. */
if (self->cur->ps_current == self) {
if (self->cur->state != CURSOR_STATE_CLOSED) {
Cursor_clear_and_leave_open(self->cur);
}
/* The lack of Py_DECREF here is deliberate, because the ps_current
* member of Cursor never (conceptually) contains an owned reference. */
self->cur->ps_current = NULL;
}
/* Note that we don't DECREF or clear self->cur here; that's for
* PreparedStatement_close_with_unlink. */
}
if (self->stmt_handle != NULL_STMT_HANDLE) {
assert (self->cur != NULL);
if (PreparedStatement_isc_drop(self, allowed_to_raise) != 0) {
goto fail;
}
}
assert (self->stmt_handle == NULL_STMT_HANDLE);
/* We don't clear self->cur here or remove self from cur's tracker. That's
* up to PreparedStatement_close_with_unlink. */
self->state = PS_STATE_DROPPED;
return 0;
fail:
assert (PyErr_Occurred());
return -1;
} /* PreparedStatement_close_without_unlink */
static int PreparedStatement_untrack_with_superior_ref_clear_control(
PreparedStatement *self, const boolean allowed_to_raise,
const boolean clear_superior_refs
)
{
if (PreparedStatement_close_without_unlink(self, allowed_to_raise) != 0) {
return -1;
}
assert (self->state == PS_STATE_DROPPED);
assert (self->cur != NULL);
if (clear_superior_refs) {
PreparedStatement_clear_references_to_superiors(self);
assert (self->cur == NULL);
}
return 0;
} /* PreparedStatement_untrack_with_superior_ref_clear_control */
static int PreparedStatement_untrack(PreparedStatement *self,
boolean allowed_to_raise
)
{
return PreparedStatement_untrack_with_superior_ref_clear_control(
self, allowed_to_raise, TRUE
);
} /* PreparedStatement_untrack */
static int PreparedStatement_close_with_unlink(PreparedStatement *self,
boolean allowed_to_raise
)
{
if (self->state != PS_STATE_DROPPED) {
if (PreparedStatement_close_without_unlink(self, allowed_to_raise) != 0) {
goto fail;
}
}
if (self->cur != NULL) {
/* If self was for internal use, there's no need to manually remove self
* from cur->cur->ps_cache_internal, because the cur will only kill one of
* its internal use PreparedStatements as part of the act of removing it
* from cur->ps_cache_internal. */
if (!self->for_internal_use) {
/* Remove self from the cursor's open prepared statement tracker.
* Normally self will be in the ps_tracker, but PreparedStatements are
* not inserted until the end of the preparation process, so if an error
* occurred, self won't be there. This can occur routinely (e.g., if
* user submits erroneous SQL to Cursor.prep), so with the final boolean
* parameter, we direct PSTracker_remove not to complain if the self is
* missing. */
if (PSTracker_remove(&self->cur->ps_tracker, self, FALSE) != 0) {
if (allowed_to_raise) {
goto fail;
} else {
SUPPRESS_EXCEPTION;
}
}
}
PreparedStatement_clear_references_to_superiors(self);
assert (self->cur == NULL);
}
assert (allowed_to_raise ? self->state == PS_STATE_DROPPED : TRUE);
return 0;
fail:
assert (PyErr_Occurred());
return -1;
} /* PreparedStatement_close_with_unlink */
/*********** PreparedStatement METHODS INACCESSIBLE TO PYTHON:END ************/
/*********** PreparedStatement METHODS ACCESSIBLE TO PYTHON:BEGIN ************/
static void pyob_PreparedStatement___del__(PreparedStatement *self) {
assert (
!self->for_internal_use
? NOT_RUNNING_IN_CONNECTION_TIMEOUT_THREAD
: TRUE
);
if (self->cur != NULL) {
Cursor *cur = self->cur;
/* We want to make sure that cur remains alive until we're done destroying
* self, but if this destructor is being called as a result of the
* execution of cur's destructor, we most definitely must not manipulate
* cur's reference count, which would cause cur to be "resurrected" and
* then for its destructor to execute again!
* Also, if self is for internal use, it never physically owns a reference
* to the cursor, so the reference count should not be manipulated. */
const boolean should_manipulate_cursor_refcnt = (
!self->for_internal_use && cur->ob_refcnt != 0
);
PyObject *con_python_wrapper = NULL;
CConnection *con;
assert (cur->trans != NULL);
con = Transaction_get_con(cur->trans);
assert (con != NULL);
con_python_wrapper = Transaction_get_con_python_wrapper(cur->trans);
assert (con_python_wrapper != NULL);
{ /* hoop-jump for C90's retarded scoping */
const boolean needed_to_acquire_tp = !CURRENT_THREAD_OWNS_CON_TP(con);
/* Make sure con stays alive until we're done with it. Note that cur
* might not stay alive until the end of this destructor. */
if (should_manipulate_cursor_refcnt) {
assert (cur->ob_refcnt != 0);
Py_INCREF(cur);
}
Py_INCREF(con);
Py_INCREF(con_python_wrapper);
#ifdef ENABLE_CONNECTION_TIMEOUT
if (needed_to_acquire_tp) {
ACQUIRE_CON_TP_WITH_GIL_HELD(con);
}
#endif
if (PreparedStatement_close_with_unlink(self, TRUE) == 0) {
assert (self->cur == NULL);
} else {
SUPPRESS_EXCEPTION;
}
#ifdef ENABLE_CONNECTION_TIMEOUT
if (needed_to_acquire_tp) {
RELEASE_CON_TP(con);
}
#endif /* ENABLE_CONNECTION_TIMEOUT */
} /* hoop-jump for C90's retarded scoping */
if (should_manipulate_cursor_refcnt) {
assert (cur->ob_refcnt != 0);
Py_DECREF(cur);
}
Py_DECREF(con);
Py_DECREF(con_python_wrapper);
} /* end of if (self->cur != NULL) block */
/* Release the PreparedStatement struct itself: */
PyObject_Del(self);
} /* pyob_PreparedStatement___del__ */
/************ PreparedStatement METHODS ACCESSIBLE TO PYTHON:END *************/
/************ PreparedStatement ATTRIBUTE GET/SET METHODS:BEGIN **************/
static PyObject *pyob_PreparedStatement_sql_get(
PreparedStatement *self, void *closure
)
{
PyObject *py_result;
PS_REQ_OPEN(self);
py_result = self->sql != NULL ? self->sql : Py_None;
Py_INCREF(py_result);
return py_result;
} /* pyob_PreparedStatement_sql_get */
static PyObject *pyob_PreparedStatement_statement_type_get(
PreparedStatement *self, void *closure
)
{
/* A PreparedStatement cannot function even minimally without knowing its own
* statement type, so kinterbasdb should never have allowed a PS that doesn't
* know its statement type to become accessible to Python client code.
*
* Because a PreparedStatement's underlying Curosr could close without the
* PS's consent, though, we ensure that the PS is open before returning the
* statement_type. */
int statement_type;
PS_REQ_OPEN(self);
statement_type = self->statement_type;
if (statement_type != NULL_STATEMENT_TYPE) {
return PyInt_FromLong(statement_type);
} else {
raise_exception(InternalError, "This PreparedStatement does not know its"
" own statement_type; kinterbasdb should not have allowed it to become"
" accessible to client code."
);
return NULL;
}
} /* pyob_PreparedStatement_statement_type_get */
static PyObject *pyob_PreparedStatement_plan_get(
PreparedStatement *self, void *closure
)
{
PyObject *ret = NULL;
PS_REQ_OPEN(self);
assert (self->cur != NULL);
#ifdef ENABLE_CONNECTION_TIMEOUT
CUR_ACTIVATE__FORBID_TRANSPARENT_RESUMPTION(self->cur, return NULL);
#endif /* ENABLE_CONNECTION_TIMEOUT */
assert (self->cur->trans != NULL);
assert (Transaction_get_con(self->cur->trans) != NULL);
CON_MUST_ALREADY_BE_ACTIVE(Transaction_get_con(self->cur->trans));
ret = _generic_single_item_isc_dsql_sql_info_request(
&self->stmt_handle, self->cur->status_vector, isc_info_sql_get_plan,
1 /* Skip 1 byte (newline character at beginning of plan string. */
);
if (ret == NULL) { goto fail; }
goto clean;
fail:
assert (PyErr_Occurred());
if (ret != NULL) {
Py_DECREF(ret);
ret = NULL;
}
/* Fall through to clean: */
clean:
#ifdef ENABLE_CONNECTION_TIMEOUT
CUR_PASSIVATE(self->cur);
CON_MUST_NOT_BE_ACTIVE(Transaction_get_con(self->cur->trans));
#endif /* ENABLE_CONNECTION_TIMEOUT */
return ret;
} /* pyob_PreparedStatement_plan_get */
#define _make_n_direction_params_getter(direction) \
/* direction is expected to be 'in' or 'out'. */ \
static PyObject *pyob_PreparedStatement_n_ ## direction ## put_params_get( \
PreparedStatement *self, void *closure \
) \
{ \
PS_REQ_OPEN(self); \
assert (self->cur != NULL); \
{ \
XSQLDA *sqlda = self->direction ## _sqlda; \
\
if (sqlda != NULL) { \
return PyInt_FromLong(sqlda->sqld); \
} else { \
raise_exception(InternalError, "Unexpected PreparedStatement state:" \
" the PS is considered 'open', but has no " # direction "put" \
"_sqlda." \
); \
return NULL; \
} \
} \
}
_make_n_direction_params_getter(in)
_make_n_direction_params_getter(out)
/************* PreparedStatement ATTRIBUTE GET/SET METHODS:END ***************/
/************************** UTILITY FUNCTIONS:BEGIN **************************/
static boolean _check_statement_length(Py_ssize_t length) {
/* Although the sql-statement-length parameter to such Firebird API functions
* as isc_dsql_prepare and isc_dsql_execute_immediate is an unsigned short,
* the documentation says that the length can be left zero for null-
* terminated strings, in which case the database engine will figure out the
* length itself.
* As of 2003.02.13, Firebird cannot handle SQL statements longer than the
* maximum value of an unsigned short even if zero is passed as the length.
*
* Test the length and raise an exception if it's too long for safe passage
* to SQL-handling isc_* functions. Return TRUE if OK; FALSE otherwise. */
assert (length >= 0);
if (length <= (Py_ssize_t) USHRT_MAX) {
return TRUE;
} else {
PyObject *py_length = PyLong_FromUnsignedLongLong(
(unsigned LONG_LONG) length
);
if (py_length != NULL) {
PyObject *py_length_str = PyObject_Str(py_length);
if (py_length_str != NULL) {
PyObject *err_msg = PyString_FromFormat(
"SQL statement of %s bytes is too long (max %d allowed). Consider"
" using bound parameters to shorten the SQL code, rather than"
" passing large values as part of the SQL string.",
PyString_AS_STRING(py_length_str), USHRT_MAX
);
if (err_msg != NULL) {
raise_exception(ProgrammingError, PyString_AS_STRING(err_msg));
Py_DECREF(err_msg);
}
Py_DECREF(py_length_str);
}
Py_DECREF(py_length);
}
return FALSE;
}
} /* _check_statement_length */
static PyObject *_generic_single_item_isc_dsql_sql_info_request(
isc_stmt_handle *stmt_handle, ISC_STATUS *sv,
const char request_code, const short skip_bytes_at_beginning_of_result
)
{
char req_buf[] = {0};
char *res_buf = NULL;
/* The size of the buffer is automatically increased if necessary. */
unsigned short res_buf_size = 128;
short result_length = -1;
PyObject *py_result = NULL;
ENTER_GDAL
req_buf[0] = request_code;
for (;;) {
assert (res_buf == NULL);
res_buf = kimem_plain_malloc(res_buf_size);
if (res_buf == NULL) {
LEAVE_GDAL_WITHOUT_ENDING_CODE_BLOCK
/* Was calling kimem_plain_malloc; need to set MemoryError explicitly. */
PyErr_NoMemory();
goto fail;
}
isc_dsql_sql_info(sv, stmt_handle, sizeof(req_buf), req_buf,
res_buf_size, res_buf
);
if (DB_API_ERROR(sv)) {
LEAVE_GDAL_WITHOUT_ENDING_CODE_BLOCK
raise_sql_exception(OperationalError, "isc_dsql_sql_info failed: ", sv);
goto fail;
}
{
const char first_byte = res_buf[0];
if (first_byte == isc_info_truncated) {
/* The result buffer wasn't large enough. Free the original result
* buffer and double the integer that will determine its size in the
* next pass, then jump to the next pass. */
kimem_plain_free(res_buf);
res_buf = NULL;
res_buf_size *= 2;
continue;
} else if (first_byte == isc_info_end) {
/* No result is available for this request; return None. */
LEAVE_GDAL_WITHOUT_ENDING_CODE_BLOCK
Py_INCREF(Py_None);
py_result = Py_None;
goto exit;
} else if (first_byte != request_code) {
LEAVE_GDAL_WITHOUT_ENDING_CODE_BLOCK
{
PyObject *err_msg = PyString_FromFormat(
"Unexpected code in result buffer. Expected %c; got %c.",
request_code, first_byte
);
if (err_msg != NULL) {
raise_exception(InternalError, PyString_AS_STRING(err_msg));
Py_DECREF(err_msg);
}
goto fail;
}
}
}
result_length = (short) isc_vax_integer(res_buf + 1, sizeof(short));
break;
}
LEAVE_GDAL
assert (result_length >= 0);
if (skip_bytes_at_beginning_of_result > result_length) {
raise_exception(InternalError,
"byte skip directive would overflow result."
);
goto fail;
}
{
const short read_n_bytes =
result_length - skip_bytes_at_beginning_of_result;
if (read_n_bytes != 0) {
py_result = PyString_FromStringAndSize(
res_buf + 3 + skip_bytes_at_beginning_of_result, read_n_bytes
);
} else {
py_result = PyString_FromStringAndSize("", 0);
}
}
if (py_result != NULL) {
goto exit;
}
/* Fall through to fail: */
fail:
assert (PyErr_Occurred());
if (py_result != NULL) {
Py_DECREF(py_result);
py_result = NULL;
}
/* Fall through to exit: */
exit:
if (res_buf != NULL) {
kimem_plain_free(res_buf);
}
return py_result;
} /* _generic_single_item_isc_dsql_sql_info_request */
static int _determine_statement_type(
isc_stmt_handle *stmt_handle, ISC_STATUS *status_vector
)
{
/* Given a pointer to the handle of a prepared statement, dynamically
* determine the database engine's internal statement type code.
*
* This function is essentially a special case of
* _generic_single_item_isc_dsql_sql_info_request, optimized for speed
* (it must be called for each and every statement preparation in
* kinterbasdb). */
int stmt_type;
short stmt_type_length;
static const char sql_info_stmt_type_req[] = {isc_info_sql_stmt_type};
#define sql_info_res_buf_size 8
char sql_info_res_buf[sql_info_res_buf_size];
ENTER_GDAL
isc_dsql_sql_info(status_vector, stmt_handle,
sizeof(sql_info_stmt_type_req),
#ifndef FIREBIRD_1_5_OR_LATER
(char *) /* Cast away constness. */
#endif
sql_info_stmt_type_req,
sql_info_res_buf_size, sql_info_res_buf
);
if (DB_API_ERROR(status_vector)) {
LEAVE_GDAL_WITHOUT_ENDING_CODE_BLOCK
raise_sql_exception(OperationalError, "_determine_statement_type: ",
status_vector
);
goto fail;
}
{
const char first_byte = sql_info_res_buf[0];
if (first_byte == isc_info_truncated) {
/* Since we know the required size of the information item we're
* requesting, this should never actually happen (and therefore we don't
* need to adjust the size dynamically). */
LEAVE_GDAL_WITHOUT_ENDING_CODE_BLOCK
raise_exception(InternalError, "_determine_statement_type: statically"
" sized result buffer was too small."
);
goto fail;
} else if (first_byte != isc_info_sql_stmt_type) {
LEAVE_GDAL_WITHOUT_ENDING_CODE_BLOCK
raise_exception(InternalError, "_determine_statement_type: expected"
" first byte of result buffer to be isc_info_sql_stmt_type."
);
goto fail;
}
}
stmt_type_length = (short)
isc_vax_integer(sql_info_res_buf + 1, sizeof(short) )
;
stmt_type = (int) isc_vax_integer(sql_info_res_buf + 3, stmt_type_length);
LEAVE_GDAL
return stmt_type;
fail:
assert (PyErr_Occurred());
return -1;
} /* _determine_statement_type */
/*************************** UTILITY FUNCTIONS:END ***************************/
/******** PreparedStatement CLASS DEFINITION AND INITIALIZATION:BEGIN ********/
static PyMethodDef PreparedStatement_methods[] = {
{NULL} /* sentinel */
};
static PyGetSetDef PreparedStatement_getters_setters[] = {
{"sql",
(getter) pyob_PreparedStatement_sql_get,
NULL,
"ASCII representation of the raw SQL string that underlies this"
" PreparedStatement."
},
{"statement_type",
(getter) pyob_PreparedStatement_statement_type_get,
NULL,
"An int that matches one of the kinterbasdb.isc_info_sql_stmt_*"
" constants. For example, the following comparison is True:"
"\n cur.prep(\"insert into test values (?,?)\").statement_type =="
" kinterbasdb.isc_info_sql_stmt_insert"
},
{"plan",
(getter) pyob_PreparedStatement_plan_get,
NULL,
"The PLAN string generated by the optimizer."
},
{"n_input_params",
(getter) pyob_PreparedStatement_n_input_params_get,
NULL,
"The number of input parameters expected by the statement."
},
{"n_output_params",
(getter) pyob_PreparedStatement_n_output_params_get,
NULL,
"The number of output parameters returned by the statement."
},
{"description",
(getter) pyob_PreparedStatement_description_get,
NULL,
"A DB API description tuple (of the same format as Cursor.description)"
" that describes the statement's output parameters."
},
{NULL} /* sentinel */
};
PyTypeObject PreparedStatementType = { /* new-style class */
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
"kinterbasdb.PreparedStatement", /* tp_name */
sizeof(PreparedStatement), /* tp_basicsize */
0, /* tp_itemsize */
(destructor) pyob_PreparedStatement___del__, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
(cmpfunc) PreparedStatement_compare,/* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
0, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
PreparedStatement_methods, /* tp_methods */
NULL, /* tp_members */
PreparedStatement_getters_setters, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
/* Currently using PreparedStatement_create only at the C level, so there's
* no conventional __init__ method: */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
static int init_kidb_preparedstatement(void) {
/* PreparedStatementType is a new-style class, so PyType_Ready must be called
* before its getters and setters will function. */
if (PyType_Ready(&PreparedStatementType) < 0) { goto fail; }
return 0;
fail:
/* This function is indirectly called by the module loader, which makes no
* provision for error recovery. */
return -1;
}
/********* PreparedStatement CLASS DEFINITION AND INITIALIZATION:END *********/
/* PSTracker MEMBER FUNC DEFINITIONS AND SUPPORTING FUNCS: BEGIN */
#include "_kisupport_lifo_linked_list.h"
LIFO_LINKED_LIST_DEFINE_BASIC_METHODS_PYALLOC_NOQUAL(PSTracker, PreparedStatement)
LIFO_LINKED_LIST_DEFINE_TRAVERSE_NOQUAL(PSTracker, PreparedStatement)
/* PSTracker MEMBER FUNC DEFINITIONS AND SUPPORTING FUNCS: END */
|