client_test.c 202 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2003 MySQL AB
unknown's avatar
unknown committed
2

unknown's avatar
unknown committed
3 4 5 6 7 8 9 10 11 12 13 14 15
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
unknown's avatar
unknown committed
16 17

/***************************************************************************
unknown's avatar
unknown committed
18 19
 This is a test sample to test the new features in MySQL client-server
 protocol                                                               
unknown's avatar
unknown committed
20

unknown's avatar
unknown committed
21
 Main author: venu ( venu@mysql.com )
unknown's avatar
unknown committed
22

unknown's avatar
unknown committed
23 24 25 26
 NOTES:
  - To be able to test which fields are used, we are not clearing
    the MYSQL_BIND with bzero() but instead just clearing the fields that
    are used by the API.
unknown's avatar
unknown committed
27

unknown's avatar
unknown committed
28
***************************************************************************/
unknown's avatar
unknown committed
29

unknown's avatar
unknown committed
30
#include <my_global.h>
unknown's avatar
unknown committed
31 32 33
#include <my_sys.h>
#include <mysql.h>
#include <my_getopt.h>
unknown's avatar
unknown committed
34
#include <m_string.h>
35 36
#include <assert.h>

37 38 39

#define MAX_TEST_QUERY_LENGTH 300 /* MAX QUERY BUFFER LENGTH */

unknown's avatar
unknown committed
40
/* set default options */
unknown's avatar
unknown committed
41 42 43
static char *opt_db=0;
static char *opt_user=0;
static char *opt_password=0;
unknown's avatar
unknown committed
44 45
static char *opt_host=0;
static char *opt_unix_socket=0;
46
static unsigned int  opt_port;
unknown's avatar
unknown committed
47
static my_bool tty_password=0;
unknown's avatar
unknown committed
48

unknown's avatar
unknown committed
49
static MYSQL *mysql=0;
50
static char query[MAX_TEST_QUERY_LENGTH]; 
51
static char current_db[]= "client_test_db";
52 53 54
static unsigned int test_count= 0;
static unsigned int opt_count= 0;
static unsigned int iter_count= 0;
unknown's avatar
unknown committed
55

56 57 58
static time_t start_time, end_time;
static double total_time;

59 60 61 62 63 64 65 66 67 68 69 70 71
#define myheader(str) \
{ \
  fprintf(stdout,"\n\n#####################################\n"); \
  fprintf(stdout,"%d of (%d/%d): %s",test_count++, iter_count,\
                                     opt_count, str); \
  fprintf(stdout,"  \n#####################################\n"); \
}
#define myheader_r(str) \
{ \
  fprintf(stdout,"\n\n#####################################\n"); \
  fprintf(stdout,"%s", str); \
  fprintf(stdout,"  \n#####################################\n"); \
}
unknown's avatar
unknown committed
72

73 74
static void print_error(const char *msg);
static void print_st_error(MYSQL_STMT *stmt, const char *msg);
unknown's avatar
unknown committed
75
static void check_errcode(const unsigned int err);
76
static void client_disconnect();
unknown's avatar
unknown committed
77

unknown's avatar
unknown committed
78
#define myerror(msg) print_error(msg)
unknown's avatar
unknown committed
79
#define mysterror(stmt, msg) print_st_error(stmt, msg)
unknown's avatar
unknown committed
80
#define myerrno(n) check_errcode(n)
unknown's avatar
unknown committed
81

unknown's avatar
unknown committed
82 83
#define myassert(exp) assert(exp)
#define myassert_r(exp) assert(!(exp))
unknown's avatar
unknown committed
84

unknown's avatar
unknown committed
85
#define myquery(r) \
unknown's avatar
unknown committed
86
{ \
unknown's avatar
unknown committed
87
if (r) \
unknown's avatar
unknown committed
88
  myerror(NULL); \
89
 myassert(r == 0); \
unknown's avatar
unknown committed
90 91
}

unknown's avatar
unknown committed
92
#define myquery_r(r) \
unknown's avatar
unknown committed
93
{ \
unknown's avatar
unknown committed
94
if (r) \
unknown's avatar
unknown committed
95
  myerror(NULL); \
96
myassert_r(r == 0); \
unknown's avatar
unknown committed
97 98 99 100
}

#define mystmt(stmt,r) \
{ \
unknown's avatar
unknown committed
101
if (r) \
unknown's avatar
unknown committed
102
  mysterror(stmt,NULL); \
103
myassert(r == 0);\
unknown's avatar
unknown committed
104 105
}

106
#define mystmt_r(stmt,r) \
unknown's avatar
unknown committed
107
{ \
unknown's avatar
unknown committed
108
if (r) \
109 110
  mysterror(stmt,NULL); \
myassert_r(r == 0);\
unknown's avatar
unknown committed
111 112
}

113
#define mystmt_init(stmt) \
unknown's avatar
unknown committed
114
{ \
unknown's avatar
unknown committed
115
if ( stmt == 0) \
unknown's avatar
unknown committed
116
  myerror(NULL); \
117 118
myassert(stmt != 0); \
}
unknown's avatar
unknown committed
119

120
#define mystmt_init_r(stmt) \
unknown's avatar
unknown committed
121
{ \
122 123
if (stmt == 0) \
  myerror(NULL);\
124 125
myassert(stmt == 0);\
} 
unknown's avatar
unknown committed
126

unknown's avatar
unknown committed
127 128
#define mytest(x) if (!x) {myerror(NULL);myassert(TRUE);}
#define mytest_r(x) if (x) {myerror(NULL);myassert(TRUE);}
unknown's avatar
unknown committed
129

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
/********************************************************
* print the error message                               *
*********************************************************/
static void print_error(const char *msg)
{  
  if (mysql && mysql_errno(mysql))
  {
    if (mysql->server_version)
      fprintf(stdout,"\n [MySQL-%s]",mysql->server_version);
    else
      fprintf(stdout,"\n [MySQL]");
    fprintf(stdout,"[%d] %s\n",mysql_errno(mysql),mysql_error(mysql));
  }
  else if (msg) fprintf(stderr, " [MySQL] %s\n", msg);
}

unknown's avatar
unknown committed
146 147 148 149 150 151 152 153 154 155
static void check_errcode(const unsigned int err)
{  
  if (mysql->server_version)
    fprintf(stdout,"\n [MySQL-%s]",mysql->server_version);
  else
    fprintf(stdout,"\n [MySQL]");
  fprintf(stdout,"[%d] %s\n",mysql_errno(mysql),mysql_error(mysql));
  myassert(mysql_errno(mysql) == err);
}

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
static void print_st_error(MYSQL_STMT *stmt, const char *msg)
{  
  if (stmt && mysql_stmt_errno(stmt))
  {
    if (stmt->mysql && stmt->mysql->server_version)
      fprintf(stdout,"\n [MySQL-%s]",stmt->mysql->server_version);
    else
      fprintf(stdout,"\n [MySQL]");

    fprintf(stdout,"[%d] %s\n",mysql_stmt_errno(stmt),
            mysql_stmt_error(stmt));
  }
  else if (msg) fprintf(stderr, " [MySQL] %s\n", msg);
}

unknown's avatar
unknown committed
171 172 173
/********************************************************
* connect to the server                                 *
*********************************************************/
unknown's avatar
unknown committed
174
static void client_connect()
unknown's avatar
unknown committed
175
{
unknown's avatar
unknown committed
176
  int  rc;
177
  myheader_r("client_connect");  
unknown's avatar
unknown committed
178

179
  fprintf(stdout, "\n Establishing a connection to '%s' ...", opt_host);
180
  
unknown's avatar
unknown committed
181
  if (!(mysql = mysql_init(NULL)))
unknown's avatar
unknown committed
182
  { 
183
    myerror("mysql_init() failed");
unknown's avatar
unknown committed
184 185
    exit(0);
  }
186
  
unknown's avatar
unknown committed
187
  if (!(mysql_real_connect(mysql,opt_host,opt_user,
188 189
                           opt_password, opt_db ? opt_db:"test", opt_port,
                           opt_unix_socket, 0)))
unknown's avatar
unknown committed
190
  {
191 192
    myerror("connection failed");    
    mysql_close(mysql);
193
    fprintf(stdout,"\n Check the connection options using --help or -?\n");
unknown's avatar
unknown committed
194 195
    exit(0);
  }    
196
  
197
  fprintf(stdout," OK");
unknown's avatar
unknown committed
198 199

  /* set AUTOCOMMIT to ON*/
unknown's avatar
unknown committed
200
  mysql_autocommit(mysql, TRUE);
201
  
202
  fprintf(stdout, "\n Creating a test database '%s' ...", current_db);
203 204 205
  strxmov(query,"CREATE DATABASE IF NOT EXISTS ", current_db, NullS);
  
  rc = mysql_query(mysql, query);
unknown's avatar
unknown committed
206
  myquery(rc);
207 208 209
  
  strxmov(query,"USE ", current_db, NullS);
  rc = mysql_query(mysql, query);
unknown's avatar
unknown committed
210
  myquery(rc);
211 212
  
  fprintf(stdout," OK");
unknown's avatar
unknown committed
213 214 215 216 217
}

/********************************************************
* close the connection                                  *
*********************************************************/
218
static void client_disconnect()
unknown's avatar
unknown committed
219
{  
220
  myheader_r("client_disconnect");  
unknown's avatar
unknown committed
221

222 223
  if (mysql)
  {
224
    fprintf(stdout, "\n droping the test database '%s' ...", current_db);
225 226 227
    strxmov(query,"DROP DATABASE IF EXISTS ", current_db, NullS);
    
    mysql_query(mysql, query);
228
    fprintf(stdout, " OK");
229
    
230
    fprintf(stdout, "\n closing the connection ...");
231
    mysql_close(mysql);
232
    fprintf(stdout, " OK\n");
233
  }
unknown's avatar
unknown committed
234 235 236 237 238
}

/********************************************************
* query processing                                      *
*********************************************************/
239
static void client_query()
unknown's avatar
unknown committed
240 241 242 243 244 245
{
  int rc;

  myheader("client_query");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS myclient_test");
unknown's avatar
unknown committed
246
  myquery(rc);
unknown's avatar
unknown committed
247 248 249

  rc = mysql_query(mysql,"CREATE TABLE myclient_test(id int primary key auto_increment,\
                                              name varchar(20))");
unknown's avatar
unknown committed
250
  myquery(rc);
unknown's avatar
unknown committed
251 252
  
  rc = mysql_query(mysql,"CREATE TABLE myclient_test(id int, name varchar(20))");
unknown's avatar
unknown committed
253
  myquery_r(rc);
unknown's avatar
unknown committed
254 255
  
  rc = mysql_query(mysql,"INSERT INTO myclient_test(name) VALUES('mysql')");
unknown's avatar
unknown committed
256
  myquery(rc);
unknown's avatar
unknown committed
257 258
  
  rc = mysql_query(mysql,"INSERT INTO myclient_test(name) VALUES('monty')");
unknown's avatar
unknown committed
259 260
  myquery(rc);

unknown's avatar
unknown committed
261
  rc = mysql_query(mysql,"INSERT INTO myclient_test(name) VALUES('venu')");
unknown's avatar
unknown committed
262 263
  myquery(rc);

unknown's avatar
unknown committed
264
  rc = mysql_query(mysql,"INSERT INTO myclient_test(name) VALUES('deleted')");
unknown's avatar
unknown committed
265
  myquery(rc);
unknown's avatar
unknown committed
266

unknown's avatar
unknown committed
267 268 269
  rc = mysql_query(mysql,"INSERT INTO myclient_test(name) VALUES('deleted')");
  myquery(rc);

unknown's avatar
unknown committed
270
  rc = mysql_query(mysql,"UPDATE myclient_test SET name='updated' WHERE name='deleted'");
unknown's avatar
unknown committed
271 272
  myquery(rc);

unknown's avatar
unknown committed
273
  rc = mysql_query(mysql,"UPDATE myclient_test SET id=3 WHERE name='updated'");
unknown's avatar
unknown committed
274
  myquery_r(rc);
unknown's avatar
unknown committed
275 276 277 278 279
}

/********************************************************
* print dashes                                          *
*********************************************************/
280
static void my_print_dashes(MYSQL_RES *result)
unknown's avatar
unknown committed
281 282 283 284 285 286 287
{
  MYSQL_FIELD  *field;
  unsigned int i,j;

  mysql_field_seek(result,0);
  fputc('\t',stdout);
  fputc('+', stdout);
unknown's avatar
unknown committed
288

unknown's avatar
unknown committed
289 290 291 292
  for(i=0; i< mysql_num_fields(result); i++)
  {
    field = mysql_fetch_field(result);
    for(j=0; j < field->max_length+2; j++)
unknown's avatar
unknown committed
293
      fputc('-',stdout);
unknown's avatar
unknown committed
294 295 296 297 298 299 300 301
    fputc('+',stdout);
  }
  fputc('\n',stdout);
}

/********************************************************
* print resultset metadata information                  *
*********************************************************/
302
static void my_print_result_metadata(MYSQL_RES *result)
unknown's avatar
unknown committed
303 304 305 306 307 308
{
  MYSQL_FIELD  *field;
  unsigned int i,j;
  unsigned int field_count;

  mysql_field_seek(result,0);
unknown's avatar
unknown committed
309
  fputc('\n', stdout);
310
  fputc('\n', stdout);
unknown's avatar
unknown committed
311 312 313 314 315 316

  field_count = mysql_num_fields(result);
  for(i=0; i< field_count; i++)
  {
    field = mysql_fetch_field(result);
    j = strlen(field->name);
unknown's avatar
unknown committed
317
    if (j < field->max_length)
unknown's avatar
unknown committed
318
      j = field->max_length;
unknown's avatar
unknown committed
319
    if (j < 4 && !IS_NOT_NULL(field->flags))
unknown's avatar
unknown committed
320 321 322 323 324 325
      j = 4;
    field->max_length = j;
  }
  my_print_dashes(result);
  fputc('\t',stdout);
  fputc('|', stdout);
unknown's avatar
unknown committed
326

unknown's avatar
unknown committed
327 328 329 330
  mysql_field_seek(result,0);
  for(i=0; i< field_count; i++)
  {
    field = mysql_fetch_field(result);
331
    fprintf(stdout, " %-*s |",(int) field->max_length, field->name);
unknown's avatar
unknown committed
332 333 334 335 336 337 338 339
  }
  fputc('\n', stdout);
  my_print_dashes(result);
}

/********************************************************
* process the result set                                *
*********************************************************/
unknown's avatar
unknown committed
340
int my_process_result_set(MYSQL_RES *result)
unknown's avatar
unknown committed
341 342 343 344 345
{
  MYSQL_ROW    row;
  MYSQL_FIELD  *field;
  unsigned int i;
  unsigned int row_count=0;
346 347 348
  
  if (!result)
    return 0;
unknown's avatar
unknown committed
349 350 351

  my_print_result_metadata(result);

unknown's avatar
unknown committed
352
  while ((row = mysql_fetch_row(result)) != NULL)
unknown's avatar
unknown committed
353
  {
unknown's avatar
unknown committed
354 355 356 357 358 359 360
    mysql_field_seek(result,0);
    fputc('\t',stdout);
    fputc('|',stdout);

    for(i=0; i< mysql_num_fields(result); i++)
    {
      field = mysql_fetch_field(result);
unknown's avatar
unknown committed
361
      if (row[i] == NULL)
362
        fprintf(stdout, " %-*s |", (int) field->max_length, "NULL");
unknown's avatar
unknown committed
363
      else if (IS_NUM(field->type))
364
        fprintf(stdout, " %*s |", (int) field->max_length, row[i]);
unknown's avatar
unknown committed
365
      else
366
        fprintf(stdout, " %-*s |", (int) field->max_length, row[i]);
unknown's avatar
unknown committed
367 368 369 370 371
    }
    fputc('\t',stdout);
    fputc('\n',stdout);
    row_count++;
  }
372 373
  if (row_count) 
    my_print_dashes(result);
unknown's avatar
unknown committed
374 375

  if (mysql_errno(mysql) != 0)
unknown's avatar
unknown committed
376
    fprintf(stderr, "\n\tmysql_fetch_row() failed\n");
unknown's avatar
unknown committed
377
  else
unknown's avatar
unknown committed
378 379 380
    fprintf(stdout,"\n\t%d %s returned\n", row_count, 
                   row_count == 1 ? "row" : "rows");
  return row_count;
unknown's avatar
unknown committed
381 382
}

unknown's avatar
unknown committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396
int my_process_result(MYSQL *mysql)
{
  MYSQL_RES *result;
  int       row_count;

  if (!(result = mysql_store_result(mysql)))
    return 0;
  
  row_count= my_process_result_set(result);
  
  mysql_free_result(result);
  return row_count;
}

397 398 399
/********************************************************
* process the stmt result set                           *
*********************************************************/
400 401 402
#define MAX_RES_FIELDS 50
#define MAX_FIELD_DATA_SIZE 255

403
uint my_process_stmt_result(MYSQL_STMT *stmt)
unknown's avatar
unknown committed
404
{
405 406
  int         field_count;
  uint        row_count= 0;
407
  MYSQL_BIND  buffer[MAX_RES_FIELDS];
408 409
  MYSQL_FIELD *field;
  MYSQL_RES   *result;
410 411 412
  char        data[MAX_RES_FIELDS][MAX_FIELD_DATA_SIZE];
  ulong       length[MAX_RES_FIELDS];
  my_bool     is_null[MAX_RES_FIELDS];
413 414
  int         rc, i;

unknown's avatar
unknown committed
415
  if (!(result= mysql_get_metadata(stmt))) /* No meta info */
416
  {
417 418 419
    while (!mysql_fetch(stmt)) 
      row_count++;
    return row_count;
420
  }
unknown's avatar
unknown committed
421
  
422
  field_count= min(mysql_num_fields(result), MAX_RES_FIELDS);
423 424 425
  for(i=0; i < field_count; i++)
  {
    buffer[i].buffer_type= MYSQL_TYPE_STRING;
426 427 428
    buffer[i].buffer_length= MAX_FIELD_DATA_SIZE;
    buffer[i].length= &length[i];
    buffer[i].buffer= (char*) data[i];
unknown's avatar
unknown committed
429
    buffer[i].is_null= &is_null[i];
430 431
  }
  my_print_result_metadata(result);
unknown's avatar
unknown committed
432

433 434
  rc= mysql_bind_result(stmt,buffer);
  mystmt(stmt,rc);
unknown's avatar
unknown committed
435

unknown's avatar
unknown committed
436 437 438
  rc= mysql_stmt_store_result(stmt);
  mystmt(stmt,rc);

439 440 441 442 443
  mysql_field_seek(result, 0);  
  while (mysql_fetch(stmt) == 0)
  {    
    fputc('\t',stdout);
    fputc('|',stdout);
unknown's avatar
unknown committed
444 445
    
    mysql_field_seek(result,0);
446 447 448
    for (i=0; i < field_count; i++)
    {
      field = mysql_fetch_field(result);
unknown's avatar
unknown committed
449
      if (is_null[i])
450
        fprintf(stdout, " %-*s |", (int) field->max_length, "NULL");
451
      else if (length[i] == 0)
452
      {
453
        data[i][0]='\0';  /* unmodified buffer */
454 455
        fprintf(stdout, " %*s |", (int) field->max_length, data[i]);
      }
456 457 458 459 460 461 462 463 464
      else if (IS_NUM(field->type))
        fprintf(stdout, " %*s |", (int) field->max_length, data[i]);
      else
        fprintf(stdout, " %-*s |", (int) field->max_length, data[i]);
    }
    fputc('\t',stdout);
    fputc('\n',stdout);
    row_count++;
  }
465 466 467
  if (row_count)
    my_print_dashes(result);

unknown's avatar
unknown committed
468 469
  fprintf(stdout,"\n\t%d %s returned\n", row_count, 
                 row_count == 1 ? "row" : "rows");
470
  mysql_free_result(result);
unknown's avatar
unknown committed
471 472 473 474 475 476
  return row_count;
}

/********************************************************
* process the stmt result set                           *
*********************************************************/
477
uint my_stmt_result(const char *buff, unsigned long length)
unknown's avatar
unknown committed
478 479 480 481 482
{
  MYSQL_STMT *stmt;
  uint       row_count;
  int        rc;

483 484
  fprintf(stdout,"\n\n %s", buff);
  stmt= mysql_prepare(mysql,buff,length);
unknown's avatar
unknown committed
485
  mystmt_init(stmt);
unknown's avatar
unknown committed
486

unknown's avatar
unknown committed
487 488 489 490 491 492
  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  row_count= my_process_stmt_result(stmt);
  mysql_stmt_close(stmt);
    
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
  return row_count;
}

/*
  Utility function to verify a particular column data
*/
static void verify_col_data(const char *table, const char *col, 
                            const char *exp_data)
{
  MYSQL_RES *result;
  MYSQL_ROW row;
  int       rc, field= 1;
 
  if (table && col)
  {
508
    strxmov(query,"SELECT ",col," FROM ",table," LIMIT 1", NullS);
509 510 511
    fprintf(stdout,"\n %s", query);
    rc = mysql_query(mysql, query);
    myquery(rc);
unknown's avatar
unknown committed
512

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
    field= 0;
  }

  result = mysql_use_result(mysql);
  mytest(result);

  if (!(row= mysql_fetch_row(result)) || !row[field])
  {
    fprintf(stdout,"\n *** ERROR: FAILED TO GET THE RESULT ***");
    exit(1);
  }
  fprintf(stdout,"\n obtained: `%s` (expected: `%s`)", 
    row[field], exp_data);
  myassert(strcmp(row[field],exp_data) == 0);
  mysql_free_result(result);
}

/*
  Utility function to verify the field members
*/

static void verify_prepare_field(MYSQL_RES *result, 
      unsigned int no,const char *name, const char *org_name, 
      enum enum_field_types type, const char *table, 
537
      const char *org_table, const char *db, 
538
      unsigned long length, const char *def)
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
{
  MYSQL_FIELD *field;

  if (!(field= mysql_fetch_field_direct(result,no)))
  {
    fprintf(stdout,"\n *** ERROR: FAILED TO GET THE RESULT ***");
    exit(1);
  }
  fprintf(stdout,"\n field[%d]:", no);
  fprintf(stdout,"\n    name     :`%s`\t(expected: `%s`)", field->name, name);
  fprintf(stdout,"\n    org_name :`%s`\t(expected: `%s`)", field->org_name, org_name);
  fprintf(stdout,"\n    type     :`%d`\t(expected: `%d`)", field->type, type);
  fprintf(stdout,"\n    table    :`%s`\t(expected: `%s`)", field->table, table);
  fprintf(stdout,"\n    org_table:`%s`\t(expected: `%s`)", field->org_table, org_table);
  fprintf(stdout,"\n    database :`%s`\t(expected: `%s`)", field->db, db);
554 555 556
  fprintf(stdout,"\n    length   :`%ld`\t(expected: `%ld`)", field->length, length);
  fprintf(stdout,"\n    maxlength:`%ld`", field->max_length);
  fprintf(stdout,"\n    charsetnr:`%d`", field->charsetnr);
557
  fprintf(stdout,"\n    default  :`%s`\t(expected: `%s`)", field->def ? field->def : "(null)", def ? def: "(null)");
558 559 560 561 562 563 564
  fprintf(stdout,"\n");
  myassert(strcmp(field->name,name) == 0);
  myassert(strcmp(field->org_name,org_name) == 0);
  myassert(field->type == type);
  myassert(strcmp(field->table,table) == 0);
  myassert(strcmp(field->org_table,org_table) == 0);
  myassert(strcmp(field->db,db) == 0);
565
  myassert(field->length == length);
566 567
  if (def)
    myassert(strcmp(field->def,def) == 0);
568 569 570 571 572 573 574 575
}

/*
  Utility function to verify the parameter count
*/
static void verify_param_count(MYSQL_STMT *stmt, long exp_count)
{
  long param_count= mysql_param_count(stmt);
576
  fprintf(stdout,"\n total parameters in stmt: `%ld` (expected: `%ld`)",
577 578
                  param_count, exp_count);
  myassert(param_count == exp_count);
unknown's avatar
unknown committed
579 580
}

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
/*
  Utility function to verify the total affected rows
*/
static void verify_st_affected_rows(MYSQL_STMT *stmt, ulonglong exp_count)
{
  ulonglong affected_rows= mysql_stmt_affected_rows(stmt);
  fprintf(stdout,"\n total affected rows: `%lld` (expected: `%lld`)",
          affected_rows, exp_count);
  myassert(affected_rows == exp_count);
}

/*
  Utility function to verify the total affected rows
*/
static void verify_affected_rows(ulonglong exp_count)
{
  ulonglong affected_rows= mysql_affected_rows(mysql);
  fprintf(stdout,"\n total affected rows: `%lld` (expected: `%lld`)",
          affected_rows, exp_count);
  myassert(affected_rows == exp_count);
}

/*
  Utility function to verify the total fields count
*/
static void verify_field_count(MYSQL_RES *result, uint exp_count)
{
  uint field_count= mysql_num_fields(result);
  fprintf(stdout,"\n total fields in the result set: `%d` (expected: `%d`)",
          field_count, exp_count);
  myassert(field_count == exp_count);
}

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
/*
  Utility function to execute a query using prepare-execute
*/
static void execute_prepare_query(const char *query, ulonglong exp_count)
{
  MYSQL_STMT *stmt;
  ulonglong  affected_rows;
  int        rc;

  stmt= mysql_prepare(mysql,query,strlen(query));
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  myquery(rc);  

  affected_rows= mysql_stmt_affected_rows(stmt);
  fprintf(stdout,"\n total affected rows: `%lld` (expected: `%lld`)",
          affected_rows, exp_count);

  myassert(affected_rows == exp_count);
  mysql_stmt_close(stmt);
}

637

unknown's avatar
unknown committed
638 639 640
/********************************************************
* store result processing                               *
*********************************************************/
641
static void client_store_result()
unknown's avatar
unknown committed
642 643 644
{
  MYSQL_RES *result;
  int       rc;
unknown's avatar
unknown committed
645

unknown's avatar
unknown committed
646 647 648
  myheader("client_store_result");

  rc = mysql_query(mysql, "SELECT * FROM myclient_test");
unknown's avatar
unknown committed
649 650
  myquery(rc);

unknown's avatar
unknown committed
651 652
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
653
  mytest(result);
unknown's avatar
unknown committed
654

unknown's avatar
unknown committed
655
  my_process_result_set(result);
unknown's avatar
unknown committed
656 657 658 659
  mysql_free_result(result);
}

/********************************************************
660
* fetch the results
unknown's avatar
unknown committed
661
*********************************************************/
662
static void client_use_result()
unknown's avatar
unknown committed
663 664 665 666 667
{
  MYSQL_RES *result;
  int       rc;
  myheader("client_use_result");

unknown's avatar
unknown committed
668 669
  rc = mysql_query(mysql, "SELECT * FROM myclient_test");
  myquery(rc);
unknown's avatar
unknown committed
670 671 672

  /* get the result */
  result = mysql_use_result(mysql);
unknown's avatar
unknown committed
673
  mytest(result);
unknown's avatar
unknown committed
674

unknown's avatar
unknown committed
675
  my_process_result_set(result);
unknown's avatar
unknown committed
676 677 678
  mysql_free_result(result);
}

unknown's avatar
unknown committed
679 680 681 682 683 684 685 686 687 688
/*
  Separate thread query to test some cases
*/
static my_bool thread_query(char *query)
{
  MYSQL *l_mysql;
  my_bool error;

  error= 0;
  fprintf(stdout,"\n in thread_query(%s)", query);
unknown's avatar
unknown committed
689
  if (!(l_mysql = mysql_init(NULL)))
unknown's avatar
unknown committed
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
  { 
    myerror("mysql_init() failed");
    return 1;
  }
  if (!(mysql_real_connect(l_mysql,opt_host,opt_user,
			   opt_password, current_db, opt_port,
			   opt_unix_socket, 0)))
  {
    myerror("connection failed");    
    error= 1;
    goto end;
  }    
  if (mysql_query(l_mysql,(char *)query))
  {
     fprintf(stderr,"Query failed (%s)\n",mysql_error(l_mysql));
     error= 1;
     goto end;
  }
  mysql_commit(l_mysql);
end:
  mysql_close(l_mysql);
  return error;
}

unknown's avatar
unknown committed
714 715 716 717

/********************************************************
* query processing                                      *
*********************************************************/
718
static void test_debug_example()
unknown's avatar
unknown committed
719 720 721 722 723 724 725
{
  int rc;
  MYSQL_RES *result;

  myheader("test_debug_example");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_debug_example");
unknown's avatar
unknown committed
726
  myquery(rc);
unknown's avatar
unknown committed
727 728 729

  rc = mysql_query(mysql,"CREATE TABLE test_debug_example(id int primary key auto_increment,\
                                              name varchar(20),xxx int)");
unknown's avatar
unknown committed
730 731
  myquery(rc);

unknown's avatar
unknown committed
732
  rc = mysql_query(mysql,"INSERT INTO test_debug_example(name) VALUES('mysql')");
unknown's avatar
unknown committed
733
  myquery(rc);
unknown's avatar
unknown committed
734 735

  rc = mysql_query(mysql,"UPDATE test_debug_example SET name='updated' WHERE name='deleted'");
unknown's avatar
unknown committed
736
  myquery(rc);
unknown's avatar
unknown committed
737

738
  rc = mysql_query(mysql,"SELECT * FROM test_debug_example where name='mysql'");
unknown's avatar
unknown committed
739 740
  myquery(rc);

unknown's avatar
unknown committed
741
  result = mysql_use_result(mysql);
unknown's avatar
unknown committed
742
  mytest(result);
unknown's avatar
unknown committed
743

unknown's avatar
unknown committed
744
  my_process_result_set(result);
unknown's avatar
unknown committed
745 746 747
  mysql_free_result(result);

  rc = mysql_query(mysql,"DROP TABLE test_debug_example");
unknown's avatar
unknown committed
748
  myquery(rc);
unknown's avatar
unknown committed
749 750 751 752 753
}

/********************************************************
* to test autocommit feature                            *
*********************************************************/
754
static void test_tran_bdb()
unknown's avatar
unknown committed
755 756 757 758 759 760 761 762
{
  MYSQL_RES *result;
  MYSQL_ROW row;
  int       rc;

  myheader("test_tran_bdb");

  /* set AUTOCOMMIT to OFF */
unknown's avatar
unknown committed
763
  rc = mysql_autocommit(mysql, FALSE);
unknown's avatar
unknown committed
764 765 766 767 768
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS my_demo_transaction");
  myquery(rc);

unknown's avatar
unknown committed
769 770

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
771
  myquery(rc);
unknown's avatar
unknown committed
772 773 774

  /* create the table 'mytran_demo' of type BDB' or 'InnoDB' */
  rc = mysql_query(mysql,"CREATE TABLE my_demo_transaction(col1 int ,col2 varchar(30)) TYPE = BDB");
unknown's avatar
unknown committed
775
  myquery(rc);
unknown's avatar
unknown committed
776 777

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
778
  myquery(rc);
unknown's avatar
unknown committed
779 780 781

  /* insert a row and commit the transaction */
  rc = mysql_query(mysql,"INSERT INTO my_demo_transaction VALUES(10,'venu')");
unknown's avatar
unknown committed
782
  myquery(rc);
unknown's avatar
unknown committed
783 784

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
785
  myquery(rc);
unknown's avatar
unknown committed
786

787
  /* now insert the second row, and rollback the transaction */
unknown's avatar
unknown committed
788
  rc = mysql_query(mysql,"INSERT INTO my_demo_transaction VALUES(20,'mysql')");
unknown's avatar
unknown committed
789
  myquery(rc);
unknown's avatar
unknown committed
790 791

  rc = mysql_rollback(mysql);
unknown's avatar
unknown committed
792
  myquery(rc);
unknown's avatar
unknown committed
793 794 795

  /* delete first row, and rollback it */
  rc = mysql_query(mysql,"DELETE FROM my_demo_transaction WHERE col1 = 10");
unknown's avatar
unknown committed
796
  myquery(rc);
unknown's avatar
unknown committed
797 798

  rc = mysql_rollback(mysql);
unknown's avatar
unknown committed
799
  myquery(rc);
unknown's avatar
unknown committed
800 801 802

  /* test the results now, only one row should exists */
  rc = mysql_query(mysql,"SELECT * FROM my_demo_transaction");
unknown's avatar
unknown committed
803 804
  myquery(rc);

unknown's avatar
unknown committed
805 806
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
807
  mytest(result);
unknown's avatar
unknown committed
808

unknown's avatar
unknown committed
809
  my_process_result_set(result);
810
  mysql_free_result(result);
unknown's avatar
unknown committed
811 812 813

  /* test the results now, only one row should exists */
  rc = mysql_query(mysql,"SELECT * FROM my_demo_transaction");
unknown's avatar
unknown committed
814 815
  myquery(rc);

unknown's avatar
unknown committed
816 817
  /* get the result */
  result = mysql_use_result(mysql);
unknown's avatar
unknown committed
818 819
  mytest(result);

unknown's avatar
unknown committed
820
  row = mysql_fetch_row(result);
unknown's avatar
unknown committed
821 822
  mytest(row);

unknown's avatar
unknown committed
823
  row = mysql_fetch_row(result);
unknown's avatar
unknown committed
824
  mytest_r(row);
unknown's avatar
unknown committed
825

unknown's avatar
unknown committed
826
  mysql_free_result(result);
unknown's avatar
unknown committed
827
  mysql_autocommit(mysql,TRUE);
unknown's avatar
unknown committed
828 829 830 831 832
}

