summaryrefslogtreecommitdiff
path: root/ext/mysqlnd/mysqlnd_ps.c
blob: bdc46bdb0610f40a35f15886e7f3dfe5bab2c714 (plain)
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
/*
  +----------------------------------------------------------------------+
  | PHP Version 5                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 2006-2014 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:           |
  | http://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@mysql.com>                           |
  |          Ulf Wendel <uwendel@mysql.com>                              |
  |          Georg Richter <georg@mysql.com>                             |
  +----------------------------------------------------------------------+
*/

/* $Id$ */
#include "php.h"
#include "mysqlnd.h"
#include "mysqlnd_wireprotocol.h"
#include "mysqlnd_priv.h"
#include "mysqlnd_result.h"
#include "mysqlnd_result_meta.h"
#include "mysqlnd_statistics.h"
#include "mysqlnd_debug.h"
#include "mysqlnd_block_alloc.h"
#include "mysqlnd_ext_plugin.h"

#define MYSQLND_SILENT


const char * const mysqlnd_not_bound_as_blob = "Can't send long data for non-string/non-binary data types";
const char * const mysqlnd_stmt_not_prepared = "Statement not prepared";

/* Exported by mysqlnd_ps_codec.c */
enum_func_status mysqlnd_stmt_execute_generate_request(MYSQLND_STMT * const s, zend_uchar ** request, size_t *request_len, zend_bool * free_buffer TSRMLS_DC);

enum_func_status mysqlnd_stmt_fetch_row_buffered(MYSQLND_RES *result, void *param,
												unsigned int flags,
												zend_bool *fetched_anything TSRMLS_DC);

enum_func_status mysqlnd_fetch_stmt_row_cursor(MYSQLND_RES *result, void *param,
											   unsigned int flags,
											   zend_bool *fetched_anything TSRMLS_DC);

static void mysqlnd_stmt_separate_result_bind(MYSQLND_STMT * const stmt TSRMLS_DC);
static void mysqlnd_stmt_separate_one_result_bind(MYSQLND_STMT * const stmt, unsigned int param_no TSRMLS_DC);

static void MYSQLND_METHOD(mysqlnd_stmt, free_stmt_result)(MYSQLND_STMT * const s TSRMLS_DC);

/* {{{ mysqlnd_stmt::store_result */
static MYSQLND_RES *
MYSQLND_METHOD(mysqlnd_stmt, store_result)(MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	enum_func_status ret;
	MYSQLND_CONN_DATA * conn;
	MYSQLND_RES * result;

	DBG_ENTER("mysqlnd_stmt::store_result");
	if (!stmt || !stmt->conn || !stmt->result) {
		DBG_RETURN(NULL);
	}
	DBG_INF_FMT("stmt=%lu", stmt->stmt_id);

	conn = stmt->conn;

	/* be compliant with libmysql - NULL will turn */
	if (!stmt->field_count) {
		DBG_RETURN(NULL);
	}

	if (stmt->cursor_exists) {
		/* Silently convert buffered to unbuffered, for now */
		DBG_RETURN(s->m->use_result(s TSRMLS_CC));
	}

	/* Nothing to store for UPSERT/LOAD DATA*/
	if (CONN_GET_STATE(conn) != CONN_FETCHING_DATA ||
		stmt->state != MYSQLND_STMT_WAITING_USE_OR_STORE)
	{
		SET_CLIENT_ERROR(*conn->error_info, CR_COMMANDS_OUT_OF_SYNC,
						 UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
		DBG_RETURN(NULL);
	}

	stmt->default_rset_handler = s->m->store_result;

	SET_EMPTY_ERROR(*stmt->error_info);
	SET_EMPTY_ERROR(*conn->error_info);
	MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_PS_BUFFERED_SETS);

	result = stmt->result;
	result->type			= MYSQLND_RES_PS_BUF;
	result->m.fetch_row		= mysqlnd_stmt_fetch_row_buffered;
	result->m.fetch_lengths	= NULL;/* makes no sense */
	result->m.row_decoder = php_mysqlnd_rowp_read_binary_protocol;

	result->result_set_memory_pool = mysqlnd_mempool_create(MYSQLND_G(mempool_default_size) TSRMLS_CC);

	ret = result->m.store_result_fetch_data(conn, result, result->meta, TRUE TSRMLS_CC);

	if (PASS == ret) {
		/* libmysql API docs say it should be so for SELECT statements */
		stmt->upsert_status->affected_rows = stmt->result->stored_data->row_count;

		stmt->state = MYSQLND_STMT_USE_OR_STORE_CALLED;
	} else {
		COPY_CLIENT_ERROR(*conn->error_info, result->stored_data->error_info);
		stmt->result->m.free_result_contents(stmt->result TSRMLS_CC);
		mnd_efree(stmt->result);
		stmt->result = NULL;
		stmt->state = MYSQLND_STMT_PREPARED;
	}

	DBG_RETURN(result);
}
/* }}} */


/* {{{ mysqlnd_stmt::get_result */
static MYSQLND_RES *
MYSQLND_METHOD(mysqlnd_stmt, get_result)(MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	MYSQLND_CONN_DATA * conn;
	MYSQLND_RES *result;

	DBG_ENTER("mysqlnd_stmt::get_result");
	if (!stmt || !stmt->conn || !stmt->result) {
		DBG_RETURN(NULL);
	}
	DBG_INF_FMT("stmt=%lu", stmt->stmt_id);

	conn = stmt->conn;

	/* be compliant with libmysql - NULL will turn */
	if (!stmt->field_count) {
		DBG_RETURN(NULL);
	}

	if (stmt->cursor_exists) {
		/* Silently convert buffered to unbuffered, for now */
		DBG_RETURN(s->m->use_result(s TSRMLS_CC));
	}

	/* Nothing to store for UPSERT/LOAD DATA*/
	if (CONN_GET_STATE(conn) != CONN_FETCHING_DATA || stmt->state != MYSQLND_STMT_WAITING_USE_OR_STORE) {
		SET_CLIENT_ERROR(*conn->error_info, CR_COMMANDS_OUT_OF_SYNC,
						 UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
		DBG_RETURN(NULL);
	}

	SET_EMPTY_ERROR(*stmt->error_info);
	SET_EMPTY_ERROR(*conn->error_info);
	MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_BUFFERED_SETS);

	do {
		result = conn->m->result_init(stmt->result->field_count, stmt->persistent TSRMLS_CC);
		if (!result) {
			SET_OOM_ERROR(*conn->error_info);
			break;
		}

		result->meta = stmt->result->meta->m->clone_metadata(stmt->result->meta, FALSE TSRMLS_CC);
		if (!result->meta) {
			SET_OOM_ERROR(*conn->error_info);
			break;
		}

		if ((result = result->m.store_result(result, conn, TRUE TSRMLS_CC))) {
			stmt->upsert_status->affected_rows = result->stored_data->row_count;
			stmt->state = MYSQLND_STMT_PREPARED;
			result->type = MYSQLND_RES_PS_BUF;
		} else {
			COPY_CLIENT_ERROR(*stmt->error_info, *conn->error_info);
			stmt->state = MYSQLND_STMT_PREPARED;
			break;
		}
		DBG_RETURN(result);
	} while (0);

	if (result) {
		result->m.free_result(result, TRUE TSRMLS_CC);
	}
	DBG_RETURN(NULL);
}
/* }}} */


/* {{{ mysqlnd_stmt::more_results */
static zend_bool
MYSQLND_METHOD(mysqlnd_stmt, more_results)(const MYSQLND_STMT * s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	DBG_ENTER("mysqlnd_stmt::more_results");
	/* (conn->state == CONN_NEXT_RESULT_PENDING) too */
	DBG_RETURN((stmt && stmt->conn && (stmt->conn->m->get_server_status(stmt->conn TSRMLS_CC) & SERVER_MORE_RESULTS_EXISTS))?
									TRUE:
									FALSE);
}
/* }}} */


/* {{{ mysqlnd_stmt::next_result */
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, next_result)(MYSQLND_STMT * s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	MYSQLND_CONN_DATA * conn;

	DBG_ENTER("mysqlnd_stmt::next_result");
	if (!stmt || !stmt->conn || !stmt->result) {
		DBG_RETURN(FAIL);
	}
	conn = stmt->conn;
	DBG_INF_FMT("stmt=%lu", stmt->stmt_id);

	if (CONN_GET_STATE(conn) != CONN_NEXT_RESULT_PENDING || !(conn->upsert_status->server_status & SERVER_MORE_RESULTS_EXISTS)) {
		DBG_RETURN(FAIL);
	}

	DBG_INF_FMT("server_status=%u cursor=%u", stmt->upsert_status->server_status, stmt->upsert_status->server_status & SERVER_STATUS_CURSOR_EXISTS);

	/* Free space for next result */
	MYSQLND_METHOD(mysqlnd_stmt, free_stmt_result)(s TSRMLS_CC);
	{
		enum_func_status ret = s->m->parse_execute_response(s TSRMLS_CC);
		DBG_RETURN(ret);
	}
}
/* }}} */


/* {{{ mysqlnd_stmt_skip_metadata */
static enum_func_status
mysqlnd_stmt_skip_metadata(MYSQLND_STMT * s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	/* Follows parameter metadata, we have just to skip it, as libmysql does */
	unsigned int i = 0;
	enum_func_status ret = FAIL;
	MYSQLND_PACKET_RES_FIELD * field_packet;

	DBG_ENTER("mysqlnd_stmt_skip_metadata");
	if (!stmt || !stmt->conn || !stmt->conn->protocol) {
		DBG_RETURN(FAIL);
	}
	DBG_INF_FMT("stmt=%lu", stmt->stmt_id);

	field_packet = stmt->conn->protocol->m.get_result_field_packet(stmt->conn->protocol, FALSE TSRMLS_CC);
	if (!field_packet) {
		SET_OOM_ERROR(*stmt->error_info);
		SET_OOM_ERROR(*stmt->conn->error_info);
	} else {
		ret = PASS;
		field_packet->skip_parsing = TRUE;
		for (;i < stmt->param_count; i++) {
			if (FAIL == PACKET_READ(field_packet, stmt->conn)) {
				ret = FAIL;
				break;
			}
		}
		PACKET_FREE(field_packet);
	}

	DBG_RETURN(ret);
}
/* }}} */


/* {{{ mysqlnd_stmt_read_prepare_response */
static enum_func_status
mysqlnd_stmt_read_prepare_response(MYSQLND_STMT * s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	MYSQLND_PACKET_PREPARE_RESPONSE * prepare_resp;
	enum_func_status ret = FAIL;

	DBG_ENTER("mysqlnd_stmt_read_prepare_response");
	if (!stmt || !stmt->conn || !stmt->conn->protocol) {
		DBG_RETURN(FAIL);
	}
	DBG_INF_FMT("stmt=%lu", stmt->stmt_id);

	prepare_resp = stmt->conn->protocol->m.get_prepare_response_packet(stmt->conn->protocol, FALSE TSRMLS_CC);
	if (!prepare_resp) {
		SET_OOM_ERROR(*stmt->error_info);
		SET_OOM_ERROR(*stmt->conn->error_info);
		goto done;
	}

	if (FAIL == PACKET_READ(prepare_resp, stmt->conn)) {
		goto done;
	}

	if (0xFF == prepare_resp->error_code) {
		COPY_CLIENT_ERROR(*stmt->error_info, prepare_resp->error_info);
		COPY_CLIENT_ERROR(*stmt->conn->error_info, prepare_resp->error_info);
		goto done;
	}
	ret = PASS;
	stmt->stmt_id = prepare_resp->stmt_id;
	stmt->warning_count = stmt->conn->upsert_status->warning_count = prepare_resp->warning_count;
	stmt->field_count = stmt->conn->field_count = prepare_resp->field_count;
	stmt->param_count = prepare_resp->param_count;
done:
	PACKET_FREE(prepare_resp);

	DBG_RETURN(ret);
}
/* }}} */


