~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to tests/mysql_client_test.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

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
#ifdef HAVE_SMEM
 
50
static char *shared_memory_base_name= 0;
 
51
#endif
 
52
static unsigned int  opt_port;
 
53
static my_bool tty_password= 0, opt_silent= 0;
 
54
 
 
55
static MYSQL *mysql= 0;
 
56
static char current_db[]= "client_test_db";
 
57
static unsigned int test_count= 0;
 
58
static unsigned int opt_count= 0;
 
59
static unsigned int iter_count= 0;
 
60
static my_bool have_innodb= FALSE;
 
61
 
 
62
static const char *opt_basedir= "./";
 
63
static const char *opt_vardir= "mysql-test/var";
 
64
 
 
65
static longlong opt_getopt_ll_test= 0;
 
66
 
 
67
static int embedded_server_arg_count= 0;
 
68
static char *embedded_server_args[MAX_SERVER_ARGS];
 
69
 
 
70
static const char *embedded_server_groups[]= {
 
71
  "server",
 
72
  "embedded",
 
73
  "mysql_client_test_SERVER",
 
74
  NullS
 
75
};
 
76
 
 
77
static time_t start_time, end_time;
 
78
static double total_time;
 
79
 
 
80
const char *default_dbug_option= "d:t:o,/tmp/mysql_client_test.trace";
 
81
 
 
82
struct my_tests_st
 
83
{
 
84
  const char *name;
 
85
  void       (*function)();
 
86
};
 
87
 
 
88
#define myheader(str) \
 
89
DBUG_PRINT("test", ("name: %s", str));        \
 
90
if (opt_silent < 2) \
 
91
{ \
 
92
  fprintf(stdout, "\n\n#####################################\n"); \
 
93
  fprintf(stdout, "%u of (%u/%u): %s", test_count++, iter_count, \
 
94
                                     opt_count, str); \
 
95
  fprintf(stdout, "  \n#####################################\n"); \
 
96
}
 
97
 
 
98
#define myheader_r(str) \
 
99
DBUG_PRINT("test", ("name: %s", str));        \
 
100
if (!opt_silent) \
 
101
{ \
 
102
  fprintf(stdout, "\n\n#####################################\n"); \
 
103
  fprintf(stdout, "%s", str); \
 
104
  fprintf(stdout, "  \n#####################################\n"); \
 
105
}
 
106
 
 
107
static void print_error(const char *msg);
 
108
static void print_st_error(MYSQL_STMT *stmt, const char *msg);
 
109
static void client_disconnect(MYSQL* mysql, my_bool drop_db);
 
110
 
 
111
 
 
112
/*
 
113
  Abort unless given experssion is non-zero.
 
114
 
 
115
  SYNOPSIS
 
116
    DIE_UNLESS(expr)
 
117
 
 
118
  DESCRIPTION
 
119
    We can't use any kind of system assert as we need to
 
120
    preserve tested invariants in release builds as well.
 
121
*/
 
122
 
 
123
#define DIE_UNLESS(expr) \
 
124
        ((void) ((expr) ? 0 : (die(__FILE__, __LINE__, #expr), 0)))
 
125
#define DIE_IF(expr) \
 
126
        ((void) ((expr) ? (die(__FILE__, __LINE__, #expr), 0) : 0))
 
127
#define DIE(expr) \
 
128
        die(__FILE__, __LINE__, #expr)
 
129
 
 
130
static void die(const char *file, int line, const char *expr)
 
131
{
 
132
  fflush(stdout);
 
133
  fprintf(stderr, "%s:%d: check failed: '%s'\n", file, line, expr);
 
134
  fflush(stderr);
 
135
  exit(1);
 
136
}
 
137
 
 
138
 
 
139
#define myerror(msg) print_error(msg)
 
140
#define mysterror(stmt, msg) print_st_error(stmt, msg)
 
141
 
 
142
#define myquery(RES) \
 
143
{ \
 
144
  int r= (RES);                                \
 
145
  if (r) \
 
146
    myerror(NULL); \
 
147
  DIE_UNLESS(r == 0); \
 
148
}
 
149
 
 
150
#define myquery_r(r) \
 
151
{ \
 
152
if (r) \
 
153
  myerror(NULL); \
 
154
DIE_UNLESS(r != 0); \
 
155
}
 
156
 
 
157
#define check_execute(stmt, r) \
 
158
{ \
 
159
if (r) \
 
160
  mysterror(stmt, NULL); \
 
161
DIE_UNLESS(r == 0);\
 
162
}
 
163
 
 
164
#define check_execute_r(stmt, r) \
 
165
{ \
 
166
if (r) \
 
167
  mysterror(stmt, NULL); \
 
168
DIE_UNLESS(r != 0);\
 
169
}
 
170
 
 
171
#define check_stmt(stmt) \
 
172
{ \
 
173
if ( stmt == 0) \
 
174
  myerror(NULL); \
 
175
DIE_UNLESS(stmt != 0); \
 
176
}
 
177
 
 
178
#define check_stmt_r(stmt) \
 
179
{ \
 
180
if (stmt == 0) \
 
181
  myerror(NULL);\
 
182
DIE_UNLESS(stmt == 0);\
 
183
}
 
184
 
 
185
#define mytest(x) if (!(x)) {myerror(NULL);DIE_UNLESS(FALSE);}
 
186
#define mytest_r(x) if ((x)) {myerror(NULL);DIE_UNLESS(FALSE);}
 
187
 
 
188
 
 
189
/* A workaround for Sun Forte 5.6 on Solaris x86 */
 
190
 
 
191
static int cmp_double(double *a, double *b)
 
192
{
 
193
  return *a == *b;
 
194
}
 
195
 
 
196
 
 
197
/* Print the error message */
 
198
 
 
199
static void print_error(const char *msg)
 
200
{
 
201
  if (!opt_silent)
 
202
  {
 
203
    if (mysql && mysql_errno(mysql))
 
204
    {
 
205
      if (mysql->server_version)
 
206
        fprintf(stdout, "\n [MySQL-%s]", mysql->server_version);
 
207
      else
 
208
        fprintf(stdout, "\n [MySQL]");
 
209
      fprintf(stdout, "[%d] %s\n", mysql_errno(mysql), mysql_error(mysql));
 
210
    }
 
211
    else if (msg)
 
212
      fprintf(stderr, " [MySQL] %s\n", msg);
 
213
  }
 
214
}
 
215
 
 
216
 
 
217
static void print_st_error(MYSQL_STMT *stmt, const char *msg)
 
218
{
 
219
  if (!opt_silent)
 
220
  {
 
221
    if (stmt && mysql_stmt_errno(stmt))
 
222
    {
 
223
      if (stmt->mysql && stmt->mysql->server_version)
 
224
        fprintf(stdout, "\n [MySQL-%s]", stmt->mysql->server_version);
 
225
      else
 
226
        fprintf(stdout, "\n [MySQL]");
 
227
 
 
228
      fprintf(stdout, "[%d] %s\n", mysql_stmt_errno(stmt),
 
229
              mysql_stmt_error(stmt));
 
230
    }
 
231
    else if (msg)
 
232
      fprintf(stderr, " [MySQL] %s\n", msg);
 
233
  }
 
234
}
 
235
 
 
236
/*
 
237
  Enhanced version of mysql_client_init(), which may also set shared memory 
 
238
  base on Windows.
 
239
*/
 
240
static MYSQL *mysql_client_init(MYSQL* con)
 
241
{
 
242
  MYSQL* res = mysql_init(con);
 
243
#ifdef HAVE_SMEM
 
244
  if (res && shared_memory_base_name)
 
245
    mysql_options(res, MYSQL_SHARED_MEMORY_BASE_NAME, shared_memory_base_name);
 
246
#endif
 
247
  return res;
 
248
}
 
249
 
 
250
/*
 
251
  Disable direct calls of mysql_init, as it disregards  shared memory base.
 
252
*/
 
253
#define mysql_init(A) Please use mysql_client_init instead of mysql_init
 
254
 
 
255
 
 
256
/* Check if the connection has InnoDB tables */
 
257
 
 
258
static my_bool check_have_innodb(MYSQL *conn)
 
259
{
 
260
  MYSQL_RES *res;
 
261
  MYSQL_ROW row;
 
262
  int rc;
 
263
  my_bool result;
 
264
 
 
265
  rc= mysql_query(conn, "show variables like 'have_innodb'");
 
266
  myquery(rc);
 
267
  res= mysql_use_result(conn);
 
268
  DIE_UNLESS(res);
 
269
 
 
270
  row= mysql_fetch_row(res);
 
271
  DIE_UNLESS(row);
 
272
 
 
273
  result= strcmp(row[1], "YES") == 0;
 
274
  mysql_free_result(res);
 
275
  return result;
 
276
}
 
277
 
 
278
 
 
279
/*
 
280
  This is to be what mysql_query() is for mysql_real_query(), for
 
281
  mysql_simple_prepare(): a variant without the 'length' parameter.
 
282
*/
 
283
 
 
284
static MYSQL_STMT *STDCALL
 
285
mysql_simple_prepare(MYSQL *mysql_arg, const char *query)
 
286
{
 
287
  MYSQL_STMT *stmt= mysql_stmt_init(mysql_arg);
 
288
  if (stmt && mysql_stmt_prepare(stmt, query, (uint) strlen(query)))
 
289
  {
 
290
    mysql_stmt_close(stmt);
 
291
    return 0;
 
292
  }
 
293
  return stmt;
 
294
}
 
295
 
 
296
 
 
297
/**
 
298
   Connect to the server with options given by arguments to this application,
 
299
   stored in global variables opt_host, opt_user, opt_password, opt_db, 
 
300
   opt_port and opt_unix_socket.
 
301
 
 
302
   @param flag[in]           client_flag passed on to mysql_real_connect
 
303
   @param protocol[in]       MYSQL_PROTOCOL_* to use for this connection
 
304
   @param auto_reconnect[in] set to 1 for auto reconnect
 
305
   
 
306
   @return pointer to initialized and connected MYSQL object
 
307
*/
 
308
static MYSQL* client_connect(ulong flag, uint protocol, my_bool auto_reconnect)
 
309
{
 
310
  MYSQL* mysql;
 
311
  int  rc;
 
312
  static char query[MAX_TEST_QUERY_LENGTH];
 
313
  myheader_r("client_connect");
 
314
 
 
315
  if (!opt_silent)
 
316
    fprintf(stdout, "\n Establishing a connection to '%s' ...",
 
317
            opt_host ? opt_host : "");
 
318
 
 
319
  if (!(mysql= mysql_client_init(NULL)))
 
320
  {
 
321
    opt_silent= 0;
 
322
    myerror("mysql_client_init() failed");
 
323
    exit(1);
 
324
  }
 
325
  /* enable local infile, in non-binary builds often disabled by default */
 
326
  mysql_options(mysql, MYSQL_OPT_LOCAL_INFILE, 0);
 
327
  mysql_options(mysql, MYSQL_OPT_PROTOCOL, &protocol);
 
328
 
 
329
  if (!(mysql_real_connect(mysql, opt_host, opt_user,
 
330
                           opt_password, opt_db ? opt_db:"test", opt_port,
 
331
                           opt_unix_socket, flag)))
 
332
  {
 
333
    opt_silent= 0;
 
334
    myerror("connection failed");
 
335
    mysql_close(mysql);
 
336
    fprintf(stdout, "\n Check the connection options using --help or -?\n");
 
337
    exit(1);
 
338
  }
 
339
  mysql->reconnect= auto_reconnect;
 
340
 
 
341
  if (!opt_silent)
 
342
    fprintf(stdout, "OK");
 
343
 
 
344
  /* set AUTOCOMMIT to ON*/
 
345
  mysql_autocommit(mysql, TRUE);
 
346
 
 
347
  if (!opt_silent)
 
348
  {
 
349
    fprintf(stdout, "\nConnected to MySQL server version: %s (%lu)\n",
 
350
            mysql_get_server_info(mysql),
 
351
            (ulong) mysql_get_server_version(mysql));
 
352
    fprintf(stdout, "\n Creating a test database '%s' ...", current_db);
 
353
  }
 
354
  strxmov(query, "CREATE DATABASE IF NOT EXISTS ", current_db, NullS);
 
355
 
 
356
  rc= mysql_query(mysql, query);
 
357
  myquery(rc);
 
358
 
 
359
  strxmov(query, "USE ", current_db, NullS);
 
360
  rc= mysql_query(mysql, query);
 
361
  myquery(rc);
 
362
  have_innodb= check_have_innodb(mysql);
 
363
 
 
364
  if (!opt_silent)
 
365
    fprintf(stdout, "OK");
 
366
 
 
367
  return mysql;
 
368
}
 
369
 
 
370
 
 
371
/* Close the connection */
 
372
 
 
373
static void client_disconnect(MYSQL* mysql, my_bool drop_db)
 
374
{
 
375
  static char query[MAX_TEST_QUERY_LENGTH];
 
376
 
 
377
  myheader_r("client_disconnect");
 
378
 
 
379
  if (mysql)
 
380
  {
 
381
    if (drop_db)
 
382
    {
 
383
      if (!opt_silent)
 
384
        fprintf(stdout, "\n dropping the test database '%s' ...", current_db);
 
385
      strxmov(query, "DROP DATABASE IF EXISTS ", current_db, NullS);
 
386
 
 
387
      mysql_query(mysql, query);
 
388
      if (!opt_silent)
 
389
        fprintf(stdout, "OK");
 
390
    }
 
391
 
 
392
    if (!opt_silent)
 
393
      fprintf(stdout, "\n closing the connection ...");
 
394
    mysql_close(mysql);
 
395
    if (!opt_silent)
 
396
      fprintf(stdout, "OK\n");
 
397
  }
 
398
}
 
399
 
 
400
 
 
401
/* Query processing */
 
402
 
 
403
static void client_query()
 
404
{
 
405
  int rc;
 
406
 
 
407
  myheader("client_query");
 
408
 
 
409
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
410
  myquery(rc);
 
411
 
 
412
  rc= mysql_query(mysql, "CREATE TABLE t1("
 
413
                         "id int primary key auto_increment, "
 
414
                         "name varchar(20))");
 
415
  myquery(rc);
 
416
 
 
417
  rc= mysql_query(mysql, "CREATE TABLE t1(id int, name varchar(20))");
 
418
  myquery_r(rc);
 
419
 
 
420
  rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('mysql')");
 
421
  myquery(rc);
 
422
 
 
423
  rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('monty')");
 
424
  myquery(rc);
 
425
 
 
426
  rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('venu')");
 
427
  myquery(rc);
 
428
 
 
429
  rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('deleted')");
 
430
  myquery(rc);
 
431
 
 
432
  rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('deleted')");
 
433
  myquery(rc);
 
434
 
 
435
  rc= mysql_query(mysql, "UPDATE t1 SET name= 'updated' "
 
436
                          "WHERE name= 'deleted'");
 
437
  myquery(rc);
 
438
 
 
439
  rc= mysql_query(mysql, "UPDATE t1 SET id= 3 WHERE name= 'updated'");
 
440
  myquery_r(rc);
 
441
 
 
442
  myquery(mysql_query(mysql, "drop table t1"));
 
443
}
 
444
 
 
445
 
 
446
/* Print dashes */
 
447
 
 
448
static void my_print_dashes(MYSQL_RES *result)
 
449
{
 
450
  MYSQL_FIELD  *field;
 
451
  unsigned int i, j;
 
452
 
 
453
  mysql_field_seek(result, 0);
 
454
  fputc('\t', stdout);
 
455
  fputc('+', stdout);
 
456
 
 
457
  for(i= 0; i< mysql_num_fields(result); i++)
 
458
  {
 
459
    field= mysql_fetch_field(result);
 
460
    for(j= 0; j < field->max_length+2; j++)
 
461
      fputc('-', stdout);
 
462
    fputc('+', stdout);
 
463
  }
 
464
  fputc('\n', stdout);
 
465
}
 
466
 
 
467
 
 
468
/* Print resultset metadata information */
 
469
 
 
470
static void my_print_result_metadata(MYSQL_RES *result)
 
471
{
 
472
  MYSQL_FIELD  *field;
 
473
  unsigned int i, j;
 
474
  unsigned int field_count;
 
475
 
 
476
  mysql_field_seek(result, 0);
 
477
  if (!opt_silent)
 
478
  {
 
479
    fputc('\n', stdout);
 
480
    fputc('\n', stdout);
 
481
  }
 
482
 
 
483
  field_count= mysql_num_fields(result);
 
484
  for(i= 0; i< field_count; i++)
 
485
  {
 
486
    field= mysql_fetch_field(result);
 
487
    j= strlen(field->name);
 
488
    if (j < field->max_length)
 
489
      j= field->max_length;
 
490
    if (j < 4 && !IS_NOT_NULL(field->flags))
 
491
      j= 4;
 
492
    field->max_length= j;
 
493
  }
 
494
  if (!opt_silent)
 
495
  {
 
496
    my_print_dashes(result);
 
497
    fputc('\t', stdout);
 
498
    fputc('|', stdout);
 
499
  }
 
500
 
 
501
  mysql_field_seek(result, 0);
 
502
  for(i= 0; i< field_count; i++)
 
503
  {
 
504
    field= mysql_fetch_field(result);
 
505
    if (!opt_silent)
 
506
      fprintf(stdout, " %-*s |", (int) field->max_length, field->name);
 
507
  }
 
508
  if (!opt_silent)
 
509
  {
 
510
    fputc('\n', stdout);
 
511
    my_print_dashes(result);
 
512
  }
 
513
}
 
514
 
 
515
 
 
516
/* Process the result set */
 
517
 
 
518
static int my_process_result_set(MYSQL_RES *result)
 
519
{
 
520
  MYSQL_ROW    row;
 
521
  MYSQL_FIELD  *field;
 
522
  unsigned int i;
 
523
  unsigned int row_count= 0;
 
524
 
 
525
  if (!result)
 
526
    return 0;
 
527
 
 
528
  my_print_result_metadata(result);
 
529
 
 
530
  while ((row= mysql_fetch_row(result)) != NULL)
 
531
  {
 
532
    mysql_field_seek(result, 0);
 
533
    if (!opt_silent)
 
534
    {
 
535
      fputc('\t', stdout);
 
536
      fputc('|', stdout);
 
537
    }
 
538
 
 
539
    for(i= 0; i< mysql_num_fields(result); i++)
 
540
    {
 
541
      field= mysql_fetch_field(result);
 
542
      if (!opt_silent)
 
543
      {
 
544
        if (row[i] == NULL)
 
545
          fprintf(stdout, " %-*s |", (int) field->max_length, "NULL");
 
546
        else if (IS_NUM(field->type))
 
547
          fprintf(stdout, " %*s |", (int) field->max_length, row[i]);
 
548
        else
 
549
          fprintf(stdout, " %-*s |", (int) field->max_length, row[i]);
 
550
      }
 
551
    }
 
552
    if (!opt_silent)
 
553
    {
 
554
      fputc('\t', stdout);
 
555
      fputc('\n', stdout);
 
556
    }
 
557
    row_count++;
 
558
  }
 
559
  if (!opt_silent)
 
560
  {
 
561
    if (row_count)
 
562
      my_print_dashes(result);
 
563
 
 
564
    if (mysql_errno(mysql) != 0)
 
565
      fprintf(stderr, "\n\tmysql_fetch_row() failed\n");
 
566
    else
 
567
      fprintf(stdout, "\n\t%d %s returned\n", row_count,
 
568
              row_count == 1 ? "row" : "rows");
 
569
  }
 
570
  return row_count;
 
571
}
 
572
 
 
573
 
 
574
static int my_process_result(MYSQL *mysql_arg)
 
575
{
 
576
  MYSQL_RES *result;
 
577
  int       row_count;
 
578
 
 
579
  if (!(result= mysql_store_result(mysql_arg)))
 
580
    return 0;
 
581
 
 
582
  row_count= my_process_result_set(result);
 
583
 
 
584
  mysql_free_result(result);
 
585
  return row_count;
 
586
}
 
587
 
 
588
 
 
589
/* Process the statement result set */
 
590
 
 
591
#define MAX_RES_FIELDS 50
 
592
#define MAX_FIELD_DATA_SIZE 255
 
593
 
 
594
static int my_process_stmt_result(MYSQL_STMT *stmt)
 
595
{
 
596
  int         field_count;
 
597
  int         row_count= 0;
 
598
  MYSQL_BIND  buffer[MAX_RES_FIELDS];
 
599
  MYSQL_FIELD *field;
 
600
  MYSQL_RES   *result;
 
601
  char        data[MAX_RES_FIELDS][MAX_FIELD_DATA_SIZE];
 
602
  ulong       length[MAX_RES_FIELDS];
 
603
  my_bool     is_null[MAX_RES_FIELDS];
 
604
  int         rc, i;
 
605
 
 
606
  if (!(result= mysql_stmt_result_metadata(stmt))) /* No meta info */
 
607
  {
 
608
    while (!mysql_stmt_fetch(stmt))
 
609
      row_count++;
 
610
    return row_count;
 
611
  }
 
612
 
 
613
  field_count= min(mysql_num_fields(result), MAX_RES_FIELDS);
 
614
 
 
615
  bzero((char*) buffer, sizeof(buffer));
 
616
  bzero((char*) length, sizeof(length));
 
617
  bzero((char*) is_null, sizeof(is_null));
 
618
 
 
619
  for(i= 0; i < field_count; i++)
 
620
  {
 
621
    buffer[i].buffer_type= MYSQL_TYPE_STRING;
 
622
    buffer[i].buffer_length= MAX_FIELD_DATA_SIZE;
 
623
    buffer[i].length= &length[i];
 
624
    buffer[i].buffer= (void *) data[i];
 
625
    buffer[i].is_null= &is_null[i];
 
626
  }
 
627
 
 
628
  rc= mysql_stmt_bind_result(stmt, buffer);
 
629
  check_execute(stmt, rc);
 
630
 
 
631
  rc= 1;
 
632
  mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*)&rc);
 
633
  rc= mysql_stmt_store_result(stmt);
 
634
  check_execute(stmt, rc);
 
635
  my_print_result_metadata(result);
 
636
 
 
637
  mysql_field_seek(result, 0);
 
638
  while ((rc= mysql_stmt_fetch(stmt)) == 0)
 
639
  {
 
640
    if (!opt_silent)
 
641
    {
 
642
      fputc('\t', stdout);
 
643
      fputc('|', stdout);
 
644
    }
 
645
    mysql_field_seek(result, 0);
 
646
    for (i= 0; i < field_count; i++)
 
647
    {
 
648
      field= mysql_fetch_field(result);
 
649
      if (!opt_silent)
 
650
      {
 
651
        if (is_null[i])
 
652
          fprintf(stdout, " %-*s |", (int) field->max_length, "NULL");
 
653
        else if (length[i] == 0)
 
654
        {
 
655
          data[i][0]= '\0';  /* unmodified buffer */
 
656
          fprintf(stdout, " %*s |", (int) field->max_length, data[i]);
 
657
        }
 
658
        else if (IS_NUM(field->type))
 
659
          fprintf(stdout, " %*s |", (int) field->max_length, data[i]);
 
660
        else
 
661
          fprintf(stdout, " %-*s |", (int) field->max_length, data[i]);
 
662
      }
 
663
    }
 
664
    if (!opt_silent)
 
665
    {
 
666
      fputc('\t', stdout);
 
667
      fputc('\n', stdout);
 
668
    }
 
669
    row_count++;
 
670
  }
 
671
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
672
  if (!opt_silent)
 
673
  {
 
674
    if (row_count)
 
675
      my_print_dashes(result);
 
676
    fprintf(stdout, "\n\t%d %s returned\n", row_count,
 
677
            row_count == 1 ? "row" : "rows");
 
678
  }
 
679
  mysql_free_result(result);
 
680
  return row_count;
 
681
}
 
682
 
 
683
 
 
684
/* Prepare statement, execute, and process result set for given query */
 
685
 
 
686
int my_stmt_result(const char *buff)
 
687
{
 
688
  MYSQL_STMT *stmt;
 
689
  int        row_count;
 
690
  int        rc;
 
691
 
 
692
  if (!opt_silent)
 
693
    fprintf(stdout, "\n\n %s", buff);
 
694
  stmt= mysql_simple_prepare(mysql, buff);
 
695
  check_stmt(stmt);
 
696
 
 
697
  rc= mysql_stmt_execute(stmt);
 
698
  check_execute(stmt, rc);
 
699
 
 
700
  row_count= my_process_stmt_result(stmt);
 
701
  mysql_stmt_close(stmt);
 
702
 
 
703
  return row_count;
 
704
}
 
705
 
 
706
 
 
707
/* Utility function to verify a particular column data */
 
708
 
 
709
static void verify_col_data(const char *table, const char *col,
 
710
                            const char *exp_data)
 
711
{
 
712
  static char query[MAX_TEST_QUERY_LENGTH];
 
713
  MYSQL_RES *result;
 
714
  MYSQL_ROW row;
 
715
  int       rc, field= 1;
 
716
 
 
717
  if (table && col)
 
718
  {
 
719
    strxmov(query, "SELECT ", col, " FROM ", table, " LIMIT 1", NullS);
 
720
    if (!opt_silent)
 
721
      fprintf(stdout, "\n %s", query);
 
722
    rc= mysql_query(mysql, query);
 
723
    myquery(rc);
 
724
 
 
725
    field= 0;
 
726
  }
 
727
 
 
728
  result= mysql_use_result(mysql);
 
729
  mytest(result);
 
730
 
 
731
  if (!(row= mysql_fetch_row(result)) || !row[field])
 
732
  {
 
733
    fprintf(stdout, "\n *** ERROR: FAILED TO GET THE RESULT ***");
 
734
    exit(1);
 
735
  }
 
736
  if (strcmp(row[field], exp_data))
 
737
  {
 
738
    fprintf(stdout, "\n obtained: `%s` (expected: `%s`)",
 
739
            row[field], exp_data);
 
740
    DIE_UNLESS(FALSE);
 
741
  }
 
742
  mysql_free_result(result);
 
743
}
 
744
 
 
745
 
 
746
/* Utility function to verify the field members */
 
747
 
 
748
#define verify_prepare_field(result,no,name,org_name,type,table,\
 
749
                             org_table,db,length,def) \
 
750
          do_verify_prepare_field((result),(no),(name),(org_name),(type), \
 
751
                                  (table),(org_table),(db),(length),(def), \
 
752
                                  __FILE__, __LINE__)
 
753
 
 
754
static void do_verify_prepare_field(MYSQL_RES *result,
 
755
                                   unsigned int no, const char *name,
 
756
                                   const char *org_name,
 
757
                                   enum enum_field_types type,
 
758
                                   const char *table,
 
759
                                   const char *org_table, const char *db,
 
760
                                   unsigned long length, const char *def,
 
761
                                   const char *file, int line)
 
762
{
 
763
  MYSQL_FIELD *field;
 
764
  CHARSET_INFO *cs;
 
765
  ulonglong expected_field_length;
 
766
 
 
767
  if (!(field= mysql_fetch_field_direct(result, no)))
 
768
  {
 
769
    fprintf(stdout, "\n *** ERROR: FAILED TO GET THE RESULT ***");
 
770
    exit(1);
 
771
  }
 
772
  cs= get_charset(field->charsetnr, 0);
 
773
  DIE_UNLESS(cs);
 
774
  if ((expected_field_length= length * cs->mbmaxlen) > UINT_MAX32)
 
775
    expected_field_length= UINT_MAX32;
 
776
  if (!opt_silent)
 
777
  {
 
778
    fprintf(stdout, "\n field[%d]:", no);
 
779
    fprintf(stdout, "\n    name     :`%s`\t(expected: `%s`)", field->name, name);
 
780
    fprintf(stdout, "\n    org_name :`%s`\t(expected: `%s`)",
 
781
            field->org_name, org_name);
 
782
    fprintf(stdout, "\n    type     :`%d`\t(expected: `%d`)", field->type, type);
 
783
    if (table)
 
784
      fprintf(stdout, "\n    table    :`%s`\t(expected: `%s`)",
 
785
              field->table, table);
 
786
    if (org_table)            
 
787
      fprintf(stdout, "\n    org_table:`%s`\t(expected: `%s`)",
 
788
              field->org_table, org_table);
 
789
    fprintf(stdout, "\n    database :`%s`\t(expected: `%s`)", field->db, db);
 
790
    fprintf(stdout, "\n    length   :`%lu`\t(expected: `%llu`)",
 
791
            field->length, expected_field_length);
 
792
    fprintf(stdout, "\n    maxlength:`%ld`", field->max_length);
 
793
    fprintf(stdout, "\n    charsetnr:`%d`", field->charsetnr);
 
794
    fprintf(stdout, "\n    default  :`%s`\t(expected: `%s`)",
 
795
            field->def ? field->def : "(null)", def ? def: "(null)");
 
796
    fprintf(stdout, "\n");
 
797
  }
 
798
  DIE_UNLESS(strcmp(field->name, name) == 0);
 
799
  DIE_UNLESS(strcmp(field->org_name, org_name) == 0);
 
800
  /*
 
801
    XXX: silent column specification change works based on number of
 
802
    bytes a column occupies. So CHAR -> VARCHAR upgrade is possible even
 
803
    for CHAR(2) column if its character set is multibyte.
 
804
    VARCHAR -> CHAR downgrade won't work for VARCHAR(3) as one would
 
805
    expect.
 
806
  */
 
807
  if (cs->mbmaxlen == 1)
 
808
  {
 
809
    if (field->type != type)
 
810
    {
 
811
      fprintf(stderr,
 
812
              "Expected field type: %d,  got type: %d in file %s, line %d\n",
 
813
              (int) type, (int) field->type, file, line);
 
814
      DIE_UNLESS(field->type == type);
 
815
    }
 
816
  }
 
817
  if (table)
 
818
    DIE_UNLESS(strcmp(field->table, table) == 0);
 
819
  if (org_table)
 
820
    DIE_UNLESS(strcmp(field->org_table, org_table) == 0);
 
821
  DIE_UNLESS(strcmp(field->db, db) == 0);
 
822
  /*
 
823
    Character set should be taken into account for multibyte encodings, such
 
824
    as utf8. Field length is calculated as number of characters * maximum
 
825
    number of bytes a character can occupy.
 
826
  */
 
827
  if (length && (field->length != expected_field_length))
 
828
  {
 
829
    fprintf(stderr, "Expected field length: %llu,  got length: %lu\n",
 
830
            expected_field_length, field->length);
 
831
    DIE_UNLESS(field->length == expected_field_length);
 
832
  }
 
833
  if (def)
 
834
    DIE_UNLESS(strcmp(field->def, def) == 0);
 
835
}
 
836
 
 
837
 
 
838
/* Utility function to verify the parameter count */
 
839
 
 
840
static void verify_param_count(MYSQL_STMT *stmt, long exp_count)
 
841
{
 
842
  long param_count= mysql_stmt_param_count(stmt);
 
843
  if (!opt_silent)
 
844
    fprintf(stdout, "\n total parameters in stmt: `%ld` (expected: `%ld`)",
 
845
            param_count, exp_count);
 
846
  DIE_UNLESS(param_count == exp_count);
 
847
}
 
848
 
 
849
 
 
850
/* Utility function to verify the total affected rows */
 
851
 
 
852
static void verify_st_affected_rows(MYSQL_STMT *stmt, ulonglong exp_count)
 
853
{
 
854
  ulonglong affected_rows= mysql_stmt_affected_rows(stmt);
 
855
  if (!opt_silent)
 
856
    fprintf(stdout, "\n total affected rows: `%ld` (expected: `%ld`)",
 
857
            (long) affected_rows, (long) exp_count);
 
858
  DIE_UNLESS(affected_rows == exp_count);
 
859
}
 
860
 
 
861
 
 
862
/* Utility function to verify the total affected rows */
 
863
 
 
864
static void verify_affected_rows(ulonglong exp_count)
 
865
{
 
866
  ulonglong affected_rows= mysql_affected_rows(mysql);
 
867
  if (!opt_silent)
 
868
    fprintf(stdout, "\n total affected rows: `%ld` (expected: `%ld`)",
 
869
            (long) affected_rows, (long) exp_count);
 
870
  DIE_UNLESS(affected_rows == exp_count);
 
871
}
 
872
 
 
873
 
 
874
/* Utility function to verify the total fields count */
 
875
 
 
876
static void verify_field_count(MYSQL_RES *result, uint exp_count)
 
877
{
 
878
  uint field_count= mysql_num_fields(result);
 
879
  if (!opt_silent)
 
880
    fprintf(stdout, "\n total fields in the result set: `%d` (expected: `%d`)",
 
881
            field_count, exp_count);
 
882
  DIE_UNLESS(field_count == exp_count);
 
883
}
 
884
 
 
885
 
 
886
/* Utility function to execute a query using prepare-execute */
 
887
 
 
888
#ifndef EMBEDDED_LIBRARY
 
889
static void execute_prepare_query(const char *query, ulonglong exp_count)
 
890
{
 
891
  MYSQL_STMT *stmt;
 
892
  ulonglong  affected_rows;
 
893
  int        rc;
 
894
 
 
895
  stmt= mysql_simple_prepare(mysql, query);
 
896
  check_stmt(stmt);
 
897
 
 
898
  rc= mysql_stmt_execute(stmt);
 
899
  myquery(rc);
 
900
 
 
901
  affected_rows= mysql_stmt_affected_rows(stmt);
 
902
  if (!opt_silent)
 
903
    fprintf(stdout, "\n total affected rows: `%ld` (expected: `%ld`)",
 
904
            (long) affected_rows, (long) exp_count);
 
905
 
 
906
  DIE_UNLESS(affected_rows == exp_count);
 
907
  mysql_stmt_close(stmt);
 
908
}
 
909
#endif
 
910
 
 
911
/* Store result processing */
 
912
 
 
913
static void client_store_result()
 
914
{
 
915
  MYSQL_RES *result;
 
916
  int       rc;
 
917
 
 
918
  myheader("client_store_result");
 
919
 
 
920
  rc= mysql_query(mysql, "SELECT * FROM t1");
 
921
  myquery(rc);
 
922
 
 
923
  /* get the result */
 
924
  result= mysql_store_result(mysql);
 
925
  mytest(result);
 
926
 
 
927
  (void) my_process_result_set(result);
 
928
  mysql_free_result(result);
 
929
}
 
930
 
 
931
 
 
932
/* Fetch the results */
 
933
 
 
934
static void client_use_result()
 
935
{
 
936
  MYSQL_RES *result;
 
937
  int       rc;
 
938
  myheader("client_use_result");
 
939
 
 
940
  rc= mysql_query(mysql, "SELECT * FROM t1");
 
941
  myquery(rc);
 
942
 
 
943
  /* get the result */
 
944
  result= mysql_use_result(mysql);
 
945
  mytest(result);
 
946
 
 
947
  (void) my_process_result_set(result);
 
948
  mysql_free_result(result);
 
949
}
 
950
 
 
951
 
 
952
/*
 
953
  Accepts arbitrary number of queries and runs them against the database.
 
954
  Used to fill tables for each test.
 
955
*/
 
956
 
 
957
void fill_tables(const char **query_list, unsigned query_count)
 
958
{
 
959
  int rc;
 
960
  const char **query;
 
961
  DBUG_ENTER("fill_tables");
 
962
  for (query= query_list; query < query_list + query_count;
 
963
       ++query)
 
964
  {
 
965
    rc= mysql_query(mysql, *query);
 
966
    myquery(rc);
 
967
  }
 
968
  DBUG_VOID_RETURN;
 
969
}
 
970
 
 
971
/*
 
972
  All state of fetch from one statement: statement handle, out buffers,
 
973
  fetch position.
 
974
  See fetch_n for for the only use case.
 
975
*/
 
976
 
 
977
enum { MAX_COLUMN_LENGTH= 255 };
 
978
 
 
979
typedef struct st_stmt_fetch
 
980
{
 
981
  const char *query;
 
982
  unsigned stmt_no;
 
983
  MYSQL_STMT *handle;
 
984
  my_bool is_open;
 
985
  MYSQL_BIND *bind_array;
 
986
  char **out_data;
 
987
  unsigned long *out_data_length;
 
988
  unsigned column_count;
 
989
  unsigned row_count;
 
990
} Stmt_fetch;
 
991
 
 
992
 
 
993
/*
 
994
  Create statement handle, prepare it with statement, execute and allocate
 
995
  fetch buffers.
 
996
*/
 
997
 
 
998
void stmt_fetch_init(Stmt_fetch *fetch, unsigned stmt_no_arg,
 
999
                     const char *query_arg)
 
1000
{
 
1001
  unsigned long type= CURSOR_TYPE_READ_ONLY;
 
1002
  int rc;
 
1003
  unsigned i;
 
1004
  MYSQL_RES *metadata;
 
1005
  DBUG_ENTER("stmt_fetch_init");
 
1006
 
 
1007
  /* Save query and statement number for error messages */
 
1008
  fetch->stmt_no= stmt_no_arg;
 
1009
  fetch->query= query_arg;
 
1010
 
 
1011
  fetch->handle= mysql_stmt_init(mysql);
 
1012
 
 
1013
  rc= mysql_stmt_prepare(fetch->handle, fetch->query, strlen(fetch->query));
 
1014
  check_execute(fetch->handle, rc);
 
1015
 
 
1016
  /*
 
1017
    The attribute is sent to server on execute and asks to open read-only
 
1018
    for result set
 
1019
  */
 
1020
  mysql_stmt_attr_set(fetch->handle, STMT_ATTR_CURSOR_TYPE,
 
1021
                      (const void*) &type);
 
1022
 
 
1023
  rc= mysql_stmt_execute(fetch->handle);
 
1024
  check_execute(fetch->handle, rc);
 
1025
 
 
1026
  /* Find out total number of columns in result set */
 
1027
  metadata= mysql_stmt_result_metadata(fetch->handle);
 
1028
  fetch->column_count= mysql_num_fields(metadata);
 
1029
  mysql_free_result(metadata);
 
1030
 
 
1031
  /*
 
1032
    Now allocate bind handles and buffers for output data:
 
1033
    calloc memory to reduce number of MYSQL_BIND members we need to
 
1034
    set up.
 
1035
  */
 
1036
 
 
1037
  fetch->bind_array= (MYSQL_BIND *) calloc(1, sizeof(MYSQL_BIND) *
 
1038
                                              fetch->column_count);
 
1039
  fetch->out_data= (char**) calloc(1, sizeof(char*) * fetch->column_count);
 
1040
  fetch->out_data_length= (ulong*) calloc(1, sizeof(ulong) *
 
1041
                                             fetch->column_count);
 
1042
  for (i= 0; i < fetch->column_count; ++i)
 
1043
  {
 
1044
    fetch->out_data[i]= (char*) calloc(1, MAX_COLUMN_LENGTH);
 
1045
    fetch->bind_array[i].buffer_type= MYSQL_TYPE_STRING;
 
1046
    fetch->bind_array[i].buffer= fetch->out_data[i];
 
1047
    fetch->bind_array[i].buffer_length= MAX_COLUMN_LENGTH;
 
1048
    fetch->bind_array[i].length= fetch->out_data_length + i;
 
1049
  }
 
1050
 
 
1051
  mysql_stmt_bind_result(fetch->handle, fetch->bind_array);
 
1052
 
 
1053
  fetch->row_count= 0;
 
1054
  fetch->is_open= TRUE;
 
1055
 
 
1056
  /* Ready for reading rows */
 
1057
  DBUG_VOID_RETURN;
 
1058
}
 
1059
 
 
1060
 
 
1061
/* Fetch and print one row from cursor */
 
1062
 
 
1063
int stmt_fetch_fetch_row(Stmt_fetch *fetch)
 
1064
{
 
1065
  int rc;
 
1066
  unsigned i;
 
1067
  DBUG_ENTER("stmt_fetch_fetch_row");
 
1068
 
 
1069
  if ((rc= mysql_stmt_fetch(fetch->handle)) == 0)
 
1070
  {
 
1071
    ++fetch->row_count;
 
1072
    if (!opt_silent)
 
1073
      printf("Stmt %d fetched row %d:\n", fetch->stmt_no, fetch->row_count);
 
1074
    for (i= 0; i < fetch->column_count; ++i)
 
1075
    {
 
1076
      fetch->out_data[i][fetch->out_data_length[i]]= '\0';
 
1077
      if (!opt_silent)
 
1078
        printf("column %d: %s\n", i+1, fetch->out_data[i]);
 
1079
    }
 
1080
  }
 
1081
  else
 
1082
    fetch->is_open= FALSE;
 
1083
  DBUG_RETURN(rc);
 
1084
}
 
1085
 
 
1086
 
 
1087
void stmt_fetch_close(Stmt_fetch *fetch)
 
1088
{
 
1089
  unsigned i;
 
1090
  DBUG_ENTER("stmt_fetch_close");
 
1091
 
 
1092
  for (i= 0; i < fetch->column_count; ++i)
 
1093
    free(fetch->out_data[i]);
 
1094
  free(fetch->out_data);
 
1095
  free(fetch->out_data_length);
 
1096
  free(fetch->bind_array);
 
1097
  mysql_stmt_close(fetch->handle);
 
1098
  DBUG_VOID_RETURN;
 
1099
}
 
1100
 
 
1101
/*
 
1102
  For given array of queries, open query_count cursors and fetch
 
1103
  from them in simultaneous manner.
 
1104
  In case there was an error in one of the cursors, continue
 
1105
  reading from the rest.
 
1106
*/
 
1107
 
 
1108
enum fetch_type { USE_ROW_BY_ROW_FETCH= 0, USE_STORE_RESULT= 1 };
 
1109
 
 
1110
my_bool fetch_n(const char **query_list, unsigned query_count,
 
1111
                enum fetch_type fetch_type)
 
1112
{
 
1113
  unsigned open_statements= query_count;
 
1114
  int rc, error_count= 0;
 
1115
  Stmt_fetch *fetch_array= (Stmt_fetch*) calloc(1, sizeof(Stmt_fetch) *
 
1116
                                                  query_count);
 
1117
  Stmt_fetch *fetch;
 
1118
  DBUG_ENTER("fetch_n");
 
1119
 
 
1120
  for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
 
1121
  {
 
1122
    /* Init will exit(1) in case of error */
 
1123
    stmt_fetch_init(fetch, fetch - fetch_array,
 
1124
                    query_list[fetch - fetch_array]);
 
1125
  }
 
1126
 
 
1127
  if (fetch_type == USE_STORE_RESULT)
 
1128
  {
 
1129
    for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
 
1130
    {
 
1131
      rc= mysql_stmt_store_result(fetch->handle);
 
1132
      check_execute(fetch->handle, rc);
 
1133
    }
 
1134
  }
 
1135
 
 
1136
  while (open_statements)
 
1137
  {
 
1138
    for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
 
1139
    {
 
1140
      if (fetch->is_open && (rc= stmt_fetch_fetch_row(fetch)))
 
1141
      {
 
1142
        open_statements--;
 
1143
        /*
 
1144
          We try to fetch from the rest of the statements in case of
 
1145
          error
 
1146
        */
 
1147
        if (rc != MYSQL_NO_DATA)
 
1148
        {
 
1149
          fprintf(stderr,
 
1150
                  "Got error reading rows from statement %d,\n"
 
1151
                  "query is: %s,\n"
 
1152
                  "error message: %s", (int) (fetch - fetch_array),
 
1153
                  fetch->query,
 
1154
                  mysql_stmt_error(fetch->handle));
 
1155
          error_count++;
 
1156
        }
 
1157
      }
 
1158
    }
 
1159
  }
 
1160
  if (error_count)
 
1161
    fprintf(stderr, "Fetch FAILED");
 
1162
  else
 
1163
  {
 
1164
    unsigned total_row_count= 0;
 
1165
    for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
 
1166
      total_row_count+= fetch->row_count;
 
1167
    if (!opt_silent)
 
1168
      printf("Success, total rows fetched: %d\n", total_row_count);
 
1169
  }
 
1170
  for (fetch= fetch_array; fetch < fetch_array + query_count; ++fetch)
 
1171
    stmt_fetch_close(fetch);
 
1172
  free(fetch_array);
 
1173
  DBUG_RETURN(error_count != 0);
 
1174
}
 
1175
 
 
1176
/* Separate thread query to test some cases */
 
1177
 
 
1178
static my_bool thread_query(char *query)
 
1179
{
 
1180
  MYSQL *l_mysql;
 
1181
  my_bool error;
 
1182
 
 
1183
  error= 0;
 
1184
  if (!opt_silent)
 
1185
    fprintf(stdout, "\n in thread_query(%s)", query);
 
1186
  if (!(l_mysql= mysql_client_init(NULL)))
 
1187
  {
 
1188
    myerror("mysql_client_init() failed");
 
1189
    return 1;
 
1190
  }
 
1191
  if (!(mysql_real_connect(l_mysql, opt_host, opt_user,
 
1192
                           opt_password, current_db, opt_port,
 
1193
                           opt_unix_socket, 0)))
 
1194
  {
 
1195
    myerror("connection failed");
 
1196
    error= 1;
 
1197
    goto end;
 
1198
  }
 
1199
  l_mysql->reconnect= 1;
 
1200
  if (mysql_query(l_mysql, (char *)query))
 
1201
  {
 
1202
     fprintf(stderr, "Query failed (%s)\n", mysql_error(l_mysql));
 
1203
     error= 1;
 
1204
     goto end;
 
1205
  }
 
1206
  mysql_commit(l_mysql);
 
1207
end:
 
1208
  mysql_close(l_mysql);
 
1209
  return error;
 
1210
}
 
1211
 
 
1212
 
 
1213
/* Query processing */
 
1214
 
 
1215
static void test_debug_example()
 
1216
{
 
1217
  int rc;
 
1218
  MYSQL_RES *result;
 
1219
 
 
1220
  myheader("test_debug_example");
 
1221
 
 
1222
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_debug_example");
 
1223
  myquery(rc);
 
1224
 
 
1225
  rc= mysql_query(mysql, "CREATE TABLE test_debug_example("
 
1226
                         "id INT PRIMARY KEY AUTO_INCREMENT, "
 
1227
                         "name VARCHAR(20), xxx INT)");
 
1228
  myquery(rc);
 
1229
 
 
1230
  rc= mysql_query(mysql, "INSERT INTO test_debug_example (name) "
 
1231
                         "VALUES ('mysql')");
 
1232
  myquery(rc);
 
1233
 
 
1234
  rc= mysql_query(mysql, "UPDATE test_debug_example SET name='updated' "
 
1235
                         "WHERE name='deleted'");
 
1236
  myquery(rc);
 
1237
 
 
1238
  rc= mysql_query(mysql, "SELECT * FROM test_debug_example where name='mysql'");
 
1239
  myquery(rc);
 
1240
 
 
1241
  result= mysql_use_result(mysql);
 
1242
  mytest(result);
 
1243
 
 
1244
  (void) my_process_result_set(result);
 
1245
  mysql_free_result(result);
 
1246
 
 
1247
  rc= mysql_query(mysql, "DROP TABLE test_debug_example");
 
1248
  myquery(rc);
 
1249
}
 
1250
 
 
1251
 
 
1252
/* Test autocommit feature for BDB tables */
 
1253
 
 
1254
static void test_tran_bdb()
 
1255
{
 
1256
  MYSQL_RES *result;
 
1257
  MYSQL_ROW row;
 
1258
  int       rc;
 
1259
 
 
1260
  myheader("test_tran_bdb");
 
1261
 
 
1262
  /* set AUTOCOMMIT to OFF */
 
1263
  rc= mysql_autocommit(mysql, FALSE);
 
1264
  myquery(rc);
 
1265
 
 
1266
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_demo_transaction");
 
1267
  myquery(rc);
 
1268
 
 
1269
 
 
1270
  /* create the table 'mytran_demo' of type BDB' or 'InnoDB' */
 
1271
  rc= mysql_query(mysql, "CREATE TABLE my_demo_transaction( "
 
1272
                         "col1 int , col2 varchar(30)) ENGINE= BDB");
 
1273
  myquery(rc);
 
1274
 
 
1275
  /* insert a row and commit the transaction */
 
1276
  rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(10, 'venu')");
 
1277
  myquery(rc);
 
1278
 
 
1279
  rc= mysql_commit(mysql);
 
1280
  myquery(rc);
 
1281
 
 
1282
  /* now insert the second row, and roll back the transaction */
 
1283
  rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(20, 'mysql')");
 
1284
  myquery(rc);
 
1285
 
 
1286
  rc= mysql_rollback(mysql);
 
1287
  myquery(rc);
 
1288
 
 
1289
  /* delete first row, and roll it back */
 
1290
  rc= mysql_query(mysql, "DELETE FROM my_demo_transaction WHERE col1= 10");
 
1291
  myquery(rc);
 
1292
 
 
1293
  rc= mysql_rollback(mysql);
 
1294
  myquery(rc);
 
1295
 
 
1296
  /* test the results now, only one row should exist */
 
1297
  rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction");
 
1298
  myquery(rc);
 
1299
 
 
1300
  /* get the result */
 
1301
  result= mysql_store_result(mysql);
 
1302
  mytest(result);
 
1303
 
 
1304
  (void) my_process_result_set(result);
 
1305
  mysql_free_result(result);
 
1306
 
 
1307
  /* test the results now, only one row should exist */
 
1308
  rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction");
 
1309
  myquery(rc);
 
1310
 
 
1311
  /* get the result */
 
1312
  result= mysql_use_result(mysql);
 
1313
  mytest(result);
 
1314
 
 
1315
  row= mysql_fetch_row(result);
 
1316
  mytest(row);
 
1317
 
 
1318
  row= mysql_fetch_row(result);
 
1319
  mytest_r(row);
 
1320
 
 
1321
  mysql_free_result(result);
 
1322
  mysql_autocommit(mysql, TRUE);
 
1323
}
 
1324
 
 
1325
 
 
1326
/* Test autocommit feature for InnoDB tables */
 
1327
 
 
1328
static void test_tran_innodb()
 
1329
{
 
1330
  MYSQL_RES *result;
 
1331
  MYSQL_ROW row;
 
1332
  int       rc;
 
1333
 
 
1334
  myheader("test_tran_innodb");
 
1335
 
 
1336
  /* set AUTOCOMMIT to OFF */
 
1337
  rc= mysql_autocommit(mysql, FALSE);
 
1338
  myquery(rc);
 
1339
 
 
1340
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_demo_transaction");
 
1341
  myquery(rc);
 
1342
 
 
1343
  /* create the table 'mytran_demo' of type BDB' or 'InnoDB' */
 
1344
  rc= mysql_query(mysql, "CREATE TABLE my_demo_transaction(col1 int, "
 
1345
                         "col2 varchar(30)) ENGINE= InnoDB");
 
1346
  myquery(rc);
 
1347
 
 
1348
  /* insert a row and commit the transaction */
 
1349
  rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(10, 'venu')");
 
1350
  myquery(rc);
 
1351
 
 
1352
  rc= mysql_commit(mysql);
 
1353
  myquery(rc);
 
1354
 
 
1355
  /* now insert the second row, and roll back the transaction */
 
1356
  rc= mysql_query(mysql, "INSERT INTO my_demo_transaction VALUES(20, 'mysql')");
 
1357
  myquery(rc);
 
1358
 
 
1359
  rc= mysql_rollback(mysql);
 
1360
  myquery(rc);
 
1361
 
 
1362
  /* delete first row, and roll it back */
 
1363
  rc= mysql_query(mysql, "DELETE FROM my_demo_transaction WHERE col1= 10");
 
1364
  myquery(rc);
 
1365
 
 
1366
  rc= mysql_rollback(mysql);
 
1367
  myquery(rc);
 
1368
 
 
1369
  /* test the results now, only one row should exist */
 
1370
  rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction");
 
1371
  myquery(rc);
 
1372
 
 
1373
  /* get the result */
 
1374
  result= mysql_store_result(mysql);
 
1375
  mytest(result);
 
1376
 
 
1377
  (void) my_process_result_set(result);
 
1378
  mysql_free_result(result);
 
1379
 
 
1380
  /* test the results now, only one row should exist */
 
1381
  rc= mysql_query(mysql, "SELECT * FROM my_demo_transaction");
 
1382
  myquery(rc);
 
1383
 
 
1384
  /* get the result */
 
1385
  result= mysql_use_result(mysql);
 
1386
  mytest(result);
 
1387
 
 
1388
  row= mysql_fetch_row(result);
 
1389
  mytest(row);
 
1390
 
 
1391
  row= mysql_fetch_row(result);
 
1392
  mytest_r(row);
 
1393
 
 
1394
  mysql_free_result(result);
 
1395
  mysql_autocommit(mysql, TRUE);
 
1396
}
 
1397
 
 
1398
 
 
1399
/* Test for BUG#7242 */
 
1400
 
 
1401
static void test_prepare_insert_update()
 
1402
{
 
1403
  MYSQL_STMT *stmt;
 
1404
  int        rc;
 
1405
  int        i;
 
1406
  const char *testcase[]= {
 
1407
    "CREATE TABLE t1 (a INT, b INT, c INT, UNIQUE (A), UNIQUE(B))",
 
1408
    "INSERT t1 VALUES (1,2,10), (3,4,20)",
 
1409
    "INSERT t1 VALUES (5,6,30), (7,4,40), (8,9,60) ON DUPLICATE KEY UPDATE c=c+100",
 
1410
    "SELECT * FROM t1",
 
1411
    "INSERT t1 SET a=5 ON DUPLICATE KEY UPDATE b=0",
 
1412
    "SELECT * FROM t1",
 
1413
    "INSERT t1 VALUES (2,1,11), (7,4,40) ON DUPLICATE KEY UPDATE c=c+VALUES(a)",
 
1414
    NULL};
 
1415
  const char **cur_query;
 
1416
 
 
1417
  myheader("test_prepare_insert_update");
 
1418
  
 
1419
  for (cur_query= testcase; *cur_query; cur_query++)
 
1420
  {
 
1421
    char query[MAX_TEST_QUERY_LENGTH];
 
1422
    printf("\nRunning query: %s", *cur_query);
 
1423
    strmov(query, *cur_query);
 
1424
    stmt= mysql_simple_prepare(mysql, query);
 
1425
    check_stmt(stmt);
 
1426
 
 
1427
    verify_param_count(stmt, 0);
 
1428
    rc= mysql_stmt_execute(stmt);
 
1429
 
 
1430
    check_execute(stmt, rc);
 
1431
    /* try the last query several times */
 
1432
    if (!cur_query[1])
 
1433
    {
 
1434
      for (i=0; i < 3;i++)
 
1435
      {
 
1436
        printf("\nExecuting last statement again");
 
1437
        rc= mysql_stmt_execute(stmt);
 
1438
        check_execute(stmt, rc);
 
1439
        rc= mysql_stmt_execute(stmt);
 
1440
        check_execute(stmt, rc);
 
1441
      }
 
1442
    }
 
1443
    mysql_stmt_close(stmt);
 
1444
  }
 
1445
 
 
1446
  rc= mysql_commit(mysql);
 
1447
  myquery(rc);
 
1448
}
 
1449
 
 
1450
/* Test simple prepares of all DML statements */
 
1451
 
 
1452
static void test_prepare_simple()
 
1453
{
 
1454
  MYSQL_STMT *stmt;
 
1455
  int        rc;
 
1456
  char query[MAX_TEST_QUERY_LENGTH];
 
1457
 
 
1458
  myheader("test_prepare_simple");
 
1459
 
 
1460
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_simple");
 
1461
  myquery(rc);
 
1462
 
 
1463
  rc= mysql_query(mysql, "CREATE TABLE test_prepare_simple("
 
1464
                         "id int, name varchar(50))");
 
1465
  myquery(rc);
 
1466
 
 
1467
  /* insert */
 
1468
  strmov(query, "INSERT INTO test_prepare_simple VALUES(?, ?)");
 
1469
  stmt= mysql_simple_prepare(mysql, query);
 
1470
  check_stmt(stmt);
 
1471
 
 
1472
  verify_param_count(stmt, 2);
 
1473
  mysql_stmt_close(stmt);
 
1474
 
 
1475
  /* update */
 
1476
  strmov(query, "UPDATE test_prepare_simple SET id=? "
 
1477
                "WHERE id=? AND CONVERT(name USING utf8)= ?");
 
1478
  stmt= mysql_simple_prepare(mysql, query);
 
1479
  check_stmt(stmt);
 
1480
 
 
1481
  verify_param_count(stmt, 3);
 
1482
  mysql_stmt_close(stmt);
 
1483
 
 
1484
  /* delete */
 
1485
  strmov(query, "DELETE FROM test_prepare_simple WHERE id=10");
 
1486
  stmt= mysql_simple_prepare(mysql, query);
 
1487
  check_stmt(stmt);
 
1488
 
 
1489
  verify_param_count(stmt, 0);
 
1490
 
 
1491
  rc= mysql_stmt_execute(stmt);
 
1492
  check_execute(stmt, rc);
 
1493
  mysql_stmt_close(stmt);
 
1494
 
 
1495
  /* delete */
 
1496
  strmov(query, "DELETE FROM test_prepare_simple WHERE id=?");
 
1497
  stmt= mysql_simple_prepare(mysql, query);
 
1498
  check_stmt(stmt);
 
1499
 
 
1500
  verify_param_count(stmt, 1);
 
1501
 
 
1502
  mysql_stmt_close(stmt);
 
1503
 
 
1504
  /* select */
 
1505
  strmov(query, "SELECT * FROM test_prepare_simple WHERE id=? "
 
1506
                "AND CONVERT(name USING utf8)= ?");
 
1507
  stmt= mysql_simple_prepare(mysql, query);
 
1508
  check_stmt(stmt);
 
1509
 
 
1510
  verify_param_count(stmt, 2);
 
1511
 
 
1512
  mysql_stmt_close(stmt);
 
1513
 
 
1514
  /* now fetch the results ..*/
 
1515
  rc= mysql_commit(mysql);
 
1516
  myquery(rc);
 
1517
}
 
1518
 
 
1519
 
 
1520
/* Test simple prepare field results */
 
1521
 
 
1522
static void test_prepare_field_result()
 
1523
{
 
1524
  MYSQL_STMT *stmt;
 
1525
  MYSQL_RES  *result;
 
1526
  int        rc;
 
1527
  char query[MAX_TEST_QUERY_LENGTH];
 
1528
 
 
1529
  myheader("test_prepare_field_result");
 
1530
 
 
1531
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_field_result");
 
1532
  myquery(rc);
 
1533
 
 
1534
  rc= mysql_query(mysql, "CREATE TABLE test_prepare_field_result(int_c int, "
 
1535
                         "var_c varchar(50), ts_c timestamp(14), "
 
1536
                         "char_c char(4), date_c date, extra tinyint)");
 
1537
  myquery(rc);
 
1538
 
 
1539
  /* insert */
 
1540
  strmov(query, "SELECT int_c, var_c, date_c as date, ts_c, char_c FROM "
 
1541
                " test_prepare_field_result as t1 WHERE int_c=?");
 
1542
  stmt= mysql_simple_prepare(mysql, query);
 
1543
  check_stmt(stmt);
 
1544
 
 
1545
  verify_param_count(stmt, 1);
 
1546
 
 
1547
  result= mysql_stmt_result_metadata(stmt);
 
1548
  mytest(result);
 
1549
 
 
1550
  my_print_result_metadata(result);
 
1551
 
 
1552
  if (!opt_silent)
 
1553
    fprintf(stdout, "\n\n field attributes:\n");
 
1554
  verify_prepare_field(result, 0, "int_c", "int_c", MYSQL_TYPE_LONG,
 
1555
                       "t1", "test_prepare_field_result", current_db, 11, 0);
 
1556
  verify_prepare_field(result, 1, "var_c", "var_c", MYSQL_TYPE_VAR_STRING,
 
1557
                       "t1", "test_prepare_field_result", current_db, 50, 0);
 
1558
  verify_prepare_field(result, 2, "date", "date_c", MYSQL_TYPE_DATE,
 
1559
                       "t1", "test_prepare_field_result", current_db, 10, 0);
 
1560
  verify_prepare_field(result, 3, "ts_c", "ts_c", MYSQL_TYPE_TIMESTAMP,
 
1561
                       "t1", "test_prepare_field_result", current_db, 19, 0);
 
1562
  verify_prepare_field(result, 4, "char_c", "char_c",
 
1563
                       (mysql_get_server_version(mysql) <= 50000 ?
 
1564
                        MYSQL_TYPE_VAR_STRING : MYSQL_TYPE_STRING),
 
1565
                       "t1", "test_prepare_field_result", current_db, 4, 0);
 
1566
 
 
1567
  verify_field_count(result, 5);
 
1568
  mysql_free_result(result);
 
1569
  mysql_stmt_close(stmt);
 
1570
}
 
1571
 
 
1572
 
 
1573
/* Test simple prepare field results */
 
1574
 
 
1575
static void test_prepare_syntax()
 
1576
{
 
1577
  MYSQL_STMT *stmt;
 
1578
  int        rc;
 
1579
  char query[MAX_TEST_QUERY_LENGTH];
 
1580
 
 
1581
  myheader("test_prepare_syntax");
 
1582
 
 
1583
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_syntax");
 
1584
  myquery(rc);
 
1585
 
 
1586
  rc= mysql_query(mysql, "CREATE TABLE test_prepare_syntax("
 
1587
                         "id int, name varchar(50), extra int)");
 
1588
  myquery(rc);
 
1589
 
 
1590
  strmov(query, "INSERT INTO test_prepare_syntax VALUES(?");
 
1591
  stmt= mysql_simple_prepare(mysql, query);
 
1592
  check_stmt_r(stmt);
 
1593
 
 
1594
  strmov(query, "SELECT id, name FROM test_prepare_syntax WHERE id=? AND WHERE");
 
1595
  stmt= mysql_simple_prepare(mysql, query);
 
1596
  check_stmt_r(stmt);
 
1597
 
 
1598
  /* now fetch the results ..*/
 
1599
  rc= mysql_commit(mysql);
 
1600
  myquery(rc);
 
1601
}
 
1602
 
 
1603
 
 
1604
/* Test a simple prepare */
 
1605
 
 
1606
static void test_prepare()
 
1607
{
 
1608
  MYSQL_STMT *stmt;
 
1609
  int        rc, i;
 
1610
  int        int_data, o_int_data;
 
1611
  char       str_data[50], data[50];
 
1612
  char       tiny_data, o_tiny_data;
 
1613
  short      small_data, o_small_data;
 
1614
  longlong   big_data, o_big_data;
 
1615
  float      real_data, o_real_data;
 
1616
  double     double_data, o_double_data;
 
1617
  ulong      length[7], len;
 
1618
  my_bool    is_null[7];
 
1619
  char       llbuf[22];
 
1620
  MYSQL_BIND my_bind[7];
 
1621
  char query[MAX_TEST_QUERY_LENGTH];
 
1622
 
 
1623
  myheader("test_prepare");
 
1624
 
 
1625
  rc= mysql_autocommit(mysql, TRUE);
 
1626
  myquery(rc);
 
1627
 
 
1628
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_prepare");
 
1629
  myquery(rc);
 
1630
 
 
1631
  rc= mysql_query(mysql, "CREATE TABLE my_prepare(col1 tinyint, "
 
1632
                         "col2 varchar(15), col3 int, "
 
1633
                         "col4 smallint, col5 bigint, "
 
1634
                         "col6 float, col7 double )");
 
1635
  myquery(rc);
 
1636
 
 
1637
  /* insert by prepare */
 
1638
  strxmov(query, "INSERT INTO my_prepare VALUES(?, ?, ?, ?, ?, ?, ?)", NullS);
 
1639
  stmt= mysql_simple_prepare(mysql, query);
 
1640
  check_stmt(stmt);
 
1641
 
 
1642
  verify_param_count(stmt, 7);
 
1643
 
 
1644
  bzero((char*) my_bind, sizeof(my_bind));
 
1645
 
 
1646
  /* tinyint */
 
1647
  my_bind[0].buffer_type= MYSQL_TYPE_TINY;
 
1648
  my_bind[0].buffer= (void *)&tiny_data;
 
1649
  /* string */
 
1650
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
1651
  my_bind[1].buffer= (void *)str_data;
 
1652
  my_bind[1].buffer_length= 1000;                  /* Max string length */
 
1653
  /* integer */
 
1654
  my_bind[2].buffer_type= MYSQL_TYPE_LONG;
 
1655
  my_bind[2].buffer= (void *)&int_data;
 
1656
  /* short */
 
1657
  my_bind[3].buffer_type= MYSQL_TYPE_SHORT;
 
1658
  my_bind[3].buffer= (void *)&small_data;
 
1659
  /* bigint */
 
1660
  my_bind[4].buffer_type= MYSQL_TYPE_LONGLONG;
 
1661
  my_bind[4].buffer= (void *)&big_data;
 
1662
  /* float */
 
1663
  my_bind[5].buffer_type= MYSQL_TYPE_FLOAT;
 
1664
  my_bind[5].buffer= (void *)&real_data;
 
1665
  /* double */
 
1666
  my_bind[6].buffer_type= MYSQL_TYPE_DOUBLE;
 
1667
  my_bind[6].buffer= (void *)&double_data;
 
1668
 
 
1669
  for (i= 0; i < (int) array_elements(my_bind); i++)
 
1670
  {
 
1671
    my_bind[i].length= &length[i];
 
1672
    my_bind[i].is_null= &is_null[i];
 
1673
    is_null[i]= 0;
 
1674
  }
 
1675
 
 
1676
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
1677
  check_execute(stmt, rc);
 
1678
 
 
1679
  int_data= 320;
 
1680
  small_data= 1867;
 
1681
  big_data= 1000;
 
1682
  real_data= 2;
 
1683
  double_data= 6578.001;
 
1684
 
 
1685
  /* now, execute the prepared statement to insert 10 records.. */
 
1686
  for (tiny_data= 0; tiny_data < 100; tiny_data++)
 
1687
  {
 
1688
    length[1]= my_sprintf(str_data, (str_data, "MySQL%d", int_data));
 
1689
    rc= mysql_stmt_execute(stmt);
 
1690
    check_execute(stmt, rc);
 
1691
    int_data += 25;
 
1692
    small_data += 10;
 
1693
    big_data += 100;
 
1694
    real_data += 1;
 
1695
    double_data += 10.09;
 
1696
  }
 
1697
 
 
1698
  mysql_stmt_close(stmt);
 
1699
 
 
1700
  /* now fetch the results ..*/
 
1701
  rc= mysql_commit(mysql);
 
1702
  myquery(rc);
 
1703
 
 
1704
  /* test the results now, only one row should exist */
 
1705
  rc= my_stmt_result("SELECT * FROM my_prepare");
 
1706
  DIE_UNLESS(tiny_data == (char) rc);
 
1707
 
 
1708
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM my_prepare");
 
1709
  check_stmt(stmt);
 
1710
 
 
1711
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
1712
  check_execute(stmt, rc);
 
1713
 
 
1714
  /* get the result */
 
1715
  rc= mysql_stmt_execute(stmt);
 
1716
  check_execute(stmt, rc);
 
1717
 
 
1718
  o_int_data= 320;
 
1719
  o_small_data= 1867;
 
1720
  o_big_data= 1000;
 
1721
  o_real_data= 2;
 
1722
  o_double_data= 6578.001;
 
1723
 
 
1724
  /* now, execute the prepared statement to insert 10 records.. */
 
1725
  for (o_tiny_data= 0; o_tiny_data < 100; o_tiny_data++)
 
1726
  {
 
1727
    len= my_sprintf(data, (data, "MySQL%d", o_int_data));
 
1728
 
 
1729
    rc= mysql_stmt_fetch(stmt);
 
1730
    check_execute(stmt, rc);
 
1731
 
 
1732
    if (!opt_silent)
 
1733
    {
 
1734
      fprintf(stdout, "\n");
 
1735
      fprintf(stdout, "\n\t tiny   : %d (%lu)", tiny_data, length[0]);
 
1736
      fprintf(stdout, "\n\t short  : %d (%lu)", small_data, length[3]);
 
1737
      fprintf(stdout, "\n\t int    : %d (%lu)", int_data, length[2]);
 
1738
      fprintf(stdout, "\n\t big    : %s (%lu)", llstr(big_data, llbuf),
 
1739
              length[4]);
 
1740
 
 
1741
      fprintf(stdout, "\n\t float  : %f (%lu)", real_data, length[5]);
 
1742
      fprintf(stdout, "\n\t double : %f (%lu)", double_data, length[6]);
 
1743
 
 
1744
      fprintf(stdout, "\n\t str    : %s (%lu)", str_data, length[1]);
 
1745
    }
 
1746
 
 
1747
    DIE_UNLESS(tiny_data == o_tiny_data);
 
1748
    DIE_UNLESS(is_null[0] == 0);
 
1749
    DIE_UNLESS(length[0] == 1);
 
1750
 
 
1751
    DIE_UNLESS(int_data == o_int_data);
 
1752
    DIE_UNLESS(length[2] == 4);
 
1753
 
 
1754
    DIE_UNLESS(small_data == o_small_data);
 
1755
    DIE_UNLESS(length[3] == 2);
 
1756
 
 
1757
    DIE_UNLESS(big_data == o_big_data);
 
1758
    DIE_UNLESS(length[4] == 8);
 
1759
 
 
1760
    DIE_UNLESS(real_data == o_real_data);
 
1761
    DIE_UNLESS(length[5] == 4);
 
1762
 
 
1763
    DIE_UNLESS(cmp_double(&double_data, &o_double_data));
 
1764
    DIE_UNLESS(length[6] == 8);
 
1765
 
 
1766
    DIE_UNLESS(strcmp(data, str_data) == 0);
 
1767
    DIE_UNLESS(length[1] == len);
 
1768
 
 
1769
    o_int_data += 25;
 
1770
    o_small_data += 10;
 
1771
    o_big_data += 100;
 
1772
    o_real_data += 1;
 
1773
    o_double_data += 10.09;
 
1774
  }
 
1775
 
 
1776
  rc= mysql_stmt_fetch(stmt);
 
1777
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
1778
 
 
1779
  mysql_stmt_close(stmt);
 
1780
 
 
1781
}
 
1782
 
 
1783
 
 
1784
/* Test double comparision */
 
1785
 
 
1786
static void test_double_compare()
 
1787
{
 
1788
  MYSQL_STMT *stmt;
 
1789
  int        rc;
 
1790
  char       real_data[10], tiny_data;
 
1791
  double     double_data;
 
1792
  MYSQL_RES  *result;
 
1793
  MYSQL_BIND my_bind[3];
 
1794
  ulong      length[3];
 
1795
  char query[MAX_TEST_QUERY_LENGTH];
 
1796
 
 
1797
  myheader("test_double_compare");
 
1798
 
 
1799
  rc= mysql_autocommit(mysql, TRUE);
 
1800
  myquery(rc);
 
1801
 
 
1802
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_double_compare");
 
1803
  myquery(rc);
 
1804
 
 
1805
  rc= mysql_query(mysql, "CREATE TABLE test_double_compare(col1 tinyint, "
 
1806
                         " col2 float, col3 double )");
 
1807
  myquery(rc);
 
1808
 
 
1809
  rc= mysql_query(mysql, "INSERT INTO test_double_compare "
 
1810
                         "VALUES (1, 10.2, 34.5)");
 
1811
  myquery(rc);
 
1812
 
 
1813
  strmov(query, "UPDATE test_double_compare SET col1=100 "
 
1814
                "WHERE col1 = ? AND col2 = ? AND COL3 = ?");
 
1815
  stmt= mysql_simple_prepare(mysql, query);
 
1816
  check_stmt(stmt);
 
1817
 
 
1818
  verify_param_count(stmt, 3);
 
1819
 
 
1820
  /* Always bzero bind array because there can be internal members */
 
1821
  bzero((char*) my_bind, sizeof(my_bind));
 
1822
 
 
1823
  /* tinyint */
 
1824
  my_bind[0].buffer_type= MYSQL_TYPE_TINY;
 
1825
  my_bind[0].buffer= (void *)&tiny_data;
 
1826
 
 
1827
  /* string->float */
 
1828
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
1829
  my_bind[1].buffer= (void *)&real_data;
 
1830
  my_bind[1].buffer_length= sizeof(real_data);
 
1831
  my_bind[1].length= &length[1];
 
1832
  length[1]= 10;
 
1833
 
 
1834
  /* double */
 
1835
  my_bind[2].buffer_type= MYSQL_TYPE_DOUBLE;
 
1836
  my_bind[2].buffer= (void *)&double_data;
 
1837
 
 
1838
  tiny_data= 1;
 
1839
  strmov(real_data, "10.2");
 
1840
  double_data= 34.5;
 
1841
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
1842
  check_execute(stmt, rc);
 
1843
 
 
1844
  rc= mysql_stmt_execute(stmt);
 
1845
  check_execute(stmt, rc);
 
1846
 
 
1847
  verify_affected_rows(0);
 
1848
 
 
1849
  mysql_stmt_close(stmt);
 
1850
 
 
1851
  /* now fetch the results ..*/
 
1852
  rc= mysql_commit(mysql);
 
1853
  myquery(rc);
 
1854
 
 
1855
  /* test the results now, only one row should exist */
 
1856
  rc= mysql_query(mysql, "SELECT * FROM test_double_compare");
 
1857
  myquery(rc);
 
1858
 
 
1859
  /* get the result */
 
1860
  result= mysql_store_result(mysql);
 
1861
  mytest(result);
 
1862
 
 
1863
  rc= my_process_result_set(result);
 
1864
  DIE_UNLESS((int)tiny_data == rc);
 
1865
  mysql_free_result(result);
 
1866
}
 
1867
 
 
1868
 
 
1869
/* Test simple null */
 
1870
 
 
1871
static void test_null()
 
1872
{
 
1873
  MYSQL_STMT *stmt;
 
1874
  int        rc;
 
1875
  uint       nData;
 
1876
  MYSQL_BIND my_bind[2];
 
1877
  my_bool    is_null[2];
 
1878
  char query[MAX_TEST_QUERY_LENGTH];
 
1879
 
 
1880
  myheader("test_null");
 
1881
 
 
1882
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_null");
 
1883
  myquery(rc);
 
1884
 
 
1885
  rc= mysql_query(mysql, "CREATE TABLE test_null(col1 int, col2 varchar(50))");
 
1886
  myquery(rc);
 
1887
 
 
1888
  /* insert by prepare, wrong column name */
 
1889
  strmov(query, "INSERT INTO test_null(col3, col2) VALUES(?, ?)");
 
1890
  stmt= mysql_simple_prepare(mysql, query);
 
1891
  check_stmt_r(stmt);
 
1892
 
 
1893
  strmov(query, "INSERT INTO test_null(col1, col2) VALUES(?, ?)");
 
1894
  stmt= mysql_simple_prepare(mysql, query);
 
1895
  check_stmt(stmt);
 
1896
 
 
1897
  verify_param_count(stmt, 2);
 
1898
 
 
1899
  /* Always bzero all members of bind parameter */
 
1900
  bzero((char*) my_bind, sizeof(my_bind));
 
1901
 
 
1902
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
1903
  my_bind[0].is_null= &is_null[0];
 
1904
  is_null[0]= 1;
 
1905
  my_bind[1]= my_bind[0];
 
1906
 
 
1907
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
1908
  check_execute(stmt, rc);
 
1909
 
 
1910
  /* now, execute the prepared statement to insert 10 records.. */
 
1911
  for (nData= 0; nData<10; nData++)
 
1912
  {
 
1913
    rc= mysql_stmt_execute(stmt);
 
1914
    check_execute(stmt, rc);
 
1915
  }
 
1916
 
 
1917
  /* Re-bind with MYSQL_TYPE_NULL */
 
1918
  my_bind[0].buffer_type= MYSQL_TYPE_NULL;
 
1919
  is_null[0]= 0; /* reset */
 
1920
  my_bind[1]= my_bind[0];
 
1921
 
 
1922
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
1923
  check_execute(stmt, rc);
 
1924
 
 
1925
  for (nData= 0; nData<10; nData++)
 
1926
  {
 
1927
    rc= mysql_stmt_execute(stmt);
 
1928
    check_execute(stmt, rc);
 
1929
  }
 
1930
 
 
1931
  mysql_stmt_close(stmt);
 
1932
 
 
1933
  /* now fetch the results ..*/
 
1934
  rc= mysql_commit(mysql);
 
1935
  myquery(rc);
 
1936
 
 
1937
  nData*= 2;
 
1938
  rc= my_stmt_result("SELECT * FROM test_null");;
 
1939
  DIE_UNLESS((int) nData == rc);
 
1940
 
 
1941
  /* Fetch results */
 
1942
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
1943
  my_bind[0].buffer= (void *)&nData; /* this buffer won't be altered */
 
1944
  my_bind[0].length= 0;
 
1945
  my_bind[1]= my_bind[0];
 
1946
  my_bind[0].is_null= &is_null[0];
 
1947
  my_bind[1].is_null= &is_null[1];
 
1948
 
 
1949
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_null");
 
1950
  check_stmt(stmt);
 
1951
 
 
1952
  rc= mysql_stmt_execute(stmt);
 
1953
  check_execute(stmt, rc);
 
1954
 
 
1955
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
1956
  check_execute(stmt, rc);
 
1957
 
 
1958
  rc= 0;
 
1959
  is_null[0]= is_null[1]= 0;
 
1960
  while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
 
1961
  {
 
1962
    DIE_UNLESS(is_null[0]);
 
1963
    DIE_UNLESS(is_null[1]);
 
1964
    rc++;
 
1965
    is_null[0]= is_null[1]= 0;
 
1966
  }
 
1967
  DIE_UNLESS(rc == (int) nData);
 
1968
  mysql_stmt_close(stmt);
 
1969
}
 
1970
 
 
1971
 
 
1972
/* Test for NULL as PS parameter (BUG#3367, BUG#3371) */
 
1973
 
 
1974
static void test_ps_null_param()
 
1975
{
 
1976
  MYSQL_STMT *stmt;
 
1977
  int        rc;
 
1978
 
 
1979
  MYSQL_BIND in_bind;
 
1980
  my_bool    in_is_null;
 
1981
  long int   in_long;
 
1982
 
 
1983
  MYSQL_BIND out_bind;
 
1984
  ulong      out_length;
 
1985
  my_bool    out_is_null;
 
1986
  char       out_str_data[20];
 
1987
 
 
1988
  const char *queries[]= {"select ?", "select ?+1",
 
1989
                    "select col1 from test_ps_nulls where col1 <=> ?",
 
1990
                    NULL
 
1991
                    };
 
1992
  const char **cur_query= queries;
 
1993
 
 
1994
  myheader("test_null_ps_param_in_result");
 
1995
 
 
1996
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ps_nulls");
 
1997
  myquery(rc);
 
1998
 
 
1999
  rc= mysql_query(mysql, "CREATE TABLE test_ps_nulls(col1 int)");
 
2000
  myquery(rc);
 
2001
 
 
2002
  rc= mysql_query(mysql, "INSERT INTO test_ps_nulls values (1), (null)");
 
2003
  myquery(rc);
 
2004
 
 
2005
  /* Always bzero all members of bind parameter */
 
2006
  bzero((char*) &in_bind, sizeof(in_bind));
 
2007
  bzero((char*) &out_bind, sizeof(out_bind));
 
2008
 
 
2009
  in_bind.buffer_type= MYSQL_TYPE_LONG;
 
2010
  in_bind.is_null= &in_is_null;
 
2011
  in_bind.length= 0;
 
2012
  in_bind.buffer= (void *)&in_long;
 
2013
  in_is_null= 1;
 
2014
  in_long= 1;
 
2015
 
 
2016
  out_bind.buffer_type= MYSQL_TYPE_STRING;
 
2017
  out_bind.is_null= &out_is_null;
 
2018
  out_bind.length= &out_length;
 
2019
  out_bind.buffer= out_str_data;
 
2020
  out_bind.buffer_length= array_elements(out_str_data);
 
2021
 
 
2022
  /* Execute several queries, all returning NULL in result. */
 
2023
  for(cur_query= queries; *cur_query; cur_query++)
 
2024
  {
 
2025
    char query[MAX_TEST_QUERY_LENGTH];
 
2026
    strmov(query, *cur_query);
 
2027
    stmt= mysql_simple_prepare(mysql, query);
 
2028
    check_stmt(stmt);
 
2029
    verify_param_count(stmt, 1);
 
2030
 
 
2031
    rc= mysql_stmt_bind_param(stmt, &in_bind);
 
2032
    check_execute(stmt, rc);
 
2033
    rc= mysql_stmt_bind_result(stmt, &out_bind);
 
2034
    check_execute(stmt, rc);
 
2035
    rc= mysql_stmt_execute(stmt);
 
2036
    check_execute(stmt, rc);
 
2037
    rc= mysql_stmt_fetch(stmt);
 
2038
    DIE_UNLESS(rc != MYSQL_NO_DATA);
 
2039
    DIE_UNLESS(out_is_null);
 
2040
    rc= mysql_stmt_fetch(stmt);
 
2041
    DIE_UNLESS(rc == MYSQL_NO_DATA);
 
2042
    mysql_stmt_close(stmt);
 
2043
  }
 
2044
}
 
2045
 
 
2046
 
 
2047
/* Test fetch null */
 
2048
 
 
2049
static void test_fetch_null()
 
2050
{
 
2051
  MYSQL_STMT *stmt;
 
2052
  int        rc;
 
2053
  int        i, nData;
 
2054
  MYSQL_BIND my_bind[11];
 
2055
  ulong      length[11];
 
2056
  my_bool    is_null[11];
 
2057
  char query[MAX_TEST_QUERY_LENGTH];
 
2058
 
 
2059
  myheader("test_fetch_null");
 
2060
 
 
2061
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_fetch_null");
 
2062
  myquery(rc);
 
2063
 
 
2064
  rc= mysql_query(mysql, "CREATE TABLE test_fetch_null("
 
2065
                         " col1 tinyint, col2 smallint, "
 
2066
                         " col3 int, col4 bigint, "
 
2067
                         " col5 float, col6 double, "
 
2068
                         " col7 date, col8 time, "
 
2069
                         " col9 varbinary(10), "
 
2070
                         " col10 varchar(50), "
 
2071
                         " col11 char(20))");
 
2072
  myquery(rc);
 
2073
 
 
2074
  rc= mysql_query(mysql, "INSERT INTO test_fetch_null (col11) "
 
2075
                         "VALUES (1000), (88), (389789)");
 
2076
  myquery(rc);
 
2077
 
 
2078
  rc= mysql_commit(mysql);
 
2079
  myquery(rc);
 
2080
 
 
2081
  /* fetch */
 
2082
  bzero((char*) my_bind, sizeof(my_bind));
 
2083
  for (i= 0; i < (int) array_elements(my_bind); i++)
 
2084
  {
 
2085
    my_bind[i].buffer_type= MYSQL_TYPE_LONG;
 
2086
    my_bind[i].is_null= &is_null[i];
 
2087
    my_bind[i].length= &length[i];
 
2088
  }
 
2089
  my_bind[i-1].buffer= (void *)&nData;              /* Last column is not null */
 
2090
 
 
2091
  strmov((char *)query , "SELECT * FROM test_fetch_null");
 
2092
 
 
2093
  rc= my_stmt_result(query);
 
2094
  DIE_UNLESS(rc == 3);
 
2095
 
 
2096
  stmt= mysql_simple_prepare(mysql, query);
 
2097
  check_stmt(stmt);
 
2098
 
 
2099
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
2100
  check_execute(stmt, rc);
 
2101
 
 
2102
  rc= mysql_stmt_execute(stmt);
 
2103
  check_execute(stmt, rc);
 
2104
 
 
2105
  rc= 0;
 
2106
  while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
 
2107
  {
 
2108
    rc++;
 
2109
    for (i= 0; i < 10; i++)
 
2110
    {
 
2111
      if (!opt_silent)
 
2112
        fprintf(stdout, "\n data[%d] : %s", i,
 
2113
                is_null[i] ? "NULL" : "NOT NULL");
 
2114
      DIE_UNLESS(is_null[i]);
 
2115
    }
 
2116
    if (!opt_silent)
 
2117
      fprintf(stdout, "\n data[%d]: %d", i, nData);
 
2118
    DIE_UNLESS(nData == 1000 || nData == 88 || nData == 389789);
 
2119
    DIE_UNLESS(is_null[i] == 0);
 
2120
    DIE_UNLESS(length[i] == 4);
 
2121
  }
 
2122
  DIE_UNLESS(rc == 3);
 
2123
  mysql_stmt_close(stmt);
 
2124
}
 
2125
 
 
2126
 
 
2127
/* Test simple select */
 
2128
 
 
2129
static void test_select_version()
 
2130
{
 
2131
  MYSQL_STMT *stmt;
 
2132
  int        rc;
 
2133
 
 
2134
  myheader("test_select_version");
 
2135
 
 
2136
  stmt= mysql_simple_prepare(mysql, "SELECT @@version");
 
2137
  check_stmt(stmt);
 
2138
 
 
2139
  verify_param_count(stmt, 0);
 
2140
 
 
2141
  rc= mysql_stmt_execute(stmt);
 
2142
  check_execute(stmt, rc);
 
2143
 
 
2144
  my_process_stmt_result(stmt);
 
2145
  mysql_stmt_close(stmt);
 
2146
}
 
2147
 
 
2148
 
 
2149
/* Test simple show */
 
2150
 
 
2151
static void test_select_show_table()
 
2152
{
 
2153
  MYSQL_STMT *stmt;
 
2154
  int        rc, i;
 
2155
 
 
2156
  myheader("test_select_show_table");
 
2157
 
 
2158
  stmt= mysql_simple_prepare(mysql, "SHOW TABLES FROM mysql");
 
2159
  check_stmt(stmt);
 
2160
 
 
2161
  verify_param_count(stmt, 0);
 
2162
 
 
2163
  for (i= 1; i < 3; i++)
 
2164
  {
 
2165
    rc= mysql_stmt_execute(stmt);
 
2166
    check_execute(stmt, rc);
 
2167
  }
 
2168
 
 
2169
  my_process_stmt_result(stmt);
 
2170
  mysql_stmt_close(stmt);
 
2171
}
 
2172
 
 
2173
 
 
2174
/* Test simple select to debug */
 
2175
 
 
2176
static void test_select_direct()
 
2177
{
 
2178
  int        rc;
 
2179
  MYSQL_RES  *result;
 
2180
 
 
2181
  myheader("test_select_direct");
 
2182
 
 
2183
  rc= mysql_autocommit(mysql, TRUE);
 
2184
  myquery(rc);
 
2185
 
 
2186
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
 
2187
  myquery(rc);
 
2188
 
 
2189
  rc= mysql_query(mysql, "CREATE TABLE test_select(id int, id1 tinyint, "
 
2190
                                                 " id2 float, "
 
2191
                                                 " id3 double, "
 
2192
                                                 " name varchar(50))");
 
2193
  myquery(rc);
 
2194
 
 
2195
  /* insert a row and commit the transaction */
 
2196
  rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 5, 2.3, 4.5, 'venu')");
 
2197
  myquery(rc);
 
2198
 
 
2199
  rc= mysql_commit(mysql);
 
2200
  myquery(rc);
 
2201
 
 
2202
  rc= mysql_query(mysql, "SELECT * FROM test_select");
 
2203
  myquery(rc);
 
2204
 
 
2205
  /* get the result */
 
2206
  result= mysql_store_result(mysql);
 
2207
  mytest(result);
 
2208
 
 
2209
  (void) my_process_result_set(result);
 
2210
  mysql_free_result(result);
 
2211
}
 
2212
 
 
2213
 
 
2214
/* Test simple select with prepare */
 
2215
 
 
2216
static void test_select_prepare()
 
2217
{
 
2218
  int        rc;
 
2219
  MYSQL_STMT *stmt;
 
2220
 
 
2221
  myheader("test_select_prepare");
 
2222
 
 
2223
  rc= mysql_autocommit(mysql, TRUE);
 
2224
  myquery(rc);
 
2225
 
 
2226
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
 
2227
  myquery(rc);
 
2228
 
 
2229
  rc= mysql_query(mysql, "CREATE TABLE test_select(id int, name varchar(50))");
 
2230
  myquery(rc);
 
2231
 
 
2232
  /* insert a row and commit the transaction */
 
2233
  rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 'venu')");
 
2234
  myquery(rc);
 
2235
 
 
2236
  rc= mysql_commit(mysql);
 
2237
  myquery(rc);
 
2238
 
 
2239
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_select");
 
2240
  check_stmt(stmt);
 
2241
 
 
2242
  rc= mysql_stmt_execute(stmt);
 
2243
  check_execute(stmt, rc);
 
2244
 
 
2245
  rc= my_process_stmt_result(stmt);
 
2246
  DIE_UNLESS(rc == 1);
 
2247
  mysql_stmt_close(stmt);
 
2248
 
 
2249
  rc= mysql_query(mysql, "DROP TABLE test_select");
 
2250
  myquery(rc);
 
2251
 
 
2252
  rc= mysql_query(mysql, "CREATE TABLE test_select(id tinyint, id1 int, "
 
2253
                                                "  id2 float, id3 float, "
 
2254
                                                "  name varchar(50))");
 
2255
  myquery(rc);
 
2256
 
 
2257
  /* insert a row and commit the transaction */
 
2258
  rc= mysql_query(mysql, "INSERT INTO test_select(id, id1, id2, name) VALUES(10, 5, 2.3, 'venu')");
 
2259
  myquery(rc);
 
2260
 
 
2261
  rc= mysql_commit(mysql);
 
2262
  myquery(rc);
 
2263
 
 
2264
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_select");
 
2265
  check_stmt(stmt);
 
2266
 
 
2267
  rc= mysql_stmt_execute(stmt);
 
2268
  check_execute(stmt, rc);
 
2269
 
 
2270
  rc= my_process_stmt_result(stmt);
 
2271
  DIE_UNLESS(rc == 1);
 
2272
  mysql_stmt_close(stmt);
 
2273
}
 
2274
 
 
2275
 
 
2276
/* Test simple select */
 
2277
 
 
2278
static void test_select()
 
2279
{
 
2280
  MYSQL_STMT *stmt;
 
2281
  int        rc;
 
2282
  char       szData[25];
 
2283
  int        nData= 1;
 
2284
  MYSQL_BIND my_bind[2];
 
2285
  ulong length[2];
 
2286
  char query[MAX_TEST_QUERY_LENGTH];
 
2287
 
 
2288
  myheader("test_select");
 
2289
 
 
2290
  rc= mysql_autocommit(mysql, TRUE);
 
2291
  myquery(rc);
 
2292
 
 
2293
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
 
2294
  myquery(rc);
 
2295
 
 
2296
  rc= mysql_query(mysql, "CREATE TABLE test_select(id int, name varchar(50))");
 
2297
  myquery(rc);
 
2298
 
 
2299
  /* insert a row and commit the transaction */
 
2300
  rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 'venu')");
 
2301
  myquery(rc);
 
2302
 
 
2303
  /* now insert the second row, and roll back the transaction */
 
2304
  rc= mysql_query(mysql, "INSERT INTO test_select VALUES(20, 'mysql')");
 
2305
  myquery(rc);
 
2306
 
 
2307
  rc= mysql_commit(mysql);
 
2308
  myquery(rc);
 
2309
 
 
2310
  strmov(query, "SELECT * FROM test_select WHERE id= ? "
 
2311
                "AND CONVERT(name USING utf8) =?");
 
2312
  stmt= mysql_simple_prepare(mysql, query);
 
2313
  check_stmt(stmt);
 
2314
 
 
2315
  verify_param_count(stmt, 2);
 
2316
 
 
2317
  /* Always bzero all members of bind parameter */
 
2318
  bzero((char*) my_bind, sizeof(my_bind));
 
2319
 
 
2320
  /* string data */
 
2321
  nData= 10;
 
2322
  strmov(szData, (char *)"venu");
 
2323
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
2324
  my_bind[1].buffer= (void *)szData;
 
2325
  my_bind[1].buffer_length= 4;
 
2326
  my_bind[1].length= &length[1];
 
2327
  length[1]= 4;
 
2328
 
 
2329
  my_bind[0].buffer= (void *)&nData;
 
2330
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
2331
 
 
2332
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2333
  check_execute(stmt, rc);
 
2334
 
 
2335
  rc= mysql_stmt_execute(stmt);
 
2336
  check_execute(stmt, rc);
 
2337
 
 
2338
  rc= my_process_stmt_result(stmt);
 
2339
  DIE_UNLESS(rc == 1);
 
2340
 
 
2341
  mysql_stmt_close(stmt);
 
2342
}
 
2343
 
 
2344
 
 
2345
/*
 
2346
  Test for BUG#3420 ("select id1, value1 from t where id= ? or value= ?"
 
2347
  returns all rows in the table)
 
2348
*/
 
2349
 
 
2350
static void test_ps_conj_select()
 
2351
{
 
2352
  MYSQL_STMT *stmt;
 
2353
  int        rc;
 
2354
  MYSQL_BIND my_bind[2];
 
2355
  int32      int_data;
 
2356
  char       str_data[32];
 
2357
  unsigned long str_length;
 
2358
  char query[MAX_TEST_QUERY_LENGTH];
 
2359
  myheader("test_ps_conj_select");
 
2360
 
 
2361
  rc= mysql_query(mysql, "drop table if exists t1");
 
2362
  myquery(rc);
 
2363
 
 
2364
  rc= mysql_query(mysql, "create table t1 (id1 int(11) NOT NULL default '0', "
 
2365
                         "value2 varchar(100), value1 varchar(100))");
 
2366
  myquery(rc);
 
2367
 
 
2368
  rc= mysql_query(mysql, "insert into t1 values (1, 'hh', 'hh'), "
 
2369
                          "(2, 'hh', 'hh'), (1, 'ii', 'ii'), (2, 'ii', 'ii')");
 
2370
  myquery(rc);
 
2371
 
 
2372
  strmov(query, "select id1, value1 from t1 where id1= ? or "
 
2373
                "CONVERT(value1 USING utf8)= ?");
 
2374
  stmt= mysql_simple_prepare(mysql, query);
 
2375
  check_stmt(stmt);
 
2376
 
 
2377
  verify_param_count(stmt, 2);
 
2378
 
 
2379
  /* Always bzero all members of bind parameter */
 
2380
  bzero((char*) my_bind, sizeof(my_bind));
 
2381
 
 
2382
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
2383
  my_bind[0].buffer= (void *)&int_data;
 
2384
 
 
2385
  my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
 
2386
  my_bind[1].buffer= (void *)str_data;
 
2387
  my_bind[1].buffer_length= array_elements(str_data);
 
2388
  my_bind[1].length= &str_length;
 
2389
 
 
2390
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2391
  check_execute(stmt, rc);
 
2392
 
 
2393
  int_data= 1;
 
2394
  strmov(str_data, "hh");
 
2395
  str_length= strlen(str_data);
 
2396
 
 
2397
  rc= mysql_stmt_execute(stmt);
 
2398
  check_execute(stmt, rc);
 
2399
 
 
2400
  rc= my_process_stmt_result(stmt);
 
2401
  DIE_UNLESS(rc == 3);
 
2402
 
 
2403
  mysql_stmt_close(stmt);
 
2404
}
 
2405
 
 
2406
 
 
2407
/* reads Qcache_hits from server and returns its value */
 
2408
static uint query_cache_hits(MYSQL *conn)
 
2409
{
 
2410
  MYSQL_RES *res;
 
2411
  MYSQL_ROW row;
 
2412
  int rc;
 
2413
  uint result;
 
2414
 
 
2415
  rc= mysql_query(conn, "show status like 'qcache_hits'");
 
2416
  myquery(rc);
 
2417
  res= mysql_use_result(conn);
 
2418
  DIE_UNLESS(res);
 
2419
 
 
2420
  row= mysql_fetch_row(res);
 
2421
  DIE_UNLESS(row);
 
2422
 
 
2423
  result= atoi(row[1]);
 
2424
  mysql_free_result(res);
 
2425
  return result;
 
2426
}
 
2427
 
 
2428
 
 
2429
/*
 
2430
  utility for the next test; expects 3 rows in the result from a SELECT,
 
2431
  compares each row/field with an expected value.
 
2432
 */
 
2433
#define test_ps_query_cache_result(i1,s1,l1,i2,s2,l2,i3,s3,l3)    \
 
2434
  r_metadata= mysql_stmt_result_metadata(stmt);                   \
 
2435
  DIE_UNLESS(r_metadata != NULL);                                 \
 
2436
  rc= mysql_stmt_fetch(stmt);                                     \
 
2437
  check_execute(stmt, rc);                                        \
 
2438
  if (!opt_silent)                                                \
 
2439
    fprintf(stdout, "\n row 1: %d, %s(%lu)", r_int_data,          \
 
2440
            r_str_data, r_str_length);                            \
 
2441
  DIE_UNLESS((r_int_data == i1) && (r_str_length == l1) &&        \
 
2442
             (strcmp(r_str_data, s1) == 0));                      \
 
2443
  rc= mysql_stmt_fetch(stmt);                                     \
 
2444
  check_execute(stmt, rc);                                        \
 
2445
  if (!opt_silent)                                                \
 
2446
    fprintf(stdout, "\n row 2: %d, %s(%lu)", r_int_data,          \
 
2447
            r_str_data, r_str_length);                            \
 
2448
  DIE_UNLESS((r_int_data == i2) && (r_str_length == l2) &&        \
 
2449
             (strcmp(r_str_data, s2) == 0));                      \
 
2450
  rc= mysql_stmt_fetch(stmt);                                     \
 
2451
  check_execute(stmt, rc);                                        \
 
2452
  if (!opt_silent)                                                \
 
2453
    fprintf(stdout, "\n row 3: %d, %s(%lu)", r_int_data,          \
 
2454
            r_str_data, r_str_length);                            \
 
2455
  DIE_UNLESS((r_int_data == i3) && (r_str_length == l3) &&        \
 
2456
             (strcmp(r_str_data, s3) == 0));                      \
 
2457
  rc= mysql_stmt_fetch(stmt);                                     \
 
2458
  DIE_UNLESS(rc == MYSQL_NO_DATA);                                \
 
2459
  mysql_free_result(r_metadata);
 
2460
 
 
2461
 
 
2462
/*
 
2463
  Test that prepared statements make use of the query cache just as normal
 
2464
  statements (BUG#735).
 
2465
*/
 
2466
static void test_ps_query_cache()
 
2467
{
 
2468
  MYSQL      *lmysql= mysql;
 
2469
  MYSQL_STMT *stmt;
 
2470
  int        rc;
 
2471
  MYSQL_BIND p_bind[2],r_bind[2]; /* p: param bind; r: result bind */
 
2472
  int32      p_int_data, r_int_data;
 
2473
  char       p_str_data[32], r_str_data[32];
 
2474
  unsigned long p_str_length, r_str_length;
 
2475
  MYSQL_RES  *r_metadata;
 
2476
  char       query[MAX_TEST_QUERY_LENGTH];
 
2477
  uint       hits1, hits2;
 
2478
  enum enum_test_ps_query_cache
 
2479
  {
 
2480
    /*
 
2481
      We iterate the same prepare/executes block, but have iterations where
 
2482
      we vary the query cache conditions.
 
2483
    */
 
2484
    /* the query cache is enabled for the duration of prep&execs: */
 
2485
    TEST_QCACHE_ON= 0,
 
2486
    /*
 
2487
      same but using a new connection (to see if qcache serves results from
 
2488
      the previous connection as it should):
 
2489
    */
 
2490
    TEST_QCACHE_ON_WITH_OTHER_CONN,
 
2491
    /*
 
2492
      First border case: disables the query cache before prepare and
 
2493
      re-enables it before execution (to test if we have no bug then):
 
2494
    */
 
2495
    TEST_QCACHE_OFF_ON,
 
2496
    /*
 
2497
      Second border case: enables the query cache before prepare and
 
2498
      disables it before execution:
 
2499
    */
 
2500
    TEST_QCACHE_ON_OFF
 
2501
  };
 
2502
  enum enum_test_ps_query_cache iteration;
 
2503
 
 
2504
  myheader("test_ps_query_cache");
 
2505
 
 
2506
  rc= mysql_query(mysql, "SET SQL_MODE=''");
 
2507
  myquery(rc);
 
2508
 
 
2509
  /* prepare the table */
 
2510
 
 
2511
  rc= mysql_query(mysql, "drop table if exists t1");
 
2512
  myquery(rc);
 
2513
 
 
2514
  rc= mysql_query(mysql, "create table t1 (id1 int(11) NOT NULL default '0', "
 
2515
                         "value2 varchar(100), value1 varchar(100))");
 
2516
  myquery(rc);
 
2517
 
 
2518
  rc= mysql_query(mysql, "insert into t1 values (1, 'hh', 'hh'), "
 
2519
                          "(2, 'hh', 'hh'), (1, 'ii', 'ii'), (2, 'ii', 'ii')");
 
2520
  myquery(rc);
 
2521
 
 
2522
  for (iteration= TEST_QCACHE_ON; iteration <= TEST_QCACHE_ON_OFF; iteration++)
 
2523
  {
 
2524
 
 
2525
    switch (iteration) {
 
2526
    case TEST_QCACHE_ON:
 
2527
    case TEST_QCACHE_ON_OFF:
 
2528
      rc= mysql_query(lmysql, "set global query_cache_size=1000000");
 
2529
      myquery(rc);
 
2530
      break;
 
2531
    case TEST_QCACHE_OFF_ON:
 
2532
      rc= mysql_query(lmysql, "set global query_cache_size=0");
 
2533
      myquery(rc);
 
2534
      break;
 
2535
    case TEST_QCACHE_ON_WITH_OTHER_CONN:
 
2536
      if (!opt_silent)
 
2537
        fprintf(stdout, "\n Establishing a test connection ...");
 
2538
      if (!(lmysql= mysql_client_init(NULL)))
 
2539
      {
 
2540
        printf("mysql_client_init() failed");
 
2541
        DIE_UNLESS(0);
 
2542
      }
 
2543
      if (!(mysql_real_connect(lmysql, opt_host, opt_user,
 
2544
                               opt_password, current_db, opt_port,
 
2545
                               opt_unix_socket, 0)))
 
2546
      {
 
2547
        printf("connection failed");
 
2548
        mysql_close(lmysql);
 
2549
        DIE_UNLESS(0);
 
2550
      }
 
2551
      rc= mysql_query(lmysql, "SET SQL_MODE=''");
 
2552
      myquery(rc);
 
2553
 
 
2554
      if (!opt_silent)
 
2555
        fprintf(stdout, "OK");
 
2556
    }
 
2557
 
 
2558
    strmov(query, "select id1, value1 from t1 where id1= ? or "
 
2559
           "CONVERT(value1 USING utf8)= ?");
 
2560
    stmt= mysql_simple_prepare(lmysql, query);
 
2561
    check_stmt(stmt);
 
2562
 
 
2563
    verify_param_count(stmt, 2);
 
2564
 
 
2565
    switch (iteration) {
 
2566
    case TEST_QCACHE_OFF_ON:
 
2567
      rc= mysql_query(lmysql, "set global query_cache_size=1000000");
 
2568
      myquery(rc);
 
2569
      break;
 
2570
    case TEST_QCACHE_ON_OFF:
 
2571
      rc= mysql_query(lmysql, "set global query_cache_size=0");
 
2572
      myquery(rc);
 
2573
    default:
 
2574
      break;
 
2575
    }
 
2576
 
 
2577
    bzero((char*) p_bind, sizeof(p_bind));
 
2578
    p_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
2579
    p_bind[0].buffer= (void *)&p_int_data;
 
2580
    p_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
 
2581
    p_bind[1].buffer= (void *)p_str_data;
 
2582
    p_bind[1].buffer_length= array_elements(p_str_data);
 
2583
    p_bind[1].length= &p_str_length;
 
2584
 
 
2585
    rc= mysql_stmt_bind_param(stmt, p_bind);
 
2586
    check_execute(stmt, rc);
 
2587
 
 
2588
    p_int_data= 1;
 
2589
    strmov(p_str_data, "hh");
 
2590
    p_str_length= strlen(p_str_data);
 
2591
 
 
2592
    bzero((char*) r_bind, sizeof(r_bind));
 
2593
    r_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
2594
    r_bind[0].buffer= (void *)&r_int_data;
 
2595
    r_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
 
2596
    r_bind[1].buffer= (void *)r_str_data;
 
2597
    r_bind[1].buffer_length= array_elements(r_str_data);
 
2598
    r_bind[1].length= &r_str_length;
 
2599
 
 
2600
    rc= mysql_stmt_bind_result(stmt, r_bind);
 
2601
    check_execute(stmt, rc);
 
2602
 
 
2603
    rc= mysql_stmt_execute(stmt);
 
2604
    check_execute(stmt, rc);
 
2605
 
 
2606
    test_ps_query_cache_result(1, "hh", 2, 2, "hh", 2, 1, "ii", 2);
 
2607
 
 
2608
    /* now retry with the same parameter values and see qcache hits */
 
2609
    hits1= query_cache_hits(lmysql);
 
2610
    rc= mysql_stmt_execute(stmt);
 
2611
    check_execute(stmt, rc);
 
2612
    test_ps_query_cache_result(1, "hh", 2, 2, "hh", 2, 1, "ii", 2);
 
2613
    hits2= query_cache_hits(lmysql);
 
2614
    switch(iteration) {
 
2615
    case TEST_QCACHE_ON_WITH_OTHER_CONN:
 
2616
    case TEST_QCACHE_ON:                 /* should have hit */
 
2617
      DIE_UNLESS(hits2-hits1 == 1);
 
2618
      break;
 
2619
    case TEST_QCACHE_OFF_ON:
 
2620
    case TEST_QCACHE_ON_OFF:             /* should not have hit */
 
2621
      DIE_UNLESS(hits2-hits1 == 0);
 
2622
      break;
 
2623
    }
 
2624
 
 
2625
    /* now modify parameter values and see qcache hits */
 
2626
    strmov(p_str_data, "ii");
 
2627
    p_str_length= strlen(p_str_data);
 
2628
    rc= mysql_stmt_execute(stmt);
 
2629
    check_execute(stmt, rc);
 
2630
    test_ps_query_cache_result(1, "hh", 2, 1, "ii", 2, 2, "ii", 2);
 
2631
    hits1= query_cache_hits(lmysql);
 
2632
 
 
2633
    switch(iteration) {
 
2634
    case TEST_QCACHE_ON:
 
2635
    case TEST_QCACHE_OFF_ON:
 
2636
    case TEST_QCACHE_ON_OFF:             /* should not have hit */
 
2637
      DIE_UNLESS(hits2-hits1 == 0);
 
2638
      break;
 
2639
    case TEST_QCACHE_ON_WITH_OTHER_CONN: /* should have hit */
 
2640
      DIE_UNLESS(hits1-hits2 == 1);
 
2641
      break;
 
2642
    }
 
2643
 
 
2644
    rc= mysql_stmt_execute(stmt);
 
2645
    check_execute(stmt, rc);
 
2646
 
 
2647
    test_ps_query_cache_result(1, "hh", 2, 1, "ii", 2, 2, "ii", 2);
 
2648
    hits2= query_cache_hits(lmysql);
 
2649
 
 
2650
    mysql_stmt_close(stmt);
 
2651
 
 
2652
    switch(iteration) {
 
2653
    case TEST_QCACHE_ON:                 /* should have hit */
 
2654
      DIE_UNLESS(hits2-hits1 == 1);
 
2655
      break;
 
2656
    case TEST_QCACHE_OFF_ON:
 
2657
    case TEST_QCACHE_ON_OFF:             /* should not have hit */
 
2658
      DIE_UNLESS(hits2-hits1 == 0);
 
2659
      break;
 
2660
    case TEST_QCACHE_ON_WITH_OTHER_CONN: /* should have hit */
 
2661
      DIE_UNLESS(hits2-hits1 == 1);
 
2662
      break;
 
2663
    }
 
2664
 
 
2665
  } /* for(iteration=...) */
 
2666
 
 
2667
  if (lmysql != mysql)
 
2668
    mysql_close(lmysql);
 
2669
 
 
2670
  rc= mysql_query(mysql, "set global query_cache_size=0");
 
2671
  myquery(rc);
 
2672
}
 
2673
 
 
2674
 
 
2675
/* Test BUG#1115 (incorrect string parameter value allocation) */
 
2676
 
 
2677
static void test_bug1115()
 
2678
{
 
2679
  MYSQL_STMT *stmt;
 
2680
  int rc;
 
2681
  MYSQL_BIND my_bind[1];
 
2682
  ulong length[1];
 
2683
  char szData[11];
 
2684
  char query[MAX_TEST_QUERY_LENGTH];
 
2685
 
 
2686
  myheader("test_bug1115");
 
2687
 
 
2688
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
 
2689
  myquery(rc);
 
2690
 
 
2691
  rc= mysql_query(mysql, "CREATE TABLE test_select(\
 
2692
session_id  char(9) NOT NULL, \
 
2693
    a       int(8) unsigned NOT NULL, \
 
2694
    b        int(5) NOT NULL, \
 
2695
    c      int(5) NOT NULL, \
 
2696
    d  datetime NOT NULL)");
 
2697
  myquery(rc);
 
2698
  rc= mysql_query(mysql, "INSERT INTO test_select VALUES "
 
2699
                         "(\"abc\", 1, 2, 3, 2003-08-30), "
 
2700
                         "(\"abd\", 1, 2, 3, 2003-08-30), "
 
2701
                         "(\"abf\", 1, 2, 3, 2003-08-30), "
 
2702
                         "(\"abg\", 1, 2, 3, 2003-08-30), "
 
2703
                         "(\"abh\", 1, 2, 3, 2003-08-30), "
 
2704
                         "(\"abj\", 1, 2, 3, 2003-08-30), "
 
2705
                         "(\"abk\", 1, 2, 3, 2003-08-30), "
 
2706
                         "(\"abl\", 1, 2, 3, 2003-08-30), "
 
2707
                         "(\"abq\", 1, 2, 3, 2003-08-30) ");
 
2708
  myquery(rc);
 
2709
  rc= mysql_query(mysql, "INSERT INTO test_select VALUES "
 
2710
                         "(\"abw\", 1, 2, 3, 2003-08-30), "
 
2711
                         "(\"abe\", 1, 2, 3, 2003-08-30), "
 
2712
                         "(\"abr\", 1, 2, 3, 2003-08-30), "
 
2713
                         "(\"abt\", 1, 2, 3, 2003-08-30), "
 
2714
                         "(\"aby\", 1, 2, 3, 2003-08-30), "
 
2715
                         "(\"abu\", 1, 2, 3, 2003-08-30), "
 
2716
                         "(\"abi\", 1, 2, 3, 2003-08-30), "
 
2717
                         "(\"abo\", 1, 2, 3, 2003-08-30), "
 
2718
                         "(\"abp\", 1, 2, 3, 2003-08-30), "
 
2719
                         "(\"abz\", 1, 2, 3, 2003-08-30), "
 
2720
                         "(\"abx\", 1, 2, 3, 2003-08-30)");
 
2721
  myquery(rc);
 
2722
 
 
2723
  strmov(query, "SELECT * FROM test_select WHERE "
 
2724
                "CONVERT(session_id USING utf8)= ?");
 
2725
  stmt= mysql_simple_prepare(mysql, query);
 
2726
  check_stmt(stmt);
 
2727
 
 
2728
  verify_param_count(stmt, 1);
 
2729
 
 
2730
  /* Always bzero all members of bind parameter */
 
2731
  bzero((char*) my_bind, sizeof(my_bind));
 
2732
 
 
2733
  strmov(szData, (char *)"abc");
 
2734
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
2735
  my_bind[0].buffer= (void *)szData;
 
2736
  my_bind[0].buffer_length= 10;
 
2737
  my_bind[0].length= &length[0];
 
2738
  length[0]= 3;
 
2739
 
 
2740
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2741
  check_execute(stmt, rc);
 
2742
 
 
2743
  rc= mysql_stmt_execute(stmt);
 
2744
  check_execute(stmt, rc);
 
2745
 
 
2746
  rc= my_process_stmt_result(stmt);
 
2747
  DIE_UNLESS(rc == 1);
 
2748
 
 
2749
  strmov(szData, (char *)"venu");
 
2750
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
2751
  my_bind[0].buffer= (void *)szData;
 
2752
  my_bind[0].buffer_length= 10;
 
2753
  my_bind[0].length= &length[0];
 
2754
  length[0]= 4;
 
2755
  my_bind[0].is_null= 0;
 
2756
 
 
2757
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2758
  check_execute(stmt, rc);
 
2759
 
 
2760
  rc= mysql_stmt_execute(stmt);
 
2761
  check_execute(stmt, rc);
 
2762
 
 
2763
  rc= my_process_stmt_result(stmt);
 
2764
  DIE_UNLESS(rc == 0);
 
2765
 
 
2766
  strmov(szData, (char *)"abc");
 
2767
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
2768
  my_bind[0].buffer= (void *)szData;
 
2769
  my_bind[0].buffer_length= 10;
 
2770
  my_bind[0].length= &length[0];
 
2771
  length[0]= 3;
 
2772
  my_bind[0].is_null= 0;
 
2773
 
 
2774
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2775
  check_execute(stmt, rc);
 
2776
 
 
2777
  rc= mysql_stmt_execute(stmt);
 
2778
  check_execute(stmt, rc);
 
2779
 
 
2780
  rc= my_process_stmt_result(stmt);
 
2781
  DIE_UNLESS(rc == 1);
 
2782
 
 
2783
  mysql_stmt_close(stmt);
 
2784
}
 
2785
 
 
2786
 
 
2787
/* Test BUG#1180 (optimized away part of WHERE clause) */
 
2788
 
 
2789
static void test_bug1180()
 
2790
{
 
2791
  MYSQL_STMT *stmt;
 
2792
  int rc;
 
2793
  MYSQL_BIND my_bind[1];
 
2794
  ulong length[1];
 
2795
  char szData[11];
 
2796
  char query[MAX_TEST_QUERY_LENGTH];
 
2797
 
 
2798
  myheader("test_select_bug");
 
2799
 
 
2800
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
 
2801
  myquery(rc);
 
2802
 
 
2803
  rc= mysql_query(mysql, "CREATE TABLE test_select(session_id  char(9) NOT NULL)");
 
2804
  myquery(rc);
 
2805
  rc= mysql_query(mysql, "INSERT INTO test_select VALUES (\"abc\")");
 
2806
  myquery(rc);
 
2807
 
 
2808
  strmov(query, "SELECT * FROM test_select WHERE ?= \"1111\" and "
 
2809
                "session_id= \"abc\"");
 
2810
  stmt= mysql_simple_prepare(mysql, query);
 
2811
  check_stmt(stmt);
 
2812
 
 
2813
  verify_param_count(stmt, 1);
 
2814
 
 
2815
  /* Always bzero all members of bind parameter */
 
2816
  bzero((char*) my_bind, sizeof(my_bind));
 
2817
 
 
2818
  strmov(szData, (char *)"abc");
 
2819
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
2820
  my_bind[0].buffer= (void *)szData;
 
2821
  my_bind[0].buffer_length= 10;
 
2822
  my_bind[0].length= &length[0];
 
2823
  length[0]= 3;
 
2824
  my_bind[0].is_null= 0;
 
2825
 
 
2826
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2827
  check_execute(stmt, rc);
 
2828
 
 
2829
  rc= mysql_stmt_execute(stmt);
 
2830
  check_execute(stmt, rc);
 
2831
 
 
2832
  rc= my_process_stmt_result(stmt);
 
2833
  DIE_UNLESS(rc == 0);
 
2834
 
 
2835
  strmov(szData, (char *)"1111");
 
2836
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
2837
  my_bind[0].buffer= (void *)szData;
 
2838
  my_bind[0].buffer_length= 10;
 
2839
  my_bind[0].length= &length[0];
 
2840
  length[0]= 4;
 
2841
  my_bind[0].is_null= 0;
 
2842
 
 
2843
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2844
  check_execute(stmt, rc);
 
2845
 
 
2846
  rc= mysql_stmt_execute(stmt);
 
2847
  check_execute(stmt, rc);
 
2848
 
 
2849
  rc= my_process_stmt_result(stmt);
 
2850
  DIE_UNLESS(rc == 1);
 
2851
 
 
2852
  strmov(szData, (char *)"abc");
 
2853
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
2854
  my_bind[0].buffer= (void *)szData;
 
2855
  my_bind[0].buffer_length= 10;
 
2856
  my_bind[0].length= &length[0];
 
2857
  length[0]= 3;
 
2858
  my_bind[0].is_null= 0;
 
2859
 
 
2860
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2861
  check_execute(stmt, rc);
 
2862
 
 
2863
  rc= mysql_stmt_execute(stmt);
 
2864
  check_execute(stmt, rc);
 
2865
 
 
2866
  rc= my_process_stmt_result(stmt);
 
2867
  DIE_UNLESS(rc == 0);
 
2868
 
 
2869
  mysql_stmt_close(stmt);
 
2870
}
 
2871
 
 
2872
 
 
2873
/*
 
2874
  Test BUG#1644 (Insertion of more than 3 NULL columns with parameter
 
2875
  binding fails)
 
2876
*/
 
2877
 
 
2878
static void test_bug1644()
 
2879
{
 
2880
  MYSQL_STMT *stmt;
 
2881
  MYSQL_RES *result;
 
2882
  MYSQL_ROW row;
 
2883
  MYSQL_BIND my_bind[4];
 
2884
  int num;
 
2885
  my_bool isnull;
 
2886
  int rc, i;
 
2887
  char query[MAX_TEST_QUERY_LENGTH];
 
2888
 
 
2889
  myheader("test_bug1644");
 
2890
 
 
2891
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS foo_dfr");
 
2892
  myquery(rc);
 
2893
 
 
2894
  rc= mysql_query(mysql,
 
2895
           "CREATE TABLE foo_dfr(col1 int, col2 int, col3 int, col4 int);");
 
2896
  myquery(rc);
 
2897
 
 
2898
  strmov(query, "INSERT INTO foo_dfr VALUES (?, ?, ?, ? )");
 
2899
  stmt= mysql_simple_prepare(mysql, query);
 
2900
  check_stmt(stmt);
 
2901
 
 
2902
  verify_param_count(stmt, 4);
 
2903
 
 
2904
  /* Always bzero all members of bind parameter */
 
2905
  bzero((char*) my_bind, sizeof(my_bind));
 
2906
 
 
2907
  num= 22;
 
2908
  isnull= 0;
 
2909
  for (i= 0 ; i < 4 ; i++)
 
2910
  {
 
2911
    my_bind[i].buffer_type= MYSQL_TYPE_LONG;
 
2912
    my_bind[i].buffer= (void *)&num;
 
2913
    my_bind[i].is_null= &isnull;
 
2914
  }
 
2915
 
 
2916
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2917
  check_execute(stmt, rc);
 
2918
 
 
2919
  rc= mysql_stmt_execute(stmt);
 
2920
  check_execute(stmt, rc);
 
2921
 
 
2922
  isnull= 1;
 
2923
  for (i= 0 ; i < 4 ; i++)
 
2924
    my_bind[i].is_null= &isnull;
 
2925
 
 
2926
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2927
  check_execute(stmt, rc);
 
2928
 
 
2929
  rc= mysql_stmt_execute(stmt);
 
2930
  check_execute(stmt, rc);
 
2931
 
 
2932
  isnull= 0;
 
2933
  num= 88;
 
2934
  for (i= 0 ; i < 4 ; i++)
 
2935
    my_bind[i].is_null= &isnull;
 
2936
 
 
2937
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
2938
  check_execute(stmt, rc);
 
2939
 
 
2940
  rc= mysql_stmt_execute(stmt);
 
2941
  check_execute(stmt, rc);
 
2942
 
 
2943
  mysql_stmt_close(stmt);
 
2944
 
 
2945
  rc= mysql_query(mysql, "SELECT * FROM foo_dfr");
 
2946
  myquery(rc);
 
2947
 
 
2948
  result= mysql_store_result(mysql);
 
2949
  mytest(result);
 
2950
 
 
2951
  rc= my_process_result_set(result);
 
2952
  DIE_UNLESS(rc == 3);
 
2953
 
 
2954
  mysql_data_seek(result, 0);
 
2955
 
 
2956
  row= mysql_fetch_row(result);
 
2957
  mytest(row);
 
2958
  for (i= 0 ; i < 4 ; i++)
 
2959
  {
 
2960
    DIE_UNLESS(strcmp(row[i], "22") == 0);
 
2961
  }
 
2962
  row= mysql_fetch_row(result);
 
2963
  mytest(row);
 
2964
  for (i= 0 ; i < 4 ; i++)
 
2965
  {
 
2966
    DIE_UNLESS(row[i] == 0);
 
2967
  }
 
2968
  row= mysql_fetch_row(result);
 
2969
  mytest(row);
 
2970
  for (i= 0 ; i < 4 ; i++)
 
2971
  {
 
2972
    DIE_UNLESS(strcmp(row[i], "88") == 0);
 
2973
  }
 
2974
  row= mysql_fetch_row(result);
 
2975
  mytest_r(row);
 
2976
 
 
2977
  mysql_free_result(result);
 
2978
}
 
2979
 
 
2980
 
 
2981
/* Test simple select show */
 
2982
 
 
2983
static void test_select_show()
 
2984
{
 
2985
  MYSQL_STMT *stmt;
 
2986
  int        rc;
 
2987
  char query[MAX_TEST_QUERY_LENGTH];
 
2988
 
 
2989
  myheader("test_select_show");
 
2990
 
 
2991
  mysql_autocommit(mysql, TRUE);
 
2992
 
 
2993
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_show");
 
2994
  myquery(rc);
 
2995
 
 
2996
  rc= mysql_query(mysql, "CREATE TABLE test_show(id int(4) NOT NULL primary "
 
2997
                         " key, name char(2))");
 
2998
  myquery(rc);
 
2999
 
 
3000
  stmt= mysql_simple_prepare(mysql, "show columns from test_show");
 
3001
  check_stmt(stmt);
 
3002
 
 
3003
  verify_param_count(stmt, 0);
 
3004
 
 
3005
  rc= mysql_stmt_execute(stmt);
 
3006
  check_execute(stmt, rc);
 
3007
 
 
3008
  my_process_stmt_result(stmt);
 
3009
  mysql_stmt_close(stmt);
 
3010
 
 
3011
  stmt= mysql_simple_prepare(mysql, "show tables from mysql like ?");
 
3012
  check_stmt_r(stmt);
 
3013
 
 
3014
  strxmov(query, "show tables from ", current_db, " like \'test_show\'", NullS);
 
3015
  stmt= mysql_simple_prepare(mysql, query);
 
3016
  check_stmt(stmt);
 
3017
 
 
3018
  rc= mysql_stmt_execute(stmt);
 
3019
  check_execute(stmt, rc);
 
3020
 
 
3021
  my_process_stmt_result(stmt);
 
3022
  mysql_stmt_close(stmt);
 
3023
 
 
3024
  stmt= mysql_simple_prepare(mysql, "describe test_show");
 
3025
  check_stmt(stmt);
 
3026
 
 
3027
  rc= mysql_stmt_execute(stmt);
 
3028
  check_execute(stmt, rc);
 
3029
 
 
3030
  my_process_stmt_result(stmt);
 
3031
  mysql_stmt_close(stmt);
 
3032
 
 
3033
  stmt= mysql_simple_prepare(mysql, "show keys from test_show");
 
3034
  check_stmt(stmt);
 
3035
 
 
3036
  rc= mysql_stmt_execute(stmt);
 
3037
  check_execute(stmt, rc);
 
3038
 
 
3039
  rc= my_process_stmt_result(stmt);
 
3040
  DIE_UNLESS(rc == 1);
 
3041
  mysql_stmt_close(stmt);
 
3042
}
 
3043
 
 
3044
 
 
3045
/* Test simple update */
 
3046
 
 
3047
static void test_simple_update()
 
3048
{
 
3049
  MYSQL_STMT *stmt;
 
3050
  int        rc;
 
3051
  char       szData[25];
 
3052
  int        nData= 1;
 
3053
  MYSQL_RES  *result;
 
3054
  MYSQL_BIND my_bind[2];
 
3055
  ulong      length[2];
 
3056
  char query[MAX_TEST_QUERY_LENGTH];
 
3057
 
 
3058
  myheader("test_simple_update");
 
3059
 
 
3060
  rc= mysql_autocommit(mysql, TRUE);
 
3061
  myquery(rc);
 
3062
 
 
3063
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_update");
 
3064
  myquery(rc);
 
3065
 
 
3066
  rc= mysql_query(mysql, "CREATE TABLE test_update(col1 int, "
 
3067
                         " col2 varchar(50), col3 int )");
 
3068
  myquery(rc);
 
3069
 
 
3070
  rc= mysql_query(mysql, "INSERT INTO test_update VALUES(1, 'MySQL', 100)");
 
3071
  myquery(rc);
 
3072
 
 
3073
  verify_affected_rows(1);
 
3074
 
 
3075
  rc= mysql_commit(mysql);
 
3076
  myquery(rc);
 
3077
 
 
3078
  /* insert by prepare */
 
3079
  strmov(query, "UPDATE test_update SET col2= ? WHERE col1= ?");
 
3080
  stmt= mysql_simple_prepare(mysql, query);
 
3081
  check_stmt(stmt);
 
3082
 
 
3083
  verify_param_count(stmt, 2);
 
3084
 
 
3085
  /* Always bzero all members of bind parameter */
 
3086
  bzero((char*) my_bind, sizeof(my_bind));
 
3087
 
 
3088
  nData= 1;
 
3089
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
3090
  my_bind[0].buffer= szData;                /* string data */
 
3091
  my_bind[0].buffer_length= sizeof(szData);
 
3092
  my_bind[0].length= &length[0];
 
3093
  length[0]= my_sprintf(szData, (szData, "updated-data"));
 
3094
 
 
3095
  my_bind[1].buffer= (void *) &nData;
 
3096
  my_bind[1].buffer_type= MYSQL_TYPE_LONG;
 
3097
 
 
3098
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
3099
  check_execute(stmt, rc);
 
3100
 
 
3101
  rc= mysql_stmt_execute(stmt);
 
3102
  check_execute(stmt, rc);
 
3103
  verify_affected_rows(1);
 
3104
 
 
3105
  mysql_stmt_close(stmt);
 
3106
 
 
3107
  /* now fetch the results ..*/
 
3108
  rc= mysql_commit(mysql);
 
3109
  myquery(rc);
 
3110
 
 
3111
  /* test the results now, only one row should exist */
 
3112
  rc= mysql_query(mysql, "SELECT * FROM test_update");
 
3113
  myquery(rc);
 
3114
 
 
3115
  /* get the result */
 
3116
  result= mysql_store_result(mysql);
 
3117
  mytest(result);
 
3118
 
 
3119
  rc= my_process_result_set(result);
 
3120
  DIE_UNLESS(rc == 1);
 
3121
  mysql_free_result(result);
 
3122
}
 
3123
 
 
3124
 
 
3125
/* Test simple long data handling */
 
3126
 
 
3127
static void test_long_data()
 
3128
{
 
3129
  MYSQL_STMT *stmt;
 
3130
  int        rc, int_data;
 
3131
  char       *data= NullS;
 
3132
  MYSQL_RES  *result;
 
3133
  MYSQL_BIND my_bind[3];
 
3134
  char query[MAX_TEST_QUERY_LENGTH];
 
3135
 
 
3136
  myheader("test_long_data");
 
3137
 
 
3138
  rc= mysql_autocommit(mysql, TRUE);
 
3139
  myquery(rc);
 
3140
 
 
3141
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data");
 
3142
  myquery(rc);
 
3143
 
 
3144
  rc= mysql_query(mysql, "CREATE TABLE test_long_data(col1 int, "
 
3145
                         "      col2 long varchar, col3 long varbinary)");
 
3146
  myquery(rc);
 
3147
 
 
3148
  strmov(query, "INSERT INTO test_long_data(col1, col2) VALUES(?)");
 
3149
  stmt= mysql_simple_prepare(mysql, query);
 
3150
  check_stmt_r(stmt);
 
3151
 
 
3152
  strmov(query, "INSERT INTO test_long_data(col1, col2, col3) VALUES(?, ?, ?)");
 
3153
  stmt= mysql_simple_prepare(mysql, query);
 
3154
  check_stmt(stmt);
 
3155
 
 
3156
  verify_param_count(stmt, 3);
 
3157
 
 
3158
  /* Always bzero all members of bind parameter */
 
3159
  bzero((char*) my_bind, sizeof(my_bind));
 
3160
 
 
3161
  my_bind[0].buffer= (void *)&int_data;
 
3162
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
3163
 
 
3164
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
3165
 
 
3166
  my_bind[2]= my_bind[1];
 
3167
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
3168
  check_execute(stmt, rc);
 
3169
 
 
3170
  int_data= 999;
 
3171
  data= (char *)"Michael";
 
3172
 
 
3173
  /* supply data in pieces */
 
3174
  rc= mysql_stmt_send_long_data(stmt, 1, data, strlen(data));
 
3175
  data= (char *)" 'Monty' Widenius";
 
3176
  rc= mysql_stmt_send_long_data(stmt, 1, data, strlen(data));
 
3177
  check_execute(stmt, rc);
 
3178
  rc= mysql_stmt_send_long_data(stmt, 2, "Venu (venu@mysql.com)", 4);
 
3179
  check_execute(stmt, rc);
 
3180
 
 
3181
  /* execute */
 
3182
  rc= mysql_stmt_execute(stmt);
 
3183
  if (!opt_silent)
 
3184
    fprintf(stdout, " mysql_stmt_execute() returned %d\n", rc);
 
3185
  check_execute(stmt, rc);
 
3186
 
 
3187
  rc= mysql_commit(mysql);
 
3188
  myquery(rc);
 
3189
 
 
3190
  /* now fetch the results ..*/
 
3191
  rc= mysql_query(mysql, "SELECT * FROM test_long_data");
 
3192
  myquery(rc);
 
3193
 
 
3194
  /* get the result */
 
3195
  result= mysql_store_result(mysql);
 
3196
  mytest(result);
 
3197
 
 
3198
  rc= my_process_result_set(result);
 
3199
  DIE_UNLESS(rc == 1);
 
3200
  mysql_free_result(result);
 
3201
 
 
3202
  verify_col_data("test_long_data", "col1", "999");
 
3203
  verify_col_data("test_long_data", "col2", "Michael 'Monty' Widenius");
 
3204
  verify_col_data("test_long_data", "col3", "Venu");
 
3205
  mysql_stmt_close(stmt);
 
3206
}
 
3207
 
 
3208
 
 
3209
/* Test long data (string) handling */
 
3210
 
 
3211
static void test_long_data_str()
 
3212
{
 
3213
  MYSQL_STMT *stmt;
 
3214
  int        rc, i;
 
3215
  char       data[255];
 
3216
  long       length;
 
3217
  ulong      length1;
 
3218
  MYSQL_RES  *result;
 
3219
  MYSQL_BIND my_bind[2];
 
3220
  my_bool    is_null[2];
 
3221
  char query[MAX_TEST_QUERY_LENGTH];
 
3222
 
 
3223
  myheader("test_long_data_str");
 
3224
 
 
3225
  rc= mysql_autocommit(mysql, TRUE);
 
3226
  myquery(rc);
 
3227
 
 
3228
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data_str");
 
3229
  myquery(rc);
 
3230
 
 
3231
  rc= mysql_query(mysql, "CREATE TABLE test_long_data_str(id int, longstr long varchar)");
 
3232
  myquery(rc);
 
3233
 
 
3234
  strmov(query, "INSERT INTO test_long_data_str VALUES(?, ?)");
 
3235
  stmt= mysql_simple_prepare(mysql, query);
 
3236
  check_stmt(stmt);
 
3237
 
 
3238
  verify_param_count(stmt, 2);
 
3239
 
 
3240
  /* Always bzero all members of bind parameter */
 
3241
  bzero((char*) my_bind, sizeof(my_bind));
 
3242
 
 
3243
  my_bind[0].buffer= (void *)&length;
 
3244
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
3245
  my_bind[0].is_null= &is_null[0];
 
3246
  is_null[0]= 0;
 
3247
  length= 0;
 
3248
 
 
3249
  my_bind[1].buffer= data;                          /* string data */
 
3250
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
3251
  my_bind[1].length= &length1;
 
3252
  my_bind[1].is_null= &is_null[1];
 
3253
  is_null[1]= 0;
 
3254
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
3255
  check_execute(stmt, rc);
 
3256
 
 
3257
  length= 40;
 
3258
  strmov(data, "MySQL AB");
 
3259
 
 
3260
  /* supply data in pieces */
 
3261
  for(i= 0; i < 4; i++)
 
3262
  {
 
3263
    rc= mysql_stmt_send_long_data(stmt, 1, (char *)data, 5);
 
3264
    check_execute(stmt, rc);
 
3265
  }
 
3266
  /* execute */
 
3267
  rc= mysql_stmt_execute(stmt);
 
3268
  if (!opt_silent)
 
3269
    fprintf(stdout, " mysql_stmt_execute() returned %d\n", rc);
 
3270
  check_execute(stmt, rc);
 
3271
 
 
3272
  mysql_stmt_close(stmt);
 
3273
 
 
3274
  rc= mysql_commit(mysql);
 
3275
  myquery(rc);
 
3276
 
 
3277
  /* now fetch the results ..*/
 
3278
  rc= mysql_query(mysql, "SELECT LENGTH(longstr), longstr FROM test_long_data_str");
 
3279
  myquery(rc);
 
3280
 
 
3281
  /* get the result */
 
3282
  result= mysql_store_result(mysql);
 
3283
  mytest(result);
 
3284
 
 
3285
  rc= my_process_result_set(result);
 
3286
  DIE_UNLESS(rc == 1);
 
3287
  mysql_free_result(result);
 
3288
 
 
3289
  my_sprintf(data, (data, "%d", i*5));
 
3290
  verify_col_data("test_long_data_str", "LENGTH(longstr)", data);
 
3291
  data[0]= '\0';
 
3292
  while (i--)
 
3293
   strxmov(data, data, "MySQL", NullS);
 
3294
  verify_col_data("test_long_data_str", "longstr", data);
 
3295
 
 
3296
  rc= mysql_query(mysql, "DROP TABLE test_long_data_str");
 
3297
  myquery(rc);
 
3298
}
 
3299
 
 
3300
 
 
3301
/* Test long data (string) handling */
 
3302
 
 
3303
static void test_long_data_str1()
 
3304
{
 
3305
  MYSQL_STMT *stmt;
 
3306
  int        rc, i;
 
3307
  char       data[255];
 
3308
  long       length;
 
3309
  ulong      max_blob_length, blob_length, length1;
 
3310
  my_bool    true_value;
 
3311
  MYSQL_RES  *result;
 
3312
  MYSQL_BIND my_bind[2];
 
3313
  MYSQL_FIELD *field;
 
3314
  char query[MAX_TEST_QUERY_LENGTH];
 
3315
 
 
3316
  myheader("test_long_data_str1");
 
3317
 
 
3318
  rc= mysql_autocommit(mysql, TRUE);
 
3319
  myquery(rc);
 
3320
 
 
3321
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data_str");
 
3322
  myquery(rc);
 
3323
 
 
3324
  rc= mysql_query(mysql, "CREATE TABLE test_long_data_str(longstr long varchar, blb long varbinary)");
 
3325
  myquery(rc);
 
3326
 
 
3327
  strmov(query, "INSERT INTO test_long_data_str VALUES(?, ?)");
 
3328
  stmt= mysql_simple_prepare(mysql, query);
 
3329
  check_stmt(stmt);
 
3330
 
 
3331
  verify_param_count(stmt, 2);
 
3332
 
 
3333
  /* Always bzero all members of bind parameter */
 
3334
  bzero((char*) my_bind, sizeof(my_bind));
 
3335
 
 
3336
  my_bind[0].buffer= data;            /* string data */
 
3337
  my_bind[0].buffer_length= sizeof(data);
 
3338
  my_bind[0].length= &length1;
 
3339
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
3340
  length1= 0;
 
3341
 
 
3342
  my_bind[1]= my_bind[0];
 
3343
  my_bind[1].buffer_type= MYSQL_TYPE_BLOB;
 
3344
 
 
3345
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
3346
  check_execute(stmt, rc);
 
3347
  length= my_sprintf(data, (data, "MySQL AB"));
 
3348
 
 
3349
  /* supply data in pieces */
 
3350
  for (i= 0; i < 3; i++)
 
3351
  {
 
3352
    rc= mysql_stmt_send_long_data(stmt, 0, data, length);
 
3353
    check_execute(stmt, rc);
 
3354
 
 
3355
    rc= mysql_stmt_send_long_data(stmt, 1, data, 2);
 
3356
    check_execute(stmt, rc);
 
3357
  }
 
3358
 
 
3359
  /* execute */
 
3360
  rc= mysql_stmt_execute(stmt);
 
3361
  if (!opt_silent)
 
3362
    fprintf(stdout, " mysql_stmt_execute() returned %d\n", rc);
 
3363
  check_execute(stmt, rc);
 
3364
 
 
3365
  mysql_stmt_close(stmt);
 
3366
 
 
3367
  rc= mysql_commit(mysql);
 
3368
  myquery(rc);
 
3369
 
 
3370
  /* now fetch the results ..*/
 
3371
  rc= mysql_query(mysql, "SELECT LENGTH(longstr), longstr, LENGTH(blb), blb FROM test_long_data_str");
 
3372
  myquery(rc);
 
3373
 
 
3374
  /* get the result */
 
3375
  result= mysql_store_result(mysql);
 
3376
 
 
3377
  mysql_field_seek(result, 1);
 
3378
  field= mysql_fetch_field(result);
 
3379
  max_blob_length= field->max_length;
 
3380
 
 
3381
  mytest(result);
 
3382
 
 
3383
  rc= my_process_result_set(result);
 
3384
  DIE_UNLESS(rc == 1);
 
3385
  mysql_free_result(result);
 
3386
 
 
3387
  my_sprintf(data, (data, "%ld", (long)i*length));
 
3388
  verify_col_data("test_long_data_str", "length(longstr)", data);
 
3389
 
 
3390
  my_sprintf(data, (data, "%d", i*2));
 
3391
  verify_col_data("test_long_data_str", "length(blb)", data);
 
3392
 
 
3393
  /* Test length of field->max_length */
 
3394
  stmt= mysql_simple_prepare(mysql, "SELECT * from test_long_data_str");
 
3395
  check_stmt(stmt);
 
3396
  verify_param_count(stmt, 0);
 
3397
 
 
3398
  rc= mysql_stmt_execute(stmt);
 
3399
  check_execute(stmt, rc);
 
3400
 
 
3401
  rc= mysql_stmt_store_result(stmt);
 
3402
  check_execute(stmt, rc);
 
3403
 
 
3404
  result= mysql_stmt_result_metadata(stmt);
 
3405
  field= mysql_fetch_fields(result);
 
3406
 
 
3407
  /* First test what happens if STMT_ATTR_UPDATE_MAX_LENGTH is not used */
 
3408
  DIE_UNLESS(field->max_length == 0);
 
3409
  mysql_free_result(result);
 
3410
 
 
3411
  /* Enable updating of field->max_length */
 
3412
  true_value= 1;
 
3413
  mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*) &true_value);
 
3414
  rc= mysql_stmt_execute(stmt);
 
3415
  check_execute(stmt, rc);
 
3416
 
 
3417
  rc= mysql_stmt_store_result(stmt);
 
3418
  check_execute(stmt, rc);
 
3419
 
 
3420
  result= mysql_stmt_result_metadata(stmt);
 
3421
  field= mysql_fetch_fields(result);
 
3422
 
 
3423
  DIE_UNLESS(field->max_length == max_blob_length);
 
3424
 
 
3425
  /* Fetch results into a data buffer that is smaller than data */
 
3426
  bzero((char*) my_bind, sizeof(*my_bind));
 
3427
  my_bind[0].buffer_type= MYSQL_TYPE_BLOB;
 
3428
  my_bind[0].buffer= (void *) &data; /* this buffer won't be altered */
 
3429
  my_bind[0].buffer_length= 16;
 
3430
  my_bind[0].length= &blob_length;
 
3431
  my_bind[0].error= &my_bind[0].error_value;
 
3432
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
3433
  data[16]= 0;
 
3434
 
 
3435
  rc= mysql_stmt_fetch(stmt);
 
3436
  DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
 
3437
  DIE_UNLESS(my_bind[0].error_value);
 
3438
  DIE_UNLESS(strlen(data) == 16);
 
3439
  DIE_UNLESS(blob_length == max_blob_length);
 
3440
 
 
3441
  /* Fetch all data */
 
3442
  bzero((char*) (my_bind+1), sizeof(*my_bind));
 
3443
  my_bind[1].buffer_type= MYSQL_TYPE_BLOB;
 
3444
  my_bind[1].buffer= (void *) &data; /* this buffer won't be altered */
 
3445
  my_bind[1].buffer_length= sizeof(data);
 
3446
  my_bind[1].length= &blob_length;
 
3447
  bzero(data, sizeof(data));
 
3448
  mysql_stmt_fetch_column(stmt, my_bind+1, 0, 0);
 
3449
  DIE_UNLESS(strlen(data) == max_blob_length);
 
3450
 
 
3451
  mysql_free_result(result);
 
3452
  mysql_stmt_close(stmt);
 
3453
 
 
3454
  /* Drop created table */
 
3455
  rc= mysql_query(mysql, "DROP TABLE test_long_data_str");
 
3456
  myquery(rc);
 
3457
}
 
3458
 
 
3459
 
 
3460
/* Test long data (binary) handling */
 
3461
 
 
3462
static void test_long_data_bin()
 
3463
{
 
3464
  MYSQL_STMT *stmt;
 
3465
  int        rc;
 
3466
  char       data[255];
 
3467
  long       length;
 
3468
  MYSQL_RES  *result;
 
3469
  MYSQL_BIND my_bind[2];
 
3470
  char query[MAX_TEST_QUERY_LENGTH];
 
3471
 
 
3472
 
 
3473
  myheader("test_long_data_bin");
 
3474
 
 
3475
  rc= mysql_autocommit(mysql, TRUE);
 
3476
  myquery(rc);
 
3477
 
 
3478
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data_bin");
 
3479
  myquery(rc);
 
3480
 
 
3481
  rc= mysql_query(mysql, "CREATE TABLE test_long_data_bin(id int, longbin long varbinary)");
 
3482
  myquery(rc);
 
3483
 
 
3484
  strmov(query, "INSERT INTO test_long_data_bin VALUES(?, ?)");
 
3485
  stmt= mysql_simple_prepare(mysql, query);
 
3486
  check_stmt(stmt);
 
3487
 
 
3488
  verify_param_count(stmt, 2);
 
3489
 
 
3490
  /* Always bzero all members of bind parameter */
 
3491
  bzero((char*) my_bind, sizeof(my_bind));
 
3492
 
 
3493
  my_bind[0].buffer= (void *)&length;
 
3494
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
3495
  length= 0;
 
3496
 
 
3497
  my_bind[1].buffer= data;           /* string data */
 
3498
  my_bind[1].buffer_type= MYSQL_TYPE_LONG_BLOB;
 
3499
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
3500
  check_execute(stmt, rc);
 
3501
 
 
3502
  length= 10;
 
3503
  strmov(data, "MySQL AB");
 
3504
 
 
3505
  /* supply data in pieces */
 
3506
  {
 
3507
    int i;
 
3508
    for (i= 0; i < 100; i++)
 
3509
    {
 
3510
      rc= mysql_stmt_send_long_data(stmt, 1, (char *)data, 4);
 
3511
      check_execute(stmt, rc);
 
3512
    }
 
3513
  }
 
3514
  /* execute */
 
3515
  rc= mysql_stmt_execute(stmt);
 
3516
  if (!opt_silent)
 
3517
    fprintf(stdout, " mysql_stmt_execute() returned %d\n", rc);
 
3518
  check_execute(stmt, rc);
 
3519
 
 
3520
  mysql_stmt_close(stmt);
 
3521
 
 
3522
  rc= mysql_commit(mysql);
 
3523
  myquery(rc);
 
3524
 
 
3525
  /* now fetch the results ..*/
 
3526
  rc= mysql_query(mysql, "SELECT LENGTH(longbin), longbin FROM test_long_data_bin");
 
3527
  myquery(rc);
 
3528
 
 
3529
  /* get the result */
 
3530
  result= mysql_store_result(mysql);
 
3531
  mytest(result);
 
3532
 
 
3533
  rc= my_process_result_set(result);
 
3534
  DIE_UNLESS(rc == 1);
 
3535
  mysql_free_result(result);
 
3536
}
 
3537
 
 
3538
 
 
3539
/* Test simple delete */
 
3540
 
 
3541
static void test_simple_delete()
 
3542
{
 
3543
  MYSQL_STMT *stmt;
 
3544
  int        rc;
 
3545
  char       szData[30]= {0};
 
3546
  int        nData= 1;
 
3547
  MYSQL_RES  *result;
 
3548
  MYSQL_BIND my_bind[2];
 
3549
  ulong length[2];
 
3550
  char query[MAX_TEST_QUERY_LENGTH];
 
3551
 
 
3552
  myheader("test_simple_delete");
 
3553
 
 
3554
  rc= mysql_autocommit(mysql, TRUE);
 
3555
  myquery(rc);
 
3556
 
 
3557
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_simple_delete");
 
3558
  myquery(rc);
 
3559
 
 
3560
  rc= mysql_query(mysql, "CREATE TABLE test_simple_delete(col1 int, \
 
3561
                                col2 varchar(50), col3 int )");
 
3562
  myquery(rc);
 
3563
 
 
3564
  rc= mysql_query(mysql, "INSERT INTO test_simple_delete VALUES(1, 'MySQL', 100)");
 
3565
  myquery(rc);
 
3566
 
 
3567
  verify_affected_rows(1);
 
3568
 
 
3569
  rc= mysql_commit(mysql);
 
3570
  myquery(rc);
 
3571
 
 
3572
  /* insert by prepare */
 
3573
  strmov(query, "DELETE FROM test_simple_delete WHERE col1= ? AND "
 
3574
                "CONVERT(col2 USING utf8)= ? AND col3= 100");
 
3575
  stmt= mysql_simple_prepare(mysql, query);
 
3576
  check_stmt(stmt);
 
3577
 
 
3578
  verify_param_count(stmt, 2);
 
3579
 
 
3580
  /* Always bzero all members of bind parameter */
 
3581
  bzero((char*) my_bind, sizeof(my_bind));
 
3582
 
 
3583
  nData= 1;
 
3584
  strmov(szData, "MySQL");
 
3585
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
3586
  my_bind[1].buffer= szData;               /* string data */
 
3587
  my_bind[1].buffer_length= sizeof(szData);
 
3588
  my_bind[1].length= &length[1];
 
3589
  length[1]= 5;
 
3590
 
 
3591
  my_bind[0].buffer= (void *)&nData;
 
3592
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
3593
 
 
3594
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
3595
  check_execute(stmt, rc);
 
3596
 
 
3597
  rc= mysql_stmt_execute(stmt);
 
3598
  check_execute(stmt, rc);
 
3599
 
 
3600
  verify_affected_rows(1);
 
3601
 
 
3602
  mysql_stmt_close(stmt);
 
3603
 
 
3604
  /* now fetch the results ..*/
 
3605
  rc= mysql_commit(mysql);
 
3606
  myquery(rc);
 
3607
 
 
3608
  /* test the results now, only one row should exist */
 
3609
  rc= mysql_query(mysql, "SELECT * FROM test_simple_delete");
 
3610
  myquery(rc);
 
3611
 
 
3612
  /* get the result */
 
3613
  result= mysql_store_result(mysql);
 
3614
  mytest(result);
 
3615
 
 
3616
  rc= my_process_result_set(result);
 
3617
  DIE_UNLESS(rc == 0);
 
3618
  mysql_free_result(result);
 
3619
}
 
3620
 
 
3621
 
 
3622
/* Test simple update */
 
3623
 
 
3624
static void test_update()
 
3625
{
 
3626
  MYSQL_STMT *stmt;
 
3627
  int        rc;
 
3628
  char       szData[25];
 
3629
  int        nData= 1;
 
3630
  MYSQL_RES  *result;
 
3631
  MYSQL_BIND my_bind[2];
 
3632
  ulong length[2];
 
3633
  char query[MAX_TEST_QUERY_LENGTH];
 
3634
 
 
3635
  myheader("test_update");
 
3636
 
 
3637
  rc= mysql_autocommit(mysql, TRUE);
 
3638
  myquery(rc);
 
3639
 
 
3640
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_update");
 
3641
  myquery(rc);
 
3642
 
 
3643
  rc= mysql_query(mysql, "CREATE TABLE test_update("
 
3644
                               "col1 int primary key auto_increment, "
 
3645
                               "col2 varchar(50), col3 int )");
 
3646
  myquery(rc);
 
3647
 
 
3648
  strmov(query, "INSERT INTO test_update(col2, col3) VALUES(?, ?)");
 
3649
  stmt= mysql_simple_prepare(mysql, query);
 
3650
  check_stmt(stmt);
 
3651
 
 
3652
  verify_param_count(stmt, 2);
 
3653
 
 
3654
  /* Always bzero all members of bind parameter */
 
3655
  bzero((char*) my_bind, sizeof(my_bind));
 
3656
 
 
3657
  /* string data */
 
3658
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
3659
  my_bind[0].buffer= szData;
 
3660
  my_bind[0].buffer_length= sizeof(szData);
 
3661
  my_bind[0].length= &length[0];
 
3662
  length[0]= my_sprintf(szData, (szData, "inserted-data"));
 
3663
 
 
3664
  my_bind[1].buffer= (void *)&nData;
 
3665
  my_bind[1].buffer_type= MYSQL_TYPE_LONG;
 
3666
 
 
3667
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
3668
  check_execute(stmt, rc);
 
3669
 
 
3670
  nData= 100;
 
3671
  rc= mysql_stmt_execute(stmt);
 
3672
  check_execute(stmt, rc);
 
3673
 
 
3674
  verify_affected_rows(1);
 
3675
  mysql_stmt_close(stmt);
 
3676
 
 
3677
  strmov(query, "UPDATE test_update SET col2= ? WHERE col3= ?");
 
3678
  stmt= mysql_simple_prepare(mysql, query);
 
3679
  check_stmt(stmt);
 
3680
 
 
3681
  verify_param_count(stmt, 2);
 
3682
  nData= 100;
 
3683
 
 
3684
  /* Always bzero all members of bind parameter */
 
3685
  bzero((char*) my_bind, sizeof(my_bind));
 
3686
 
 
3687
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
3688
  my_bind[0].buffer= szData;
 
3689
  my_bind[0].buffer_length= sizeof(szData);
 
3690
  my_bind[0].length= &length[0];
 
3691
  length[0]= my_sprintf(szData, (szData, "updated-data"));
 
3692
 
 
3693
  my_bind[1].buffer= (void *)&nData;
 
3694
  my_bind[1].buffer_type= MYSQL_TYPE_LONG;
 
3695
 
 
3696
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
3697
  check_execute(stmt, rc);
 
3698
 
 
3699
  rc= mysql_stmt_execute(stmt);
 
3700
  check_execute(stmt, rc);
 
3701
  verify_affected_rows(1);
 
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 test_update");
 
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 prepare without parameters */
 
3724
 
 
3725
static void test_prepare_noparam()
 
3726
{
 
3727
  MYSQL_STMT *stmt;
 
3728
  int        rc;
 
3729
  MYSQL_RES  *result;
 
3730
  char query[MAX_TEST_QUERY_LENGTH];
 
3731
 
 
3732
  myheader("test_prepare_noparam");
 
3733
 
 
3734
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS my_prepare");
 
3735
  myquery(rc);
 
3736
 
 
3737
 
 
3738
  rc= mysql_query(mysql, "CREATE TABLE my_prepare(col1 int, col2 varchar(50))");
 
3739
  myquery(rc);
 
3740
 
 
3741
  /* insert by prepare */
 
3742
  strmov(query, "INSERT INTO my_prepare VALUES(10, 'venu')");
 
3743
  stmt= mysql_simple_prepare(mysql, query);
 
3744
  check_stmt(stmt);
 
3745
 
 
3746
  verify_param_count(stmt, 0);
 
3747
 
 
3748
  rc= mysql_stmt_execute(stmt);
 
3749
  check_execute(stmt, rc);
 
3750
 
 
3751
  mysql_stmt_close(stmt);
 
3752
 
 
3753
  /* now fetch the results ..*/
 
3754
  rc= mysql_commit(mysql);
 
3755
  myquery(rc);
 
3756
 
 
3757
  /* test the results now, only one row should exist */
 
3758
  rc= mysql_query(mysql, "SELECT * FROM my_prepare");
 
3759
  myquery(rc);
 
3760
 
 
3761
  /* get the result */
 
3762
  result= mysql_store_result(mysql);
 
3763
  mytest(result);
 
3764
 
 
3765
  rc= my_process_result_set(result);
 
3766
  DIE_UNLESS(rc == 1);
 
3767
  mysql_free_result(result);
 
3768
}
 
3769
 
 
3770
 
 
3771
/* Test simple bind result */
 
3772
 
 
3773
static void test_bind_result()
 
3774
{
 
3775
  MYSQL_STMT *stmt;
 
3776
  int        rc;
 
3777
  int        nData;
 
3778
  ulong      length1;
 
3779
  char       szData[100];
 
3780
  MYSQL_BIND my_bind[2];
 
3781
  my_bool    is_null[2];
 
3782
 
 
3783
  myheader("test_bind_result");
 
3784
 
 
3785
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
 
3786
  myquery(rc);
 
3787
 
 
3788
  rc= mysql_query(mysql, "CREATE TABLE test_bind_result(col1 int , col2 varchar(50))");
 
3789
  myquery(rc);
 
3790
 
 
3791
  rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES(10, 'venu')");
 
3792
  myquery(rc);
 
3793
 
 
3794
  rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES(20, 'MySQL')");
 
3795
  myquery(rc);
 
3796
 
 
3797
  rc= mysql_query(mysql, "INSERT INTO test_bind_result(col2) VALUES('monty')");
 
3798
  myquery(rc);
 
3799
 
 
3800
  rc= mysql_commit(mysql);
 
3801
  myquery(rc);
 
3802
 
 
3803
  /* fetch */
 
3804
 
 
3805
  bzero((char*) my_bind, sizeof(my_bind));
 
3806
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
3807
  my_bind[0].buffer= (void *) &nData;      /* integer data */
 
3808
  my_bind[0].is_null= &is_null[0];
 
3809
 
 
3810
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
3811
  my_bind[1].buffer= szData;                /* string data */
 
3812
  my_bind[1].buffer_length= sizeof(szData);
 
3813
  my_bind[1].length= &length1;
 
3814
  my_bind[1].is_null= &is_null[1];
 
3815
 
 
3816
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_bind_result");
 
3817
  check_stmt(stmt);
 
3818
 
 
3819
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
3820
  check_execute(stmt, rc);
 
3821
 
 
3822
  rc= mysql_stmt_execute(stmt);
 
3823
  check_execute(stmt, rc);
 
3824
 
 
3825
  rc= mysql_stmt_fetch(stmt);
 
3826
  check_execute(stmt, rc);
 
3827
 
 
3828
  if (!opt_silent)
 
3829
    fprintf(stdout, "\n row 1: %d, %s(%lu)", nData, szData, length1);
 
3830
  DIE_UNLESS(nData == 10);
 
3831
  DIE_UNLESS(strcmp(szData, "venu") == 0);
 
3832
  DIE_UNLESS(length1 == 4);
 
3833
 
 
3834
  rc= mysql_stmt_fetch(stmt);
 
3835
  check_execute(stmt, rc);
 
3836
 
 
3837
  if (!opt_silent)
 
3838
    fprintf(stdout, "\n row 2: %d, %s(%lu)", nData, szData, length1);
 
3839
  DIE_UNLESS(nData == 20);
 
3840
  DIE_UNLESS(strcmp(szData, "MySQL") == 0);
 
3841
  DIE_UNLESS(length1 == 5);
 
3842
 
 
3843
  rc= mysql_stmt_fetch(stmt);
 
3844
  check_execute(stmt, rc);
 
3845
 
 
3846
  if (!opt_silent && is_null[0])
 
3847
    fprintf(stdout, "\n row 3: NULL, %s(%lu)", szData, length1);
 
3848
  DIE_UNLESS(is_null[0]);
 
3849
  DIE_UNLESS(strcmp(szData, "monty") == 0);
 
3850
  DIE_UNLESS(length1 == 5);
 
3851
 
 
3852
  rc= mysql_stmt_fetch(stmt);
 
3853
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
3854
 
 
3855
  mysql_stmt_close(stmt);
 
3856
}
 
3857
 
 
3858
 
 
3859
/* Test ext bind result */
 
3860
 
 
3861
static void test_bind_result_ext()
 
3862
{
 
3863
  MYSQL_STMT *stmt;
 
3864
  int        rc, i;
 
3865
  uchar      t_data;
 
3866
  short      s_data;
 
3867
  int        i_data;
 
3868
  longlong   b_data;
 
3869
  float      f_data;
 
3870
  double     d_data;
 
3871
  char       szData[20], bData[20];
 
3872
  ulong       szLength, bLength;
 
3873
  MYSQL_BIND my_bind[8];
 
3874
  ulong      length[8];
 
3875
  my_bool    is_null[8];
 
3876
  char       llbuf[22];
 
3877
  myheader("test_bind_result_ext");
 
3878
 
 
3879
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
 
3880
  myquery(rc);
 
3881
 
 
3882
  rc= mysql_query(mysql, "CREATE TABLE test_bind_result(c1 tinyint, "
 
3883
                                                      " c2 smallint, "
 
3884
                                                      " c3 int, c4 bigint, "
 
3885
                                                      " c5 float, c6 double, "
 
3886
                                                      " c7 varbinary(10), "
 
3887
                                                      " c8 varchar(50))");
 
3888
  myquery(rc);
 
3889
 
 
3890
  rc= mysql_query(mysql, "INSERT INTO test_bind_result "
 
3891
                         "VALUES (19, 2999, 3999, 4999999, "
 
3892
                         " 2345.6, 5678.89563, 'venu', 'mysql')");
 
3893
  myquery(rc);
 
3894
 
 
3895
  rc= mysql_commit(mysql);
 
3896
  myquery(rc);
 
3897
 
 
3898
  bzero((char*) my_bind, sizeof(my_bind));
 
3899
  for (i= 0; i < (int) array_elements(my_bind); i++)
 
3900
  {
 
3901
    my_bind[i].length=  &length[i];
 
3902
    my_bind[i].is_null= &is_null[i];
 
3903
  }
 
3904
 
 
3905
  my_bind[0].buffer_type= MYSQL_TYPE_TINY;
 
3906
  my_bind[0].buffer= (void *)&t_data;
 
3907
 
 
3908
  my_bind[1].buffer_type= MYSQL_TYPE_SHORT;
 
3909
  my_bind[2].buffer_type= MYSQL_TYPE_LONG;
 
3910
 
 
3911
  my_bind[3].buffer_type= MYSQL_TYPE_LONGLONG;
 
3912
  my_bind[1].buffer= (void *)&s_data;
 
3913
 
 
3914
  my_bind[2].buffer= (void *)&i_data;
 
3915
  my_bind[3].buffer= (void *)&b_data;
 
3916
 
 
3917
  my_bind[4].buffer_type= MYSQL_TYPE_FLOAT;
 
3918
  my_bind[4].buffer= (void *)&f_data;
 
3919
 
 
3920
  my_bind[5].buffer_type= MYSQL_TYPE_DOUBLE;
 
3921
  my_bind[5].buffer= (void *)&d_data;
 
3922
 
 
3923
  my_bind[6].buffer_type= MYSQL_TYPE_STRING;
 
3924
  my_bind[6].buffer= (void *)szData;
 
3925
  my_bind[6].buffer_length= sizeof(szData);
 
3926
  my_bind[6].length= &szLength;
 
3927
 
 
3928
  my_bind[7].buffer_type= MYSQL_TYPE_TINY_BLOB;
 
3929
  my_bind[7].buffer= (void *)&bData;
 
3930
  my_bind[7].length= &bLength;
 
3931
  my_bind[7].buffer_length= sizeof(bData);
 
3932
 
 
3933
  stmt= mysql_simple_prepare(mysql, "select * from test_bind_result");
 
3934
  check_stmt(stmt);
 
3935
 
 
3936
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
3937
  check_execute(stmt, rc);
 
3938
 
 
3939
  rc= mysql_stmt_execute(stmt);
 
3940
  check_execute(stmt, rc);
 
3941
 
 
3942
  rc= mysql_stmt_fetch(stmt);
 
3943
  check_execute(stmt, rc);
 
3944
 
 
3945
  if (!opt_silent)
 
3946
  {
 
3947
    fprintf(stdout, "\n data (tiny)   : %d", t_data);
 
3948
    fprintf(stdout, "\n data (short)  : %d", s_data);
 
3949
    fprintf(stdout, "\n data (int)    : %d", i_data);
 
3950
    fprintf(stdout, "\n data (big)    : %s", llstr(b_data, llbuf));
 
3951
 
 
3952
    fprintf(stdout, "\n data (float)  : %f", f_data);
 
3953
    fprintf(stdout, "\n data (double) : %f", d_data);
 
3954
 
 
3955
    fprintf(stdout, "\n data (str)    : %s(%lu)", szData, szLength);
 
3956
 
 
3957
    bData[bLength]= '\0';                         /* bData is binary */
 
3958
    fprintf(stdout, "\n data (bin)    : %s(%lu)", bData, bLength);
 
3959
  }
 
3960
 
 
3961
  DIE_UNLESS(t_data == 19);
 
3962
  DIE_UNLESS(s_data == 2999);
 
3963
  DIE_UNLESS(i_data == 3999);
 
3964
  DIE_UNLESS(b_data == 4999999);
 
3965
  /*DIE_UNLESS(f_data == 2345.60);*/
 
3966
  /*DIE_UNLESS(d_data == 5678.89563);*/
 
3967
  DIE_UNLESS(strcmp(szData, "venu") == 0);
 
3968
  DIE_UNLESS(strncmp(bData, "mysql", 5) == 0);
 
3969
  DIE_UNLESS(szLength == 4);
 
3970
  DIE_UNLESS(bLength == 5);
 
3971
 
 
3972
  rc= mysql_stmt_fetch(stmt);
 
3973
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
3974
 
 
3975
  mysql_stmt_close(stmt);
 
3976
}
 
3977
 
 
3978
 
 
3979
/* Test ext bind result */
 
3980
 
 
3981
static void test_bind_result_ext1()
 
3982
{
 
3983
  MYSQL_STMT *stmt;
 
3984
  uint       i;
 
3985
  int        rc;
 
3986
  char       t_data[20];
 
3987
  float      s_data;
 
3988
  short      i_data;
 
3989
  uchar      b_data;
 
3990
  int        f_data;
 
3991
  long       bData;
 
3992
  char       d_data[20];
 
3993
  double     szData;
 
3994
  MYSQL_BIND my_bind[8];
 
3995
  ulong      length[8];
 
3996
  my_bool    is_null[8];
 
3997
  myheader("test_bind_result_ext1");
 
3998
 
 
3999
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
 
4000
  myquery(rc);
 
4001
 
 
4002
  rc= mysql_query(mysql, "CREATE TABLE test_bind_result(c1 tinyint, c2 smallint, \
 
4003
                                                        c3 int, c4 bigint, \
 
4004
                                                        c5 float, c6 double, \
 
4005
                                                        c7 varbinary(10), \
 
4006
                                                        c8 varchar(10))");
 
4007
  myquery(rc);
 
4008
 
 
4009
  rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES(120, 2999, 3999, 54, \
 
4010
                                                              2.6, 58.89, \
 
4011
                                                              '206', '6.7')");
 
4012
  myquery(rc);
 
4013
 
 
4014
  rc= mysql_commit(mysql);
 
4015
  myquery(rc);
 
4016
 
 
4017
  bzero((char*) my_bind, sizeof(my_bind));
 
4018
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
4019
  my_bind[0].buffer= (void *) t_data;
 
4020
  my_bind[0].buffer_length= sizeof(t_data);
 
4021
  my_bind[0].error= &my_bind[0].error_value;
 
4022
 
 
4023
  my_bind[1].buffer_type= MYSQL_TYPE_FLOAT;
 
4024
  my_bind[1].buffer= (void *)&s_data;
 
4025
  my_bind[1].buffer_length= 0;
 
4026
  my_bind[1].error= &my_bind[1].error_value;
 
4027
 
 
4028
  my_bind[2].buffer_type= MYSQL_TYPE_SHORT;
 
4029
  my_bind[2].buffer= (void *)&i_data;
 
4030
  my_bind[2].buffer_length= 0;
 
4031
  my_bind[2].error= &my_bind[2].error_value;
 
4032
 
 
4033
  my_bind[3].buffer_type= MYSQL_TYPE_TINY;
 
4034
  my_bind[3].buffer= (void *)&b_data;
 
4035
  my_bind[3].buffer_length= 0;
 
4036
  my_bind[3].error= &my_bind[3].error_value;
 
4037
 
 
4038
  my_bind[4].buffer_type= MYSQL_TYPE_LONG;
 
4039
  my_bind[4].buffer= (void *)&f_data;
 
4040
  my_bind[4].buffer_length= 0;
 
4041
  my_bind[4].error= &my_bind[4].error_value;
 
4042
 
 
4043
  my_bind[5].buffer_type= MYSQL_TYPE_STRING;
 
4044
  my_bind[5].buffer= (void *)d_data;
 
4045
  my_bind[5].buffer_length= sizeof(d_data);
 
4046
  my_bind[5].error= &my_bind[5].error_value;
 
4047
 
 
4048
  my_bind[6].buffer_type= MYSQL_TYPE_LONG;
 
4049
  my_bind[6].buffer= (void *)&bData;
 
4050
  my_bind[6].buffer_length= 0;
 
4051
  my_bind[6].error= &my_bind[6].error_value;
 
4052
 
 
4053
  my_bind[7].buffer_type= MYSQL_TYPE_DOUBLE;
 
4054
  my_bind[7].buffer= (void *)&szData;
 
4055
  my_bind[7].buffer_length= 0;
 
4056
  my_bind[7].error= &my_bind[7].error_value;
 
4057
 
 
4058
  for (i= 0; i < array_elements(my_bind); i++)
 
4059
  {
 
4060
    my_bind[i].is_null= &is_null[i];
 
4061
    my_bind[i].length= &length[i];
 
4062
  }
 
4063
 
 
4064
  stmt= mysql_simple_prepare(mysql, "select * from test_bind_result");
 
4065
  check_stmt(stmt);
 
4066
 
 
4067
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
4068
  check_execute(stmt, rc);
 
4069
 
 
4070
  rc= mysql_stmt_execute(stmt);
 
4071
  check_execute(stmt, rc);
 
4072
 
 
4073
  rc= mysql_stmt_fetch(stmt);
 
4074
  printf("rc=%d\n", rc);
 
4075
  DIE_UNLESS(rc == 0);
 
4076
 
 
4077
  if (!opt_silent)
 
4078
  {
 
4079
    fprintf(stdout, "\n data (tiny)   : %s(%lu)", t_data, length[0]);
 
4080
    fprintf(stdout, "\n data (short)  : %f(%lu)", s_data, length[1]);
 
4081
    fprintf(stdout, "\n data (int)    : %d(%lu)", i_data, length[2]);
 
4082
    fprintf(stdout, "\n data (big)    : %d(%lu)", b_data, length[3]);
 
4083
 
 
4084
    fprintf(stdout, "\n data (float)  : %d(%lu)", f_data, length[4]);
 
4085
    fprintf(stdout, "\n data (double) : %s(%lu)", d_data, length[5]);
 
4086
 
 
4087
    fprintf(stdout, "\n data (bin)    : %ld(%lu)", bData, length[6]);
 
4088
    fprintf(stdout, "\n data (str)    : %g(%lu)", szData, length[7]);
 
4089
  }
 
4090
 
 
4091
  DIE_UNLESS(strcmp(t_data, "120") == 0);
 
4092
  DIE_UNLESS(i_data == 3999);
 
4093
  DIE_UNLESS(f_data == 2);
 
4094
  DIE_UNLESS(strcmp(d_data, "58.89") == 0);
 
4095
  DIE_UNLESS(b_data == 54);
 
4096
 
 
4097
  DIE_UNLESS(length[0] == 3);
 
4098
  DIE_UNLESS(length[1] == 4);
 
4099
  DIE_UNLESS(length[2] == 2);
 
4100
  DIE_UNLESS(length[3] == 1);
 
4101
  DIE_UNLESS(length[4] == 4);
 
4102
  DIE_UNLESS(length[5] == 5);
 
4103
  DIE_UNLESS(length[6] == 4);
 
4104
  DIE_UNLESS(length[7] == 8);
 
4105
 
 
4106
  rc= mysql_stmt_fetch(stmt);
 
4107
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
4108
 
 
4109
  mysql_stmt_close(stmt);
 
4110
}
 
4111
 
 
4112
 
 
4113
/* Generalized fetch conversion routine for all basic types */
 
4114
 
 
4115
static void bind_fetch(int row_count)
 
4116
{
 
4117
  MYSQL_STMT   *stmt;
 
4118
  int          rc, i, count= row_count;
 
4119
  int32        data[10];
 
4120
  int8         i8_data;
 
4121
  int16        i16_data;
 
4122
  int32        i32_data;
 
4123
  longlong     i64_data;
 
4124
  float        f_data;
 
4125
  double       d_data;
 
4126
  char         s_data[10];
 
4127
  ulong        length[10];
 
4128
  MYSQL_BIND   my_bind[7];
 
4129
  my_bool      is_null[7];
 
4130
 
 
4131
  stmt= mysql_simple_prepare(mysql, "INSERT INTO test_bind_fetch VALUES "
 
4132
                                    "(?, ?, ?, ?, ?, ?, ?)");
 
4133
  check_stmt(stmt);
 
4134
 
 
4135
  verify_param_count(stmt, 7);
 
4136
 
 
4137
  /* Always bzero all members of bind parameter */
 
4138
  bzero((char*) my_bind, sizeof(my_bind));
 
4139
 
 
4140
  for (i= 0; i < (int) array_elements(my_bind); i++)
 
4141
  {
 
4142
    my_bind[i].buffer_type= MYSQL_TYPE_LONG;
 
4143
    my_bind[i].buffer= (void *) &data[i];
 
4144
  }
 
4145
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
4146
  check_execute(stmt, rc);
 
4147
 
 
4148
  while (count--)
 
4149
  {
 
4150
    rc= 10+count;
 
4151
    for (i= 0; i < (int) array_elements(my_bind); i++)
 
4152
    {
 
4153
      data[i]= rc+i;
 
4154
      rc+= 12;
 
4155
    }
 
4156
    rc= mysql_stmt_execute(stmt);
 
4157
    check_execute(stmt, rc);
 
4158
  }
 
4159
 
 
4160
  rc= mysql_commit(mysql);
 
4161
  myquery(rc);
 
4162
 
 
4163
  mysql_stmt_close(stmt);
 
4164
 
 
4165
  rc= my_stmt_result("SELECT * FROM test_bind_fetch");
 
4166
  DIE_UNLESS(row_count == rc);
 
4167
 
 
4168
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_bind_fetch");
 
4169
  check_stmt(stmt);
 
4170
 
 
4171
  for (i= 0; i < (int) array_elements(my_bind); i++)
 
4172
  {
 
4173
    my_bind[i].buffer= (void *) &data[i];
 
4174
    my_bind[i].length= &length[i];
 
4175
    my_bind[i].is_null= &is_null[i];
 
4176
  }
 
4177
 
 
4178
  my_bind[0].buffer_type= MYSQL_TYPE_TINY;
 
4179
  my_bind[0].buffer= (void *)&i8_data;
 
4180
 
 
4181
  my_bind[1].buffer_type= MYSQL_TYPE_SHORT;
 
4182
  my_bind[1].buffer= (void *)&i16_data;
 
4183
 
 
4184
  my_bind[2].buffer_type= MYSQL_TYPE_LONG;
 
4185
  my_bind[2].buffer= (void *)&i32_data;
 
4186
 
 
4187
  my_bind[3].buffer_type= MYSQL_TYPE_LONGLONG;
 
4188
  my_bind[3].buffer= (void *)&i64_data;
 
4189
 
 
4190
  my_bind[4].buffer_type= MYSQL_TYPE_FLOAT;
 
4191
  my_bind[4].buffer= (void *)&f_data;
 
4192
 
 
4193
  my_bind[5].buffer_type= MYSQL_TYPE_DOUBLE;
 
4194
  my_bind[5].buffer= (void *)&d_data;
 
4195
 
 
4196
  my_bind[6].buffer_type= MYSQL_TYPE_STRING;
 
4197
  my_bind[6].buffer= (void *)&s_data;
 
4198
  my_bind[6].buffer_length= sizeof(s_data);
 
4199
 
 
4200
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
4201
  check_execute(stmt, rc);
 
4202
 
 
4203
  rc= mysql_stmt_execute(stmt);
 
4204
  check_execute(stmt, rc);
 
4205
 
 
4206
  rc= mysql_stmt_store_result(stmt);
 
4207
  check_execute(stmt, rc);
 
4208
 
 
4209
  while (row_count--)
 
4210
  {
 
4211
    rc= mysql_stmt_fetch(stmt);
 
4212
    check_execute(stmt, rc);
 
4213
 
 
4214
    if (!opt_silent)
 
4215
    {
 
4216
      fprintf(stdout, "\n");
 
4217
      fprintf(stdout, "\n tiny     : %ld(%lu)", (ulong) i8_data, length[0]);
 
4218
      fprintf(stdout, "\n short    : %ld(%lu)", (ulong) i16_data, length[1]);
 
4219
      fprintf(stdout, "\n int      : %ld(%lu)", (ulong) i32_data, length[2]);
 
4220
      fprintf(stdout, "\n longlong : %ld(%lu)", (ulong) i64_data, length[3]);
 
4221
      fprintf(stdout, "\n float    : %f(%lu)",  f_data,  length[4]);
 
4222
      fprintf(stdout, "\n double   : %g(%lu)",  d_data,  length[5]);
 
4223
      fprintf(stdout, "\n char     : %s(%lu)",  s_data,  length[6]);
 
4224
    }
 
4225
    rc= 10+row_count;
 
4226
 
 
4227
    /* TINY */
 
4228
    DIE_UNLESS((int) i8_data == rc);
 
4229
    DIE_UNLESS(length[0] == 1);
 
4230
    rc+= 13;
 
4231
 
 
4232
    /* SHORT */
 
4233
    DIE_UNLESS((int) i16_data == rc);
 
4234
    DIE_UNLESS(length[1] == 2);
 
4235
    rc+= 13;
 
4236
 
 
4237
    /* LONG */
 
4238
    DIE_UNLESS((int) i32_data == rc);
 
4239
    DIE_UNLESS(length[2] == 4);
 
4240
    rc+= 13;
 
4241
 
 
4242
    /* LONGLONG */
 
4243
    DIE_UNLESS((int) i64_data == rc);
 
4244
    DIE_UNLESS(length[3] == 8);
 
4245
    rc+= 13;
 
4246
 
 
4247
    /* FLOAT */
 
4248
    DIE_UNLESS((int)f_data == rc);
 
4249
    DIE_UNLESS(length[4] == 4);
 
4250
    rc+= 13;
 
4251
 
 
4252
    /* DOUBLE */
 
4253
    DIE_UNLESS((int)d_data == rc);
 
4254
    DIE_UNLESS(length[5] == 8);
 
4255
    rc+= 13;
 
4256
 
 
4257
    /* CHAR */
 
4258
    {
 
4259
      char buff[20];
 
4260
      long len= my_sprintf(buff, (buff, "%d", rc));
 
4261
      DIE_UNLESS(strcmp(s_data, buff) == 0);
 
4262
      DIE_UNLESS(length[6] == (ulong) len);
 
4263
    }
 
4264
  }
 
4265
  rc= mysql_stmt_fetch(stmt);
 
4266
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
4267
 
 
4268
  mysql_stmt_close(stmt);
 
4269
}
 
4270
 
 
4271
 
 
4272
/* Test fetching of date, time and ts */
 
4273
 
 
4274
static void test_fetch_date()
 
4275
{
 
4276
  MYSQL_STMT *stmt;
 
4277
  uint       i;
 
4278
  int        rc, year;
 
4279
  char       date[25], my_time[25], ts[25], ts_4[25], ts_6[20], dt[20];
 
4280
  ulong      d_length, t_length, ts_length, ts4_length, ts6_length,
 
4281
             dt_length, y_length;
 
4282
  MYSQL_BIND my_bind[8];
 
4283
  my_bool    is_null[8];
 
4284
  ulong      length[8];
 
4285
 
 
4286
  myheader("test_fetch_date");
 
4287
 
 
4288
  /* Will not work if sql_mode is NO_ZERO_DATE (implicit if TRADITIONAL) */
 
4289
  rc= mysql_query(mysql, "SET SQL_MODE=''");
 
4290
  myquery(rc);
 
4291
 
 
4292
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_result");
 
4293
  myquery(rc);
 
4294
 
 
4295
  rc= mysql_query(mysql, "CREATE TABLE test_bind_result(c1 date, c2 time, \
 
4296
                                                        c3 timestamp(14), \
 
4297
                                                        c4 year, \
 
4298
                                                        c5 datetime, \
 
4299
                                                        c6 timestamp(4), \
 
4300
                                                        c7 timestamp(6))");
 
4301
  myquery(rc);
 
4302
 
 
4303
  rc= mysql_query(mysql, "SET SQL_MODE=''");
 
4304
  rc= mysql_query(mysql, "INSERT INTO test_bind_result VALUES('2002-01-02', \
 
4305
                                                              '12:49:00', \
 
4306
                                                              '2002-01-02 17:46:59', \
 
4307
                                                              2010, \
 
4308
                                                              '2010-07-10', \
 
4309
                                                              '2020', '1999-12-29')");
 
4310
  myquery(rc);
 
4311
 
 
4312
  rc= mysql_commit(mysql);
 
4313
  myquery(rc);
 
4314
 
 
4315
  bzero((char*) my_bind, sizeof(my_bind));
 
4316
  for (i= 0; i < array_elements(my_bind); i++)
 
4317
  {
 
4318
    my_bind[i].is_null= &is_null[i];
 
4319
    my_bind[i].length= &length[i];
 
4320
  }
 
4321
 
 
4322
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
4323
  my_bind[1]= my_bind[2]= my_bind[0];
 
4324
 
 
4325
  my_bind[0].buffer= (void *)&date;
 
4326
  my_bind[0].buffer_length= sizeof(date);
 
4327
  my_bind[0].length= &d_length;
 
4328
 
 
4329
  my_bind[1].buffer= (void *)&my_time;
 
4330
  my_bind[1].buffer_length= sizeof(my_time);
 
4331
  my_bind[1].length= &t_length;
 
4332
 
 
4333
  my_bind[2].buffer= (void *)&ts;
 
4334
  my_bind[2].buffer_length= sizeof(ts);
 
4335
  my_bind[2].length= &ts_length;
 
4336
 
 
4337
  my_bind[3].buffer_type= MYSQL_TYPE_LONG;
 
4338
  my_bind[3].buffer= (void *)&year;
 
4339
  my_bind[3].length= &y_length;
 
4340
 
 
4341
  my_bind[4].buffer_type= MYSQL_TYPE_STRING;
 
4342
  my_bind[4].buffer= (void *)&dt;
 
4343
  my_bind[4].buffer_length= sizeof(dt);
 
4344
  my_bind[4].length= &dt_length;
 
4345
 
 
4346
  my_bind[5].buffer_type= MYSQL_TYPE_STRING;
 
4347
  my_bind[5].buffer= (void *)&ts_4;
 
4348
  my_bind[5].buffer_length= sizeof(ts_4);
 
4349
  my_bind[5].length= &ts4_length;
 
4350
 
 
4351
  my_bind[6].buffer_type= MYSQL_TYPE_STRING;
 
4352
  my_bind[6].buffer= (void *)&ts_6;
 
4353
  my_bind[6].buffer_length= sizeof(ts_6);
 
4354
  my_bind[6].length= &ts6_length;
 
4355
 
 
4356
  rc= my_stmt_result("SELECT * FROM test_bind_result");
 
4357
  DIE_UNLESS(rc == 1);
 
4358
 
 
4359
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_bind_result");
 
4360
  check_stmt(stmt);
 
4361
 
 
4362
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
4363
  check_execute(stmt, rc);
 
4364
 
 
4365
  rc= mysql_stmt_execute(stmt);
 
4366
  check_execute(stmt, rc);
 
4367
 
 
4368
  ts_4[0]= '\0';
 
4369
  rc= mysql_stmt_fetch(stmt);
 
4370
  check_execute(stmt, rc);
 
4371
 
 
4372
  if (!opt_silent)
 
4373
  {
 
4374
    fprintf(stdout, "\n date   : %s(%lu)", date, d_length);
 
4375
    fprintf(stdout, "\n time   : %s(%lu)", my_time, t_length);
 
4376
    fprintf(stdout, "\n ts     : %s(%lu)", ts, ts_length);
 
4377
    fprintf(stdout, "\n year   : %d(%lu)", year, y_length);
 
4378
    fprintf(stdout, "\n dt     : %s(%lu)", dt,  dt_length);
 
4379
    fprintf(stdout, "\n ts(4)  : %s(%lu)", ts_4, ts4_length);
 
4380
    fprintf(stdout, "\n ts(6)  : %s(%lu)", ts_6, ts6_length);
 
4381
  }
 
4382
 
 
4383
  DIE_UNLESS(strcmp(date, "2002-01-02") == 0);
 
4384
  DIE_UNLESS(d_length == 10);
 
4385
 
 
4386
  DIE_UNLESS(strcmp(my_time, "12:49:00") == 0);
 
4387
  DIE_UNLESS(t_length == 8);
 
4388
 
 
4389
  DIE_UNLESS(strcmp(ts, "2002-01-02 17:46:59") == 0);
 
4390
  DIE_UNLESS(ts_length == 19);
 
4391
 
 
4392
  DIE_UNLESS(year == 2010);
 
4393
  DIE_UNLESS(y_length == 4);
 
4394
 
 
4395
  DIE_UNLESS(strcmp(dt, "2010-07-10 00:00:00") == 0);
 
4396
  DIE_UNLESS(dt_length == 19);
 
4397
 
 
4398
  DIE_UNLESS(strcmp(ts_4, "0000-00-00 00:00:00") == 0);
 
4399
  DIE_UNLESS(ts4_length == strlen("0000-00-00 00:00:00"));
 
4400
 
 
4401
  DIE_UNLESS(strcmp(ts_6, "1999-12-29 00:00:00") == 0);
 
4402
  DIE_UNLESS(ts6_length == 19);
 
4403
 
 
4404
  rc= mysql_stmt_fetch(stmt);
 
4405
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
4406
 
 
4407
  mysql_stmt_close(stmt);
 
4408
}
 
4409
 
 
4410
 
 
4411
/* Test fetching of str to all types */
 
4412
 
 
4413
static void test_fetch_str()
 
4414
{
 
4415
  int rc;
 
4416
 
 
4417
  myheader("test_fetch_str");
 
4418
 
 
4419
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
 
4420
  myquery(rc);
 
4421
 
 
4422
  rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 char(10), \
 
4423
                                                     c2 char(10), \
 
4424
                                                     c3 char(20), \
 
4425
                                                     c4 char(20), \
 
4426
                                                     c5 char(30), \
 
4427
                                                     c6 char(40), \
 
4428
                                                     c7 char(20))");
 
4429
  myquery(rc);
 
4430
 
 
4431
  bind_fetch(3);
 
4432
}
 
4433
 
 
4434
 
 
4435
/* Test fetching of long to all types */
 
4436
 
 
4437
static void test_fetch_long()
 
4438
{
 
4439
  int rc;
 
4440
 
 
4441
  myheader("test_fetch_long");
 
4442
 
 
4443
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
 
4444
  myquery(rc);
 
4445
 
 
4446
  rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 int unsigned, \
 
4447
                                                     c2 int unsigned, \
 
4448
                                                     c3 int, \
 
4449
                                                     c4 int, \
 
4450
                                                     c5 int, \
 
4451
                                                     c6 int unsigned, \
 
4452
                                                     c7 int)");
 
4453
  myquery(rc);
 
4454
 
 
4455
  bind_fetch(4);
 
4456
}
 
4457
 
 
4458
 
 
4459
/* Test fetching of short to all types */
 
4460
 
 
4461
static void test_fetch_short()
 
4462
{
 
4463
  int rc;
 
4464
 
 
4465
  myheader("test_fetch_short");
 
4466
 
 
4467
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
 
4468
  myquery(rc);
 
4469
 
 
4470
  rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 smallint unsigned, \
 
4471
                                                     c2 smallint, \
 
4472
                                                     c3 smallint unsigned, \
 
4473
                                                     c4 smallint, \
 
4474
                                                     c5 smallint, \
 
4475
                                                     c6 smallint, \
 
4476
                                                     c7 smallint unsigned)");
 
4477
  myquery(rc);
 
4478
 
 
4479
  bind_fetch(5);
 
4480
}
 
4481
 
 
4482
 
 
4483
/* Test fetching of tiny to all types */
 
4484
 
 
4485
static void test_fetch_tiny()
 
4486
{
 
4487
  int rc;
 
4488
 
 
4489
  myheader("test_fetch_tiny");
 
4490
 
 
4491
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
 
4492
  myquery(rc);
 
4493
 
 
4494
  rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 tinyint unsigned, \
 
4495
                                                     c2 tinyint, \
 
4496
                                                     c3 tinyint unsigned, \
 
4497
                                                     c4 tinyint, \
 
4498
                                                     c5 tinyint, \
 
4499
                                                     c6 tinyint, \
 
4500
                                                     c7 tinyint unsigned)");
 
4501
  myquery(rc);
 
4502
 
 
4503
  bind_fetch(3);
 
4504
 
 
4505
}
 
4506
 
 
4507
 
 
4508
/* Test fetching of longlong to all types */
 
4509
 
 
4510
static void test_fetch_bigint()
 
4511
{
 
4512
  int rc;
 
4513
 
 
4514
  myheader("test_fetch_bigint");
 
4515
 
 
4516
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
 
4517
  myquery(rc);
 
4518
 
 
4519
  rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 bigint, \
 
4520
                                                     c2 bigint, \
 
4521
                                                     c3 bigint unsigned, \
 
4522
                                                     c4 bigint unsigned, \
 
4523
                                                     c5 bigint unsigned, \
 
4524
                                                     c6 bigint unsigned, \
 
4525
                                                     c7 bigint unsigned)");
 
4526
  myquery(rc);
 
4527
 
 
4528
  bind_fetch(2);
 
4529
 
 
4530
}
 
4531
 
 
4532
 
 
4533
/* Test fetching of float to all types */
 
4534
 
 
4535
static void test_fetch_float()
 
4536
{
 
4537
  int rc;
 
4538
 
 
4539
  myheader("test_fetch_float");
 
4540
 
 
4541
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
 
4542
  myquery(rc);
 
4543
 
 
4544
  rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 float(3), \
 
4545
                                                     c2 float, \
 
4546
                                                     c3 float unsigned, \
 
4547
                                                     c4 float, \
 
4548
                                                     c5 float, \
 
4549
                                                     c6 float, \
 
4550
                                                     c7 float(10) unsigned)");
 
4551
  myquery(rc);
 
4552
 
 
4553
  bind_fetch(2);
 
4554
 
 
4555
}
 
4556
 
 
4557
 
 
4558
/* Test fetching of double to all types */
 
4559
 
 
4560
static void test_fetch_double()
 
4561
{
 
4562
  int rc;
 
4563
 
 
4564
  myheader("test_fetch_double");
 
4565
 
 
4566
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bind_fetch");
 
4567
  myquery(rc);
 
4568
 
 
4569
  rc= mysql_query(mysql, "CREATE TABLE test_bind_fetch(c1 double(5, 2), "
 
4570
                         "c2 double unsigned, c3 double unsigned, "
 
4571
                         "c4 double unsigned, c5 double unsigned, "
 
4572
                         "c6 double unsigned, c7 double unsigned)");
 
4573
  myquery(rc);
 
4574
 
 
4575
  bind_fetch(3);
 
4576
 
 
4577
}
 
4578
 
 
4579
 
 
4580
/* Test simple prepare with all possible types */
 
4581
 
 
4582
static void test_prepare_ext()
 
4583
{
 
4584
  MYSQL_STMT *stmt;
 
4585
  int        rc;
 
4586
  char       *sql;
 
4587
  int        nData= 1;
 
4588
  char       tData= 1;
 
4589
  short      sData= 10;
 
4590
  longlong   bData= 20;
 
4591
  MYSQL_BIND my_bind[6];
 
4592
  char query[MAX_TEST_QUERY_LENGTH];
 
4593
  myheader("test_prepare_ext");
 
4594
 
 
4595
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_ext");
 
4596
  myquery(rc);
 
4597
 
 
4598
  sql= (char *)"CREATE TABLE test_prepare_ext"
 
4599
               "("
 
4600
               " c1  tinyint,"
 
4601
               " c2  smallint,"
 
4602
               " c3  mediumint,"
 
4603
               " c4  int,"
 
4604
               " c5  integer,"
 
4605
               " c6  bigint,"
 
4606
               " c7  float,"
 
4607
               " c8  double,"
 
4608
               " c9  double precision,"
 
4609
               " c10 real,"
 
4610
               " c11 decimal(7, 4),"
 
4611
               " c12 numeric(8, 4),"
 
4612
               " c13 date,"
 
4613
               " c14 datetime,"
 
4614
               " c15 timestamp(14),"
 
4615
               " c16 time,"
 
4616
               " c17 year,"
 
4617
               " c18 bit,"
 
4618
               " c19 bool,"
 
4619
               " c20 char,"
 
4620
               " c21 char(10),"
 
4621
               " c22 varchar(30),"
 
4622
               " c23 tinyblob,"
 
4623
               " c24 tinytext,"
 
4624
               " c25 blob,"
 
4625
               " c26 text,"
 
4626
               " c27 mediumblob,"
 
4627
               " c28 mediumtext,"
 
4628
               " c29 longblob,"
 
4629
               " c30 longtext,"
 
4630
               " c31 enum('one', 'two', 'three'),"
 
4631
               " c32 set('monday', 'tuesday', 'wednesday'))";
 
4632
 
 
4633
  rc= mysql_query(mysql, sql);
 
4634
  myquery(rc);
 
4635
 
 
4636
  /* insert by prepare - all integers */
 
4637
  strmov(query, (char *)"INSERT INTO test_prepare_ext(c1, c2, c3, c4, c5, c6) VALUES(?, ?, ?, ?, ?, ?)");
 
4638
  stmt= mysql_simple_prepare(mysql, query);
 
4639
  check_stmt(stmt);
 
4640
 
 
4641
  verify_param_count(stmt, 6);
 
4642
 
 
4643
  /* Always bzero all members of bind parameter */
 
4644
  bzero((char*) my_bind, sizeof(my_bind));
 
4645
 
 
4646
  /*tinyint*/
 
4647
  my_bind[0].buffer_type= MYSQL_TYPE_TINY;
 
4648
  my_bind[0].buffer= (void *)&tData;
 
4649
 
 
4650
  /*smallint*/
 
4651
  my_bind[1].buffer_type= MYSQL_TYPE_SHORT;
 
4652
  my_bind[1].buffer= (void *)&sData;
 
4653
 
 
4654
  /*mediumint*/
 
4655
  my_bind[2].buffer_type= MYSQL_TYPE_LONG;
 
4656
  my_bind[2].buffer= (void *)&nData;
 
4657
 
 
4658
  /*int*/
 
4659
  my_bind[3].buffer_type= MYSQL_TYPE_LONG;
 
4660
  my_bind[3].buffer= (void *)&nData;
 
4661
 
 
4662
  /*integer*/
 
4663
  my_bind[4].buffer_type= MYSQL_TYPE_LONG;
 
4664
  my_bind[4].buffer= (void *)&nData;
 
4665
 
 
4666
  /*bigint*/
 
4667
  my_bind[5].buffer_type= MYSQL_TYPE_LONGLONG;
 
4668
  my_bind[5].buffer= (void *)&bData;
 
4669
 
 
4670
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
4671
  check_execute(stmt, rc);
 
4672
 
 
4673
  /*
 
4674
  *  integer to integer
 
4675
  */
 
4676
  for (nData= 0; nData<10; nData++, tData++, sData++, bData++)
 
4677
  {
 
4678
    rc= mysql_stmt_execute(stmt);
 
4679
    check_execute(stmt, rc);
 
4680
  }
 
4681
  mysql_stmt_close(stmt);
 
4682
 
 
4683
  /* now fetch the results ..*/
 
4684
 
 
4685
  stmt= mysql_simple_prepare(mysql, "SELECT c1, c2, c3, c4, c5, c6 "
 
4686
                                    "FROM test_prepare_ext");
 
4687
  check_stmt(stmt);
 
4688
 
 
4689
  /* get the result */
 
4690
  rc= mysql_stmt_execute(stmt);
 
4691
  check_execute(stmt, rc);
 
4692
 
 
4693
  rc= my_process_stmt_result(stmt);
 
4694
  DIE_UNLESS(nData == rc);
 
4695
 
 
4696
  mysql_stmt_close(stmt);
 
4697
}
 
4698
 
 
4699
 
 
4700
/* Test real and alias names */
 
4701
 
 
4702
static void test_field_names()
 
4703
{
 
4704
  int        rc;
 
4705
  MYSQL_RES  *result;
 
4706
 
 
4707
  myheader("test_field_names");
 
4708
 
 
4709
  if (!opt_silent)
 
4710
    fprintf(stdout, "\n %d, %d, %d", MYSQL_TYPE_DECIMAL, MYSQL_TYPE_NEWDATE, MYSQL_TYPE_ENUM);
 
4711
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_names1");
 
4712
  myquery(rc);
 
4713
 
 
4714
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_names2");
 
4715
  myquery(rc);
 
4716
 
 
4717
  rc= mysql_query(mysql, "CREATE TABLE test_field_names1(id int, name varchar(50))");
 
4718
  myquery(rc);
 
4719
 
 
4720
  rc= mysql_query(mysql, "CREATE TABLE test_field_names2(id int, name varchar(50))");
 
4721
  myquery(rc);
 
4722
 
 
4723
  /* with table name included with TRUE column name */
 
4724
  rc= mysql_query(mysql, "SELECT id as 'id-alias' FROM test_field_names1");
 
4725
  myquery(rc);
 
4726
 
 
4727
  result= mysql_use_result(mysql);
 
4728
  mytest(result);
 
4729
 
 
4730
  rc= my_process_result_set(result);
 
4731
  DIE_UNLESS(rc == 0);
 
4732
  mysql_free_result(result);
 
4733
 
 
4734
  /* with table name included with TRUE column name */
 
4735
  rc= mysql_query(mysql, "SELECT t1.id as 'id-alias', test_field_names2.name FROM test_field_names1 t1, test_field_names2");
 
4736
  myquery(rc);
 
4737
 
 
4738
  result= mysql_use_result(mysql);
 
4739
  mytest(result);
 
4740
 
 
4741
  rc= my_process_result_set(result);
 
4742
  DIE_UNLESS(rc == 0);
 
4743
  mysql_free_result(result);
 
4744
}
 
4745
 
 
4746
 
 
4747
/* Test warnings */
 
4748
 
 
4749
static void test_warnings()
 
4750
{
 
4751
  int        rc;
 
4752
  MYSQL_RES  *result;
 
4753
 
 
4754
  myheader("test_warnings");
 
4755
 
 
4756
  mysql_query(mysql, "DROP TABLE if exists test_non_exists");
 
4757
 
 
4758
  rc= mysql_query(mysql, "DROP TABLE if exists test_non_exists");
 
4759
  myquery(rc);
 
4760
 
 
4761
  if (!opt_silent)
 
4762
    fprintf(stdout, "\n total warnings: %d", mysql_warning_count(mysql));
 
4763
  rc= mysql_query(mysql, "SHOW WARNINGS");
 
4764
  myquery(rc);
 
4765
 
 
4766
  result= mysql_store_result(mysql);
 
4767
  mytest(result);
 
4768
 
 
4769
  rc= my_process_result_set(result);
 
4770
  DIE_UNLESS(rc == 1);
 
4771
  mysql_free_result(result);
 
4772
}
 
4773
 
 
4774
 
 
4775
/* Test errors */
 
4776
 
 
4777
static void test_errors()
 
4778
{
 
4779
  int        rc;
 
4780
  MYSQL_RES  *result;
 
4781
 
 
4782
  myheader("test_errors");
 
4783
 
 
4784
  mysql_query(mysql, "DROP TABLE if exists test_non_exists");
 
4785
 
 
4786
  rc= mysql_query(mysql, "DROP TABLE test_non_exists");
 
4787
  myquery_r(rc);
 
4788
 
 
4789
  rc= mysql_query(mysql, "SHOW ERRORS");
 
4790
  myquery(rc);
 
4791
 
 
4792
  result= mysql_store_result(mysql);
 
4793
  mytest(result);
 
4794
 
 
4795
  (void) my_process_result_set(result);
 
4796
  mysql_free_result(result);
 
4797
}
 
4798
 
 
4799
 
 
4800
/* Test simple prepare-insert */
 
4801
 
 
4802
static void test_insert()
 
4803
{
 
4804
  MYSQL_STMT *stmt;
 
4805
  int        rc;
 
4806
  char       str_data[50];
 
4807
  char       tiny_data;
 
4808
  MYSQL_RES  *result;
 
4809
  MYSQL_BIND my_bind[2];
 
4810
  ulong      length;
 
4811
 
 
4812
  myheader("test_insert");
 
4813
 
 
4814
  rc= mysql_autocommit(mysql, TRUE);
 
4815
  myquery(rc);
 
4816
 
 
4817
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_insert");
 
4818
  myquery(rc);
 
4819
 
 
4820
  rc= mysql_query(mysql, "CREATE TABLE test_prep_insert(col1 tinyint, \
 
4821
                                col2 varchar(50))");
 
4822
  myquery(rc);
 
4823
 
 
4824
  /* insert by prepare */
 
4825
  stmt= mysql_simple_prepare(mysql,
 
4826
                             "INSERT INTO test_prep_insert VALUES(?, ?)");
 
4827
  check_stmt(stmt);
 
4828
 
 
4829
  verify_param_count(stmt, 2);
 
4830
 
 
4831
  /*
 
4832
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
4833
    its members.
 
4834
  */
 
4835
  bzero((char*) my_bind, sizeof(my_bind));
 
4836
 
 
4837
  /* tinyint */
 
4838
  my_bind[0].buffer_type= MYSQL_TYPE_TINY;
 
4839
  my_bind[0].buffer= (void *)&tiny_data;
 
4840
 
 
4841
  /* string */
 
4842
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
4843
  my_bind[1].buffer= str_data;
 
4844
  my_bind[1].buffer_length= sizeof(str_data);;
 
4845
  my_bind[1].length= &length;
 
4846
 
 
4847
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
4848
  check_execute(stmt, rc);
 
4849
 
 
4850
  /* now, execute the prepared statement to insert 10 records.. */
 
4851
  for (tiny_data= 0; tiny_data < 3; tiny_data++)
 
4852
  {
 
4853
    length= my_sprintf(str_data, (str_data, "MySQL%d", tiny_data));
 
4854
    rc= mysql_stmt_execute(stmt);
 
4855
    check_execute(stmt, rc);
 
4856
  }
 
4857
 
 
4858
  mysql_stmt_close(stmt);
 
4859
 
 
4860
  /* now fetch the results ..*/
 
4861
  rc= mysql_commit(mysql);
 
4862
  myquery(rc);
 
4863
 
 
4864
  /* test the results now, only one row should exist */
 
4865
  rc= mysql_query(mysql, "SELECT * FROM test_prep_insert");
 
4866
  myquery(rc);
 
4867
 
 
4868
  /* get the result */
 
4869
  result= mysql_store_result(mysql);
 
4870
  mytest(result);
 
4871
 
 
4872
  rc= my_process_result_set(result);
 
4873
  DIE_UNLESS((int) tiny_data == rc);
 
4874
  mysql_free_result(result);
 
4875
 
 
4876
}
 
4877
 
 
4878
 
 
4879
/* Test simple prepare-resultset info */
 
4880
 
 
4881
static void test_prepare_resultset()
 
4882
{
 
4883
  MYSQL_STMT *stmt;
 
4884
  int        rc;
 
4885
  MYSQL_RES  *result;
 
4886
 
 
4887
  myheader("test_prepare_resultset");
 
4888
 
 
4889
  rc= mysql_autocommit(mysql, TRUE);
 
4890
  myquery(rc);
 
4891
 
 
4892
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prepare_resultset");
 
4893
  myquery(rc);
 
4894
 
 
4895
  rc= mysql_query(mysql, "CREATE TABLE test_prepare_resultset(id int, \
 
4896
                                name varchar(50), extra double)");
 
4897
  myquery(rc);
 
4898
 
 
4899
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_prepare_resultset");
 
4900
  check_stmt(stmt);
 
4901
 
 
4902
  verify_param_count(stmt, 0);
 
4903
 
 
4904
  result= mysql_stmt_result_metadata(stmt);
 
4905
  mytest(result);
 
4906
  my_print_result_metadata(result);
 
4907
  mysql_free_result(result);
 
4908
  mysql_stmt_close(stmt);
 
4909
}
 
4910
 
 
4911
 
 
4912
/* Test field flags (verify .NET provider) */
 
4913
 
 
4914
static void test_field_flags()
 
4915
{
 
4916
  int          rc;
 
4917
  MYSQL_RES    *result;
 
4918
  MYSQL_FIELD  *field;
 
4919
  unsigned int i;
 
4920
 
 
4921
 
 
4922
  myheader("test_field_flags");
 
4923
 
 
4924
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_field_flags");
 
4925
  myquery(rc);
 
4926
 
 
4927
  rc= mysql_query(mysql, "CREATE TABLE test_field_flags(id int NOT NULL AUTO_INCREMENT PRIMARY KEY, \
 
4928
                                                        id1 int NOT NULL, \
 
4929
                                                        id2 int UNIQUE, \
 
4930
                                                        id3 int, \
 
4931
                                                        id4 int NOT NULL, \
 
4932
                                                        id5 int, \
 
4933
                                                        KEY(id3, id4))");
 
4934
  myquery(rc);
 
4935
 
 
4936
  /* with table name included with TRUE column name */
 
4937
  rc= mysql_query(mysql, "SELECT * FROM test_field_flags");
 
4938
  myquery(rc);
 
4939
 
 
4940
  result= mysql_use_result(mysql);
 
4941
  mytest(result);
 
4942
 
 
4943
  mysql_field_seek(result, 0);
 
4944
  if (!opt_silent)
 
4945
    fputc('\n', stdout);
 
4946
 
 
4947
  for(i= 0; i< mysql_num_fields(result); i++)
 
4948
  {
 
4949
    field= mysql_fetch_field(result);
 
4950
    if (!opt_silent)
 
4951
    {
 
4952
      fprintf(stdout, "\n field:%d", i);
 
4953
      if (field->flags & NOT_NULL_FLAG)
 
4954
        fprintf(stdout, "\n  NOT_NULL_FLAG");
 
4955
      if (field->flags & PRI_KEY_FLAG)
 
4956
        fprintf(stdout, "\n  PRI_KEY_FLAG");
 
4957
      if (field->flags & UNIQUE_KEY_FLAG)
 
4958
        fprintf(stdout, "\n  UNIQUE_KEY_FLAG");
 
4959
      if (field->flags & MULTIPLE_KEY_FLAG)
 
4960
        fprintf(stdout, "\n  MULTIPLE_KEY_FLAG");
 
4961
      if (field->flags & AUTO_INCREMENT_FLAG)
 
4962
        fprintf(stdout, "\n  AUTO_INCREMENT_FLAG");
 
4963
 
 
4964
    }
 
4965
  }
 
4966
  mysql_free_result(result);
 
4967
}
 
4968
 
 
4969
 
 
4970
/* Test mysql_stmt_close for open stmts */
 
4971
 
 
4972
static void test_stmt_close()
 
4973
{
 
4974
  MYSQL *lmysql;
 
4975
  MYSQL_STMT *stmt1, *stmt2, *stmt3, *stmt_x;
 
4976
  MYSQL_BIND  my_bind[1];
 
4977
  MYSQL_RES   *result;
 
4978
  unsigned int  count;
 
4979
  int   rc;
 
4980
  char query[MAX_TEST_QUERY_LENGTH];
 
4981
 
 
4982
  myheader("test_stmt_close");
 
4983
 
 
4984
  if (!opt_silent)
 
4985
    fprintf(stdout, "\n Establishing a test connection ...");
 
4986
  if (!(lmysql= mysql_client_init(NULL)))
 
4987
  {
 
4988
    myerror("mysql_client_init() failed");
 
4989
    exit(1);
 
4990
  }
 
4991
  if (!(mysql_real_connect(lmysql, opt_host, opt_user,
 
4992
                           opt_password, current_db, opt_port,
 
4993
                           opt_unix_socket, 0)))
 
4994
  {
 
4995
    myerror("connection failed");
 
4996
    exit(1);
 
4997
  }
 
4998
  lmysql->reconnect= 1;
 
4999
  if (!opt_silent)
 
5000
    fprintf(stdout, "OK");
 
5001
 
 
5002
 
 
5003
  /* set AUTOCOMMIT to ON*/
 
5004
  mysql_autocommit(lmysql, TRUE);
 
5005
 
 
5006
  rc= mysql_query(lmysql, "SET SQL_MODE = ''");
 
5007
  myquery(rc);
 
5008
 
 
5009
  rc= mysql_query(lmysql, "DROP TABLE IF EXISTS test_stmt_close");
 
5010
  myquery(rc);
 
5011
 
 
5012
  rc= mysql_query(lmysql, "CREATE TABLE test_stmt_close(id int)");
 
5013
  myquery(rc);
 
5014
 
 
5015
  strmov(query, "DO \"nothing\"");
 
5016
  stmt1= mysql_simple_prepare(lmysql, query);
 
5017
  check_stmt(stmt1);
 
5018
 
 
5019
  verify_param_count(stmt1, 0);
 
5020
 
 
5021
  strmov(query, "INSERT INTO test_stmt_close(id) VALUES(?)");
 
5022
  stmt_x= mysql_simple_prepare(mysql, query);
 
5023
  check_stmt(stmt_x);
 
5024
 
 
5025
  verify_param_count(stmt_x, 1);
 
5026
 
 
5027
  strmov(query, "UPDATE test_stmt_close SET id= ? WHERE id= ?");
 
5028
  stmt3= mysql_simple_prepare(lmysql, query);
 
5029
  check_stmt(stmt3);
 
5030
 
 
5031
  verify_param_count(stmt3, 2);
 
5032
 
 
5033
  strmov(query, "SELECT * FROM test_stmt_close WHERE id= ?");
 
5034
  stmt2= mysql_simple_prepare(lmysql, query);
 
5035
  check_stmt(stmt2);
 
5036
 
 
5037
  verify_param_count(stmt2, 1);
 
5038
 
 
5039
  rc= mysql_stmt_close(stmt1);
 
5040
  if (!opt_silent)
 
5041
    fprintf(stdout, "\n mysql_close_stmt(1) returned: %d", rc);
 
5042
  DIE_UNLESS(rc == 0);
 
5043
 
 
5044
  /*
 
5045
    Originally we were going to close all statements automatically in
 
5046
    mysql_close(). This proved to not work well - users weren't able to
 
5047
    close statements by hand once mysql_close() had been called.
 
5048
    Now mysql_close() doesn't free any statements, so this test doesn't
 
5049
    serve its original designation any more.
 
5050
    Here we free stmt2 and stmt3 by hand to avoid memory leaks.
 
5051
  */
 
5052
  mysql_stmt_close(stmt2);
 
5053
  mysql_stmt_close(stmt3);
 
5054
  mysql_close(lmysql);
 
5055
 
 
5056
  /*
 
5057
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
5058
    its members.
 
5059
  */
 
5060
  bzero((char*) my_bind, sizeof(my_bind));
 
5061
 
 
5062
  my_bind[0].buffer= (void *)&count;
 
5063
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
5064
  count= 100;
 
5065
 
 
5066
  rc= mysql_stmt_bind_param(stmt_x, my_bind);
 
5067
  check_execute(stmt_x, rc);
 
5068
 
 
5069
  rc= mysql_stmt_execute(stmt_x);
 
5070
  check_execute(stmt_x, rc);
 
5071
 
 
5072
  verify_st_affected_rows(stmt_x, 1);
 
5073
 
 
5074
  rc= mysql_stmt_close(stmt_x);
 
5075
  if (!opt_silent)
 
5076
    fprintf(stdout, "\n mysql_close_stmt(x) returned: %d", rc);
 
5077
  DIE_UNLESS( rc == 0);
 
5078
 
 
5079
  rc= mysql_query(mysql, "SELECT id FROM test_stmt_close");
 
5080
  myquery(rc);
 
5081
 
 
5082
  result= mysql_store_result(mysql);
 
5083
  mytest(result);
 
5084
 
 
5085
  rc= my_process_result_set(result);
 
5086
  DIE_UNLESS(rc == 1);
 
5087
  mysql_free_result(result);
 
5088
}
 
5089
 
 
5090
 
 
5091
/* Test simple set-variable prepare */
 
5092
 
 
5093
static void test_set_variable()
 
5094
{
 
5095
  MYSQL_STMT *stmt, *stmt1;
 
5096
  int        rc;
 
5097
  int        set_count, def_count, get_count;
 
5098
  ulong      length;
 
5099
  char       var[NAME_LEN+1];
 
5100
  MYSQL_BIND set_bind[1], get_bind[2];
 
5101
 
 
5102
  myheader("test_set_variable");
 
5103
 
 
5104
  mysql_autocommit(mysql, TRUE);
 
5105
 
 
5106
  stmt1= mysql_simple_prepare(mysql, "show variables like 'max_error_count'");
 
5107
  check_stmt(stmt1);
 
5108
 
 
5109
  /*
 
5110
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
5111
    its members.
 
5112
  */
 
5113
  bzero((char*) get_bind, sizeof(get_bind));
 
5114
 
 
5115
  get_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
5116
  get_bind[0].buffer= (void *)var;
 
5117
  get_bind[0].length= &length;
 
5118
  get_bind[0].buffer_length= (int)NAME_LEN;
 
5119
  length= NAME_LEN;
 
5120
 
 
5121
  get_bind[1].buffer_type= MYSQL_TYPE_LONG;
 
5122
  get_bind[1].buffer= (void *)&get_count;
 
5123
 
 
5124
  rc= mysql_stmt_execute(stmt1);
 
5125
  check_execute(stmt1, rc);
 
5126
 
 
5127
  rc= mysql_stmt_bind_result(stmt1, get_bind);
 
5128
  check_execute(stmt1, rc);
 
5129
 
 
5130
  rc= mysql_stmt_fetch(stmt1);
 
5131
  check_execute(stmt1, rc);
 
5132
 
 
5133
  if (!opt_silent)
 
5134
    fprintf(stdout, "\n max_error_count(default): %d", get_count);
 
5135
  def_count= get_count;
 
5136
 
 
5137
  DIE_UNLESS(strcmp(var, "max_error_count") == 0);
 
5138
  rc= mysql_stmt_fetch(stmt1);
 
5139
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
5140
 
 
5141
  stmt= mysql_simple_prepare(mysql, "set max_error_count= ?");
 
5142
  check_stmt(stmt);
 
5143
 
 
5144
  bzero((char*) set_bind, sizeof(set_bind));
 
5145
 
 
5146
  set_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
5147
  set_bind[0].buffer= (void *)&set_count;
 
5148
 
 
5149
  rc= mysql_stmt_bind_param(stmt, set_bind);
 
5150
  check_execute(stmt, rc);
 
5151
 
 
5152
  set_count= 31;
 
5153
  rc= mysql_stmt_execute(stmt);
 
5154
  check_execute(stmt, rc);
 
5155
 
 
5156
  mysql_commit(mysql);
 
5157
 
 
5158
  rc= mysql_stmt_execute(stmt1);
 
5159
  check_execute(stmt1, rc);
 
5160
 
 
5161
  rc= mysql_stmt_fetch(stmt1);
 
5162
  check_execute(stmt1, rc);
 
5163
 
 
5164
  if (!opt_silent)
 
5165
    fprintf(stdout, "\n max_error_count         : %d", get_count);
 
5166
  DIE_UNLESS(get_count == set_count);
 
5167
 
 
5168
  rc= mysql_stmt_fetch(stmt1);
 
5169
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
5170
 
 
5171
  /* restore back to default */
 
5172
  set_count= def_count;
 
5173
  rc= mysql_stmt_execute(stmt);
 
5174
  check_execute(stmt, rc);
 
5175
 
 
5176
  rc= mysql_stmt_execute(stmt1);
 
5177
  check_execute(stmt1, rc);
 
5178
 
 
5179
  rc= mysql_stmt_fetch(stmt1);
 
5180
  check_execute(stmt1, rc);
 
5181
 
 
5182
  if (!opt_silent)
 
5183
    fprintf(stdout, "\n max_error_count(default): %d", get_count);
 
5184
  DIE_UNLESS(get_count == set_count);
 
5185
 
 
5186
  rc= mysql_stmt_fetch(stmt1);
 
5187
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
5188
 
 
5189
  mysql_stmt_close(stmt);
 
5190
  mysql_stmt_close(stmt1);
 
5191
}
 
5192
 
 
5193
#if NOT_USED
 
5194
 
 
5195
/* Insert meta info .. */
 
5196
 
 
5197
static void test_insert_meta()
 
5198
{
 
5199
  MYSQL_STMT *stmt;
 
5200
  int        rc;
 
5201
  MYSQL_RES  *result;
 
5202
  MYSQL_FIELD *field;
 
5203
 
 
5204
  myheader("test_insert_meta");
 
5205
 
 
5206
  rc= mysql_autocommit(mysql, TRUE);
 
5207
  myquery(rc);
 
5208
 
 
5209
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_insert");
 
5210
  myquery(rc);
 
5211
 
 
5212
  rc= mysql_query(mysql, "CREATE TABLE test_prep_insert(col1 tinyint, \
 
5213
                                col2 varchar(50), col3 varchar(30))");
 
5214
  myquery(rc);
 
5215
 
 
5216
  strmov(query, "INSERT INTO test_prep_insert VALUES(10, 'venu1', 'test')");
 
5217
  stmt= mysql_simple_prepare(mysql, query);
 
5218
  check_stmt(stmt);
 
5219
 
 
5220
  verify_param_count(stmt, 0);
 
5221
 
 
5222
  result= mysql_param_result(stmt);
 
5223
  mytest_r(result);
 
5224
 
 
5225
  mysql_stmt_close(stmt);
 
5226
 
 
5227
  strmov(query, "INSERT INTO test_prep_insert VALUES(?, 'venu', ?)");
 
5228
  stmt= mysql_simple_prepare(mysql, query);
 
5229
  check_stmt(stmt);
 
5230
 
 
5231
  verify_param_count(stmt, 2);
 
5232
 
 
5233
  result= mysql_param_result(stmt);
 
5234
  mytest(result);
 
5235
 
 
5236
  my_print_result_metadata(result);
 
5237
 
 
5238
  mysql_field_seek(result, 0);
 
5239
  field= mysql_fetch_field(result);
 
5240
  mytest(field);
 
5241
  if (!opt_silent)
 
5242
    fprintf(stdout, "\n obtained: `%s` (expected: `%s`)", field->name, "col1");
 
5243
  DIE_UNLESS(strcmp(field->name, "col1") == 0);
 
5244
 
 
5245
  field= mysql_fetch_field(result);
 
5246
  mytest(field);
 
5247
  if (!opt_silent)
 
5248
    fprintf(stdout, "\n obtained: `%s` (expected: `%s`)", field->name, "col3");
 
5249
  DIE_UNLESS(strcmp(field->name, "col3") == 0);
 
5250
 
 
5251
  field= mysql_fetch_field(result);
 
5252
  mytest_r(field);
 
5253
 
 
5254
  mysql_free_result(result);
 
5255
  mysql_stmt_close(stmt);
 
5256
}
 
5257
 
 
5258
 
 
5259
/* Update meta info .. */
 
5260
 
 
5261
static void test_update_meta()
 
5262
{
 
5263
  MYSQL_STMT *stmt;
 
5264
  int        rc;
 
5265
  MYSQL_RES  *result;
 
5266
  MYSQL_FIELD *field;
 
5267
 
 
5268
  myheader("test_update_meta");
 
5269
 
 
5270
  rc= mysql_autocommit(mysql, TRUE);
 
5271
  myquery(rc);
 
5272
 
 
5273
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_update");
 
5274
  myquery(rc);
 
5275
 
 
5276
  rc= mysql_query(mysql, "CREATE TABLE test_prep_update(col1 tinyint, \
 
5277
                                col2 varchar(50), col3 varchar(30))");
 
5278
  myquery(rc);
 
5279
 
 
5280
  strmov(query, "UPDATE test_prep_update SET col1=10, col2='venu1' WHERE col3='test'");
 
5281
  stmt= mysql_simple_prepare(mysql, query);
 
5282
  check_stmt(stmt);
 
5283
 
 
5284
  verify_param_count(stmt, 0);
 
5285
 
 
5286
  result= mysql_param_result(stmt);
 
5287
  mytest_r(result);
 
5288
 
 
5289
  mysql_stmt_close(stmt);
 
5290
 
 
5291
  strmov(query, "UPDATE test_prep_update SET col1=?, col2='venu' WHERE col3=?");
 
5292
  stmt= mysql_simple_prepare(mysql, query);
 
5293
  check_stmt(stmt);
 
5294
 
 
5295
  verify_param_count(stmt, 2);
 
5296
 
 
5297
  result= mysql_param_result(stmt);
 
5298
  mytest(result);
 
5299
 
 
5300
  my_print_result_metadata(result);
 
5301
 
 
5302
  mysql_field_seek(result, 0);
 
5303
  field= mysql_fetch_field(result);
 
5304
  mytest(field);
 
5305
  if (!opt_silent)
 
5306
  {
 
5307
    fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col1");
 
5308
    fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_update");
 
5309
  }
 
5310
  DIE_UNLESS(strcmp(field->name, "col1") == 0);
 
5311
  DIE_UNLESS(strcmp(field->table, "test_prep_update") == 0);
 
5312
 
 
5313
  field= mysql_fetch_field(result);
 
5314
  mytest(field);
 
5315
  if (!opt_silent)
 
5316
  {
 
5317
    fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col3");
 
5318
    fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_update");
 
5319
  }
 
5320
  DIE_UNLESS(strcmp(field->name, "col3") == 0);
 
5321
  DIE_UNLESS(strcmp(field->table, "test_prep_update") == 0);
 
5322
 
 
5323
  field= mysql_fetch_field(result);
 
5324
  mytest_r(field);
 
5325
 
 
5326
  mysql_free_result(result);
 
5327
  mysql_stmt_close(stmt);
 
5328
}
 
5329
 
 
5330
 
 
5331
/* Select meta info .. */
 
5332
 
 
5333
static void test_select_meta()
 
5334
{
 
5335
  MYSQL_STMT *stmt;
 
5336
  int        rc;
 
5337
  MYSQL_RES  *result;
 
5338
  MYSQL_FIELD *field;
 
5339
 
 
5340
  myheader("test_select_meta");
 
5341
 
 
5342
  rc= mysql_autocommit(mysql, TRUE);
 
5343
  myquery(rc);
 
5344
 
 
5345
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_select");
 
5346
  myquery(rc);
 
5347
 
 
5348
  rc= mysql_query(mysql, "CREATE TABLE test_prep_select(col1 tinyint, \
 
5349
                                col2 varchar(50), col3 varchar(30))");
 
5350
  myquery(rc);
 
5351
 
 
5352
  strmov(query, "SELECT * FROM test_prep_select WHERE col1=10");
 
5353
  stmt= mysql_simple_prepare(mysql, query);
 
5354
  check_stmt(stmt);
 
5355
 
 
5356
  verify_param_count(stmt, 0);
 
5357
 
 
5358
  result= mysql_param_result(stmt);
 
5359
  mytest_r(result);
 
5360
 
 
5361
  strmov(query, "SELECT col1, col3 from test_prep_select WHERE col1=? AND col3='test' AND col2= ?");
 
5362
  stmt= mysql_simple_prepare(mysql, query);
 
5363
  check_stmt(stmt);
 
5364
 
 
5365
  verify_param_count(stmt, 2);
 
5366
 
 
5367
  result= mysql_param_result(stmt);
 
5368
  mytest(result);
 
5369
 
 
5370
  my_print_result_metadata(result);
 
5371
 
 
5372
  mysql_field_seek(result, 0);
 
5373
  field= mysql_fetch_field(result);
 
5374
  mytest(field);
 
5375
  if (!opt_silent)
 
5376
  {
 
5377
    fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col1");
 
5378
    fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_select");
 
5379
  }
 
5380
  DIE_UNLESS(strcmp(field->name, "col1") == 0);
 
5381
  DIE_UNLESS(strcmp(field->table, "test_prep_select") == 0);
 
5382
 
 
5383
  field= mysql_fetch_field(result);
 
5384
  mytest(field);
 
5385
  if (!opt_silent)
 
5386
  {
 
5387
    fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col2");
 
5388
    fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_select");
 
5389
  }
 
5390
  DIE_UNLESS(strcmp(field->name, "col2") == 0);
 
5391
  DIE_UNLESS(strcmp(field->table, "test_prep_select") == 0);
 
5392
 
 
5393
  field= mysql_fetch_field(result);
 
5394
  mytest_r(field);
 
5395
 
 
5396
  mysql_free_result(result);
 
5397
  mysql_stmt_close(stmt);
 
5398
}
 
5399
#endif
 
5400
 
 
5401
 
 
5402
/* Test FUNCTION field info / DATE_FORMAT() table_name . */
 
5403
 
 
5404
static void test_func_fields()
 
5405
{
 
5406
  int        rc;
 
5407
  MYSQL_RES  *result;
 
5408
  MYSQL_FIELD *field;
 
5409
 
 
5410
  myheader("test_func_fields");
 
5411
 
 
5412
  rc= mysql_autocommit(mysql, TRUE);
 
5413
  myquery(rc);
 
5414
 
 
5415
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_dateformat");
 
5416
  myquery(rc);
 
5417
 
 
5418
  rc= mysql_query(mysql, "CREATE TABLE test_dateformat(id int, \
 
5419
                                                       ts timestamp)");
 
5420
  myquery(rc);
 
5421
 
 
5422
  rc= mysql_query(mysql, "INSERT INTO test_dateformat(id) values(10)");
 
5423
  myquery(rc);
 
5424
 
 
5425
  rc= mysql_query(mysql, "SELECT ts FROM test_dateformat");
 
5426
  myquery(rc);
 
5427
 
 
5428
  result= mysql_store_result(mysql);
 
5429
  mytest(result);
 
5430
 
 
5431
  field= mysql_fetch_field(result);
 
5432
  mytest(field);
 
5433
  if (!opt_silent)
 
5434
    fprintf(stdout, "\n table name: `%s` (expected: `%s`)", field->table,
 
5435
            "test_dateformat");
 
5436
  DIE_UNLESS(strcmp(field->table, "test_dateformat") == 0);
 
5437
 
 
5438
  field= mysql_fetch_field(result);
 
5439
  mytest_r(field); /* no more fields */
 
5440
 
 
5441
  mysql_free_result(result);
 
5442
 
 
5443
  /* DATE_FORMAT */
 
5444
  rc= mysql_query(mysql, "SELECT DATE_FORMAT(ts, '%Y') AS 'venu' FROM test_dateformat");
 
5445
  myquery(rc);
 
5446
 
 
5447
  result= mysql_store_result(mysql);
 
5448
  mytest(result);
 
5449
 
 
5450
  field= mysql_fetch_field(result);
 
5451
  mytest(field);
 
5452
  if (!opt_silent)
 
5453
    fprintf(stdout, "\n table name: `%s` (expected: `%s`)", field->table, "");
 
5454
  DIE_UNLESS(field->table[0] == '\0');
 
5455
 
 
5456
  field= mysql_fetch_field(result);
 
5457
  mytest_r(field); /* no more fields */
 
5458
 
 
5459
  mysql_free_result(result);
 
5460
 
 
5461
  /* FIELD ALIAS TEST */
 
5462
  rc= mysql_query(mysql, "SELECT DATE_FORMAT(ts, '%Y')  AS 'YEAR' FROM test_dateformat");
 
5463
  myquery(rc);
 
5464
 
 
5465
  result= mysql_store_result(mysql);
 
5466
  mytest(result);
 
5467
 
 
5468
  field= mysql_fetch_field(result);
 
5469
  mytest(field);
 
5470
  if (!opt_silent)
 
5471
  {
 
5472
    printf("\n field name: `%s` (expected: `%s`)", field->name, "YEAR");
 
5473
    printf("\n field org name: `%s` (expected: `%s`)", field->org_name, "");
 
5474
  }
 
5475
  DIE_UNLESS(strcmp(field->name, "YEAR") == 0);
 
5476
  DIE_UNLESS(field->org_name[0] == '\0');
 
5477
 
 
5478
  field= mysql_fetch_field(result);
 
5479
  mytest_r(field); /* no more fields */
 
5480
 
 
5481
  mysql_free_result(result);
 
5482
}
 
5483
 
 
5484
 
 
5485
/* Multiple stmts .. */
 
5486
 
 
5487
static void test_multi_stmt()
 
5488
{
 
5489
 
 
5490
  MYSQL_STMT  *stmt, *stmt1, *stmt2;
 
5491
  int         rc;
 
5492
  uint32      id;
 
5493
  char        name[50];
 
5494
  MYSQL_BIND  my_bind[2];
 
5495
  ulong       length[2];
 
5496
  my_bool     is_null[2];
 
5497
  myheader("test_multi_stmt");
 
5498
 
 
5499
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_multi_table");
 
5500
  myquery(rc);
 
5501
 
 
5502
  rc= mysql_query(mysql, "CREATE TABLE test_multi_table(id int, name char(20))");
 
5503
  myquery(rc);
 
5504
 
 
5505
  rc= mysql_query(mysql, "INSERT INTO test_multi_table values(10, 'mysql')");
 
5506
  myquery(rc);
 
5507
 
 
5508
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_multi_table "
 
5509
                                    "WHERE id= ?");
 
5510
  check_stmt(stmt);
 
5511
 
 
5512
  stmt2= mysql_simple_prepare(mysql, "UPDATE test_multi_table "
 
5513
                                     "SET name='updated' WHERE id=10");
 
5514
  check_stmt(stmt2);
 
5515
 
 
5516
  verify_param_count(stmt, 1);
 
5517
 
 
5518
  /*
 
5519
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
5520
    its members.
 
5521
  */
 
5522
  bzero((char*) my_bind, sizeof(my_bind));
 
5523
 
 
5524
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
5525
  my_bind[0].buffer= (void *)&id;
 
5526
  my_bind[0].is_null= &is_null[0];
 
5527
  my_bind[0].length= &length[0];
 
5528
  is_null[0]= 0;
 
5529
  length[0]= 0;
 
5530
 
 
5531
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
5532
  my_bind[1].buffer= (void *)name;
 
5533
  my_bind[1].buffer_length= sizeof(name);
 
5534
  my_bind[1].length= &length[1];
 
5535
  my_bind[1].is_null= &is_null[1];
 
5536
 
 
5537
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
5538
  check_execute(stmt, rc);
 
5539
 
 
5540
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
5541
  check_execute(stmt, rc);
 
5542
 
 
5543
  id= 10;
 
5544
  rc= mysql_stmt_execute(stmt);
 
5545
  check_execute(stmt, rc);
 
5546
 
 
5547
  id= 999;
 
5548
  rc= mysql_stmt_fetch(stmt);
 
5549
  check_execute(stmt, rc);
 
5550
 
 
5551
  if (!opt_silent)
 
5552
  {
 
5553
    fprintf(stdout, "\n int_data: %lu(%lu)", (ulong) id, length[0]);
 
5554
    fprintf(stdout, "\n str_data: %s(%lu)", name, length[1]);
 
5555
  }
 
5556
  DIE_UNLESS(id == 10);
 
5557
  DIE_UNLESS(strcmp(name, "mysql") == 0);
 
5558
 
 
5559
  rc= mysql_stmt_fetch(stmt);
 
5560
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
5561
 
 
5562
  /* alter the table schema now */
 
5563
  stmt1= mysql_simple_prepare(mysql, "DELETE FROM test_multi_table "
 
5564
                                     "WHERE id= ? AND "
 
5565
                                     "CONVERT(name USING utf8)=?");
 
5566
  check_stmt(stmt1);
 
5567
 
 
5568
  verify_param_count(stmt1, 2);
 
5569
 
 
5570
  rc= mysql_stmt_bind_param(stmt1, my_bind);
 
5571
  check_execute(stmt1, rc);
 
5572
 
 
5573
  rc= mysql_stmt_execute(stmt2);
 
5574
  check_execute(stmt2, rc);
 
5575
 
 
5576
  verify_st_affected_rows(stmt2, 1);
 
5577
 
 
5578
  rc= mysql_stmt_execute(stmt);
 
5579
  check_execute(stmt, rc);
 
5580
 
 
5581
  rc= mysql_stmt_fetch(stmt);
 
5582
  check_execute(stmt, rc);
 
5583
 
 
5584
  if (!opt_silent)
 
5585
  {
 
5586
    fprintf(stdout, "\n int_data: %lu(%lu)", (ulong) id, length[0]);
 
5587
    fprintf(stdout, "\n str_data: %s(%lu)", name, length[1]);
 
5588
  }
 
5589
  DIE_UNLESS(id == 10);
 
5590
  DIE_UNLESS(strcmp(name, "updated") == 0);
 
5591
 
 
5592
  rc= mysql_stmt_fetch(stmt);
 
5593
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
5594
 
 
5595
  rc= mysql_stmt_execute(stmt1);
 
5596
  check_execute(stmt1, rc);
 
5597
 
 
5598
  verify_st_affected_rows(stmt1, 1);
 
5599
 
 
5600
  mysql_stmt_close(stmt1);
 
5601
 
 
5602
  rc= mysql_stmt_execute(stmt);
 
5603
  check_execute(stmt, rc);
 
5604
 
 
5605
  rc= mysql_stmt_fetch(stmt);
 
5606
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
5607
 
 
5608
  rc= my_stmt_result("SELECT * FROM test_multi_table");
 
5609
  DIE_UNLESS(rc == 0);
 
5610
 
 
5611
  mysql_stmt_close(stmt);
 
5612
  mysql_stmt_close(stmt2);
 
5613
 
 
5614
}
 
5615
 
 
5616
 
 
5617
/* Test simple sample - manual */
 
5618
 
 
5619
static void test_manual_sample()
 
5620
{
 
5621
  unsigned int param_count;
 
5622
  MYSQL_STMT   *stmt;
 
5623
  short        small_data;
 
5624
  int          int_data;
 
5625
  int          rc;
 
5626
  char         str_data[50];
 
5627
  ulonglong    affected_rows;
 
5628
  MYSQL_BIND   my_bind[3];
 
5629
  my_bool      is_null;
 
5630
  char query[MAX_TEST_QUERY_LENGTH];
 
5631
 
 
5632
  myheader("test_manual_sample");
 
5633
 
 
5634
  /*
 
5635
    Sample which is incorporated directly in the manual under Prepared
 
5636
    statements section (Example from mysql_stmt_execute()
 
5637
  */
 
5638
 
 
5639
  mysql_autocommit(mysql, 1);
 
5640
  if (mysql_query(mysql, "DROP TABLE IF EXISTS test_table"))
 
5641
  {
 
5642
    fprintf(stderr, "\n drop table failed");
 
5643
    fprintf(stderr, "\n %s", mysql_error(mysql));
 
5644
    exit(1);
 
5645
  }
 
5646
  if (mysql_query(mysql, "CREATE TABLE test_table(col1 int, col2 varchar(50), \
 
5647
                                                 col3 smallint, \
 
5648
                                                 col4 timestamp(14))"))
 
5649
  {
 
5650
    fprintf(stderr, "\n create table failed");
 
5651
    fprintf(stderr, "\n %s", mysql_error(mysql));
 
5652
    exit(1);
 
5653
  }
 
5654
 
 
5655
  /* Prepare a insert query with 3 parameters */
 
5656
  strmov(query, "INSERT INTO test_table(col1, col2, col3) values(?, ?, ?)");
 
5657
  if (!(stmt= mysql_simple_prepare(mysql, query)))
 
5658
  {
 
5659
    fprintf(stderr, "\n prepare, insert failed");
 
5660
    fprintf(stderr, "\n %s", mysql_error(mysql));
 
5661
    exit(1);
 
5662
  }
 
5663
  if (!opt_silent)
 
5664
    fprintf(stdout, "\n prepare, insert successful");
 
5665
 
 
5666
  /* Get the parameter count from the statement */
 
5667
  param_count= mysql_stmt_param_count(stmt);
 
5668
 
 
5669
  if (!opt_silent)
 
5670
    fprintf(stdout, "\n total parameters in insert: %d", param_count);
 
5671
  if (param_count != 3) /* validate parameter count */
 
5672
  {
 
5673
    fprintf(stderr, "\n invalid parameter count returned by MySQL");
 
5674
    exit(1);
 
5675
  }
 
5676
 
 
5677
  /* Bind the data for the parameters */
 
5678
 
 
5679
  /*
 
5680
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
5681
    its members.
 
5682
  */
 
5683
  bzero((char*) my_bind, sizeof(my_bind));
 
5684
 
 
5685
  /* INTEGER PART */
 
5686
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
5687
  my_bind[0].buffer= (void *)&int_data;
 
5688
 
 
5689
  /* STRING PART */
 
5690
  my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
 
5691
  my_bind[1].buffer= (void *)str_data;
 
5692
  my_bind[1].buffer_length= sizeof(str_data);
 
5693
 
 
5694
  /* SMALLINT PART */
 
5695
  my_bind[2].buffer_type= MYSQL_TYPE_SHORT;
 
5696
  my_bind[2].buffer= (void *)&small_data;
 
5697
  my_bind[2].is_null= &is_null;
 
5698
  is_null= 0;
 
5699
 
 
5700
  /* Bind the buffers */
 
5701
  if (mysql_stmt_bind_param(stmt, my_bind))
 
5702
  {
 
5703
    fprintf(stderr, "\n param bind failed");
 
5704
    fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
 
5705
    exit(1);
 
5706
  }
 
5707
 
 
5708
  /* Specify the data */
 
5709
  int_data= 10;             /* integer */
 
5710
  strmov(str_data, "MySQL"); /* string  */
 
5711
 
 
5712
  /* INSERT SMALLINT data as NULL */
 
5713
  is_null= 1;
 
5714
 
 
5715
  /* Execute the insert statement - 1*/
 
5716
  if (mysql_stmt_execute(stmt))
 
5717
  {
 
5718
    fprintf(stderr, "\n execute 1 failed");
 
5719
    fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
 
5720
    exit(1);
 
5721
  }
 
5722
 
 
5723
  /* Get the total rows affected */
 
5724
  affected_rows= mysql_stmt_affected_rows(stmt);
 
5725
 
 
5726
  if (!opt_silent)
 
5727
    fprintf(stdout, "\n total affected rows: %ld", (ulong) affected_rows);
 
5728
  if (affected_rows != 1) /* validate affected rows */
 
5729
  {
 
5730
    fprintf(stderr, "\n invalid affected rows by MySQL");
 
5731
    exit(1);
 
5732
  }
 
5733
 
 
5734
  /* Re-execute the insert, by changing the values */
 
5735
  int_data= 1000;
 
5736
  strmov(str_data, "The most popular open source database");
 
5737
  small_data= 1000;         /* smallint */
 
5738
  is_null= 0;               /* reset */
 
5739
 
 
5740
  /* Execute the insert statement - 2*/
 
5741
  if (mysql_stmt_execute(stmt))
 
5742
  {
 
5743
    fprintf(stderr, "\n execute 2 failed");
 
5744
    fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
 
5745
    exit(1);
 
5746
  }
 
5747
 
 
5748
  /* Get the total rows affected */
 
5749
  affected_rows= mysql_stmt_affected_rows(stmt);
 
5750
 
 
5751
  if (!opt_silent)
 
5752
    fprintf(stdout, "\n total affected rows: %ld", (ulong) affected_rows);
 
5753
  if (affected_rows != 1) /* validate affected rows */
 
5754
  {
 
5755
    fprintf(stderr, "\n invalid affected rows by MySQL");
 
5756
    exit(1);
 
5757
  }
 
5758
 
 
5759
  /* Close the statement */
 
5760
  if (mysql_stmt_close(stmt))
 
5761
  {
 
5762
    fprintf(stderr, "\n failed while closing the statement");
 
5763
    fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
 
5764
    exit(1);
 
5765
  }
 
5766
  rc= my_stmt_result("SELECT * FROM test_table");
 
5767
  DIE_UNLESS(rc == 2);
 
5768
 
 
5769
  /* DROP THE TABLE */
 
5770
  if (mysql_query(mysql, "DROP TABLE test_table"))
 
5771
  {
 
5772
    fprintf(stderr, "\n drop table failed");
 
5773
    fprintf(stderr, "\n %s", mysql_error(mysql));
 
5774
    exit(1);
 
5775
  }
 
5776
  if (!opt_silent)
 
5777
    fprintf(stdout, "Success !!!");
 
5778
}
 
5779
 
 
5780
 
 
5781
/* Test alter table scenario in the middle of prepare */
 
5782
 
 
5783
static void test_prepare_alter()
 
5784
{
 
5785
  MYSQL_STMT  *stmt;
 
5786
  int         rc, id;
 
5787
  MYSQL_BIND  my_bind[1];
 
5788
  my_bool     is_null;
 
5789
 
 
5790
  myheader("test_prepare_alter");
 
5791
 
 
5792
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_prep_alter");
 
5793
  myquery(rc);
 
5794
 
 
5795
  rc= mysql_query(mysql, "CREATE TABLE test_prep_alter(id int, name char(20))");
 
5796
  myquery(rc);
 
5797
 
 
5798
  rc= mysql_query(mysql, "INSERT INTO test_prep_alter values(10, 'venu'), (20, 'mysql')");
 
5799
  myquery(rc);
 
5800
 
 
5801
  stmt= mysql_simple_prepare(mysql, "INSERT INTO test_prep_alter VALUES(?, 'monty')");
 
5802
  check_stmt(stmt);
 
5803
 
 
5804
  verify_param_count(stmt, 1);
 
5805
 
 
5806
  /*
 
5807
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
5808
    its members.
 
5809
  */
 
5810
  bzero((char*) my_bind, sizeof(my_bind));
 
5811
 
 
5812
  is_null= 0;
 
5813
  my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
 
5814
  my_bind[0].buffer= (void *)&id;
 
5815
  my_bind[0].is_null= &is_null;
 
5816
 
 
5817
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
5818
  check_execute(stmt, rc);
 
5819
 
 
5820
  id= 30;
 
5821
  rc= mysql_stmt_execute(stmt);
 
5822
  check_execute(stmt, rc);
 
5823
 
 
5824
  if (thread_query((char *)"ALTER TABLE test_prep_alter change id id_new varchar(20)"))
 
5825
    exit(1);
 
5826
 
 
5827
  is_null= 1;
 
5828
  rc= mysql_stmt_execute(stmt);
 
5829
  check_execute(stmt, rc);
 
5830
 
 
5831
  rc= my_stmt_result("SELECT * FROM test_prep_alter");
 
5832
  DIE_UNLESS(rc == 4);
 
5833
 
 
5834
  mysql_stmt_close(stmt);
 
5835
}
 
5836
 
 
5837
 
 
5838
/* Test the support of multi-statement executions */
 
5839
 
 
5840
static void test_multi_statements()
 
5841
{
 
5842
  MYSQL *mysql_local;
 
5843
  MYSQL_RES *result;
 
5844
  int    rc;
 
5845
 
 
5846
  const char *query= "\
 
5847
DROP TABLE IF EXISTS test_multi_tab;\
 
5848
CREATE TABLE test_multi_tab(id int, name char(20));\
 
5849
INSERT INTO test_multi_tab(id) VALUES(10), (20);\
 
5850
INSERT INTO test_multi_tab VALUES(20, 'insert;comma');\
 
5851
SELECT * FROM test_multi_tab;\
 
5852
UPDATE test_multi_tab SET name='new;name' WHERE id=20;\
 
5853
DELETE FROM test_multi_tab WHERE name='new;name';\
 
5854
SELECT * FROM test_multi_tab;\
 
5855
DELETE FROM test_multi_tab WHERE id=10;\
 
5856
SELECT * FROM test_multi_tab;\
 
5857
DROP TABLE test_multi_tab;\
 
5858
select 1;\
 
5859
DROP TABLE IF EXISTS test_multi_tab";
 
5860
  uint count, exp_value;
 
5861
  uint rows[]= {0, 0, 2, 1, 3, 2, 2, 1, 1, 0, 0, 1, 0};
 
5862
 
 
5863
  myheader("test_multi_statements");
 
5864
 
 
5865
  /*
 
5866
    First test that we get an error for multi statements
 
5867
    (Because default connection is not opened with CLIENT_MULTI_STATEMENTS)
 
5868
  */
 
5869
  rc= mysql_query(mysql, query); /* syntax error */
 
5870
  myquery_r(rc);
 
5871
 
 
5872
  rc= mysql_next_result(mysql);
 
5873
  DIE_UNLESS(rc == -1);
 
5874
  rc= mysql_more_results(mysql);
 
5875
  DIE_UNLESS(rc == 0);
 
5876
 
 
5877
  if (!(mysql_local= mysql_client_init(NULL)))
 
5878
  {
 
5879
    fprintf(stdout, "\n mysql_client_init() failed");
 
5880
    exit(1);
 
5881
  }
 
5882
 
 
5883
  /* Create connection that supports multi statements */
 
5884
  if (!(mysql_real_connect(mysql_local, opt_host, opt_user,
 
5885
                           opt_password, current_db, opt_port,
 
5886
                           opt_unix_socket, CLIENT_MULTI_STATEMENTS)))
 
5887
  {
 
5888
    fprintf(stdout, "\n connection failed(%s)", mysql_error(mysql_local));
 
5889
    exit(1);
 
5890
  }
 
5891
  mysql_local->reconnect= 1;
 
5892
 
 
5893
  rc= mysql_query(mysql_local, query);
 
5894
  myquery(rc);
 
5895
 
 
5896
  for (count= 0 ; count < array_elements(rows) ; count++)
 
5897
  {
 
5898
    if (!opt_silent)
 
5899
      fprintf(stdout, "\n Query %d: ", count);
 
5900
    if ((result= mysql_store_result(mysql_local)))
 
5901
    {
 
5902
      (void) my_process_result_set(result);
 
5903
      mysql_free_result(result);
 
5904
    }
 
5905
    else if (!opt_silent)
 
5906
      fprintf(stdout, "OK, %ld row(s) affected, %ld warning(s)\n",
 
5907
              (ulong) mysql_affected_rows(mysql_local),
 
5908
              (ulong) mysql_warning_count(mysql_local));
 
5909
 
 
5910
    exp_value= (uint) mysql_affected_rows(mysql_local);
 
5911
    if (rows[count] !=  exp_value)
 
5912
    {
 
5913
      fprintf(stderr, "row %d  had affected rows: %d, should be %d\n",
 
5914
              count, exp_value, rows[count]);
 
5915
      exit(1);
 
5916
    }
 
5917
    if (count != array_elements(rows) -1)
 
5918
    {
 
5919
      if (!(rc= mysql_more_results(mysql_local)))
 
5920
      {
 
5921
        fprintf(stdout,
 
5922
                "mysql_more_result returned wrong value: %d for row %d\n",
 
5923
                rc, count);
 
5924
        exit(1);
 
5925
      }
 
5926
      if ((rc= mysql_next_result(mysql_local)))
 
5927
      {
 
5928
        exp_value= mysql_errno(mysql_local);
 
5929
 
 
5930
        exit(1);
 
5931
      }
 
5932
    }
 
5933
    else
 
5934
    {
 
5935
      rc= mysql_more_results(mysql_local);
 
5936
      DIE_UNLESS(rc == 0);
 
5937
      rc= mysql_next_result(mysql_local);
 
5938
      DIE_UNLESS(rc == -1);
 
5939
    }
 
5940
  }
 
5941
 
 
5942
  /* check that errors abort multi statements */
 
5943
 
 
5944
  rc= mysql_query(mysql_local, "select 1+1+a;select 1+1");
 
5945
  myquery_r(rc);
 
5946
  rc= mysql_more_results(mysql_local);
 
5947
  DIE_UNLESS(rc == 0);
 
5948
  rc= mysql_next_result(mysql_local);
 
5949
  DIE_UNLESS(rc == -1);
 
5950
 
 
5951
  rc= mysql_query(mysql_local, "select 1+1;select 1+1+a;select 1");
 
5952
  myquery(rc);
 
5953
  result= mysql_store_result(mysql_local);
 
5954
  mytest(result);
 
5955
  mysql_free_result(result);
 
5956
  rc= mysql_more_results(mysql_local);
 
5957
  DIE_UNLESS(rc == 1);
 
5958
  rc= mysql_next_result(mysql_local);
 
5959
  DIE_UNLESS(rc > 0);
 
5960
 
 
5961
  /*
 
5962
    Ensure that we can now do a simple query (this checks that the server is
 
5963
    not trying to send us the results for the last 'select 1'
 
5964
  */
 
5965
  rc= mysql_query(mysql_local, "select 1+1+1");
 
5966
  myquery(rc);
 
5967
  result= mysql_store_result(mysql_local);
 
5968
  mytest(result);
 
5969
  (void) my_process_result_set(result);
 
5970
  mysql_free_result(result);
 
5971
 
 
5972
  /*
 
5973
    Check if errors in one of the queries handled properly.
 
5974
  */
 
5975
  rc= mysql_query(mysql_local, "select 1; select * from not_existing_table");
 
5976
  myquery(rc);
 
5977
  result= mysql_store_result(mysql_local);
 
5978
  mysql_free_result(result);
 
5979
 
 
5980
  rc= mysql_next_result(mysql_local);
 
5981
  DIE_UNLESS(rc > 0);
 
5982
 
 
5983
  rc= mysql_next_result(mysql_local);
 
5984
  DIE_UNLESS(rc < 0);
 
5985
 
 
5986
  mysql_close(mysql_local);
 
5987
}
 
5988
 
 
5989
 
 
5990
/*
 
5991
  Check that Prepared statement cannot contain several
 
5992
  SQL statements
 
5993
*/
 
5994
 
 
5995
static void test_prepare_multi_statements()
 
5996
{
 
5997
  MYSQL *mysql_local;
 
5998
  MYSQL_STMT *stmt;
 
5999
  char query[MAX_TEST_QUERY_LENGTH];
 
6000
  myheader("test_prepare_multi_statements");
 
6001
 
 
6002
  if (!(mysql_local= mysql_client_init(NULL)))
 
6003
  {
 
6004
    fprintf(stderr, "\n mysql_client_init() failed");
 
6005
    exit(1);
 
6006
  }
 
6007
 
 
6008
  if (!(mysql_real_connect(mysql_local, opt_host, opt_user,
 
6009
                           opt_password, current_db, opt_port,
 
6010
                           opt_unix_socket, CLIENT_MULTI_STATEMENTS)))
 
6011
  {
 
6012
    fprintf(stderr, "\n connection failed(%s)", mysql_error(mysql_local));
 
6013
    exit(1);
 
6014
  }
 
6015
  mysql_local->reconnect= 1;
 
6016
  strmov(query, "select 1; select 'another value'");
 
6017
  stmt= mysql_simple_prepare(mysql_local, query);
 
6018
  check_stmt_r(stmt);
 
6019
  mysql_close(mysql_local);
 
6020
}
 
6021
 
 
6022
 
 
6023
/* Test simple bind store result */
 
6024
 
 
6025
static void test_store_result()
 
6026
{
 
6027
  MYSQL_STMT *stmt;
 
6028
  int        rc;
 
6029
  int32      nData;
 
6030
  char       szData[100];
 
6031
  MYSQL_BIND my_bind[2];
 
6032
  ulong      length, length1;
 
6033
  my_bool    is_null[2];
 
6034
 
 
6035
  myheader("test_store_result");
 
6036
 
 
6037
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
 
6038
  myquery(rc);
 
6039
 
 
6040
  rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
 
6041
  myquery(rc);
 
6042
 
 
6043
  rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
 
6044
  myquery(rc);
 
6045
 
 
6046
  rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
 
6047
  myquery(rc);
 
6048
 
 
6049
  rc= mysql_commit(mysql);
 
6050
  myquery(rc);
 
6051
 
 
6052
  /* fetch */
 
6053
  bzero((char*) my_bind, sizeof(my_bind));
 
6054
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
6055
  my_bind[0].buffer= (void *) &nData;       /* integer data */
 
6056
  my_bind[0].length= &length;
 
6057
  my_bind[0].is_null= &is_null[0];
 
6058
 
 
6059
  length= 0;
 
6060
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
6061
  my_bind[1].buffer= szData;                /* string data */
 
6062
  my_bind[1].buffer_length= sizeof(szData);
 
6063
  my_bind[1].length= &length1;
 
6064
  my_bind[1].is_null= &is_null[1];
 
6065
  length1= 0;
 
6066
 
 
6067
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_store_result");
 
6068
  check_stmt(stmt);
 
6069
 
 
6070
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
6071
  check_execute(stmt, rc);
 
6072
 
 
6073
  rc= mysql_stmt_execute(stmt);
 
6074
  check_execute(stmt, rc);
 
6075
 
 
6076
  rc= mysql_stmt_store_result(stmt);
 
6077
  check_execute(stmt, rc);
 
6078
 
 
6079
  rc= mysql_stmt_fetch(stmt);
 
6080
  check_execute(stmt, rc);
 
6081
 
 
6082
  if (!opt_silent)
 
6083
    fprintf(stdout, "\n row 1: %ld, %s(%lu)", (long) nData, szData, length1);
 
6084
  DIE_UNLESS(nData == 10);
 
6085
  DIE_UNLESS(strcmp(szData, "venu") == 0);
 
6086
  DIE_UNLESS(length1 == 4);
 
6087
 
 
6088
  rc= mysql_stmt_fetch(stmt);
 
6089
  check_execute(stmt, rc);
 
6090
 
 
6091
  if (!opt_silent)
 
6092
    fprintf(stdout, "\n row 2: %ld, %s(%lu)", (long) nData, szData, length1);
 
6093
  DIE_UNLESS(nData == 20);
 
6094
  DIE_UNLESS(strcmp(szData, "mysql") == 0);
 
6095
  DIE_UNLESS(length1 == 5);
 
6096
 
 
6097
  length= 99;
 
6098
  rc= mysql_stmt_fetch(stmt);
 
6099
  check_execute(stmt, rc);
 
6100
 
 
6101
  if (!opt_silent && is_null[0])
 
6102
    fprintf(stdout, "\n row 3: NULL, %s(%lu)", szData, length1);
 
6103
  DIE_UNLESS(is_null[0]);
 
6104
  DIE_UNLESS(strcmp(szData, "monty") == 0);
 
6105
  DIE_UNLESS(length1 == 5);
 
6106
 
 
6107
  rc= mysql_stmt_fetch(stmt);
 
6108
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
6109
 
 
6110
  rc= mysql_stmt_execute(stmt);
 
6111
  check_execute(stmt, rc);
 
6112
 
 
6113
  rc= mysql_stmt_store_result(stmt);
 
6114
  check_execute(stmt, rc);
 
6115
 
 
6116
  rc= mysql_stmt_fetch(stmt);
 
6117
  check_execute(stmt, rc);
 
6118
 
 
6119
  if (!opt_silent)
 
6120
    fprintf(stdout, "\n row 1: %ld, %s(%lu)", (long) nData, szData, length1);
 
6121
  DIE_UNLESS(nData == 10);
 
6122
  DIE_UNLESS(strcmp(szData, "venu") == 0);
 
6123
  DIE_UNLESS(length1 == 4);
 
6124
 
 
6125
  rc= mysql_stmt_fetch(stmt);
 
6126
  check_execute(stmt, rc);
 
6127
 
 
6128
  if (!opt_silent)
 
6129
    fprintf(stdout, "\n row 2: %ld, %s(%lu)", (long) nData, szData, length1);
 
6130
  DIE_UNLESS(nData == 20);
 
6131
  DIE_UNLESS(strcmp(szData, "mysql") == 0);
 
6132
  DIE_UNLESS(length1 == 5);
 
6133
 
 
6134
  length= 99;
 
6135
  rc= mysql_stmt_fetch(stmt);
 
6136
  check_execute(stmt, rc);
 
6137
 
 
6138
  if (!opt_silent && is_null[0])
 
6139
    fprintf(stdout, "\n row 3: NULL, %s(%lu)", szData, length1);
 
6140
  DIE_UNLESS(is_null[0]);
 
6141
  DIE_UNLESS(strcmp(szData, "monty") == 0);
 
6142
  DIE_UNLESS(length1 == 5);
 
6143
 
 
6144
  rc= mysql_stmt_fetch(stmt);
 
6145
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
6146
 
 
6147
  mysql_stmt_close(stmt);
 
6148
}
 
6149
 
 
6150
 
 
6151
/* Test simple bind store result */
 
6152
 
 
6153
static void test_store_result1()
 
6154
{
 
6155
  MYSQL_STMT *stmt;
 
6156
  int        rc;
 
6157
 
 
6158
  myheader("test_store_result1");
 
6159
 
 
6160
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
 
6161
  myquery(rc);
 
6162
 
 
6163
  rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
 
6164
  myquery(rc);
 
6165
 
 
6166
  rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
 
6167
  myquery(rc);
 
6168
 
 
6169
  rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
 
6170
  myquery(rc);
 
6171
 
 
6172
  rc= mysql_commit(mysql);
 
6173
  myquery(rc);
 
6174
 
 
6175
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_store_result");
 
6176
  check_stmt(stmt);
 
6177
 
 
6178
  rc= mysql_stmt_execute(stmt);
 
6179
  check_execute(stmt, rc);
 
6180
 
 
6181
  rc= mysql_stmt_store_result(stmt);
 
6182
  check_execute(stmt, rc);
 
6183
 
 
6184
  rc= 0;
 
6185
  while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
 
6186
    rc++;
 
6187
  if (!opt_silent)
 
6188
    fprintf(stdout, "\n total rows: %d", rc);
 
6189
  DIE_UNLESS(rc == 3);
 
6190
 
 
6191
  rc= mysql_stmt_execute(stmt);
 
6192
  check_execute(stmt, rc);
 
6193
 
 
6194
  rc= mysql_stmt_store_result(stmt);
 
6195
  check_execute(stmt, rc);
 
6196
 
 
6197
  rc= 0;
 
6198
  while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
 
6199
    rc++;
 
6200
  if (!opt_silent)
 
6201
    fprintf(stdout, "\n total rows: %d", rc);
 
6202
  DIE_UNLESS(rc == 3);
 
6203
 
 
6204
  mysql_stmt_close(stmt);
 
6205
}
 
6206
 
 
6207
 
 
6208
/* Another test for bind and store result */
 
6209
 
 
6210
static void test_store_result2()
 
6211
{
 
6212
  MYSQL_STMT *stmt;
 
6213
  int        rc;
 
6214
  int        nData;
 
6215
  ulong      length;
 
6216
  MYSQL_BIND my_bind[1];
 
6217
  char query[MAX_TEST_QUERY_LENGTH];
 
6218
 
 
6219
  myheader("test_store_result2");
 
6220
 
 
6221
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_store_result");
 
6222
  myquery(rc);
 
6223
 
 
6224
  rc= mysql_query(mysql, "CREATE TABLE test_store_result(col1 int , col2 varchar(50))");
 
6225
  myquery(rc);
 
6226
 
 
6227
  rc= mysql_query(mysql, "INSERT INTO test_store_result VALUES(10, 'venu'), (20, 'mysql')");
 
6228
  myquery(rc);
 
6229
 
 
6230
  rc= mysql_query(mysql, "INSERT INTO test_store_result(col2) VALUES('monty')");
 
6231
  myquery(rc);
 
6232
 
 
6233
  rc= mysql_commit(mysql);
 
6234
  myquery(rc);
 
6235
 
 
6236
  /*
 
6237
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
6238
    its members.
 
6239
  */
 
6240
  bzero((char*) my_bind, sizeof(my_bind));
 
6241
 
 
6242
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
6243
  my_bind[0].buffer= (void *) &nData;      /* integer data */
 
6244
  my_bind[0].length= &length;
 
6245
  my_bind[0].is_null= 0;
 
6246
 
 
6247
  strmov((char *)query , "SELECT col1 FROM test_store_result where col1= ?");
 
6248
  stmt= mysql_simple_prepare(mysql, query);
 
6249
  check_stmt(stmt);
 
6250
 
 
6251
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
6252
  check_execute(stmt, rc);
 
6253
 
 
6254
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
6255
  check_execute(stmt, rc);
 
6256
 
 
6257
  nData= 10; length= 0;
 
6258
  rc= mysql_stmt_execute(stmt);
 
6259
  check_execute(stmt, rc);
 
6260
 
 
6261
  nData= 0;
 
6262
  rc= mysql_stmt_store_result(stmt);
 
6263
  check_execute(stmt, rc);
 
6264
 
 
6265
  rc= mysql_stmt_fetch(stmt);
 
6266
  check_execute(stmt, rc);
 
6267
 
 
6268
  if (!opt_silent)
 
6269
    fprintf(stdout, "\n row 1: %d", nData);
 
6270
  DIE_UNLESS(nData == 10);
 
6271
 
 
6272
  rc= mysql_stmt_fetch(stmt);
 
6273
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
6274
 
 
6275
  nData= 20;
 
6276
  rc= mysql_stmt_execute(stmt);
 
6277
  check_execute(stmt, rc);
 
6278
 
 
6279
  nData= 0;
 
6280
  rc= mysql_stmt_store_result(stmt);
 
6281
  check_execute(stmt, rc);
 
6282
 
 
6283
  rc= mysql_stmt_fetch(stmt);
 
6284
  check_execute(stmt, rc);
 
6285
 
 
6286
  if (!opt_silent)
 
6287
    fprintf(stdout, "\n row 1: %d", nData);
 
6288
  DIE_UNLESS(nData == 20);
 
6289
 
 
6290
  rc= mysql_stmt_fetch(stmt);
 
6291
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
6292
  mysql_stmt_close(stmt);
 
6293
}
 
6294
 
 
6295
 
 
6296
/* Test simple subselect prepare */
 
6297
 
 
6298
static void test_subselect()
 
6299
{
 
6300
 
 
6301
  MYSQL_STMT *stmt;
 
6302
  int        rc, id;
 
6303
  MYSQL_BIND my_bind[1];
 
6304
  DBUG_ENTER("test_subselect");
 
6305
 
 
6306
  myheader("test_subselect");
 
6307
 
 
6308
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_sub1");
 
6309
  myquery(rc);
 
6310
 
 
6311
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_sub2");
 
6312
  myquery(rc);
 
6313
 
 
6314
  rc= mysql_query(mysql, "CREATE TABLE test_sub1(id int)");
 
6315
  myquery(rc);
 
6316
 
 
6317
  rc= mysql_query(mysql, "CREATE TABLE test_sub2(id int, id1 int)");
 
6318
  myquery(rc);
 
6319
 
 
6320
  rc= mysql_query(mysql, "INSERT INTO test_sub1 values(2)");
 
6321
  myquery(rc);
 
6322
 
 
6323
  rc= mysql_query(mysql, "INSERT INTO test_sub2 VALUES(1, 7), (2, 7)");
 
6324
  myquery(rc);
 
6325
 
 
6326
  rc= mysql_commit(mysql);
 
6327
  myquery(rc);
 
6328
 
 
6329
  /* fetch */
 
6330
  /*
 
6331
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
6332
    its members.
 
6333
  */
 
6334
  bzero((char*) my_bind, sizeof(my_bind));
 
6335
 
 
6336
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
6337
  my_bind[0].buffer= (void *) &id;
 
6338
  my_bind[0].length= 0;
 
6339
  my_bind[0].is_null= 0;
 
6340
 
 
6341
  stmt= mysql_simple_prepare(mysql, "INSERT INTO test_sub2(id) SELECT * FROM test_sub1 WHERE id= ?");
 
6342
  check_stmt(stmt);
 
6343
 
 
6344
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
6345
  check_execute(stmt, rc);
 
6346
 
 
6347
  id= 2;
 
6348
  rc= mysql_stmt_execute(stmt);
 
6349
  check_execute(stmt, rc);
 
6350
 
 
6351
  verify_st_affected_rows(stmt, 1);
 
6352
 
 
6353
  id= 9;
 
6354
  rc= mysql_stmt_execute(stmt);
 
6355
  check_execute(stmt, rc);
 
6356
 
 
6357
  verify_st_affected_rows(stmt, 0);
 
6358
 
 
6359
  mysql_stmt_close(stmt);
 
6360
 
 
6361
  rc= my_stmt_result("SELECT * FROM test_sub2");
 
6362
  DIE_UNLESS(rc == 3);
 
6363
 
 
6364
  rc= my_stmt_result("SELECT ROW(1, 7) IN (select id, id1 "
 
6365
                     "from test_sub2 WHERE id1= 8)");
 
6366
  DIE_UNLESS(rc == 1);
 
6367
  rc= my_stmt_result("SELECT ROW(1, 7) IN (select id, id1 "
 
6368
                     "from test_sub2 WHERE id1= 7)");
 
6369
  DIE_UNLESS(rc == 1);
 
6370
 
 
6371
  stmt= mysql_simple_prepare(mysql, ("SELECT ROW(1, 7) IN (select id, id1 "
 
6372
                                     "from test_sub2 WHERE id1= ?)"));
 
6373
  check_stmt(stmt);
 
6374
 
 
6375
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
6376
  check_execute(stmt, rc);
 
6377
 
 
6378
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
6379
  check_execute(stmt, rc);
 
6380
 
 
6381
  id= 7;
 
6382
  rc= mysql_stmt_execute(stmt);
 
6383
  check_execute(stmt, rc);
 
6384
 
 
6385
  rc= mysql_stmt_fetch(stmt);
 
6386
  check_execute(stmt, rc);
 
6387
 
 
6388
  if (!opt_silent)
 
6389
    fprintf(stdout, "\n row 1: %d", id);
 
6390
  DIE_UNLESS(id == 1);
 
6391
 
 
6392
  rc= mysql_stmt_fetch(stmt);
 
6393
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
6394
 
 
6395
  id= 8;
 
6396
  rc= mysql_stmt_execute(stmt);
 
6397
  check_execute(stmt, rc);
 
6398
 
 
6399
  rc= mysql_stmt_fetch(stmt);
 
6400
  check_execute(stmt, rc);
 
6401
 
 
6402
  if (!opt_silent)
 
6403
    fprintf(stdout, "\n row 1: %d", id);
 
6404
  DIE_UNLESS(id == 0);
 
6405
 
 
6406
  rc= mysql_stmt_fetch(stmt);
 
6407
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
6408
 
 
6409
  mysql_stmt_close(stmt);
 
6410
  DBUG_VOID_RETURN;
 
6411
}
 
6412
 
 
6413
 
 
6414
/*
 
6415
  Generalized conversion routine to handle DATE, TIME and DATETIME
 
6416
  conversion using MYSQL_TIME structure
 
6417
*/
 
6418
 
 
6419
static void test_bind_date_conv(uint row_count)
 
6420
{
 
6421
  MYSQL_STMT   *stmt= 0;
 
6422
  uint         rc, i, count= row_count;
 
6423
  ulong        length[4];
 
6424
  MYSQL_BIND   my_bind[4];
 
6425
  my_bool      is_null[4]= {0};
 
6426
  MYSQL_TIME   tm[4];
 
6427
  ulong        second_part;
 
6428
  uint         year, month, day, hour, minute, sec;
 
6429
 
 
6430
  stmt= mysql_simple_prepare(mysql, "INSERT INTO test_date VALUES(?, ?, ?, ?)");
 
6431
  check_stmt(stmt);
 
6432
 
 
6433
  verify_param_count(stmt, 4);
 
6434
 
 
6435
  /*
 
6436
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
6437
    its members.
 
6438
  */
 
6439
  bzero((char*) my_bind, sizeof(my_bind));
 
6440
 
 
6441
  my_bind[0].buffer_type= MYSQL_TYPE_TIMESTAMP;
 
6442
  my_bind[1].buffer_type= MYSQL_TYPE_TIME;
 
6443
  my_bind[2].buffer_type= MYSQL_TYPE_DATETIME;
 
6444
  my_bind[3].buffer_type= MYSQL_TYPE_DATE;
 
6445
 
 
6446
  for (i= 0; i < (int) array_elements(my_bind); i++)
 
6447
  {
 
6448
    my_bind[i].buffer= (void *) &tm[i];
 
6449
    my_bind[i].is_null= &is_null[i];
 
6450
    my_bind[i].length= &length[i];
 
6451
    my_bind[i].buffer_length= 30;
 
6452
    length[i]= 20;
 
6453
  }
 
6454
 
 
6455
  second_part= 0;
 
6456
 
 
6457
  year= 2000;
 
6458
  month= 01;
 
6459
  day= 10;
 
6460
 
 
6461
  hour= 11;
 
6462
  minute= 16;
 
6463
  sec= 20;
 
6464
 
 
6465
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
6466
  check_execute(stmt, rc);
 
6467
 
 
6468
  for (count= 0; count < row_count; count++)
 
6469
  {
 
6470
    for (i= 0; i < (int) array_elements(my_bind); i++)
 
6471
    {
 
6472
      tm[i].neg= 0;
 
6473
      tm[i].second_part= second_part+count;
 
6474
      if (my_bind[i].buffer_type != MYSQL_TYPE_TIME)
 
6475
      {
 
6476
        tm[i].year= year+count;
 
6477
        tm[i].month= month+count;
 
6478
        tm[i].day= day+count;
 
6479
      }
 
6480
      else
 
6481
        tm[i].year= tm[i].month= tm[i].day= 0;
 
6482
      if (my_bind[i].buffer_type != MYSQL_TYPE_DATE)
 
6483
      {
 
6484
        tm[i].hour= hour+count;
 
6485
        tm[i].minute= minute+count;
 
6486
        tm[i].second= sec+count;
 
6487
      }
 
6488
      else
 
6489
        tm[i].hour= tm[i].minute= tm[i].second= 0;
 
6490
    }
 
6491
    rc= mysql_stmt_execute(stmt);
 
6492
    check_execute(stmt, rc);
 
6493
  }
 
6494
 
 
6495
  rc= mysql_commit(mysql);
 
6496
  myquery(rc);
 
6497
 
 
6498
  mysql_stmt_close(stmt);
 
6499
 
 
6500
  rc= my_stmt_result("SELECT * FROM test_date");
 
6501
  DIE_UNLESS(row_count == rc);
 
6502
 
 
6503
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_date");
 
6504
  check_stmt(stmt);
 
6505
 
 
6506
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
6507
  check_execute(stmt, rc);
 
6508
 
 
6509
  rc= mysql_stmt_execute(stmt);
 
6510
  check_execute(stmt, rc);
 
6511
 
 
6512
  rc= mysql_stmt_store_result(stmt);
 
6513
  check_execute(stmt, rc);
 
6514
 
 
6515
  for (count= 0; count < row_count; count++)
 
6516
  {
 
6517
    rc= mysql_stmt_fetch(stmt);
 
6518
    DIE_UNLESS(rc == 0 || rc == MYSQL_DATA_TRUNCATED);
 
6519
 
 
6520
    if (!opt_silent)
 
6521
      fprintf(stdout, "\n");
 
6522
    for (i= 0; i < array_elements(my_bind); i++)
 
6523
    {
 
6524
      if (!opt_silent)
 
6525
        fprintf(stdout, "\ntime[%d]: %02d-%02d-%02d %02d:%02d:%02d.%02lu",
 
6526
                i, tm[i].year, tm[i].month, tm[i].day,
 
6527
                tm[i].hour, tm[i].minute, tm[i].second,
 
6528
                tm[i].second_part);
 
6529
      DIE_UNLESS(tm[i].year == 0 || tm[i].year == year+count);
 
6530
      DIE_UNLESS(tm[i].month == 0 || tm[i].month == month+count);
 
6531
      DIE_UNLESS(tm[i].day == 0 || tm[i].day == day+count);
 
6532
 
 
6533
      DIE_UNLESS(tm[i].hour == 0 || tm[i].hour == hour+count);
 
6534
      DIE_UNLESS(tm[i].minute == 0 || tm[i].minute == minute+count);
 
6535
      DIE_UNLESS(tm[i].second == 0 || tm[i].second == sec+count);
 
6536
      DIE_UNLESS(tm[i].second_part == 0 ||
 
6537
                 tm[i].second_part == second_part+count);
 
6538
    }
 
6539
  }
 
6540
  rc= mysql_stmt_fetch(stmt);
 
6541
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
6542
 
 
6543
  mysql_stmt_close(stmt);
 
6544
}
 
6545
 
 
6546
 
 
6547
/* Test DATE, TIME, DATETIME and TS with MYSQL_TIME conversion */
 
6548
 
 
6549
static void test_date()
 
6550
{
 
6551
  int        rc;
 
6552
 
 
6553
  myheader("test_date");
 
6554
 
 
6555
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
 
6556
  myquery(rc);
 
6557
 
 
6558
  rc= mysql_query(mysql, "CREATE TABLE test_date(c1 TIMESTAMP(14), \
 
6559
                                                 c2 TIME, \
 
6560
                                                 c3 DATETIME, \
 
6561
                                                 c4 DATE)");
 
6562
 
 
6563
  myquery(rc);
 
6564
 
 
6565
  test_bind_date_conv(5);
 
6566
}
 
6567
 
 
6568
 
 
6569
/* Test all time types to DATE and DATE to all types */
 
6570
 
 
6571
static void test_date_date()
 
6572
{
 
6573
  int        rc;
 
6574
 
 
6575
  myheader("test_date_date");
 
6576
 
 
6577
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
 
6578
  myquery(rc);
 
6579
 
 
6580
  rc= mysql_query(mysql, "CREATE TABLE test_date(c1 DATE, \
 
6581
                                                 c2 DATE, \
 
6582
                                                 c3 DATE, \
 
6583
                                                 c4 DATE)");
 
6584
 
 
6585
  myquery(rc);
 
6586
 
 
6587
  test_bind_date_conv(3);
 
6588
}
 
6589
 
 
6590
 
 
6591
/* Test all time types to TIME and TIME to all types */
 
6592
 
 
6593
static void test_date_time()
 
6594
{
 
6595
  int        rc;
 
6596
 
 
6597
  myheader("test_date_time");
 
6598
 
 
6599
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
 
6600
  myquery(rc);
 
6601
 
 
6602
  rc= mysql_query(mysql, "CREATE TABLE test_date(c1 TIME, \
 
6603
                                                 c2 TIME, \
 
6604
                                                 c3 TIME, \
 
6605
                                                 c4 TIME)");
 
6606
 
 
6607
  myquery(rc);
 
6608
 
 
6609
  test_bind_date_conv(3);
 
6610
}
 
6611
 
 
6612
 
 
6613
/* Test all time types to TIMESTAMP and TIMESTAMP to all types */
 
6614
 
 
6615
static void test_date_ts()
 
6616
{
 
6617
  int        rc;
 
6618
 
 
6619
  myheader("test_date_ts");
 
6620
 
 
6621
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
 
6622
  myquery(rc);
 
6623
 
 
6624
  rc= mysql_query(mysql, "CREATE TABLE test_date(c1 TIMESTAMP(10), \
 
6625
                                                 c2 TIMESTAMP(14), \
 
6626
                                                 c3 TIMESTAMP, \
 
6627
                                                 c4 TIMESTAMP(6))");
 
6628
 
 
6629
  myquery(rc);
 
6630
 
 
6631
  test_bind_date_conv(2);
 
6632
}
 
6633
 
 
6634
 
 
6635
/* Test all time types to DATETIME and DATETIME to all types */
 
6636
 
 
6637
static void test_date_dt()
 
6638
{
 
6639
  int rc;
 
6640
 
 
6641
  myheader("test_date_dt");
 
6642
 
 
6643
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_date");
 
6644
  myquery(rc);
 
6645
 
 
6646
  rc= mysql_query(mysql, "CREATE TABLE test_date(c1 datetime, "
 
6647
                         " c2 datetime, c3 datetime, c4 date)");
 
6648
  myquery(rc);
 
6649
 
 
6650
  test_bind_date_conv(2);
 
6651
}
 
6652
 
 
6653
 
 
6654
/* Misc tests to keep pure coverage happy */
 
6655
 
 
6656
static void test_pure_coverage()
 
6657
{
 
6658
  MYSQL_STMT *stmt;
 
6659
  MYSQL_BIND my_bind[1];
 
6660
  int        rc;
 
6661
  ulong      length;
 
6662
 
 
6663
  myheader("test_pure_coverage");
 
6664
 
 
6665
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_pure");
 
6666
  myquery(rc);
 
6667
 
 
6668
  rc= mysql_query(mysql, "CREATE TABLE test_pure(c1 int, c2 varchar(20))");
 
6669
  myquery(rc);
 
6670
 
 
6671
  stmt= mysql_simple_prepare(mysql, "insert into test_pure(c67788) values(10)");
 
6672
  check_stmt_r(stmt);
 
6673
 
 
6674
  /* Query without params and result should allow to bind 0 arrays */
 
6675
  stmt= mysql_simple_prepare(mysql, "insert into test_pure(c2) values(10)");
 
6676
  check_stmt(stmt);
 
6677
 
 
6678
  rc= mysql_stmt_bind_param(stmt, (MYSQL_BIND*)0);
 
6679
  check_execute(stmt, rc);
 
6680
 
 
6681
  rc= mysql_stmt_execute(stmt);
 
6682
  check_execute(stmt, rc);
 
6683
 
 
6684
  rc= mysql_stmt_bind_result(stmt, (MYSQL_BIND*)0);
 
6685
  DIE_UNLESS(rc == 1);
 
6686
 
 
6687
  mysql_stmt_close(stmt);
 
6688
 
 
6689
  stmt= mysql_simple_prepare(mysql, "insert into test_pure(c2) values(?)");
 
6690
  check_stmt(stmt);
 
6691
 
 
6692
  /*
 
6693
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
6694
    its members.
 
6695
  */
 
6696
  bzero((char*) my_bind, sizeof(my_bind));
 
6697
 
 
6698
  my_bind[0].length= &length;
 
6699
  my_bind[0].is_null= 0;
 
6700
  my_bind[0].buffer_length= 0;
 
6701
 
 
6702
  my_bind[0].buffer_type= MYSQL_TYPE_GEOMETRY;
 
6703
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
6704
  check_execute_r(stmt, rc); /* unsupported buffer type */
 
6705
 
 
6706
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
6707
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
6708
  check_execute(stmt, rc);
 
6709
 
 
6710
  rc= mysql_stmt_store_result(stmt);
 
6711
  check_execute(stmt, rc);
 
6712
 
 
6713
  mysql_stmt_close(stmt);
 
6714
 
 
6715
  stmt= mysql_simple_prepare(mysql, "select * from test_pure");
 
6716
  check_execute(stmt, rc);
 
6717
 
 
6718
  rc= mysql_stmt_execute(stmt);
 
6719
  check_execute(stmt, rc);
 
6720
 
 
6721
  my_bind[0].buffer_type= MYSQL_TYPE_GEOMETRY;
 
6722
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
6723
  check_execute_r(stmt, rc); /* unsupported buffer type */
 
6724
 
 
6725
  rc= mysql_stmt_store_result(stmt);
 
6726
  DIE_UNLESS(rc);
 
6727
 
 
6728
  rc= mysql_stmt_store_result(stmt);
 
6729
  DIE_UNLESS(rc); /* Old error must be reset first */
 
6730
 
 
6731
  mysql_stmt_close(stmt);
 
6732
 
 
6733
  mysql_query(mysql, "DROP TABLE test_pure");
 
6734
}
 
6735
 
 
6736
 
 
6737
/* Test for string buffer fetch */
 
6738
 
 
6739
static void test_buffers()
 
6740
{
 
6741
  MYSQL_STMT *stmt;
 
6742
  MYSQL_BIND my_bind[1];
 
6743
  int        rc;
 
6744
  ulong      length;
 
6745
  my_bool    is_null;
 
6746
  char       buffer[20];
 
6747
 
 
6748
  myheader("test_buffers");
 
6749
 
 
6750
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_buffer");
 
6751
  myquery(rc);
 
6752
 
 
6753
  rc= mysql_query(mysql, "CREATE TABLE test_buffer(str varchar(20))");
 
6754
  myquery(rc);
 
6755
 
 
6756
  rc= mysql_query(mysql, "insert into test_buffer values('MySQL')\
 
6757
                          , ('Database'), ('Open-Source'), ('Popular')");
 
6758
  myquery(rc);
 
6759
 
 
6760
  stmt= mysql_simple_prepare(mysql, "select str from test_buffer");
 
6761
  check_stmt(stmt);
 
6762
 
 
6763
  rc= mysql_stmt_execute(stmt);
 
6764
  check_execute(stmt, rc);
 
6765
 
 
6766
  bzero(buffer, sizeof(buffer));              /* Avoid overruns in printf() */
 
6767
 
 
6768
  bzero((char*) my_bind, sizeof(my_bind));
 
6769
  my_bind[0].length= &length;
 
6770
  my_bind[0].is_null= &is_null;
 
6771
  my_bind[0].buffer_length= 1;
 
6772
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
6773
  my_bind[0].buffer= (void *)buffer;
 
6774
  my_bind[0].error= &my_bind[0].error_value;
 
6775
 
 
6776
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
6777
  check_execute(stmt, rc);
 
6778
 
 
6779
  rc= mysql_stmt_store_result(stmt);
 
6780
  check_execute(stmt, rc);
 
6781
 
 
6782
  buffer[1]= 'X';
 
6783
  rc= mysql_stmt_fetch(stmt);
 
6784
  DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
 
6785
  DIE_UNLESS(my_bind[0].error_value);
 
6786
  if (!opt_silent)
 
6787
    fprintf(stdout, "\n data: %s (%lu)", buffer, length);
 
6788
  DIE_UNLESS(buffer[0] == 'M');
 
6789
  DIE_UNLESS(buffer[1] == 'X');
 
6790
  DIE_UNLESS(length == 5);
 
6791
 
 
6792
  my_bind[0].buffer_length= 8;
 
6793
  rc= mysql_stmt_bind_result(stmt, my_bind);/* re-bind */
 
6794
  check_execute(stmt, rc);
 
6795
 
 
6796
  rc= mysql_stmt_fetch(stmt);
 
6797
  check_execute(stmt, rc);
 
6798
  if (!opt_silent)
 
6799
    fprintf(stdout, "\n data: %s (%lu)", buffer, length);
 
6800
  DIE_UNLESS(strncmp(buffer, "Database", 8) == 0);
 
6801
  DIE_UNLESS(length == 8);
 
6802
 
 
6803
  my_bind[0].buffer_length= 12;
 
6804
  rc= mysql_stmt_bind_result(stmt, my_bind);/* re-bind */
 
6805
  check_execute(stmt, rc);
 
6806
 
 
6807
  rc= mysql_stmt_fetch(stmt);
 
6808
  check_execute(stmt, rc);
 
6809
  if (!opt_silent)
 
6810
    fprintf(stdout, "\n data: %s (%lu)", buffer, length);
 
6811
  DIE_UNLESS(strcmp(buffer, "Open-Source") == 0);
 
6812
  DIE_UNLESS(length == 11);
 
6813
 
 
6814
  my_bind[0].buffer_length= 6;
 
6815
  rc= mysql_stmt_bind_result(stmt, my_bind);/* re-bind */
 
6816
  check_execute(stmt, rc);
 
6817
 
 
6818
  rc= mysql_stmt_fetch(stmt);
 
6819
  DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
 
6820
  DIE_UNLESS(my_bind[0].error_value);
 
6821
  if (!opt_silent)
 
6822
    fprintf(stdout, "\n data: %s (%lu)", buffer, length);
 
6823
  DIE_UNLESS(strncmp(buffer, "Popula", 6) == 0);
 
6824
  DIE_UNLESS(length == 7);
 
6825
 
 
6826
  mysql_stmt_close(stmt);
 
6827
}
 
6828
 
 
6829
 
 
6830
/* Test the direct query execution in the middle of open stmts */
 
6831
 
 
6832
static void test_open_direct()
 
6833
{
 
6834
  MYSQL_STMT  *stmt;
 
6835
  MYSQL_RES   *result;
 
6836
  int         rc;
 
6837
 
 
6838
  myheader("test_open_direct");
 
6839
 
 
6840
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_open_direct");
 
6841
  myquery(rc);
 
6842
 
 
6843
  rc= mysql_query(mysql, "CREATE TABLE test_open_direct(id int, name char(6))");
 
6844
  myquery(rc);
 
6845
 
 
6846
  stmt= mysql_simple_prepare(mysql, "INSERT INTO test_open_direct values(10, 'mysql')");
 
6847
  check_stmt(stmt);
 
6848
 
 
6849
  rc= mysql_query(mysql, "SELECT * FROM test_open_direct");
 
6850
  myquery(rc);
 
6851
 
 
6852
  result= mysql_store_result(mysql);
 
6853
  mytest(result);
 
6854
 
 
6855
  rc= my_process_result_set(result);
 
6856
  DIE_UNLESS(rc == 0);
 
6857
  mysql_free_result(result);
 
6858
 
 
6859
  rc= mysql_stmt_execute(stmt);
 
6860
  check_execute(stmt, rc);
 
6861
 
 
6862
  verify_st_affected_rows(stmt, 1);
 
6863
 
 
6864
  rc= mysql_query(mysql, "SELECT * FROM test_open_direct");
 
6865
  myquery(rc);
 
6866
 
 
6867
  result= mysql_store_result(mysql);
 
6868
  mytest(result);
 
6869
 
 
6870
  rc= my_process_result_set(result);
 
6871
  DIE_UNLESS(rc == 1);
 
6872
  mysql_free_result(result);
 
6873
 
 
6874
  rc= mysql_stmt_execute(stmt);
 
6875
  check_execute(stmt, rc);
 
6876
 
 
6877
  verify_st_affected_rows(stmt, 1);
 
6878
 
 
6879
  rc= mysql_query(mysql, "SELECT * FROM test_open_direct");
 
6880
  myquery(rc);
 
6881
 
 
6882
  result= mysql_store_result(mysql);
 
6883
  mytest(result);
 
6884
 
 
6885
  rc= my_process_result_set(result);
 
6886
  DIE_UNLESS(rc == 2);
 
6887
  mysql_free_result(result);
 
6888
 
 
6889
  mysql_stmt_close(stmt);
 
6890
 
 
6891
  /* run a direct query in the middle of a fetch */
 
6892
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_open_direct");
 
6893
  check_stmt(stmt);
 
6894
 
 
6895
  rc= mysql_stmt_execute(stmt);
 
6896
  check_execute(stmt, rc);
 
6897
 
 
6898
  rc= mysql_stmt_fetch(stmt);
 
6899
  check_execute(stmt, rc);
 
6900
 
 
6901
  rc= mysql_query(mysql, "INSERT INTO test_open_direct(id) VALUES(20)");
 
6902
  myquery_r(rc);
 
6903
 
 
6904
  rc= mysql_stmt_close(stmt);
 
6905
  check_execute(stmt, rc);
 
6906
 
 
6907
  rc= mysql_query(mysql, "INSERT INTO test_open_direct(id) VALUES(20)");
 
6908
  myquery(rc);
 
6909
 
 
6910
  /* run a direct query with store result */
 
6911
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_open_direct");
 
6912
  check_stmt(stmt);
 
6913
 
 
6914
  rc= mysql_stmt_execute(stmt);
 
6915
  check_execute(stmt, rc);
 
6916
 
 
6917
  rc= mysql_stmt_store_result(stmt);
 
6918
  check_execute(stmt, rc);
 
6919
 
 
6920
  rc= mysql_stmt_fetch(stmt);
 
6921
  check_execute(stmt, rc);
 
6922
 
 
6923
  rc= mysql_query(mysql, "drop table test_open_direct");
 
6924
  myquery(rc);
 
6925
 
 
6926
  rc= mysql_stmt_close(stmt);
 
6927
  check_execute(stmt, rc);
 
6928
}
 
6929
 
 
6930
 
 
6931
/* Test fetch without prior bound buffers */
 
6932
 
 
6933
static void test_fetch_nobuffs()
 
6934
{
 
6935
  MYSQL_STMT *stmt;
 
6936
  MYSQL_BIND my_bind[4];
 
6937
  char       str[4][50];
 
6938
  int        rc;
 
6939
 
 
6940
  myheader("test_fetch_nobuffs");
 
6941
 
 
6942
  stmt= mysql_simple_prepare(mysql, "SELECT DATABASE(), CURRENT_USER(), \
 
6943
                              CURRENT_DATE(), CURRENT_TIME()");
 
6944
  check_stmt(stmt);
 
6945
 
 
6946
  rc= mysql_stmt_execute(stmt);
 
6947
  check_execute(stmt, rc);
 
6948
 
 
6949
  rc= 0;
 
6950
  while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
 
6951
    rc++;
 
6952
 
 
6953
  if (!opt_silent)
 
6954
    fprintf(stdout, "\n total rows        : %d", rc);
 
6955
  DIE_UNLESS(rc == 1);
 
6956
 
 
6957
  bzero((char*) my_bind, sizeof(MYSQL_BIND));
 
6958
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
6959
  my_bind[0].buffer= (void *)str[0];
 
6960
  my_bind[0].buffer_length= sizeof(str[0]);
 
6961
  my_bind[1]= my_bind[2]= my_bind[3]= my_bind[0];
 
6962
  my_bind[1].buffer= (void *)str[1];
 
6963
  my_bind[2].buffer= (void *)str[2];
 
6964
  my_bind[3].buffer= (void *)str[3];
 
6965
 
 
6966
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
6967
  check_execute(stmt, rc);
 
6968
 
 
6969
  rc= mysql_stmt_execute(stmt);
 
6970
  check_execute(stmt, rc);
 
6971
 
 
6972
  rc= 0;
 
6973
  while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
 
6974
  {
 
6975
    rc++;
 
6976
    if (!opt_silent)
 
6977
    {
 
6978
      fprintf(stdout, "\n CURRENT_DATABASE(): %s", str[0]);
 
6979
      fprintf(stdout, "\n CURRENT_USER()    : %s", str[1]);
 
6980
      fprintf(stdout, "\n CURRENT_DATE()    : %s", str[2]);
 
6981
      fprintf(stdout, "\n CURRENT_TIME()    : %s", str[3]);
 
6982
    }
 
6983
  }
 
6984
  if (!opt_silent)
 
6985
    fprintf(stdout, "\n total rows        : %d", rc);
 
6986
  DIE_UNLESS(rc == 1);
 
6987
 
 
6988
  mysql_stmt_close(stmt);
 
6989
}
 
6990
 
 
6991
 
 
6992
/* Test a misc bug */
 
6993
 
 
6994
static void test_ushort_bug()
 
6995
{
 
6996
  MYSQL_STMT *stmt;
 
6997
  MYSQL_BIND my_bind[4];
 
6998
  ushort     short_value;
 
6999
  uint32     long_value;
 
7000
  ulong      s_length, l_length, ll_length, t_length;
 
7001
  ulonglong  longlong_value;
 
7002
  int        rc;
 
7003
  uchar      tiny_value;
 
7004
  char       llbuf[22];
 
7005
  myheader("test_ushort_bug");
 
7006
 
 
7007
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ushort");
 
7008
  myquery(rc);
 
7009
 
 
7010
  rc= mysql_query(mysql, "CREATE TABLE test_ushort(a smallint unsigned, \
 
7011
                                                  b smallint unsigned, \
 
7012
                                                  c smallint unsigned, \
 
7013
                                                  d smallint unsigned)");
 
7014
  myquery(rc);
 
7015
 
 
7016
  rc= mysql_query(mysql,
 
7017
                  "INSERT INTO test_ushort VALUES(35999, 35999, 35999, 200)");
 
7018
  myquery(rc);
 
7019
 
 
7020
 
 
7021
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_ushort");
 
7022
  check_stmt(stmt);
 
7023
 
 
7024
  rc= mysql_stmt_execute(stmt);
 
7025
  check_execute(stmt, rc);
 
7026
 
 
7027
  bzero((char*) my_bind, sizeof(my_bind));
 
7028
  my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
 
7029
  my_bind[0].buffer= (void *)&short_value;
 
7030
  my_bind[0].is_unsigned= TRUE;
 
7031
  my_bind[0].length= &s_length;
 
7032
 
 
7033
  my_bind[1].buffer_type= MYSQL_TYPE_LONG;
 
7034
  my_bind[1].buffer= (void *)&long_value;
 
7035
  my_bind[1].length= &l_length;
 
7036
 
 
7037
  my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
 
7038
  my_bind[2].buffer= (void *)&longlong_value;
 
7039
  my_bind[2].length= &ll_length;
 
7040
 
 
7041
  my_bind[3].buffer_type= MYSQL_TYPE_TINY;
 
7042
  my_bind[3].buffer= (void *)&tiny_value;
 
7043
  my_bind[3].is_unsigned= TRUE;
 
7044
  my_bind[3].length= &t_length;
 
7045
 
 
7046
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
7047
  check_execute(stmt, rc);
 
7048
 
 
7049
  rc= mysql_stmt_fetch(stmt);
 
7050
  check_execute(stmt, rc);
 
7051
 
 
7052
  if (!opt_silent)
 
7053
  {
 
7054
    fprintf(stdout, "\n ushort   : %d (%ld)", short_value, s_length);
 
7055
    fprintf(stdout, "\n ulong    : %lu (%ld)", (ulong) long_value, l_length);
 
7056
    fprintf(stdout, "\n longlong : %s (%ld)", llstr(longlong_value, llbuf),
 
7057
            ll_length);
 
7058
    fprintf(stdout, "\n tinyint  : %d   (%ld)", tiny_value, t_length);
 
7059
  }
 
7060
 
 
7061
  DIE_UNLESS(short_value == 35999);
 
7062
  DIE_UNLESS(s_length == 2);
 
7063
 
 
7064
  DIE_UNLESS(long_value == 35999);
 
7065
  DIE_UNLESS(l_length == 4);
 
7066
 
 
7067
  DIE_UNLESS(longlong_value == 35999);
 
7068
  DIE_UNLESS(ll_length == 8);
 
7069
 
 
7070
  DIE_UNLESS(tiny_value == 200);
 
7071
  DIE_UNLESS(t_length == 1);
 
7072
 
 
7073
  rc= mysql_stmt_fetch(stmt);
 
7074
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
7075
 
 
7076
  mysql_stmt_close(stmt);
 
7077
}
 
7078
 
 
7079
 
 
7080
/* Test a misc smallint-signed conversion bug */
 
7081
 
 
7082
static void test_sshort_bug()
 
7083
{
 
7084
  MYSQL_STMT *stmt;
 
7085
  MYSQL_BIND my_bind[4];
 
7086
  short      short_value;
 
7087
  int32      long_value;
 
7088
  ulong      s_length, l_length, ll_length, t_length;
 
7089
  ulonglong  longlong_value;
 
7090
  int        rc;
 
7091
  uchar      tiny_value;
 
7092
  char       llbuf[22];
 
7093
 
 
7094
  myheader("test_sshort_bug");
 
7095
 
 
7096
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_sshort");
 
7097
  myquery(rc);
 
7098
 
 
7099
  rc= mysql_query(mysql, "CREATE TABLE test_sshort(a smallint signed, \
 
7100
                                                  b smallint signed, \
 
7101
                                                  c smallint unsigned, \
 
7102
                                                  d smallint unsigned)");
 
7103
  myquery(rc);
 
7104
 
 
7105
  rc= mysql_query(mysql, "INSERT INTO test_sshort VALUES(-5999, -5999, 35999, 200)");
 
7106
  myquery(rc);
 
7107
 
 
7108
 
 
7109
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_sshort");
 
7110
  check_stmt(stmt);
 
7111
 
 
7112
  rc= mysql_stmt_execute(stmt);
 
7113
  check_execute(stmt, rc);
 
7114
 
 
7115
  bzero((char*) my_bind, sizeof(my_bind));
 
7116
  my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
 
7117
  my_bind[0].buffer= (void *)&short_value;
 
7118
  my_bind[0].length= &s_length;
 
7119
 
 
7120
  my_bind[1].buffer_type= MYSQL_TYPE_LONG;
 
7121
  my_bind[1].buffer= (void *)&long_value;
 
7122
  my_bind[1].length= &l_length;
 
7123
 
 
7124
  my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
 
7125
  my_bind[2].buffer= (void *)&longlong_value;
 
7126
  my_bind[2].length= &ll_length;
 
7127
 
 
7128
  my_bind[3].buffer_type= MYSQL_TYPE_TINY;
 
7129
  my_bind[3].buffer= (void *)&tiny_value;
 
7130
  my_bind[3].is_unsigned= TRUE;
 
7131
  my_bind[3].length= &t_length;
 
7132
 
 
7133
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
7134
  check_execute(stmt, rc);
 
7135
 
 
7136
  rc= mysql_stmt_fetch(stmt);
 
7137
  check_execute(stmt, rc);
 
7138
 
 
7139
  if (!opt_silent)
 
7140
  {
 
7141
    fprintf(stdout, "\n sshort   : %d (%ld)", short_value, s_length);
 
7142
    fprintf(stdout, "\n slong    : %ld (%ld)", (long) long_value, l_length);
 
7143
    fprintf(stdout, "\n longlong : %s (%ld)", llstr(longlong_value, llbuf),
 
7144
            ll_length);
 
7145
    fprintf(stdout, "\n tinyint  : %d   (%ld)", tiny_value, t_length);
 
7146
  }
 
7147
 
 
7148
  DIE_UNLESS(short_value == -5999);
 
7149
  DIE_UNLESS(s_length == 2);
 
7150
 
 
7151
  DIE_UNLESS(long_value == -5999);
 
7152
  DIE_UNLESS(l_length == 4);
 
7153
 
 
7154
  DIE_UNLESS(longlong_value == 35999);
 
7155
  DIE_UNLESS(ll_length == 8);
 
7156
 
 
7157
  DIE_UNLESS(tiny_value == 200);
 
7158
  DIE_UNLESS(t_length == 1);
 
7159
 
 
7160
  rc= mysql_stmt_fetch(stmt);
 
7161
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
7162
 
 
7163
  mysql_stmt_close(stmt);
 
7164
}
 
7165
 
 
7166
 
 
7167
/* Test a misc tinyint-signed conversion bug */
 
7168
 
 
7169
static void test_stiny_bug()
 
7170
{
 
7171
  MYSQL_STMT *stmt;
 
7172
  MYSQL_BIND my_bind[4];
 
7173
  short      short_value;
 
7174
  int32      long_value;
 
7175
  ulong      s_length, l_length, ll_length, t_length;
 
7176
  ulonglong  longlong_value;
 
7177
  int        rc;
 
7178
  uchar      tiny_value;
 
7179
  char       llbuf[22];
 
7180
 
 
7181
  myheader("test_stiny_bug");
 
7182
 
 
7183
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_stiny");
 
7184
  myquery(rc);
 
7185
 
 
7186
  rc= mysql_query(mysql, "CREATE TABLE test_stiny(a tinyint signed, \
 
7187
                                                  b tinyint signed, \
 
7188
                                                  c tinyint unsigned, \
 
7189
                                                  d tinyint unsigned)");
 
7190
  myquery(rc);
 
7191
 
 
7192
  rc= mysql_query(mysql, "INSERT INTO test_stiny VALUES(-128, -127, 255, 0)");
 
7193
  myquery(rc);
 
7194
 
 
7195
 
 
7196
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_stiny");
 
7197
  check_stmt(stmt);
 
7198
 
 
7199
  rc= mysql_stmt_execute(stmt);
 
7200
  check_execute(stmt, rc);
 
7201
 
 
7202
  bzero((char*) my_bind, sizeof(my_bind));
 
7203
  my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
 
7204
  my_bind[0].buffer= (void *)&short_value;
 
7205
  my_bind[0].length= &s_length;
 
7206
 
 
7207
  my_bind[1].buffer_type= MYSQL_TYPE_LONG;
 
7208
  my_bind[1].buffer= (void *)&long_value;
 
7209
  my_bind[1].length= &l_length;
 
7210
 
 
7211
  my_bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
 
7212
  my_bind[2].buffer= (void *)&longlong_value;
 
7213
  my_bind[2].length= &ll_length;
 
7214
 
 
7215
  my_bind[3].buffer_type= MYSQL_TYPE_TINY;
 
7216
  my_bind[3].buffer= (void *)&tiny_value;
 
7217
  my_bind[3].length= &t_length;
 
7218
 
 
7219
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
7220
  check_execute(stmt, rc);
 
7221
 
 
7222
  rc= mysql_stmt_fetch(stmt);
 
7223
  check_execute(stmt, rc);
 
7224
 
 
7225
  if (!opt_silent)
 
7226
  {
 
7227
    fprintf(stdout, "\n sshort   : %d (%ld)", short_value, s_length);
 
7228
    fprintf(stdout, "\n slong    : %ld (%ld)", (long) long_value, l_length);
 
7229
    fprintf(stdout, "\n longlong : %s  (%ld)", llstr(longlong_value, llbuf),
 
7230
            ll_length);
 
7231
    fprintf(stdout, "\n tinyint  : %d    (%ld)", tiny_value, t_length);
 
7232
  }
 
7233
 
 
7234
  DIE_UNLESS(short_value == -128);
 
7235
  DIE_UNLESS(s_length == 2);
 
7236
 
 
7237
  DIE_UNLESS(long_value == -127);
 
7238
  DIE_UNLESS(l_length == 4);
 
7239
 
 
7240
  DIE_UNLESS(longlong_value == 255);
 
7241
  DIE_UNLESS(ll_length == 8);
 
7242
 
 
7243
  DIE_UNLESS(tiny_value == 0);
 
7244
  DIE_UNLESS(t_length == 1);
 
7245
 
 
7246
  rc= mysql_stmt_fetch(stmt);
 
7247
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
7248
 
 
7249
  mysql_stmt_close(stmt);
 
7250
}
 
7251
 
 
7252
 
 
7253
/* Test misc field information, bug: #74 */
 
7254
 
 
7255
static void test_field_misc()
 
7256
{
 
7257
  MYSQL_STMT  *stmt;
 
7258
  MYSQL_RES   *result;
 
7259
  int         rc;
 
7260
 
 
7261
  myheader("test_field_misc");
 
7262
 
 
7263
  rc= mysql_query(mysql, "SELECT @@autocommit");
 
7264
  myquery(rc);
 
7265
 
 
7266
  result= mysql_store_result(mysql);
 
7267
  mytest(result);
 
7268
 
 
7269
  rc= my_process_result_set(result);
 
7270
  DIE_UNLESS(rc == 1);
 
7271
 
 
7272
  verify_prepare_field(result, 0,
 
7273
                       "@@autocommit", "",  /* field and its org name */
 
7274
                       MYSQL_TYPE_LONGLONG, /* field type */
 
7275
                       "", "",              /* table and its org name */
 
7276
                       "", 1, 0);           /* db name, length(its bool flag)*/
 
7277
 
 
7278
  mysql_free_result(result);
 
7279
 
 
7280
  stmt= mysql_simple_prepare(mysql, "SELECT @@autocommit");
 
7281
  check_stmt(stmt);
 
7282
 
 
7283
  rc= mysql_stmt_execute(stmt);
 
7284
  check_execute(stmt, rc);
 
7285
 
 
7286
  result= mysql_stmt_result_metadata(stmt);
 
7287
  mytest(result);
 
7288
 
 
7289
  rc= my_process_stmt_result(stmt);
 
7290
  DIE_UNLESS(rc == 1);
 
7291
 
 
7292
  verify_prepare_field(result, 0,
 
7293
                       "@@autocommit", "",  /* field and its org name */
 
7294
                       MYSQL_TYPE_LONGLONG, /* field type */
 
7295
                       "", "",              /* table and its org name */
 
7296
                       "", 1, 0);           /* db name, length(its bool flag)*/
 
7297
 
 
7298
  mysql_free_result(result);
 
7299
  mysql_stmt_close(stmt);
 
7300
 
 
7301
  stmt= mysql_simple_prepare(mysql, "SELECT @@max_error_count");
 
7302
  check_stmt(stmt);
 
7303
 
 
7304
  result= mysql_stmt_result_metadata(stmt);
 
7305
  mytest(result);
 
7306
 
 
7307
  rc= mysql_stmt_execute(stmt);
 
7308
  check_execute(stmt, rc);
 
7309
 
 
7310
  rc= my_process_stmt_result(stmt);
 
7311
  DIE_UNLESS(rc == 1);
 
7312
 
 
7313
  verify_prepare_field(result, 0,
 
7314
                       "@@max_error_count", "",   /* field and its org name */
 
7315
                       MYSQL_TYPE_LONGLONG, /* field type */
 
7316
                       "", "",              /* table and its org name */
 
7317
                       /* db name, length */
 
7318
                       "", MY_INT64_NUM_DECIMAL_DIGITS , 0);
 
7319
 
 
7320
  mysql_free_result(result);
 
7321
  mysql_stmt_close(stmt);
 
7322
 
 
7323
  stmt= mysql_simple_prepare(mysql, "SELECT @@max_allowed_packet");
 
7324
  check_stmt(stmt);
 
7325
 
 
7326
  result= mysql_stmt_result_metadata(stmt);
 
7327
  mytest(result);
 
7328
 
 
7329
  rc= mysql_stmt_execute(stmt);
 
7330
  check_execute(stmt, rc);
 
7331
 
 
7332
  DIE_UNLESS(1 == my_process_stmt_result(stmt));
 
7333
 
 
7334
  verify_prepare_field(result, 0,
 
7335
                       "@@max_allowed_packet", "", /* field and its org name */
 
7336
                       MYSQL_TYPE_LONGLONG, /* field type */
 
7337
                       "", "",              /* table and its org name */
 
7338
                       /* db name, length */
 
7339
                       "", MY_INT64_NUM_DECIMAL_DIGITS, 0);
 
7340
 
 
7341
  mysql_free_result(result);
 
7342
  mysql_stmt_close(stmt);
 
7343
 
 
7344
  stmt= mysql_simple_prepare(mysql, "SELECT @@sql_warnings");
 
7345
  check_stmt(stmt);
 
7346
 
 
7347
  result= mysql_stmt_result_metadata(stmt);
 
7348
  mytest(result);
 
7349
 
 
7350
  rc= mysql_stmt_execute(stmt);
 
7351
  check_execute(stmt, rc);
 
7352
 
 
7353
  rc= my_process_stmt_result(stmt);
 
7354
  DIE_UNLESS(rc == 1);
 
7355
 
 
7356
  verify_prepare_field(result, 0,
 
7357
                       "@@sql_warnings", "",  /* field and its org name */
 
7358
                       MYSQL_TYPE_LONGLONG,   /* field type */
 
7359
                       "", "",                /* table and its org name */
 
7360
                       "", 1, 0);             /* db name, length */
 
7361
 
 
7362
  mysql_free_result(result);
 
7363
  mysql_stmt_close(stmt);
 
7364
}
 
7365
 
 
7366
 
 
7367
/*
 
7368
  Test SET OPTION feature with prepare stmts
 
7369
  bug #85 (reported by mark@mysql.com)
 
7370
*/
 
7371
 
 
7372
static void test_set_option()
 
7373
{
 
7374
  MYSQL_STMT *stmt;
 
7375
  MYSQL_RES  *result;
 
7376
  int        rc;
 
7377
 
 
7378
  myheader("test_set_option");
 
7379
 
 
7380
  mysql_autocommit(mysql, TRUE);
 
7381
 
 
7382
  /* LIMIT the rows count to 2 */
 
7383
  rc= mysql_query(mysql, "SET OPTION SQL_SELECT_LIMIT= 2");
 
7384
  myquery(rc);
 
7385
 
 
7386
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_limit");
 
7387
  myquery(rc);
 
7388
 
 
7389
  rc= mysql_query(mysql, "CREATE TABLE test_limit(a tinyint)");
 
7390
  myquery(rc);
 
7391
 
 
7392
  rc= mysql_query(mysql, "INSERT INTO test_limit VALUES(10), (20), (30), (40)");
 
7393
  myquery(rc);
 
7394
 
 
7395
  if (!opt_silent)
 
7396
    fprintf(stdout, "\n with SQL_SELECT_LIMIT= 2 (direct)");
 
7397
  rc= mysql_query(mysql, "SELECT * FROM test_limit");
 
7398
  myquery(rc);
 
7399
 
 
7400
  result= mysql_store_result(mysql);
 
7401
  mytest(result);
 
7402
 
 
7403
  rc= my_process_result_set(result);
 
7404
  DIE_UNLESS(rc == 2);
 
7405
 
 
7406
  mysql_free_result(result);
 
7407
 
 
7408
  if (!opt_silent)
 
7409
    fprintf(stdout, "\n with SQL_SELECT_LIMIT=2 (prepare)");
 
7410
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_limit");
 
7411
  check_stmt(stmt);
 
7412
 
 
7413
  rc= mysql_stmt_execute(stmt);
 
7414
  check_execute(stmt, rc);
 
7415
 
 
7416
  rc= my_process_stmt_result(stmt);
 
7417
  DIE_UNLESS(rc == 2);
 
7418
 
 
7419
  mysql_stmt_close(stmt);
 
7420
 
 
7421
  /* RESET the LIMIT the rows count to 0 */
 
7422
  if (!opt_silent)
 
7423
    fprintf(stdout, "\n with SQL_SELECT_LIMIT=DEFAULT (prepare)");
 
7424
  rc= mysql_query(mysql, "SET OPTION SQL_SELECT_LIMIT=DEFAULT");
 
7425
  myquery(rc);
 
7426
 
 
7427
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_limit");
 
7428
  check_stmt(stmt);
 
7429
 
 
7430
  rc= mysql_stmt_execute(stmt);
 
7431
  check_execute(stmt, rc);
 
7432
 
 
7433
  rc= my_process_stmt_result(stmt);
 
7434
  DIE_UNLESS(rc == 4);
 
7435
 
 
7436
  mysql_stmt_close(stmt);
 
7437
}
 
7438
 
 
7439
 
 
7440
/*
 
7441
  Test a misc GRANT option
 
7442
  bug #89 (reported by mark@mysql.com)
 
7443
*/
 
7444
 
 
7445
#ifndef EMBEDDED_LIBRARY
 
7446
static void test_prepare_grant()
 
7447
{
 
7448
  int rc;
 
7449
  char query[MAX_TEST_QUERY_LENGTH];
 
7450
 
 
7451
  myheader("test_prepare_grant");
 
7452
 
 
7453
  mysql_autocommit(mysql, TRUE);
 
7454
 
 
7455
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_grant");
 
7456
  myquery(rc);
 
7457
 
 
7458
  rc= mysql_query(mysql, "CREATE TABLE test_grant(a tinyint primary key auto_increment)");
 
7459
  myquery(rc);
 
7460
 
 
7461
  strxmov(query, "GRANT INSERT, UPDATE, SELECT ON ", current_db,
 
7462
                ".test_grant TO 'test_grant'@",
 
7463
                opt_host ? opt_host : "'localhost'", NullS);
 
7464
 
 
7465
  if (mysql_query(mysql, query))
 
7466
  {
 
7467
    myerror("GRANT failed");
 
7468
 
 
7469
    /*
 
7470
       If server started with --skip-grant-tables, skip this test, else
 
7471
       exit to indicate an error
 
7472
 
 
7473
       ER_UNKNOWN_COM_ERROR= 1047
 
7474
     */
 
7475
    if (mysql_errno(mysql) != 1047)
 
7476
      exit(1);
 
7477
  }
 
7478
  else
 
7479
  {
 
7480
    MYSQL *org_mysql= mysql, *lmysql;
 
7481
    MYSQL_STMT *stmt;
 
7482
 
 
7483
    if (!opt_silent)
 
7484
      fprintf(stdout, "\n Establishing a test connection ...");
 
7485
    if (!(lmysql= mysql_client_init(NULL)))
 
7486
    {
 
7487
      myerror("mysql_client_init() failed");
 
7488
      exit(1);
 
7489
    }
 
7490
    if (!(mysql_real_connect(lmysql, opt_host, "test_grant",
 
7491
                             "", current_db, opt_port,
 
7492
                             opt_unix_socket, 0)))
 
7493
    {
 
7494
      myerror("connection failed");
 
7495
      mysql_close(lmysql);
 
7496
      exit(1);
 
7497
    }
 
7498
    lmysql->reconnect= 1;
 
7499
    if (!opt_silent)
 
7500
      fprintf(stdout, "OK");
 
7501
 
 
7502
    mysql= lmysql;
 
7503
    rc= mysql_query(mysql, "INSERT INTO test_grant VALUES(NULL)");
 
7504
    myquery(rc);
 
7505
 
 
7506
    rc= mysql_query(mysql, "INSERT INTO test_grant(a) VALUES(NULL)");
 
7507
    myquery(rc);
 
7508
 
 
7509
    execute_prepare_query("INSERT INTO test_grant(a) VALUES(NULL)", 1);
 
7510
    execute_prepare_query("INSERT INTO test_grant VALUES(NULL)", 1);
 
7511
    execute_prepare_query("UPDATE test_grant SET a=9 WHERE a=1", 1);
 
7512
    rc= my_stmt_result("SELECT a FROM test_grant");
 
7513
    DIE_UNLESS(rc == 4);
 
7514
 
 
7515
    /* Both DELETE expected to fail as user does not have DELETE privs */
 
7516
 
 
7517
    rc= mysql_query(mysql, "DELETE FROM test_grant");
 
7518
    myquery_r(rc);
 
7519
 
 
7520
    stmt= mysql_simple_prepare(mysql, "DELETE FROM test_grant");
 
7521
    check_stmt_r(stmt);
 
7522
 
 
7523
    rc= my_stmt_result("SELECT * FROM test_grant");
 
7524
    DIE_UNLESS(rc == 4);
 
7525
 
 
7526
    mysql_close(lmysql);
 
7527
    mysql= org_mysql;
 
7528
 
 
7529
    rc= mysql_query(mysql, "delete from mysql.user where User='test_grant'");
 
7530
    myquery(rc);
 
7531
    DIE_UNLESS(1 == mysql_affected_rows(mysql));
 
7532
 
 
7533
    rc= mysql_query(mysql, "delete from mysql.tables_priv where User='test_grant'");
 
7534
    myquery(rc);
 
7535
    DIE_UNLESS(1 == mysql_affected_rows(mysql));
 
7536
 
 
7537
  }
 
7538
}
 
7539
#endif /* EMBEDDED_LIBRARY */
 
7540
 
 
7541
/*
 
7542
  Test a crash when invalid/corrupted .frm is used in the
 
7543
  SHOW TABLE STATUS
 
7544
  bug #93 (reported by serg@mysql.com).
 
7545
*/
 
7546
 
 
7547
static void test_frm_bug()
 
7548
{
 
7549
  MYSQL_STMT *stmt;
 
7550
  MYSQL_BIND my_bind[2];
 
7551
  MYSQL_RES  *result;
 
7552
  MYSQL_ROW  row;
 
7553
  FILE       *test_file;
 
7554
  char       data_dir[FN_REFLEN];
 
7555
  char       test_frm[FN_REFLEN];
 
7556
  int        rc;
 
7557
 
 
7558
  myheader("test_frm_bug");
 
7559
 
 
7560
  mysql_autocommit(mysql, TRUE);
 
7561
 
 
7562
  rc= mysql_query(mysql, "drop table if exists test_frm_bug");
 
7563
  myquery(rc);
 
7564
 
 
7565
  rc= mysql_query(mysql, "flush tables");
 
7566
  myquery(rc);
 
7567
 
 
7568
  stmt= mysql_simple_prepare(mysql, "show variables like 'datadir'");
 
7569
  check_stmt(stmt);
 
7570
 
 
7571
  rc= mysql_stmt_execute(stmt);
 
7572
  check_execute(stmt, rc);
 
7573
 
 
7574
  bzero((char*) my_bind, sizeof(my_bind));
 
7575
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
7576
  my_bind[0].buffer= data_dir;
 
7577
  my_bind[0].buffer_length= FN_REFLEN;
 
7578
  my_bind[1]= my_bind[0];
 
7579
 
 
7580
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
7581
  check_execute(stmt, rc);
 
7582
 
 
7583
  rc= mysql_stmt_fetch(stmt);
 
7584
  check_execute(stmt, rc);
 
7585
 
 
7586
  if (!opt_silent)
 
7587
    fprintf(stdout, "\n data directory: %s", data_dir);
 
7588
 
 
7589
  rc= mysql_stmt_fetch(stmt);
 
7590
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
7591
 
 
7592
  strxmov(test_frm, data_dir, "/", current_db, "/", "test_frm_bug.frm", NullS);
 
7593
 
 
7594
  if (!opt_silent)
 
7595
    fprintf(stdout, "\n test_frm: %s", test_frm);
 
7596
 
 
7597
  if (!(test_file= my_fopen(test_frm, (int) (O_RDWR | O_CREAT), MYF(MY_WME))))
 
7598
  {
 
7599
    fprintf(stdout, "\n ERROR: my_fopen failed for '%s'", test_frm);
 
7600
    fprintf(stdout, "\n test cancelled");
 
7601
    exit(1);
 
7602
  }
 
7603
  if (!opt_silent)
 
7604
    fprintf(test_file, "this is a junk file for test");
 
7605
 
 
7606
  rc= mysql_query(mysql, "SHOW TABLE STATUS like 'test_frm_bug'");
 
7607
  myquery(rc);
 
7608
 
 
7609
  result= mysql_store_result(mysql);
 
7610
  mytest(result);/* It can't be NULL */
 
7611
 
 
7612
  rc= my_process_result_set(result);
 
7613
  DIE_UNLESS(rc == 1);
 
7614
 
 
7615
  mysql_data_seek(result, 0);
 
7616
 
 
7617
  row= mysql_fetch_row(result);
 
7618
  mytest(row);
 
7619
 
 
7620
  if (!opt_silent)
 
7621
    fprintf(stdout, "\n Comment: %s", row[17]);
 
7622
  DIE_UNLESS(row[17] != 0);
 
7623
 
 
7624
  mysql_free_result(result);
 
7625
  mysql_stmt_close(stmt);
 
7626
 
 
7627
  my_fclose(test_file, MYF(0));
 
7628
  mysql_query(mysql, "drop table if exists test_frm_bug");
 
7629
}
 
7630
 
 
7631
 
 
7632
/* Test DECIMAL conversion */
 
7633
 
 
7634
static void test_decimal_bug()
 
7635
{
 
7636
  MYSQL_STMT *stmt;
 
7637
  MYSQL_BIND my_bind[1];
 
7638
  char       data[30];
 
7639
  int        rc;
 
7640
  my_bool    is_null;
 
7641
 
 
7642
  myheader("test_decimal_bug");
 
7643
 
 
7644
  mysql_autocommit(mysql, TRUE);
 
7645
 
 
7646
  rc= mysql_query(mysql, "drop table if exists test_decimal_bug");
 
7647
  myquery(rc);
 
7648
 
 
7649
  rc= mysql_query(mysql, "create table test_decimal_bug(c1 decimal(10, 2))");
 
7650
  myquery(rc);
 
7651
 
 
7652
  rc= mysql_query(mysql, "insert into test_decimal_bug value(8), (10.22), (5.61)");
 
7653
  myquery(rc);
 
7654
 
 
7655
  stmt= mysql_simple_prepare(mysql, "select c1 from test_decimal_bug where c1= ?");
 
7656
  check_stmt(stmt);
 
7657
 
 
7658
  /*
 
7659
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
7660
    its members.
 
7661
  */
 
7662
  bzero((char*) my_bind, sizeof(my_bind));
 
7663
 
 
7664
  my_bind[0].buffer_type= MYSQL_TYPE_NEWDECIMAL;
 
7665
  my_bind[0].buffer= (void *)data;
 
7666
  my_bind[0].buffer_length= 25;
 
7667
  my_bind[0].is_null= &is_null;
 
7668
 
 
7669
  is_null= 0;
 
7670
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
7671
  check_execute(stmt, rc);
 
7672
 
 
7673
  strmov(data, "8.0");
 
7674
  rc= mysql_stmt_execute(stmt);
 
7675
  check_execute(stmt, rc);
 
7676
 
 
7677
  data[0]= 0;
 
7678
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
7679
  check_execute(stmt, rc);
 
7680
 
 
7681
  rc= mysql_stmt_fetch(stmt);
 
7682
  check_execute(stmt, rc);
 
7683
 
 
7684
  if (!opt_silent)
 
7685
    fprintf(stdout, "\n data: %s", data);
 
7686
  DIE_UNLESS(strcmp(data, "8.00") == 0);
 
7687
 
 
7688
  rc= mysql_stmt_fetch(stmt);
 
7689
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
7690
 
 
7691
  strmov(data, "5.61");
 
7692
  rc= mysql_stmt_execute(stmt);
 
7693
  check_execute(stmt, rc);
 
7694
 
 
7695
  data[0]= 0;
 
7696
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
7697
  check_execute(stmt, rc);
 
7698
 
 
7699
  rc= mysql_stmt_fetch(stmt);
 
7700
  check_execute(stmt, rc);
 
7701
 
 
7702
  if (!opt_silent)
 
7703
    fprintf(stdout, "\n data: %s", data);
 
7704
  DIE_UNLESS(strcmp(data, "5.61") == 0);
 
7705
 
 
7706
  rc= mysql_stmt_fetch(stmt);
 
7707
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
7708
 
 
7709
  is_null= 1;
 
7710
  rc= mysql_stmt_execute(stmt);
 
7711
  check_execute(stmt, rc);
 
7712
 
 
7713
  rc= mysql_stmt_fetch(stmt);
 
7714
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
7715
 
 
7716
  strmov(data, "10.22"); is_null= 0;
 
7717
  rc= mysql_stmt_execute(stmt);
 
7718
  check_execute(stmt, rc);
 
7719
 
 
7720
  data[0]= 0;
 
7721
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
7722
  check_execute(stmt, rc);
 
7723
 
 
7724
  rc= mysql_stmt_fetch(stmt);
 
7725
  check_execute(stmt, rc);
 
7726
 
 
7727
  if (!opt_silent)
 
7728
    fprintf(stdout, "\n data: %s", data);
 
7729
  DIE_UNLESS(strcmp(data, "10.22") == 0);
 
7730
 
 
7731
  rc= mysql_stmt_fetch(stmt);
 
7732
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
7733
 
 
7734
  mysql_stmt_close(stmt);
 
7735
}
 
7736
 
 
7737
 
 
7738
/* Test EXPLAIN bug (#115, reported by mark@mysql.com & georg@php.net). */
 
7739
 
 
7740
static void test_explain_bug()
 
7741
{
 
7742
  MYSQL_STMT *stmt;
 
7743
  MYSQL_RES  *result;
 
7744
  int        rc;
 
7745
 
 
7746
  myheader("test_explain_bug");
 
7747
 
 
7748
  mysql_autocommit(mysql, TRUE);
 
7749
 
 
7750
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_explain");
 
7751
  myquery(rc);
 
7752
 
 
7753
  rc= mysql_query(mysql, "CREATE TABLE test_explain(id int, name char(2))");
 
7754
  myquery(rc);
 
7755
 
 
7756
  stmt= mysql_simple_prepare(mysql, "explain test_explain");
 
7757
  check_stmt(stmt);
 
7758
 
 
7759
  rc= mysql_stmt_execute(stmt);
 
7760
  check_execute(stmt, rc);
 
7761
 
 
7762
  rc= my_process_stmt_result(stmt);
 
7763
  DIE_UNLESS(rc == 2);
 
7764
 
 
7765
  result= mysql_stmt_result_metadata(stmt);
 
7766
  mytest(result);
 
7767
 
 
7768
  if (!opt_silent)
 
7769
    fprintf(stdout, "\n total fields in the result: %d",
 
7770
            mysql_num_fields(result));
 
7771
  DIE_UNLESS(6 == mysql_num_fields(result));
 
7772
 
 
7773
  verify_prepare_field(result, 0, "Field", "COLUMN_NAME",
 
7774
                       mysql_get_server_version(mysql) <= 50000 ?
 
7775
                       MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
 
7776
                       0, 0, "", 64, 0);
 
7777
 
 
7778
  verify_prepare_field(result, 1, "Type", "COLUMN_TYPE", MYSQL_TYPE_BLOB,
 
7779
                       0, 0, "", 0, 0);
 
7780
 
 
7781
  verify_prepare_field(result, 2, "Null", "IS_NULLABLE",
 
7782
                       mysql_get_server_version(mysql) <= 50000 ?
 
7783
                       MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
 
7784
                       0, 0, "", 3, 0);
 
7785
 
 
7786
  verify_prepare_field(result, 3, "Key", "COLUMN_KEY",
 
7787
                       mysql_get_server_version(mysql) <= 50000 ?
 
7788
                       MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
 
7789
                       0, 0, "", 3, 0);
 
7790
 
 
7791
  if ( mysql_get_server_version(mysql) >= 50027 )
 
7792
  {
 
7793
    /*  The patch for bug#23037 changes column type of DEAULT to blob */
 
7794
    verify_prepare_field(result, 4, "Default", "COLUMN_DEFAULT",
 
7795
                         MYSQL_TYPE_BLOB, 0, 0, "", 0, 0);
 
7796
  }
 
7797
  else
 
7798
  {
 
7799
    verify_prepare_field(result, 4, "Default", "COLUMN_DEFAULT",
 
7800
                         mysql_get_server_version(mysql) >= 50027 ?
 
7801
                         MYSQL_TYPE_BLOB :
 
7802
                         mysql_get_server_version(mysql) <= 50000 ?
 
7803
                         MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
 
7804
                         0, 0, "",
 
7805
                         mysql_get_server_version(mysql) >= 50027 ? 0 :64, 0);
 
7806
  }
 
7807
 
 
7808
  verify_prepare_field(result, 5, "Extra", "EXTRA",
 
7809
                       mysql_get_server_version(mysql) <= 50000 ?
 
7810
                       MYSQL_TYPE_STRING : MYSQL_TYPE_VAR_STRING,
 
7811
                       0, 0, "", 27, 0);
 
7812
 
 
7813
  mysql_free_result(result);
 
7814
  mysql_stmt_close(stmt);
 
7815
 
 
7816
  stmt= mysql_simple_prepare(mysql, "explain select id, name FROM test_explain");
 
7817
  check_stmt(stmt);
 
7818
 
 
7819
  rc= mysql_stmt_execute(stmt);
 
7820
  check_execute(stmt, rc);
 
7821
 
 
7822
  rc= my_process_stmt_result(stmt);
 
7823
  DIE_UNLESS(rc == 1);
 
7824
 
 
7825
  result= mysql_stmt_result_metadata(stmt);
 
7826
  mytest(result);
 
7827
 
 
7828
  if (!opt_silent)
 
7829
    fprintf(stdout, "\n total fields in the result: %d",
 
7830
            mysql_num_fields(result));
 
7831
  DIE_UNLESS(10 == mysql_num_fields(result));
 
7832
 
 
7833
  verify_prepare_field(result, 0, "id", "", MYSQL_TYPE_LONGLONG,
 
7834
                       "", "", "", 3, 0);
 
7835
 
 
7836
  verify_prepare_field(result, 1, "select_type", "", MYSQL_TYPE_VAR_STRING,
 
7837
                       "", "", "", 19, 0);
 
7838
 
 
7839
  verify_prepare_field(result, 2, "table", "", MYSQL_TYPE_VAR_STRING,
 
7840
                       "", "", "", NAME_CHAR_LEN, 0);
 
7841
 
 
7842
  verify_prepare_field(result, 3, "type", "", MYSQL_TYPE_VAR_STRING,
 
7843
                       "", "", "", 10, 0);
 
7844
 
 
7845
  verify_prepare_field(result, 4, "possible_keys", "", MYSQL_TYPE_VAR_STRING,
 
7846
                       "", "", "", NAME_CHAR_LEN*MAX_KEY, 0);
 
7847
 
 
7848
  verify_prepare_field(result, 5, "key", "", MYSQL_TYPE_VAR_STRING,
 
7849
                       "", "", "", NAME_CHAR_LEN, 0);
 
7850
 
 
7851
  if (mysql_get_server_version(mysql) <= 50000)
 
7852
  {
 
7853
    verify_prepare_field(result, 6, "key_len", "", MYSQL_TYPE_LONGLONG, "",
 
7854
                         "", "", 3, 0);
 
7855
  }
 
7856
  else
 
7857
  {
 
7858
    verify_prepare_field(result, 6, "key_len", "", MYSQL_TYPE_VAR_STRING, "", 
 
7859
                         "", "", NAME_CHAR_LEN*MAX_KEY, 0);
 
7860
  }
 
7861
 
 
7862
  verify_prepare_field(result, 7, "ref", "", MYSQL_TYPE_VAR_STRING,
 
7863
                       "", "", "", NAME_CHAR_LEN*16, 0);
 
7864
 
 
7865
  verify_prepare_field(result, 8, "rows", "", MYSQL_TYPE_LONGLONG,
 
7866
                       "", "", "", 10, 0);
 
7867
 
 
7868
  verify_prepare_field(result, 9, "Extra", "", MYSQL_TYPE_VAR_STRING,
 
7869
                       "", "", "", 255, 0);
 
7870
 
 
7871
  mysql_free_result(result);
 
7872
  mysql_stmt_close(stmt);
 
7873
}
 
7874
 
 
7875
#ifdef NOT_YET_WORKING
 
7876
 
 
7877
/*
 
7878
  Test math functions.
 
7879
  Bug #148 (reported by salle@mysql.com).
 
7880
*/
 
7881
 
 
7882
#define myerrno(n) check_errcode(n)
 
7883
 
 
7884
static void check_errcode(const unsigned int err)
 
7885
{
 
7886
  if (!opt_silent || mysql_errno(mysql) != err)
 
7887
  {
 
7888
    if (mysql->server_version)
 
7889
      fprintf(stdout, "\n [MySQL-%s]", mysql->server_version);
 
7890
    else
 
7891
      fprintf(stdout, "\n [MySQL]");
 
7892
    fprintf(stdout, "[%d] %s\n", mysql_errno(mysql), mysql_error(mysql));
 
7893
  }
 
7894
  DIE_UNLESS(mysql_errno(mysql) == err);
 
7895
}
 
7896
 
 
7897
 
 
7898
static void test_drop_temp()
 
7899
{
 
7900
  int rc;
 
7901
 
 
7902
  myheader("test_drop_temp");
 
7903
 
 
7904
  rc= mysql_query(mysql, "DROP DATABASE IF EXISTS test_drop_temp_db");
 
7905
  myquery(rc);
 
7906
 
 
7907
  rc= mysql_query(mysql, "CREATE DATABASE test_drop_temp_db");
 
7908
  myquery(rc);
 
7909
 
 
7910
  rc= mysql_query(mysql, "CREATE TABLE test_drop_temp_db.t1(c1 int, c2 char(1))");
 
7911
  myquery(rc);
 
7912
 
 
7913
  rc= mysql_query(mysql, "delete from mysql.db where Db='test_drop_temp_db'");
 
7914
  myquery(rc);
 
7915
 
 
7916
  rc= mysql_query(mysql, "delete from mysql.db where Db='test_drop_temp_db'");
 
7917
  myquery(rc);
 
7918
 
 
7919
  strxmov(query, "GRANT SELECT, USAGE, DROP ON test_drop_temp_db.* TO test_temp@",
 
7920
                opt_host ? opt_host : "localhost", NullS);
 
7921
 
 
7922
  if (mysql_query(mysql, query))
 
7923
  {
 
7924
    myerror("GRANT failed");
 
7925
 
 
7926
    /*
 
7927
       If server started with --skip-grant-tables, skip this test, else
 
7928
       exit to indicate an error
 
7929
 
 
7930
       ER_UNKNOWN_COM_ERROR= 1047
 
7931
     */
 
7932
    if (mysql_errno(mysql) != 1047)
 
7933
      exit(1);
 
7934
  }
 
7935
  else
 
7936
  {
 
7937
    MYSQL *org_mysql= mysql, *lmysql;
 
7938
 
 
7939
    if (!opt_silent)
 
7940
      fprintf(stdout, "\n Establishing a test connection ...");
 
7941
    if (!(lmysql= mysql_client_init(NULL)))
 
7942
    {
 
7943
      myerror("mysql_client_init() failed");
 
7944
      exit(1);
 
7945
    }
 
7946
 
 
7947
    rc= mysql_query(mysql, "flush privileges");
 
7948
    myquery(rc);
 
7949
 
 
7950
    if (!(mysql_real_connect(lmysql, opt_host ? opt_host : "localhost", "test_temp",
 
7951
                             "", "test_drop_temp_db", opt_port,
 
7952
                             opt_unix_socket, 0)))
 
7953
    {
 
7954
      mysql= lmysql;
 
7955
      myerror("connection failed");
 
7956
      mysql_close(lmysql);
 
7957
      exit(1);
 
7958
    }
 
7959
    lmysql->reconnect= 1;
 
7960
    if (!opt_silent)
 
7961
      fprintf(stdout, "OK");
 
7962
 
 
7963
    mysql= lmysql;
 
7964
    rc= mysql_query(mysql, "INSERT INTO t1 VALUES(10, 'C')");
 
7965
    myerrno((uint)1142);
 
7966
 
 
7967
    rc= mysql_query(mysql, "DROP TABLE t1");
 
7968
    myerrno((uint)1142);
 
7969
 
 
7970
    mysql= org_mysql;
 
7971
    rc= mysql_query(mysql, "CREATE TEMPORARY TABLE test_drop_temp_db.t1(c1 int)");
 
7972
    myquery(rc);
 
7973
 
 
7974
    rc= mysql_query(mysql, "CREATE TEMPORARY TABLE test_drop_temp_db.t2 LIKE test_drop_temp_db.t1");
 
7975
    myquery(rc);
 
7976
 
 
7977
    mysql= lmysql;
 
7978
 
 
7979
    rc= mysql_query(mysql, "DROP TABLE t1, t2");
 
7980
    myquery_r(rc);
 
7981
 
 
7982
    rc= mysql_query(mysql, "DROP TEMPORARY TABLE t1");
 
7983
    myquery_r(rc);
 
7984
 
 
7985
    rc= mysql_query(mysql, "DROP TEMPORARY TABLE t2");
 
7986
    myquery_r(rc);
 
7987
 
 
7988
    mysql_close(lmysql);
 
7989
    mysql= org_mysql;
 
7990
 
 
7991
    rc= mysql_query(mysql, "drop database test_drop_temp_db");
 
7992
    myquery(rc);
 
7993
    DIE_UNLESS(1 == mysql_affected_rows(mysql));
 
7994
 
 
7995
    rc= mysql_query(mysql, "delete from mysql.user where User='test_temp'");
 
7996
    myquery(rc);
 
7997
    DIE_UNLESS(1 == mysql_affected_rows(mysql));
 
7998
 
 
7999
 
 
8000
    rc= mysql_query(mysql, "delete from mysql.tables_priv where User='test_temp'");
 
8001
    myquery(rc);
 
8002
    DIE_UNLESS(1 == mysql_affected_rows(mysql));
 
8003
  }
 
8004
}
 
8005
#endif
 
8006
 
 
8007
 
 
8008
/* Test warnings for cuted rows */
 
8009
 
 
8010
static void test_cuted_rows()
 
8011
{
 
8012
  int        rc, count;
 
8013
  MYSQL_RES  *result;
 
8014
 
 
8015
  myheader("test_cuted_rows");
 
8016
 
 
8017
  mysql_query(mysql, "DROP TABLE if exists t1");
 
8018
  mysql_query(mysql, "DROP TABLE if exists t2");
 
8019
 
 
8020
  rc= mysql_query(mysql, "CREATE TABLE t1(c1 tinyint)");
 
8021
  myquery(rc);
 
8022
 
 
8023
  rc= mysql_query(mysql, "CREATE TABLE t2(c1 int not null)");
 
8024
  myquery(rc);
 
8025
 
 
8026
  rc= mysql_query(mysql, "INSERT INTO t1 values(10), (NULL), (NULL)");
 
8027
  myquery(rc);
 
8028
 
 
8029
  count= mysql_warning_count(mysql);
 
8030
  if (!opt_silent)
 
8031
    fprintf(stdout, "\n total warnings: %d", count);
 
8032
  DIE_UNLESS(count == 0);
 
8033
 
 
8034
  rc= mysql_query(mysql, "INSERT INTO t2 SELECT * FROM t1");
 
8035
  myquery(rc);
 
8036
 
 
8037
  count= mysql_warning_count(mysql);
 
8038
  if (!opt_silent)
 
8039
    fprintf(stdout, "\n total warnings: %d", count);
 
8040
  DIE_UNLESS(count == 2);
 
8041
 
 
8042
  rc= mysql_query(mysql, "SHOW WARNINGS");
 
8043
  myquery(rc);
 
8044
 
 
8045
  result= mysql_store_result(mysql);
 
8046
  mytest(result);
 
8047
 
 
8048
  rc= my_process_result_set(result);
 
8049
  DIE_UNLESS(rc == 2);
 
8050
  mysql_free_result(result);
 
8051
 
 
8052
  rc= mysql_query(mysql, "INSERT INTO t1 VALUES('junk'), (876789)");
 
8053
  myquery(rc);
 
8054
 
 
8055
  count= mysql_warning_count(mysql);
 
8056
  if (!opt_silent)
 
8057
    fprintf(stdout, "\n total warnings: %d", count);
 
8058
  DIE_UNLESS(count == 2);
 
8059
 
 
8060
  rc= mysql_query(mysql, "SHOW WARNINGS");
 
8061
  myquery(rc);
 
8062
 
 
8063
  result= mysql_store_result(mysql);
 
8064
  mytest(result);
 
8065
 
 
8066
  rc= my_process_result_set(result);
 
8067
  DIE_UNLESS(rc == 2);
 
8068
  mysql_free_result(result);
 
8069
}
 
8070
 
 
8071
 
 
8072
/* Test update/binary logs */
 
8073
 
 
8074
static void test_logs()
 
8075
{
 
8076
  MYSQL_STMT *stmt;
 
8077
  MYSQL_BIND my_bind[2];
 
8078
  char       data[255];
 
8079
  ulong      length;
 
8080
  int        rc;
 
8081
  short      id;
 
8082
 
 
8083
  myheader("test_logs");
 
8084
 
 
8085
 
 
8086
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_logs");
 
8087
  myquery(rc);
 
8088
 
 
8089
  rc= mysql_query(mysql, "CREATE TABLE test_logs(id smallint, name varchar(20))");
 
8090
  myquery(rc);
 
8091
 
 
8092
  strmov((char *)data, "INSERT INTO test_logs VALUES(?, ?)");
 
8093
  stmt= mysql_simple_prepare(mysql, data);
 
8094
  check_stmt(stmt);
 
8095
 
 
8096
  /*
 
8097
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
8098
    its members.
 
8099
  */
 
8100
  bzero((char*) my_bind, sizeof(my_bind));
 
8101
 
 
8102
  my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
 
8103
  my_bind[0].buffer= (void *)&id;
 
8104
 
 
8105
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
8106
  my_bind[1].buffer= (void *)&data;
 
8107
  my_bind[1].buffer_length= 255;
 
8108
  my_bind[1].length= &length;
 
8109
 
 
8110
  id= 9876;
 
8111
  length= (ulong)(strmov((char *)data, "MySQL - Open Source Database")- data);
 
8112
 
 
8113
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
8114
  check_execute(stmt, rc);
 
8115
 
 
8116
  rc= mysql_stmt_execute(stmt);
 
8117
  check_execute(stmt, rc);
 
8118
 
 
8119
  strmov((char *)data, "'");
 
8120
  length= 1;
 
8121
 
 
8122
  rc= mysql_stmt_execute(stmt);
 
8123
  check_execute(stmt, rc);
 
8124
 
 
8125
  strmov((char *)data, "\"");
 
8126
  length= 1;
 
8127
 
 
8128
  rc= mysql_stmt_execute(stmt);
 
8129
  check_execute(stmt, rc);
 
8130
 
 
8131
  length= (ulong)(strmov((char *)data, "my\'sql\'")-data);
 
8132
  rc= mysql_stmt_execute(stmt);
 
8133
  check_execute(stmt, rc);
 
8134
 
 
8135
  length= (ulong)(strmov((char *)data, "my\"sql\"")-data);
 
8136
  rc= mysql_stmt_execute(stmt);
 
8137
  check_execute(stmt, rc);
 
8138
 
 
8139
  mysql_stmt_close(stmt);
 
8140
 
 
8141
  strmov((char *)data, "INSERT INTO test_logs VALUES(20, 'mysql')");
 
8142
  stmt= mysql_simple_prepare(mysql, data);
 
8143
  check_stmt(stmt);
 
8144
 
 
8145
  rc= mysql_stmt_execute(stmt);
 
8146
  check_execute(stmt, rc);
 
8147
 
 
8148
  rc= mysql_stmt_execute(stmt);
 
8149
  check_execute(stmt, rc);
 
8150
 
 
8151
  mysql_stmt_close(stmt);
 
8152
 
 
8153
  strmov((char *)data, "SELECT * FROM test_logs WHERE id=?");
 
8154
  stmt= mysql_simple_prepare(mysql, data);
 
8155
  check_stmt(stmt);
 
8156
 
 
8157
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
8158
  check_execute(stmt, rc);
 
8159
 
 
8160
  rc= mysql_stmt_execute(stmt);
 
8161
  check_execute(stmt, rc);
 
8162
 
 
8163
  my_bind[1].buffer_length= 255;
 
8164
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
8165
  check_execute(stmt, rc);
 
8166
 
 
8167
  rc= mysql_stmt_fetch(stmt);
 
8168
  check_execute(stmt, rc);
 
8169
 
 
8170
  if (!opt_silent)
 
8171
  {
 
8172
    fprintf(stdout, "id    : %d\n", id);
 
8173
    fprintf(stdout, "name  : %s(%ld)\n", data, length);
 
8174
  }
 
8175
 
 
8176
  DIE_UNLESS(id == 9876);
 
8177
  DIE_UNLESS(length == 19 || length == 20); /* Due to VARCHAR(20) */
 
8178
  DIE_UNLESS(is_prefix(data, "MySQL - Open Source") == 1);
 
8179
 
 
8180
  rc= mysql_stmt_fetch(stmt);
 
8181
  check_execute(stmt, rc);
 
8182
 
 
8183
  if (!opt_silent)
 
8184
    fprintf(stdout, "\n name  : %s(%ld)", data, length);
 
8185
 
 
8186
  DIE_UNLESS(length == 1);
 
8187
  DIE_UNLESS(strcmp(data, "'") == 0);
 
8188
 
 
8189
  rc= mysql_stmt_fetch(stmt);
 
8190
  check_execute(stmt, rc);
 
8191
 
 
8192
  if (!opt_silent)
 
8193
    fprintf(stdout, "\n name  : %s(%ld)", data, length);
 
8194
 
 
8195
  DIE_UNLESS(length == 1);
 
8196
  DIE_UNLESS(strcmp(data, "\"") == 0);
 
8197
 
 
8198
  rc= mysql_stmt_fetch(stmt);
 
8199
  check_execute(stmt, rc);
 
8200
 
 
8201
  if (!opt_silent)
 
8202
    fprintf(stdout, "\n name  : %s(%ld)", data, length);
 
8203
 
 
8204
  DIE_UNLESS(length == 7);
 
8205
  DIE_UNLESS(strcmp(data, "my\'sql\'") == 0);
 
8206
 
 
8207
  rc= mysql_stmt_fetch(stmt);
 
8208
  check_execute(stmt, rc);
 
8209
 
 
8210
  if (!opt_silent)
 
8211
    fprintf(stdout, "\n name  : %s(%ld)", data, length);
 
8212
 
 
8213
  DIE_UNLESS(length == 7);
 
8214
  /*DIE_UNLESS(strcmp(data, "my\"sql\"") == 0); */
 
8215
 
 
8216
  rc= mysql_stmt_fetch(stmt);
 
8217
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
8218
 
 
8219
  mysql_stmt_close(stmt);
 
8220
 
 
8221
  rc= mysql_query(mysql, "DROP TABLE test_logs");
 
8222
  myquery(rc);
 
8223
}
 
8224
 
 
8225
 
 
8226
/* Test 'n' statements create and close */
 
8227
 
 
8228
static void test_nstmts()
 
8229
{
 
8230
  MYSQL_STMT  *stmt;
 
8231
  char        query[255];
 
8232
  int         rc;
 
8233
  static uint i, total_stmts= 2000;
 
8234
  MYSQL_BIND  my_bind[1];
 
8235
 
 
8236
  myheader("test_nstmts");
 
8237
 
 
8238
  mysql_autocommit(mysql, TRUE);
 
8239
 
 
8240
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_nstmts");
 
8241
  myquery(rc);
 
8242
 
 
8243
  rc= mysql_query(mysql, "CREATE TABLE test_nstmts(id int)");
 
8244
  myquery(rc);
 
8245
 
 
8246
  /*
 
8247
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
8248
    its members.
 
8249
  */
 
8250
  bzero((char*) my_bind, sizeof(my_bind));
 
8251
 
 
8252
  my_bind[0].buffer= (void *)&i;
 
8253
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
8254
 
 
8255
  for (i= 0; i < total_stmts; i++)
 
8256
  {
 
8257
    if (!opt_silent)
 
8258
      fprintf(stdout, "\r stmt: %d", i);
 
8259
 
 
8260
    strmov(query, "insert into test_nstmts values(?)");
 
8261
    stmt= mysql_simple_prepare(mysql, query);
 
8262
    check_stmt(stmt);
 
8263
 
 
8264
    rc= mysql_stmt_bind_param(stmt, my_bind);
 
8265
    check_execute(stmt, rc);
 
8266
 
 
8267
    rc= mysql_stmt_execute(stmt);
 
8268
    check_execute(stmt, rc);
 
8269
 
 
8270
    mysql_stmt_close(stmt);
 
8271
  }
 
8272
 
 
8273
  stmt= mysql_simple_prepare(mysql, " select count(*) from test_nstmts");
 
8274
  check_stmt(stmt);
 
8275
 
 
8276
  rc= mysql_stmt_execute(stmt);
 
8277
  check_execute(stmt, rc);
 
8278
 
 
8279
  i= 0;
 
8280
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
8281
  check_execute(stmt, rc);
 
8282
 
 
8283
  rc= mysql_stmt_fetch(stmt);
 
8284
  check_execute(stmt, rc);
 
8285
  if (!opt_silent)
 
8286
    fprintf(stdout, "\n total rows: %d", i);
 
8287
  DIE_UNLESS( i == total_stmts);
 
8288
 
 
8289
  rc= mysql_stmt_fetch(stmt);
 
8290
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
8291
 
 
8292
  mysql_stmt_close(stmt);
 
8293
 
 
8294
  rc= mysql_query(mysql, "DROP TABLE test_nstmts");
 
8295
  myquery(rc);
 
8296
}
 
8297
 
 
8298
 
 
8299
/* Test stmt seek() functions */
 
8300
 
 
8301
static void test_fetch_seek()
 
8302
{
 
8303
  MYSQL_STMT *stmt;
 
8304
  MYSQL_BIND my_bind[3];
 
8305
  MYSQL_ROW_OFFSET row;
 
8306
  int        rc;
 
8307
  int32      c1;
 
8308
  char       c2[11], c3[20];
 
8309
 
 
8310
  myheader("test_fetch_seek");
 
8311
  rc= mysql_query(mysql, "drop table if exists t1");
 
8312
 
 
8313
  myquery(rc);
 
8314
 
 
8315
  rc= mysql_query(mysql, "create table t1(c1 int primary key auto_increment, c2 char(10), c3 timestamp(14))");
 
8316
  myquery(rc);
 
8317
 
 
8318
  rc= mysql_query(mysql, "insert into t1(c2) values('venu'), ('mysql'), ('open'), ('source')");
 
8319
  myquery(rc);
 
8320
 
 
8321
  stmt= mysql_simple_prepare(mysql, "select * from t1");
 
8322
  check_stmt(stmt);
 
8323
 
 
8324
  bzero((char*) my_bind, sizeof(my_bind));
 
8325
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
8326
  my_bind[0].buffer= (void *)&c1;
 
8327
 
 
8328
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
8329
  my_bind[1].buffer= (void *)c2;
 
8330
  my_bind[1].buffer_length= sizeof(c2);
 
8331
 
 
8332
  my_bind[2]= my_bind[1];
 
8333
  my_bind[2].buffer= (void *)c3;
 
8334
  my_bind[2].buffer_length= sizeof(c3);
 
8335
 
 
8336
  rc= mysql_stmt_execute(stmt);
 
8337
  check_execute(stmt, rc);
 
8338
 
 
8339
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
8340
  check_execute(stmt, rc);
 
8341
 
 
8342
  rc= mysql_stmt_store_result(stmt);
 
8343
  check_execute(stmt, rc);
 
8344
 
 
8345
  rc= mysql_stmt_fetch(stmt);
 
8346
  check_execute(stmt, rc);
 
8347
 
 
8348
  if (!opt_silent)
 
8349
    fprintf(stdout, "\n row 0: %ld, %s, %s", (long) c1, c2, c3);
 
8350
 
 
8351
  row= mysql_stmt_row_tell(stmt);
 
8352
 
 
8353
  row= mysql_stmt_row_seek(stmt, row);
 
8354
 
 
8355
  rc= mysql_stmt_fetch(stmt);
 
8356
  check_execute(stmt, rc);
 
8357
 
 
8358
  if (!opt_silent)
 
8359
    fprintf(stdout, "\n row 2: %ld, %s, %s", (long) c1, c2, c3);
 
8360
 
 
8361
  row= mysql_stmt_row_seek(stmt, row);
 
8362
 
 
8363
  rc= mysql_stmt_fetch(stmt);
 
8364
  check_execute(stmt, rc);
 
8365
 
 
8366
  if (!opt_silent)
 
8367
    fprintf(stdout, "\n row 2: %ld, %s, %s", (long) c1, c2, c3);
 
8368
 
 
8369
  mysql_stmt_data_seek(stmt, 0);
 
8370
 
 
8371
  rc= mysql_stmt_fetch(stmt);
 
8372
  check_execute(stmt, rc);
 
8373
 
 
8374
  if (!opt_silent)
 
8375
    fprintf(stdout, "\n row 0: %ld, %s, %s", (long) c1, c2, c3);
 
8376
 
 
8377
  rc= mysql_stmt_fetch(stmt);
 
8378
  check_execute(stmt, rc);
 
8379
 
 
8380
  rc= mysql_stmt_fetch(stmt);
 
8381
  check_execute(stmt, rc);
 
8382
 
 
8383
  rc= mysql_stmt_fetch(stmt);
 
8384
  check_execute(stmt, rc);
 
8385
 
 
8386
  rc= mysql_stmt_fetch(stmt);
 
8387
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
8388
 
 
8389
  mysql_stmt_close(stmt);
 
8390
  myquery(mysql_query(mysql, "drop table t1"));
 
8391
}
 
8392
 
 
8393
 
 
8394
/* Test mysql_stmt_fetch_column() with offset */
 
8395
 
 
8396
static void test_fetch_offset()
 
8397
{
 
8398
  MYSQL_STMT *stmt;
 
8399
  MYSQL_BIND my_bind[1];
 
8400
  char       data[11];
 
8401
  ulong      length;
 
8402
  int        rc;
 
8403
  my_bool    is_null;
 
8404
 
 
8405
 
 
8406
  myheader("test_fetch_offset");
 
8407
 
 
8408
  rc= mysql_query(mysql, "drop table if exists t1");
 
8409
  myquery(rc);
 
8410
 
 
8411
  rc= mysql_query(mysql, "create table t1(a char(10))");
 
8412
  myquery(rc);
 
8413
 
 
8414
  rc= mysql_query(mysql, "insert into t1 values('abcdefghij'), (null)");
 
8415
  myquery(rc);
 
8416
 
 
8417
  stmt= mysql_simple_prepare(mysql, "select * from t1");
 
8418
  check_stmt(stmt);
 
8419
 
 
8420
  bzero((char*) my_bind, sizeof(my_bind));
 
8421
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
8422
  my_bind[0].buffer= (void *)data;
 
8423
  my_bind[0].buffer_length= 11;
 
8424
  my_bind[0].is_null= &is_null;
 
8425
  my_bind[0].length= &length;
 
8426
 
 
8427
  rc= mysql_stmt_execute(stmt);
 
8428
  check_execute(stmt, rc);
 
8429
 
 
8430
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
 
8431
  check_execute_r(stmt, rc);
 
8432
 
 
8433
  rc= mysql_stmt_execute(stmt);
 
8434
  check_execute(stmt, rc);
 
8435
 
 
8436
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
8437
  check_execute(stmt, rc);
 
8438
 
 
8439
  rc= mysql_stmt_store_result(stmt);
 
8440
  check_execute(stmt, rc);
 
8441
 
 
8442
  rc= mysql_stmt_fetch(stmt);
 
8443
  check_execute(stmt, rc);
 
8444
 
 
8445
  data[0]= '\0';
 
8446
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
 
8447
  check_execute(stmt, rc);
 
8448
  if (!opt_silent)
 
8449
    fprintf(stdout, "\n col 1: %s (%ld)", data, length);
 
8450
  DIE_UNLESS(strncmp(data, "abcd", 4) == 0 && length == 10);
 
8451
 
 
8452
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 5);
 
8453
  check_execute(stmt, rc);
 
8454
  if (!opt_silent)
 
8455
    fprintf(stdout, "\n col 1: %s (%ld)", data, length);
 
8456
  DIE_UNLESS(strncmp(data, "fg", 2) == 0 && length == 10);
 
8457
 
 
8458
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 9);
 
8459
  check_execute(stmt, rc);
 
8460
  if (!opt_silent)
 
8461
    fprintf(stdout, "\n col 0: %s (%ld)", data, length);
 
8462
  DIE_UNLESS(strncmp(data, "j", 1) == 0 && length == 10);
 
8463
 
 
8464
  rc= mysql_stmt_fetch(stmt);
 
8465
  check_execute(stmt, rc);
 
8466
 
 
8467
  is_null= 0;
 
8468
 
 
8469
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
 
8470
  check_execute(stmt, rc);
 
8471
 
 
8472
  DIE_UNLESS(is_null == 1);
 
8473
 
 
8474
  rc= mysql_stmt_fetch(stmt);
 
8475
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
8476
 
 
8477
  rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
 
8478
  check_execute_r(stmt, rc);
 
8479
 
 
8480
  mysql_stmt_close(stmt);
 
8481
 
 
8482
  myquery(mysql_query(mysql, "drop table t1"));
 
8483
}
 
8484
 
 
8485
 
 
8486
/* Test mysql_stmt_fetch_column() */
 
8487
 
 
8488
static void test_fetch_column()
 
8489
{
 
8490
  MYSQL_STMT *stmt;
 
8491
  MYSQL_BIND my_bind[2];
 
8492
  char       c2[20], bc2[20];
 
8493
  ulong      l1, l2, bl1, bl2;
 
8494
  int        rc, c1, bc1;
 
8495
 
 
8496
  myheader("test_fetch_column");
 
8497
 
 
8498
  rc= mysql_query(mysql, "drop table if exists t1");
 
8499
  myquery(rc);
 
8500
 
 
8501
  rc= mysql_query(mysql, "create table t1(c1 int primary key auto_increment, c2 char(10))");
 
8502
  myquery(rc);
 
8503
 
 
8504
  rc= mysql_query(mysql, "insert into t1(c2) values('venu'), ('mysql')");
 
8505
  myquery(rc);
 
8506
 
 
8507
  stmt= mysql_simple_prepare(mysql, "select * from t1 order by c2 desc");
 
8508
  check_stmt(stmt);
 
8509
 
 
8510
  bzero((char*) my_bind, sizeof(my_bind));
 
8511
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
8512
  my_bind[0].buffer= (void *)&bc1;
 
8513
  my_bind[0].buffer_length= 0;
 
8514
  my_bind[0].is_null= 0;
 
8515
  my_bind[0].length= &bl1;
 
8516
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
8517
  my_bind[1].buffer= (void *)bc2;
 
8518
  my_bind[1].buffer_length= 7;
 
8519
  my_bind[1].is_null= 0;
 
8520
  my_bind[1].length= &bl2;
 
8521
 
 
8522
  rc= mysql_stmt_execute(stmt);
 
8523
  check_execute(stmt, rc);
 
8524
 
 
8525
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
8526
  check_execute(stmt, rc);
 
8527
 
 
8528
  rc= mysql_stmt_store_result(stmt);
 
8529
  check_execute(stmt, rc);
 
8530
 
 
8531
  rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0); /* No-op at this point */
 
8532
  check_execute_r(stmt, rc);
 
8533
 
 
8534
  rc= mysql_stmt_fetch(stmt);
 
8535
  check_execute(stmt, rc);
 
8536
 
 
8537
  if (!opt_silent)
 
8538
    fprintf(stdout, "\n row 0: %d, %s", bc1, bc2);
 
8539
 
 
8540
  c2[0]= '\0'; l2= 0;
 
8541
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
8542
  my_bind[0].buffer= (void *)c2;
 
8543
  my_bind[0].buffer_length= 7;
 
8544
  my_bind[0].is_null= 0;
 
8545
  my_bind[0].length= &l2;
 
8546
 
 
8547
  rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
 
8548
  check_execute(stmt, rc);
 
8549
  if (!opt_silent)
 
8550
    fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
 
8551
  DIE_UNLESS(strncmp(c2, "venu", 4) == 0 && l2 == 4);
 
8552
 
 
8553
  c2[0]= '\0'; l2= 0;
 
8554
  rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
 
8555
  check_execute(stmt, rc);
 
8556
  if (!opt_silent)
 
8557
    fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
 
8558
  DIE_UNLESS(strcmp(c2, "venu") == 0 && l2 == 4);
 
8559
 
 
8560
  c1= 0;
 
8561
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
8562
  my_bind[0].buffer= (void *)&c1;
 
8563
  my_bind[0].buffer_length= 0;
 
8564
  my_bind[0].is_null= 0;
 
8565
  my_bind[0].length= &l1;
 
8566
 
 
8567
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
 
8568
  check_execute(stmt, rc);
 
8569
  if (!opt_silent)
 
8570
    fprintf(stdout, "\n col 0: %d(%ld)", c1, l1);
 
8571
  DIE_UNLESS(c1 == 1 && l1 == 4);
 
8572
 
 
8573
  rc= mysql_stmt_fetch(stmt);
 
8574
  check_execute(stmt, rc);
 
8575
 
 
8576
  if (!opt_silent)
 
8577
    fprintf(stdout, "\n row 1: %d, %s", bc1, bc2);
 
8578
 
 
8579
  c2[0]= '\0'; l2= 0;
 
8580
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
8581
  my_bind[0].buffer= (void *)c2;
 
8582
  my_bind[0].buffer_length= 7;
 
8583
  my_bind[0].is_null= 0;
 
8584
  my_bind[0].length= &l2;
 
8585
 
 
8586
  rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
 
8587
  check_execute(stmt, rc);
 
8588
  if (!opt_silent)
 
8589
    fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
 
8590
  DIE_UNLESS(strncmp(c2, "mysq", 4) == 0 && l2 == 5);
 
8591
 
 
8592
  c2[0]= '\0'; l2= 0;
 
8593
  rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
 
8594
  check_execute(stmt, rc);
 
8595
  if (!opt_silent)
 
8596
    fprintf(stdout, "\n col 1: %si(%ld)", c2, l2);
 
8597
  DIE_UNLESS(strcmp(c2, "mysql") == 0 && l2 == 5);
 
8598
 
 
8599
  c1= 0;
 
8600
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
8601
  my_bind[0].buffer= (void *)&c1;
 
8602
  my_bind[0].buffer_length= 0;
 
8603
  my_bind[0].is_null= 0;
 
8604
  my_bind[0].length= &l1;
 
8605
 
 
8606
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
 
8607
  check_execute(stmt, rc);
 
8608
  if (!opt_silent)
 
8609
    fprintf(stdout, "\n col 0: %d(%ld)", c1, l1);
 
8610
  DIE_UNLESS(c1 == 2 && l1 == 4);
 
8611
 
 
8612
  rc= mysql_stmt_fetch(stmt);
 
8613
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
8614
 
 
8615
  rc= mysql_stmt_fetch_column(stmt, my_bind, 1, 0);
 
8616
  check_execute_r(stmt, rc);
 
8617
 
 
8618
  mysql_stmt_close(stmt);
 
8619
  myquery(mysql_query(mysql, "drop table t1"));
 
8620
}
 
8621
 
 
8622
 
 
8623
/* Test mysql_list_fields() */
 
8624
 
 
8625
static void test_list_fields()
 
8626
{
 
8627
  MYSQL_RES *result;
 
8628
  int rc;
 
8629
  myheader("test_list_fields");
 
8630
 
 
8631
  rc= mysql_query(mysql, "drop table if exists t1");
 
8632
  myquery(rc);
 
8633
 
 
8634
  rc= mysql_query(mysql, "create table t1(c1 int primary key auto_increment, c2 char(10) default 'mysql')");
 
8635
  myquery(rc);
 
8636
 
 
8637
  result= mysql_list_fields(mysql, "t1", NULL);
 
8638
  mytest(result);
 
8639
 
 
8640
  rc= my_process_result_set(result);
 
8641
  DIE_UNLESS(rc == 0);
 
8642
 
 
8643
  verify_prepare_field(result, 0, "c1", "c1", MYSQL_TYPE_LONG,
 
8644
                       "t1", "t1",
 
8645
                       current_db, 11, "0");
 
8646
 
 
8647
  verify_prepare_field(result, 1, "c2", "c2", MYSQL_TYPE_STRING,
 
8648
                       "t1", "t1",
 
8649
                       current_db, 10, "mysql");
 
8650
 
 
8651
  mysql_free_result(result);
 
8652
  myquery(mysql_query(mysql, "drop table t1"));
 
8653
}
 
8654
 
 
8655
 
 
8656
static void test_bug19671()
 
8657
{
 
8658
  MYSQL_RES *result;
 
8659
  int rc;
 
8660
  myheader("test_bug19671");
 
8661
 
 
8662
  mysql_query(mysql, "set sql_mode=''");
 
8663
  rc= mysql_query(mysql, "drop table if exists t1");
 
8664
  myquery(rc);
 
8665
 
 
8666
  rc= mysql_query(mysql, "drop view if exists v1");
 
8667
  myquery(rc);
 
8668
 
 
8669
  rc= mysql_query(mysql, "create table t1(f1 int)");
 
8670
  myquery(rc);
 
8671
 
 
8672
  rc= mysql_query(mysql, "create view v1 as select va.* from t1 va");
 
8673
  myquery(rc);
 
8674
 
 
8675
  result= mysql_list_fields(mysql, "v1", NULL);
 
8676
  mytest(result);
 
8677
 
 
8678
  rc= my_process_result_set(result);
 
8679
  DIE_UNLESS(rc == 0);
 
8680
 
 
8681
  verify_prepare_field(result, 0, "f1", "f1", MYSQL_TYPE_LONG,
 
8682
                       "v1", "v1", current_db, 11, "0");
 
8683
 
 
8684
  mysql_free_result(result);
 
8685
  myquery(mysql_query(mysql, "drop view v1"));
 
8686
  myquery(mysql_query(mysql, "drop table t1"));
 
8687
}
 
8688
 
 
8689
 
 
8690
/* Test a memory ovverun bug */
 
8691
 
 
8692
static void test_mem_overun()
 
8693
{
 
8694
  char       buffer[10000], field[10];
 
8695
  MYSQL_STMT *stmt;
 
8696
  MYSQL_RES  *field_res;
 
8697
  int        rc, i, length;
 
8698
 
 
8699
  myheader("test_mem_overun");
 
8700
 
 
8701
  /*
 
8702
    Test a memory ovverun bug when a table had 1000 fields with
 
8703
    a row of data
 
8704
  */
 
8705
  rc= mysql_query(mysql, "drop table if exists t_mem_overun");
 
8706
  myquery(rc);
 
8707
 
 
8708
  strxmov(buffer, "create table t_mem_overun(", NullS);
 
8709
  for (i= 0; i < 1000; i++)
 
8710
  {
 
8711
    sprintf(field, "c%d int", i);
 
8712
    strxmov(buffer, buffer, field, ", ", NullS);
 
8713
  }
 
8714
  length= strlen(buffer);
 
8715
  buffer[length-2]= ')';
 
8716
  buffer[--length]= '\0';
 
8717
 
 
8718
  rc= mysql_real_query(mysql, buffer, length);
 
8719
  myquery(rc);
 
8720
 
 
8721
  strxmov(buffer, "insert into t_mem_overun values(", NullS);
 
8722
  for (i= 0; i < 1000; i++)
 
8723
  {
 
8724
    strxmov(buffer, buffer, "1, ", NullS);
 
8725
  }
 
8726
  length= strlen(buffer);
 
8727
  buffer[length-2]= ')';
 
8728
  buffer[--length]= '\0';
 
8729
 
 
8730
  rc= mysql_real_query(mysql, buffer, length);
 
8731
  myquery(rc);
 
8732
 
 
8733
  rc= mysql_query(mysql, "select * from t_mem_overun");
 
8734
  myquery(rc);
 
8735
 
 
8736
  rc= my_process_result(mysql);
 
8737
  DIE_UNLESS(rc == 1);
 
8738
 
 
8739
  stmt= mysql_simple_prepare(mysql, "select * from t_mem_overun");
 
8740
  check_stmt(stmt);
 
8741
 
 
8742
  rc= mysql_stmt_execute(stmt);
 
8743
  check_execute(stmt, rc);
 
8744
 
 
8745
  field_res= mysql_stmt_result_metadata(stmt);
 
8746
  mytest(field_res);
 
8747
 
 
8748
  if (!opt_silent)
 
8749
    fprintf(stdout, "\n total fields : %d", mysql_num_fields(field_res));
 
8750
  DIE_UNLESS( 1000 == mysql_num_fields(field_res));
 
8751
 
 
8752
  rc= mysql_stmt_store_result(stmt);
 
8753
  check_execute(stmt, rc);
 
8754
 
 
8755
  rc= mysql_stmt_fetch(stmt);
 
8756
  check_execute(stmt, rc);
 
8757
 
 
8758
  rc= mysql_stmt_fetch(stmt);
 
8759
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
8760
 
 
8761
  mysql_free_result(field_res);
 
8762
 
 
8763
  mysql_stmt_close(stmt);
 
8764
}
 
8765
 
 
8766
 
 
8767
/* Test mysql_stmt_free_result() */
 
8768
 
 
8769
static void test_free_result()
 
8770
{
 
8771
  MYSQL_STMT *stmt;
 
8772
  MYSQL_BIND my_bind[1];
 
8773
  char       c2[5];
 
8774
  ulong      bl1, l2;
 
8775
  int        rc, c1, bc1;
 
8776
 
 
8777
  myheader("test_free_result");
 
8778
 
 
8779
  rc= mysql_query(mysql, "drop table if exists test_free_result");
 
8780
  myquery(rc);
 
8781
 
 
8782
  rc= mysql_query(mysql, "create table test_free_result("
 
8783
                         "c1 int primary key auto_increment)");
 
8784
  myquery(rc);
 
8785
 
 
8786
  rc= mysql_query(mysql, "insert into test_free_result values(), (), ()");
 
8787
  myquery(rc);
 
8788
 
 
8789
  stmt= mysql_simple_prepare(mysql, "select * from test_free_result");
 
8790
  check_stmt(stmt);
 
8791
 
 
8792
  bzero((char*) my_bind, sizeof(my_bind));
 
8793
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
8794
  my_bind[0].buffer= (void *)&bc1;
 
8795
  my_bind[0].length= &bl1;
 
8796
 
 
8797
  rc= mysql_stmt_execute(stmt);
 
8798
  check_execute(stmt, rc);
 
8799
 
 
8800
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
8801
  check_execute(stmt, rc);
 
8802
 
 
8803
  rc= mysql_stmt_fetch(stmt);
 
8804
  check_execute(stmt, rc);
 
8805
 
 
8806
  c2[0]= '\0'; l2= 0;
 
8807
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
8808
  my_bind[0].buffer= (void *)c2;
 
8809
  my_bind[0].buffer_length= 7;
 
8810
  my_bind[0].is_null= 0;
 
8811
  my_bind[0].length= &l2;
 
8812
 
 
8813
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
 
8814
  check_execute(stmt, rc);
 
8815
  if (!opt_silent)
 
8816
    fprintf(stdout, "\n col 0: %s(%ld)", c2, l2);
 
8817
  DIE_UNLESS(strncmp(c2, "1", 1) == 0 && l2 == 1);
 
8818
 
 
8819
  rc= mysql_stmt_fetch(stmt);
 
8820
  check_execute(stmt, rc);
 
8821
 
 
8822
  c1= 0, l2= 0;
 
8823
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
8824
  my_bind[0].buffer= (void *)&c1;
 
8825
  my_bind[0].buffer_length= 0;
 
8826
  my_bind[0].is_null= 0;
 
8827
  my_bind[0].length= &l2;
 
8828
 
 
8829
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
 
8830
  check_execute(stmt, rc);
 
8831
  if (!opt_silent)
 
8832
    fprintf(stdout, "\n col 0: %d(%ld)", c1, l2);
 
8833
  DIE_UNLESS(c1 == 2 && l2 == 4);
 
8834
 
 
8835
  rc= mysql_query(mysql, "drop table test_free_result");
 
8836
  myquery_r(rc); /* error should be, COMMANDS OUT OF SYNC */
 
8837
 
 
8838
  rc= mysql_stmt_free_result(stmt);
 
8839
  check_execute(stmt, rc);
 
8840
 
 
8841
  rc= mysql_query(mysql, "drop table test_free_result");
 
8842
  myquery(rc);  /* should be successful */
 
8843
 
 
8844
  mysql_stmt_close(stmt);
 
8845
}
 
8846
 
 
8847
 
 
8848
/* Test mysql_stmt_store_result() */
 
8849
 
 
8850
static void test_free_store_result()
 
8851
{
 
8852
  MYSQL_STMT *stmt;
 
8853
  MYSQL_BIND my_bind[1];
 
8854
  char       c2[5];
 
8855
  ulong      bl1, l2;
 
8856
  int        rc, c1, bc1;
 
8857
 
 
8858
  myheader("test_free_store_result");
 
8859
 
 
8860
  rc= mysql_query(mysql, "drop table if exists test_free_result");
 
8861
  myquery(rc);
 
8862
 
 
8863
  rc= mysql_query(mysql, "create table test_free_result(c1 int primary key auto_increment)");
 
8864
  myquery(rc);
 
8865
 
 
8866
  rc= mysql_query(mysql, "insert into test_free_result values(), (), ()");
 
8867
  myquery(rc);
 
8868
 
 
8869
  stmt= mysql_simple_prepare(mysql, "select * from test_free_result");
 
8870
  check_stmt(stmt);
 
8871
 
 
8872
  bzero((char*) my_bind, sizeof(my_bind));
 
8873
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
8874
  my_bind[0].buffer= (void *)&bc1;
 
8875
  my_bind[0].buffer_length= 0;
 
8876
  my_bind[0].is_null= 0;
 
8877
  my_bind[0].length= &bl1;
 
8878
 
 
8879
  rc= mysql_stmt_execute(stmt);
 
8880
  check_execute(stmt, rc);
 
8881
 
 
8882
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
8883
  check_execute(stmt, rc);
 
8884
 
 
8885
  rc= mysql_stmt_store_result(stmt);
 
8886
  check_execute(stmt, rc);
 
8887
 
 
8888
  rc= mysql_stmt_fetch(stmt);
 
8889
  check_execute(stmt, rc);
 
8890
 
 
8891
  c2[0]= '\0'; l2= 0;
 
8892
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
8893
  my_bind[0].buffer= (void *)c2;
 
8894
  my_bind[0].buffer_length= 7;
 
8895
  my_bind[0].is_null= 0;
 
8896
  my_bind[0].length= &l2;
 
8897
 
 
8898
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
 
8899
  check_execute(stmt, rc);
 
8900
  if (!opt_silent)
 
8901
    fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
 
8902
  DIE_UNLESS(strncmp(c2, "1", 1) == 0 && l2 == 1);
 
8903
 
 
8904
  rc= mysql_stmt_fetch(stmt);
 
8905
  check_execute(stmt, rc);
 
8906
 
 
8907
  c1= 0, l2= 0;
 
8908
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
8909
  my_bind[0].buffer= (void *)&c1;
 
8910
  my_bind[0].buffer_length= 0;
 
8911
  my_bind[0].is_null= 0;
 
8912
  my_bind[0].length= &l2;
 
8913
 
 
8914
  rc= mysql_stmt_fetch_column(stmt, my_bind, 0, 0);
 
8915
  check_execute(stmt, rc);
 
8916
  if (!opt_silent)
 
8917
    fprintf(stdout, "\n col 0: %d(%ld)", c1, l2);
 
8918
  DIE_UNLESS(c1 == 2 && l2 == 4);
 
8919
 
 
8920
  rc= mysql_stmt_free_result(stmt);
 
8921
  check_execute(stmt, rc);
 
8922
 
 
8923
  rc= mysql_query(mysql, "drop table test_free_result");
 
8924
  myquery(rc);
 
8925
 
 
8926
  mysql_stmt_close(stmt);
 
8927
}
 
8928
 
 
8929
 
 
8930
/* Test SQLmode */
 
8931
 
 
8932
static void test_sqlmode()
 
8933
{
 
8934
  MYSQL_STMT *stmt;
 
8935
  MYSQL_BIND my_bind[2];
 
8936
  char       c1[5], c2[5];
 
8937
  int        rc;
 
8938
  char query[MAX_TEST_QUERY_LENGTH];
 
8939
 
 
8940
  myheader("test_sqlmode");
 
8941
 
 
8942
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_piping");
 
8943
  myquery(rc);
 
8944
 
 
8945
  rc= mysql_query(mysql, "CREATE TABLE test_piping(name varchar(10))");
 
8946
  myquery(rc);
 
8947
 
 
8948
  /* PIPES_AS_CONCAT */
 
8949
  strmov(query, "SET SQL_MODE= \"PIPES_AS_CONCAT\"");
 
8950
  if (!opt_silent)
 
8951
    fprintf(stdout, "\n With %s", query);
 
8952
  rc= mysql_query(mysql, query);
 
8953
  myquery(rc);
 
8954
 
 
8955
  strmov(query, "INSERT INTO test_piping VALUES(?||?)");
 
8956
  if (!opt_silent)
 
8957
    fprintf(stdout, "\n  query: %s", query);
 
8958
  stmt= mysql_simple_prepare(mysql, query);
 
8959
  check_stmt(stmt);
 
8960
 
 
8961
  if (!opt_silent)
 
8962
    fprintf(stdout, "\n  total parameters: %ld", mysql_stmt_param_count(stmt));
 
8963
 
 
8964
  /*
 
8965
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
8966
    its members.
 
8967
  */
 
8968
  bzero((char*) my_bind, sizeof(my_bind));
 
8969
 
 
8970
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
8971
  my_bind[0].buffer= (void *)c1;
 
8972
  my_bind[0].buffer_length= 2;
 
8973
 
 
8974
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
8975
  my_bind[1].buffer= (void *)c2;
 
8976
  my_bind[1].buffer_length= 3;
 
8977
 
 
8978
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
8979
  check_execute(stmt, rc);
 
8980
 
 
8981
  strmov(c1, "My"); strmov(c2, "SQL");
 
8982
  rc= mysql_stmt_execute(stmt);
 
8983
  check_execute(stmt, rc);
 
8984
  mysql_stmt_close(stmt);
 
8985
 
 
8986
  verify_col_data("test_piping", "name", "MySQL");
 
8987
 
 
8988
  rc= mysql_query(mysql, "DELETE FROM test_piping");
 
8989
  myquery(rc);
 
8990
 
 
8991
  strmov(query, "SELECT connection_id    ()");
 
8992
  if (!opt_silent)
 
8993
    fprintf(stdout, "\n  query: %s", query);
 
8994
  stmt= mysql_simple_prepare(mysql, query);
 
8995
  check_stmt(stmt);
 
8996
  mysql_stmt_close(stmt);
 
8997
 
 
8998
  /* ANSI */
 
8999
  strmov(query, "SET SQL_MODE= \"ANSI\"");
 
9000
  if (!opt_silent)
 
9001
    fprintf(stdout, "\n With %s", query);
 
9002
  rc= mysql_query(mysql, query);
 
9003
  myquery(rc);
 
9004
 
 
9005
  strmov(query, "INSERT INTO test_piping VALUES(?||?)");
 
9006
  if (!opt_silent)
 
9007
    fprintf(stdout, "\n  query: %s", query);
 
9008
  stmt= mysql_simple_prepare(mysql, query);
 
9009
  check_stmt(stmt);
 
9010
  if (!opt_silent)
 
9011
    fprintf(stdout, "\n  total parameters: %ld", mysql_stmt_param_count(stmt));
 
9012
 
 
9013
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
9014
  check_execute(stmt, rc);
 
9015
 
 
9016
  strmov(c1, "My"); strmov(c2, "SQL");
 
9017
  rc= mysql_stmt_execute(stmt);
 
9018
  check_execute(stmt, rc);
 
9019
 
 
9020
  mysql_stmt_close(stmt);
 
9021
  verify_col_data("test_piping", "name", "MySQL");
 
9022
 
 
9023
  /* ANSI mode spaces ... */
 
9024
  strmov(query, "SELECT connection_id    ()");
 
9025
  if (!opt_silent)
 
9026
    fprintf(stdout, "\n  query: %s", query);
 
9027
  stmt= mysql_simple_prepare(mysql, query);
 
9028
  check_stmt(stmt);
 
9029
 
 
9030
  rc= mysql_stmt_execute(stmt);
 
9031
  check_execute(stmt, rc);
 
9032
 
 
9033
  rc= mysql_stmt_fetch(stmt);
 
9034
  check_execute(stmt, rc);
 
9035
 
 
9036
  rc= mysql_stmt_fetch(stmt);
 
9037
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
9038
  if (!opt_silent)
 
9039
    fprintf(stdout, "\n  returned 1 row\n");
 
9040
 
 
9041
  mysql_stmt_close(stmt);
 
9042
 
 
9043
  /* IGNORE SPACE MODE */
 
9044
  strmov(query, "SET SQL_MODE= \"IGNORE_SPACE\"");
 
9045
  if (!opt_silent)
 
9046
    fprintf(stdout, "\n With %s", query);
 
9047
  rc= mysql_query(mysql, query);
 
9048
  myquery(rc);
 
9049
 
 
9050
  strmov(query, "SELECT connection_id    ()");
 
9051
  if (!opt_silent)
 
9052
    fprintf(stdout, "\n  query: %s", query);
 
9053
  stmt= mysql_simple_prepare(mysql, query);
 
9054
  check_stmt(stmt);
 
9055
 
 
9056
  rc= mysql_stmt_execute(stmt);
 
9057
  check_execute(stmt, rc);
 
9058
 
 
9059
  rc= mysql_stmt_fetch(stmt);
 
9060
  check_execute(stmt, rc);
 
9061
 
 
9062
  rc= mysql_stmt_fetch(stmt);
 
9063
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
9064
  if (!opt_silent)
 
9065
    fprintf(stdout, "\n  returned 1 row");
 
9066
 
 
9067
  mysql_stmt_close(stmt);
 
9068
}
 
9069
 
 
9070
 
 
9071
/* Test for timestamp handling */
 
9072
 
 
9073
static void test_ts()
 
9074
{
 
9075
  MYSQL_STMT *stmt;
 
9076
  MYSQL_BIND my_bind[6];
 
9077
  MYSQL_TIME ts;
 
9078
  MYSQL_RES  *prep_res;
 
9079
  char       strts[30];
 
9080
  ulong      length;
 
9081
  int        rc, field_count;
 
9082
  char       name;
 
9083
  char query[MAX_TEST_QUERY_LENGTH];
 
9084
  const char *queries [3]= {"SELECT a, b, c FROM test_ts WHERE %c=?",
 
9085
                            "SELECT a, b, c FROM test_ts WHERE %c=?",
 
9086
                            "SELECT a, b, c FROM test_ts WHERE %c=CAST(? AS DATE)"};
 
9087
  myheader("test_ts");
 
9088
 
 
9089
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_ts");
 
9090
  myquery(rc);
 
9091
 
 
9092
  rc= mysql_query(mysql, "CREATE TABLE test_ts(a DATE, b TIME, c TIMESTAMP)");
 
9093
  myquery(rc);
 
9094
 
 
9095
  stmt= mysql_simple_prepare(mysql, "INSERT INTO test_ts VALUES(?, ?, ?), (?, ?, ?)");
 
9096
  check_stmt(stmt);
 
9097
 
 
9098
  ts.year= 2003;
 
9099
  ts.month= 07;
 
9100
  ts.day= 12;
 
9101
  ts.hour= 21;
 
9102
  ts.minute= 07;
 
9103
  ts.second= 46;
 
9104
  ts.second_part= 0;
 
9105
  length= (long)(strmov(strts, "2003-07-12 21:07:46") - strts);
 
9106
 
 
9107
  /*
 
9108
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
9109
    its members.
 
9110
  */
 
9111
  bzero((char*) my_bind, sizeof(my_bind));
 
9112
 
 
9113
  my_bind[0].buffer_type= MYSQL_TYPE_TIMESTAMP;
 
9114
  my_bind[0].buffer= (void *)&ts;
 
9115
  my_bind[0].buffer_length= sizeof(ts);
 
9116
 
 
9117
  my_bind[2]= my_bind[1]= my_bind[0];
 
9118
 
 
9119
  my_bind[3].buffer_type= MYSQL_TYPE_STRING;
 
9120
  my_bind[3].buffer= (void *)strts;
 
9121
  my_bind[3].buffer_length= sizeof(strts);
 
9122
  my_bind[3].length= &length;
 
9123
 
 
9124
  my_bind[5]= my_bind[4]= my_bind[3];
 
9125
 
 
9126
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
9127
  check_execute(stmt, rc);
 
9128
 
 
9129
  rc= mysql_stmt_execute(stmt);
 
9130
  check_execute(stmt, rc);
 
9131
 
 
9132
  mysql_stmt_close(stmt);
 
9133
 
 
9134
  verify_col_data("test_ts", "a", "2003-07-12");
 
9135
  verify_col_data("test_ts", "b", "21:07:46");
 
9136
  verify_col_data("test_ts", "c", "2003-07-12 21:07:46");
 
9137
 
 
9138
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM test_ts");
 
9139
  check_stmt(stmt);
 
9140
 
 
9141
  prep_res= mysql_stmt_result_metadata(stmt);
 
9142
  mytest(prep_res);
 
9143
 
 
9144
  rc= mysql_stmt_execute(stmt);
 
9145
  check_execute(stmt, rc);
 
9146
 
 
9147
  rc= my_process_stmt_result(stmt);
 
9148
  DIE_UNLESS(rc == 2);
 
9149
  field_count= mysql_num_fields(prep_res);
 
9150
 
 
9151
  mysql_free_result(prep_res);
 
9152
  mysql_stmt_close(stmt);
 
9153
 
 
9154
  for (name= 'a'; field_count--; name++)
 
9155
  {
 
9156
    int row_count= 0;
 
9157
 
 
9158
    sprintf(query, queries[field_count], name);
 
9159
 
 
9160
    if (!opt_silent)
 
9161
      fprintf(stdout, "\n  %s", query);
 
9162
    stmt= mysql_simple_prepare(mysql, query);
 
9163
    check_stmt(stmt);
 
9164
 
 
9165
    rc= mysql_stmt_bind_param(stmt, my_bind);
 
9166
    check_execute(stmt, rc);
 
9167
 
 
9168
    rc= mysql_stmt_execute(stmt);
 
9169
    check_execute(stmt, rc);
 
9170
 
 
9171
    while (mysql_stmt_fetch(stmt) == 0)
 
9172
      row_count++;
 
9173
 
 
9174
    if (!opt_silent)
 
9175
      fprintf(stdout, "\n   returned '%d' rows", row_count);
 
9176
    DIE_UNLESS(row_count == 2);
 
9177
    mysql_stmt_close(stmt);
 
9178
  }
 
9179
}
 
9180
 
 
9181
 
 
9182
/* Test for bug #1500. */
 
9183
 
 
9184
static void test_bug1500()
 
9185
{
 
9186
  MYSQL_STMT *stmt;
 
9187
  MYSQL_BIND my_bind[3];
 
9188
  int        rc;
 
9189
  int32 int_data[3]= {2, 3, 4};
 
9190
  const char *data;
 
9191
 
 
9192
  myheader("test_bug1500");
 
9193
 
 
9194
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bg1500");
 
9195
  myquery(rc);
 
9196
 
 
9197
  rc= mysql_query(mysql, "CREATE TABLE test_bg1500 (i INT)");
 
9198
  myquery(rc);
 
9199
 
 
9200
  rc= mysql_query(mysql, "INSERT INTO test_bg1500 VALUES (1), (2)");
 
9201
  myquery(rc);
 
9202
 
 
9203
  rc= mysql_commit(mysql);
 
9204
  myquery(rc);
 
9205
 
 
9206
  stmt= mysql_simple_prepare(mysql, "SELECT i FROM test_bg1500 WHERE i IN (?, ?, ?)");
 
9207
  check_stmt(stmt);
 
9208
  verify_param_count(stmt, 3);
 
9209
 
 
9210
  /*
 
9211
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
9212
    its members.
 
9213
  */
 
9214
  bzero((char*) my_bind, sizeof(my_bind));
 
9215
 
 
9216
  my_bind[0].buffer= (void *)int_data;
 
9217
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
9218
  my_bind[2]= my_bind[1]= my_bind[0];
 
9219
  my_bind[1].buffer= (void *)(int_data + 1);
 
9220
  my_bind[2].buffer= (void *)(int_data + 2);
 
9221
 
 
9222
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
9223
  check_execute(stmt, rc);
 
9224
 
 
9225
  rc= mysql_stmt_execute(stmt);
 
9226
  check_execute(stmt, rc);
 
9227
 
 
9228
  rc= my_process_stmt_result(stmt);
 
9229
  DIE_UNLESS(rc == 1);
 
9230
 
 
9231
  mysql_stmt_close(stmt);
 
9232
 
 
9233
  rc= mysql_query(mysql, "DROP TABLE test_bg1500");
 
9234
  myquery(rc);
 
9235
 
 
9236
  rc= mysql_query(mysql, "CREATE TABLE test_bg1500 (s VARCHAR(25), FULLTEXT(s)) engine=MyISAM");
 
9237
  myquery(rc);
 
9238
 
 
9239
  rc= mysql_query(mysql,
 
9240
        "INSERT INTO test_bg1500 VALUES ('Gravedigger'), ('Greed'), ('Hollow Dogs')");
 
9241
  myquery(rc);
 
9242
 
 
9243
  rc= mysql_commit(mysql);
 
9244
  myquery(rc);
 
9245
 
 
9246
  stmt= mysql_simple_prepare(mysql,
 
9247
          "SELECT s FROM test_bg1500 WHERE MATCH (s) AGAINST (?)");
 
9248
  check_stmt(stmt);
 
9249
 
 
9250
  verify_param_count(stmt, 1);
 
9251
 
 
9252
  data= "Dogs";
 
9253
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
9254
  my_bind[0].buffer= (void *) data;
 
9255
  my_bind[0].buffer_length= strlen(data);
 
9256
  my_bind[0].is_null= 0;
 
9257
  my_bind[0].length= 0;
 
9258
 
 
9259
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
9260
  check_execute(stmt, rc);
 
9261
 
 
9262
  rc= mysql_stmt_execute(stmt);
 
9263
  check_execute(stmt, rc);
 
9264
 
 
9265
  rc= my_process_stmt_result(stmt);
 
9266
  DIE_UNLESS(rc == 1);
 
9267
 
 
9268
  mysql_stmt_close(stmt);
 
9269
 
 
9270
  /* This should work too */
 
9271
  stmt= mysql_simple_prepare(mysql,
 
9272
          "SELECT s FROM test_bg1500 WHERE MATCH (s) AGAINST (CONCAT(?, 'digger'))");
 
9273
  check_stmt(stmt);
 
9274
 
 
9275
  verify_param_count(stmt, 1);
 
9276
 
 
9277
  data= "Grave";
 
9278
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
9279
  my_bind[0].buffer= (void *) data;
 
9280
  my_bind[0].buffer_length= strlen(data);
 
9281
 
 
9282
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
9283
  check_execute(stmt, rc);
 
9284
 
 
9285
  rc= mysql_stmt_execute(stmt);
 
9286
  check_execute(stmt, rc);
 
9287
 
 
9288
  rc= my_process_stmt_result(stmt);
 
9289
  DIE_UNLESS(rc == 1);
 
9290
 
 
9291
  mysql_stmt_close(stmt);
 
9292
}
 
9293
 
 
9294
 
 
9295
static void test_bug1946()
 
9296
{
 
9297
  MYSQL_STMT *stmt;
 
9298
  int rc;
 
9299
  const char *query= "INSERT INTO prepare_command VALUES (?)";
 
9300
 
 
9301
  myheader("test_bug1946");
 
9302
 
 
9303
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS prepare_command");
 
9304
  myquery(rc);
 
9305
 
 
9306
  rc= mysql_query(mysql, "CREATE TABLE prepare_command(ID INT)");
 
9307
  myquery(rc);
 
9308
 
 
9309
  stmt= mysql_simple_prepare(mysql, query);
 
9310
  check_stmt(stmt);
 
9311
  rc= mysql_real_query(mysql, query, strlen(query));
 
9312
  DIE_UNLESS(rc != 0);
 
9313
  if (!opt_silent)
 
9314
    fprintf(stdout, "Got error (as expected):\n");
 
9315
  myerror(NULL);
 
9316
 
 
9317
  mysql_stmt_close(stmt);
 
9318
  rc= mysql_query(mysql, "DROP TABLE prepare_command");
 
9319
}
 
9320
 
 
9321
 
 
9322
static void test_parse_error_and_bad_length()
 
9323
{
 
9324
  MYSQL_STMT *stmt;
 
9325
  int rc;
 
9326
 
 
9327
  /* check that we get 4 syntax errors over the 4 calls */
 
9328
  myheader("test_parse_error_and_bad_length");
 
9329
 
 
9330
  rc= mysql_query(mysql, "SHOW DATABAAAA");
 
9331
  DIE_UNLESS(rc);
 
9332
  if (!opt_silent)
 
9333
    fprintf(stdout, "Got error (as expected): '%s'\n", mysql_error(mysql));
 
9334
  rc= mysql_real_query(mysql, "SHOW DATABASES", 100);
 
9335
  DIE_UNLESS(rc);
 
9336
  if (!opt_silent)
 
9337
    fprintf(stdout, "Got error (as expected): '%s'\n", mysql_error(mysql));
 
9338
 
 
9339
  stmt= mysql_simple_prepare(mysql, "SHOW DATABAAAA");
 
9340
  DIE_UNLESS(!stmt);
 
9341
  if (!opt_silent)
 
9342
    fprintf(stdout, "Got error (as expected): '%s'\n", mysql_error(mysql));
 
9343
  stmt= mysql_stmt_init(mysql);
 
9344
  DIE_UNLESS(stmt);
 
9345
  rc= mysql_stmt_prepare(stmt, "SHOW DATABASES", 100);
 
9346
  DIE_UNLESS(rc != 0);
 
9347
  if (!opt_silent)
 
9348
    fprintf(stdout, "Got error (as expected): '%s'\n", mysql_stmt_error(stmt));
 
9349
  mysql_stmt_close(stmt);
 
9350
}
 
9351
 
 
9352
 
 
9353
static void test_bug2247()
 
9354
{
 
9355
  MYSQL_STMT *stmt;
 
9356
  MYSQL_RES *res;
 
9357
  int rc;
 
9358
  int i;
 
9359
  const char *create= "CREATE TABLE bug2247(id INT UNIQUE AUTO_INCREMENT)";
 
9360
  const char *insert= "INSERT INTO bug2247 VALUES (NULL)";
 
9361
  const char *SELECT= "SELECT id FROM bug2247";
 
9362
  const char *update= "UPDATE bug2247 SET id=id+10";
 
9363
  const char *drop= "DROP TABLE IF EXISTS bug2247";
 
9364
  ulonglong exp_count;
 
9365
  enum { NUM_ROWS= 5 };
 
9366
 
 
9367
  myheader("test_bug2247");
 
9368
 
 
9369
  if (!opt_silent)
 
9370
    fprintf(stdout, "\nChecking if stmt_affected_rows is not affected by\n"
 
9371
                  "mysql_query ... ");
 
9372
  /* create table and insert few rows */
 
9373
  rc= mysql_query(mysql, drop);
 
9374
  myquery(rc);
 
9375
 
 
9376
  rc= mysql_query(mysql, create);
 
9377
  myquery(rc);
 
9378
 
 
9379
  stmt= mysql_simple_prepare(mysql, insert);
 
9380
  check_stmt(stmt);
 
9381
  for (i= 0; i < NUM_ROWS; ++i)
 
9382
  {
 
9383
    rc= mysql_stmt_execute(stmt);
 
9384
    check_execute(stmt, rc);
 
9385
  }
 
9386
  exp_count= mysql_stmt_affected_rows(stmt);
 
9387
  DIE_UNLESS(exp_count == 1);
 
9388
 
 
9389
  rc= mysql_query(mysql, SELECT);
 
9390
  myquery(rc);
 
9391
  /*
 
9392
    mysql_store_result overwrites mysql->affected_rows. Check that
 
9393
    mysql_stmt_affected_rows() returns the same value, whereas
 
9394
    mysql_affected_rows() value is correct.
 
9395
  */
 
9396
  res= mysql_store_result(mysql);
 
9397
  mytest(res);
 
9398
 
 
9399
  DIE_UNLESS(mysql_affected_rows(mysql) == NUM_ROWS);
 
9400
  DIE_UNLESS(exp_count == mysql_stmt_affected_rows(stmt));
 
9401
 
 
9402
  rc= mysql_query(mysql, update);
 
9403
  myquery(rc);
 
9404
  DIE_UNLESS(mysql_affected_rows(mysql) == NUM_ROWS);
 
9405
  DIE_UNLESS(exp_count == mysql_stmt_affected_rows(stmt));
 
9406
 
 
9407
  mysql_free_result(res);
 
9408
  mysql_stmt_close(stmt);
 
9409
 
 
9410
  /* check that mysql_stmt_store_result modifies mysql_stmt_affected_rows */
 
9411
  stmt= mysql_simple_prepare(mysql, SELECT);
 
9412
  check_stmt(stmt);
 
9413
 
 
9414
  rc= mysql_stmt_execute(stmt);
 
9415
  check_execute(stmt, rc);
 
9416
  rc= mysql_stmt_store_result(stmt);
 
9417
  check_execute(stmt, rc);
 
9418
  exp_count= mysql_stmt_affected_rows(stmt);
 
9419
  DIE_UNLESS(exp_count == NUM_ROWS);
 
9420
 
 
9421
  rc= mysql_query(mysql, insert);
 
9422
  myquery(rc);
 
9423
  DIE_UNLESS(mysql_affected_rows(mysql) == 1);
 
9424
  DIE_UNLESS(mysql_stmt_affected_rows(stmt) == exp_count);
 
9425
 
 
9426
  mysql_stmt_close(stmt);
 
9427
  if (!opt_silent)
 
9428
    fprintf(stdout, "OK");
 
9429
}
 
9430
 
 
9431
 
 
9432
static void test_subqueries()
 
9433
{
 
9434
  MYSQL_STMT *stmt;
 
9435
  int rc, i;
 
9436
  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";
 
9437
 
 
9438
  myheader("test_subqueries");
 
9439
 
 
9440
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
 
9441
  myquery(rc);
 
9442
 
 
9443
  rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
 
9444
  myquery(rc);
 
9445
 
 
9446
  rc= mysql_query(mysql,
 
9447
                  "insert into t1 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);");
 
9448
  myquery(rc);
 
9449
 
 
9450
  rc= mysql_query(mysql, "create table t2 select * from t1;");
 
9451
  myquery(rc);
 
9452
 
 
9453
  stmt= mysql_simple_prepare(mysql, query);
 
9454
  check_stmt(stmt);
 
9455
  for (i= 0; i < 3; i++)
 
9456
  {
 
9457
    rc= mysql_stmt_execute(stmt);
 
9458
    check_execute(stmt, rc);
 
9459
    rc= my_process_stmt_result(stmt);
 
9460
    DIE_UNLESS(rc == 5);
 
9461
  }
 
9462
  mysql_stmt_close(stmt);
 
9463
 
 
9464
  rc= mysql_query(mysql, "DROP TABLE t1, t2");
 
9465
  myquery(rc);
 
9466
}
 
9467
 
 
9468
 
 
9469
static void test_bad_union()
 
9470
{
 
9471
  MYSQL_STMT *stmt;
 
9472
  const char *query= "SELECT 1, 2 union SELECT 1";
 
9473
 
 
9474
  myheader("test_bad_union");
 
9475
 
 
9476
  stmt= mysql_simple_prepare(mysql, query);
 
9477
  DIE_UNLESS(stmt == 0);
 
9478
  myerror(NULL);
 
9479
}
 
9480
 
 
9481
 
 
9482
static void test_distinct()
 
9483
{
 
9484
  MYSQL_STMT *stmt;
 
9485
  int rc, i;
 
9486
  const char *query=
 
9487
    "SELECT 2+count(distinct b), group_concat(a) FROM t1 group by a";
 
9488
 
 
9489
  myheader("test_distinct");
 
9490
 
 
9491
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
9492
  myquery(rc);
 
9493
 
 
9494
  rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
 
9495
  myquery(rc);
 
9496
 
 
9497
  rc= mysql_query(mysql,
 
9498
                  "insert into t1 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), \
 
9499
(1, 10), (2, 20), (3, 30), (4, 40), (5, 50);");
 
9500
  myquery(rc);
 
9501
 
 
9502
  for (i= 0; i < 3; i++)
 
9503
  {
 
9504
    stmt= mysql_simple_prepare(mysql, query);
 
9505
    check_stmt(stmt);
 
9506
    rc= mysql_stmt_execute(stmt);
 
9507
    check_execute(stmt, rc);
 
9508
    rc= my_process_stmt_result(stmt);
 
9509
    DIE_UNLESS(rc == 5);
 
9510
    mysql_stmt_close(stmt);
 
9511
  }
 
9512
 
 
9513
  rc= mysql_query(mysql, "DROP TABLE t1");
 
9514
  myquery(rc);
 
9515
}
 
9516
 
 
9517
 
 
9518
/*
 
9519
  Test for bug#2248 "mysql_fetch without prior mysql_stmt_execute hangs"
 
9520
*/
 
9521
 
 
9522
static void test_bug2248()
 
9523
{
 
9524
  MYSQL_STMT *stmt;
 
9525
  int rc;
 
9526
  const char *query1= "SELECT DATABASE()";
 
9527
  const char *query2= "INSERT INTO test_bug2248 VALUES (10)";
 
9528
 
 
9529
  myheader("test_bug2248");
 
9530
 
 
9531
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bug2248");
 
9532
  myquery(rc);
 
9533
 
 
9534
  rc= mysql_query(mysql, "CREATE TABLE test_bug2248 (id int)");
 
9535
  myquery(rc);
 
9536
 
 
9537
  stmt= mysql_simple_prepare(mysql, query1);
 
9538
  check_stmt(stmt);
 
9539
 
 
9540
  /* This should not hang */
 
9541
  rc= mysql_stmt_fetch(stmt);
 
9542
  check_execute_r(stmt, rc);
 
9543
 
 
9544
  /* And this too */
 
9545
  rc= mysql_stmt_store_result(stmt);
 
9546
  check_execute_r(stmt, rc);
 
9547
 
 
9548
  mysql_stmt_close(stmt);
 
9549
 
 
9550
  stmt= mysql_simple_prepare(mysql, query2);
 
9551
  check_stmt(stmt);
 
9552
 
 
9553
  rc= mysql_stmt_execute(stmt);
 
9554
  check_execute(stmt, rc);
 
9555
 
 
9556
  /* This too should not hang but should return proper error */
 
9557
  rc= mysql_stmt_fetch(stmt);
 
9558
  DIE_UNLESS(rc == 1);
 
9559
 
 
9560
  /* This too should not hang but should not bark */
 
9561
  rc= mysql_stmt_store_result(stmt);
 
9562
  check_execute(stmt, rc);
 
9563
 
 
9564
  /* This should return proper error */
 
9565
  rc= mysql_stmt_fetch(stmt);
 
9566
  check_execute_r(stmt, rc);
 
9567
  DIE_UNLESS(rc == 1);
 
9568
 
 
9569
  mysql_stmt_close(stmt);
 
9570
 
 
9571
  rc= mysql_query(mysql, "DROP TABLE test_bug2248");
 
9572
  myquery(rc);
 
9573
}
 
9574
 
 
9575
 
 
9576
static void test_subqueries_ref()
 
9577
{
 
9578
  MYSQL_STMT *stmt;
 
9579
  int rc, i;
 
9580
  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)";
 
9581
 
 
9582
  myheader("test_subqueries_ref");
 
9583
 
 
9584
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
9585
  myquery(rc);
 
9586
 
 
9587
  rc= mysql_query(mysql, "CREATE TABLE t1 (a int);");
 
9588
  myquery(rc);
 
9589
 
 
9590
  rc= mysql_query(mysql,
 
9591
                  "insert into t1 values (1), (2), (3), (4), (5);");
 
9592
  myquery(rc);
 
9593
 
 
9594
  stmt= mysql_simple_prepare(mysql, query);
 
9595
  check_stmt(stmt);
 
9596
  for (i= 0; i < 3; i++)
 
9597
  {
 
9598
    rc= mysql_stmt_execute(stmt);
 
9599
    check_execute(stmt, rc);
 
9600
    rc= my_process_stmt_result(stmt);
 
9601
    DIE_UNLESS(rc == 1);
 
9602
  }
 
9603
  mysql_stmt_close(stmt);
 
9604
 
 
9605
  rc= mysql_query(mysql, "DROP TABLE t1");
 
9606
  myquery(rc);
 
9607
}
 
9608
 
 
9609
 
 
9610
static void test_union()
 
9611
{
 
9612
  MYSQL_STMT *stmt;
 
9613
  int rc;
 
9614
 
 
9615
  myheader("test_union");
 
9616
 
 
9617
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
 
9618
  myquery(rc);
 
9619
 
 
9620
  rc= mysql_query(mysql,
 
9621
                  "CREATE TABLE t1 "
 
9622
                  "(id INTEGER NOT NULL PRIMARY KEY, "
 
9623
                  " name VARCHAR(20) NOT NULL)");
 
9624
  myquery(rc);
 
9625
  rc= mysql_query(mysql,
 
9626
                  "INSERT INTO t1 (id, name) VALUES "
 
9627
                  "(2, 'Ja'), (3, 'Ede'), "
 
9628
                  "(4, 'Haag'), (5, 'Kabul'), "
 
9629
                  "(6, 'Almere'), (7, 'Utrecht'), "
 
9630
                  "(8, 'Qandahar'), (9, 'Amsterdam'), "
 
9631
                  "(10, 'Amersfoort'), (11, 'Constantine')");
 
9632
  myquery(rc);
 
9633
  rc= mysql_query(mysql,
 
9634
                  "CREATE TABLE t2 "
 
9635
                  "(id INTEGER NOT NULL PRIMARY KEY, "
 
9636
                  " name VARCHAR(20) NOT NULL)");
 
9637
  myquery(rc);
 
9638
  rc= mysql_query(mysql,
 
9639
                  "INSERT INTO t2 (id, name) VALUES "
 
9640
                  "(4, 'Guam'), (5, 'Aruba'), "
 
9641
                  "(6, 'Angola'), (7, 'Albania'), "
 
9642
                  "(8, 'Anguilla'), (9, 'Argentina'), "
 
9643
                  "(10, 'Azerbaijan'), (11, 'Afghanistan'), "
 
9644
                  "(12, 'Burkina Faso'), (13, 'Faroe Islands')");
 
9645
  myquery(rc);
 
9646
 
 
9647
  stmt= mysql_simple_prepare(mysql,
 
9648
                             "SELECT t1.name FROM t1 UNION "
 
9649
                             "SELECT t2.name FROM t2");
 
9650
  check_stmt(stmt);
 
9651
 
 
9652
  rc= mysql_stmt_execute(stmt);
 
9653
  check_execute(stmt, rc);
 
9654
  rc= my_process_stmt_result(stmt);
 
9655
  DIE_UNLESS(rc == 20);
 
9656
  mysql_stmt_close(stmt);
 
9657
 
 
9658
  rc= mysql_query(mysql, "DROP TABLE t1, t2");
 
9659
  myquery(rc);
 
9660
}
 
9661
 
 
9662
 
 
9663
static void test_bug3117()
 
9664
{
 
9665
  MYSQL_STMT *stmt;
 
9666
  MYSQL_BIND buffer;
 
9667
  longlong lii;
 
9668
  ulong length;
 
9669
  my_bool is_null;
 
9670
  int rc;
 
9671
 
 
9672
  myheader("test_bug3117");
 
9673
 
 
9674
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
9675
  myquery(rc);
 
9676
 
 
9677
  rc= mysql_query(mysql, "CREATE TABLE t1 (id int auto_increment primary key)");
 
9678
  myquery(rc);
 
9679
 
 
9680
  stmt= mysql_simple_prepare(mysql, "SELECT LAST_INSERT_ID()");
 
9681
  check_stmt(stmt);
 
9682
 
 
9683
  rc= mysql_query(mysql, "INSERT INTO t1 VALUES (NULL)");
 
9684
  myquery(rc);
 
9685
 
 
9686
  rc= mysql_stmt_execute(stmt);
 
9687
  check_execute(stmt, rc);
 
9688
 
 
9689
  bzero((char*) &buffer, sizeof(buffer));
 
9690
  buffer.buffer_type= MYSQL_TYPE_LONGLONG;
 
9691
  buffer.buffer_length= sizeof(lii);
 
9692
  buffer.buffer= (void *)&lii;
 
9693
  buffer.length= &length;
 
9694
  buffer.is_null= &is_null;
 
9695
 
 
9696
  rc= mysql_stmt_bind_result(stmt, &buffer);
 
9697
  check_execute(stmt, rc);
 
9698
 
 
9699
  rc= mysql_stmt_store_result(stmt);
 
9700
  check_execute(stmt, rc);
 
9701
 
 
9702
  rc= mysql_stmt_fetch(stmt);
 
9703
  check_execute(stmt, rc);
 
9704
 
 
9705
  DIE_UNLESS(is_null == 0 && lii == 1);
 
9706
  if (!opt_silent)
 
9707
    fprintf(stdout, "\n\tLAST_INSERT_ID()= 1 ok\n");
 
9708
 
 
9709
  rc= mysql_query(mysql, "INSERT INTO t1 VALUES (NULL)");
 
9710
  myquery(rc);
 
9711
 
 
9712
  rc= mysql_stmt_execute(stmt);
 
9713
  check_execute(stmt, rc);
 
9714
 
 
9715
  rc= mysql_stmt_fetch(stmt);
 
9716
  check_execute(stmt, rc);
 
9717
 
 
9718
  DIE_UNLESS(is_null == 0 && lii == 2);
 
9719
  if (!opt_silent)
 
9720
    fprintf(stdout, "\tLAST_INSERT_ID()= 2 ok\n");
 
9721
 
 
9722
  mysql_stmt_close(stmt);
 
9723
 
 
9724
  rc= mysql_query(mysql, "DROP TABLE t1");
 
9725
  myquery(rc);
 
9726
}
 
9727
 
 
9728
 
 
9729
static void test_join()
 
9730
{
 
9731
  MYSQL_STMT *stmt;
 
9732
  int rc, i, j;
 
9733
  const char *query[]= {"SELECT * FROM t2 join t1 on (t1.a=t2.a)",
 
9734
                        "SELECT * FROM t2 natural join t1",
 
9735
                        "SELECT * FROM t2 join t1 using(a)",
 
9736
                        "SELECT * FROM t2 left join t1 on(t1.a=t2.a)",
 
9737
                        "SELECT * FROM t2 natural left join t1",
 
9738
                        "SELECT * FROM t2 left join t1 using(a)",
 
9739
                        "SELECT * FROM t2 right join t1 on(t1.a=t2.a)",
 
9740
                        "SELECT * FROM t2 natural right join t1",
 
9741
                        "SELECT * FROM t2 right join t1 using(a)"};
 
9742
 
 
9743
  myheader("test_join");
 
9744
 
 
9745
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
 
9746
  myquery(rc);
 
9747
 
 
9748
  rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
 
9749
  myquery(rc);
 
9750
 
 
9751
  rc= mysql_query(mysql,
 
9752
                  "insert into t1 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);");
 
9753
  myquery(rc);
 
9754
 
 
9755
  rc= mysql_query(mysql, "CREATE TABLE t2 (a int , c int);");
 
9756
  myquery(rc);
 
9757
 
 
9758
  rc= mysql_query(mysql,
 
9759
                  "insert into t2 values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);");
 
9760
  myquery(rc);
 
9761
 
 
9762
  for (j= 0; j < 9; j++)
 
9763
  {
 
9764
    stmt= mysql_simple_prepare(mysql, query[j]);
 
9765
    check_stmt(stmt);
 
9766
    for (i= 0; i < 3; i++)
 
9767
    {
 
9768
      rc= mysql_stmt_execute(stmt);
 
9769
      check_execute(stmt, rc);
 
9770
      rc= my_process_stmt_result(stmt);
 
9771
      DIE_UNLESS(rc == 5);
 
9772
    }
 
9773
    mysql_stmt_close(stmt);
 
9774
  }
 
9775
 
 
9776
  rc= mysql_query(mysql, "DROP TABLE t1, t2");
 
9777
  myquery(rc);
 
9778
}
 
9779
 
 
9780
 
 
9781
static void test_selecttmp()
 
9782
{
 
9783
  MYSQL_STMT *stmt;
 
9784
  int rc, i;
 
9785
  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";
 
9786
 
 
9787
  myheader("test_select_tmp");
 
9788
 
 
9789
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2, t3");
 
9790
  myquery(rc);
 
9791
 
 
9792
  rc= mysql_query(mysql, "CREATE TABLE t1 (a int , b int);");
 
9793
  myquery(rc);
 
9794
 
 
9795
  rc= mysql_query(mysql, "create table t2 (a int, b int);");
 
9796
  myquery(rc);
 
9797
 
 
9798
  rc= mysql_query(mysql, "create table t3 (a int, b int);");
 
9799
  myquery(rc);
 
9800
 
 
9801
  rc= mysql_query(mysql,
 
9802
                  "insert into t1 values (0, 100), (1, 2), (1, 3), (2, 2), (2, 7), \
 
9803
(2, -1), (3, 10);");
 
9804
  myquery(rc);
 
9805
  rc= mysql_query(mysql,
 
9806
                  "insert into t2 values (0, 0), (1, 1), (2, 1), (3, 1), (4, 1);");
 
9807
  myquery(rc);
 
9808
  rc= mysql_query(mysql,
 
9809
                  "insert into t3 values (3, 3), (2, 2), (1, 1);");
 
9810
  myquery(rc);
 
9811
 
 
9812
  stmt= mysql_simple_prepare(mysql, query);
 
9813
  check_stmt(stmt);
 
9814
  for (i= 0; i < 3; i++)
 
9815
  {
 
9816
    rc= mysql_stmt_execute(stmt);
 
9817
    check_execute(stmt, rc);
 
9818
    rc= my_process_stmt_result(stmt);
 
9819
    DIE_UNLESS(rc == 3);
 
9820
  }
 
9821
  mysql_stmt_close(stmt);
 
9822
 
 
9823
  rc= mysql_query(mysql, "DROP TABLE t1, t2, t3");
 
9824
  myquery(rc);
 
9825
}
 
9826
 
 
9827
 
 
9828
static void test_create_drop()
 
9829
{
 
9830
  MYSQL_STMT *stmt_create, *stmt_drop, *stmt_select, *stmt_create_select;
 
9831
  char *query;
 
9832
  int rc, i;
 
9833
  myheader("test_table_manipulation");
 
9834
 
 
9835
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
 
9836
  myquery(rc);
 
9837
 
 
9838
  rc= mysql_query(mysql, "create table t2 (a int);");
 
9839
  myquery(rc);
 
9840
 
 
9841
  rc= mysql_query(mysql, "create table t1 (a int);");
 
9842
  myquery(rc);
 
9843
 
 
9844
  rc= mysql_query(mysql, "insert into t2 values (3), (2), (1);");
 
9845
  myquery(rc);
 
9846
 
 
9847
  query= (char*)"create table t1 (a int)";
 
9848
  stmt_create= mysql_simple_prepare(mysql, query);
 
9849
  check_stmt(stmt_create);
 
9850
 
 
9851
  query= (char*)"drop table t1";
 
9852
  stmt_drop= mysql_simple_prepare(mysql, query);
 
9853
  check_stmt(stmt_drop);
 
9854
 
 
9855
  query= (char*)"select a in (select a from t2) from t1";
 
9856
  stmt_select= mysql_simple_prepare(mysql, query);
 
9857
  check_stmt(stmt_select);
 
9858
 
 
9859
  rc= mysql_query(mysql, "DROP TABLE t1");
 
9860
  myquery(rc);
 
9861
 
 
9862
  query= (char*)"create table t1 select a from t2";
 
9863
  stmt_create_select= mysql_simple_prepare(mysql, query);
 
9864
  check_stmt(stmt_create_select);
 
9865
 
 
9866
  for (i= 0; i < 3; i++)
 
9867
  {
 
9868
    rc= mysql_stmt_execute(stmt_create);
 
9869
    check_execute(stmt_create, rc);
 
9870
    if (!opt_silent)
 
9871
      fprintf(stdout, "created %i\n", i);
 
9872
 
 
9873
    rc= mysql_stmt_execute(stmt_select);
 
9874
    check_execute(stmt_select, rc);
 
9875
    rc= my_process_stmt_result(stmt_select);
 
9876
    DIE_UNLESS(rc == 0);
 
9877
 
 
9878
    rc= mysql_stmt_execute(stmt_drop);
 
9879
    check_execute(stmt_drop, rc);
 
9880
    if (!opt_silent)
 
9881
      fprintf(stdout, "dropped %i\n", i);
 
9882
 
 
9883
    rc= mysql_stmt_execute(stmt_create_select);
 
9884
    check_execute(stmt_create, rc);
 
9885
    if (!opt_silent)
 
9886
      fprintf(stdout, "created select %i\n", i);
 
9887
 
 
9888
    rc= mysql_stmt_execute(stmt_select);
 
9889
    check_execute(stmt_select, rc);
 
9890
    rc= my_process_stmt_result(stmt_select);
 
9891
    DIE_UNLESS(rc == 3);
 
9892
 
 
9893
    rc= mysql_stmt_execute(stmt_drop);
 
9894
    check_execute(stmt_drop, rc);
 
9895
    if (!opt_silent)
 
9896
      fprintf(stdout, "dropped %i\n", i);
 
9897
  }
 
9898
 
 
9899
  mysql_stmt_close(stmt_create);
 
9900
  mysql_stmt_close(stmt_drop);
 
9901
  mysql_stmt_close(stmt_select);
 
9902
  mysql_stmt_close(stmt_create_select);
 
9903
 
 
9904
  rc= mysql_query(mysql, "DROP TABLE t2");
 
9905
  myquery(rc);
 
9906
}
 
9907
 
 
9908
 
 
9909
static void test_rename()
 
9910
{
 
9911
  MYSQL_STMT *stmt;
 
9912
  const char *query= "rename table t1 to t2, t3 to t4";
 
9913
  int rc;
 
9914
  myheader("test_table_rename");
 
9915
 
 
9916
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2, t3, t4");
 
9917
  myquery(rc);
 
9918
 
 
9919
  stmt= mysql_simple_prepare(mysql, query);
 
9920
  check_stmt(stmt);
 
9921
 
 
9922
  rc= mysql_query(mysql, "create table t1 (a int)");
 
9923
  myquery(rc);
 
9924
 
 
9925
  rc= mysql_stmt_execute(stmt);
 
9926
  check_execute_r(stmt, rc);
 
9927
  if (!opt_silent)
 
9928
    fprintf(stdout, "rename without t3\n");
 
9929
 
 
9930
  rc= mysql_query(mysql, "create table t3 (a int)");
 
9931
  myquery(rc);
 
9932
 
 
9933
  rc= mysql_stmt_execute(stmt);
 
9934
  check_execute(stmt, rc);
 
9935
  if (!opt_silent)
 
9936
    fprintf(stdout, "rename with t3\n");
 
9937
 
 
9938
  rc= mysql_stmt_execute(stmt);
 
9939
  check_execute_r(stmt, rc);
 
9940
  if (!opt_silent)
 
9941
    fprintf(stdout, "rename renamed\n");
 
9942
 
 
9943
  rc= mysql_query(mysql, "rename table t2 to t1, t4 to t3");
 
9944
  myquery(rc);
 
9945
 
 
9946
  rc= mysql_stmt_execute(stmt);
 
9947
  check_execute(stmt, rc);
 
9948
  if (!opt_silent)
 
9949
    fprintf(stdout, "rename reverted\n");
 
9950
 
 
9951
  mysql_stmt_close(stmt);
 
9952
 
 
9953
  rc= mysql_query(mysql, "DROP TABLE t2, t4");
 
9954
  myquery(rc);
 
9955
}
 
9956
 
 
9957
 
 
9958
static void test_do_set()
 
9959
{
 
9960
  MYSQL_STMT *stmt_do, *stmt_set;
 
9961
  char *query;
 
9962
  int rc, i;
 
9963
  myheader("test_do_set");
 
9964
 
 
9965
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
9966
  myquery(rc);
 
9967
 
 
9968
  rc= mysql_query(mysql, "create table t1 (a int)");
 
9969
  myquery(rc);
 
9970
 
 
9971
  query= (char*)"do @var:=(1 in (select * from t1))";
 
9972
  stmt_do= mysql_simple_prepare(mysql, query);
 
9973
  check_stmt(stmt_do);
 
9974
 
 
9975
  query= (char*)"set @var=(1 in (select * from t1))";
 
9976
  stmt_set= mysql_simple_prepare(mysql, query);
 
9977
  check_stmt(stmt_set);
 
9978
 
 
9979
  for (i= 0; i < 3; i++)
 
9980
  {
 
9981
    rc= mysql_stmt_execute(stmt_do);
 
9982
    check_execute(stmt_do, rc);
 
9983
    if (!opt_silent)
 
9984
      fprintf(stdout, "do %i\n", i);
 
9985
    rc= mysql_stmt_execute(stmt_set);
 
9986
    check_execute(stmt_set, rc);
 
9987
    if (!opt_silent)
 
9988
      fprintf(stdout, "set %i\n", i);
 
9989
  }
 
9990
 
 
9991
  mysql_stmt_close(stmt_do);
 
9992
  mysql_stmt_close(stmt_set);
 
9993
}
 
9994
 
 
9995
 
 
9996
static void test_multi()
 
9997
{
 
9998
  MYSQL_STMT *stmt_delete, *stmt_update, *stmt_select1, *stmt_select2;
 
9999
  char *query;
 
10000
  MYSQL_BIND my_bind[1];
 
10001
  int rc, i;
 
10002
  int32 param= 1;
 
10003
  ulong length= 1;
 
10004
  myheader("test_multi");
 
10005
 
 
10006
  /*
 
10007
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
10008
    its members.
 
10009
  */
 
10010
  bzero((char*) my_bind, sizeof(my_bind));
 
10011
 
 
10012
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
10013
  my_bind[0].buffer= (void *)&param;
 
10014
  my_bind[0].length= &length;
 
10015
 
 
10016
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
 
10017
  myquery(rc);
 
10018
 
 
10019
  rc= mysql_query(mysql, "create table t1 (a int, b int)");
 
10020
  myquery(rc);
 
10021
 
 
10022
  rc= mysql_query(mysql, "create table t2 (a int, b int)");
 
10023
  myquery(rc);
 
10024
 
 
10025
  rc= mysql_query(mysql, "insert into t1 values (3, 3), (2, 2), (1, 1)");
 
10026
  myquery(rc);
 
10027
 
 
10028
  rc= mysql_query(mysql, "insert into t2 values (3, 3), (2, 2), (1, 1)");
 
10029
  myquery(rc);
 
10030
 
 
10031
  query= (char*)"delete t1, t2 from t1, t2 where t1.a=t2.a and t1.b=10";
 
10032
  stmt_delete= mysql_simple_prepare(mysql, query);
 
10033
  check_stmt(stmt_delete);
 
10034
 
 
10035
  query= (char*)"update t1, t2 set t1.b=10, t2.b=10 where t1.a=t2.a and t1.b=?";
 
10036
  stmt_update= mysql_simple_prepare(mysql, query);
 
10037
  check_stmt(stmt_update);
 
10038
 
 
10039
  query= (char*)"select * from t1";
 
10040
  stmt_select1= mysql_simple_prepare(mysql, query);
 
10041
  check_stmt(stmt_select1);
 
10042
 
 
10043
  query= (char*)"select * from t2";
 
10044
  stmt_select2= mysql_simple_prepare(mysql, query);
 
10045
  check_stmt(stmt_select2);
 
10046
 
 
10047
  for(i= 0; i < 3; i++)
 
10048
  {
 
10049
    rc= mysql_stmt_bind_param(stmt_update, my_bind);
 
10050
    check_execute(stmt_update, rc);
 
10051
 
 
10052
    rc= mysql_stmt_execute(stmt_update);
 
10053
    check_execute(stmt_update, rc);
 
10054
    if (!opt_silent)
 
10055
      fprintf(stdout, "update %ld\n", (long) param);
 
10056
 
 
10057
    rc= mysql_stmt_execute(stmt_delete);
 
10058
    check_execute(stmt_delete, rc);
 
10059
    if (!opt_silent)
 
10060
      fprintf(stdout, "delete %ld\n", (long) param);
 
10061
 
 
10062
    rc= mysql_stmt_execute(stmt_select1);
 
10063
    check_execute(stmt_select1, rc);
 
10064
    rc= my_process_stmt_result(stmt_select1);
 
10065
    DIE_UNLESS(rc == 3-param);
 
10066
 
 
10067
    rc= mysql_stmt_execute(stmt_select2);
 
10068
    check_execute(stmt_select2, rc);
 
10069
    rc= my_process_stmt_result(stmt_select2);
 
10070
    DIE_UNLESS(rc == 3-param);
 
10071
 
 
10072
    param++;
 
10073
  }
 
10074
 
 
10075
  mysql_stmt_close(stmt_delete);
 
10076
  mysql_stmt_close(stmt_update);
 
10077
  mysql_stmt_close(stmt_select1);
 
10078
  mysql_stmt_close(stmt_select2);
 
10079
  rc= mysql_query(mysql, "drop table t1, t2");
 
10080
  myquery(rc);
 
10081
}
 
10082
 
 
10083
 
 
10084
static void test_insert_select()
 
10085
{
 
10086
  MYSQL_STMT *stmt_insert, *stmt_select;
 
10087
  char *query;
 
10088
  int rc;
 
10089
  uint i;
 
10090
  myheader("test_insert_select");
 
10091
 
 
10092
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2");
 
10093
  myquery(rc);
 
10094
 
 
10095
  rc= mysql_query(mysql, "create table t1 (a int)");
 
10096
  myquery(rc);
 
10097
 
 
10098
  rc= mysql_query(mysql, "create table t2 (a int)");
 
10099
  myquery(rc);
 
10100
 
 
10101
  rc= mysql_query(mysql, "insert into t2 values (1)");
 
10102
  myquery(rc);
 
10103
 
 
10104
  query= (char*)"insert into t1 select a from t2";
 
10105
  stmt_insert= mysql_simple_prepare(mysql, query);
 
10106
  check_stmt(stmt_insert);
 
10107
 
 
10108
  query= (char*)"select * from t1";
 
10109
  stmt_select= mysql_simple_prepare(mysql, query);
 
10110
  check_stmt(stmt_select);
 
10111
 
 
10112
  for(i= 0; i < 3; i++)
 
10113
  {
 
10114
    rc= mysql_stmt_execute(stmt_insert);
 
10115
    check_execute(stmt_insert, rc);
 
10116
    if (!opt_silent)
 
10117
      fprintf(stdout, "insert %u\n", i);
 
10118
 
 
10119
    rc= mysql_stmt_execute(stmt_select);
 
10120
    check_execute(stmt_select, rc);
 
10121
    rc= my_process_stmt_result(stmt_select);
 
10122
    DIE_UNLESS(rc == (int)(i+1));
 
10123
  }
 
10124
 
 
10125
  mysql_stmt_close(stmt_insert);
 
10126
  mysql_stmt_close(stmt_select);
 
10127
  rc= mysql_query(mysql, "drop table t1, t2");
 
10128
  myquery(rc);
 
10129
}
 
10130
 
 
10131
 
 
10132
static void test_bind_nagative()
 
10133
{
 
10134
  MYSQL_STMT *stmt_insert;
 
10135
  char *query;
 
10136
  int rc;
 
10137
  MYSQL_BIND      my_bind[1];
 
10138
  int32           my_val= 0;
 
10139
  ulong           my_length= 0L;
 
10140
  my_bool         my_null= FALSE;
 
10141
  myheader("test_insert_select");
 
10142
 
 
10143
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
10144
  myquery(rc);
 
10145
 
 
10146
  rc= mysql_query(mysql, "create temporary table t1 (c1 int unsigned)");
 
10147
  myquery(rc);
 
10148
 
 
10149
  rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1), (-1)");
 
10150
  myquery(rc);
 
10151
 
 
10152
  query= (char*)"INSERT INTO t1 VALUES (?)";
 
10153
  stmt_insert= mysql_simple_prepare(mysql, query);
 
10154
  check_stmt(stmt_insert);
 
10155
 
 
10156
  /* bind parameters */
 
10157
  bzero((char*) my_bind, sizeof(my_bind));
 
10158
 
 
10159
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
10160
  my_bind[0].buffer= (void *)&my_val;
 
10161
  my_bind[0].length= &my_length;
 
10162
  my_bind[0].is_null= (char*)&my_null;
 
10163
 
 
10164
  rc= mysql_stmt_bind_param(stmt_insert, my_bind);
 
10165
  check_execute(stmt_insert, rc);
 
10166
 
 
10167
  my_val= -1;
 
10168
  rc= mysql_stmt_execute(stmt_insert);
 
10169
  check_execute(stmt_insert, rc);
 
10170
 
 
10171
  mysql_stmt_close(stmt_insert);
 
10172
  rc= mysql_query(mysql, "drop table t1");
 
10173
  myquery(rc);
 
10174
}
 
10175
 
 
10176
 
 
10177
static void test_derived()
 
10178
{
 
10179
  MYSQL_STMT *stmt;
 
10180
  int rc, i;
 
10181
  MYSQL_BIND      my_bind[1];
 
10182
  int32           my_val= 0;
 
10183
  ulong           my_length= 0L;
 
10184
  my_bool         my_null= FALSE;
 
10185
  const char *query=
 
10186
    "select count(1) from (select f.id from t1 f where f.id=?) as x";
 
10187
 
 
10188
  myheader("test_derived");
 
10189
 
 
10190
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
10191
  myquery(rc);
 
10192
 
 
10193
  rc= mysql_query(mysql, "create table t1 (id  int(8), primary key (id)) \
 
10194
ENGINE=InnoDB DEFAULT CHARSET=utf8");
 
10195
  myquery(rc);
 
10196
 
 
10197
  rc= mysql_query(mysql, "insert into t1 values (1)");
 
10198
  myquery(rc);
 
10199
 
 
10200
  stmt= mysql_simple_prepare(mysql, query);
 
10201
  check_stmt(stmt);
 
10202
  /*
 
10203
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
10204
    its members.
 
10205
  */
 
10206
  bzero((char*) my_bind, sizeof(my_bind));
 
10207
 
 
10208
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
10209
  my_bind[0].buffer= (void *)&my_val;
 
10210
  my_bind[0].length= &my_length;
 
10211
  my_bind[0].is_null= (char*)&my_null;
 
10212
  my_val= 1;
 
10213
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
10214
  check_execute(stmt, rc);
 
10215
 
 
10216
  for (i= 0; i < 3; i++)
 
10217
  {
 
10218
    rc= mysql_stmt_execute(stmt);
 
10219
    check_execute(stmt, rc);
 
10220
    rc= my_process_stmt_result(stmt);
 
10221
    DIE_UNLESS(rc == 1);
 
10222
  }
 
10223
  mysql_stmt_close(stmt);
 
10224
 
 
10225
  rc= mysql_query(mysql, "DROP TABLE t1");
 
10226
  myquery(rc);
 
10227
}
 
10228
 
 
10229
 
 
10230
static void test_xjoin()
 
10231
{
 
10232
  MYSQL_STMT *stmt;
 
10233
  int rc, i;
 
10234
  const char *query=
 
10235
    "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";
 
10236
 
 
10237
  myheader("test_xjoin");
 
10238
 
 
10239
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, t2, t3, t4");
 
10240
  myquery(rc);
 
10241
 
 
10242
  rc= mysql_query(mysql, "create table t3 (id int(8), param1_id int(8), param2_id int(8)) ENGINE=InnoDB DEFAULT CHARSET=utf8");
 
10243
  myquery(rc);
 
10244
 
 
10245
  rc= mysql_query(mysql, "create table t1 ( id int(8), name_id int(8), value varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8");
 
10246
  myquery(rc);
 
10247
 
 
10248
  rc= mysql_query(mysql, "create table t2 (id int(8), name_id int(8), value varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
 
10249
  myquery(rc);
 
10250
 
 
10251
  rc= mysql_query(mysql, "create table t4(id int(8), value varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8");
 
10252
  myquery(rc);
 
10253
 
 
10254
  rc= mysql_query(mysql, "insert into t3 values (1, 1, 1), (2, 2, null)");
 
10255
  myquery(rc);
 
10256
 
 
10257
  rc= mysql_query(mysql, "insert into t1 values (1, 1, 'aaa'), (2, null, 'bbb')");
 
10258
  myquery(rc);
 
10259
 
 
10260
  rc= mysql_query(mysql, "insert into t2 values (1, 2, 'ccc')");
 
10261
  myquery(rc);
 
10262
 
 
10263
  rc= mysql_query(mysql, "insert into t4 values (1, 'Name1'), (2, null)");
 
10264
  myquery(rc);
 
10265
 
 
10266
  stmt= mysql_simple_prepare(mysql, query);
 
10267
  check_stmt(stmt);
 
10268
 
 
10269
  for (i= 0; i < 3; i++)
 
10270
  {
 
10271
    rc= mysql_stmt_execute(stmt);
 
10272
    check_execute(stmt, rc);
 
10273
    rc= my_process_stmt_result(stmt);
 
10274
    DIE_UNLESS(rc == 1);
 
10275
  }
 
10276
  mysql_stmt_close(stmt);
 
10277
 
 
10278
  rc= mysql_query(mysql, "DROP TABLE t1, t2, t3, t4");
 
10279
  myquery(rc);
 
10280
}
 
10281
 
 
10282
 
 
10283
static void test_bug3035()
 
10284
{
 
10285
  MYSQL_STMT *stmt;
 
10286
  int rc;
 
10287
  MYSQL_BIND bind_array[12], *my_bind= bind_array, *bind_end= my_bind + 12;
 
10288
  int8 int8_val;
 
10289
  uint8 uint8_val;
 
10290
  int16 int16_val;
 
10291
  uint16 uint16_val;
 
10292
  int32 int32_val;
 
10293
  uint32 uint32_val;
 
10294
  longlong int64_val;
 
10295
  ulonglong uint64_val;
 
10296
  double double_val, udouble_val, double_tmp;
 
10297
  char longlong_as_string[22], ulonglong_as_string[22];
 
10298
 
 
10299
  /* mins and maxes */
 
10300
  const int8 int8_min= -128;
 
10301
  const int8 int8_max= 127;
 
10302
  const uint8 uint8_min= 0;
 
10303
  const uint8 uint8_max= 255;
 
10304
 
 
10305
  const int16 int16_min= -32768;
 
10306
  const int16 int16_max= 32767;
 
10307
  const uint16 uint16_min= 0;
 
10308
  const uint16 uint16_max= 65535;
 
10309
 
 
10310
  const int32 int32_max= 2147483647L;
 
10311
  const int32 int32_min= -int32_max - 1;
 
10312
  const uint32 uint32_min= 0;
 
10313
  const uint32 uint32_max= 4294967295U;
 
10314
 
 
10315
  /* it might not work okay everyplace */
 
10316
  const longlong int64_max= LL(9223372036854775807);
 
10317
  const longlong int64_min= -int64_max - 1;
 
10318
 
 
10319
  const ulonglong uint64_min= 0U;
 
10320
  const ulonglong uint64_max= ULL(18446744073709551615);
 
10321
 
 
10322
  const char *stmt_text;
 
10323
 
 
10324
  myheader("test_bug3035");
 
10325
 
 
10326
  stmt_text= "DROP TABLE IF EXISTS t1";
 
10327
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10328
  myquery(rc);
 
10329
 
 
10330
  stmt_text= "CREATE TABLE t1 (i8 TINYINT, ui8 TINYINT UNSIGNED, "
 
10331
                              "i16 SMALLINT, ui16 SMALLINT UNSIGNED, "
 
10332
                              "i32 INT, ui32 INT UNSIGNED, "
 
10333
                              "i64 BIGINT, ui64 BIGINT UNSIGNED, "
 
10334
                              "id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT)";
 
10335
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10336
  myquery(rc);
 
10337
 
 
10338
  bzero((char*) bind_array, sizeof(bind_array));
 
10339
 
 
10340
  for (my_bind= bind_array; my_bind < bind_end; my_bind++)
 
10341
    my_bind->error= &my_bind->error_value;
 
10342
 
 
10343
  bind_array[0].buffer_type= MYSQL_TYPE_TINY;
 
10344
  bind_array[0].buffer= (void *) &int8_val;
 
10345
 
 
10346
  bind_array[1].buffer_type= MYSQL_TYPE_TINY;
 
10347
  bind_array[1].buffer= (void *) &uint8_val;
 
10348
  bind_array[1].is_unsigned= 1;
 
10349
 
 
10350
  bind_array[2].buffer_type= MYSQL_TYPE_SHORT;
 
10351
  bind_array[2].buffer= (void *) &int16_val;
 
10352
 
 
10353
  bind_array[3].buffer_type= MYSQL_TYPE_SHORT;
 
10354
  bind_array[3].buffer= (void *) &uint16_val;
 
10355
  bind_array[3].is_unsigned= 1;
 
10356
 
 
10357
  bind_array[4].buffer_type= MYSQL_TYPE_LONG;
 
10358
  bind_array[4].buffer= (void *) &int32_val;
 
10359
 
 
10360
  bind_array[5].buffer_type= MYSQL_TYPE_LONG;
 
10361
  bind_array[5].buffer= (void *) &uint32_val;
 
10362
  bind_array[5].is_unsigned= 1;
 
10363
 
 
10364
  bind_array[6].buffer_type= MYSQL_TYPE_LONGLONG;
 
10365
  bind_array[6].buffer= (void *) &int64_val;
 
10366
 
 
10367
  bind_array[7].buffer_type= MYSQL_TYPE_LONGLONG;
 
10368
  bind_array[7].buffer= (void *) &uint64_val;
 
10369
  bind_array[7].is_unsigned= 1;
 
10370
 
 
10371
  stmt= mysql_stmt_init(mysql);
 
10372
  check_stmt(stmt);
 
10373
 
 
10374
  stmt_text= "INSERT INTO t1 (i8, ui8, i16, ui16, i32, ui32, i64, ui64) "
 
10375
                     "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
 
10376
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
10377
  check_execute(stmt, rc);
 
10378
 
 
10379
  mysql_stmt_bind_param(stmt, bind_array);
 
10380
 
 
10381
  int8_val= int8_min;
 
10382
  uint8_val= uint8_min;
 
10383
  int16_val= int16_min;
 
10384
  uint16_val= uint16_min;
 
10385
  int32_val= int32_min;
 
10386
  uint32_val= uint32_min;
 
10387
  int64_val= int64_min;
 
10388
  uint64_val= uint64_min;
 
10389
 
 
10390
  rc= mysql_stmt_execute(stmt);
 
10391
  check_execute(stmt, rc);
 
10392
 
 
10393
  int8_val= int8_max;
 
10394
  uint8_val= uint8_max;
 
10395
  int16_val= int16_max;
 
10396
  uint16_val= uint16_max;
 
10397
  int32_val= int32_max;
 
10398
  uint32_val= uint32_max;
 
10399
  int64_val= int64_max;
 
10400
  uint64_val= uint64_max;
 
10401
 
 
10402
  rc= mysql_stmt_execute(stmt);
 
10403
  check_execute(stmt, rc);
 
10404
 
 
10405
  stmt_text= "SELECT i8, ui8, i16, ui16, i32, ui32, i64, ui64, ui64, "
 
10406
             "cast(ui64 as signed), ui64, cast(ui64 as signed)"
 
10407
             "FROM t1 ORDER BY id ASC";
 
10408
 
 
10409
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
10410
  check_execute(stmt, rc);
 
10411
 
 
10412
  rc= mysql_stmt_execute(stmt);
 
10413
  check_execute(stmt, rc);
 
10414
 
 
10415
  bind_array[8].buffer_type= MYSQL_TYPE_DOUBLE;
 
10416
  bind_array[8].buffer= (void *) &udouble_val;
 
10417
 
 
10418
  bind_array[9].buffer_type= MYSQL_TYPE_DOUBLE;
 
10419
  bind_array[9].buffer= (void *) &double_val;
 
10420
 
 
10421
  bind_array[10].buffer_type= MYSQL_TYPE_STRING;
 
10422
  bind_array[10].buffer= (void *) &ulonglong_as_string;
 
10423
  bind_array[10].buffer_length= sizeof(ulonglong_as_string);
 
10424
 
 
10425
  bind_array[11].buffer_type= MYSQL_TYPE_STRING;
 
10426
  bind_array[11].buffer= (void *) &longlong_as_string;
 
10427
  bind_array[11].buffer_length= sizeof(longlong_as_string);
 
10428
 
 
10429
  mysql_stmt_bind_result(stmt, bind_array);
 
10430
 
 
10431
  rc= mysql_stmt_fetch(stmt);
 
10432
  check_execute(stmt, rc);
 
10433
 
 
10434
  DIE_UNLESS(int8_val == int8_min);
 
10435
  DIE_UNLESS(uint8_val == uint8_min);
 
10436
  DIE_UNLESS(int16_val == int16_min);
 
10437
  DIE_UNLESS(uint16_val == uint16_min);
 
10438
  DIE_UNLESS(int32_val == int32_min);
 
10439
  DIE_UNLESS(uint32_val == uint32_min);
 
10440
  DIE_UNLESS(int64_val == int64_min);
 
10441
  DIE_UNLESS(uint64_val == uint64_min);
 
10442
  DIE_UNLESS(double_val == (longlong) uint64_min);
 
10443
  double_tmp= ulonglong2double(uint64_val);
 
10444
  DIE_UNLESS(cmp_double(&udouble_val, &double_tmp));
 
10445
  DIE_UNLESS(!strcmp(longlong_as_string, "0"));
 
10446
  DIE_UNLESS(!strcmp(ulonglong_as_string, "0"));
 
10447
 
 
10448
  rc= mysql_stmt_fetch(stmt);
 
10449
 
 
10450
  if (!opt_silent)
 
10451
  {
 
10452
    printf("Truncation mask: ");
 
10453
    for (my_bind= bind_array; my_bind < bind_end; my_bind++)
 
10454
      printf("%d", (int) my_bind->error_value);
 
10455
    printf("\n");
 
10456
  }
 
10457
  DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED || rc == 0);
 
10458
 
 
10459
  DIE_UNLESS(int8_val == int8_max);
 
10460
  DIE_UNLESS(uint8_val == uint8_max);
 
10461
  DIE_UNLESS(int16_val == int16_max);
 
10462
  DIE_UNLESS(uint16_val == uint16_max);
 
10463
  DIE_UNLESS(int32_val == int32_max);
 
10464
  DIE_UNLESS(uint32_val == uint32_max);
 
10465
  DIE_UNLESS(int64_val == int64_max);
 
10466
  DIE_UNLESS(uint64_val == uint64_max);
 
10467
  DIE_UNLESS(double_val == (longlong) uint64_val);
 
10468
  double_tmp= ulonglong2double(uint64_val);
 
10469
  DIE_UNLESS(cmp_double(&udouble_val, &double_tmp));
 
10470
  DIE_UNLESS(!strcmp(longlong_as_string, "-1"));
 
10471
  DIE_UNLESS(!strcmp(ulonglong_as_string, "18446744073709551615"));
 
10472
 
 
10473
  rc= mysql_stmt_fetch(stmt);
 
10474
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
10475
 
 
10476
  mysql_stmt_close(stmt);
 
10477
 
 
10478
  stmt_text= "DROP TABLE t1";
 
10479
  mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10480
}
 
10481
 
 
10482
 
 
10483
static void test_union2()
 
10484
{
 
10485
  MYSQL_STMT *stmt;
 
10486
  int rc, i;
 
10487
 
 
10488
  myheader("test_union2");
 
10489
 
 
10490
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
10491
  myquery(rc);
 
10492
 
 
10493
  rc= mysql_query(mysql, "CREATE TABLE t1(col1 INT, \
 
10494
                                         col2 VARCHAR(40),      \
 
10495
                                         col3 SMALLINT, \
 
10496
                                         col4 TIMESTAMP)");
 
10497
  myquery(rc);
 
10498
 
 
10499
  stmt= mysql_simple_prepare(mysql,
 
10500
                             "select col1 FROM t1 where col1=1 union distinct "
 
10501
                             "select col1 FROM t1 where col1=2");
 
10502
  check_stmt(stmt);
 
10503
 
 
10504
  for (i= 0; i < 3; i++)
 
10505
  {
 
10506
    rc= mysql_stmt_execute(stmt);
 
10507
    check_execute(stmt, rc);
 
10508
    rc= my_process_stmt_result(stmt);
 
10509
    DIE_UNLESS(rc == 0);
 
10510
  }
 
10511
 
 
10512
  mysql_stmt_close(stmt);
 
10513
 
 
10514
  rc= mysql_query(mysql, "DROP TABLE t1");
 
10515
  myquery(rc);
 
10516
}
 
10517
 
 
10518
 
 
10519
/*
 
10520
  This tests for various mysql_stmt_send_long_data bugs described in #1664
 
10521
*/
 
10522
 
 
10523
static void test_bug1664()
 
10524
{
 
10525
    MYSQL_STMT *stmt;
 
10526
    int        rc, int_data;
 
10527
    const char *data;
 
10528
    const char *str_data= "Simple string";
 
10529
    MYSQL_BIND my_bind[2];
 
10530
    const char *query= "INSERT INTO test_long_data(col2, col1) VALUES(?, ?)";
 
10531
 
 
10532
    myheader("test_bug1664");
 
10533
 
 
10534
    rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_long_data");
 
10535
    myquery(rc);
 
10536
 
 
10537
    rc= mysql_query(mysql, "CREATE TABLE test_long_data(col1 int, col2 long varchar)");
 
10538
    myquery(rc);
 
10539
 
 
10540
    stmt= mysql_stmt_init(mysql);
 
10541
    check_stmt(stmt);
 
10542
    rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
10543
    check_execute(stmt, rc);
 
10544
 
 
10545
    verify_param_count(stmt, 2);
 
10546
 
 
10547
    bzero((char*) my_bind, sizeof(my_bind));
 
10548
 
 
10549
    my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
10550
    my_bind[0].buffer= (void *)str_data;
 
10551
    my_bind[0].buffer_length= strlen(str_data);
 
10552
 
 
10553
    my_bind[1].buffer= (void *)&int_data;
 
10554
    my_bind[1].buffer_type= MYSQL_TYPE_LONG;
 
10555
 
 
10556
    rc= mysql_stmt_bind_param(stmt, my_bind);
 
10557
    check_execute(stmt, rc);
 
10558
 
 
10559
    int_data= 1;
 
10560
 
 
10561
    /*
 
10562
      Let us supply empty long_data. This should work and should
 
10563
      not break following execution.
 
10564
    */
 
10565
    data= "";
 
10566
    rc= mysql_stmt_send_long_data(stmt, 0, data, strlen(data));
 
10567
    check_execute(stmt, rc);
 
10568
 
 
10569
    rc= mysql_stmt_execute(stmt);
 
10570
    check_execute(stmt, rc);
 
10571
 
 
10572
    verify_col_data("test_long_data", "col1", "1");
 
10573
    verify_col_data("test_long_data", "col2", "");
 
10574
 
 
10575
    rc= mysql_query(mysql, "DELETE FROM test_long_data");
 
10576
    myquery(rc);
 
10577
 
 
10578
    /* This should pass OK */
 
10579
    data= (char *)"Data";
 
10580
    rc= mysql_stmt_send_long_data(stmt, 0, data, strlen(data));
 
10581
    check_execute(stmt, rc);
 
10582
 
 
10583
    rc= mysql_stmt_execute(stmt);
 
10584
    check_execute(stmt, rc);
 
10585
 
 
10586
    verify_col_data("test_long_data", "col1", "1");
 
10587
    verify_col_data("test_long_data", "col2", "Data");
 
10588
 
 
10589
    /* clean up */
 
10590
    rc= mysql_query(mysql, "DELETE FROM test_long_data");
 
10591
    myquery(rc);
 
10592
 
 
10593
    /*
 
10594
      Now we are changing int parameter and don't do anything
 
10595
      with first parameter. Second mysql_stmt_execute() should run
 
10596
      OK treating this first parameter as string parameter.
 
10597
    */
 
10598
 
 
10599
    int_data= 2;
 
10600
    /* execute */
 
10601
    rc= mysql_stmt_execute(stmt);
 
10602
    check_execute(stmt, rc);
 
10603
 
 
10604
    verify_col_data("test_long_data", "col1", "2");
 
10605
    verify_col_data("test_long_data", "col2", str_data);
 
10606
 
 
10607
    /* clean up */
 
10608
    rc= mysql_query(mysql, "DELETE FROM test_long_data");
 
10609
    myquery(rc);
 
10610
 
 
10611
    /*
 
10612
      Now we are sending other long data. It should not be
 
10613
      concatened to previous.
 
10614
    */
 
10615
 
 
10616
    data= (char *)"SomeOtherData";
 
10617
    rc= mysql_stmt_send_long_data(stmt, 0, data, strlen(data));
 
10618
    check_execute(stmt, rc);
 
10619
 
 
10620
    rc= mysql_stmt_execute(stmt);
 
10621
    check_execute(stmt, rc);
 
10622
 
 
10623
    verify_col_data("test_long_data", "col1", "2");
 
10624
    verify_col_data("test_long_data", "col2", "SomeOtherData");
 
10625
 
 
10626
    mysql_stmt_close(stmt);
 
10627
 
 
10628
    /* clean up */
 
10629
    rc= mysql_query(mysql, "DELETE FROM test_long_data");
 
10630
    myquery(rc);
 
10631
 
 
10632
    /* Now let us test how mysql_stmt_reset works. */
 
10633
    stmt= mysql_stmt_init(mysql);
 
10634
    check_stmt(stmt);
 
10635
    rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
10636
    check_execute(stmt, rc);
 
10637
    rc= mysql_stmt_bind_param(stmt, my_bind);
 
10638
    check_execute(stmt, rc);
 
10639
 
 
10640
    data= (char *)"SomeData";
 
10641
    rc= mysql_stmt_send_long_data(stmt, 0, data, strlen(data));
 
10642
    check_execute(stmt, rc);
 
10643
 
 
10644
    rc= mysql_stmt_reset(stmt);
 
10645
    check_execute(stmt, rc);
 
10646
 
 
10647
    rc= mysql_stmt_execute(stmt);
 
10648
    check_execute(stmt, rc);
 
10649
 
 
10650
    verify_col_data("test_long_data", "col1", "2");
 
10651
    verify_col_data("test_long_data", "col2", str_data);
 
10652
 
 
10653
    mysql_stmt_close(stmt);
 
10654
 
 
10655
    /* Final clean up */
 
10656
    rc= mysql_query(mysql, "DROP TABLE test_long_data");
 
10657
    myquery(rc);
 
10658
}
 
10659
 
 
10660
 
 
10661
static void test_order_param()
 
10662
{
 
10663
  MYSQL_STMT *stmt;
 
10664
  int rc;
 
10665
 
 
10666
  myheader("test_order_param");
 
10667
 
 
10668
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
10669
  myquery(rc);
 
10670
 
 
10671
  rc= mysql_query(mysql, "CREATE TABLE t1(a INT, b char(10))");
 
10672
  myquery(rc);
 
10673
 
 
10674
  stmt= mysql_simple_prepare(mysql,
 
10675
                             "select sum(a) + 200, 1 from t1 "
 
10676
                             " union distinct "
 
10677
                             "select sum(a) + 200, 1 from t1 group by b ");
 
10678
  check_stmt(stmt);
 
10679
  mysql_stmt_close(stmt);
 
10680
 
 
10681
  stmt= mysql_simple_prepare(mysql,
 
10682
                             "select sum(a) + 200, ? from t1 group by b "
 
10683
                             " union distinct "
 
10684
                             "select sum(a) + 200, 1 from t1 group by b ");
 
10685
  check_stmt(stmt);
 
10686
  mysql_stmt_close(stmt);
 
10687
 
 
10688
  stmt= mysql_simple_prepare(mysql,
 
10689
                             "select sum(a) + 200, ? from t1 "
 
10690
                             " union distinct "
 
10691
                             "select sum(a) + 200, 1 from t1 group by b ");
 
10692
  check_stmt(stmt);
 
10693
  mysql_stmt_close(stmt);
 
10694
 
 
10695
  rc= mysql_query(mysql, "DROP TABLE t1");
 
10696
  myquery(rc);
 
10697
}
 
10698
 
 
10699
 
 
10700
static void test_union_param()
 
10701
{
 
10702
  MYSQL_STMT *stmt;
 
10703
  char *query;
 
10704
  int rc, i;
 
10705
  MYSQL_BIND      my_bind[2];
 
10706
  char            my_val[4];
 
10707
  ulong           my_length= 3L;
 
10708
  my_bool         my_null= FALSE;
 
10709
  myheader("test_union_param");
 
10710
 
 
10711
  strmov(my_val, "abc");
 
10712
 
 
10713
  query= (char*)"select ? as my_col union distinct select ?";
 
10714
  stmt= mysql_simple_prepare(mysql, query);
 
10715
  check_stmt(stmt);
 
10716
 
 
10717
  /*
 
10718
    We need to bzero bind structure because mysql_stmt_bind_param checks all
 
10719
    its members.
 
10720
  */
 
10721
  bzero((char*) my_bind, sizeof(my_bind));
 
10722
 
 
10723
  /* bind parameters */
 
10724
  my_bind[0].buffer_type=    MYSQL_TYPE_STRING;
 
10725
  my_bind[0].buffer=         (char*) &my_val;
 
10726
  my_bind[0].buffer_length=  4;
 
10727
  my_bind[0].length=         &my_length;
 
10728
  my_bind[0].is_null=        (char*)&my_null;
 
10729
  my_bind[1].buffer_type=    MYSQL_TYPE_STRING;
 
10730
  my_bind[1].buffer=         (char*) &my_val;
 
10731
  my_bind[1].buffer_length=  4;
 
10732
  my_bind[1].length=         &my_length;
 
10733
  my_bind[1].is_null=        (char*)&my_null;
 
10734
 
 
10735
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
10736
  check_execute(stmt, rc);
 
10737
 
 
10738
  for (i= 0; i < 3; i++)
 
10739
  {
 
10740
    rc= mysql_stmt_execute(stmt);
 
10741
    check_execute(stmt, rc);
 
10742
    rc= my_process_stmt_result(stmt);
 
10743
    DIE_UNLESS(rc == 1);
 
10744
  }
 
10745
 
 
10746
  mysql_stmt_close(stmt);
 
10747
}
 
10748
 
 
10749
 
 
10750
static void test_ps_i18n()
 
10751
{
 
10752
  MYSQL_STMT *stmt;
 
10753
  int rc;
 
10754
  const char *stmt_text;
 
10755
  MYSQL_BIND bind_array[2];
 
10756
 
 
10757
  /* Represented as numbers to keep UTF8 tools from clobbering them. */
 
10758
  const char *koi8= "\xee\xd5\x2c\x20\xda\xc1\x20\xd2\xd9\xc2\xc1\xcc\xcb\xd5";
 
10759
  const char *cp1251= "\xcd\xf3\x2c\x20\xe7\xe0\x20\xf0\xfb\xe1\xe0\xeb\xea\xf3";
 
10760
  char buf1[16], buf2[16];
 
10761
  ulong buf1_len, buf2_len;
 
10762
 
 
10763
 
 
10764
  myheader("test_ps_i18n");
 
10765
 
 
10766
  stmt_text= "DROP TABLE IF EXISTS t1";
 
10767
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10768
  myquery(rc);
 
10769
 
 
10770
  /*
 
10771
    Create table with binary columns, set session character set to cp1251,
 
10772
    client character set to koi8, and make sure that there is conversion
 
10773
    on insert and no conversion on select
 
10774
  */
 
10775
 
 
10776
  stmt_text= "CREATE TABLE t1 (c1 VARBINARY(255), c2 VARBINARY(255))";
 
10777
 
 
10778
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10779
  myquery(rc);
 
10780
 
 
10781
  stmt_text= "SET CHARACTER_SET_CLIENT=koi8r, "
 
10782
                 "CHARACTER_SET_CONNECTION=cp1251, "
 
10783
                 "CHARACTER_SET_RESULTS=koi8r";
 
10784
 
 
10785
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10786
  myquery(rc);
 
10787
 
 
10788
  bzero((char*) bind_array, sizeof(bind_array));
 
10789
 
 
10790
  bind_array[0].buffer_type= MYSQL_TYPE_STRING;
 
10791
  bind_array[0].buffer= (void *) koi8;
 
10792
  bind_array[0].buffer_length= strlen(koi8);
 
10793
 
 
10794
  bind_array[1].buffer_type= MYSQL_TYPE_STRING;
 
10795
  bind_array[1].buffer= (void *) koi8;
 
10796
  bind_array[1].buffer_length= strlen(koi8);
 
10797
 
 
10798
  stmt= mysql_stmt_init(mysql);
 
10799
  check_stmt(stmt);
 
10800
 
 
10801
  stmt_text= "INSERT INTO t1 (c1, c2) VALUES (?, ?)";
 
10802
 
 
10803
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
10804
  check_execute(stmt, rc);
 
10805
 
 
10806
  mysql_stmt_bind_param(stmt, bind_array);
 
10807
 
 
10808
  mysql_stmt_send_long_data(stmt, 0, koi8, strlen(koi8));
 
10809
 
 
10810
  rc= mysql_stmt_execute(stmt);
 
10811
  check_execute(stmt, rc);
 
10812
 
 
10813
  stmt_text= "SELECT c1, c2 FROM t1";
 
10814
 
 
10815
  /* c1 and c2 are binary so no conversion will be done on select */
 
10816
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
10817
  check_execute(stmt, rc);
 
10818
 
 
10819
  rc= mysql_stmt_execute(stmt);
 
10820
  check_execute(stmt, rc);
 
10821
 
 
10822
  bind_array[0].buffer= buf1;
 
10823
  bind_array[0].buffer_length= sizeof(buf1);
 
10824
  bind_array[0].length= &buf1_len;
 
10825
 
 
10826
  bind_array[1].buffer= buf2;
 
10827
  bind_array[1].buffer_length= sizeof(buf2);
 
10828
  bind_array[1].length= &buf2_len;
 
10829
 
 
10830
  mysql_stmt_bind_result(stmt, bind_array);
 
10831
 
 
10832
  rc= mysql_stmt_fetch(stmt);
 
10833
  check_execute(stmt, rc);
 
10834
 
 
10835
  DIE_UNLESS(buf1_len == strlen(cp1251));
 
10836
  DIE_UNLESS(buf2_len == strlen(cp1251));
 
10837
  DIE_UNLESS(!memcmp(buf1, cp1251, buf1_len));
 
10838
  DIE_UNLESS(!memcmp(buf2, cp1251, buf1_len));
 
10839
 
 
10840
  rc= mysql_stmt_fetch(stmt);
 
10841
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
10842
 
 
10843
  stmt_text= "DROP TABLE IF EXISTS t1";
 
10844
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10845
  myquery(rc);
 
10846
 
 
10847
  /*
 
10848
    Now create table with two cp1251 columns, set client character
 
10849
    set to koi8 and supply columns of one row as string and another as
 
10850
    binary data. Binary data must not be converted on insert, and both
 
10851
    columns must be converted to client character set on select.
 
10852
  */
 
10853
 
 
10854
  stmt_text= "CREATE TABLE t1 (c1 VARCHAR(255) CHARACTER SET cp1251, "
 
10855
                              "c2 VARCHAR(255) CHARACTER SET cp1251)";
 
10856
 
 
10857
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10858
  myquery(rc);
 
10859
 
 
10860
  stmt_text= "INSERT INTO t1 (c1, c2) VALUES (?, ?)";
 
10861
 
 
10862
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
10863
  check_execute(stmt, rc);
 
10864
 
 
10865
  /* this data must be converted */
 
10866
  bind_array[0].buffer_type= MYSQL_TYPE_STRING;
 
10867
  bind_array[0].buffer= (void *) koi8;
 
10868
  bind_array[0].buffer_length= strlen(koi8);
 
10869
 
 
10870
  bind_array[1].buffer_type= MYSQL_TYPE_STRING;
 
10871
  bind_array[1].buffer= (void *) koi8;
 
10872
  bind_array[1].buffer_length= strlen(koi8);
 
10873
 
 
10874
  mysql_stmt_bind_param(stmt, bind_array);
 
10875
 
 
10876
  mysql_stmt_send_long_data(stmt, 0, koi8, strlen(koi8));
 
10877
 
 
10878
  rc= mysql_stmt_execute(stmt);
 
10879
  check_execute(stmt, rc);
 
10880
 
 
10881
  /* this data must not be converted */
 
10882
  bind_array[0].buffer_type= MYSQL_TYPE_BLOB;
 
10883
  bind_array[0].buffer= (void *) cp1251;
 
10884
  bind_array[0].buffer_length= strlen(cp1251);
 
10885
 
 
10886
  bind_array[1].buffer_type= MYSQL_TYPE_BLOB;
 
10887
  bind_array[1].buffer= (void *) cp1251;
 
10888
  bind_array[1].buffer_length= strlen(cp1251);
 
10889
 
 
10890
  mysql_stmt_bind_param(stmt, bind_array);
 
10891
 
 
10892
  mysql_stmt_send_long_data(stmt, 0, cp1251, strlen(cp1251));
 
10893
 
 
10894
  rc= mysql_stmt_execute(stmt);
 
10895
  check_execute(stmt, rc);
 
10896
 
 
10897
  /* Fetch data and verify that rows are in koi8 */
 
10898
 
 
10899
  stmt_text= "SELECT c1, c2 FROM t1";
 
10900
 
 
10901
  /* c1 and c2 are binary so no conversion will be done on select */
 
10902
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
10903
  check_execute(stmt, rc);
 
10904
 
 
10905
  rc= mysql_stmt_execute(stmt);
 
10906
  check_execute(stmt, rc);
 
10907
 
 
10908
  bind_array[0].buffer= buf1;
 
10909
  bind_array[0].buffer_length= sizeof(buf1);
 
10910
  bind_array[0].length= &buf1_len;
 
10911
 
 
10912
  bind_array[1].buffer= buf2;
 
10913
  bind_array[1].buffer_length= sizeof(buf2);
 
10914
  bind_array[1].length= &buf2_len;
 
10915
 
 
10916
  mysql_stmt_bind_result(stmt, bind_array);
 
10917
 
 
10918
  while ((rc= mysql_stmt_fetch(stmt)) == 0)
 
10919
  {
 
10920
    DIE_UNLESS(buf1_len == strlen(koi8));
 
10921
    DIE_UNLESS(buf2_len == strlen(koi8));
 
10922
    DIE_UNLESS(!memcmp(buf1, koi8, buf1_len));
 
10923
    DIE_UNLESS(!memcmp(buf2, koi8, buf1_len));
 
10924
  }
 
10925
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
10926
  mysql_stmt_close(stmt);
 
10927
 
 
10928
  stmt_text= "DROP TABLE t1";
 
10929
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10930
  myquery(rc);
 
10931
  stmt_text= "SET NAMES DEFAULT";
 
10932
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10933
  myquery(rc);
 
10934
}
 
10935
 
 
10936
 
 
10937
static void test_bug3796()
 
10938
{
 
10939
  MYSQL_STMT *stmt;
 
10940
  MYSQL_BIND my_bind[1];
 
10941
  const char *concat_arg0= "concat_with_";
 
10942
  enum { OUT_BUFF_SIZE= 30 };
 
10943
  char out_buff[OUT_BUFF_SIZE];
 
10944
  char canonical_buff[OUT_BUFF_SIZE];
 
10945
  ulong out_length;
 
10946
  const char *stmt_text;
 
10947
  int rc;
 
10948
 
 
10949
  myheader("test_bug3796");
 
10950
 
 
10951
  /* Create and fill test table */
 
10952
  stmt_text= "DROP TABLE IF EXISTS t1";
 
10953
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10954
  myquery(rc);
 
10955
 
 
10956
  stmt_text= "CREATE TABLE t1 (a INT, b VARCHAR(30))";
 
10957
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10958
  myquery(rc);
 
10959
 
 
10960
  stmt_text= "INSERT INTO t1 VALUES(1, 'ONE'), (2, 'TWO')";
 
10961
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
10962
  myquery(rc);
 
10963
 
 
10964
  /* Create statement handle and prepare it with select */
 
10965
  stmt= mysql_stmt_init(mysql);
 
10966
  stmt_text= "SELECT concat(?, b) FROM t1";
 
10967
 
 
10968
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
10969
  check_execute(stmt, rc);
 
10970
 
 
10971
  /* Bind input buffers */
 
10972
  bzero((char*) my_bind, sizeof(my_bind));
 
10973
 
 
10974
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
10975
  my_bind[0].buffer= (void *) concat_arg0;
 
10976
  my_bind[0].buffer_length= strlen(concat_arg0);
 
10977
 
 
10978
  mysql_stmt_bind_param(stmt, my_bind);
 
10979
 
 
10980
  /* Execute the select statement */
 
10981
  rc= mysql_stmt_execute(stmt);
 
10982
  check_execute(stmt, rc);
 
10983
 
 
10984
  my_bind[0].buffer= (void *) out_buff;
 
10985
  my_bind[0].buffer_length= OUT_BUFF_SIZE;
 
10986
  my_bind[0].length= &out_length;
 
10987
 
 
10988
  mysql_stmt_bind_result(stmt, my_bind);
 
10989
 
 
10990
  rc= mysql_stmt_fetch(stmt);
 
10991
  if (!opt_silent)
 
10992
    printf("Concat result: '%s'\n", out_buff);
 
10993
  check_execute(stmt, rc);
 
10994
  strmov(canonical_buff, concat_arg0);
 
10995
  strcat(canonical_buff, "ONE");
 
10996
  DIE_UNLESS(strlen(canonical_buff) == out_length &&
 
10997
         strncmp(out_buff, canonical_buff, out_length) == 0);
 
10998
 
 
10999
  rc= mysql_stmt_fetch(stmt);
 
11000
  check_execute(stmt, rc);
 
11001
  strmov(canonical_buff + strlen(concat_arg0), "TWO");
 
11002
  DIE_UNLESS(strlen(canonical_buff) == out_length &&
 
11003
         strncmp(out_buff, canonical_buff, out_length) == 0);
 
11004
  if (!opt_silent)
 
11005
    printf("Concat result: '%s'\n", out_buff);
 
11006
 
 
11007
  rc= mysql_stmt_fetch(stmt);
 
11008
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
11009
 
 
11010
  mysql_stmt_close(stmt);
 
11011
 
 
11012
  stmt_text= "DROP TABLE IF EXISTS t1";
 
11013
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
11014
  myquery(rc);
 
11015
}
 
11016
 
 
11017
 
 
11018
static void test_bug4026()
 
11019
{
 
11020
  MYSQL_STMT *stmt;
 
11021
  MYSQL_BIND my_bind[2];
 
11022
  MYSQL_TIME time_in, time_out;
 
11023
  MYSQL_TIME datetime_in, datetime_out;
 
11024
  const char *stmt_text;
 
11025
  int rc;
 
11026
 
 
11027
  myheader("test_bug4026");
 
11028
 
 
11029
  /* Check that microseconds are inserted and selected successfully */
 
11030
 
 
11031
  /* Create a statement handle and prepare it with select */
 
11032
  stmt= mysql_stmt_init(mysql);
 
11033
  stmt_text= "SELECT ?, ?";
 
11034
 
 
11035
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
11036
  check_execute(stmt, rc);
 
11037
 
 
11038
  /* Bind input buffers */
 
11039
  bzero((char*) my_bind, sizeof(my_bind));
 
11040
  bzero((char*) &time_in, sizeof(time_in));
 
11041
  bzero((char*) &time_out, sizeof(time_out));
 
11042
  bzero((char*) &datetime_in, sizeof(datetime_in));
 
11043
  bzero((char*) &datetime_out, sizeof(datetime_out));
 
11044
 
 
11045
  my_bind[0].buffer_type= MYSQL_TYPE_TIME;
 
11046
  my_bind[0].buffer= (void *) &time_in;
 
11047
  my_bind[1].buffer_type= MYSQL_TYPE_DATETIME;
 
11048
  my_bind[1].buffer= (void *) &datetime_in;
 
11049
 
 
11050
  time_in.hour= 23;
 
11051
  time_in.minute= 59;
 
11052
  time_in.second= 59;
 
11053
  time_in.second_part= 123456;
 
11054
  /*
 
11055
    This is not necessary, just to make DIE_UNLESS below work: this field
 
11056
    is filled in when time is received from server
 
11057
  */
 
11058
  time_in.time_type= MYSQL_TIMESTAMP_TIME;
 
11059
 
 
11060
  datetime_in= time_in;
 
11061
  datetime_in.year= 2003;
 
11062
  datetime_in.month= 12;
 
11063
  datetime_in.day= 31;
 
11064
  datetime_in.time_type= MYSQL_TIMESTAMP_DATETIME;
 
11065
 
 
11066
  mysql_stmt_bind_param(stmt, my_bind);
 
11067
 
 
11068
  /* Execute the select statement */
 
11069
  rc= mysql_stmt_execute(stmt);
 
11070
  check_execute(stmt, rc);
 
11071
 
 
11072
  my_bind[0].buffer= (void *) &time_out;
 
11073
  my_bind[1].buffer= (void *) &datetime_out;
 
11074
 
 
11075
  mysql_stmt_bind_result(stmt, my_bind);
 
11076
 
 
11077
  rc= mysql_stmt_fetch(stmt);
 
11078
  DIE_UNLESS(rc == 0);
 
11079
  if (!opt_silent)
 
11080
  {
 
11081
    printf("%d:%d:%d.%lu\n", time_out.hour, time_out.minute, time_out.second,
 
11082
           time_out.second_part);
 
11083
    printf("%d-%d-%d %d:%d:%d.%lu\n", datetime_out.year, datetime_out.month,
 
11084
           datetime_out.day, datetime_out.hour,
 
11085
           datetime_out.minute, datetime_out.second,
 
11086
           datetime_out.second_part);
 
11087
  }
 
11088
  DIE_UNLESS(memcmp(&time_in, &time_out, sizeof(time_in)) == 0);
 
11089
  DIE_UNLESS(memcmp(&datetime_in, &datetime_out, sizeof(datetime_in)) == 0);
 
11090
  mysql_stmt_close(stmt);
 
11091
}
 
11092
 
 
11093
 
 
11094
static void test_bug4079()
 
11095
{
 
11096
  MYSQL_STMT *stmt;
 
11097
  MYSQL_BIND my_bind[1];
 
11098
  const char *stmt_text;
 
11099
  uint32 res;
 
11100
  int rc;
 
11101
 
 
11102
  myheader("test_bug4079");
 
11103
 
 
11104
  /* Create and fill table */
 
11105
  mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
11106
  mysql_query(mysql, "CREATE TABLE t1 (a int)");
 
11107
  mysql_query(mysql, "INSERT INTO t1 VALUES (1), (2)");
 
11108
 
 
11109
  /* Prepare erroneous statement */
 
11110
  stmt= mysql_stmt_init(mysql);
 
11111
  stmt_text= "SELECT 1 < (SELECT a FROM t1)";
 
11112
 
 
11113
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
11114
  check_execute(stmt, rc);
 
11115
 
 
11116
  /* Execute the select statement */
 
11117
  rc= mysql_stmt_execute(stmt);
 
11118
  check_execute(stmt, rc);
 
11119
 
 
11120
  /* Bind input buffers */
 
11121
  bzero((char*) my_bind, sizeof(my_bind));
 
11122
 
 
11123
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
11124
  my_bind[0].buffer= (void *) &res;
 
11125
 
 
11126
  mysql_stmt_bind_result(stmt, my_bind);
 
11127
 
 
11128
  rc= mysql_stmt_fetch(stmt);
 
11129
  DIE_UNLESS(rc != 0 && rc != MYSQL_NO_DATA);
 
11130
  if (!opt_silent)
 
11131
    printf("Got error from mysql_stmt_fetch (as expected):\n%s\n",
 
11132
           mysql_stmt_error(stmt));
 
11133
  /* buggy version of libmysql hanged up here */
 
11134
  mysql_stmt_close(stmt);
 
11135
}
 
11136
 
 
11137
 
 
11138
static void test_bug4236()
 
11139
{
 
11140
  MYSQL_STMT *stmt;
 
11141
  const char *stmt_text;
 
11142
  int rc;
 
11143
  MYSQL_STMT backup;
 
11144
 
 
11145
  myheader("test_bug4236");
 
11146
 
 
11147
  stmt= mysql_stmt_init(mysql);
 
11148
 
 
11149
  /* mysql_stmt_execute() of statement with statement id= 0 crashed server */
 
11150
  stmt_text= "SELECT 1";
 
11151
  /* We need to prepare statement to pass by possible check in libmysql */
 
11152
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
11153
  check_execute(stmt, rc);
 
11154
  /* Hack to check that server works OK if statement wasn't found */
 
11155
  backup.stmt_id= stmt->stmt_id;
 
11156
  stmt->stmt_id= 0;
 
11157
  rc= mysql_stmt_execute(stmt);
 
11158
  DIE_UNLESS(rc);
 
11159
  /* Restore original statement id to be able to reprepare it */
 
11160
  stmt->stmt_id= backup.stmt_id;
 
11161
 
 
11162
  mysql_stmt_close(stmt);
 
11163
}
 
11164
 
 
11165
 
 
11166
static void test_bug4030()
 
11167
{
 
11168
  MYSQL_STMT *stmt;
 
11169
  MYSQL_BIND my_bind[3];
 
11170
  MYSQL_TIME time_canonical, time_out;
 
11171
  MYSQL_TIME date_canonical, date_out;
 
11172
  MYSQL_TIME datetime_canonical, datetime_out;
 
11173
  const char *stmt_text;
 
11174
  int rc;
 
11175
 
 
11176
  myheader("test_bug4030");
 
11177
 
 
11178
  /* Check that microseconds are inserted and selected successfully */
 
11179
 
 
11180
  /* Execute a query with time values in prepared mode */
 
11181
  stmt= mysql_stmt_init(mysql);
 
11182
  stmt_text= "SELECT '23:59:59.123456', '2003-12-31', "
 
11183
             "'2003-12-31 23:59:59.123456'";
 
11184
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
11185
  check_execute(stmt, rc);
 
11186
  rc= mysql_stmt_execute(stmt);
 
11187
  check_execute(stmt, rc);
 
11188
 
 
11189
  /* Bind output buffers */
 
11190
  bzero((char*) my_bind, sizeof(my_bind));
 
11191
  bzero((char*) &time_canonical, sizeof(time_canonical));
 
11192
  bzero((char*) &time_out, sizeof(time_out));
 
11193
  bzero((char*) &date_canonical, sizeof(date_canonical));
 
11194
  bzero((char*) &date_out, sizeof(date_out));
 
11195
  bzero((char*) &datetime_canonical, sizeof(datetime_canonical));
 
11196
  bzero((char*) &datetime_out, sizeof(datetime_out));
 
11197
 
 
11198
  my_bind[0].buffer_type= MYSQL_TYPE_TIME;
 
11199
  my_bind[0].buffer= (void *) &time_out;
 
11200
  my_bind[1].buffer_type= MYSQL_TYPE_DATE;
 
11201
  my_bind[1].buffer= (void *) &date_out;
 
11202
  my_bind[2].buffer_type= MYSQL_TYPE_DATETIME;
 
11203
  my_bind[2].buffer= (void *) &datetime_out;
 
11204
 
 
11205
  time_canonical.hour= 23;
 
11206
  time_canonical.minute= 59;
 
11207
  time_canonical.second= 59;
 
11208
  time_canonical.second_part= 123456;
 
11209
  time_canonical.time_type= MYSQL_TIMESTAMP_TIME;
 
11210
 
 
11211
  date_canonical.year= 2003;
 
11212
  date_canonical.month= 12;
 
11213
  date_canonical.day= 31;
 
11214
  date_canonical.time_type= MYSQL_TIMESTAMP_DATE;
 
11215
 
 
11216
  datetime_canonical= time_canonical;
 
11217
  datetime_canonical.year= 2003;
 
11218
  datetime_canonical.month= 12;
 
11219
  datetime_canonical.day= 31;
 
11220
  datetime_canonical.time_type= MYSQL_TIMESTAMP_DATETIME;
 
11221
 
 
11222
  mysql_stmt_bind_result(stmt, my_bind);
 
11223
 
 
11224
  rc= mysql_stmt_fetch(stmt);
 
11225
  DIE_UNLESS(rc == 0);
 
11226
  if (!opt_silent)
 
11227
  {
 
11228
    printf("%d:%d:%d.%lu\n", time_out.hour, time_out.minute, time_out.second,
 
11229
           time_out.second_part);
 
11230
    printf("%d-%d-%d\n", date_out.year, date_out.month, date_out.day);
 
11231
    printf("%d-%d-%d %d:%d:%d.%lu\n", datetime_out.year, datetime_out.month,
 
11232
           datetime_out.day, datetime_out.hour,
 
11233
           datetime_out.minute, datetime_out.second,
 
11234
           datetime_out.second_part);
 
11235
  }
 
11236
  DIE_UNLESS(memcmp(&time_canonical, &time_out, sizeof(time_out)) == 0);
 
11237
  DIE_UNLESS(memcmp(&date_canonical, &date_out, sizeof(date_out)) == 0);
 
11238
  DIE_UNLESS(memcmp(&datetime_canonical, &datetime_out, sizeof(datetime_out)) == 0);
 
11239
  mysql_stmt_close(stmt);
 
11240
}
 
11241
 
 
11242
static void test_view()
 
11243
{
 
11244
  MYSQL_STMT *stmt;
 
11245
  int rc, i;
 
11246
  MYSQL_BIND      my_bind[1];
 
11247
  char            str_data[50];
 
11248
  ulong           length = 0L;
 
11249
  long            is_null = 0L;
 
11250
  const char *query=
 
11251
    "SELECT COUNT(*) FROM v1 WHERE SERVERNAME=?";
 
11252
 
 
11253
  myheader("test_view");
 
11254
 
 
11255
  rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,t2,t3,v1");
 
11256
  myquery(rc);
 
11257
 
 
11258
  rc = mysql_query(mysql, "DROP VIEW IF EXISTS v1,t1,t2,t3");
 
11259
  myquery(rc);
 
11260
  rc= mysql_query(mysql,"CREATE TABLE t1 ("
 
11261
                        " SERVERGRP varchar(20) NOT NULL default '', "
 
11262
                        " DBINSTANCE varchar(20) NOT NULL default '', "
 
11263
                        " PRIMARY KEY  (SERVERGRP)) "
 
11264
                        " CHARSET=latin1 collate=latin1_bin");
 
11265
  myquery(rc);
 
11266
  rc= mysql_query(mysql,"CREATE TABLE t2 ("
 
11267
                        " SERVERNAME varchar(20) NOT NULL, "
 
11268
                        " SERVERGRP varchar(20) NOT NULL, "
 
11269
                        " PRIMARY KEY (SERVERNAME)) "
 
11270
                        " CHARSET=latin1 COLLATE latin1_bin");
 
11271
  myquery(rc);
 
11272
  rc= mysql_query(mysql,
 
11273
                  "CREATE TABLE t3 ("
 
11274
                  " SERVERGRP varchar(20) BINARY NOT NULL, "
 
11275
                  " TABNAME varchar(30) NOT NULL, MAPSTATE char(1) NOT NULL, "
 
11276
                  " ACTSTATE char(1) NOT NULL , "
 
11277
                  " LOCAL_NAME varchar(30) NOT NULL, "
 
11278
                  " CHG_DATE varchar(8) NOT NULL default '00000000', "
 
11279
                  " CHG_TIME varchar(6) NOT NULL default '000000', "
 
11280
                  " MXUSER varchar(12) NOT NULL default '', "
 
11281
                  " PRIMARY KEY (SERVERGRP, TABNAME, MAPSTATE, ACTSTATE, "
 
11282
                  " LOCAL_NAME)) CHARSET=latin1 COLLATE latin1_bin");
 
11283
  myquery(rc);
 
11284
  rc= mysql_query(mysql,"CREATE VIEW v1 AS select sql_no_cache"
 
11285
                  " T0001.SERVERNAME AS SERVERNAME, T0003.TABNAME AS"
 
11286
                  " TABNAME,T0003.LOCAL_NAME AS LOCAL_NAME,T0002.DBINSTANCE AS"
 
11287
                  " DBINSTANCE from t2 T0001 join t1 T0002 join t3 T0003 where"
 
11288
                  " ((T0002.SERVERGRP = T0001.SERVERGRP) and"
 
11289
                  " (T0002.SERVERGRP = T0003.SERVERGRP)"
 
11290
                  " and (T0003.MAPSTATE = _latin1'A') and"
 
11291
                  " (T0003.ACTSTATE = _latin1' '))");
 
11292
  myquery(rc);
 
11293
 
 
11294
  stmt= mysql_stmt_init(mysql);
 
11295
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
11296
  check_execute(stmt, rc);
 
11297
 
 
11298
  strmov(str_data, "TEST");
 
11299
  bzero((char*) my_bind, sizeof(my_bind));
 
11300
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
11301
  my_bind[0].buffer= (char *)&str_data;
 
11302
  my_bind[0].buffer_length= 50;
 
11303
  my_bind[0].length= &length;
 
11304
  length= 4;
 
11305
  my_bind[0].is_null= (char*)&is_null;
 
11306
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
11307
  check_execute(stmt,rc);
 
11308
 
 
11309
  for (i= 0; i < 3; i++)
 
11310
  {
 
11311
    rc= mysql_stmt_execute(stmt);
 
11312
    check_execute(stmt, rc);
 
11313
    rc= my_process_stmt_result(stmt);
 
11314
    DIE_UNLESS(1 == rc);
 
11315
  }
 
11316
  mysql_stmt_close(stmt);
 
11317
 
 
11318
  rc= mysql_query(mysql, "DROP TABLE t1,t2,t3");
 
11319
  myquery(rc);
 
11320
  rc= mysql_query(mysql, "DROP VIEW v1");
 
11321
  myquery(rc);
 
11322
}
 
11323
 
 
11324
 
 
11325
static void test_view_where()
 
11326
{
 
11327
  MYSQL_STMT *stmt;
 
11328
  int rc, i;
 
11329
  const char *query=
 
11330
    "select v1.c,v2.c from v1, v2";
 
11331
 
 
11332
  myheader("test_view_where");
 
11333
 
 
11334
  rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,v1,v2");
 
11335
  myquery(rc);
 
11336
 
 
11337
  rc = mysql_query(mysql, "DROP VIEW IF EXISTS v1,v2,t1");
 
11338
  myquery(rc);
 
11339
  rc= mysql_query(mysql,"CREATE TABLE t1 (a int, b int)");
 
11340
  myquery(rc);
 
11341
  rc= mysql_query(mysql,"insert into t1 values (1,2), (1,3), (2,4), (2,5), (3,10)");
 
11342
  myquery(rc);
 
11343
  rc= mysql_query(mysql,"create view v1 (c) as select b from t1 where a<3");
 
11344
  myquery(rc);
 
11345
  rc= mysql_query(mysql,"create view v2 (c) as select b from t1 where a>=3");
 
11346
  myquery(rc);
 
11347
 
 
11348
  stmt= mysql_stmt_init(mysql);
 
11349
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
11350
  check_execute(stmt, rc);
 
11351
 
 
11352
  for (i= 0; i < 3; i++)
 
11353
  {
 
11354
    rc= mysql_stmt_execute(stmt);
 
11355
    check_execute(stmt, rc);
 
11356
    rc= my_process_stmt_result(stmt);
 
11357
    DIE_UNLESS(4 == rc);
 
11358
  }
 
11359
  mysql_stmt_close(stmt);
 
11360
 
 
11361
  rc= mysql_query(mysql, "DROP TABLE t1");
 
11362
  myquery(rc);
 
11363
  rc= mysql_query(mysql, "DROP VIEW v1, v2");
 
11364
  myquery(rc);
 
11365
}
 
11366
 
 
11367
 
 
11368
static void test_view_2where()
 
11369
{
 
11370
  MYSQL_STMT *stmt;
 
11371
  int rc, i;
 
11372
  MYSQL_BIND      my_bind[8];
 
11373
  char            parms[8][100];
 
11374
  ulong           length[8];
 
11375
  const char *query=
 
11376
    "select relid, report, handle, log_group, username, variant, type, "
 
11377
    "version, erfdat, erftime, erfname, aedat, aetime, aename, dependvars, "
 
11378
    "inactive from V_LTDX where mandt = ? and relid = ? and report = ? and "
 
11379
    "handle = ? and log_group = ? and username in ( ? , ? ) and type = ?";
 
11380
 
 
11381
  myheader("test_view_2where");
 
11382
 
 
11383
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS LTDX");
 
11384
  myquery(rc);
 
11385
  rc= mysql_query(mysql, "DROP VIEW IF EXISTS V_LTDX");
 
11386
  myquery(rc);
 
11387
  rc= mysql_query(mysql,
 
11388
                  "CREATE TABLE LTDX (MANDT char(3) NOT NULL default '000', "
 
11389
                  " RELID char(2) NOT NULL, REPORT varchar(40) NOT NULL,"
 
11390
                  " HANDLE varchar(4) NOT NULL, LOG_GROUP varchar(4) NOT NULL,"
 
11391
                  " USERNAME varchar(12) NOT NULL,"
 
11392
                  " VARIANT varchar(12) NOT NULL,"
 
11393
                  " TYPE char(1) NOT NULL, SRTF2 int(11) NOT NULL,"
 
11394
                  " VERSION varchar(6) NOT NULL default '000000',"
 
11395
                  " ERFDAT varchar(8) NOT NULL default '00000000',"
 
11396
                  " ERFTIME varchar(6) NOT NULL default '000000',"
 
11397
                  " ERFNAME varchar(12) NOT NULL,"
 
11398
                  " AEDAT varchar(8) NOT NULL default '00000000',"
 
11399
                  " AETIME varchar(6) NOT NULL default '000000',"
 
11400
                  " AENAME varchar(12) NOT NULL,"
 
11401
                  " DEPENDVARS varchar(10) NOT NULL,"
 
11402
                  " INACTIVE char(1) NOT NULL, CLUSTR smallint(6) NOT NULL,"
 
11403
                  " CLUSTD blob,"
 
11404
                  " PRIMARY KEY (MANDT, RELID, REPORT, HANDLE, LOG_GROUP, "
 
11405
                                "USERNAME, VARIANT, TYPE, SRTF2))"
 
11406
                 " CHARSET=latin1 COLLATE latin1_bin");
 
11407
  myquery(rc);
 
11408
  rc= mysql_query(mysql,
 
11409
                  "CREATE VIEW V_LTDX AS select T0001.MANDT AS "
 
11410
                  " MANDT,T0001.RELID AS RELID,T0001.REPORT AS "
 
11411
                  " REPORT,T0001.HANDLE AS HANDLE,T0001.LOG_GROUP AS "
 
11412
                  " LOG_GROUP,T0001.USERNAME AS USERNAME,T0001.VARIANT AS "
 
11413
                  " VARIANT,T0001.TYPE AS TYPE,T0001.VERSION AS "
 
11414
                  " VERSION,T0001.ERFDAT AS ERFDAT,T0001.ERFTIME AS "
 
11415
                  " ERFTIME,T0001.ERFNAME AS ERFNAME,T0001.AEDAT AS "
 
11416
                  " AEDAT,T0001.AETIME AS AETIME,T0001.AENAME AS "
 
11417
                  " AENAME,T0001.DEPENDVARS AS DEPENDVARS,T0001.INACTIVE AS "
 
11418
                  " INACTIVE from LTDX T0001 where (T0001.SRTF2 = 0)");
 
11419
  myquery(rc);
 
11420
  bzero((char*) my_bind, sizeof(my_bind));
 
11421
  for (i=0; i < 8; i++) {
 
11422
    strmov(parms[i], "1");
 
11423
    my_bind[i].buffer_type = MYSQL_TYPE_VAR_STRING;
 
11424
    my_bind[i].buffer = (char *)&parms[i];
 
11425
    my_bind[i].buffer_length = 100;
 
11426
    my_bind[i].is_null = 0;
 
11427
    my_bind[i].length = &length[i];
 
11428
    length[i] = 1;
 
11429
  }
 
11430
  stmt= mysql_stmt_init(mysql);
 
11431
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
11432
  check_execute(stmt, rc);
 
11433
 
 
11434
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
11435
  check_execute(stmt,rc);
 
11436
 
 
11437
  rc= mysql_stmt_execute(stmt);
 
11438
  check_execute(stmt, rc);
 
11439
  rc= my_process_stmt_result(stmt);
 
11440
  DIE_UNLESS(0 == rc);
 
11441
 
 
11442
  mysql_stmt_close(stmt);
 
11443
 
 
11444
  rc= mysql_query(mysql, "DROP VIEW V_LTDX");
 
11445
  myquery(rc);
 
11446
  rc= mysql_query(mysql, "DROP TABLE LTDX");
 
11447
  myquery(rc);
 
11448
}
 
11449
 
 
11450
 
 
11451
static void test_view_star()
 
11452
{
 
11453
  MYSQL_STMT *stmt;
 
11454
  int rc, i;
 
11455
  MYSQL_BIND      my_bind[8];
 
11456
  char            parms[8][100];
 
11457
  ulong           length[8];
 
11458
  const char *query= "SELECT * FROM vt1 WHERE a IN (?,?)";
 
11459
 
 
11460
  myheader("test_view_star");
 
11461
 
 
11462
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, vt1");
 
11463
  myquery(rc);
 
11464
  rc= mysql_query(mysql, "DROP VIEW IF EXISTS t1, vt1");
 
11465
  myquery(rc);
 
11466
  rc= mysql_query(mysql, "CREATE TABLE t1 (a int)");
 
11467
  myquery(rc);
 
11468
  rc= mysql_query(mysql, "CREATE VIEW vt1 AS SELECT a FROM t1");
 
11469
  myquery(rc);
 
11470
  bzero((char*) my_bind, sizeof(my_bind));
 
11471
  for (i= 0; i < 2; i++) {
 
11472
    sprintf((char *)&parms[i], "%d", i);
 
11473
    my_bind[i].buffer_type = MYSQL_TYPE_VAR_STRING;
 
11474
    my_bind[i].buffer = (char *)&parms[i];
 
11475
    my_bind[i].buffer_length = 100;
 
11476
    my_bind[i].is_null = 0;
 
11477
    my_bind[i].length = &length[i];
 
11478
    length[i] = 1;
 
11479
  }
 
11480
 
 
11481
  stmt= mysql_stmt_init(mysql);
 
11482
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
11483
  check_execute(stmt, rc);
 
11484
 
 
11485
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
11486
  check_execute(stmt,rc);
 
11487
 
 
11488
  for (i= 0; i < 3; i++)
 
11489
  {
 
11490
    rc= mysql_stmt_execute(stmt);
 
11491
    check_execute(stmt, rc);
 
11492
    rc= my_process_stmt_result(stmt);
 
11493
    DIE_UNLESS(0 == rc);
 
11494
  }
 
11495
 
 
11496
  mysql_stmt_close(stmt);
 
11497
 
 
11498
  rc= mysql_query(mysql, "DROP TABLE t1");
 
11499
  myquery(rc);
 
11500
  rc= mysql_query(mysql, "DROP VIEW vt1");
 
11501
  myquery(rc);
 
11502
}
 
11503
 
 
11504
 
 
11505
static void test_view_insert()
 
11506
{
 
11507
  MYSQL_STMT *insert_stmt, *select_stmt;
 
11508
  int rc, i;
 
11509
  MYSQL_BIND      my_bind[1];
 
11510
  int             my_val = 0;
 
11511
  ulong           my_length = 0L;
 
11512
  long            my_null = 0L;
 
11513
  const char *query=
 
11514
    "insert into v1 values (?)";
 
11515
 
 
11516
  myheader("test_view_insert");
 
11517
 
 
11518
  rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,v1");
 
11519
  myquery(rc);
 
11520
  rc = mysql_query(mysql, "DROP VIEW IF EXISTS t1,v1");
 
11521
  myquery(rc);
 
11522
 
 
11523
  rc= mysql_query(mysql,"create table t1 (a int, primary key (a))");
 
11524
  myquery(rc);
 
11525
 
 
11526
  rc= mysql_query(mysql, "create view v1 as select a from t1 where a>=1");
 
11527
  myquery(rc);
 
11528
 
 
11529
  insert_stmt= mysql_stmt_init(mysql);
 
11530
  rc= mysql_stmt_prepare(insert_stmt, query, strlen(query));
 
11531
  check_execute(insert_stmt, rc);
 
11532
  query= "select * from t1";
 
11533
  select_stmt= mysql_stmt_init(mysql);
 
11534
  rc= mysql_stmt_prepare(select_stmt, query, strlen(query));
 
11535
  check_execute(select_stmt, rc);
 
11536
 
 
11537
  bzero((char*) my_bind, sizeof(my_bind));
 
11538
  my_bind[0].buffer_type = MYSQL_TYPE_LONG;
 
11539
  my_bind[0].buffer = (char *)&my_val;
 
11540
  my_bind[0].length = &my_length;
 
11541
  my_bind[0].is_null = (char*)&my_null;
 
11542
  rc= mysql_stmt_bind_param(insert_stmt, my_bind);
 
11543
  check_execute(insert_stmt, rc);
 
11544
 
 
11545
  for (i= 0; i < 3; i++)
 
11546
  {
 
11547
    int rowcount= 0;
 
11548
    my_val= i;
 
11549
 
 
11550
    rc= mysql_stmt_execute(insert_stmt);
 
11551
    check_execute(insert_stmt, rc);
 
11552
 
 
11553
    rc= mysql_stmt_execute(select_stmt);
 
11554
    check_execute(select_stmt, rc);
 
11555
    rowcount= (int)my_process_stmt_result(select_stmt);
 
11556
    DIE_UNLESS((i+1) == rowcount);
 
11557
  }
 
11558
  mysql_stmt_close(insert_stmt);
 
11559
  mysql_stmt_close(select_stmt);
 
11560
 
 
11561
  rc= mysql_query(mysql, "DROP VIEW v1");
 
11562
  myquery(rc);
 
11563
  rc= mysql_query(mysql, "DROP TABLE t1");
 
11564
  myquery(rc);
 
11565
}
 
11566
 
 
11567
 
 
11568
static void test_left_join_view()
 
11569
{
 
11570
  MYSQL_STMT *stmt;
 
11571
  int rc, i;
 
11572
  const char *query=
 
11573
    "select t1.a, v1.x from t1 left join v1 on (t1.a= v1.x);";
 
11574
 
 
11575
  myheader("test_left_join_view");
 
11576
 
 
11577
  rc = mysql_query(mysql, "DROP TABLE IF EXISTS t1,v1");
 
11578
  myquery(rc);
 
11579
 
 
11580
  rc = mysql_query(mysql, "DROP VIEW IF EXISTS v1,t1");
 
11581
  myquery(rc);
 
11582
  rc= mysql_query(mysql,"CREATE TABLE t1 (a int)");
 
11583
  myquery(rc);
 
11584
  rc= mysql_query(mysql,"insert into t1 values (1), (2), (3)");
 
11585
  myquery(rc);
 
11586
  rc= mysql_query(mysql,"create view v1 (x) as select a from t1 where a > 1");
 
11587
  myquery(rc);
 
11588
  stmt= mysql_stmt_init(mysql);
 
11589
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
11590
  check_execute(stmt, rc);
 
11591
 
 
11592
  for (i= 0; i < 3; i++)
 
11593
  {
 
11594
    rc= mysql_stmt_execute(stmt);
 
11595
    check_execute(stmt, rc);
 
11596
    rc= my_process_stmt_result(stmt);
 
11597
    DIE_UNLESS(3 == rc);
 
11598
  }
 
11599
  mysql_stmt_close(stmt);
 
11600
 
 
11601
  rc= mysql_query(mysql, "DROP VIEW v1");
 
11602
  myquery(rc);
 
11603
  rc= mysql_query(mysql, "DROP TABLE t1");
 
11604
  myquery(rc);
 
11605
}
 
11606
 
 
11607
 
 
11608
static void test_view_insert_fields()
 
11609
{
 
11610
  MYSQL_STMT    *stmt;
 
11611
  char          parm[11][1000];
 
11612
  ulong         l[11];
 
11613
  int           rc, i;
 
11614
  MYSQL_BIND    my_bind[11];
 
11615
  const char    *query= "INSERT INTO `v1` ( `K1C4` ,`K2C4` ,`K3C4` ,`K4N4` ,`F1C4` ,`F2I4` ,`F3N5` ,`F7F8` ,`F6N4` ,`F5C8` ,`F9D8` ) VALUES( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? )";
 
11616
 
 
11617
  myheader("test_view_insert_fields");
 
11618
 
 
11619
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1, v1");
 
11620
  myquery(rc);
 
11621
  rc= mysql_query(mysql, "DROP VIEW IF EXISTS t1, v1");
 
11622
  myquery(rc);
 
11623
  rc= mysql_query(mysql,
 
11624
                  "CREATE TABLE t1 (K1C4 varchar(4) NOT NULL,"
 
11625
                  "K2C4 varchar(4) NOT NULL, K3C4 varchar(4) NOT NULL,"
 
11626
                  "K4N4 varchar(4) NOT NULL default '0000',"
 
11627
                  "F1C4 varchar(4) NOT NULL, F2I4 int(11) NOT NULL,"
 
11628
                  "F3N5 varchar(5) NOT NULL default '00000',"
 
11629
                  "F4I4 int(11) NOT NULL default '0', F5C8 varchar(8) NOT NULL,"
 
11630
                  "F6N4 varchar(4) NOT NULL default '0000',"
 
11631
                  "F7F8 double NOT NULL default '0',"
 
11632
                  "F8F8 double NOT NULL default '0',"
 
11633
                  "F9D8 decimal(8,2) NOT NULL default '0.00',"
 
11634
                  "PRIMARY KEY (K1C4,K2C4,K3C4,K4N4)) "
 
11635
                  "CHARSET=latin1 COLLATE latin1_bin");
 
11636
  myquery(rc);
 
11637
  rc= mysql_query(mysql,
 
11638
                  "CREATE VIEW v1 AS select sql_no_cache "
 
11639
                  " K1C4 AS K1C4, K2C4 AS K2C4, K3C4 AS K3C4, K4N4 AS K4N4, "
 
11640
                  " F1C4 AS F1C4, F2I4 AS F2I4, F3N5 AS F3N5,"
 
11641
                  " F7F8 AS F7F8, F6N4 AS F6N4, F5C8 AS F5C8, F9D8 AS F9D8"
 
11642
                  " from t1 T0001");
 
11643
 
 
11644
  bzero((char*) my_bind, sizeof(my_bind));
 
11645
  for (i= 0; i < 11; i++)
 
11646
  {
 
11647
    l[i]= 20;
 
11648
    my_bind[i].buffer_type= MYSQL_TYPE_STRING;
 
11649
    my_bind[i].is_null= 0;
 
11650
    my_bind[i].buffer= (char *)&parm[i];
 
11651
 
 
11652
    strmov(parm[i], "1");
 
11653
    my_bind[i].buffer_length= 2;
 
11654
    my_bind[i].length= &l[i];
 
11655
  }
 
11656
  stmt= mysql_stmt_init(mysql);
 
11657
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
11658
  check_execute(stmt, rc);
 
11659
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
11660
  check_execute(stmt, rc);
 
11661
 
 
11662
  rc= mysql_stmt_execute(stmt);
 
11663
  check_execute(stmt, rc);
 
11664
  mysql_stmt_close(stmt);
 
11665
 
 
11666
  query= "select * from t1";
 
11667
  stmt= mysql_stmt_init(mysql);
 
11668
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
11669
  check_execute(stmt, rc);
 
11670
  rc= mysql_stmt_execute(stmt);
 
11671
  check_execute(stmt, rc);
 
11672
  rc= my_process_stmt_result(stmt);
 
11673
  DIE_UNLESS(1 == rc);
 
11674
 
 
11675
  mysql_stmt_close(stmt);
 
11676
  rc= mysql_query(mysql, "DROP VIEW v1");
 
11677
  myquery(rc);
 
11678
  rc= mysql_query(mysql, "DROP TABLE t1");
 
11679
  myquery(rc);
 
11680
 
 
11681
}
 
11682
 
 
11683
static void test_bug5126()
 
11684
{
 
11685
  MYSQL_STMT *stmt;
 
11686
  MYSQL_BIND my_bind[2];
 
11687
  int32 c1, c2;
 
11688
  const char *stmt_text;
 
11689
  int rc;
 
11690
 
 
11691
  myheader("test_bug5126");
 
11692
 
 
11693
  stmt_text= "DROP TABLE IF EXISTS t1";
 
11694
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
11695
  myquery(rc);
 
11696
 
 
11697
  stmt_text= "CREATE TABLE t1 (a mediumint, b int)";
 
11698
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
11699
  myquery(rc);
 
11700
 
 
11701
  stmt_text= "INSERT INTO t1 VALUES (8386608, 1)";
 
11702
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
11703
  myquery(rc);
 
11704
 
 
11705
  stmt= mysql_stmt_init(mysql);
 
11706
  stmt_text= "SELECT a, b FROM t1";
 
11707
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
11708
  check_execute(stmt, rc);
 
11709
  rc= mysql_stmt_execute(stmt);
 
11710
  check_execute(stmt, rc);
 
11711
 
 
11712
  /* Bind output buffers */
 
11713
  bzero((char*) my_bind, sizeof(my_bind));
 
11714
 
 
11715
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
11716
  my_bind[0].buffer= &c1;
 
11717
  my_bind[1].buffer_type= MYSQL_TYPE_LONG;
 
11718
  my_bind[1].buffer= &c2;
 
11719
 
 
11720
  mysql_stmt_bind_result(stmt, my_bind);
 
11721
 
 
11722
  rc= mysql_stmt_fetch(stmt);
 
11723
  DIE_UNLESS(rc == 0);
 
11724
  DIE_UNLESS(c1 == 8386608 && c2 == 1);
 
11725
  if (!opt_silent)
 
11726
    printf("%ld, %ld\n", (long) c1, (long) c2);
 
11727
  mysql_stmt_close(stmt);
 
11728
}
 
11729
 
 
11730
 
 
11731
static void test_bug4231()
 
11732
{
 
11733
  MYSQL_STMT *stmt;
 
11734
  MYSQL_BIND my_bind[2];
 
11735
  MYSQL_TIME tm[2];
 
11736
  const char *stmt_text;
 
11737
  int rc;
 
11738
 
 
11739
  myheader("test_bug4231");
 
11740
 
 
11741
  stmt_text= "DROP TABLE IF EXISTS t1";
 
11742
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
11743
  myquery(rc);
 
11744
 
 
11745
  stmt_text= "CREATE TABLE t1 (a int)";
 
11746
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
11747
  myquery(rc);
 
11748
 
 
11749
  stmt_text= "INSERT INTO t1 VALUES (1)";
 
11750
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
11751
  myquery(rc);
 
11752
 
 
11753
  stmt= mysql_stmt_init(mysql);
 
11754
  stmt_text= "SELECT a FROM t1 WHERE ? = ?";
 
11755
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
11756
  check_execute(stmt, rc);
 
11757
 
 
11758
  /* Bind input buffers */
 
11759
  bzero((char*) my_bind, sizeof(my_bind));
 
11760
  bzero((char*) tm, sizeof(tm));
 
11761
 
 
11762
  my_bind[0].buffer_type= MYSQL_TYPE_DATE;
 
11763
  my_bind[0].buffer= &tm[0];
 
11764
  my_bind[1].buffer_type= MYSQL_TYPE_DATE;
 
11765
  my_bind[1].buffer= &tm[1];
 
11766
 
 
11767
  mysql_stmt_bind_param(stmt, my_bind);
 
11768
  check_execute(stmt, rc);
 
11769
 
 
11770
  /*
 
11771
    First set server-side params to some non-zero non-equal values:
 
11772
    then we will check that they are not used when client sends
 
11773
    new (zero) times.
 
11774
  */
 
11775
  tm[0].time_type = MYSQL_TIMESTAMP_DATE;
 
11776
  tm[0].year = 2000;
 
11777
  tm[0].month = 1;
 
11778
  tm[0].day = 1;
 
11779
  tm[1]= tm[0];
 
11780
  --tm[1].year;                                 /* tm[0] != tm[1] */
 
11781
 
 
11782
  rc= mysql_stmt_execute(stmt);
 
11783
  check_execute(stmt, rc);
 
11784
 
 
11785
  rc= mysql_stmt_fetch(stmt);
 
11786
 
 
11787
  /* binds are unequal, no rows should be returned */
 
11788
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
11789
 
 
11790
  /* Set one of the dates to zero */
 
11791
  tm[0].year= tm[0].month= tm[0].day= 0;
 
11792
  tm[1]= tm[0];
 
11793
  mysql_stmt_execute(stmt);
 
11794
  rc= mysql_stmt_fetch(stmt);
 
11795
  DIE_UNLESS(rc == 0);
 
11796
 
 
11797
  mysql_stmt_close(stmt);
 
11798
  stmt_text= "DROP TABLE t1";
 
11799
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
11800
  myquery(rc);
 
11801
}
 
11802
 
 
11803
 
 
11804
static void test_bug5399()
 
11805
{
 
11806
  /*
 
11807
    Ascii 97 is 'a', which gets mapped to Ascii 65 'A' unless internal
 
11808
    statement id hash in the server uses binary collation.
 
11809
  */
 
11810
#define NUM_OF_USED_STMT 97 
 
11811
  MYSQL_STMT *stmt_list[NUM_OF_USED_STMT];
 
11812
  MYSQL_STMT **stmt;
 
11813
  MYSQL_BIND my_bind[1];
 
11814
  char buff[600];
 
11815
  int rc;
 
11816
  int32 no;
 
11817
 
 
11818
  myheader("test_bug5399");
 
11819
 
 
11820
  bzero((char*) my_bind, sizeof(my_bind));
 
11821
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
11822
  my_bind[0].buffer= &no;
 
11823
 
 
11824
  for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
 
11825
  {
 
11826
    sprintf(buff, "select %d", (int) (stmt - stmt_list));
 
11827
    *stmt= mysql_stmt_init(mysql);
 
11828
    rc= mysql_stmt_prepare(*stmt, buff, strlen(buff));
 
11829
    check_execute(*stmt, rc);
 
11830
    mysql_stmt_bind_result(*stmt, my_bind);
 
11831
  }
 
11832
  if (!opt_silent)
 
11833
    printf("%d statements prepared.\n", NUM_OF_USED_STMT);
 
11834
 
 
11835
  for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
 
11836
  {
 
11837
    rc= mysql_stmt_execute(*stmt);
 
11838
    check_execute(*stmt, rc);
 
11839
    rc= mysql_stmt_store_result(*stmt);
 
11840
    check_execute(*stmt, rc);
 
11841
    rc= mysql_stmt_fetch(*stmt);
 
11842
    DIE_UNLESS(rc == 0);
 
11843
    DIE_UNLESS((int32) (stmt - stmt_list) == no);
 
11844
  }
 
11845
 
 
11846
  for (stmt= stmt_list; stmt != stmt_list + NUM_OF_USED_STMT; ++stmt)
 
11847
    mysql_stmt_close(*stmt);
 
11848
#undef NUM_OF_USED_STMT
 
11849
}
 
11850
 
 
11851
 
 
11852
static void test_bug5194()
 
11853
{
 
11854
  MYSQL_STMT *stmt;
 
11855
  MYSQL_BIND *my_bind;
 
11856
  char *query;
 
11857
  char *param_str;
 
11858
  int param_str_length;
 
11859
  const char *stmt_text;
 
11860
  int rc;
 
11861
  float float_array[250] =
 
11862
  {
 
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.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11867
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11868
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11869
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11870
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11871
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11872
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11873
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11874
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
11875
    0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
 
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
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
 
11879
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
 
11880
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
 
11881
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
 
11882
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
 
11883
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
 
11884
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
 
11885
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
 
11886
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,
 
11887
    0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25,  0.25
 
11888
  };
 
11889
  float *fa_ptr= float_array;
 
11890
  /* Number of columns per row */
 
11891
  const int COLUMN_COUNT= sizeof(float_array)/sizeof(*float_array);
 
11892
  /* Number of rows per bulk insert to start with */
 
11893
  const int MIN_ROWS_PER_INSERT= 262;
 
11894
  /* Max number of rows per bulk insert to end with */
 
11895
  const int MAX_ROWS_PER_INSERT= 300;
 
11896
  const int MAX_PARAM_COUNT= COLUMN_COUNT*MAX_ROWS_PER_INSERT;
 
11897
  const char *query_template= "insert into t1 values %s";
 
11898
  const int CHARS_PER_PARAM= 5; /* space needed to place ", ?" in the query */
 
11899
  const int uint16_max= 65535;
 
11900
  int nrows, i;
 
11901
 
 
11902
  myheader("test_bug5194");
 
11903
 
 
11904
  stmt_text= "drop table if exists t1";
 
11905
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
11906
 
 
11907
  stmt_text= "create table if not exists t1"
 
11908
   "(c1 float, c2 float, c3 float, c4 float, c5 float, c6 float, "
 
11909
   "c7 float, c8 float, c9 float, c10 float, c11 float, c12 float, "
 
11910
   "c13 float, c14 float, c15 float, c16 float, c17 float, c18 float, "
 
11911
   "c19 float, c20 float, c21 float, c22 float, c23 float, c24 float, "
 
11912
   "c25 float, c26 float, c27 float, c28 float, c29 float, c30 float, "
 
11913
   "c31 float, c32 float, c33 float, c34 float, c35 float, c36 float, "
 
11914
   "c37 float, c38 float, c39 float, c40 float, c41 float, c42 float, "
 
11915
   "c43 float, c44 float, c45 float, c46 float, c47 float, c48 float, "
 
11916
   "c49 float, c50 float, c51 float, c52 float, c53 float, c54 float, "
 
11917
   "c55 float, c56 float, c57 float, c58 float, c59 float, c60 float, "
 
11918
   "c61 float, c62 float, c63 float, c64 float, c65 float, c66 float, "
 
11919
   "c67 float, c68 float, c69 float, c70 float, c71 float, c72 float, "
 
11920
   "c73 float, c74 float, c75 float, c76 float, c77 float, c78 float, "
 
11921
   "c79 float, c80 float, c81 float, c82 float, c83 float, c84 float, "
 
11922
   "c85 float, c86 float, c87 float, c88 float, c89 float, c90 float, "
 
11923
   "c91 float, c92 float, c93 float, c94 float, c95 float, c96 float, "
 
11924
   "c97 float, c98 float, c99 float, c100 float, c101 float, c102 float, "
 
11925
   "c103 float, c104 float, c105 float, c106 float, c107 float, c108 float, "
 
11926
   "c109 float, c110 float, c111 float, c112 float, c113 float, c114 float, "
 
11927
   "c115 float, c116 float, c117 float, c118 float, c119 float, c120 float, "
 
11928
   "c121 float, c122 float, c123 float, c124 float, c125 float, c126 float, "
 
11929
   "c127 float, c128 float, c129 float, c130 float, c131 float, c132 float, "
 
11930
   "c133 float, c134 float, c135 float, c136 float, c137 float, c138 float, "
 
11931
   "c139 float, c140 float, c141 float, c142 float, c143 float, c144 float, "
 
11932
   "c145 float, c146 float, c147 float, c148 float, c149 float, c150 float, "
 
11933
   "c151 float, c152 float, c153 float, c154 float, c155 float, c156 float, "
 
11934
   "c157 float, c158 float, c159 float, c160 float, c161 float, c162 float, "
 
11935
   "c163 float, c164 float, c165 float, c166 float, c167 float, c168 float, "
 
11936
   "c169 float, c170 float, c171 float, c172 float, c173 float, c174 float, "
 
11937
   "c175 float, c176 float, c177 float, c178 float, c179 float, c180 float, "
 
11938
   "c181 float, c182 float, c183 float, c184 float, c185 float, c186 float, "
 
11939
   "c187 float, c188 float, c189 float, c190 float, c191 float, c192 float, "
 
11940
   "c193 float, c194 float, c195 float, c196 float, c197 float, c198 float, "
 
11941
   "c199 float, c200 float, c201 float, c202 float, c203 float, c204 float, "
 
11942
   "c205 float, c206 float, c207 float, c208 float, c209 float, c210 float, "
 
11943
   "c211 float, c212 float, c213 float, c214 float, c215 float, c216 float, "
 
11944
   "c217 float, c218 float, c219 float, c220 float, c221 float, c222 float, "
 
11945
   "c223 float, c224 float, c225 float, c226 float, c227 float, c228 float, "
 
11946
   "c229 float, c230 float, c231 float, c232 float, c233 float, c234 float, "
 
11947
   "c235 float, c236 float, c237 float, c238 float, c239 float, c240 float, "
 
11948
   "c241 float, c242 float, c243 float, c244 float, c245 float, c246 float, "
 
11949
   "c247 float, c248 float, c249 float, c250 float)";
 
11950
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
11951
  myquery(rc);
 
11952
 
 
11953
  my_bind= (MYSQL_BIND*) malloc(MAX_PARAM_COUNT * sizeof(MYSQL_BIND));
 
11954
  query= (char*) malloc(strlen(query_template) +
 
11955
                        MAX_PARAM_COUNT * CHARS_PER_PARAM + 1);
 
11956
  param_str= (char*) malloc(COLUMN_COUNT * CHARS_PER_PARAM);
 
11957
 
 
11958
  if (my_bind == 0 || query == 0 || param_str == 0)
 
11959
  {
 
11960
    fprintf(stderr, "Can't allocate enough memory for query structs\n");
 
11961
    if (my_bind)
 
11962
      free(my_bind);
 
11963
    if (query)
 
11964
      free(query);
 
11965
    if (param_str)
 
11966
      free(param_str);
 
11967
    return;
 
11968
  }
 
11969
 
 
11970
  stmt= mysql_stmt_init(mysql);
 
11971
 
 
11972
  /* setup a template for one row of parameters */
 
11973
  sprintf(param_str, "(");
 
11974
  for (i= 1; i < COLUMN_COUNT; ++i)
 
11975
    strcat(param_str, "?, ");
 
11976
  strcat(param_str, "?)");
 
11977
  param_str_length= strlen(param_str);
 
11978
 
 
11979
  /* setup bind array */
 
11980
  bzero((char*) my_bind, MAX_PARAM_COUNT * sizeof(MYSQL_BIND));
 
11981
  for (i= 0; i < MAX_PARAM_COUNT; ++i)
 
11982
  {
 
11983
    my_bind[i].buffer_type= MYSQL_TYPE_FLOAT;
 
11984
    my_bind[i].buffer= fa_ptr;
 
11985
    if (++fa_ptr == float_array + COLUMN_COUNT)
 
11986
      fa_ptr= float_array;
 
11987
  }
 
11988
 
 
11989
  /*
 
11990
    Test each number of rows per bulk insert, so that we can see where
 
11991
    MySQL fails.
 
11992
  */
 
11993
  for (nrows= MIN_ROWS_PER_INSERT; nrows <= MAX_ROWS_PER_INSERT; ++nrows)
 
11994
  {
 
11995
    char *query_ptr;
 
11996
    /* Create statement text for current number of rows */
 
11997
    sprintf(query, query_template, param_str);
 
11998
    query_ptr= query + strlen(query);
 
11999
    for (i= 1; i < nrows; ++i)
 
12000
    {
 
12001
      memcpy(query_ptr, ", ", 2);
 
12002
      query_ptr+= 2;
 
12003
      memcpy(query_ptr, param_str, param_str_length);
 
12004
      query_ptr+= param_str_length;
 
12005
    }
 
12006
    *query_ptr= '\0';
 
12007
 
 
12008
    rc= mysql_stmt_prepare(stmt, query, query_ptr - query);
 
12009
    if (rc && nrows * COLUMN_COUNT > uint16_max)
 
12010
    {
 
12011
      if (!opt_silent)
 
12012
        printf("Failed to prepare a statement with %d placeholders "
 
12013
               "(as expected).\n", nrows * COLUMN_COUNT);
 
12014
      break;
 
12015
    }
 
12016
    else
 
12017
      check_execute(stmt, rc);
 
12018
 
 
12019
    if (!opt_silent)
 
12020
      printf("Insert: query length= %d, row count= %d, param count= %lu\n",
 
12021
             (int) strlen(query), nrows, mysql_stmt_param_count(stmt));
 
12022
 
 
12023
    /* bind the parameter array and execute the query */
 
12024
    rc= mysql_stmt_bind_param(stmt, my_bind);
 
12025
    check_execute(stmt, rc);
 
12026
 
 
12027
    rc= mysql_stmt_execute(stmt);
 
12028
    check_execute(stmt, rc);
 
12029
    mysql_stmt_reset(stmt);
 
12030
  }
 
12031
 
 
12032
  mysql_stmt_close(stmt);
 
12033
  free(my_bind);
 
12034
  free(query);
 
12035
  free(param_str);
 
12036
  stmt_text= "drop table t1";
 
12037
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12038
  myquery(rc);
 
12039
}
 
12040
 
 
12041
 
 
12042
static void test_bug5315()
 
12043
{
 
12044
  MYSQL_STMT *stmt;
 
12045
  const char *stmt_text;
 
12046
  int rc;
 
12047
 
 
12048
  myheader("test_bug5315");
 
12049
 
 
12050
  stmt_text= "SELECT 1";
 
12051
  stmt= mysql_stmt_init(mysql);
 
12052
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12053
  DIE_UNLESS(rc == 0);
 
12054
  if (!opt_silent)
 
12055
    printf("Excuting mysql_change_user\n");
 
12056
  mysql_change_user(mysql, opt_user, opt_password, current_db);
 
12057
  if (!opt_silent)
 
12058
    printf("Excuting mysql_stmt_execute\n");
 
12059
  rc= mysql_stmt_execute(stmt);
 
12060
  DIE_UNLESS(rc != 0);
 
12061
  if (rc)
 
12062
  {
 
12063
    if (!opt_silent)
 
12064
      printf("Got error (as expected): '%s'\n", mysql_stmt_error(stmt));
 
12065
  }
 
12066
  /* check that connection is OK */
 
12067
  if (!opt_silent)
 
12068
    printf("Excuting mysql_stmt_close\n");
 
12069
  mysql_stmt_close(stmt);
 
12070
  if (!opt_silent)
 
12071
    printf("Excuting mysql_stmt_init\n");
 
12072
  stmt= mysql_stmt_init(mysql);
 
12073
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12074
  DIE_UNLESS(rc == 0);
 
12075
  rc= mysql_stmt_execute(stmt);
 
12076
  DIE_UNLESS(rc == 0);
 
12077
  mysql_stmt_close(stmt);
 
12078
}
 
12079
 
 
12080
 
 
12081
static void test_bug6049()
 
12082
{
 
12083
  MYSQL_STMT *stmt;
 
12084
  MYSQL_BIND my_bind[1];
 
12085
  MYSQL_RES *res;
 
12086
  MYSQL_ROW row;
 
12087
  const char *stmt_text;
 
12088
  char buffer[30];
 
12089
  ulong length;
 
12090
  int rc;
 
12091
 
 
12092
  myheader("test_bug6049");
 
12093
 
 
12094
  stmt_text= "SELECT MAKETIME(-25, 12, 12)";
 
12095
 
 
12096
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12097
  myquery(rc);
 
12098
  res= mysql_store_result(mysql);
 
12099
  row= mysql_fetch_row(res);
 
12100
 
 
12101
  stmt= mysql_stmt_init(mysql);
 
12102
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12103
  check_execute(stmt, rc);
 
12104
  rc= mysql_stmt_execute(stmt);
 
12105
  check_execute(stmt, rc);
 
12106
 
 
12107
  bzero((char*) my_bind, sizeof(my_bind));
 
12108
  my_bind[0].buffer_type    = MYSQL_TYPE_STRING;
 
12109
  my_bind[0].buffer         = &buffer;
 
12110
  my_bind[0].buffer_length  = sizeof(buffer);
 
12111
  my_bind[0].length         = &length;
 
12112
 
 
12113
  mysql_stmt_bind_result(stmt, my_bind);
 
12114
  rc= mysql_stmt_fetch(stmt);
 
12115
  DIE_UNLESS(rc == 0);
 
12116
 
 
12117
  if (!opt_silent)
 
12118
  {
 
12119
    printf("Result from query: %s\n", row[0]);
 
12120
    printf("Result from prepared statement: %s\n", (char*) buffer);
 
12121
  }
 
12122
 
 
12123
  DIE_UNLESS(strcmp(row[0], (char*) buffer) == 0);
 
12124
 
 
12125
  mysql_free_result(res);
 
12126
  mysql_stmt_close(stmt);
 
12127
}
 
12128
 
 
12129
 
 
12130
static void test_bug6058()
 
12131
{
 
12132
  MYSQL_STMT *stmt;
 
12133
  MYSQL_BIND my_bind[1];
 
12134
  MYSQL_RES *res;
 
12135
  MYSQL_ROW row;
 
12136
  const char *stmt_text;
 
12137
  char buffer[30];
 
12138
  ulong length;
 
12139
  int rc;
 
12140
 
 
12141
  myheader("test_bug6058");
 
12142
 
 
12143
  rc= mysql_query(mysql, "SET SQL_MODE=''");
 
12144
  myquery(rc);
 
12145
 
 
12146
  stmt_text= "SELECT CAST('0000-00-00' AS DATE)";
 
12147
 
 
12148
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12149
  myquery(rc);
 
12150
  res= mysql_store_result(mysql);
 
12151
  row= mysql_fetch_row(res);
 
12152
 
 
12153
  stmt= mysql_stmt_init(mysql);
 
12154
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12155
  check_execute(stmt, rc);
 
12156
  rc= mysql_stmt_execute(stmt);
 
12157
  check_execute(stmt, rc);
 
12158
 
 
12159
  bzero((char*) my_bind, sizeof(my_bind));
 
12160
  my_bind[0].buffer_type    = MYSQL_TYPE_STRING;
 
12161
  my_bind[0].buffer         = &buffer;
 
12162
  my_bind[0].buffer_length  = sizeof(buffer);
 
12163
  my_bind[0].length         = &length;
 
12164
 
 
12165
  mysql_stmt_bind_result(stmt, my_bind);
 
12166
  rc= mysql_stmt_fetch(stmt);
 
12167
  DIE_UNLESS(rc == 0);
 
12168
 
 
12169
  if (!opt_silent)
 
12170
  {
 
12171
    printf("Result from query: %s\n", row[0]);
 
12172
    printf("Result from prepared statement: %s\n", buffer);
 
12173
  }
 
12174
 
 
12175
  DIE_UNLESS(strcmp(row[0], buffer) == 0);
 
12176
 
 
12177
  mysql_free_result(res);
 
12178
  mysql_stmt_close(stmt);
 
12179
}
 
12180
 
 
12181
 
 
12182
static void test_bug6059()
 
12183
{
 
12184
  MYSQL_STMT *stmt;
 
12185
  const char *stmt_text;
 
12186
 
 
12187
  myheader("test_bug6059");
 
12188
 
 
12189
  stmt_text= "SELECT 'foo' INTO OUTFILE 'x.3'";
 
12190
 
 
12191
  stmt= mysql_stmt_init(mysql);
 
12192
  (void) mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12193
  DIE_UNLESS(mysql_stmt_field_count(stmt) == 0);
 
12194
  mysql_stmt_close(stmt);
 
12195
}
 
12196
 
 
12197
 
 
12198
static void test_bug6046()
 
12199
{
 
12200
  MYSQL_STMT *stmt;
 
12201
  const char *stmt_text;
 
12202
  int rc;
 
12203
  short b= 1;
 
12204
  MYSQL_BIND my_bind[1];
 
12205
 
 
12206
  myheader("test_bug6046");
 
12207
 
 
12208
  stmt_text= "DROP TABLE IF EXISTS t1";
 
12209
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12210
  myquery(rc);
 
12211
  stmt_text= "CREATE TABLE t1 (a int, b int)";
 
12212
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12213
  myquery(rc);
 
12214
  stmt_text= "INSERT INTO t1 VALUES (1,1),(2,2),(3,1),(4,2)";
 
12215
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12216
  myquery(rc);
 
12217
 
 
12218
  stmt= mysql_stmt_init(mysql);
 
12219
 
 
12220
  stmt_text= "SELECT t1.a FROM t1 NATURAL JOIN t1 as X1 "
 
12221
             "WHERE t1.b > ? ORDER BY t1.a";
 
12222
 
 
12223
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12224
  check_execute(stmt, rc);
 
12225
 
 
12226
  b= 1;
 
12227
  bzero((char*) my_bind, sizeof(my_bind));
 
12228
  my_bind[0].buffer= &b;
 
12229
  my_bind[0].buffer_type= MYSQL_TYPE_SHORT;
 
12230
 
 
12231
  mysql_stmt_bind_param(stmt, my_bind);
 
12232
 
 
12233
  rc= mysql_stmt_execute(stmt);
 
12234
  check_execute(stmt, rc);
 
12235
  mysql_stmt_store_result(stmt);
 
12236
 
 
12237
  rc= mysql_stmt_execute(stmt);
 
12238
  check_execute(stmt, rc);
 
12239
 
 
12240
  mysql_stmt_close(stmt);
 
12241
}
 
12242
 
 
12243
 
 
12244
 
 
12245
static void test_basic_cursors()
 
12246
{
 
12247
  const char *basic_tables[]=
 
12248
  {
 
12249
    "DROP TABLE IF EXISTS t1, t2",
 
12250
 
 
12251
    "CREATE TABLE t1 "
 
12252
    "(id INTEGER NOT NULL PRIMARY KEY, "
 
12253
    " name VARCHAR(20) NOT NULL)",
 
12254
 
 
12255
    "INSERT INTO t1 (id, name) VALUES "
 
12256
    "  (2, 'Ja'), (3, 'Ede'), "
 
12257
    "  (4, 'Haag'), (5, 'Kabul'), "
 
12258
    "  (6, 'Almere'), (7, 'Utrecht'), "
 
12259
    "  (8, 'Qandahar'), (9, 'Amsterdam'), "
 
12260
    "  (10, 'Amersfoort'), (11, 'Constantine')",
 
12261
 
 
12262
    "CREATE TABLE t2 "
 
12263
    "(id INTEGER NOT NULL PRIMARY KEY, "
 
12264
    " name VARCHAR(20) NOT NULL)",
 
12265
 
 
12266
    "INSERT INTO t2 (id, name) VALUES "
 
12267
    "  (4, 'Guam'), (5, 'Aruba'), "
 
12268
    "  (6, 'Angola'), (7, 'Albania'), "
 
12269
    "  (8, 'Anguilla'), (9, 'Argentina'), "
 
12270
    "  (10, 'Azerbaijan'), (11, 'Afghanistan'), "
 
12271
    "  (12, 'Burkina Faso'), (13, 'Faroe Islands')"
 
12272
  };
 
12273
  const char *queries[]=
 
12274
  {
 
12275
    "SELECT * FROM t1",
 
12276
    "SELECT * FROM t2"
 
12277
  };
 
12278
 
 
12279
  DBUG_ENTER("test_basic_cursors");
 
12280
  myheader("test_basic_cursors");
 
12281
 
 
12282
  fill_tables(basic_tables, sizeof(basic_tables)/sizeof(*basic_tables));
 
12283
 
 
12284
  fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_ROW_BY_ROW_FETCH);
 
12285
  fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_STORE_RESULT);
 
12286
  DBUG_VOID_RETURN;
 
12287
}
 
12288
 
 
12289
 
 
12290
static void test_cursors_with_union()
 
12291
{
 
12292
  const char *queries[]=
 
12293
  {
 
12294
    "SELECT t1.name FROM t1 UNION SELECT t2.name FROM t2",
 
12295
    "SELECT t1.id FROM t1 WHERE t1.id < 5"
 
12296
  };
 
12297
  myheader("test_cursors_with_union");
 
12298
  fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_ROW_BY_ROW_FETCH);
 
12299
  fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_STORE_RESULT);
 
12300
}
 
12301
 
 
12302
 
 
12303
static void test_cursors_with_procedure()
 
12304
{
 
12305
  const char *queries[]=
 
12306
  {
 
12307
    "SELECT * FROM t1 procedure analyse()"
 
12308
  };
 
12309
  myheader("test_cursors_with_procedure");
 
12310
  fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_ROW_BY_ROW_FETCH);
 
12311
  fetch_n(queries, sizeof(queries)/sizeof(*queries), USE_STORE_RESULT);
 
12312
}
 
12313
 
 
12314
 
 
12315
/*
 
12316
  Altough mysql_create_db(), mysql_rm_db() are deprecated since 4.0 they
 
12317
  should not crash server and should not hang in case of errors.
 
12318
 
 
12319
  Since those functions can't be seen in modern API (unless client library
 
12320
  was compiled with USE_OLD_FUNCTIONS define) we use simple_command() macro.
 
12321
*/
 
12322
static void test_bug6081()
 
12323
{
 
12324
  int rc;
 
12325
  myheader("test_bug6081");
 
12326
 
 
12327
  rc= simple_command(mysql, COM_DROP_DB, (uchar*) current_db,
 
12328
                     (ulong)strlen(current_db), 0);
 
12329
  if (rc == 0 && mysql_errno(mysql) != ER_UNKNOWN_COM_ERROR)
 
12330
  {
 
12331
    myerror(NULL);                                   /* purecov: inspected */
 
12332
    die(__FILE__, __LINE__, "COM_DROP_DB failed");   /* purecov: inspected */
 
12333
  }
 
12334
  rc= simple_command(mysql, COM_DROP_DB, (uchar*) current_db,
 
12335
                     (ulong)strlen(current_db), 0);
 
12336
  myquery_r(rc);
 
12337
  rc= simple_command(mysql, COM_CREATE_DB, (uchar*) current_db,
 
12338
                     (ulong)strlen(current_db), 0);
 
12339
  if (rc == 0 && mysql_errno(mysql) != ER_UNKNOWN_COM_ERROR)
 
12340
  {
 
12341
    myerror(NULL);                                   /* purecov: inspected */
 
12342
    die(__FILE__, __LINE__, "COM_CREATE_DB failed"); /* purecov: inspected */
 
12343
  }
 
12344
  rc= simple_command(mysql, COM_CREATE_DB, (uchar*) current_db,
 
12345
                     (ulong)strlen(current_db), 0);
 
12346
  myquery_r(rc);
 
12347
  rc= mysql_select_db(mysql, current_db);
 
12348
  myquery(rc);
 
12349
}
 
12350
 
 
12351
 
 
12352
static void test_bug6096()
 
12353
{
 
12354
  MYSQL_STMT *stmt;
 
12355
  MYSQL_RES *query_result, *stmt_metadata;
 
12356
  const char *stmt_text;
 
12357
  MYSQL_BIND my_bind[12];
 
12358
  MYSQL_FIELD *query_field_list, *stmt_field_list;
 
12359
  ulong query_field_count, stmt_field_count;
 
12360
  int rc;
 
12361
  my_bool update_max_length= TRUE;
 
12362
  uint i;
 
12363
 
 
12364
  myheader("test_bug6096");
 
12365
 
 
12366
  stmt_text= "drop table if exists t1";
 
12367
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12368
  myquery(rc);
 
12369
 
 
12370
  mysql_query(mysql, "set sql_mode=''");
 
12371
  stmt_text= "create table t1 (c_tinyint tinyint, c_smallint smallint, "
 
12372
                             " c_mediumint mediumint, c_int int, "
 
12373
                             " c_bigint bigint, c_float float, "
 
12374
                             " c_double double, c_varchar varchar(20), "
 
12375
                             " c_char char(20), c_time time, c_date date, "
 
12376
                             " c_datetime datetime)";
 
12377
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12378
  myquery(rc);
 
12379
  stmt_text= "insert into t1  values (-100, -20000, 30000000, 4, 8, 1.0, "
 
12380
                                     "2.0, 'abc', 'def', now(), now(), now())";
 
12381
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12382
  myquery(rc);
 
12383
 
 
12384
  stmt_text= "select * from t1";
 
12385
 
 
12386
  /* Run select in prepared and non-prepared mode and compare metadata */
 
12387
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12388
  myquery(rc);
 
12389
  query_result= mysql_store_result(mysql);
 
12390
  query_field_list= mysql_fetch_fields(query_result);
 
12391
  query_field_count= mysql_num_fields(query_result);
 
12392
 
 
12393
  stmt= mysql_stmt_init(mysql);
 
12394
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12395
  check_execute(stmt, rc);
 
12396
  rc= mysql_stmt_execute(stmt);
 
12397
  check_execute(stmt, rc);
 
12398
  mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH,
 
12399
                      (void*) &update_max_length);
 
12400
  mysql_stmt_store_result(stmt);
 
12401
  stmt_metadata= mysql_stmt_result_metadata(stmt);
 
12402
  stmt_field_list= mysql_fetch_fields(stmt_metadata);
 
12403
  stmt_field_count= mysql_num_fields(stmt_metadata);
 
12404
  DIE_UNLESS(stmt_field_count == query_field_count);
 
12405
 
 
12406
  /* Print out and check the metadata */
 
12407
 
 
12408
  if (!opt_silent)
 
12409
  {
 
12410
    printf(" ------------------------------------------------------------\n");
 
12411
    printf("             |                     Metadata \n");
 
12412
    printf(" ------------------------------------------------------------\n");
 
12413
    printf("             |         Query          |   Prepared statement \n");
 
12414
    printf(" ------------------------------------------------------------\n");
 
12415
    printf(" field name  |  length   | max_length |  length   |  max_length\n");
 
12416
    printf(" ------------------------------------------------------------\n");
 
12417
 
 
12418
    for (i= 0; i < query_field_count; ++i)
 
12419
    {
 
12420
      MYSQL_FIELD *f1= &query_field_list[i], *f2= &stmt_field_list[i];
 
12421
      printf(" %-11s | %9lu | %10lu | %9lu | %10lu \n",
 
12422
             f1->name, f1->length, f1->max_length, f2->length, f2->max_length);
 
12423
      DIE_UNLESS(f1->length == f2->length);
 
12424
    }
 
12425
    printf(" ---------------------------------------------------------------\n");
 
12426
  }
 
12427
 
 
12428
  /* Bind and fetch the data */
 
12429
 
 
12430
  bzero((char*) my_bind, sizeof(my_bind));
 
12431
  for (i= 0; i < stmt_field_count; ++i)
 
12432
  {
 
12433
    my_bind[i].buffer_type= MYSQL_TYPE_STRING;
 
12434
    my_bind[i].buffer_length= stmt_field_list[i].max_length + 1;
 
12435
    my_bind[i].buffer= malloc(my_bind[i].buffer_length);
 
12436
  }
 
12437
  mysql_stmt_bind_result(stmt, my_bind);
 
12438
  rc= mysql_stmt_fetch(stmt);
 
12439
  check_execute(stmt, rc);
 
12440
  rc= mysql_stmt_fetch(stmt);
 
12441
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
12442
 
 
12443
  /* Clean up */
 
12444
 
 
12445
  for (i= 0; i < stmt_field_count; ++i)
 
12446
    free(my_bind[i].buffer);
 
12447
  mysql_stmt_close(stmt);
 
12448
  mysql_free_result(query_result);
 
12449
  mysql_free_result(stmt_metadata);
 
12450
  stmt_text= "drop table t1";
 
12451
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12452
  myquery(rc);
 
12453
}
 
12454
 
 
12455
 
 
12456
/*
 
12457
  Test of basic checks that are performed in server for components
 
12458
  of MYSQL_TIME parameters.
 
12459
*/
 
12460
 
 
12461
static void test_datetime_ranges()
 
12462
{
 
12463
  const char *stmt_text;
 
12464
  int rc, i;
 
12465
  MYSQL_STMT *stmt;
 
12466
  MYSQL_BIND my_bind[6];
 
12467
  MYSQL_TIME tm[6];
 
12468
 
 
12469
  myheader("test_datetime_ranges");
 
12470
 
 
12471
  stmt_text= "drop table if exists t1";
 
12472
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12473
  myquery(rc);
 
12474
 
 
12475
  stmt_text= "create table t1 (year datetime, month datetime, day datetime, "
 
12476
                              "hour datetime, min datetime, sec datetime)";
 
12477
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12478
  myquery(rc);
 
12479
 
 
12480
  stmt= mysql_simple_prepare(mysql,
 
12481
                             "INSERT INTO t1 VALUES (?, ?, ?, ?, ?, ?)");
 
12482
  check_stmt(stmt);
 
12483
  verify_param_count(stmt, 6);
 
12484
 
 
12485
  bzero((char*) my_bind, sizeof(my_bind));
 
12486
  for (i= 0; i < 6; i++)
 
12487
  {
 
12488
    my_bind[i].buffer_type= MYSQL_TYPE_DATETIME;
 
12489
    my_bind[i].buffer= &tm[i];
 
12490
  }
 
12491
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
12492
  check_execute(stmt, rc);
 
12493
 
 
12494
  tm[0].year= 2004; tm[0].month= 11; tm[0].day= 10;
 
12495
  tm[0].hour= 12; tm[0].minute= 30; tm[0].second= 30;
 
12496
  tm[0].second_part= 0; tm[0].neg= 0;
 
12497
 
 
12498
  tm[5]= tm[4]= tm[3]= tm[2]= tm[1]= tm[0];
 
12499
  tm[0].year= 10000;  tm[1].month= 13; tm[2].day= 32;
 
12500
  tm[3].hour= 24; tm[4].minute= 60; tm[5].second= 60;
 
12501
 
 
12502
  rc= mysql_stmt_execute(stmt);
 
12503
  check_execute(stmt, rc);
 
12504
  DIE_UNLESS(mysql_warning_count(mysql) != 6);
 
12505
 
 
12506
  verify_col_data("t1", "year", "0000-00-00 00:00:00");
 
12507
  verify_col_data("t1", "month", "0000-00-00 00:00:00");
 
12508
  verify_col_data("t1", "day", "0000-00-00 00:00:00");
 
12509
  verify_col_data("t1", "hour", "0000-00-00 00:00:00");
 
12510
  verify_col_data("t1", "min", "0000-00-00 00:00:00");
 
12511
  verify_col_data("t1", "sec", "0000-00-00 00:00:00");
 
12512
 
 
12513
  mysql_stmt_close(stmt);
 
12514
 
 
12515
  stmt_text= "delete from t1";
 
12516
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12517
  myquery(rc);
 
12518
 
 
12519
  stmt= mysql_simple_prepare(mysql, "INSERT INTO t1 (year, month, day) "
 
12520
                                    "VALUES (?, ?, ?)");
 
12521
  check_stmt(stmt);
 
12522
  verify_param_count(stmt, 3);
 
12523
 
 
12524
  /*
 
12525
    We reuse contents of bind and tm arrays left from previous part of test.
 
12526
  */
 
12527
  for (i= 0; i < 3; i++)
 
12528
    my_bind[i].buffer_type= MYSQL_TYPE_DATE;
 
12529
 
 
12530
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
12531
  check_execute(stmt, rc);
 
12532
 
 
12533
  rc= mysql_stmt_execute(stmt);
 
12534
  check_execute(stmt, rc);
 
12535
  DIE_UNLESS(mysql_warning_count(mysql) != 3);
 
12536
 
 
12537
  verify_col_data("t1", "year", "0000-00-00 00:00:00");
 
12538
  verify_col_data("t1", "month", "0000-00-00 00:00:00");
 
12539
  verify_col_data("t1", "day", "0000-00-00 00:00:00");
 
12540
 
 
12541
  mysql_stmt_close(stmt);
 
12542
 
 
12543
  stmt_text= "drop table t1";
 
12544
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12545
  myquery(rc);
 
12546
 
 
12547
  stmt_text= "create table t1 (day_ovfl time, day time, hour time, min time, sec time)";
 
12548
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12549
  myquery(rc);
 
12550
 
 
12551
  stmt= mysql_simple_prepare(mysql,
 
12552
                             "INSERT INTO t1 VALUES (?, ?, ?, ?, ?)");
 
12553
  check_stmt(stmt);
 
12554
  verify_param_count(stmt, 5);
 
12555
 
 
12556
  /*
 
12557
    Again we reuse what we can from previous part of test.
 
12558
  */
 
12559
  for (i= 0; i < 5; i++)
 
12560
    my_bind[i].buffer_type= MYSQL_TYPE_TIME;
 
12561
 
 
12562
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
12563
  check_execute(stmt, rc);
 
12564
 
 
12565
  tm[0].year= 0; tm[0].month= 0; tm[0].day= 10;
 
12566
  tm[0].hour= 12; tm[0].minute= 30; tm[0].second= 30;
 
12567
  tm[0].second_part= 0; tm[0].neg= 0;
 
12568
 
 
12569
  tm[4]= tm[3]= tm[2]= tm[1]= tm[0];
 
12570
  tm[0].day= 35; tm[1].day= 34; tm[2].hour= 30; tm[3].minute= 60; tm[4].second= 60;
 
12571
 
 
12572
  rc= mysql_stmt_execute(stmt);
 
12573
  check_execute(stmt, rc);
 
12574
  DIE_UNLESS(mysql_warning_count(mysql) == 2);
 
12575
 
 
12576
  verify_col_data("t1", "day_ovfl", "838:59:59");
 
12577
  verify_col_data("t1", "day", "828:30:30");
 
12578
  verify_col_data("t1", "hour", "270:30:30");
 
12579
  verify_col_data("t1", "min", "00:00:00");
 
12580
  verify_col_data("t1", "sec", "00:00:00");
 
12581
 
 
12582
  mysql_stmt_close(stmt);
 
12583
 
 
12584
  stmt_text= "drop table t1";
 
12585
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12586
  myquery(rc);
 
12587
}
 
12588
 
 
12589
 
 
12590
static void test_bug4172()
 
12591
{
 
12592
  MYSQL_STMT *stmt;
 
12593
  MYSQL_BIND my_bind[3];
 
12594
  const char *stmt_text;
 
12595
  MYSQL_RES *res;
 
12596
  MYSQL_ROW row;
 
12597
  int rc;
 
12598
  char f[100], d[100], e[100];
 
12599
  ulong f_len, d_len, e_len;
 
12600
 
 
12601
  myheader("test_bug4172");
 
12602
 
 
12603
  mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
12604
  mysql_query(mysql, "CREATE TABLE t1 (f float, d double, e decimal(10,4))");
 
12605
  mysql_query(mysql, "INSERT INTO t1 VALUES (12345.1234, 123456.123456, "
 
12606
                                            "123456.1234)");
 
12607
 
 
12608
  stmt= mysql_stmt_init(mysql);
 
12609
  stmt_text= "SELECT f, d, e FROM t1";
 
12610
 
 
12611
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12612
  check_execute(stmt, rc);
 
12613
  rc= mysql_stmt_execute(stmt);
 
12614
  check_execute(stmt, rc);
 
12615
 
 
12616
  bzero((char*) my_bind, sizeof(my_bind));
 
12617
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
12618
  my_bind[0].buffer= f;
 
12619
  my_bind[0].buffer_length= sizeof(f);
 
12620
  my_bind[0].length= &f_len;
 
12621
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
12622
  my_bind[1].buffer= d;
 
12623
  my_bind[1].buffer_length= sizeof(d);
 
12624
  my_bind[1].length= &d_len;
 
12625
  my_bind[2].buffer_type= MYSQL_TYPE_STRING;
 
12626
  my_bind[2].buffer= e;
 
12627
  my_bind[2].buffer_length= sizeof(e);
 
12628
  my_bind[2].length= &e_len;
 
12629
 
 
12630
  mysql_stmt_bind_result(stmt, my_bind);
 
12631
 
 
12632
  mysql_stmt_store_result(stmt);
 
12633
  rc= mysql_stmt_fetch(stmt);
 
12634
  check_execute(stmt, rc);
 
12635
 
 
12636
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12637
  myquery(rc);
 
12638
  res= mysql_store_result(mysql);
 
12639
  row= mysql_fetch_row(res);
 
12640
 
 
12641
  if (!opt_silent)
 
12642
  {
 
12643
    printf("Binary protocol: float=%s, double=%s, decimal(10,4)=%s\n",
 
12644
           f, d, e);
 
12645
    printf("Text protocol:   float=%s, double=%s, decimal(10,4)=%s\n",
 
12646
           row[0], row[1], row[2]);
 
12647
  }
 
12648
  DIE_UNLESS(!strcmp(f, row[0]) && !strcmp(d, row[1]) && !strcmp(e, row[2]));
 
12649
 
 
12650
  mysql_free_result(res);
 
12651
  mysql_stmt_close(stmt);
 
12652
}
 
12653
 
 
12654
 
 
12655
static void test_conversion()
 
12656
{
 
12657
  MYSQL_STMT *stmt;
 
12658
  const char *stmt_text;
 
12659
  int rc;
 
12660
  MYSQL_BIND my_bind[1];
 
12661
  char buff[4];
 
12662
  ulong length;
 
12663
 
 
12664
  myheader("test_conversion");
 
12665
 
 
12666
  stmt_text= "DROP TABLE IF EXISTS t1";
 
12667
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12668
  myquery(rc);
 
12669
  stmt_text= "CREATE TABLE t1 (a TEXT) DEFAULT CHARSET latin1";
 
12670
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12671
  myquery(rc);
 
12672
  stmt_text= "SET character_set_connection=utf8, character_set_client=utf8, "
 
12673
             " character_set_results=latin1";
 
12674
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12675
  myquery(rc);
 
12676
 
 
12677
  stmt= mysql_stmt_init(mysql);
 
12678
 
 
12679
  stmt_text= "INSERT INTO t1 (a) VALUES (?)";
 
12680
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12681
  check_execute(stmt, rc);
 
12682
 
 
12683
  bzero((char*) my_bind, sizeof(my_bind));
 
12684
  my_bind[0].buffer= buff;
 
12685
  my_bind[0].length= &length;
 
12686
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
12687
 
 
12688
  mysql_stmt_bind_param(stmt, my_bind);
 
12689
 
 
12690
  buff[0]= (uchar) 0xC3;
 
12691
  buff[1]= (uchar) 0xA0;
 
12692
  length= 2;
 
12693
 
 
12694
  rc= mysql_stmt_execute(stmt);
 
12695
  check_execute(stmt, rc);
 
12696
 
 
12697
  stmt_text= "SELECT a FROM t1";
 
12698
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12699
  check_execute(stmt, rc);
 
12700
  rc= mysql_stmt_execute(stmt);
 
12701
  check_execute(stmt, rc);
 
12702
 
 
12703
  my_bind[0].buffer_length= sizeof(buff);
 
12704
  mysql_stmt_bind_result(stmt, my_bind);
 
12705
 
 
12706
  rc= mysql_stmt_fetch(stmt);
 
12707
  DIE_UNLESS(rc == 0);
 
12708
  DIE_UNLESS(length == 1);
 
12709
  DIE_UNLESS((uchar) buff[0] == 0xE0);
 
12710
  rc= mysql_stmt_fetch(stmt);
 
12711
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
12712
 
 
12713
  mysql_stmt_close(stmt);
 
12714
  stmt_text= "DROP TABLE t1";
 
12715
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12716
  myquery(rc);
 
12717
  stmt_text= "SET NAMES DEFAULT";
 
12718
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12719
  myquery(rc);
 
12720
}
 
12721
 
 
12722
static void test_rewind(void)
 
12723
{
 
12724
  MYSQL_STMT *stmt;
 
12725
  MYSQL_BIND my_bind;
 
12726
  int rc = 0;
 
12727
  const char *stmt_text;
 
12728
  long unsigned int length=4, Data=0;
 
12729
  my_bool isnull=0;
 
12730
 
 
12731
  myheader("test_rewind");
 
12732
 
 
12733
  stmt_text= "CREATE TABLE t1 (a int)";
 
12734
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12735
  myquery(rc);
 
12736
  stmt_text= "INSERT INTO t1 VALUES(2),(3),(4)";
 
12737
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12738
  myquery(rc);
 
12739
 
 
12740
  stmt= mysql_stmt_init(mysql);
 
12741
 
 
12742
  stmt_text= "SELECT * FROM t1";
 
12743
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12744
  check_execute(stmt, rc);
 
12745
 
 
12746
  bzero((char*) &my_bind, sizeof(MYSQL_BIND));
 
12747
  my_bind.buffer_type= MYSQL_TYPE_LONG;
 
12748
  my_bind.buffer= (void *)&Data; /* this buffer won't be altered */
 
12749
  my_bind.length= &length;
 
12750
  my_bind.is_null= &isnull;
 
12751
 
 
12752
  rc= mysql_stmt_execute(stmt);
 
12753
  check_execute(stmt, rc);
 
12754
 
 
12755
  rc= mysql_stmt_store_result(stmt);
 
12756
  DIE_UNLESS(rc == 0);
 
12757
 
 
12758
  rc= mysql_stmt_bind_result(stmt, &my_bind);
 
12759
  DIE_UNLESS(rc == 0);
 
12760
 
 
12761
  /* retreive all result sets till we are at the end */
 
12762
  while(!mysql_stmt_fetch(stmt))
 
12763
    if (!opt_silent)
 
12764
      printf("fetched result:%ld\n", Data);
 
12765
 
 
12766
  DIE_UNLESS(rc != MYSQL_NO_DATA);
 
12767
 
 
12768
  /* seek to the first row */
 
12769
  mysql_stmt_data_seek(stmt, 0);
 
12770
 
 
12771
  /* now we should be able to fetch the results again */
 
12772
  /* but mysql_stmt_fetch returns MYSQL_NO_DATA */
 
12773
  while(!(rc= mysql_stmt_fetch(stmt)))
 
12774
    if (!opt_silent)
 
12775
      printf("fetched result after seek:%ld\n", Data);
 
12776
  
 
12777
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
12778
 
 
12779
  stmt_text= "DROP TABLE t1";
 
12780
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12781
  myquery(rc);
 
12782
  rc= mysql_stmt_free_result(stmt);
 
12783
  rc= mysql_stmt_close(stmt);
 
12784
}
 
12785
 
 
12786
 
 
12787
static void test_truncation()
 
12788
{
 
12789
  MYSQL_STMT *stmt;
 
12790
  const char *stmt_text;
 
12791
  int rc;
 
12792
  uint bind_count;
 
12793
  MYSQL_BIND *bind_array, *my_bind;
 
12794
 
 
12795
  myheader("test_truncation");
 
12796
 
 
12797
  /* Prepare the test table */
 
12798
  rc= mysql_query(mysql, "drop table if exists t1");
 
12799
  myquery(rc);
 
12800
 
 
12801
  stmt_text= "create table t1 ("
 
12802
             "i8 tinyint, ui8 tinyint unsigned, "
 
12803
             "i16 smallint, i16_1 smallint, "
 
12804
             "ui16 smallint unsigned, i32 int, i32_1 int, "
 
12805
             "d double, d_1 double, ch char(30), ch_1 char(30), "
 
12806
             "tx text, tx_1 text, ch_2 char(30) "
 
12807
             ")";
 
12808
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12809
  myquery(rc);
 
12810
  stmt_text= "insert into t1 VALUES ("
 
12811
             "-10, "                            /* i8 */
 
12812
             "200, "                            /* ui8 */
 
12813
             "32000, "                          /* i16 */
 
12814
             "-32767, "                         /* i16_1 */
 
12815
             "64000, "                          /* ui16 */
 
12816
             "1073741824, "                     /* i32 */
 
12817
             "1073741825, "                     /* i32_1 */
 
12818
             "123.456, "                        /* d */
 
12819
             "-12345678910, "                   /* d_1 */
 
12820
             "'111111111111111111111111111111',"/* ch */
 
12821
             "'abcdef', "                       /* ch_1 */
 
12822
             "'12345          ', "              /* tx */
 
12823
             "'12345.67               ', "      /* tx_1 */
 
12824
             "'12345.67abc'"                    /* ch_2 */
 
12825
             ")";
 
12826
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
12827
  myquery(rc);
 
12828
 
 
12829
  stmt_text= "select i8 c1, i8 c2, ui8 c3, i16_1 c4, ui16 c5, "
 
12830
             "       i16 c6, ui16 c7, i32 c8, i32_1 c9, i32_1 c10, "
 
12831
             "       d c11, d_1 c12, d_1 c13, ch c14, ch_1 c15, tx c16, "
 
12832
             "       tx_1 c17, ch_2 c18 "
 
12833
             "from t1";
 
12834
 
 
12835
  stmt= mysql_stmt_init(mysql);
 
12836
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
12837
  check_execute(stmt, rc);
 
12838
  rc= mysql_stmt_execute(stmt);
 
12839
  check_execute(stmt, rc);
 
12840
  bind_count= (uint) mysql_stmt_field_count(stmt);
 
12841
 
 
12842
  /*************** Fill in the bind structure and bind it **************/
 
12843
  bind_array= malloc(sizeof(MYSQL_BIND) * bind_count);
 
12844
  bzero((char*) bind_array, sizeof(MYSQL_BIND) * bind_count);
 
12845
  for (my_bind= bind_array; my_bind < bind_array + bind_count; my_bind++)
 
12846
    my_bind->error= &my_bind->error_value;
 
12847
  my_bind= bind_array;
 
12848
 
 
12849
  my_bind->buffer= malloc(sizeof(uint8));
 
12850
  my_bind->buffer_type= MYSQL_TYPE_TINY;
 
12851
  my_bind->is_unsigned= TRUE;
 
12852
 
 
12853
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12854
  my_bind->buffer= malloc(sizeof(uint32));
 
12855
  my_bind->buffer_type= MYSQL_TYPE_LONG;
 
12856
  my_bind->is_unsigned= TRUE;
 
12857
 
 
12858
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12859
  my_bind->buffer= malloc(sizeof(int8));
 
12860
  my_bind->buffer_type= MYSQL_TYPE_TINY;
 
12861
 
 
12862
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12863
  my_bind->buffer= malloc(sizeof(uint16));
 
12864
  my_bind->buffer_type= MYSQL_TYPE_SHORT;
 
12865
  my_bind->is_unsigned= TRUE;
 
12866
 
 
12867
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12868
  my_bind->buffer= malloc(sizeof(int16));
 
12869
  my_bind->buffer_type= MYSQL_TYPE_SHORT;
 
12870
 
 
12871
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12872
  my_bind->buffer= malloc(sizeof(uint16));
 
12873
  my_bind->buffer_type= MYSQL_TYPE_SHORT;
 
12874
  my_bind->is_unsigned= TRUE;
 
12875
 
 
12876
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12877
  my_bind->buffer= malloc(sizeof(int8));
 
12878
  my_bind->buffer_type= MYSQL_TYPE_TINY;
 
12879
  my_bind->is_unsigned= TRUE;
 
12880
 
 
12881
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12882
  my_bind->buffer= malloc(sizeof(float));
 
12883
  my_bind->buffer_type= MYSQL_TYPE_FLOAT;
 
12884
 
 
12885
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12886
  my_bind->buffer= malloc(sizeof(float));
 
12887
  my_bind->buffer_type= MYSQL_TYPE_FLOAT;
 
12888
 
 
12889
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12890
  my_bind->buffer= malloc(sizeof(double));
 
12891
  my_bind->buffer_type= MYSQL_TYPE_DOUBLE;
 
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(ulonglong));
 
12899
  my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
 
12900
  my_bind->is_unsigned= TRUE;
 
12901
 
 
12902
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12903
  my_bind->buffer= malloc(sizeof(longlong));
 
12904
  my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
 
12905
 
 
12906
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12907
  my_bind->buffer= malloc(sizeof(longlong));
 
12908
  my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
 
12909
 
 
12910
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12911
  my_bind->buffer= malloc(sizeof(longlong));
 
12912
  my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
 
12913
 
 
12914
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12915
  my_bind->buffer= malloc(sizeof(longlong));
 
12916
  my_bind->buffer_type= MYSQL_TYPE_LONGLONG;
 
12917
 
 
12918
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12919
  my_bind->buffer= malloc(sizeof(double));
 
12920
  my_bind->buffer_type= MYSQL_TYPE_DOUBLE;
 
12921
 
 
12922
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12923
  my_bind->buffer= malloc(sizeof(double));
 
12924
  my_bind->buffer_type= MYSQL_TYPE_DOUBLE;
 
12925
 
 
12926
  rc= mysql_stmt_bind_result(stmt, bind_array);
 
12927
  check_execute(stmt, rc);
 
12928
  rc= mysql_stmt_fetch(stmt);
 
12929
  DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
 
12930
 
 
12931
  /*************** Verify truncation results ***************************/
 
12932
  my_bind= bind_array;
 
12933
 
 
12934
  /* signed tiny -> tiny */
 
12935
  DIE_UNLESS(*my_bind->error && * (int8*) my_bind->buffer == -10);
 
12936
 
 
12937
  /* signed tiny -> uint32 */
 
12938
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12939
  DIE_UNLESS(*my_bind->error && * (int32*) my_bind->buffer == -10);
 
12940
 
 
12941
  /* unsigned tiny -> tiny */
 
12942
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12943
  DIE_UNLESS(*my_bind->error && * (uint8*) my_bind->buffer == 200);
 
12944
 
 
12945
  /* short -> ushort */
 
12946
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12947
  DIE_UNLESS(*my_bind->error && * (int16*) my_bind->buffer == -32767);
 
12948
 
 
12949
  /* ushort -> short */
 
12950
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12951
  DIE_UNLESS(*my_bind->error && * (uint16*) my_bind->buffer == 64000);
 
12952
 
 
12953
  /* short -> ushort (no truncation, data is in the range of target type) */
 
12954
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12955
  DIE_UNLESS(! *my_bind->error && * (uint16*) my_bind->buffer == 32000);
 
12956
 
 
12957
  /* ushort -> utiny */
 
12958
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12959
  DIE_UNLESS(*my_bind->error && * (int8*) my_bind->buffer == 0);
 
12960
 
 
12961
  /* int -> float: no truncation, the number is a power of two */
 
12962
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12963
  DIE_UNLESS(! *my_bind->error && * (float*) my_bind->buffer == 1073741824);
 
12964
 
 
12965
  /* int -> float: truncation, not enough bits in float */
 
12966
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12967
  DIE_UNLESS(*my_bind->error);
 
12968
 
 
12969
  /* int -> double: no truncation */
 
12970
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12971
  DIE_UNLESS(! *my_bind->error && * (double*) my_bind->buffer == 1073741825);
 
12972
 
 
12973
  /* double -> longlong: fractional part is lost */
 
12974
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12975
 
 
12976
  /* double -> ulonglong, negative fp number to unsigned integer */
 
12977
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12978
  /* Value in the buffer is not defined: don't test it */
 
12979
  DIE_UNLESS(*my_bind->error);
 
12980
 
 
12981
  /* double -> longlong, negative fp number to signed integer: no loss */
 
12982
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12983
  DIE_UNLESS(! *my_bind->error && * (longlong*) my_bind->buffer == LL(-12345678910));
 
12984
 
 
12985
  /* big numeric string -> number */
 
12986
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12987
  DIE_UNLESS(*my_bind->error);
 
12988
 
 
12989
  /* junk string -> number */
 
12990
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12991
  DIE_UNLESS(*my_bind->error && *(longlong*) my_bind->buffer == 0);
 
12992
 
 
12993
  /* string with trailing spaces -> number */
 
12994
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12995
  DIE_UNLESS(! *my_bind->error && *(longlong*) my_bind->buffer == 12345);
 
12996
 
 
12997
  /* string with trailing spaces -> double */
 
12998
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
12999
  DIE_UNLESS(! *my_bind->error && *(double*) my_bind->buffer == 12345.67);
 
13000
 
 
13001
  /* string with trailing junk -> double */
 
13002
  DIE_UNLESS(my_bind++ < bind_array + bind_count);
 
13003
  /*
 
13004
    XXX: There must be a truncation error: but it's not the way the server
 
13005
    behaves, so let's leave it for now.
 
13006
  */
 
13007
  DIE_UNLESS(*(double*) my_bind->buffer == 12345.67);
 
13008
  /*
 
13009
    TODO: string -> double,  double -> time, double -> string (truncation
 
13010
          errors are not supported here yet)
 
13011
          longlong -> time/date/datetime
 
13012
          date -> time, date -> timestamp, date -> number
 
13013
          time -> string, time -> date, time -> timestamp,
 
13014
          number -> date string -> date
 
13015
  */
 
13016
  /*************** Cleanup *********************************************/
 
13017
 
 
13018
  mysql_stmt_close(stmt);
 
13019
 
 
13020
  for (my_bind= bind_array; my_bind < bind_array + bind_count; my_bind++)
 
13021
    free(my_bind->buffer);
 
13022
  free(bind_array);
 
13023
 
 
13024
  rc= mysql_query(mysql, "drop table t1");
 
13025
  myquery(rc);
 
13026
}
 
13027
 
 
13028
static void test_truncation_option()
 
13029
{
 
13030
  MYSQL_STMT *stmt;
 
13031
  const char *stmt_text;
 
13032
  int rc;
 
13033
  uint8 buf;
 
13034
  my_bool option= 0;
 
13035
  my_bool error;
 
13036
  MYSQL_BIND my_bind;
 
13037
 
 
13038
  myheader("test_truncation_option");
 
13039
 
 
13040
  /* Prepare the test table */
 
13041
  stmt_text= "select -1";
 
13042
 
 
13043
  stmt= mysql_stmt_init(mysql);
 
13044
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
13045
  check_execute(stmt, rc);
 
13046
  rc= mysql_stmt_execute(stmt);
 
13047
  check_execute(stmt, rc);
 
13048
 
 
13049
  bzero((char*) &my_bind, sizeof(my_bind));
 
13050
 
 
13051
  my_bind.buffer= (void*) &buf;
 
13052
  my_bind.buffer_type= MYSQL_TYPE_TINY;
 
13053
  my_bind.is_unsigned= TRUE;
 
13054
  my_bind.error= &error;
 
13055
 
 
13056
  rc= mysql_stmt_bind_result(stmt, &my_bind);
 
13057
  check_execute(stmt, rc);
 
13058
  rc= mysql_stmt_fetch(stmt);
 
13059
  DIE_UNLESS(rc == MYSQL_DATA_TRUNCATED);
 
13060
  DIE_UNLESS(error);
 
13061
  rc= mysql_options(mysql, MYSQL_REPORT_DATA_TRUNCATION, (char*) &option);
 
13062
  myquery(rc);
 
13063
  /* need to rebind for the new setting to take effect */
 
13064
  rc= mysql_stmt_bind_result(stmt, &my_bind);
 
13065
  check_execute(stmt, rc);
 
13066
  rc= mysql_stmt_execute(stmt);
 
13067
  check_execute(stmt, rc);
 
13068
  rc= mysql_stmt_fetch(stmt);
 
13069
  check_execute(stmt, rc);
 
13070
  /* The only change is rc - error pointers are still filled in */
 
13071
  DIE_UNLESS(error == 1);
 
13072
  /* restore back the defaults */
 
13073
  option= 1;
 
13074
  mysql_options(mysql, MYSQL_REPORT_DATA_TRUNCATION, (char*) &option);
 
13075
 
 
13076
  mysql_stmt_close(stmt);
 
13077
}
 
13078
 
 
13079
 
 
13080
/* Bug#6761 - mysql_list_fields doesn't work */
 
13081
 
 
13082
static void test_bug6761(void)
 
13083
{
 
13084
  const char *stmt_text;
 
13085
  MYSQL_RES *res;
 
13086
  int rc;
 
13087
  myheader("test_bug6761");
 
13088
 
 
13089
  stmt_text= "CREATE TABLE t1 (a int, b char(255), c decimal)";
 
13090
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
13091
  myquery(rc);
 
13092
 
 
13093
  res= mysql_list_fields(mysql, "t1", "%");
 
13094
  DIE_UNLESS(res && mysql_num_fields(res) == 3);
 
13095
  mysql_free_result(res);
 
13096
 
 
13097
  stmt_text= "DROP TABLE t1";
 
13098
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
13099
  myquery(rc);
 
13100
}
 
13101
 
 
13102
 
 
13103
/* Bug#8330 - mysql_stmt_execute crashes (libmysql) */
 
13104
 
 
13105
static void test_bug8330()
 
13106
{
 
13107
  const char *stmt_text;
 
13108
  MYSQL_STMT *stmt[2];
 
13109
  int i, rc;
 
13110
  const char *query= "select a,b from t1 where a=?";
 
13111
  MYSQL_BIND my_bind[2];
 
13112
  long lval[2];
 
13113
 
 
13114
  myheader("test_bug8330");
 
13115
 
 
13116
  stmt_text= "drop table if exists t1";
 
13117
  /* in case some previos test failed */
 
13118
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
13119
  myquery(rc);
 
13120
  stmt_text= "create table t1 (a int, b int)";
 
13121
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
13122
  myquery(rc);
 
13123
 
 
13124
  bzero((char*) my_bind, sizeof(my_bind));
 
13125
  for (i=0; i < 2; i++)
 
13126
  {
 
13127
    stmt[i]= mysql_stmt_init(mysql);
 
13128
    rc= mysql_stmt_prepare(stmt[i], query, strlen(query));
 
13129
    check_execute(stmt[i], rc);
 
13130
 
 
13131
    my_bind[i].buffer_type= MYSQL_TYPE_LONG;
 
13132
    my_bind[i].buffer= (void*) &lval[i];
 
13133
    my_bind[i].is_null= 0;
 
13134
    mysql_stmt_bind_param(stmt[i], &my_bind[i]);
 
13135
  }
 
13136
 
 
13137
  rc= mysql_stmt_execute(stmt[0]);
 
13138
  check_execute(stmt[0], rc);
 
13139
 
 
13140
  rc= mysql_stmt_execute(stmt[1]);
 
13141
  DIE_UNLESS(rc && mysql_stmt_errno(stmt[1]) == CR_COMMANDS_OUT_OF_SYNC);
 
13142
  rc= mysql_stmt_execute(stmt[0]);
 
13143
  check_execute(stmt[0], rc);
 
13144
 
 
13145
  mysql_stmt_close(stmt[0]);
 
13146
  mysql_stmt_close(stmt[1]);
 
13147
 
 
13148
  stmt_text= "drop table t1";
 
13149
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
13150
  myquery(rc);
 
13151
}
 
13152
 
 
13153
 
 
13154
/* Bug#7990 - mysql_stmt_close doesn't reset mysql->net.last_error */
 
13155
 
 
13156
static void test_bug7990()
 
13157
{
 
13158
  MYSQL_STMT *stmt;
 
13159
  int rc;
 
13160
  myheader("test_bug7990");
 
13161
 
 
13162
  stmt= mysql_stmt_init(mysql);
 
13163
  rc= mysql_stmt_prepare(stmt, "foo", 3);
 
13164
  /*
 
13165
    XXX: the fact that we store errno both in STMT and in
 
13166
    MYSQL is not documented and is subject to change in 5.0
 
13167
  */
 
13168
  DIE_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql));
 
13169
  mysql_stmt_close(stmt);
 
13170
  DIE_UNLESS(!mysql_errno(mysql));
 
13171
}
 
13172
 
 
13173
/*
 
13174
  Bug #15518 - Reusing a stmt that has failed during prepare
 
13175
  does not clear error
 
13176
*/
 
13177
 
 
13178
static void test_bug15518()
 
13179
{
 
13180
  MYSQL_STMT *stmt;
 
13181
  MYSQL* mysql1;
 
13182
  int rc;
 
13183
  myheader("test_bug15518");
 
13184
 
 
13185
  mysql1= mysql_client_init(NULL);
 
13186
 
 
13187
  if (!mysql_real_connect(mysql1, opt_host, opt_user, opt_password,
 
13188
                          opt_db ? opt_db : "test", opt_port, opt_unix_socket,
 
13189
                          CLIENT_MULTI_STATEMENTS))
 
13190
  {
 
13191
    fprintf(stderr, "Failed to connect to the database\n");
 
13192
    DIE_UNLESS(0);
 
13193
  }
 
13194
 
 
13195
  stmt= mysql_stmt_init(mysql1);
 
13196
 
 
13197
  /*
 
13198
    The prepare of foo should fail with errno 1064 since
 
13199
    it's not a valid query
 
13200
  */
 
13201
  rc= mysql_stmt_prepare(stmt, "foo", 3);
 
13202
  if (!opt_silent)
 
13203
    fprintf(stdout, "rc: %d, mysql_stmt_errno: %d, mysql_errno: %d\n",
 
13204
            rc, mysql_stmt_errno(stmt), mysql_errno(mysql1));
 
13205
  DIE_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql1));
 
13206
 
 
13207
  /*
 
13208
    Use the same stmt and reprepare with another query that
 
13209
    suceeds
 
13210
  */
 
13211
  rc= mysql_stmt_prepare(stmt, "SHOW STATUS", 12);
 
13212
  if (!opt_silent)
 
13213
    fprintf(stdout, "rc: %d, mysql_stmt_errno: %d, mysql_errno: %d\n",
 
13214
            rc, mysql_stmt_errno(stmt), mysql_errno(mysql1));
 
13215
  DIE_UNLESS(!rc || mysql_stmt_errno(stmt) || mysql_errno(mysql1));
 
13216
 
 
13217
  mysql_stmt_close(stmt);
 
13218
  DIE_UNLESS(!mysql_errno(mysql1));
 
13219
 
 
13220
  /*
 
13221
    part2, when connection to server has been closed
 
13222
    after first prepare
 
13223
  */
 
13224
  stmt= mysql_stmt_init(mysql1);
 
13225
  rc= mysql_stmt_prepare(stmt, "foo", 3);
 
13226
  if (!opt_silent)
 
13227
    fprintf(stdout, "rc: %d, mysql_stmt_errno: %d, mysql_errno: %d\n",
 
13228
            rc, mysql_stmt_errno(stmt), mysql_errno(mysql1));
 
13229
  DIE_UNLESS(rc && mysql_stmt_errno(stmt) && mysql_errno(mysql1));
 
13230
 
 
13231
  /* Close connection to server */
 
13232
  mysql_close(mysql1);
 
13233
 
 
13234
  /*
 
13235
    Use the same stmt and reprepare with another query that
 
13236
    suceeds. The prepare should fail with error 2013 since
 
13237
    connection to server has been closed.
 
13238
  */
 
13239
  rc= mysql_stmt_prepare(stmt, "SHOW STATUS", 12);
 
13240
  if (!opt_silent)
 
13241
    fprintf(stdout, "rc: %d, mysql_stmt_errno: %d\n",
 
13242
            rc, mysql_stmt_errno(stmt));
 
13243
  DIE_UNLESS(rc && mysql_stmt_errno(stmt));
 
13244
 
 
13245
  mysql_stmt_close(stmt);
 
13246
}
 
13247
 
 
13248
 
 
13249
static void disable_general_log()
 
13250
{
 
13251
  int rc;
 
13252
  rc= mysql_query(mysql, "set @@global.general_log=off");
 
13253
  myquery(rc);
 
13254
}
 
13255
 
 
13256
 
 
13257
static void enable_general_log(int truncate)
 
13258
{
 
13259
  int rc;
 
13260
 
 
13261
  rc= mysql_query(mysql, "set @save_global_general_log=@@global.general_log");
 
13262
  myquery(rc);
 
13263
 
 
13264
  rc= mysql_query(mysql, "set @@global.general_log=on");
 
13265
  myquery(rc);
 
13266
 
 
13267
  if (truncate)
 
13268
  {
 
13269
    rc= mysql_query(mysql, "truncate mysql.general_log");
 
13270
    myquery(rc);
 
13271
  }
 
13272
}
 
13273
 
 
13274
 
 
13275
static void restore_general_log()
 
13276
{
 
13277
  int rc;
 
13278
  rc= mysql_query(mysql, "set @@global.general_log=@save_global_general_log");
 
13279
  myquery(rc);
 
13280
}
 
13281
 
 
13282
 
 
13283
static void test_view_sp_list_fields()
 
13284
{
 
13285
  int           rc;
 
13286
  MYSQL_RES     *res;
 
13287
 
 
13288
  myheader("test_view_sp_list_fields");
 
13289
 
 
13290
  rc= mysql_query(mysql, "DROP FUNCTION IF EXISTS f1");
 
13291
  myquery(rc);
 
13292
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS v1, t1, t2");
 
13293
  myquery(rc);
 
13294
  rc= mysql_query(mysql, "DROP VIEW IF EXISTS v1, t1, t2");
 
13295
  myquery(rc);
 
13296
  rc= mysql_query(mysql, "create function f1 () returns int return 5");
 
13297
  myquery(rc);
 
13298
  rc= mysql_query(mysql, "create table t1 (s1 char,s2 char)");
 
13299
  myquery(rc);
 
13300
  rc= mysql_query(mysql, "create table t2 (s1 int);");
 
13301
  myquery(rc);
 
13302
  rc= mysql_query(mysql, "create view v1 as select s2,sum(s1) - \
 
13303
count(s2) as vx from t1 group by s2 having sum(s1) - count(s2) < (select f1() \
 
13304
from t2);");
 
13305
  myquery(rc);
 
13306
  res= mysql_list_fields(mysql, "v1", NullS);
 
13307
  DIE_UNLESS(res != 0 && mysql_num_fields(res) != 0);
 
13308
  rc= mysql_query(mysql, "DROP FUNCTION f1");
 
13309
  myquery(rc);
 
13310
  rc= mysql_query(mysql, "DROP VIEW v1");
 
13311
  myquery(rc);
 
13312
  rc= mysql_query(mysql, "DROP TABLE t1, t2");
 
13313
  mysql_free_result(res);
 
13314
  myquery(rc);
 
13315
 
 
13316
}
 
13317
 
 
13318
 
 
13319
/*
 
13320
 Test mysql_real_escape_string() with gbk charset
 
13321
 
 
13322
 The important part is that 0x27 (') is the second-byte in a invalid
 
13323
 two-byte GBK character here. But 0xbf5c is a valid GBK character, so
 
13324
 it needs to be escaped as 0x5cbf27
 
13325
*/
 
13326
#define TEST_BUG8378_IN  "\xef\xbb\xbf\x27\xbf\x10"
 
13327
#define TEST_BUG8378_OUT "\xef\xbb\x5c\xbf\x5c\x27\x5c\xbf\x10"
 
13328
 
 
13329
static void test_bug8378()
 
13330
{
 
13331
#if defined(HAVE_CHARSET_gbk) && !defined(EMBEDDED_LIBRARY)
 
13332
  MYSQL *lmysql;
 
13333
  char out[9]; /* strlen(TEST_BUG8378)*2+1 */
 
13334
  char buf[256];
 
13335
  int len, rc;
 
13336
 
 
13337
  myheader("test_bug8378");
 
13338
 
 
13339
  if (!opt_silent)
 
13340
    fprintf(stdout, "\n Establishing a test connection ...");
 
13341
  if (!(lmysql= mysql_client_init(NULL)))
 
13342
  {
 
13343
    myerror("mysql_client_init() failed");
 
13344
    exit(1);
 
13345
  }
 
13346
  if (mysql_options(lmysql, MYSQL_SET_CHARSET_NAME, "gbk"))
 
13347
  {
 
13348
    myerror("mysql_options() failed");
 
13349
    exit(1);
 
13350
  }
 
13351
  if (!(mysql_real_connect(lmysql, opt_host, opt_user,
 
13352
                           opt_password, current_db, opt_port,
 
13353
                           opt_unix_socket, 0)))
 
13354
  {
 
13355
    myerror("connection failed");
 
13356
    exit(1);
 
13357
  }
 
13358
  if (!opt_silent)
 
13359
    fprintf(stdout, "OK");
 
13360
 
 
13361
  rc= mysql_query(lmysql, "SET SQL_MODE=''");
 
13362
  myquery(rc);
 
13363
 
 
13364
  len= mysql_real_escape_string(lmysql, out, TEST_BUG8378_IN, 4);
 
13365
 
 
13366
  /* No escaping should have actually happened. */
 
13367
  DIE_UNLESS(memcmp(out, TEST_BUG8378_OUT, len) == 0);
 
13368
 
 
13369
  sprintf(buf, "SELECT '%s'", out);
 
13370
  
 
13371
  rc=mysql_real_query(lmysql, buf, strlen(buf));
 
13372
  myquery(rc);
 
13373
 
 
13374
  mysql_close(lmysql);
 
13375
#endif
 
13376
}
 
13377
 
 
13378
 
 
13379
static void test_bug8722()
 
13380
{
 
13381
  MYSQL_STMT *stmt;
 
13382
  int rc;
 
13383
  const char *stmt_text;
 
13384
 
 
13385
  myheader("test_bug8722");
 
13386
  /* Prepare test data */
 
13387
  stmt_text= "drop table if exists t1, v1";
 
13388
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
13389
  myquery(rc);
 
13390
  stmt_text= "CREATE TABLE t1 (c1 varchar(10), c2 varchar(10), c3 varchar(10),"
 
13391
                             " c4 varchar(10), c5 varchar(10), c6 varchar(10),"
 
13392
                             " c7 varchar(10), c8 varchar(10), c9 varchar(10),"
 
13393
                             "c10 varchar(10))";
 
13394
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
13395
  myquery(rc);
 
13396
  stmt_text= "INSERT INTO t1 VALUES (1,2,3,4,5,6,7,8,9,10)";
 
13397
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
13398
  myquery(rc);
 
13399
  stmt_text= "CREATE VIEW v1 AS SELECT * FROM t1";
 
13400
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
13401
  myquery(rc);
 
13402
  /* Note: if you uncomment following block everything works fine */
 
13403
/*
 
13404
  rc= mysql_query(mysql, "sellect * from v1");
 
13405
  myquery(rc);
 
13406
  mysql_free_result(mysql_store_result(mysql));
 
13407
*/
 
13408
 
 
13409
  stmt= mysql_stmt_init(mysql);
 
13410
  stmt_text= "select * from v1";
 
13411
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
13412
  check_execute(stmt, rc);
 
13413
  mysql_stmt_close(stmt);
 
13414
  stmt_text= "drop table if exists t1, v1";
 
13415
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
13416
  myquery(rc);
 
13417
}
 
13418
 
 
13419
 
 
13420
MYSQL_STMT *open_cursor(const char *query)
 
13421
{
 
13422
  int rc;
 
13423
  const ulong type= (ulong)CURSOR_TYPE_READ_ONLY;
 
13424
 
 
13425
  MYSQL_STMT *stmt= mysql_stmt_init(mysql);
 
13426
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
13427
  check_execute(stmt, rc);
 
13428
 
 
13429
  mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
 
13430
  return stmt;
 
13431
}
 
13432
 
 
13433
 
 
13434
static void test_bug8880()
 
13435
{
 
13436
  MYSQL_STMT *stmt_list[2], **stmt;
 
13437
  MYSQL_STMT **stmt_list_end= (MYSQL_STMT**) stmt_list + 2;
 
13438
  int rc;
 
13439
 
 
13440
  myheader("test_bug8880");
 
13441
 
 
13442
  mysql_query(mysql, "drop table if exists t1");
 
13443
  mysql_query(mysql, "create table t1 (a int not null primary key, b int)");
 
13444
  rc= mysql_query(mysql, "insert into t1 values (1,1)");
 
13445
  myquery(rc);                                  /* one check is enough */
 
13446
  /*
 
13447
    when inserting 2 rows everything works well
 
13448
    mysql_query(mysql, "INSERT INTO t1 VALUES (1,1),(2,2)");
 
13449
  */
 
13450
  for (stmt= stmt_list; stmt < stmt_list_end; stmt++)
 
13451
    *stmt= open_cursor("select a from t1");
 
13452
  for (stmt= stmt_list; stmt < stmt_list_end; stmt++)
 
13453
  {
 
13454
    rc= mysql_stmt_execute(*stmt);
 
13455
    check_execute(*stmt, rc);
 
13456
  }
 
13457
  for (stmt= stmt_list; stmt < stmt_list_end; stmt++)
 
13458
    mysql_stmt_close(*stmt);
 
13459
}
 
13460
 
 
13461
 
 
13462
static void test_bug9159()
 
13463
{
 
13464
  MYSQL_STMT *stmt;
 
13465
  int rc;
 
13466
  const char *stmt_text= "select a, b from t1";
 
13467
  const unsigned long type= CURSOR_TYPE_READ_ONLY;
 
13468
 
 
13469
  myheader("test_bug9159");
 
13470
 
 
13471
  mysql_query(mysql, "drop table if exists t1");
 
13472
  mysql_query(mysql, "create table t1 (a int not null primary key, b int)");
 
13473
  rc= mysql_query(mysql, "insert into t1 values (1,1)");
 
13474
  myquery(rc);
 
13475
 
 
13476
  stmt= mysql_stmt_init(mysql);
 
13477
  mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
13478
  mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void *)&type);
 
13479
 
 
13480
  mysql_stmt_execute(stmt);
 
13481
  mysql_stmt_close(stmt);
 
13482
  rc= mysql_query(mysql, "drop table if exists t1");
 
13483
  myquery(rc);
 
13484
}
 
13485
 
 
13486
 
 
13487
/* Crash when opening a cursor to a query with DISTICNT and no key */
 
13488
 
 
13489
static void test_bug9520()
 
13490
{
 
13491
  MYSQL_STMT *stmt;
 
13492
  MYSQL_BIND my_bind[1];
 
13493
  char a[6];
 
13494
  ulong a_len;
 
13495
  int rc, row_count= 0;
 
13496
 
 
13497
  myheader("test_bug9520");
 
13498
 
 
13499
  mysql_query(mysql, "drop table if exists t1");
 
13500
  mysql_query(mysql, "create table t1 (a char(5), b char(5), c char(5),"
 
13501
                     " primary key (a, b, c))");
 
13502
  rc= mysql_query(mysql, "insert into t1 values ('x', 'y', 'z'), "
 
13503
                  " ('a', 'b', 'c'), ('k', 'l', 'm')");
 
13504
  myquery(rc);
 
13505
 
 
13506
  stmt= open_cursor("select distinct b from t1");
 
13507
 
 
13508
  /*
 
13509
    Not crashes with:
 
13510
    stmt= open_cursor("select distinct a from t1");
 
13511
  */
 
13512
 
 
13513
  rc= mysql_stmt_execute(stmt);
 
13514
  check_execute(stmt, rc);
 
13515
 
 
13516
  bzero((char*) my_bind, sizeof(my_bind));
 
13517
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
13518
  my_bind[0].buffer= (char*) a;
 
13519
  my_bind[0].buffer_length= sizeof(a);
 
13520
  my_bind[0].length= &a_len;
 
13521
 
 
13522
  mysql_stmt_bind_result(stmt, my_bind);
 
13523
 
 
13524
  while (!(rc= mysql_stmt_fetch(stmt)))
 
13525
    row_count++;
 
13526
 
 
13527
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
13528
 
 
13529
  if (!opt_silent)
 
13530
    printf("Fetched %d rows\n", row_count);
 
13531
  DBUG_ASSERT(row_count == 3);
 
13532
 
 
13533
  mysql_stmt_close(stmt);
 
13534
 
 
13535
  rc= mysql_query(mysql, "drop table t1");
 
13536
  myquery(rc);
 
13537
}
 
13538
 
 
13539
 
 
13540
/*
 
13541
  We can't have more than one cursor open for a prepared statement.
 
13542
  Test re-executions of a PS with cursor; mysql_stmt_reset must close
 
13543
  the cursor attached to the statement, if there is one.
 
13544
*/
 
13545
 
 
13546
static void test_bug9478()
 
13547
{
 
13548
  MYSQL_STMT *stmt;
 
13549
  MYSQL_BIND my_bind[1];
 
13550
  char a[6];
 
13551
  ulong a_len;
 
13552
  int rc, i;
 
13553
  DBUG_ENTER("test_bug9478");
 
13554
 
 
13555
  myheader("test_bug9478");
 
13556
 
 
13557
  mysql_query(mysql, "drop table if exists t1");
 
13558
  mysql_query(mysql, "create table t1 (id integer not null primary key, "
 
13559
                     " name varchar(20) not null)");
 
13560
  rc= mysql_query(mysql, "insert into t1 (id, name) values "
 
13561
                         " (1, 'aaa'), (2, 'bbb'), (3, 'ccc')");
 
13562
  myquery(rc);
 
13563
 
 
13564
  stmt= open_cursor("select name from t1 where id=2");
 
13565
 
 
13566
  bzero((char*) my_bind, sizeof(my_bind));
 
13567
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
13568
  my_bind[0].buffer= (char*) a;
 
13569
  my_bind[0].buffer_length= sizeof(a);
 
13570
  my_bind[0].length= &a_len;
 
13571
  mysql_stmt_bind_result(stmt, my_bind);
 
13572
 
 
13573
  for (i= 0; i < 5; i++)
 
13574
  {
 
13575
    rc= mysql_stmt_execute(stmt);
 
13576
    check_execute(stmt, rc);
 
13577
    rc= mysql_stmt_fetch(stmt);
 
13578
    check_execute(stmt, rc);
 
13579
    if (!opt_silent && i == 0)
 
13580
      printf("Fetched row: %s\n", a);
 
13581
 
 
13582
    /*
 
13583
      The query above is a one-row result set. Therefore, there is no
 
13584
      cursor associated with it, as the server won't bother with opening
 
13585
      a cursor for a one-row result set. The first row was read from the
 
13586
      server in the fetch above. But there is eof packet pending in the
 
13587
      network. mysql_stmt_execute will flush the packet and successfully
 
13588
      execute the statement.
 
13589
    */
 
13590
 
 
13591
    rc= mysql_stmt_execute(stmt);
 
13592
    check_execute(stmt, rc);
 
13593
 
 
13594
    rc= mysql_stmt_fetch(stmt);
 
13595
    check_execute(stmt, rc);
 
13596
    if (!opt_silent && i == 0)
 
13597
      printf("Fetched row: %s\n", a);
 
13598
    rc= mysql_stmt_fetch(stmt);
 
13599
    DIE_UNLESS(rc == MYSQL_NO_DATA);
 
13600
 
 
13601
    {
 
13602
      char buff[8];
 
13603
      /* Fill in the fetch packet */
 
13604
      int4store(buff, stmt->stmt_id);
 
13605
      buff[4]= 1;                               /* prefetch rows */
 
13606
      rc= ((*mysql->methods->advanced_command)(mysql, COM_STMT_FETCH,
 
13607
                                               (uchar*) buff,
 
13608
                                               sizeof(buff), 0,0,1,NULL) ||
 
13609
           (*mysql->methods->read_query_result)(mysql));
 
13610
      DIE_UNLESS(rc);
 
13611
      if (!opt_silent && i == 0)
 
13612
        printf("Got error (as expected): %s\n", mysql_error(mysql));
 
13613
    }
 
13614
 
 
13615
    rc= mysql_stmt_execute(stmt);
 
13616
    check_execute(stmt, rc);
 
13617
 
 
13618
    rc= mysql_stmt_fetch(stmt);
 
13619
    check_execute(stmt, rc);
 
13620
    if (!opt_silent && i == 0)
 
13621
      printf("Fetched row: %s\n", a);
 
13622
 
 
13623
    rc= mysql_stmt_reset(stmt);
 
13624
    check_execute(stmt, rc);
 
13625
    rc= mysql_stmt_fetch(stmt);
 
13626
    DIE_UNLESS(rc && mysql_stmt_errno(stmt));
 
13627
    if (!opt_silent && i == 0)
 
13628
      printf("Got error (as expected): %s\n", mysql_stmt_error(stmt));
 
13629
  }
 
13630
  rc= mysql_stmt_close(stmt);
 
13631
  DIE_UNLESS(rc == 0);
 
13632
 
 
13633
  /* Test the case with a server side cursor */
 
13634
  stmt= open_cursor("select name from t1");
 
13635
 
 
13636
  mysql_stmt_bind_result(stmt, my_bind);
 
13637
 
 
13638
  for (i= 0; i < 5; i++)
 
13639
  {
 
13640
    DBUG_PRINT("loop",("i: %d", i));
 
13641
    rc= mysql_stmt_execute(stmt);
 
13642
    check_execute(stmt, rc);
 
13643
    rc= mysql_stmt_fetch(stmt);
 
13644
    check_execute(stmt, rc);
 
13645
    if (!opt_silent && i == 0)
 
13646
      printf("Fetched row: %s\n", a);
 
13647
    rc= mysql_stmt_execute(stmt);
 
13648
    check_execute(stmt, rc);
 
13649
 
 
13650
    while (! (rc= mysql_stmt_fetch(stmt)))
 
13651
    {
 
13652
      if (!opt_silent && i == 0)
 
13653
        printf("Fetched row: %s\n", a);
 
13654
    }
 
13655
    DIE_UNLESS(rc == MYSQL_NO_DATA);
 
13656
 
 
13657
    rc= mysql_stmt_execute(stmt);
 
13658
    check_execute(stmt, rc);
 
13659
 
 
13660
    rc= mysql_stmt_fetch(stmt);
 
13661
    check_execute(stmt, rc);
 
13662
    if (!opt_silent && i == 0)
 
13663
      printf("Fetched row: %s\n", a);
 
13664
 
 
13665
    rc= mysql_stmt_reset(stmt);
 
13666
    check_execute(stmt, rc);
 
13667
    rc= mysql_stmt_fetch(stmt);
 
13668
    DIE_UNLESS(rc && mysql_stmt_errno(stmt));
 
13669
    if (!opt_silent && i == 0)
 
13670
      printf("Got error (as expected): %s\n", mysql_stmt_error(stmt));
 
13671
  }
 
13672
 
 
13673
  rc= mysql_stmt_close(stmt);
 
13674
  DIE_UNLESS(rc == 0);
 
13675
 
 
13676
  rc= mysql_query(mysql, "drop table t1");
 
13677
  myquery(rc);
 
13678
  DBUG_VOID_RETURN;
 
13679
}
 
13680
 
 
13681
 
 
13682
/*
 
13683
  Error message is returned for unsupported features.
 
13684
  Test also cursors with non-default PREFETCH_ROWS
 
13685
*/
 
13686
 
 
13687
static void test_bug9643()
 
13688
{
 
13689
  MYSQL_STMT *stmt;
 
13690
  MYSQL_BIND my_bind[1];
 
13691
  int32 a;
 
13692
  int rc;
 
13693
  const char *stmt_text;
 
13694
  int num_rows= 0;
 
13695
  ulong type;
 
13696
  ulong prefetch_rows= 5;
 
13697
 
 
13698
  myheader("test_bug9643");
 
13699
 
 
13700
  mysql_query(mysql, "drop table if exists t1");
 
13701
  mysql_query(mysql, "create table t1 (id integer not null primary key)");
 
13702
  rc= mysql_query(mysql, "insert into t1 (id) values "
 
13703
                         " (1), (2), (3), (4), (5), (6), (7), (8), (9)");
 
13704
  myquery(rc);
 
13705
 
 
13706
  stmt= mysql_stmt_init(mysql);
 
13707
  /* Not implemented in 5.0 */
 
13708
  type= (ulong) CURSOR_TYPE_SCROLLABLE;
 
13709
  rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
 
13710
  DIE_UNLESS(rc);
 
13711
  if (! opt_silent)
 
13712
    printf("Got error (as expected): %s\n", mysql_stmt_error(stmt));
 
13713
 
 
13714
  type= (ulong) CURSOR_TYPE_READ_ONLY;
 
13715
  rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
 
13716
  check_execute(stmt, rc);
 
13717
  rc= mysql_stmt_attr_set(stmt, STMT_ATTR_PREFETCH_ROWS,
 
13718
                          (void*) &prefetch_rows);
 
13719
  check_execute(stmt, rc);
 
13720
  stmt_text= "select * from t1";
 
13721
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
13722
  check_execute(stmt, rc);
 
13723
 
 
13724
  bzero((char*) my_bind, sizeof(my_bind));
 
13725
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
13726
  my_bind[0].buffer= (void*) &a;
 
13727
  my_bind[0].buffer_length= sizeof(a);
 
13728
  mysql_stmt_bind_result(stmt, my_bind);
 
13729
 
 
13730
  rc= mysql_stmt_execute(stmt);
 
13731
  check_execute(stmt, rc);
 
13732
 
 
13733
  while ((rc= mysql_stmt_fetch(stmt)) == 0)
 
13734
    ++num_rows;
 
13735
  DIE_UNLESS(num_rows == 9);
 
13736
 
 
13737
  rc= mysql_stmt_close(stmt);
 
13738
  DIE_UNLESS(rc == 0);
 
13739
 
 
13740
  rc= mysql_query(mysql, "drop table t1");
 
13741
  myquery(rc);
 
13742
}
 
13743
 
 
13744
/*
 
13745
  Bug#11111: fetch from view returns wrong data
 
13746
*/
 
13747
 
 
13748
static void test_bug11111()
 
13749
{
 
13750
  MYSQL_STMT    *stmt;
 
13751
  MYSQL_BIND    my_bind[2];
 
13752
  char          buf[2][20];
 
13753
  ulong         len[2];
 
13754
  int i;
 
13755
  int rc;
 
13756
  const char *query= "SELECT DISTINCT f1,ff2 FROM v1";
 
13757
 
 
13758
  myheader("test_bug11111");
 
13759
 
 
13760
  rc= mysql_query(mysql, "drop table if exists t1, t2, v1");
 
13761
  myquery(rc);
 
13762
  rc= mysql_query(mysql, "drop view if exists t1, t2, v1");
 
13763
  myquery(rc);
 
13764
  rc= mysql_query(mysql, "create table t1 (f1 int, f2 int)");
 
13765
  myquery(rc);
 
13766
  rc= mysql_query(mysql, "create table t2 (ff1 int, ff2 int)");
 
13767
  myquery(rc);
 
13768
  rc= mysql_query(mysql, "create view v1 as select * from t1, t2 where f1=ff1");
 
13769
  myquery(rc);
 
13770
  rc= mysql_query(mysql, "insert into t1 values (1,1), (2,2), (3,3)");
 
13771
  myquery(rc);
 
13772
  rc= mysql_query(mysql, "insert into t2 values (1,1), (2,2), (3,3)");
 
13773
  myquery(rc);
 
13774
 
 
13775
  stmt= mysql_stmt_init(mysql);
 
13776
 
 
13777
  mysql_stmt_prepare(stmt, query, strlen(query));
 
13778
  mysql_stmt_execute(stmt);
 
13779
 
 
13780
  bzero((char*) my_bind, sizeof(my_bind));
 
13781
  for (i=0; i < 2; i++)
 
13782
  {
 
13783
    my_bind[i].buffer_type= MYSQL_TYPE_STRING;
 
13784
    my_bind[i].buffer= (uchar* *)&buf[i];
 
13785
    my_bind[i].buffer_length= 20;
 
13786
    my_bind[i].length= &len[i];
 
13787
  }
 
13788
 
 
13789
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
13790
  check_execute(stmt, rc);
 
13791
 
 
13792
  rc= mysql_stmt_fetch(stmt);
 
13793
  check_execute(stmt, rc);
 
13794
  if (!opt_silent)
 
13795
    printf("return: %s", buf[1]);
 
13796
  DIE_UNLESS(!strcmp(buf[1],"1"));
 
13797
  mysql_stmt_close(stmt);
 
13798
  rc= mysql_query(mysql, "drop view v1");
 
13799
  myquery(rc);
 
13800
  rc= mysql_query(mysql, "drop table t1, t2");
 
13801
  myquery(rc);
 
13802
}
 
13803
 
 
13804
/*
 
13805
  Check that proper cleanups are done for prepared statement when
 
13806
  fetching thorugh a cursor.
 
13807
*/
 
13808
 
 
13809
static void test_bug10729()
 
13810
{
 
13811
  MYSQL_STMT *stmt;
 
13812
  MYSQL_BIND my_bind[1];
 
13813
  char a[21];
 
13814
  int rc;
 
13815
  const char *stmt_text;
 
13816
  int i= 0;
 
13817
  const char *name_array[3]= { "aaa", "bbb", "ccc" };
 
13818
  ulong type;
 
13819
 
 
13820
  myheader("test_bug10729");
 
13821
 
 
13822
  mysql_query(mysql, "drop table if exists t1");
 
13823
  mysql_query(mysql, "create table t1 (id integer not null primary key,"
 
13824
                                      "name VARCHAR(20) NOT NULL)");
 
13825
  rc= mysql_query(mysql, "insert into t1 (id, name) values "
 
13826
                         "(1, 'aaa'), (2, 'bbb'), (3, 'ccc')");
 
13827
  myquery(rc);
 
13828
 
 
13829
  stmt= mysql_stmt_init(mysql);
 
13830
 
 
13831
  type= (ulong) CURSOR_TYPE_READ_ONLY;
 
13832
  rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
 
13833
  check_execute(stmt, rc);
 
13834
  stmt_text= "select name from t1";
 
13835
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
13836
  check_execute(stmt, rc);
 
13837
 
 
13838
  bzero((char*) my_bind, sizeof(my_bind));
 
13839
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
13840
  my_bind[0].buffer= (void*) a;
 
13841
  my_bind[0].buffer_length= sizeof(a);
 
13842
  mysql_stmt_bind_result(stmt, my_bind);
 
13843
 
 
13844
  for (i= 0; i < 3; i++)
 
13845
  {
 
13846
    int row_no= 0;
 
13847
    rc= mysql_stmt_execute(stmt);
 
13848
    check_execute(stmt, rc);
 
13849
    while ((rc= mysql_stmt_fetch(stmt)) == 0)
 
13850
    {
 
13851
      DIE_UNLESS(strcmp(a, name_array[row_no]) == 0);
 
13852
      if (!opt_silent)
 
13853
        printf("%d: %s\n", row_no, a);
 
13854
      ++row_no;
 
13855
    }
 
13856
    DIE_UNLESS(rc == MYSQL_NO_DATA);
 
13857
  }
 
13858
  rc= mysql_stmt_close(stmt);
 
13859
  DIE_UNLESS(rc == 0);
 
13860
 
 
13861
  rc= mysql_query(mysql, "drop table t1");
 
13862
  myquery(rc);
 
13863
}
 
13864
 
 
13865
 
 
13866
/*
 
13867
  Check that mysql_next_result works properly in case when one of
 
13868
  the statements used in a multi-statement query is erroneous
 
13869
*/
 
13870
 
 
13871
static void test_bug9992()
 
13872
{
 
13873
  MYSQL *mysql1;
 
13874
  MYSQL_RES* res ;
 
13875
  int   rc;
 
13876
 
 
13877
  myheader("test_bug9992");
 
13878
 
 
13879
  if (!opt_silent)
 
13880
    printf("Establishing a connection with option CLIENT_MULTI_STATEMENTS..\n");
 
13881
 
 
13882
  mysql1= mysql_client_init(NULL);
 
13883
 
 
13884
  if (!mysql_real_connect(mysql1, opt_host, opt_user, opt_password,
 
13885
                          opt_db ? opt_db : "test", opt_port, opt_unix_socket,
 
13886
                          CLIENT_MULTI_STATEMENTS))
 
13887
  {
 
13888
    fprintf(stderr, "Failed to connect to the database\n");
 
13889
    DIE_UNLESS(0);
 
13890
  }
 
13891
 
 
13892
 
 
13893
  /* Sic: SHOW DATABASE is incorrect syntax. */
 
13894
  rc= mysql_query(mysql1, "SHOW TABLES; SHOW DATABASE; SELECT 1;");
 
13895
 
 
13896
  if (rc)
 
13897
  {
 
13898
    fprintf(stderr, "[%d] %s\n", mysql_errno(mysql1), mysql_error(mysql1));
 
13899
    DIE_UNLESS(0);
 
13900
  }
 
13901
 
 
13902
  if (!opt_silent)
 
13903
    printf("Testing mysql_store_result/mysql_next_result..\n");
 
13904
 
 
13905
  res= mysql_store_result(mysql1);
 
13906
  DIE_UNLESS(res);
 
13907
  mysql_free_result(res);
 
13908
  rc= mysql_next_result(mysql1);
 
13909
  DIE_UNLESS(rc == 1);                         /* Got errors, as expected */
 
13910
 
 
13911
  if (!opt_silent)
 
13912
    fprintf(stdout, "Got error, as expected:\n [%d] %s\n",
 
13913
            mysql_errno(mysql1), mysql_error(mysql1));
 
13914
 
 
13915
  mysql_close(mysql1);
 
13916
}
 
13917
 
 
13918
/* Bug#10736: cursors and subqueries, memroot management */
 
13919
 
 
13920
static void test_bug10736()
 
13921
{
 
13922
  MYSQL_STMT *stmt;
 
13923
  MYSQL_BIND my_bind[1];
 
13924
  char a[21];
 
13925
  int rc;
 
13926
  const char *stmt_text;
 
13927
  int i= 0;
 
13928
  ulong type;
 
13929
 
 
13930
  myheader("test_bug10736");
 
13931
 
 
13932
  mysql_query(mysql, "drop table if exists t1");
 
13933
  mysql_query(mysql, "create table t1 (id integer not null primary key,"
 
13934
                                      "name VARCHAR(20) NOT NULL)");
 
13935
  rc= mysql_query(mysql, "insert into t1 (id, name) values "
 
13936
                         "(1, 'aaa'), (2, 'bbb'), (3, 'ccc')");
 
13937
  myquery(rc);
 
13938
 
 
13939
  stmt= mysql_stmt_init(mysql);
 
13940
 
 
13941
  type= (ulong) CURSOR_TYPE_READ_ONLY;
 
13942
  rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
 
13943
  check_execute(stmt, rc);
 
13944
  stmt_text= "select name from t1 where name=(select name from t1 where id=2)";
 
13945
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
13946
  check_execute(stmt, rc);
 
13947
 
 
13948
  bzero((char*) my_bind, sizeof(my_bind));
 
13949
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
13950
  my_bind[0].buffer= (void*) a;
 
13951
  my_bind[0].buffer_length= sizeof(a);
 
13952
  mysql_stmt_bind_result(stmt, my_bind);
 
13953
 
 
13954
  for (i= 0; i < 3; i++)
 
13955
  {
 
13956
    int row_no= 0;
 
13957
    rc= mysql_stmt_execute(stmt);
 
13958
    check_execute(stmt, rc);
 
13959
    while ((rc= mysql_stmt_fetch(stmt)) == 0)
 
13960
    {
 
13961
      if (!opt_silent)
 
13962
        printf("%d: %s\n", row_no, a);
 
13963
      ++row_no;
 
13964
    }
 
13965
    DIE_UNLESS(rc == MYSQL_NO_DATA);
 
13966
  }
 
13967
  rc= mysql_stmt_close(stmt);
 
13968
  DIE_UNLESS(rc == 0);
 
13969
 
 
13970
  rc= mysql_query(mysql, "drop table t1");
 
13971
  myquery(rc);
 
13972
}
 
13973
 
 
13974
/* Bug#10794: cursors, packets out of order */
 
13975
 
 
13976
static void test_bug10794()
 
13977
{
 
13978
  MYSQL_STMT *stmt, *stmt1;
 
13979
  MYSQL_BIND my_bind[2];
 
13980
  char a[21];
 
13981
  int id_val;
 
13982
  ulong a_len;
 
13983
  int rc;
 
13984
  const char *stmt_text;
 
13985
  int i= 0;
 
13986
  ulong type;
 
13987
 
 
13988
  myheader("test_bug10794");
 
13989
 
 
13990
  mysql_query(mysql, "drop table if exists t1");
 
13991
  mysql_query(mysql, "create table t1 (id integer not null primary key,"
 
13992
                                      "name varchar(20) not null)");
 
13993
  stmt= mysql_stmt_init(mysql);
 
13994
  stmt_text= "insert into t1 (id, name) values (?, ?)";
 
13995
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
13996
  check_execute(stmt, rc);
 
13997
  bzero((char*) my_bind, sizeof(my_bind));
 
13998
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
13999
  my_bind[0].buffer= (void*) &id_val;
 
14000
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
14001
  my_bind[1].buffer= (void*) a;
 
14002
  my_bind[1].length= &a_len;
 
14003
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
14004
  check_execute(stmt, rc);
 
14005
  for (i= 0; i < 42; i++)
 
14006
  {
 
14007
    id_val= (i+1)*10;
 
14008
    sprintf(a, "a%d", i);
 
14009
    a_len= strlen(a); /* safety against broken sprintf */
 
14010
    rc= mysql_stmt_execute(stmt);
 
14011
    check_execute(stmt, rc);
 
14012
  }
 
14013
  stmt_text= "select name from t1";
 
14014
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
14015
  type= (ulong) CURSOR_TYPE_READ_ONLY;
 
14016
  mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
14017
  stmt1= mysql_stmt_init(mysql);
 
14018
  mysql_stmt_attr_set(stmt1, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
14019
  bzero((char*) my_bind, sizeof(my_bind));
 
14020
  my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
14021
  my_bind[0].buffer= (void*) a;
 
14022
  my_bind[0].buffer_length= sizeof(a);
 
14023
  my_bind[0].length= &a_len;
 
14024
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
14025
  check_execute(stmt, rc);
 
14026
  rc= mysql_stmt_execute(stmt);
 
14027
  check_execute(stmt, rc);
 
14028
  rc= mysql_stmt_fetch(stmt);
 
14029
  check_execute(stmt, rc);
 
14030
  if (!opt_silent)
 
14031
    printf("Fetched row from stmt: %s\n", a);
 
14032
  /* Don't optimize: an attribute of the original test case */
 
14033
  mysql_stmt_free_result(stmt);
 
14034
  mysql_stmt_reset(stmt);
 
14035
  stmt_text= "select name from t1 where id=10";
 
14036
  rc= mysql_stmt_prepare(stmt1, stmt_text, strlen(stmt_text));
 
14037
  check_execute(stmt1, rc);
 
14038
  rc= mysql_stmt_bind_result(stmt1, my_bind);
 
14039
  check_execute(stmt1, rc);
 
14040
  rc= mysql_stmt_execute(stmt1);
 
14041
  while (1)
 
14042
  {
 
14043
    rc= mysql_stmt_fetch(stmt1);
 
14044
    if (rc == MYSQL_NO_DATA)
 
14045
    {
 
14046
      if (!opt_silent)
 
14047
        printf("End of data in stmt1\n");
 
14048
      break;
 
14049
    }
 
14050
    check_execute(stmt1, rc);
 
14051
    if (!opt_silent)
 
14052
      printf("Fetched row from stmt1: %s\n", a);
 
14053
  }
 
14054
  mysql_stmt_close(stmt);
 
14055
  mysql_stmt_close(stmt1);
 
14056
 
 
14057
  rc= mysql_query(mysql, "drop table t1");
 
14058
  myquery(rc);
 
14059
}
 
14060
 
 
14061
 
 
14062
/* Bug#11172: cursors, crash on a fetch from a datetime column */
 
14063
 
 
14064
static void test_bug11172()
 
14065
{
 
14066
  MYSQL_STMT *stmt;
 
14067
  MYSQL_BIND bind_in[1], bind_out[2];
 
14068
  MYSQL_TIME hired;
 
14069
  int rc;
 
14070
  const char *stmt_text;
 
14071
  int i= 0, id;
 
14072
  ulong type;
 
14073
 
 
14074
  myheader("test_bug11172");
 
14075
 
 
14076
  mysql_query(mysql, "drop table if exists t1");
 
14077
  mysql_query(mysql, "create table t1 (id integer not null primary key,"
 
14078
                                      "hired date not null)");
 
14079
  rc= mysql_query(mysql,
 
14080
                  "insert into t1 (id, hired) values (1, '1933-08-24'), "
 
14081
                  "(2, '1965-01-01'), (3, '1949-08-17'), (4, '1945-07-07'), "
 
14082
                  "(5, '1941-05-15'), (6, '1978-09-15'), (7, '1936-03-28')");
 
14083
  myquery(rc);
 
14084
  stmt= mysql_stmt_init(mysql);
 
14085
  stmt_text= "SELECT id, hired FROM t1 WHERE hired=?";
 
14086
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
14087
  check_execute(stmt, rc);
 
14088
 
 
14089
  type= (ulong) CURSOR_TYPE_READ_ONLY;
 
14090
  mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
14091
 
 
14092
  bzero((char*) bind_in, sizeof(bind_in));
 
14093
  bzero((char*) bind_out, sizeof(bind_out));
 
14094
  bzero((char*) &hired, sizeof(hired));
 
14095
  hired.year= 1965;
 
14096
  hired.month= 1;
 
14097
  hired.day= 1;
 
14098
  bind_in[0].buffer_type= MYSQL_TYPE_DATE;
 
14099
  bind_in[0].buffer= (void*) &hired;
 
14100
  bind_in[0].buffer_length= sizeof(hired);
 
14101
  bind_out[0].buffer_type= MYSQL_TYPE_LONG;
 
14102
  bind_out[0].buffer= (void*) &id;
 
14103
  bind_out[1]= bind_in[0];
 
14104
 
 
14105
  for (i= 0; i < 3; i++)
 
14106
  {
 
14107
    rc= mysql_stmt_bind_param(stmt, bind_in);
 
14108
    check_execute(stmt, rc);
 
14109
    rc= mysql_stmt_bind_result(stmt, bind_out);
 
14110
    check_execute(stmt, rc);
 
14111
    rc= mysql_stmt_execute(stmt);
 
14112
    check_execute(stmt, rc);
 
14113
    while ((rc= mysql_stmt_fetch(stmt)) == 0)
 
14114
    {
 
14115
      if (!opt_silent)
 
14116
        printf("fetched data %d:%d-%d-%d\n", id,
 
14117
               hired.year, hired.month, hired.day);
 
14118
    }
 
14119
    DIE_UNLESS(rc == MYSQL_NO_DATA);
 
14120
    if (!mysql_stmt_free_result(stmt))
 
14121
      mysql_stmt_reset(stmt);
 
14122
  }
 
14123
  mysql_stmt_close(stmt);
 
14124
  mysql_rollback(mysql);
 
14125
  mysql_rollback(mysql);
 
14126
 
 
14127
  rc= mysql_query(mysql, "drop table t1");
 
14128
  myquery(rc);
 
14129
}
 
14130
 
 
14131
 
 
14132
/* Bug#11656: cursors, crash on a fetch from a query with distinct. */
 
14133
 
 
14134
static void test_bug11656()
 
14135
{
 
14136
  MYSQL_STMT *stmt;
 
14137
  MYSQL_BIND my_bind[2];
 
14138
  int rc;
 
14139
  const char *stmt_text;
 
14140
  char buf[2][20];
 
14141
  int i= 0;
 
14142
  ulong type;
 
14143
 
 
14144
  myheader("test_bug11656");
 
14145
 
 
14146
  mysql_query(mysql, "drop table if exists t1");
 
14147
 
 
14148
  rc= mysql_query(mysql, "create table t1 ("
 
14149
                  "server varchar(40) not null, "
 
14150
                  "test_kind varchar(1) not null, "
 
14151
                  "test_id varchar(30) not null , "
 
14152
                  "primary key (server,test_kind,test_id))");
 
14153
  myquery(rc);
 
14154
 
 
14155
  stmt_text= "select distinct test_kind, test_id from t1 "
 
14156
             "where server in (?, ?)";
 
14157
  stmt= mysql_stmt_init(mysql);
 
14158
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
14159
  check_execute(stmt, rc);
 
14160
  type= (ulong) CURSOR_TYPE_READ_ONLY;
 
14161
  mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
14162
 
 
14163
  bzero((char*) my_bind, sizeof(my_bind));
 
14164
  strmov(buf[0], "pcint502_MY2");
 
14165
  strmov(buf[1], "*");
 
14166
  for (i=0; i < 2; i++)
 
14167
  {
 
14168
    my_bind[i].buffer_type= MYSQL_TYPE_STRING;
 
14169
    my_bind[i].buffer= (uchar* *)&buf[i];
 
14170
    my_bind[i].buffer_length= strlen(buf[i]);
 
14171
  }
 
14172
  mysql_stmt_bind_param(stmt, my_bind);
 
14173
 
 
14174
  rc= mysql_stmt_execute(stmt);
 
14175
  check_execute(stmt, rc);
 
14176
 
 
14177
  rc= mysql_stmt_fetch(stmt);
 
14178
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
14179
 
 
14180
  mysql_stmt_close(stmt);
 
14181
  rc= mysql_query(mysql, "drop table t1");
 
14182
  myquery(rc);
 
14183
}
 
14184
 
 
14185
 
 
14186
/*
 
14187
  Check that the server signals when NO_BACKSLASH_ESCAPES mode is in effect,
 
14188
  and mysql_real_escape_string() does the right thing as a result.
 
14189
*/
 
14190
 
 
14191
static void test_bug10214()
 
14192
{
 
14193
  int   len;
 
14194
  char  out[8];
 
14195
 
 
14196
  myheader("test_bug10214");
 
14197
 
 
14198
  DIE_UNLESS(!(mysql->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES));
 
14199
 
 
14200
  len= mysql_real_escape_string(mysql, out, "a'b\\c", 5);
 
14201
  DIE_UNLESS(memcmp(out, "a\\'b\\\\c", len) == 0);
 
14202
 
 
14203
  mysql_query(mysql, "set sql_mode='NO_BACKSLASH_ESCAPES'");
 
14204
  DIE_UNLESS(mysql->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES);
 
14205
 
 
14206
  len= mysql_real_escape_string(mysql, out, "a'b\\c", 5);
 
14207
  DIE_UNLESS(memcmp(out, "a''b\\c", len) == 0);
 
14208
 
 
14209
  mysql_query(mysql, "set sql_mode=''");
 
14210
}
 
14211
 
 
14212
static void test_client_character_set()
 
14213
{
 
14214
  MY_CHARSET_INFO cs;
 
14215
  char *csname= (char*) "utf8";
 
14216
  char *csdefault= (char*)mysql_character_set_name(mysql);
 
14217
  int rc;
 
14218
 
 
14219
  myheader("test_client_character_set");
 
14220
 
 
14221
  rc= mysql_set_character_set(mysql, csname);
 
14222
  DIE_UNLESS(rc == 0);
 
14223
 
 
14224
  mysql_get_character_set_info(mysql, &cs);
 
14225
  DIE_UNLESS(!strcmp(cs.csname, "utf8"));
 
14226
  DIE_UNLESS(!strcmp(cs.name, "utf8_general_ci"));
 
14227
  /* Restore the default character set */
 
14228
  rc= mysql_set_character_set(mysql, csdefault);
 
14229
  myquery(rc);
 
14230
}
 
14231
 
 
14232
/* Test correct max length for MEDIUMTEXT and LONGTEXT columns */
 
14233
 
 
14234
static void test_bug9735()
 
14235
{
 
14236
  MYSQL_RES *res;
 
14237
  int rc;
 
14238
 
 
14239
  myheader("test_bug9735");
 
14240
 
 
14241
  rc= mysql_query(mysql, "drop table if exists t1");
 
14242
  myquery(rc);
 
14243
  rc= mysql_query(mysql, "create table t1 (a mediumtext, b longtext) "
 
14244
                         "character set latin1");
 
14245
  myquery(rc);
 
14246
  rc= mysql_query(mysql, "select * from t1");
 
14247
  myquery(rc);
 
14248
  res= mysql_store_result(mysql);
 
14249
  verify_prepare_field(res, 0, "a", "a", MYSQL_TYPE_BLOB,
 
14250
                       "t1", "t1", current_db, (1U << 24)-1, 0);
 
14251
  verify_prepare_field(res, 1, "b", "b", MYSQL_TYPE_BLOB,
 
14252
                       "t1", "t1", current_db, ~0U, 0);
 
14253
  mysql_free_result(res);
 
14254
  rc= mysql_query(mysql, "drop table t1");
 
14255
  myquery(rc);
 
14256
}
 
14257
 
 
14258
 
 
14259
/* Bug#11183 "mysql_stmt_reset() doesn't reset information about error" */
 
14260
 
 
14261
static void test_bug11183()
 
14262
{
 
14263
  int rc;
 
14264
  MYSQL_STMT *stmt;
 
14265
  char bug_statement[]= "insert into t1 values (1)";
 
14266
 
 
14267
  myheader("test_bug11183");
 
14268
 
 
14269
  mysql_query(mysql, "drop table t1 if exists");
 
14270
  mysql_query(mysql, "create table t1 (a int)");
 
14271
 
 
14272
  stmt= mysql_stmt_init(mysql);
 
14273
  DIE_UNLESS(stmt != 0);
 
14274
 
 
14275
  rc= mysql_stmt_prepare(stmt, bug_statement, strlen(bug_statement));
 
14276
  check_execute(stmt, rc);
 
14277
 
 
14278
  rc= mysql_query(mysql, "drop table t1");
 
14279
  myquery(rc);
 
14280
 
 
14281
  /* Trying to execute statement that should fail on execute stage */
 
14282
  rc= mysql_stmt_execute(stmt);
 
14283
  DIE_UNLESS(rc);
 
14284
 
 
14285
  mysql_stmt_reset(stmt);
 
14286
  DIE_UNLESS(mysql_stmt_errno(stmt) == 0);
 
14287
 
 
14288
  mysql_query(mysql, "create table t1 (a int)");
 
14289
 
 
14290
  /* Trying to execute statement that should pass ok */
 
14291
  if (mysql_stmt_execute(stmt))
 
14292
  {
 
14293
    mysql_stmt_reset(stmt);
 
14294
    DIE_UNLESS(mysql_stmt_errno(stmt) == 0);
 
14295
  }
 
14296
 
 
14297
  mysql_stmt_close(stmt);
 
14298
 
 
14299
  rc= mysql_query(mysql, "drop table t1");
 
14300
  myquery(rc);
 
14301
}
 
14302
 
 
14303
static void test_bug11037()
 
14304
{
 
14305
  MYSQL_STMT *stmt;
 
14306
  int rc;
 
14307
  const char *stmt_text;
 
14308
 
 
14309
  myheader("test_bug11037");
 
14310
 
 
14311
  mysql_query(mysql, "drop table if exists t1");
 
14312
 
 
14313
  rc= mysql_query(mysql, "create table t1 (id int not null)");
 
14314
  myquery(rc);
 
14315
 
 
14316
  rc= mysql_query(mysql, "insert into t1 values (1)");
 
14317
  myquery(rc);
 
14318
 
 
14319
  stmt_text= "select id FROM t1";
 
14320
  stmt= mysql_stmt_init(mysql);
 
14321
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
14322
 
 
14323
  /* expected error */
 
14324
  rc = mysql_stmt_fetch(stmt);
 
14325
  DIE_UNLESS(rc==1);
 
14326
  if (!opt_silent)
 
14327
    fprintf(stdout, "Got error, as expected:\n [%d] %s\n",
 
14328
            mysql_stmt_errno(stmt), mysql_stmt_error(stmt));
 
14329
 
 
14330
  rc= mysql_stmt_execute(stmt);
 
14331
  check_execute(stmt, rc);
 
14332
 
 
14333
  rc= mysql_stmt_fetch(stmt);
 
14334
  DIE_UNLESS(rc==0);
 
14335
 
 
14336
  rc= mysql_stmt_fetch(stmt);
 
14337
  DIE_UNLESS(rc==MYSQL_NO_DATA);
 
14338
 
 
14339
  rc= mysql_stmt_fetch(stmt);
 
14340
  DIE_UNLESS(rc==MYSQL_NO_DATA);
 
14341
 
 
14342
  mysql_stmt_close(stmt);
 
14343
  rc= mysql_query(mysql, "drop table t1");
 
14344
  myquery(rc);
 
14345
}
 
14346
 
 
14347
/* Bug#10760: cursors, crash in a fetch after rollback. */
 
14348
 
 
14349
static void test_bug10760()
 
14350
{
 
14351
  MYSQL_STMT *stmt;
 
14352
  MYSQL_BIND my_bind[1];
 
14353
  int rc;
 
14354
  const char *stmt_text;
 
14355
  char id_buf[20];
 
14356
  ulong id_len;
 
14357
  int i= 0;
 
14358
  ulong type;
 
14359
 
 
14360
  myheader("test_bug10760");
 
14361
 
 
14362
  mysql_query(mysql, "drop table if exists t1, t2");
 
14363
 
 
14364
  /* create tables */
 
14365
  rc= mysql_query(mysql, "create table t1 (id integer not null primary key)"
 
14366
                         " engine=MyISAM");
 
14367
  myquery(rc);
 
14368
  for (; i < 42; ++i)
 
14369
  {
 
14370
    char buf[100];
 
14371
    sprintf(buf, "insert into t1 (id) values (%d)", i+1);
 
14372
    rc= mysql_query(mysql, buf);
 
14373
    myquery(rc);
 
14374
  }
 
14375
  mysql_autocommit(mysql, FALSE);
 
14376
  /* create statement */
 
14377
  stmt= mysql_stmt_init(mysql);
 
14378
  type= (ulong) CURSOR_TYPE_READ_ONLY;
 
14379
  mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
14380
 
 
14381
  /*
 
14382
    1: check that a deadlock within the same connection
 
14383
    is resolved and an error is returned. The deadlock is modelled
 
14384
    as follows:
 
14385
    con1: open cursor for select * from t1;
 
14386
    con1: insert into t1 (id) values (1)
 
14387
  */
 
14388
  stmt_text= "select id from t1 order by 1";
 
14389
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
14390
  check_execute(stmt, rc);
 
14391
  rc= mysql_stmt_execute(stmt);
 
14392
  check_execute(stmt, rc);
 
14393
  rc= mysql_query(mysql, "update t1 set id=id+100");
 
14394
  /*
 
14395
    If cursors are not materialized, the update will return an error;
 
14396
    we mainly test that it won't deadlock.
 
14397
  */
 
14398
  if (rc && !opt_silent)
 
14399
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
14400
  /*
 
14401
    2: check that MyISAM tables used in cursors survive
 
14402
    COMMIT/ROLLBACK.
 
14403
  */
 
14404
  rc= mysql_rollback(mysql);                  /* should not close the cursor */
 
14405
  myquery(rc);
 
14406
  rc= mysql_stmt_fetch(stmt);
 
14407
  check_execute(stmt, rc);
 
14408
 
 
14409
  /*
 
14410
    3: check that cursors to InnoDB tables are closed (for now) by
 
14411
    COMMIT/ROLLBACK.
 
14412
  */
 
14413
  if (! have_innodb)
 
14414
  {
 
14415
    if (!opt_silent)
 
14416
      printf("Testing that cursors are closed at COMMIT/ROLLBACK requires "
 
14417
             "InnoDB.\n");
 
14418
  }
 
14419
  else
 
14420
  {
 
14421
    stmt_text= "select id from t1 order by 1";
 
14422
    rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
14423
    check_execute(stmt, rc);
 
14424
 
 
14425
    rc= mysql_query(mysql, "alter table t1 engine=InnoDB");
 
14426
    myquery(rc);
 
14427
 
 
14428
    bzero(my_bind, sizeof(my_bind));
 
14429
    my_bind[0].buffer_type= MYSQL_TYPE_STRING;
 
14430
    my_bind[0].buffer= (void*) id_buf;
 
14431
    my_bind[0].buffer_length= sizeof(id_buf);
 
14432
    my_bind[0].length= &id_len;
 
14433
    check_execute(stmt, rc);
 
14434
    mysql_stmt_bind_result(stmt, my_bind);
 
14435
 
 
14436
    rc= mysql_stmt_execute(stmt);
 
14437
    rc= mysql_stmt_fetch(stmt);
 
14438
    DIE_UNLESS(rc == 0);
 
14439
    if (!opt_silent)
 
14440
      printf("Fetched row %s\n", id_buf);
 
14441
    rc= mysql_rollback(mysql);                  /* should close the cursor */
 
14442
    myquery(rc);
 
14443
#if 0
 
14444
    rc= mysql_stmt_fetch(stmt);
 
14445
    DIE_UNLESS(rc);
 
14446
    if (!opt_silent)
 
14447
      printf("Got error (as expected): %s\n", mysql_error(mysql));
 
14448
#endif
 
14449
  }
 
14450
 
 
14451
  mysql_stmt_close(stmt);
 
14452
  rc= mysql_query(mysql, "drop table t1");
 
14453
  myquery(rc);
 
14454
  mysql_autocommit(mysql, TRUE);                /* restore default */
 
14455
}
 
14456
 
 
14457
static void test_bug12001()
 
14458
{
 
14459
  MYSQL *mysql_local;
 
14460
  MYSQL_RES *result;
 
14461
  const char *query= "DROP TABLE IF EXISTS test_table;"
 
14462
                     "CREATE TABLE test_table(id INT);"
 
14463
                     "INSERT INTO test_table VALUES(10);"
 
14464
                     "UPDATE test_table SET id=20 WHERE id=10;"
 
14465
                     "SELECT * FROM test_table;"
 
14466
                     "INSERT INTO non_existent_table VALUES(11);";
 
14467
  int rc, res;
 
14468
 
 
14469
  myheader("test_bug12001");
 
14470
 
 
14471
  if (!(mysql_local= mysql_client_init(NULL)))
 
14472
  {
 
14473
    fprintf(stdout, "\n mysql_client_init() failed");
 
14474
    exit(1);
 
14475
  }
 
14476
 
 
14477
  /* Create connection that supports multi statements */
 
14478
  if (!mysql_real_connect(mysql_local, opt_host, opt_user,
 
14479
                           opt_password, current_db, opt_port,
 
14480
                           opt_unix_socket, CLIENT_MULTI_STATEMENTS |
 
14481
                           CLIENT_MULTI_RESULTS))
 
14482
  {
 
14483
    fprintf(stdout, "\n mysql_real_connect() failed");
 
14484
    exit(1);
 
14485
  }
 
14486
 
 
14487
  rc= mysql_query(mysql_local, query);
 
14488
  myquery(rc);
 
14489
 
 
14490
  do
 
14491
  {
 
14492
    if (mysql_field_count(mysql_local) &&
 
14493
        (result= mysql_use_result(mysql_local)))
 
14494
    {
 
14495
      mysql_free_result(result);
 
14496
    }
 
14497
  }
 
14498
  while (!(res= mysql_next_result(mysql_local)));
 
14499
 
 
14500
  rc= mysql_query(mysql_local, "DROP TABLE IF EXISTS test_table");
 
14501
  myquery(rc);
 
14502
 
 
14503
  mysql_close(mysql_local);
 
14504
  DIE_UNLESS(res==1);
 
14505
}
 
14506
 
 
14507
 
 
14508
/* Bug#11909: wrong metadata if fetching from two cursors */
 
14509
 
 
14510
static void test_bug11909()
 
14511
{
 
14512
  MYSQL_STMT *stmt1, *stmt2;
 
14513
  MYSQL_BIND my_bind[7];
 
14514
  int rc;
 
14515
  char firstname[20], midinit[20], lastname[20], workdept[20];
 
14516
  ulong firstname_len, midinit_len, lastname_len, workdept_len;
 
14517
  uint32 empno;
 
14518
  double salary;
 
14519
  float bonus;
 
14520
  const char *stmt_text;
 
14521
 
 
14522
  myheader("test_bug11909");
 
14523
 
 
14524
  stmt_text= "drop table if exists t1";
 
14525
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
14526
  myquery(rc);
 
14527
 
 
14528
  stmt_text= "create table t1 ("
 
14529
    "  empno int(11) not null, firstname varchar(20) not null,"
 
14530
    "  midinit varchar(20) not null, lastname varchar(20) not null,"
 
14531
    "  workdept varchar(6) not null, salary double not null,"
 
14532
    "  bonus float not null, primary key (empno)"
 
14533
    ") default charset=latin1 collate=latin1_bin";
 
14534
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
14535
  myquery(rc);
 
14536
 
 
14537
  stmt_text= "insert into t1 values "
 
14538
    "(10, 'CHRISTINE', 'I', 'HAAS',     'A00', 52750, 1000), "
 
14539
    "(20, 'MICHAEL',   'L', 'THOMPSON', 'B01', 41250, 800),"
 
14540
    "(30, 'SALLY',     'A', 'KWAN',     'C01', 38250, 800),"
 
14541
    "(50, 'JOHN',      'B', 'GEYER',    'E01', 40175, 800), "
 
14542
    "(60, 'IRVING',    'F', 'STERN',    'D11', 32250, 500)";
 
14543
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
14544
  myquery(rc);
 
14545
 
 
14546
  /* ****** Begin of trace ****** */
 
14547
 
 
14548
  stmt1= open_cursor("SELECT empno, firstname, midinit, lastname,"
 
14549
                     "workdept, salary, bonus FROM t1");
 
14550
 
 
14551
  bzero(my_bind, sizeof(my_bind));
 
14552
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
14553
  my_bind[0].buffer= (void*) &empno;
 
14554
 
 
14555
  my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
 
14556
  my_bind[1].buffer= (void*) firstname;
 
14557
  my_bind[1].buffer_length= sizeof(firstname);
 
14558
  my_bind[1].length= &firstname_len;
 
14559
 
 
14560
  my_bind[2].buffer_type= MYSQL_TYPE_VAR_STRING;
 
14561
  my_bind[2].buffer= (void*) midinit;
 
14562
  my_bind[2].buffer_length= sizeof(midinit);
 
14563
  my_bind[2].length= &midinit_len;
 
14564
 
 
14565
  my_bind[3].buffer_type= MYSQL_TYPE_VAR_STRING;
 
14566
  my_bind[3].buffer= (void*) lastname;
 
14567
  my_bind[3].buffer_length= sizeof(lastname);
 
14568
  my_bind[3].length= &lastname_len;
 
14569
 
 
14570
  my_bind[4].buffer_type= MYSQL_TYPE_VAR_STRING;
 
14571
  my_bind[4].buffer= (void*) workdept;
 
14572
  my_bind[4].buffer_length= sizeof(workdept);
 
14573
  my_bind[4].length= &workdept_len;
 
14574
 
 
14575
  my_bind[5].buffer_type= MYSQL_TYPE_DOUBLE;
 
14576
  my_bind[5].buffer= (void*) &salary;
 
14577
 
 
14578
  my_bind[6].buffer_type= MYSQL_TYPE_FLOAT;
 
14579
  my_bind[6].buffer= (void*) &bonus;
 
14580
  rc= mysql_stmt_bind_result(stmt1, my_bind);
 
14581
  check_execute(stmt1, rc);
 
14582
 
 
14583
  rc= mysql_stmt_execute(stmt1);
 
14584
  check_execute(stmt1, rc);
 
14585
 
 
14586
  rc= mysql_stmt_fetch(stmt1);
 
14587
  DIE_UNLESS(rc == 0);
 
14588
  DIE_UNLESS(empno == 10);
 
14589
  DIE_UNLESS(strcmp(firstname, "CHRISTINE") == 0);
 
14590
  DIE_UNLESS(strcmp(midinit, "I") == 0);
 
14591
  DIE_UNLESS(strcmp(lastname, "HAAS") == 0);
 
14592
  DIE_UNLESS(strcmp(workdept, "A00") == 0);
 
14593
  DIE_UNLESS(salary == (double) 52750.0);
 
14594
  DIE_UNLESS(bonus == (float) 1000.0);
 
14595
 
 
14596
  stmt2= open_cursor("SELECT empno, firstname FROM t1");
 
14597
  rc= mysql_stmt_bind_result(stmt2, my_bind);
 
14598
  check_execute(stmt2, rc);
 
14599
 
 
14600
  rc= mysql_stmt_execute(stmt2);
 
14601
  check_execute(stmt2, rc);
 
14602
 
 
14603
  rc= mysql_stmt_fetch(stmt2);
 
14604
  DIE_UNLESS(rc == 0);
 
14605
 
 
14606
  DIE_UNLESS(empno == 10);
 
14607
  DIE_UNLESS(strcmp(firstname, "CHRISTINE") == 0);
 
14608
 
 
14609
  rc= mysql_stmt_reset(stmt2);
 
14610
  check_execute(stmt2, rc);
 
14611
 
 
14612
  /* ERROR: next statement should return 0 */
 
14613
 
 
14614
  rc= mysql_stmt_fetch(stmt1);
 
14615
  DIE_UNLESS(rc == 0);
 
14616
 
 
14617
  mysql_stmt_close(stmt1);
 
14618
  mysql_stmt_close(stmt2);
 
14619
  rc= mysql_rollback(mysql);
 
14620
  myquery(rc);
 
14621
 
 
14622
  rc= mysql_query(mysql, "drop table t1");
 
14623
  myquery(rc);
 
14624
}
 
14625
 
 
14626
/* Cursors: opening a cursor to a compilicated query with ORDER BY */
 
14627
 
 
14628
static void test_bug11901()
 
14629
{
 
14630
  MYSQL_STMT *stmt;
 
14631
  MYSQL_BIND my_bind[2];
 
14632
  int rc;
 
14633
  char workdept[20];
 
14634
  ulong workdept_len;
 
14635
  uint32 empno;
 
14636
  const char *stmt_text;
 
14637
 
 
14638
  myheader("test_bug11901");
 
14639
 
 
14640
  stmt_text= "drop table if exists t1, t2";
 
14641
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
14642
  myquery(rc);
 
14643
 
 
14644
  stmt_text= "create table t1 ("
 
14645
    "  empno int(11) not null, firstname varchar(20) not null,"
 
14646
    "  midinit varchar(20) not null, lastname varchar(20) not null,"
 
14647
    "  workdept varchar(6) not null, salary double not null,"
 
14648
    "  bonus float not null, primary key (empno), "
 
14649
    " unique key (workdept, empno) "
 
14650
    ") default charset=latin1 collate=latin1_bin";
 
14651
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
14652
  myquery(rc);
 
14653
 
 
14654
  stmt_text= "insert into t1 values "
 
14655
     "(10,  'CHRISTINE', 'I', 'HAAS',      'A00', 52750, 1000),"
 
14656
     "(20,  'MICHAEL',   'L', 'THOMPSON',  'B01', 41250, 800), "
 
14657
     "(30,  'SALLY',     'A', 'KWAN',      'C01', 38250, 800), "
 
14658
     "(50,  'JOHN',      'B', 'GEYER',     'E01', 40175, 800), "
 
14659
     "(60,  'IRVING',    'F', 'STERN',     'D11', 32250, 500), "
 
14660
     "(70,  'EVA',       'D', 'PULASKI',   'D21', 36170, 700), "
 
14661
     "(90,  'EILEEN',    'W', 'HENDERSON', 'E11', 29750, 600), "
 
14662
     "(100, 'THEODORE',  'Q', 'SPENSER',   'E21', 26150, 500), "
 
14663
     "(110, 'VINCENZO',  'G', 'LUCCHESSI', 'A00', 46500, 900), "
 
14664
     "(120, 'SEAN',      '',  'O\\'CONNELL', 'A00', 29250, 600), "
 
14665
     "(130, 'DOLORES',   'M', 'QUINTANA',  'C01', 23800, 500), "
 
14666
     "(140, 'HEATHER',   'A', 'NICHOLLS',  'C01', 28420, 600), "
 
14667
     "(150, 'BRUCE',     '',  'ADAMSON',   'D11', 25280, 500), "
 
14668
     "(160, 'ELIZABETH', 'R', 'PIANKA',    'D11', 22250, 400), "
 
14669
     "(170, 'MASATOSHI', 'J', 'YOSHIMURA', 'D11', 24680, 500), "
 
14670
     "(180, 'MARILYN',   'S', 'SCOUTTEN',  'D11', 21340, 500), "
 
14671
     "(190, 'JAMES',     'H', 'WALKER',    'D11', 20450, 400), "
 
14672
     "(200, 'DAVID',     '',  'BROWN',     'D11', 27740, 600), "
 
14673
     "(210, 'WILLIAM',   'T', 'JONES',     'D11', 18270, 400), "
 
14674
     "(220, 'JENNIFER',  'K', 'LUTZ',      'D11', 29840, 600), "
 
14675
     "(230, 'JAMES',     'J', 'JEFFERSON', 'D21', 22180, 400), "
 
14676
     "(240, 'SALVATORE', 'M', 'MARINO',    'D21', 28760, 600), "
 
14677
     "(250, 'DANIEL',    'S', 'SMITH',     'D21', 19180, 400), "
 
14678
     "(260, 'SYBIL',     'P', 'JOHNSON',   'D21', 17250, 300), "
 
14679
     "(270, 'MARIA',     'L', 'PEREZ',     'D21', 27380, 500), "
 
14680
     "(280, 'ETHEL',     'R', 'SCHNEIDER', 'E11', 26250, 500), "
 
14681
     "(290, 'JOHN',      'R', 'PARKER',    'E11', 15340, 300), "
 
14682
     "(300, 'PHILIP',    'X', 'SMITH',     'E11', 17750, 400), "
 
14683
     "(310, 'MAUDE',     'F', 'SETRIGHT',  'E11', 15900, 300), "
 
14684
     "(320, 'RAMLAL',    'V', 'MEHTA',     'E21', 19950, 400), "
 
14685
     "(330, 'WING',      '',  'LEE',       'E21', 25370, 500), "
 
14686
     "(340, 'JASON',     'R', 'GOUNOT',    'E21', 23840, 500)";
 
14687
 
 
14688
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
14689
  myquery(rc);
 
14690
 
 
14691
  stmt_text= "create table t2 ("
 
14692
    " deptno varchar(6) not null, deptname varchar(20) not null,"
 
14693
    " mgrno int(11) not null, location varchar(20) not null,"
 
14694
    " admrdept varchar(6) not null, refcntd int(11) not null,"
 
14695
    " refcntu int(11) not null, primary key (deptno)"
 
14696
    ") default charset=latin1 collate=latin1_bin";
 
14697
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
14698
  myquery(rc);
 
14699
 
 
14700
  stmt_text= "insert into t2 values "
 
14701
    "('A00', 'SPIFFY COMPUTER SERV', 10, '', 'A00', 0, 0), "
 
14702
    "('B01', 'PLANNING',             20, '', 'A00', 0, 0), "
 
14703
    "('C01', 'INFORMATION CENTER',   30, '', 'A00', 0, 0), "
 
14704
    "('D01', 'DEVELOPMENT CENTER',   0,  '', 'A00', 0, 0),"
 
14705
    "('D11', 'MANUFACTURING SYSTEM', 60, '', 'D01', 0, 0), "
 
14706
    "('D21', 'ADMINISTRATION SYSTE', 70, '', 'D01', 0, 0), "
 
14707
    "('E01', 'SUPPORT SERVICES',     50, '', 'A00', 0, 0), "
 
14708
    "('E11', 'OPERATIONS',           90, '', 'E01', 0, 0), "
 
14709
    "('E21', 'SOFTWARE SUPPORT',     100,'', 'E01', 0, 0)";
 
14710
  rc= mysql_real_query(mysql, stmt_text, strlen(stmt_text));
 
14711
  myquery(rc);
 
14712
 
 
14713
  /* ****** Begin of trace ****** */
 
14714
 
 
14715
  stmt= open_cursor("select t1.empno, t1.workdept "
 
14716
                    "from (t1 left join t2 on t2.deptno = t1.workdept) "
 
14717
                    "where t2.deptno in "
 
14718
                    "   (select t2.deptno "
 
14719
                    "    from (t1 left join t2 on t2.deptno = t1.workdept) "
 
14720
                    "    where t1.empno = ?) "
 
14721
                    "order by 1");
 
14722
  bzero(my_bind, sizeof(my_bind));
 
14723
 
 
14724
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
14725
  my_bind[0].buffer= &empno;
 
14726
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
14727
  check_execute(stmt, rc);
 
14728
 
 
14729
  my_bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
 
14730
  my_bind[1].buffer= (void*) workdept;
 
14731
  my_bind[1].buffer_length= sizeof(workdept);
 
14732
  my_bind[1].length= &workdept_len;
 
14733
 
 
14734
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
14735
  check_execute(stmt, rc);
 
14736
 
 
14737
  empno= 10;
 
14738
  /* ERROR: next statement causes a server crash */
 
14739
  rc= mysql_stmt_execute(stmt);
 
14740
  check_execute(stmt, rc);
 
14741
 
 
14742
  mysql_stmt_close(stmt);
 
14743
 
 
14744
  rc= mysql_query(mysql, "drop table t1, t2");
 
14745
  myquery(rc);
 
14746
}
 
14747
 
 
14748
/* Bug#11904: mysql_stmt_attr_set CURSOR_TYPE_READ_ONLY grouping wrong result */
 
14749
 
 
14750
static void test_bug11904()
 
14751
{
 
14752
  MYSQL_STMT *stmt1;
 
14753
  int rc;
 
14754
  const char *stmt_text;
 
14755
  const ulong type= (ulong)CURSOR_TYPE_READ_ONLY;
 
14756
  MYSQL_BIND my_bind[2];
 
14757
  int country_id=0;
 
14758
  char row_data[11]= {0};
 
14759
 
 
14760
  myheader("test_bug11904");
 
14761
 
 
14762
  /* create tables */
 
14763
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS bug11904b");
 
14764
  myquery(rc);
 
14765
  rc= mysql_query(mysql, "CREATE TABLE bug11904b (id int, name char(10), primary key(id, name))");
 
14766
  myquery(rc);
 
14767
 
 
14768
  rc= mysql_query(mysql, "INSERT INTO bug11904b VALUES (1, 'sofia'), (1,'plovdiv'),"
 
14769
                          " (1,'varna'), (2,'LA'), (2,'new york'), (3,'heidelberg'),"
 
14770
                          " (3,'berlin'), (3, 'frankfurt')");
 
14771
 
 
14772
  myquery(rc);
 
14773
  mysql_commit(mysql);
 
14774
  /* create statement */
 
14775
  stmt1= mysql_stmt_init(mysql);
 
14776
  mysql_stmt_attr_set(stmt1, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
14777
 
 
14778
  stmt_text= "SELECT id, MIN(name) FROM bug11904b GROUP BY id";
 
14779
 
 
14780
  rc= mysql_stmt_prepare(stmt1, stmt_text, strlen(stmt_text));
 
14781
  check_execute(stmt1, rc);
 
14782
 
 
14783
  memset(my_bind, 0, sizeof(my_bind));
 
14784
  my_bind[0].buffer_type= MYSQL_TYPE_LONG;
 
14785
  my_bind[0].buffer=& country_id;
 
14786
  my_bind[0].buffer_length= 0;
 
14787
  my_bind[0].length= 0;
 
14788
 
 
14789
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
14790
  my_bind[1].buffer=& row_data;
 
14791
  my_bind[1].buffer_length= sizeof(row_data) - 1;
 
14792
  my_bind[1].length= 0;
 
14793
 
 
14794
  rc= mysql_stmt_bind_result(stmt1, my_bind);
 
14795
  check_execute(stmt1, rc);
 
14796
 
 
14797
  rc= mysql_stmt_execute(stmt1);
 
14798
  check_execute(stmt1, rc);
 
14799
 
 
14800
  rc= mysql_stmt_fetch(stmt1);
 
14801
  check_execute(stmt1, rc);
 
14802
  DIE_UNLESS(country_id == 1);
 
14803
  DIE_UNLESS(memcmp(row_data, "plovdiv", 7) == 0);
 
14804
 
 
14805
  rc= mysql_stmt_fetch(stmt1);
 
14806
  check_execute(stmt1, rc);
 
14807
  DIE_UNLESS(country_id == 2);
 
14808
  DIE_UNLESS(memcmp(row_data, "LA", 2) == 0);
 
14809
 
 
14810
  rc= mysql_stmt_fetch(stmt1);
 
14811
  check_execute(stmt1, rc);
 
14812
  DIE_UNLESS(country_id == 3);
 
14813
  DIE_UNLESS(memcmp(row_data, "berlin", 6) == 0);
 
14814
 
 
14815
  rc= mysql_stmt_close(stmt1);
 
14816
  check_execute(stmt1, rc);
 
14817
 
 
14818
  rc= mysql_query(mysql, "drop table bug11904b");
 
14819
  myquery(rc);
 
14820
}
 
14821
 
 
14822
 
 
14823
/* Bug#12243: multiple cursors, crash in a fetch after commit. */
 
14824
 
 
14825
static void test_bug12243()
 
14826
{
 
14827
  MYSQL_STMT *stmt1, *stmt2;
 
14828
  int rc;
 
14829
  const char *stmt_text;
 
14830
  ulong type;
 
14831
 
 
14832
  myheader("test_bug12243");
 
14833
 
 
14834
  if (! have_innodb)
 
14835
  {
 
14836
    if (!opt_silent)
 
14837
      printf("This test requires InnoDB.\n");
 
14838
    return;
 
14839
  }
 
14840
 
 
14841
  /* create tables */
 
14842
  mysql_query(mysql, "drop table if exists t1");
 
14843
  mysql_query(mysql, "create table t1 (a int) engine=InnoDB");
 
14844
  rc= mysql_query(mysql, "insert into t1 (a) values (1), (2)");
 
14845
  myquery(rc);
 
14846
  mysql_autocommit(mysql, FALSE);
 
14847
  /* create statement */
 
14848
  stmt1= mysql_stmt_init(mysql);
 
14849
  stmt2= mysql_stmt_init(mysql);
 
14850
  type= (ulong) CURSOR_TYPE_READ_ONLY;
 
14851
  mysql_stmt_attr_set(stmt1, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
14852
  mysql_stmt_attr_set(stmt2, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
14853
 
 
14854
  stmt_text= "select a from t1";
 
14855
 
 
14856
  rc= mysql_stmt_prepare(stmt1, stmt_text, strlen(stmt_text));
 
14857
  check_execute(stmt1, rc);
 
14858
  rc= mysql_stmt_execute(stmt1);
 
14859
  check_execute(stmt1, rc);
 
14860
  rc= mysql_stmt_fetch(stmt1);
 
14861
  check_execute(stmt1, rc);
 
14862
 
 
14863
  rc= mysql_stmt_prepare(stmt2, stmt_text, strlen(stmt_text));
 
14864
  check_execute(stmt2, rc);
 
14865
  rc= mysql_stmt_execute(stmt2);
 
14866
  check_execute(stmt2, rc);
 
14867
  rc= mysql_stmt_fetch(stmt2);
 
14868
  check_execute(stmt2, rc);
 
14869
 
 
14870
  rc= mysql_stmt_close(stmt1);
 
14871
  check_execute(stmt1, rc);
 
14872
  rc= mysql_commit(mysql);
 
14873
  myquery(rc);
 
14874
  rc= mysql_stmt_fetch(stmt2);
 
14875
  check_execute(stmt2, rc);
 
14876
 
 
14877
  mysql_stmt_close(stmt2);
 
14878
  rc= mysql_query(mysql, "drop table t1");
 
14879
  myquery(rc);
 
14880
  mysql_autocommit(mysql, TRUE);                /* restore default */
 
14881
}
 
14882
 
 
14883
 
 
14884
/*
 
14885
  Bug#11718: query with function, join and order by returns wrong type
 
14886
*/
 
14887
 
 
14888
static void test_bug11718()
 
14889
{
 
14890
  MYSQL_RES     *res;
 
14891
  int rc;
 
14892
  const char *query= "select str_to_date(concat(f3),'%Y%m%d') from t1,t2 "
 
14893
                     "where f1=f2 order by f1";
 
14894
 
 
14895
  myheader("test_bug11718");
 
14896
 
 
14897
  rc= mysql_query(mysql, "drop table if exists t1, t2");
 
14898
  myquery(rc);
 
14899
  rc= mysql_query(mysql, "create table t1 (f1 int)");
 
14900
  myquery(rc);
 
14901
  rc= mysql_query(mysql, "create table t2 (f2 int, f3 numeric(8))");
 
14902
  myquery(rc);
 
14903
  rc= mysql_query(mysql, "insert into t1 values (1), (2)");
 
14904
  myquery(rc);
 
14905
  rc= mysql_query(mysql, "insert into t2 values (1,20050101), (2,20050202)");
 
14906
  myquery(rc);
 
14907
  rc= mysql_query(mysql, query);
 
14908
  myquery(rc);
 
14909
  res = mysql_store_result(mysql);
 
14910
 
 
14911
  if (!opt_silent)
 
14912
    printf("return type: %s", (res->fields[0].type == MYSQL_TYPE_DATE)?"DATE":
 
14913
           "not DATE");
 
14914
  DIE_UNLESS(res->fields[0].type == MYSQL_TYPE_DATE);
 
14915
  mysql_free_result(res);
 
14916
  rc= mysql_query(mysql, "drop table t1, t2");
 
14917
  myquery(rc);
 
14918
}
 
14919
 
 
14920
 
 
14921
/*
 
14922
  Bug #12925: Bad handling of maximum values in getopt
 
14923
*/
 
14924
static void test_bug12925()
 
14925
{
 
14926
  myheader("test_bug12925");
 
14927
  if (opt_getopt_ll_test)
 
14928
    DIE_UNLESS(opt_getopt_ll_test == LL(25600*1024*1024));
 
14929
}
 
14930
 
 
14931
 
 
14932
/*
 
14933
  Bug#14210 "Simple query with > operator on large table gives server
 
14934
  crash"
 
14935
*/
 
14936
 
 
14937
static void test_bug14210()
 
14938
{
 
14939
  MYSQL_STMT *stmt;
 
14940
  int rc, i;
 
14941
  const char *stmt_text;
 
14942
  ulong type;
 
14943
 
 
14944
  myheader("test_bug14210");
 
14945
 
 
14946
  mysql_query(mysql, "drop table if exists t1");
 
14947
  /*
 
14948
    To trigger the problem the table must be InnoDB, although the problem
 
14949
    itself is not InnoDB related. In case the table is MyISAM this test
 
14950
    is harmless.
 
14951
  */
 
14952
  mysql_query(mysql, "create table t1 (a varchar(255)) engine=InnoDB");
 
14953
  rc= mysql_query(mysql, "insert into t1 (a) values (repeat('a', 256))");
 
14954
  myquery(rc);
 
14955
  rc= mysql_query(mysql, "set @@session.max_heap_table_size=16384");
 
14956
  /* Create a big enough table (more than max_heap_table_size) */
 
14957
  for (i= 0; i < 8; i++)
 
14958
  {
 
14959
    rc= mysql_query(mysql, "insert into t1 (a) select a from t1");
 
14960
    myquery(rc);
 
14961
  }
 
14962
  /* create statement */
 
14963
  stmt= mysql_stmt_init(mysql);
 
14964
  type= (ulong) CURSOR_TYPE_READ_ONLY;
 
14965
  mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
14966
 
 
14967
  stmt_text= "select a from t1";
 
14968
 
 
14969
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
14970
  check_execute(stmt, rc);
 
14971
  rc= mysql_stmt_execute(stmt);
 
14972
  while ((rc= mysql_stmt_fetch(stmt)) == 0)
 
14973
    ;
 
14974
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
14975
 
 
14976
  rc= mysql_stmt_close(stmt);
 
14977
 
 
14978
  rc= mysql_query(mysql, "drop table t1");
 
14979
  myquery(rc);
 
14980
  rc= mysql_query(mysql, "set @@session.max_heap_table_size=default");
 
14981
  myquery(rc);
 
14982
}
 
14983
 
 
14984
/* Bug#13488: wrong column metadata when fetching from cursor */
 
14985
 
 
14986
static void test_bug13488()
 
14987
{
 
14988
  MYSQL_BIND my_bind[3];
 
14989
  MYSQL_STMT *stmt1;
 
14990
  int rc, f1, f2, f3, i;
 
14991
  const ulong type= CURSOR_TYPE_READ_ONLY;
 
14992
  const char *query= "select * from t1 left join t2 on f1=f2 where f1=1";
 
14993
 
 
14994
  myheader("test_bug13488");
 
14995
 
 
14996
  rc= mysql_query(mysql, "drop table if exists t1, t2");
 
14997
  myquery(rc);
 
14998
  rc= mysql_query(mysql, "create table t1 (f1 int not null primary key)");
 
14999
  myquery(rc);
 
15000
  rc= mysql_query(mysql, "create table t2 (f2 int not null primary key, "
 
15001
                  "f3 int not null)");
 
15002
  myquery(rc);
 
15003
  rc= mysql_query(mysql, "insert into t1 values (1), (2)");
 
15004
  myquery(rc);
 
15005
  rc= mysql_query(mysql, "insert into t2 values (1,2), (2,4)");
 
15006
  myquery(rc);
 
15007
 
 
15008
  memset(my_bind, 0, sizeof(my_bind));
 
15009
  for (i= 0; i < 3; i++)
 
15010
  {
 
15011
    my_bind[i].buffer_type= MYSQL_TYPE_LONG;
 
15012
    my_bind[i].buffer_length= 4;
 
15013
    my_bind[i].length= 0;
 
15014
  }
 
15015
  my_bind[0].buffer=&f1;
 
15016
  my_bind[1].buffer=&f2;
 
15017
  my_bind[2].buffer=&f3;
 
15018
 
 
15019
  stmt1= mysql_stmt_init(mysql);
 
15020
  rc= mysql_stmt_attr_set(stmt1,STMT_ATTR_CURSOR_TYPE, (const void *)&type);
 
15021
  check_execute(stmt1, rc);
 
15022
 
 
15023
  rc= mysql_stmt_prepare(stmt1, query, strlen(query));
 
15024
  check_execute(stmt1, rc);
 
15025
 
 
15026
  rc= mysql_stmt_execute(stmt1);
 
15027
  check_execute(stmt1, rc);
 
15028
 
 
15029
  rc= mysql_stmt_bind_result(stmt1, my_bind);
 
15030
  check_execute(stmt1, rc);
 
15031
 
 
15032
  rc= mysql_stmt_fetch(stmt1);
 
15033
  check_execute(stmt1, rc);
 
15034
 
 
15035
  rc= mysql_stmt_free_result(stmt1);
 
15036
  check_execute(stmt1, rc);
 
15037
 
 
15038
  rc= mysql_stmt_reset(stmt1);
 
15039
  check_execute(stmt1, rc);
 
15040
 
 
15041
  rc= mysql_stmt_close(stmt1);
 
15042
  check_execute(stmt1, rc);
 
15043
 
 
15044
  if (!opt_silent)
 
15045
    printf("data is: %s", (f1 == 1 && f2 == 1 && f3 == 2)?"OK":
 
15046
           "wrong");
 
15047
  DIE_UNLESS(f1 == 1 && f2 == 1 && f3 == 2);
 
15048
  rc= mysql_query(mysql, "drop table t1, t2");
 
15049
  myquery(rc);
 
15050
}
 
15051
 
 
15052
/*
 
15053
  Bug#13524: warnings of a previous command are not reset when fetching
 
15054
  from a cursor.
 
15055
*/
 
15056
 
 
15057
static void test_bug13524()
 
15058
{
 
15059
  MYSQL_STMT *stmt;
 
15060
  int rc;
 
15061
  unsigned int warning_count;
 
15062
  const ulong type= CURSOR_TYPE_READ_ONLY;
 
15063
  const char *query= "select * from t1";
 
15064
 
 
15065
  myheader("test_bug13524");
 
15066
 
 
15067
  rc= mysql_query(mysql, "drop table if exists t1, t2");
 
15068
  myquery(rc);
 
15069
  rc= mysql_query(mysql, "create table t1 (a int not null primary key)");
 
15070
  myquery(rc);
 
15071
  rc= mysql_query(mysql, "insert into t1 values (1), (2), (3), (4)");
 
15072
  myquery(rc);
 
15073
 
 
15074
  stmt= mysql_stmt_init(mysql);
 
15075
  rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
15076
  check_execute(stmt, rc);
 
15077
 
 
15078
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
15079
  check_execute(stmt, rc);
 
15080
 
 
15081
  rc= mysql_stmt_execute(stmt);
 
15082
  check_execute(stmt, rc);
 
15083
 
 
15084
  rc= mysql_stmt_fetch(stmt);
 
15085
  check_execute(stmt, rc);
 
15086
 
 
15087
  warning_count= mysql_warning_count(mysql);
 
15088
  DIE_UNLESS(warning_count == 0);
 
15089
 
 
15090
  /* Check that DROP TABLE produced a warning (no such table) */
 
15091
  rc= mysql_query(mysql, "drop table if exists t2");
 
15092
  myquery(rc);
 
15093
  warning_count= mysql_warning_count(mysql);
 
15094
  DIE_UNLESS(warning_count == 1);
 
15095
 
 
15096
  /*
 
15097
    Check that fetch from a cursor cleared the warning from the previous
 
15098
    command.
 
15099
  */
 
15100
  rc= mysql_stmt_fetch(stmt);
 
15101
  check_execute(stmt, rc);
 
15102
  warning_count= mysql_warning_count(mysql);
 
15103
  DIE_UNLESS(warning_count == 0);
 
15104
 
 
15105
  /* Cleanup */
 
15106
  mysql_stmt_close(stmt);
 
15107
  rc= mysql_query(mysql, "drop table t1");
 
15108
  myquery(rc);
 
15109
}
 
15110
 
 
15111
/*
 
15112
  Bug#14845 "mysql_stmt_fetch returns MYSQL_NO_DATA when COUNT(*) is 0"
 
15113
*/
 
15114
 
 
15115
static void test_bug14845()
 
15116
{
 
15117
  MYSQL_STMT *stmt;
 
15118
  int rc;
 
15119
  const ulong type= CURSOR_TYPE_READ_ONLY;
 
15120
  const char *query= "select count(*) from t1 where 1 = 0";
 
15121
 
 
15122
  myheader("test_bug14845");
 
15123
 
 
15124
  rc= mysql_query(mysql, "drop table if exists t1");
 
15125
  myquery(rc);
 
15126
  rc= mysql_query(mysql, "create table t1 (id int(11) default null, "
 
15127
                         "name varchar(20) default null)"
 
15128
                         "engine=MyISAM DEFAULT CHARSET=utf8");
 
15129
  myquery(rc);
 
15130
  rc= mysql_query(mysql, "insert into t1 values (1,'abc'),(2,'def')");
 
15131
  myquery(rc);
 
15132
 
 
15133
  stmt= mysql_stmt_init(mysql);
 
15134
  rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (const void*) &type);
 
15135
  check_execute(stmt, rc);
 
15136
 
 
15137
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
15138
  check_execute(stmt, rc);
 
15139
 
 
15140
  rc= mysql_stmt_execute(stmt);
 
15141
  check_execute(stmt, rc);
 
15142
 
 
15143
  rc= mysql_stmt_fetch(stmt);
 
15144
  DIE_UNLESS(rc == 0);
 
15145
 
 
15146
  rc= mysql_stmt_fetch(stmt);
 
15147
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
15148
 
 
15149
  /* Cleanup */
 
15150
  mysql_stmt_close(stmt);
 
15151
  rc= mysql_query(mysql, "drop table t1");
 
15152
  myquery(rc);
 
15153
}
 
15154
 
 
15155
 
 
15156
/*
 
15157
  Bug #15510: mysql_warning_count returns 0 after mysql_stmt_fetch which
 
15158
  should warn
 
15159
*/
 
15160
static void test_bug15510()
 
15161
{
 
15162
  MYSQL_STMT *stmt;
 
15163
  int rc;
 
15164
  const char *query= "select 1 from dual where 1/0";
 
15165
 
 
15166
  myheader("test_bug15510");
 
15167
 
 
15168
  rc= mysql_query(mysql, "set @@sql_mode='ERROR_FOR_DIVISION_BY_ZERO'");
 
15169
  myquery(rc);
 
15170
 
 
15171
  stmt= mysql_stmt_init(mysql);
 
15172
 
 
15173
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
15174
  check_execute(stmt, rc);
 
15175
 
 
15176
  rc= mysql_stmt_execute(stmt);
 
15177
  check_execute(stmt, rc);
 
15178
 
 
15179
  rc= mysql_stmt_fetch(stmt);
 
15180
  DIE_UNLESS(mysql_warning_count(mysql));
 
15181
 
 
15182
  /* Cleanup */
 
15183
  mysql_stmt_close(stmt);
 
15184
  rc= mysql_query(mysql, "set @@sql_mode=''");
 
15185
  myquery(rc);
 
15186
}
 
15187
 
 
15188
 
 
15189
/* Test MYSQL_OPT_RECONNECT, Bug#15719 */
 
15190
 
 
15191
static void test_opt_reconnect()
 
15192
{
 
15193
  MYSQL *lmysql;
 
15194
  my_bool my_true= TRUE;
 
15195
 
 
15196
  myheader("test_opt_reconnect");
 
15197
 
 
15198
  if (!(lmysql= mysql_client_init(NULL)))
 
15199
  {
 
15200
    myerror("mysql_client_init() failed");
 
15201
    exit(1);
 
15202
  }
 
15203
 
 
15204
  if (!opt_silent)
 
15205
    fprintf(stdout, "reconnect before mysql_options: %d\n", lmysql->reconnect);
 
15206
  DIE_UNLESS(lmysql->reconnect == 0);
 
15207
 
 
15208
  if (mysql_options(lmysql, MYSQL_OPT_RECONNECT, &my_true))
 
15209
  {
 
15210
    myerror("mysql_options failed: unknown option MYSQL_OPT_RECONNECT\n");
 
15211
    DIE_UNLESS(0);
 
15212
  }
 
15213
 
 
15214
  /* reconnect should be 1 */
 
15215
  if (!opt_silent)
 
15216
    fprintf(stdout, "reconnect after mysql_options: %d\n", lmysql->reconnect);
 
15217
  DIE_UNLESS(lmysql->reconnect == 1);
 
15218
 
 
15219
  if (!(mysql_real_connect(lmysql, opt_host, opt_user,
 
15220
                           opt_password, current_db, opt_port,
 
15221
                           opt_unix_socket, 0)))
 
15222
  {
 
15223
    myerror("connection failed");
 
15224
    DIE_UNLESS(0);
 
15225
  }
 
15226
 
 
15227
  /* reconnect should still be 1 */
 
15228
  if (!opt_silent)
 
15229
    fprintf(stdout, "reconnect after mysql_real_connect: %d\n",
 
15230
            lmysql->reconnect);
 
15231
  DIE_UNLESS(lmysql->reconnect == 1);
 
15232
 
 
15233
  mysql_close(lmysql);
 
15234
 
 
15235
  if (!(lmysql= mysql_client_init(NULL)))
 
15236
  {
 
15237
    myerror("mysql_client_init() failed");
 
15238
    DIE_UNLESS(0);
 
15239
  }
 
15240
 
 
15241
  if (!opt_silent)
 
15242
    fprintf(stdout, "reconnect before mysql_real_connect: %d\n", lmysql->reconnect);
 
15243
  DIE_UNLESS(lmysql->reconnect == 0);
 
15244
 
 
15245
  if (!(mysql_real_connect(lmysql, opt_host, opt_user,
 
15246
                           opt_password, current_db, opt_port,
 
15247
                           opt_unix_socket, 0)))
 
15248
  {
 
15249
    myerror("connection failed");
 
15250
    DIE_UNLESS(0);
 
15251
  }
 
15252
 
 
15253
  /* reconnect should still be 0 */
 
15254
  if (!opt_silent)
 
15255
    fprintf(stdout, "reconnect after mysql_real_connect: %d\n",
 
15256
            lmysql->reconnect);
 
15257
  DIE_UNLESS(lmysql->reconnect == 0);
 
15258
 
 
15259
  mysql_close(lmysql);
 
15260
}
 
15261
 
 
15262
 
 
15263
#ifndef EMBEDDED_LIBRARY
 
15264
 
 
15265
static void test_bug12744()
 
15266
{
 
15267
  MYSQL_STMT *prep_stmt = NULL;
 
15268
  MYSQL *lmysql;
 
15269
  int rc;
 
15270
  myheader("test_bug12744");
 
15271
 
 
15272
  lmysql= mysql_client_init(NULL);
 
15273
  DIE_UNLESS(lmysql);
 
15274
 
 
15275
  if (!mysql_real_connect(lmysql, opt_host, opt_user, opt_password,
 
15276
                          current_db, opt_port, opt_unix_socket, 0))
 
15277
  {
 
15278
    fprintf(stderr, "Failed to connect to the database\n");
 
15279
    DIE_UNLESS(0);
 
15280
  }
 
15281
 
 
15282
  prep_stmt= mysql_stmt_init(lmysql);
 
15283
  rc= mysql_stmt_prepare(prep_stmt, "SELECT 1", 8);
 
15284
  DIE_UNLESS(rc == 0);
 
15285
 
 
15286
  mysql_close(lmysql);
 
15287
 
 
15288
  rc= mysql_stmt_execute(prep_stmt);
 
15289
  DIE_UNLESS(rc);
 
15290
  rc= mysql_stmt_reset(prep_stmt);
 
15291
  DIE_UNLESS(rc);
 
15292
  rc= mysql_stmt_close(prep_stmt);
 
15293
  DIE_UNLESS(rc == 0);
 
15294
}
 
15295
 
 
15296
#endif /* EMBEDDED_LIBRARY */
 
15297
 
 
15298
/* Bug #16143: mysql_stmt_sqlstate returns an empty string instead of '00000' */
 
15299
 
 
15300
static void test_bug16143()
 
15301
{
 
15302
  MYSQL_STMT *stmt;
 
15303
  myheader("test_bug16143");
 
15304
 
 
15305
  stmt= mysql_stmt_init(mysql);
 
15306
  /* Check mysql_stmt_sqlstate return "no error" */
 
15307
  DIE_UNLESS(strcmp(mysql_stmt_sqlstate(stmt), "00000") == 0);
 
15308
 
 
15309
  mysql_stmt_close(stmt);
 
15310
}
 
15311
 
 
15312
 
 
15313
/* Bug #16144: mysql_stmt_attr_get type error */
 
15314
 
 
15315
static void test_bug16144()
 
15316
{
 
15317
  const my_bool flag_orig= (my_bool) 0xde;
 
15318
  my_bool flag= flag_orig;
 
15319
  MYSQL_STMT *stmt;
 
15320
  myheader("test_bug16144");
 
15321
 
 
15322
  /* Check that attr_get returns correct data on little and big endian CPUs */
 
15323
  stmt= mysql_stmt_init(mysql);
 
15324
  mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (const void*) &flag);
 
15325
  mysql_stmt_attr_get(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*) &flag);
 
15326
  DIE_UNLESS(flag == flag_orig);
 
15327
 
 
15328
  mysql_stmt_close(stmt);
 
15329
}
 
15330
 
 
15331
/*
 
15332
  Bug #15613: "libmysqlclient API function mysql_stmt_prepare returns wrong
 
15333
  field length"
 
15334
*/
 
15335
 
 
15336
static void test_bug15613()
 
15337
{
 
15338
  MYSQL_STMT *stmt;
 
15339
  const char *stmt_text;
 
15340
  MYSQL_RES *metadata;
 
15341
  MYSQL_FIELD *field;
 
15342
  int rc;
 
15343
  myheader("test_bug15613");
 
15344
 
 
15345
  /* I. Prepare the table */
 
15346
  rc= mysql_query(mysql, "set names latin1");
 
15347
  myquery(rc);
 
15348
  mysql_query(mysql, "drop table if exists t1");
 
15349
  rc= mysql_query(mysql,
 
15350
                  "create table t1 (t text character set utf8, "
 
15351
                                   "tt tinytext character set utf8, "
 
15352
                                   "mt mediumtext character set utf8, "
 
15353
                                   "lt longtext character set utf8, "
 
15354
                                   "vl varchar(255) character set latin1,"
 
15355
                                   "vb varchar(255) character set binary,"
 
15356
                                   "vu varchar(255) character set utf8)");
 
15357
  myquery(rc);
 
15358
 
 
15359
  stmt= mysql_stmt_init(mysql);
 
15360
 
 
15361
  /* II. Check SELECT metadata */
 
15362
  stmt_text= ("select t, tt, mt, lt, vl, vb, vu from t1");
 
15363
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
15364
  metadata= mysql_stmt_result_metadata(stmt);
 
15365
  field= mysql_fetch_fields(metadata);
 
15366
  if (!opt_silent)
 
15367
  {
 
15368
    printf("Field lengths (client character set is latin1):\n"
 
15369
           "text character set utf8:\t\t%lu\n"
 
15370
           "tinytext character set utf8:\t\t%lu\n"
 
15371
           "mediumtext character set utf8:\t\t%lu\n"
 
15372
           "longtext character set utf8:\t\t%lu\n"
 
15373
           "varchar(255) character set latin1:\t%lu\n"
 
15374
           "varchar(255) character set binary:\t%lu\n"
 
15375
           "varchar(255) character set utf8:\t%lu\n",
 
15376
           field[0].length, field[1].length, field[2].length, field[3].length,
 
15377
           field[4].length, field[5].length, field[6].length);
 
15378
  }
 
15379
  DIE_UNLESS(field[0].length == 65535);
 
15380
  DIE_UNLESS(field[1].length == 255);
 
15381
  DIE_UNLESS(field[2].length == 16777215);
 
15382
  DIE_UNLESS(field[3].length == 4294967295UL);
 
15383
  DIE_UNLESS(field[4].length == 255);
 
15384
  DIE_UNLESS(field[5].length == 255);
 
15385
  DIE_UNLESS(field[6].length == 255);
 
15386
  mysql_free_result(metadata);
 
15387
  mysql_stmt_free_result(stmt);
 
15388
 
 
15389
  /* III. Cleanup */
 
15390
  rc= mysql_query(mysql, "drop table t1");
 
15391
  myquery(rc);
 
15392
  rc= mysql_query(mysql, "set names default");
 
15393
  myquery(rc);
 
15394
  mysql_stmt_close(stmt);
 
15395
}
 
15396
 
 
15397
/*
 
15398
  Bug#17667: An attacker has the opportunity to bypass query logging.
 
15399
 
 
15400
  Note! Also tests Bug#21813, where prepared statements are used to
 
15401
  run queries
 
15402
*/
 
15403
static void test_bug17667()
 
15404
{
 
15405
  int rc;
 
15406
  MYSQL_STMT *stmt;
 
15407
  enum query_type { QT_NORMAL, QT_PREPARED};
 
15408
  struct buffer_and_length {
 
15409
    enum query_type qt;
 
15410
    const char *buffer;
 
15411
    const uint length;
 
15412
  } statements[]= {
 
15413
    { QT_NORMAL, "drop table if exists bug17667", 29 },
 
15414
    { QT_NORMAL, "create table bug17667 (c varchar(20))", 37 },
 
15415
    { QT_NORMAL, "insert into bug17667 (c) values ('regular') /* NUL=\0 with comment */", 68 },
 
15416
    { QT_PREPARED,
 
15417
      "insert into bug17667 (c) values ('prepared') /* NUL=\0 with comment */", 69, },
 
15418
    { QT_NORMAL, "insert into bug17667 (c) values ('NUL=\0 in value')", 50 },
 
15419
    { QT_NORMAL, "insert into bug17667 (c) values ('5 NULs=\0\0\0\0\0')", 48 },
 
15420
    { QT_PREPARED, "insert into bug17667 (c) values ('6 NULs=\0\0\0\0\0\0')", 50 },
 
15421
    { QT_NORMAL, "/* NUL=\0 with comment */ insert into bug17667 (c) values ('encore')", 67 },
 
15422
    { QT_NORMAL, "drop table bug17667", 19 },
 
15423
    { QT_NORMAL, NULL, 0 } };
 
15424
 
 
15425
  struct buffer_and_length *statement_cursor;
 
15426
  FILE *log_file;
 
15427
  char *master_log_filename;
 
15428
 
 
15429
  myheader("test_bug17667");
 
15430
 
 
15431
  master_log_filename = (char *) malloc(strlen(opt_vardir) + strlen("/log/master.log") + 1);
 
15432
  strxmov(master_log_filename, opt_vardir, "/log/master.log", NullS);
 
15433
  if (!opt_silent)
 
15434
    printf("Opening '%s'\n", master_log_filename);
 
15435
  log_file= my_fopen(master_log_filename, (int) (O_RDONLY | O_BINARY), MYF(0));
 
15436
  free(master_log_filename);
 
15437
 
 
15438
  if (log_file == NULL)
 
15439
  {
 
15440
    if (!opt_silent)
 
15441
    {
 
15442
      printf("Could not find the log file, VARDIR/log/master.log, so "
 
15443
             "test_bug17667 is not run.\n"
 
15444
             "Run test from the mysql-test/mysql-test-run* program to set up "
 
15445
             "correct environment for this test.\n\n");
 
15446
    }
 
15447
    return;
 
15448
  }
 
15449
 
 
15450
  enable_general_log(1);
 
15451
 
 
15452
  for (statement_cursor= statements; statement_cursor->buffer != NULL;
 
15453
       statement_cursor++)
 
15454
  {
 
15455
    if (statement_cursor->qt == QT_NORMAL)
 
15456
    {
 
15457
      /* Run statement as normal query */
 
15458
      rc= mysql_real_query(mysql, statement_cursor->buffer,
 
15459
                           statement_cursor->length);
 
15460
      myquery(rc);
 
15461
    }
 
15462
    else if (statement_cursor->qt == QT_PREPARED)
 
15463
    {
 
15464
      /*
 
15465
        Run as prepared statement
 
15466
 
 
15467
        NOTE! All these queries should be in the log twice,
 
15468
        one time for prepare and one time for execute
 
15469
      */
 
15470
      stmt= mysql_stmt_init(mysql);
 
15471
 
 
15472
      rc= mysql_stmt_prepare(stmt, statement_cursor->buffer,
 
15473
                             statement_cursor->length);
 
15474
      check_execute(stmt, rc);
 
15475
 
 
15476
      rc= mysql_stmt_execute(stmt);
 
15477
      check_execute(stmt, rc);
 
15478
 
 
15479
      mysql_stmt_close(stmt);
 
15480
    }
 
15481
    else
 
15482
    {
 
15483
      DIE_UNLESS(0==1);
 
15484
    }
 
15485
  }
 
15486
 
 
15487
  /* Make sure the server has written the logs to disk before reading it */
 
15488
  rc= mysql_query(mysql, "flush logs");
 
15489
  myquery(rc);
 
15490
 
 
15491
  for (statement_cursor= statements; statement_cursor->buffer != NULL;
 
15492
       statement_cursor++)
 
15493
  {
 
15494
    int expected_hits= 1, hits= 0;
 
15495
    char line_buffer[MAX_TEST_QUERY_LENGTH*2];
 
15496
    /* more than enough room for the query and some marginalia. */
 
15497
 
 
15498
    /* Prepared statments always occurs twice in log */
 
15499
    if (statement_cursor->qt == QT_PREPARED)
 
15500
      expected_hits++;
 
15501
 
 
15502
    /* Loop until we found expected number of log entries */
 
15503
    do {
 
15504
      /* Loop until statement is found in log */
 
15505
      do {
 
15506
        memset(line_buffer, '/', MAX_TEST_QUERY_LENGTH*2);
 
15507
 
 
15508
        if(fgets(line_buffer, MAX_TEST_QUERY_LENGTH*2, log_file) == NULL)
 
15509
        {
 
15510
          /* If fgets returned NULL, it indicates either error or EOF */
 
15511
          if (feof(log_file))
 
15512
            DIE("Found EOF before all statements where found");
 
15513
 
 
15514
          fprintf(stderr, "Got error %d while reading from file\n",
 
15515
                  ferror(log_file));
 
15516
          DIE("Read error");
 
15517
        }
 
15518
 
 
15519
      } while (my_memmem(line_buffer, MAX_TEST_QUERY_LENGTH*2,
 
15520
                         statement_cursor->buffer,
 
15521
                         statement_cursor->length) == NULL);
 
15522
      hits++;
 
15523
    } while (hits < expected_hits);
 
15524
 
 
15525
    if (!opt_silent)
 
15526
      printf("Found statement starting with \"%s\"\n",
 
15527
             statement_cursor->buffer);
 
15528
  }
 
15529
 
 
15530
  restore_general_log();
 
15531
 
 
15532
  if (!opt_silent)
 
15533
    printf("success.  All queries found intact in the log.\n");
 
15534
 
 
15535
  my_fclose(log_file, MYF(0));
 
15536
}
 
15537
 
 
15538
 
 
15539
/*
 
15540
  Bug#14169: type of group_concat() result changed to blob if tmp_table was
 
15541
  used
 
15542
*/
 
15543
static void test_bug14169()
 
15544
{
 
15545
  MYSQL_STMT *stmt;
 
15546
  const char *stmt_text;
 
15547
  MYSQL_RES *res;
 
15548
  MYSQL_FIELD *field;
 
15549
  int rc;
 
15550
 
 
15551
  myheader("test_bug14169");
 
15552
 
 
15553
  rc= mysql_query(mysql, "drop table if exists t1");
 
15554
  myquery(rc);
 
15555
  rc= mysql_query(mysql, "set session group_concat_max_len=1024");
 
15556
  myquery(rc);
 
15557
  rc= mysql_query(mysql, "create table t1 (f1 int unsigned, f2 varchar(255))");
 
15558
  myquery(rc);
 
15559
  rc= mysql_query(mysql, "insert into t1 values (1,repeat('a',255)),"
 
15560
                         "(2,repeat('b',255))");
 
15561
  myquery(rc);
 
15562
  stmt= mysql_stmt_init(mysql);
 
15563
  stmt_text= "select f2,group_concat(f1) from t1 group by f2";
 
15564
  rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
15565
  myquery(rc);
 
15566
  res= mysql_stmt_result_metadata(stmt);
 
15567
  field= mysql_fetch_fields(res);
 
15568
  if (!opt_silent)
 
15569
    printf("GROUP_CONCAT() result type %i", field[1].type);
 
15570
  DIE_UNLESS(field[1].type == MYSQL_TYPE_BLOB);
 
15571
  mysql_free_result(res);
 
15572
  mysql_stmt_free_result(stmt);
 
15573
  mysql_stmt_close(stmt);
 
15574
 
 
15575
  rc= mysql_query(mysql, "drop table t1");
 
15576
  myquery(rc);
 
15577
}
 
15578
 
 
15579
/*
 
15580
   Test that mysql_insert_id() behaves as documented in our manual
 
15581
*/
 
15582
static void test_mysql_insert_id()
 
15583
{
 
15584
  my_ulonglong res;
 
15585
  int rc;
 
15586
 
 
15587
  myheader("test_mysql_insert_id");
 
15588
 
 
15589
  rc= mysql_query(mysql, "drop table if exists t1");
 
15590
  myquery(rc);
 
15591
  /* table without auto_increment column */
 
15592
  rc= mysql_query(mysql, "create table t1 (f1 int, f2 varchar(255), key(f1))");
 
15593
  myquery(rc);
 
15594
  rc= mysql_query(mysql, "insert into t1 values (1,'a')");
 
15595
  myquery(rc);
 
15596
  res= mysql_insert_id(mysql);
 
15597
  DIE_UNLESS(res == 0);
 
15598
  rc= mysql_query(mysql, "insert into t1 values (null,'b')");
 
15599
  myquery(rc);
 
15600
  res= mysql_insert_id(mysql);
 
15601
  DIE_UNLESS(res == 0);
 
15602
  rc= mysql_query(mysql, "insert into t1 select 5,'c'");
 
15603
  myquery(rc);
 
15604
  res= mysql_insert_id(mysql);
 
15605
  DIE_UNLESS(res == 0);
 
15606
 
 
15607
  /*
 
15608
    Test for bug #34889: mysql_client_test::test_mysql_insert_id test fails
 
15609
    sporadically
 
15610
  */
 
15611
  rc= mysql_query(mysql, "create table t2 (f1 int not null primary key auto_increment, f2 varchar(255))");
 
15612
  myquery(rc);
 
15613
  rc= mysql_query(mysql, "insert into t2 values (null,'b')");
 
15614
  myquery(rc);
 
15615
  rc= mysql_query(mysql, "insert into t1 select 5,'c'");
 
15616
  myquery(rc);
 
15617
  res= mysql_insert_id(mysql);
 
15618
  DIE_UNLESS(res == 0);
 
15619
  rc= mysql_query(mysql, "drop table t2");
 
15620
  myquery(rc);
 
15621
  
 
15622
  rc= mysql_query(mysql, "insert into t1 select null,'d'");
 
15623
  myquery(rc);
 
15624
  res= mysql_insert_id(mysql);
 
15625
  DIE_UNLESS(res == 0);
 
15626
  rc= mysql_query(mysql, "insert into t1 values (null,last_insert_id(300))");
 
15627
  myquery(rc);
 
15628
  res= mysql_insert_id(mysql);
 
15629
  DIE_UNLESS(res == 300);
 
15630
  rc= mysql_query(mysql, "insert into t1 select null,last_insert_id(400)");
 
15631
  myquery(rc);
 
15632
  res= mysql_insert_id(mysql);
 
15633
  /*
 
15634
    Behaviour change: old code used to return 0; but 400 is consistent
 
15635
    with INSERT VALUES, and the manual's section of mysql_insert_id() does not
 
15636
    say INSERT SELECT should be different.
 
15637
  */
 
15638
  DIE_UNLESS(res == 400);
 
15639
 
 
15640
  /* table with auto_increment column */
 
15641
  rc= mysql_query(mysql, "create table t2 (f1 int not null primary key auto_increment, f2 varchar(255))");
 
15642
  myquery(rc);
 
15643
  rc= mysql_query(mysql, "insert into t2 values (1,'a')");
 
15644
  myquery(rc);
 
15645
  res= mysql_insert_id(mysql);
 
15646
  DIE_UNLESS(res == 1);
 
15647
  /* this should not influence next INSERT if it doesn't have auto_inc */
 
15648
  rc= mysql_query(mysql, "insert into t1 values (10,'e')");
 
15649
  myquery(rc);
 
15650
  res= mysql_insert_id(mysql);
 
15651
  DIE_UNLESS(res == 0);
 
15652
 
 
15653
  rc= mysql_query(mysql, "insert into t2 values (null,'b')");
 
15654
  myquery(rc);
 
15655
  res= mysql_insert_id(mysql);
 
15656
  DIE_UNLESS(res == 2);
 
15657
  rc= mysql_query(mysql, "insert into t2 select 5,'c'");
 
15658
  myquery(rc);
 
15659
  res= mysql_insert_id(mysql);
 
15660
  /*
 
15661
    Manual says that for multirow insert this should have been 5, but does not
 
15662
    say for INSERT SELECT. This is a behaviour change: old code used to return
 
15663
    0. We try to be consistent with INSERT VALUES.
 
15664
  */
 
15665
  DIE_UNLESS(res == 5);
 
15666
  rc= mysql_query(mysql, "insert into t2 select null,'d'");
 
15667
  myquery(rc);
 
15668
  res= mysql_insert_id(mysql);
 
15669
  DIE_UNLESS(res == 6);
 
15670
  /* with more than one row */
 
15671
  rc= mysql_query(mysql, "insert into t2 values (10,'a'),(11,'b')");
 
15672
  myquery(rc);
 
15673
  res= mysql_insert_id(mysql);
 
15674
  DIE_UNLESS(res == 11);
 
15675
  rc= mysql_query(mysql, "insert into t2 select 12,'a' union select 13,'b'");
 
15676
  myquery(rc);
 
15677
  res= mysql_insert_id(mysql);
 
15678
  /*
 
15679
    Manual says that for multirow insert this should have been 13, but does
 
15680
    not say for INSERT SELECT. This is a behaviour change: old code used to
 
15681
    return 0. We try to be consistent with INSERT VALUES.
 
15682
  */
 
15683
  DIE_UNLESS(res == 13);
 
15684
  rc= mysql_query(mysql, "insert into t2 values (null,'a'),(null,'b')");
 
15685
  myquery(rc);
 
15686
  res= mysql_insert_id(mysql);
 
15687
  DIE_UNLESS(res == 14);
 
15688
  rc= mysql_query(mysql, "insert into t2 select null,'a' union select null,'b'");
 
15689
  myquery(rc);
 
15690
  res= mysql_insert_id(mysql);
 
15691
  DIE_UNLESS(res == 16);
 
15692
  rc= mysql_query(mysql, "insert into t2 select 12,'a' union select 13,'b'");
 
15693
  myquery_r(rc);
 
15694
  rc= mysql_query(mysql, "insert ignore into t2 select 12,'a' union select 13,'b'");
 
15695
  myquery(rc);
 
15696
  res= mysql_insert_id(mysql);
 
15697
  DIE_UNLESS(res == 0);
 
15698
  rc= mysql_query(mysql, "insert into t2 values (12,'a'),(13,'b')");
 
15699
  myquery_r(rc);
 
15700
  res= mysql_insert_id(mysql);
 
15701
  DIE_UNLESS(res == 0);
 
15702
  rc= mysql_query(mysql, "insert ignore into t2 values (12,'a'),(13,'b')");
 
15703
  myquery(rc);
 
15704
  res= mysql_insert_id(mysql);
 
15705
  DIE_UNLESS(res == 0);
 
15706
  /* mixing autogenerated and explicit values */
 
15707
  rc= mysql_query(mysql, "insert into t2 values (null,'e'),(12,'a'),(13,'b')");
 
15708
  myquery_r(rc);
 
15709
  rc= mysql_query(mysql, "insert into t2 values (null,'e'),(12,'a'),(13,'b'),(25,'g')");
 
15710
  myquery_r(rc);
 
15711
  rc= mysql_query(mysql, "insert into t2 values (null,last_insert_id(300))");
 
15712
  myquery(rc);
 
15713
  res= mysql_insert_id(mysql);
 
15714
  /*
 
15715
    according to the manual, this might be 20 or 300, but it looks like
 
15716
    auto_increment column takes priority over last_insert_id().
 
15717
  */
 
15718
  DIE_UNLESS(res == 20);
 
15719
  /* If first autogenerated number fails and 2nd works: */
 
15720
  rc= mysql_query(mysql, "drop table t2");
 
15721
  myquery(rc);
 
15722
  rc= mysql_query(mysql, "create table t2 (f1 int not null primary key "
 
15723
                  "auto_increment, f2 varchar(255), unique (f2))");
 
15724
  myquery(rc);
 
15725
  rc= mysql_query(mysql, "insert into t2 values (null,'e')");
 
15726
  res= mysql_insert_id(mysql);
 
15727
  DIE_UNLESS(res == 1);
 
15728
  rc= mysql_query(mysql, "insert ignore into t2 values (null,'e'),(null,'a'),(null,'e')");
 
15729
  myquery(rc);
 
15730
  res= mysql_insert_id(mysql);
 
15731
  DIE_UNLESS(res == 2);
 
15732
  /* If autogenerated fails and explicit works: */
 
15733
  rc= mysql_query(mysql, "insert ignore into t2 values (null,'e'),(12,'c'),(null,'d')");
 
15734
  myquery(rc);
 
15735
  res= mysql_insert_id(mysql);
 
15736
  /*
 
15737
    Behaviour change: old code returned 3 (first autogenerated, even if it
 
15738
    fails); we now return first successful autogenerated.
 
15739
  */
 
15740
  DIE_UNLESS(res == 13);
 
15741
  /* UPDATE may update mysql_insert_id() if it uses LAST_INSERT_ID(#) */
 
15742
  rc= mysql_query(mysql, "update t2 set f1=14 where f1=12");
 
15743
  myquery(rc);
 
15744
  res= mysql_insert_id(mysql);
 
15745
  DIE_UNLESS(res == 0);
 
15746
  rc= mysql_query(mysql, "update t2 set f1=0 where f1=14");
 
15747
  myquery(rc);
 
15748
  res= mysql_insert_id(mysql);
 
15749
  DIE_UNLESS(res == 0);
 
15750
  rc= mysql_query(mysql, "update t2 set f2=last_insert_id(372) where f1=0");
 
15751
  myquery(rc);
 
15752
  res= mysql_insert_id(mysql);
 
15753
  DIE_UNLESS(res == 372);
 
15754
  /* check that LAST_INSERT_ID() does not update mysql_insert_id(): */
 
15755
  rc= mysql_query(mysql, "insert into t2 values (null,'g')");
 
15756
  myquery(rc);
 
15757
  res= mysql_insert_id(mysql);
 
15758
  DIE_UNLESS(res == 15);
 
15759
  rc= mysql_query(mysql, "update t2 set f2=(@li:=last_insert_id()) where f1=15");
 
15760
  myquery(rc);
 
15761
  res= mysql_insert_id(mysql);
 
15762
  DIE_UNLESS(res == 0);
 
15763
  /*
 
15764
    Behaviour change: now if ON DUPLICATE KEY UPDATE updates a row,
 
15765
    mysql_insert_id() returns the id of the row, instead of not being
 
15766
    affected.
 
15767
  */
 
15768
  rc= mysql_query(mysql, "insert into t2 values (null,@li) on duplicate key "
 
15769
                  "update f2=concat('we updated ',f2)");
 
15770
  myquery(rc);
 
15771
  res= mysql_insert_id(mysql);
 
15772
  DIE_UNLESS(res == 15);
 
15773
 
 
15774
  rc= mysql_query(mysql, "drop table t1,t2");
 
15775
  myquery(rc);
 
15776
}
 
15777
 
 
15778
/*
 
15779
  Bug#20152: mysql_stmt_execute() writes to MYSQL_TYPE_DATE buffer
 
15780
*/
 
15781
 
 
15782
static void test_bug20152()
 
15783
{
 
15784
  MYSQL_BIND my_bind[1];
 
15785
  MYSQL_STMT *stmt;
 
15786
  MYSQL_TIME tm;
 
15787
  int rc;
 
15788
  const char *query= "INSERT INTO t1 (f1) VALUES (?)";
 
15789
 
 
15790
  myheader("test_bug20152");
 
15791
 
 
15792
  memset(my_bind, 0, sizeof(my_bind));
 
15793
  my_bind[0].buffer_type= MYSQL_TYPE_DATE;
 
15794
  my_bind[0].buffer= (void*)&tm;
 
15795
 
 
15796
  tm.year = 2006;
 
15797
  tm.month = 6;
 
15798
  tm.day = 18;
 
15799
  tm.hour = 14;
 
15800
  tm.minute = 9;
 
15801
  tm.second = 42;
 
15802
 
 
15803
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
15804
  myquery(rc);
 
15805
  rc= mysql_query(mysql, "CREATE TABLE t1 (f1 DATE)");
 
15806
  myquery(rc);
 
15807
 
 
15808
  stmt= mysql_stmt_init(mysql);
 
15809
  rc= mysql_stmt_prepare(stmt, query, strlen(query));
 
15810
  check_execute(stmt, rc);
 
15811
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
15812
  check_execute(stmt, rc);
 
15813
  rc= mysql_stmt_execute(stmt);
 
15814
  check_execute(stmt, rc);
 
15815
  rc= mysql_stmt_close(stmt);
 
15816
  check_execute(stmt, rc);
 
15817
  rc= mysql_query(mysql, "DROP TABLE t1");
 
15818
  myquery(rc);
 
15819
 
 
15820
  if (tm.hour == 14 && tm.minute == 9 && tm.second == 42) {
 
15821
    if (!opt_silent)
 
15822
      printf("OK!");
 
15823
  } else {
 
15824
    printf("[14:09:42] != [%02d:%02d:%02d]\n", tm.hour, tm.minute, tm.second);
 
15825
    DIE_UNLESS(0==1);
 
15826
  }
 
15827
}
 
15828
 
 
15829
/* Bug#15752 "Lost connection to MySQL server when calling a SP from C API" */
 
15830
 
 
15831
static void test_bug15752()
 
15832
{
 
15833
  MYSQL mysql_local;
 
15834
  int rc, i;
 
15835
  const int ITERATION_COUNT= 100;
 
15836
  const char *query= "CALL p1()";
 
15837
 
 
15838
  myheader("test_bug15752");
 
15839
 
 
15840
  rc= mysql_query(mysql, "drop procedure if exists p1");
 
15841
  myquery(rc);
 
15842
  rc= mysql_query(mysql, "create procedure p1() select 1");
 
15843
  myquery(rc);
 
15844
 
 
15845
  mysql_client_init(&mysql_local);
 
15846
  if (! mysql_real_connect(&mysql_local, opt_host, opt_user,
 
15847
                           opt_password, current_db, opt_port,
 
15848
                           opt_unix_socket,
 
15849
                           CLIENT_MULTI_STATEMENTS|CLIENT_MULTI_RESULTS))
 
15850
  {
 
15851
    printf("Unable connect to MySQL server: %s\n", mysql_error(&mysql_local));
 
15852
    DIE_UNLESS(0);
 
15853
  }
 
15854
  rc= mysql_real_query(&mysql_local, query, strlen(query));
 
15855
  myquery(rc);
 
15856
  mysql_free_result(mysql_store_result(&mysql_local));
 
15857
 
 
15858
  rc= mysql_real_query(&mysql_local, query, strlen(query));
 
15859
  DIE_UNLESS(rc && mysql_errno(&mysql_local) == CR_COMMANDS_OUT_OF_SYNC);
 
15860
 
 
15861
  if (! opt_silent)
 
15862
    printf("Got error (as expected): %s\n", mysql_error(&mysql_local));
 
15863
 
 
15864
  /* Check some other commands too */
 
15865
 
 
15866
  DIE_UNLESS(mysql_next_result(&mysql_local) == 0);
 
15867
  mysql_free_result(mysql_store_result(&mysql_local));
 
15868
  DIE_UNLESS(mysql_next_result(&mysql_local) == -1);
 
15869
 
 
15870
  /* The second problem is not reproducible: add the test case */
 
15871
  for (i = 0; i < ITERATION_COUNT; i++)
 
15872
  {
 
15873
    if (mysql_real_query(&mysql_local, query, strlen(query)))
 
15874
    {
 
15875
      printf("\ni=%d %s failed: %s\n", i, query, mysql_error(&mysql_local));
 
15876
      break;
 
15877
    }
 
15878
    mysql_free_result(mysql_store_result(&mysql_local));
 
15879
    DIE_UNLESS(mysql_next_result(&mysql_local) == 0);
 
15880
    mysql_free_result(mysql_store_result(&mysql_local));
 
15881
    DIE_UNLESS(mysql_next_result(&mysql_local) == -1);
 
15882
 
 
15883
  }
 
15884
  mysql_close(&mysql_local);
 
15885
  rc= mysql_query(mysql, "drop procedure p1");
 
15886
  myquery(rc);
 
15887
}
 
15888
 
 
15889
/*
 
15890
  Bug#21206: memory corruption when too many cursors are opened at once
 
15891
 
 
15892
  Memory corruption happens when more than 1024 cursors are open
 
15893
  simultaneously.
 
15894
*/
 
15895
static void test_bug21206()
 
15896
{
 
15897
  const size_t cursor_count= 1025;
 
15898
 
 
15899
  const char *create_table[]=
 
15900
  {
 
15901
    "DROP TABLE IF EXISTS t1",
 
15902
    "CREATE TABLE t1 (i INT)",
 
15903
    "INSERT INTO t1 VALUES (1), (2), (3)"
 
15904
  };
 
15905
  const char *query= "SELECT * FROM t1";
 
15906
 
 
15907
  Stmt_fetch *fetch_array=
 
15908
    (Stmt_fetch*) calloc(cursor_count, sizeof(Stmt_fetch));
 
15909
 
 
15910
  Stmt_fetch *fetch;
 
15911
 
 
15912
  DBUG_ENTER("test_bug21206");
 
15913
  myheader("test_bug21206");
 
15914
 
 
15915
  fill_tables(create_table, sizeof(create_table) / sizeof(*create_table));
 
15916
 
 
15917
  for (fetch= fetch_array; fetch < fetch_array + cursor_count; ++fetch)
 
15918
  {
 
15919
    /* Init will exit(1) in case of error */
 
15920
    stmt_fetch_init(fetch, fetch - fetch_array, query);
 
15921
  }
 
15922
 
 
15923
  for (fetch= fetch_array; fetch < fetch_array + cursor_count; ++fetch)
 
15924
    stmt_fetch_close(fetch);
 
15925
 
 
15926
  free(fetch_array);
 
15927
 
 
15928
  DBUG_VOID_RETURN;
 
15929
}
 
15930
 
 
15931
/*
 
15932
  Ensure we execute the status code while testing
 
15933
*/
 
15934
 
 
15935
static void test_status()
 
15936
{
 
15937
  const char *status;
 
15938
  DBUG_ENTER("test_status");
 
15939
  myheader("test_status");
 
15940
 
 
15941
  if (!(status= mysql_stat(mysql)))
 
15942
  {
 
15943
    myerror("mysql_stat failed");                 /* purecov: inspected */
 
15944
    die(__FILE__, __LINE__, "mysql_stat failed"); /* purecov: inspected */
 
15945
  }
 
15946
  DBUG_VOID_RETURN;
 
15947
}
 
15948
 
 
15949
/*
 
15950
  Bug#21726: Incorrect result with multiple invocations of
 
15951
  LAST_INSERT_ID
 
15952
 
 
15953
  Test that client gets updated value of insert_id on UPDATE that uses
 
15954
  LAST_INSERT_ID(expr).
 
15955
  select_query added to test for bug
 
15956
    #26921 Problem in mysql_insert_id() Embedded C API function
 
15957
*/
 
15958
static void test_bug21726()
 
15959
{
 
15960
  const char *create_table[]=
 
15961
  {
 
15962
    "DROP TABLE IF EXISTS t1",
 
15963
    "CREATE TABLE t1 (i INT)",
 
15964
    "INSERT INTO t1 VALUES (1)",
 
15965
  };
 
15966
  const char *update_query= "UPDATE t1 SET i= LAST_INSERT_ID(i + 1)";
 
15967
  int rc;
 
15968
  my_ulonglong insert_id;
 
15969
  const char *select_query= "SELECT * FROM t1";
 
15970
  MYSQL_RES  *result;
 
15971
 
 
15972
  DBUG_ENTER("test_bug21726");
 
15973
  myheader("test_bug21726");
 
15974
 
 
15975
  fill_tables(create_table, sizeof(create_table) / sizeof(*create_table));
 
15976
 
 
15977
  rc= mysql_query(mysql, update_query);
 
15978
  myquery(rc);
 
15979
  insert_id= mysql_insert_id(mysql);
 
15980
  DIE_UNLESS(insert_id == 2);
 
15981
 
 
15982
  rc= mysql_query(mysql, update_query);
 
15983
  myquery(rc);
 
15984
  insert_id= mysql_insert_id(mysql);
 
15985
  DIE_UNLESS(insert_id == 3);
 
15986
 
 
15987
  rc= mysql_query(mysql, select_query);
 
15988
  myquery(rc);
 
15989
  insert_id= mysql_insert_id(mysql);
 
15990
  DIE_UNLESS(insert_id == 3);
 
15991
  result= mysql_store_result(mysql);
 
15992
  mysql_free_result(result);
 
15993
 
 
15994
  DBUG_VOID_RETURN;
 
15995
}
 
15996
 
 
15997
 
 
15998
/*
 
15999
  BUG#23383: mysql_affected_rows() returns different values than
 
16000
  mysql_stmt_affected_rows()
 
16001
 
 
16002
  Test that both mysql_affected_rows() and mysql_stmt_affected_rows()
 
16003
  return -1 on error, 0 when no rows were affected, and (positive) row
 
16004
  count when some rows were affected.
 
16005
*/
 
16006
static void test_bug23383()
 
16007
{
 
16008
  const char *insert_query= "INSERT INTO t1 VALUES (1), (2)";
 
16009
  const char *update_query= "UPDATE t1 SET i= 4 WHERE i = 3";
 
16010
  MYSQL_STMT *stmt;
 
16011
  my_ulonglong row_count;
 
16012
  int rc;
 
16013
 
 
16014
  DBUG_ENTER("test_bug23383");
 
16015
  myheader("test_bug23383");
 
16016
 
 
16017
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
16018
  myquery(rc);
 
16019
 
 
16020
  rc= mysql_query(mysql, "CREATE TABLE t1 (i INT UNIQUE)");
 
16021
  myquery(rc);
 
16022
 
 
16023
  rc= mysql_query(mysql, insert_query);
 
16024
  myquery(rc);
 
16025
  row_count= mysql_affected_rows(mysql);
 
16026
  DIE_UNLESS(row_count == 2);
 
16027
 
 
16028
  rc= mysql_query(mysql, insert_query);
 
16029
  DIE_UNLESS(rc != 0);
 
16030
  row_count= mysql_affected_rows(mysql);
 
16031
  DIE_UNLESS(row_count == (my_ulonglong)-1);
 
16032
 
 
16033
  rc= mysql_query(mysql, update_query);
 
16034
  myquery(rc);
 
16035
  row_count= mysql_affected_rows(mysql);
 
16036
  DIE_UNLESS(row_count == 0);
 
16037
 
 
16038
  rc= mysql_query(mysql, "DELETE FROM t1");
 
16039
  myquery(rc);
 
16040
 
 
16041
  stmt= mysql_stmt_init(mysql);
 
16042
  DIE_UNLESS(stmt != 0);
 
16043
 
 
16044
  rc= mysql_stmt_prepare(stmt, insert_query, strlen(insert_query));
 
16045
  check_execute(stmt, rc);
 
16046
 
 
16047
  rc= mysql_stmt_execute(stmt);
 
16048
  check_execute(stmt, rc);
 
16049
  row_count= mysql_stmt_affected_rows(stmt);
 
16050
  DIE_UNLESS(row_count == 2);
 
16051
 
 
16052
  rc= mysql_stmt_execute(stmt);
 
16053
  DIE_UNLESS(rc != 0);
 
16054
  row_count= mysql_stmt_affected_rows(stmt);
 
16055
  DIE_UNLESS(row_count == (my_ulonglong)-1);
 
16056
 
 
16057
  rc= mysql_stmt_prepare(stmt, update_query, strlen(update_query));
 
16058
  check_execute(stmt, rc);
 
16059
 
 
16060
  rc= mysql_stmt_execute(stmt);
 
16061
  check_execute(stmt, rc);
 
16062
  row_count= mysql_stmt_affected_rows(stmt);
 
16063
  DIE_UNLESS(row_count == 0);
 
16064
 
 
16065
  rc= mysql_stmt_close(stmt);
 
16066
  check_execute(stmt, rc);
 
16067
 
 
16068
  rc= mysql_query(mysql, "DROP TABLE t1");
 
16069
  myquery(rc);
 
16070
 
 
16071
  DBUG_VOID_RETURN;
 
16072
}
 
16073
 
 
16074
 
 
16075
/*
 
16076
  BUG#21635: MYSQL_FIELD struct's member strings seem to misbehave for
 
16077
  expression cols
 
16078
 
 
16079
  Check that for MIN(), MAX(), COUNT() only MYSQL_FIELD::name is set
 
16080
  to either expression or its alias, and db, org_table, table,
 
16081
  org_name fields are empty strings.
 
16082
*/
 
16083
static void test_bug21635()
 
16084
{
 
16085
  const char *expr[]=
 
16086
  {
 
16087
    "MIN(i)", "MIN(i)",
 
16088
    "MIN(i) AS A1", "A1",
 
16089
    "MAX(i)", "MAX(i)",
 
16090
    "MAX(i) AS A2", "A2",
 
16091
    "COUNT(i)", "COUNT(i)",
 
16092
    "COUNT(i) AS A3", "A3",
 
16093
  };
 
16094
  char query[MAX_TEST_QUERY_LENGTH];
 
16095
  char *query_end;
 
16096
  MYSQL_RES *result;
 
16097
  MYSQL_FIELD *field;
 
16098
  unsigned int field_count, i, j;
 
16099
  int rc;
 
16100
 
 
16101
  DBUG_ENTER("test_bug21635");
 
16102
  myheader("test_bug21635");
 
16103
 
 
16104
  query_end= strxmov(query, "SELECT ", NullS);
 
16105
  for (i= 0; i < sizeof(expr) / sizeof(*expr) / 2; ++i)
 
16106
    query_end= strxmov(query_end, expr[i * 2], ", ", NullS);
 
16107
  query_end= strxmov(query_end - 2, " FROM t1 GROUP BY i", NullS);
 
16108
  DIE_UNLESS(query_end - query < MAX_TEST_QUERY_LENGTH);
 
16109
 
 
16110
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
16111
  myquery(rc);
 
16112
  rc= mysql_query(mysql, "CREATE TABLE t1 (i INT)");
 
16113
  myquery(rc);
 
16114
  /*
 
16115
    We need this loop to ensure correct behavior with both constant and
 
16116
    non-constant tables.
 
16117
  */
 
16118
  for (j= 0; j < 2 ; j++)
 
16119
  {
 
16120
    rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1)");
 
16121
    myquery(rc);
 
16122
 
 
16123
    rc= mysql_real_query(mysql, query, query_end - query);
 
16124
    myquery(rc);
 
16125
 
 
16126
    result= mysql_use_result(mysql);
 
16127
    DIE_UNLESS(result);
 
16128
 
 
16129
  field_count= mysql_field_count(mysql);
 
16130
  for (i= 0; i < field_count; ++i)
 
16131
  {
 
16132
    field= mysql_fetch_field_direct(result, i);
 
16133
    if (!opt_silent)
 
16134
      if (!opt_silent)
 
16135
        printf("%s -> %s ... ", expr[i * 2], field->name);
 
16136
    fflush(stdout);
 
16137
    DIE_UNLESS(field->db[0] == 0 && field->org_table[0] == 0 &&
 
16138
               field->table[0] == 0 && field->org_name[0] == 0);
 
16139
    DIE_UNLESS(strcmp(field->name, expr[i * 2 + 1]) == 0);
 
16140
    if (!opt_silent)
 
16141
      if (!opt_silent)
 
16142
        puts("OK");
 
16143
  }
 
16144
 
 
16145
    mysql_free_result(result);
 
16146
  }
 
16147
  rc= mysql_query(mysql, "DROP TABLE t1");
 
16148
  myquery(rc);
 
16149
 
 
16150
  DBUG_VOID_RETURN;
 
16151
}
 
16152
 
 
16153
/*
 
16154
  Bug#24179 "select b into $var" fails with --cursor_protocol"
 
16155
  The failure is correct, check that the returned message is meaningful.
 
16156
*/
 
16157
 
 
16158
static void test_bug24179()
 
16159
{
 
16160
  int rc;
 
16161
  MYSQL_STMT *stmt;
 
16162
 
 
16163
  DBUG_ENTER("test_bug24179");
 
16164
  myheader("test_bug24179");
 
16165
 
 
16166
  stmt= open_cursor("select 1 into @a");
 
16167
  rc= mysql_stmt_execute(stmt);
 
16168
  DIE_UNLESS(rc);
 
16169
  if (!opt_silent)
 
16170
  {
 
16171
    printf("Got error (as expected): %d %s\n",
 
16172
           mysql_stmt_errno(stmt),
 
16173
           mysql_stmt_error(stmt));
 
16174
  }
 
16175
  DIE_UNLESS(mysql_stmt_errno(stmt) == 1323);
 
16176
  mysql_stmt_close(stmt);
 
16177
 
 
16178
  DBUG_VOID_RETURN;
 
16179
}
 
16180
 
 
16181
 
 
16182
/**
 
16183
  Bug#32265 Server returns different metadata if prepared statement is used
 
16184
*/
 
16185
 
 
16186
static void test_bug32265()
 
16187
{
 
16188
  int rc;
 
16189
  MYSQL_STMT *stmt;
 
16190
  MYSQL_FIELD *field;
 
16191
  MYSQL_RES *metadata;
 
16192
 
 
16193
  DBUG_ENTER("test_bug32265");
 
16194
  myheader("test_bug32265");
 
16195
 
 
16196
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
16197
  myquery(rc);
 
16198
  rc= mysql_query(mysql, "CREATE  TABLE t1 (a INTEGER)");
 
16199
  myquery(rc);
 
16200
  rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1)");
 
16201
  myquery(rc);
 
16202
  rc= mysql_query(mysql, "CREATE VIEW v1 AS SELECT * FROM t1");
 
16203
  myquery(rc);
 
16204
 
 
16205
  stmt= open_cursor("SELECT * FROM t1");
 
16206
  rc= mysql_stmt_execute(stmt);
 
16207
  check_execute(stmt, rc);
 
16208
 
 
16209
  metadata= mysql_stmt_result_metadata(stmt);
 
16210
  field= mysql_fetch_field(metadata);
 
16211
  DIE_UNLESS(field);
 
16212
  DIE_UNLESS(strcmp(field->table, "t1") == 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 a '' FROM t1 ``");
 
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, "") == 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");
 
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, "v1") == 0);
 
16250
  DIE_UNLESS(strcmp(field->db, "client_test_db") == 0);
 
16251
  mysql_free_result(metadata);
 
16252
  mysql_stmt_close(stmt);
 
16253
 
 
16254
  stmt= open_cursor("SELECT * FROM v1 /* SIC */ GROUP BY 1");
 
16255
  rc= mysql_stmt_execute(stmt);
 
16256
  check_execute(stmt, rc);
 
16257
 
 
16258
  metadata= mysql_stmt_result_metadata(stmt);
 
16259
  field= mysql_fetch_field(metadata);
 
16260
  DIE_UNLESS(strcmp(field->table, "v1") == 0);
 
16261
  DIE_UNLESS(strcmp(field->org_table, "v1") == 0);
 
16262
  DIE_UNLESS(strcmp(field->db, "client_test_db") == 0);
 
16263
  mysql_free_result(metadata);
 
16264
  mysql_stmt_close(stmt);
 
16265
 
 
16266
  rc= mysql_query(mysql, "DROP VIEW v1");
 
16267
  myquery(rc);
 
16268
  rc= mysql_query(mysql, "DROP TABLE t1");
 
16269
  myquery(rc);
 
16270
 
 
16271
  DBUG_VOID_RETURN;
 
16272
}
 
16273
 
 
16274
/*
 
16275
  Bug#28075 "COM_DEBUG crashes mysqld"
 
16276
*/
 
16277
 
 
16278
static void test_bug28075()
 
16279
{
 
16280
  int rc;
 
16281
 
 
16282
  DBUG_ENTER("test_bug28075");
 
16283
  myheader("test_bug28075");
 
16284
 
 
16285
  rc= mysql_dump_debug_info(mysql);
 
16286
  DIE_UNLESS(rc == 0);
 
16287
 
 
16288
  rc= mysql_ping(mysql);
 
16289
  DIE_UNLESS(rc == 0);
 
16290
 
 
16291
  DBUG_VOID_RETURN;
 
16292
}
 
16293
 
 
16294
 
 
16295
/*
 
16296
  Bug#27876 (SF with cyrillic variable name fails during execution (regression))
 
16297
*/
 
16298
 
 
16299
static void test_bug27876()
 
16300
{
 
16301
  int rc;
 
16302
  MYSQL_RES *result;
 
16303
 
 
16304
  uchar utf8_func[] =
 
16305
  {
 
16306
    0xd1, 0x84, 0xd1, 0x83, 0xd0, 0xbd, 0xd0, 0xba,
 
16307
    0xd1, 0x86, 0xd0, 0xb8, 0xd0, 0xb9, 0xd0, 0xba,
 
16308
    0xd0, 0xb0,
 
16309
    0x00
 
16310
  };
 
16311
 
 
16312
  uchar utf8_param[] =
 
16313
  {
 
16314
    0xd0, 0xbf, 0xd0, 0xb0, 0xd1, 0x80, 0xd0, 0xb0,
 
16315
    0xd0, 0xbc, 0xd0, 0xb5, 0xd1, 0x82, 0xd1, 0x8a,
 
16316
    0xd1, 0x80, 0x5f, 0xd0, 0xb2, 0xd0, 0xb5, 0xd1,
 
16317
    0x80, 0xd1, 0x81, 0xd0, 0xb8, 0xd1, 0x8f,
 
16318
    0x00
 
16319
  };
 
16320
 
 
16321
  char query[500];
 
16322
 
 
16323
  DBUG_ENTER("test_bug27876");
 
16324
  myheader("test_bug27876");
 
16325
 
 
16326
  rc= mysql_query(mysql, "set names utf8");
 
16327
  myquery(rc);
 
16328
 
 
16329
  rc= mysql_query(mysql, "select version()");
 
16330
  myquery(rc);
 
16331
  result= mysql_store_result(mysql);
 
16332
  mytest(result);
 
16333
  mysql_free_result(result);
 
16334
 
 
16335
  sprintf(query, "DROP FUNCTION IF EXISTS %s", (char*) utf8_func);
 
16336
  rc= mysql_query(mysql, query);
 
16337
  myquery(rc);
 
16338
 
 
16339
  sprintf(query,
 
16340
          "CREATE FUNCTION %s( %s VARCHAR(25))"
 
16341
          " RETURNS VARCHAR(25) DETERMINISTIC RETURN %s",
 
16342
          (char*) utf8_func, (char*) utf8_param, (char*) utf8_param);
 
16343
  rc= mysql_query(mysql, query);
 
16344
  myquery(rc);
 
16345
  sprintf(query, "SELECT %s(VERSION())", (char*) utf8_func);
 
16346
  rc= mysql_query(mysql, query);
 
16347
  myquery(rc);
 
16348
  result= mysql_store_result(mysql);
 
16349
  mytest(result);
 
16350
  mysql_free_result(result);
 
16351
 
 
16352
  sprintf(query, "DROP FUNCTION %s", (char*) utf8_func);
 
16353
  rc= mysql_query(mysql, query);
 
16354
  myquery(rc);
 
16355
 
 
16356
  rc= mysql_query(mysql, "set names default");
 
16357
  myquery(rc);
 
16358
}
 
16359
 
 
16360
 
 
16361
/*
 
16362
  Bug#28505: mysql_affected_rows() returns wrong value if CLIENT_FOUND_ROWS
 
16363
  flag is set.
 
16364
*/
 
16365
 
 
16366
static void test_bug28505()
 
16367
{
 
16368
  my_ulonglong res;
 
16369
 
 
16370
  myquery(mysql_query(mysql, "drop table if exists t1"));
 
16371
  myquery(mysql_query(mysql, "create table t1(f1 int primary key)"));
 
16372
  myquery(mysql_query(mysql, "insert into t1 values(1)"));
 
16373
  myquery(mysql_query(mysql,
 
16374
                  "insert into t1 values(1) on duplicate key update f1=1"));
 
16375
  res= mysql_affected_rows(mysql);
 
16376
  DIE_UNLESS(!res);
 
16377
  myquery(mysql_query(mysql, "drop table t1"));
 
16378
}
 
16379
 
 
16380
 
 
16381
/*
 
16382
  Bug#28934: server crash when receiving malformed com_execute packets
 
16383
*/
 
16384
 
 
16385
static void test_bug28934()
 
16386
{
 
16387
  my_bool error= 0;
 
16388
  MYSQL_BIND bind[5];
 
16389
  MYSQL_STMT *stmt;
 
16390
  int cnt;
 
16391
 
 
16392
  myquery(mysql_query(mysql, "drop table if exists t1"));
 
16393
  myquery(mysql_query(mysql, "create table t1(id int)"));
 
16394
 
 
16395
  myquery(mysql_query(mysql, "insert into t1 values(1),(2),(3),(4),(5)"));
 
16396
  stmt= mysql_simple_prepare(mysql,"select * from t1 where id in(?,?,?,?,?)");
 
16397
  check_stmt(stmt);
 
16398
 
 
16399
  memset (&bind, 0, sizeof (bind));
 
16400
  for (cnt= 0; cnt < 5; cnt++)
 
16401
  {
 
16402
    bind[cnt].buffer_type= MYSQL_TYPE_LONG;
 
16403
    bind[cnt].buffer= (char*)&cnt;
 
16404
    bind[cnt].buffer_length= 0;
 
16405
  }
 
16406
  myquery(mysql_stmt_bind_param(stmt, bind));
 
16407
 
 
16408
  stmt->param_count=2;
 
16409
  error= mysql_stmt_execute(stmt);
 
16410
  DIE_UNLESS(error != 0);
 
16411
  myerror(NULL);
 
16412
  mysql_stmt_close(stmt);
 
16413
 
 
16414
  myquery(mysql_query(mysql, "drop table t1"));
 
16415
}
 
16416
 
 
16417
/*
 
16418
  Test mysql_change_user() C API and COM_CHANGE_USER
 
16419
*/
 
16420
 
 
16421
static void test_change_user()
 
16422
{
 
16423
  char buff[256];
 
16424
  const char *user_pw= "mysqltest_pw";
 
16425
  const char *user_no_pw= "mysqltest_no_pw";
 
16426
  const char *pw= "password";
 
16427
  const char *db= "mysqltest_user_test_database";
 
16428
  int rc;
 
16429
 
 
16430
  DBUG_ENTER("test_change_user");
 
16431
  myheader("test_change_user");
 
16432
 
 
16433
  /* Prepare environment */
 
16434
  sprintf(buff, "drop database if exists %s", db);
 
16435
  rc= mysql_query(mysql, buff);
 
16436
  myquery(rc);
 
16437
 
 
16438
  sprintf(buff, "create database %s", db);
 
16439
  rc= mysql_query(mysql, buff);
 
16440
  myquery(rc);
 
16441
 
 
16442
  sprintf(buff,
 
16443
          "grant select on %s.* to %s@'%%' identified by '%s'",
 
16444
          db,
 
16445
          user_pw,
 
16446
          pw);
 
16447
  rc= mysql_query(mysql, buff);
 
16448
  myquery(rc);
 
16449
 
 
16450
  sprintf(buff,
 
16451
          "grant select on %s.* to %s@'localhost' identified by '%s'",
 
16452
          db,
 
16453
          user_pw,
 
16454
          pw);
 
16455
  rc= mysql_query(mysql, buff);
 
16456
  myquery(rc);
 
16457
 
 
16458
  sprintf(buff,
 
16459
          "grant select on %s.* to %s@'%%'",
 
16460
          db,
 
16461
          user_no_pw);
 
16462
  rc= mysql_query(mysql, buff);
 
16463
  myquery(rc);
 
16464
 
 
16465
  sprintf(buff,
 
16466
          "grant select on %s.* to %s@'localhost'",
 
16467
          db,
 
16468
          user_no_pw);
 
16469
  rc= mysql_query(mysql, buff);
 
16470
  myquery(rc);
 
16471
 
 
16472
 
 
16473
  /* Try some combinations */
 
16474
  rc= mysql_change_user(mysql, NULL, NULL, NULL);
 
16475
  DIE_UNLESS(rc);
 
16476
  if (! opt_silent)
 
16477
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16478
 
 
16479
 
 
16480
  rc= mysql_change_user(mysql, "", NULL, NULL);
 
16481
  DIE_UNLESS(rc);
 
16482
  if (! opt_silent)
 
16483
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16484
 
 
16485
  rc= mysql_change_user(mysql, "", "", NULL);
 
16486
  DIE_UNLESS(rc);
 
16487
  if (! opt_silent)
 
16488
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16489
 
 
16490
  rc= mysql_change_user(mysql, "", "", "");
 
16491
  DIE_UNLESS(rc);
 
16492
  if (! opt_silent)
 
16493
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16494
 
 
16495
  rc= mysql_change_user(mysql, NULL, "", "");
 
16496
  DIE_UNLESS(rc);
 
16497
  if (! opt_silent)
 
16498
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16499
 
 
16500
 
 
16501
  rc= mysql_change_user(mysql, NULL, NULL, "");
 
16502
  DIE_UNLESS(rc);
 
16503
  if (! opt_silent)
 
16504
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16505
 
 
16506
  rc= mysql_change_user(mysql, "", NULL, "");
 
16507
  DIE_UNLESS(rc);
 
16508
  if (! opt_silent)
 
16509
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16510
 
 
16511
  rc= mysql_change_user(mysql, user_pw, NULL, "");
 
16512
  DIE_UNLESS(rc);
 
16513
  if (! opt_silent)
 
16514
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16515
 
 
16516
  rc= mysql_change_user(mysql, user_pw, "", "");
 
16517
  DIE_UNLESS(rc);
 
16518
  if (! opt_silent)
 
16519
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16520
 
 
16521
  rc= mysql_change_user(mysql, user_pw, "", NULL);
 
16522
  DIE_UNLESS(rc);
 
16523
  if (! opt_silent)
 
16524
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16525
 
 
16526
  rc= mysql_change_user(mysql, user_pw, NULL, NULL);
 
16527
  DIE_UNLESS(rc);
 
16528
  if (! opt_silent)
 
16529
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16530
 
 
16531
  rc= mysql_change_user(mysql, user_pw, "", db);
 
16532
  DIE_UNLESS(rc);
 
16533
  if (! opt_silent)
 
16534
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16535
 
 
16536
  rc= mysql_change_user(mysql, user_pw, NULL, db);
 
16537
  DIE_UNLESS(rc);
 
16538
  if (! opt_silent)
 
16539
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16540
 
 
16541
  rc= mysql_change_user(mysql, user_pw, pw, db);
 
16542
  myquery(rc);
 
16543
 
 
16544
  rc= mysql_change_user(mysql, user_pw, pw, NULL);
 
16545
  myquery(rc);
 
16546
 
 
16547
  rc= mysql_change_user(mysql, user_pw, pw, "");
 
16548
  myquery(rc);
 
16549
 
 
16550
  rc= mysql_change_user(mysql, user_no_pw, 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, user_no_pw, 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, user_no_pw, 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, user_no_pw, "", NULL);
 
16566
  myquery(rc);
 
16567
 
 
16568
  rc= mysql_change_user(mysql, user_no_pw, "", "");
 
16569
  myquery(rc);
 
16570
 
 
16571
  rc= mysql_change_user(mysql, user_no_pw, "", db);
 
16572
  myquery(rc);
 
16573
 
 
16574
  rc= mysql_change_user(mysql, user_no_pw, NULL, db);
 
16575
  myquery(rc);
 
16576
 
 
16577
  rc= mysql_change_user(mysql, "", pw, db);
 
16578
  DIE_UNLESS(rc);
 
16579
  if (! opt_silent)
 
16580
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16581
 
 
16582
  rc= mysql_change_user(mysql, "", pw, "");
 
16583
  DIE_UNLESS(rc);
 
16584
  if (! opt_silent)
 
16585
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16586
 
 
16587
  rc= mysql_change_user(mysql, "", pw, NULL);
 
16588
  DIE_UNLESS(rc);
 
16589
  if (! opt_silent)
 
16590
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16591
 
 
16592
  rc= mysql_change_user(mysql, NULL, pw, NULL);
 
16593
  DIE_UNLESS(rc);
 
16594
  if (! opt_silent)
 
16595
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16596
 
 
16597
  rc= mysql_change_user(mysql, NULL, NULL, db);
 
16598
  DIE_UNLESS(rc);
 
16599
  if (! opt_silent)
 
16600
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16601
 
 
16602
  rc= mysql_change_user(mysql, NULL, "", db);
 
16603
  DIE_UNLESS(rc);
 
16604
  if (! opt_silent)
 
16605
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16606
 
 
16607
  rc= mysql_change_user(mysql, "", "", db);
 
16608
  DIE_UNLESS(rc);
 
16609
  if (! opt_silent)
 
16610
    printf("Got error (as expected): %s\n", mysql_error(mysql));
 
16611
 
 
16612
  /* Cleanup the environment */
 
16613
 
 
16614
  mysql_change_user(mysql, opt_user, opt_password, current_db);
 
16615
 
 
16616
  sprintf(buff, "drop database %s", db);
 
16617
  rc= mysql_query(mysql, buff);
 
16618
  myquery(rc);
 
16619
 
 
16620
  sprintf(buff, "drop user %s@'%%'", user_pw);
 
16621
  rc= mysql_query(mysql, buff);
 
16622
  myquery(rc);
 
16623
 
 
16624
  sprintf(buff, "drop user %s@'%%'", user_no_pw);
 
16625
  rc= mysql_query(mysql, buff);
 
16626
  myquery(rc);
 
16627
 
 
16628
  sprintf(buff, "drop user %s@'localhost'", user_pw);
 
16629
  rc= mysql_query(mysql, buff);
 
16630
  myquery(rc);
 
16631
 
 
16632
  sprintf(buff, "drop user %s@'localhost'", user_no_pw);
 
16633
  rc= mysql_query(mysql, buff);
 
16634
  myquery(rc);
 
16635
 
 
16636
  DBUG_VOID_RETURN;
 
16637
}
 
16638
 
 
16639
/*
 
16640
  Bug#27592 (stack overrun when storing datetime value using prepared statements)
 
16641
*/
 
16642
 
 
16643
static void test_bug27592()
 
16644
{
 
16645
  const int NUM_ITERATIONS= 40;
 
16646
  int i;
 
16647
  int rc;
 
16648
  MYSQL_STMT *stmt= NULL;
 
16649
  MYSQL_BIND bind[1];
 
16650
  MYSQL_TIME time_val;
 
16651
 
 
16652
  DBUG_ENTER("test_bug27592");
 
16653
  myheader("test_bug27592");
 
16654
 
 
16655
  mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
16656
  mysql_query(mysql, "CREATE TABLE t1(c2 DATETIME)");
 
16657
 
 
16658
  stmt= mysql_simple_prepare(mysql, "INSERT INTO t1 VALUES (?)");
 
16659
  DIE_UNLESS(stmt);
 
16660
 
 
16661
  memset(bind, 0, sizeof(bind));
 
16662
 
 
16663
  bind[0].buffer_type= MYSQL_TYPE_DATETIME;
 
16664
  bind[0].buffer= (char *) &time_val;
 
16665
  bind[0].length= NULL;
 
16666
 
 
16667
  for (i= 0; i < NUM_ITERATIONS; i++)
 
16668
  {
 
16669
    time_val.year= 2007;
 
16670
    time_val.month= 6;
 
16671
    time_val.day= 7;
 
16672
    time_val.hour= 18;
 
16673
    time_val.minute= 41;
 
16674
    time_val.second= 3;
 
16675
 
 
16676
    time_val.second_part=0;
 
16677
    time_val.neg=0;
 
16678
 
 
16679
    rc= mysql_stmt_bind_param(stmt, bind);
 
16680
    check_execute(stmt, rc);
 
16681
 
 
16682
    rc= mysql_stmt_execute(stmt);
 
16683
    check_execute(stmt, rc);
 
16684
  }
 
16685
 
 
16686
  mysql_stmt_close(stmt);
 
16687
 
 
16688
  DBUG_VOID_RETURN;
 
16689
}
 
16690
 
 
16691
/*
 
16692
  Bug#29687 mysql_stmt_store_result memory leak in libmysqld
 
16693
*/
 
16694
 
 
16695
static void test_bug29687()
 
16696
{
 
16697
  const int NUM_ITERATIONS= 40;
 
16698
  int i;
 
16699
  int rc;
 
16700
  MYSQL_STMT *stmt= NULL;
 
16701
 
 
16702
  DBUG_ENTER("test_bug29687");
 
16703
  myheader("test_bug29687");
 
16704
 
 
16705
  stmt= mysql_simple_prepare(mysql, "SELECT 1 FROM dual WHERE 0=2");
 
16706
  DIE_UNLESS(stmt);
 
16707
 
 
16708
  for (i= 0; i < NUM_ITERATIONS; i++)
 
16709
  {
 
16710
    rc= mysql_stmt_execute(stmt);
 
16711
    check_execute(stmt, rc);
 
16712
    mysql_stmt_store_result(stmt);
 
16713
    while (mysql_stmt_fetch(stmt)==0);
 
16714
    mysql_stmt_free_result(stmt);
 
16715
  }
 
16716
 
 
16717
  mysql_stmt_close(stmt);
 
16718
  DBUG_VOID_RETURN;
 
16719
}
 
16720
 
 
16721
 
 
16722
/*
 
16723
  Bug #29692    Single row inserts can incorrectly report a huge number of 
 
16724
  row insertions
 
16725
*/
 
16726
 
 
16727
static void test_bug29692()
 
16728
{
 
16729
  MYSQL* conn;
 
16730
 
 
16731
  if (!(conn= mysql_client_init(NULL)))
 
16732
  {
 
16733
    myerror("test_bug29692 init failed");
 
16734
    exit(1);
 
16735
  }
 
16736
 
 
16737
  if (!(mysql_real_connect(conn, opt_host, opt_user,
 
16738
                           opt_password, opt_db ? opt_db:"test", opt_port,
 
16739
                           opt_unix_socket,  CLIENT_FOUND_ROWS)))
 
16740
  {
 
16741
    myerror("test_bug29692 connection failed");
 
16742
    mysql_close(mysql);
 
16743
    exit(1);
 
16744
  }
 
16745
  myquery(mysql_query(conn, "drop table if exists t1"));
 
16746
  myquery(mysql_query(conn, "create table t1(f1 int)"));
 
16747
  myquery(mysql_query(conn, "insert into t1 values(1)"));
 
16748
  DIE_UNLESS(1 == mysql_affected_rows(conn));
 
16749
  myquery(mysql_query(conn, "drop table t1"));
 
16750
  mysql_close(conn);
 
16751
}
 
16752
 
 
16753
/**
 
16754
  Bug#29306 Truncated data in MS Access with decimal (3,1) columns in a VIEW
 
16755
*/
 
16756
 
 
16757
static void test_bug29306()
 
16758
{
 
16759
  MYSQL_FIELD *field;
 
16760
  int rc;
 
16761
  MYSQL_RES *res;
 
16762
 
 
16763
  DBUG_ENTER("test_bug29306");
 
16764
  myheader("test_bug29306");
 
16765
 
 
16766
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS tab17557");
 
16767
  myquery(rc);
 
16768
  rc= mysql_query(mysql, "DROP VIEW IF EXISTS view17557");
 
16769
  myquery(rc);
 
16770
  rc= mysql_query(mysql, "CREATE TABLE tab17557 (dd decimal (3,1))");
 
16771
  myquery(rc);
 
16772
  rc= mysql_query(mysql, "CREATE VIEW view17557 as SELECT dd FROM tab17557");
 
16773
  myquery(rc);
 
16774
  rc= mysql_query(mysql, "INSERT INTO tab17557 VALUES (7.6)");
 
16775
  myquery(rc);
 
16776
 
 
16777
  /* Checking the view */
 
16778
  res= mysql_list_fields(mysql, "view17557", NULL);
 
16779
  while ((field= mysql_fetch_field(res)))
 
16780
  {
 
16781
    if (! opt_silent)
 
16782
    {
 
16783
      printf("field name %s\n", field->name);
 
16784
      printf("field table %s\n", field->table);
 
16785
      printf("field decimals %d\n", field->decimals);
 
16786
      if (field->decimals < 1)
 
16787
        printf("Error! No decimals! \n");
 
16788
      printf("\n\n");
 
16789
    }
 
16790
    DIE_UNLESS(field->decimals == 1);
 
16791
  }
 
16792
  mysql_free_result(res);
 
16793
 
 
16794
  rc= mysql_query(mysql, "DROP TABLE tab17557");
 
16795
  myquery(rc);
 
16796
  rc= mysql_query(mysql, "DROP VIEW view17557");
 
16797
  myquery(rc);
 
16798
 
 
16799
  DBUG_VOID_RETURN;
 
16800
}
 
16801
/*
 
16802
  Bug#30472: libmysql doesn't reset charset, insert_id after succ.
 
16803
  mysql_change_user() call row insertions.
 
16804
*/
 
16805
 
 
16806
static void bug30472_retrieve_charset_info(MYSQL *con,
 
16807
                                           char *character_set_name,
 
16808
                                           char *character_set_client,
 
16809
                                           char *character_set_results,
 
16810
                                           char *collation_connection)
 
16811
{
 
16812
  MYSQL_RES *rs;
 
16813
  MYSQL_ROW row;
 
16814
 
 
16815
  /* Get the cached client character set name. */
 
16816
 
 
16817
  strcpy(character_set_name, mysql_character_set_name(con));
 
16818
 
 
16819
  /* Retrieve server character set information. */
 
16820
 
 
16821
  DIE_IF(mysql_query(con, "SHOW VARIABLES LIKE 'character_set_client'"));
 
16822
  DIE_UNLESS(rs= mysql_store_result(con));
 
16823
  DIE_UNLESS(row= mysql_fetch_row(rs));
 
16824
  strcpy(character_set_client, row[1]);
 
16825
  mysql_free_result(rs);
 
16826
 
 
16827
  DIE_IF(mysql_query(con, "SHOW VARIABLES LIKE 'character_set_results'"));
 
16828
  DIE_UNLESS(rs= mysql_store_result(con));
 
16829
  DIE_UNLESS(row= mysql_fetch_row(rs));
 
16830
  strcpy(character_set_results, row[1]);
 
16831
  mysql_free_result(rs);
 
16832
 
 
16833
  DIE_IF(mysql_query(con, "SHOW VARIABLES LIKE 'collation_connection'"));
 
16834
  DIE_UNLESS(rs= mysql_store_result(con));
 
16835
  DIE_UNLESS(row= mysql_fetch_row(rs));
 
16836
  strcpy(collation_connection, row[1]);
 
16837
  mysql_free_result(rs);
 
16838
}
 
16839
 
 
16840
static void test_bug30472()
 
16841
{
 
16842
  MYSQL con;
 
16843
 
 
16844
  char character_set_name_1[MY_CS_NAME_SIZE];
 
16845
  char character_set_client_1[MY_CS_NAME_SIZE];
 
16846
  char character_set_results_1[MY_CS_NAME_SIZE];
 
16847
  char collation_connnection_1[MY_CS_NAME_SIZE];
 
16848
 
 
16849
  char character_set_name_2[MY_CS_NAME_SIZE];
 
16850
  char character_set_client_2[MY_CS_NAME_SIZE];
 
16851
  char character_set_results_2[MY_CS_NAME_SIZE];
 
16852
  char collation_connnection_2[MY_CS_NAME_SIZE];
 
16853
 
 
16854
  char character_set_name_3[MY_CS_NAME_SIZE];
 
16855
  char character_set_client_3[MY_CS_NAME_SIZE];
 
16856
  char character_set_results_3[MY_CS_NAME_SIZE];
 
16857
  char collation_connnection_3[MY_CS_NAME_SIZE];
 
16858
 
 
16859
  char character_set_name_4[MY_CS_NAME_SIZE];
 
16860
  char character_set_client_4[MY_CS_NAME_SIZE];
 
16861
  char character_set_results_4[MY_CS_NAME_SIZE];
 
16862
  char collation_connnection_4[MY_CS_NAME_SIZE];
 
16863
 
 
16864
  /* Create a new connection. */
 
16865
 
 
16866
  DIE_UNLESS(mysql_client_init(&con));
 
16867
 
 
16868
  DIE_UNLESS(mysql_real_connect(&con,
 
16869
                                opt_host,
 
16870
                                opt_user,
 
16871
                                opt_password,
 
16872
                                opt_db ? opt_db : "test",
 
16873
                                opt_port,
 
16874
                                opt_unix_socket,
 
16875
                                CLIENT_FOUND_ROWS));
 
16876
 
 
16877
  /* Retrieve character set information. */
 
16878
 
 
16879
  bug30472_retrieve_charset_info(&con,
 
16880
                                 character_set_name_1,
 
16881
                                 character_set_client_1,
 
16882
                                 character_set_results_1,
 
16883
                                 collation_connnection_1);
 
16884
 
 
16885
  /* Switch client character set. */
 
16886
 
 
16887
  DIE_IF(mysql_set_character_set(&con, "utf8"));
 
16888
 
 
16889
  /* Retrieve character set information. */
 
16890
 
 
16891
  bug30472_retrieve_charset_info(&con,
 
16892
                                 character_set_name_2,
 
16893
                                 character_set_client_2,
 
16894
                                 character_set_results_2,
 
16895
                                 collation_connnection_2);
 
16896
 
 
16897
  /*
 
16898
    Check that
 
16899
      1) character set has been switched and
 
16900
      2) new character set is different from the original one.
 
16901
  */
 
16902
 
 
16903
  DIE_UNLESS(strcmp(character_set_name_2, "utf8") == 0);
 
16904
  DIE_UNLESS(strcmp(character_set_client_2, "utf8") == 0);
 
16905
  DIE_UNLESS(strcmp(character_set_results_2, "utf8") == 0);
 
16906
  DIE_UNLESS(strcmp(collation_connnection_2, "utf8_general_ci") == 0);
 
16907
 
 
16908
  DIE_UNLESS(strcmp(character_set_name_1, character_set_name_2) != 0);
 
16909
  DIE_UNLESS(strcmp(character_set_client_1, character_set_client_2) != 0);
 
16910
  DIE_UNLESS(strcmp(character_set_results_1, character_set_results_2) != 0);
 
16911
  DIE_UNLESS(strcmp(collation_connnection_1, collation_connnection_2) != 0);
 
16912
 
 
16913
  /* Call mysql_change_user() with the same username, password, database. */
 
16914
 
 
16915
  DIE_IF(mysql_change_user(&con,
 
16916
                           opt_user,
 
16917
                           opt_password,
 
16918
                           opt_db ? opt_db : "test"));
 
16919
 
 
16920
  /* Retrieve character set information. */
 
16921
 
 
16922
  bug30472_retrieve_charset_info(&con,
 
16923
                                 character_set_name_3,
 
16924
                                 character_set_client_3,
 
16925
                                 character_set_results_3,
 
16926
                                 collation_connnection_3);
 
16927
 
 
16928
  /* Check that character set information has been reset. */
 
16929
 
 
16930
  DIE_UNLESS(strcmp(character_set_name_1, character_set_name_3) == 0);
 
16931
  DIE_UNLESS(strcmp(character_set_client_1, character_set_client_3) == 0);
 
16932
  DIE_UNLESS(strcmp(character_set_results_1, character_set_results_3) == 0);
 
16933
  DIE_UNLESS(strcmp(collation_connnection_1, collation_connnection_3) == 0);
 
16934
 
 
16935
  /* Change connection-default character set in the client. */
 
16936
 
 
16937
  mysql_options(&con, MYSQL_SET_CHARSET_NAME, "utf8");
 
16938
 
 
16939
  /*
 
16940
    Call mysql_change_user() in order to check that new connection will
 
16941
    have UTF8 character set on the client and on the server.
 
16942
  */
 
16943
 
 
16944
  DIE_IF(mysql_change_user(&con,
 
16945
                           opt_user,
 
16946
                           opt_password,
 
16947
                           opt_db ? opt_db : "test"));
 
16948
 
 
16949
  /* Retrieve character set information. */
 
16950
 
 
16951
  bug30472_retrieve_charset_info(&con,
 
16952
                                 character_set_name_4,
 
16953
                                 character_set_client_4,
 
16954
                                 character_set_results_4,
 
16955
                                 collation_connnection_4);
 
16956
 
 
16957
  /* Check that we have UTF8 on the server and on the client. */
 
16958
 
 
16959
  DIE_UNLESS(strcmp(character_set_name_4, "utf8") == 0);
 
16960
  DIE_UNLESS(strcmp(character_set_client_4, "utf8") == 0);
 
16961
  DIE_UNLESS(strcmp(character_set_results_4, "utf8") == 0);
 
16962
  DIE_UNLESS(strcmp(collation_connnection_4, "utf8_general_ci") == 0);
 
16963
 
 
16964
  /* That's it. Cleanup. */
 
16965
 
 
16966
  mysql_close(&con);
 
16967
}
 
16968
 
 
16969
static void bug20023_change_user(MYSQL *con)
 
16970
{
 
16971
  DIE_IF(mysql_change_user(con,
 
16972
                           opt_user,
 
16973
                           opt_password,
 
16974
                           opt_db ? opt_db : "test"));
 
16975
}
 
16976
 
 
16977
static my_bool query_str_variable(MYSQL *con,
 
16978
                                  const char *var_name,
 
16979
                                  char *str,
 
16980
                                  size_t len)
 
16981
{
 
16982
  MYSQL_RES *rs;
 
16983
  MYSQL_ROW row;
 
16984
 
 
16985
  char query_buffer[MAX_TEST_QUERY_LENGTH];
 
16986
 
 
16987
  my_bool is_null;
 
16988
 
 
16989
  my_snprintf(query_buffer, sizeof (query_buffer),
 
16990
              "SELECT %s", var_name);
 
16991
 
 
16992
  DIE_IF(mysql_query(con, query_buffer));
 
16993
  DIE_UNLESS(rs= mysql_store_result(con));
 
16994
  DIE_UNLESS(row= mysql_fetch_row(rs));
 
16995
 
 
16996
  is_null= row[0] == NULL;
 
16997
 
 
16998
  if (!is_null)
 
16999
    my_snprintf(str, len, "%s", row[0]);
 
17000
 
 
17001
  mysql_free_result(rs);
 
17002
 
 
17003
  return is_null;
 
17004
}
 
17005
 
 
17006
static my_bool query_int_variable(MYSQL *con,
 
17007
                                  const char *var_name,
 
17008
                                  int *var_value)
 
17009
{
 
17010
  char str[32];
 
17011
  my_bool is_null= query_str_variable(con, var_name, str, sizeof(str));
 
17012
 
 
17013
  if (!is_null)
 
17014
    *var_value= atoi(str);
 
17015
 
 
17016
  return is_null;
 
17017
}
 
17018
 
 
17019
static void test_bug20023()
 
17020
{
 
17021
  MYSQL con;
 
17022
 
 
17023
  int sql_big_selects_orig= 0;
 
17024
  /*
 
17025
    Type of max_join_size is ha_rows, which might be ulong or off_t
 
17026
    depending on the platform or configure options. Preserve the string
 
17027
    to avoid type overflow pitfalls.
 
17028
  */
 
17029
  char max_join_size_orig[32];
 
17030
 
 
17031
  int sql_big_selects_2= 0;
 
17032
  int sql_big_selects_3= 0;
 
17033
  int sql_big_selects_4= 0;
 
17034
  int sql_big_selects_5= 0;
 
17035
 
 
17036
  char query_buffer[MAX_TEST_QUERY_LENGTH];
 
17037
 
 
17038
  /* Create a new connection. */
 
17039
 
 
17040
  DIE_UNLESS(mysql_client_init(&con));
 
17041
 
 
17042
  DIE_UNLESS(mysql_real_connect(&con,
 
17043
                                opt_host,
 
17044
                                opt_user,
 
17045
                                opt_password,
 
17046
                                opt_db ? opt_db : "test",
 
17047
                                opt_port,
 
17048
                                opt_unix_socket,
 
17049
                                CLIENT_FOUND_ROWS));
 
17050
 
 
17051
  /***********************************************************************
 
17052
    Remember original SQL_BIG_SELECTS, MAX_JOIN_SIZE values.
 
17053
  ***********************************************************************/
 
17054
 
 
17055
  query_int_variable(&con,
 
17056
                     "@@session.sql_big_selects",
 
17057
                     &sql_big_selects_orig);
 
17058
 
 
17059
  query_str_variable(&con,
 
17060
                     "@@global.max_join_size",
 
17061
                     max_join_size_orig,
 
17062
                     sizeof(max_join_size_orig));
 
17063
 
 
17064
  /***********************************************************************
 
17065
    Test that COM_CHANGE_USER resets the SQL_BIG_SELECTS to the initial value.
 
17066
  ***********************************************************************/
 
17067
 
 
17068
  /* Issue COM_CHANGE_USER. */
 
17069
 
 
17070
  bug20023_change_user(&con);
 
17071
 
 
17072
  /* Query SQL_BIG_SELECTS. */
 
17073
 
 
17074
  query_int_variable(&con,
 
17075
                     "@@session.sql_big_selects",
 
17076
                     &sql_big_selects_2);
 
17077
 
 
17078
  /* Check that SQL_BIG_SELECTS is reset properly. */
 
17079
 
 
17080
  DIE_UNLESS(sql_big_selects_orig == sql_big_selects_2);
 
17081
 
 
17082
  /***********************************************************************
 
17083
    Test that if MAX_JOIN_SIZE set to non-default value,
 
17084
    SQL_BIG_SELECTS will be 0.
 
17085
  ***********************************************************************/
 
17086
 
 
17087
  /* Set MAX_JOIN_SIZE to some non-default value. */
 
17088
 
 
17089
  DIE_IF(mysql_query(&con, "SET @@global.max_join_size = 10000"));
 
17090
  DIE_IF(mysql_query(&con, "SET @@session.max_join_size = default"));
 
17091
 
 
17092
  /* Issue COM_CHANGE_USER. */
 
17093
 
 
17094
  bug20023_change_user(&con);
 
17095
 
 
17096
  /* Query SQL_BIG_SELECTS. */
 
17097
 
 
17098
  query_int_variable(&con,
 
17099
                     "@@session.sql_big_selects",
 
17100
                     &sql_big_selects_3);
 
17101
 
 
17102
  /* Check that SQL_BIG_SELECTS is 0. */
 
17103
 
 
17104
  DIE_UNLESS(sql_big_selects_3 == 0);
 
17105
 
 
17106
  /***********************************************************************
 
17107
    Test that if MAX_JOIN_SIZE set to default value,
 
17108
    SQL_BIG_SELECTS will be 1.
 
17109
  ***********************************************************************/
 
17110
 
 
17111
  /* Set MAX_JOIN_SIZE to the default value (-1). */
 
17112
 
 
17113
  DIE_IF(mysql_query(&con, "SET @@global.max_join_size = -1"));
 
17114
  DIE_IF(mysql_query(&con, "SET @@session.max_join_size = default"));
 
17115
 
 
17116
  /* Issue COM_CHANGE_USER. */
 
17117
 
 
17118
  bug20023_change_user(&con);
 
17119
 
 
17120
  /* Query SQL_BIG_SELECTS. */
 
17121
 
 
17122
  query_int_variable(&con,
 
17123
                     "@@session.sql_big_selects",
 
17124
                     &sql_big_selects_4);
 
17125
 
 
17126
  /* Check that SQL_BIG_SELECTS is 1. */
 
17127
 
 
17128
  DIE_UNLESS(sql_big_selects_4 == 1);
 
17129
 
 
17130
  /***********************************************************************
 
17131
    Restore MAX_JOIN_SIZE.
 
17132
    Check that SQL_BIG_SELECTS will be the original one.
 
17133
  ***********************************************************************/
 
17134
 
 
17135
  /* Restore MAX_JOIN_SIZE. */
 
17136
 
 
17137
  my_snprintf(query_buffer,
 
17138
           sizeof (query_buffer),
 
17139
           "SET @@global.max_join_size = %s",
 
17140
           max_join_size_orig);
 
17141
 
 
17142
  DIE_IF(mysql_query(&con, query_buffer));
 
17143
 
 
17144
  DIE_IF(mysql_query(&con, "SET @@global.max_join_size = -1"));
 
17145
  DIE_IF(mysql_query(&con, "SET @@session.max_join_size = default"));
 
17146
 
 
17147
  /* Issue COM_CHANGE_USER. */
 
17148
 
 
17149
  bug20023_change_user(&con);
 
17150
 
 
17151
  /* Query SQL_BIG_SELECTS. */
 
17152
 
 
17153
  query_int_variable(&con,
 
17154
                     "@@session.sql_big_selects",
 
17155
                     &sql_big_selects_5);
 
17156
 
 
17157
  /* Check that SQL_BIG_SELECTS is 1. */
 
17158
 
 
17159
  DIE_UNLESS(sql_big_selects_5 == sql_big_selects_orig);
 
17160
 
 
17161
  /***********************************************************************
 
17162
    That's it. Cleanup.
 
17163
  ***********************************************************************/
 
17164
 
 
17165
  mysql_close(&con);
 
17166
}
 
17167
 
 
17168
static void bug31418_impl()
 
17169
{
 
17170
  MYSQL con;
 
17171
 
 
17172
  my_bool is_null;
 
17173
  int rc= 0;
 
17174
 
 
17175
  /* Create a new connection. */
 
17176
 
 
17177
  DIE_UNLESS(mysql_client_init(&con));
 
17178
 
 
17179
  DIE_UNLESS(mysql_real_connect(&con,
 
17180
                                opt_host,
 
17181
                                opt_user,
 
17182
                                opt_password,
 
17183
                                opt_db ? opt_db : "test",
 
17184
                                opt_port,
 
17185
                                opt_unix_socket,
 
17186
                                CLIENT_FOUND_ROWS));
 
17187
 
 
17188
  /***********************************************************************
 
17189
    Check that lock is free:
 
17190
      - IS_FREE_LOCK() should return 1;
 
17191
      - IS_USED_LOCK() should return NULL;
 
17192
  ***********************************************************************/
 
17193
 
 
17194
  is_null= query_int_variable(&con,
 
17195
                              "IS_FREE_LOCK('bug31418')",
 
17196
                              &rc);
 
17197
  DIE_UNLESS(!is_null && rc);
 
17198
 
 
17199
  is_null= query_int_variable(&con,
 
17200
                              "IS_USED_LOCK('bug31418')",
 
17201
                              &rc);
 
17202
  DIE_UNLESS(is_null);
 
17203
 
 
17204
  /***********************************************************************
 
17205
    Acquire lock and check the lock status (the lock must be in use):
 
17206
      - IS_FREE_LOCK() should return 0;
 
17207
      - IS_USED_LOCK() should return non-zero thread id;
 
17208
  ***********************************************************************/
 
17209
 
 
17210
  query_int_variable(&con, "GET_LOCK('bug31418', 1)", &rc);
 
17211
  DIE_UNLESS(rc);
 
17212
 
 
17213
  is_null= query_int_variable(&con,
 
17214
                              "IS_FREE_LOCK('bug31418')",
 
17215
                              &rc);
 
17216
  DIE_UNLESS(!is_null && !rc);
 
17217
 
 
17218
  is_null= query_int_variable(&con,
 
17219
                              "IS_USED_LOCK('bug31418')",
 
17220
                              &rc);
 
17221
  DIE_UNLESS(!is_null && rc);
 
17222
 
 
17223
  /***********************************************************************
 
17224
    Issue COM_CHANGE_USER command and check the lock status
 
17225
    (the lock must be free):
 
17226
      - IS_FREE_LOCK() should return 1;
 
17227
      - IS_USED_LOCK() should return NULL;
 
17228
  **********************************************************************/
 
17229
 
 
17230
  bug20023_change_user(&con);
 
17231
 
 
17232
  is_null= query_int_variable(&con,
 
17233
                              "IS_FREE_LOCK('bug31418')",
 
17234
                              &rc);
 
17235
  DIE_UNLESS(!is_null && rc);
 
17236
 
 
17237
  is_null= query_int_variable(&con,
 
17238
                              "IS_USED_LOCK('bug31418')",
 
17239
                              &rc);
 
17240
  DIE_UNLESS(is_null);
 
17241
 
 
17242
  /***********************************************************************
 
17243
   That's it. Cleanup.
 
17244
  ***********************************************************************/
 
17245
 
 
17246
  mysql_close(&con);
 
17247
}
 
17248
 
 
17249
static void test_bug31418()
 
17250
{
 
17251
  /* Run test case for BUG#31418 for three different connections. */
 
17252
 
 
17253
  bug31418_impl();
 
17254
 
 
17255
  bug31418_impl();
 
17256
 
 
17257
  bug31418_impl();
 
17258
}
 
17259
 
 
17260
 
 
17261
 
 
17262
/**
 
17263
  Bug#31669 Buffer overflow in mysql_change_user()
 
17264
*/
 
17265
 
 
17266
#define LARGE_BUFFER_SIZE 2048
 
17267
 
 
17268
static void test_bug31669()
 
17269
{
 
17270
  int rc;
 
17271
  static char buff[LARGE_BUFFER_SIZE+1];
 
17272
#ifndef EMBEDDED_LIBRARY
 
17273
  static char user[USERNAME_CHAR_LENGTH+1];
 
17274
  static char db[NAME_CHAR_LEN+1];
 
17275
  static char query[LARGE_BUFFER_SIZE*2];
 
17276
#endif
 
17277
 
 
17278
  DBUG_ENTER("test_bug31669");
 
17279
  myheader("test_bug31669");
 
17280
 
 
17281
  rc= mysql_change_user(mysql, NULL, NULL, NULL);
 
17282
  DIE_UNLESS(rc);
 
17283
 
 
17284
  rc= mysql_change_user(mysql, "", "", "");
 
17285
  DIE_UNLESS(rc);
 
17286
 
 
17287
  memset(buff, 'a', sizeof(buff));
 
17288
 
 
17289
  rc= mysql_change_user(mysql, buff, buff, buff);
 
17290
  DIE_UNLESS(rc);
 
17291
 
 
17292
  rc = mysql_change_user(mysql, opt_user, opt_password, current_db);
 
17293
  DIE_UNLESS(!rc);
 
17294
 
 
17295
#ifndef EMBEDDED_LIBRARY
 
17296
  memset(db, 'a', sizeof(db));
 
17297
  db[NAME_CHAR_LEN]= 0;
 
17298
  strxmov(query, "CREATE DATABASE IF NOT EXISTS ", db, NullS);
 
17299
  rc= mysql_query(mysql, query);
 
17300
  myquery(rc);
 
17301
 
 
17302
  memset(user, 'b', sizeof(user));
 
17303
  user[USERNAME_CHAR_LENGTH]= 0;
 
17304
  memset(buff, 'c', sizeof(buff));
 
17305
  buff[LARGE_BUFFER_SIZE]= 0;
 
17306
  strxmov(query, "GRANT ALL PRIVILEGES ON *.* TO '", user, "'@'%' IDENTIFIED BY "
 
17307
                 "'", buff, "' WITH GRANT OPTION", NullS);
 
17308
  rc= mysql_query(mysql, query);
 
17309
  myquery(rc);
 
17310
 
 
17311
  strxmov(query, "GRANT ALL PRIVILEGES ON *.* TO '", user, "'@'localhost' IDENTIFIED BY "
 
17312
                 "'", buff, "' WITH GRANT OPTION", NullS);
 
17313
  rc= mysql_query(mysql, query);
 
17314
  myquery(rc);
 
17315
 
 
17316
  rc= mysql_query(mysql, "FLUSH PRIVILEGES");
 
17317
  myquery(rc);
 
17318
 
 
17319
  rc= mysql_change_user(mysql, user, buff, db);
 
17320
  DIE_UNLESS(!rc);
 
17321
 
 
17322
  user[USERNAME_CHAR_LENGTH-1]= 'a';
 
17323
  rc= mysql_change_user(mysql, user, buff, db);
 
17324
  DIE_UNLESS(rc);
 
17325
 
 
17326
  user[USERNAME_CHAR_LENGTH-1]= 'b';
 
17327
  buff[LARGE_BUFFER_SIZE-1]= 'd';
 
17328
  rc= mysql_change_user(mysql, user, buff, db);
 
17329
  DIE_UNLESS(rc);
 
17330
 
 
17331
  buff[LARGE_BUFFER_SIZE-1]= 'c';
 
17332
  db[NAME_CHAR_LEN-1]= 'e';
 
17333
  rc= mysql_change_user(mysql, user, buff, db);
 
17334
  DIE_UNLESS(rc);
 
17335
 
 
17336
  db[NAME_CHAR_LEN-1]= 'a';
 
17337
  rc= mysql_change_user(mysql, user, buff, db);
 
17338
  DIE_UNLESS(!rc);
 
17339
 
 
17340
  rc= mysql_change_user(mysql, user + 1, buff + 1, db + 1);
 
17341
  DIE_UNLESS(rc);
 
17342
 
 
17343
  rc = mysql_change_user(mysql, opt_user, opt_password, current_db);
 
17344
  DIE_UNLESS(!rc);
 
17345
 
 
17346
  strxmov(query, "DROP DATABASE ", db, NullS);
 
17347
  rc= mysql_query(mysql, query);
 
17348
  myquery(rc);
 
17349
 
 
17350
  strxmov(query, "DELETE FROM mysql.user WHERE User='", user, "'", NullS);
 
17351
  rc= mysql_query(mysql, query);
 
17352
  myquery(rc);
 
17353
  DIE_UNLESS(mysql_affected_rows(mysql) == 2);
 
17354
#endif
 
17355
 
 
17356
  DBUG_VOID_RETURN;
 
17357
}
 
17358
 
 
17359
 
 
17360
/**
 
17361
  Bug#28386 the general log is incomplete
 
17362
*/
 
17363
 
 
17364
static void test_bug28386()
 
17365
{
 
17366
  int rc;
 
17367
  MYSQL_STMT *stmt;
 
17368
  MYSQL_RES *result;
 
17369
  MYSQL_ROW row;
 
17370
  MYSQL_BIND bind;
 
17371
  const char hello[]= "hello world!";
 
17372
 
 
17373
  DBUG_ENTER("test_bug28386");
 
17374
  myheader("test_bug28386");
 
17375
 
 
17376
  rc= mysql_query(mysql, "select @@global.log_output");
 
17377
  myquery(rc);
 
17378
 
 
17379
  result= mysql_store_result(mysql);
 
17380
  DIE_UNLESS(result);
 
17381
 
 
17382
  row= mysql_fetch_row(result);
 
17383
  if (! strstr(row[0], "TABLE"))
 
17384
  {
 
17385
    mysql_free_result(result);
 
17386
    if (! opt_silent)
 
17387
      printf("Skipping the test since logging to tables is not enabled\n");
 
17388
    /* Log output is not to tables */
 
17389
    return;
 
17390
  }
 
17391
  mysql_free_result(result);
 
17392
 
 
17393
  enable_general_log(1);
 
17394
 
 
17395
  stmt= mysql_simple_prepare(mysql, "SELECT ?");
 
17396
  check_stmt(stmt);
 
17397
 
 
17398
  memset(&bind, 0, sizeof(bind));
 
17399
 
 
17400
  bind.buffer_type= MYSQL_TYPE_STRING;
 
17401
  bind.buffer= (void *) hello;
 
17402
  bind.buffer_length= sizeof(hello);
 
17403
 
 
17404
  mysql_stmt_bind_param(stmt, &bind);
 
17405
  mysql_stmt_send_long_data(stmt, 0, hello, sizeof(hello));
 
17406
 
 
17407
  rc= mysql_stmt_execute(stmt);
 
17408
  check_execute(stmt, rc);
 
17409
 
 
17410
  rc= my_process_stmt_result(stmt);
 
17411
  DIE_UNLESS(rc == 1);
 
17412
 
 
17413
  rc= mysql_stmt_reset(stmt);
 
17414
  check_execute(stmt, rc);
 
17415
 
 
17416
  rc= mysql_stmt_close(stmt);
 
17417
  DIE_UNLESS(!rc);
 
17418
 
 
17419
  rc= mysql_query(mysql, "select * from mysql.general_log where "
 
17420
                         "command_type='Close stmt' or "
 
17421
                         "command_type='Reset stmt' or "
 
17422
                         "command_type='Long Data'");
 
17423
  myquery(rc);
 
17424
 
 
17425
  result= mysql_store_result(mysql);
 
17426
  mytest(result);
 
17427
 
 
17428
  DIE_UNLESS(mysql_num_rows(result) == 3);
 
17429
 
 
17430
  mysql_free_result(result);
 
17431
 
 
17432
  restore_general_log();
 
17433
 
 
17434
  DBUG_VOID_RETURN;
 
17435
}
 
17436
 
 
17437
static void test_wl4166_1()
 
17438
{
 
17439
  MYSQL_STMT *stmt;
 
17440
  int        int_data;
 
17441
  char       str_data[50];
 
17442
  char       tiny_data;
 
17443
  short      small_data;
 
17444
  longlong   big_data;
 
17445
  float      real_data;
 
17446
  double     double_data;
 
17447
  ulong      length[7];
 
17448
  my_bool    is_null[7];
 
17449
  MYSQL_BIND my_bind[7];
 
17450
  int rc;
 
17451
  int i;
 
17452
 
 
17453
  myheader("test_wl4166_1");
 
17454
 
 
17455
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS table_4166");
 
17456
  myquery(rc);
 
17457
 
 
17458
  rc= mysql_query(mysql, "CREATE TABLE table_4166(col1 tinyint NOT NULL, "
 
17459
                         "col2 varchar(15), col3 int, "
 
17460
                         "col4 smallint, col5 bigint, "
 
17461
                         "col6 float, col7 double, "
 
17462
                         "colX varchar(10) default NULL)");
 
17463
  myquery(rc);
 
17464
 
 
17465
  stmt= mysql_simple_prepare(mysql,
 
17466
    "INSERT INTO table_4166(col1, col2, col3, col4, col5, col6, col7) "
 
17467
    "VALUES(?, ?, ?, ?, ?, ?, ?)");
 
17468
  check_stmt(stmt);
 
17469
 
 
17470
  verify_param_count(stmt, 7);
 
17471
 
 
17472
  bzero(my_bind, sizeof(my_bind));
 
17473
  /* tinyint */
 
17474
  my_bind[0].buffer_type= MYSQL_TYPE_TINY;
 
17475
  my_bind[0].buffer= (void *)&tiny_data;
 
17476
  /* string */
 
17477
  my_bind[1].buffer_type= MYSQL_TYPE_STRING;
 
17478
  my_bind[1].buffer= (void *)str_data;
 
17479
  my_bind[1].buffer_length= 1000;                  /* Max string length */
 
17480
  /* integer */
 
17481
  my_bind[2].buffer_type= MYSQL_TYPE_LONG;
 
17482
  my_bind[2].buffer= (void *)&int_data;
 
17483
  /* short */
 
17484
  my_bind[3].buffer_type= MYSQL_TYPE_SHORT;
 
17485
  my_bind[3].buffer= (void *)&small_data;
 
17486
  /* bigint */
 
17487
  my_bind[4].buffer_type= MYSQL_TYPE_LONGLONG;
 
17488
  my_bind[4].buffer= (void *)&big_data;
 
17489
  /* float */
 
17490
  my_bind[5].buffer_type= MYSQL_TYPE_FLOAT;
 
17491
  my_bind[5].buffer= (void *)&real_data;
 
17492
  /* double */
 
17493
  my_bind[6].buffer_type= MYSQL_TYPE_DOUBLE;
 
17494
  my_bind[6].buffer= (void *)&double_data;
 
17495
 
 
17496
  for (i= 0; i < (int) array_elements(my_bind); i++)
 
17497
  {
 
17498
    my_bind[i].length= &length[i];
 
17499
    my_bind[i].is_null= &is_null[i];
 
17500
    is_null[i]= 0;
 
17501
  }
 
17502
 
 
17503
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
17504
  check_execute(stmt, rc);
 
17505
 
 
17506
  int_data= 320;
 
17507
  small_data= 1867;
 
17508
  big_data= 1000;
 
17509
  real_data= 2;
 
17510
  double_data= 6578.001;
 
17511
 
 
17512
  /* now, execute the prepared statement to insert 10 records.. */
 
17513
  for (tiny_data= 0; tiny_data < 10; tiny_data++)
 
17514
  {
 
17515
    length[1]= my_sprintf(str_data, (str_data, "MySQL%d", int_data));
 
17516
    rc= mysql_stmt_execute(stmt);
 
17517
    check_execute(stmt, rc);
 
17518
    int_data += 25;
 
17519
    small_data += 10;
 
17520
    big_data += 100;
 
17521
    real_data += 1;
 
17522
    double_data += 10.09;
 
17523
  }
 
17524
 
 
17525
  /* force a re-prepare with some DDL */
 
17526
 
 
17527
  rc= mysql_query(mysql,
 
17528
    "ALTER TABLE table_4166 change colX colX varchar(20) default NULL");
 
17529
  myquery(rc);
 
17530
 
 
17531
  /*
 
17532
    execute the prepared statement again,
 
17533
    without changing the types of parameters already bound.
 
17534
  */
 
17535
 
 
17536
  for (tiny_data= 50; tiny_data < 60; tiny_data++)
 
17537
  {
 
17538
    length[1]= my_sprintf(str_data, (str_data, "MySQL%d", int_data));
 
17539
    rc= mysql_stmt_execute(stmt);
 
17540
    check_execute(stmt, rc);
 
17541
    int_data += 25;
 
17542
    small_data += 10;
 
17543
    big_data += 100;
 
17544
    real_data += 1;
 
17545
    double_data += 10.09;
 
17546
  }
 
17547
 
 
17548
  mysql_stmt_close(stmt);
 
17549
 
 
17550
  rc= mysql_query(mysql, "DROP TABLE table_4166");
 
17551
  myquery(rc);
 
17552
}
 
17553
 
 
17554
 
 
17555
static void test_wl4166_2()
 
17556
{
 
17557
  MYSQL_STMT *stmt;
 
17558
  int        c_int;
 
17559
  MYSQL_TIME d_date;
 
17560
  MYSQL_BIND bind_out[2];
 
17561
  int rc;
 
17562
 
 
17563
  myheader("test_wl4166_2");
 
17564
 
 
17565
  rc= mysql_query(mysql, "SET SQL_MODE=''");
 
17566
  myquery(rc);
 
17567
 
 
17568
  rc= mysql_query(mysql, "drop table if exists t1");
 
17569
  myquery(rc);
 
17570
  rc= mysql_query(mysql, "create table t1 (c_int int, d_date date)");
 
17571
  myquery(rc);
 
17572
  rc= mysql_query(mysql,
 
17573
                  "insert into t1 (c_int, d_date) values (42, '1948-05-15')");
 
17574
  myquery(rc);
 
17575
 
 
17576
  stmt= mysql_simple_prepare(mysql, "select * from t1");
 
17577
  check_stmt(stmt);
 
17578
 
 
17579
  bzero(bind_out, sizeof(bind_out));
 
17580
  bind_out[0].buffer_type= MYSQL_TYPE_LONG;
 
17581
  bind_out[0].buffer= (void*) &c_int;
 
17582
 
 
17583
  bind_out[1].buffer_type= MYSQL_TYPE_DATE;
 
17584
  bind_out[1].buffer= (void*) &d_date;
 
17585
 
 
17586
  rc= mysql_stmt_bind_result(stmt, bind_out);
 
17587
  check_execute(stmt, rc);
 
17588
 
 
17589
  /* int -> varchar transition */
 
17590
 
 
17591
  rc= mysql_query(mysql,
 
17592
                  "alter table t1 change column c_int c_int varchar(11)");
 
17593
  myquery(rc);
 
17594
 
 
17595
  rc= mysql_stmt_execute(stmt);
 
17596
  check_execute(stmt, rc);
 
17597
 
 
17598
  rc= mysql_stmt_fetch(stmt);
 
17599
  check_execute(stmt, rc);
 
17600
 
 
17601
  DIE_UNLESS(c_int == 42);
 
17602
  DIE_UNLESS(d_date.year == 1948);
 
17603
  DIE_UNLESS(d_date.month == 5);
 
17604
  DIE_UNLESS(d_date.day == 15);
 
17605
 
 
17606
  rc= mysql_stmt_fetch(stmt);
 
17607
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
17608
 
 
17609
  /* varchar to int retrieval with truncation */
 
17610
 
 
17611
  rc= mysql_query(mysql, "update t1 set c_int='abcde'");
 
17612
  myquery(rc);
 
17613
 
 
17614
  rc= mysql_stmt_execute(stmt);
 
17615
  check_execute(stmt, rc);
 
17616
 
 
17617
  rc= mysql_stmt_fetch(stmt);
 
17618
  check_execute_r(stmt, rc);
 
17619
 
 
17620
  DIE_UNLESS(c_int == 0);
 
17621
 
 
17622
  rc= mysql_stmt_fetch(stmt);
 
17623
  DIE_UNLESS(rc == MYSQL_NO_DATA);
 
17624
 
 
17625
  /* alter table and increase the number of columns */
 
17626
  rc= mysql_query(mysql, "alter table t1 add column d_int int");
 
17627
  myquery(rc);
 
17628
 
 
17629
  rc= mysql_stmt_execute(stmt);
 
17630
  check_execute_r(stmt, rc);
 
17631
 
 
17632
  rc= mysql_stmt_reset(stmt);
 
17633
  check_execute(stmt, rc);
 
17634
 
 
17635
  /* decrease the number of columns */
 
17636
  rc= mysql_query(mysql, "alter table t1 drop d_date, drop d_int");
 
17637
  myquery(rc);
 
17638
 
 
17639
  rc= mysql_stmt_execute(stmt);
 
17640
  check_execute_r(stmt, rc);
 
17641
 
 
17642
  mysql_stmt_close(stmt);
 
17643
  rc= mysql_query(mysql, "drop table t1");
 
17644
  myquery(rc);
 
17645
 
 
17646
}
 
17647
 
 
17648
/**
 
17649
  Bug#38486 Crash when using cursor protocol
 
17650
*/
 
17651
 
 
17652
static void test_bug38486(void)
 
17653
{
 
17654
  MYSQL_STMT *stmt;
 
17655
  const char *stmt_text;
 
17656
  unsigned long type= CURSOR_TYPE_READ_ONLY;
 
17657
 
 
17658
  DBUG_ENTER("test_bug38486");
 
17659
  myheader("test_bug38486");
 
17660
 
 
17661
  stmt= mysql_stmt_init(mysql);
 
17662
  mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*)&type);
 
17663
  stmt_text= "CREATE TABLE t1 (a INT)";
 
17664
  mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
17665
  mysql_stmt_execute(stmt);
 
17666
  mysql_stmt_close(stmt);
 
17667
 
 
17668
  stmt= mysql_stmt_init(mysql);
 
17669
  mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*)&type);
 
17670
  stmt_text= "INSERT INTO t1 VALUES (1)";
 
17671
  mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
 
17672
  mysql_stmt_execute(stmt);
 
17673
  mysql_stmt_close(stmt);
 
17674
 
 
17675
  DBUG_VOID_RETURN;
 
17676
}
 
17677
 
 
17678
static void test_bug40365(void)
 
17679
{
 
17680
  uint         rc, i;
 
17681
  MYSQL_STMT   *stmt= 0;
 
17682
  MYSQL_BIND   my_bind[2];
 
17683
  my_bool      is_null[2]= {0};
 
17684
  MYSQL_TIME   tm[2];
 
17685
 
 
17686
  DBUG_ENTER("test_bug40365");
 
17687
 
 
17688
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
17689
  myquery(rc);
 
17690
  rc= mysql_query(mysql, "CREATE TABLE t1(c1 DATETIME, \
 
17691
                                          c2 DATE)");
 
17692
  myquery(rc);
 
17693
 
 
17694
  stmt= mysql_simple_prepare(mysql, "INSERT INTO t1 VALUES(?, ?)");
 
17695
  check_stmt(stmt);
 
17696
  verify_param_count(stmt, 2);
 
17697
 
 
17698
  bzero((char*) my_bind, sizeof(my_bind));
 
17699
  my_bind[0].buffer_type= MYSQL_TYPE_DATETIME;
 
17700
  my_bind[1].buffer_type= MYSQL_TYPE_DATE;
 
17701
  for (i= 0; i < (int) array_elements(my_bind); i++)
 
17702
  {
 
17703
    my_bind[i].buffer= (void *) &tm[i];
 
17704
    my_bind[i].is_null= &is_null[i];
 
17705
  }
 
17706
 
 
17707
  rc= mysql_stmt_bind_param(stmt, my_bind);
 
17708
  check_execute(stmt, rc);
 
17709
 
 
17710
  for (i= 0; i < (int) array_elements(my_bind); i++)
 
17711
  {
 
17712
    tm[i].neg= 0;
 
17713
    tm[i].second_part= 0;
 
17714
    tm[i].year= 2009;
 
17715
    tm[i].month= 2;
 
17716
    tm[i].day= 29;
 
17717
    tm[i].hour= 0;
 
17718
    tm[i].minute= 0;
 
17719
    tm[i].second= 0;
 
17720
  }
 
17721
  rc= mysql_stmt_execute(stmt);
 
17722
  check_execute(stmt, rc);
 
17723
 
 
17724
  rc= mysql_commit(mysql);
 
17725
  myquery(rc);
 
17726
  mysql_stmt_close(stmt);
 
17727
 
 
17728
  stmt= mysql_simple_prepare(mysql, "SELECT * FROM t1");
 
17729
  check_stmt(stmt);
 
17730
 
 
17731
  rc= mysql_stmt_bind_result(stmt, my_bind);
 
17732
  check_execute(stmt, rc);
 
17733
 
 
17734
  rc= mysql_stmt_execute(stmt);
 
17735
  check_execute(stmt, rc);
 
17736
 
 
17737
  rc= mysql_stmt_store_result(stmt);
 
17738
  check_execute(stmt, rc);
 
17739
 
 
17740
  rc= mysql_stmt_fetch(stmt);
 
17741
  check_execute(stmt, rc);
 
17742
 
 
17743
  if (!opt_silent)
 
17744
    fprintf(stdout, "\n");
 
17745
 
 
17746
  for (i= 0; i < array_elements(my_bind); i++)
 
17747
  {
 
17748
    if (!opt_silent)
 
17749
      fprintf(stdout, "\ntime[%d]: %02d-%02d-%02d ",
 
17750
              i, tm[i].year, tm[i].month, tm[i].day);
 
17751
      DIE_UNLESS(tm[i].year == 0);
 
17752
      DIE_UNLESS(tm[i].month == 0);
 
17753
      DIE_UNLESS(tm[i].day == 0);
 
17754
  }
 
17755
  mysql_stmt_close(stmt);
 
17756
 
 
17757
  DBUG_VOID_RETURN;
 
17758
}
 
17759
 
 
17760
 
 
17761
/**
 
17762
  Subtest for Bug#43560. Verifies that a loss of connection on the server side
 
17763
  is handled well by the mysql_stmt_execute() call, i.e., no SIGSEGV due to
 
17764
  a vio socket that is cleared upon closed connection.
 
17765
 
 
17766
  Assumes the presence of the close_conn_after_stmt_execute debug feature in
 
17767
  the server. Verifies that it is connected to a debug server before proceeding
 
17768
  with the test.
 
17769
 */
 
17770
static void test_bug43560(void)
 
17771
{
 
17772
  MYSQL*       conn;
 
17773
  uint         rc;
 
17774
  MYSQL_STMT   *stmt= 0;
 
17775
  MYSQL_BIND   bind;
 
17776
  my_bool      is_null= 0;
 
17777
  char         buffer[256];
 
17778
  const uint   BUFSIZE= sizeof(buffer);
 
17779
  const char*  values[] = {"eins", "zwei", "drei", "viele", NULL};
 
17780
  const char   insert_str[] = "INSERT INTO t1 (c2) VALUES (?)";
 
17781
  unsigned long length;
 
17782
  
 
17783
  DBUG_ENTER("test_bug43560");
 
17784
  myheader("test_bug43560");
 
17785
 
 
17786
  /* Make sure we only run against a debug server. */
 
17787
  if (!strstr(mysql->server_version, "debug"))
 
17788
  {
 
17789
    fprintf(stdout, "Skipping test_bug43560: server not DEBUG version\n");
 
17790
    DBUG_VOID_RETURN;
 
17791
  }
 
17792
 
 
17793
  /*
 
17794
    Set up a separate connection for this test to avoid messing up the
 
17795
    general MYSQL object used in other subtests. Use TCP protocol to avoid
 
17796
    problems with the buffer semantics of AF_UNIX, and turn off auto reconnect.
 
17797
  */
 
17798
  conn= client_connect(0, MYSQL_PROTOCOL_TCP, 0);
 
17799
 
 
17800
  rc= mysql_query(conn, "DROP TABLE IF EXISTS t1");
 
17801
  myquery(rc);
 
17802
  rc= mysql_query(conn,
 
17803
    "CREATE TABLE t1 (c1 INT PRIMARY KEY AUTO_INCREMENT, c2 CHAR(10))");
 
17804
  myquery(rc);
 
17805
 
 
17806
  stmt= mysql_stmt_init(conn);
 
17807
  check_stmt(stmt);
 
17808
  rc= mysql_stmt_prepare(stmt, insert_str, strlen(insert_str));
 
17809
  check_execute(stmt, rc);
 
17810
 
 
17811
  bind.buffer_type= MYSQL_TYPE_STRING;
 
17812
  bind.buffer_length= BUFSIZE;
 
17813
  bind.buffer= buffer;
 
17814
  bind.is_null= &is_null;
 
17815
  bind.length= &length;
 
17816
  rc= mysql_stmt_bind_param(stmt, &bind);
 
17817
  check_execute(stmt, rc);
 
17818
 
 
17819
  /* First execute; should succeed. */
 
17820
  strncpy(buffer, values[0], BUFSIZE);
 
17821
  length= strlen(buffer);
 
17822
  rc= mysql_stmt_execute(stmt);
 
17823
  check_execute(stmt, rc);
 
17824
 
 
17825
  /* 
 
17826
    Set up the server to close this session's server-side socket after
 
17827
    next execution of prep statement.
 
17828
  */
 
17829
  rc= mysql_query(conn,"SET SESSION debug='+d,close_conn_after_stmt_execute'");
 
17830
  myquery(rc);
 
17831
 
 
17832
  /* Second execute; should fail due to socket closed during execution. */
 
17833
  strncpy(buffer, values[1], BUFSIZE);
 
17834
  length= strlen(buffer);
 
17835
  rc= mysql_stmt_execute(stmt);
 
17836
  DIE_UNLESS(rc && mysql_stmt_errno(stmt) == CR_SERVER_LOST);
 
17837
 
 
17838
  /* 
 
17839
    Third execute; should fail (connection already closed), or SIGSEGV in
 
17840
    case of a Bug#43560 type regression in which case the whole test fails.
 
17841
  */
 
17842
  strncpy(buffer, values[2], BUFSIZE);
 
17843
  length= strlen(buffer);
 
17844
  rc= mysql_stmt_execute(stmt);
 
17845
  DIE_UNLESS(rc && mysql_stmt_errno(stmt) == CR_SERVER_LOST);
 
17846
 
 
17847
  client_disconnect(conn, 0);
 
17848
  rc= mysql_query(mysql, "DROP TABLE t1");
 
17849
  myquery(rc);
 
17850
 
 
17851
  DBUG_VOID_RETURN;
 
17852
}
 
17853
 
 
17854
 
 
17855
/**
 
17856
  Bug#36326: nested transaction and select
 
17857
*/
 
17858
 
 
17859
#ifdef HAVE_QUERY_CACHE
 
17860
 
 
17861
static void test_bug36326()
 
17862
{
 
17863
  int rc;
 
17864
 
 
17865
  DBUG_ENTER("test_bug36326");
 
17866
  myheader("test_bug36326");
 
17867
 
 
17868
  rc= mysql_autocommit(mysql, TRUE);
 
17869
  myquery(rc);
 
17870
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
 
17871
  myquery(rc);
 
17872
  rc= mysql_query(mysql, "CREATE  TABLE t1 (a INTEGER)");
 
17873
  myquery(rc);
 
17874
  rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1)");
 
17875
  myquery(rc);
 
17876
  rc= mysql_query(mysql, "SET GLOBAL query_cache_type = 1");
 
17877
  myquery(rc);
 
17878
  rc= mysql_query(mysql, "SET GLOBAL query_cache_size = 1048576");
 
17879
  myquery(rc);
 
17880
  DIE_UNLESS(!(mysql->server_status & SERVER_STATUS_IN_TRANS));
 
17881
  DIE_UNLESS(mysql->server_status & SERVER_STATUS_AUTOCOMMIT);
 
17882
  rc= mysql_query(mysql, "BEGIN");
 
17883
  myquery(rc);
 
17884
  DIE_UNLESS(mysql->server_status & SERVER_STATUS_IN_TRANS);
 
17885
  rc= mysql_query(mysql, "SELECT * FROM t1");
 
17886
  myquery(rc);
 
17887
  rc= my_process_result(mysql);
 
17888
  DIE_UNLESS(rc == 1);
 
17889
  rc= mysql_rollback(mysql);
 
17890
  myquery(rc);
 
17891
  rc= mysql_query(mysql, "ROLLBACK");
 
17892
  myquery(rc);
 
17893
  DIE_UNLESS(!(mysql->server_status & SERVER_STATUS_IN_TRANS));
 
17894
  rc= mysql_query(mysql, "SELECT * FROM t1");
 
17895
  myquery(rc);
 
17896
  DIE_UNLESS(!(mysql->server_status & SERVER_STATUS_IN_TRANS));
 
17897
  rc= my_process_result(mysql);
 
17898
  DIE_UNLESS(rc == 1);
 
17899
  rc= mysql_query(mysql, "DROP TABLE t1");
 
17900
  myquery(rc);
 
17901
  rc= mysql_query(mysql, "SET GLOBAL query_cache_size = 0");
 
17902
  myquery(rc);
 
17903
 
 
17904
  DBUG_VOID_RETURN;
 
17905
}
 
17906
 
 
17907
#endif
 
17908
 
 
17909
/**
 
17910
  Bug#41078: With CURSOR_TYPE_READ_ONLY mysql_stmt_fetch() returns short
 
17911
             string value.
 
17912
*/
 
17913
 
 
17914
static void test_bug41078(void)
 
17915
{
 
17916
  uint         rc;
 
17917
  MYSQL_STMT   *stmt= 0;
 
17918
  MYSQL_BIND   param, result;
 
17919
  ulong        cursor_type= CURSOR_TYPE_READ_ONLY;
 
17920
  ulong        len;
 
17921
  char         str[64];
 
17922
  const char   param_str[]= "abcdefghijklmn";
 
17923
  my_bool      is_null, error;
 
17924
 
 
17925
  DBUG_ENTER("test_bug41078");
 
17926
 
 
17927
  rc= mysql_query(mysql, "SET NAMES UTF8");
 
17928
  myquery(rc);
 
17929
 
 
17930
  stmt= mysql_simple_prepare(mysql, "SELECT ?");
 
17931
  check_stmt(stmt);
 
17932
  verify_param_count(stmt, 1);
 
17933
 
 
17934
  rc= mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, &cursor_type);
 
17935
  check_execute(stmt, rc);
 
17936
  
 
17937
  bzero(&param, sizeof(param));
 
17938
  param.buffer_type= MYSQL_TYPE_STRING;
 
17939
  param.buffer= (void *) param_str;
 
17940
  len= sizeof(param_str) - 1;
 
17941
  param.length= &len;
 
17942
 
 
17943
  rc= mysql_stmt_bind_param(stmt, &param);
 
17944
  check_execute(stmt, rc);
 
17945
 
 
17946
  rc= mysql_stmt_execute(stmt);
 
17947
  check_execute(stmt, rc);
 
17948
 
 
17949
  bzero(&result, sizeof(result));
 
17950
  result.buffer_type= MYSQL_TYPE_STRING;
 
17951
  result.buffer= str;
 
17952
  result.buffer_length= sizeof(str);
 
17953
  result.is_null= &is_null;
 
17954
  result.length= &len;
 
17955
  result.error=  &error;
 
17956
  
 
17957
  rc= mysql_stmt_bind_result(stmt, &result);
 
17958
  check_execute(stmt, rc);
 
17959
 
 
17960
  rc= mysql_stmt_store_result(stmt);
 
17961
  check_execute(stmt, rc);
 
17962
 
 
17963
  rc= mysql_stmt_fetch(stmt);
 
17964
  check_execute(stmt, rc);
 
17965
 
 
17966
  DIE_UNLESS(len == sizeof(param_str) - 1 && !strcmp(str, param_str));
 
17967
 
 
17968
  mysql_stmt_close(stmt);
 
17969
 
 
17970
  DBUG_VOID_RETURN;
 
17971
}
 
17972
 
 
17973
/**
 
17974
  Bug#45010: invalid memory reads during parsing some strange statements
 
17975
*/
 
17976
static void test_bug45010()
 
17977
{
 
17978
  int rc;
 
17979
  const char query1[]= "select a.\x80",
 
17980
             query2[]= "describe `table\xef";
 
17981
 
 
17982
  DBUG_ENTER("test_bug45010");
 
17983
  myheader("test_bug45010");
 
17984
 
 
17985
  rc= mysql_query(mysql, "set names utf8");
 
17986
  myquery(rc);
 
17987
 
 
17988
  /* \x80 (-128) could be used as a index of ident_map. */
 
17989
  rc= mysql_real_query(mysql, query1, sizeof(query1) - 1);
 
17990
  DIE_UNLESS(rc);
 
17991
 
 
17992
  /* \xef (-17) could be used to skip 3 bytes past the buffer end. */
 
17993
  rc= mysql_real_query(mysql, query2, sizeof(query2) - 1);
 
17994
  DIE_UNLESS(rc);
 
17995
 
 
17996
  rc= mysql_query(mysql, "set names default");
 
17997
  myquery(rc);
 
17998
 
 
17999
  DBUG_VOID_RETURN;
 
18000
}
 
18001
 
 
18002
/**
 
18003
  Bug#44495: Prepared Statement:
 
18004
             CALL p(<x>) - `thd->protocol == &thd->protocol_text' failed
 
18005
*/
 
18006
 
 
18007
static void test_bug44495()
 
18008
{
 
18009
  int rc;
 
18010
  MYSQL con;
 
18011
  MYSQL_STMT *stmt;
 
18012
 
 
18013
  DBUG_ENTER("test_bug44495");
 
18014
  myheader("test_44495");
 
18015
 
 
18016
  rc= mysql_query(mysql, "DROP PROCEDURE IF EXISTS p1");
 
18017
  myquery(rc);
 
18018
 
 
18019
  rc= mysql_query(mysql, "CREATE PROCEDURE p1(IN arg VARCHAR(25))"
 
18020
                         "  BEGIN SET @stmt = CONCAT('SELECT \"', arg, '\"');"
 
18021
                         "  PREPARE ps1 FROM @stmt;"
 
18022
                         "  EXECUTE ps1;"
 
18023
                         "  DROP PREPARE ps1;"
 
18024
                         "END;");
 
18025
  myquery(rc);
 
18026
 
 
18027
  DIE_UNLESS(mysql_client_init(&con));
 
18028
 
 
18029
  DIE_UNLESS(mysql_real_connect(&con, opt_host, opt_user, opt_password,
 
18030
                                current_db, opt_port, opt_unix_socket,
 
18031
                                CLIENT_MULTI_RESULTS));
 
18032
 
 
18033
  stmt= mysql_simple_prepare(&con, "CALL p1('abc')");
 
18034
  check_stmt(stmt);
 
18035
 
 
18036
  rc= mysql_stmt_execute(stmt);
 
18037
  check_execute(stmt, rc);
 
18038
 
 
18039
  rc= my_process_stmt_result(stmt);
 
18040
  DIE_UNLESS(rc == 1);
 
18041
 
 
18042
  mysql_stmt_close(stmt);
 
18043
 
 
18044
  mysql_close(&con);
 
18045
 
 
18046
  rc= mysql_query(mysql, "DROP PROCEDURE p1");
 
18047
  myquery(rc);
 
18048
 
 
18049
  DBUG_VOID_RETURN;
 
18050
}
 
18051
 
 
18052
/*
 
18053
  Read and parse arguments and MySQL options from my.cnf
 
18054
*/
 
18055
 
 
18056
static const char *client_test_load_default_groups[]= { "client", 0 };
 
18057
static char **defaults_argv;
 
18058
 
 
18059
static struct my_option client_test_long_options[] =
 
18060
{
 
18061
  {"basedir", 'b', "Basedir for tests.", (uchar**) &opt_basedir,
 
18062
   (uchar**) &opt_basedir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
18063
  {"count", 't', "Number of times test to be executed", (uchar **) &opt_count,
 
18064
   (uchar **) &opt_count, 0, GET_UINT, REQUIRED_ARG, 1, 0, 0, 0, 0, 0},
 
18065
  {"database", 'D', "Database to use", (uchar **) &opt_db, (uchar **) &opt_db,
 
18066
   0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
18067
  {"debug", '#', "Output debug log", (uchar**) &default_dbug_option,
 
18068
   (uchar**) &default_dbug_option, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
18069
  {"help", '?', "Display this help and exit", 0, 0, 0, GET_NO_ARG, NO_ARG, 0,
 
18070
   0, 0, 0, 0, 0},
 
18071
  {"host", 'h', "Connect to host", (uchar **) &opt_host, (uchar **) &opt_host,
 
18072
   0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
18073
  {"password", 'p',
 
18074
   "Password to use when connecting to server. If password is not given it's asked from the tty.",
 
18075
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
18076
  {"port", 'P', "Port number to use for connection or 0 for default to, in "
 
18077
   "order of preference, my.cnf, $MYSQL_TCP_PORT, "
 
18078
#if MYSQL_PORT_DEFAULT == 0
 
18079
   "/etc/services, "
 
18080
#endif
 
18081
   "built-in default (" STRINGIFY_ARG(MYSQL_PORT) ").",
 
18082
   (uchar **) &opt_port,
 
18083
   (uchar **) &opt_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
18084
  {"server-arg", 'A', "Send embedded server this as a parameter.",
 
18085
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
18086
  {"show-tests", 'T', "Show all tests' names", 0, 0, 0, GET_NO_ARG, NO_ARG,
 
18087
   0, 0, 0, 0, 0, 0},
 
18088
  {"silent", 's', "Be more silent", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0,
 
18089
   0},
 
18090
#ifdef HAVE_SMEM
 
18091
  {"shared-memory-base-name", 'm', "Base name of shared memory.", 
 
18092
  (uchar**) &shared_memory_base_name, (uchar**)&shared_memory_base_name, 0, 
 
18093
  GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
18094
#endif
 
18095
  {"socket", 'S', "Socket file to use for connection",
 
18096
   (uchar **) &opt_unix_socket, (uchar **) &opt_unix_socket, 0, GET_STR,
 
18097
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
18098
  {"testcase", 'c',
 
18099
   "May disable some code when runs as mysql-test-run testcase.",
 
18100
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
18101
#ifndef DONT_ALLOW_USER_CHANGE
 
18102
  {"user", 'u', "User for login if not current user", (uchar **) &opt_user,
 
18103
   (uchar **) &opt_user, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
18104
#endif
 
18105
  {"vardir", 'v', "Data dir for tests.", (uchar**) &opt_vardir,
 
18106
   (uchar**) &opt_vardir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
18107
  {"getopt-ll-test", 'g', "Option for testing bug in getopt library",
 
18108
   (uchar **) &opt_getopt_ll_test, (uchar **) &opt_getopt_ll_test, 0,
 
18109
   GET_LL, REQUIRED_ARG, 0, 0, LONGLONG_MAX, 0, 0, 0},
 
18110
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
 
18111
};
 
18112
 
 
18113
 
 
18114
static void usage(void)
 
18115
{
 
18116
  /* show the usage string when the user asks for this */
 
18117
  putc('\n', stdout);
 
18118
  printf("%s  Ver %s Distrib %s, for %s (%s)\n",
 
18119
         my_progname, VER, MYSQL_SERVER_VERSION, SYSTEM_TYPE, MACHINE_TYPE);
 
18120
  puts("By Monty, Venu, Kent and others\n");
 
18121
  printf("\
 
18122
Copyright (C) 2002-2004 MySQL AB\n\
 
18123
This software comes with ABSOLUTELY NO WARRANTY. This is free software,\n\
 
18124
and you are welcome to modify and redistribute it under the GPL license\n");
 
18125
  printf("Usage: %s [OPTIONS] [TESTNAME1 TESTNAME2...]\n", my_progname);
 
18126
  my_print_help(client_test_long_options);
 
18127
  print_defaults("my", client_test_load_default_groups);
 
18128
  my_print_variables(client_test_long_options);
 
18129
}
 
18130
 
 
18131
 
 
18132
static struct my_tests_st my_tests[]= {
 
18133
  { "disable_general_log", disable_general_log },
 
18134
  { "test_view_sp_list_fields", test_view_sp_list_fields },
 
18135
  { "client_query", client_query },
 
18136
  { "test_prepare_insert_update", test_prepare_insert_update},
 
18137
#if NOT_YET_WORKING
 
18138
  { "test_drop_temp", test_drop_temp },
 
18139
#endif
 
18140
  { "test_fetch_seek", test_fetch_seek },
 
18141
  { "test_fetch_nobuffs", test_fetch_nobuffs },
 
18142
  { "test_open_direct", test_open_direct },
 
18143
  { "test_fetch_null", test_fetch_null },
 
18144
  { "test_ps_null_param", test_ps_null_param },
 
18145
  { "test_fetch_date", test_fetch_date },
 
18146
  { "test_fetch_str", test_fetch_str },
 
18147
  { "test_fetch_long", test_fetch_long },
 
18148
  { "test_fetch_short", test_fetch_short },
 
18149
  { "test_fetch_tiny", test_fetch_tiny },
 
18150
  { "test_fetch_bigint", test_fetch_bigint },
 
18151
  { "test_fetch_float", test_fetch_float },
 
18152
  { "test_fetch_double", test_fetch_double },
 
18153
  { "test_bind_result_ext", test_bind_result_ext },
 
18154
  { "test_bind_result_ext1", test_bind_result_ext1 },
 
18155
  { "test_select_direct", test_select_direct },
 
18156
  { "test_select_prepare", test_select_prepare },
 
18157
  { "test_select", test_select },
 
18158
  { "test_select_version", test_select_version },
 
18159
  { "test_ps_conj_select", test_ps_conj_select },
 
18160
  { "test_select_show_table", test_select_show_table },
 
18161
  { "test_func_fields", test_func_fields },
 
18162
  { "test_long_data", test_long_data },
 
18163
  { "test_insert", test_insert },
 
18164
  { "test_set_variable", test_set_variable },
 
18165
  { "test_select_show", test_select_show },
 
18166
  { "test_prepare_noparam", test_prepare_noparam },
 
18167
  { "test_bind_result", test_bind_result },
 
18168
  { "test_prepare_simple", test_prepare_simple },
 
18169
  { "test_prepare", test_prepare },
 
18170
  { "test_null", test_null },
 
18171
  { "test_debug_example", test_debug_example },
 
18172
  { "test_update", test_update },
 
18173
  { "test_simple_update", test_simple_update },
 
18174
  { "test_simple_delete", test_simple_delete },
 
18175
  { "test_double_compare", test_double_compare },
 
18176
  { "client_store_result", client_store_result },
 
18177
  { "client_use_result", client_use_result },
 
18178
  { "test_tran_bdb", test_tran_bdb },
 
18179
  { "test_tran_innodb", test_tran_innodb },
 
18180
  { "test_prepare_ext", test_prepare_ext },
 
18181
  { "test_prepare_syntax", test_prepare_syntax },
 
18182
  { "test_field_names", test_field_names },
 
18183
  { "test_field_flags", test_field_flags },
 
18184
  { "test_long_data_str", test_long_data_str },
 
18185
  { "test_long_data_str1", test_long_data_str1 },
 
18186
  { "test_long_data_bin", test_long_data_bin },
 
18187
  { "test_warnings", test_warnings },
 
18188
  { "test_errors", test_errors },
 
18189
  { "test_prepare_resultset", test_prepare_resultset },
 
18190
  { "test_stmt_close", test_stmt_close },
 
18191
  { "test_prepare_field_result", test_prepare_field_result },
 
18192
  { "test_multi_stmt", test_multi_stmt },
 
18193
  { "test_multi_statements", test_multi_statements },
 
18194
  { "test_prepare_multi_statements", test_prepare_multi_statements },
 
18195
  { "test_store_result", test_store_result },
 
18196
  { "test_store_result1", test_store_result1 },
 
18197
  { "test_store_result2", test_store_result2 },
 
18198
  { "test_subselect", test_subselect },
 
18199
  { "test_date", test_date },
 
18200
  { "test_date_date", test_date_date },
 
18201
  { "test_date_time", test_date_time },
 
18202
  { "test_date_ts", test_date_ts },
 
18203
  { "test_date_dt", test_date_dt },
 
18204
  { "test_prepare_alter", test_prepare_alter },
 
18205
  { "test_manual_sample", test_manual_sample },
 
18206
  { "test_pure_coverage", test_pure_coverage },
 
18207
  { "test_buffers", test_buffers },
 
18208
  { "test_ushort_bug", test_ushort_bug },
 
18209
  { "test_sshort_bug", test_sshort_bug },
 
18210
  { "test_stiny_bug", test_stiny_bug },
 
18211
  { "test_field_misc", test_field_misc },
 
18212
  { "test_set_option", test_set_option },
 
18213
#ifndef EMBEDDED_LIBRARY
 
18214
  { "test_prepare_grant", test_prepare_grant },
 
18215
#endif
 
18216
  { "test_frm_bug", test_frm_bug },
 
18217
  { "test_explain_bug", test_explain_bug },
 
18218
  { "test_decimal_bug", test_decimal_bug },
 
18219
  { "test_nstmts", test_nstmts },
 
18220
  { "test_logs;", test_logs },
 
18221
  { "test_cuted_rows", test_cuted_rows },
 
18222
  { "test_fetch_offset", test_fetch_offset },
 
18223
  { "test_fetch_column", test_fetch_column },
 
18224
  { "test_mem_overun", test_mem_overun },
 
18225
  { "test_list_fields", test_list_fields },
 
18226
  { "test_free_result", test_free_result },
 
18227
  { "test_free_store_result", test_free_store_result },
 
18228
  { "test_sqlmode", test_sqlmode },
 
18229
  { "test_ts", test_ts },
 
18230
  { "test_bug1115", test_bug1115 },
 
18231
  { "test_bug1180", test_bug1180 },
 
18232
  { "test_bug1500", test_bug1500 },
 
18233
  { "test_bug1644", test_bug1644 },
 
18234
  { "test_bug1946", test_bug1946 },
 
18235
  { "test_bug2248", test_bug2248 },
 
18236
  { "test_parse_error_and_bad_length", test_parse_error_and_bad_length },
 
18237
  { "test_bug2247", test_bug2247 },
 
18238
  { "test_subqueries", test_subqueries },
 
18239
  { "test_bad_union", test_bad_union },
 
18240
  { "test_distinct", test_distinct },
 
18241
  { "test_subqueries_ref", test_subqueries_ref },
 
18242
  { "test_union", test_union },
 
18243
  { "test_bug3117", test_bug3117 },
 
18244
  { "test_join", test_join },
 
18245
  { "test_selecttmp", test_selecttmp },
 
18246
  { "test_create_drop", test_create_drop },
 
18247
  { "test_rename", test_rename },
 
18248
  { "test_do_set", test_do_set },
 
18249
  { "test_multi", test_multi },
 
18250
  { "test_insert_select", test_insert_select },
 
18251
  { "test_bind_nagative", test_bind_nagative },
 
18252
  { "test_derived", test_derived },
 
18253
  { "test_xjoin", test_xjoin },
 
18254
  { "test_bug3035", test_bug3035 },
 
18255
  { "test_union2", test_union2 },
 
18256
  { "test_bug1664", test_bug1664 },
 
18257
  { "test_union_param", test_union_param },
 
18258
  { "test_order_param", test_order_param },
 
18259
  { "test_ps_i18n", test_ps_i18n },
 
18260
  { "test_bug3796", test_bug3796 },
 
18261
  { "test_bug4026", test_bug4026 },
 
18262
  { "test_bug4079", test_bug4079 },
 
18263
  { "test_bug4236", test_bug4236 },
 
18264
  { "test_bug4030", test_bug4030 },
 
18265
  { "test_bug5126", test_bug5126 },
 
18266
  { "test_bug4231", test_bug4231 },
 
18267
  { "test_bug5399", test_bug5399 },
 
18268
  { "test_bug5194", test_bug5194 },
 
18269
  { "test_bug5315", test_bug5315 },
 
18270
  { "test_bug6049", test_bug6049 },
 
18271
  { "test_bug6058", test_bug6058 },
 
18272
  { "test_bug6059", test_bug6059 },
 
18273
  { "test_bug6046", test_bug6046 },
 
18274
  { "test_bug6081", test_bug6081 },
 
18275
  { "test_bug6096", test_bug6096 },
 
18276
  { "test_datetime_ranges", test_datetime_ranges },
 
18277
  { "test_bug4172", test_bug4172 },
 
18278
  { "test_conversion", test_conversion },
 
18279
  { "test_rewind", test_rewind },
 
18280
  { "test_bug6761", test_bug6761 },
 
18281
  { "test_view", test_view },
 
18282
  { "test_view_where", test_view_where },
 
18283
  { "test_view_2where", test_view_2where },
 
18284
  { "test_view_star", test_view_star },
 
18285
  { "test_view_insert", test_view_insert },
 
18286
  { "test_left_join_view", test_left_join_view },
 
18287
  { "test_view_insert_fields", test_view_insert_fields },
 
18288
  { "test_basic_cursors", test_basic_cursors },
 
18289
  { "test_cursors_with_union", test_cursors_with_union },
 
18290
  { "test_cursors_with_procedure", test_cursors_with_procedure },
 
18291
  { "test_truncation", test_truncation },
 
18292
  { "test_truncation_option", test_truncation_option },
 
18293
  { "test_client_character_set", test_client_character_set },
 
18294
  { "test_bug8330", test_bug8330 },
 
18295
  { "test_bug7990", test_bug7990 },
 
18296
  { "test_bug8378", test_bug8378 },
 
18297
  { "test_bug8722", test_bug8722 },
 
18298
  { "test_bug8880", test_bug8880 },
 
18299
  { "test_bug9159", test_bug9159 },
 
18300
  { "test_bug9520", test_bug9520 },
 
18301
  { "test_bug9478", test_bug9478 },
 
18302
  { "test_bug9643", test_bug9643 },
 
18303
  { "test_bug10729", test_bug10729 },
 
18304
  { "test_bug11111", test_bug11111 },
 
18305
  { "test_bug9992", test_bug9992 },
 
18306
  { "test_bug10736", test_bug10736 },
 
18307
  { "test_bug10794", test_bug10794 },
 
18308
  { "test_bug11172", test_bug11172 },
 
18309
  { "test_bug11656", test_bug11656 },
 
18310
  { "test_bug10214", test_bug10214 },
 
18311
  { "test_bug9735", test_bug9735 },
 
18312
  { "test_bug11183", test_bug11183 },
 
18313
  { "test_bug11037", test_bug11037 },
 
18314
  { "test_bug10760", test_bug10760 },
 
18315
  { "test_bug12001", test_bug12001 },
 
18316
  { "test_bug11718", test_bug11718 },
 
18317
  { "test_bug12925", test_bug12925 },
 
18318
  { "test_bug11909", test_bug11909 },
 
18319
  { "test_bug11901", test_bug11901 },
 
18320
  { "test_bug11904", test_bug11904 },
 
18321
  { "test_bug12243", test_bug12243 },
 
18322
  { "test_bug14210", test_bug14210 },
 
18323
  { "test_bug13488", test_bug13488 },
 
18324
  { "test_bug13524", test_bug13524 },
 
18325
  { "test_bug14845", test_bug14845 },
 
18326
  { "test_opt_reconnect", test_opt_reconnect },
 
18327
  { "test_bug15510", test_bug15510},
 
18328
#ifndef EMBEDDED_LIBRARY
 
18329
  { "test_bug12744", test_bug12744 },
 
18330
#endif
 
18331
  { "test_bug16143", test_bug16143 },
 
18332
  { "test_bug16144", test_bug16144 },
 
18333
  { "test_bug15613", test_bug15613 },
 
18334
  { "test_bug20152", test_bug20152 },
 
18335
  { "test_bug14169", test_bug14169 },
 
18336
  { "test_bug17667", test_bug17667 },
 
18337
  { "test_bug15752", test_bug15752 },
 
18338
  { "test_mysql_insert_id", test_mysql_insert_id },
 
18339
  { "test_bug19671", test_bug19671 },
 
18340
  { "test_bug21206", test_bug21206 },
 
18341
  { "test_bug21726", test_bug21726 },
 
18342
  { "test_bug15518", test_bug15518 },
 
18343
  { "test_bug23383", test_bug23383 },
 
18344
  { "test_bug32265", test_bug32265 },
 
18345
  { "test_bug21635", test_bug21635 },
 
18346
  { "test_status",   test_status   },
 
18347
  { "test_bug24179", test_bug24179 },
 
18348
  { "test_ps_query_cache", test_ps_query_cache },
 
18349
  { "test_bug28075", test_bug28075 },
 
18350
  { "test_bug27876", test_bug27876 },
 
18351
  { "test_bug28505", test_bug28505 },
 
18352
  { "test_bug28934", test_bug28934 },
 
18353
  { "test_bug27592", test_bug27592 },
 
18354
  { "test_bug29687", test_bug29687 },
 
18355
  { "test_bug29692", test_bug29692 },
 
18356
  { "test_bug29306", test_bug29306 },
 
18357
  { "test_change_user", test_change_user },
 
18358
  { "test_bug30472", test_bug30472 },
 
18359
  { "test_bug20023", test_bug20023 },
 
18360
  { "test_bug45010", test_bug45010 },
 
18361
  { "test_bug31418", test_bug31418 },
 
18362
  { "test_bug31669", test_bug31669 },
 
18363
  { "test_bug28386", test_bug28386 },
 
18364
  { "test_wl4166_1", test_wl4166_1 },
 
18365
  { "test_wl4166_2", test_wl4166_2 },
 
18366
  { "test_bug38486", test_bug38486 },
 
18367
  { "test_bug40365", test_bug40365 },
 
18368
  { "test_bug43560", test_bug43560 },
 
18369
#ifdef HAVE_QUERY_CACHE
 
18370
  { "test_bug36326", test_bug36326 },
 
18371
#endif
 
18372
  { "test_bug41078", test_bug41078 },
 
18373
  { "test_bug44495", test_bug44495 },
 
18374
  { 0, 0 }
 
18375
};
 
18376
 
 
18377
 
 
18378
static my_bool
 
18379
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
 
18380
               char *argument)
 
18381
{
 
18382
  switch (optid) {
 
18383
  case '#':
 
18384
    DBUG_PUSH(argument ? argument : default_dbug_option);
 
18385
    break;
 
18386
  case 'c':
 
18387
    opt_testcase = 1;
 
18388
    break;
 
18389
  case 'p':
 
18390
    if (argument)
 
18391
    {
 
18392
      char *start=argument;
 
18393
      my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR));
 
18394
      opt_password= my_strdup(argument, MYF(MY_FAE));
 
18395
      while (*argument) *argument++= 'x';               /* Destroy argument */
 
18396
      if (*start)
 
18397
        start[1]=0;
 
18398
    }
 
18399
    else
 
18400
      tty_password= 1;
 
18401
    break;
 
18402
  case 's':
 
18403
    if (argument == disabled_my_option)
 
18404
      opt_silent= 0;
 
18405
    else
 
18406
      opt_silent++;
 
18407
    break;
 
18408
  case 'A':
 
18409
    /*
 
18410
      When the embedded server is being tested, the test suite needs to be
 
18411
      able to pass command-line arguments to the embedded server so it can
 
18412
      locate the language files and data directory. The test suite
 
18413
      (mysql-test-run) never uses config files, just command-line options.
 
18414
    */
 
18415
    if (!embedded_server_arg_count)
 
18416
    {
 
18417
      embedded_server_arg_count= 1;
 
18418
      embedded_server_args[0]= (char*) "";
 
18419
    }
 
18420
    if (embedded_server_arg_count == MAX_SERVER_ARGS-1 ||
 
18421
        !(embedded_server_args[embedded_server_arg_count++]=
 
18422
          my_strdup(argument, MYF(MY_FAE))))
 
18423
    {
 
18424
      DIE("Can't use server argument");
 
18425
    }
 
18426
    break;
 
18427
  case 'T':
 
18428
    {
 
18429
      struct my_tests_st *fptr;
 
18430
      
 
18431
      printf("All possible test names:\n\n");
 
18432
      for (fptr= my_tests; fptr->name; fptr++)
 
18433
        printf("%s\n", fptr->name);
 
18434
      exit(0);
 
18435
      break;
 
18436
    }
 
18437
  case '?':
 
18438
  case 'I':                                     /* Info */
 
18439
    usage();
 
18440
    exit(0);
 
18441
    break;
 
18442
  }
 
18443
  return 0;
 
18444
}
 
18445
 
 
18446
static void get_options(int *argc, char ***argv)
 
18447
{
 
18448
  int ho_error;
 
18449
 
 
18450
  if ((ho_error= handle_options(argc, argv, client_test_long_options,
 
18451
                                get_one_option)))
 
18452
    exit(ho_error);
 
18453
 
 
18454
  if (tty_password)
 
18455
    opt_password= get_tty_password(NullS);
 
18456
  return;
 
18457
}
 
18458
 
 
18459
/*
 
18460
  Print the test output on successful execution before exiting
 
18461
*/
 
18462
 
 
18463
static void print_test_output()
 
18464
{
 
18465
  if (opt_silent < 3)
 
18466
  {
 
18467
    fprintf(stdout, "\n\n");
 
18468
    fprintf(stdout, "All '%d' tests were successful (in '%d' iterations)",
 
18469
            test_count-1, opt_count);
 
18470
    fprintf(stdout, "\n  Total execution time: %g SECS", total_time);
 
18471
    if (opt_count > 1)
 
18472
      fprintf(stdout, " (Avg: %g SECS)", total_time/opt_count);
 
18473
 
 
18474
    fprintf(stdout, "\n\n!!! SUCCESS !!!\n");
 
18475
  }
 
18476
}
 
18477
 
 
18478
/***************************************************************************
 
18479
  main routine
 
18480
***************************************************************************/
 
18481
 
 
18482
 
 
18483
int main(int argc, char **argv)
 
18484
{
 
18485
  struct my_tests_st *fptr;
 
18486
 
 
18487
  MY_INIT(argv[0]);
 
18488
 
 
18489
  load_defaults("my", client_test_load_default_groups, &argc, &argv);
 
18490
  defaults_argv= argv;
 
18491
  get_options(&argc, &argv);
 
18492
 
 
18493
  if (mysql_server_init(embedded_server_arg_count,
 
18494
                        embedded_server_args,
 
18495
                        (char**) embedded_server_groups))
 
18496
    DIE("Can't initialize MySQL server");
 
18497
 
 
18498
  /* connect to server with no flags, default protocol, auto reconnect true */
 
18499
  mysql= client_connect(0, MYSQL_PROTOCOL_DEFAULT, 1);
 
18500
 
 
18501
  total_time= 0;
 
18502
  for (iter_count= 1; iter_count <= opt_count; iter_count++)
 
18503
  {
 
18504
    /* Start of tests */
 
18505
    test_count= 1;
 
18506
    start_time= time((time_t *)0);
 
18507
    if (!argc)
 
18508
    {
 
18509
      for (fptr= my_tests; fptr->name; fptr++)
 
18510
        (*fptr->function)();    
 
18511
    }
 
18512
    else
 
18513
    {
 
18514
      for ( ; *argv ; argv++)
 
18515
      {
 
18516
        for (fptr= my_tests; fptr->name; fptr++)
 
18517
        {
 
18518
          if (!strcmp(fptr->name, *argv))
 
18519
          {
 
18520
            (*fptr->function)();
 
18521
            break;
 
18522
          }
 
18523
        }
 
18524
        if (!fptr->name)
 
18525
        {
 
18526
          fprintf(stderr, "\n\nGiven test not found: '%s'\n", *argv);
 
18527
          fprintf(stderr, "See legal test names with %s -T\n\nAborting!\n",
 
18528
                  my_progname);
 
18529
          client_disconnect(mysql, 1);
 
18530
          free_defaults(defaults_argv);
 
18531
          exit(1);
 
18532
        }
 
18533
      }
 
18534
    }
 
18535
 
 
18536
    end_time= time((time_t *)0);
 
18537
    total_time+= difftime(end_time, start_time);
 
18538
 
 
18539
    /* End of tests */
 
18540
  }
 
18541
 
 
18542
  client_disconnect(mysql, 1);    /* disconnect from server */
 
18543
 
 
18544
  free_defaults(defaults_argv);
 
18545
  print_test_output();
 
18546
 
 
18547
  while (embedded_server_arg_count > 1)
 
18548
    my_free(embedded_server_args[--embedded_server_arg_count],MYF(0));
 
18549
 
 
18550
  mysql_server_end();
 
18551
 
 
18552
  my_end(0);
 
18553
 
 
18554
  exit(0);
 
18555
}