/********************************************************
* to test autocommit feature                            *
*********************************************************/
833
static void test_tran_innodb()
unknown's avatar
unknown committed
834 835 836 837 838 839 840 841
{
  MYSQL_RES *result;
  MYSQL_ROW row;
  int       rc;

  myheader("test_tran_innodb");

  /* set AUTOCOMMIT to OFF */
unknown's avatar
unknown committed
842
  rc = mysql_autocommit(mysql, FALSE);
unknown's avatar
unknown committed
843 844 845 846
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS my_demo_transaction");
  myquery(rc);
unknown's avatar
unknown committed
847 848

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
849
  myquery(rc);
unknown's avatar
unknown committed
850 851 852

  /* create the table 'mytran_demo' of type BDB' or 'InnoDB' */
  rc = mysql_query(mysql,"CREATE TABLE my_demo_transaction(col1 int ,col2 varchar(30)) TYPE = InnoDB");
unknown's avatar
unknown committed
853
  myquery(rc);
unknown's avatar
unknown committed
854 855

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
856
  myquery(rc);
unknown's avatar
unknown committed
857 858 859

  /* insert a row and commit the transaction */
  rc = mysql_query(mysql,"INSERT INTO my_demo_transaction VALUES(10,'venu')");
unknown's avatar
unknown committed
860
  myquery(rc);
unknown's avatar
unknown committed
861 862

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
863
  myquery(rc);
unknown's avatar
unknown committed
864

865
  /* now insert the second row, and rollback the transaction */
unknown's avatar
unknown committed
866
  rc = mysql_query(mysql,"INSERT INTO my_demo_transaction VALUES(20,'mysql')");
unknown's avatar
unknown committed
867
  myquery(rc);
unknown's avatar
unknown committed
868 869

  rc = mysql_rollback(mysql);
unknown's avatar
unknown committed
870
  myquery(rc);
unknown's avatar
unknown committed
871 872 873

  /* delete first row, and rollback it */
  rc = mysql_query(mysql,"DELETE FROM my_demo_transaction WHERE col1 = 10");
unknown's avatar
unknown committed
874
  myquery(rc);
unknown's avatar
unknown committed
875 876

  rc = mysql_rollback(mysql);
unknown's avatar
unknown committed
877
  myquery(rc);
unknown's avatar
unknown committed
878 879 880

  /* test the results now, only one row should exists */
  rc = mysql_query(mysql,"SELECT * FROM my_demo_transaction");
unknown's avatar
unknown committed
881 882
  myquery(rc);

unknown's avatar
unknown committed
883 884
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
885
  mytest(result);
unknown's avatar
unknown committed
886

unknown's avatar
unknown committed
887
  my_process_result_set(result);
888
  mysql_free_result(result);
unknown's avatar
unknown committed
889 890 891

  /* test the results now, only one row should exists */
  rc = mysql_query(mysql,"SELECT * FROM my_demo_transaction");
unknown's avatar
unknown committed
892 893
  myquery(rc);

unknown's avatar
unknown committed
894 895
  /* get the result */
  result = mysql_use_result(mysql);
unknown's avatar
unknown committed
896 897
  mytest(result);

unknown's avatar
unknown committed
898
  row = mysql_fetch_row(result);
unknown's avatar
unknown committed
899 900
  mytest(row);

unknown's avatar
unknown committed
901
  row = mysql_fetch_row(result);
unknown's avatar
unknown committed
902
  mytest_r(row);
unknown's avatar
unknown committed
903 904

  mysql_free_result(result);
unknown's avatar
unknown committed
905
  mysql_autocommit(mysql,TRUE);
unknown's avatar
unknown committed
906 907
}

908

unknown's avatar
unknown committed
909
/********************************************************
910
 To test simple prepares of all DML statements
unknown's avatar
unknown committed
911
*********************************************************/
912

913
static void test_prepare_simple()
unknown's avatar
unknown committed
914 915
{
  MYSQL_STMT *stmt;
916
  int        rc;
unknown's avatar
unknown committed
917

unknown's avatar
unknown committed
918 919 920 921 922
  myheader("test_prepare_simple");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_prepare_simple");
  myquery(rc);

unknown's avatar
unknown committed
923
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
924
  myquery(rc);
unknown's avatar
unknown committed
925 926

  rc = mysql_query(mysql,"CREATE TABLE test_prepare_simple(id int, name varchar(50))");
unknown's avatar
unknown committed
927
  myquery(rc);
unknown's avatar
unknown committed
928 929

  /* alter table */
930 931
  strmov(query,"ALTER TABLE test_prepare_simple ADD new char(20)");
  stmt = mysql_prepare(mysql, query, 70);
932
  mystmt_init(stmt);
unknown's avatar
unknown committed
933

934
  verify_param_count(stmt,0);
unknown's avatar
unknown committed
935 936 937
  mysql_stmt_close(stmt);

  /* insert */
938 939
  strmov(query,"INSERT INTO test_prepare_simple VALUES(?,?)");
  stmt = mysql_prepare(mysql, query, 70);
940
  mystmt_init(stmt);
unknown's avatar
unknown committed
941

942
  verify_param_count(stmt,2);
unknown's avatar
unknown committed
943 944 945
  mysql_stmt_close(stmt);

  /* update */
946 947
  strmov(query,"UPDATE test_prepare_simple SET id=? WHERE id=? AND name= ?");
  stmt = mysql_prepare(mysql, query, 100);
948
  mystmt_init(stmt);
unknown's avatar
unknown committed
949

950
  verify_param_count(stmt,3);
unknown's avatar
unknown committed
951 952 953
  mysql_stmt_close(stmt);

  /* delete */
954 955
  strmov(query,"DELETE FROM test_prepare_simple WHERE id=10");
  stmt = mysql_prepare(mysql, query, 60);
956
  mystmt_init(stmt);
unknown's avatar
unknown committed
957

958
  verify_param_count(stmt,0);
unknown's avatar
unknown committed
959 960 961 962 963 964

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);
  mysql_stmt_close(stmt);

  /* delete */
965 966
  strmov(query,"DELETE FROM test_prepare_simple WHERE id=?");
  stmt = mysql_prepare(mysql, query, 50);
967
  mystmt_init(stmt);
unknown's avatar
unknown committed
968

969
  verify_param_count(stmt,1);
unknown's avatar
unknown committed
970 971 972 973 974 975

  rc = mysql_execute(stmt);
  mystmt_r(stmt, rc);
  mysql_stmt_close(stmt);

  /* select */
976 977
  strmov(query,"SELECT * FROM test_prepare_simple WHERE id=? AND name= ?");
  stmt = mysql_prepare(mysql, query, 100);
978
  mystmt_init(stmt);
unknown's avatar
unknown committed
979

980
  verify_param_count(stmt,2);
unknown's avatar
unknown committed
981 982 983 984 985

  mysql_stmt_close(stmt);

  /* now fetch the results ..*/
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
986
  myquery(rc);
unknown's avatar
unknown committed
987 988 989 990 991 992
}


/********************************************************
* to test simple prepare field results                  *
*********************************************************/
993
static void test_prepare_field_result()
unknown's avatar
unknown committed
994 995
{
  MYSQL_STMT *stmt;
996
  MYSQL_RES  *result;
997
  int        rc;
unknown's avatar
unknown committed
998

unknown's avatar
unknown committed
999 1000 1001 1002 1003
  myheader("test_prepare_field_result");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_prepare_field_result");
  myquery(rc);

unknown's avatar
unknown committed
1004
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1005
  myquery(rc);
unknown's avatar
unknown committed
1006

1007 1008 1009
  rc = mysql_query(mysql,"CREATE TABLE test_prepare_field_result(int_c int, \
                          var_c varchar(50), ts_c timestamp(14),\
                          char_c char(3), date_c date,extra tinyint)");
unknown's avatar
unknown committed
1010
  myquery(rc); 
unknown's avatar
unknown committed
1011 1012

  /* insert */
1013
  strmov(query,"SELECT int_c,var_c,date_c as date,ts_c,char_c FROM \
1014
                  test_prepare_field_result as t1 WHERE int_c=?");
1015
  stmt = mysql_prepare(mysql, query, strlen(query));
1016
  mystmt_init(stmt);
unknown's avatar
unknown committed
1017

1018
  verify_param_count(stmt,1);
unknown's avatar
unknown committed
1019

unknown's avatar
unknown committed
1020
  result = mysql_get_metadata(stmt);
1021 1022 1023 1024 1025 1026
  mytest(result);
  
  my_print_result_metadata(result);

  fprintf(stdout,"\n\n field attributes:\n");
  verify_prepare_field(result,0,"int_c","int_c",MYSQL_TYPE_LONG,
1027
                       "t1","test_prepare_field_result",current_db,11,0);
1028
  verify_prepare_field(result,1,"var_c","var_c",MYSQL_TYPE_VAR_STRING,
1029
                       "t1","test_prepare_field_result",current_db,50,0);
1030
  verify_prepare_field(result,2,"date","date_c",MYSQL_TYPE_DATE,
1031
                       "t1","test_prepare_field_result",current_db,10,0);
1032
  verify_prepare_field(result,3,"ts_c","ts_c",MYSQL_TYPE_TIMESTAMP,
1033
                       "t1","test_prepare_field_result",current_db,19,0);
1034
  verify_prepare_field(result,4,"char_c","char_c",MYSQL_TYPE_STRING,
1035
                       "t1","test_prepare_field_result",current_db,3,0);
1036

1037
  verify_field_count(result, 5);
1038
  mysql_free_result(result);
1039
  mysql_stmt_close(stmt);
unknown's avatar
unknown committed
1040 1041 1042 1043 1044 1045
}


/********************************************************
* to test simple prepare field results                  *
*********************************************************/
1046
static void test_prepare_syntax()
unknown's avatar
unknown committed
1047 1048
{
  MYSQL_STMT *stmt;
1049
  int        rc;
unknown's avatar
unknown committed
1050

unknown's avatar
unknown committed
1051 1052 1053 1054 1055
  myheader("test_prepare_syntax");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_prepare_syntax");
  myquery(rc);

unknown's avatar
unknown committed
1056
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1057
  myquery(rc);
unknown's avatar
unknown committed
1058 1059

  rc = mysql_query(mysql,"CREATE TABLE test_prepare_syntax(id int, name varchar(50), extra int)");
unknown's avatar
unknown committed
1060
  myquery(rc);
unknown's avatar
unknown committed
1061

1062
  strmov(query,"INSERT INTO test_prepare_syntax VALUES(?");
1063
  stmt = mysql_prepare(mysql, query, strlen(query));
1064
  mystmt_init_r(stmt);
unknown's avatar
unknown committed
1065

1066
  strmov(query,"SELECT id,name FROM test_prepare_syntax WHERE id=? AND WHERE");
1067
  stmt = mysql_prepare(mysql, query, strlen(query));
1068
  mystmt_init_r(stmt);
unknown's avatar
unknown committed
1069 1070 1071

  /* now fetch the results ..*/
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1072
  myquery(rc);
unknown's avatar
unknown committed
1073 1074 1075 1076 1077 1078
}


/********************************************************
* to test simple prepare                                *
*********************************************************/
1079
static void test_prepare()
unknown's avatar
unknown committed
1080
{
unknown's avatar
unknown committed
1081
  MYSQL_STMT *stmt;
unknown's avatar
unknown committed
1082
  int        rc, i;
unknown's avatar
unknown committed
1083 1084 1085 1086 1087 1088 1089
  int        int_data, o_int_data;
  char       str_data[50], data[50];
  char       tiny_data, o_tiny_data;
  short      small_data, o_small_data;
  longlong   big_data, o_big_data;
  float      real_data, o_real_data;
  double     double_data, o_double_data;
unknown's avatar
unknown committed
1090 1091
  ulong      length[7], len;
  my_bool    is_null[7];
unknown's avatar
unknown committed
1092
  MYSQL_BIND bind[7];
unknown's avatar
unknown committed
1093

unknown's avatar
unknown committed
1094
  myheader("test_prepare");
unknown's avatar
unknown committed
1095

unknown's avatar
unknown committed
1096
  rc = mysql_autocommit(mysql, TRUE);
unknown's avatar
unknown committed
1097 1098 1099 1100
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS my_prepare");
  myquery(rc);
unknown's avatar
unknown committed
1101 1102

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1103
  myquery(rc);
unknown's avatar
unknown committed
1104 1105

  rc = mysql_query(mysql,"CREATE TABLE my_prepare(col1 tinyint,\
1106
                                col2 varchar(15), col3 int,\
unknown's avatar
unknown committed
1107 1108
                                col4 smallint, col5 bigint, \
                                col6 float, col7 double )");
unknown's avatar
unknown committed
1109
  myquery(rc);
unknown's avatar
unknown committed
1110 1111

  /* insert by prepare */
1112
  strxmov(query,"INSERT INTO my_prepare VALUES(?,?,?,?,?,?,?)",NullS);
1113
  stmt = mysql_prepare(mysql, query, strlen(query));
1114
  mystmt_init(stmt);
unknown's avatar
unknown committed
1115

1116
  verify_param_count(stmt,7);
unknown's avatar
unknown committed
1117 1118 1119

  /* tinyint */
  bind[0].buffer_type=FIELD_TYPE_TINY;
unknown's avatar
unknown committed
1120
  bind[0].buffer= (char *)&tiny_data;
unknown's avatar
unknown committed
1121 1122
  /* string */
  bind[1].buffer_type=FIELD_TYPE_STRING;
unknown's avatar
unknown committed
1123 1124
  bind[1].buffer= (char *)str_data;
  bind[1].buffer_length= 1000;			/* Max string length */
unknown's avatar
unknown committed
1125 1126
  /* integer */
  bind[2].buffer_type=FIELD_TYPE_LONG;
unknown's avatar
unknown committed
1127
  bind[2].buffer= (char *)&int_data;
unknown's avatar
unknown committed
1128 1129
  /* short */
  bind[3].buffer_type=FIELD_TYPE_SHORT;
unknown's avatar
unknown committed
1130
  bind[3].buffer= (char *)&small_data;
unknown's avatar
unknown committed
1131 1132
  /* bigint */
  bind[4].buffer_type=FIELD_TYPE_LONGLONG;
unknown's avatar
unknown committed
1133
  bind[4].buffer= (char *)&big_data;
unknown's avatar
unknown committed
1134
  /* float */
unknown's avatar
unknown committed
1135
  bind[5].buffer_type=FIELD_TYPE_FLOAT;
unknown's avatar
unknown committed
1136
  bind[5].buffer= (char *)&real_data;
unknown's avatar
unknown committed
1137 1138
  /* double */
  bind[6].buffer_type=FIELD_TYPE_DOUBLE;
unknown's avatar
unknown committed
1139 1140 1141 1142 1143 1144 1145 1146
  bind[6].buffer= (char *)&double_data;

  for (i= 0; i < (int) array_elements(bind); i++)
  {
    bind[i].length= &length[i];
    bind[i].is_null= &is_null[i];
    is_null[i]= 0;
  }
unknown's avatar
unknown committed
1147

unknown's avatar
unknown committed
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  int_data = 320;
  small_data = 1867;
  big_data   = 1000;
  real_data = 2;
  double_data = 6578.001;

  /* now, execute the prepared statement to insert 10 records.. */
  for (tiny_data=0; tiny_data < 100; tiny_data++)
  {
unknown's avatar
unknown committed
1160
    length[1]= my_sprintf(str_data,(str_data, "MySQL%d",int_data));
unknown's avatar
unknown committed
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
    rc = mysql_execute(stmt);
    mystmt(stmt, rc);
    int_data += 25;
    small_data += 10;
    big_data += 100;
    real_data += 1;
    double_data += 10.09;
  }

  mysql_stmt_close(stmt);

  /* now fetch the results ..*/
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1174
  myquery(rc);
unknown's avatar
unknown committed
1175 1176

  /* test the results now, only one row should exists */
1177
  myassert(tiny_data == (char) my_stmt_result("SELECT * FROM my_prepare",50));
unknown's avatar
unknown committed
1178 1179 1180 1181 1182 1183
      
  stmt = mysql_prepare(mysql,"SELECT * FROM my_prepare",50);
  mystmt_init(stmt);

  rc = mysql_bind_result(stmt, bind);
  mystmt(stmt, rc);
unknown's avatar
unknown committed
1184

unknown's avatar
unknown committed
1185
  /* get the result */
unknown's avatar
unknown committed
1186 1187 1188 1189 1190 1191 1192 1193
  rc = mysql_execute(stmt);
  mystmt(stmt, rc);
  
  o_int_data = 320;
  o_small_data = 1867;
  o_big_data   = 1000;
  o_real_data = 2;
  o_double_data = 6578.001;
unknown's avatar
unknown committed
1194

unknown's avatar
unknown committed
1195 1196 1197
  /* now, execute the prepared statement to insert 10 records.. */
  for (o_tiny_data=0; o_tiny_data < 100; o_tiny_data++)
  {
unknown's avatar
unknown committed
1198
    len = my_sprintf(data, (data, "MySQL%d",o_int_data));
unknown's avatar
unknown committed
1199 1200 1201 1202
    
    rc = mysql_fetch(stmt);
    mystmt(stmt, rc);

1203 1204 1205 1206 1207 1208
    fprintf(stdout, "\n");
    
    fprintf(stdout, "\n\t tiny   : %d (%lu)", tiny_data,length[0]);
    fprintf(stdout, "\n\t short  : %d (%lu)", small_data,length[3]);
    fprintf(stdout, "\n\t int    : %d (%lu)", int_data,length[2]);
    fprintf(stdout, "\n\t big    : %lld (%lu)", big_data,length[4]);
unknown's avatar
unknown committed
1209

1210 1211
    fprintf(stdout, "\n\t float  : %f (%lu)", real_data,length[5]);
    fprintf(stdout, "\n\t double : %f (%lu)", double_data,length[6]);
unknown's avatar
unknown committed
1212

1213
    fprintf(stdout, "\n\t str    : %s (%lu)", str_data, length[1]);
unknown's avatar
unknown committed
1214 1215

    myassert(tiny_data == o_tiny_data);
unknown's avatar
unknown committed
1216
    myassert(is_null[0] == 0);
unknown's avatar
unknown committed
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
    myassert(length[0] == 1);

    myassert(int_data == o_int_data);
    myassert(length[2] == 4);
    
    myassert(small_data == o_small_data);
    myassert(length[3] == 2);
    
    myassert(big_data == o_big_data);
    myassert(length[4] == 8);
    
    myassert(real_data == o_real_data);
    myassert(length[5] == 4);
    
    myassert(double_data == o_double_data);
    myassert(length[6] == 8);
    
    myassert(strcmp(data,str_data) == 0);
    myassert(length[1] == len);
    
    o_int_data += 25;
    o_small_data += 10;
    o_big_data += 100;
    o_real_data += 1;
    o_double_data += 10.09;
  }

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  mysql_stmt_close(stmt);
unknown's avatar
unknown committed
1248 1249 1250

}

unknown's avatar
unknown committed
1251

unknown's avatar
unknown committed
1252 1253 1254
/********************************************************
* to test double comparision                            *
*********************************************************/
1255
static void test_double_compare()
unknown's avatar
unknown committed
1256
{
unknown's avatar
unknown committed
1257
  MYSQL_STMT *stmt;
1258
  int        rc;
1259
  char       real_data[10], tiny_data;
unknown's avatar
unknown committed
1260 1261
  double     double_data;
  MYSQL_RES  *result;
unknown's avatar
unknown committed
1262
  MYSQL_BIND bind[3];
unknown's avatar
unknown committed
1263
  ulong	     length[3];
unknown's avatar
unknown committed
1264

unknown's avatar
unknown committed
1265
  myheader("test_double_compare");
unknown's avatar
unknown committed
1266

unknown's avatar
unknown committed
1267
  rc = mysql_autocommit(mysql, TRUE);
unknown's avatar
unknown committed
1268 1269 1270 1271
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_double_compare");
  myquery(rc);
unknown's avatar
unknown committed
1272 1273

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1274
  myquery(rc);
unknown's avatar
unknown committed
1275 1276 1277

  rc = mysql_query(mysql,"CREATE TABLE test_double_compare(col1 tinyint,\
                                col2 float, col3 double )");
unknown's avatar
unknown committed
1278
  myquery(rc);
unknown's avatar
unknown committed
1279 1280

  rc = mysql_query(mysql,"INSERT INTO test_double_compare VALUES(1,10.2,34.5)");
unknown's avatar
unknown committed
1281
  myquery(rc);
unknown's avatar
unknown committed
1282

1283
  strmov(query, "UPDATE test_double_compare SET col1=100 WHERE col1 = ? AND col2 = ? AND COL3 = ?");
1284
  stmt = mysql_prepare(mysql,query, strlen(query));
1285
  mystmt_init(stmt);
unknown's avatar
unknown committed
1286

1287
  verify_param_count(stmt,3);
unknown's avatar
unknown committed
1288 1289 1290

  /* tinyint */
  bind[0].buffer_type=FIELD_TYPE_TINY;
unknown's avatar
unknown committed
1291 1292 1293
  bind[0].buffer=(char *)&tiny_data;
  bind[0].is_null= 0;				/* Can never be null */

unknown's avatar
unknown committed
1294 1295
  /* string->float */
  bind[1].buffer_type=FIELD_TYPE_STRING;
unknown's avatar
unknown committed
1296 1297 1298 1299 1300 1301
  bind[1].buffer= (char *)&real_data;
  bind[1].buffer_length=sizeof(real_data);
  bind[1].is_null= 0;
  bind[1].length= &length[1];
  length[1]= 10; 

unknown's avatar
unknown committed
1302 1303
  /* double */
  bind[2].buffer_type=FIELD_TYPE_DOUBLE;
unknown's avatar
unknown committed
1304 1305
  bind[2].buffer= (char *)&double_data;
  bind[2].is_null= 0;
unknown's avatar
unknown committed
1306

unknown's avatar
unknown committed
1307
  tiny_data = 1;
unknown's avatar
unknown committed
1308
  strmov(real_data,"10.2");
unknown's avatar
unknown committed
1309 1310 1311 1312 1313 1314 1315
  double_data = 34.5;
  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

1316
  verify_affected_rows(0);
unknown's avatar
unknown committed
1317

unknown's avatar
unknown committed
1318 1319 1320 1321
  mysql_stmt_close(stmt);

  /* now fetch the results ..*/
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1322
  myquery(rc);
unknown's avatar
unknown committed
1323 1324 1325

  /* test the results now, only one row should exists */
  rc = mysql_query(mysql,"SELECT * FROM test_double_compare");
unknown's avatar
unknown committed
1326 1327
  myquery(rc);

unknown's avatar
unknown committed
1328 1329
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
1330
  mytest(result);
unknown's avatar
unknown committed
1331

1332
  myassert((int)tiny_data == my_process_result_set(result));
unknown's avatar
unknown committed
1333 1334 1335
  mysql_free_result(result);
}

unknown's avatar
unknown committed
1336

unknown's avatar
unknown committed
1337 1338 1339
/********************************************************
* to test simple null                                   *
*********************************************************/
1340
static void test_null()
unknown's avatar
unknown committed
1341 1342
{
  MYSQL_STMT *stmt;
1343
  int        rc;
1344
  uint       nData;
unknown's avatar
unknown committed
1345
  MYSQL_BIND bind[2];
unknown's avatar
unknown committed
1346
  my_bool    is_null[2];
unknown's avatar
unknown committed
1347

unknown's avatar
unknown committed
1348
  myheader("test_null");
unknown's avatar
unknown committed
1349

unknown's avatar
unknown committed
1350 1351 1352
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_null");
  myquery(rc);

unknown's avatar
unknown committed
1353
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1354
  myquery(rc);
unknown's avatar
unknown committed
1355 1356

  rc = mysql_query(mysql,"CREATE TABLE test_null(col1 int,col2 varchar(50))");
unknown's avatar
unknown committed
1357
  myquery(rc);
unknown's avatar
unknown committed
1358 1359

  /* insert by prepare, wrong column name */
unknown's avatar
unknown committed
1360
  strmov(query,"INSERT INTO test_null(col3,col2) VALUES(?,?)");
1361
  stmt = mysql_prepare(mysql, query, strlen(query));
1362
  mystmt_init_r(stmt);
unknown's avatar
unknown committed
1363

unknown's avatar
unknown committed
1364
  strmov(query,"INSERT INTO test_null(col1,col2) VALUES(?,?)");
1365
  stmt = mysql_prepare(mysql, query, strlen(query));
1366
  mystmt_init(stmt);
unknown's avatar
unknown committed
1367

1368
  verify_param_count(stmt,2);
unknown's avatar
unknown committed
1369

unknown's avatar
unknown committed
1370 1371 1372
  bind[0].buffer_type=MYSQL_TYPE_LONG;
  bind[0].is_null= &is_null[0];
  is_null[0]= 1;
1373
  bind[1]=bind[0];		
unknown's avatar
unknown committed
1374

unknown's avatar
unknown committed
1375 1376 1377 1378
  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  /* now, execute the prepared statement to insert 10 records.. */
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
  for (nData=0; nData<10; nData++)
  {
    rc = mysql_execute(stmt);
    mystmt(stmt, rc);
  }

  /* Re-bind with MYSQL_TYPE_NULL */
  bind[0].buffer_type= MYSQL_TYPE_NULL;
  is_null[0]= 0; /* reset */
  bind[1]= bind[0];

  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt,rc);
  
  for (nData=0; nData<10; nData++)
unknown's avatar
unknown committed
1394 1395 1396 1397
  {
    rc = mysql_execute(stmt);
    mystmt(stmt, rc);
  }
1398
  
unknown's avatar
unknown committed
1399 1400 1401 1402
  mysql_stmt_close(stmt);

  /* now fetch the results ..*/
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1403
  myquery(rc);
unknown's avatar
unknown committed
1404

1405 1406
  nData*= 2;
  myassert(nData == my_stmt_result("SELECT * FROM test_null", 30));
unknown's avatar
unknown committed
1407

1408 1409 1410 1411 1412 1413 1414
  /* Fetch results */
  bind[0].buffer_type= MYSQL_TYPE_LONG;
  bind[0].buffer= (char *)&nData; /* this buffer won't be altered */
  bind[0].length= 0;
  bind[1]= bind[0];
  bind[0].is_null= &is_null[0];
  bind[1].is_null= &is_null[1];
unknown's avatar
unknown committed
1415

1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
  stmt = mysql_prepare(mysql,"SELECT * FROM test_null",30);
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  rc = mysql_bind_result(stmt,bind);
  mystmt(stmt,rc);

  rc= 0;
  is_null[0]= is_null[1]= 0;
  while (mysql_fetch(stmt) != MYSQL_NO_DATA)
  {
    myassert(is_null[0]);
    myassert(is_null[1]);
    rc++;
    is_null[0]= is_null[1]= 0;
  }
  myassert(rc == (int)nData);
  mysql_stmt_close(stmt);
unknown's avatar
unknown committed
1436 1437 1438 1439
}


/********************************************************
1440
* to test fetch null                                    *
unknown's avatar
unknown committed
1441
*********************************************************/
1442
static void test_fetch_null()
unknown's avatar
unknown committed
1443 1444
{
  MYSQL_STMT *stmt;
1445
  int        rc;
unknown's avatar
unknown committed
1446
  int        i, nData;
1447
  MYSQL_BIND bind[11];
unknown's avatar
unknown committed
1448 1449
  ulong	     length[11];
  my_bool    is_null[11];
unknown's avatar
unknown committed
1450

1451
  myheader("test_fetch_null");
unknown's avatar
unknown committed
1452

1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_fetch_null");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_fetch_null(col1 tinyint, col2 smallint, \
                                                       col3 int, col4 bigint, \
                                                       col5 float, col6 double, \
                                                       col7 date, col8 time, \
                                                       col9 varbinary(10), \
                                                       col10 varchar(50),\
                                                       col11 char(20))");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

unknown's avatar
unknown committed
1471
  rc = mysql_query(mysql,"INSERT INTO test_fetch_null(col11) VALUES(1000),(88),(389789)");
1472 1473 1474 1475 1476 1477
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  /* fetch */
unknown's avatar
unknown committed
1478
  for (i= 0; i < (int) array_elements(bind); i++)
1479 1480
  {
    bind[i].buffer_type=FIELD_TYPE_LONG;
unknown's avatar
unknown committed
1481 1482
    bind[i].is_null= &is_null[i];
    bind[i].length= &length[i];
1483
  }
unknown's avatar
unknown committed
1484
  bind[i-1].buffer=(char *)&nData;		/* Last column is not null */
1485

unknown's avatar
unknown committed
1486
  strmov((char *)query , "SELECT * FROM test_fetch_null");
unknown's avatar
unknown committed
1487 1488 1489 1490

  myassert(3 == my_stmt_result(query,50));

  stmt = mysql_prepare(mysql, query, 50);
1491
  mystmt_init(stmt);
unknown's avatar
unknown committed
1492

1493 1494
  rc = mysql_bind_result(stmt,bind);
  mystmt(stmt, rc);
unknown's avatar
unknown committed
1495 1496 1497

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);
unknown's avatar
unknown committed
1498

unknown's avatar
unknown committed
1499 1500
  rc= 0;
  while (mysql_fetch(stmt) != MYSQL_NO_DATA)
1501
  {
unknown's avatar
unknown committed
1502 1503 1504 1505
    rc++;
    for (i=0; i < 10; i++)
    {
      fprintf(stdout, "\n data[%d] : %s", i, 
unknown's avatar
unknown committed
1506 1507
              is_null[i] ? "NULL" : "NOT NULL");
      myassert(is_null[i]);
unknown's avatar
unknown committed
1508 1509 1510
    }
    fprintf(stdout, "\n data[%d]: %d", i, nData);
    myassert(nData == 1000 || nData == 88 || nData == 389789);
unknown's avatar
unknown committed
1511
    myassert(is_null[i] == 0);
unknown's avatar
unknown committed
1512
    myassert(length[i] == 4);
1513
  }
unknown's avatar
unknown committed
1514
  myassert(rc == 3);
unknown's avatar
unknown committed
1515
  mysql_stmt_close(stmt);
1516
}
unknown's avatar
unknown committed
1517

unknown's avatar
unknown committed
1518

1519 1520 1521 1522 1523 1524 1525
/********************************************************
* to test simple select                                 *
*********************************************************/
static void test_select_version()
{
  MYSQL_STMT *stmt;
  int        rc;
unknown's avatar
unknown committed
1526

1527
  myheader("test_select_version");
unknown's avatar
unknown committed
1528

1529
  stmt = mysql_prepare(mysql, "SELECT @@version", 30);
1530 1531 1532 1533 1534 1535
  mystmt_init(stmt);

  verify_param_count(stmt,0);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);
unknown's avatar
unknown committed
1536

1537 1538 1539
  my_process_stmt_result(stmt);
  mysql_stmt_close(stmt);
}
unknown's avatar
unknown committed
1540 1541 1542 1543

/********************************************************
* to test simple select                                 *
*********************************************************/
1544
static void test_select_simple()
unknown's avatar
unknown committed
1545 1546
{
  MYSQL_STMT *stmt;
1547
  int        rc;
unknown's avatar
unknown committed
1548

1549
  myheader("test_select_simple");
unknown's avatar
unknown committed
1550

1551
  stmt = mysql_prepare(mysql, "SHOW TABLES FROM mysql", 50);
1552
  mystmt_init(stmt);
unknown's avatar
unknown committed
1553

1554 1555 1556 1557 1558 1559 1560 1561 1562
  verify_param_count(stmt,0);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  my_process_stmt_result(stmt);
  mysql_stmt_close(stmt);
}

unknown's avatar
unknown committed
1563

1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
/********************************************************
* to test simple select to debug                        *
*********************************************************/
static void test_select_direct()
{
  int        rc;
  MYSQL_RES  *result;

  myheader("test_select_direct");
  
unknown's avatar
unknown committed
1574
  rc = mysql_autocommit(mysql,TRUE);
unknown's avatar
unknown committed
1575 1576 1577 1578
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_select");
  myquery(rc);
unknown's avatar
unknown committed
1579 1580

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1581
  myquery(rc);
unknown's avatar
unknown committed
1582

1583 1584 1585 1586
  rc = mysql_query(mysql,"CREATE TABLE test_select(id int, id1 tinyint, \
                                                   id2 float, \
                                                   id3 double, \
                                                   name varchar(50))");
unknown's avatar
unknown committed
1587 1588
  myquery(rc);

unknown's avatar
unknown committed
1589
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1590
  myquery(rc);
unknown's avatar
unknown committed
1591 1592

  /* insert a row and commit the transaction */
1593
  rc = mysql_query(mysql,"INSERT INTO test_select VALUES(10,5,2.3,4.5,'venu')");
unknown's avatar
unknown committed
1594
  myquery(rc);
unknown's avatar
unknown committed
1595 1596

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1597
  myquery(rc);
unknown's avatar
unknown committed
1598

1599
  rc = mysql_query(mysql,"SELECT * FROM test_select");
unknown's avatar
unknown committed
1600
  myquery(rc);
unknown's avatar
unknown committed
1601 1602 1603

  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
1604
  mytest(result);
unknown's avatar
unknown committed
1605

1606
  my_process_result_set(result);
unknown's avatar
unknown committed
1607 1608 1609
  mysql_free_result(result);
}

unknown's avatar
unknown committed
1610

unknown's avatar
unknown committed
1611
/********************************************************
1612
* to test simple select with prepare                    *
unknown's avatar
unknown committed
1613
*********************************************************/
1614
static void test_select_prepare()
unknown's avatar
unknown committed
1615
{
1616
  int        rc;
unknown's avatar
unknown committed
1617 1618
  MYSQL_STMT *stmt;

1619 1620
  myheader("test_select_prepare");
  
unknown's avatar
unknown committed
1621
  rc = mysql_autocommit(mysql,TRUE);
unknown's avatar
unknown committed
1622 1623
  myquery(rc);

1624
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_select");
unknown's avatar
unknown committed
1625
  myquery(rc);
unknown's avatar
unknown committed
1626 1627

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1628
  myquery(rc);
unknown's avatar
unknown committed
1629

1630
  rc = mysql_query(mysql,"CREATE TABLE test_select(id int, name varchar(50))");
unknown's avatar
unknown committed
1631 1632
  myquery(rc);

unknown's avatar
unknown committed
1633
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1634
  myquery(rc);
unknown's avatar
unknown committed
1635

1636 1637
  /* insert a row and commit the transaction */
  rc = mysql_query(mysql,"INSERT INTO test_select VALUES(10,'venu')");
unknown's avatar
unknown committed
1638 1639
  myquery(rc);

unknown's avatar
unknown committed
1640
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1641
  myquery(rc);
unknown's avatar
unknown committed
1642

1643 1644 1645 1646 1647
  stmt = mysql_prepare(mysql,"SELECT * FROM test_select",50);
  mystmt_init(stmt);
  
  rc = mysql_execute(stmt);
  mystmt(stmt,rc);
unknown's avatar
unknown committed
1648

1649 1650
  myassert(1 == my_process_stmt_result(stmt));
  mysql_stmt_close(stmt);
unknown's avatar
unknown committed
1651

1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
  rc = mysql_query(mysql,"DROP TABLE test_select");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_select(id tinyint, id1 int, \
                                                   id2 float, id3 float, \
                                                   name varchar(50))");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  /* insert a row and commit the transaction */
  rc = mysql_query(mysql,"INSERT INTO test_select(id,id1,id2,name) VALUES(10,5,2.3,'venu')");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  stmt = mysql_prepare(mysql,"SELECT * FROM test_select",25);
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

1679
  myassert(1 == my_process_stmt_result(stmt));
unknown's avatar
unknown committed
1680
  mysql_stmt_close(stmt);
1681 1682
}

unknown's avatar
unknown committed
1683

1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
/********************************************************
* to test simple select                                 *
*********************************************************/
static void test_select()
{
  MYSQL_STMT *stmt;
  int        rc;
  char       szData[25];
  int        nData=1;
  MYSQL_BIND bind[2];
unknown's avatar
unknown committed
1694
  ulong length[2];
1695 1696 1697

  myheader("test_select");

unknown's avatar
unknown committed
1698
  rc = mysql_autocommit(mysql,TRUE);
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_select");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_select(id int,name varchar(50))");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  /* insert a row and commit the transaction */
  rc = mysql_query(mysql,"INSERT INTO test_select VALUES(10,'venu')");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

1720
  /* now insert the second row, and rollback the transaction */
1721 1722 1723 1724 1725 1726
  rc = mysql_query(mysql,"INSERT INTO test_select VALUES(20,'mysql')");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

unknown's avatar
unknown committed
1727
  strmov(query,"SELECT * FROM test_select WHERE id=? AND name=?");
1728 1729 1730 1731 1732 1733 1734
  stmt = mysql_prepare(mysql, query, strlen(query));
  mystmt_init(stmt);

  verify_param_count(stmt,2);

  /* string data */
  nData=10;
1735
  strmov(szData,(char *)"venu");
1736
  bind[1].buffer_type=FIELD_TYPE_STRING;
1737
  bind[1].buffer=(char *)szData;