/* {{{ mysqlnd_stmt_prepare_read_eof */
static enum_func_status
mysqlnd_stmt_prepare_read_eof(MYSQLND_STMT * s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	MYSQLND_PACKET_EOF * fields_eof;
	enum_func_status ret = FAIL;

	DBG_ENTER("mysqlnd_stmt_prepare_read_eof");
	if (!stmt || !stmt->conn || !stmt->conn->protocol) {
		DBG_RETURN(FAIL);
	}
	DBG_INF_FMT("stmt=%lu", stmt->stmt_id);

	fields_eof = stmt->conn->protocol->m.get_eof_packet(stmt->conn->protocol, FALSE TSRMLS_CC);
	if (!fields_eof) {
		SET_OOM_ERROR(*stmt->error_info);
		SET_OOM_ERROR(*stmt->conn->error_info);
	} else {
		if (FAIL == (ret = PACKET_READ(fields_eof, stmt->conn))) {
			if (stmt->result) {
				stmt->result->m.free_result_contents(stmt->result TSRMLS_CC);
				mnd_efree(stmt->result);
				memset(stmt, 0, sizeof(MYSQLND_STMT_DATA));
				stmt->state = MYSQLND_STMT_INITTED;
			}
		} else {
			stmt->upsert_status->server_status = fields_eof->server_status;
			stmt->upsert_status->warning_count = fields_eof->warning_count;
			stmt->state = MYSQLND_STMT_PREPARED;
		}
		PACKET_FREE(fields_eof);
	}

	DBG_RETURN(ret);
}
/* }}} */


/* {{{ mysqlnd_stmt::prepare */
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, prepare)(MYSQLND_STMT * const s, const char * const query, unsigned int query_len TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	MYSQLND_STMT * s_to_prepare = s;
	MYSQLND_STMT_DATA * stmt_to_prepare = stmt;

	DBG_ENTER("mysqlnd_stmt::prepare");
	if (!stmt || !stmt->conn) {
		DBG_RETURN(FAIL);
	}
	DBG_INF_FMT("stmt=%lu", stmt->stmt_id);
	DBG_INF_FMT("query=%s", query);

	SET_ERROR_AFF_ROWS(stmt);
	SET_ERROR_AFF_ROWS(stmt->conn);

	SET_EMPTY_ERROR(*stmt->error_info);
	SET_EMPTY_ERROR(*stmt->conn->error_info);

	if (stmt->state > MYSQLND_STMT_INITTED) {
		/* See if we have to clean the wire */
		if (stmt->state == MYSQLND_STMT_WAITING_USE_OR_STORE) {
			/* Do implicit use_result and then flush the result */
			stmt->default_rset_handler = s->m->use_result;
			stmt->default_rset_handler(s TSRMLS_CC);
		}
		/* No 'else' here please :) */
		if (stmt->state > MYSQLND_STMT_WAITING_USE_OR_STORE && stmt->result) {
			stmt->result->m.skip_result(stmt->result TSRMLS_CC);
		}
		/*
		  Create a new test statement, which we will prepare, but if anything
		  fails, we will scrap it.
		*/
		s_to_prepare = stmt->conn->m->stmt_init(stmt->conn TSRMLS_CC);
		if (!s_to_prepare) {
			goto fail;
		}
		stmt_to_prepare = s_to_prepare->data;
	}

	if (FAIL == stmt_to_prepare->conn->m->simple_command(stmt_to_prepare->conn, COM_STMT_PREPARE, (const zend_uchar *) query, query_len, PROT_LAST, FALSE, TRUE TSRMLS_CC) ||
		FAIL == mysqlnd_stmt_read_prepare_response(s_to_prepare TSRMLS_CC))
	{
		goto fail;
	}

	if (stmt_to_prepare->param_count) {
		if (FAIL == mysqlnd_stmt_skip_metadata(s_to_prepare TSRMLS_CC) ||
			FAIL == mysqlnd_stmt_prepare_read_eof(s_to_prepare TSRMLS_CC))
		{
			goto fail;
		}
	}

	/*
	  Read metadata only if there is actual result set.
	  Beware that SHOW statements bypass the PS framework and thus they send
	  no metadata at prepare.
	*/
	if (stmt_to_prepare->field_count) {
		MYSQLND_RES * result = stmt->conn->m->result_init(stmt_to_prepare->field_count, stmt_to_prepare->persistent TSRMLS_CC);
		if (!result) {
			SET_OOM_ERROR(*stmt->conn->error_info);
			goto fail;
		}
		/* Allocate the result now as it is needed for the reading of metadata */
		stmt_to_prepare->result = result; 

		result->conn = stmt_to_prepare->conn->m->get_reference(stmt_to_prepare->conn TSRMLS_CC);

		result->type = MYSQLND_RES_PS_BUF;

		if (FAIL == result->m.read_result_metadata(result, stmt_to_prepare->conn TSRMLS_CC) ||
			FAIL == mysqlnd_stmt_prepare_read_eof(s_to_prepare TSRMLS_CC))
		{
			goto fail;
		}
	}

	if (stmt_to_prepare != stmt) {
		/* swap */
		size_t real_size = sizeof(MYSQLND_STMT) + mysqlnd_plugin_count() * sizeof(void *);
		char * tmp_swap = mnd_malloc(real_size);
		memcpy(tmp_swap, s, real_size);
		memcpy(s, s_to_prepare, real_size);
		memcpy(s_to_prepare, tmp_swap, real_size);
		mnd_free(tmp_swap);
		{
			MYSQLND_STMT_DATA * tmp_swap_data = stmt_to_prepare;
			stmt_to_prepare = stmt;
			stmt = tmp_swap_data;
		}
		s_to_prepare->m->dtor(s_to_prepare, TRUE TSRMLS_CC);
	}
	stmt->state = MYSQLND_STMT_PREPARED;
	DBG_INF("PASS");
	DBG_RETURN(PASS);

fail:
	if (stmt_to_prepare != stmt && s_to_prepare) {
		s_to_prepare->m->dtor(s_to_prepare, TRUE TSRMLS_CC);
	}
	stmt->state = MYSQLND_STMT_INITTED;

	DBG_INF("FAIL");
	DBG_RETURN(FAIL);
}
/* }}} */


/* {{{ mysqlnd_stmt_execute_parse_response */
static enum_func_status
mysqlnd_stmt_execute_parse_response(MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	enum_func_status ret;
	MYSQLND_CONN_DATA * conn;

	DBG_ENTER("mysqlnd_stmt_execute_parse_response");
	if (!stmt || !stmt->conn) {
		DBG_RETURN(FAIL);
	}
	conn = stmt->conn;
	CONN_SET_STATE(conn, CONN_QUERY_SENT);

	ret = mysqlnd_query_read_result_set_header(stmt->conn, s TSRMLS_CC);
	if (ret == FAIL) {
		COPY_CLIENT_ERROR(*stmt->error_info, *conn->error_info);
		memset(stmt->upsert_status, 0, sizeof(*stmt->upsert_status));
		stmt->upsert_status->affected_rows = conn->upsert_status->affected_rows;
		if (CONN_GET_STATE(conn) == CONN_QUIT_SENT) {
			/* close the statement here, the connection has been closed */
		}
		stmt->state = MYSQLND_STMT_PREPARED;
		stmt->send_types_to_server = 1;
	} else {
		/*
		  stmt->send_types_to_server has already been set to 0 in
		  mysqlnd_stmt_execute_generate_request / mysqlnd_stmt_execute_store_params
		  In case there is a situation in which binding was done for integer and the
		  value is > LONG_MAX or < LONG_MIN, there is string conversion and we have
		  to resend the types. Next execution will also need to resend the type.
		*/
		SET_EMPTY_ERROR(*stmt->error_info);
		SET_EMPTY_ERROR(*stmt->conn->error_info);
		*stmt->upsert_status = *conn->upsert_status; /* copy status */
		stmt->state = MYSQLND_STMT_EXECUTED;
		if (conn->last_query_type == QUERY_UPSERT || conn->last_query_type == QUERY_LOAD_LOCAL) {
			DBG_INF("PASS");
			DBG_RETURN(PASS);
		}

		stmt->result->type = MYSQLND_RES_PS_BUF;
		if (!stmt->result->conn) {
			/*
			  For SHOW we don't create (bypasses PS in server)
			  a result set at prepare and thus a connection was missing
			*/
			stmt->result->conn = stmt->conn->m->get_reference(stmt->conn TSRMLS_CC);
		}

		/* Update stmt->field_count as SHOW sets it to 0 at prepare */
		stmt->field_count = stmt->result->field_count = conn->field_count;
		stmt->result->lengths = NULL;
		if (stmt->field_count) {
			stmt->state = MYSQLND_STMT_WAITING_USE_OR_STORE;
			/*
			  We need to set this because the user might not call
			  use_result() or store_result() and we should be able to scrap the
			  data on the line, if he just decides to close the statement.
			*/
			DBG_INF_FMT("server_status=%u cursor=%u", stmt->upsert_status->server_status,
						stmt->upsert_status->server_status & SERVER_STATUS_CURSOR_EXISTS);

			if (stmt->upsert_status->server_status & SERVER_STATUS_CURSOR_EXISTS) {
				DBG_INF("cursor exists");
				stmt->cursor_exists = TRUE;
				CONN_SET_STATE(conn, CONN_READY);
				/* Only cursor read */
				stmt->default_rset_handler = s->m->use_result;
				DBG_INF("use_result");
			} else if (stmt->flags & CURSOR_TYPE_READ_ONLY) {
				DBG_INF("asked for cursor but got none");
				/*
				  We have asked for CURSOR but got no cursor, because the condition
				  above is not fulfilled. Then...

				  This is a single-row result set, a result set with no rows, EXPLAIN,
				  SHOW VARIABLES, or some other command which either a) bypasses the
				  cursors framework in the server and writes rows directly to the
				  network or b) is more efficient if all (few) result set rows are
				  precached on client and server's resources are freed.
				*/
				/* preferred is buffered read */
				stmt->default_rset_handler = s->m->store_result;
				DBG_INF("store_result");
			} else {
				DBG_INF("no cursor");
				/* preferred is unbuffered read */
				stmt->default_rset_handler = s->m->use_result;
				DBG_INF("use_result");
			}
		}
	}
#ifndef MYSQLND_DONT_SKIP_OUT_PARAMS_RESULTSET
	if (stmt->upsert_status->server_status & SERVER_PS_OUT_PARAMS) {
		s->m->free_stmt_content(s TSRMLS_CC);
		DBG_INF("PS OUT Variable RSet, skipping");
		/* OUT params result set. Skip for now to retain compatibility */
		ret = mysqlnd_stmt_execute_parse_response(s TSRMLS_CC);
	}
#endif

	DBG_INF(ret == PASS? "PASS":"FAIL");
	DBG_RETURN(ret);
}
/* }}} */


