~linuxjedi/drizzle/trunk-bug-667053

« back to all changes in this revision

Viewing changes to tests/mysql_client_test.c

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003-2004 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
/***************************************************************************
 
17
 This is a test sample to test the new features in MySQL client-server
 
18
 protocol
 
19
 
 
20
 Main author: venu ( venu@mysql.com )
 
21
***************************************************************************/
 
22
 
 
23
/*
 
24
  XXX: PLEASE RUN THIS PROGRAM UNDER VALGRIND AND VERIFY THAT YOUR TEST
 
25
  DOESN'T CONTAIN WARNINGS/ERRORS BEFORE YOU PUSH.
 
26
*/
 
27
 
 
28
 
 
29
#include <my_global.h>
 
30
#include <my_sys.h>
 
31
#include <mysql.h>
 
32
#include <errmsg.h>
 
33
#include <my_getopt.h>
 
34
#include <m_string.h>
 
35
#include <mysqld_error.h>
 
36
 
 
37
#define VER "2.1"
 
38
#define MAX_TEST_QUERY_LENGTH 300 /* MAX QUERY BUFFER LENGTH */
 
39
#define MAX_KEY MAX_INDEXES
 
40
#define MAX_SERVER_ARGS 64
 
41
 
 
42
/* set default options */
 
43
static int   opt_testcase = 0;
 
44
static char *opt_db= 0;
 
45
static char *opt_user= 0;
 
46
static char *opt_password= 0;
 
47
static char *opt_host= 0;
 
48
static char *opt_unix_socket= 0;
 
49
static unsigned int  opt_port;
 
50
static my_bool tty_password= 0, opt_silent= 0;
 
51
 
 
52
static MYSQL *mysql= 0;
 
53
static char current_db[]= "client_test_db";
 
54
static unsigned int test_count= 0;
 
55
static unsigned int opt_count= 0;
 
56
static unsigned int iter_count= 0;
 
57
static my_bool have_innodb= FALSE;
 
58
 
 
59
static const char *opt_basedir= "./";
 
60
static const char *opt_vardir= "mysql-test/var";
 
61
 
 
62
static longlong opt_getopt_ll_test= 0;
 
63
 
 
64
static int embedded_server_arg_count= 0;
 
65
static char *embedded_server_args[MAX_SERVER_ARGS];
 
66
 
 
67
static const char *embedded_server_groups[]= {
 
68
  "server",
 
69
  "embedded",
 
70
  "mysql_client_test_SERVER",
 
71
  NullS
 
72
};
 
73
 
 
74
static time_t start_time, end_time;
 
75
static double total_time;
 
76
 
 
77
const char *default_dbug_option= "d:t:o,/tmp/mysql_client_test.trace";
 
78
 
 
79
struct my_tests_st
 
80
{
 
81
  const char *name;
 
82
  void       (*function)();
 
83
};
 
84
 
 
85
#define myheader(str) \
 
86
DBUG_PRINT("test", ("name: %s", str));        \
 
87
if (opt_silent < 2) \
 
88
{ \
 
89
  fprintf(stdout, "\n\n#####################################\n"); \
 
90
  fprintf(stdout, "%u of (%u/%u): %s", test_count++, iter_count, \
 
91
                                     opt_count, str); \
 
92
  fprintf(stdout, "  \n#####################################\n"); \
 
93
}
 
94
 
 
95
#define myheader_r(str) \
 
96
DBUG_PRINT("test", ("name: %s", str));        \
 
97
if (!opt_silent) \
 
98
{ \
 
99
  fprintf(stdout, "\n\n#####################################\n"); \
 
100
  fprintf(stdout, "%s", str); \
 
101
  fprintf(stdout, "  \n#####################################\n"); \
 
102
}
 
103
 
 
104
static void print_error(const char *msg);
 
105
static void print_st_error(MYSQL_STMT *stmt, const char *msg);
 
106
static void client_disconnect(void);
 
107
 
 
108
 
 
109
/*
 
110
  Abort unless given experssion is non-zero.
 
111
 
 
112
  SYNOPSIS
 
113
    DIE_UNLESS(expr)
 
114
 
 
115
  DESCRIPTION
 
116
    We can't use any kind of system assert as we need to
 
117
    preserve tested invariants in release builds as well.
 
118
*/
 
119
 
 
120
#define DIE_UNLESS(expr) \
 
121
        ((void) ((expr) ? 0 : (die(__FILE__, __LINE__, #expr), 0)))
 
122
#define DIE_IF(expr) \
 
123
        ((void) ((expr) ? (die(__FILE__, __LINE__, #expr), 0) : 0))
 
124
#define DIE(expr) \
 
125
        die(__FILE__, __LINE__, #expr)
 
126
 
 
127
static void die(const char *file, int line, const char *expr)
 
128
{
 
129
  fflush(stdout);
 
130
  fprintf(stderr, "%s:%d: check failed: '%s'\n", file, line, expr);
 
131
  fflush(stderr);
 
132
  exit(1);
 
133
}
 
134
 
 
135
 
 
136
#define myerror(msg) print_error(msg)
 
137
#define mysterror(stmt, msg) print_st_error(stmt, msg)
 
138
 
 
139
#define myquery(RES) \
 
140
{ \
 
141
  int r= (RES);                                \
 
142
  if (r) \
 
143
    myerror(NULL); \
 
144
  DIE_UNLESS(r == 0); \
 
145
}
 
146
 
 
147
#define myquery_r(r) \
 
148
{ \
 
149
if (r) \
 
150
  myerror(NULL); \
 
151
DIE_UNLESS(r != 0); \
 
152
}
 
153
 
 
154
#define check_execute(stmt, r) \
 
155
{ \
 
156
if (r) \
 
157
  mysterror(stmt, NULL); \
 
158
DIE_UNLESS(r == 0);\
 
159
}
 
160
 
 
161
#define check_execute_r(stmt, r) \
 
162
{ \
 
163
if (r) \
 
164
  mysterror(stmt, NULL); \
 
165
DIE_UNLESS(r != 0);\
 
166
}
 
167
 
 
168
#define check_stmt(stmt) \
 
169
{ \
 
170
if ( stmt == 0) \
 
171
  myerror(NULL); \
 
172
DIE_UNLESS(stmt != 0); \
 
173
}
 
174
 
 
175
#define check_stmt_r(stmt) \
 
176
{ \
 
177
if (stmt == 0) \
 
178
  myerror(NULL);\
 
179
DIE_UNLESS(stmt == 0);\
 
180
}
 
181
 
 
182
#define mytest(x) if (!(x)) {myerror(NULL);DIE_UNLESS(FALSE);}
 
183
#define mytest_r(x) if ((x)) {myerror(NULL);DIE_UNLESS(FALSE);}
 
184
 
 
185
 
 
186
/* A workaround for Sun Forte 5.6 on Solaris x86 */
 
187
 
 
188
static int cmp_double(double *a, double *b)
 
189
{
 
190
  return *a == *b;
 
191
}
 
192
 
 
193
 
 
194
/* Print the error message */
 
195
 
 
196
static void print_error(const char *msg)
 
197
{
 
198
  if (!opt_silent)
 
199
  {
 
200
    if (mysql && mysql_errno(mysql))
 
201
    {
 
202
      if (mysql->server_version)
 
203
        fprintf(stdout, "\n [MySQL-%s]", mysql->server_version);
 
204
      else
 
205
        fprintf(stdout, "\n [MySQL]");
 
206
      fprintf(stdout, "[%d] %s\n", mysql_errno(mysql), mysql_error(mysql));
 
207
    }
 
208
    else if (msg)
 
209
      fprintf(stderr, " [MySQL] %s\n", msg);
 
210
  }
 
211
}
 
212
 
 
213
 
 
214
static void print_st_error(MYSQL_STMT *stmt, const char *msg)
 
215
{
 
216
  if (!opt_silent)
 
217
  {
 
218
    if (stmt && mysql_stmt_errno(stmt))
 
219
    {
 
220
      if (stmt->mysql && stmt->mysql->server_version)
 
221
        fprintf(stdout, "\n [MySQL-%s]", stmt->mysql->server_version);
 
222
      else
 
223
        fprintf(stdout, "\n [MySQL]");
 
224
 
 
225
      fprintf(stdout, "[%d] %s\n", mysql_stmt_errno(stmt),
 
226
              mysql_stmt_error(stmt));
 
227
    }
 
228
    else if (msg)
 
229
      fprintf(stderr, " [MySQL] %s\n", msg);
 
230
  }
 
231
}
 
232
 
 
233
/* Check if the connection has InnoDB tables */
 
234
 
 
235
static my_bool check_have_innodb(MYSQL *conn)
 
236
{
 
237
  MYSQL_RES *res;
 
238
  MYSQL_ROW row;
 
239
  int rc;
 
240
  my_bool result;
 
241
 
 
242
  rc= mysql_query(conn, "show variables like 'have_innodb'");
 
243
  myquery(rc);
 
244
  res= mysql_use_result(conn);
 
245
  DIE_UNLESS(res);
 
246
 
 
247
  row= mysql_fetch_row(res);
 
248
  DIE_UNLESS(row);
 
249
 
 
250
  result= strcmp(row[1], "YES") == 0;
 
251
  mysql_free_result(res);
 
252
  return result;
 
253
}
 
254
 
 
255
 
 
256
/*
 
257
  This is to be what mysql_query() is for mysql_real_query(), for
 
258
  mysql_simple_prepare(): a variant without the 'length' parameter.
 
259
*/
 
260
 
 
261
static MYSQL_STMT *STDCALL
 
262
mysql_simple_prepare(MYSQL *mysql_arg, const char *query)
 
263
{
 
264
  MYSQL_STMT *stmt= mysql_stmt_init(mysql_arg);
 
265
  if (stmt && mysql_stmt_prepare(stmt, query, strlen(query)))
 
266
  {
 
267
    mysql_stmt_close(stmt);
 
268
    return 0;
 
269
  }
 
270
  return stmt;
 
271
}
 
272
 
 
273
 
 
274
/* Connect to the server */
 
275
 
 
276
static void client_connect(ulong flag)
 
277
{
 
278
  int  rc;
 
279
  static char query[MAX_TEST_QUERY_LENGTH];
 
280
  myheader_r("client_connect");
 
281
 
 
282
  if (!opt_silent)
 
283
    fprintf(stdout, "\n Establishing a connection to '%s' ...",
 
284
            opt_host ? opt_host : "");
 
285
 
 
286
  if (!(mysql= mysql_init(NULL)))
 
287
  {
 
288
    opt_silent= 0;
 
289
    myerror("mysql_init() failed");
 
290
    exit(1);
 
291
  }
 
292
  /* enable local infile, in non-binary builds often disabled by default */
 
293
  mysql_options(mysql, MYSQL_OPT_LOCAL_INFILE, 0);
 
294
 
 
295
  if (!(mysql_real_connect(mysql, opt_host, opt_user,
 
296
                           opt_password, opt_db ? opt_db:"test", opt_port,
 
297
                           opt_unix_socket, flag)))
 
298
  {
 
299
    opt_silent= 0;
 
300
    myerror("connection failed");
 
301
    mysql_close(mysql);
 
302
    fprintf(stdout, "\n Check the connection options using --help or -?\n");
 
303
    exit(1);
 
304
  }
 
305
  mysql->reconnect= 1;
 
306
 
 
307
  if (!opt_silent)
 
308
    fprintf(stdout, "OK");
 
309
 
 
310
  /* set AUTOCOMMIT to ON*/
 
311
  mysql_autocommit(mysql, TRUE);
 
312
 
 
313
  if (!opt_silent)
 
314
  {
 
315
    fprintf(stdout, "\nConnected to MySQL server version: %s (%lu)\n",
 
316
            mysql_get_server_info(mysql),
 
317
            (ulong) mysql_get_server_version(mysql));
 
318
    fprintf(stdout, "\n Creating a test database '%s' ...", current_db);
 
319
  }
 
320
  strxmov(query, "CREATE DATABASE IF NOT EXISTS ", current_db, NullS);
 
321
 
 
322
  rc= mysql_query(mysql, query);
 
323
  myquery(rc);
 
324
 
 
325
  strxmov(query, "USE ", current_db, NullS);
 
326
  rc= mysql_query(mysql, query);
 
327
  myquery(rc);
 
328
  have_innodb= check_have_innodb(mysql);
 
329
 
 
330
  if (!opt_silent)
 
331
    fprintf(stdout, "OK");
 
332
}
 
333
 
 
334
 
 
335
/* Close the connection */
 
336
 
 
337
static void client_disconnect()
 
338
{
 
339
  static char query[MAX_TEST_QUERY_LENGTH];
 
340
 
 
341
  myheader_r("client_disconnect");
 
342
 
 
343
  if (mysql)
 
344
  {
 
345
    if (!opt_silent)
 
346
      fprintf(stdout, "\n dropping the test database '%s' ...", current_db);
 
347
    strxmov(query, "DROP DATABASE IF EXISTS ", current_db, NullS);
 
348
 
 
349
    mysql_query(mysql, query);
 
350
    if (!opt_silent)
 
351
      fprintf(stdout, "OK");
 
352
 
 
353
    if (!opt_silent)
 
354
      fprintf(stdout, "\n closing the connection ...");
 
355
    mysql_close(mysql);
 
356
    if (!opt_silent)
 
357
      fprintf(stdout, "OK\n");
 
358
  }
 
359
}
 
360
 
 
361
 
 
362
/* Query processing */
 
363
 
 
364
static void client_query()
 
365
{
 
366
  int rc;
 
367
 
 
368
  myheader("client_query");
 
369
 
 
370
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
371
  myquery(rc);
 
372
 
 
373
  rc= mysql_query(mysql, "CREATE TABLE t1("
 
374
                         "id int primary key auto_increment, "
 
375
                         "name varchar(20))");
 
376
  myquery(rc);
 
377
 
 
378
  rc= mysql_query(mysql, "CREATE TABLE t1(id int, name varchar(20))");
 
379
  myquery_r(rc);
 
380
 
 
381
  rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('mysql')");
 
382
  myquery(rc);
 
383
 
 
384
  rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('monty')");
 
385
  myquery(rc);
 
386
 
 
387
  rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('venu')");
 
388
  myquery(rc);
 
389
 
 
390
  rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('deleted')");
 
391
  myquery(rc);
 
392
 
 
393
  rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('deleted')");
 
394
  myquery(rc);
 
395
 
 
396
  rc= mysql_query(mysql, "UPDATE t1 SET name= 'updated' "
 
397
                          "WHERE name= 'deleted'");
 
398
  myquery(rc);
 
399
 
 
400
  rc= mysql_query(mysql, "UPDATE t1 SET id= 3 WHERE name= 'updated'");
 
401
  myquery_r(rc);
 
402
 
 
403
  myquery(mysql_query(mysql, "drop table t1"));
 
404
}
 
405
 
 
406
 
 
407
/* Print dashes */
 
408
 
 
409
static void my_print_dashes(MYSQL_RES *result)
 
410
{
 
411
  MYSQL_FIELD  *field;
 
412
  unsigned int i, j;
 
413
 
 
414
  mysql_field_seek(result, 0);
 
415
  fputc('\t', stdout);
 
416
  fputc('+', stdout);
 
417
 
 
418
  for(i= 0; i< mysql_num_fields(result); i++)
 
419
  {
 
420
    field= mysql_fetch_field(result);
 
421
    for(j= 0; j < field->max_length+2; j++)
 
422
      fputc('-', stdout);
 
423
    fputc('+', stdout);
 
424
  }
 
425
  fputc('\n', stdout);
 
426
}
 
427
 
 
428
 
 
429
/* Print resultset metadata information */
 
430
 
 
431
static void my_print_result_metadata(MYSQL_RES *result)
 
432
{
 
433
  MYSQL_FIELD  *field;
 
434
  unsigned int i, j;
 
435
  unsigned int field_count;
 
436
 
 
437
  mysql_field_seek(result, 0);
 
438
  if (!opt_silent)
 
439
  {
 
440
    fputc('\n', stdout);
 
441
    fputc('\n', stdout);
 
442
  }
 
443
 
 
444
  field_count= mysql_num_fields(result);
 
445
  for(i= 0; i< field_count; i++)
 
446
  {
 
447
    field= mysql_fetch_field(result);
 
448
    j= strlen(field->name);
 
449
    if (j < field->max_length)
 
450
      j= field->max_length;
 
451
    if (j < 4 && !IS_NOT_NULL(field->flags))
 
452
      j= 4;
 
453
    field->max_length= j;
 
454
  }
 
455
  if (!opt_silent)
 
456
  {
 
457
    my_print_dashes(result);
 
458
    fputc('\t', stdout);
 
459
    fputc('|', stdout);
 
460
  }
 
461
 
 
462
  mysql_field_seek(result, 0);
 
463
  for(i= 0; i< field_count; i++)
 
464
  {
 
465
    field= mysql_fetch_field(result);
 
466
    if (!opt_silent)
 
467
      fprintf(stdout, " %-*s |", (int) field->max_length, field->name);
 
468
  }
 
469
  if (!opt_silent)
 
470
  {
 
471
    fputc('\n', stdout);
 
472
    my_print_dashes(result);
 
473
  }
 
474
}
 
475
 
 
476
 
 
477
/* Process the result set */
 
478
 
 
479
static int my_process_result_set(MYSQL_RES *result)
 
480
{
 
481
  MYSQL_ROW    row;
 
482
  MYSQL_FIELD  *field;
 
483
  unsigned int i;
 
484
  unsigned int row_count= 0;
 
485
 
 
486
  if (!result)
 
487
    return 0;
 
488
 
 
489
  my_print_result_metadata(result);
 
490
 
 
491
  while ((row= mysql_fetch_row(result)) != NULL)
 
492
  {
 
493
    mysql_field_seek(result, 0);
 
494
    if (!opt_silent)
 
495
    {
 
496
      fputc('\t', stdout);
 
497
      fputc('|', stdout);
 
498
    }
 
499
 
 
500
    for(i= 0; i< mysql_num_fields(result); i++)
 
501
    {
 
502
      field= mysql_fetch_field(result);
 
503
      if (!opt_silent)
 
504
      {
 
505
        if (row[i] == NULL)
 
506
          fprintf(stdout, " %-*s |", (int) field->max_length, "NULL");
 
507
        else if (IS_NUM(field->type))
 
508
          fprintf(stdout, " %*s |", (int) field->max_length, row[i]);
 
509
        else
 
510
          fprintf(stdout, " %-*s |", (int) field->max_length, row[i]);
 
511
      }
 
512
    }
 
513
    if (!opt_silent)
 
514
    {
 
515
      fputc('\t', stdout);
 
516
      fputc('\n', stdout);
 
517
    }
 
518
    row_count++;
 
519
  }
 
520
  if (!opt_silent)
 
521
  {
 
522
    if (row_count)
 
523
      my_print_dashes(result);
 
524
 
 
525
    if (mysql_errno(mysql) != 0)
 
526
      fprintf(stderr, "\n\tmysql_fetch_row() failed\n");
 
527
    else
 
528
      fprintf(stdout, "\n\t%d %s returned\n", row_count,
 
529
              row_count == 1 ? "row" : "rows");
 
530
  }
 
531
  return row_count;
 
532
}
 
533
 
 
534
 
 
535
static int my_process_result(MYSQL *mysql_arg)
 
536
{
 
537
  MYSQL_RES *result;
 
538
  int       row_count;
 
539
 
 
540
  if (!(result= mysql_store_result(mysql_arg)))
 
541
    return 0;
 
542
 
 
543
  row_count= my_process_result_set(result);
 
544
 
 
545
  mysql_free_result(result);
 
546
  return row_count;
 
547
}
 
548
 
 
549
 
 
550
/* Process the statement result set */
 
551
 
 
552
#define MAX_RES_FIELDS 50
 
553
#define MAX_FIELD_DATA_SIZE 255
 
554
 
 
555
static int my_process_stmt_result(MYSQL_STMT *stmt)
 
556
{
 
557
  int         field_count;
 
558
  int         row_count= 0;
 
559
  MYSQL_BIND  buffer[MAX_RES_FIELDS];
 
560
  MYSQL_FIELD *field;
 
561
  MYSQL_RES   *result;
 
562
  char        data[MAX_RES_FIELDS][MAX_FIELD_DATA_SIZE];
 
563
  ulong       length[MAX_RES_FIELDS];
 
564
  my_bool     is_null[MAX_RES_FIELDS];
 
565
  int         rc, i;
 
566
 
 
567
  if (!(result= mysql_stmt_result_metadata(stmt))) /* No meta info */
 
568
  {
 
569
    while (!mysql_stmt_fetch(stmt))
 
570
      row_count++;
 
571
    return row_count;
 
572
  }
 
573
 
 
574
  field_count= min(mysql_num_fields(result), MAX_RES_FIELDS);
 
575
 
 
576
  bzero((char*) buffer, sizeof(buffer));
 
577
  bzero((char*) length, sizeof(length));
 
578
  bzero((char*) is_null, sizeof(is_null));
 
579
 
 
580
  for(i= 0; i < field_count; i++)
 
581
  {
 
582
    buffer[i].buffer_type= MYSQL_TYPE_STRING;
 
583
    buffer[i].buffer_length= MAX_FIELD_DATA_SIZE;
 
584
    buffer[i].length= &length[i];
 
585
    buffer[i].buffer= (void *) data[i];
 
586
    buffer[i].is_null= &is_null[i];
 
587
  }
 
588
 
 
589
  rc= mysql_stmt_bind_result(stmt, buffer);
 
590
  check_execute(stmt, rc);
 
591
 
 
592
  rc= 1;
 
593
  mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*)&rc);
 
594
  rc= mysql_stmt_store_result(stmt);
 
595
  check_execute(stmt, rc);
 
596
  my_print_result_metadata(result);
 
597
 
 
598
  mysql_field_seek(result, 0);
 
599
  while ((rc= mysql_stmt_fetch(stmt)) == 0)
 
600
  {
 
601
    if (!opt_silent)
 
602
    {
 
603
      fputc('\t', stdout);
 
604
      fputc('|', stdout);
 
605
    }
 
606
    mysql_field_seek(result, 0);
 
607
    for (i= 0; i < field_count; i++)
 
608
    {
 
609
      field= mysql_fetch_field(result);
 
610
      if (!opt_silent)
 
611
      {
 
612
        if (is_null[i])
 
613
          fprintf(stdout, " %-*s |", (int) field->max_length, "NULL");
 
614
        else if (length[i] == 0)
 
615
        {
 
616
          data[i][0]= '\0';  /* unmodified buffer */
 
617
          fprintf(stdout, " %*s |", (int) field->max_length, data[i]);
 
618
        }
 
619
        else if (IS_NUM(field->type))
 
620
          fprintf(stdout, " %*s |", (int) field->max_length, data[i]);
 
621
        else
 
622
          fprintf(stdout, " %-*s |", (int) field->max_length, data[i]);
 
623
      }
 
624
    }
 
625
    if (!opt_silent)
 
626
    {
 
627
      fputc('\t', stdout);
 
628
      fputc('\n', stdout);
 
629
    }
 
630
    row_count++;
 
631
  }
 
632
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
633
  if (!opt_silent)
 
634
  {
 
635
    if (row_count)
 
636
      my_print_dashes(result);
 
637
    fprintf(stdout, "\n\t%d %s returned\n", row_count,
 
638
            row_count == 1 ? "row" : "rows");
 
639
  }
 
640
  mysql_free_result(result);
 
641
  return row_count;
 
642
}
 
643
 
 
644
 
 
645
/* Prepare statement, execute, and process result set for given query */
 
646
 
 
647
int my_stmt_result(const char *buff)
 
648
{
 
649
  MYSQL_STMT *stmt;
 
650
  int        row_count;
 
651
  int        rc;
 
652
 
 
653
  if (!opt_silent)
 
654
    fprintf(stdout, "\n\n %s", buff);
 
655
  stmt= mysql_simple_prepare(mysql, buff);
 
656
  check_stmt(stmt);
 
657
 
 
658
  rc= mysql_stmt_execute(stmt);
 
659
  check_execute(stmt, rc);
 
660
 
 
661
  row_count= my_process_stmt_result(stmt);
 
662
  mysql_stmt_close(stmt);
 
663
 
 
664
  return row_count;
 
665
}
 
666
 
 
667
 
 
668
/* Utility function to verify a particular column data */
 
669
 
 
670
static void verify_col_data(const char *table, const char *col,
 
671
                            const char *exp_data)
 
672
{
 
673
  static char query[MAX_TEST_QUERY_LENGTH];
 
674
  MYSQL_RES *result;
 
675
  MYSQL_ROW row;
 
676
  int       rc, field= 1;
 
677
 
 
678
  if (table && col)
 
679
  {
 
680
    strxmov(query, "SELECT ", col, " FROM ", table, " LIMIT 1", NullS);
 
681
    if (!opt_silent)
 
682
      fprintf(stdout, "\n %s", query);
 
683
    rc= mysql_query(mysql, query);
 
684
    myquery(rc);
 
685
 
 
686
    field= 0;
 
687
  }
 
688
 
 
689
  result= mysql_use_result(mysql);
 
690
  mytest(result);
 
691
 
 
692
  if (!(row= mysql_fetch_row(result)) || !row[field])
 
693
  {
 
694
    fprintf(stdout, "\n *** ERROR: FAILED TO GET THE RESULT ***");
 
695
    exit(1);
 
696
  }
 
697
  if (strcmp(row[field], exp_data))
 
698
  {
 
699
    fprintf(stdout, "\n obtained: `%s` (expected: `%s`)",
 
700
            row[field], exp_data);
 
701
    DIE_UNLESS(FALSE);
 
702
  }
 
703
  mysql_free_result(result);
 
704
}
 
705
 
 
706
 
 
707
/* Utility function to verify the field members */
 
708
 
 
709
#define verify_prepare_field(result,no,name,org_name,type,table,\
 
710
                             org_table,db,length,def) \
 
711
          do_verify_prepare_field((result),(no),(name),(org_name),(type), \
 
712
                                  (table),(org_table),(db),(length),(def), \
 
713
                                  __FILE__, __LINE__)
 
714
 
 
715
static void do_verify_prepare_field(MYSQL_RES *result,
 
716
                                   unsigned int no, const char *name,
 
717
                                   const char *org_name,
 
718
                                   enum enum_field_types type,
 
719
                                   const char *table,
 
720
                                   const char *org_table, const char *db,
 
721
                                   unsigned long length, const char *def,
 
722
                                   const char *file, int line)
 
723
{
 
724
  MYSQL_FIELD *field;
 
725
  CHARSET_INFO *cs;
 
726
 
 
727
  if (!(field= mysql_fetch_field_direct(result, no)))
 
728
  {
 
729
    fprintf(stdout, "\n *** ERROR: FAILED TO GET THE RESULT ***");
 
730
    exit(1);
 
731
  }
 
732
  cs= get_charset(field->charsetnr, 0);
 
733
  DIE_UNLESS(cs);
 
734
  if (!opt_silent)
 
735
  {
 
736
    fprintf(stdout, "\n field[%d]:", no);
 
737
    fprintf(stdout, "\n    name     :`%s`\t(expected: `%s`)", field->name, name);
 
738
    fprintf(stdout, "\n    org_name :`%s`\t(expected: `%s`)",
 
739
            field->org_name, org_name);
 
740
    fprintf(stdout, "\n    type     :`%d`\t(expected: `%d`)", field->type, type);
 
741
    if (table)
 
742
      fprintf(stdout, "\n    table    :`%s`\t(expected: `%s`)",
 
743
              field->table, table);
 
744
    if (org_table)            
 
745
      fprintf(stdout, "\n    org_table:`%s`\t(expected: `%s`)",
 
746
              field->org_table, org_table);
 
747
    fprintf(stdout, "\n    database :`%s`\t(expected: `%s`)", field->db, db);
 
748
    fprintf(stdout, "\n    length   :`%lu`\t(expected: `%lu`)",
 
749
            field->length, length * cs->mbmaxlen);
 
750
    fprintf(stdout, "\n    maxlength:`%ld`", field->max_length);
 
751
    fprintf(stdout, "\n    charsetnr:`%d`", field->charsetnr);
 
752
    fprintf(stdout, "\n    default  :`%s`\t(expected: `%s`)",
 
753
            field->def ? field->def : "(null)", def ? def: "(null)");
 
754
    fprintf(stdout, "\n");
 
755
  }
 
756
  DIE_UNLESS(strcmp(field->name, name) == 0);
 
757
  DIE_UNLESS(strcmp(field->org_name, org_name) == 0);
 
758
  /*
 
759
    XXX: silent column specification change works based on number of
 
760
    bytes a column occupies. So CHAR -> VARCHAR upgrade is possible even
 
761
    for CHAR(2) column if its character set is multibyte.
 
762
    VARCHAR -> CHAR downgrade won't work for VARCHAR(3) as one would
 
763
    expect.
 
764
  */
 
765
  if (cs->mbmaxlen == 1)
 
766
  {
 
767
    if (field->type != type)
 
768
    {
 
769
      fprintf(stderr,
 
770
              "Expected field type: %d,  got type: %d in file %s, line %d\n",
 
771
              (int) type, (int) field->type, file, line);
 
772
      DIE_UNLESS(field->type == type);
 
773
    }
 
774
  }
 
775
  if (table)
 
776
    DIE_UNLESS(strcmp(field->table, table) == 0);
 
777
  if (org_table)
 
778
    DIE_UNLESS(strcmp(field->org_table, org_table) == 0);
 
779
  DIE_UNLESS(strcmp(field->db, db) == 0);
 
780
  /*
 
781
    Character set should be taken into account for multibyte encodings, such
 
782
    as utf8. Field length is calculated as number of characters * maximum
 
783
    number of bytes a character can occupy.
 
784
  */
 
785
  if (length && field->length != length * cs->mbmaxlen)
 
786
  {
 
787
    fprintf(stderr, "Expected field length: %d,  got length: %d\n",
 
788
            (int) (length * cs->mbmaxlen), (int) field->length);
 
789
    DIE_UNLESS(field->length == length * cs->mbmaxlen);
 
790
  }
 
791
  if (def)
 
792
    DIE_UNLESS(strcmp(field->def, def) == 0);
 
793
}
 
794
 
 
795
 
 
796
/* Utility function to verify the parameter count */
 
797
 
 
798
static void verify_param_count(MYSQL_STMT *stmt, long exp_count)
 
799
{
 
800
  long param_count= mysql_stmt_param_count(stmt);
 
801
  if (!opt_silent)
 
802
    fprintf(stdout, "\n total parameters in stmt: `%ld` (expected: `%ld`)",
 
803
            param_count, exp_count);
 
804
  DIE_UNLESS(param_count == exp_count);
 
805
}
 
806
 
 
807
 
 
808
/* Utility function to verify the total affected rows */
 
809
 
 
810
static void verify_st_affected_rows(MYSQL_STMT *stmt, ulonglong exp_count)
 
811
{
 
812
  ulonglong affected_rows= mysql_stmt_affected_rows(stmt);
 
813
  if (!opt_silent)
 
814
    fprintf(stdout, "\n total affected rows: `%ld` (expected: `%ld`)",
 
815
            (long) affected_rows, (long) exp_count);
 
816
  DIE_UNLESS(affected_rows == exp_count);
 
817
}
 
818
 
 
819
 
 
820
/* Utility function to verify the total affected rows */
 
821
 
 
822
static void verify_affected_rows(ulonglong exp_count)
 
823
{
 
824
  ulonglong affected_rows= mysql_affected_rows(mysql);
 
825
  if (!opt_silent)
 
826
    fprintf(stdout, "\n total affected rows: `%ld` (expected: `%ld`)",
 
827
            (long) affected_rows, (long) exp_count);
 
828
  DIE_UNLESS(affected_rows == exp_count);
 
829
}
 
830
 
 
831
 
 
832
/* Utility function to verify the total fields count */
 
833
 
 
834
static void verify_field_count(MYSQL_RES *result, uint exp_count)
 
835
{
 
836
  uint field_count= mysql_num_fields(result);
 
837
  if (!opt_silent)
 
838
    fprintf(stdout, "\n total fields in the result set: `%d` (expected: `%d`)",
 
839
            field_count, exp_count);
 
840
  DIE_UNLESS(field_count == exp_count);
 
841
}
 
842
 
 
843
 
 
844
/* Utility function to execute a query using prepare-execute */
 
845
 
 
846
#ifndef EMBEDDED_LIBRARY
 
847
static void execute_prepare_query(const char *query, ulonglong exp_count)
 
848
{
 
849
  MYSQL_STMT *stmt;
 
850
  ulonglong  affected_rows;
 
851
  int        rc;
 
852
 
 
853
  stmt= mysql_simple_prepare(mysql, query);
 
854
  check_stmt(stmt);
 
855
 
 
856
  rc= mysql_stmt_execute(stmt);
 
857
  myquery(rc);
 
858
 
 
859
  affected_rows= mysql_stmt_affected_rows(stmt);
 
860
  if (!opt_silent)
 
861
    fprintf(stdout, "\n total affected rows: `%ld` (expected: `%ld`)",
 
862
            (long) affected_rows, (long) exp_count);
 
863
 
 
864
  DIE_UNLESS(affected_rows == exp_count);
 
865
  mysql_stmt_close(stmt);
 
866
}
 
867
#endif
 
868
 
 
869
/* Store result processing */
 
870
 
 
871
static void client_store_result()
 
872
{
 
873
  MYSQL_RES *result;
 
874
  int       rc;
 
875
 
 
876
  myheader("client_store_result");
 
877
 
 
878
  rc= mysql_query(mysql, "SELECT * FROM t1");
 
879
  myquery(rc);
 
880
 
 
881
  /* get the result */
 
882
  result= mysql_store_result(mysql);
 
883
  mytest(result);
 
884
 
 
885
  (void) my_process_result_set(result);
 
886
  mysql_free_result(result);
 
887
}
 
888
 
 
889
 
 
890
/* Fetch the results */
 
891
 
 
892
static void client_use_result()
 
893
{
 
894
  MYSQL_RES *result;
 
895
  int       rc;
 
896
  myheader("client_use_result");
 
897
 
 
898
  rc= mysql_query(mysql, "SELECT * FROM t1");
 
899
  myquery(rc);
 
900
 
 
901
  /* get the result */
 
902
  result= mysql_use_result(mysql);
 
903
  mytest(result);
 
904
 
 
905
  (void) my_process_result_set(result);
 
906
  mysql_free_result(result);
 
907
}
 
908
 
 
909
 
 
910
/*
 
911
  Accepts arbitrary number of queries and runs them against the database.
 
912
  Used to fill tables for each test.
 
913
*/
 
914
 
 
915
void fill_tables(const char **query_list, unsigned query_count)
 
916
{
 
917
  int rc;
 
918
  const char **query;
 
919
  DBUG_ENTER("fill_tables");
 
920
  for (query= query_list; query < query_list + query_count;
 
921
       ++query)
 
922
  {
 
923
    rc= mysql_query(mysql, *query);
 
924
    myquery(rc);
 
925
  }
 
926
  DBUG_VOID_RETURN;
 
927
}
 
928
 
 
929
/*
 
930
  All state of fetch from one statement: statement handle, out buffers,
 
931
  fetch position.
 
932
  See fetch_n for for the only use case.
 
933
*/
 
934
 
 
935
enum { MAX_COLUMN_LENGTH= 255 };
 
936
 
 
937
typedef struct st_stmt_fetch
 
938
{
 
939
  const char *query;
 
940
  unsigned stmt_no;
 
941
  MYSQL_STMT *handle;
 
942
  my_bool is_open;
 
943
  MYSQL_BIND *bind_array;
 
944
  char **out_data;
 
945
  unsigned long *out_data_length;
 
946
  unsigned column_count;
 
947
  unsigned row_count;
 
948
} Stmt_fetch;
 
949
 
 
950
 
 
951
/*
 
952
  Create statement handle, prepare it with statement, execute and allocate
 
953
  fetch buffers.
 
954
*/
 
955
 
 
956
void stmt_fetch_init(Stmt_fetch *fetch, unsigned stmt_no_arg,
 
957
                     const char *query_arg)
 
958
{
 
959
  unsigned long type= CURSOR_TYPE_READ_ONLY;
 
960
  int rc;
 
961
  unsigned i;
 
962
  MYSQL_RES *metadata;
 
963
  DBUG_ENTER("stmt_fetch_init");
 
964
 
 
965
  /* Save query and statement number for error messages */
 
966
  fetch->stmt_no= stmt_no_arg;
 
967
  fetch->query= query_arg;
 
968
 
 
969
  fetch->handle= mysql_stmt_init(mysql);
 
970
 
 
971
  rc= mysql_stmt_prepare(fetch->handle, fetch->query, strlen(fetch->query));
 
972
  check_execute(fetch->handle, rc);
 
973
 
 
974
  /*
 
975
    The attribute is sent to server on execute and asks to open read-only
 
976
    for result set
 
977
  */
 
978
  mysql_stmt_attr_set(fetch->handle, STMT_ATTR_CURSOR_TYPE,
 
979
                      (const void*) &type);
 
980
 
 
981
  rc= mysql_stmt_execute(fetch->handle);
 
982
  check_execute(fetch->handle, rc);
 
983
 
 
984
  /* Find out total number of columns in result set */
 
985
  metadata= mysql_stmt_result_metadata(fetch->handle);
 
986
  fetch->column_count= mysql_num_fields(metadata);
 
987
  mysql_free_result(metadata);
 
988
 
 
989
  /*
 
990
    Now allocate bind handles and buffers for output data:
 
991
    calloc memory to reduce number of MYSQL_BIND members we need to
 
992
    set up.
 
993
  */
 
994
 
 
995
  fetch->bind_array= (MYSQL_BIND *) calloc(1, sizeof(MYSQL_BIND) *
 
996
                                              fetch->column_count);
 
997
  fetch->out_data= (char**) calloc(1, sizeof(char*) * fetch->column_count);
 
998
  fetch->out_data_length= (ulong*) calloc(1, sizeof(ulong) *
 
999
                                             fetch->column_count);
 
1000
  for (i= 0; i < fetch->column_count; ++i)
 
1001
  {
 
1002
    fetch->out_data[i]= (char*) calloc(1, MAX_COLUMN_LENGTH);
 
1003
    fetch->bind_array[i].buffer_type= MYSQL_TYPE_STRING;
 
1004
    fetch->bind_array[i].buffer= fetch->out_data[i];
 
1005
    fetch->bind_array[i].buffer_length= MAX_COLUMN_LENGTH;
 
1006
    fetch->bind_array[i].length= fetch->out_data_length + i;
 
1007
  }
 
1008
 
 
1009
  mysql_stmt_bind_result(fetch->handle, fetch->bind_array);
 
1010
 
 
1011
  fetch->row_count= 0;
 
1012
  fetch->is_open= TRUE;
 
1013
 
 
1014
  /* Ready for reading rows */
 
1015
  DBUG_VOID_RETURN;
 
1016
}
 
1017
 
 
1018
 
 
1019
/* Fetch and print one row from cursor */
 
1020
 
 
1021
int stmt_fetch_fetch_row(Stmt_fetch *fetch)
 
1022
{
 
1023
  int rc;
 
1024
  unsigned i;
 
1025
  DBUG_ENTER("stmt_fetch_fetch_row");
 
1026
 
 
1027
  if ((rc= mysql_stmt_fetch(fetch->handle)) == 0)
 
1028
  {
 
1029
    ++fetch->row_count;
 
1030
    if (!opt_silent)
 
1031
      printf("Stmt %d fetched row %d:\n", fetch->stmt_no, fetch->row_count);
 
1032
    for (i= 0; i < fetch->column_count; ++i)
 
1033
    {
 
1034
      fetch->out_data[i][fetch->out_data_length[i]]= '\0';
 
1035
      if (!opt_silent)
 
1036
        printf("column %d: %s\n", i+1, fetch->out_data[i]);
 
1037
    }
 
1038
  }
 
1039
  else
 
1040
    fetch->is_open= FALSE;
 
1041
  DBUG_RETURN(rc);
 
1042
}
 
1043
 
 
1044
 
 
1045
void stmt_fetch_close(Stmt_fetch *fetch)
 
1046
{
 
1047
  unsigned i;
 
1048
  DBUG_ENTER("stmt_fetch_close");
 
1049
 
 
1050
  for (i= 0; i < fetch->column_count; ++i)
 
1051
    free(fetch->out_data[i]);
 
1052
  free(fetch->out_data);
 
1053
  free(fetch->out_data_length);
 
1054
  free(fetch->bind_array);
 
1055
  mysql_stmt_close(fetch->handle);
 
1056
  DBUG_VOID_RETURN;
 
1057
}
 
1058
 
 
1059
/*
 
1060
  For given array of queries, open query_count cursors and fetch
 
1061
  from them in simultaneous manner.
 
1062
  In case there was an error in one of the cursors, continue
 
1063
  reading from the rest.
 
1064
*/
 
1065
 
 
1066
enum fetch_type { USE_ROW_BY_ROW_FETCH= 0, USE_STORE_RESULT= 1 };
 
1067
 
 
1068
my_bool fetch_n(const char **query_list, unsigned query_count,
 
1069
                enum fetch_type fetch_type)
 
1070
{
 
1071
  unsigned open_statements= query_count;
 
1072
  int rc, error_count= 0;
 
1073
  Stmt_fetch *fetch_array= (Stmt_fetch*) calloc(1, sizeof(Stmt_fetch) *
 
1074
                                                  query_count);
 
1075
  Stmt_fetch *fetch;
 
1076
  DBUG_ENTER("fetch_n");
 
1077
 
 
1078
  for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
 
1079
  {
 
1080
    /* Init will exit(1) in case of error */
 
1081
    stmt_fetch_init(fetch, fetch - fetch_array,
 
1082
                    query_list[fetch - fetch_array]);
 
1083
  }
 
1084
 
 
1085
  if (fetch_type == USE_STORE_RESULT)
 
1086
  {
 
1087
    for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
 
1088
    {
 
1089
      rc= mysql_stmt_store_result(fetch->handle);
 
1090
      check_execute(fetch->handle, rc);
 
1091
    }
 
1092
  }
 
1093
 
 
1094
  while (open_statements)
 
1095
  {
 
1096
    for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
 
1097
    {
 
1098
      if (fetch->is_open && (rc= stmt_fetch_fetch_row(fetch)))
 
1099
      {
 
1100
        open_statements--;
 
1101
        /*
 
1102
          We try to fetch from the rest of the statements in case of
 
1103
          error
 
1104
        */
 
1105
        if (rc != MYSQL_NO_DATA)
 
1106
        {
 
1107
          fprintf(stderr,
 
1108
                  "Got error reading rows from statement %d,\n"
 
1109
                  "query is: %s,\n"
 
1110
                  "error message: %s", (int) (fetch - fetch_array),
 
1111
                  fetch->query,
 
1112
                  mysql_stmt_error(fetch->handle));
 
1113
          error_count++;
 
1114
        }
 
1115
      }
 
1116
    }
 
1117
  }
 
1118
  if (error_count)
 
1119
    fprintf(stderr, "Fetch FAILED");
 
1120
  else
 
1121
  {
 
1122
    unsigned total_row_count= 0;
 
1123
    for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
 
1124
      total_row_count+= fetch->row_count;
 
1125
    if (!opt_silent)
 
1126
      printf("Success, total rows fetched: %d\n", total_row_count);
 
1127
  }
 
1128
  for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
 
1129
    stmt_fetch_close(fetch);
 
1130
  free(fetch_array);
 
1131
  DBUG_RETURN(error_count != 0);
 
1132
}
 
1133
 
 
1134
/* Separate thread query to test some cases */
 
1135
 
 
1136
static my_bool thread_query(char *query)
 
1137
{
 
1138
  MYSQL *l_mysql;
 
1139
  my_bool error;
 
1140
 
 
1141
  error= 0;
 
1142
  if (!opt_silent)
 
1143
    fprintf(stdout, "\n in thread_query(%s)", query);
 
1144
  if (!(l_mysql= mysql_init(NULL)))
 
1145
  {
 
1146
    myerror("mysql_init() failed");
 
1147
    return 1;
 
1148
  }
 
1149
  if (!(mysql_real_connect(l_mysql, opt_host, opt_user,
 
1150
                           opt_password, current_db, opt_port,
 
1151
                           opt_unix_socket, 0)))
 
1152
  {
 
1153
    myerror("connection failed");
 
1154
    error= 1;
 
1155
    goto end;
 
1156
  }
 
1157
  l_mysql->reconnect= 1;
 
1158
  if (mysql_query(l_mysql, (char *)query))
 
1159
  {
 
1160
     fprintf(stderr, "Query failed (%s)\n", mysql_error(l_mysql));
 
1161
     error= 1;
 
1162
     goto end;
 
1163
  }
 
1164
  mysql_commit(l_mysql);
 
1165
end:
 
1166
  mysql_close(l_mysql);
 
1167
  return error;
 
1168
}
 
1169
 
 
1170
 
 
1171
/* Query processing */
 
1172
 
 
1173
static void test_debug_example()
 
1174
{
 
1175
  int rc;
 
1176
  MYSQL_RES *result;
 
1177
 
 
1178
  myheader("test_debug_example");
 
1179
 
 
1180
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_debug_example");
 
1181
  myquery(rc);
 
1182
 
 
1183
  rc= mysql_query(mysql, "CREATE TABLE test_debug_example("
 
1184
                         "id INT PRIMARY KEY AUTO_INCREMENT, "
 
1185
                         "name VARCHAR(20), xxx INT)");
 
1186
  myquery(rc);
 
1187
 
 
1188
  rc= mysql_query(mysql, "INSERT INTO test_debug_example (name) "
 
1189
                         "VALUES ('mysql')");
 
1190
  myquery(rc);
 
1191
 
 
1192
  rc= mysql_query(mysql, "UPDATE test_debug_example SET name='updated' "
 
1193
                         "WHERE name='deleted'");
 
1194
  myquery(rc);
 
1195
 
 
1196
  rc= mysql_query(mysql, "SELECT * FROM test_debug_example where name='mysql'");
 
1197
  myquery(rc);
 
1198
 
 
1199
  result= mysql_use_result(mysql);
 
1200
  mytest(result);
 
1201
 
 
1202
  (void) my_process_result_set(result);
 
1203
  mysql_free_result(result);
 
1204
 
 
1205
  rc= mysql_query(mysql, "DROP TABLE test_debug_example");
 
1206
  myquery(rc);
 
1207
}
 
1208
 
 
1209
 
 
1210
/* Test autocommit feature for BDB tables */
 
1211
 
 
1212
static void test_tran_bdb()
 
1213
{
 
1214
  MYSQL_RES *result;
 
1215
  MYSQL_ROW row;
 
1216
  int       rc;
 
1217
 
 
1218
  myheader("test_tran_bdb");
 
1219
 
 
1220
  /* set AUTOCOMMIT to OFF */
 
1221
  rc= mysql_autocommit(mysql, FALSE);
 
1222
  myquery(rc);
 
1223
 
 
1224
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_demo_transaction");
 
1225
  myquery(rc);
 
1226
 
 
1227
 
 
1228
  /* create the table 'mytran_demo' of type BDB' or 'InnoDB' */
 
1229
  rc= mysql_query(mysql, "CREATE TABLE my_demo_transaction( "
 
1230
                         "col1 int , col2 varchar(30)) ENGINE= BDB");
 
1231
  myquery(rc);
 
1232
 
 
1233
  /* insert a row and commit the transaction */
 
1234
  rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(10, 'venu')");
 
1235
  myquery(rc);
 
1236
 
 
1237
  rc= mysql_commit(mysql);
 
1238
  myquery(rc);
 
1239
 
 
1240
  /* now insert the second row, and roll back the transaction */
 
1241
  rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(20, 'mysql')");
 
1242
  myquery(rc);
 
1243
 
 
1244
  rc= mysql_rollback(mysql);
 
1245
  myquery(rc);
 
1246
 
 
1247
  /* delete first row, and roll it back */
 
1248
  rc= mysql_query(mysql, "DELETE FROM my_demo_transaction WHERE col1= 10");
 
1249
  myquery(rc);
 
1250
 
 
1251
  rc= mysql_rollback(mysql);
 
1252
  myquery(rc);
 
1253
 
 
1254
  /* test the results now, only one row should exist */
 
1255
  rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction");
 
1256
  myquery(rc);
 
1257
 
 
1258
  /* get the result */
 
1259
  result= mysql_store_result(mysql);
 
1260
  mytest(result);
 
1261
 
 
1262
  (void) my_process_result_set(result);
 
1263
  mysql_free_result(result);
 
1264
 
 
1265
  /* test the results now, only one row should exist */
 
1266
  rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction");
 
1267
  myquery(rc);
 
1268
 
 
1269
  /* get the result */
 
1270
  result= mysql_use_result(mysql);
 
1271
  mytest(result);
 
1272
 
 
1273
  row= mysql_fetch_row(result);
 
1274
  mytest(row);
 
1275
 
 
1276
  row= mysql_fetch_row(result);
 
1277
  mytest_r(row);
 
1278
 
 
1279
  mysql_free_result(result);
 
1280
  mysql_autocommit(mysql, TRUE);
 
1281
}
 
1282
 
 
1283
 
 
1284
/* Test autocommit feature for InnoDB tables */
 
1285
 
 
1286
static void test_tran_innodb()
 
1287
{
 
1288
  MYSQL_RES *result;
 
1289
  MYSQL_ROW row;
 
1290
  int       rc;
 
1291
 
 
1292
  myheader("test_tran_innodb");
 
1293
 
 
1294
  /* set AUTOCOMMIT to OFF */
 
1295
  rc= mysql_autocommit(mysql, FALSE);
 
1296
  myquery(rc);
 
1297
 
 
1298
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_demo_transaction");
 
1299
  myquery(rc);
 
1300
 
 
1301
  /* create the table 'mytran_demo' of type BDB' or 'InnoDB' */
 
1302
  rc= mysql_query(mysql, "CREATE TABLE my_demo_transaction(col1 int, "
 
1303
                         "col2 varchar(30)) ENGINE= InnoDB");
 
1304
  myquery(rc);
 
1305
 
 
1306
  /* insert a row and commit the transaction */
 
1307
  rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(10, 'venu')");
 
1308
  myquery(rc);
 
1309
 
 
1310
  rc= mysql_commit(mysql);
 
1311
  myquery(rc);
 
1312
 
 
1313
  /* now insert the second row, and roll back the transaction */
 
1314
  rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(20, 'mysql')");
 
1315
  myquery(rc);
 
1316
 
 
1317
  rc= mysql_rollback(mysql);
 
1318
  myquery(rc);
 
1319
 
 
1320
  /* delete first row, and roll it back */
 
1321
  rc= mysql_query(mysql, "DELETE FROM my_demo_transaction WHERE col1= 10");
 
1322
  myquery(rc);
 
1323
 
 
1324
  rc= mysql_rollback(mysql);
 
1325
  myquery(rc);
 
1326
 
 
1327
  /* test the results now, only one row should exist */
 
1328
  rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction");
 
1329
  myquery(rc);
 
1330
 
 
1331
  /* get the result */
 
1332
  result= mysql_store_result(mysql);
 
1333
  mytest(result);
 
1334
 
 
1335
  (void) my_process_result_set(result);
 
1336
  mysql_free_result(result);
 
1337
 
 
1338
  /* test the results now, only one row should exist */
 
1339
  rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction");
 
1340
  myquery(rc);
 
1341
 
 
1342
  /* get the result */
 
1343
  result= mysql_use_result(mysql);
 
1344
  mytest(result);
 
1345
 
 
1346
  row= mysql_fetch_row(result);
 
1347
  mytest(row);
 
1348
 
 
1349
  row= mysql_fetch_row(result);
 
1350
  mytest_r(row);
 
1351
 
 
1352
  mysql_free_result(result);
 
1353
  mysql_autocommit(mysql, TRUE);
 
1354
}
 
1355
 
 
1356
 
 
1357
/* Test for BUG#7242 */
 
1358
 
 
1359
static void test_prepare_insert_update()
 
1360
{
 
1361
  MYSQL_STMT *stmt;
 
1362
  int        rc;
 
1363
  int        i;
 
1364
  const char *testcase[]= {
 
1365
    "CREATE TABLE t1 (a INT, b INT, c INT, UNIQUE (A), UNIQUE(B))",
 
1366
    "INSERT t1 VALUES (1,2,10), (3,4,20)",
 
1367
    "INSERT t1 VALUES (5,6,30), (7,4,40), (8,9,60) ON DUPLICATE KEY UPDATE c=c+100",
 
1368
    "SELECT * FROM t1",
 
1369
    "INSERT t1 SET a=5 ON DUPLICATE KEY UPDATE b=0",
 
1370
    "SELECT * FROM t1",
 
1371
    "INSERT t1 VALUES (2,1,11), (7,4,40) ON DUPLICATE KEY UPDATE c=c+VALUES(a)",
 
1372
    NULL};
 
1373
  const char **cur_query;
 
1374
 
 
1375
  myheader("test_prepare_insert_update");
 
1376
  
 
1377
  for (cur_query= testcase; *cur_query; cur_query++)
 
1378
  {
 
1379
    char query[MAX_TEST_QUERY_LENGTH];
 
1380
    printf("\nRunning query: %s", *cur_query);
 
1381
    strmov(query, *cur_query);
 
1382
    stmt= mysql_simple_prepare(mysql, query);
 
1383
    check_stmt(stmt);
 
1384
 
 
1385
    verify_param_count(stmt, 0);
 
1386
    rc= mysql_stmt_execute(stmt);
 
1387
 
 
1388
    check_execute(stmt, rc);
 
1389
    /* try the last query several times */
 
1390
    if (!cur_query[1])
 
1391
    {
 
1392
      for (i=0; i < 3;i++)
 
1393
      {
 
1394
        printf("\nExecuting last statement again");
 
1395
        rc= mysql_stmt_execute(stmt);
 
1396
        check_execute(stmt, rc);
 
1397
        rc= mysql_stmt_execute(stmt);
 
1398
        check_execute(stmt, rc);
 
1399
      }
 
1400
    }
 
1401
    mysql_stmt_close(stmt);
 
1402
  }
 
1403
 
 
1404
  rc= mysql_commit(mysql);
 
1405
  myquery(rc);
 
1406
}
 
1407
 
 
1408
/* Test simple prepares of all DML statements */
 
1409
 
 
1410
static void test_prepare_simple()
 
1411
{
 
1412
  MYSQL_STMT *stmt;
 
1413
  int        rc;
 
1414
  char query[MAX_TEST_QUERY_LENGTH];
 
1415
 
 
1416
  myheader("test_prepare_simple");
 
1417
 
 
1418
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_simple");
 
1419
  myquery(rc);
 
1420
 
 
1421
  rc= mysql_query(mysql, "CREATE TABLE test_prepare_simple("
 
1422
                         "id int, name varchar(50))");
 
1423
  myquery(rc);
 
1424
 
 
1425
  /* insert */
 
1426
  strmov(query, "INSERT INTO test_prepare_simple VALUES(?, ?)");
 
1427
  stmt= mysql_simple_prepare(mysql, query);
 
1428
  check_stmt(stmt);
 
1429
 
 
1430
  verify_param_count(stmt, 2);
 
1431
  mysql_stmt_close(stmt);
 
1432
 
 
1433
  /* update */
 
1434
  strmov(query, "UPDATE test_prepare_simple SET id=? "
 
1435
                "WHERE id=? AND CONVERT(name USING utf8)= ?");
 
1436
  stmt= mysql_simple_prepare(mysql, query);
 
1437
  check_stmt(stmt);
 
1438
 
 
1439
  verify_param_count(stmt, 3);
 
1440
  mysql_stmt_close(stmt);
 
1441
 
 
1442
  /* delete */
 
1443
  strmov(query, "DELETE FROM test_prepare_simple WHERE id=10");
 
1444
  stmt= mysql_simple_prepare(mysql, query);
 
1445
  check_stmt(stmt);
 
1446
 
 
1447
  verify_param_count(stmt, 0);
 
1448
 
 
1449
  rc= mysql_stmt_execute(stmt);
 
1450
  check_execute(stmt, rc);
 
1451
  mysql_stmt_close(stmt);
 
1452
 
 
1453
  /* delete */
 
1454
  strmov(query, "DELETE FROM test_prepare_simple WHERE id=?");
 
1455
  stmt= mysql_simple_prepare(mysql, query);
 
1456
  check_stmt(stmt);
 
1457
 
 
1458
  verify_param_count(stmt, 1);
 
1459
 
 
1460
  mysql_stmt_close(stmt);
 
1461
 
 
1462
  /* select */
 
1463
  strmov(query, "SELECT * FROM test_prepare_simple WHERE id=? "
 
1464
                "AND CONVERT(name USING utf8)= ?");
 
1465
  stmt= mysql_simple_prepare(mysql, query);
 
1466
  check_stmt(stmt);
 
1467
 
 
1468
  verify_param_count(stmt, 2);
 
1469
 
 
1470
  mysql_stmt_close(stmt);
 
1471
 
 
1472
  /* now fetch the results ..*/
 
1473
  rc= mysql_commit(mysql);
 
1474
  myquery(rc);
 
1475
}
 
1476
 
 
1477
 
 
1478
/* Test simple prepare field results */
 
1479
 
 
1480
static void test_prepare_field_result()
 
1481
{
 
1482
  MYSQL_STMT *stmt;
 
1483
  MYSQL_RES  *result;
 
1484
  int        rc;
 
1485
  char query[MAX_TEST_QUERY_LENGTH];
 
1486
 
 
1487
  myheader("test_prepare_field_result");
 
1488
 
 
1489
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_field_result");
 
1490
  myquery(rc);
 
1491
 
 
1492
  rc= mysql_query(mysql, "CREATE TABLE test_prepare_field_result(int_c int, "
 
1493
                         "var_c varchar(50), ts_c timestamp, "
 
1494
                         "char_c char(4), date_c date, extra tinyint)");
 
1495
  myquery(rc);
 
1496
 
 
1497
  /* insert */
 
1498
  strmov(query, "SELECT int_c, var_c, date_c as date, ts_c, char_c FROM "
 
1499
                " test_prepare_field_result as t1 WHERE int_c=?");
 
1500
  stmt= mysql_simple_prepare(mysql, query);
 
1501
  check_stmt(stmt);
 
1502
 
 
1503
  verify_param_count(stmt, 1);
 
1504
 
 
1505
  result= mysql_stmt_result_metadata(stmt);
 
1506
  mytest(result);
 
1507
 
 
1508
  my_print_result_metadata(result);
 
1509
 
 
1510
  if (!opt_silent)
 
1511
    fprintf(stdout, "\n\n field attributes:\n");
 
1512
  verify_prepare_field(result, 0, "int_c", "int_c", MYSQL_TYPE_LONG,
 
1513
                       "t1", "test_prepare_field_result", current_db, 11, 0);
 
1514
  verify_prepare_field(result, 1, "var_c", "var_c", MYSQL_TYPE_VAR_STRING,
 
1515
                       "t1", "test_prepare_field_result", current_db, 50, 0);
 
1516
  verify_prepare_field(result, 2, "date", "date_c", MYSQL_TYPE_DATE,
 
1517
                       "t1", "test_prepare_field_result", current_db, 10, 0);
 
1518
  verify_prepare_field(result, 3, "ts_c", "ts_c", MYSQL_TYPE_TIMESTAMP,
 
1519
                       "t1", "test_prepare_field_result", current_db, 19, 0);
 
1520
  verify_prepare_field(result, 4, "char_c", "char_c",
 
1521
                       (mysql_get_server_version(mysql) <= 50000 ?
 
1522
                        MYSQL_TYPE_VAR_STRING : MYSQL_TYPE_STRING),
 
1523
                       "t1", "test_prepare_field_result", current_db, 4, 0);
 
1524
 
 
1525
  verify_field_count(result, 5);
 
1526
  mysql_free_result(result);
 
1527
  mysql_stmt_close(stmt);
 
1528
}
 
1529
 
 
1530
 
 
1531
/* Test simple prepare field results */
 
1532
 
 
1533
static void test_prepare_syntax()
 
1534
{
 
1535
  MYSQL_STMT *stmt;
 
1536
  int        rc;
 
1537
  char query[MAX_TEST_QUERY_LENGTH];
 
1538
 
 
1539
  myheader("test_prepare_syntax");
 
1540
 
 
1541
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_syntax");
 
1542
  myquery(rc);
 
1543
 
 
1544
  rc= mysql_query(mysql, "CREATE TABLE test_prepare_syntax("
 
1545
                         "id int, name varchar(50), extra int)");
 
1546
  myquery(rc);
 
1547
 
 
1548
  strmov(query, "INSERT INTO test_prepare_syntax VALUES(?");
 
1549
  stmt= mysql_simple_prepare(mysql, query);
 
1550
  check_stmt_r(stmt);
 
1551
 
 
1552
  strmov(query, "SELECT id, name FROM test_prepare_syntax WHERE id=? AND WHERE");
 
1553
  stmt= mysql_simple_prepare(mysql, query);
 
1554
  check_stmt_r(stmt);
 
1555
 
 
1556
  /* now fetch the results ..*/
 
1557
  rc= mysql_commit(mysql);
 
1558
  myquery(rc);
 
1559
}
 
1560
 
 
1561
 
 
1562
/* Test a simple prepare */
 
1563
 
 
1564
static void test_prepare()
 
1565
{
 
1566
  MYSQL_STMT *stmt;
 
1567
  int        rc, i;
 
1568
  int        int_data, o_int_data;
 
1569
  char       str_data[50], data[50];
 
1570
  char       tiny_data, o_tiny_data;
 
1571
  short      small_data, o_small_data;
 
1572
  longlong   big_data, o_big_data;
 
1573
  float      real_data, o_real_data;
 
1574
  double     double_data, o_double_data;
 
1575
  ulong      length[7], len;
 
1576
  my_bool    is_null[7];
 
1577
  char       llbuf[22];
 
1578
  MYSQL_BIND my_bind[7];
 
1579
  char query[MAX_TEST_QUERY_LENGTH];
 
1580
 
 
1581
  myheader("test_prepare");
 
1582
 
 
1583
  rc= mysql_autocommit(mysql, TRUE);
 
1584
  myquery(rc);
 
1585
 
 
1586
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_prepare");
 
1587
  myquery(rc);
 
1588
 
 
1589
  rc= mysql_query(mysql, "CREATE TABLE my_prepare(col1 tinyint, "
 
1590
                         "col2 varchar(15), col3 int, "
 
1591
                         "col4 smallint, col5 bigint, "
 
1592
                         "col6 float, col7 double )");
 
1593
  myquery(rc);
 
1594
 
 
1595
  /* insert by prepare */
 
1596
  strxmov(query, "INSERT INTO my_prepare VALUES(?, ?, ?, ?, ?, ?, ?)", NullS);
 
1597
  stmt= mysql_simple_prepare(mysql, query);
 
1598
  check_stmt(stmt);
 
1599
 
 
1600
  verify_param_count(stmt, 7);
 
1601
 
 
1602
  bzero((char*) my_bind, sizeof(my_bind));
 
1603
 
 
1604
  /* tinyint */
 
1605
  my_bind[0].buffer_type= MYSQL_TYPE_TINY;
 
1606
  my_bind[0].buffer= (void *)&tiny_data;
 
1607
  /* string */
 
1608
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
1609
  my_bind[1].buffer= (void *)str_data;
 
1610
  my_bind[1].buffer_length= 1000;                  /* Max string length */
 
1611
  /* integer */
 
1612
  my_bind[2].buffer_type= MYSQL_TYPE_LONG;
 
1613
  my_bind[2].buffer= (void *)&int_data;
 
1614
  /* short */
 
1615
  my_bind[3].buffer_type= MYSQL_TYPE_SHORT;
 
1616
  my_bind[3].buffer= (void *)&small_data;
 
1617
  /* bigint */
 
1618
  my_bind[4].buffer_type= MYSQL_TYPE_LONGLONG;
 
1619
  my_bind[4].buffer= (void *)&big_data;
 
1620
  /* float */
 
1621
  my_bind[5].buffer_type= MYSQL_TYPE_FLOAT;
 
1622
  my_bind[5].buffer= (void *)&real_data;
 
1623
  /* double */
 
1624
  my_bind[6].buffer_type= MYSQL_TYPE_DOUBLE;
 
1625
  my_bind[6].buffer= (void *)&double_data;
 
1626
 
 
1627
  for (i= 0; i < (int) array_elements(my_bind); i++)
 
1628
  {
 
1629
    my_bind[i].length= &length[i];
 
1630
    my_bind[i].is_null= &is_null[i];
 
1631
    is_null[i]= 0;
 
1632
  }
 
1633
 
 
1634
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
1635
  check_execute(stmt, rc);
 
1636
 
 
1637
  int_data= 320;
 
1638
  small_data= 1867;
 
1639
  big_data= 1000;
 
1640
  real_data= 2;
 
1641
  double_data= 6578.001;
 
1642
 
 
1643
  /* now, execute the prepared statement to insert 10 records.. */
 
1644
  for (tiny_data= 0; tiny_data < 100; tiny_data++)
 
1645
  {
 
1646
    length[1]= my_sprintf(str_data, (str_data, "MySQL%d", int_data));
 
1647
    rc= mysql_stmt_execute(stmt);
 
1648
    check_execute(stmt, rc);
 
1649
    int_data += 25;
 
1650
    small_data += 10;
 
1651
    big_data += 100;
 
1652
    real_data += 1;
 
1653
    double_data += 10.09;
 
1654
  }
 
1655
 
 
1656
  mysql_stmt_close(stmt);
 
1657
 
 
1658
  /* now fetch the results ..*/
 
1659
  rc= mysql_commit(mysql);
 
1660
  myquery(rc);
 
1661
 
 
1662
  /* test the results now, only one row should exist */
 
1663
  rc= my_stmt_result("SELECT * FROM my_prepare");
 
1664
  DIE_UNLESS(tiny_data == (char) rc);
 
1665
 
 
1666
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM my_prepare");
 
1667
  check_stmt(stmt);
 
1668
 
 
1669
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
1670
  check_execute(stmt, rc);
 
1671
 
 
1672
  /* get the result */
 
1673
  rc= mysql_stmt_execute(stmt);
 
1674
  check_execute(stmt, rc);
 
1675
 
 
1676
  o_int_data= 320;
 
1677
  o_small_data= 1867;
 
1678
  o_big_data= 1000;
 
1679
  o_real_data= 2;
 
1680
  o_double_data= 6578.001;
 
1681
 
 
1682
  /* now, execute the prepared statement to insert 10 records.. */
 
1683
  for (o_tiny_data= 0; o_tiny_data < 100; o_tiny_data++)
 
1684
  {
 
1685
    len= my_sprintf(data, (data, "MySQL%d", o_int_data));
 
1686
 
 
1687
    rc= mysql_stmt_fetch(stmt);
 
1688
    check_execute(stmt, rc);
 
1689
 
 
1690
    if (!opt_silent)
 
1691
    {
 
1692
      fprintf(stdout, "\n");
 
1693
      fprintf(stdout, "\n\t tiny   : %d (%lu)", tiny_data, length[0]);
 
1694
      fprintf(stdout, "\n\t short  : %d (%lu)", small_data, length[3]);
 
1695
      fprintf(stdout, "\n\t int    : %d (%lu)", int_data, length[2]);
 
1696
      fprintf(stdout, "\n\t big    : %s (%lu)", llstr(big_data, llbuf),
 
1697
              length[4]);
 
1698
 
 
1699
      fprintf(stdout, "\n\t float  : %f (%lu)", real_data, length[5]);
 
1700
      fprintf(stdout, "\n\t double : %f (%lu)", double_data, length[6]);
 
1701
 
 
1702
      fprintf(stdout, "\n\t str    : %s (%lu)", str_data, length[1]);
 
1703
    }
 
1704
 
 
1705
    DIE_UNLESS(tiny_data == o_tiny_data);
 
1706
    DIE_UNLESS(is_null[0] == 0);
 
1707
    DIE_UNLESS(length[0] == 1);
 
1708
 
 
1709
    DIE_UNLESS(int_data == o_int_data);
 
1710
    DIE_UNLESS(length[2] == 4);
 
1711
 
 
1712
    DIE_UNLESS(small_data == o_small_data);
 
1713
    DIE_UNLESS(length[3] == 2);
 
1714
 
 
1715
    DIE_UNLESS(big_data == o_big_data);
 
1716
    DIE_UNLESS(length[4] == 8);
 
1717
 
 
1718
    DIE_UNLESS(real_data == o_real_data);
 
1719
    DIE_UNLESS(length[5] == 4);
 
1720
 
 
1721
    DIE_UNLESS(cmp_double(&double_data, &o_double_data));
 
1722
    DIE_UNLESS(length[6] == 8);
 
1723
 
 
1724
    DIE_UNLESS(strcmp(data, str_data) == 0);
 
1725
    DIE_UNLESS(length[1] == len);
 
1726
 
 
1727
    o_int_data += 25;
 
1728
    o_small_data += 10;
 
1729
    o_big_data += 100;
 
1730
    o_real_data += 1;
 
1731
    o_double_data += 10.09;
 
1732
  }
 
1733
 
 
1734
  rc= mysql_stmt_fetch(stmt);
 
1735
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
1736
 
 
1737
  mysql_stmt_close(stmt);
 
1738
 
 
1739
}
 
1740
 
 
1741
 
 
1742
/* Test double comparision */
 
1743
 
 
1744
static void test_double_compare()
 
1745
{
 
1746
  MYSQL_STMT *stmt;
 
1747
  int        rc;
 
1748
  char       real_data[10], tiny_data;
 
1749
  double     double_data;
 
1750
  MYSQL_RES  *result;
 
1751
  MYSQL_BIND my_bind[3];
 
1752
  ulong      length[3];
 
1753
  char query[MAX_TEST_QUERY_LENGTH];
 
1754
 
 
1755
  myheader("test_double_compare");
 
1756
 
 
1757
  rc= mysql_autocommit(mysql, TRUE);
 
1758
  myquery(rc);
 
1759
 
 
1760
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_double_compare");
 
1761
  myquery(rc);
 
1762
 
 
1763
  rc= mysql_query(mysql, "CREATE TABLE test_double_compare(col1 tinyint, "
 
1764
                         " col2 float, col3 double )");
 
1765
  myquery(rc);
 
1766
 
 
1767
  rc= mysql_query(mysql, "INSERT INTO test_double_compare "
 
1768
                         "VALUES (1, 10.2, 34.5)");
 
1769
  myquery(rc);
 
1770
 
 
1771
  strmov(query, "UPDATE test_double_compare SET col1=100 "
 
1772
                "WHERE col1 = ? AND col2 = ? AND COL3 = ?");
 
1773
  stmt= mysql_simple_prepare(mysql, query);
 
1774
  check_stmt(stmt);
 
1775
 
 
1776
  verify_param_count(stmt, 3);
 
1777
 
 
1778
  /* Always bzero bind array because there can be internal members */
 
1779
  bzero((char*) my_bind, sizeof(my_bind));
 
1780
 
 
1781
  /* tinyint */
 
1782
  my_bind[0].buffer_type= MYSQL_TYPE_TINY;
 
1783
  my_bind[0].buffer= (void *)&tiny_data;
 
1784
 
 
1785
  /* string->float */
 
1786
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
1787
  my_bind[1].buffer= (void *)&real_data;
 
1788
  my_bind[1].buffer_length= sizeof(real_data);
 
1789
  my_bind[1].length= &length[1];
 
1790
  length[1]= 10;
 
1791
 
 
1792
  /* double */
 
1793
  my_bind[2].buffer_type= MYSQL_TYPE_DOUBLE;
 
1794
  my_bind[2].buffer= (void *)&double_data;
 
1795
 
 
1796
  tiny_data= 1;
 
1797
  strmov(real_data, "10.2");
 
1798
  double_data= 34.5;
 
1799
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
1800
  check_execute(stmt, rc);
 
1801
 
 
1802
  rc= mysql_stmt_execute(stmt);
 
1803
  check_execute(stmt, rc);
 
1804
 
 
1805
  verify_affected_rows(0);
 
1806
 
 
1807
  mysql_stmt_close(stmt);
 
1808
 
 
1809
  /* now fetch the results ..*/
 
1810
  rc= mysql_commit(mysql);
 
1811
  myquery(rc);
 
1812
 
 
1813
  /* test the results now, only one row should exist */
 
1814
  rc= mysql_query(mysql, "SELECT * FROM test_double_compare");
 
1815
  myquery(rc);
 
1816
 
 
1817
  /* get the result */
 
1818
  result= mysql_store_result(mysql);
 
1819
  mytest(result);
 
1820
 
 
1821
  rc= my_process_result_set(result);
 
1822
  DIE_UNLESS((int)tiny_data == rc);
 
1823
  mysql_free_result(result);
 
1824
}
 
1825
 
 
1826
 
 
1827
/* Test simple null */
 
1828
 
 
1829
static void test_null()
 
1830
{
 
1831
  MYSQL_STMT *stmt;
 
1832
  int        rc;
 
1833
  uint       nData;
 
1834
  MYSQL_BIND my_bind[2];
 
1835
  my_bool    is_null[2];
 
1836
  char query[MAX_TEST_QUERY_LENGTH];
 
1837
 
 
1838
  myheader("test_null");
 
1839
 
 
1840
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_null");
 
1841
  myquery(rc);
 
1842
 
 
1843
  rc= mysql_query(mysql, "CREATE TABLE test_null(col1 int, col2 varchar(50))");
 
1844
  myquery(rc);
 
1845
 
 
1846
  /* insert by prepare, wrong column name */
 
1847
  strmov(query, "INSERT INTO test_null(col3, col2) VALUES(?, ?)");
 
1848
  stmt= mysql_simple_prepare(mysql, query);
 
1849
  check_stmt_r(stmt);
 
1850
 
 
1851
  strmov(query, "INSERT INTO test_null(col1, col2) VALUES(?, ?)");
 
1852
  stmt= mysql_simple_prepare(mysql, query);
 
1853
  check_stmt(stmt);
 
1854
 
 
1855
  verify_param_count(stmt, 2);
 
1856
 
 
1857
  /* Always bzero all members of bind parameter */
 
1858
  bzero((char*) my_bind, sizeof(my_bind));
 
1859
 
 
1860
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
1861
  my_bind[0].is_null= &is_null[0];
 
1862
  is_null[0]= 1;
 
1863
  my_bind[1]= my_bind[0];
 
1864
 
 
1865
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
1866
  check_execute(stmt, rc);
 
1867
 
 
1868
  /* now, execute the prepared statement to insert 10 records.. */
 
1869
  for (nData= 0; nData<10; nData++)
 
1870
  {
 
1871
    rc= mysql_stmt_execute(stmt);
 
1872
    check_execute(stmt, rc);
 
1873
  }
 
1874
 
 
1875
  /* Re-bind with MYSQL_TYPE_NULL */
 
1876
  my_bind[0].buffer_type= MYSQL_TYPE_NULL;
 
1877
  is_null[0]= 0; /* reset */
 
1878
  my_bind[1]= my_bind[0];
 
1879
 
 
1880
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
1881
  check_execute(stmt, rc);
 
1882
 
 
1883
  for (nData= 0; nData<10; nData++)
 
1884
  {
 
1885
    rc= mysql_stmt_execute(stmt);
 
1886
    check_execute(stmt, rc);
 
1887
  }
 
1888
 
 
1889
  mysql_stmt_close(stmt);
 
1890
 
 
1891
  /* now fetch the results ..*/
 
1892
  rc= mysql_commit(mysql);
 
1893
  myquery(rc);
 
1894
 
 
1895
  nData*= 2;
 
1896
  rc= my_stmt_result("SELECT * FROM test_null");;
 
1897
  DIE_UNLESS((int) nData == rc);
 
1898
 
 
1899
  /* Fetch results */
 
1900
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
1901
  my_bind[0].buffer= (void *)&nData; /* this buffer won't be altered */
 
1902
  my_bind[0].length= 0;
 
1903
  my_bind[1]= my_bind[0];
 
1904
  my_bind[0].is_null= &is_null[0];
 
1905
  my_bind[1].is_null= &is_null[1];
 
1906
 
 
1907
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_null");
 
1908
  check_stmt(stmt);
 
1909
 
 
1910
  rc= mysql_stmt_execute(stmt);
 
1911
  check_execute(stmt, rc);
 
1912
 
 
1913
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
1914
  check_execute(stmt, rc);
 
1915
 
 
1916
  rc= 0;
 
1917
  is_null[0]= is_null[1]= 0;
 
1918
  while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
 
1919
  {
 
1920
    DIE_UNLESS(is_null[0]);
 
1921
    DIE_UNLESS(is_null[1]);
 
1922
    rc++;
 
1923
    is_null[0]= is_null[1]= 0;
 
1924
  }
 
1925
  DIE_UNLESS(rc == (int) nData);
 
1926
  mysql_stmt_close(stmt);
 
1927
}
 
1928
 
 
1929
 
 
1930
/* Test for NULL as PS parameter (BUG#3367, BUG#3371) */
 
1931
 
 
1932
static void test_ps_null_param()
 
1933
{
 
1934
  MYSQL_STMT *stmt;
 
1935
  int        rc;
 
1936
 
 
1937
  MYSQL_BIND in_bind;
 
1938
  my_bool    in_is_null;
 
1939
  long int   in_long;
 
1940
 
 
1941
  MYSQL_BIND out_bind;
 
1942
  ulong      out_length;
 
1943
  my_bool    out_is_null;
 
1944
  char       out_str_data[20];
 
1945
 
 
1946
  const char *queries[]= {"select ?", "select ?+1",
 
1947
                    "select col1 from test_ps_nulls where col1 <=> ?",
 
1948
                    NULL
 
1949
                    };
 
1950
  const char **cur_query= queries;
 
1951
 
 
1952
  myheader("test_null_ps_param_in_result");
 
1953
 
 
1954
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ps_nulls");
 
1955
  myquery(rc);
 
1956
 
 
1957
  rc= mysql_query(mysql, "CREATE TABLE test_ps_nulls(col1 int)");
 
1958
  myquery(rc);
 
1959
 
 
1960
  rc= mysql_query(mysql, "INSERT INTO test_ps_nulls values (1), (null)");
 
1961
  myquery(rc);
 
1962
 
 
1963
  /* Always bzero all members of bind parameter */
 
1964
  bzero((char*) &in_bind, sizeof(in_bind));
 
1965
  bzero((char*) &out_bind, sizeof(out_bind));
 
1966
 
 
1967
  in_bind.buffer_type= MYSQL_TYPE_LONG;
 
1968
  in_bind.is_null= &in_is_null;
 
1969
  in_bind.length= 0;
 
1970
  in_bind.buffer= (void *)&in_long;
 
1971
  in_is_null= 1;
 
1972
  in_long= 1;
 
1973
 
 
1974
  out_bind.buffer_type= MYSQL_TYPE_STRING;
 
1975
  out_bind.is_null= &out_is_null;
 
1976
  out_bind.length= &out_length;
 
1977
  out_bind.buffer= out_str_data;
 
1978
  out_bind.buffer_length= array_elements(out_str_data);
 
1979
 
 
1980
  /* Execute several queries, all returning NULL in result. */
 
1981
  for(cur_query= queries; *cur_query; cur_query++)
 
1982
  {
 
1983
    char query[MAX_TEST_QUERY_LENGTH];
 
1984
    strmov(query, *cur_query);
 
1985
    stmt= mysql_simple_prepare(mysql, query);
 
1986
    check_stmt(stmt);
 
1987
    verify_param_count(stmt, 1);
 
1988
 
 
1989
    rc= mysql_stmt_bind_param(stmt, &in_bind);
 
1990
    check_execute(stmt, rc);
 
1991
    rc= mysql_stmt_bind_result(stmt, &out_bind);
 
1992
    check_execute(stmt, rc);
 
1993
    rc= mysql_stmt_execute(stmt);
 
1994
    check_execute(stmt, rc);
 
1995
    rc= mysql_stmt_fetch(stmt);
 
1996
    DIE_UNLESS(rc != MYSQL_NO_DATA);
 
1997
    DIE_UNLESS(out_is_null);
 
1998
    rc= mysql_stmt_fetch(stmt);
 
1999
    DIE_UNLESS(rc == MYSQL_NO_DATA);
 
2000
    mysql_stmt_close(stmt);
 
2001
  }
 
2002
}
 
2003
 
 
2004
 
 
2005
/* Test fetch null */
 
2006
 
 
2007
static void test_fetch_null()
 
2008
{
 
2009
  MYSQL_STMT *stmt;
 
2010
  int        rc;
 
2011
  int        i, nData;
 
2012
  MYSQL_BIND my_bind[11];
 
2013
  ulong      length[11];
 
2014
  my_bool    is_null[11];
 
2015
  char query[MAX_TEST_QUERY_LENGTH];
 
2016
 
 
2017
  myheader("test_fetch_null");
 
2018
 
 
2019
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_fetch_null");
 
2020
  myquery(rc);
 
2021
 
 
2022
  rc= mysql_query(mysql, "CREATE TABLE test_fetch_null("
 
2023
                         " col1 tinyint, col2 smallint, "
 
2024
                         " col3 int, col4 bigint, "
 
2025
                         " col5 float, col6 double, "
 
2026
                         " col7 date, col8 time, "
 
2027
                         " col9 varbinary(10), "
 
2028
                         " col10 varchar(50), "
 
2029
                         " col11 char(20))");
 
2030
  myquery(rc);
 
2031
 
 
2032
  rc= mysql_query(mysql, "INSERT INTO test_fetch_null (col11) "
 
2033
                         "VALUES (1000), (88), (389789)");
 
2034
  myquery(rc);
 
2035
 
 
2036
  rc= mysql_commit(mysql);
 
2037
  myquery(rc);
 
2038
 
 
2039
  /* fetch */
 
2040
  bzero((char*) my_bind, sizeof(my_bind));
 
2041
  for (i= 0; i < (int) array_elements(my_bind); i++)
 
2042
  {
 
2043
    my_bind[i].buffer_type= MYSQL_TYPE_LONG;
 
2044
    my_bind[i].is_null= &is_null[i];
 
2045
    my_bind[i].length= &length[i];
 
2046
  }
 
2047
  my_bind[i-1].buffer= (void *)&nData;              /* Last column is not null */
 
2048
 
 
2049
  strmov((char *)query , "SELECT * FROM test_fetch_null");
 
2050
 
 
2051
  rc= my_stmt_result(query);
 
2052
  DIE_UNLESS(rc == 3);
 
2053
 
 
2054
  stmt= mysql_simple_prepare(mysql, query);
 
2055
  check_stmt(stmt);
 
2056
 
 
2057
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
2058
  check_execute(stmt, rc);
 
2059
 
 
2060
  rc= mysql_stmt_execute(stmt);
 
2061
  check_execute(stmt, rc);
 
2062
 
 
2063
  rc= 0;
 
2064
  while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
 
2065
  {
 
2066
    rc++;
 
2067
    for (i= 0; i < 10; i++)
 
2068
    {
 
2069
      if (!opt_silent)
 
2070
        fprintf(stdout, "\n data[%d] : %s", i,
 
2071
                is_null[i] ? "NULL" : "NOT NULL");
 
2072
      DIE_UNLESS(is_null[i]);
 
2073
    }
 
2074
    if (!opt_silent)
 
2075
      fprintf(stdout, "\n data[%d]: %d", i, nData);
 
2076
    DIE_UNLESS(nData == 1000 || nData == 88 || nData == 389789);
 
2077
    DIE_UNLESS(is_null[i] == 0);
 
2078
    DIE_UNLESS(length[i] == 4);
 
2079
  }
 
2080
  DIE_UNLESS(rc == 3);
 
2081
  mysql_stmt_close(stmt);
 
2082
}
 
2083
 
 
2084
 
 
2085
/* Test simple select */
 
2086
 
 
2087
static void test_select_version()
 
2088
{
 
2089
  MYSQL_STMT *stmt;
 
2090
  int        rc;
 
2091
 
 
2092
  myheader("test_select_version");
 
2093
 
 
2094
  stmt= mysql_simple_prepare(mysql, "SELECT @@version");
 
2095
  check_stmt(stmt);
 
2096
 
 
2097
  verify_param_count(stmt, 0);
 
2098
 
 
2099
  rc= mysql_stmt_execute(stmt);
 
2100
  check_execute(stmt, rc);
 
2101
 
 
2102
  my_process_stmt_result(stmt);
 
2103
  mysql_stmt_close(stmt);
 
2104
}
 
2105
 
 
2106
 
 
2107
/* Test simple show */
 
2108
 
 
2109
static void test_select_show_table()
 
2110
{
 
2111
  MYSQL_STMT *stmt;
 
2112
  int        rc, i;
 
2113
 
 
2114
  myheader("test_select_show_table");
 
2115
 
 
2116
  stmt= mysql_simple_prepare(mysql, "SHOW TABLES FROM mysql");
 
2117
  check_stmt(stmt);
 
2118
 
 
2119
  verify_param_count(stmt, 0);
 
2120
 
 
2121
  for (i= 1; i < 3; i++)
 
2122
  {
 
2123
    rc= mysql_stmt_execute(stmt);
 
2124
    check_execute(stmt, rc);
 
2125
  }
 
2126
 
 
2127
  my_process_stmt_result(stmt);
 
2128
  mysql_stmt_close(stmt);
 
2129
}
 
2130
 
 
2131
 
 
2132
/* Test simple select to debug */
 
2133
 
 
2134
static void test_select_direct()
 
2135
{
 
2136
  int        rc;
 
2137
  MYSQL_RES  *result;
 
2138
 
 
2139
  myheader("test_select_direct");
 
2140
 
 
2141
  rc= mysql_autocommit(mysql, TRUE);
 
2142
  myquery(rc);
 
2143
 
 
2144
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
 
2145
  myquery(rc);
 
2146
 
 
2147
  rc= mysql_query(mysql, "CREATE TABLE test_select(id int, id1 tinyint, "
 
2148
                                                 " id2 float, "
 
2149
                                                 " id3 double, "
 
2150
                                                 " name varchar(50))");
 
2151
  myquery(rc);
 
2152
 
 
2153
  /* insert a row and commit the transaction */
 
2154
  rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 5, 2.3, 4.5, 'venu')");
 
2155
  myquery(rc);
 
2156
 
 
2157
  rc= mysql_commit(mysql);
 
2158
  myquery(rc);
 
2159
 
 
2160
  rc= mysql_query(mysql, "SELECT * FROM test_select");
 
2161
  myquery(rc);
 
2162
 
 
2163
  /* get the result */
 
2164
  result= mysql_store_result(mysql);
 
2165
  mytest(result);
 
2166
 
 
2167
  (void) my_process_result_set(result);
 
2168
  mysql_free_result(result);
 
2169
}
 
2170
 
 
2171
 
 
2172
/* Test simple select with prepare */
 
2173
 
 
2174
static void test_select_prepare()
 
2175
{
 
2176
  int        rc;
 
2177
  MYSQL_STMT *stmt;
 
2178
 
 
2179
  myheader("test_select_prepare");
 
2180
 
 
2181
  rc= mysql_autocommit(mysql, TRUE);
 
2182
  myquery(rc);
 
2183
 
 
2184
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
 
2185
  myquery(rc);
 
2186
 
 
2187
  rc= mysql_query(mysql, "CREATE TABLE test_select(id int, name varchar(50))");
 
2188
  myquery(rc);
 
2189
 
 
2190
  /* insert a row and commit the transaction */
 
2191
  rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 'venu')");
 
2192
  myquery(rc);
 
2193
 
 
2194
  rc= mysql_commit(mysql);
 
2195
  myquery(rc);
 
2196
 
 
2197
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_select");
 
2198
  check_stmt(stmt);
 
2199
 
 
2200
  rc= mysql_stmt_execute(stmt);
 
2201
  check_execute(stmt, rc);
 
2202
 
 
2203
  rc= my_process_stmt_result(stmt);
 
2204
  DIE_UNLESS(rc == 1);
 
2205
  mysql_stmt_close(stmt);
 
2206
 
 
2207
  rc= mysql_query(mysql, "DROP TABLE test_select");
 
2208
  myquery(rc);
 
2209
 
 
2210
  rc= mysql_query(mysql, "CREATE TABLE test_select(id tinyint, id1 int, "
 
2211
                                                "  id2 float, id3 float, "
 
2212
                                                "  name varchar(50))");
 
2213
  myquery(rc);
 
2214
 
 
2215
  /* insert a row and commit the transaction */
 
2216
  rc= mysql_query(mysql, "INSERT INTO test_select(id, id1, id2, name) VALUES(10, 5, 2.3, 'venu')");
 
2217
  myquery(rc);
 
2218
 
 
2219
  rc= mysql_commit(mysql);
 
2220
  myquery(rc);
 
2221
 
 
2222
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_select");
 
2223
  check_stmt(stmt);
 
2224
 
 
2225
  rc= mysql_stmt_execute(stmt);
 
2226
  check_execute(stmt, rc);
 
2227
 
 
2228
  rc= my_process_stmt_result(stmt);
 
2229
  DIE_UNLESS(rc == 1);
 
2230
  mysql_stmt_close(stmt);
 
2231
}
 
2232
 
 
2233
 
 
2234
/* Test simple select */
 
2235
 
 
2236
static void test_select()
 
2237
{
 
2238
  MYSQL_STMT *stmt;
 
2239
  int        rc;
 
2240
  char       szData[25];
 
2241
  int        nData= 1;
 
2242
  MYSQL_BIND my_bind[2];
 
2243
  ulong length[2];
 
2244
  char query[MAX_TEST_QUERY_LENGTH];
 
2245
 
 
2246
  myheader("test_select");
 
2247
 
 
2248
  rc= mysql_autocommit(mysql, TRUE);
 
2249
  myquery(rc);
 
2250
 
 
2251
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
 
2252
  myquery(rc);
 
2253
 
 
2254
  rc= mysql_query(mysql, "CREATE TABLE test_select(id int, name varchar(50))");
 
2255
  myquery(rc);
 
2256
 
 
2257
  /* insert a row and commit the transaction */
 
2258
  rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 'venu')");
 
2259
  myquery(rc);
 
2260
 
 
2261
  /* now insert the second row, and roll back the transaction */
 
2262
  rc= mysql_query(mysql, "INSERT INTO test_select VALUES(20, 'mysql')");
 
2263
  myquery(rc);
 
2264
 
 
2265
  rc= mysql_commit(mysql);
 
2266
  myquery(rc);
 
2267
 
 
2268
  strmov(query, "SELECT * FROM test_select WHERE id= ? "
 
2269
                "AND CONVERT(name USING utf8) =?");
 
2270
  stmt= mysql_simple_prepare(mysql, query);
 
2271
  check_stmt(stmt);
 
2272
 
 
2273
  verify_param_count(stmt, 2);
 
2274
 
 
2275
  /* Always bzero all members of bind parameter */
 
2276
  bzero((char*) my_bind, sizeof(my_bind));
 
2277
 
 
2278
  /* string data */
 
2279
  nData= 10;
 
2280
  strmov(szData, (char *)"venu");
 
2281
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
2282
  my_bind[1].buffer= (void *)szData;
 
2283
  my_bind[1].buffer_length= 4;
 
2284
  my_bind[1].length= &length[1];
 
2285
  length[1]= 4;
 
2286
 
 
2287
  my_bind[0].buffer= (void *)&nData;
 
2288
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
2289
 
 
2290
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2291
  check_execute(stmt, rc);
 
2292
 
 
2293
  rc= mysql_stmt_execute(stmt);
 
2294
  check_execute(stmt, rc);
 
2295
 
 
2296
  rc= my_process_stmt_result(stmt);
 
2297
  DIE_UNLESS(rc == 1);
 
2298
 
 
2299
  mysql_stmt_close(stmt);
 
2300
}
 
2301
 
 
2302
 
 
2303
/*
 
2304
  Test for BUG#3420 ("select id1, value1 from t where id= ? or value= ?"
 
2305
  returns all rows in the table)
 
2306
*/
 
2307
 
 
2308
static void test_ps_conj_select()
 
2309
{
 
2310
  MYSQL_STMT *stmt;
 
2311
  int        rc;
 
2312
  MYSQL_BIND my_bind[2];
 
2313
  int32      int_data;
 
2314
  char       str_data[32];
 
2315
  unsigned long str_length;
 
2316
  char query[MAX_TEST_QUERY_LENGTH];
 
2317
  myheader("test_ps_conj_select");
 
2318
 
 
2319
  rc= mysql_query(mysql, "drop table if exists t1");
 
2320
  myquery(rc);
 
2321
 
 
2322
  rc= mysql_query(mysql, "create table t1 (id1 int(11) NOT NULL default '0', "
 
2323
                         "value2 varchar(100), value1 varchar(100))");
 
2324
  myquery(rc);
 
2325
 
 
2326
  rc= mysql_query(mysql, "insert into t1 values (1, 'hh', 'hh'), "
 
2327
                          "(2, 'hh', 'hh'), (1, 'ii', 'ii'), (2, 'ii', 'ii')");
 
2328
  myquery(rc);
 
2329
 
 
2330
  strmov(query, "select id1, value1 from t1 where id1= ? or "
 
2331
                "CONVERT(value1 USING utf8)= ?");
 
2332
  stmt= mysql_simple_prepare(mysql, query);
 
2333
  check_stmt(stmt);
 
2334
 
 
2335
  verify_param_count(stmt, 2);
 
2336
 
 
2337
  /* Always bzero all members of bind parameter */
 
2338
  bzero((char*) my_bind, sizeof(my_bind));
 
2339
 
 
2340
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
2341
  my_bind[0].buffer= (void *)&int_data;
 
2342
 
 
2343
  my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
 
2344
  my_bind[1].buffer= (void *)str_data;
 
2345
  my_bind[1].buffer_length= array_elements(str_data);
 
2346
  my_bind[1].length= &str_length;
 
2347
 
 
2348
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2349
  check_execute(stmt, rc);
 
2350
 
 
2351
  int_data= 1;
 
2352
  strmov(str_data, "hh");
 
2353
  str_length= strlen(str_data);
 
2354
 
 
2355
  rc= mysql_stmt_execute(stmt);
 
2356
  check_execute(stmt, rc);
 
2357
 
 
2358
  rc= my_process_stmt_result(stmt);
 
2359
  DIE_UNLESS(rc == 3);
 
2360
 
 
2361
  mysql_stmt_close(stmt);
 
2362
}
 
2363
 
 
2364
 
 
2365
/* reads Qcache_hits from server and returns its value */
 
2366
static uint query_cache_hits(MYSQL *conn)
 
2367
{
 
2368
  MYSQL_RES *res;
 
2369
  MYSQL_ROW row;
 
2370
  int rc;
 
2371
  uint result;
 
2372
 
 
2373
  rc= mysql_query(conn, "show status like 'qcache_hits'");
 
2374
  myquery(rc);
 
2375
  res= mysql_use_result(conn);
 
2376
  DIE_UNLESS(res);
 
2377
 
 
2378
  row= mysql_fetch_row(res);
 
2379
  DIE_UNLESS(row);
 
2380
 
 
2381
  result= atoi(row[1]);
 
2382
  mysql_free_result(res);
 
2383
  return result;
 
2384
}
 
2385
 
 
2386
 
 
2387
/*
 
2388
  utility for the next test; expects 3 rows in the result from a SELECT,
 
2389
  compares each row/field with an expected value.
 
2390
 */
 
2391
#define test_ps_query_cache_result(i1,s1,l1,i2,s2,l2,i3,s3,l3)    \
 
2392
  r_metadata= mysql_stmt_result_metadata(stmt);                   \
 
2393
  DIE_UNLESS(r_metadata != NULL);                                 \
 
2394
  rc= mysql_stmt_fetch(stmt);                                     \
 
2395
  check_execute(stmt, rc);                                        \
 
2396
  if (!opt_silent)                                                \
 
2397
    fprintf(stdout, "\n row 1: %d, %s(%lu)", r_int_data,          \
 
2398
            r_str_data, r_str_length);                            \
 
2399
  DIE_UNLESS((r_int_data == i1) && (r_str_length == l1) &&        \
 
2400
             (strcmp(r_str_data, s1) == 0));                      \
 
2401
  rc= mysql_stmt_fetch(stmt);                                     \
 
2402
  check_execute(stmt, rc);                                        \
 
2403
  if (!opt_silent)                                                \
 
2404
    fprintf(stdout, "\n row 2: %d, %s(%lu)", r_int_data,          \
 
2405
            r_str_data, r_str_length);                            \
 
2406
  DIE_UNLESS((r_int_data == i2) && (r_str_length == l2) &&        \
 
2407
             (strcmp(r_str_data, s2) == 0));                      \
 
2408
  rc= mysql_stmt_fetch(stmt);                                     \
 
2409
  check_execute(stmt, rc);                                        \
 
2410
  if (!opt_silent)                                                \
 
2411
    fprintf(stdout, "\n row 3: %d, %s(%lu)", r_int_data,          \
 
2412
            r_str_data, r_str_length);                            \
 
2413
  DIE_UNLESS((r_int_data == i3) && (r_str_length == l3) &&        \
 
2414
             (strcmp(r_str_data, s3) == 0));                      \
 
2415
  rc= mysql_stmt_fetch(stmt);                                     \
 
2416
  DIE_UNLESS(rc == MYSQL_NO_DATA);                                \
 
2417
  mysql_free_result(r_metadata);
 
2418
 
 
2419
 
 
2420
/*
 
2421
  Test that prepared statements make use of the query cache just as normal
 
2422
  statements (BUG#735).
 
2423
*/
 
2424
static void test_ps_query_cache()
 
2425
{
 
2426
  MYSQL      *lmysql= mysql;
 
2427
  MYSQL_STMT *stmt;
 
2428
  int        rc;
 
2429
  MYSQL_BIND p_bind[2],r_bind[2]; /* p: param bind; r: result bind */
 
2430
  int32      p_int_data, r_int_data;
 
2431
  char       p_str_data[32], r_str_data[32];
 
2432
  unsigned long p_str_length, r_str_length;
 
2433
  MYSQL_RES  *r_metadata;
 
2434
  char       query[MAX_TEST_QUERY_LENGTH];
 
2435
  uint       hits1, hits2;
 
2436
  enum enum_test_ps_query_cache
 
2437
  {
 
2438
    /*
 
2439
      We iterate the same prepare/executes block, but have iterations where
 
2440
      we vary the query cache conditions.
 
2441
    */
 
2442
    /* the query cache is enabled for the duration of prep&execs: */
 
2443
    TEST_QCACHE_ON= 0,
 
2444
    /*
 
2445
      same but using a new connection (to see if qcache serves results from
 
2446
      the previous connection as it should):
 
2447
    */
 
2448
    TEST_QCACHE_ON_WITH_OTHER_CONN,
 
2449
    /*
 
2450
      First border case: disables the query cache before prepare and
 
2451
      re-enables it before execution (to test if we have no bug then):
 
2452
    */
 
2453
    TEST_QCACHE_OFF_ON,
 
2454
    /*
 
2455
      Second border case: enables the query cache before prepare and
 
2456
      disables it before execution:
 
2457
    */
 
2458
    TEST_QCACHE_ON_OFF
 
2459
  };
 
2460
  enum enum_test_ps_query_cache iteration;
 
2461
 
 
2462
  myheader("test_ps_query_cache");
 
2463
 
 
2464
  /* prepare the table */
 
2465
 
 
2466
  rc= mysql_query(mysql, "drop table if exists t1");
 
2467
  myquery(rc);
 
2468
 
 
2469
  rc= mysql_query(mysql, "create table t1 (id1 int(11) NOT NULL default '0', "
 
2470
                         "value2 varchar(100), value1 varchar(100))");
 
2471
  myquery(rc);
 
2472
 
 
2473
  rc= mysql_query(mysql, "insert into t1 values (1, 'hh', 'hh'), "
 
2474
                          "(2, 'hh', 'hh'), (1, 'ii', 'ii'), (2, 'ii', 'ii')");
 
2475
  myquery(rc);
 
2476
 
 
2477
  for (iteration= TEST_QCACHE_ON; iteration <= TEST_QCACHE_ON_OFF; iteration++)
 
2478
  {
 
2479
 
 
2480
    switch (iteration) {
 
2481
    case TEST_QCACHE_ON:
 
2482
    case TEST_QCACHE_ON_OFF:
 
2483
      rc= mysql_query(lmysql, "set global query_cache_size=1000000");
 
2484
      myquery(rc);
 
2485
      break;
 
2486
    case TEST_QCACHE_OFF_ON:
 
2487
      rc= mysql_query(lmysql, "set global query_cache_size=0");
 
2488
      myquery(rc);
 
2489
      break;
 
2490
    case TEST_QCACHE_ON_WITH_OTHER_CONN:
 
2491
      if (!opt_silent)
 
2492
        fprintf(stdout, "\n Establishing a test connection ...");
 
2493
      if (!(lmysql= mysql_init(NULL)))
 
2494
      {
 
2495
        printf("mysql_init() failed");
 
2496
        DIE_UNLESS(0);
 
2497
      }
 
2498
      if (!(mysql_real_connect(lmysql, opt_host, opt_user,
 
2499
                               opt_password, current_db, opt_port,
 
2500
                               opt_unix_socket, 0)))
 
2501
      {
 
2502
        printf("connection failed");
 
2503
        mysql_close(lmysql);
 
2504
        DIE_UNLESS(0);
 
2505
      }
 
2506
      if (!opt_silent)
 
2507
        fprintf(stdout, "OK");
 
2508
    }
 
2509
 
 
2510
    strmov(query, "select id1, value1 from t1 where id1= ? or "
 
2511
           "CONVERT(value1 USING utf8)= ?");
 
2512
    stmt= mysql_simple_prepare(lmysql, query);
 
2513
    check_stmt(stmt);
 
2514
 
 
2515
    verify_param_count(stmt, 2);
 
2516
 
 
2517
    switch (iteration) {
 
2518
    case TEST_QCACHE_OFF_ON:
 
2519
      rc= mysql_query(lmysql, "set global query_cache_size=1000000");
 
2520
      myquery(rc);
 
2521
      break;
 
2522
    case TEST_QCACHE_ON_OFF:
 
2523
      rc= mysql_query(lmysql, "set global query_cache_size=0");
 
2524
      myquery(rc);
 
2525
    default:
 
2526
      break;
 
2527
    }
 
2528
 
 
2529
    bzero((char*) p_bind, sizeof(p_bind));
 
2530
    p_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
2531
    p_bind[0].buffer= (void *)&p_int_data;
 
2532
    p_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
 
2533
    p_bind[1].buffer= (void *)p_str_data;
 
2534
    p_bind[1].buffer_length= array_elements(p_str_data);
 
2535
    p_bind[1].length= &p_str_length;
 
2536
 
 
2537
    rc= mysql_stmt_bind_param(stmt, p_bind);
 
2538
    check_execute(stmt, rc);
 
2539
 
 
2540
    p_int_data= 1;
 
2541
    strmov(p_str_data, "hh");
 
2542
    p_str_length= strlen(p_str_data);
 
2543
 
 
2544
    bzero((char*) r_bind, sizeof(r_bind));
 
2545
    r_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
2546
    r_bind[0].buffer= (void *)&r_int_data;
 
2547
    r_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
 
2548
    r_bind[1].buffer= (void *)r_str_data;
 
2549
    r_bind[1].buffer_length= array_elements(r_str_data);
 
2550
    r_bind[1].length= &r_str_length;
 
2551
 
 
2552
    rc= mysql_stmt_bind_result(stmt, r_bind);
 
2553
    check_execute(stmt, rc);
 
2554
 
 
2555
    rc= mysql_stmt_execute(stmt);
 
2556
    check_execute(stmt, rc);
 
2557
 
 
2558
    test_ps_query_cache_result(1, "hh", 2, 2, "hh", 2, 1, "ii", 2);
 
2559
 
 
2560
    /* now retry with the same parameter values and see qcache hits */
 
2561
    hits1= query_cache_hits(lmysql);
 
2562
    rc= mysql_stmt_execute(stmt);
 
2563
    check_execute(stmt, rc);
 
2564
    test_ps_query_cache_result(1, "hh", 2, 2, "hh", 2, 1, "ii", 2);
 
2565
    hits2= query_cache_hits(lmysql);
 
2566
    switch(iteration) {
 
2567
    case TEST_QCACHE_ON_WITH_OTHER_CONN:
 
2568
    case TEST_QCACHE_ON:                 /* should have hit */
 
2569
      DIE_UNLESS(hits2-hits1 == 1);
 
2570
      break;
 
2571
    case TEST_QCACHE_OFF_ON:
 
2572
    case TEST_QCACHE_ON_OFF:             /* should not have hit */
 
2573
      DIE_UNLESS(hits2-hits1 == 0);
 
2574
      break;
 
2575
    }
 
2576
 
 
2577
    /* now modify parameter values and see qcache hits */
 
2578
    strmov(p_str_data, "ii");
 
2579
    p_str_length= strlen(p_str_data);
 
2580
    rc= mysql_stmt_execute(stmt);
 
2581
    check_execute(stmt, rc);
 
2582
    test_ps_query_cache_result(1, "hh", 2, 1, "ii", 2, 2, "ii", 2);
 
2583
    hits1= query_cache_hits(lmysql);
 
2584
 
 
2585
    switch(iteration) {
 
2586
    case TEST_QCACHE_ON:
 
2587
    case TEST_QCACHE_OFF_ON:
 
2588
    case TEST_QCACHE_ON_OFF:             /* should not have hit */
 
2589
      DIE_UNLESS(hits2-hits1 == 0);
 
2590
      break;
 
2591
    case TEST_QCACHE_ON_WITH_OTHER_CONN: /* should have hit */
 
2592
      DIE_UNLESS(hits1-hits2 == 1);
 
2593
      break;
 
2594
    }
 
2595
 
 
2596
    rc= mysql_stmt_execute(stmt);
 
2597
    check_execute(stmt, rc);
 
2598
 
 
2599
    test_ps_query_cache_result(1, "hh", 2, 1, "ii", 2, 2, "ii", 2);
 
2600
    hits2= query_cache_hits(lmysql);
 
2601
 
 
2602
    mysql_stmt_close(stmt);
 
2603
 
 
2604
    switch(iteration) {
 
2605
    case TEST_QCACHE_ON:                 /* should have hit */
 
2606
      DIE_UNLESS(hits2-hits1 == 1);
 
2607
      break;
 
2608
    case TEST_QCACHE_OFF_ON:
 
2609
    case TEST_QCACHE_ON_OFF:             /* should not have hit */
 
2610
      DIE_UNLESS(hits2-hits1 == 0);
 
2611
      break;
 
2612
    case TEST_QCACHE_ON_WITH_OTHER_CONN: /* should have hit */
 
2613
      DIE_UNLESS(hits2-hits1 == 1);
 
2614
      break;
 
2615
    }
 
2616
 
 
2617
  } /* for(iteration=...) */
 
2618
 
 
2619
  if (lmysql != mysql)
 
2620
    mysql_close(lmysql);
 
2621
 
 
2622
  rc= mysql_query(mysql, "set global query_cache_size=0");
 
2623
  myquery(rc);
 
2624
}
 
2625
 
 
2626
 
 
2627
/* Test BUG#1115 (incorrect string parameter value allocation) */
 
2628
 
 
2629
static void test_bug1115()
 
2630
{
 
2631
  MYSQL_STMT *stmt;
 
2632
  int rc;
 
2633
  MYSQL_BIND my_bind[1];
 
2634
  ulong length[1];
 
2635
  char szData[11];
 
2636
  char query[MAX_TEST_QUERY_LENGTH];
 
2637
 
 
2638
  myheader("test_bug1115");
 
2639
 
 
2640
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
 
2641
  myquery(rc);
 
2642
 
 
2643
  rc= mysql_query(mysql, "CREATE TABLE test_select(\
 
2644
session_id  char(9) NOT NULL, \
 
2645
    a       int(8) unsigned NOT NULL, \
 
2646
    b        int(5) NOT NULL, \
 
2647
    c      int(5) NOT NULL, \
 
2648
    d  datetime NOT NULL)");
 
2649
  myquery(rc);
 
2650
  rc= mysql_query(mysql, "INSERT INTO test_select VALUES "
 
2651
                         "(\"abc\", 1, 2, 3, 2003-08-30), "
 
2652
                         "(\"abd\", 1, 2, 3, 2003-08-30), "
 
2653
                         "(\"abf\", 1, 2, 3, 2003-08-30), "
 
2654
                         "(\"abg\", 1, 2, 3, 2003-08-30), "
 
2655
                         "(\"abh\", 1, 2, 3, 2003-08-30), "
 
2656
                         "(\"abj\", 1, 2, 3, 2003-08-30), "
 
2657
                         "(\"abk\", 1, 2, 3, 2003-08-30), "
 
2658
                         "(\"abl\", 1, 2, 3, 2003-08-30), "
 
2659
                         "(\"abq\", 1, 2, 3, 2003-08-30) ");
 
2660
  myquery(rc);
 
2661
  rc= mysql_query(mysql, "INSERT INTO test_select VALUES "
 
2662
                         "(\"abw\", 1, 2, 3, 2003-08-30), "
 
2663
                         "(\"abe\", 1, 2, 3, 2003-08-30), "
 
2664
                         "(\"abr\", 1, 2, 3, 2003-08-30), "
 
2665
                         "(\"abt\", 1, 2, 3, 2003-08-30), "
 
2666
                         "(\"aby\", 1, 2, 3, 2003-08-30), "
 
2667
                         "(\"abu\", 1, 2, 3, 2003-08-30), "
 
2668
                         "(\"abi\", 1, 2, 3, 2003-08-30), "
 
2669
                         "(\"abo\", 1, 2, 3, 2003-08-30), "
 
2670
                         "(\"abp\", 1, 2, 3, 2003-08-30), "
 
2671
                         "(\"abz\", 1, 2, 3, 2003-08-30), "
 
2672
                         "(\"abx\", 1, 2, 3, 2003-08-30)");
 
2673
  myquery(rc);
 
2674
 
 
2675
  strmov(query, "SELECT * FROM test_select WHERE "
 
2676
                "CONVERT(session_id USING utf8)= ?");
 
2677
  stmt= mysql_simple_prepare(mysql, query);
 
2678
  check_stmt(stmt);
 
2679
 
 
2680
  verify_param_count(stmt, 1);
 
2681
 
 
2682
  /* Always bzero all members of bind parameter */
 
2683
  bzero((char*) my_bind, sizeof(my_bind));
 
2684
 
 
2685
  strmov(szData, (char *)"abc");
 
2686
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
2687
  my_bind[0].buffer= (void *)szData;
 
2688
  my_bind[0].buffer_length= 10;
 
2689
  my_bind[0].length= &length[0];
 
2690
  length[0]= 3;
 
2691
 
 
2692
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2693
  check_execute(stmt, rc);
 
2694
 
 
2695
  rc= mysql_stmt_execute(stmt);
 
2696
  check_execute(stmt, rc);
 
2697
 
 
2698
  rc= my_process_stmt_result(stmt);
 
2699
  DIE_UNLESS(rc == 1);
 
2700
 
 
2701
  strmov(szData, (char *)"venu");
 
2702
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
2703
  my_bind[0].buffer= (void *)szData;
 
2704
  my_bind[0].buffer_length= 10;
 
2705
  my_bind[0].length= &length[0];
 
2706
  length[0]= 4;
 
2707
  my_bind[0].is_null= 0;
 
2708
 
 
2709
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2710
  check_execute(stmt, rc);
 
2711
 
 
2712
  rc= mysql_stmt_execute(stmt);
 
2713
  check_execute(stmt, rc);
 
2714
 
 
2715
  rc= my_process_stmt_result(stmt);
 
2716
  DIE_UNLESS(rc == 0);
 
2717
 
 
2718
  strmov(szData, (char *)"abc");
 
2719
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
2720
  my_bind[0].buffer= (void *)szData;
 
2721
  my_bind[0].buffer_length= 10;
 
2722
  my_bind[0].length= &length[0];
 
2723
  length[0]= 3;
 
2724
  my_bind[0].is_null= 0;
 
2725
 
 
2726
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2727
  check_execute(stmt, rc);
 
2728
 
 
2729
  rc= mysql_stmt_execute(stmt);
 
2730
  check_execute(stmt, rc);
 
2731
 
 
2732
  rc= my_process_stmt_result(stmt);
 
2733
  DIE_UNLESS(rc == 1);
 
2734
 
 
2735
  mysql_stmt_close(stmt);
 
2736
}
 
2737
 
 
2738
 
 
2739
/* Test BUG#1180 (optimized away part of WHERE clause) */
 
2740
 
 
2741
static void test_bug1180()
 
2742
{
 
2743
  MYSQL_STMT *stmt;
 
2744
  int rc;
 
2745
  MYSQL_BIND my_bind[1];
 
2746
  ulong length[1];
 
2747
  char szData[11];
 
2748
  char query[MAX_TEST_QUERY_LENGTH];
 
2749
 
 
2750
  myheader("test_select_bug");
 
2751
 
 
2752
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
 
2753
  myquery(rc);
 
2754
 
 
2755
  rc= mysql_query(mysql, "CREATE TABLE test_select(session_id  char(9) NOT NULL)");
 
2756
  myquery(rc);
 
2757
  rc= mysql_query(mysql, "INSERT INTO test_select VALUES (\"abc\")");
 
2758
  myquery(rc);
 
2759
 
 
2760
  strmov(query, "SELECT * FROM test_select WHERE ?= \"1111\" and "
 
2761
                "session_id= \"abc\"");
 
2762
  stmt= mysql_simple_prepare(mysql, query);
 
2763
  check_stmt(stmt);
 
2764
 
 
2765
  verify_param_count(stmt, 1);
 
2766
 
 
2767
  /* Always bzero all members of bind parameter */
 
2768
  bzero((char*) my_bind, sizeof(my_bind));
 
2769
 
 
2770
  strmov(szData, (char *)"abc");
 
2771
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
2772
  my_bind[0].buffer= (void *)szData;
 
2773
  my_bind[0].buffer_length= 10;
 
2774
  my_bind[0].length= &length[0];
 
2775
  length[0]= 3;
 
2776
  my_bind[0].is_null= 0;
 
2777
 
 
2778
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2779
  check_execute(stmt, rc);
 
2780
 
 
2781
  rc= mysql_stmt_execute(stmt);
 
2782
  check_execute(stmt, rc);
 
2783
 
 
2784
  rc= my_process_stmt_result(stmt);
 
2785
  DIE_UNLESS(rc == 0);
 
2786
 
 
2787
  strmov(szData, (char *)"1111");
 
2788
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
2789
  my_bind[0].buffer= (void *)szData;
 
2790
  my_bind[0].buffer_length= 10;
 
2791
  my_bind[0].length= &length[0];
 
2792
  length[0]= 4;
 
2793
  my_bind[0].is_null= 0;
 
2794
 
 
2795
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2796
  check_execute(stmt, rc);
 
2797
 
 
2798
  rc= mysql_stmt_execute(stmt);
 
2799
  check_execute(stmt, rc);
 
2800
 
 
2801
  rc= my_process_stmt_result(stmt);
 
2802
  DIE_UNLESS(rc == 1);
 
2803
 
 
2804
  strmov(szData, (char *)"abc");
 
2805
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
2806
  my_bind[0].buffer= (void *)szData;
 
2807
  my_bind[0].buffer_length= 10;
 
2808
  my_bind[0].length= &length[0];
 
2809
  length[0]= 3;
 
2810
  my_bind[0].is_null= 0;
 
2811
 
 
2812
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2813
  check_execute(stmt, rc);
 
2814
 
 
2815
  rc= mysql_stmt_execute(stmt);
 
2816
  check_execute(stmt, rc);
 
2817
 
 
2818
  rc= my_process_stmt_result(stmt);
 
2819
  DIE_UNLESS(rc == 0);
 
2820
 
 
2821
  mysql_stmt_close(stmt);
 
2822
}
 
2823
 
 
2824
 
 
2825
/*
 
2826
  Test BUG#1644 (Insertion of more than 3 NULL columns with parameter
 
2827
  binding fails)
 
2828
*/
 
2829
 
 
2830
static void test_bug1644()
 
2831
{
 
2832
  MYSQL_STMT *stmt;
 
2833
  MYSQL_RES *result;
 
2834
  MYSQL_ROW row;
 
2835
  MYSQL_BIND my_bind[4];
 
2836
  int num;
 
2837
  my_bool isnull;
 
2838
  int rc, i;
 
2839
  char query[MAX_TEST_QUERY_LENGTH];
 
2840
 
 
2841
  myheader("test_bug1644");
 
2842
 
 
2843
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS foo_dfr");
 
2844
  myquery(rc);
 
2845
 
 
2846
  rc= mysql_query(mysql,
 
2847
           "CREATE TABLE foo_dfr(col1 int, col2 int, col3 int, col4 int);");
 
2848
  myquery(rc);
 
2849
 
 
2850
  strmov(query, "INSERT INTO foo_dfr VALUES (?, ?, ?, ? )");
 
2851
  stmt= mysql_simple_prepare(mysql, query);
 
2852
  check_stmt(stmt);
 
2853
 
 
2854
  verify_param_count(stmt, 4);
 
2855
 
 
2856
  /* Always bzero all members of bind parameter */
 
2857
  bzero((char*) my_bind, sizeof(my_bind));
 
2858
 
 
2859
  num= 22;
 
2860
  isnull= 0;
 
2861
  for (i= 0 ; i < 4 ; i++)
 
2862
  {
 
2863
    my_bind[i].buffer_type= MYSQL_TYPE_LONG;
 
2864
    my_bind[i].buffer= (void *)&num;
 
2865
    my_bind[i].is_null= &isnull;
 
2866
  }
 
2867
 
 
2868
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2869
  check_execute(stmt, rc);
 
2870
 
 
2871
  rc= mysql_stmt_execute(stmt);
 
2872
  check_execute(stmt, rc);
 
2873
 
 
2874
  isnull= 1;
 
2875
  for (i= 0 ; i < 4 ; i++)
 
2876
    my_bind[i].is_null= &isnull;
 
2877
 
 
2878
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2879
  check_execute(stmt, rc);
 
2880
 
 
2881
  rc= mysql_stmt_execute(stmt);
 
2882
  check_execute(stmt, rc);
 
2883
 
 
2884
  isnull= 0;
 
2885
  num= 88;
 
2886
  for (i= 0 ; i < 4 ; i++)
 
2887
    my_bind[i].is_null= &isnull;
 
2888
 
 
2889
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2890
  check_execute(stmt, rc);
 
2891
 
 
2892
  rc= mysql_stmt_execute(stmt);
 
2893
  check_execute(stmt, rc);
 
2894
 
 
2895
  mysql_stmt_close(stmt);
 
2896
 
 
2897
  rc= mysql_query(mysql, "SELECT * FROM foo_dfr");
 
2898
  myquery(rc);
 
2899
 
 
2900
  result= mysql_store_result(mysql);
 
2901
  mytest(result);
 
2902
 
 
2903
  rc= my_process_result_set(result);
 
2904
  DIE_UNLESS(rc == 3);
 
2905
 
 
2906
  mysql_data_seek(result, 0);
 
2907
 
 
2908
  row= mysql_fetch_row(result);
 
2909
  mytest(row);
 
2910
  for (i= 0 ; i < 4 ; i++)
 
2911
  {
 
2912
    DIE_UNLESS(strcmp(row[i], "22") == 0);
 
2913
  }
 
2914
  row= mysql_fetch_row(result);
 
2915
  mytest(row);
 
2916
  for (i= 0 ; i < 4 ; i++)
 
2917
  {
 
2918
    DIE_UNLESS(row[i] == 0);
 
2919
  }
 
2920
  row= mysql_fetch_row(result);
 
2921
  mytest(row);
 
2922
  for (i= 0 ; i < 4 ; i++)
 
2923
  {
 
2924
    DIE_UNLESS(strcmp(row[i], "88") == 0);
 
2925
  }
 
2926
  row= mysql_fetch_row(result);
 
2927
  mytest_r(row);
 
2928
 
 
2929
  mysql_free_result(result);
 
2930
}
 
2931
 
 
2932
 
 
2933
/* Test simple select show */
 
2934
 
 
2935
static void test_select_show()
 
2936
{
 
2937
  MYSQL_STMT *stmt;
 
2938
  int        rc;
 
2939
  char query[MAX_TEST_QUERY_LENGTH];
 
2940
 
 
2941
  myheader("test_select_show");
 
2942
 
 
2943
  mysql_autocommit(mysql, TRUE);
 
2944
 
 
2945
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_show");
 
2946
  myquery(rc);
 
2947
 
 
2948
  rc= mysql_query(mysql, "CREATE TABLE test_show(id int(4) NOT NULL primary "
 
2949
                         " key, name char(2))");
 
2950
  myquery(rc);
 
2951
 
 
2952
  stmt= mysql_simple_prepare(mysql, "show columns from test_show");
 
2953
  check_stmt(stmt);
 
2954
 
 
2955
  verify_param_count(stmt, 0);
 
2956
 
 
2957
  rc= mysql_stmt_execute(stmt);
 
2958
  check_execute(stmt, rc);
 
2959
 
 
2960
  my_process_stmt_result(stmt);
 
2961
  mysql_stmt_close(stmt);
 
2962
 
 
2963
  stmt= mysql_simple_prepare(mysql, "show tables from mysql like ?");
 
2964
  check_stmt_r(stmt);
 
2965
 
 
2966
  strxmov(query, "show tables from ", current_db, " like \'test_show\'", NullS);
 
2967
  stmt= mysql_simple_prepare(mysql, query);
 
2968
  check_stmt(stmt);
 
2969
 
 
2970
  rc= mysql_stmt_execute(stmt);
 
2971
  check_execute(stmt, rc);
 
2972
 
 
2973
  my_process_stmt_result(stmt);
 
2974
  mysql_stmt_close(stmt);
 
2975
 
 
2976
  stmt= mysql_simple_prepare(mysql, "describe test_show");
 
2977
  check_stmt(stmt);
 
2978
 
 
2979
  rc= mysql_stmt_execute(stmt);
 
2980
  check_execute(stmt, rc);
 
2981
 
 
2982
  my_process_stmt_result(stmt);
 
2983
  mysql_stmt_close(stmt);
 
2984
 
 
2985
  stmt= mysql_simple_prepare(mysql, "show keys from test_show");
 
2986
  check_stmt(stmt);
 
2987
 
 
2988
  rc= mysql_stmt_execute(stmt);
 
2989
  check_execute(stmt, rc);
 
2990
 
 
2991
  rc= my_process_stmt_result(stmt);
 
2992
  DIE_UNLESS(rc == 1);
 
2993
  mysql_stmt_close(stmt);
 
2994
}
 
2995
 
 
2996
 
 
2997
/* Test simple update */
 
2998
 
 
2999
static void test_simple_update()
 
3000
{
 
3001
  MYSQL_STMT *stmt;
 
3002
  int        rc;
 
3003
  char       szData[25];
 
3004
  int        nData= 1;
 
3005
  MYSQL_RES  *result;
 
3006
  MYSQL_BIND my_bind[2];
 
3007
  ulong      length[2];
 
3008
  char query[MAX_TEST_QUERY_LENGTH];
 
3009
 
 
3010
  myheader("test_simple_update");
 
3011
 
 
3012
  rc= mysql_autocommit(mysql, TRUE);
 
3013
  myquery(rc);
 
3014
 
 
3015
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_update");
 
3016
  myquery(rc);
 
3017
 
 
3018
  rc= mysql_query(mysql, "CREATE TABLE test_update(col1 int, "
 
3019
                         " col2 varchar(50), col3 int )");
 
3020
  myquery(rc);
 
3021
 
 
3022
  rc= mysql_query(mysql, "INSERT INTO test_update VALUES(1, 'MySQL', 100)");
 
3023
  myquery(rc);
 
3024
 
 
3025
  verify_affected_rows(1);
 
3026
 
 
3027
  rc= mysql_commit(mysql);
 
3028
  myquery(rc);
 
3029
 
 
3030
  /* insert by prepare */
 
3031
  strmov(query, "UPDATE test_update SET col2= ? WHERE col1= ?");
 
3032
  stmt= mysql_simple_prepare(mysql, query);
 
3033
  check_stmt(stmt);
 
3034
 
 
3035
  verify_param_count(stmt, 2);
 
3036
 
 
3037
  /* Always bzero all members of bind parameter */
 
3038
  bzero((char*) my_bind, sizeof(my_bind));
 
3039
 
 
3040
  nData= 1;
 
3041
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
3042
  my_bind[0].buffer= szData;                /* string data */
 
3043
  my_bind[0].buffer_length= sizeof(szData);
 
3044
  my_bind[0].length= &length[0];
 
3045
  length[0]= my_sprintf(szData, (szData, "updated-data"));
 
3046
 
 
3047
  my_bind[1].buffer= (void *) &nData;
 
3048
  my_bind[1].buffer_type= MYSQL_TYPE_LONG;
 
3049
 
 
3050
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
3051
  check_execute(stmt, rc);
 
3052
 
 
3053
  rc= mysql_stmt_execute(stmt);
 
3054
  check_execute(stmt, rc);
 
3055
  verify_affected_rows(1);
 
3056
 
 
3057
  mysql_stmt_close(stmt);
 
3058
 
 
3059
  /* now fetch the results ..*/
 
3060
  rc= mysql_commit(mysql);
 
3061
  myquery(rc);
 
3062
 
 
3063
  /* test the results now, only one row should exist */
 
3064
  rc= mysql_query(mysql, "SELECT * FROM test_update");
 
3065
  myquery(rc);
 
3066
 
 
3067
  /* get the result */
 
3068
  result= mysql_store_result(mysql);
 
3069
  mytest(result);
 
3070
 
 
3071
  rc= my_process_result_set(result);
 
3072
  DIE_UNLESS(rc == 1);
 
3073
  mysql_free_result(result);
 
3074
}
 
3075
 
 
3076
 
 
3077
/* Test simple long data handling */
 
3078
 
 
3079
static void test_long_data()
 
3080
{
 
3081
  MYSQL_STMT *stmt;
 
3082
  int        rc, int_data;
 
3083
  char       *data= NullS;
 
3084
  MYSQL_RES  *result;
 
3085
  MYSQL_BIND my_bind[3];
 
3086
  char query[MAX_TEST_QUERY_LENGTH];
 
3087
 
 
3088
  myheader("test_long_data");
 
3089
 
 
3090
  rc= mysql_autocommit(mysql, TRUE);
 
3091
  myquery(rc);
 
3092
 
 
3093
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data");
 
3094
  myquery(rc);
 
3095
 
 
3096
  rc= mysql_query(mysql, "CREATE TABLE test_long_data(col1 int, "
 
3097
                         "      col2 long varchar, col3 long varbinary)");
 
3098
  myquery(rc);
 
3099
 
 
3100
  strmov(query, "INSERT INTO test_long_data(col1, col2) VALUES(?)");
 
3101
  stmt= mysql_simple_prepare(mysql, query);
 
3102
  check_stmt_r(stmt);
 
3103
 
 
3104
  strmov(query, "INSERT INTO test_long_data(col1, col2, col3) VALUES(?, ?, ?)");
 
3105
  stmt= mysql_simple_prepare(mysql, query);
 
3106
  check_stmt(stmt);
 
3107
 
 
3108
  verify_param_count(stmt, 3);
 
3109
 
 
3110
  /* Always bzero all members of bind parameter */
 
3111
  bzero((char*) my_bind, sizeof(my_bind));
 
3112
 
 
3113
  my_bind[0].buffer= (void *)&int_data;
 
3114
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
3115
 
 
3116
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
3117
 
 
3118
  my_bind[2]= my_bind[1];
 
3119
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
3120
  check_execute(stmt, rc);
 
3121
 
 
3122
  int_data= 999;
 
3123
  data= (char *)"Michael";
 
3124
 
 
3125
  /* supply data in pieces */
 
3126
  rc= mysql_stmt_send_long_data(stmt, 1, data, strlen(data));
 
3127
  data= (char *)" 'Monty' Widenius";
 
3128
  rc= mysql_stmt_send_long_data(stmt, 1, data, strlen(data));
 
3129
  check_execute(stmt, rc);
 
3130
  rc= mysql_stmt_send_long_data(stmt, 2, "Venu (venu@mysql.com)", 4);
 
3131
  check_execute(stmt, rc);
 
3132
 
 
3133
  /* execute */
 
3134
  rc= mysql_stmt_execute(stmt);
 
3135
  if (!opt_silent)
 
3136
    fprintf(stdout, " mysql_stmt_execute() returned %d\n", rc);
 
3137
  check_execute(stmt, rc);
 
3138
 
 
3139
  rc= mysql_commit(mysql);
 
3140
  myquery(rc);
 
3141
 
 
3142
  /* now fetch the results ..*/
 
3143
  rc= mysql_query(mysql, "SELECT * FROM test_long_data");
 
3144
  myquery(rc);
 
3145
 
 
3146
  /* get the result */
 
3147
  result= mysql_store_result(mysql);
 
3148
  mytest(result);
 
3149
 
 
3150
  rc= my_process_result_set(result);
 
3151
  DIE_UNLESS(rc == 1);
 
3152
  mysql_free_result(result);
 
3153
 
 
3154
  verify_col_data("test_long_data", "col1", "999");
 
3155
  verify_col_data("test_long_data", "col2", "Michael 'Monty' Widenius");
 
3156
  verify_col_data("test_long_data", "col3", "Venu");
 
3157
  mysql_stmt_close(stmt);
 
3158
}
 
3159
 
 
3160
 
 
3161
/* Test long data (string) handling */
 
3162
 
 
3163
static void test_long_data_str()
 
3164
{
 
3165
  MYSQL_STMT *stmt;
 
3166
  int        rc, i;
 
3167
  char       data[255];
 
3168
  long       length;
 
3169
  ulong      length1;
 
3170
  MYSQL_RES  *result;
 
3171
  MYSQL_BIND my_bind[2];
 
3172
  my_bool    is_null[2];
 
3173
  char query[MAX_TEST_QUERY_LENGTH];
 
3174
 
 
3175
  myheader("test_long_data_str");
 
3176
 
 
3177
  rc= mysql_autocommit(mysql, TRUE);
 
3178
  myquery(rc);
 
3179
 
 
3180
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data_str");
 
3181
  myquery(rc);
 
3182
 
 
3183
  rc= mysql_query(mysql, "CREATE TABLE test_long_data_str(id int, longstr long varchar)");
 
3184
  myquery(rc);
 
3185
 
 
3186
  strmov(query, "INSERT INTO test_long_data_str VALUES(?, ?)");
 
3187
  stmt= mysql_simple_prepare(mysql, query);
 
3188
  check_stmt(stmt);
 
3189
 
 
3190
  verify_param_count(stmt, 2);
 
3191
 
 
3192
  /* Always bzero all members of bind parameter */
 
3193
  bzero((char*) my_bind, sizeof(my_bind));
 
3194
 
 
3195
  my_bind[0].buffer= (void *)&length;
 
3196
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
3197
  my_bind[0].is_null= &is_null[0];
 
3198
  is_null[0]= 0;
 
3199
  length= 0;
 
3200
 
 
3201
  my_bind[1].buffer= data;                          /* string data */
 
3202
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
3203
  my_bind[1].length= &length1;
 
3204
  my_bind[1].is_null= &is_null[1];
 
3205
  is_null[1]= 0;
 
3206
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
3207
  check_execute(stmt, rc);
 
3208
 
 
3209
  length= 40;
 
3210
  strmov(data, "MySQL AB");
 
3211
 
 
3212
  /* supply data in pieces */
 
3213
  for(i= 0; i < 4; i++)
 
3214
  {
 
3215
    rc= mysql_stmt_send_long_data(stmt, 1, (char *)data, 5);
 
3216
    check_execute(stmt, rc);
 
3217
  }
 
3218
  /* execute */
 
3219
  rc= mysql_stmt_execute(stmt);
 
3220
  if (!opt_silent)
 
3221
    fprintf(stdout, " mysql_stmt_execute() returned %d\n", rc);
 
3222
  check_execute(stmt, rc);
 
3223
 
 
3224
  mysql_stmt_close(stmt);
 
3225
 
 
3226
  rc= mysql_commit(mysql);
 
3227
  myquery(rc);
 
3228
 
 
3229
  /* now fetch the results ..*/
 
3230
  rc= mysql_query(mysql, "SELECT LENGTH(longstr), longstr FROM test_long_data_str");
 
3231
  myquery(rc);
 
3232
 
 
3233
  /* get the result */
 
3234
  result= mysql_store_result(mysql);
 
3235
  mytest(result);
 
3236
 
 
3237
  rc= my_process_result_set(result);
 
3238
  DIE_UNLESS(rc == 1);
 
3239
  mysql_free_result(result);
 
3240
 
 
3241
  my_sprintf(data, (data, "%d", i*5));
 
3242
  verify_col_data("test_long_data_str", "LENGTH(longstr)", data);
 
3243
  data[0]= '\0';
 
3244
  while (i--)
 
3245
   strxmov(data, data, "MySQL", NullS);
 
3246
  verify_col_data("test_long_data_str", "longstr", data);
 
3247
 
 
3248
  rc= mysql_query(mysql, "DROP TABLE test_long_data_str");
 
3249
  myquery(rc);
 
3250
}
 
3251
 
 
3252
 
 
3253
/* Test long data (string) handling */
 
3254
 
 
3255
static void test_long_data_str1()
 
3256
{
 
3257
  MYSQL_STMT *stmt;
 
3258
  int        rc, i;
 
3259
  char       data[255];
 
3260
  long       length;
 
3261
  ulong      max_blob_length, blob_length, length1;
 
3262
  my_bool    true_value;
 
3263
  MYSQL_RES  *result;
 
3264
  MYSQL_BIND my_bind[2];
 
3265
  MYSQL_FIELD *field;
 
3266
  char query[MAX_TEST_QUERY_LENGTH];
 
3267
 
 
3268
  myheader("test_long_data_str1");
 
3269
 
 
3270
  rc= mysql_autocommit(mysql, TRUE);
 
3271
  myquery(rc);
 
3272
 
 
3273
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data_str");
 
3274
  myquery(rc);
 
3275
 
 
3276
  rc= mysql_query(mysql, "CREATE TABLE test_long_data_str(longstr long varchar, blb long varbinary)");
 
3277
  myquery(rc);
 
3278
 
 
3279
  strmov(query, "INSERT INTO test_long_data_str VALUES(?, ?)");
 
3280
  stmt= mysql_simple_prepare(mysql, query);
 
3281
  check_stmt(stmt);
 
3282
 
 
3283
  verify_param_count(stmt, 2);
 
3284
 
 
3285
  /* Always bzero all members of bind parameter */
 
3286
  bzero((char*) my_bind, sizeof(my_bind));
 
3287
 
 
3288
  my_bind[0].buffer= data;            /* string data */
 
3289
  my_bind[0].buffer_length= sizeof(data);
 
3290
  my_bind[0].length= &length1;
 
3291
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
3292
  length1= 0;
 
3293
 
 
3294
  my_bind[1]= my_bind[0];
 
3295
  my_bind[1].buffer_type= MYSQL_TYPE_BLOB;
 
3296
 
 
3297
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
3298
  check_execute(stmt, rc);
 
3299
  length= my_sprintf(data, (data, "MySQL AB"));
 
3300
 
 
3301
  /* supply data in pieces */
 
3302
  for (i= 0; i < 3; i++)
 
3303
  {
 
3304
    rc= mysql_stmt_send_long_data(stmt, 0, data, length);
 
3305
    check_execute(stmt, rc);
 
3306
 
 
3307
    rc= mysql_stmt_send_long_data(stmt, 1, data, 2);
 
3308
    check_execute(stmt, rc);
 
3309
  }
 
3310
 
 
3311
  /* execute */
 
3312
  rc= mysql_stmt_execute(stmt);
 
3313
  if (!opt_silent)
 
3314
    fprintf(stdout, " mysql_stmt_execute() returned %d\n", rc);
 
3315
  check_execute(stmt, rc);
 
3316
 
 
3317
  mysql_stmt_close(stmt);
 
3318
 
 
3319
  rc= mysql_commit(mysql);
 
3320
  myquery(rc);
 
3321
 
 
3322
  /* now fetch the results ..*/
 
3323
  rc= mysql_query(mysql, "SELECT LENGTH(longstr), longstr, LENGTH(blb), blb FROM test_long_data_str");
 
3324
  myquery(rc);
 
3325
 
 
3326
  /* get the result */
 
3327
  result= mysql_store_result(mysql);
 
3328
 
 
3329
  mysql_field_seek(result, 1);
 
3330
  field= mysql_fetch_field(result);
 
3331
  max_blob_length= field->max_length;
 
3332
 
 
3333
  mytest(result);
 
3334
 
 
3335
  rc= my_process_result_set(result);
 
3336
  DIE_UNLESS(rc == 1);
 
3337
  mysql_free_result(result);
 
3338
 
 
3339
  my_sprintf(data, (data, "%ld", (long)i*length));
 
3340
  verify_col_data("test_long_data_str", "length(longstr)", data);
 
3341
 
 
3342
  my_sprintf(data, (data, "%d", i*2));
 
3343
  verify_col_data("test_long_data_str", "length(blb)", data);
 
3344
 
 
3345
  /* Test length of field->max_length */
 
3346
  stmt= mysql_simple_prepare(mysql, "SELECT * from test_long_data_str");
 
3347
  check_stmt(stmt);
 
3348
  verify_param_count(stmt, 0);
 
3349
 
 
3350
  rc= mysql_stmt_execute(stmt);
 
3351
  check_execute(stmt, rc);
 
3352
 
 
3353
  rc= mysql_stmt_store_result(stmt);
 
3354
  check_execute(stmt, rc);
 
3355
 
 
3356
  result= mysql_stmt_result_metadata(stmt);
 
3357
  field= mysql_fetch_fields(result);
 
3358
 
 
3359
  /* First test what happens if STMT_ATTR_UPDATE_MAX_LENGTH is not used */
 
3360
  DIE_UNLESS(field->max_length == 0);
 
3361
  mysql_free_result(result);
 
3362
 
 
3363
  /* Enable updating of field->max_length */
 
3364
  true_value= 1;
 
3365
  mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*) &true_value);
 
3366
  rc= mysql_stmt_execute(stmt);
 
3367
  check_execute(stmt, rc);
 
3368
 
 
3369
  rc= mysql_stmt_store_result(stmt);
 
3370
  check_execute(stmt, rc);
 
3371
 
 
3372
  result= mysql_stmt_result_metadata(stmt);
 
3373
  field= mysql_fetch_fields(result);
 
3374
 
 
3375
  DIE_UNLESS(field->max_length == max_blob_length);
 
3376
 
 
3377
  /* Fetch results into a data buffer that is smaller than data */
 
3378
  bzero((char*) my_bind, sizeof(*my_bind));
 
3379
  my_bind[0].buffer_type= MYSQL_TYPE_BLOB;
 
3380
  my_bind[0].buffer= (void *) &data; /* this buffer won't be altered */
 
3381
  my_bind[0].buffer_length= 16;
 
3382
  my_bind[0].length= &blob_length;
 
3383
  my_bind[0].error= &my_bind[0].error_value;
 
3384
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
3385
  data[16]= 0;
 
3386
 
 
3387
  rc= mysql_stmt_fetch(stmt);
 
3388
  DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
 
3389
  DIE_UNLESS(my_bind[0].error_value);
 
3390
  DIE_UNLESS(strlen(data) == 16);
 
3391
  DIE_UNLESS(blob_length == max_blob_length);
 
3392
 
 
3393
  /* Fetch all data */
 
3394
  bzero((char*) (my_bind+1), sizeof(*my_bind));
 
3395
  my_bind[1].buffer_type= MYSQL_TYPE_BLOB;
 
3396
  my_bind[1].buffer= (void *) &data; /* this buffer won't be altered */
 
3397
  my_bind[1].buffer_length= sizeof(data);
 
3398
  my_bind[1].length= &blob_length;
 
3399
  bzero(data, sizeof(data));
 
3400
  mysql_stmt_fetch_column(stmt, my_bind+1, 0, 0);
 
3401
  DIE_UNLESS(strlen(data) == max_blob_length);
 
3402
 
 
3403
  mysql_free_result(result);
 
3404
  mysql_stmt_close(stmt);
 
3405
 
 
3406
  /* Drop created table */
 
3407
  rc= mysql_query(mysql, "DROP TABLE test_long_data_str");
 
3408
  myquery(rc);
 
3409
}
 
3410
 
 
3411
 
 
3412
/* Test long data (binary) handling */
 
3413
 
 
3414
static void test_long_data_bin()
 
3415
{
 
3416
  MYSQL_STMT *stmt;
 
3417
  int        rc;
 
3418
  char       data[255];
 
3419
  long       length;
 
3420
  MYSQL_RES  *result;
 
3421
  MYSQL_BIND my_bind[2];
 
3422
  char query[MAX_TEST_QUERY_LENGTH];
 
3423
 
 
3424
 
 
3425
  myheader("test_long_data_bin");
 
3426
 
 
3427
  rc= mysql_autocommit(mysql, TRUE);
 
3428
  myquery(rc);
 
3429
 
 
3430
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data_bin");
 
3431
  myquery(rc);
 
3432
 
 
3433
  rc= mysql_query(mysql, "CREATE TABLE test_long_data_bin(id int, longbin long varbinary)");
 
3434
  myquery(rc);
 
3435
 
 
3436
  strmov(query, "INSERT INTO test_long_data_bin VALUES(?, ?)");
 
3437
  stmt= mysql_simple_prepare(mysql, query);
 
3438
  check_stmt(stmt);
 
3439
 
 
3440
  verify_param_count(stmt, 2);
 
3441
 
 
3442
  /* Always bzero all members of bind parameter */
 
3443
  bzero((char*) my_bind, sizeof(my_bind));
 
3444
 
 
3445
  my_bind[0].buffer= (void *)&length;
 
3446
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
3447
  length= 0;
 
3448
 
 
3449
  my_bind[1].buffer= data;           /* string data */
 
3450
  my_bind[1].buffer_type= MYSQL_TYPE_LONG_BLOB;
 
3451
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
3452
  check_execute(stmt, rc);
 
3453
 
 
3454
  length= 10;
 
3455
  strmov(data, "MySQL AB");
 
3456
 
 
3457
  /* supply data in pieces */
 
3458
  {
 
3459
    int i;
 
3460
    for (i= 0; i < 100; i++)
 
3461
    {
 
3462
      rc= mysql_stmt_send_long_data(stmt, 1, (char *)data, 4);
 
3463
      check_execute(stmt, rc);
 
3464
    }
 
3465
  }
 
3466
  /* execute */
 
3467
  rc= mysql_stmt_execute(stmt);
 
3468
  if (!opt_silent)
 
3469
    fprintf(stdout, " mysql_stmt_execute() returned %d\n", rc);
 
3470
  check_execute(stmt, rc);
 
3471
 
 
3472
  mysql_stmt_close(stmt);
 
3473
 
 
3474
  rc= mysql_commit(mysql);
 
3475
  myquery(rc);
 
3476
 
 
3477
  /* now fetch the results ..*/
 
3478
  rc= mysql_query(mysql, "SELECT LENGTH(longbin), longbin FROM test_long_data_bin");
 
3479
  myquery(rc);
 
3480
 
 
3481
  /* get the result */
 
3482
  result= mysql_store_result(mysql);
 
3483
  mytest(result);
 
3484
 
 
3485
  rc= my_process_result_set(result);
 
3486
  DIE_UNLESS(rc == 1);
 
3487
  mysql_free_result(result);
 
3488
}
 
3489
 
 
3490
 
 
3491
/* Test simple delete */
 
3492
 
 
3493
static void test_simple_delete()
 
3494
{
 
3495
  MYSQL_STMT *stmt;
 
3496
  int        rc;
 
3497
  char       szData[30]= {0};
 
3498
  int        nData= 1;
 
3499
  MYSQL_RES  *result;
 
3500
  MYSQL_BIND my_bind[2];
 
3501
  ulong length[2];
 
3502
  char query[MAX_TEST_QUERY_LENGTH];
 
3503
 
 
3504
  myheader("test_simple_delete");
 
3505
 
 
3506
  rc= mysql_autocommit(mysql, TRUE);
 
3507
  myquery(rc);
 
3508
 
 
3509
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_simple_delete");
 
3510
  myquery(rc);
 
3511
 
 
3512
  rc= mysql_query(mysql, "CREATE TABLE test_simple_delete(col1 int, \
 
3513
                                col2 varchar(50), col3 int )");
 
3514
  myquery(rc);
 
3515
 
 
3516
  rc= mysql_query(mysql, "INSERT INTO test_simple_delete VALUES(1, 'MySQL', 100)");
 
3517
  myquery(rc);
 
3518
 
 
3519
  verify_affected_rows(1);
 
3520
 
 
3521
  rc= mysql_commit(mysql);
 
3522
  myquery(rc);
 
3523
 
 
3524
  /* insert by prepare */
 
3525
  strmov(query, "DELETE FROM test_simple_delete WHERE col1= ? AND "
 
3526
                "CONVERT(col2 USING utf8)= ? AND col3= 100");
 
3527
  stmt= mysql_simple_prepare(mysql, query);
 
3528
  check_stmt(stmt);
 
3529
 
 
3530
  verify_param_count(stmt, 2);
 
3531
 
 
3532
  /* Always bzero all members of bind parameter */
 
3533
  bzero((char*) my_bind, sizeof(my_bind));
 
3534
 
 
3535
  nData= 1;
 
3536
  strmov(szData, "MySQL");
 
3537
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
3538
  my_bind[1].buffer= szData;               /* string data */
 
3539
  my_bind[1].buffer_length= sizeof(szData);
 
3540
  my_bind[1].length= &length[1];
 
3541
  length[1]= 5;
 
3542
 
 
3543
  my_bind[0].buffer= (void *)&nData;
 
3544
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
3545
 
 
3546
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
3547
  check_execute(stmt, rc);
 
3548
 
 
3549
  rc= mysql_stmt_execute(stmt);
 
3550
  check_execute(stmt, rc);
 
3551
 
 
3552
  verify_affected_rows(1);
 
3553
 
 
3554
  mysql_stmt_close(stmt);
 
3555
 
 
3556
  /* now fetch the results ..*/
 
3557
  rc= mysql_commit(mysql);
 
3558
  myquery(rc);
 
3559
 
 
3560
  /* test the results now, only one row should exist */
 
3561
  rc= mysql_query(mysql, "SELECT * FROM test_simple_delete");
 
3562
  myquery(rc);
 
3563
 
 
3564
  /* get the result */
 
3565
  result= mysql_store_result(mysql);
 
3566
  mytest(result);
 
3567
 
 
3568
  rc= my_process_result_set(result);
 
3569
  DIE_UNLESS(rc == 0);
 
3570
  mysql_free_result(result);
 
3571
}
 
3572
 
 
3573
 
 
3574
/* Test simple update */
 
3575
 
 
3576
static void test_update()
 
3577
{
 
3578
  MYSQL_STMT *stmt;
 
3579
  int        rc;
 
3580
  char       szData[25];
 
3581
  int        nData= 1;
 
3582
  MYSQL_RES  *result;
 
3583
  MYSQL_BIND my_bind[2];
 
3584
  ulong length[2];
 
3585
  char query[MAX_TEST_QUERY_LENGTH];
 
3586
 
 
3587
  myheader("test_update");
 
3588
 
 
3589
  rc= mysql_autocommit(mysql, TRUE);
 
3590
  myquery(rc);
 
3591
 
 
3592
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_update");
 
3593
  myquery(rc);
 
3594
 
 
3595
  rc= mysql_query(mysql, "CREATE TABLE test_update("
 
3596
                               "col1 int primary key auto_increment, "
 
3597
                               "col2 varchar(50), col3 int )");
 
3598
  myquery(rc);
 
3599
 
 
3600
  strmov(query, "INSERT INTO test_update(col2, col3) VALUES(?, ?)");
 
3601
  stmt= mysql_simple_prepare(mysql, query);
 
3602
  check_stmt(stmt);
 
3603
 
 
3604
  verify_param_count(stmt, 2);
 
3605
 
 
3606
  /* Always bzero all members of bind parameter */
 
3607
  bzero((char*) my_bind, sizeof(my_bind));
 
3608
 
 
3609
  /* string data */
 
3610
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
3611
  my_bind[0].buffer= szData;
 
3612
  my_bind[0].buffer_length= sizeof(szData);
 
3613
  my_bind[0].length= &length[0];
 
3614
  length[0]= my_sprintf(szData, (szData, "inserted-data"));
 
3615
 
 
3616
  my_bind[1].buffer= (void *)&nData;
 
3617
  my_bind[1].buffer_type= MYSQL_TYPE_LONG;
 
3618
 
 
3619
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
3620
  check_execute(stmt, rc);
 
3621
 
 
3622
  nData= 100;
 
3623
  rc= mysql_stmt_execute(stmt);
 
3624
  check_execute(stmt, rc);
 
3625
 
 
3626
  verify_affected_rows(1);
 
3627
  mysql_stmt_close(stmt);
 
3628
 
 
3629
  strmov(query, "UPDATE test_update SET col2= ? WHERE col3= ?");
 
3630
  stmt= mysql_simple_prepare(mysql, query);
 
3631
  check_stmt(stmt);
 
3632
 
 
3633
  verify_param_count(stmt, 2);
 
3634
  nData= 100;
 
3635
 
 
3636
  /* Always bzero all members of bind parameter */
 
3637
  bzero((char*) my_bind, sizeof(my_bind));
 
3638
 
 
3639
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
3640
  my_bind[0].buffer= szData;
 
3641
  my_bind[0].buffer_length= sizeof(szData);
 
3642
  my_bind[0].length= &length[0];
 
3643
  length[0]= my_sprintf(szData, (szData, "updated-data"));
 
3644
 
 
3645
  my_bind[1].buffer= (void *)&nData;
 
3646
  my_bind[1].buffer_type= MYSQL_TYPE_LONG;
 
3647
 
 
3648
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
3649
  check_execute(stmt, rc);
 
3650
 
 
3651
  rc= mysql_stmt_execute(stmt);
 
3652
  check_execute(stmt, rc);
 
3653
  verify_affected_rows(1);
 
3654
 
 
3655
  mysql_stmt_close(stmt);
 
3656
 
 
3657
  /* now fetch the results ..*/
 
3658
  rc= mysql_commit(mysql);
 
3659
  myquery(rc);
 
3660
 
 
3661
  /* test the results now, only one row should exist */
 
3662
  rc= mysql_query(mysql, "SELECT * FROM test_update");
 
3663
  myquery(rc);
 
3664
 
 
3665
  /* get the result */
 
3666
  result= mysql_store_result(mysql);
 
3667
  mytest(result);
 
3668
 
 
3669
  rc= my_process_result_set(result);
 
3670
  DIE_UNLESS(rc == 1);
 
3671
  mysql_free_result(result);
 
3672
}
 
3673
 
 
3674
 
 
3675
/* Test prepare without parameters */
 
3676
 
 
3677
static void test_prepare_noparam()
 
3678
{
 
3679
  MYSQL_STMT *stmt;
 
3680
  int        rc;
 
3681
  MYSQL_RES  *result;
 
3682
  char query[MAX_TEST_QUERY_LENGTH];
 
3683
 
 
3684
  myheader("test_prepare_noparam");
 
3685
 
 
3686
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_prepare");
 
3687
  myquery(rc);
 
3688
 
 
3689
 
 
3690
  rc= mysql_query(mysql, "CREATE TABLE my_prepare(col1 int, col2 varchar(50))");
 
3691
  myquery(rc);
 
3692
 
 
3693
  /* insert by prepare */
 
3694
  strmov(query, "INSERT INTO my_prepare VALUES(10, 'venu')");
 
3695
  stmt= mysql_simple_prepare(mysql, query);
 
3696
  check_stmt(stmt);
 
3697
 
 
3698
  verify_param_count(stmt, 0);
 
3699
 
 
3700
  rc= mysql_stmt_execute(stmt);
 
3701
  check_execute(stmt, rc);
 
3702
 
 
3703
  mysql_stmt_close(stmt);
 
3704
 
 
3705
  /* now fetch the results ..*/
 
3706
  rc= mysql_commit(mysql);
 
3707
  myquery(rc);
 
3708
 
 
3709
  /* test the results now, only one row should exist */
 
3710
  rc= mysql_query(mysql, "SELECT * FROM my_prepare");
 
3711
  myquery(rc);
 
3712
 
 
3713
  /* get the result */
 
3714
  result= mysql_store_result(mysql);
 
3715
  mytest(result);
 
3716
 
 
3717
  rc= my_process_result_set(result);
 
3718
  DIE_UNLESS(rc == 1);
 
3719
  mysql_free_result(result);
 
3720
}
 
3721
 
 
3722
 
 
3723
/* Test simple bind result */
 
3724
 
 
3725
static void test_bind_result()
 
3726
{
 
3727
  MYSQL_STMT *stmt;
 
3728
  int        rc;
 
3729
  int        nData;
 
3730
  ulong      length1;
 
3731
  char       szData[100];
 
3732
  MYSQL_BIND my_bind[2];
 
3733
  my_bool    is_null[2];
 
3734
 
 
3735
  myheader("test_bind_result");
 
3736
 
 
3737
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
 
3738
  myquery(rc);
 
3739
 
 
3740
  rc= mysql_query(mysql, "CREATE TABLE test_bind_result(col1 int , col2 varchar(50))");
 
3741
  myquery(rc);
 
3742
 
 
3743
  rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES(10, 'venu')");
 
3744
  myquery(rc);
 
3745
 
 
3746
  rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES(20, 'MySQL')");
 
3747
  myquery(rc);
 
3748
 
 
3749
  rc= mysql_query(mysql, "INSERT INTO test_bind_result(col2) VALUES('monty')");
 
3750
  myquery(rc);
 
3751
 
 
3752
  rc= mysql_commit(mysql);
 
3753
  myquery(rc);
 
3754
 
 
3755
  /* fetch */
 
3756
 
 
3757
  bzero((char*) my_bind, sizeof(my_bind));
 
3758
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
3759
  my_bind[0].buffer= (void *) &nData;      /* integer data */
 
3760
  my_bind[0].is_null= &is_null[0];
 
3761
 
 
3762
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
3763
  my_bind[1].buffer= szData;                /* string data */
 
3764
  my_bind[1].buffer_length= sizeof(szData);
 
3765
  my_bind[1].length= &length1;
 
3766
  my_bind[1].is_null= &is_null[1];
 
3767
 
 
3768
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_bind_result");
 
3769
  check_stmt(stmt);
 
3770
 
 
3771
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
3772
  check_execute(stmt, rc);
 
3773
 
 
3774
  rc= mysql_stmt_execute(stmt);
 
3775
  check_execute(stmt, rc);
 
3776
 
 
3777
  rc= mysql_stmt_fetch(stmt);
 
3778
  check_execute(stmt, rc);
 
3779
 
 
3780
  if (!opt_silent)
 
3781
    fprintf(stdout, "\n row 1: %d, %s(%lu)", nData, szData, length1);
 
3782
  DIE_UNLESS(nData == 10);
 
3783
  DIE_UNLESS(strcmp(szData, "venu") == 0);
 
3784
  DIE_UNLESS(length1 == 4);
 
3785
 
 
3786
  rc= mysql_stmt_fetch(stmt);
 
3787
  check_execute(stmt, rc);
 
3788
 
 
3789
  if (!opt_silent)
 
3790
    fprintf(stdout, "\n row 2: %d, %s(%lu)", nData, szData, length1);
 
3791
  DIE_UNLESS(nData == 20);
 
3792
  DIE_UNLESS(strcmp(szData, "MySQL") == 0);
 
3793
  DIE_UNLESS(length1 == 5);
 
3794
 
 
3795
  rc= mysql_stmt_fetch(stmt);
 
3796
  check_execute(stmt, rc);
 
3797
 
 
3798
  if (!opt_silent && is_null[0])
 
3799
    fprintf(stdout, "\n row 3: NULL, %s(%lu)", szData, length1);
 
3800
  DIE_UNLESS(is_null[0]);
 
3801
  DIE_UNLESS(strcmp(szData, "monty") == 0);
 
3802
  DIE_UNLESS(length1 == 5);
 
3803
 
 
3804
  rc= mysql_stmt_fetch(stmt);
 
3805
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
3806
 
 
3807
  mysql_stmt_close(stmt);
 
3808
}
 
3809
 
 
3810
 
 
3811
/* Test ext bind result */
 
3812
 
 
3813
static void test_bind_result_ext()
 
3814
{
 
3815
  MYSQL_STMT *stmt;
 
3816
  int        rc, i;
 
3817
  uchar      t_data;
 
3818
  short      s_data;
 
3819
  int        i_data;
 
3820
  longlong   b_data;
 
3821
  float      f_data;
 
3822
  double     d_data;
 
3823
  char       szData[20], bData[20];
 
3824
  ulong       szLength, bLength;
 
3825
  MYSQL_BIND my_bind[8];
 
3826
  ulong      length[8];
 
3827
  my_bool    is_null[8];
 
3828
  char       llbuf[22];
 
3829
  myheader("test_bind_result_ext");
 
3830
 
 
3831
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
 
3832
  myquery(rc);
 
3833
 
 
3834
  rc= mysql_query(mysql, "CREATE TABLE test_bind_result(c1 tinyint, "
 
3835
                                                      " c2 smallint, "
 
3836
                                                      " c3 int, c4 bigint, "
 
3837
                                                      " c5 float, c6 double, "
 
3838
                                                      " c7 varbinary(10), "
 
3839
                                                      " c8 varchar(50))");
 
3840
  myquery(rc);
 
3841
 
 
3842
  rc= mysql_query(mysql, "INSERT INTO test_bind_result "
 
3843
                         "VALUES (19, 2999, 3999, 4999999, "
 
3844
                         " 2345.6, 5678.89563, 'venu', 'mysql')");
 
3845
  myquery(rc);
 
3846
 
 
3847
  rc= mysql_commit(mysql);
 
3848
  myquery(rc);
 
3849
 
 
3850
  bzero((char*) my_bind, sizeof(my_bind));
 
3851
  for (i= 0; i < (int) array_elements(my_bind); i++)
 
3852
  {
 
3853
    my_bind[i].length=  &length[i];
 
3854
    my_bind[i].is_null= &is_null[i];
 
3855
  }
 
3856
 
 
3857
  my_bind[0].buffer_type= MYSQL_TYPE_TINY;
 
3858
  my_bind[0].buffer= (void *)&t_data;
 
3859
 
 
3860
  my_bind[1].buffer_type= MYSQL_TYPE_SHORT;
 
3861
  my_bind[2].buffer_type= MYSQL_TYPE_LONG;
 
3862
 
 
3863
  my_bind[3].buffer_type= MYSQL_TYPE_LONGLONG;
 
3864
  my_bind[1].buffer= (void *)&s_data;
 
3865
 
 
3866
  my_bind[2].buffer= (void *)&i_data;
 
3867
  my_bind[3].buffer= (void *)&b_data;
 
3868
 
 
3869
  my_bind[4].buffer_type= MYSQL_TYPE_FLOAT;
 
3870
  my_bind[4].buffer= (void *)&f_data;
 
3871
 
 
3872
  my_bind[5].buffer_type= MYSQL_TYPE_DOUBLE;
 
3873
  my_bind[5].buffer= (void *)&d_data;
 
3874
 
 
3875
  my_bind[6].buffer_type= MYSQL_TYPE_STRING;
 
3876
  my_bind[6].buffer= (void *)szData;
 
3877
  my_bind[6].buffer_length= sizeof(szData);
 
3878
  my_bind[6].length= &szLength;
 
3879
 
 
3880
  my_bind[7].buffer_type= MYSQL_TYPE_TINY_BLOB;
 
3881
  my_bind[7].buffer= (void *)&bData;
 
3882
  my_bind[7].length= &bLength;
 
3883
  my_bind[7].buffer_length= sizeof(bData);
 
3884
 
 
3885
  stmt= mysql_simple_prepare(mysql, "select * from test_bind_result");
 
3886
  check_stmt(stmt);
 
3887
 
 
3888
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
3889
  check_execute(stmt, rc);
 
3890
 
 
3891
  rc= mysql_stmt_execute(stmt);
 
3892
  check_execute(stmt, rc);
 
3893
 
 
3894
  rc= mysql_stmt_fetch(stmt);
 
3895
  check_execute(stmt, rc);
 
3896
 
 
3897
  if (!opt_silent)
 
3898
  {
 
3899
    fprintf(stdout, "\n data (tiny)   : %d", t_data);
 
3900
    fprintf(stdout, "\n data (short)  : %d", s_data);
 
3901
    fprintf(stdout, "\n data (int)    : %d", i_data);
 
3902
    fprintf(stdout, "\n data (big)    : %s", llstr(b_data, llbuf));
 
3903
 
 
3904
    fprintf(stdout, "\n data (float)  : %f", f_data);
 
3905
    fprintf(stdout, "\n data (double) : %f", d_data);
 
3906
 
 
3907
    fprintf(stdout, "\n data (str)    : %s(%lu)", szData, szLength);
 
3908
 
 
3909
    bData[bLength]= '\0';                         /* bData is binary */
 
3910
    fprintf(stdout, "\n data (bin)    : %s(%lu)", bData, bLength);
 
3911
  }
 
3912
 
 
3913
  DIE_UNLESS(t_data == 19);
 
3914
  DIE_UNLESS(s_data == 2999);
 
3915
  DIE_UNLESS(i_data == 3999);
 
3916
  DIE_UNLESS(b_data == 4999999);
 
3917
  /*DIE_UNLESS(f_data == 2345.60);*/
 
3918
  /*DIE_UNLESS(d_data == 5678.89563);*/
 
3919
  DIE_UNLESS(strcmp(szData, "venu") == 0);
 
3920
  DIE_UNLESS(strncmp(bData, "mysql", 5) == 0);
 
3921
  DIE_UNLESS(szLength == 4);
 
3922
  DIE_UNLESS(bLength == 5);
 
3923
 
 
3924
  rc= mysql_stmt_fetch(stmt);
 
3925
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
3926
 
 
3927
  mysql_stmt_close(stmt);
 
3928
}
 
3929
 
 
3930
 
 
3931
/* Test ext bind result */
 
3932
 
 
3933
static void test_bind_result_ext1()
 
3934
{
 
3935
  MYSQL_STMT *stmt;
 
3936
  uint       i;
 
3937
  int        rc;
 
3938
  char       t_data[20];
 
3939
  float      s_data;
 
3940
  short      i_data;
 
3941
  uchar      b_data;
 
3942
  int        f_data;
 
3943
  long       bData;
 
3944
  char       d_data[20];
 
3945
  double     szData;
 
3946
  MYSQL_BIND my_bind[8];
 
3947
  ulong      length[8];
 
3948
  my_bool    is_null[8];
 
3949
  myheader("test_bind_result_ext1");
 
3950
 
 
3951
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
 
3952
  myquery(rc);
 
3953
 
 
3954
  rc= mysql_query(mysql, "CREATE TABLE test_bind_result(c1 tinyint, c2 smallint, \
 
3955
                                                        c3 int, c4 bigint, \
 
3956
                                                        c5 float, c6 double, \
 
3957
                                                        c7 varbinary(10), \
 
3958
                                                        c8 varchar(10))");
 
3959
  myquery(rc);
 
3960
 
 
3961
  rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES(120, 2999, 3999, 54, \
 
3962
                                                              2.6, 58.89, \
 
3963
                                                              '206', '6.7')");
 
3964
  myquery(rc);
 
3965
 
 
3966
  rc= mysql_commit(mysql);
 
3967
  myquery(rc);
 
3968
 
 
3969
  bzero((char*) my_bind, sizeof(my_bind));
 
3970
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
3971
  my_bind[0].buffer= (void *) t_data;
 
3972
  my_bind[0].buffer_length= sizeof(t_data);
 
3973
  my_bind[0].error= &my_bind[0].error_value;
 
3974
 
 
3975
  my_bind[1].buffer_type= MYSQL_TYPE_FLOAT;
 
3976
  my_bind[1].buffer= (void *)&s_data;
 
3977
  my_bind[1].buffer_length= 0;
 
3978
  my_bind[1].error= &my_bind[1].error_value;
 
3979
 
 
3980
  my_bind[2].buffer_type= MYSQL_TYPE_SHORT;
 
3981
  my_bind[2].buffer= (void *)&i_data;
 
3982
  my_bind[2].buffer_length= 0;
 
3983
  my_bind[2].error= &my_bind[2].error_value;
 
3984
 
 
3985
  my_bind[3].buffer_type= MYSQL_TYPE_TINY;
 
3986
  my_bind[3].buffer= (void *)&b_data;
 
3987
  my_bind[3].buffer_length= 0;
 
3988
  my_bind[3].error= &my_bind[3].error_value;
 
3989
 
 
3990
  my_bind[4].buffer_type= MYSQL_TYPE_LONG;
 
3991
  my_bind[4].buffer= (void *)&f_data;
 
3992
  my_bind[4].buffer_length= 0;
 
3993
  my_bind[4].error= &my_bind[4].error_value;
 
3994
 
 
3995
  my_bind[5].buffer_type= MYSQL_TYPE_STRING;
 
3996
  my_bind[5].buffer= (void *)d_data;
 
3997
  my_bind[5].buffer_length= sizeof(d_data);
 
3998
  my_bind[5].error= &my_bind[5].error_value;
 
3999
 
 
4000
  my_bind[6].buffer_type= MYSQL_TYPE_LONG;
 
4001
  my_bind[6].buffer= (void *)&bData;
 
4002
  my_bind[6].buffer_length= 0;
 
4003
  my_bind[6].error= &my_bind[6].error_value;
 
4004
 
 
4005
  my_bind[7].buffer_type= MYSQL_TYPE_DOUBLE;
 
4006
  my_bind[7].buffer= (void *)&szData;
 
4007
  my_bind[7].buffer_length= 0;
 
4008
  my_bind[7].error= &my_bind[7].error_value;
 
4009
 
 
4010
  for (i= 0; i < array_elements(my_bind); i++)
 
4011
  {
 
4012
    my_bind[i].is_null= &is_null[i];
 
4013
    my_bind[i].length= &length[i];
 
4014
  }
 
4015
 
 
4016
  stmt= mysql_simple_prepare(mysql, "select * from test_bind_result");
 
4017
  check_stmt(stmt);
 
4018
 
 
4019
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
4020
  check_execute(stmt, rc);
 
4021
 
 
4022
  rc= mysql_stmt_execute(stmt);
 
4023
  check_execute(stmt, rc);
 
4024
 
 
4025
  rc= mysql_stmt_fetch(stmt);
 
4026
  printf("rc=%d\n", rc);
 
4027
  DIE_UNLESS(rc == 0);
 
4028
 
 
4029
  if (!opt_silent)
 
4030
  {
 
4031
    fprintf(stdout, "\n data (tiny)   : %s(%lu)", t_data, length[0]);
 
4032
    fprintf(stdout, "\n data (short)  : %f(%lu)", s_data, length[1]);
 
4033
    fprintf(stdout, "\n data (int)    : %d(%lu)", i_data, length[2]);
 
4034
    fprintf(stdout, "\n data (big)    : %d(%lu)", b_data, length[3]);
 
4035
 
 
4036
    fprintf(stdout, "\n data (float)  : %d(%lu)", f_data, length[4]);
 
4037
    fprintf(stdout, "\n data (double) : %s(%lu)", d_data, length[5]);
 
4038
 
 
4039
    fprintf(stdout, "\n data (bin)    : %ld(%lu)", bData, length[6]);
 
4040
    fprintf(stdout, "\n data (str)    : %g(%lu)", szData, length[7]);
 
4041
  }
 
4042
 
 
4043
  DIE_UNLESS(strcmp(t_data, "120") == 0);
 
4044
  DIE_UNLESS(i_data == 3999);
 
4045
  DIE_UNLESS(f_data == 2);
 
4046
  DIE_UNLESS(strcmp(d_data, "58.89") == 0);
 
4047
  DIE_UNLESS(b_data == 54);
 
4048
 
 
4049
  DIE_UNLESS(length[0] == 3);
 
4050
  DIE_UNLESS(length[1] == 4);
 
4051
  DIE_UNLESS(length[2] == 2);
 
4052
  DIE_UNLESS(length[3] == 1);
 
4053
  DIE_UNLESS(length[4] == 4);
 
4054
  DIE_UNLESS(length[5] == 5);
 
4055
  DIE_UNLESS(length[6] == 4);
 
4056
  DIE_UNLESS(length[7] == 8);
 
4057
 
 
4058
  rc= mysql_stmt_fetch(stmt);
 
4059
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
4060
 
 
4061
  mysql_stmt_close(stmt);
 
4062
}
 
4063
 
 
4064
 
 
4065
/* Generalized fetch conversion routine for all basic types */
 
4066
 
 
4067
static void bind_fetch(int row_count)
 
4068
{
 
4069
  MYSQL_STMT   *stmt;
 
4070
  int          rc, i, count= row_count;
 
4071
  int32        data[10];
 
4072
  int8         i8_data;
 
4073
  int16        i16_data;
 
4074
  int32        i32_data;
 
4075
  longlong     i64_data;
 
4076
  float        f_data;
 
4077
  double       d_data;
 
4078
  char         s_data[10];
 
4079
  ulong        length[10];
 
4080
  MYSQL_BIND   my_bind[7];
 
4081
  my_bool      is_null[7];
 
4082
 
 
4083
  stmt= mysql_simple_prepare(mysql, "INSERT INTO test_bind_fetch VALUES "
 
4084
                                    "(?, ?, ?, ?, ?, ?, ?)");
 
4085
  check_stmt(stmt);
 
4086
 
 
4087
  verify_param_count(stmt, 7);
 
4088
 
 
4089
  /* Always bzero all members of bind parameter */
 
4090
  bzero((char*) my_bind, sizeof(my_bind));
 
4091
 
 
4092
  for (i= 0; i < (int) array_elements(my_bind); i++)
 
4093
  {
 
4094
    my_bind[i].buffer_type= MYSQL_TYPE_LONG;
 
4095
    my_bind[i].buffer= (void *) &data[i];
 
4096
  }
 
4097
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
4098
  check_execute(stmt, rc);
 
4099
 
 
4100
  while (count--)
 
4101
  {
 
4102
    rc= 10+count;
 
4103
    for (i= 0; i < (int) array_elements(my_bind); i++)
 
4104
    {
 
4105
      data[i]= rc+i;
 
4106
      rc+= 12;
 
4107
    }
 
4108
    rc= mysql_stmt_execute(stmt);
 
4109
    check_execute(stmt, rc);
 
4110
  }
 
4111
 
 
4112
  rc= mysql_commit(mysql);
 
4113
  myquery(rc);
 
4114
 
 
4115
  mysql_stmt_close(stmt);
 
4116
 
 
4117
  rc= my_stmt_result("SELECT * FROM test_bind_fetch");
 
4118
  DIE_UNLESS(row_count == rc);
 
4119
 
 
4120
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_bind_fetch");
 
4121
  check_stmt(stmt);
 
4122
 
 
4123
  for (i= 0; i < (int) array_elements(my_bind); i++)
 
4124
  {
 
4125
    my_bind[i].buffer= (void *) &data[i];
 
4126
    my_bind[i].length= &length[i];
 
4127
    my_bind[i].is_null= &is_null[i];
 
4128
  }
 
4129
 
 
4130
  my_bind[0].buffer_type= MYSQL_TYPE_TINY;
 
4131
  my_bind[0].buffer= (void *)&i8_data;
 
4132
 
 
4133
  my_bind[1].buffer_type= MYSQL_TYPE_SHORT;
 
4134
  my_bind[1].buffer= (void *)&i16_data;
 
4135
 
 
4136
  my_bind[2].buffer_type= MYSQL_TYPE_LONG;
 
4137
  my_bind[2].buffer= (void *)&i32_data;
 
4138
 
 
4139
  my_bind[3].buffer_type= MYSQL_TYPE_LONGLONG;
 
4140
  my_bind[3].buffer= (void *)&i64_data;
 
4141
 
 
4142
  my_bind[4].buffer_type= MYSQL_TYPE_FLOAT;
 
4143
  my_bind[4].buffer= (void *)&f_data;
 
4144
 
 
4145
  my_bind[5].buffer_type= MYSQL_TYPE_DOUBLE;
 
4146
  my_bind[5].buffer= (void *)&d_data;
 
4147
 
 
4148
  my_bind[6].buffer_type= MYSQL_TYPE_STRING;
 
4149
  my_bind[6].buffer= (void *)&s_data;
 
4150
  my_bind[6].buffer_length= sizeof(s_data);
 
4151
 
 
4152
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
4153
  check_execute(stmt, rc);
 
4154
 
 
4155
  rc= mysql_stmt_execute(stmt);
 
4156
  check_execute(stmt, rc);
 
4157
 
 
4158
  rc= mysql_stmt_store_result(stmt);
 
4159
  check_execute(stmt, rc);
 
4160
 
 
4161
  while (row_count--)
 
4162
  {
 
4163
    rc= mysql_stmt_fetch(stmt);
 
4164
    check_execute(stmt, rc);
 
4165
 
 
4166
    if (!opt_silent)
 
4167
    {
 
4168
      fprintf(stdout, "\n");
 
4169
      fprintf(stdout, "\n tiny     : %ld(%lu)", (ulong) i8_data, length[0]);
 
4170
      fprintf(stdout, "\n short    : %ld(%lu)", (ulong) i16_data, length[1]);
 
4171
      fprintf(stdout, "\n int      : %ld(%lu)", (ulong) i32_data, length[2]);
 
4172
      fprintf(stdout, "\n longlong : %ld(%lu)", (ulong) i64_data, length[3]);
 
4173
      fprintf(stdout, "\n float    : %f(%lu)",  f_data,  length[4]);
 
4174
      fprintf(stdout, "\n double   : %g(%lu)",  d_data,  length[5]);
 
4175
      fprintf(stdout, "\n char     : %s(%lu)",  s_data,  length[6]);
 
4176
    }
 
4177
    rc= 10+row_count;
 
4178
 
 
4179
    /* TINY */
 
4180
    DIE_UNLESS((int) i8_data == rc);
 
4181
    DIE_UNLESS(length[0] == 1);
 
4182
    rc+= 13;
 
4183
 
 
4184
    /* SHORT */
 
4185
    DIE_UNLESS((int) i16_data == rc);
 
4186
    DIE_UNLESS(length[1] == 2);
 
4187
    rc+= 13;
 
4188
 
 
4189
    /* LONG */
 
4190
    DIE_UNLESS((int) i32_data == rc);
 
4191
    DIE_UNLESS(length[2] == 4);
 
4192
    rc+= 13;
 
4193
 
 
4194
    /* LONGLONG */
 
4195
    DIE_UNLESS((int) i64_data == rc);
 
4196
    DIE_UNLESS(length[3] == 8);
 
4197
    rc+= 13;
 
4198
 
 
4199
    /* FLOAT */
 
4200
    DIE_UNLESS((int)f_data == rc);
 
4201
    DIE_UNLESS(length[4] == 4);
 
4202
    rc+= 13;
 
4203
 
 
4204
    /* DOUBLE */
 
4205
    DIE_UNLESS((int)d_data == rc);
 
4206
    DIE_UNLESS(length[5] == 8);
 
4207
    rc+= 13;
 
4208
 
 
4209
    /* CHAR */
 
4210
    {
 
4211
      char buff[20];
 
4212
      long len= my_sprintf(buff, (buff, "%d", rc));
 
4213
      DIE_UNLESS(strcmp(s_data, buff) == 0);
 
4214
      DIE_UNLESS(length[6] == (ulong) len);
 
4215
    }
 
4216
  }
 
4217
  rc= mysql_stmt_fetch(stmt);
 
4218
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
4219
 
 
4220
  mysql_stmt_close(stmt);
 
4221
}
 
4222
 
 
4223
 
 
4224
/* Test fetching of date, time and ts */
 
4225
 
 
4226
static void test_fetch_date()
 
4227
{
 
4228
  MYSQL_STMT *stmt;
 
4229
  uint       i;
 
4230
  int        rc, year;
 
4231
  char       date[25], my_time[25], ts[25], ts_4[25], ts_6[20], dt[20];
 
4232
  ulong      d_length, t_length, ts_length, ts4_length, ts6_length,
 
4233
             dt_length, y_length;
 
4234
  MYSQL_BIND my_bind[8];
 
4235
  my_bool    is_null[8];
 
4236
  ulong      length[8];
 
4237
 
 
4238
  myheader("test_fetch_date");
 
4239
 
 
4240
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
 
4241
  myquery(rc);
 
4242
 
 
4243
  rc= mysql_query(mysql, "CREATE TABLE test_bind_result(c1 date, c2 time, \
 
4244
                                                        c3 timestamp, \
 
4245
                                                        c4 year, \
 
4246
                                                        c5 datetime, \
 
4247
                                                        c6 timestamp, \
 
4248
                                                        c7 timestamp)");
 
4249
  myquery(rc);
 
4250
 
 
4251
  rc= mysql_query(mysql, "SET SQL_MODE=''");
 
4252
  rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES('2002-01-02', \
 
4253
                                                              '12:49:00', \
 
4254
                                                              '2002-01-02 17:46:59', \
 
4255
                                                              2010, \
 
4256
                                                              '2010-07-10', \
 
4257
                                                              '2020', '1999-12-29')");
 
4258
  myquery(rc);
 
4259
 
 
4260
  rc= mysql_commit(mysql);
 
4261
  myquery(rc);
 
4262
 
 
4263
  bzero((char*) my_bind, sizeof(my_bind));
 
4264
  for (i= 0; i < array_elements(my_bind); i++)
 
4265
  {
 
4266
    my_bind[i].is_null= &is_null[i];
 
4267
    my_bind[i].length= &length[i];
 
4268
  }
 
4269
 
 
4270
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
4271
  my_bind[1]= my_bind[2]= my_bind[0];
 
4272
 
 
4273
  my_bind[0].buffer= (void *)&date;
 
4274
  my_bind[0].buffer_length= sizeof(date);
 
4275
  my_bind[0].length= &d_length;
 
4276
 
 
4277
  my_bind[1].buffer= (void *)&my_time;
 
4278
  my_bind[1].buffer_length= sizeof(my_time);
 
4279
  my_bind[1].length= &t_length;
 
4280
 
 
4281
  my_bind[2].buffer= (void *)&ts;
 
4282
  my_bind[2].buffer_length= sizeof(ts);
 
4283
  my_bind[2].length= &ts_length;
 
4284
 
 
4285
  my_bind[3].buffer_type= MYSQL_TYPE_LONG;
 
4286
  my_bind[3].buffer= (void *)&year;
 
4287
  my_bind[3].length= &y_length;
 
4288
 
 
4289
  my_bind[4].buffer_type= MYSQL_TYPE_STRING;
 
4290
  my_bind[4].buffer= (void *)&dt;
 
4291
  my_bind[4].buffer_length= sizeof(dt);
 
4292
  my_bind[4].length= &dt_length;
 
4293
 
 
4294
  my_bind[5].buffer_type= MYSQL_TYPE_STRING;
 
4295
  my_bind[5].buffer= (void *)&ts_4;
 
4296
  my_bind[5].buffer_length= sizeof(ts_4);
 
4297
  my_bind[5].length= &ts4_length;
 
4298
 
 
4299
  my_bind[6].buffer_type= MYSQL_TYPE_STRING;
 
4300
  my_bind[6].buffer= (void *)&ts_6;
 
4301
  my_bind[6].buffer_length= sizeof(ts_6);
 
4302
  my_bind[6].length= &ts6_length;
 
4303
 
 
4304
  rc= my_stmt_result("SELECT * FROM test_bind_result");
 
4305
  DIE_UNLESS(rc == 1);
 
4306
 
 
4307
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_bind_result");
 
4308
  check_stmt(stmt);
 
4309
 
 
4310
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
4311
  check_execute(stmt, rc);
 
4312
 
 
4313
  rc= mysql_stmt_execute(stmt);
 
4314
  check_execute(stmt, rc);
 
4315
 
 
4316
  ts_4[0]= '\0';
 
4317
  rc= mysql_stmt_fetch(stmt);
 
4318
  check_execute(stmt, rc);
 
4319
 
 
4320
  if (!opt_silent)
 
4321
  {
 
4322
    fprintf(stdout, "\n date   : %s(%lu)", date, d_length);
 
4323
    fprintf(stdout, "\n time   : %s(%lu)", my_time, t_length);
 
4324
    fprintf(stdout, "\n ts     : %s(%lu)", ts, ts_length);
 
4325
    fprintf(stdout, "\n year   : %d(%lu)", year, y_length);
 
4326
    fprintf(stdout, "\n dt     : %s(%lu)", dt,  dt_length);
 
4327
    fprintf(stdout, "\n ts(4)  : %s(%lu)", ts_4, ts4_length);
 
4328
    fprintf(stdout, "\n ts(6)  : %s(%lu)", ts_6, ts6_length);
 
4329
  }
 
4330
 
 
4331
  DIE_UNLESS(strcmp(date, "2002-01-02") == 0);
 
4332
  DIE_UNLESS(d_length == 10);
 
4333
 
 
4334
  DIE_UNLESS(strcmp(my_time, "12:49:00") == 0);
 
4335
  DIE_UNLESS(t_length == 8);
 
4336
 
 
4337
  DIE_UNLESS(strcmp(ts, "2002-01-02 17:46:59") == 0);
 
4338
  DIE_UNLESS(ts_length == 19);
 
4339
 
 
4340
  DIE_UNLESS(year == 2010);
 
4341
  DIE_UNLESS(y_length == 4);
 
4342
 
 
4343
  DIE_UNLESS(strcmp(dt, "2010-07-10 00:00:00") == 0);
 
4344
  DIE_UNLESS(dt_length == 19);
 
4345
 
 
4346
  DIE_UNLESS(strcmp(ts_4, "0000-00-00 00:00:00") == 0);
 
4347
  DIE_UNLESS(ts4_length == strlen("0000-00-00 00:00:00"));
 
4348
 
 
4349
  DIE_UNLESS(strcmp(ts_6, "1999-12-29 00:00:00") == 0);
 
4350
  DIE_UNLESS(ts6_length == 19);
 
4351
 
 
4352
  rc= mysql_stmt_fetch(stmt);
 
4353
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
4354
 
 
4355
  mysql_stmt_close(stmt);
 
4356
}
 
4357
 
 
4358
 
 
4359
/* Test fetching of str to all types */
 
4360
 
 
4361
static void test_fetch_str()
 
4362
{
 
4363
  int rc;
 
4364
 
 
4365
  myheader("test_fetch_str");
 
4366
 
 
4367
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
 
4368
  myquery(rc);
 
4369
 
 
4370
  rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 char(10), \
 
4371
                                                     c2 char(10), \
 
4372
                                                     c3 char(20), \
 
4373
                                                     c4 char(20), \
 
4374
                                                     c5 char(30), \
 
4375
                                                     c6 char(40), \
 
4376
                                                     c7 char(20))");
 
4377
  myquery(rc);
 
4378
 
 
4379
  bind_fetch(3);
 
4380
}
 
4381
 
 
4382
 
 
4383
/* Test fetching of long to all types */
 
4384
 
 
4385
static void test_fetch_long()
 
4386
{
 
4387
  int rc;
 
4388
 
 
4389
  myheader("test_fetch_long");
 
4390
 
 
4391
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
 
4392
  myquery(rc);
 
4393
 
 
4394
  rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 int unsigned, \
 
4395
                                                     c2 int unsigned, \
 
4396
                                                     c3 int, \
 
4397
                                                     c4 int, \
 
4398
                                                     c5 int, \
 
4399
                                                     c6 int unsigned, \
 
4400
                                                     c7 int)");
 
4401
  myquery(rc);
 
4402
 
 
4403
  bind_fetch(4);
 
4404
}
 
4405
 
 
4406
 
 
4407
/* Test fetching of short to all types */
 
4408
 
 
4409
static void test_fetch_short()
 
4410
{
 
4411
  int rc;
 
4412
 
 
4413
  myheader("test_fetch_short");
 
4414
 
 
4415
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
 
4416
  myquery(rc);
 
4417
 
 
4418
  rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 smallint unsigned, \
 
4419
                                                     c2 smallint, \
 
4420
                                                     c3 smallint unsigned, \
 
4421
                                                     c4 smallint, \
 
4422
                                                     c5 smallint, \
 
4423
                                                     c6 smallint, \
 
4424
                                                     c7 smallint unsigned)");
 
4425
  myquery(rc);
 
4426
 
 
4427
  bind_fetch(5);
 
4428
}
 
4429
 
 
4430
 
 
4431
/* Test fetching of tiny to all types */
 
4432
 
 
4433
static void test_fetch_tiny()
 
4434
{
 
4435
  int rc;
 
4436
 
 
4437
  myheader("test_fetch_tiny");
 
4438
 
 
4439
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
 
4440
  myquery(rc);
 
4441
 
 
4442
  rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 tinyint unsigned, \
 
4443
                                                     c2 tinyint, \
 
4444
                                                     c3 tinyint unsigned, \
 
4445
                                                     c4 tinyint, \
 
4446
                                                     c5 tinyint, \
 
4447
                                                     c6 tinyint, \
 
4448
                                                     c7 tinyint unsigned)");
 
4449
  myquery(rc);
 
4450
 
 
4451
  bind_fetch(3);
 
4452
 
 
4453
}
 
4454
 
 
4455
 
 
4456
/* Test fetching of longlong to all types */
 
4457
 
 
4458
static void test_fetch_bigint()
 
4459
{
 
4460
  int rc;
 
4461
 
 
4462
  myheader("test_fetch_bigint");
 
4463
 
 
4464
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
 
4465
  myquery(rc);
 
4466
 
 
4467
  rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 bigint, \
 
4468
                                                     c2 bigint, \
 
4469
                                                     c3 bigint unsigned, \
 
4470
                                                     c4 bigint unsigned, \
 
4471
                                                     c5 bigint unsigned, \
 
4472
                                                     c6 bigint unsigned, \
 
4473
                                                     c7 bigint unsigned)");
 
4474
  myquery(rc);
 
4475
 
 
4476
  bind_fetch(2);
 
4477
 
 
4478
}
 
4479
 
 
4480
 
 
4481
/* Test fetching of float to all types */
 
4482
 
 
4483
static void test_fetch_float()
 
4484
{
 
4485
  int rc;
 
4486
 
 
4487
  myheader("test_fetch_float");
 
4488
 
 
4489
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
 
4490
  myquery(rc);
 
4491
 
 
4492
  rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 float(3), \
 
4493
                                                     c2 float, \
 
4494
                                                     c3 float unsigned, \
 
4495
                                                     c4 float, \
 
4496
                                                     c5 float, \
 
4497
                                                     c6 float, \
 
4498
                                                     c7 float(10) unsigned)");
 
4499
  myquery(rc);
 
4500
 
 
4501
  bind_fetch(2);
 
4502
 
 
4503
}
 
4504
 
 
4505
 
 
4506
/* Test fetching of double to all types */
 
4507
 
 
4508
static void test_fetch_double()
 
4509
{
 
4510
  int rc;
 
4511
 
 
4512
  myheader("test_fetch_double");
 
4513
 
 
4514
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
 
4515
  myquery(rc);
 
4516
 
 
4517
  rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 double(5, 2), "
 
4518
                         "c2 double unsigned, c3 double unsigned, "
 
4519
                         "c4 double unsigned, c5 double unsigned, "
 
4520
                         "c6 double unsigned, c7 double unsigned)");
 
4521
  myquery(rc);
 
4522
 
 
4523
  bind_fetch(3);
 
4524
 
 
4525
}
 
4526
 
 
4527
 
 
4528
/* Test simple prepare with all possible types */
 
4529
 
 
4530
static void test_prepare_ext()
 
4531
{
 
4532
  MYSQL_STMT *stmt;
 
4533
  int        rc;
 
4534
  char       *sql;
 
4535
  int        nData= 1;
 
4536
  char       tData= 1;
 
4537
  short      sData= 10;
 
4538
  longlong   bData= 20;
 
4539
  MYSQL_BIND my_bind[6];
 
4540
  char query[MAX_TEST_QUERY_LENGTH];
 
4541
  myheader("test_prepare_ext");
 
4542
 
 
4543
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_ext");
 
4544
  myquery(rc);
 
4545
 
 
4546
  sql= (char *)"CREATE TABLE test_prepare_ext"
 
4547
               "("
 
4548
               " c1  tinyint,"
 
4549
               " c2  smallint,"
 
4550
               " c3  mediumint,"
 
4551
               " c4  int,"
 
4552
               " c5  integer,"
 
4553
               " c6  bigint,"
 
4554
               " c7  float,"
 
4555
               " c8  double,"
 
4556
               " c9  double precision,"
 
4557
               " c10 real,"
 
4558
               " c11 decimal(7, 4),"
 
4559
               " c12 numeric(8, 4),"
 
4560
               " c13 date,"
 
4561
               " c14 datetime,"
 
4562
               " c15 timestamp,"
 
4563
               " c16 time,"
 
4564
               " c17 year,"
 
4565
               " c18 bit,"
 
4566
               " c19 bool,"
 
4567
               " c20 char,"
 
4568
               " c21 char(10),"
 
4569
               " c22 varchar(30),"
 
4570
               " c23 tinyblob,"
 
4571
               " c24 tinytext,"
 
4572
               " c25 blob,"
 
4573
               " c26 text,"
 
4574
               " c27 mediumblob,"
 
4575
               " c28 mediumtext,"
 
4576
               " c29 longblob,"
 
4577
               " c30 longtext,"
 
4578
               " c31 enum('one', 'two', 'three'),"
 
4579
               " c32 set('monday', 'tuesday', 'wednesday'))";
 
4580
 
 
4581
  rc= mysql_query(mysql, sql);
 
4582
  myquery(rc);
 
4583
 
 
4584
  /* insert by prepare - all integers */
 
4585
  strmov(query, (char *)"INSERT INTO test_prepare_ext(c1, c2, c3, c4, c5, c6) VALUES(?, ?, ?, ?, ?, ?)");
 
4586
  stmt= mysql_simple_prepare(mysql, query);
 
4587
  check_stmt(stmt);
 
4588
 
 
4589
  verify_param_count(stmt, 6);
 
4590
 
 
4591
  /* Always bzero all members of bind parameter */
 
4592
  bzero((char*) my_bind, sizeof(my_bind));
 
4593
 
 
4594
  /*tinyint*/
 
4595
  my_bind[0].buffer_type= MYSQL_TYPE_TINY;
 
4596
  my_bind[0].buffer= (void *)&tData;
 
4597
 
 
4598
  /*smallint*/
 
4599
  my_bind[1].buffer_type= MYSQL_TYPE_SHORT;
 
4600
  my_bind[1].buffer= (void *)&sData;
 
4601
 
 
4602
  /*mediumint*/
 
4603
  my_bind[2].buffer_type= MYSQL_TYPE_LONG;
 
4604
  my_bind[2].buffer= (void *)&nData;
 
4605
 
 
4606
  /*int*/
 
4607
  my_bind[3].buffer_type= MYSQL_TYPE_LONG;
 
4608
  my_bind[3].buffer= (void *)&nData;
 
4609
 
 
4610
  /*integer*/
 
4611
  my_bind[4].buffer_type= MYSQL_TYPE_LONG;
 
4612
  my_bind[4].buffer= (void *)&nData;
 
4613
 
 
4614
  /*bigint*/
 
4615
  my_bind[5].buffer_type= MYSQL_TYPE_LONGLONG;
 
4616
  my_bind[5].buffer= (void *)&bData;
 
4617
 
 
4618
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
4619
  check_execute(stmt, rc);
 
4620
 
 
4621
  /*
 
4622
  *  integer to integer
 
4623
  */
 
4624
  for (nData= 0; nData<10; nData++, tData++, sData++, bData++)
 
4625
  {
 
4626
    rc= mysql_stmt_execute(stmt);
 
4627
    check_execute(stmt, rc);
 
4628
  }
 
4629
  mysql_stmt_close(stmt);
 
4630
 
 
4631
  /* now fetch the results ..*/
 
4632
 
 
4633
  stmt= mysql_simple_prepare(mysql, "SELECT c1, c2, c3, c4, c5, c6 "
 
4634
                                    "FROM test_prepare_ext");
 
4635
  check_stmt(stmt);
 
4636
 
 
4637
  /* get the result */
 
4638
  rc= mysql_stmt_execute(stmt);
 
4639
  check_execute(stmt, rc);
 
4640
 
 
4641
  rc= my_process_stmt_result(stmt);
 
4642
  DIE_UNLESS(nData == rc);
 
4643
 
 
4644
  mysql_stmt_close(stmt);
 
4645
}
 
4646
 
 
4647
 
 
4648
/* Test real and alias names */
 
4649
 
 
4650
static void test_field_names()
 
4651
{
 
4652
  int        rc;
 
4653
  MYSQL_RES  *result;
 
4654
 
 
4655
  myheader("test_field_names");
 
4656
 
 
4657
  if (!opt_silent)
 
4658
    fprintf(stdout, "\n %d, %d, %d", MYSQL_TYPE_DECIMAL, MYSQL_TYPE_NEWDATE, MYSQL_TYPE_ENUM);
 
4659
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_names1");
 
4660
  myquery(rc);
 
4661
 
 
4662
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_names2");
 
4663
  myquery(rc);
 
4664
 
 
4665
  rc= mysql_query(mysql, "CREATE TABLE test_field_names1(id int, name varchar(50))");
 
4666
  myquery(rc);
 
4667
 
 
4668
  rc= mysql_query(mysql, "CREATE TABLE test_field_names2(id int, name varchar(50))");
 
4669
  myquery(rc);
 
4670
 
 
4671
  /* with table name included with TRUE column name */
 
4672
  rc= mysql_query(mysql, "SELECT id as 'id-alias' FROM test_field_names1");
 
4673
  myquery(rc);
 
4674
 
 
4675
  result= mysql_use_result(mysql);
 
4676
  mytest(result);
 
4677
 
 
4678
  rc= my_process_result_set(result);
 
4679
  DIE_UNLESS(rc == 0);
 
4680
  mysql_free_result(result);
 
4681
 
 
4682
  /* with table name included with TRUE column name */
 
4683
  rc= mysql_query(mysql, "SELECT t1.id as 'id-alias', test_field_names2.name FROM test_field_names1 t1, test_field_names2");
 
4684
  myquery(rc);
 
4685
 
 
4686
  result= mysql_use_result(mysql);
 
4687
  mytest(result);
 
4688
 
 
4689
  rc= my_process_result_set(result);
 
4690
  DIE_UNLESS(rc == 0);
 
4691
  mysql_free_result(result);
 
4692
}
 
4693
 
 
4694
 
 
4695
/* Test warnings */
 
4696
 
 
4697
static void test_warnings()
 
4698
{
 
4699
  int        rc;
 
4700
  MYSQL_RES  *result;
 
4701
 
 
4702
  myheader("test_warnings");
 
4703
 
 
4704
  mysql_query(mysql, "DROP TABLE if exists test_non_exists");
 
4705
 
 
4706
  rc= mysql_query(mysql, "DROP TABLE if exists test_non_exists");
 
4707
  myquery(rc);
 
4708
 
 
4709
  if (!opt_silent)
 
4710
    fprintf(stdout, "\n total warnings: %d", mysql_warning_count(mysql));
 
4711
  rc= mysql_query(mysql, "SHOW WARNINGS");
 
4712
  myquery(rc);
 
4713
 
 
4714
  result= mysql_store_result(mysql);
 
4715
  mytest(result);
 
4716
 
 
4717
  rc= my_process_result_set(result);
 
4718
  DIE_UNLESS(rc == 1);
 
4719
  mysql_free_result(result);
 
4720
}
 
4721
 
 
4722
 
 
4723
/* Test errors */
 
4724
 
 
4725
static void test_errors()
 
4726
{
 
4727
  int        rc;
 
4728
  MYSQL_RES  *result;
 
4729
 
 
4730
  myheader("test_errors");
 
4731
 
 
4732
  mysql_query(mysql, "DROP TABLE if exists test_non_exists");
 
4733
 
 
4734
  rc= mysql_query(mysql, "DROP TABLE test_non_exists");
 
4735
  myquery_r(rc);
 
4736
 
 
4737
  rc= mysql_query(mysql, "SHOW ERRORS");
 
4738
  myquery(rc);
 
4739
 
 
4740
  result= mysql_store_result(mysql);
 
4741
  mytest(result);
 
4742
 
 
4743
  (void) my_process_result_set(result);
 
4744
  mysql_free_result(result);
 
4745
}
 
4746
 
 
4747
 
 
4748
/* Test simple prepare-insert */
 
4749
 
 
4750
static void test_insert()
 
4751
{
 
4752
  MYSQL_STMT *stmt;
 
4753
  int        rc;
 
4754
  char       str_data[50];
 
4755
  char       tiny_data;
 
4756
  MYSQL_RES  *result;
 
4757
  MYSQL_BIND my_bind[2];
 
4758
  ulong      length;
 
4759
 
 
4760
  myheader("test_insert");
 
4761
 
 
4762
  rc= mysql_autocommit(mysql, TRUE);
 
4763
  myquery(rc);
 
4764
 
 
4765
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_insert");
 
4766
  myquery(rc);
 
4767
 
 
4768
  rc= mysql_query(mysql, "CREATE TABLE test_prep_insert(col1 tinyint, \
 
4769
                                col2 varchar(50))");
 
4770
  myquery(rc);
 
4771
 
 
4772
  /* insert by prepare */
 
4773
  stmt= mysql_simple_prepare(mysql,
 
4774
                             "INSERT INTO test_prep_insert VALUES(?, ?)");
 
4775
  check_stmt(stmt);
 
4776
 
 
4777
  verify_param_count(stmt, 2);
 
4778
 
 
4779
  /*
 
4780
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
4781
    its members.
 
4782
  */
 
4783
  bzero((char*) my_bind, sizeof(my_bind));
 
4784
 
 
4785
  /* tinyint */
 
4786
  my_bind[0].buffer_type= MYSQL_TYPE_TINY;
 
4787
  my_bind[0].buffer= (void *)&tiny_data;
 
4788
 
 
4789
  /* string */
 
4790
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
4791
  my_bind[1].buffer= str_data;
 
4792
  my_bind[1].buffer_length= sizeof(str_data);;
 
4793
  my_bind[1].length= &length;
 
4794
 
 
4795
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
4796
  check_execute(stmt, rc);
 
4797
 
 
4798
  /* now, execute the prepared statement to insert 10 records.. */
 
4799
  for (tiny_data= 0; tiny_data < 3; tiny_data++)
 
4800
  {
 
4801
    length= my_sprintf(str_data, (str_data, "MySQL%d", tiny_data));
 
4802
    rc= mysql_stmt_execute(stmt);
 
4803
    check_execute(stmt, rc);
 
4804
  }
 
4805
 
 
4806
  mysql_stmt_close(stmt);
 
4807
 
 
4808
  /* now fetch the results ..*/
 
4809
  rc= mysql_commit(mysql);
 
4810
  myquery(rc);
 
4811
 
 
4812
  /* test the results now, only one row should exist */
 
4813
  rc= mysql_query(mysql, "SELECT * FROM test_prep_insert");
 
4814
  myquery(rc);
 
4815
 
 
4816
  /* get the result */
 
4817
  result= mysql_store_result(mysql);
 
4818
  mytest(result);
 
4819
 
 
4820
  rc= my_process_result_set(result);
 
4821
  DIE_UNLESS((int) tiny_data == rc);
 
4822
  mysql_free_result(result);
 
4823
 
 
4824
}
 
4825
 
 
4826
 
 
4827
/* Test simple prepare-resultset info */
 
4828
 
 
4829
static void test_prepare_resultset()
 
4830
{
 
4831
  MYSQL_STMT *stmt;
 
4832
  int        rc;
 
4833
  MYSQL_RES  *result;
 
4834
 
 
4835
  myheader("test_prepare_resultset");
 
4836
 
 
4837
  rc= mysql_autocommit(mysql, TRUE);
 
4838
  myquery(rc);
 
4839
 
 
4840
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_resultset");
 
4841
  myquery(rc);
 
4842
 
 
4843
  rc= mysql_query(mysql, "CREATE TABLE test_prepare_resultset(id int, \
 
4844
                                name varchar(50), extra double)");
 
4845
  myquery(rc);
 
4846
 
 
4847
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_prepare_resultset");
 
4848
  check_stmt(stmt);
 
4849
 
 
4850
  verify_param_count(stmt, 0);
 
4851
 
 
4852
  result= mysql_stmt_result_metadata(stmt);
 
4853
  mytest(result);
 
4854
  my_print_result_metadata(result);
 
4855
  mysql_free_result(result);
 
4856
  mysql_stmt_close(stmt);
 
4857
}
 
4858
 
 
4859
 
 
4860
/* Test field flags (verify .NET provider) */
 
4861
 
 
4862
static void test_field_flags()
 
4863
{
 
4864
  int          rc;
 
4865
  MYSQL_RES    *result;
 
4866
  MYSQL_FIELD  *field;
 
4867
  unsigned int i;
 
4868
 
 
4869
 
 
4870
  myheader("test_field_flags");
 
4871
 
 
4872
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_flags");
 
4873
  myquery(rc);
 
4874
 
 
4875
  rc= mysql_query(mysql, "CREATE TABLE test_field_flags(id int NOT NULL AUTO_INCREMENT PRIMARY KEY, \
 
4876
                                                        id1 int NOT NULL, \
 
4877
                                                        id2 int UNIQUE, \
 
4878
                                                        id3 int, \
 
4879
                                                        id4 int NOT NULL, \
 
4880
                                                        id5 int, \
 
4881
                                                        KEY(id3, id4))");
 
4882
  myquery(rc);
 
4883
 
 
4884
  /* with table name included with TRUE column name */
 
4885
  rc= mysql_query(mysql, "SELECT * FROM test_field_flags");
 
4886
  myquery(rc);
 
4887
 
 
4888
  result= mysql_use_result(mysql);
 
4889
  mytest(result);
 
4890
 
 
4891
  mysql_field_seek(result, 0);
 
4892
  if (!opt_silent)
 
4893
    fputc('\n', stdout);
 
4894
 
 
4895
  for(i= 0; i< mysql_num_fields(result); i++)
 
4896
  {
 
4897
    field= mysql_fetch_field(result);
 
4898
    if (!opt_silent)
 
4899
    {
 
4900
      fprintf(stdout, "\n field:%d", i);
 
4901
      if (field->flags & NOT_NULL_FLAG)
 
4902
        fprintf(stdout, "\n  NOT_NULL_FLAG");
 
4903
      if (field->flags & PRI_KEY_FLAG)
 
4904
        fprintf(stdout, "\n  PRI_KEY_FLAG");
 
4905
      if (field->flags & UNIQUE_KEY_FLAG)
 
4906
        fprintf(stdout, "\n  UNIQUE_KEY_FLAG");
 
4907
      if (field->flags & MULTIPLE_KEY_FLAG)
 
4908
        fprintf(stdout, "\n  MULTIPLE_KEY_FLAG");
 
4909
      if (field->flags & AUTO_INCREMENT_FLAG)
 
4910
        fprintf(stdout, "\n  AUTO_INCREMENT_FLAG");
 
4911
 
 
4912
    }
 
4913
  }
 
4914
  mysql_free_result(result);
 
4915
}
 
4916
 
 
4917
 
 
4918
/* Test mysql_stmt_close for open stmts */
 
4919
 
 
4920
static void test_stmt_close()
 
4921
{
 
4922
  MYSQL *lmysql;
 
4923
  MYSQL_STMT *stmt1, *stmt2, *stmt3, *stmt_x;
 
4924
  MYSQL_BIND  my_bind[1];
 
4925
  MYSQL_RES   *result;
 
4926
  unsigned int  count;
 
4927
  int   rc;
 
4928
  char query[MAX_TEST_QUERY_LENGTH];
 
4929
 
 
4930
  myheader("test_stmt_close");
 
4931
 
 
4932
  if (!opt_silent)
 
4933
    fprintf(stdout, "\n Establishing a test connection ...");
 
4934
  if (!(lmysql= mysql_init(NULL)))
 
4935
  {
 
4936
    myerror("mysql_init() failed");
 
4937
    exit(1);
 
4938
  }
 
4939
  if (!(mysql_real_connect(lmysql, opt_host, opt_user,
 
4940
                           opt_password, current_db, opt_port,
 
4941
                           opt_unix_socket, 0)))
 
4942
  {
 
4943
    myerror("connection failed");
 
4944
    exit(1);
 
4945
  }
 
4946
  lmysql->reconnect= 1;
 
4947
  if (!opt_silent)
 
4948
    fprintf(stdout, "OK");
 
4949
 
 
4950
 
 
4951
  /* set AUTOCOMMIT to ON*/
 
4952
  mysql_autocommit(lmysql, TRUE);
 
4953
 
 
4954
  rc= mysql_query(lmysql, "DROP TABLE IF EXISTS test_stmt_close");
 
4955
  myquery(rc);
 
4956
 
 
4957
  rc= mysql_query(lmysql, "CREATE TABLE test_stmt_close(id int)");
 
4958
  myquery(rc);
 
4959
 
 
4960
  strmov(query, "DO \"nothing\"");
 
4961
  stmt1= mysql_simple_prepare(lmysql, query);
 
4962
  check_stmt(stmt1);
 
4963
 
 
4964
  verify_param_count(stmt1, 0);
 
4965
 
 
4966
  strmov(query, "INSERT INTO test_stmt_close(id) VALUES(?)");
 
4967
  stmt_x= mysql_simple_prepare(mysql, query);
 
4968
  check_stmt(stmt_x);
 
4969
 
 
4970
  verify_param_count(stmt_x, 1);
 
4971
 
 
4972
  strmov(query, "UPDATE test_stmt_close SET id= ? WHERE id= ?");
 
4973
  stmt3= mysql_simple_prepare(lmysql, query);
 
4974
  check_stmt(stmt3);
 
4975
 
 
4976
  verify_param_count(stmt3, 2);
 
4977
 
 
4978
  strmov(query, "SELECT * FROM test_stmt_close WHERE id= ?");
 
4979
  stmt2= mysql_simple_prepare(lmysql, query);
 
4980
  check_stmt(stmt2);
 
4981
 
 
4982
  verify_param_count(stmt2, 1);
 
4983
 
 
4984
  rc= mysql_stmt_close(stmt1);
 
4985
  if (!opt_silent)
 
4986
    fprintf(stdout, "\n mysql_close_stmt(1) returned: %d", rc);
 
4987
  DIE_UNLESS(rc == 0);
 
4988
 
 
4989
  /*
 
4990
    Originally we were going to close all statements automatically in
 
4991
    mysql_close(). This proved to not work well - users weren't able to
 
4992
    close statements by hand once mysql_close() had been called.
 
4993
    Now mysql_close() doesn't free any statements, so this test doesn't
 
4994
    serve its original designation any more.
 
4995
    Here we free stmt2 and stmt3 by hand to avoid memory leaks.
 
4996
  */
 
4997
  mysql_stmt_close(stmt2);
 
4998
  mysql_stmt_close(stmt3);
 
4999
  mysql_close(lmysql);
 
5000
 
 
5001
  /*
 
5002
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
5003
    its members.
 
5004
  */
 
5005
  bzero((char*) my_bind, sizeof(my_bind));
 
5006
 
 
5007
  my_bind[0].buffer= (void *)&count;
 
5008
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
5009
  count= 100;
 
5010
 
 
5011
  rc= mysql_stmt_bind_param(stmt_x, my_bind);
 
5012
  check_execute(stmt_x, rc);
 
5013
 
 
5014
  rc= mysql_stmt_execute(stmt_x);
 
5015
  check_execute(stmt_x, rc);
 
5016
 
 
5017
  verify_st_affected_rows(stmt_x, 1);
 
5018
 
 
5019
  rc= mysql_stmt_close(stmt_x);
 
5020
  if (!opt_silent)
 
5021
    fprintf(stdout, "\n mysql_close_stmt(x) returned: %d", rc);
 
5022
  DIE_UNLESS( rc == 0);
 
5023
 
 
5024
  rc= mysql_query(mysql, "SELECT id FROM test_stmt_close");
 
5025
  myquery(rc);
 
5026
 
 
5027
  result= mysql_store_result(mysql);
 
5028
  mytest(result);
 
5029
 
 
5030
  rc= my_process_result_set(result);
 
5031
  DIE_UNLESS(rc == 1);
 
5032
  mysql_free_result(result);
 
5033
}
 
5034
 
 
5035
 
 
5036
/* Test simple set-variable prepare */
 
5037
 
 
5038
static void test_set_variable()
 
5039
{
 
5040
  MYSQL_STMT *stmt, *stmt1;
 
5041
  int        rc;
 
5042
  int        set_count, def_count, get_count;
 
5043
  ulong      length;
 
5044
  char       var[NAME_LEN+1];
 
5045
  MYSQL_BIND set_bind[1], get_bind[2];
 
5046
 
 
5047
  myheader("test_set_variable");
 
5048
 
 
5049
  mysql_autocommit(mysql, TRUE);
 
5050
 
 
5051
  stmt1= mysql_simple_prepare(mysql, "show variables like 'max_error_count'");
 
5052
  check_stmt(stmt1);
 
5053
 
 
5054
  /*
 
5055
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
5056
    its members.
 
5057
  */
 
5058
  bzero((char*) get_bind, sizeof(get_bind));
 
5059
 
 
5060
  get_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
5061
  get_bind[0].buffer= (void *)var;
 
5062
  get_bind[0].length= &length;
 
5063
  get_bind[0].buffer_length= (int)NAME_LEN;
 
5064
  length= NAME_LEN;
 
5065
 
 
5066
  get_bind[1].buffer_type= MYSQL_TYPE_LONG;
 
5067
  get_bind[1].buffer= (void *)&get_count;
 
5068
 
 
5069
  rc= mysql_stmt_execute(stmt1);
 
5070
  check_execute(stmt1, rc);
 
5071
 
 
5072
  rc= mysql_stmt_bind_result(stmt1, get_bind);
 
5073
  check_execute(stmt1, rc);
 
5074
 
 
5075
  rc= mysql_stmt_fetch(stmt1);
 
5076
  check_execute(stmt1, rc);
 
5077
 
 
5078
  if (!opt_silent)
 
5079
    fprintf(stdout, "\n max_error_count(default): %d", get_count);
 
5080
  def_count= get_count;
 
5081
 
 
5082
  DIE_UNLESS(strcmp(var, "max_error_count") == 0);
 
5083
  rc= mysql_stmt_fetch(stmt1);
 
5084
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
5085
 
 
5086
  stmt= mysql_simple_prepare(mysql, "set max_error_count= ?");
 
5087
  check_stmt(stmt);
 
5088
 
 
5089
  bzero((char*) set_bind, sizeof(set_bind));
 
5090
 
 
5091
  set_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
5092
  set_bind[0].buffer= (void *)&set_count;
 
5093
 
 
5094
  rc= mysql_stmt_bind_param(stmt, set_bind);
 
5095
  check_execute(stmt, rc);
 
5096
 
 
5097
  set_count= 31;
 
5098
  rc= mysql_stmt_execute(stmt);
 
5099
  check_execute(stmt, rc);
 
5100
 
 
5101
  mysql_commit(mysql);
 
5102
 
 
5103
  rc= mysql_stmt_execute(stmt1);
 
5104
  check_execute(stmt1, rc);
 
5105
 
 
5106
  rc= mysql_stmt_fetch(stmt1);
 
5107
  check_execute(stmt1, rc);
 
5108
 
 
5109
  if (!opt_silent)
 
5110
    fprintf(stdout, "\n max_error_count         : %d", get_count);
 
5111
  DIE_UNLESS(get_count == set_count);
 
5112
 
 
5113
  rc= mysql_stmt_fetch(stmt1);
 
5114
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
5115
 
 
5116
  /* restore back to default */
 
5117
  set_count= def_count;
 
5118
  rc= mysql_stmt_execute(stmt);
 
5119
  check_execute(stmt, rc);
 
5120
 
 
5121
  rc= mysql_stmt_execute(stmt1);
 
5122
  check_execute(stmt1, rc);
 
5123
 
 
5124
  rc= mysql_stmt_fetch(stmt1);
 
5125
  check_execute(stmt1, rc);
 
5126
 
 
5127
  if (!opt_silent)
 
5128
    fprintf(stdout, "\n max_error_count(default): %d", get_count);
 
5129
  DIE_UNLESS(get_count == set_count);
 
5130
 
 
5131
  rc= mysql_stmt_fetch(stmt1);
 
5132
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
5133
 
 
5134
  mysql_stmt_close(stmt);
 
5135
  mysql_stmt_close(stmt1);
 
5136
}
 
5137
 
 
5138
#if NOT_USED
 
5139
 
 
5140
/* Insert meta info .. */
 
5141
 
 
5142
static void test_insert_meta()
 
5143
{
 
5144
  MYSQL_STMT *stmt;
 
5145
  int        rc;
 
5146
  MYSQL_RES  *result;
 
5147
  MYSQL_FIELD *field;
 
5148
 
 
5149
  myheader("test_insert_meta");
 
5150
 
 
5151
  rc= mysql_autocommit(mysql, TRUE);
 
5152
  myquery(rc);
 
5153
 
 
5154
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_insert");
 
5155
  myquery(rc);
 
5156
 
 
5157
  rc= mysql_query(mysql, "CREATE TABLE test_prep_insert(col1 tinyint, \
 
5158
                                col2 varchar(50), col3 varchar(30))");
 
5159
  myquery(rc);
 
5160
 
 
5161
  strmov(query, "INSERT INTO test_prep_insert VALUES(10, 'venu1', 'test')");
 
5162
  stmt= mysql_simple_prepare(mysql, query);
 
5163
  check_stmt(stmt);
 
5164
 
 
5165
  verify_param_count(stmt, 0);
 
5166
 
 
5167
  result= mysql_param_result(stmt);
 
5168
  mytest_r(result);
 
5169
 
 
5170
  mysql_stmt_close(stmt);
 
5171
 
 
5172
  strmov(query, "INSERT INTO test_prep_insert VALUES(?, 'venu', ?)");
 
5173
  stmt= mysql_simple_prepare(mysql, query);
 
5174
  check_stmt(stmt);
 
5175
 
 
5176
  verify_param_count(stmt, 2);
 
5177
 
 
5178
  result= mysql_param_result(stmt);
 
5179
  mytest(result);
 
5180
 
 
5181
  my_print_result_metadata(result);
 
5182
 
 
5183
  mysql_field_seek(result, 0);
 
5184
  field= mysql_fetch_field(result);
 
5185
  mytest(field);
 
5186
  if (!opt_silent)
 
5187
    fprintf(stdout, "\n obtained: `%s` (expected: `%s`)", field->name, "col1");
 
5188
  DIE_UNLESS(strcmp(field->name, "col1") == 0);
 
5189
 
 
5190
  field= mysql_fetch_field(result);
 
5191
  mytest(field);
 
5192
  if (!opt_silent)
 
5193
    fprintf(stdout, "\n obtained: `%s` (expected: `%s`)", field->name, "col3");
 
5194
  DIE_UNLESS(strcmp(field->name, "col3") == 0);
 
5195
 
 
5196
  field= mysql_fetch_field(result);
 
5197
  mytest_r(field);
 
5198
 
 
5199
  mysql_free_result(result);
 
5200
  mysql_stmt_close(stmt);
 
5201
}
 
5202
 
 
5203
 
 
5204
/* Update meta info .. */
 
5205
 
 
5206
static void test_update_meta()
 
5207
{
 
5208
  MYSQL_STMT *stmt;
 
5209
  int        rc;
 
5210
  MYSQL_RES  *result;
 
5211
  MYSQL_FIELD *field;
 
5212
 
 
5213
  myheader("test_update_meta");
 
5214
 
 
5215
  rc= mysql_autocommit(mysql, TRUE);
 
5216
  myquery(rc);
 
5217
 
 
5218
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_update");
 
5219
  myquery(rc);
 
5220
 
 
5221
  rc= mysql_query(mysql, "CREATE TABLE test_prep_update(col1 tinyint, \
 
5222
                                col2 varchar(50), col3 varchar(30))");
 
5223
  myquery(rc);
 
5224
 
 
5225
  strmov(query, "UPDATE test_prep_update SET col1=10, col2='venu1' WHERE col3='test'");
 
5226
  stmt= mysql_simple_prepare(mysql, query);
 
5227
  check_stmt(stmt);
 
5228
 
 
5229
  verify_param_count(stmt, 0);
 
5230
 
 
5231
  result= mysql_param_result(stmt);
 
5232
  mytest_r(result);
 
5233
 
 
5234
  mysql_stmt_close(stmt);
 
5235
 
 
5236
  strmov(query, "UPDATE test_prep_update SET col1=?, col2='venu' WHERE col3=?");
 
5237
  stmt= mysql_simple_prepare(mysql, query);
 
5238
  check_stmt(stmt);
 
5239
 
 
5240
  verify_param_count(stmt, 2);
 
5241
 
 
5242
  result= mysql_param_result(stmt);
 
5243
  mytest(result);
 
5244
 
 
5245
  my_print_result_metadata(result);
 
5246
 
 
5247
  mysql_field_seek(result, 0);
 
5248
  field= mysql_fetch_field(result);
 
5249
  mytest(field);
 
5250
  if (!opt_silent)
 
5251
  {
 
5252
    fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col1");
 
5253
    fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_update");
 
5254
  }
 
5255
  DIE_UNLESS(strcmp(field->name, "col1") == 0);
 
5256
  DIE_UNLESS(strcmp(field->table, "test_prep_update") == 0);
 
5257
 
 
5258
  field= mysql_fetch_field(result);
 
5259
  mytest(field);
 
5260
  if (!opt_silent)
 
5261
  {
 
5262
    fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col3");
 
5263
    fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_update");
 
5264
  }
 
5265
  DIE_UNLESS(strcmp(field->name, "col3") == 0);
 
5266
  DIE_UNLESS(strcmp(field->table, "test_prep_update") == 0);
 
5267
 
 
5268
  field= mysql_fetch_field(result);
 
5269
  mytest_r(field);
 
5270
 
 
5271
  mysql_free_result(result);
 
5272
  mysql_stmt_close(stmt);
 
5273
}
 
5274
 
 
5275
 
 
5276
/* Select meta info .. */
 
5277
 
 
5278
static void test_select_meta()
 
5279
{
 
5280
  MYSQL_STMT *stmt;
 
5281
  int        rc;
 
5282
  MYSQL_RES  *result;
 
5283
  MYSQL_FIELD *field;
 
5284
 
 
5285
  myheader("test_select_meta");
 
5286
 
 
5287
  rc= mysql_autocommit(mysql, TRUE);
 
5288
  myquery(rc);
 
5289
 
 
5290
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_select");
 
5291
  myquery(rc);
 
5292
 
 
5293
  rc= mysql_query(mysql, "CREATE TABLE test_prep_select(col1 tinyint, \
 
5294
                                col2 varchar(50), col3 varchar(30))");
 
5295
  myquery(rc);
 
5296
 
 
5297
  strmov(query, "SELECT * FROM test_prep_select WHERE col1=10");
 
5298
  stmt= mysql_simple_prepare(mysql, query);
 
5299
  check_stmt(stmt);
 
5300
 
 
5301
  verify_param_count(stmt, 0);
 
5302
 
 
5303
  result= mysql_param_result(stmt);
 
5304
  mytest_r(result);
 
5305
 
 
5306
  strmov(query, "SELECT col1, col3 from test_prep_select WHERE col1=? AND col3='test' AND col2= ?");
 
5307
  stmt= mysql_simple_prepare(mysql, query);
 
5308
  check_stmt(stmt);
 
5309
 
 
5310
  verify_param_count(stmt, 2);
 
5311
 
 
5312
  result= mysql_param_result(stmt);
 
5313
  mytest(result);
 
5314
 
 
5315
  my_print_result_metadata(result);
 
5316
 
 
5317
  mysql_field_seek(result, 0);
 
5318
  field= mysql_fetch_field(result);
 
5319
  mytest(field);
 
5320
  if (!opt_silent)
 
5321
  {
 
5322
    fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col1");
 
5323
    fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_select");
 
5324
  }
 
5325
  DIE_UNLESS(strcmp(field->name, "col1") == 0);
 
5326
  DIE_UNLESS(strcmp(field->table, "test_prep_select") == 0);
 
5327
 
 
5328
  field= mysql_fetch_field(result);
 
5329
  mytest(field);
 
5330
  if (!opt_silent)
 
5331
  {
 
5332
    fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col2");
 
5333
    fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_select");
 
5334
  }
 
5335
  DIE_UNLESS(strcmp(field->name, "col2") == 0);
 
5336
  DIE_UNLESS(strcmp(field->table, "test_prep_select") == 0);
 
5337
 
 
5338
  field= mysql_fetch_field(result);
 
5339
  mytest_r(field);
 
5340
 
 
5341
  mysql_free_result(result);
 
5342
  mysql_stmt_close(stmt);
 
5343
}
 
5344
#endif
 
5345
 
 
5346
 
 
5347
/* Test FUNCTION field info / DATE_FORMAT() table_name . */
 
5348
 
 
5349
static void test_func_fields()
 
5350
{
 
5351
  int        rc;
 
5352
  MYSQL_RES  *result;
 
5353
  MYSQL_FIELD *field;
 
5354
 
 
5355
  myheader("test_func_fields");
 
5356
 
 
5357
  rc= mysql_autocommit(mysql, TRUE);
 
5358
  myquery(rc);
 
5359
 
 
5360
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_dateformat");
 
5361
  myquery(rc);
 
5362
 
 
5363
  rc= mysql_query(mysql, "CREATE TABLE test_dateformat(id int, \
 
5364
                                                       ts timestamp)");
 
5365
  myquery(rc);
 
5366
 
 
5367
  rc= mysql_query(mysql, "INSERT INTO test_dateformat(id) values(10)");
 
5368
  myquery(rc);
 
5369
 
 
5370
  rc= mysql_query(mysql, "SELECT ts FROM test_dateformat");
 
5371
  myquery(rc);
 
5372
 
 
5373
  result= mysql_store_result(mysql);
 
5374
  mytest(result);
 
5375
 
 
5376
  field= mysql_fetch_field(result);
 
5377
  mytest(field);
 
5378
  if (!opt_silent)
 
5379
    fprintf(stdout, "\n table name: `%s` (expected: `%s`)", field->table,
 
5380
            "test_dateformat");
 
5381
  DIE_UNLESS(strcmp(field->table, "test_dateformat") == 0);
 
5382
 
 
5383
  field= mysql_fetch_field(result);
 
5384
  mytest_r(field); /* no more fields */
 
5385
 
 
5386
  mysql_free_result(result);
 
5387
 
 
5388
  /* DATE_FORMAT */
 
5389
  rc= mysql_query(mysql, "SELECT DATE_FORMAT(ts, '%Y') AS 'venu' FROM test_dateformat");
 
5390
  myquery(rc);
 
5391
 
 
5392
  result= mysql_store_result(mysql);
 
5393
  mytest(result);
 
5394
 
 
5395
  field= mysql_fetch_field(result);
 
5396
  mytest(field);
 
5397
  if (!opt_silent)
 
5398
    fprintf(stdout, "\n table name: `%s` (expected: `%s`)", field->table, "");
 
5399
  DIE_UNLESS(field->table[0] == '\0');
 
5400
 
 
5401
  field= mysql_fetch_field(result);
 
5402
  mytest_r(field); /* no more fields */
 
5403
 
 
5404
  mysql_free_result(result);
 
5405
 
 
5406
  /* FIELD ALIAS TEST */
 
5407
  rc= mysql_query(mysql, "SELECT DATE_FORMAT(ts, '%Y')  AS 'YEAR' FROM test_dateformat");
 
5408
  myquery(rc);
 
5409
 
 
5410
  result= mysql_store_result(mysql);
 
5411
  mytest(result);
 
5412
 
 
5413
  field= mysql_fetch_field(result);
 
5414
  mytest(field);
 
5415
  if (!opt_silent)
 
5416
  {
 
5417
    printf("\n field name: `%s` (expected: `%s`)", field->name, "YEAR");
 
5418
    printf("\n field org name: `%s` (expected: `%s`)", field->org_name, "");
 
5419
  }
 
5420
  DIE_UNLESS(strcmp(field->name, "YEAR") == 0);
 
5421
  DIE_UNLESS(field->org_name[0] == '\0');
 
5422
 
 
5423
  field= mysql_fetch_field(result);
 
5424
  mytest_r(field); /* no more fields */
 
5425
 
 
5426
  mysql_free_result(result);
 
5427
}
 
5428
 
 
5429
 
 
5430
/* Multiple stmts .. */
 
5431
 
 
5432
static void test_multi_stmt()
 
5433
{
 
5434
 
 
5435
  MYSQL_STMT  *stmt, *stmt1, *stmt2;
 
5436
  int         rc;
 
5437
  uint32      id;
 
5438
  char        name[50];
 
5439
  MYSQL_BIND  my_bind[2];
 
5440
  ulong       length[2];
 
5441
  my_bool     is_null[2];
 
5442
  myheader("test_multi_stmt");
 
5443
 
 
5444
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_multi_table");
 
5445
  myquery(rc);
 
5446
 
 
5447
  rc= mysql_query(mysql, "CREATE TABLE test_multi_table(id int, name char(20))");
 
5448
  myquery(rc);
 
5449
 
 
5450
  rc= mysql_query(mysql, "INSERT INTO test_multi_table values(10, 'mysql')");
 
5451
  myquery(rc);
 
5452
 
 
5453
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_multi_table "
 
5454
                                    "WHERE id= ?");
 
5455
  check_stmt(stmt);
 
5456
 
 
5457
  stmt2= mysql_simple_prepare(mysql, "UPDATE test_multi_table "
 
5458
                                     "SET name='updated' WHERE id=10");
 
5459
  check_stmt(stmt2);
 
5460
 
 
5461
  verify_param_count(stmt, 1);
 
5462
 
 
5463
  /*
 
5464
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
5465
    its members.
 
5466
  */
 
5467
  bzero((char*) my_bind, sizeof(my_bind));
 
5468
 
 
5469
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
5470
  my_bind[0].buffer= (void *)&id;
 
5471
  my_bind[0].is_null= &is_null[0];
 
5472
  my_bind[0].length= &length[0];
 
5473
  is_null[0]= 0;
 
5474
  length[0]= 0;
 
5475
 
 
5476
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
5477
  my_bind[1].buffer= (void *)name;
 
5478
  my_bind[1].buffer_length= sizeof(name);
 
5479
  my_bind[1].length= &length[1];
 
5480
  my_bind[1].is_null= &is_null[1];
 
5481
 
 
5482
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
5483
  check_execute(stmt, rc);
 
5484
 
 
5485
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
5486
  check_execute(stmt, rc);
 
5487
 
 
5488
  id= 10;
 
5489
  rc= mysql_stmt_execute(stmt);
 
5490
  check_execute(stmt, rc);
 
5491
 
 
5492
  id= 999;
 
5493
  rc= mysql_stmt_fetch(stmt);
 
5494
  check_execute(stmt, rc);
 
5495
 
 
5496
  if (!opt_silent)
 
5497
  {
 
5498
    fprintf(stdout, "\n int_data: %lu(%lu)", (ulong) id, length[0]);
 
5499
    fprintf(stdout, "\n str_data: %s(%lu)", name, length[1]);
 
5500
  }
 
5501
  DIE_UNLESS(id == 10);
 
5502
  DIE_UNLESS(strcmp(name, "mysql") == 0);
 
5503
 
 
5504
  rc= mysql_stmt_fetch(stmt);
 
5505
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
5506
 
 
5507
  /* alter the table schema now */
 
5508
  stmt1= mysql_simple_prepare(mysql, "DELETE FROM test_multi_table "
 
5509
                                     "WHERE id= ? AND "
 
5510
                                     "CONVERT(name USING utf8)=?");
 
5511
  check_stmt(stmt1);
 
5512
 
 
5513
  verify_param_count(stmt1, 2);
 
5514
 
 
5515
  rc= mysql_stmt_bind_param(stmt1, my_bind);
 
5516
  check_execute(stmt1, rc);
 
5517
 
 
5518
  rc= mysql_stmt_execute(stmt2);
 
5519
  check_execute(stmt2, rc);
 
5520
 
 
5521
  verify_st_affected_rows(stmt2, 1);
 
5522
 
 
5523
  rc= mysql_stmt_execute(stmt);
 
5524
  check_execute(stmt, rc);
 
5525
 
 
5526
  rc= mysql_stmt_fetch(stmt);
 
5527
  check_execute(stmt, rc);
 
5528
 
 
5529
  if (!opt_silent)
 
5530
  {
 
5531
    fprintf(stdout, "\n int_data: %lu(%lu)", (ulong) id, length[0]);
 
5532
    fprintf(stdout, "\n str_data: %s(%lu)", name, length[1]);
 
5533
  }
 
5534
  DIE_UNLESS(id == 10);
 
5535
  DIE_UNLESS(strcmp(name, "updated") == 0);
 
5536
 
 
5537
  rc= mysql_stmt_fetch(stmt);
 
5538
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
5539
 
 
5540
  rc= mysql_stmt_execute(stmt1);
 
5541
  check_execute(stmt1, rc);
 
5542
 
 
5543
  verify_st_affected_rows(stmt1, 1);
 
5544
 
 
5545
  mysql_stmt_close(stmt1);
 
5546
 
 
5547
  rc= mysql_stmt_execute(stmt);
 
5548
  check_execute(stmt, rc);
 
5549
 
 
5550
  rc= mysql_stmt_fetch(stmt);
 
5551
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
5552
 
 
5553
  rc= my_stmt_result("SELECT * FROM test_multi_table");
 
5554
  DIE_UNLESS(rc == 0);
 
5555
 
 
5556
  mysql_stmt_close(stmt);
 
5557
  mysql_stmt_close(stmt2);
 
5558
 
 
5559
}
 
5560
 
 
5561
 
 
5562
/* Test simple sample - manual */
 
5563
 
 
5564
static void test_manual_sample()
 
5565
{
 
5566
  unsigned int param_count;
 
5567
  MYSQL_STMT   *stmt;
 
5568
  short        small_data;
 
5569
  int          int_data;
 
5570
  int          rc;
 
5571
  char         str_data[50];
 
5572
  ulonglong    affected_rows;
 
5573
  MYSQL_BIND   my_bind[3];
 
5574
  my_bool      is_null;
 
5575
  char query[MAX_TEST_QUERY_LENGTH];
 
5576
 
 
5577
  myheader("test_manual_sample");
 
5578
 
 
5579
  /*
 
5580
    Sample which is incorporated directly in the manual under Prepared
 
5581
    statements section (Example from mysql_stmt_execute()
 
5582
  */
 
5583
 
 
5584
  mysql_autocommit(mysql, 1);
 
5585
  if (mysql_query(mysql, "DROP TABLE IF EXISTS test_table"))
 
5586
  {
 
5587
    fprintf(stderr, "\n drop table failed");
 
5588
    fprintf(stderr, "\n %s", mysql_error(mysql));
 
5589
    exit(1);
 
5590
  }
 
5591
  if (mysql_query(mysql, "CREATE TABLE test_table(col1 int, col2 varchar(50), \
 
5592
                                                 col3 smallint, \
 
5593
                                                 col4 timestamp)"))
 
5594
  {
 
5595
    fprintf(stderr, "\n create table failed");
 
5596
    fprintf(stderr, "\n %s", mysql_error(mysql));
 
5597
    exit(1);
 
5598
  }
 
5599
 
 
5600
  /* Prepare a insert query with 3 parameters */
 
5601
  strmov(query, "INSERT INTO test_table(col1, col2, col3) values(?, ?, ?)");
 
5602
  if (!(stmt= mysql_simple_prepare(mysql, query)))
 
5603
  {
 
5604
    fprintf(stderr, "\n prepare, insert failed");
 
5605
    fprintf(stderr, "\n %s", mysql_error(mysql));
 
5606
    exit(1);
 
5607
  }
 
5608
  if (!opt_silent)
 
5609
    fprintf(stdout, "\n prepare, insert successful");
 
5610
 
 
5611
  /* Get the parameter count from the statement */
 
5612
  param_count= mysql_stmt_param_count(stmt);
 
5613
 
 
5614
  if (!opt_silent)
 
5615
    fprintf(stdout, "\n total parameters in insert: %d", param_count);
 
5616
  if (param_count != 3) /* validate parameter count */
 
5617
  {
 
5618
    fprintf(stderr, "\n invalid parameter count returned by MySQL");
 
5619
    exit(1);
 
5620
  }
 
5621
 
 
5622
  /* Bind the data for the parameters */
 
5623
 
 
5624
  /*
 
5625
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
5626
    its members.
 
5627
  */
 
5628
  bzero((char*) my_bind, sizeof(my_bind));
 
5629
 
 
5630
  /* INTEGER PART */
 
5631
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
5632
  my_bind[0].buffer= (void *)&int_data;
 
5633
 
 
5634
  /* STRING PART */
 
5635
  my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
 
5636
  my_bind[1].buffer= (void *)str_data;
 
5637
  my_bind[1].buffer_length= sizeof(str_data);
 
5638
 
 
5639
  /* SMALLINT PART */
 
5640
  my_bind[2].buffer_type= MYSQL_TYPE_SHORT;
 
5641
  my_bind[2].buffer= (void *)&small_data;
 
5642
  my_bind[2].is_null= &is_null;
 
5643
  is_null= 0;
 
5644
 
 
5645
  /* Bind the buffers */
 
5646
  if (mysql_stmt_bind_param(stmt, my_bind))
 
5647
  {
 
5648
    fprintf(stderr, "\n param bind failed");
 
5649
    fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
 
5650
    exit(1);
 
5651
  }
 
5652
 
 
5653
  /* Specify the data */
 
5654
  int_data= 10;             /* integer */
 
5655
  strmov(str_data, "MySQL"); /* string  */
 
5656
 
 
5657
  /* INSERT SMALLINT data as NULL */
 
5658
  is_null= 1;
 
5659
 
 
5660
  /* Execute the insert statement - 1*/
 
5661
  if (mysql_stmt_execute(stmt))
 
5662
  {
 
5663
    fprintf(stderr, "\n execute 1 failed");
 
5664
    fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
 
5665
    exit(1);
 
5666
  }
 
5667
 
 
5668
  /* Get the total rows affected */
 
5669
  affected_rows= mysql_stmt_affected_rows(stmt);
 
5670
 
 
5671
  if (!opt_silent)
 
5672
    fprintf(stdout, "\n total affected rows: %ld", (ulong) affected_rows);
 
5673
  if (affected_rows != 1) /* validate affected rows */
 
5674
  {
 
5675
    fprintf(stderr, "\n invalid affected rows by MySQL");
 
5676
    exit(1);
 
5677
  }
 
5678
 
 
5679
  /* Re-execute the insert, by changing the values */
 
5680
  int_data= 1000;
 
5681
  strmov(str_data, "The most popular open source database");
 
5682
  small_data= 1000;         /* smallint */
 
5683
  is_null= 0;               /* reset */
 
5684
 
 
5685
  /* Execute the insert statement - 2*/
 
5686
  if (mysql_stmt_execute(stmt))
 
5687
  {
 
5688
    fprintf(stderr, "\n execute 2 failed");
 
5689
    fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
 
5690
    exit(1);
 
5691
  }
 
5692
 
 
5693
  /* Get the total rows affected */
 
5694
  affected_rows= mysql_stmt_affected_rows(stmt);
 
5695
 
 
5696
  if (!opt_silent)
 
5697
    fprintf(stdout, "\n total affected rows: %ld", (ulong) affected_rows);
 
5698
  if (affected_rows != 1) /* validate affected rows */
 
5699
  {
 
5700
    fprintf(stderr, "\n invalid affected rows by MySQL");
 
5701
    exit(1);
 
5702
  }
 
5703
 
 
5704
  /* Close the statement */
 
5705
  if (mysql_stmt_close(stmt))
 
5706
  {
 
5707
    fprintf(stderr, "\n failed while closing the statement");
 
5708
    fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
 
5709
    exit(1);
 
5710
  }
 
5711
  rc= my_stmt_result("SELECT * FROM test_table");
 
5712
  DIE_UNLESS(rc == 2);
 
5713
 
 
5714
  /* DROP THE TABLE */
 
5715
  if (mysql_query(mysql, "DROP TABLE test_table"))
 
5716
  {
 
5717
    fprintf(stderr, "\n drop table failed");
 
5718
    fprintf(stderr, "\n %s", mysql_error(mysql));
 
5719
    exit(1);
 
5720
  }
 
5721
  if (!opt_silent)
 
5722
    fprintf(stdout, "Success !!!");
 
5723
}
 
5724
 
 
5725
 
 
5726
/* Test alter table scenario in the middle of prepare */
 
5727
 
 
5728
static void test_prepare_alter()
 
5729
{
 
5730
  MYSQL_STMT  *stmt;
 
5731
  int         rc, id;
 
5732
  MYSQL_BIND  my_bind[1];
 
5733
  my_bool     is_null;
 
5734
 
 
5735
  myheader("test_prepare_alter");
 
5736
 
 
5737
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_alter");
 
5738
  myquery(rc);
 
5739
 
 
5740
  rc= mysql_query(mysql, "CREATE TABLE test_prep_alter(id int, name char(20))");
 
5741
  myquery(rc);
 
5742
 
 
5743
  rc= mysql_query(mysql, "INSERT INTO test_prep_alter values(10, 'venu'), (20, 'mysql')");
 
5744
  myquery(rc);
 
5745
 
 
5746
  stmt= mysql_simple_prepare(mysql, "INSERT INTO test_prep_alter VALUES(?, 'monty')");
 
5747
  check_stmt(stmt);
 
5748
 
 
5749
  verify_param_count(stmt, 1);
 
5750
 
 
5751
  /*
 
5752
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
5753
    its members.
 
5754
  */
 
5755
  bzero((char*) my_bind, sizeof(my_bind));
 
5756
 
 
5757
  is_null= 0;
 
5758
  my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
 
5759
  my_bind[0].buffer= (void *)&id;
 
5760
  my_bind[0].is_null= &is_null;
 
5761
 
 
5762
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
5763
  check_execute(stmt, rc);
 
5764
 
 
5765
  id= 30;
 
5766
  rc= mysql_stmt_execute(stmt);
 
5767
  check_execute(stmt, rc);
 
5768
 
 
5769
  if (thread_query((char *)"ALTER TABLE test_prep_alter change id id_new varchar(20)"))
 
5770
    exit(1);
 
5771
 
 
5772
  is_null= 1;
 
5773
  rc= mysql_stmt_execute(stmt);
 
5774
  check_execute(stmt, rc);
 
5775
 
 
5776
  rc= my_stmt_result("SELECT * FROM test_prep_alter");
 
5777
  DIE_UNLESS(rc == 4);
 
5778
 
 
5779
  mysql_stmt_close(stmt);
 
5780
}
 
5781
 
 
5782
 
 
5783
/* Test the support of multi-statement executions */
 
5784
 
 
5785
static void test_multi_statements()
 
5786
{
 
5787
  MYSQL *mysql_local;
 
5788
  MYSQL_RES *result;
 
5789
  int    rc;
 
5790
 
 
5791
  const char *query= "\
 
5792
DROP TABLE IF EXISTS test_multi_tab;\
 
5793
CREATE TABLE test_multi_tab(id int, name char(20));\
 
5794
INSERT INTO test_multi_tab(id) VALUES(10), (20);\
 
5795
INSERT INTO test_multi_tab VALUES(20, 'insert;comma');\
 
5796
SELECT * FROM test_multi_tab;\
 
5797
UPDATE test_multi_tab SET name='new;name' WHERE id=20;\
 
5798
DELETE FROM test_multi_tab WHERE name='new;name';\
 
5799
SELECT * FROM test_multi_tab;\
 
5800
DELETE FROM test_multi_tab WHERE id=10;\
 
5801
SELECT * FROM test_multi_tab;\
 
5802
DROP TABLE test_multi_tab;\
 
5803
select 1;\
 
5804
DROP TABLE IF EXISTS test_multi_tab";
 
5805
  uint count, exp_value;
 
5806
  uint rows[]= {0, 0, 2, 1, 3, 2, 2, 1, 1, 0, 0, 1, 0};
 
5807
 
 
5808
  myheader("test_multi_statements");
 
5809
 
 
5810
  /*
 
5811
    First test that we get an error for multi statements
 
5812
    (Because default connection is not opened with CLIENT_MULTI_STATEMENTS)
 
5813
  */
 
5814
  rc= mysql_query(mysql, query); /* syntax error */
 
5815
  myquery_r(rc);
 
5816
 
 
5817
  rc= mysql_next_result(mysql);
 
5818
  DIE_UNLESS(rc == -1);
 
5819
  rc= mysql_more_results(mysql);
 
5820
  DIE_UNLESS(rc == 0);
 
5821
 
 
5822
  if (!(mysql_local= mysql_init(NULL)))
 
5823
  {
 
5824
    fprintf(stdout, "\n mysql_init() failed");
 
5825
    exit(1);
 
5826
  }
 
5827
 
 
5828
  /* Create connection that supports multi statements */
 
5829
  if (!(mysql_real_connect(mysql_local, opt_host, opt_user,
 
5830
                           opt_password, current_db, opt_port,
 
5831
                           opt_unix_socket, CLIENT_MULTI_STATEMENTS)))
 
5832
  {
 
5833
    fprintf(stdout, "\n connection failed(%s)", mysql_error(mysql_local));
 
5834
    exit(1);
 
5835
  }
 
5836
  mysql_local->reconnect= 1;
 
5837
 
 
5838
  rc= mysql_query(mysql_local, query);
 
5839
  myquery(rc);
 
5840
 
 
5841
  for (count= 0 ; count < array_elements(rows) ; count++)
 
5842
  {
 
5843
    if (!opt_silent)
 
5844
      fprintf(stdout, "\n Query %d: ", count);
 
5845
    if ((result= mysql_store_result(mysql_local)))
 
5846
    {
 
5847
      (void) my_process_result_set(result);
 
5848
      mysql_free_result(result);
 
5849
    }
 
5850
    else if (!opt_silent)
 
5851
      fprintf(stdout, "OK, %ld row(s) affected, %ld warning(s)\n",
 
5852
              (ulong) mysql_affected_rows(mysql_local),
 
5853
              (ulong) mysql_warning_count(mysql_local));
 
5854
 
 
5855
    exp_value= (uint) mysql_affected_rows(mysql_local);
 
5856
    if (rows[count] !=  exp_value)
 
5857
    {
 
5858
      fprintf(stderr, "row %d  had affected rows: %d, should be %d\n",
 
5859
              count, exp_value, rows[count]);
 
5860
      exit(1);
 
5861
    }
 
5862
    if (count != array_elements(rows) -1)
 
5863
    {
 
5864
      if (!(rc= mysql_more_results(mysql_local)))
 
5865
      {
 
5866
        fprintf(stdout,
 
5867
                "mysql_more_result returned wrong value: %d for row %d\n",
 
5868
                rc, count);
 
5869
        exit(1);
 
5870
      }
 
5871
      if ((rc= mysql_next_result(mysql_local)))
 
5872
      {
 
5873
        exp_value= mysql_errno(mysql_local);
 
5874
 
 
5875
        exit(1);
 
5876
      }
 
5877
    }
 
5878
    else
 
5879
    {
 
5880
      rc= mysql_more_results(mysql_local);
 
5881
      DIE_UNLESS(rc == 0);
 
5882
      rc= mysql_next_result(mysql_local);
 
5883
      DIE_UNLESS(rc == -1);
 
5884
    }
 
5885
  }
 
5886
 
 
5887
  /* check that errors abort multi statements */
 
5888
 
 
5889
  rc= mysql_query(mysql_local, "select 1+1+a;select 1+1");
 
5890
  myquery_r(rc);
 
5891
  rc= mysql_more_results(mysql_local);
 
5892
  DIE_UNLESS(rc == 0);
 
5893
  rc= mysql_next_result(mysql_local);
 
5894
  DIE_UNLESS(rc == -1);
 
5895
 
 
5896
  rc= mysql_query(mysql_local, "select 1+1;select 1+1+a;select 1");
 
5897
  myquery(rc);
 
5898
  result= mysql_store_result(mysql_local);
 
5899
  mytest(result);
 
5900
  mysql_free_result(result);
 
5901
  rc= mysql_more_results(mysql_local);
 
5902
  DIE_UNLESS(rc == 1);
 
5903
  rc= mysql_next_result(mysql_local);
 
5904
  DIE_UNLESS(rc > 0);
 
5905
 
 
5906
  /*
 
5907
    Ensure that we can now do a simple query (this checks that the server is
 
5908
    not trying to send us the results for the last 'select 1'
 
5909
  */
 
5910
  rc= mysql_query(mysql_local, "select 1+1+1");
 
5911
  myquery(rc);
 
5912
  result= mysql_store_result(mysql_local);
 
5913
  mytest(result);
 
5914
  (void) my_process_result_set(result);
 
5915
  mysql_free_result(result);
 
5916
 
 
5917
  /*
 
5918
    Check if errors in one of the queries handled properly.
 
5919
  */
 
5920
  rc= mysql_query(mysql_local, "select 1; select * from not_existing_table");
 
5921
  myquery(rc);
 
5922
  result= mysql_store_result(mysql_local);
 
5923
  mysql_free_result(result);
 
5924
 
 
5925
  rc= mysql_next_result(mysql_local);
 
5926
  DIE_UNLESS(rc > 0);
 
5927
 
 
5928
  rc= mysql_next_result(mysql_local);
 
5929
  DIE_UNLESS(rc < 0);
 
5930
 
 
5931
  mysql_close(mysql_local);
 
5932
}
 
5933
 
 
5934
 
 
5935
/*
 
5936
  Check that Prepared statement cannot contain several
 
5937
  SQL statements
 
5938
*/
 
5939
 
 
5940
static void test_prepare_multi_statements()
 
5941
{
 
5942
  MYSQL *mysql_local;
 
5943
  MYSQL_STMT *stmt;
 
5944
  char query[MAX_TEST_QUERY_LENGTH];
 
5945
  myheader("test_prepare_multi_statements");
 
5946
 
 
5947
  if (!(mysql_local= mysql_init(NULL)))
 
5948
  {
 
5949
    fprintf(stderr, "\n mysql_init() failed");
 
5950
    exit(1);
 
5951
  }
 
5952
 
 
5953
  if (!(mysql_real_connect(mysql_local, opt_host, opt_user,
 
5954
                           opt_password, current_db, opt_port,
 
5955
                           opt_unix_socket, CLIENT_MULTI_STATEMENTS)))
 
5956
  {
 
5957
    fprintf(stderr, "\n connection failed(%s)", mysql_error(mysql_local));
 
5958
    exit(1);
 
5959
  }
 
5960
  mysql_local->reconnect= 1;
 
5961
  strmov(query, "select 1; select 'another value'");
 
5962
  stmt= mysql_simple_prepare(mysql_local, query);
 
5963
  check_stmt_r(stmt);
 
5964
  mysql_close(mysql_local);
 
5965
}
 
5966
 
 
5967
 
 
5968
/* Test simple bind store result */
 
5969
 
 
5970
static void test_store_result()
 
5971
{
 
5972
  MYSQL_STMT *stmt;
 
5973
  int        rc;
 
5974
  int32      nData;
 
5975
  char       szData[100];
 
5976
  MYSQL_BIND my_bind[2];
 
5977
  ulong      length, length1;
 
5978
  my_bool    is_null[2];
 
5979
 
 
5980
  myheader("test_store_result");
 
5981
 
 
5982
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
 
5983
  myquery(rc);
 
5984
 
 
5985
  rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
 
5986
  myquery(rc);
 
5987
 
 
5988
  rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
 
5989
  myquery(rc);
 
5990
 
 
5991
  rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
 
5992
  myquery(rc);
 
5993
 
 
5994
  rc= mysql_commit(mysql);
 
5995
  myquery(rc);
 
5996
 
 
5997
  /* fetch */
 
5998
  bzero((char*) my_bind, sizeof(my_bind));
 
5999
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
6000
  my_bind[0].buffer= (void *) &nData;       /* integer data */
 
6001
  my_bind[0].length= &length;
 
6002
  my_bind[0].is_null= &is_null[0];
 
6003
 
 
6004
  length= 0;
 
6005
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
6006
  my_bind[1].buffer= szData;                /* string data */
 
6007
  my_bind[1].buffer_length= sizeof(szData);
 
6008
  my_bind[1].length= &length1;
 
6009
  my_bind[1].is_null= &is_null[1];
 
6010
  length1= 0;
 
6011
 
 
6012
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_store_result");
 
6013
  check_stmt(stmt);
 
6014
 
 
6015
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
6016
  check_execute(stmt, rc);
 
6017
 
 
6018
  rc= mysql_stmt_execute(stmt);
 
6019
  check_execute(stmt, rc);
 
6020
 
 
6021
  rc= mysql_stmt_store_result(stmt);
 
6022
  check_execute(stmt, rc);
 
6023
 
 
6024
  rc= mysql_stmt_fetch(stmt);
 
6025
  check_execute(stmt, rc);
 
6026
 
 
6027
  if (!opt_silent)
 
6028
    fprintf(stdout, "\n row 1: %ld, %s(%lu)", (long) nData, szData, length1);
 
6029
  DIE_UNLESS(nData == 10);
 
6030
  DIE_UNLESS(strcmp(szData, "venu") == 0);
 
6031
  DIE_UNLESS(length1 == 4);
 
6032
 
 
6033
  rc= mysql_stmt_fetch(stmt);
 
6034
  check_execute(stmt, rc);
 
6035
 
 
6036
  if (!opt_silent)
 
6037
    fprintf(stdout, "\n row 2: %ld, %s(%lu)", (long) nData, szData, length1);
 
6038
  DIE_UNLESS(nData == 20);
 
6039
  DIE_UNLESS(strcmp(szData, "mysql") == 0);
 
6040
  DIE_UNLESS(length1 == 5);
 
6041
 
 
6042
  length= 99;
 
6043
  rc= mysql_stmt_fetch(stmt);
 
6044
  check_execute(stmt, rc);
 
6045
 
 
6046
  if (!opt_silent && is_null[0])
 
6047
    fprintf(stdout, "\n row 3: NULL, %s(%lu)", szData, length1);
 
6048
  DIE_UNLESS(is_null[0]);
 
6049
  DIE_UNLESS(strcmp(szData, "monty") == 0);
 
6050
  DIE_UNLESS(length1 == 5);
 
6051
 
 
6052
  rc= mysql_stmt_fetch(stmt);
 
6053
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
6054
 
 
6055
  rc= mysql_stmt_execute(stmt);
 
6056
  check_execute(stmt, rc);
 
6057
 
 
6058
  rc= mysql_stmt_store_result(stmt);
 
6059
  check_execute(stmt, rc);
 
6060
 
 
6061
  rc= mysql_stmt_fetch(stmt);
 
6062
  check_execute(stmt, rc);
 
6063
 
 
6064
  if (!opt_silent)
 
6065
    fprintf(stdout, "\n row 1: %ld, %s(%lu)", (long) nData, szData, length1);
 
6066
  DIE_UNLESS(nData == 10);
 
6067
  DIE_UNLESS(strcmp(szData, "venu") == 0);
 
6068
  DIE_UNLESS(length1 == 4);
 
6069
 
 
6070
  rc= mysql_stmt_fetch(stmt);
 
6071
  check_execute(stmt, rc);
 
6072
 
 
6073
  if (!opt_silent)
 
6074
    fprintf(stdout, "\n row 2: %ld, %s(%lu)", (long) nData, szData, length1);
 
6075
  DIE_UNLESS(nData == 20);
 
6076
  DIE_UNLESS(strcmp(szData, "mysql") == 0);
 
6077
  DIE_UNLESS(length1 == 5);
 
6078
 
 
6079
  length= 99;
 
6080
  rc= mysql_stmt_fetch(stmt);
 
6081
  check_execute(stmt, rc);
 
6082
 
 
6083
  if (!opt_silent && is_null[0])
 
6084
    fprintf(stdout, "\n row 3: NULL, %s(%lu)", szData, length1);
 
6085
  DIE_UNLESS(is_null[0]);
 
6086
  DIE_UNLESS(strcmp(szData, "monty") == 0);
 
6087
  DIE_UNLESS(length1 == 5);
 
6088
 
 
6089
  rc= mysql_stmt_fetch(stmt);
 
6090
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
6091
 
 
6092
  mysql_stmt_close(stmt);
 
6093
}
 
6094
 
 
6095
 
 
6096
/* Test simple bind store result */
 
6097
 
 
6098
static void test_store_result1()
 
6099
{
 
6100
  MYSQL_STMT *stmt;
 
6101
  int        rc;
 
6102
 
 
6103
  myheader("test_store_result1");
 
6104
 
 
6105
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
 
6106
  myquery(rc);
 
6107
 
 
6108
  rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
 
6109
  myquery(rc);
 
6110
 
 
6111
  rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
 
6112
  myquery(rc);
 
6113
 
 
6114
  rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
 
6115
  myquery(rc);
 
6116
 
 
6117
  rc= mysql_commit(mysql);
 
6118
  myquery(rc);
 
6119
 
 
6120
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_store_result");
 
6121
  check_stmt(stmt);
 
6122
 
 
6123
  rc= mysql_stmt_execute(stmt);
 
6124
  check_execute(stmt, rc);
 
6125
 
 
6126
  rc= mysql_stmt_store_result(stmt);
 
6127
  check_execute(stmt, rc);
 
6128
 
 
6129
  rc= 0;
 
6130
  while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
 
6131
    rc++;
 
6132
  if (!opt_silent)
 
6133
    fprintf(stdout, "\n total rows: %d", rc);
 
6134
  DIE_UNLESS(rc == 3);
 
6135
 
 
6136
  rc= mysql_stmt_execute(stmt);
 
6137
  check_execute(stmt, rc);
 
6138
 
 
6139
  rc= mysql_stmt_store_result(stmt);
 
6140
  check_execute(stmt, rc);
 
6141
 
 
6142
  rc= 0;
 
6143
  while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
 
6144
    rc++;
 
6145
  if (!opt_silent)
 
6146
    fprintf(stdout, "\n total rows: %d", rc);
 
6147
  DIE_UNLESS(rc == 3);
 
6148
 
 
6149
  mysql_stmt_close(stmt);
 
6150
}
 
6151
 
 
6152
 
 
6153
/* Another test for bind and store result */
 
6154
 
 
6155
static void test_store_result2()
 
6156
{
 
6157
  MYSQL_STMT *stmt;
 
6158
  int        rc;
 
6159
  int        nData;
 
6160
  ulong      length;
 
6161
  MYSQL_BIND my_bind[1];
 
6162
  char query[MAX_TEST_QUERY_LENGTH];
 
6163
 
 
6164
  myheader("test_store_result2");
 
6165
 
 
6166
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
 
6167
  myquery(rc);
 
6168
 
 
6169
  rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
 
6170
  myquery(rc);
 
6171
 
 
6172
  rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
 
6173
  myquery(rc);
 
6174
 
 
6175
  rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
 
6176
  myquery(rc);
 
6177
 
 
6178
  rc= mysql_commit(mysql);
 
6179
  myquery(rc);
 
6180
 
 
6181
  /*
 
6182
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
6183
    its members.
 
6184
  */
 
6185
  bzero((char*) my_bind, sizeof(my_bind));
 
6186
 
 
6187
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
6188
  my_bind[0].buffer= (void *) &nData;      /* integer data */
 
6189
  my_bind[0].length= &length;
 
6190
  my_bind[0].is_null= 0;
 
6191
 
 
6192
  strmov((char *)query , "SELECT col1 FROM test_store_result where col1= ?");
 
6193
  stmt= mysql_simple_prepare(mysql, query);
 
6194
  check_stmt(stmt);
 
6195
 
 
6196
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
6197
  check_execute(stmt, rc);
 
6198
 
 
6199
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
6200
  check_execute(stmt, rc);
 
6201
 
 
6202
  nData= 10; length= 0;
 
6203
  rc= mysql_stmt_execute(stmt);
 
6204
  check_execute(stmt, rc);
 
6205
 
 
6206
  nData= 0;
 
6207
  rc= mysql_stmt_store_result(stmt);
 
6208
  check_execute(stmt, rc);
 
6209
 
 
6210
  rc= mysql_stmt_fetch(stmt);
 
6211
  check_execute(stmt, rc);
 
6212
 
 
6213
  if (!opt_silent)
 
6214
    fprintf(stdout, "\n row 1: %d", nData);
 
6215
  DIE_UNLESS(nData == 10);
 
6216
 
 
6217
  rc= mysql_stmt_fetch(stmt);
 
6218
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
6219
 
 
6220
  nData= 20;
 
6221
  rc= mysql_stmt_execute(stmt);
 
6222
  check_execute(stmt, rc);
 
6223
 
 
6224
  nData= 0;
 
6225
  rc= mysql_stmt_store_result(stmt);
 
6226
  check_execute(stmt, rc);
 
6227
 
 
6228
  rc= mysql_stmt_fetch(stmt);
 
6229
  check_execute(stmt, rc);
 
6230
 
 
6231
  if (!opt_silent)
 
6232
    fprintf(stdout, "\n row 1: %d", nData);
 
6233
  DIE_UNLESS(nData == 20);
 
6234
 
 
6235
  rc= mysql_stmt_fetch(stmt);
 
6236
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
6237
  mysql_stmt_close(stmt);
 
6238
}
 
6239
 
 
6240
 
 
6241
/* Test simple subselect prepare */
 
6242
 
 
6243
static void test_subselect()
 
6244
{
 
6245
 
 
6246
  MYSQL_STMT *stmt;
 
6247
  int        rc, id;
 
6248
  MYSQL_BIND my_bind[1];
 
6249
  DBUG_ENTER("test_subselect");
 
6250
 
 
6251
  myheader("test_subselect");
 
6252
 
 
6253
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_sub1");
 
6254
  myquery(rc);
 
6255
 
 
6256
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_sub2");
 
6257
  myquery(rc);
 
6258
 
 
6259
  rc= mysql_query(mysql, "CREATE TABLE test_sub1(id int)");
 
6260
  myquery(rc);
 
6261
 
 
6262
  rc= mysql_query(mysql, "CREATE TABLE test_sub2(id int, id1 int)");
 
6263
  myquery(rc);
 
6264
 
 
6265
  rc= mysql_query(mysql, "INSERT INTO test_sub1 values(2)");
 
6266
  myquery(rc);
 
6267
 
 
6268
  rc= mysql_query(mysql, "INSERT INTO test_sub2 VALUES(1, 7), (2, 7)");
 
6269
  myquery(rc);
 
6270
 
 
6271
  rc= mysql_commit(mysql);
 
6272
  myquery(rc);
 
6273
 
 
6274
  /* fetch */
 
6275
  /*
 
6276
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
6277
    its members.
 
6278
  */
 
6279
  bzero((char*) my_bind, sizeof(my_bind));
 
6280
 
 
6281
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
6282
  my_bind[0].buffer= (void *) &id;
 
6283
  my_bind[0].length= 0;
 
6284
  my_bind[0].is_null= 0;
 
6285
 
 
6286
  stmt= mysql_simple_prepare(mysql, "INSERT INTO test_sub2(id) SELECT * FROM test_sub1 WHERE id= ?");
 
6287
  check_stmt(stmt);
 
6288
 
 
6289
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
6290
  check_execute(stmt, rc);
 
6291
 
 
6292
  id= 2;
 
6293
  rc= mysql_stmt_execute(stmt);
 
6294
  check_execute(stmt, rc);
 
6295
 
 
6296
  verify_st_affected_rows(stmt, 1);
 
6297
 
 
6298
  id= 9;
 
6299
  rc= mysql_stmt_execute(stmt);
 
6300
  check_execute(stmt, rc);
 
6301
 
 
6302
  verify_st_affected_rows(stmt, 0);
 
6303
 
 
6304
  mysql_stmt_close(stmt);
 
6305
 
 
6306
  rc= my_stmt_result("SELECT * FROM test_sub2");
 
6307
  DIE_UNLESS(rc == 3);
 
6308
 
 
6309
  rc= my_stmt_result("SELECT ROW(1, 7) IN (select id, id1 "
 
6310
                     "from test_sub2 WHERE id1= 8)");
 
6311
  DIE_UNLESS(rc == 1);
 
6312
  rc= my_stmt_result("SELECT ROW(1, 7) IN (select id, id1 "
 
6313
                     "from test_sub2 WHERE id1= 7)");
 
6314
  DIE_UNLESS(rc == 1);
 
6315
 
 
6316
  stmt= mysql_simple_prepare(mysql, ("SELECT ROW(1, 7) IN (select id, id1 "
 
6317
                                     "from test_sub2 WHERE id1= ?)"));
 
6318
  check_stmt(stmt);
 
6319
 
 
6320
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
6321
  check_execute(stmt, rc);
 
6322
 
 
6323
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
6324
  check_execute(stmt, rc);
 
6325
 
 
6326
  id= 7;
 
6327
  rc= mysql_stmt_execute(stmt);
 
6328
  check_execute(stmt, rc);
 
6329
 
 
6330
  rc= mysql_stmt_fetch(stmt);
 
6331
  check_execute(stmt, rc);
 
6332
 
 
6333
  if (!opt_silent)
 
6334
    fprintf(stdout, "\n row 1: %d", id);
 
6335
  DIE_UNLESS(id == 1);
 
6336
 
 
6337
  rc= mysql_stmt_fetch(stmt);
 
6338
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
6339
 
 
6340
  id= 8;
 
6341
  rc= mysql_stmt_execute(stmt);
 
6342
  check_execute(stmt, rc);
 
6343
 
 
6344
  rc= mysql_stmt_fetch(stmt);
 
6345
  check_execute(stmt, rc);
 
6346
 
 
6347
  if (!opt_silent)
 
6348
    fprintf(stdout, "\n row 1: %d", id);
 
6349
  DIE_UNLESS(id == 0);
 
6350
 
 
6351
  rc= mysql_stmt_fetch(stmt);
 
6352
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
6353
 
 
6354
  mysql_stmt_close(stmt);
 
6355
  DBUG_VOID_RETURN;
 
6356
}
 
6357
 
 
6358
 
 
6359
/*
 
6360
  Generalized conversion routine to handle DATE, TIME and DATETIME
 
6361
  conversion using MYSQL_TIME structure
 
6362
*/
 
6363
 
 
6364
static void test_bind_date_conv(uint row_count)
 
6365
{
 
6366
  MYSQL_STMT   *stmt= 0;
 
6367
  uint         rc, i, count= row_count;
 
6368
  ulong        length[4];
 
6369
  MYSQL_BIND   my_bind[4];
 
6370
  my_bool      is_null[4]= {0};
 
6371
  MYSQL_TIME   tm[4];
 
6372
  ulong        second_part;
 
6373
  uint         year, month, day, hour, minute, sec;
 
6374
 
 
6375
  stmt= mysql_simple_prepare(mysql, "INSERT INTO test_date VALUES(?, ?, ?, ?)");
 
6376
  check_stmt(stmt);
 
6377
 
 
6378
  verify_param_count(stmt, 4);
 
6379
 
 
6380
  /*
 
6381
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
6382
    its members.
 
6383
  */
 
6384
  bzero((char*) my_bind, sizeof(my_bind));
 
6385
 
 
6386
  my_bind[0].buffer_type= MYSQL_TYPE_TIMESTAMP;
 
6387
  my_bind[1].buffer_type= MYSQL_TYPE_TIME;
 
6388
  my_bind[2].buffer_type= MYSQL_TYPE_DATETIME;
 
6389
  my_bind[3].buffer_type= MYSQL_TYPE_DATE;
 
6390
 
 
6391
  for (i= 0; i < (int) array_elements(my_bind); i++)
 
6392
  {
 
6393
    my_bind[i].buffer= (void *) &tm[i];
 
6394
    my_bind[i].is_null= &is_null[i];
 
6395
    my_bind[i].length= &length[i];
 
6396
    my_bind[i].buffer_length= 30;
 
6397
    length[i]= 20;
 
6398
  }
 
6399
 
 
6400
  second_part= 0;
 
6401
 
 
6402
  year= 2000;
 
6403
  month= 01;
 
6404
  day= 10;
 
6405
 
 
6406
  hour= 11;
 
6407
  minute= 16;
 
6408
  sec= 20;
 
6409
 
 
6410
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
6411
  check_execute(stmt, rc);
 
6412
 
 
6413
  for (count= 0; count < row_count; count++)
 
6414
  {
 
6415
    for (i= 0; i < (int) array_elements(my_bind); i++)
 
6416
    {
 
6417
      tm[i].neg= 0;
 
6418
      tm[i].second_part= second_part+count;
 
6419
      if (my_bind[i].buffer_type != MYSQL_TYPE_TIME)
 
6420
      {
 
6421
        tm[i].year= year+count;
 
6422
        tm[i].month= month+count;
 
6423
        tm[i].day= day+count;
 
6424
      }
 
6425
      else
 
6426
        tm[i].year= tm[i].month= tm[i].day= 0;
 
6427
      if (my_bind[i].buffer_type != MYSQL_TYPE_DATE)
 
6428
      {
 
6429
        tm[i].hour= hour+count;
 
6430
        tm[i].minute= minute+count;
 
6431
        tm[i].second= sec+count;
 
6432
      }
 
6433
      else
 
6434
        tm[i].hour= tm[i].minute= tm[i].second= 0;
 
6435
    }
 
6436
    rc= mysql_stmt_execute(stmt);
 
6437
    check_execute(stmt, rc);
 
6438
  }
 
6439
 
 
6440
  rc= mysql_commit(mysql);
 
6441
  myquery(rc);
 
6442
 
 
6443
  mysql_stmt_close(stmt);
 
6444
 
 
6445
  rc= my_stmt_result("SELECT * FROM test_date");
 
6446
  DIE_UNLESS(row_count == rc);
 
6447
 
 
6448
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_date");
 
6449
  check_stmt(stmt);
 
6450
 
 
6451
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
6452
  check_execute(stmt, rc);
 
6453
 
 
6454
  rc= mysql_stmt_execute(stmt);
 
6455
  check_execute(stmt, rc);
 
6456
 
 
6457
  rc= mysql_stmt_store_result(stmt);
 
6458
  check_execute(stmt, rc);
 
6459
 
 
6460
  for (count= 0; count < row_count; count++)
 
6461
  {
 
6462
    rc= mysql_stmt_fetch(stmt);
 
6463
    DIE_UNLESS(rc == 0 || rc == MYSQL_DATA_TRUNCATED);
 
6464
 
 
6465
    if (!opt_silent)
 
6466
      fprintf(stdout, "\n");
 
6467
    for (i= 0; i < array_elements(my_bind); i++)
 
6468
    {
 
6469
      if (!opt_silent)
 
6470
        fprintf(stdout, "\ntime[%d]: %02d-%02d-%02d %02d:%02d:%02d.%02lu",
 
6471
                i, tm[i].year, tm[i].month, tm[i].day,
 
6472
                tm[i].hour, tm[i].minute, tm[i].second,
 
6473
                tm[i].second_part);
 
6474
      DIE_UNLESS(tm[i].year == 0 || tm[i].year == year+count);
 
6475
      DIE_UNLESS(tm[i].month == 0 || tm[i].month == month+count);
 
6476
      DIE_UNLESS(tm[i].day == 0 || tm[i].day == day+count);
 
6477
 
 
6478
      DIE_UNLESS(tm[i].hour == 0 || tm[i].hour == hour+count);
 
6479
      DIE_UNLESS(tm[i].minute == 0 || tm[i].minute == minute+count);
 
6480
      DIE_UNLESS(tm[i].second == 0 || tm[i].second == sec+count);
 
6481
      DIE_UNLESS(tm[i].second_part == 0 ||
 
6482
                 tm[i].second_part == second_part+count);
 
6483
    }
 
6484
  }
 
6485
  rc= mysql_stmt_fetch(stmt);
 
6486
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
6487
 
 
6488
  mysql_stmt_close(stmt);
 
6489
}
 
6490
 
 
6491
 
 
6492
/* Test DATE, TIME, DATETIME and TS with MYSQL_TIME conversion */
 
6493
 
 
6494
static void test_date()
 
6495
{
 
6496
  int        rc;
 
6497
 
 
6498
  myheader("test_date");
 
6499
 
 
6500
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
 
6501
  myquery(rc);
 
6502
 
 
6503
  rc= mysql_query(mysql, "CREATE TABLE test_date(c1 TIMESTAMP, \
 
6504
                                                 c2 TIME, \
 
6505
                                                 c3 DATETIME, \
 
6506
                                                 c4 DATE)");
 
6507
 
 
6508
  myquery(rc);
 
6509
 
 
6510
  test_bind_date_conv(5);
 
6511
}
 
6512
 
 
6513
 
 
6514
/* Test all time types to DATE and DATE to all types */
 
6515
 
 
6516
static void test_date_date()
 
6517
{
 
6518
  int        rc;
 
6519
 
 
6520
  myheader("test_date_date");
 
6521
 
 
6522
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
 
6523
  myquery(rc);
 
6524
 
 
6525
  rc= mysql_query(mysql, "CREATE TABLE test_date(c1 DATE, \
 
6526
                                                 c2 DATE, \
 
6527
                                                 c3 DATE, \
 
6528
                                                 c4 DATE)");
 
6529
 
 
6530
  myquery(rc);
 
6531
 
 
6532
  test_bind_date_conv(3);
 
6533
}
 
6534
 
 
6535
 
 
6536
/* Test all time types to TIME and TIME to all types */
 
6537
 
 
6538
static void test_date_time()
 
6539
{
 
6540
  int        rc;
 
6541
 
 
6542
  myheader("test_date_time");
 
6543
 
 
6544
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
 
6545
  myquery(rc);
 
6546
 
 
6547
  rc= mysql_query(mysql, "CREATE TABLE test_date(c1 TIME, \
 
6548
                                                 c2 TIME, \
 
6549
                                                 c3 TIME, \
 
6550
                                                 c4 TIME)");
 
6551
 
 
6552
  myquery(rc);
 
6553
 
 
6554
  test_bind_date_conv(3);
 
6555
}
 
6556
 
 
6557
 
 
6558
/* Test all time types to TIMESTAMP and TIMESTAMP to all types */
 
6559
 
 
6560
static void test_date_ts()
 
6561
{
 
6562
  int        rc;
 
6563
 
 
6564
  myheader("test_date_ts");
 
6565
 
 
6566
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
 
6567
  myquery(rc);
 
6568
 
 
6569
  rc= mysql_query(mysql, "CREATE TABLE test_date(c1 TIMESTAMP, \
 
6570
                                                 c2 TIMESTAMP, \
 
6571
                                                 c3 TIMESTAMP, \
 
6572
                                                 c4 TIMESTAMP)");
 
6573
 
 
6574
  myquery(rc);
 
6575
 
 
6576
  test_bind_date_conv(2);
 
6577
}
 
6578
 
 
6579
 
 
6580
/* Test all time types to DATETIME and DATETIME to all types */
 
6581
 
 
6582
static void test_date_dt()
 
6583
{
 
6584
  int rc;
 
6585
 
 
6586
  myheader("test_date_dt");
 
6587
 
 
6588
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
 
6589
  myquery(rc);
 
6590
 
 
6591
  rc= mysql_query(mysql, "CREATE TABLE test_date(c1 datetime, "
 
6592
                         " c2 datetime, c3 datetime, c4 date)");
 
6593
  myquery(rc);
 
6594
 
 
6595
  test_bind_date_conv(2);
 
6596
}
 
6597
 
 
6598
 
 
6599
/* Misc tests to keep pure coverage happy */
 
6600
 
 
6601
static void test_pure_coverage()
 
6602
{
 
6603
  MYSQL_STMT *stmt;
 
6604
  MYSQL_BIND my_bind[1];
 
6605
  int        rc;
 
6606
  ulong      length;
 
6607
 
 
6608
  myheader("test_pure_coverage");
 
6609
 
 
6610
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_pure");
 
6611
  myquery(rc);
 
6612
 
 
6613
  rc= mysql_query(mysql, "CREATE TABLE test_pure(c1 int, c2 varchar(20))");
 
6614
  myquery(rc);
 
6615
 
 
6616
  stmt= mysql_simple_prepare(mysql, "insert into test_pure(c67788) values(10)");
 
6617
  check_stmt_r(stmt);
 
6618
 
 
6619
  /* Query without params and result should allow to bind 0 arrays */
 
6620
  stmt= mysql_simple_prepare(mysql, "insert into test_pure(c2) values(10)");
 
6621
  check_stmt(stmt);
 
6622
 
 
6623
  rc= mysql_stmt_bind_param(stmt, (MYSQL_BIND*)0);
 
6624
  check_execute(stmt, rc);
 
6625
 
 
6626
  rc= mysql_stmt_execute(stmt);
 
6627
  check_execute(stmt, rc);
 
6628
 
 
6629
  rc= mysql_stmt_bind_result(stmt, (MYSQL_BIND*)0);
 
6630
  DIE_UNLESS(rc == 1);
 
6631
 
 
6632
  mysql_stmt_close(stmt);
 
6633
 
 
6634
  stmt= mysql_simple_prepare(mysql, "insert into test_pure(c2) values(?)");
 
6635
  check_stmt(stmt);
 
6636
 
 
6637
  /*
 
6638
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
6639
    its members.
 
6640
  */
 
6641
  bzero((char*) my_bind, sizeof(my_bind));
 
6642
 
 
6643
  my_bind[0].length= &length;
 
6644
  my_bind[0].is_null= 0;
 
6645
  my_bind[0].buffer_length= 0;
 
6646
 
 
6647
  my_bind[0].buffer_type= MYSQL_TYPE_GEOMETRY;
 
6648
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
6649
  check_execute_r(stmt, rc); /* unsupported buffer type */
 
6650
 
 
6651
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
6652
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
6653
  check_execute(stmt, rc);
 
6654
 
 
6655
  rc= mysql_stmt_store_result(stmt);
 
6656
  check_execute(stmt, rc);
 
6657
 
 
6658
  mysql_stmt_close(stmt);
 
6659
 
 
6660
  stmt= mysql_simple_prepare(mysql, "select * from test_pure");
 
6661
  check_execute(stmt, rc);
 
6662
 
 
6663
  rc= mysql_stmt_execute(stmt);
 
6664
  check_execute(stmt, rc);
 
6665
 
 
6666
  my_bind[0].buffer_type= MYSQL_TYPE_GEOMETRY;
 
6667
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
6668
  check_execute_r(stmt, rc); /* unsupported buffer type */
 
6669
 
 
6670
  rc= mysql_stmt_store_result(stmt);
 
6671
  check_execute(stmt, rc);
 
6672
 
 
6673
  rc= mysql_stmt_store_result(stmt);
 
6674
  check_execute_r(stmt, rc); /* commands out of sync */
 
6675
 
 
6676
  mysql_stmt_close(stmt);
 
6677
 
 
6678
  mysql_query(mysql, "DROP TABLE test_pure");
 
6679
}
 
6680
 
 
6681
 
 
6682
/* Test for string buffer fetch */
 
6683
 
 
6684
static void test_buffers()
 
6685
{
 
6686
  MYSQL_STMT *stmt;
 
6687
  MYSQL_BIND my_bind[1];
 
6688
  int        rc;
 
6689
  ulong      length;
 
6690
  my_bool    is_null;
 
6691
  char       buffer[20];
 
6692
 
 
6693
  myheader("test_buffers");
 
6694
 
 
6695
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_buffer");
 
6696
  myquery(rc);
 
6697
 
 
6698
  rc= mysql_query(mysql, "CREATE TABLE test_buffer(str varchar(20))");
 
6699
  myquery(rc);
 
6700
 
 
6701
  rc= mysql_query(mysql, "insert into test_buffer values('MySQL')\
 
6702
                          , ('Database'), ('Open-Source'), ('Popular')");
 
6703
  myquery(rc);
 
6704
 
 
6705
  stmt= mysql_simple_prepare(mysql, "select str from test_buffer");
 
6706
  check_stmt(stmt);
 
6707
 
 
6708
  rc= mysql_stmt_execute(stmt);
 
6709
  check_execute(stmt, rc);
 
6710
 
 
6711
  bzero(buffer, sizeof(buffer));              /* Avoid overruns in printf() */
 
6712
 
 
6713
  bzero((char*) my_bind, sizeof(my_bind));
 
6714
  my_bind[0].length= &length;
 
6715
  my_bind[0].is_null= &is_null;
 
6716
  my_bind[0].buffer_length= 1;
 
6717
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
6718
  my_bind[0].buffer= (void *)buffer;
 
6719
  my_bind[0].error= &my_bind[0].error_value;
 
6720
 
 
6721
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
6722
  check_execute(stmt, rc);
 
6723
 
 
6724
  rc= mysql_stmt_store_result(stmt);
 
6725
  check_execute(stmt, rc);
 
6726
 
 
6727
  buffer[1]= 'X';
 
6728
  rc= mysql_stmt_fetch(stmt);
 
6729
  DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
 
6730
  DIE_UNLESS(my_bind[0].error_value);
 
6731
  if (!opt_silent)
 
6732
    fprintf(stdout, "\n data: %s (%lu)", buffer, length);
 
6733
  DIE_UNLESS(buffer[0] == 'M');
 
6734
  DIE_UNLESS(buffer[1] == 'X');
 
6735
  DIE_UNLESS(length == 5);
 
6736
 
 
6737
  my_bind[0].buffer_length= 8;
 
6738
  rc= mysql_stmt_bind_result(stmt, my_bind);/* re-bind */
 
6739
  check_execute(stmt, rc);
 
6740
 
 
6741
  rc= mysql_stmt_fetch(stmt);
 
6742
  check_execute(stmt, rc);
 
6743
  if (!opt_silent)
 
6744
    fprintf(stdout, "\n data: %s (%lu)", buffer, length);
 
6745
  DIE_UNLESS(strncmp(buffer, "Database", 8) == 0);
 
6746
  DIE_UNLESS(length == 8);
 
6747
 
 
6748
  my_bind[0].buffer_length= 12;
 
6749
  rc= mysql_stmt_bind_result(stmt, my_bind);/* re-bind */
 
6750
  check_execute(stmt, rc);
 
6751
 
 
6752
  rc= mysql_stmt_fetch(stmt);
 
6753
  check_execute(stmt, rc);
 
6754
  if (!opt_silent)
 
6755
    fprintf(stdout, "\n data: %s (%lu)", buffer, length);
 
6756
  DIE_UNLESS(strcmp(buffer, "Open-Source") == 0);
 
6757
  DIE_UNLESS(length == 11);
 
6758
 
 
6759
  my_bind[0].buffer_length= 6;
 
6760
  rc= mysql_stmt_bind_result(stmt, my_bind);/* re-bind */
 
6761
  check_execute(stmt, rc);
 
6762
 
 
6763
  rc= mysql_stmt_fetch(stmt);
 
6764
  DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
 
6765
  DIE_UNLESS(my_bind[0].error_value);
 
6766
  if (!opt_silent)
 
6767
    fprintf(stdout, "\n data: %s (%lu)", buffer, length);
 
6768
  DIE_UNLESS(strncmp(buffer, "Popula", 6) == 0);
 
6769
  DIE_UNLESS(length == 7);
 
6770
 
 
6771
  mysql_stmt_close(stmt);
 
6772
}
 
6773
 
 
6774
 
 
6775
/* Test the direct query execution in the middle of open stmts */
 
6776
 
 
6777
static void test_open_direct()
 
6778
{
 
6779
  MYSQL_STMT  *stmt;
 
6780
  MYSQL_RES   *result;
 
6781
  int         rc;
 
6782
 
 
6783
  myheader("test_open_direct");
 
6784
 
 
6785
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_open_direct");
 
6786
  myquery(rc);
 
6787
 
 
6788
  rc= mysql_query(mysql, "CREATE TABLE test_open_direct(id int, name char(6))");
 
6789
  myquery(rc);
 
6790
 
 
6791
  stmt= mysql_simple_prepare(mysql, "INSERT INTO test_open_direct values(10, 'mysql')");
 
6792
  check_stmt(stmt);
 
6793
 
 
6794
  rc= mysql_query(mysql, "SELECT * FROM test_open_direct");
 
6795
  myquery(rc);
 
6796
 
 
6797
  result= mysql_store_result(mysql);
 
6798
  mytest(result);
 
6799
 
 
6800
  rc= my_process_result_set(result);
 
6801
  DIE_UNLESS(rc == 0);
 
6802
  mysql_free_result(result);
 
6803
 
 
6804
  rc= mysql_stmt_execute(stmt);
 
6805
  check_execute(stmt, rc);
 
6806
 
 
6807
  verify_st_affected_rows(stmt, 1);
 
6808
 
 
6809
  rc= mysql_query(mysql, "SELECT * FROM test_open_direct");
 
6810
  myquery(rc);
 
6811
 
 
6812
  result= mysql_store_result(mysql);
 
6813
  mytest(result);
 
6814
 
 
6815
  rc= my_process_result_set(result);
 
6816
  DIE_UNLESS(rc == 1);
 
6817
  mysql_free_result(result);
 
6818
 
 
6819
  rc= mysql_stmt_execute(stmt);
 
6820
  check_execute(stmt, rc);
 
6821
 
 
6822
  verify_st_affected_rows(stmt, 1);
 
6823
 
 
6824
  rc= mysql_query(mysql, "SELECT * FROM test_open_direct");
 
6825
  myquery(rc);
 
6826
 
 
6827
  result= mysql_store_result(mysql);
 
6828
  mytest(result);
 
6829
 
 
6830
  rc= my_process_result_set(result);
 
6831
  DIE_UNLESS(rc == 2);
 
6832
  mysql_free_result(result);
 
6833
 
 
6834
  mysql_stmt_close(stmt);
 
6835
 
 
6836
  /* run a direct query in the middle of a fetch */
 
6837
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_open_direct");
 
6838
  check_stmt(stmt);
 
6839
 
 
6840
  rc= mysql_stmt_execute(stmt);
 
6841
  check_execute(stmt, rc);
 
6842
 
 
6843
  rc= mysql_stmt_fetch(stmt);
 
6844
  check_execute(stmt, rc);
 
6845
 
 
6846
  rc= mysql_query(mysql, "INSERT INTO test_open_direct(id) VALUES(20)");
 
6847
  myquery_r(rc);
 
6848
 
 
6849
  rc= mysql_stmt_close(stmt);
 
6850
  check_execute(stmt, rc);
 
6851
 
 
6852
  rc= mysql_query(mysql, "INSERT INTO test_open_direct(id) VALUES(20)");
 
6853
  myquery(rc);
 
6854
 
 
6855
  /* run a direct query with store result */
 
6856
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_open_direct");
 
6857
  check_stmt(stmt);
 
6858
 
 
6859
  rc= mysql_stmt_execute(stmt);
 
6860
  check_execute(stmt, rc);
 
6861
 
 
6862
  rc= mysql_stmt_store_result(stmt);
 
6863
  check_execute(stmt, rc);
 
6864
 
 
6865
  rc= mysql_stmt_fetch(stmt);
 
6866
  check_execute(stmt, rc);
 
6867
 
 
6868
  rc= mysql_query(mysql, "drop table test_open_direct");
 
6869
  myquery(rc);
 
6870
 
 
6871
  rc= mysql_stmt_close(stmt);
 
6872
  check_execute(stmt, rc);
 
6873
}
 
6874
 
 
6875
 
 
6876
/* Test fetch without prior bound buffers */
 
6877
 
 
6878
static void test_fetch_nobuffs()
 
6879
{
 
6880
  MYSQL_STMT *stmt;
 
6881
  MYSQL_BIND my_bind[4];
 
6882
  char       str[4][50];
 
6883
  int        rc;
 
6884
 
 
6885
  myheader("test_fetch_nobuffs");
 
6886
 
 
6887
  stmt= mysql_simple_prepare(mysql, "SELECT DATABASE(), CURRENT_USER(), \
 
6888
                              CURRENT_DATE(), CURRENT_TIME()");
 
6889
  check_stmt(stmt);
 
6890
 
 
6891
  rc= mysql_stmt_execute(stmt);
 
6892
  check_execute(stmt, rc);
 
6893
 
 
6894
  rc= 0;
 
6895
  while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
 
6896
    rc++;
 
6897
 
 
6898
  if (!opt_silent)
 
6899
    fprintf(stdout, "\n total rows        : %d", rc);
 
6900
  DIE_UNLESS(rc == 1);
 
6901
 
 
6902
  bzero((char*) my_bind, sizeof(MYSQL_BIND));
 
6903
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
6904
  my_bind[0].buffer= (void *)str[0];
 
6905
  my_bind[0].buffer_length= sizeof(str[0]);
 
6906
  my_bind[1]= my_bind[2]= my_bind[3]= my_bind[0];
 
6907
  my_bind[1].buffer= (void *)str[1];
 
6908
  my_bind[2].buffer= (void *)str[2];
 
6909
  my_bind[3].buffer= (void *)str[3];
 
6910
 
 
6911
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
6912
  check_execute(stmt, rc);
 
6913
 
 
6914
  rc= mysql_stmt_execute(stmt);
 
6915
  check_execute(stmt, rc);
 
6916
 
 
6917
  rc= 0;
 
6918
  while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
 
6919
  {
 
6920
    rc++;
 
6921
    if (!opt_silent)
 
6922
    {
 
6923
      fprintf(stdout, "\n CURRENT_DATABASE(): %s", str[0]);
 
6924
      fprintf(stdout, "\n CURRENT_USER()    : %s", str[1]);
 
6925
      fprintf(stdout, "\n CURRENT_DATE()    : %s", str[2]);
 
6926
      fprintf(stdout, "\n CURRENT_TIME()    : %s", str[3]);
 
6927
    }
 
6928
  }
 
6929
  if (!opt_silent)
 
6930
    fprintf(stdout, "\n total rows        : %d", rc);
 
6931
  DIE_UNLESS(rc == 1);
 
6932
 
 
6933
  mysql_stmt_close(stmt);
 
6934
}
 
6935
 
 
6936
 
 
6937
/* Test a misc bug */
 
6938
 
 
6939
static void test_ushort_bug()
 
6940
{
 
6941
  MYSQL_STMT *stmt;
 
6942
  MYSQL_BIND my_bind[4];
 
6943
  ushort     short_value;
 
6944
  uint32     long_value;
 
6945
  ulong      s_length, l_length, ll_length, t_length;
 
6946
  ulonglong  longlong_value;
 
6947
  int        rc;
 
6948
  uchar      tiny_value;
 
6949
  char       llbuf[22];
 
6950
  myheader("test_ushort_bug");
 
6951
 
 
6952
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ushort");
 
6953
  myquery(rc);
 
6954
 
 
6955
  rc= mysql_query(mysql, "CREATE TABLE test_ushort(a smallint unsigned, \
 
6956
                                                  b smallint unsigned, \
 
6957
                                                  c smallint unsigned, \
 
6958
                                                  d smallint unsigned)");
 
6959
  myquery(rc);
 
6960
 
 
6961
  rc= mysql_query(mysql,
 
6962
                  "INSERT INTO test_ushort VALUES(35999, 35999, 35999, 200)");
 
6963
  myquery(rc);
 
6964
 
 
6965
 
 
6966
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_ushort");
 
6967
  check_stmt(stmt);
 
6968
 
 
6969
  rc= mysql_stmt_execute(stmt);
 
6970
  check_execute(stmt, rc);
 
6971
 
 
6972
  bzero((char*) my_bind, sizeof(my_bind));
 
6973
  my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
 
6974
  my_bind[0].buffer= (void *)&short_value;
 
6975
  my_bind[0].is_unsigned= TRUE;
 
6976
  my_bind[0].length= &s_length;
 
6977
 
 
6978
  my_bind[1].buffer_type= MYSQL_TYPE_LONG;
 
6979
  my_bind[1].buffer= (void *)&long_value;
 
6980
  my_bind[1].length= &l_length;
 
6981
 
 
6982
  my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
 
6983
  my_bind[2].buffer= (void *)&longlong_value;
 
6984
  my_bind[2].length= &ll_length;
 
6985
 
 
6986
  my_bind[3].buffer_type= MYSQL_TYPE_TINY;
 
6987
  my_bind[3].buffer= (void *)&tiny_value;
 
6988
  my_bind[3].is_unsigned= TRUE;
 
6989
  my_bind[3].length= &t_length;
 
6990
 
 
6991
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
6992
  check_execute(stmt, rc);
 
6993
 
 
6994
  rc= mysql_stmt_fetch(stmt);
 
6995
  check_execute(stmt, rc);
 
6996
 
 
6997
  if (!opt_silent)
 
6998
  {
 
6999
    fprintf(stdout, "\n ushort   : %d (%ld)", short_value, s_length);
 
7000
    fprintf(stdout, "\n ulong    : %lu (%ld)", (ulong) long_value, l_length);
 
7001
    fprintf(stdout, "\n longlong : %s (%ld)", llstr(longlong_value, llbuf),
 
7002
            ll_length);
 
7003
    fprintf(stdout, "\n tinyint  : %d   (%ld)", tiny_value, t_length);
 
7004
  }
 
7005
 
 
7006
  DIE_UNLESS(short_value == 35999);
 
7007
  DIE_UNLESS(s_length == 2);
 
7008
 
 
7009
  DIE_UNLESS(long_value == 35999);
 
7010
  DIE_UNLESS(l_length == 4);
 
7011
 
 
7012
  DIE_UNLESS(longlong_value == 35999);
 
7013
  DIE_UNLESS(ll_length == 8);
 
7014
 
 
7015
  DIE_UNLESS(tiny_value == 200);
 
7016
  DIE_UNLESS(t_length == 1);
 
7017
 
 
7018
  rc= mysql_stmt_fetch(stmt);
 
7019
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
7020
 
 
7021
  mysql_stmt_close(stmt);
 
7022
}
 
7023
 
 
7024
 
 
7025
/* Test a misc smallint-signed conversion bug */
 
7026
 
 
7027
static void test_sshort_bug()
 
7028
{
 
7029
  MYSQL_STMT *stmt;
 
7030
  MYSQL_BIND my_bind[4];
 
7031
  short      short_value;
 
7032
  int32      long_value;
 
7033
  ulong      s_length, l_length, ll_length, t_length;
 
7034
  ulonglong  longlong_value;
 
7035
  int        rc;
 
7036
  uchar      tiny_value;
 
7037
  char       llbuf[22];
 
7038
 
 
7039
  myheader("test_sshort_bug");
 
7040
 
 
7041
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_sshort");
 
7042
  myquery(rc);
 
7043
 
 
7044
  rc= mysql_query(mysql, "CREATE TABLE test_sshort(a smallint signed, \
 
7045
                                                  b smallint signed, \
 
7046
                                                  c smallint unsigned, \
 
7047
                                                  d smallint unsigned)");
 
7048
  myquery(rc);
 
7049
 
 
7050
  rc= mysql_query(mysql, "INSERT INTO test_sshort VALUES(-5999, -5999, 35999, 200)");
 
7051
  myquery(rc);
 
7052
 
 
7053
 
 
7054
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_sshort");
 
7055
  check_stmt(stmt);
 
7056
 
 
7057
  rc= mysql_stmt_execute(stmt);
 
7058
  check_execute(stmt, rc);
 
7059
 
 
7060
  bzero((char*) my_bind, sizeof(my_bind));
 
7061
  my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
 
7062
  my_bind[0].buffer= (void *)&short_value;
 
7063
  my_bind[0].length= &s_length;
 
7064
 
 
7065
  my_bind[1].buffer_type= MYSQL_TYPE_LONG;
 
7066
  my_bind[1].buffer= (void *)&long_value;
 
7067
  my_bind[1].length= &l_length;
 
7068
 
 
7069
  my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
 
7070
  my_bind[2].buffer= (void *)&longlong_value;
 
7071
  my_bind[2].length= &ll_length;
 
7072
 
 
7073
  my_bind[3].buffer_type= MYSQL_TYPE_TINY;
 
7074
  my_bind[3].buffer= (void *)&tiny_value;
 
7075
  my_bind[3].is_unsigned= TRUE;
 
7076
  my_bind[3].length= &t_length;
 
7077
 
 
7078
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
7079
  check_execute(stmt, rc);
 
7080
 
 
7081
  rc= mysql_stmt_fetch(stmt);
 
7082
  check_execute(stmt, rc);
 
7083
 
 
7084
  if (!opt_silent)
 
7085
  {
 
7086
    fprintf(stdout, "\n sshort   : %d (%ld)", short_value, s_length);
 
7087
    fprintf(stdout, "\n slong    : %ld (%ld)", (long) long_value, l_length);
 
7088
    fprintf(stdout, "\n longlong : %s (%ld)", llstr(longlong_value, llbuf),
 
7089
            ll_length);
 
7090
    fprintf(stdout, "\n tinyint  : %d   (%ld)", tiny_value, t_length);
 
7091
  }
 
7092
 
 
7093
  DIE_UNLESS(short_value == -5999);
 
7094
  DIE_UNLESS(s_length == 2);
 
7095
 
 
7096
  DIE_UNLESS(long_value == -5999);
 
7097
  DIE_UNLESS(l_length == 4);
 
7098
 
 
7099
  DIE_UNLESS(longlong_value == 35999);
 
7100
  DIE_UNLESS(ll_length == 8);
 
7101
 
 
7102
  DIE_UNLESS(tiny_value == 200);
 
7103
  DIE_UNLESS(t_length == 1);
 
7104
 
 
7105
  rc= mysql_stmt_fetch(stmt);
 
7106
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
7107
 
 
7108
  mysql_stmt_close(stmt);
 
7109
}
 
7110
 
 
7111
 
 
7112
/* Test a misc tinyint-signed conversion bug */
 
7113
 
 
7114
static void test_stiny_bug()
 
7115
{
 
7116
  MYSQL_STMT *stmt;
 
7117
  MYSQL_BIND my_bind[4];
 
7118
  short      short_value;
 
7119
  int32      long_value;
 
7120
  ulong      s_length, l_length, ll_length, t_length;
 
7121
  ulonglong  longlong_value;
 
7122
  int        rc;
 
7123
  uchar      tiny_value;
 
7124
  char       llbuf[22];
 
7125
 
 
7126
  myheader("test_stiny_bug");
 
7127
 
 
7128
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_stiny");
 
7129
  myquery(rc);
 
7130
 
 
7131
  rc= mysql_query(mysql, "CREATE TABLE test_stiny(a tinyint signed, \
 
7132
                                                  b tinyint signed, \
 
7133
                                                  c tinyint unsigned, \
 
7134
                                                  d tinyint unsigned)");
 
7135
  myquery(rc);
 
7136
 
 
7137
  rc= mysql_query(mysql, "INSERT INTO test_stiny VALUES(-128, -127, 255, 0)");
 
7138
  myquery(rc);
 
7139
 
 
7140
 
 
7141
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_stiny");
 
7142
  check_stmt(stmt);
 
7143
 
 
7144
  rc= mysql_stmt_execute(stmt);
 
7145
  check_execute(stmt, rc);
 
7146
 
 
7147
  bzero((char*) my_bind, sizeof(my_bind));
 
7148
  my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
 
7149
  my_bind[0].buffer= (void *)&short_value;
 
7150
  my_bind[0].length= &s_length;
 
7151
 
 
7152
  my_bind[1].buffer_type= MYSQL_TYPE_LONG;
 
7153
  my_bind[1].buffer= (void *)&long_value;
 
7154
  my_bind[1].length= &l_length;
 
7155
 
 
7156
  my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
 
7157
  my_bind[2].buffer= (void *)&longlong_value;
 
7158
  my_bind[2].length= &ll_length;
 
7159
 
 
7160
  my_bind[3].buffer_type= MYSQL_TYPE_TINY;
 
7161
  my_bind[3].buffer= (void *)&tiny_value;
 
7162
  my_bind[3].length= &t_length;
 
7163
 
 
7164
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
7165
  check_execute(stmt, rc);
 
7166
 
 
7167
  rc= mysql_stmt_fetch(stmt);
 
7168
  check_execute(stmt, rc);
 
7169
 
 
7170
  if (!opt_silent)
 
7171
  {
 
7172
    fprintf(stdout, "\n sshort   : %d (%ld)", short_value, s_length);
 
7173
    fprintf(stdout, "\n slong    : %ld (%ld)", (long) long_value, l_length);
 
7174
    fprintf(stdout, "\n longlong : %s  (%ld)", llstr(longlong_value, llbuf),
 
7175
            ll_length);
 
7176
    fprintf(stdout, "\n tinyint  : %d    (%ld)", tiny_value, t_length);
 
7177
  }
 
7178
 
 
7179
  DIE_UNLESS(short_value == -128);
 
7180
  DIE_UNLESS(s_length == 2);
 
7181
 
 
7182
  DIE_UNLESS(long_value == -127);
 
7183
  DIE_UNLESS(l_length == 4);
 
7184
 
 
7185
  DIE_UNLESS(longlong_value == 255);
 
7186
  DIE_UNLESS(ll_length == 8);
 
7187
 
 
7188
  DIE_UNLESS(tiny_value == 0);
 
7189
  DIE_UNLESS(t_length == 1);
 
7190
 
 
7191
  rc= mysql_stmt_fetch(stmt);
 
7192
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
7193
 
 
7194
  mysql_stmt_close(stmt);
 
7195
}
 
7196
 
 
7197
 
 
7198
/* Test misc field information, bug: #74 */
 
7199
 
 
7200
static void test_field_misc()
 
7201
{
 
7202
  MYSQL_STMT  *stmt;
 
7203
  MYSQL_RES   *result;
 
7204
  MYSQL_BIND  my_bind[1];
 
7205
  char        table_type[NAME_LEN];
 
7206
  ulong       type_length;
 
7207
  int         rc;
 
7208
 
 
7209
  myheader("test_field_misc");
 
7210
 
 
7211
  rc= mysql_query(mysql, "SELECT @@autocommit");
 
7212
  myquery(rc);
 
7213
 
 
7214
  result= mysql_store_result(mysql);
 
7215
  mytest(result);
 
7216
 
 
7217
  rc= my_process_result_set(result);
 
7218
  DIE_UNLESS(rc == 1);
 
7219
 
 
7220
  verify_prepare_field(result, 0,
 
7221
                       "@@autocommit", "",  /* field and its org name */
 
7222
                       MYSQL_TYPE_LONGLONG, /* field type */
 
7223
                       "", "",              /* table and its org name */
 
7224
                       "", 1, 0);           /* db name, length(its bool flag)*/
 
7225
 
 
7226
  mysql_free_result(result);
 
7227
 
 
7228
  stmt= mysql_simple_prepare(mysql, "SELECT @@autocommit");
 
7229
  check_stmt(stmt);
 
7230
 
 
7231
  rc= mysql_stmt_execute(stmt);
 
7232
  check_execute(stmt, rc);
 
7233
 
 
7234
  result= mysql_stmt_result_metadata(stmt);
 
7235
  mytest(result);
 
7236
 
 
7237
  rc= my_process_stmt_result(stmt);
 
7238
  DIE_UNLESS(rc == 1);
 
7239
 
 
7240
  verify_prepare_field(result, 0,
 
7241
                       "@@autocommit", "",  /* field and its org name */
 
7242
                       MYSQL_TYPE_LONGLONG, /* field type */
 
7243
                       "", "",              /* table and its org name */
 
7244
                       "", 1, 0);           /* db name, length(its bool flag)*/
 
7245
 
 
7246
  mysql_free_result(result);
 
7247
  mysql_stmt_close(stmt);
 
7248
 
 
7249
  stmt= mysql_simple_prepare(mysql, "SELECT @@storage_engine");
 
7250
  check_stmt(stmt);
 
7251
 
 
7252
  rc= mysql_stmt_execute(stmt);
 
7253
  check_execute(stmt, rc);
 
7254
 
 
7255
  bzero((char*) my_bind, sizeof(my_bind));
 
7256
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
7257
  my_bind[0].buffer= table_type;
 
7258
  my_bind[0].length= &type_length;
 
7259
  my_bind[0].buffer_length= NAME_LEN;
 
7260
 
 
7261
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
7262
  check_execute(stmt, rc);
 
7263
 
 
7264
  rc= mysql_stmt_fetch(stmt);
 
7265
  check_execute(stmt, rc);
 
7266
  if (!opt_silent)
 
7267
    fprintf(stdout, "\n default table type: %s(%ld)", table_type, type_length);
 
7268
 
 
7269
  rc= mysql_stmt_fetch(stmt);
 
7270
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
7271
 
 
7272
  mysql_stmt_close(stmt);
 
7273
 
 
7274
  stmt= mysql_simple_prepare(mysql, "SELECT @@storage_engine");
 
7275
  check_stmt(stmt);
 
7276
 
 
7277
  result= mysql_stmt_result_metadata(stmt);
 
7278
  mytest(result);
 
7279
  DIE_UNLESS(mysql_stmt_field_count(stmt) == mysql_num_fields(result));
 
7280
 
 
7281
  rc= mysql_stmt_execute(stmt);
 
7282
  check_execute(stmt, rc);
 
7283
 
 
7284
  DIE_UNLESS(1 == my_process_stmt_result(stmt));
 
7285
 
 
7286
  verify_prepare_field(result, 0,
 
7287
                       "@@storage_engine", "",   /* field and its org name */
 
7288
                       mysql_get_server_version(mysql) <= 50000 ?
 
7289
                       MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
 
7290
                       "", "",              /* table and its org name */
 
7291
                       "", type_length, 0);   /* db name, length */
 
7292
 
 
7293
  mysql_free_result(result);
 
7294
  mysql_stmt_close(stmt);
 
7295
 
 
7296
  stmt= mysql_simple_prepare(mysql, "SELECT @@max_error_count");
 
7297
  check_stmt(stmt);
 
7298
 
 
7299
  result= mysql_stmt_result_metadata(stmt);
 
7300
  mytest(result);
 
7301
 
 
7302
  rc= mysql_stmt_execute(stmt);
 
7303
  check_execute(stmt, rc);
 
7304
 
 
7305
  rc= my_process_stmt_result(stmt);
 
7306
  DIE_UNLESS(rc == 1);
 
7307
 
 
7308
  verify_prepare_field(result, 0,
 
7309
                       "@@max_error_count", "",   /* field and its org name */
 
7310
                       MYSQL_TYPE_LONGLONG, /* field type */
 
7311
                       "", "",              /* table and its org name */
 
7312
                       "", 10, 0);            /* db name, length */
 
7313
 
 
7314
  mysql_free_result(result);
 
7315
  mysql_stmt_close(stmt);
 
7316
 
 
7317
  stmt= mysql_simple_prepare(mysql, "SELECT @@max_allowed_packet");
 
7318
  check_stmt(stmt);
 
7319
 
 
7320
  result= mysql_stmt_result_metadata(stmt);
 
7321
  mytest(result);
 
7322
 
 
7323
  rc= mysql_stmt_execute(stmt);
 
7324
  check_execute(stmt, rc);
 
7325
 
 
7326
  DIE_UNLESS(1 == my_process_stmt_result(stmt));
 
7327
 
 
7328
  verify_prepare_field(result, 0,
 
7329
                       "@@max_allowed_packet", "", /* field and its org name */
 
7330
                       MYSQL_TYPE_LONGLONG, /* field type */
 
7331
                       "", "",              /* table and its org name */
 
7332
                       "", 10, 0);          /* db name, length */
 
7333
 
 
7334
  mysql_free_result(result);
 
7335
  mysql_stmt_close(stmt);
 
7336
 
 
7337
  stmt= mysql_simple_prepare(mysql, "SELECT @@sql_warnings");
 
7338
  check_stmt(stmt);
 
7339
 
 
7340
  result= mysql_stmt_result_metadata(stmt);
 
7341
  mytest(result);
 
7342
 
 
7343
  rc= mysql_stmt_execute(stmt);
 
7344
  check_execute(stmt, rc);
 
7345
 
 
7346
  rc= my_process_stmt_result(stmt);
 
7347
  DIE_UNLESS(rc == 1);
 
7348
 
 
7349
  verify_prepare_field(result, 0,
 
7350
                       "@@sql_warnings", "",  /* field and its org name */
 
7351
                       MYSQL_TYPE_LONGLONG,   /* field type */
 
7352
                       "", "",                /* table and its org name */
 
7353
                       "", 1, 0);             /* db name, length */
 
7354
 
 
7355
  mysql_free_result(result);
 
7356
  mysql_stmt_close(stmt);
 
7357
}
 
7358
 
 
7359
 
 
7360
/*
 
7361
  Test SET OPTION feature with prepare stmts
 
7362
  bug #85 (reported by mark@mysql.com)
 
7363
*/
 
7364
 
 
7365
static void test_set_option()
 
7366
{
 
7367
  MYSQL_STMT *stmt;
 
7368
  MYSQL_RES  *result;
 
7369
  int        rc;
 
7370
 
 
7371
  myheader("test_set_option");
 
7372
 
 
7373
  mysql_autocommit(mysql, TRUE);
 
7374
 
 
7375
  /* LIMIT the rows count to 2 */
 
7376
  rc= mysql_query(mysql, "SET OPTION SQL_SELECT_LIMIT= 2");
 
7377
  myquery(rc);
 
7378
 
 
7379
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_limit");
 
7380
  myquery(rc);
 
7381
 
 
7382
  rc= mysql_query(mysql, "CREATE TABLE test_limit(a tinyint)");
 
7383
  myquery(rc);
 
7384
 
 
7385
  rc= mysql_query(mysql, "INSERT INTO test_limit VALUES(10), (20), (30), (40)");
 
7386
  myquery(rc);
 
7387
 
 
7388
  if (!opt_silent)
 
7389
    fprintf(stdout, "\n with SQL_SELECT_LIMIT= 2 (direct)");
 
7390
  rc= mysql_query(mysql, "SELECT * FROM test_limit");
 
7391
  myquery(rc);
 
7392
 
 
7393
  result= mysql_store_result(mysql);
 
7394
  mytest(result);
 
7395
 
 
7396
  rc= my_process_result_set(result);
 
7397
  DIE_UNLESS(rc == 2);
 
7398
 
 
7399
  mysql_free_result(result);
 
7400
 
 
7401
  if (!opt_silent)
 
7402
    fprintf(stdout, "\n with SQL_SELECT_LIMIT=2 (prepare)");
 
7403
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_limit");
 
7404
  check_stmt(stmt);
 
7405
 
 
7406
  rc= mysql_stmt_execute(stmt);
 
7407
  check_execute(stmt, rc);
 
7408
 
 
7409
  rc= my_process_stmt_result(stmt);
 
7410
  DIE_UNLESS(rc == 2);
 
7411
 
 
7412
  mysql_stmt_close(stmt);
 
7413
 
 
7414
  /* RESET the LIMIT the rows count to 0 */
 
7415
  if (!opt_silent)
 
7416
    fprintf(stdout, "\n with SQL_SELECT_LIMIT=DEFAULT (prepare)");
 
7417
  rc= mysql_query(mysql, "SET OPTION SQL_SELECT_LIMIT=DEFAULT");
 
7418
  myquery(rc);
 
7419
 
 
7420
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_limit");
 
7421
  check_stmt(stmt);
 
7422
 
 
7423
  rc= mysql_stmt_execute(stmt);
 
7424
  check_execute(stmt, rc);
 
7425
 
 
7426
  rc= my_process_stmt_result(stmt);
 
7427
  DIE_UNLESS(rc == 4);
 
7428
 
 
7429
  mysql_stmt_close(stmt);
 
7430
}
 
7431
 
 
7432
 
 
7433
/*
 
7434
  Test a misc GRANT option
 
7435
  bug #89 (reported by mark@mysql.com)
 
7436
*/
 
7437
 
 
7438
#ifndef EMBEDDED_LIBRARY
 
7439
static void test_prepare_grant()
 
7440
{
 
7441
  int rc;
 
7442
  char query[MAX_TEST_QUERY_LENGTH];
 
7443
 
 
7444
  myheader("test_prepare_grant");
 
7445
 
 
7446
  mysql_autocommit(mysql, TRUE);
 
7447
 
 
7448
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_grant");
 
7449
  myquery(rc);
 
7450
 
 
7451
  rc= mysql_query(mysql, "CREATE TABLE test_grant(a tinyint primary key auto_increment)");
 
7452
  myquery(rc);
 
7453
 
 
7454
  strxmov(query, "GRANT INSERT, UPDATE, SELECT ON ", current_db,
 
7455
                ".test_grant TO 'test_grant'@",
 
7456
                opt_host ? opt_host : "'localhost'", NullS);
 
7457
 
 
7458
  if (mysql_query(mysql, query))
 
7459
  {
 
7460
    myerror("GRANT failed");
 
7461
 
 
7462
    /*
 
7463
       If server started with --skip-grant-tables, skip this test, else
 
7464
       exit to indicate an error
 
7465
 
 
7466
       ER_UNKNOWN_COM_ERROR= 1047
 
7467
     */
 
7468
    if (mysql_errno(mysql) != 1047)
 
7469
      exit(1);
 
7470
  }
 
7471
  else
 
7472
  {
 
7473
    MYSQL *org_mysql= mysql, *lmysql;
 
7474
    MYSQL_STMT *stmt;
 
7475
 
 
7476
    if (!opt_silent)
 
7477
      fprintf(stdout, "\n Establishing a test connection ...");
 
7478
    if (!(lmysql= mysql_init(NULL)))
 
7479
    {
 
7480
      myerror("mysql_init() failed");
 
7481
      exit(1);
 
7482
    }
 
7483
    if (!(mysql_real_connect(lmysql, opt_host, "test_grant",
 
7484
                             "", current_db, opt_port,
 
7485
                             opt_unix_socket, 0)))
 
7486
    {
 
7487
      myerror("connection failed");
 
7488
      mysql_close(lmysql);
 
7489
      exit(1);
 
7490
    }
 
7491
    lmysql->reconnect= 1;
 
7492
    if (!opt_silent)
 
7493
      fprintf(stdout, "OK");
 
7494
 
 
7495
    mysql= lmysql;
 
7496
    rc= mysql_query(mysql, "INSERT INTO test_grant VALUES(NULL)");
 
7497
    myquery(rc);
 
7498
 
 
7499
    rc= mysql_query(mysql, "INSERT INTO test_grant(a) VALUES(NULL)");
 
7500
    myquery(rc);
 
7501
 
 
7502
    execute_prepare_query("INSERT INTO test_grant(a) VALUES(NULL)", 1);
 
7503
    execute_prepare_query("INSERT INTO test_grant VALUES(NULL)", 1);
 
7504
    execute_prepare_query("UPDATE test_grant SET a=9 WHERE a=1", 1);
 
7505
    rc= my_stmt_result("SELECT a FROM test_grant");
 
7506
    DIE_UNLESS(rc == 4);
 
7507
 
 
7508
    /* Both DELETE expected to fail as user does not have DELETE privs */
 
7509
 
 
7510
    rc= mysql_query(mysql, "DELETE FROM test_grant");
 
7511
    myquery_r(rc);
 
7512
 
 
7513
    stmt= mysql_simple_prepare(mysql, "DELETE FROM test_grant");
 
7514
    check_stmt_r(stmt);
 
7515
 
 
7516
    rc= my_stmt_result("SELECT * FROM test_grant");
 
7517
    DIE_UNLESS(rc == 4);
 
7518
 
 
7519
    mysql_close(lmysql);
 
7520
    mysql= org_mysql;
 
7521
 
 
7522
    rc= mysql_query(mysql, "delete from mysql.user where User='test_grant'");
 
7523
    myquery(rc);
 
7524
    DIE_UNLESS(1 == mysql_affected_rows(mysql));
 
7525
 
 
7526
    rc= mysql_query(mysql, "delete from mysql.tables_priv where User='test_grant'");
 
7527
    myquery(rc);
 
7528
    DIE_UNLESS(1 == mysql_affected_rows(mysql));
 
7529
 
 
7530
  }
 
7531
}
 
7532
#endif /* EMBEDDED_LIBRARY */
 
7533
 
 
7534
/*
 
7535
  Test a crash when invalid/corrupted .frm is used in the
 
7536
  SHOW TABLE STATUS
 
7537
  bug #93 (reported by serg@mysql.com).
 
7538
*/
 
7539
 
 
7540
static void test_frm_bug()
 
7541
{
 
7542
  MYSQL_STMT *stmt;
 
7543
  MYSQL_BIND my_bind[2];
 
7544
  MYSQL_RES  *result;
 
7545
  MYSQL_ROW  row;
 
7546
  FILE       *test_file;
 
7547
  char       data_dir[FN_REFLEN];
 
7548
  char       test_frm[FN_REFLEN];
 
7549
  int        rc;
 
7550
 
 
7551
  myheader("test_frm_bug");
 
7552
 
 
7553
  mysql_autocommit(mysql, TRUE);
 
7554
 
 
7555
  rc= mysql_query(mysql, "drop table if exists test_frm_bug");
 
7556
  myquery(rc);
 
7557
 
 
7558
  rc= mysql_query(mysql, "flush tables");
 
7559
  myquery(rc);
 
7560
 
 
7561
  stmt= mysql_simple_prepare(mysql, "show variables like 'datadir'");
 
7562
  check_stmt(stmt);
 
7563
 
 
7564
  rc= mysql_stmt_execute(stmt);
 
7565
  check_execute(stmt, rc);
 
7566
 
 
7567
  bzero((char*) my_bind, sizeof(my_bind));
 
7568
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
7569
  my_bind[0].buffer= data_dir;
 
7570
  my_bind[0].buffer_length= FN_REFLEN;
 
7571
  my_bind[1]= my_bind[0];
 
7572
 
 
7573
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
7574
  check_execute(stmt, rc);
 
7575
 
 
7576
  rc= mysql_stmt_fetch(stmt);
 
7577
  check_execute(stmt, rc);
 
7578
 
 
7579
  if (!opt_silent)
 
7580
    fprintf(stdout, "\n data directory: %s", data_dir);
 
7581
 
 
7582
  rc= mysql_stmt_fetch(stmt);
 
7583
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
7584
 
 
7585
  strxmov(test_frm, data_dir, "/", current_db, "/", "test_frm_bug.frm", NullS);
 
7586
 
 
7587
  if (!opt_silent)
 
7588
    fprintf(stdout, "\n test_frm: %s", test_frm);
 
7589
 
 
7590
  if (!(test_file= my_fopen(test_frm, (int) (O_RDWR | O_CREAT), MYF(MY_WME))))
 
7591
  {
 
7592
    fprintf(stdout, "\n ERROR: my_fopen failed for '%s'", test_frm);
 
7593
    fprintf(stdout, "\n test cancelled");
 
7594
    exit(1);
 
7595
  }
 
7596
  if (!opt_silent)
 
7597
    fprintf(test_file, "this is a junk file for test");
 
7598
 
 
7599
  rc= mysql_query(mysql, "SHOW TABLE STATUS like 'test_frm_bug'");
 
7600
  myquery(rc);
 
7601
 
 
7602
  result= mysql_store_result(mysql);
 
7603
  mytest(result);/* It can't be NULL */
 
7604
 
 
7605
  rc= my_process_result_set(result);
 
7606
  DIE_UNLESS(rc == 1);
 
7607
 
 
7608
  mysql_data_seek(result, 0);
 
7609
 
 
7610
  row= mysql_fetch_row(result);
 
7611
  mytest(row);
 
7612
 
 
7613
  if (!opt_silent)
 
7614
    fprintf(stdout, "\n Comment: %s", row[17]);
 
7615
  DIE_UNLESS(row[17] != 0);
 
7616
 
 
7617
  mysql_free_result(result);
 
7618
  mysql_stmt_close(stmt);
 
7619
 
 
7620
  my_fclose(test_file, MYF(0));
 
7621
  mysql_query(mysql, "drop table if exists test_frm_bug");
 
7622
}
 
7623
 
 
7624
 
 
7625
/* Test DECIMAL conversion */
 
7626
 
 
7627
static void test_decimal_bug()
 
7628
{
 
7629
  MYSQL_STMT *stmt;
 
7630
  MYSQL_BIND my_bind[1];
 
7631
  char       data[30];
 
7632
  int        rc;
 
7633
  my_bool    is_null;
 
7634
 
 
7635
  myheader("test_decimal_bug");
 
7636
 
 
7637
  mysql_autocommit(mysql, TRUE);
 
7638
 
 
7639
  rc= mysql_query(mysql, "drop table if exists test_decimal_bug");
 
7640
  myquery(rc);
 
7641
 
 
7642
  rc= mysql_query(mysql, "create table test_decimal_bug(c1 decimal(10, 2))");
 
7643
  myquery(rc);
 
7644
 
 
7645
  rc= mysql_query(mysql, "insert into test_decimal_bug value(8), (10.22), (5.61)");
 
7646
  myquery(rc);
 
7647
 
 
7648
  stmt= mysql_simple_prepare(mysql, "select c1 from test_decimal_bug where c1= ?");
 
7649
  check_stmt(stmt);
 
7650
 
 
7651
  /*
 
7652
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
7653
    its members.
 
7654
  */
 
7655
  bzero((char*) my_bind, sizeof(my_bind));
 
7656
 
 
7657
  my_bind[0].buffer_type= MYSQL_TYPE_NEWDECIMAL;
 
7658
  my_bind[0].buffer= (void *)data;
 
7659
  my_bind[0].buffer_length= 25;
 
7660
  my_bind[0].is_null= &is_null;
 
7661
 
 
7662
  is_null= 0;
 
7663
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
7664
  check_execute(stmt, rc);
 
7665
 
 
7666
  strmov(data, "8.0");
 
7667
  rc= mysql_stmt_execute(stmt);
 
7668
  check_execute(stmt, rc);
 
7669
 
 
7670
  data[0]= 0;
 
7671
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
7672
  check_execute(stmt, rc);
 
7673
 
 
7674
  rc= mysql_stmt_fetch(stmt);
 
7675
  check_execute(stmt, rc);
 
7676
 
 
7677
  if (!opt_silent)
 
7678
    fprintf(stdout, "\n data: %s", data);
 
7679
  DIE_UNLESS(strcmp(data, "8.00") == 0);
 
7680
 
 
7681
  rc= mysql_stmt_fetch(stmt);
 
7682
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
7683
 
 
7684
  strmov(data, "5.61");
 
7685
  rc= mysql_stmt_execute(stmt);
 
7686
  check_execute(stmt, rc);
 
7687
 
 
7688
  data[0]= 0;
 
7689
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
7690
  check_execute(stmt, rc);
 
7691
 
 
7692
  rc= mysql_stmt_fetch(stmt);
 
7693
  check_execute(stmt, rc);
 
7694
 
 
7695
  if (!opt_silent)
 
7696
    fprintf(stdout, "\n data: %s", data);
 
7697
  DIE_UNLESS(strcmp(data, "5.61") == 0);
 
7698
 
 
7699
  rc= mysql_stmt_fetch(stmt);
 
7700
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
7701
 
 
7702
  is_null= 1;
 
7703
  rc= mysql_stmt_execute(stmt);
 
7704
  check_execute(stmt, rc);
 
7705
 
 
7706
  rc= mysql_stmt_fetch(stmt);
 
7707
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
7708
 
 
7709
  strmov(data, "10.22"); is_null= 0;
 
7710
  rc= mysql_stmt_execute(stmt);
 
7711
  check_execute(stmt, rc);
 
7712
 
 
7713
  data[0]= 0;
 
7714
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
7715
  check_execute(stmt, rc);
 
7716
 
 
7717
  rc= mysql_stmt_fetch(stmt);
 
7718
  check_execute(stmt, rc);
 
7719
 
 
7720
  if (!opt_silent)
 
7721
    fprintf(stdout, "\n data: %s", data);
 
7722
  DIE_UNLESS(strcmp(data, "10.22") == 0);
 
7723
 
 
7724
  rc= mysql_stmt_fetch(stmt);
 
7725
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
7726
 
 
7727
  mysql_stmt_close(stmt);
 
7728
}
 
7729
 
 
7730
 
 
7731
/* Test EXPLAIN bug (#115, reported by mark@mysql.com & georg@php.net). */
 
7732
 
 
7733
static void test_explain_bug()
 
7734
{
 
7735
  MYSQL_STMT *stmt;
 
7736
  MYSQL_RES  *result;
 
7737
  int        rc;
 
7738
 
 
7739
  myheader("test_explain_bug");
 
7740
 
 
7741
  mysql_autocommit(mysql, TRUE);
 
7742
 
 
7743
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_explain");
 
7744
  myquery(rc);
 
7745
 
 
7746
  rc= mysql_query(mysql, "CREATE TABLE test_explain(id int, name char(2))");
 
7747
  myquery(rc);
 
7748
 
 
7749
  stmt= mysql_simple_prepare(mysql, "explain test_explain");
 
7750
  check_stmt(stmt);
 
7751
 
 
7752
  rc= mysql_stmt_execute(stmt);
 
7753
  check_execute(stmt, rc);
 
7754
 
 
7755
  rc= my_process_stmt_result(stmt);
 
7756
  DIE_UNLESS(rc == 2);
 
7757
 
 
7758
  result= mysql_stmt_result_metadata(stmt);
 
7759
  mytest(result);
 
7760
 
 
7761
  if (!opt_silent)
 
7762
    fprintf(stdout, "\n total fields in the result: %d",
 
7763
            mysql_num_fields(result));
 
7764
  DIE_UNLESS(6 == mysql_num_fields(result));
 
7765
 
 
7766
  verify_prepare_field(result, 0, "Field", "COLUMN_NAME",
 
7767
                       mysql_get_server_version(mysql) <= 50000 ?
 
7768
                       MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
 
7769
                       0, 0, "", 64, 0);
 
7770
 
 
7771
  verify_prepare_field(result, 1, "Type", "COLUMN_TYPE", MYSQL_TYPE_BLOB,
 
7772
                       0, 0, "", 0, 0);
 
7773
 
 
7774
  verify_prepare_field(result, 2, "Null", "IS_NULLABLE",
 
7775
                       mysql_get_server_version(mysql) <= 50000 ?
 
7776
                       MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
 
7777
                       0, 0, "", 3, 0);
 
7778
 
 
7779
  verify_prepare_field(result, 3, "Key", "COLUMN_KEY",
 
7780
                       mysql_get_server_version(mysql) <= 50000 ?
 
7781
                       MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
 
7782
                       0, 0, "", 3, 0);
 
7783
 
 
7784
  if ( mysql_get_server_version(mysql) >= 50027 )
 
7785
  {
 
7786
    /*  The patch for bug#23037 changes column type of DEAULT to blob */
 
7787
    verify_prepare_field(result, 4, "Default", "COLUMN_DEFAULT",
 
7788
                         MYSQL_TYPE_BLOB, 0, 0, "", 0, 0);
 
7789
  }
 
7790
  else
 
7791
  {
 
7792
    verify_prepare_field(result, 4, "Default", "COLUMN_DEFAULT",
 
7793
                         mysql_get_server_version(mysql) >= 50027 ?
 
7794
                         MYSQL_TYPE_BLOB :
 
7795
                         mysql_get_server_version(mysql) <= 50000 ?
 
7796
                         MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
 
7797
                         0, 0, "",
 
7798
                         mysql_get_server_version(mysql) >= 50027 ? 0 :64, 0);
 
7799
  }
 
7800
 
 
7801
  verify_prepare_field(result, 5, "Extra", "EXTRA",
 
7802
                       mysql_get_server_version(mysql) <= 50000 ?
 
7803
                       MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
 
7804
                       0, 0, "", 27, 0);
 
7805
 
 
7806
  mysql_free_result(result);
 
7807
  mysql_stmt_close(stmt);
 
7808
 
 
7809
  stmt= mysql_simple_prepare(mysql, "explain select id, name FROM test_explain");
 
7810
  check_stmt(stmt);
 
7811
 
 
7812
  rc= mysql_stmt_execute(stmt);
 
7813
  check_execute(stmt, rc);
 
7814
 
 
7815
  rc= my_process_stmt_result(stmt);
 
7816
  DIE_UNLESS(rc == 1);
 
7817
 
 
7818
  result= mysql_stmt_result_metadata(stmt);
 
7819
  mytest(result);
 
7820
 
 
7821
  if (!opt_silent)
 
7822
    fprintf(stdout, "\n total fields in the result: %d",
 
7823
            mysql_num_fields(result));
 
7824
  DIE_UNLESS(10 == mysql_num_fields(result));
 
7825
 
 
7826
  verify_prepare_field(result, 0, "id", "", MYSQL_TYPE_LONGLONG,
 
7827
                       "", "", "", 3, 0);
 
7828
 
 
7829
  verify_prepare_field(result, 1, "select_type", "", MYSQL_TYPE_VAR_STRING,
 
7830
                       "", "", "", 19, 0);
 
7831
 
 
7832
  verify_prepare_field(result, 2, "table", "", MYSQL_TYPE_VAR_STRING,
 
7833
                       "", "", "", NAME_CHAR_LEN, 0);
 
7834
 
 
7835
  verify_prepare_field(result, 3, "type", "", MYSQL_TYPE_VAR_STRING,
 
7836
                       "", "", "", 10, 0);
 
7837
 
 
7838
  verify_prepare_field(result, 4, "possible_keys", "", MYSQL_TYPE_VAR_STRING,
 
7839
                       "", "", "", NAME_CHAR_LEN*MAX_KEY, 0);
 
7840
 
 
7841
  verify_prepare_field(result, 5, "key", "", MYSQL_TYPE_VAR_STRING,
 
7842
                       "", "", "", NAME_CHAR_LEN, 0);
 
7843
 
 
7844
  if (mysql_get_server_version(mysql) <= 50000)
 
7845
  {
 
7846
    verify_prepare_field(result, 6, "key_len", "", MYSQL_TYPE_LONGLONG, "",
 
7847
                         "", "", 3, 0);
 
7848
  }
 
7849
  else
 
7850
  {
 
7851
    verify_prepare_field(result, 6, "key_len", "", MYSQL_TYPE_VAR_STRING, "", 
 
7852
                         "", "", 320, 0);
 
7853
  }
 
7854
 
 
7855
  verify_prepare_field(result, 7, "ref", "", MYSQL_TYPE_VAR_STRING,
 
7856
                       "", "", "", NAME_CHAR_LEN*16, 0);
 
7857
 
 
7858
  verify_prepare_field(result, 8, "rows", "", MYSQL_TYPE_LONGLONG,
 
7859
                       "", "", "", 10, 0);
 
7860
 
 
7861
  verify_prepare_field(result, 9, "Extra", "", MYSQL_TYPE_VAR_STRING,
 
7862
                       "", "", "", 255, 0);
 
7863
 
 
7864
  mysql_free_result(result);
 
7865
  mysql_stmt_close(stmt);
 
7866
}
 
7867
 
 
7868
#ifdef NOT_YET_WORKING
 
7869
 
 
7870
/*
 
7871
  Test math functions.
 
7872
  Bug #148 (reported by salle@mysql.com).
 
7873
*/
 
7874
 
 
7875
#define myerrno(n) check_errcode(n)
 
7876
 
 
7877
static void check_errcode(const unsigned int err)
 
7878
{
 
7879
  if (!opt_silent || mysql_errno(mysql) != err)
 
7880
  {
 
7881
    if (mysql->server_version)
 
7882
      fprintf(stdout, "\n [MySQL-%s]", mysql->server_version);
 
7883
    else
 
7884
      fprintf(stdout, "\n [MySQL]");
 
7885
    fprintf(stdout, "[%d] %s\n", mysql_errno(mysql), mysql_error(mysql));
 
7886
  }
 
7887
  DIE_UNLESS(mysql_errno(mysql) == err);
 
7888
}
 
7889
 
 
7890
 
 
7891
static void test_drop_temp()
 
7892
{
 
7893
  int rc;
 
7894
 
 
7895
  myheader("test_drop_temp");
 
7896
 
 
7897
  rc= mysql_query(mysql, "DROP DATABASE IF EXISTS test_drop_temp_db");
 
7898
  myquery(rc);
 
7899
 
 
7900
  rc= mysql_query(mysql, "CREATE DATABASE test_drop_temp_db");
 
7901
  myquery(rc);
 
7902
 
 
7903
  rc= mysql_query(mysql, "CREATE TABLE test_drop_temp_db.t1(c1 int, c2 char(1))");
 
7904
  myquery(rc);
 
7905
 
 
7906
  rc= mysql_query(mysql, "delete from mysql.db where Db='test_drop_temp_db'");
 
7907
  myquery(rc);
 
7908
 
 
7909
  rc= mysql_query(mysql, "delete from mysql.db where Db='test_drop_temp_db'");
 
7910
  myquery(rc);
 
7911
 
 
7912
  strxmov(query, "GRANT SELECT, USAGE, DROP ON test_drop_temp_db.* TO test_temp@",
 
7913
                opt_host ? opt_host : "localhost", NullS);
 
7914
 
 
7915
  if (mysql_query(mysql, query))
 
7916
  {
 
7917
    myerror("GRANT failed");
 
7918
 
 
7919
    /*
 
7920
       If server started with --skip-grant-tables, skip this test, else
 
7921
       exit to indicate an error
 
7922
 
 
7923
       ER_UNKNOWN_COM_ERROR= 1047
 
7924
     */
 
7925
    if (mysql_errno(mysql) != 1047)
 
7926
      exit(1);
 
7927
  }
 
7928
  else
 
7929
  {
 
7930
    MYSQL *org_mysql= mysql, *lmysql;
 
7931
 
 
7932
    if (!opt_silent)
 
7933
      fprintf(stdout, "\n Establishing a test connection ...");
 
7934
    if (!(lmysql= mysql_init(NULL)))
 
7935
    {
 
7936
      myerror("mysql_init() failed");
 
7937
      exit(1);
 
7938
    }
 
7939
 
 
7940
    rc= mysql_query(mysql, "flush privileges");
 
7941
    myquery(rc);
 
7942
 
 
7943
    if (!(mysql_real_connect(lmysql, opt_host ? opt_host : "localhost", "test_temp",
 
7944
                             "", "test_drop_temp_db", opt_port,
 
7945
                             opt_unix_socket, 0)))
 
7946
    {
 
7947
      mysql= lmysql;
 
7948
      myerror("connection failed");
 
7949
      mysql_close(lmysql);
 
7950
      exit(1);
 
7951
    }
 
7952
    lmysql->reconnect= 1;
 
7953
    if (!opt_silent)
 
7954
      fprintf(stdout, "OK");
 
7955
 
 
7956
    mysql= lmysql;
 
7957
    rc= mysql_query(mysql, "INSERT INTO t1 VALUES(10, 'C')");
 
7958
    myerrno((uint)1142);
 
7959
 
 
7960
    rc= mysql_query(mysql, "DROP TABLE t1");
 
7961
    myerrno((uint)1142);
 
7962
 
 
7963
    mysql= org_mysql;
 
7964
    rc= mysql_query(mysql, "CREATE TEMPORARY TABLE test_drop_temp_db.t1(c1 int)");
 
7965
    myquery(rc);
 
7966
 
 
7967
    rc= mysql_query(mysql, "CREATE TEMPORARY TABLE test_drop_temp_db.t2 LIKE test_drop_temp_db.t1");
 
7968
    myquery(rc);
 
7969
 
 
7970
    mysql= lmysql;
 
7971
 
 
7972
    rc= mysql_query(mysql, "DROP TABLE t1, t2");
 
7973
    myquery_r(rc);
 
7974
 
 
7975
    rc= mysql_query(mysql, "DROP TEMPORARY TABLE t1");
 
7976
    myquery_r(rc);
 
7977
 
 
7978
    rc= mysql_query(mysql, "DROP TEMPORARY TABLE t2");
 
7979
    myquery_r(rc);
 
7980
 
 
7981
    mysql_close(lmysql);
 
7982
    mysql= org_mysql;
 
7983
 
 
7984
    rc= mysql_query(mysql, "drop database test_drop_temp_db");
 
7985
    myquery(rc);
 
7986
    DIE_UNLESS(1 == mysql_affected_rows(mysql));
 
7987
 
 
7988
    rc= mysql_query(mysql, "delete from mysql.user where User='test_temp'");
 
7989
    myquery(rc);
 
7990
    DIE_UNLESS(1 == mysql_affected_rows(mysql));
 
7991
 
 
7992
 
 
7993
    rc= mysql_query(mysql, "delete from mysql.tables_priv where User='test_temp'");
 
7994
    myquery(rc);
 
7995
    DIE_UNLESS(1 == mysql_affected_rows(mysql));
 
7996
  }
 
7997
}
 
7998
#endif
 
7999
 
 
8000
 
 
8001
/* Test warnings for cuted rows */
 
8002
 
 
8003
static void test_cuted_rows()
 
8004
{
 
8005
  int        rc, count;
 
8006
  MYSQL_RES  *result;
 
8007
 
 
8008
  myheader("test_cuted_rows");
 
8009
 
 
8010
  mysql_query(mysql, "DROP TABLE if exists t1");
 
8011
  mysql_query(mysql, "DROP TABLE if exists t2");
 
8012
 
 
8013
  rc= mysql_query(mysql, "CREATE TABLE t1(c1 tinyint)");
 
8014
  myquery(rc);
 
8015
 
 
8016
  rc= mysql_query(mysql, "CREATE TABLE t2(c1 int not null)");
 
8017
  myquery(rc);
 
8018
 
 
8019
  rc= mysql_query(mysql, "INSERT INTO t1 values(10), (NULL), (NULL)");
 
8020
  myquery(rc);
 
8021
 
 
8022
  count= mysql_warning_count(mysql);
 
8023
  if (!opt_silent)
 
8024
    fprintf(stdout, "\n total warnings: %d", count);
 
8025
  DIE_UNLESS(count == 0);
 
8026
 
 
8027
  rc= mysql_query(mysql, "INSERT INTO t2 SELECT * FROM t1");
 
8028
  myquery(rc);
 
8029
 
 
8030
  count= mysql_warning_count(mysql);
 
8031
  if (!opt_silent)
 
8032
    fprintf(stdout, "\n total warnings: %d", count);
 
8033
  DIE_UNLESS(count == 2);
 
8034
 
 
8035
  rc= mysql_query(mysql, "SHOW WARNINGS");
 
8036
  myquery(rc);
 
8037
 
 
8038
  result= mysql_store_result(mysql);
 
8039
  mytest(result);
 
8040
 
 
8041
  rc= my_process_result_set(result);
 
8042
  DIE_UNLESS(rc == 2);
 
8043
  mysql_free_result(result);
 
8044
 
 
8045
  rc= mysql_query(mysql, "INSERT INTO t1 VALUES('junk'), (876789)");
 
8046
  myquery(rc);
 
8047
 
 
8048
  count= mysql_warning_count(mysql);
 
8049
  if (!opt_silent)
 
8050
    fprintf(stdout, "\n total warnings: %d", count);
 
8051
  DIE_UNLESS(count == 2);
 
8052
 
 
8053
  rc= mysql_query(mysql, "SHOW WARNINGS");
 
8054
  myquery(rc);
 
8055
 
 
8056
  result= mysql_store_result(mysql);
 
8057
  mytest(result);
 
8058
 
 
8059
  rc= my_process_result_set(result);
 
8060
  DIE_UNLESS(rc == 2);
 
8061
  mysql_free_result(result);
 
8062
}
 
8063
 
 
8064
 
 
8065
/* Test update/binary logs */
 
8066
 
 
8067
static void test_logs()
 
8068
{
 
8069
  MYSQL_STMT *stmt;
 
8070
  MYSQL_BIND my_bind[2];
 
8071
  char       data[255];
 
8072
  ulong      length;
 
8073
  int        rc;
 
8074
  short      id;
 
8075
 
 
8076
  myheader("test_logs");
 
8077
 
 
8078
 
 
8079
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_logs");
 
8080
  myquery(rc);
 
8081
 
 
8082
  rc= mysql_query(mysql, "CREATE TABLE test_logs(id smallint, name varchar(20))");
 
8083
  myquery(rc);
 
8084
 
 
8085
  strmov((char *)data, "INSERT INTO test_logs VALUES(?, ?)");
 
8086
  stmt= mysql_simple_prepare(mysql, data);
 
8087
  check_stmt(stmt);
 
8088
 
 
8089
  /*
 
8090
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
8091
    its members.
 
8092
  */
 
8093
  bzero((char*) my_bind, sizeof(my_bind));
 
8094
 
 
8095
  my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
 
8096
  my_bind[0].buffer= (void *)&id;
 
8097
 
 
8098
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
8099
  my_bind[1].buffer= (void *)&data;
 
8100
  my_bind[1].buffer_length= 255;
 
8101
  my_bind[1].length= &length;
 
8102
 
 
8103
  id= 9876;
 
8104
  length= (ulong)(strmov((char *)data, "MySQL - Open Source Database")- data);
 
8105
 
 
8106
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
8107
  check_execute(stmt, rc);
 
8108
 
 
8109
  rc= mysql_stmt_execute(stmt);
 
8110
  check_execute(stmt, rc);
 
8111
 
 
8112
  strmov((char *)data, "'");
 
8113
  length= 1;
 
8114
 
 
8115
  rc= mysql_stmt_execute(stmt);
 
8116
  check_execute(stmt, rc);
 
8117
 
 
8118
  strmov((char *)data, "\"");
 
8119
  length= 1;
 
8120
 
 
8121
  rc= mysql_stmt_execute(stmt);
 
8122
  check_execute(stmt, rc);
 
8123
 
 
8124
  length= (ulong)(strmov((char *)data, "my\'sql\'")-data);
 
8125
  rc= mysql_stmt_execute(stmt);
 
8126
  check_execute(stmt, rc);
 
8127
 
 
8128
  length= (ulong)(strmov((char *)data, "my\"sql\"")-data);
 
8129
  rc= mysql_stmt_execute(stmt);
 
8130
  check_execute(stmt, rc);
 
8131
 
 
8132
  mysql_stmt_close(stmt);
 
8133
 
 
8134
  strmov((char *)data, "INSERT INTO test_logs VALUES(20, 'mysql')");
 
8135
  stmt= mysql_simple_prepare(mysql, data);
 
8136
  check_stmt(stmt);
 
8137
 
 
8138
  rc= mysql_stmt_execute(stmt);
 
8139
  check_execute(stmt, rc);
 
8140
 
 
8141
  rc= mysql_stmt_execute(stmt);
 
8142
  check_execute(stmt, rc);
 
8143
 
 
8144
  mysql_stmt_close(stmt);
 
8145
 
 
8146
  strmov((char *)data, "SELECT * FROM test_logs WHERE id=?");
 
8147
  stmt= mysql_simple_prepare(mysql, data);
 
8148
  check_stmt(stmt);
 
8149
 
 
8150
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
8151
  check_execute(stmt, rc);
 
8152
 
 
8153
  rc= mysql_stmt_execute(stmt);
 
8154
  check_execute(stmt, rc);
 
8155
 
 
8156
  my_bind[1].buffer_length= 255;
 
8157
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
8158
  check_execute(stmt, rc);
 
8159
 
 
8160
  rc= mysql_stmt_fetch(stmt);
 
8161
  check_execute(stmt, rc);
 
8162
 
 
8163
  if (!opt_silent)
 
8164
  {
 
8165
    fprintf(stdout, "id    : %d\n", id);
 
8166
    fprintf(stdout, "name  : %s(%ld)\n", data, length);
 
8167
  }
 
8168
 
 
8169
  DIE_UNLESS(id == 9876);
 
8170
  DIE_UNLESS(length == 19 || length == 20); /* Due to VARCHAR(20) */
 
8171
  DIE_UNLESS(is_prefix(data, "MySQL - Open Source") == 1);
 
8172
 
 
8173
  rc= mysql_stmt_fetch(stmt);
 
8174
  check_execute(stmt, rc);
 
8175
 
 
8176
  if (!opt_silent)
 
8177
    fprintf(stdout, "\n name  : %s(%ld)", data, length);
 
8178
 
 
8179
  DIE_UNLESS(length == 1);
 
8180
  DIE_UNLESS(strcmp(data, "'") == 0);
 
8181
 
 
8182
  rc= mysql_stmt_fetch(stmt);
 
8183
  check_execute(stmt, rc);
 
8184
 
 
8185
  if (!opt_silent)
 
8186
    fprintf(stdout, "\n name  : %s(%ld)", data, length);
 
8187
 
 
8188
  DIE_UNLESS(length == 1);
 
8189
  DIE_UNLESS(strcmp(data, "\"") == 0);
 
8190
 
 
8191
  rc= mysql_stmt_fetch(stmt);
 
8192
  check_execute(stmt, rc);
 
8193
 
 
8194
  if (!opt_silent)
 
8195
    fprintf(stdout, "\n name  : %s(%ld)", data, length);
 
8196
 
 
8197
  DIE_UNLESS(length == 7);
 
8198
  DIE_UNLESS(strcmp(data, "my\'sql\'") == 0);
 
8199
 
 
8200
  rc= mysql_stmt_fetch(stmt);
 
8201
  check_execute(stmt, rc);
 
8202
 
 
8203
  if (!opt_silent)
 
8204
    fprintf(stdout, "\n name  : %s(%ld)", data, length);
 
8205
 
 
8206
  DIE_UNLESS(length == 7);
 
8207
  /*DIE_UNLESS(strcmp(data, "my\"sql\"") == 0); */
 
8208
 
 
8209
  rc= mysql_stmt_fetch(stmt);
 
8210
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
8211
 
 
8212
  mysql_stmt_close(stmt);
 
8213
 
 
8214
  rc= mysql_query(mysql, "DROP TABLE test_logs");
 
8215
  myquery(rc);
 
8216
}
 
8217
 
 
8218
 
 
8219
/* Test 'n' statements create and close */
 
8220
 
 
8221
static void test_nstmts()
 
8222
{
 
8223
  MYSQL_STMT  *stmt;
 
8224
  char        query[255];
 
8225
  int         rc;
 
8226
  static uint i, total_stmts= 2000;
 
8227
  MYSQL_BIND  my_bind[1];
 
8228
 
 
8229
  myheader("test_nstmts");
 
8230
 
 
8231
  mysql_autocommit(mysql, TRUE);
 
8232
 
 
8233
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_nstmts");
 
8234
  myquery(rc);
 
8235
 
 
8236
  rc= mysql_query(mysql, "CREATE TABLE test_nstmts(id int)");
 
8237
  myquery(rc);
 
8238
 
 
8239
  /*
 
8240
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
8241
    its members.
 
8242
  */
 
8243
  bzero((char*) my_bind, sizeof(my_bind));
 
8244
 
 
8245
  my_bind[0].buffer= (void *)&i;
 
8246
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
8247
 
 
8248
  for (i= 0; i < total_stmts; i++)
 
8249
  {
 
8250
    if (!opt_silent)
 
8251
      fprintf(stdout, "\r stmt: %d", i);
 
8252
 
 
8253
    strmov(query, "insert into test_nstmts values(?)");
 
8254
    stmt= mysql_simple_prepare(mysql, query);
 
8255
    check_stmt(stmt);
 
8256
 
 
8257
    rc= mysql_stmt_bind_param(stmt, my_bind);
 
8258
    check_execute(stmt, rc);
 
8259
 
 
8260
    rc= mysql_stmt_execute(stmt);
 
8261
    check_execute(stmt, rc);
 
8262
 
 
8263
    mysql_stmt_close(stmt);
 
8264
  }
 
8265
 
 
8266
  stmt= mysql_simple_prepare(mysql, " select count(*) from test_nstmts");
 
8267
  check_stmt(stmt);
 
8268
 
 
8269
  rc= mysql_stmt_execute(stmt);
 
8270
  check_execute(stmt, rc);
 
8271
 
 
8272
  i= 0;
 
8273
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
8274
  check_execute(stmt, rc);
 
8275
 
 
8276
  rc= mysql_stmt_fetch(stmt);
 
8277
  check_execute(stmt, rc);
 
8278
  if (!opt_silent)
 
8279
    fprintf(stdout, "\n total rows: %d", i);
 
8280
  DIE_UNLESS( i == total_stmts);
 
8281
 
 
8282
  rc= mysql_stmt_fetch(stmt);
 
8283
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
8284
 
 
8285
  mysql_stmt_close(stmt);
 
8286
 
 
8287
  rc= mysql_query(mysql, "DROP TABLE test_nstmts");
 
8288
  myquery(rc);
 
8289
}
 
8290
 
 
8291
 
 
8292
/* Test stmt seek() functions */
 
8293
 
 
8294
static void test_fetch_seek()
 
8295
{
 
8296
  MYSQL_STMT *stmt;
 
8297
  MYSQL_BIND my_bind[3];
 
8298
  MYSQL_ROW_OFFSET row;
 
8299
  int        rc;
 
8300
  int32      c1;
 
8301
  char       c2[11], c3[20];
 
8302
 
 
8303
  myheader("test_fetch_seek");
 
8304
  rc= mysql_query(mysql, "drop table if exists t1");
 
8305
 
 
8306
  myquery(rc);
 
8307
 
 
8308
  rc= mysql_query(mysql, "create table t1(c1 int primary key auto_increment, c2 char(10), c3 timestamp)");
 
8309
  myquery(rc);
 
8310
 
 
8311
  rc= mysql_query(mysql, "insert into t1(c2) values('venu'), ('mysql'), ('open'), ('source')");
 
8312
  myquery(rc);
 
8313
 
 
8314
  stmt= mysql_simple_prepare(mysql, "select * from t1");
 
8315
  check_stmt(stmt);
 
8316
 
 
8317
  bzero((char*) my_bind, sizeof(my_bind));
 
8318
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
8319
  my_bind[0].buffer= (void *)&c1;
 
8320
 
 
8321
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
8322
  my_bind[1].buffer= (void *)c2;
 
8323
  my_bind[1].buffer_length= sizeof(c2);
 
8324
 
 
8325
  my_bind[2]= my_bind[1];
 
8326
  my_bind[2].buffer= (void *)c3;
 
8327
  my_bind[2].buffer_length= sizeof(c3);
 
8328
 
 
8329
  rc= mysql_stmt_execute(stmt);
 
8330
  check_execute(stmt, rc);
 
8331
 
 
8332
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
8333
  check_execute(stmt, rc);
 
8334
 
 
8335
  rc= mysql_stmt_store_result(stmt);
 
8336
  check_execute(stmt, rc);
 
8337
 
 
8338
  rc= mysql_stmt_fetch(stmt);
 
8339
  check_execute(stmt, rc);
 
8340
 
 
8341
  if (!opt_silent)
 
8342
    fprintf(stdout, "\n row 0: %ld, %s, %s", (long) c1, c2, c3);
 
8343
 
 
8344
  row= mysql_stmt_row_tell(stmt);
 
8345
 
 
8346
  row= mysql_stmt_row_seek(stmt, row);
 
8347
 
 
8348
  rc= mysql_stmt_fetch(stmt);
 
8349
  check_execute(stmt, rc);
 
8350
 
 
8351
  if (!opt_silent)
 
8352
    fprintf(stdout, "\n row 2: %ld, %s, %s", (long) c1, c2, c3);
 
8353
 
 
8354
  row= mysql_stmt_row_seek(stmt, row);
 
8355
 
 
8356
  rc= mysql_stmt_fetch(stmt);
 
8357
  check_execute(stmt, rc);
 
8358
 
 
8359
  if (!opt_silent)
 
8360
    fprintf(stdout, "\n row 2: %ld, %s, %s", (long) c1, c2, c3);
 
8361
 
 
8362
  mysql_stmt_data_seek(stmt, 0);
 
8363
 
 
8364
  rc= mysql_stmt_fetch(stmt);
 
8365
  check_execute(stmt, rc);
 
8366
 
 
8367
  if (!opt_silent)
 
8368
    fprintf(stdout, "\n row 0: %ld, %s, %s", (long) c1, c2, c3);
 
8369
 
 
8370
  rc= mysql_stmt_fetch(stmt);
 
8371
  check_execute(stmt, rc);
 
8372
 
 
8373
  rc= mysql_stmt_fetch(stmt);
 
8374
  check_execute(stmt, rc);
 
8375
 
 
8376
  rc= mysql_stmt_fetch(stmt);
 
8377
  check_execute(stmt, rc);
 
8378
 
 
8379
  rc= mysql_stmt_fetch(stmt);
 
8380
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
8381
 
 
8382
  mysql_stmt_close(stmt);
 
8383
  myquery(mysql_query(mysql, "drop table t1"));
 
8384
}
 
8385
 
 
8386
 
 
8387
/* Test mysql_stmt_fetch_column() with offset */
 
8388
 
 
8389
static void test_fetch_offset()
 
8390
{
 
8391
  MYSQL_STMT *stmt;
 
8392
  MYSQL_BIND my_bind[1];
 
8393
  char       data[11];
 
8394
  ulong      length;
 
8395
  int        rc;
 
8396
  my_bool    is_null;
 
8397
 
 
8398
 
 
8399
  myheader("test_fetch_offset");
 
8400
 
 
8401
  rc= mysql_query(mysql, "drop table if exists t1");
 
8402
  myquery(rc);
 
8403
 
 
8404
  rc= mysql_query(mysql, "create table t1(a char(10))");
 
8405
  myquery(rc);
 
8406
 
 
8407
  rc= mysql_query(mysql, "insert into t1 values('abcdefghij'), (null)");
 
8408
  myquery(rc);
 
8409
 
 
8410
  stmt= mysql_simple_prepare(mysql, "select * from t1");
 
8411
  check_stmt(stmt);
 
8412
 
 
8413
  bzero((char*) my_bind, sizeof(my_bind));
 
8414
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
8415
  my_bind[0].buffer= (void *)data;
 
8416
  my_bind[0].buffer_length= 11;
 
8417
  my_bind[0].is_null= &is_null;
 
8418
  my_bind[0].length= &length;
 
8419
 
 
8420
  rc= mysql_stmt_execute(stmt);
 
8421
  check_execute(stmt, rc);
 
8422
 
 
8423
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
 
8424
  check_execute_r(stmt, rc);
 
8425
 
 
8426
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
8427
  check_execute(stmt, rc);
 
8428
 
 
8429
  rc= mysql_stmt_store_result(stmt);
 
8430
  check_execute(stmt, rc);
 
8431
 
 
8432
  rc= mysql_stmt_fetch(stmt);
 
8433
  check_execute(stmt, rc);
 
8434
 
 
8435
  data[0]= '\0';
 
8436
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
 
8437
  check_execute(stmt, rc);
 
8438
  if (!opt_silent)
 
8439
    fprintf(stdout, "\n col 1: %s (%ld)", data, length);
 
8440
  DIE_UNLESS(strncmp(data, "abcd", 4) == 0 && length == 10);
 
8441
 
 
8442
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 5);
 
8443
  check_execute(stmt, rc);
 
8444
  if (!opt_silent)
 
8445
    fprintf(stdout, "\n col 1: %s (%ld)", data, length);
 
8446
  DIE_UNLESS(strncmp(data, "fg", 2) == 0 && length == 10);
 
8447
 
 
8448
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 9);
 
8449
  check_execute(stmt, rc);
 
8450
  if (!opt_silent)
 
8451
    fprintf(stdout, "\n col 0: %s (%ld)", data, length);
 
8452
  DIE_UNLESS(strncmp(data, "j", 1) == 0 && length == 10);
 
8453
 
 
8454
  rc= mysql_stmt_fetch(stmt);
 
8455
  check_execute(stmt, rc);
 
8456
 
 
8457
  is_null= 0;
 
8458
 
 
8459
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
 
8460
  check_execute(stmt, rc);
 
8461
 
 
8462
  DIE_UNLESS(is_null == 1);
 
8463
 
 
8464
  rc= mysql_stmt_fetch(stmt);
 
8465
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
8466
 
 
8467
  rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
 
8468
  check_execute_r(stmt, rc);
 
8469
 
 
8470
  mysql_stmt_close(stmt);
 
8471
 
 
8472
  myquery(mysql_query(mysql, "drop table t1"));
 
8473
}
 
8474
 
 
8475
 
 
8476
/* Test mysql_stmt_fetch_column() */
 
8477
 
 
8478
static void test_fetch_column()
 
8479
{
 
8480
  MYSQL_STMT *stmt;
 
8481
  MYSQL_BIND my_bind[2];
 
8482
  char       c2[20], bc2[20];
 
8483
  ulong      l1, l2, bl1, bl2;
 
8484
  int        rc, c1, bc1;
 
8485
 
 
8486
  myheader("test_fetch_column");
 
8487
 
 
8488
  rc= mysql_query(mysql, "drop table if exists t1");
 
8489
  myquery(rc);
 
8490
 
 
8491
  rc= mysql_query(mysql, "create table t1(c1 int primary key auto_increment, c2 char(10))");
 
8492
  myquery(rc);
 
8493
 
 
8494
  rc= mysql_query(mysql, "insert into t1(c2) values('venu'), ('mysql')");
 
8495
  myquery(rc);
 
8496
 
 
8497
  stmt= mysql_simple_prepare(mysql, "select * from t1 order by c2 desc");
 
8498
  check_stmt(stmt);
 
8499
 
 
8500
  bzero((char*) my_bind, sizeof(my_bind));
 
8501
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
8502
  my_bind[0].buffer= (void *)&bc1;
 
8503
  my_bind[0].buffer_length= 0;
 
8504
  my_bind[0].is_null= 0;
 
8505
  my_bind[0].length= &bl1;
 
8506
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
8507
  my_bind[1].buffer= (void *)bc2;
 
8508
  my_bind[1].buffer_length= 7;
 
8509
  my_bind[1].is_null= 0;
 
8510
  my_bind[1].length= &bl2;
 
8511
 
 
8512
  rc= mysql_stmt_execute(stmt);
 
8513
  check_execute(stmt, rc);
 
8514
 
 
8515
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
8516
  check_execute(stmt, rc);
 
8517
 
 
8518
  rc= mysql_stmt_store_result(stmt);
 
8519
  check_execute(stmt, rc);
 
8520
 
 
8521
  rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0); /* No-op at this point */
 
8522
  check_execute_r(stmt, rc);
 
8523
 
 
8524
  rc= mysql_stmt_fetch(stmt);
 
8525
  check_execute(stmt, rc);
 
8526
 
 
8527
  if (!opt_silent)
 
8528
    fprintf(stdout, "\n row 0: %d, %s", bc1, bc2);
 
8529
 
 
8530
  c2[0]= '\0'; l2= 0;
 
8531
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
8532
  my_bind[0].buffer= (void *)c2;
 
8533
  my_bind[0].buffer_length= 7;
 
8534
  my_bind[0].is_null= 0;
 
8535
  my_bind[0].length= &l2;
 
8536
 
 
8537
  rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
 
8538
  check_execute(stmt, rc);
 
8539
  if (!opt_silent)
 
8540
    fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
 
8541
  DIE_UNLESS(strncmp(c2, "venu", 4) == 0 && l2 == 4);
 
8542
 
 
8543
  c2[0]= '\0'; l2= 0;
 
8544
  rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
 
8545
  check_execute(stmt, rc);
 
8546
  if (!opt_silent)
 
8547
    fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
 
8548
  DIE_UNLESS(strcmp(c2, "venu") == 0 && l2 == 4);
 
8549
 
 
8550
  c1= 0;
 
8551
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
8552
  my_bind[0].buffer= (void *)&c1;
 
8553
  my_bind[0].buffer_length= 0;
 
8554
  my_bind[0].is_null= 0;
 
8555
  my_bind[0].length= &l1;
 
8556
 
 
8557
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
 
8558
  check_execute(stmt, rc);
 
8559
  if (!opt_silent)
 
8560
    fprintf(stdout, "\n col 0: %d(%ld)", c1, l1);
 
8561
  DIE_UNLESS(c1 == 1 && l1 == 4);
 
8562
 
 
8563
  rc= mysql_stmt_fetch(stmt);
 
8564
  check_execute(stmt, rc);
 
8565
 
 
8566
  if (!opt_silent)
 
8567
    fprintf(stdout, "\n row 1: %d, %s", bc1, bc2);
 
8568
 
 
8569
  c2[0]= '\0'; l2= 0;
 
8570
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
8571
  my_bind[0].buffer= (void *)c2;
 
8572
  my_bind[0].buffer_length= 7;
 
8573
  my_bind[0].is_null= 0;
 
8574
  my_bind[0].length= &l2;
 
8575
 
 
8576
  rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
 
8577
  check_execute(stmt, rc);
 
8578
  if (!opt_silent)
 
8579
    fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
 
8580
  DIE_UNLESS(strncmp(c2, "mysq", 4) == 0 && l2 == 5);
 
8581
 
 
8582
  c2[0]= '\0'; l2= 0;
 
8583
  rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
 
8584
  check_execute(stmt, rc);
 
8585
  if (!opt_silent)
 
8586
    fprintf(stdout, "\n col 1: %si(%ld)", c2, l2);
 
8587
  DIE_UNLESS(strcmp(c2, "mysql") == 0 && l2 == 5);
 
8588
 
 
8589
  c1= 0;
 
8590
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
8591
  my_bind[0].buffer= (void *)&c1;
 
8592
  my_bind[0].buffer_length= 0;
 
8593
  my_bind[0].is_null= 0;
 
8594
  my_bind[0].length= &l1;
 
8595
 
 
8596
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
 
8597
  check_execute(stmt, rc);
 
8598
  if (!opt_silent)
 
8599
    fprintf(stdout, "\n col 0: %d(%ld)", c1, l1);
 
8600
  DIE_UNLESS(c1 == 2 && l1 == 4);
 
8601
 
 
8602
  rc= mysql_stmt_fetch(stmt);
 
8603
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
8604
 
 
8605
  rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
 
8606
  check_execute_r(stmt, rc);
 
8607
 
 
8608
  mysql_stmt_close(stmt);
 
8609
  myquery(mysql_query(mysql, "drop table t1"));
 
8610
}
 
8611
 
 
8612
 
 
8613
/* Test mysql_list_fields() */
 
8614
 
 
8615
static void test_list_fields()
 
8616
{
 
8617
  MYSQL_RES *result;
 
8618
  int rc;
 
8619
  myheader("test_list_fields");
 
8620
 
 
8621
  rc= mysql_query(mysql, "drop table if exists t1");
 
8622
  myquery(rc);
 
8623
 
 
8624
  rc= mysql_query(mysql, "create table t1(c1 int primary key auto_increment, c2 char(10) default 'mysql')");
 
8625
  myquery(rc);
 
8626
 
 
8627
  result= mysql_list_fields(mysql, "t1", NULL);
 
8628
  mytest(result);
 
8629
 
 
8630
  rc= my_process_result_set(result);
 
8631
  DIE_UNLESS(rc == 0);
 
8632
 
 
8633
  verify_prepare_field(result, 0, "c1", "c1", MYSQL_TYPE_LONG,
 
8634
                       "t1", "t1",
 
8635
                       current_db, 11, "0");
 
8636
 
 
8637
  verify_prepare_field(result, 1, "c2", "c2", MYSQL_TYPE_STRING,
 
8638
                       "t1", "t1",
 
8639
                       current_db, 10, "mysql");
 
8640
 
 
8641
  mysql_free_result(result);
 
8642
  myquery(mysql_query(mysql, "drop table t1"));
 
8643
}
 
8644
 
 
8645
 
 
8646
static void test_bug19671()
 
8647
{
 
8648
  MYSQL_RES *result;
 
8649
  int rc;
 
8650
  myheader("test_bug19671");
 
8651
 
 
8652
  mysql_query(mysql, "set sql_mode=''");
 
8653
  rc= mysql_query(mysql, "drop table if exists t1");
 
8654
  myquery(rc);
 
8655
 
 
8656
  rc= mysql_query(mysql, "drop view if exists v1");
 
8657
  myquery(rc);
 
8658
 
 
8659
  rc= mysql_query(mysql, "create table t1(f1 int)");
 
8660
  myquery(rc);
 
8661
 
 
8662
  rc= mysql_query(mysql, "create view v1 as select va.* from t1 va");
 
8663
  myquery(rc);
 
8664
 
 
8665
  result= mysql_list_fields(mysql, "v1", NULL);
 
8666
  mytest(result);
 
8667
 
 
8668
  rc= my_process_result_set(result);
 
8669
  DIE_UNLESS(rc == 0);
 
8670
 
 
8671
  verify_prepare_field(result, 0, "f1", "f1", MYSQL_TYPE_LONG,
 
8672
                       "v1", "v1", current_db, 11, "0");
 
8673
 
 
8674
  mysql_free_result(result);
 
8675
  myquery(mysql_query(mysql, "drop view v1"));
 
8676
  myquery(mysql_query(mysql, "drop table t1"));
 
8677
}
 
8678
 
 
8679
 
 
8680
/* Test a memory ovverun bug */
 
8681
 
 
8682
static void test_mem_overun()
 
8683
{
 
8684
  char       buffer[10000], field[10];
 
8685
  MYSQL_STMT *stmt;
 
8686
  MYSQL_RES  *field_res;
 
8687
  int        rc, i, length;
 
8688
 
 
8689
  myheader("test_mem_overun");
 
8690
 
 
8691
  /*
 
8692
    Test a memory ovverun bug when a table had 1000 fields with
 
8693
    a row of data
 
8694
  */
 
8695
  rc= mysql_query(mysql, "drop table if exists t_mem_overun");
 
8696
  myquery(rc);
 
8697
 
 
8698
  strxmov(buffer, "create table t_mem_overun(", NullS);
 
8699
  for (i= 0; i < 1000; i++)
 
8700
  {
 
8701
    sprintf(field, "c%d int", i);
 
8702
    strxmov(buffer, buffer, field, ", ", NullS);
 
8703
  }
 
8704
  length= strlen(buffer);
 
8705
  buffer[length-2]= ')';
 
8706
  buffer[--length]= '\0';
 
8707
 
 
8708
  rc= mysql_real_query(mysql, buffer, length);
 
8709
  myquery(rc);
 
8710
 
 
8711
  strxmov(buffer, "insert into t_mem_overun values(", NullS);
 
8712
  for (i= 0; i < 1000; i++)
 
8713
  {
 
8714
    strxmov(buffer, buffer, "1, ", NullS);
 
8715
  }
 
8716
  length= strlen(buffer);
 
8717
  buffer[length-2]= ')';
 
8718
  buffer[--length]= '\0';
 
8719
 
 
8720
  rc= mysql_real_query(mysql, buffer, length);
 
8721
  myquery(rc);
 
8722
 
 
8723
  rc= mysql_query(mysql, "select * from t_mem_overun");
 
8724
  myquery(rc);
 
8725
 
 
8726
  rc= my_process_result(mysql);
 
8727
  DIE_UNLESS(rc == 1);
 
8728
 
 
8729
  stmt= mysql_simple_prepare(mysql, "select * from t_mem_overun");
 
8730
  check_stmt(stmt);
 
8731
 
 
8732
  rc= mysql_stmt_execute(stmt);
 
8733
  check_execute(stmt, rc);
 
8734
 
 
8735
  field_res= mysql_stmt_result_metadata(stmt);
 
8736
  mytest(field_res);
 
8737
 
 
8738
  if (!opt_silent)
 
8739
    fprintf(stdout, "\n total fields : %d", mysql_num_fields(field_res));
 
8740
  DIE_UNLESS( 1000 == mysql_num_fields(field_res));
 
8741
 
 
8742
  rc= mysql_stmt_store_result(stmt);
 
8743
  check_execute(stmt, rc);
 
8744
 
 
8745
  rc= mysql_stmt_fetch(stmt);
 
8746
  check_execute(stmt, rc);
 
8747
 
 
8748
  rc= mysql_stmt_fetch(stmt);
 
8749
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
8750
 
 
8751
  mysql_free_result(field_res);
 
8752
 
 
8753
  mysql_stmt_close(stmt);
 
8754
}
 
8755
 
 
8756
 
 
8757
/* Test mysql_stmt_free_result() */
 
8758
 
 
8759
static void test_free_result()
 
8760
{
 
8761
  MYSQL_STMT *stmt;
 
8762
  MYSQL_BIND my_bind[1];
 
8763
  char       c2[5];
 
8764
  ulong      bl1, l2;
 
8765
  int        rc, c1, bc1;
 
8766
 
 
8767
  myheader("test_free_result");
 
8768
 
 
8769
  rc= mysql_query(mysql, "drop table if exists test_free_result");
 
8770
  myquery(rc);
 
8771
 
 
8772
  rc= mysql_query(mysql, "create table test_free_result("
 
8773
                         "c1 int primary key auto_increment)");
 
8774
  myquery(rc);
 
8775
 
 
8776
  rc= mysql_query(mysql, "insert into test_free_result values(), (), ()");
 
8777
  myquery(rc);
 
8778
 
 
8779
  stmt= mysql_simple_prepare(mysql, "select * from test_free_result");
 
8780
  check_stmt(stmt);
 
8781
 
 
8782
  bzero((char*) my_bind, sizeof(my_bind));
 
8783
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
8784
  my_bind[0].buffer= (void *)&bc1;
 
8785
  my_bind[0].length= &bl1;
 
8786
 
 
8787
  rc= mysql_stmt_execute(stmt);
 
8788
  check_execute(stmt, rc);
 
8789
 
 
8790
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
8791
  check_execute(stmt, rc);
 
8792
 
 
8793
  rc= mysql_stmt_fetch(stmt);
 
8794
  check_execute(stmt, rc);
 
8795
 
 
8796
  c2[0]= '\0'; l2= 0;
 
8797
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
8798
  my_bind[0].buffer= (void *)c2;
 
8799
  my_bind[0].buffer_length= 7;
 
8800
  my_bind[0].is_null= 0;
 
8801
  my_bind[0].length= &l2;
 
8802
 
 
8803
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
 
8804
  check_execute(stmt, rc);
 
8805
  if (!opt_silent)
 
8806
    fprintf(stdout, "\n col 0: %s(%ld)", c2, l2);
 
8807
  DIE_UNLESS(strncmp(c2, "1", 1) == 0 && l2 == 1);
 
8808
 
 
8809
  rc= mysql_stmt_fetch(stmt);
 
8810
  check_execute(stmt, rc);
 
8811
 
 
8812
  c1= 0, l2= 0;
 
8813
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
8814
  my_bind[0].buffer= (void *)&c1;
 
8815
  my_bind[0].buffer_length= 0;
 
8816
  my_bind[0].is_null= 0;
 
8817
  my_bind[0].length= &l2;
 
8818
 
 
8819
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
 
8820
  check_execute(stmt, rc);
 
8821
  if (!opt_silent)
 
8822
    fprintf(stdout, "\n col 0: %d(%ld)", c1, l2);
 
8823
  DIE_UNLESS(c1 == 2 && l2 == 4);
 
8824
 
 
8825
  rc= mysql_query(mysql, "drop table test_free_result");
 
8826
  myquery_r(rc); /* error should be, COMMANDS OUT OF SYNC */
 
8827
 
 
8828
  rc= mysql_stmt_free_result(stmt);
 
8829
  check_execute(stmt, rc);
 
8830
 
 
8831
  rc= mysql_query(mysql, "drop table test_free_result");
 
8832
  myquery(rc);  /* should be successful */
 
8833
 
 
8834
  mysql_stmt_close(stmt);
 
8835
}
 
8836
 
 
8837
 
 
8838
/* Test mysql_stmt_store_result() */
 
8839
 
 
8840
static void test_free_store_result()
 
8841
{
 
8842
  MYSQL_STMT *stmt;
 
8843
  MYSQL_BIND my_bind[1];
 
8844
  char       c2[5];
 
8845
  ulong      bl1, l2;
 
8846
  int        rc, c1, bc1;
 
8847
 
 
8848
  myheader("test_free_store_result");
 
8849
 
 
8850
  rc= mysql_query(mysql, "drop table if exists test_free_result");
 
8851
  myquery(rc);
 
8852
 
 
8853
  rc= mysql_query(mysql, "create table test_free_result(c1 int primary key auto_increment)");
 
8854
  myquery(rc);
 
8855
 
 
8856
  rc= mysql_query(mysql, "insert into test_free_result values(), (), ()");
 
8857
  myquery(rc);
 
8858
 
 
8859
  stmt= mysql_simple_prepare(mysql, "select * from test_free_result");
 
8860
  check_stmt(stmt);
 
8861
 
 
8862
  bzero((char*) my_bind, sizeof(my_bind));
 
8863
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
8864
  my_bind[0].buffer= (void *)&bc1;
 
8865
  my_bind[0].buffer_length= 0;
 
8866
  my_bind[0].is_null= 0;
 
8867
  my_bind[0].length= &bl1;
 
8868
 
 
8869
  rc= mysql_stmt_execute(stmt);
 
8870
  check_execute(stmt, rc);
 
8871
 
 
8872
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
8873
  check_execute(stmt, rc);
 
8874
 
 
8875
  rc= mysql_stmt_store_result(stmt);
 
8876
  check_execute(stmt, rc);
 
8877
 
 
8878
  rc= mysql_stmt_fetch(stmt);
 
8879
  check_execute(stmt, rc);
 
8880
 
 
8881
  c2[0]= '\0'; l2= 0;
 
8882
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
8883
  my_bind[0].buffer= (void *)c2;
 
8884
  my_bind[0].buffer_length= 7;
 
8885
  my_bind[0].is_null= 0;
 
8886
  my_bind[0].length= &l2;
 
8887
 
 
8888
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
 
8889
  check_execute(stmt, rc);
 
8890
  if (!opt_silent)
 
8891
    fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
 
8892
  DIE_UNLESS(strncmp(c2, "1", 1) == 0 && l2 == 1);
 
8893
 
 
8894
  rc= mysql_stmt_fetch(stmt);
 
8895
  check_execute(stmt, rc);
 
8896
 
 
8897
  c1= 0, l2= 0;
 
8898
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
8899
  my_bind[0].buffer= (void *)&c1;
 
8900
  my_bind[0].buffer_length= 0;
 
8901
  my_bind[0].is_null= 0;
 
8902
  my_bind[0].length= &l2;
 
8903
 
 
8904
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
 
8905
  check_execute(stmt, rc);
 
8906
  if (!opt_silent)
 
8907
    fprintf(stdout, "\n col 0: %d(%ld)", c1, l2);
 
8908
  DIE_UNLESS(c1 == 2 && l2 == 4);
 
8909
 
 
8910
  rc= mysql_stmt_free_result(stmt);
 
8911
  check_execute(stmt, rc);
 
8912
 
 
8913
  rc= mysql_query(mysql, "drop table test_free_result");
 
8914
  myquery(rc);
 
8915
 
 
8916
  mysql_stmt_close(stmt);
 
8917
}
 
8918
 
 
8919
 
 
8920
/* Test SQLmode */
 
8921
 
 
8922
static void test_sqlmode()
 
8923
{
 
8924
  MYSQL_STMT *stmt;
 
8925
  MYSQL_BIND my_bind[2];
 
8926
  char       c1[5], c2[5];
 
8927
  int        rc;
 
8928
  char query[MAX_TEST_QUERY_LENGTH];
 
8929
 
 
8930
  myheader("test_sqlmode");
 
8931
 
 
8932
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_piping");
 
8933
  myquery(rc);
 
8934
 
 
8935
  rc= mysql_query(mysql, "CREATE TABLE test_piping(name varchar(10))");
 
8936
  myquery(rc);
 
8937
 
 
8938
  /* PIPES_AS_CONCAT */
 
8939
  strmov(query, "SET SQL_MODE= \"PIPES_AS_CONCAT\"");
 
8940
  if (!opt_silent)
 
8941
    fprintf(stdout, "\n With %s", query);
 
8942
  rc= mysql_query(mysql, query);
 
8943
  myquery(rc);
 
8944
 
 
8945
  strmov(query, "INSERT INTO test_piping VALUES(?||?)");
 
8946
  if (!opt_silent)
 
8947
    fprintf(stdout, "\n  query: %s", query);
 
8948
  stmt= mysql_simple_prepare(mysql, query);
 
8949
  check_stmt(stmt);
 
8950
 
 
8951
  if (!opt_silent)
 
8952
    fprintf(stdout, "\n  total parameters: %ld", mysql_stmt_param_count(stmt));
 
8953
 
 
8954
  /*
 
8955
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
8956
    its members.
 
8957
  */
 
8958
  bzero((char*) my_bind, sizeof(my_bind));
 
8959
 
 
8960
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
8961
  my_bind[0].buffer= (void *)c1;
 
8962
  my_bind[0].buffer_length= 2;
 
8963
 
 
8964
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
8965
  my_bind[1].buffer= (void *)c2;
 
8966
  my_bind[1].buffer_length= 3;
 
8967
 
 
8968
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
8969
  check_execute(stmt, rc);
 
8970
 
 
8971
  strmov(c1, "My"); strmov(c2, "SQL");
 
8972
  rc= mysql_stmt_execute(stmt);
 
8973
  check_execute(stmt, rc);
 
8974
  mysql_stmt_close(stmt);
 
8975
 
 
8976
  verify_col_data("test_piping", "name", "MySQL");
 
8977
 
 
8978
  rc= mysql_query(mysql, "DELETE FROM test_piping");
 
8979
  myquery(rc);
 
8980
 
 
8981
  strmov(query, "SELECT connection_id    ()");
 
8982
  if (!opt_silent)
 
8983
    fprintf(stdout, "\n  query: %s", query);
 
8984
  stmt= mysql_simple_prepare(mysql, query);
 
8985
  check_stmt(stmt);
 
8986
  mysql_stmt_close(stmt);
 
8987
 
 
8988
  /* ANSI */
 
8989
  strmov(query, "SET SQL_MODE= \"ANSI\"");
 
8990
  if (!opt_silent)
 
8991
    fprintf(stdout, "\n With %s", query);
 
8992
  rc= mysql_query(mysql, query);
 
8993
  myquery(rc);
 
8994
 
 
8995
  strmov(query, "INSERT INTO test_piping VALUES(?||?)");
 
8996
  if (!opt_silent)
 
8997
    fprintf(stdout, "\n  query: %s", query);
 
8998
  stmt= mysql_simple_prepare(mysql, query);
 
8999
  check_stmt(stmt);
 
9000
  if (!opt_silent)
 
9001
    fprintf(stdout, "\n  total parameters: %ld", mysql_stmt_param_count(stmt));
 
9002
 
 
9003
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
9004
  check_execute(stmt, rc);
 
9005
 
 
9006
  strmov(c1, "My"); strmov(c2, "SQL");
 
9007
  rc= mysql_stmt_execute(stmt);
 
9008
  check_execute(stmt, rc);
 
9009
 
 
9010
  mysql_stmt_close(stmt);
 
9011
  verify_col_data("test_piping", "name", "MySQL");
 
9012
 
 
9013
  /* ANSI mode spaces ... */
 
9014
  strmov(query, "SELECT connection_id    ()");
 
9015
  if (!opt_silent)
 
9016
    fprintf(stdout, "\n  query: %s", query);
 
9017
  stmt= mysql_simple_prepare(mysql, query);
 
9018
  check_stmt(stmt);
 
9019
 
 
9020
  rc= mysql_stmt_execute(stmt);
 
9021
  check_execute(stmt, rc);
 
9022
 
 
9023
  rc= mysql_stmt_fetch(stmt);
 
9024
  check_execute(stmt, rc);
 
9025
 
 
9026
  rc= mysql_stmt_fetch(stmt);
 
9027
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
9028
  if (!opt_silent)
 
9029
    fprintf(stdout, "\n  returned 1 row\n");
 
9030
 
 
9031
  mysql_stmt_close(stmt);
 
9032
 
 
9033
  /* IGNORE SPACE MODE */
 
9034
  strmov(query, "SET SQL_MODE= \"IGNORE_SPACE\"");
 
9035
  if (!opt_silent)
 
9036
    fprintf(stdout, "\n With %s", query);
 
9037
  rc= mysql_query(mysql, query);
 
9038
  myquery(rc);
 
9039
 
 
9040
  strmov(query, "SELECT connection_id    ()");
 
9041
  if (!opt_silent)
 
9042
    fprintf(stdout, "\n  query: %s", query);
 
9043
  stmt= mysql_simple_prepare(mysql, query);
 
9044
  check_stmt(stmt);
 
9045
 
 
9046
  rc= mysql_stmt_execute(stmt);
 
9047
  check_execute(stmt, rc);
 
9048
 
 
9049
  rc= mysql_stmt_fetch(stmt);
 
9050
  check_execute(stmt, rc);
 
9051
 
 
9052
  rc= mysql_stmt_fetch(stmt);
 
9053
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
9054
  if (!opt_silent)
 
9055
    fprintf(stdout, "\n  returned 1 row");
 
9056
 
 
9057
  mysql_stmt_close(stmt);
 
9058
}
 
9059
 
 
9060
 
 
9061
/* Test for timestamp handling */
 
9062
 
 
9063
static void test_ts()
 
9064
{
 
9065
  MYSQL_STMT *stmt;
 
9066
  MYSQL_BIND my_bind[6];
 
9067
  MYSQL_TIME ts;
 
9068
  MYSQL_RES  *prep_res;
 
9069
  char       strts[30];
 
9070
  ulong      length;
 
9071
  int        rc, field_count;
 
9072
  char       name;
 
9073
  char query[MAX_TEST_QUERY_LENGTH];
 
9074
  const char *queries [3]= {"SELECT a, b, c FROM test_ts WHERE %c=?",
 
9075
                            "SELECT a, b, c FROM test_ts WHERE %c=?",
 
9076
                            "SELECT a, b, c FROM test_ts WHERE %c=CAST(? AS DATE)"};
 
9077
  myheader("test_ts");
 
9078
 
 
9079
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ts");
 
9080
  myquery(rc);
 
9081
 
 
9082
  rc= mysql_query(mysql, "CREATE TABLE test_ts(a DATE, b TIME, c TIMESTAMP)");
 
9083
  myquery(rc);
 
9084
 
 
9085
  stmt= mysql_simple_prepare(mysql, "INSERT INTO test_ts VALUES(?, ?, ?), (?, ?, ?)");
 
9086
  check_stmt(stmt);
 
9087
 
 
9088
  ts.year= 2003;
 
9089
  ts.month= 07;
 
9090
  ts.day= 12;
 
9091
  ts.hour= 21;
 
9092
  ts.minute= 07;
 
9093
  ts.second= 46;
 
9094
  ts.second_part= 0;
 
9095
  length= (long)(strmov(strts, "2003-07-12 21:07:46") - strts);
 
9096
 
 
9097
  /*
 
9098
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
9099
    its members.
 
9100
  */
 
9101
  bzero((char*) my_bind, sizeof(my_bind));
 
9102
 
 
9103
  my_bind[0].buffer_type= MYSQL_TYPE_TIMESTAMP;
 
9104
  my_bind[0].buffer= (void *)&ts;
 
9105
  my_bind[0].buffer_length= sizeof(ts);
 
9106
 
 
9107
  my_bind[2]= my_bind[1]= my_bind[0];
 
9108
 
 
9109
  my_bind[3].buffer_type= MYSQL_TYPE_STRING;
 
9110
  my_bind[3].buffer= (void *)strts;
 
9111
  my_bind[3].buffer_length= sizeof(strts);
 
9112
  my_bind[3].length= &length;
 
9113
 
 
9114
  my_bind[5]= my_bind[4]= my_bind[3];
 
9115
 
 
9116
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
9117
  check_execute(stmt, rc);
 
9118
 
 
9119
  rc= mysql_stmt_execute(stmt);
 
9120
  check_execute(stmt, rc);
 
9121
 
 
9122
  mysql_stmt_close(stmt);
 
9123
 
 
9124
  verify_col_data("test_ts", "a", "2003-07-12");
 
9125
  verify_col_data("test_ts", "b", "21:07:46");
 
9126
  verify_col_data("test_ts", "c", "2003-07-12 21:07:46");
 
9127
 
 
9128
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_ts");
 
9129
  check_stmt(stmt);
 
9130
 
 
9131
  prep_res= mysql_stmt_result_metadata(stmt);
 
9132
  mytest(prep_res);
 
9133
 
 
9134
  rc= mysql_stmt_execute(stmt);
 
9135
  check_execute(stmt, rc);
 
9136
 
 
9137
  rc= my_process_stmt_result(stmt);
 
9138
  DIE_UNLESS(rc == 2);
 
9139
  field_count= mysql_num_fields(prep_res);
 
9140
 
 
9141
  mysql_free_result(prep_res);
 
9142
  mysql_stmt_close(stmt);
 
9143
 
 
9144
  for (name= 'a'; field_count--; name++)
 
9145
  {
 
9146
    int row_count= 0;
 
9147
 
 
9148
    sprintf(query, queries[field_count], name);
 
9149
 
 
9150
    if (!opt_silent)
 
9151
      fprintf(stdout, "\n  %s", query);
 
9152
    stmt= mysql_simple_prepare(mysql, query);
 
9153
    check_stmt(stmt);
 
9154
 
 
9155
    rc= mysql_stmt_bind_param(stmt, my_bind);
 
9156
    check_execute(stmt, rc);
 
9157
 
 
9158
    rc= mysql_stmt_execute(stmt);
 
9159
    check_execute(stmt, rc);
 
9160
 
 
9161
    while (mysql_stmt_fetch(stmt) == 0)
 
9162
      row_count++;
 
9163
 
 
9164
    if (!opt_silent)
 
9165
      fprintf(stdout, "\n   returned '%d' rows", row_count);
 
9166
    DIE_UNLESS(row_count == 2);
 
9167
    mysql_stmt_close(stmt);
 
9168
  }
 
9169
}
 
9170
 
 
9171
 
 
9172
/* Test for bug #1500. */
 
9173
 
 
9174
static void test_bug1500()
 
9175
{
 
9176
  MYSQL_STMT *stmt;
 
9177
  MYSQL_BIND my_bind[3];
 
9178
  int        rc;
 
9179
  int32 int_data[3]= {2, 3, 4};
 
9180
  const char *data;
 
9181
 
 
9182
  myheader("test_bug1500");
 
9183
 
 
9184
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bg1500");
 
9185
  myquery(rc);
 
9186
 
 
9187
  rc= mysql_query(mysql, "CREATE TABLE test_bg1500 (i INT)");
 
9188
  myquery(rc);
 
9189
 
 
9190
  rc= mysql_query(mysql, "INSERT INTO test_bg1500 VALUES (1), (2)");
 
9191
  myquery(rc);
 
9192
 
 
9193
  rc= mysql_commit(mysql);
 
9194
  myquery(rc);
 
9195
 
 
9196
  stmt= mysql_simple_prepare(mysql, "SELECT i FROM test_bg1500 WHERE i IN (?, ?, ?)");
 
9197
  check_stmt(stmt);
 
9198
  verify_param_count(stmt, 3);
 
9199
 
 
9200
  /*
 
9201
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
9202
    its members.
 
9203
  */
 
9204
  bzero((char*) my_bind, sizeof(my_bind));
 
9205
 
 
9206
  my_bind[0].buffer= (void *)int_data;
 
9207
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
9208
  my_bind[2]= my_bind[1]= my_bind[0];
 
9209
  my_bind[1].buffer= (void *)(int_data + 1);
 
9210
  my_bind[2].buffer= (void *)(int_data + 2);
 
9211
 
 
9212
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
9213
  check_execute(stmt, rc);
 
9214
 
 
9215
  rc= mysql_stmt_execute(stmt);
 
9216
  check_execute(stmt, rc);
 
9217
 
 
9218
  rc= my_process_stmt_result(stmt);
 
9219
  DIE_UNLESS(rc == 1);
 
9220
 
 
9221
  mysql_stmt_close(stmt);
 
9222
 
 
9223
  rc= mysql_query(mysql, "DROP TABLE test_bg1500");
 
9224
  myquery(rc);
 
9225
 
 
9226
  rc= mysql_query(mysql, "CREATE TABLE test_bg1500 (s VARCHAR(25), FULLTEXT(s)) engine=MyISAM");
 
9227
  myquery(rc);
 
9228
 
 
9229
  rc= mysql_query(mysql,
 
9230
        "INSERT INTO test_bg1500 VALUES ('Gravedigger'), ('Greed'), ('Hollow Dogs')");
 
9231
  myquery(rc);
 
9232
 
 
9233
  rc= mysql_commit(mysql);
 
9234
  myquery(rc);
 
9235
 
 
9236
  stmt= mysql_simple_prepare(mysql,
 
9237
          "SELECT s FROM test_bg1500 WHERE MATCH (s) AGAINST (?)");
 
9238
  check_stmt(stmt);
 
9239
 
 
9240
  verify_param_count(stmt, 1);
 
9241
 
 
9242
  data= "Dogs";
 
9243
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
9244
  my_bind[0].buffer= (void *) data;
 
9245
  my_bind[0].buffer_length= strlen(data);
 
9246
  my_bind[0].is_null= 0;
 
9247
  my_bind[0].length= 0;
 
9248
 
 
9249
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
9250
  check_execute(stmt, rc);
 
9251
 
 
9252
  rc= mysql_stmt_execute(stmt);
 
9253
  check_execute(stmt, rc);
 
9254
 
 
9255
  rc= my_process_stmt_result(stmt);
 
9256
  DIE_UNLESS(rc == 1);
 
9257
 
 
9258
  mysql_stmt_close(stmt);
 
9259
 
 
9260
  /* This should work too */
 
9261
  stmt= mysql_simple_prepare(mysql,
 
9262
          "SELECT s FROM test_bg1500 WHERE MATCH (s) AGAINST (CONCAT(?, 'digger'))");
 
9263
  check_stmt(stmt);
 
9264
 
 
9265
  verify_param_count(stmt, 1);
 
9266
 
 
9267
  data= "Grave";
 
9268
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
9269
  my_bind[0].buffer= (void *) data;
 
9270
  my_bind[0].buffer_length= strlen(data);
 
9271
 
 
9272
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
9273
  check_execute(stmt, rc);
 
9274
 
 
9275
  rc= mysql_stmt_execute(stmt);
 
9276
  check_execute(stmt, rc);
 
9277
 
 
9278
  rc= my_process_stmt_result(stmt);
 
9279
  DIE_UNLESS(rc == 1);
 
9280
 
 
9281
  mysql_stmt_close(stmt);
 
9282
}
 
9283
 
 
9284
 
 
9285
static void test_bug1946()
 
9286
{
 
9287
  MYSQL_STMT *stmt;
 
9288
  int rc;
 
9289
  const char *query= "INSERT INTO prepare_command VALUES (?)";
 
9290
 
 
9291
  myheader("test_bug1946");
 
9292
 
 
9293
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS prepare_command");
 
9294
  myquery(rc);
 
9295
 
 
9296
  rc= mysql_query(mysql, "CREATE TABLE prepare_command(ID INT)");
 
9297
  myquery(rc);
 
9298
 
 
9299
  stmt= mysql_simple_prepare(mysql, query);
 
9300
  check_stmt(stmt);
 
9301
  rc= mysql_real_query(mysql, query, strlen(query));
 
9302
  DIE_UNLESS(rc != 0);
 
9303
  if (!opt_silent)
 
9304
    fprintf(stdout, "Got error (as expected):\n");
 
9305
  myerror(NULL);
 
9306
 
 
9307
  mysql_stmt_close(stmt);
 
9308
  rc= mysql_query(mysql, "DROP TABLE prepare_command");
 
9309
}
 
9310
 
 
9311
 
 
9312
static void test_parse_error_and_bad_length()
 
9313
{
 
9314
  MYSQL_STMT *stmt;
 
9315
  int rc;
 
9316
 
 
9317
  /* check that we get 4 syntax errors over the 4 calls */
 
9318
  myheader("test_parse_error_and_bad_length");
 
9319
 
 
9320
  rc= mysql_query(mysql, "SHOW DATABAAAA");
 
9321
  DIE_UNLESS(rc);
 
9322
  if (!opt_silent)
 
9323
    fprintf(stdout, "Got error (as expected): '%s'\n", mysql_error(mysql));
 
9324
  rc= mysql_real_query(mysql, "SHOW DATABASES", 100);
 
9325
  DIE_UNLESS(rc);
 
9326
  if (!opt_silent)
 
9327
    fprintf(stdout, "Got error (as expected): '%s'\n", mysql_error(mysql));
 
9328
 
 
9329
  stmt= mysql_simple_prepare(mysql, "SHOW DATABAAAA");
 
9330
  DIE_UNLESS(!stmt);
 
9331
  if (!opt_silent)
 
9332
    fprintf(stdout, "Got error (as expected): '%s'\n", mysql_error(mysql));
 
9333
  stmt= mysql_stmt_init(mysql);
 
9334
  DIE_UNLESS(stmt);
 
9335
  rc= mysql_stmt_prepare(stmt, "SHOW DATABASES", 100);
 
9336
  DIE_UNLESS(rc != 0);
 
9337
  if (!opt_silent)
 
9338
    fprintf(stdout, "Got error (as expected): '%s'\n", mysql_stmt_error(stmt));
 
9339
  mysql_stmt_close(stmt);
 
9340
}
 
9341
 
 
9342
 
 
9343
static void test_bug2247()
 
9344
{
 
9345
  MYSQL_STMT *stmt;
 
9346
  MYSQL_RES *res;
 
9347
  int rc;
 
9348
  int i;
 
9349
  const char *create= "CREATE TABLE bug2247(id INT UNIQUE AUTO_INCREMENT)";
 
9350
  const char *insert= "INSERT INTO bug2247 VALUES (NULL)";
 
9351
  const char *SELECT= "SELECT id FROM bug2247";
 
9352
  const char *update= "UPDATE bug2247 SET id=id+10";
 
9353
  const char *drop= "DROP TABLE IF EXISTS bug2247";
 
9354
  ulonglong exp_count;
 
9355
  enum { NUM_ROWS= 5 };
 
9356
 
 
9357
  myheader("test_bug2247");
 
9358
 
 
9359
  if (!opt_silent)
 
9360
    fprintf(stdout, "\nChecking if stmt_affected_rows is not affected by\n"
 
9361
                  "mysql_query ... ");
 
9362
  /* create table and insert few rows */
 
9363
  rc= mysql_query(mysql, drop);
 
9364
  myquery(rc);
 
9365
 
 
9366
  rc= mysql_query(mysql, create);
 
9367
  myquery(rc);
 
9368
 
 
9369
  stmt= mysql_simple_prepare(mysql, insert);
 
9370
  check_stmt(stmt);
 
9371
  for (i= 0; i < NUM_ROWS; ++i)
 
9372
  {
 
9373
    rc= mysql_stmt_execute(stmt);
 
9374
    check_execute(stmt, rc);
 
9375
  }
 
9376
  exp_count= mysql_stmt_affected_rows(stmt);
 
9377
  DIE_UNLESS(exp_count == 1);
 
9378
 
 
9379
  rc= mysql_query(mysql, SELECT);
 
9380
  myquery(rc);
 
9381
  /*
 
9382
    mysql_store_result overwrites mysql->affected_rows. Check that
 
9383
    mysql_stmt_affected_rows() returns the same value, whereas
 
9384
    mysql_affected_rows() value is correct.
 
9385
  */
 
9386
  res= mysql_store_result(mysql);
 
9387
  mytest(res);
 
9388
 
 
9389
  DIE_UNLESS(mysql_affected_rows(mysql) == NUM_ROWS);
 
9390
  DIE_UNLESS(exp_count == mysql_stmt_affected_rows(stmt));
 
9391
 
 
9392
  rc= mysql_query(mysql, update);
 
9393
  myquery(rc);
 
9394
  DIE_UNLESS(mysql_affected_rows(mysql) == NUM_ROWS);
 
9395
  DIE_UNLESS(exp_count == mysql_stmt_affected_rows(stmt));
 
9396
 
 
9397
  mysql_free_result(res);
 
9398
  mysql_stmt_close(stmt);
 
9399
 
 
9400
  /* check that mysql_stmt_store_result modifies mysql_stmt_affected_rows */
 
9401
  stmt= mysql_simple_prepare(mysql, SELECT);
 
9402
  check_stmt(stmt);
 
9403
 
 
9404
  rc= mysql_stmt_execute(stmt);
 
9405
  check_execute(stmt, rc);
 
9406
  rc= mysql_stmt_store_result(stmt);
 
9407
  check_execute(stmt, rc);
 
9408
  exp_count= mysql_stmt_affected_rows(stmt);
 
9409
  DIE_UNLESS(exp_count == NUM_ROWS);
 
9410
 
 
9411
  rc= mysql_query(mysql, insert);
 
9412
  myquery(rc);
 
9413
  DIE_UNLESS(mysql_affected_rows(mysql) == 1);
 
9414
  DIE_UNLESS(mysql_stmt_affected_rows(stmt) == exp_count);
 
9415
 
 
9416
  mysql_stmt_close(stmt);
 
9417
  if (!opt_silent)
 
9418
    fprintf(stdout, "OK");
 
9419
}
 
9420
 
 
9421
 
 
9422
static void test_subqueries()
 
9423
{
 
9424
  MYSQL_STMT *stmt;
 
9425
  int rc, i;
 
9426
  const char *query= "SELECT (SELECT SUM(a+b) FROM t2 where t1.b=t2.b GROUP BY t1.a LIMIT 1) as scalar_s, exists (select 1 from t2 where t2.a/2=t1.a) as exists_s, a in (select a+3 from t2) as in_s, (a-1, b-1) in (select a, b from t2) as in_row_s FROM t1, (select a x, b y from t2) tt WHERE x=a";
 
9427
 
 
9428
  myheader("test_subqueries");
 
9429
 
 
9430
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
 
9431
  myquery(rc);
 
9432
 
 
9433
  rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
 
9434
  myquery(rc);
 
9435
 
 
9436
  rc= mysql_query(mysql,
 
9437
                  "insert into t1 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);");
 
9438
  myquery(rc);
 
9439
 
 
9440
  rc= mysql_query(mysql, "create table t2 select * from t1;");
 
9441
  myquery(rc);
 
9442
 
 
9443
  stmt= mysql_simple_prepare(mysql, query);
 
9444
  check_stmt(stmt);
 
9445
  for (i= 0; i < 3; i++)
 
9446
  {
 
9447
    rc= mysql_stmt_execute(stmt);
 
9448
    check_execute(stmt, rc);
 
9449
    rc= my_process_stmt_result(stmt);
 
9450
    DIE_UNLESS(rc == 5);
 
9451
  }
 
9452
  mysql_stmt_close(stmt);
 
9453
 
 
9454
  rc= mysql_query(mysql, "DROP TABLE t1, t2");
 
9455
  myquery(rc);
 
9456
}
 
9457
 
 
9458
 
 
9459
static void test_bad_union()
 
9460
{
 
9461
  MYSQL_STMT *stmt;
 
9462
  const char *query= "SELECT 1, 2 union SELECT 1";
 
9463
 
 
9464
  myheader("test_bad_union");
 
9465
 
 
9466
  stmt= mysql_simple_prepare(mysql, query);
 
9467
  DIE_UNLESS(stmt == 0);
 
9468
  myerror(NULL);
 
9469
}
 
9470
 
 
9471
 
 
9472
static void test_distinct()
 
9473
{
 
9474
  MYSQL_STMT *stmt;
 
9475
  int rc, i;
 
9476
  const char *query=
 
9477
    "SELECT 2+count(distinct b), group_concat(a) FROM t1 group by a";
 
9478
 
 
9479
  myheader("test_distinct");
 
9480
 
 
9481
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
9482
  myquery(rc);
 
9483
 
 
9484
  rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
 
9485
  myquery(rc);
 
9486
 
 
9487
  rc= mysql_query(mysql,
 
9488
                  "insert into t1 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), \
 
9489
(1, 10), (2, 20), (3, 30), (4, 40), (5, 50);");
 
9490
  myquery(rc);
 
9491
 
 
9492
  for (i= 0; i < 3; i++)
 
9493
  {
 
9494
    stmt= mysql_simple_prepare(mysql, query);
 
9495
    check_stmt(stmt);
 
9496
    rc= mysql_stmt_execute(stmt);
 
9497
    check_execute(stmt, rc);
 
9498
    rc= my_process_stmt_result(stmt);
 
9499
    DIE_UNLESS(rc == 5);
 
9500
    mysql_stmt_close(stmt);
 
9501
  }
 
9502
 
 
9503
  rc= mysql_query(mysql, "DROP TABLE t1");
 
9504
  myquery(rc);
 
9505
}
 
9506
 
 
9507
 
 
9508
/*
 
9509
  Test for bug#2248 "mysql_fetch without prior mysql_stmt_execute hangs"
 
9510
*/
 
9511
 
 
9512
static void test_bug2248()
 
9513
{
 
9514
  MYSQL_STMT *stmt;
 
9515
  int rc;
 
9516
  const char *query1= "SELECT DATABASE()";
 
9517
  const char *query2= "INSERT INTO test_bug2248 VALUES (10)";
 
9518
 
 
9519
  myheader("test_bug2248");
 
9520
 
 
9521
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bug2248");
 
9522
  myquery(rc);
 
9523
 
 
9524
  rc= mysql_query(mysql, "CREATE TABLE test_bug2248 (id int)");
 
9525
  myquery(rc);
 
9526
 
 
9527
  stmt= mysql_simple_prepare(mysql, query1);
 
9528
  check_stmt(stmt);
 
9529
 
 
9530
  /* This should not hang */
 
9531
  rc= mysql_stmt_fetch(stmt);
 
9532
  check_execute_r(stmt, rc);
 
9533
 
 
9534
  /* And this too */
 
9535
  rc= mysql_stmt_store_result(stmt);
 
9536
  check_execute_r(stmt, rc);
 
9537
 
 
9538
  mysql_stmt_close(stmt);
 
9539
 
 
9540
  stmt= mysql_simple_prepare(mysql, query2);
 
9541
  check_stmt(stmt);
 
9542
 
 
9543
  rc= mysql_stmt_execute(stmt);
 
9544
  check_execute(stmt, rc);
 
9545
 
 
9546
  /* This too should not hang but should return proper error */
 
9547
  rc= mysql_stmt_fetch(stmt);
 
9548
  DIE_UNLESS(rc == 1);
 
9549
 
 
9550
  /* This too should not hang but should not bark */
 
9551
  rc= mysql_stmt_store_result(stmt);
 
9552
  check_execute(stmt, rc);
 
9553
 
 
9554
  /* This should return proper error */
 
9555
  rc= mysql_stmt_fetch(stmt);
 
9556
  check_execute_r(stmt, rc);
 
9557
  DIE_UNLESS(rc == 1);
 
9558
 
 
9559
  mysql_stmt_close(stmt);
 
9560
 
 
9561
  rc= mysql_query(mysql, "DROP TABLE test_bug2248");
 
9562
  myquery(rc);
 
9563
}
 
9564
 
 
9565
 
 
9566
static void test_subqueries_ref()
 
9567
{
 
9568
  MYSQL_STMT *stmt;
 
9569
  int rc, i;
 
9570
  const char *query= "SELECT a as ccc from t1 outr where a+1=(SELECT 1+outr.a from t1 where outr.a+1=a+1 and a=1)";
 
9571
 
 
9572
  myheader("test_subqueries_ref");
 
9573
 
 
9574
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
9575
  myquery(rc);
 
9576
 
 
9577
  rc= mysql_query(mysql, "CREATE TABLE t1 (a int);");
 
9578
  myquery(rc);
 
9579
 
 
9580
  rc= mysql_query(mysql,
 
9581
                  "insert into t1 values (1), (2), (3), (4), (5);");
 
9582
  myquery(rc);
 
9583
 
 
9584
  stmt= mysql_simple_prepare(mysql, query);
 
9585
  check_stmt(stmt);
 
9586
  for (i= 0; i < 3; i++)
 
9587
  {
 
9588
    rc= mysql_stmt_execute(stmt);
 
9589
    check_execute(stmt, rc);
 
9590
    rc= my_process_stmt_result(stmt);
 
9591
    DIE_UNLESS(rc == 1);
 
9592
  }
 
9593
  mysql_stmt_close(stmt);
 
9594
 
 
9595
  rc= mysql_query(mysql, "DROP TABLE t1");
 
9596
  myquery(rc);
 
9597
}
 
9598
 
 
9599
 
 
9600
static void test_union()
 
9601
{
 
9602
  MYSQL_STMT *stmt;
 
9603
  int rc;
 
9604
 
 
9605
  myheader("test_union");
 
9606
 
 
9607
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
 
9608
  myquery(rc);
 
9609
 
 
9610
  rc= mysql_query(mysql,
 
9611
                  "CREATE TABLE t1 "
 
9612
                  "(id INTEGER NOT NULL PRIMARY KEY, "
 
9613
                  " name VARCHAR(20) NOT NULL)");
 
9614
  myquery(rc);
 
9615
  rc= mysql_query(mysql,
 
9616
                  "INSERT INTO t1 (id, name) VALUES "
 
9617
                  "(2, 'Ja'), (3, 'Ede'), "
 
9618
                  "(4, 'Haag'), (5, 'Kabul'), "
 
9619
                  "(6, 'Almere'), (7, 'Utrecht'), "
 
9620
                  "(8, 'Qandahar'), (9, 'Amsterdam'), "
 
9621
                  "(10, 'Amersfoort'), (11, 'Constantine')");
 
9622
  myquery(rc);
 
9623
  rc= mysql_query(mysql,
 
9624
                  "CREATE TABLE t2 "
 
9625
                  "(id INTEGER NOT NULL PRIMARY KEY, "
 
9626
                  " name VARCHAR(20) NOT NULL)");
 
9627
  myquery(rc);
 
9628
  rc= mysql_query(mysql,
 
9629
                  "INSERT INTO t2 (id, name) VALUES "
 
9630
                  "(4, 'Guam'), (5, 'Aruba'), "
 
9631
                  "(6, 'Angola'), (7, 'Albania'), "
 
9632
                  "(8, 'Anguilla'), (9, 'Argentina'), "
 
9633
                  "(10, 'Azerbaijan'), (11, 'Afghanistan'), "
 
9634
                  "(12, 'Burkina Faso'), (13, 'Faroe Islands')");
 
9635
  myquery(rc);
 
9636
 
 
9637
  stmt= mysql_simple_prepare(mysql,
 
9638
                             "SELECT t1.name FROM t1 UNION "
 
9639
                             "SELECT t2.name FROM t2");
 
9640
  check_stmt(stmt);
 
9641
 
 
9642
  rc= mysql_stmt_execute(stmt);
 
9643
  check_execute(stmt, rc);
 
9644
  rc= my_process_stmt_result(stmt);
 
9645
  DIE_UNLESS(rc == 20);
 
9646
  mysql_stmt_close(stmt);
 
9647
 
 
9648
  rc= mysql_query(mysql, "DROP TABLE t1, t2");
 
9649
  myquery(rc);
 
9650
}
 
9651
 
 
9652
 
 
9653
static void test_bug3117()
 
9654
{
 
9655
  MYSQL_STMT *stmt;
 
9656
  MYSQL_BIND buffer;
 
9657
  longlong lii;
 
9658
  ulong length;
 
9659
  my_bool is_null;
 
9660
  int rc;
 
9661
 
 
9662
  myheader("test_bug3117");
 
9663
 
 
9664
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
9665
  myquery(rc);
 
9666
 
 
9667
  rc= mysql_query(mysql, "CREATE TABLE t1 (id int auto_increment primary key)");
 
9668
  myquery(rc);
 
9669
 
 
9670
  stmt= mysql_simple_prepare(mysql, "SELECT LAST_INSERT_ID()");
 
9671
  check_stmt(stmt);
 
9672
 
 
9673
  rc= mysql_query(mysql, "INSERT INTO t1 VALUES (NULL)");
 
9674
  myquery(rc);
 
9675
 
 
9676
  rc= mysql_stmt_execute(stmt);
 
9677
  check_execute(stmt, rc);
 
9678
 
 
9679
  bzero((char*) &buffer, sizeof(buffer));
 
9680
  buffer.buffer_type= MYSQL_TYPE_LONGLONG;
 
9681
  buffer.buffer_length= sizeof(lii);
 
9682
  buffer.buffer= (void *)&lii;
 
9683
  buffer.length= &length;
 
9684
  buffer.is_null= &is_null;
 
9685
 
 
9686
  rc= mysql_stmt_bind_result(stmt, &buffer);
 
9687
  check_execute(stmt, rc);
 
9688
 
 
9689
  rc= mysql_stmt_store_result(stmt);
 
9690
  check_execute(stmt, rc);
 
9691
 
 
9692
  rc= mysql_stmt_fetch(stmt);
 
9693
  check_execute(stmt, rc);
 
9694
 
 
9695
  DIE_UNLESS(is_null == 0 && lii == 1);
 
9696
  if (!opt_silent)
 
9697
    fprintf(stdout, "\n\tLAST_INSERT_ID()= 1 ok\n");
 
9698
 
 
9699
  rc= mysql_query(mysql, "INSERT INTO t1 VALUES (NULL)");
 
9700
  myquery(rc);
 
9701
 
 
9702
  rc= mysql_stmt_execute(stmt);
 
9703
  check_execute(stmt, rc);
 
9704
 
 
9705
  rc= mysql_stmt_fetch(stmt);
 
9706
  check_execute(stmt, rc);
 
9707
 
 
9708
  DIE_UNLESS(is_null == 0 && lii == 2);
 
9709
  if (!opt_silent)
 
9710
    fprintf(stdout, "\tLAST_INSERT_ID()= 2 ok\n");
 
9711
 
 
9712
  mysql_stmt_close(stmt);
 
9713
 
 
9714
  rc= mysql_query(mysql, "DROP TABLE t1");
 
9715
  myquery(rc);
 
9716
}
 
9717
 
 
9718
 
 
9719
static void test_join()
 
9720
{
 
9721
  MYSQL_STMT *stmt;
 
9722
  int rc, i, j;
 
9723
  const char *query[]= {"SELECT * FROM t2 join t1 on (t1.a=t2.a)",
 
9724
                        "SELECT * FROM t2 natural join t1",
 
9725
                        "SELECT * FROM t2 join t1 using(a)",
 
9726
                        "SELECT * FROM t2 left join t1 on(t1.a=t2.a)",
 
9727
                        "SELECT * FROM t2 natural left join t1",
 
9728
                        "SELECT * FROM t2 left join t1 using(a)",
 
9729
                        "SELECT * FROM t2 right join t1 on(t1.a=t2.a)",
 
9730
                        "SELECT * FROM t2 natural right join t1",
 
9731
                        "SELECT * FROM t2 right join t1 using(a)"};
 
9732
 
 
9733
  myheader("test_join");
 
9734
 
 
9735
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
 
9736
  myquery(rc);
 
9737
 
 
9738
  rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
 
9739
  myquery(rc);
 
9740
 
 
9741
  rc= mysql_query(mysql,
 
9742
                  "insert into t1 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);");
 
9743
  myquery(rc);
 
9744
 
 
9745
  rc= mysql_query(mysql, "CREATE TABLE t2 (a int , c int);");
 
9746
  myquery(rc);
 
9747
 
 
9748
  rc= mysql_query(mysql,
 
9749
                  "insert into t2 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);");
 
9750
  myquery(rc);
 
9751
 
 
9752
  for (j= 0; j < 9; j++)
 
9753
  {
 
9754
    stmt= mysql_simple_prepare(mysql, query[j]);
 
9755
    check_stmt(stmt);
 
9756
    for (i= 0; i < 3; i++)
 
9757
    {
 
9758
      rc= mysql_stmt_execute(stmt);
 
9759
      check_execute(stmt, rc);
 
9760
      rc= my_process_stmt_result(stmt);
 
9761
      DIE_UNLESS(rc == 5);
 
9762
    }
 
9763
    mysql_stmt_close(stmt);
 
9764
  }
 
9765
 
 
9766
  rc= mysql_query(mysql, "DROP TABLE t1, t2");
 
9767
  myquery(rc);
 
9768
}
 
9769
 
 
9770
 
 
9771
static void test_selecttmp()
 
9772
{
 
9773
  MYSQL_STMT *stmt;
 
9774
  int rc, i;
 
9775
  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";
 
9776
 
 
9777
  myheader("test_select_tmp");
 
9778
 
 
9779
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2, t3");
 
9780
  myquery(rc);
 
9781
 
 
9782
  rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
 
9783
  myquery(rc);
 
9784
 
 
9785
  rc= mysql_query(mysql, "create table t2 (a int, b int);");
 
9786
  myquery(rc);
 
9787
 
 
9788
  rc= mysql_query(mysql, "create table t3 (a int, b int);");
 
9789
  myquery(rc);
 
9790
 
 
9791
  rc= mysql_query(mysql,
 
9792
                  "insert into t1 values (0, 100), (1, 2), (1, 3), (2, 2), (2, 7), \
 
9793
(2, -1), (3, 10);");
 
9794
  myquery(rc);
 
9795
  rc= mysql_query(mysql,
 
9796
                  "insert into t2 values (0, 0), (1, 1), (2, 1), (3, 1), (4, 1);");
 
9797
  myquery(rc);
 
9798
  rc= mysql_query(mysql,
 
9799
                  "insert into t3 values (3, 3), (2, 2), (1, 1);");
 
9800
  myquery(rc);
 
9801
 
 
9802
  stmt= mysql_simple_prepare(mysql, query);
 
9803
  check_stmt(stmt);
 
9804
  for (i= 0; i < 3; i++)
 
9805
  {
 
9806
    rc= mysql_stmt_execute(stmt);
 
9807
    check_execute(stmt, rc);
 
9808
    rc= my_process_stmt_result(stmt);
 
9809
    DIE_UNLESS(rc == 3);
 
9810
  }
 
9811
  mysql_stmt_close(stmt);
 
9812
 
 
9813
  rc= mysql_query(mysql, "DROP TABLE t1, t2, t3");
 
9814
  myquery(rc);
 
9815
}
 
9816
 
 
9817
 
 
9818
static void test_create_drop()
 
9819
{
 
9820
  MYSQL_STMT *stmt_create, *stmt_drop, *stmt_select, *stmt_create_select;
 
9821
  char *query;
 
9822
  int rc, i;
 
9823
  myheader("test_table_manipulation");
 
9824
 
 
9825
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
 
9826
  myquery(rc);
 
9827
 
 
9828
  rc= mysql_query(mysql, "create table t2 (a int);");
 
9829
  myquery(rc);
 
9830
 
 
9831
  rc= mysql_query(mysql, "create table t1 (a int);");
 
9832
  myquery(rc);
 
9833
 
 
9834
  rc= mysql_query(mysql, "insert into t2 values (3), (2), (1);");
 
9835
  myquery(rc);
 
9836
 
 
9837
  query= (char*)"create table t1 (a int)";
 
9838
  stmt_create= mysql_simple_prepare(mysql, query);
 
9839
  check_stmt(stmt_create);
 
9840
 
 
9841
  query= (char*)"drop table t1";
 
9842
  stmt_drop= mysql_simple_prepare(mysql, query);
 
9843
  check_stmt(stmt_drop);
 
9844
 
 
9845
  query= (char*)"select a in (select a from t2) from t1";
 
9846
  stmt_select= mysql_simple_prepare(mysql, query);
 
9847
  check_stmt(stmt_select);
 
9848
 
 
9849
  rc= mysql_query(mysql, "DROP TABLE t1");
 
9850
  myquery(rc);
 
9851
 
 
9852
  query= (char*)"create table t1 select a from t2";
 
9853
  stmt_create_select= mysql_simple_prepare(mysql, query);
 
9854
  check_stmt(stmt_create_select);
 
9855
 
 
9856
  for (i= 0; i < 3; i++)
 
9857
  {
 
9858
    rc= mysql_stmt_execute(stmt_create);
 
9859
    check_execute(stmt_create, rc);
 
9860
    if (!opt_silent)
 
9861
      fprintf(stdout, "created %i\n", i);
 
9862
 
 
9863
    rc= mysql_stmt_execute(stmt_select);
 
9864
    check_execute(stmt_select, rc);
 
9865
    rc= my_process_stmt_result(stmt_select);
 
9866
    DIE_UNLESS(rc == 0);
 
9867
 
 
9868
    rc= mysql_stmt_execute(stmt_drop);
 
9869
    check_execute(stmt_drop, rc);
 
9870
    if (!opt_silent)
 
9871
      fprintf(stdout, "dropped %i\n", i);
 
9872
 
 
9873
    rc= mysql_stmt_execute(stmt_create_select);
 
9874
    check_execute(stmt_create, rc);
 
9875
    if (!opt_silent)
 
9876
      fprintf(stdout, "created select %i\n", i);
 
9877
 
 
9878
    rc= mysql_stmt_execute(stmt_select);
 
9879
    check_execute(stmt_select, rc);
 
9880
    rc= my_process_stmt_result(stmt_select);
 
9881
    DIE_UNLESS(rc == 3);
 
9882
 
 
9883
    rc= mysql_stmt_execute(stmt_drop);
 
9884
    check_execute(stmt_drop, rc);
 
9885
    if (!opt_silent)
 
9886
      fprintf(stdout, "dropped %i\n", i);
 
9887
  }
 
9888
 
 
9889
  mysql_stmt_close(stmt_create);
 
9890
  mysql_stmt_close(stmt_drop);
 
9891
  mysql_stmt_close(stmt_select);
 
9892
  mysql_stmt_close(stmt_create_select);
 
9893
 
 
9894
  rc= mysql_query(mysql, "DROP TABLE t2");
 
9895
  myquery(rc);
 
9896
}
 
9897
 
 
9898
 
 
9899
static void test_rename()
 
9900
{
 
9901
  MYSQL_STMT *stmt;
 
9902
  const char *query= "rename table t1 to t2, t3 to t4";
 
9903
  int rc;
 
9904
  myheader("test_table_manipulation");
 
9905
 
 
9906
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2, t3, t4");
 
9907
  myquery(rc);
 
9908
 
 
9909
  stmt= mysql_simple_prepare(mysql, query);
 
9910
  check_stmt(stmt);
 
9911
 
 
9912
  rc= mysql_query(mysql, "create table t1 (a int)");
 
9913
  myquery(rc);
 
9914
 
 
9915
  rc= mysql_stmt_execute(stmt);
 
9916
  check_execute_r(stmt, rc);
 
9917
  if (!opt_silent)
 
9918
    fprintf(stdout, "rename without t3\n");
 
9919
 
 
9920
  rc= mysql_query(mysql, "create table t3 (a int)");
 
9921
  myquery(rc);
 
9922
 
 
9923
  rc= mysql_stmt_execute(stmt);
 
9924
  check_execute(stmt, rc);
 
9925
  if (!opt_silent)
 
9926
    fprintf(stdout, "rename with t3\n");
 
9927
 
 
9928
  rc= mysql_stmt_execute(stmt);
 
9929
  check_execute_r(stmt, rc);
 
9930
  if (!opt_silent)
 
9931
    fprintf(stdout, "rename renamed\n");
 
9932
 
 
9933
  rc= mysql_query(mysql, "rename table t2 to t1, t4 to t3");
 
9934
  myquery(rc);
 
9935
 
 
9936
  rc= mysql_stmt_execute(stmt);
 
9937
  check_execute(stmt, rc);
 
9938
  if (!opt_silent)
 
9939
    fprintf(stdout, "rename reverted\n");
 
9940
 
 
9941
  mysql_stmt_close(stmt);
 
9942
 
 
9943
  rc= mysql_query(mysql, "DROP TABLE t2, t4");
 
9944
  myquery(rc);
 
9945
}
 
9946
 
 
9947
 
 
9948
static void test_do_set()
 
9949
{
 
9950
  MYSQL_STMT *stmt_do, *stmt_set;
 
9951
  char *query;
 
9952
  int rc, i;
 
9953
  myheader("test_do_set");
 
9954
 
 
9955
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
9956
  myquery(rc);
 
9957
 
 
9958
  rc= mysql_query(mysql, "create table t1 (a int)");
 
9959
  myquery(rc);
 
9960
 
 
9961
  query= (char*)"do @var:=(1 in (select * from t1))";
 
9962
  stmt_do= mysql_simple_prepare(mysql, query);
 
9963
  check_stmt(stmt_do);
 
9964
 
 
9965
  query= (char*)"set @var=(1 in (select * from t1))";
 
9966
  stmt_set= mysql_simple_prepare(mysql, query);
 
9967
  check_stmt(stmt_set);
 
9968
 
 
9969
  for (i= 0; i < 3; i++)
 
9970
  {
 
9971
    rc= mysql_stmt_execute(stmt_do);
 
9972
    check_execute(stmt_do, rc);
 
9973
    if (!opt_silent)
 
9974
      fprintf(stdout, "do %i\n", i);
 
9975
    rc= mysql_stmt_execute(stmt_set);
 
9976
    check_execute(stmt_set, rc);
 
9977
    if (!opt_silent)
 
9978
      fprintf(stdout, "set %i\n", i);
 
9979
  }
 
9980
 
 
9981
  mysql_stmt_close(stmt_do);
 
9982
  mysql_stmt_close(stmt_set);
 
9983
}
 
9984
 
 
9985
 
 
9986
static void test_multi()
 
9987
{
 
9988
  MYSQL_STMT *stmt_delete, *stmt_update, *stmt_select1, *stmt_select2;
 
9989
  char *query;
 
9990
  MYSQL_BIND my_bind[1];
 
9991
  int rc, i;
 
9992
  int32 param= 1;
 
9993
  ulong length= 1;
 
9994
  myheader("test_multi");
 
9995
 
 
9996
  /*
 
9997
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
9998
    its members.
 
9999
  */
 
10000
  bzero((char*) my_bind, sizeof(my_bind));
 
10001
 
 
10002
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
10003
  my_bind[0].buffer= (void *)&param;
 
10004
  my_bind[0].length= &length;
 
10005
 
 
10006
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
 
10007
  myquery(rc);
 
10008
 
 
10009
  rc= mysql_query(mysql, "create table t1 (a int, b int)");
 
10010
  myquery(rc);
 
10011
 
 
10012
  rc= mysql_query(mysql, "create table t2 (a int, b int)");
 
10013
  myquery(rc);
 
10014
 
 
10015
  rc= mysql_query(mysql, "insert into t1 values (3, 3), (2, 2), (1, 1)");
 
10016
  myquery(rc);
 
10017
 
 
10018
  rc= mysql_query(mysql, "insert into t2 values (3, 3), (2, 2), (1, 1)");
 
10019
  myquery(rc);
 
10020
 
 
10021
  query= (char*)"delete t1, t2 from t1, t2 where t1.a=t2.a and t1.b=10";
 
10022
  stmt_delete= mysql_simple_prepare(mysql, query);
 
10023
  check_stmt(stmt_delete);
 
10024
 
 
10025
  query= (char*)"update t1, t2 set t1.b=10, t2.b=10 where t1.a=t2.a and t1.b=?";
 
10026
  stmt_update= mysql_simple_prepare(mysql, query);
 
10027
  check_stmt(stmt_update);
 
10028
 
 
10029
  query= (char*)"select * from t1";
 
10030
  stmt_select1= mysql_simple_prepare(mysql, query);
 
10031
  check_stmt(stmt_select1);
 
10032
 
 
10033
  query= (char*)"select * from t2";
 
10034
  stmt_select2= mysql_simple_prepare(mysql, query);
 
10035
  check_stmt(stmt_select2);
 
10036
 
 
10037
  for(i= 0; i < 3; i++)
 
10038
  {
 
10039
    rc= mysql_stmt_bind_param(stmt_update, my_bind);
 
10040
    check_execute(stmt_update, rc);
 
10041
 
 
10042
    rc= mysql_stmt_execute(stmt_update);
 
10043
    check_execute(stmt_update, rc);
 
10044
    if (!opt_silent)
 
10045
      fprintf(stdout, "update %ld\n", (long) param);
 
10046
 
 
10047
    rc= mysql_stmt_execute(stmt_delete);
 
10048
    check_execute(stmt_delete, rc);
 
10049
    if (!opt_silent)
 
10050
      fprintf(stdout, "delete %ld\n", (long) param);
 
10051
 
 
10052
    rc= mysql_stmt_execute(stmt_select1);
 
10053
    check_execute(stmt_select1, rc);
 
10054
    rc= my_process_stmt_result(stmt_select1);
 
10055
    DIE_UNLESS(rc == 3-param);
 
10056
 
 
10057
    rc= mysql_stmt_execute(stmt_select2);
 
10058
    check_execute(stmt_select2, rc);
 
10059
    rc= my_process_stmt_result(stmt_select2);
 
10060
    DIE_UNLESS(rc == 3-param);
 
10061
 
 
10062
    param++;
 
10063
  }
 
10064
 
 
10065
  mysql_stmt_close(stmt_delete);
 
10066
  mysql_stmt_close(stmt_update);
 
10067
  mysql_stmt_close(stmt_select1);
 
10068
  mysql_stmt_close(stmt_select2);
 
10069
  rc= mysql_query(mysql, "drop table t1, t2");
 
10070
  myquery(rc);
 
10071
}
 
10072
 
 
10073
 
 
10074
static void test_insert_select()
 
10075
{
 
10076
  MYSQL_STMT *stmt_insert, *stmt_select;
 
10077
  char *query;
 
10078
  int rc;
 
10079
  uint i;
 
10080
  myheader("test_insert_select");
 
10081
 
 
10082
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
 
10083
  myquery(rc);
 
10084
 
 
10085
  rc= mysql_query(mysql, "create table t1 (a int)");
 
10086
  myquery(rc);
 
10087
 
 
10088
  rc= mysql_query(mysql, "create table t2 (a int)");
 
10089
  myquery(rc);
 
10090
 
 
10091
  rc= mysql_query(mysql, "insert into t2 values (1)");
 
10092
  myquery(rc);
 
10093
 
 
10094
  query= (char*)"insert into t1 select a from t2";
 
10095
  stmt_insert= mysql_simple_prepare(mysql, query);
 
10096
  check_stmt(stmt_insert);
 
10097
 
 
10098
  query= (char*)"select * from t1";
 
10099
  stmt_select= mysql_simple_prepare(mysql, query);
 
10100
  check_stmt(stmt_select);
 
10101
 
 
10102
  for(i= 0; i < 3; i++)
 
10103
  {
 
10104
    rc= mysql_stmt_execute(stmt_insert);
 
10105
    check_execute(stmt_insert, rc);
 
10106
    if (!opt_silent)
 
10107
      fprintf(stdout, "insert %u\n", i);
 
10108
 
 
10109
    rc= mysql_stmt_execute(stmt_select);
 
10110
    check_execute(stmt_select, rc);
 
10111
    rc= my_process_stmt_result(stmt_select);
 
10112
    DIE_UNLESS(rc == (int)(i+1));
 
10113
  }
 
10114
 
 
10115
  mysql_stmt_close(stmt_insert);
 
10116
  mysql_stmt_close(stmt_select);
 
10117
  rc= mysql_query(mysql, "drop table t1, t2");
 
10118
  myquery(rc);
 
10119
}
 
10120
 
 
10121
 
 
10122
static void test_bind_nagative()
 
10123
{
 
10124
  MYSQL_STMT *stmt_insert;
 
10125
  char *query;
 
10126
  int rc;
 
10127
  MYSQL_BIND      my_bind[1];
 
10128
  int32           my_val= 0;
 
10129
  ulong           my_length= 0L;
 
10130
  my_bool         my_null= FALSE;
 
10131
  myheader("test_insert_select");
 
10132
 
 
10133
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
10134
  myquery(rc);
 
10135
 
 
10136
  rc= mysql_query(mysql, "create temporary table t1 (c1 int unsigned)");
 
10137
  myquery(rc);
 
10138
 
 
10139
  rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1), (-1)");
 
10140
  myquery(rc);
 
10141
 
 
10142
  query= (char*)"INSERT INTO t1 VALUES (?)";
 
10143
  stmt_insert= mysql_simple_prepare(mysql, query);
 
10144
  check_stmt(stmt_insert);
 
10145
 
 
10146
  /* bind parameters */
 
10147
  bzero((char*) my_bind, sizeof(my_bind));
 
10148
 
 
10149
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
10150
  my_bind[0].buffer= (void *)&my_val;
 
10151
  my_bind[0].length= &my_length;
 
10152
  my_bind[0].is_null= (char*)&my_null;
 
10153
 
 
10154
  rc= mysql_stmt_bind_param(stmt_insert, my_bind);
 
10155
  check_execute(stmt_insert, rc);
 
10156
 
 
10157
  my_val= -1;
 
10158
  rc= mysql_stmt_execute(stmt_insert);
 
10159
  check_execute(stmt_insert, rc);
 
10160
 
 
10161
  mysql_stmt_close(stmt_insert);
 
10162
  rc= mysql_query(mysql, "drop table t1");
 
10163
  myquery(rc);
 
10164
}
 
10165
 
 
10166
 
 
10167
static void test_derived()
 
10168
{
 
10169
  MYSQL_STMT *stmt;
 
10170
  int rc, i;
 
10171
  MYSQL_BIND      my_bind[1];
 
10172
  int32           my_val= 0;
 
10173
  ulong           my_length= 0L;
 
10174
  my_bool         my_null= FALSE;
 
10175
  const char *query=
 
10176
    "select count(1) from (select f.id from t1 f where f.id=?) as x";
 
10177
 
 
10178
  myheader("test_derived");
 
10179
 
 
10180
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
10181
  myquery(rc);
 
10182
 
 
10183
  rc= mysql_query(mysql, "create table t1 (id  int(8), primary key (id)) \
 
10184
ENGINE=InnoDB DEFAULT CHARSET=utf8");
 
10185
  myquery(rc);
 
10186
 
 
10187
  rc= mysql_query(mysql, "insert into t1 values (1)");
 
10188
  myquery(rc);
 
10189
 
 
10190
  stmt= mysql_simple_prepare(mysql, query);
 
10191
  check_stmt(stmt);
 
10192
  /*
 
10193
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
10194
    its members.
 
10195
  */
 
10196
  bzero((char*) my_bind, sizeof(my_bind));
 
10197
 
 
10198
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
10199
  my_bind[0].buffer= (void *)&my_val;
 
10200
  my_bind[0].length= &my_length;
 
10201
  my_bind[0].is_null= (char*)&my_null;
 
10202
  my_val= 1;
 
10203
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
10204
  check_execute(stmt, rc);
 
10205
 
 
10206
  for (i= 0; i < 3; i++)
 
10207
  {
 
10208
    rc= mysql_stmt_execute(stmt);
 
10209
    check_execute(stmt, rc);
 
10210
    rc= my_process_stmt_result(stmt);
 
10211
    DIE_UNLESS(rc == 1);
 
10212
  }
 
10213
  mysql_stmt_close(stmt);
 
10214
 
 
10215
  rc= mysql_query(mysql, "DROP TABLE t1");
 
10216
  myquery(rc);
 
10217
}
 
10218
 
 
10219
 
 
10220
static void test_xjoin()
 
10221
{
 
10222
  MYSQL_STMT *stmt;
 
10223
  int rc, i;
 
10224
  const char *query=
 
10225
    "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";
 
10226
 
 
10227
  myheader("test_xjoin");
 
10228
 
 
10229
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2, t3, t4");
 
10230
  myquery(rc);
 
10231
 
 
10232
  rc= mysql_query(mysql, "create table t3 (id int(8), param1_id int(8), param2_id int(8)) ENGINE=InnoDB DEFAULT CHARSET=utf8");
 
10233
  myquery(rc);
 
10234
 
 
10235
  rc= mysql_query(mysql, "create table t1 ( id int(8), name_id int(8), value varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8");
 
10236
  myquery(rc);
 
10237
 
 
10238
  rc= mysql_query(mysql, "create table t2 (id int(8), name_id int(8), value varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
 
10239
  myquery(rc);
 
10240
 
 
10241
  rc= mysql_query(mysql, "create table t4(id int(8), value varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8");
 
10242
  myquery(rc);
 
10243
 
 
10244
  rc= mysql_query(mysql, "insert into t3 values (1, 1, 1), (2, 2, null)");
 
10245
  myquery(rc);
 
10246
 
 
10247
  rc= mysql_query(mysql, "insert into t1 values (1, 1, 'aaa'), (2, null, 'bbb')");
 
10248
  myquery(rc);
 
10249
 
 
10250
  rc= mysql_query(mysql, "insert into t2 values (1, 2, 'ccc')");
 
10251
  myquery(rc);
 
10252
 
 
10253
  rc= mysql_query(mysql, "insert into t4 values (1, 'Name1'), (2, null)");
 
10254
  myquery(rc);
 
10255
 
 
10256
  stmt= mysql_simple_prepare(mysql, query);
 
10257
  check_stmt(stmt);
 
10258
 
 
10259
  for (i= 0; i < 3; i++)
 
10260
  {
 
10261
    rc= mysql_stmt_execute(stmt);
 
10262
    check_execute(stmt, rc);
 
10263
    rc= my_process_stmt_result(stmt);
 
10264
    DIE_UNLESS(rc == 1);
 
10265
  }
 
10266
  mysql_stmt_close(stmt);
 
10267
 
 
10268
  rc= mysql_query(mysql, "DROP TABLE t1, t2, t3, t4");
 
10269
  myquery(rc);
 
10270
}
 
10271
 
 
10272
 
 
10273
static void test_bug3035()
 
10274
{
 
10275
  MYSQL_STMT *stmt;
 
10276
  int rc;
 
10277
  MYSQL_BIND bind_array[12], *my_bind= bind_array, *bind_end= my_bind + 12;
 
10278
  int8 int8_val;
 
10279
  uint8 uint8_val;
 
10280
  int16 int16_val;
 
10281
  uint16 uint16_val;
 
10282
  int32 int32_val;
 
10283
  uint32 uint32_val;
 
10284
  longlong int64_val;
 
10285
  ulonglong uint64_val;
 
10286
  double double_val, udouble_val, double_tmp;
 
10287
  char longlong_as_string[22], ulonglong_as_string[22];
 
10288
 
 
10289
  /* mins and maxes */
 
10290
  const int8 int8_min= -128;
 
10291
  const int8 int8_max= 127;
 
10292
  const uint8 uint8_min= 0;
 
10293
  const uint8 uint8_max= 255;
 
10294
 
 
10295
  const int16 int16_min= -32768;
 
10296
  const int16 int16_max= 32767;
 
10297
  const uint16 uint16_min= 0;
 
10298
  const uint16 uint16_max= 65535;
 
10299
 
 
10300
  const int32 int32_max= 2147483647L;
 
10301
  const int32 int32_min= -int32_max - 1;
 
10302
  const uint32 uint32_min= 0;
 
10303
  const uint32 uint32_max= 4294967295U;
 
10304
 
 
10305
  /* it might not work okay everyplace */
 
10306
  const longlong int64_max= LL(9223372036854775807);
 
10307
  const longlong int64_min= -int64_max - 1;
 
10308
 
 
10309
  const ulonglong uint64_min= 0U;
 
10310
  const ulonglong uint64_max= ULL(18446744073709551615);
 
10311
 
 
10312
  const char *stmt_text;
 
10313
 
 
10314
  myheader("test_bug3035");
 
10315
 
 
10316
  stmt_text= "DROP TABLE IF EXISTS t1";
 
10317
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10318
  myquery(rc);
 
10319
 
 
10320
  stmt_text= "CREATE TABLE t1 (i8 TINYINT, ui8 TINYINT UNSIGNED, "
 
10321
                              "i16 SMALLINT, ui16 SMALLINT UNSIGNED, "
 
10322
                              "i32 INT, ui32 INT UNSIGNED, "
 
10323
                              "i64 BIGINT, ui64 BIGINT UNSIGNED, "
 
10324
                              "id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT)";
 
10325
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10326
  myquery(rc);
 
10327
 
 
10328
  bzero((char*) bind_array, sizeof(bind_array));
 
10329
 
 
10330
  for (my_bind= bind_array; my_bind < bind_end; my_bind++)
 
10331
    my_bind->error= &my_bind->error_value;
 
10332
 
 
10333
  bind_array[0].buffer_type= MYSQL_TYPE_TINY;
 
10334
  bind_array[0].buffer= (void *) &int8_val;
 
10335
 
 
10336
  bind_array[1].buffer_type= MYSQL_TYPE_TINY;
 
10337
  bind_array[1].buffer= (void *) &uint8_val;
 
10338
  bind_array[1].is_unsigned= 1;
 
10339
 
 
10340
  bind_array[2].buffer_type= MYSQL_TYPE_SHORT;
 
10341
  bind_array[2].buffer= (void *) &int16_val;
 
10342
 
 
10343
  bind_array[3].buffer_type= MYSQL_TYPE_SHORT;
 
10344
  bind_array[3].buffer= (void *) &uint16_val;
 
10345
  bind_array[3].is_unsigned= 1;
 
10346
 
 
10347
  bind_array[4].buffer_type= MYSQL_TYPE_LONG;
 
10348
  bind_array[4].buffer= (void *) &int32_val;
 
10349
 
 
10350
  bind_array[5].buffer_type= MYSQL_TYPE_LONG;
 
10351
  bind_array[5].buffer= (void *) &uint32_val;
 
10352
  bind_array[5].is_unsigned= 1;
 
10353
 
 
10354
  bind_array[6].buffer_type= MYSQL_TYPE_LONGLONG;
 
10355
  bind_array[6].buffer= (void *) &int64_val;
 
10356
 
 
10357
  bind_array[7].buffer_type= MYSQL_TYPE_LONGLONG;
 
10358
  bind_array[7].buffer= (void *) &uint64_val;
 
10359
  bind_array[7].is_unsigned= 1;
 
10360
 
 
10361
  stmt= mysql_stmt_init(mysql);
 
10362
  check_stmt(stmt);
 
10363
 
 
10364
  stmt_text= "INSERT INTO t1 (i8, ui8, i16, ui16, i32, ui32, i64, ui64) "
 
10365
                     "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
 
10366
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
10367
  check_execute(stmt, rc);
 
10368
 
 
10369
  mysql_stmt_bind_param(stmt, bind_array);
 
10370
 
 
10371
  int8_val= int8_min;
 
10372
  uint8_val= uint8_min;
 
10373
  int16_val= int16_min;
 
10374
  uint16_val= uint16_min;
 
10375
  int32_val= int32_min;
 
10376
  uint32_val= uint32_min;
 
10377
  int64_val= int64_min;
 
10378
  uint64_val= uint64_min;
 
10379
 
 
10380
  rc= mysql_stmt_execute(stmt);
 
10381
  check_execute(stmt, rc);
 
10382
 
 
10383
  int8_val= int8_max;
 
10384
  uint8_val= uint8_max;
 
10385
  int16_val= int16_max;
 
10386
  uint16_val= uint16_max;
 
10387
  int32_val= int32_max;
 
10388
  uint32_val= uint32_max;
 
10389
  int64_val= int64_max;
 
10390
  uint64_val= uint64_max;
 
10391
 
 
10392
  rc= mysql_stmt_execute(stmt);
 
10393
  check_execute(stmt, rc);
 
10394
 
 
10395
  stmt_text= "SELECT i8, ui8, i16, ui16, i32, ui32, i64, ui64, ui64, "
 
10396
             "cast(ui64 as signed), ui64, cast(ui64 as signed)"
 
10397
             "FROM t1 ORDER BY id ASC";
 
10398
 
 
10399
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
10400
  check_execute(stmt, rc);
 
10401
 
 
10402
  rc= mysql_stmt_execute(stmt);
 
10403
  check_execute(stmt, rc);
 
10404
 
 
10405
  bind_array[8].buffer_type= MYSQL_TYPE_DOUBLE;
 
10406
  bind_array[8].buffer= (void *) &udouble_val;
 
10407
 
 
10408
  bind_array[9].buffer_type= MYSQL_TYPE_DOUBLE;
 
10409
  bind_array[9].buffer= (void *) &double_val;
 
10410
 
 
10411
  bind_array[10].buffer_type= MYSQL_TYPE_STRING;
 
10412
  bind_array[10].buffer= (void *) &ulonglong_as_string;
 
10413
  bind_array[10].buffer_length= sizeof(ulonglong_as_string);
 
10414
 
 
10415
  bind_array[11].buffer_type= MYSQL_TYPE_STRING;
 
10416
  bind_array[11].buffer= (void *) &longlong_as_string;
 
10417
  bind_array[11].buffer_length= sizeof(longlong_as_string);
 
10418
 
 
10419
  mysql_stmt_bind_result(stmt, bind_array);
 
10420
 
 
10421
  rc= mysql_stmt_fetch(stmt);
 
10422
  check_execute(stmt, rc);
 
10423
 
 
10424
  DIE_UNLESS(int8_val == int8_min);
 
10425
  DIE_UNLESS(uint8_val == uint8_min);
 
10426
  DIE_UNLESS(int16_val == int16_min);
 
10427
  DIE_UNLESS(uint16_val == uint16_min);
 
10428
  DIE_UNLESS(int32_val == int32_min);
 
10429
  DIE_UNLESS(uint32_val == uint32_min);
 
10430
  DIE_UNLESS(int64_val == int64_min);
 
10431
  DIE_UNLESS(uint64_val == uint64_min);
 
10432
  DIE_UNLESS(double_val == (longlong) uint64_min);
 
10433
  double_tmp= ulonglong2double(uint64_val);
 
10434
  DIE_UNLESS(cmp_double(&udouble_val, &double_tmp));
 
10435
  DIE_UNLESS(!strcmp(longlong_as_string, "0"));
 
10436
  DIE_UNLESS(!strcmp(ulonglong_as_string, "0"));
 
10437
 
 
10438
  rc= mysql_stmt_fetch(stmt);
 
10439
 
 
10440
  if (!opt_silent)
 
10441
  {
 
10442
    printf("Truncation mask: ");
 
10443
    for (my_bind= bind_array; my_bind < bind_end; my_bind++)
 
10444
      printf("%d", (int) my_bind->error_value);
 
10445
    printf("\n");
 
10446
  }
 
10447
  DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED || rc == 0);
 
10448
 
 
10449
  DIE_UNLESS(int8_val == int8_max);
 
10450
  DIE_UNLESS(uint8_val == uint8_max);
 
10451
  DIE_UNLESS(int16_val == int16_max);
 
10452
  DIE_UNLESS(uint16_val == uint16_max);
 
10453
  DIE_UNLESS(int32_val == int32_max);
 
10454
  DIE_UNLESS(uint32_val == uint32_max);
 
10455
  DIE_UNLESS(int64_val == int64_max);
 
10456
  DIE_UNLESS(uint64_val == uint64_max);
 
10457
  DIE_UNLESS(double_val == (longlong) uint64_val);
 
10458
  double_tmp= ulonglong2double(uint64_val);
 
10459
  DIE_UNLESS(cmp_double(&udouble_val, &double_tmp));
 
10460
  DIE_UNLESS(!strcmp(longlong_as_string, "-1"));
 
10461
  DIE_UNLESS(!strcmp(ulonglong_as_string, "18446744073709551615"));
 
10462
 
 
10463
  rc= mysql_stmt_fetch(stmt);
 
10464
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
10465
 
 
10466
  mysql_stmt_close(stmt);
 
10467
 
 
10468
  stmt_text= "DROP TABLE t1";
 
10469
  mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10470
}
 
10471
 
 
10472
 
 
10473
static void test_union2()
 
10474
{
 
10475
  MYSQL_STMT *stmt;
 
10476
  int rc, i;
 
10477
 
 
10478
  myheader("test_union2");
 
10479
 
 
10480
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
10481
  myquery(rc);
 
10482
 
 
10483
  rc= mysql_query(mysql, "CREATE TABLE t1(col1 INT, \
 
10484
                                         col2 VARCHAR(40),      \
 
10485
                                         col3 SMALLINT, \
 
10486
                                         col4 TIMESTAMP)");
 
10487
  myquery(rc);
 
10488
 
 
10489
  stmt= mysql_simple_prepare(mysql,
 
10490
                             "select col1 FROM t1 where col1=1 union distinct "
 
10491
                             "select col1 FROM t1 where col1=2");
 
10492
  check_stmt(stmt);
 
10493
 
 
10494
  for (i= 0; i < 3; i++)
 
10495
  {
 
10496
    rc= mysql_stmt_execute(stmt);
 
10497
    check_execute(stmt, rc);
 
10498
    rc= my_process_stmt_result(stmt);
 
10499
    DIE_UNLESS(rc == 0);
 
10500
  }
 
10501
 
 
10502
  mysql_stmt_close(stmt);
 
10503
 
 
10504
  rc= mysql_query(mysql, "DROP TABLE t1");
 
10505
  myquery(rc);
 
10506
}
 
10507
 
 
10508
 
 
10509
/*
 
10510
  This tests for various mysql_stmt_send_long_data bugs described in #1664
 
10511
*/
 
10512
 
 
10513
static void test_bug1664()
 
10514
{
 
10515
    MYSQL_STMT *stmt;
 
10516
    int        rc, int_data;
 
10517
    const char *data;
 
10518
    const char *str_data= "Simple string";
 
10519
    MYSQL_BIND my_bind[2];
 
10520
    const char *query= "INSERT INTO test_long_data(col2, col1) VALUES(?, ?)";
 
10521
 
 
10522
    myheader("test_bug1664");
 
10523
 
 
10524
    rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data");
 
10525
    myquery(rc);
 
10526
 
 
10527
    rc= mysql_query(mysql, "CREATE TABLE test_long_data(col1 int, col2 long varchar)");
 
10528
    myquery(rc);
 
10529
 
 
10530
    stmt= mysql_stmt_init(mysql);
 
10531
    check_stmt(stmt);
 
10532
    rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
10533
    check_execute(stmt, rc);
 
10534
 
 
10535
    verify_param_count(stmt, 2);
 
10536
 
 
10537
    bzero((char*) my_bind, sizeof(my_bind));
 
10538
 
 
10539
    my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
10540
    my_bind[0].buffer= (void *)str_data;
 
10541
    my_bind[0].buffer_length= strlen(str_data);
 
10542
 
 
10543
    my_bind[1].buffer= (void *)&int_data;
 
10544
    my_bind[1].buffer_type= MYSQL_TYPE_LONG;
 
10545
 
 
10546
    rc= mysql_stmt_bind_param(stmt, my_bind);
 
10547
    check_execute(stmt, rc);
 
10548
 
 
10549
    int_data= 1;
 
10550
 
 
10551
    /*
 
10552
      Let us supply empty long_data. This should work and should
 
10553
      not break following execution.
 
10554
    */
 
10555
    data= "";
 
10556
    rc= mysql_stmt_send_long_data(stmt, 0, data, strlen(data));
 
10557
    check_execute(stmt, rc);
 
10558
 
 
10559
    rc= mysql_stmt_execute(stmt);
 
10560
    check_execute(stmt, rc);
 
10561
 
 
10562
    verify_col_data("test_long_data", "col1", "1");
 
10563
    verify_col_data("test_long_data", "col2", "");
 
10564
 
 
10565
    rc= mysql_query(mysql, "DELETE FROM test_long_data");
 
10566
    myquery(rc);
 
10567
 
 
10568
    /* This should pass OK */
 
10569
    data= (char *)"Data";
 
10570
    rc= mysql_stmt_send_long_data(stmt, 0, data, strlen(data));
 
10571
    check_execute(stmt, rc);
 
10572
 
 
10573
    rc= mysql_stmt_execute(stmt);
 
10574
    check_execute(stmt, rc);
 
10575
 
 
10576
    verify_col_data("test_long_data", "col1", "1");
 
10577
    verify_col_data("test_long_data", "col2", "Data");
 
10578
 
 
10579
    /* clean up */
 
10580
    rc= mysql_query(mysql, "DELETE FROM test_long_data");
 
10581
    myquery(rc);
 
10582
 
 
10583
    /*
 
10584
      Now we are changing int parameter and don't do anything
 
10585
      with first parameter. Second mysql_stmt_execute() should run
 
10586
      OK treating this first parameter as string parameter.
 
10587
    */
 
10588
 
 
10589
    int_data= 2;
 
10590
    /* execute */
 
10591
    rc= mysql_stmt_execute(stmt);
 
10592
    check_execute(stmt, rc);
 
10593
 
 
10594
    verify_col_data("test_long_data", "col1", "2");
 
10595
    verify_col_data("test_long_data", "col2", str_data);
 
10596
 
 
10597
    /* clean up */
 
10598
    rc= mysql_query(mysql, "DELETE FROM test_long_data");
 
10599
    myquery(rc);
 
10600
 
 
10601
    /*
 
10602
      Now we are sending other long data. It should not be
 
10603
      concatened to previous.
 
10604
    */
 
10605
 
 
10606
    data= (char *)"SomeOtherData";
 
10607
    rc= mysql_stmt_send_long_data(stmt, 0, data, strlen(data));
 
10608
    check_execute(stmt, rc);
 
10609
 
 
10610
    rc= mysql_stmt_execute(stmt);
 
10611
    check_execute(stmt, rc);
 
10612
 
 
10613
    verify_col_data("test_long_data", "col1", "2");
 
10614
    verify_col_data("test_long_data", "col2", "SomeOtherData");
 
10615
 
 
10616
    mysql_stmt_close(stmt);
 
10617
 
 
10618
    /* clean up */
 
10619
    rc= mysql_query(mysql, "DELETE FROM test_long_data");
 
10620
    myquery(rc);
 
10621
 
 
10622
    /* Now let us test how mysql_stmt_reset works. */
 
10623
    stmt= mysql_stmt_init(mysql);
 
10624
    check_stmt(stmt);
 
10625
    rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
10626
    check_execute(stmt, rc);
 
10627
    rc= mysql_stmt_bind_param(stmt, my_bind);
 
10628
    check_execute(stmt, rc);
 
10629
 
 
10630
    data= (char *)"SomeData";
 
10631
    rc= mysql_stmt_send_long_data(stmt, 0, data, strlen(data));
 
10632
    check_execute(stmt, rc);
 
10633
 
 
10634
    rc= mysql_stmt_reset(stmt);
 
10635
    check_execute(stmt, rc);
 
10636
 
 
10637
    rc= mysql_stmt_execute(stmt);
 
10638
    check_execute(stmt, rc);
 
10639
 
 
10640
    verify_col_data("test_long_data", "col1", "2");
 
10641
    verify_col_data("test_long_data", "col2", str_data);
 
10642
 
 
10643
    mysql_stmt_close(stmt);
 
10644
 
 
10645
    /* Final clean up */
 
10646
    rc= mysql_query(mysql, "DROP TABLE test_long_data");
 
10647
    myquery(rc);
 
10648
}
 
10649
 
 
10650
 
 
10651
static void test_order_param()
 
10652
{
 
10653
  MYSQL_STMT *stmt;
 
10654
  int rc;
 
10655
 
 
10656
  myheader("test_order_param");
 
10657
 
 
10658
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
10659
  myquery(rc);
 
10660
 
 
10661
  rc= mysql_query(mysql, "CREATE TABLE t1(a INT, b char(10))");
 
10662
  myquery(rc);
 
10663
 
 
10664
  stmt= mysql_simple_prepare(mysql,
 
10665
                             "select sum(a) + 200, 1 from t1 "
 
10666
                             " union distinct "
 
10667
                             "select sum(a) + 200, 1 from t1 group by b ");
 
10668
  check_stmt(stmt);
 
10669
  mysql_stmt_close(stmt);
 
10670
 
 
10671
  stmt= mysql_simple_prepare(mysql,
 
10672
                             "select sum(a) + 200, ? from t1 group by b "
 
10673
                             " union distinct "
 
10674
                             "select sum(a) + 200, 1 from t1 group by b ");
 
10675
  check_stmt(stmt);
 
10676
  mysql_stmt_close(stmt);
 
10677
 
 
10678
  stmt= mysql_simple_prepare(mysql,
 
10679
                             "select sum(a) + 200, ? from t1 "
 
10680
                             " union distinct "
 
10681
                             "select sum(a) + 200, 1 from t1 group by b ");
 
10682
  check_stmt(stmt);
 
10683
  mysql_stmt_close(stmt);
 
10684
 
 
10685
  rc= mysql_query(mysql, "DROP TABLE t1");
 
10686
  myquery(rc);
 
10687
}
 
10688
 
 
10689
 
 
10690
static void test_union_param()
 
10691
{
 
10692
  MYSQL_STMT *stmt;
 
10693
  char *query;
 
10694
  int rc, i;
 
10695
  MYSQL_BIND      my_bind[2];
 
10696
  char            my_val[4];
 
10697
  ulong           my_length= 3L;
 
10698
  my_bool         my_null= FALSE;
 
10699
  myheader("test_union_param");
 
10700
 
 
10701
  strmov(my_val, "abc");
 
10702
 
 
10703
  query= (char*)"select ? as my_col union distinct select ?";
 
10704
  stmt= mysql_simple_prepare(mysql, query);
 
10705
  check_stmt(stmt);
 
10706
 
 
10707
  /*
 
10708
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
10709
    its members.
 
10710
  */
 
10711
  bzero((char*) my_bind, sizeof(my_bind));
 
10712
 
 
10713
  /* bind parameters */
 
10714
  my_bind[0].buffer_type=    MYSQL_TYPE_STRING;
 
10715
  my_bind[0].buffer=         (char*) &my_val;
 
10716
  my_bind[0].buffer_length=  4;
 
10717
  my_bind[0].length=         &my_length;
 
10718
  my_bind[0].is_null=        (char*)&my_null;
 
10719
  my_bind[1].buffer_type=    MYSQL_TYPE_STRING;
 
10720
  my_bind[1].buffer=         (char*) &my_val;
 
10721
  my_bind[1].buffer_length=  4;
 
10722
  my_bind[1].length=         &my_length;
 
10723
  my_bind[1].is_null=        (char*)&my_null;
 
10724
 
 
10725
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
10726
  check_execute(stmt, rc);
 
10727
 
 
10728
  for (i= 0; i < 3; i++)
 
10729
  {
 
10730
    rc= mysql_stmt_execute(stmt);
 
10731
    check_execute(stmt, rc);
 
10732
    rc= my_process_stmt_result(stmt);
 
10733
    DIE_UNLESS(rc == 1);
 
10734
  }
 
10735
 
 
10736
  mysql_stmt_close(stmt);
 
10737
}
 
10738
 
 
10739
 
 
10740
static void test_ps_i18n()
 
10741
{
 
10742
  MYSQL_STMT *stmt;
 
10743
  int rc;
 
10744
  const char *stmt_text;
 
10745
  MYSQL_BIND bind_array[2];
 
10746
 
 
10747
  /* Represented as numbers to keep UTF8 tools from clobbering them. */
 
10748
  const char *koi8= "\xee\xd5\x2c\x20\xda\xc1\x20\xd2\xd9\xc2\xc1\xcc\xcb\xd5";
 
10749
  const char *cp1251= "\xcd\xf3\x2c\x20\xe7\xe0\x20\xf0\xfb\xe1\xe0\xeb\xea\xf3";
 
10750
  char buf1[16], buf2[16];
 
10751
  ulong buf1_len, buf2_len;
 
10752
 
 
10753
 
 
10754
  myheader("test_ps_i18n");
 
10755
 
 
10756
  stmt_text= "DROP TABLE IF EXISTS t1";
 
10757
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10758
  myquery(rc);
 
10759
 
 
10760
  /*
 
10761
    Create table with binary columns, set session character set to cp1251,
 
10762
    client character set to koi8, and make sure that there is conversion
 
10763
    on insert and no conversion on select
 
10764
  */
 
10765
 
 
10766
  stmt_text= "CREATE TABLE t1 (c1 VARBINARY(255), c2 VARBINARY(255))";
 
10767
 
 
10768
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10769
  myquery(rc);
 
10770
 
 
10771
  stmt_text= "SET CHARACTER_SET_CLIENT=koi8r, "
 
10772
                 "CHARACTER_SET_CONNECTION=cp1251, "
 
10773
                 "CHARACTER_SET_RESULTS=koi8r";
 
10774
 
 
10775
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10776
  myquery(rc);
 
10777
 
 
10778
  bzero((char*) bind_array, sizeof(bind_array));
 
10779
 
 
10780
  bind_array[0].buffer_type= MYSQL_TYPE_STRING;
 
10781
  bind_array[0].buffer= (void *) koi8;
 
10782
  bind_array[0].buffer_length= strlen(koi8);
 
10783
 
 
10784
  bind_array[1].buffer_type= MYSQL_TYPE_STRING;
 
10785
  bind_array[1].buffer= (void *) koi8;
 
10786
  bind_array[1].buffer_length= strlen(koi8);
 
10787
 
 
10788
  stmt= mysql_stmt_init(mysql);
 
10789
  check_stmt(stmt);
 
10790
 
 
10791
  stmt_text= "INSERT INTO t1 (c1, c2) VALUES (?, ?)";
 
10792
 
 
10793
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
10794
  check_execute(stmt, rc);
 
10795
 
 
10796
  mysql_stmt_bind_param(stmt, bind_array);
 
10797
 
 
10798
  mysql_stmt_send_long_data(stmt, 0, koi8, strlen(koi8));
 
10799
 
 
10800
  rc= mysql_stmt_execute(stmt);
 
10801
  check_execute(stmt, rc);
 
10802
 
 
10803
  stmt_text= "SELECT c1, c2 FROM t1";
 
10804
 
 
10805
  /* c1 and c2 are binary so no conversion will be done on select */
 
10806
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
10807
  check_execute(stmt, rc);
 
10808
 
 
10809
  rc= mysql_stmt_execute(stmt);
 
10810
  check_execute(stmt, rc);
 
10811
 
 
10812
  bind_array[0].buffer= buf1;
 
10813
  bind_array[0].buffer_length= sizeof(buf1);
 
10814
  bind_array[0].length= &buf1_len;
 
10815
 
 
10816
  bind_array[1].buffer= buf2;
 
10817
  bind_array[1].buffer_length= sizeof(buf2);
 
10818
  bind_array[1].length= &buf2_len;
 
10819
 
 
10820
  mysql_stmt_bind_result(stmt, bind_array);
 
10821
 
 
10822
  rc= mysql_stmt_fetch(stmt);
 
10823
  check_execute(stmt, rc);
 
10824
 
 
10825
  DIE_UNLESS(buf1_len == strlen(cp1251));
 
10826
  DIE_UNLESS(buf2_len == strlen(cp1251));
 
10827
  DIE_UNLESS(!memcmp(buf1, cp1251, buf1_len));
 
10828
  DIE_UNLESS(!memcmp(buf2, cp1251, buf1_len));
 
10829
 
 
10830
  rc= mysql_stmt_fetch(stmt);
 
10831
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
10832
 
 
10833
  stmt_text= "DROP TABLE IF EXISTS t1";
 
10834
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10835
  myquery(rc);
 
10836
 
 
10837
  /*
 
10838
    Now create table with two cp1251 columns, set client character
 
10839
    set to koi8 and supply columns of one row as string and another as
 
10840
    binary data. Binary data must not be converted on insert, and both
 
10841
    columns must be converted to client character set on select.
 
10842
  */
 
10843
 
 
10844
  stmt_text= "CREATE TABLE t1 (c1 VARCHAR(255) CHARACTER SET cp1251, "
 
10845
                              "c2 VARCHAR(255) CHARACTER SET cp1251)";
 
10846
 
 
10847
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10848
  myquery(rc);
 
10849
 
 
10850
  stmt_text= "INSERT INTO t1 (c1, c2) VALUES (?, ?)";
 
10851
 
 
10852
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
10853
  check_execute(stmt, rc);
 
10854
 
 
10855
  /* this data must be converted */
 
10856
  bind_array[0].buffer_type= MYSQL_TYPE_STRING;
 
10857
  bind_array[0].buffer= (void *) koi8;
 
10858
  bind_array[0].buffer_length= strlen(koi8);
 
10859
 
 
10860
  bind_array[1].buffer_type= MYSQL_TYPE_STRING;
 
10861
  bind_array[1].buffer= (void *) koi8;
 
10862
  bind_array[1].buffer_length= strlen(koi8);
 
10863
 
 
10864
  mysql_stmt_bind_param(stmt, bind_array);
 
10865
 
 
10866
  mysql_stmt_send_long_data(stmt, 0, koi8, strlen(koi8));
 
10867
 
 
10868
  rc= mysql_stmt_execute(stmt);
 
10869
  check_execute(stmt, rc);
 
10870
 
 
10871
  /* this data must not be converted */
 
10872
  bind_array[0].buffer_type= MYSQL_TYPE_BLOB;
 
10873
  bind_array[0].buffer= (void *) cp1251;
 
10874
  bind_array[0].buffer_length= strlen(cp1251);
 
10875
 
 
10876
  bind_array[1].buffer_type= MYSQL_TYPE_BLOB;
 
10877
  bind_array[1].buffer= (void *) cp1251;
 
10878
  bind_array[1].buffer_length= strlen(cp1251);
 
10879
 
 
10880
  mysql_stmt_bind_param(stmt, bind_array);
 
10881
 
 
10882
  mysql_stmt_send_long_data(stmt, 0, cp1251, strlen(cp1251));
 
10883
 
 
10884
  rc= mysql_stmt_execute(stmt);
 
10885
  check_execute(stmt, rc);
 
10886
 
 
10887
  /* Fetch data and verify that rows are in koi8 */
 
10888
 
 
10889
  stmt_text= "SELECT c1, c2 FROM t1";
 
10890
 
 
10891
  /* c1 and c2 are binary so no conversion will be done on select */
 
10892
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
10893
  check_execute(stmt, rc);
 
10894
 
 
10895
  rc= mysql_stmt_execute(stmt);
 
10896
  check_execute(stmt, rc);
 
10897
 
 
10898
  bind_array[0].buffer= buf1;
 
10899
  bind_array[0].buffer_length= sizeof(buf1);
 
10900
  bind_array[0].length= &buf1_len;
 
10901
 
 
10902
  bind_array[1].buffer= buf2;
 
10903
  bind_array[1].buffer_length= sizeof(buf2);
 
10904
  bind_array[1].length= &buf2_len;
 
10905
 
 
10906
  mysql_stmt_bind_result(stmt, bind_array);
 
10907
 
 
10908
  while ((rc= mysql_stmt_fetch(stmt)) == 0)
 
10909
  {
 
10910
    DIE_UNLESS(buf1_len == strlen(koi8));
 
10911
    DIE_UNLESS(buf2_len == strlen(koi8));
 
10912
    DIE_UNLESS(!memcmp(buf1, koi8, buf1_len));
 
10913
    DIE_UNLESS(!memcmp(buf2, koi8, buf1_len));
 
10914
  }
 
10915
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
10916
  mysql_stmt_close(stmt);
 
10917
 
 
10918
  stmt_text= "DROP TABLE t1";
 
10919
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10920
  myquery(rc);
 
10921
  stmt_text= "SET NAMES DEFAULT";
 
10922
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10923
  myquery(rc);
 
10924
}
 
10925
 
 
10926
 
 
10927
static void test_bug3796()
 
10928
{
 
10929
  MYSQL_STMT *stmt;
 
10930
  MYSQL_BIND my_bind[1];
 
10931
  const char *concat_arg0= "concat_with_";
 
10932
  enum { OUT_BUFF_SIZE= 30 };
 
10933
  char out_buff[OUT_BUFF_SIZE];
 
10934
  char canonical_buff[OUT_BUFF_SIZE];
 
10935
  ulong out_length;
 
10936
  const char *stmt_text;
 
10937
  int rc;
 
10938
 
 
10939
  myheader("test_bug3796");
 
10940
 
 
10941
  /* Create and fill test table */
 
10942
  stmt_text= "DROP TABLE IF EXISTS t1";
 
10943
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10944
  myquery(rc);
 
10945
 
 
10946
  stmt_text= "CREATE TABLE t1 (a INT, b VARCHAR(30))";
 
10947
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10948
  myquery(rc);
 
10949
 
 
10950
  stmt_text= "INSERT INTO t1 VALUES(1, 'ONE'), (2, 'TWO')";
 
10951
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10952
  myquery(rc);
 
10953
 
 
10954
  /* Create statement handle and prepare it with select */
 
10955
  stmt= mysql_stmt_init(mysql);
 
10956
  stmt_text= "SELECT concat(?, b) FROM t1";
 
10957
 
 
10958
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
10959
  check_execute(stmt, rc);
 
10960
 
 
10961
  /* Bind input buffers */
 
10962
  bzero((char*) my_bind, sizeof(my_bind));
 
10963
 
 
10964
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
10965
  my_bind[0].buffer= (void *) concat_arg0;
 
10966
  my_bind[0].buffer_length= strlen(concat_arg0);
 
10967
 
 
10968
  mysql_stmt_bind_param(stmt, my_bind);
 
10969
 
 
10970
  /* Execute the select statement */
 
10971
  rc= mysql_stmt_execute(stmt);
 
10972
  check_execute(stmt, rc);
 
10973
 
 
10974
  my_bind[0].buffer= (void *) out_buff;
 
10975
  my_bind[0].buffer_length= OUT_BUFF_SIZE;
 
10976
  my_bind[0].length= &out_length;
 
10977
 
 
10978
  mysql_stmt_bind_result(stmt, my_bind);
 
10979
 
 
10980
  rc= mysql_stmt_fetch(stmt);
 
10981
  if (!opt_silent)
 
10982
    printf("Concat result: '%s'\n", out_buff);
 
10983
  check_execute(stmt, rc);
 
10984
  strmov(canonical_buff, concat_arg0);
 
10985
  strcat(canonical_buff, "ONE");
 
10986
  DIE_UNLESS(strlen(canonical_buff) == out_length &&
 
10987
         strncmp(out_buff, canonical_buff, out_length) == 0);
 
10988
 
 
10989
  rc= mysql_stmt_fetch(stmt);
 
10990
  check_execute(stmt, rc);
 
10991
  strmov(canonical_buff + strlen(concat_arg0), "TWO");
 
10992
  DIE_UNLESS(strlen(canonical_buff) == out_length &&
 
10993
         strncmp(out_buff, canonical_buff, out_length) == 0);
 
10994
  if (!opt_silent)
 
10995
    printf("Concat result: '%s'\n", out_buff);
 
10996
 
 
10997
  rc= mysql_stmt_fetch(stmt);
 
10998
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
10999
 
 
11000
  mysql_stmt_close(stmt);
 
11001
 
 
11002
  stmt_text= "DROP TABLE IF EXISTS t1";
 
11003
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
11004
  myquery(rc);
 
11005
}
 
11006
 
 
11007
 
 
11008
static void test_bug4026()
 
11009
{
 
11010
  MYSQL_STMT *stmt;
 
11011
  MYSQL_BIND my_bind[2];
 
11012
  MYSQL_TIME time_in, time_out;
 
11013
  MYSQL_TIME datetime_in, datetime_out;
 
11014
  const char *stmt_text;
 
11015
  int rc;
 
11016
 
 
11017
  myheader("test_bug4026");
 
11018
 
 
11019
  /* Check that microseconds are inserted and selected successfully */
 
11020
 
 
11021
  /* Create a statement handle and prepare it with select */
 
11022
  stmt= mysql_stmt_init(mysql);
 
11023
  stmt_text= "SELECT ?, ?";
 
11024
 
 
11025
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
11026
  check_execute(stmt, rc);
 
11027
 
 
11028
  /* Bind input buffers */
 
11029
  bzero((char*) my_bind, sizeof(my_bind));
 
11030
  bzero((char*) &time_in, sizeof(time_in));
 
11031
  bzero((char*) &time_out, sizeof(time_out));
 
11032
  bzero((char*) &datetime_in, sizeof(datetime_in));
 
11033
  bzero((char*) &datetime_out, sizeof(datetime_out));
 
11034
 
 
11035
  my_bind[0].buffer_type= MYSQL_TYPE_TIME;
 
11036
  my_bind[0].buffer= (void *) &time_in;
 
11037
  my_bind[1].buffer_type= MYSQL_TYPE_DATETIME;
 
11038
  my_bind[1].buffer= (void *) &datetime_in;
 
11039
 
 
11040
  time_in.hour= 23;
 
11041
  time_in.minute= 59;
 
11042
  time_in.second= 59;
 
11043
  time_in.second_part= 123456;
 
11044
  /*
 
11045
    This is not necessary, just to make DIE_UNLESS below work: this field
 
11046
    is filled in when time is received from server
 
11047
  */
 
11048
  time_in.time_type= MYSQL_TIMESTAMP_TIME;
 
11049
 
 
11050
  datetime_in= time_in;
 
11051
  datetime_in.year= 2003;
 
11052
  datetime_in.month= 12;
 
11053
  datetime_in.day= 31;
 
11054
  datetime_in.time_type= MYSQL_TIMESTAMP_DATETIME;
 
11055
 
 
11056
  mysql_stmt_bind_param(stmt, my_bind);
 
11057
 
 
11058
  /* Execute the select statement */
 
11059
  rc= mysql_stmt_execute(stmt);
 
11060
  check_execute(stmt, rc);
 
11061
 
 
11062
  my_bind[0].buffer= (void *) &time_out;
 
11063
  my_bind[1].buffer= (void *) &datetime_out;
 
11064
 
 
11065
  mysql_stmt_bind_result(stmt, my_bind);
 
11066
 
 
11067
  rc= mysql_stmt_fetch(stmt);
 
11068
  DIE_UNLESS(rc == 0);
 
11069
  if (!opt_silent)
 
11070
  {
 
11071
    printf("%d:%d:%d.%lu\n", time_out.hour, time_out.minute, time_out.second,
 
11072
           time_out.second_part);
 
11073
    printf("%d-%d-%d %d:%d:%d.%lu\n", datetime_out.year, datetime_out.month,
 
11074
           datetime_out.day, datetime_out.hour,
 
11075
           datetime_out.minute, datetime_out.second,
 
11076
           datetime_out.second_part);
 
11077
  }
 
11078
  DIE_UNLESS(memcmp(&time_in, &time_out, sizeof(time_in)) == 0);
 
11079
  DIE_UNLESS(memcmp(&datetime_in, &datetime_out, sizeof(datetime_in)) == 0);
 
11080
  mysql_stmt_close(stmt);
 
11081
}
 
11082
 
 
11083
 
 
11084
static void test_bug4079()
 
11085
{
 
11086
  MYSQL_STMT *stmt;
 
11087
  MYSQL_BIND my_bind[1];
 
11088
  const char *stmt_text;
 
11089
  uint32 res;
 
11090
  int rc;
 
11091
 
 
11092
  myheader("test_bug4079");
 
11093
 
 
11094
  /* Create and fill table */
 
11095
  mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
11096
  mysql_query(mysql, "CREATE TABLE t1 (a int)");
 
11097
  mysql_query(mysql, "INSERT INTO t1 VALUES (1), (2)");
 
11098
 
 
11099
  /* Prepare erroneous statement */
 
11100
  stmt= mysql_stmt_init(mysql);
 
11101
  stmt_text= "SELECT 1 < (SELECT a FROM t1)";
 
11102
 
 
11103
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
11104
  check_execute(stmt, rc);
 
11105
 
 
11106
  /* Execute the select statement */
 
11107
  rc= mysql_stmt_execute(stmt);
 
11108
  check_execute(stmt, rc);
 
11109
 
 
11110
  /* Bind input buffers */
 
11111
  bzero((char*) my_bind, sizeof(my_bind));
 
11112
 
 
11113
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
11114
  my_bind[0].buffer= (void *) &res;
 
11115
 
 
11116
  mysql_stmt_bind_result(stmt, my_bind);
 
11117
 
 
11118
  rc= mysql_stmt_fetch(stmt);
 
11119
  DIE_UNLESS(rc != 0 && rc != MYSQL_NO_DATA);
 
11120
  if (!opt_silent)
 
11121
    printf("Got error from mysql_stmt_fetch (as expected):\n%s\n",
 
11122
           mysql_stmt_error(stmt));
 
11123
  /* buggy version of libmysql hanged up here */
 
11124
  mysql_stmt_close(stmt);
 
11125
}
 
11126
 
 
11127
 
 
11128
static void test_bug4236()
 
11129
{
 
11130
  MYSQL_STMT *stmt;
 
11131
  const char *stmt_text;
 
11132
  int rc;
 
11133
  MYSQL_STMT backup;
 
11134
 
 
11135
  myheader("test_bug4296");
 
11136
 
 
11137
  stmt= mysql_stmt_init(mysql);
 
11138
 
 
11139
  /* mysql_stmt_execute() of statement with statement id= 0 crashed server */
 
11140
  stmt_text= "SELECT 1";
 
11141
  /* We need to prepare statement to pass by possible check in libmysql */
 
11142
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
11143
  check_execute(stmt, rc);
 
11144
  /* Hack to check that server works OK if statement wasn't found */
 
11145
  backup.stmt_id= stmt->stmt_id;
 
11146
  stmt->stmt_id= 0;
 
11147
  rc= mysql_stmt_execute(stmt);
 
11148
  DIE_UNLESS(rc);
 
11149
  /* Restore original statement id to be able to reprepare it */
 
11150
  stmt->stmt_id= backup.stmt_id;
 
11151
 
 
11152
  mysql_stmt_close(stmt);
 
11153
}
 
11154
 
 
11155
 
 
11156
static void test_bug4030()
 
11157
{
 
11158
  MYSQL_STMT *stmt;
 
11159
  MYSQL_BIND my_bind[3];
 
11160
  MYSQL_TIME time_canonical, time_out;
 
11161
  MYSQL_TIME date_canonical, date_out;
 
11162
  MYSQL_TIME datetime_canonical, datetime_out;
 
11163
  const char *stmt_text;
 
11164
  int rc;
 
11165
 
 
11166
  myheader("test_bug4030");
 
11167
 
 
11168
  /* Check that microseconds are inserted and selected successfully */
 
11169
 
 
11170
  /* Execute a query with time values in prepared mode */
 
11171
  stmt= mysql_stmt_init(mysql);
 
11172
  stmt_text= "SELECT '23:59:59.123456', '2003-12-31', "
 
11173
             "'2003-12-31 23:59:59.123456'";
 
11174
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
11175
  check_execute(stmt, rc);
 
11176
  rc= mysql_stmt_execute(stmt);
 
11177
  check_execute(stmt, rc);
 
11178
 
 
11179
  /* Bind output buffers */
 
11180
  bzero((char*) my_bind, sizeof(my_bind));
 
11181
  bzero((char*) &time_canonical, sizeof(time_canonical));
 
11182
  bzero((char*) &time_out, sizeof(time_out));
 
11183
  bzero((char*) &date_canonical, sizeof(date_canonical));
 
11184
  bzero((char*) &date_out, sizeof(date_out));
 
11185
  bzero((char*) &datetime_canonical, sizeof(datetime_canonical));
 
11186
  bzero((char*) &datetime_out, sizeof(datetime_out));
 
11187
 
 
11188
  my_bind[0].buffer_type= MYSQL_TYPE_TIME;
 
11189
  my_bind[0].buffer= (void *) &time_out;
 
11190
  my_bind[1].buffer_type= MYSQL_TYPE_DATE;
 
11191
  my_bind[1].buffer= (void *) &date_out;
 
11192
  my_bind[2].buffer_type= MYSQL_TYPE_DATETIME;
 
11193
  my_bind[2].buffer= (void *) &datetime_out;
 
11194
 
 
11195
  time_canonical.hour= 23;
 
11196
  time_canonical.minute= 59;
 
11197
  time_canonical.second= 59;
 
11198
  time_canonical.second_part= 123456;
 
11199
  time_canonical.time_type= MYSQL_TIMESTAMP_TIME;
 
11200
 
 
11201
  date_canonical.year= 2003;
 
11202
  date_canonical.month= 12;
 
11203
  date_canonical.day= 31;
 
11204
  date_canonical.time_type= MYSQL_TIMESTAMP_DATE;
 
11205
 
 
11206
  datetime_canonical= time_canonical;
 
11207
  datetime_canonical.year= 2003;
 
11208
  datetime_canonical.month= 12;
 
11209
  datetime_canonical.day= 31;
 
11210
  datetime_canonical.time_type= MYSQL_TIMESTAMP_DATETIME;
 
11211
 
 
11212
  mysql_stmt_bind_result(stmt, my_bind);
 
11213
 
 
11214
  rc= mysql_stmt_fetch(stmt);
 
11215
  DIE_UNLESS(rc == 0);
 
11216
  if (!opt_silent)
 
11217
  {
 
11218
    printf("%d:%d:%d.%lu\n", time_out.hour, time_out.minute, time_out.second,
 
11219
           time_out.second_part);
 
11220
    printf("%d-%d-%d\n", date_out.year, date_out.month, date_out.day);
 
11221
    printf("%d-%d-%d %d:%d:%d.%lu\n", datetime_out.year, datetime_out.month,
 
11222
           datetime_out.day, datetime_out.hour,
 
11223
           datetime_out.minute, datetime_out.second,
 
11224
           datetime_out.second_part);
 
11225
  }
 
11226
  DIE_UNLESS(memcmp(&time_canonical, &time_out, sizeof(time_out)) == 0);
 
11227
  DIE_UNLESS(memcmp(&date_canonical, &date_out, sizeof(date_out)) == 0);
 
11228
  DIE_UNLESS(memcmp(&datetime_canonical, &datetime_out, sizeof(datetime_out)) == 0);
 
11229
  mysql_stmt_close(stmt);
 
11230
}
 
11231
 
 
11232
static void test_view()
 
11233
{
 
11234
  MYSQL_STMT *stmt;
 
11235
  int rc, i;
 
11236
  MYSQL_BIND      my_bind[1];
 
11237
  char            str_data[50];
 
11238
  ulong           length = 0L;
 
11239
  long            is_null = 0L;
 
11240
  const char *query=
 
11241
    "SELECT COUNT(*) FROM v1 WHERE SERVERNAME=?";
 
11242
 
 
11243
  myheader("test_view");
 
11244
 
 
11245
  rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,t2,t3,v1");
 
11246
  myquery(rc);
 
11247
 
 
11248
  rc = mysql_query(mysql, "DROP VIEW IF EXISTS v1,t1,t2,t3");
 
11249
  myquery(rc);
 
11250
  rc= mysql_query(mysql,"CREATE TABLE t1 ("
 
11251
                        " SERVERGRP varchar(20) NOT NULL default '', "
 
11252
                        " DBINSTANCE varchar(20) NOT NULL default '', "
 
11253
                        " PRIMARY KEY  (SERVERGRP)) "
 
11254
                        " CHARSET=latin1 collate=latin1_bin");
 
11255
  myquery(rc);
 
11256
  rc= mysql_query(mysql,"CREATE TABLE t2 ("
 
11257
                        " SERVERNAME varchar(20) NOT NULL, "
 
11258
                        " SERVERGRP varchar(20) NOT NULL, "
 
11259
                        " PRIMARY KEY (SERVERNAME)) "
 
11260
                        " CHARSET=latin1 COLLATE latin1_bin");
 
11261
  myquery(rc);
 
11262
  rc= mysql_query(mysql,
 
11263
                  "CREATE TABLE t3 ("
 
11264
                  " SERVERGRP varchar(20) BINARY NOT NULL, "
 
11265
                  " TABNAME varchar(30) NOT NULL, MAPSTATE char(1) NOT NULL, "
 
11266
                  " ACTSTATE char(1) NOT NULL , "
 
11267
                  " LOCAL_NAME varchar(30) NOT NULL, "
 
11268
                  " CHG_DATE varchar(8) NOT NULL default '00000000', "
 
11269
                  " CHG_TIME varchar(6) NOT NULL default '000000', "
 
11270
                  " MXUSER varchar(12) NOT NULL default '', "
 
11271
                  " PRIMARY KEY (SERVERGRP, TABNAME, MAPSTATE, ACTSTATE, "
 
11272
                  " LOCAL_NAME)) CHARSET=latin1 COLLATE latin1_bin");
 
11273
  myquery(rc);
 
11274
  rc= mysql_query(mysql,"CREATE VIEW v1 AS select sql_no_cache"
 
11275
                  " T0001.SERVERNAME AS SERVERNAME, T0003.TABNAME AS"
 
11276
                  " TABNAME,T0003.LOCAL_NAME AS LOCAL_NAME,T0002.DBINSTANCE AS"
 
11277
                  " DBINSTANCE from t2 T0001 join t1 T0002 join t3 T0003 where"
 
11278
                  " ((T0002.SERVERGRP = T0001.SERVERGRP) and"
 
11279
                  " (T0002.SERVERGRP = T0003.SERVERGRP)"
 
11280
                  " and (T0003.MAPSTATE = _latin1'A') and"
 
11281
                  " (T0003.ACTSTATE = _latin1' '))");
 
11282
  myquery(rc);
 
11283
 
 
11284
  stmt= mysql_stmt_init(mysql);
 
11285
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
11286
  check_execute(stmt, rc);
 
11287
 
 
11288
  strmov(str_data, "TEST");
 
11289
  bzero((char*) my_bind, sizeof(my_bind));
 
11290
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
11291
  my_bind[0].buffer= (char *)&str_data;
 
11292
  my_bind[0].buffer_length= 50;
 
11293
  my_bind[0].length= &length;
 
11294
  length= 4;
 
11295
  my_bind[0].is_null= (char*)&is_null;
 
11296
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
11297
  check_execute(stmt,rc);
 
11298
 
 
11299
  for (i= 0; i < 3; i++)
 
11300
  {
 
11301
    rc= mysql_stmt_execute(stmt);
 
11302
    check_execute(stmt, rc);
 
11303
    rc= my_process_stmt_result(stmt);
 
11304
    DIE_UNLESS(1 == rc);
 
11305
  }
 
11306
  mysql_stmt_close(stmt);
 
11307
 
 
11308
  rc= mysql_query(mysql, "DROP TABLE t1,t2,t3");
 
11309
  myquery(rc);
 
11310
  rc= mysql_query(mysql, "DROP VIEW v1");
 
11311
  myquery(rc);
 
11312
}
 
11313
 
 
11314
 
 
11315
static void test_view_where()
 
11316
{
 
11317
  MYSQL_STMT *stmt;
 
11318
  int rc, i;
 
11319
  const char *query=
 
11320
    "select v1.c,v2.c from v1, v2";
 
11321
 
 
11322
  myheader("test_view_where");
 
11323
 
 
11324
  rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,v1,v2");
 
11325
  myquery(rc);
 
11326
 
 
11327
  rc = mysql_query(mysql, "DROP VIEW IF EXISTS v1,v2,t1");
 
11328
  myquery(rc);
 
11329
  rc= mysql_query(mysql,"CREATE TABLE t1 (a int, b int)");
 
11330
  myquery(rc);
 
11331
  rc= mysql_query(mysql,"insert into t1 values (1,2), (1,3), (2,4), (2,5), (3,10)");
 
11332
  myquery(rc);
 
11333
  rc= mysql_query(mysql,"create view v1 (c) as select b from t1 where a<3");
 
11334
  myquery(rc);
 
11335
  rc= mysql_query(mysql,"create view v2 (c) as select b from t1 where a>=3");
 
11336
  myquery(rc);
 
11337
 
 
11338
  stmt= mysql_stmt_init(mysql);
 
11339
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
11340
  check_execute(stmt, rc);
 
11341
 
 
11342
  for (i= 0; i < 3; i++)
 
11343
  {
 
11344
    rc= mysql_stmt_execute(stmt);
 
11345
    check_execute(stmt, rc);
 
11346
    rc= my_process_stmt_result(stmt);
 
11347
    DIE_UNLESS(4 == rc);
 
11348
  }
 
11349
  mysql_stmt_close(stmt);
 
11350
 
 
11351
  rc= mysql_query(mysql, "DROP TABLE t1");
 
11352
  myquery(rc);
 
11353
  rc= mysql_query(mysql, "DROP VIEW v1, v2");
 
11354
  myquery(rc);
 
11355
}
 
11356
 
 
11357
 
 
11358
static void test_view_2where()
 
11359
{
 
11360
  MYSQL_STMT *stmt;
 
11361
  int rc, i;
 
11362
  MYSQL_BIND      my_bind[8];
 
11363
  char            parms[8][100];
 
11364
  ulong           length[8];
 
11365
  const char *query=
 
11366
    "select relid, report, handle, log_group, username, variant, type, "
 
11367
    "version, erfdat, erftime, erfname, aedat, aetime, aename, dependvars, "
 
11368
    "inactive from V_LTDX where mandt = ? and relid = ? and report = ? and "
 
11369
    "handle = ? and log_group = ? and username in ( ? , ? ) and type = ?";
 
11370
 
 
11371
  myheader("test_view_2where");
 
11372
 
 
11373
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS LTDX");
 
11374
  myquery(rc);
 
11375
  rc= mysql_query(mysql, "DROP VIEW IF EXISTS V_LTDX");
 
11376
  myquery(rc);
 
11377
  rc= mysql_query(mysql,
 
11378
                  "CREATE TABLE LTDX (MANDT char(3) NOT NULL default '000', "
 
11379
                  " RELID char(2) NOT NULL, REPORT varchar(40) NOT NULL,"
 
11380
                  " HANDLE varchar(4) NOT NULL, LOG_GROUP varchar(4) NOT NULL,"
 
11381
                  " USERNAME varchar(12) NOT NULL,"
 
11382
                  " VARIANT varchar(12) NOT NULL,"
 
11383
                  " TYPE char(1) NOT NULL, SRTF2 int(11) NOT NULL,"
 
11384
                  " VERSION varchar(6) NOT NULL default '000000',"
 
11385
                  " ERFDAT varchar(8) NOT NULL default '00000000',"
 
11386
                  " ERFTIME varchar(6) NOT NULL default '000000',"
 
11387
                  " ERFNAME varchar(12) NOT NULL,"
 
11388
                  " AEDAT varchar(8) NOT NULL default '00000000',"
 
11389
                  " AETIME varchar(6) NOT NULL default '000000',"
 
11390
                  " AENAME varchar(12) NOT NULL,"
 
11391
                  " DEPENDVARS varchar(10) NOT NULL,"
 
11392
                  " INACTIVE char(1) NOT NULL, CLUSTR smallint(6) NOT NULL,"
 
11393
                  " CLUSTD blob,"
 
11394
                  " PRIMARY KEY (MANDT, RELID, REPORT, HANDLE, LOG_GROUP, "
 
11395
                                "USERNAME, VARIANT, TYPE, SRTF2))"
 
11396
                 " CHARSET=latin1 COLLATE latin1_bin");
 
11397
  myquery(rc);
 
11398
  rc= mysql_query(mysql,
 
11399
                  "CREATE VIEW V_LTDX AS select T0001.MANDT AS "
 
11400
                  " MANDT,T0001.RELID AS RELID,T0001.REPORT AS "
 
11401
                  " REPORT,T0001.HANDLE AS HANDLE,T0001.LOG_GROUP AS "
 
11402
                  " LOG_GROUP,T0001.USERNAME AS USERNAME,T0001.VARIANT AS "
 
11403
                  " VARIANT,T0001.TYPE AS TYPE,T0001.VERSION AS "
 
11404
                  " VERSION,T0001.ERFDAT AS ERFDAT,T0001.ERFTIME AS "
 
11405
                  " ERFTIME,T0001.ERFNAME AS ERFNAME,T0001.AEDAT AS "
 
11406
                  " AEDAT,T0001.AETIME AS AETIME,T0001.AENAME AS "
 
11407
                  " AENAME,T0001.DEPENDVARS AS DEPENDVARS,T0001.INACTIVE AS "
 
11408
                  " INACTIVE from LTDX T0001 where (T0001.SRTF2 = 0)");
 
11409
  myquery(rc);
 
11410
  bzero((char*) my_bind, sizeof(my_bind));
 
11411
  for (i=0; i < 8; i++) {
 
11412
    strmov(parms[i], "1");
 
11413
    my_bind[i].buffer_type = MYSQL_TYPE_VAR_STRING;
 
11414
    my_bind[i].buffer = (char *)&parms[i];
 
11415
    my_bind[i].buffer_length = 100;
 
11416
    my_bind[i].is_null = 0;
 
11417
    my_bind[i].length = &length[i];
 
11418
    length[i] = 1;
 
11419
  }
 
11420
  stmt= mysql_stmt_init(mysql);
 
11421
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
11422
  check_execute(stmt, rc);
 
11423
 
 
11424
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
11425
  check_execute(stmt,rc);
 
11426
 
 
11427
  rc= mysql_stmt_execute(stmt);
 
11428
  check_execute(stmt, rc);
 
11429
  rc= my_process_stmt_result(stmt);
 
11430
  DIE_UNLESS(0 == rc);
 
11431
 
 
11432
  mysql_stmt_close(stmt);
 
11433
 
 
11434
  rc= mysql_query(mysql, "DROP VIEW V_LTDX");
 
11435
  myquery(rc);
 
11436
  rc= mysql_query(mysql, "DROP TABLE LTDX");
 
11437
  myquery(rc);
 
11438
}
 
11439
 
 
11440
 
 
11441
static void test_view_star()
 
11442
{
 
11443
  MYSQL_STMT *stmt;
 
11444
  int rc, i;
 
11445
  MYSQL_BIND      my_bind[8];
 
11446
  char            parms[8][100];
 
11447
  ulong           length[8];
 
11448
  const char *query= "SELECT * FROM vt1 WHERE a IN (?,?)";
 
11449
 
 
11450
  myheader("test_view_star");
 
11451
 
 
11452
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, vt1");
 
11453
  myquery(rc);
 
11454
  rc= mysql_query(mysql, "DROP VIEW IF EXISTS t1, vt1");
 
11455
  myquery(rc);
 
11456
  rc= mysql_query(mysql, "CREATE TABLE t1 (a int)");
 
11457
  myquery(rc);
 
11458
  rc= mysql_query(mysql, "CREATE VIEW vt1 AS SELECT a FROM t1");
 
11459
  myquery(rc);
 
11460
  bzero((char*) my_bind, sizeof(my_bind));
 
11461
  for (i= 0; i < 2; i++) {
 
11462
    sprintf((char *)&parms[i], "%d", i);
 
11463
    my_bind[i].buffer_type = MYSQL_TYPE_VAR_STRING;
 
11464
    my_bind[i].buffer = (char *)&parms[i];
 
11465
    my_bind[i].buffer_length = 100;
 
11466
    my_bind[i].is_null = 0;
 
11467
    my_bind[i].length = &length[i];
 
11468
    length[i] = 1;
 
11469
  }
 
11470
 
 
11471
  stmt= mysql_stmt_init(mysql);
 
11472
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
11473
  check_execute(stmt, rc);
 
11474
 
 
11475
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
11476
  check_execute(stmt,rc);
 
11477
 
 
11478
  for (i= 0; i < 3; i++)
 
11479
  {
 
11480
    rc= mysql_stmt_execute(stmt);
 
11481
    check_execute(stmt, rc);
 
11482
    rc= my_process_stmt_result(stmt);
 
11483
    DIE_UNLESS(0 == rc);
 
11484
  }
 
11485
 
 
11486
  mysql_stmt_close(stmt);
 
11487
 
 
11488
  rc= mysql_query(mysql, "DROP TABLE t1");
 
11489
  myquery(rc);
 
11490
  rc= mysql_query(mysql, "DROP VIEW vt1");
 
11491
  myquery(rc);
 
11492
}
 
11493
 
 
11494
 
 
11495
static void test_view_insert()
 
11496
{
 
11497
  MYSQL_STMT *insert_stmt, *select_stmt;
 
11498
  int rc, i;
 
11499
  MYSQL_BIND      my_bind[1];
 
11500
  int             my_val = 0;
 
11501
  ulong           my_length = 0L;
 
11502
  long            my_null = 0L;
 
11503
  const char *query=
 
11504
    "insert into v1 values (?)";
 
11505
 
 
11506
  myheader("test_view_insert");
 
11507
 
 
11508
  rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,v1");
 
11509
  myquery(rc);
 
11510
  rc = mysql_query(mysql, "DROP VIEW IF EXISTS t1,v1");
 
11511
  myquery(rc);
 
11512
 
 
11513
  rc= mysql_query(mysql,"create table t1 (a int, primary key (a))");
 
11514
  myquery(rc);
 
11515
 
 
11516
  rc= mysql_query(mysql, "create view v1 as select a from t1 where a>=1");
 
11517
  myquery(rc);
 
11518
 
 
11519
  insert_stmt= mysql_stmt_init(mysql);
 
11520
  rc= mysql_stmt_prepare(insert_stmt, query, strlen(query));
 
11521
  check_execute(insert_stmt, rc);
 
11522
  query= "select * from t1";
 
11523
  select_stmt= mysql_stmt_init(mysql);
 
11524
  rc= mysql_stmt_prepare(select_stmt, query, strlen(query));
 
11525
  check_execute(select_stmt, rc);
 
11526
 
 
11527
  bzero((char*) my_bind, sizeof(my_bind));
 
11528
  my_bind[0].buffer_type = MYSQL_TYPE_LONG;
 
11529
  my_bind[0].buffer = (char *)&my_val;
 
11530
  my_bind[0].length = &my_length;
 
11531
  my_bind[0].is_null = (char*)&my_null;
 
11532
  rc= mysql_stmt_bind_param(insert_stmt, my_bind);
 
11533
  check_execute(insert_stmt, rc);
 
11534
 
 
11535
  for (i= 0; i < 3; i++)
 
11536
  {
 
11537
    int rowcount= 0;
 
11538
    my_val= i;
 
11539
 
 
11540
    rc= mysql_stmt_execute(insert_stmt);
 
11541
    check_execute(insert_stmt, rc);
 
11542
 
 
11543
    rc= mysql_stmt_execute(select_stmt);
 
11544
    check_execute(select_stmt, rc);
 
11545
    rowcount= (int)my_process_stmt_result(select_stmt);
 
11546
    DIE_UNLESS((i+1) == rowcount);
 
11547
  }
 
11548
  mysql_stmt_close(insert_stmt);
 
11549
  mysql_stmt_close(select_stmt);
 
11550
 
 
11551
  rc= mysql_query(mysql, "DROP VIEW v1");
 
11552
  myquery(rc);
 
11553
  rc= mysql_query(mysql, "DROP TABLE t1");
 
11554
  myquery(rc);
 
11555
}
 
11556
 
 
11557
 
 
11558
static void test_left_join_view()
 
11559
{
 
11560
  MYSQL_STMT *stmt;
 
11561
  int rc, i;
 
11562
  const char *query=
 
11563
    "select t1.a, v1.x from t1 left join v1 on (t1.a= v1.x);";
 
11564
 
 
11565
  myheader("test_left_join_view");
 
11566
 
 
11567
  rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,v1");
 
11568
  myquery(rc);
 
11569
 
 
11570
  rc = mysql_query(mysql, "DROP VIEW IF EXISTS v1,t1");
 
11571
  myquery(rc);
 
11572
  rc= mysql_query(mysql,"CREATE TABLE t1 (a int)");
 
11573
  myquery(rc);
 
11574
  rc= mysql_query(mysql,"insert into t1 values (1), (2), (3)");
 
11575
  myquery(rc);
 
11576
  rc= mysql_query(mysql,"create view v1 (x) as select a from t1 where a > 1");
 
11577
  myquery(rc);
 
11578
  stmt= mysql_stmt_init(mysql);
 
11579
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
11580
  check_execute(stmt, rc);
 
11581
 
 
11582
  for (i= 0; i < 3; i++)
 
11583
  {
 
11584
    rc= mysql_stmt_execute(stmt);
 
11585
    check_execute(stmt, rc);
 
11586
    rc= my_process_stmt_result(stmt);
 
11587
    DIE_UNLESS(3 == rc);
 
11588
  }
 
11589
  mysql_stmt_close(stmt);
 
11590
 
 
11591
  rc= mysql_query(mysql, "DROP VIEW v1");
 
11592
  myquery(rc);
 
11593
  rc= mysql_query(mysql, "DROP TABLE t1");
 
11594
  myquery(rc);
 
11595
}
 
11596
 
 
11597
 
 
11598
static void test_view_insert_fields()
 
11599
{
 
11600
  MYSQL_STMT    *stmt;
 
11601
  char          parm[11][1000];
 
11602
  ulong         l[11];
 
11603
  int           rc, i;
 
11604
  MYSQL_BIND    my_bind[11];
 
11605
  const char    *query= "INSERT INTO `v1` ( `K1C4` ,`K2C4` ,`K3C4` ,`K4N4` ,`F1C4` ,`F2I4` ,`F3N5` ,`F7F8` ,`F6N4` ,`F5C8` ,`F9D8` ) VALUES( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? )";
 
11606
 
 
11607
  myheader("test_view_insert_fields");
 
11608
 
 
11609
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, v1");
 
11610
  myquery(rc);
 
11611
  rc= mysql_query(mysql, "DROP VIEW IF EXISTS t1, v1");
 
11612
  myquery(rc);
 
11613
  rc= mysql_query(mysql,
 
11614
                  "CREATE TABLE t1 (K1C4 varchar(4) NOT NULL,"
 
11615
                  "K2C4 varchar(4) NOT NULL, K3C4 varchar(4) NOT NULL,"
 
11616
                  "K4N4 varchar(4) NOT NULL default '0000',"
 
11617
                  "F1C4 varchar(4) NOT NULL, F2I4 int(11) NOT NULL,"
 
11618
                  "F3N5 varchar(5) NOT NULL default '00000',"
 
11619
                  "F4I4 int(11) NOT NULL default '0', F5C8 varchar(8) NOT NULL,"
 
11620
                  "F6N4 varchar(4) NOT NULL default '0000',"
 
11621
                  "F7F8 double NOT NULL default '0',"
 
11622
                  "F8F8 double NOT NULL default '0',"
 
11623
                  "F9D8 decimal(8,2) NOT NULL default '0.00',"
 
11624
                  "PRIMARY KEY (K1C4,K2C4,K3C4,K4N4)) "
 
11625
                  "CHARSET=latin1 COLLATE latin1_bin");
 
11626
  myquery(rc);
 
11627
  rc= mysql_query(mysql,
 
11628
                  "CREATE VIEW v1 AS select sql_no_cache "
 
11629
                  " K1C4 AS K1C4, K2C4 AS K2C4, K3C4 AS K3C4, K4N4 AS K4N4, "
 
11630
                  " F1C4 AS F1C4, F2I4 AS F2I4, F3N5 AS F3N5,"
 
11631
                  " F7F8 AS F7F8, F6N4 AS F6N4, F5C8 AS F5C8, F9D8 AS F9D8"
 
11632
                  " from t1 T0001");
 
11633
 
 
11634
  bzero((char*) my_bind, sizeof(my_bind));
 
11635
  for (i= 0; i < 11; i++)
 
11636
  {
 
11637
    l[i]= 20;
 
11638
    my_bind[i].buffer_type= MYSQL_TYPE_STRING;
 
11639
    my_bind[i].is_null= 0;
 
11640
    my_bind[i].buffer= (char *)&parm[i];
 
11641
 
 
11642
    strmov(parm[i], "1");
 
11643
    my_bind[i].buffer_length= 2;
 
11644
    my_bind[i].length= &l[i];
 
11645
  }
 
11646
  stmt= mysql_stmt_init(mysql);
 
11647
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
11648
  check_execute(stmt, rc);
 
11649
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
11650
  check_execute(stmt, rc);
 
11651
 
 
11652
  rc= mysql_stmt_execute(stmt);
 
11653
  check_execute(stmt, rc);
 
11654
  mysql_stmt_close(stmt);
 
11655
 
 
11656
  query= "select * from t1";
 
11657
  stmt= mysql_stmt_init(mysql);
 
11658
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
11659
  check_execute(stmt, rc);
 
11660
  rc= mysql_stmt_execute(stmt);
 
11661
  check_execute(stmt, rc);
 
11662
  rc= my_process_stmt_result(stmt);
 
11663
  DIE_UNLESS(1 == rc);
 
11664
 
 
11665
  mysql_stmt_close(stmt);
 
11666
  rc= mysql_query(mysql, "DROP VIEW v1");
 
11667
  myquery(rc);
 
11668
  rc= mysql_query(mysql, "DROP TABLE t1");
 
11669
  myquery(rc);
 
11670
 
 
11671
}
 
11672
 
 
11673
static void test_bug5126()
 
11674
{
 
11675
  MYSQL_STMT *stmt;
 
11676
  MYSQL_BIND my_bind[2];
 
11677
  int32 c1, c2;
 
11678
  const char *stmt_text;
 
11679
  int rc;
 
11680
 
 
11681
  myheader("test_bug5126");
 
11682
 
 
11683
  stmt_text= "DROP TABLE IF EXISTS t1";
 
11684
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
11685
  myquery(rc);
 
11686
 
 
11687
  stmt_text= "CREATE TABLE t1 (a mediumint, b int)";
 
11688
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
11689
  myquery(rc);
 
11690
 
 
11691
  stmt_text= "INSERT INTO t1 VALUES (8386608, 1)";
 
11692
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
11693
  myquery(rc);
 
11694
 
 
11695
  stmt= mysql_stmt_init(mysql);
 
11696
  stmt_text= "SELECT a, b FROM t1";
 
11697
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
11698
  check_execute(stmt, rc);
 
11699
  rc= mysql_stmt_execute(stmt);
 
11700
  check_execute(stmt, rc);
 
11701
 
 
11702
  /* Bind output buffers */
 
11703
  bzero((char*) my_bind, sizeof(my_bind));
 
11704
 
 
11705
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
11706
  my_bind[0].buffer= &c1;
 
11707
  my_bind[1].buffer_type= MYSQL_TYPE_LONG;
 
11708
  my_bind[1].buffer= &c2;
 
11709
 
 
11710
  mysql_stmt_bind_result(stmt, my_bind);
 
11711
 
 
11712
  rc= mysql_stmt_fetch(stmt);
 
11713
  DIE_UNLESS(rc == 0);
 
11714
  DIE_UNLESS(c1 == 8386608 && c2 == 1);
 
11715
  if (!opt_silent)
 
11716
    printf("%ld, %ld\n", (long) c1, (long) c2);
 
11717
  mysql_stmt_close(stmt);
 
11718
}
 
11719
 
 
11720
 
 
11721
static void test_bug4231()
 
11722
{
 
11723
  MYSQL_STMT *stmt;
 
11724
  MYSQL_BIND my_bind[2];
 
11725
  MYSQL_TIME tm[2];
 
11726
  const char *stmt_text;
 
11727
  int rc;
 
11728
 
 
11729
  myheader("test_bug4231");
 
11730
 
 
11731
  stmt_text= "DROP TABLE IF EXISTS t1";
 
11732
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
11733
  myquery(rc);
 
11734
 
 
11735
  stmt_text= "CREATE TABLE t1 (a int)";
 
11736
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
11737
  myquery(rc);
 
11738
 
 
11739
  stmt_text= "INSERT INTO t1 VALUES (1)";
 
11740
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
11741
  myquery(rc);
 
11742
 
 
11743
  stmt= mysql_stmt_init(mysql);
 
11744
  stmt_text= "SELECT a FROM t1 WHERE ? = ?";
 
11745
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
11746
  check_execute(stmt, rc);
 
11747
 
 
11748
  /* Bind input buffers */
 
11749
  bzero((char*) my_bind, sizeof(my_bind));
 
11750
  bzero((char*) tm, sizeof(tm));
 
11751
 
 
11752
  my_bind[0].buffer_type= MYSQL_TYPE_DATE;
 
11753
  my_bind[0].buffer= &tm[0];
 
11754
  my_bind[1].buffer_type= MYSQL_TYPE_DATE;
 
11755
  my_bind[1].buffer= &tm[1];
 
11756
 
 
11757
  mysql_stmt_bind_param(stmt, my_bind);
 
11758
  check_execute(stmt, rc);
 
11759
 
 
11760
  /*
 
11761
    First set server-side params to some non-zero non-equal values:
 
11762
    then we will check that they are not used when client sends
 
11763
    new (zero) times.
 
11764
  */
 
11765
  tm[0].time_type = MYSQL_TIMESTAMP_DATE;
 
11766
  tm[0].year = 2000;
 
11767
  tm[0].month = 1;
 
11768
  tm[0].day = 1;
 
11769
  tm[1]= tm[0];
 
11770
  --tm[1].year;                                 /* tm[0] != tm[1] */
 
11771
 
 
11772
  rc= mysql_stmt_execute(stmt);
 
11773
  check_execute(stmt, rc);
 
11774
 
 
11775
  rc= mysql_stmt_fetch(stmt);
 
11776
 
 
11777
  /* binds are unequal, no rows should be returned */
 
11778
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
11779
 
 
11780
  /* Set one of the dates to zero */
 
11781
  tm[0].year= tm[0].month= tm[0].day= 0;
 
11782
  tm[1]= tm[0];
 
11783
  mysql_stmt_execute(stmt);
 
11784
  rc= mysql_stmt_fetch(stmt);
 
11785
  DIE_UNLESS(rc == 0);
 
11786
 
 
11787
  mysql_stmt_close(stmt);
 
11788
  stmt_text= "DROP TABLE t1";
 
11789
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
11790
  myquery(rc);
 
11791
}
 
11792
 
 
11793
 
 
11794
static void test_bug5399()
 
11795
{
 
11796
  /*
 
11797
    Ascii 97 is 'a', which gets mapped to Ascii 65 'A' unless internal
 
11798
    statement id hash in the server uses binary collation.
 
11799
  */
 
11800
#define NUM_OF_USED_STMT 97 
 
11801
  MYSQL_STMT *stmt_list[NUM_OF_USED_STMT];
 
11802
  MYSQL_STMT **stmt;
 
11803
  MYSQL_BIND my_bind[1];
 
11804
  char buff[600];
 
11805
  int rc;
 
11806
  int32 no;
 
11807
 
 
11808
  myheader("test_bug5399");
 
11809
 
 
11810
  bzero((char*) my_bind, sizeof(my_bind));
 
11811
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
11812
  my_bind[0].buffer= &no;
 
11813
 
 
11814
  for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
 
11815
  {
 
11816
    sprintf(buff, "select %d", (int) (stmt - stmt_list));
 
11817
    *stmt= mysql_stmt_init(mysql);
 
11818
    rc= mysql_stmt_prepare(*stmt, buff, strlen(buff));
 
11819
    check_execute(*stmt, rc);
 
11820
    mysql_stmt_bind_result(*stmt, my_bind);
 
11821
  }
 
11822
  if (!opt_silent)
 
11823
    printf("%d statements prepared.\n", NUM_OF_USED_STMT);
 
11824
 
 
11825
  for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
 
11826
  {
 
11827
    rc= mysql_stmt_execute(*stmt);
 
11828
    check_execute(*stmt, rc);
 
11829
    rc= mysql_stmt_store_result(*stmt);
 
11830
    check_execute(*stmt, rc);
 
11831
    rc= mysql_stmt_fetch(*stmt);
 
11832
    DIE_UNLESS(rc == 0);
 
11833
    DIE_UNLESS((int32) (stmt - stmt_list) == no);
 
11834
  }
 
11835
 
 
11836
  for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
 
11837
    mysql_stmt_close(*stmt);
 
11838
#undef NUM_OF_USED_STMT
 
11839
}
 
11840
 
 
11841
 
 
11842
static void test_bug5194()
 
11843
{
 
11844
  MYSQL_STMT *stmt;
 
11845
  MYSQL_BIND *my_bind;
 
11846
  char *query;
 
11847
  char *param_str;
 
11848
  int param_str_length;
 
11849
  const char *stmt_text;
 
11850
  int rc;
 
11851
  float float_array[250] =
 
11852
  {
 
11853
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11854
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11855
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11856
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11857
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11858
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11859
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11860
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11861
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11862
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11863
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11864
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11865
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11866
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
 
11867
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
 
11868
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
 
11869
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
 
11870
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
 
11871
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
 
11872
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
 
11873
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
 
11874
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
 
11875
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
 
11876
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
 
11877
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25
 
11878
  };
 
11879
  float *fa_ptr= float_array;
 
11880
  /* Number of columns per row */
 
11881
  const int COLUMN_COUNT= sizeof(float_array)/sizeof(*float_array);
 
11882
  /* Number of rows per bulk insert to start with */
 
11883
  const int MIN_ROWS_PER_INSERT= 262;
 
11884
  /* Max number of rows per bulk insert to end with */
 
11885
  const int MAX_ROWS_PER_INSERT= 300;
 
11886
  const int MAX_PARAM_COUNT= COLUMN_COUNT*MAX_ROWS_PER_INSERT;
 
11887
  const char *query_template= "insert into t1 values %s";
 
11888
  const int CHARS_PER_PARAM= 5; /* space needed to place ", ?" in the query */
 
11889
  const int uint16_max= 65535;
 
11890
  int nrows, i;
 
11891
 
 
11892
  myheader("test_bug5194");
 
11893
 
 
11894
  stmt_text= "drop table if exists t1";
 
11895
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
11896
 
 
11897
  stmt_text= "create table if not exists t1"
 
11898
   "(c1 float, c2 float, c3 float, c4 float, c5 float, c6 float, "
 
11899
   "c7 float, c8 float, c9 float, c10 float, c11 float, c12 float, "
 
11900
   "c13 float, c14 float, c15 float, c16 float, c17 float, c18 float, "
 
11901
   "c19 float, c20 float, c21 float, c22 float, c23 float, c24 float, "
 
11902
   "c25 float, c26 float, c27 float, c28 float, c29 float, c30 float, "
 
11903
   "c31 float, c32 float, c33 float, c34 float, c35 float, c36 float, "
 
11904
   "c37 float, c38 float, c39 float, c40 float, c41 float, c42 float, "
 
11905
   "c43 float, c44 float, c45 float, c46 float, c47 float, c48 float, "
 
11906
   "c49 float, c50 float, c51 float, c52 float, c53 float, c54 float, "
 
11907
   "c55 float, c56 float, c57 float, c58 float, c59 float, c60 float, "
 
11908
   "c61 float, c62 float, c63 float, c64 float, c65 float, c66 float, "
 
11909
   "c67 float, c68 float, c69 float, c70 float, c71 float, c72 float, "
 
11910
   "c73 float, c74 float, c75 float, c76 float, c77 float, c78 float, "
 
11911
   "c79 float, c80 float, c81 float, c82 float, c83 float, c84 float, "
 
11912
   "c85 float, c86 float, c87 float, c88 float, c89 float, c90 float, "
 
11913
   "c91 float, c92 float, c93 float, c94 float, c95 float, c96 float, "
 
11914
   "c97 float, c98 float, c99 float, c100 float, c101 float, c102 float, "
 
11915
   "c103 float, c104 float, c105 float, c106 float, c107 float, c108 float, "
 
11916
   "c109 float, c110 float, c111 float, c112 float, c113 float, c114 float, "
 
11917
   "c115 float, c116 float, c117 float, c118 float, c119 float, c120 float, "
 
11918
   "c121 float, c122 float, c123 float, c124 float, c125 float, c126 float, "
 
11919
   "c127 float, c128 float, c129 float, c130 float, c131 float, c132 float, "
 
11920
   "c133 float, c134 float, c135 float, c136 float, c137 float, c138 float, "
 
11921
   "c139 float, c140 float, c141 float, c142 float, c143 float, c144 float, "
 
11922
   "c145 float, c146 float, c147 float, c148 float, c149 float, c150 float, "
 
11923
   "c151 float, c152 float, c153 float, c154 float, c155 float, c156 float, "
 
11924
   "c157 float, c158 float, c159 float, c160 float, c161 float, c162 float, "
 
11925
   "c163 float, c164 float, c165 float, c166 float, c167 float, c168 float, "
 
11926
   "c169 float, c170 float, c171 float, c172 float, c173 float, c174 float, "
 
11927
   "c175 float, c176 float, c177 float, c178 float, c179 float, c180 float, "
 
11928
   "c181 float, c182 float, c183 float, c184 float, c185 float, c186 float, "
 
11929
   "c187 float, c188 float, c189 float, c190 float, c191 float, c192 float, "
 
11930
   "c193 float, c194 float, c195 float, c196 float, c197 float, c198 float, "
 
11931
   "c199 float, c200 float, c201 float, c202 float, c203 float, c204 float, "
 
11932
   "c205 float, c206 float, c207 float, c208 float, c209 float, c210 float, "
 
11933
   "c211 float, c212 float, c213 float, c214 float, c215 float, c216 float, "
 
11934
   "c217 float, c218 float, c219 float, c220 float, c221 float, c222 float, "
 
11935
   "c223 float, c224 float, c225 float, c226 float, c227 float, c228 float, "
 
11936
   "c229 float, c230 float, c231 float, c232 float, c233 float, c234 float, "
 
11937
   "c235 float, c236 float, c237 float, c238 float, c239 float, c240 float, "
 
11938
   "c241 float, c242 float, c243 float, c244 float, c245 float, c246 float, "
 
11939
   "c247 float, c248 float, c249 float, c250 float)";
 
11940
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
11941
  myquery(rc);
 
11942
 
 
11943
  my_bind= (MYSQL_BIND*) malloc(MAX_PARAM_COUNT * sizeof(MYSQL_BIND));
 
11944
  query= (char*) malloc(strlen(query_template) +
 
11945
                        MAX_PARAM_COUNT * CHARS_PER_PARAM + 1);
 
11946
  param_str= (char*) malloc(COLUMN_COUNT * CHARS_PER_PARAM);
 
11947
 
 
11948
  if (my_bind == 0 || query == 0 || param_str == 0)
 
11949
  {
 
11950
    fprintf(stderr, "Can't allocate enough memory for query structs\n");
 
11951
    if (my_bind)
 
11952
      free(my_bind);
 
11953
    if (query)
 
11954
      free(query);
 
11955
    if (param_str)
 
11956
      free(param_str);
 
11957
    return;
 
11958
  }
 
11959
 
 
11960
  stmt= mysql_stmt_init(mysql);
 
11961
 
 
11962
  /* setup a template for one row of parameters */
 
11963
  sprintf(param_str, "(");
 
11964
  for (i= 1; i < COLUMN_COUNT; ++i)
 
11965
    strcat(param_str, "?, ");
 
11966
  strcat(param_str, "?)");
 
11967
  param_str_length= strlen(param_str);
 
11968
 
 
11969
  /* setup bind array */
 
11970
  bzero((char*) my_bind, MAX_PARAM_COUNT * sizeof(MYSQL_BIND));
 
11971
  for (i= 0; i < MAX_PARAM_COUNT; ++i)
 
11972
  {
 
11973
    my_bind[i].buffer_type= MYSQL_TYPE_FLOAT;
 
11974
    my_bind[i].buffer= fa_ptr;
 
11975
    if (++fa_ptr == float_array + COLUMN_COUNT)
 
11976
      fa_ptr= float_array;
 
11977
  }
 
11978
 
 
11979
  /*
 
11980
    Test each number of rows per bulk insert, so that we can see where
 
11981
    MySQL fails.
 
11982
  */
 
11983
  for (nrows= MIN_ROWS_PER_INSERT; nrows <= MAX_ROWS_PER_INSERT; ++nrows)
 
11984
  {
 
11985
    char *query_ptr;
 
11986
    /* Create statement text for current number of rows */
 
11987
    sprintf(query, query_template, param_str);
 
11988
    query_ptr= query + strlen(query);
 
11989
    for (i= 1; i < nrows; ++i)
 
11990
    {
 
11991
      memcpy(query_ptr, ", ", 2);
 
11992
      query_ptr+= 2;
 
11993
      memcpy(query_ptr, param_str, param_str_length);
 
11994
      query_ptr+= param_str_length;
 
11995
    }
 
11996
    *query_ptr= '\0';
 
11997
 
 
11998
    rc= mysql_stmt_prepare(stmt, query, query_ptr - query);
 
11999
    if (rc && nrows * COLUMN_COUNT > uint16_max)
 
12000
    {
 
12001
      if (!opt_silent)
 
12002
        printf("Failed to prepare a statement with %d placeholders "
 
12003
               "(as expected).\n", nrows * COLUMN_COUNT);
 
12004
      break;
 
12005
    }
 
12006
    else
 
12007
      check_execute(stmt, rc);
 
12008
 
 
12009
    if (!opt_silent)
 
12010
      printf("Insert: query length= %d, row count= %d, param count= %lu\n",
 
12011
             (int) strlen(query), nrows, mysql_stmt_param_count(stmt));
 
12012
 
 
12013
    /* bind the parameter array and execute the query */
 
12014
    rc= mysql_stmt_bind_param(stmt, my_bind);
 
12015
    check_execute(stmt, rc);
 
12016
 
 
12017
    rc= mysql_stmt_execute(stmt);
 
12018
    check_execute(stmt, rc);
 
12019
    mysql_stmt_reset(stmt);
 
12020
  }
 
12021
 
 
12022
  mysql_stmt_close(stmt);
 
12023
  free(my_bind);
 
12024
  free(query);
 
12025
  free(param_str);
 
12026
  stmt_text= "drop table t1";
 
12027
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12028
  myquery(rc);
 
12029
}
 
12030
 
 
12031
 
 
12032
static void test_bug5315()
 
12033
{
 
12034
  MYSQL_STMT *stmt;
 
12035
  const char *stmt_text;
 
12036
  int rc;
 
12037
 
 
12038
  myheader("test_bug5315");
 
12039
 
 
12040
  stmt_text= "SELECT 1";
 
12041
  stmt= mysql_stmt_init(mysql);
 
12042
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12043
  DIE_UNLESS(rc == 0);
 
12044
  if (!opt_silent)
 
12045
    printf("Excuting mysql_change_user\n");
 
12046
  mysql_change_user(mysql, opt_user, opt_password, current_db);
 
12047
  if (!opt_silent)
 
12048
    printf("Excuting mysql_stmt_execute\n");
 
12049
  rc= mysql_stmt_execute(stmt);
 
12050
  DIE_UNLESS(rc != 0);
 
12051
  if (rc)
 
12052
  {
 
12053
    if (!opt_silent)
 
12054
      printf("Got error (as expected): '%s'\n", mysql_stmt_error(stmt));
 
12055
  }
 
12056
  /* check that connection is OK */
 
12057
  if (!opt_silent)
 
12058
    printf("Excuting mysql_stmt_close\n");
 
12059
  mysql_stmt_close(stmt);
 
12060
  if (!opt_silent)
 
12061
    printf("Excuting mysql_stmt_init\n");
 
12062
  stmt= mysql_stmt_init(mysql);
 
12063
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12064
  DIE_UNLESS(rc == 0);
 
12065
  rc= mysql_stmt_execute(stmt);
 
12066
  DIE_UNLESS(rc == 0);
 
12067
  mysql_stmt_close(stmt);
 
12068
}
 
12069
 
 
12070
 
 
12071
static void test_bug6049()
 
12072
{
 
12073
  MYSQL_STMT *stmt;
 
12074
  MYSQL_BIND my_bind[1];
 
12075
  MYSQL_RES *res;
 
12076
  MYSQL_ROW row;
 
12077
  const char *stmt_text;
 
12078
  char buffer[30];
 
12079
  ulong length;
 
12080
  int rc;
 
12081
 
 
12082
  myheader("test_bug6049");
 
12083
 
 
12084
  stmt_text= "SELECT MAKETIME(-25, 12, 12)";
 
12085
 
 
12086
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12087
  myquery(rc);
 
12088
  res= mysql_store_result(mysql);
 
12089
  row= mysql_fetch_row(res);
 
12090
 
 
12091
  stmt= mysql_stmt_init(mysql);
 
12092
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12093
  check_execute(stmt, rc);
 
12094
  rc= mysql_stmt_execute(stmt);
 
12095
  check_execute(stmt, rc);
 
12096
 
 
12097
  bzero((char*) my_bind, sizeof(my_bind));
 
12098
  my_bind[0].buffer_type    = MYSQL_TYPE_STRING;
 
12099
  my_bind[0].buffer         = &buffer;
 
12100
  my_bind[0].buffer_length  = sizeof(buffer);
 
12101
  my_bind[0].length         = &length;
 
12102
 
 
12103
  mysql_stmt_bind_result(stmt, my_bind);
 
12104
  rc= mysql_stmt_fetch(stmt);
 
12105
  DIE_UNLESS(rc == 0);
 
12106
 
 
12107
  if (!opt_silent)
 
12108
  {
 
12109
    printf("Result from query: %s\n", row[0]);
 
12110
    printf("Result from prepared statement: %s\n", (char*) buffer);
 
12111
  }
 
12112
 
 
12113
  DIE_UNLESS(strcmp(row[0], (char*) buffer) == 0);
 
12114
 
 
12115
  mysql_free_result(res);
 
12116
  mysql_stmt_close(stmt);
 
12117
}
 
12118
 
 
12119
 
 
12120
static void test_bug6058()
 
12121
{
 
12122
  MYSQL_STMT *stmt;
 
12123
  MYSQL_BIND my_bind[1];
 
12124
  MYSQL_RES *res;
 
12125
  MYSQL_ROW row;
 
12126
  const char *stmt_text;
 
12127
  char buffer[30];
 
12128
  ulong length;
 
12129
  int rc;
 
12130
 
 
12131
  myheader("test_bug6058");
 
12132
 
 
12133
  stmt_text= "SELECT CAST('0000-00-00' AS DATE)";
 
12134
 
 
12135
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12136
  myquery(rc);
 
12137
  res= mysql_store_result(mysql);
 
12138
  row= mysql_fetch_row(res);
 
12139
 
 
12140
  stmt= mysql_stmt_init(mysql);
 
12141
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12142
  check_execute(stmt, rc);
 
12143
  rc= mysql_stmt_execute(stmt);
 
12144
  check_execute(stmt, rc);
 
12145
 
 
12146
  bzero((char*) my_bind, sizeof(my_bind));
 
12147
  my_bind[0].buffer_type    = MYSQL_TYPE_STRING;
 
12148
  my_bind[0].buffer         = &buffer;
 
12149
  my_bind[0].buffer_length  = sizeof(buffer);
 
12150
  my_bind[0].length         = &length;
 
12151
 
 
12152
  mysql_stmt_bind_result(stmt, my_bind);
 
12153
  rc= mysql_stmt_fetch(stmt);
 
12154
  DIE_UNLESS(rc == 0);
 
12155
 
 
12156
  if (!opt_silent)
 
12157
  {
 
12158
    printf("Result from query: %s\n", row[0]);
 
12159
    printf("Result from prepared statement: %s\n", buffer);
 
12160
  }
 
12161
 
 
12162
  DIE_UNLESS(strcmp(row[0], buffer) == 0);
 
12163
 
 
12164
  mysql_free_result(res);
 
12165
  mysql_stmt_close(stmt);
 
12166
}
 
12167
 
 
12168
 
 
12169
static void test_bug6059()
 
12170
{
 
12171
  MYSQL_STMT *stmt;
 
12172
  const char *stmt_text;
 
12173
 
 
12174
  myheader("test_bug6059");
 
12175
 
 
12176
  stmt_text= "SELECT 'foo' INTO OUTFILE 'x.3'";
 
12177
 
 
12178
  stmt= mysql_stmt_init(mysql);
 
12179
  (void) mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12180
  DIE_UNLESS(mysql_stmt_field_count(stmt) == 0);
 
12181
  mysql_stmt_close(stmt);
 
12182
}
 
12183
 
 
12184
 
 
12185
static void test_bug6046()
 
12186
{
 
12187
  MYSQL_STMT *stmt;
 
12188
  const char *stmt_text;
 
12189
  int rc;
 
12190
  short b= 1;
 
12191
  MYSQL_BIND my_bind[1];
 
12192
 
 
12193
  myheader("test_bug6046");
 
12194
 
 
12195
  stmt_text= "DROP TABLE IF EXISTS t1";
 
12196
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12197
  myquery(rc);
 
12198
  stmt_text= "CREATE TABLE t1 (a int, b int)";
 
12199
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12200
  myquery(rc);
 
12201
  stmt_text= "INSERT INTO t1 VALUES (1,1),(2,2),(3,1),(4,2)";
 
12202
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12203
  myquery(rc);
 
12204
 
 
12205
  stmt= mysql_stmt_init(mysql);
 
12206
 
 
12207
  stmt_text= "SELECT t1.a FROM t1 NATURAL JOIN t1 as X1 "
 
12208
             "WHERE t1.b > ? ORDER BY t1.a";
 
12209
 
 
12210
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12211
  check_execute(stmt, rc);
 
12212
 
 
12213
  b= 1;
 
12214
  bzero((char*) my_bind, sizeof(my_bind));
 
12215
  my_bind[0].buffer= &b;
 
12216
  my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
 
12217
 
 
12218
  mysql_stmt_bind_param(stmt, my_bind);
 
12219
 
 
12220
  rc= mysql_stmt_execute(stmt);
 
12221
  check_execute(stmt, rc);
 
12222
  mysql_stmt_store_result(stmt);
 
12223
 
 
12224
  rc= mysql_stmt_execute(stmt);
 
12225
  check_execute(stmt, rc);
 
12226
 
 
12227
  mysql_stmt_close(stmt);
 
12228
}
 
12229
 
 
12230
 
 
12231
 
 
12232
static void test_basic_cursors()
 
12233
{
 
12234
  const char *basic_tables[]=
 
12235
  {
 
12236
    "DROP TABLE IF EXISTS t1, t2",
 
12237
 
 
12238
    "CREATE TABLE t1 "
 
12239
    "(id INTEGER NOT NULL PRIMARY KEY, "
 
12240
    " name VARCHAR(20) NOT NULL)",
 
12241
 
 
12242
    "INSERT INTO t1 (id, name) VALUES "
 
12243
    "  (2, 'Ja'), (3, 'Ede'), "
 
12244
    "  (4, 'Haag'), (5, 'Kabul'), "
 
12245
    "  (6, 'Almere'), (7, 'Utrecht'), "
 
12246
    "  (8, 'Qandahar'), (9, 'Amsterdam'), "
 
12247
    "  (10, 'Amersfoort'), (11, 'Constantine')",
 
12248
 
 
12249
    "CREATE TABLE t2 "
 
12250
    "(id INTEGER NOT NULL PRIMARY KEY, "
 
12251
    " name VARCHAR(20) NOT NULL)",
 
12252
 
 
12253
    "INSERT INTO t2 (id, name) VALUES "
 
12254
    "  (4, 'Guam'), (5, 'Aruba'), "
 
12255
    "  (6, 'Angola'), (7, 'Albania'), "
 
12256
    "  (8, 'Anguilla'), (9, 'Argentina'), "
 
12257
    "  (10, 'Azerbaijan'), (11, 'Afghanistan'), "
 
12258
    "  (12, 'Burkina Faso'), (13, 'Faroe Islands')"
 
12259
  };
 
12260
  const char *queries[]=
 
12261
  {
 
12262
    "SELECT * FROM t1",
 
12263
    "SELECT * FROM t2"
 
12264
  };
 
12265
 
 
12266
  DBUG_ENTER("test_basic_cursors");
 
12267
  myheader("test_basic_cursors");
 
12268
 
 
12269
  fill_tables(basic_tables, sizeof(basic_tables)/sizeof(*basic_tables));
 
12270
 
 
12271
  fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_ROW_BY_ROW_FETCH);
 
12272
  fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_STORE_RESULT);
 
12273
  DBUG_VOID_RETURN;
 
12274
}
 
12275
 
 
12276
 
 
12277
static void test_cursors_with_union()
 
12278
{
 
12279
  const char *queries[]=
 
12280
  {
 
12281
    "SELECT t1.name FROM t1 UNION SELECT t2.name FROM t2",
 
12282
    "SELECT t1.id FROM t1 WHERE t1.id < 5"
 
12283
  };
 
12284
  myheader("test_cursors_with_union");
 
12285
  fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_ROW_BY_ROW_FETCH);
 
12286
  fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_STORE_RESULT);
 
12287
}
 
12288
 
 
12289
 
 
12290
static void test_cursors_with_procedure()
 
12291
{
 
12292
  const char *queries[]=
 
12293
  {
 
12294
    "SELECT * FROM t1 procedure analyse()"
 
12295
  };
 
12296
  myheader("test_cursors_with_procedure");
 
12297
  fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_ROW_BY_ROW_FETCH);
 
12298
  fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_STORE_RESULT);
 
12299
}
 
12300
 
 
12301
 
 
12302
/*
 
12303
  Altough mysql_create_db(), mysql_rm_db() are deprecated since 4.0 they
 
12304
  should not crash server and should not hang in case of errors.
 
12305
 
 
12306
  Since those functions can't be seen in modern API (unless client library
 
12307
  was compiled with USE_OLD_FUNCTIONS define) we use simple_command() macro.
 
12308
*/
 
12309
static void test_bug6081()
 
12310
{
 
12311
  int rc;
 
12312
  myheader("test_bug6081");
 
12313
 
 
12314
  rc= simple_command(mysql, COM_DROP_DB, (uchar*) current_db,
 
12315
                     (ulong)strlen(current_db), 0);
 
12316
  if (rc == 0 && mysql_errno(mysql) != ER_UNKNOWN_COM_ERROR)
 
12317
  {
 
12318
    myerror(NULL);                                   /* purecov: inspected */
 
12319
    die(__FILE__, __LINE__, "COM_DROP_DB failed");   /* purecov: inspected */
 
12320
  }
 
12321
  rc= simple_command(mysql, COM_DROP_DB, (uchar*) current_db,
 
12322
                     (ulong)strlen(current_db), 0U);
 
12323
  myquery_r(rc);
 
12324
  rc= simple_command(mysql, COM_CREATE_DB, (uchar*) current_db,
 
12325
                     (ulong)strlen(current_db), 0U);
 
12326
  if (rc == 0 && mysql_errno(mysql) != ER_UNKNOWN_COM_ERROR)
 
12327
  {
 
12328
    myerror(NULL);                                   /* purecov: inspected */
 
12329
    die(__FILE__, __LINE__, "COM_CREATE_DB failed"); /* purecov: inspected */
 
12330
  }
 
12331
  rc= simple_command(mysql, COM_CREATE_DB, (uchar*) current_db,
 
12332
                     (ulong)strlen(current_db), 0U);
 
12333
  myquery_r(rc);
 
12334
  rc= mysql_select_db(mysql, current_db);
 
12335
  myquery(rc);
 
12336
}
 
12337
 
 
12338
 
 
12339
static void test_bug6096()
 
12340
{
 
12341
  MYSQL_STMT *stmt;
 
12342
  MYSQL_RES *query_result, *stmt_metadata;
 
12343
  const char *stmt_text;
 
12344
  MYSQL_BIND my_bind[12];
 
12345
  MYSQL_FIELD *query_field_list, *stmt_field_list;
 
12346
  ulong query_field_count, stmt_field_count;
 
12347
  int rc;
 
12348
  my_bool update_max_length= TRUE;
 
12349
  uint i;
 
12350
 
 
12351
  myheader("test_bug6096");
 
12352
 
 
12353
  stmt_text= "drop table if exists t1";
 
12354
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12355
  myquery(rc);
 
12356
 
 
12357
  mysql_query(mysql, "set sql_mode=''");
 
12358
  stmt_text= "create table t1 (c_tinyint tinyint, c_smallint smallint, "
 
12359
                             " c_mediumint mediumint, c_int int, "
 
12360
                             " c_bigint bigint, c_float float, "
 
12361
                             " c_double double, c_varchar varchar(20), "
 
12362
                             " c_char char(20), c_time time, c_date date, "
 
12363
                             " c_datetime datetime)";
 
12364
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12365
  myquery(rc);
 
12366
  stmt_text= "insert into t1  values (-100, -20000, 30000000, 4, 8, 1.0, "
 
12367
                                     "2.0, 'abc', 'def', now(), now(), now())";
 
12368
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12369
  myquery(rc);
 
12370
 
 
12371
  stmt_text= "select * from t1";
 
12372
 
 
12373
  /* Run select in prepared and non-prepared mode and compare metadata */
 
12374
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12375
  myquery(rc);
 
12376
  query_result= mysql_store_result(mysql);
 
12377
  query_field_list= mysql_fetch_fields(query_result);
 
12378
  query_field_count= mysql_num_fields(query_result);
 
12379
 
 
12380
  stmt= mysql_stmt_init(mysql);
 
12381
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12382
  check_execute(stmt, rc);
 
12383
  rc= mysql_stmt_execute(stmt);
 
12384
  check_execute(stmt, rc);
 
12385
  mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH,
 
12386
                      (void*) &update_max_length);
 
12387
  mysql_stmt_store_result(stmt);
 
12388
  stmt_metadata= mysql_stmt_result_metadata(stmt);
 
12389
  stmt_field_list= mysql_fetch_fields(stmt_metadata);
 
12390
  stmt_field_count= mysql_num_fields(stmt_metadata);
 
12391
  DIE_UNLESS(stmt_field_count == query_field_count);
 
12392
 
 
12393
  /* Print out and check the metadata */
 
12394
 
 
12395
  if (!opt_silent)
 
12396
  {
 
12397
    printf(" ------------------------------------------------------------\n");
 
12398
    printf("             |                     Metadata \n");
 
12399
    printf(" ------------------------------------------------------------\n");
 
12400
    printf("             |         Query          |   Prepared statement \n");
 
12401
    printf(" ------------------------------------------------------------\n");
 
12402
    printf(" field name  |  length   | max_length |  length   |  max_length\n");
 
12403
    printf(" ------------------------------------------------------------\n");
 
12404
 
 
12405
    for (i= 0; i < query_field_count; ++i)
 
12406
    {
 
12407
      MYSQL_FIELD *f1= &query_field_list[i], *f2= &stmt_field_list[i];
 
12408
      printf(" %-11s | %9lu | %10lu | %9lu | %10lu \n",
 
12409
             f1->name, f1->length, f1->max_length, f2->length, f2->max_length);
 
12410
      DIE_UNLESS(f1->length == f2->length);
 
12411
    }
 
12412
    printf(" ---------------------------------------------------------------\n");
 
12413
  }
 
12414
 
 
12415
  /* Bind and fetch the data */
 
12416
 
 
12417
  bzero((char*) my_bind, sizeof(my_bind));
 
12418
  for (i= 0; i < stmt_field_count; ++i)
 
12419
  {
 
12420
    my_bind[i].buffer_type= MYSQL_TYPE_STRING;
 
12421
    my_bind[i].buffer_length= stmt_field_list[i].max_length + 1;
 
12422
    my_bind[i].buffer= malloc(my_bind[i].buffer_length);
 
12423
  }
 
12424
  mysql_stmt_bind_result(stmt, my_bind);
 
12425
  rc= mysql_stmt_fetch(stmt);
 
12426
  check_execute(stmt, rc);
 
12427
  rc= mysql_stmt_fetch(stmt);
 
12428
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
12429
 
 
12430
  /* Clean up */
 
12431
 
 
12432
  for (i= 0; i < stmt_field_count; ++i)
 
12433
    free(my_bind[i].buffer);
 
12434
  mysql_stmt_close(stmt);
 
12435
  mysql_free_result(query_result);
 
12436
  mysql_free_result(stmt_metadata);
 
12437
  stmt_text= "drop table t1";
 
12438
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12439
  myquery(rc);
 
12440
}
 
12441
 
 
12442
 
 
12443
/*
 
12444
  Test of basic checks that are performed in server for components
 
12445
  of MYSQL_TIME parameters.
 
12446
*/
 
12447
 
 
12448
static void test_datetime_ranges()
 
12449
{
 
12450
  const char *stmt_text;
 
12451
  int rc, i;
 
12452
  MYSQL_STMT *stmt;
 
12453
  MYSQL_BIND my_bind[6];
 
12454
  MYSQL_TIME tm[6];
 
12455
 
 
12456
  myheader("test_datetime_ranges");
 
12457
 
 
12458
  stmt_text= "drop table if exists t1";
 
12459
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12460
  myquery(rc);
 
12461
 
 
12462
  stmt_text= "create table t1 (year datetime, month datetime, day datetime, "
 
12463
                              "hour datetime, min datetime, sec datetime)";
 
12464
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12465
  myquery(rc);
 
12466
 
 
12467
  stmt= mysql_simple_prepare(mysql,
 
12468
                             "INSERT INTO t1 VALUES (?, ?, ?, ?, ?, ?)");
 
12469
  check_stmt(stmt);
 
12470
  verify_param_count(stmt, 6);
 
12471
 
 
12472
  bzero((char*) my_bind, sizeof(my_bind));
 
12473
  for (i= 0; i < 6; i++)
 
12474
  {
 
12475
    my_bind[i].buffer_type= MYSQL_TYPE_DATETIME;
 
12476
    my_bind[i].buffer= &tm[i];
 
12477
  }
 
12478
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
12479
  check_execute(stmt, rc);
 
12480
 
 
12481
  tm[0].year= 2004; tm[0].month= 11; tm[0].day= 10;
 
12482
  tm[0].hour= 12; tm[0].minute= 30; tm[0].second= 30;
 
12483
  tm[0].second_part= 0; tm[0].neg= 0;
 
12484
 
 
12485
  tm[5]= tm[4]= tm[3]= tm[2]= tm[1]= tm[0];
 
12486
  tm[0].year= 10000;  tm[1].month= 13; tm[2].day= 32;
 
12487
  tm[3].hour= 24; tm[4].minute= 60; tm[5].second= 60;
 
12488
 
 
12489
  rc= mysql_stmt_execute(stmt);
 
12490
  check_execute(stmt, rc);
 
12491
  DIE_UNLESS(mysql_warning_count(mysql) != 6);
 
12492
 
 
12493
  verify_col_data("t1", "year", "0000-00-00 00:00:00");
 
12494
  verify_col_data("t1", "month", "0000-00-00 00:00:00");
 
12495
  verify_col_data("t1", "day", "0000-00-00 00:00:00");
 
12496
  verify_col_data("t1", "hour", "0000-00-00 00:00:00");
 
12497
  verify_col_data("t1", "min", "0000-00-00 00:00:00");
 
12498
  verify_col_data("t1", "sec", "0000-00-00 00:00:00");
 
12499
 
 
12500
  mysql_stmt_close(stmt);
 
12501
 
 
12502
  stmt_text= "delete from t1";
 
12503
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12504
  myquery(rc);
 
12505
 
 
12506
  stmt= mysql_simple_prepare(mysql, "INSERT INTO t1 (year, month, day) "
 
12507
                                    "VALUES (?, ?, ?)");
 
12508
  check_stmt(stmt);
 
12509
  verify_param_count(stmt, 3);
 
12510
 
 
12511
  /*
 
12512
    We reuse contents of bind and tm arrays left from previous part of test.
 
12513
  */
 
12514
  for (i= 0; i < 3; i++)
 
12515
    my_bind[i].buffer_type= MYSQL_TYPE_DATE;
 
12516
 
 
12517
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
12518
  check_execute(stmt, rc);
 
12519
 
 
12520
  rc= mysql_stmt_execute(stmt);
 
12521
  check_execute(stmt, rc);
 
12522
  DIE_UNLESS(mysql_warning_count(mysql) != 3);
 
12523
 
 
12524
  verify_col_data("t1", "year", "0000-00-00 00:00:00");
 
12525
  verify_col_data("t1", "month", "0000-00-00 00:00:00");
 
12526
  verify_col_data("t1", "day", "0000-00-00 00:00:00");
 
12527
 
 
12528
  mysql_stmt_close(stmt);
 
12529
 
 
12530
  stmt_text= "drop table t1";
 
12531
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12532
  myquery(rc);
 
12533
 
 
12534
  stmt_text= "create table t1 (day_ovfl time, day time, hour time, min time, sec time)";
 
12535
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12536
  myquery(rc);
 
12537
 
 
12538
  stmt= mysql_simple_prepare(mysql,
 
12539
                             "INSERT INTO t1 VALUES (?, ?, ?, ?, ?)");
 
12540
  check_stmt(stmt);
 
12541
  verify_param_count(stmt, 5);
 
12542
 
 
12543
  /*
 
12544
    Again we reuse what we can from previous part of test.
 
12545
  */
 
12546
  for (i= 0; i < 5; i++)
 
12547
    my_bind[i].buffer_type= MYSQL_TYPE_TIME;
 
12548
 
 
12549
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
12550
  check_execute(stmt, rc);
 
12551
 
 
12552
  tm[0].year= 0; tm[0].month= 0; tm[0].day= 10;
 
12553
  tm[0].hour= 12; tm[0].minute= 30; tm[0].second= 30;
 
12554
  tm[0].second_part= 0; tm[0].neg= 0;
 
12555
 
 
12556
  tm[4]= tm[3]= tm[2]= tm[1]= tm[0];
 
12557
  tm[0].day= 35; tm[1].day= 34; tm[2].hour= 30; tm[3].minute= 60; tm[4].second= 60;
 
12558
 
 
12559
  rc= mysql_stmt_execute(stmt);
 
12560
  check_execute(stmt, rc);
 
12561
  DIE_UNLESS(mysql_warning_count(mysql) == 2);
 
12562
 
 
12563
  verify_col_data("t1", "day_ovfl", "838:59:59");
 
12564
  verify_col_data("t1", "day", "828:30:30");
 
12565
  verify_col_data("t1", "hour", "270:30:30");
 
12566
  verify_col_data("t1", "min", "00:00:00");
 
12567
  verify_col_data("t1", "sec", "00:00:00");
 
12568
 
 
12569
  mysql_stmt_close(stmt);
 
12570
 
 
12571
  stmt_text= "drop table t1";
 
12572
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12573
  myquery(rc);
 
12574
}
 
12575
 
 
12576
 
 
12577
static void test_bug4172()
 
12578
{
 
12579
  MYSQL_STMT *stmt;
 
12580
  MYSQL_BIND my_bind[3];
 
12581
  const char *stmt_text;
 
12582
  MYSQL_RES *res;
 
12583
  MYSQL_ROW row;
 
12584
  int rc;
 
12585
  char f[100], d[100], e[100];
 
12586
  ulong f_len, d_len, e_len;
 
12587
 
 
12588
  myheader("test_bug4172");
 
12589
 
 
12590
  mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
12591
  mysql_query(mysql, "CREATE TABLE t1 (f float, d double, e decimal(10,4))");
 
12592
  mysql_query(mysql, "INSERT INTO t1 VALUES (12345.1234, 123456.123456, "
 
12593
                                            "123456.1234)");
 
12594
 
 
12595
  stmt= mysql_stmt_init(mysql);
 
12596
  stmt_text= "SELECT f, d, e FROM t1";
 
12597
 
 
12598
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12599
  check_execute(stmt, rc);
 
12600
  rc= mysql_stmt_execute(stmt);
 
12601
  check_execute(stmt, rc);
 
12602
 
 
12603
  bzero((char*) my_bind, sizeof(my_bind));
 
12604
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
12605
  my_bind[0].buffer= f;
 
12606
  my_bind[0].buffer_length= sizeof(f);
 
12607
  my_bind[0].length= &f_len;
 
12608
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
12609
  my_bind[1].buffer= d;
 
12610
  my_bind[1].buffer_length= sizeof(d);
 
12611
  my_bind[1].length= &d_len;
 
12612
  my_bind[2].buffer_type= MYSQL_TYPE_STRING;
 
12613
  my_bind[2].buffer= e;
 
12614
  my_bind[2].buffer_length= sizeof(e);
 
12615
  my_bind[2].length= &e_len;
 
12616
 
 
12617
  mysql_stmt_bind_result(stmt, my_bind);
 
12618
 
 
12619
  mysql_stmt_store_result(stmt);
 
12620
  rc= mysql_stmt_fetch(stmt);
 
12621
  check_execute(stmt, rc);
 
12622
 
 
12623
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12624
  myquery(rc);
 
12625
  res= mysql_store_result(mysql);
 
12626
  row= mysql_fetch_row(res);
 
12627
 
 
12628
  if (!opt_silent)
 
12629
  {
 
12630
    printf("Binary protocol: float=%s, double=%s, decimal(10,4)=%s\n",
 
12631
           f, d, e);
 
12632
    printf("Text protocol:   float=%s, double=%s, decimal(10,4)=%s\n",
 
12633
           row[0], row[1], row[2]);
 
12634
  }
 
12635
  DIE_UNLESS(!strcmp(f, row[0]) && !strcmp(d, row[1]) && !strcmp(e, row[2]));
 
12636
 
 
12637
  mysql_free_result(res);
 
12638
  mysql_stmt_close(stmt);
 
12639
}
 
12640
 
 
12641
 
 
12642
static void test_conversion()
 
12643
{
 
12644
  MYSQL_STMT *stmt;
 
12645
  const char *stmt_text;
 
12646
  int rc;
 
12647
  MYSQL_BIND my_bind[1];
 
12648
  char buff[4];
 
12649
  ulong length;
 
12650
 
 
12651
  myheader("test_conversion");
 
12652
 
 
12653
  stmt_text= "DROP TABLE IF EXISTS t1";
 
12654
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12655
  myquery(rc);
 
12656
  stmt_text= "CREATE TABLE t1 (a TEXT) DEFAULT CHARSET latin1";
 
12657
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12658
  myquery(rc);
 
12659
  stmt_text= "SET character_set_connection=utf8, character_set_client=utf8, "
 
12660
             " character_set_results=latin1";
 
12661
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12662
  myquery(rc);
 
12663
 
 
12664
  stmt= mysql_stmt_init(mysql);
 
12665
 
 
12666
  stmt_text= "INSERT INTO t1 (a) VALUES (?)";
 
12667
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12668
  check_execute(stmt, rc);
 
12669
 
 
12670
  bzero((char*) my_bind, sizeof(my_bind));
 
12671
  my_bind[0].buffer= buff;
 
12672
  my_bind[0].length= &length;
 
12673
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
12674
 
 
12675
  mysql_stmt_bind_param(stmt, my_bind);
 
12676
 
 
12677
  buff[0]= (uchar) 0xC3;
 
12678
  buff[1]= (uchar) 0xA0;
 
12679
  length= 2;
 
12680
 
 
12681
  rc= mysql_stmt_execute(stmt);
 
12682
  check_execute(stmt, rc);
 
12683
 
 
12684
  stmt_text= "SELECT a FROM t1";
 
12685
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12686
  check_execute(stmt, rc);
 
12687
  rc= mysql_stmt_execute(stmt);
 
12688
  check_execute(stmt, rc);
 
12689
 
 
12690
  my_bind[0].buffer_length= sizeof(buff);
 
12691
  mysql_stmt_bind_result(stmt, my_bind);
 
12692
 
 
12693
  rc= mysql_stmt_fetch(stmt);
 
12694
  DIE_UNLESS(rc == 0);
 
12695
  DIE_UNLESS(length == 1);
 
12696
  DIE_UNLESS((uchar) buff[0] == 0xE0);
 
12697
  rc= mysql_stmt_fetch(stmt);
 
12698
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
12699
 
 
12700
  mysql_stmt_close(stmt);
 
12701
  stmt_text= "DROP TABLE t1";
 
12702
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12703
  myquery(rc);
 
12704
  stmt_text= "SET NAMES DEFAULT";
 
12705
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12706
  myquery(rc);
 
12707
}
 
12708
 
 
12709
static void test_rewind(void)
 
12710
{
 
12711
  MYSQL_STMT *stmt;
 
12712
  MYSQL_BIND my_bind;
 
12713
  int rc = 0;
 
12714
  const char *stmt_text;
 
12715
  long unsigned int length=4, Data=0;
 
12716
  my_bool isnull=0;
 
12717
 
 
12718
  myheader("test_rewind");
 
12719
 
 
12720
  stmt_text= "CREATE TABLE t1 (a int)";
 
12721
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12722
  myquery(rc);
 
12723
  stmt_text= "INSERT INTO t1 VALUES(2),(3),(4)";
 
12724
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12725
  myquery(rc);
 
12726
 
 
12727
  stmt= mysql_stmt_init(mysql);
 
12728
 
 
12729
  stmt_text= "SELECT * FROM t1";
 
12730
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12731
  check_execute(stmt, rc);
 
12732
 
 
12733
  bzero((char*) &my_bind, sizeof(MYSQL_BIND));
 
12734
  my_bind.buffer_type= MYSQL_TYPE_LONG;
 
12735
  my_bind.buffer= (void *)&Data; /* this buffer won't be altered */
 
12736
  my_bind.length= &length;
 
12737
  my_bind.is_null= &isnull;
 
12738
 
 
12739
  rc= mysql_stmt_execute(stmt);
 
12740
  check_execute(stmt, rc);
 
12741
 
 
12742
  rc= mysql_stmt_store_result(stmt);
 
12743
  DIE_UNLESS(rc == 0);
 
12744
 
 
12745
  rc= mysql_stmt_bind_result(stmt, &my_bind);
 
12746
  DIE_UNLESS(rc == 0);
 
12747
 
 
12748
  /* retreive all result sets till we are at the end */
 
12749
  while(!mysql_stmt_fetch(stmt))
 
12750
    if (!opt_silent)
 
12751
      printf("fetched result:%ld\n", Data);
 
12752
 
 
12753
  DIE_UNLESS(rc != MYSQL_NO_DATA);
 
12754
 
 
12755
  /* seek to the first row */
 
12756
  mysql_stmt_data_seek(stmt, 0);
 
12757
 
 
12758
  /* now we should be able to fetch the results again */
 
12759
  /* but mysql_stmt_fetch returns MYSQL_NO_DATA */
 
12760
  while(!(rc= mysql_stmt_fetch(stmt)))
 
12761
    if (!opt_silent)
 
12762
      printf("fetched result after seek:%ld\n", Data);
 
12763
  
 
12764
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
12765
 
 
12766
  stmt_text= "DROP TABLE t1";
 
12767
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12768
  myquery(rc);
 
12769
  rc= mysql_stmt_free_result(stmt);
 
12770
  rc= mysql_stmt_close(stmt);
 
12771
}
 
12772
 
 
12773
 
 
12774
static void test_truncation()
 
12775
{
 
12776
  MYSQL_STMT *stmt;
 
12777
  const char *stmt_text;
 
12778
  int rc;
 
12779
  uint bind_count;
 
12780
  MYSQL_BIND *bind_array, *my_bind;
 
12781
 
 
12782
  myheader("test_truncation");
 
12783
 
 
12784
  /* Prepare the test table */
 
12785
  rc= mysql_query(mysql, "drop table if exists t1");
 
12786
  myquery(rc);
 
12787
 
 
12788
  stmt_text= "create table t1 ("
 
12789
             "i8 tinyint, ui8 tinyint unsigned, "
 
12790
             "i16 smallint, i16_1 smallint, "
 
12791
             "ui16 smallint unsigned, i32 int, i32_1 int, "
 
12792
             "d double, d_1 double, ch char(30), ch_1 char(30), "
 
12793
             "tx text, tx_1 text, ch_2 char(30) "
 
12794
             ")";
 
12795
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12796
  myquery(rc);
 
12797
  stmt_text= "insert into t1 VALUES ("
 
12798
             "-10, "                            /* i8 */
 
12799
             "200, "                            /* ui8 */
 
12800
             "32000, "                          /* i16 */
 
12801
             "-32767, "                         /* i16_1 */
 
12802
             "64000, "                          /* ui16 */
 
12803
             "1073741824, "                     /* i32 */
 
12804
             "1073741825, "                     /* i32_1 */
 
12805
             "123.456, "                        /* d */
 
12806
             "-12345678910, "                   /* d_1 */
 
12807
             "'111111111111111111111111111111',"/* ch */
 
12808
             "'abcdef', "                       /* ch_1 */
 
12809
             "'12345          ', "              /* tx */
 
12810
             "'12345.67               ', "      /* tx_1 */
 
12811
             "'12345.67abc'"                    /* ch_2 */
 
12812
             ")";
 
12813
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12814
  myquery(rc);
 
12815
 
 
12816
  stmt_text= "select i8 c1, i8 c2, ui8 c3, i16_1 c4, ui16 c5, "
 
12817
             "       i16 c6, ui16 c7, i32 c8, i32_1 c9, i32_1 c10, "
 
12818
             "       d c11, d_1 c12, d_1 c13, ch c14, ch_1 c15, tx c16, "
 
12819
             "       tx_1 c17, ch_2 c18 "
 
12820
             "from t1";
 
12821
 
 
12822
  stmt= mysql_stmt_init(mysql);
 
12823
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12824
  check_execute(stmt, rc);
 
12825
  rc= mysql_stmt_execute(stmt);
 
12826
  check_execute(stmt, rc);
 
12827
  bind_count= (uint) mysql_stmt_field_count(stmt);
 
12828
 
 
12829
  /*************** Fill in the bind structure and bind it **************/
 
12830
  bind_array= malloc(sizeof(MYSQL_BIND) * bind_count);
 
12831
  bzero((char*) bind_array, sizeof(MYSQL_BIND) * bind_count);
 
12832
  for (my_bind= bind_array; my_bind < bind_array + bind_count; my_bind++)
 
12833
    my_bind->error= &my_bind->error_value;
 
12834
  my_bind= bind_array;
 
12835
 
 
12836
  my_bind->buffer= malloc(sizeof(uint8));
 
12837
  my_bind->buffer_type= MYSQL_TYPE_TINY;
 
12838
  my_bind->is_unsigned= TRUE;
 
12839
 
 
12840
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12841
  my_bind->buffer= malloc(sizeof(uint32));
 
12842
  my_bind->buffer_type= MYSQL_TYPE_LONG;
 
12843
  my_bind->is_unsigned= TRUE;
 
12844
 
 
12845
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12846
  my_bind->buffer= malloc(sizeof(int8));
 
12847
  my_bind->buffer_type= MYSQL_TYPE_TINY;
 
12848
 
 
12849
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12850
  my_bind->buffer= malloc(sizeof(uint16));
 
12851
  my_bind->buffer_type= MYSQL_TYPE_SHORT;
 
12852
  my_bind->is_unsigned= TRUE;
 
12853
 
 
12854
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12855
  my_bind->buffer= malloc(sizeof(int16));
 
12856
  my_bind->buffer_type= MYSQL_TYPE_SHORT;
 
12857
 
 
12858
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12859
  my_bind->buffer= malloc(sizeof(uint16));
 
12860
  my_bind->buffer_type= MYSQL_TYPE_SHORT;
 
12861
  my_bind->is_unsigned= TRUE;
 
12862
 
 
12863
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12864
  my_bind->buffer= malloc(sizeof(int8));
 
12865
  my_bind->buffer_type= MYSQL_TYPE_TINY;
 
12866
  my_bind->is_unsigned= TRUE;
 
12867
 
 
12868
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12869
  my_bind->buffer= malloc(sizeof(float));
 
12870
  my_bind->buffer_type= MYSQL_TYPE_FLOAT;
 
12871
 
 
12872
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12873
  my_bind->buffer= malloc(sizeof(float));
 
12874
  my_bind->buffer_type= MYSQL_TYPE_FLOAT;
 
12875
 
 
12876
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12877
  my_bind->buffer= malloc(sizeof(double));
 
12878
  my_bind->buffer_type= MYSQL_TYPE_DOUBLE;
 
12879
 
 
12880
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12881
  my_bind->buffer= malloc(sizeof(longlong));
 
12882
  my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
 
12883
 
 
12884
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12885
  my_bind->buffer= malloc(sizeof(ulonglong));
 
12886
  my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
 
12887
  my_bind->is_unsigned= TRUE;
 
12888
 
 
12889
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12890
  my_bind->buffer= malloc(sizeof(longlong));
 
12891
  my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
 
12892
 
 
12893
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12894
  my_bind->buffer= malloc(sizeof(longlong));
 
12895
  my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
 
12896
 
 
12897
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12898
  my_bind->buffer= malloc(sizeof(longlong));
 
12899
  my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
 
12900
 
 
12901
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12902
  my_bind->buffer= malloc(sizeof(longlong));
 
12903
  my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
 
12904
 
 
12905
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12906
  my_bind->buffer= malloc(sizeof(double));
 
12907
  my_bind->buffer_type= MYSQL_TYPE_DOUBLE;
 
12908
 
 
12909
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12910
  my_bind->buffer= malloc(sizeof(double));
 
12911
  my_bind->buffer_type= MYSQL_TYPE_DOUBLE;
 
12912
 
 
12913
  rc= mysql_stmt_bind_result(stmt, bind_array);
 
12914
  check_execute(stmt, rc);
 
12915
  rc= mysql_stmt_fetch(stmt);
 
12916
  DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
 
12917
 
 
12918
  /*************** Verify truncation results ***************************/
 
12919
  my_bind= bind_array;
 
12920
 
 
12921
  /* signed tiny -> tiny */
 
12922
  DIE_UNLESS(*my_bind->error && * (int8*) my_bind->buffer == -10);
 
12923
 
 
12924
  /* signed tiny -> uint32 */
 
12925
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12926
  DIE_UNLESS(*my_bind->error && * (int32*) my_bind->buffer == -10);
 
12927
 
 
12928
  /* unsigned tiny -> tiny */
 
12929
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12930
  DIE_UNLESS(*my_bind->error && * (uint8*) my_bind->buffer == 200);
 
12931
 
 
12932
  /* short -> ushort */
 
12933
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12934
  DIE_UNLESS(*my_bind->error && * (int16*) my_bind->buffer == -32767);
 
12935
 
 
12936
  /* ushort -> short */
 
12937
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12938
  DIE_UNLESS(*my_bind->error && * (uint16*) my_bind->buffer == 64000);
 
12939
 
 
12940
  /* short -> ushort (no truncation, data is in the range of target type) */
 
12941
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12942
  DIE_UNLESS(! *my_bind->error && * (uint16*) my_bind->buffer == 32000);
 
12943
 
 
12944
  /* ushort -> utiny */
 
12945
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12946
  DIE_UNLESS(*my_bind->error && * (int8*) my_bind->buffer == 0);
 
12947
 
 
12948
  /* int -> float: no truncation, the number is a power of two */
 
12949
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12950
  DIE_UNLESS(! *my_bind->error && * (float*) my_bind->buffer == 1073741824);
 
12951
 
 
12952
  /* int -> float: truncation, not enough bits in float */
 
12953
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12954
  DIE_UNLESS(*my_bind->error);
 
12955
 
 
12956
  /* int -> double: no truncation */
 
12957
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12958
  DIE_UNLESS(! *my_bind->error && * (double*) my_bind->buffer == 1073741825);
 
12959
 
 
12960
  /* double -> longlong: fractional part is lost */
 
12961
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12962
 
 
12963
  /* double -> ulonglong, negative fp number to unsigned integer */
 
12964
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12965
  /* Value in the buffer is not defined: don't test it */
 
12966
  DIE_UNLESS(*my_bind->error);
 
12967
 
 
12968
  /* double -> longlong, negative fp number to signed integer: no loss */
 
12969
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12970
  DIE_UNLESS(! *my_bind->error && * (longlong*) my_bind->buffer == LL(-12345678910));
 
12971
 
 
12972
  /* big numeric string -> number */
 
12973
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12974
  DIE_UNLESS(*my_bind->error);
 
12975
 
 
12976
  /* junk string -> number */
 
12977
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12978
  DIE_UNLESS(*my_bind->error && *(longlong*) my_bind->buffer == 0);
 
12979
 
 
12980
  /* string with trailing spaces -> number */
 
12981
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12982
  DIE_UNLESS(! *my_bind->error && *(longlong*) my_bind->buffer == 12345);
 
12983
 
 
12984
  /* string with trailing spaces -> double */
 
12985
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12986
  DIE_UNLESS(! *my_bind->error && *(double*) my_bind->buffer == 12345.67);
 
12987
 
 
12988
  /* string with trailing junk -> double */
 
12989
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12990
  /*
 
12991
    XXX: There must be a truncation error: but it's not the way the server
 
12992
    behaves, so let's leave it for now.
 
12993
  */
 
12994
  DIE_UNLESS(*(double*) my_bind->buffer == 12345.67);
 
12995
  /*
 
12996
    TODO: string -> double,  double -> time, double -> string (truncation
 
12997
          errors are not supported here yet)
 
12998
          longlong -> time/date/datetime
 
12999
          date -> time, date -> timestamp, date -> number
 
13000
          time -> string, time -> date, time -> timestamp,
 
13001
          number -> date string -> date
 
13002
  */
 
13003
  /*************** Cleanup *********************************************/
 
13004
 
 
13005
  mysql_stmt_close(stmt);
 
13006
 
 
13007
  for (my_bind= bind_array; my_bind < bind_array + bind_count; my_bind++)
 
13008
    free(my_bind->buffer);
 
13009
  free(bind_array);
 
13010
 
 
13011
  rc= mysql_query(mysql, "drop table t1");
 
13012
  myquery(rc);
 
13013
}
 
13014
 
 
13015
static void test_truncation_option()
 
13016
{
 
13017
  MYSQL_STMT *stmt;
 
13018
  const char *stmt_text;
 
13019
  int rc;
 
13020
  uint8 buf;
 
13021
  my_bool option= 0;
 
13022
  my_bool error;
 
13023
  MYSQL_BIND my_bind;
 
13024
 
 
13025
  myheader("test_truncation_option");
 
13026
 
 
13027
  /* Prepare the test table */
 
13028
  stmt_text= "select -1";
 
13029
 
 
13030
  stmt= mysql_stmt_init(mysql);
 
13031
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
13032
  check_execute(stmt, rc);
 
13033
  rc= mysql_stmt_execute(stmt);
 
13034
  check_execute(stmt, rc);
 
13035
 
 
13036
  bzero((char*) &my_bind, sizeof(my_bind));
 
13037
 
 
13038
  my_bind.buffer= (void*) &buf;
 
13039
  my_bind.buffer_type= MYSQL_TYPE_TINY;
 
13040
  my_bind.is_unsigned= TRUE;
 
13041
  my_bind.error= &error;
 
13042
 
 
13043
  rc= mysql_stmt_bind_result(stmt, &my_bind);
 
13044
  check_execute(stmt, rc);
 
13045
  rc= mysql_stmt_fetch(stmt);
 
13046
  DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
 
13047
  DIE_UNLESS(error);
 
13048
  rc= mysql_options(mysql, MYSQL_REPORT_DATA_TRUNCATION, (char*) &option);
 
13049
  myquery(rc);
 
13050
  /* need to rebind for the new setting to take effect */
 
13051
  rc= mysql_stmt_bind_result(stmt, &my_bind);
 
13052
  check_execute(stmt, rc);
 
13053
  rc= mysql_stmt_execute(stmt);
 
13054
  check_execute(stmt, rc);
 
13055
  rc= mysql_stmt_fetch(stmt);
 
13056
  check_execute(stmt, rc);
 
13057
  /* The only change is rc - error pointers are still filled in */
 
13058
  DIE_UNLESS(error == 1);
 
13059
  /* restore back the defaults */
 
13060
  option= 1;
 
13061
  mysql_options(mysql, MYSQL_REPORT_DATA_TRUNCATION, (char*) &option);
 
13062
 
 
13063
  mysql_stmt_close(stmt);
 
13064
}
 
13065
 
 
13066
 
 
13067
/* Bug#6761 - mysql_list_fields doesn't work */
 
13068
 
 
13069
static void test_bug6761(void)
 
13070
{
 
13071
  const char *stmt_text;
 
13072
  MYSQL_RES *res;
 
13073
  int rc;
 
13074
  myheader("test_bug6761");
 
13075
 
 
13076
  stmt_text= "CREATE TABLE t1 (a int, b char(255), c decimal)";
 
13077
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
13078
  myquery(rc);
 
13079
 
 
13080
  res= mysql_list_fields(mysql, "t1", "%");
 
13081
  DIE_UNLESS(res && mysql_num_fields(res) == 3);
 
13082
  mysql_free_result(res);
 
13083
 
 
13084
  stmt_text= "DROP TABLE t1";
 
13085
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
13086
  myquery(rc);
 
13087
}
 
13088
 
 
13089
 
 
13090
/* Bug#8330 - mysql_stmt_execute crashes (libmysql) */
 
13091
 
 
13092
static void test_bug8330()
 
13093
{
 
13094
  const char *stmt_text;
 
13095
  MYSQL_STMT *stmt[2];
 
13096
  int i, rc;
 
13097
  const char *query= "select a,b from t1 where a=?";
 
13098
  MYSQL_BIND my_bind[2];
 
13099
  long lval[2];
 
13100
 
 
13101
  myheader("test_bug8330");
 
13102
 
 
13103
  stmt_text= "drop table if exists t1";
 
13104
  /* in case some previos test failed */
 
13105
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
13106
  myquery(rc);
 
13107
  stmt_text= "create table t1 (a int, b int)";
 
13108
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
13109
  myquery(rc);
 
13110
 
 
13111
  bzero((char*) my_bind, sizeof(my_bind));
 
13112
  for (i=0; i < 2; i++)
 
13113
  {
 
13114
    stmt[i]= mysql_stmt_init(mysql);
 
13115
    rc= mysql_stmt_prepare(stmt[i], query, strlen(query));
 
13116
    check_execute(stmt[i], rc);
 
13117
 
 
13118
    my_bind[i].buffer_type= MYSQL_TYPE_LONG;
 
13119
    my_bind[i].buffer= (void*) &lval[i];
 
13120
    my_bind[i].is_null= 0;
 
13121
    mysql_stmt_bind_param(stmt[i], &my_bind[i]);
 
13122
  }
 
13123
 
 
13124
  rc= mysql_stmt_execute(stmt[0]);
 
13125
  check_execute(stmt[0], rc);
 
13126
 
 
13127
  rc= mysql_stmt_execute(stmt[1]);
 
13128
  DIE_UNLESS(rc && mysql_stmt_errno(stmt[1]) == CR_COMMANDS_OUT_OF_SYNC);
 
13129
  rc= mysql_stmt_execute(stmt[0]);
 
13130
  check_execute(stmt[0], rc);
 
13131
 
 
13132
  mysql_stmt_close(stmt[0]);
 
13133
  mysql_stmt_close(stmt[1]);
 
13134
 
 
13135
  stmt_text= "drop table t1";
 
13136
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
13137
  myquery(rc);
 
13138
}
 
13139
 
 
13140
 
 
13141
/* Bug#7990 - mysql_stmt_close doesn't reset mysql->net.last_error */
 
13142
 
 
13143
static void test_bug7990()
 
13144
{
 
13145
  MYSQL_STMT *stmt;
 
13146
  int rc;
 
13147
  myheader("test_bug7990");
 
13148
 
 
13149
  stmt= mysql_stmt_init(mysql);
 
13150
  rc= mysql_stmt_prepare(stmt, "foo", 3);
 
13151
  /*
 
13152
    XXX: the fact that we store errno both in STMT and in
 
13153
    MYSQL is not documented and is subject to change in 5.0
 
13154
  */
 
13155
  DIE_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql));
 
13156
  mysql_stmt_close(stmt);
 
13157
  DIE_UNLESS(!mysql_errno(mysql));
 
13158
}
 
13159
 
 
13160
/*
 
13161
  Bug #15518 - Reusing a stmt that has failed during prepare
 
13162
  does not clear error
 
13163
*/
 
13164
 
 
13165
static void test_bug15518()
 
13166
{
 
13167
  MYSQL_STMT *stmt;
 
13168
  MYSQL* mysql1;
 
13169
  int rc;
 
13170
  myheader("test_bug15518");
 
13171
 
 
13172
  mysql1= mysql_init(NULL);
 
13173
 
 
13174
  if (!mysql_real_connect(mysql1, opt_host, opt_user, opt_password,
 
13175
                          opt_db ? opt_db : "test", opt_port, opt_unix_socket,
 
13176
                          CLIENT_MULTI_STATEMENTS))
 
13177
  {
 
13178
    fprintf(stderr, "Failed to connect to the database\n");
 
13179
    DIE_UNLESS(0);
 
13180
  }
 
13181
 
 
13182
  stmt= mysql_stmt_init(mysql1);
 
13183
 
 
13184
  /*
 
13185
    The prepare of foo should fail with errno 1064 since
 
13186
    it's not a valid query
 
13187
  */
 
13188
  rc= mysql_stmt_prepare(stmt, "foo", 3);
 
13189
  if (!opt_silent)
 
13190
    fprintf(stdout, "rc: %d, mysql_stmt_errno: %d, mysql_errno: %d\n",
 
13191
            rc, mysql_stmt_errno(stmt), mysql_errno(mysql1));
 
13192
  DIE_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql1));
 
13193
 
 
13194
  /*
 
13195
    Use the same stmt and reprepare with another query that
 
13196
    suceeds
 
13197
  */
 
13198
  rc= mysql_stmt_prepare(stmt, "SHOW STATUS", 12);
 
13199
  if (!opt_silent)
 
13200
    fprintf(stdout, "rc: %d, mysql_stmt_errno: %d, mysql_errno: %d\n",
 
13201
            rc, mysql_stmt_errno(stmt), mysql_errno(mysql1));
 
13202
  DIE_UNLESS(!rc || mysql_stmt_errno(stmt) || mysql_errno(mysql1));
 
13203
 
 
13204
  mysql_stmt_close(stmt);
 
13205
  DIE_UNLESS(!mysql_errno(mysql1));
 
13206
 
 
13207
  /*
 
13208
    part2, when connection to server has been closed
 
13209
    after first prepare
 
13210
  */
 
13211
  stmt= mysql_stmt_init(mysql1);
 
13212
  rc= mysql_stmt_prepare(stmt, "foo", 3);
 
13213
  if (!opt_silent)
 
13214
    fprintf(stdout, "rc: %d, mysql_stmt_errno: %d, mysql_errno: %d\n",
 
13215
            rc, mysql_stmt_errno(stmt), mysql_errno(mysql1));
 
13216
  DIE_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql1));
 
13217
 
 
13218
  /* Close connection to server */
 
13219
  mysql_close(mysql1);
 
13220
 
 
13221
  /*
 
13222
    Use the same stmt and reprepare with another query that
 
13223
    suceeds. The prepare should fail with error 2013 since
 
13224
    connection to server has been closed.
 
13225
  */
 
13226
  rc= mysql_stmt_prepare(stmt, "SHOW STATUS", 12);
 
13227
  if (!opt_silent)
 
13228
    fprintf(stdout, "rc: %d, mysql_stmt_errno: %d\n",
 
13229
            rc, mysql_stmt_errno(stmt));
 
13230
  DIE_UNLESS(rc && mysql_stmt_errno(stmt));
 
13231
 
 
13232
  mysql_stmt_close(stmt);
 
13233
}
 
13234
 
 
13235
 
 
13236
static void disable_general_log()
 
13237
{
 
13238
  int rc;
 
13239
  rc= mysql_query(mysql, "set @@global.general_log=off");
 
13240
  myquery(rc);
 
13241
}
 
13242
 
 
13243
 
 
13244
static void enable_general_log(int truncate)
 
13245
{
 
13246
  int rc;
 
13247
 
 
13248
  rc= mysql_query(mysql, "set @save_global_general_log=@@global.general_log");
 
13249
  myquery(rc);
 
13250
 
 
13251
  rc= mysql_query(mysql, "set @@global.general_log=on");
 
13252
  myquery(rc);
 
13253
 
 
13254
  if (truncate)
 
13255
  {
 
13256
    rc= mysql_query(mysql, "truncate mysql.general_log");
 
13257
    myquery(rc);
 
13258
  }
 
13259
}
 
13260
 
 
13261
 
 
13262
static void restore_general_log()
 
13263
{
 
13264
  int rc;
 
13265
  rc= mysql_query(mysql, "set @@global.general_log=@save_global_general_log");
 
13266
  myquery(rc);
 
13267
}
 
13268
 
 
13269
 
 
13270
static void test_view_sp_list_fields()
 
13271
{
 
13272
  int           rc;
 
13273
  MYSQL_RES     *res;
 
13274
 
 
13275
  myheader("test_view_sp_list_fields");
 
13276
 
 
13277
  rc= mysql_query(mysql, "DROP FUNCTION IF EXISTS f1");
 
13278
  myquery(rc);
 
13279
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS v1, t1, t2");
 
13280
  myquery(rc);
 
13281
  rc= mysql_query(mysql, "DROP VIEW IF EXISTS v1, t1, t2");
 
13282
  myquery(rc);
 
13283
  rc= mysql_query(mysql, "create function f1 () returns int return 5");
 
13284
  myquery(rc);
 
13285
  rc= mysql_query(mysql, "create table t1 (s1 char,s2 char)");
 
13286
  myquery(rc);
 
13287
  rc= mysql_query(mysql, "create table t2 (s1 int);");
 
13288
  myquery(rc);
 
13289
  rc= mysql_query(mysql, "create view v1 as select s2,sum(s1) - \
 
13290
count(s2) as vx from t1 group by s2 having sum(s1) - count(s2) < (select f1() \
 
13291
from t2);");
 
13292
  myquery(rc);
 
13293
  res= mysql_list_fields(mysql, "v1", NullS);
 
13294
  DIE_UNLESS(res != 0 && mysql_num_fields(res) != 0);
 
13295
  rc= mysql_query(mysql, "DROP FUNCTION f1");
 
13296
  myquery(rc);
 
13297
  rc= mysql_query(mysql, "DROP VIEW v1");
 
13298
  myquery(rc);
 
13299
  rc= mysql_query(mysql, "DROP TABLE t1, t2");
 
13300
  mysql_free_result(res);
 
13301
  myquery(rc);
 
13302
 
 
13303
}
 
13304
 
 
13305
 
 
13306
/*
 
13307
 Test mysql_real_escape_string() with gbk charset
 
13308
 
 
13309
 The important part is that 0x27 (') is the second-byte in a invalid
 
13310
 two-byte GBK character here. But 0xbf5c is a valid GBK character, so
 
13311
 it needs to be escaped as 0x5cbf27
 
13312
*/
 
13313
#define TEST_BUG8378_IN  "\xef\xbb\xbf\x27\xbf\x10"
 
13314
#define TEST_BUG8378_OUT "\xef\xbb\x5c\xbf\x5c\x27\x5c\xbf\x10"
 
13315
 
 
13316
static void test_bug8378()
 
13317
{
 
13318
#if defined(HAVE_CHARSET_gbk) && !defined(EMBEDDED_LIBRARY)
 
13319
  MYSQL *lmysql;
 
13320
  char out[9]; /* strlen(TEST_BUG8378)*2+1 */
 
13321
  char buf[256];
 
13322
  int len, rc;
 
13323
 
 
13324
  myheader("test_bug8378");
 
13325
 
 
13326
  if (!opt_silent)
 
13327
    fprintf(stdout, "\n Establishing a test connection ...");
 
13328
  if (!(lmysql= mysql_init(NULL)))
 
13329
  {
 
13330
    myerror("mysql_init() failed");
 
13331
    exit(1);
 
13332
  }
 
13333
  if (mysql_options(lmysql, MYSQL_SET_CHARSET_NAME, "gbk"))
 
13334
  {
 
13335
    myerror("mysql_options() failed");
 
13336
    exit(1);
 
13337
  }
 
13338
  if (!(mysql_real_connect(lmysql, opt_host, opt_user,
 
13339
                           opt_password, current_db, opt_port,
 
13340
                           opt_unix_socket, 0)))
 
13341
  {
 
13342
    myerror("connection failed");
 
13343
    exit(1);
 
13344
  }
 
13345
  if (!opt_silent)
 
13346
    fprintf(stdout, "OK");
 
13347
 
 
13348
  len= mysql_real_escape_string(lmysql, out, TEST_BUG8378_IN, 4);
 
13349
 
 
13350
  /* No escaping should have actually happened. */
 
13351
  DIE_UNLESS(memcmp(out, TEST_BUG8378_OUT, len) == 0);
 
13352
 
 
13353
  sprintf(buf, "SELECT '%s'", out);
 
13354
  
 
13355
  rc=mysql_real_query(lmysql, buf, strlen(buf));
 
13356
  myquery(rc);
 
13357
 
 
13358
  mysql_close(lmysql);
 
13359
#endif
 
13360
}
 
13361
 
 
13362
 
 
13363
static void test_bug8722()
 
13364
{
 
13365
  MYSQL_STMT *stmt;
 
13366
  int rc;
 
13367
  const char *stmt_text;
 
13368
 
 
13369
  myheader("test_bug8722");
 
13370
  /* Prepare test data */
 
13371
  stmt_text= "drop table if exists t1, v1";
 
13372
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
13373
  myquery(rc);
 
13374
  stmt_text= "CREATE TABLE t1 (c1 varchar(10), c2 varchar(10), c3 varchar(10),"
 
13375
                             " c4 varchar(10), c5 varchar(10), c6 varchar(10),"
 
13376
                             " c7 varchar(10), c8 varchar(10), c9 varchar(10),"
 
13377
                             "c10 varchar(10))";
 
13378
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
13379
  myquery(rc);
 
13380
  stmt_text= "INSERT INTO t1 VALUES (1,2,3,4,5,6,7,8,9,10)";
 
13381
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
13382
  myquery(rc);
 
13383
  stmt_text= "CREATE VIEW v1 AS SELECT * FROM t1";
 
13384
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
13385
  myquery(rc);
 
13386
  /* Note: if you uncomment following block everything works fine */
 
13387
/*
 
13388
  rc= mysql_query(mysql, "sellect * from v1");
 
13389
  myquery(rc);
 
13390
  mysql_free_result(mysql_store_result(mysql));
 
13391
*/
 
13392
 
 
13393
  stmt= mysql_stmt_init(mysql);
 
13394
  stmt_text= "select * from v1";
 
13395
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
13396
  check_execute(stmt, rc);
 
13397
  mysql_stmt_close(stmt);
 
13398
  stmt_text= "drop table if exists t1, v1";
 
13399
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
13400
  myquery(rc);
 
13401
}
 
13402
 
 
13403
 
 
13404
MYSQL_STMT *open_cursor(const char *query)
 
13405
{
 
13406
  int rc;
 
13407
  const ulong type= (ulong)CURSOR_TYPE_READ_ONLY;
 
13408
 
 
13409
  MYSQL_STMT *stmt= mysql_stmt_init(mysql);
 
13410
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
13411
  check_execute(stmt, rc);
 
13412
 
 
13413
  mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
 
13414
  return stmt;
 
13415
}
 
13416
 
 
13417
 
 
13418
static void test_bug8880()
 
13419
{
 
13420
  MYSQL_STMT *stmt_list[2], **stmt;
 
13421
  MYSQL_STMT **stmt_list_end= (MYSQL_STMT**) stmt_list + 2;
 
13422
  int rc;
 
13423
 
 
13424
  myheader("test_bug8880");
 
13425
 
 
13426
  mysql_query(mysql, "drop table if exists t1");
 
13427
  mysql_query(mysql, "create table t1 (a int not null primary key, b int)");
 
13428
  rc= mysql_query(mysql, "insert into t1 values (1,1)");
 
13429
  myquery(rc);                                  /* one check is enough */
 
13430
  /*
 
13431
    when inserting 2 rows everything works well
 
13432
    mysql_query(mysql, "INSERT INTO t1 VALUES (1,1),(2,2)");
 
13433
  */
 
13434
  for (stmt= stmt_list; stmt < stmt_list_end; stmt++)
 
13435
    *stmt= open_cursor("select a from t1");
 
13436
  for (stmt= stmt_list; stmt < stmt_list_end; stmt++)
 
13437
  {
 
13438
    rc= mysql_stmt_execute(*stmt);
 
13439
    check_execute(*stmt, rc);
 
13440
  }
 
13441
  for (stmt= stmt_list; stmt < stmt_list_end; stmt++)
 
13442
    mysql_stmt_close(*stmt);
 
13443
}
 
13444
 
 
13445
 
 
13446
static void test_bug9159()
 
13447
{
 
13448
  MYSQL_STMT *stmt;
 
13449
  int rc;
 
13450
  const char *stmt_text= "select a, b from t1";
 
13451
  const unsigned long type= CURSOR_TYPE_READ_ONLY;
 
13452
 
 
13453
  myheader("test_bug9159");
 
13454
 
 
13455
  mysql_query(mysql, "drop table if exists t1");
 
13456
  mysql_query(mysql, "create table t1 (a int not null primary key, b int)");
 
13457
  rc= mysql_query(mysql, "insert into t1 values (1,1)");
 
13458
  myquery(rc);
 
13459
 
 
13460
  stmt= mysql_stmt_init(mysql);
 
13461
  mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
13462
  mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void *)&type);
 
13463
 
 
13464
  mysql_stmt_execute(stmt);
 
13465
  mysql_stmt_close(stmt);
 
13466
  rc= mysql_query(mysql, "drop table if exists t1");
 
13467
  myquery(rc);
 
13468
}
 
13469
 
 
13470
 
 
13471
/* Crash when opening a cursor to a query with DISTICNT and no key */
 
13472
 
 
13473
static void test_bug9520()
 
13474
{
 
13475
  MYSQL_STMT *stmt;
 
13476
  MYSQL_BIND my_bind[1];
 
13477
  char a[6];
 
13478
  ulong a_len;
 
13479
  int rc, row_count= 0;
 
13480
 
 
13481
  myheader("test_bug9520");
 
13482
 
 
13483
  mysql_query(mysql, "drop table if exists t1");
 
13484
  mysql_query(mysql, "create table t1 (a char(5), b char(5), c char(5),"
 
13485
                     " primary key (a, b, c))");
 
13486
  rc= mysql_query(mysql, "insert into t1 values ('x', 'y', 'z'), "
 
13487
                  " ('a', 'b', 'c'), ('k', 'l', 'm')");
 
13488
  myquery(rc);
 
13489
 
 
13490
  stmt= open_cursor("select distinct b from t1");
 
13491
 
 
13492
  /*
 
13493
    Not crashes with:
 
13494
    stmt= open_cursor("select distinct a from t1");
 
13495
  */
 
13496
 
 
13497
  rc= mysql_stmt_execute(stmt);
 
13498
  check_execute(stmt, rc);
 
13499
 
 
13500
  bzero((char*) my_bind, sizeof(my_bind));
 
13501
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
13502
  my_bind[0].buffer= (char*) a;
 
13503
  my_bind[0].buffer_length= sizeof(a);
 
13504
  my_bind[0].length= &a_len;
 
13505
 
 
13506
  mysql_stmt_bind_result(stmt, my_bind);
 
13507
 
 
13508
  while (!(rc= mysql_stmt_fetch(stmt)))
 
13509
    row_count++;
 
13510
 
 
13511
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
13512
 
 
13513
  if (!opt_silent)
 
13514
    printf("Fetched %d rows\n", row_count);
 
13515
  DBUG_ASSERT(row_count == 3);
 
13516
 
 
13517
  mysql_stmt_close(stmt);
 
13518
 
 
13519
  rc= mysql_query(mysql, "drop table t1");
 
13520
  myquery(rc);
 
13521
}
 
13522
 
 
13523
 
 
13524
/*
 
13525
  We can't have more than one cursor open for a prepared statement.
 
13526
  Test re-executions of a PS with cursor; mysql_stmt_reset must close
 
13527
  the cursor attached to the statement, if there is one.
 
13528
*/
 
13529
 
 
13530
static void test_bug9478()
 
13531
{
 
13532
  MYSQL_STMT *stmt;
 
13533
  MYSQL_BIND my_bind[1];
 
13534
  char a[6];
 
13535
  ulong a_len;
 
13536
  int rc, i;
 
13537
  DBUG_ENTER("test_bug9478");
 
13538
 
 
13539
  myheader("test_bug9478");
 
13540
 
 
13541
  mysql_query(mysql, "drop table if exists t1");
 
13542
  mysql_query(mysql, "create table t1 (id integer not null primary key, "
 
13543
                     " name varchar(20) not null)");
 
13544
  rc= mysql_query(mysql, "insert into t1 (id, name) values "
 
13545
                         " (1, 'aaa'), (2, 'bbb'), (3, 'ccc')");
 
13546
  myquery(rc);
 
13547
 
 
13548
  stmt= open_cursor("select name from t1 where id=2");
 
13549
 
 
13550
  bzero((char*) my_bind, sizeof(my_bind));
 
13551
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
13552
  my_bind[0].buffer= (char*) a;
 
13553
  my_bind[0].buffer_length= sizeof(a);
 
13554
  my_bind[0].length= &a_len;
 
13555
  mysql_stmt_bind_result(stmt, my_bind);
 
13556
 
 
13557
  for (i= 0; i < 5; i++)
 
13558
  {
 
13559
    rc= mysql_stmt_execute(stmt);
 
13560
    check_execute(stmt, rc);
 
13561
    rc= mysql_stmt_fetch(stmt);
 
13562
    check_execute(stmt, rc);
 
13563
    if (!opt_silent && i == 0)
 
13564
      printf("Fetched row: %s\n", a);
 
13565
 
 
13566
    /*
 
13567
      The query above is a one-row result set. Therefore, there is no
 
13568
      cursor associated with it, as the server won't bother with opening
 
13569
      a cursor for a one-row result set. The first row was read from the
 
13570
      server in the fetch above. But there is eof packet pending in the
 
13571
      network. mysql_stmt_execute will flush the packet and successfully
 
13572
      execute the statement.
 
13573
    */
 
13574
 
 
13575
    rc= mysql_stmt_execute(stmt);
 
13576
    check_execute(stmt, rc);
 
13577
 
 
13578
    rc= mysql_stmt_fetch(stmt);
 
13579
    check_execute(stmt, rc);
 
13580
    if (!opt_silent && i == 0)
 
13581
      printf("Fetched row: %s\n", a);
 
13582
    rc= mysql_stmt_fetch(stmt);
 
13583
    DIE_UNLESS(rc == MYSQL_NO_DATA);
 
13584
 
 
13585
    {
 
13586
      char buff[8];
 
13587
      /* Fill in the fetch packet */
 
13588
      int4store(buff, stmt->stmt_id);
 
13589
      buff[4]= 1;                               /* prefetch rows */
 
13590
      rc= ((*mysql->methods->advanced_command)(mysql, COM_STMT_FETCH,
 
13591
                                               (uchar*) buff,
 
13592
                                               sizeof(buff), 0,0,1,NULL) ||
 
13593
           (*mysql->methods->read_query_result)(mysql));
 
13594
      DIE_UNLESS(rc);
 
13595
      if (!opt_silent && i == 0)
 
13596
        printf("Got error (as expected): %s\n", mysql_error(mysql));
 
13597
    }
 
13598
 
 
13599
    rc= mysql_stmt_execute(stmt);
 
13600
    check_execute(stmt, rc);
 
13601
 
 
13602
    rc= mysql_stmt_fetch(stmt);
 
13603
    check_execute(stmt, rc);
 
13604
    if (!opt_silent && i == 0)
 
13605
      printf("Fetched row: %s\n", a);
 
13606
 
 
13607
    rc= mysql_stmt_reset(stmt);
 
13608
    check_execute(stmt, rc);
 
13609
    rc= mysql_stmt_fetch(stmt);
 
13610
    DIE_UNLESS(rc && mysql_stmt_errno(stmt));
 
13611
    if (!opt_silent && i == 0)
 
13612
      printf("Got error (as expected): %s\n", mysql_stmt_error(stmt));
 
13613
  }
 
13614
  rc= mysql_stmt_close(stmt);
 
13615
  DIE_UNLESS(rc == 0);
 
13616
 
 
13617
  /* Test the case with a server side cursor */
 
13618
  stmt= open_cursor("select name from t1");
 
13619
 
 
13620
  mysql_stmt_bind_result(stmt, my_bind);
 
13621
 
 
13622
  for (i= 0; i < 5; i++)
 
13623
  {
 
13624
    DBUG_PRINT("loop",("i: %d", i));
 
13625
    rc= mysql_stmt_execute(stmt);
 
13626
    check_execute(stmt, rc);
 
13627
    rc= mysql_stmt_fetch(stmt);
 
13628
    check_execute(stmt, rc);
 
13629
    if (!opt_silent && i == 0)
 
13630
      printf("Fetched row: %s\n", a);
 
13631
    rc= mysql_stmt_execute(stmt);
 
13632
    check_execute(stmt, rc);
 
13633
 
 
13634
    while (! (rc= mysql_stmt_fetch(stmt)))
 
13635
    {
 
13636
      if (!opt_silent && i == 0)
 
13637
        printf("Fetched row: %s\n", a);
 
13638
    }
 
13639
    DIE_UNLESS(rc == MYSQL_NO_DATA);
 
13640
 
 
13641
    rc= mysql_stmt_execute(stmt);
 
13642
    check_execute(stmt, rc);
 
13643
 
 
13644
    rc= mysql_stmt_fetch(stmt);
 
13645
    check_execute(stmt, rc);
 
13646
    if (!opt_silent && i == 0)
 
13647
      printf("Fetched row: %s\n", a);
 
13648
 
 
13649
    rc= mysql_stmt_reset(stmt);
 
13650
    check_execute(stmt, rc);
 
13651
    rc= mysql_stmt_fetch(stmt);
 
13652
    DIE_UNLESS(rc && mysql_stmt_errno(stmt));
 
13653
    if (!opt_silent && i == 0)
 
13654
      printf("Got error (as expected): %s\n", mysql_stmt_error(stmt));
 
13655
  }
 
13656
 
 
13657
  rc= mysql_stmt_close(stmt);
 
13658
  DIE_UNLESS(rc == 0);
 
13659
 
 
13660
  rc= mysql_query(mysql, "drop table t1");
 
13661
  myquery(rc);
 
13662
  DBUG_VOID_RETURN;
 
13663
}
 
13664
 
 
13665
 
 
13666
/*
 
13667
  Error message is returned for unsupported features.
 
13668
  Test also cursors with non-default PREFETCH_ROWS
 
13669
*/
 
13670
 
 
13671
static void test_bug9643()
 
13672
{
 
13673
  MYSQL_STMT *stmt;
 
13674
  MYSQL_BIND my_bind[1];
 
13675
  int32 a;
 
13676
  int rc;
 
13677
  const char *stmt_text;
 
13678
  int num_rows= 0;
 
13679
  ulong type;
 
13680
  ulong prefetch_rows= 5;
 
13681
 
 
13682
  myheader("test_bug9643");
 
13683
 
 
13684
  mysql_query(mysql, "drop table if exists t1");
 
13685
  mysql_query(mysql, "create table t1 (id integer not null primary key)");
 
13686
  rc= mysql_query(mysql, "insert into t1 (id) values "
 
13687
                         " (1), (2), (3), (4), (5), (6), (7), (8), (9)");
 
13688
  myquery(rc);
 
13689
 
 
13690
  stmt= mysql_stmt_init(mysql);
 
13691
  /* Not implemented in 5.0 */
 
13692
  type= (ulong) CURSOR_TYPE_SCROLLABLE;
 
13693
  rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
 
13694
  DIE_UNLESS(rc);
 
13695
  if (! opt_silent)
 
13696
    printf("Got error (as expected): %s\n", mysql_stmt_error(stmt));
 
13697
 
 
13698
  type= (ulong) CURSOR_TYPE_READ_ONLY;
 
13699
  rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
 
13700
  check_execute(stmt, rc);
 
13701
  rc= mysql_stmt_attr_set(stmt, STMT_ATTR_PREFETCH_ROWS,
 
13702
                          (void*) &prefetch_rows);
 
13703
  check_execute(stmt, rc);
 
13704
  stmt_text= "select * from t1";
 
13705
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
13706
  check_execute(stmt, rc);
 
13707
 
 
13708
  bzero((char*) my_bind, sizeof(my_bind));
 
13709
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
13710
  my_bind[0].buffer= (void*) &a;
 
13711
  my_bind[0].buffer_length= sizeof(a);
 
13712
  mysql_stmt_bind_result(stmt, my_bind);
 
13713
 
 
13714
  rc= mysql_stmt_execute(stmt);
 
13715
  check_execute(stmt, rc);
 
13716
 
 
13717
  while ((rc= mysql_stmt_fetch(stmt)) == 0)
 
13718
    ++num_rows;
 
13719
  DIE_UNLESS(num_rows == 9);
 
13720
 
 
13721
  rc= mysql_stmt_close(stmt);
 
13722
  DIE_UNLESS(rc == 0);
 
13723
 
 
13724
  rc= mysql_query(mysql, "drop table t1");
 
13725
  myquery(rc);
 
13726
}
 
13727
 
 
13728
/*
 
13729
  Bug#11111: fetch from view returns wrong data
 
13730
*/
 
13731
 
 
13732
static void test_bug11111()
 
13733
{
 
13734
  MYSQL_STMT    *stmt;
 
13735
  MYSQL_BIND    my_bind[2];
 
13736
  char          buf[2][20];
 
13737
  ulong         len[2];
 
13738
  int i;
 
13739
  int rc;
 
13740
  const char *query= "SELECT DISTINCT f1,ff2 FROM v1";
 
13741
 
 
13742
  myheader("test_bug11111");
 
13743
 
 
13744
  rc= mysql_query(mysql, "drop table if exists t1, t2, v1");
 
13745
  myquery(rc);
 
13746
  rc= mysql_query(mysql, "drop view if exists t1, t2, v1");
 
13747
  myquery(rc);
 
13748
  rc= mysql_query(mysql, "create table t1 (f1 int, f2 int)");
 
13749
  myquery(rc);
 
13750
  rc= mysql_query(mysql, "create table t2 (ff1 int, ff2 int)");
 
13751
  myquery(rc);
 
13752
  rc= mysql_query(mysql, "create view v1 as select * from t1, t2 where f1=ff1");
 
13753
  myquery(rc);
 
13754
  rc= mysql_query(mysql, "insert into t1 values (1,1), (2,2), (3,3)");
 
13755
  myquery(rc);
 
13756
  rc= mysql_query(mysql, "insert into t2 values (1,1), (2,2), (3,3)");
 
13757
  myquery(rc);
 
13758
 
 
13759
  stmt= mysql_stmt_init(mysql);
 
13760
 
 
13761
  mysql_stmt_prepare(stmt, query, strlen(query));
 
13762
  mysql_stmt_execute(stmt);
 
13763
 
 
13764
  bzero((char*) my_bind, sizeof(my_bind));
 
13765
  for (i=0; i < 2; i++)
 
13766
  {
 
13767
    my_bind[i].buffer_type= MYSQL_TYPE_STRING;
 
13768
    my_bind[i].buffer= (uchar* *)&buf[i];
 
13769
    my_bind[i].buffer_length= 20;
 
13770
    my_bind[i].length= &len[i];
 
13771
  }
 
13772
 
 
13773
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
13774
  check_execute(stmt, rc);
 
13775
 
 
13776
  rc= mysql_stmt_fetch(stmt);
 
13777
  check_execute(stmt, rc);
 
13778
  if (!opt_silent)
 
13779
    printf("return: %s", buf[1]);
 
13780
  DIE_UNLESS(!strcmp(buf[1],"1"));
 
13781
  mysql_stmt_close(stmt);
 
13782
  rc= mysql_query(mysql, "drop view v1");
 
13783
  myquery(rc);
 
13784
  rc= mysql_query(mysql, "drop table t1, t2");
 
13785
  myquery(rc);
 
13786
}
 
13787
 
 
13788
/*
 
13789
  Check that proper cleanups are done for prepared statement when
 
13790
  fetching thorugh a cursor.
 
13791
*/
 
13792
 
 
13793
static void test_bug10729()
 
13794
{
 
13795
  MYSQL_STMT *stmt;
 
13796
  MYSQL_BIND my_bind[1];
 
13797
  char a[21];
 
13798
  int rc;
 
13799
  const char *stmt_text;
 
13800
  int i= 0;
 
13801
  const char *name_array[3]= { "aaa", "bbb", "ccc" };
 
13802
  ulong type;
 
13803
 
 
13804
  myheader("test_bug10729");
 
13805
 
 
13806
  mysql_query(mysql, "drop table if exists t1");
 
13807
  mysql_query(mysql, "create table t1 (id integer not null primary key,"
 
13808
                                      "name VARCHAR(20) NOT NULL)");
 
13809
  rc= mysql_query(mysql, "insert into t1 (id, name) values "
 
13810
                         "(1, 'aaa'), (2, 'bbb'), (3, 'ccc')");
 
13811
  myquery(rc);
 
13812
 
 
13813
  stmt= mysql_stmt_init(mysql);
 
13814
 
 
13815
  type= (ulong) CURSOR_TYPE_READ_ONLY;
 
13816
  rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
 
13817
  check_execute(stmt, rc);
 
13818
  stmt_text= "select name from t1";
 
13819
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
13820
  check_execute(stmt, rc);
 
13821
 
 
13822
  bzero((char*) my_bind, sizeof(my_bind));
 
13823
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
13824
  my_bind[0].buffer= (void*) a;
 
13825
  my_bind[0].buffer_length= sizeof(a);
 
13826
  mysql_stmt_bind_result(stmt, my_bind);
 
13827
 
 
13828
  for (i= 0; i < 3; i++)
 
13829
  {
 
13830
    int row_no= 0;
 
13831
    rc= mysql_stmt_execute(stmt);
 
13832
    check_execute(stmt, rc);
 
13833
    while ((rc= mysql_stmt_fetch(stmt)) == 0)
 
13834
    {
 
13835
      DIE_UNLESS(strcmp(a, name_array[row_no]) == 0);
 
13836
      if (!opt_silent)
 
13837
        printf("%d: %s\n", row_no, a);
 
13838
      ++row_no;
 
13839
    }
 
13840
    DIE_UNLESS(rc == MYSQL_NO_DATA);
 
13841
  }
 
13842
  rc= mysql_stmt_close(stmt);
 
13843
  DIE_UNLESS(rc == 0);
 
13844
 
 
13845
  rc= mysql_query(mysql, "drop table t1");
 
13846
  myquery(rc);
 
13847
}
 
13848
 
 
13849
 
 
13850
/*
 
13851
  Check that mysql_next_result works properly in case when one of
 
13852
  the statements used in a multi-statement query is erroneous
 
13853
*/
 
13854
 
 
13855
static void test_bug9992()
 
13856
{
 
13857
  MYSQL *mysql1;
 
13858
  MYSQL_RES* res ;
 
13859
  int   rc;
 
13860
 
 
13861
  myheader("test_bug9992");
 
13862
 
 
13863
  if (!opt_silent)
 
13864
    printf("Establishing a connection with option CLIENT_MULTI_STATEMENTS..\n");
 
13865
 
 
13866
  mysql1= mysql_init(NULL);
 
13867
 
 
13868
  if (!mysql_real_connect(mysql1, opt_host, opt_user, opt_password,
 
13869
                          opt_db ? opt_db : "test", opt_port, opt_unix_socket,
 
13870
                          CLIENT_MULTI_STATEMENTS))
 
13871
  {
 
13872
    fprintf(stderr, "Failed to connect to the database\n");
 
13873
    DIE_UNLESS(0);
 
13874
  }
 
13875
 
 
13876
 
 
13877
  /* Sic: SHOW DATABASE is incorrect syntax. */
 
13878
  rc= mysql_query(mysql1, "SHOW TABLES; SHOW DATABASE; SELECT 1;");
 
13879
 
 
13880
  if (rc)
 
13881
  {
 
13882
    fprintf(stderr, "[%d] %s\n", mysql_errno(mysql1), mysql_error(mysql1));
 
13883
    DIE_UNLESS(0);
 
13884
  }
 
13885
 
 
13886
  if (!opt_silent)
 
13887
    printf("Testing mysql_store_result/mysql_next_result..\n");
 
13888
 
 
13889
  res= mysql_store_result(mysql1);
 
13890
  DIE_UNLESS(res);
 
13891
  mysql_free_result(res);
 
13892
  rc= mysql_next_result(mysql1);
 
13893
  DIE_UNLESS(rc == 1);                         /* Got errors, as expected */
 
13894
 
 
13895
  if (!opt_silent)
 
13896
    fprintf(stdout, "Got error, as expected:\n [%d] %s\n",
 
13897
            mysql_errno(mysql1), mysql_error(mysql1));
 
13898
 
 
13899
  mysql_close(mysql1);
 
13900
}
 
13901
 
 
13902
/* Bug#10736: cursors and subqueries, memroot management */
 
13903
 
 
13904
static void test_bug10736()
 
13905
{
 
13906
  MYSQL_STMT *stmt;
 
13907
  MYSQL_BIND my_bind[1];
 
13908
  char a[21];
 
13909
  int rc;
 
13910
  const char *stmt_text;
 
13911
  int i= 0;
 
13912
  ulong type;
 
13913
 
 
13914
  myheader("test_bug10736");
 
13915
 
 
13916
  mysql_query(mysql, "drop table if exists t1");
 
13917
  mysql_query(mysql, "create table t1 (id integer not null primary key,"
 
13918
                                      "name VARCHAR(20) NOT NULL)");
 
13919
  rc= mysql_query(mysql, "insert into t1 (id, name) values "
 
13920
                         "(1, 'aaa'), (2, 'bbb'), (3, 'ccc')");
 
13921
  myquery(rc);
 
13922
 
 
13923
  stmt= mysql_stmt_init(mysql);
 
13924
 
 
13925
  type= (ulong) CURSOR_TYPE_READ_ONLY;
 
13926
  rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
 
13927
  check_execute(stmt, rc);
 
13928
  stmt_text= "select name from t1 where name=(select name from t1 where id=2)";
 
13929
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
13930
  check_execute(stmt, rc);
 
13931
 
 
13932
  bzero((char*) my_bind, sizeof(my_bind));
 
13933
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
13934
  my_bind[0].buffer= (void*) a;
 
13935
  my_bind[0].buffer_length= sizeof(a);
 
13936
  mysql_stmt_bind_result(stmt, my_bind);
 
13937
 
 
13938
  for (i= 0; i < 3; i++)
 
13939
  {
 
13940
    int row_no= 0;
 
13941
    rc= mysql_stmt_execute(stmt);
 
13942
    check_execute(stmt, rc);
 
13943
    while ((rc= mysql_stmt_fetch(stmt)) == 0)
 
13944
    {
 
13945
      if (!opt_silent)
 
13946
        printf("%d: %s\n", row_no, a);
 
13947
      ++row_no;
 
13948
    }
 
13949
    DIE_UNLESS(rc == MYSQL_NO_DATA);
 
13950
  }
 
13951
  rc= mysql_stmt_close(stmt);
 
13952
  DIE_UNLESS(rc == 0);
 
13953
 
 
13954
  rc= mysql_query(mysql, "drop table t1");
 
13955
  myquery(rc);
 
13956
}
 
13957
 
 
13958
/* Bug#10794: cursors, packets out of order */
 
13959
 
 
13960
static void test_bug10794()
 
13961
{
 
13962
  MYSQL_STMT *stmt, *stmt1;
 
13963
  MYSQL_BIND my_bind[2];
 
13964
  char a[21];
 
13965
  int id_val;
 
13966
  ulong a_len;
 
13967
  int rc;
 
13968
  const char *stmt_text;
 
13969
  int i= 0;
 
13970
  ulong type;
 
13971
 
 
13972
  myheader("test_bug10794");
 
13973
 
 
13974
  mysql_query(mysql, "drop table if exists t1");
 
13975
  mysql_query(mysql, "create table t1 (id integer not null primary key,"
 
13976
                                      "name varchar(20) not null)");
 
13977
  stmt= mysql_stmt_init(mysql);
 
13978
  stmt_text= "insert into t1 (id, name) values (?, ?)";
 
13979
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
13980
  check_execute(stmt, rc);
 
13981
  bzero((char*) my_bind, sizeof(my_bind));
 
13982
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
13983
  my_bind[0].buffer= (void*) &id_val;
 
13984
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
13985
  my_bind[1].buffer= (void*) a;
 
13986
  my_bind[1].length= &a_len;
 
13987
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
13988
  check_execute(stmt, rc);
 
13989
  for (i= 0; i < 42; i++)
 
13990
  {
 
13991
    id_val= (i+1)*10;
 
13992
    sprintf(a, "a%d", i);
 
13993
    a_len= strlen(a); /* safety against broken sprintf */
 
13994
    rc= mysql_stmt_execute(stmt);
 
13995
    check_execute(stmt, rc);
 
13996
  }
 
13997
  stmt_text= "select name from t1";
 
13998
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
13999
  type= (ulong) CURSOR_TYPE_READ_ONLY;
 
14000
  mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
14001
  stmt1= mysql_stmt_init(mysql);
 
14002
  mysql_stmt_attr_set(stmt1, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
14003
  bzero((char*) my_bind, sizeof(my_bind));
 
14004
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
14005
  my_bind[0].buffer= (void*) a;
 
14006
  my_bind[0].buffer_length= sizeof(a);
 
14007
  my_bind[0].length= &a_len;
 
14008
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
14009
  check_execute(stmt, rc);
 
14010
  rc= mysql_stmt_execute(stmt);
 
14011
  check_execute(stmt, rc);
 
14012
  rc= mysql_stmt_fetch(stmt);
 
14013
  check_execute(stmt, rc);
 
14014
  if (!opt_silent)
 
14015
    printf("Fetched row from stmt: %s\n", a);
 
14016
  /* Don't optimize: an attribute of the original test case */
 
14017
  mysql_stmt_free_result(stmt);
 
14018
  mysql_stmt_reset(stmt);
 
14019
  stmt_text= "select name from t1 where id=10";
 
14020
  rc= mysql_stmt_prepare(stmt1, stmt_text, strlen(stmt_text));
 
14021
  check_execute(stmt1, rc);
 
14022
  rc= mysql_stmt_bind_result(stmt1, my_bind);
 
14023
  check_execute(stmt1, rc);
 
14024
  rc= mysql_stmt_execute(stmt1);
 
14025
  while (1)
 
14026
  {
 
14027
    rc= mysql_stmt_fetch(stmt1);
 
14028
    if (rc == MYSQL_NO_DATA)
 
14029
    {
 
14030
      if (!opt_silent)
 
14031
        printf("End of data in stmt1\n");
 
14032
      break;
 
14033
    }
 
14034
    check_execute(stmt1, rc);
 
14035
    if (!opt_silent)
 
14036
      printf("Fetched row from stmt1: %s\n", a);
 
14037
  }
 
14038
  mysql_stmt_close(stmt);
 
14039
  mysql_stmt_close(stmt1);
 
14040
 
 
14041
  rc= mysql_query(mysql, "drop table t1");
 
14042
  myquery(rc);
 
14043
}
 
14044
 
 
14045
 
 
14046
/* Bug#11172: cursors, crash on a fetch from a datetime column */
 
14047
 
 
14048
static void test_bug11172()
 
14049
{
 
14050
  MYSQL_STMT *stmt;
 
14051
  MYSQL_BIND bind_in[1], bind_out[2];
 
14052
  MYSQL_TIME hired;
 
14053
  int rc;
 
14054
  const char *stmt_text;
 
14055
  int i= 0, id;
 
14056
  ulong type;
 
14057
 
 
14058
  myheader("test_bug11172");
 
14059
 
 
14060
  mysql_query(mysql, "drop table if exists t1");
 
14061
  mysql_query(mysql, "create table t1 (id integer not null primary key,"
 
14062
                                      "hired date not null)");
 
14063
  rc= mysql_query(mysql,
 
14064
                  "insert into t1 (id, hired) values (1, '1933-08-24'), "
 
14065
                  "(2, '1965-01-01'), (3, '1949-08-17'), (4, '1945-07-07'), "
 
14066
                  "(5, '1941-05-15'), (6, '1978-09-15'), (7, '1936-03-28')");
 
14067
  myquery(rc);
 
14068
  stmt= mysql_stmt_init(mysql);
 
14069
  stmt_text= "SELECT id, hired FROM t1 WHERE hired=?";
 
14070
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
14071
  check_execute(stmt, rc);
 
14072
 
 
14073
  type= (ulong) CURSOR_TYPE_READ_ONLY;
 
14074
  mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
14075
 
 
14076
  bzero((char*) bind_in, sizeof(bind_in));
 
14077
  bzero((char*) bind_out, sizeof(bind_out));
 
14078
  bzero((char*) &hired, sizeof(hired));
 
14079
  hired.year= 1965;
 
14080
  hired.month= 1;
 
14081
  hired.day= 1;
 
14082
  bind_in[0].buffer_type= MYSQL_TYPE_DATE;
 
14083
  bind_in[0].buffer= (void*) &hired;
 
14084
  bind_in[0].buffer_length= sizeof(hired);
 
14085
  bind_out[0].buffer_type= MYSQL_TYPE_LONG;
 
14086
  bind_out[0].buffer= (void*) &id;
 
14087
  bind_out[1]= bind_in[0];
 
14088
 
 
14089
  for (i= 0; i < 3; i++)
 
14090
  {
 
14091
    rc= mysql_stmt_bind_param(stmt, bind_in);
 
14092
    check_execute(stmt, rc);
 
14093
    rc= mysql_stmt_bind_result(stmt, bind_out);
 
14094
    check_execute(stmt, rc);
 
14095
    rc= mysql_stmt_execute(stmt);
 
14096
    check_execute(stmt, rc);
 
14097
    while ((rc= mysql_stmt_fetch(stmt)) == 0)
 
14098
    {
 
14099
      if (!opt_silent)
 
14100
        printf("fetched data %d:%d-%d-%d\n", id,
 
14101
               hired.year, hired.month, hired.day);
 
14102
    }
 
14103
    DIE_UNLESS(rc == MYSQL_NO_DATA);
 
14104
    if (!mysql_stmt_free_result(stmt))
 
14105
      mysql_stmt_reset(stmt);
 
14106
  }
 
14107
  mysql_stmt_close(stmt);
 
14108
  mysql_rollback(mysql);
 
14109
  mysql_rollback(mysql);
 
14110
 
 
14111
  rc= mysql_query(mysql, "drop table t1");
 
14112
  myquery(rc);
 
14113
}
 
14114
 
 
14115
 
 
14116
/* Bug#11656: cursors, crash on a fetch from a query with distinct. */
 
14117
 
 
14118
static void test_bug11656()
 
14119
{
 
14120
  MYSQL_STMT *stmt;
 
14121
  MYSQL_BIND my_bind[2];
 
14122
  int rc;
 
14123
  const char *stmt_text;
 
14124
  char buf[2][20];
 
14125
  int i= 0;
 
14126
  ulong type;
 
14127
 
 
14128
  myheader("test_bug11656");
 
14129
 
 
14130
  mysql_query(mysql, "drop table if exists t1");
 
14131
 
 
14132
  rc= mysql_query(mysql, "create table t1 ("
 
14133
                  "server varchar(40) not null, "
 
14134
                  "test_kind varchar(1) not null, "
 
14135
                  "test_id varchar(30) not null , "
 
14136
                  "primary key (server,test_kind,test_id))");
 
14137
  myquery(rc);
 
14138
 
 
14139
  stmt_text= "select distinct test_kind, test_id from t1 "
 
14140
             "where server in (?, ?)";
 
14141
  stmt= mysql_stmt_init(mysql);
 
14142
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
14143
  check_execute(stmt, rc);
 
14144
  type= (ulong) CURSOR_TYPE_READ_ONLY;
 
14145
  mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
14146
 
 
14147
  bzero((char*) my_bind, sizeof(my_bind));
 
14148
  strmov(buf[0], "pcint502_MY2");
 
14149
  strmov(buf[1], "*");
 
14150
  for (i=0; i < 2; i++)
 
14151
  {
 
14152
    my_bind[i].buffer_type= MYSQL_TYPE_STRING;
 
14153
    my_bind[i].buffer= (uchar* *)&buf[i];
 
14154
    my_bind[i].buffer_length= strlen(buf[i]);
 
14155
  }
 
14156
  mysql_stmt_bind_param(stmt, my_bind);
 
14157
 
 
14158
  rc= mysql_stmt_execute(stmt);
 
14159
  check_execute(stmt, rc);
 
14160
 
 
14161
  rc= mysql_stmt_fetch(stmt);
 
14162
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
14163
 
 
14164
  mysql_stmt_close(stmt);
 
14165
  rc= mysql_query(mysql, "drop table t1");
 
14166
  myquery(rc);
 
14167
}
 
14168
 
 
14169
 
 
14170
/*
 
14171
  Check that the server signals when NO_BACKSLASH_ESCAPES mode is in effect,
 
14172
  and mysql_real_escape_string() does the right thing as a result.
 
14173
*/
 
14174
 
 
14175
static void test_bug10214()
 
14176
{
 
14177
  int   len;
 
14178
  char  out[8];
 
14179
 
 
14180
  myheader("test_bug10214");
 
14181
 
 
14182
  DIE_UNLESS(!(mysql->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES));
 
14183
 
 
14184
  len= mysql_real_escape_string(mysql, out, "a'b\\c", 5);
 
14185
  DIE_UNLESS(memcmp(out, "a\\'b\\\\c", len) == 0);
 
14186
 
 
14187
  mysql_query(mysql, "set sql_mode='NO_BACKSLASH_ESCAPES'");
 
14188
  DIE_UNLESS(mysql->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES);
 
14189
 
 
14190
  len= mysql_real_escape_string(mysql, out, "a'b\\c", 5);
 
14191
  DIE_UNLESS(memcmp(out, "a''b\\c", len) == 0);
 
14192
 
 
14193
  mysql_query(mysql, "set sql_mode=''");
 
14194
}
 
14195
 
 
14196
static void test_client_character_set()
 
14197
{
 
14198
  MY_CHARSET_INFO cs;
 
14199
  char *csname= (char*) "utf8";
 
14200
  char *csdefault= (char*)mysql_character_set_name(mysql);
 
14201
  int rc;
 
14202
 
 
14203
  myheader("test_client_character_set");
 
14204
 
 
14205
  rc= mysql_set_character_set(mysql, csname);
 
14206
  DIE_UNLESS(rc == 0);
 
14207
 
 
14208
  mysql_get_character_set_info(mysql, &cs);
 
14209
  DIE_UNLESS(!strcmp(cs.csname, "utf8"));
 
14210
  DIE_UNLESS(!strcmp(cs.name, "utf8_general_ci"));
 
14211
  /* Restore the default character set */
 
14212
  rc= mysql_set_character_set(mysql, csdefault);
 
14213
  myquery(rc);
 
14214
}
 
14215
 
 
14216
/* Test correct max length for MEDIUMTEXT and LONGTEXT columns */
 
14217
 
 
14218
static void test_bug9735()
 
14219
{
 
14220
  MYSQL_RES *res;
 
14221
  int rc;
 
14222
 
 
14223
  myheader("test_bug9735");
 
14224
 
 
14225
  rc= mysql_query(mysql, "drop table if exists t1");
 
14226
  myquery(rc);
 
14227
  rc= mysql_query(mysql, "create table t1 (a mediumtext, b longtext) "
 
14228
                         "character set latin1");
 
14229
  myquery(rc);
 
14230
  rc= mysql_query(mysql, "select * from t1");
 
14231
  myquery(rc);
 
14232
  res= mysql_store_result(mysql);
 
14233
  verify_prepare_field(res, 0, "a", "a", MYSQL_TYPE_BLOB,
 
14234
                       "t1", "t1", current_db, (1U << 24)-1, 0);
 
14235
  verify_prepare_field(res, 1, "b", "b", MYSQL_TYPE_BLOB,
 
14236
                       "t1", "t1", current_db, ~0U, 0);
 
14237
  mysql_free_result(res);
 
14238
  rc= mysql_query(mysql, "drop table t1");
 
14239
  myquery(rc);
 
14240
}
 
14241
 
 
14242
 
 
14243
/* Bug#11183 "mysql_stmt_reset() doesn't reset information about error" */
 
14244
 
 
14245
static void test_bug11183()
 
14246
{
 
14247
  int rc;
 
14248
  MYSQL_STMT *stmt;
 
14249
  char bug_statement[]= "insert into t1 values (1)";
 
14250
 
 
14251
  myheader("test_bug11183");
 
14252
 
 
14253
  mysql_query(mysql, "drop table t1 if exists");
 
14254
  mysql_query(mysql, "create table t1 (a int)");
 
14255
 
 
14256
  stmt= mysql_stmt_init(mysql);
 
14257
  DIE_UNLESS(stmt != 0);
 
14258
 
 
14259
  rc= mysql_stmt_prepare(stmt, bug_statement, strlen(bug_statement));
 
14260
  check_execute(stmt, rc);
 
14261
 
 
14262
  rc= mysql_query(mysql, "drop table t1");
 
14263
  myquery(rc);
 
14264
 
 
14265
  /* Trying to execute statement that should fail on execute stage */
 
14266
  rc= mysql_stmt_execute(stmt);
 
14267
  DIE_UNLESS(rc);
 
14268
 
 
14269
  mysql_stmt_reset(stmt);
 
14270
  DIE_UNLESS(mysql_stmt_errno(stmt) == 0);
 
14271
 
 
14272
  mysql_query(mysql, "create table t1 (a int)");
 
14273
 
 
14274
  /* Trying to execute statement that should pass ok */
 
14275
  if (mysql_stmt_execute(stmt))
 
14276
  {
 
14277
    mysql_stmt_reset(stmt);
 
14278
    DIE_UNLESS(mysql_stmt_errno(stmt) == 0);
 
14279
  }
 
14280
 
 
14281
  mysql_stmt_close(stmt);
 
14282
 
 
14283
  rc= mysql_query(mysql, "drop table t1");
 
14284
  myquery(rc);
 
14285
}
 
14286
 
 
14287
static void test_bug11037()
 
14288
{
 
14289
  MYSQL_STMT *stmt;
 
14290
  int rc;
 
14291
  const char *stmt_text;
 
14292
 
 
14293
  myheader("test_bug11037");
 
14294
 
 
14295
  mysql_query(mysql, "drop table if exists t1");
 
14296
 
 
14297
  rc= mysql_query(mysql, "create table t1 (id int not null)");
 
14298
  myquery(rc);
 
14299
 
 
14300
  rc= mysql_query(mysql, "insert into t1 values (1)");
 
14301
  myquery(rc);
 
14302
 
 
14303
  stmt_text= "select id FROM t1";
 
14304
  stmt= mysql_stmt_init(mysql);
 
14305
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
14306
 
 
14307
  /* expected error */
 
14308
  rc = mysql_stmt_fetch(stmt);
 
14309
  DIE_UNLESS(rc==1);
 
14310
  if (!opt_silent)
 
14311
    fprintf(stdout, "Got error, as expected:\n [%d] %s\n",
 
14312
            mysql_stmt_errno(stmt), mysql_stmt_error(stmt));
 
14313
 
 
14314
  rc= mysql_stmt_execute(stmt);
 
14315
  check_execute(stmt, rc);
 
14316
 
 
14317
  rc= mysql_stmt_fetch(stmt);
 
14318
  DIE_UNLESS(rc==0);
 
14319
 
 
14320
  rc= mysql_stmt_fetch(stmt);
 
14321
  DIE_UNLESS(rc==MYSQL_NO_DATA);
 
14322
 
 
14323
  rc= mysql_stmt_fetch(stmt);
 
14324
  DIE_UNLESS(rc==MYSQL_NO_DATA);
 
14325
 
 
14326
  mysql_stmt_close(stmt);
 
14327
  rc= mysql_query(mysql, "drop table t1");
 
14328
  myquery(rc);
 
14329
}
 
14330
 
 
14331
/* Bug#10760: cursors, crash in a fetch after rollback. */
 
14332
 
 
14333
static void test_bug10760()
 
14334
{
 
14335
  MYSQL_STMT *stmt;
 
14336
  MYSQL_BIND my_bind[1];
 
14337
  int rc;
 
14338
  const char *stmt_text;
 
14339
  char id_buf[20];
 
14340
  ulong id_len;
 
14341
  int i= 0;
 
14342
  ulong type;
 
14343
 
 
14344
  myheader("test_bug10760");
 
14345
 
 
14346
  mysql_query(mysql, "drop table if exists t1, t2");
 
14347
 
 
14348
  /* create tables */
 
14349
  rc= mysql_query(mysql, "create table t1 (id integer not null primary key)"
 
14350
                         " engine=MyISAM");
 
14351
  myquery(rc);
 
14352
  for (; i < 42; ++i)
 
14353
  {
 
14354
    char buf[100];
 
14355
    sprintf(buf, "insert into t1 (id) values (%d)", i+1);
 
14356
    rc= mysql_query(mysql, buf);
 
14357
    myquery(rc);
 
14358
  }
 
14359
  mysql_autocommit(mysql, FALSE);
 
14360
  /* create statement */
 
14361
  stmt= mysql_stmt_init(mysql);
 
14362
  type= (ulong) CURSOR_TYPE_READ_ONLY;
 
14363
  mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
14364
 
 
14365
  /*
 
14366
    1: check that a deadlock within the same connection
 
14367
    is resolved and an error is returned. The deadlock is modelled
 
14368
    as follows:
 
14369
    con1: open cursor for select * from t1;
 
14370
    con1: insert into t1 (id) values (1)
 
14371
  */
 
14372
  stmt_text= "select id from t1 order by 1";
 
14373
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
14374
  check_execute(stmt, rc);
 
14375
  rc= mysql_stmt_execute(stmt);
 
14376
  check_execute(stmt, rc);
 
14377
  rc= mysql_query(mysql, "update t1 set id=id+100");
 
14378
  /*
 
14379
    If cursors are not materialized, the update will return an error;
 
14380
    we mainly test that it won't deadlock.
 
14381
  */
 
14382
  if (rc && !opt_silent)
 
14383
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
14384
  /*
 
14385
    2: check that MyISAM tables used in cursors survive
 
14386
    COMMIT/ROLLBACK.
 
14387
  */
 
14388
  rc= mysql_rollback(mysql);                  /* should not close the cursor */
 
14389
  myquery(rc);
 
14390
  rc= mysql_stmt_fetch(stmt);
 
14391
  check_execute(stmt, rc);
 
14392
 
 
14393
  /*
 
14394
    3: check that cursors to InnoDB tables are closed (for now) by
 
14395
    COMMIT/ROLLBACK.
 
14396
  */
 
14397
  if (! have_innodb)
 
14398
  {
 
14399
    if (!opt_silent)
 
14400
      printf("Testing that cursors are closed at COMMIT/ROLLBACK requires "
 
14401
             "InnoDB.\n");
 
14402
  }
 
14403
  else
 
14404
  {
 
14405
    stmt_text= "select id from t1 order by 1";
 
14406
    rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
14407
    check_execute(stmt, rc);
 
14408
 
 
14409
    rc= mysql_query(mysql, "alter table t1 engine=InnoDB");
 
14410
    myquery(rc);
 
14411
 
 
14412
    bzero(my_bind, sizeof(my_bind));
 
14413
    my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
14414
    my_bind[0].buffer= (void*) id_buf;
 
14415
    my_bind[0].buffer_length= sizeof(id_buf);
 
14416
    my_bind[0].length= &id_len;
 
14417
    check_execute(stmt, rc);
 
14418
    mysql_stmt_bind_result(stmt, my_bind);
 
14419
 
 
14420
    rc= mysql_stmt_execute(stmt);
 
14421
    rc= mysql_stmt_fetch(stmt);
 
14422
    DIE_UNLESS(rc == 0);
 
14423
    if (!opt_silent)
 
14424
      printf("Fetched row %s\n", id_buf);
 
14425
    rc= mysql_rollback(mysql);                  /* should close the cursor */
 
14426
    myquery(rc);
 
14427
#if 0
 
14428
    rc= mysql_stmt_fetch(stmt);
 
14429
    DIE_UNLESS(rc);
 
14430
    if (!opt_silent)
 
14431
      printf("Got error (as expected): %s\n", mysql_error(mysql));
 
14432
#endif
 
14433
  }
 
14434
 
 
14435
  mysql_stmt_close(stmt);
 
14436
  rc= mysql_query(mysql, "drop table t1");
 
14437
  myquery(rc);
 
14438
  mysql_autocommit(mysql, TRUE);                /* restore default */
 
14439
}
 
14440
 
 
14441
static void test_bug12001()
 
14442
{
 
14443
  MYSQL *mysql_local;
 
14444
  MYSQL_RES *result;
 
14445
  const char *query= "DROP TABLE IF EXISTS test_table;"
 
14446
                     "CREATE TABLE test_table(id INT);"
 
14447
                     "INSERT INTO test_table VALUES(10);"
 
14448
                     "UPDATE test_table SET id=20 WHERE id=10;"
 
14449
                     "SELECT * FROM test_table;"
 
14450
                     "INSERT INTO non_existent_table VALUES(11);";
 
14451
  int rc, res;
 
14452
 
 
14453
  myheader("test_bug12001");
 
14454
 
 
14455
  if (!(mysql_local= mysql_init(NULL)))
 
14456
  {
 
14457
    fprintf(stdout, "\n mysql_init() failed");
 
14458
    exit(1);
 
14459
  }
 
14460
 
 
14461
  /* Create connection that supports multi statements */
 
14462
  if (!mysql_real_connect(mysql_local, opt_host, opt_user,
 
14463
                           opt_password, current_db, opt_port,
 
14464
                           opt_unix_socket, CLIENT_MULTI_STATEMENTS |
 
14465
                           CLIENT_MULTI_RESULTS))
 
14466
  {
 
14467
    fprintf(stdout, "\n mysql_real_connect() failed");
 
14468
    exit(1);
 
14469
  }
 
14470
 
 
14471
  rc= mysql_query(mysql_local, query);
 
14472
  myquery(rc);
 
14473
 
 
14474
  do
 
14475
  {
 
14476
    if (mysql_field_count(mysql_local) &&
 
14477
        (result= mysql_use_result(mysql_local)))
 
14478
    {
 
14479
      mysql_free_result(result);
 
14480
    }
 
14481
  }
 
14482
  while (!(res= mysql_next_result(mysql_local)));
 
14483
 
 
14484
  rc= mysql_query(mysql_local, "DROP TABLE IF EXISTS test_table");
 
14485
  myquery(rc);
 
14486
 
 
14487
  mysql_close(mysql_local);
 
14488
  DIE_UNLESS(res==1);
 
14489
}
 
14490
 
 
14491
 
 
14492
/* Bug#11909: wrong metadata if fetching from two cursors */
 
14493
 
 
14494
static void test_bug11909()
 
14495
{
 
14496
  MYSQL_STMT *stmt1, *stmt2;
 
14497
  MYSQL_BIND my_bind[7];
 
14498
  int rc;
 
14499
  char firstname[20], midinit[20], lastname[20], workdept[20];
 
14500
  ulong firstname_len, midinit_len, lastname_len, workdept_len;
 
14501
  uint32 empno;
 
14502
  double salary;
 
14503
  float bonus;
 
14504
  const char *stmt_text;
 
14505
 
 
14506
  myheader("test_bug11909");
 
14507
 
 
14508
  stmt_text= "drop table if exists t1";
 
14509
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
14510
  myquery(rc);
 
14511
 
 
14512
  stmt_text= "create table t1 ("
 
14513
    "  empno int(11) not null, firstname varchar(20) not null,"
 
14514
    "  midinit varchar(20) not null, lastname varchar(20) not null,"
 
14515
    "  workdept varchar(6) not null, salary double not null,"
 
14516
    "  bonus float not null, primary key (empno)"
 
14517
    ") default charset=latin1 collate=latin1_bin";
 
14518
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
14519
  myquery(rc);
 
14520
 
 
14521
  stmt_text= "insert into t1 values "
 
14522
    "(10, 'CHRISTINE', 'I', 'HAAS',     'A00', 52750, 1000), "
 
14523
    "(20, 'MICHAEL',   'L', 'THOMPSON', 'B01', 41250, 800),"
 
14524
    "(30, 'SALLY',     'A', 'KWAN',     'C01', 38250, 800),"
 
14525
    "(50, 'JOHN',      'B', 'GEYER',    'E01', 40175, 800), "
 
14526
    "(60, 'IRVING',    'F', 'STERN',    'D11', 32250, 500)";
 
14527
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
14528
  myquery(rc);
 
14529
 
 
14530
  /* ****** Begin of trace ****** */
 
14531
 
 
14532
  stmt1= open_cursor("SELECT empno, firstname, midinit, lastname,"
 
14533
                     "workdept, salary, bonus FROM t1");
 
14534
 
 
14535
  bzero(my_bind, sizeof(my_bind));
 
14536
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
14537
  my_bind[0].buffer= (void*) &empno;
 
14538
 
 
14539
  my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
 
14540
  my_bind[1].buffer= (void*) firstname;
 
14541
  my_bind[1].buffer_length= sizeof(firstname);
 
14542
  my_bind[1].length= &firstname_len;
 
14543
 
 
14544
  my_bind[2].buffer_type= MYSQL_TYPE_VAR_STRING;
 
14545
  my_bind[2].buffer= (void*) midinit;
 
14546
  my_bind[2].buffer_length= sizeof(midinit);
 
14547
  my_bind[2].length= &midinit_len;
 
14548
 
 
14549
  my_bind[3].buffer_type= MYSQL_TYPE_VAR_STRING;
 
14550
  my_bind[3].buffer= (void*) lastname;
 
14551
  my_bind[3].buffer_length= sizeof(lastname);
 
14552
  my_bind[3].length= &lastname_len;
 
14553
 
 
14554
  my_bind[4].buffer_type= MYSQL_TYPE_VAR_STRING;
 
14555
  my_bind[4].buffer= (void*) workdept;
 
14556
  my_bind[4].buffer_length= sizeof(workdept);
 
14557
  my_bind[4].length= &workdept_len;
 
14558
 
 
14559
  my_bind[5].buffer_type= MYSQL_TYPE_DOUBLE;
 
14560
  my_bind[5].buffer= (void*) &salary;
 
14561
 
 
14562
  my_bind[6].buffer_type= MYSQL_TYPE_FLOAT;
 
14563
  my_bind[6].buffer= (void*) &bonus;
 
14564
  rc= mysql_stmt_bind_result(stmt1, my_bind);
 
14565
  check_execute(stmt1, rc);
 
14566
 
 
14567
  rc= mysql_stmt_execute(stmt1);
 
14568
  check_execute(stmt1, rc);
 
14569
 
 
14570
  rc= mysql_stmt_fetch(stmt1);
 
14571
  DIE_UNLESS(rc == 0);
 
14572
  DIE_UNLESS(empno == 10);
 
14573
  DIE_UNLESS(strcmp(firstname, "CHRISTINE") == 0);
 
14574
  DIE_UNLESS(strcmp(midinit, "I") == 0);
 
14575
  DIE_UNLESS(strcmp(lastname, "HAAS") == 0);
 
14576
  DIE_UNLESS(strcmp(workdept, "A00") == 0);
 
14577
  DIE_UNLESS(salary == (double) 52750.0);
 
14578
  DIE_UNLESS(bonus == (float) 1000.0);
 
14579
 
 
14580
  stmt2= open_cursor("SELECT empno, firstname FROM t1");
 
14581
  rc= mysql_stmt_bind_result(stmt2, my_bind);
 
14582
  check_execute(stmt2, rc);
 
14583
 
 
14584
  rc= mysql_stmt_execute(stmt2);
 
14585
  check_execute(stmt2, rc);
 
14586
 
 
14587
  rc= mysql_stmt_fetch(stmt2);
 
14588
  DIE_UNLESS(rc == 0);
 
14589
 
 
14590
  DIE_UNLESS(empno == 10);
 
14591
  DIE_UNLESS(strcmp(firstname, "CHRISTINE") == 0);
 
14592
 
 
14593
  rc= mysql_stmt_reset(stmt2);
 
14594
  check_execute(stmt2, rc);
 
14595
 
 
14596
  /* ERROR: next statement should return 0 */
 
14597
 
 
14598
  rc= mysql_stmt_fetch(stmt1);
 
14599
  DIE_UNLESS(rc == 0);
 
14600
 
 
14601
  mysql_stmt_close(stmt1);
 
14602
  mysql_stmt_close(stmt2);
 
14603
  rc= mysql_rollback(mysql);
 
14604
  myquery(rc);
 
14605
 
 
14606
  rc= mysql_query(mysql, "drop table t1");
 
14607
  myquery(rc);
 
14608
}
 
14609
 
 
14610
/* Cursors: opening a cursor to a compilicated query with ORDER BY */
 
14611
 
 
14612
static void test_bug11901()
 
14613
{
 
14614
/*  MYSQL_STMT *stmt;
 
14615
  MYSQL_BIND my_bind[2]; */
 
14616
  int rc;
 
14617
/*  char workdept[20];
 
14618
  ulong workdept_len; 
 
14619
  uint32 empno; */
 
14620
  const char *stmt_text;
 
14621
 
 
14622
  myheader("test_bug11901");
 
14623
 
 
14624
  stmt_text= "drop table if exists t1, t2";
 
14625
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
14626
  myquery(rc);
 
14627
 
 
14628
  stmt_text= "create table t1 ("
 
14629
    "  empno int(11) not null, firstname varchar(20) not null,"
 
14630
    "  midinit varchar(20) not null, lastname varchar(20) not null,"
 
14631
    "  workdept varchar(6) not null, salary double not null,"
 
14632
    "  bonus float not null, primary key (empno), "
 
14633
    " unique key (workdept, empno) "
 
14634
    ") default charset=latin1 collate=latin1_bin";
 
14635
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
14636
  myquery(rc);
 
14637
 
 
14638
  stmt_text= "insert into t1 values "
 
14639
     "(10,  'CHRISTINE', 'I', 'HAAS',      'A00', 52750, 1000),"
 
14640
     "(20,  'MICHAEL',   'L', 'THOMPSON',  'B01', 41250, 800), "
 
14641
     "(30,  'SALLY',     'A', 'KWAN',      'C01', 38250, 800), "
 
14642
     "(50,  'JOHN',      'B', 'GEYER',     'E01', 40175, 800), "
 
14643
     "(60,  'IRVING',    'F', 'STERN',     'D11', 32250, 500), "
 
14644
     "(70,  'EVA',       'D', 'PULASKI',   'D21', 36170, 700), "
 
14645
     "(90,  'EILEEN',    'W', 'HENDERSON', 'E11', 29750, 600), "
 
14646
     "(100, 'THEODORE',  'Q', 'SPENSER',   'E21', 26150, 500), "
 
14647
     "(110, 'VINCENZO',  'G', 'LUCCHESSI', 'A00', 46500, 900), "
 
14648
     "(120, 'SEAN',      '',  'O\\'CONNELL', 'A00', 29250, 600), "
 
14649
     "(130, 'DOLORES',   'M', 'QUINTANA',  'C01', 23800, 500), "
 
14650
     "(140, 'HEATHER',   'A', 'NICHOLLS',  'C01', 28420, 600), "
 
14651
     "(150, 'BRUCE',     '',  'ADAMSON',   'D11', 25280, 500), "
 
14652
     "(160, 'ELIZABETH', 'R', 'PIANKA',    'D11', 22250, 400), "
 
14653
     "(170, 'MASATOSHI', 'J', 'YOSHIMURA', 'D11', 24680, 500), "
 
14654
     "(180, 'MARILYN',   'S', 'SCOUTTEN',  'D11', 21340, 500), "
 
14655
     "(190, 'JAMES',     'H', 'WALKER',    'D11', 20450, 400), "
 
14656
     "(200, 'DAVID',     '',  'BROWN',     'D11', 27740, 600), "
 
14657
     "(210, 'WILLIAM',   'T', 'JONES',     'D11', 18270, 400), "
 
14658
     "(220, 'JENNIFER',  'K', 'LUTZ',      'D11', 29840, 600), "
 
14659
     "(230, 'JAMES',     'J', 'JEFFERSON', 'D21', 22180, 400), "
 
14660
     "(240, 'SALVATORE', 'M', 'MARINO',    'D21', 28760, 600), "
 
14661
     "(250, 'DANIEL',    'S', 'SMITH',     'D21', 19180, 400), "
 
14662
     "(260, 'SYBIL',     'P', 'JOHNSON',   'D21', 17250, 300), "
 
14663
     "(270, 'MARIA',     'L', 'PEREZ',     'D21', 27380, 500), "
 
14664
     "(280, 'ETHEL',     'R', 'SCHNEIDER', 'E11', 26250, 500), "
 
14665
     "(290, 'JOHN',      'R', 'PARKER',    'E11', 15340, 300), "
 
14666
     "(300, 'PHILIP',    'X', 'SMITH',     'E11', 17750, 400), "
 
14667
     "(310, 'MAUDE',     'F', 'SETRIGHT',  'E11', 15900, 300), "
 
14668
     "(320, 'RAMLAL',    'V', 'MEHTA',     'E21', 19950, 400), "
 
14669
     "(330, 'WING',      '',  'LEE',       'E21', 25370, 500), "
 
14670
     "(340, 'JASON',     'R', 'GOUNOT',    'E21', 23840, 500)";
 
14671
 
 
14672
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
14673
  myquery(rc);
 
14674
 
 
14675
  stmt_text= "create table t2 ("
 
14676
    " deptno varchar(6) not null, deptname varchar(20) not null,"
 
14677
    " mgrno int(11) not null, location varchar(20) not null,"
 
14678
    " admrdept varchar(6) not null, refcntd int(11) not null,"
 
14679
    " refcntu int(11) not null, primary key (deptno)"
 
14680
    ") default charset=latin1 collate=latin1_bin";
 
14681
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
14682
  myquery(rc);
 
14683
 
 
14684
  stmt_text= "insert into t2 values "
 
14685
    "('A00', 'SPIFFY COMPUTER SERV', 10, '', 'A00', 0, 0), "
 
14686
    "('B01', 'PLANNING',             20, '', 'A00', 0, 0), "
 
14687
    "('C01', 'INFORMATION CENTER',   30, '', 'A00', 0, 0), "
 
14688
    "('D01', 'DEVELOPMENT CENTER',   0,  '', 'A00', 0, 0),"
 
14689
    "('D11', 'MANUFACTURING SYSTEM', 60, '', 'D01', 0, 0), "
 
14690
    "('D21', 'ADMINISTRATION SYSTE', 70, '', 'D01', 0, 0), "
 
14691
    "('E01', 'SUPPORT SERVICES',     50, '', 'A00', 0, 0), "
 
14692
    "('E11', 'OPERATIONS',           90, '', 'E01', 0, 0), "
 
14693
    "('E21', 'SOFTWARE SUPPORT',     100,'', 'E01', 0, 0)";
 
14694
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
14695
  myquery(rc);
 
14696
 
 
14697
  /* ****** Begin of trace ****** */
 
14698
/* WL#1110 - disabled test case failure - crash. */
 
14699
/*
 
14700
  stmt= open_cursor("select t1.emp, t1.workdept "
 
14701
                    "from (t1 left join t2 on t2.deptno = t1.workdept) "
 
14702
                    "where t2.deptno in "
 
14703
                    "   (select t2.deptno "
 
14704
                    "    from (t1 left join t2 on t2.deptno = t1.workdept) "
 
14705
                    "    where t1.empno = ?) "
 
14706
                    "order by 1");
 
14707
  bzero(my_bind, sizeof(my_bind));
 
14708
 
 
14709
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
14710
  my_bind[0].buffer= &empno;
 
14711
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
14712
  check_execute(stmt, rc);
 
14713
 
 
14714
  my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
 
14715
  my_bind[1].buffer= (void*) workdept;
 
14716
  my_bind[1].buffer_length= sizeof(workdept);
 
14717
  my_bind[1].length= &workdept_len;
 
14718
 
 
14719
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
14720
  check_execute(stmt, rc);
 
14721
 
 
14722
  empno= 10;
 
14723
*/
 
14724
  /* ERROR: next statement causes a server crash */
 
14725
/*
 
14726
  rc= mysql_stmt_execute(stmt);
 
14727
  check_execute(stmt, rc);
 
14728
 
 
14729
  mysql_stmt_close(stmt);
 
14730
 
 
14731
  rc= mysql_query(mysql, "drop table t1, t2");
 
14732
  myquery(rc);
 
14733
*/
 
14734
}
 
14735
 
 
14736
/* Bug#11904: mysql_stmt_attr_set CURSOR_TYPE_READ_ONLY grouping wrong result */
 
14737
 
 
14738
static void test_bug11904()
 
14739
{
 
14740
  MYSQL_STMT *stmt1;
 
14741
  int rc;
 
14742
  const char *stmt_text;
 
14743
  const ulong type= (ulong)CURSOR_TYPE_READ_ONLY;
 
14744
  MYSQL_BIND my_bind[2];
 
14745
  int country_id=0;
 
14746
  char row_data[11]= {0};
 
14747
 
 
14748
  myheader("test_bug11904");
 
14749
 
 
14750
  /* create tables */
 
14751
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS bug11904b");
 
14752
  myquery(rc);
 
14753
  rc= mysql_query(mysql, "CREATE TABLE bug11904b (id int, name char(10), primary key(id, name))");
 
14754
  myquery(rc);
 
14755
 
 
14756
  rc= mysql_query(mysql, "INSERT INTO bug11904b VALUES (1, 'sofia'), (1,'plovdiv'),"
 
14757
                          " (1,'varna'), (2,'LA'), (2,'new york'), (3,'heidelberg'),"
 
14758
                          " (3,'berlin'), (3, 'frankfurt')");
 
14759
 
 
14760
  myquery(rc);
 
14761
  mysql_commit(mysql);
 
14762
  /* create statement */
 
14763
  stmt1= mysql_stmt_init(mysql);
 
14764
  mysql_stmt_attr_set(stmt1, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
14765
 
 
14766
  stmt_text= "SELECT id, MIN(name) FROM bug11904b GROUP BY id";
 
14767
 
 
14768
  rc= mysql_stmt_prepare(stmt1, stmt_text, strlen(stmt_text));
 
14769
  check_execute(stmt1, rc);
 
14770
 
 
14771
  memset(my_bind, 0, sizeof(my_bind));
 
14772
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
14773
  my_bind[0].buffer=& country_id;
 
14774
  my_bind[0].buffer_length= 0;
 
14775
  my_bind[0].length= 0;
 
14776
 
 
14777
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
14778
  my_bind[1].buffer=& row_data;
 
14779
  my_bind[1].buffer_length= sizeof(row_data) - 1;
 
14780
  my_bind[1].length= 0;
 
14781
 
 
14782
  rc= mysql_stmt_bind_result(stmt1, my_bind);
 
14783
  check_execute(stmt1, rc);
 
14784
 
 
14785
  rc= mysql_stmt_execute(stmt1);
 
14786
  check_execute(stmt1, rc);
 
14787
 
 
14788
  rc= mysql_stmt_fetch(stmt1);
 
14789
  check_execute(stmt1, rc);
 
14790
  DIE_UNLESS(country_id == 1);
 
14791
  DIE_UNLESS(memcmp(row_data, "plovdiv", 7) == 0);
 
14792
 
 
14793
  rc= mysql_stmt_fetch(stmt1);
 
14794
  check_execute(stmt1, rc);
 
14795
  DIE_UNLESS(country_id == 2);
 
14796
  DIE_UNLESS(memcmp(row_data, "LA", 2) == 0);
 
14797
 
 
14798
  rc= mysql_stmt_fetch(stmt1);
 
14799
  check_execute(stmt1, rc);
 
14800
  DIE_UNLESS(country_id == 3);
 
14801
  DIE_UNLESS(memcmp(row_data, "berlin", 6) == 0);
 
14802
 
 
14803
  rc= mysql_stmt_close(stmt1);
 
14804
  check_execute(stmt1, rc);
 
14805
 
 
14806
  rc= mysql_query(mysql, "drop table bug11904b");
 
14807
  myquery(rc);
 
14808
}
 
14809
 
 
14810
 
 
14811
/* Bug#12243: multiple cursors, crash in a fetch after commit. */
 
14812
 
 
14813
static void test_bug12243()
 
14814
{
 
14815
  MYSQL_STMT *stmt1, *stmt2;
 
14816
  int rc;
 
14817
  const char *stmt_text;
 
14818
  ulong type;
 
14819
 
 
14820
  myheader("test_bug12243");
 
14821
 
 
14822
  if (! have_innodb)
 
14823
  {
 
14824
    if (!opt_silent)
 
14825
      printf("This test requires InnoDB.\n");
 
14826
    return;
 
14827
  }
 
14828
 
 
14829
  /* create tables */
 
14830
  mysql_query(mysql, "drop table if exists t1");
 
14831
  mysql_query(mysql, "create table t1 (a int) engine=InnoDB");
 
14832
  rc= mysql_query(mysql, "insert into t1 (a) values (1), (2)");
 
14833
  myquery(rc);
 
14834
  mysql_autocommit(mysql, FALSE);
 
14835
  /* create statement */
 
14836
  stmt1= mysql_stmt_init(mysql);
 
14837
  stmt2= mysql_stmt_init(mysql);
 
14838
  type= (ulong) CURSOR_TYPE_READ_ONLY;
 
14839
  mysql_stmt_attr_set(stmt1, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
14840
  mysql_stmt_attr_set(stmt2, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
14841
 
 
14842
  stmt_text= "select a from t1";
 
14843
 
 
14844
  rc= mysql_stmt_prepare(stmt1, stmt_text, strlen(stmt_text));
 
14845
  check_execute(stmt1, rc);
 
14846
  rc= mysql_stmt_execute(stmt1);
 
14847
  check_execute(stmt1, rc);
 
14848
  rc= mysql_stmt_fetch(stmt1);
 
14849
  check_execute(stmt1, rc);
 
14850
 
 
14851
  rc= mysql_stmt_prepare(stmt2, stmt_text, strlen(stmt_text));
 
14852
  check_execute(stmt2, rc);
 
14853
  rc= mysql_stmt_execute(stmt2);
 
14854
  check_execute(stmt2, rc);
 
14855
  rc= mysql_stmt_fetch(stmt2);
 
14856
  check_execute(stmt2, rc);
 
14857
 
 
14858
  rc= mysql_stmt_close(stmt1);
 
14859
  check_execute(stmt1, rc);
 
14860
  rc= mysql_commit(mysql);
 
14861
  myquery(rc);
 
14862
  rc= mysql_stmt_fetch(stmt2);
 
14863
  check_execute(stmt2, rc);
 
14864
 
 
14865
  mysql_stmt_close(stmt2);
 
14866
  rc= mysql_query(mysql, "drop table t1");
 
14867
  myquery(rc);
 
14868
  mysql_autocommit(mysql, TRUE);                /* restore default */
 
14869
}
 
14870
 
 
14871
 
 
14872
/*
 
14873
  Bug#11718: query with function, join and order by returns wrong type
 
14874
*/
 
14875
 
 
14876
static void test_bug11718()
 
14877
{
 
14878
  MYSQL_RES     *res;
 
14879
  int rc;
 
14880
  const char *query= "select str_to_date(concat(f3),'%Y%m%d') from t1,t2 "
 
14881
                     "where f1=f2 order by f1";
 
14882
 
 
14883
  myheader("test_bug11718");
 
14884
 
 
14885
  rc= mysql_query(mysql, "drop table if exists t1, t2");
 
14886
  myquery(rc);
 
14887
  rc= mysql_query(mysql, "create table t1 (f1 int)");
 
14888
  myquery(rc);
 
14889
  rc= mysql_query(mysql, "create table t2 (f2 int, f3 numeric(8))");
 
14890
  myquery(rc);
 
14891
  rc= mysql_query(mysql, "insert into t1 values (1), (2)");
 
14892
  myquery(rc);
 
14893
  rc= mysql_query(mysql, "insert into t2 values (1,20050101), (2,20050202)");
 
14894
  myquery(rc);
 
14895
  rc= mysql_query(mysql, query);
 
14896
  myquery(rc);
 
14897
  res = mysql_store_result(mysql);
 
14898
 
 
14899
  if (!opt_silent)
 
14900
    printf("return type: %s", (res->fields[0].type == MYSQL_TYPE_DATE)?"DATE":
 
14901
           "not DATE");
 
14902
  DIE_UNLESS(res->fields[0].type == MYSQL_TYPE_DATE);
 
14903
  mysql_free_result(res);
 
14904
  rc= mysql_query(mysql, "drop table t1, t2");
 
14905
  myquery(rc);
 
14906
}
 
14907
 
 
14908
 
 
14909
/*
 
14910
  Bug #12925: Bad handling of maximum values in getopt
 
14911
*/
 
14912
static void test_bug12925()
 
14913
{
 
14914
  myheader("test_bug12925");
 
14915
  if (opt_getopt_ll_test)
 
14916
    DIE_UNLESS(opt_getopt_ll_test == LL(25600*1024*1024));
 
14917
}
 
14918
 
 
14919
 
 
14920
/*
 
14921
  Bug#14210 "Simple query with > operator on large table gives server
 
14922
  crash"
 
14923
*/
 
14924
 
 
14925
static void test_bug14210()
 
14926
{
 
14927
  MYSQL_STMT *stmt;
 
14928
  int rc, i;
 
14929
  const char *stmt_text;
 
14930
  ulong type;
 
14931
 
 
14932
  myheader("test_bug14210");
 
14933
 
 
14934
  mysql_query(mysql, "drop table if exists t1");
 
14935
  /*
 
14936
    To trigger the problem the table must be InnoDB, although the problem
 
14937
    itself is not InnoDB related. In case the table is MyISAM this test
 
14938
    is harmless.
 
14939
  */
 
14940
  mysql_query(mysql, "create table t1 (a varchar(255)) engine=InnoDB");
 
14941
  rc= mysql_query(mysql, "insert into t1 (a) values (repeat('a', 256))");
 
14942
  myquery(rc);
 
14943
  rc= mysql_query(mysql, "set @@session.max_heap_table_size=16384");
 
14944
  /* Create a big enough table (more than max_heap_table_size) */
 
14945
  for (i= 0; i < 8; i++)
 
14946
  {
 
14947
    rc= mysql_query(mysql, "insert into t1 (a) select a from t1");
 
14948
    myquery(rc);
 
14949
  }
 
14950
  /* create statement */
 
14951
  stmt= mysql_stmt_init(mysql);
 
14952
  type= (ulong) CURSOR_TYPE_READ_ONLY;
 
14953
  mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
14954
 
 
14955
  stmt_text= "select a from t1";
 
14956
 
 
14957
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
14958
  check_execute(stmt, rc);
 
14959
  rc= mysql_stmt_execute(stmt);
 
14960
  while ((rc= mysql_stmt_fetch(stmt)) == 0)
 
14961
    ;
 
14962
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
14963
 
 
14964
  rc= mysql_stmt_close(stmt);
 
14965
 
 
14966
  rc= mysql_query(mysql, "drop table t1");
 
14967
  myquery(rc);
 
14968
  rc= mysql_query(mysql, "set @@session.max_heap_table_size=default");
 
14969
  myquery(rc);
 
14970
}
 
14971
 
 
14972
/* Bug#13488: wrong column metadata when fetching from cursor */
 
14973
 
 
14974
static void test_bug13488()
 
14975
{
 
14976
  MYSQL_BIND my_bind[3];
 
14977
  MYSQL_STMT *stmt1;
 
14978
  int rc, f1, f2, f3, i;
 
14979
  const ulong type= CURSOR_TYPE_READ_ONLY;
 
14980
  const char *query= "select * from t1 left join t2 on f1=f2 where f1=1";
 
14981
 
 
14982
  myheader("test_bug13488");
 
14983
 
 
14984
  rc= mysql_query(mysql, "drop table if exists t1, t2");
 
14985
  myquery(rc);
 
14986
  rc= mysql_query(mysql, "create table t1 (f1 int not null primary key)");
 
14987
  myquery(rc);
 
14988
  rc= mysql_query(mysql, "create table t2 (f2 int not null primary key, "
 
14989
                  "f3 int not null)");
 
14990
  myquery(rc);
 
14991
  rc= mysql_query(mysql, "insert into t1 values (1), (2)");
 
14992
  myquery(rc);
 
14993
  rc= mysql_query(mysql, "insert into t2 values (1,2), (2,4)");
 
14994
  myquery(rc);
 
14995
 
 
14996
  memset(my_bind, 0, sizeof(my_bind));
 
14997
  for (i= 0; i < 3; i++)
 
14998
  {
 
14999
    my_bind[i].buffer_type= MYSQL_TYPE_LONG;
 
15000
    my_bind[i].buffer_length= 4;
 
15001
    my_bind[i].length= 0;
 
15002
  }
 
15003
  my_bind[0].buffer=&f1;
 
15004
  my_bind[1].buffer=&f2;
 
15005
  my_bind[2].buffer=&f3;
 
15006
 
 
15007
  stmt1= mysql_stmt_init(mysql);
 
15008
  rc= mysql_stmt_attr_set(stmt1,STMT_ATTR_CURSOR_TYPE, (const void *)&type);
 
15009
  check_execute(stmt1, rc);
 
15010
 
 
15011
  rc= mysql_stmt_prepare(stmt1, query, strlen(query));
 
15012
  check_execute(stmt1, rc);
 
15013
 
 
15014
  rc= mysql_stmt_execute(stmt1);
 
15015
  check_execute(stmt1, rc);
 
15016
 
 
15017
  rc= mysql_stmt_bind_result(stmt1, my_bind);
 
15018
  check_execute(stmt1, rc);
 
15019
 
 
15020
  rc= mysql_stmt_fetch(stmt1);
 
15021
  check_execute(stmt1, rc);
 
15022
 
 
15023
  rc= mysql_stmt_free_result(stmt1);
 
15024
  check_execute(stmt1, rc);
 
15025
 
 
15026
  rc= mysql_stmt_reset(stmt1);
 
15027
  check_execute(stmt1, rc);
 
15028
 
 
15029
  rc= mysql_stmt_close(stmt1);
 
15030
  check_execute(stmt1, rc);
 
15031
 
 
15032
  if (!opt_silent)
 
15033
    printf("data is: %s", (f1 == 1 && f2 == 1 && f3 == 2)?"OK":
 
15034
           "wrong");
 
15035
  DIE_UNLESS(f1 == 1 && f2 == 1 && f3 == 2);
 
15036
  rc= mysql_query(mysql, "drop table t1, t2");
 
15037
  myquery(rc);
 
15038
}
 
15039
 
 
15040
/*
 
15041
  Bug#13524: warnings of a previous command are not reset when fetching
 
15042
  from a cursor.
 
15043
*/
 
15044
 
 
15045
static void test_bug13524()
 
15046
{
 
15047
  MYSQL_STMT *stmt;
 
15048
  int rc;
 
15049
  unsigned int warning_count;
 
15050
  const ulong type= CURSOR_TYPE_READ_ONLY;
 
15051
  const char *query= "select * from t1";
 
15052
 
 
15053
  myheader("test_bug13524");
 
15054
 
 
15055
  rc= mysql_query(mysql, "drop table if exists t1, t2");
 
15056
  myquery(rc);
 
15057
  rc= mysql_query(mysql, "create table t1 (a int not null primary key)");
 
15058
  myquery(rc);
 
15059
  rc= mysql_query(mysql, "insert into t1 values (1), (2), (3), (4)");
 
15060
  myquery(rc);
 
15061
 
 
15062
  stmt= mysql_stmt_init(mysql);
 
15063
  rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
15064
  check_execute(stmt, rc);
 
15065
 
 
15066
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
15067
  check_execute(stmt, rc);
 
15068
 
 
15069
  rc= mysql_stmt_execute(stmt);
 
15070
  check_execute(stmt, rc);
 
15071
 
 
15072
  rc= mysql_stmt_fetch(stmt);
 
15073
  check_execute(stmt, rc);
 
15074
 
 
15075
  warning_count= mysql_warning_count(mysql);
 
15076
  DIE_UNLESS(warning_count == 0);
 
15077
 
 
15078
  /* Check that DROP TABLE produced a warning (no such table) */
 
15079
  rc= mysql_query(mysql, "drop table if exists t2");
 
15080
  myquery(rc);
 
15081
  warning_count= mysql_warning_count(mysql);
 
15082
  DIE_UNLESS(warning_count == 1);
 
15083
 
 
15084
  /*
 
15085
    Check that fetch from a cursor cleared the warning from the previous
 
15086
    command.
 
15087
  */
 
15088
  rc= mysql_stmt_fetch(stmt);
 
15089
  check_execute(stmt, rc);
 
15090
  warning_count= mysql_warning_count(mysql);
 
15091
  DIE_UNLESS(warning_count == 0);
 
15092
 
 
15093
  /* Cleanup */
 
15094
  mysql_stmt_close(stmt);
 
15095
  rc= mysql_query(mysql, "drop table t1");
 
15096
  myquery(rc);
 
15097
}
 
15098
 
 
15099
/*
 
15100
  Bug#14845 "mysql_stmt_fetch returns MYSQL_NO_DATA when COUNT(*) is 0"
 
15101
*/
 
15102
 
 
15103
static void test_bug14845()
 
15104
{
 
15105
  MYSQL_STMT *stmt;
 
15106
  int rc;
 
15107
  const ulong type= CURSOR_TYPE_READ_ONLY;
 
15108
  const char *query= "select count(*) from t1 where 1 = 0";
 
15109
 
 
15110
  myheader("test_bug14845");
 
15111
 
 
15112
  rc= mysql_query(mysql, "drop table if exists t1");
 
15113
  myquery(rc);
 
15114
  rc= mysql_query(mysql, "create table t1 (id int(11) default null, "
 
15115
                         "name varchar(20) default null)"
 
15116
                         "engine=MyISAM DEFAULT CHARSET=utf8");
 
15117
  myquery(rc);
 
15118
  rc= mysql_query(mysql, "insert into t1 values (1,'abc'),(2,'def')");
 
15119
  myquery(rc);
 
15120
 
 
15121
  stmt= mysql_stmt_init(mysql);
 
15122
  rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
15123
  check_execute(stmt, rc);
 
15124
 
 
15125
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
15126
  check_execute(stmt, rc);
 
15127
 
 
15128
  rc= mysql_stmt_execute(stmt);
 
15129
  check_execute(stmt, rc);
 
15130
 
 
15131
  rc= mysql_stmt_fetch(stmt);
 
15132
  DIE_UNLESS(rc == 0);
 
15133
 
 
15134
  rc= mysql_stmt_fetch(stmt);
 
15135
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
15136
 
 
15137
  /* Cleanup */
 
15138
  mysql_stmt_close(stmt);
 
15139
  rc= mysql_query(mysql, "drop table t1");
 
15140
  myquery(rc);
 
15141
}
 
15142
 
 
15143
 
 
15144
/*
 
15145
  Bug #15510: mysql_warning_count returns 0 after mysql_stmt_fetch which
 
15146
  should warn
 
15147
*/
 
15148
static void test_bug15510()
 
15149
{
 
15150
  MYSQL_STMT *stmt;
 
15151
  int rc;
 
15152
  const char *query= "select 1 from dual where 1/0";
 
15153
 
 
15154
  myheader("test_bug15510");
 
15155
 
 
15156
  rc= mysql_query(mysql, "set @@sql_mode='ERROR_FOR_DIVISION_BY_ZERO'");
 
15157
  myquery(rc);
 
15158
 
 
15159
  stmt= mysql_stmt_init(mysql);
 
15160
 
 
15161
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
15162
  check_execute(stmt, rc);
 
15163
 
 
15164
  rc= mysql_stmt_execute(stmt);
 
15165
  check_execute(stmt, rc);
 
15166
 
 
15167
  rc= mysql_stmt_fetch(stmt);
 
15168
  DIE_UNLESS(mysql_warning_count(mysql));
 
15169
 
 
15170
  /* Cleanup */
 
15171
  mysql_stmt_close(stmt);
 
15172
  rc= mysql_query(mysql, "set @@sql_mode=''");
 
15173
  myquery(rc);
 
15174
}
 
15175
 
 
15176
 
 
15177
/* Test MYSQL_OPT_RECONNECT, Bug#15719 */
 
15178
 
 
15179
static void test_opt_reconnect()
 
15180
{
 
15181
  MYSQL *lmysql;
 
15182
  my_bool my_true= TRUE;
 
15183
 
 
15184
  myheader("test_opt_reconnect");
 
15185
 
 
15186
  if (!(lmysql= mysql_init(NULL)))
 
15187
  {
 
15188
    myerror("mysql_init() failed");
 
15189
    exit(1);
 
15190
  }
 
15191
 
 
15192
  if (!opt_silent)
 
15193
    fprintf(stdout, "reconnect before mysql_options: %d\n", lmysql->reconnect);
 
15194
  DIE_UNLESS(lmysql->reconnect == 0);
 
15195
 
 
15196
  if (mysql_options(lmysql, MYSQL_OPT_RECONNECT, &my_true))
 
15197
  {
 
15198
    myerror("mysql_options failed: unknown option MYSQL_OPT_RECONNECT\n");
 
15199
    DIE_UNLESS(0);
 
15200
  }
 
15201
 
 
15202
  /* reconnect should be 1 */
 
15203
  if (!opt_silent)
 
15204
    fprintf(stdout, "reconnect after mysql_options: %d\n", lmysql->reconnect);
 
15205
  DIE_UNLESS(lmysql->reconnect == 1);
 
15206
 
 
15207
  if (!(mysql_real_connect(lmysql, opt_host, opt_user,
 
15208
                           opt_password, current_db, opt_port,
 
15209
                           opt_unix_socket, 0)))
 
15210
  {
 
15211
    myerror("connection failed");
 
15212
    DIE_UNLESS(0);
 
15213
  }
 
15214
 
 
15215
  /* reconnect should still be 1 */
 
15216
  if (!opt_silent)
 
15217
    fprintf(stdout, "reconnect after mysql_real_connect: %d\n",
 
15218
            lmysql->reconnect);
 
15219
  DIE_UNLESS(lmysql->reconnect == 1);
 
15220
 
 
15221
  mysql_close(lmysql);
 
15222
 
 
15223
  if (!(lmysql= mysql_init(NULL)))
 
15224
  {
 
15225
    myerror("mysql_init() failed");
 
15226
    DIE_UNLESS(0);
 
15227
  }
 
15228
 
 
15229
  if (!opt_silent)
 
15230
    fprintf(stdout, "reconnect before mysql_real_connect: %d\n", lmysql->reconnect);
 
15231
  DIE_UNLESS(lmysql->reconnect == 0);
 
15232
 
 
15233
  if (!(mysql_real_connect(lmysql, opt_host, opt_user,
 
15234
                           opt_password, current_db, opt_port,
 
15235
                           opt_unix_socket, 0)))
 
15236
  {
 
15237
    myerror("connection failed");
 
15238
    DIE_UNLESS(0);
 
15239
  }
 
15240
 
 
15241
  /* reconnect should still be 0 */
 
15242
  if (!opt_silent)
 
15243
    fprintf(stdout, "reconnect after mysql_real_connect: %d\n",
 
15244
            lmysql->reconnect);
 
15245
  DIE_UNLESS(lmysql->reconnect == 0);
 
15246
 
 
15247
  mysql_close(lmysql);
 
15248
}
 
15249
 
 
15250
 
 
15251
#ifndef EMBEDDED_LIBRARY
 
15252
 
 
15253
static void test_bug12744()
 
15254
{
 
15255
  MYSQL_STMT *prep_stmt = NULL;
 
15256
  MYSQL *lmysql;
 
15257
  int rc;
 
15258
  myheader("test_bug12744");
 
15259
 
 
15260
  lmysql= mysql_init(NULL);
 
15261
  DIE_UNLESS(lmysql);
 
15262
 
 
15263
  if (!mysql_real_connect(lmysql, opt_host, opt_user, opt_password,
 
15264
                          current_db, opt_port, opt_unix_socket, 0))
 
15265
  {
 
15266
    fprintf(stderr, "Failed to connect to the database\n");
 
15267
    DIE_UNLESS(0);
 
15268
  }
 
15269
 
 
15270
  prep_stmt= mysql_stmt_init(lmysql);
 
15271
  rc= mysql_stmt_prepare(prep_stmt, "SELECT 1", 8);
 
15272
  DIE_UNLESS(rc == 0);
 
15273
 
 
15274
  mysql_close(lmysql);
 
15275
 
 
15276
  rc= mysql_stmt_execute(prep_stmt);
 
15277
  DIE_UNLESS(rc);
 
15278
  rc= mysql_stmt_reset(prep_stmt);
 
15279
  DIE_UNLESS(rc);
 
15280
  rc= mysql_stmt_close(prep_stmt);
 
15281
  DIE_UNLESS(rc == 0);
 
15282
}
 
15283
 
 
15284
#endif /* EMBEDDED_LIBRARY */
 
15285
 
 
15286
/* Bug #16143: mysql_stmt_sqlstate returns an empty string instead of '00000' */
 
15287
 
 
15288
static void test_bug16143()
 
15289
{
 
15290
  MYSQL_STMT *stmt;
 
15291
  myheader("test_bug16143");
 
15292
 
 
15293
  stmt= mysql_stmt_init(mysql);
 
15294
  /* Check mysql_stmt_sqlstate return "no error" */
 
15295
  DIE_UNLESS(strcmp(mysql_stmt_sqlstate(stmt), "00000") == 0);
 
15296
 
 
15297
  mysql_stmt_close(stmt);
 
15298
}
 
15299
 
 
15300
 
 
15301
/* Bug #16144: mysql_stmt_attr_get type error */
 
15302
 
 
15303
static void test_bug16144()
 
15304
{
 
15305
  const my_bool flag_orig= (my_bool) 0xde;
 
15306
  my_bool flag= flag_orig;
 
15307
  MYSQL_STMT *stmt;
 
15308
  myheader("test_bug16144");
 
15309
 
 
15310
  /* Check that attr_get returns correct data on little and big endian CPUs */
 
15311
  stmt= mysql_stmt_init(mysql);
 
15312
  mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (const void*) &flag);
 
15313
  mysql_stmt_attr_get(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*) &flag);
 
15314
  DIE_UNLESS(flag == flag_orig);
 
15315
 
 
15316
  mysql_stmt_close(stmt);
 
15317
}
 
15318
 
 
15319
/*
 
15320
  Bug #15613: "libmysqlclient API function mysql_stmt_prepare returns wrong
 
15321
  field length"
 
15322
*/
 
15323
 
 
15324
static void test_bug15613()
 
15325
{
 
15326
  MYSQL_STMT *stmt;
 
15327
  const char *stmt_text;
 
15328
  MYSQL_RES *metadata;
 
15329
  MYSQL_FIELD *field;
 
15330
  int rc;
 
15331
  myheader("test_bug15613");
 
15332
 
 
15333
  /* I. Prepare the table */
 
15334
  rc= mysql_query(mysql, "set names latin1");
 
15335
  myquery(rc);
 
15336
  mysql_query(mysql, "drop table if exists t1");
 
15337
  rc= mysql_query(mysql,
 
15338
                  "create table t1 (t text character set utf8, "
 
15339
                                   "tt tinytext character set utf8, "
 
15340
                                   "mt mediumtext character set utf8, "
 
15341
                                   "lt longtext character set utf8, "
 
15342
                                   "vl varchar(255) character set latin1,"
 
15343
                                   "vb varchar(255) character set binary,"
 
15344
                                   "vu varchar(255) character set utf8)");
 
15345
  myquery(rc);
 
15346
 
 
15347
  stmt= mysql_stmt_init(mysql);
 
15348
 
 
15349
  /* II. Check SELECT metadata */
 
15350
  stmt_text= ("select t, tt, mt, lt, vl, vb, vu from t1");
 
15351
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
15352
  metadata= mysql_stmt_result_metadata(stmt);
 
15353
  field= mysql_fetch_fields(metadata);
 
15354
  if (!opt_silent)
 
15355
  {
 
15356
    printf("Field lengths (client character set is latin1):\n"
 
15357
           "text character set utf8:\t\t%lu\n"
 
15358
           "tinytext character set utf8:\t\t%lu\n"
 
15359
           "mediumtext character set utf8:\t\t%lu\n"
 
15360
           "longtext character set utf8:\t\t%lu\n"
 
15361
           "varchar(255) character set latin1:\t%lu\n"
 
15362
           "varchar(255) character set binary:\t%lu\n"
 
15363
           "varchar(255) character set utf8:\t%lu\n",
 
15364
           field[0].length, field[1].length, field[2].length, field[3].length,
 
15365
           field[4].length, field[5].length, field[6].length);
 
15366
  }
 
15367
  DIE_UNLESS(field[0].length == 65535);
 
15368
  DIE_UNLESS(field[1].length == 255);
 
15369
  DIE_UNLESS(field[2].length == 16777215);
 
15370
  DIE_UNLESS(field[3].length == 4294967295UL);
 
15371
  DIE_UNLESS(field[4].length == 255);
 
15372
  DIE_UNLESS(field[5].length == 255);
 
15373
  DIE_UNLESS(field[6].length == 255);
 
15374
  mysql_free_result(metadata);
 
15375
  mysql_stmt_free_result(stmt);
 
15376
 
 
15377
  /* III. Cleanup */
 
15378
  rc= mysql_query(mysql, "drop table t1");
 
15379
  myquery(rc);
 
15380
  rc= mysql_query(mysql, "set names default");
 
15381
  myquery(rc);
 
15382
  mysql_stmt_close(stmt);
 
15383
}
 
15384
 
 
15385
/*
 
15386
  Bug#17667: An attacker has the opportunity to bypass query logging.
 
15387
 
 
15388
  Note! Also tests Bug#21813, where prepared statements are used to
 
15389
  run queries
 
15390
*/
 
15391
static void test_bug17667()
 
15392
{
 
15393
  int rc;
 
15394
  MYSQL_STMT *stmt;
 
15395
  enum query_type { QT_NORMAL, QT_PREPARED};
 
15396
  struct buffer_and_length {
 
15397
    enum query_type qt;
 
15398
    const char *buffer;
 
15399
    const uint length;
 
15400
  } statements[]= {
 
15401
    { QT_NORMAL, "drop table if exists bug17667", 29 },
 
15402
    { QT_NORMAL, "create table bug17667 (c varchar(20))", 37 },
 
15403
    { QT_NORMAL, "insert into bug17667 (c) values ('regular') /* NUL=\0 with comment */", 68 },
 
15404
    { QT_PREPARED,
 
15405
      "insert into bug17667 (c) values ('prepared') /* NUL=\0 with comment */", 69, },
 
15406
    { QT_NORMAL, "insert into bug17667 (c) values ('NUL=\0 in value')", 50 },
 
15407
    { QT_NORMAL, "insert into bug17667 (c) values ('5 NULs=\0\0\0\0\0')", 48 },
 
15408
    { QT_PREPARED, "insert into bug17667 (c) values ('6 NULs=\0\0\0\0\0\0')", 50 },
 
15409
    { QT_NORMAL, "/* NUL=\0 with comment */ insert into bug17667 (c) values ('encore')", 67 },
 
15410
    { QT_NORMAL, "drop table bug17667", 19 },
 
15411
    { QT_NORMAL, NULL, 0 } };
 
15412
 
 
15413
  struct buffer_and_length *statement_cursor;
 
15414
  FILE *log_file;
 
15415
  char *master_log_filename;
 
15416
 
 
15417
  myheader("test_bug17667");
 
15418
 
 
15419
  master_log_filename = (char *) malloc(strlen(opt_vardir) + strlen("/log/master.log") + 1);
 
15420
  strxmov(master_log_filename, opt_vardir, "/log/master.log", NullS);
 
15421
  if (!opt_silent)
 
15422
    printf("Opening '%s'\n", master_log_filename);
 
15423
  log_file= my_fopen(master_log_filename, (int) (O_RDONLY | O_BINARY), MYF(0));
 
15424
  free(master_log_filename);
 
15425
 
 
15426
  if (log_file == NULL)
 
15427
  {
 
15428
    if (!opt_silent)
 
15429
    {
 
15430
      printf("Could not find the log file, VARDIR/log/master.log, so "
 
15431
             "test_bug17667 is not run.\n"
 
15432
             "Run test from the mysql-test/mysql-test-run* program to set up "
 
15433
             "correct environment for this test.\n\n");
 
15434
    }
 
15435
    return;
 
15436
  }
 
15437
 
 
15438
  enable_general_log(1);
 
15439
 
 
15440
  for (statement_cursor= statements; statement_cursor->buffer != NULL;
 
15441
       statement_cursor++)
 
15442
  {
 
15443
    if (statement_cursor->qt == QT_NORMAL)
 
15444
    {
 
15445
      /* Run statement as normal query */
 
15446
      rc= mysql_real_query(mysql, statement_cursor->buffer,
 
15447
                           statement_cursor->length);
 
15448
      myquery(rc);
 
15449
    }
 
15450
    else if (statement_cursor->qt == QT_PREPARED)
 
15451
    {
 
15452
      /*
 
15453
        Run as prepared statement
 
15454
 
 
15455
        NOTE! All these queries should be in the log twice,
 
15456
        one time for prepare and one time for execute
 
15457
      */
 
15458
      stmt= mysql_stmt_init(mysql);
 
15459
 
 
15460
      rc= mysql_stmt_prepare(stmt, statement_cursor->buffer,
 
15461
                             statement_cursor->length);
 
15462
      check_execute(stmt, rc);
 
15463
 
 
15464
      rc= mysql_stmt_execute(stmt);
 
15465
      check_execute(stmt, rc);
 
15466
 
 
15467
      mysql_stmt_close(stmt);
 
15468
    }
 
15469
    else
 
15470
    {
 
15471
      DIE_UNLESS(0==1);
 
15472
    }
 
15473
  }
 
15474
 
 
15475
  /* Make sure the server has written the logs to disk before reading it */
 
15476
  rc= mysql_query(mysql, "flush logs");
 
15477
  myquery(rc);
 
15478
 
 
15479
  for (statement_cursor= statements; statement_cursor->buffer != NULL;
 
15480
       statement_cursor++)
 
15481
  {
 
15482
    int expected_hits= 1, hits= 0;
 
15483
    char line_buffer[MAX_TEST_QUERY_LENGTH*2];
 
15484
    /* more than enough room for the query and some marginalia. */
 
15485
 
 
15486
    /* Prepared statments always occurs twice in log */
 
15487
    if (statement_cursor->qt == QT_PREPARED)
 
15488
      expected_hits++;
 
15489
 
 
15490
    /* Loop until we found expected number of log entries */
 
15491
    do {
 
15492
      /* Loop until statement is found in log */
 
15493
      do {
 
15494
        memset(line_buffer, '/', MAX_TEST_QUERY_LENGTH*2);
 
15495
 
 
15496
        if(fgets(line_buffer, MAX_TEST_QUERY_LENGTH*2, log_file) == NULL)
 
15497
        {
 
15498
          /* If fgets returned NULL, it indicates either error or EOF */
 
15499
          if (feof(log_file))
 
15500
            DIE("Found EOF before all statements where found");
 
15501
 
 
15502
          fprintf(stderr, "Got error %d while reading from file\n",
 
15503
                  ferror(log_file));
 
15504
          DIE("Read error");
 
15505
        }
 
15506
 
 
15507
      } while (my_memmem(line_buffer, MAX_TEST_QUERY_LENGTH*2,
 
15508
                         statement_cursor->buffer,
 
15509
                         statement_cursor->length) == NULL);
 
15510
      hits++;
 
15511
    } while (hits < expected_hits);
 
15512
 
 
15513
    if (!opt_silent)
 
15514
      printf("Found statement starting with \"%s\"\n",
 
15515
             statement_cursor->buffer);
 
15516
  }
 
15517
 
 
15518
  restore_general_log();
 
15519
 
 
15520
  if (!opt_silent)
 
15521
    printf("success.  All queries found intact in the log.\n");
 
15522
 
 
15523
  my_fclose(log_file, MYF(0));
 
15524
}
 
15525
 
 
15526
 
 
15527
/*
 
15528
  Bug#14169: type of group_concat() result changed to blob if tmp_table was
 
15529
  used
 
15530
*/
 
15531
static void test_bug14169()
 
15532
{
 
15533
  MYSQL_STMT *stmt;
 
15534
  const char *stmt_text;
 
15535
  MYSQL_RES *res;
 
15536
  MYSQL_FIELD *field;
 
15537
  int rc;
 
15538
 
 
15539
  myheader("test_bug14169");
 
15540
 
 
15541
  rc= mysql_query(mysql, "drop table if exists t1");
 
15542
  myquery(rc);
 
15543
  rc= mysql_query(mysql, "set session group_concat_max_len=1024");
 
15544
  myquery(rc);
 
15545
  rc= mysql_query(mysql, "create table t1 (f1 int unsigned, f2 varchar(255))");
 
15546
  myquery(rc);
 
15547
  rc= mysql_query(mysql, "insert into t1 values (1,repeat('a',255)),"
 
15548
                         "(2,repeat('b',255))");
 
15549
  myquery(rc);
 
15550
  stmt= mysql_stmt_init(mysql);
 
15551
  stmt_text= "select f2,group_concat(f1) from t1 group by f2";
 
15552
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
15553
  myquery(rc);
 
15554
  res= mysql_stmt_result_metadata(stmt);
 
15555
  field= mysql_fetch_fields(res);
 
15556
  if (!opt_silent)
 
15557
    printf("GROUP_CONCAT() result type %i", field[1].type);
 
15558
  DIE_UNLESS(field[1].type == MYSQL_TYPE_BLOB);
 
15559
  mysql_free_result(res);
 
15560
  mysql_stmt_free_result(stmt);
 
15561
  mysql_stmt_close(stmt);
 
15562
 
 
15563
  rc= mysql_query(mysql, "drop table t1");
 
15564
  myquery(rc);
 
15565
}
 
15566
 
 
15567
/*
 
15568
   Test that mysql_insert_id() behaves as documented in our manual
 
15569
*/
 
15570
static void test_mysql_insert_id()
 
15571
{
 
15572
  my_ulonglong res;
 
15573
  int rc;
 
15574
 
 
15575
  myheader("test_mysql_insert_id");
 
15576
 
 
15577
  rc= mysql_query(mysql, "drop table if exists t1");
 
15578
  myquery(rc);
 
15579
  /* table without auto_increment column */
 
15580
  rc= mysql_query(mysql, "create table t1 (f1 int, f2 varchar(255), key(f1))");
 
15581
  myquery(rc);
 
15582
  rc= mysql_query(mysql, "insert into t1 values (1,'a')");
 
15583
  myquery(rc);
 
15584
  res= mysql_insert_id(mysql);
 
15585
  DIE_UNLESS(res == 0);
 
15586
  rc= mysql_query(mysql, "insert into t1 values (null,'b')");
 
15587
  myquery(rc);
 
15588
  res= mysql_insert_id(mysql);
 
15589
  DIE_UNLESS(res == 0);
 
15590
  rc= mysql_query(mysql, "insert into t1 select 5,'c'");
 
15591
  myquery(rc);
 
15592
  res= mysql_insert_id(mysql);
 
15593
  DIE_UNLESS(res == 0);
 
15594
 
 
15595
  /*
 
15596
    Test for bug #34889: mysql_client_test::test_mysql_insert_id test fails
 
15597
    sporadically
 
15598
  */
 
15599
  rc= mysql_query(mysql, "create table t2 (f1 int not null primary key auto_increment, f2 varchar(255))");
 
15600
  myquery(rc);
 
15601
  rc= mysql_query(mysql, "insert into t2 values (null,'b')");
 
15602
  myquery(rc);
 
15603
  rc= mysql_query(mysql, "insert into t1 select 5,'c'");
 
15604
  myquery(rc);
 
15605
  res= mysql_insert_id(mysql);
 
15606
  DIE_UNLESS(res == 0);
 
15607
  rc= mysql_query(mysql, "drop table t2");
 
15608
  myquery(rc);
 
15609
  
 
15610
  rc= mysql_query(mysql, "insert into t1 select null,'d'");
 
15611
  myquery(rc);
 
15612
  res= mysql_insert_id(mysql);
 
15613
  DIE_UNLESS(res == 0);
 
15614
  rc= mysql_query(mysql, "insert into t1 values (null,last_insert_id(300))");
 
15615
  myquery(rc);
 
15616
  res= mysql_insert_id(mysql);
 
15617
  DIE_UNLESS(res == 300);
 
15618
  rc= mysql_query(mysql, "insert into t1 select null,last_insert_id(400)");
 
15619
  myquery(rc);
 
15620
  res= mysql_insert_id(mysql);
 
15621
  /*
 
15622
    Behaviour change: old code used to return 0; but 400 is consistent
 
15623
    with INSERT VALUES, and the manual's section of mysql_insert_id() does not
 
15624
    say INSERT SELECT should be different.
 
15625
  */
 
15626
  DIE_UNLESS(res == 400);
 
15627
 
 
15628
  /* table with auto_increment column */
 
15629
  rc= mysql_query(mysql, "create table t2 (f1 int not null primary key auto_increment, f2 varchar(255))");
 
15630
  myquery(rc);
 
15631
  rc= mysql_query(mysql, "insert into t2 values (1,'a')");
 
15632
  myquery(rc);
 
15633
  res= mysql_insert_id(mysql);
 
15634
  DIE_UNLESS(res == 1);
 
15635
  /* this should not influence next INSERT if it doesn't have auto_inc */
 
15636
  rc= mysql_query(mysql, "insert into t1 values (10,'e')");
 
15637
  myquery(rc);
 
15638
  res= mysql_insert_id(mysql);
 
15639
  DIE_UNLESS(res == 0);
 
15640
 
 
15641
  rc= mysql_query(mysql, "insert into t2 values (null,'b')");
 
15642
  myquery(rc);
 
15643
  res= mysql_insert_id(mysql);
 
15644
  DIE_UNLESS(res == 2);
 
15645
  rc= mysql_query(mysql, "insert into t2 select 5,'c'");
 
15646
  myquery(rc);
 
15647
  res= mysql_insert_id(mysql);
 
15648
  /*
 
15649
    Manual says that for multirow insert this should have been 5, but does not
 
15650
    say for INSERT SELECT. This is a behaviour change: old code used to return
 
15651
    0. We try to be consistent with INSERT VALUES.
 
15652
  */
 
15653
  DIE_UNLESS(res == 5);
 
15654
  rc= mysql_query(mysql, "insert into t2 select null,'d'");
 
15655
  myquery(rc);
 
15656
  res= mysql_insert_id(mysql);
 
15657
  DIE_UNLESS(res == 6);
 
15658
  /* with more than one row */
 
15659
  rc= mysql_query(mysql, "insert into t2 values (10,'a'),(11,'b')");
 
15660
  myquery(rc);
 
15661
  res= mysql_insert_id(mysql);
 
15662
  DIE_UNLESS(res == 11);
 
15663
  rc= mysql_query(mysql, "insert into t2 select 12,'a' union select 13,'b'");
 
15664
  myquery(rc);
 
15665
  res= mysql_insert_id(mysql);
 
15666
  /*
 
15667
    Manual says that for multirow insert this should have been 13, but does
 
15668
    not say for INSERT SELECT. This is a behaviour change: old code used to
 
15669
    return 0. We try to be consistent with INSERT VALUES.
 
15670
  */
 
15671
  DIE_UNLESS(res == 13);
 
15672
  rc= mysql_query(mysql, "insert into t2 values (null,'a'),(null,'b')");
 
15673
  myquery(rc);
 
15674
  res= mysql_insert_id(mysql);
 
15675
  DIE_UNLESS(res == 14);
 
15676
  rc= mysql_query(mysql, "insert into t2 select null,'a' union select null,'b'");
 
15677
  myquery(rc);
 
15678
  res= mysql_insert_id(mysql);
 
15679
  DIE_UNLESS(res == 16);
 
15680
  rc= mysql_query(mysql, "insert into t2 select 12,'a' union select 13,'b'");
 
15681
  myquery_r(rc);
 
15682
  rc= mysql_query(mysql, "insert ignore into t2 select 12,'a' union select 13,'b'");
 
15683
  myquery(rc);
 
15684
  res= mysql_insert_id(mysql);
 
15685
  DIE_UNLESS(res == 0);
 
15686
  rc= mysql_query(mysql, "insert into t2 values (12,'a'),(13,'b')");
 
15687
  myquery_r(rc);
 
15688
  res= mysql_insert_id(mysql);
 
15689
  DIE_UNLESS(res == 0);
 
15690
  rc= mysql_query(mysql, "insert ignore into t2 values (12,'a'),(13,'b')");
 
15691
  myquery(rc);
 
15692
  res= mysql_insert_id(mysql);
 
15693
  DIE_UNLESS(res == 0);
 
15694
  /* mixing autogenerated and explicit values */
 
15695
  rc= mysql_query(mysql, "insert into t2 values (null,'e'),(12,'a'),(13,'b')");
 
15696
  myquery_r(rc);
 
15697
  rc= mysql_query(mysql, "insert into t2 values (null,'e'),(12,'a'),(13,'b'),(25,'g')");
 
15698
  myquery_r(rc);
 
15699
  rc= mysql_query(mysql, "insert into t2 values (null,last_insert_id(300))");
 
15700
  myquery(rc);
 
15701
  res= mysql_insert_id(mysql);
 
15702
  /*
 
15703
    according to the manual, this might be 20 or 300, but it looks like
 
15704
    auto_increment column takes priority over last_insert_id().
 
15705
  */
 
15706
  DIE_UNLESS(res == 20);
 
15707
  /* If first autogenerated number fails and 2nd works: */
 
15708
  rc= mysql_query(mysql, "drop table t2");
 
15709
  myquery(rc);
 
15710
  rc= mysql_query(mysql, "create table t2 (f1 int not null primary key "
 
15711
                  "auto_increment, f2 varchar(255), unique (f2))");
 
15712
  myquery(rc);
 
15713
  rc= mysql_query(mysql, "insert into t2 values (null,'e')");
 
15714
  res= mysql_insert_id(mysql);
 
15715
  DIE_UNLESS(res == 1);
 
15716
  rc= mysql_query(mysql, "insert ignore into t2 values (null,'e'),(null,'a'),(null,'e')");
 
15717
  myquery(rc);
 
15718
  res= mysql_insert_id(mysql);
 
15719
  DIE_UNLESS(res == 2);
 
15720
  /* If autogenerated fails and explicit works: */
 
15721
  rc= mysql_query(mysql, "insert ignore into t2 values (null,'e'),(12,'c'),(null,'d')");
 
15722
  myquery(rc);
 
15723
  res= mysql_insert_id(mysql);
 
15724
  /*
 
15725
    Behaviour change: old code returned 3 (first autogenerated, even if it
 
15726
    fails); we now return first successful autogenerated.
 
15727
  */
 
15728
  DIE_UNLESS(res == 13);
 
15729
  /* UPDATE may update mysql_insert_id() if it uses LAST_INSERT_ID(#) */
 
15730
  rc= mysql_query(mysql, "update t2 set f1=14 where f1=12");
 
15731
  myquery(rc);
 
15732
  res= mysql_insert_id(mysql);
 
15733
  DIE_UNLESS(res == 0);
 
15734
  rc= mysql_query(mysql, "update t2 set f1=0 where f1=14");
 
15735
  myquery(rc);
 
15736
  res= mysql_insert_id(mysql);
 
15737
  DIE_UNLESS(res == 0);
 
15738
  rc= mysql_query(mysql, "update t2 set f2=last_insert_id(372) where f1=0");
 
15739
  myquery(rc);
 
15740
  res= mysql_insert_id(mysql);
 
15741
  DIE_UNLESS(res == 372);
 
15742
  /* check that LAST_INSERT_ID() does not update mysql_insert_id(): */
 
15743
  rc= mysql_query(mysql, "insert into t2 values (null,'g')");
 
15744
  myquery(rc);
 
15745
  res= mysql_insert_id(mysql);
 
15746
  DIE_UNLESS(res == 15);
 
15747
  rc= mysql_query(mysql, "update t2 set f2=(@li:=last_insert_id()) where f1=15");
 
15748
  myquery(rc);
 
15749
  res= mysql_insert_id(mysql);
 
15750
  DIE_UNLESS(res == 0);
 
15751
  /*
 
15752
    Behaviour change: now if ON DUPLICATE KEY UPDATE updates a row,
 
15753
    mysql_insert_id() returns the id of the row, instead of not being
 
15754
    affected.
 
15755
  */
 
15756
  rc= mysql_query(mysql, "insert into t2 values (null,@li) on duplicate key "
 
15757
                  "update f2=concat('we updated ',f2)");
 
15758
  myquery(rc);
 
15759
  res= mysql_insert_id(mysql);
 
15760
  DIE_UNLESS(res == 15);
 
15761
 
 
15762
  rc= mysql_query(mysql, "drop table t1,t2");
 
15763
  myquery(rc);
 
15764
}
 
15765
 
 
15766
/*
 
15767
  Bug#20152: mysql_stmt_execute() writes to MYSQL_TYPE_DATE buffer
 
15768
*/
 
15769
 
 
15770
static void test_bug20152()
 
15771
{
 
15772
  MYSQL_BIND my_bind[1];
 
15773
  MYSQL_STMT *stmt;
 
15774
  MYSQL_TIME tm;
 
15775
  int rc;
 
15776
  const char *query= "INSERT INTO t1 (f1) VALUES (?)";
 
15777
 
 
15778
  myheader("test_bug20152");
 
15779
 
 
15780
  memset(my_bind, 0, sizeof(my_bind));
 
15781
  my_bind[0].buffer_type= MYSQL_TYPE_DATE;
 
15782
  my_bind[0].buffer= (void*)&tm;
 
15783
 
 
15784
  tm.year = 2006;
 
15785
  tm.month = 6;
 
15786
  tm.day = 18;
 
15787
  tm.hour = 14;
 
15788
  tm.minute = 9;
 
15789
  tm.second = 42;
 
15790
 
 
15791
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
15792
  myquery(rc);
 
15793
  rc= mysql_query(mysql, "CREATE TABLE t1 (f1 DATE)");
 
15794
  myquery(rc);
 
15795
 
 
15796
  stmt= mysql_stmt_init(mysql);
 
15797
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
15798
  check_execute(stmt, rc);
 
15799
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
15800
  check_execute(stmt, rc);
 
15801
  rc= mysql_stmt_execute(stmt);
 
15802
  check_execute(stmt, rc);
 
15803
  rc= mysql_stmt_close(stmt);
 
15804
  check_execute(stmt, rc);
 
15805
  rc= mysql_query(mysql, "DROP TABLE t1");
 
15806
  myquery(rc);
 
15807
 
 
15808
  if (tm.hour == 14 && tm.minute == 9 && tm.second == 42) {
 
15809
    if (!opt_silent)
 
15810
      printf("OK!");
 
15811
  } else {
 
15812
    printf("[14:09:42] != [%02d:%02d:%02d]\n", tm.hour, tm.minute, tm.second);
 
15813
    DIE_UNLESS(0==1);
 
15814
  }
 
15815
}
 
15816
 
 
15817
/* Bug#15752 "Lost connection to MySQL server when calling a SP from C API" */
 
15818
 
 
15819
static void test_bug15752()
 
15820
{
 
15821
  MYSQL mysql_local;
 
15822
  int rc, i;
 
15823
  const int ITERATION_COUNT= 100;
 
15824
  const char *query= "CALL p1()";
 
15825
 
 
15826
  myheader("test_bug15752");
 
15827
 
 
15828
  rc= mysql_query(mysql, "drop procedure if exists p1");
 
15829
  myquery(rc);
 
15830
  rc= mysql_query(mysql, "create procedure p1() select 1");
 
15831
  myquery(rc);
 
15832
 
 
15833
  mysql_init(&mysql_local);
 
15834
  if (! mysql_real_connect(&mysql_local, opt_host, opt_user,
 
15835
                           opt_password, current_db, opt_port,
 
15836
                           opt_unix_socket,
 
15837
                           CLIENT_MULTI_STATEMENTS|CLIENT_MULTI_RESULTS))
 
15838
  {
 
15839
    printf("Unable connect to MySQL server: %s\n", mysql_error(&mysql_local));
 
15840
    DIE_UNLESS(0);
 
15841
  }
 
15842
  rc= mysql_real_query(&mysql_local, query, strlen(query));
 
15843
  myquery(rc);
 
15844
  mysql_free_result(mysql_store_result(&mysql_local));
 
15845
 
 
15846
  rc= mysql_real_query(&mysql_local, query, strlen(query));
 
15847
  DIE_UNLESS(rc && mysql_errno(&mysql_local) == CR_COMMANDS_OUT_OF_SYNC);
 
15848
 
 
15849
  if (! opt_silent)
 
15850
    printf("Got error (as expected): %s\n", mysql_error(&mysql_local));
 
15851
 
 
15852
  /* Check some other commands too */
 
15853
 
 
15854
  DIE_UNLESS(mysql_next_result(&mysql_local) == 0);
 
15855
  mysql_free_result(mysql_store_result(&mysql_local));
 
15856
  DIE_UNLESS(mysql_next_result(&mysql_local) == -1);
 
15857
 
 
15858
  /* The second problem is not reproducible: add the test case */
 
15859
  for (i = 0; i < ITERATION_COUNT; i++)
 
15860
  {
 
15861
    if (mysql_real_query(&mysql_local, query, strlen(query)))
 
15862
    {
 
15863
      printf("\ni=%d %s failed: %s\n", i, query, mysql_error(&mysql_local));
 
15864
      break;
 
15865
    }
 
15866
    mysql_free_result(mysql_store_result(&mysql_local));
 
15867
    DIE_UNLESS(mysql_next_result(&mysql_local) == 0);
 
15868
    mysql_free_result(mysql_store_result(&mysql_local));
 
15869
    DIE_UNLESS(mysql_next_result(&mysql_local) == -1);
 
15870
 
 
15871
  }
 
15872
  mysql_close(&mysql_local);
 
15873
  rc= mysql_query(mysql, "drop procedure p1");
 
15874
  myquery(rc);
 
15875
}
 
15876
 
 
15877
/*
 
15878
  Bug#21206: memory corruption when too many cursors are opened at once
 
15879
 
 
15880
  Memory corruption happens when more than 1024 cursors are open
 
15881
  simultaneously.
 
15882
*/
 
15883
static void test_bug21206()
 
15884
{
 
15885
  const size_t cursor_count= 1025;
 
15886
 
 
15887
  const char *create_table[]=
 
15888
  {
 
15889
    "DROP TABLE IF EXISTS t1",
 
15890
    "CREATE TABLE t1 (i INT)",
 
15891
    "INSERT INTO t1 VALUES (1), (2), (3)"
 
15892
  };
 
15893
  const char *query= "SELECT * FROM t1";
 
15894
 
 
15895
  Stmt_fetch *fetch_array=
 
15896
    (Stmt_fetch*) calloc(cursor_count, sizeof(Stmt_fetch));
 
15897
 
 
15898
  Stmt_fetch *fetch;
 
15899
 
 
15900
  DBUG_ENTER("test_bug21206");
 
15901
  myheader("test_bug21206");
 
15902
 
 
15903
  fill_tables(create_table, sizeof(create_table) / sizeof(*create_table));
 
15904
 
 
15905
  for (fetch= fetch_array; fetch < fetch_array + cursor_count; ++fetch)
 
15906
  {
 
15907
    /* Init will exit(1) in case of error */
 
15908
    stmt_fetch_init(fetch, fetch - fetch_array, query);
 
15909
  }
 
15910
 
 
15911
  for (fetch= fetch_array; fetch < fetch_array + cursor_count; ++fetch)
 
15912
    stmt_fetch_close(fetch);
 
15913
 
 
15914
  free(fetch_array);
 
15915
 
 
15916
  DBUG_VOID_RETURN;
 
15917
}
 
15918
 
 
15919
/*
 
15920
  Ensure we execute the status code while testing
 
15921
*/
 
15922
 
 
15923
static void test_status()
 
15924
{
 
15925
  const char *status;
 
15926
  DBUG_ENTER("test_status");
 
15927
  myheader("test_status");
 
15928
 
 
15929
  if (!(status= mysql_stat(mysql)))
 
15930
  {
 
15931
    myerror("mysql_stat failed");                 /* purecov: inspected */
 
15932
    die(__FILE__, __LINE__, "mysql_stat failed"); /* purecov: inspected */
 
15933
  }
 
15934
  DBUG_VOID_RETURN;
 
15935
}
 
15936
 
 
15937
/*
 
15938
  Bug#21726: Incorrect result with multiple invocations of
 
15939
  LAST_INSERT_ID
 
15940
 
 
15941
  Test that client gets updated value of insert_id on UPDATE that uses
 
15942
  LAST_INSERT_ID(expr).
 
15943
  select_query added to test for bug
 
15944
    #26921 Problem in mysql_insert_id() Embedded C API function
 
15945
*/
 
15946
static void test_bug21726()
 
15947
{
 
15948
  const char *create_table[]=
 
15949
  {
 
15950
    "DROP TABLE IF EXISTS t1",
 
15951
    "CREATE TABLE t1 (i INT)",
 
15952
    "INSERT INTO t1 VALUES (1)",
 
15953
  };
 
15954
  const char *update_query= "UPDATE t1 SET i= LAST_INSERT_ID(i + 1)";
 
15955
  int rc;
 
15956
  my_ulonglong insert_id;
 
15957
  const char *select_query= "SELECT * FROM t1";
 
15958
  MYSQL_RES  *result;
 
15959
 
 
15960
  DBUG_ENTER("test_bug21726");
 
15961
  myheader("test_bug21726");
 
15962
 
 
15963
  fill_tables(create_table, sizeof(create_table) / sizeof(*create_table));
 
15964
 
 
15965
  rc= mysql_query(mysql, update_query);
 
15966
  myquery(rc);
 
15967
  insert_id= mysql_insert_id(mysql);
 
15968
  DIE_UNLESS(insert_id == 2);
 
15969
 
 
15970
  rc= mysql_query(mysql, update_query);
 
15971
  myquery(rc);
 
15972
  insert_id= mysql_insert_id(mysql);
 
15973
  DIE_UNLESS(insert_id == 3);
 
15974
 
 
15975
  rc= mysql_query(mysql, select_query);
 
15976
  myquery(rc);
 
15977
  insert_id= mysql_insert_id(mysql);
 
15978
  DIE_UNLESS(insert_id == 3);
 
15979
  result= mysql_store_result(mysql);
 
15980
  mysql_free_result(result);
 
15981
 
 
15982
  DBUG_VOID_RETURN;
 
15983
}
 
15984
 
 
15985
 
 
15986
/*
 
15987
  BUG#23383: mysql_affected_rows() returns different values than
 
15988
  mysql_stmt_affected_rows()
 
15989
 
 
15990
  Test that both mysql_affected_rows() and mysql_stmt_affected_rows()
 
15991
  return -1 on error, 0 when no rows were affected, and (positive) row
 
15992
  count when some rows were affected.
 
15993
*/
 
15994
static void test_bug23383()
 
15995
{
 
15996
  const char *insert_query= "INSERT INTO t1 VALUES (1), (2)";
 
15997
  const char *update_query= "UPDATE t1 SET i= 4 WHERE i = 3";
 
15998
  MYSQL_STMT *stmt;
 
15999
  my_ulonglong row_count;
 
16000
  int rc;
 
16001
 
 
16002
  DBUG_ENTER("test_bug23383");
 
16003
  myheader("test_bug23383");
 
16004
 
 
16005
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
16006
  myquery(rc);
 
16007
 
 
16008
  rc= mysql_query(mysql, "CREATE TABLE t1 (i INT UNIQUE)");
 
16009
  myquery(rc);
 
16010
 
 
16011
  rc= mysql_query(mysql, insert_query);
 
16012
  myquery(rc);
 
16013
  row_count= mysql_affected_rows(mysql);
 
16014
  DIE_UNLESS(row_count == 2);
 
16015
 
 
16016
  rc= mysql_query(mysql, insert_query);
 
16017
  DIE_UNLESS(rc != 0);
 
16018
  row_count= mysql_affected_rows(mysql);
 
16019
  DIE_UNLESS(row_count == (my_ulonglong)-1);
 
16020
 
 
16021
  rc= mysql_query(mysql, update_query);
 
16022
  myquery(rc);
 
16023
  row_count= mysql_affected_rows(mysql);
 
16024
  DIE_UNLESS(row_count == 0);
 
16025
 
 
16026
  rc= mysql_query(mysql, "DELETE FROM t1");
 
16027
  myquery(rc);
 
16028
 
 
16029
  stmt= mysql_stmt_init(mysql);
 
16030
  DIE_UNLESS(stmt != 0);
 
16031
 
 
16032
  rc= mysql_stmt_prepare(stmt, insert_query, strlen(insert_query));
 
16033
  check_execute(stmt, rc);
 
16034
 
 
16035
  rc= mysql_stmt_execute(stmt);
 
16036
  check_execute(stmt, rc);
 
16037
  row_count= mysql_stmt_affected_rows(stmt);
 
16038
  DIE_UNLESS(row_count == 2);
 
16039
 
 
16040
  rc= mysql_stmt_execute(stmt);
 
16041
  DIE_UNLESS(rc != 0);
 
16042
  row_count= mysql_stmt_affected_rows(stmt);
 
16043
  DIE_UNLESS(row_count == (my_ulonglong)-1);
 
16044
 
 
16045
  rc= mysql_stmt_prepare(stmt, update_query, strlen(update_query));
 
16046
  check_execute(stmt, rc);
 
16047
 
 
16048
  rc= mysql_stmt_execute(stmt);
 
16049
  check_execute(stmt, rc);
 
16050
  row_count= mysql_stmt_affected_rows(stmt);
 
16051
  DIE_UNLESS(row_count == 0);
 
16052
 
 
16053
  rc= mysql_stmt_close(stmt);
 
16054
  check_execute(stmt, rc);
 
16055
 
 
16056
  rc= mysql_query(mysql, "DROP TABLE t1");
 
16057
  myquery(rc);
 
16058
 
 
16059
  DBUG_VOID_RETURN;
 
16060
}
 
16061
 
 
16062
 
 
16063
/*
 
16064
  BUG#21635: MYSQL_FIELD struct's member strings seem to misbehave for
 
16065
  expression cols
 
16066
 
 
16067
  Check that for MIN(), MAX(), COUNT() only MYSQL_FIELD::name is set
 
16068
  to either expression or its alias, and db, org_table, table,
 
16069
  org_name fields are empty strings.
 
16070
*/
 
16071
static void test_bug21635()
 
16072
{
 
16073
  const char *expr[]=
 
16074
  {
 
16075
    "MIN(i)", "MIN(i)",
 
16076
    "MIN(i) AS A1", "A1",
 
16077
    "MAX(i)", "MAX(i)",
 
16078
    "MAX(i) AS A2", "A2",
 
16079
    "COUNT(i)", "COUNT(i)",
 
16080
    "COUNT(i) AS A3", "A3",
 
16081
  };
 
16082
  char query[MAX_TEST_QUERY_LENGTH];
 
16083
  char *query_end;
 
16084
  MYSQL_RES *result;
 
16085
  MYSQL_FIELD *field;
 
16086
  unsigned int field_count, i, j;
 
16087
  int rc;
 
16088
 
 
16089
  DBUG_ENTER("test_bug21635");
 
16090
  myheader("test_bug21635");
 
16091
 
 
16092
  query_end= strxmov(query, "SELECT ", NullS);
 
16093
  for (i= 0; i < sizeof(expr) / sizeof(*expr) / 2; ++i)
 
16094
    query_end= strxmov(query_end, expr[i * 2], ", ", NullS);
 
16095
  query_end= strxmov(query_end - 2, " FROM t1 GROUP BY i", NullS);
 
16096
  DIE_UNLESS(query_end - query < MAX_TEST_QUERY_LENGTH);
 
16097
 
 
16098
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
16099
  myquery(rc);
 
16100
  rc= mysql_query(mysql, "CREATE TABLE t1 (i INT)");
 
16101
  myquery(rc);
 
16102
  /*
 
16103
    We need this loop to ensure correct behavior with both constant and
 
16104
    non-constant tables.
 
16105
  */
 
16106
  for (j= 0; j < 2 ; j++)
 
16107
  {
 
16108
    rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1)");
 
16109
    myquery(rc);
 
16110
 
 
16111
    rc= mysql_real_query(mysql, query, query_end - query);
 
16112
    myquery(rc);
 
16113
 
 
16114
    result= mysql_use_result(mysql);
 
16115
    DIE_UNLESS(result);
 
16116
 
 
16117
  field_count= mysql_field_count(mysql);
 
16118
  for (i= 0; i < field_count; ++i)
 
16119
  {
 
16120
    field= mysql_fetch_field_direct(result, i);
 
16121
    if (!opt_silent)
 
16122
      if (!opt_silent)
 
16123
        printf("%s -> %s ... ", expr[i * 2], field->name);
 
16124
    fflush(stdout);
 
16125
    DIE_UNLESS(field->db[0] == 0 && field->org_table[0] == 0 &&
 
16126
               field->table[0] == 0 && field->org_name[0] == 0);
 
16127
    DIE_UNLESS(strcmp(field->name, expr[i * 2 + 1]) == 0);
 
16128
    if (!opt_silent)
 
16129
      if (!opt_silent)
 
16130
        puts("OK");
 
16131
  }
 
16132
 
 
16133
    mysql_free_result(result);
 
16134
  }
 
16135
  rc= mysql_query(mysql, "DROP TABLE t1");
 
16136
  myquery(rc);
 
16137
 
 
16138
  DBUG_VOID_RETURN;
 
16139
}
 
16140
 
 
16141
/*
 
16142
  Bug#24179 "select b into $var" fails with --cursor_protocol"
 
16143
  The failure is correct, check that the returned message is meaningful.
 
16144
*/
 
16145
 
 
16146
static void test_bug24179()
 
16147
{
 
16148
  int rc;
 
16149
  MYSQL_STMT *stmt;
 
16150
 
 
16151
  DBUG_ENTER("test_bug24179");
 
16152
  myheader("test_bug24179");
 
16153
 
 
16154
  stmt= open_cursor("select 1 into @a");
 
16155
  rc= mysql_stmt_execute(stmt);
 
16156
  DIE_UNLESS(rc);
 
16157
  if (!opt_silent)
 
16158
  {
 
16159
    printf("Got error (as expected): %d %s\n",
 
16160
           mysql_stmt_errno(stmt),
 
16161
           mysql_stmt_error(stmt));
 
16162
  }
 
16163
  DIE_UNLESS(mysql_stmt_errno(stmt) == 1323);
 
16164
  mysql_stmt_close(stmt);
 
16165
 
 
16166
  DBUG_VOID_RETURN;
 
16167
}
 
16168
 
 
16169
 
 
16170
/**
 
16171
  Bug#32265 Server returns different metadata if prepared statement is used
 
16172
*/
 
16173
 
 
16174
static void test_bug32265()
 
16175
{
 
16176
  int rc;
 
16177
  MYSQL_STMT *stmt;
 
16178
  MYSQL_FIELD *field;
 
16179
  MYSQL_RES *metadata;
 
16180
 
 
16181
  DBUG_ENTER("test_bug32265");
 
16182
  myheader("test_bug32265");
 
16183
 
 
16184
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
16185
  myquery(rc);
 
16186
  rc= mysql_query(mysql, "CREATE  TABLE t1 (a INTEGER)");
 
16187
  myquery(rc);
 
16188
  rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1)");
 
16189
  myquery(rc);
 
16190
  rc= mysql_query(mysql, "CREATE VIEW v1 AS SELECT * FROM t1");
 
16191
  myquery(rc);
 
16192
 
 
16193
  stmt= open_cursor("SELECT * FROM t1");
 
16194
  rc= mysql_stmt_execute(stmt);
 
16195
  check_execute(stmt, rc);
 
16196
 
 
16197
  metadata= mysql_stmt_result_metadata(stmt);
 
16198
  field= mysql_fetch_field(metadata);
 
16199
  DIE_UNLESS(field);
 
16200
  DIE_UNLESS(strcmp(field->table, "t1") == 0);
 
16201
  DIE_UNLESS(strcmp(field->org_table, "t1") == 0);
 
16202
  DIE_UNLESS(strcmp(field->db, "client_test_db") == 0);
 
16203
  mysql_free_result(metadata);
 
16204
  mysql_stmt_close(stmt);
 
16205
 
 
16206
  stmt= open_cursor("SELECT a '' FROM t1 ``");
 
16207
  rc= mysql_stmt_execute(stmt);
 
16208
  check_execute(stmt, rc);
 
16209
 
 
16210
  metadata= mysql_stmt_result_metadata(stmt);
 
16211
  field= mysql_fetch_field(metadata);
 
16212
  DIE_UNLESS(strcmp(field->table, "") == 0);
 
16213
  DIE_UNLESS(strcmp(field->org_table, "t1") == 0);
 
16214
  DIE_UNLESS(strcmp(field->db, "client_test_db") == 0);
 
16215
  mysql_free_result(metadata);
 
16216
  mysql_stmt_close(stmt);
 
16217
 
 
16218
  stmt= open_cursor("SELECT a '' FROM t1 ``");
 
16219
  rc= mysql_stmt_execute(stmt);
 
16220
  check_execute(stmt, rc);
 
16221
 
 
16222
  metadata= mysql_stmt_result_metadata(stmt);
 
16223
  field= mysql_fetch_field(metadata);
 
16224
  DIE_UNLESS(strcmp(field->table, "") == 0);
 
16225
  DIE_UNLESS(strcmp(field->org_table, "t1") == 0);
 
16226
  DIE_UNLESS(strcmp(field->db, "client_test_db") == 0);
 
16227
  mysql_free_result(metadata);
 
16228
  mysql_stmt_close(stmt);
 
16229
 
 
16230
  stmt= open_cursor("SELECT * FROM v1");
 
16231
  rc= mysql_stmt_execute(stmt);
 
16232
  check_execute(stmt, rc);
 
16233
 
 
16234
  metadata= mysql_stmt_result_metadata(stmt);
 
16235
  field= mysql_fetch_field(metadata);
 
16236
  DIE_UNLESS(strcmp(field->table, "v1") == 0);
 
16237
  DIE_UNLESS(strcmp(field->org_table, "t1") == 0);
 
16238
  DIE_UNLESS(strcmp(field->db, "client_test_db") == 0);
 
16239
  mysql_free_result(metadata);
 
16240
  mysql_stmt_close(stmt);
 
16241
 
 
16242
  stmt= open_cursor("SELECT * FROM v1 /* SIC */ GROUP BY 1");
 
16243
  rc= mysql_stmt_execute(stmt);
 
16244
  check_execute(stmt, rc);
 
16245
 
 
16246
  metadata= mysql_stmt_result_metadata(stmt);
 
16247
  field= mysql_fetch_field(metadata);
 
16248
  DIE_UNLESS(strcmp(field->table, "v1") == 0);
 
16249
  DIE_UNLESS(strcmp(field->org_table, "t1") == 0);
 
16250
  DIE_UNLESS(strcmp(field->db, "client_test_db") == 0);
 
16251
  mysql_free_result(metadata);
 
16252
  mysql_stmt_close(stmt);
 
16253
 
 
16254
  rc= mysql_query(mysql, "DROP VIEW v1");
 
16255
  myquery(rc);
 
16256
  rc= mysql_query(mysql, "DROP TABLE t1");
 
16257
  myquery(rc);
 
16258
 
 
16259
  DBUG_VOID_RETURN;
 
16260
}
 
16261
 
 
16262
/*
 
16263
  Bug#28075 "COM_DEBUG crashes mysqld"
 
16264
*/
 
16265
 
 
16266
static void test_bug28075()
 
16267
{
 
16268
  int rc;
 
16269
 
 
16270
  DBUG_ENTER("test_bug28075");
 
16271
  myheader("test_bug28075");
 
16272
 
 
16273
  rc= mysql_dump_debug_info(mysql);
 
16274
  DIE_UNLESS(rc == 0);
 
16275
 
 
16276
  rc= mysql_ping(mysql);
 
16277
  DIE_UNLESS(rc == 0);
 
16278
 
 
16279
  DBUG_VOID_RETURN;
 
16280
}
 
16281
 
 
16282
 
 
16283
/*
 
16284
  Bug#27876 (SF with cyrillic variable name fails during execution (regression))
 
16285
*/
 
16286
 
 
16287
static void test_bug27876()
 
16288
{
 
16289
  int rc;
 
16290
  MYSQL_RES *result;
 
16291
 
 
16292
  uchar utf8_func[] =
 
16293
  {
 
16294
    0xd1, 0x84, 0xd1, 0x83, 0xd0, 0xbd, 0xd0, 0xba,
 
16295
    0xd1, 0x86, 0xd0, 0xb8, 0xd0, 0xb9, 0xd0, 0xba,
 
16296
    0xd0, 0xb0,
 
16297
    0x00
 
16298
  };
 
16299
 
 
16300
  uchar utf8_param[] =
 
16301
  {
 
16302
    0xd0, 0xbf, 0xd0, 0xb0, 0xd1, 0x80, 0xd0, 0xb0,
 
16303
    0xd0, 0xbc, 0xd0, 0xb5, 0xd1, 0x82, 0xd1, 0x8a,
 
16304
    0xd1, 0x80, 0x5f, 0xd0, 0xb2, 0xd0, 0xb5, 0xd1,
 
16305
    0x80, 0xd1, 0x81, 0xd0, 0xb8, 0xd1, 0x8f,
 
16306
    0x00
 
16307
  };
 
16308
 
 
16309
  char query[500];
 
16310
 
 
16311
  DBUG_ENTER("test_bug27876");
 
16312
  myheader("test_bug27876");
 
16313
 
 
16314
  rc= mysql_query(mysql, "set names utf8");
 
16315
  myquery(rc);
 
16316
 
 
16317
  rc= mysql_query(mysql, "select version()");
 
16318
  myquery(rc);
 
16319
  result= mysql_store_result(mysql);
 
16320
  mytest(result);
 
16321
  mysql_free_result(result);
 
16322
 
 
16323
  sprintf(query, "DROP FUNCTION IF EXISTS %s", (char*) utf8_func);
 
16324
  rc= mysql_query(mysql, query);
 
16325
  myquery(rc);
 
16326
 
 
16327
  sprintf(query,
 
16328
          "CREATE FUNCTION %s( %s VARCHAR(25))"
 
16329
          " RETURNS VARCHAR(25) DETERMINISTIC RETURN %s",
 
16330
          (char*) utf8_func, (char*) utf8_param, (char*) utf8_param);
 
16331
  rc= mysql_query(mysql, query);
 
16332
  myquery(rc);
 
16333
  sprintf(query, "SELECT %s(VERSION())", (char*) utf8_func);
 
16334
  rc= mysql_query(mysql, query);
 
16335
  myquery(rc);
 
16336
  result= mysql_store_result(mysql);
 
16337
  mytest(result);
 
16338
  mysql_free_result(result);
 
16339
 
 
16340
  sprintf(query, "DROP FUNCTION %s", (char*) utf8_func);
 
16341
  rc= mysql_query(mysql, query);
 
16342
  myquery(rc);
 
16343
 
 
16344
  rc= mysql_query(mysql, "set names default");
 
16345
  myquery(rc);
 
16346
}
 
16347
 
 
16348
 
 
16349
/*
 
16350
  Bug#28505: mysql_affected_rows() returns wrong value if CLIENT_FOUND_ROWS
 
16351
  flag is set.
 
16352
*/
 
16353
 
 
16354
static void test_bug28505()
 
16355
{
 
16356
  my_ulonglong res;
 
16357
 
 
16358
  myquery(mysql_query(mysql, "drop table if exists t1"));
 
16359
  myquery(mysql_query(mysql, "create table t1(f1 int primary key)"));
 
16360
  myquery(mysql_query(mysql, "insert into t1 values(1)"));
 
16361
  myquery(mysql_query(mysql,
 
16362
                  "insert into t1 values(1) on duplicate key update f1=1"));
 
16363
  res= mysql_affected_rows(mysql);
 
16364
  DIE_UNLESS(!res);
 
16365
  myquery(mysql_query(mysql, "drop table t1"));
 
16366
}
 
16367
 
 
16368
 
 
16369
/*
 
16370
  Bug#28934: server crash when receiving malformed com_execute packets
 
16371
*/
 
16372
 
 
16373
static void test_bug28934()
 
16374
{
 
16375
  my_bool error= 0;
 
16376
  MYSQL_BIND bind[5];
 
16377
  MYSQL_STMT *stmt;
 
16378
  int cnt;
 
16379
 
 
16380
  myquery(mysql_query(mysql, "drop table if exists t1"));
 
16381
  myquery(mysql_query(mysql, "create table t1(id int)"));
 
16382
 
 
16383
  myquery(mysql_query(mysql, "insert into t1 values(1),(2),(3),(4),(5)"));
 
16384
  stmt= mysql_simple_prepare(mysql,"select * from t1 where id in(?,?,?,?,?)");
 
16385
  check_stmt(stmt);
 
16386
 
 
16387
  memset (&bind, 0, sizeof (bind));
 
16388
  for (cnt= 0; cnt < 5; cnt++)
 
16389
  {
 
16390
    bind[cnt].buffer_type= MYSQL_TYPE_LONG;
 
16391
    bind[cnt].buffer= (char*)&cnt;
 
16392
    bind[cnt].buffer_length= 0;
 
16393
  }
 
16394
  myquery(mysql_stmt_bind_param(stmt, bind));
 
16395
 
 
16396
  stmt->param_count=2;
 
16397
  error= mysql_stmt_execute(stmt);
 
16398
  DIE_UNLESS(error != 0);
 
16399
  myerror(NULL);
 
16400
  mysql_stmt_close(stmt);
 
16401
 
 
16402
  myquery(mysql_query(mysql, "drop table t1"));
 
16403
}
 
16404
 
 
16405
/*
 
16406
  Test mysql_change_user() C API and COM_CHANGE_USER
 
16407
*/
 
16408
 
 
16409
static void test_change_user()
 
16410
{
 
16411
  char buff[256];
 
16412
  const char *user_pw= "mysqltest_pw";
 
16413
  const char *user_no_pw= "mysqltest_no_pw";
 
16414
  const char *pw= "password";
 
16415
  const char *db= "mysqltest_user_test_database";
 
16416
  int rc;
 
16417
 
 
16418
  DBUG_ENTER("test_change_user");
 
16419
  myheader("test_change_user");
 
16420
 
 
16421
  /* Prepare environment */
 
16422
  sprintf(buff, "drop database if exists %s", db);
 
16423
  rc= mysql_query(mysql, buff);
 
16424
  myquery(rc);
 
16425
 
 
16426
  sprintf(buff, "create database %s", db);
 
16427
  rc= mysql_query(mysql, buff);
 
16428
  myquery(rc);
 
16429
 
 
16430
  sprintf(buff,
 
16431
          "grant select on %s.* to %s@'%%' identified by '%s'",
 
16432
          db,
 
16433
          user_pw,
 
16434
          pw);
 
16435
  rc= mysql_query(mysql, buff);
 
16436
  myquery(rc);
 
16437
 
 
16438
  sprintf(buff,
 
16439
          "grant select on %s.* to %s@'%%'",
 
16440
          db,
 
16441
          user_no_pw);
 
16442
  rc= mysql_query(mysql, buff);
 
16443
  myquery(rc);
 
16444
 
 
16445
 
 
16446
  /* Try some combinations */
 
16447
  rc= mysql_change_user(mysql, NULL, NULL, NULL);
 
16448
  DIE_UNLESS(rc);
 
16449
  if (! opt_silent)
 
16450
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16451
 
 
16452
 
 
16453
  rc= mysql_change_user(mysql, "", NULL, NULL);
 
16454
  DIE_UNLESS(rc);
 
16455
  if (! opt_silent)
 
16456
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16457
 
 
16458
  rc= mysql_change_user(mysql, "", "", NULL);
 
16459
  DIE_UNLESS(rc);
 
16460
  if (! opt_silent)
 
16461
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16462
 
 
16463
  rc= mysql_change_user(mysql, "", "", "");
 
16464
  DIE_UNLESS(rc);
 
16465
  if (! opt_silent)
 
16466
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16467
 
 
16468
  rc= mysql_change_user(mysql, NULL, "", "");
 
16469
  DIE_UNLESS(rc);
 
16470
  if (! opt_silent)
 
16471
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16472
 
 
16473
 
 
16474
  rc= mysql_change_user(mysql, NULL, NULL, "");
 
16475
  DIE_UNLESS(rc);
 
16476
  if (! opt_silent)
 
16477
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16478
 
 
16479
  rc= mysql_change_user(mysql, "", NULL, "");
 
16480
  DIE_UNLESS(rc);
 
16481
  if (! opt_silent)
 
16482
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16483
 
 
16484
  rc= mysql_change_user(mysql, user_pw, NULL, "");
 
16485
  DIE_UNLESS(rc);
 
16486
  if (! opt_silent)
 
16487
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16488
 
 
16489
  rc= mysql_change_user(mysql, user_pw, "", "");
 
16490
  DIE_UNLESS(rc);
 
16491
  if (! opt_silent)
 
16492
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16493
 
 
16494
  rc= mysql_change_user(mysql, user_pw, "", NULL);
 
16495
  DIE_UNLESS(rc);
 
16496
  if (! opt_silent)
 
16497
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16498
 
 
16499
  rc= mysql_change_user(mysql, user_pw, NULL, NULL);
 
16500
  DIE_UNLESS(rc);
 
16501
  if (! opt_silent)
 
16502
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16503
 
 
16504
  rc= mysql_change_user(mysql, user_pw, "", db);
 
16505
  DIE_UNLESS(rc);
 
16506
  if (! opt_silent)
 
16507
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16508
 
 
16509
  rc= mysql_change_user(mysql, user_pw, NULL, db);
 
16510
  DIE_UNLESS(rc);
 
16511
  if (! opt_silent)
 
16512
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16513
 
 
16514
  rc= mysql_change_user(mysql, user_pw, pw, db);
 
16515
  myquery(rc);
 
16516
 
 
16517
  rc= mysql_change_user(mysql, user_pw, pw, NULL);
 
16518
  myquery(rc);
 
16519
 
 
16520
  rc= mysql_change_user(mysql, user_pw, pw, "");
 
16521
  myquery(rc);
 
16522
 
 
16523
  rc= mysql_change_user(mysql, user_no_pw, pw, db);
 
16524
  DIE_UNLESS(rc);
 
16525
  if (! opt_silent)
 
16526
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16527
 
 
16528
  rc= mysql_change_user(mysql, user_no_pw, pw, "");
 
16529
  DIE_UNLESS(rc);
 
16530
  if (! opt_silent)
 
16531
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16532
 
 
16533
  rc= mysql_change_user(mysql, user_no_pw, pw, NULL);
 
16534
  DIE_UNLESS(rc);
 
16535
  if (! opt_silent)
 
16536
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16537
 
 
16538
  rc= mysql_change_user(mysql, user_no_pw, "", NULL);
 
16539
  myquery(rc);
 
16540
 
 
16541
  rc= mysql_change_user(mysql, user_no_pw, "", "");
 
16542
  myquery(rc);
 
16543
 
 
16544
  rc= mysql_change_user(mysql, user_no_pw, "", db);
 
16545
  myquery(rc);
 
16546
 
 
16547
  rc= mysql_change_user(mysql, user_no_pw, NULL, db);
 
16548
  myquery(rc);
 
16549
 
 
16550
  rc= mysql_change_user(mysql, "", pw, db);
 
16551
  DIE_UNLESS(rc);
 
16552
  if (! opt_silent)
 
16553
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16554
 
 
16555
  rc= mysql_change_user(mysql, "", pw, "");
 
16556
  DIE_UNLESS(rc);
 
16557
  if (! opt_silent)
 
16558
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16559
 
 
16560
  rc= mysql_change_user(mysql, "", pw, NULL);
 
16561
  DIE_UNLESS(rc);
 
16562
  if (! opt_silent)
 
16563
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16564
 
 
16565
  rc= mysql_change_user(mysql, NULL, pw, NULL);
 
16566
  DIE_UNLESS(rc);
 
16567
  if (! opt_silent)
 
16568
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16569
 
 
16570
  rc= mysql_change_user(mysql, NULL, NULL, db);
 
16571
  DIE_UNLESS(rc);
 
16572
  if (! opt_silent)
 
16573
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16574
 
 
16575
  rc= mysql_change_user(mysql, NULL, "", db);
 
16576
  DIE_UNLESS(rc);
 
16577
  if (! opt_silent)
 
16578
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16579
 
 
16580
  rc= mysql_change_user(mysql, "", "", db);
 
16581
  DIE_UNLESS(rc);
 
16582
  if (! opt_silent)
 
16583
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16584
 
 
16585
  /* Cleanup the environment */
 
16586
 
 
16587
  mysql_change_user(mysql, opt_user, opt_password, current_db);
 
16588
 
 
16589
  sprintf(buff, "drop database %s", db);
 
16590
  rc= mysql_query(mysql, buff);
 
16591
  myquery(rc);
 
16592
 
 
16593
  sprintf(buff, "drop user %s@'%%'", user_pw);
 
16594
  rc= mysql_query(mysql, buff);
 
16595
  myquery(rc);
 
16596
 
 
16597
  sprintf(buff, "drop user %s@'%%'", user_no_pw);
 
16598
  rc= mysql_query(mysql, buff);
 
16599
  myquery(rc);
 
16600
 
 
16601
  DBUG_VOID_RETURN;
 
16602
}
 
16603
 
 
16604
/*
 
16605
  Bug#27592 (stack overrun when storing datetime value using prepared statements)
 
16606
*/
 
16607
 
 
16608
static void test_bug27592()
 
16609
{
 
16610
  const int NUM_ITERATIONS= 40;
 
16611
  int i;
 
16612
  int rc;
 
16613
  MYSQL_STMT *stmt= NULL;
 
16614
  MYSQL_BIND bind[1];
 
16615
  MYSQL_TIME time_val;
 
16616
 
 
16617
  DBUG_ENTER("test_bug27592");
 
16618
  myheader("test_bug27592");
 
16619
 
 
16620
  mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
16621
  mysql_query(mysql, "CREATE TABLE t1(c2 DATETIME)");
 
16622
 
 
16623
  stmt= mysql_simple_prepare(mysql, "INSERT INTO t1 VALUES (?)");
 
16624
  DIE_UNLESS(stmt);
 
16625
 
 
16626
  memset(bind, 0, sizeof(bind));
 
16627
 
 
16628
  bind[0].buffer_type= MYSQL_TYPE_DATETIME;
 
16629
  bind[0].buffer= (char *) &time_val;
 
16630
  bind[0].length= NULL;
 
16631
 
 
16632
  for (i= 0; i < NUM_ITERATIONS; i++)
 
16633
  {
 
16634
    time_val.year= 2007;
 
16635
    time_val.month= 6;
 
16636
    time_val.day= 7;
 
16637
    time_val.hour= 18;
 
16638
    time_val.minute= 41;
 
16639
    time_val.second= 3;
 
16640
 
 
16641
    time_val.second_part=0;
 
16642
    time_val.neg=0;
 
16643
 
 
16644
    rc= mysql_stmt_bind_param(stmt, bind);
 
16645
    check_execute(stmt, rc);
 
16646
 
 
16647
    rc= mysql_stmt_execute(stmt);
 
16648
    check_execute(stmt, rc);
 
16649
  }
 
16650
 
 
16651
  mysql_stmt_close(stmt);
 
16652
 
 
16653
  DBUG_VOID_RETURN;
 
16654
}
 
16655
 
 
16656
/*
 
16657
  Bug#29687 mysql_stmt_store_result memory leak in libmysqld
 
16658
*/
 
16659
 
 
16660
static void test_bug29687()
 
16661
{
 
16662
  const int NUM_ITERATIONS= 40;
 
16663
  int i;
 
16664
  int rc;
 
16665
  MYSQL_STMT *stmt= NULL;
 
16666
 
 
16667
  DBUG_ENTER("test_bug29687");
 
16668
  myheader("test_bug29687");
 
16669
 
 
16670
  stmt= mysql_simple_prepare(mysql, "SELECT 1 FROM dual WHERE 0=2");
 
16671
  DIE_UNLESS(stmt);
 
16672
 
 
16673
  for (i= 0; i < NUM_ITERATIONS; i++)
 
16674
  {
 
16675
    rc= mysql_stmt_execute(stmt);
 
16676
    check_execute(stmt, rc);
 
16677
    mysql_stmt_store_result(stmt);
 
16678
    while (mysql_stmt_fetch(stmt)==0);
 
16679
    mysql_stmt_free_result(stmt);
 
16680
  }
 
16681
 
 
16682
  mysql_stmt_close(stmt);
 
16683
  DBUG_VOID_RETURN;
 
16684
}
 
16685
 
 
16686
 
 
16687
/*
 
16688
  Bug #29692    Single row inserts can incorrectly report a huge number of 
 
16689
  row insertions
 
16690
*/
 
16691
 
 
16692
static void test_bug29692()
 
16693
{
 
16694
  MYSQL* conn;
 
16695
 
 
16696
  if (!(conn= mysql_init(NULL)))
 
16697
  {
 
16698
    myerror("test_bug29692 init failed");
 
16699
    exit(1);
 
16700
  }
 
16701
 
 
16702
  if (!(mysql_real_connect(conn, opt_host, opt_user,
 
16703
                           opt_password, opt_db ? opt_db:"test", opt_port,
 
16704
                           opt_unix_socket,  CLIENT_FOUND_ROWS)))
 
16705
  {
 
16706
    myerror("test_bug29692 connection failed");
 
16707
    mysql_close(mysql);
 
16708
    exit(1);
 
16709
  }
 
16710
  myquery(mysql_query(conn, "drop table if exists t1"));
 
16711
  myquery(mysql_query(conn, "create table t1(f1 int)"));
 
16712
  myquery(mysql_query(conn, "insert into t1 values(1)"));
 
16713
  DIE_UNLESS(1 == mysql_affected_rows(conn));
 
16714
  myquery(mysql_query(conn, "drop table t1"));
 
16715
  mysql_close(conn);
 
16716
}
 
16717
 
 
16718
/**
 
16719
  Bug#29306 Truncated data in MS Access with decimal (3,1) columns in a VIEW
 
16720
*/
 
16721
 
 
16722
static void test_bug29306()
 
16723
{
 
16724
  MYSQL_FIELD *field;
 
16725
  int rc;
 
16726
  MYSQL_RES *res;
 
16727
 
 
16728
  DBUG_ENTER("test_bug29306");
 
16729
  myheader("test_bug29306");
 
16730
 
 
16731
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS tab17557");
 
16732
  myquery(rc);
 
16733
  rc= mysql_query(mysql, "DROP VIEW IF EXISTS view17557");
 
16734
  myquery(rc);
 
16735
  rc= mysql_query(mysql, "CREATE TABLE tab17557 (dd decimal (3,1))");
 
16736
  myquery(rc);
 
16737
  rc= mysql_query(mysql, "CREATE VIEW view17557 as SELECT dd FROM tab17557");
 
16738
  myquery(rc);
 
16739
  rc= mysql_query(mysql, "INSERT INTO tab17557 VALUES (7.6)");
 
16740
  myquery(rc);
 
16741
 
 
16742
  /* Checking the view */
 
16743
  res= mysql_list_fields(mysql, "view17557", NULL);
 
16744
  while ((field= mysql_fetch_field(res)))
 
16745
  {
 
16746
    if (! opt_silent)
 
16747
    {
 
16748
      printf("field name %s\n", field->name);
 
16749
      printf("field table %s\n", field->table);
 
16750
      printf("field decimals %d\n", field->decimals);
 
16751
      if (field->decimals < 1)
 
16752
        printf("Error! No decimals! \n");
 
16753
      printf("\n\n");
 
16754
    }
 
16755
    DIE_UNLESS(field->decimals == 1);
 
16756
  }
 
16757
  mysql_free_result(res);
 
16758
 
 
16759
  rc= mysql_query(mysql, "DROP TABLE tab17557");
 
16760
  myquery(rc);
 
16761
  rc= mysql_query(mysql, "DROP VIEW view17557");
 
16762
  myquery(rc);
 
16763
 
 
16764
  DBUG_VOID_RETURN;
 
16765
}
 
16766
/*
 
16767
  Bug#30472: libmysql doesn't reset charset, insert_id after succ.
 
16768
  mysql_change_user() call row insertions.
 
16769
*/
 
16770
 
 
16771
static void bug30472_retrieve_charset_info(MYSQL *con,
 
16772
                                           char *character_set_name,
 
16773
                                           char *character_set_client,
 
16774
                                           char *character_set_results,
 
16775
                                           char *collation_connection)
 
16776
{
 
16777
  MYSQL_RES *rs;
 
16778
  MYSQL_ROW row;
 
16779
 
 
16780
  /* Get the cached client character set name. */
 
16781
 
 
16782
  strcpy(character_set_name, mysql_character_set_name(con));
 
16783
 
 
16784
  /* Retrieve server character set information. */
 
16785
 
 
16786
  DIE_IF(mysql_query(con, "SHOW VARIABLES LIKE 'character_set_client'"));
 
16787
  DIE_UNLESS(rs= mysql_store_result(con));
 
16788
  DIE_UNLESS(row= mysql_fetch_row(rs));
 
16789
  strcpy(character_set_client, row[1]);
 
16790
  mysql_free_result(rs);
 
16791
 
 
16792
  DIE_IF(mysql_query(con, "SHOW VARIABLES LIKE 'character_set_results'"));
 
16793
  DIE_UNLESS(rs= mysql_store_result(con));
 
16794
  DIE_UNLESS(row= mysql_fetch_row(rs));
 
16795
  strcpy(character_set_results, row[1]);
 
16796
  mysql_free_result(rs);
 
16797
 
 
16798
  DIE_IF(mysql_query(con, "SHOW VARIABLES LIKE 'collation_connection'"));
 
16799
  DIE_UNLESS(rs= mysql_store_result(con));
 
16800
  DIE_UNLESS(row= mysql_fetch_row(rs));
 
16801
  strcpy(collation_connection, row[1]);
 
16802
  mysql_free_result(rs);
 
16803
}
 
16804
 
 
16805
static void test_bug30472()
 
16806
{
 
16807
  MYSQL con;
 
16808
 
 
16809
  char character_set_name_1[MY_CS_NAME_SIZE];
 
16810
  char character_set_client_1[MY_CS_NAME_SIZE];
 
16811
  char character_set_results_1[MY_CS_NAME_SIZE];
 
16812
  char collation_connnection_1[MY_CS_NAME_SIZE];
 
16813
 
 
16814
  char character_set_name_2[MY_CS_NAME_SIZE];
 
16815
  char character_set_client_2[MY_CS_NAME_SIZE];
 
16816
  char character_set_results_2[MY_CS_NAME_SIZE];
 
16817
  char collation_connnection_2[MY_CS_NAME_SIZE];
 
16818
 
 
16819
  char character_set_name_3[MY_CS_NAME_SIZE];
 
16820
  char character_set_client_3[MY_CS_NAME_SIZE];
 
16821
  char character_set_results_3[MY_CS_NAME_SIZE];
 
16822
  char collation_connnection_3[MY_CS_NAME_SIZE];
 
16823
 
 
16824
  char character_set_name_4[MY_CS_NAME_SIZE];
 
16825
  char character_set_client_4[MY_CS_NAME_SIZE];
 
16826
  char character_set_results_4[MY_CS_NAME_SIZE];
 
16827
  char collation_connnection_4[MY_CS_NAME_SIZE];
 
16828
 
 
16829
  /* Create a new connection. */
 
16830
 
 
16831
  DIE_UNLESS(mysql_init(&con));
 
16832
 
 
16833
  DIE_UNLESS(mysql_real_connect(&con,
 
16834
                                opt_host,
 
16835
                                opt_user,
 
16836
                                opt_password,
 
16837
                                opt_db ? opt_db : "test",
 
16838
                                opt_port,
 
16839
                                opt_unix_socket,
 
16840
                                CLIENT_FOUND_ROWS));
 
16841
 
 
16842
  /* Retrieve character set information. */
 
16843
 
 
16844
  bug30472_retrieve_charset_info(&con,
 
16845
                                 character_set_name_1,
 
16846
                                 character_set_client_1,
 
16847
                                 character_set_results_1,
 
16848
                                 collation_connnection_1);
 
16849
 
 
16850
  /* Switch client character set. */
 
16851
 
 
16852
  DIE_IF(mysql_set_character_set(&con, "utf8"));
 
16853
 
 
16854
  /* Retrieve character set information. */
 
16855
 
 
16856
  bug30472_retrieve_charset_info(&con,
 
16857
                                 character_set_name_2,
 
16858
                                 character_set_client_2,
 
16859
                                 character_set_results_2,
 
16860
                                 collation_connnection_2);
 
16861
 
 
16862
  /*
 
16863
    Check that
 
16864
      1) character set has been switched and
 
16865
      2) new character set is different from the original one.
 
16866
  */
 
16867
 
 
16868
  DIE_UNLESS(strcmp(character_set_name_2, "utf8") == 0);
 
16869
  DIE_UNLESS(strcmp(character_set_client_2, "utf8") == 0);
 
16870
  DIE_UNLESS(strcmp(character_set_results_2, "utf8") == 0);
 
16871
  DIE_UNLESS(strcmp(collation_connnection_2, "utf8_general_ci") == 0);
 
16872
 
 
16873
  DIE_UNLESS(strcmp(character_set_name_1, character_set_name_2) != 0);
 
16874
  DIE_UNLESS(strcmp(character_set_client_1, character_set_client_2) != 0);
 
16875
  DIE_UNLESS(strcmp(character_set_results_1, character_set_results_2) != 0);
 
16876
  DIE_UNLESS(strcmp(collation_connnection_1, collation_connnection_2) != 0);
 
16877
 
 
16878
  /* Call mysql_change_user() with the same username, password, database. */
 
16879
 
 
16880
  DIE_IF(mysql_change_user(&con,
 
16881
                           opt_user,
 
16882
                           opt_password,
 
16883
                           opt_db ? opt_db : "test"));
 
16884
 
 
16885
  /* Retrieve character set information. */
 
16886
 
 
16887
  bug30472_retrieve_charset_info(&con,
 
16888
                                 character_set_name_3,
 
16889
                                 character_set_client_3,
 
16890
                                 character_set_results_3,
 
16891
                                 collation_connnection_3);
 
16892
 
 
16893
  /* Check that character set information has been reset. */
 
16894
 
 
16895
  DIE_UNLESS(strcmp(character_set_name_1, character_set_name_3) == 0);
 
16896
  DIE_UNLESS(strcmp(character_set_client_1, character_set_client_3) == 0);
 
16897
  DIE_UNLESS(strcmp(character_set_results_1, character_set_results_3) == 0);
 
16898
  DIE_UNLESS(strcmp(collation_connnection_1, collation_connnection_3) == 0);
 
16899
 
 
16900
  /* Change connection-default character set in the client. */
 
16901
 
 
16902
  mysql_options(&con, MYSQL_SET_CHARSET_NAME, "utf8");
 
16903
 
 
16904
  /*
 
16905
    Call mysql_change_user() in order to check that new connection will
 
16906
    have UTF8 character set on the client and on the server.
 
16907
  */
 
16908
 
 
16909
  DIE_IF(mysql_change_user(&con,
 
16910
                           opt_user,
 
16911
                           opt_password,
 
16912
                           opt_db ? opt_db : "test"));
 
16913
 
 
16914
  /* Retrieve character set information. */
 
16915
 
 
16916
  bug30472_retrieve_charset_info(&con,
 
16917
                                 character_set_name_4,
 
16918
                                 character_set_client_4,
 
16919
                                 character_set_results_4,
 
16920
                                 collation_connnection_4);
 
16921
 
 
16922
  /* Check that we have UTF8 on the server and on the client. */
 
16923
 
 
16924
  DIE_UNLESS(strcmp(character_set_name_4, "utf8") == 0);
 
16925
  DIE_UNLESS(strcmp(character_set_client_4, "utf8") == 0);
 
16926
  DIE_UNLESS(strcmp(character_set_results_4, "utf8") == 0);
 
16927
  DIE_UNLESS(strcmp(collation_connnection_4, "utf8_general_ci") == 0);
 
16928
 
 
16929
  /* That's it. Cleanup. */
 
16930
 
 
16931
  mysql_close(&con);
 
16932
}
 
16933
 
 
16934
static void bug20023_change_user(MYSQL *con)
 
16935
{
 
16936
  DIE_IF(mysql_change_user(con,
 
16937
                           opt_user,
 
16938
                           opt_password,
 
16939
                           opt_db ? opt_db : "test"));
 
16940
}
 
16941
 
 
16942
static my_bool query_int_variable(MYSQL *con,
 
16943
                                  const char *var_name,
 
16944
                                  int *var_value)
 
16945
{
 
16946
  MYSQL_RES *rs;
 
16947
  MYSQL_ROW row;
 
16948
 
 
16949
  char query_buffer[MAX_TEST_QUERY_LENGTH];
 
16950
 
 
16951
  my_bool is_null;
 
16952
 
 
16953
  my_snprintf(query_buffer,
 
16954
          sizeof (query_buffer),
 
16955
          "SELECT %s",
 
16956
          (const char *) var_name);
 
16957
 
 
16958
  DIE_IF(mysql_query(con, query_buffer));
 
16959
  DIE_UNLESS(rs= mysql_store_result(con));
 
16960
  DIE_UNLESS(row= mysql_fetch_row(rs));
 
16961
 
 
16962
  is_null= row[0] == NULL;
 
16963
 
 
16964
  if (!is_null)
 
16965
    *var_value= atoi(row[0]);
 
16966
 
 
16967
  mysql_free_result(rs);
 
16968
 
 
16969
  return is_null;
 
16970
}
 
16971
 
 
16972
static void test_bug20023()
 
16973
{
 
16974
  MYSQL con;
 
16975
 
 
16976
  int sql_big_selects_orig;
 
16977
  int max_join_size_orig;
 
16978
 
 
16979
  int sql_big_selects_2;
 
16980
  int sql_big_selects_3;
 
16981
  int sql_big_selects_4;
 
16982
  int sql_big_selects_5;
 
16983
 
 
16984
#if NOT_USED
 
16985
  char query_buffer[MAX_TEST_QUERY_LENGTH];
 
16986
#endif
 
16987
 
 
16988
  /* Create a new connection. */
 
16989
 
 
16990
  DIE_UNLESS(mysql_init(&con));
 
16991
 
 
16992
  DIE_UNLESS(mysql_real_connect(&con,
 
16993
                                opt_host,
 
16994
                                opt_user,
 
16995
                                opt_password,
 
16996
                                opt_db ? opt_db : "test",
 
16997
                                opt_port,
 
16998
                                opt_unix_socket,
 
16999
                                CLIENT_FOUND_ROWS));
 
17000
 
 
17001
  /***********************************************************************
 
17002
    Remember original SQL_BIG_SELECTS, MAX_JOIN_SIZE values.
 
17003
  ***********************************************************************/
 
17004
 
 
17005
  query_int_variable(&con,
 
17006
                     "@@session.sql_big_selects",
 
17007
                     &sql_big_selects_orig);
 
17008
 
 
17009
  query_int_variable(&con,
 
17010
                     "@@global.max_join_size",
 
17011
                     &max_join_size_orig);
 
17012
 
 
17013
  /***********************************************************************
 
17014
    Test that COM_CHANGE_USER resets the SQL_BIG_SELECTS to the initial value.
 
17015
  ***********************************************************************/
 
17016
 
 
17017
  /* Issue COM_CHANGE_USER. */
 
17018
 
 
17019
  bug20023_change_user(&con);
 
17020
 
 
17021
  /* Query SQL_BIG_SELECTS. */
 
17022
 
 
17023
  query_int_variable(&con,
 
17024
                     "@@session.sql_big_selects",
 
17025
                     &sql_big_selects_2);
 
17026
 
 
17027
  /* Check that SQL_BIG_SELECTS is reset properly. */
 
17028
 
 
17029
  DIE_UNLESS(sql_big_selects_orig == sql_big_selects_2);
 
17030
 
 
17031
  /***********************************************************************
 
17032
    Test that if MAX_JOIN_SIZE set to non-default value,
 
17033
    SQL_BIG_SELECTS will be 0.
 
17034
  ***********************************************************************/
 
17035
 
 
17036
  /* Set MAX_JOIN_SIZE to some non-default value. */
 
17037
 
 
17038
  DIE_IF(mysql_query(&con, "SET @@global.max_join_size = 10000"));
 
17039
  DIE_IF(mysql_query(&con, "SET @@session.max_join_size = default"));
 
17040
 
 
17041
  /* Issue COM_CHANGE_USER. */
 
17042
 
 
17043
  bug20023_change_user(&con);
 
17044
 
 
17045
  /* Query SQL_BIG_SELECTS. */
 
17046
 
 
17047
  query_int_variable(&con,
 
17048
                     "@@session.sql_big_selects",
 
17049
                     &sql_big_selects_3);
 
17050
 
 
17051
  /* Check that SQL_BIG_SELECTS is 0. */
 
17052
 
 
17053
  DIE_UNLESS(sql_big_selects_3 == 0);
 
17054
 
 
17055
  /***********************************************************************
 
17056
    Test that if MAX_JOIN_SIZE set to default value,
 
17057
    SQL_BIG_SELECTS will be 1.
 
17058
  ***********************************************************************/
 
17059
 
 
17060
  /* Set MAX_JOIN_SIZE to the default value (-1). */
 
17061
 
 
17062
  DIE_IF(mysql_query(&con, "SET @@global.max_join_size = -1"));
 
17063
  DIE_IF(mysql_query(&con, "SET @@session.max_join_size = default"));
 
17064
 
 
17065
  /* Issue COM_CHANGE_USER. */
 
17066
 
 
17067
  bug20023_change_user(&con);
 
17068
 
 
17069
  /* Query SQL_BIG_SELECTS. */
 
17070
 
 
17071
  query_int_variable(&con,
 
17072
                     "@@session.sql_big_selects",
 
17073
                     &sql_big_selects_4);
 
17074
 
 
17075
  /* Check that SQL_BIG_SELECTS is 1. */
 
17076
 
 
17077
  DIE_UNLESS(sql_big_selects_4 == 1);
 
17078
 
 
17079
  /***********************************************************************
 
17080
    Restore MAX_JOIN_SIZE.
 
17081
    Check that SQL_BIG_SELECTS will be the original one.
 
17082
  ***********************************************************************/
 
17083
 
 
17084
#if NOT_USED
 
17085
  /*
 
17086
    max_join_size is a ulong or better.
 
17087
    my_snprintf() only goes up to ul.
 
17088
  */
 
17089
 
 
17090
  /* Restore MAX_JOIN_SIZE. */
 
17091
 
 
17092
  my_snprintf(query_buffer,
 
17093
           sizeof (query_buffer),
 
17094
           "SET @@global.max_join_size = %d",
 
17095
           (int) max_join_size_orig);
 
17096
 
 
17097
  DIE_IF(mysql_query(&con, query_buffer));
 
17098
 
 
17099
#else
 
17100
  DIE_IF(mysql_query(&con, "SET @@global.max_join_size = -1"));
 
17101
#endif
 
17102
 
 
17103
  DIE_IF(mysql_query(&con, "SET @@session.max_join_size = default"));
 
17104
 
 
17105
  /* Issue COM_CHANGE_USER. */
 
17106
 
 
17107
  bug20023_change_user(&con);
 
17108
 
 
17109
  /* Query SQL_BIG_SELECTS. */
 
17110
 
 
17111
  query_int_variable(&con,
 
17112
                     "@@session.sql_big_selects",
 
17113
                     &sql_big_selects_5);
 
17114
 
 
17115
  /* Check that SQL_BIG_SELECTS is 1. */
 
17116
 
 
17117
  DIE_UNLESS(sql_big_selects_5 == sql_big_selects_orig);
 
17118
 
 
17119
  /***********************************************************************
 
17120
    That's it. Cleanup.
 
17121
  ***********************************************************************/
 
17122
 
 
17123
  mysql_close(&con);
 
17124
}
 
17125
 
 
17126
static void bug31418_impl()
 
17127
{
 
17128
  MYSQL con;
 
17129
 
 
17130
  my_bool is_null;
 
17131
  int rc;
 
17132
 
 
17133
  /* Create a new connection. */
 
17134
 
 
17135
  DIE_UNLESS(mysql_init(&con));
 
17136
 
 
17137
  DIE_UNLESS(mysql_real_connect(&con,
 
17138
                                opt_host,
 
17139
                                opt_user,
 
17140
                                opt_password,
 
17141
                                opt_db ? opt_db : "test",
 
17142
                                opt_port,
 
17143
                                opt_unix_socket,
 
17144
                                CLIENT_FOUND_ROWS));
 
17145
 
 
17146
  /***********************************************************************
 
17147
    Check that lock is free:
 
17148
      - IS_FREE_LOCK() should return 1;
 
17149
      - IS_USED_LOCK() should return NULL;
 
17150
  ***********************************************************************/
 
17151
 
 
17152
  is_null= query_int_variable(&con,
 
17153
                              "IS_FREE_LOCK('bug31418')",
 
17154
                              &rc);
 
17155
  DIE_UNLESS(!is_null && rc);
 
17156
 
 
17157
  is_null= query_int_variable(&con,
 
17158
                              "IS_USED_LOCK('bug31418')",
 
17159
                              &rc);
 
17160
  DIE_UNLESS(is_null);
 
17161
 
 
17162
  /***********************************************************************
 
17163
    Acquire lock and check the lock status (the lock must be in use):
 
17164
      - IS_FREE_LOCK() should return 0;
 
17165
      - IS_USED_LOCK() should return non-zero thread id;
 
17166
  ***********************************************************************/
 
17167
 
 
17168
  query_int_variable(&con, "GET_LOCK('bug31418', 1)", &rc);
 
17169
  DIE_UNLESS(rc);
 
17170
 
 
17171
  is_null= query_int_variable(&con,
 
17172
                              "IS_FREE_LOCK('bug31418')",
 
17173
                              &rc);
 
17174
  DIE_UNLESS(!is_null && !rc);
 
17175
 
 
17176
  is_null= query_int_variable(&con,
 
17177
                              "IS_USED_LOCK('bug31418')",
 
17178
                              &rc);
 
17179
  DIE_UNLESS(!is_null && rc);
 
17180
 
 
17181
  /***********************************************************************
 
17182
    Issue COM_CHANGE_USER command and check the lock status
 
17183
    (the lock must be free):
 
17184
      - IS_FREE_LOCK() should return 1;
 
17185
      - IS_USED_LOCK() should return NULL;
 
17186
  **********************************************************************/
 
17187
 
 
17188
  bug20023_change_user(&con);
 
17189
 
 
17190
  is_null= query_int_variable(&con,
 
17191
                              "IS_FREE_LOCK('bug31418')",
 
17192
                              &rc);
 
17193
  DIE_UNLESS(!is_null && rc);
 
17194
 
 
17195
  is_null= query_int_variable(&con,
 
17196
                              "IS_USED_LOCK('bug31418')",
 
17197
                              &rc);
 
17198
  DIE_UNLESS(is_null);
 
17199
 
 
17200
  /***********************************************************************
 
17201
   That's it. Cleanup.
 
17202
  ***********************************************************************/
 
17203
 
 
17204
  mysql_close(&con);
 
17205
}
 
17206
 
 
17207
static void test_bug31418()
 
17208
{
 
17209
  /* Run test case for BUG#31418 for three different connections. */
 
17210
 
 
17211
  bug31418_impl();
 
17212
 
 
17213
  bug31418_impl();
 
17214
 
 
17215
  bug31418_impl();
 
17216
}
 
17217
 
 
17218
 
 
17219
 
 
17220
/**
 
17221
  Bug#31669 Buffer overflow in mysql_change_user()
 
17222
*/
 
17223
 
 
17224
#define LARGE_BUFFER_SIZE 2048
 
17225
 
 
17226
static void test_bug31669()
 
17227
{
 
17228
  int rc;
 
17229
  static char buff[LARGE_BUFFER_SIZE+1];
 
17230
#ifndef EMBEDDED_LIBRARY
 
17231
  static char user[USERNAME_CHAR_LENGTH+1];
 
17232
  static char db[NAME_CHAR_LEN+1];
 
17233
  static char query[LARGE_BUFFER_SIZE*2];
 
17234
#endif
 
17235
 
 
17236
  DBUG_ENTER("test_bug31669");
 
17237
  myheader("test_bug31669");
 
17238
 
 
17239
  rc= mysql_change_user(mysql, NULL, NULL, NULL);
 
17240
  DIE_UNLESS(rc);
 
17241
 
 
17242
  rc= mysql_change_user(mysql, "", "", "");
 
17243
  DIE_UNLESS(rc);
 
17244
 
 
17245
  memset(buff, 'a', sizeof(buff));
 
17246
 
 
17247
  rc= mysql_change_user(mysql, buff, buff, buff);
 
17248
  DIE_UNLESS(rc);
 
17249
 
 
17250
  rc = mysql_change_user(mysql, opt_user, opt_password, current_db);
 
17251
  DIE_UNLESS(!rc);
 
17252
 
 
17253
#ifndef EMBEDDED_LIBRARY
 
17254
  memset(db, 'a', sizeof(db));
 
17255
  db[NAME_CHAR_LEN]= 0;
 
17256
  strxmov(query, "CREATE DATABASE IF NOT EXISTS ", db, NullS);
 
17257
  rc= mysql_query(mysql, query);
 
17258
  myquery(rc);
 
17259
 
 
17260
  memset(user, 'b', sizeof(user));
 
17261
  user[USERNAME_CHAR_LENGTH]= 0;
 
17262
  memset(buff, 'c', sizeof(buff));
 
17263
  buff[LARGE_BUFFER_SIZE]= 0;
 
17264
  strxmov(query, "GRANT ALL PRIVILEGES ON *.* TO '", user, "'@'%' IDENTIFIED BY "
 
17265
                 "'", buff, "' WITH GRANT OPTION", NullS);
 
17266
  rc= mysql_query(mysql, query);
 
17267
  myquery(rc);
 
17268
 
 
17269
  rc= mysql_query(mysql, "FLUSH PRIVILEGES");
 
17270
  myquery(rc);
 
17271
 
 
17272
  rc= mysql_change_user(mysql, user, buff, db);
 
17273
  DIE_UNLESS(!rc);
 
17274
 
 
17275
  user[USERNAME_CHAR_LENGTH-1]= 'a';
 
17276
  rc= mysql_change_user(mysql, user, buff, db);
 
17277
  DIE_UNLESS(rc);
 
17278
 
 
17279
  user[USERNAME_CHAR_LENGTH-1]= 'b';
 
17280
  buff[LARGE_BUFFER_SIZE-1]= 'd';
 
17281
  rc= mysql_change_user(mysql, user, buff, db);
 
17282
  DIE_UNLESS(rc);
 
17283
 
 
17284
  buff[LARGE_BUFFER_SIZE-1]= 'c';
 
17285
  db[NAME_CHAR_LEN-1]= 'e';
 
17286
  rc= mysql_change_user(mysql, user, buff, db);
 
17287
  DIE_UNLESS(rc);
 
17288
 
 
17289
  db[NAME_CHAR_LEN-1]= 'a';
 
17290
  rc= mysql_change_user(mysql, user, buff, db);
 
17291
  DIE_UNLESS(!rc);
 
17292
 
 
17293
  rc= mysql_change_user(mysql, user + 1, buff + 1, db + 1);
 
17294
  DIE_UNLESS(rc);
 
17295
 
 
17296
  rc = mysql_change_user(mysql, opt_user, opt_password, current_db);
 
17297
  DIE_UNLESS(!rc);
 
17298
 
 
17299
  strxmov(query, "DROP DATABASE ", db, NullS);
 
17300
  rc= mysql_query(mysql, query);
 
17301
  myquery(rc);
 
17302
 
 
17303
  strxmov(query, "DELETE FROM mysql.user WHERE User='", user, "'", NullS);
 
17304
  rc= mysql_query(mysql, query);
 
17305
  myquery(rc);
 
17306
  DIE_UNLESS(mysql_affected_rows(mysql) == 1);
 
17307
#endif
 
17308
 
 
17309
  DBUG_VOID_RETURN;
 
17310
}
 
17311
 
 
17312
 
 
17313
/**
 
17314
  Bug#28386 the general log is incomplete
 
17315
*/
 
17316
 
 
17317
static void test_bug28386()
 
17318
{
 
17319
  int rc;
 
17320
  MYSQL_STMT *stmt;
 
17321
  MYSQL_RES *result;
 
17322
  MYSQL_ROW row;
 
17323
  MYSQL_BIND bind;
 
17324
  const char hello[]= "hello world!";
 
17325
 
 
17326
  DBUG_ENTER("test_bug28386");
 
17327
  myheader("test_bug28386");
 
17328
 
 
17329
  rc= mysql_query(mysql, "select @@global.log_output");
 
17330
  myquery(rc);
 
17331
 
 
17332
  result= mysql_store_result(mysql);
 
17333
  DIE_UNLESS(result);
 
17334
 
 
17335
  row= mysql_fetch_row(result);
 
17336
  if (! strstr(row[0], "TABLE"))
 
17337
  {
 
17338
    mysql_free_result(result);
 
17339
    if (! opt_silent)
 
17340
      printf("Skipping the test since logging to tables is not enabled\n");
 
17341
    /* Log output is not to tables */
 
17342
    return;
 
17343
  }
 
17344
  mysql_free_result(result);
 
17345
 
 
17346
  enable_general_log(1);
 
17347
 
 
17348
  stmt= mysql_simple_prepare(mysql, "SELECT ?");
 
17349
  check_stmt(stmt);
 
17350
 
 
17351
  memset(&bind, 0, sizeof(bind));
 
17352
 
 
17353
  bind.buffer_type= MYSQL_TYPE_STRING;
 
17354
  bind.buffer= (void *) hello;
 
17355
  bind.buffer_length= sizeof(hello);
 
17356
 
 
17357
  mysql_stmt_bind_param(stmt, &bind);
 
17358
  mysql_stmt_send_long_data(stmt, 0, hello, sizeof(hello));
 
17359
 
 
17360
  rc= mysql_stmt_execute(stmt);
 
17361
  check_execute(stmt, rc);
 
17362
 
 
17363
  rc= my_process_stmt_result(stmt);
 
17364
  DIE_UNLESS(rc == 1);
 
17365
 
 
17366
  rc= mysql_stmt_reset(stmt);
 
17367
  check_execute(stmt, rc);
 
17368
 
 
17369
  rc= mysql_stmt_close(stmt);
 
17370
  DIE_UNLESS(!rc);
 
17371
 
 
17372
  rc= mysql_query(mysql, "select * from mysql.general_log where "
 
17373
                         "command_type='Close stmt' or "
 
17374
                         "command_type='Reset stmt' or "
 
17375
                         "command_type='Long Data'");
 
17376
  myquery(rc);
 
17377
 
 
17378
  result= mysql_store_result(mysql);
 
17379
  mytest(result);
 
17380
 
 
17381
  DIE_UNLESS(mysql_num_rows(result) == 3);
 
17382
 
 
17383
  mysql_free_result(result);
 
17384
 
 
17385
  restore_general_log();
 
17386
 
 
17387
  DBUG_VOID_RETURN;
 
17388
}
 
17389
 
 
17390
 
 
17391
/**
 
17392
  Bug#36004 mysql_stmt_prepare resets the list of warnings
 
17393
*/
 
17394
 
 
17395
static void test_bug36004()
 
17396
{
 
17397
  int rc, warning_count= 0;
 
17398
  MYSQL_STMT *stmt;
 
17399
 
 
17400
  DBUG_ENTER("test_bug36004");
 
17401
  myheader("test_bug36004");
 
17402
 
 
17403
  rc= mysql_query(mysql, "drop table if exists inexistant");
 
17404
  myquery(rc);
 
17405
 
 
17406
  DIE_UNLESS(mysql_warning_count(mysql) == 1);
 
17407
  query_int_variable(mysql, "@@warning_count", &warning_count);
 
17408
  DIE_UNLESS(warning_count);
 
17409
 
 
17410
  stmt= mysql_simple_prepare(mysql, "select 1");
 
17411
  check_stmt(stmt);
 
17412
 
 
17413
  DIE_UNLESS(mysql_warning_count(mysql) == 0);
 
17414
  query_int_variable(mysql, "@@warning_count", &warning_count);
 
17415
  DIE_UNLESS(warning_count);
 
17416
 
 
17417
  rc= mysql_stmt_execute(stmt);
 
17418
  check_execute(stmt, rc);
 
17419
 
 
17420
  DIE_UNLESS(mysql_warning_count(mysql) == 0);
 
17421
  mysql_stmt_close(stmt);
 
17422
 
 
17423
  query_int_variable(mysql, "@@warning_count", &warning_count);
 
17424
  DIE_UNLESS(warning_count);
 
17425
 
 
17426
  stmt= mysql_simple_prepare(mysql, "drop table if exists inexistant");
 
17427
  check_stmt(stmt);
 
17428
 
 
17429
  query_int_variable(mysql, "@@warning_count", &warning_count);
 
17430
  DIE_UNLESS(warning_count == 0);
 
17431
 
 
17432
  DBUG_VOID_RETURN;
 
17433
}
 
17434
 
 
17435
/*
 
17436
  Read and parse arguments and MySQL options from my.cnf
 
17437
*/
 
17438
 
 
17439
static const char *client_test_load_default_groups[]= { "client", 0 };
 
17440
static char **defaults_argv;
 
17441
 
 
17442
static struct my_option client_test_long_options[] =
 
17443
{
 
17444
  {"basedir", 'b', "Basedir for tests.", (uchar**) &opt_basedir,
 
17445
   (uchar**) &opt_basedir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
17446
  {"count", 't', "Number of times test to be executed", (uchar **) &opt_count,
 
17447
   (uchar **) &opt_count, 0, GET_UINT, REQUIRED_ARG, 1, 0, 0, 0, 0, 0},
 
17448
  {"database", 'D', "Database to use", (uchar **) &opt_db, (uchar **) &opt_db,
 
17449
   0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
17450
  {"debug", '#', "Output debug log", (uchar**) &default_dbug_option,
 
17451
   (uchar**) &default_dbug_option, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
17452
  {"help", '?', "Display this help and exit", 0, 0, 0, GET_NO_ARG, NO_ARG, 0,
 
17453
   0, 0, 0, 0, 0},
 
17454
  {"host", 'h', "Connect to host", (uchar **) &opt_host, (uchar **) &opt_host,
 
17455
   0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
17456
  {"password", 'p',
 
17457
   "Password to use when connecting to server. If password is not given it's asked from the tty.",
 
17458
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
17459
  {"port", 'P', "Port number to use for connection or 0 for default to, in "
 
17460
   "order of preference, my.cnf, $MYSQL_TCP_PORT, "
 
17461
#if MYSQL_PORT_DEFAULT == 0
 
17462
   "/etc/services, "
 
17463
#endif
 
17464
   "built-in default (" STRINGIFY_ARG(MYSQL_PORT) ").",
 
17465
   (uchar **) &opt_port,
 
17466
   (uchar **) &opt_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
17467
  {"server-arg", 'A', "Send embedded server this as a parameter.",
 
17468
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
17469
  {"show-tests", 'T', "Show all tests' names", 0, 0, 0, GET_NO_ARG, NO_ARG,
 
17470
   0, 0, 0, 0, 0, 0},
 
17471
  {"silent", 's', "Be more silent", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0,
 
17472
   0},
 
17473
  {"socket", 'S', "Socket file to use for connection",
 
17474
   (uchar **) &opt_unix_socket, (uchar **) &opt_unix_socket, 0, GET_STR,
 
17475
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
17476
  {"testcase", 'c',
 
17477
   "May disable some code when runs as mysql-test-run testcase.",
 
17478
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
17479
#ifndef DONT_ALLOW_USER_CHANGE
 
17480
  {"user", 'u', "User for login if not current user", (uchar **) &opt_user,
 
17481
   (uchar **) &opt_user, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
17482
#endif
 
17483
  {"vardir", 'v', "Data dir for tests.", (uchar**) &opt_vardir,
 
17484
   (uchar**) &opt_vardir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
17485
  {"getopt-ll-test", 'g', "Option for testing bug in getopt library",
 
17486
   (uchar **) &opt_getopt_ll_test, (uchar **) &opt_getopt_ll_test, 0,
 
17487
   GET_LL, REQUIRED_ARG, 0, 0, LONGLONG_MAX, 0, 0, 0},
 
17488
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
 
17489
};
 
17490
 
 
17491
 
 
17492
static void usage(void)
 
17493
{
 
17494
  /* show the usage string when the user asks for this */
 
17495
  putc('\n', stdout);
 
17496
  printf("%s  Ver %s Distrib %s, for %s (%s)\n",
 
17497
         my_progname, VER, MYSQL_SERVER_VERSION, SYSTEM_TYPE, MACHINE_TYPE);
 
17498
  puts("By Monty, Venu, Kent and others\n");
 
17499
  printf("\
 
17500
Copyright (C) 2002-2004 MySQL AB\n\
 
17501
This software comes with ABSOLUTELY NO WARRANTY. This is free software,\n\
 
17502
and you are welcome to modify and redistribute it under the GPL license\n");
 
17503
  printf("Usage: %s [OPTIONS] [TESTNAME1 TESTNAME2...]\n", my_progname);
 
17504
  my_print_help(client_test_long_options);
 
17505
  print_defaults("my", client_test_load_default_groups);
 
17506
  my_print_variables(client_test_long_options);
 
17507
}
 
17508
 
 
17509
 
 
17510
static struct my_tests_st my_tests[]= {
 
17511
  { "disable_general_log", disable_general_log },
 
17512
  { "test_view_sp_list_fields", test_view_sp_list_fields },
 
17513
  { "client_query", client_query },
 
17514
  { "test_prepare_insert_update", test_prepare_insert_update},
 
17515
#if NOT_YET_WORKING
 
17516
  { "test_drop_temp", test_drop_temp },
 
17517
#endif
 
17518
  { "test_fetch_seek", test_fetch_seek },
 
17519
  { "test_fetch_nobuffs", test_fetch_nobuffs },
 
17520
  { "test_open_direct", test_open_direct },
 
17521
  { "test_fetch_null", test_fetch_null },
 
17522
  { "test_ps_null_param", test_ps_null_param },
 
17523
  { "test_fetch_date", test_fetch_date },
 
17524
  { "test_fetch_str", test_fetch_str },
 
17525
  { "test_fetch_long", test_fetch_long },
 
17526
  { "test_fetch_short", test_fetch_short },
 
17527
  { "test_fetch_tiny", test_fetch_tiny },
 
17528
  { "test_fetch_bigint", test_fetch_bigint },
 
17529
  { "test_fetch_float", test_fetch_float },
 
17530
  { "test_fetch_double", test_fetch_double },
 
17531
  { "test_bind_result_ext", test_bind_result_ext },
 
17532
  { "test_bind_result_ext1", test_bind_result_ext1 },
 
17533
  { "test_select_direct", test_select_direct },
 
17534
  { "test_select_prepare", test_select_prepare },
 
17535
  { "test_select", test_select },
 
17536
  { "test_select_version", test_select_version },
 
17537
  { "test_ps_conj_select", test_ps_conj_select },
 
17538
  { "test_select_show_table", test_select_show_table },
 
17539
  { "test_func_fields", test_func_fields },
 
17540
  { "test_long_data", test_long_data },
 
17541
  { "test_insert", test_insert },
 
17542
  { "test_set_variable", test_set_variable },
 
17543
  { "test_select_show", test_select_show },
 
17544
  { "test_prepare_noparam", test_prepare_noparam },
 
17545
  { "test_bind_result", test_bind_result },
 
17546
  { "test_prepare_simple", test_prepare_simple },
 
17547
  { "test_prepare", test_prepare },
 
17548
  { "test_null", test_null },
 
17549
  { "test_debug_example", test_debug_example },
 
17550
  { "test_update", test_update },
 
17551
  { "test_simple_update", test_simple_update },
 
17552
  { "test_simple_delete", test_simple_delete },
 
17553
  { "test_double_compare", test_double_compare },
 
17554
  { "client_store_result", client_store_result },
 
17555
  { "client_use_result", client_use_result },
 
17556
  { "test_tran_bdb", test_tran_bdb },
 
17557
  { "test_tran_innodb", test_tran_innodb },
 
17558
  { "test_prepare_ext", test_prepare_ext },
 
17559
  { "test_prepare_syntax", test_prepare_syntax },
 
17560
  { "test_field_names", test_field_names },
 
17561
  { "test_field_flags", test_field_flags },
 
17562
  { "test_long_data_str", test_long_data_str },
 
17563
  { "test_long_data_str1", test_long_data_str1 },
 
17564
  { "test_long_data_bin", test_long_data_bin },
 
17565
  { "test_warnings", test_warnings },
 
17566
  { "test_errors", test_errors },
 
17567
  { "test_prepare_resultset", test_prepare_resultset },
 
17568
  { "test_stmt_close", test_stmt_close },
 
17569
  { "test_prepare_field_result", test_prepare_field_result },
 
17570
  { "test_multi_stmt", test_multi_stmt },
 
17571
  { "test_multi_statements", test_multi_statements },
 
17572
  { "test_prepare_multi_statements", test_prepare_multi_statements },
 
17573
  { "test_store_result", test_store_result },
 
17574
  { "test_store_result1", test_store_result1 },
 
17575
  { "test_store_result2", test_store_result2 },
 
17576
  { "test_subselect", test_subselect },
 
17577
  { "test_date", test_date },
 
17578
  { "test_date_date", test_date_date },
 
17579
  { "test_date_time", test_date_time },
 
17580
  { "test_date_ts", test_date_ts },
 
17581
  { "test_date_dt", test_date_dt },
 
17582
  { "test_prepare_alter", test_prepare_alter },
 
17583
  { "test_manual_sample", test_manual_sample },
 
17584
  { "test_pure_coverage", test_pure_coverage },
 
17585
  { "test_buffers", test_buffers },
 
17586
  { "test_ushort_bug", test_ushort_bug },
 
17587
  { "test_sshort_bug", test_sshort_bug },
 
17588
  { "test_stiny_bug", test_stiny_bug },
 
17589
  { "test_field_misc", test_field_misc },
 
17590
  { "test_set_option", test_set_option },
 
17591
#ifndef EMBEDDED_LIBRARY
 
17592
  { "test_prepare_grant", test_prepare_grant },
 
17593
#endif
 
17594
  { "test_frm_bug", test_frm_bug },
 
17595
  { "test_explain_bug", test_explain_bug },
 
17596
  { "test_decimal_bug", test_decimal_bug },
 
17597
  { "test_nstmts", test_nstmts },
 
17598
  { "test_logs;", test_logs },
 
17599
  { "test_cuted_rows", test_cuted_rows },
 
17600
  { "test_fetch_offset", test_fetch_offset },
 
17601
  { "test_fetch_column", test_fetch_column },
 
17602
  { "test_mem_overun", test_mem_overun },
 
17603
  { "test_list_fields", test_list_fields },
 
17604
  { "test_free_result", test_free_result },
 
17605
  { "test_free_store_result", test_free_store_result },
 
17606
  { "test_sqlmode", test_sqlmode },
 
17607
  { "test_ts", test_ts },
 
17608
  { "test_bug1115", test_bug1115 },
 
17609
  { "test_bug1180", test_bug1180 },
 
17610
  { "test_bug1500", test_bug1500 },
 
17611
  { "test_bug1644", test_bug1644 },
 
17612
  { "test_bug1946", test_bug1946 },
 
17613
  { "test_bug2248", test_bug2248 },
 
17614
  { "test_parse_error_and_bad_length", test_parse_error_and_bad_length },
 
17615
  { "test_bug2247", test_bug2247 },
 
17616
  { "test_subqueries", test_subqueries },
 
17617
  { "test_bad_union", test_bad_union },
 
17618
  { "test_distinct", test_distinct },
 
17619
  { "test_subqueries_ref", test_subqueries_ref },
 
17620
  { "test_union", test_union },
 
17621
  { "test_bug3117", test_bug3117 },
 
17622
  { "test_join", test_join },
 
17623
  { "test_selecttmp", test_selecttmp },
 
17624
  { "test_create_drop", test_create_drop },
 
17625
  { "test_rename", test_rename },
 
17626
  { "test_do_set", test_do_set },
 
17627
  { "test_multi", test_multi },
 
17628
  { "test_insert_select", test_insert_select },
 
17629
  { "test_bind_nagative", test_bind_nagative },
 
17630
  { "test_derived", test_derived },
 
17631
  { "test_xjoin", test_xjoin },
 
17632
  { "test_bug3035", test_bug3035 },
 
17633
  { "test_union2", test_union2 },
 
17634
  { "test_bug1664", test_bug1664 },
 
17635
  { "test_union_param", test_union_param },
 
17636
  { "test_order_param", test_order_param },
 
17637
  { "test_ps_i18n", test_ps_i18n },
 
17638
  { "test_bug3796", test_bug3796 },
 
17639
  { "test_bug4026", test_bug4026 },
 
17640
  { "test_bug4079", test_bug4079 },
 
17641
  { "test_bug4236", test_bug4236 },
 
17642
  { "test_bug4030", test_bug4030 },
 
17643
  { "test_bug5126", test_bug5126 },
 
17644
  { "test_bug4231", test_bug4231 },
 
17645
  { "test_bug5399", test_bug5399 },
 
17646
  { "test_bug5194", test_bug5194 },
 
17647
  { "test_bug5315", test_bug5315 },
 
17648
  { "test_bug6049", test_bug6049 },
 
17649
  { "test_bug6058", test_bug6058 },
 
17650
  { "test_bug6059", test_bug6059 },
 
17651
  { "test_bug6046", test_bug6046 },
 
17652
  { "test_bug6081", test_bug6081 },
 
17653
  { "test_bug6096", test_bug6096 },
 
17654
  { "test_datetime_ranges", test_datetime_ranges },
 
17655
  { "test_bug4172", test_bug4172 },
 
17656
  { "test_conversion", test_conversion },
 
17657
  { "test_rewind", test_rewind },
 
17658
  { "test_bug6761", test_bug6761 },
 
17659
  { "test_view", test_view },
 
17660
  { "test_view_where", test_view_where },
 
17661
  { "test_view_2where", test_view_2where },
 
17662
  { "test_view_star", test_view_star },
 
17663
  { "test_view_insert", test_view_insert },
 
17664
  { "test_left_join_view", test_left_join_view },
 
17665
  { "test_view_insert_fields", test_view_insert_fields },
 
17666
  { "test_basic_cursors", test_basic_cursors },
 
17667
  { "test_cursors_with_union", test_cursors_with_union },
 
17668
  { "test_cursors_with_procedure", test_cursors_with_procedure },
 
17669
  { "test_truncation", test_truncation },
 
17670
  { "test_truncation_option", test_truncation_option },
 
17671
  { "test_client_character_set", test_client_character_set },
 
17672
  { "test_bug8330", test_bug8330 },
 
17673
  { "test_bug7990", test_bug7990 },
 
17674
  { "test_bug8378", test_bug8378 },
 
17675
  { "test_bug8722", test_bug8722 },
 
17676
  { "test_bug8880", test_bug8880 },
 
17677
  { "test_bug9159", test_bug9159 },
 
17678
  { "test_bug9520", test_bug9520 },
 
17679
  { "test_bug9478", test_bug9478 },
 
17680
  { "test_bug9643", test_bug9643 },
 
17681
  { "test_bug10729", test_bug10729 },
 
17682
  { "test_bug11111", test_bug11111 },
 
17683
  { "test_bug9992", test_bug9992 },
 
17684
  { "test_bug10736", test_bug10736 },
 
17685
  { "test_bug10794", test_bug10794 },
 
17686
  { "test_bug11172", test_bug11172 },
 
17687
  { "test_bug11656", test_bug11656 },
 
17688
  { "test_bug10214", test_bug10214 },
 
17689
  { "test_bug9735", test_bug9735 },
 
17690
  { "test_bug11183", test_bug11183 },
 
17691
  { "test_bug11037", test_bug11037 },
 
17692
  { "test_bug10760", test_bug10760 },
 
17693
  { "test_bug12001", test_bug12001 },
 
17694
  { "test_bug11718", test_bug11718 },
 
17695
  { "test_bug12925", test_bug12925 },
 
17696
  { "test_bug11909", test_bug11909 },
 
17697
  { "test_bug11901", test_bug11901 },
 
17698
  { "test_bug11904", test_bug11904 },
 
17699
  { "test_bug12243", test_bug12243 },
 
17700
  { "test_bug14210", test_bug14210 },
 
17701
  { "test_bug13488", test_bug13488 },
 
17702
  { "test_bug13524", test_bug13524 },
 
17703
  { "test_bug14845", test_bug14845 },
 
17704
  { "test_opt_reconnect", test_opt_reconnect },
 
17705
  { "test_bug15510", test_bug15510},
 
17706
#ifndef EMBEDDED_LIBRARY
 
17707
  { "test_bug12744", test_bug12744 },
 
17708
#endif
 
17709
  { "test_bug16143", test_bug16143 },
 
17710
  { "test_bug16144", test_bug16144 },
 
17711
  { "test_bug15613", test_bug15613 },
 
17712
  { "test_bug20152", test_bug20152 },
 
17713
  { "test_bug14169", test_bug14169 },
 
17714
  { "test_bug17667", test_bug17667 },
 
17715
  { "test_bug15752", test_bug15752 },
 
17716
  { "test_mysql_insert_id", test_mysql_insert_id },
 
17717
  { "test_bug19671", test_bug19671 },
 
17718
  { "test_bug21206", test_bug21206 },
 
17719
  { "test_bug21726", test_bug21726 },
 
17720
  { "test_bug15518", test_bug15518 },
 
17721
  { "test_bug23383", test_bug23383 },
 
17722
  { "test_bug32265", test_bug32265 },
 
17723
  { "test_bug21635", test_bug21635 },
 
17724
  { "test_status",   test_status   },
 
17725
  { "test_bug24179", test_bug24179 },
 
17726
  { "test_ps_query_cache", test_ps_query_cache },
 
17727
  { "test_bug28075", test_bug28075 },
 
17728
  { "test_bug27876", test_bug27876 },
 
17729
  { "test_bug28505", test_bug28505 },
 
17730
  { "test_bug28934", test_bug28934 },
 
17731
  { "test_bug27592", test_bug27592 },
 
17732
  { "test_bug29687", test_bug29687 },
 
17733
  { "test_bug29692", test_bug29692 },
 
17734
  { "test_bug29306", test_bug29306 },
 
17735
  { "test_change_user", test_change_user },
 
17736
  { "test_bug30472", test_bug30472 },
 
17737
  { "test_bug20023", test_bug20023 },
 
17738
  { "test_bug31418", test_bug31418 },
 
17739
  { "test_bug31669", test_bug31669 },
 
17740
  { "test_bug28386", test_bug28386 },
 
17741
  { "test_bug36004", test_bug36004 },
 
17742
  { 0, 0 }
 
17743
};
 
17744
 
 
17745
 
 
17746
static my_bool
 
17747
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
 
17748
               char *argument)
 
17749
{
 
17750
  switch (optid) {
 
17751
  case '#':
 
17752
    DBUG_PUSH(argument ? argument : default_dbug_option);
 
17753
    break;
 
17754
  case 'c':
 
17755
    opt_testcase = 1;
 
17756
    break;
 
17757
  case 'p':
 
17758
    if (argument)
 
17759
    {
 
17760
      char *start=argument;
 
17761
      my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR));
 
17762
      opt_password= my_strdup(argument, MYF(MY_FAE));
 
17763
      while (*argument) *argument++= 'x';               /* Destroy argument */
 
17764
      if (*start)
 
17765
        start[1]=0;
 
17766
    }
 
17767
    else
 
17768
      tty_password= 1;
 
17769
    break;
 
17770
  case 's':
 
17771
    if (argument == disabled_my_option)
 
17772
      opt_silent= 0;
 
17773
    else
 
17774
      opt_silent++;
 
17775
    break;
 
17776
  case 'A':
 
17777
    /*
 
17778
      When the embedded server is being tested, the test suite needs to be
 
17779
      able to pass command-line arguments to the embedded server so it can
 
17780
      locate the language files and data directory. The test suite
 
17781
      (mysql-test-run) never uses config files, just command-line options.
 
17782
    */
 
17783
    if (!embedded_server_arg_count)
 
17784
    {
 
17785
      embedded_server_arg_count= 1;
 
17786
      embedded_server_args[0]= (char*) "";
 
17787
    }
 
17788
    if (embedded_server_arg_count == MAX_SERVER_ARGS-1 ||
 
17789
        !(embedded_server_args[embedded_server_arg_count++]=
 
17790
          my_strdup(argument, MYF(MY_FAE))))
 
17791
    {
 
17792
      DIE("Can't use server argument");
 
17793
    }
 
17794
    break;
 
17795
  case 'T':
 
17796
    {
 
17797
      struct my_tests_st *fptr;
 
17798
      
 
17799
      printf("All possible test names:\n\n");
 
17800
      for (fptr= my_tests; fptr->name; fptr++)
 
17801
        printf("%s\n", fptr->name);
 
17802
      exit(0);
 
17803
      break;
 
17804
    }
 
17805
  case '?':
 
17806
  case 'I':                                     /* Info */
 
17807
    usage();
 
17808
    exit(0);
 
17809
    break;
 
17810
  }
 
17811
  return 0;
 
17812
}
 
17813
 
 
17814
static void get_options(int *argc, char ***argv)
 
17815
{
 
17816
  int ho_error;
 
17817
 
 
17818
  if ((ho_error= handle_options(argc, argv, client_test_long_options,
 
17819
                                get_one_option)))
 
17820
    exit(ho_error);
 
17821
 
 
17822
  if (tty_password)
 
17823
    opt_password= get_tty_password(NullS);
 
17824
  return;
 
17825
}
 
17826
 
 
17827
/*
 
17828
  Print the test output on successful execution before exiting
 
17829
*/
 
17830
 
 
17831
static void print_test_output()
 
17832
{
 
17833
  if (opt_silent < 3)
 
17834
  {
 
17835
    fprintf(stdout, "\n\n");
 
17836
    fprintf(stdout, "All '%d' tests were successful (in '%d' iterations)",
 
17837
            test_count-1, opt_count);
 
17838
    fprintf(stdout, "\n  Total execution time: %g SECS", total_time);
 
17839
    if (opt_count > 1)
 
17840
      fprintf(stdout, " (Avg: %g SECS)", total_time/opt_count);
 
17841
 
 
17842
    fprintf(stdout, "\n\n!!! SUCCESS !!!\n");
 
17843
  }
 
17844
}
 
17845
 
 
17846
/***************************************************************************
 
17847
  main routine
 
17848
***************************************************************************/
 
17849
 
 
17850
 
 
17851
int main(int argc, char **argv)
 
17852
{
 
17853
  struct my_tests_st *fptr;
 
17854
 
 
17855
  MY_INIT(argv[0]);
 
17856
 
 
17857
  load_defaults("my", client_test_load_default_groups, &argc, &argv);
 
17858
  defaults_argv= argv;
 
17859
  get_options(&argc, &argv);
 
17860
 
 
17861
  if (mysql_server_init(embedded_server_arg_count,
 
17862
                        embedded_server_args,
 
17863
                        (char**) embedded_server_groups))
 
17864
    DIE("Can't initialize MySQL server");
 
17865
 
 
17866
  client_connect(0);       /* connect to server */
 
17867
 
 
17868
  total_time= 0;
 
17869
  for (iter_count= 1; iter_count <= opt_count; iter_count++)
 
17870
  {
 
17871
    /* Start of tests */
 
17872
    test_count= 1;
 
17873
    start_time= time((time_t *)0);
 
17874
    if (!argc)
 
17875
    {
 
17876
      for (fptr= my_tests; fptr->name; fptr++)
 
17877
        (*fptr->function)();    
 
17878
    }
 
17879
    else
 
17880
    {
 
17881
      for ( ; *argv ; argv++)
 
17882
      {
 
17883
        for (fptr= my_tests; fptr->name; fptr++)
 
17884
        {
 
17885
          if (!strcmp(fptr->name, *argv))
 
17886
          {
 
17887
            (*fptr->function)();
 
17888
            break;
 
17889
          }
 
17890
        }
 
17891
        if (!fptr->name)
 
17892
        {
 
17893
          fprintf(stderr, "\n\nGiven test not found: '%s'\n", *argv);
 
17894
          fprintf(stderr, "See legal test names with %s -T\n\nAborting!\n",
 
17895
                  my_progname);
 
17896
          client_disconnect();
 
17897
          free_defaults(defaults_argv);
 
17898
          exit(1);
 
17899
        }
 
17900
      }
 
17901
    }
 
17902
 
 
17903
    end_time= time((time_t *)0);
 
17904
    total_time+= difftime(end_time, start_time);
 
17905
 
 
17906
    /* End of tests */
 
17907
  }
 
17908
 
 
17909
  client_disconnect();    /* disconnect from server */
 
17910
 
 
17911
  free_defaults(defaults_argv);
 
17912
  print_test_output();
 
17913
 
 
17914
  while (embedded_server_arg_count > 1)
 
17915
    my_free(embedded_server_args[--embedded_server_arg_count],MYF(0));
 
17916
 
 
17917
  mysql_server_end();
 
17918
 
 
17919
  my_end(0);
 
17920
 
 
17921
  exit(0);
 
17922
}