1738
  bind[1].buffer_length= 4;
unknown's avatar
unknown committed
1739 1740 1741 1742 1743
  bind[1].length= &length[1];
  length[1]= 4;
  bind[1].is_null=0;

  bind[0].buffer=(char *)&nData;
1744
  bind[0].buffer_type=FIELD_TYPE_LONG;
unknown's avatar
unknown committed
1745
  bind[0].is_null= 0;
1746 1747 1748 1749 1750 1751 1752

  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

1753
  myassert(my_process_stmt_result(stmt) == 1);
1754 1755 1756 1757

  mysql_stmt_close(stmt);
}

1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839
/*
  test BUG#1115 (incorrect string parameter value allocation)
*/
static void test_bug1115()
{
  MYSQL_STMT *stmt;
  int rc;
  MYSQL_BIND bind[1];
  ulong length[1];
  char szData[11];

  myheader("test_bug1115");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_select");
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_select(\
session_id  char(9) NOT NULL, \
    a       int(8) unsigned NOT NULL, \
    b        int(5) NOT NULL, \
    c      int(5) NOT NULL, \
    d  datetime NOT NULL)");
  myquery(rc);
  rc = mysql_query(mysql,"INSERT INTO test_select VALUES (\"abc\",1,2,3,2003-08-30), (\"abd\",1,2,3,2003-08-30), (\"abf\",1,2,3,2003-08-30), (\"abg\",1,2,3,2003-08-30), (\"abh\",1,2,3,2003-08-30), (\"abj\",1,2,3,2003-08-30), (\"abk\",1,2,3,2003-08-30), (\"abl\",1,2,3,2003-08-30), (\"abq\",1,2,3,2003-08-30), (\"abw\",1,2,3,2003-08-30), (\"abe\",1,2,3,2003-08-30), (\"abr\",1,2,3,2003-08-30), (\"abt\",1,2,3,2003-08-30), (\"aby\",1,2,3,2003-08-30), (\"abu\",1,2,3,2003-08-30), (\"abi\",1,2,3,2003-08-30), (\"abo\",1,2,3,2003-08-30), (\"abp\",1,2,3,2003-08-30), (\"abz\",1,2,3,2003-08-30), (\"abx\",1,2,3,2003-08-30)");
  myquery(rc);

  strmov(query,"SELECT * FROM test_select WHERE session_id = ?");
  stmt = mysql_prepare(mysql, query, strlen(query));
  mystmt_init(stmt);

  verify_param_count(stmt,1);

  strmov(szData,(char *)"abc");
  bind[0].buffer_type=FIELD_TYPE_STRING;
  bind[0].buffer=(char *)szData;
  bind[0].buffer_length= 10;
  bind[0].length= &length[0];
  length[0]= 3;
  bind[0].is_null=0;

  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  myassert(my_process_stmt_result(stmt) == 1);

  strmov(szData,(char *)"venu");
  bind[0].buffer_type=FIELD_TYPE_STRING;
  bind[0].buffer=(char *)szData;
  bind[0].buffer_length= 10;
  bind[0].length= &length[0];
  length[0]= 4;
  bind[0].is_null=0;

  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  myassert(my_process_stmt_result(stmt) == 0);

  strmov(szData,(char *)"abc");
  bind[0].buffer_type=FIELD_TYPE_STRING;
  bind[0].buffer=(char *)szData;
  bind[0].buffer_length= 10;
  bind[0].length= &length[0];
  length[0]= 3;
  bind[0].is_null=0;

  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  myassert(my_process_stmt_result(stmt) == 1);

  mysql_stmt_close(stmt);
}
unknown's avatar
unknown committed
1840

1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
/*
  test BUG#1180 (optimized away part of WHERE clause)
*/
static void test_bug1180()
{
  MYSQL_STMT *stmt;
  int rc;
  MYSQL_BIND bind[1];
  ulong length[1];
  char szData[11];
  int nData=1;

  myheader("test_select_bug");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_select");
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_select(session_id  char(9) NOT NULL)");
  myquery(rc);
  rc = mysql_query(mysql,"INSERT INTO test_select VALUES (\"abc\")");
  myquery(rc);

  strmov(query,"SELECT * FROM test_select WHERE ?=\"1111\" and session_id = \"abc\"");
  stmt = mysql_prepare(mysql, query, strlen(query));
  mystmt_init(stmt);

  verify_param_count(stmt,1);

  strmov(szData,(char *)"abc");
  bind[0].buffer_type=FIELD_TYPE_STRING;
  bind[0].buffer=(char *)szData;
  bind[0].buffer_length= 10;
  bind[0].length= &length[0];
  length[0]= 3;
  bind[0].is_null=0;

  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  myassert(my_process_stmt_result(stmt) == 0);

  strmov(szData,(char *)"1111");
  bind[0].buffer_type=FIELD_TYPE_STRING;
  bind[0].buffer=(char *)szData;
  bind[0].buffer_length= 10;
  bind[0].length= &length[0];
  length[0]= 4;
  bind[0].is_null=0;

  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  myassert(my_process_stmt_result(stmt) == 1);

  strmov(szData,(char *)"abc");
  bind[0].buffer_type=FIELD_TYPE_STRING;
  bind[0].buffer=(char *)szData;
  bind[0].buffer_length= 10;
  bind[0].length= &length[0];
  length[0]= 3;
  bind[0].is_null=0;

  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  myassert(my_process_stmt_result(stmt) == 0);

  mysql_stmt_close(stmt);
}

1920 1921 1922 1923 1924 1925 1926 1927 1928 1929
/********************************************************
* to test simple select show                            *
*********************************************************/
static void test_select_show()
{
  MYSQL_STMT *stmt;
  int        rc;

  myheader("test_select_show");

unknown's avatar
unknown committed
1930
  mysql_autocommit(mysql,TRUE);
1931

1932 1933 1934
  rc = mysql_query(mysql, "DROP TABLE IF EXISTS test_show");
  myquery(rc);
  
unknown's avatar
unknown committed
1935
  rc = mysql_query(mysql, "CREATE TABLE test_show(id int(4) NOT NULL primary key, name char(2))");
1936 1937 1938
  myquery(rc);

  stmt = mysql_prepare(mysql, "show columns from test_show", 30);
1939 1940 1941 1942 1943 1944 1945
  mystmt_init(stmt);

  verify_param_count(stmt,0);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

unknown's avatar
unknown committed
1946
  my_process_stmt_result(stmt);
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
  mysql_stmt_close(stmt);
  
  stmt = mysql_prepare(mysql, "show tables from mysql like ?", 50);
  mystmt_init_r(stmt);
  
  strxmov(query,"show tables from ", current_db, " like \'test_show\'", NullS);
  stmt = mysql_prepare(mysql, query, strlen(query));
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  my_process_stmt_result(stmt);
  mysql_stmt_close(stmt);
  
  stmt = mysql_prepare(mysql, "describe test_show", 30);
  mystmt_init(stmt);
  
  rc = mysql_execute(stmt);
  mystmt(stmt, rc);
1967

1968
  my_process_stmt_result(stmt);
1969
  mysql_stmt_close(stmt);
unknown's avatar
unknown committed
1970 1971 1972 1973 1974 1975 1976 1977 1978
  
  stmt = mysql_prepare(mysql, "show keys from test_show", 30);
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  myassert(1 == my_process_stmt_result(stmt));
  mysql_stmt_close(stmt);
1979 1980
}

unknown's avatar
unknown committed
1981

1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
/********************************************************
* to test simple update                                *
*********************************************************/
static void test_simple_update()
{
  MYSQL_STMT *stmt;
  int        rc;
  char       szData[25];
  int        nData=1;
  MYSQL_RES  *result;
  MYSQL_BIND bind[2];   
unknown's avatar
unknown committed
1993
  ulong	     length[2];
1994 1995 1996

  myheader("test_simple_update");

unknown's avatar
unknown committed
1997
  rc = mysql_autocommit(mysql,TRUE);
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_update");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_update(col1 int,\
                                col2 varchar(50), col3 int )");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"INSERT INTO test_update VALUES(1,'MySQL',100)");
  myquery(rc);

2016
  verify_affected_rows(1);
2017 2018 2019 2020 2021

  rc = mysql_commit(mysql);
  myquery(rc);

  /* insert by prepare */
unknown's avatar
unknown committed
2022
  strmov(query,"UPDATE test_update SET col2=? WHERE col1=?");
2023 2024 2025 2026 2027 2028 2029 2030
  stmt = mysql_prepare(mysql, query, strlen(query));
  mystmt_init(stmt);

  verify_param_count(stmt,2);

  nData=1;
  bind[0].buffer_type=FIELD_TYPE_STRING;
  bind[0].buffer=szData;		/* string data */
unknown's avatar
unknown committed
2031 2032 2033 2034 2035 2036
  bind[0].buffer_length=sizeof(szData);
  bind[0].length= &length[0];
  bind[0].is_null= 0;
  length[0]= my_sprintf(szData, (szData,"updated-data"));

  bind[1].buffer=(char *) &nData;
unknown's avatar
unknown committed
2037
  bind[1].buffer_type=FIELD_TYPE_LONG;
unknown's avatar
unknown committed
2038
  bind[1].is_null= 0;
unknown's avatar
unknown committed
2039

unknown's avatar
unknown committed
2040 2041 2042 2043 2044
  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);
2045
  verify_affected_rows(1);
unknown's avatar
unknown committed
2046 2047 2048 2049 2050

  mysql_stmt_close(stmt);

  /* now fetch the results ..*/
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2051
  myquery(rc);
unknown's avatar
unknown committed
2052 2053 2054

  /* test the results now, only one row should exists */
  rc = mysql_query(mysql,"SELECT * FROM test_update");
unknown's avatar
unknown committed
2055 2056
  myquery(rc);

unknown's avatar
unknown committed
2057 2058
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
2059
  mytest(result);
unknown's avatar
unknown committed
2060

2061
  myassert(1 == my_process_result_set(result));
unknown's avatar
unknown committed
2062 2063 2064 2065 2066 2067 2068
  mysql_free_result(result);
}


/********************************************************
* to test simple long data handling                     *
*********************************************************/
2069
static void test_long_data()
unknown's avatar
unknown committed
2070 2071
{
  MYSQL_STMT *stmt;
2072
  int        rc, int_data;
unknown's avatar
unknown committed
2073 2074
  char       *data=NullS;
  MYSQL_RES  *result;
unknown's avatar
unknown committed
2075
  MYSQL_BIND bind[3];
unknown's avatar
unknown committed
2076

unknown's avatar
unknown committed
2077
  myheader("test_long_data");
unknown's avatar
unknown committed
2078

unknown's avatar
unknown committed
2079
  rc = mysql_autocommit(mysql,TRUE);
unknown's avatar
unknown committed
2080 2081 2082 2083
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_long_data");
  myquery(rc);
unknown's avatar
unknown committed
2084 2085

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2086
  myquery(rc);
unknown's avatar
unknown committed
2087 2088 2089

  rc = mysql_query(mysql,"CREATE TABLE test_long_data(col1 int,\
                                col2 long varchar, col3 long varbinary)");
unknown's avatar
unknown committed
2090 2091
  myquery(rc);

unknown's avatar
unknown committed
2092
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2093 2094
  myquery(rc);

unknown's avatar
unknown committed
2095
  strmov(query,"INSERT INTO test_long_data(col1,col2) VALUES(?)");
unknown's avatar
unknown committed
2096
  stmt = mysql_prepare(mysql, query, strlen(query));
2097
  mystmt_init_r(stmt);
unknown's avatar
unknown committed
2098

unknown's avatar
unknown committed
2099
  strmov(query,"INSERT INTO test_long_data(col1,col2,col3) VALUES(?,?,?)");
2100
  stmt = mysql_prepare(mysql, query, strlen(query));
2101
  mystmt_init(stmt);
unknown's avatar
unknown committed
2102

2103
  verify_param_count(stmt,3);
unknown's avatar
unknown committed
2104

unknown's avatar
unknown committed
2105 2106
  bind[0].buffer=(char *)&int_data;
  bind[0].buffer_type=FIELD_TYPE_LONG;
unknown's avatar
unknown committed
2107
  bind[0].is_null=0;
unknown's avatar
unknown committed
2108 2109

  bind[1].buffer_type=FIELD_TYPE_STRING;
unknown's avatar
unknown committed
2110 2111 2112 2113
  bind[1].is_null=0;
  bind[1].buffer_length=0;			/* Will not be used */
  bind[1].length=0;				/* Will not be used */

unknown's avatar
unknown committed
2114
  bind[2]=bind[1];
unknown's avatar
unknown committed
2115 2116 2117
  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

2118
  int_data= 999;
unknown's avatar
unknown committed
2119
  data = (char *)"Michael";
unknown's avatar
unknown committed
2120 2121

  /* supply data in pieces */
unknown's avatar
unknown committed
2122 2123 2124
  rc = mysql_send_long_data(stmt,1,data,strlen(data));
  data = (char *)" 'Monty' Widenius";
  rc = mysql_send_long_data(stmt,1,data,strlen(data));
unknown's avatar
unknown committed
2125
  mystmt(stmt, rc);
unknown's avatar
unknown committed
2126
  rc = mysql_send_long_data(stmt,2,"Venu (venu@mysql.com)",4);
unknown's avatar
unknown committed
2127 2128 2129 2130
  mystmt(stmt, rc);

  /* execute */
  rc = mysql_execute(stmt);
2131
  fprintf(stdout," mysql_execute() returned %d\n",rc);
unknown's avatar
unknown committed
2132 2133 2134
  mystmt(stmt,rc);

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2135
  myquery(rc);
unknown's avatar
unknown committed
2136 2137

  /* now fetch the results ..*/
unknown's avatar
unknown committed
2138 2139 2140
  rc = mysql_query(mysql,"SELECT * FROM test_long_data");
  myquery(rc);

unknown's avatar
unknown committed
2141 2142
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
2143
  mytest(result);
unknown's avatar
unknown committed
2144

2145
  myassert(1 == my_process_result_set(result));
unknown's avatar
unknown committed
2146
  mysql_free_result(result);
2147 2148

  verify_col_data("test_long_data","col1","999");
unknown's avatar
unknown committed
2149
  verify_col_data("test_long_data","col2","Michael 'Monty' Widenius");
2150
  verify_col_data("test_long_data","col3","Venu");
2151
  mysql_stmt_close(stmt);
unknown's avatar
unknown committed
2152 2153
}

unknown's avatar
unknown committed
2154

unknown's avatar
unknown committed
2155 2156 2157
/********************************************************
* to test long data (string) handling                   *
*********************************************************/
2158
static void test_long_data_str()
unknown's avatar
unknown committed
2159 2160
{
  MYSQL_STMT *stmt;
2161
  int        rc, i;
unknown's avatar
unknown committed
2162
  char       data[255];
2163
  long       length, length1;
unknown's avatar
unknown committed
2164
  MYSQL_RES  *result;
unknown's avatar
unknown committed
2165
  MYSQL_BIND bind[2];
unknown's avatar
unknown committed
2166
  my_bool    is_null[2];
unknown's avatar
unknown committed
2167

unknown's avatar
unknown committed
2168
  myheader("test_long_data_str");
unknown's avatar
unknown committed
2169

unknown's avatar
unknown committed
2170
  rc = mysql_autocommit(mysql,TRUE);
unknown's avatar
unknown committed
2171 2172 2173 2174
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_long_data_str");
  myquery(rc);
unknown's avatar
unknown committed
2175 2176

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2177
  myquery(rc);
unknown's avatar
unknown committed
2178 2179

  rc = mysql_query(mysql,"CREATE TABLE test_long_data_str(id int, longstr long varchar)");
unknown's avatar
unknown committed
2180 2181
  myquery(rc);

unknown's avatar
unknown committed
2182
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2183
  myquery(rc);
unknown's avatar
unknown committed
2184

unknown's avatar
unknown committed
2185
  strmov(query,"INSERT INTO test_long_data_str VALUES(?,?)");
2186
  stmt = mysql_prepare(mysql, query, strlen(query));
2187
  mystmt_init(stmt);
unknown's avatar
unknown committed
2188

2189
  verify_param_count(stmt,2);
unknown's avatar
unknown committed
2190

unknown's avatar
unknown committed
2191
  bind[0].buffer = (char *)&length;
unknown's avatar
unknown committed
2192
  bind[0].buffer_type = FIELD_TYPE_LONG;
unknown's avatar
unknown committed
2193 2194 2195
  bind[0].is_null= &is_null[0];
  is_null[0]=0;
  length= 0;
unknown's avatar
unknown committed
2196

unknown's avatar
unknown committed
2197
  bind[1].buffer=data;				/* string data */
unknown's avatar
unknown committed
2198
  bind[1].buffer_type=FIELD_TYPE_STRING;
2199
  bind[1].length= &length1;
unknown's avatar
unknown committed
2200 2201 2202
  bind[1].buffer_length=0;			/* Will not be used */
  bind[1].is_null= &is_null[1];
  is_null[1]=0;
unknown's avatar
unknown committed
2203 2204 2205 2206
  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  length = 40;
2207
  strmov(data,"MySQL AB");
unknown's avatar
unknown committed
2208 2209

  /* supply data in pieces */
2210
  for(i=0; i < 4; i++)
unknown's avatar
unknown committed
2211
  {
unknown's avatar
unknown committed
2212
    rc = mysql_send_long_data(stmt,1,(char *)data,5);
2213
    mystmt(stmt, rc);
unknown's avatar
unknown committed
2214 2215 2216
  }
  /* execute */
  rc = mysql_execute(stmt);
2217
  fprintf(stdout," mysql_execute() returned %d\n",rc);
unknown's avatar
unknown committed
2218 2219 2220 2221 2222
  mystmt(stmt,rc);

  mysql_stmt_close(stmt);

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2223
  myquery(rc);
unknown's avatar
unknown committed
2224 2225 2226

  /* now fetch the results ..*/
  rc = mysql_query(mysql,"SELECT LENGTH(longstr), longstr FROM test_long_data_str");
unknown's avatar
unknown committed
2227 2228
  myquery(rc);

unknown's avatar
unknown committed
2229 2230
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
2231
  mytest(result);
unknown's avatar
unknown committed
2232

2233
  myassert(1 == my_process_result_set(result));
unknown's avatar
unknown committed
2234
  mysql_free_result(result);
2235

2236
  my_sprintf(data,(data,"%d", i*5));
2237 2238 2239
  verify_col_data("test_long_data_str","LENGTH(longstr)", data);
  data[0]='\0';
  while (i--)
2240
   strxmov(data,data,"MySQL",NullS);
2241
  verify_col_data("test_long_data_str","longstr", data);
unknown's avatar
unknown committed
2242 2243 2244 2245 2246 2247
}


/********************************************************
* to test long data (string) handling                   *
*********************************************************/
2248
static void test_long_data_str1()
unknown's avatar
unknown committed
2249 2250
{
  MYSQL_STMT *stmt;
2251
  int        rc, i;
2252
  char       data[255];
2253
  long       length, length1;
unknown's avatar
unknown committed
2254
  MYSQL_RES  *result;
unknown's avatar
unknown committed
2255 2256 2257
  MYSQL_BIND bind[2];

  myheader("test_long_data_str1");
unknown's avatar
unknown committed
2258

unknown's avatar
unknown committed
2259
  rc = mysql_autocommit(mysql,TRUE);
unknown's avatar
unknown committed
2260 2261 2262 2263
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_long_data_str");
  myquery(rc);
unknown's avatar
unknown committed
2264 2265

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2266
  myquery(rc);
unknown's avatar
unknown committed
2267 2268

  rc = mysql_query(mysql,"CREATE TABLE test_long_data_str(longstr long varchar,blb long varbinary)");
unknown's avatar
unknown committed
2269 2270
  myquery(rc);

unknown's avatar
unknown committed
2271
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2272
  myquery(rc);
unknown's avatar
unknown committed
2273

unknown's avatar
unknown committed
2274
  strmov(query,"INSERT INTO test_long_data_str VALUES(?,?)");
2275
  stmt = mysql_prepare(mysql, query, strlen(query));
2276
  mystmt_init(stmt);
unknown's avatar
unknown committed
2277

2278
  verify_param_count(stmt,2);
unknown's avatar
unknown committed
2279 2280

  bind[0].buffer=data;		  /* string data */
unknown's avatar
unknown committed
2281
  bind[0].buffer_length= sizeof(data);
2282
  bind[0].length= &length1;
unknown's avatar
unknown committed
2283
  bind[0].buffer_type=FIELD_TYPE_STRING;
unknown's avatar
unknown committed
2284 2285
  bind[0].is_null= 0;
  length1= 0;
unknown's avatar
unknown committed
2286 2287 2288 2289 2290 2291

  bind[1] = bind[0];
  bind[1].buffer_type=FIELD_TYPE_BLOB;

  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);
unknown's avatar
unknown committed
2292
  length = my_sprintf(data, (data, "MySQL AB"));
unknown's avatar
unknown committed
2293 2294

  /* supply data in pieces */
unknown's avatar
unknown committed
2295
  for (i=0; i < 3; i++)
unknown's avatar
unknown committed
2296
  {
unknown's avatar
unknown committed
2297
    rc = mysql_send_long_data(stmt,0,data,length);
2298
    mystmt(stmt, rc);
unknown's avatar
unknown committed
2299

unknown's avatar
unknown committed
2300
    rc = mysql_send_long_data(stmt,1,data,2);
2301
    mystmt(stmt, rc);
unknown's avatar
unknown committed
2302
  }
unknown's avatar
unknown committed
2303

unknown's avatar
unknown committed
2304 2305
  /* execute */
  rc = mysql_execute(stmt);
2306
  fprintf(stdout," mysql_execute() returned %d\n",rc);
unknown's avatar
unknown committed
2307 2308 2309 2310 2311
  mystmt(stmt,rc);

  mysql_stmt_close(stmt);

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2312
  myquery(rc);
unknown's avatar
unknown committed
2313 2314 2315

  /* now fetch the results ..*/
  rc = mysql_query(mysql,"SELECT LENGTH(longstr),longstr,LENGTH(blb),blb FROM test_long_data_str");
unknown's avatar
unknown committed
2316 2317
  myquery(rc);

unknown's avatar
unknown committed
2318 2319
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
2320
  mytest(result);
unknown's avatar
unknown committed
2321

2322
  myassert(1 == my_process_result_set(result));
unknown's avatar
unknown committed
2323
  mysql_free_result(result);
2324

2325
  my_sprintf(data,(data,"%ld",(long)i*length));
2326 2327
  verify_col_data("test_long_data_str","length(longstr)",data);

2328
  my_sprintf(data,(data,"%d",i*2));
2329
  verify_col_data("test_long_data_str","length(blb)",data);
unknown's avatar
unknown committed
2330 2331 2332 2333 2334 2335
}


/********************************************************
* to test long data (binary) handling                   *
*********************************************************/
2336
static void test_long_data_bin()
unknown's avatar
unknown committed
2337 2338
{
  MYSQL_STMT *stmt;
2339
  int        rc;
unknown's avatar
unknown committed
2340
  char       data[255];
unknown's avatar
unknown committed
2341
  long       length;
unknown's avatar
unknown committed
2342
  MYSQL_RES  *result;
unknown's avatar
unknown committed
2343 2344
  MYSQL_BIND bind[2];

unknown's avatar
unknown committed
2345

unknown's avatar
unknown committed
2346
  myheader("test_long_data_bin");
unknown's avatar
unknown committed
2347

unknown's avatar
unknown committed
2348
  rc = mysql_autocommit(mysql,TRUE);
unknown's avatar
unknown committed
2349 2350 2351 2352
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_long_data_bin");
  myquery(rc);
unknown's avatar
unknown committed
2353 2354

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2355
  myquery(rc);
unknown's avatar
unknown committed
2356 2357

  rc = mysql_query(mysql,"CREATE TABLE test_long_data_bin(id int, longbin long varbinary)");
unknown's avatar
unknown committed
2358 2359
  myquery(rc);

unknown's avatar
unknown committed
2360
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2361
  myquery(rc);
unknown's avatar
unknown committed
2362

unknown's avatar
unknown committed
2363
  strmov(query,"INSERT INTO test_long_data_bin VALUES(?,?)");
2364
  stmt = mysql_prepare(mysql, query, strlen(query));
2365
  mystmt_init(stmt);
unknown's avatar
unknown committed
2366

2367
  verify_param_count(stmt,2);
unknown's avatar
unknown committed
2368

unknown's avatar
unknown committed
2369
  bind[0].buffer = (char *)&length;
unknown's avatar
unknown committed
2370
  bind[0].buffer_type = FIELD_TYPE_LONG;
unknown's avatar
unknown committed
2371 2372
  bind[0].is_null= 0;
  length= 0;
unknown's avatar
unknown committed
2373

unknown's avatar
unknown committed
2374
  bind[1].buffer=data;		 /* string data */
unknown's avatar
unknown committed
2375
  bind[1].buffer_type=FIELD_TYPE_LONG_BLOB;
unknown's avatar
unknown committed
2376 2377
  bind[1].length= 0;		/* Will not be used */
  bind[1].is_null= 0;
unknown's avatar
unknown committed
2378 2379 2380 2381
  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  length = 10;
2382
  strmov(data,"MySQL AB");
unknown's avatar
unknown committed
2383 2384 2385 2386

  /* supply data in pieces */
  {
    int i;
unknown's avatar
unknown committed
2387
    for (i=0; i < 100; i++)
unknown's avatar
unknown committed
2388
    {
unknown's avatar
unknown committed
2389
      rc = mysql_send_long_data(stmt,1,(char *)data,4);
unknown's avatar
unknown committed
2390 2391 2392 2393 2394
      mystmt(stmt, rc);
    }
  }
  /* execute */
  rc = mysql_execute(stmt);
2395
  fprintf(stdout," mysql_execute() returned %d\n",rc);
unknown's avatar
unknown committed
2396 2397 2398 2399 2400
  mystmt(stmt,rc);

  mysql_stmt_close(stmt);

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2401
  myquery(rc);
unknown's avatar
unknown committed
2402 2403 2404

  /* now fetch the results ..*/
  rc = mysql_query(mysql,"SELECT LENGTH(longbin), longbin FROM test_long_data_bin");
unknown's avatar
unknown committed
2405 2406
  myquery(rc);

unknown's avatar
unknown committed
2407 2408
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
2409
  mytest(result);
unknown's avatar
unknown committed
2410

2411
  myassert(1 == my_process_result_set(result));
unknown's avatar
unknown committed
2412 2413 2414 2415 2416 2417 2418
  mysql_free_result(result);
}


/********************************************************
* to test simple delete                                 *
*********************************************************/
2419
static void test_simple_delete()
unknown's avatar
unknown committed
2420 2421
{
  MYSQL_STMT *stmt;
2422
  int        rc;
unknown's avatar
unknown committed
2423 2424 2425
  char       szData[30]={0};
  int        nData=1;
  MYSQL_RES  *result;
unknown's avatar
unknown committed
2426
  MYSQL_BIND bind[2];
unknown's avatar
unknown committed
2427
  ulong length[2];
unknown's avatar
unknown committed
2428 2429

  myheader("test_simple_delete");
unknown's avatar
unknown committed
2430

unknown's avatar
unknown committed
2431
  rc = mysql_autocommit(mysql,TRUE);
unknown's avatar
unknown committed
2432 2433 2434 2435
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_simple_delete");
  myquery(rc);
unknown's avatar
unknown committed
2436 2437

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2438
  myquery(rc);
unknown's avatar
unknown committed
2439 2440 2441

  rc = mysql_query(mysql,"CREATE TABLE test_simple_delete(col1 int,\
                                col2 varchar(50), col3 int )");
unknown's avatar
unknown committed
2442 2443
  myquery(rc);

unknown's avatar
unknown committed
2444
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2445
  myquery(rc);
unknown's avatar
unknown committed
2446 2447

  rc = mysql_query(mysql,"INSERT INTO test_simple_delete VALUES(1,'MySQL',100)");
unknown's avatar
unknown committed
2448 2449
  myquery(rc);

2450
  verify_affected_rows(1);
unknown's avatar
unknown committed
2451 2452

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2453
  myquery(rc);
unknown's avatar
unknown committed
2454 2455

  /* insert by prepare */
unknown's avatar
unknown committed
2456
  strmov(query,"DELETE FROM test_simple_delete WHERE col1=? AND col2=? AND col3=100");
2457
  stmt = mysql_prepare(mysql, query, strlen(query));
2458
  mystmt_init(stmt);
unknown's avatar
unknown committed
2459

2460
  verify_param_count(stmt,2);
unknown's avatar
unknown committed
2461 2462

  nData=1;
unknown's avatar
unknown committed
2463
  strmov(szData,"MySQL");
unknown's avatar
unknown committed
2464
  bind[1].buffer_type=FIELD_TYPE_STRING;
unknown's avatar
unknown committed
2465 2466 2467
  bind[1].buffer= szData;		/* string data */
  bind[1].buffer_length=sizeof(szData);
  bind[1].length= &length[1];
2468
  bind[1].is_null= 0;
unknown's avatar
unknown committed
2469 2470 2471
  length[1]= 5;

  bind[0].buffer=(char *)&nData;
unknown's avatar
unknown committed
2472
  bind[0].buffer_type=FIELD_TYPE_LONG;
unknown's avatar
unknown committed
2473
  bind[0].is_null= 0;
unknown's avatar
unknown committed
2474

unknown's avatar
unknown committed
2475 2476 2477 2478 2479
  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);
2480 2481

  verify_affected_rows(1);
unknown's avatar
unknown committed
2482 2483 2484 2485 2486

  mysql_stmt_close(stmt);

  /* now fetch the results ..*/
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2487
  myquery(rc);
unknown's avatar
unknown committed
2488 2489 2490

  /* test the results now, only one row should exists */
  rc = mysql_query(mysql,"SELECT * FROM test_simple_delete");
unknown's avatar
unknown committed
2491 2492
  myquery(rc);

unknown's avatar
unknown committed
2493 2494
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
2495
  mytest(result);
unknown's avatar
unknown committed
2496

2497
  myassert(0 == my_process_result_set(result));
unknown's avatar
unknown committed
2498 2499 2500 2501 2502 2503 2504 2505
  mysql_free_result(result);
}



/********************************************************
* to test simple update                                 *
*********************************************************/
2506
static void test_update()
unknown's avatar
unknown committed
2507 2508
{
  MYSQL_STMT *stmt;
2509
  int        rc;
unknown's avatar
unknown committed
2510
  char       szData[25];
unknown's avatar
unknown committed
2511 2512
  int        nData=1;
  MYSQL_RES  *result;
unknown's avatar
unknown committed
2513
  MYSQL_BIND bind[2];
unknown's avatar
unknown committed
2514
  ulong length[2];
unknown's avatar
unknown committed
2515

unknown's avatar
unknown committed
2516
  myheader("test_update");
unknown's avatar
unknown committed
2517

unknown's avatar
unknown committed
2518
  rc = mysql_autocommit(mysql,TRUE);
unknown's avatar
unknown committed
2519 2520 2521 2522
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_update");
  myquery(rc);
unknown's avatar
unknown committed
2523 2524

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2525
  myquery(rc);
unknown's avatar
unknown committed
2526 2527 2528

  rc = mysql_query(mysql,"CREATE TABLE test_update(col1 int primary key auto_increment,\
                                col2 varchar(50), col3 int )");
unknown's avatar
unknown committed
2529 2530
  myquery(rc);

unknown's avatar
unknown committed
2531
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2532
  myquery(rc);
unknown's avatar
unknown committed
2533

unknown's avatar
unknown committed
2534
  strmov(query,"INSERT INTO test_update(col2,col3) VALUES(?,?)");
2535
  stmt = mysql_prepare(mysql, query, strlen(query));
2536
  mystmt_init(stmt);
unknown's avatar
unknown committed
2537

2538
  verify_param_count(stmt,2);
unknown's avatar
unknown committed
2539 2540 2541 2542

  /* string data */
  bind[0].buffer_type=FIELD_TYPE_STRING;
  bind[0].buffer=szData;
unknown's avatar
unknown committed
2543 2544 2545 2546 2547 2548
  bind[0].buffer_length= sizeof(szData);
  bind[0].length= &length[0];
  length[0]= my_sprintf(szData, (szData, "inserted-data"));
  bind[0].is_null= 0;

  bind[1].buffer=(char *)&nData;
unknown's avatar
unknown committed
2549
  bind[1].buffer_type=FIELD_TYPE_LONG;
unknown's avatar
unknown committed
2550
  bind[1].is_null= 0;
unknown's avatar
unknown committed
2551

unknown's avatar
unknown committed
2552 2553 2554 2555 2556 2557
  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  nData=100;
  rc = mysql_execute(stmt);
  mystmt(stmt, rc);
unknown's avatar
unknown committed
2558

2559
  verify_affected_rows(1);
unknown's avatar
unknown committed
2560 2561
  mysql_stmt_close(stmt);

unknown's avatar
unknown committed
2562
  strmov(query,"UPDATE test_update SET col2=? WHERE col3=?");
2563
  stmt = mysql_prepare(mysql, query, strlen(query));
2564
  mystmt_init(stmt);
unknown's avatar
unknown committed
2565

2566
  verify_param_count(stmt,2);
unknown's avatar
unknown committed
2567 2568
  nData=100;

unknown's avatar
unknown committed
2569 2570
  bind[0].buffer_type=FIELD_TYPE_STRING;
  bind[0].buffer=szData;
unknown's avatar
unknown committed
2571 2572 2573 2574
  bind[0].buffer_length= sizeof(szData);
  bind[0].length= &length[0];
  length[0]= my_sprintf(szData, (szData, "updated-data"));
  bind[1].buffer=(char *)&nData;
unknown's avatar
unknown committed
2575
  bind[1].buffer_type=FIELD_TYPE_LONG;
unknown's avatar
unknown committed
2576
  bind[1].is_null= 0;
unknown's avatar
unknown committed
2577 2578 2579 2580 2581 2582

  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);
2583
  verify_affected_rows(1);
unknown's avatar
unknown committed
2584 2585 2586 2587 2588

  mysql_stmt_close(stmt);

  /* now fetch the results ..*/
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2589
  myquery(rc);
unknown's avatar
unknown committed
2590 2591 2592

  /* test the results now, only one row should exists */
  rc = mysql_query(mysql,"SELECT * FROM test_update");
unknown's avatar
unknown committed
2593 2594
  myquery(rc);

unknown's avatar
unknown committed
2595 2596
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
2597
  mytest(result);
unknown's avatar
unknown committed
2598

2599
  myassert(1 == my_process_result_set(result));
unknown's avatar
unknown committed
2600 2601 2602 2603 2604 2605 2606
  mysql_free_result(result);
}