/* {{{ mysqlnd_stmt::execute */
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, execute)(MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	enum_func_status ret;
	MYSQLND_CONN_DATA * conn;
	zend_uchar *request = NULL;
	size_t		request_len;
	zend_bool	free_request;

	DBG_ENTER("mysqlnd_stmt::execute");
	if (!stmt || !stmt->conn) {
		DBG_RETURN(FAIL);
	}
	conn = stmt->conn;
	DBG_INF_FMT("stmt=%lu", stmt->stmt_id);

	SET_ERROR_AFF_ROWS(stmt);
	SET_ERROR_AFF_ROWS(stmt->conn);

	if (stmt->result && stmt->state >= MYSQLND_STMT_PREPARED && stmt->field_count) {
		/*
		  We don need to copy the data from the buffers which we will clean.
		  Because it has already been copied. See
		  #ifndef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
		*/
#ifdef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
		if (stmt->result_bind &&
			stmt->result_zvals_separated_once == TRUE && 
			stmt->state >= MYSQLND_STMT_USER_FETCHING)
		{
			/*
			  We need to copy the data from the buffers which we will clean.
			  The bound variables point to them only if the user has started
			  to fetch data (MYSQLND_STMT_USER_FETCHING).
			  We need to check 'result_zvals_separated_once' or we will leak
			  in the following scenario
			  prepare("select 1 from dual");
			  execute();
			  fetch(); <-- no binding, but that's not a problem
			  bind_result();
			  execute(); <-- here we will leak because we separate without need
			*/
			unsigned int i;
			for (i = 0; i < stmt->field_count; i++) {
				if (stmt->result_bind[i].bound == TRUE) {
					zval_copy_ctor(stmt->result_bind[i].zv);
				}
			}
		}
#endif

		s->m->flush(s TSRMLS_CC);

		/*
		  Executed, but the user hasn't started to fetch
		  This will clean also the metadata, but after the EXECUTE call we will
		  have it again.
		*/
		stmt->result->m.free_result_buffers(stmt->result TSRMLS_CC);

		stmt->state = MYSQLND_STMT_PREPARED;
	} else if (stmt->state < MYSQLND_STMT_PREPARED) {
		/* Only initted - error */
		SET_CLIENT_ERROR(*conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE,
						 mysqlnd_out_of_sync);
		SET_STMT_ERROR(stmt, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
		DBG_INF("FAIL");
		DBG_RETURN(FAIL);
	}

	if (stmt->param_count) {
		unsigned int i, not_bound = 0;
		if (!stmt->param_bind) {
			SET_STMT_ERROR(stmt, CR_PARAMS_NOT_BOUND, UNKNOWN_SQLSTATE,
							 "No data supplied for parameters in prepared statement");
			DBG_INF("FAIL");
			DBG_RETURN(FAIL);
		}
		for (i = 0; i < stmt->param_count; i++) {
			if (stmt->param_bind[i].zv == NULL) {
				not_bound++;
			}
		}
		if (not_bound) {
			char * msg;
			mnd_sprintf(&msg, 0, "No data supplied for %u parameter%s in prepared statement",
						not_bound, not_bound>1 ?"s":"");
			SET_STMT_ERROR(stmt, CR_PARAMS_NOT_BOUND, UNKNOWN_SQLSTATE, msg);
			if (msg) {
				mnd_sprintf_free(msg);
			}
			DBG_INF("FAIL");
			DBG_RETURN(FAIL);
		}
	}
	ret = s->m->generate_execute_request(s, &request, &request_len, &free_request TSRMLS_CC);
	if (ret == PASS) {
		/* support for buffer types should be added here ! */
		ret = stmt->conn->m->simple_command(stmt->conn, COM_STMT_EXECUTE, request, request_len,
											PROT_LAST /* we will handle the response packet*/,
											FALSE, FALSE TSRMLS_CC);
	} else {
		SET_STMT_ERROR(stmt, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "Couldn't generate the request. Possibly OOM.");
	}

	if (free_request) {
		mnd_efree(request);
	}

	if (ret == FAIL) {
		COPY_CLIENT_ERROR(*stmt->error_info, *conn->error_info);
		DBG_INF("FAIL");
		DBG_RETURN(FAIL);
	}
	stmt->execute_count++;

	ret = s->m->parse_execute_response(s TSRMLS_CC);

	DBG_INF_FMT("server_status=%u cursor=%u", stmt->upsert_status->server_status, stmt->upsert_status->server_status & SERVER_STATUS_CURSOR_EXISTS);

	if (ret == PASS && conn->last_query_type == QUERY_UPSERT && stmt->upsert_status->affected_rows) {
		MYSQLND_INC_CONN_STATISTIC_W_VALUE(conn->stats, STAT_ROWS_AFFECTED_PS, stmt->upsert_status->affected_rows);
	}
	DBG_RETURN(ret);
}
/* }}} */


/* {{{ mysqlnd_stmt_fetch_row_buffered */
enum_func_status
mysqlnd_stmt_fetch_row_buffered(MYSQLND_RES *result, void *param, unsigned int flags, zend_bool *fetched_anything TSRMLS_DC)
{
	MYSQLND_STMT * s = (MYSQLND_STMT *) param;
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	MYSQLND_RES_BUFFERED *set = result->stored_data;
	unsigned int field_count = result->meta->field_count;

	DBG_ENTER("mysqlnd_stmt_fetch_row_buffered");
	*fetched_anything = FALSE;
	DBG_INF_FMT("stmt=%lu", stmt != NULL ? stmt->stmt_id : 0L);

	/* If we haven't read everything */
	if (set->data_cursor &&
		(set->data_cursor - set->data) < (set->row_count * field_count))
	{
		/* The user could have skipped binding - don't crash*/
		if (stmt->result_bind) {
			unsigned int i;
			MYSQLND_RES_METADATA * meta = result->meta;
			zval **current_row = set->data_cursor;

			if (NULL == current_row[0]) {
				uint64_t row_num = (set->data_cursor - set->data) / field_count;
				enum_func_status rc = result->m.row_decoder(set->row_buffers[row_num],
												current_row,
												meta->field_count,
												meta->fields,
												result->conn->options->numeric_and_datetime_as_unicode,
												result->conn->options->int_and_float_native,
												result->conn->stats TSRMLS_CC);
				if (PASS != rc) {
					DBG_RETURN(FAIL);
				}
				set->initialized_rows++;
				if (stmt->update_max_length) {
					for (i = 0; i < result->field_count; i++) {
						/*
						  NULL fields are 0 length, 0 is not more than 0
						  String of zero size, definitely can't be the next max_length.
						  Thus for NULL and zero-length we are quite efficient.
						*/
						if (Z_TYPE_P(current_row[i]) >= IS_STRING) {
							unsigned long len = Z_STRLEN_P(current_row[i]);
							if (meta->fields[i].max_length < len) {
								meta->fields[i].max_length = len;
							}
						}
					}
				}
			}

			for (i = 0; i < result->field_count; i++) {
				/* Clean what we copied last time */
#ifndef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
				if (stmt->result_bind[i].zv) {
					zval_dtor(stmt->result_bind[i].zv);
				}
#endif
				/* copy the type */
				if (stmt->result_bind[i].bound == TRUE) {
					DBG_INF_FMT("i=%u type=%u", i, Z_TYPE_P(current_row[i]));
					if (Z_TYPE_P(current_row[i]) != IS_NULL) {
						/*
						  Copy the value.
						  Pre-condition is that the zvals in the result_bind buffer
						  have been  ZVAL_NULL()-ed or to another simple type
						  (int, double, bool but not string). Because of the reference
						  counting the user can't delete the strings the variables point to.
						*/

						Z_TYPE_P(stmt->result_bind[i].zv) = Z_TYPE_P(current_row[i]);
						stmt->result_bind[i].zv->value = current_row[i]->value;
#ifndef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
						zval_copy_ctor(stmt->result_bind[i].zv);
#endif
					} else {
						ZVAL_NULL(stmt->result_bind[i].zv);
					}
				}
			}
		}
		set->data_cursor += field_count;
		*fetched_anything = TRUE;
		/* buffered result sets don't have a connection */
		MYSQLND_INC_GLOBAL_STATISTIC(STAT_ROWS_FETCHED_FROM_CLIENT_PS_BUF);
		DBG_INF("row fetched");
	} else {
		set->data_cursor = NULL;
		DBG_INF("no more data");
	}
	DBG_INF("PASS");
	DBG_RETURN(PASS);
}
/* }}} */


/* {{{ mysqlnd_stmt_fetch_row_unbuffered */
static enum_func_status
mysqlnd_stmt_fetch_row_unbuffered(MYSQLND_RES *result, void *param, unsigned int flags, zend_bool *fetched_anything TSRMLS_DC)
{
	enum_func_status ret;
	MYSQLND_STMT * s = (MYSQLND_STMT *) param;
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	MYSQLND_PACKET_ROW * row_packet;

	DBG_ENTER("mysqlnd_stmt_fetch_row_unbuffered");

	*fetched_anything = FALSE;

	if (result->unbuf->eof_reached) {
		/* No more rows obviously */
		DBG_INF("EOF already reached");
		DBG_RETURN(PASS);
	}
	if (CONN_GET_STATE(result->conn) != CONN_FETCHING_DATA) {
		SET_CLIENT_ERROR(*result->conn->error_info, CR_COMMANDS_OUT_OF_SYNC,
						 UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
		DBG_ERR("command out of sync");
		DBG_RETURN(FAIL);
	}
	if (!(row_packet = result->row_packet)) {
		DBG_RETURN(FAIL);
	}

	/* Let the row packet fill our buffer and skip additional malloc + memcpy */
	row_packet->skip_extraction = stmt && stmt->result_bind? FALSE:TRUE;

	/*
	  If we skip rows (stmt == NULL || stmt->result_bind == NULL) we have to
	  result->m.unbuffered_free_last_data() before it. The function returns always true.
	*/
	if (PASS == (ret = PACKET_READ(row_packet, result->conn)) && !row_packet->eof) {
		unsigned int i, field_count = result->field_count;

		if (!row_packet->skip_extraction) {
			result->m.unbuffered_free_last_data(result TSRMLS_CC);

			result->unbuf->last_row_data = row_packet->fields;
			result->unbuf->last_row_buffer = row_packet->row_buffer;
			row_packet->fields = NULL;
			row_packet->row_buffer = NULL;

			if (PASS != result->m.row_decoder(result->unbuf->last_row_buffer,
									result->unbuf->last_row_data,
									row_packet->field_count,
									row_packet->fields_metadata,
									result->conn->options->numeric_and_datetime_as_unicode,
									result->conn->options->int_and_float_native,
									result->conn->stats TSRMLS_CC))
			{
				DBG_RETURN(FAIL);
			}

			for (i = 0; i < field_count; i++) {
				if (stmt->result_bind[i].bound == TRUE) {
					zval *data = result->unbuf->last_row_data[i];
					/*
					  stmt->result_bind[i].zv has been already destructed
					  in result->m.unbuffered_free_last_data()
					*/
#ifndef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
					zval_dtor(stmt->result_bind[i].zv);
#endif
					if (IS_NULL != (Z_TYPE_P(stmt->result_bind[i].zv) = Z_TYPE_P(data)) ) {
						if (
							(Z_TYPE_P(data) == IS_STRING
#if MYSQLND_UNICODE
							|| Z_TYPE_P(data) == IS_UNICODE
#endif
							)
							 && (result->meta->fields[i].max_length < (unsigned long) Z_STRLEN_P(data)))
						{
							result->meta->fields[i].max_length = Z_STRLEN_P(data);
						}
						stmt->result_bind[i].zv->value = data->value;
						/* copied data, thus also the ownership. Thus null data */
						ZVAL_NULL(data);
					}
				}
			}
			MYSQLND_INC_CONN_STATISTIC(stmt->conn->stats, STAT_ROWS_FETCHED_FROM_CLIENT_PS_UNBUF);
		} else {
			DBG_INF("skipping extraction");
			/*
			  Data has been allocated and usually result->m.unbuffered_free_last_data()
			  frees it but we can't call this function as it will cause problems with
			  the bound variables. Thus we need to do part of what it does or Zend will
			  report leaks.
			*/
			row_packet->row_buffer->free_chunk(row_packet->row_buffer TSRMLS_CC);
			row_packet->row_buffer = NULL;
		}

		result->unbuf->row_count++;
		*fetched_anything = TRUE;
	} else if (ret == FAIL) {
		if (row_packet->error_info.error_no) {
			COPY_CLIENT_ERROR(*stmt->conn->error_info, row_packet->error_info);
			COPY_CLIENT_ERROR(*stmt->error_info, row_packet->error_info);
		}
		CONN_SET_STATE(result->conn, CONN_READY);
		result->unbuf->eof_reached = TRUE; /* so next time we won't get an error */
	} else if (row_packet->eof) {
		DBG_INF("EOF");
		/* Mark the connection as usable again */
		result->unbuf->eof_reached = TRUE;
		memset(result->conn->upsert_status, 0, sizeof(*result->conn->upsert_status));
		result->conn->upsert_status->warning_count = row_packet->warning_count;
		result->conn->upsert_status->server_status = row_packet->server_status;
		/*
		  result->row_packet will be cleaned when
		  destroying the result object
		*/
		if (result->conn->upsert_status->server_status & SERVER_MORE_RESULTS_EXISTS) {
			CONN_SET_STATE(result->conn, CONN_NEXT_RESULT_PENDING);
		} else {
			CONN_SET_STATE(result->conn, CONN_READY);
		}
	}

	DBG_INF_FMT("ret=%s fetched_anything=%u", ret == PASS? "PASS":"FAIL", *fetched_anything);
	DBG_RETURN(ret);
}
/* }}} */


/* {{{ mysqlnd_stmt::use_result */
static MYSQLND_RES *
MYSQLND_METHOD(mysqlnd_stmt, use_result)(MYSQLND_STMT * s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	MYSQLND_RES * result;
	MYSQLND_CONN_DATA * conn;

	DBG_ENTER("mysqlnd_stmt::use_result");
	if (!stmt || !stmt->conn || !stmt->result) {
		DBG_RETURN(NULL);
	}
	DBG_INF_FMT("stmt=%lu", stmt->stmt_id);

	conn = stmt->conn;

	if (!stmt->field_count ||
		(!stmt->cursor_exists && CONN_GET_STATE(conn) != CONN_FETCHING_DATA) ||
		(stmt->cursor_exists && CONN_GET_STATE(conn) != CONN_READY) ||
		(stmt->state != MYSQLND_STMT_WAITING_USE_OR_STORE))
	{
		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);
	}

	SET_EMPTY_ERROR(*stmt->error_info);

	MYSQLND_INC_CONN_STATISTIC(stmt->conn->stats, STAT_PS_UNBUFFERED_SETS);
	result = stmt->result;

	result->m.use_result(stmt->result, TRUE TSRMLS_CC);
	result->m.fetch_row	= stmt->cursor_exists? mysqlnd_fetch_stmt_row_cursor:
											   mysqlnd_stmt_fetch_row_unbuffered;
	stmt->state = MYSQLND_STMT_USE_OR_STORE_CALLED;

	DBG_INF_FMT("%p", result);
	DBG_RETURN(result);
}
/* }}} */


#define STMT_ID_LENGTH 4

/* {{{ mysqlnd_fetch_row_cursor */
enum_func_status
mysqlnd_fetch_stmt_row_cursor(MYSQLND_RES *result, void *param, unsigned int flags, zend_bool *fetched_anything TSRMLS_DC)
{
	enum_func_status ret;
	MYSQLND_STMT * s = (MYSQLND_STMT *) param;
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	zend_uchar buf[STMT_ID_LENGTH /* statement id */ + 4 /* number of rows to fetch */];
	MYSQLND_PACKET_ROW * row_packet;

	DBG_ENTER("mysqlnd_fetch_stmt_row_cursor");

	if (!stmt || !stmt->conn || !result || !result->conn || !result->unbuf) {
		DBG_ERR("no statement");
		DBG_RETURN(FAIL);
	}

	DBG_INF_FMT("stmt=%lu flags=%u", stmt->stmt_id, flags);

	if (stmt->state < MYSQLND_STMT_USER_FETCHING) {
		/* Only initted - error */
		SET_CLIENT_ERROR(*stmt->conn->error_info, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE,
						mysqlnd_out_of_sync);
		DBG_ERR("command out of sync");
		DBG_RETURN(FAIL);
	}
	if (!(row_packet = result->row_packet)) {
		DBG_RETURN(FAIL);
	}

	SET_EMPTY_ERROR(*stmt->error_info);
	SET_EMPTY_ERROR(*stmt->conn->error_info);

	int4store(buf, stmt->stmt_id);
	int4store(buf + STMT_ID_LENGTH, 1); /* for now fetch only one row */

	if (FAIL == stmt->conn->m->simple_command(stmt->conn, COM_STMT_FETCH, buf, sizeof(buf),
											  PROT_LAST /* we will handle the response packet*/,
											  FALSE, TRUE TSRMLS_CC)) {
		COPY_CLIENT_ERROR(*stmt->error_info, *stmt->conn->error_info);
		DBG_RETURN(FAIL);
	}

	row_packet->skip_extraction = stmt->result_bind? FALSE:TRUE;

	memset(stmt->upsert_status, 0, sizeof(*stmt->upsert_status));
	if (PASS == (ret = PACKET_READ(row_packet, result->conn)) && !row_packet->eof) {
		unsigned int i, field_count = result->field_count;

		if (!row_packet->skip_extraction) {
			result->m.unbuffered_free_last_data(result TSRMLS_CC);

			result->unbuf->last_row_data = row_packet->fields;
			result->unbuf->last_row_buffer = row_packet->row_buffer;
			row_packet->fields = NULL;
			row_packet->row_buffer = NULL;

			if (PASS != result->m.row_decoder(result->unbuf->last_row_buffer,
									  result->unbuf->last_row_data,
									  row_packet->field_count,
									  row_packet->fields_metadata,
									  result->conn->options->numeric_and_datetime_as_unicode,
									  result->conn->options->int_and_float_native,
									  result->conn->stats TSRMLS_CC))
			{
				DBG_RETURN(FAIL);						  
			}

			/* If no result bind, do nothing. We consumed the data */
			for (i = 0; i < field_count; i++) {
				if (stmt->result_bind[i].bound == TRUE) {
					zval *data = result->unbuf->last_row_data[i];
					/*
					  stmt->result_bind[i].zv has been already destructed
					  in result->m.unbuffered_free_last_data()
					*/
#ifndef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
					zval_dtor(stmt->result_bind[i].zv);
#endif
					DBG_INF_FMT("i=%u bound_var=%p type=%u refc=%u", i, stmt->result_bind[i].zv,
								Z_TYPE_P(data), Z_REFCOUNT_P(stmt->result_bind[i].zv));
					if (IS_NULL != (Z_TYPE_P(stmt->result_bind[i].zv) = Z_TYPE_P(data))) {
						if ((Z_TYPE_P(data) == IS_STRING
#if MYSQLND_UNICODE
							|| Z_TYPE_P(data) == IS_UNICODE
#endif
							)
							 && (result->meta->fields[i].max_length < (unsigned long) Z_STRLEN_P(data)))
						{
							result->meta->fields[i].max_length = Z_STRLEN_P(data);
						}
						stmt->result_bind[i].zv->value = data->value;
						/* copied data, thus also the ownership. Thus null data */
						ZVAL_NULL(data);
					}
				}
			}
		} else {
			DBG_INF("skipping extraction");
			/*
			  Data has been allocated and usually result->m.unbuffered_free_last_data()
			  frees it but we can't call this function as it will cause problems with
			  the bound variables. Thus we need to do part of what it does or Zend will
			  report leaks.
			*/
			row_packet->row_buffer->free_chunk(row_packet->row_buffer TSRMLS_CC);
			row_packet->row_buffer = NULL;
		}
		/* We asked for one row, the next one should be EOF, eat it */
		ret = PACKET_READ(row_packet, result->conn);
		if (row_packet->row_buffer) {
			row_packet->row_buffer->free_chunk(row_packet->row_buffer TSRMLS_CC);
			row_packet->row_buffer = NULL;
		}
		MYSQLND_INC_CONN_STATISTIC(stmt->conn->stats, STAT_ROWS_FETCHED_FROM_CLIENT_PS_CURSOR);

		result->unbuf->row_count++;
		*fetched_anything = TRUE;
	} else {
		*fetched_anything = FALSE;

		stmt->upsert_status->warning_count =
			stmt->conn->upsert_status->warning_count =
				row_packet->warning_count;

		stmt->upsert_status->server_status = 
			stmt->conn->upsert_status->server_status =
				row_packet->server_status;

		result->unbuf->eof_reached = row_packet->eof;
	}
	stmt->upsert_status->warning_count =
		stmt->conn->upsert_status->warning_count =
			row_packet->warning_count;
	stmt->upsert_status->server_status = 
		stmt->conn->upsert_status->server_status =
			row_packet->server_status;

	DBG_INF_FMT("ret=%s fetched=%u server_status=%u warnings=%u eof=%u",
				ret == PASS? "PASS":"FAIL", *fetched_anything,
				row_packet->server_status, row_packet->warning_count,
				result->unbuf->eof_reached);
	DBG_RETURN(ret);
}
/* }}} */


/* {{{ mysqlnd_stmt::fetch */
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, fetch)(MYSQLND_STMT * const s, zend_bool * const fetched_anything TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	enum_func_status ret;
	DBG_ENTER("mysqlnd_stmt::fetch");
	if (!stmt || !stmt->conn) {
		DBG_RETURN(FAIL);
	}
	DBG_INF_FMT("stmt=%lu", stmt->stmt_id);

	if (!stmt->result ||
		stmt->state < MYSQLND_STMT_WAITING_USE_OR_STORE) {
		SET_STMT_ERROR(stmt, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);

		DBG_ERR("command out of sync");
		DBG_RETURN(FAIL);
	} else if (stmt->state == MYSQLND_STMT_WAITING_USE_OR_STORE) {
		/* Execute only once. We have to free the previous contents of user's bound vars */

		stmt->default_rset_handler(s TSRMLS_CC);
	}
	stmt->state = MYSQLND_STMT_USER_FETCHING;

	SET_EMPTY_ERROR(*stmt->error_info);
	SET_EMPTY_ERROR(*stmt->conn->error_info);

	DBG_INF_FMT("result_bind=%p separated_once=%u", stmt->result_bind, stmt->result_zvals_separated_once);
	/*
	  The user might have not bound any variables for result.
	  Do the binding once she does it.
	*/
	if (stmt->result_bind && !stmt->result_zvals_separated_once) {
		unsigned int i;
		/*
		  mysqlnd_stmt_store_result() has been called free the bind
		  variables to prevent leaking of their previous content.
		*/
		for (i = 0; i < stmt->result->field_count; i++) {
			if (stmt->result_bind[i].bound == TRUE) {
				zval_dtor(stmt->result_bind[i].zv);
				ZVAL_NULL(stmt->result_bind[i].zv);
			}
		}
		stmt->result_zvals_separated_once = TRUE;
	}

	ret = stmt->result->m.fetch_row(stmt->result, (void*)s, 0, fetched_anything TSRMLS_CC);
	DBG_RETURN(ret);
}
/* }}} */


