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 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908
|
/*
+----------------------------------------------------------------------+
| PHP HTML Embedded Scripting Language Version 3.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2000 PHP Development Team (See Credits file) |
+----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or modify |
| it under the terms of one of the following licenses: |
| |
| A) the GNU General Public License as published by the Free Software |
| Foundation; either version 2 of the License, or (at your option) |
| any later version. |
| |
| B) the PHP License as published by the PHP Development Team and |
| included in the distribution in the file: LICENSE |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of both licenses referred to here. |
| If you did not, or have any questions about PHP licensing, please |
| contact core@php.net. |
+----------------------------------------------------------------------+
| Authors: Stig Sther Bakken <ssb@fast.no> |
| Mitch Golden <mgolden@interport.net> |
| Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
| Andreas Karajannis <Andreas.Karajannis@gmd.de> |
| Thies C. Arntzen <thies@digicol.de> |
+----------------------------------------------------------------------+
*/
/* comment out the next line if you're on Oracle 7.x and don't have the olog
call. */
#define HAS_OLOG 1
#if defined(COMPILE_DL)
# include "dl/phpdl.h"
#endif
#include "php.h"
#if PHP_API_VERSION < 19990421
#include "internal_functions.h"
#include "php3_list.h"
#include "head.h"
#else
#include "zend_globals.h"
#endif
#if HAVE_ORACLE
#if PHP_API_VERSION < 19990421
#include "oracle.h"
#define HASH_DTOR (void (*)(void *))
#else
#include "php3_oracle.h"
#define HASH_DTOR (int (*)(void *))
#endif
#ifndef ZEND_MODULE_INFO_FUNC_ARGS
#define ZEND_MODULE_INFO_FUNC_ARGS void
#endif
#ifdef WIN32
# include "variables.h"
#else
# include "build-defs.h"
#endif
#include "snprintf.h"
#ifndef min
#define min(a, b) ((a) > (b) ? (b) : (a))
#endif
oracle_module php3_oracle_module;
#define ORACLE_GLOBAL(a) a
#define ORACLE_TLS_VARS
#undef ORACLE_DEBUG
#define DB_SIZE 65536
#define ORA_FETCHINTO_ASSOC (1<<0)
#define ORA_FETCHINTO_NULLS (1<<1)
static oraConnection *ora_get_conn(HashTable *,HashTable *, int);
static int ora_add_cursor(HashTable *, oraCursor *);
static oraCursor *ora_get_cursor(HashTable *, int);
static void ora_del_cursor(HashTable *, int);
static char *ora_error(Cda_Def *);
static int ora_describe_define(oraCursor *);
static int _close_oraconn(oraConnection *conn);
static int _close_orapconn(oraConnection *conn);
static int _close_oracur(oraCursor *cur);
static int _ora_ping(oraConnection *conn);
int ora_set_param_values(oraCursor *cursor, int isout);
void php3_Ora_Do_Logon(INTERNAL_FUNCTION_PARAMETERS, int persistent);
function_entry oracle_functions[] = {
{"ora_bind", php3_Ora_Bind, NULL},
{"ora_close", php3_Ora_Close, NULL},
{"ora_commit", php3_Ora_Commit, NULL},
{"ora_commitoff", php3_Ora_CommitOff, NULL},
{"ora_commiton", php3_Ora_CommitOn, NULL},
{"ora_do", php3_Ora_Do, NULL},
{"ora_error", php3_Ora_Error, NULL},
{"ora_errorcode", php3_Ora_ErrorCode, NULL},
{"ora_exec", php3_Ora_Exec, NULL},
{"ora_fetch", php3_Ora_Fetch, NULL},
{"ora_fetch_into", php3_Ora_FetchInto, NULL},
{"ora_columntype", php3_Ora_ColumnType, NULL},
{"ora_columnname", php3_Ora_ColumnName, NULL},
{"ora_columnsize", php3_Ora_ColumnSize, NULL},
{"ora_getcolumn", php3_Ora_GetColumn, NULL},
{"ora_numcols", php3_Ora_NumCols, NULL},
{"ora_numrows", php3_Ora_NumRows, NULL},
{"ora_logoff", php3_Ora_Logoff, NULL},
{"ora_logon", php3_Ora_Logon, NULL},
{"ora_plogon", php3_Ora_PLogon, NULL},
{"ora_open", php3_Ora_Open, NULL},
{"ora_parse", php3_Ora_Parse, NULL},
{"ora_rollback", php3_Ora_Rollback, NULL},
{NULL, NULL, NULL}
};
php3_module_entry oracle_module_entry = {
"Oracle",
oracle_functions,
php3_minit_oracle,
php3_mshutdown_oracle,
php3_rinit_oracle,
php3_rshutdown_oracle,
php3_info_oracle,
STANDARD_MODULE_PROPERTIES
};
static const text *ora_func_tab[] =
{(text *) "unused",
/* 1, 2 */ (text *) "unused", (text *) "OSQL",
/* 3, 4 */ (text *) "unused", (text *) "OEXEC/OEXN",
/* 5, 6 */ (text *) "unused", (text *) "OBIND",
/* 7, 8 */ (text *) "unused", (text *) "ODEFIN",
/* 9, 10 */ (text *) "unused", (text *) "ODSRBN",
/* 11, 12 */ (text *) "unused", (text *) "OFETCH/OFEN",
/* 13, 14 */ (text *) "unused", (text *) "OOPEN",
/* 15, 16 */ (text *) "unused", (text *) "OCLOSE",
/* 17, 18 */ (text *) "unused", (text *) "unused",
/* 19, 20 */ (text *) "unused", (text *) "unused",
/* 21, 22 */ (text *) "unused", (text *) "ODSC",
/* 23, 24 */ (text *) "unused", (text *) "ONAME",
/* 25, 26 */ (text *) "unused", (text *) "OSQL3",
/* 27, 28 */ (text *) "unused", (text *) "OBNDRV",
/* 29, 30 */ (text *) "unused", (text *) "OBNDRN",
/* 31, 32 */ (text *) "unused", (text *) "unused",
/* 33, 34 */ (text *) "unused", (text *) "OOPT",
/* 35, 36 */ (text *) "unused", (text *) "unused",
/* 37, 38 */ (text *) "unused", (text *) "unused",
/* 39, 40 */ (text *) "unused", (text *) "unused",
/* 41, 42 */ (text *) "unused", (text *) "unused",
/* 43, 44 */ (text *) "unused", (text *) "unused",
/* 45, 46 */ (text *) "unused", (text *) "unused",
/* 47, 48 */ (text *) "unused", (text *) "unused",
/* 49, 50 */ (text *) "unused", (text *) "unused",
/* 51, 52 */ (text *) "unused", (text *) "OCAN",
/* 53, 54 */ (text *) "unused", (text *) "OPARSE",
/* 55, 56 */ (text *) "unused", (text *) "OEXFET",
/* 57, 58 */ (text *) "unused", (text *) "OFLNG",
/* 59, 60 */ (text *) "unused", (text *) "ODESCR",
/* 61, 62 */ (text *) "unused", (text *) "OBNDRA"
};
/* This variable is to store error information in the case
* when there was connection error */
static oraConnection db_err_conn;
#if COMPILE_DL
DLEXPORT php3_module_entry *get_module() { return &oracle_module_entry; };
#endif
static int _close_oraconn(oraConnection *conn)
{
ORACLE_TLS_VARS;
conn->open = 0;
ologof(&conn->lda);
ORACLE_GLOBAL(php3_oracle_module).num_links--;
efree(conn);
_php3_hash_del(ORACLE_GLOBAL(php3_oracle_module).conns,(void*)&conn,sizeof(void*));
return 1;
}
static int _close_orapconn(oraConnection *conn)
{
ORACLE_TLS_VARS;
conn->open = 0;
ologof(&conn->lda);
free(conn);
ORACLE_GLOBAL(php3_oracle_module).num_links--;
ORACLE_GLOBAL(php3_oracle_module).num_persistent--;
_php3_hash_del(ORACLE_GLOBAL(php3_oracle_module).conns,(void*)&conn,sizeof(void*));
return 1;
}
static int
pval_ora_param_destructor(oraParam *param)
{
if (param->progv) {
efree(param->progv);
}
return 1;
}
static int _close_oracur(oraCursor *cur)
{
int i;
ORACLE_TLS_VARS;
if (cur){
if (cur->query){
efree(cur->query);
}
if (cur->params){
_php3_hash_destroy(cur->params);
efree(cur->params);
cur->params = NULL;
}
if (cur->columns){
for(i = 0; i < cur->ncols; i++){
if (cur->columns[i].buf)
efree(cur->columns[i].buf);
}
efree(cur->columns);
cur->columns = NULL;
}
if (cur->open){
oraConnection *db_conn;
if (_php3_hash_find(ORACLE_GLOBAL(php3_oracle_module).conns,(void*)&(cur->conn_ptr),sizeof(void*),(void **)&db_conn) == SUCCESS) {
oclose(&cur->cda);
}
}
efree(cur);
}
return 1;
}
int php3_minit_oracle(INIT_FUNC_ARGS)
{
if (cfg_get_long("oracle.allow_persistent",
&ORACLE_GLOBAL(php3_oracle_module).allow_persistent)
== FAILURE) {
ORACLE_GLOBAL(php3_oracle_module).allow_persistent = -1;
}
if (cfg_get_long("oracle.max_persistent",
&ORACLE_GLOBAL(php3_oracle_module).max_persistent)
== FAILURE) {
ORACLE_GLOBAL(php3_oracle_module).max_persistent = -1;
}
if (cfg_get_long("oracle.max_links",
&ORACLE_GLOBAL(php3_oracle_module).max_links)
== FAILURE) {
ORACLE_GLOBAL(php3_oracle_module).max_links = -1;
}
ORACLE_GLOBAL(php3_oracle_module).num_persistent = 0;
ORACLE_GLOBAL(php3_oracle_module).le_cursor =
register_list_destructors(_close_oracur, NULL);
ORACLE_GLOBAL(php3_oracle_module).le_conn =
register_list_destructors(_close_oraconn, NULL);
ORACLE_GLOBAL(php3_oracle_module).le_pconn =
register_list_destructors(NULL, _close_orapconn);
ORACLE_GLOBAL(php3_oracle_module).conns = malloc(sizeof(HashTable));
_php3_hash_init(ORACLE_GLOBAL(php3_oracle_module).conns, 13, NULL, NULL, 1);
REGISTER_LONG_CONSTANT("ORA_BIND_INOUT", 0, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("ORA_BIND_IN", 1, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("ORA_BIND_OUT", 2, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("ORA_FETCHINTO_ASSOC",ORA_FETCHINTO_ASSOC, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("ORA_FETCHINTO_NULLS",ORA_FETCHINTO_NULLS, CONST_CS | CONST_PERSISTENT);
#if NEEDED
opinit(OCI_EV_TSF); /* initialize threaded environment - must match the threaded mode
of the oci8 driver if both are used at the same time!! */
#endif
return SUCCESS;
}
int php3_rinit_oracle(INIT_FUNC_ARGS)
{
ORACLE_TLS_VARS;
ORACLE_GLOBAL(php3_oracle_module).num_links =
ORACLE_GLOBAL(php3_oracle_module).num_persistent;
/*
ORACLE_GLOBAL(php3_oracle_module).defaultlrl = 0;
ORACLE_GLOBAL(php3_oracle_module).defaultbinmode = 0;
ORACLE_GLOBAL(php3_oracle_module).defaultconn = 0;
*/
return SUCCESS;
}
int php3_mshutdown_oracle(SHUTDOWN_FUNC_ARGS)
{
ORACLE_TLS_VARS;
_php3_hash_destroy(ORACLE_GLOBAL(php3_oracle_module).conns);
free(ORACLE_GLOBAL(php3_oracle_module).conns);
return SUCCESS;
}
int php3_rshutdown_oracle(SHUTDOWN_FUNC_ARGS)
{
ORACLE_TLS_VARS;
return SUCCESS;
}
static int _ora_ping(oraConnection *conn)
{
Cda_Def cda;
if (oopen(&cda, &conn->lda, (text *) 0, -1, -1, (text *) 0, -1)) {
return 0;
}
if (oparse(&cda, "select sysdate from dual", (sb4) - 1, 0, VERSION_7)) {
oclose(&cda);
return 0;
}
oclose(&cda);
return 1;
}
/*
** PHP functions
*/
/* {{{ proto int ora_logon(string user, string password)
Open an Oracle connection */
void php3_Ora_Logon(INTERNAL_FUNCTION_PARAMETERS)
{
php3_Ora_Do_Logon(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ proto int ora_plogon(string user, string password)
Open a persistant Oracle connection */
void php3_Ora_PLogon(INTERNAL_FUNCTION_PARAMETERS)
{
php3_Ora_Do_Logon(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
void php3_Ora_Do_Logon(INTERNAL_FUNCTION_PARAMETERS, int persistent)
{
char *user = NULL;
char *pwd = NULL;
pval *arg1, *arg2;
oraConnection *db_conn;
list_entry *index_ptr;
char *hashed_details;
int hashed_len, len, id;
ORACLE_TLS_VARS;
if (getParameters(ht, 2, &arg1, &arg2) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(arg1);
convert_to_string(arg2);
user = arg1->value.str.val;
pwd = arg2->value.str.val;
if (!ORACLE_GLOBAL(php3_oracle_module).allow_persistent) {
persistent = 0;
}
if (ORACLE_GLOBAL(php3_oracle_module).max_links != -1 &&
ORACLE_GLOBAL(php3_oracle_module).num_links >=
ORACLE_GLOBAL(php3_oracle_module).max_links) {
php3_error(E_WARNING, "Oracle: Too many open links (%d)",
ORACLE_GLOBAL(php3_oracle_module).num_links);
RETURN_FALSE;
}
/* the user requested a persistent connection */
if (persistent &&
ORACLE_GLOBAL(php3_oracle_module).max_persistent != -1 &&
ORACLE_GLOBAL(php3_oracle_module).num_persistent >=
ORACLE_GLOBAL(php3_oracle_module).max_persistent) {
php3_error(E_WARNING,"Oracle: Too many open persistent links (%d)",
ORACLE_GLOBAL(php3_oracle_module).num_persistent);
RETURN_FALSE;
}
len = strlen(user) + strlen(pwd) + 9;
hashed_details = emalloc(len);
if (hashed_details == NULL) {
php3_error(E_WARNING, "Out of memory");
RETURN_FALSE;
}
hashed_len = _php3_sprintf(hashed_details, "ora_%s_%s", user, pwd);
/* try to find if we already have this link in our persistent list,
* no matter if it is to be persistent or not
*/
if (_php3_hash_find(plist, hashed_details, hashed_len + 1,
(void **) &index_ptr) == FAILURE) {
/* the link is not in the persistent list */
list_entry new_index_ptr;
if (persistent)
db_conn = (oraConnection *)malloc(sizeof(oraConnection));
else
db_conn = (oraConnection *)emalloc(sizeof(oraConnection));
if (db_conn == NULL){
efree(hashed_details);
php3_error(E_WARNING, "Out of memory");
RETURN_FALSE;
}
memset((void *) db_conn,0,sizeof(oraConnection));
#if HAS_OLOG
if (olog(&db_conn->lda, db_conn->hda, user,
strlen(user), pwd, strlen(pwd), 0, -1, OCI_LM_DEF)) {
#else
if (orlon(&db_conn->lda, db_conn->hda, user,
strlen(user), pwd, strlen(pwd), 0)) {
#endif
php3_error(E_WARNING, "Unable to connect to ORACLE (%s)",
ora_error(&db_conn->lda));
/* The next line is to provide error information
* for OraError && OraErrorCode calls */
db_err_conn = *db_conn;
if (persistent)
free(db_conn);
else
efree(db_conn);
efree(hashed_details);
RETURN_FALSE;
}
db_conn->open = 1;
if (persistent){
/*new_le.type = ORACLE_GLOBAL(php3_oracle_module).le_pconn;
new_le.ptr = db_conn;*/
RETVAL_RESOURCE(php3_plist_insert(db_conn, ORACLE_GLOBAL(php3_oracle_module).le_pconn));
new_index_ptr.ptr = (void *) return_value->value.lval;
new_index_ptr.type = le_index_ptr;
if (_php3_hash_update(plist,hashed_details,hashed_len + 1,(void *) &new_index_ptr,
sizeof(list_entry),NULL) == FAILURE) {
ologof(&db_conn->lda);
free(db_conn);
efree(hashed_details);
php3_error(E_WARNING, "Can't update hashed details list");
RETURN_FALSE;
}
ORACLE_GLOBAL(php3_oracle_module).num_persistent++;
} else {
/* non persistent, simply add to list */
RETVAL_RESOURCE(php3_list_insert(db_conn, ORACLE_GLOBAL(php3_oracle_module).le_conn));
}
ORACLE_GLOBAL(php3_oracle_module).num_links++;
} else {
int type;
/* the link is already in the persistent list */
if (index_ptr->type != le_index_ptr) {
efree(hashed_details);
php3_error(E_WARNING, "Oops, something went completly wrong");
RETURN_FALSE;
}
id = (int) index_ptr->ptr;
db_conn = (oraConnection *)php3_plist_find(id, &type);
if (db_conn && (type == ORACLE_GLOBAL(php3_oracle_module).le_conn ||
type == ORACLE_GLOBAL(php3_oracle_module).le_pconn)){
if(!_ora_ping(db_conn)) {
/* XXX Reinitialize lda, hda ? */
#if HAS_OLOG
if(olog(&db_conn->lda, db_conn->hda, user,
strlen(user), pwd, strlen(pwd), 0, -1, OCI_LM_DEF)) {
#else
if(orlon(&db_conn->lda, db_conn->hda, user,
strlen(user), pwd, strlen(pwd), 0)) {
#endif
/* The next line is to provide error information
* for OraError && OraErrorCode calls */
db_err_conn = *db_conn;
php3_error(E_WARNING, "Unable to reconnect to ORACLE (%s)",
ora_error(&db_conn->lda));
/* Delete list entry for this connection */
php3_plist_delete(id);
/* Delete hashed list entry for this dead connection */
_php3_hash_del(plist, hashed_details, hashed_len);
efree(hashed_details);
RETURN_FALSE;
}
}
RETVAL_RESOURCE(id);
}
}
_php3_hash_add(ORACLE_GLOBAL(php3_oracle_module).conns,
(void*)&db_conn,
sizeof(void*),
(void*)&db_conn,
sizeof(void*),
NULL);
efree(hashed_details);
}
/* {{{ proto int ora_logoff(int connection)
Close an Oracle connection */
void php3_Ora_Logoff(INTERNAL_FUNCTION_PARAMETERS)
{ /* conn_index */
int type, ind;
oraConnection *conn;
pval *arg;
ORACLE_TLS_VARS;
if (getParameters(ht, 1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(arg);
ind = (int)arg->value.lval;
conn = (oraConnection *)php3_list_find(ind, &type);
if (!conn || (type != ORACLE_GLOBAL(php3_oracle_module).le_conn &&
type != ORACLE_GLOBAL(php3_oracle_module).le_pconn)) {
return;
}
php3_list_delete(ind);
}
/* }}} */
/* {{{ proto int ora_open(int connection)
Open an Oracle cursor */
void php3_Ora_Open(INTERNAL_FUNCTION_PARAMETERS)
{ /* conn_index */
pval *arg;
oraConnection *conn = NULL;
oraCursor *cursor = NULL;
int conn_ind;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(arg);
conn_ind = arg->value.lval;
conn = ora_get_conn(list,plist, conn_ind);
if (conn == NULL) {
RETURN_FALSE;
}
if ((cursor = (oraCursor *)emalloc(sizeof(oraCursor))) == NULL){
php3_error(E_WARNING, "Out of memory");
RETURN_FALSE;
}
memset(cursor, 0, sizeof(oraCursor));
if (oopen(&cursor->cda, &conn->lda, (text *) 0, -1, -1, (text *) 0, -1)) {
php3_error(E_WARNING, "Unable to open new cursor (%s)",
ora_error(&cursor->cda));
efree(cursor);
RETURN_FALSE;
}
cursor->open = 1;
cursor->conn_ptr = conn;
cursor->conn_id = conn_ind;
RETURN_RESOURCE(ora_add_cursor(list, cursor));
}
/* }}} */
/* {{{ proto int ora_close(int cursor)
Close an Oracle cursor */
void php3_Ora_Close(INTERNAL_FUNCTION_PARAMETERS)
{ /* conn_index */
pval *arg;
if (getParameters(ht, 1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(arg);
ora_del_cursor(list, arg->value.lval);
RETVAL_TRUE;
}
/* }}} */
/* {{{ proto int ora_commitoff(int connection)
Disable automatic commit */
void php3_Ora_CommitOff(INTERNAL_FUNCTION_PARAMETERS)
{ /* conn_index */
pval *arg;
oraConnection *conn;
if (getParameters(ht, 1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(arg);
conn = ora_get_conn(list,plist, arg->value.lval);
if (conn == NULL) {
RETURN_FALSE;
}
if (ocof(&conn->lda)) {
php3_error(E_WARNING, "Unable to turn off auto-commit (%s)",
ora_error(&conn->lda));
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int ora_commiton(int connection)
Enable automatic commit */
void php3_Ora_CommitOn(INTERNAL_FUNCTION_PARAMETERS)
{ /* conn_index */
pval *arg;
oraConnection *conn;
if (getParameters(ht, 1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(arg);
if (!(conn = ora_get_conn(list,plist, arg->value.lval))) {
RETURN_FALSE;
}
if (ocon(&conn->lda)) {
php3_error(E_WARNING, "Unable to turn on auto-commit (%s)",
ora_error(&conn->lda));
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int ora_commit(int connection)
Commit an Oracle transaction */
void php3_Ora_Commit(INTERNAL_FUNCTION_PARAMETERS)
{ /* conn_index */
pval *arg;
oraConnection *conn;
if (getParameters(ht, 1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(arg);
conn = ora_get_conn(list,plist, arg->value.lval);
if (conn == NULL) {
RETURN_FALSE;
}
if (ocom(&conn->lda)) {
php3_error(E_WARNING, "Unable to commit transaction (%s)",
ora_error(&conn->lda));
RETURN_FALSE;
}
RETVAL_TRUE;
}
/* }}} */
/* {{{ proto int ora_rollback(int connection)
Roll back an Oracle transaction */
void php3_Ora_Rollback(INTERNAL_FUNCTION_PARAMETERS)
{ /* conn_index */
pval *arg;
oraConnection *conn;
if (getParameters(ht, 1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(arg);
conn = ora_get_conn(list,plist, arg->value.lval);
if (conn == NULL) {
RETURN_FALSE;
}
if (orol(&conn->lda)) {
php3_error(E_WARNING, "Unable to roll back transaction (%s)",
ora_error(&conn->lda));
RETURN_FALSE;
}
RETVAL_TRUE;
}
/* }}} */
/* {{{ proto int ora_parse(int cursor, string sql_statement [, int defer])
Parse an Oracle SQL statement */
void php3_Ora_Parse(INTERNAL_FUNCTION_PARAMETERS)
{
/* cursor_ind, sql_statement [, defer] */
int argc;
pval *argv[3];
oraCursor *cursor;
sword defer = 0;
text *query;
argc = ARG_COUNT(ht);
if ((argc != 2 && argc != 3) || getParametersArray(ht, argc, argv) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(argv[0]);
convert_to_string(argv[1]);
if (argc == 3) {
convert_to_long(argv[2]);
if (argv[2]->value.lval != 0) {
defer = DEFER_PARSE;
}
}
query = (text *) estrndup(argv[1]->value.str.val,argv[1]->value.str.len);
if (query == NULL) {
php3_error(E_WARNING, "Invalid query");
RETURN_FALSE;
}
if (!(cursor = ora_get_cursor(list, argv[0]->value.lval))){
efree(query);
RETURN_FALSE;
}
if (cursor->query) {
efree(cursor->query);
}
cursor->query = query;
cursor->fetched = 0;
if(cursor->params && cursor->nparams > 0){
_php3_hash_destroy(cursor->params);
efree(cursor->params);
cursor->params = NULL;
cursor->nparams = 0;
}
if (oparse(&cursor->cda, query, (sb4) - 1, defer, VERSION_7)) {
php3_error(E_WARNING, "Ora_Parse failed (%s)",
ora_error(&cursor->cda));
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int ora_bind(int cursor, string php_variable_name, string sql_parameter_name, int length [, int type])
Bind a PHP variable to an Oracle parameter */
void php3_Ora_Bind(INTERNAL_FUNCTION_PARAMETERS)
{ /* cursor_ind, php_var_name, sql_var_name, data_len [, inout]*/
/* inout: 0 = in/out, 1 = in, 2 = out */
int argc;
pval *argv[5];
oraParam *newparam, *paramptr;
oraCursor *cursor;
char *paramname;
argc = ARG_COUNT(ht);
if (argc < 4 || argc > 5 || getParametersArray(ht, argc, argv) == FAILURE){
WRONG_PARAM_COUNT;
}
convert_to_long(argv[0]);
convert_to_string(argv[1]);
convert_to_string(argv[2]);
convert_to_long(argv[3]);
cursor = ora_get_cursor(list, argv[0]->value.lval);
if (cursor == NULL) {
php3_error(E_WARNING, "Invalid cursor index %d",
argv[0]->value.lval);
RETURN_FALSE;
}
if(cursor->params == NULL){
cursor->params = (HashTable *)emalloc(sizeof(HashTable));
if (!cursor->params ||
_php3_hash_init(cursor->params, 19, NULL,
HASH_DTOR pval_ora_param_destructor, 0) == FAILURE) {
php3_error(E_ERROR, "Unable to initialize parameter list");
RETURN_FALSE;
}
}
if((newparam = (oraParam *)emalloc(sizeof(oraParam))) == NULL){
php3_error(E_WARNING, "Out of memory for parameter");
RETURN_FALSE;
}
if((paramname = estrndup(argv[1]->value.str.val, argv[1]->value.str.len)) == NULL){
php3_error(E_WARNING, "Out of memory for parametername");
efree(newparam);
RETURN_FALSE;
}
if (_php3_hash_add(cursor->params, paramname, argv[1]->value.str.len + 1, newparam, sizeof(oraParam), (void **)¶mptr) == FAILURE) {
/* XXX _php3_hash_destroy */
efree(paramname);
efree(newparam);
php3_error(E_ERROR, "Could not make parameter placeholder");
RETURN_FALSE;
}
efree(newparam);
efree(paramname);
paramptr->progvl = argv[3]->value.lval + 1;
if(argc > 4){
convert_to_long(argv[4]);
paramptr->inout = (short)argv[4]->value.lval;
}else{
paramptr->inout = 0;
}
if((paramptr->progv = (text *)emalloc(paramptr->progvl)) == NULL){
php3_error(E_WARNING, "Out of memory for parameter value");
RETURN_FALSE;
}
/* XXX Maximum for progvl */
paramptr->alen = paramptr->progvl;
if (obndra(&cursor->cda,
argv[2]->value.str.val,
-1,
(ub1 *)paramptr->progv,
paramptr->progvl,
SQLT_STR, /* ftype */
-1, /* scale */
0/*¶mptr->ind*/, /* ind */
¶mptr->alen, /* alen */
0 /*¶mptr->arcode*/,
0, /* maxsize */
0,
0,
-1,
-1)) {
php3_error(E_WARNING, "Ora_Bind failed (%s)",
ora_error(&cursor->cda));
RETURN_FALSE;
}
cursor->nparams++;
RETURN_TRUE;
}
/* }}} */
/*
XXX Make return values compatible with old module ?
*/
/* {{{ proto int ora_exec(int cursor)
Execute a parsed statement */
void php3_Ora_Exec(INTERNAL_FUNCTION_PARAMETERS)
{ /* cursor_index */
pval *arg;
oraCursor *cursor = NULL;
if (getParameters(ht, 1, &arg) == FAILURE)
WRONG_PARAM_COUNT;
convert_to_long(arg);
if ((cursor = ora_get_cursor(list, arg->value.lval)) == NULL) {
RETURN_FALSE;
}
if (cursor->cda.ft == FT_SELECT) {
if (ora_describe_define(cursor) < 0) {
/* error message is given by ora_describe_define() */
RETURN_FALSE;
}
}
if(cursor->nparams > 0){
if(!ora_set_param_values(cursor, 0)){
RETURN_FALSE;
}
}
if (oexec(&cursor->cda)) {
php3_error(E_WARNING, "Ora_Exec failed (%s)",
ora_error(&cursor->cda));
RETURN_FALSE;
}
if(cursor->nparams > 0){
if(!ora_set_param_values(cursor, 1)){
RETURN_FALSE;
}
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int ora_numcols(int cursor)
Returns the numbers of columns in a result */
void php3_Ora_NumCols(INTERNAL_FUNCTION_PARAMETERS)
{ /* cursor_index */
pval *arg;
oraCursor *cursor = NULL;
if (getParameters(ht, 1, &arg) == FAILURE)
WRONG_PARAM_COUNT;
convert_to_long(arg);
if ((cursor = ora_get_cursor(list, arg->value.lval)) == NULL) {
RETURN_FALSE;
}
RETURN_LONG(cursor->ncols);
}
/* }}} */
/* {{{ proto int ora_numrows(int cursor)
Returns the number of rows in a result */
void php3_Ora_NumRows(INTERNAL_FUNCTION_PARAMETERS)
{ /* cursor_index */
pval *arg;
oraCursor *cursor = NULL;
if(getParameters(ht, 1, &arg) == FAILURE)
WRONG_PARAM_COUNT;
convert_to_long(arg);
if((cursor = ora_get_cursor(list, arg->value.lval)) == NULL) {
RETURN_FALSE;
}
RETURN_LONG(cursor->cda.rpc);
}
/* }}} */
/* prepares/executes/fetches 1st row if avail */
/* {{{ proto int ora_do(int connection, int cursor)
Parse and execute a statement and fetch first result row */
void php3_Ora_Do(INTERNAL_FUNCTION_PARAMETERS)
{
pval *argv[2];
oraConnection *conn = NULL;
oraCursor *cursor = NULL;
text *query;
if (ARG_COUNT(ht) != 2 || getParametersArray(ht, 2, argv) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(argv[0]);
convert_to_string(argv[1]);
conn = ora_get_conn(list,plist, argv[0]->value.lval);
if (conn == NULL) {
RETURN_FALSE;
}
if ((cursor = (oraCursor *)emalloc(sizeof(oraCursor))) == NULL){
php3_error(E_WARNING, "Out of memory");
RETURN_FALSE;
}
memset(cursor, 0, sizeof(oraCursor));
query = (text *) estrndup(argv[1]->value.str.val,argv[1]->value.str.len);
if (query == NULL) {
php3_error(E_WARNING, "Invalid query in Ora_Do");
RETURN_FALSE;
}
cursor->query = query;
if (oopen(&cursor->cda, &conn->lda, (text *) 0, -1, -1, (text *) 0, -1)) {
php3_error(E_WARNING, "Unable to open new cursor (%s)",
ora_error(&cursor->cda));
efree(cursor);
RETURN_FALSE;
}
cursor->open = 1;
cursor->conn_ptr = conn;
cursor->conn_id = argv[0]->value.lval;
/* Prepare stmt */
if (oparse(&cursor->cda, query, (sb4) - 1, 1, VERSION_7)){
php3_error(E_WARNING, "Ora_Do failed (%s)",
ora_error(&cursor->cda));
_close_oracur(cursor);
RETURN_FALSE;
}
/* Execute stmt (and fetch 1st row for selects) */
if (cursor->cda.ft == FT_SELECT) {
if (ora_describe_define(cursor) < 0){
/* error message is given by ora_describe_define() */
_close_oracur(cursor);
RETURN_FALSE;
}
if (oexfet(&cursor->cda, 1, 0, 0)) {
php3_error(E_WARNING, "Ora_Do failed (%s)",
ora_error(&cursor->cda));
_close_oracur(cursor);
RETURN_FALSE;
}
cursor->fetched = 1;
} else {
if (oexec(&cursor->cda)) {
php3_error(E_WARNING, "Ora_Do failed (%s)",
ora_error(&cursor->cda));
_close_oracur(cursor);
RETURN_FALSE;
}
}
RETURN_RESOURCE(ora_add_cursor(list, cursor));
}
/* }}} */
/* {{{ proto int ora_fetch(int cursor)
Fetch a row of result data from a cursor */
void php3_Ora_Fetch(INTERNAL_FUNCTION_PARAMETERS)
{ /* cursor_index */
pval *arg;
oraCursor *cursor;
if (getParameters(ht, 1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(arg);
/* Find the cursor */
if ((cursor = ora_get_cursor(list, arg->value.lval)) == NULL) {
RETURN_FALSE;
}
if (cursor->ncols == 0){
php3_error(E_WARNING, "No tuples available on this cursor");
RETURN_FALSE;
}
/* Get data from Oracle */
if (ofetch(&cursor->cda)) {
if (cursor->cda.rc != NO_DATA_FOUND) {
php3_error(E_WARNING, "Ora_Fetch failed (%s)",
ora_error(&cursor->cda));
}
RETURN_FALSE;
}
cursor->fetched++;
RETVAL_TRUE;
}
/* }}} */
/* {{{ proto int ora_fetch_into(int cursor, array result [, int flags])
Fetch a row into the specified result array */
void php3_Ora_FetchInto(INTERNAL_FUNCTION_PARAMETERS)
{
pval *arg1, *arr, *flg, *tmp;
oraCursor *cursor;
int i;
int flags = 0;
switch(ARG_COUNT(ht)){
case 2:
if (getParameters(ht, 2, &arg1, &arr) == FAILURE) {
WRONG_PARAM_COUNT;
}
break;
case 3:
if (getParameters(ht, 3, &arg1, &arr, &flg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(flg);
flags = flg->value.lval;
break;
default:
WRONG_PARAM_COUNT;
break;
}
if (!ParameterPassedByReference(ht, 2)){
php3_error(E_WARNING, "Array not passed by reference in call to ora_fetch_into()");
RETURN_FALSE;
}
convert_to_long(arg1);
/* Find the cursor */
if ((cursor = ora_get_cursor(list, arg1->value.lval)) == NULL) {
RETURN_FALSE;
}
if (cursor->ncols == 0){
php3_error(E_WARNING, "No tuples available on this cursor");
RETURN_FALSE;
}
if (ofetch(&cursor->cda)) {
if (cursor->cda.rc != NO_DATA_FOUND) {
php3_error(E_WARNING, "Ora_Fetch_Into failed (%s)",
ora_error(&cursor->cda));
}
RETURN_FALSE;
}
cursor->fetched++;
if (arr->type != IS_ARRAY){
php3tls_pval_destructor(arr);
if (array_init(arr) == FAILURE){
php3_error(E_WARNING, "Can't convert to type Array");
RETURN_FALSE;
}
}
_php3_hash_internal_pointer_reset(arr->value.ht);
#if PHP_API_VERSION < 19990421
tmp = emalloc(sizeof(pval));
#endif
for (i = 0; i < cursor->ncols; i++) {
if (cursor->columns[i].col_retcode == 1405) {
if (!(flags&ORA_FETCHINTO_NULLS)){
continue; /* don't add anything for NULL columns, unless the calles wants it */
} else {
tmp->value.str.val = empty_string;
tmp->value.str.len = 0;
}
} else if (cursor->columns[i].col_retcode != 0 &&
cursor->columns[i].col_retcode != 1406) {
/* So error fetching column. The most common is 1405, a NULL */
/* was retreived. 1406 is ASCII or string buffer data was */
/* truncated. The converted data from the database did not fit */
/* into the buffer. Since we allocated the buffer to be large */
/* enough, this should not occur. Anyway, we probably want to */
/* return what we did get, in that case */
RETURN_FALSE;
} else {
#if PHP_API_VERSION >= 19990421
MAKE_STD_ZVAL(tmp);
#endif
tmp->type = IS_STRING;
tmp->value.str.len = 0;
switch(cursor->columns[i].dbtype) {
case SQLT_LNG:
case SQLT_LBI:
{
ub4 ret_len;
int offset = cursor->columns[i].col_retlen;
sb2 result;
if (cursor->columns[i].col_retcode == 1406) { /* truncation -> get the rest! */
while (1) {
cursor->columns[i].buf = erealloc(cursor->columns[i].buf,offset + DB_SIZE + 1);
if (! cursor->columns[i].buf) {
offset = 0;
break;
}
result = oflng(&cursor->cda,
(sword)(i + 1),
cursor->columns[i].buf + offset,
DB_SIZE,
1,
&ret_len,
offset);
if (result) {
break;
}
if (ret_len <= 0) {
break;
}
offset += ret_len;
}
}
if (cursor->columns[i].buf && offset) {
tmp->value.str.len = offset;
} else {
tmp->value.str.len = 0;
}
}
break;
default:
tmp->value.str.len = min(cursor->columns[i].col_retlen,
cursor->columns[i].dsize);
break;
}
tmp->value.str.val = estrndup(cursor->columns[i].buf,tmp->value.str.len);
}
if (flags&ORA_FETCHINTO_ASSOC){
#if PHP_API_VERSION >= 19990421
_php3_hash_update(arr->value.ht, cursor->columns[i].cbuf, cursor->columns[i].cbufl+1, (void *) &tmp, sizeof(pval*), NULL);
#else
_php3_hash_update(arr->value.ht, cursor->columns[i].cbuf, cursor->columns[i].cbufl+1, (void *) tmp, sizeof(pval), NULL);
#endif
} else {
#if PHP_API_VERSION >= 19990421
_php3_hash_index_update(arr->value.ht, i, (void *) &tmp, sizeof(pval*), NULL);
#else
_php3_hash_index_update(arr->value.ht, i, (void *) tmp, sizeof(pval), NULL);
#endif
}
}
#if PHP_API_VERSION < 19990421
efree(tmp);
#endif
RETURN_LONG(cursor->ncols);
}
/* }}} */
/* {{{ proto string ora_columnname(int cursor, int column)
Get the name of an Oracle result column */
void php3_Ora_ColumnName(INTERNAL_FUNCTION_PARAMETERS)
{ /* cursor_index, column_index */
pval *argv[2];
int cursor_ind;
oraCursor *cursor = NULL;
if (ARG_COUNT(ht) != 2 || getParametersArray(ht, 2, argv) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(argv[0]);
cursor_ind = argv[0]->value.lval;
/* Find the cursor */
if ((cursor = ora_get_cursor(list, cursor_ind)) == NULL) {
RETURN_FALSE;
}
convert_to_long(argv[1]);
if (cursor->ncols == 0){
php3_error(E_WARNING, "No tuples available at this cursor index");
RETURN_FALSE;
}
if (argv[1]->value.lval >= cursor->ncols){
php3_error(E_WARNING, "Column index larger than number of columns");
RETURN_FALSE;
}
if (argv[1]->value.lval < 0){
php3_error(E_WARNING, "Column numbering starts at 0");
RETURN_FALSE;
}
RETURN_STRINGL(cursor->columns[argv[1]->value.lval].cbuf,
cursor->columns[argv[1]->value.lval].cbufl,1);
}
/* }}} */
/* {{{ proto string ora_columntype(int cursor, int column)
Get the type of an Oracle result column */
void php3_Ora_ColumnType(INTERNAL_FUNCTION_PARAMETERS)
{ /* cursor_index, column_index */
pval *argv[2];
int cursor_ind, colno;
oraCursor *cursor = NULL;
if (ARG_COUNT(ht) != 2 || getParametersArray(ht, 2, argv) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(argv[0]);
/* don't convert the column index yet, it might be the column name */
cursor_ind = argv[0]->value.lval;
/* Find the cursor */
if ((cursor = ora_get_cursor(list, cursor_ind)) == NULL) {
RETURN_FALSE;
}
convert_to_long(argv[1]);
colno = argv[1]->value.lval;
if (cursor->ncols == 0){
php3_error(E_WARNING, "No tuples available at this cursor index");
RETURN_FALSE;
}
if (colno >= cursor->ncols){
php3_error(E_WARNING, "Column index larger than number of columns");
RETURN_FALSE;
}
if (colno < 0){
php3_error(E_WARNING, "Column numbering starts at 0");
RETURN_FALSE;
}
switch (cursor->columns[colno].dbtype) {
case SQLT_CHR:
RETURN_STRINGL("VARCHAR2", 8, 1);
case SQLT_VCS:
case SQLT_AVC:
RETURN_STRINGL("VARCHAR", 7, 1);
case SQLT_STR:
case SQLT_AFC:
RETURN_STRINGL("CHAR", 4, 1);
case SQLT_NUM: case SQLT_INT:
case SQLT_FLT: case SQLT_UIN:
RETURN_STRINGL("NUMBER", 6, 1);
case SQLT_LNG:
RETURN_STRINGL("LONG", 4, 1);
case SQLT_LBI:
RETURN_STRINGL("LONG RAW", 8, 1);
case SQLT_RID:
RETURN_STRINGL("ROWID", 5, 1);
case SQLT_DAT:
RETURN_STRINGL("DATE", 4, 1);
#ifdef SQLT_CUR
case SQLT_CUR:
RETURN_STRINGL("CURSOR", 6, 1);
#endif
default:
{
char numbuf[21];
snprintf(numbuf, 20, "UNKNOWN(%d)", cursor->columns[colno].dbtype);
numbuf[20] = '\0';
RETVAL_STRING(numbuf,1);
}
}
}
/* }}} */
/* {{{ proto int ora_columnsize(int cursor, int column)
Return the size of the column */
void php3_Ora_ColumnSize(INTERNAL_FUNCTION_PARAMETERS)
{ /* cursor_index, column_index */
pval *argv[2];
int cursor_ind;
oraCursor *cursor = NULL;
if (ARG_COUNT(ht) != 2 || getParametersArray(ht, 2, argv) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(argv[0]);
cursor_ind = argv[0]->value.lval;
/* Find the cursor */
if ((cursor = ora_get_cursor(list, cursor_ind)) == NULL) {
RETURN_FALSE;
}
convert_to_long(argv[1]);
if (cursor->ncols == 0){
php3_error(E_WARNING, "No tuples available at this cursor index");
RETURN_FALSE;
}
if (argv[1]->value.lval >= cursor->ncols){
php3_error(E_WARNING, "Column index larger than number of columns");
RETURN_FALSE;
}
if (argv[1]->value.lval < 0){
php3_error(E_WARNING, "Column numbering starts at 0");
RETURN_FALSE;
}
RETURN_LONG(cursor->columns[argv[1]->value.lval].dbsize);
}
/* }}} */
/* {{{ proto mixed ora_getcolumn(int cursor, int column)
Get data from a fetched row */
void php3_Ora_GetColumn(INTERNAL_FUNCTION_PARAMETERS)
{ /* cursor_index, column_index */
pval *argv[2];
int colno;
oraCursor *cursor = NULL;
oraColumn *column = NULL;
sb2 type;
if (ARG_COUNT(ht) != 2 || getParametersArray(ht, 2, argv) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(argv[0]);
/* Find the cursor */
if ((cursor = ora_get_cursor(list, argv[0]->value.lval)) == NULL) {
RETURN_FALSE;
}
if (cursor->ncols == 0){
php3_error(E_WARNING, "No tuples available at this cursor index");
RETURN_FALSE;
}
convert_to_long(argv[1]);
colno = argv[1]->value.lval;
if (colno >= cursor->ncols){
php3_error(E_WARNING, "Column index larger than number of columns");
RETURN_FALSE;
}
if (colno < 0){
php3_error(E_WARNING, "Column numbering starts at 0");
RETURN_FALSE;
}
if (cursor->fetched == 0){
if (ofetch(&cursor->cda)) {
if (cursor->cda.rc != NO_DATA_FOUND) {
php3_error(E_WARNING, "Ora_Fetch failed (%s)",
ora_error(&cursor->cda));
}
RETURN_FALSE;
}
cursor->fetched++;
}
column = &cursor->columns[colno];
type = column->dbtype;
if (column->col_retcode != 0 && column->col_retcode != 1406) {
/* So error fetching column. The most common is 1405, a NULL
* was retreived. 1406 is ASCII or string buffer data was
* truncated. The converted data from the database did not fit
* into the buffer. Since we allocated the buffer to be large
* enough, this should not occur. Anyway, we probably want to
* return what we did get, in that case
*/
RETURN_FALSE;
} else {
switch(type)
{
case SQLT_CHR:
case SQLT_NUM:
case SQLT_INT:
case SQLT_FLT:
case SQLT_STR:
case SQLT_UIN:
case SQLT_AFC:
case SQLT_AVC:
case SQLT_DAT:
RETURN_STRINGL(column->buf, min(column->col_retlen, column->dsize), 1);
case SQLT_LNG:
case SQLT_LBI:
#if 0
{
ub4 ret_len;
/* XXX 64k max for LONG and LONG RAW */
oflng(&cursor->cda, (sword)(colno + 1), column->buf, DB_SIZE, 1,
&ret_len, 0);
RETURN_STRINGL(column->buf, ret_len, 1);
}
#else
{
ub4 ret_len;
int offset = column->col_retlen;
sb2 result;
if (column->col_retcode == 1406) { /* truncation -> get the rest! */
while (1) {
column->buf = erealloc(column->buf,offset + DB_SIZE + 1);
if (! column->buf) {
offset = 0;
break;
}
result = oflng(&cursor->cda,
(sword)(colno + 1),
column->buf + offset,
DB_SIZE,
1,
&ret_len,
offset);
if (result) {
break;
}
if (ret_len <= 0) {
break;
}
offset += ret_len;
}
}
if (column->buf && offset) {
RETURN_STRINGL(column->buf, offset, 1);
} else {
RETURN_FALSE;
}
}
#endif
default:
php3_error(E_WARNING,
"Ora_GetColumn found invalid type (%d)", type);
RETURN_FALSE;
}
}
}
/* }}} */
/* {{{ proto string ora_error(int cursor_or_connection)
Get an Oracle error message */
void php3_Ora_Error(INTERNAL_FUNCTION_PARAMETERS)
{
pval *arg;
oraCursor *cursor;
oraConnection *conn;
int argc = ARG_COUNT(ht);
if (argc < 0 || argc > 1 || getParametersArray(ht, argc, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
if (argc == 1) {
convert_to_long(arg);
if ((cursor = ora_get_cursor(list, arg->value.lval)) != NULL) {
return_value->type = IS_STRING;
return_value->value.str.val = estrdup(ora_error(&cursor->cda));
return_value->value.str.len = strlen(return_value->value.str.val);
} else if ((conn = ora_get_conn(list,plist, arg->value.lval)) != NULL) {
return_value->type = IS_STRING;
return_value->value.str.val = estrdup(ora_error(&conn->lda));
return_value->value.str.len = strlen(return_value->value.str.val);
}
} else {
return_value->type = IS_STRING;
return_value->value.str.val = estrdup(ora_error(&db_err_conn.lda));
return_value->value.str.len = strlen(return_value->value.str.val);
}
}
/* }}} */
/* {{{ proto int ora_errorcode(int cursor_or_connection)
Get an Oracle error code */
void php3_Ora_ErrorCode(INTERNAL_FUNCTION_PARAMETERS)
{
pval *arg;
oraCursor *cursor;
oraConnection *conn;
int argc = ARG_COUNT(ht);
if (argc < 0 || argc > 1 || getParametersArray(ht, argc, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
if (argc == 1) {
convert_to_long(arg);
if ((cursor = ora_get_cursor(list, arg->value.lval)) != NULL) {
RETURN_LONG(cursor->cda.rc);
} else if ((conn = ora_get_conn(list,plist, arg->value.lval)) != NULL) {
RETURN_LONG(conn->lda.rc);
}
} else {
RETURN_LONG(db_err_conn.lda.rc);
}
}
/* }}} */
void php3_info_oracle(ZEND_MODULE_INFO_FUNC_ARGS)
{
#if !(WIN32|WINNT)
php3_printf("Oracle version: %s<br>\n"
"Compile-time ORACLE_HOME: %s<br>\n"
"Libraries used: %s",
PHP_ORACLE_VERSION, PHP_ORACLE_HOME, PHP_ORACLE_LIBS);
#endif
}
/*
** Functions internal to this module.
*/
static oraConnection *
ora_get_conn(HashTable *list,HashTable *plist,int ind)
{
oraConnection *conn = NULL;
int type;
ORACLE_TLS_VARS;
conn = (oraConnection *)php3_list_find(ind, &type);
if (conn && type == ORACLE_GLOBAL(php3_oracle_module).le_conn)
return conn;
conn = (oraConnection *)php3_plist_find(ind, &type);
if (conn && type == ORACLE_GLOBAL(php3_oracle_module).le_pconn)
return conn;
php3_error(E_WARNING,"Bad Oracle connection number (%d)", ind);
return NULL;
}
int ora_add_cursor(HashTable *list, oraCursor *cursor)
{
ORACLE_TLS_VARS;
return php3_list_insert(cursor, ORACLE_GLOBAL(php3_oracle_module).le_cursor);
}
static oraCursor *
ora_get_cursor(HashTable *list, int ind)
{
oraCursor *cursor;
oraConnection *db_conn;
int type;
ORACLE_TLS_VARS;
cursor = php3_list_find(ind, &type);
if (!cursor || type != ORACLE_GLOBAL(php3_oracle_module).le_cursor) {
php3_error(E_WARNING, "Invalid cursor index %d", ind);
return NULL;
}
if (_php3_hash_find(ORACLE_GLOBAL(php3_oracle_module).conns,(void*)&(cursor->conn_ptr),sizeof(void*),(void **)&db_conn) == FAILURE) {
php3_error(E_WARNING, "Connection already closed for cursor index %d", ind);
return NULL;
}
return cursor;
}
void ora_del_cursor(HashTable *list, int ind)
{
oraCursor *cursor;
int type;
ORACLE_TLS_VARS;
cursor = (oraCursor *) php3_list_find(ind, &type);
if (!cursor || type != ORACLE_GLOBAL(php3_oracle_module).le_cursor) {
php3_error(E_WARNING,"Can't find cursor %d",ind);
return;
}
php3_list_delete(ind);
}
static char *
ora_error(Cda_Def * cda)
{
sword n, l;
static text errmsg[ 512 ];
n = oerhms(cda, cda->rc, errmsg, 400);
/* remove the last newline */
l = strlen(errmsg);
if (l < 400 && errmsg[l - 1] == '\n') {
errmsg[l - 1] = '\0';
l--;
}
if (cda->fc > 0) {
strcat(errmsg, " -- while processing OCI function ");
strncat(errmsg, ora_func_tab[cda->fc], 75); /* 512 - 400 - 36 */
}
return (char *) errmsg;
}
static sword
ora_describe_define(oraCursor * cursor)
{
long col = 0;
int i;
sb2 type;
sb4 dbsize;
if (cursor == NULL) {
return -1;
}
if (cursor->columns) {
for(i = 0; i < cursor->ncols; i++){
if (cursor->columns[i].buf)
efree(cursor->columns[i].buf);
}
efree(cursor->columns);
}
cursor->ncols = 0;
while(1){
if (odescr(&cursor->cda, (sword) cursor->ncols + 1, &dbsize, (sb2 *)0, (sb1 *)0,
(sb4 *)0, (sb4 *)0, (sb2 *)0, (sb2 *)0, (sb2 *)0)){
if (cursor->cda.rc == VAR_NOT_IN_LIST) {
break;
} else {
php3_error(E_WARNING, "%s", ora_error(&cursor->cda));
cursor->ncols = 0;
return -1;
}
}
cursor->ncols++;
}
if (cursor->ncols > 0){
cursor->columns = (oraColumn *) emalloc(sizeof(oraColumn) * cursor->ncols);
if (cursor->columns == NULL){
php3_error(E_WARNING, "Out of memory");
return -1;
}
}
for(col = 0; col < cursor->ncols; col++){
memset(&cursor->columns[col], 0, sizeof(oraColumn));
cursor->columns[col].cbufl = ORANAMELEN;
if (odescr(&cursor->cda, (sword)col + 1, &cursor->columns[col].dbsize,
&cursor->columns[col].dbtype, &cursor->columns[col].cbuf[0],
&cursor->columns[col].cbufl, &cursor->columns[col].dsize,
&cursor->columns[col].prec, &cursor->columns[col].scale,
&cursor->columns[col].nullok)) {
if (cursor->cda.rc == VAR_NOT_IN_LIST) {
break;
} else {
php3_error(E_WARNING, "%s", ora_error(&cursor->cda));
return -1;
}
}
cursor->columns[col].cbuf[cursor->columns[col].cbufl] = '\0';
switch (cursor->columns[col].dbtype) {
case SQLT_LBI:
cursor->columns[col].dsize = DB_SIZE;
type = SQLT_LBI;
break;
case SQLT_LNG:
cursor->columns[col].dsize = DB_SIZE;
default:
type = SQLT_STR;
break;
}
if ((cursor->columns[col].buf = (ub1 *) emalloc(cursor->columns[col].dsize + 1)) == NULL){
php3_error(E_WARNING, "Out of memory");
return -1;
}
/* Define an output variable for the column */
if (odefin(&cursor->cda, (sword)col + 1, cursor->columns[col].buf,
cursor->columns[col].dsize + 1, type, -1, &cursor->columns[col].indp,
(text *) 0, -1, -1, &cursor->columns[col].col_retlen,
&cursor->columns[col].col_retcode)) {
php3_error(E_WARNING, "%s", ora_error(&cursor->cda));
return -1;
}
}
return 1;
}
int ora_set_param_values(oraCursor *cursor, int isout)
{
char *paramname;
oraParam *param;
#if PHP_API_VERSION < 19990421
pval *pdata;
#else
pval **pdata;
#endif
int i, len, plen;
#if (WIN32|WINNT)
/* see variables.c */
HashTable *symbol_table=php3i_get_symbol_table();
#endif
_php3_hash_internal_pointer_reset(cursor->params);
if(_php3_hash_num_elements(cursor->params) != cursor->nparams){
php3_error(E_WARNING, "Mismatch in number of parameters");
return 0;
}
for(i = 0; i < cursor->nparams; i++, _php3_hash_move_forward(cursor->params)){
if(_php3_hash_get_current_key(cursor->params, ¶mname, NULL) != HASH_KEY_IS_STRING){
php3_error(E_WARNING, "Can't get parameter name");
return 0;
}
if(_php3_hash_get_current_data(cursor->params, (void **)¶m) == FAILURE){
php3_error(E_WARNING, "Can't get parameter data");
efree(paramname);
return 0;
}
if(isout){
#if (WIN32|WINNT)
/* see oracle_hack.c */
{
pval var;
char *name=(paramname);
var.value.str.val = estrdup(param->progv);
var.value.str.len = strlen(param->progv);
var.type = IS_STRING;
_php3_hash_update(symbol_table, name, strlen(name)+1, &var, sizeof(pval),NULL);
}
#else
SET_VAR_STRINGL(paramname, estrdup(param->progv), strlen(param->progv));
#endif
efree(paramname);
continue;
}
/* doing the in-loop */
/* FIXME Globals don't work in extensions on windows, have to do something
else here. See oracle_hack.c */
#if (WIN32|WINNT)
if(_php3_hash_find(symbol_table, paramname, strlen(paramname) + 1, (void **)&pdata) == FAILURE){
php3_error(E_WARNING, "Can't find variable for parameter");
efree(paramname);
return 0;
}
#else
#if PHP_API_VERSION < 19990421
if(_php3_hash_find(&GLOBAL(symbol_table), paramname, strlen(paramname) + 1, (void **)&pdata) == FAILURE){
#else
if(_php3_hash_find(&EG(symbol_table), paramname, strlen(paramname) + 1, (void **)&pdata) == FAILURE){
#endif
php3_error(E_WARNING, "Can't find variable for parameter");
efree(paramname);
return 0;
}
#endif
#if PHP_API_VERSION < 19990421
convert_to_string(pdata);
plen = pdata->value.str.len;
#else
convert_to_string(*pdata);
plen = (*pdata)->value.str.len;
#endif
if (param->progvl <= plen){
php3_error(E_NOTICE, "Input value will be truncated");
}
len = min(param->progvl - 1, plen);
#if PHP_API_VERSION < 19990421
strncpy(param->progv, pdata->value.str.val, len);
#else
strncpy(param->progv, (*pdata)->value.str.val, len);
#endif
param->progv[len] = '\0';
efree(paramname);
}
return 1;
}
#endif /* HAVE_ORACLE */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
*/
|