/********************************************************
* to test simple prepare                                *
*********************************************************/
2607
static void test_prepare_noparam()
unknown's avatar
unknown committed
2608 2609
{
  MYSQL_STMT *stmt;
2610
  int        rc;
unknown's avatar
unknown committed
2611 2612
  MYSQL_RES  *result;

2613
  myheader("test_prepare_noparam");
unknown's avatar
unknown committed
2614 2615 2616 2617

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS my_prepare");
  myquery(rc);

unknown's avatar
unknown committed
2618
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2619
  myquery(rc);
unknown's avatar
unknown committed
2620 2621

  rc = mysql_query(mysql,"CREATE TABLE my_prepare(col1 int ,col2 varchar(50))");
unknown's avatar
unknown committed
2622 2623 2624
  myquery(rc);


unknown's avatar
unknown committed
2625
  /* insert by prepare */
unknown's avatar
unknown committed
2626
  strmov(query,"INSERT INTO my_prepare VALUES(10,'venu')");
2627
  stmt = mysql_prepare(mysql, query, strlen(query));
2628
  mystmt_init(stmt);
unknown's avatar
unknown committed
2629

2630
  verify_param_count(stmt,0);
unknown's avatar
unknown committed
2631 2632 2633 2634 2635 2636 2637 2638

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  mysql_stmt_close(stmt);

  /* now fetch the results ..*/
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2639
  myquery(rc);
unknown's avatar
unknown committed
2640 2641 2642

  /* test the results now, only one row should exists */
  rc = mysql_query(mysql,"SELECT * FROM my_prepare");
unknown's avatar
unknown committed
2643 2644
  myquery(rc);

unknown's avatar
unknown committed
2645 2646
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
2647
  mytest(result);
unknown's avatar
unknown committed
2648

2649
  myassert(1 == my_process_result_set(result));
unknown's avatar
unknown committed
2650 2651 2652 2653 2654 2655 2656
  mysql_free_result(result);
}


/********************************************************
* to test simple bind result                            *
*********************************************************/
2657
static void test_bind_result()
unknown's avatar
unknown committed
2658 2659 2660
{
  MYSQL_STMT *stmt;
  int        rc;
unknown's avatar
unknown committed
2661 2662
  int        nData;
  ulong	     length, length1;
unknown's avatar
unknown committed
2663
  char       szData[100];
unknown's avatar
unknown committed
2664
  MYSQL_BIND bind[2];
unknown's avatar
unknown committed
2665
  my_bool    is_null[2];
unknown's avatar
unknown committed
2666

unknown's avatar
unknown committed
2667
  myheader("test_bind_result");
unknown's avatar
unknown committed
2668

unknown's avatar
unknown committed
2669 2670 2671
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_bind_result");
  myquery(rc);

unknown's avatar
unknown committed
2672
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2673
  myquery(rc);
unknown's avatar
unknown committed
2674 2675

  rc = mysql_query(mysql,"CREATE TABLE test_bind_result(col1 int ,col2 varchar(50))");
unknown's avatar
unknown committed
2676 2677
  myquery(rc);

unknown's avatar
unknown committed
2678
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2679
  myquery(rc);
unknown's avatar
unknown committed
2680 2681

  rc = mysql_query(mysql,"INSERT INTO test_bind_result VALUES(10,'venu')");
unknown's avatar
unknown committed
2682
  myquery(rc);
unknown's avatar
unknown committed
2683 2684

  rc = mysql_query(mysql,"INSERT INTO test_bind_result VALUES(20,'MySQL')");
unknown's avatar
unknown committed
2685 2686
  myquery(rc);

2687 2688 2689
  rc = mysql_query(mysql,"INSERT INTO test_bind_result(col2) VALUES('monty')");
  myquery(rc);

unknown's avatar
unknown committed
2690
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2691
  myquery(rc);
unknown's avatar
unknown committed
2692

unknown's avatar
unknown committed
2693
  /* fetch */
unknown's avatar
unknown committed
2694 2695

  bind[0].buffer_type=FIELD_TYPE_LONG;
unknown's avatar
unknown committed
2696 2697 2698 2699
  bind[0].buffer= (char *) &nData;	/* integer data */
  bind[0].is_null= &is_null[0];
  bind[0].length= 0;

unknown's avatar
unknown committed
2700 2701
  bind[1].buffer_type=FIELD_TYPE_STRING;
  bind[1].buffer=szData;		/* string data */
unknown's avatar
unknown committed
2702
  bind[1].buffer_length=sizeof(szData);
unknown's avatar
unknown committed
2703 2704
  bind[1].length= &length1;
  bind[1].is_null= &is_null[1];
unknown's avatar
unknown committed
2705

2706
  stmt = mysql_prepare(mysql, "SELECT * FROM test_bind_result", 50);
2707
  mystmt_init(stmt);
unknown's avatar
unknown committed
2708

unknown's avatar
unknown committed
2709
  rc = mysql_bind_result(stmt,bind);
unknown's avatar
unknown committed
2710
  mystmt(stmt, rc);
unknown's avatar
unknown committed
2711 2712

  rc = mysql_execute(stmt);
unknown's avatar
unknown committed
2713
  mystmt(stmt, rc);
unknown's avatar
unknown committed
2714 2715 2716 2717

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

unknown's avatar
unknown committed
2718
  fprintf(stdout,"\n row 1: %d,%s(%lu)",nData, szData, length1);
2719 2720 2721
  myassert(nData == 10);
  myassert(strcmp(szData,"venu")==0);
  myassert(length1 == 4);
unknown's avatar
unknown committed
2722 2723 2724 2725

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

unknown's avatar
unknown committed
2726
  fprintf(stdout,"\n row 2: %d,%s(%lu)",nData, szData, length1);
2727 2728 2729 2730 2731 2732 2733 2734
  myassert(nData == 20);
  myassert(strcmp(szData,"MySQL")==0);
  myassert(length1 == 5);

  length=99;
  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);
 
unknown's avatar
unknown committed
2735 2736 2737
  if (is_null[0])
    fprintf(stdout,"\n row 3: NULL,%s(%lu)", szData, length1);
  myassert(is_null[0]);
2738 2739
  myassert(strcmp(szData,"monty")==0);
  myassert(length1 == 5);
unknown's avatar
unknown committed
2740 2741

  rc = mysql_fetch(stmt);
2742
  myassert(rc == MYSQL_NO_DATA);
unknown's avatar
unknown committed
2743 2744 2745 2746

  mysql_stmt_close(stmt);
}

2747

unknown's avatar
unknown committed
2748
/********************************************************
2749
* to test ext bind result                               *
unknown's avatar
unknown committed
2750
*********************************************************/
2751
static void test_bind_result_ext()
unknown's avatar
unknown committed
2752 2753
{
  MYSQL_STMT *stmt;
unknown's avatar
unknown committed
2754
  int        rc, i;
2755 2756 2757 2758 2759 2760 2761
  uchar      t_data;
  short      s_data;
  int        i_data;
  longlong   b_data;
  float      f_data;
  double     d_data;
  char       szData[20], bData[20];
unknown's avatar
unknown committed
2762
  ulong       szLength, bLength;
2763
  MYSQL_BIND bind[8];
unknown's avatar
unknown committed
2764 2765
  long	     length[8];
  my_bool    is_null[8];
unknown's avatar
unknown committed
2766

2767
  myheader("test_bind_result_ext");
unknown's avatar
unknown committed
2768

2769 2770
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_bind_result");
  myquery(rc);
unknown's avatar
unknown committed
2771

2772 2773 2774 2775 2776 2777 2778 2779
  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_bind_result(c1 tinyint, c2 smallint, \
                                                        c3 int, c4 bigint, \
                                                        c5 float, c6 double, \
                                                        c7 varbinary(10), \
                                                        c8 varchar(50))");
unknown's avatar
unknown committed
2780 2781
  myquery(rc);

unknown's avatar
unknown committed
2782
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2783
  myquery(rc);
unknown's avatar
unknown committed
2784

2785 2786 2787 2788 2789 2790 2791 2792
  rc = mysql_query(mysql,"INSERT INTO test_bind_result VALUES(19,2999,3999,4999999,\
                                                              2345.6,5678.89563,\
                                                              'venu','mysql')");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

unknown's avatar
unknown committed
2793 2794 2795 2796 2797 2798
  for (i= 0; i < (int) array_elements(bind); i++)
  {
    bind[i].length=  &length[i];
    bind[i].is_null= &is_null[i];
  }

2799
  bind[0].buffer_type=MYSQL_TYPE_TINY;
unknown's avatar
unknown committed
2800
  bind[0].buffer=(char *)&t_data;
2801 2802 2803 2804 2805

  bind[1].buffer_type=MYSQL_TYPE_SHORT;
  bind[2].buffer_type=MYSQL_TYPE_LONG;
  
  bind[3].buffer_type=MYSQL_TYPE_LONGLONG;
unknown's avatar
unknown committed
2806
  bind[1].buffer=(char *)&s_data;
2807
  
unknown's avatar
unknown committed
2808 2809
  bind[2].buffer=(char *)&i_data;
  bind[3].buffer=(char *)&b_data;
2810 2811

  bind[4].buffer_type=MYSQL_TYPE_FLOAT;
unknown's avatar
unknown committed
2812
  bind[4].buffer=(char *)&f_data;
2813 2814

  bind[5].buffer_type=MYSQL_TYPE_DOUBLE;
unknown's avatar
unknown committed
2815
  bind[5].buffer=(char *)&d_data;
2816 2817

  bind[6].buffer_type=MYSQL_TYPE_STRING;
2818
  bind[6].buffer= (char *)szData;
unknown's avatar
unknown committed
2819 2820
  bind[6].buffer_length= sizeof(szData);
  bind[6].length= &szLength;
2821 2822

  bind[7].buffer_type=MYSQL_TYPE_TINY_BLOB;
unknown's avatar
unknown committed
2823 2824 2825
  bind[7].buffer=(char *)&bData;
  bind[7].length= &bLength;
  bind[7].buffer_length= sizeof(bData);
2826

2827
  stmt = mysql_prepare(mysql, "select * from test_bind_result", 50);
2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846
  mystmt_init(stmt);

  rc = mysql_bind_result(stmt,bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  fprintf(stdout, "\n data (tiny)   : %d", t_data);
  fprintf(stdout, "\n data (short)  : %d", s_data);
  fprintf(stdout, "\n data (int)    : %d", i_data);
  fprintf(stdout, "\n data (big)    : %lld", b_data);

  fprintf(stdout, "\n data (float)  : %f", f_data);
  fprintf(stdout, "\n data (double) : %f", d_data);

unknown's avatar
unknown committed
2847 2848
  fprintf(stdout, "\n data (str)    : %s(%lu)", szData, szLength);
  fprintf(stdout, "\n data (bin)    : %s(%lu)", bData, bLength);
2849 2850 2851 2852 2853 2854 2855 2856 2857


  myassert(t_data == 19);
  myassert(s_data == 2999);
  myassert(i_data == 3999);
  myassert(b_data == 4999999);
  /*myassert(f_data == 2345.60);*/
  /*myassert(d_data == 5678.89563);*/
  myassert(strcmp(szData,"venu")==0);
unknown's avatar
unknown committed
2858
  myassert(strncmp(bData,"mysql",5)==0);
2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874
  myassert(szLength == 4);
  myassert(bLength == 5);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  mysql_stmt_close(stmt);
}


/********************************************************
* to test ext bind result                               *
*********************************************************/
static void test_bind_result_ext1()
{
  MYSQL_STMT *stmt;
unknown's avatar
unknown committed
2875
  uint	     i;
2876 2877 2878 2879
  int        rc;
  char       t_data[20];
  float      s_data;
  short      i_data;
2880
  uchar      b_data;
2881 2882 2883 2884 2885
  int        f_data;
  long       bData;
  char       d_data[20];
  double     szData;
  MYSQL_BIND bind[8];
unknown's avatar
unknown committed
2886 2887
  ulong      length[8];
  my_bool    is_null[8];
2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914
  myheader("test_bind_result_ext1");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_bind_result");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_bind_result(c1 tinyint, c2 smallint, \
                                                        c3 int, c4 bigint, \
                                                        c5 float, c6 double, \
                                                        c7 varbinary(10), \
                                                        c8 varchar(10))");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"INSERT INTO test_bind_result VALUES(120,2999,3999,54,\
                                                              2.6,58.89,\
                                                              '206','6.7')");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  bind[0].buffer_type=MYSQL_TYPE_STRING;
unknown's avatar
unknown committed
2915 2916
  bind[0].buffer=(char *) t_data;
  bind[0].buffer_length= sizeof(t_data);
2917 2918

  bind[1].buffer_type=MYSQL_TYPE_FLOAT;
unknown's avatar
unknown committed
2919
  bind[1].buffer=(char *)&s_data;
2920 2921

  bind[2].buffer_type=MYSQL_TYPE_SHORT;
unknown's avatar
unknown committed
2922
  bind[2].buffer=(char *)&i_data;
2923 2924

  bind[3].buffer_type=MYSQL_TYPE_TINY;
unknown's avatar
unknown committed
2925
  bind[3].buffer=(char *)&b_data;
2926 2927

  bind[4].buffer_type=MYSQL_TYPE_LONG;
unknown's avatar
unknown committed
2928
  bind[4].buffer=(char *)&f_data;
2929 2930

  bind[5].buffer_type=MYSQL_TYPE_STRING;
unknown's avatar
unknown committed
2931
  bind[5].buffer=(char *)d_data;
2932 2933

  bind[6].buffer_type=MYSQL_TYPE_LONG;
unknown's avatar
unknown committed
2934
  bind[6].buffer=(char *)&bData;
2935 2936

  bind[7].buffer_type=MYSQL_TYPE_DOUBLE;
unknown's avatar
unknown committed
2937
  bind[7].buffer=(char *)&szData;
2938

unknown's avatar
unknown committed
2939 2940 2941 2942 2943 2944
  for (i= 0; i < array_elements(bind); i++)
  {
    bind[i].is_null= &is_null[i];
    bind[i].length= &length[i];
  }

2945
  stmt = mysql_prepare(mysql, "select * from test_bind_result", 50);
2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956
  mystmt_init(stmt);

  rc = mysql_bind_result(stmt,bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

unknown's avatar
unknown committed
2957 2958 2959 2960
  fprintf(stdout, "\n data (tiny)   : %s(%lu)", t_data, length[0]);
  fprintf(stdout, "\n data (short)  : %f(%lu)", s_data, length[1]);
  fprintf(stdout, "\n data (int)    : %d(%lu)", i_data, length[2]);
  fprintf(stdout, "\n data (big)    : %d(%lu)", b_data, length[3]);
2961

unknown's avatar
unknown committed
2962 2963
  fprintf(stdout, "\n data (float)  : %d(%lu)", f_data, length[4]);
  fprintf(stdout, "\n data (double) : %s(%lu)", d_data, length[5]);
2964

unknown's avatar
unknown committed
2965 2966
  fprintf(stdout, "\n data (bin)    : %ld(%lu)", bData, length[6]);
  fprintf(stdout, "\n data (str)    : %g(%lu)", szData, length[7]);
2967 2968 2969 2970 2971

  myassert(strcmp(t_data,"120")==0);
  myassert(i_data == 3999);
  myassert(f_data == 2);
  myassert(strcmp(d_data,"58.89")==0);
2972
  myassert(b_data == 54);
2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988

  myassert(length[0] == 3);
  myassert(length[1] == 4);
  myassert(length[2] == 2);
  myassert(length[3] == 1);
  myassert(length[4] == 4);
  myassert(length[5] == 5);
  myassert(length[6] == 4);
  myassert(length[7] == 8);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  mysql_stmt_close(stmt);
}

2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121
/*
 Generalized fetch conversion routine for all basic types           
*/
static void bind_fetch(int row_count)
{ 
  MYSQL_STMT   *stmt;
  int          rc, i, count= row_count;
  ulong	       bit;
  long         data[10];
  float        f_data;
  double       d_data;
  char         s_data[10];
  ulong	       length[10];
  MYSQL_BIND   bind[7];
  my_bool      is_null[7]; 

  stmt = mysql_prepare(mysql,"INSERT INTO test_bind_fetch VALUES(?,?,?,?,?,?,?)",100);
  mystmt_init(stmt);

  verify_param_count(stmt, 7);
 
  for (i= 0; i < (int) array_elements(bind); i++)
  {  
    bind[i].buffer_type= MYSQL_TYPE_LONG;
    bind[i].buffer= (char *) &data[i];
    bind[i].is_null= 0;
  }   
  rc = mysql_bind_param(stmt, bind);
  mystmt(stmt,rc);
 
  while (count--)
  {
    rc= 10+count;
    for (i= 0; i < (int) array_elements(bind); i++)
    {  
      data[i]= rc+i;
      rc+= 12;
    }
    rc = mysql_execute(stmt);
    mystmt(stmt, rc);
  }

  rc = mysql_commit(mysql);
  myquery(rc);

  mysql_stmt_close(stmt);

  myassert(row_count == (int)
           my_stmt_result("SELECT * FROM test_bind_fetch",50));

  stmt = mysql_prepare(mysql,"SELECT * FROM test_bind_fetch",50);
  myquery(rc);

  for (i= 0; i < (int) array_elements(bind); i++)
  {
    bind[i].buffer= (char *) &data[i];
    bind[i].length= &length[i];
    bind[i].is_null= &is_null[i];
  }

  bind[0].buffer_type= MYSQL_TYPE_TINY;
  bind[1].buffer_type= MYSQL_TYPE_SHORT;
  bind[2].buffer_type= MYSQL_TYPE_LONG;
  bind[3].buffer_type= MYSQL_TYPE_LONGLONG;

  bind[4].buffer_type= MYSQL_TYPE_FLOAT;
  bind[4].buffer= (char *)&f_data;

  bind[5].buffer_type= MYSQL_TYPE_DOUBLE;
  bind[5].buffer= (char *)&d_data;

  bind[6].buffer_type= MYSQL_TYPE_STRING;
  bind[6].buffer= (char *)&s_data;
  bind[6].buffer_length= sizeof(s_data);

  rc = mysql_bind_result(stmt, bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  rc = mysql_stmt_store_result(stmt);
  mystmt(stmt, rc);

  while (row_count--)
  {
    rc = mysql_fetch(stmt);
    mystmt(stmt,rc);

    fprintf(stdout, "\n");
    fprintf(stdout, "\n tiny     : %ld(%lu)", data[0], length[0]);
    fprintf(stdout, "\n short    : %ld(%lu)", data[1], length[1]);
    fprintf(stdout, "\n int      : %ld(%lu)", data[2], length[2]);
    fprintf(stdout, "\n longlong : %ld(%lu)", data[3], length[3]);
    fprintf(stdout, "\n float    : %f(%lu)",  f_data,  length[4]);
    fprintf(stdout, "\n double   : %g(%lu)",  d_data,  length[5]);
    fprintf(stdout, "\n char     : %s(%lu)",  s_data,  length[6]);

    bit= 1;
    rc= 10+row_count;
    for (i=0; i < 4; i++)
    {
      myassert(data[i] == rc+i);
      myassert(length[i] == bit);
      bit<<= 1;
      rc+= 12;
    }

    /* FLOAT */
    rc+= i;
    myassert((int)f_data == rc);
    myassert(length[4] == 4);

    /* DOUBLE */
    rc+= 13;
    myassert((int)d_data == rc);
    myassert(length[5] == 8);

    /* CHAR */
    rc+= 13;
    {
      char buff[20];
      long len= my_sprintf(buff, (buff, "%d", rc));
      myassert(strcmp(s_data,buff)==0);
      myassert(length[6] == (ulong) len);
    }
  }
  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  mysql_stmt_close(stmt);
}

3122 3123 3124 3125 3126 3127
/********************************************************
* to test fetching of date, time and ts                 *
*********************************************************/
static void test_fetch_date()
{
  MYSQL_STMT *stmt;
unknown's avatar
unknown committed
3128
  uint	     i;
3129 3130
  int        rc, year;
  char       date[25], time[25], ts[25], ts_4[15], ts_6[20], dt[20];
unknown's avatar
unknown committed
3131
  ulong      d_length, t_length, ts_length, ts4_length, ts6_length, 
3132
             dt_length, y_length;
unknown's avatar
unknown committed
3133 3134 3135
  MYSQL_BIND bind[8];
  my_bool    is_null[8];
  ulong	     length[8];
3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166

  myheader("test_fetch_date");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_bind_result");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_bind_result(c1 date, c2 time, \
                                                        c3 timestamp(14), \
                                                        c4 year, \
                                                        c5 datetime, \
                                                        c6 timestamp(4), \
                                                        c7 timestamp(6))");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"INSERT INTO test_bind_result VALUES('2002-01-02',\
                                                              '12:49:00',\
                                                              '2002-01-02 17:46:59', \
                                                              2010,\
                                                              '2010-07-10', \
                                                              '2020','1999-12-29')");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

unknown's avatar
unknown committed
3167 3168 3169 3170 3171 3172
  for (i= 0; i < array_elements(bind); i++)
  {
    bind[i].is_null= &is_null[i];
    bind[i].length= &length[i];
  }

3173 3174 3175
  bind[0].buffer_type=MYSQL_TYPE_STRING;
  bind[1]=bind[2]=bind[0];

unknown's avatar
unknown committed
3176 3177 3178
  bind[0].buffer=(char *)&date;
  bind[0].buffer_length= sizeof(date);
  bind[0].length= &d_length;
3179

unknown's avatar
unknown committed
3180 3181 3182
  bind[1].buffer=(char *)&time;
  bind[1].buffer_length= sizeof(time);
  bind[1].length= &t_length;
3183

unknown's avatar
unknown committed
3184 3185 3186
  bind[2].buffer=(char *)&ts;
  bind[2].buffer_length= sizeof(ts);
  bind[2].length= &ts_length;
3187 3188

  bind[3].buffer_type=MYSQL_TYPE_LONG;
unknown's avatar
unknown committed
3189 3190
  bind[3].buffer=(char *)&year;
  bind[3].length= &y_length;
3191 3192

  bind[4].buffer_type=MYSQL_TYPE_STRING;
unknown's avatar
unknown committed
3193 3194 3195
  bind[4].buffer=(char *)&dt;
  bind[4].buffer_length= sizeof(dt);
  bind[4].length= &dt_length;
3196 3197

  bind[5].buffer_type=MYSQL_TYPE_STRING;
unknown's avatar
unknown committed
3198 3199 3200
  bind[5].buffer=(char *)&ts_4;
  bind[5].buffer_length= sizeof(ts_4);
  bind[5].length= &ts4_length;
3201 3202

  bind[6].buffer_type=MYSQL_TYPE_STRING;
unknown's avatar
unknown committed
3203 3204 3205
  bind[6].buffer=(char *)&ts_6;
  bind[6].buffer_length= sizeof(ts_6);
  bind[6].length= &ts6_length;
3206

unknown's avatar
unknown committed
3207 3208
  myassert(1 == my_stmt_result("SELECT * FROM test_bind_result",50));

3209 3210 3211 3212 3213 3214 3215 3216
  stmt = mysql_prepare(mysql, "SELECT * FROM test_bind_result", 50);
  mystmt_init(stmt);

  rc = mysql_bind_result(stmt,bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);
unknown's avatar
unknown committed
3217
  
3218 3219 3220 3221
  ts_4[0]='\0';
  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

unknown's avatar
unknown committed
3222 3223 3224 3225 3226 3227 3228
  fprintf(stdout, "\n date   : %s(%lu)", date, d_length);
  fprintf(stdout, "\n time   : %s(%lu)", time, t_length);
  fprintf(stdout, "\n ts     : %s(%lu)", ts, ts_length);
  fprintf(stdout, "\n year   : %d(%lu)", year, y_length);
  fprintf(stdout, "\n dt     : %s(%lu)", dt,  dt_length);
  fprintf(stdout, "\n ts(4)  : %s(%lu)", ts_4, ts4_length);
  fprintf(stdout, "\n ts(6)  : %s(%lu)", ts_6, ts6_length);
3229 3230 3231 3232 3233

  myassert(strcmp(date,"2002-01-02")==0);
  myassert(d_length == 10);

  myassert(strcmp(time,"12:49:00")==0);
unknown's avatar
unknown committed
3234
  myassert(t_length == 8);
3235 3236 3237 3238 3239 3240 3241

  myassert(strcmp(ts,"2002-01-02 17:46:59")==0);
  myassert(ts_length == 19);

  myassert(year == 2010);
  myassert(y_length == 4);
  
3242 3243
  myassert(strcmp(dt,"2010-07-10 00:00:00")==0);
  myassert(dt_length == 19);
3244 3245 3246 3247

  myassert(ts_4[0] == '\0');
  myassert(ts4_length == 0);

3248 3249
  myassert(strcmp(ts_6,"1999-12-29 00:00:00")==0);
  myassert(ts6_length == 19);
3250 3251 3252 3253 3254 3255 3256

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  mysql_stmt_close(stmt);
}

unknown's avatar
unknown committed
3257 3258 3259 3260 3261
/********************************************************
* to test fetching of str to all types                  *
*********************************************************/
static void test_fetch_str()
{
3262
  int rc;
unknown's avatar
unknown committed
3263 3264 3265

  myheader("test_fetch_str");

3266
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_bind_fetch");
unknown's avatar
unknown committed
3267 3268 3269 3270 3271
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

3272
  rc = mysql_query(mysql,"CREATE TABLE test_bind_fetch(c1 char(10),\
unknown's avatar
unknown committed
3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283
                                                     c2 char(10),\
                                                     c3 char(20),\
                                                     c4 char(20),\
                                                     c5 char(30),\
                                                     c6 char(40),\
                                                     c7 char(20))");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

3284
  bind_fetch(3);
unknown's avatar
unknown committed
3285 3286 3287 3288 3289 3290 3291
}

/********************************************************
* to test fetching of long to all types                 *
*********************************************************/
static void test_fetch_long()
{
3292
  int rc;
unknown's avatar
unknown committed
3293 3294 3295

  myheader("test_fetch_long");

3296
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_bind_fetch");
unknown's avatar
unknown committed
3297 3298 3299 3300 3301
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

3302
  rc = mysql_query(mysql,"CREATE TABLE test_bind_fetch(c1 int unsigned,\
unknown's avatar
unknown committed
3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313
                                                     c2 int unsigned,\
                                                     c3 int,\
                                                     c4 int,\
                                                     c5 int,\
                                                     c6 int unsigned,\
                                                     c7 int)");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

3314
  bind_fetch(4);
unknown's avatar
unknown committed
3315 3316
}

unknown's avatar
unknown committed
3317

unknown's avatar
unknown committed
3318 3319 3320 3321 3322
/********************************************************
* to test fetching of short to all types                 *
*********************************************************/
static void test_fetch_short()
{
3323
  int rc;
unknown's avatar
unknown committed
3324 3325 3326

  myheader("test_fetch_short");

3327
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_bind_fetch");
unknown's avatar
unknown committed
3328 3329 3330 3331 3332
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

3333
  rc = mysql_query(mysql,"CREATE TABLE test_bind_fetch(c1 smallint unsigned,\
unknown's avatar
unknown committed
3334 3335 3336 3337 3338 3339 3340 3341 3342 3343
                                                     c2 smallint,\
                                                     c3 smallint unsigned,\
                                                     c4 smallint,\
                                                     c5 smallint,\
                                                     c6 smallint,\
                                                     c7 smallint unsigned)");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);
3344 3345 3346
  
  bind_fetch(5);
}
unknown's avatar
unknown committed
3347

unknown's avatar
unknown committed
3348

unknown's avatar
unknown committed
3349 3350 3351 3352 3353
/********************************************************
* to test fetching of tiny to all types                 *
*********************************************************/
static void test_fetch_tiny()
{
3354
  int rc;
unknown's avatar
unknown committed
3355 3356 3357

  myheader("test_fetch_tiny");

3358
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_bind_fetch");
unknown's avatar
unknown committed
3359 3360 3361 3362 3363
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

3364
  rc = mysql_query(mysql,"CREATE TABLE test_bind_fetch(c1 tinyint unsigned,\
unknown's avatar
unknown committed
3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375
                                                     c2 tinyint,\
                                                     c3 tinyint unsigned,\
                                                     c4 tinyint,\
                                                     c5 tinyint,\
                                                     c6 tinyint,\
                                                     c7 tinyint unsigned)");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);
  
3376
  bind_fetch(3);
unknown's avatar
unknown committed
3377 3378 3379

}

unknown's avatar
unknown committed
3380

unknown's avatar
unknown committed
3381 3382 3383 3384 3385
/********************************************************
* to test fetching of longlong to all types             *
*********************************************************/
static void test_fetch_bigint()
{
3386
  int rc;
unknown's avatar
unknown committed
3387 3388 3389

  myheader("test_fetch_bigint");

3390
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_bind_fetch");
unknown's avatar
unknown committed
3391 3392 3393 3394 3395
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

3396
  rc = mysql_query(mysql,"CREATE TABLE test_bind_fetch(c1 bigint,\
unknown's avatar
unknown committed
3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407
                                                     c2 bigint,\
                                                     c3 bigint unsigned,\
                                                     c4 bigint unsigned,\
                                                     c5 bigint unsigned,\
                                                     c6 bigint unsigned,\
                                                     c7 bigint unsigned)");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);
  
3408
  bind_fetch(2);
unknown's avatar
unknown committed
3409 3410 3411

}

unknown's avatar
unknown committed
3412

unknown's avatar
unknown committed
3413 3414 3415 3416 3417
/********************************************************
* to test fetching of float to all types                 *
*********************************************************/
static void test_fetch_float()
{
3418
  int rc;
unknown's avatar
unknown committed
3419 3420 3421

  myheader("test_fetch_float");

3422
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_bind_fetch");
unknown's avatar
unknown committed
3423 3424 3425 3426 3427
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

3428
  rc = mysql_query(mysql,"CREATE TABLE test_bind_fetch(c1 float(3),\
unknown's avatar
unknown committed
3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439
                                                     c2 float,\
                                                     c3 float unsigned,\
                                                     c4 float,\
                                                     c5 float,\
                                                     c6 float,\
                                                     c7 float(10) unsigned)");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);
  
3440
  bind_fetch(2);
unknown's avatar
unknown committed
3441 3442 3443 3444 3445 3446 3447 3448

}

/********************************************************
* to test fetching of double to all types               *
*********************************************************/
static void test_fetch_double()
{
3449
  int rc;
unknown's avatar
unknown committed
3450 3451 3452

  myheader("test_fetch_double");

3453
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_bind_fetch");
unknown's avatar
unknown committed
3454 3455 3456 3457 3458
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

3459
  rc = mysql_query(mysql,"CREATE TABLE test_bind_fetch(c1 double(5,2),\
unknown's avatar
unknown committed
3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470
                                                     c2 double unsigned,\
                                                     c3 double unsigned,\
                                                     c4 double unsigned,\
                                                     c5 double unsigned,\
                                                     c6 double unsigned,\
                                                     c7 double unsigned)");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);
  
3471
  bind_fetch(3);
unknown's avatar
unknown committed
3472 3473

}
3474 3475 3476 3477 3478 3479 3480

/********************************************************
* to test simple prepare with all possible types        *
*********************************************************/
static void test_prepare_ext()
{
  MYSQL_STMT *stmt;
unknown's avatar
unknown committed
3481
  uint	     i;
3482 3483 3484 3485 3486 3487
  int        rc;
  char       *sql;
  int        nData=1;
  char       tData=1;
  short      sData=10;
  longlong   bData=20;
unknown's avatar
unknown committed
3488
  MYSQL_BIND bind[6];
3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522
  myheader("test_prepare_ext");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_prepare_ext");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  sql = (char *)"CREATE TABLE test_prepare_ext\
			(\
			c1  tinyint,\
			c2  smallint,\
			c3  mediumint,\
			c4  int,\
			c5  integer,\
			c6  bigint,\
			c7  float,\
			c8  double,\
			c9  double precision,\
			c10 real,\
			c11 decimal(7,4),\
      c12 numeric(8,4),\
			c13 date,\
			c14 datetime,\
			c15 timestamp(14),\
			c16 time,\
			c17 year,\
			c18 bit,\
      c19 bool,\
			c20 char,\
			c21 char(10),\
			c22 varchar(30),\
			c23 tinyblob,\
			c24 tinytext,\
unknown's avatar
unknown committed
3523 3524 3525 3526 3527 3528 3529 3530 3531
			c25 blob,\
			c26 text,\
			c27 mediumblob,\
			c28 mediumtext,\
			c29 longblob,\
			c30 longtext,\
			c31 enum('one','two','three'),\
			c32 set('monday','tuesday','wednesday'))";

unknown's avatar
unknown committed
3532 3533
  rc = mysql_query(mysql,sql);
  myquery(rc);
unknown's avatar
unknown committed
3534 3535

  /* insert by prepare - all integers */
unknown's avatar
unknown committed
3536
  strmov(query,(char *)"INSERT INTO test_prepare_ext(c1,c2,c3,c4,c5,c6) VALUES(?,?,?,?,?,?)");
3537
  stmt = mysql_prepare(mysql,query, strlen(query));
3538
  mystmt_init(stmt);
unknown's avatar
unknown committed
3539

3540
  verify_param_count(stmt,6);
unknown's avatar
unknown committed
3541 3542

  /*tinyint*/
unknown's avatar
unknown committed
3543 3544
  bind[0].buffer_type=FIELD_TYPE_TINY;
  bind[0].buffer= (char *)&tData;
unknown's avatar
unknown committed
3545 3546

  /*smallint*/
unknown's avatar
unknown committed
3547 3548
  bind[1].buffer_type=FIELD_TYPE_SHORT;
  bind[1].buffer= (char *)&sData;
unknown's avatar
unknown committed
3549 3550

  /*mediumint*/
unknown's avatar
unknown committed
3551 3552
  bind[2].buffer_type=FIELD_TYPE_LONG;
  bind[2].buffer= (char *)&nData;
unknown's avatar
unknown committed
3553 3554

  /*int*/
unknown's avatar
unknown committed
3555 3556
  bind[3].buffer_type=FIELD_TYPE_LONG;
  bind[3].buffer= (char *)&nData;
unknown's avatar
unknown committed
3557 3558

  /*integer*/
unknown's avatar
unknown committed
3559 3560
  bind[4].buffer_type=FIELD_TYPE_LONG;
  bind[4].buffer= (char *)&nData;
unknown's avatar
unknown committed
3561 3562

  /*bigint*/
unknown's avatar
unknown committed
3563 3564
  bind[5].buffer_type=FIELD_TYPE_LONGLONG;
  bind[5].buffer= (char *)&bData;
unknown's avatar
unknown committed
3565

unknown's avatar
unknown committed
3566 3567 3568 3569
  for (i= 0; i < array_elements(bind); i++)
    bind[i].is_null=0;

  rc = mysql_bind_param(stmt,bind);
unknown's avatar
unknown committed
3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583
  mystmt(stmt, rc);

  /*
  *  integer to integer
  */
  for (nData=0; nData<10; nData++, tData++, sData++,bData++)
  {
    rc = mysql_execute(stmt);
    mystmt(stmt, rc);
  }
  mysql_stmt_close(stmt);

  /* now fetch the results ..*/
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
3584
  myquery(rc);
unknown's avatar
unknown committed
3585

3586 3587
  stmt = mysql_prepare(mysql,"SELECT c1,c2,c3,c4,c5,c6 FROM test_prepare_ext",100);
  mystmt_init(stmt);
unknown's avatar
unknown committed
3588

unknown's avatar
unknown committed
3589
  /* get the result */
3590 3591
  rc = mysql_execute(stmt);
  mystmt(stmt, rc);
unknown's avatar
unknown committed
3592

3593 3594 3595
  myassert(nData == (int)my_process_stmt_result(stmt));

  mysql_stmt_close(stmt);
unknown's avatar
unknown committed
3596 3597 3598 3599 3600 3601 3602
}