/* {{{ mysqlnd_stmt::reset */
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, reset)(MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	enum_func_status ret = PASS;
	zend_uchar cmd_buf[STMT_ID_LENGTH /* statement id */];

	DBG_ENTER("mysqlnd_stmt::reset");
	if (!stmt || !stmt->conn) {
		DBG_RETURN(FAIL);
	}
	DBG_INF_FMT("stmt=%lu", stmt->stmt_id);

	SET_EMPTY_ERROR(*stmt->error_info);
	SET_EMPTY_ERROR(*stmt->conn->error_info);

	if (stmt->stmt_id) {
		MYSQLND_CONN_DATA * conn = stmt->conn;
		if (stmt->param_bind) {
			unsigned int i;
			DBG_INF("resetting long data");
			/* Reset Long Data */
			for (i = 0; i < stmt->param_count; i++) {
				if (stmt->param_bind[i].flags & MYSQLND_PARAM_BIND_BLOB_USED) {
					stmt->param_bind[i].flags &= ~MYSQLND_PARAM_BIND_BLOB_USED;
				}
			}
		}

		s->m->flush(s TSRMLS_CC);

		/*
		  Don't free now, let the result be usable. When the stmt will again be
		  executed then the result set will be cleaned, the bound variables will
		  be separated before that.
		*/

		int4store(cmd_buf, stmt->stmt_id);
		if (CONN_GET_STATE(conn) == CONN_READY &&
			FAIL == (ret = conn->m->simple_command(conn, COM_STMT_RESET, cmd_buf,
												  sizeof(cmd_buf), PROT_OK_PACKET,
												  FALSE, TRUE TSRMLS_CC))) {
			COPY_CLIENT_ERROR(*stmt->error_info, *conn->error_info);
		}
		*stmt->upsert_status = *conn->upsert_status;
	}
	DBG_INF(ret == PASS? "PASS":"FAIL");
	DBG_RETURN(ret);
}
/* }}} */


