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
|
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Andrey Hristov <andrey@php.net> |
| Ulf Wendel <uw@php.net> |
+----------------------------------------------------------------------+
*/
#include "php.h"
#include "mysqlnd.h"
#include "mysqlnd_connection.h"
#include "mysqlnd_vio.h"
#include "mysqlnd_protocol_frame_codec.h"
#include "mysqlnd_auth.h"
#include "mysqlnd_wireprotocol.h"
#include "mysqlnd_priv.h"
#include "mysqlnd_result.h"
#include "mysqlnd_statistics.h"
#include "mysqlnd_charset.h"
#include "mysqlnd_debug.h"
#include "mysqlnd_ext_plugin.h"
#include "zend_smart_str.h"
extern MYSQLND_CHARSET *mysqlnd_charsets;
PHPAPI const char * const mysqlnd_server_gone = "MySQL server has gone away";
PHPAPI const char * const mysqlnd_out_of_sync = "Commands out of sync; you can't run this command now";
PHPAPI const char * const mysqlnd_out_of_memory = "Out of memory";
PHPAPI MYSQLND_STATS * mysqlnd_global_stats = NULL;
/* {{{ mysqlnd_upsert_status::reset */
void
MYSQLND_METHOD(mysqlnd_upsert_status, reset)(MYSQLND_UPSERT_STATUS * const upsert_status)
{
upsert_status->warning_count = 0;
upsert_status->server_status = 0;
upsert_status->affected_rows = 0;
upsert_status->last_insert_id = 0;
}
/* }}} */
/* {{{ mysqlnd_upsert_status::set_affected_rows_to_error */
void
MYSQLND_METHOD(mysqlnd_upsert_status, set_affected_rows_to_error)(MYSQLND_UPSERT_STATUS * upsert_status)
{
upsert_status->affected_rows = (uint64_t) ~0;
}
/* }}} */
MYSQLND_CLASS_METHODS_START(mysqlnd_upsert_status)
MYSQLND_METHOD(mysqlnd_upsert_status, reset),
MYSQLND_METHOD(mysqlnd_upsert_status, set_affected_rows_to_error),
MYSQLND_CLASS_METHODS_END;
/* {{{ mysqlnd_upsert_status_init */
void
mysqlnd_upsert_status_init(MYSQLND_UPSERT_STATUS * const upsert_status)
{
upsert_status->m = &MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_upsert_status);
upsert_status->m->reset(upsert_status);
}
/* }}} */
/* {{{ mysqlnd_error_list_pdtor */
static void
mysqlnd_error_list_pdtor(void * pDest)
{
MYSQLND_ERROR_LIST_ELEMENT * element = (MYSQLND_ERROR_LIST_ELEMENT *) pDest;
DBG_ENTER("mysqlnd_error_list_pdtor");
if (element->error) {
mnd_pefree(element->error, TRUE);
}
DBG_VOID_RETURN;
}
/* }}} */
/* {{{ mysqlnd_error_info::reset */
static void
MYSQLND_METHOD(mysqlnd_error_info, reset)(MYSQLND_ERROR_INFO * const info)
{
DBG_ENTER("mysqlnd_error_info::reset");
info->error_no = 0;
info->error[0] = '\0';
memset(&info->sqlstate, 0, sizeof(info->sqlstate));
zend_llist_clean(&info->error_list);
DBG_VOID_RETURN;
}
/* }}} */
/* {{{ mysqlnd_error_info::set_client_error */
static void
MYSQLND_METHOD(mysqlnd_error_info, set_client_error)(MYSQLND_ERROR_INFO * const info,
const unsigned int err_no,
const char * const sqlstate,
const char * const error)
{
DBG_ENTER("mysqlnd_error_info::set_client_error");
if (err_no) {
MYSQLND_ERROR_LIST_ELEMENT error_for_the_list = {0};
info->error_no = err_no;
strlcpy(info->sqlstate, sqlstate, sizeof(info->sqlstate));
strlcpy(info->error, error, sizeof(info->error));
error_for_the_list.error_no = err_no;
strlcpy(error_for_the_list.sqlstate, sqlstate, sizeof(error_for_the_list.sqlstate));
error_for_the_list.error = mnd_pestrdup(error, TRUE);
if (error_for_the_list.error) {
DBG_INF_FMT("adding error [%s] to the list", error_for_the_list.error);
zend_llist_add_element(&info->error_list, &error_for_the_list);
}
} else {
info->m->reset(info);
}
DBG_VOID_RETURN;
}
/* }}} */
MYSQLND_CLASS_METHODS_START(mysqlnd_error_info)
MYSQLND_METHOD(mysqlnd_error_info, reset),
MYSQLND_METHOD(mysqlnd_error_info, set_client_error),
MYSQLND_CLASS_METHODS_END;
/* {{{ mysqlnd_error_info_init */
PHPAPI void
mysqlnd_error_info_init(MYSQLND_ERROR_INFO * const info, const bool persistent)
{
DBG_ENTER("mysqlnd_error_info_init");
info->m = mysqlnd_error_info_get_methods();
info->m->reset(info);
zend_llist_init(&info->error_list, sizeof(MYSQLND_ERROR_LIST_ELEMENT), (llist_dtor_func_t) mysqlnd_error_list_pdtor, persistent);
info->persistent = persistent;
DBG_VOID_RETURN;
}
/* }}} */
/* {{{ mysqlnd_error_info_free_contents */
PHPAPI void
mysqlnd_error_info_free_contents(MYSQLND_ERROR_INFO * const info)
{
DBG_ENTER("mysqlnd_error_info_free_contents");
info->m->reset(info);
DBG_VOID_RETURN;
}
/* }}} */
/* {{{ mysqlnd_connection_state::get */
static enum mysqlnd_connection_state
MYSQLND_METHOD(mysqlnd_connection_state, get)(const struct st_mysqlnd_connection_state * const state_struct)
{
DBG_ENTER("mysqlnd_connection_state::get");
DBG_INF_FMT("State=%u", state_struct->state);
DBG_RETURN(state_struct->state);
}
/* }}} */
/* {{{ mysqlnd_connection_state::set */
static void
MYSQLND_METHOD(mysqlnd_connection_state, set)(struct st_mysqlnd_connection_state * const state_struct, const enum mysqlnd_connection_state state)
{
DBG_ENTER("mysqlnd_connection_state::set");
DBG_INF_FMT("New state=%u", state);
state_struct->state = state;
DBG_VOID_RETURN;
}
/* }}} */
MYSQLND_CLASS_METHODS_START(mysqlnd_connection_state)
MYSQLND_METHOD(mysqlnd_connection_state, get),
MYSQLND_METHOD(mysqlnd_connection_state, set),
MYSQLND_CLASS_METHODS_END;
/* {{{ mysqlnd_connection_state_init */
PHPAPI void
mysqlnd_connection_state_init(struct st_mysqlnd_connection_state * const state)
{
DBG_ENTER("mysqlnd_connection_state_init");
state->m = &MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_connection_state);
state->state = CONN_ALLOCED;
DBG_VOID_RETURN;
}
/* }}} */
/* {{{ mysqlnd_conn_data::free_options */
static void
MYSQLND_METHOD(mysqlnd_conn_data, free_options)(MYSQLND_CONN_DATA * conn)
{
bool pers = conn->persistent;
if (conn->options->charset_name) {
mnd_pefree(conn->options->charset_name, pers);
conn->options->charset_name = NULL;
}
if (conn->options->auth_protocol) {
mnd_pefree(conn->options->auth_protocol, pers);
conn->options->auth_protocol = NULL;
}
if (conn->options->num_commands) {
unsigned int i;
for (i = 0; i < conn->options->num_commands; i++) {
/* allocated with pestrdup */
mnd_pefree(conn->options->init_commands[i], pers);
}
mnd_pefree(conn->options->init_commands, pers);
conn->options->init_commands = NULL;
}
if (conn->options->cfg_file) {
mnd_pefree(conn->options->cfg_file, pers);
conn->options->cfg_file = NULL;
}
if (conn->options->cfg_section) {
mnd_pefree(conn->options->cfg_section, pers);
conn->options->cfg_section = NULL;
}
if (conn->options->connect_attr) {
zend_hash_destroy(conn->options->connect_attr);
mnd_pefree(conn->options->connect_attr, pers);
conn->options->connect_attr = NULL;
}
if (conn->options->local_infile_directory) {
mnd_pefree(conn->options->local_infile_directory, pers);
conn->options->local_infile_directory = NULL;
}
}
/* }}} */
/* {{{ mysqlnd_conn_data::free_contents */
static void
MYSQLND_METHOD(mysqlnd_conn_data, free_contents)(MYSQLND_CONN_DATA * conn)
{
bool pers = conn->persistent;
DBG_ENTER("mysqlnd_conn_data::free_contents");
if (conn->current_result) {
conn->current_result->m.free_result(conn->current_result, TRUE);
conn->current_result = NULL;
}
if (conn->protocol_frame_codec) {
conn->protocol_frame_codec->data->m.free_contents(conn->protocol_frame_codec);
}
if (conn->vio) {
conn->vio->data->m.free_contents(conn->vio);
}
DBG_INF("Freeing memory of members");
mysqlnd_set_persistent_string(&conn->hostname, NULL, 0, pers);
mysqlnd_set_persistent_string(&conn->username, NULL, 0, pers);
mysqlnd_set_persistent_string(&conn->password, NULL, 0, pers);
mysqlnd_set_persistent_string(&conn->connect_or_select_db, NULL, 0, pers);
mysqlnd_set_persistent_string(&conn->unix_socket, NULL, 0, pers);
DBG_INF_FMT("scheme=%s", conn->scheme.s);
mysqlnd_set_persistent_string(&conn->scheme, NULL, 0, pers);
if (conn->server_version) {
mnd_pefree(conn->server_version, pers);
conn->server_version = NULL;
}
if (conn->host_info) {
mnd_pefree(conn->host_info, pers);
conn->host_info = NULL;
}
mysqlnd_set_persistent_string(&conn->authentication_plugin_data, NULL, 0, pers);
mysqlnd_set_string(&conn->last_message, NULL, 0);
conn->charset = NULL;
conn->greet_charset = NULL;
DBG_VOID_RETURN;
}
/* }}} */
/* {{{ mysqlnd_conn_data::dtor */
static void
MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, dtor)(MYSQLND_CONN_DATA * conn)
{
DBG_ENTER("mysqlnd_conn_data::dtor");
DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
conn->m->free_contents(conn);
conn->m->free_options(conn);
if (conn->error_info) {
mysqlnd_error_info_free_contents(conn->error_info);
conn->error_info = NULL;
}
if (conn->protocol_frame_codec) {
mysqlnd_pfc_free(conn->protocol_frame_codec, conn->stats, conn->error_info);
conn->protocol_frame_codec = NULL;
}
if (conn->vio) {
mysqlnd_vio_free(conn->vio, conn->stats, conn->error_info);
conn->vio = NULL;
}
if (conn->payload_decoder_factory) {
mysqlnd_protocol_payload_decoder_factory_free(conn->payload_decoder_factory);
conn->payload_decoder_factory = NULL;
}
if (conn->stats) {
mysqlnd_stats_end(conn->stats, conn->persistent);
}
mnd_pefree(conn, conn->persistent);
DBG_VOID_RETURN;
}
/* }}} */
/* {{{ mysqlnd_conn_data::set_server_option */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, set_server_option)(MYSQLND_CONN_DATA * const conn, enum_mysqlnd_server_option option)
{
DBG_ENTER("mysqlnd_conn_data::set_server_option");
DBG_RETURN(conn->command->set_option(conn, option));
}
/* }}} */
/* {{{ mysqlnd_conn_data::restart_psession */
static void
MYSQLND_METHOD(mysqlnd_conn_data, restart_psession)(MYSQLND_CONN_DATA * conn)
{
DBG_ENTER("mysqlnd_conn_data::restart_psession");
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CONNECT_REUSED);
conn->current_result = NULL;
conn->last_message.s = NULL;
DBG_VOID_RETURN;
}
/* }}} */
/* {{{ mysqlnd_conn_data::end_psession */
static void
MYSQLND_METHOD(mysqlnd_conn_data, end_psession)(MYSQLND_CONN_DATA * conn)
{
DBG_ENTER("mysqlnd_conn_data::end_psession");
/* Free here what should not be seen by the next script */
if (conn->current_result) {
conn->current_result->m.free_result(conn->current_result, TRUE);
conn->current_result = NULL;
}
mysqlnd_set_string(&conn->last_message, NULL, 0);
conn->error_info = &conn->error_info_impl;
DBG_VOID_RETURN;
}
/* }}} */
/* {{{ mysqlnd_conn_data::fetch_auth_plugin_by_name */
static struct st_mysqlnd_authentication_plugin *
MYSQLND_METHOD(mysqlnd_conn_data, fetch_auth_plugin_by_name)(const char * const requested_protocol)
{
struct st_mysqlnd_authentication_plugin * auth_plugin;
char * plugin_name = NULL;
DBG_ENTER("mysqlnd_conn_data::fetch_auth_plugin_by_name");
mnd_sprintf(&plugin_name, 0, "auth_plugin_%s", requested_protocol);
DBG_INF_FMT("looking for %s auth plugin", plugin_name);
auth_plugin = mysqlnd_plugin_find(plugin_name);
mnd_sprintf_free(plugin_name);
DBG_RETURN(auth_plugin);
}
/* }}} */
/* {{{ mysqlnd_conn_data::execute_init_commands */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, execute_init_commands)(MYSQLND_CONN_DATA * conn)
{
enum_func_status ret = PASS;
DBG_ENTER("mysqlnd_conn_data::execute_init_commands");
if (conn->options->init_commands) {
unsigned int current_command = 0;
for (; current_command < conn->options->num_commands; ++current_command) {
const char * const command = conn->options->init_commands[current_command];
if (command) {
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_INIT_COMMAND_EXECUTED_COUNT);
if (PASS != conn->m->query(conn, command, strlen(command))) {
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_INIT_COMMAND_FAILED_COUNT);
ret = FAIL;
break;
}
do {
if (conn->last_query_type == QUERY_SELECT) {
MYSQLND_RES * result = conn->m->use_result(conn);
if (result) {
result->m.free_result(result, TRUE);
}
}
} while (conn->m->next_result(conn) != FAIL);
}
}
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::get_updated_connect_flags */
static unsigned int
MYSQLND_METHOD(mysqlnd_conn_data, get_updated_connect_flags)(MYSQLND_CONN_DATA * conn, unsigned int mysql_flags)
{
#ifdef MYSQLND_COMPRESSION_ENABLED
MYSQLND_PFC * pfc = conn->protocol_frame_codec;
#endif
MYSQLND_VIO * vio = conn->vio;
DBG_ENTER("mysqlnd_conn_data::get_updated_connect_flags");
/* allow CLIENT_LOCAL_FILES capability, although extensions basing on mysqlnd
shouldn't allow 'load data local infile' by default due to security issues */
mysql_flags |= MYSQLND_CAPABILITIES;
mysql_flags |= conn->options->flags; /* use the flags from set_client_option() */
#ifndef MYSQLND_COMPRESSION_ENABLED
if (mysql_flags & CLIENT_COMPRESS) {
mysql_flags &= ~CLIENT_COMPRESS;
}
#else
if (pfc && pfc->data->flags & MYSQLND_PROTOCOL_FLAG_USE_COMPRESSION) {
mysql_flags |= CLIENT_COMPRESS;
}
#endif
#ifndef MYSQLND_SSL_SUPPORTED
if (mysql_flags & CLIENT_SSL) {
mysql_flags &= ~CLIENT_SSL;
}
#else
if (vio && (vio->data->options.ssl_key ||
vio->data->options.ssl_cert ||
vio->data->options.ssl_ca ||
vio->data->options.ssl_capath ||
vio->data->options.ssl_cipher))
{
mysql_flags |= CLIENT_SSL;
}
#endif
if (conn->options->connect_attr && zend_hash_num_elements(conn->options->connect_attr)) {
mysql_flags |= CLIENT_CONNECT_ATTRS;
}
DBG_RETURN(mysql_flags);
}
/* }}} */
/* {{{ mysqlnd_conn_data::connect_handshake */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, connect_handshake)(MYSQLND_CONN_DATA * conn,
const MYSQLND_CSTRING * const scheme,
const MYSQLND_CSTRING * const username,
const MYSQLND_CSTRING * const password,
const MYSQLND_CSTRING * const database,
const unsigned int mysql_flags)
{
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_conn_data::connect_handshake");
if (PASS == conn->vio->data->m.connect(conn->vio, *scheme, conn->persistent, conn->stats, conn->error_info)) {
conn->protocol_frame_codec->data->m.reset(conn->protocol_frame_codec, conn->stats, conn->error_info);
size_t client_flags = mysql_flags;
ret = conn->command->handshake(conn, *username, *password, *database, client_flags);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::get_scheme */
static MYSQLND_STRING
MYSQLND_METHOD(mysqlnd_conn_data, get_scheme)(MYSQLND_CONN_DATA * conn, MYSQLND_CSTRING hostname, MYSQLND_CSTRING *socket_or_pipe, unsigned int port, bool * unix_socket, bool * named_pipe)
{
MYSQLND_STRING transport;
DBG_ENTER("mysqlnd_conn_data::get_scheme");
#ifndef PHP_WIN32
if (hostname.l == sizeof("localhost") - 1 && !strncasecmp(hostname.s, "localhost", hostname.l)) {
DBG_INF_FMT("socket=%s", socket_or_pipe->s? socket_or_pipe->s:"n/a");
if (!socket_or_pipe->s) {
socket_or_pipe->s = "/tmp/mysql.sock";
socket_or_pipe->l = strlen(socket_or_pipe->s);
}
transport.l = mnd_sprintf(&transport.s, 0, "unix://%s", socket_or_pipe->s);
*unix_socket = TRUE;
#else
if (hostname.l == sizeof(".") - 1 && hostname.s[0] == '.') {
/* named pipe in socket */
if (!socket_or_pipe->s) {
socket_or_pipe->s = "\\\\.\\pipe\\MySQL";
socket_or_pipe->l = strlen(socket_or_pipe->s);
}
transport.l = mnd_sprintf(&transport.s, 0, "pipe://%s", socket_or_pipe->s);
*named_pipe = TRUE;
#endif
} else {
if (!port) {
port = 3306;
}
transport.l = mnd_sprintf(&transport.s, 0, "tcp://%s:%u", hostname.s, port);
}
DBG_INF_FMT("transport=%s", transport.s? transport.s:"OOM");
DBG_RETURN(transport);
}
/* }}} */
/* {{{ mysqlnd_conn_data::connect */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
MYSQLND_CSTRING hostname,
MYSQLND_CSTRING username,
MYSQLND_CSTRING password,
MYSQLND_CSTRING database,
unsigned int port,
MYSQLND_CSTRING socket_or_pipe,
unsigned int mysql_flags
)
{
bool unix_socket = FALSE;
bool named_pipe = FALSE;
bool reconnect = FALSE;
bool saved_compression = FALSE;
MYSQLND_PFC * pfc = conn->protocol_frame_codec;
MYSQLND_STRING transport = { NULL, 0 };
DBG_ENTER("mysqlnd_conn_data::connect");
DBG_INF_FMT("conn=%p", conn);
SET_EMPTY_ERROR(conn->error_info);
UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(conn->upsert_status);
DBG_INF_FMT("host=%s user=%s db=%s port=%u flags=%u persistent=%u state=%u",
hostname.s?hostname.s:"", username.s?username.s:"", database.s?database.s:"", port, mysql_flags,
conn->persistent, (int)GET_CONNECTION_STATE(&conn->state));
if (GET_CONNECTION_STATE(&conn->state) > CONN_ALLOCED) {
DBG_INF("Connecting on a connected handle.");
if (GET_CONNECTION_STATE(&conn->state) < CONN_QUIT_SENT) {
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CLOSE_IMPLICIT);
reconnect = TRUE;
conn->m->send_close(conn);
}
conn->m->free_contents(conn);
/* Now reconnect using the same handle */
if (pfc->data->compressed) {
/*
we need to save the state. As we will re-connect, pfc->compressed should be off, or
we will look for a compression header as part of the greet message, but there will
be none.
*/
saved_compression = TRUE;
pfc->data->compressed = FALSE;
}
if (pfc->data->ssl) {
pfc->data->ssl = FALSE;
}
} else {
unsigned int max_allowed_size = MYSQLND_ASSEMBLED_PACKET_MAX_SIZE;
conn->m->set_client_option(conn, MYSQLND_OPT_MAX_ALLOWED_PACKET, (char *)&max_allowed_size);
}
if (!hostname.s || !hostname.s[0]) {
hostname.s = "localhost";
hostname.l = strlen(hostname.s);
}
if (!username.s) {
DBG_INF_FMT("no user given, using empty string");
username.s = "";
username.l = 0;
}
if (!password.s) {
DBG_INF_FMT("no password given, using empty string");
password.s = "";
password.l = 0;
}
if (!database.s || !database.s[0]) {
DBG_INF_FMT("no db given, using empty string");
database.s = "";
database.l = 0;
} else {
mysql_flags |= CLIENT_CONNECT_WITH_DB;
}
transport = conn->m->get_scheme(conn, hostname, &socket_or_pipe, port, &unix_socket, &named_pipe);
mysql_flags = conn->m->get_updated_connect_flags(conn, mysql_flags);
{
const MYSQLND_CSTRING scheme = { transport.s, transport.l };
if (FAIL == conn->m->connect_handshake(conn, &scheme, &username, &password, &database, mysql_flags)) {
goto err;
}
}
{
SET_CONNECTION_STATE(&conn->state, CONN_READY);
if (saved_compression) {
pfc->data->compressed = TRUE;
}
/*
If a connect on a existing handle is performed and mysql_flags is
passed which doesn't CLIENT_COMPRESS, then we need to overwrite the value
which we set based on saved_compression.
*/
pfc->data->compressed = mysql_flags & CLIENT_COMPRESS? TRUE:FALSE;
mysqlnd_set_persistent_string(&conn->scheme, transport.s, transport.l, conn->persistent);
if (transport.s) {
mnd_sprintf_free(transport.s);
transport.s = NULL;
}
if (!conn->scheme.s) {
goto err; /* OOM */
}
mysqlnd_set_persistent_string(&conn->username, username.s, username.l, conn->persistent);
mysqlnd_set_persistent_string(&conn->password, username.s, password.l, conn->persistent);
conn->port = port;
mysqlnd_set_persistent_string(&conn->connect_or_select_db, database.s, database.l, conn->persistent);
if (!unix_socket && !named_pipe) {
mysqlnd_set_persistent_string(&conn->hostname, hostname.s, hostname.l, conn->persistent);
{
char *p;
mnd_sprintf(&p, 0, "%s via TCP/IP", conn->hostname.s);
if (!p) {
SET_OOM_ERROR(conn->error_info);
goto err; /* OOM */
}
conn->host_info = mnd_pestrdup(p, conn->persistent);
mnd_sprintf_free(p);
}
} else {
conn->unix_socket.s = mnd_pestrdup(socket_or_pipe.s, conn->persistent);
if (unix_socket) {
conn->host_info = mnd_pestrdup("Localhost via UNIX socket", conn->persistent);
} else if (named_pipe) {
char *p;
mnd_sprintf(&p, 0, "%s via named pipe", conn->unix_socket.s);
if (!p) {
SET_OOM_ERROR(conn->error_info);
goto err; /* OOM */
}
conn->host_info = mnd_pestrdup(p, conn->persistent);
mnd_sprintf_free(p);
} else {
php_error_docref(NULL, E_WARNING, "Impossible. Should be either socket or a pipe. Report a bug!");
}
if (!conn->unix_socket.s || !conn->host_info) {
SET_OOM_ERROR(conn->error_info);
goto err; /* OOM */
}
conn->unix_socket.l = strlen(conn->unix_socket.s);
}
SET_EMPTY_ERROR(conn->error_info);
mysqlnd_local_infile_default(conn);
if (FAIL == conn->m->execute_init_commands(conn)) {
goto err;
}
MYSQLND_INC_CONN_STATISTIC_W_VALUE2(conn->stats, STAT_CONNECT_SUCCESS, 1, STAT_OPENED_CONNECTIONS, 1);
if (reconnect) {
MYSQLND_INC_GLOBAL_STATISTIC(STAT_RECONNECT);
}
if (conn->persistent) {
MYSQLND_INC_CONN_STATISTIC_W_VALUE2(conn->stats, STAT_PCONNECT_SUCCESS, 1, STAT_OPENED_PERSISTENT_CONNECTIONS, 1);
}
DBG_INF_FMT("connection_id=%" PRIu64, conn->thread_id);
DBG_RETURN(PASS);
}
err:
DBG_ERR_FMT("[%u] %.128s (trying to connect via %s)", conn->error_info->error_no, conn->error_info->error, transport.s ? transport.s : conn->scheme.s);
if (!conn->error_info->error_no) {
/* There was an unknown error if the connection failed but we have no error number */
char * msg;
mnd_sprintf(&msg, 0, "Unknown error while trying to connect via %s", transport.s ? transport.s : conn->scheme.s);
SET_CLIENT_ERROR(conn->error_info, CR_CONNECTION_ERROR, UNKNOWN_SQLSTATE, msg);
mnd_sprintf_free(msg);
}
if (transport.s) {
mnd_sprintf_free(transport.s);
transport.s = NULL;
}
conn->m->free_contents(conn);
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_CONNECT_FAILURE);
DBG_RETURN(FAIL);
}
/* }}} */
/* {{{ mysqlnd_conn::connect */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn, connect)(MYSQLND * conn_handle,
const MYSQLND_CSTRING hostname,
const MYSQLND_CSTRING username,
const MYSQLND_CSTRING password,
const MYSQLND_CSTRING database,
unsigned int port,
const MYSQLND_CSTRING socket_or_pipe,
unsigned int mysql_flags)
{
MYSQLND_CONN_DATA * conn = conn_handle->data;
DBG_ENTER("mysqlnd_conn::connect");
mysqlnd_options4(conn_handle, MYSQL_OPT_CONNECT_ATTR_ADD, "_client_name", "mysqlnd");
if (hostname.l > 0) {
mysqlnd_options4(conn_handle, MYSQL_OPT_CONNECT_ATTR_ADD, "_server_host", hostname.s);
}
DBG_RETURN(conn->m->connect(conn, hostname, username, password, database, port, socket_or_pipe, mysql_flags));
}
/* }}} */
/* {{{ mysqlnd_conn_data::query */
/*
If conn->error_info->error_no is not zero, then we had an error.
Still the result from the query is PASS
*/
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, query)(MYSQLND_CONN_DATA * conn, const char * const query, const size_t query_len)
{
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_conn_data::query");
DBG_INF_FMT("conn=%p conn=%" PRIu64 " query=%s", conn, conn->thread_id, query);
if (PASS == conn->m->send_query(conn, query, query_len, NULL, NULL) &&
PASS == conn->m->reap_query(conn))
{
ret = PASS;
if (conn->last_query_type == QUERY_UPSERT && UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status)) {
MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn->stats, STAT_ROWS_AFFECTED_NORMAL, UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status));
}
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::send_query */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, send_query)(
MYSQLND_CONN_DATA * conn, const char * const query, const size_t query_len,
zval *read_cb, zval *err_cb)
{
DBG_ENTER("mysqlnd_conn_data::send_query");
DBG_INF_FMT("conn=%" PRIu64 " query=%s", conn->thread_id, query);
DBG_INF_FMT("conn->server_status=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
const MYSQLND_CSTRING query_string = {query, query_len};
enum_func_status ret = conn->command->query(conn, query_string);
DBG_INF_FMT("conn->server_status=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::reap_query */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, reap_query)(MYSQLND_CONN_DATA * conn)
{
DBG_ENTER("mysqlnd_conn_data::reap_query");
DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
DBG_INF_FMT("conn->server_status=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
enum_func_status ret = conn->command->reap_result(conn);
DBG_INF_FMT("conn->server_status=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::list_method */
MYSQLND_RES *
MYSQLND_METHOD(mysqlnd_conn_data, list_method)(MYSQLND_CONN_DATA * conn, const char * const query, const char * const achtung_wild, const char * const par1)
{
char * show_query = NULL;
size_t show_query_len;
MYSQLND_RES * result = NULL;
DBG_ENTER("mysqlnd_conn_data::list_method");
DBG_INF_FMT("conn=%" PRIu64 " query=%s wild=%p", conn->thread_id, query, achtung_wild);
if (par1) {
if (achtung_wild) {
show_query_len = mnd_sprintf(&show_query, 0, query, par1, achtung_wild);
} else {
show_query_len = mnd_sprintf(&show_query, 0, query, par1);
}
} else {
if (achtung_wild) {
show_query_len = mnd_sprintf(&show_query, 0, query, achtung_wild);
} else {
show_query_len = strlen(show_query = (char *)query);
}
}
if (PASS == conn->m->query(conn, show_query, show_query_len)) {
result = conn->m->store_result(conn);
}
if (show_query != query) {
mnd_sprintf_free(show_query);
}
DBG_RETURN(result);
}
/* }}} */
/* {{{ mysqlnd_conn_data::err_no */
static unsigned int
MYSQLND_METHOD(mysqlnd_conn_data, err_no)(const MYSQLND_CONN_DATA * const conn)
{
return conn->error_info->error_no;
}
/* }}} */
/* {{{ mysqlnd_conn_data::error */
static const char *
MYSQLND_METHOD(mysqlnd_conn_data, error)(const MYSQLND_CONN_DATA * const conn)
{
return conn->error_info->error;
}
/* }}} */
/* {{{ mysqlnd_conn_data::sqlstate */
static const char *
MYSQLND_METHOD(mysqlnd_conn_data, sqlstate)(const MYSQLND_CONN_DATA * const conn)
{
return conn->error_info->sqlstate[0] ? conn->error_info->sqlstate:MYSQLND_SQLSTATE_NULL;
}
/* }}} */
/* {{{ mysqlnd_old_escape_string */
PHPAPI zend_ulong
mysqlnd_old_escape_string(char * newstr, const char * escapestr, size_t escapestr_len)
{
DBG_ENTER("mysqlnd_old_escape_string");
DBG_RETURN(mysqlnd_cset_escape_slashes(mysqlnd_find_charset_name("latin1"), newstr, escapestr, escapestr_len));
}
/* }}} */
/* {{{ mysqlnd_conn_data::ssl_set */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, ssl_set)(MYSQLND_CONN_DATA * const conn, const char * key, const char * const cert,
const char * const ca, const char * const capath, const char * const cipher)
{
MYSQLND_VIO * vio = conn->vio;
DBG_ENTER("mysqlnd_conn_data::ssl_set");
enum_func_status ret = (
PASS == vio->data->m.set_client_option(vio, MYSQLND_OPT_SSL_KEY, key) &&
PASS == vio->data->m.set_client_option(vio, MYSQLND_OPT_SSL_CERT, cert) &&
PASS == vio->data->m.set_client_option(vio, MYSQLND_OPT_SSL_CA, ca) &&
PASS == vio->data->m.set_client_option(vio, MYSQLND_OPT_SSL_CAPATH, capath) &&
PASS == vio->data->m.set_client_option(vio, MYSQLND_OPT_SSL_CIPHER, cipher)) ? PASS : FAIL;
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::escape_string */
static zend_ulong
MYSQLND_METHOD(mysqlnd_conn_data, escape_string)(MYSQLND_CONN_DATA * const conn, char * newstr, const char * escapestr, size_t escapestr_len)
{
zend_ulong ret = FAIL;
DBG_ENTER("mysqlnd_conn_data::escape_string");
DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
DBG_INF_FMT("server_status=%u", UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status));
if (UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status) & SERVER_STATUS_NO_BACKSLASH_ESCAPES) {
ret = mysqlnd_cset_escape_quotes(conn->charset, newstr, escapestr, escapestr_len);
} else {
ret = mysqlnd_cset_escape_slashes(conn->charset, newstr, escapestr, escapestr_len);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::dump_debug_info */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, dump_debug_info)(MYSQLND_CONN_DATA * const conn)
{
DBG_ENTER("mysqlnd_conn_data::dump_debug_info");
DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
DBG_RETURN(conn->command->debug(conn));
}
/* }}} */
/* {{{ mysqlnd_conn_data::select_db */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, select_db)(MYSQLND_CONN_DATA * const conn, const char * const db, const size_t db_len)
{
DBG_ENTER("mysqlnd_conn_data::select_db");
DBG_INF_FMT("conn=%" PRIu64 " db=%s", conn->thread_id, db);
const MYSQLND_CSTRING database = {db, db_len};
DBG_RETURN(conn->command->init_db(conn, database));
}
/* }}} */
/* {{{ mysqlnd_conn_data::ping */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, ping)(MYSQLND_CONN_DATA * const conn)
{
DBG_ENTER("mysqlnd_conn_data::ping");
DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
enum_func_status ret = conn->command->ping(conn);
DBG_INF_FMT("ret=%u", ret);
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::statistic */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, statistic)(MYSQLND_CONN_DATA * conn, zend_string **message)
{
DBG_ENTER("mysqlnd_conn_data::statistic");
DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
DBG_RETURN(conn->command->statistics(conn, message));
}
/* }}} */
/* {{{ mysqlnd_conn_data::kill */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, kill)(MYSQLND_CONN_DATA * conn, unsigned int pid)
{
DBG_ENTER("mysqlnd_conn_data::kill");
DBG_INF_FMT("conn=%" PRIu64 " pid=%u", conn->thread_id, pid);
const unsigned int process_id = pid;
/* 'unsigned char' is promoted to 'int' when passed through '...' */
const unsigned int read_response = (pid != conn->thread_id);
DBG_RETURN(conn->command->process_kill(conn, process_id, read_response));
}
/* }}} */
/* {{{ mysqlnd_conn_data::set_charset */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, set_charset)(MYSQLND_CONN_DATA * const conn, const char * const csname)
{
enum_func_status ret = FAIL;
const MYSQLND_CHARSET * const charset = mysqlnd_find_charset_name(csname);
DBG_ENTER("mysqlnd_conn_data::set_charset");
DBG_INF_FMT("conn=%" PRIu64 " cs=%s", conn->thread_id, csname);
if (!charset) {
SET_CLIENT_ERROR(conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE, "Invalid character set was provided");
DBG_RETURN(ret);
}
char * query;
size_t query_len = mnd_sprintf(&query, 0, "SET NAMES %s", csname);
if (FAIL == (ret = conn->m->query(conn, query, query_len)) || conn->error_info->error_no) {
ret = FAIL;
} else {
conn->charset = charset;
}
mnd_sprintf_free(query);
DBG_INF(ret == PASS? "PASS":"FAIL");
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::refresh */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, refresh)(MYSQLND_CONN_DATA * const conn, uint8_t options)
{
DBG_ENTER("mysqlnd_conn_data::refresh");
DBG_INF_FMT("conn=%" PRIu64 " options=%u", conn->thread_id, options);
DBG_RETURN(conn->command->refresh(conn, options));
}
/* }}} */
/* {{{ mysqlnd_send_close */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, send_close)(MYSQLND_CONN_DATA * const conn)
{
enum_func_status ret = PASS;
MYSQLND_VIO * vio = conn->vio;
php_stream * net_stream = vio->data->m.get_stream(vio);
enum mysqlnd_connection_state state = GET_CONNECTION_STATE(&conn->state);
DBG_ENTER("mysqlnd_send_close");
DBG_INF_FMT("conn=%" PRIu64 " vio->data->stream->abstract=%p", conn->thread_id, net_stream? net_stream->abstract:NULL);
DBG_INF_FMT("state=%u", state);
if (state >= CONN_READY) {
MYSQLND_DEC_GLOBAL_STATISTIC(STAT_OPENED_CONNECTIONS);
if (conn->persistent) {
MYSQLND_DEC_GLOBAL_STATISTIC(STAT_OPENED_PERSISTENT_CONNECTIONS);
}
}
switch (state) {
case CONN_READY:
DBG_INF("Connection clean, sending COM_QUIT");
if (net_stream) {
ret = conn->command->quit(conn);
vio->data->m.close_stream(vio, conn->stats, conn->error_info);
}
SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT);
break;
case CONN_SENDING_LOAD_DATA:
/*
Don't send COM_QUIT if we are in a middle of a LOAD DATA or we
will crash (assert) a debug server.
*/
case CONN_NEXT_RESULT_PENDING:
case CONN_QUERY_SENT:
case CONN_FETCHING_DATA:
MYSQLND_INC_GLOBAL_STATISTIC(STAT_CLOSE_IN_MIDDLE);
DBG_ERR_FMT("Brutally closing connection [%p][%s]", conn, conn->scheme.s);
/*
Do nothing, the connection will be brutally closed
and the server will catch it and free close from its side.
*/
ZEND_FALLTHROUGH;
case CONN_ALLOCED:
/*
Allocated but not connected or there was failure when trying
to connect with pre-allocated connect.
Fall-through
*/
SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT);
ZEND_FALLTHROUGH;
case CONN_QUIT_SENT:
/* The user has killed its own connection */
vio->data->m.close_stream(vio, conn->stats, conn->error_info);
break;
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::get_reference */
static MYSQLND_CONN_DATA *
MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_reference)(MYSQLND_CONN_DATA * const conn)
{
DBG_ENTER("mysqlnd_conn_data::get_reference");
++conn->refcount;
DBG_INF_FMT("conn=%" PRIu64 " new_refcount=%u", conn->thread_id, conn->refcount);
DBG_RETURN(conn);
}
/* }}} */
/* {{{ mysqlnd_conn_data::free_reference */
static enum_func_status
MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, free_reference)(MYSQLND_CONN_DATA * const conn)
{
enum_func_status ret = PASS;
DBG_ENTER("mysqlnd_conn_data::free_reference");
DBG_INF_FMT("conn=%" PRIu64 " old_refcount=%u", conn->thread_id, conn->refcount);
if (!(--conn->refcount)) {
/*
No multithreading issues as we don't share the connection :)
This will free the object too, of course because references has
reached zero.
*/
ret = conn->m->send_close(conn);
conn->m->dtor(conn);
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::field_count */
static unsigned int
MYSQLND_METHOD(mysqlnd_conn_data, field_count)(const MYSQLND_CONN_DATA * const conn)
{
return conn->field_count;
}
/* }}} */
/* {{{ mysqlnd_conn_data::server_status */
static unsigned int
MYSQLND_METHOD(mysqlnd_conn_data, server_status)(const MYSQLND_CONN_DATA * const conn)
{
return UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status);
}
/* }}} */
/* {{{ mysqlnd_conn_data::insert_id */
static uint64_t
MYSQLND_METHOD(mysqlnd_conn_data, insert_id)(const MYSQLND_CONN_DATA * const conn)
{
return UPSERT_STATUS_GET_LAST_INSERT_ID(conn->upsert_status);
}
/* }}} */
/* {{{ mysqlnd_conn_data::affected_rows */
static uint64_t
MYSQLND_METHOD(mysqlnd_conn_data, affected_rows)(const MYSQLND_CONN_DATA * const conn)
{
return UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status);
}
/* }}} */
/* {{{ mysqlnd_conn_data::warning_count */
static unsigned int
MYSQLND_METHOD(mysqlnd_conn_data, warning_count)(const MYSQLND_CONN_DATA * const conn)
{
return UPSERT_STATUS_GET_WARNINGS(conn->upsert_status);
}
/* }}} */
/* {{{ mysqlnd_conn_data::info */
static const char *
MYSQLND_METHOD(mysqlnd_conn_data, info)(const MYSQLND_CONN_DATA * const conn)
{
return conn->last_message.s;
}
/* }}} */
/* {{{ mysqlnd_get_client_info */
PHPAPI const char * mysqlnd_get_client_info(void)
{
return PHP_MYSQLND_VERSION;
}
/* }}} */
/* {{{ mysqlnd_get_client_version */
PHPAPI unsigned long mysqlnd_get_client_version(void)
{
return MYSQLND_VERSION_ID;
}
/* }}} */
/* {{{ mysqlnd_conn_data::get_server_info */
static const char *
MYSQLND_METHOD(mysqlnd_conn_data, get_server_info)(const MYSQLND_CONN_DATA * const conn)
{
return conn->server_version;
}
/* }}} */
/* {{{ mysqlnd_conn_data::get_host_info */
static const char *
MYSQLND_METHOD(mysqlnd_conn_data, get_host_info)(const MYSQLND_CONN_DATA * const conn)
{
return conn->host_info;
}
/* }}} */
/* {{{ mysqlnd_conn_data::get_proto_info */
static unsigned int
MYSQLND_METHOD(mysqlnd_conn_data, get_proto_info)(const MYSQLND_CONN_DATA * const conn)
{
return conn->protocol_version;
}
/* }}} */
/* {{{ mysqlnd_conn_data::charset_name */
static const char *
MYSQLND_METHOD(mysqlnd_conn_data, charset_name)(const MYSQLND_CONN_DATA * const conn)
{
return conn->charset->name;
}
/* }}} */
/* {{{ mysqlnd_conn_data::thread_id */
static uint64_t
MYSQLND_METHOD(mysqlnd_conn_data, thread_id)(const MYSQLND_CONN_DATA * const conn)
{
return conn->thread_id;
}
/* }}} */
/* {{{ mysqlnd_conn_data::get_server_version */
static zend_ulong
MYSQLND_METHOD(mysqlnd_conn_data, get_server_version)(const MYSQLND_CONN_DATA * const conn)
{
zend_long major, minor, patch;
char *p;
if (!(p = conn->server_version)) {
return 0;
}
major = ZEND_STRTOL(p, &p, 10);
p += 1; /* consume the dot */
minor = ZEND_STRTOL(p, &p, 10);
p += 1; /* consume the dot */
patch = ZEND_STRTOL(p, &p, 10);
return (zend_ulong)(major * Z_L(10000) + (zend_ulong)(minor * Z_L(100) + patch));
}
/* }}} */
/* {{{ mysqlnd_conn_data::more_results */
static bool
MYSQLND_METHOD(mysqlnd_conn_data, more_results)(const MYSQLND_CONN_DATA * const conn)
{
DBG_ENTER("mysqlnd_conn_data::more_results");
/* (conn->state == CONN_NEXT_RESULT_PENDING) too */
DBG_RETURN(UPSERT_STATUS_GET_SERVER_STATUS(conn->upsert_status) & SERVER_MORE_RESULTS_EXISTS? TRUE:FALSE);
}
/* }}} */
/* {{{ mysqlnd_conn_data::next_result */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, next_result)(MYSQLND_CONN_DATA * const conn)
{
DBG_ENTER("mysqlnd_conn_data::next_result");
DBG_INF_FMT("conn=%" PRIu64 "", conn->thread_id);
SET_EMPTY_ERROR(conn->error_info);
if (GET_CONNECTION_STATE(&conn->state) != CONN_NEXT_RESULT_PENDING) {
DBG_RETURN(FAIL);
}
UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(conn->upsert_status);
/*
We are sure that there is a result set, since conn->state is set accordingly
in mysqlnd_store_result() or mysqlnd_fetch_row_unbuffered()
*/
enum_func_status ret = conn->m->query_read_result_set_header(conn, NULL);
if (FAIL == ret) {
/*
There can be an error in the middle of a multi-statement, which will cancel the multi-statement.
So there are no more results and we should just return FALSE, error_no has been set
*/
if (!conn->error_info->error_no) {
DBG_ERR_FMT("Serious error. %s::%u", __FILE__, __LINE__);
php_error_docref(NULL, E_WARNING, "Serious error. PID=%d", getpid());
SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT);
conn->m->send_close(conn);
} else {
DBG_INF_FMT("Error from the server : (%u) %s", conn->error_info->error_no, conn->error_info->error);
}
DBG_RETURN(FAIL);
}
if (conn->last_query_type == QUERY_UPSERT && UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status)) {
MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn->stats, STAT_ROWS_AFFECTED_NORMAL, UPSERT_STATUS_GET_AFFECTED_ROWS(conn->upsert_status));
}
DBG_RETURN(PASS);
}
/* }}} */
/* {{{ mysqlnd_conn_data::change_user */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, change_user)(MYSQLND_CONN_DATA * const conn,
const char * user,
const char * passwd,
const char * db,
bool silent,
size_t passwd_len
)
{
enum_func_status ret = FAIL;
DBG_ENTER("mysqlnd_conn_data::change_user");
DBG_INF_FMT("conn=%" PRIu64 " user=%s passwd=%s db=%s silent=%u",
conn->thread_id, user?user:"", passwd?"***":"null", db?db:"", silent == TRUE);
SET_EMPTY_ERROR(conn->error_info);
UPSERT_STATUS_SET_AFFECTED_ROWS_TO_ERROR(conn->upsert_status);
if (!user) {
user = "";
}
if (!passwd) {
passwd = "";
passwd_len = 0;
}
if (!db) {
db = "";
}
/* XXX: passwords that have \0 inside work during auth, but in this case won't work with change user */
ret = mysqlnd_run_authentication(conn, user, passwd, passwd_len, db, strlen(db),
conn->authentication_plugin_data, conn->options->auth_protocol,
0 /*charset not used*/, conn->options, conn->server_capabilities, silent, TRUE/*is_change*/);
/*
Here we should close all statements. Unbuffered queries should not be a
problem as we won't allow sending COM_CHANGE_USER.
*/
DBG_INF(ret == PASS? "PASS":"FAIL");
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::set_client_option */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, set_client_option)(MYSQLND_CONN_DATA * const conn,
enum_mysqlnd_client_option option,
const char * const value
)
{
enum_func_status ret = PASS;
DBG_ENTER("mysqlnd_conn_data::set_client_option");
DBG_INF_FMT("conn=%" PRIu64 " option=%u", conn->thread_id, option);
switch (option) {
case MYSQL_OPT_READ_TIMEOUT:
case MYSQL_OPT_WRITE_TIMEOUT:
case MYSQLND_OPT_SSL_KEY:
case MYSQLND_OPT_SSL_CERT:
case MYSQLND_OPT_SSL_CA:
case MYSQLND_OPT_SSL_CAPATH:
case MYSQLND_OPT_SSL_CIPHER:
case MYSQL_OPT_SSL_VERIFY_SERVER_CERT:
case MYSQL_OPT_CONNECT_TIMEOUT:
case MYSQLND_OPT_NET_READ_BUFFER_SIZE:
ret = conn->vio->data->m.set_client_option(conn->vio, option, value);
break;
case MYSQLND_OPT_NET_CMD_BUFFER_SIZE:
case MYSQL_OPT_COMPRESS:
case MYSQL_SERVER_PUBLIC_KEY:
ret = conn->protocol_frame_codec->data->m.set_client_option(conn->protocol_frame_codec, option, value);
break;
case MYSQLND_OPT_INT_AND_FLOAT_NATIVE:
conn->options->int_and_float_native = *(unsigned int*) value;
break;
case MYSQL_OPT_LOCAL_INFILE:
if (value && (*(unsigned int*) value) ? 1 : 0) {
conn->options->flags |= CLIENT_LOCAL_FILES;
} else {
conn->options->flags &= ~CLIENT_LOCAL_FILES;
}
break;
case MYSQL_OPT_LOAD_DATA_LOCAL_DIR:
{
if (conn->options->local_infile_directory) {
mnd_pefree(conn->options->local_infile_directory, conn->persistent);
}
if (!value || (*value == '\0')) {
conn->options->local_infile_directory = NULL;
} else {
conn->options->local_infile_directory = mnd_pestrdup(value, conn->persistent);
}
break;
}
case MYSQL_INIT_COMMAND:
{
char ** new_init_commands;
char * new_command;
/* when num_commands is 0, then realloc will be effectively a malloc call, internally */
/* Don't assign to conn->options->init_commands because in case of OOM we will lose the pointer and leak */
new_init_commands = mnd_perealloc(conn->options->init_commands, sizeof(char *) * (conn->options->num_commands + 1), conn->persistent);
conn->options->init_commands = new_init_commands;
new_command = mnd_pestrdup(value, conn->persistent);
conn->options->init_commands[conn->options->num_commands] = new_command;
++conn->options->num_commands;
break;
}
case MYSQL_READ_DEFAULT_FILE:
case MYSQL_READ_DEFAULT_GROUP:
/* currently not supported. Todo!! */
break;
case MYSQL_SET_CHARSET_NAME:
{
char * new_charset_name;
if (!mysqlnd_find_charset_name(value)) {
SET_CLIENT_ERROR(conn->error_info, CR_CANT_FIND_CHARSET, UNKNOWN_SQLSTATE, "Unknown character set");
ret = FAIL;
break;
}
new_charset_name = mnd_pestrdup(value, conn->persistent);
if (conn->options->charset_name) {
mnd_pefree(conn->options->charset_name, conn->persistent);
}
conn->options->charset_name = new_charset_name;
DBG_INF_FMT("charset=%s", conn->options->charset_name);
break;
}
case MYSQL_OPT_NAMED_PIPE:
conn->options->protocol = MYSQL_PROTOCOL_PIPE;
break;
case MYSQL_OPT_PROTOCOL:
if (*(unsigned int*) value < MYSQL_PROTOCOL_LAST) {
conn->options->protocol = *(unsigned int*) value;
}
break;
case MYSQLND_OPT_MAX_ALLOWED_PACKET:
if (*(unsigned int*) value > (1<<16)) {
conn->options->max_allowed_packet = *(unsigned int*) value;
}
break;
case MYSQLND_OPT_AUTH_PROTOCOL:
{
char * new_auth_protocol = value? mnd_pestrdup(value, conn->persistent) : NULL;
if (conn->options->auth_protocol) {
mnd_pefree(conn->options->auth_protocol, conn->persistent);
}
conn->options->auth_protocol = new_auth_protocol;
DBG_INF_FMT("auth_protocol=%s", conn->options->auth_protocol);
break;
}
case MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS:
if (value && (*(unsigned int*) value) ? 1 : 0) {
conn->options->flags |= CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS;
} else {
conn->options->flags &= ~CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS;
}
break;
case MYSQL_OPT_CONNECT_ATTR_RESET:
if (conn->options->connect_attr) {
DBG_INF_FMT("Before reset %d attribute(s)", zend_hash_num_elements(conn->options->connect_attr));
zend_hash_clean(conn->options->connect_attr);
}
break;
case MYSQL_OPT_CONNECT_ATTR_DELETE:
if (conn->options->connect_attr && value) {
DBG_INF_FMT("Before delete %d attribute(s)", zend_hash_num_elements(conn->options->connect_attr));
zend_hash_str_del(conn->options->connect_attr, value, strlen(value));
DBG_INF_FMT("%d left", zend_hash_num_elements(conn->options->connect_attr));
}
break;
default:
ret = FAIL;
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::set_client_option_2d */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, set_client_option_2d)(MYSQLND_CONN_DATA * const conn,
const enum_mysqlnd_client_option option,
const char * const key,
const char * const value
)
{
enum_func_status ret = PASS;
DBG_ENTER("mysqlnd_conn_data::set_client_option_2d");
DBG_INF_FMT("conn=%" PRIu64 " option=%u", conn->thread_id, option);
switch (option) {
case MYSQL_OPT_CONNECT_ATTR_ADD:
if (!conn->options->connect_attr) {
DBG_INF("Initializing connect_attr hash");
conn->options->connect_attr = mnd_pemalloc(sizeof(HashTable), conn->persistent);
zend_hash_init(conn->options->connect_attr, 0, NULL, conn->persistent ? ZVAL_INTERNAL_PTR_DTOR : ZVAL_PTR_DTOR, conn->persistent);
}
DBG_INF_FMT("Adding [%s][%s]", key, value);
{
zval attrz;
zend_string *str;
if (conn->persistent) {
str = zend_string_init(key, strlen(key), 1);
GC_MAKE_PERSISTENT_LOCAL(str);
ZVAL_NEW_STR(&attrz, zend_string_init(value, strlen(value), 1));
GC_MAKE_PERSISTENT_LOCAL(Z_COUNTED(attrz));
} else {
str = zend_string_init(key, strlen(key), 0);
ZVAL_NEW_STR(&attrz, zend_string_init(value, strlen(value), 0));
}
zend_hash_update(conn->options->connect_attr, str, &attrz);
zend_string_release_ex(str, 1);
}
break;
default:
ret = FAIL;
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::use_result */
static MYSQLND_RES *
MYSQLND_METHOD(mysqlnd_conn_data, use_result)(MYSQLND_CONN_DATA * const conn)
{
DBG_ENTER("mysqlnd_conn_data::use_result");
DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
if (!conn->current_result) {
DBG_RETURN(NULL);
}
/* Nothing to store for UPSERT/LOAD DATA */
if (conn->last_query_type != QUERY_SELECT || GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA) {
SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
DBG_ERR("Command out of sync");
DBG_RETURN(NULL);
}
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_UNBUFFERED_SETS);
conn->current_result->conn = conn->m->get_reference(conn);
MYSQLND_RES *result = conn->current_result->m.use_result(conn->current_result, FALSE);
if (!result) {
conn->current_result->m.free_result(conn->current_result, TRUE);
}
conn->current_result = NULL;
DBG_RETURN(result);
}
/* }}} */
/* {{{ mysqlnd_conn_data::store_result */
static MYSQLND_RES *
MYSQLND_METHOD(mysqlnd_conn_data, store_result)(MYSQLND_CONN_DATA * const conn)
{
DBG_ENTER("mysqlnd_conn_data::store_result");
DBG_INF_FMT("conn=%" PRIu64 " conn=%p", conn->thread_id, conn);
if (!conn->current_result) {
DBG_RETURN(NULL);
}
/* Nothing to store for UPSERT/LOAD DATA*/
if (conn->last_query_type != QUERY_SELECT || GET_CONNECTION_STATE(&conn->state) != CONN_FETCHING_DATA) {
SET_CLIENT_ERROR(conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
DBG_ERR("Command out of sync");
DBG_RETURN(NULL);
}
MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_BUFFERED_SETS);
MYSQLND_RES *result = conn->current_result->m.store_result(conn->current_result, conn, NULL);
if (!result) {
conn->current_result->m.free_result(conn->current_result, TRUE);
}
conn->current_result = NULL;
DBG_RETURN(result);
}
/* }}} */
/* {{{ mysqlnd_conn_data::get_connection_stats */
static void
MYSQLND_METHOD(mysqlnd_conn_data, get_connection_stats)(const MYSQLND_CONN_DATA * const conn,
zval * return_value ZEND_FILE_LINE_DC)
{
DBG_ENTER("mysqlnd_conn_data::get_connection_stats");
mysqlnd_fill_stats_hash(conn->stats, mysqlnd_stats_values_names, return_value ZEND_FILE_LINE_CC);
DBG_VOID_RETURN;
}
/* }}} */
/* {{{ mysqlnd_conn_data::set_autocommit */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, set_autocommit)(MYSQLND_CONN_DATA * conn, unsigned int mode)
{
DBG_ENTER("mysqlnd_conn_data::set_autocommit");
DBG_RETURN(conn->m->query(conn, (mode) ? "SET AUTOCOMMIT=1":"SET AUTOCOMMIT=0", sizeof("SET AUTOCOMMIT=1") - 1));
}
/* }}} */
/* {{{ mysqlnd_conn_data::tx_commit */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, tx_commit)(MYSQLND_CONN_DATA * conn)
{
return conn->m->tx_commit_or_rollback(conn, TRUE, TRANS_COR_NO_OPT, NULL);
}
/* }}} */
/* {{{ mysqlnd_conn_data::tx_rollback */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, tx_rollback)(MYSQLND_CONN_DATA * conn)
{
return conn->m->tx_commit_or_rollback(conn, FALSE, TRANS_COR_NO_OPT, NULL);
}
/* }}} */
/* {{{ mysqlnd_tx_cor_options_to_string */
static void
MYSQLND_METHOD(mysqlnd_conn_data, tx_cor_options_to_string)(const MYSQLND_CONN_DATA * const conn, smart_str * str, const unsigned int mode)
{
if ((mode & TRANS_COR_AND_CHAIN) && !(mode & TRANS_COR_AND_NO_CHAIN)) {
if (str->s && ZSTR_LEN(str->s)) {
smart_str_appendl(str, " ", sizeof(" ") - 1);
}
smart_str_appendl(str, "AND CHAIN", sizeof("AND CHAIN") - 1);
} else if ((mode & TRANS_COR_AND_NO_CHAIN) && !(mode & TRANS_COR_AND_CHAIN)) {
if (str->s && ZSTR_LEN(str->s)) {
smart_str_appendl(str, " ", sizeof(" ") - 1);
}
smart_str_appendl(str, "AND NO CHAIN", sizeof("AND NO CHAIN") - 1);
}
if ((mode & TRANS_COR_RELEASE) && !(mode & TRANS_COR_NO_RELEASE)) {
if (str->s && ZSTR_LEN(str->s)) {
smart_str_appendl(str, " ", sizeof(" ") - 1);
}
smart_str_appendl(str, "RELEASE", sizeof("RELEASE") - 1);
} else if ((mode & TRANS_COR_NO_RELEASE) && !(mode & TRANS_COR_RELEASE)) {
if (str->s && ZSTR_LEN(str->s)) {
smart_str_appendl(str, " ", sizeof(" ") - 1);
}
smart_str_appendl(str, "NO RELEASE", sizeof("NO RELEASE") - 1);
}
smart_str_0(str);
}
/* }}} */
/* {{{ mysqlnd_escape_string_for_tx_name_in_comment */
static char *
mysqlnd_escape_string_for_tx_name_in_comment(const char * const name)
{
char * ret = NULL;
DBG_ENTER("mysqlnd_escape_string_for_tx_name_in_comment");
if (name) {
bool warned = FALSE;
const char * p_orig = name;
char * p_copy;
p_copy = ret = mnd_emalloc(strlen(name) + 1 + 2 + 2 + 1); /* space, open, close, NullS */
*p_copy++ = ' ';
*p_copy++ = '/';
*p_copy++ = '*';
while (1) {
register char v = *p_orig;
if (v == 0) {
break;
}
if ((v >= '0' && v <= '9') ||
(v >= 'a' && v <= 'z') ||
(v >= 'A' && v <= 'Z') ||
v == '-' ||
v == '_' ||
v == ' ' ||
v == '=')
{
*p_copy++ = v;
} else if (warned == FALSE) {
php_error_docref(NULL, E_WARNING, "Transaction name has been truncated, since it can only contain the A-Z, a-z, 0-9, \"\\\", \"-\", \"_\", and \"=\" characters");
warned = TRUE;
}
++p_orig;
}
*p_copy++ = '*';
*p_copy++ = '/';
*p_copy++ = 0;
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::tx_commit_ex */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, tx_commit_or_rollback)(MYSQLND_CONN_DATA * conn, const bool commit, const unsigned int flags, const char * const name)
{
DBG_ENTER("mysqlnd_conn_data::tx_commit_or_rollback");
smart_str tmp_str = {0, 0};
conn->m->tx_cor_options_to_string(conn, &tmp_str, flags);
smart_str_0(&tmp_str);
char * query;
size_t query_len;
char * name_esc = mysqlnd_escape_string_for_tx_name_in_comment(name);
query_len = mnd_sprintf(&query, 0, (commit? "COMMIT%s %s":"ROLLBACK%s %s"),
name_esc? name_esc:"", tmp_str.s? ZSTR_VAL(tmp_str.s):"");
smart_str_free(&tmp_str);
if (name_esc) {
mnd_efree(name_esc);
name_esc = NULL;
}
if (!query) {
SET_OOM_ERROR(conn->error_info);
DBG_RETURN(FAIL);
}
enum_func_status ret = conn->m->query(conn, query, query_len);
mnd_sprintf_free(query);
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::tx_begin */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, tx_begin)(MYSQLND_CONN_DATA * conn, const unsigned int mode, const char * const name)
{
DBG_ENTER("mysqlnd_conn_data::tx_begin");
smart_str tmp_str = {0, 0};
if (mode & TRANS_START_WITH_CONSISTENT_SNAPSHOT) {
if (tmp_str.s) {
smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
}
smart_str_appendl(&tmp_str, "WITH CONSISTENT SNAPSHOT", sizeof("WITH CONSISTENT SNAPSHOT") - 1);
}
if (mode & TRANS_START_READ_WRITE) {
if (tmp_str.s && ZSTR_LEN(tmp_str.s)) {
smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
}
smart_str_appendl(&tmp_str, "READ WRITE", sizeof("READ WRITE") - 1);
} else if (mode & TRANS_START_READ_ONLY) {
if (tmp_str.s && ZSTR_LEN(tmp_str.s)) {
smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
}
smart_str_appendl(&tmp_str, "READ ONLY", sizeof("READ ONLY") - 1);
}
smart_str_0(&tmp_str);
char * name_esc = mysqlnd_escape_string_for_tx_name_in_comment(name);
char * query;
unsigned int query_len = mnd_sprintf(&query, 0, "START TRANSACTION%s %s", name_esc? name_esc:"", tmp_str.s? ZSTR_VAL(tmp_str.s):"");
smart_str_free(&tmp_str);
if (name_esc) {
mnd_efree(name_esc);
name_esc = NULL;
}
if (!query) {
SET_OOM_ERROR(conn->error_info);
DBG_RETURN(FAIL);
}
enum_func_status ret = conn->m->query(conn, query, query_len);
mnd_sprintf_free(query);
if (ret && mode & (TRANS_START_READ_WRITE | TRANS_START_READ_ONLY) && mysqlnd_stmt_errno(conn) == 1064) {
SET_CLIENT_ERROR(conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE,
"This server version doesn't support 'READ WRITE' and 'READ ONLY'. Minimum 5.6.5 is required");
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::tx_savepoint */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint)(MYSQLND_CONN_DATA * conn, const char * const name)
{
DBG_ENTER("mysqlnd_conn_data::tx_savepoint");
if (!name) {
SET_CLIENT_ERROR(conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Savepoint name not provided");
DBG_RETURN(FAIL);
}
char *query;
size_t query_len = mnd_sprintf(&query, 0, "SAVEPOINT `%s`", name);
if (!query) {
SET_OOM_ERROR(conn->error_info);
DBG_RETURN(FAIL);
}
enum_func_status ret = conn->m->query(conn, query, query_len);
mnd_sprintf_free(query);
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::tx_savepoint_release */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint_release)(MYSQLND_CONN_DATA * conn, const char * const name)
{
DBG_ENTER("mysqlnd_conn_data::tx_savepoint_release");
if (!name) {
SET_CLIENT_ERROR(conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Savepoint name not provided");
DBG_RETURN(FAIL);
}
char *query;
size_t query_len = mnd_sprintf(&query, 0, "RELEASE SAVEPOINT `%s`", name);
if (!query) {
SET_OOM_ERROR(conn->error_info);
DBG_RETURN(FAIL);
}
enum_func_status ret = conn->m->query(conn, query, query_len);
mnd_sprintf_free(query);
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::negotiate_client_api_capabilities */
static size_t
MYSQLND_METHOD(mysqlnd_conn_data, negotiate_client_api_capabilities)(MYSQLND_CONN_DATA * const conn, const size_t flags)
{
unsigned int ret = 0;
DBG_ENTER("mysqlnd_conn_data::negotiate_client_api_capabilities");
if (conn) {
ret = conn->client_api_capabilities;
conn->client_api_capabilities = flags;
}
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::get_client_api_capabilities */
static size_t
MYSQLND_METHOD(mysqlnd_conn_data, get_client_api_capabilities)(const MYSQLND_CONN_DATA * const conn)
{
DBG_ENTER("mysqlnd_conn_data::get_client_api_capabilities");
DBG_RETURN(conn? conn->client_api_capabilities : 0);
}
/* }}} */
/* {{{ _mysqlnd_stmt_init */
MYSQLND_STMT *
MYSQLND_METHOD(mysqlnd_conn_data, stmt_init)(MYSQLND_CONN_DATA * const conn)
{
MYSQLND_STMT * ret;
DBG_ENTER("mysqlnd_conn_data::stmt_init");
ret = conn->object_factory.get_prepared_statement(conn);
DBG_RETURN(ret);
}
/* }}} */
MYSQLND_CLASS_METHODS_START(mysqlnd_conn_data)
MYSQLND_METHOD(mysqlnd_conn_data, connect),
MYSQLND_METHOD(mysqlnd_conn_data, escape_string),
MYSQLND_METHOD(mysqlnd_conn_data, set_charset),
MYSQLND_METHOD(mysqlnd_conn_data, query),
MYSQLND_METHOD(mysqlnd_conn_data, send_query),
MYSQLND_METHOD(mysqlnd_conn_data, reap_query),
MYSQLND_METHOD(mysqlnd_conn_data, use_result),
MYSQLND_METHOD(mysqlnd_conn_data, store_result),
MYSQLND_METHOD(mysqlnd_conn_data, next_result),
MYSQLND_METHOD(mysqlnd_conn_data, more_results),
MYSQLND_METHOD(mysqlnd_conn_data, stmt_init),
MYSQLND_METHOD(mysqlnd_conn_data, refresh),
MYSQLND_METHOD(mysqlnd_conn_data, ping),
MYSQLND_METHOD(mysqlnd_conn_data, kill),
MYSQLND_METHOD(mysqlnd_conn_data, select_db),
MYSQLND_METHOD(mysqlnd_conn_data, dump_debug_info),
MYSQLND_METHOD(mysqlnd_conn_data, change_user),
MYSQLND_METHOD(mysqlnd_conn_data, err_no),
MYSQLND_METHOD(mysqlnd_conn_data, error),
MYSQLND_METHOD(mysqlnd_conn_data, sqlstate),
MYSQLND_METHOD(mysqlnd_conn_data, thread_id),
MYSQLND_METHOD(mysqlnd_conn_data, get_connection_stats),
MYSQLND_METHOD(mysqlnd_conn_data, get_server_version),
MYSQLND_METHOD(mysqlnd_conn_data, get_server_info),
MYSQLND_METHOD(mysqlnd_conn_data, statistic),
MYSQLND_METHOD(mysqlnd_conn_data, get_host_info),
MYSQLND_METHOD(mysqlnd_conn_data, get_proto_info),
MYSQLND_METHOD(mysqlnd_conn_data, info),
MYSQLND_METHOD(mysqlnd_conn_data, charset_name),
MYSQLND_METHOD(mysqlnd_conn_data, list_method),
MYSQLND_METHOD(mysqlnd_conn_data, insert_id),
MYSQLND_METHOD(mysqlnd_conn_data, affected_rows),
MYSQLND_METHOD(mysqlnd_conn_data, warning_count),
MYSQLND_METHOD(mysqlnd_conn_data, field_count),
MYSQLND_METHOD(mysqlnd_conn_data, server_status),
MYSQLND_METHOD(mysqlnd_conn_data, set_server_option),
MYSQLND_METHOD(mysqlnd_conn_data, set_client_option),
MYSQLND_METHOD(mysqlnd_conn_data, free_contents),
MYSQLND_METHOD(mysqlnd_conn_data, free_options),
MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, dtor),
mysqlnd_query_read_result_set_header,
MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, get_reference),
MYSQLND_METHOD_PRIVATE(mysqlnd_conn_data, free_reference),
MYSQLND_METHOD(mysqlnd_conn_data, restart_psession),
MYSQLND_METHOD(mysqlnd_conn_data, end_psession),
MYSQLND_METHOD(mysqlnd_conn_data, send_close),
MYSQLND_METHOD(mysqlnd_conn_data, ssl_set),
mysqlnd_result_init,
MYSQLND_METHOD(mysqlnd_conn_data, set_autocommit),
MYSQLND_METHOD(mysqlnd_conn_data, tx_commit),
MYSQLND_METHOD(mysqlnd_conn_data, tx_rollback),
MYSQLND_METHOD(mysqlnd_conn_data, tx_begin),
MYSQLND_METHOD(mysqlnd_conn_data, tx_commit_or_rollback),
MYSQLND_METHOD(mysqlnd_conn_data, tx_cor_options_to_string),
MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint),
MYSQLND_METHOD(mysqlnd_conn_data, tx_savepoint_release),
MYSQLND_METHOD(mysqlnd_conn_data, execute_init_commands),
MYSQLND_METHOD(mysqlnd_conn_data, get_updated_connect_flags),
MYSQLND_METHOD(mysqlnd_conn_data, connect_handshake),
MYSQLND_METHOD(mysqlnd_conn_data, fetch_auth_plugin_by_name),
MYSQLND_METHOD(mysqlnd_conn_data, set_client_option_2d),
MYSQLND_METHOD(mysqlnd_conn_data, negotiate_client_api_capabilities),
MYSQLND_METHOD(mysqlnd_conn_data, get_client_api_capabilities),
MYSQLND_METHOD(mysqlnd_conn_data, get_scheme)
MYSQLND_CLASS_METHODS_END;
/* {{{ mysqlnd_conn::get_reference */
static MYSQLND *
MYSQLND_METHOD(mysqlnd_conn, clone_object)(MYSQLND * const conn)
{
MYSQLND * ret;
DBG_ENTER("mysqlnd_conn::get_reference");
ret = conn->data->object_factory.clone_connection_object(conn);
DBG_RETURN(ret);
}
/* }}} */
/* {{{ mysqlnd_conn_data::dtor */
static void
MYSQLND_METHOD_PRIVATE(mysqlnd_conn, dtor)(MYSQLND * conn)
{
DBG_ENTER("mysqlnd_conn::dtor");
DBG_INF_FMT("conn=%" PRIu64, conn->data->thread_id);
conn->data->m->free_reference(conn->data);
mnd_pefree(conn, conn->persistent);
DBG_VOID_RETURN;
}
/* }}} */
/* {{{ mysqlnd_conn_data::close */
static enum_func_status
MYSQLND_METHOD(mysqlnd_conn, close)(MYSQLND * conn_handle, const enum_connection_close_type close_type)
{
MYSQLND_CONN_DATA * conn = conn_handle->data;
DBG_ENTER("mysqlnd_conn::close");
DBG_INF_FMT("conn=%" PRIu64, conn->thread_id);
if (GET_CONNECTION_STATE(&conn->state) >= CONN_READY) {
static enum_mysqlnd_collected_stats close_type_to_stat_map[MYSQLND_CLOSE_LAST] = {
STAT_CLOSE_EXPLICIT,
STAT_CLOSE_IMPLICIT,
STAT_CLOSE_DISCONNECT
};
MYSQLND_INC_CONN_STATISTIC(conn->stats, close_type_to_stat_map[close_type]);
}
/*
Close now, free_reference will try,
if we are last, but that's not a problem.
*/
enum_func_status ret = conn->m->send_close(conn);
conn_handle->m->dtor(conn_handle);
DBG_RETURN(ret);
}
/* }}} */
MYSQLND_CLASS_METHODS_START(mysqlnd_conn)
MYSQLND_METHOD(mysqlnd_conn, connect),
MYSQLND_METHOD(mysqlnd_conn, clone_object),
MYSQLND_METHOD_PRIVATE(mysqlnd_conn, dtor),
MYSQLND_METHOD(mysqlnd_conn, close)
MYSQLND_CLASS_METHODS_END;
#include "php_network.h"
/* {{{ mysqlnd_stream_array_to_fd_set */
MYSQLND **
mysqlnd_stream_array_check_for_readiness(MYSQLND ** conn_array)
{
unsigned int cnt = 0;
MYSQLND **p = conn_array, **p_p;
MYSQLND **ret = NULL;
while (*p) {
const enum mysqlnd_connection_state conn_state = GET_CONNECTION_STATE(&((*p)->data->state));
if (conn_state <= CONN_READY || conn_state == CONN_QUIT_SENT) {
cnt++;
}
p++;
}
if (cnt) {
MYSQLND **ret_p = ret = ecalloc(cnt + 1, sizeof(MYSQLND *));
p_p = p = conn_array;
while (*p) {
const enum mysqlnd_connection_state conn_state = GET_CONNECTION_STATE(&((*p)->data->state));
if (conn_state <= CONN_READY || conn_state == CONN_QUIT_SENT) {
*ret_p = *p;
*p = NULL;
ret_p++;
} else {
*p_p = *p;
p_p++;
}
p++;
}
*ret_p = NULL;
}
return ret;
}
/* }}} */
/* {{{ mysqlnd_stream_array_to_fd_set */
static unsigned int
mysqlnd_stream_array_to_fd_set(MYSQLND ** conn_array, fd_set * fds, php_socket_t * max_fd)
{
php_socket_t this_fd;
php_stream *stream = NULL;
unsigned int cnt = 0;
MYSQLND **p = conn_array;
DBG_ENTER("mysqlnd_stream_array_to_fd_set");
while (*p) {
/* get the fd.
* NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag
* when casting. It is only used here so that the buffered data warning
* is not displayed.
* */
stream = (*p)->data->vio->data->m.get_stream((*p)->data->vio);
DBG_INF_FMT("conn=%" PRIu64 " stream=%p", (*p)->data->thread_id, stream);
if (stream != NULL &&
SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) &&
ZEND_VALID_SOCKET(this_fd))
{
PHP_SAFE_FD_SET(this_fd, fds);
if (this_fd > *max_fd) {
*max_fd = this_fd;
}
++cnt;
}
++p;
}
DBG_RETURN(cnt ? 1 : 0);
}
/* }}} */
/* {{{ mysqlnd_stream_array_from_fd_set */
static unsigned int
mysqlnd_stream_array_from_fd_set(MYSQLND ** conn_array, fd_set * fds)
{
php_socket_t this_fd;
php_stream *stream = NULL;
unsigned int ret = 0;
bool disproportion = FALSE;
MYSQLND **fwd = conn_array, **bckwd = conn_array;
DBG_ENTER("mysqlnd_stream_array_from_fd_set");
while (*fwd) {
stream = (*fwd)->data->vio->data->m.get_stream((*fwd)->data->vio);
DBG_INF_FMT("conn=%" PRIu64 " stream=%p", (*fwd)->data->thread_id, stream);
if (stream != NULL && SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL,
(void*)&this_fd, 1) && ZEND_VALID_SOCKET(this_fd)) {
if (PHP_SAFE_FD_ISSET(this_fd, fds)) {
if (disproportion) {
*bckwd = *fwd;
}
++bckwd;
++fwd;
++ret;
continue;
}
}
disproportion = TRUE;
++fwd;
}
*bckwd = NULL;/* NULL-terminate the list */
DBG_RETURN(ret);
}
/* }}} */
#ifndef PHP_WIN32
#define php_select(m, r, w, e, t) select(m, r, w, e, t)
#else
#include "win32/select.h"
#endif
/* {{{ mysqlnd_poll */
PHPAPI enum_func_status
mysqlnd_poll(MYSQLND **r_array, MYSQLND **e_array, MYSQLND ***dont_poll, long sec, long usec, int * desc_num)
{
struct timeval tv;
struct timeval *tv_p = NULL;
fd_set rfds, wfds, efds;
php_socket_t max_fd = 0;
int retval, sets = 0;
int set_count, max_set_count = 0;
DBG_ENTER("_mysqlnd_poll");
if (sec < 0 || usec < 0) {
php_error_docref(NULL, E_WARNING, "Negative values passed for sec and/or usec");
DBG_RETURN(FAIL);
}
FD_ZERO(&rfds);
FD_ZERO(&wfds);
FD_ZERO(&efds);
if (r_array != NULL) {
*dont_poll = mysqlnd_stream_array_check_for_readiness(r_array);
set_count = mysqlnd_stream_array_to_fd_set(r_array, &rfds, &max_fd);
if (set_count > max_set_count) {
max_set_count = set_count;
}
sets += set_count;
}
if (e_array != NULL) {
set_count = mysqlnd_stream_array_to_fd_set(e_array, &efds, &max_fd);
if (set_count > max_set_count) {
max_set_count = set_count;
}
sets += set_count;
}
if (!sets) {
php_error_docref(NULL, E_WARNING, *dont_poll ? "All arrays passed are clear":"No stream arrays were passed");
DBG_ERR_FMT(*dont_poll ? "All arrays passed are clear":"No stream arrays were passed");
DBG_RETURN(FAIL);
}
if (!PHP_SAFE_MAX_FD(max_fd, max_set_count)) {
DBG_RETURN(FAIL);
}
/* Solaris + BSD do not like microsecond values which are >= 1 sec */
if (usec > 999999) {
tv.tv_sec = sec + (usec / 1000000);
tv.tv_usec = usec % 1000000;
} else {
tv.tv_sec = sec;
tv.tv_usec = usec;
}
tv_p = &tv;
retval = php_select(max_fd + 1, &rfds, &wfds, &efds, tv_p);
if (retval == -1) {
php_error_docref(NULL, E_WARNING, "Unable to select [%d]: %s (max_fd=%d)",
errno, strerror(errno), max_fd);
DBG_RETURN(FAIL);
}
if (r_array != NULL) {
mysqlnd_stream_array_from_fd_set(r_array, &rfds);
}
if (e_array != NULL) {
mysqlnd_stream_array_from_fd_set(e_array, &efds);
}
*desc_num = retval;
DBG_RETURN(PASS);
}
/* }}} */
/* {{{ mysqlnd_connect */
PHPAPI MYSQLND * mysqlnd_connection_connect(MYSQLND * conn_handle,
const char * const host,
const char * const user,
const char * const passwd, unsigned int passwd_len,
const char * const db, unsigned int db_len,
unsigned int port,
const char * const sock_or_pipe,
unsigned int mysql_flags,
unsigned int client_api_flags
)
{
enum_func_status ret = FAIL;
bool self_alloced = FALSE;
MYSQLND_CSTRING hostname = { host, host? strlen(host) : 0 };
MYSQLND_CSTRING username = { user, user? strlen(user) : 0 };
MYSQLND_CSTRING password = { passwd, passwd_len };
MYSQLND_CSTRING database = { db, db_len };
MYSQLND_CSTRING socket_or_pipe = { sock_or_pipe, sock_or_pipe? strlen(sock_or_pipe) : 0 };
DBG_ENTER("mysqlnd_connect");
DBG_INF_FMT("host=%s user=%s db=%s port=%u flags=%u", host? host:"", user? user:"", db? db:"", port, mysql_flags);
if (!conn_handle) {
self_alloced = TRUE;
if (!(conn_handle = mysqlnd_connection_init(client_api_flags, FALSE, NULL))) {
/* OOM */
DBG_RETURN(NULL);
}
}
ret = conn_handle->m->connect(conn_handle, hostname, username, password, database, port, socket_or_pipe, mysql_flags);
if (ret == FAIL) {
if (self_alloced) {
/*
We have alloced, thus there are no references to this
object - we are free to kill it!
*/
conn_handle->m->dtor(conn_handle);
}
DBG_RETURN(NULL);
}
DBG_RETURN(conn_handle);
}
/* }}} */
/* {{{ mysqlnd_connection_init */
PHPAPI MYSQLND *
mysqlnd_connection_init(const size_t client_flags, const bool persistent, MYSQLND_CLASS_METHODS_TYPE(mysqlnd_object_factory) *object_factory)
{
MYSQLND_CLASS_METHODS_TYPE(mysqlnd_object_factory) *factory = object_factory? object_factory : &MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_object_factory);
MYSQLND * ret;
DBG_ENTER("mysqlnd_connection_init");
ret = factory->get_connection(factory, persistent);
if (ret && ret->data) {
ret->data->m->negotiate_client_api_capabilities(ret->data, client_flags);
}
DBG_RETURN(ret);
}
/* }}} */
|