1 /*
2 Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4 The MySQL Connector/C is licensed under the terms of the GPLv2
5 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
6 MySQL Connectors. There are special exceptions to the terms and
7 conditions of the GPLv2 as it is applied to this software, see the
8 FLOSS License Exception
9 <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
10 
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published
13 by the Free Software Foundation; version 2 of the License.
14 
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
19 
20 You should have received a copy of the GNU General Public License along
21 with this program; if not, write to the Free Software Foundation, Inc.,
22 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 #include "my_test.h"
25 
26 #define MY_INT64_NUM_DECIMAL_DIGITS 21
27 #define MAX_INDEXES 64
28 
29 /* A workaround for Sun Forte 5.6 on Solaris x86 */
30 
cmp_double(double * a,double * b)31 static int cmp_double(double *a, double *b)
32 {
33   return *a == *b;
34   return OK;
35 }
36 
37 /* Test BUG#1115 (incorrect string parameter value allocation) */
38 
test_conc67(MYSQL * mysql)39 static int test_conc67(MYSQL *mysql)
40 {
41   MYSQL_STMT *stmt= mysql_stmt_init(mysql);
42   const char *query= "SELECT a,b FROM conc67 WHERE a=?";
43   int rc, i;
44   MYSQL_BIND bind[2];
45   char val[20];
46   MYSQL_BIND rbind;
47   MYSQL_RES *res;
48   ulong prefetch_rows= 1000;
49   ulong cursor_type= CURSOR_TYPE_READ_ONLY;
50 
51   rc= mysql_query(mysql, "DROP TABLE IF EXISTS conc67");
52   check_mysql_rc(rc, mysql);
53 
54   rc= mysql_query(mysql, "CREATE TABLE conc67 (a int, b text)");
55   check_mysql_rc(rc, mysql);
56 
57   rc= mysql_query(mysql, "INSERT INTO conc67 VALUES (1, 'foo')");
58   check_mysql_rc(rc, mysql);
59 
60   rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, &cursor_type);
61   check_stmt_rc(rc, stmt);
62   rc= mysql_stmt_attr_set(stmt, STMT_ATTR_PREFETCH_ROWS, &prefetch_rows);
63   check_stmt_rc(rc, stmt);
64 
65   rc= mysql_stmt_prepare(stmt, SL(query));
66   check_stmt_rc(rc, stmt);
67 
68   memset(&rbind, 0, sizeof(MYSQL_BIND));
69   i= 1;
70   rbind.buffer_type= MYSQL_TYPE_LONG;
71   rbind.buffer= &i;
72   rbind.buffer_length= 4;
73   mysql_stmt_bind_param(stmt, &rbind);
74 
75   rc= mysql_stmt_execute(stmt);
76   check_stmt_rc(rc, stmt);
77 
78   res= mysql_stmt_result_metadata(stmt);
79   mysql_free_result(res);
80 
81   memset(bind, 0, 2 * sizeof(MYSQL_BIND));
82 
83   i= 0;
84   bind[0].buffer_type= MYSQL_TYPE_LONG;
85   bind[0].buffer= &i;
86   bind[0].buffer_length= 4;
87   bind[1].buffer_type= MYSQL_TYPE_STRING;
88   bind[1].buffer= &val;
89   bind[1].buffer_length= 20;
90 
91   mysql_stmt_bind_result(stmt, bind);
92 
93   rc= mysql_stmt_fetch(stmt);
94   check_stmt_rc(rc, stmt);
95 
96   FAIL_IF(i != 1, "expected value 1 for first row");
97 
98   rc= mysql_stmt_fetch(stmt);
99   FAIL_IF(rc != MYSQL_NO_DATA, "Eof expected");
100 
101   mysql_stmt_close(stmt);
102   rc= mysql_query(mysql, "DROP TABLE IF EXISTS conc67");
103   check_mysql_rc(rc, mysql);
104   return OK;
105 }
106 
test_bug1115(MYSQL * mysql)107 static int test_bug1115(MYSQL *mysql)
108 {
109   MYSQL_STMT *stmt;
110   int rc, rowcount;
111   MYSQL_BIND my_bind[1];
112   ulong length[1];
113   char szData[11];
114   char query[MAX_TEST_QUERY_LENGTH];
115 
116   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
117   check_mysql_rc(rc, mysql);
118 
119   rc= mysql_query(mysql, "CREATE TABLE test_select(\
120 session_id  char(9) NOT NULL, \
121     a       int(8) unsigned NOT NULL, \
122     b        int(5) NOT NULL, \
123     c      int(5) NOT NULL, \
124     d  datetime NOT NULL)");
125   check_mysql_rc(rc, mysql);
126   rc= mysql_query(mysql, "INSERT INTO test_select VALUES "
127                          "(\"abc\", 1, 2, 3, 2003-08-30), "
128                          "(\"abd\", 1, 2, 3, 2003-08-30), "
129                          "(\"abf\", 1, 2, 3, 2003-08-30), "
130                          "(\"abg\", 1, 2, 3, 2003-08-30), "
131                          "(\"abh\", 1, 2, 3, 2003-08-30), "
132                          "(\"abj\", 1, 2, 3, 2003-08-30), "
133                          "(\"abk\", 1, 2, 3, 2003-08-30), "
134                          "(\"abl\", 1, 2, 3, 2003-08-30), "
135                          "(\"abq\", 1, 2, 3, 2003-08-30) ");
136   check_mysql_rc(rc, mysql);
137   rc= mysql_query(mysql, "INSERT INTO test_select VALUES "
138                          "(\"abw\", 1, 2, 3, 2003-08-30), "
139                          "(\"abe\", 1, 2, 3, 2003-08-30), "
140                          "(\"abr\", 1, 2, 3, 2003-08-30), "
141                          "(\"abt\", 1, 2, 3, 2003-08-30), "
142                          "(\"aby\", 1, 2, 3, 2003-08-30), "
143                          "(\"abu\", 1, 2, 3, 2003-08-30), "
144                          "(\"abi\", 1, 2, 3, 2003-08-30), "
145                          "(\"abo\", 1, 2, 3, 2003-08-30), "
146                          "(\"abp\", 1, 2, 3, 2003-08-30), "
147                          "(\"abz\", 1, 2, 3, 2003-08-30), "
148                          "(\"abx\", 1, 2, 3, 2003-08-30)");
149   check_mysql_rc(rc, mysql);
150 
151   strcpy(query, "SELECT * FROM test_select WHERE "
152                 "CONVERT(session_id USING utf8)= ?");
153   stmt= mysql_stmt_init(mysql);
154   FAIL_IF(!stmt, mysql_error(mysql));
155   rc= mysql_stmt_prepare(stmt, SL(query));
156   check_stmt_rc(rc, stmt);
157 
158   FAIL_IF(mysql_stmt_param_count(stmt) != 1, "Paramcount != 1");
159 
160   memset(my_bind, '\0', sizeof(MYSQL_BIND));
161 
162   strcpy(szData, (char *)"abc");
163   my_bind[0].buffer_type= MYSQL_TYPE_STRING;
164   my_bind[0].buffer= (void *)szData;
165   my_bind[0].buffer_length= 10;
166   my_bind[0].length= &length[0];
167   length[0]= 3;
168 
169   rc= mysql_stmt_bind_param(stmt, my_bind);
170   check_stmt_rc(rc, stmt);
171 
172   rc= mysql_stmt_execute(stmt);
173   check_stmt_rc(rc, stmt);
174 
175   rowcount= 0;
176   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
177     rowcount++;
178   FAIL_IF(rowcount != 1, "rowcount=%d != 1");
179 
180   strcpy(szData, (char *)"venu");
181   my_bind[0].buffer_type= MYSQL_TYPE_STRING;
182   my_bind[0].buffer= (void *)szData;
183   my_bind[0].buffer_length= 10;
184   my_bind[0].length= &length[0];
185   length[0]= 4;
186   my_bind[0].is_null= 0;
187 
188   rc= mysql_stmt_bind_param(stmt, my_bind);
189   check_stmt_rc(rc, stmt);
190 
191   rc= mysql_stmt_execute(stmt);
192   check_stmt_rc(rc, stmt);
193 
194   rowcount= 0;
195   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
196     rowcount++;
197   FAIL_IF(rowcount != 0, "rowcount != 0");
198 
199   strcpy(szData, (char *)"abc");
200   my_bind[0].buffer_type= MYSQL_TYPE_STRING;
201   my_bind[0].buffer= (void *)szData;
202   my_bind[0].buffer_length= 10;
203   my_bind[0].length= &length[0];
204   length[0]= 3;
205   my_bind[0].is_null= 0;
206 
207   rc= mysql_stmt_bind_param(stmt, my_bind);
208   check_stmt_rc(rc, stmt);
209 
210   rc= mysql_stmt_execute(stmt);
211   check_stmt_rc(rc, stmt);
212 
213   rowcount= 0;
214   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
215     rowcount++;
216   FAIL_IF(rowcount != 1, "rowcount != 1");
217 
218   mysql_stmt_close(stmt);
219   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
220   check_mysql_rc(rc, mysql);
221 
222   return OK;
223 }
224 /* Test BUG#1180 (optimized away part of WHERE clause) */
225 
test_bug1180(MYSQL * mysql)226 static int test_bug1180(MYSQL *mysql)
227 {
228   MYSQL_STMT *stmt;
229   int rc, rowcount;
230   MYSQL_BIND my_bind[1];
231   ulong length[1];
232   char szData[11];
233   char query[MAX_TEST_QUERY_LENGTH];
234 
235   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
236   check_mysql_rc(rc, mysql);
237 
238   rc= mysql_query(mysql, "CREATE TABLE test_select(session_id  char(9) NOT NULL)");
239   check_mysql_rc(rc, mysql);
240   rc= mysql_query(mysql, "INSERT INTO test_select VALUES (\"abc\")");
241   check_mysql_rc(rc, mysql);
242 
243   strcpy(query, "SELECT * FROM test_select WHERE ?= \"1111\" and "
244                 "session_id= \"abc\"");
245   stmt= mysql_stmt_init(mysql);
246   FAIL_IF(!stmt, mysql_error(mysql));
247   rc= mysql_stmt_prepare(stmt, SL(query));
248   check_stmt_rc(rc, stmt);
249 
250   FAIL_IF(mysql_stmt_param_count(stmt) != 1, "Paramcount != 1");
251 
252   memset(my_bind, '\0', sizeof(MYSQL_BIND));
253 
254   strcpy(szData, (char *)"abc");
255   my_bind[0].buffer_type= MYSQL_TYPE_STRING;
256   my_bind[0].buffer= (void *)szData;
257   my_bind[0].buffer_length= 10;
258   my_bind[0].length= &length[0];
259   length[0]= 3;
260   my_bind[0].is_null= 0;
261 
262   rc= mysql_stmt_bind_param(stmt, my_bind);
263   check_stmt_rc(rc, stmt);
264 
265   rc= mysql_stmt_execute(stmt);
266   check_stmt_rc(rc, stmt);
267 
268 
269   rowcount= 0;
270   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
271     rowcount++;
272   FAIL_IF(rowcount != 0, "rowcount != 0");
273 
274   strcpy(szData, (char *)"1111");
275   my_bind[0].buffer_type= MYSQL_TYPE_STRING;
276   my_bind[0].buffer= (void *)szData;
277   my_bind[0].buffer_length= 10;
278   my_bind[0].length= &length[0];
279   length[0]= 4;
280   my_bind[0].is_null= 0;
281 
282   rc= mysql_stmt_bind_param(stmt, my_bind);
283   check_stmt_rc(rc, stmt);
284 
285   rc= mysql_stmt_execute(stmt);
286   check_stmt_rc(rc, stmt);
287 
288   rowcount= 0;
289   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
290     rowcount++;
291   FAIL_IF(rowcount != 1, "rowcount != 1");
292 
293   strcpy(szData, (char *)"abc");
294   my_bind[0].buffer_type= MYSQL_TYPE_STRING;
295   my_bind[0].buffer= (void *)szData;
296   my_bind[0].buffer_length= 10;
297   my_bind[0].length= &length[0];
298   length[0]= 3;
299   my_bind[0].is_null= 0;
300 
301   rc= mysql_stmt_bind_param(stmt, my_bind);
302   check_stmt_rc(rc, stmt);
303 
304   rc= mysql_stmt_execute(stmt);
305   check_stmt_rc(rc, stmt);
306 
307   rowcount= 0;
308   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
309     rowcount++;
310   FAIL_IF(rowcount != 0, "rowcount != 0");
311 
312   mysql_stmt_close(stmt);
313   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
314   check_mysql_rc(rc, mysql);
315 
316   return OK;
317 }
318 
319 
320 /*
321   Test BUG#1644 (Insertion of more than 3 NULL columns with parameter
322   binding fails)
323 */
324 
test_bug1644(MYSQL * mysql)325 static int test_bug1644(MYSQL *mysql)
326 {
327   MYSQL_STMT *stmt;
328   MYSQL_RES *result;
329   MYSQL_ROW row;
330   MYSQL_BIND my_bind[4];
331   int num;
332   my_bool isnull;
333   int rc, i;
334   char query[MAX_TEST_QUERY_LENGTH];
335 
336   rc= mysql_query(mysql, "DROP TABLE IF EXISTS foo_dfr");
337   check_mysql_rc(rc, mysql);
338 
339   rc= mysql_query(mysql,
340            "CREATE TABLE foo_dfr(col1 int, col2 int, col3 int, col4 int);");
341   check_mysql_rc(rc, mysql);
342 
343   strcpy(query, "INSERT INTO foo_dfr VALUES (?, ?, ?, ? )");
344   stmt= mysql_stmt_init(mysql);
345   FAIL_IF(!stmt, mysql_error(mysql));
346   rc= mysql_stmt_prepare(stmt, SL(query));
347   check_stmt_rc(rc, stmt);
348 
349   FAIL_IF(mysql_stmt_param_count(stmt) != 4, "Paramcount != 4");
350 
351   memset(my_bind, '\0', sizeof(MYSQL_BIND) * 4);
352 
353   num= 22;
354   isnull= 0;
355   for (i= 0 ; i < 4 ; i++)
356   {
357     my_bind[i].buffer_type= MYSQL_TYPE_LONG;
358     my_bind[i].buffer= (void *)&num;
359     my_bind[i].is_null= &isnull;
360   }
361 
362   rc= mysql_stmt_bind_param(stmt, my_bind);
363   check_stmt_rc(rc, stmt);
364 
365   rc= mysql_stmt_execute(stmt);
366   check_stmt_rc(rc, stmt);
367 
368   isnull= 1;
369   for (i= 0 ; i < 4 ; i++)
370     my_bind[i].is_null= &isnull;
371 
372   rc= mysql_stmt_bind_param(stmt, my_bind);
373   check_stmt_rc(rc, stmt);
374 
375   rc= mysql_stmt_execute(stmt);
376   check_stmt_rc(rc, stmt);
377 
378   isnull= 0;
379   num= 88;
380   for (i= 0 ; i < 4 ; i++)
381     my_bind[i].is_null= &isnull;
382 
383   rc= mysql_stmt_bind_param(stmt, my_bind);
384   check_stmt_rc(rc, stmt);
385 
386   rc= mysql_stmt_execute(stmt);
387   check_stmt_rc(rc, stmt);
388 
389   mysql_stmt_close(stmt);
390 
391   rc= mysql_query(mysql, "SELECT * FROM foo_dfr");
392   check_mysql_rc(rc, mysql);
393 
394   result= mysql_store_result(mysql);
395   FAIL_IF(!result, "Invalid resultset");
396 
397   FAIL_IF(mysql_num_rows(result) != 3, "rowcount != 3");
398 
399   mysql_data_seek(result, 0);
400 
401   row= mysql_fetch_row(result);
402   FAIL_IF(!row, "row = NULL");
403   for (i= 0 ; i < 4 ; i++)
404   {
405     FAIL_UNLESS(strcmp(row[i], "22") == 0, "Wrong value");
406   }
407   row= mysql_fetch_row(result);
408   FAIL_IF(!row, "Invalid row");
409   for (i= 0 ; i < 4 ; i++)
410   {
411     FAIL_UNLESS(row[i] == 0, "row[i] != 0");
412   }
413   row= mysql_fetch_row(result);
414   FAIL_IF(!row, "Invalid row");
415   for (i= 0 ; i < 4 ; i++)
416   {
417     FAIL_UNLESS(strcmp(row[i], "88") == 0, "row[i] != 88");
418   }
419   row= mysql_fetch_row(result);
420   FAIL_IF(row, "row != NULL");
421 
422   mysql_free_result(result);
423   rc= mysql_query(mysql, "DROP TABLE IF EXISTS foo_dfr");
424   check_mysql_rc(rc, mysql);
425 
426   return OK;
427 }
428 
test_bug11037(MYSQL * mysql)429 static int test_bug11037(MYSQL *mysql)
430 {
431   MYSQL_STMT *stmt;
432   int rc;
433   const char *stmt_text;
434 
435   rc= mysql_query(mysql, "drop table if exists t1");
436   check_mysql_rc(rc, mysql);
437 
438   rc= mysql_query(mysql, "create table t1 (id int not null)");
439   check_mysql_rc(rc, mysql);
440 
441   rc= mysql_query(mysql, "insert into t1 values (1)");
442   check_mysql_rc(rc, mysql);
443 
444   stmt_text= "select id FROM t1";
445   stmt= mysql_stmt_init(mysql);
446   FAIL_IF(!stmt, mysql_error(mysql));
447   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
448   check_stmt_rc(rc, stmt);
449 
450   /* expected error */
451   rc = mysql_stmt_fetch(stmt);
452   FAIL_UNLESS(rc==1, "Error expected");
453 
454   rc= mysql_stmt_execute(stmt);
455   check_stmt_rc(rc, stmt);
456 
457   rc= mysql_stmt_fetch(stmt);
458   check_stmt_rc(rc, stmt);
459 
460   rc= mysql_stmt_fetch(stmt);
461   FAIL_UNLESS(rc==MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
462 
463   rc= mysql_stmt_fetch(stmt);
464   FAIL_UNLESS(rc==MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
465 
466   mysql_stmt_close(stmt);
467   rc= mysql_query(mysql, "drop table t1");
468   check_mysql_rc(rc, mysql);
469 
470   return OK;
471 }
472 
473 /* Bug#11183 "mysql_stmt_reset() doesn't reset information about error" */
474 
test_bug11183(MYSQL * mysql)475 static int test_bug11183(MYSQL *mysql)
476 {
477   int rc;
478   MYSQL_STMT *stmt;
479   char bug_statement[]= "insert into t1 values (1)";
480 
481   rc= mysql_query(mysql, "drop table if exists t1");
482   check_mysql_rc(rc, mysql);
483   rc= mysql_query(mysql, "create table t1 (a int)");
484   check_mysql_rc(rc, mysql);
485 
486   stmt= mysql_stmt_init(mysql);
487   FAIL_IF(!stmt, mysql_error(mysql));
488 
489   rc= mysql_stmt_prepare(stmt, SL(bug_statement));
490   check_stmt_rc(rc, stmt);
491 
492   rc= mysql_query(mysql, "drop table t1");
493   check_mysql_rc(rc, mysql);
494 
495   /* Trying to execute statement that should fail on execute stage */
496   rc= mysql_stmt_execute(stmt);
497   FAIL_IF(!rc, "Error expected");
498 
499   mysql_stmt_reset(stmt);
500   FAIL_IF(mysql_stmt_errno(stmt) != 0, "stmt->error != 0");
501 
502   rc= mysql_query(mysql, "create table t1 (a int)");
503   check_mysql_rc(rc, mysql);
504 
505   /* Trying to execute statement that should pass ok */
506   if (mysql_stmt_execute(stmt))
507   {
508     mysql_stmt_reset(stmt);
509     FAIL_IF(mysql_stmt_errno(stmt) == 0, "stmt->error != 0");
510   }
511 
512   mysql_stmt_close(stmt);
513 
514   rc= mysql_query(mysql, "drop table t1");
515   check_mysql_rc(rc, mysql);
516 
517   return OK;
518 }
519 
test_bug12744(MYSQL * mysql)520 static int test_bug12744(MYSQL *mysql)
521 {
522   MYSQL_STMT *stmt = NULL;
523   int rc;
524 
525   SKIP_MAXSCALE;
526 
527   stmt = mysql_stmt_init(mysql);
528   FAIL_IF(!stmt, mysql_error(mysql));
529   rc= mysql_stmt_prepare(stmt, "SET @a:=1", 9);
530   check_stmt_rc(rc, stmt);
531 
532   rc= mysql_stmt_execute(stmt);
533   check_stmt_rc(rc, stmt);
534 
535   /* set reconnect, kill and ping to reconnect */
536   rc= mysql_query(mysql, "SET @a:=1");
537   check_mysql_rc(rc, mysql);
538   rc= mysql_options(mysql, MYSQL_OPT_RECONNECT, "1");
539   check_mysql_rc(rc, mysql);
540   rc= mysql_kill(mysql, mysql_thread_id(mysql));
541 
542   rc= mysql_ping(mysql);
543   check_mysql_rc(rc, mysql);
544 
545   rc= mysql_stmt_close(stmt);
546   check_stmt_rc(rc, stmt);
547 
548   return OK;
549 }
550 
test_bug1500(MYSQL * mysql)551 static int test_bug1500(MYSQL *mysql)
552 {
553   MYSQL_STMT *stmt;
554   MYSQL_BIND my_bind[3];
555   int        rc= 0;
556   int32 int_data[3]= {2, 3, 4};
557   const char *data;
558   const char *query;
559 
560 
561   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bg1500");
562   check_mysql_rc(rc, mysql);
563 
564   rc= mysql_query(mysql, "CREATE TABLE test_bg1500 (i INT)");
565   check_mysql_rc(rc, mysql);
566 
567   rc= mysql_query(mysql, "INSERT INTO test_bg1500 VALUES (1), (2)");
568   check_mysql_rc(rc, mysql);
569 
570   rc= mysql_commit(mysql);
571   check_mysql_rc(rc, mysql);
572 
573   query= "SELECT i FROM test_bg1500 WHERE i IN (?, ?, ?)";
574   stmt= mysql_stmt_init(mysql);
575   FAIL_IF(!stmt, mysql_error(mysql));
576   rc= mysql_stmt_prepare(stmt, SL(query));
577   check_stmt_rc(rc, stmt);
578 
579   FAIL_IF(mysql_stmt_param_count(stmt) != 3, "paramcount != 3");
580 
581   memset(my_bind, '\0', sizeof(my_bind));
582 
583   my_bind[0].buffer= (void *)int_data;
584   my_bind[0].buffer_type= MYSQL_TYPE_LONG;
585   my_bind[2]= my_bind[1]= my_bind[0];
586   my_bind[1].buffer= (void *)(int_data + 1);
587   my_bind[2].buffer= (void *)(int_data + 2);
588 
589   rc= mysql_stmt_bind_param(stmt, my_bind);
590   check_stmt_rc(rc, stmt);
591 
592   rc= mysql_stmt_execute(stmt);
593   check_stmt_rc(rc, stmt);
594 
595   rc= 0;
596   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
597     rc++;
598   FAIL_UNLESS(rc == 1, "rowcount != 1");
599 
600   mysql_stmt_close(stmt);
601 
602   rc= mysql_query(mysql, "DROP TABLE test_bg1500");
603   check_mysql_rc(rc, mysql);
604 
605   rc= mysql_query(mysql, "CREATE TABLE test_bg1500 (s VARCHAR(25), FULLTEXT(s)) engine=MyISAM");
606   check_mysql_rc(rc, mysql);
607 
608   rc= mysql_query(mysql,
609         "INSERT INTO test_bg1500 VALUES ('Gravedigger'), ('Greed'), ('Hollow Dogs')");
610   check_mysql_rc(rc, mysql);
611 
612   rc= mysql_commit(mysql);
613   check_mysql_rc(rc, mysql);
614 
615   query= "SELECT s FROM test_bg1500 WHERE MATCH (s) AGAINST (?)";
616   stmt= mysql_stmt_init(mysql);
617   FAIL_IF(!stmt, mysql_error(mysql));
618   rc= mysql_stmt_prepare(stmt, SL(query));
619   check_stmt_rc(rc, stmt);
620 
621   FAIL_IF(mysql_stmt_param_count(stmt) != 1, "paramcount != 1");
622 
623   data= "Dogs";
624   my_bind[0].buffer_type= MYSQL_TYPE_STRING;
625   my_bind[0].buffer= (void *) data;
626   my_bind[0].buffer_length= (unsigned long)strlen(data);
627   my_bind[0].is_null= 0;
628   my_bind[0].length= 0;
629 
630   rc= mysql_stmt_bind_param(stmt, my_bind);
631   check_stmt_rc(rc, stmt);
632 
633   rc= mysql_stmt_execute(stmt);
634   check_stmt_rc(rc, stmt);
635 
636   rc= 0;
637   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
638     rc++;
639   FAIL_UNLESS(rc == 1, "rowcount != 1");
640 
641   mysql_stmt_close(stmt);
642 
643   /* This should work too */
644   query= "SELECT s FROM test_bg1500 WHERE MATCH (s) AGAINST (CONCAT(?, 'digger'))";
645   stmt= mysql_stmt_init(mysql);
646   FAIL_IF(!stmt, mysql_error(mysql));
647   rc= mysql_stmt_prepare(stmt, SL(query));
648   check_stmt_rc(rc, stmt);
649 
650   FAIL_IF(mysql_stmt_param_count(stmt) != 1, "paramcount != 1");
651 
652   data= "Grave";
653   my_bind[0].buffer_type= MYSQL_TYPE_STRING;
654   my_bind[0].buffer= (void *) data;
655   my_bind[0].buffer_length= (unsigned long)strlen(data);
656 
657   rc= mysql_stmt_bind_param(stmt, my_bind);
658   check_stmt_rc(rc, stmt);
659 
660   rc= mysql_stmt_execute(stmt);
661   check_stmt_rc(rc, stmt);
662 
663   rc= 0;
664   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
665     rc++;
666   FAIL_UNLESS(rc == 1, "rowcount != 1");
667 
668   mysql_stmt_close(stmt);
669   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bg1500");
670   check_mysql_rc(rc, mysql);
671 
672   return OK;
673 }
674 
test_bug15510(MYSQL * mysql)675 static int test_bug15510(MYSQL *mysql)
676 {
677   MYSQL_STMT *stmt;
678   int rc;
679   const char *query= "select 1 from dual where 1/0";
680 
681   SKIP_MYSQL(mysql);
682 
683   rc= mysql_query(mysql, "set @@sql_mode='ERROR_FOR_DIVISION_BY_ZERO'");
684   check_mysql_rc(rc, mysql);
685 
686   stmt= mysql_stmt_init(mysql);
687 
688   rc= mysql_stmt_prepare(stmt, SL(query));
689   check_stmt_rc(rc, stmt);
690 
691   rc= mysql_stmt_execute(stmt);
692   check_stmt_rc(rc, stmt);
693 
694   rc= mysql_stmt_fetch(stmt);
695   FAIL_UNLESS(mysql_warning_count(mysql), "Warning expected");
696 
697   /* Cleanup */
698   mysql_stmt_close(stmt);
699   rc= mysql_query(mysql, "set @@sql_mode=''");
700   check_mysql_rc(rc, mysql);
701 
702   return OK;
703 }
704 
705 /*
706   Bug #15518 - Reusing a stmt that has failed during prepare
707   does not clear error
708 */
709 
test_bug15518(MYSQL * mysql)710 static int test_bug15518(MYSQL *mysql)
711 {
712   MYSQL_STMT *stmt;
713   int rc;
714 
715   stmt= mysql_stmt_init(mysql);
716 
717   /*
718     The prepare of foo should fail with errno 1064 since
719     it's not a valid query
720   */
721   rc= mysql_stmt_prepare(stmt, "foo", 3);
722   FAIL_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql), "Error expected");
723 
724   /*
725     Use the same stmt and reprepare with another query that
726     succeeds
727   */
728   rc= mysql_stmt_prepare(stmt, "SHOW STATUS", 12);
729   FAIL_UNLESS(!rc || mysql_stmt_errno(stmt) || mysql_errno(mysql), "Error not expected");
730 
731   rc= mysql_stmt_close(stmt);
732   check_mysql_rc(rc, mysql);
733   /*
734     part2, when connection to server has been closed
735     after first prepare
736   */
737   stmt= mysql_stmt_init(mysql);
738   rc= mysql_stmt_prepare(stmt, "foo", 3);
739   FAIL_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql), "Error expected");
740 
741   /* Close connection to server */
742   mysql_close(mysql);
743 
744   /*
745     Use the same stmt and reprepare with another query that
746     succeeds. The prepare should fail with error 2013 since
747     connection to server has been closed.
748   */
749   rc= mysql_stmt_prepare(stmt, "SHOW STATUS", 12);
750   FAIL_UNLESS(rc && mysql_stmt_errno(stmt), "Error expected");
751 
752   mysql_stmt_close(stmt);
753 
754   return OK;
755 }
756 
757 /*
758   Bug #15613: "libmysqlclient API function mysql_stmt_prepare returns wrong
759   field length"
760 */
761 
test_bug15613(MYSQL * mysql)762 static int test_bug15613(MYSQL *mysql)
763 {
764   MYSQL_STMT *stmt;
765   const char *stmt_text;
766   MYSQL_RES *metadata;
767   MYSQL_FIELD *field;
768   int rc;
769 
770   /* I. Prepare the table */
771   rc= mysql_query(mysql, "set names latin1");
772   check_mysql_rc(rc, mysql);
773   mysql_query(mysql, "drop table if exists t1");
774   rc= mysql_query(mysql,
775                   "create table t1 (t text character set utf8, "
776                                    "tt tinytext character set utf8, "
777                                    "mt mediumtext character set utf8, "
778                                    "lt longtext character set utf8, "
779                                    "vl varchar(255) character set latin1,"
780                                    "vb varchar(255) character set binary,"
781                                    "vu varchar(255) character set utf8)");
782   check_mysql_rc(rc, mysql);
783 
784   stmt= mysql_stmt_init(mysql);
785 
786   /* II. Check SELECT metadata */
787   stmt_text= ("select t, tt, mt, lt, vl, vb, vu from t1");
788   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
789   metadata= mysql_stmt_result_metadata(stmt);
790   field= mysql_fetch_fields(metadata);
791   FAIL_UNLESS(field[0].length == 65535, "length != 65535");
792   FAIL_UNLESS(field[1].length == 255, "length != 255");
793   FAIL_UNLESS(field[2].length == 16777215, "length != 166777215");
794   FAIL_UNLESS(field[3].length == 4294967295UL, "length != 4294967295UL");
795   FAIL_UNLESS(field[4].length == 255, "length != 255");
796   FAIL_UNLESS(field[5].length == 255, "length != 255");
797   FAIL_UNLESS(field[6].length == 255, "length != 255");
798   mysql_free_result(metadata);
799   mysql_stmt_free_result(stmt);
800 
801   /* III. Cleanup */
802   rc= mysql_query(mysql, "drop table t1");
803   check_mysql_rc(rc, mysql);
804   rc= mysql_query(mysql, "set names default");
805   check_mysql_rc(rc, mysql);
806   mysql_stmt_close(stmt);
807 
808   return OK;
809 }
810 
test_bug16144(MYSQL * mysql)811 static int test_bug16144(MYSQL *mysql)
812 {
813   const my_bool flag_orig= (my_bool) 0xde;
814   my_bool flag= flag_orig;
815   MYSQL_STMT *stmt;
816 
817   /* Check that attr_get returns correct data on little and big endian CPUs */
818   stmt= mysql_stmt_init(mysql);
819   mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (const void*) &flag);
820   mysql_stmt_attr_get(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*) &flag);
821   FAIL_UNLESS(flag == flag_orig, "flag != flag_orig");
822 
823   mysql_stmt_close(stmt);
824 
825   return OK;
826 }
827 
828 /*
829   This tests for various mysql_stmt_send_long_data bugs described in #1664
830 */
831 
test_bug1664(MYSQL * mysql)832 static int test_bug1664(MYSQL *mysql)
833 {
834     MYSQL_STMT *stmt;
835     int        rc, int_data;
836     const char *data;
837     const char *str_data= "Simple string";
838     MYSQL_BIND my_bind[2];
839     const char *query= "INSERT INTO test_long_data(col2, col1) VALUES(?, ?)";
840 
841 
842     rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data");
843     check_mysql_rc(rc, mysql);
844 
845     rc= mysql_query(mysql, "CREATE TABLE test_long_data(col1 int, col2 long varchar)");
846     check_mysql_rc(rc, mysql);
847 
848     stmt= mysql_stmt_init(mysql);
849     rc= mysql_stmt_prepare(stmt, SL(query));
850     check_stmt_rc(rc, stmt);
851 
852     FAIL_IF(mysql_stmt_param_count(stmt) != 2, "Param count != 2");
853 
854     memset(my_bind, '\0', sizeof(my_bind));
855 
856     my_bind[0].buffer_type= MYSQL_TYPE_STRING;
857     my_bind[0].buffer= (void *)str_data;
858     my_bind[0].buffer_length= (unsigned long)strlen(str_data);
859 
860     my_bind[1].buffer= (void *)&int_data;
861     my_bind[1].buffer_type= MYSQL_TYPE_LONG;
862 
863     rc= mysql_stmt_bind_param(stmt, my_bind);
864     check_stmt_rc(rc, stmt);
865 
866     int_data= 1;
867 
868     /*
869       Let us supply empty long_data. This should work and should
870       not break following execution.
871     */
872     data= "";
873     rc= mysql_stmt_send_long_data(stmt, 0, SL(data));
874     check_stmt_rc(rc, stmt);
875 
876     rc= mysql_stmt_execute(stmt);
877     check_stmt_rc(rc, stmt);
878     if (verify_col_data(mysql, "test_long_data", "col1", "1"))
879       goto error;
880     if (verify_col_data(mysql, "test_long_data", "col2", ""))
881       goto error;
882     rc= mysql_query(mysql, "DELETE FROM test_long_data");
883     check_mysql_rc(rc, mysql);
884 
885     /* This should pass OK */
886     data= (char *)"Data";
887     rc= mysql_stmt_send_long_data(stmt, 0, SL(data));
888     check_stmt_rc(rc, stmt);
889 
890     rc= mysql_stmt_execute(stmt);
891     check_stmt_rc(rc, stmt);
892 
893     if (verify_col_data(mysql, "test_long_data", "col1", "1"))
894       goto error;
895     if (verify_col_data(mysql, "test_long_data", "col2", "Data"))
896       goto error;
897 
898     /* clean up */
899     rc= mysql_query(mysql, "DELETE FROM test_long_data");
900     check_mysql_rc(rc, mysql);
901 
902     /*
903       Now we are changing int parameter and don't do anything
904       with first parameter. Second mysql_stmt_execute() should run
905       OK treating this first parameter as string parameter.
906     */
907 
908     int_data= 2;
909     /* execute */
910     rc= mysql_stmt_execute(stmt);
911     check_stmt_rc(rc, stmt);
912 
913     if (verify_col_data(mysql, "test_long_data", "col1", "2"))
914       goto error;
915     if (verify_col_data(mysql, "test_long_data", "col2", str_data))
916       goto error;
917 
918     /* clean up */
919     rc= mysql_query(mysql, "DELETE FROM test_long_data");
920     check_mysql_rc(rc, mysql);
921 
922     /*
923       Now we are sending other long data. It should not be
924       concatenated to previous.
925     */
926 
927     data= (char *)"SomeOtherData";
928     rc= mysql_stmt_send_long_data(stmt, 0, SL(data));
929     check_stmt_rc(rc, stmt);
930 
931     rc= mysql_stmt_execute(stmt);
932     check_stmt_rc(rc, stmt);
933 
934     if (verify_col_data(mysql, "test_long_data", "col1", "2"))
935       goto error;
936     if (verify_col_data(mysql, "test_long_data", "col2", "SomeOtherData"))
937       goto error;
938 
939     mysql_stmt_close(stmt);
940 
941     /* clean up */
942     rc= mysql_query(mysql, "DELETE FROM test_long_data");
943     check_mysql_rc(rc, mysql);
944 
945     /* Now let us test how mysql_stmt_reset works. */
946     stmt= mysql_stmt_init(mysql);
947     rc= mysql_stmt_prepare(stmt, SL(query));
948     check_stmt_rc(rc, stmt);
949     rc= mysql_stmt_bind_param(stmt, my_bind);
950     check_stmt_rc(rc, stmt);
951 
952     data= (char *)"SomeData";
953     rc= mysql_stmt_send_long_data(stmt, 0, SL(data));
954     check_stmt_rc(rc, stmt);
955 
956     rc= mysql_stmt_reset(stmt);
957     check_stmt_rc(rc, stmt);
958 
959     rc= mysql_stmt_execute(stmt);
960     check_stmt_rc(rc, stmt);
961 
962     if (verify_col_data(mysql, "test_long_data", "col1", "2"))
963       goto error;
964     if (verify_col_data(mysql, "test_long_data", "col2", str_data))
965       goto error;
966 
967     mysql_stmt_close(stmt);
968 
969     /* Final clean up */
970     rc= mysql_query(mysql, "DROP TABLE test_long_data");
971     check_mysql_rc(rc, mysql);
972 
973     return OK;
974 
975 error:
976     mysql_stmt_close(stmt);
977     rc= mysql_query(mysql, "DROP TABLE test_long_data");
978     return FAIL;
979 }
980 /* Test a misc bug */
981 
test_ushort_bug(MYSQL * mysql)982 static int test_ushort_bug(MYSQL *mysql)
983 {
984   MYSQL_STMT *stmt;
985   MYSQL_BIND my_bind[4];
986   ushort     short_value;
987   uint32     long_value;
988   ulong      s_length, l_length, ll_length, t_length;
989   ulonglong  longlong_value;
990   int        rc;
991   uchar      tiny_value;
992   const char *query= "SELECT * FROM test_ushort";
993 
994   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ushort");
995   check_mysql_rc(rc, mysql);
996 
997   rc= mysql_query(mysql, "CREATE TABLE test_ushort(a smallint unsigned, \
998                                                   b smallint unsigned, \
999                                                   c smallint unsigned, \
1000                                                   d smallint unsigned)");
1001   check_mysql_rc(rc, mysql);
1002 
1003   rc= mysql_query(mysql,
1004                   "INSERT INTO test_ushort VALUES(35999, 35999, 35999, 200)");
1005   check_mysql_rc(rc, mysql);
1006 
1007   stmt= mysql_stmt_init(mysql);
1008   FAIL_IF(!stmt, mysql_error(mysql));
1009   rc= mysql_stmt_prepare(stmt, SL(query));
1010   check_stmt_rc(rc, stmt);
1011 
1012   rc= mysql_stmt_execute(stmt);
1013   check_stmt_rc(rc, stmt);
1014 
1015   memset(my_bind, '\0', sizeof(my_bind));
1016   my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
1017   my_bind[0].buffer= (void *)&short_value;
1018   my_bind[0].is_unsigned= TRUE;
1019   my_bind[0].length= &s_length;
1020 
1021   my_bind[1].buffer_type= MYSQL_TYPE_LONG;
1022   my_bind[1].buffer= (void *)&long_value;
1023   my_bind[1].length= &l_length;
1024 
1025   my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
1026   my_bind[2].buffer= (void *)&longlong_value;
1027   my_bind[2].length= &ll_length;
1028 
1029   my_bind[3].buffer_type= MYSQL_TYPE_TINY;
1030   my_bind[3].buffer= (void *)&tiny_value;
1031   my_bind[3].is_unsigned= TRUE;
1032   my_bind[3].length= &t_length;
1033 
1034   rc= mysql_stmt_bind_result(stmt, my_bind);
1035   check_stmt_rc(rc, stmt);
1036 
1037   rc= mysql_stmt_fetch(stmt);
1038   check_stmt_rc(rc, stmt);
1039 
1040   FAIL_UNLESS(short_value == 35999, "short_value != 35999");
1041   FAIL_UNLESS(s_length == 2, "length != 2");
1042 
1043   FAIL_UNLESS(long_value == 35999, "long_value != 35999");
1044   FAIL_UNLESS(l_length == 4, "length != 4");
1045 
1046   FAIL_UNLESS(longlong_value == 35999, "longlong_value != 35999");
1047   FAIL_UNLESS(ll_length == 8, "length != 8");
1048 
1049   FAIL_UNLESS(tiny_value == 200, "tiny_value != 200");
1050   FAIL_UNLESS(t_length == 1, "length != 1");
1051 
1052   rc= mysql_stmt_fetch(stmt);
1053   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
1054 
1055   mysql_stmt_close(stmt);
1056   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ushort");
1057   check_mysql_rc(rc, mysql);
1058 
1059   return OK;
1060 }
1061 
test_bug1946(MYSQL * mysql)1062 static int test_bug1946(MYSQL *mysql)
1063 {
1064   MYSQL_STMT *stmt;
1065   int rc;
1066   const char *query= "INSERT INTO prepare_command VALUES (?)";
1067 
1068 
1069   rc= mysql_query(mysql, "DROP TABLE IF EXISTS prepare_command");
1070   check_mysql_rc(rc, mysql);
1071 
1072   rc= mysql_query(mysql, "CREATE TABLE prepare_command(ID INT)");
1073   check_mysql_rc(rc, mysql);
1074 
1075   stmt= mysql_stmt_init(mysql);
1076   FAIL_IF(!stmt, mysql_error(mysql));
1077   rc= mysql_stmt_prepare(stmt, SL(query));
1078   check_stmt_rc(rc, stmt);
1079 
1080   rc= mysql_real_query(mysql, SL(query));
1081   FAIL_IF(!rc, "Error expected");
1082 
1083   mysql_stmt_close(stmt);
1084   rc= mysql_query(mysql, "DROP TABLE prepare_command");
1085   check_mysql_rc(rc, mysql);
1086   return OK;
1087 }
1088 
test_bug20152(MYSQL * mysql)1089 static int test_bug20152(MYSQL *mysql)
1090 {
1091   MYSQL_BIND my_bind[1];
1092   MYSQL_STMT *stmt;
1093   MYSQL_TIME tm;
1094   int rc;
1095   const char *query= "INSERT INTO t1 (f1) VALUES (?)";
1096 
1097 
1098   memset(my_bind, '\0', sizeof(my_bind));
1099   my_bind[0].buffer_type= MYSQL_TYPE_DATE;
1100   my_bind[0].buffer= (void*)&tm;
1101 
1102   memset(&tm, 0, sizeof(MYSQL_TIME));
1103 
1104   tm.year = 2006;
1105   tm.month = 6;
1106   tm.day = 18;
1107   tm.hour = 14;
1108   tm.minute = 9;
1109   tm.second = 42;
1110 
1111   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
1112   check_mysql_rc(rc, mysql);
1113   rc= mysql_query(mysql, "CREATE TABLE t1 (f1 DATE)");
1114   check_mysql_rc(rc, mysql);
1115 
1116   stmt= mysql_stmt_init(mysql);
1117   rc= mysql_stmt_prepare(stmt, SL(query));
1118   check_stmt_rc(rc, stmt);
1119   rc= mysql_stmt_bind_param(stmt, my_bind);
1120   check_stmt_rc(rc, stmt);
1121   rc= mysql_stmt_execute(stmt);
1122   check_stmt_rc(rc, stmt);
1123   rc= mysql_stmt_close(stmt);
1124   check_stmt_rc(rc, stmt);
1125   rc= mysql_query(mysql, "DROP TABLE t1");
1126   check_mysql_rc(rc, mysql);
1127   FAIL_UNLESS(tm.hour == 14 && tm.minute == 9 && tm.second == 42, "time != 14:09:42");
1128   return OK;
1129 }
1130 
test_bug2247(MYSQL * mysql)1131 static int test_bug2247(MYSQL *mysql)
1132 {
1133   MYSQL_STMT *stmt;
1134   MYSQL_RES *res;
1135   int rc;
1136   int i;
1137   const char *create= "CREATE TABLE bug2247(id INT UNIQUE AUTO_INCREMENT)";
1138   const char *insert= "INSERT INTO bug2247 VALUES (NULL)";
1139   const char *SELECT= "SELECT id FROM bug2247";
1140   const char *update= "UPDATE bug2247 SET id=id+10";
1141   const char *drop= "DROP TABLE IF EXISTS bug2247";
1142   ulonglong exp_count;
1143   enum { NUM_ROWS= 5 };
1144 
1145 
1146   /* create table and insert few rows */
1147   rc= mysql_query(mysql, drop);
1148   check_mysql_rc(rc, mysql);
1149 
1150   rc= mysql_query(mysql, create);
1151   check_mysql_rc(rc, mysql);
1152 
1153   stmt= mysql_stmt_init(mysql);
1154   FAIL_IF(!stmt, mysql_error(mysql));
1155   rc= mysql_stmt_prepare(stmt, SL(insert));
1156   check_stmt_rc(rc, stmt);
1157   for (i= 0; i < NUM_ROWS; ++i)
1158   {
1159     rc= mysql_stmt_execute(stmt);
1160     check_stmt_rc(rc, stmt);
1161   }
1162   exp_count= mysql_stmt_affected_rows(stmt);
1163   FAIL_UNLESS(exp_count == 1, "exp_count != 1");
1164 
1165   rc= mysql_query(mysql, SELECT);
1166   check_mysql_rc(rc, mysql);
1167   /*
1168     mysql_store_result overwrites mysql->affected_rows. Check that
1169     mysql_stmt_affected_rows() returns the same value, whereas
1170     mysql_affected_rows() value is correct.
1171   */
1172   res= mysql_store_result(mysql);
1173   FAIL_IF(!res, "Invalid result set");
1174 
1175   FAIL_UNLESS(mysql_affected_rows(mysql) == NUM_ROWS, "affected_rows != NUM_ROWS");
1176   FAIL_UNLESS(exp_count == mysql_stmt_affected_rows(stmt), "affected_rows != exp_count");
1177 
1178   rc= mysql_query(mysql, update);
1179   check_mysql_rc(rc, mysql);
1180   FAIL_UNLESS(mysql_affected_rows(mysql) == NUM_ROWS, "affected_rows != NUM_ROWS");
1181   FAIL_UNLESS(exp_count == mysql_stmt_affected_rows(stmt), "affected_rows != exp_count");
1182 
1183   mysql_free_result(res);
1184   mysql_stmt_close(stmt);
1185 
1186   /* check that mysql_stmt_store_result modifies mysql_stmt_affected_rows */
1187   stmt= mysql_stmt_init(mysql);
1188   FAIL_IF(!stmt, mysql_error(mysql));
1189   rc= mysql_stmt_prepare(stmt, SL(SELECT));
1190   check_stmt_rc(rc, stmt);
1191 
1192   rc= mysql_stmt_execute(stmt);
1193   check_stmt_rc(rc, stmt);  rc= mysql_stmt_store_result(stmt);
1194   check_stmt_rc(rc, stmt);  exp_count= mysql_stmt_affected_rows(stmt);
1195   FAIL_UNLESS(exp_count == NUM_ROWS, "exp_count != NUM_ROWS");
1196 
1197   rc= mysql_query(mysql, insert);
1198   check_mysql_rc(rc, mysql);
1199   FAIL_UNLESS(mysql_affected_rows(mysql) == 1, "affected_rows != 1");
1200   FAIL_UNLESS(exp_count == mysql_stmt_affected_rows(stmt), "affected_rows != exp_count");
1201 
1202   mysql_stmt_close(stmt);
1203   rc= mysql_query(mysql, drop);
1204   check_mysql_rc(rc, mysql);
1205   return OK;
1206 }
1207 
1208 /*
1209   Test for bug#2248 "mysql_fetch without prior mysql_stmt_execute hangs"
1210 */
1211 
test_bug2248(MYSQL * mysql)1212 static int test_bug2248(MYSQL *mysql)
1213 {
1214   MYSQL_STMT *stmt;
1215   int rc;
1216   const char *query1= "SELECT DATABASE()";
1217   const char *query2= "INSERT INTO test_bug2248 VALUES (10)";
1218 
1219 
1220   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bug2248");
1221   check_mysql_rc(rc, mysql);
1222 
1223   rc= mysql_query(mysql, "CREATE TABLE test_bug2248 (id int)");
1224   check_mysql_rc(rc, mysql);
1225 
1226   stmt= mysql_stmt_init(mysql);
1227   FAIL_IF(!stmt, mysql_error(mysql));
1228   rc= mysql_stmt_prepare(stmt, SL(query1));
1229   check_stmt_rc(rc, stmt);
1230 
1231   /* This should not hang */
1232   rc= mysql_stmt_fetch(stmt);
1233   FAIL_IF(!rc, "Error expected");
1234 
1235   /* And this too */
1236   rc= mysql_stmt_store_result(stmt);
1237   FAIL_IF(!rc, "Error expected");
1238 
1239   mysql_stmt_close(stmt);
1240 
1241   stmt= mysql_stmt_init(mysql);
1242   FAIL_IF(!stmt, mysql_error(mysql));
1243   rc= mysql_stmt_prepare(stmt, SL(query2));
1244   check_stmt_rc(rc, stmt);
1245 
1246   rc= mysql_stmt_execute(stmt);
1247   check_stmt_rc(rc, stmt);
1248   /* This too should not hang but should return proper error */
1249   rc= mysql_stmt_fetch(stmt);
1250   FAIL_UNLESS(rc == 1, "rc != 1");
1251 
1252   /* This too should not hang but should not bark */
1253   rc= mysql_stmt_store_result(stmt);
1254   check_stmt_rc(rc, stmt);
1255   /* This should return proper error */
1256   rc= mysql_stmt_fetch(stmt);
1257   FAIL_UNLESS(rc == 1, "rc != 1");
1258 
1259   mysql_stmt_close(stmt);
1260 
1261   rc= mysql_query(mysql, "DROP TABLE test_bug2248");
1262   check_mysql_rc(rc, mysql);
1263   return OK;
1264 }
1265 
1266 /*
1267   BUG#23383: mysql_affected_rows() returns different values than
1268   mysql_stmt_affected_rows()
1269 
1270   Test that both mysql_affected_rows() and mysql_stmt_affected_rows()
1271   return -1 on error, 0 when no rows were affected, and (positive) row
1272   count when some rows were affected.
1273 */
test_bug23383(MYSQL * mysql)1274 static int test_bug23383(MYSQL *mysql)
1275 {
1276   const char *insert_query= "INSERT INTO t1 VALUES (1), (2)";
1277   const char *update_query= "UPDATE t1 SET i= 4 WHERE i = 3";
1278   MYSQL_STMT *stmt;
1279   unsigned long long row_count;
1280   int rc;
1281 
1282   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
1283   check_mysql_rc(rc, mysql);
1284 
1285   rc= mysql_query(mysql, "CREATE TABLE t1 (i INT UNIQUE)");
1286   check_mysql_rc(rc, mysql);
1287 
1288   rc= mysql_query(mysql, insert_query);
1289   check_mysql_rc(rc, mysql);
1290   row_count= mysql_affected_rows(mysql);
1291   FAIL_UNLESS(row_count == 2, "row_count != 2");
1292 
1293   rc= mysql_query(mysql, insert_query);
1294   FAIL_IF(!rc, "Error expected");
1295   row_count= mysql_affected_rows(mysql);
1296   FAIL_UNLESS(row_count == (unsigned long long)-1, "rowcount != -1");
1297 
1298   rc= mysql_query(mysql, update_query);
1299   check_mysql_rc(rc, mysql);
1300   row_count= mysql_affected_rows(mysql);
1301   FAIL_UNLESS(row_count == 0, "");
1302 
1303   rc= mysql_query(mysql, "DELETE FROM t1");
1304   check_mysql_rc(rc, mysql);
1305 
1306   stmt= mysql_stmt_init(mysql);
1307   FAIL_IF(!stmt, mysql_error(mysql));
1308 
1309   rc= mysql_stmt_prepare(stmt, SL(insert_query));
1310   check_stmt_rc(rc, stmt);
1311   rc= mysql_stmt_execute(stmt);
1312   check_stmt_rc(rc, stmt);
1313   row_count= mysql_stmt_affected_rows(stmt);
1314   FAIL_UNLESS(row_count == 2, "row_count != 2");
1315 
1316   rc= mysql_stmt_execute(stmt);
1317   FAIL_UNLESS(rc != 0, "");
1318   row_count= mysql_stmt_affected_rows(stmt);
1319   FAIL_UNLESS(row_count == (unsigned long long)-1, "rowcount != -1");
1320 
1321   rc= mysql_stmt_prepare(stmt, SL(update_query));
1322   check_stmt_rc(rc, stmt);
1323   rc= mysql_stmt_execute(stmt);
1324   check_stmt_rc(rc, stmt);
1325   row_count= mysql_stmt_affected_rows(stmt);
1326   FAIL_UNLESS(row_count == 0, "rowcount != 0");
1327 
1328   rc= mysql_stmt_close(stmt);
1329   check_stmt_rc(rc, stmt);
1330   rc= mysql_query(mysql, "DROP TABLE t1");
1331   check_mysql_rc(rc, mysql);
1332 
1333   return OK;
1334 }
1335 
1336 /*
1337   Bug#27592 (stack overrun when storing datetime value using prepared statements)
1338 */
1339 
test_bug27592(MYSQL * mysql)1340 static int test_bug27592(MYSQL *mysql)
1341 {
1342   const int NUM_ITERATIONS= 40;
1343   int i;
1344   int rc;
1345   MYSQL_STMT *stmt= NULL;
1346   MYSQL_BIND bind[1];
1347   MYSQL_TIME time_val;
1348 
1349   mysql_query(mysql, "DROP TABLE IF EXISTS t1");
1350   mysql_query(mysql, "CREATE TABLE t1(c2 DATETIME)");
1351 
1352   stmt= mysql_stmt_init(mysql);
1353   FAIL_IF(!stmt, mysql_error(mysql));
1354   rc= mysql_stmt_prepare(stmt, SL("INSERT INTO t1 VALUES (?)"));
1355   check_stmt_rc(rc, stmt);
1356 
1357   memset(bind, '\0', sizeof(bind));
1358 
1359   bind[0].buffer_type= MYSQL_TYPE_DATETIME;
1360   bind[0].buffer= (char *) &time_val;
1361   bind[0].length= NULL;
1362 
1363   for (i= 0; i < NUM_ITERATIONS; i++)
1364   {
1365     time_val.year= 2007;
1366     time_val.month= 6;
1367     time_val.day= 7;
1368     time_val.hour= 18;
1369     time_val.minute= 41;
1370     time_val.second= 3;
1371 
1372     time_val.second_part=0;
1373     time_val.neg=0;
1374 
1375     rc= mysql_stmt_bind_param(stmt, bind);
1376     check_stmt_rc(rc, stmt);
1377     rc= mysql_stmt_execute(stmt);
1378     check_stmt_rc(rc, stmt);
1379   }
1380 
1381   mysql_stmt_close(stmt);
1382   mysql_query(mysql, "DROP TABLE IF EXISTS t1");
1383 
1384   return OK;
1385 }
1386 
1387 /*
1388   Bug#28934: server crash when receiving malformed com_execute packets
1389 */
1390 
test_bug28934(MYSQL * mysql)1391 static int test_bug28934(MYSQL *mysql)
1392 {
1393   my_bool error= 0;
1394   MYSQL_BIND bind[5];
1395   MYSQL_STMT *stmt;
1396   int rc, cnt;
1397 
1398   rc= mysql_query(mysql, "drop table if exists t1");
1399   check_mysql_rc(rc, mysql);
1400   rc= mysql_query(mysql, "create table t1(id int)");
1401   check_mysql_rc(rc, mysql);
1402 
1403   rc= mysql_query(mysql, "insert into t1 values(1),(2),(3),(4),(5)");
1404   check_mysql_rc(rc, mysql);
1405 
1406   stmt= mysql_stmt_init(mysql);
1407   FAIL_IF(!stmt, mysql_error(mysql));
1408   rc= mysql_stmt_prepare(stmt, SL("select * from t1 where id in(?,?,?,?,?)"));
1409   check_stmt_rc(rc, stmt);
1410 
1411   memset (&bind, '\0', sizeof (bind));
1412   for (cnt= 0; cnt < 5; cnt++)
1413   {
1414     bind[cnt].buffer_type= MYSQL_TYPE_LONG;
1415     bind[cnt].buffer= (char*)&cnt;
1416     bind[cnt].buffer_length= 0;
1417   }
1418   rc= mysql_stmt_bind_param(stmt, bind);
1419   check_stmt_rc(rc, stmt);
1420 
1421   stmt->param_count=2;
1422   error= mysql_stmt_execute(stmt);
1423   FAIL_UNLESS(error != 0, "Error expected");
1424   mysql_stmt_close(stmt);
1425 
1426   rc= mysql_query(mysql, "drop table t1");
1427   check_mysql_rc(rc, mysql);
1428   return OK;
1429 }
1430 
test_bug3035(MYSQL * mysql)1431 static int test_bug3035(MYSQL *mysql)
1432 {
1433   MYSQL_STMT *stmt;
1434   int rc;
1435   MYSQL_BIND bind_array[12], *my_bind= bind_array, *bind_end= my_bind + 12;
1436   int8 int8_val;
1437   uint8 uint8_val;
1438   int16 int16_val;
1439   uint16 uint16_val;
1440   int32 int32_val;
1441   uint32 uint32_val;
1442   longlong int64_val;
1443   ulonglong uint64_val;
1444   double double_val, udouble_val, double_tmp;
1445   char longlong_as_string[22], ulonglong_as_string[22];
1446 
1447   /* mins and maxes */
1448   const int8 int8_min= -128;
1449   const int8 int8_max= 127;
1450   const uint8 uint8_min= 0;
1451   const uint8 uint8_max= 255;
1452 
1453   const int16 int16_min= -32768;
1454   const int16 int16_max= 32767;
1455   const uint16 uint16_min= 0;
1456   const uint16 uint16_max= 65535;
1457 
1458   const int32 int32_max= 2147483647L;
1459   const int32 int32_min= -int32_max - 1;
1460   const uint32 uint32_min= 0;
1461   const uint32 uint32_max= 4294967295U;
1462 
1463   /* it might not work okay everyplace */
1464   const longlong int64_max= 9223372036854775807LL;
1465   const longlong int64_min= -int64_max - 1;
1466 
1467   const ulonglong uint64_min= 0U;
1468   const ulonglong uint64_max= 18446744073709551615ULL;
1469 
1470   const char *stmt_text;
1471 
1472 
1473   stmt_text= "DROP TABLE IF EXISTS t1";
1474   rc= mysql_real_query(mysql, SL(stmt_text));
1475   check_mysql_rc(rc, mysql);
1476 
1477   stmt_text= "CREATE TABLE t1 (i8 TINYINT, ui8 TINYINT UNSIGNED, "
1478                               "i16 SMALLINT, ui16 SMALLINT UNSIGNED, "
1479                               "i32 INT, ui32 INT UNSIGNED, "
1480                               "i64 BIGINT, ui64 BIGINT UNSIGNED, "
1481                               "id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT)";
1482   rc= mysql_real_query(mysql, SL(stmt_text));
1483   check_mysql_rc(rc, mysql);
1484 
1485   memset(bind_array, '\0', sizeof(bind_array));
1486   for (my_bind= bind_array; my_bind < bind_end; my_bind++)
1487     my_bind->error= &my_bind->error_value;
1488 
1489   bind_array[0].buffer_type= MYSQL_TYPE_TINY;
1490   bind_array[0].buffer= (void *) &int8_val;
1491 
1492   bind_array[1].buffer_type= MYSQL_TYPE_TINY;
1493   bind_array[1].buffer= (void *) &uint8_val;
1494   bind_array[1].is_unsigned= 1;
1495 
1496   bind_array[2].buffer_type= MYSQL_TYPE_SHORT;
1497   bind_array[2].buffer= (void *) &int16_val;
1498 
1499   bind_array[3].buffer_type= MYSQL_TYPE_SHORT;
1500   bind_array[3].buffer= (void *) &uint16_val;
1501   bind_array[3].is_unsigned= 1;
1502 
1503   bind_array[4].buffer_type= MYSQL_TYPE_LONG;
1504   bind_array[4].buffer= (void *) &int32_val;
1505 
1506   bind_array[5].buffer_type= MYSQL_TYPE_LONG;
1507   bind_array[5].buffer= (void *) &uint32_val;
1508   bind_array[5].is_unsigned= 1;
1509 
1510   bind_array[6].buffer_type= MYSQL_TYPE_LONGLONG;
1511   bind_array[6].buffer= (void *) &int64_val;
1512 
1513   bind_array[7].buffer_type= MYSQL_TYPE_LONGLONG;
1514   bind_array[7].buffer= (void *) &uint64_val;
1515   bind_array[7].is_unsigned= 1;
1516 
1517   stmt= mysql_stmt_init(mysql);
1518   check_stmt_rc(rc, stmt);
1519 
1520   stmt_text= "INSERT INTO t1 (i8, ui8, i16, ui16, i32, ui32, i64, ui64) "
1521                      "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
1522   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
1523   check_stmt_rc(rc, stmt);
1524   mysql_stmt_bind_param(stmt, bind_array);
1525 
1526   int8_val= int8_min;
1527   uint8_val= uint8_min;
1528   int16_val= int16_min;
1529   uint16_val= uint16_min;
1530   int32_val= int32_min;
1531   uint32_val= uint32_min;
1532   int64_val= int64_min;
1533   uint64_val= uint64_min;
1534 
1535   rc= mysql_stmt_execute(stmt);
1536   check_stmt_rc(rc, stmt);
1537   int8_val= int8_max;
1538   uint8_val= uint8_max;
1539   int16_val= int16_max;
1540   uint16_val= uint16_max;
1541   int32_val= int32_max;
1542   uint32_val= uint32_max;
1543   int64_val= int64_max;
1544   uint64_val= uint64_max;
1545 
1546   rc= mysql_stmt_execute(stmt);
1547   check_stmt_rc(rc, stmt);
1548   stmt_text= "SELECT i8, ui8, i16, ui16, i32, ui32, i64, ui64, ui64, "
1549              "cast(ui64 as signed), ui64, cast(ui64 as signed)"
1550              "FROM t1 ORDER BY id ASC";
1551 
1552   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
1553   check_stmt_rc(rc, stmt);
1554   rc= mysql_stmt_execute(stmt);
1555   check_stmt_rc(rc, stmt);
1556   bind_array[8].buffer_type= MYSQL_TYPE_DOUBLE;
1557   bind_array[8].buffer= (void *) &udouble_val;
1558 
1559   bind_array[9].buffer_type= MYSQL_TYPE_DOUBLE;
1560   bind_array[9].buffer= (void *) &double_val;
1561 
1562   bind_array[10].buffer_type= MYSQL_TYPE_STRING;
1563   bind_array[10].buffer= (void *) &ulonglong_as_string;
1564   bind_array[10].buffer_length= sizeof(ulonglong_as_string);
1565 
1566   bind_array[11].buffer_type= MYSQL_TYPE_STRING;
1567   bind_array[11].buffer= (void *) &longlong_as_string;
1568   bind_array[11].buffer_length= sizeof(longlong_as_string);
1569 
1570   mysql_stmt_bind_result(stmt, bind_array);
1571 
1572   rc= mysql_stmt_fetch(stmt);
1573   check_stmt_rc(rc, stmt);
1574   FAIL_UNLESS(int8_val == int8_min, "int8_val != int8_min");
1575   FAIL_UNLESS(uint8_val == uint8_min, "uint8_val != uint8_min");
1576   FAIL_UNLESS(int16_val == int16_min, "int16_val != int16_min");
1577   FAIL_UNLESS(uint16_val == uint16_min, "uint16_val != uint16_min");
1578   FAIL_UNLESS(int32_val == int32_min, "int32_val != int32_min");
1579   FAIL_UNLESS(uint32_val == uint32_min, "uint32_val != uint32_min");
1580   FAIL_UNLESS(int64_val == int64_min, "int64_val != int64_min");
1581   FAIL_UNLESS(uint64_val == uint64_min, "uint64_val != uint64_min");
1582   FAIL_UNLESS(double_val == (longlong) uint64_min, "double_val != uint64_min");
1583   double_tmp= ulonglong2double(uint64_val);
1584   FAIL_UNLESS(cmp_double(&udouble_val,&double_tmp), "udouble_val != double_tmp");
1585   FAIL_UNLESS(!strcmp(longlong_as_string, "0"), "longlong_as_string != '0'");
1586   FAIL_UNLESS(!strcmp(ulonglong_as_string, "0"), "ulonglong_as_string != '0'");
1587 
1588   rc= mysql_stmt_fetch(stmt);
1589 
1590   FAIL_UNLESS(rc == MYSQL_DATA_TRUNCATED || rc == 0, "rc != 0,MYSQL_DATA_TRUNCATED");
1591 
1592   FAIL_UNLESS(int8_val == int8_max, "int8_val != int8_max");
1593   FAIL_UNLESS(uint8_val == uint8_max, "uint8_val != uint8_max");
1594   FAIL_UNLESS(int16_val == int16_max, "int16_val != int16_max");
1595   FAIL_UNLESS(uint16_val == uint16_max, "uint16_val != uint16_max");
1596   FAIL_UNLESS(int32_val == int32_max, "int32_val != int32_max");
1597   FAIL_UNLESS(uint32_val == uint32_max, "uint32_val != uint32_max");
1598   FAIL_UNLESS(int64_val == int64_max, "int64_val != int64_max");
1599   FAIL_UNLESS(uint64_val == uint64_max, "uint64_val != uint64_max");
1600   FAIL_UNLESS(double_val == (longlong) uint64_val, "double_val != uint64_val");
1601   double_tmp= ulonglong2double(uint64_val);
1602   FAIL_UNLESS(cmp_double(&udouble_val,&double_tmp), "udouble_val != double_tmp");
1603   FAIL_UNLESS(!strcmp(longlong_as_string, "-1"), "longlong_as_string != '-1'");
1604   FAIL_UNLESS(!strcmp(ulonglong_as_string, "18446744073709551615"), "ulonglong_as_string != '18446744073709551615'");
1605 
1606   rc= mysql_stmt_fetch(stmt);
1607   FAIL_UNLESS(rc == MYSQL_NO_DATA, "");
1608 
1609   mysql_stmt_close(stmt);
1610 
1611   stmt_text= "DROP TABLE t1";
1612   mysql_real_query(mysql, SL(stmt_text));
1613   return OK;
1614 }
1615 
1616 /*
1617   Test for BUG#3420 ("select id1, value1 from t where id= ? or value= ?"
1618   returns all rows in the table)
1619 */
1620 
test_ps_conj_select(MYSQL * mysql)1621 static int test_ps_conj_select(MYSQL *mysql)
1622 {
1623   MYSQL_STMT *stmt;
1624   int        rc;
1625   MYSQL_BIND my_bind[2];
1626   int32      int_data;
1627   char       str_data[32];
1628   unsigned long str_length;
1629   char query[MAX_TEST_QUERY_LENGTH];
1630 
1631   rc= mysql_query(mysql, "drop table if exists t1");
1632   check_mysql_rc(rc, mysql);
1633 
1634   rc= mysql_query(mysql, "create table t1 (id1 int(11) NOT NULL default '0', "
1635                          "value2 varchar(100), value1 varchar(100))");
1636   check_mysql_rc(rc, mysql);
1637 
1638   rc= mysql_query(mysql, "insert into t1 values (1, 'hh', 'hh'), "
1639                           "(2, 'hh', 'hh'), (1, 'ii', 'ii'), (2, 'ii', 'ii')");
1640   check_mysql_rc(rc, mysql);
1641 
1642   strcpy(query, "select id1, value1 from t1 where id1= ? or "
1643                 "CONVERT(value1 USING utf8)= ?");
1644   stmt= mysql_stmt_init(mysql);
1645   FAIL_IF(!stmt, mysql_error(mysql));
1646   rc= mysql_stmt_prepare(stmt, SL(query));
1647   check_stmt_rc(rc, stmt);
1648 
1649   FAIL_IF(mysql_stmt_param_count(stmt) != 2, "param_count != 2");
1650 
1651   /* Always bzero all members of bind parameter */
1652   memset(my_bind, '\0', sizeof(my_bind));
1653   my_bind[0].buffer_type= MYSQL_TYPE_LONG;
1654   my_bind[0].buffer= (void *)&int_data;
1655 
1656   my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
1657   my_bind[1].buffer= (void *)str_data;
1658   my_bind[1].buffer_length= array_elements(str_data);
1659   my_bind[1].length= &str_length;
1660 
1661   rc= mysql_stmt_bind_param(stmt, my_bind);
1662   check_stmt_rc(rc, stmt);
1663   int_data= 1;
1664   strcpy(str_data, "hh");
1665   str_length= (unsigned long)strlen(str_data);
1666 
1667   rc= mysql_stmt_execute(stmt);
1668   check_stmt_rc(rc, stmt);
1669 
1670   rc=0;
1671   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
1672    rc++;
1673   FAIL_UNLESS(rc == 3, "rc != 3");
1674 
1675   mysql_stmt_close(stmt);
1676   rc= mysql_query(mysql, "drop table if exists t1");
1677   check_mysql_rc(rc, mysql);
1678   return OK;
1679 }
1680 
1681 /* Test for NULL as PS parameter (BUG#3367, BUG#3371) */
1682 
test_ps_null_param(MYSQL * mysql)1683 static int test_ps_null_param(MYSQL *mysql)
1684 {
1685   MYSQL_STMT *stmt;
1686   int        rc;
1687 
1688   MYSQL_BIND in_bind;
1689   my_bool    in_is_null;
1690   long int   in_long;
1691 
1692   MYSQL_BIND out_bind;
1693   ulong      out_length;
1694   my_bool    out_is_null;
1695   char       out_str_data[20];
1696 
1697   const char *queries[]= {"select ?", "select ?+1",
1698                     "select col1 from test_ps_nulls where col1 <=> ?",
1699                     NULL
1700                     };
1701   const char **cur_query= queries;
1702 
1703 
1704   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ps_nulls");
1705   check_mysql_rc(rc, mysql);
1706 
1707   rc= mysql_query(mysql, "CREATE TABLE test_ps_nulls(col1 int)");
1708   check_mysql_rc(rc, mysql);
1709 
1710   rc= mysql_query(mysql, "INSERT INTO test_ps_nulls values (1), (null)");
1711   check_mysql_rc(rc, mysql);
1712 
1713   /* Always bzero all members of bind parameter */
1714   memset(&in_bind, '\0', sizeof(in_bind));
1715   memset(&out_bind, '\0', sizeof(out_bind));
1716   in_bind.buffer_type= MYSQL_TYPE_LONG;
1717   in_bind.is_null= &in_is_null;
1718   in_bind.length= 0;
1719   in_bind.buffer= (void *)&in_long;
1720   in_is_null= 1;
1721   in_long= 1;
1722 
1723   out_bind.buffer_type= MYSQL_TYPE_STRING;
1724   out_bind.is_null= &out_is_null;
1725   out_bind.length= &out_length;
1726   out_bind.buffer= out_str_data;
1727   out_bind.buffer_length= array_elements(out_str_data);
1728 
1729   /* Execute several queries, all returning NULL in result. */
1730   for(cur_query= queries; *cur_query; cur_query++)
1731   {
1732     char query[MAX_TEST_QUERY_LENGTH];
1733     strcpy(query, *cur_query);
1734     stmt= mysql_stmt_init(mysql);
1735     FAIL_IF(!stmt, mysql_error(mysql));
1736     rc= mysql_stmt_prepare(stmt, SL(query));
1737     diag("statement: %s", query);
1738     check_stmt_rc(rc, stmt);
1739     FAIL_IF(mysql_stmt_param_count(stmt) != 1, "param_count != 1");
1740 
1741     rc= mysql_stmt_bind_param(stmt, &in_bind);
1742     check_stmt_rc(rc, stmt);
1743     rc= mysql_stmt_bind_result(stmt, &out_bind);
1744     check_stmt_rc(rc, stmt);
1745     rc= mysql_stmt_execute(stmt);
1746     check_stmt_rc(rc, stmt);
1747     rc= mysql_stmt_fetch(stmt);
1748     FAIL_UNLESS(rc != MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
1749     FAIL_UNLESS(out_is_null, "!out_is_null");
1750     rc= mysql_stmt_fetch(stmt);
1751     FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
1752     mysql_stmt_close(stmt);
1753   }
1754   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ps_nulls");
1755   check_mysql_rc(rc, mysql);
1756   return OK;
1757 }
1758 
1759 /*
1760   utility for the next test; expects 3 rows in the result from a SELECT,
1761   compares each row/field with an expected value.
1762  */
1763 #define test_ps_query_cache_result(i1,s1,l1,i2,s2,l2,i3,s3,l3)    \
1764   r_metadata= mysql_stmt_result_metadata(stmt);                   \
1765   FAIL_UNLESS(r_metadata != NULL, "");                            \
1766   rc= mysql_stmt_fetch(stmt);                                     \
1767   check_stmt_rc(rc,stmt);                                         \
1768   FAIL_UNLESS((r_int_data == i1) && (r_str_length == l1) &&       \
1769              (strcmp(r_str_data, s1) == 0), "test_ps_query_cache_result failure"); \
1770   rc= mysql_stmt_fetch(stmt);                                     \
1771   check_stmt_rc(rc,stmt);                                         \
1772   FAIL_UNLESS((r_int_data == i2) && (r_str_length == l2) &&       \
1773              (strcmp(r_str_data, s2) == 0), "test_ps_query_cache_result failure"); \
1774   rc= mysql_stmt_fetch(stmt);                                     \
1775   check_stmt_rc(rc,stmt);                                         \
1776   FAIL_UNLESS((r_int_data == i3) && (r_str_length == l3) &&       \
1777              (strcmp(r_str_data, s3) == 0), "test_ps_query_cache_result failure"); \
1778   rc= mysql_stmt_fetch(stmt);                                     \
1779   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");        \
1780   mysql_free_result(r_metadata);
1781 
1782 /* reads Qcache_hits from server and returns its value */
query_cache_hits(MYSQL * mysql)1783 static int query_cache_hits(MYSQL *mysql)
1784 {
1785   MYSQL_RES *res;
1786   MYSQL_ROW row;
1787   int rc;
1788   uint result;
1789 
1790   rc= mysql_query(mysql, "show status like 'qcache_hits'");
1791   check_mysql_rc(rc, mysql);
1792   res= mysql_use_result(mysql);
1793 
1794   row= mysql_fetch_row(res);
1795 
1796   result= atoi(row[1]);
1797   mysql_free_result(res);
1798   return result;
1799 }
1800 
1801 
1802 /*
1803   Test that prepared statements make use of the query cache just as normal
1804   statements (BUG#735).
1805 */
test_ps_query_cache(MYSQL * mysql)1806 static int test_ps_query_cache(MYSQL *mysql)
1807 {
1808   MYSQL      *lmysql= mysql;
1809   MYSQL_STMT *stmt;
1810   int        rc;
1811   MYSQL_BIND p_bind[2],r_bind[2]; /* p: param bind; r: result bind */
1812   int32      p_int_data, r_int_data;
1813   char       p_str_data[32], r_str_data[32];
1814   unsigned long p_str_length, r_str_length;
1815   MYSQL_RES  *r_metadata;
1816   char       query[MAX_TEST_QUERY_LENGTH];
1817   uint       hits1, hits2;
1818   enum enum_test_ps_query_cache
1819   {
1820     /*
1821       We iterate the same prepare/executes block, but have iterations where
1822       we vary the query cache conditions.
1823     */
1824     /* the query cache is enabled for the duration of prep&execs: */
1825     TEST_QCACHE_ON= 0,
1826     /*
1827       same but using a new connection (to see if qcache serves results from
1828       the previous connection as it should):
1829     */
1830     TEST_QCACHE_ON_WITH_OTHER_CONN,
1831     /*
1832       First border case: disables the query cache before prepare and
1833       re-enables it before execution (to test if we have no bug then):
1834     */
1835     TEST_QCACHE_OFF_ON,
1836     /*
1837       Second border case: enables the query cache before prepare and
1838       disables it before execution:
1839     */
1840     TEST_QCACHE_ON_OFF
1841   };
1842   enum enum_test_ps_query_cache iteration;
1843 
1844   diag("test needs to be fixed");
1845   return SKIP;
1846   /* prepare the table */
1847 
1848   rc= mysql_query(mysql, "drop table if exists t1");
1849   check_mysql_rc(rc, mysql);
1850 
1851   rc= mysql_query(mysql, "create table t1 (id1 int(11) NOT NULL default '0', "
1852                          "value2 varchar(100), value1 varchar(100))");
1853   check_mysql_rc(rc, mysql);
1854 
1855   rc= mysql_query(mysql, "insert into t1 values (1, 'hh', 'hh'), "
1856                           "(2, 'hh', 'hh'), (1, 'ii', 'ii'), (2, 'ii', 'ii')");
1857   check_mysql_rc(rc, mysql);
1858 
1859   for (iteration= TEST_QCACHE_ON; iteration <= TEST_QCACHE_ON_OFF; iteration++)
1860   {
1861     switch (iteration) {
1862     case TEST_QCACHE_ON:
1863     case TEST_QCACHE_ON_OFF:
1864       rc= mysql_query(lmysql, "set global query_cache_size=1000000");
1865       check_mysql_rc(rc, mysql);
1866       break;
1867     case TEST_QCACHE_OFF_ON:
1868       rc= mysql_query(lmysql, "set global query_cache_size=0");
1869       check_mysql_rc(rc, mysql);
1870       break;
1871     case TEST_QCACHE_ON_WITH_OTHER_CONN:
1872       lmysql= test_connect(NULL);
1873       FAIL_IF(!lmysql, "Opening new connection failed");
1874       break;
1875     }
1876 
1877     strcpy(query, "select id1, value1 from t1 where id1= ? or "
1878            "CONVERT(value1 USING utf8)= ?");
1879     stmt= mysql_stmt_init(lmysql);
1880     FAIL_IF(!stmt, mysql_error(lmysql));
1881     rc= mysql_stmt_prepare(stmt, SL(query));
1882     check_stmt_rc(rc, stmt);
1883 
1884     FAIL_IF(mysql_stmt_param_count(stmt) != 2, "param_count != 2");
1885 
1886     switch (iteration) {
1887     case TEST_QCACHE_OFF_ON:
1888       rc= mysql_query(lmysql, "set global query_cache_size=1000000");
1889       check_mysql_rc(rc, mysql);
1890       break;
1891     case TEST_QCACHE_ON_OFF:
1892       rc= mysql_query(lmysql, "set global query_cache_size=0");
1893       check_mysql_rc(rc, mysql);
1894     default:
1895       break;
1896     }
1897 
1898     memset(p_bind, '\0', sizeof(p_bind));
1899     p_bind[0].buffer_type= MYSQL_TYPE_LONG;
1900     p_bind[0].buffer= (void *)&p_int_data;
1901     p_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
1902     p_bind[1].buffer= (void *)p_str_data;
1903     p_bind[1].buffer_length= array_elements(p_str_data);
1904     p_bind[1].length= &p_str_length;
1905 
1906     rc= mysql_stmt_bind_param(stmt, p_bind);
1907     check_stmt_rc(rc, stmt);
1908     p_int_data= 1;
1909     strcpy(p_str_data, "hh");
1910     p_str_length= (unsigned long)strlen(p_str_data);
1911 
1912     memset(r_bind, '\0', sizeof(r_bind));
1913     r_bind[0].buffer_type= MYSQL_TYPE_LONG;
1914     r_bind[0].buffer= (void *)&r_int_data;
1915     r_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
1916     r_bind[1].buffer= (void *)r_str_data;
1917     r_bind[1].buffer_length= array_elements(r_str_data);
1918     r_bind[1].length= &r_str_length;
1919 
1920     rc= mysql_stmt_bind_result(stmt, r_bind);
1921     check_stmt_rc(rc, stmt);
1922     rc= mysql_stmt_execute(stmt);
1923     check_stmt_rc(rc, stmt);
1924     test_ps_query_cache_result(1, "hh", 2, 2, "hh", 2, 1, "ii", 2);
1925   r_metadata= mysql_stmt_result_metadata(stmt);
1926   FAIL_UNLESS(r_metadata != NULL, "");
1927   rc= mysql_stmt_fetch(stmt);
1928   check_stmt_rc(rc,stmt);
1929   FAIL_UNLESS((r_int_data == 1) && (r_str_length == 2) &&
1930              (strcmp(r_str_data, "hh") == 0), "test_ps_query_cache_result failure"); \
1931   rc= mysql_stmt_fetch(stmt);
1932   check_stmt_rc(rc,stmt);
1933   FAIL_UNLESS((r_int_data == 2) && (r_str_length == 2) &&
1934              (strcmp(r_str_data, "hh") == 0), "test_ps_query_cache_result failure"); \
1935   rc= mysql_stmt_fetch(stmt);
1936   check_stmt_rc(rc,stmt);
1937   FAIL_UNLESS((r_int_data == 1) && (r_str_length == 2) &&
1938              (strcmp(r_str_data, "ii") == 0), "test_ps_query_cache_result failure"); \
1939   rc= mysql_stmt_fetch(stmt);
1940   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
1941   mysql_free_result(r_metadata);
1942 
1943 
1944     /* now retry with the same parameter values and see qcache hits */
1945     hits1= query_cache_hits(lmysql);
1946     rc= mysql_stmt_execute(stmt);
1947     check_stmt_rc(rc, stmt);    test_ps_query_cache_result(1, "hh", 2, 2, "hh", 2, 1, "ii", 2);
1948     hits2= query_cache_hits(lmysql);
1949     switch(iteration) {
1950     case TEST_QCACHE_ON_WITH_OTHER_CONN:
1951     case TEST_QCACHE_ON:                 /* should have hit */
1952       FAIL_UNLESS(hits2-hits1 == 1, "hits2 != hits1 + 1");
1953       break;
1954     case TEST_QCACHE_OFF_ON:
1955     case TEST_QCACHE_ON_OFF:             /* should not have hit */
1956       FAIL_UNLESS(hits2-hits1 == 0, "hits2 != hits1");
1957       break;
1958     }
1959 
1960     /* now modify parameter values and see qcache hits */
1961     strcpy(p_str_data, "ii");
1962     p_str_length= (unsigned long)strlen(p_str_data);
1963     rc= mysql_stmt_execute(stmt);
1964     check_stmt_rc(rc, stmt);
1965     test_ps_query_cache_result(1, "hh", 2, 1, "ii", 2, 2, "ii", 2);
1966     hits1= query_cache_hits(lmysql);
1967 
1968     switch(iteration) {
1969     case TEST_QCACHE_ON:
1970     case TEST_QCACHE_OFF_ON:
1971     case TEST_QCACHE_ON_OFF:             /* should not have hit */
1972       FAIL_UNLESS(hits2-hits1 == 0, "hits2 != hits1");
1973       break;
1974     case TEST_QCACHE_ON_WITH_OTHER_CONN: /* should have hit */
1975       FAIL_UNLESS(hits1-hits2 == 1, "hits2 != hits1+1");
1976       break;
1977     }
1978 
1979     rc= mysql_stmt_execute(stmt);
1980     check_stmt_rc(rc, stmt);
1981     test_ps_query_cache_result(1, "hh", 2, 1, "ii", 2, 2, "ii", 2);
1982     hits2= query_cache_hits(lmysql);
1983 
1984     mysql_stmt_close(stmt);
1985 
1986     switch(iteration) {
1987     case TEST_QCACHE_ON:                 /* should have hit */
1988       FAIL_UNLESS(hits2-hits1 == 1, "hits2 != hits1+1");
1989       break;
1990     case TEST_QCACHE_OFF_ON:
1991     case TEST_QCACHE_ON_OFF:             /* should not have hit */
1992       FAIL_UNLESS(hits2-hits1 == 0, "hits2 != hits1");
1993       break;
1994     case TEST_QCACHE_ON_WITH_OTHER_CONN: /* should have hit */
1995       FAIL_UNLESS(hits2-hits1 == 1, "hits2 != hits1+1");
1996       break;
1997     }
1998 
1999   } /* for(iteration=...) */
2000 
2001   if (lmysql != mysql)
2002     mysql_close(lmysql);
2003 
2004   rc= mysql_query(mysql, "set global query_cache_size=0");
2005   check_mysql_rc(rc, mysql);
2006   return OK;
2007 }
2008 
test_bug3117(MYSQL * mysql)2009 static int test_bug3117(MYSQL *mysql)
2010 {
2011   MYSQL_STMT *stmt;
2012   MYSQL_BIND buffer;
2013   longlong lii;
2014   ulong length;
2015   my_bool is_null;
2016   int rc;
2017 
2018   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
2019   check_mysql_rc(rc, mysql);
2020 
2021   rc= mysql_query(mysql, "CREATE TABLE t1 (id int auto_increment primary key)");
2022   check_mysql_rc(rc, mysql);
2023 
2024   stmt= mysql_stmt_init(mysql);
2025   FAIL_IF(!stmt, mysql_error(mysql));
2026   rc= mysql_stmt_prepare(stmt, SL("SELECT LAST_INSERT_ID()"));
2027   check_stmt_rc(rc, stmt);
2028 
2029   rc= mysql_query(mysql, "INSERT INTO t1 VALUES (NULL)");
2030   check_mysql_rc(rc, mysql);
2031 
2032   rc= mysql_stmt_execute(stmt);
2033   check_stmt_rc(rc, stmt);
2034   memset(&buffer, '\0', sizeof(buffer));
2035   buffer.buffer_type= MYSQL_TYPE_LONGLONG;
2036   buffer.buffer_length= sizeof(lii);
2037   buffer.buffer= (void *)&lii;
2038   buffer.length= &length;
2039   buffer.is_null= &is_null;
2040 
2041   rc= mysql_stmt_bind_result(stmt, &buffer);
2042   check_stmt_rc(rc, stmt);
2043   rc= mysql_stmt_store_result(stmt);
2044   check_stmt_rc(rc, stmt);
2045   rc= mysql_stmt_fetch(stmt);
2046   check_stmt_rc(rc, stmt);
2047   FAIL_UNLESS(is_null == 0 && lii == 1, "is_null != 0 || lii != 1");
2048 
2049   rc= mysql_query(mysql, "INSERT INTO t1 VALUES (NULL)");
2050   check_mysql_rc(rc, mysql);
2051 
2052   rc= mysql_stmt_execute(stmt);
2053   check_stmt_rc(rc, stmt);
2054   rc= mysql_stmt_fetch(stmt);
2055   check_stmt_rc(rc, stmt);
2056   FAIL_UNLESS(is_null == 0 && lii == 2, "is_null != 0 || lii != 2");
2057 
2058   mysql_stmt_close(stmt);
2059 
2060   rc= mysql_query(mysql, "DROP TABLE t1");
2061   check_mysql_rc(rc, mysql);
2062   return OK;
2063 }
2064 
2065 /**
2066   Bug#36004 mysql_stmt_prepare resets the list of warnings
2067 */
2068 
test_bug36004(MYSQL * mysql)2069 static int test_bug36004(MYSQL *mysql)
2070 {
2071   int rc, warning_count= 0;
2072   MYSQL_STMT *stmt;
2073   SKIP_MAXSCALE;
2074   SKIP_MYSQL(mysql); // don't send expected warnings
2075 
2076   if (mysql_get_server_version(mysql) < 60000) {
2077     diag("Test requires MySQL Server version 6.0 or above");
2078     return SKIP;
2079   }
2080 
2081   rc= mysql_query(mysql, "drop table if exists inexistant");
2082   check_mysql_rc(rc, mysql);
2083 
2084   FAIL_UNLESS(mysql_warning_count(mysql) == 1, "");
2085   query_int_variable(mysql, "@@warning_count", &warning_count);
2086   FAIL_UNLESS(warning_count, "Warning expected");
2087 
2088   stmt= mysql_stmt_init(mysql);
2089   FAIL_IF(!stmt, mysql_error(mysql));
2090   rc= mysql_stmt_prepare(stmt, SL("select 1"));
2091   check_stmt_rc(rc, stmt);
2092 
2093   FAIL_UNLESS(mysql_warning_count(mysql) == 0, "No warning expected");
2094   query_int_variable(mysql, "@@warning_count", &warning_count);
2095   FAIL_UNLESS(warning_count, "warning expected");
2096 
2097   rc= mysql_stmt_execute(stmt);
2098   check_stmt_rc(rc, stmt);
2099   FAIL_UNLESS(mysql_warning_count(mysql) == 0, "No warning expected");
2100   mysql_stmt_close(stmt);
2101 
2102   query_int_variable(mysql, "@@warning_count", &warning_count);
2103   FAIL_UNLESS(warning_count, "Warning expected");
2104 
2105   stmt= mysql_stmt_init(mysql);
2106   FAIL_IF(!stmt, mysql_error(mysql));
2107   rc= mysql_stmt_prepare(stmt, SL("drop table if exists inexistant"));
2108   check_stmt_rc(rc, stmt);
2109 
2110   query_int_variable(mysql, "@@warning_count", &warning_count);
2111   FAIL_UNLESS(warning_count == 0, "No warning expected");
2112   mysql_stmt_close(stmt);
2113 
2114   return OK;
2115 }
2116 
test_bug3796(MYSQL * mysql)2117 static int test_bug3796(MYSQL *mysql)
2118 {
2119   MYSQL_STMT *stmt;
2120   MYSQL_BIND my_bind[1];
2121   const char *concat_arg0= "concat_with_";
2122   enum { OUT_BUFF_SIZE= 30 };
2123   char out_buff[OUT_BUFF_SIZE];
2124   char canonical_buff[OUT_BUFF_SIZE];
2125   ulong out_length;
2126   const char *stmt_text;
2127   int rc;
2128 
2129 
2130   /* Create and fill test table */
2131   stmt_text= "DROP TABLE IF EXISTS t1";
2132   rc= mysql_real_query(mysql, SL(stmt_text));
2133   check_mysql_rc(rc, mysql);
2134 
2135   stmt_text= "CREATE TABLE t1 (a INT, b VARCHAR(30))";
2136   rc= mysql_real_query(mysql, SL(stmt_text));
2137   check_mysql_rc(rc, mysql);
2138 
2139   stmt_text= "INSERT INTO t1 VALUES(1, 'ONE'), (2, 'TWO')";
2140   rc= mysql_real_query(mysql, SL(stmt_text));
2141   check_mysql_rc(rc, mysql);
2142 
2143   /* Create statement handle and prepare it with select */
2144   stmt= mysql_stmt_init(mysql);
2145   stmt_text= "SELECT concat(?, b) FROM t1";
2146 
2147   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2148   check_stmt_rc(rc, stmt);
2149   /* Bind input buffers */
2150   memset(my_bind, '\0', sizeof(my_bind));
2151   my_bind[0].buffer_type= MYSQL_TYPE_STRING;
2152   my_bind[0].buffer= (void *) concat_arg0;
2153   my_bind[0].buffer_length= (unsigned long)strlen(concat_arg0);
2154 
2155   mysql_stmt_bind_param(stmt, my_bind);
2156 
2157   /* Execute the select statement */
2158   rc= mysql_stmt_execute(stmt);
2159   check_stmt_rc(rc, stmt);
2160   my_bind[0].buffer= (void *) out_buff;
2161   my_bind[0].buffer_length= OUT_BUFF_SIZE;
2162   my_bind[0].length= &out_length;
2163 
2164   mysql_stmt_bind_result(stmt, my_bind);
2165 
2166   rc= mysql_stmt_fetch(stmt);
2167   check_stmt_rc(rc, stmt);
2168   strcpy(canonical_buff, concat_arg0);
2169   strcat(canonical_buff, "ONE");
2170   FAIL_UNLESS(strlen(canonical_buff) == out_length &&
2171          strncmp(out_buff, canonical_buff, out_length) == 0, "");
2172 
2173   rc= mysql_stmt_fetch(stmt);
2174   check_stmt_rc(rc, stmt);
2175   strcpy(canonical_buff + strlen(concat_arg0), "TWO");
2176   FAIL_UNLESS(strlen(canonical_buff) == out_length &&
2177          strncmp(out_buff, canonical_buff, out_length) == 0, "");
2178 
2179   rc= mysql_stmt_fetch(stmt);
2180   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
2181 
2182   mysql_stmt_close(stmt);
2183 
2184   stmt_text= "DROP TABLE IF EXISTS t1";
2185   rc= mysql_real_query(mysql, SL(stmt_text));
2186   check_mysql_rc(rc, mysql);
2187   return OK;
2188 }
2189 
test_bug4026(MYSQL * mysql)2190 static int test_bug4026(MYSQL *mysql)
2191 {
2192   MYSQL_STMT *stmt;
2193   MYSQL_BIND my_bind[2];
2194   MYSQL_TIME time_in, time_out;
2195   MYSQL_TIME datetime_in, datetime_out;
2196   const char *stmt_text;
2197   int rc;
2198 
2199 
2200   /* Check that microseconds are inserted and selected successfully */
2201 
2202   /* Create a statement handle and prepare it with select */
2203   stmt= mysql_stmt_init(mysql);
2204   stmt_text= "SELECT ?, ?";
2205 
2206   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2207   check_stmt_rc(rc, stmt);
2208   /* Bind input buffers */
2209   memset(my_bind, '\0', sizeof(MYSQL_BIND) * 2);
2210   memset(&time_in, '\0', sizeof(MYSQL_TIME));
2211   memset(&time_out, '\0', sizeof(MYSQL_TIME));
2212   memset(&datetime_in, '\0', sizeof(MYSQL_TIME));
2213   memset(&datetime_out, '\0', sizeof(MYSQL_TIME));
2214   my_bind[0].buffer_type= MYSQL_TYPE_TIME;
2215   my_bind[0].buffer= (void *) &time_in;
2216   my_bind[1].buffer_type= MYSQL_TYPE_DATETIME;
2217   my_bind[1].buffer= (void *) &datetime_in;
2218 
2219   time_in.hour= 23;
2220   time_in.minute= 59;
2221   time_in.second= 59;
2222   time_in.second_part= 123456;
2223   /*
2224     This is not necessary, just to make DIE_UNLESS below work: this field
2225     is filled in when time is received from server
2226   */
2227   time_in.time_type= MYSQL_TIMESTAMP_TIME;
2228 
2229   datetime_in= time_in;
2230   datetime_in.year= 2003;
2231   datetime_in.month= 12;
2232   datetime_in.day= 31;
2233   datetime_in.time_type= MYSQL_TIMESTAMP_DATETIME;
2234 
2235   mysql_stmt_bind_param(stmt, my_bind);
2236 
2237   /* Execute the select statement */
2238   rc= mysql_stmt_execute(stmt);
2239   check_stmt_rc(rc, stmt);
2240   my_bind[0].buffer= (void *) &time_out;
2241   my_bind[1].buffer= (void *) &datetime_out;
2242 
2243   mysql_stmt_bind_result(stmt, my_bind);
2244 
2245   rc= mysql_stmt_fetch(stmt);
2246   FAIL_UNLESS(rc == 0, "rc != 0");
2247   FAIL_UNLESS(memcmp(&time_in, &time_out, sizeof(time_in)) == 0, "time_in != time_out");
2248   FAIL_UNLESS(memcmp(&datetime_in, &datetime_out, sizeof(datetime_in)) == 0, "datetime_in != datetime_out");
2249   mysql_stmt_close(stmt);
2250 
2251   return OK;
2252 }
2253 
test_bug4030(MYSQL * mysql)2254 static int test_bug4030(MYSQL *mysql)
2255 {
2256   MYSQL_STMT *stmt;
2257   MYSQL_BIND my_bind[3];
2258   MYSQL_TIME time_canonical, time_out;
2259   MYSQL_TIME date_canonical, date_out;
2260   MYSQL_TIME datetime_canonical, datetime_out;
2261   const char *stmt_text;
2262   int rc;
2263 
2264 
2265   /* Check that microseconds are inserted and selected successfully */
2266 
2267   /* Execute a query with time values in prepared mode */
2268   stmt= mysql_stmt_init(mysql);
2269   stmt_text= "SELECT '23:59:59.123456', '2003-12-31', "
2270              "'2003-12-31 23:59:59.123456'";
2271   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2272   check_stmt_rc(rc, stmt);
2273   rc= mysql_stmt_execute(stmt);
2274   check_stmt_rc(rc, stmt);
2275   /* Bind output buffers */
2276   memset(my_bind, '\0', sizeof(my_bind));
2277   memset(&time_canonical, '\0', sizeof(time_canonical));
2278   memset(&time_out, '\0', sizeof(time_out));
2279   memset(&date_canonical, '\0', sizeof(date_canonical));
2280   memset(&date_out, '\0', sizeof(date_out));
2281   memset(&datetime_canonical, '\0', sizeof(datetime_canonical));
2282   memset(&datetime_out, '\0', sizeof(datetime_out));
2283   my_bind[0].buffer_type= MYSQL_TYPE_TIME;
2284   my_bind[0].buffer= (void *) &time_out;
2285   my_bind[1].buffer_type= MYSQL_TYPE_DATE;
2286   my_bind[1].buffer= (void *) &date_out;
2287   my_bind[2].buffer_type= MYSQL_TYPE_DATETIME;
2288   my_bind[2].buffer= (void *) &datetime_out;
2289 
2290   time_canonical.hour= 23;
2291   time_canonical.minute= 59;
2292   time_canonical.second= 59;
2293   time_canonical.second_part= 123456;
2294   time_canonical.time_type= MYSQL_TIMESTAMP_TIME;
2295 
2296   date_canonical.year= 2003;
2297   date_canonical.month= 12;
2298   date_canonical.day= 31;
2299   date_canonical.time_type= MYSQL_TIMESTAMP_DATE;
2300 
2301   datetime_canonical= time_canonical;
2302   datetime_canonical.year= 2003;
2303   datetime_canonical.month= 12;
2304   datetime_canonical.day= 31;
2305   datetime_canonical.time_type= MYSQL_TIMESTAMP_DATETIME;
2306 
2307   mysql_stmt_bind_result(stmt, my_bind);
2308 
2309   rc= mysql_stmt_fetch(stmt);
2310   FAIL_UNLESS(rc == 0, "rc != 0");
2311   FAIL_UNLESS(memcmp(&time_canonical, &time_out, sizeof(time_out)) == 0, "time_canonical != time_out");
2312   FAIL_UNLESS(memcmp(&date_canonical, &date_out, sizeof(date_out)) == 0, "date_canoncical != date_out");
2313   FAIL_UNLESS(memcmp(&datetime_canonical, &datetime_out, sizeof(datetime_out)) == 0, "datetime_canonical != datetime_out");
2314   mysql_stmt_close(stmt);
2315   return OK;
2316 }
2317 
test_bug4079(MYSQL * mysql)2318 static int test_bug4079(MYSQL *mysql)
2319 {
2320   MYSQL_STMT *stmt;
2321   MYSQL_BIND my_bind[1];
2322   const char *stmt_text;
2323   uint32 res;
2324   int rc;
2325 
2326   /* Create and fill table */
2327   mysql_query(mysql, "DROP TABLE IF EXISTS t1");
2328   mysql_query(mysql, "CREATE TABLE t1 (a int)");
2329   mysql_query(mysql, "INSERT INTO t1 VALUES (1), (2)");
2330 
2331   /* Prepare erroneous statement */
2332   stmt= mysql_stmt_init(mysql);
2333   stmt_text= "SELECT 1 < (SELECT a FROM t1)";
2334 
2335   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2336   check_stmt_rc(rc, stmt);
2337   /* Execute the select statement */
2338   rc= mysql_stmt_execute(stmt);
2339   check_stmt_rc(rc, stmt);
2340   /* Bind input buffers */
2341   memset(my_bind, '\0', sizeof(my_bind));
2342   my_bind[0].buffer_type= MYSQL_TYPE_LONG;
2343   my_bind[0].buffer= (void *) &res;
2344 
2345   mysql_stmt_bind_result(stmt, my_bind);
2346 
2347   rc= mysql_stmt_fetch(stmt);
2348   FAIL_UNLESS(rc == 1, "rc != 1");
2349   /* buggy version of libmysql hanged up here */
2350   mysql_stmt_close(stmt);
2351   return OK;
2352 }
2353 
test_bug4172(MYSQL * mysql)2354 static int test_bug4172(MYSQL *mysql)
2355 {
2356   MYSQL_STMT *stmt;
2357   MYSQL_BIND my_bind[3];
2358   const char *stmt_text;
2359   MYSQL_RES *res;
2360   MYSQL_ROW row;
2361   int rc;
2362   char f[100], d[100], e[100];
2363   ulong f_len, d_len, e_len;
2364 
2365   mysql_query(mysql, "DROP TABLE IF EXISTS t1");
2366   mysql_query(mysql, "CREATE TABLE t1 (f float, d double, e decimal(10,4))");
2367   mysql_query(mysql, "INSERT INTO t1 VALUES (12345.1234, 123456.123456, "
2368                                             "123456.1234)");
2369 
2370   stmt= mysql_stmt_init(mysql);
2371   stmt_text= "SELECT f, d, e FROM t1";
2372 
2373   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2374   check_stmt_rc(rc, stmt);  rc= mysql_stmt_execute(stmt);
2375   check_stmt_rc(rc, stmt);
2376   memset(my_bind, '\0', sizeof(my_bind));  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
2377   my_bind[0].buffer= f;
2378   my_bind[0].buffer_length= sizeof(f);
2379   my_bind[0].length= &f_len;
2380   my_bind[1].buffer_type= MYSQL_TYPE_STRING;
2381   my_bind[1].buffer= d;
2382   my_bind[1].buffer_length= sizeof(d);
2383   my_bind[1].length= &d_len;
2384   my_bind[2].buffer_type= MYSQL_TYPE_STRING;
2385   my_bind[2].buffer= e;
2386   my_bind[2].buffer_length= sizeof(e);
2387   my_bind[2].length= &e_len;
2388 
2389   mysql_stmt_bind_result(stmt, my_bind);
2390 
2391   mysql_stmt_store_result(stmt);
2392   rc= mysql_stmt_fetch(stmt);
2393   check_stmt_rc(rc, stmt);
2394   rc= mysql_real_query(mysql, SL(stmt_text));
2395   check_mysql_rc(rc, mysql);
2396   res= mysql_store_result(mysql);
2397   row= mysql_fetch_row(res);
2398 
2399   diag("expected %s %s %s", row[0], row[1], row[2]);
2400   diag("fetched %s %s %s", f, d, e);
2401   FAIL_UNLESS(!strcmp(f, row[0]) && !strcmp(d, row[1]) && !strcmp(e, row[2]), "");
2402 
2403   mysql_free_result(res);
2404   mysql_stmt_close(stmt);
2405   return OK;
2406 }
2407 
test_bug4231(MYSQL * mysql)2408 static int test_bug4231(MYSQL *mysql)
2409 {
2410   MYSQL_STMT *stmt;
2411   MYSQL_BIND my_bind[2];
2412   MYSQL_TIME tm[2];
2413   const char *stmt_text;
2414   int rc;
2415 
2416 
2417   stmt_text= "DROP TABLE IF EXISTS t1";
2418   rc= mysql_real_query(mysql, SL(stmt_text));
2419   check_mysql_rc(rc, mysql);
2420 
2421   stmt_text= "CREATE TABLE t1 (a int)";
2422   rc= mysql_real_query(mysql, SL(stmt_text));
2423   check_mysql_rc(rc, mysql);
2424 
2425   stmt_text= "INSERT INTO t1 VALUES (1)";
2426   rc= mysql_real_query(mysql, SL(stmt_text));
2427   check_mysql_rc(rc, mysql);
2428 
2429   stmt= mysql_stmt_init(mysql);
2430   stmt_text= "SELECT a FROM t1 WHERE ? = ?";
2431   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2432   check_stmt_rc(rc, stmt);
2433   /* Bind input buffers */
2434   memset(my_bind, '\0', sizeof(my_bind));  memset(tm, '\0', sizeof(tm));
2435   my_bind[0].buffer_type= MYSQL_TYPE_DATE;
2436   my_bind[0].buffer= &tm[0];
2437   my_bind[1].buffer_type= MYSQL_TYPE_DATE;
2438   my_bind[1].buffer= &tm[1];
2439 
2440   mysql_stmt_bind_param(stmt, my_bind);
2441   check_stmt_rc(rc, stmt);
2442   /*
2443     First set server-side params to some non-zero non-equal values:
2444     then we will check that they are not used when client sends
2445     new (zero) times.
2446   */
2447   tm[0].time_type = MYSQL_TIMESTAMP_DATE;
2448   tm[0].year = 2000;
2449   tm[0].month = 1;
2450   tm[0].day = 1;
2451   tm[1]= tm[0];
2452   --tm[1].year;                                 /* tm[0] != tm[1] */
2453 
2454   rc= mysql_stmt_execute(stmt);
2455   check_stmt_rc(rc, stmt);
2456   rc= mysql_stmt_fetch(stmt);
2457 
2458   /* binds are unequal, no rows should be returned */
2459   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
2460 
2461   /* Set one of the dates to zero */
2462   tm[0].year= tm[0].month= tm[0].day= 0;
2463   tm[1]= tm[0];
2464   mysql_stmt_execute(stmt);
2465   rc= mysql_stmt_fetch(stmt);
2466   FAIL_UNLESS(rc == 0, "rc != 0");
2467 
2468   mysql_stmt_close(stmt);
2469   stmt_text= "DROP TABLE t1";
2470   rc= mysql_real_query(mysql, SL(stmt_text));
2471   check_mysql_rc(rc, mysql);
2472   return OK;
2473 }
2474 
test_bug4236(MYSQL * mysql)2475 static int test_bug4236(MYSQL *mysql)
2476 {
2477   MYSQL_STMT *stmt, *stmt1;
2478   const char *stmt_text;
2479   int rc;
2480   MYSQL_STMT backup;
2481   MYSQL      *mysql1;
2482 
2483 
2484   stmt= mysql_stmt_init(mysql);
2485 
2486   /* mysql_stmt_execute() of statement with statement id= 0 crashed server */
2487   stmt_text= "SELECT 1";
2488   /* We need to prepare statement to pass by possible check in libmysql */
2489   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2490   check_stmt_rc(rc, stmt);  /* Hack to check that server works OK if statement wasn't found */
2491   backup.stmt_id= stmt->stmt_id;
2492   stmt->stmt_id= 0;
2493   rc= mysql_stmt_execute(stmt);
2494   FAIL_IF(!rc, "Error expected");
2495 
2496   /* lets try to hack with a new connection */
2497   mysql1= test_connect(NULL);
2498   stmt1= mysql_stmt_init(mysql1);
2499   stmt_text= "SELECT 2";
2500   rc= mysql_stmt_prepare(stmt1, SL(stmt_text));
2501   check_stmt_rc(rc, stmt);
2502 
2503   stmt->stmt_id= stmt1->stmt_id;
2504   rc= mysql_stmt_execute(stmt);
2505   FAIL_IF(!rc, "Error expected");
2506 
2507   mysql_stmt_close(stmt1);
2508   mysql_close(mysql1);
2509 
2510   /* Restore original statement id to be able to reprepare it */
2511   stmt->stmt_id= backup.stmt_id;
2512 
2513   mysql_stmt_close(stmt);
2514   return OK;
2515 }
2516 
test_bug5126(MYSQL * mysql)2517 static int test_bug5126(MYSQL *mysql)
2518 {
2519   MYSQL_STMT *stmt;
2520   MYSQL_BIND my_bind[2];
2521   int32 c1, c2;
2522   const char *stmt_text;
2523   int rc;
2524 
2525 
2526   stmt_text= "DROP TABLE IF EXISTS t1";
2527   rc= mysql_real_query(mysql, SL(stmt_text));
2528   check_mysql_rc(rc, mysql);
2529 
2530   stmt_text= "CREATE TABLE t1 (a mediumint, b int)";
2531   rc= mysql_real_query(mysql, SL(stmt_text));
2532   check_mysql_rc(rc, mysql);
2533 
2534   stmt_text= "INSERT INTO t1 VALUES (8386608, 1)";
2535   rc= mysql_real_query(mysql, SL(stmt_text));
2536   check_mysql_rc(rc, mysql);
2537 
2538   stmt= mysql_stmt_init(mysql);
2539   stmt_text= "SELECT a, b FROM t1";
2540   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2541   check_stmt_rc(rc, stmt);
2542   rc= mysql_stmt_execute(stmt);
2543   check_stmt_rc(rc, stmt);
2544   /* Bind output buffers */
2545   memset(my_bind, '\0', sizeof(my_bind));
2546   my_bind[0].buffer_type= MYSQL_TYPE_LONG;
2547   my_bind[0].buffer= &c1;
2548   my_bind[1].buffer_type= MYSQL_TYPE_LONG;
2549   my_bind[1].buffer= &c2;
2550 
2551   mysql_stmt_bind_result(stmt, my_bind);
2552 
2553   rc= mysql_stmt_fetch(stmt);
2554   FAIL_UNLESS(rc == 0, "rc != 0");
2555   FAIL_UNLESS(c1 == 8386608 && c2 == 1, "c1 != 8386608 || c2 != 1");
2556   mysql_stmt_close(stmt);
2557   return OK;
2558 }
2559 
test_bug5194(MYSQL * mysql)2560 static int test_bug5194(MYSQL *mysql)
2561 {
2562   MYSQL_STMT *stmt;
2563   MYSQL_BIND *my_bind;
2564   char *query;
2565   char *param_str;
2566   int param_str_length;
2567   const char *stmt_text;
2568   int rc;
2569   float float_array[250] =
2570   {
2571     0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
2572     0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
2573     0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
2574     0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
2575     0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
2576     0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
2577     0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
2578     0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
2579     0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
2580     0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
2581     0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
2582     0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
2583     0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
2584     0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
2585     0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
2586     0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
2587     0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
2588     0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
2589     0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
2590     0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
2591     0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
2592     0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
2593     0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
2594     0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
2595     0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25
2596   };
2597   float *fa_ptr= float_array;
2598   /* Number of columns per row */
2599   const int COLUMN_COUNT= sizeof(float_array)/sizeof(*float_array);
2600   /* Number of rows per bulk insert to start with */
2601   const int MIN_ROWS_PER_INSERT= 262;
2602   /* Max number of rows per bulk insert to end with */
2603   const int MAX_ROWS_PER_INSERT= 300;
2604   const int MAX_PARAM_COUNT= COLUMN_COUNT*MAX_ROWS_PER_INSERT;
2605   const char *query_template= "insert into t1 values %s";
2606   const int CHARS_PER_PARAM= 5; /* space needed to place ", ?" in the query */
2607   const int uint16_max= 65535;
2608   int nrows, i;
2609 
2610   SKIP_MAXSCALE;
2611 
2612   stmt_text= "drop table if exists t1";
2613   rc= mysql_real_query(mysql, SL(stmt_text));
2614 
2615   stmt_text= "create table if not exists t1"
2616    "(c1 float, c2 float, c3 float, c4 float, c5 float, c6 float, "
2617    "c7 float, c8 float, c9 float, c10 float, c11 float, c12 float, "
2618    "c13 float, c14 float, c15 float, c16 float, c17 float, c18 float, "
2619    "c19 float, c20 float, c21 float, c22 float, c23 float, c24 float, "
2620    "c25 float, c26 float, c27 float, c28 float, c29 float, c30 float, "
2621    "c31 float, c32 float, c33 float, c34 float, c35 float, c36 float, "
2622    "c37 float, c38 float, c39 float, c40 float, c41 float, c42 float, "
2623    "c43 float, c44 float, c45 float, c46 float, c47 float, c48 float, "
2624    "c49 float, c50 float, c51 float, c52 float, c53 float, c54 float, "
2625    "c55 float, c56 float, c57 float, c58 float, c59 float, c60 float, "
2626    "c61 float, c62 float, c63 float, c64 float, c65 float, c66 float, "
2627    "c67 float, c68 float, c69 float, c70 float, c71 float, c72 float, "
2628    "c73 float, c74 float, c75 float, c76 float, c77 float, c78 float, "
2629    "c79 float, c80 float, c81 float, c82 float, c83 float, c84 float, "
2630    "c85 float, c86 float, c87 float, c88 float, c89 float, c90 float, "
2631    "c91 float, c92 float, c93 float, c94 float, c95 float, c96 float, "
2632    "c97 float, c98 float, c99 float, c100 float, c101 float, c102 float, "
2633    "c103 float, c104 float, c105 float, c106 float, c107 float, c108 float, "
2634    "c109 float, c110 float, c111 float, c112 float, c113 float, c114 float, "
2635    "c115 float, c116 float, c117 float, c118 float, c119 float, c120 float, "
2636    "c121 float, c122 float, c123 float, c124 float, c125 float, c126 float, "
2637    "c127 float, c128 float, c129 float, c130 float, c131 float, c132 float, "
2638    "c133 float, c134 float, c135 float, c136 float, c137 float, c138 float, "
2639    "c139 float, c140 float, c141 float, c142 float, c143 float, c144 float, "
2640    "c145 float, c146 float, c147 float, c148 float, c149 float, c150 float, "
2641    "c151 float, c152 float, c153 float, c154 float, c155 float, c156 float, "
2642    "c157 float, c158 float, c159 float, c160 float, c161 float, c162 float, "
2643    "c163 float, c164 float, c165 float, c166 float, c167 float, c168 float, "
2644    "c169 float, c170 float, c171 float, c172 float, c173 float, c174 float, "
2645    "c175 float, c176 float, c177 float, c178 float, c179 float, c180 float, "
2646    "c181 float, c182 float, c183 float, c184 float, c185 float, c186 float, "
2647    "c187 float, c188 float, c189 float, c190 float, c191 float, c192 float, "
2648    "c193 float, c194 float, c195 float, c196 float, c197 float, c198 float, "
2649    "c199 float, c200 float, c201 float, c202 float, c203 float, c204 float, "
2650    "c205 float, c206 float, c207 float, c208 float, c209 float, c210 float, "
2651    "c211 float, c212 float, c213 float, c214 float, c215 float, c216 float, "
2652    "c217 float, c218 float, c219 float, c220 float, c221 float, c222 float, "
2653    "c223 float, c224 float, c225 float, c226 float, c227 float, c228 float, "
2654    "c229 float, c230 float, c231 float, c232 float, c233 float, c234 float, "
2655    "c235 float, c236 float, c237 float, c238 float, c239 float, c240 float, "
2656    "c241 float, c242 float, c243 float, c244 float, c245 float, c246 float, "
2657    "c247 float, c248 float, c249 float, c250 float)";
2658   rc= mysql_real_query(mysql, SL(stmt_text));
2659   check_mysql_rc(rc, mysql);
2660 
2661   my_bind= (MYSQL_BIND*) malloc(MAX_PARAM_COUNT * sizeof(MYSQL_BIND));
2662   query= (char*) malloc(strlen(query_template) +
2663                         MAX_PARAM_COUNT * CHARS_PER_PARAM + 1);
2664   param_str= (char*) malloc(COLUMN_COUNT * CHARS_PER_PARAM);
2665 
2666   FAIL_IF(my_bind == 0 || query == 0 || param_str == 0, "Not enough memory");
2667 
2668   stmt= mysql_stmt_init(mysql);
2669 
2670   /* setup a template for one row of parameters */
2671   sprintf(param_str, "(");
2672   for (i= 1; i < COLUMN_COUNT; ++i)
2673     strcat(param_str, "?, ");
2674   strcat(param_str, "?)");
2675   param_str_length= (int)strlen(param_str);
2676 
2677   /* setup bind array */
2678   memset(my_bind, '\0', MAX_PARAM_COUNT * sizeof(MYSQL_BIND));
2679   for (i= 0; i < MAX_PARAM_COUNT; ++i)
2680   {
2681     my_bind[i].buffer_type= MYSQL_TYPE_FLOAT;
2682     my_bind[i].buffer= fa_ptr;
2683     if (++fa_ptr == float_array + COLUMN_COUNT)
2684       fa_ptr= float_array;
2685   }
2686 
2687   /*
2688     Test each number of rows per bulk insert, so that we can see where
2689     MySQL fails.
2690   */
2691   for (nrows= MIN_ROWS_PER_INSERT; nrows <= MAX_ROWS_PER_INSERT; ++nrows)
2692   {
2693     char *query_ptr;
2694     /* Create statement text for current number of rows */
2695     sprintf(query, query_template, param_str);
2696     query_ptr= query + (unsigned long)strlen(query);
2697     for (i= 1; i < nrows; ++i)
2698     {
2699       memcpy(query_ptr, ", ", 2);
2700       query_ptr+= 2;
2701       memcpy(query_ptr, param_str, param_str_length);
2702       query_ptr+= param_str_length;
2703     }
2704     *query_ptr= '\0';
2705 
2706     rc= mysql_stmt_prepare(stmt, query, (ulong)(query_ptr - query));
2707 
2708     if (rc && nrows * COLUMN_COUNT > uint16_max) /* expected error */
2709       break;
2710 
2711     check_stmt_rc(rc, stmt);
2712 
2713     /* bind the parameter array and execute the query */
2714     rc= mysql_stmt_bind_param(stmt, my_bind);
2715     check_stmt_rc(rc, stmt);
2716     rc= mysql_stmt_execute(stmt);
2717     check_stmt_rc(rc, stmt);
2718     rc= mysql_stmt_reset(stmt);
2719   }
2720 
2721   free(param_str);
2722   free(query);
2723   rc= mysql_stmt_close(stmt);
2724   check_stmt_rc(rc, stmt);
2725   free(my_bind);
2726   stmt_text= "drop table t1";
2727   rc= mysql_real_query(mysql, SL(stmt_text));
2728   check_mysql_rc(rc, mysql);
2729   return OK;
2730 }
2731 
test_bug5315(MYSQL * mysql)2732 static int test_bug5315(MYSQL *mysql)
2733 {
2734   MYSQL_STMT *stmt;
2735   const char *stmt_text;
2736   int rc;
2737   SKIP_MAXSCALE;
2738 
2739   if (!is_mariadb)
2740     return SKIP;
2741 
2742   stmt_text= "SELECT 1";
2743   stmt= mysql_stmt_init(mysql);
2744   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2745   check_stmt_rc(rc, stmt);
2746   rc= mysql_change_user(mysql, username, password, schema);
2747   check_mysql_rc(rc, mysql);
2748 
2749   rc= mysql_stmt_execute(stmt);
2750   FAIL_UNLESS(rc != 0, "Error expected");
2751 
2752   rc= mysql_stmt_close(stmt);
2753   check_stmt_rc(rc, stmt);
2754 
2755   stmt= mysql_stmt_init(mysql);
2756   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2757   check_stmt_rc(rc, stmt);
2758   rc= mysql_stmt_execute(stmt);
2759   check_stmt_rc(rc, stmt);
2760   mysql_stmt_close(stmt);
2761   return OK;
2762 }
2763 
test_bug5399(MYSQL * mysql)2764 static int test_bug5399(MYSQL *mysql)
2765 {
2766   /*
2767     Ascii 97 is 'a', which gets mapped to Ascii 65 'A' unless internal
2768     statement id hash in the server uses binary collation.
2769   */
2770 #define NUM_OF_USED_STMT 97
2771   MYSQL_STMT *stmt_list[NUM_OF_USED_STMT];
2772   MYSQL_STMT **stmt;
2773   MYSQL_BIND my_bind[1];
2774   char buff[600];
2775   int rc;
2776   int32 no;
2777 
2778 
2779   memset(my_bind, '\0', sizeof(my_bind));  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
2780   my_bind[0].buffer= &no;
2781 
2782   for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
2783   {
2784     sprintf(buff, "select %d", (int) (stmt - stmt_list));
2785     *stmt= mysql_stmt_init(mysql);
2786     rc= mysql_stmt_prepare(*stmt, SL(buff));
2787     check_stmt_rc(rc, *stmt);    mysql_stmt_bind_result(*stmt, my_bind);
2788   }
2789 
2790   for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
2791   {
2792     rc= mysql_stmt_execute(*stmt);
2793     check_stmt_rc(rc, *stmt);
2794     rc= mysql_stmt_store_result(*stmt);
2795     check_stmt_rc(rc, *stmt);
2796     rc= mysql_stmt_fetch(*stmt);
2797     FAIL_UNLESS((int32) (stmt - stmt_list) == no, "");
2798   }
2799 
2800   for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
2801     mysql_stmt_close(*stmt);
2802 #undef NUM_OF_USED_STMT
2803   return OK;
2804 }
2805 
test_bug6046(MYSQL * mysql)2806 static int test_bug6046(MYSQL *mysql)
2807 {
2808   MYSQL_STMT *stmt;
2809   const char *stmt_text;
2810   int rc;
2811   short b= 1;
2812   MYSQL_BIND my_bind[1];
2813 
2814 
2815   stmt_text= "DROP TABLE IF EXISTS t1";
2816   rc= mysql_real_query(mysql, SL(stmt_text));
2817   check_mysql_rc(rc, mysql);
2818   stmt_text= "CREATE TABLE t1 (a int, b int)";
2819   rc= mysql_real_query(mysql, SL(stmt_text));
2820   check_mysql_rc(rc, mysql);
2821   stmt_text= "INSERT INTO t1 VALUES (1,1),(2,2),(3,1),(4,2)";
2822   rc= mysql_real_query(mysql, SL(stmt_text));
2823   check_mysql_rc(rc, mysql);
2824 
2825   stmt= mysql_stmt_init(mysql);
2826 
2827   stmt_text= "SELECT t1.a FROM t1 NATURAL JOIN t1 as X1 "
2828              "WHERE t1.b > ? ORDER BY t1.a";
2829 
2830   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2831   check_stmt_rc(rc, stmt);
2832   b= 1;
2833   memset(my_bind, '\0', sizeof(my_bind));  my_bind[0].buffer= &b;
2834   my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
2835 
2836   mysql_stmt_bind_param(stmt, my_bind);
2837 
2838   rc= mysql_stmt_execute(stmt);
2839   check_stmt_rc(rc, stmt);
2840   mysql_stmt_store_result(stmt);
2841 
2842   rc= mysql_stmt_execute(stmt);
2843   check_stmt_rc(rc, stmt);
2844   mysql_stmt_close(stmt);
2845   return OK;
2846 }
2847 
test_bug6049(MYSQL * mysql)2848 static int test_bug6049(MYSQL *mysql)
2849 {
2850   MYSQL_STMT *stmt;
2851   MYSQL_BIND my_bind[1];
2852   MYSQL_RES *res;
2853   MYSQL_ROW row;
2854   const char *stmt_text;
2855   char buffer[30];
2856   ulong length;
2857   int rc;
2858 
2859 
2860   stmt_text= "SELECT MAKETIME(-25, 12, 12)";
2861 
2862   rc= mysql_real_query(mysql, SL(stmt_text));
2863   check_mysql_rc(rc, mysql);
2864   res= mysql_store_result(mysql);
2865   row= mysql_fetch_row(res);
2866 
2867   stmt= mysql_stmt_init(mysql);
2868   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2869   check_stmt_rc(rc, stmt);
2870   rc= mysql_stmt_execute(stmt);
2871   check_stmt_rc(rc, stmt);
2872   memset(my_bind, '\0', sizeof(my_bind));
2873   my_bind[0].buffer_type    = MYSQL_TYPE_STRING;
2874   my_bind[0].buffer         = &buffer;
2875   my_bind[0].buffer_length  = sizeof(buffer);
2876   my_bind[0].length         = &length;
2877 
2878   mysql_stmt_bind_result(stmt, my_bind);
2879   rc= mysql_stmt_fetch(stmt);
2880   check_stmt_rc(rc, stmt);
2881 
2882   FAIL_UNLESS(strcmp(row[0], (char*) buffer) == 0, "row[0] != buffer");
2883 
2884   mysql_free_result(res);
2885   mysql_stmt_close(stmt);
2886   return OK;
2887 }
2888 
test_bug6058(MYSQL * mysql)2889 static int test_bug6058(MYSQL *mysql)
2890 {
2891   MYSQL_STMT *stmt;
2892   MYSQL_BIND my_bind[1];
2893   MYSQL_RES *res;
2894   MYSQL_ROW row;
2895   const char *stmt_text;
2896   char buffer[30];
2897   ulong length;
2898   int rc;
2899 
2900 
2901   stmt_text= "SELECT CAST('0000-00-00' AS DATE)";
2902 
2903   rc= mysql_real_query(mysql, SL(stmt_text));
2904   check_mysql_rc(rc, mysql);
2905   res= mysql_store_result(mysql);
2906   row= mysql_fetch_row(res);
2907 
2908   stmt= mysql_stmt_init(mysql);
2909   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2910   check_stmt_rc(rc, stmt);
2911   rc= mysql_stmt_execute(stmt);
2912   check_stmt_rc(rc, stmt);
2913   memset(my_bind, '\0', sizeof(my_bind));
2914   my_bind[0].buffer_type    = MYSQL_TYPE_STRING;
2915   my_bind[0].buffer         = &buffer;
2916   my_bind[0].buffer_length  = sizeof(buffer);
2917   my_bind[0].length         = &length;
2918 
2919   mysql_stmt_bind_result(stmt, my_bind);
2920   rc= mysql_stmt_fetch(stmt);
2921   check_stmt_rc(rc, stmt);
2922 
2923   FAIL_UNLESS(strcmp(row[0], buffer) == 0, "row[0] != buffer");
2924 
2925   mysql_free_result(res);
2926   mysql_stmt_close(stmt);
2927   return OK;
2928 }
2929 
2930 
test_bug6059(MYSQL * mysql)2931 static int test_bug6059(MYSQL *mysql)
2932 {
2933   MYSQL_STMT *stmt;
2934   const char *stmt_text;
2935   int rc;
2936 
2937   SKIP_SKYSQL;
2938 
2939   stmt_text= "SELECT 'foo' INTO OUTFILE 'x.3'";
2940 
2941   stmt= mysql_stmt_init(mysql);
2942   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2943   check_stmt_rc(rc, stmt);
2944   FAIL_UNLESS(mysql_stmt_field_count(stmt) == 0, "");
2945   mysql_stmt_close(stmt);
2946   return OK;
2947 }
2948 
test_bug6096(MYSQL * mysql)2949 static int test_bug6096(MYSQL *mysql)
2950 {
2951   MYSQL_STMT *stmt;
2952   MYSQL_RES *query_result, *stmt_metadata;
2953   const char *stmt_text;
2954   MYSQL_BIND my_bind[12];
2955   MYSQL_FIELD *query_field_list, *stmt_field_list;
2956   ulong query_field_count, stmt_field_count;
2957   int rc;
2958   my_bool update_max_length= TRUE;
2959   uint i;
2960 
2961 
2962   stmt_text= "drop table if exists t1";
2963   rc= mysql_real_query(mysql, SL(stmt_text));
2964   check_mysql_rc(rc, mysql);
2965 
2966   mysql_query(mysql, "set sql_mode=''");
2967   stmt_text= "create table t1 (c_tinyint tinyint, c_smallint smallint, "
2968                              " c_mediumint mediumint, c_int int, "
2969                              " c_bigint bigint, c_float float, "
2970                              " c_double double, c_varchar varchar(20), "
2971                              " c_char char(20), c_time time, c_date date, "
2972                              " c_datetime datetime)";
2973   rc= mysql_real_query(mysql, SL(stmt_text));
2974   check_mysql_rc(rc, mysql);
2975   stmt_text= "insert into t1  values (-100, -20000, 30000000, 4, 8, 1.0, "
2976                                      "2.0, 'abc', 'def', now(), now(), now())";
2977   rc= mysql_real_query(mysql, SL(stmt_text));
2978   check_mysql_rc(rc, mysql);
2979 
2980   stmt_text= "select * from t1";
2981 
2982   /* Run select in prepared and non-prepared mode and compare metadata */
2983   rc= mysql_real_query(mysql, SL(stmt_text));
2984   check_mysql_rc(rc, mysql);
2985   query_result= mysql_store_result(mysql);
2986   query_field_list= mysql_fetch_fields(query_result);
2987   FAIL_IF(!query_field_list, "fetch_fields failed");
2988   query_field_count= mysql_num_fields(query_result);
2989 
2990   stmt= mysql_stmt_init(mysql);
2991   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
2992   check_stmt_rc(rc, stmt);  rc= mysql_stmt_execute(stmt);
2993   check_stmt_rc(rc, stmt);  mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH,
2994                       (void*) &update_max_length);
2995   mysql_stmt_store_result(stmt);
2996   stmt_metadata= mysql_stmt_result_metadata(stmt);
2997   stmt_field_list= mysql_fetch_fields(stmt_metadata);
2998   stmt_field_count= mysql_num_fields(stmt_metadata);
2999   FAIL_UNLESS(stmt_field_count == query_field_count, "");
3000 
3001 
3002   /* Bind and fetch the data */
3003 
3004   memset(my_bind, '\0', sizeof(my_bind));
3005   for (i= 0; i < stmt_field_count; ++i)
3006   {
3007     my_bind[i].buffer_type= MYSQL_TYPE_STRING;
3008     my_bind[i].buffer_length= stmt_field_list[i].max_length + 1;
3009     my_bind[i].buffer= malloc(my_bind[i].buffer_length);
3010   }
3011   mysql_stmt_bind_result(stmt, my_bind);
3012   rc= mysql_stmt_fetch(stmt);
3013   diag("rc=%d", rc);
3014   check_stmt_rc(rc, stmt);
3015   rc= mysql_stmt_fetch(stmt);
3016   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
3017 
3018   /* Clean up */
3019 
3020   for (i= 0; i < stmt_field_count; ++i)
3021     free(my_bind[i].buffer);
3022   mysql_stmt_close(stmt);
3023   mysql_free_result(query_result);
3024   mysql_free_result(stmt_metadata);
3025   stmt_text= "drop table t1";
3026   rc= mysql_real_query(mysql, SL(stmt_text));
3027   check_mysql_rc(rc, mysql);
3028   return OK;
3029 }
3030 
3031 /* Bug#7990 - mysql_stmt_close doesn't reset mysql->net.last_error */
3032 
test_bug7990(MYSQL * mysql)3033 static int test_bug7990(MYSQL *mysql)
3034 {
3035   MYSQL_STMT *stmt;
3036   int rc;
3037 
3038   stmt= mysql_stmt_init(mysql);
3039   rc= mysql_stmt_prepare(stmt, "foo", 3);
3040   /*
3041     XXX: the fact that we store errno both in STMT and in
3042     MYSQL is not documented and is subject to change in 5.0
3043   */
3044   FAIL_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql), "Error expected");
3045   mysql_stmt_close(stmt);
3046   return OK;
3047 }
3048 
3049 /* Bug#8330 - mysql_stmt_execute crashes (libmysql) */
3050 
test_bug8330(MYSQL * mysql)3051 static int test_bug8330(MYSQL *mysql)
3052 {
3053   const char *stmt_text;
3054   MYSQL_STMT *stmt[2];
3055   int i, rc;
3056   const char *query= "select a,b from t1 where a=?";
3057   MYSQL_BIND my_bind[2];
3058   long lval[2]= {1,2};
3059 
3060   stmt_text= "drop table if exists t1";
3061   /* in case some previous test failed */
3062   rc= mysql_real_query(mysql, SL(stmt_text));
3063   check_mysql_rc(rc, mysql);
3064   stmt_text= "create table t1 (a int, b int)";
3065   rc= mysql_real_query(mysql, SL(stmt_text));
3066   check_mysql_rc(rc, mysql);
3067 
3068   memset(my_bind, '\0', sizeof(my_bind));
3069   for (i=0; i < 2; i++)
3070   {
3071     stmt[i]= mysql_stmt_init(mysql);
3072     rc= mysql_stmt_prepare(stmt[i], SL(query));
3073     check_stmt_rc(rc, stmt[i]);
3074     my_bind[i].buffer_type= MYSQL_TYPE_LONG;
3075     my_bind[i].buffer= (void*) &lval[i];
3076     my_bind[i].is_null= 0;
3077     mysql_stmt_bind_param(stmt[i], &my_bind[i]);
3078   }
3079 
3080   rc= mysql_stmt_execute(stmt[0]);
3081   check_stmt_rc(rc, stmt[0]);
3082   rc= mysql_stmt_execute(stmt[1]);
3083   FAIL_UNLESS(rc && mysql_stmt_errno(stmt[1]) == CR_COMMANDS_OUT_OF_SYNC, "Error expected");
3084   rc= mysql_stmt_execute(stmt[0]);
3085   check_stmt_rc(rc, stmt[0]);
3086   mysql_stmt_close(stmt[0]);
3087   mysql_stmt_close(stmt[1]);
3088 
3089   stmt_text= "drop table t1";
3090   rc= mysql_real_query(mysql, SL(stmt_text));
3091   check_mysql_rc(rc, mysql);
3092   return OK;
3093 }
3094 
3095 /* Test misc field information, bug: #74 */
3096 
test_field_misc(MYSQL * mysql)3097 static int test_field_misc(MYSQL *mysql)
3098 {
3099   MYSQL_STMT  *stmt;
3100   MYSQL_RES   *result;
3101   int         rc;
3102 
3103 
3104   rc= mysql_query(mysql, "SELECT @@autocommit");
3105   check_mysql_rc(rc, mysql);
3106 
3107   result= mysql_store_result(mysql);
3108   FAIL_IF(!result, "Invalid result set");
3109 
3110   rc= 0;
3111   while (mysql_fetch_row(result))
3112     rc++;
3113   FAIL_UNLESS(rc == 1, "rowcount != 1");
3114 
3115   verify_prepare_field(result, 0,
3116                        "@@autocommit", "",  /* field and its org name */
3117                        MYSQL_TYPE_LONGLONG, /* field type */
3118                        "", "",              /* table and its org name */
3119                        "", 1, 0);           /* db name, length(its bool flag)*/
3120 
3121   mysql_free_result(result);
3122 
3123   stmt= mysql_stmt_init(mysql);
3124   FAIL_IF(!stmt, mysql_error(mysql));
3125   rc= mysql_stmt_prepare(stmt, SL("SELECT @@autocommit"));
3126   check_stmt_rc(rc, stmt);
3127 
3128   rc= mysql_stmt_execute(stmt);
3129   check_stmt_rc(rc, stmt);
3130 
3131   result= mysql_stmt_result_metadata(stmt);
3132   FAIL_IF(!result, "Invalid result set");
3133 
3134   rc= 0;
3135   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
3136     rc++;
3137   FAIL_UNLESS(rc == 1, "rowcount != 1");
3138 
3139   verify_prepare_field(result, 0,
3140                        "@@autocommit", "",  /* field and its org name */
3141                        MYSQL_TYPE_LONGLONG, /* field type */
3142                        "", "",              /* table and its org name */
3143                        "", 1, 0);           /* db name, length(its bool flag)*/
3144 
3145   mysql_free_result(result);
3146   mysql_stmt_close(stmt);
3147 
3148   stmt= mysql_stmt_init(mysql);
3149   FAIL_IF(!stmt, mysql_error(mysql));
3150   rc= mysql_stmt_prepare(stmt, SL("SELECT @@max_error_count"));
3151   check_stmt_rc(rc, stmt);
3152 
3153   result= mysql_stmt_result_metadata(stmt);
3154   FAIL_IF(!result, "Invalid result set");
3155 
3156   rc= mysql_stmt_execute(stmt);
3157   check_stmt_rc(rc, stmt);
3158 
3159   rc= 0;
3160   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
3161     rc++;
3162   FAIL_UNLESS(rc == 1, "rowcount != 1");
3163 
3164   if (verify_prepare_field(result, 0,
3165                        "@@max_error_count", "",   /* field and its org name */
3166                        MYSQL_TYPE_LONGLONG, /* field type */
3167                        "", "",              /* table and its org name */
3168                        /* db name, length */
3169                        "", MY_INT64_NUM_DECIMAL_DIGITS , 0))
3170     goto error;
3171 
3172   mysql_free_result(result);
3173   mysql_stmt_close(stmt);
3174 
3175   stmt= mysql_stmt_init(mysql);
3176   FAIL_IF(!stmt, mysql_error(mysql));
3177   rc= mysql_stmt_prepare(stmt, SL("SELECT @@max_allowed_packet"));
3178   check_stmt_rc(rc, stmt);
3179 
3180   result= mysql_stmt_result_metadata(stmt);
3181   FAIL_IF(!result, "Invalid result set");
3182 
3183   rc= mysql_stmt_execute(stmt);
3184   check_stmt_rc(rc, stmt);
3185 
3186   rc= 0;
3187   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
3188     rc++;
3189   FAIL_UNLESS(rc == 1, "rowcount != 1");
3190 
3191   if (verify_prepare_field(result, 0,
3192                        "@@max_allowed_packet", "", /* field and its org name */
3193                        MYSQL_TYPE_LONGLONG, /* field type */
3194                        "", "",              /* table and its org name */
3195                        /* db name, length */
3196                        "", MY_INT64_NUM_DECIMAL_DIGITS, 0))
3197     goto error;
3198 
3199   mysql_free_result(result);
3200   mysql_stmt_close(stmt);
3201 
3202   stmt= mysql_stmt_init(mysql);
3203   FAIL_IF(!stmt, mysql_error(mysql));
3204   rc= mysql_stmt_prepare(stmt, SL("SELECT @@sql_warnings"));
3205   check_stmt_rc(rc, stmt);
3206 
3207   result= mysql_stmt_result_metadata(stmt);
3208   FAIL_IF(!result, "Invalid result set");
3209 
3210   rc= mysql_stmt_execute(stmt);
3211   check_stmt_rc(rc, stmt);
3212 
3213   rc= 0;
3214   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
3215     rc++;
3216   FAIL_UNLESS(rc == 1, "rowcount != 1");
3217 
3218   if (verify_prepare_field(result, 0,
3219                        "@@sql_warnings", "",  /* field and its org name */
3220                        MYSQL_TYPE_LONGLONG,   /* field type */
3221                        "", "",                /* table and its org name */
3222                        "", 1, 0))             /* db name, length */
3223     goto error;
3224 
3225   mysql_free_result(result);
3226   mysql_stmt_close(stmt);
3227   return OK;
3228 
3229 error:
3230   mysql_free_result(result);
3231   mysql_stmt_close(stmt);
3232   return FAIL;
3233 }
3234 
3235 /* Test a memory ovverun bug */
3236 
test_mem_overun(MYSQL * mysql)3237 static int test_mem_overun(MYSQL *mysql)
3238 {
3239   char       buffer[10000], field[12];
3240   MYSQL_STMT *stmt;
3241   MYSQL_RES  *field_res, *res;
3242   int        rc, i, length;
3243 
3244   /*
3245     Test a memory ovverun bug when a table had 1000 fields with
3246     a row of data
3247   */
3248   rc= mysql_query(mysql, "drop table if exists t_mem_overun");
3249   check_mysql_rc(rc, mysql);
3250 
3251   strcpy(buffer, "create table t_mem_overun(");
3252   for (i= 0; i < 1000; i++)
3253   {
3254     sprintf(field, "c%d int, ", i);
3255     strcat(buffer, field);
3256   }
3257   length= (int)strlen(buffer);
3258   buffer[length-2]= ')';
3259   buffer[--length]= '\0';
3260 
3261   rc= mysql_real_query(mysql, buffer, length);
3262   check_mysql_rc(rc, mysql);
3263 
3264   strcpy(buffer, "insert into t_mem_overun values(");
3265   for (i= 0; i < 1000; i++)
3266   {
3267     strcat(buffer, "1, ");
3268   }
3269   length= (int)strlen(buffer);
3270   buffer[length-2]= ')';
3271   buffer[--length]= '\0';
3272 
3273   rc= mysql_real_query(mysql, buffer, length);
3274   check_mysql_rc(rc, mysql);
3275 
3276   rc= mysql_query(mysql, "select * from t_mem_overun");
3277   check_mysql_rc(rc, mysql);
3278 
3279   res= mysql_store_result(mysql);
3280   rc= 0;
3281   while (mysql_fetch_row(res))
3282     rc++;
3283   FAIL_UNLESS(rc == 1, "rowcount != 1");
3284   mysql_free_result(res);
3285 
3286   stmt= mysql_stmt_init(mysql);
3287   FAIL_IF(!stmt, mysql_error(mysql));
3288   rc= mysql_stmt_prepare(stmt, SL("select * from t_mem_overun"));
3289   check_stmt_rc(rc, stmt);
3290 
3291   rc= mysql_stmt_execute(stmt);
3292   check_stmt_rc(rc, stmt);
3293 
3294   field_res= mysql_stmt_result_metadata(stmt);
3295   FAIL_IF(!field_res, "Invalid result set");
3296 
3297   FAIL_UNLESS( 1000 == mysql_num_fields(field_res), "fields != 1000");
3298 
3299   rc= mysql_stmt_store_result(stmt);
3300   check_stmt_rc(rc, stmt);
3301 
3302   rc= mysql_stmt_fetch(stmt);
3303   check_stmt_rc(rc, stmt);
3304 
3305   rc= mysql_stmt_fetch(stmt);
3306   FAIL_UNLESS(rc == MYSQL_NO_DATA, "");
3307 
3308   mysql_free_result(field_res);
3309 
3310   mysql_stmt_close(stmt);
3311   rc= mysql_query(mysql, "drop table if exists t_mem_overun");
3312   check_mysql_rc(rc, mysql);
3313   return OK;
3314 }
3315 
test_bug8722(MYSQL * mysql)3316 static int test_bug8722(MYSQL *mysql)
3317 {
3318   MYSQL_STMT *stmt;
3319   int rc;
3320   const char *stmt_text;
3321 
3322   /* Prepare test data */
3323   stmt_text= "drop table if exists t1";
3324   rc= mysql_real_query(mysql, SL(stmt_text));
3325   check_mysql_rc(rc, mysql);
3326   stmt_text= "drop view if exists v1";
3327   rc= mysql_real_query(mysql, SL(stmt_text));
3328   check_mysql_rc(rc, mysql);
3329   stmt_text= "CREATE TABLE t1 (c1 varchar(10), c2 varchar(10), c3 varchar(10),"
3330                              " c4 varchar(10), c5 varchar(10), c6 varchar(10),"
3331                              " c7 varchar(10), c8 varchar(10), c9 varchar(10),"
3332                              "c10 varchar(10))";
3333   rc= mysql_real_query(mysql, SL(stmt_text));
3334   check_mysql_rc(rc, mysql);
3335   stmt_text= "INSERT INTO t1 VALUES (1,2,3,4,5,6,7,8,9,10)";
3336   rc= mysql_real_query(mysql, SL(stmt_text));
3337   check_mysql_rc(rc, mysql);
3338   stmt_text= "CREATE VIEW v1 AS SELECT * FROM t1";
3339   rc= mysql_real_query(mysql, SL(stmt_text));
3340   check_mysql_rc(rc, mysql);
3341 
3342   stmt= mysql_stmt_init(mysql);
3343   stmt_text= "select * from v1";
3344   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
3345   check_stmt_rc(rc, stmt);
3346   mysql_stmt_close(stmt);
3347   stmt_text= "drop table if exists t1, v1";
3348   rc= mysql_query(mysql, "DROP TABLE t1");
3349   check_mysql_rc(rc, mysql);
3350   rc= mysql_query(mysql, "DROP VIEW v1");
3351   check_mysql_rc(rc, mysql);
3352   return OK;
3353 }
3354 
3355 /* Test DECIMAL conversion */
3356 
test_decimal_bug(MYSQL * mysql)3357 static int test_decimal_bug(MYSQL *mysql)
3358 {
3359   MYSQL_STMT *stmt;
3360   MYSQL_BIND my_bind[1];
3361   char       data[30];
3362   int        rc;
3363   my_bool    is_null;
3364 
3365   mysql_autocommit(mysql, TRUE);
3366 
3367   rc= mysql_query(mysql, "drop table if exists test_decimal_bug");
3368   check_mysql_rc(rc, mysql);
3369 
3370   rc= mysql_query(mysql, "create table test_decimal_bug(c1 decimal(10, 2))");
3371   check_mysql_rc(rc, mysql);
3372 
3373   rc= mysql_query(mysql, "insert into test_decimal_bug value(8), (10.22), (5.61)");
3374   check_mysql_rc(rc, mysql);
3375 
3376   stmt= mysql_stmt_init(mysql);
3377   FAIL_IF(!stmt, mysql_error(mysql));
3378   rc= mysql_stmt_prepare(stmt, SL("select c1 from test_decimal_bug where c1=?"));
3379   check_stmt_rc(rc, stmt);
3380 
3381   /*
3382     We need to bzero bind structure because mysql_stmt_bind_param checks all
3383     its members.
3384   */
3385   memset(my_bind, '\0', sizeof(my_bind));
3386 
3387   memset(data, 0, sizeof(data));
3388   my_bind[0].buffer_type= MYSQL_TYPE_NEWDECIMAL;
3389   my_bind[0].buffer= (void *)data;
3390   my_bind[0].buffer_length= 25;
3391   my_bind[0].is_null= &is_null;
3392 
3393   is_null= 0;
3394   rc= mysql_stmt_bind_param(stmt, my_bind);
3395   check_stmt_rc(rc, stmt);
3396 
3397   strcpy(data, "8.0");
3398   rc= mysql_stmt_execute(stmt);
3399   check_stmt_rc(rc, stmt);
3400 
3401   data[0]= 0;
3402   rc= mysql_stmt_bind_result(stmt, my_bind);
3403   check_stmt_rc(rc, stmt);
3404 
3405   rc= mysql_stmt_fetch(stmt);
3406   check_stmt_rc(rc, stmt);
3407 
3408   FAIL_UNLESS(strcmp(data, "8.00") == 0, "data != '8.00'");
3409 
3410   rc= mysql_stmt_fetch(stmt);
3411   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
3412 
3413   strcpy(data, "5.61");
3414   rc= mysql_stmt_execute(stmt);
3415   check_stmt_rc(rc, stmt);
3416 
3417   data[0]= 0;
3418   rc= mysql_stmt_bind_result(stmt, my_bind);
3419   check_stmt_rc(rc, stmt);
3420 
3421   rc= mysql_stmt_fetch(stmt);
3422   check_stmt_rc(rc, stmt);
3423 
3424   FAIL_UNLESS(strcmp(data, "5.61") == 0, "data != '5.61'");
3425 
3426   rc= mysql_stmt_fetch(stmt);
3427   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
3428 
3429   is_null= 1;
3430   rc= mysql_stmt_execute(stmt);
3431   check_stmt_rc(rc, stmt);
3432 
3433   rc= mysql_stmt_fetch(stmt);
3434   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
3435 
3436   strcpy(data, "10.22"); is_null= 0;
3437   rc= mysql_stmt_execute(stmt);
3438   check_stmt_rc(rc, stmt);
3439 
3440   data[0]= 0;
3441   rc= mysql_stmt_bind_result(stmt, my_bind);
3442   check_stmt_rc(rc, stmt);
3443 
3444   rc= mysql_stmt_fetch(stmt);
3445   check_stmt_rc(rc, stmt);
3446 
3447   FAIL_UNLESS(strcmp(data, "10.22") == 0, "data != '10.22'");
3448 
3449   rc= mysql_stmt_fetch(stmt);
3450   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
3451 
3452   mysql_stmt_close(stmt);
3453   rc= mysql_query(mysql, "drop table if exists test_decimal_bug");
3454   check_mysql_rc(rc, mysql);
3455   return OK;
3456 }
3457 
3458 /* Test EXPLAIN bug (#115, reported by mark@mysql.com & georg@php.net). */
3459 
test_explain_bug(MYSQL * mysql)3460 static int test_explain_bug(MYSQL *mysql)
3461 {
3462   MYSQL_STMT *stmt;
3463   MYSQL_RES  *result;
3464   int        rc;
3465 
3466   if (!is_mariadb)
3467     return SKIP;
3468 
3469   mysql_autocommit(mysql, TRUE);
3470 
3471   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_explain");
3472   check_mysql_rc(rc, mysql);
3473 
3474   rc= mysql_query(mysql, "CREATE TABLE test_explain(id int, name char(2))");
3475   check_mysql_rc(rc, mysql);
3476 
3477   stmt= mysql_stmt_init(mysql);
3478   FAIL_IF(!stmt, mysql_error(mysql));
3479   rc= mysql_stmt_prepare(stmt, SL("explain test_explain"));
3480   check_stmt_rc(rc, stmt);
3481 
3482   rc= mysql_stmt_execute(stmt);
3483   check_stmt_rc(rc, stmt);
3484 
3485   rc= 0;
3486   while (!mysql_stmt_fetch(stmt))
3487     rc++;
3488   FAIL_UNLESS(rc == 2, "rowcount != 2");
3489 
3490   result= mysql_stmt_result_metadata(stmt);
3491   FAIL_IF(!result, "Invalid result set");
3492 
3493   FAIL_UNLESS(6 == mysql_num_fields(result), "fields != 6");
3494 
3495   if (verify_prepare_field(result, 0, "Field", "COLUMN_NAME",
3496                        mysql_get_server_version(mysql) <= 50000 ?
3497                        MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
3498                        0, 0,
3499                        mysql_get_server_version(mysql) <= 50400 ? "" : "information_schema",
3500                        64, 0))
3501     goto error;
3502 
3503   if (verify_prepare_field(result, 1, "Type", "COLUMN_TYPE", MYSQL_TYPE_BLOB,
3504                        0, 0,
3505                        mysql_get_server_version(mysql) <= 50400 ? "" : "information_schema",
3506                        0, 0))
3507     goto error;
3508 
3509   if (verify_prepare_field(result, 2, "Null", "IS_NULLABLE",
3510                        mysql_get_server_version(mysql) <= 50000 ?
3511                        MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
3512                        0, 0,
3513                        mysql_get_server_version(mysql) <= 50400 ? "" : "information_schema",
3514                        3, 0))
3515     goto error;
3516 
3517   if (verify_prepare_field(result, 3, "Key", "COLUMN_KEY",
3518                        mysql_get_server_version(mysql) <= 50000 ?
3519                        MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
3520                        0, 0,
3521                        mysql_get_server_version(mysql) <= 50400 ? "" : "information_schema",
3522                        3, 0))
3523     goto error;
3524 
3525   if ( mysql_get_server_version(mysql) >= 50027 )
3526   {
3527     /*  The patch for bug#23037 changes column type of DEAULT to blob */
3528     if (verify_prepare_field(result, 4, "Default", "COLUMN_DEFAULT",
3529                          MYSQL_TYPE_BLOB, 0, 0,
3530                          mysql_get_server_version(mysql) <= 50400 ? "" : "information_schema",
3531                          0, 0))
3532       goto error;
3533   }
3534   else
3535   {
3536     if (verify_prepare_field(result, 4, "Default", "COLUMN_DEFAULT",
3537                          mysql_get_server_version(mysql) >= 50027 ?
3538                          MYSQL_TYPE_BLOB :
3539                          mysql_get_server_version(mysql) <= 50000 ?
3540                          MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
3541                          0, 0,
3542                          mysql_get_server_version(mysql) <= 50400 ? "" : "information_schema",
3543                          mysql_get_server_version(mysql) >= 50027 ? 0 :64, 0))
3544       goto error;
3545   }
3546 
3547   if (verify_prepare_field(result, 5, "Extra", "EXTRA",
3548                        mysql_get_server_version(mysql) <= 50000 ?
3549                        MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
3550                        0, 0,
3551                        mysql_get_server_version(mysql) <= 50400 ? "" : "information_schema",
3552                        27, 0))
3553     goto error;
3554 
3555   mysql_free_result(result);
3556   mysql_stmt_close(stmt);
3557 
3558   stmt= mysql_stmt_init(mysql);
3559   FAIL_IF(!stmt, mysql_error(mysql));
3560   rc= mysql_stmt_prepare(stmt, SL("explain select id, name FROM test_explain"));
3561   check_stmt_rc(rc, stmt);
3562 
3563   rc= mysql_stmt_execute(stmt);
3564   check_stmt_rc(rc, stmt);
3565 
3566   rc= 0;
3567   while (!mysql_stmt_fetch(stmt))
3568     rc++;
3569   FAIL_UNLESS(rc == 1, "rowcount != 1");
3570 
3571   result= mysql_stmt_result_metadata(stmt);
3572   FAIL_IF(!result, "Invalid result set");
3573 
3574   FAIL_UNLESS(10 == mysql_num_fields(result), "fields != 10");
3575 
3576   if (verify_prepare_field(result, 0, "id", "", MYSQL_TYPE_LONGLONG, "", "", "", 3, 0))
3577     goto error;
3578 
3579   if (verify_prepare_field(result, 1, "select_type", "", MYSQL_TYPE_VAR_STRING, "", "", "", 19, 0))
3580     goto error;
3581 
3582   if (verify_prepare_field(result, 2, "table", "", MYSQL_TYPE_VAR_STRING, "", "", "", NAME_CHAR_LEN, 0))
3583     goto error;
3584 
3585   if (verify_prepare_field(result, 3, "type", "", MYSQL_TYPE_VAR_STRING, "", "", "", 10, 0))
3586     goto error;
3587 
3588   if (verify_prepare_field(result, 4, "possible_keys", "", MYSQL_TYPE_VAR_STRING, "", "", "", NAME_CHAR_LEN*MAX_KEY, 0))
3589     goto error;
3590 
3591   if ( verify_prepare_field(result, 5, "key", "", MYSQL_TYPE_VAR_STRING, "", "", "", NAME_CHAR_LEN, 0))
3592     goto error;
3593 
3594   if (mysql_get_server_version(mysql) <= 50000)
3595   {
3596     if (verify_prepare_field(result, 6, "key_len", "", MYSQL_TYPE_LONGLONG, "", "", "", 3, 0))
3597       goto error;
3598   }
3599   else if (mysql_get_server_version(mysql) <= 60000)
3600   {
3601     if (verify_prepare_field(result, 6, "key_len", "", MYSQL_TYPE_VAR_STRING, "", "", "", NAME_CHAR_LEN*MAX_KEY, 0))
3602       goto error;
3603   }
3604   else
3605   {
3606     if (verify_prepare_field(result, 6, "key_len", "", MYSQL_TYPE_VAR_STRING, "", "", "", (MAX_KEY_LENGTH_DECIMAL_WIDTH + 1) * MAX_KEY, 0))
3607     goto error;
3608   }
3609 
3610   if (verify_prepare_field(result, 7, "ref", "", MYSQL_TYPE_VAR_STRING, "", "", "",
3611                            NAME_CHAR_LEN*16, 0))
3612     goto error;
3613 
3614   if (verify_prepare_field(result, 8, "rows", "", MYSQL_TYPE_LONGLONG, "", "", "", 10, 0))
3615     goto error;
3616 
3617   if (verify_prepare_field(result, 9, "Extra", "", MYSQL_TYPE_VAR_STRING, "", "", "", 255, 0))
3618     goto error;
3619 
3620   mysql_free_result(result);
3621   mysql_stmt_close(stmt);
3622   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_explain");
3623   check_mysql_rc(rc, mysql);
3624   return OK;
3625 error:
3626   mysql_free_result(result);
3627   mysql_stmt_close(stmt);
3628   return FAIL;
3629 }
3630 
test_sshort_bug(MYSQL * mysql)3631 static int test_sshort_bug(MYSQL *mysql)
3632 {
3633   MYSQL_STMT *stmt;
3634   MYSQL_BIND my_bind[4];
3635   short      short_value;
3636   int32      long_value;
3637   ulong      s_length, l_length, ll_length, t_length;
3638   ulonglong  longlong_value;
3639   int        rc;
3640   uchar      tiny_value;
3641 
3642   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_sshort");
3643   check_mysql_rc(rc, mysql);
3644 
3645   rc= mysql_query(mysql, "CREATE TABLE test_sshort(a smallint signed, \
3646                                                   b smallint signed, \
3647                                                   c smallint unsigned, \
3648                                                   d smallint unsigned)");
3649   check_mysql_rc(rc, mysql);
3650 
3651   rc= mysql_query(mysql, "INSERT INTO test_sshort VALUES(-5999, -5999, 35999, 200)");
3652   check_mysql_rc(rc, mysql);
3653 
3654 
3655   stmt= mysql_stmt_init(mysql);
3656   FAIL_IF(!stmt, mysql_error(mysql));
3657   rc= mysql_stmt_prepare(stmt, SL("SELECT * FROM test_sshort"));
3658   check_stmt_rc(rc, stmt);
3659 
3660   rc= mysql_stmt_execute(stmt);
3661   check_stmt_rc(rc, stmt);
3662 
3663   memset(my_bind, '\0', sizeof(my_bind));
3664   my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
3665   my_bind[0].buffer= (void *)&short_value;
3666   my_bind[0].length= &s_length;
3667 
3668   my_bind[1].buffer_type= MYSQL_TYPE_LONG;
3669   my_bind[1].buffer= (void *)&long_value;
3670   my_bind[1].length= &l_length;
3671 
3672   my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
3673   my_bind[2].buffer= (void *)&longlong_value;
3674   my_bind[2].length= &ll_length;
3675 
3676   my_bind[3].buffer_type= MYSQL_TYPE_TINY;
3677   my_bind[3].buffer= (void *)&tiny_value;
3678   my_bind[3].is_unsigned= TRUE;
3679   my_bind[3].length= &t_length;
3680 
3681   rc= mysql_stmt_bind_result(stmt, my_bind);
3682   check_stmt_rc(rc, stmt);
3683 
3684   rc= mysql_stmt_fetch(stmt);
3685   check_stmt_rc(rc, stmt);
3686 
3687   FAIL_UNLESS(short_value == -5999, "sv != -5999");
3688   FAIL_UNLESS(s_length == 2, "s_length != 2");
3689 
3690   FAIL_UNLESS(long_value == -5999, "l_v != -5999");
3691   FAIL_UNLESS(l_length == 4, "l_length != 4");
3692 
3693   FAIL_UNLESS(longlong_value == 35999, "llv != 35999");
3694   FAIL_UNLESS(ll_length == 8, "ll_length != 8");
3695 
3696   FAIL_UNLESS(tiny_value == 200, "t_v != 200");
3697   FAIL_UNLESS(t_length == 1, "t_length != 1");
3698 
3699   rc= mysql_stmt_fetch(stmt);
3700   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
3701 
3702   mysql_stmt_close(stmt);
3703   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_sshort");
3704   check_mysql_rc(rc, mysql);
3705   return OK;
3706 }
3707 
3708 
3709 /* Test a misc tinyint-signed conversion bug */
3710 
test_stiny_bug(MYSQL * mysql)3711 static int test_stiny_bug(MYSQL *mysql)
3712 {
3713   MYSQL_STMT *stmt;
3714   MYSQL_BIND my_bind[4];
3715   short      short_value;
3716   int32      long_value;
3717   ulong      s_length, l_length, ll_length, t_length;
3718   ulonglong  longlong_value;
3719   int        rc;
3720   uchar      tiny_value;
3721 
3722   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_stiny");
3723   check_mysql_rc(rc, mysql);
3724 
3725   rc= mysql_query(mysql, "CREATE TABLE test_stiny(a tinyint signed, \
3726                                                   b tinyint signed, \
3727                                                   c tinyint unsigned, \
3728                                                   d tinyint unsigned)");
3729   check_mysql_rc(rc, mysql);
3730 
3731   rc= mysql_query(mysql, "INSERT INTO test_stiny VALUES(-128, -127, 255, 0)");
3732   check_mysql_rc(rc, mysql);
3733 
3734 
3735   stmt= mysql_stmt_init(mysql);
3736   FAIL_IF(!stmt, mysql_error(mysql));
3737   rc= mysql_stmt_prepare(stmt, SL("SELECT * FROM test_stiny"));
3738   check_stmt_rc(rc, stmt);
3739 
3740   rc= mysql_stmt_execute(stmt);
3741   check_stmt_rc(rc, stmt);
3742 
3743   memset(my_bind, '\0', sizeof(my_bind));
3744   my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
3745   my_bind[0].buffer= (void *)&short_value;
3746   my_bind[0].length= &s_length;
3747 
3748   my_bind[1].buffer_type= MYSQL_TYPE_LONG;
3749   my_bind[1].buffer= (void *)&long_value;
3750   my_bind[1].length= &l_length;
3751 
3752   my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
3753   my_bind[2].buffer= (void *)&longlong_value;
3754   my_bind[2].length= &ll_length;
3755 
3756   my_bind[3].buffer_type= MYSQL_TYPE_TINY;
3757   my_bind[3].buffer= (void *)&tiny_value;
3758   my_bind[3].length= &t_length;
3759 
3760   rc= mysql_stmt_bind_result(stmt, my_bind);
3761   check_stmt_rc(rc, stmt);
3762 
3763   rc= mysql_stmt_fetch(stmt);
3764   check_stmt_rc(rc, stmt);
3765 
3766   FAIL_UNLESS(short_value == -128, "s_v != -128");
3767   FAIL_UNLESS(s_length == 2, "s_length != 2");
3768 
3769   FAIL_UNLESS(long_value == -127, "l_v != -127");
3770   FAIL_UNLESS(l_length == 4, "l_length != 4");
3771 
3772   FAIL_UNLESS(longlong_value == 255, "llv != 255");
3773   FAIL_UNLESS(ll_length == 8, "ll_length != 8");
3774 
3775   FAIL_UNLESS(tiny_value == 0, "t_v != 0");
3776   FAIL_UNLESS(t_length == 1, "t_length != 1");
3777 
3778   rc= mysql_stmt_fetch(stmt);
3779   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
3780 
3781   mysql_stmt_close(stmt);
3782   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_stiny");
3783   check_mysql_rc(rc, mysql);
3784   return OK;
3785 }
3786 
test_bug53311(MYSQL * mysql)3787 static int test_bug53311(MYSQL *mysql)
3788 {
3789   int rc;
3790   MYSQL_STMT *stmt;
3791   int i;
3792   const char *query= "INSERT INTO bug53311 VALUES (1)";
3793   SKIP_MAXSCALE;
3794 
3795   rc= mysql_options(mysql, MYSQL_OPT_RECONNECT, "1");
3796   check_mysql_rc(rc, mysql);
3797 
3798   rc= mysql_query(mysql, "DROP TABLE IF EXISTS bug53311");
3799   check_mysql_rc(rc, mysql);
3800 
3801   rc= mysql_query(mysql, "CREATE TABLE bug53311 (a int)");
3802   check_mysql_rc(rc, mysql);
3803 
3804   stmt= mysql_stmt_init(mysql);
3805   rc= mysql_stmt_prepare(stmt, SL(query));
3806   check_stmt_rc(rc, stmt);
3807 
3808   for (i=0; i < 2; i++)
3809   {
3810     rc= mysql_stmt_execute(stmt);
3811     check_stmt_rc(rc, stmt);
3812   }
3813 
3814   /* kill connection */
3815   rc= mysql_kill(mysql, mysql_thread_id(mysql));
3816 
3817   rc= mysql_stmt_execute(stmt);
3818   FAIL_IF(rc == 0, "Error expected");
3819   FAIL_IF(mysql_stmt_errno(stmt) == 0, "Errno != 0 expected");
3820   rc= mysql_stmt_close(stmt);
3821   check_mysql_rc(rc, mysql);
3822   rc= mysql_query(mysql, "DROP TABLE IF EXISTS bug53311");
3823   check_mysql_rc(rc, mysql);
3824 
3825   return OK;
3826 }
3827 #define PREPARE_SQL "EXPLAIN SELECT t1.*, t2.* FROM test AS t1, test AS t2"
3828 
3829 #ifdef NOT_IN_USE
test_metadata(MYSQL * mysql)3830 static int test_metadata(MYSQL *mysql)
3831 {
3832   int rc;
3833 
3834 	rc= mysql_query(mysql, "DROP TABLE IF EXISTS test");
3835   check_mysql_rc(rc, mysql);
3836 	rc= mysql_query(mysql, "CREATE TABLE test(id INT, label CHAR(1), PRIMARY KEY(id)) ENGINE=MYISAM");
3837   check_mysql_rc(rc, mysql);
3838 
3839 	rc= mysql_query(mysql, "INSERT INTO test(id, label) VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f')");
3840   check_mysql_rc(rc, mysql);
3841 	printf("Client=%s\n", mysql_get_client_info());
3842 	printf("Server=%s\n", mysql_get_server_info(mysql));
3843 
3844 	{
3845 		MYSQL_STMT * stmt = mysql_stmt_init(mysql);
3846 		if (!stmt) {
3847 			fprintf(stderr, "Failed to init stmt: Error: %s\n", mysql_error(mysql));
3848 			goto end;
3849 		}
3850 		if (mysql_stmt_prepare(stmt, PREPARE_SQL, sizeof(PREPARE_SQL) - 1)) {
3851 			fprintf(stderr, "Failed to prepare stmt: Error: %s\n", mysql_stmt_error(stmt));
3852 			goto end2;
3853 		}
3854 		if (mysql_stmt_execute(stmt)) {
3855 			fprintf(stderr, "Failed to execute stmt: Error: %s\n", mysql_stmt_error(stmt));
3856 			goto end2;
3857 		}
3858 		{
3859 			MYSQL_FIELD * field = NULL;
3860 			MYSQL_RES * res = mysql_stmt_result_metadata(stmt);
3861 			if (!res) {
3862 				fprintf(stderr, "Failed to get metadata: Error: %s\n", mysql_stmt_error(stmt));
3863 				goto end2;
3864 			}
3865 			while ((field = mysql_fetch_field(res))) {
3866 				printf("name=%s\n", field->name);
3867 				printf("catalog=%s\n", field->catalog);
3868 			}
3869 			mysql_free_result(res);
3870 
3871 		}
3872 end2:
3873 		mysql_stmt_close(stmt);
3874 	}
3875 end:
3876 	return 0;
3877 }
3878 #endif
3879 
test_conc_5(MYSQL * mysql)3880 static int test_conc_5(MYSQL *mysql)
3881 {
3882   const char *query= "SELECT a FROM t1";
3883   MYSQL_RES *res;
3884   MYSQL_STMT *stmt;
3885   MYSQL_FIELD *fields;
3886   int rc;
3887 
3888   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
3889   check_mysql_rc(rc, mysql);
3890   rc= mysql_query(mysql, "CREATE TABLE t1 (a int)");
3891   check_mysql_rc(rc, mysql);
3892   rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1)");
3893   check_mysql_rc(rc, mysql);
3894 
3895   stmt= mysql_stmt_init(mysql);
3896   FAIL_IF(!stmt, "couldn't allocate memory");
3897 
3898   rc= mysql_stmt_prepare(stmt, SL(query));
3899   check_stmt_rc(rc, stmt);
3900   rc= mysql_stmt_execute(stmt);
3901   check_stmt_rc(rc, stmt);
3902 
3903   res= mysql_stmt_result_metadata(stmt);
3904   FAIL_IF(!res, "Can't obtain resultset");
3905 
3906   fields= mysql_fetch_fields(res);
3907   FAIL_IF(!fields, "Can't obtain fields");
3908 
3909   FAIL_IF(strcmp("def", fields[0].catalog), "unexpected value for field->catalog");
3910 
3911   mysql_free_result(res);
3912   mysql_stmt_close(stmt);
3913   return OK;
3914 }
3915 
test_conc141(MYSQL * mysql)3916 static int test_conc141(MYSQL *mysql)
3917 {
3918   int rc;
3919   const char *query= "CALL p_conc141";
3920   MYSQL_STMT *stmt= mysql_stmt_init(mysql);
3921 
3922   rc= mysql_query(mysql, "DROP TABLE IF EXISTS conc141");
3923   check_mysql_rc(rc, mysql);
3924   rc= mysql_query(mysql, "CREATE TABLE conc141 (KeyVal int not null primary key)");
3925   check_mysql_rc(rc, mysql);
3926   rc= mysql_query(mysql, "INSERT INTO conc141 VALUES(1)");
3927   check_mysql_rc(rc, mysql);
3928   rc= mysql_query(mysql, "DROP PROCEDURE IF EXISTS p_conc141");
3929   check_mysql_rc(rc, mysql);
3930   rc= mysql_query(mysql, "CREATE PROCEDURE p_conc141()\n"
3931                    "BEGIN\n"
3932                      "select * from conc141;\n"
3933                      "insert into conc141(KeyVal) VALUES(1);\n"
3934                    "END");
3935   check_mysql_rc(rc, mysql);
3936 
3937   rc= mysql_stmt_prepare(stmt, SL(query));
3938   check_stmt_rc(rc, stmt);
3939 
3940   rc= mysql_stmt_execute(stmt);
3941   check_stmt_rc(rc, stmt);
3942   /* skip first result */
3943   rc= mysql_stmt_next_result(stmt);
3944   FAIL_IF(rc==-1, "No more results and error expected");
3945   mysql_stmt_free_result(stmt);
3946   FAIL_IF(mysql_stmt_errno(stmt), "No Error expected");
3947   rc= mysql_stmt_execute(stmt);
3948   check_stmt_rc(rc, stmt);
3949   mysql_stmt_close(stmt);
3950   rc= mysql_query(mysql, "DROP TABLE IF EXISTS conc141");
3951   check_mysql_rc(rc, mysql);
3952   rc= mysql_query(mysql, "DROP PROCEDURE IF EXISTS p_conc141");
3953   check_mysql_rc(rc, mysql);
3954   return OK;
3955 }
3956 
test_conc154(MYSQL * mysql)3957 static int test_conc154(MYSQL *mysql)
3958 {
3959   MYSQL_STMT *stmt;
3960   const char *stmtstr= "SELECT * FROM t1";
3961   int rc;
3962 
3963   /* 1st: empty result set without free_result */
3964   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
3965   check_mysql_rc(rc, mysql);
3966   rc= mysql_query(mysql, "CREATE TABLE t1 (a varchar(20))");
3967   check_mysql_rc(rc, mysql);
3968 
3969   stmt= mysql_stmt_init(mysql);
3970   rc= mysql_stmt_prepare(stmt, SL(stmtstr));
3971   check_stmt_rc(rc, stmt);
3972 
3973   rc= mysql_stmt_execute(stmt);
3974   check_stmt_rc(rc, stmt);
3975 
3976   rc= mysql_stmt_store_result(stmt);
3977   check_stmt_rc(rc, stmt);
3978 
3979   rc= mysql_stmt_execute(stmt);
3980   check_stmt_rc(rc, stmt);
3981 
3982   rc= mysql_stmt_store_result(stmt);
3983   check_stmt_rc(rc, stmt);
3984 
3985   mysql_stmt_close(stmt);
3986 
3987   /* 2nd: empty result set with free_result */
3988   stmt= mysql_stmt_init(mysql);
3989   rc= mysql_stmt_prepare(stmt, SL(stmtstr));
3990   check_stmt_rc(rc, stmt);
3991 
3992   rc= mysql_stmt_execute(stmt);
3993   check_stmt_rc(rc, stmt);
3994 
3995   rc= mysql_stmt_store_result(stmt);
3996   check_stmt_rc(rc, stmt);
3997 
3998   rc= mysql_stmt_free_result(stmt);
3999   check_stmt_rc(rc, stmt);
4000 
4001   rc= mysql_stmt_execute(stmt);
4002   check_stmt_rc(rc, stmt);
4003 
4004   rc= mysql_stmt_store_result(stmt);
4005   check_stmt_rc(rc, stmt);
4006   rc= mysql_stmt_free_result(stmt);
4007   check_stmt_rc(rc, stmt);
4008 
4009   mysql_stmt_close(stmt);
4010 
4011   /* 3rd: non empty result without free_result */
4012   rc= mysql_query(mysql, "INSERT INTO t1 VALUES ('test_conc154')");
4013   check_mysql_rc(rc, mysql);
4014 
4015   stmt= mysql_stmt_init(mysql);
4016   rc= mysql_stmt_prepare(stmt, SL(stmtstr));
4017   check_stmt_rc(rc, stmt);
4018 
4019   rc= mysql_stmt_execute(stmt);
4020   check_stmt_rc(rc, stmt);
4021 
4022   rc= mysql_stmt_store_result(stmt);
4023   check_stmt_rc(rc, stmt);
4024 
4025   rc= mysql_stmt_execute(stmt);
4026   check_stmt_rc(rc, stmt);
4027 
4028   rc= mysql_stmt_store_result(stmt);
4029   check_stmt_rc(rc, stmt);
4030 
4031   mysql_stmt_close(stmt);
4032 
4033   /* 4th non empty result set with free_result */
4034   stmt= mysql_stmt_init(mysql);
4035   rc= mysql_stmt_prepare(stmt, SL(stmtstr));
4036   check_stmt_rc(rc, stmt);
4037 
4038   rc= mysql_stmt_execute(stmt);
4039   check_stmt_rc(rc, stmt);
4040 
4041   rc= mysql_stmt_store_result(stmt);
4042   check_stmt_rc(rc, stmt);
4043 
4044   rc= mysql_stmt_free_result(stmt);
4045   check_stmt_rc(rc, stmt);
4046 
4047   rc= mysql_stmt_execute(stmt);
4048   check_stmt_rc(rc, stmt);
4049 
4050   rc= mysql_stmt_store_result(stmt);
4051   check_stmt_rc(rc, stmt);
4052   rc= mysql_stmt_free_result(stmt);
4053   check_stmt_rc(rc, stmt);
4054 
4055   mysql_stmt_close(stmt);
4056   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4057   check_mysql_rc(rc, mysql);
4058 
4059   return OK;
4060 }
4061 
test_conc155(MYSQL * mysql)4062 static int test_conc155(MYSQL *mysql)
4063 {
4064   MYSQL_STMT *stmt;
4065   MYSQL_BIND bind;
4066   char buffer[50];
4067   int rc;
4068 
4069   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4070   check_mysql_rc(rc, mysql);
4071   rc= mysql_query(mysql, "CREATE TABLE t1 (a TEXT)");
4072   check_mysql_rc(rc, mysql);
4073   rc= mysql_query(mysql, "INSERT INTO t1 VALUES ('zero terminated string')");
4074   check_mysql_rc(rc, mysql);
4075 
4076   stmt= mysql_stmt_init(mysql);
4077   rc= mysql_stmt_prepare(stmt, SL("SELECT a FROM t1"));
4078   check_stmt_rc(rc, stmt);
4079 
4080   rc= mysql_stmt_execute(stmt);
4081   check_stmt_rc(rc, stmt);
4082 
4083   memset(buffer, 'X', 50);
4084   memset(&bind, 0, sizeof(MYSQL_BIND));
4085 
4086   bind.buffer= buffer;
4087   bind.buffer_length= 50;
4088   bind.buffer_type= MYSQL_TYPE_STRING;
4089 
4090   rc= mysql_stmt_bind_result(stmt, &bind);
4091   check_stmt_rc(rc, stmt);
4092 
4093   rc= mysql_stmt_fetch(stmt);
4094   check_stmt_rc(rc, stmt);
4095 
4096   if (strlen(buffer) != strlen("zero terminated string"))
4097   {
4098     diag("Wrong buffer length");
4099     return FAIL;
4100   }
4101 
4102   mysql_stmt_close(stmt);
4103   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4104   check_mysql_rc(rc, mysql);
4105   return OK;
4106 }
4107 
test_conc168(MYSQL * mysql)4108 static int test_conc168(MYSQL *mysql)
4109 {
4110   MYSQL_STMT *stmt= mysql_stmt_init(mysql);
4111 
4112   MYSQL_BIND bind;
4113   char buffer[100];
4114   int rc;
4115 
4116   rc= mysql_query(mysql, "DROP TABLE IF EXISTS conc168");
4117   check_mysql_rc(rc, mysql);
4118   rc= mysql_query(mysql, "CREATE TABLE conc168(a datetime(3))");
4119   check_mysql_rc(rc, mysql);
4120   rc= mysql_query(mysql, "INSERT INTO conc168 VALUES ('2016-03-09 07:51:49.000'),('2016-03-09 07:51:49.001'),('2016-03-09 07:51:49.010')");
4121   check_mysql_rc(rc, mysql);
4122 
4123   memset(&bind, 0, sizeof(MYSQL_BIND));
4124   bind.buffer= buffer;
4125   bind.buffer_type= MYSQL_TYPE_STRING;
4126   bind.buffer_length= 100;
4127 
4128   rc= mysql_stmt_prepare(stmt, SL("SELECT a FROM conc168"));
4129   check_stmt_rc(rc, stmt);
4130 
4131   rc= mysql_stmt_execute(stmt);
4132   check_stmt_rc(rc, stmt);
4133 
4134   rc= mysql_stmt_bind_result(stmt, &bind);
4135   check_stmt_rc(rc, stmt);
4136 
4137   rc= mysql_stmt_fetch(stmt);
4138   check_stmt_rc(rc, stmt);
4139   FAIL_IF(strcmp(buffer, "2016-03-09 07:51:49.000"), "expected: 2016-03-09 07:51:49.000");
4140 
4141   rc= mysql_stmt_fetch(stmt);
4142   check_stmt_rc(rc, stmt);
4143   FAIL_IF(strcmp(buffer, "2016-03-09 07:51:49.001"), "expected: 2016-03-09 07:51:49.001");
4144 
4145   rc= mysql_stmt_fetch(stmt);
4146   check_stmt_rc(rc, stmt);
4147   FAIL_IF(strcmp(buffer, "2016-03-09 07:51:49.010"), "expected: 2016-03-09 07:51:49.010");
4148 
4149   mysql_stmt_close(stmt);
4150   rc= mysql_query(mysql, "DROP TABLE IF EXISTS conc168");
4151   check_mysql_rc(rc, mysql);
4152   return OK;
4153 }
4154 
test_conc167(MYSQL * mysql)4155 static int test_conc167(MYSQL *mysql)
4156 {
4157   MYSQL_STMT *stmt= mysql_stmt_init(mysql);
4158 
4159   MYSQL_BIND bind[3];
4160   char buffer[100];
4161   int bit1=0, bit2=0;
4162   int rc;
4163   const char *stmt_str= "SELECT a,b,c FROM conc168";
4164 
4165   rc= mysql_query(mysql, "DROP TABLE IF EXISTS conc168");
4166   check_mysql_rc(rc, mysql);
4167   rc= mysql_query(mysql, "CREATE TABLE conc168(a bit, b bit, c varchar(10))");
4168   check_mysql_rc(rc, mysql);
4169   rc= mysql_query(mysql, "INSERT INTO conc168 VALUES (1,0, 'test12345')");
4170   check_mysql_rc(rc, mysql);
4171 
4172   memset(bind, 0, 3 * sizeof(MYSQL_BIND));
4173   bind[0].buffer= &bit1;
4174   bind[0].buffer_type= MYSQL_TYPE_BIT;
4175   bind[0].buffer_length= sizeof(int);
4176   bind[1].buffer= &bit2;
4177   bind[1].buffer_type= MYSQL_TYPE_BIT;
4178   bind[1].buffer_length= sizeof(int);
4179   bind[2].buffer= buffer;
4180   bind[2].buffer_type= MYSQL_TYPE_STRING;
4181   bind[2].buffer_length= 100;
4182 
4183   rc= mysql_stmt_prepare(stmt, SL(stmt_str));
4184   check_stmt_rc(rc, stmt);
4185 
4186   rc= mysql_stmt_execute(stmt);
4187   check_stmt_rc(rc, stmt);
4188 
4189   rc= mysql_stmt_bind_result(stmt, bind);
4190   check_stmt_rc(rc, stmt);
4191 
4192   rc= mysql_stmt_store_result(stmt);
4193   check_stmt_rc(rc, stmt);
4194 
4195   rc= mysql_stmt_fetch(stmt);
4196   check_stmt_rc(rc, stmt);
4197 
4198   diag("bit=%d %d char=%s", bit1, bit2, buffer);
4199 
4200   mysql_stmt_close(stmt);
4201   return OK;
4202 }
4203 
test_conc177(MYSQL * mysql)4204 static int test_conc177(MYSQL *mysql)
4205 {
4206   MYSQL_STMT *stmt;
4207   int rc;
4208   MYSQL_BIND bind[2];
4209   const char *stmt_str= "SELECT a,b FROM t1";
4210   char buf1[128], buf2[128];
4211 
4212   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4213   check_mysql_rc(rc, mysql);
4214 
4215   rc= mysql_query(mysql, "CREATE TABLE t1 (a double zerofill default 8.8,b float zerofill default 8.8)");
4216   check_mysql_rc(rc, mysql);
4217   rc= mysql_query(mysql, "INSERT INTO t1 VALUES (DEFAULT, DEFAULT)");
4218   check_mysql_rc(rc, mysql);
4219 
4220   stmt= mysql_stmt_init(mysql);
4221   rc= mysql_stmt_prepare(stmt, SL(stmt_str));
4222   check_stmt_rc(rc, stmt);
4223   rc= mysql_stmt_execute(stmt);
4224   check_stmt_rc(rc, stmt);
4225 
4226   memset(bind, 0, 2 * sizeof(MYSQL_BIND));
4227   bind[0].buffer= &buf1;
4228   bind[0].buffer_type= MYSQL_TYPE_STRING;
4229   bind[0].buffer_length= 128;
4230   bind[1].buffer= &buf2;
4231   bind[1].buffer_type= MYSQL_TYPE_STRING;
4232   bind[1].buffer_length= 128;
4233 
4234   rc= mysql_stmt_bind_result(stmt, bind);
4235   check_stmt_rc(rc, stmt);
4236 
4237   rc= mysql_stmt_fetch(stmt);
4238   mysql_stmt_close(stmt);
4239 
4240   diag("buf1 %s\nbuf2 %s", buf1, buf2);
4241 
4242   FAIL_IF(strcmp(buf1, "00000000000000000008.8"), "Expected 00000000000000000008.8");
4243   FAIL_IF(strcmp(buf2, "0000000008.8"), "Expected 0000000008.8");
4244 
4245   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4246   check_mysql_rc(rc, mysql);
4247 
4248   rc= mysql_query(mysql, "CREATE TABLE t1 (a int(8) zerofill default 1, b int(4) zerofill default 1)");
4249   check_mysql_rc(rc, mysql);
4250   rc= mysql_query(mysql, "INSERT INTO t1 VALUES (DEFAULT, DEFAULT)");
4251   check_mysql_rc(rc, mysql);
4252 
4253   stmt= mysql_stmt_init(mysql);
4254   rc= mysql_stmt_prepare(stmt, SL(stmt_str));
4255   check_stmt_rc(rc, stmt);
4256   rc= mysql_stmt_execute(stmt);
4257   check_stmt_rc(rc, stmt);
4258 
4259   memset(bind, 0, 2 * sizeof(MYSQL_BIND));
4260   bind[0].buffer= &buf1;
4261   bind[0].buffer_type= MYSQL_TYPE_STRING;
4262   bind[0].buffer_length= 128;
4263   bind[1].buffer= &buf2;
4264   bind[1].buffer_type= MYSQL_TYPE_STRING;
4265   bind[1].buffer_length= 128;
4266 
4267   rc= mysql_stmt_bind_result(stmt, bind);
4268   check_stmt_rc(rc, stmt);
4269 
4270   rc= mysql_stmt_fetch(stmt);
4271   mysql_stmt_close(stmt);
4272 
4273   diag("buf1 %s\nbuf2 %s", buf1, buf2);
4274 
4275   FAIL_IF(strcmp(buf1, "00000001"), "Expected 00000001");
4276   FAIL_IF(strcmp(buf2, "0001"), "Expected 0001");
4277   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4278   check_mysql_rc(rc, mysql);
4279   return OK;
4280 }
4281 
test_conc179(MYSQL * mysql)4282 static int test_conc179(MYSQL *mysql)
4283 {
4284   MYSQL_STMT *stmt;
4285   int rc;
4286   const char *stmtstr= "select 1 as ' '";
4287 
4288   stmt= mysql_stmt_init(mysql);
4289   rc= mysql_stmt_prepare(stmt, SL(stmtstr));
4290   check_stmt_rc(rc, stmt);
4291 
4292   if (mysql_get_server_version(mysql) >= 100100)
4293   {
4294     FAIL_IF(mysql_warning_count(mysql) < 1, "expected 1 or more warnings");
4295     FAIL_IF(mysql_stmt_warning_count(stmt) < 1, "expected 1 or more warnings");
4296   }
4297 
4298   mysql_stmt_close(stmt);
4299   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4300   check_mysql_rc(rc, mysql);
4301 
4302   return OK;
4303 }
4304 
test_conc182(MYSQL * mysql)4305 static int test_conc182(MYSQL *mysql)
4306 {
4307   MYSQL_STMT *stmt;
4308   int rc;
4309   MYSQL_BIND bind[2];
4310   char buf1[22];
4311   MYSQL_RES *result;
4312   MYSQL_ROW row;
4313 
4314   stmt= mysql_stmt_init(mysql);
4315   rc= mariadb_stmt_execute_direct(stmt, "DROP TABLE IF EXISTS t1", -1);
4316   check_stmt_rc(rc, stmt);
4317   rc= mariadb_stmt_execute_direct(stmt, "DROP TABLE IF EXISTS t1", -1);
4318   check_stmt_rc(rc, stmt);
4319   rc= mariadb_stmt_execute_direct(stmt, "SELECT 1", -1);
4320   check_stmt_rc(rc, stmt);
4321   rc= mariadb_stmt_execute_direct(stmt, "SELECT 1", -1);
4322   check_stmt_rc(rc, stmt);
4323 
4324   rc= mysql_stmt_close(stmt);
4325   check_stmt_rc(rc, stmt);
4326 
4327   rc= mysql_query(mysql, "SELECT row_count()");
4328   result= mysql_store_result(mysql);
4329   row= mysql_fetch_row(result);
4330   diag("buf: %s", row[0]);
4331   mysql_free_result(result);
4332 
4333 
4334   stmt= mysql_stmt_init(mysql);
4335   rc= mysql_stmt_prepare(stmt, "SELECT row_count()", -1);
4336   check_stmt_rc(rc, stmt);
4337   rc= mysql_stmt_execute(stmt);
4338 
4339   memset(bind, 0, 2 * sizeof(MYSQL_BIND));
4340   bind[0].buffer= &buf1;
4341   bind[0].buffer_length= bind[1].buffer_length= 20;
4342   bind[0].buffer_type= bind[1].buffer_type= MYSQL_TYPE_STRING;
4343 
4344   rc= mysql_stmt_bind_result(stmt, bind);
4345 
4346   while(!mysql_stmt_fetch(stmt))
4347   diag("b1: %s", buf1);
4348   rc= mysql_stmt_close(stmt);
4349   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4350   check_mysql_rc(rc, mysql);
4351   return OK;
4352 }
4353 
test_conc181(MYSQL * mysql)4354 static int test_conc181(MYSQL *mysql)
4355 {
4356   MYSQL_STMT *stmt;
4357   int rc;
4358   MYSQL_BIND bind;
4359   const char *stmt_str= "SELECT a FROM t1";
4360   float f=1;
4361   my_bool err= 0;
4362 
4363   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4364   check_mysql_rc(rc, mysql);
4365   rc= mysql_query(mysql, "CREATE TABLE t1 (a int)");
4366   check_mysql_rc(rc, mysql);
4367   rc= mysql_query(mysql, "INSERT INTO t1 VALUES(1073741825)");
4368   check_mysql_rc(rc, mysql);
4369 
4370   stmt= mysql_stmt_init(mysql);
4371   rc= mysql_stmt_prepare(stmt, SL(stmt_str));
4372   check_stmt_rc(rc, stmt);
4373 
4374   rc= mysql_stmt_execute(stmt);
4375   check_stmt_rc(rc, stmt);
4376 
4377   memset(&bind, 0, sizeof(MYSQL_BIND));
4378   bind.buffer= &f;
4379   bind.error= &err;
4380   bind.buffer_type= MYSQL_TYPE_FLOAT;
4381   rc= mysql_stmt_bind_result(stmt, &bind);
4382   check_stmt_rc(rc, stmt);
4383 
4384   rc= mysql_stmt_fetch(stmt);
4385   diag("rc=%d err=%d float=%f, %d", rc, err, f, MYSQL_DATA_TRUNCATED);
4386 
4387   rc= mysql_stmt_close(stmt);
4388   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4389   check_mysql_rc(rc, mysql);
4390   return OK;
4391 }
4392 
test_conc198(MYSQL * mysql)4393 static int test_conc198(MYSQL *mysql)
4394 {
4395   MYSQL_STMT *stmt1, *stmt2;
4396   MYSQL_BIND my_bind[1];
4397   int32 a;
4398   int rc;
4399   int num_rows= 0;
4400   ulong type;
4401   ulong prefetch_rows= 3;
4402 
4403 
4404   mysql_query(mysql, "drop table if exists t1");
4405   mysql_query(mysql, "create table t1 (id integer not null primary key)");
4406   rc= mysql_query(mysql, "insert into t1 (id) values "
4407                          " (1), (2), (3), (4), (5), (6), (7), (8), (9)");
4408   check_mysql_rc(rc, mysql);
4409 
4410   stmt1= mysql_stmt_init(mysql);
4411   stmt2= mysql_stmt_init(mysql);
4412   /* Not implemented in 5.0 */
4413   type= (ulong) CURSOR_TYPE_SCROLLABLE;
4414   rc= mysql_stmt_attr_set(stmt1, STMT_ATTR_CURSOR_TYPE, (void*) &type);
4415   FAIL_UNLESS(rc, "Error expected");
4416   rc= mysql_stmt_attr_set(stmt2, STMT_ATTR_CURSOR_TYPE, (void*) &type);
4417   FAIL_UNLESS(rc, "Error expected");
4418 
4419   type= (ulong) CURSOR_TYPE_READ_ONLY;
4420   rc= mysql_stmt_attr_set(stmt1, STMT_ATTR_CURSOR_TYPE, (void*) &type);
4421   check_stmt_rc(rc, stmt1);
4422   rc= mysql_stmt_attr_set(stmt2, STMT_ATTR_CURSOR_TYPE, (void*) &type);
4423   check_stmt_rc(rc, stmt2);
4424   rc= mysql_stmt_attr_set(stmt1, STMT_ATTR_PREFETCH_ROWS,
4425                           (void*) &prefetch_rows);
4426   check_stmt_rc(rc, stmt1);
4427   rc= mysql_stmt_attr_set(stmt2, STMT_ATTR_PREFETCH_ROWS,
4428                           (void*) &prefetch_rows);
4429   check_stmt_rc(rc, stmt2);
4430   rc= mysql_stmt_prepare(stmt1, "SELECT * FROM t1 ORDER by id ASC" , -1);
4431   check_stmt_rc(rc, stmt1);
4432   rc= mysql_stmt_prepare(stmt2, "SELECT * FROM t1 ORDER by id DESC", -1);
4433   check_stmt_rc(rc, stmt2);
4434 
4435   rc= mysql_stmt_execute(stmt1);
4436   check_stmt_rc(rc, stmt1);
4437   rc= mysql_stmt_execute(stmt2);
4438   check_stmt_rc(rc, stmt2);
4439 
4440   memset(my_bind, '\0', sizeof(my_bind));
4441   my_bind[0].buffer_type= MYSQL_TYPE_LONG;
4442   my_bind[0].buffer= (void*) &a;
4443   my_bind[0].buffer_length= sizeof(a);
4444   mysql_stmt_bind_result(stmt1, my_bind);
4445   mysql_stmt_bind_result(stmt2, my_bind);
4446 
4447   while ((rc= mysql_stmt_fetch(stmt1)) == 0)
4448     ++num_rows;
4449   FAIL_UNLESS(num_rows == 9, "num_rows != 9");
4450 
4451   num_rows= 0;
4452   while ((rc= mysql_stmt_fetch(stmt2)) == 0)
4453     ++num_rows;
4454   FAIL_UNLESS(num_rows == 9, "num_rows != 9");
4455 
4456   rc= mysql_stmt_close(stmt1);
4457   rc= mysql_stmt_close(stmt2);
4458   FAIL_UNLESS(rc == 0, "");
4459 
4460   rc= mysql_query(mysql, "drop table t1");
4461   check_mysql_rc(rc, mysql);
4462   return OK;
4463 }
4464 
test_conc205(MYSQL * mysql)4465 static int test_conc205(MYSQL *mysql)
4466 {
4467   MYSQL_STMT *stmt;
4468   MYSQL_BIND my_bind[3];
4469   char       data[8];
4470   ulong      length[3];
4471   int        rc, int_col;
4472   short      smint_col;
4473   my_bool    is_null[3];
4474   const char *query = "SELECT text_col, smint_col, int_col FROM test_conc205";
4475 
4476   rc= mysql_query(mysql, "drop table if exists test_conc205");
4477   check_mysql_rc(rc, mysql);
4478   rc= mysql_query(mysql, "CREATE TABLE test_conc205 (text_col TEXT, smint_col SMALLINT, int_col INT)");
4479   check_mysql_rc(rc, mysql);
4480   rc= mysql_query(mysql, "INSERT INTO test_conc205 VALUES('data01', 21893, 1718038908), ('data2', -25734, -1857802040)");
4481   check_mysql_rc(rc, mysql);
4482 
4483   stmt= mysql_stmt_init(mysql);
4484   FAIL_IF(!stmt, mysql_error(mysql));
4485 
4486   rc= mysql_stmt_prepare(stmt, SL(query));
4487   check_stmt_rc(rc, stmt);
4488 
4489   memset(my_bind, '\0', sizeof(my_bind));
4490   my_bind[0].buffer_type= MYSQL_TYPE_STRING;
4491   my_bind[0].buffer= (void *)data;
4492   my_bind[0].buffer_length= sizeof(data);
4493   my_bind[0].is_null= &is_null[0];
4494   my_bind[0].length= &length[0];
4495 
4496   my_bind[1].buffer_type= MYSQL_TYPE_SHORT;
4497   my_bind[1].buffer= &smint_col;
4498   my_bind[1].buffer_length= 2;
4499   my_bind[1].is_null= &is_null[1];
4500   my_bind[1].length= &length[1];
4501 
4502   my_bind[2].buffer_type= MYSQL_TYPE_LONG;
4503   my_bind[2].buffer= &int_col;
4504   my_bind[2].buffer_length= 4;
4505   my_bind[2].is_null= &is_null[2];
4506   my_bind[2].length= &length[2];
4507 
4508   rc= mysql_stmt_execute(stmt);
4509   check_stmt_rc(rc, stmt);
4510 
4511   rc= mysql_stmt_bind_result(stmt, my_bind);
4512   check_stmt_rc(rc, stmt);
4513 
4514   rc= mysql_stmt_fetch(stmt);
4515   check_stmt_rc(rc, stmt);
4516 
4517   FAIL_IF(length[0] != 6, "Wrong fetched string length");
4518   FAIL_IF(length[1] != 2, "Wrong fetched short length");
4519   FAIL_IF(length[2] != 4, "Wrong fetched int length");
4520 
4521   FAIL_IF(strncmp(data, "data01", length[0] + 1) != 0, "Wrong string value");
4522   FAIL_IF(smint_col != 21893, "Expected 21893");
4523   FAIL_IF(int_col != 1718038908, "Expected 1718038908");
4524 
4525   rc= mysql_stmt_fetch(stmt);
4526   check_stmt_rc(rc, stmt);
4527 
4528   FAIL_IF(length[0] != 5, "Wrong fetched string length");
4529   FAIL_IF(length[1] != 2, "Wrong fetched short length");
4530   FAIL_IF(length[2] != 4, "Wrong fetched int length");
4531 
4532   FAIL_IF(strncmp(data, "data2", length[0] + 1) != 0, "Wrong string value");
4533   FAIL_IF(smint_col != -25734, "Expected -25734");
4534   FAIL_IF(int_col != -1857802040, "Expected -1857802040");
4535 
4536   rc= mysql_stmt_fetch(stmt);
4537   FAIL_IF(rc != MYSQL_NO_DATA, "Expected MYSQL_NO_DATA");
4538 
4539   mysql_stmt_close(stmt);
4540 
4541   rc= mysql_query(mysql, "drop table test_conc205");
4542   check_mysql_rc(rc, mysql);
4543 
4544   return OK;
4545 }
4546 
test_conc217(MYSQL * mysql)4547 static int test_conc217(MYSQL *mysql)
4548 {
4549   MYSQL_STMT *stmt= mysql_stmt_init(mysql);
4550   int rc;
4551 
4552   SKIP_MAXSCALE;
4553   rc= mariadb_stmt_execute_direct(stmt, "SELECT 1 FROM nonexisting_table", -1);
4554   FAIL_IF(rc==0, "Expected error\n");
4555   rc= mysql_query(mysql, "drop table if exists t_count");
4556   check_mysql_rc(rc, mysql);
4557   mysql_stmt_close(stmt);
4558   check_mysql_rc(rc, mysql);
4559   return OK;
4560 }
4561 
test_conc208(MYSQL * mysql)4562 static int test_conc208(MYSQL *mysql)
4563 {
4564   MYSQL_STMT *stmt= mysql_stmt_init(mysql);
4565   int rc;
4566   int data;
4567   MYSQL_BIND bind;
4568 
4569   rc= mysql_stmt_prepare(stmt, "SELECT \"100\" UNION SELECT \"88\" UNION SELECT \"389789\"", -1);
4570   check_stmt_rc(rc, stmt);
4571 
4572   memset(&bind, 0, sizeof(MYSQL_BIND));
4573   bind.buffer_type= MYSQL_TYPE_LONG;
4574   bind.buffer= (void *)&data;
4575 
4576   rc= mysql_stmt_execute(stmt);
4577   check_stmt_rc(rc, stmt);
4578 
4579   rc= mysql_stmt_bind_result(stmt, &bind);
4580 
4581   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
4582   {
4583     diag("data=%d", data);
4584     FAIL_IF(data != 100 && data != 88 && data != 389789, "Wrong value");
4585   }
4586   mysql_stmt_close(stmt);
4587   return OK;
4588 }
4589 
test_mdev14165(MYSQL * mysql)4590 static int test_mdev14165(MYSQL *mysql)
4591 {
4592   int rc;
4593   MYSQL_STMT *stmt= mysql_stmt_init(mysql);
4594   MYSQL_FIELD *fields;
4595   MYSQL_RES *result;
4596   my_bool val= 1;
4597   MYSQL_BIND bind[1];
4598   char buf1[52];
4599 
4600   rc= mysql_options(mysql, MYSQL_REPORT_DATA_TRUNCATION, &val);
4601 
4602   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4603   rc= mysql_query(mysql, "CREATE TABLE t1 (i INT(20) ZEROFILL)");
4604   check_mysql_rc(rc, mysql);
4605   rc= mysql_query(mysql, "INSERT INTO t1 VALUES (2),(1)");
4606   check_mysql_rc(rc, mysql);
4607   rc= mysql_stmt_prepare(stmt, "SELECT i FROM t1", -1);
4608   check_stmt_rc(rc, stmt);
4609 
4610   rc= mysql_stmt_execute(stmt);
4611   check_stmt_rc(rc, stmt);
4612 
4613   memset(bind, 0, sizeof(MYSQL_BIND));
4614   bind[0].buffer_type= MYSQL_TYPE_STRING;
4615   bind[0].buffer_length= 51;
4616   bind[0].buffer= buf1;
4617 
4618   mysql_stmt_bind_result(stmt, bind);
4619 
4620   rc= mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &val);
4621   check_stmt_rc(rc, stmt);
4622   rc= mysql_stmt_store_result(stmt);
4623   check_stmt_rc(rc, stmt);
4624 
4625   result= mysql_stmt_result_metadata(stmt);
4626 
4627   fields= mysql_fetch_fields(result);
4628 
4629   FAIL_IF(fields[0].length < 20, "Expected length=20");
4630   FAIL_IF(fields[0].max_length < 20, "Expected max_length=20");
4631 
4632   mysql_stmt_fetch(stmt);
4633 
4634   FAIL_UNLESS(strcmp(buf1, "00000000000000000002") == 0, "Wrong result");
4635   mysql_free_result(result);
4636 
4637   mysql_stmt_close(stmt);
4638 
4639   rc= mysql_query(mysql, "DROP TABLE t1");
4640   check_mysql_rc(rc, mysql);
4641   return OK;
4642 }
4643 
test_compress(MYSQL * mysql)4644 static int test_compress(MYSQL *mysql)
4645 {
4646   MYSQL_STMT *stmt= mysql_stmt_init(mysql);
4647   int rc;
4648 
4649   rc= mariadb_stmt_execute_direct(stmt, SL("SELECT 1 FROM DUAL"));
4650   check_stmt_rc(rc, stmt);
4651 
4652   mysql_stmt_close(stmt);
4653 
4654   return OK;
4655 }
4656 
equal_MYSQL_TIME(MYSQL_TIME * tm1,MYSQL_TIME * tm2)4657 static int equal_MYSQL_TIME(MYSQL_TIME *tm1, MYSQL_TIME *tm2)
4658 {
4659   return tm1->day==tm2->day && tm1->hour==tm2->hour && tm1->minute==tm2->minute &&
4660     tm1->month==tm2->month && tm1->neg==tm2->neg && tm1->second==tm2->second &&
4661     tm1->second_part==tm2->second_part && tm1->time_type==tm2->time_type && tm1->year==tm2->year;
4662 }
4663 
test_str_to_int(MYSQL * mysql)4664 static int test_str_to_int(MYSQL *mysql)
4665 {
4666  int i;
4667  struct st_atoi_test{
4668     const char *str_value;
4669     int int_value;
4670     int rc;
4671   } atoi_tests[]=
4672   {
4673     {"0", 0, 0},
4674     {" 1",1, 0},
4675     {"123 ",123, 0},
4676     {"10.2",10, MYSQL_DATA_TRUNCATED},
4677     {"a", 0, MYSQL_DATA_TRUNCATED},
4678     {"1 2 3", 1, MYSQL_DATA_TRUNCATED},
4679     {NULL, 0, 0}
4680   };
4681 
4682   for(i=0; atoi_tests[i].str_value; i++)
4683   {
4684     int rc;
4685     MYSQL_STMT *stmt;
4686     MYSQL_BIND bind[1];
4687     struct st_atoi_test *test= &atoi_tests[i];
4688     char sql[256];
4689     int int_value;
4690 
4691     snprintf(sql, sizeof(sql), "SELECT '%s'",test->str_value);
4692 
4693     stmt= mysql_stmt_init(mysql);
4694 
4695     rc= mysql_stmt_prepare(stmt, sql, (ulong)strlen(sql));
4696     check_stmt_rc(rc, stmt);
4697     rc= mysql_stmt_execute(stmt);
4698     check_stmt_rc(rc, stmt);
4699     rc= mysql_stmt_store_result(stmt);
4700 
4701     memset(bind, 0, sizeof(MYSQL_BIND));
4702     bind[0].buffer_type= MYSQL_TYPE_LONG;
4703     bind[0].buffer= &int_value;
4704     bind[0].buffer_length= sizeof(int_value);
4705 
4706     rc= mysql_stmt_bind_result(stmt, bind);
4707     check_stmt_rc(rc, stmt);
4708     rc= mysql_stmt_fetch(stmt);
4709 
4710     diag("test: str='%s', expected/returned value =%d/%d, expected/returned rc=%d/%d",
4711       test->str_value, test->int_value, int_value, test->rc, rc);
4712     FAIL_UNLESS(rc == test->rc, "unexpected return code");
4713     FAIL_UNLESS(int_value == test->int_value, "unexpected int value");
4714     mysql_stmt_close(stmt);
4715   }
4716   return OK;
4717 }
4718 
4719 
test_codbc138(MYSQL * mysql)4720 static int test_codbc138(MYSQL *mysql)
4721 {
4722   int rc;
4723   MYSQL_STMT *stmt;
4724   MYSQL_BIND bind[1];
4725   MYSQL_TIME tm;
4726   int i= 0;
4727 
4728   struct st_time_test {
4729     const char *statement;
4730     MYSQL_TIME tm;
4731   } time_test[]={
4732     { "SELECT DATE_ADD('2018-02-01', INTERVAL -188 DAY)",
4733   { 2017,7,28,0,0,0,0L,0, MYSQL_TIMESTAMP_DATE }
4734     },
4735   { "SELECT '2001-02-03 11:12:13.123456'",
4736   { 2001,2,3,11,12,13,123456L,0, MYSQL_TIMESTAMP_DATETIME }
4737   },
4738   { "SELECT '2001-02-03 11:12:13.123'",
4739   { 2001,2,3,11,12,13,123000L,0, MYSQL_TIMESTAMP_DATETIME }
4740   },
4741   { "SELECT '-11:12:13'",
4742   { 0,0,0,11,12,13,0,1, MYSQL_TIMESTAMP_TIME }
4743   },
4744   { "SELECT ' '",
4745   { 0,0,0,0,0,0,0,0, MYSQL_TIMESTAMP_ERROR }
4746   },
4747   { "SELECT '1--'",
4748   { 0,0,0,0,0,0,0,0, MYSQL_TIMESTAMP_ERROR }
4749   },
4750   { "SELECT '-2001-01-01'",
4751   { 0,0,0,0,0,0,0,0, MYSQL_TIMESTAMP_ERROR }
4752   },
4753   { "SELECT '-11:00'",
4754   { 0,0,0,0,0,0,0,0, MYSQL_TIMESTAMP_ERROR }
4755   },
4756   {"SELECT '1972-04-22'",
4757   {1972,4,22, 0,0,0, 0,0,MYSQL_TIMESTAMP_DATE}
4758   },
4759   {"SELECT ' 1972-04-22 '",
4760   {1972,4,22, 0,0,0, 0,0,MYSQL_TIMESTAMP_DATE}
4761   },
4762   {"SELECT '1972-04-22a'",
4763   {1972,4,22, 0,0,0, 0,0,MYSQL_TIMESTAMP_DATE}
4764   },
4765   {"SELECT '0000-00-00'",
4766   {0,0,0, 0,0,0 ,0,0,MYSQL_TIMESTAMP_DATE}
4767   },
4768   {"SELECT '1970-01-00'",
4769   {1970,1,0, 0,0,0, 0,0, MYSQL_TIMESTAMP_DATE}
4770   },
4771   {"SELECT '0069-12-31'",
4772   {69,12,31, 0,0,0, 0,0, MYSQL_TIMESTAMP_DATE}
4773   },
4774   {"SELECT '69-12-31'",
4775   {2069,12,31, 0,0,0, 0,0, MYSQL_TIMESTAMP_DATE}
4776   },
4777   {"SELECT '68-12-31'",
4778   {2068,12,31, 0,0,0, 0,0, MYSQL_TIMESTAMP_DATE}
4779   },
4780   {"SELECT '70-01-01'",
4781   {1970,1,1, 0,0,0, 0,0, MYSQL_TIMESTAMP_DATE}
4782   },
4783   {"SELECT '2010-1-1'",
4784   {2010,1,1, 0,0,0, 0,0, MYSQL_TIMESTAMP_DATE}
4785   },
4786 
4787   {"SELECT '10000-01-01'",
4788   {0,0,0, 0,0,0, 0,0, MYSQL_TIMESTAMP_ERROR}
4789   },
4790   {"SELECT '1979-a-01'",
4791   {0,0,0, 0,0,0, 0,0, MYSQL_TIMESTAMP_ERROR}
4792   },
4793   {"SELECT '1979-01-32'",
4794   {0,0,0, 0,0,0, 0,0, MYSQL_TIMESTAMP_ERROR}
4795   },
4796   {"SELECT '1979-13-01'",
4797   {0,0,0, 0,0,0, 0,0, MYSQL_TIMESTAMP_ERROR}
4798   },
4799   {"SELECT '1YYY-01-01'",
4800   {0,0,0, 0,0,0, 0,0, MYSQL_TIMESTAMP_ERROR}
4801   },
4802   {"SELECT '1979-0M-01'",
4803   {0,0,0, 0,0,0, 0,0, MYSQL_TIMESTAMP_ERROR}
4804   },
4805   {"SELECT '1979-00-'",
4806   {0,0,0, 0,0,0, 0,0, MYSQL_TIMESTAMP_ERROR}
4807   },
4808   {"SELECT '1979-00'",
4809   {0,0,0, 0,0,0, 0,0,MYSQL_TIMESTAMP_ERROR}
4810   },
4811   {"SELECT '1979'",
4812   {0,0,0, 0,0,0, 0,0, MYSQL_TIMESTAMP_ERROR}
4813   },
4814   {"SELECT '79'",
4815   {0,0,0, 0,0,0, 0,0, MYSQL_TIMESTAMP_ERROR}
4816   },
4817 
4818   {"SELECT '10:15:00'",
4819   {0,0,0, 10,15,0, 0,0, MYSQL_TIMESTAMP_TIME}
4820   },
4821   {"SELECT '10:15:01'",
4822   {0,0,0, 10,15,1, 0,0, MYSQL_TIMESTAMP_TIME}
4823   },
4824   {"SELECT '00:00:00'",
4825   {0,0,0, 0,0,0, 0,0, MYSQL_TIMESTAMP_TIME}
4826   },
4827   {"SELECT '0:0:0'",
4828   {0,0,0, 0,0,0, 0,0, MYSQL_TIMESTAMP_TIME}
4829   },
4830   {"SELECT '10:15:01.'",
4831   {0,0,0, 10,15,1, 0,0, MYSQL_TIMESTAMP_TIME},
4832   },
4833   {"SELECT '25:59:59'",
4834   {0,0,0, 25,59,59, 0,0, MYSQL_TIMESTAMP_TIME},
4835   },
4836   {"SELECT '838:59:59'",
4837   {0,0,0, 838,59,59, 0,0, MYSQL_TIMESTAMP_TIME},
4838   },
4839   {"SELECT '-838:59:59'",
4840   {0,0,0, 838,59,59, 0, 1, MYSQL_TIMESTAMP_TIME},
4841   },
4842 
4843   {"SELECT '00:60:00'",
4844   {0,0,0, 0,0,0, 0,0, MYSQL_TIMESTAMP_ERROR},
4845   },
4846   {"SELECT '839:00:00'",
4847   {0,0,0, 0,0,0, 0,0, MYSQL_TIMESTAMP_ERROR},
4848   },
4849   {"SELECT '-839:00:00'",
4850   {0,0,0, 0,0,0, 0,0, MYSQL_TIMESTAMP_ERROR},
4851   },
4852   {"SELECT '-10:15:a'",
4853   { 0,0,0, 0,0,0, 0,0, MYSQL_TIMESTAMP_ERROR },
4854   },
4855   {"SELECT '1999-12-31 23:59:59.9999999'",
4856   {1999,12,31, 23,59,59, 999999, 0, MYSQL_TIMESTAMP_DATETIME},
4857   },
4858   {"SELECT '00-08-11 8:46:40'",
4859   {2000,8,11, 8,46,40, 0,0, MYSQL_TIMESTAMP_DATETIME},
4860   },
4861   {"SELECT '1999-12-31 25:59:59.999999'",
4862   {0,0,0, 0,0,0, 0,0, MYSQL_TIMESTAMP_ERROR },
4863   },
4864   { NULL,{ 0 } }
4865   };
4866 
4867   while (time_test[i].statement)
4868   {
4869     stmt= mysql_stmt_init(mysql);
4870     rc= mysql_stmt_prepare(stmt, SL(time_test[i].statement));
4871     check_stmt_rc(rc, stmt);
4872     rc= mysql_stmt_execute(stmt);
4873     check_stmt_rc(rc, stmt);
4874     rc= mysql_stmt_store_result(stmt);
4875 
4876     memset(bind, 0, sizeof(MYSQL_BIND));
4877     bind[0].buffer_type= MYSQL_TYPE_DATETIME;
4878     bind[0].buffer= &tm;
4879     bind[0].buffer_length= sizeof(MYSQL_TIME);
4880 
4881     rc= mysql_stmt_bind_result(stmt, bind);
4882     check_stmt_rc(rc, stmt);
4883     rc= mysql_stmt_fetch(stmt);
4884     check_stmt_rc(rc, stmt);
4885     diag("test: %s %d %d", time_test[i].statement, tm.time_type, time_test[i].tm.time_type);
4886     if (time_test[i].tm.time_type == MYSQL_TIMESTAMP_ERROR)
4887     {
4888       FAIL_UNLESS(tm.time_type == MYSQL_TIMESTAMP_ERROR, "MYSQL_TIMESTAMP_ERROR expected");
4889     }
4890     else
4891       FAIL_UNLESS(equal_MYSQL_TIME(&tm, &time_test[i].tm), "time_in != time_out");
4892     mysql_stmt_close(stmt);
4893     i++;
4894   }
4895 
4896   return OK;
4897 }
4898 
test_conc334(MYSQL * mysql)4899 static int test_conc334(MYSQL *mysql)
4900 {
4901   MYSQL_STMT *stmt= mysql_stmt_init(mysql);
4902   MYSQL_RES *result;
4903   MYSQL_FIELD *field;
4904   int rc;
4905 
4906   rc= mysql_stmt_prepare(stmt, SL("SHOW ENGINES"));
4907   check_stmt_rc(rc, stmt);
4908 
4909   rc= mysql_stmt_execute(stmt);
4910   check_stmt_rc(rc, stmt);
4911 
4912   result= mysql_stmt_result_metadata(stmt);
4913   if (!result)
4914   {
4915     diag("Couldn't retrieve result set");
4916     mysql_stmt_close(stmt);
4917     return FAIL;
4918   }
4919 
4920   mysql_field_seek(result, 0);
4921 
4922   while ((field= mysql_fetch_field(result)))
4923   {
4924     FAIL_IF(field->name_length == 0, "Invalid name length (0)");
4925     FAIL_IF(field->table_length == 0, "Invalid name length (0)");
4926   }
4927   mysql_free_result(result);
4928   mysql_stmt_close(stmt);
4929 
4930   return OK;
4931 }
test_conc344(MYSQL * mysql)4932 static int test_conc344(MYSQL *mysql)
4933 {
4934   MYSQL_STMT *stmt= mysql_stmt_init(mysql);
4935   int rc;
4936 
4937   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4938   check_mysql_rc(rc, mysql);
4939 
4940   rc= mysql_query(mysql, "CREATE TABLE t1 (a int, b int)");
4941   check_mysql_rc(rc, mysql);
4942   rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1,1), (2,2),(3,3),(4,4),(5,5)");
4943   check_mysql_rc(rc, mysql);
4944 
4945   rc= mysql_stmt_prepare(stmt, SL("SELECT * FROM t1 ORDER BY a"));
4946   check_stmt_rc(rc, stmt);
4947 
4948   rc= mysql_stmt_execute(stmt);
4949   check_stmt_rc(rc, stmt);
4950 
4951   while (!mysql_stmt_fetch(stmt));
4952   FAIL_IF(mysql_stmt_num_rows(stmt) != 5, "expected 5 rows");
4953   rc= mysql_stmt_execute(stmt);
4954   check_stmt_rc(rc, stmt);
4955   rc= mysql_stmt_fetch(stmt);
4956   diag("num_rows: %lld", mysql_stmt_num_rows(stmt));
4957   FAIL_IF(mysql_stmt_num_rows(stmt) != 1, "expected 1 row");
4958 
4959   mysql_stmt_close(stmt);
4960   return OK;
4961 }
4962 
4963 
test_conc_fraction(MYSQL * mysql)4964 static int test_conc_fraction(MYSQL *mysql)
4965 {
4966   MYSQL_TIME tm;
4967   MYSQL_BIND bind[1];
4968   char query[1024];
4969   int i;
4970   MYSQL_STMT *stmt= mysql_stmt_init(mysql);
4971   int rc;
4972   unsigned long frac= 0;
4973 
4974   for (i=0; i < 10; i++, frac=frac*10+i)
4975   {
4976     unsigned long expected= 0;
4977     sprintf(query, "SELECT '2018-11-05 22:25:59.%ld'", frac);
4978 
4979     diag("%d: %s", i, query);
4980 
4981     rc= mysql_stmt_prepare(stmt, SL(query));
4982     check_stmt_rc(rc, stmt);
4983 
4984     rc= mysql_stmt_execute(stmt);
4985     check_stmt_rc(rc, stmt);
4986 
4987     rc = mysql_stmt_store_result(stmt);
4988     check_stmt_rc(rc, stmt);
4989 
4990     memset(bind, 0, sizeof(MYSQL_BIND));
4991     bind[0].buffer_type= MYSQL_TYPE_DATETIME;
4992     bind[0].buffer= &tm;
4993     bind[0].buffer_length= sizeof(MYSQL_TIME);
4994 
4995     rc= mysql_stmt_bind_result(stmt, bind);
4996     check_stmt_rc(rc, stmt);
4997     rc= mysql_stmt_fetch(stmt);
4998     check_stmt_rc(rc, stmt);
4999 
5000     diag("second_part: %ld", tm.second_part);
5001 
5002     expected= i > 6 ? 123456 : frac * (unsigned int)powl(10, (6 - i));
5003 
5004     if (tm.second_part != expected)
5005     {
5006       diag("Error: tm.second_part=%ld expected=%ld", tm.second_part, expected);
5007       return FAIL;
5008     }
5009   }
5010   mysql_stmt_close(stmt);
5011   return OK;
5012 }
5013 
test_zerofill_1byte(MYSQL * mysql)5014 static int test_zerofill_1byte(MYSQL *mysql)
5015 {
5016   MYSQL_STMT *stmt= mysql_stmt_init(mysql);
5017   int rc;
5018   MYSQL_BIND bind;
5019   char buffer[3];
5020 
5021   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
5022   check_mysql_rc(rc, mysql);
5023 
5024   rc= mysql_query(mysql, "CREATE TABLE t1 (a int zerofill)");
5025   check_mysql_rc(rc, mysql);
5026 
5027   rc= mysql_query(mysql, "INSERT INTO t1 VALUES(1)");
5028   check_mysql_rc(rc, mysql);
5029 
5030   rc= mysql_stmt_prepare(stmt, SL("SELECT a FROM t1"));
5031   check_stmt_rc(rc, stmt);
5032 
5033   rc= mysql_stmt_execute(stmt);
5034   check_stmt_rc(rc, stmt);
5035 
5036   memset(&bind, 0, sizeof(MYSQL_BIND));
5037   bind.buffer_type= MYSQL_TYPE_STRING;
5038   bind.buffer= buffer;
5039   bind.buffer_length= 1;
5040 
5041   rc= mysql_stmt_bind_result(stmt, &bind);
5042 
5043   rc= mysql_stmt_fetch(stmt);
5044   FAIL_IF(rc != 101, "expected truncation warning");
5045 
5046   mysql_stmt_close(stmt);
5047   rc= mysql_query(mysql, "DROP TABLE t1");
5048   check_mysql_rc(rc, mysql);
5049 
5050   return OK;
5051 }
5052 
test_conc424(MYSQL * mysql)5053 static int test_conc424(MYSQL *mysql)
5054 {
5055   int rc;
5056   MYSQL_STMT *stmt;
5057   my_bool max_len= 1;
5058 
5059   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_table1");
5060   check_mysql_rc(rc, mysql);
5061   rc= mysql_query(mysql, "CREATE TABLE test_table1 (test_int INT, b int)");
5062   check_mysql_rc(rc, mysql);
5063   rc= mysql_query(mysql, "INSERT INTO test_table1 values(10,11),(11,12)");
5064   check_mysql_rc(rc, mysql);
5065 
5066   rc= mysql_query(mysql, "DROP PROCEDURE IF EXISTS testCursor");
5067   check_mysql_rc(rc, mysql);
5068 
5069   rc= mysql_query(mysql, "CREATE PROCEDURE testCursor()\n"
5070                   "BEGIN\n"
5071                   "DECLARE test_int INT;\n"
5072                   "DECLARE b INT;\n"
5073                   "DECLARE done INT DEFAULT FALSE;\n"
5074                   "DECLARE testCursor CURSOR\n"
5075                   "FOR\n"
5076                   "SELECT test_int,b FROM test_table1;\n"
5077                   "DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;\n"
5078                   "OPEN testCursor;\n"
5079 
5080                   " read_loop: LOOP\n"
5081                   "   FETCH testCursor INTO test_int, b;\n"
5082                   "   IF done THEN\n"
5083                   "     LEAVE read_loop;\n"
5084                   "   END IF;\n"
5085                   "   SELECT test_int,b;"
5086                   " END LOOP;\n"
5087                   "CLOSE testCursor;\n"
5088                   "END");
5089   check_mysql_rc(rc, mysql);
5090 
5091   stmt= mysql_stmt_init(mysql);
5092   rc= mysql_stmt_prepare(stmt, SL("CALL testCursor()"));
5093   check_stmt_rc(rc, stmt);
5094 
5095   rc= mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &max_len);
5096   check_stmt_rc(rc, stmt);
5097 
5098   rc= mysql_stmt_execute(stmt);
5099   check_stmt_rc(rc, stmt);
5100 
5101   do {
5102     if (mysql_stmt_field_count(stmt))
5103     {
5104       MYSQL_RES *res= mysql_stmt_result_metadata(stmt);
5105       rc= mysql_stmt_fetch(stmt);
5106       FAIL_IF(rc, "Wrong return code");
5107       mysql_free_result(res);
5108     }
5109     rc= mysql_stmt_next_result(stmt);
5110 
5111   } while (!rc);
5112 
5113   mysql_stmt_close(stmt);
5114   rc= mysql_query(mysql, "DROP PROCEDURE testCursor");
5115   check_mysql_rc(rc, mysql);
5116 
5117   rc= mysql_query(mysql, "DROP TABLE test_table1");
5118   check_mysql_rc(rc, mysql);
5119 
5120   return OK;
5121 }
5122 
test_maxparam(MYSQL * mysql)5123 static int test_maxparam(MYSQL *mysql)
5124 {
5125   const char *query= "INSERT INTO t1 VALUES (?)";
5126   int rc;
5127   char *buffer;
5128   int i;
5129   int val= 1;
5130   size_t mem= strlen(query) + 1 + 4 * 65535 + 1;
5131   MYSQL_STMT *stmt= mysql_stmt_init(mysql);
5132   MYSQL_BIND* bind;
5133 
5134   bind = calloc(sizeof(MYSQL_BIND), 65535);
5135 
5136   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
5137   check_mysql_rc(rc, mysql);
5138 
5139   rc= mysql_query(mysql, "CREATE TABLE t1 (a int)");
5140   check_mysql_rc(rc, mysql);
5141 
5142   buffer= calloc(1, mem);
5143   strcpy(buffer, query);
5144   for (i=0; i < 65534.; i++)
5145     strcat(buffer, ",(?)");
5146   rc= mysql_stmt_prepare(stmt, SL(buffer));
5147   check_stmt_rc(rc, stmt);
5148 
5149   for (i=0; i < 65534; i++)
5150   {
5151     bind[i].buffer_type= MYSQL_TYPE_LONG;
5152     bind[i].buffer= &val;
5153   }
5154 
5155   rc= mysql_stmt_bind_param(stmt, bind);
5156   check_stmt_rc(rc, stmt);
5157 
5158   rc= mysql_stmt_execute(stmt);
5159   check_stmt_rc(rc, stmt);
5160 
5161   FAIL_IF(mysql_stmt_affected_rows(stmt) != 65535, "Expected affected_rows=65535");
5162 
5163   strcat(buffer, ",(?)");
5164   rc= mysql_stmt_prepare(stmt, SL(buffer));
5165   free(buffer);
5166   FAIL_IF(!rc, "Error expected");
5167   FAIL_IF(mysql_stmt_errno(stmt) != ER_PS_MANY_PARAM, "Expected ER_PS_MANY_PARAM error");
5168 
5169   mysql_stmt_close(stmt);
5170   free(bind);
5171   return OK;
5172 }
5173 
test_mdev_21920(MYSQL * mysql)5174 static int test_mdev_21920(MYSQL *mysql)
5175 {
5176   MYSQL_STMT *stmt= mysql_stmt_init(mysql);
5177   MYSQL_BIND bind[1];
5178   int rc;
5179   char buffer[128];
5180 
5181   rc= mysql_stmt_prepare(stmt, SL("SELECT ''"));
5182   check_stmt_rc(rc, stmt);
5183 
5184   rc= mysql_stmt_execute(stmt);
5185   check_stmt_rc(rc, stmt);
5186 
5187   buffer[0]= 1;
5188 
5189   memset(bind, 0, sizeof(MYSQL_BIND));
5190   bind[0].buffer_type= MYSQL_TYPE_STRING;
5191   bind[0].buffer= buffer;
5192   bind[0].buffer_length= 127;
5193 
5194   rc= mysql_stmt_bind_result(stmt, bind);
5195   check_stmt_rc(rc, stmt);
5196 
5197   rc= mysql_stmt_fetch(stmt);
5198   check_stmt_rc(rc, stmt);
5199 
5200   FAIL_IF(buffer[0] != 0, "Expected empty string");
5201 
5202 
5203   mysql_stmt_close(stmt);
5204 
5205   return OK;
5206 }
5207 
test_returning(MYSQL * mysql)5208 static int test_returning(MYSQL *mysql)
5209 {
5210   MYSQL_STMT *stmt= mysql_stmt_init(mysql);
5211   MYSQL_RES *result;
5212   int rc;
5213 
5214   diag("MDEV-23768 not fixed yet");
5215   mysql_stmt_close(stmt);
5216   return SKIP;
5217 
5218   rc= mysql_query(mysql, "CREATE TEMPORARY TABLE t1 (a int not null auto_increment primary key, b json)");
5219   check_mysql_rc(rc, mysql);
5220 
5221   rc= mysql_query(mysql, "INSERT INTO t1 (a,b) VALUES (NULL, '[incorrect json]') RETURNING a");
5222   check_mysql_rc(rc, mysql);
5223 
5224   if (!rc) diag("should have fail");
5225 
5226   result= mysql_store_result(mysql);
5227   mysql_free_result(result);
5228 
5229   diag("Error: %s", mysql_error(mysql));
5230 
5231   rc= mysql_stmt_prepare(stmt, SL("INSERT INTO t1 (a,b) VALUES (NULL, '[incorrect json]') RETURNING a"));
5232   check_stmt_rc(rc, stmt);
5233 
5234   rc= mysql_stmt_execute(stmt);
5235   check_stmt_rc(rc, stmt);
5236 
5237   rc= mysql_stmt_close(stmt);
5238 
5239   return OK;
5240 }
5241 
test_conc504(MYSQL * mysql)5242 static int test_conc504(MYSQL *mysql)
5243 {
5244   int rc;
5245   MYSQL_STMT *stmt= mysql_stmt_init(mysql);
5246   const char *sp= "CREATE PROCEDURE p1()\n" \
5247                   "BEGIN\n"\
5248                   "  SELECT 1;\n"\
5249                   "  SELECT 2;\n"\
5250                   "  SELECT 3;\n"\
5251                   "END";
5252 
5253   rc= mysql_query(mysql, "DROP PROCEDURE IF EXISTS p1");
5254   check_mysql_rc(rc, mysql);
5255 
5256   rc= mysql_query(mysql, sp);
5257   check_mysql_rc(rc, mysql);
5258 
5259   rc= mysql_stmt_prepare(stmt, SL("CALL p1()"));
5260   check_stmt_rc(rc, stmt);
5261 
5262   rc= mysql_stmt_execute(stmt);
5263   check_stmt_rc(rc, stmt);
5264 
5265   mysql_stmt_store_result(stmt);
5266   FAIL_IF(mysql_stmt_num_rows(stmt) != 1, "Expected 1 row");
5267 
5268   mysql_stmt_next_result(stmt);
5269   mysql_stmt_store_result(stmt);
5270   FAIL_IF(mysql_stmt_num_rows(stmt) != 1, "Expected 1 row");
5271 
5272   mysql_stmt_next_result(stmt);
5273   mysql_stmt_store_result(stmt);
5274   FAIL_IF(mysql_stmt_num_rows(stmt) != 1, "Expected 1 row");
5275 
5276   mysql_stmt_close(stmt);
5277 
5278   rc= mysql_query(mysql, "DROP PROCEDURE p1");
5279   check_mysql_rc(rc, mysql);
5280 
5281   return OK;
5282 }
5283 
test_conc512(MYSQL * mysql)5284 static int test_conc512(MYSQL *mysql)
5285 {
5286   int rc;
5287   MYSQL_STMT *stmt;
5288   MYSQL_BIND bind;
5289   float f;
5290 
5291   rc= mysql_query(mysql, "drop table if exists t1");
5292 
5293   rc= mysql_real_query(mysql, SL("CREATE TABLE t1 (a int)"));
5294 
5295   rc= mysql_real_query(mysql, SL("INSERT INTO t1 VALUES (1073741825)"));
5296 
5297   stmt=  mysql_stmt_init(mysql);
5298   rc= mysql_stmt_prepare(stmt, SL("SELECT a FROM t1"));
5299   check_stmt_rc(rc, stmt);
5300 
5301   memset(&bind, 0, sizeof(MYSQL_BIND));
5302   bind.buffer= &f;
5303   bind.buffer_type= MYSQL_TYPE_FLOAT;
5304 
5305   rc= mysql_stmt_execute(stmt);
5306   check_stmt_rc(rc, stmt);
5307 
5308   rc= mysql_stmt_bind_result(stmt, &bind);
5309   check_stmt_rc(rc, stmt);
5310 
5311   rc= mysql_stmt_fetch(stmt);
5312   FAIL_IF(rc != 101, "Truncation expected");
5313 
5314   mysql_stmt_close(stmt);
5315 
5316   rc= mysql_query(mysql, "DROP TABLE t1");
5317   check_mysql_rc(rc, mysql);
5318   return OK;
5319 }
5320 
test_conc566(MYSQL * mysql)5321 static int test_conc566(MYSQL *mysql)
5322 {
5323   int rc;
5324   MYSQL_STMT *stmt = mysql_stmt_init(mysql);
5325   unsigned long cursor = CURSOR_TYPE_READ_ONLY;
5326   const char* query= "call sp()";
5327 
5328   rc= mysql_query(mysql,"drop procedure if exists sp");
5329   check_mysql_rc(rc, mysql);
5330 
5331   rc= mysql_query(mysql,"create procedure sp() select 1");
5332   check_mysql_rc(rc, mysql);
5333 
5334   rc= mysql_stmt_prepare(stmt,query,-1);
5335   check_stmt_rc(rc, stmt);
5336 
5337   rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, &cursor);
5338   check_stmt_rc(rc, stmt);
5339 
5340   rc= mysql_stmt_execute(stmt);
5341   check_stmt_rc(rc, stmt);
5342 
5343   mysql_stmt_close(stmt);
5344 
5345   rc= mysql_query(mysql,"drop procedure sp");
5346   check_mysql_rc(rc, mysql);
5347   return OK;
5348 }
5349 
5350 #define MDEV19838_MAX_PARAM_COUNT 32
5351 #define MDEV19838_FIELDS_COUNT 17
5352 
test_mdev19838(MYSQL * mysql)5353 static int test_mdev19838(MYSQL *mysql)
5354 {
5355   int rc;
5356   MYSQL_BIND bind[MDEV19838_MAX_PARAM_COUNT];
5357   unsigned int i, paramCount = 1;
5358   char charvalue[] = "012345678901234567890123456789012345";
5359   MYSQL_STMT *stmt;
5360 
5361   rc = mysql_query(mysql, "CREATE temporary TABLE mdev19838("
5362           "f1  char(36),"
5363           "f2  char(36),"
5364           "f3  char(36),"
5365           "f4  char(36),"
5366           "f5  char(36),"
5367           "f6  char(36),"
5368           "f7  char(36),"
5369           "f8  char(36),"
5370           "f9  char(36),"
5371           "f10 char(36),"
5372           "f11 char(36),"
5373           "f12 char(36),"
5374           "f13 char(36),"
5375           "f14 char(36),"
5376           "f15 char(36),"
5377           "f16 char(36),"
5378           "f17 char(36)"
5379     ")");
5380   check_mysql_rc(rc, mysql);
5381 
5382   stmt = mysql_stmt_init(mysql);
5383 
5384   memset(bind, 0, sizeof(bind));
5385 
5386   for (i = 0; i < MDEV19838_MAX_PARAM_COUNT; ++i)
5387   {
5388     bind[i].buffer = charvalue;
5389     bind[i].buffer_type = MYSQL_TYPE_STRING;
5390     bind[i].buffer_length = sizeof charvalue;
5391     bind[i].length = &bind[i].length_value;
5392     bind[i].length_value = bind[i].buffer_length - 1;
5393   }
5394 
5395   for (paramCount = 1; paramCount < MDEV19838_FIELDS_COUNT; ++paramCount)
5396   {
5397     mysql_stmt_attr_set(stmt, STMT_ATTR_PREBIND_PARAMS, &paramCount);
5398 
5399     rc = mysql_stmt_bind_param(stmt, bind);
5400     check_stmt_rc(rc, stmt);
5401 
5402     rc = mariadb_stmt_execute_direct(stmt, "INSERT INTO mdev19838"
5403       "(f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15, f16, f17)"
5404       " VALUES "
5405       "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", -1);
5406 
5407     /* Expecting an error */
5408     FAIL_UNLESS(rc != 0, "rc!=0");
5409 
5410     mysql_stmt_close(stmt);
5411     stmt = mysql_stmt_init(mysql);
5412   }
5413 
5414   paramCount = 0;
5415   mysql_stmt_attr_set(stmt, STMT_ATTR_PREBIND_PARAMS, &paramCount);
5416   rc = mariadb_stmt_execute_direct(stmt, "INSERT INTO mdev19838(f1)"
5417     " VALUES (?)", -1);
5418   /* Expecting an error */
5419   FAIL_UNLESS(rc != 0, "rc!=0");
5420   mysql_stmt_close(stmt);
5421 
5422   stmt = mysql_stmt_init(mysql);
5423   /* Correct number of parameters */
5424   paramCount = MDEV19838_FIELDS_COUNT;
5425   mysql_stmt_attr_set(stmt, STMT_ATTR_PREBIND_PARAMS, &paramCount);
5426   mysql_stmt_bind_param(stmt, bind);
5427 
5428   rc = mariadb_stmt_execute_direct(stmt, "INSERT INTO mdev19838"
5429     "(f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15, f16, f17)"
5430     " VALUES "
5431     "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", -1);
5432   check_stmt_rc(rc, stmt);
5433 
5434   /* MYSQL_TYPE_TINY = 1. This parameter byte can be read as "parameters send" flag byte.
5435      Checking that wrong packet is still detected */
5436   bind[0].buffer_type = MYSQL_TYPE_TINY;
5437   bind[0].length_value = 1;
5438   bind[0].buffer_length = 1;
5439 
5440   for (paramCount = 8; paramCount > 0; --paramCount)
5441   {
5442     mysql_stmt_close(stmt);
5443     stmt = mysql_stmt_init(mysql);
5444 
5445     mysql_stmt_attr_set(stmt, STMT_ATTR_PREBIND_PARAMS, &paramCount);
5446 
5447     rc = mysql_stmt_bind_param(stmt, bind);
5448 
5449     rc = mariadb_stmt_execute_direct(stmt, "INSERT INTO mdev19838"
5450       "(f1, f2, f3, f4, f5, f6, f7, f8, f9)"
5451       " VALUES "
5452       "(?, ?, ?, ?, ?, ?, ?, ?, ?)", -1);
5453 
5454     /* Expecting an error */
5455     FAIL_UNLESS(rc != 0, "rc");
5456   }
5457 
5458   /* Test of query w/out parameters, with parameter sent and not sent */
5459   for (paramCount = MDEV19838_MAX_PARAM_COUNT; paramCount != (unsigned int)-1; --paramCount)
5460   {
5461     mysql_stmt_close(stmt);
5462     stmt = mysql_stmt_init(mysql);
5463 
5464     mysql_stmt_attr_set(stmt, STMT_ATTR_PREBIND_PARAMS, &paramCount);
5465 
5466     if (paramCount > 0)
5467     {
5468       rc = mysql_stmt_bind_param(stmt, bind);
5469       check_stmt_rc(rc, stmt);
5470     }
5471 
5472     rc = mariadb_stmt_execute_direct(stmt, "INSERT INTO mdev19838"
5473       "(f1)"
5474       " VALUES "
5475       "(0x1111111111111111)", -1);
5476 
5477     /*
5478       We allow junk at the end of the packet in case of
5479       no parameters. So it will succeed.
5480     */
5481     FAIL_UNLESS(rc == 0, "");
5482   }
5483 
5484   mysql_stmt_close(stmt);
5485   return OK;
5486 }
5487 
5488 struct my_tests_st my_tests[] = {
5489   {"test_mdev19838", test_mdev19838, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5490   {"test_conc566", test_conc566, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5491   {"test_conc512", test_conc512, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5492   {"test_conc504", test_conc504, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5493   {"test_returning", test_returning, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5494   {"test_mdev_21920", test_mdev_21920, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5495   {"test_maxparam", test_maxparam, TEST_CONNECTION_NEW, 0, NULL, NULL},
5496   {"test_conc424", test_conc424, TEST_CONNECTION_NEW, 0, NULL, NULL},
5497   {"test_conc344", test_conc344, TEST_CONNECTION_NEW, 0, NULL, NULL},
5498   {"test_conc334", test_conc334, TEST_CONNECTION_NEW, 0, NULL, NULL},
5499   {"test_compress", test_compress, TEST_CONNECTION_NEW, CLIENT_COMPRESS, NULL, NULL},
5500   {"test_zerofill_1byte", test_zerofill_1byte, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5501   {"test_codbc138", test_codbc138, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5502   {"test_conc208", test_conc208, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5503   {"test_mdev14165", test_mdev14165, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5504   {"test_conc208", test_conc208, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5505   {"test_conc217", test_conc217, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5506   {"test_conc205", test_conc205, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5507   {"test_conc198", test_conc198, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5508   {"test_conc182", test_conc182, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5509   {"test_conc181", test_conc181, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5510   {"test_conc179", test_conc179, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5511   {"test_conc177", test_conc177, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5512   {"test_conc167", test_conc167, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5513   {"test_conc168", test_conc168, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5514   {"test_conc155", test_conc155, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5515   {"test_conc154", test_conc154, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5516   {"test_conc141", test_conc141, TEST_CONNECTION_NEW, 0, NULL , NULL},
5517   {"test_conc67", test_conc67, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5518   {"test_conc_5", test_conc_5, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5519   {"test_bug1115", test_bug1115, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5520   {"test_bug1180", test_bug1180, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5521   {"test_bug1644", test_bug1644, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5522   {"test_bug11037", test_bug11037, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5523   {"test_bug11183", test_bug11183, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5524   {"test_bug12744", test_bug12744, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5525   {"test_bug1500", test_bug1500, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5526   {"test_bug15510", test_bug15510, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5527   {"test_bug15518", test_bug15518, TEST_CONNECTION_NEW | TEST_CONNECTION_DONT_CLOSE, CLIENT_MULTI_STATEMENTS, NULL , NULL},
5528   {"test_bug15613", test_bug15613, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5529   {"test_bug16144", test_bug16144, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5530   {"test_bug1664", test_bug1664, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5531   {"test_bug1946", test_bug1946, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5532   {"test_bug2247", test_bug2247, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5533   {"test_bug2248", test_bug2248, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5534   {"test_bug20152", test_bug20152, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5535   {"test_bug23383", test_bug23383, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5536   {"test_bug27592", test_bug27592, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5537   {"test_bug28934", test_bug28934, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5538   {"test_bug36004", test_bug36004, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5539   {"test_bug3035", test_bug3035, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5540   {"test_bug3117", test_bug3117, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5541   {"test_bug3796", test_bug3796, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5542   {"test_bug4026", test_bug4026, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5543   {"test_bug4030", test_bug4030, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5544   {"test_bug4079", test_bug4079, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5545   {"test_bug4172", test_bug4172, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5546   {"test_bug4231", test_bug4231, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5547   {"test_bug4236", test_bug4236, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5548   {"test_bug5126", test_bug5126, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5549   {"test_bug5194", test_bug5194, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5550   {"test_bug5315", test_bug5315, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5551   {"test_bug5399", test_bug5399, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5552   {"test_bug6046", test_bug6046, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5553   {"test_bug6049", test_bug6049, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5554   {"test_bug6058", test_bug6058, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5555   {"test_bug6059", test_bug6059, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5556   {"test_bug6096", test_bug6096, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5557   {"test_bug7990", test_bug7990, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5558   {"test_bug8330", test_bug8330, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5559   {"test_bug8722", test_bug8722, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5560   {"test_ps_conj_select", test_ps_conj_select, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5561   {"test_ps_null_param", test_ps_null_param, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5562   {"test_ps_query_cache", test_ps_query_cache, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5563   {"test_ushort_bug", test_ushort_bug, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5564   {"test_field_misc", test_field_misc, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5565   {"test_mem_overun", test_mem_overun, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5566   {"test_decimal_bug", test_decimal_bug, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5567   {"test_explain_bug", test_explain_bug, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5568   {"test_sshort_bug", test_sshort_bug, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5569   {"test_stiny_bug", test_stiny_bug, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5570   {"test_bug53311", test_bug53311, TEST_CONNECTION_NEW, 0, NULL , NULL},
5571   {"test_conc_fraction", test_conc_fraction, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5572   {"test_str_to_int", test_str_to_int, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5573   {NULL, NULL, 0, 0, NULL, NULL}
5574 };
5575 
main(int argc,char ** argv)5576 int main(int argc, char **argv)
5577 {
5578   if (argc > 1)
5579     get_options(argc, argv);
5580 
5581   get_envvars();
5582 
5583   run_tests(my_tests);
5584 
5585   return(exit_status());
5586 }
5587