/* {{{ mysqlnd_stmt::flush */
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, flush)(MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	enum_func_status ret = PASS;

	DBG_ENTER("mysqlnd_stmt::flush");
	if (!stmt || !stmt->conn) {
		DBG_RETURN(FAIL);
	}
	DBG_INF_FMT("stmt=%lu", stmt->stmt_id);

	if (stmt->stmt_id) {
		/*
		  If the user decided to close the statement right after execute()
		  We have to call the appropriate use_result() or store_result() and
		  clean.
		*/
		do {
			if (stmt->state == MYSQLND_STMT_WAITING_USE_OR_STORE) {
				DBG_INF("fetching result set header");
				stmt->default_rset_handler(s TSRMLS_CC);
				stmt->state = MYSQLND_STMT_USER_FETCHING;
			}

			if (stmt->result) {
				DBG_INF("skipping result");
				stmt->result->m.skip_result(stmt->result TSRMLS_CC);
			}
		} while (mysqlnd_stmt_more_results(s) && mysqlnd_stmt_next_result(s) == PASS);
	}
	DBG_INF(ret == PASS? "PASS":"FAIL");
	DBG_RETURN(ret);
}
/* }}} */


/* {{{ mysqlnd_stmt::send_long_data */
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, send_long_data)(MYSQLND_STMT * const s, unsigned int param_no,
							 				 const char * const data, unsigned long length TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	enum_func_status ret = FAIL;
	MYSQLND_CONN_DATA * conn;
	zend_uchar * cmd_buf;
	enum php_mysqlnd_server_command cmd = COM_STMT_SEND_LONG_DATA;

	DBG_ENTER("mysqlnd_stmt::send_long_data");
	if (!stmt || !stmt->conn) {
		DBG_RETURN(FAIL);
	}
	DBG_INF_FMT("stmt=%lu param_no=%u data_len=%lu", stmt->stmt_id, param_no, length);

	conn = stmt->conn;

	SET_EMPTY_ERROR(*stmt->error_info);
	SET_EMPTY_ERROR(*stmt->conn->error_info);

	if (stmt->state < MYSQLND_STMT_PREPARED) {
		SET_STMT_ERROR(stmt, CR_NO_PREPARE_STMT, UNKNOWN_SQLSTATE, mysqlnd_stmt_not_prepared);
		DBG_ERR("not prepared");
		DBG_RETURN(FAIL);
	}
	if (!stmt->param_bind) {
		SET_STMT_ERROR(stmt, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, mysqlnd_out_of_sync);
		DBG_ERR("command out of sync");
		DBG_RETURN(FAIL);
	}
	if (param_no >= stmt->param_count) {
		SET_STMT_ERROR(stmt, CR_INVALID_PARAMETER_NO, UNKNOWN_SQLSTATE, "Invalid parameter number");
		DBG_ERR("invalid param_no");
		DBG_RETURN(FAIL);
	}
	if (stmt->param_bind[param_no].type != MYSQL_TYPE_LONG_BLOB) {
		SET_STMT_ERROR(stmt, CR_INVALID_BUFFER_USE, UNKNOWN_SQLSTATE, mysqlnd_not_bound_as_blob);
		DBG_ERR("param_no is not of a blob type");
		DBG_RETURN(FAIL);
	}

	/*
	  XXX:	Unfortunately we have to allocate additional buffer to be able the
			additional data, which is like a header inside the payload.
			This should be optimised, but it will be a pervasive change, so
			conn->m->simple_command() will accept not a buffer, but actually MYSQLND_STRING*
			terminated by NULL, to send. If the strings are not big, we can collapse them
			on the buffer every connection has, but otherwise we will just send them
			one by one to the wire.
	*/

	if (CONN_GET_STATE(conn) == CONN_READY) {
		size_t packet_len;
		cmd_buf = mnd_emalloc(packet_len = STMT_ID_LENGTH + 2 + length);
		if (cmd_buf) {
			stmt->param_bind[param_no].flags |= MYSQLND_PARAM_BIND_BLOB_USED;

			int4store(cmd_buf, stmt->stmt_id);
			int2store(cmd_buf + STMT_ID_LENGTH, param_no);
			memcpy(cmd_buf + STMT_ID_LENGTH + 2, data, length);

			/* COM_STMT_SEND_LONG_DATA doesn't send an OK packet*/
			ret = conn->m->simple_command(conn, cmd, cmd_buf, packet_len, PROT_LAST , FALSE, TRUE TSRMLS_CC);
			mnd_efree(cmd_buf);
			if (FAIL == ret) {
				COPY_CLIENT_ERROR(*stmt->error_info, *conn->error_info);
			}
		} else {
			ret = FAIL;
			SET_OOM_ERROR(*stmt->error_info);
			SET_OOM_ERROR(*conn->error_info);
		}
		/*
		  Cover protocol error: COM_STMT_SEND_LONG_DATA was designed to be quick and not
		  sent response packets. According to documentation the only way to get an error
		  is to have out-of-memory on the server-side. However, that's not true, as if
		  max_allowed_packet_size is smaller than the chunk being sent to the server, the
		  latter will complain with an error message. However, normally we don't expect
		  an error message, thus we continue. When sending the next command, which expects
		  response we will read the unexpected data and error message will look weird.
		  Therefore we do non-blocking read to clean the line, if there is a need.
		  Nevertheless, there is a built-in protection when sending a command packet, that
		  checks if the line is clear - useful for debug purposes and to be switched off
		  in release builds.

		  Maybe we can make it automatic by checking what's the value of
		  max_allowed_packet_size on the server and resending the data.
		*/
#ifdef MYSQLND_DO_WIRE_CHECK_BEFORE_COMMAND
#if HAVE_USLEEP && !defined(PHP_WIN32)
		usleep(120000);
#endif
		if ((packet_len = conn->net->m.consume_uneaten_data(conn->net, cmd TSRMLS_CC))) {
			php_error_docref(NULL TSRMLS_CC, E_WARNING, "There was an error "
							 "while sending long data. Probably max_allowed_packet_size "
							 "is smaller than the data. You have to increase it or send "
							 "smaller chunks of data. Answer was "MYSQLND_SZ_T_SPEC" bytes long.", packet_len);
			SET_STMT_ERROR(stmt, CR_CONNECTION_ERROR, UNKNOWN_SQLSTATE,
							"Server responded to COM_STMT_SEND_LONG_DATA.");
			ret = FAIL;
		}
#endif
	}

	DBG_INF(ret == PASS? "PASS":"FAIL");
	DBG_RETURN(ret);
}
/* }}} */


/* {{{ mysqlnd_stmt::bind_parameters */
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, bind_parameters)(MYSQLND_STMT * const s, MYSQLND_PARAM_BIND * const param_bind TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	DBG_ENTER("mysqlnd_stmt::bind_param");
	if (!stmt || !stmt->conn) {
		DBG_RETURN(FAIL);
	}
	DBG_INF_FMT("stmt=%lu param_count=%u", stmt->stmt_id, stmt->param_count);

	if (stmt->state < MYSQLND_STMT_PREPARED) {
		SET_STMT_ERROR(stmt, CR_NO_PREPARE_STMT, UNKNOWN_SQLSTATE, mysqlnd_stmt_not_prepared);
		DBG_ERR("not prepared");
		if (param_bind) {
			s->m->free_parameter_bind(s, param_bind TSRMLS_CC);
		}
		DBG_RETURN(FAIL);
	}

	SET_EMPTY_ERROR(*stmt->error_info);
	SET_EMPTY_ERROR(*stmt->conn->error_info);

	if (stmt->param_count) {
		unsigned int i = 0;

		if (!param_bind) {
			SET_STMT_ERROR(stmt, CR_COMMANDS_OUT_OF_SYNC, UNKNOWN_SQLSTATE, "Re-binding (still) not supported");
			DBG_ERR("Re-binding (still) not supported");
			DBG_RETURN(FAIL);
		} else if (stmt->param_bind) {
			DBG_INF("Binding");
			/*
			  There is already result bound.
			  Forbid for now re-binding!!
			*/
			for (i = 0; i < stmt->param_count; i++) {
				/*
				  We may have the last reference, then call zval_ptr_dtor() or we may leak memory.
				  Switching from bind_one_parameter to bind_parameters may result in zv being NULL
				*/
				if (stmt->param_bind[i].zv) {
					zval_ptr_dtor(&stmt->param_bind[i].zv);
				}
			}
			if (stmt->param_bind != param_bind) {
				s->m->free_parameter_bind(s, stmt->param_bind TSRMLS_CC);
			}
		}

		stmt->param_bind = param_bind;
		for (i = 0; i < stmt->param_count; i++) {
			/* The client will use stmt_send_long_data */
			DBG_INF_FMT("%u is of type %u", i, stmt->param_bind[i].type);
			/* Prevent from freeing */
			/* Don't update is_ref, or we will leak during conversion */
			Z_ADDREF_P(stmt->param_bind[i].zv);
			stmt->param_bind[i].flags = 0;
			if (stmt->param_bind[i].type == MYSQL_TYPE_LONG_BLOB) {
				stmt->param_bind[i].flags &= ~MYSQLND_PARAM_BIND_BLOB_USED;
			}
		}
		stmt->send_types_to_server = 1;
	}
	DBG_INF("PASS");
	DBG_RETURN(PASS);
}
/* }}} */


