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 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667
|
/*
+----------------------------------------------------------------------+
| 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: Jouni Ahto <jah@mork.net> |
| Andrew Avdeev <andy@rsc.mv.ru> |
+----------------------------------------------------------------------+
*/
/* $Id: interbase.c,v 1.32 2000/06/11 16:21:43 eschmid Exp $ */
/* TODO: Arrays, roles?
A lot... */
/*
Changes:
1999-09-21: Ivo Panacek <ivop@regionet.cz>
- added COMPILE_DL section
- more verbose php3_info_ibase function
mostly stolen from pgsql.c for now
1999-10-05: Ivo Panacek <ivop@regionet.cz>
- safe rinit/rfinish: check for NULL so
rfinish could be called repeatedly
emalloc & co. replaced with malloc & co.
2000-2-20: Nicolas Moreau <nicolas@enttec.com>
- Modify to compile and create a Windows DLL
*/
#ifndef MSVC5
#include "config.h"
#endif
#if MSVC5
#include <windows.h>
#endif
#include "php.h"
#include "internal_functions.h"
#include "php3_interbase.h"
#if COMPILE_DL
# include "dl/phpdl.h"
#endif
#if HAVE_IBASE
#include <ibase.h>
#include <time.h>
#include "php3_list.h"
#include "php3_string.h"
#include "fsock.h"
#include "head.h"
#define SAFE_STRING(s) ((s)?(s):"")
/* {{{ extension definition structures */
function_entry ibase_functions[] = {
PHP_FE(ibase_connect, NULL)
PHP_FE(ibase_pconnect, NULL)
PHP_FE(ibase_close, NULL)
PHP_FE(ibase_query, NULL)
PHP_FE(ibase_fetch_row, NULL)
PHP_FE(ibase_fetch_object, NULL)
PHP_FE(ibase_free_result, NULL)
PHP_FE(ibase_prepare, NULL)
PHP_FE(ibase_execute, NULL)
PHP_FE(ibase_free_query, NULL)
PHP_FE(ibase_timefmt, NULL)
PHP_FE(ibase_num_fields, NULL)
PHP_FE(ibase_field_info, NULL)
PHP_FE(ibase_trans, NULL)
PHP_FE(ibase_commit, NULL)
PHP_FE(ibase_rollback, NULL)
PHP_FE(ibase_blob_info, NULL)
PHP_FE(ibase_blob_create, NULL)
PHP_FE(ibase_blob_add, NULL)
PHP_FE(ibase_blob_cancel, NULL)
PHP_FE(ibase_blob_close, NULL)
PHP_FE(ibase_blob_open, NULL)
PHP_FE(ibase_blob_get, NULL)
PHP_FE(ibase_blob_echo, NULL)
#ifndef MSVC5
PHP_FE(ibase_blob_import, NULL)
#endif
PHP_FE(ibase_errmsg, NULL)
{NULL, NULL, NULL}
};
php3_module_entry ibase_module_entry =
{
"InterBase",
ibase_functions,
php3_minit_ibase,
php3_mfinish_ibase,
php3_rinit_ibase,
php3_rfinish_ibase,
php3_info_ibase,
STANDARD_MODULE_PROPERTIES
};
#if COMPILE_DL
#include "dl/phpdl.h"
DLEXPORT php3_module_entry *get_module() { return &ibase_module_entry; }
#define DL_MALLOC(size) malloc(size)
#define DL_STRDUP(str) strdup(str)
#define DL_FREE(ptr) free(ptr)
#else
#define DL_MALLOC(size) emalloc(size)
#define DL_STRDUP(str) estrdup(str)
#define DL_FREE(ptr) efree(ptr)
#endif
/* }}} */
/* {{{ internal macros and structures */
#define IB_STATUS (IBASE_GLOBAL(php3_ibase_module).status)
/* db_handle and transaction handle keep in one variable
link = db_handle * IBASE_TRANS_ON_LINK + trans_handle
*/
/* get link and transaction from long argument
*/
#define GET_LINK_TRANS(link_id, ib_link, trans_n) { \
int type; \
trans_n = link_id % IBASE_TRANS_ON_LINK; \
link_id /= IBASE_TRANS_ON_LINK; \
ib_link = (ibase_db_link *) php3_list_find(link_id, &type); \
if (type!=IBASE_GLOBAL(php3_ibase_module).le_link && type!=IBASE_GLOBAL(php3_ibase_module).le_plink) { \
_php3_ibase_module_error("%d is not link or transaction index",link_id); \
RETURN_FALSE; \
}}
/* get query */
#define GET_QUERY(query_id, ib_query) { \
int type; \
ib_query = (ibase_query *) php3_list_find(query_id, &type); \
if (type!=IBASE_GLOBAL(php3_ibase_module).le_query) { \
_php3_ibase_module_error("%d is not query index",query_id); \
RETURN_FALSE; \
}}
/* get result */
#define GET_RESULT(result_id, ib_result) { \
int type; \
ib_result = (ibase_result *) php3_list_find(result_id, &type); \
if (type!=IBASE_GLOBAL(php3_ibase_module).le_result) { \
_php3_ibase_module_error("%d is not result index",result_id); \
RETURN_FALSE; \
}}
#define RESET_ERRMSG { IBASE_GLOBAL(php3_ibase_module).errmsg[0] = '\0';}
#define TEST_ERRMSG ( IBASE_GLOBAL(php3_ibase_module).errmsg[0] != '\0')
/* sql variables union
used for convert and binding input variables
*/
typedef struct{
union{
short sval;
float fval;
ISC_QUAD qval;
}val;
short sqlind;
}BIND_BUF;
/* get blob identifier from argument
on empty unset argumnet ib_blob set to NULL
*/
#define GET_BLOB_ID_ARG(blob_arg, ib_blob)\
{\
if(blob_arg->type == IS_STRING && blob_arg->value.str.len == 0){\
ib_blob = NULL;\
}else if(blob_arg->type != IS_STRING\
|| blob_arg->value.str.len != sizeof(ibase_blob_handle)\
|| ((ibase_blob_handle *)(blob_arg->value.str.val))->bl_handle != 0){\
_php3_ibase_module_error("invalid blob id");\
RETURN_FALSE;\
}else{\
ib_blob = (ibase_blob_handle *)blob_arg->value.str.val;\
}\
}
/* get blob handle from argument
note: blob already open when handle active
*/
#define GET_BLOB_HANDLE_ARG(blob_arg, blob_ptr) \
{ \
int type; \
convert_to_long(blob_arg); \
blob_ptr = (ibase_blob_handle *) php3_list_find(blob_arg->value.lval, &type); \
if (type!=IBASE_GLOBAL(php3_ibase_module).le_blob) { \
_php3_ibase_module_error("%d is not blob handle",blob_arg->value.lval); \
RETURN_FALSE; \
} \
}
/* blob information struct */
typedef struct {
ISC_LONG max_segment; /* Length of longest segment */
ISC_LONG num_segments; /* Total number of segments */
ISC_LONG total_length; /* Total length of blob */
int bl_stream; /* blob is stream ? */
}IBASE_BLOBINFO;
/* }}} */
#define IBASE_GLOBAL(a) a
#define IBASE_TLS_VARS
ibase_module php3_ibase_module;
/* error handling ---------------------------- */
/* {{{ proto string ibase_errmsg(void)
Return error message */
void php3_ibase_errmsg(INTERNAL_FUNCTION_PARAMETERS)
{
char *errmsg = IBASE_GLOBAL(php3_ibase_module).errmsg;
IBASE_TLS_VARS;
if(errmsg[0]){
RETURN_STRING(errmsg,1);
}
RETURN_FALSE;
}
/* }}} */
/* {{{ _php3_ibase_error()
print interbase error and save it for ibase_errmsg() */
static void _php3_ibase_error(void)
{
char *s, *errmsg = IBASE_GLOBAL(php3_ibase_module).errmsg;
ISC_STATUS *statusp = IB_STATUS;
s = errmsg;
while((s - errmsg) < MAX_ERRMSG - (IBASE_MSGSIZE + 2) && isc_interprete(s, &statusp)){
strcat(errmsg, " ");
s = errmsg + strlen(errmsg);
}
php3_error(E_WARNING, "InterBase: %s",errmsg);
}
/* }}} */
/* {{{ _php3_ibase_module_error()
print php interbase module error and save it for ibase_errmsg() */
static void _php3_ibase_module_error(char *msg, ...)
{
char *errmsg = IBASE_GLOBAL(php3_ibase_module).errmsg;
va_list ap;
int len;
va_start(ap, msg);
len = vsnprintf(errmsg, MAX_ERRMSG - 1, msg, ap);
va_end(ap);
errmsg[len] = '\0';
php3_error(E_WARNING, "InterBase module: %s",errmsg);
}
/* }}} */
/* destructors ---------------------- */
/* {{{ _php3_ibase_free_xsqlda() */
/* not actual destructor ... */
static void _php3_ibase_free_xsqlda(XSQLDA *sqlda)
{
int i;
XSQLVAR *var;
if(sqlda){
var = sqlda->sqlvar;
for (i = 0; i < sqlda->sqld; i++, var++) {
efree(var->sqldata);
if(var->sqlind)
efree(var->sqlind);
}
efree(sqlda);
}
}
/* }}} */
/* {{{ _php3_ibase_commit_link() */
static void _php3_ibase_commit_link(ibase_db_link *link)
{
int i;
if(link->trans[0] != NULL){ /* commit default */
if(isc_commit_transaction(IB_STATUS, &link->trans[0])){
_php3_ibase_error();
}
link->trans[0] = NULL;
}
for(i = 1; i < IBASE_TRANS_ON_LINK; i++){
if(link->trans[i] != NULL){
if(isc_rollback_transaction(IB_STATUS, &link->trans[i])){
_php3_ibase_error();
}
link->trans[i] = NULL;
}
}
}
/* }}} */
/* {{{ _php3_ibase_close_link() */
static void _php3_ibase_close_link(ibase_db_link *link)
{
_php3_ibase_commit_link(link);
isc_detach_database(IB_STATUS, &link->link);
IBASE_GLOBAL(php3_ibase_module).num_links--;
efree(link);
}
/* }}} */
/* {{{ _php3_ibase_close_plink() */
static void _php3_ibase_close_plink(ibase_db_link *link)
{
_php3_ibase_commit_link(link);
isc_detach_database(IB_STATUS, &link->link);
IBASE_GLOBAL(php3_ibase_module).num_persistent--;
IBASE_GLOBAL(php3_ibase_module).num_links--;
free(link);
}
/* }}} */
/* {{{ _php3_ibase_free_result() */
static void _php3_ibase_free_result(ibase_result *ib_result)
{
if (ib_result){
_php3_ibase_free_xsqlda(ib_result->out_sqlda);
if(ib_result->drop_stmt && ib_result->stmt){
if(isc_dsql_free_statement(IB_STATUS, &ib_result->stmt, DSQL_drop)){
_php3_ibase_error();
}
}else{
if(isc_dsql_free_statement(IB_STATUS, &ib_result->stmt, DSQL_close)){
_php3_ibase_error();
}
}
if(ib_result->out_array){
efree(ib_result->out_array);
}
efree(ib_result);
}
}
/* }}} */
/* {{{ _php3_ibase_free_query() */
static void _php3_ibase_free_query(ibase_query *ib_query)
{
if(ib_query){
if (ib_query->in_sqlda) {
efree(ib_query->in_sqlda);
}
if (ib_query->out_sqlda) {
efree(ib_query->out_sqlda);
}
if(ib_query->stmt){
if(isc_dsql_free_statement(IB_STATUS, &ib_query->stmt, DSQL_drop)){
_php3_ibase_error();
}
}
if(ib_query->in_array){
efree(ib_query->in_array);
}
if(ib_query->out_array){
efree(ib_query->out_array);
}
efree(ib_query);
}
}
/* }}} */
/* {{{ _php3_ibase_free_blob() */
static void _php3_ibase_free_blob(ibase_blob_handle *ib_blob)
{
if(ib_blob->bl_handle != NULL){ /* blob open*/
if(isc_cancel_blob(IB_STATUS, &ib_blob->bl_handle)){
_php3_ibase_error();
}
}
efree(ib_blob);
}
/* }}} */
/* {{{ startup, shutdown and info functions */
int php3_minit_ibase(INIT_FUNC_ARGS)
{
IBASE_TLS_VARS;
if (cfg_get_long("ibase.allow_persistent", &IBASE_GLOBAL(php3_ibase_module).allow_persistent) == FAILURE) {
IBASE_GLOBAL(php3_ibase_module).allow_persistent = 1;
}
if (cfg_get_long("ibase.max_persistent", &IBASE_GLOBAL(php3_ibase_module).max_persistent) == FAILURE) {
IBASE_GLOBAL(php3_ibase_module).max_persistent = -1;
}
if (cfg_get_long("ibase.max_links", &IBASE_GLOBAL(php3_ibase_module).max_links) == FAILURE) {
IBASE_GLOBAL(php3_ibase_module).max_links = -1;
}
if (cfg_get_string("ibase.default_user", &IBASE_GLOBAL(php3_ibase_module).default_user) == FAILURE
|| IBASE_GLOBAL(php3_ibase_module).default_user[0] == 0) {
IBASE_GLOBAL(php3_ibase_module).default_user = "";
}
if (cfg_get_string("ibase.default_password", &IBASE_GLOBAL(php3_ibase_module).default_password) == FAILURE
|| IBASE_GLOBAL(php3_ibase_module).default_password[0] == 0) {
IBASE_GLOBAL(php3_ibase_module).default_password = "";
}
if (cfg_get_string("ibase.timeformat", &IBASE_GLOBAL(php3_ibase_module).timeformat) == FAILURE) {
IBASE_GLOBAL(php3_ibase_module).cfg_timeformat = "%m/%d/%Y %H:%M:%S";
}
IBASE_GLOBAL(php3_ibase_module).timeformat = NULL;
IBASE_GLOBAL(php3_ibase_module).errmsg = NULL;
IBASE_GLOBAL(php3_ibase_module).num_persistent = 0;
IBASE_GLOBAL(php3_ibase_module).le_result = register_list_destructors(_php3_ibase_free_result, NULL);
IBASE_GLOBAL(php3_ibase_module).le_query = register_list_destructors(_php3_ibase_free_query, NULL);
IBASE_GLOBAL(php3_ibase_module).le_blob = register_list_destructors(_php3_ibase_free_blob, NULL);
IBASE_GLOBAL(php3_ibase_module).le_link = register_list_destructors(_php3_ibase_close_link, NULL);
IBASE_GLOBAL(php3_ibase_module).le_plink = register_list_destructors(_php3_ibase_commit_link, _php3_ibase_close_plink);
REGISTER_LONG_CONSTANT("IBASE_DEFAULT", PHP3_IBASE_DEFAULT, CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IBASE_TEXT", PHP3_IBASE_TEXT, CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IBASE_TIMESTAMP", PHP3_IBASE_TIMESTAMP, CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IBASE_READ", PHP3_IBASE_READ, CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IBASE_COMMITED", PHP3_IBASE_COMMITED, CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IBASE_CONSISTENCY", PHP3_IBASE_CONSISTENCY, CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IBASE_NOWAIT", PHP3_IBASE_NOWAIT, CONST_PERSISTENT);
return SUCCESS;
}
int php3_rinit_ibase(INIT_FUNC_ARGS)
{
IBASE_TLS_VARS;
IBASE_GLOBAL(php3_ibase_module).default_link= -1;
IBASE_GLOBAL(php3_ibase_module).num_links = IBASE_GLOBAL(php3_ibase_module).num_persistent;
if(IBASE_GLOBAL(php3_ibase_module).timeformat)
DL_FREE(IBASE_GLOBAL(php3_ibase_module).timeformat);
IBASE_GLOBAL(php3_ibase_module).timeformat = DL_STRDUP(IBASE_GLOBAL(php3_ibase_module).cfg_timeformat);
if(IBASE_GLOBAL(php3_ibase_module).errmsg)
DL_FREE(IBASE_GLOBAL(php3_ibase_module).errmsg);
IBASE_GLOBAL(php3_ibase_module).errmsg = DL_MALLOC(sizeof(char)*MAX_ERRMSG+1);
return SUCCESS;
}
int php3_mfinish_ibase(void)
{
return SUCCESS;
}
int php3_rfinish_ibase(void)
{
IBASE_TLS_VARS;
if(IBASE_GLOBAL(php3_ibase_module).timeformat)
DL_FREE(IBASE_GLOBAL(php3_ibase_module).timeformat);
IBASE_GLOBAL(php3_ibase_module).timeformat = NULL;
if(IBASE_GLOBAL(php3_ibase_module).errmsg)
DL_FREE(IBASE_GLOBAL(php3_ibase_module).errmsg);
IBASE_GLOBAL(php3_ibase_module).errmsg = NULL;
return SUCCESS;
}
void php3_info_ibase(void)
{
IBASE_TLS_VARS;
php3_printf(
"<table>"
"<tr><td>Revision:</td><td>$Revision: 1.32 $</td></tr>\n"
#ifdef COMPILE_DL
"<tr><td>Dynamic module:</td><td>Yes</td></tr>\n"
#endif
"<tr><td>Allow persistent links:</td><td>%s</td></tr>\n"
"<tr><td>Persistent links:</td><td>%d/",
(IBASE_GLOBAL(php3_ibase_module).allow_persistent?"Yes":"No"),
IBASE_GLOBAL(php3_ibase_module).num_persistent);
if (IBASE_GLOBAL(php3_ibase_module).max_persistent == -1) {
php3_printf("Unlimited");
} else {
php3_printf("%ld",IBASE_GLOBAL(php3_ibase_module).max_persistent);
}
php3_printf("</td></tr>\n"
"<tr><td>Total links:</td><td>%d/",
IBASE_GLOBAL(php3_ibase_module).num_links);
if (IBASE_GLOBAL(php3_ibase_module).max_links == -1) {
php3_printf("Unlimited");
} else {
php3_printf("%ld",IBASE_GLOBAL(php3_ibase_module).max_links);
}
php3_printf("</td></tr>\n"
"<tr><td>Time format:</td><td>\"%s\"</td></tr>\n"
"</table>\n",
IBASE_GLOBAL(php3_ibase_module).timeformat);
}
/* }}} */
/* {{{ _php_ibase_attach_db() */
static int _php_ibase_attach_db(char *server, char *uname, char *passwd, char *charset, int buffers, char *role, isc_db_handle *db)
{
char dpb_buffer[256], *dpb, *p;
int dpb_length, len;
dpb = dpb_buffer;
*dpb++ = isc_dpb_version1;
if (uname != NULL && (len = strlen(uname))) {
*dpb++ = isc_dpb_user_name;
*dpb++ = len;
for (p = uname; *p;) {
*dpb++ = *p++;
}
}
if (passwd != NULL && (len = strlen(passwd))) {
*dpb++ = isc_dpb_password;
*dpb++ = strlen(passwd);
for (p = passwd; *p;) {
*dpb++ = *p++;
}
}
if (charset != NULL && (len = strlen(charset))) {
*dpb++ = isc_dpb_lc_ctype;
*dpb++ = strlen(charset);
for (p = charset; *p;) {
*dpb++ = *p++;
}
}
if (buffers) {
*dpb++ = isc_dpb_num_buffers;
*dpb++ = 1;
*dpb++ = buffers;
}
#ifdef isc_dpb_sql_role_name
if (role != NULL && (len = strlen(role))) {
*dpb++ = isc_dpb_sql_role_name;
*dpb++ = strlen(role);
for (p = role; *p;) {
*dpb++ = *p++;
}
}
#endif
dpb_length = dpb - dpb_buffer;
if(isc_attach_database(IB_STATUS, strlen(server), server, db, dpb_length, dpb_buffer)){
_php3_ibase_error();
return FAILURE;
}
return SUCCESS;
}
/* }}} */
/* {{{ _php3_ibase_connect() */
static void _php3_ibase_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
{
pval **args;
char *ib_server = NULL, *ib_uname, *ib_passwd, *ib_charset = NULL;
int i, ib_uname_len, ib_passwd_len;
isc_db_handle db_handle = NULL;
char *hashed_details;
int hashed_details_length = 0;
ibase_db_link *ib_link;
IBASE_TLS_VARS;
RESET_ERRMSG;
ib_uname = IBASE_GLOBAL(php3_ibase_module).default_user;
ib_passwd = IBASE_GLOBAL(php3_ibase_module).default_password;
ib_uname_len = ib_uname ? strlen(ib_uname) : 0;
ib_passwd_len = ib_passwd ? strlen(ib_passwd) : 0;
if(ARG_COUNT(ht) < 1 || ARG_COUNT(ht) > 4){
WRONG_PARAM_COUNT;
}
args = emalloc(sizeof(pval*) * ARG_COUNT(ht));
if (getParametersArray(ht, ARG_COUNT(ht), args) == FAILURE) {
efree(args);
RETURN_FALSE;
}
switch(ARG_COUNT(ht)) {
case 4:
convert_to_string(args[3]);
ib_charset = args[3]->value.str.val;
hashed_details_length += args[3]->value.str.len;
/* fallout */
case 3:
convert_to_string(args[2]);
ib_passwd = args[2]->value.str.val;
hashed_details_length += args[2]->value.str.len;
/* fallout */
case 2:
convert_to_string(args[1]);
ib_uname = args[1]->value.str.val;
hashed_details_length += args[1]->value.str.len;
/* fallout */
case 1:
convert_to_string(args[0]);
ib_server = args[0]->value.str.val;
hashed_details_length += args[0]->value.str.len;
}/* case */
efree(args);
hashed_details = (char *) emalloc(hashed_details_length+strlen("ibase_%s_%s_%s_%s")+1);
sprintf(hashed_details, "ibase_%s_%s_%s_%s", SAFE_STRING(ib_server), SAFE_STRING(ib_uname), SAFE_STRING(ib_passwd), SAFE_STRING(ib_charset));
if (persistent) {
list_entry *le;
int open_new_connection = 1;
if (_php3_hash_find(plist, hashed_details, hashed_details_length+1, (void **) &le)!=FAILURE) {
char tmp_1[] = {isc_info_base_level, isc_info_end};
char tmp_2[8]; /* Enough? Hope so... */
if (le->type != IBASE_GLOBAL(php3_ibase_module).le_plink) {
RETURN_FALSE;
}
/* Check if connection has timed out */
ib_link = (ibase_db_link *) le->ptr;
if (!isc_database_info(IB_STATUS, &ib_link->link, sizeof(tmp_1), tmp_1, sizeof(tmp_2), tmp_2)) {
open_new_connection = 0;
}
}
/* There was no previous connection to use or it has timed out */
if (open_new_connection) {
list_entry new_le;
if (IBASE_GLOBAL(php3_ibase_module).max_links!=-1 && IBASE_GLOBAL(php3_ibase_module).num_links>=IBASE_GLOBAL(php3_ibase_module).max_links) {
_php3_ibase_module_error("Too many open links (%d)", IBASE_GLOBAL(php3_ibase_module).num_links);
efree(hashed_details);
RETURN_FALSE;
}
if (IBASE_GLOBAL(php3_ibase_module).max_persistent!=-1 && IBASE_GLOBAL(php3_ibase_module).num_persistent>=IBASE_GLOBAL(php3_ibase_module).max_persistent) {
_php3_ibase_module_error("Too many open persistent links (%d)", IBASE_GLOBAL(php3_ibase_module).num_persistent);
efree(hashed_details);
RETURN_FALSE;
}
/* create the ib_link */
if (_php_ibase_attach_db(ib_server, ib_uname, ib_passwd, ib_charset, 0, NULL, &db_handle) == FAILURE) {
efree(hashed_details);
RETURN_FALSE;
}
ib_link = (ibase_db_link *) malloc(sizeof(ibase_db_link));
ib_link->link = db_handle;
for(i = 0; i < IBASE_TRANS_ON_LINK; i++)
ib_link->trans[i] = NULL;
/* hash it up */
new_le.type = IBASE_GLOBAL(php3_ibase_module).le_plink;
new_le.ptr = ib_link;
if (_php3_hash_update(plist, hashed_details, hashed_details_length+1, (void *) &new_le, sizeof(list_entry), NULL)==FAILURE) {
efree(hashed_details);
free(ib_link);
RETURN_FALSE;
}
IBASE_GLOBAL(php3_ibase_module).num_links++;
IBASE_GLOBAL(php3_ibase_module).num_persistent++;
}
return_value->value.lval = IBASE_TRANS_ON_LINK *
php3_list_insert(ib_link, IBASE_GLOBAL(php3_ibase_module).le_plink);
return_value->type = IS_LONG;
} else {
list_entry *index_ptr, new_index_ptr;
/* first we check the hash for the hashed_details key. if it exists,
* it should point us to the right offset where the actual ib_link sits.
* if it doesn't, open a new ib_link, add it to the resource list,
* and add a pointer to it with hashed_details as the key.
*/
if (_php3_hash_find(list,hashed_details,hashed_details_length+1,(void **) &index_ptr)==SUCCESS) {
int type,xlink;
void *ptr;
if (index_ptr->type != le_index_ptr) {
RETURN_FALSE;
}
xlink = (int) index_ptr->ptr;
ptr = php3_list_find(xlink,&type); /* check if the xlink is still there */
if (ptr && (type==IBASE_GLOBAL(php3_ibase_module).le_link || type==IBASE_GLOBAL(php3_ibase_module).le_plink)) {
return_value->value.lval = IBASE_GLOBAL(php3_ibase_module).default_link = xlink;
return_value->type = IS_LONG;
efree(hashed_details);
return;
} else {
_php3_hash_del(list,hashed_details,hashed_details_length+1);
}
}
if (IBASE_GLOBAL(php3_ibase_module).max_links!=-1 && IBASE_GLOBAL(php3_ibase_module).num_links>=IBASE_GLOBAL(php3_ibase_module).max_links) {
_php3_ibase_module_error("Too many open links (%d)", IBASE_GLOBAL(php3_ibase_module).num_links);
efree(hashed_details);
RETURN_FALSE;
}
/* create the ib_link */
if (_php_ibase_attach_db(ib_server, ib_uname, ib_passwd, ib_charset, 0, NULL, &db_handle) == FAILURE) {
efree(hashed_details);
RETURN_FALSE;
}
ib_link = (ibase_db_link *) emalloc(sizeof(ibase_db_link));
ib_link->link = db_handle;
for(i = 0; i < IBASE_TRANS_ON_LINK; i++)
ib_link->trans[i] = NULL;
/* add it to the list */
return_value->value.lval = IBASE_TRANS_ON_LINK *
php3_list_insert(ib_link, IBASE_GLOBAL(php3_ibase_module).le_link);
return_value->type = IS_LONG;
/* add it to the hash */
new_index_ptr.ptr = (void *) return_value->value.lval;
new_index_ptr.type = le_index_ptr;
if (_php3_hash_update(list,hashed_details,hashed_details_length+1,(void *) &new_index_ptr, sizeof(list_entry), NULL)==FAILURE) {
efree(hashed_details);
RETURN_FALSE;
}
IBASE_GLOBAL(php3_ibase_module).num_links++;
}
efree(hashed_details);
IBASE_GLOBAL(php3_ibase_module).default_link=return_value->value.lval;
}
/* }}} */
/* {{{ proto int ibase_connect(string database [, string username] [, string password] [, string charset])
Open a connection to an InterBase database */
PHP_FUNCTION(ibase_connect)
{
_php3_ibase_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ proto int ibase_pconnect(string database [, string username] [, string password] [, string charset])
Open a persistent connection to an InterBase database */
PHP_FUNCTION(ibase_pconnect)
{
_php3_ibase_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
/* {{{ proto int ibase_close([int link_identifier])
Close an InterBase connection */
PHP_FUNCTION(ibase_close)
{
pval *link_arg;
ibase_db_link *ib_link;
int link_id, trans_n;
IBASE_TLS_VARS;
RESET_ERRMSG;
switch (ARG_COUNT(ht)) {
case 0:
link_id = IBASE_GLOBAL(php3_ibase_module).default_link;
break;
case 1:
if (getParameters(ht, 1, &link_arg) == FAILURE) {
RETURN_FALSE;
}
convert_to_long(link_arg);
link_id = link_arg->value.lval;
break;
default:
WRONG_PARAM_COUNT;
break;
}
GET_LINK_TRANS(link_id, ib_link, trans_n);
php3_list_delete(link_id);
RETURN_TRUE;
}
/* }}} */
/* {{{ _php3_ibase_alloc_array() */
static int _php3_ibase_alloc_array(ibase_array **ib_arrayp, int *array_cntp,
XSQLDA *sqlda, isc_db_handle link, isc_tr_handle trans)
{
#define IB_ARRAY (*ib_arrayp)
int i, dim, ar_cnt, ar_length;
XSQLVAR *var;
IB_ARRAY = NULL;
ar_cnt = 0; /* find arrays */
var = sqlda->sqlvar;
for(i = 0; i < sqlda->sqld; i++, var++){
if((var->sqltype & ~1) == SQL_ARRAY)
ar_cnt++;
}
if(ar_cnt){ /* have arrays ? */
*array_cntp = ar_cnt;
IB_ARRAY = emalloc(sizeof(ibase_array)*ar_cnt);
ar_cnt = 0;
var = sqlda->sqlvar;
for(i = 0; i < sqlda->sqld; i++, var++){
if((var->sqltype & ~1) == SQL_ARRAY){
ISC_ARRAY_DESC *ar_desc = &IB_ARRAY[ar_cnt].ar_desc;
if(isc_array_lookup_bounds(IB_STATUS, &link, &trans,
var->relname, var->sqlname, ar_desc)){
_php3_ibase_error();
efree(IB_ARRAY);
IB_ARRAY = NULL;
return FAILURE;
}
switch (ar_desc->array_desc_dtype){
case blr_text:
case blr_text2:
IB_ARRAY[ar_cnt].el_type = SQL_TEXT;
IB_ARRAY[ar_cnt].el_size = ar_desc->array_desc_length+1;
break;
case blr_short:
IB_ARRAY[ar_cnt].el_type = SQL_SHORT;
IB_ARRAY[ar_cnt].el_size = sizeof(short);
break;
case blr_long:
IB_ARRAY[ar_cnt].el_type = SQL_LONG;
IB_ARRAY[ar_cnt].el_size = sizeof(long);
break;
case blr_float:
IB_ARRAY[ar_cnt].el_type = SQL_FLOAT;
IB_ARRAY[ar_cnt].el_size = sizeof(float);
break;
case blr_double:
IB_ARRAY[ar_cnt].el_type = SQL_DOUBLE;
IB_ARRAY[ar_cnt].el_size = sizeof(double);
break;
case blr_date:
IB_ARRAY[ar_cnt].el_type = SQL_DATE;
IB_ARRAY[ar_cnt].el_size = sizeof(ISC_QUAD);
break;
case blr_varying:
case blr_varying2: /* changed to SQL_TEXT ? */
/* sql_type = SQL_VARYING; Why? FIXME: ??? */
IB_ARRAY[ar_cnt].el_type = SQL_TEXT;
IB_ARRAY[ar_cnt].el_size = ar_desc->array_desc_length+sizeof(short);
break;
default:
_php3_ibase_module_error("unexpected array type %d in relation '%s' column '%s')",
ar_desc->array_desc_dtype,var->relname, var->sqlname);
efree(IB_ARRAY);
IB_ARRAY = NULL;
return FAILURE;
}/* switch array_desc_type */
ar_length = 0; /* calculate elements count */
for(dim = 0; dim < ar_desc->array_desc_dimensions; dim++){
ar_length += 1 + ar_desc->array_desc_bounds[dim].array_bound_upper
- ar_desc->array_desc_bounds[dim].array_bound_lower;
}
IB_ARRAY[ar_cnt].ar_size = IB_ARRAY[ar_cnt].el_size * ar_length;
ar_cnt++;
}/* if SQL_ARRAY */
}/* for column */
}/* if array_cnt */
return SUCCESS;
#undef IB_ARRAY
}
/* }}} */
/* {{{ _php3_ibase_alloc_query() */
/* allocate and prepare query */
static int _php3_ibase_alloc_query(ibase_query **ib_queryp, isc_db_handle link, isc_tr_handle trans, char *query)
{
#define IB_QUERY (*ib_queryp)
IB_QUERY = emalloc(sizeof(ibase_query));
IB_QUERY->link = link;
IB_QUERY->trans = trans;
IB_QUERY->stmt = NULL;
IB_QUERY->out_sqlda = NULL;
IB_QUERY->in_sqlda = NULL;
IB_QUERY->in_array = NULL;
IB_QUERY->in_array_cnt = 0;
IB_QUERY->out_array = NULL;
IB_QUERY->out_array_cnt = 0;
if (isc_dsql_allocate_statement(IB_STATUS, &link, &IB_QUERY->stmt)) {
_php3_ibase_error();
goto _php3_ibase_alloc_query_error;
}
IB_QUERY->out_sqlda = emalloc(XSQLDA_LENGTH(0));
IB_QUERY->out_sqlda->sqln = 0;
IB_QUERY->out_sqlda->version = SQLDA_VERSION1;
if (isc_dsql_prepare(IB_STATUS, &IB_QUERY->trans, &IB_QUERY->stmt, 0, query, SQLDA_VERSION1, IB_QUERY->out_sqlda)) {
_php3_ibase_error();
goto _php3_ibase_alloc_query_error;
}
/* not enough output variables ? */
if(IB_QUERY->out_sqlda->sqld > IB_QUERY->out_sqlda->sqln){
IB_QUERY->out_sqlda = erealloc(IB_QUERY->out_sqlda,XSQLDA_LENGTH(IB_QUERY->out_sqlda->sqld));
IB_QUERY->out_sqlda->sqln = IB_QUERY->out_sqlda->sqld;
IB_QUERY->out_sqlda->version = SQLDA_VERSION1;
if (isc_dsql_describe(IB_STATUS, &IB_QUERY->stmt, SQLDA_VERSION1, IB_QUERY->out_sqlda)) {
_php3_ibase_error();
goto _php3_ibase_alloc_query_error;
}
}
/* maybe have input placeholders? */
IB_QUERY->in_sqlda = emalloc(XSQLDA_LENGTH(0));
IB_QUERY->in_sqlda->sqln = 0;
IB_QUERY->in_sqlda->version = SQLDA_VERSION1;
if (isc_dsql_describe_bind(IB_STATUS, &IB_QUERY->stmt, SQLDA_VERSION1, IB_QUERY->in_sqlda)) {
_php3_ibase_error();
goto _php3_ibase_alloc_query_error;
}
/* not enough input variables ? */
if(IB_QUERY->in_sqlda->sqln < IB_QUERY->in_sqlda->sqld){
IB_QUERY->in_sqlda = erealloc(IB_QUERY->in_sqlda,XSQLDA_LENGTH(IB_QUERY->in_sqlda->sqld));
IB_QUERY->in_sqlda->sqln = IB_QUERY->in_sqlda->sqld;
IB_QUERY->in_sqlda->version = SQLDA_VERSION1;
if (isc_dsql_describe_bind(IB_STATUS, &IB_QUERY->stmt, SQLDA_VERSION1, IB_QUERY->in_sqlda)) {
_php3_ibase_error();
goto _php3_ibase_alloc_query_error;
}
}
/* allocate arrays... */
if (_php3_ibase_alloc_array(&IB_QUERY->in_array, &IB_QUERY->in_array_cnt,
IB_QUERY->in_sqlda, link, trans) == FAILURE){
goto _php3_ibase_alloc_query_error; /* error report already done */
}
if (_php3_ibase_alloc_array(&IB_QUERY->out_array, &IB_QUERY->out_array_cnt,
IB_QUERY->out_sqlda, link, trans) == FAILURE){
goto _php3_ibase_alloc_query_error;
}
/* no, haven't placeholders at all */
if(IB_QUERY->in_sqlda->sqld == 0){
efree(IB_QUERY->in_sqlda);
IB_QUERY->in_sqlda = NULL;
}
if(IB_QUERY->out_sqlda->sqld == 0){
efree(IB_QUERY->out_sqlda);
IB_QUERY->out_sqlda = NULL;
}
return SUCCESS;
_php3_ibase_alloc_query_error:
if(IB_QUERY->out_sqlda)
efree(IB_QUERY->out_sqlda);
if(IB_QUERY->in_sqlda)
efree(IB_QUERY->in_sqlda);
if(IB_QUERY->out_array)
efree(IB_QUERY->out_array);
efree(IB_QUERY);
IB_QUERY = NULL;
return FAILURE;
#undef IB_QUERY
}
/* }}} */
/* {{{ proto int ibase_bind(int query)
Bind parameter placeholders in a previously prepared query */
static int _php3_ibase_bind(XSQLDA *sqlda, pval **b_vars, BIND_BUF *buf)
{
XSQLVAR *var;
pval *b_var;
int i;
var = sqlda->sqlvar;
for(i = 0; i < sqlda->sqld; var++, i++) { /* binded vars */
buf[i].sqlind = 0;
var->sqlind = &buf[i].sqlind;
b_var = b_vars[i];
switch(var->sqltype & ~1) {
case SQL_TEXT: /* direct to variable */
case SQL_VARYING:
convert_to_string(b_var);
var->sqldata = (void ISC_FAR *)b_var->value.str.val;
var->sqllen = b_var->value.str.len;
var->sqltype = SQL_TEXT + (var->sqltype & 1);
break;
case SQL_SHORT:
convert_to_long(b_var);
if(b_var->value.lval > SHRT_MAX || b_var->value.lval < SHRT_MIN){
_php3_ibase_module_error("field %*s overflow", var->aliasname_length, var->aliasname);
return FAILURE;
}
buf[i].val.sval = (short)b_var->value.lval;
var->sqldata = (void ISC_FAR *)(&buf[i].val.sval);
break;
case SQL_LONG: /* direct to variable */
convert_to_long(b_var);
var->sqldata = (void ISC_FAR *)(&b_var->value.lval);
break;
case SQL_FLOAT:
convert_to_double(b_var);
buf[i].val.fval = (float)b_var->value.dval;
var->sqldata = (void ISC_FAR *)(&buf[i].val.fval);
break;
case SQL_DOUBLE: /* direct to variable */
convert_to_double(b_var);
var->sqldata = (void ISC_FAR *)(&b_var->value.dval);
break;
case SQL_DATE:
{
struct tm t;
t.tm_year = t.tm_mon = t.tm_mday = t.tm_hour =
t.tm_min = t.tm_sec = 0;
convert_to_string(b_var);
#if HAVE_STRPTIME /*FIXME: HAVE_STRPTIME ?*/
strptime(b_var->value.str.val, IBASE_GLOBAL(php3_ibase_module).timeformat, &t);
#else
{
int n = sscanf(b_var->value.str.val,"%d%*[/]%d%*[/]%d %d%*[:]%d%*[:]%d",
&t.tm_mon, &t.tm_mday, &t.tm_year, &t.tm_hour, &t.tm_min, &t.tm_sec);
if(n != 3 && n != 6){
_php3_ibase_module_error("invalid date/time format");
return FAILURE;
}
t.tm_year -= 1900;
t.tm_mon--;
}
#endif
isc_encode_date(&t, &buf[i].val.qval);
var->sqldata = (void ISC_FAR *)(&buf[i].val.qval);
}
break;
case SQL_BLOB:
{
ibase_blob_handle *ib_blob_id;
if(b_var->type != IS_STRING
|| b_var->value.str.len != sizeof(ibase_blob_handle)
|| ((ibase_blob_handle *)(b_var->value.str.val))->bl_handle != 0){
_php3_ibase_module_error("invalid blob id string");
return FAILURE;
}
ib_blob_id = (ibase_blob_handle *)b_var->value.str.val;
var->sqldata = (void ISC_FAR *)&ib_blob_id->bl_qd;
}
break;
case SQL_ARRAY:
_php3_ibase_module_error("binding arrays not supported yet");
return FAILURE;
break;
}/*switch*/
}/*for*/
return SUCCESS;
}
/* }}} */
/* {{{ _php3_ibase_alloc_xsqlda() */
static void _php3_ibase_alloc_xsqlda(XSQLDA *sqlda)
{
int i;
XSQLVAR *var = sqlda->sqlvar;
for (i = 0; i < sqlda->sqld; i++, var++) {
switch(var->sqltype & ~1){
case SQL_TEXT:
var->sqldata = emalloc(sizeof(char)*(var->sqllen));
break;
case SQL_VARYING:
var->sqldata = emalloc(sizeof(char)*(var->sqllen+sizeof(short)));
break;
case SQL_SHORT:
var->sqldata = emalloc(sizeof(short));
break;
case SQL_LONG:
var->sqldata = emalloc(sizeof(long));
break;
case SQL_FLOAT:
var->sqldata = emalloc(sizeof(float));
break;
case SQL_DOUBLE:
var->sqldata = emalloc(sizeof(double));
break;
case SQL_DATE:
case SQL_BLOB:
case SQL_ARRAY:
var->sqldata = emalloc(sizeof(ISC_QUAD));
break;
}/*switch*/
if(var->sqltype & 1) /* sql NULL flag */
var->sqlind = emalloc(sizeof(short));
else
var->sqlind = NULL;
}/* for*/
}
/* }}} */
/* {{{ _php3_ibase_exec() */
static int _php3_ibase_exec(ibase_result **ib_resultp, ibase_query *ib_query, int argc, pval **args)
{
#define IB_RESULT (*ib_resultp)
XSQLDA *in_sqlda = NULL, *out_sqlda = NULL;
BIND_BUF *bind_buf = NULL;
int rv = FAILURE;
IB_RESULT = NULL;
/* allocate sqlda and output buffers */
if(ib_query->out_sqlda){ /* output variables in select, select for update */
IB_RESULT = emalloc(sizeof(ibase_result));
IB_RESULT->link = ib_query->link;
IB_RESULT->trans = ib_query->trans;
IB_RESULT->stmt = ib_query->stmt;
IB_RESULT->drop_stmt = 0; /* when free result close but not drop!*/
out_sqlda = IB_RESULT->out_sqlda = emalloc(XSQLDA_LENGTH(ib_query->out_sqlda->sqld));
memcpy(out_sqlda,ib_query->out_sqlda,XSQLDA_LENGTH(ib_query->out_sqlda->sqld));
_php3_ibase_alloc_xsqlda(out_sqlda);
if(ib_query->out_array){
IB_RESULT->out_array = emalloc(sizeof(ibase_array)*ib_query->out_array_cnt);
memcpy(IB_RESULT->out_array, ib_query->out_array, sizeof(ibase_array)*ib_query->out_array_cnt);
}else{
IB_RESULT->out_array = NULL;
}
}
if(ib_query->in_sqlda){ /* has placeholders */
if (ib_query->in_sqlda->sqld != argc){
_php3_ibase_module_error("placeholders (%d) and variables (%d) mismatch",ib_query->in_sqlda->sqld, argc);
goto _php3_ibase_exec_error; /* yes mommy, goto! */
}
in_sqlda = emalloc(XSQLDA_LENGTH(ib_query->in_sqlda->sqld));
memcpy(in_sqlda,ib_query->in_sqlda,XSQLDA_LENGTH(ib_query->in_sqlda->sqld));
bind_buf = emalloc(sizeof(BIND_BUF) * ib_query->in_sqlda->sqld);
if(_php3_ibase_bind(in_sqlda, args, bind_buf) == FAILURE){
goto _php3_ibase_exec_error;
}
}
if (isc_dsql_execute(IB_STATUS, &ib_query->trans, &ib_query->stmt, 1, in_sqlda)) {
_php3_ibase_error();
goto _php3_ibase_exec_error;
}
rv = SUCCESS;
_php3_ibase_exec_error: /* I'm a bad boy... */
if(in_sqlda)
efree(in_sqlda);
if(bind_buf)
efree(bind_buf);
if(rv == FAILURE){
if(IB_RESULT){
efree(IB_RESULT);
IB_RESULT = NULL;
}
if(out_sqlda)
efree(out_sqlda);
}
return rv;
#undef IB_RESULT
}
/* }}} */
/* {{{ proto int ibase_trans([int trans_args [, int link_identifier]])
Start transaction */
PHP_FUNCTION(ibase_trans)
{
pval **args;
char tpb[20], *tpbp = NULL;
long trans_argl = 0;
int tpb_len = 0, argn, link_id, trans_n;
ibase_db_link *ib_link;
IBASE_TLS_VARS;
RESET_ERRMSG;
link_id = IBASE_GLOBAL(php3_ibase_module).default_link;
/* TODO: multi-databases trans */
argn = ARG_COUNT(ht);
if(argn < 0 || argn > 2){
WRONG_PARAM_COUNT;
}
if(argn){
args = emalloc(sizeof(pval*) * argn);
if (getParametersArray(ht, argn, args) == FAILURE) {
efree(args);
RETURN_FALSE;
}
argn--;
convert_to_long(args[argn]); /* last arg - trans_args */
trans_argl = args[argn]->value.lval;
/* TODO: multi
do{
link_id[argn] = args[argn];
convert_to_long(args[argn]);
GET_LINK_TRANS(args[argn].value.lval, ib_link[argn], trans_n);
}while(argn--); */
if(argn){
argn--;
convert_to_long(args[argn]);
link_id = args[argn]->value.lval;
}
efree(args);
}
GET_LINK_TRANS(link_id, ib_link, trans_n);
if(trans_argl){
tpb[tpb_len++] = isc_tpb_version3;
tpbp = tpb;
/* access mode */
if(trans_argl & PHP3_IBASE_READ) /* READ ONLY TRANSACTION */
tpb[tpb_len++] = isc_tpb_read;
else
tpb[tpb_len++] = isc_tpb_write;
/* isolation level */
if(trans_argl & PHP3_IBASE_COMMITED){
tpb[tpb_len++] = isc_tpb_read_committed;
}else if (trans_argl & PHP3_IBASE_CONSISTENCY)
tpb[tpb_len++] = isc_tpb_consistency;
else
tpb[tpb_len++] = isc_tpb_concurrency;
/* lock resolution */
if(trans_argl & PHP3_IBASE_NOWAIT)
tpb[tpb_len++] = isc_tpb_nowait;
else
tpb[tpb_len++] = isc_tpb_wait;
}
/* find empty transaction slot */
for(trans_n = 0; trans_n < IBASE_TRANS_ON_LINK
&& ib_link->trans[trans_n]; trans_n++)
;
if(trans_n == IBASE_TRANS_ON_LINK){
_php3_ibase_module_error("too many transactions on link");
RETURN_FALSE;
}
if (isc_start_transaction(IB_STATUS, &ib_link->trans[trans_n], 1, &ib_link->link, tpb_len, tpbp)) {
_php3_ibase_error();
RETURN_FALSE;
}
RETURN_LONG(link_id * IBASE_TRANS_ON_LINK + trans_n);
}
/* }}} */
/* {{{ _php3_ibase_def_trans() */
/* open default transaction */
static int _php3_ibase_def_trans(ibase_db_link * ib_link, int trans_n)
{
if(trans_n == 0 && ib_link->trans[0] == NULL){
if (isc_start_transaction(IB_STATUS, &ib_link->trans[0], 1, &ib_link->link, 0, NULL)) {
_php3_ibase_error();
return FAILURE;
}
}
return SUCCESS;
}
/*}}}*/
/* {{{ _php3_ibase_trans_end() */
#define COMMIT 1
#define ROLLBACK 0
static void _php3_ibase_trans_end(INTERNAL_FUNCTION_PARAMETERS, int commit)
{
pval *link_trans_arg;
int link_id = 0, trans_n = 0;
ibase_db_link *ib_link;
IBASE_TLS_VARS;
RESET_ERRMSG;
switch (ARG_COUNT(ht)) {
case 0:
link_id = IBASE_GLOBAL(php3_ibase_module).default_link;
break;
case 1:
if (getParameters(ht, 1, &link_trans_arg) == FAILURE) {
RETURN_FALSE;
}
convert_to_long(link_trans_arg);
link_id = link_trans_arg->value.lval;
break;
default:
WRONG_PARAM_COUNT;
break;
}
GET_LINK_TRANS(link_id, ib_link, trans_n);
if (commit) {
if(isc_commit_transaction(IB_STATUS, &ib_link->trans[trans_n])){
_php3_ibase_error();
RETURN_FALSE;
}
}else{
if(isc_rollback_transaction(IB_STATUS, &ib_link->trans[trans_n])){
_php3_ibase_error();
RETURN_FALSE;
}
}
ib_link->trans[trans_n] = NULL;
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int ibase_commit([int link_identifier,] int trans_number)
Commit transaction */
PHP_FUNCTION(ibase_commit)
{
_php3_ibase_trans_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, COMMIT);
}
/* }}} */
/* {{{ proto int ibase_rollback([int link_identifier,] int trans_number)
Roolback transaction */
PHP_FUNCTION(ibase_rollback)
{
_php3_ibase_trans_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, ROLLBACK);
}
/* }}} */
/* {{{ proto int ibase_query([int link_identifier,] string query [, int bind_args])
Execute a query */
PHP_FUNCTION(ibase_query)
{
pval **args, **bind_args = NULL;
int i, link_id = 0, trans_n = 0, bind_n = 0;
char *query;
ibase_db_link *ib_link;
ibase_query *ib_query;
ibase_result *ib_result;
IBASE_TLS_VARS;
RESET_ERRMSG;
if(ARG_COUNT(ht) < 1){
WRONG_PARAM_COUNT;
}
args = emalloc(sizeof(pval*) * ARG_COUNT(ht));
if (getParametersArray(ht, ARG_COUNT(ht), args) == FAILURE) {
efree(args);
RETURN_FALSE;
}
i = 0;
if(args[i]->type == IS_LONG){ /* link or transaction argument */
link_id = args[i]->value.lval;
i++; /* next arg */
}else{
link_id = IBASE_GLOBAL(php3_ibase_module).default_link;
}
GET_LINK_TRANS(link_id, ib_link, trans_n);
if(args[i]->type == IS_STRING){ /* query argument */
query = args[i]->value.str.val;
i++; /* next arg */
}else{
_php3_ibase_module_error("query argument missed");
efree(args);
RETURN_FALSE;
}
if(ARG_COUNT(ht) > i){ /* have variables to bind */
bind_n = ARG_COUNT(ht) - i;
bind_args = &args[i];
}
/* open default transaction */
if(_php3_ibase_def_trans(ib_link, trans_n) == FAILURE){
efree(args);
RETURN_FALSE;
}
if(_php3_ibase_alloc_query(&ib_query, ib_link->link, ib_link->trans[trans_n], query) == FAILURE){
efree(args);
RETURN_FALSE;
}
if(_php3_ibase_exec(&ib_result, ib_query, bind_n, bind_args) == FAILURE){
_php3_ibase_free_query(ib_query);
efree(args);
RETURN_FALSE;
}
efree(args);
if(ib_result) { /* select statement */
ib_result->drop_stmt = 1; /* drop stmt when free result */
ib_query->stmt = NULL; /* keep stmt when free query */
_php3_ibase_free_query(ib_query);
RETURN_LONG(php3_list_insert(ib_result, IBASE_GLOBAL(php3_ibase_module).le_result));
}else{
_php3_ibase_free_query(ib_query);
RETURN_TRUE;
}
}
/* }}} */
/* {{{ _php3_ibase_var_pval() */
static int _php3_ibase_var_pval(pval *val, void *data, int type, int len, int scale, int flag)
{
char string_data[255];
switch(type & ~1) {
case SQL_VARYING:
len = ((IBASE_VCHAR *) data)->var_len;
data = ((IBASE_VCHAR *) data)->var_str;
/* fallout */
case SQL_TEXT:
val->value.str.val = (char *)emalloc(sizeof(char)*(len+1));
strncpy(val->value.str.val, data, len);
if (php3_ini.magic_quotes_runtime) {
char *tmp = val->value.str.val;
val->value.str.val = _php3_addslashes(val->value.str.val, len, &len, 0);
efree(tmp);
}
val->type = IS_STRING;
val->value.str.len = len;
break;
case SQL_LONG:
if (scale) {
int j, f = 1;
float n = *(long *)(data);
for (j = 0; j < -scale; j++)
f *= 10;
val->type = IS_STRING;
val->value.str.len = sprintf(string_data, "%.*f", -scale, n / f);
val->value.str.val = estrdup(string_data);
}else{
val->type = IS_LONG;
val->value.lval = *(long *)(data);
}
break;
case SQL_SHORT:
val->type = IS_LONG;
val->value.lval = *(short *)(data);
break;
case SQL_FLOAT:
val->type = IS_DOUBLE;
val->value.dval = *(float *)(data);
break;
case SQL_DOUBLE:
if (scale) {
val->type = IS_STRING;
val->value.str.len = sprintf(string_data, "%.*f", -scale, *(double *)data);
val->value.str.val = estrdup(string_data);
}else{
val->type = IS_DOUBLE;
val->value.dval = *(double *)data;
}
break;
case SQL_DATE:{
struct tm t;
long timestamp = -1;
isc_decode_date((ISC_QUAD *) data, &t);
/*
XXX - Might have to remove this later - seems that isc_decode_date()
always sets tm_isdst to 0, sometimes incorrectly (InterBase 6 bug?)
*/
t.tm_isdst = -1;
timestamp = mktime(&t);
#if HAVE_TM_ZONE
t.tm_zone = tzname[0];
#endif
if(flag & PHP3_IBASE_TIMESTAMP){
val->type = IS_LONG;
val->value.lval = timestamp;
}else{
val->type = IS_STRING;
#if HAVE_STRFTIME
val->value.str.len = strftime(string_data, sizeof(string_data), IBASE_GLOBAL(php3_ibase_module).timeformat, &t);
#else
if(!t.tm_hour && !t.tm_min && !t.tm_sec)
val->value.str.len = sprintf(string_data, "%02d/%02d/%4d", t.tm_mon+1, t.tm_mday, t.tm_year+1900);
else
val->value.str.len = sprintf(string_data, "%02d/%02d/%4d %02d:%02d:%02d",
t.tm_mon+1, t.tm_mday, t.tm_year+1900, t.tm_hour, t.tm_min, t.tm_sec);
#endif
val->value.str.val = estrdup(string_data);
break;
}
}
default:
return FAILURE;
}/* switch (type) */
return SUCCESS;
}
/* }}} */
/* {{{ _php3_ibase_arr_pval() */
/* create multidimension array - resursion function
(*datap) argument changed */
static int _php3_ibase_arr_pval(pval *ar_pval, char **datap, ibase_array *ib_array, int dim, int flag)
{
pval tmp;
int i, dim_len, l_bound, u_bound;
if (dim > 16){ /* InterBase limit */
_php3_ibase_module_error("php module internal error in %s %d",__FILE__,__LINE__);
return FAILURE;
}
u_bound = ib_array->ar_desc.array_desc_bounds[dim].array_bound_upper;
l_bound = ib_array->ar_desc.array_desc_bounds[dim].array_bound_lower;
dim_len = 1 + u_bound - l_bound;
if(dim < ib_array->ar_desc.array_desc_dimensions - 1){ /* array again */
for(i = 0; i < dim_len; i++){
/* recursion here */
if(_php3_ibase_arr_pval(ar_pval, datap, ib_array, dim+1, flag) == FAILURE){
return FAILURE;
}
}
}else{ /* data at last */
array_init(ar_pval);
for(i = 0; i < dim_len; i++){
if(_php3_ibase_var_pval(&tmp, *datap, ib_array->el_type,
ib_array->ar_desc.array_desc_length,
ib_array->ar_desc.array_desc_scale,
flag) == FAILURE){
return FAILURE;
}
_php3_hash_index_update(ar_pval->value.ht,
l_bound + i,
(void *) &tmp, sizeof(pval),NULL);
*datap += ib_array->el_size;
}
}
return SUCCESS;
}
/* {{{ _php3_ibase_fetch_hash() */
#define FETCH_ARRAY 1
#define FETCH_OBJECT 2
static void _php3_ibase_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int fetch_type)
{
pval *result_arg, *flag_arg;
long flag = 0;
int i, arr_cnt;
ibase_result *ib_result;
XSQLVAR *var;
IBASE_TLS_VARS;
RESET_ERRMSG;
switch(ARG_COUNT(ht)){
case 1:
if (ARG_COUNT(ht)==1 && getParameters(ht, 1, &result_arg)==FAILURE) {
RETURN_FALSE;
}
break;
case 2:
if(ARG_COUNT(ht)==2 && getParameters(ht, 2, &result_arg, &flag_arg)==FAILURE) {
RETURN_FALSE;
}
convert_to_long(flag_arg);
flag = flag_arg->value.lval;
break;
default:
WRONG_PARAM_COUNT;
break;
}
convert_to_long(result_arg);
GET_RESULT(result_arg->value.lval, ib_result);
if (ib_result->out_sqlda == NULL) {
_php3_ibase_module_error("trying to fetch results from a non-select query");
RETURN_FALSE;
}
if(isc_dsql_fetch(IB_STATUS, &ib_result->stmt, 1, ib_result->out_sqlda) == 100L){
RETURN_FALSE; /* end of cursor */
}
if(IB_STATUS[0] && IB_STATUS[1]){ /* error in fetch */
_php3_ibase_error();
RETURN_FALSE;
}
if(fetch_type == FETCH_ARRAY){
if (array_init(return_value)==FAILURE) {
RETURN_FALSE;
}
}else if(fetch_type == FETCH_OBJECT){
if (object_init(return_value)==FAILURE) {
RETURN_FALSE;
}
}
arr_cnt = 0;
var = ib_result->out_sqlda->sqlvar;
for (i = 0; i < ib_result->out_sqlda->sqld; i++, var++) {
pval tmp;
if (((var->sqltype & 1) == 0) || *var->sqlind != -1){
switch(var->sqltype & ~1) {
case SQL_VARYING:
case SQL_TEXT:
case SQL_SHORT:
case SQL_LONG:
case SQL_FLOAT:
case SQL_DOUBLE:
case SQL_DATE:
_php3_ibase_var_pval(&tmp, var->sqldata, var->sqltype, var->sqllen, var->sqlscale, flag);
break;
case SQL_BLOB:
if(flag & PHP3_IBASE_TEXT){ /* text ? */
int stat;
isc_blob_handle bl_handle = NULL;
ISC_LONG max_len = 0, cur_len = 0;
char bl_items[1], *bl_data, bl_info[20], *p;
if(isc_open_blob(IB_STATUS, &ib_result->link, &ib_result->trans, &bl_handle, (ISC_QUAD ISC_FAR *) var->sqldata)){
_php3_ibase_error();
RETURN_FALSE;
}
bl_items[0] = isc_info_blob_total_length;
if(isc_blob_info(IB_STATUS,&bl_handle,sizeof(bl_items),bl_items,sizeof(bl_info),bl_info)){
_php3_ibase_error();
RETURN_FALSE;
}
/* find total length of blob's data */
for (p = bl_info; *p != isc_info_end && p < bl_info+sizeof(bl_info);){
int item_len, item = *p++;
item_len = (short)isc_vax_integer(p, 2);
p += 2;
if (item == isc_info_blob_total_length)
max_len = isc_vax_integer(p, item_len);
p += item_len;
}
bl_data = emalloc(max_len+1);
for(cur_len = stat = 0; stat == 0 && cur_len < max_len; ){
unsigned short seg_len, max_seg = max_len-cur_len > USHRT_MAX ? USHRT_MAX : max_len-cur_len;
stat = isc_get_segment(IB_STATUS, &bl_handle, &seg_len, max_seg, &bl_data[cur_len]);
cur_len += seg_len;
if(cur_len > max_len){ /* never!*/
efree(bl_data);
_php3_ibase_module_error("php module internal error in %s %d",__FILE__,__LINE__);
RETURN_FALSE;
}
}
if (IB_STATUS[0] && IB_STATUS[1] && (IB_STATUS[1] != isc_segstr_eof)){
efree(bl_data);
_php3_ibase_error();
RETURN_FALSE;
}
bl_data[cur_len] = '\0';
if(isc_close_blob(IB_STATUS, &bl_handle)){
efree(bl_data);
_php3_ibase_error();
RETURN_FALSE;
}
tmp.type = IS_STRING;
tmp.value.str.len = cur_len;
tmp.value.str.val = bl_data;
}else{ /* blob id only */
ISC_QUAD *bl_qd = (ISC_QUAD ISC_FAR *) var->sqldata;
ibase_blob_handle *ib_blob_id;
ib_blob_id = (ibase_blob_handle *) emalloc(sizeof(ibase_blob_handle));
ib_blob_id->link = ib_result->link;
ib_blob_id->trans_handle = ib_result->trans;
ib_blob_id->bl_qd.gds_quad_high = bl_qd->gds_quad_high;
ib_blob_id->bl_qd.gds_quad_low = bl_qd->gds_quad_low;
ib_blob_id->bl_handle = NULL;
tmp.type = IS_STRING;
tmp.value.str.len = sizeof(ibase_blob_handle);
tmp.value.str.val = (char *)ib_blob_id;
}
break;
case SQL_ARRAY:{
ISC_QUAD ar_qd = *(ISC_QUAD ISC_FAR *) var->sqldata;
ibase_array *ib_array = &ib_result->out_array[arr_cnt];
void *ar_data;
char *tmp_ptr;
ar_data = emalloc(ib_array->ar_size);
if (isc_array_get_slice(IB_STATUS, &ib_result->link, &ib_result->trans,
&ar_qd, &ib_array->ar_desc, ar_data, &ib_array->ar_size)){
_php3_ibase_error();
RETURN_FALSE;
}
tmp_ptr = ar_data; /* avoid changes in _arr_pval */
if(_php3_ibase_arr_pval(&tmp, &tmp_ptr, ib_array, 0, flag) == FAILURE){
RETURN_FALSE;
}
efree(ar_data);
}
break;
default:
break;
}/*switch*/
if (fetch_type & FETCH_ARRAY) {
_php3_hash_index_update(return_value->value.ht, i, (void *) &tmp, sizeof(pval),NULL);
}else{
_php3_hash_update(return_value->value.ht, var->aliasname, var->aliasname_length+1, (void *) &tmp, sizeof(pval), NULL);
}
}/* if not null */
if((var->sqltype & ~1) == SQL_ARRAY){
arr_cnt++;
}
}/*for field*/
}
/* }}} */
/* {{{ proto array ibase_fetch_row(int result [, int blob_flag])
Fetch a row from the results of a query */
PHP_FUNCTION(ibase_fetch_row)
{
_php3_ibase_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, FETCH_ARRAY);
}
/* }}} */
/* {{{ proto object ibase_fetch_object(int result [, int blob_flag])
Fetch a object from the results of a query */
PHP_FUNCTION(ibase_fetch_object)
{
_php3_ibase_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, FETCH_OBJECT);
}
/* }}} */
/* {{{ proto int ibase_free_result(int result)
Free the memory used by a result */
PHP_FUNCTION(ibase_free_result)
{
pval *result_arg;
ibase_result *ib_result;
IBASE_TLS_VARS;
RESET_ERRMSG;
if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &result_arg)==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(result_arg);
if (result_arg->value.lval==0) {
RETURN_FALSE;
}
GET_RESULT(result_arg->value.lval, ib_result);
php3_list_delete(result_arg->value.lval);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int ibase_prepare([int link_identifier,] string query)
Prepare a query for later execution */
PHP_FUNCTION(ibase_prepare)
{
pval *link_arg, *query_arg;
int link_id, trans_n = 0;
ibase_db_link *ib_link;
ibase_query *ib_query;
char *query;
IBASE_TLS_VARS;
RESET_ERRMSG;
switch (ARG_COUNT(ht)) {
case 1:
if (getParameters(ht, 1, &query_arg) == FAILURE) {
RETURN_FALSE;
}
link_id = IBASE_GLOBAL(php3_ibase_module).default_link;
break;
case 2:
if (getParameters(ht, 2, &link_arg, &query_arg) == FAILURE) {
RETURN_FALSE;
}
convert_to_long(link_arg);
link_id = link_arg->value.lval;
break;
default:
WRONG_PARAM_COUNT;
break;
}
GET_LINK_TRANS(link_id, ib_link, trans_n);
convert_to_string(query_arg);
query = query_arg->value.str.val;
/* open default transaction */
if(_php3_ibase_def_trans(ib_link, trans_n) == FAILURE){
RETURN_FALSE;
}
if(_php3_ibase_alloc_query(&ib_query, ib_link->link, ib_link->trans[trans_n], query) == FAILURE){
RETURN_FALSE;
}
RETURN_LONG(php3_list_insert(ib_query, IBASE_GLOBAL(php3_ibase_module).le_query));
}
/* }}} */
/* {{{ proto int ibase_execute(int query [, int bind_args [, int ...])
Execute a previously prepared query */
PHP_FUNCTION(ibase_execute)
{
pval **args, **bind_args = NULL;
ibase_query *ib_query;
ibase_result *ib_result;
IBASE_TLS_VARS;
RESET_ERRMSG;
if(ARG_COUNT(ht) < 1){
WRONG_PARAM_COUNT;
}
args = emalloc(sizeof(pval*) * ARG_COUNT(ht));
if (getParametersArray(ht, ARG_COUNT(ht), args) == FAILURE) {
efree(args);
RETURN_FALSE;
}
GET_QUERY(args[0]->value.lval, ib_query);
if(ARG_COUNT(ht) > 1){ /* have variables to bind */
bind_args = &args[1];
}
if( _php3_ibase_exec(&ib_result, ib_query, ARG_COUNT(ht)-1, bind_args) == FAILURE){;
efree(args);
RETURN_FALSE;
}
efree(args);
if (ib_result) { /* select statement */
RETURN_LONG(php3_list_insert(ib_result, IBASE_GLOBAL(php3_ibase_module).le_result));
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int ibase_free_query(int query)
Free memory used by a query */
PHP_FUNCTION(ibase_free_query)
{
pval *query_arg;
ibase_query *ib_query;
IBASE_TLS_VARS;
RESET_ERRMSG;
if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &query_arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(query_arg);
GET_QUERY(query_arg->value.lval, ib_query);
php3_list_delete(query_arg->value.lval);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int ibase_timefmt(string format)
Sets the format of datetime columns returned from queries. Still nonfunctional */
PHP_FUNCTION(ibase_timefmt)
{
pval *fmt;
IBASE_TLS_VARS;
RESET_ERRMSG;
#if HAVE_STRFTIME
if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &fmt)==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(fmt);
if(IBASE_GLOBAL(php3_ibase_module).timeformat)
DL_FREE(IBASE_GLOBAL(php3_ibase_module).timeformat);
IBASE_GLOBAL(php3_ibase_module).timeformat = DL_STRDUP(fmt->value.str.val);
RETURN_TRUE;
#else
_php3_ibase_module_error("ibase_timefmt not supported on this platform");
RETURN_FALSE;
#endif
}
/* }}} */
/* {{{ proto int ibase_num_fields(int result)
Get the number of fields in result */
PHP_FUNCTION(ibase_num_fields)
{
pval *result;
int type;
ibase_result *ib_result;
RESET_ERRMSG;
if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &result)==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(result);
ib_result = (ibase_result *) php3_list_find(result->value.lval, &type);
if (type!=IBASE_GLOBAL(php3_ibase_module).le_result) {
_php3_ibase_module_error("%d is not result index", result->value.lval);
RETURN_FALSE;
}
if (ib_result->out_sqlda == NULL) {
_php3_ibase_module_error("trying to get num fields from a non-select query");
RETURN_FALSE;
}
RETURN_LONG(ib_result->out_sqlda->sqld);
}
/* }}} */
/* {{{ proto array ibase_field_info(int result, int field_number)
Get information about a field */
PHP_FUNCTION(ibase_field_info)
{
pval *ret_val, *result_arg, *field_arg;
ibase_result *ib_result;
char buf[30], *s;
int len;
XSQLVAR *var;
IBASE_TLS_VARS;
RESET_ERRMSG;
if (ARG_COUNT(ht)!=2 || getParameters(ht, 2, &result_arg, &field_arg)==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(result_arg);
GET_RESULT(result_arg->value.lval, ib_result);
if (ib_result->out_sqlda == NULL) {
_php3_ibase_module_error("trying to get field info from a non-select query");
RETURN_FALSE;
}
convert_to_long(field_arg);
if (field_arg->value.lval<0 || field_arg->value.lval>=ib_result->out_sqlda->sqld)
RETURN_FALSE;
if (array_init(return_value)==FAILURE)
RETURN_FALSE;
var = ib_result->out_sqlda->sqlvar + field_arg->value.lval;
add_get_index_stringl(return_value, 0, var->sqlname, var->sqlname_length, (void **) &ret_val, 1);
_php3_hash_pointer_update(return_value->value.ht, "name", sizeof(char)*5, ret_val);
add_get_index_stringl(return_value, 1, var->aliasname, var->aliasname_length, (void **) &ret_val, 1);
_php3_hash_pointer_update(return_value->value.ht, "alias", sizeof(char)*6, ret_val);
add_get_index_stringl(return_value, 2, var->relname, var->relname_length, (void **) &ret_val, 1);
_php3_hash_pointer_update(return_value->value.ht, "relation", sizeof(char)*9, ret_val);
len = sprintf(buf, "%d", var->sqllen);
add_get_index_stringl(return_value, 3, buf, len, (void **) &ret_val, 1);
_php3_hash_pointer_update(return_value->value.ht, "length", sizeof(char)*7, ret_val);
switch (var->sqltype & ~1){
case SQL_TEXT: s = "TEXT"; break;
case SQL_VARYING: s = "VARYING"; break;
case SQL_SHORT: s = "SHORT"; break;
case SQL_LONG: s = "LONG"; break;
case SQL_FLOAT: s = "FLOAT"; break;
case SQL_DOUBLE: s = "DOUBLE"; break;
case SQL_D_FLOAT: s = "D_FLOAT"; break;
case SQL_DATE: s = "DATE"; break;
case SQL_BLOB: s = "BLOB"; break;
case SQL_ARRAY: s = "ARRAY"; break;
case SQL_QUAD: s = "QUAD"; break;
default:
sprintf(buf,"unknown (%d)", var->sqltype & ~1);
s = buf;
break;
}
add_get_index_stringl(return_value, 4, s, strlen(s), (void **) &ret_val, 1);
_php3_hash_pointer_update(return_value->value.ht, "type", sizeof(char)*5, ret_val);
}
/* }}} */
/* blobs ----------------------------------- */
/* {{{ _php3_ibase_blob_info(isc_blob_handle bl_handle,IBASE_BLOBINFO *bl_info) */
static int _php3_ibase_blob_info(isc_blob_handle bl_handle,IBASE_BLOBINFO *bl_info)
{
static char bl_items[] = {
isc_info_blob_num_segments,
isc_info_blob_max_segment,
isc_info_blob_total_length,
isc_info_blob_type
};
char bl_inf[sizeof(long)*8], *p;
bl_info->max_segment = 0;
bl_info->num_segments = 0;
bl_info->total_length = 0;
bl_info->bl_stream = 0;
if(isc_blob_info(IB_STATUS,&bl_handle,sizeof(bl_items),bl_items,sizeof(bl_inf),bl_inf)){
_php3_ibase_error();
return FAILURE;
}
for (p = bl_inf; *p != isc_info_end && p < bl_inf+sizeof(bl_inf);){
int item_len, item = *p++;
item_len = (short)isc_vax_integer(p, 2);
p += 2;
switch(item){
case isc_info_blob_num_segments:
bl_info->num_segments = isc_vax_integer(p, item_len);
break;
case isc_info_blob_max_segment:
bl_info->max_segment = isc_vax_integer(p, item_len);
break;
case isc_info_blob_total_length:
bl_info->total_length = isc_vax_integer(p, item_len);
break;
case isc_info_blob_type:
bl_info->bl_stream = isc_vax_integer(p, item_len);
break;
case isc_info_end:
break;
case isc_info_truncated:
case isc_info_error: /* hmm. don't think so...*/
_php3_ibase_module_error("php module internal error in %s %d",__FILE__,__LINE__);
return FAILURE;
}/*switch*/
p += item_len;
}/*for*/
return SUCCESS;
}
/* }}} */
/* {{{ proto int ibase_blob_create([int link_identifier])
Create blob for adding data */
PHP_FUNCTION(ibase_blob_create)
{
pval *link_arg;
int trans_n, link_id = 0;
ibase_db_link *ib_link;
ibase_blob_handle *ib_blob;
IBASE_TLS_VARS;
RESET_ERRMSG;
switch (ARG_COUNT(ht)) {
case 0:
link_id = IBASE_GLOBAL(php3_ibase_module).default_link;
break;
case 1:
if (getParameters(ht, 1, &link_arg) == FAILURE) {
RETURN_FALSE;
}
convert_to_long(link_arg);
link_id = link_arg->value.lval;
break;
default:
WRONG_PARAM_COUNT;
break;
}
GET_LINK_TRANS(link_id, ib_link, trans_n);
/* open default transaction */
if(_php3_ibase_def_trans(ib_link, trans_n) == FAILURE){
RETURN_FALSE;
}
ib_blob = (ibase_blob_handle *) emalloc(sizeof(ibase_blob_handle));
ib_blob->trans_handle = ib_link->trans[trans_n];
ib_blob->link = ib_link->link;
ib_blob->bl_handle = NULL;
if (isc_create_blob(IB_STATUS, &ib_blob->link, &ib_blob->trans_handle, &ib_blob->bl_handle,&ib_blob->bl_qd)){
efree(ib_blob);
_php3_ibase_error();
RETURN_FALSE;
}
RETURN_LONG(php3_list_insert(ib_blob, IBASE_GLOBAL(php3_ibase_module).le_blob));
}
/* }}} */
/* {{{ proto int ibase_blob_open(string blob_id)
Open blob for retriving data parts */
PHP_FUNCTION(ibase_blob_open)
{
pval *blob_arg;
ibase_blob_handle *ib_blob, *ib_blob_id;
IBASE_TLS_VARS;
RESET_ERRMSG;
if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &blob_arg)==FAILURE) {
WRONG_PARAM_COUNT;
}
ib_blob = (ibase_blob_handle *) emalloc(sizeof(ibase_blob_handle));
GET_BLOB_ID_ARG(blob_arg,ib_blob_id);
if(ib_blob_id == NULL){ /* blob IS NULL or argument unset */
RETURN_FALSE;
}
ib_blob->link = ib_blob_id->link;
ib_blob->trans_handle = ib_blob_id->trans_handle;
ib_blob->bl_qd.gds_quad_high = ib_blob_id->bl_qd.gds_quad_high;
ib_blob->bl_qd.gds_quad_low = ib_blob_id->bl_qd.gds_quad_low;
ib_blob->bl_handle = NULL;
if (isc_open_blob(IB_STATUS, &ib_blob->link, &ib_blob->trans_handle, &ib_blob->bl_handle, &ib_blob->bl_qd)){
efree(ib_blob);
_php3_ibase_error();
RETURN_FALSE;
}
RETURN_LONG(php3_list_insert(ib_blob, IBASE_GLOBAL(php3_ibase_module).le_blob));
}
/* }}} */
/* {{{ proto int ibase_blob_add(int blob_id, string data)
Add data into created blob */
PHP_FUNCTION(ibase_blob_add)
{
pval *blob_arg,*string_arg;
ibase_blob_handle *ib_blob;
IBASE_TLS_VARS;
RESET_ERRMSG;
if (ARG_COUNT(ht)!=2 || getParameters(ht, 2, &blob_arg, &string_arg)==FAILURE) {
WRONG_PARAM_COUNT;
}
GET_BLOB_HANDLE_ARG(blob_arg, ib_blob);
convert_to_string(string_arg);
if (isc_put_segment(IB_STATUS, &ib_blob->bl_handle, string_arg->value.str.len, string_arg->value.str.val)){
_php3_ibase_error();
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto string ibase_blob_get(int blob_id, int len)
Get len bytes data from open blob */
PHP_FUNCTION(ibase_blob_get)
{
pval *blob_arg, *len_arg;
int stat;
char *bl_data;
unsigned short max_len = 0, cur_len, seg_len;
ibase_blob_handle *ib_blob;
IBASE_TLS_VARS;
RESET_ERRMSG;
if(ARG_COUNT(ht) != 2 || getParameters(ht, 2, &blob_arg, &len_arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(len_arg);
max_len = len_arg->value.lval;
GET_BLOB_HANDLE_ARG(blob_arg, ib_blob);
if(ib_blob->bl_qd.gds_quad_high || ib_blob->bl_qd.gds_quad_low){ /*not null ?*/
bl_data = emalloc(max_len+1);
for(cur_len = stat = 0; stat == 0; ){
stat = isc_get_segment(IB_STATUS, &ib_blob->bl_handle, &seg_len, max_len-cur_len, &bl_data[cur_len]);
cur_len += seg_len;
if(cur_len > max_len){ /* never!*/
efree(bl_data);
_php3_ibase_module_error("php module internal error in %s %d",__FILE__,__LINE__);
RETURN_FALSE;
}
}
bl_data[cur_len] = '\0';
if (IB_STATUS[0] && (IB_STATUS[1] != isc_segstr_eof && IB_STATUS[1] != isc_segment)){
efree(bl_data);
_php3_ibase_error();
RETURN_FALSE;
}
RETURN_STRINGL(bl_data,cur_len,0);
}else{ /* null blob */
RETURN_STRING("",1); /* empty string */
}
}
/* }}} */
#define BLOB_CLOSE 1
#define BLOB_CANCEL 2
/* {{{ _php3_ibase_blob_end() */
/* Close or Cancel created or Close open blob */
static void _php3_ibase_blob_end(INTERNAL_FUNCTION_PARAMETERS, int bl_end)
{
pval *blob_arg;
ibase_blob_handle *ib_blob;
IBASE_TLS_VARS;
RESET_ERRMSG;
if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &blob_arg)==FAILURE) {
WRONG_PARAM_COUNT;
}
GET_BLOB_HANDLE_ARG(blob_arg, ib_blob);
if(bl_end == BLOB_CLOSE){ /* return id here */
if(ib_blob->bl_qd.gds_quad_high || ib_blob->bl_qd.gds_quad_low){ /*not null ?*/
if (isc_close_blob(IB_STATUS, &ib_blob->bl_handle)){
_php3_ibase_error();
RETURN_FALSE;
}
}
ib_blob->bl_handle = NULL;
RETVAL_STRINGL((char *)ib_blob, sizeof(ibase_blob_handle), 1);
php3_list_delete(blob_arg->value.lval);
}else{ /* discard created blob */
if (isc_cancel_blob(IB_STATUS, &ib_blob->bl_handle)){
_php3_ibase_error();
RETURN_FALSE;
}
ib_blob->bl_handle = NULL;
php3_list_delete(blob_arg->value.lval);
RETURN_TRUE;
}
}
/* }}} */
/* {{{ proto int ibase_blob_close(int blob_id)
Close blob */
PHP_FUNCTION(ibase_blob_close)
{
_php3_ibase_blob_end(INTERNAL_FUNCTION_PARAM_PASSTHRU,BLOB_CLOSE);
}
/* }}} */
/* {{{ proto int ibase_blob_cancel(int blob_id)
Cancel creating blob */
PHP_FUNCTION(ibase_blob_cancel)
{
_php3_ibase_blob_end(INTERNAL_FUNCTION_PARAM_PASSTHRU,BLOB_CANCEL);
}
/* }}} */
/* {{{ proto object ibase_blob_info(string blob_id_str)
Return blob length and other useful info */
PHP_FUNCTION(ibase_blob_info)
{
pval *blob_arg, *result_var;
ibase_blob_handle *ib_blob_id;
IBASE_BLOBINFO bl_info;
IBASE_TLS_VARS;
RESET_ERRMSG;
if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &blob_arg)==FAILURE) {
WRONG_PARAM_COUNT;
}
GET_BLOB_ID_ARG(blob_arg, ib_blob_id);
if (array_init(return_value)==FAILURE){
RETURN_FALSE;
}
if(ib_blob_id->bl_qd.gds_quad_high || ib_blob_id->bl_qd.gds_quad_low){ /*not null ?*/
if (isc_open_blob(IB_STATUS, &ib_blob_id->link, &ib_blob_id->trans_handle,
&ib_blob_id->bl_handle, &ib_blob_id->bl_qd)){
_php3_ibase_error();
RETURN_FALSE;
}
if(_php3_ibase_blob_info(ib_blob_id->bl_handle,&bl_info)){
RETURN_FALSE;
}
if(isc_close_blob(IB_STATUS, &ib_blob_id->bl_handle)){
_php3_ibase_error();
RETURN_FALSE;
}
ib_blob_id->bl_handle = NULL;
}else{ /* null blob, all values to zero */
bl_info.max_segment = 0;
bl_info.num_segments = 0;
bl_info.total_length = 0;
bl_info.bl_stream = 0;
}
add_get_index_long(return_value, 0, bl_info.total_length, (void **)&result_var);
_php3_hash_pointer_update(return_value->value.ht, "length", sizeof("length"), result_var);
add_get_index_long(return_value, 1, bl_info.num_segments, (void **)&result_var);
_php3_hash_pointer_update(return_value->value.ht, "numseg", sizeof("numseg"), result_var);
add_get_index_long(return_value, 2, bl_info.max_segment, (void **)&result_var);
_php3_hash_pointer_update(return_value->value.ht, "maxseg", sizeof("maxseg"), result_var);
add_get_index_long(return_value, 3, bl_info.bl_stream, (void **)&result_var);
_php3_hash_pointer_update(return_value->value.ht, "stream", sizeof("stream"), result_var);
add_get_index_long(return_value, 4,
(!ib_blob_id->bl_qd.gds_quad_high
&& !ib_blob_id->bl_qd.gds_quad_low),
(void **)&result_var);
_php3_hash_pointer_update(return_value->value.ht, "isnull", sizeof("isnull"), result_var);
}
/* }}} */
/* {{{ proto int ibase_blob_echo(string blob_id_str)
Output blob contents to browser */
PHP_FUNCTION(ibase_blob_echo)
{
pval *blob_arg;
char bl_data[IBASE_BLOB_SEG];
unsigned short seg_len;
ibase_blob_handle *ib_blob_id;
IBASE_TLS_VARS;
RESET_ERRMSG;
if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &blob_arg)==FAILURE) {
WRONG_PARAM_COUNT;
}
GET_BLOB_ID_ARG(blob_arg, ib_blob_id);
if (!php3_header()) {
RETURN_FALSE;
}
if(ib_blob_id){ /*not null ?*/
if (isc_open_blob(IB_STATUS, &ib_blob_id->link, &ib_blob_id->trans_handle,
&ib_blob_id->bl_handle,&ib_blob_id->bl_qd)){
_php3_ibase_error();
RETURN_FALSE;
}
while(!isc_get_segment(IB_STATUS, &ib_blob_id->bl_handle, &seg_len, sizeof(bl_data), bl_data)
|| IB_STATUS[1] == isc_segment){
PHPWRITE(bl_data,seg_len);
}
if (IB_STATUS[0] && (IB_STATUS[1] != isc_segstr_eof)){
_php3_ibase_error();
RETURN_FALSE;
}
if (isc_close_blob(IB_STATUS, &ib_blob_id->bl_handle)){
_php3_ibase_error();
RETURN_FALSE;
}
ib_blob_id->bl_handle = NULL;
}/* not null ? */
RETURN_TRUE;
}
/* }}} */
#ifndef MSVC5
extern int le_fp,le_pp;
extern int wsa_fp; /*to handle reading and writing to windows sockets*/
/* {{{ proto string ibase_blob_import([link_identifier,] int file_id)
Create blob, copy file in it, and close it */
PHP_FUNCTION(ibase_blob_import)
{
pval *link_arg, *file_arg;
int trans_n, type, link_id = 0, file_id, size, b;
int issock=0, socketd=0, *sock;
ibase_blob_handle ib_blob;
ibase_db_link *ib_link;
char bl_data[IBASE_BLOB_SEG]; /* FIXME? blob_seg_size parameter? */
FILE *fp;
IBASE_TLS_VARS;
RESET_ERRMSG;
switch (ARG_COUNT(ht)) {
case 1:
if (getParameters(ht, 1, &file_arg) == FAILURE) {
RETURN_FALSE;
}
link_id = IBASE_GLOBAL(php3_ibase_module).default_link;
break;
case 2:
if (getParameters(ht, 2, &link_arg, &file_arg) == FAILURE) {
RETURN_FALSE;
}
convert_to_long(link_arg);
link_id = link_arg->value.lval;
break;
default:
WRONG_PARAM_COUNT;
break;
}
GET_LINK_TRANS(link_id, ib_link, trans_n);
/* open default transaction */
if(_php3_ibase_def_trans(ib_link, trans_n) == FAILURE){
RETURN_FALSE;
}
convert_to_long(file_arg);
file_id = file_arg->value.lval;
fp = php3_list_find(file_id, &type);
if (type == wsa_fp){
issock = 1;
sock = php3_list_find(file_id, &type);
socketd =* sock;
}
if ((!fp || (type!= le_fp) && type!=le_pp
&& (!socketd || type != wsa_fp))) {
_php3_ibase_module_error("Unable to find file identifier %d",file_id);
RETURN_FALSE;
}
ib_blob.link = ib_link->link;
ib_blob.trans_handle = ib_link->trans[trans_n];
ib_blob.bl_handle = NULL;
ib_blob.bl_qd.gds_quad_high = 0;
ib_blob.bl_qd.gds_quad_low = 0;
if (isc_create_blob(IB_STATUS, &ib_blob.link, &ib_blob.trans_handle, &ib_blob.bl_handle,&ib_blob.bl_qd)){
_php3_ibase_error();
RETURN_FALSE;
}
size = 0;
while(issock?(b=SOCK_FREAD(bl_data,sizeof(bl_data),socketd)):(b = fread(bl_data, 1, sizeof(bl_data), fp)) > 0) {
if (isc_put_segment(IB_STATUS, &ib_blob.bl_handle, b, bl_data)){
_php3_ibase_error();
RETURN_FALSE;
}
size += b ;
}
if (isc_close_blob(IB_STATUS, &ib_blob.bl_handle)){
_php3_ibase_error();
RETURN_FALSE;
}
php3_list_delete(file_id);
ib_blob.bl_handle = NULL;
RETVAL_STRINGL((char *)&ib_blob, sizeof(ibase_blob_handle), 1);
}
/* }}} */
#endif
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
*/
|