/********************************************************
* to test real and alias names                          *
*********************************************************/
3603
static void test_field_names()
unknown's avatar
unknown committed
3604 3605 3606
{
  int        rc;
  MYSQL_RES  *result;
unknown's avatar
unknown committed
3607 3608

  myheader("test_field_names");
unknown's avatar
unknown committed
3609

3610
  fprintf(stdout,"\n %d,%d,%d",MYSQL_TYPE_DECIMAL,MYSQL_TYPE_NEWDATE,MYSQL_TYPE_ENUM);
unknown's avatar
unknown committed
3611 3612 3613 3614 3615
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_field_names1");
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_field_names2");
  myquery(rc);
unknown's avatar
unknown committed
3616 3617

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
3618
  myquery(rc);
unknown's avatar
unknown committed
3619 3620

  rc = mysql_query(mysql,"CREATE TABLE test_field_names1(id int,name varchar(50))");
unknown's avatar
unknown committed
3621
  myquery(rc);
unknown's avatar
unknown committed
3622 3623

  rc = mysql_query(mysql,"CREATE TABLE test_field_names2(id int,name varchar(50))");
unknown's avatar
unknown committed
3624 3625
  myquery(rc);

unknown's avatar
unknown committed
3626
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
3627 3628
  myquery(rc);

unknown's avatar
unknown committed
3629
  /* with table name included with TRUE column name */
unknown's avatar
unknown committed
3630
  rc = mysql_query(mysql,"SELECT id as 'id-alias' FROM test_field_names1");
unknown's avatar
unknown committed
3631 3632
  myquery(rc);

unknown's avatar
unknown committed
3633
  result = mysql_use_result(mysql);
unknown's avatar
unknown committed
3634
  mytest(result);
unknown's avatar
unknown committed
3635

3636
  myassert(0 == my_process_result_set(result));
unknown's avatar
unknown committed
3637
  mysql_free_result(result);
unknown's avatar
unknown committed
3638

unknown's avatar
unknown committed
3639
  /* with table name included with TRUE column name */
unknown's avatar
unknown committed
3640
  rc = mysql_query(mysql,"SELECT t1.id as 'id-alias',test_field_names2.name FROM test_field_names1 t1,test_field_names2");
unknown's avatar
unknown committed
3641 3642
  myquery(rc);

unknown's avatar
unknown committed
3643
  result = mysql_use_result(mysql);
unknown's avatar
unknown committed
3644
  mytest(result);
unknown's avatar
unknown committed
3645

3646
  myassert(0 == my_process_result_set(result));
unknown's avatar
unknown committed
3647 3648 3649 3650 3651 3652
  mysql_free_result(result);
}

/********************************************************
* to test warnings                                      *
*********************************************************/
3653
static void test_warnings()
unknown's avatar
unknown committed
3654 3655 3656 3657
{
  int        rc;
  MYSQL_RES  *result;

unknown's avatar
unknown committed
3658 3659
  myheader("test_warnings");

3660 3661 3662 3663 3664 3665
  mysql_query(mysql, "DROP TABLE if exists test_non_exists");

  rc = mysql_query(mysql, "DROP TABLE if exists test_non_exists");
  myquery(rc);

  fprintf(stdout, "\n total warnings: %d", mysql_warning_count(mysql));
unknown's avatar
unknown committed
3666 3667 3668
  rc = mysql_query(mysql,"SHOW WARNINGS");
  myquery(rc);

3669
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
3670
  mytest(result);
unknown's avatar
unknown committed
3671

3672
  myassert(1 == my_process_result_set(result));
unknown's avatar
unknown committed
3673 3674 3675 3676 3677 3678
  mysql_free_result(result);
}

/********************************************************
* to test errors                                        *
*********************************************************/
3679
static void test_errors()
unknown's avatar
unknown committed
3680 3681 3682
{
  int        rc;
  MYSQL_RES  *result;
unknown's avatar
unknown committed
3683 3684 3685

  myheader("test_errors");

3686 3687 3688 3689 3690
  mysql_query(mysql, "DROP TABLE if exists test_non_exists");

  rc = mysql_query(mysql, "DROP TABLE test_non_exists");
  myquery_r(rc);

unknown's avatar
unknown committed
3691 3692 3693
  rc = mysql_query(mysql,"SHOW ERRORS");
  myquery(rc);

3694
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
3695
  mytest(result);
unknown's avatar
unknown committed
3696

unknown's avatar
unknown committed
3697
  my_process_result_set(result);
unknown's avatar
unknown committed
3698 3699 3700 3701 3702 3703 3704 3705
  mysql_free_result(result);
}



/********************************************************
* to test simple prepare-insert                         *
*********************************************************/
3706
static void test_insert()
unknown's avatar
unknown committed
3707
{
unknown's avatar
unknown committed
3708
  MYSQL_STMT *stmt;
unknown's avatar
unknown committed
3709
  int        rc;
unknown's avatar
unknown committed
3710 3711 3712
  char       str_data[50];
  char       tiny_data;
  MYSQL_RES  *result;
unknown's avatar
unknown committed
3713
  MYSQL_BIND bind[2];
3714
  ulong	     length;
unknown's avatar
unknown committed
3715

unknown's avatar
unknown committed
3716
  myheader("test_insert");
unknown's avatar
unknown committed
3717

unknown's avatar
unknown committed
3718
  rc = mysql_autocommit(mysql, TRUE);
unknown's avatar
unknown committed
3719 3720 3721 3722
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_prep_insert");
  myquery(rc);
unknown's avatar
unknown committed
3723 3724

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
3725
  myquery(rc);
unknown's avatar
unknown committed
3726 3727 3728

  rc = mysql_query(mysql,"CREATE TABLE test_prep_insert(col1 tinyint,\
                                col2 varchar(50))");
unknown's avatar
unknown committed
3729
  myquery(rc);
unknown's avatar
unknown committed
3730 3731

  /* insert by prepare */
3732
  stmt = mysql_prepare(mysql, "INSERT INTO test_prep_insert VALUES(?,?)", 70);
3733
  mystmt_init(stmt);
unknown's avatar
unknown committed
3734

3735
  verify_param_count(stmt,2);
unknown's avatar
unknown committed
3736 3737 3738

  /* tinyint */
  bind[0].buffer_type=FIELD_TYPE_TINY;
unknown's avatar
unknown committed
3739 3740
  bind[0].buffer=(char *)&tiny_data;
  bind[0].is_null= 0;
3741
  bind[0].length= 0;
unknown's avatar
unknown committed
3742

unknown's avatar
unknown committed
3743 3744 3745
  /* string */
  bind[1].buffer_type=FIELD_TYPE_STRING;
  bind[1].buffer=str_data;
unknown's avatar
unknown committed
3746 3747
  bind[1].buffer_length=sizeof(str_data);;
  bind[1].is_null= 0;
3748
  bind[1].length= &length;
unknown's avatar
unknown committed
3749

unknown's avatar
unknown committed
3750 3751 3752 3753 3754 3755
  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  /* now, execute the prepared statement to insert 10 records.. */
  for (tiny_data=0; tiny_data < 3; tiny_data++)
  {
3756
    length = my_sprintf(str_data, (str_data, "MySQL%d",tiny_data));
unknown's avatar
unknown committed
3757 3758 3759 3760 3761 3762 3763 3764
    rc = mysql_execute(stmt);
    mystmt(stmt, rc);
  }

  mysql_stmt_close(stmt);

  /* now fetch the results ..*/
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
3765
  myquery(rc);
unknown's avatar
unknown committed
3766 3767 3768

  /* test the results now, only one row should exists */
  rc = mysql_query(mysql,"SELECT * FROM test_prep_insert");
unknown's avatar
unknown committed
3769 3770
  myquery(rc);

unknown's avatar
unknown committed
3771 3772
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
3773
  mytest(result);
unknown's avatar
unknown committed
3774

3775
  myassert((int)tiny_data == my_process_result_set(result));
unknown's avatar
unknown committed
3776 3777 3778 3779 3780 3781 3782
  mysql_free_result(result);

}

/********************************************************
* to test simple prepare-resultset info                 *
*********************************************************/
3783
static void test_prepare_resultset()
unknown's avatar
unknown committed
3784
{
unknown's avatar
unknown committed
3785
  MYSQL_STMT *stmt;
3786
  int        rc;
unknown's avatar
unknown committed
3787 3788
  MYSQL_RES  *result;

unknown's avatar
unknown committed
3789
  myheader("test_prepare_resultset");
unknown's avatar
unknown committed
3790

unknown's avatar
unknown committed
3791
  rc = mysql_autocommit(mysql, TRUE);
unknown's avatar
unknown committed
3792 3793 3794 3795
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_prepare_resultset");
  myquery(rc);
unknown's avatar
unknown committed
3796 3797

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
3798
  myquery(rc);
unknown's avatar
unknown committed
3799 3800 3801

  rc = mysql_query(mysql,"CREATE TABLE test_prepare_resultset(id int,\
                                name varchar(50),extra double)");
unknown's avatar
unknown committed
3802
  myquery(rc);
unknown's avatar
unknown committed
3803

3804
  stmt = mysql_prepare(mysql, "SELECT * FROM test_prepare_resultset", 60);
3805
  mystmt_init(stmt);
unknown's avatar
unknown committed
3806

3807
  verify_param_count(stmt,0);
unknown's avatar
unknown committed
3808

unknown's avatar
unknown committed
3809
  result = mysql_get_metadata(stmt);
unknown's avatar
unknown committed
3810 3811
  mytest(result);
  my_print_result_metadata(result);
unknown's avatar
unknown committed
3812 3813 3814 3815 3816 3817 3818
  mysql_stmt_close(stmt);
}

/********************************************************
* to test field flags (verify .NET provider)            *
*********************************************************/

3819
static void test_field_flags()
unknown's avatar
unknown committed
3820 3821 3822 3823 3824 3825 3826
{
  int          rc;
  MYSQL_RES    *result;
  MYSQL_FIELD  *field;
  unsigned int i;


unknown's avatar
unknown committed
3827 3828 3829 3830 3831
  myheader("test_field_flags");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_field_flags");
  myquery(rc);

unknown's avatar
unknown committed
3832
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
3833
  myquery(rc);
unknown's avatar
unknown committed
3834 3835 3836 3837 3838 3839 3840 3841

  rc = mysql_query(mysql,"CREATE TABLE test_field_flags(id int NOT NULL AUTO_INCREMENT PRIMARY KEY,\
                                                        id1 int NOT NULL,\
                                                        id2 int UNIQUE,\
                                                        id3 int,\
                                                        id4 int NOT NULL,\
                                                        id5 int,\
                                                        KEY(id3,id4))");
unknown's avatar
unknown committed
3842
  myquery(rc);
unknown's avatar
unknown committed
3843 3844

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
3845 3846
  myquery(rc);

unknown's avatar
unknown committed
3847
  /* with table name included with TRUE column name */
unknown's avatar
unknown committed
3848
  rc = mysql_query(mysql,"SELECT * FROM test_field_flags");
unknown's avatar
unknown committed
3849 3850
  myquery(rc);

unknown's avatar
unknown committed
3851
  result = mysql_use_result(mysql);
unknown's avatar
unknown committed
3852
  mytest(result);
unknown's avatar
unknown committed
3853 3854 3855 3856 3857 3858 3859

  mysql_field_seek(result,0);
  fputc('\n', stdout);  

  for(i=0; i< mysql_num_fields(result); i++)
  {
    field = mysql_fetch_field(result);
3860
    fprintf(stdout,"\n field:%d",i);
unknown's avatar
unknown committed
3861
    if (field->flags & NOT_NULL_FLAG)
3862
      fprintf(stdout,"\n  NOT_NULL_FLAG");
unknown's avatar
unknown committed
3863
    if (field->flags & PRI_KEY_FLAG)
3864
      fprintf(stdout,"\n  PRI_KEY_FLAG");
unknown's avatar
unknown committed
3865
    if (field->flags & UNIQUE_KEY_FLAG)
3866
      fprintf(stdout,"\n  UNIQUE_KEY_FLAG");
unknown's avatar
unknown committed
3867
    if (field->flags & MULTIPLE_KEY_FLAG)
3868
      fprintf(stdout,"\n  MULTIPLE_KEY_FLAG");
unknown's avatar
unknown committed
3869
    if (field->flags & AUTO_INCREMENT_FLAG)
3870 3871 3872 3873 3874
      fprintf(stdout,"\n  AUTO_INCREMENT_FLAG");

  }
  mysql_free_result(result);
}
unknown's avatar
unknown committed
3875

3876 3877 3878 3879 3880 3881 3882
/**************************************************************
 * Test mysql_stmt_close for open stmts                       *
**************************************************************/
static void test_stmt_close()
{
  MYSQL *lmysql;
  MYSQL_STMT *stmt1, *stmt2, *stmt3, *stmt_x;
unknown's avatar
unknown committed
3883
  MYSQL_BIND  bind[1];
3884 3885 3886 3887 3888 3889
  MYSQL_RES   *result;
  unsigned int  count;
  int   rc;
  
  myheader("test_stmt_close");  

3890
  fprintf(stdout, "\n Establishing a test connection ...");
unknown's avatar
unknown committed
3891
  if (!(lmysql = mysql_init(NULL)))
3892
  { 
3893
    myerror("mysql_init() failed");
3894
    exit(0);
unknown's avatar
unknown committed
3895
  }
3896
  if (!(mysql_real_connect(lmysql,opt_host,opt_user,
3897
			   opt_password, current_db, opt_port,
3898 3899 3900 3901 3902
			   opt_unix_socket, 0)))
  {
    myerror("connection failed");
    exit(0);
  }   
3903 3904
  fprintf(stdout," OK");
  
3905 3906

  /* set AUTOCOMMIT to ON*/
unknown's avatar
unknown committed
3907
  mysql_autocommit(lmysql, TRUE);
3908 3909 3910 3911 3912 3913
  
  rc = mysql_query(lmysql,"DROP TABLE IF EXISTS test_stmt_close");
  myquery(rc);
  
  rc = mysql_query(lmysql,"CREATE TABLE test_stmt_close(id int)");
  myquery(rc);
3914

unknown's avatar
unknown committed
3915
  strmov(query,"ALTER TABLE test_stmt_close ADD name varchar(20)");
3916
  stmt1= mysql_prepare(lmysql, query, 70);
3917
  mystmt_init(stmt1);
3918 3919 3920
  
  verify_param_count(stmt1, 0);
  
unknown's avatar
unknown committed
3921
  strmov(query,"INSERT INTO test_stmt_close(id) VALUES(?)");
3922
  stmt_x= mysql_prepare(mysql, query, 70);
3923
  mystmt_init(stmt_x);
3924 3925 3926

  verify_param_count(stmt_x, 1);
  
unknown's avatar
unknown committed
3927
  strmov(query,"UPDATE test_stmt_close SET id=? WHERE id=?");
3928
  stmt3= mysql_prepare(lmysql, query, 70);
3929
  mystmt_init(stmt3);
3930 3931 3932
  
  verify_param_count(stmt3, 2);
  
unknown's avatar
unknown committed
3933
  strmov(query,"SELECT * FROM test_stmt_close WHERE id=?");
3934
  stmt2= mysql_prepare(lmysql, query, 70);
3935
  mystmt_init(stmt2);
3936 3937

  verify_param_count(stmt2, 1);
3938 3939 3940 3941

  rc= mysql_stmt_close(stmt1);
  fprintf(stdout,"\n mysql_close_stmt(1) returned: %d", rc);
  myassert(rc == 0);
3942
  
3943 3944 3945
  mysql_close(lmysql); /* it should free all open stmts(stmt3, 2 and 1) */
 
#if NOT_VALID
3946 3947 3948
  rc= mysql_stmt_close(stmt3);
  fprintf(stdout,"\n mysql_close_stmt(3) returned: %d", rc);
  myassert( rc == 1);
3949
  
3950 3951 3952
  rc= mysql_stmt_close(stmt2);
  fprintf(stdout,"\n mysql_close_stmt(2) returned: %d", rc);
  myassert( rc == 1);
3953
#endif
3954 3955

  count= 100;
unknown's avatar
unknown committed
3956 3957 3958 3959 3960
  bind[0].buffer=(char *)&count;
  bind[0].buffer_type=MYSQL_TYPE_LONG;
  bind[0].is_null=0;

  rc = mysql_bind_param(stmt_x, bind);
3961
  mystmt(stmt_x, rc);
3962
  
3963 3964 3965
  rc = mysql_execute(stmt_x);
  mystmt(stmt_x, rc);

3966
  verify_st_affected_rows(stmt_x, 1);
3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981

  rc= mysql_stmt_close(stmt_x);
  fprintf(stdout,"\n mysql_close_stmt(x) returned: %d", rc);
  myassert( rc == 0);

  rc = mysql_query(mysql,"SELECT id FROM test_stmt_close");
  myquery(rc);

  result = mysql_store_result(mysql);
  mytest(result);

  myassert(1 == my_process_result_set(result));
  mysql_free_result(result);
}

unknown's avatar
unknown committed
3982

3983 3984 3985 3986 3987
/********************************************************
 * To test simple set-variable prepare                   *
*********************************************************/
static void test_set_variable()
{
3988 3989 3990 3991 3992 3993
  MYSQL_STMT *stmt, *stmt1;
  int        rc;
  int        set_count, def_count, get_count;
  ulong      length;
  char       var[NAME_LEN+1];
  MYSQL_BIND set_bind[1], get_bind[2];
3994 3995 3996

  myheader("test_set_variable");

3997 3998 3999 4000
  mysql_autocommit(mysql, TRUE);
  
  stmt1 = mysql_prepare(mysql, "show variables like 'max_error_count'", 50);
  mystmt_init(stmt1);
4001

4002 4003 4004 4005 4006 4007
  get_bind[0].buffer_type= MYSQL_TYPE_STRING;
  get_bind[0].buffer= (char *)var;
  get_bind[0].is_null= 0;
  get_bind[0].length= &length;
  get_bind[0].buffer_length= (int)NAME_LEN;
  length= NAME_LEN;
4008

4009 4010 4011 4012
  get_bind[1].buffer_type= MYSQL_TYPE_LONG;
  get_bind[1].buffer= (char *)&get_count;
  get_bind[1].is_null= 0;
  get_bind[1].length= 0;
4013

4014 4015 4016 4017 4018
  rc = mysql_execute(stmt1);
  mystmt(stmt1, rc);
  
  rc = mysql_bind_result(stmt1, get_bind);
  mystmt(stmt1, rc);
4019

4020 4021
  rc = mysql_fetch(stmt1);
  mystmt(stmt1, rc);
4022

4023 4024
  fprintf(stdout, "\n max_error_count(default): %d", get_count);
  def_count= get_count;
4025

4026 4027 4028
  myassert(strcmp(var,"max_error_count") == 0);
  rc = mysql_fetch(stmt1);
  myassert(rc == MYSQL_NO_DATA);
4029

4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041
  stmt = mysql_prepare(mysql, "set max_error_count=?", 50);
  mystmt_init(stmt);

  set_bind[0].buffer_type= MYSQL_TYPE_LONG;
  set_bind[0].buffer= (char *)&set_count;
  set_bind[0].is_null= 0;
  set_bind[0].length= 0;
  
  rc = mysql_bind_param(stmt, set_bind);
  mystmt(stmt,rc);
  
  set_count= 31;
4042 4043 4044
  rc= mysql_execute(stmt);
  mystmt(stmt,rc);

4045
  mysql_commit(mysql);
4046

4047 4048 4049 4050 4051
  rc = mysql_execute(stmt1);
  mystmt(stmt1, rc);
  
  rc = mysql_fetch(stmt1);
  mystmt(stmt1, rc);
4052

4053 4054
  fprintf(stdout, "\n max_error_count         : %d", get_count);
  myassert(get_count == set_count);
4055

4056 4057 4058 4059 4060
  rc = mysql_fetch(stmt1);
  myassert(rc == MYSQL_NO_DATA);
  
  /* restore back to default */
  set_count= def_count;
4061
  rc= mysql_execute(stmt);
4062 4063 4064 4065 4066 4067 4068
  mystmt(stmt, rc);
  
  rc = mysql_execute(stmt1);
  mystmt(stmt1, rc);
  
  rc = mysql_fetch(stmt1);
  mystmt(stmt1, rc);
4069

4070 4071
  fprintf(stdout, "\n max_error_count(default): %d", get_count);
  myassert(get_count == set_count);
4072

4073 4074
  rc = mysql_fetch(stmt1);
  myassert(rc == MYSQL_NO_DATA);
4075 4076
  
  mysql_stmt_close(stmt);
4077
  mysql_stmt_close(stmt1);
4078
}
unknown's avatar
unknown committed
4079

4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090
#if NOT_USED
/* Insert meta info .. */
static void test_insert_meta()
{
  MYSQL_STMT *stmt;
  int        rc;
  MYSQL_RES  *result;
  MYSQL_FIELD *field;

  myheader("test_insert_meta");

unknown's avatar
unknown committed
4091
  rc = mysql_autocommit(mysql, TRUE);
4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_prep_insert");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_prep_insert(col1 tinyint,\
                                col2 varchar(50), col3 varchar(30))");
  myquery(rc);

unknown's avatar
unknown committed
4104
  strmov(query,"INSERT INTO test_prep_insert VALUES(10,'venu1','test')");
4105
  stmt = mysql_prepare(mysql, query, 70);
4106 4107 4108 4109 4110 4111 4112
  mystmt_init(stmt);

  verify_param_count(stmt,0);

  result= mysql_param_result(stmt);
  mytest_r(result);

4113 4114
  mysql_stmt_close(stmt);

unknown's avatar
unknown committed
4115
  strmov(query,"INSERT INTO test_prep_insert VALUES(?,'venu',?)");
4116
  stmt = mysql_prepare(mysql, query, 70);
4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153
  mystmt_init(stmt);

  verify_param_count(stmt,2);

  result= mysql_param_result(stmt);
  mytest(result);

  my_print_result_metadata(result);

  mysql_field_seek(result, 0);
  field= mysql_fetch_field(result);
  mytest(field);
  fprintf(stdout, "\n obtained: `%s` (expected: `%s`)", field->name, "col1");
  myassert(strcmp(field->name,"col1")==0);

  field= mysql_fetch_field(result);
  mytest(field);
  fprintf(stdout, "\n obtained: `%s` (expected: `%s`)", field->name, "col3");
  myassert(strcmp(field->name,"col3")==0);

  field= mysql_fetch_field(result);
  mytest_r(field);

  mysql_free_result(result);
  mysql_stmt_close(stmt);
}

/* Update meta info .. */
static void test_update_meta()
{
  MYSQL_STMT *stmt;
  int        rc;
  MYSQL_RES  *result;
  MYSQL_FIELD *field;

  myheader("test_update_meta");

unknown's avatar
unknown committed
4154
  rc = mysql_autocommit(mysql, TRUE);
4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_prep_update");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_prep_update(col1 tinyint,\
                                col2 varchar(50), col3 varchar(30))");
  myquery(rc);

unknown's avatar
unknown committed
4167
  strmov(query,"UPDATE test_prep_update SET col1=10, col2='venu1' WHERE col3='test'");
4168
  stmt = mysql_prepare(mysql, query, 100);
4169 4170 4171 4172 4173 4174 4175
  mystmt_init(stmt);

  verify_param_count(stmt,0);

  result= mysql_param_result(stmt);
  mytest_r(result);

4176 4177
  mysql_stmt_close(stmt);

unknown's avatar
unknown committed
4178
  strmov(query,"UPDATE test_prep_update SET col1=?, col2='venu' WHERE col3=?");
4179
  stmt = mysql_prepare(mysql, query, 100);
4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220
  mystmt_init(stmt);

  verify_param_count(stmt,2);

  result= mysql_param_result(stmt);
  mytest(result);

  my_print_result_metadata(result);

  mysql_field_seek(result, 0);
  field= mysql_fetch_field(result);
  mytest(field);
  fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col1");
  fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_update");
  myassert(strcmp(field->name,"col1")==0);
  myassert(strcmp(field->table,"test_prep_update")==0);

  field= mysql_fetch_field(result);
  mytest(field);
  fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col3");
  fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_update");
  myassert(strcmp(field->name,"col3")==0);
  myassert(strcmp(field->table,"test_prep_update")==0);

  field= mysql_fetch_field(result);
  mytest_r(field);

  mysql_free_result(result);
  mysql_stmt_close(stmt);
}

/* Select meta info .. */
static void test_select_meta()
{
  MYSQL_STMT *stmt;
  int        rc;
  MYSQL_RES  *result;
  MYSQL_FIELD *field;

  myheader("test_select_meta");

unknown's avatar
unknown committed
4221
  rc = mysql_autocommit(mysql, TRUE);
4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_prep_select");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_prep_select(col1 tinyint,\
                                col2 varchar(50), col3 varchar(30))");
  myquery(rc);

unknown's avatar
unknown committed
4234
  strmov(query,"SELECT * FROM test_prep_select WHERE col1=10");
4235
  stmt = mysql_prepare(mysql, query, 70);
4236 4237 4238 4239 4240 4241 4242
  mystmt_init(stmt);

  verify_param_count(stmt,0);

  result= mysql_param_result(stmt);
  mytest_r(result);

unknown's avatar
unknown committed
4243
  strmov(query,"SELECT col1, col3 from test_prep_select WHERE col1=? AND col3='test' AND col2= ?");
4244
  stmt = mysql_prepare(mysql, query, 120);
4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270
  mystmt_init(stmt);

  verify_param_count(stmt,2);

  result= mysql_param_result(stmt);
  mytest(result);

  my_print_result_metadata(result);

  mysql_field_seek(result, 0);
  field= mysql_fetch_field(result);
  mytest(field);
  fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col1");
  fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_select");
  myassert(strcmp(field->name,"col1")==0);
  myassert(strcmp(field->table,"test_prep_select")==0);

  field= mysql_fetch_field(result);
  mytest(field);
  fprintf(stdout, "\n col obtained: `%s` (expected: `%s`)", field->name, "col2");
  fprintf(stdout, "\n tab obtained: `%s` (expected: `%s`)", field->table, "test_prep_select");
  myassert(strcmp(field->name,"col2")==0);
  myassert(strcmp(field->table,"test_prep_select")==0);

  field= mysql_fetch_field(result);
  mytest_r(field);
4271

4272 4273 4274 4275 4276
  mysql_free_result(result);
  mysql_stmt_close(stmt);
}
#endif

unknown's avatar
unknown committed
4277

4278 4279 4280 4281 4282 4283 4284 4285 4286
/* Test FUNCTION field info / DATE_FORMAT() table_name . */
static void test_func_fields()
{
  int        rc;
  MYSQL_RES  *result;
  MYSQL_FIELD *field;

  myheader("test_func_fields");

unknown's avatar
unknown committed
4287
  rc = mysql_autocommit(mysql, TRUE);
4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_dateformat");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_dateformat(id int, \
                                                       ts timestamp)");
  myquery(rc);

  rc = mysql_query(mysql, "INSERT INTO test_dateformat(id) values(10)");
  myquery(rc);

  rc = mysql_query(mysql, "SELECT ts FROM test_dateformat");
  myquery(rc);

  result = mysql_store_result(mysql);
  mytest(result);

  field = mysql_fetch_field(result);
  mytest(field);
  fprintf(stdout,"\n table name: `%s` (expected: `%s`)", field->table,
                  "test_dateformat");
  myassert(strcmp(field->table, "test_dateformat")==0);

  field = mysql_fetch_field(result);
  mytest_r(field); /* no more fields */

  mysql_free_result(result);

  /* DATE_FORMAT */
  rc = mysql_query(mysql, "SELECT DATE_FORMAT(ts,'%Y') AS 'venu' FROM test_dateformat");
  myquery(rc);

  result = mysql_store_result(mysql);
  mytest(result);

  field = mysql_fetch_field(result);
  mytest(field);
  fprintf(stdout,"\n table name: `%s` (expected: `%s`)", field->table, "");
  myassert(field->table[0] == '\0');

  field = mysql_fetch_field(result);
  mytest_r(field); /* no more fields */

  mysql_free_result(result);

  /* FIELD ALIAS TEST */
  rc = mysql_query(mysql, "SELECT DATE_FORMAT(ts,'%Y')  AS 'YEAR' FROM test_dateformat");
  myquery(rc);

  result = mysql_store_result(mysql);
  mytest(result);

  field = mysql_fetch_field(result);
  mytest(field);
  fprintf(stdout,"\n field name: `%s` (expected: `%s`)", field->name, "YEAR");
  fprintf(stdout,"\n field org name: `%s` (expected: `%s`)",field->org_name,"");
  myassert(strcmp(field->name, "YEAR")==0);
  myassert(field->org_name[0] == '\0');

  field = mysql_fetch_field(result);
  mytest_r(field); /* no more fields */

unknown's avatar
unknown committed
4354 4355 4356
  mysql_free_result(result);
}

unknown's avatar
unknown committed
4357

unknown's avatar
unknown committed
4358 4359 4360
/* Multiple stmts .. */
static void test_multi_stmt()
{
4361 4362

  MYSQL_STMT  *stmt, *stmt1, *stmt2;
unknown's avatar
unknown committed
4363
  int         rc, id;
4364
  char        name[50];
unknown's avatar
unknown committed
4365
  MYSQL_BIND  bind[2];
unknown's avatar
unknown committed
4366 4367
  ulong       length[2];
  my_bool     is_null[2];
unknown's avatar
unknown committed
4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381
  myheader("test_multi_stmt");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_multi_table");
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_multi_table(id int, name char(20))");
  myquery(rc);

  rc = mysql_query(mysql,"INSERT INTO test_multi_table values(10,'mysql')");
  myquery(rc);

  stmt = mysql_prepare(mysql, "SELECT * FROM test_multi_table WHERE id = ?", 100);
  mystmt_init(stmt);

4382 4383 4384
  stmt2 = mysql_prepare(mysql, "UPDATE test_multi_table SET name='updated' WHERE id=10",100);
  mystmt_init(stmt2);

unknown's avatar
unknown committed
4385 4386
  verify_param_count(stmt,1);

unknown's avatar
unknown committed
4387 4388 4389
  bind[0].buffer_type= MYSQL_TYPE_SHORT;
  bind[0].buffer= (char *)&id;
  bind[0].is_null= &is_null[0];
4390 4391 4392
  bind[0].length= &length[0];
  is_null[0]= 0;
  length[0]= 0;
unknown's avatar
unknown committed
4393 4394

  bind[1].buffer_type = MYSQL_TYPE_STRING;
4395
  bind[1].buffer = (char *)name;
unknown's avatar
unknown committed
4396
  bind[1].length = &length[1];
4397 4398
  bind[1].is_null= &is_null[1];
    
unknown's avatar
unknown committed
4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412
  rc = mysql_bind_param(stmt, bind);
  mystmt(stmt, rc);
  
  rc = mysql_bind_result(stmt, bind);
  mystmt(stmt, rc);

  id = 10;
  rc = mysql_execute(stmt);
  mystmt(stmt, rc);
  
  id = 999;  
  rc = mysql_fetch(stmt);
  mystmt(stmt, rc);

4413 4414
  fprintf(stdout, "\n int_data: %d(%lu)", id, length[0]);
  fprintf(stdout, "\n str_data: %s(%lu)", name, length[1]);
unknown's avatar
unknown committed
4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428
  myassert(id == 10);
  myassert(strcmp(name,"mysql")==0);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  /* alter the table schema now */
  stmt1 = mysql_prepare(mysql,"DELETE FROM test_multi_table WHERE id = ? AND name=?",100);
  mystmt_init(stmt1);

  verify_param_count(stmt1,2);

  rc = mysql_bind_param(stmt1, bind);
  mystmt(stmt1, rc);
4429 4430 4431 4432
  
  rc = mysql_execute(stmt2);
  mystmt(stmt2, rc);

4433
  verify_st_affected_rows(stmt2, 1);
4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);
  
  rc = mysql_fetch(stmt);
  mystmt(stmt, rc);

  fprintf(stdout, "\n int_data: %d(%lu)", id, length[0]);
  fprintf(stdout, "\n str_data: %s(%lu)", name, length[1]);
  myassert(id == 10);
  myassert(strcmp(name,"updated")==0);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);
unknown's avatar
unknown committed
4448 4449 4450 4451

  rc = mysql_execute(stmt1);
  mystmt(stmt1, rc);

4452
  verify_st_affected_rows(stmt1, 1);
unknown's avatar
unknown committed
4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464

  mysql_stmt_close(stmt1);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  myassert(0 == my_stmt_result("SELECT * FROM test_multi_table",50));

  mysql_stmt_close(stmt);
4465 4466
  mysql_stmt_close(stmt2);

unknown's avatar
unknown committed
4467 4468
}

unknown's avatar
unknown committed
4469

4470 4471 4472 4473 4474 4475 4476 4477
/********************************************************
* to test simple sample - manual                        *
*********************************************************/
static void test_manual_sample()
{
  unsigned int param_count;
  MYSQL_STMT   *stmt;
  short        small_data;
4478 4479
  int          int_data;
  char         str_data[50];
4480
  ulonglong    affected_rows;
unknown's avatar
unknown committed
4481
  MYSQL_BIND   bind[3];
4482
  my_bool      is_null;
4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507

  myheader("test_manual_sample");

  /*
    Sample which is incorporated directly in the manual under Prepared 
    statements section (Example from mysql_execute()
  */

  mysql_autocommit(mysql, 1);
  if (mysql_query(mysql,"DROP TABLE IF EXISTS test_table"))
  {
    fprintf(stderr, "\n drop table failed");
    fprintf(stderr, "\n %s", mysql_error(mysql));
    exit(0);
  }
  if (mysql_query(mysql,"CREATE TABLE test_table(col1 int, col2 varchar(50), \
                                                 col3 smallint,\
                                                 col4 timestamp(14))"))
  {
    fprintf(stderr, "\n create table failed");
    fprintf(stderr, "\n %s", mysql_error(mysql));
    exit(0);
  }
  
  /* Prepare a insert query with 3 parameters */
unknown's avatar
unknown committed
4508 4509
  strmov(query, "INSERT INTO test_table(col1,col2,col3) values(?,?,?)");
  if (!(stmt = mysql_prepare(mysql,query,strlen(query))))
4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530
  {
    fprintf(stderr, "\n prepare, insert failed");
    fprintf(stderr, "\n %s", mysql_error(mysql));
    exit(0);
  }
  fprintf(stdout, "\n prepare, insert successful");

  /* Get the parameter count from the statement */
  param_count= mysql_param_count(stmt);

  fprintf(stdout, "\n total parameters in insert: %d", param_count);
  if (param_count != 3) /* validate parameter count */
  {
    fprintf(stderr, "\n invalid parameter count returned by MySQL");
    exit(0);
  }

  /* Bind the data for the parameters */

  /* INTEGER PART */
  bind[0].buffer_type= MYSQL_TYPE_LONG;
unknown's avatar
unknown committed
4531
  bind[0].buffer= (char *)&int_data;
4532 4533
  bind[0].is_null= 0;
  bind[0].length= 0;
unknown's avatar
unknown committed
4534

4535 4536
  /* STRING PART */
  bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
unknown's avatar
unknown committed
4537
  bind[1].buffer= (char *)str_data;
4538
  bind[1].buffer_length= sizeof(str_data);
4539 4540
  bind[1].is_null= 0;
  bind[1].length= 0;
4541 4542 4543
 
  /* SMALLINT PART */
  bind[2].buffer_type= MYSQL_TYPE_SHORT;
unknown's avatar
unknown committed
4544
  bind[2].buffer= (char *)&small_data;       
4545 4546 4547
  bind[2].is_null= &is_null;
  bind[2].length= 0;
  is_null= 0;
4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558

  /* Bind the buffers */
  if (mysql_bind_param(stmt, bind))
  {
    fprintf(stderr, "\n param bind failed");
    fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
    exit(0);
  }

  /* Specify the data */
  int_data= 10;             /* integer */
unknown's avatar
unknown committed
4559
  strmov(str_data,"MySQL"); /* string  */
4560
  
4561
  /* INSERT SMALLINT data as NULL */
4562
  is_null= 1;
4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583

  /* Execute the insert statement - 1*/
  if (mysql_execute(stmt))
  {
    fprintf(stderr, "\n execute 1 failed");
    fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
    exit(0);
  }
    
  /* Get the total rows affected */   
  affected_rows= mysql_stmt_affected_rows(stmt);

  fprintf(stdout, "\n total affected rows: %lld", affected_rows);
  if (affected_rows != 1) /* validate affected rows */
  {
    fprintf(stderr, "\n invalid affected rows by MySQL");
    exit(0);
  }

  /* Re-execute the insert, by changing the values */
  int_data= 1000;             
unknown's avatar
unknown committed
4584
  strmov(str_data,"The most popular open source database"); 
4585
  small_data= 1000;         /* smallint */
4586
  is_null= 0;               /* reset */
4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624

  /* Execute the insert statement - 2*/
  if (mysql_execute(stmt))
  {
    fprintf(stderr, "\n execute 2 failed");
    fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
    exit(0);
  }
    
  /* Get the total rows affected */   
  affected_rows= mysql_stmt_affected_rows(stmt);

  fprintf(stdout, "\n total affected rows: %lld", affected_rows);
  if (affected_rows != 1) /* validate affected rows */
  {
    fprintf(stderr, "\n invalid affected rows by MySQL");
    exit(0);
  }

  /* Close the statement */
  if (mysql_stmt_close(stmt))
  {
    fprintf(stderr, "\n failed while closing the statement");
    fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
    exit(0);
  }
  myassert(2 == my_stmt_result("SELECT * FROM test_table",50));

  /* DROP THE TABLE */
  if (mysql_query(mysql,"DROP TABLE test_table"))
  {
    fprintf(stderr, "\n drop table failed");
    fprintf(stderr, "\n %s", mysql_error(mysql));
    exit(0);
  }
  fprintf(stdout, "Success !!!");
}