/* {{{ mysqlnd_stmt::bind_one_parameter */
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, bind_one_parameter)(MYSQLND_STMT * const s, unsigned int param_no,
												 zval * const zv, zend_uchar type TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	DBG_ENTER("mysqlnd_stmt::bind_one_parameter");
	if (!stmt || !stmt->conn) {
		DBG_RETURN(FAIL);
	}
	DBG_INF_FMT("stmt=%lu param_no=%u param_count=%u type=%u", stmt->stmt_id, param_no, stmt->param_count, type);

	if (stmt->state < MYSQLND_STMT_PREPARED) {
		SET_STMT_ERROR(stmt, CR_NO_PREPARE_STMT, UNKNOWN_SQLSTATE, mysqlnd_stmt_not_prepared);
		DBG_ERR("not prepared");
		DBG_RETURN(FAIL);
	}

	if (param_no >= stmt->param_count) {
		SET_STMT_ERROR(stmt, CR_INVALID_PARAMETER_NO, UNKNOWN_SQLSTATE, "Invalid parameter number");
		DBG_ERR("invalid param_no");
		DBG_RETURN(FAIL);
	}
	SET_EMPTY_ERROR(*stmt->error_info);
	SET_EMPTY_ERROR(*stmt->conn->error_info);

	if (stmt->param_count) {
		if (!stmt->param_bind) {
			stmt->param_bind = mnd_pecalloc(stmt->param_count, sizeof(MYSQLND_PARAM_BIND), stmt->persistent);
			if (!stmt->param_bind) {
				DBG_RETURN(FAIL);
			}
		}

		/* Prevent from freeing */
		/* Don't update is_ref, or we will leak during conversion */
		Z_ADDREF_P(zv);
		DBG_INF("Binding");
		/* Release what we had, if we had */
		if (stmt->param_bind[param_no].zv) {
			zval_ptr_dtor(&stmt->param_bind[param_no].zv);
		}
		if (type == MYSQL_TYPE_LONG_BLOB) {
			/* The client will use stmt_send_long_data */
			stmt->param_bind[param_no].flags &= ~MYSQLND_PARAM_BIND_BLOB_USED;
		}
		stmt->param_bind[param_no].zv = zv;
		stmt->param_bind[param_no].type = type;

		stmt->send_types_to_server = 1;
	}
	DBG_INF("PASS");
	DBG_RETURN(PASS);
}
/* }}} */


/* {{{ mysqlnd_stmt::refresh_bind_param */
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, refresh_bind_param)(MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	DBG_ENTER("mysqlnd_stmt::refresh_bind_param");
	if (!stmt || !stmt->conn) {
		DBG_RETURN(FAIL);
	}
	DBG_INF_FMT("stmt=%lu param_count=%u", stmt->stmt_id, stmt->param_count);

	if (stmt->state < MYSQLND_STMT_PREPARED) {
		SET_STMT_ERROR(stmt, CR_NO_PREPARE_STMT, UNKNOWN_SQLSTATE, mysqlnd_stmt_not_prepared);
		DBG_ERR("not prepared");
		DBG_RETURN(FAIL);
	}

	SET_EMPTY_ERROR(*stmt->error_info);
	SET_EMPTY_ERROR(*stmt->conn->error_info);

	if (stmt->param_count) {
		stmt->send_types_to_server = 1;
	}
	DBG_RETURN(PASS);
}
/* }}} */


/* {{{ mysqlnd_stmt::bind_result */
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, bind_result)(MYSQLND_STMT * const s,
										  MYSQLND_RESULT_BIND * const result_bind TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	DBG_ENTER("mysqlnd_stmt::bind_result");
	if (!stmt || !stmt->conn) {
		DBG_RETURN(FAIL);
	}
	DBG_INF_FMT("stmt=%lu field_count=%u", stmt->stmt_id, stmt->field_count);

	if (stmt->state < MYSQLND_STMT_PREPARED) {
		SET_STMT_ERROR(stmt, CR_NO_PREPARE_STMT, UNKNOWN_SQLSTATE, mysqlnd_stmt_not_prepared);
		if (result_bind) {
			s->m->free_result_bind(s, result_bind TSRMLS_CC);
		}
		DBG_ERR("not prepared");
		DBG_RETURN(FAIL);
	}

	SET_EMPTY_ERROR(*stmt->error_info);
	SET_EMPTY_ERROR(*stmt->conn->error_info);

	if (stmt->field_count) {
		unsigned int i = 0;

		if (!result_bind) {
			DBG_ERR("no result bind passed");
			DBG_RETURN(FAIL);
		}

		mysqlnd_stmt_separate_result_bind(s TSRMLS_CC);
		stmt->result_zvals_separated_once = FALSE;
		stmt->result_bind = result_bind;
		for (i = 0; i < stmt->field_count; i++) {
			/* Prevent from freeing */
			Z_ADDREF_P(stmt->result_bind[i].zv);
			DBG_INF_FMT("ref of %p = %u", stmt->result_bind[i].zv, Z_REFCOUNT_P(stmt->result_bind[i].zv));
			/*
			  Don't update is_ref !!! it's not our job
			  Otherwise either 009.phpt or mysqli_stmt_bind_result.phpt
			  will fail.
			*/
			stmt->result_bind[i].bound = TRUE;
		}
	} else if (result_bind) {
		s->m->free_result_bind(s, result_bind TSRMLS_CC);
	}
	DBG_INF("PASS");
	DBG_RETURN(PASS);
}
/* }}} */


/* {{{ mysqlnd_stmt::bind_result */
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, bind_one_result)(MYSQLND_STMT * const s, unsigned int param_no TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	DBG_ENTER("mysqlnd_stmt::bind_result");
	if (!stmt || !stmt->conn) {
		DBG_RETURN(FAIL);
	}
	DBG_INF_FMT("stmt=%lu field_count=%u", stmt->stmt_id, stmt->field_count);

	if (stmt->state < MYSQLND_STMT_PREPARED) {
		SET_STMT_ERROR(stmt, CR_NO_PREPARE_STMT, UNKNOWN_SQLSTATE, mysqlnd_stmt_not_prepared);
		DBG_ERR("not prepared");
		DBG_RETURN(FAIL);
	}

	if (param_no >= stmt->field_count) {
		SET_STMT_ERROR(stmt, CR_INVALID_PARAMETER_NO, UNKNOWN_SQLSTATE, "Invalid parameter number");
		DBG_ERR("invalid param_no");
		DBG_RETURN(FAIL);
	}

	SET_EMPTY_ERROR(*stmt->error_info);
	SET_EMPTY_ERROR(*stmt->conn->error_info);

	if (stmt->field_count) {
		mysqlnd_stmt_separate_one_result_bind(s, param_no TSRMLS_CC);
		/* Guaranteed is that stmt->result_bind is NULL */
		if (!stmt->result_bind) {
			stmt->result_bind = mnd_pecalloc(stmt->field_count, sizeof(MYSQLND_RESULT_BIND), stmt->persistent);
		} else {
			stmt->result_bind = mnd_perealloc(stmt->result_bind, stmt->field_count * sizeof(MYSQLND_RESULT_BIND), stmt->persistent);
		}
		if (!stmt->result_bind) {
			DBG_RETURN(FAIL);
		}
		ALLOC_INIT_ZVAL(stmt->result_bind[param_no].zv);
		/*
		  Don't update is_ref !!! it's not our job
		  Otherwise either 009.phpt or mysqli_stmt_bind_result.phpt
		  will fail.
		*/
		stmt->result_bind[param_no].bound = TRUE;
	}
	DBG_INF("PASS");
	DBG_RETURN(PASS);
}
/* }}} */


/* {{{ mysqlnd_stmt::insert_id */
static uint64_t
MYSQLND_METHOD(mysqlnd_stmt, insert_id)(const MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	return stmt? stmt->upsert_status->last_insert_id : 0;
}
/* }}} */


/* {{{ mysqlnd_stmt::affected_rows */
static uint64_t
MYSQLND_METHOD(mysqlnd_stmt, affected_rows)(const MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	return stmt? stmt->upsert_status->affected_rows : 0;
}
/* }}} */


/* {{{ mysqlnd_stmt::num_rows */
static uint64_t
MYSQLND_METHOD(mysqlnd_stmt, num_rows)(const MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	return stmt && stmt->result? mysqlnd_num_rows(stmt->result):0;
}
/* }}} */


/* {{{ mysqlnd_stmt::warning_count */
static unsigned int
MYSQLND_METHOD(mysqlnd_stmt, warning_count)(const MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	return stmt? stmt->upsert_status->warning_count : 0;
}
/* }}} */


/* {{{ mysqlnd_stmt::server_status */
static unsigned int
MYSQLND_METHOD(mysqlnd_stmt, server_status)(const MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	return stmt? stmt->upsert_status->server_status : 0;
}
/* }}} */


/* {{{ mysqlnd_stmt::field_count */
static unsigned int
MYSQLND_METHOD(mysqlnd_stmt, field_count)(const MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	return stmt? stmt->field_count : 0;
}
/* }}} */


/* {{{ mysqlnd_stmt::param_count */
static unsigned int
MYSQLND_METHOD(mysqlnd_stmt, param_count)(const MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	return stmt? stmt->param_count : 0;
}
/* }}} */


/* {{{ mysqlnd_stmt::errno */
static unsigned int
MYSQLND_METHOD(mysqlnd_stmt, errno)(const MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	return stmt? stmt->error_info->error_no : 0;
}
/* }}} */


/* {{{ mysqlnd_stmt::error */
static const char *
MYSQLND_METHOD(mysqlnd_stmt, error)(const MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	return stmt? stmt->error_info->error : 0;
}
/* }}} */


/* {{{ mysqlnd_stmt::sqlstate */
static const char *
MYSQLND_METHOD(mysqlnd_stmt, sqlstate)(const MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	return stmt && stmt->error_info->sqlstate[0] ? stmt->error_info->sqlstate:MYSQLND_SQLSTATE_NULL;
}
/* }}} */


/* {{{ mysqlnd_stmt::data_seek */
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, data_seek)(const MYSQLND_STMT * const s, uint64_t row TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	return stmt && stmt->result? stmt->result->m.seek_data(stmt->result, row TSRMLS_CC) : FAIL;
}
/* }}} */


/* {{{ mysqlnd_stmt::param_metadata */
static MYSQLND_RES *
MYSQLND_METHOD(mysqlnd_stmt, param_metadata)(MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	if (!stmt || !stmt->param_count) {
		return NULL;
	}
	return NULL;
}
/* }}} */


