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 /* Utility function to verify the field members */
27 
test_conc97(MYSQL * mysql)28 static int test_conc97(MYSQL *mysql)
29 {
30   MYSQL_STMT *stmt;
31   int rc;
32 
33   diag("Please run this test manually");
34   return SKIP;
35   stmt= mysql_stmt_init(mysql);
36 
37   mysql_close(mysql);
38 
39   rc= mysql_stmt_reset(stmt);
40   FAIL_IF(!rc, "Error expected while resetting stmt");
41 
42   rc= mysql_stmt_close(stmt);
43   check_stmt_rc(rc, stmt);
44 
45   mysql= mysql_init(NULL);
46 
47   return OK;
48 }
49 
test_conc83(MYSQL * unused)50 static int test_conc83(MYSQL *unused __attribute__((unused)))
51 {
52   MYSQL_STMT *stmt;
53   int rc;
54   MYSQL *mysql= mysql_init(NULL);
55   my_bool reconnect= 1;
56 
57   const char *query= "SELECT 1,2,3 FROM DUAL";
58 
59   SKIP_MAXSCALE;
60 
61   stmt= mysql_stmt_init(mysql);
62 
63   mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect);
64   FAIL_IF(!(my_test_connect(mysql, hostname, username, password,
65                            schema, port, socketname, 0)), "my_test_connect failed");
66 
67   /* 1. Status is inited, so prepare should work */
68 
69   rc= mysql_kill(mysql, mysql_thread_id(mysql));
70 
71   rc= mysql_ping(mysql);
72   check_mysql_rc(rc, mysql);
73 
74   rc= mysql_stmt_prepare(stmt, SL(query));
75   check_stmt_rc(rc, stmt);
76   diag("Ok");
77 
78   /* 2. Status is prepared, execute should fail */
79   rc= mysql_kill(mysql, mysql_thread_id(mysql));
80 
81   rc= mysql_stmt_execute(stmt);
82   FAIL_IF(!rc, "Error expected");
83 
84   mysql_stmt_close(stmt);
85   mysql_close(mysql);
86   return OK;
87 }
88 
89 
test_conc60(MYSQL * mysql)90 static int test_conc60(MYSQL *mysql)
91 {
92   MYSQL_STMT *stmt;
93   int rc;
94   const char *query= "SELECT * FROM agendas";
95   my_bool x= 1;
96 
97   stmt= mysql_stmt_init(mysql);
98 
99   rc= mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void *)&x);
100 
101   rc= mysql_stmt_prepare(stmt, SL(query));
102   if (rc && mysql_stmt_errno(stmt) == 1146) {
103     diag("Internal test - customer data not available");
104     mysql_stmt_close(stmt);
105     return SKIP;
106   }
107   check_stmt_rc(rc, stmt);
108 
109   rc= mysql_stmt_execute(stmt);
110   check_stmt_rc(rc, stmt);
111 
112   rc= mysql_stmt_store_result(stmt);
113   check_stmt_rc(rc, stmt);
114 
115   rc= mysql_stmt_free_result(stmt);
116   check_stmt_rc(rc, stmt);
117 
118   mysql_stmt_close(stmt);
119 
120   return OK;
121 }
122 
test_prepare_insert_update(MYSQL * mysql)123 static int test_prepare_insert_update(MYSQL *mysql)
124 {
125   MYSQL_STMT *stmt;
126   int        rc;
127   int        i;
128   const char *testcase[]= {
129     "CREATE TABLE t1 (a INT, b INT, c INT, UNIQUE (A), UNIQUE(B))",
130     "INSERT t1 VALUES (1,2,10), (3,4,20)",
131     "INSERT t1 VALUES (5,6,30), (7,4,40), (8,9,60) ON DUPLICATE KEY UPDATE c=c+100",
132     "SELECT * FROM t1",
133     "INSERT t1 SET a=5 ON DUPLICATE KEY UPDATE b=0",
134     "SELECT * FROM t1",
135     "INSERT t1 VALUES (2,1,11), (7,4,40) ON DUPLICATE KEY UPDATE c=c+VALUES(a)",
136     NULL};
137   const char **cur_query;
138 
139   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
140   check_mysql_rc(rc, mysql);
141 
142   for (cur_query= testcase; *cur_query; cur_query++)
143   {
144     char query[MAX_TEST_QUERY_LENGTH];
145     strcpy(query, *cur_query);
146     stmt= mysql_stmt_init(mysql);
147     FAIL_IF(!stmt, mysql_error(mysql));
148     rc= mysql_stmt_prepare(stmt, SL(query));
149     check_stmt_rc(rc, stmt);
150 
151     FAIL_IF(mysql_stmt_param_count(stmt) != 0, "Paramcount is not 0");
152     rc= mysql_stmt_execute(stmt);
153     check_stmt_rc(rc, stmt);
154 
155     /* try the last query several times */
156     if (!cur_query[1])
157     {
158       for (i=0; i < 3;i++)
159       {
160         rc= mysql_stmt_execute(stmt);
161         check_stmt_rc(rc, stmt);
162         rc= mysql_stmt_execute(stmt);
163         check_stmt_rc(rc, stmt);
164       }
165     }
166     mysql_stmt_close(stmt);
167   }
168 
169   rc= mysql_commit(mysql);
170   check_mysql_rc(rc, mysql);
171 
172   return OK;
173 }
174 
175 /*
176   Generalized conversion routine to handle DATE, TIME and DATETIME
177   conversion using MYSQL_TIME structure
178 */
179 
test_bind_date_conv(MYSQL * mysql,uint row_count)180 static int test_bind_date_conv(MYSQL *mysql, uint row_count)
181 {
182   MYSQL_STMT   *stmt= 0;
183   uint         rc, i, count;
184   MYSQL_BIND   my_bind[4];
185   my_bool      is_null[4]= {0,0,0,0};
186   MYSQL_TIME   tm[4];
187   ulong        second_part;
188   uint         year, month, day, hour, minute, sec;
189 
190   stmt= mysql_stmt_init(mysql);
191   FAIL_IF(!stmt, mysql_error(mysql));
192   rc= mysql_stmt_prepare(stmt, SL("INSERT INTO test_date VALUES(?, ?, ?, ?)"));
193   check_stmt_rc(rc, stmt);
194 
195   FAIL_IF(mysql_stmt_param_count(stmt) != 4, "param_count != 4");
196 
197   /*
198     We need to bzero bind structure because mysql_stmt_bind_param checks all
199     its members.
200   */
201   memset(my_bind, '\0', sizeof(my_bind));
202 
203   my_bind[0].buffer_type= MYSQL_TYPE_TIMESTAMP;
204   my_bind[1].buffer_type= MYSQL_TYPE_TIME;
205   my_bind[2].buffer_type= MYSQL_TYPE_DATETIME;
206   my_bind[3].buffer_type= MYSQL_TYPE_DATETIME;
207 
208   for (i= 0; i < (int) array_elements(my_bind); i++)
209   {
210     my_bind[i].buffer= (void *) &tm[i];
211     my_bind[i].is_null= &is_null[i];
212     my_bind[i].buffer_length= sizeof(MYSQL_TIME);
213   }
214 
215   second_part= 0;
216 
217   year= 2000;
218   month= 01;
219   day= 10;
220 
221   hour= 11;
222   minute= 16;
223   sec= 20;
224 
225   rc= mysql_stmt_bind_param(stmt, my_bind);
226   check_stmt_rc(rc, stmt);
227 
228   for (count= 0; count < row_count; count++)
229   {
230     for (i= 0; i < (int) array_elements(my_bind); i++)
231     {
232       memset(&tm[i],  0, sizeof(MYSQL_TIME));
233       tm[i].neg= 0;
234       tm[i].second_part= second_part+count;
235       if (my_bind[i].buffer_type != MYSQL_TYPE_TIME)
236       {
237         tm[i].year= year+count;
238         tm[i].month= month+count;
239         tm[i].day= day+count;
240       }
241       else
242         tm[i].year= tm[i].month= tm[i].day= 0;
243       if (my_bind[i].buffer_type != MYSQL_TYPE_DATE)
244       {
245         tm[i].hour= hour+count;
246         tm[i].minute= minute+count;
247         tm[i].second= sec+count;
248       }
249       else
250         tm[i].hour= tm[i].minute= tm[i].second= 0;
251     }
252     rc= mysql_stmt_execute(stmt);
253     check_stmt_rc(rc, stmt);
254   }
255 
256   rc= mysql_commit(mysql);
257   check_mysql_rc(rc, mysql);
258 
259   mysql_stmt_close(stmt);
260 
261   rc= my_stmt_result(mysql, "SELECT * FROM test_date");
262   FAIL_UNLESS(row_count == rc, "rowcount != rc");
263 
264   stmt= mysql_stmt_init(mysql);
265   FAIL_IF(!stmt, mysql_error(mysql));
266   rc= mysql_stmt_prepare(stmt, SL("SELECT * FROM test_date"));
267   check_stmt_rc(rc, stmt);
268 
269   rc= mysql_stmt_bind_result(stmt, my_bind);
270   check_stmt_rc(rc, stmt);
271 
272   rc= mysql_stmt_execute(stmt);
273   check_stmt_rc(rc, stmt);
274 
275   rc= mysql_stmt_store_result(stmt);
276   check_stmt_rc(rc, stmt);
277 
278   for (count= 0; count < row_count; count++)
279   {
280     rc= mysql_stmt_fetch(stmt);
281     FAIL_UNLESS(rc == 0 || rc == MYSQL_DATA_TRUNCATED, "rc != 0 | rc != MYSQL_DATA_TRUNCATED");
282 
283     for (i= 0; i < array_elements(my_bind); i++)
284     {
285       FAIL_UNLESS(tm[i].year == 0 || tm[i].year == year+count, "wrong value for year");
286       FAIL_UNLESS(tm[i].month == 0 || tm[i].month == month+count, "wrong value for month");
287       FAIL_UNLESS(tm[i].day == 0 || tm[i].day == day+count, "wrong value for day");
288       FAIL_UNLESS(tm[i].hour == 0 || tm[i].hour % 24 == 0 || tm[i].hour % 24 == hour+count, "wrong value for hour");
289       FAIL_UNLESS(tm[i].minute == 0 || tm[i].minute == minute+count, "wrong value for minute");
290       FAIL_UNLESS(tm[i].second == 0 || tm[i].second == sec+count, "wrong value for second");
291       FAIL_UNLESS(tm[i].second_part == 0 ||
292                  tm[i].second_part == second_part+count, "wrong value for second_part");
293     }
294   }
295   rc= mysql_stmt_fetch(stmt);
296   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
297 
298   mysql_stmt_close(stmt);
299   return OK;
300 }
301 
302 
303 /* Test simple prepares of all DML statements */
304 
test_prepare_simple(MYSQL * mysql)305 static int test_prepare_simple(MYSQL *mysql)
306 {
307   MYSQL_STMT *stmt;
308   int        rc;
309   char query[MAX_TEST_QUERY_LENGTH];
310 
311   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_simple");
312   check_mysql_rc(rc, mysql);
313 
314   rc= mysql_query(mysql, "CREATE TABLE test_prepare_simple("
315                          "id int, name varchar(50))");
316   check_mysql_rc(rc, mysql);
317 
318   /* insert */
319   strcpy(query, "INSERT INTO test_prepare_simple VALUES(?, ?)");
320   stmt= mysql_stmt_init(mysql);
321   FAIL_IF(!stmt, mysql_error(mysql));
322   rc= mysql_stmt_prepare(stmt, SL(query));
323   check_stmt_rc(rc, stmt);
324 
325   FAIL_IF(mysql_stmt_param_count(stmt) != 2, "Paramcount is not 2");
326   mysql_stmt_close(stmt);
327 
328   /* update */
329   strcpy(query, "UPDATE test_prepare_simple SET id=? "
330                 "WHERE id=? AND CONVERT(name USING utf8)= ?");
331   stmt= mysql_stmt_init(mysql);
332   FAIL_IF(!stmt, mysql_error(mysql));
333   rc= mysql_stmt_prepare(stmt, SL(query));
334   check_stmt_rc(rc, stmt);
335 
336   FAIL_IF(mysql_stmt_param_count(stmt) != 3, "Paramcount is not 3");
337   mysql_stmt_close(stmt);
338 
339   /* delete */
340   strcpy(query, "DELETE FROM test_prepare_simple WHERE id=10");
341   stmt= mysql_stmt_init(mysql);
342   FAIL_IF(!stmt, mysql_error(mysql));
343   rc= mysql_stmt_prepare(stmt, SL(query));
344   check_stmt_rc(rc, stmt);
345 
346   FAIL_IF(mysql_stmt_param_count(stmt) != 0, "Paramcount is not 0");
347 
348   rc= mysql_stmt_execute(stmt);
349   check_stmt_rc(rc, stmt);
350   mysql_stmt_close(stmt);
351 
352   /* delete */
353   strcpy(query, "DELETE FROM test_prepare_simple WHERE id=?");
354   stmt= mysql_stmt_init(mysql);
355   FAIL_IF(!stmt, mysql_error(mysql));
356   rc= mysql_stmt_prepare(stmt, SL(query));
357   check_stmt_rc(rc, stmt);
358 
359   FAIL_IF(mysql_stmt_param_count(stmt) != 1, "Paramcount != 1");
360 
361   mysql_stmt_close(stmt);
362 
363   /* select */
364   strcpy(query, "SELECT * FROM test_prepare_simple WHERE id=? "
365                 "AND CONVERT(name USING utf8)= ?");
366   stmt= mysql_stmt_init(mysql);
367   FAIL_IF(!stmt, mysql_error(mysql));
368   rc= mysql_stmt_prepare(stmt, SL(query));
369   check_stmt_rc(rc, stmt);
370 
371   FAIL_IF(mysql_stmt_param_count(stmt) != 2, "Paramcount != 2");
372 
373   mysql_stmt_close(stmt);
374 
375   /* now fetch the results ..*/
376   rc= mysql_commit(mysql);
377   check_mysql_rc(rc, mysql);
378   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_simple");
379   check_mysql_rc(rc, mysql);
380 
381   return OK;
382 }
383 
test_prepare_field_result(MYSQL * mysql)384 static int test_prepare_field_result(MYSQL *mysql)
385 {
386   MYSQL_STMT *stmt;
387   MYSQL_RES  *result;
388   int        rc;
389   char query[MAX_TEST_QUERY_LENGTH];
390 
391   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_field_result");
392   check_mysql_rc(rc, mysql);
393 
394   rc= mysql_query(mysql, "CREATE TABLE test_prepare_field_result(int_c int, "
395                          "var_c varchar(50), ts_c timestamp, "
396                          "char_c char(4), date_c date, extra tinyint)");
397   check_mysql_rc(rc, mysql);
398 
399   /* insert */
400   strcpy(query, "SELECT int_c, var_c, date_c as date, ts_c, char_c FROM "
401                 " test_prepare_field_result as t1 WHERE int_c=?");
402   stmt= mysql_stmt_init(mysql);
403   FAIL_IF(!stmt, mysql_error(mysql));
404   rc= mysql_stmt_prepare(stmt, SL(query));
405   check_stmt_rc(rc, stmt);
406 
407   FAIL_IF(mysql_stmt_param_count(stmt) != 1, "Paramcount != 1");
408 
409   result= mysql_stmt_result_metadata(stmt);
410   FAIL_IF(!result, mysql_stmt_error(stmt));
411 
412   if (verify_prepare_field(result, 0, "int_c", "int_c", MYSQL_TYPE_LONG,
413                        "t1", "test_prepare_field_result", schema, 11, 0))
414     goto error;
415   if (verify_prepare_field(result, 1, "var_c", "var_c", MYSQL_TYPE_VAR_STRING,
416                        "t1", "test_prepare_field_result", schema, 50, 0))
417     goto error;
418   if (verify_prepare_field(result, 2, "date", "date_c", MYSQL_TYPE_DATE,
419                        "t1", "test_prepare_field_result", schema, 10, 0))
420     goto error;
421   if (verify_prepare_field(result, 3, "ts_c", "ts_c", MYSQL_TYPE_TIMESTAMP,
422                        "t1", "test_prepare_field_result", schema, 19, 0))
423     goto error;
424   if (verify_prepare_field(result, 4, "char_c", "char_c",
425                        (mysql_get_server_version(mysql) <= 50000 ?
426                         MYSQL_TYPE_VAR_STRING : MYSQL_TYPE_STRING),
427                        "t1", "test_prepare_field_result", schema, 4, 0))
428     goto error;
429 
430   FAIL_IF(mysql_num_fields(result) != 5, "Paramcount != 5");
431   mysql_free_result(result);
432   mysql_stmt_close(stmt);
433   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_field_result");
434   check_mysql_rc(rc, mysql);
435 
436   return OK;
437 
438 error:
439   mysql_free_result(result);
440   mysql_stmt_close(stmt);
441   return FAIL;
442 }
443 
444 
445 /* Test simple prepare field results */
446 
test_prepare_syntax(MYSQL * mysql)447 static int test_prepare_syntax(MYSQL *mysql)
448 {
449   MYSQL_STMT *stmt;
450   int        rc;
451   char query[MAX_TEST_QUERY_LENGTH];
452 
453   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_syntax");
454   check_mysql_rc(rc, mysql);
455 
456   rc= mysql_query(mysql, "CREATE TABLE test_prepare_syntax("
457                          "id int, name varchar(50), extra int)");
458   check_mysql_rc(rc, mysql);
459 
460   rc= mysql_query(mysql, "FLUSH TABLES");
461   check_mysql_rc(rc, mysql);
462 
463   rc= mysql_query(mysql, "START TRANSACTION");
464   check_mysql_rc(rc, mysql);
465 
466   strcpy(query, "INSERT INTO test_prepare_syntax VALUES(?");
467   stmt= mysql_stmt_init(mysql);
468   FAIL_IF(!stmt, mysql_error(mysql));
469   rc= mysql_stmt_prepare(stmt, SL(query));
470   FAIL_IF(!rc, "error expected");
471 
472   strcpy(query, "SELECT id, name FROM test_prepare_syntax WHERE id=? AND WHERE");
473   FAIL_IF(!stmt, mysql_error(mysql));
474   rc= mysql_stmt_prepare(stmt, SL(query));
475   FAIL_IF(!rc, "error expected");
476 
477   /* now fetch the results ..*/
478   rc= mysql_commit(mysql);
479   check_mysql_rc(rc, mysql);
480 
481   mysql_stmt_close(stmt);
482   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_syntax");
483   check_mysql_rc(rc, mysql);
484 
485   return OK;
486 }
487 
test_prepare(MYSQL * mysql)488 static int test_prepare(MYSQL *mysql)
489 {
490   MYSQL_STMT *stmt;
491   int        rc, i;
492   int        int_data, o_int_data;
493   char       str_data[50], data[50];
494   char       tiny_data, o_tiny_data;
495   short      small_data, o_small_data;
496   longlong   big_data, o_big_data;
497   float      real_data, o_real_data;
498   double     double_data, o_double_data;
499   ulong      length[7], len;
500   my_bool    is_null[7];
501   MYSQL_BIND my_bind[7];
502   char query[MAX_TEST_QUERY_LENGTH];
503 
504   rc= mysql_autocommit(mysql, TRUE);
505   check_mysql_rc(rc, mysql);
506 
507   rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_prepare");
508   check_mysql_rc(rc, mysql);
509 
510   rc= mysql_query(mysql, "CREATE TABLE my_prepare(col1 tinyint, "
511                          "col2 varchar(15), col3 int, "
512                          "col4 smallint, col5 bigint, "
513                          "col6 float, col7 double )");
514   check_mysql_rc(rc, mysql);
515 
516   /* insert by prepare */
517   strcpy(query, "INSERT INTO my_prepare VALUES(?, ?, ?, ?, ?, ?, ?)");
518   stmt= mysql_stmt_init(mysql);
519   FAIL_IF(!stmt, mysql_error(mysql));
520   rc= mysql_stmt_prepare(stmt, SL(query));
521   check_stmt_rc(rc, stmt);
522 
523   FAIL_IF(mysql_stmt_param_count(stmt) != 7, "Paramcount != 7");
524 
525   memset(my_bind, '\0', sizeof(my_bind));
526 
527   /* tinyint */
528   my_bind[0].buffer_type= MYSQL_TYPE_TINY;
529   my_bind[0].buffer= (void *)&tiny_data;
530   /* string */
531   my_bind[1].buffer_type= MYSQL_TYPE_STRING;
532   my_bind[1].buffer= (void *)str_data;
533   my_bind[1].buffer_length= 1000;                  /* Max string length */
534   /* integer */
535   my_bind[2].buffer_type= MYSQL_TYPE_LONG;
536   my_bind[2].buffer= (void *)&int_data;
537   /* short */
538   my_bind[3].buffer_type= MYSQL_TYPE_SHORT;
539   my_bind[3].buffer= (void *)&small_data;
540   /* bigint */
541   my_bind[4].buffer_type= MYSQL_TYPE_LONGLONG;
542   my_bind[4].buffer= (void *)&big_data;
543   /* float */
544   my_bind[5].buffer_type= MYSQL_TYPE_FLOAT;
545   my_bind[5].buffer= (void *)&real_data;
546   /* double */
547   my_bind[6].buffer_type= MYSQL_TYPE_DOUBLE;
548   my_bind[6].buffer= (void *)&double_data;
549 
550   for (i= 0; i < (int) array_elements(my_bind); i++)
551   {
552     my_bind[i].length= &length[i];
553     my_bind[i].is_null= &is_null[i];
554     is_null[i]= 0;
555   }
556 
557   rc= mysql_stmt_bind_param(stmt, my_bind);
558   check_stmt_rc(rc, stmt);
559 
560   int_data= 320;
561   small_data= 1867;
562   big_data= 1000;
563   real_data= 2;
564   double_data= 6578.001;
565 
566   /* now, execute the prepared statement to insert 10 records.. */
567   for (tiny_data= 0; tiny_data < 100; tiny_data++)
568   {
569     length[1]= sprintf(str_data, "MySQL%d", int_data);
570     rc= mysql_stmt_execute(stmt);
571     check_stmt_rc(rc, stmt);
572     int_data += 25;
573     small_data += 10;
574     big_data += 100;
575     real_data += 1;
576     double_data += 10.09;
577   }
578 
579   mysql_stmt_close(stmt);
580 
581   /* now fetch the results ..*/
582   rc= mysql_commit(mysql);
583   check_mysql_rc(rc, mysql);
584 
585   /* test the results now, only one row should exist */
586   rc= my_stmt_result(mysql, "SELECT * FROM my_prepare");
587   FAIL_UNLESS(rc != 1, "rowcount != 1");
588 
589   stmt= mysql_stmt_init(mysql);
590   FAIL_IF(!stmt, mysql_error(mysql));
591   rc= mysql_stmt_prepare(stmt, "SELECT * FROM my_prepare", 25);
592   check_stmt_rc(rc, stmt);
593 
594   rc= mysql_stmt_bind_result(stmt, my_bind);
595   check_stmt_rc(rc, stmt);
596 
597   /* get the result */
598   rc= mysql_stmt_execute(stmt);
599   check_stmt_rc(rc, stmt);
600 
601   o_int_data= 320;
602   o_small_data= 1867;
603   o_big_data= 1000;
604   o_real_data= 2;
605   o_double_data= 6578.001;
606 
607   /* now, execute the prepared statement to insert 10 records.. */
608   for (o_tiny_data= 0; o_tiny_data < 100; o_tiny_data++)
609   {
610     len= sprintf(data, "MySQL%d", o_int_data);
611 
612     rc= mysql_stmt_fetch(stmt);
613     check_stmt_rc(rc, stmt);
614 
615     FAIL_UNLESS(tiny_data == o_tiny_data, "Wrong value for tiny_data");
616     FAIL_UNLESS(is_null[0] == 0, "Wrong value for is_null");
617     FAIL_UNLESS(length[0] == 1, "length != 0");
618 
619     FAIL_UNLESS(int_data == o_int_data, "Wrong value for int_data");
620     FAIL_UNLESS(length[2] == 4, "length != 4");
621 
622     FAIL_UNLESS(small_data == o_small_data, "Wrong value for small_data");
623     FAIL_UNLESS(length[3] == 2, "length != 2");
624 
625     FAIL_UNLESS(big_data == o_big_data, "Wrong value for big_data");
626     FAIL_UNLESS(length[4] == 8, "length != 8");
627 
628     FAIL_UNLESS(real_data == o_real_data, "Wrong value for real_data");
629     FAIL_UNLESS(length[5] == 4, "length != 4");
630 
631     FAIL_UNLESS(double_data == o_double_data, "Wrong value for double_data");
632     FAIL_UNLESS(length[6] == 8, "length != 8");
633 
634     FAIL_UNLESS(strcmp(data, str_data) == 0, "Wrong value for data");
635     FAIL_UNLESS(length[1] == len, "length != len");
636 
637     o_int_data += 25;
638     o_small_data += 10;
639     o_big_data += 100;
640     o_real_data += 1;
641     o_double_data += 10.09;
642   }
643 
644   rc= mysql_stmt_fetch(stmt);
645   FAIL_UNLESS(rc == MYSQL_NO_DATA, "MYSQL_NO_DATA expected");
646 
647   mysql_stmt_close(stmt);
648   rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_prepare");
649   check_mysql_rc(rc, mysql);
650 
651   return OK;
652 }
653 
test_prepare_multi_statements(MYSQL * mysql)654 static int test_prepare_multi_statements(MYSQL *mysql)
655 {
656   MYSQL_STMT *stmt;
657   char query[MAX_TEST_QUERY_LENGTH];
658   int rc;
659 
660   strcpy(query, "select 1; select 'another value'");
661 
662   stmt= mysql_stmt_init(mysql);
663   FAIL_IF(!stmt, mysql_error(mysql));
664   rc= mysql_stmt_prepare(stmt, SL(query));
665   FAIL_IF(!rc, "Error expected");
666 
667   mysql_stmt_close(stmt);
668 
669   return OK;
670 }
671 
test_prepare_ext(MYSQL * mysql)672 static int test_prepare_ext(MYSQL *mysql)
673 {
674   MYSQL_STMT *stmt;
675   int        rc;
676   char       *sql;
677   int        nData= 1;
678   char       tData= 1;
679   short      sData= 10;
680   longlong   bData= 20;
681   int        rowcount= 0;
682   MYSQL_BIND my_bind[6];
683   char query[MAX_TEST_QUERY_LENGTH];
684 
685   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_ext");
686   check_mysql_rc(rc, mysql);
687 
688   sql= (char *)"CREATE TABLE test_prepare_ext"
689                "("
690                " c1  tinyint,"
691                " c2  smallint,"
692                " c3  mediumint,"
693                " c4  int,"
694                " c5  integer,"
695                " c6  bigint,"
696                " c7  float,"
697                " c8  double,"
698                " c9  double precision,"
699                " c10 real,"
700                " c11 decimal(7, 4),"
701                " c12 numeric(8, 4),"
702                " c13 date,"
703                " c14 datetime,"
704                " c15 timestamp,"
705                " c16 time,"
706                " c17 year,"
707                " c18 bit,"
708                " c19 bool,"
709                " c20 char,"
710                " c21 char(10),"
711                " c22 varchar(30),"
712                " c23 tinyblob,"
713                " c24 tinytext,"
714                " c25 blob,"
715                " c26 text,"
716                " c27 mediumblob,"
717                " c28 mediumtext,"
718                " c29 longblob,"
719                " c30 longtext,"
720                " c31 enum('one', 'two', 'three'),"
721                " c32 set('monday', 'tuesday', 'wednesday'))";
722 
723   rc= mysql_query(mysql, sql);
724   check_mysql_rc(rc, mysql);
725 
726   /* insert by prepare - all integers */
727   strcpy(query, "INSERT INTO test_prepare_ext(c1, c2, c3, c4, c5, c6) VALUES(?, ?, ?, ?, ?, ?)");
728   stmt= mysql_stmt_init(mysql);
729   FAIL_IF(!stmt, mysql_error(mysql));
730   rc= mysql_stmt_prepare(stmt, SL(query));
731   check_stmt_rc(rc, stmt);
732 
733   FAIL_IF(mysql_stmt_param_count(stmt) != 6, "Paramcount != 6");
734 
735   memset(my_bind, '\0', sizeof(my_bind));
736 
737   /*tinyint*/
738   my_bind[0].buffer_type= MYSQL_TYPE_TINY;
739   my_bind[0].buffer= (void *)&tData;
740 
741   /*smallint*/
742   my_bind[1].buffer_type= MYSQL_TYPE_SHORT;
743   my_bind[1].buffer= (void *)&sData;
744 
745   /*mediumint*/
746   my_bind[2].buffer_type= MYSQL_TYPE_LONG;
747   my_bind[2].buffer= (void *)&nData;
748 
749   /*int*/
750   my_bind[3].buffer_type= MYSQL_TYPE_LONG;
751   my_bind[3].buffer= (void *)&nData;
752 
753   /*integer*/
754   my_bind[4].buffer_type= MYSQL_TYPE_LONG;
755   my_bind[4].buffer= (void *)&nData;
756 
757   /*bigint*/
758   my_bind[5].buffer_type= MYSQL_TYPE_LONGLONG;
759   my_bind[5].buffer= (void *)&bData;
760 
761   rc= mysql_stmt_bind_param(stmt, my_bind);
762   check_stmt_rc(rc, stmt);
763 
764   /*
765   *  integer to integer
766   */
767   for (nData= 0; nData<10; nData++, tData++, sData++, bData++)
768   {
769     rc= mysql_stmt_execute(stmt);
770     check_stmt_rc(rc, stmt);
771   }
772   mysql_stmt_close(stmt);
773 
774   /* now fetch the results ..*/
775 
776   strcpy(query, "SELECT c1, c2, c3, c4, c5, c6 FROM test_prepare_ext");
777   stmt= mysql_stmt_init(mysql);
778   FAIL_IF(!stmt, mysql_error(mysql));
779   rc= mysql_stmt_prepare(stmt, SL(query));
780   check_stmt_rc(rc, stmt);
781 
782   /* get the result */
783   rc= mysql_stmt_execute(stmt);
784   check_stmt_rc(rc, stmt);
785 
786   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
787     rowcount++;
788 
789   FAIL_UNLESS(nData == rowcount, "Invalid rowcount");
790 
791   mysql_stmt_close(stmt);
792   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_ext");
793   check_mysql_rc(rc, mysql);
794 
795   return OK;
796 }
797 
test_prepare_alter(MYSQL * mysql)798 static int test_prepare_alter(MYSQL *mysql)
799 {
800   MYSQL_STMT  *stmt;
801   MYSQL       *mysql_new;
802   int         rc, id;
803   MYSQL_BIND  my_bind[1];
804   my_bool     is_null;
805   char        query[MAX_TEST_QUERY_LENGTH];
806 
807   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_alter");
808   check_mysql_rc(rc, mysql);
809 
810   rc= mysql_query(mysql, "CREATE TABLE test_prep_alter(id int, name char(20))");
811   check_mysql_rc(rc, mysql);
812 
813   rc= mysql_query(mysql, "INSERT INTO test_prep_alter values(10, 'venu'), (20, 'mysql')");
814   check_mysql_rc(rc, mysql);
815 
816   strcpy(query, "INSERT INTO test_prep_alter VALUES(?, 'monty')");
817   stmt= mysql_stmt_init(mysql);
818   FAIL_IF(!stmt, mysql_error(mysql));
819   rc= mysql_stmt_prepare(stmt, SL(query));
820   check_stmt_rc(rc, stmt);
821 
822   FAIL_IF(mysql_stmt_param_count(stmt) != 1, "Paramcount != 1");
823 
824   memset(my_bind, '\0', sizeof(my_bind));
825 
826   is_null= 0;
827   my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
828   my_bind[0].buffer= (void *)&id;
829   my_bind[0].is_null= &is_null;
830 
831   rc= mysql_stmt_bind_param(stmt, my_bind);
832   check_stmt_rc(rc, stmt);
833 
834   id= 30;
835   rc= mysql_stmt_execute(stmt);
836   check_stmt_rc(rc, stmt);
837 
838   mysql_new= mysql_init(NULL);
839   FAIL_IF(!mysql_new, "mysql_init failed");
840   FAIL_IF(!(my_test_connect(mysql_new, hostname, username, password,
841                            schema, port, socketname, 0)), "my_test_connect failed");
842   rc= mysql_query(mysql_new, "ALTER TABLE test_prep_alter change id id_new varchar(20)");
843   diag("Error: %d %s", mysql_errno(mysql_new), mysql_error(mysql_new));
844   check_mysql_rc(rc, mysql_new);
845   mysql_close(mysql_new);
846 
847   is_null= 1;
848   rc= mysql_stmt_execute(stmt);
849   check_stmt_rc(rc, stmt);
850 
851   rc= my_stmt_result(mysql, "SELECT * FROM test_prep_alter");
852   FAIL_UNLESS(rc == 4, "rowcount != 4");
853 
854   mysql_stmt_close(stmt);
855   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_alter");
856   check_mysql_rc(rc, mysql);
857 
858   return OK;
859 }
860 
test_prepare_resultset(MYSQL * mysql)861 static int test_prepare_resultset(MYSQL *mysql)
862 {
863   MYSQL_STMT *stmt;
864   int        rc;
865   MYSQL_RES  *result;
866   char       query[MAX_TEST_QUERY_LENGTH];
867 
868   rc= mysql_autocommit(mysql, TRUE);
869   check_mysql_rc(rc, mysql);
870 
871   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_resultset");
872   check_mysql_rc(rc, mysql);
873 
874   rc= mysql_query(mysql, "CREATE TABLE test_prepare_resultset(id int, \
875                                 name varchar(50), extra double)");
876   check_mysql_rc(rc, mysql);
877 
878   stmt= mysql_stmt_init(mysql);
879   strcpy(query, "SELECT * FROM test_prepare_resultset");
880   rc= mysql_stmt_prepare(stmt, SL(query));
881   check_stmt_rc(rc, stmt);
882 
883   FAIL_IF(mysql_stmt_param_count(stmt), "Paramcount != 0");
884 
885   result= mysql_stmt_result_metadata(stmt);
886   FAIL_IF(!result, "Invalid resultset");
887   mysql_free_result(result);
888   mysql_stmt_close(stmt);
889   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_resultset");
890   check_mysql_rc(rc, mysql);
891 
892   return OK;
893 }
894 
895 /* Test the direct query execution in the middle of open stmts */
896 
test_open_direct(MYSQL * mysql)897 static int test_open_direct(MYSQL *mysql)
898 {
899   MYSQL_STMT  *stmt;
900   MYSQL_RES   *result;
901   int         rc;
902   char        query[MAX_TEST_QUERY_LENGTH];
903 
904   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_open_direct");
905   check_mysql_rc(rc, mysql);
906 
907   rc= mysql_query(mysql, "CREATE TABLE test_open_direct(id int, name char(6))");
908   check_mysql_rc(rc, mysql);
909 
910   strcpy(query, "INSERT INTO test_open_direct values(10, 'mysql')");
911   stmt= mysql_stmt_init(mysql);
912   FAIL_IF(!stmt, mysql_error(mysql));
913   rc= mysql_stmt_prepare(stmt, SL(query));
914   check_stmt_rc(rc, stmt);
915 
916   rc= mysql_query(mysql, "SELECT * FROM test_open_direct");
917 
918   result= mysql_store_result(mysql);
919   FAIL_IF(!result, "invalid resultset");
920 
921   FAIL_IF(mysql_num_rows(result), "rowcount != 0");
922   mysql_free_result(result);
923 
924   rc= mysql_stmt_execute(stmt);
925   check_stmt_rc(rc, stmt);
926 
927   FAIL_IF(mysql_stmt_affected_rows(stmt) != 1, "affected rows != 1");
928 
929   rc= mysql_query(mysql, "SELECT * FROM test_open_direct");
930   check_mysql_rc(rc, mysql);
931 
932   result= mysql_store_result(mysql);
933   FAIL_IF(!result, "invalid resultset");
934 
935   FAIL_IF(mysql_num_rows(result) != 1, "rowcount != 1");
936   mysql_free_result(result);
937 
938   rc= mysql_stmt_execute(stmt);
939   check_stmt_rc(rc, stmt);
940 
941   FAIL_IF(mysql_stmt_affected_rows(stmt) != 1, "affected rows != 1");
942 
943   rc= mysql_query(mysql, "SELECT * FROM test_open_direct");
944   check_mysql_rc(rc, mysql);
945 
946   result= mysql_store_result(mysql);
947   FAIL_IF(!result, "Invalid resultset");
948   FAIL_IF(mysql_num_rows(result) != 2, "rowcount != 2");
949 
950   mysql_free_result(result);
951 
952   mysql_stmt_close(stmt);
953 
954   /* run a direct query in the middle of a fetch */
955 
956   strcpy(query, "SELECT * FROM test_open_direct");
957   stmt= mysql_stmt_init(mysql);
958   FAIL_IF(!stmt, mysql_error(mysql));
959   rc= mysql_stmt_prepare(stmt, SL(query));
960   check_stmt_rc(rc, stmt);
961 
962   rc= mysql_stmt_execute(stmt);
963   check_stmt_rc(rc, stmt);
964 
965   rc= mysql_stmt_fetch(stmt);
966   check_stmt_rc(rc, stmt);
967 
968   rc= mysql_query(mysql, "INSERT INTO test_open_direct(id) VALUES(20)");
969   FAIL_IF(!rc, "Error expected");
970 
971   rc= mysql_stmt_close(stmt);
972   check_stmt_rc(rc, stmt);
973 
974   rc= mysql_query(mysql, "INSERT INTO test_open_direct(id) VALUES(20)");
975   check_mysql_rc(rc, mysql);
976 
977   /* run a direct query with store result */
978   stmt= mysql_stmt_init(mysql);
979   FAIL_IF(!stmt, mysql_error(mysql));
980   rc= mysql_stmt_prepare(stmt, SL(query));
981   check_stmt_rc(rc, stmt);
982 
983   rc= mysql_stmt_execute(stmt);
984   check_stmt_rc(rc, stmt);
985 
986   rc= mysql_stmt_store_result(stmt);
987   check_stmt_rc(rc, stmt);
988 
989   rc= mysql_stmt_fetch(stmt);
990   check_stmt_rc(rc, stmt);
991 
992   rc= mysql_query(mysql, "drop table test_open_direct");
993   check_mysql_rc(rc, mysql);
994 
995   rc= mysql_stmt_close(stmt);
996   check_stmt_rc(rc, stmt);
997 
998   return OK;
999 }
1000 
test_select_show(MYSQL * mysql)1001 static int test_select_show(MYSQL *mysql)
1002 {
1003   MYSQL_STMT *stmt;
1004   int        rc;
1005   char query[MAX_TEST_QUERY_LENGTH];
1006   int        rowcount;
1007 
1008   rc= mysql_autocommit(mysql, TRUE);
1009   check_mysql_rc(rc, mysql);
1010 
1011   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_show");
1012   check_mysql_rc(rc, mysql);
1013 
1014   rc= mysql_query(mysql, "CREATE TABLE test_show(id int(4) NOT NULL primary "
1015                          " key, name char(2))");
1016   check_mysql_rc(rc, mysql);
1017 
1018   strcpy(query, "show columns from test_show");
1019   stmt= mysql_stmt_init(mysql);
1020   FAIL_IF(!stmt, mysql_stmt_error(stmt));
1021   rc= mysql_stmt_prepare(stmt, SL(query));
1022   check_stmt_rc(rc, stmt);
1023 
1024   FAIL_IF(mysql_stmt_param_count(stmt) != 0, "Paramcount != 0");
1025 
1026   rc= mysql_stmt_execute(stmt);
1027   check_stmt_rc(rc, stmt);
1028 
1029   rowcount= 0;
1030   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
1031     rowcount++;
1032   FAIL_IF(rowcount != 2, "rowcount != 2");
1033 
1034   mysql_stmt_close(stmt);
1035 
1036   strcpy(query, "show tables from mysql like ?");
1037   stmt= mysql_stmt_init(mysql);
1038   FAIL_IF(!stmt, mysql_error(mysql));
1039   rc= mysql_stmt_prepare(stmt, SL(query));
1040   FAIL_IF(!rc, "Error expected");
1041 
1042   strcpy(query, "show tables like \'test_show\'");
1043   FAIL_IF(!stmt, mysql_error(mysql));
1044   rc= mysql_stmt_prepare(stmt, SL(query));
1045   check_stmt_rc(rc, stmt);
1046 
1047   rc= mysql_stmt_execute(stmt);
1048   check_stmt_rc(rc, stmt);
1049 
1050   rowcount= 0;
1051   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
1052     rowcount++;
1053   FAIL_IF(rowcount != 1, "rowcount != 1");
1054   mysql_stmt_close(stmt);
1055 
1056   strcpy(query, "describe test_show");
1057   stmt= mysql_stmt_init(mysql);
1058   FAIL_IF(!stmt, mysql_error(mysql));
1059   rc= mysql_stmt_prepare(stmt, SL(query));
1060   check_stmt_rc(rc, stmt);
1061 
1062   rc= mysql_stmt_execute(stmt);
1063   check_stmt_rc(rc, stmt);
1064 
1065   rowcount= 0;
1066   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
1067     rowcount++;
1068   FAIL_IF(rowcount != 2, "rowcount != 2");
1069   mysql_stmt_close(stmt);
1070 
1071   strcpy(query, "show keys from test_show");
1072   stmt= mysql_stmt_init(mysql);
1073   FAIL_IF(!stmt, mysql_error(mysql));
1074   rc= mysql_stmt_prepare(stmt, SL(query));
1075   check_stmt_rc(rc, stmt);
1076 
1077   rc= mysql_stmt_execute(stmt);
1078   check_stmt_rc(rc, stmt);
1079 
1080   rowcount= 0;
1081   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
1082     rowcount++;
1083   FAIL_IF(rowcount != 1, "rowcount != 1");
1084 
1085   mysql_stmt_close(stmt);
1086   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_show");
1087   check_mysql_rc(rc, mysql);
1088 
1089   return OK;
1090 }
1091 
test_simple_update(MYSQL * mysql)1092 static int test_simple_update(MYSQL *mysql)
1093 {
1094   MYSQL_STMT *stmt;
1095   int        rc;
1096   char       szData[25];
1097   int        nData= 1;
1098   MYSQL_RES  *result;
1099   MYSQL_BIND my_bind[2];
1100   ulong      length[2];
1101   int        rowcount= 0;
1102   char query[MAX_TEST_QUERY_LENGTH];
1103 
1104   rc= mysql_autocommit(mysql, TRUE);
1105   check_mysql_rc(rc, mysql);
1106 
1107   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_update");
1108   check_mysql_rc(rc, mysql);
1109 
1110   rc= mysql_query(mysql, "CREATE TABLE test_update(col1 int, "
1111                          " col2 varchar(50), col3 int )");
1112   check_mysql_rc(rc, mysql);
1113 
1114   rc= mysql_query(mysql, "INSERT INTO test_update VALUES(1, 'MySQL', 100)");
1115   check_mysql_rc(rc, mysql);
1116 
1117   FAIL_IF(mysql_affected_rows(mysql) != 1, "Affected rows != 1");
1118 
1119   rc= mysql_commit(mysql);
1120   check_mysql_rc(rc, mysql);
1121 
1122   /* insert by prepare */
1123   strcpy(query, "UPDATE test_update SET col2= ? WHERE col1= ?");
1124   stmt= mysql_stmt_init(mysql);
1125   FAIL_IF(!stmt, mysql_error(mysql));
1126   rc= mysql_stmt_prepare(stmt, SL(query));
1127   check_stmt_rc(rc, stmt);
1128 
1129   FAIL_IF(mysql_stmt_param_count(stmt) != 2, "Paramcount != 2");
1130 
1131   memset(my_bind, '\0', sizeof(my_bind));
1132 
1133   nData= 1;
1134   my_bind[0].buffer_type= MYSQL_TYPE_STRING;
1135   my_bind[0].buffer= szData;                /* string data */
1136   my_bind[0].buffer_length= sizeof(szData);
1137   my_bind[0].length= &length[0];
1138   length[0]= sprintf(szData, "updated-data");
1139 
1140   my_bind[1].buffer= (void *) &nData;
1141   my_bind[1].buffer_type= MYSQL_TYPE_LONG;
1142 
1143   rc= mysql_stmt_bind_param(stmt, my_bind);
1144   check_stmt_rc(rc, stmt);
1145 
1146   rc= mysql_stmt_execute(stmt);
1147   check_stmt_rc(rc, stmt);
1148   FAIL_IF(mysql_stmt_affected_rows(stmt) != 1, "Affected_rows != 1");
1149 
1150   mysql_stmt_close(stmt);
1151 
1152   /* now fetch the results ..*/
1153   rc= mysql_commit(mysql);
1154   check_mysql_rc(rc, mysql);
1155 
1156   /* test the results now, only one row should exist */
1157   rc= mysql_query(mysql, "SELECT * FROM test_update");
1158   check_mysql_rc(rc, mysql);
1159 
1160   /* get the result */
1161   result= mysql_store_result(mysql);
1162   FAIL_IF(!result, "Invalid resultset");
1163 
1164   while (mysql_fetch_row(result))
1165     rowcount++;
1166 
1167   FAIL_IF(rowcount != 1, "rowcount != 1");
1168 
1169   mysql_free_result(result);
1170   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_update");
1171   check_mysql_rc(rc, mysql);
1172 
1173   return OK;
1174 }
1175 
1176 
1177 /* Test simple long data handling */
1178 
test_long_data(MYSQL * mysql)1179 static int test_long_data(MYSQL *mysql)
1180 {
1181   MYSQL_STMT *stmt;
1182   int        rc, int_data;
1183   char       *data= NullS;
1184   MYSQL_RES  *result;
1185   MYSQL_BIND my_bind[3];
1186   int        rowcount;
1187   char query[MAX_TEST_QUERY_LENGTH];
1188 
1189   rc= mysql_autocommit(mysql, TRUE);
1190   check_mysql_rc(rc, mysql);
1191 
1192   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data");
1193   check_mysql_rc(rc, mysql);
1194 
1195   rc= mysql_query(mysql, "CREATE TABLE test_long_data(col1 int, "
1196                          "      col2 long varchar, col3 long varbinary)");
1197   check_mysql_rc(rc, mysql);
1198 
1199   strcpy(query, "INSERT INTO test_long_data(col1, col2) VALUES(?)");
1200   stmt= mysql_stmt_init(mysql);
1201   FAIL_IF(!stmt, mysql_error(mysql));
1202   rc= mysql_stmt_prepare(stmt, SL(query));
1203   FAIL_IF(!rc, "Error expected");
1204   rc= mysql_stmt_close(stmt);
1205   check_stmt_rc(rc, stmt);
1206 
1207   strcpy(query, "INSERT INTO test_long_data(col1, col2, col3) VALUES(?, ?, ?)");
1208   stmt= mysql_stmt_init(mysql);
1209   FAIL_IF(!stmt, mysql_error(mysql));
1210   rc= mysql_stmt_prepare(stmt, SL(query));
1211   check_stmt_rc(rc, stmt);
1212 
1213   FAIL_IF(mysql_stmt_param_count(stmt) != 3, "Paramcount != 3");
1214 
1215   memset(my_bind, '\0', sizeof(my_bind));
1216 
1217   my_bind[0].buffer= (void *)&int_data;
1218   my_bind[0].buffer_type= MYSQL_TYPE_LONG;
1219 
1220   my_bind[1].buffer_type= MYSQL_TYPE_STRING;
1221 
1222   my_bind[2]= my_bind[1];
1223   rc= mysql_stmt_bind_param(stmt, my_bind);
1224   check_stmt_rc(rc, stmt);
1225 
1226   int_data= 999;
1227   data= (char *)"Michael";
1228 
1229   /* supply data in pieces */
1230   rc= mysql_stmt_send_long_data(stmt, 1, SL(data));
1231   check_stmt_rc(rc, stmt);
1232   data= (char *)" 'Monty' Widenius";
1233   rc= mysql_stmt_send_long_data(stmt, 1, SL(data));
1234   check_stmt_rc(rc, stmt);
1235   rc= mysql_stmt_send_long_data(stmt, 2, "Venu (venu@mysql.com)", 4);
1236   check_stmt_rc(rc, stmt);
1237 
1238   /* execute */
1239   rc= mysql_stmt_execute(stmt);
1240   check_stmt_rc(rc, stmt);
1241 
1242   rc= mysql_commit(mysql);
1243   check_mysql_rc(rc, mysql);
1244 
1245   /* now fetch the results ..*/
1246   rc= mysql_query(mysql, "SELECT * FROM test_long_data");
1247   check_mysql_rc(rc, mysql);
1248 
1249   /* get the result */
1250   result= mysql_store_result(mysql);
1251   FAIL_IF(!result, "Invalid result set");
1252 
1253   rowcount= 0;
1254   while (mysql_fetch_row(result))
1255     rowcount++;
1256   FAIL_IF(rowcount != 1, "rowcount != 1");
1257   mysql_free_result(result);
1258 
1259   if (verify_col_data(mysql, "test_long_data", "col1", "999"))
1260     goto error;
1261   if (verify_col_data(mysql, "test_long_data", "col2", "Michael 'Monty' Widenius"))
1262     goto error;
1263   if (verify_col_data(mysql, "test_long_data", "col3", "Venu"))
1264     goto error;
1265 
1266   mysql_stmt_close(stmt);
1267   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data");
1268   check_mysql_rc(rc, mysql);
1269   return OK;
1270 
1271 error:
1272   mysql_stmt_close(stmt);
1273   return FAIL;
1274 }
1275 
1276 
1277 /* Test long data (string) handling */
1278 
test_long_data_str(MYSQL * mysql)1279 static int test_long_data_str(MYSQL *mysql)
1280 {
1281   MYSQL_STMT *stmt;
1282   int        rc, i, rowcount= 0;
1283   char       data[255];
1284   long       length;
1285   ulong      length1;
1286   MYSQL_RES  *result;
1287   MYSQL_BIND my_bind[2];
1288   my_bool    is_null[2];
1289   char query[MAX_TEST_QUERY_LENGTH];
1290 
1291   rc= mysql_autocommit(mysql, TRUE);
1292   check_mysql_rc(rc, mysql);
1293 
1294   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data_str");
1295   check_mysql_rc(rc, mysql);
1296 
1297   rc= mysql_query(mysql, "CREATE TABLE test_long_data_str(id int, longstr long varchar)");
1298   check_mysql_rc(rc, mysql);
1299 
1300   strcpy(query, "INSERT INTO test_long_data_str VALUES(?, ?)");
1301   stmt= mysql_stmt_init(mysql);
1302   FAIL_IF(!stmt, mysql_error(mysql));
1303   rc= mysql_stmt_prepare(stmt, SL(query));
1304   check_stmt_rc(rc, stmt);
1305 
1306   FAIL_IF(mysql_stmt_param_count(stmt) != 2, "Paramcount != 2");
1307 
1308   memset(my_bind, '\0', sizeof(my_bind));
1309 
1310   my_bind[0].buffer= (void *)&length;
1311   my_bind[0].buffer_type= MYSQL_TYPE_LONG;
1312   my_bind[0].is_null= &is_null[0];
1313   is_null[0]= 0;
1314   length= 0;
1315 
1316   my_bind[1].buffer= data;                          /* string data */
1317   my_bind[1].buffer_type= MYSQL_TYPE_STRING;
1318   my_bind[1].length= &length1;
1319   my_bind[1].is_null= &is_null[1];
1320   is_null[1]= 0;
1321   rc= mysql_stmt_bind_param(stmt, my_bind);
1322   check_stmt_rc(rc, stmt);
1323 
1324   length= 40;
1325   strcpy(data, "MySQL AB");
1326 
1327   /* supply data in pieces */
1328   for(i= 0; i < 4; i++)
1329   {
1330     rc= mysql_stmt_send_long_data(stmt, 1, (char *)data, 5);
1331     check_stmt_rc(rc, stmt);
1332   }
1333   /* execute */
1334   rc= mysql_stmt_execute(stmt);
1335   check_stmt_rc(rc, stmt);
1336 
1337   mysql_stmt_close(stmt);
1338 
1339   rc= mysql_commit(mysql);
1340   check_mysql_rc(rc, mysql);
1341 
1342   /* now fetch the results ..*/
1343   rc= mysql_query(mysql, "SELECT LENGTH(longstr), longstr FROM test_long_data_str");
1344   check_mysql_rc(rc, mysql);
1345 
1346   /* get the result */
1347   result= mysql_store_result(mysql);
1348   FAIL_IF(!result, "Invalid result set");
1349 
1350   while (mysql_fetch_row(result))
1351     rowcount++;
1352   FAIL_IF(rowcount != 1, "rowcount != 1");
1353 
1354   mysql_free_result(result);
1355 
1356   sprintf(data, "%d", i*5);
1357   if (verify_col_data(mysql, "test_long_data_str", "LENGTH(longstr)", data))
1358     goto error;
1359   strcpy(data, "MySQLMySQLMySQLMySQL");
1360   if (verify_col_data(mysql, "test_long_data_str", "longstr", data))
1361     goto error;
1362 
1363   rc= mysql_query(mysql, "DROP TABLE test_long_data_str");
1364   check_mysql_rc(rc, mysql);
1365 
1366   return OK;
1367 
1368 error:
1369   rc= mysql_query(mysql, "DROP TABLE test_long_data_str");
1370   check_mysql_rc(rc, mysql);
1371   return FAIL;
1372 }
1373 
1374 
1375 /* Test long data (string) handling */
1376 
test_long_data_str1(MYSQL * mysql)1377 static int test_long_data_str1(MYSQL *mysql)
1378 {
1379   MYSQL_STMT *stmt;
1380   int        rc, i, rowcount= 0;
1381   char       data[255];
1382   long       length;
1383   unsigned long max_blob_length, blob_length, length1;
1384   my_bool    true_value;
1385   MYSQL_RES  *result;
1386   MYSQL_BIND my_bind[2];
1387   MYSQL_FIELD *field;
1388   char query[MAX_TEST_QUERY_LENGTH];
1389 
1390   rc= mysql_autocommit(mysql, TRUE);
1391   check_mysql_rc(rc, mysql);
1392 
1393   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data_str");
1394   check_mysql_rc(rc, mysql);
1395 
1396   rc= mysql_query(mysql, "CREATE TABLE test_long_data_str(longstr long varchar, blb long varbinary)");
1397   check_mysql_rc(rc, mysql);
1398 
1399   strcpy(query, "INSERT INTO test_long_data_str VALUES(?, ?)");
1400   stmt= mysql_stmt_init(mysql);
1401   FAIL_IF(!stmt, mysql_error(mysql));
1402   rc= mysql_stmt_prepare(stmt, SL(query));
1403   check_stmt_rc(rc, stmt);
1404 
1405   FAIL_IF(mysql_stmt_param_count(stmt) != 2, "Paramcount != 2");
1406 
1407   memset(my_bind, '\0', sizeof(my_bind));
1408 
1409   my_bind[0].buffer= data;            /* string data */
1410   my_bind[0].buffer_length= sizeof(data);
1411   my_bind[0].length= (unsigned long *)&length1;
1412   my_bind[0].buffer_type= MYSQL_TYPE_STRING;
1413   length1= 0;
1414 
1415   my_bind[1]= my_bind[0];
1416   my_bind[1].buffer_type= MYSQL_TYPE_BLOB;
1417 
1418   rc= mysql_stmt_bind_param(stmt, my_bind);
1419   check_stmt_rc(rc, stmt);
1420   length= sprintf(data, "MySQL AB");
1421 
1422   /* supply data in pieces */
1423   for (i= 0; i < 3; i++)
1424   {
1425     rc= mysql_stmt_send_long_data(stmt, 0, data, length);
1426     check_stmt_rc(rc, stmt);
1427 
1428     rc= mysql_stmt_send_long_data(stmt, 1, data, 2);
1429     check_stmt_rc(rc, stmt);
1430   }
1431 
1432   /* execute */
1433   rc= mysql_stmt_execute(stmt);
1434   check_stmt_rc(rc, stmt);
1435 
1436   mysql_stmt_close(stmt);
1437 
1438   rc= mysql_commit(mysql);
1439   check_mysql_rc(rc, mysql);
1440 
1441   /* now fetch the results ..*/
1442   rc= mysql_query(mysql, "SELECT LENGTH(longstr), longstr, LENGTH(blb), blb FROM test_long_data_str");
1443   check_mysql_rc(rc, mysql);
1444 
1445   /* get the result */
1446   result= mysql_store_result(mysql);
1447 
1448   mysql_field_seek(result, 1);
1449   field= mysql_fetch_field(result);
1450   max_blob_length= field->max_length;
1451 
1452   FAIL_IF(!result, "Invalid result set");
1453 
1454   while (mysql_fetch_row(result))
1455     rowcount++;
1456 
1457   FAIL_IF(rowcount != 1, "rowcount != 1");
1458   mysql_free_result(result);
1459 
1460   sprintf(data, "%ld", (long)i*length);
1461   if (verify_col_data(mysql, "test_long_data_str", "length(longstr)", data))
1462     return FAIL;
1463 
1464   sprintf(data, "%d", i*2);
1465   if (verify_col_data(mysql, "test_long_data_str", "length(blb)", data))
1466     return FAIL;
1467 
1468   /* Test length of field->max_length */
1469   strcpy(query, "SELECT * from test_long_data_str");
1470   stmt= mysql_stmt_init(mysql);
1471   FAIL_IF(!stmt, mysql_error(mysql));
1472   rc= mysql_stmt_prepare(stmt, SL(query));
1473   check_stmt_rc(rc, stmt);
1474 
1475   FAIL_IF(mysql_stmt_param_count(stmt) != 0, "Paramcount != 0");
1476 
1477   rc= mysql_stmt_execute(stmt);
1478   check_stmt_rc(rc, stmt);
1479 
1480   rc= mysql_stmt_store_result(stmt);
1481   check_stmt_rc(rc, stmt);
1482 
1483   result= mysql_stmt_result_metadata(stmt);
1484   field= mysql_fetch_fields(result);
1485 
1486   /* First test what happens if STMT_ATTR_UPDATE_MAX_LENGTH is not used */
1487   FAIL_IF(field->max_length != 0, "field->max_length != 0");
1488   mysql_free_result(result);
1489 
1490   /* Enable updating of field->max_length */
1491   true_value= 1;
1492   mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*) &true_value);
1493   rc= mysql_stmt_execute(stmt);
1494   check_stmt_rc(rc, stmt);
1495 
1496   rc= mysql_stmt_store_result(stmt);
1497   check_stmt_rc(rc, stmt);
1498 
1499   result= mysql_stmt_result_metadata(stmt);
1500   field= mysql_fetch_fields(result);
1501 
1502   diag("max_length: %lu  max_blob_length: %lu", (unsigned long)field->max_length, (unsigned long)max_blob_length);
1503   FAIL_UNLESS(field->max_length == max_blob_length, "field->max_length != max_blob_length");
1504 
1505   /* Fetch results into a data buffer that is smaller than data */
1506   memset(my_bind, '\0', sizeof(*my_bind));
1507   my_bind[0].buffer_type= MYSQL_TYPE_BLOB;
1508   my_bind[0].buffer= (void *) &data; /* this buffer won't be altered */
1509   my_bind[0].buffer_length= 16;
1510   my_bind[0].length= (unsigned long *)&blob_length;
1511   my_bind[0].error= &my_bind[0].error_value;
1512   rc= mysql_stmt_bind_result(stmt, my_bind);
1513   data[16]= 0;
1514 
1515   rc= mysql_stmt_fetch(stmt);
1516   FAIL_UNLESS(rc == MYSQL_DATA_TRUNCATED, "truncation expected");
1517   FAIL_UNLESS(my_bind[0].error_value, "No error value");
1518   FAIL_UNLESS(strlen(data) == 16, "Invalid string length");
1519   FAIL_UNLESS(blob_length == max_blob_length, "blob_length != max_blob_length");
1520 
1521   /* Fetch all data */
1522   memset((my_bind+1), '\0', sizeof(*my_bind));
1523   my_bind[1].buffer_type= MYSQL_TYPE_BLOB;
1524   my_bind[1].buffer= (void *) &data; /* this buffer won't be altered */
1525   my_bind[1].buffer_length= sizeof(data);
1526   my_bind[1].length= (unsigned long *)&blob_length;
1527   memset(data, '\0', sizeof(data));
1528   mysql_stmt_fetch_column(stmt, my_bind+1, 0, 0);
1529   FAIL_UNLESS(strlen(data) == max_blob_length, "strlen(data) != max_blob_length");
1530 
1531   mysql_free_result(result);
1532   mysql_stmt_close(stmt);
1533 
1534   /* Drop created table */
1535   rc= mysql_query(mysql, "DROP TABLE test_long_data_str");
1536   check_mysql_rc(rc, mysql);
1537 
1538   return OK;
1539 }
1540 
1541 
1542 /* Test long data (binary) handling */
1543 
test_long_data_bin(MYSQL * mysql)1544 static int test_long_data_bin(MYSQL *mysql)
1545 {
1546   MYSQL_STMT *stmt;
1547   int        rc, rowcount= 0;
1548   char       data[255];
1549   long       length;
1550   MYSQL_RES  *result;
1551   MYSQL_BIND my_bind[2];
1552   char query[MAX_TEST_QUERY_LENGTH];
1553 
1554 
1555   rc= mysql_autocommit(mysql, TRUE);
1556   check_mysql_rc(rc, mysql);
1557 
1558   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data_bin");
1559   check_mysql_rc(rc, mysql);
1560 
1561   rc= mysql_query(mysql, "CREATE TABLE test_long_data_bin(id int, longbin long varbinary)");
1562   check_mysql_rc(rc, mysql);
1563 
1564   strcpy(query, "INSERT INTO test_long_data_bin VALUES(?, ?)");
1565   stmt= mysql_stmt_init(mysql);
1566   FAIL_IF(!stmt, mysql_error(mysql));
1567   rc= mysql_stmt_prepare(stmt, SL(query));
1568   check_stmt_rc(rc, stmt);
1569 
1570   FAIL_IF(mysql_stmt_param_count(stmt) != 2, "Paramcount != 2");
1571 
1572   memset(my_bind, '\0', sizeof(my_bind));
1573 
1574   my_bind[0].buffer= (void *)&length;
1575   my_bind[0].buffer_type= MYSQL_TYPE_LONG;
1576   length= 0;
1577 
1578   my_bind[1].buffer= data;           /* string data */
1579   my_bind[1].buffer_type= MYSQL_TYPE_LONG_BLOB;
1580   rc= mysql_stmt_bind_param(stmt, my_bind);
1581   check_stmt_rc(rc, stmt);
1582 
1583   length= 10;
1584   strcpy(data, "MySQL AB");
1585 
1586   /* supply data in pieces */
1587   {
1588     int i;
1589     for (i= 0; i < 100; i++)
1590     {
1591       rc= mysql_stmt_send_long_data(stmt, 1, (char *)data, 4);
1592       check_stmt_rc(rc, stmt);
1593     }
1594   }
1595   /* execute */
1596   rc= mysql_stmt_execute(stmt);
1597   check_stmt_rc(rc, stmt);
1598 
1599   mysql_stmt_close(stmt);
1600 
1601   rc= mysql_commit(mysql);
1602   check_mysql_rc(rc, mysql);
1603 
1604   /* now fetch the results ..*/
1605   rc= mysql_query(mysql, "SELECT LENGTH(longbin), longbin FROM test_long_data_bin");
1606   check_mysql_rc(rc, mysql);
1607 
1608   /* get the result */
1609   result= mysql_store_result(mysql);
1610   FAIL_IF(!result, "Invalid result set");
1611 
1612   while (mysql_fetch_row(result))
1613     rowcount++;
1614 
1615   FAIL_IF(rowcount != 1, "rowcount != 1");
1616   mysql_free_result(result);
1617 
1618   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data_bin");
1619   check_mysql_rc(rc, mysql);
1620   return OK;
1621 }
1622 
1623 
1624 /* Test simple delete */
1625 
test_simple_delete(MYSQL * mysql)1626 static int test_simple_delete(MYSQL *mysql)
1627 {
1628   MYSQL_STMT *stmt;
1629   int        rc, rowcount= 0;
1630   char       szData[30]= {0};
1631   int        nData= 1;
1632   MYSQL_RES  *result;
1633   MYSQL_BIND my_bind[2];
1634   ulong length[2];
1635   char query[MAX_TEST_QUERY_LENGTH];
1636 
1637   rc= mysql_autocommit(mysql, TRUE);
1638   check_mysql_rc(rc, mysql);
1639 
1640   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_simple_delete");
1641   check_mysql_rc(rc, mysql);
1642 
1643   rc= mysql_query(mysql, "CREATE TABLE test_simple_delete(col1 int, \
1644                                 col2 varchar(50), col3 int )");
1645   check_mysql_rc(rc, mysql);
1646 
1647   rc= mysql_query(mysql, "INSERT INTO test_simple_delete VALUES(1, 'MySQL', 100)");
1648   check_mysql_rc(rc, mysql);
1649 
1650   FAIL_IF(mysql_affected_rows(mysql) != 1, "Affected rows != 1");
1651 
1652   rc= mysql_commit(mysql);
1653   check_mysql_rc(rc, mysql);
1654 
1655   /* insert by prepare */
1656   strcpy(query, "DELETE FROM test_simple_delete WHERE col1= ? AND "
1657                 "CONVERT(col2 USING utf8)= ? AND col3= 100");
1658   stmt= mysql_stmt_init(mysql);
1659   FAIL_IF(!stmt, mysql_error(mysql));
1660   rc= mysql_stmt_prepare(stmt, SL(query));
1661   check_stmt_rc(rc, stmt);
1662 
1663   FAIL_IF(mysql_stmt_param_count(stmt) != 2, "Paramcount != 2");
1664 
1665   memset(my_bind, '\0', sizeof(my_bind));
1666 
1667   nData= 1;
1668   strcpy(szData, "MySQL");
1669   my_bind[1].buffer_type= MYSQL_TYPE_STRING;
1670   my_bind[1].buffer= szData;               /* string data */
1671   my_bind[1].buffer_length= sizeof(szData);
1672   my_bind[1].length= &length[1];
1673   length[1]= 5;
1674 
1675   my_bind[0].buffer= (void *)&nData;
1676   my_bind[0].buffer_type= MYSQL_TYPE_LONG;
1677 
1678   rc= mysql_stmt_bind_param(stmt, my_bind);
1679   check_stmt_rc(rc, stmt);
1680 
1681   rc= mysql_stmt_execute(stmt);
1682   check_stmt_rc(rc, stmt);
1683 
1684   FAIL_IF(mysql_stmt_affected_rows(stmt) != 1, "Affected rows != 1");
1685 
1686   mysql_stmt_close(stmt);
1687 
1688   /* now fetch the results ..*/
1689   rc= mysql_commit(mysql);
1690   check_mysql_rc(rc, mysql);
1691 
1692   /* test the results now, only one row should exist */
1693   rc= mysql_query(mysql, "SELECT * FROM test_simple_delete");
1694   check_mysql_rc(rc, mysql);
1695 
1696   /* get the result */
1697   result= mysql_store_result(mysql);
1698   FAIL_IF(!result, "Invalid result set");
1699 
1700   while (mysql_fetch_row(result))
1701     rowcount++;
1702 
1703   FAIL_IF(rowcount, "rowcount > 0");
1704   mysql_free_result(result);
1705   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_simple_delete");
1706   check_mysql_rc(rc, mysql);
1707 
1708   return OK;
1709 }
1710 
test_update(MYSQL * mysql)1711 static int test_update(MYSQL *mysql)
1712 {
1713   MYSQL_STMT *stmt;
1714   int        rc;
1715   char       szData[25];
1716   int        nData= 1, rowcount= 0;
1717   MYSQL_RES  *result;
1718   MYSQL_BIND my_bind[2];
1719   ulong length[2];
1720   char query[MAX_TEST_QUERY_LENGTH];
1721 
1722   rc= mysql_autocommit(mysql, TRUE);
1723   check_mysql_rc(rc, mysql);
1724 
1725   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_update");
1726   check_mysql_rc(rc, mysql);
1727 
1728   rc= mysql_query(mysql, "CREATE TABLE test_update("
1729                                "col1 int primary key auto_increment, "
1730                                "col2 varchar(50), col3 int )");
1731   check_mysql_rc(rc, mysql);
1732 
1733   strcpy(query, "INSERT INTO test_update(col2, col3) VALUES(?, ?)");
1734   stmt= mysql_stmt_init(mysql);
1735   FAIL_IF(!stmt, mysql_error(mysql));
1736   rc= mysql_stmt_prepare(stmt, SL(query));
1737   check_stmt_rc(rc, stmt);
1738 
1739   FAIL_IF(mysql_stmt_param_count(stmt) != 2, "Paramcount != 2");
1740 
1741   memset(my_bind, '\0', sizeof(my_bind));
1742 
1743   /* string data */
1744   my_bind[0].buffer_type= MYSQL_TYPE_STRING;
1745   my_bind[0].buffer= szData;
1746   my_bind[0].buffer_length= sizeof(szData);
1747   my_bind[0].length= &length[0];
1748   length[0]= sprintf(szData, "inserted-data");
1749 
1750   my_bind[1].buffer= (void *)&nData;
1751   my_bind[1].buffer_type= MYSQL_TYPE_LONG;
1752 
1753   rc= mysql_stmt_bind_param(stmt, my_bind);
1754   check_stmt_rc(rc, stmt);
1755 
1756   nData= 100;
1757   rc= mysql_stmt_execute(stmt);
1758   check_stmt_rc(rc, stmt);
1759 
1760   FAIL_IF(mysql_stmt_affected_rows(stmt) != 1, "Affected rows != 1");
1761   mysql_stmt_close(stmt);
1762 
1763   strcpy(query, "UPDATE test_update SET col2= ? WHERE col3= ?");
1764   stmt= mysql_stmt_init(mysql);
1765   FAIL_IF(!stmt, mysql_error(mysql));
1766   rc= mysql_stmt_prepare(stmt, SL(query));
1767   check_stmt_rc(rc, stmt);
1768 
1769   FAIL_IF(mysql_stmt_param_count(stmt) != 2, "Paramcount != 2");
1770   nData= 100;
1771 
1772   memset(my_bind, '\0', sizeof(my_bind));
1773 
1774   my_bind[0].buffer_type= MYSQL_TYPE_STRING;
1775   my_bind[0].buffer= szData;
1776   my_bind[0].buffer_length= sizeof(szData);
1777   my_bind[0].length= &length[0];
1778   length[0]= sprintf(szData, "updated-data");
1779 
1780   my_bind[1].buffer= (void *)&nData;
1781   my_bind[1].buffer_type= MYSQL_TYPE_LONG;
1782 
1783   rc= mysql_stmt_bind_param(stmt, my_bind);
1784   check_stmt_rc(rc, stmt);
1785 
1786   rc= mysql_stmt_execute(stmt);
1787   check_stmt_rc(rc, stmt);
1788   FAIL_IF(mysql_stmt_affected_rows(stmt) != 1, "Affected rows != 1");
1789 
1790 
1791   mysql_stmt_close(stmt);
1792 
1793   /* now fetch the results ..*/
1794   rc= mysql_commit(mysql);
1795   check_mysql_rc(rc, mysql);
1796 
1797   /* test the results now, only one row should exist */
1798   rc= mysql_query(mysql, "SELECT * FROM test_update");
1799   check_mysql_rc(rc, mysql);
1800 
1801   /* get the result */
1802   result= mysql_store_result(mysql);
1803   FAIL_IF(!result, "Invalid result set");
1804 
1805   while (mysql_fetch_row(result))
1806     rowcount++;
1807   FAIL_IF(rowcount != 1, "rowcount != 1");
1808   mysql_free_result(result);
1809   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_update");
1810   check_mysql_rc(rc, mysql);
1811 
1812   return OK;
1813 }
1814 
1815 
1816 /* Test prepare without parameters */
1817 
test_prepare_noparam(MYSQL * mysql)1818 static int test_prepare_noparam(MYSQL *mysql)
1819 {
1820   MYSQL_STMT *stmt;
1821   int        rc, rowcount= 0;
1822   MYSQL_RES  *result;
1823   char query[MAX_TEST_QUERY_LENGTH];
1824 
1825   rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_prepare");
1826   check_mysql_rc(rc, mysql);
1827 
1828 
1829   rc= mysql_query(mysql, "CREATE TABLE my_prepare(col1 int, col2 varchar(50))");
1830   check_mysql_rc(rc, mysql);
1831 
1832   /* insert by prepare */
1833   strcpy(query, "INSERT INTO my_prepare VALUES(10, 'venu')");
1834   stmt= mysql_stmt_init(mysql);
1835   FAIL_IF(!stmt, mysql_error(mysql));
1836   rc= mysql_stmt_prepare(stmt, SL(query));
1837   check_stmt_rc(rc, stmt);
1838 
1839   FAIL_IF(mysql_stmt_param_count(stmt) != 0, "Paramcount != 0");
1840 
1841   rc= mysql_stmt_execute(stmt);
1842   check_stmt_rc(rc, stmt);
1843 
1844   mysql_stmt_close(stmt);
1845 
1846   /* now fetch the results ..*/
1847   rc= mysql_commit(mysql);
1848   check_mysql_rc(rc, mysql);
1849 
1850   /* test the results now, only one row should exist */
1851   rc= mysql_query(mysql, "SELECT * FROM my_prepare");
1852   check_mysql_rc(rc, mysql);
1853 
1854   /* get the result */
1855   result= mysql_store_result(mysql);
1856   FAIL_IF(!result, "Invalid result set");
1857 
1858   while (mysql_fetch_row(result))
1859     rowcount++;
1860 
1861   FAIL_IF(rowcount != 1, "rowcount != 1");
1862   mysql_free_result(result);
1863   rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_prepare");
1864   check_mysql_rc(rc, mysql);
1865 
1866   return OK;
1867 }
1868 
1869 
1870 /* Test simple bind result */
1871 
test_bind_result(MYSQL * mysql)1872 static int test_bind_result(MYSQL *mysql)
1873 {
1874   MYSQL_STMT *stmt;
1875   int        rc;
1876   int        nData;
1877   ulong      length1;
1878   char       szData[100];
1879   MYSQL_BIND my_bind[2];
1880   my_bool    is_null[2];
1881   char       query[MAX_TEST_QUERY_LENGTH];
1882 
1883   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
1884   check_mysql_rc(rc, mysql);
1885 
1886   rc= mysql_query(mysql, "CREATE TABLE test_bind_result(col1 int , col2 varchar(50))");
1887   check_mysql_rc(rc, mysql);
1888 
1889   rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES(10, 'venu')");
1890   check_mysql_rc(rc, mysql);
1891 
1892   rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES(20, 'MySQL')");
1893   check_mysql_rc(rc, mysql);
1894 
1895   rc= mysql_query(mysql, "INSERT INTO test_bind_result(col2) VALUES('monty')");
1896   check_mysql_rc(rc, mysql);
1897 
1898   rc= mysql_commit(mysql);
1899   check_mysql_rc(rc, mysql);
1900 
1901   /* fetch */
1902 
1903   memset(my_bind, '\0', sizeof(my_bind));
1904   my_bind[0].buffer_type= MYSQL_TYPE_LONG;
1905   my_bind[0].buffer= (void *) &nData;      /* integer data */
1906   my_bind[0].is_null= &is_null[0];
1907 
1908   my_bind[1].buffer_type= MYSQL_TYPE_STRING;
1909   my_bind[1].buffer= szData;                /* string data */
1910   my_bind[1].buffer_length= sizeof(szData);
1911   my_bind[1].length= &length1;
1912   my_bind[1].is_null= &is_null[1];
1913 
1914   strcpy(query, "SELECT * FROM test_bind_result");
1915   stmt= mysql_stmt_init(mysql);
1916   FAIL_IF(!stmt, mysql_error(mysql));
1917   rc= mysql_stmt_prepare(stmt, SL(query));
1918   check_stmt_rc(rc, stmt);
1919 
1920   rc= mysql_stmt_bind_result(stmt, my_bind);
1921   check_stmt_rc(rc, stmt);
1922 
1923   rc= mysql_stmt_execute(stmt);
1924   check_stmt_rc(rc, stmt);
1925 
1926   rc= mysql_stmt_fetch(stmt);
1927   check_stmt_rc(rc, stmt);
1928 
1929   FAIL_UNLESS(nData == 10, "nData != 10");
1930   FAIL_UNLESS(strcmp(szData, "venu") == 0, "szData != 'Venu'");
1931   FAIL_UNLESS(length1 == 4, "length1 != 4");
1932 
1933   rc= mysql_stmt_fetch(stmt);
1934   check_stmt_rc(rc, stmt);
1935 
1936   FAIL_UNLESS(nData == 20, "nData != 20");
1937   FAIL_UNLESS(strcmp(szData, "MySQL") == 0, "szData != 'MySQL'");
1938   FAIL_UNLESS(length1 == 5, "length1 != 5");
1939 
1940   rc= mysql_stmt_fetch(stmt);
1941   check_stmt_rc(rc, stmt);
1942 
1943   FAIL_UNLESS(is_null[0], "null flag not set");
1944   FAIL_UNLESS(strcmp(szData, "monty") == 0, "szData != 'Monty'");
1945   FAIL_UNLESS(length1 == 5, "length1 != 5");
1946 
1947   rc= mysql_stmt_fetch(stmt);
1948   FAIL_UNLESS(rc == MYSQL_NO_DATA, "MYSQL_NO_DATA expected");
1949 
1950   mysql_stmt_close(stmt);
1951   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
1952   check_mysql_rc(rc, mysql);
1953 
1954   return OK;
1955 }
1956 
test_bind_result_ext(MYSQL * mysql)1957 static int test_bind_result_ext(MYSQL *mysql)
1958 {
1959   MYSQL_STMT *stmt;
1960   int        rc, i;
1961   uchar      t_data;
1962   short      s_data;
1963   int        i_data;
1964   longlong   b_data;
1965   float      f_data;
1966   double     d_data;
1967   char       szData[20], bData[20];
1968   ulong       szLength, bLength;
1969   MYSQL_BIND my_bind[8];
1970   ulong      length[8];
1971   my_bool    is_null[8];
1972   char       query[MAX_TEST_QUERY_LENGTH];
1973 
1974   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
1975   check_mysql_rc(rc, mysql);
1976 
1977   rc= mysql_query(mysql, "CREATE TABLE test_bind_result(c1 tinyint, "
1978                                                       " c2 smallint, "
1979                                                       " c3 int, c4 bigint, "
1980                                                       " c5 float, c6 double, "
1981                                                       " c7 varbinary(10), "
1982                                                       " c8 varchar(50))");
1983   check_mysql_rc(rc, mysql);
1984 
1985   rc= mysql_query(mysql, "INSERT INTO test_bind_result "
1986                          "VALUES (19, 2999, 3999, 4999999, "
1987                          " 2345.6, 5678.89563, 'venu', 'mysql')");
1988   check_mysql_rc(rc, mysql);
1989 
1990   rc= mysql_commit(mysql);
1991   check_mysql_rc(rc, mysql);
1992 
1993   memset(my_bind, '\0', sizeof(my_bind));
1994   for (i= 0; i < (int) array_elements(my_bind); i++)
1995   {
1996     my_bind[i].length=  &length[i];
1997     my_bind[i].is_null= &is_null[i];
1998   }
1999 
2000   my_bind[0].buffer_type= MYSQL_TYPE_TINY;
2001   my_bind[0].buffer= (void *)&t_data;
2002 
2003   my_bind[1].buffer_type= MYSQL_TYPE_SHORT;
2004   my_bind[2].buffer_type= MYSQL_TYPE_LONG;
2005 
2006   my_bind[3].buffer_type= MYSQL_TYPE_LONGLONG;
2007   my_bind[1].buffer= (void *)&s_data;
2008 
2009   my_bind[2].buffer= (void *)&i_data;
2010   my_bind[3].buffer= (void *)&b_data;
2011 
2012   my_bind[4].buffer_type= MYSQL_TYPE_FLOAT;
2013   my_bind[4].buffer= (void *)&f_data;
2014 
2015   my_bind[5].buffer_type= MYSQL_TYPE_DOUBLE;
2016   my_bind[5].buffer= (void *)&d_data;
2017 
2018   my_bind[6].buffer_type= MYSQL_TYPE_STRING;
2019   my_bind[6].buffer= (void *)szData;
2020   my_bind[6].buffer_length= sizeof(szData);
2021   my_bind[6].length= &szLength;
2022 
2023   my_bind[7].buffer_type= MYSQL_TYPE_TINY_BLOB;
2024   my_bind[7].buffer= (void *)&bData;
2025   my_bind[7].length= &bLength;
2026   my_bind[7].buffer_length= sizeof(bData);
2027 
2028   strcpy(query, "select * from test_bind_result");
2029   stmt= mysql_stmt_init(mysql);
2030   FAIL_IF(!stmt, mysql_error(mysql));
2031   rc= mysql_stmt_prepare(stmt, SL(query));
2032   check_stmt_rc(rc, stmt);
2033 
2034   rc= mysql_stmt_bind_result(stmt, my_bind);
2035   check_stmt_rc(rc, stmt);
2036 
2037   rc= mysql_stmt_execute(stmt);
2038   check_stmt_rc(rc, stmt);
2039 
2040   rc= mysql_stmt_fetch(stmt);
2041   check_stmt_rc(rc, stmt);
2042 
2043   FAIL_UNLESS(t_data == 19, "tdata != 19");
2044   FAIL_UNLESS(s_data == 2999, "s_data != 2999");
2045   FAIL_UNLESS(i_data == 3999, "i_data != 3999");
2046   FAIL_UNLESS(b_data == 4999999, "b_data != 4999999");
2047   FAIL_UNLESS(strcmp(szData, "venu") == 0, "szData != 'Venu'");
2048   FAIL_UNLESS(strncmp(bData, "mysql", 5) == 0, "nData != 'mysql'");
2049   FAIL_UNLESS(szLength == 4, "szLength != 4");
2050   FAIL_UNLESS(bLength == 5, "bLength != 5");
2051 
2052   rc= mysql_stmt_fetch(stmt);
2053   FAIL_UNLESS(rc == MYSQL_NO_DATA, "MYSQL_NO_DATA expected");
2054 
2055   mysql_stmt_close(stmt);
2056   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
2057   check_mysql_rc(rc, mysql);
2058   return OK;
2059 }
2060 
2061 
2062 /* Test ext bind result */
2063 
test_bind_result_ext1(MYSQL * mysql)2064 static int test_bind_result_ext1(MYSQL *mysql)
2065 {
2066   MYSQL_STMT *stmt;
2067   uint       i;
2068   int        rc;
2069   char       t_data[20];
2070   float      s_data;
2071   short      i_data;
2072   uchar      b_data;
2073   int        f_data;
2074   long       bData;
2075   char       d_data[20];
2076   double     szData;
2077   MYSQL_BIND my_bind[8];
2078   ulong      length[8];
2079   my_bool    is_null[8];
2080   char       query[MAX_TEST_QUERY_LENGTH];
2081 
2082   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
2083   check_mysql_rc(rc, mysql);
2084 
2085   rc= mysql_query(mysql, "CREATE TABLE test_bind_result(c1 tinyint, c2 smallint, \
2086                                                         c3 int, c4 bigint, \
2087                                                         c5 float, c6 double, \
2088                                                         c7 varbinary(10), \
2089                                                         c8 varchar(10))");
2090   check_mysql_rc(rc, mysql);
2091 
2092   rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES(120, 2999, 3999, 54, \
2093                                                               2.6, 58.89, \
2094                                                               '206', '6.7')");
2095   check_mysql_rc(rc, mysql);
2096 
2097   rc= mysql_commit(mysql);
2098   check_mysql_rc(rc, mysql);
2099 
2100   memset(my_bind, '\0', sizeof(my_bind));
2101   my_bind[0].buffer_type= MYSQL_TYPE_STRING;
2102   my_bind[0].buffer= (void *) t_data;
2103   my_bind[0].buffer_length= sizeof(t_data);
2104   my_bind[0].error= &my_bind[0].error_value;
2105 
2106   my_bind[1].buffer_type= MYSQL_TYPE_FLOAT;
2107   my_bind[1].buffer= (void *)&s_data;
2108   my_bind[1].buffer_length= 0;
2109   my_bind[1].error= &my_bind[1].error_value;
2110 
2111   my_bind[2].buffer_type= MYSQL_TYPE_SHORT;
2112   my_bind[2].buffer= (void *)&i_data;
2113   my_bind[2].buffer_length= 0;
2114   my_bind[2].error= &my_bind[2].error_value;
2115 
2116   my_bind[3].buffer_type= MYSQL_TYPE_TINY;
2117   my_bind[3].buffer= (void *)&b_data;
2118   my_bind[3].buffer_length= 0;
2119   my_bind[3].error= &my_bind[3].error_value;
2120 
2121   my_bind[4].buffer_type= MYSQL_TYPE_LONG;
2122   my_bind[4].buffer= (void *)&f_data;
2123   my_bind[4].buffer_length= 0;
2124   my_bind[4].error= &my_bind[4].error_value;
2125 
2126   my_bind[5].buffer_type= MYSQL_TYPE_STRING;
2127   my_bind[5].buffer= (void *)d_data;
2128   my_bind[5].buffer_length= sizeof(d_data);
2129   my_bind[5].error= &my_bind[5].error_value;
2130 
2131   my_bind[6].buffer_type= MYSQL_TYPE_LONG;
2132   my_bind[6].buffer= (void *)&bData;
2133   my_bind[6].buffer_length= 0;
2134   my_bind[6].error= &my_bind[6].error_value;
2135 
2136   my_bind[7].buffer_type= MYSQL_TYPE_DOUBLE;
2137   my_bind[7].buffer= (void *)&szData;
2138   my_bind[7].buffer_length= 0;
2139   my_bind[7].error= &my_bind[7].error_value;
2140 
2141   for (i= 0; i < array_elements(my_bind); i++)
2142   {
2143     my_bind[i].is_null= &is_null[i];
2144     my_bind[i].length= &length[i];
2145   }
2146 
2147   strcpy(query, "select * from test_bind_result");
2148   stmt= mysql_stmt_init(mysql);
2149   FAIL_IF(!stmt, mysql_error(mysql));
2150   rc= mysql_stmt_prepare(stmt, SL(query));
2151   check_stmt_rc(rc, stmt);
2152 
2153   rc= mysql_stmt_bind_result(stmt, my_bind);
2154   check_stmt_rc(rc, stmt);
2155 
2156   rc= mysql_stmt_execute(stmt);
2157   check_stmt_rc(rc, stmt);
2158 
2159   rc= mysql_stmt_fetch(stmt);
2160   check_stmt_rc(rc, stmt);
2161 
2162   FAIL_UNLESS(strcmp(t_data, "120") == 0, "t_data != 120");
2163   FAIL_UNLESS(i_data == 3999, "i_data != 3999");
2164   FAIL_UNLESS(f_data == 2, "f_data != 2");
2165   FAIL_UNLESS(strcmp(d_data, "58.89") == 0, "d_data != 58.89");
2166   FAIL_UNLESS(b_data == 54, "b_data != 54");
2167 
2168   FAIL_UNLESS(length[0] == 3, "Wrong length");
2169   FAIL_UNLESS(length[1] == 4, "Wrong length");
2170   FAIL_UNLESS(length[2] == 2, "Wrong length");
2171   FAIL_UNLESS(length[3] == 1, "Wrong length");
2172   FAIL_UNLESS(length[4] == 4, "Wrong length");
2173   FAIL_UNLESS(length[5] == 5, "Wrong length");
2174   FAIL_UNLESS(length[6] == 4, "Wrong length");
2175   FAIL_UNLESS(length[7] == 8, "Wrong length");
2176 
2177   rc= mysql_stmt_fetch(stmt);
2178   FAIL_UNLESS(rc == MYSQL_NO_DATA, "MYSQL_NO_DATA expected");
2179 
2180   mysql_stmt_close(stmt);
2181   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
2182   check_mysql_rc(rc, mysql);
2183   return OK;
2184 }
2185 
test_bind_negative(MYSQL * mysql)2186 static int test_bind_negative(MYSQL *mysql)
2187 {
2188   MYSQL_STMT *stmt;
2189   char *query;
2190   int rc;
2191   MYSQL_BIND      my_bind[1];
2192   int32           my_val= 0;
2193   ulong           my_length= 0L;
2194   my_bool         my_null= FALSE;
2195 
2196   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
2197   check_mysql_rc(rc, mysql);
2198 
2199   rc= mysql_query(mysql, "create temporary table t1 (c1 int)");
2200   check_mysql_rc(rc, mysql);
2201 
2202   rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1), (-1)");
2203   check_mysql_rc(rc, mysql);
2204 
2205   query= (char*)"INSERT INTO t1 VALUES (?)";
2206   stmt= mysql_stmt_init(mysql);
2207   FAIL_IF(!stmt, mysql_error(mysql));
2208   rc= mysql_stmt_prepare(stmt, SL(query));
2209   check_stmt_rc(rc, stmt);
2210 
2211   /* bind parameters */
2212   memset(my_bind, '\0', sizeof(my_bind));
2213 
2214   my_bind[0].buffer_type= MYSQL_TYPE_LONG;
2215   my_bind[0].buffer= (void *)&my_val;
2216   my_bind[0].length= &my_length;
2217   my_bind[0].is_null= &my_null;
2218 
2219   rc= mysql_stmt_bind_param(stmt, my_bind);
2220   check_stmt_rc(rc, stmt);
2221 
2222   my_val= -1;
2223   rc= mysql_stmt_execute(stmt);
2224   check_stmt_rc(rc, stmt);
2225 
2226   mysql_stmt_close(stmt);
2227   rc= mysql_query(mysql, "drop table t1");
2228   check_mysql_rc(rc, mysql);
2229 
2230   return OK;
2231 }
2232 
test_buffers(MYSQL * mysql)2233 static int test_buffers(MYSQL *mysql)
2234 {
2235   MYSQL_STMT *stmt;
2236   MYSQL_BIND my_bind[1];
2237   int        rc;
2238   ulong      length;
2239   my_bool    is_null;
2240   char       buffer[20];
2241   char       query[MAX_TEST_QUERY_LENGTH];
2242 
2243   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_buffer");
2244   check_mysql_rc(rc, mysql);
2245 
2246   rc= mysql_query(mysql, "CREATE TABLE test_buffer(str varchar(20))");
2247   check_mysql_rc(rc, mysql);
2248 
2249   rc= mysql_query(mysql, "insert into test_buffer values('MySQL')\
2250                           , ('Database'), ('Open-Source'), ('Popular')");
2251   check_mysql_rc(rc, mysql);
2252 
2253   strcpy(query, "select str from test_buffer");
2254   stmt= mysql_stmt_init(mysql);
2255   FAIL_IF(!stmt, mysql_error(mysql));
2256   rc= mysql_stmt_prepare(stmt, SL(query));
2257   check_stmt_rc(rc, stmt);
2258 
2259   rc= mysql_stmt_execute(stmt);
2260   check_stmt_rc(rc, stmt);
2261 
2262   memset(buffer, '\0', sizeof(buffer));              /* Avoid overruns in printf() */
2263 
2264   memset(my_bind, '\0', sizeof(my_bind));
2265   my_bind[0].length= &length;
2266   my_bind[0].is_null= &is_null;
2267   my_bind[0].buffer_length= 1;
2268   my_bind[0].buffer_type= MYSQL_TYPE_STRING;
2269   my_bind[0].buffer= (void *)buffer;
2270   my_bind[0].error= &my_bind[0].error_value;
2271 
2272   rc= mysql_stmt_bind_result(stmt, my_bind);
2273   check_stmt_rc(rc, stmt);
2274 
2275   rc= mysql_stmt_store_result(stmt);
2276   check_stmt_rc(rc, stmt);
2277 
2278   buffer[1]= 'X';
2279   rc= mysql_stmt_fetch(stmt);
2280 
2281   FAIL_UNLESS(rc == MYSQL_DATA_TRUNCATED, "rc != MYSQL_DATA_TRUNCATED");
2282   FAIL_UNLESS(my_bind[0].error_value, "Errorflag not set");
2283   FAIL_UNLESS(buffer[0] == 'M', "buffer[0] != M");
2284   FAIL_UNLESS(buffer[1] == 'X', "buffer[1] != X");
2285   FAIL_UNLESS(length == 5, "length != 5");
2286 
2287   my_bind[0].buffer_length= 8;
2288   rc= mysql_stmt_bind_result(stmt, my_bind);/* re-bind */
2289   check_stmt_rc(rc, stmt);
2290 
2291   rc= mysql_stmt_fetch(stmt);
2292   check_stmt_rc(rc, stmt);
2293   FAIL_UNLESS(strncmp(buffer, "Database", 8) == 0, "buffer != 'Database'");
2294   FAIL_UNLESS(length == 8, "length != 8");
2295 
2296   my_bind[0].buffer_length= 12;
2297   rc= mysql_stmt_bind_result(stmt, my_bind);/* re-bind */
2298   check_stmt_rc(rc, stmt);
2299 
2300   rc= mysql_stmt_fetch(stmt);
2301   check_stmt_rc(rc, stmt);
2302   FAIL_UNLESS(strcmp(buffer, "Open-Source") == 0, "buffer != 'Open-Source'");
2303   FAIL_UNLESS(length == 11, "Length != 11");
2304 
2305   my_bind[0].buffer_length= 6;
2306   rc= mysql_stmt_bind_result(stmt, my_bind);/* re-bind */
2307   check_stmt_rc(rc, stmt);
2308 
2309   rc= mysql_stmt_fetch(stmt);
2310   FAIL_UNLESS(rc == MYSQL_DATA_TRUNCATED, "rc != MYSQL_DATA_TRUNCATED");
2311   FAIL_UNLESS(my_bind[0].error_value, "Errorflag not set");
2312   FAIL_UNLESS(strncmp(buffer, "Popula", 6) == 0, "buffer != 'Popula'");
2313   FAIL_UNLESS(length == 7, "length != 7");
2314 
2315   mysql_stmt_close(stmt);
2316   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_buffer");
2317   check_mysql_rc(rc, mysql);
2318 
2319   return OK;
2320 }
2321 
test_xjoin(MYSQL * mysql)2322 static int test_xjoin(MYSQL *mysql)
2323 {
2324   MYSQL_STMT *stmt;
2325   int rc, i;
2326   const char *query=
2327     "select t.id, p1.value, n1.value, p2.value, n2.value from t3 t LEFT JOIN t1 p1 ON (p1.id=t.param1_id) LEFT JOIN t2 p2 ON (p2.id=t.param2_id) LEFT JOIN t4 n1 ON (n1.id=p1.name_id) LEFT JOIN t4 n2 ON (n2.id=p2.name_id) where t.id=1";
2328 
2329 
2330   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2, t3, t4");
2331   check_mysql_rc(rc, mysql);
2332 
2333   rc= mysql_query(mysql, "create table t3 (id int(8), param1_id int(8), param2_id int(8)) ENGINE=InnoDB DEFAULT CHARSET=utf8");
2334   check_mysql_rc(rc, mysql);
2335 
2336   rc= mysql_query(mysql, "create table t1 ( id int(8), name_id int(8), value varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8");
2337   check_mysql_rc(rc, mysql);
2338 
2339   rc= mysql_query(mysql, "create table t2 (id int(8), name_id int(8), value varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
2340   check_mysql_rc(rc, mysql);
2341 
2342   rc= mysql_query(mysql, "create table t4(id int(8), value varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8");
2343   check_mysql_rc(rc, mysql);
2344 
2345   rc= mysql_query(mysql, "insert into t3 values (1, 1, 1), (2, 2, null)");
2346   check_mysql_rc(rc, mysql);
2347 
2348   rc= mysql_query(mysql, "insert into t1 values (1, 1, 'aaa'), (2, null, 'bbb')");
2349   check_mysql_rc(rc, mysql);
2350 
2351   rc= mysql_query(mysql, "insert into t2 values (1, 2, 'ccc')");
2352   check_mysql_rc(rc, mysql);
2353 
2354   rc= mysql_query(mysql, "insert into t4 values (1, 'Name1'), (2, null)");
2355   check_mysql_rc(rc, mysql);
2356 
2357   stmt= mysql_stmt_init(mysql);
2358   FAIL_IF(!stmt, mysql_error(mysql));
2359   rc= mysql_stmt_prepare(stmt, SL(query));
2360   check_stmt_rc(rc, stmt);
2361 
2362   for (i= 0; i < 3; i++)
2363   {
2364     rc= mysql_stmt_execute(stmt);
2365     check_stmt_rc(rc, stmt);
2366     rc= 0;
2367     while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
2368       rc++;
2369     FAIL_UNLESS(rc == 1, "rowcount != 1");
2370   }
2371   mysql_stmt_close(stmt);
2372 
2373   rc= mysql_query(mysql, "DROP TABLE t1, t2, t3, t4");
2374   check_mysql_rc(rc, mysql);
2375 
2376   return OK;
2377 }
2378 
test_union_param(MYSQL * mysql)2379 static int test_union_param(MYSQL *mysql)
2380 {
2381   MYSQL_STMT *stmt;
2382   char *query;
2383   int rc, i;
2384   MYSQL_BIND      my_bind[2];
2385   char            my_val[4];
2386   ulong           my_length= 3L;
2387   my_bool         my_null= FALSE;
2388 
2389   strcpy(my_val, "abc");
2390 
2391   query= (char*)"select ? as my_col union distinct select ?";
2392   stmt= mysql_stmt_init(mysql);
2393   FAIL_IF(!stmt, mysql_error(mysql));
2394   rc= mysql_stmt_prepare(stmt, SL(query));
2395   check_stmt_rc(rc, stmt);
2396 
2397   /*
2398     We need to bzero bind structure because mysql_stmt_bind_param checks all
2399     its members.
2400   */
2401   memset(my_bind, '\0', sizeof(my_bind));
2402 
2403   /* bind parameters */
2404   my_bind[0].buffer_type=    MYSQL_TYPE_STRING;
2405   my_bind[0].buffer=         (char*) &my_val;
2406   my_bind[0].buffer_length=  4;
2407   my_bind[0].length=         &my_length;
2408   my_bind[0].is_null=        &my_null;
2409   my_bind[1].buffer_type=    MYSQL_TYPE_STRING;
2410   my_bind[1].buffer=         (char*) &my_val;
2411   my_bind[1].buffer_length=  4;
2412   my_bind[1].length=         &my_length;
2413   my_bind[1].is_null=        &my_null;
2414 
2415   rc= mysql_stmt_bind_param(stmt, my_bind);
2416   check_stmt_rc(rc, stmt);
2417 
2418   for (i= 0; i < 3; i++)
2419   {
2420     rc= mysql_stmt_execute(stmt);
2421     check_stmt_rc(rc, stmt);
2422     rc= 0;
2423     while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
2424       rc++;
2425     FAIL_UNLESS(rc == 1, "rowcount != 1");
2426   }
2427 
2428   mysql_stmt_close(stmt);
2429 
2430   return OK;
2431 }
2432 
test_union(MYSQL * mysql)2433 static int test_union(MYSQL *mysql)
2434 {
2435   MYSQL_STMT *stmt;
2436   int rc;
2437   const char *query= "SELECT t1.name FROM t1 UNION "
2438                      "SELECT t2.name FROM t2";
2439 
2440   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
2441   check_mysql_rc(rc, mysql);
2442 
2443   rc= mysql_query(mysql,
2444                   "CREATE TABLE t1 "
2445                   "(id INTEGER NOT NULL PRIMARY KEY, "
2446                   " name VARCHAR(20) NOT NULL)");
2447   check_mysql_rc(rc, mysql);
2448   rc= mysql_query(mysql,
2449                   "INSERT INTO t1 (id, name) VALUES "
2450                   "(2, 'Ja'), (3, 'Ede'), "
2451                   "(4, 'Haag'), (5, 'Kabul'), "
2452                   "(6, 'Almere'), (7, 'Utrecht'), "
2453                   "(8, 'Qandahar'), (9, 'Amsterdam'), "
2454                   "(10, 'Amersfoort'), (11, 'Constantine')");
2455   check_mysql_rc(rc, mysql);
2456   rc= mysql_query(mysql,
2457                   "CREATE TABLE t2 "
2458                   "(id INTEGER NOT NULL PRIMARY KEY, "
2459                   " name VARCHAR(20) NOT NULL)");
2460   check_mysql_rc(rc, mysql);
2461   rc= mysql_query(mysql,
2462                   "INSERT INTO t2 (id, name) VALUES "
2463                   "(4, 'Guam'), (5, 'Aruba'), "
2464                   "(6, 'Angola'), (7, 'Albania'), "
2465                   "(8, 'Anguilla'), (9, 'Argentina'), "
2466                   "(10, 'Azerbaijan'), (11, 'Afghanistan'), "
2467                   "(12, 'Burkina Faso'), (13, 'Faroe Islands')");
2468   check_mysql_rc(rc, mysql);
2469 
2470   stmt= mysql_stmt_init(mysql);
2471   FAIL_IF(!stmt, mysql_error(mysql));
2472   rc= mysql_stmt_prepare(stmt, SL(query));
2473   check_stmt_rc(rc, stmt);
2474 
2475   rc= mysql_stmt_execute(stmt);
2476   check_stmt_rc(rc, stmt);
2477   rc= 0;
2478   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
2479     rc++;
2480   FAIL_UNLESS(rc == 20, "rc != 20");
2481   mysql_stmt_close(stmt);
2482 
2483   rc= mysql_query(mysql, "DROP TABLE t1, t2");
2484   check_mysql_rc(rc, mysql);
2485 
2486   return OK;
2487 }
2488 
test_union2(MYSQL * mysql)2489 static int test_union2(MYSQL *mysql)
2490 {
2491   MYSQL_STMT *stmt;
2492   int rc, i;
2493   const char *query= "select col1 FROM t1 where col1=1 union distinct "
2494                      "select col1 FROM t1 where col1=2";
2495 
2496 
2497   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
2498   check_mysql_rc(rc, mysql);
2499 
2500   rc= mysql_query(mysql, "CREATE TABLE t1(col1 INT, \
2501                                          col2 VARCHAR(40),      \
2502                                          col3 SMALLINT, \
2503                                          col4 TIMESTAMP)");
2504   check_mysql_rc(rc, mysql);
2505 
2506   stmt= mysql_stmt_init(mysql);
2507   FAIL_IF(!stmt, mysql_error(mysql));
2508   rc= mysql_stmt_prepare(stmt, SL(query));
2509   check_stmt_rc(rc, stmt);
2510 
2511   for (i= 0; i < 3; i++)
2512   {
2513     rc= mysql_stmt_execute(stmt);
2514     check_stmt_rc(rc, stmt);
2515     rc= 0;
2516     while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
2517       rc++;
2518     FAIL_UNLESS(rc == 0, "rowcount != 0");
2519   }
2520 
2521   mysql_stmt_close(stmt);
2522 
2523   rc= mysql_query(mysql, "DROP TABLE t1");
2524   check_mysql_rc(rc, mysql);
2525 
2526   return OK;
2527 }
2528 
2529 /* Misc tests to keep pure coverage happy */
2530 
test_pure_coverage(MYSQL * mysql)2531 static int test_pure_coverage(MYSQL *mysql)
2532 {
2533   MYSQL_STMT *stmt;
2534   MYSQL_BIND my_bind[1];
2535   int        rc;
2536   ulong      length;
2537 
2538 
2539   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_pure");
2540   check_mysql_rc(rc, mysql);
2541 
2542   rc= mysql_query(mysql, "CREATE TABLE test_pure(c1 int, c2 varchar(20))");
2543   check_mysql_rc(rc, mysql);
2544 
2545   rc= mysql_query(mysql, "FLUSH TABLES");
2546   check_mysql_rc(rc, mysql);
2547 
2548   rc= mysql_query(mysql, "START TRANSACTION");
2549   check_mysql_rc(rc, mysql);
2550 
2551   stmt= mysql_stmt_init(mysql);
2552   FAIL_IF(!stmt, mysql_error(mysql));
2553   rc= mysql_stmt_prepare(stmt, SL("insert into test_pure(c67788) values(10)"));
2554   FAIL_IF(!rc, "Error expected");
2555   mysql_stmt_close(stmt);
2556 
2557   /* Query without params and result should allow one to bind 0 arrays */
2558   stmt= mysql_stmt_init(mysql);
2559   FAIL_IF(!stmt, mysql_error(mysql));
2560   rc= mysql_stmt_prepare(stmt, SL("insert into test_pure(c2) values(10)"));
2561   check_stmt_rc(rc, stmt);
2562 
2563   rc= mysql_stmt_bind_param(stmt, (MYSQL_BIND*)0);
2564   check_stmt_rc(rc, stmt);
2565   rc= mysql_stmt_execute(stmt);
2566   check_stmt_rc(rc, stmt);
2567   rc= mysql_stmt_bind_result(stmt, (MYSQL_BIND*)0);
2568   FAIL_UNLESS(rc == 1, "");
2569 
2570   mysql_stmt_close(stmt);
2571 
2572   stmt= mysql_stmt_init(mysql);
2573   FAIL_IF(!stmt, mysql_error(mysql));
2574   rc= mysql_stmt_prepare(stmt, SL("insert into test_pure(c2) values(?)"));
2575   check_stmt_rc(rc, stmt);
2576 
2577   /*
2578     We need to bzero bind structure because mysql_stmt_bind_param checks all
2579     its members.
2580   */
2581   memset(my_bind, '\0', sizeof(my_bind));
2582   my_bind[0].length= &length;
2583   my_bind[0].is_null= 0;
2584   my_bind[0].buffer_length= 0;
2585 
2586   my_bind[0].buffer_type= MYSQL_TYPE_GEOMETRY;
2587   rc= mysql_stmt_bind_param(stmt, my_bind);
2588   FAIL_IF(!rc, "Error expected");
2589 
2590   my_bind[0].buffer_type= MYSQL_TYPE_STRING;
2591   rc= mysql_stmt_bind_param(stmt, my_bind);
2592   check_stmt_rc(rc, stmt);
2593   rc= mysql_stmt_store_result(stmt);
2594   check_stmt_rc(rc, stmt);
2595   mysql_stmt_close(stmt);
2596 
2597   stmt= mysql_stmt_init(mysql);
2598   FAIL_IF(!stmt, mysql_error(mysql));
2599   rc= mysql_stmt_prepare(stmt, SL("select * from test_pure"));
2600   check_stmt_rc(rc, stmt);
2601   rc= mysql_stmt_execute(stmt);
2602   check_stmt_rc(rc, stmt);
2603   mysql_stmt_close(stmt);
2604 
2605   mysql_query(mysql, "DROP TABLE test_pure");
2606   return OK;
2607 }
2608 
test_insert_select(MYSQL * mysql)2609 static int test_insert_select(MYSQL *mysql)
2610 {
2611   MYSQL_STMT *stmt_insert, *stmt_select;
2612   char *query;
2613   int rc;
2614   uint i;
2615 
2616   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
2617   check_mysql_rc(rc, mysql);
2618 
2619   rc= mysql_query(mysql, "create table t1 (a int)");
2620   check_mysql_rc(rc, mysql);
2621 
2622   rc= mysql_query(mysql, "create table t2 (a int)");
2623   check_mysql_rc(rc, mysql);
2624 
2625   rc= mysql_query(mysql, "insert into t2 values (1)");
2626   check_mysql_rc(rc, mysql);
2627 
2628   query= (char*)"insert into t1 select a from t2";
2629   stmt_insert= mysql_stmt_init(mysql);
2630   FAIL_IF(!stmt_insert, mysql_error(mysql));
2631   rc= mysql_stmt_prepare(stmt_insert, SL(query));
2632   check_stmt_rc(rc, stmt_insert);
2633 
2634   query= (char*)"select * from t1";
2635   stmt_select= mysql_stmt_init(mysql);
2636   FAIL_IF(!stmt_select, mysql_error(mysql));
2637   rc= mysql_stmt_prepare(stmt_select, SL(query));
2638   check_stmt_rc(rc, stmt_select);
2639 
2640   for(i= 0; i < 3; i++)
2641   {
2642     rc= mysql_stmt_execute(stmt_insert);
2643     check_stmt_rc(rc, stmt_insert);
2644 
2645     rc= mysql_stmt_execute(stmt_select);
2646     check_stmt_rc(rc, stmt_select);
2647     rc= 0;
2648     while (mysql_stmt_fetch(stmt_select) != MYSQL_NO_DATA)
2649       rc++;
2650     FAIL_UNLESS(rc == (int)(i+1), "rc != i+1");
2651   }
2652 
2653   mysql_stmt_close(stmt_insert);
2654   mysql_stmt_close(stmt_select);
2655   rc= mysql_query(mysql, "drop table t1, t2");
2656   check_mysql_rc(rc, mysql);
2657   return OK;
2658 }
2659 
2660 /* Test simple prepare-insert */
2661 
test_insert(MYSQL * mysql)2662 static int test_insert(MYSQL *mysql)
2663 {
2664   MYSQL_STMT *stmt;
2665   int        rc;
2666   char       str_data[50];
2667   char       tiny_data;
2668   MYSQL_RES  *result;
2669   MYSQL_BIND my_bind[2];
2670   ulong      length;
2671 
2672 
2673   rc= mysql_autocommit(mysql, TRUE);
2674   check_mysql_rc(rc, mysql);
2675 
2676   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_insert");
2677   check_mysql_rc(rc, mysql);
2678 
2679   rc= mysql_query(mysql, "CREATE TABLE test_prep_insert(col1 tinyint, \
2680                                 col2 varchar(50))");
2681   check_mysql_rc(rc, mysql);
2682 
2683   /* insert by prepare */
2684   stmt= mysql_stmt_init(mysql);
2685   FAIL_IF(!stmt, mysql_error(mysql));
2686   rc= mysql_stmt_prepare(stmt, SL("INSERT INTO test_prep_insert VALUES(?, ?)"));
2687   check_stmt_rc(rc, stmt);
2688 
2689   FAIL_IF(mysql_stmt_param_count(stmt) != 2, "Param_count != 2");
2690 
2691   /*
2692     We need to bzero bind structure because mysql_stmt_bind_param checks all
2693     its members.
2694   */
2695   memset(my_bind, '\0', sizeof(my_bind));
2696 
2697   /* tinyint */
2698   my_bind[0].buffer_type= MYSQL_TYPE_TINY;
2699   my_bind[0].buffer= (void *)&tiny_data;
2700 
2701   /* string */
2702   my_bind[1].buffer_type= MYSQL_TYPE_STRING;
2703   my_bind[1].buffer= str_data;
2704   my_bind[1].buffer_length= sizeof(str_data);;
2705   my_bind[1].length= &length;
2706 
2707   rc= mysql_stmt_bind_param(stmt, my_bind);
2708   check_stmt_rc(rc, stmt);
2709 
2710   /* now, execute the prepared statement to insert 10 records.. */
2711   for (tiny_data= 0; tiny_data < 3; tiny_data++)
2712   {
2713     length= sprintf(str_data, "MySQL%d", tiny_data);
2714     rc= mysql_stmt_execute(stmt);
2715     check_stmt_rc(rc, stmt);
2716   }
2717 
2718   mysql_stmt_close(stmt);
2719 
2720   /* now fetch the results ..*/
2721   rc= mysql_commit(mysql);
2722   check_mysql_rc(rc, mysql);
2723 
2724   /* test the results now, only one row should exist */
2725   rc= mysql_query(mysql, "SELECT * FROM test_prep_insert");
2726   check_mysql_rc(rc, mysql);
2727 
2728   /* get the result */
2729   result= mysql_store_result(mysql);
2730   FAIL_IF(!result, "Invalid result set");
2731 
2732   rc= 0;
2733   while (mysql_fetch_row(result))
2734     rc++;
2735   FAIL_UNLESS((int) tiny_data == rc, "rowcount != tinydata");
2736   mysql_free_result(result);
2737 
2738   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_insert");
2739   check_mysql_rc(rc, mysql);
2740 
2741   return OK;
2742 }
2743 
test_join(MYSQL * mysql)2744 static int test_join(MYSQL *mysql)
2745 {
2746   MYSQL_STMT *stmt;
2747   int rc, i, j;
2748   const char *query[]= {"SELECT * FROM t2 join t1 on (t1.a=t2.a)",
2749                         "SELECT * FROM t2 natural join t1",
2750                         "SELECT * FROM t2 join t1 using(a)",
2751                         "SELECT * FROM t2 left join t1 on(t1.a=t2.a)",
2752                         "SELECT * FROM t2 natural left join t1",
2753                         "SELECT * FROM t2 left join t1 using(a)",
2754                         "SELECT * FROM t2 right join t1 on(t1.a=t2.a)",
2755                         "SELECT * FROM t2 natural right join t1",
2756                         "SELECT * FROM t2 right join t1 using(a)"};
2757 
2758 
2759   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
2760   check_mysql_rc(rc, mysql);
2761 
2762   rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
2763   check_mysql_rc(rc, mysql);
2764 
2765   rc= mysql_query(mysql,
2766                   "insert into t1 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);");
2767   check_mysql_rc(rc, mysql);
2768 
2769   rc= mysql_query(mysql, "CREATE TABLE t2 (a int , c int);");
2770   check_mysql_rc(rc, mysql);
2771 
2772   rc= mysql_query(mysql,
2773                   "insert into t2 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);");
2774   check_mysql_rc(rc, mysql);
2775 
2776   for (j= 0; j < 9; j++)
2777   {
2778     stmt= mysql_stmt_init(mysql);
2779     FAIL_IF(!stmt, mysql_error(mysql));
2780     rc= mysql_stmt_prepare(stmt, SL(query[j]));
2781     check_stmt_rc(rc, stmt);
2782     for (i= 0; i < 3; i++)
2783     {
2784       rc= mysql_stmt_execute(stmt);
2785       check_stmt_rc(rc, stmt);
2786       rc= 0;
2787       while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
2788         rc++;
2789       FAIL_UNLESS(rc == 5, "rowcount != 5");
2790     }
2791     mysql_stmt_close(stmt);
2792   }
2793 
2794   rc= mysql_query(mysql, "DROP TABLE t1, t2");
2795   check_mysql_rc(rc, mysql);
2796   return OK;
2797 }
2798 
test_left_join_view(MYSQL * mysql)2799 static int test_left_join_view(MYSQL *mysql)
2800 {
2801   MYSQL_STMT *stmt;
2802   int rc, i;
2803   const char *query=
2804     "select t1.a, v1.x from t1 left join v1 on (t1.a= v1.x);";
2805 
2806 
2807   rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,v1");
2808   check_mysql_rc(rc, mysql);
2809 
2810   rc = mysql_query(mysql, "DROP VIEW IF EXISTS v1,t1");
2811   check_mysql_rc(rc, mysql);
2812   rc= mysql_query(mysql,"CREATE TABLE t1 (a int)");
2813   check_mysql_rc(rc, mysql);
2814   rc= mysql_query(mysql,"insert into t1 values (1), (2), (3)");
2815   check_mysql_rc(rc, mysql);
2816   rc= mysql_query(mysql,"create view v1 (x) as select a from t1 where a > 1");
2817   check_mysql_rc(rc, mysql);
2818   stmt= mysql_stmt_init(mysql);
2819   rc= mysql_stmt_prepare(stmt, SL(query));
2820   check_stmt_rc(rc, stmt);
2821 
2822   for (i= 0; i < 3; i++)
2823   {
2824     rc= mysql_stmt_execute(stmt);
2825     check_stmt_rc(rc, stmt);
2826     rc= 0;
2827      while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
2828        rc++;
2829      FAIL_UNLESS(rc == 3, "rowcount != 3");
2830   }
2831   mysql_stmt_close(stmt);
2832 
2833   rc= mysql_query(mysql, "DROP VIEW v1");
2834   check_mysql_rc(rc, mysql);
2835   rc= mysql_query(mysql, "DROP TABLE t1");
2836   check_mysql_rc(rc, mysql);
2837   return OK;
2838 }
2839 
2840 /* Test simple sample - manual */
2841 
test_manual_sample(MYSQL * mysql)2842 static int test_manual_sample(MYSQL *mysql)
2843 {
2844   unsigned int param_count;
2845   MYSQL_STMT   *stmt;
2846   short        small_data;
2847   int          int_data;
2848   int          rc;
2849   char         str_data[50];
2850   ulonglong    affected_rows;
2851   MYSQL_BIND   my_bind[3];
2852   my_bool      is_null;
2853   char query[MAX_TEST_QUERY_LENGTH];
2854 
2855 
2856   /*
2857     Sample which is incorporated directly in the manual under Prepared
2858     statements section (Example from mysql_stmt_execute()
2859   */
2860 
2861   memset(str_data, 0, sizeof(str_data));
2862   mysql_autocommit(mysql, 1);
2863   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_table");
2864   check_mysql_rc(rc, mysql);
2865   rc= mysql_query(mysql, "CREATE TABLE test_table(col1 int, col2 varchar(50), \
2866                                                  col3 smallint, \
2867                                                  col4 timestamp)");
2868   check_mysql_rc(rc, mysql);
2869 
2870   /* Prepare a insert query with 3 parameters */
2871   strcpy(query, "INSERT INTO test_table(col1, col2, col3) values(?, ?, ?)");
2872   stmt= mysql_stmt_init(mysql);
2873   FAIL_IF(!stmt, mysql_error(mysql));
2874   rc= mysql_stmt_prepare(stmt, SL(query));
2875   check_stmt_rc(rc, stmt);
2876 
2877   /* Get the parameter count from the statement */
2878   param_count= mysql_stmt_param_count(stmt);
2879   FAIL_IF(param_count != 3, "param_count != 3");
2880 
2881   memset(my_bind, '\0', sizeof(my_bind));
2882 
2883   /* INTEGER PART */
2884   my_bind[0].buffer_type= MYSQL_TYPE_LONG;
2885   my_bind[0].buffer= (void *)&int_data;
2886 
2887   /* STRING PART */
2888   my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
2889   my_bind[1].buffer= (void *)str_data;
2890   my_bind[1].buffer_length= sizeof(str_data);
2891 
2892   /* SMALLINT PART */
2893   my_bind[2].buffer_type= MYSQL_TYPE_SHORT;
2894   my_bind[2].buffer= (void *)&small_data;
2895   my_bind[2].is_null= &is_null;
2896   is_null= 0;
2897 
2898   /* Bind the buffers */
2899   rc= mysql_stmt_bind_param(stmt, my_bind);
2900   check_stmt_rc(rc, stmt);
2901 
2902   /* Specify the data */
2903   int_data= 10;             /* integer */
2904   strcpy(str_data, "MySQL"); /* string  */
2905 
2906   /* INSERT SMALLINT data as NULL */
2907   is_null= 1;
2908 
2909   /* Execute the insert statement - 1*/
2910   rc= mysql_stmt_execute(stmt);
2911   check_stmt_rc(rc, stmt);
2912 
2913   /* Get the total rows affected */
2914   affected_rows= mysql_stmt_affected_rows(stmt);
2915   FAIL_IF(affected_rows != 1, "affected-rows != 1");
2916 
2917   /* Re-execute the insert, by changing the values */
2918   int_data= 1000;
2919   strcpy(str_data, "The most popular open source database");
2920   small_data= 1000;         /* smallint */
2921   is_null= 0;               /* reset */
2922 
2923   /* Execute the insert statement - 2*/
2924   rc= mysql_stmt_execute(stmt);
2925   check_stmt_rc(rc, stmt);
2926 
2927   /* Get the total rows affected */
2928   affected_rows= mysql_stmt_affected_rows(stmt);
2929 
2930   FAIL_IF(affected_rows != 1, "affected_rows != 1");
2931 
2932   /* Close the statement */
2933   rc= mysql_stmt_close(stmt);
2934   check_stmt_rc(rc, stmt);
2935 
2936   /* DROP THE TABLE */
2937   rc= mysql_query(mysql, "DROP TABLE test_table");
2938   check_mysql_rc(rc, mysql);
2939   return OK;
2940 }
2941 
test_create_drop(MYSQL * mysql)2942 static int test_create_drop(MYSQL *mysql)
2943 {
2944   MYSQL_STMT *stmt_create, *stmt_drop, *stmt_select, *stmt_create_select;
2945   char *query;
2946   int rc, i;
2947 
2948   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
2949   check_mysql_rc(rc, mysql);
2950 
2951   rc= mysql_query(mysql, "create table t2 (a int);");
2952   check_mysql_rc(rc, mysql);
2953 
2954   rc= mysql_query(mysql, "create table t1 (a int);");
2955   check_mysql_rc(rc, mysql);
2956 
2957   rc= mysql_query(mysql, "insert into t2 values (3), (2), (1);");
2958   check_mysql_rc(rc, mysql);
2959 
2960   query= (char*)"create table t1 (a int)";
2961   stmt_create= mysql_stmt_init(mysql);
2962   FAIL_IF(!stmt_create, mysql_error(mysql));
2963   rc= mysql_stmt_prepare(stmt_create, SL(query));
2964   check_stmt_rc(rc, stmt_create);
2965 
2966   query= (char*)"drop table t1";
2967   stmt_drop= mysql_stmt_init(mysql);
2968   FAIL_IF(!stmt_drop, mysql_error(mysql));
2969   rc= mysql_stmt_prepare(stmt_drop, SL(query));
2970   check_stmt_rc(rc, stmt_drop);
2971 
2972   query= (char*)"select a in (select a from t2) from t1";
2973   stmt_select= mysql_stmt_init(mysql);
2974   FAIL_IF(!stmt_select, mysql_error(mysql));
2975   rc= mysql_stmt_prepare(stmt_select, SL(query));
2976   check_stmt_rc(rc, stmt_select);
2977 
2978   rc= mysql_query(mysql, "DROP TABLE t1");
2979   check_mysql_rc(rc, mysql);
2980 
2981   query= (char*)"create table t1 select a from t2";
2982   stmt_create_select= mysql_stmt_init(mysql);
2983   FAIL_IF(!stmt_create_select, mysql_error(mysql));
2984   rc= mysql_stmt_prepare(stmt_create_select, SL(query));
2985   check_stmt_rc(rc, stmt_create_select);
2986 
2987   for (i= 0; i < 3; i++)
2988   {
2989     rc= mysql_stmt_execute(stmt_create);
2990     check_stmt_rc(rc, stmt_create);
2991 
2992     rc= mysql_stmt_execute(stmt_select);
2993     check_stmt_rc(rc, stmt_select);
2994 
2995     rc= 0;
2996     while (mysql_stmt_fetch(stmt_select) != MYSQL_NO_DATA)
2997       rc++;
2998     FAIL_UNLESS(rc == 0, "rowcount != 0");
2999 
3000     rc= mysql_stmt_execute(stmt_drop);
3001     check_stmt_rc(rc, stmt_drop);
3002 
3003     rc= mysql_stmt_execute(stmt_create_select);
3004     check_stmt_rc(rc, stmt_create_select);
3005 
3006     rc= mysql_stmt_execute(stmt_select);
3007     check_stmt_rc(rc, stmt_select);
3008     rc= 0;
3009     while (mysql_stmt_fetch(stmt_select) != MYSQL_NO_DATA)
3010       rc++;
3011     FAIL_UNLESS(rc == 3, "rowcount != 3");
3012 
3013     rc= mysql_stmt_execute(stmt_drop);
3014     check_stmt_rc(rc, stmt_drop);
3015   }
3016 
3017   mysql_stmt_close(stmt_create);
3018   mysql_stmt_close(stmt_drop);
3019   mysql_stmt_close(stmt_select);
3020   mysql_stmt_close(stmt_create_select);
3021 
3022   rc= mysql_query(mysql, "DROP TABLE t2");
3023   check_mysql_rc(rc, mysql);
3024   return OK;
3025 }
3026 
3027 /* Test DATE, TIME, DATETIME and TS with MYSQL_TIME conversion */
3028 
test_date(MYSQL * mysql)3029 static int test_date(MYSQL *mysql)
3030 {
3031   int        rc;
3032 
3033   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
3034   check_mysql_rc(rc, mysql);
3035 
3036   rc= mysql_query(mysql, "CREATE TABLE test_date(c1 TIMESTAMP, \
3037                                                  c2 TIME, \
3038                                                  c3 DATETIME, \
3039                                                  c4 DATE)");
3040 
3041   check_mysql_rc(rc, mysql);
3042 
3043   rc= test_bind_date_conv(mysql, 5);
3044   mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
3045   return rc;
3046 }
3047 
3048 
3049 /* Test all time types to DATE and DATE to all types */
3050 
test_date_date(MYSQL * mysql)3051 static int test_date_date(MYSQL *mysql)
3052 {
3053   int        rc;
3054 
3055 
3056   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
3057   check_mysql_rc(rc, mysql);
3058 
3059   rc= mysql_query(mysql, "CREATE TABLE test_date(c1 DATE, \
3060                                                  c2 DATE, \
3061                                                  c3 DATE, \
3062                                                  c4 DATE)");
3063 
3064   check_mysql_rc(rc, mysql);
3065 
3066   rc= test_bind_date_conv(mysql, 3);
3067   mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
3068   return rc;
3069 }
3070 
3071 /* Test all time types to TIMESTAMP and TIMESTAMP to all types */
3072 
test_date_ts(MYSQL * mysql)3073 static int test_date_ts(MYSQL *mysql)
3074 {
3075   int        rc;
3076 
3077 
3078   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
3079   check_mysql_rc(rc, mysql);
3080 
3081   rc= mysql_query(mysql, "CREATE TABLE test_date(c1 TIMESTAMP, \
3082                                                  c2 TIMESTAMP, \
3083                                                  c3 TIMESTAMP, \
3084                                                  c4 TIMESTAMP)");
3085 
3086   check_mysql_rc(rc, mysql);
3087 
3088   rc= test_bind_date_conv(mysql, 2);
3089   mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
3090   return rc;
3091 }
3092 
3093 
3094 /* Test all time types to DATETIME and DATETIME to all types */
3095 
test_date_dt(MYSQL * mysql)3096 static int test_date_dt(MYSQL *mysql)
3097 {
3098   int rc;
3099 
3100 
3101   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
3102   check_mysql_rc(rc, mysql);
3103 
3104   rc= mysql_query(mysql, "CREATE TABLE test_date(c1 datetime, "
3105                          " c2 datetime, c3 datetime, c4 date)");
3106   check_mysql_rc(rc, mysql);
3107 
3108   rc= test_bind_date_conv(mysql, 2);
3109   mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
3110   return rc;
3111 }
3112 
3113 /* Test all time types to TIME and TIME to all types */
3114 
test_date_time(MYSQL * mysql)3115 static int test_date_time(MYSQL *mysql)
3116 {
3117   int        rc;
3118 
3119 
3120   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
3121   check_mysql_rc(rc, mysql);
3122 
3123   rc= mysql_query(mysql, "CREATE TABLE test_date(c1 TIME, \
3124                                                  c2 TIME, \
3125                                                  c3 TIME, \
3126                                                  c4 TIME)");
3127 
3128   check_mysql_rc(rc, mysql);
3129 
3130   rc= test_bind_date_conv(mysql, 3);
3131   mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
3132   return rc;
3133 }
3134 
3135 /*
3136   Test of basic checks that are performed in server for components
3137   of MYSQL_TIME parameters.
3138 */
3139 
test_datetime_ranges(MYSQL * mysql)3140 static int test_datetime_ranges(MYSQL *mysql)
3141 {
3142   const char *stmt_text;
3143   int rc, i;
3144   MYSQL_STMT *stmt;
3145   MYSQL_BIND my_bind[6];
3146   MYSQL_TIME tm[6];
3147 
3148   if (!is_mariadb)
3149     return SKIP;
3150 
3151   stmt_text= "drop table if exists t1";
3152   rc= mysql_real_query(mysql, SL(stmt_text));
3153   check_mysql_rc(rc, mysql);
3154 
3155   stmt_text= "create table t1 (year datetime, month datetime, day datetime, "
3156                               "hour datetime, min datetime, sec datetime)";
3157   rc= mysql_real_query(mysql, SL(stmt_text));
3158   check_mysql_rc(rc, mysql);
3159 
3160   stmt= mysql_stmt_init(mysql);
3161   FAIL_IF(!stmt, mysql_error(mysql));
3162   stmt_text= "INSERT INTO t1 VALUES (?, ?, ?, ?, ?, ?)";
3163   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
3164   check_stmt_rc(rc, stmt);
3165   FAIL_IF(mysql_stmt_param_count(stmt) != 6, "param_count != 6");
3166 
3167   memset(my_bind, '\0', sizeof(my_bind));
3168   for (i= 0; i < 6; i++)
3169   {
3170     my_bind[i].buffer_type= MYSQL_TYPE_DATETIME;
3171     my_bind[i].buffer= &tm[i];
3172   }
3173   rc= mysql_stmt_bind_param(stmt, my_bind);
3174   check_stmt_rc(rc, stmt);
3175 
3176   tm[0].year= 2004; tm[0].month= 11; tm[0].day= 10;
3177   tm[0].hour= 12; tm[0].minute= 30; tm[0].second= 30;
3178   tm[0].second_part= 0; tm[0].neg= 0;
3179 
3180   tm[5]= tm[4]= tm[3]= tm[2]= tm[1]= tm[0];
3181   tm[0].year= 10000;  tm[1].month= 13; tm[2].day= 32;
3182   tm[3].hour= 24; tm[4].minute= 60; tm[5].second= 60;
3183 
3184   rc= mysql_stmt_execute(stmt);
3185   check_stmt_rc(rc, stmt);
3186 
3187   FAIL_IF(!mysql_warning_count(mysql), "warnings expected");
3188 
3189   if (verify_col_data(mysql, "t1", "year", "0000-00-00 00:00:00"))
3190     goto error;
3191   if (verify_col_data(mysql, "t1", "month", "0000-00-00 00:00:00"))
3192     goto error;
3193   if (verify_col_data(mysql, "t1", "day", "0000-00-00 00:00:00"))
3194     goto error;
3195   if (verify_col_data(mysql, "t1", "hour", "0000-00-00 00:00:00"))
3196     goto error;
3197   if (verify_col_data(mysql, "t1", "min", "0000-00-00 00:00:00"))
3198     goto error;
3199   if (verify_col_data(mysql, "t1", "sec", "0000-00-00 00:00:00"))
3200     goto error;
3201 
3202   mysql_stmt_close(stmt);
3203 
3204   stmt_text= "delete from t1";
3205   rc= mysql_real_query(mysql, SL(stmt_text));
3206   check_mysql_rc(rc, mysql);
3207 
3208   stmt_text= "INSERT INTO t1 (year, month, day) VALUES (?, ?, ?)";
3209   stmt= mysql_stmt_init(mysql);
3210   FAIL_IF(!stmt, mysql_error(mysql));
3211   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
3212   check_stmt_rc(rc, stmt);
3213 
3214   /*
3215     We reuse contents of bind and tm arrays left from previous part of test.
3216   */
3217   for (i= 0; i < 3; i++)
3218     my_bind[i].buffer_type= MYSQL_TYPE_DATE;
3219 
3220   rc= mysql_stmt_bind_param(stmt, my_bind);
3221   check_stmt_rc(rc, stmt);
3222 
3223   rc= mysql_stmt_execute(stmt);
3224   check_stmt_rc(rc, stmt);
3225   FAIL_IF(!mysql_warning_count(mysql), "warnings expected");
3226 
3227   if (verify_col_data(mysql, "t1", "year", "0000-00-00 00:00:00"))
3228     goto error;
3229   if (verify_col_data(mysql, "t1", "month", "0000-00-00 00:00:00"))
3230     goto error;
3231   if (verify_col_data(mysql, "t1", "day", "0000-00-00 00:00:00"))
3232     goto error;
3233 
3234   mysql_stmt_close(stmt);
3235 
3236   stmt_text= "drop table t1";
3237   rc= mysql_real_query(mysql, SL(stmt_text));
3238   check_mysql_rc(rc, mysql);
3239 
3240   stmt_text= "create table t1 (day_ovfl time, day time, hour time, min time, sec time)";
3241   rc= mysql_real_query(mysql, SL(stmt_text));
3242   check_mysql_rc(rc, mysql);
3243 
3244   stmt= mysql_stmt_init(mysql);
3245   FAIL_IF(!stmt, mysql_error(mysql));
3246   stmt_text= "INSERT INTO t1 VALUES (?,?,?,?,?)";
3247   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
3248   check_stmt_rc(rc, stmt);
3249   FAIL_IF(mysql_stmt_param_count(stmt) != 5, "param_count != 5");
3250 
3251   /*
3252     Again we reuse what we can from previous part of test.
3253   */
3254   for (i= 0; i < 5; i++)
3255     my_bind[i].buffer_type= MYSQL_TYPE_TIME;
3256 
3257   rc= mysql_stmt_bind_param(stmt, my_bind);
3258   check_stmt_rc(rc, stmt);
3259 
3260   tm[0].year= 0; tm[0].month= 0; tm[0].day= 10;
3261   tm[0].hour= 12; tm[0].minute= 30; tm[0].second= 30;
3262   tm[0].second_part= 0; tm[0].neg= 0;
3263 
3264   tm[4]= tm[3]= tm[2]= tm[1]= tm[0];
3265   tm[0].day= 35; tm[1].day= 34; tm[2].hour= 30; tm[3].minute= 60; tm[4].second= 60;
3266 
3267   rc= mysql_stmt_execute(stmt);
3268   check_stmt_rc(rc, stmt);
3269   FAIL_IF(mysql_warning_count(mysql) != 2, "warning_count != 2");
3270 
3271   if (verify_col_data(mysql, "t1", "day_ovfl", "838:59:59"))
3272     goto error;
3273   if (verify_col_data(mysql, "t1", "day", "828:30:30"))
3274     goto error;
3275   if (verify_col_data(mysql, "t1", "hour", "270:30:30"))
3276     goto error;
3277   if (verify_col_data(mysql, "t1", "min", "00:00:00"))
3278     goto error;
3279   if (verify_col_data(mysql, "t1", "sec", "00:00:00"))
3280     goto error;
3281 
3282   mysql_stmt_close(stmt);
3283   stmt_text= "drop table t1";
3284   rc= mysql_real_query(mysql, SL(stmt_text));
3285   check_mysql_rc(rc, mysql);
3286   return OK;
3287 error:
3288   mysql_stmt_close(stmt);
3289   stmt_text= "drop table t1";
3290   rc= mysql_real_query(mysql, SL(stmt_text));
3291   check_mysql_rc(rc, mysql);
3292   return OK;
3293 }
3294 
test_derived(MYSQL * mysql)3295 static int test_derived(MYSQL *mysql)
3296 {
3297   MYSQL_STMT *stmt;
3298   int rc, i;
3299   MYSQL_BIND      my_bind[1];
3300   int32           my_val= 0;
3301   ulong           my_length= 0L;
3302   my_bool         my_null= FALSE;
3303   const char *query=
3304     "select count(1) from (select f.id from t1 f where f.id=?) as x";
3305 
3306 
3307   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
3308   check_mysql_rc(rc, mysql);
3309 
3310   rc= mysql_query(mysql, "create table t1 (id  int(8), primary key (id)) \
3311 ENGINE=InnoDB DEFAULT CHARSET=utf8");
3312   check_mysql_rc(rc, mysql);
3313 
3314   rc= mysql_query(mysql, "insert into t1 values (1)");
3315   check_mysql_rc(rc, mysql);
3316 
3317   stmt= mysql_stmt_init(mysql);
3318   FAIL_IF(!stmt, mysql_error(mysql));
3319   rc= mysql_stmt_prepare(stmt, SL(query));
3320   check_stmt_rc(rc, stmt);
3321 
3322   memset(my_bind, '\0', sizeof(my_bind));
3323 
3324   my_bind[0].buffer_type= MYSQL_TYPE_LONG;
3325   my_bind[0].buffer= (void *)&my_val;
3326   my_bind[0].length= &my_length;
3327   my_bind[0].is_null= &my_null;
3328   my_val= 1;
3329   rc= mysql_stmt_bind_param(stmt, my_bind);
3330   check_stmt_rc(rc, stmt);
3331 
3332   for (i= 0; i < 3; i++)
3333   {
3334     rc= mysql_stmt_execute(stmt);
3335     check_stmt_rc(rc, stmt);
3336     rc= 0;
3337     while (!mysql_stmt_fetch(stmt))
3338       rc++;
3339     FAIL_UNLESS(rc == 1, "rowcount != 1");
3340   }
3341   mysql_stmt_close(stmt);
3342 
3343   rc= mysql_query(mysql, "DROP TABLE t1");
3344   check_mysql_rc(rc, mysql);
3345   return OK;
3346 }
3347 
test_distinct(MYSQL * mysql)3348 static int test_distinct(MYSQL *mysql)
3349 {
3350   MYSQL_STMT *stmt;
3351   int rc, i;
3352   const char *query=
3353     "SELECT 2+count(distinct b), group_concat(a) FROM t1 group by a";
3354 
3355 
3356   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
3357   check_mysql_rc(rc, mysql);
3358 
3359   rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
3360   check_mysql_rc(rc, mysql);
3361 
3362   rc= mysql_query(mysql,
3363                   "insert into t1 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), \
3364 (1, 10), (2, 20), (3, 30), (4, 40), (5, 50);");
3365   check_mysql_rc(rc, mysql);
3366 
3367   for (i= 0; i < 3; i++)
3368   {
3369     stmt= mysql_stmt_init(mysql);
3370     FAIL_IF(!stmt, mysql_error(mysql));
3371     rc= mysql_stmt_prepare(stmt, SL(query));
3372     check_stmt_rc(rc, stmt);
3373     rc= mysql_stmt_execute(stmt);
3374     check_stmt_rc(rc, stmt);
3375 
3376     rc= 0;
3377     while (!mysql_stmt_fetch(stmt))
3378       rc++;
3379     FAIL_UNLESS(rc == 5, "rowcount != 5");
3380     mysql_stmt_close(stmt);
3381   }
3382 
3383   rc= mysql_query(mysql, "DROP TABLE t1");
3384   check_mysql_rc(rc, mysql);
3385   return OK;
3386 }
3387 
test_do_set(MYSQL * mysql)3388 static int test_do_set(MYSQL *mysql)
3389 {
3390   MYSQL_STMT *stmt_do, *stmt_set;
3391   char *query;
3392   int rc, i;
3393 
3394   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
3395   check_mysql_rc(rc, mysql);
3396 
3397   rc= mysql_query(mysql, "create table t1 (a int)");
3398   check_mysql_rc(rc, mysql);
3399 
3400   query= (char*)"do @var:=(1 in (select * from t1))";
3401   stmt_do= mysql_stmt_init(mysql);
3402   FAIL_IF(!stmt_do, mysql_error(mysql));
3403   rc= mysql_stmt_prepare(stmt_do, SL(query));
3404   check_stmt_rc(rc, stmt_do);
3405 
3406   query= (char*)"set @var=(1 in (select * from t1))";
3407   stmt_set= mysql_stmt_init(mysql);
3408   FAIL_IF(!stmt_set, mysql_error(mysql));
3409   rc= mysql_stmt_prepare(stmt_set, SL(query));
3410   check_stmt_rc(rc, stmt_set);
3411 
3412   for (i= 0; i < 3; i++)
3413   {
3414     rc= mysql_stmt_execute(stmt_do);
3415     check_stmt_rc(rc, stmt_do);
3416     rc= mysql_stmt_execute(stmt_set);
3417     check_stmt_rc(rc, stmt_set);
3418   }
3419 
3420   mysql_stmt_close(stmt_do);
3421   mysql_stmt_close(stmt_set);
3422   return OK;
3423 }
3424 
test_double_compare(MYSQL * mysql)3425 static int test_double_compare(MYSQL *mysql)
3426 {
3427   MYSQL_STMT *stmt;
3428   int        rc;
3429   char       real_data[10], tiny_data;
3430   double     double_data;
3431   MYSQL_RES  *result;
3432   MYSQL_BIND my_bind[3];
3433   ulong      length[3];
3434   char query[MAX_TEST_QUERY_LENGTH];
3435 
3436 
3437   rc= mysql_autocommit(mysql, TRUE);
3438   check_mysql_rc(rc, mysql);
3439 
3440   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_double_compare");
3441   check_mysql_rc(rc, mysql);
3442 
3443   rc= mysql_query(mysql, "CREATE TABLE test_double_compare(col1 tinyint, "
3444                          " col2 float, col3 double )");
3445   check_mysql_rc(rc, mysql);
3446 
3447   rc= mysql_query(mysql, "INSERT INTO test_double_compare "
3448                          "VALUES (1, 10.2, 34.5)");
3449   check_mysql_rc(rc, mysql);
3450 
3451   strcpy(query, "UPDATE test_double_compare SET col1=100 "
3452                 "WHERE col1 = ? AND col2 = ? AND COL3 = ?");
3453   stmt= mysql_stmt_init(mysql);
3454   FAIL_IF(!stmt, mysql_error(mysql));
3455   rc= mysql_stmt_prepare(stmt, SL(query));
3456   check_stmt_rc(rc, stmt);
3457 
3458   FAIL_IF(mysql_stmt_param_count(stmt) != 3, "param_count != 3");
3459 
3460   memset(my_bind, '\0', sizeof(my_bind));
3461 
3462   /* tinyint */
3463   my_bind[0].buffer_type= MYSQL_TYPE_TINY;
3464   my_bind[0].buffer= (void *)&tiny_data;
3465 
3466   /* string->float */
3467   my_bind[1].buffer_type= MYSQL_TYPE_STRING;
3468   my_bind[1].buffer= (void *)&real_data;
3469   my_bind[1].buffer_length= sizeof(real_data);
3470   my_bind[1].length= &length[1];
3471 
3472   /* double */
3473   my_bind[2].buffer_type= MYSQL_TYPE_DOUBLE;
3474   my_bind[2].buffer= (void *)&double_data;
3475 
3476   tiny_data= 1;
3477   strcpy(real_data, "10.2");
3478   length[1]= (ulong)strlen(real_data);
3479   double_data= 34.5;
3480   rc= mysql_stmt_bind_param(stmt, my_bind);
3481   check_stmt_rc(rc, stmt);
3482 
3483   rc= mysql_stmt_execute(stmt);
3484   check_stmt_rc(rc, stmt);
3485 
3486   FAIL_IF(mysql_stmt_affected_rows(stmt), "affected_rows != 0");
3487 
3488   mysql_stmt_close(stmt);
3489 
3490   /* now fetch the results ..*/
3491   rc= mysql_commit(mysql);
3492   check_mysql_rc(rc, mysql);
3493 
3494   /* test the results now, only one row should exist */
3495   rc= mysql_query(mysql, "SELECT * FROM test_double_compare");
3496   check_mysql_rc(rc, mysql);
3497 
3498   /* get the result */
3499   result= mysql_store_result(mysql);
3500   FAIL_IF(!result, "Invalid result set");
3501 
3502   rc= 0;
3503   while (mysql_fetch_row(result))
3504     rc++;
3505   FAIL_UNLESS((int)tiny_data == rc, "rowcount != tinydata");
3506   mysql_free_result(result);
3507   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_double_compare");
3508   check_mysql_rc(rc, mysql);
3509   return OK;
3510 }
3511 
test_multi(MYSQL * mysql)3512 static int test_multi(MYSQL *mysql)
3513 {
3514   MYSQL_STMT *stmt_delete, *stmt_update, *stmt_select1, *stmt_select2;
3515   char *query;
3516   MYSQL_BIND my_bind[1];
3517   int rc, i;
3518   int32 param= 1;
3519   ulong length= 1;
3520 
3521   /*
3522     We need to bzero bind structure because mysql_stmt_bind_param checks all
3523     its members.
3524   */
3525   memset(my_bind, '\0', sizeof(my_bind));
3526 
3527   my_bind[0].buffer_type= MYSQL_TYPE_LONG;
3528   my_bind[0].buffer= (void *)&param;
3529   my_bind[0].length= &length;
3530 
3531   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
3532   check_mysql_rc(rc, mysql);
3533 
3534   rc= mysql_query(mysql, "create table t1 (a int, b int)");
3535   check_mysql_rc(rc, mysql);
3536 
3537   rc= mysql_query(mysql, "create table t2 (a int, b int)");
3538   check_mysql_rc(rc, mysql);
3539 
3540   rc= mysql_query(mysql, "insert into t1 values (3, 3), (2, 2), (1, 1)");
3541   check_mysql_rc(rc, mysql);
3542 
3543   rc= mysql_query(mysql, "insert into t2 values (3, 3), (2, 2), (1, 1)");
3544   check_mysql_rc(rc, mysql);
3545 
3546   query= (char*)"delete t1, t2 from t1, t2 where t1.a=t2.a and t1.b=10";
3547   stmt_delete= mysql_stmt_init(mysql);
3548   FAIL_IF(!stmt_delete, mysql_error(mysql));
3549   rc= mysql_stmt_prepare(stmt_delete, SL(query));
3550   check_stmt_rc(rc, stmt_delete);
3551 
3552   query= (char*)"update t1, t2 set t1.b=10, t2.b=10 where t1.a=t2.a and t1.b=?";
3553   stmt_update= mysql_stmt_init(mysql);
3554   FAIL_IF(!stmt_update, mysql_error(mysql));
3555   rc= mysql_stmt_prepare(stmt_update, SL(query));
3556   check_stmt_rc(rc, stmt_update);
3557 
3558   query= (char*)"select * from t1";
3559   stmt_select1= mysql_stmt_init(mysql);
3560   FAIL_IF(!stmt_select1, mysql_error(mysql));
3561   rc= mysql_stmt_prepare(stmt_select1, SL(query));
3562   check_stmt_rc(rc, stmt_select1);
3563 
3564   query= (char*)"select * from t2";
3565   stmt_select2= mysql_stmt_init(mysql);
3566   FAIL_IF(!stmt_select2, mysql_error(mysql));
3567   rc= mysql_stmt_prepare(stmt_select2, SL(query));
3568   check_stmt_rc(rc, stmt_select2);
3569 
3570   for(i= 0; i < 3; i++)
3571   {
3572     rc= mysql_stmt_bind_param(stmt_update, my_bind);
3573     check_stmt_rc(rc, stmt_update);
3574 
3575     rc= mysql_stmt_execute(stmt_update);
3576     check_stmt_rc(rc, stmt_update);
3577 
3578     rc= mysql_stmt_execute(stmt_delete);
3579     check_stmt_rc(rc, stmt_delete);
3580 
3581     rc= mysql_stmt_execute(stmt_select1);
3582     check_stmt_rc(rc, stmt_select1);
3583     rc= 0;
3584     while (!mysql_stmt_fetch(stmt_select1))
3585       rc++;
3586     FAIL_UNLESS(rc == 3-param, "rc != 3 - param");
3587 
3588     rc= mysql_stmt_execute(stmt_select2);
3589     check_stmt_rc(rc, stmt_select2);
3590     rc= 0;
3591     while (!mysql_stmt_fetch(stmt_select2))
3592       rc++;
3593     FAIL_UNLESS(rc == 3-param, "rc != 3 - param");
3594 
3595     param++;
3596   }
3597 
3598   mysql_stmt_close(stmt_delete);
3599   mysql_stmt_close(stmt_update);
3600   mysql_stmt_close(stmt_select1);
3601   mysql_stmt_close(stmt_select2);
3602   rc= mysql_query(mysql, "drop table t1, t2");
3603   check_mysql_rc(rc, mysql);
3604 
3605   return OK;
3606 }
3607 
3608 /* Multiple stmts .. */
3609 
test_multi_stmt(MYSQL * mysql)3610 static int test_multi_stmt(MYSQL *mysql)
3611 {
3612 
3613   MYSQL_STMT  *stmt, *stmt1, *stmt2;
3614   int         rc;
3615   uint32      id;
3616   char        name[50];
3617   MYSQL_BIND  my_bind[2];
3618   ulong       length[2];
3619   my_bool     is_null[2];
3620   const char *query;
3621 
3622   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_multi_table");
3623   check_mysql_rc(rc, mysql);
3624 
3625   rc= mysql_query(mysql, "CREATE TABLE test_multi_table(id int, name char(20))");
3626   check_mysql_rc(rc, mysql);
3627 
3628   rc= mysql_query(mysql, "INSERT INTO test_multi_table values(10, 'mysql')");
3629   check_mysql_rc(rc, mysql);
3630 
3631   stmt= mysql_stmt_init(mysql);
3632   FAIL_IF(!stmt, mysql_error(mysql));
3633   query= "SELECT * FROM test_multi_table WHERE id=?";
3634   rc= mysql_stmt_prepare(stmt, SL(query));
3635   check_stmt_rc(rc, stmt);
3636 
3637   stmt2= mysql_stmt_init(mysql);
3638   FAIL_IF(!stmt2, mysql_error(mysql));
3639   query= "UPDATE test_multi_table SET name='updated' WHERE id=10";
3640   rc= mysql_stmt_prepare(stmt2, SL(query));
3641   check_stmt_rc(rc, stmt2);
3642 
3643   FAIL_IF(mysql_stmt_param_count(stmt) != 1, "param_count != 1");
3644 
3645   memset(my_bind, '\0', sizeof(my_bind));
3646 
3647   my_bind[0].buffer_type= MYSQL_TYPE_LONG;
3648   my_bind[0].buffer= (void *)&id;
3649   my_bind[0].is_null= &is_null[0];
3650   my_bind[0].length= &length[0];
3651   is_null[0]= 0;
3652   length[0]= 0;
3653 
3654   my_bind[1].buffer_type= MYSQL_TYPE_STRING;
3655   my_bind[1].buffer= (void *)name;
3656   my_bind[1].buffer_length= sizeof(name);
3657   my_bind[1].length= &length[1];
3658   my_bind[1].is_null= &is_null[1];
3659 
3660   rc= mysql_stmt_bind_param(stmt, my_bind);
3661   check_stmt_rc(rc, stmt);
3662 
3663   rc= mysql_stmt_bind_result(stmt, my_bind);
3664   check_stmt_rc(rc, stmt);
3665 
3666   id= 10;
3667   rc= mysql_stmt_execute(stmt);
3668   check_stmt_rc(rc, stmt);
3669 
3670   id= 999;
3671   rc= mysql_stmt_fetch(stmt);
3672   check_stmt_rc(rc, stmt);
3673 
3674   FAIL_UNLESS(id == 10, "id != 10");
3675   FAIL_UNLESS(strcmp(name, "mysql") == 0, "name != 'mysql'");
3676 
3677   rc= mysql_stmt_fetch(stmt);
3678   FAIL_UNLESS(rc == MYSQL_NO_DATA, "");
3679 
3680   /* alter the table schema now */
3681   stmt1= mysql_stmt_init(mysql);
3682   FAIL_IF(!stmt1, mysql_error(mysql));
3683   query= "DELETE FROM test_multi_table WHERE id=? AND CONVERT(name USING utf8)=?";
3684   rc= mysql_stmt_prepare(stmt1, SL(query));
3685   check_stmt_rc(rc, stmt1);
3686 
3687   FAIL_IF(mysql_stmt_param_count(stmt1) != 2, "param_count != 2");
3688 
3689   rc= mysql_stmt_bind_param(stmt1, my_bind);
3690   check_stmt_rc(rc, stmt1);
3691 
3692   rc= mysql_stmt_execute(stmt2);
3693   check_stmt_rc(rc, stmt2);
3694 
3695   FAIL_IF(mysql_stmt_affected_rows(stmt2) != 1, "affected_rows != 1");
3696 
3697   rc= mysql_stmt_execute(stmt);
3698   check_stmt_rc(rc, stmt);
3699 
3700   rc= mysql_stmt_fetch(stmt);
3701   check_stmt_rc(rc, stmt);
3702 
3703   FAIL_UNLESS(id == 10, "id != 10");
3704   FAIL_UNLESS(strcmp(name, "updated") == 0, "name != 'updated'");
3705 
3706   rc= mysql_stmt_fetch(stmt);
3707   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
3708 
3709   rc= mysql_stmt_execute(stmt1);
3710   check_stmt_rc(rc, stmt1);
3711 
3712   FAIL_IF(mysql_stmt_affected_rows(stmt1) != 1, "affected_rows != 1");
3713 
3714   mysql_stmt_close(stmt1);
3715 
3716   rc= mysql_stmt_execute(stmt);
3717   check_stmt_rc(rc, stmt);
3718 
3719   rc= mysql_stmt_fetch(stmt);
3720   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
3721 
3722   rc= my_stmt_result(mysql, "SELECT * FROM test_multi_table");
3723   FAIL_UNLESS(rc == 0, "rc != 0");
3724 
3725   mysql_stmt_close(stmt);
3726   mysql_stmt_close(stmt2);
3727   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_multi_table");
3728   check_mysql_rc(rc, mysql);
3729 
3730   return OK;
3731 }
3732 
3733 /* Test 'n' statements create and close */
3734 
test_nstmts(MYSQL * mysql)3735 static int test_nstmts(MYSQL *mysql)
3736 {
3737   MYSQL_STMT  *stmt;
3738   char        query[255];
3739   int         rc;
3740   static uint i, total_stmts= 2000;
3741   MYSQL_BIND  my_bind[1];
3742 
3743   SKIP_SKYSQL;
3744 
3745   mysql_autocommit(mysql, TRUE);
3746 
3747   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_nstmts");
3748   check_mysql_rc(rc, mysql);
3749 
3750   rc= mysql_query(mysql, "CREATE TABLE test_nstmts(id int)");
3751   check_mysql_rc(rc, mysql);
3752 
3753   memset(my_bind, '\0', sizeof(my_bind));
3754 
3755   my_bind[0].buffer= (void *)&i;
3756   my_bind[0].buffer_type= MYSQL_TYPE_LONG;
3757 
3758   for (i= 0; i < total_stmts; i++)
3759   {
3760     strcpy(query, "insert into test_nstmts values(?)");
3761     stmt= mysql_stmt_init(mysql);
3762     FAIL_IF(!stmt, mysql_error(mysql));
3763     rc= mysql_stmt_prepare(stmt, SL(query));
3764     check_stmt_rc(rc, stmt);
3765 
3766     rc= mysql_stmt_bind_param(stmt, my_bind);
3767     check_stmt_rc(rc, stmt);
3768 
3769     rc= mysql_stmt_execute(stmt);
3770     check_stmt_rc(rc, stmt);
3771 
3772     mysql_stmt_close(stmt);
3773   }
3774 
3775   stmt= mysql_stmt_init(mysql);
3776   FAIL_IF(!stmt, mysql_error(mysql));
3777   rc= mysql_stmt_prepare(stmt, SL(" select count(*) from test_nstmts"));
3778   check_stmt_rc(rc, stmt);
3779 
3780   rc= mysql_stmt_execute(stmt);
3781   check_stmt_rc(rc, stmt);
3782 
3783   i= 0;
3784   rc= mysql_stmt_bind_result(stmt, my_bind);
3785   check_stmt_rc(rc, stmt);
3786 
3787   rc= mysql_stmt_fetch(stmt);
3788   check_stmt_rc(rc, stmt);
3789   FAIL_UNLESS( i == total_stmts, "total_stmts != i");
3790 
3791   rc= mysql_stmt_fetch(stmt);
3792   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
3793 
3794   mysql_stmt_close(stmt);
3795 
3796   rc= mysql_query(mysql, "DROP TABLE test_nstmts");
3797   check_mysql_rc(rc, mysql);
3798   return OK;
3799 }
3800 
3801 /* Test simple null */
3802 
test_null(MYSQL * mysql)3803 static int test_null(MYSQL *mysql)
3804 {
3805   MYSQL_STMT *stmt;
3806   int        rc;
3807   uint       nData;
3808   MYSQL_BIND my_bind[2];
3809   my_bool    is_null[2];
3810   char query[MAX_TEST_QUERY_LENGTH];
3811 
3812 
3813   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_null");
3814   check_mysql_rc(rc, mysql);
3815 
3816   rc= mysql_query(mysql, "CREATE TABLE test_null(col1 int, col2 varchar(50))");
3817   check_mysql_rc(rc, mysql);
3818 
3819   rc= mysql_query(mysql, "FLUSH TABLES");
3820   check_mysql_rc(rc, mysql);
3821 
3822   rc= mysql_query(mysql, "START TRANSACTION");
3823   check_mysql_rc(rc, mysql);
3824 
3825   /* insert by prepare, wrong column name */
3826   strcpy(query, "INSERT INTO test_null(col3, col2) VALUES(?, ?)");
3827   stmt= mysql_stmt_init(mysql);
3828   FAIL_IF(!stmt, mysql_error(mysql));
3829   rc= mysql_stmt_prepare(stmt, SL(query));
3830   FAIL_IF(!rc, "Error expected");
3831   mysql_stmt_close(stmt);
3832 
3833   strcpy(query, "INSERT INTO test_null(col1, col2) VALUES(?, ?)");
3834   stmt= mysql_stmt_init(mysql);
3835   FAIL_IF(!stmt, mysql_error(mysql));
3836   rc= mysql_stmt_prepare(stmt, SL(query));
3837   check_stmt_rc(rc, stmt);
3838 
3839   FAIL_IF(mysql_stmt_param_count(stmt) != 2, "param_count != 2");
3840 
3841   memset(my_bind, '\0', sizeof(my_bind));
3842 
3843   my_bind[0].buffer_type= MYSQL_TYPE_LONG;
3844   my_bind[0].is_null= &is_null[0];
3845   is_null[0]= 1;
3846   my_bind[1]= my_bind[0];
3847 
3848   rc= mysql_stmt_bind_param(stmt, my_bind);
3849   check_stmt_rc(rc, stmt);
3850 
3851   /* now, execute the prepared statement to insert 10 records.. */
3852   for (nData= 0; nData<10; nData++)
3853   {
3854     rc= mysql_stmt_execute(stmt);
3855     check_stmt_rc(rc, stmt);
3856   }
3857 
3858   /* Re-bind with MYSQL_TYPE_NULL */
3859   my_bind[0].buffer_type= MYSQL_TYPE_NULL;
3860   is_null[0]= 0; /* reset */
3861   my_bind[1]= my_bind[0];
3862 
3863   rc= mysql_stmt_bind_param(stmt, my_bind);
3864   check_stmt_rc(rc, stmt);
3865 
3866   for (nData= 0; nData<10; nData++)
3867   {
3868     rc= mysql_stmt_execute(stmt);
3869     check_stmt_rc(rc, stmt);
3870   }
3871 
3872   mysql_stmt_close(stmt);
3873 
3874   /* now fetch the results ..*/
3875   rc= mysql_commit(mysql);
3876   check_mysql_rc(rc, mysql);
3877 
3878   nData*= 2;
3879   rc= my_stmt_result(mysql, "SELECT * FROM test_null");;
3880   FAIL_UNLESS((int) nData == rc, "rc != ndata");
3881 
3882   /* Fetch results */
3883   my_bind[0].buffer_type= MYSQL_TYPE_LONG;
3884   my_bind[0].buffer= (void *)&nData; /* this buffer won't be altered */
3885   my_bind[0].length= 0;
3886   my_bind[1]= my_bind[0];
3887   my_bind[0].is_null= &is_null[0];
3888   my_bind[1].is_null= &is_null[1];
3889 
3890   stmt= mysql_stmt_init(mysql);
3891   FAIL_IF(!stmt, mysql_error(mysql));
3892   rc= mysql_stmt_prepare(stmt, SL("SELECT * FROM test_null"));
3893   check_stmt_rc(rc, stmt);
3894 
3895   rc= mysql_stmt_execute(stmt);
3896   check_stmt_rc(rc, stmt);
3897 
3898   rc= mysql_stmt_bind_result(stmt, my_bind);
3899   check_stmt_rc(rc, stmt);
3900 
3901   rc= 0;
3902   is_null[0]= is_null[1]= 0;
3903   while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
3904   {
3905     FAIL_UNLESS(is_null[0], "!is_null");
3906     FAIL_UNLESS(is_null[1], "!is_null");
3907     rc++;
3908     is_null[0]= is_null[1]= 0;
3909   }
3910   FAIL_UNLESS(rc == (int) nData, "rc != nData");
3911   mysql_stmt_close(stmt);
3912   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_null");
3913   check_mysql_rc(rc, mysql);
3914   return OK;
3915 }
3916 
test_order_param(MYSQL * mysql)3917 static int test_order_param(MYSQL *mysql)
3918 {
3919   MYSQL_STMT *stmt;
3920   int rc;
3921   const char *query;
3922 
3923   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
3924   check_mysql_rc(rc, mysql);
3925 
3926   rc= mysql_query(mysql, "CREATE TABLE t1(a INT, b char(10))");
3927   check_mysql_rc(rc, mysql);
3928 
3929   stmt= mysql_stmt_init(mysql);
3930   FAIL_IF(!stmt, mysql_error(mysql));
3931   query= "select sum(a) + 200, 1 from t1 "
3932          " union distinct "
3933          "select sum(a) + 200, 1 from t1 group by b ";
3934   rc= mysql_stmt_prepare(stmt, SL(query));
3935   check_stmt_rc(rc, stmt);
3936   mysql_stmt_close(stmt);
3937 
3938   stmt= mysql_stmt_init(mysql);
3939   FAIL_IF(!stmt, mysql_error(mysql));
3940   query= "select sum(a) + 200, ? from t1 group by b "
3941          " union distinct "
3942          "select sum(a) + 200, 1 from t1 group by b ";
3943   rc= mysql_stmt_prepare(stmt, SL(query));
3944   check_stmt_rc(rc, stmt);
3945   mysql_stmt_close(stmt);
3946 
3947   stmt= mysql_stmt_init(mysql);
3948   FAIL_IF(!stmt, mysql_error(mysql));
3949   query= "select sum(a) + 200, ? from t1 "
3950          " union distinct "
3951          "select sum(a) + 200, 1 from t1 group by b ";
3952   rc= mysql_stmt_prepare(stmt, SL(query));
3953   check_stmt_rc(rc, stmt);
3954   mysql_stmt_close(stmt);
3955 
3956   rc= mysql_query(mysql, "DROP TABLE t1");
3957   check_mysql_rc(rc, mysql);
3958   return OK;
3959 }
3960 
test_rename(MYSQL * mysql)3961 static int test_rename(MYSQL *mysql)
3962 {
3963   MYSQL_STMT *stmt;
3964   const char *query= "rename table t1 to t2, t3 to t4";
3965   int rc;
3966 
3967   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2, t3, t4");
3968   check_mysql_rc(rc, mysql);
3969 
3970   stmt= mysql_stmt_init(mysql);
3971   FAIL_IF(!stmt, mysql_error(mysql));
3972   rc= mysql_stmt_prepare(stmt, SL(query));
3973   check_stmt_rc(rc, stmt);
3974 
3975   rc= mysql_query(mysql, "create table t1 (a int)");
3976   check_mysql_rc(rc, mysql);
3977 
3978   rc= mysql_stmt_execute(stmt);
3979   FAIL_IF(!rc, "Error expected");
3980 
3981   rc= mysql_query(mysql, "create table t3 (a int)");
3982   check_mysql_rc(rc, mysql);
3983 
3984   rc= mysql_stmt_execute(stmt);
3985   check_stmt_rc(rc, stmt);
3986 
3987   rc= mysql_stmt_execute(stmt);
3988   FAIL_IF(!rc, "Errr expected");
3989 
3990   rc= mysql_query(mysql, "rename table t2 to t1, t4 to t3");
3991   check_mysql_rc(rc, mysql);
3992 
3993   rc= mysql_stmt_execute(stmt);
3994   check_stmt_rc(rc, stmt);
3995 
3996   mysql_stmt_close(stmt);
3997 
3998   rc= mysql_query(mysql, "DROP TABLE t2, t4");
3999   check_mysql_rc(rc, mysql);
4000   return OK;
4001 }
4002 
test_rewind(MYSQL * mysql)4003 static int test_rewind(MYSQL *mysql)
4004 {
4005   MYSQL_STMT *stmt;
4006   MYSQL_BIND my_bind;
4007   int rc = 0;
4008   const char *stmt_text;
4009   long unsigned int length=4, Data=0;
4010   my_bool isnull=0;
4011 
4012 
4013   stmt_text= "CREATE TABLE t1 (a int)";
4014   rc= mysql_real_query(mysql, SL(stmt_text));
4015   check_mysql_rc(rc, mysql);
4016   stmt_text= "INSERT INTO t1 VALUES(2),(3),(4)";
4017   rc= mysql_real_query(mysql, SL(stmt_text));
4018   check_mysql_rc(rc, mysql);
4019 
4020   stmt= mysql_stmt_init(mysql);
4021   stmt_text= "SELECT * FROM t1";
4022   rc= mysql_stmt_prepare(stmt, SL(stmt_text));
4023   check_stmt_rc(rc, stmt);
4024 
4025   memset(&my_bind, '\0', sizeof(MYSQL_BIND));
4026   my_bind.buffer_type= MYSQL_TYPE_LONG;
4027   my_bind.buffer= (void *)&Data; /* this buffer won't be altered */
4028   my_bind.length= &length;
4029   my_bind.is_null= &isnull;
4030 
4031   rc= mysql_stmt_execute(stmt);
4032   check_stmt_rc(rc, stmt);
4033 
4034   rc= mysql_stmt_store_result(stmt);
4035   check_stmt_rc(rc, stmt);
4036 
4037   rc= mysql_stmt_bind_result(stmt, &my_bind);
4038   check_stmt_rc(rc, stmt);
4039 
4040   /* retrieve all result sets till we are at the end */
4041   while(!(rc=mysql_stmt_fetch(stmt)));
4042   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
4043 
4044   /* seek to the first row */
4045   mysql_stmt_data_seek(stmt, 0);
4046 
4047   /* now we should be able to fetch the results again */
4048   /* but mysql_stmt_fetch returns MYSQL_NO_DATA */
4049   while(!(rc= mysql_stmt_fetch(stmt)));
4050 
4051   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
4052 
4053   stmt_text= "DROP TABLE t1";
4054   rc= mysql_real_query(mysql, SL(stmt_text));
4055   check_mysql_rc(rc, mysql);
4056   rc= mysql_stmt_free_result(stmt);
4057   rc= mysql_stmt_close(stmt);
4058   return OK;
4059 }
4060 
4061 /* Test simple select */
4062 
test_select(MYSQL * mysql)4063 static int test_select(MYSQL *mysql)
4064 {
4065   MYSQL_STMT *stmt;
4066   int        rc;
4067   char       szData[25];
4068   int        nData= 1;
4069   MYSQL_BIND my_bind[2];
4070   ulong length[2];
4071   char query[MAX_TEST_QUERY_LENGTH];
4072 
4073 
4074   rc= mysql_autocommit(mysql, TRUE);
4075   check_mysql_rc(rc, mysql);
4076 
4077   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
4078   check_mysql_rc(rc, mysql);
4079 
4080   rc= mysql_query(mysql, "CREATE TABLE test_select(id int, name varchar(50))");
4081   check_mysql_rc(rc, mysql);
4082 
4083   /* insert a row and commit the transaction */
4084   rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 'venu')");
4085   check_mysql_rc(rc, mysql);
4086 
4087   /* now insert the second row, and roll back the transaction */
4088   rc= mysql_query(mysql, "INSERT INTO test_select VALUES(20, 'mysql')");
4089   check_mysql_rc(rc, mysql);
4090 
4091   rc= mysql_commit(mysql);
4092   check_mysql_rc(rc, mysql);
4093 
4094   strcpy(query, "SELECT * FROM test_select WHERE id= ? "
4095                 "AND CONVERT(name USING utf8) =?");
4096   stmt= mysql_stmt_init(mysql);
4097   FAIL_IF(!stmt, mysql_error(mysql));
4098   rc= mysql_stmt_prepare(stmt, SL(query));
4099   check_stmt_rc(rc, stmt);
4100 
4101   FAIL_IF(mysql_stmt_param_count(stmt) != 2, "param_count != 2");
4102 
4103   memset(my_bind, '\0', sizeof(my_bind));
4104 
4105   /* string data */
4106   nData= 10;
4107   strcpy(szData, (char *)"venu");
4108   my_bind[1].buffer_type= MYSQL_TYPE_STRING;
4109   my_bind[1].buffer= (void *)szData;
4110   my_bind[1].buffer_length= 4;
4111   my_bind[1].length= &length[1];
4112   length[1]= 4;
4113 
4114   my_bind[0].buffer= (void *)&nData;
4115   my_bind[0].buffer_type= MYSQL_TYPE_LONG;
4116 
4117   rc= mysql_stmt_bind_param(stmt, my_bind);
4118   check_stmt_rc(rc, stmt);
4119 
4120   rc= mysql_stmt_execute(stmt);
4121   check_stmt_rc(rc, stmt);
4122 
4123   rc= 0;
4124   while (!mysql_stmt_fetch(stmt))
4125     rc++;
4126   FAIL_UNLESS(rc == 1, "rc != 1");
4127 
4128   mysql_stmt_close(stmt);
4129   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
4130   check_mysql_rc(rc, mysql);
4131   return OK;
4132 }
4133 
4134 /* Test simple select with prepare */
4135 
test_select_prepare(MYSQL * mysql)4136 static int test_select_prepare(MYSQL *mysql)
4137 {
4138   int        rc;
4139   MYSQL_STMT *stmt;
4140 
4141 
4142   rc= mysql_autocommit(mysql, TRUE);
4143   check_mysql_rc(rc, mysql);
4144 
4145   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
4146   check_mysql_rc(rc, mysql);
4147 
4148   rc= mysql_query(mysql, "CREATE TABLE test_select(id int, name varchar(50))");
4149   check_mysql_rc(rc, mysql);
4150 
4151   /* insert a row and commit the transaction */
4152   rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 'venu')");
4153   check_mysql_rc(rc, mysql);
4154 
4155   rc= mysql_commit(mysql);
4156   check_mysql_rc(rc, mysql);
4157 
4158   stmt= mysql_stmt_init(mysql);
4159   FAIL_IF(!stmt, mysql_error(mysql));
4160   rc= mysql_stmt_prepare(stmt, SL("SELECT * FROM test_select"));
4161   check_stmt_rc(rc, stmt);
4162 
4163   rc= mysql_stmt_execute(stmt);
4164   check_stmt_rc(rc, stmt);
4165 
4166   rc= 0;
4167   while (!mysql_stmt_fetch(stmt))
4168     rc++;
4169   FAIL_UNLESS(rc == 1, "rowcount != 1");
4170   mysql_stmt_close(stmt);
4171 
4172   rc= mysql_query(mysql, "DROP TABLE test_select");
4173   check_mysql_rc(rc, mysql);
4174 
4175   rc= mysql_query(mysql, "CREATE TABLE test_select(id tinyint, id1 int, "
4176                                                 "  id2 float, id3 float, "
4177                                                 "  name varchar(50))");
4178   check_mysql_rc(rc, mysql);
4179 
4180   /* insert a row and commit the transaction */
4181   rc= mysql_query(mysql, "INSERT INTO test_select(id, id1, id2, name) VALUES(10, 5, 2.3, 'venu')");
4182   check_mysql_rc(rc, mysql);
4183 
4184   rc= mysql_commit(mysql);
4185   check_mysql_rc(rc, mysql);
4186 
4187   stmt= mysql_stmt_init(mysql);
4188   FAIL_IF(!stmt, mysql_error(mysql));
4189   rc= mysql_stmt_prepare(stmt, SL("SELECT * FROM test_select"));
4190   check_stmt_rc(rc, stmt);
4191 
4192   rc= mysql_stmt_execute(stmt);
4193   check_stmt_rc(rc, stmt);
4194 
4195   rc= 0;
4196   while (!mysql_stmt_fetch(stmt))
4197     rc++;
4198   FAIL_UNLESS(rc == 1, "rowcount != 1");
4199   mysql_stmt_close(stmt);
4200   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
4201   check_mysql_rc(rc, mysql);
4202   return OK;
4203 }
4204 
4205 /* Test simple show */
4206 
test_select_show_table(MYSQL * mysql)4207 static int test_select_show_table(MYSQL *mysql)
4208 {
4209   MYSQL_STMT *stmt;
4210   int        rc, i;
4211 
4212   stmt= mysql_stmt_init(mysql);
4213   FAIL_IF(!stmt, mysql_error(mysql));
4214   rc= mysql_stmt_prepare(stmt, SL("SHOW TABLES FROM mysql"));
4215   check_stmt_rc(rc, stmt);
4216 
4217   FAIL_IF(mysql_stmt_param_count(stmt), "param_count != 0");
4218 
4219   for (i= 1; i < 3; i++)
4220   {
4221     rc= mysql_stmt_execute(stmt);
4222     check_stmt_rc(rc, stmt);
4223   }
4224 
4225   while (!mysql_stmt_fetch(stmt));
4226   mysql_stmt_close(stmt);
4227   return OK;
4228 }
4229 
4230 /* Test simple select */
4231 
test_select_version(MYSQL * mysql)4232 static int test_select_version(MYSQL *mysql)
4233 {
4234   MYSQL_STMT *stmt;
4235   int        rc;
4236 
4237 
4238   stmt= mysql_stmt_init(mysql);
4239   FAIL_IF(!stmt, mysql_error(mysql));
4240   rc= mysql_stmt_prepare(stmt, SL("SELECT @@version"));
4241   check_stmt_rc(rc, stmt);
4242 
4243   FAIL_IF(mysql_stmt_param_count(stmt), "param_count != 0");
4244 
4245   rc= mysql_stmt_execute(stmt);
4246   check_stmt_rc(rc, stmt);
4247 
4248   while (!mysql_stmt_fetch(stmt));
4249   mysql_stmt_close(stmt);
4250   return OK;
4251 }
4252 
test_selecttmp(MYSQL * mysql)4253 static int test_selecttmp(MYSQL *mysql)
4254 {
4255   MYSQL_STMT *stmt;
4256   int rc, i;
4257   const char *query= "select a, (select count(distinct t1.b) as sum from t1, t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) from t3";
4258 
4259 
4260   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2, t3");
4261   check_mysql_rc(rc, mysql);
4262 
4263   rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
4264   check_mysql_rc(rc, mysql);
4265 
4266   rc= mysql_query(mysql, "create table t2 (a int, b int);");
4267   check_mysql_rc(rc, mysql);
4268 
4269   rc= mysql_query(mysql, "create table t3 (a int, b int);");
4270   check_mysql_rc(rc, mysql);
4271 
4272   rc= mysql_query(mysql,
4273                   "insert into t1 values (0, 100), (1, 2), (1, 3), (2, 2), (2, 7), \
4274 (2, -1), (3, 10);");
4275   check_mysql_rc(rc, mysql);
4276   rc= mysql_query(mysql,
4277                   "insert into t2 values (0, 0), (1, 1), (2, 1), (3, 1), (4, 1);");
4278   check_mysql_rc(rc, mysql);
4279   rc= mysql_query(mysql,
4280                   "insert into t3 values (3, 3), (2, 2), (1, 1);");
4281   check_mysql_rc(rc, mysql);
4282 
4283   stmt= mysql_stmt_init(mysql);
4284   FAIL_IF(!stmt, mysql_error(mysql));
4285   rc= mysql_stmt_prepare(stmt, SL(query));
4286   check_stmt_rc(rc, stmt);
4287   for (i= 0; i < 3; i++)
4288   {
4289     rc= mysql_stmt_execute(stmt);
4290     check_stmt_rc(rc, stmt);
4291     rc= 0;
4292     while (!mysql_stmt_fetch(stmt))
4293       rc++;
4294     FAIL_UNLESS(rc == 3, "rowcount != 3");
4295   }
4296   mysql_stmt_close(stmt);
4297 
4298   rc= mysql_query(mysql, "DROP TABLE t1, t2, t3");
4299   check_mysql_rc(rc, mysql);
4300   return OK;
4301 }
4302 
test_set_option(MYSQL * mysql)4303 static int test_set_option(MYSQL *mysql)
4304 {
4305   MYSQL_STMT *stmt;
4306   MYSQL_RES  *result;
4307   int        rc;
4308 
4309 
4310   mysql_autocommit(mysql, TRUE);
4311 
4312   /* LIMIT the rows count to 2 */
4313   rc= mysql_query(mysql, "SET SQL_SELECT_LIMIT= 2");
4314   check_mysql_rc(rc, mysql);
4315 
4316   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_limit");
4317   check_mysql_rc(rc, mysql);
4318 
4319   rc= mysql_query(mysql, "CREATE TABLE test_limit(a tinyint)");
4320   check_mysql_rc(rc, mysql);
4321 
4322   rc= mysql_query(mysql, "INSERT INTO test_limit VALUES(10), (20), (30), (40)");
4323   check_mysql_rc(rc, mysql);
4324 
4325   rc= mysql_query(mysql, "SELECT * FROM test_limit");
4326   check_mysql_rc(rc, mysql);
4327 
4328   result= mysql_store_result(mysql);
4329   FAIL_IF(!result, "Invalid result set");
4330 
4331   rc= 0;
4332   while (mysql_fetch_row(result))
4333     rc++;
4334   FAIL_UNLESS(rc == 2, "rowcunt != 2");
4335   mysql_free_result(result);
4336 
4337   stmt= mysql_stmt_init(mysql);
4338   FAIL_IF(!stmt, mysql_error(mysql));
4339   rc= mysql_stmt_prepare(stmt, SL("SELECT * FROM test_limit"));
4340   check_stmt_rc(rc, stmt);
4341 
4342   rc= mysql_stmt_execute(stmt);
4343   check_stmt_rc(rc, stmt);
4344 
4345   rc= 0;
4346   while (!mysql_stmt_fetch(stmt))
4347     rc++;
4348   FAIL_UNLESS(rc == 2, "");
4349 
4350   mysql_stmt_close(stmt);
4351 
4352   /* RESET the LIMIT the rows count to 0 */
4353   rc= mysql_query(mysql, "SET SQL_SELECT_LIMIT=DEFAULT");
4354   check_mysql_rc(rc, mysql);
4355 
4356   stmt= mysql_stmt_init(mysql);
4357   FAIL_IF(!stmt, mysql_error(mysql));
4358   rc= mysql_stmt_prepare(stmt, SL("SELECT * FROM test_limit"));
4359   check_stmt_rc(rc, stmt);
4360 
4361   rc= mysql_stmt_execute(stmt);
4362   check_stmt_rc(rc, stmt);
4363 
4364   rc= 0;
4365   while (!mysql_stmt_fetch(stmt))
4366     rc++;
4367   FAIL_UNLESS(rc == 4, "rowcount != 4");
4368 
4369   mysql_stmt_close(stmt);
4370   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_limit");
4371   check_mysql_rc(rc, mysql);
4372   return OK;
4373 }
4374 
4375 /* Test simple set-variable prepare */
4376 
test_set_variable(MYSQL * mysql)4377 static int test_set_variable(MYSQL *mysql)
4378 {
4379   MYSQL_STMT *stmt, *stmt1;
4380   int        rc;
4381   int        set_count, def_count, get_count;
4382   ulong      length;
4383   char       var[NAME_LEN+1];
4384   MYSQL_BIND set_bind[1], get_bind[2];
4385 
4386 
4387   mysql_autocommit(mysql, TRUE);
4388 
4389   stmt1= mysql_stmt_init(mysql);
4390   FAIL_IF(!stmt1, mysql_error(mysql));
4391   rc= mysql_stmt_prepare(stmt1, SL("show variables like 'max_error_count'"));
4392   check_stmt_rc(rc, stmt1);
4393 
4394   memset(get_bind, '\0', sizeof(get_bind));
4395 
4396   get_bind[0].buffer_type= MYSQL_TYPE_STRING;
4397   get_bind[0].buffer= (void *)var;
4398   get_bind[0].length= &length;
4399   get_bind[0].buffer_length= (int)NAME_LEN;
4400   length= NAME_LEN;
4401 
4402   get_bind[1].buffer_type= MYSQL_TYPE_LONG;
4403   get_bind[1].buffer= (void *)&get_count;
4404 
4405   rc= mysql_stmt_execute(stmt1);
4406   check_stmt_rc(rc, stmt1);
4407 
4408   rc= mysql_stmt_bind_result(stmt1, get_bind);
4409   check_stmt_rc(rc, stmt1);
4410 
4411   rc= mysql_stmt_fetch(stmt1);
4412   check_stmt_rc(rc, stmt1);
4413 
4414   def_count= get_count;
4415 
4416   FAIL_UNLESS(strcmp(var, "max_error_count") == 0, "var != max_error_count");
4417   rc= mysql_stmt_fetch(stmt1);
4418   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
4419 
4420   stmt= mysql_stmt_init(mysql);
4421   FAIL_IF(!stmt, mysql_error(mysql));
4422   rc= mysql_stmt_prepare(stmt, SL("set max_error_count=?"));
4423   check_stmt_rc(rc, stmt);
4424 
4425   memset(set_bind, '\0', sizeof(set_bind));
4426 
4427   set_bind[0].buffer_type= MYSQL_TYPE_LONG;
4428   set_bind[0].buffer= (void *)&set_count;
4429 
4430   rc= mysql_stmt_bind_param(stmt, set_bind);
4431   check_stmt_rc(rc, stmt);
4432 
4433   set_count= 31;
4434   rc= mysql_stmt_execute(stmt);
4435   check_stmt_rc(rc, stmt);
4436 
4437   mysql_commit(mysql);
4438 
4439   rc= mysql_stmt_execute(stmt1);
4440   check_stmt_rc(rc, stmt1);
4441 
4442   rc= mysql_stmt_fetch(stmt1);
4443   check_stmt_rc(rc, stmt1);
4444 
4445   FAIL_UNLESS(get_count == set_count, "get_count != set_count");
4446 
4447   rc= mysql_stmt_fetch(stmt1);
4448   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
4449 
4450   /* restore back to default */
4451   set_count= def_count;
4452   rc= mysql_stmt_execute(stmt);
4453   check_stmt_rc(rc, stmt);
4454 
4455   rc= mysql_stmt_execute(stmt1);
4456   check_stmt_rc(rc, stmt1);
4457 
4458   rc= mysql_stmt_fetch(stmt1);
4459   check_stmt_rc(rc, stmt1);
4460 
4461   FAIL_UNLESS(get_count == set_count, "get_count != set_count");
4462 
4463   rc= mysql_stmt_fetch(stmt1);
4464   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
4465 
4466   mysql_stmt_close(stmt);
4467   mysql_stmt_close(stmt1);
4468   return OK;
4469 }
4470 
4471 /* Test SQLmode */
4472 
test_sqlmode(MYSQL * mysql)4473 static int test_sqlmode(MYSQL *mysql)
4474 {
4475   MYSQL_STMT *stmt;
4476   MYSQL_BIND my_bind[2];
4477   char       c1[5], c2[5];
4478   int        rc;
4479   int        ignore_space= 0;
4480   char query[MAX_TEST_QUERY_LENGTH];
4481 
4482 
4483   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_piping");
4484   check_mysql_rc(rc, mysql);
4485 
4486   rc= mysql_query(mysql, "CREATE TABLE test_piping(name varchar(10))");
4487   check_mysql_rc(rc, mysql);
4488 
4489   /* PIPES_AS_CONCAT */
4490   strcpy(query, "SET SQL_MODE= \"PIPES_AS_CONCAT\"");
4491   rc= mysql_query(mysql, query);
4492   check_mysql_rc(rc, mysql);
4493 
4494   strcpy(query, "INSERT INTO test_piping VALUES(?||?)");
4495   stmt= mysql_stmt_init(mysql);
4496   FAIL_IF(!stmt, mysql_error(mysql));
4497   rc= mysql_stmt_prepare(stmt, SL(query));
4498   check_stmt_rc(rc, stmt);
4499 
4500   memset(my_bind, '\0', sizeof(my_bind));
4501 
4502   my_bind[0].buffer_type= MYSQL_TYPE_STRING;
4503   my_bind[0].buffer= (void *)c1;
4504   my_bind[0].buffer_length= 2;
4505 
4506   my_bind[1].buffer_type= MYSQL_TYPE_STRING;
4507   my_bind[1].buffer= (void *)c2;
4508   my_bind[1].buffer_length= 3;
4509 
4510   rc= mysql_stmt_bind_param(stmt, my_bind);
4511   check_stmt_rc(rc, stmt);
4512 
4513   strcpy(c1, "My"); strcpy(c2, "SQL");
4514   rc= mysql_stmt_execute(stmt);
4515   check_stmt_rc(rc, stmt);
4516   mysql_stmt_close(stmt);
4517 
4518   if (verify_col_data(mysql, "test_piping", "name", "MySQL"))
4519     return FAIL;
4520 
4521   rc= mysql_query(mysql, "DELETE FROM test_piping");
4522   check_mysql_rc(rc, mysql);
4523 
4524   strcpy(query, "SELECT connection_id    ()");
4525   stmt= mysql_stmt_init(mysql);
4526   FAIL_IF(!stmt, mysql_error(mysql));
4527   rc= mysql_stmt_prepare(stmt, SL(query));
4528   check_stmt_rc(rc, stmt);
4529   mysql_stmt_close(stmt);
4530 
4531   /* ANSI */
4532   strcpy(query, "SET SQL_MODE= \"ANSI\"");
4533   rc= mysql_query(mysql, query);
4534   check_mysql_rc(rc, mysql);
4535 
4536   strcpy(query, "INSERT INTO test_piping VALUES(?||?)");
4537   stmt= mysql_stmt_init(mysql);
4538   FAIL_IF(!stmt, mysql_error(mysql));
4539   rc= mysql_stmt_prepare(stmt, SL(query));
4540   check_stmt_rc(rc, stmt);
4541 
4542   rc= mysql_stmt_bind_param(stmt, my_bind);
4543   check_stmt_rc(rc, stmt);
4544 
4545   strcpy(c1, "My"); strcpy(c2, "SQL");
4546   rc= mysql_stmt_execute(stmt);
4547   check_stmt_rc(rc, stmt);
4548 
4549   mysql_stmt_close(stmt);
4550   if (verify_col_data(mysql, "test_piping", "name", "MySQL"))
4551     return FAIL;
4552 
4553   /* ANSI mode spaces ...
4554     skip, if ignore_space was set
4555   */
4556   query_int_variable(mysql, "@@sql_mode LIKE '%IGNORE_SPACE%'", &ignore_space);
4557 
4558   if (!ignore_space)
4559   {
4560     strcpy(query, "SELECT connection_id ()");
4561     stmt= mysql_stmt_init(mysql);
4562     FAIL_IF(!stmt, mysql_error(mysql));
4563     rc= mysql_stmt_prepare(stmt, SL(query));
4564     check_stmt_rc(rc, stmt);
4565 
4566     rc= mysql_stmt_execute(stmt);
4567     check_stmt_rc(rc, stmt);
4568 
4569     rc= mysql_stmt_fetch(stmt);
4570     check_stmt_rc(rc, stmt);
4571 
4572     rc= mysql_stmt_fetch(stmt);
4573     FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
4574 
4575     mysql_stmt_close(stmt);
4576   }
4577   /* IGNORE SPACE MODE */
4578   strcpy(query, "SET SQL_MODE= \"IGNORE_SPACE\"");
4579   rc= mysql_query(mysql, query);
4580   check_mysql_rc(rc, mysql);
4581 
4582   strcpy(query, "SELECT connection_id    ()");
4583   stmt= mysql_stmt_init(mysql);
4584   FAIL_IF(!stmt, mysql_error(mysql));
4585   rc= mysql_stmt_prepare(stmt, SL(query));
4586   check_stmt_rc(rc, stmt);
4587 
4588   rc= mysql_stmt_execute(stmt);
4589   check_stmt_rc(rc, stmt);
4590 
4591   rc= mysql_stmt_fetch(stmt);
4592   check_stmt_rc(rc, stmt);
4593 
4594   rc= mysql_stmt_fetch(stmt);
4595   FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");
4596 
4597   mysql_stmt_close(stmt);
4598   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_piping");
4599   check_mysql_rc(rc, mysql);
4600   return OK;
4601 }
4602 
4603 /* Test mysql_stmt_close for open stmts */
4604 
test_stmt_close(MYSQL * mysql)4605 static int test_stmt_close(MYSQL *mysql)
4606 {
4607   MYSQL_STMT *stmt1, *stmt2, *stmt3, *stmt_x;
4608   MYSQL_BIND  my_bind[1];
4609   MYSQL_RES   *result;
4610   unsigned int  count;
4611   int   rc;
4612   char query[MAX_TEST_QUERY_LENGTH];
4613   my_bool reconnect= 1;
4614 
4615   mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect);
4616 
4617   /* set AUTOCOMMIT to ON*/
4618   mysql_autocommit(mysql, TRUE);
4619 
4620   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_stmt_close");
4621   check_mysql_rc(rc, mysql);
4622 
4623   rc= mysql_query(mysql, "CREATE TABLE test_stmt_close(id int)");
4624   check_mysql_rc(rc, mysql);
4625 
4626   strcpy(query, "DO \"nothing\"");
4627   stmt1= mysql_stmt_init(mysql);
4628   FAIL_IF(!stmt1, mysql_error(mysql));
4629   rc= mysql_stmt_prepare(stmt1, SL(query));
4630   check_stmt_rc(rc, stmt1);
4631 
4632   FAIL_IF(mysql_stmt_param_count(stmt1), "param_count != 0");
4633 
4634   strcpy(query, "INSERT INTO test_stmt_close(id) VALUES(?)");
4635   stmt_x= mysql_stmt_init(mysql);
4636   FAIL_IF(!stmt_x, mysql_error(mysql));
4637   rc= mysql_stmt_prepare(stmt_x, SL(query));
4638   check_stmt_rc(rc, stmt_x);
4639 
4640   FAIL_IF(mysql_stmt_param_count(stmt_x) != 1, "param_count != 1");
4641 
4642   strcpy(query, "UPDATE test_stmt_close SET id= ? WHERE id= ?");
4643   stmt3= mysql_stmt_init(mysql);
4644   FAIL_IF(!stmt3, mysql_error(mysql));
4645   rc= mysql_stmt_prepare(stmt3, SL(query));
4646   check_stmt_rc(rc, stmt3);
4647 
4648   FAIL_IF(mysql_stmt_param_count(stmt3) != 2, "param_count != 2");
4649 
4650   strcpy(query, "SELECT * FROM test_stmt_close WHERE id= ?");
4651   stmt2= mysql_stmt_init(mysql);
4652   FAIL_IF(!stmt2, mysql_error(mysql));
4653   rc= mysql_stmt_prepare(stmt2, SL(query));
4654   check_stmt_rc(rc, stmt2);
4655 
4656   FAIL_IF(mysql_stmt_param_count(stmt2) != 1, "param_count != 1");
4657 
4658   rc= mysql_stmt_close(stmt1);
4659 
4660   /*
4661     Originally we were going to close all statements automatically in
4662     mysql_close(). This proved to not work well - users weren't able to
4663     close statements by hand once mysql_close() had been called.
4664     Now mysql_close() doesn't free any statements, so this test doesn't
4665     serve its original designation any more.
4666     Here we free stmt2 and stmt3 by hand to avoid memory leaks.
4667   */
4668   mysql_stmt_close(stmt2);
4669   mysql_stmt_close(stmt3);
4670 
4671   /*
4672     We need to bzero bind structure because mysql_stmt_bind_param checks all
4673     its members.
4674   */
4675   memset(my_bind, '\0', sizeof(my_bind));
4676 
4677   my_bind[0].buffer= (void *)&count;
4678   my_bind[0].buffer_type= MYSQL_TYPE_LONG;
4679   count= 100;
4680 
4681   rc= mysql_stmt_bind_param(stmt_x, my_bind);
4682   check_stmt_rc(rc, stmt_x);
4683 
4684   rc= mysql_stmt_execute(stmt_x);
4685   check_stmt_rc(rc, stmt_x);
4686 
4687   FAIL_IF(mysql_stmt_affected_rows(stmt_x) != 1, "affected_rows != 1");
4688 
4689   rc= mysql_stmt_close(stmt_x);
4690   check_stmt_rc(rc, stmt_x);
4691 
4692   rc= mysql_query(mysql, "SELECT id FROM test_stmt_close");
4693   check_mysql_rc(rc, mysql);
4694 
4695   result= mysql_store_result(mysql);
4696   FAIL_IF(!result, "Invalid result set");
4697 
4698   rc= 0;
4699   while (mysql_fetch_row(result))
4700     rc++;
4701   FAIL_UNLESS(rc == 1, "rwcount != 1");
4702   mysql_free_result(result);
4703   rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_stmt_close");
4704   check_mysql_rc(rc, mysql);
4705   return OK;
4706 }
4707 
test_new_date(MYSQL * mysql)4708 static int test_new_date(MYSQL *mysql)
4709 {
4710   MYSQL_STMT *stmt;
4711   MYSQL_BIND bind[1];
4712   int rc;
4713   char buffer[50];
4714   my_bool reconnect= 1;
4715   mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect);
4716 
4717   /* set AUTOCOMMIT to ON*/
4718   mysql_autocommit(mysql, TRUE);
4719 
4720   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4721   check_mysql_rc(rc, mysql);
4722 
4723   rc= mysql_query(mysql, "CREATE TABLE t1 (a date, b date)");
4724   check_mysql_rc(rc, mysql);
4725 
4726   rc= mysql_query(mysql, "INSERT INTO t1 VALUES (now(), now() + INTERVAL 1 day)");
4727   check_mysql_rc(rc, mysql);
4728 
4729   stmt= mysql_stmt_init(mysql);
4730   rc= mysql_stmt_prepare(stmt, "SELECT if(1, a, b) FROM t1", 26);
4731   check_stmt_rc(rc, stmt);
4732 
4733   memset(bind, 0, sizeof(MYSQL_BIND));
4734   bind[0].buffer_length= 50;
4735   bind[0].buffer= (void *)buffer;
4736   bind[0].buffer_type= MYSQL_TYPE_STRING;
4737 
4738   rc= mysql_stmt_execute(stmt);
4739   check_stmt_rc(rc, stmt);
4740 
4741   rc= mysql_stmt_bind_result(stmt, bind);
4742   check_stmt_rc(rc, stmt);
4743 
4744   rc= mysql_stmt_fetch(stmt);
4745   check_stmt_rc(rc, stmt);
4746 
4747   rc= mysql_stmt_fetch(stmt);
4748   FAIL_IF(rc != MYSQL_NO_DATA, "NO DATA expected");
4749 
4750   mysql_stmt_close(stmt);
4751   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4752   check_mysql_rc(rc, mysql);
4753   return OK;
4754 }
4755 
test_long_data1(MYSQL * mysql)4756 static int test_long_data1(MYSQL *mysql)
4757 {
4758   MYSQL_STMT *stmt;
4759   int        rc;
4760   MYSQL_BIND bind[1];
4761   char query[MAX_TEST_QUERY_LENGTH];
4762   const char *data= "12345";
4763 
4764   rc= mysql_autocommit(mysql, TRUE);
4765   check_mysql_rc(rc, mysql);
4766 
4767   rc= mysql_query(mysql, "DROP TABLE IF EXISTS tld");
4768   check_mysql_rc(rc, mysql);
4769 
4770   rc= mysql_query(mysql, "CREATE TABLE tld (col1 int, "
4771                          "col2 long varbinary)");
4772   check_mysql_rc(rc, mysql);
4773   rc= mysql_query(mysql, "INSERT INTO tld VALUES (1,'test')");
4774   check_mysql_rc(rc, mysql);
4775 
4776   strcpy(query, "UPDATE tld SET col2=? WHERE col1=1");
4777   stmt= mysql_stmt_init(mysql);
4778   FAIL_IF(!stmt, mysql_error(mysql));
4779   rc= mysql_stmt_prepare(stmt, SL(query));
4780   check_stmt_rc(rc, stmt);
4781   memset(bind, 0, sizeof(MYSQL_BIND));
4782   bind[0].buffer_type= MYSQL_TYPE_STRING;
4783   rc= mysql_stmt_bind_param(stmt, bind);
4784   check_stmt_rc(rc, stmt);
4785   rc= mysql_stmt_send_long_data(stmt, 0, data, 6);
4786   check_stmt_rc(rc, stmt);
4787   rc= mysql_stmt_execute(stmt);
4788   check_stmt_rc(rc, stmt);
4789   rc= mysql_stmt_close(stmt);
4790   check_stmt_rc(rc, stmt);
4791   rc= mysql_query(mysql, "DROP TABLE IF EXISTS tld");
4792   check_mysql_rc(rc, mysql);
4793   return OK;
4794 }
4795 
test_blob_9000(MYSQL * mysql)4796 int test_blob_9000(MYSQL *mysql)
4797 {
4798   MYSQL_BIND bind[1];
4799   MYSQL_STMT *stmt;
4800   int rc;
4801   char buffer[9200];
4802   const char *query= "INSERT INTO tb9000 VALUES (?)";
4803 
4804   rc= mysql_query(mysql, "DROP TABLE IF EXISTS tb9000");
4805   check_mysql_rc(rc, mysql);
4806   rc= mysql_query(mysql, "CREATE TABLE tb9000 (a blob)");
4807   check_mysql_rc(rc, mysql);
4808 
4809   stmt= mysql_stmt_init(mysql);
4810   rc= mysql_stmt_prepare(stmt, SL(query));
4811 
4812   memset(bind, 0, sizeof(MYSQL_BIND));
4813   memset(buffer, 'C', 9200);
4814   bind[0].buffer= buffer;
4815   bind[0].buffer_length= 9200;
4816   bind[0].buffer_type= MYSQL_TYPE_STRING;
4817   rc= mysql_stmt_bind_param(stmt, bind);
4818   check_stmt_rc(rc, stmt);
4819   rc= mysql_stmt_execute(stmt);
4820   check_stmt_rc(rc, stmt);
4821 
4822   mysql_stmt_close(stmt);
4823   rc= mysql_query(mysql, "DROP TABLE IF EXISTS tb9000");
4824   check_mysql_rc(rc, mysql);
4825   return OK;
4826 }
4827 
test_fracseconds(MYSQL * mysql)4828 int test_fracseconds(MYSQL *mysql)
4829 {
4830   MYSQL_STMT *stmt;
4831   int rc;
4832   const char *str= "SELECT NOW(6)";
4833   char buffer[60], buffer1[60];
4834   MYSQL_BIND bind[2];
4835 
4836   stmt= mysql_stmt_init(mysql);
4837   rc= mysql_stmt_prepare(stmt, SL(str));
4838   check_stmt_rc(rc, stmt);
4839 
4840   rc= mysql_stmt_execute(stmt);
4841   check_stmt_rc(rc, stmt);
4842 
4843   memset(&bind, 0, sizeof(MYSQL_BIND));
4844   bind[0].buffer= buffer;
4845   bind[0].buffer_length=60;
4846   bind[0].buffer_type= MYSQL_TYPE_STRING;
4847 
4848   rc= mysql_stmt_bind_result(stmt, bind);
4849   check_stmt_rc(rc, stmt);
4850 
4851   rc= mysql_stmt_fetch(stmt);
4852   check_stmt_rc(rc, stmt);
4853 
4854   FAIL_IF(strlen(buffer) != 26, "Expected timestamp with length of 26");
4855 
4856   rc= mysql_stmt_close(stmt);
4857   check_stmt_rc(rc, stmt);
4858 
4859   rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
4860   check_mysql_rc(rc, mysql);
4861 
4862   rc= mysql_query(mysql, "CREATE TABLE t1 (a timestamp(6), b time(6))");
4863   check_mysql_rc(rc, mysql);
4864 
4865   rc= mysql_query(mysql, "INSERT INTO t1 VALUES ('2012-04-25 10:20:49.0194','10:20:49.0194' )");
4866   check_mysql_rc(rc, mysql);
4867 
4868   stmt= mysql_stmt_init(mysql);
4869   rc= mysql_stmt_prepare(stmt, "SELECT a,b FROM t1", 18);
4870   check_stmt_rc(rc, stmt);
4871 
4872   rc= mysql_stmt_execute(stmt);
4873   check_stmt_rc(rc, stmt);
4874 
4875   memset(bind, 0, 2 * sizeof(MYSQL_BIND));
4876   bind[0].buffer= buffer;
4877   bind[1].buffer= buffer1;
4878   bind[0].buffer_length=  bind[1].buffer_length= 60;
4879   bind[0].buffer_type= bind[1].buffer_type= MYSQL_TYPE_STRING;
4880 
4881   rc= mysql_stmt_bind_result(stmt, bind);
4882   check_stmt_rc(rc, stmt);
4883 
4884   rc= mysql_stmt_fetch(stmt);
4885   check_stmt_rc(rc, stmt);
4886   FAIL_IF(strcmp(buffer, "2012-04-25 10:20:49.019400") != 0, "Wrong result");
4887   FAIL_IF(strcmp(buffer1, "10:20:49.019400") != 0, "Wrong result");
4888 
4889   rc= mysql_stmt_close(stmt);
4890   check_stmt_rc(rc, stmt);
4891 
4892   rc= mysql_query(mysql, "DROP TABLE t1");
4893 
4894   return OK;
4895 }
4896 
test_notrunc(MYSQL * mysql)4897 int test_notrunc(MYSQL *mysql)
4898 {
4899   MYSQL_STMT *stmt;
4900   my_bool trunc= 1;
4901   MYSQL_BIND bind[2];
4902   char buffer[5], buffer2[5];
4903   int rc;
4904   my_bool error= 0;
4905   unsigned long len= 1;
4906 
4907   const char *query= "SELECT '1234567890', 'foo' FROM DUAL";
4908 
4909   mysql_options(mysql, MYSQL_REPORT_DATA_TRUNCATION, &trunc);
4910 
4911   stmt= mysql_stmt_init(mysql);
4912 
4913   rc= mysql_stmt_prepare(stmt, SL(query));
4914   check_stmt_rc(rc, stmt);
4915 
4916   rc= mysql_stmt_execute(stmt);
4917   check_stmt_rc(rc, stmt);
4918 
4919   strcpy(buffer, "bar");
4920 
4921   memset(bind, 0, sizeof(MYSQL_BIND) * 2);
4922   bind[0].buffer_type= MYSQL_TYPE_NULL;
4923   bind[0].buffer= buffer;
4924   bind[0].buffer_length= 1;
4925   bind[0].length= &len;
4926   bind[0].flags|= MADB_BIND_DUMMY;
4927   bind[0].error= &error;
4928   bind[1].buffer_type= MYSQL_TYPE_STRING;
4929   bind[1].buffer= buffer2;
4930   bind[1].buffer_length= 5;
4931 
4932   rc= mysql_stmt_bind_result(stmt, bind);
4933   check_stmt_rc(rc, stmt);
4934   mysql_stmt_store_result(stmt);
4935 
4936   rc= mysql_stmt_fetch(stmt);
4937   mysql_stmt_close(stmt);
4938 
4939   FAIL_IF(rc!= 0, "expected rc= 0");
4940   FAIL_IF(strcmp(buffer, "bar"), "Bind dummy failed");
4941   FAIL_IF(strcmp(buffer2, "foo"), "Invalid second buffer");
4942 
4943   return OK;
4944 }
4945 
test_bit2tiny(MYSQL * mysql)4946 static int test_bit2tiny(MYSQL *mysql)
4947 {
4948   MYSQL_BIND bind[2];
4949   char       data[11];
4950   unsigned   long length[2];
4951   my_bool    is_null[2], error[2];
4952   const char *query = "SELECT val FROM justbit";
4953   MYSQL_STMT *stmt;
4954   int rc;
4955 
4956   mysql_query(mysql, "DROP TABLE IF EXISTS justbit");
4957   mysql_query(mysql, "CREATE TABLE justbit(val bit(1) not null)");
4958   mysql_query(mysql, "INSERT INTO justbit values (1)");
4959 
4960   stmt= mysql_stmt_init(mysql);
4961   rc= mysql_stmt_prepare(stmt, SL(query));
4962   check_stmt_rc(rc, stmt);
4963 
4964   memset(bind, '\0', sizeof(bind));
4965 
4966   bind[0].buffer_type=  MYSQL_TYPE_TINY;
4967   bind[0].buffer=       &data[0];
4968   bind[0].buffer_length= 1;
4969   bind[0].is_null=      &is_null[0];
4970   bind[0].length=       &length[0];
4971   bind[0].error=        &error[0];
4972 
4973   rc= mysql_stmt_execute(stmt);
4974   check_stmt_rc(rc, stmt);
4975 
4976   rc= mysql_stmt_bind_result(stmt, bind);
4977   check_stmt_rc(rc, stmt);
4978 
4979   rc= mysql_stmt_store_result(stmt);
4980   check_stmt_rc(rc, stmt);
4981 
4982   mysql_stmt_fetch(stmt);
4983 
4984   FAIL_IF(data[0] != 1, "Value should be 1");
4985 
4986   mysql_stmt_free_result(stmt);
4987   mysql_stmt_close(stmt);
4988   rc= mysql_query(mysql, "DROP TABLE IF EXISTS justbit");
4989   check_mysql_rc(rc, mysql);
4990   return OK;
4991 }
4992 
test_reexecute(MYSQL * mysql)4993 static int test_reexecute(MYSQL *mysql)
4994 {
4995     MYSQL_STMT *stmt;
4996   MYSQL_BIND ps_params[3];  /* input parameter buffers */
4997   int        int_data[3];   /* input/output values */
4998   int        rc;
4999 
5000   if (!mariadb_connection(mysql))
5001     return SKIP;
5002 
5003   /* set up stored procedure */
5004   rc = mysql_query(mysql, "DROP PROCEDURE IF EXISTS p1");
5005   check_mysql_rc(rc, mysql);
5006 
5007   rc = mysql_query(mysql,
5008       "CREATE PROCEDURE p1("
5009       "  IN p_in INT, "
5010       "  OUT p_out INT, "
5011       "  INOUT p_inout INT) "
5012       "BEGIN "
5013       "  SELECT p_in, p_out, p_inout; "
5014       "  SET p_in = 100, p_out = 200, p_inout = 300; "
5015       "  SELECT p_in, p_out, p_inout; "
5016       "END");
5017   check_mysql_rc(rc, mysql);
5018 
5019   /* initialize and prepare CALL statement with parameter placeholders */
5020   stmt = mysql_stmt_init(mysql);
5021   if (!stmt)
5022   {
5023     diag("Could not initialize statement");
5024     exit(1);
5025   }
5026   rc = mysql_stmt_prepare(stmt, "CALL p1(?, ?, ?)", 16);
5027   check_stmt_rc(rc, stmt);
5028 
5029   /* initialize parameters: p_in, p_out, p_inout (all INT) */
5030   memset(ps_params, 0, sizeof (ps_params));
5031 
5032   ps_params[0].buffer_type = MYSQL_TYPE_LONG;
5033   ps_params[0].buffer = (char *) &int_data[0];
5034   ps_params[0].length = 0;
5035   ps_params[0].is_null = 0;
5036 
5037   ps_params[1].buffer_type = MYSQL_TYPE_LONG;
5038   ps_params[1].buffer = (char *) &int_data[1];
5039   ps_params[1].length = 0;
5040   ps_params[1].is_null = 0;
5041 
5042   ps_params[2].buffer_type = MYSQL_TYPE_LONG;
5043   ps_params[2].buffer = (char *) &int_data[2];
5044   ps_params[2].length = 0;
5045   ps_params[2].is_null = 0;
5046 
5047   /* bind parameters */
5048   rc = mysql_stmt_bind_param(stmt, ps_params);
5049   check_stmt_rc(rc, stmt);
5050 
5051   /* assign values to parameters and execute statement */
5052   int_data[0]= 10;  /* p_in */
5053   int_data[1]= 20;  /* p_out */
5054   int_data[2]= 30;  /* p_inout */
5055 
5056   rc = mysql_stmt_execute(stmt);
5057   check_stmt_rc(rc, stmt);
5058 
5059   rc= mysql_stmt_execute(stmt);
5060   check_stmt_rc(rc, stmt);
5061 
5062   mysql_stmt_close(stmt);
5063 
5064   rc = mysql_query(mysql, "DROP PROCEDURE IF EXISTS p1");
5065   check_mysql_rc(rc, mysql);
5066   return OK;
5067 }
5068 
test_prepare_error(MYSQL * mysql)5069 static int test_prepare_error(MYSQL *mysql)
5070 {
5071   MYSQL_STMT *stmt= mysql_stmt_init(mysql);
5072   int rc;
5073 
5074   rc= mysql_stmt_prepare(stmt, SL("SELECT 1 FROM tbl_not_exists"));
5075   FAIL_IF(!rc, "Expected error");
5076 
5077   rc= mysql_stmt_reset(stmt);
5078   check_stmt_rc(rc, stmt);
5079 
5080   rc= mysql_stmt_prepare(stmt, SL("SELECT 1 FROM tbl_not_exists"));
5081   FAIL_IF(!rc, "Expected error");
5082 
5083   rc= mysql_stmt_reset(stmt);
5084   check_stmt_rc(rc, stmt);
5085 
5086   rc= mysql_stmt_prepare(stmt, SL("SET @a:=1"));
5087   check_stmt_rc(rc, stmt);
5088 
5089   mysql_stmt_close(stmt);
5090   return OK;
5091 }
5092 
test_conc349(MYSQL * mysql)5093 static int test_conc349(MYSQL *mysql)
5094 {
5095   MYSQL_STMT *stmt= mysql_stmt_init(mysql);
5096   int rc;
5097   enum mysql_stmt_state state;
5098 
5099   rc= mysql_stmt_attr_get(stmt, STMT_ATTR_STATE, &state);
5100   FAIL_IF(state != MYSQL_STMT_INITTED, "expected status MYSQL_STMT_INITTED");
5101 
5102   rc= mysql_stmt_prepare(stmt, SL("SET @a:=1"));
5103   check_stmt_rc(rc, stmt);
5104 
5105   rc= mysql_stmt_attr_get(stmt, STMT_ATTR_STATE, &state);
5106   FAIL_IF(state != MYSQL_STMT_PREPARED, "expected status MYSQL_STMT_PREPARED");
5107 
5108   rc= mysql_stmt_execute(stmt);
5109   check_stmt_rc(rc, stmt);
5110 
5111   rc= mysql_stmt_attr_get(stmt, STMT_ATTR_STATE, &state);
5112   FAIL_IF(state != MYSQL_STMT_EXECUTED, "expected status MYSQL_STMT_EXECUTED");
5113 
5114   mysql_stmt_close(stmt);
5115   return OK;
5116 }
5117 
test_conc565(MYSQL * mysql)5118 static int test_conc565(MYSQL *mysql)
5119 {
5120   MYSQL_STMT *stmt= mysql_stmt_init(mysql);
5121   MYSQL_FIELD *fields_binary, *fields_text;
5122   MYSQL_RES *result;
5123   int rc;
5124   unsigned int i;
5125   my_bool x=1;
5126   my_bool error= 0;
5127 
5128   rc= mysql_query(mysql, "CREATE TEMPORARY TABLE t1 (a year, b tinyint unsigned, c smallint unsigned, d mediumint unsigned, e int unsigned, f bigint unsigned)");
5129   check_mysql_rc(rc, mysql);
5130 
5131   rc= mysql_query(mysql, "INSERT INTO t1 VALUES (2020, 127, 0xFFFF, 0xFFFFFF, 0xFFFFFFFF, 0xFFFFFFFFFFFFFFFF)");
5132   check_mysql_rc(rc, mysql);
5133 
5134   rc= mysql_stmt_prepare(stmt, "select a,b,c,d,e,f from t1", -1);
5135   check_stmt_rc(rc, stmt);
5136 
5137   rc= mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void *)&x);
5138   check_stmt_rc(rc, stmt);
5139 
5140   rc= mysql_stmt_execute(stmt);
5141   check_stmt_rc(rc, stmt);
5142 
5143   mysql_stmt_store_result(stmt);
5144   fields_binary= mariadb_stmt_fetch_fields(stmt);
5145 
5146   rc= mysql_query(mysql, "SELECT a,b,c,d,e,f FROM t1");
5147   result= mysql_store_result(mysql);
5148   fields_text= mysql_fetch_fields(result);
5149 
5150   for (i=0; i < mysql_field_count(mysql); i++)
5151   {
5152      if (fields_binary[i].length != fields_text[i].length ||
5153          fields_binary[i].max_length != fields_text[i].max_length)
5154      {
5155        diag("Sizes differ for column %d (type= %d)", i, fields_binary[i].type);
5156        diag("Binary (length=%ld max_length=%ld) != Text(length=%ld max_length=%ld",
5157              fields_binary[i].length, fields_binary[i].max_length,
5158              fields_text[i].length, fields_text[i].max_length);
5159        error= 1;
5160        goto end;
5161      }
5162   }
5163 end:
5164   mysql_free_result(result);
5165   mysql_stmt_close(stmt);
5166 
5167   return error ? FAIL : OK;
5168 }
5169 
5170 struct my_tests_st my_tests[] = {
5171   {"test_conc565", test_conc565, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5172   {"test_conc349", test_conc349, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5173   {"test_prepare_error", test_prepare_error, TEST_CONNECTION_NEW, 0, NULL, NULL},
5174   {"test_reexecute", test_reexecute, TEST_CONNECTION_NEW, 0, NULL, NULL},
5175   {"test_bit2tiny", test_bit2tiny, TEST_CONNECTION_NEW, 0, NULL, NULL},
5176   {"test_conc97", test_conc97, TEST_CONNECTION_NEW, 0, NULL, NULL},
5177   {"test_conc83", test_conc83, TEST_CONNECTION_NONE, 0, NULL, NULL},
5178   {"test_conc60", test_conc60, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5179   {"test_notrunc", test_notrunc, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5180   {"test_fracseconds", test_fracseconds, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5181   {"test_blob_9000", test_blob_9000, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5182   {"test_long_data1", test_long_data1, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5183   {"test_prepare_insert_update", test_prepare_insert_update, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5184   {"test_prepare_simple", test_prepare_simple, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5185   {"test_prepare_syntax", test_prepare_syntax, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5186   {"test_prepare_field_result", test_prepare_field_result, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5187   {"test_prepare", test_prepare, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5188   {"test_prepare_ext", test_prepare_ext, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5189   {"test_prepare_multi_statements", test_prepare_multi_statements, TEST_CONNECTION_NEW, 0, NULL , NULL},
5190   {"test_prepare_alter", test_prepare_alter, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5191   {"test_prepare_resultset", test_prepare_resultset, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5192   {"test_open_direct", test_open_direct, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5193   {"test_select_show", test_select_show, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5194   {"test_select", test_select, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5195   {"test_long_data", test_long_data, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5196   {"test_long_data_str", test_long_data_str, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5197   {"test_long_data_str1", test_long_data_str1, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5198   {"test_long_data_bin", test_long_data_bin, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5199   {"test_simple_update", test_simple_update, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5200   {"test_simple_delete", test_simple_delete, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5201   {"test_update", test_update, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5202   {"test_prepare_noparam", test_prepare_noparam, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5203   {"test_bind_result", test_bind_result, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5204   {"test_bind_result_ext", test_bind_result_ext, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5205   {"test_bind_result_ext1", test_bind_result_ext1, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5206   {"test_bind_negative", test_bind_negative, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5207   {"test_buffers", test_buffers, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5208   {"test_xjoin", test_xjoin, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5209   {"test_union", test_union, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5210   {"test_union2", test_union2, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5211   {"test_union_param", test_union_param, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5212   {"test_pure_coverage", test_pure_coverage, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5213   {"test_insert_select", test_insert_select, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5214   {"test_insert", test_insert, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5215   {"test_join", test_join, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5216   {"test_left_join_view", test_left_join_view, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5217   {"test_manual_sample", test_manual_sample, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5218   {"test_create_drop", test_create_drop, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5219   {"test_date", test_date, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5220   {"test_date_ts", test_date_ts, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5221   {"test_date_dt", test_date_dt, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5222   {"test_date_date", test_date_date, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5223   {"test_date_time", test_date_time, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5224   {"test_datetime_ranges", test_datetime_ranges, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5225   {"test_derived", test_derived, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5226   {"test_distinct", test_distinct, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5227   {"test_do_set", test_do_set, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5228   {"test_double_compare", test_double_compare, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5229   {"test_multi", test_multi, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5230   {"test_multi_stmt", test_multi_stmt, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5231   {"test_nstmts", test_nstmts, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5232   {"test_null", test_null, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5233   {"test_order_param", test_order_param, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5234   {"test_rename", test_rename, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5235   {"test_rewind", test_rewind, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5236   {"test_select_prepare", test_select_prepare, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5237   {"test_select_show_table", test_select_show_table, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5238   {"test_select_version", test_select_version, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5239   {"test_selecttmp", test_selecttmp, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5240   {"test_set_option", test_set_option, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5241   {"test_set_variable", test_set_variable, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5242   {"test_sqlmode", test_sqlmode, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5243   {"test_stmt_close", test_stmt_close, TEST_CONNECTION_DEFAULT, 0, NULL , NULL},
5244   {"test_new_date", test_new_date, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
5245   {NULL, NULL, 0, 0, NULL, NULL}
5246 };
5247 
main(int argc,char ** argv)5248 int main(int argc, char **argv)
5249 {
5250   if (argc > 1)
5251     get_options(argc, argv);
5252 
5253   get_envvars();
5254 
5255   run_tests(my_tests);
5256 
5257   return(exit_status());
5258 }
5259