unknown's avatar
unknown committed
4625

unknown's avatar
unknown committed
4626 4627 4628 4629 4630 4631 4632 4633 4634
/********************************************************
* to test alter table scenario in the middle of prepare *
*********************************************************/
static void test_prepare_alter()
{
  MYSQL_STMT  *stmt;
  int         rc, id;
  long        length;
  MYSQL_BIND  bind[1];
unknown's avatar
unknown committed
4635
  my_bool     is_null;
unknown's avatar
unknown committed
4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653

  myheader("test_prepare_alter");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_prep_alter");
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_prep_alter(id int, name char(20))");
  myquery(rc);

  rc = mysql_query(mysql,"INSERT INTO test_prep_alter values(10,'venu'),(20,'mysql')");
  myquery(rc);

  stmt = mysql_prepare(mysql, "INSERT INTO test_prep_alter VALUES(?,'monty')", 100);
  mystmt_init(stmt);

  verify_param_count(stmt,1);

  bind[0].buffer_type= MYSQL_TYPE_SHORT;
unknown's avatar
unknown committed
4654 4655
  bind[0].buffer= (char *)&id;
  bind[0].is_null= &is_null;
unknown's avatar
unknown committed
4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666

  rc = mysql_bind_param(stmt, bind);
  mystmt(stmt, rc);
  
  id = 30; length= 0;
  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  if (thread_query((char *)"ALTER TABLE test_prep_alter change id id_new varchar(20)"))
    exit(0);

unknown's avatar
unknown committed
4667
  is_null=1;
unknown's avatar
unknown committed
4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685
  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  myassert(4 == my_stmt_result("SELECT * FROM test_prep_alter",50));

  mysql_stmt_close(stmt);
}

/********************************************************
* to test the support of multi-query executions         *
*********************************************************/
static void test_multi_query()
{
  MYSQL *l_mysql, *org_mysql;
  MYSQL_RES *result;
  int    rc;

  const char *query= "DROP TABLE IF EXISTS test_multi_tab;\
4686 4687 4688
                  CREATE TABLE test_multi_tab(id int,name char(20));\
                  INSERT INTO test_multi_tab(xxxx) VALUES(10);\
                  UPDATE test_multi_tab SET id=10 WHERE unkown_col=10;\
unknown's avatar
unknown committed
4689 4690 4691 4692 4693 4694 4695 4696 4697
                  CREATE TABLE test_multi_tab(id int,name char(20));\
                  INSERT INTO test_multi_tab(id) VALUES(10),(20);\
                  INSERT INTO test_multi_tab VALUES(20,'insert;comma');\
                  SELECT * FROM test_multi_tab;\
                  UPDATE test_multi_tab SET unknown_col=100 WHERE id=100;\
                  UPDATE test_multi_tab SET name='new;name' WHERE id=20;\
                  DELETE FROM test_multi_tab WHERE name='new;name';\
                  SELECT * FROM test_multi_tab;\
                  DELETE FROM test_multi_tab WHERE id=10;\
4698 4699 4700 4701 4702 4703
                  SELECT * FROM test_multi_tab;\
                  DROP TABLE test_multi_tab;\
                  DROP TABLE test_multi_tab;\
                  DROP TABLE IF EXISTS test_multi_tab";
  uint  count, rows[16]={0,1054,1054,1050,2,1,3,1054,2,2,1,1,0,0,1051,0}, exp_value;
  
unknown's avatar
unknown committed
4704 4705 4706 4707 4708
  myheader("test_multi_query");

  rc = mysql_query(mysql, query); /* syntax error */
  myquery_r(rc);

4709 4710 4711
  myassert(0 == mysql_next_result(mysql));
  myassert(0 == mysql_more_results(mysql));

unknown's avatar
unknown committed
4712
  if (!(l_mysql = mysql_init(NULL)))
unknown's avatar
unknown committed
4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729
  { 
    fprintf(stdout,"\n mysql_init() failed");
    exit(1);
  }
  if (!(mysql_real_connect(l_mysql,opt_host,opt_user,
			   opt_password, current_db, opt_port,
			   opt_unix_socket, CLIENT_MULTI_QUERIES))) /* enable multi queries */
  {
    fprintf(stdout,"\n connection failed(%s)", mysql_error(l_mysql));    
    exit(1);
  }    
  org_mysql= mysql;
  mysql= l_mysql;

  rc = mysql_query(mysql, query);
  myquery(rc);

4730 4731
  count= exp_value= 0;
  while (mysql_more_results(mysql) && count < array_elements(rows))
unknown's avatar
unknown committed
4732
  {
4733
    fprintf(stdout,"\n Query %d: ", count);
unknown's avatar
unknown committed
4734
    if ((rc= mysql_next_result(mysql)))
4735 4736 4737 4738
    {
      exp_value= mysql_errno(mysql);
      fprintf(stdout, "ERROR %d: %s", exp_value, mysql_error(mysql));
    }
unknown's avatar
unknown committed
4739
    else
4740 4741 4742 4743
    {
      if ((result= mysql_store_result(mysql)))
        my_process_result_set(result);
      else
4744 4745
        fprintf(stdout,"OK, %lld row(s) affected, %d warning(s)",   
                        mysql_affected_rows(mysql),
4746 4747 4748 4749
                        mysql_warning_count(mysql));
      exp_value= (uint) mysql_affected_rows(mysql);
    }
    myassert(rows[count++] == exp_value);    
unknown's avatar
unknown committed
4750 4751 4752 4753
  }
  mysql= org_mysql;
}

unknown's avatar
unknown committed
4754

unknown's avatar
unknown committed
4755 4756 4757 4758 4759 4760 4761
/********************************************************
* to test simple bind store result                      *
*********************************************************/
static void test_store_result()
{
  MYSQL_STMT *stmt;
  int        rc;
unknown's avatar
unknown committed
4762
  long        nData;
unknown's avatar
unknown committed
4763 4764
  char       szData[100];
  MYSQL_BIND bind[2];
unknown's avatar
unknown committed
4765 4766
  ulong	     length, length1;
  my_bool    is_null[2];
unknown's avatar
unknown committed
4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792

  myheader("test_store_result");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_store_result");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_store_result(col1 int ,col2 varchar(50))");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"INSERT INTO test_store_result VALUES(10,'venu'),(20,'mysql')");
  myquery(rc);

  rc = mysql_query(mysql,"INSERT INTO test_store_result(col2) VALUES('monty')");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  /* fetch */
  bind[0].buffer_type=FIELD_TYPE_LONG;
unknown's avatar
unknown committed
4793 4794
  bind[0].buffer= (char*) &nData;	/* integer data */
  bind[0].length= &length;
4795
  bind[0].is_null= &is_null[0];
unknown's avatar
unknown committed
4796 4797

  length= 0; 
unknown's avatar
unknown committed
4798 4799 4800
  bind[1].buffer_type=FIELD_TYPE_STRING;
  bind[1].buffer=szData;		/* string data */
  bind[1].buffer_length=sizeof(szData);
unknown's avatar
unknown committed
4801 4802 4803
  bind[1].length= &length1;
  bind[1].is_null= &is_null[1];
  length1= 0;
unknown's avatar
unknown committed
4804

4805
  stmt = mysql_prepare(mysql, "SELECT * FROM test_store_result", 50);
unknown's avatar
unknown committed
4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819
  mystmt_init(stmt);

  rc = mysql_bind_result(stmt,bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  rc = mysql_stmt_store_result(stmt);
  mystmt(stmt, rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

unknown's avatar
unknown committed
4820
  fprintf(stdout,"\n row 1: %ld,%s(%lu)", nData, szData, length1);
unknown's avatar
unknown committed
4821 4822 4823 4824 4825 4826 4827
  myassert(nData == 10);
  myassert(strcmp(szData,"venu")==0);
  myassert(length1 == 4);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

unknown's avatar
unknown committed
4828
  fprintf(stdout,"\n row 2: %ld,%s(%lu)",nData, szData, length1);
unknown's avatar
unknown committed
4829 4830 4831 4832 4833 4834 4835 4836
  myassert(nData == 20);
  myassert(strcmp(szData,"mysql")==0);
  myassert(length1 == 5);

  length=99;
  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);
 
4837
  if (is_null[0])
unknown's avatar
unknown committed
4838
    fprintf(stdout,"\n row 3: NULL,%s(%lu)", szData, length1);
4839
  myassert(is_null[0]);
unknown's avatar
unknown committed
4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854
  myassert(strcmp(szData,"monty")==0);
  myassert(length1 == 5);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);
  
  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  rc = mysql_stmt_store_result(stmt);
  mystmt(stmt, rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

unknown's avatar
unknown committed
4855
  fprintf(stdout,"\n row 1: %ld,%s(%lu)",nData, szData, length1);
unknown's avatar
unknown committed
4856 4857 4858 4859 4860 4861 4862
  myassert(nData == 10);
  myassert(strcmp(szData,"venu")==0);
  myassert(length1 == 4);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

unknown's avatar
unknown committed
4863
  fprintf(stdout,"\n row 2: %ld,%s(%lu)",nData, szData, length1);
unknown's avatar
unknown committed
4864 4865 4866 4867 4868 4869 4870 4871
  myassert(nData == 20);
  myassert(strcmp(szData,"mysql")==0);
  myassert(length1 == 5);

  length=99;
  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);
 
4872
  if (is_null[0])
unknown's avatar
unknown committed
4873
    fprintf(stdout,"\n row 3: NULL,%s(%lu)", szData, length1);
4874
  myassert(is_null[0]);
unknown's avatar
unknown committed
4875 4876 4877 4878 4879 4880 4881 4882 4883
  myassert(strcmp(szData,"monty")==0);
  myassert(length1 == 5);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  mysql_stmt_close(stmt);
}

unknown's avatar
unknown committed
4884

unknown's avatar
unknown committed
4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945
/********************************************************
* to test simple bind store result                      *
*********************************************************/
static void test_store_result1()
{
  MYSQL_STMT *stmt;
  int        rc;

  myheader("test_store_result1");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_store_result");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_store_result(col1 int ,col2 varchar(50))");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"INSERT INTO test_store_result VALUES(10,'venu'),(20,'mysql')");
  myquery(rc);

  rc = mysql_query(mysql,"INSERT INTO test_store_result(col2) VALUES('monty')");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  stmt = mysql_prepare(mysql,"SELECT * FROM test_store_result",100);
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  rc = mysql_stmt_store_result(stmt);
  mystmt(stmt, rc);

  rc = 0;
  while (mysql_fetch(stmt) != MYSQL_NO_DATA)
    rc++;
  fprintf(stdout, "\n total rows: %d", rc);
  myassert(rc == 3);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  rc = mysql_stmt_store_result(stmt);
  mystmt(stmt, rc);

  rc = 0;
  while (mysql_fetch(stmt) != MYSQL_NO_DATA)
    rc++;
  fprintf(stdout, "\n total rows: %d", rc);
  myassert(rc == 3);

  mysql_stmt_close(stmt);
}

unknown's avatar
unknown committed
4946

unknown's avatar
unknown committed
4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981
/********************************************************
* to test simple bind store result                      *
*********************************************************/
static void test_store_result2()
{
  MYSQL_STMT *stmt;
  int        rc;
  int        nData;
  long       length;
  MYSQL_BIND bind[1];

  myheader("test_store_result2");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_store_result");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_store_result(col1 int ,col2 varchar(50))");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"INSERT INTO test_store_result VALUES(10,'venu'),(20,'mysql')");
  myquery(rc);

  rc = mysql_query(mysql,"INSERT INTO test_store_result(col2) VALUES('monty')");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  bind[0].buffer_type=FIELD_TYPE_LONG;
unknown's avatar
unknown committed
4982
  bind[0].buffer= (char *) &nData;	/* integer data */
unknown's avatar
unknown committed
4983
  bind[0].length= &length;
unknown's avatar
unknown committed
4984
  bind[0].is_null= 0;
unknown's avatar
unknown committed
4985

unknown's avatar
unknown committed
4986
  strmov((char *)query , "SELECT col1 FROM test_store_result where col1= ?");
unknown's avatar
unknown committed
4987 4988 4989 4990 4991 4992 4993 4994
  stmt = mysql_prepare(mysql, query, strlen(query));
  mystmt_init(stmt);

  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  rc = mysql_bind_result(stmt,bind);
  mystmt(stmt, rc);
4995
  
unknown's avatar
unknown committed
4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031
  nData = 10; length= 0;
  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  nData = 0;
  rc = mysql_stmt_store_result(stmt);
  mystmt(stmt, rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  fprintf(stdout,"\n row 1: %d",nData);
  myassert(nData == 10);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  nData = 20;
  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  nData = 0;
  rc = mysql_stmt_store_result(stmt);
  mystmt(stmt, rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  fprintf(stdout,"\n row 1: %d",nData);
  myassert(nData == 20);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);
  mysql_stmt_close(stmt);
}

unknown's avatar
unknown committed
5032

unknown's avatar
unknown committed
5033 5034 5035 5036 5037 5038
/********************************************************
* to test simple subselect prepare                      *
*********************************************************/
static void test_subselect()
{
#if TO_BE_FIXED_IN_SERVER
unknown's avatar
unknown committed
5039

unknown's avatar
unknown committed
5040
  MYSQL_STMT *stmt;
unknown's avatar
unknown committed
5041
  int        rc, id;
unknown's avatar
unknown committed
5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073
  MYSQL_BIND bind[1];

  myheader("test_subselect");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_sub1");
  myquery(rc);

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_sub2");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_sub1(id int)");
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_sub2(id int, id1 int)");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"INSERT INTO test_sub1 values(2)");
  myquery(rc);

  rc = mysql_query(mysql,"INSERT INTO test_sub2 VALUES(1,7),(2,7)");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  /* fetch */
5074
  bind[0].buffer_type= FIELD_TYPE_LONG;
unknown's avatar
unknown committed
5075
  bind[0].buffer= (char *) &id;	
5076
  bind[0].length= 0;
5077
  bind[0].is_null= 0;
5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102

  stmt = mysql_prepare(mysql, "INSERT INTO test_sub2(id) SELECT * FROM test_sub1 WHERE id=?", 100);
  mystmt_init(stmt);

  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  rc = mysql_bind_result(stmt,bind);
  mystmt(stmt, rc);

  id = 2; 
  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  verify_st_affected_rows(stmt, 1);

  id = 9; 
  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  verify_st_affected_rows(stmt, 0);

  mysql_stmt_close(stmt);

  myassert(3 == my_stmt_result("SELECT * FROM test_sub2",50));
unknown's avatar
unknown committed
5103

unknown's avatar
unknown committed
5104
  strmov((char *)query , "SELECT ROW(1,7) IN (select id, id1 from test_sub2 WHERE id1=?)");
unknown's avatar
unknown committed
5105 5106
  myassert(1 == my_stmt_result("SELECT ROW(1,7) IN (select id, id1 from test_sub2 WHERE id1=8)",100));
  myassert(1 == my_stmt_result("SELECT ROW(1,7) IN (select id, id1 from test_sub2 WHERE id1=7)",100));
5107 5108

  stmt = mysql_prepare(mysql, query, 150);
unknown's avatar
unknown committed
5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123
  mystmt_init(stmt);

  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  rc = mysql_bind_result(stmt,bind);
  mystmt(stmt, rc);

  id = 7; 
  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

5124
  fprintf(stdout,"\n row 1: %d",id);
unknown's avatar
unknown committed
5125 5126
  myassert(id == 1);

5127 5128 5129 5130 5131 5132 5133 5134 5135 5136
  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);
  
  id= 8;
  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

5137
  fprintf(stdout,"\n row 1: %d",id);
5138 5139
  myassert(id == 0);

unknown's avatar
unknown committed
5140 5141 5142 5143 5144 5145 5146
  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  mysql_stmt_close(stmt);
#endif
}

5147 5148 5149 5150 5151 5152
/*
  Generalized conversion routine to handle DATE, TIME and DATETIME
  conversion using MYSQL_TIME structure
*/
static void test_bind_date_conv(uint row_count)
{ 
5153
  MYSQL_STMT   *stmt= 0;
5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238
  uint         rc, i, count= row_count;
  ulong	       length[4];
  MYSQL_BIND   bind[4];
  my_bool      is_null[4]={0};
  MYSQL_TIME   tm[4]; 
  ulong        second_part;
  uint         year, month, day, hour, minute, sec;

  stmt = mysql_prepare(mysql,"INSERT INTO test_date VALUES(?,?,?,?)", 100);
  mystmt_init(stmt);

  verify_param_count(stmt, 4);  

  bind[0].buffer_type= MYSQL_TYPE_TIMESTAMP;
  bind[1].buffer_type= MYSQL_TYPE_TIME;
  bind[2].buffer_type= MYSQL_TYPE_DATETIME;
  bind[3].buffer_type= MYSQL_TYPE_DATE;
 
  second_part= 0;

  year=2000;
  month=01;
  day=10;

  hour=11;
  minute=16;
  sec= 20;

  for (i= 0; i < (int) array_elements(bind); i++)
  {  
    bind[i].buffer= (char *) &tm[i];
    bind[i].is_null= &is_null[i];
    bind[i].length= &length[i];
    bind[i].buffer_length= 30;
    length[i]=20;
  }   

  rc = mysql_bind_param(stmt, bind);
  mystmt(stmt,rc);
 
  for (count= 0; count < row_count; count++)
  { 
    for (i= 0; i < (int) array_elements(bind); i++)
    {
      tm[i].neg= 0;   
      tm[i].second_part= second_part+count;
      tm[i].year= year+count;
      tm[i].month= month+count;
      tm[i].day= day+count;
      tm[i].hour= hour+count;
      tm[i].minute= minute+count;
      tm[i].second= sec+count;
    }   
    rc = mysql_execute(stmt);
    mystmt(stmt, rc);    
  }

  rc = mysql_commit(mysql);
  myquery(rc);

  mysql_stmt_close(stmt);

  myassert(row_count == my_stmt_result("SELECT * FROM test_date",50));

  stmt = mysql_prepare(mysql,"SELECT * FROM test_date",50);
  myquery(rc);

  rc = mysql_bind_result(stmt, bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  rc = mysql_stmt_store_result(stmt);
  mystmt(stmt, rc);

  for (count=0; count < row_count; count++)
  {
    rc = mysql_fetch(stmt);
    mystmt(stmt,rc);

    fprintf(stdout, "\n");
    for (i= 0; i < array_elements(bind); i++)
    {  
      fprintf(stdout, "\n");
5239
      fprintf(stdout," time[%d]: %02d-%02d-%02d %02d:%02d:%02d.%02lu",
5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394
                      i, tm[i].year, tm[i].month, tm[i].day, 
                      tm[i].hour, tm[i].minute, tm[i].second,
                      tm[i].second_part);                      

      myassert(tm[i].year == 0 || tm[i].year == year+count);
      myassert(tm[i].month == 0 || tm[i].month == month+count);
      myassert(tm[i].day == 0 || tm[i].day == day+count);

      myassert(tm[i].hour == 0 || tm[i].hour == hour+count);
      /* 
         minute causes problems from date<->time, don't assert, instead
         validate separatly in another routine
       */
      /*myassert(tm[i].minute == 0 || tm[i].minute == minute+count);
      myassert(tm[i].second == 0 || tm[i].second == sec+count);*/

      myassert(tm[i].second_part == 0 || tm[i].second_part == second_part+count);
    }
  }
  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  mysql_stmt_close(stmt);
}

/*
  Test DATE, TIME, DATETIME and TS with MYSQL_TIME conversion
*/

static void test_date()
{
  int        rc;

  myheader("test_date");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_date");
  myquery(rc);
  
  rc= mysql_query(mysql,"CREATE TABLE test_date(c1 TIMESTAMP(14), \
                                                 c2 TIME,\
                                                 c3 DATETIME,\
                                                 c4 DATE)");

  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  test_bind_date_conv(5);
}

/*
  Test all time types to DATE and DATE to all types
*/

static void test_date_date()
{
  int        rc;

  myheader("test_date_date");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_date");
  myquery(rc);
  
  rc= mysql_query(mysql,"CREATE TABLE test_date(c1 DATE, \
                                                 c2 DATE,\
                                                 c3 DATE,\
                                                 c4 DATE)");

  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  test_bind_date_conv(3);
}

/*
  Test all time types to TIME and TIME to all types
*/

static void test_date_time()
{
  int        rc;

  myheader("test_date_time");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_date");
  myquery(rc);
  
  rc= mysql_query(mysql,"CREATE TABLE test_date(c1 TIME, \
                                                 c2 TIME,\
                                                 c3 TIME,\
                                                 c4 TIME)");

  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  test_bind_date_conv(3);
}

/*
  Test all time types to TIMESTAMP and TIMESTAMP to all types
*/

static void test_date_ts()
{
  int        rc;

  myheader("test_date_ts");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_date");
  myquery(rc);
  
  rc= mysql_query(mysql,"CREATE TABLE test_date(c1 TIMESTAMP(10), \
                                                 c2 TIMESTAMP(14),\
                                                 c3 TIMESTAMP,\
                                                 c4 TIMESTAMP(6))");

  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  test_bind_date_conv(2);
}

/*
  Test all time types to DATETIME and DATETIME to all types
*/

static void test_date_dt()
{
  int        rc;

  myheader("test_date_dt");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_date");
  myquery(rc);
  
  rc= mysql_query(mysql,"CREATE TABLE test_date(c1 datetime, \
                                                 c2 datetime,\
                                                 c3 datetime,\
                                                 c4 date)");

  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  test_bind_date_conv(2);
}

5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415
/*
  Misc tests to keep pure coverage happy
*/
static void test_pure_coverage()
{
  MYSQL_STMT *stmt;
  MYSQL_BIND bind[1];
  int        rc;
  ulong      length;
  
  myheader("test_pure_coverage");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_pure");
  myquery(rc);
  
  rc = mysql_query(mysql,"CREATE TABLE test_pure(c1 int, c2 varchar(20))");
  myquery(rc);

  stmt = mysql_prepare(mysql,"insert into test_pure(c67788) values(10)",100);
  mystmt_init_r(stmt);
  
unknown's avatar
unknown committed
5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428
#ifndef DBUG_OFF
  stmt = mysql_prepare(mysql,(const char *)0,0);
  mystmt_init_r(stmt);
  
  stmt = mysql_prepare(mysql,"insert into test_pure(c2) values(10)",100);
  mystmt_init(stmt);

  rc = mysql_bind_param(stmt, bind);
  mystmt_r(stmt, rc);

  mysql_stmt_close(stmt);
#endif

5429 5430 5431
  stmt = mysql_prepare(mysql,"insert into test_pure(c2) values(?)",100);
  mystmt_init(stmt);

unknown's avatar
unknown committed
5432
#ifndef DBUG_OFF
5433 5434
  rc = mysql_execute(stmt);
  mystmt_r(stmt, rc);/* No parameters supplied */
unknown's avatar
unknown committed
5435
#endif
5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462

  bind[0].length= &length;
  bind[0].is_null= 0;
  bind[0].buffer_length= 0;

  bind[0].buffer_type= MYSQL_TYPE_GEOMETRY;
  rc = mysql_bind_param(stmt, bind);
  mystmt_r(stmt, rc); /* unsupported buffer type */
  
  bind[0].buffer_type= MYSQL_TYPE_STRING;
  rc = mysql_bind_param(stmt, bind);
  mystmt(stmt, rc);

  rc = mysql_send_long_data(stmt, 20, (char *)"venu", 4);
  mystmt_r(stmt, rc); /* wrong param number */

  rc = mysql_stmt_store_result(stmt);
  mystmt(stmt, rc); 

  mysql_stmt_close(stmt);

  stmt = mysql_prepare(mysql,"select * from test_pure",100);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

unknown's avatar
unknown committed
5463 5464 5465
#ifndef DBUG_OFF
  rc = mysql_bind_result(stmt, (MYSQL_BIND *)0);
  mystmt_r(stmt, rc);
5466 5467 5468 5469
  
  bind[0].buffer_type= MYSQL_TYPE_GEOMETRY;
  rc = mysql_bind_result(stmt, bind);
  mystmt_r(stmt, rc); /* unsupported buffer type */
unknown's avatar
unknown committed
5470 5471
#endif

5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566
  rc = mysql_stmt_store_result(stmt);
  mystmt(stmt, rc);
  
  rc = mysql_stmt_store_result(stmt);
  mystmt_r(stmt, rc); /* commands out of sync */

  mysql_stmt_close(stmt);

  mysql_query(mysql,"DROP TABLE test_pure");
  mysql_commit(mysql);
}

/*
  test for string buffer fetch
*/

static void test_buffers()
{
  MYSQL_STMT *stmt;
  MYSQL_BIND bind[1];
  int        rc;
  ulong      length;
  my_bool    is_null;
  char       buffer[20];
  
  myheader("test_buffers");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_buffer");
  myquery(rc);
  
  rc = mysql_query(mysql,"CREATE TABLE test_buffer(str varchar(20))");
  myquery(rc);

  rc = mysql_query(mysql,"insert into test_buffer values('MySQL')\
                          ,('Database'),('Open-Source'),('Popular')");
  myquery(rc);
  
  stmt = mysql_prepare(mysql,"select str from test_buffer",100);
  mystmt_init(stmt);
  
  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  bind[0].length= &length;
  bind[0].is_null= &is_null;
  bind[0].buffer_length= 1;
  bind[0].buffer_type= MYSQL_TYPE_STRING;
  bind[0].buffer= (char *)buffer;
  
  rc = mysql_bind_result(stmt, bind);
  mystmt(stmt, rc);

  rc = mysql_stmt_store_result(stmt);
  mystmt(stmt, rc);

  buffer[1]='X';
  rc = mysql_fetch(stmt);
  mystmt(stmt, rc);
  fprintf(stdout, "\n data: %s (%lu)", buffer, length);
  myassert(buffer[0] == 'M');
  myassert(buffer[1] == 'X');
  myassert(length == 5);

  bind[0].buffer_length=8;
  rc = mysql_bind_result(stmt, bind);/* re-bind */
  mystmt(stmt, rc);
  
  rc = mysql_fetch(stmt);
  mystmt(stmt, rc);
  fprintf(stdout, "\n data: %s (%lu)", buffer, length);
  myassert(strncmp(buffer,"Database",8) == 0);
  myassert(length == 8);
  
  bind[0].buffer_length=12;
  rc = mysql_bind_result(stmt, bind);/* re-bind */
  mystmt(stmt, rc);
  
  rc = mysql_fetch(stmt);
  mystmt(stmt, rc);
  fprintf(stdout, "\n data: %s (%lu)", buffer, length);
  myassert(strcmp(buffer,"Open-Source") == 0);
  myassert(length == 11);
  
  bind[0].buffer_length=6;
  rc = mysql_bind_result(stmt, bind);/* re-bind */
  mystmt(stmt, rc);
  
  rc = mysql_fetch(stmt);
  mystmt(stmt, rc);
  fprintf(stdout, "\n data: %s (%lu)", buffer, length);
  myassert(strncmp(buffer,"Popula",6) == 0);
  myassert(length == 7);
  
  mysql_stmt_close(stmt);
}
unknown's avatar
unknown committed
5567

5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598
/* 
 Test the direct query execution in the middle of open stmts 
*/
static void test_open_direct()
{
  MYSQL_STMT  *stmt;
  MYSQL_RES   *result;
  int         rc;
  
  myheader("test_open_direct");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_open_direct");
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_open_direct(id int, name char(6))");
  myquery(rc);

  stmt = mysql_prepare(mysql,"INSERT INTO test_open_direct values(10,'mysql')", 100);
  mystmt_init(stmt);

  rc = mysql_query(mysql, "SELECT * FROM test_open_direct");
  myquery(rc);

  result = mysql_store_result(mysql);
  mytest(result);

  myassert(0 == my_process_result_set(result));

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

5599
  verify_st_affected_rows(stmt, 1);
5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611
  
  rc = mysql_query(mysql, "SELECT * FROM test_open_direct");
  myquery(rc);

  result = mysql_store_result(mysql);
  mytest(result);

  myassert(1 == my_process_result_set(result));

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

5612
  verify_st_affected_rows(stmt, 1);
5613 5614 5615 5616 5617 5618 5619 5620 5621
  
  rc = mysql_query(mysql, "SELECT * FROM test_open_direct");
  myquery(rc);

  result = mysql_store_result(mysql);
  mytest(result);

  myassert(2 == my_process_result_set(result));
  mysql_stmt_close(stmt);
5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659

  /* run a direct query in the middle of a fetch */
  stmt= mysql_prepare(mysql,"SELECT * FROM test_open_direct",100);
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt, rc);

  rc = mysql_query(mysql,"INSERT INTO test_open_direct(id) VALUES(20)");
  myquery_r(rc);

  rc = mysql_stmt_close(stmt);
  mystmt(stmt, rc);

  rc = mysql_query(mysql,"INSERT INTO test_open_direct(id) VALUES(20)");
  myquery(rc);

  /* run a direct query with store result */
  stmt= mysql_prepare(mysql,"SELECT * FROM test_open_direct",100);
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  rc = mysql_stmt_store_result(stmt);
  mystmt(stmt, rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt, rc);

  rc = mysql_query(mysql,"drop table test_open_direct");
  myquery(rc);

  rc = mysql_stmt_close(stmt);
  mystmt(stmt, rc);
5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673
}