/* {{{ mysqlnd_stmt::result_metadata */
static MYSQLND_RES *
MYSQLND_METHOD(mysqlnd_stmt, result_metadata)(MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	MYSQLND_RES *result;

	DBG_ENTER("mysqlnd_stmt::result_metadata");
	if (!stmt) {
		DBG_RETURN(NULL);
	}
	DBG_INF_FMT("stmt=%u field_count=%u", stmt->stmt_id, stmt->field_count);

	if (!stmt->field_count || !stmt->conn || !stmt->result || !stmt->result->meta) {
		DBG_INF("NULL");
		DBG_RETURN(NULL);
	}

	if (stmt->update_max_length && stmt->result->stored_data) {
		/* stored result, we have to update the max_length before we clone the meta data :( */
		stmt->result->m.initialize_result_set_rest(stmt->result TSRMLS_CC);
	}
	/*
	  TODO: This implementation is kind of a hack,
			find a better way to do it. In different functions I have put
			fuses to check for result->m.fetch_row() being NULL. This should
			be handled in a better way.

	  In the meantime we don't need a zval cache reference for this fake
	  result set, so we don't get one.
	*/
	do {
		result = stmt->conn->m->result_init(stmt->field_count, stmt->persistent TSRMLS_CC);
		if (!result) {
			break;
		}
		result->type = MYSQLND_RES_NORMAL;
		result->m.fetch_row = result->m.fetch_row_normal_unbuffered;
		result->unbuf = mnd_ecalloc(1, sizeof(MYSQLND_RES_UNBUFFERED));
		if (!result->unbuf) {
			break;
		}
		result->unbuf->eof_reached = TRUE;
		result->meta = stmt->result->meta->m->clone_metadata(stmt->result->meta, FALSE TSRMLS_CC);
		if (!result->meta) {
			break;
		}

		DBG_INF_FMT("result=%p", result);
		DBG_RETURN(result);
	} while (0);

	SET_OOM_ERROR(*stmt->conn->error_info);
	if (result) {
		result->m.free_result(result, TRUE TSRMLS_CC);
	}
	DBG_RETURN(NULL);
}
/* }}} */


/* {{{ mysqlnd_stmt::attr_set */
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, attr_set)(MYSQLND_STMT * const s,
									   enum mysqlnd_stmt_attr attr_type,
									   const void * const value TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	DBG_ENTER("mysqlnd_stmt::attr_set");
	if (!stmt) {
		DBG_RETURN(FAIL);
	}
	DBG_INF_FMT("stmt=%lu attr_type=%u", stmt->stmt_id, attr_type);

	switch (attr_type) {
		case STMT_ATTR_UPDATE_MAX_LENGTH:{
			zend_uchar bval = *(zend_uchar *) value;
			/*
			  XXX : libmysql uses my_bool, but mysqli uses ulong as storage on the stack
			  and mysqlnd won't be used out of the scope of PHP -> use ulong.
			*/
			stmt->update_max_length = bval? TRUE:FALSE;
			break;
		}
		case STMT_ATTR_CURSOR_TYPE: {
			unsigned int ival = *(unsigned int *) value;
			if (ival > (unsigned long) CURSOR_TYPE_READ_ONLY) {
				SET_STMT_ERROR(stmt, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, "Not implemented");
				DBG_INF("FAIL");
				DBG_RETURN(FAIL);
			}
			stmt->flags = ival;
			break;
		}
		case STMT_ATTR_PREFETCH_ROWS: {
			unsigned int ival = *(unsigned int *) value;
			if (ival == 0) {
				ival = MYSQLND_DEFAULT_PREFETCH_ROWS;
			} else if (ival > 1) {
				SET_STMT_ERROR(stmt, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, "Not implemented");
				DBG_INF("FAIL");
				DBG_RETURN(FAIL);
			}
			stmt->prefetch_rows = ival;
			break;
		}
		default:
			SET_STMT_ERROR(stmt, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, "Not implemented");
			DBG_RETURN(FAIL);
	}
	DBG_INF("PASS");
	DBG_RETURN(PASS);
}
/* }}} */


/* {{{ mysqlnd_stmt::attr_get */
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, attr_get)(const MYSQLND_STMT * const s,
									   enum mysqlnd_stmt_attr attr_type,
									   void * const value TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	DBG_ENTER("mysqlnd_stmt::attr_set");
	if (!stmt) {
		DBG_RETURN(FAIL);
	}
	DBG_INF_FMT("stmt=%lu attr_type=%u", stmt->stmt_id, attr_type);

	switch (attr_type) {
		case STMT_ATTR_UPDATE_MAX_LENGTH:
			*(zend_bool *) value= stmt->update_max_length;
			break;
		case STMT_ATTR_CURSOR_TYPE:
			*(unsigned long *) value= stmt->flags;
			break;
		case STMT_ATTR_PREFETCH_ROWS:
			*(unsigned long *) value= stmt->prefetch_rows;
			break;
		default:
			DBG_RETURN(FAIL);
	}
	DBG_INF_FMT("value=%lu", value);
	DBG_RETURN(PASS);
}
/* }}} */


/* free_result() doesn't actually free stmt->result but only the buffers */
/* {{{ mysqlnd_stmt::free_result */
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, free_result)(MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	DBG_ENTER("mysqlnd_stmt::free_result");
	if (!stmt || !stmt->conn) {
		DBG_RETURN(FAIL);
	}
	DBG_INF_FMT("stmt=%lu", stmt->stmt_id);

	if (!stmt->result) {
		DBG_INF("no result");
		DBG_RETURN(PASS);
	}

	/*
	  If right after execute() we have to call the appropriate
	  use_result() or store_result() and clean.
	*/
	if (stmt->state == MYSQLND_STMT_WAITING_USE_OR_STORE) {
		DBG_INF("fetching result set header");
		/* Do implicit use_result and then flush the result */
		stmt->default_rset_handler = s->m->use_result;
		stmt->default_rset_handler(s TSRMLS_CC);
	}

	if (stmt->state > MYSQLND_STMT_WAITING_USE_OR_STORE) {
		DBG_INF("skipping result");
		/* Flush if anything is left and unbuffered set */
		stmt->result->m.skip_result(stmt->result TSRMLS_CC);
		/*
		  Separate the bound variables, which point to the result set, then
		  destroy the set.
		*/
		mysqlnd_stmt_separate_result_bind(s TSRMLS_CC);

		/* Now we can destroy the result set */
		stmt->result->m.free_result_buffers(stmt->result TSRMLS_CC);
	}

	if (stmt->state > MYSQLND_STMT_PREPARED) {
		/* As the buffers have been freed, we should go back to PREPARED */
		stmt->state = MYSQLND_STMT_PREPARED;
	}

	/* Line is free! */
	CONN_SET_STATE(stmt->conn, CONN_READY);

	DBG_RETURN(PASS);
}
/* }}} */


/* {{{ mysqlnd_stmt_separate_result_bind */
static void
mysqlnd_stmt_separate_result_bind(MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	unsigned int i;

	DBG_ENTER("mysqlnd_stmt_separate_result_bind");
	if (!stmt) {
		DBG_VOID_RETURN;
	}
	DBG_INF_FMT("stmt=%lu result_bind=%p field_count=%u", stmt->stmt_id, stmt->result_bind, stmt->field_count);

	if (!stmt->result_bind) {
		DBG_VOID_RETURN;
	}

	/*
	  Because only the bound variables can point to our internal buffers, then
	  separate or free only them. Free is possible because the user could have
	  lost reference.
	*/
	for (i = 0; i < stmt->field_count; i++) {
		/* Let's try with no cache */
		if (stmt->result_bind[i].bound == TRUE) {
			DBG_INF_FMT("%u has refcount=%u", i, Z_REFCOUNT_P(stmt->result_bind[i].zv));
			/*
			  We have to separate the actual zval value of the bound
			  variable from our allocated zvals or we will face double-free
			*/
			if (Z_REFCOUNT_P(stmt->result_bind[i].zv) > 1) {
#ifdef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
				zval_copy_ctor(stmt->result_bind[i].zv);
#endif
				zval_ptr_dtor(&stmt->result_bind[i].zv);
			} else {
				/*
				  If it is a string, what is pointed will be freed
				  later in free_result(). We need to remove the variable to
				  which the user has lost reference.
				*/
#ifdef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
				ZVAL_NULL(stmt->result_bind[i].zv);
#endif
				zval_ptr_dtor(&stmt->result_bind[i].zv);
			}
		}
	}
	s->m->free_result_bind(s, stmt->result_bind TSRMLS_CC);
	stmt->result_bind = NULL;

	DBG_VOID_RETURN;
}
/* }}} */


/* {{{ mysqlnd_stmt_separate_one_result_bind */
static void
mysqlnd_stmt_separate_one_result_bind(MYSQLND_STMT * const s, unsigned int param_no TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	DBG_ENTER("mysqlnd_stmt_separate_one_result_bind");
	if (!stmt) {
		DBG_VOID_RETURN;
	}
	DBG_INF_FMT("stmt=%lu result_bind=%p field_count=%u param_no=%u", stmt->stmt_id, stmt->result_bind, stmt->field_count, param_no);

	if (!stmt->result_bind) {
		DBG_VOID_RETURN;
	}

	/*
	  Because only the bound variables can point to our internal buffers, then
	  separate or free only them. Free is possible because the user could have
	  lost reference.
	*/
	/* Let's try with no cache */
	if (stmt->result_bind[param_no].bound == TRUE) {
		DBG_INF_FMT("%u has refcount=%u", param_no, Z_REFCOUNT_P(stmt->result_bind[param_no].zv));
		/*
		  We have to separate the actual zval value of the bound
		  variable from our allocated zvals or we will face double-free
		*/
		if (Z_REFCOUNT_P(stmt->result_bind[param_no].zv) > 1) {
#ifdef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
			zval_copy_ctor(stmt->result_bind[param_no].zv);
#endif
			zval_ptr_dtor(&stmt->result_bind[param_no].zv);
		} else {
			/*
			  If it is a string, what is pointed will be freed
			  later in free_result(). We need to remove the variable to
			  which the user has lost reference.
			*/
#ifdef WE_DONT_COPY_IN_BUFFERED_AND_UNBUFFERED_BECAUSEOF_IS_REF
			ZVAL_NULL(stmt->result_bind[param_no].zv);
#endif
			zval_ptr_dtor(&stmt->result_bind[param_no].zv);
		}
	}

	DBG_VOID_RETURN;
}
/* }}} */


/* {{{ mysqlnd_stmt::free_stmt_result */
static void
MYSQLND_METHOD(mysqlnd_stmt, free_stmt_result)(MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	DBG_ENTER("mysqlnd_stmt::free_stmt_result");
	if (!stmt) {
		DBG_VOID_RETURN;
	}

	/*
	  First separate the bound variables, which point to the result set, then
	  destroy the set.
	*/
	mysqlnd_stmt_separate_result_bind(s TSRMLS_CC);
	/* Not every statement has a result set attached */
	if (stmt->result) {
		stmt->result->m.free_result_internal(stmt->result TSRMLS_CC);
		stmt->result = NULL;
	}
	if (stmt->error_info->error_list) {
		zend_llist_clean(stmt->error_info->error_list);
		mnd_pefree(stmt->error_info->error_list, s->persistent);
		stmt->error_info->error_list = NULL;
	}

	DBG_VOID_RETURN;
}
/* }}} */


