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