/*
  To test fetch without prior bound buffers
*/
static void test_fetch_nobuffs()
{
  MYSQL_STMT *stmt;
  MYSQL_BIND bind[4];
  char       str[4][50];
  int        rc;

  myheader("test_fetch_nobuffs");

5674 5675
  stmt = mysql_prepare(mysql,"SELECT DATABASE(), CURRENT_USER(), \
                              CURRENT_DATE(), CURRENT_TIME()",100);
5676 5677 5678 5679 5680 5681 5682 5683
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  rc = 0;
  while (mysql_fetch(stmt) != MYSQL_NO_DATA)
    rc++;
5684 5685

  fprintf(stdout, "\n total rows        : %d", rc);
5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707
  myassert(rc == 1);

  bind[0].buffer_type= MYSQL_TYPE_STRING;
  bind[0].buffer= (char *)str[0];
  bind[0].is_null= 0;
  bind[0].length= 0;
  bind[0].buffer_length= sizeof(str[0]);
  bind[1]= bind[2]= bind[3]= bind[0];
  bind[1].buffer= (char *)str[1];
  bind[2].buffer= (char *)str[2];
  bind[3].buffer= (char *)str[3];

  rc = mysql_bind_result(stmt, bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  rc = 0;
  while (mysql_fetch(stmt) != MYSQL_NO_DATA)
  {
    rc++;
5708 5709 5710 5711
    fprintf(stdout, "\n CURRENT_DATABASE(): %s", str[0]);
    fprintf(stdout, "\n CURRENT_USER()    : %s", str[1]);
    fprintf(stdout, "\n CURRENT_DATE()    : %s", str[2]);
    fprintf(stdout, "\n CURRENT_TIME()    : %s", str[3]);
5712
  }
5713
  fprintf(stdout, "\n total rows        : %d", rc);
5714 5715 5716 5717 5718
  myassert(rc == 1);

  mysql_stmt_close(stmt);
}

5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779
/*
  To test a misc bug 
*/
static void test_ushort_bug()
{
  MYSQL_STMT *stmt;
  MYSQL_BIND bind[4];
  ushort     short_value;
  ulong      long_value;
  ulong      s_length, l_length, ll_length, t_length;
  ulonglong  longlong_value;
  int        rc;
  uchar      tiny_value;

  myheader("test_ushort_bug");

  rc= mysql_query(mysql,"DROP TABLE IF EXISTS test_ushort");
  myquery(rc);
  
  rc= mysql_query(mysql,"CREATE TABLE test_ushort(a smallint unsigned, \
                                                  b smallint unsigned, \
                                                  c smallint unsigned, \
                                                  d smallint unsigned)");
  myquery(rc);
  
  rc= mysql_query(mysql,"INSERT INTO test_ushort VALUES(35999, 35999, 35999, 200)");
  myquery(rc);
  
  
  stmt = mysql_prepare(mysql,"SELECT * FROM test_ushort",50);
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  bind[0].buffer_type= MYSQL_TYPE_SHORT;
  bind[0].buffer= (char *)&short_value;
  bind[0].is_null= 0;
  bind[0].length= &s_length;
  
  bind[1].buffer_type= MYSQL_TYPE_LONG;
  bind[1].buffer= (char *)&long_value;
  bind[1].is_null= 0;
  bind[1].length= &l_length;

  bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
  bind[2].buffer= (char *)&longlong_value;
  bind[2].is_null= 0;
  bind[2].length= &ll_length;
  
  bind[3].buffer_type= MYSQL_TYPE_TINY;
  bind[3].buffer= (char *)&tiny_value;
  bind[3].is_null= 0;
  bind[3].length= &t_length;
  
  rc = mysql_bind_result(stmt, bind);
  mystmt(stmt, rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt, rc);
  
5780 5781 5782 5783
  fprintf(stdout,"\n ushort   : %d (%ld)", short_value, s_length);
  fprintf(stdout,"\n ulong    : %ld (%ld)", long_value, l_length);
  fprintf(stdout,"\n longlong : %lld (%ld)", longlong_value, ll_length);
  fprintf(stdout,"\n tinyint  : %d   (%ld)", tiny_value, t_length);
5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802
  
  myassert(short_value == 35999);
  myassert(s_length == 2);
  
  myassert(long_value == 35999);
  myassert(l_length == 4);

  myassert(longlong_value == 35999);
  myassert(ll_length == 8);

  myassert(tiny_value == 200);
  myassert(t_length == 1);
  
  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  mysql_stmt_close(stmt);
}

5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970
/*
  To test a misc smallint-signed conversion bug 
*/
static void test_sshort_bug()
{
  MYSQL_STMT *stmt;
  MYSQL_BIND bind[4];
  short      short_value;
  long       long_value;
  ulong      s_length, l_length, ll_length, t_length;
  ulonglong  longlong_value;
  int        rc;
  uchar      tiny_value;

  myheader("test_sshort_bug");

  rc= mysql_query(mysql,"DROP TABLE IF EXISTS test_sshort");
  myquery(rc);
  
  rc= mysql_query(mysql,"CREATE TABLE test_sshort(a smallint signed, \
                                                  b smallint signed, \
                                                  c smallint unsigned, \
                                                  d smallint unsigned)");
  myquery(rc);
  
  rc= mysql_query(mysql,"INSERT INTO test_sshort VALUES(-5999, -5999, 35999, 200)");
  myquery(rc);
  
  
  stmt = mysql_prepare(mysql,"SELECT * FROM test_sshort",50);
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  bind[0].buffer_type= MYSQL_TYPE_SHORT;
  bind[0].buffer= (char *)&short_value;
  bind[0].is_null= 0;
  bind[0].length= &s_length;
  
  bind[1].buffer_type= MYSQL_TYPE_LONG;
  bind[1].buffer= (char *)&long_value;
  bind[1].is_null= 0;
  bind[1].length= &l_length;

  bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
  bind[2].buffer= (char *)&longlong_value;
  bind[2].is_null= 0;
  bind[2].length= &ll_length;
  
  bind[3].buffer_type= MYSQL_TYPE_TINY;
  bind[3].buffer= (char *)&tiny_value;
  bind[3].is_null= 0;
  bind[3].length= &t_length;
  
  rc = mysql_bind_result(stmt, bind);
  mystmt(stmt, rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt, rc);
  
  fprintf(stdout,"\n sshort   : %d (%ld)", short_value, s_length);
  fprintf(stdout,"\n slong    : %ld (%ld)", long_value, l_length);
  fprintf(stdout,"\n longlong : %lld (%ld)", longlong_value, ll_length);
  fprintf(stdout,"\n tinyint  : %d   (%ld)", tiny_value, t_length);
  
  myassert(short_value == -5999);
  myassert(s_length == 2);
  
  myassert(long_value == -5999);
  myassert(l_length == 4);

  myassert(longlong_value == 35999);
  myassert(ll_length == 8);

  myassert(tiny_value == 200);
  myassert(t_length == 1);
  
  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  mysql_stmt_close(stmt);
}

/*
  To test a misc tinyint-signed conversion bug 
*/
static void test_stiny_bug()
{
  MYSQL_STMT *stmt;
  MYSQL_BIND bind[4];
  short      short_value;
  long       long_value;
  ulong      s_length, l_length, ll_length, t_length;
  ulonglong  longlong_value;
  int        rc;
  uchar      tiny_value;

  myheader("test_stiny_bug");

  rc= mysql_query(mysql,"DROP TABLE IF EXISTS test_stiny");
  myquery(rc);
  
  rc= mysql_query(mysql,"CREATE TABLE test_stiny(a tinyint signed, \
                                                  b tinyint signed, \
                                                  c tinyint unsigned, \
                                                  d tinyint unsigned)");
  myquery(rc);
  
  rc= mysql_query(mysql,"INSERT INTO test_stiny VALUES(-128, -127, 255, 0)");
  myquery(rc);
  
  
  stmt = mysql_prepare(mysql,"SELECT * FROM test_stiny",50);
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  bind[0].buffer_type= MYSQL_TYPE_SHORT;
  bind[0].buffer= (char *)&short_value;
  bind[0].is_null= 0;
  bind[0].length= &s_length;
  
  bind[1].buffer_type= MYSQL_TYPE_LONG;
  bind[1].buffer= (char *)&long_value;
  bind[1].is_null= 0;
  bind[1].length= &l_length;

  bind[2].buffer_type= MYSQL_TYPE_LONGLONG;
  bind[2].buffer= (char *)&longlong_value;
  bind[2].is_null= 0;
  bind[2].length= &ll_length;
  
  bind[3].buffer_type= MYSQL_TYPE_TINY;
  bind[3].buffer= (char *)&tiny_value;
  bind[3].is_null= 0;
  bind[3].length= &t_length;
  
  rc = mysql_bind_result(stmt, bind);
  mystmt(stmt, rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt, rc);
  
  fprintf(stdout,"\n sshort   : %d (%ld)", short_value, s_length);
  fprintf(stdout,"\n slong    : %ld (%ld)", long_value, l_length);
  fprintf(stdout,"\n longlong : %lld  (%ld)", longlong_value, ll_length);
  fprintf(stdout,"\n tinyint  : %d    (%ld)", tiny_value, t_length);
  
  myassert(short_value == -128);
  myassert(s_length == 2);
  
  myassert(long_value == -127);
  myassert(l_length == 4);

  myassert(longlong_value == 255);
  myassert(ll_length == 8);

  myassert(tiny_value == 0);
  myassert(t_length == 1);
  
  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  mysql_stmt_close(stmt);
}

5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996
/********************************************************
* to test misc field information, bug: #74              *
*********************************************************/
static void test_field_misc()
{
  MYSQL_STMT  *stmt;
  MYSQL_RES   *result;
  MYSQL_BIND  bind[1];
  char        table_type[NAME_LEN];
  ulong       type_length;
  int         rc;

  myheader("test_field_misc");

  rc = mysql_query(mysql,"SELECT @@autocommit");
  myquery(rc);

  result = mysql_store_result(mysql);
  mytest(result);

  myassert(1 == my_process_result_set(result));
  
  verify_prepare_field(result,0,
                       "@@autocommit","",   /* field and its org name */
                       MYSQL_TYPE_LONGLONG, /* field type */
                       "", "",              /* table and its org name */
5997
                       "",1,0);             /* db name, length(its bool flag)*/
5998 5999 6000 6001 6002 6003 6004 6005 6006

  mysql_free_result(result);
  
  stmt = mysql_prepare(mysql,"SELECT @@autocommit",20);
  mystmt_init(stmt);
  
  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

unknown's avatar
unknown committed
6007
  result = mysql_get_metadata(stmt);
6008 6009 6010 6011 6012 6013 6014 6015
  mytest(result);
  
  myassert(1 == my_process_stmt_result(stmt));
  
  verify_prepare_field(result,0,
                       "@@autocommit","",   /* field and its org name */
                       MYSQL_TYPE_LONGLONG, /* field type */
                       "", "",              /* table and its org name */
6016
                       "",1,0);             /* db name, length(its bool flag)*/
6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047

  mysql_free_result(result);
  mysql_stmt_close(stmt);

  stmt = mysql_prepare(mysql, "SELECT @@table_type", 30);
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  bind[0].buffer_type= MYSQL_TYPE_STRING;
  bind[0].buffer= table_type;
  bind[0].length= &type_length;
  bind[0].is_null= 0;
  bind[0].buffer_length= NAME_LEN;

  rc = mysql_bind_result(stmt, bind);
  mystmt(stmt,rc);    

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);
  fprintf(stdout,"\n default table type: %s(%ld)", table_type, type_length);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  mysql_stmt_close(stmt);

  stmt = mysql_prepare(mysql, "SELECT @@table_type", 30);
  mystmt_init(stmt);

unknown's avatar
unknown committed
6048
  result = mysql_get_metadata(stmt);
6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059
  mytest(result);

  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  myassert(1 == my_process_stmt_result(stmt));
  
  verify_prepare_field(result,0,
                       "@@table_type","",   /* field and its org name */
                       MYSQL_TYPE_STRING,   /* field type */
                       "", "",              /* table and its org name */
6060
                       "",type_length*3,0);   /* db name, length */
6061 6062 6063 6064 6065 6066 6067

  mysql_free_result(result);
  mysql_stmt_close(stmt);

  stmt = mysql_prepare(mysql, "SELECT @@max_error_count", 30);
  mystmt_init(stmt);

unknown's avatar
unknown committed
6068
  result = mysql_get_metadata(stmt);
6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079
  mytest(result);

  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  myassert(1 == my_process_stmt_result(stmt));
  
  verify_prepare_field(result,0,
                       "@@max_error_count","",   /* field and its org name */
                       MYSQL_TYPE_LONGLONG, /* field type */
                       "", "",              /* table and its org name */
6080
                       "",10,0);            /* db name, length */
6081 6082 6083 6084 6085 6086 6087

  mysql_free_result(result);
  mysql_stmt_close(stmt);
  
  stmt = mysql_prepare(mysql, "SELECT @@max_allowed_packet", 30);
  mystmt_init(stmt);

unknown's avatar
unknown committed
6088
  result = mysql_get_metadata(stmt);
6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099
  mytest(result);

  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  myassert(1 == my_process_stmt_result(stmt));
  
  verify_prepare_field(result,0,
                       "@@max_allowed_packet","",   /* field and its org name */
                       MYSQL_TYPE_LONGLONG, /* field type */
                       "", "",              /* table and its org name */
6100
                       "",10,0);            /* db name, length */
6101 6102 6103

  mysql_free_result(result);
  mysql_stmt_close(stmt);
6104 6105 6106 6107
  
  stmt = mysql_prepare(mysql, "SELECT @@sql_warnings", 30);
  mystmt_init(stmt);
  
unknown's avatar
unknown committed
6108
  result = mysql_get_metadata(stmt);
6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119
  mytest(result);

  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  myassert(1 == my_process_stmt_result(stmt));
  
  verify_prepare_field(result,0,
                       "@@sql_warnings","",   /* field and its org name */
                       MYSQL_TYPE_LONGLONG,   /* field type */
                       "", "",                /* table and its org name */
6120
                       "",1,0);               /* db name, length */
6121 6122 6123

  mysql_free_result(result);
  mysql_stmt_close(stmt);
6124 6125
}

6126 6127 6128

/*
  To test SET OPTION feature with prepare stmts
6129
  bug #85 (reported by mark@mysql.com)
6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193
*/
static void test_set_option()
{
  MYSQL_STMT *stmt;
  MYSQL_RES  *result;
  int        rc;

  myheader("test_set_option");

  mysql_autocommit(mysql, TRUE);

  /* LIMIT the rows count to 2 */
  rc= mysql_query(mysql,"SET OPTION SQL_SELECT_LIMIT=2");
  myquery(rc);

  rc= mysql_query(mysql,"DROP TABLE IF EXISTS test_limit");
  myquery(rc);
  
  rc= mysql_query(mysql,"CREATE TABLE test_limit(a tinyint)");
  myquery(rc);
  
  rc= mysql_query(mysql,"INSERT INTO test_limit VALUES(10),(20),(30),(40)");
  myquery(rc);  
  
  fprintf(stdout,"\n with SQL_SELECT_LIMIT=2 (direct)");
  rc = mysql_query(mysql,"SELECT * FROM test_limit");
  myquery(rc);

  result = mysql_store_result(mysql);
  mytest(result);

  myassert(2 == my_process_result_set(result));

  mysql_free_result(result);
  
  fprintf(stdout,"\n with SQL_SELECT_LIMIT=2 (prepare)");  
  stmt = mysql_prepare(mysql, "SELECT * FROM test_limit", 50);
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  myassert(2 == my_process_stmt_result(stmt));

  mysql_stmt_close(stmt);

  /* RESET the LIMIT the rows count to 0 */  
  fprintf(stdout,"\n with SQL_SELECT_LIMIT=DEFAULT (prepare)");
  rc= mysql_query(mysql,"SET OPTION SQL_SELECT_LIMIT=DEFAULT");
  myquery(rc);
  
  stmt = mysql_prepare(mysql, "SELECT * FROM test_limit", 50);
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  myassert(4 == my_process_stmt_result(stmt));

  mysql_stmt_close(stmt);
}

/*
  To test a misc GRANT option
6194
  bug #89 (reported by mark@mysql.com)
6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234
*/
static void test_prepare_grant()
{
  int rc;

  myheader("test_prepare_grant");

  mysql_autocommit(mysql, TRUE);

  rc= mysql_query(mysql,"DROP TABLE IF EXISTS test_grant");
  myquery(rc);
  
  rc= mysql_query(mysql,"CREATE TABLE test_grant(a tinyint primary key auto_increment)");
  myquery(rc);

  strxmov(query,"GRANT INSERT,UPDATE,SELECT ON ", current_db,
                ".test_grant TO 'test_grant'@",
                opt_host ? opt_host : "'localhost'", NullS);

  if (mysql_query(mysql,query))
  {
    myerror("GRANT failed");
    
    /* 
       If server started with --skip-grant-tables, skip this test, else
       exit to indicate an error

       ER_UNKNOWN_COM_ERROR = 1047
     */ 
    if (mysql_errno(mysql) != 1047)  
      exit(0);  
  }
  else
  {
    MYSQL *org_mysql= mysql, *lmysql;
    MYSQL_STMT *stmt;
    
    fprintf(stdout, "\n Establishing a test connection ...");
    if (!(lmysql = mysql_init(NULL)))
    { 
6235
      myerror("mysql_init() failed");
6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280
      exit(0);
    }
    if (!(mysql_real_connect(lmysql,opt_host,"test_grant",
			     "", current_db, opt_port,
			     opt_unix_socket, 0)))
    {
      myerror("connection failed");   
      mysql_close(lmysql);
      exit(0);
    }   
    fprintf(stdout," OK");

    mysql= lmysql;
    rc = mysql_query(mysql,"INSERT INTO test_grant VALUES(NULL)");
    myquery(rc);

    rc = mysql_query(mysql,"INSERT INTO test_grant(a) VALUES(NULL)");
    myquery(rc);
    
    execute_prepare_query("INSERT INTO test_grant(a) VALUES(NULL)",1);    
    execute_prepare_query("INSERT INTO test_grant VALUES(NULL)",1);
    execute_prepare_query("UPDATE test_grant SET a=9 WHERE a=1",1);
    myassert(4 == my_stmt_result("SELECT a FROM test_grant",50));
    
    rc = mysql_query(mysql,"DELETE FROM test_grant");
    myquery_r(rc);

    stmt= mysql_prepare(mysql,"DELETE FROM test_grant",50);
    mystmt_init(stmt);

    rc = mysql_execute(stmt);
    myquery_r(rc);
    
    myassert(4 == my_stmt_result("SELECT * FROM test_grant",50));
    
    mysql_close(lmysql);        
    mysql= org_mysql;

    rc = mysql_query(mysql,"delete from mysql.user where User='test_grant'");
    myquery(rc);
    myassert(1 == mysql_affected_rows(mysql));

    rc = mysql_query(mysql,"delete from mysql.tables_priv where User='test_grant'");
    myquery(rc);
    myassert(1 == mysql_affected_rows(mysql));
unknown's avatar
unknown committed
6281

6282 6283 6284 6285 6286 6287
  }
}


/*
  To test a crash when invalid/corrupted .frm is used in the 
6288 6289
  SHOW TABLE STATUS
  bug #93 (reported by serg@mysql.com).
6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370
*/
static void test_frm_bug()
{
  MYSQL_STMT *stmt;
  MYSQL_BIND bind[2];
  MYSQL_RES  *result;
  MYSQL_ROW  row;
  FILE       *test_file;
  char       data_dir[NAME_LEN];
  char       test_frm[255];
  int        rc;

  myheader("test_frm_bug");

  mysql_autocommit(mysql, TRUE);

  rc= mysql_query(mysql,"drop table if exists test_frm_bug");
  myquery(rc);

  rc= mysql_query(mysql,"flush tables");
  myquery(rc);
  
  stmt = mysql_prepare(mysql, "show variables like 'datadir'", 50);
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  bind[0].buffer_type= MYSQL_TYPE_STRING;
  bind[0].buffer= data_dir;
  bind[0].buffer_length= NAME_LEN;
  bind[0].is_null= 0;
  bind[0].length= 0;
  bind[1]=bind[0];

  rc = mysql_bind_result(stmt,bind);
  mystmt(stmt,rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  fprintf(stdout,"\n data directory: %s", data_dir);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  strxmov(test_frm,data_dir,"/",current_db,"/","test_frm_bug.frm",NullS);

  fprintf(stdout,"\n test_frm: %s", test_frm);

  if (!(test_file= my_fopen(test_frm, (int) (O_RDWR | O_CREAT), MYF(MY_WME))))
  {
    fprintf(stdout,"\n ERROR: my_fopen failed for '%s'", test_frm);
    fprintf(stdout,"\n test cancelled");
    return;  
  }
  fprintf(test_file,"this is a junk file for test");

  rc = mysql_query(mysql,"SHOW TABLE STATUS like 'test_frm_bug'");
  myquery(rc);

  result = mysql_store_result(mysql);
  mytest(result);/* It can't be NULL */

  myassert(1 == my_process_result_set(result));

  mysql_data_seek(result,0);

  row= mysql_fetch_row(result);
  mytest(row);

  fprintf(stdout,"\n Comment: %s", row[15]);
  myassert(row[15] != 0);

  mysql_free_result(result);
  mysql_stmt_close(stmt);

  my_fclose(test_file,MYF(0));
  mysql_query(mysql,"drop table if exists test_frm_bug");
}

6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498
/*
  To test DECIMAL conversion 
*/
static void test_decimal_bug()
{
  MYSQL_STMT *stmt;
  MYSQL_BIND bind[1];
  double     data;
  int        rc;
  my_bool    is_null;

  myheader("test_decimal_bug");

  mysql_autocommit(mysql, TRUE);

  rc= mysql_query(mysql,"drop table if exists test_decimal_bug");
  myquery(rc);
  
  rc = mysql_query(mysql, "create table test_decimal_bug(c1 decimal(10,2))");
  myquery(rc);
  
  rc = mysql_query(mysql, "insert into test_decimal_bug value(8),(10.22),(5.61)");
  myquery(rc);

  stmt = mysql_prepare(mysql,"select c1 from test_decimal_bug where c1= ?",50);
  mystmt_init(stmt);

  bind[0].buffer_type= MYSQL_TYPE_DOUBLE;
  bind[0].buffer= (char *)&data;
  bind[0].buffer_length= 0;
  bind[0].is_null= &is_null;
  bind[0].length= 0;

  is_null= 0; 
  rc = mysql_bind_param(stmt, bind);
  mystmt(stmt,rc);

  data= 8.0;
  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  data=0; 
  rc = mysql_bind_result(stmt, bind);
  mystmt(stmt,rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  fprintf(stdout, "\n data: %g", data);
  myassert(data == 8.0);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  data= 5.61;
  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  data=0;
  rc = mysql_bind_result(stmt, bind);
  mystmt(stmt,rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  fprintf(stdout, "\n data: %g", data);
  myassert(data == 5.61);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  is_null= 1;
  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  data= 10.22; is_null= 0;
  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  data=0; 
  rc = mysql_bind_result(stmt, bind);
  mystmt(stmt,rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  fprintf(stdout, "\n data: %g", data);
  myassert(data == 10.22);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  mysql_stmt_close(stmt);
}


/*
  To test EXPLAIN bug
  bug #115 (reported by mark@mysql.com & georg@php.net).
*/

static void test_explain_bug()
{
  MYSQL_STMT *stmt;
  MYSQL_RES  *result;
  int        rc;

  myheader("test_explain_bug");

  mysql_autocommit(mysql,TRUE);

  rc = mysql_query(mysql, "DROP TABLE IF EXISTS test_explain");
  myquery(rc);
  
  rc = mysql_query(mysql, "CREATE TABLE test_explain(id int, name char(2))");
  myquery(rc);

  stmt = mysql_prepare(mysql, "explain test_explain", 30);
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  myassert( 2 == my_process_stmt_result(stmt));  

unknown's avatar
unknown committed
6499
  result = mysql_get_metadata(stmt);
6500 6501 6502 6503
  mytest(result);

  fprintf(stdout, "\n total fields in the result: %d", 
          mysql_num_fields(result));
unknown's avatar
unknown committed
6504
  myassert(6 == mysql_num_fields(result));
6505 6506

  verify_prepare_field(result,0,"Field","",MYSQL_TYPE_STRING,
6507
                       "","","",NAME_LEN,0);
6508 6509

  verify_prepare_field(result,1,"Type","",MYSQL_TYPE_STRING,
6510
                       "","","",40,0);
6511

unknown's avatar
unknown committed
6512
  verify_prepare_field(result,2,"Null","",MYSQL_TYPE_STRING,
6513
                       "","","",1,0);
unknown's avatar
unknown committed
6514 6515

  verify_prepare_field(result,3,"Key","",MYSQL_TYPE_STRING,
6516
                       "","","",3,0);
unknown's avatar
unknown committed
6517 6518

  verify_prepare_field(result,4,"Default","",MYSQL_TYPE_STRING,
6519
                       "","","",NAME_LEN,0);
unknown's avatar
unknown committed
6520 6521

  verify_prepare_field(result,5,"Extra","",MYSQL_TYPE_STRING,
6522
                       "","","",20,0);
6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534

  mysql_free_result(result);
  mysql_stmt_close(stmt);
  
  stmt = mysql_prepare(mysql, "explain select id, name FROM test_explain", 50);
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  myassert( 1 == my_process_stmt_result(stmt)); 

unknown's avatar
unknown committed
6535
  result = mysql_get_metadata(stmt);
6536 6537 6538 6539 6540 6541 6542
  mytest(result);

  fprintf(stdout, "\n total fields in the result: %d", 
          mysql_num_fields(result));
  myassert(10 == mysql_num_fields(result));

  verify_prepare_field(result,0,"id","",MYSQL_TYPE_LONGLONG,
6543
                       "","","",3,0);
6544 6545

  verify_prepare_field(result,1,"select_type","",MYSQL_TYPE_STRING,
6546
                       "","","",19,0);
6547 6548

  verify_prepare_field(result,2,"table","",MYSQL_TYPE_STRING,
6549
                       "","","",NAME_LEN,0);
6550 6551

  verify_prepare_field(result,3,"type","",MYSQL_TYPE_STRING,
6552
                       "","","",10,0);
6553 6554

  verify_prepare_field(result,4,"possible_keys","",MYSQL_TYPE_STRING,
6555
                       "","","",NAME_LEN*32,0);
6556 6557

  verify_prepare_field(result,5,"key","",MYSQL_TYPE_STRING,
6558
                       "","","",NAME_LEN,0);
6559 6560

  verify_prepare_field(result,6,"key_len","",MYSQL_TYPE_LONGLONG,
6561
                       "","","",3,0);
6562 6563

  verify_prepare_field(result,7,"ref","",MYSQL_TYPE_STRING,
6564
                       "","","",NAME_LEN*16,0);
6565 6566

  verify_prepare_field(result,8,"rows","",MYSQL_TYPE_LONGLONG,
6567
                       "","","",10,0);
6568 6569

  verify_prepare_field(result,9,"Extra","",MYSQL_TYPE_STRING,
6570
                       "","","",255,0);
6571 6572 6573 6574 6575

  mysql_free_result(result);
  mysql_stmt_close(stmt);
}

unknown's avatar
unknown committed
6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623
/*
  To test math functions
  bug #148 (reported by salle@mysql.com).
*/
static void test_drop_temp()
{
  int rc;

  myheader("test_drop_temp");

  rc= mysql_query(mysql,"DROP DATABASE IF EXISTS test_drop_temp_db");
  myquery(rc);

  rc= mysql_query(mysql,"CREATE DATABASE test_drop_temp_db");
  myquery(rc);
  
  rc= mysql_query(mysql,"CREATE TABLE test_drop_temp_db.t1(c1 int, c2 char(1))");
  myquery(rc);

  rc = mysql_query(mysql,"delete from mysql.db where Db='test_drop_temp_db'");
  myquery(rc);

  rc = mysql_query(mysql,"delete from mysql.db where Db='test_drop_temp_db'");
  myquery(rc);

  strxmov(query,"GRANT SELECT,USAGE,DROP ON test_drop_temp_db.* TO test_temp@",
                opt_host ? opt_host : "localhost", NullS);

  if (mysql_query(mysql,query))
  {
    myerror("GRANT failed");
    
    /* 
       If server started with --skip-grant-tables, skip this test, else
       exit to indicate an error

       ER_UNKNOWN_COM_ERROR = 1047
     */ 
    if (mysql_errno(mysql) != 1047)  
      exit(0);  
  }
  else
  {
    MYSQL *org_mysql= mysql, *lmysql;
    
    fprintf(stdout, "\n Establishing a test connection ...");
    if (!(lmysql = mysql_init(NULL)))
    { 
6624
      myerror("mysql_init() failed");
unknown's avatar
unknown committed
6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843
      exit(0);
    }

    rc = mysql_query(mysql,"flush privileges");
    myquery(rc);

    if (!(mysql_real_connect(lmysql,opt_host ? opt_host : "localhost","test_temp",
			     "", "test_drop_temp_db", opt_port,
			     opt_unix_socket, 0)))
    {
      mysql= lmysql;
      myerror("connection failed");   
      mysql_close(lmysql);
      exit(0);
    }   
    fprintf(stdout," OK");

    mysql= lmysql;
    rc = mysql_query(mysql,"INSERT INTO t1 VALUES(10,'C')");
    myerrno((uint)1142);

    rc = mysql_query(mysql,"DROP TABLE t1");
    myerrno((uint)1142);
  
    mysql= org_mysql;
    rc= mysql_query(mysql,"CREATE TEMPORARY TABLE test_drop_temp_db.t1(c1 int)");
    myquery(rc);
  
    rc= mysql_query(mysql,"CREATE TEMPORARY TABLE test_drop_temp_db.t2 LIKE test_drop_temp_db.t1");
    myquery(rc);

    mysql= lmysql;

    rc = mysql_query(mysql,"DROP TABLE t1,t2");
    myquery_r(rc);

    rc = mysql_query(mysql,"DROP TEMPORARY TABLE t1");
    myquery_r(rc);

    rc = mysql_query(mysql,"DROP TEMPORARY TABLE t2");
    myquery_r(rc);
    
    mysql_close(lmysql);        
    mysql= org_mysql;

    rc = mysql_query(mysql,"drop database test_drop_temp_db");
    myquery(rc);
    myassert(1 == mysql_affected_rows(mysql));

    rc = mysql_query(mysql,"delete from mysql.user where User='test_temp'");
    myquery(rc);
    myassert(1 == mysql_affected_rows(mysql));


    rc = mysql_query(mysql,"delete from mysql.tables_priv where User='test_temp'");
    myquery(rc);
    myassert(1 == mysql_affected_rows(mysql));
  }
}

/*
  To test warnings for cuted rows
*/
static void test_cuted_rows()
{
  int        rc, count;
  MYSQL_RES  *result;

  myheader("test_cuted_rows");

  mysql_query(mysql, "DROP TABLE if exists t1");  
  mysql_query(mysql, "DROP TABLE if exists t2");

  rc = mysql_query(mysql, "CREATE TABLE t1(c1 tinyint)");
  myquery(rc);

  rc = mysql_query(mysql, "CREATE TABLE t2(c1 int not null)");
  myquery(rc);

  rc = mysql_query(mysql, "INSERT INTO t1 values(10),(NULL),(NULL)");
  myquery(rc);

  count= mysql_warning_count(mysql);
  fprintf(stdout, "\n total warnings: %d", count);
  myassert(count == 0);

  rc = mysql_query(mysql, "INSERT INTO t2 SELECT * FROM t1");
  myquery(rc);

  count= mysql_warning_count(mysql);
  fprintf(stdout, "\n total warnings: %d", count);
  myassert(count == 2);

  rc = mysql_query(mysql, "SHOW WARNINGS");
  myquery(rc);

  result = mysql_store_result(mysql);
  mytest(result);

  myassert(2 == my_process_result_set(result));
  mysql_free_result(result);

  rc = mysql_query(mysql, "INSERT INTO t1 VALUES('junk'),(876789)");
  myquery(rc);

  count= mysql_warning_count(mysql);
  fprintf(stdout, "\n total warnings: %d", count);
  myassert(count == 2);

  rc = mysql_query(mysql, "SHOW WARNINGS");
  myquery(rc);

  result = mysql_store_result(mysql);
  mytest(result);

  myassert(2 == my_process_result_set(result));
  mysql_free_result(result);
}

/*
  To test update/binary logs
*/
static void test_logs()
{
  MYSQL_STMT *stmt;
  MYSQL_BIND bind[2];
  char       data[255];
  ulong      length;
  int        rc;
  short      id;

  myheader("test_logs");


  rc = mysql_real_query(mysql, "DROP TABLE IF EXISTS test_logs", 100);
  myquery(rc);

  rc = mysql_real_query(mysql, "CREATE TABLE test_logs(id smallint, name varchar(20))", 100);
  myquery(rc);

  length= (ulong)(strmov((char *)data,"INSERT INTO test_logs VALUES(?,?)") - data);
  stmt = mysql_prepare(mysql, data, length);
  mystmt_init(stmt); 
  
  bind[0].buffer_type= MYSQL_TYPE_SHORT;
  bind[0].buffer= (char *)&id;
  bind[0].is_null= 0;
  bind[0].length= 0;
  
  bind[1].buffer_type= MYSQL_TYPE_STRING;
  bind[1].buffer= (char *)&data;
  bind[1].is_null= 0;
  bind[1].buffer_length= 255;
  bind[1].length= &length;

  id= 9876;
  length= (ulong)(strmov((char *)data,"MySQL - Open Source Database")- data);    

  rc = mysql_bind_param(stmt, bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  strmov((char *)data, "'");
  length= 1;

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);
  
  strmov((char *)data, "\"");
  length= 1;

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);
  
  length= (ulong)(strmov((char *)data, "my\'sql\'")-data);
  rc = mysql_execute(stmt);
  mystmt(stmt, rc);
  
  length= (ulong)(strmov((char *)data, "my\"sql\"")-data);
  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  mysql_stmt_close(stmt);

  length= (ulong)(strmov((char *)data,"INSERT INTO test_logs VALUES(20,'mysql')") - data);
  stmt = mysql_prepare(mysql, data, length);
  mystmt_init(stmt); 

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  mysql_stmt_close(stmt);

  length= (ulong)(strmov((char *)data, "SELECT * FROM test_logs WHERE id=?") - data);
  stmt = mysql_prepare(mysql, data, length+2);
  mystmt_init(stmt); 

  rc = mysql_bind_param(stmt, bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  bind[1].buffer_length= 255;
  rc = mysql_bind_result(stmt, bind);
  mystmt(stmt, rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt, rc);

  fprintf(stdout, "\n id    : %d", id);
  fprintf(stdout, "\n name  : %s(%ld)", data, length);

  myassert(id == 9876);  
6844
  myassert(length == 19); /* Due to VARCHAR(20) */
unknown's avatar
unknown committed
6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876
  myassert(strcmp(data,"MySQL - Open Source")==0); 

  rc = mysql_fetch(stmt);
  mystmt(stmt, rc);
  
  fprintf(stdout, "\n name  : %s(%ld)", data, length);

  myassert(length == 1);
  myassert(strcmp(data,"'")==0); 

  rc = mysql_fetch(stmt);
  mystmt(stmt, rc);
  
  fprintf(stdout, "\n name  : %s(%ld)", data, length);

  myassert(length == 1);
  myassert(strcmp(data,"\"")==0); 

  rc = mysql_fetch(stmt);
  mystmt(stmt, rc);
  
  fprintf(stdout, "\n name  : %s(%ld)", data, length);

  myassert(length == 7);
  myassert(strcmp(data,"my\'sql\'")==0); 

  rc = mysql_fetch(stmt);
  mystmt(stmt, rc);
  
  fprintf(stdout, "\n name  : %s(%ld)", data, length);

  myassert(length == 7);
unknown's avatar
unknown committed
6877
  /*myassert(strcmp(data,"my\"sql\"")==0); */
unknown's avatar
unknown committed
6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  mysql_stmt_close(stmt);

  rc = mysql_query(mysql,"DROP TABLE test_logs");
  myquery(rc);
}

/*
  To test 'n' statements create and close  
*/

static void test_nstmts()
{
  MYSQL_STMT  *stmt;
  char        query[255];
  int         rc;
  static uint i, total_stmts= 2000;
  long        length;
  MYSQL_BIND  bind[1];

  myheader("test_nstmts");

  mysql_autocommit(mysql,TRUE);

  rc = mysql_query(mysql, "DROP TABLE IF EXISTS test_nstmts");
  myquery(rc);
  
  rc = mysql_query(mysql, "CREATE TABLE test_nstmts(id int)");
  myquery(rc);

  bind[0].buffer= (char *)&i;
  bind[0].buffer_type= MYSQL_TYPE_LONG;
  bind[0].length= 0;
  bind[0].is_null= 0;
  bind[0].buffer_length= 0;
  
  for (i=0; i < total_stmts; i++)
  {
    fprintf(stdout, "\r stmt: %d", i);
    
    length = (long)(strmov(query, "insert into test_nstmts values(?)")-query);
    stmt = mysql_prepare(mysql, query, length);
    mystmt_init(stmt);

    rc = mysql_bind_param(stmt, bind);
    mystmt(stmt, rc);

    rc = mysql_execute(stmt);
    mystmt(stmt, rc);

    mysql_stmt_close(stmt);
  }

  stmt = mysql_prepare(mysql," select count(*) from test_nstmts", 50);
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);

  i = 0;
  rc = mysql_bind_result(stmt, bind);
  mystmt(stmt, rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt, rc);
  fprintf(stdout, "\n total rows: %d", i);
  myassert( i == total_stmts);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  mysql_stmt_close(stmt);
  
  rc = mysql_query(mysql,"DROP TABLE test_nstmts");
  myquery(rc);
}

/*
  To test stmt seek() functions
*/
static void test_fetch_seek()
{
  MYSQL_STMT *stmt;
  MYSQL_BIND bind[3];
  MYSQL_ROW_OFFSET row;
  int        rc;
  long       c1;
  char       c2[11], c3[20];

  myheader("test_fetch_seek");

  rc= mysql_query(mysql,"drop table if exists test_seek");
  myquery(rc);
  
  rc = mysql_query(mysql, "create table test_seek(c1 int primary key auto_increment, c2 char(10), c3 timestamp(14))");
  myquery(rc);
  
  rc = mysql_query(mysql, "insert into test_seek(c2) values('venu'),('mysql'),('open'),('source')");
  myquery(rc);

  stmt = mysql_prepare(mysql,"select * from test_seek",50);
  mystmt_init(stmt);

  bind[0].buffer_type= MYSQL_TYPE_LONG;
  bind[0].buffer= (char *)&c1;
  bind[0].buffer_length= 0;
  bind[0].is_null= 0;
  bind[0].length= 0;

  bind[1].buffer_type= MYSQL_TYPE_STRING;
  bind[1].buffer= (char *)c2;
  bind[1].buffer_length= sizeof(c2);
  bind[1].is_null= 0;
  bind[1].length= 0;

  bind[2]= bind[1];
  bind[2].buffer= (char *)c3;
  bind[2].buffer_length= sizeof(c3);

  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  rc = mysql_bind_result(stmt, bind);
  mystmt(stmt,rc);

  rc = mysql_stmt_store_result(stmt);
  mystmt(stmt,rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  fprintf(stdout, "\n row 0: %ld,%s,%s", c1,c2,c3);

  row = mysql_stmt_row_tell(stmt);

  row = mysql_stmt_row_seek(stmt, row);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  fprintf(stdout, "\n row 2: %ld,%s,%s", c1,c2,c3);

  row = mysql_stmt_row_seek(stmt, row);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  fprintf(stdout, "\n row 2: %ld,%s,%s", c1,c2,c3);

  mysql_stmt_data_seek(stmt, 0);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  fprintf(stdout, "\n row 0: %ld,%s,%s", c1,c2,c3);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  mysql_stmt_close(stmt);
}

/*
  To test mysql_fetch_column() with offset 
*/
static void test_fetch_offset()
{
  MYSQL_STMT *stmt;
  MYSQL_BIND bind[1];
  char       data[11];
  ulong      length;
  int        rc;
  my_bool    is_null;


  myheader("test_fetch_offset");

  rc= mysql_query(mysql,"drop table if exists test_column");
  myquery(rc);
  
  rc = mysql_query(mysql, "create table test_column(a char(10))");
  myquery(rc);
  
  rc = mysql_query(mysql, "insert into test_column values('abcdefghij'),(null)");
  myquery(rc);

  stmt = mysql_prepare(mysql,"select * from test_column",50);
  mystmt_init(stmt);

  bind[0].buffer_type= MYSQL_TYPE_STRING;
  bind[0].buffer= (char *)data;
  bind[0].buffer_length= 11;
  bind[0].is_null= &is_null;
  bind[0].length= &length;

  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  rc = mysql_fetch_column(stmt,bind,0,0);
  mystmt_r(stmt,rc);

  rc = mysql_stmt_store_result(stmt);
  mystmt(stmt,rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);
  
  rc = mysql_fetch_column(stmt,bind,4,0);
  mystmt_r(stmt,rc);

  data[0]= '\0';
  rc = mysql_fetch_column(stmt,bind,0,0);
  mystmt(stmt,rc);
  fprintf(stdout, "\n col 1: %s (%ld)", data, length);
  myassert(strncmp(data,"abcd",4) == 0 && length == 10);
  
  rc = mysql_fetch_column(stmt,bind,0,5);
  mystmt(stmt,rc);
  fprintf(stdout, "\n col 1: %s (%ld)", data, length);
  myassert(strncmp(data,"fg",2) == 0 && length == 10);  

  rc = mysql_fetch_column(stmt,bind,0,9);
  mystmt(stmt,rc);
  fprintf(stdout, "\n col 0: %s (%ld)", data, length);
  myassert(strncmp(data,"j",1) == 0 && length == 10);  

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  is_null= 0;

  rc = mysql_fetch_column(stmt,bind,0,0);
  mystmt(stmt,rc);

  myassert(is_null == 1);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  rc = mysql_fetch_column(stmt,bind,1,0);
  mystmt_r(stmt,rc);

  mysql_stmt_close(stmt);
}
/*
  To test mysql_fetch_column()
*/
static void test_fetch_column()
{
  MYSQL_STMT *stmt;
  MYSQL_BIND bind[1];
  char       c2[20];
  ulong      l1, l2;
  int        rc, c1;

7145

unknown's avatar
unknown committed
7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202
  myheader("test_fetch_column");

  rc= mysql_query(mysql,"drop table if exists test_column");
  myquery(rc);
  
  rc = mysql_query(mysql, "create table test_column(c1 int primary key auto_increment, c2 char(10))");
  myquery(rc);
  
  rc = mysql_query(mysql, "insert into test_column(c2) values('venu'),('mysql')");
  myquery(rc);

  stmt = mysql_prepare(mysql,"select * from test_column",50);
  mystmt_init(stmt);

  bind[0].buffer_type= MYSQL_TYPE_STRING;
  bind[0].buffer= (char *)c2;
  bind[0].buffer_length= 7;
  bind[0].is_null= 0;
  bind[0].length= &l2;

  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  rc = mysql_stmt_store_result(stmt);
  mystmt(stmt,rc);

  rc = mysql_fetch_column(stmt,bind,1,0);
  mystmt_r(stmt,rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  c2[0]= '\0'; l2= 0;
  rc = mysql_fetch_column(stmt,bind,1,0);
  mystmt(stmt,rc);
  fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
  myassert(strncmp(c2,"venu",4)==0 && l2 == 4);
  
  c2[0]= '\0'; l2= 0;
  rc = mysql_fetch_column(stmt,bind,1,0);
  mystmt(stmt,rc);
  fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
  myassert(strcmp(c2,"venu")==0 && l2 == 4);  

  c1= 0;   

  bind[0].buffer_type= MYSQL_TYPE_LONG;
  bind[0].buffer= (char *)&c1;
  bind[0].buffer_length= 0;
  bind[0].is_null= 0;
  bind[0].length= &l1;

  rc = mysql_fetch_column(stmt,bind,0,0);
  mystmt(stmt,rc);
  fprintf(stdout, "\n col 0: %d(%ld)", c1, l1);
  myassert(c1 == 1 && l1 == 4);

7203 7204 7205
  rc = mysql_fetch_column(stmt,bind,10,0);
  mystmt_r(stmt,rc);

unknown's avatar
unknown committed
7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249
  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);  

  bind[0].buffer_type= MYSQL_TYPE_STRING;
  bind[0].buffer= (char *)c2;
  bind[0].buffer_length= 7;
  bind[0].is_null= 0;
  bind[0].length= &l2;

  fprintf(stdout, "\n row 1: %d,%s", c1,c2);

  c2[0]= '\0'; l2= 0;
  rc = mysql_fetch_column(stmt,bind,1,0);
  mystmt(stmt,rc);
  fprintf(stdout, "\n col 1: %s(%ld)", c2, l2);
  myassert(strncmp(c2,"mysq",4)==0 && l2 == 5);
  
  c2[0]= '\0'; l2= 0;
  rc = mysql_fetch_column(stmt,bind,1,0);
  mystmt(stmt,rc);
  fprintf(stdout, "\n col 1: %si(%ld)", c2, l2);
  myassert(strcmp(c2,"mysql")==0 && l2 == 5);  

  c1= 0;     

  bind[0].buffer_type= MYSQL_TYPE_LONG;
  bind[0].buffer= (char *)&c1;
  bind[0].buffer_length= 0;
  bind[0].is_null= 0;
  bind[0].length= &l1;

  rc = mysql_fetch_column(stmt,bind,0,0);
  mystmt(stmt,rc);
  fprintf(stdout, "\n col 0: %d(%ld)", c1, l1);
  myassert(c1 == 2 && l1 == 4);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  rc = mysql_fetch_column(stmt,bind,1,0);
  mystmt_r(stmt,rc);

  mysql_stmt_close(stmt);
}
7250

unknown's avatar
unknown committed
7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262
/*
  To test mysql_list_fields()
*/
static void test_list_fields()
{
  MYSQL_RES *result;
  int rc;
  myheader("test_list_fields");

  rc= mysql_query(mysql,"drop table if exists test_list_fields");
  myquery(rc);
  
7263
  rc = mysql_query(mysql, "create table test_list_fields(c1 int primary key auto_increment, c2 char(10) default 'mysql')");
unknown's avatar
unknown committed
7264 7265 7266 7267 7268 7269
  myquery(rc);

  result = mysql_list_fields(mysql, "test_list_fields",NULL);
  mytest(result);

  myassert( 0 == my_process_result_set(result));
7270 7271 7272 7273 7274 7275 7276
  
  verify_prepare_field(result,0,"c1","c1",MYSQL_TYPE_LONG,
                       "test_list_fields","test_list_fields",current_db,11,"0");
  
  verify_prepare_field(result,1,"c2","c2",MYSQL_TYPE_STRING,
                       "test_list_fields","test_list_fields",current_db,10,"mysql");

unknown's avatar
unknown committed
7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323
  mysql_free_result(result);
}

/*
  To test a memory ovverun bug
*/
static void test_mem_overun()
{
  char       buffer[10000], field[10];
  MYSQL_STMT *stmt;
  MYSQL_RES  *field_res;
  int        rc,i, length;


  myheader("test_mem_overun");

  /*
    Test a memory ovverun bug when a table had 1000 fields with 
    a row of data
  */
  rc= mysql_query(mysql,"drop table if exists t_mem_overun");
  myquery(rc);

  strxmov(buffer,"create table t_mem_overun(",NullS);
  for (i=0; i < 1000; i++)
  {
    sprintf(field,"c%d int", i);
    strxmov(buffer,buffer,field,",",NullS);
  }
  length= (int)(strmov(buffer,buffer) - buffer);
  buffer[length-1]='\0';
  strxmov(buffer,buffer,")",NullS);
  
  rc = mysql_real_query(mysql, buffer, length);
  myquery(rc);

  strxmov(buffer,"insert into t_mem_overun values(",NullS);
  for (i=0; i < 1000; i++)
  {
    strxmov(buffer,buffer,"1,",NullS);
  }
  length= (int)(strmov(buffer,buffer) - buffer);
  buffer[length-1]='\0';
  strxmov(buffer,buffer,")",NullS);
  
  rc = mysql_real_query(mysql, buffer, length);
  myquery(rc);
7324 7325 7326 7327 7328

  rc = mysql_query(mysql,"select * from t_mem_overun");
  myquery(rc);

  myassert(1 == my_process_result(mysql));
unknown's avatar
unknown committed
7329 7330 7331 7332 7333 7334 7335
  
  stmt = mysql_prepare(mysql, "select * from t_mem_overun",30);
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt,rc); 
  
unknown's avatar
unknown committed
7336
  field_res = mysql_get_metadata(stmt);
unknown's avatar
unknown committed
7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352
  mytest(field_res);

  fprintf(stdout,"\n total fields : %d", mysql_num_fields(field_res));
  myassert( 1000 == mysql_num_fields(field_res));

  rc = mysql_stmt_store_result(stmt);
  mystmt(stmt,rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  mysql_stmt_close(stmt);
}
7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495

/*
  To test mysql_stmt_free_result()
*/
static void test_free_result()
{
  MYSQL_STMT *stmt;
  MYSQL_BIND bind[1];
  char       c2[5];
  ulong      length;
  int        rc, c1;

  myheader("test_free_result");

  rc= mysql_query(mysql,"drop table if exists test_free_result");
  myquery(rc);
  
  rc = mysql_query(mysql, "create table test_free_result(c1 int primary key auto_increment)");
  myquery(rc);
  
  rc = mysql_query(mysql, "insert into test_free_result values(),(),()");
  myquery(rc);

  stmt = mysql_prepare(mysql,"select * from test_free_result",50);
  mystmt_init(stmt);

  bind[0].buffer_type= MYSQL_TYPE_STRING;
  bind[0].buffer= (char *)c2;
  bind[0].buffer_length= 7;
  bind[0].is_null= 0;
  bind[0].length= &length;

  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  c2[0]= '\0'; length= 0;
  rc = mysql_fetch_column(stmt,bind,0,0);
  mystmt(stmt,rc);
  fprintf(stdout, "\n col 1: %s(%ld)", c2, length);
  myassert(strncmp(c2,"1",1)==0 && length == 1);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);  

  c1= 0, length= 0;   

  bind[0].buffer_type= MYSQL_TYPE_LONG;
  bind[0].buffer= (char *)&c1;
  bind[0].buffer_length= 0;
  bind[0].is_null= 0;
  bind[0].length= &length;

  rc = mysql_fetch_column(stmt,bind,0,0);
  mystmt(stmt,rc);
  fprintf(stdout, "\n col 0: %d(%ld)", c1, length);
  myassert(c1 == 2 && length == 4);  

  rc = mysql_query(mysql,"drop table test_free_result");
  myquery_r(rc); /* error should be, COMMANDS OUT OF SYNC */

  rc = mysql_stmt_free_result(stmt);
  mystmt(stmt,rc);

  rc = mysql_query(mysql,"drop table test_free_result");
  myquery(rc);  /* should be successful */

  mysql_stmt_close(stmt);
}

/*
  To test mysql_stmt_free_result()
*/
static void test_free_store_result()
{
  MYSQL_STMT *stmt;
  MYSQL_BIND bind[1];
  char       c2[5];
  ulong      length;
  int        rc, c1;

  myheader("test_free_store_result");

  rc= mysql_query(mysql,"drop table if exists test_free_result");
  myquery(rc);
  
  rc = mysql_query(mysql, "create table test_free_result(c1 int primary key auto_increment)");
  myquery(rc);
  
  rc = mysql_query(mysql, "insert into test_free_result values(),(),()");
  myquery(rc);

  stmt = mysql_prepare(mysql,"select * from test_free_result",50);
  mystmt_init(stmt);

  bind[0].buffer_type= MYSQL_TYPE_STRING;
  bind[0].buffer= (char *)c2;
  bind[0].buffer_length= 7;
  bind[0].is_null= 0;
  bind[0].length= &length;

  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  rc = mysql_stmt_store_result(stmt);
  mystmt(stmt,rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  c2[0]= '\0'; length= 0;
  rc = mysql_fetch_column(stmt,bind,0,0);
  mystmt(stmt,rc);
  fprintf(stdout, "\n col 1: %s(%ld)", c2, length);
  myassert(strncmp(c2,"1",1)==0 && length == 1);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);  

  c1= 0, length= 0;   

  bind[0].buffer_type= MYSQL_TYPE_LONG;
  bind[0].buffer= (char *)&c1;
  bind[0].buffer_length= 0;
  bind[0].is_null= 0;
  bind[0].length= &length;

  rc = mysql_fetch_column(stmt,bind,0,0);
  mystmt(stmt,rc);
  fprintf(stdout, "\n col 0: %d(%ld)", c1, length);
  myassert(c1 == 2 && length == 4);  

  rc = mysql_stmt_free_result(stmt);
  mystmt(stmt,rc);

  rc = mysql_query(mysql,"drop table test_free_result");
  myquery(rc); 

  mysql_stmt_close(stmt);
}

7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624
/********************************************************
 To test SQLmode
*********************************************************/

static void test_sqlmode()
{
  MYSQL_STMT *stmt;
  MYSQL_BIND bind[2];
  char       c1[5], c2[5];
  int        rc;

  myheader("test_sqlmode");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_piping");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

  rc = mysql_query(mysql,"CREATE TABLE test_piping(name varchar(10))");
  myquery(rc);
  
  /* PIPES_AS_CONCAT */
  strcpy(query,"SET SQL_MODE=\"PIPES_AS_CONCAT\"");
  fprintf(stdout,"\n With %s", query);
  rc = mysql_query(mysql,query);
  myquery(rc);

  strcpy(query, "INSERT INTO test_piping VALUES(?||?)");
  fprintf(stdout,"\n  query: %s", query);
  stmt = mysql_prepare(mysql, query, strlen(query));
  mystmt_init(stmt);

  fprintf(stdout,"\n  total parameters: %ld", mysql_param_count(stmt));      

  bind[0].buffer_type= MYSQL_TYPE_STRING;
  bind[0].buffer= (char *)c1;
  bind[0].buffer_length= 2;
  bind[0].is_null= 0;
  bind[0].length= 0;

  bind[1].buffer_type= MYSQL_TYPE_STRING;
  bind[1].buffer= (char *)c2;
  bind[1].buffer_length= 3;
  bind[1].is_null= 0;
  bind[1].length= 0;

  rc = mysql_bind_param(stmt, bind);
  mystmt(stmt,rc);

  strcpy(c1,"My"); strcpy(c2, "SQL");
  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  mysql_stmt_close(stmt);
  verify_col_data("test_piping","name","MySQL");  

  rc = mysql_query(mysql,"DELETE FROM test_piping");
  myquery(rc);
  
  strcpy(query, "SELECT connection_id    ()");
  fprintf(stdout,"\n  query: %s", query);
  stmt = mysql_prepare(mysql, query, 70);
  mystmt_init_r(stmt);

  /* ANSI */
  strcpy(query,"SET SQL_MODE=\"ANSI\"");
  fprintf(stdout,"\n With %s", query);
  rc = mysql_query(mysql,query);
  myquery(rc);

  strcpy(query, "INSERT INTO test_piping VALUES(?||?)");
  fprintf(stdout,"\n  query: %s", query);
  stmt = mysql_prepare(mysql, query, strlen(query));
  mystmt_init(stmt);
  fprintf(stdout,"\n  total parameters: %ld", mysql_param_count(stmt));      

  rc = mysql_bind_param(stmt, bind);
  mystmt(stmt,rc);

  strcpy(c1,"My"); strcpy(c2, "SQL");
  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  mysql_stmt_close(stmt);
  verify_col_data("test_piping","name","MySQL");  

  /* ANSI mode spaces ... */
  strcpy(query, "SELECT connection_id    ()");
  fprintf(stdout,"\n  query: %s", query);
  stmt = mysql_prepare(mysql, query, 70);
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);
  fprintf(stdout,"\n  returned 1 row\n");

  mysql_stmt_close(stmt);
  
  /* IGNORE SPACE MODE */
  strcpy(query,"SET SQL_MODE=\"IGNORE_SPACE\"");
  fprintf(stdout,"\n With %s", query);
  rc = mysql_query(mysql,query);
  myquery(rc);

  strcpy(query, "SELECT connection_id    ()");
  fprintf(stdout,"\n  query: %s", query);
  stmt = mysql_prepare(mysql, query, 70);
  mystmt_init(stmt);

  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);
  fprintf(stdout,"\n  returned 1 row");

  mysql_stmt_close(stmt);
}

7625 7626 7627 7628 7629 7630
/*
  test for timestamp handling
*/
static void test_ts()
{
  MYSQL_STMT *stmt;
unknown's avatar
unknown committed
7631
  MYSQL_BIND bind[6];
7632
  MYSQL_TIME ts;
unknown's avatar
unknown committed
7633
  MYSQL_RES  *prep_res;
7634 7635
  char       strts[30];
  long       length;
unknown's avatar
unknown committed
7636 7637
  int        rc, field_count;
  char       name;
7638 7639 7640 7641 7642 7643

  myheader("test_ts");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_ts");
  myquery(rc);
  
unknown's avatar
unknown committed
7644
  rc= mysql_query(mysql,"CREATE TABLE test_ts(a DATE, b TIME, c TIMESTAMP)");
7645 7646 7647 7648 7649
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);

unknown's avatar
unknown committed
7650
  stmt = mysql_prepare(mysql,"INSERT INTO test_ts VALUES(?,?,?),(?,?,?)",50);
7651 7652 7653 7654 7655 7656 7657 7658 7659 7660
  mystmt_init(stmt);

  ts.year= 2003;
  ts.month= 07;
  ts.day= 12;
  ts.hour= 21;
  ts.minute= 07;
  ts.second= 46;
  length= (long)(strmov(strts,"2003-07-12 21:07:46") - strts);

unknown's avatar
unknown committed
7661 7662 7663
  bind[0].buffer_type= MYSQL_TYPE_TIMESTAMP;
  bind[0].buffer= (char *)&ts;
  bind[0].buffer_length= sizeof(ts);
7664
  bind[0].is_null= 0;
unknown's avatar
unknown committed
7665
  bind[0].length= 0;
7666

unknown's avatar
unknown committed
7667 7668 7669 7670 7671 7672 7673 7674 7675
  bind[2]= bind[1]= bind[0];

  bind[3].buffer_type= MYSQL_TYPE_STRING;
  bind[3].buffer= (char *)strts;
  bind[3].buffer_length= sizeof(strts);
  bind[3].is_null= 0;
  bind[3].length= &length;

  bind[5]= bind[4]= bind[3];
7676 7677 7678 7679 7680 7681 7682 7683 7684

  rc = mysql_bind_param(stmt, bind);
  mystmt(stmt,rc);

  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  mysql_stmt_close(stmt);

unknown's avatar
unknown committed
7685 7686 7687
  verify_col_data("test_ts","a","2003-07-12");
  verify_col_data("test_ts","b","21:07:46");
  verify_col_data("test_ts","c","2003-07-12 21:07:46");
7688

unknown's avatar
unknown committed
7689
  stmt = mysql_prepare(mysql,"SELECT * FROM test_ts",50);
7690 7691
  mystmt_init(stmt);

unknown's avatar
unknown committed
7692
  prep_res = mysql_get_metadata(stmt);
unknown's avatar
unknown committed
7693
  mytest(prep_res);
7694 7695 7696 7697

  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

unknown's avatar
unknown committed
7698 7699
  myassert( 2== my_process_stmt_result(stmt));
  field_count= mysql_num_fields(prep_res);
7700

unknown's avatar
unknown committed
7701 7702
  mysql_free_result(prep_res);
  mysql_stmt_close(stmt);
7703

unknown's avatar
unknown committed
7704 7705 7706
  for (name= 'a'; field_count--; name++)
  {
    int row_count= 0;
7707

unknown's avatar
unknown committed
7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727
    sprintf(query,"SELECT a,b,c FROM test_ts WHERE %c=?",name);
    length= (long)(strmov(query,query)- query);

    fprintf(stdout,"\n  %s", query);
    stmt = mysql_prepare(mysql, query, length);
    mystmt_init(stmt);

    rc = mysql_bind_param(stmt, bind);
    mystmt(stmt,rc);

    rc = mysql_execute(stmt);
    mystmt(stmt,rc);

    while (mysql_fetch(stmt) == 0)
      row_count++;

    fprintf(stdout, "\n   returned '%d' rows", row_count);
    myassert(row_count == 2);
    mysql_stmt_close(stmt);
  }
7728 7729
}

7730

7731 7732 7733 7734 7735 7736 7737 7738
/*
  Read and parse arguments and MySQL options from my.cnf
*/

static const char *client_test_load_default_groups[]= { "client", 0 };
static char **defaults_argv;

static struct my_option client_test_long_options[] =
unknown's avatar
unknown committed
7739
{
unknown's avatar
unknown committed
7740 7741
  {"help", '?', "Display this help and exit", 0, 0, 0, GET_NO_ARG, NO_ARG, 0,
   0, 0, 0, 0, 0},
unknown's avatar
unknown committed
7742
  {"database", 'D', "Database to use", (char **) &opt_db, (char **) &opt_db,
unknown's avatar
unknown committed
7743
   0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
unknown's avatar
unknown committed
7744
  {"host", 'h', "Connect to host", (char **) &opt_host, (char **) &opt_host, 0, GET_STR_ALLOC,
unknown's avatar
unknown committed
7745 7746 7747 7748
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  {"password", 'p',
   "Password to use when connecting to server. If password is not given it's asked from the tty.",
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
unknown's avatar
unknown committed
7749
#ifndef DONT_ALLOW_USER_CHANGE
unknown's avatar
unknown committed
7750 7751
  {"user", 'u', "User for login if not current user", (char **) &opt_user,
   (char **) &opt_user, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
unknown's avatar
unknown committed
7752
#endif
unknown's avatar
unknown committed
7753 7754 7755 7756
  {"port", 'P', "Port number to use for connection", (char **) &opt_port,
   (char **) &opt_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  {"socket", 'S', "Socket file to use for connection", (char **) &opt_unix_socket,
   (char **) &opt_unix_socket, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
7757
  {"count", 't', "Number of times test to be executed", (char **) &opt_count,
7758
   (char **) &opt_count, 0, GET_UINT, REQUIRED_ARG, 1, 0, 0, 0, 0, 0},
unknown's avatar
unknown committed
7759
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
unknown's avatar
unknown committed
7760 7761
};

7762 7763 7764 7765 7766 7767 7768
static void client_test_print_version(void)
{
  fprintf(stdout, "%s Distrib %s, for %s (%s)\n\n",
    my_progname,MYSQL_SERVER_VERSION,SYSTEM_TYPE,MACHINE_TYPE);
}


unknown's avatar
unknown committed
7769 7770 7771 7772 7773
static void usage(void)
{
  /*
   *  show the usage string when the user asks for this
  */    
7774
  putc('\n',stdout);
unknown's avatar
unknown committed
7775 7776 7777 7778 7779
  puts("***********************************************************************\n");
  puts("                Test for client-server protocol 4.1");
  puts("                        By Monty & Venu \n");
  puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,");
  puts("and you are welcome to modify and redistribute it under the GPL license\n");
unknown's avatar
unknown committed
7780
  puts("                 Copyright (C) 1995-2003 MySQL AB ");
unknown's avatar
unknown committed
7781
  puts("-----------------------------------------------------------------------\n");
7782 7783 7784 7785 7786 7787 7788
  client_test_print_version();
  fprintf(stdout,"Usage: %s [OPTIONS]\n\n", my_progname);  
  
  my_print_help(client_test_long_options);
  print_defaults("my", client_test_load_default_groups);
  my_print_variables(client_test_long_options);

7789
  puts("***********************************************************************\n");
unknown's avatar
unknown committed
7790 7791
}

unknown's avatar
unknown committed
7792 7793 7794
static my_bool
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
	       char *argument)
unknown's avatar
unknown committed
7795
{
unknown's avatar
unknown committed
7796 7797 7798 7799
  switch (optid) {
  case 'p':
    if (argument)
    {
unknown's avatar
unknown committed
7800
      char *start=argument;
unknown's avatar
unknown committed
7801 7802 7803
      my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR));
      opt_password= my_strdup(argument, MYF(MY_FAE));
      while (*argument) *argument++= 'x';		/* Destroy argument */
unknown's avatar
unknown committed
7804 7805
      if (*start)
        start[1]=0;
unknown's avatar
unknown committed
7806
    }
unknown's avatar
unknown committed
7807 7808 7809 7810 7811
    else
      tty_password= 1;
    break;
  case '?':
  case 'I':					/* Info */
unknown's avatar
unknown committed
7812
    usage();
unknown's avatar
unknown committed
7813
    exit(0);
unknown's avatar
unknown committed
7814
    break;
unknown's avatar
unknown committed
7815
  }
unknown's avatar
unknown committed
7816
  return 0;
unknown's avatar
unknown committed
7817 7818
}

unknown's avatar
unknown committed
7819 7820 7821
static void get_options(int argc, char **argv)
{
  int ho_error;
7822
  load_defaults("my",client_test_load_default_groups,&argc,&argv);
unknown's avatar
unknown committed
7823

7824 7825
  if ((ho_error= handle_options(&argc,&argv, client_test_long_options, 
                                get_one_option)))
unknown's avatar
unknown committed
7826 7827 7828 7829 7830 7831
    exit(ho_error);

  if (tty_password)
    opt_password=get_tty_password(NullS);
  return;
}
unknown's avatar
unknown committed
7832

7833 7834 7835 7836 7837 7838 7839 7840
/*
  Print the test output on successful execution before exiting
*/

static void print_test_output()
{
  fprintf(stdout,"\n\n");
  fprintf(stdout,"All '%d' tests were successful (in '%d' iterations)", 
7841
          test_count-1, opt_count);
7842 7843 7844 7845 7846 7847 7848
  fprintf(stdout,"\n  Total execution time: %g SECS", total_time);
  if (opt_count > 1)
    fprintf(stdout," (Avg: %g SECS)", total_time/opt_count);
  
  fprintf(stdout,"\n\n!!! SUCCESS !!!\n");
}

unknown's avatar
unknown committed
7849 7850 7851
/********************************************************
* main routine                                          *
*********************************************************/
unknown's avatar
unknown committed
7852
int main(int argc, char **argv)
unknown's avatar
unknown committed
7853
{  
unknown's avatar
unknown committed
7854
  MY_INIT(argv[0]);
7855 7856 7857

  load_defaults("my",client_test_load_default_groups,&argc,&argv);
  defaults_argv= argv;
unknown's avatar
unknown committed
7858
  get_options(argc,argv);
unknown's avatar
unknown committed
7859
    
unknown's avatar
unknown committed
7860
  client_connect();       /* connect to server */
unknown's avatar
unknown committed
7861
  
7862
  total_time= 0;
7863 7864 7865
  for (iter_count=1; iter_count <= opt_count; iter_count++) 
  {
    /* Start of tests */
7866 7867 7868
    test_count= 1;
   
    start_time= time((time_t *)0);
7869

unknown's avatar
unknown committed
7870 7871 7872 7873 7874
    client_query();         /* simple client query test */
#if NOT_YET_WORKING
    /* Used for internal new development debugging */
    test_drop_temp();       /* to test DROP TEMPORARY TABLE Access checks */
#endif
unknown's avatar
unknown committed
7875 7876
    test_fetch_seek();      /* to test stmt seek() functions */
    test_fetch_nobuffs();   /* to fecth without prior bound buffers */
7877
    test_open_direct();     /* direct execution in the middle of open stmts */
7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892
    test_fetch_null();      /* to fetch null data */
    test_fetch_date();      /* to fetch date,time and timestamp */
    test_fetch_str();       /* to fetch string to all types */
    test_fetch_long();      /* to fetch long to all types */
    test_fetch_short();     /* to fetch short to all types */
    test_fetch_tiny();      /* to fetch tiny to all types */
    test_fetch_bigint();    /* to fetch bigint to all types */
    test_fetch_float();     /* to fetch float to all types */
    test_fetch_double();    /* to fetch double to all types */
    test_bind_result_ext(); /* result bind test - extension */
    test_bind_result_ext1(); /* result bind test - extension */
    test_select_direct();   /* direct select - protocol_simple debug */
    test_select_prepare();  /* prepare select - protocol_prep debug */
    test_select();          /* simple select test */
    test_select_version();  /* select with variables */
7893
    test_select_simple();   /* simple select prepare */
7894
#if NOT_USED
7895 7896 7897 7898 7899 7900 7901
  /* 
     Enable this tests from 4.1.1 when mysql_param_result() is 
     supported 
  */
    test_select_meta();     /* select param meta information */
    test_update_meta();     /* update param meta information */  
    test_insert_meta();     /* insert param meta information */
7902
#endif
7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917
    test_func_fields();     /* test for new 4.1 MYSQL_FIELD members */
    test_long_data();       /* test for sending text data in chunks */
    test_insert();          /* simple insert test - prepare */
    test_set_variable();    /* prepare with set variables */
    test_select_show();     /* prepare - show test */
    test_prepare_noparam(); /* prepare without parameters */
    test_bind_result();     /* result bind test */   
    test_prepare_simple();  /* simple prepare */ 
    test_prepare();         /* prepare test */
    test_null();            /* test null data handling */
    test_debug_example();   /* some debugging case */
    test_update();          /* prepare-update test */
    test_simple_update();   /* simple prepare with update */
    test_simple_delete();   /* prepare with delete */
    test_double_compare();  /* float comparision */ 
unknown's avatar
unknown committed
7918 7919 7920
    client_query();         /* simple client query test */
    client_store_result();  /* usage of mysql_store_result() */
    client_use_result();    /* usage of mysql_use_result() */  
7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932
    test_tran_bdb();        /* transaction test on BDB table type */
    test_tran_innodb();     /* transaction test on InnoDB table type */ 
    test_prepare_ext();     /* test prepare with all types conversion -- TODO */
    test_prepare_syntax();  /* syntax check for prepares */
    test_field_names();     /* test for field names */
    test_field_flags();     /* test to help .NET provider team */
    test_long_data_str();   /* long data handling */
    test_long_data_str1();  /* yet another long data handling */
    test_long_data_bin();   /* long binary insertion */
    test_warnings();        /* show warnings test */
    test_errors();          /* show errors test */
    test_prepare_resultset();/* prepare meta info test */
7933
    test_stmt_close();      /* mysql_stmt_close() test -- hangs */
7934
    test_prepare_field_result(); /* prepare meta info */
7935
    test_multi_stmt();      /* multi stmt test */
7936
    test_multi_query();     /* test multi query execution */
7937 7938 7939
    test_store_result();    /* test the store_result */
    test_store_result1();   /* test store result without buffers */
    test_store_result2();   /* test store result for misc case */
7940 7941 7942 7943 7944 7945
    test_subselect();       /* test subselect prepare -TODO*/
    test_date();            /* test the MYSQL_TIME conversion */
    test_date_date();       /* test conversion from DATE to all */
    test_date_time();       /* test conversion from TIME to all */
    test_date_ts()  ;       /* test conversion from TIMESTAMP to all */
    test_date_dt()  ;       /* test conversion from DATETIME to all */
7946 7947 7948 7949
    test_prepare_alter();   /* change table schema in middle of prepare */
    test_manual_sample();   /* sample in the manual */
    test_pure_coverage();   /* keep pure coverage happy */
    test_buffers();         /* misc buffer handling */
7950
    test_ushort_bug();      /* test a simple conv bug from php */
7951 7952
    test_sshort_bug();      /* test a simple conv bug from php */
    test_stiny_bug();       /* test a simple conv bug from php */
7953
    test_field_misc();      /* check the field info for misc case, bug: #74 */   
7954 7955 7956
    test_set_option();      /* test the SET OPTION feature, bug #85 */
    test_prepare_grant();   /* to test the GRANT command, bug #89 */
    test_frm_bug();         /* test the crash when .frm is invalid, bug #93 */
7957 7958
    test_explain_bug();     /* test for the EXPLAIN, bug #115 */
    test_decimal_bug();     /* test for the decimal bug */
unknown's avatar
unknown committed
7959 7960 7961 7962 7963 7964 7965 7966
    test_nstmts();          /* test n statements */
    test_logs(); ;          /* to test logs */
    test_cuted_rows();      /* to test for WARNINGS from cuted rows */
    test_fetch_seek();      /* to test stmt seek() functions */
    test_fetch_nobuffs();   /* to fecth without prior bound buffers */
    test_open_direct();     /* direct execution in the middle of open stmts */
    test_fetch_offset();    /* to test mysql_fetch_column with offset */
    test_fetch_column();    /* to test mysql_fetch_column */
7967 7968 7969 7970 7971
    test_mem_overun();      /* test DBD ovverun bug */
    test_list_fields();     /* test COM_LIST_FIELDS for DEFAULT */
    test_free_result();     /* test mysql_stmt_free_result() */
    test_free_store_result(); /* test to make sure stmt results are cleared 
                                 during stmt_free_result() */
7972 7973 7974 7975 7976
    test_mem_overun();      /* memory ovverun bug */
    test_list_fields();     /* list_fields test */
    test_fetch_offset();    /* to test mysql_fetch_column with offset */
    test_fetch_column();    /* to test mysql_fetch_column */
    test_sqlmode();         /* test for SQL_MODE */
7977
    test_ts();              /* test for timestamp BR#819 */
7978
    test_bug1115();         /* BUG#1115 */
7979
    test_bug1180();         /* BUG#1180 */
7980 7981 7982

    end_time= time((time_t *)0);
    total_time+= difftime(end_time, start_time);
7983
    
7984 7985
    /* End of tests */
  }
unknown's avatar
unknown committed
7986 7987
  
  client_disconnect();    /* disconnect from server */
7988
  free_defaults(defaults_argv);
7989
  print_test_output();
7990
  
unknown's avatar
unknown committed
7991 7992
  return(0);
}
unknown's avatar
unknown committed
7993