/* {{{ mysqlnd_stmt::free_stmt_content */
static void
MYSQLND_METHOD(mysqlnd_stmt, free_stmt_content)(MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	DBG_ENTER("mysqlnd_stmt::free_stmt_content");
	if (!stmt) {
		DBG_VOID_RETURN;
	}
	DBG_INF_FMT("stmt=%lu param_bind=%p param_count=%u", stmt->stmt_id, stmt->param_bind, stmt->param_count);

	/* Destroy the input bind */
	if (stmt->param_bind) {
		unsigned int i;
		/*
		  Because only the bound variables can point to our internal buffers, then
		  separate or free only them. Free is possible because the user could have
		  lost reference.
		*/
		for (i = 0; i < stmt->param_count; i++) {
			/*
			  If bind_one_parameter was used, but not everything was
			  bound and nothing was fetched, then some `zv` could be NULL
			*/
			if (stmt->param_bind[i].zv) {
				zval_ptr_dtor(&stmt->param_bind[i].zv);
			}
		}
		s->m->free_parameter_bind(s, stmt->param_bind TSRMLS_CC);
		stmt->param_bind = NULL;
	}

	MYSQLND_METHOD(mysqlnd_stmt, free_stmt_result)(s TSRMLS_CC);
	DBG_VOID_RETURN;
}
/* }}} */


/* {{{ mysqlnd_stmt::net_close */
static enum_func_status
MYSQLND_METHOD_PRIVATE(mysqlnd_stmt, net_close)(MYSQLND_STMT * const s, zend_bool implicit TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	MYSQLND_CONN_DATA * conn;
	zend_uchar cmd_buf[STMT_ID_LENGTH /* statement id */];
	enum_mysqlnd_collected_stats statistic = STAT_LAST;

	DBG_ENTER("mysqlnd_stmt::net_close");
	if (!stmt || !stmt->conn) {
		DBG_RETURN(FAIL);
	}
	DBG_INF_FMT("stmt=%lu", stmt->stmt_id);

	conn = stmt->conn;

	SET_EMPTY_ERROR(*stmt->error_info);
	SET_EMPTY_ERROR(*stmt->conn->error_info);

	/*
	  If the user decided to close the statement right after execute()
	  We have to call the appropriate use_result() or store_result() and
	  clean.
	*/
	do {
		if (stmt->state == MYSQLND_STMT_WAITING_USE_OR_STORE) {
			DBG_INF("fetching result set header");
			stmt->default_rset_handler(s TSRMLS_CC);
			stmt->state = MYSQLND_STMT_USER_FETCHING;
		}

		/* unbuffered set not fetched to the end ? Clean the line */
		if (stmt->result) {
			DBG_INF("skipping result");
			stmt->result->m.skip_result(stmt->result TSRMLS_CC);
		}
	} while (mysqlnd_stmt_more_results(s) && mysqlnd_stmt_next_result(s) == PASS);
	/*
	  After this point we are allowed to free the result set,
	  as we have cleaned the line
	*/
	if (stmt->stmt_id) {
		MYSQLND_INC_GLOBAL_STATISTIC(implicit == TRUE?	STAT_FREE_RESULT_IMPLICIT:
														STAT_FREE_RESULT_EXPLICIT);

		int4store(cmd_buf, stmt->stmt_id);
		if (CONN_GET_STATE(conn) == CONN_READY &&
			FAIL == conn->m->simple_command(conn, COM_STMT_CLOSE, cmd_buf, sizeof(cmd_buf),
										   PROT_LAST /* COM_STMT_CLOSE doesn't send an OK packet*/,
										   FALSE, TRUE TSRMLS_CC)) {
			COPY_CLIENT_ERROR(*stmt->error_info, *conn->error_info);
			DBG_RETURN(FAIL);
		}
	}
	switch (stmt->execute_count) {
		case 0:
			statistic = STAT_PS_PREPARED_NEVER_EXECUTED;
			break;
		case 1:
			statistic = STAT_PS_PREPARED_ONCE_USED;
			break;
		default:
			break;
	}
	if (statistic != STAT_LAST) {
		MYSQLND_INC_CONN_STATISTIC(conn->stats, statistic);
	}

	if (stmt->execute_cmd_buffer.buffer) {
		mnd_pefree(stmt->execute_cmd_buffer.buffer, stmt->persistent);
		stmt->execute_cmd_buffer.buffer = NULL;
	}

	s->m->free_stmt_content(s TSRMLS_CC);

	if (stmt->conn) {
		stmt->conn->m->free_reference(stmt->conn TSRMLS_CC);
		stmt->conn = NULL;
	}

	DBG_RETURN(PASS);
}
/* }}} */

/* {{{ mysqlnd_stmt::dtor */
static enum_func_status
MYSQLND_METHOD(mysqlnd_stmt, dtor)(MYSQLND_STMT * const s, zend_bool implicit TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = (s != NULL) ? s->data:NULL;
	enum_func_status ret = FAIL;
	zend_bool persistent = (s != NULL) ? s->persistent : 0;

	DBG_ENTER("mysqlnd_stmt::dtor");
	if (stmt) {
		DBG_INF_FMT("stmt=%p", stmt);

		MYSQLND_INC_GLOBAL_STATISTIC(implicit == TRUE?	STAT_STMT_CLOSE_IMPLICIT:
														STAT_STMT_CLOSE_EXPLICIT);

		ret = s->m->net_close(s, implicit TSRMLS_CC);
		mnd_pefree(stmt, persistent);
	}
	mnd_pefree(s, persistent);

	DBG_INF(ret == PASS? "PASS":"FAIL");
	DBG_RETURN(ret);
}
/* }}} */


/* {{{ mysqlnd_stmt::alloc_param_bind */
static MYSQLND_PARAM_BIND *
MYSQLND_METHOD(mysqlnd_stmt, alloc_param_bind)(MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	DBG_ENTER("mysqlnd_stmt::alloc_param_bind");
	if (!stmt) {
		DBG_RETURN(NULL);
	}
	DBG_RETURN(mnd_pecalloc(stmt->param_count, sizeof(MYSQLND_PARAM_BIND), stmt->persistent));
}
/* }}} */


/* {{{ mysqlnd_stmt::alloc_result_bind */
static MYSQLND_RESULT_BIND *
MYSQLND_METHOD(mysqlnd_stmt, alloc_result_bind)(MYSQLND_STMT * const s TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	DBG_ENTER("mysqlnd_stmt::alloc_result_bind");
	if (!stmt) {
		DBG_RETURN(NULL);
	}
	DBG_RETURN(mnd_pecalloc(stmt->field_count, sizeof(MYSQLND_RESULT_BIND), stmt->persistent));
}
/* }}} */


/* {{{ param_bind::free_parameter_bind */
PHPAPI void
MYSQLND_METHOD(mysqlnd_stmt, free_parameter_bind)(MYSQLND_STMT * const s, MYSQLND_PARAM_BIND * param_bind TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	if (stmt) {
		mnd_pefree(param_bind, stmt->persistent);
	}
}
/* }}} */


/* {{{ mysqlnd_stmt::free_result_bind */
PHPAPI void
MYSQLND_METHOD(mysqlnd_stmt, free_result_bind)(MYSQLND_STMT * const s, MYSQLND_RESULT_BIND * result_bind TSRMLS_DC)
{
	MYSQLND_STMT_DATA * stmt = s? s->data:NULL;
	if (stmt) {
		mnd_pefree(result_bind, stmt->persistent);
	}
}
/* }}} */



MYSQLND_CLASS_METHODS_START(mysqlnd_stmt)
	MYSQLND_METHOD(mysqlnd_stmt, prepare),
	MYSQLND_METHOD(mysqlnd_stmt, execute),
	MYSQLND_METHOD(mysqlnd_stmt, use_result),
	MYSQLND_METHOD(mysqlnd_stmt, store_result),
	MYSQLND_METHOD(mysqlnd_stmt, get_result),
	MYSQLND_METHOD(mysqlnd_stmt, more_results),
	MYSQLND_METHOD(mysqlnd_stmt, next_result),
	MYSQLND_METHOD(mysqlnd_stmt, free_result),
	MYSQLND_METHOD(mysqlnd_stmt, data_seek),
	MYSQLND_METHOD(mysqlnd_stmt, reset),
	MYSQLND_METHOD_PRIVATE(mysqlnd_stmt, net_close),
	MYSQLND_METHOD(mysqlnd_stmt, dtor),

	MYSQLND_METHOD(mysqlnd_stmt, fetch),

	MYSQLND_METHOD(mysqlnd_stmt, bind_parameters),
	MYSQLND_METHOD(mysqlnd_stmt, bind_one_parameter),
	MYSQLND_METHOD(mysqlnd_stmt, refresh_bind_param),
	MYSQLND_METHOD(mysqlnd_stmt, bind_result),
	MYSQLND_METHOD(mysqlnd_stmt, bind_one_result),
	MYSQLND_METHOD(mysqlnd_stmt, send_long_data),
	MYSQLND_METHOD(mysqlnd_stmt, param_metadata),
	MYSQLND_METHOD(mysqlnd_stmt, result_metadata),

	MYSQLND_METHOD(mysqlnd_stmt, insert_id),
	MYSQLND_METHOD(mysqlnd_stmt, affected_rows),
	MYSQLND_METHOD(mysqlnd_stmt, num_rows),

	MYSQLND_METHOD(mysqlnd_stmt, param_count),
	MYSQLND_METHOD(mysqlnd_stmt, field_count),
	MYSQLND_METHOD(mysqlnd_stmt, warning_count),

	MYSQLND_METHOD(mysqlnd_stmt, errno),
	MYSQLND_METHOD(mysqlnd_stmt, error),
	MYSQLND_METHOD(mysqlnd_stmt, sqlstate),

	MYSQLND_METHOD(mysqlnd_stmt, attr_get),
	MYSQLND_METHOD(mysqlnd_stmt, attr_set),


	MYSQLND_METHOD(mysqlnd_stmt, alloc_param_bind),
	MYSQLND_METHOD(mysqlnd_stmt, alloc_result_bind),
	MYSQLND_METHOD(mysqlnd_stmt, free_parameter_bind),
	MYSQLND_METHOD(mysqlnd_stmt, free_result_bind),
	MYSQLND_METHOD(mysqlnd_stmt, server_status),
	mysqlnd_stmt_execute_generate_request,
	mysqlnd_stmt_execute_parse_response,
	MYSQLND_METHOD(mysqlnd_stmt, free_stmt_content),
	MYSQLND_METHOD(mysqlnd_stmt, flush)
MYSQLND_CLASS_METHODS_END;


/* {{{ _mysqlnd_stmt_init */
MYSQLND_STMT *
_mysqlnd_stmt_init(MYSQLND_CONN_DATA * const conn TSRMLS_DC)
{
	MYSQLND_STMT * ret;
	DBG_ENTER("_mysqlnd_stmt_init");
	ret = MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_object_factory).get_prepared_statement(conn TSRMLS_CC);
	DBG_RETURN(ret);
}
/* }}} */


/* {{{ _mysqlnd_init_ps_subsystem */
void _mysqlnd_init_ps_subsystem()
{
	mysqlnd_stmt_set_methods(&MYSQLND_CLASS_METHOD_TABLE_NAME(mysqlnd_stmt));
	_mysqlnd_init_ps_fetch_subsystem();
}
/* }}} */


/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */