client_test.c 158 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 75 76
/* 
  TODO: un comment this in 4.1.1 when it supports mysql_param_result 
  and also enable all meta_param_result tests 
*/
77 78 79 80 81
#ifndef mysql_param_result
#define mysql_param_result mysql_prepare_result
#endif

static void print_error(const char *msg)
unknown's avatar
unknown committed
82
{  
83
  if (mysql && mysql_errno(mysql))
unknown's avatar
unknown committed
84
  {
85
    if (mysql->server_version)
86
      fprintf(stdout,"\n [MySQL-%s]",mysql->server_version);
87
    else
88 89
      fprintf(stdout,"\n [MySQL]");
    fprintf(stdout,"[%d] %s\n",mysql_errno(mysql),mysql_error(mysql));
unknown's avatar
unknown committed
90
  }
unknown's avatar
unknown committed
91
  else if (msg) fprintf(stderr, " [MySQL] %s\n", msg);
unknown's avatar
unknown committed
92 93
}

94
static void print_st_error(MYSQL_STMT *stmt, const char *msg)
unknown's avatar
unknown committed
95
{  
96
  if (stmt && mysql_stmt_errno(stmt))
unknown's avatar
unknown committed
97
  {
98
    if (stmt->mysql && stmt->mysql->server_version)
99
      fprintf(stdout,"\n [MySQL-%s]",stmt->mysql->server_version);
100
    else
101
      fprintf(stdout,"\n [MySQL]");
102

103
    fprintf(stdout,"[%d] %s\n",mysql_stmt_errno(stmt),
104
            mysql_stmt_error(stmt));
unknown's avatar
unknown committed
105
  }
unknown's avatar
unknown committed
106
	else if (msg) fprintf(stderr, " [MySQL] %s\n", msg);
unknown's avatar
unknown committed
107
}
108
static void client_disconnect();
unknown's avatar
unknown committed
109

unknown's avatar
unknown committed
110
#define myerror(msg) print_error(msg)
unknown's avatar
unknown committed
111
#define mysterror(stmt, msg) print_st_error(stmt, msg)
unknown's avatar
unknown committed
112

unknown's avatar
unknown committed
113 114
#define myassert(exp) assert(exp)
#define myassert_r(exp) assert(!(exp))
unknown's avatar
unknown committed
115

unknown's avatar
unknown committed
116
#define myquery(r) \
unknown's avatar
unknown committed
117
{ \
unknown's avatar
unknown committed
118
if (r) \
unknown's avatar
unknown committed
119
  myerror(NULL); \
120
 myassert(r == 0); \
unknown's avatar
unknown committed
121 122
}

unknown's avatar
unknown committed
123
#define myquery_r(r) \
unknown's avatar
unknown committed
124
{ \
unknown's avatar
unknown committed
125
if (r) \
unknown's avatar
unknown committed
126
  myerror(NULL); \
127
myassert_r(r == 0); \
unknown's avatar
unknown committed
128 129 130 131
}

#define mystmt(stmt,r) \
{ \
unknown's avatar
unknown committed
132
if (r) \
unknown's avatar
unknown committed
133
  mysterror(stmt,NULL); \
134
myassert(r == 0);\
unknown's avatar
unknown committed
135 136
}

137
#define mystmt_r(stmt,r) \
unknown's avatar
unknown committed
138
{ \
unknown's avatar
unknown committed
139
if (r) \
140 141
  mysterror(stmt,NULL); \
myassert_r(r == 0);\
unknown's avatar
unknown committed
142 143
}

144
#define mystmt_init(stmt) \
unknown's avatar
unknown committed
145
{ \
unknown's avatar
unknown committed
146
if ( stmt == 0) \
unknown's avatar
unknown committed
147
  myerror(NULL); \
148 149
myassert(stmt != 0); \
}
unknown's avatar
unknown committed
150

151
#define mystmt_init_r(stmt) \
unknown's avatar
unknown committed
152
{ \
153 154
if (stmt == 0) \
  myerror(NULL);\
155 156
myassert(stmt == 0);\
} 
unknown's avatar
unknown committed
157

unknown's avatar
unknown committed
158 159
#define mytest(x) if (!x) {myerror(NULL);myassert(TRUE);}
#define mytest_r(x) if (x) {myerror(NULL);myassert(TRUE);}
unknown's avatar
unknown committed
160 161 162 163

/********************************************************
* connect to the server                                 *
*********************************************************/
unknown's avatar
unknown committed
164
static void client_connect()
unknown's avatar
unknown committed
165
{
unknown's avatar
unknown committed
166
  int  rc;
167
  myheader_r("client_connect");  
unknown's avatar
unknown committed
168

169
  fprintf(stdout, "\n Establishing a connection ...");
170
  
unknown's avatar
unknown committed
171
  if (!(mysql = mysql_init(NULL)))
unknown's avatar
unknown committed
172
  { 
unknown's avatar
unknown committed
173
	  myerror("mysql_init() failed");
unknown's avatar
unknown committed
174 175
    exit(0);
  }
176
  
unknown's avatar
unknown committed
177
  if (!(mysql_real_connect(mysql,opt_host,opt_user,
178 179
                           opt_password, opt_db ? opt_db:"test", opt_port,
                           opt_unix_socket, 0)))
unknown's avatar
unknown committed
180
  {
181 182
    myerror("connection failed");    
    mysql_close(mysql);
183
    fprintf(stdout,"\n Check the connection options using --help or -?\n");
unknown's avatar
unknown committed
184 185
    exit(0);
  }    
186
  
187
  fprintf(stdout," OK");
unknown's avatar
unknown committed
188 189

  /* set AUTOCOMMIT to ON*/
unknown's avatar
unknown committed
190
  mysql_autocommit(mysql, TRUE);
191
  
192
  fprintf(stdout, "\n Creating a test database '%s' ...", current_db);
193 194 195
  strxmov(query,"CREATE DATABASE IF NOT EXISTS ", current_db, NullS);
  
  rc = mysql_query(mysql, query);
unknown's avatar
unknown committed
196
  myquery(rc);
197 198 199
  
  strxmov(query,"USE ", current_db, NullS);
  rc = mysql_query(mysql, query);
unknown's avatar
unknown committed
200
  myquery(rc);
201 202
  
  fprintf(stdout," OK");
unknown's avatar
unknown committed
203 204 205 206 207
}

/********************************************************
* close the connection                                  *
*********************************************************/
208
static void client_disconnect()
unknown's avatar
unknown committed
209
{  
210
  myheader_r("client_disconnect");  
unknown's avatar
unknown committed
211

212 213
  if (mysql)
  {
214
    fprintf(stdout, "\n droping the test database '%s' ...", current_db);
215 216 217
    strxmov(query,"DROP DATABASE IF EXISTS ", current_db, NullS);
    
    mysql_query(mysql, query);
218
    fprintf(stdout, " OK");
219
    
220
    fprintf(stdout, "\n closing the connection ...");
221
    mysql_close(mysql);
222
    fprintf(stdout, " OK\n");
223
  }
unknown's avatar
unknown committed
224 225 226 227 228
}

/********************************************************
* query processing                                      *
*********************************************************/
229
static void client_query()
unknown's avatar
unknown committed
230 231 232 233 234 235
{
  int rc;

  myheader("client_query");

  rc = mysql_query(mysql,"DROP TABLE IF EXISTS myclient_test");
unknown's avatar
unknown committed
236
  myquery(rc);
unknown's avatar
unknown committed
237 238 239

  rc = mysql_query(mysql,"CREATE TABLE myclient_test(id int primary key auto_increment,\
                                              name varchar(20))");
unknown's avatar
unknown committed
240
  myquery(rc);
unknown's avatar
unknown committed
241 242
  
  rc = mysql_query(mysql,"CREATE TABLE myclient_test(id int, name varchar(20))");
unknown's avatar
unknown committed
243
  myquery_r(rc);
unknown's avatar
unknown committed
244 245
  
  rc = mysql_query(mysql,"INSERT INTO myclient_test(name) VALUES('mysql')");
unknown's avatar
unknown committed
246
  myquery(rc);
unknown's avatar
unknown committed
247 248
  
  rc = mysql_query(mysql,"INSERT INTO myclient_test(name) VALUES('monty')");
unknown's avatar
unknown committed
249 250
  myquery(rc);

unknown's avatar
unknown committed
251
  rc = mysql_query(mysql,"INSERT INTO myclient_test(name) VALUES('venu')");
unknown's avatar
unknown committed
252 253
  myquery(rc);

unknown's avatar
unknown committed
254
  rc = mysql_query(mysql,"INSERT INTO myclient_test(name) VALUES('deleted')");
unknown's avatar
unknown committed
255
  myquery(rc);
unknown's avatar
unknown committed
256

unknown's avatar
unknown committed
257 258 259
  rc = mysql_query(mysql,"INSERT INTO myclient_test(name) VALUES('deleted')");
  myquery(rc);

unknown's avatar
unknown committed
260
  rc = mysql_query(mysql,"UPDATE myclient_test SET name='updated' WHERE name='deleted'");
unknown's avatar
unknown committed
261 262
  myquery(rc);

unknown's avatar
unknown committed
263
  rc = mysql_query(mysql,"UPDATE myclient_test SET id=3 WHERE name='updated'");
unknown's avatar
unknown committed
264
  myquery_r(rc);
unknown's avatar
unknown committed
265 266 267 268 269
}

/********************************************************
* print dashes                                          *
*********************************************************/
270
static void my_print_dashes(MYSQL_RES *result)
unknown's avatar
unknown committed
271 272 273 274 275 276 277
{
  MYSQL_FIELD  *field;
  unsigned int i,j;

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

unknown's avatar
unknown committed
279 280 281 282
  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
283
      fputc('-',stdout);
unknown's avatar
unknown committed
284 285 286 287 288 289 290 291
    fputc('+',stdout);
  }
  fputc('\n',stdout);
}

/********************************************************
* print resultset metadata information                  *
*********************************************************/
292
static void my_print_result_metadata(MYSQL_RES *result)
unknown's avatar
unknown committed
293 294 295 296 297 298
{
  MYSQL_FIELD  *field;
  unsigned int i,j;
  unsigned int field_count;

  mysql_field_seek(result,0);
unknown's avatar
unknown committed
299
  fputc('\n', stdout);
300
  fputc('\n', stdout);
unknown's avatar
unknown committed
301 302 303 304 305 306

  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
307
    if (j < field->max_length)
unknown's avatar
unknown committed
308
      j = field->max_length;
unknown's avatar
unknown committed
309
    if (j < 4 && !IS_NOT_NULL(field->flags))
unknown's avatar
unknown committed
310 311 312 313 314 315
      j = 4;
    field->max_length = j;
  }
  my_print_dashes(result);
  fputc('\t',stdout);
  fputc('|', stdout);
unknown's avatar
unknown committed
316

unknown's avatar
unknown committed
317 318 319 320
  mysql_field_seek(result,0);
  for(i=0; i< field_count; i++)
  {
    field = mysql_fetch_field(result);
321
    fprintf(stdout, " %-*s |",(int) field->max_length, field->name);
unknown's avatar
unknown committed
322 323 324 325 326 327 328 329
  }
  fputc('\n', stdout);
  my_print_dashes(result);
}

/********************************************************
* process the result set                                *
*********************************************************/
unknown's avatar
unknown committed
330
int my_process_result_set(MYSQL_RES *result)
unknown's avatar
unknown committed
331 332 333 334 335
{
  MYSQL_ROW    row;
  MYSQL_FIELD  *field;
  unsigned int i;
  unsigned int row_count=0;
336 337 338
  
  if (!result)
    return 0;
unknown's avatar
unknown committed
339 340 341

  my_print_result_metadata(result);

unknown's avatar
unknown committed
342
  while ((row = mysql_fetch_row(result)) != NULL)
unknown's avatar
unknown committed
343
  {
unknown's avatar
unknown committed
344 345 346 347 348 349 350
    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
351
      if (row[i] == NULL)
352
        fprintf(stdout, " %-*s |", (int) field->max_length, "NULL");
unknown's avatar
unknown committed
353
      else if (IS_NUM(field->type))
354
        fprintf(stdout, " %*s |", (int) field->max_length, row[i]);
unknown's avatar
unknown committed
355
      else
356
        fprintf(stdout, " %-*s |", (int) field->max_length, row[i]);
unknown's avatar
unknown committed
357 358 359 360 361
    }
    fputc('\t',stdout);
    fputc('\n',stdout);
    row_count++;
  }
362 363
  if (row_count) 
    my_print_dashes(result);
unknown's avatar
unknown committed
364 365

  if (mysql_errno(mysql) != 0)
unknown's avatar
unknown committed
366
    fprintf(stderr, "\n\tmysql_fetch_row() failed\n");
unknown's avatar
unknown committed
367
  else
unknown's avatar
unknown committed
368 369 370
    fprintf(stdout,"\n\t%d %s returned\n", row_count, 
                   row_count == 1 ? "row" : "rows");
  return row_count;
unknown's avatar
unknown committed
371 372
}

373 374 375
/********************************************************
* process the stmt result set                           *
*********************************************************/
376 377 378
#define MAX_RES_FIELDS 50
#define MAX_FIELD_DATA_SIZE 255

379
uint my_process_stmt_result(MYSQL_STMT *stmt)
unknown's avatar
unknown committed
380
{
381 382
  int         field_count;
  uint        row_count= 0;
383
  MYSQL_BIND  buffer[MAX_RES_FIELDS];
384 385
  MYSQL_FIELD *field;
  MYSQL_RES   *result;
386 387 388
  char        data[MAX_RES_FIELDS][MAX_FIELD_DATA_SIZE];
  ulong       length[MAX_RES_FIELDS];
  my_bool     is_null[MAX_RES_FIELDS];
389 390
  int         rc, i;

391
  if (!(result= mysql_prepare_result(stmt))) /* No meta info */
392
  {
393 394 395
    while (!mysql_fetch(stmt)) 
      row_count++;
    return row_count;
396
  }
unknown's avatar
unknown committed
397
  
398
  field_count= min(mysql_num_fields(result), MAX_RES_FIELDS);
399 400 401
  for(i=0; i < field_count; i++)
  {
    buffer[i].buffer_type= MYSQL_TYPE_STRING;
402 403 404
    buffer[i].buffer_length= MAX_FIELD_DATA_SIZE;
    buffer[i].length= &length[i];
    buffer[i].buffer= (char*) data[i];
unknown's avatar
unknown committed
405
    buffer[i].is_null= &is_null[i];
406 407
  }
  my_print_result_metadata(result);
unknown's avatar
unknown committed
408

409 410
  rc= mysql_bind_result(stmt,buffer);
  mystmt(stmt,rc);
unknown's avatar
unknown committed
411

unknown's avatar
unknown committed
412 413 414
  rc= mysql_stmt_store_result(stmt);
  mystmt(stmt,rc);

415 416 417 418 419
  mysql_field_seek(result, 0);  
  while (mysql_fetch(stmt) == 0)
  {    
    fputc('\t',stdout);
    fputc('|',stdout);
unknown's avatar
unknown committed
420 421
    
    mysql_field_seek(result,0);
422 423 424
    for (i=0; i < field_count; i++)
    {
      field = mysql_fetch_field(result);
unknown's avatar
unknown committed
425
      if (is_null[i])
426
        fprintf(stdout, " %-*s |", (int) field->max_length, "NULL");
427
      else if (length[i] == 0)
428
      {
429
        data[i][0]='\0';  /* unmodified buffer */
430 431
        fprintf(stdout, " %*s |", (int) field->max_length, data[i]);
      }
432 433 434 435 436 437 438 439 440
      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++;
  }
441 442 443
  if (row_count)
    my_print_dashes(result);

unknown's avatar
unknown committed
444 445
  fprintf(stdout,"\n\t%d %s returned\n", row_count, 
                 row_count == 1 ? "row" : "rows");
446
  mysql_free_result(result);
unknown's avatar
unknown committed
447 448 449 450 451 452
  return row_count;
}

/********************************************************
* process the stmt result set                           *
*********************************************************/
453
uint my_stmt_result(const char *buff, unsigned long length)
unknown's avatar
unknown committed
454 455 456 457 458
{
  MYSQL_STMT *stmt;
  uint       row_count;
  int        rc;

459 460
  fprintf(stdout,"\n\n %s", buff);
  stmt= mysql_prepare(mysql,buff,length);
unknown's avatar
unknown committed
461
  mystmt_init(stmt);
unknown's avatar
unknown committed
462

unknown's avatar
unknown committed
463 464 465 466 467 468
  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  row_count= my_process_stmt_result(stmt);
  mysql_stmt_close(stmt);
    
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
  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)
  {
484
    strxmov(query,"SELECT ",col," FROM ",table," LIMIT 1", NullS);
485 486 487
    fprintf(stdout,"\n %s", query);
    rc = mysql_query(mysql, query);
    myquery(rc);
unknown's avatar
unknown committed
488

489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
    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, 
513 514
      const char *org_table, const char *db, 
      unsigned long length)
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
{
  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);
530 531 532
  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);
533 534 535 536 537 538 539
  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);
540
  myassert(field->length == length);
541 542 543 544 545 546 547 548
}

/*
  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);
549
  fprintf(stdout,"\n total parameters in stmt: `%ld` (expected: `%ld`)",
550 551
                  param_count, exp_count);
  myassert(param_count == exp_count);
unknown's avatar
unknown committed
552 553
}

554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
/*
  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);
}

587

unknown's avatar
unknown committed
588 589 590
/********************************************************
* store result processing                               *
*********************************************************/
591
static void client_store_result()
unknown's avatar
unknown committed
592 593 594
{
  MYSQL_RES *result;
  int       rc;
unknown's avatar
unknown committed
595

unknown's avatar
unknown committed
596 597 598
  myheader("client_store_result");

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

unknown's avatar
unknown committed
601 602
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
603
  mytest(result);
unknown's avatar
unknown committed
604

unknown's avatar
unknown committed
605
  my_process_result_set(result);
unknown's avatar
unknown committed
606 607 608 609
  mysql_free_result(result);
}

/********************************************************
610
* fetch the results
unknown's avatar
unknown committed
611
*********************************************************/
612
static void client_use_result()
unknown's avatar
unknown committed
613 614 615 616 617
{
  MYSQL_RES *result;
  int       rc;
  myheader("client_use_result");

unknown's avatar
unknown committed
618 619
  rc = mysql_query(mysql, "SELECT * FROM myclient_test");
  myquery(rc);
unknown's avatar
unknown committed
620 621 622

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

unknown's avatar
unknown committed
625
  my_process_result_set(result);
unknown's avatar
unknown committed
626 627 628
  mysql_free_result(result);
}

unknown's avatar
unknown committed
629 630 631 632 633 634 635 636 637 638
/*
  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
639
  if (!(l_mysql = mysql_init(NULL)))
unknown's avatar
unknown committed
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
  { 
    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
664 665 666 667

/********************************************************
* query processing                                      *
*********************************************************/
668
static void test_debug_example()
unknown's avatar
unknown committed
669 670 671 672 673 674 675
{
  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
676
  myquery(rc);
unknown's avatar
unknown committed
677 678 679

  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
680 681
  myquery(rc);

unknown's avatar
unknown committed
682
  rc = mysql_query(mysql,"INSERT INTO test_debug_example(name) VALUES('mysql')");
unknown's avatar
unknown committed
683
  myquery(rc);
unknown's avatar
unknown committed
684 685

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

688
  rc = mysql_query(mysql,"SELECT * FROM test_debug_example where name='mysql'");
unknown's avatar
unknown committed
689 690
  myquery(rc);

unknown's avatar
unknown committed
691
  result = mysql_use_result(mysql);
unknown's avatar
unknown committed
692
  mytest(result);
unknown's avatar
unknown committed
693

unknown's avatar
unknown committed
694
  my_process_result_set(result);
unknown's avatar
unknown committed
695 696 697
  mysql_free_result(result);

  rc = mysql_query(mysql,"DROP TABLE test_debug_example");
unknown's avatar
unknown committed
698
  myquery(rc);
unknown's avatar
unknown committed
699 700 701 702 703
}

/********************************************************
* to test autocommit feature                            *
*********************************************************/
704
static void test_tran_bdb()
unknown's avatar
unknown committed
705 706 707 708 709 710 711 712
{
  MYSQL_RES *result;
  MYSQL_ROW row;
  int       rc;

  myheader("test_tran_bdb");

  /* set AUTOCOMMIT to OFF */
unknown's avatar
unknown committed
713
  rc = mysql_autocommit(mysql, FALSE);
unknown's avatar
unknown committed
714 715 716 717 718
  myquery(rc);

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

unknown's avatar
unknown committed
719 720

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
721
  myquery(rc);
unknown's avatar
unknown committed
722 723 724

  /* 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
725
  myquery(rc);
unknown's avatar
unknown committed
726 727

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
728
  myquery(rc);
unknown's avatar
unknown committed
729 730 731

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

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
735
  myquery(rc);
unknown's avatar
unknown committed
736

737
  /* now insert the second row, and rollback the transaction */
unknown's avatar
unknown committed
738
  rc = mysql_query(mysql,"INSERT INTO my_demo_transaction VALUES(20,'mysql')");
unknown's avatar
unknown committed
739
  myquery(rc);
unknown's avatar
unknown committed
740 741

  rc = mysql_rollback(mysql);
unknown's avatar
unknown committed
742
  myquery(rc);
unknown's avatar
unknown committed
743 744 745

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

  rc = mysql_rollback(mysql);
unknown's avatar
unknown committed
749
  myquery(rc);
unknown's avatar
unknown committed
750 751 752

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

unknown's avatar
unknown committed
755 756
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
757
  mytest(result);
unknown's avatar
unknown committed
758

unknown's avatar
unknown committed
759
  my_process_result_set(result);
760
  mysql_free_result(result);
unknown's avatar
unknown committed
761 762 763

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

unknown's avatar
unknown committed
766 767
  /* get the result */
  result = mysql_use_result(mysql);
unknown's avatar
unknown committed
768 769
  mytest(result);

unknown's avatar
unknown committed
770
  row = mysql_fetch_row(result);
unknown's avatar
unknown committed
771 772
  mytest(row);

unknown's avatar
unknown committed
773
  row = mysql_fetch_row(result);
unknown's avatar
unknown committed
774
  mytest_r(row);
unknown's avatar
unknown committed
775

unknown's avatar
unknown committed
776
  mysql_free_result(result);
unknown's avatar
unknown committed
777
  mysql_autocommit(mysql,TRUE);
unknown's avatar
unknown committed
778 779 780 781 782
}

/********************************************************
* to test autocommit feature                            *
*********************************************************/
783
static void test_tran_innodb()
unknown's avatar
unknown committed
784 785 786 787 788 789 790 791
{
  MYSQL_RES *result;
  MYSQL_ROW row;
  int       rc;

  myheader("test_tran_innodb");

  /* set AUTOCOMMIT to OFF */
unknown's avatar
unknown committed
792
  rc = mysql_autocommit(mysql, FALSE);
unknown's avatar
unknown committed
793 794 795 796
  myquery(rc);

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

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

  /* 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
803
  myquery(rc);
unknown's avatar
unknown committed
804 805

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
806
  myquery(rc);
unknown's avatar
unknown committed
807 808 809

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

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
813
  myquery(rc);
unknown's avatar
unknown committed
814

815
  /* now insert the second row, and rollback the transaction */
unknown's avatar
unknown committed
816
  rc = mysql_query(mysql,"INSERT INTO my_demo_transaction VALUES(20,'mysql')");
unknown's avatar
unknown committed
817
  myquery(rc);
unknown's avatar
unknown committed
818 819

  rc = mysql_rollback(mysql);
unknown's avatar
unknown committed
820
  myquery(rc);
unknown's avatar
unknown committed
821 822 823

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

  rc = mysql_rollback(mysql);
unknown's avatar
unknown committed
827
  myquery(rc);
unknown's avatar
unknown committed
828 829 830

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

unknown's avatar
unknown committed
833 834
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
835
  mytest(result);
unknown's avatar
unknown committed
836

unknown's avatar
unknown committed
837
  my_process_result_set(result);
838
  mysql_free_result(result);
unknown's avatar
unknown committed
839 840 841

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

unknown's avatar
unknown committed
844 845
  /* get the result */
  result = mysql_use_result(mysql);
unknown's avatar
unknown committed
846 847
  mytest(result);

unknown's avatar
unknown committed
848
  row = mysql_fetch_row(result);
unknown's avatar
unknown committed
849 850
  mytest(row);

unknown's avatar
unknown committed
851
  row = mysql_fetch_row(result);
unknown's avatar
unknown committed
852
  mytest_r(row);
unknown's avatar
unknown committed
853 854

  mysql_free_result(result);
unknown's avatar
unknown committed
855
  mysql_autocommit(mysql,TRUE);
unknown's avatar
unknown committed
856 857
}

858

unknown's avatar
unknown committed
859
/********************************************************
860
 To test simple prepares of all DML statements
unknown's avatar
unknown committed
861
*********************************************************/
862

863
static void test_prepare_simple()
unknown's avatar
unknown committed
864 865
{
  MYSQL_STMT *stmt;
866
  int        rc;
unknown's avatar
unknown committed
867

unknown's avatar
unknown committed
868 869 870 871 872
  myheader("test_prepare_simple");

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

unknown's avatar
unknown committed
873
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
874
  myquery(rc);
unknown's avatar
unknown committed
875 876

  rc = mysql_query(mysql,"CREATE TABLE test_prepare_simple(id int, name varchar(50))");
unknown's avatar
unknown committed
877
  myquery(rc);
unknown's avatar
unknown committed
878 879

  /* alter table */
880 881
  strmov(query,"ALTER TABLE test_prepare_simple ADD new char(20)");
  stmt = mysql_prepare(mysql, query, 70);
882
  mystmt_init(stmt);
unknown's avatar
unknown committed
883

884
  verify_param_count(stmt,0);
unknown's avatar
unknown committed
885 886 887
  mysql_stmt_close(stmt);

  /* insert */
888 889
  strmov(query,"INSERT INTO test_prepare_simple VALUES(?,?)");
  stmt = mysql_prepare(mysql, query, 70);
890
  mystmt_init(stmt);
unknown's avatar
unknown committed
891

892
  verify_param_count(stmt,2);
unknown's avatar
unknown committed
893 894 895
  mysql_stmt_close(stmt);

  /* update */
896 897
  strmov(query,"UPDATE test_prepare_simple SET id=? WHERE id=? AND name= ?");
  stmt = mysql_prepare(mysql, query, 100);
898
  mystmt_init(stmt);
unknown's avatar
unknown committed
899

900
  verify_param_count(stmt,3);
unknown's avatar
unknown committed
901 902 903
  mysql_stmt_close(stmt);

  /* delete */
904 905
  strmov(query,"DELETE FROM test_prepare_simple WHERE id=10");
  stmt = mysql_prepare(mysql, query, 60);
906
  mystmt_init(stmt);
unknown's avatar
unknown committed
907

908
  verify_param_count(stmt,0);
unknown's avatar
unknown committed
909 910 911 912 913 914

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

  /* delete */
915 916
  strmov(query,"DELETE FROM test_prepare_simple WHERE id=?");
  stmt = mysql_prepare(mysql, query, 50);
917
  mystmt_init(stmt);
unknown's avatar
unknown committed
918

919
  verify_param_count(stmt,1);
unknown's avatar
unknown committed
920 921 922 923 924 925

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

  /* select */
926 927
  strmov(query,"SELECT * FROM test_prepare_simple WHERE id=? AND name= ?");
  stmt = mysql_prepare(mysql, query, 100);
928
  mystmt_init(stmt);
unknown's avatar
unknown committed
929

930
  verify_param_count(stmt,2);
unknown's avatar
unknown committed
931 932 933 934 935

  mysql_stmt_close(stmt);

  /* now fetch the results ..*/
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
936
  myquery(rc);
unknown's avatar
unknown committed
937 938 939 940 941 942
}


/********************************************************
* to test simple prepare field results                  *
*********************************************************/
943
static void test_prepare_field_result()
unknown's avatar
unknown committed
944 945
{
  MYSQL_STMT *stmt;
946
  MYSQL_RES  *result;
947
  int        rc;
unknown's avatar
unknown committed
948

unknown's avatar
unknown committed
949 950 951 952 953
  myheader("test_prepare_field_result");

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

unknown's avatar
unknown committed
954
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
955
  myquery(rc);
unknown's avatar
unknown committed
956

957 958 959
  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
960
  myquery(rc); 
unknown's avatar
unknown committed
961 962

  /* insert */
963
  strmov(query,"SELECT int_c,var_c,date_c as date,ts_c,char_c FROM \
964
                  test_prepare_field_result as t1 WHERE int_c=?");
965
  stmt = mysql_prepare(mysql, query, strlen(query));
966
  mystmt_init(stmt);
unknown's avatar
unknown committed
967

968
  verify_param_count(stmt,1);
unknown's avatar
unknown committed
969

970 971 972 973 974 975 976
  result = mysql_prepare_result(stmt);
  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,
977
                       "t1","test_prepare_field_result",current_db,11);
978
  verify_prepare_field(result,1,"var_c","var_c",MYSQL_TYPE_VAR_STRING,
979
                       "t1","test_prepare_field_result",current_db,50);
980
  verify_prepare_field(result,2,"date","date_c",MYSQL_TYPE_DATE,
981
                       "t1","test_prepare_field_result",current_db,10);
982
  verify_prepare_field(result,3,"ts_c","ts_c",MYSQL_TYPE_TIMESTAMP,
983
                       "t1","test_prepare_field_result",current_db,19);
984
  verify_prepare_field(result,4,"char_c","char_c",MYSQL_TYPE_STRING,
985
                       "t1","test_prepare_field_result",current_db,3);
986

987
  verify_field_count(result, 5);
988
  mysql_free_result(result);
989
  mysql_stmt_close(stmt);
unknown's avatar
unknown committed
990 991 992 993 994 995
}


/********************************************************
* to test simple prepare field results                  *
*********************************************************/
996
static void test_prepare_syntax()
unknown's avatar
unknown committed
997 998
{
  MYSQL_STMT *stmt;
999
  int        rc;
unknown's avatar
unknown committed
1000

unknown's avatar
unknown committed
1001 1002 1003 1004 1005
  myheader("test_prepare_syntax");

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

unknown's avatar
unknown committed
1006
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1007
  myquery(rc);
unknown's avatar
unknown committed
1008 1009

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

1012
  strmov(query,"INSERT INTO test_prepare_syntax VALUES(?");
1013
  stmt = mysql_prepare(mysql, query, strlen(query));
1014
  mystmt_init_r(stmt);
unknown's avatar
unknown committed
1015

1016
  strmov(query,"SELECT id,name FROM test_prepare_syntax WHERE id=? AND WHERE");
1017
  stmt = mysql_prepare(mysql, query, strlen(query));
1018
  mystmt_init_r(stmt);
unknown's avatar
unknown committed
1019 1020 1021

  /* now fetch the results ..*/
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1022
  myquery(rc);
unknown's avatar
unknown committed
1023 1024 1025 1026 1027 1028
}


/********************************************************
* to test simple prepare                                *
*********************************************************/
1029
static void test_prepare()
unknown's avatar
unknown committed
1030
{
unknown's avatar
unknown committed
1031
  MYSQL_STMT *stmt;
unknown's avatar
unknown committed
1032
  int        rc, i;
unknown's avatar
unknown committed
1033 1034 1035 1036 1037 1038 1039
  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
1040 1041
  ulong      length[7], len;
  my_bool    is_null[7];
unknown's avatar
unknown committed
1042
  MYSQL_BIND bind[7];
unknown's avatar
unknown committed
1043

unknown's avatar
unknown committed
1044
  myheader("test_prepare");
unknown's avatar
unknown committed
1045

unknown's avatar
unknown committed
1046
  rc = mysql_autocommit(mysql, TRUE);
unknown's avatar
unknown committed
1047 1048 1049 1050
  myquery(rc);

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

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1053
  myquery(rc);
unknown's avatar
unknown committed
1054 1055

  rc = mysql_query(mysql,"CREATE TABLE my_prepare(col1 tinyint,\
1056
                                col2 varchar(15), col3 int,\
unknown's avatar
unknown committed
1057 1058
                                col4 smallint, col5 bigint, \
                                col6 float, col7 double )");
unknown's avatar
unknown committed
1059
  myquery(rc);
unknown's avatar
unknown committed
1060 1061

  /* insert by prepare */
1062
  strxmov(query,"INSERT INTO my_prepare VALUES(?,?,?,?,?,?,?)",NullS);
1063
  stmt = mysql_prepare(mysql, query, strlen(query));
1064
  mystmt_init(stmt);
unknown's avatar
unknown committed
1065

1066
  verify_param_count(stmt,7);
unknown's avatar
unknown committed
1067 1068 1069

  /* tinyint */
  bind[0].buffer_type=FIELD_TYPE_TINY;
unknown's avatar
unknown committed
1070
  bind[0].buffer= (char *)&tiny_data;
unknown's avatar
unknown committed
1071 1072
  /* string */
  bind[1].buffer_type=FIELD_TYPE_STRING;
unknown's avatar
unknown committed
1073 1074
  bind[1].buffer= (char *)str_data;
  bind[1].buffer_length= 1000;			/* Max string length */
unknown's avatar
unknown committed
1075 1076
  /* integer */
  bind[2].buffer_type=FIELD_TYPE_LONG;
unknown's avatar
unknown committed
1077
  bind[2].buffer= (char *)&int_data;
unknown's avatar
unknown committed
1078 1079
  /* short */
  bind[3].buffer_type=FIELD_TYPE_SHORT;
unknown's avatar
unknown committed
1080
  bind[3].buffer= (char *)&small_data;
unknown's avatar
unknown committed
1081 1082
  /* bigint */
  bind[4].buffer_type=FIELD_TYPE_LONGLONG;
unknown's avatar
unknown committed
1083
  bind[4].buffer= (char *)&big_data;
unknown's avatar
unknown committed
1084
  /* float */
unknown's avatar
unknown committed
1085
  bind[5].buffer_type=FIELD_TYPE_FLOAT;
unknown's avatar
unknown committed
1086
  bind[5].buffer= (char *)&real_data;
unknown's avatar
unknown committed
1087 1088
  /* double */
  bind[6].buffer_type=FIELD_TYPE_DOUBLE;
unknown's avatar
unknown committed
1089 1090 1091 1092 1093 1094 1095 1096
  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
1097

unknown's avatar
unknown committed
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
  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
1110
    length[1]= my_sprintf(str_data,(str_data, "MySQL%d",int_data));
unknown's avatar
unknown committed
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
    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
1124
  myquery(rc);
unknown's avatar
unknown committed
1125 1126

  /* test the results now, only one row should exists */
1127
  myassert(tiny_data == (char) my_stmt_result("SELECT * FROM my_prepare",50));
unknown's avatar
unknown committed
1128 1129 1130 1131 1132 1133
      
  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
1134

unknown's avatar
unknown committed
1135
  /* get the result */
unknown's avatar
unknown committed
1136 1137 1138 1139 1140 1141 1142 1143
  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
1144

unknown's avatar
unknown committed
1145 1146 1147
  /* 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
1148
    len = my_sprintf(data, (data, "MySQL%d",o_int_data));
unknown's avatar
unknown committed
1149 1150 1151 1152
    
    rc = mysql_fetch(stmt);
    mystmt(stmt, rc);

1153 1154 1155 1156 1157 1158
    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
1159

1160 1161
    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
1162

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

    myassert(tiny_data == o_tiny_data);
unknown's avatar
unknown committed
1166
    myassert(is_null[0] == 0);
unknown's avatar
unknown committed
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
    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
1198 1199 1200

}

unknown's avatar
unknown committed
1201

unknown's avatar
unknown committed
1202 1203 1204
/********************************************************
* to test double comparision                            *
*********************************************************/
1205
static void test_double_compare()
unknown's avatar
unknown committed
1206
{
unknown's avatar
unknown committed
1207
  MYSQL_STMT *stmt;
1208
  int        rc;
1209
  char       real_data[10], tiny_data;
unknown's avatar
unknown committed
1210 1211
  double     double_data;
  MYSQL_RES  *result;
unknown's avatar
unknown committed
1212
  MYSQL_BIND bind[3];
unknown's avatar
unknown committed
1213
  ulong	     length[3];
unknown's avatar
unknown committed
1214

unknown's avatar
unknown committed
1215
  myheader("test_double_compare");
unknown's avatar
unknown committed
1216

unknown's avatar
unknown committed
1217
  rc = mysql_autocommit(mysql, TRUE);
unknown's avatar
unknown committed
1218 1219 1220 1221
  myquery(rc);

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

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1224
  myquery(rc);
unknown's avatar
unknown committed
1225 1226 1227

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

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

1233
  strmov(query, "UPDATE test_double_compare SET col1=100 WHERE col1 = ? AND col2 = ? AND COL3 = ?");
1234
  stmt = mysql_prepare(mysql,query, strlen(query));
1235
  mystmt_init(stmt);
unknown's avatar
unknown committed
1236

1237
  verify_param_count(stmt,3);
unknown's avatar
unknown committed
1238 1239 1240

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

unknown's avatar
unknown committed
1244 1245
  /* string->float */
  bind[1].buffer_type=FIELD_TYPE_STRING;
unknown's avatar
unknown committed
1246 1247 1248 1249 1250 1251
  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
1252 1253
  /* double */
  bind[2].buffer_type=FIELD_TYPE_DOUBLE;
unknown's avatar
unknown committed
1254 1255
  bind[2].buffer= (char *)&double_data;
  bind[2].is_null= 0;
unknown's avatar
unknown committed
1256

unknown's avatar
unknown committed
1257
  tiny_data = 1;
unknown's avatar
unknown committed
1258
  strmov(real_data,"10.2");
unknown's avatar
unknown committed
1259 1260 1261 1262 1263 1264 1265
  double_data = 34.5;
  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

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

1266
  verify_affected_rows(0);
unknown's avatar
unknown committed
1267

unknown's avatar
unknown committed
1268 1269 1270 1271
  mysql_stmt_close(stmt);

  /* now fetch the results ..*/
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1272
  myquery(rc);
unknown's avatar
unknown committed
1273 1274 1275

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

unknown's avatar
unknown committed
1278 1279
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
1280
  mytest(result);
unknown's avatar
unknown committed
1281

1282
  myassert((int)tiny_data == my_process_result_set(result));
unknown's avatar
unknown committed
1283 1284 1285
  mysql_free_result(result);
}

unknown's avatar
unknown committed
1286

unknown's avatar
unknown committed
1287 1288 1289
/********************************************************
* to test simple null                                   *
*********************************************************/
1290
static void test_null()
unknown's avatar
unknown committed
1291 1292
{
  MYSQL_STMT *stmt;
1293
  int        rc;
1294
  uint       nData;
unknown's avatar
unknown committed
1295
  MYSQL_BIND bind[2];
unknown's avatar
unknown committed
1296
  my_bool    is_null[2];
unknown's avatar
unknown committed
1297

unknown's avatar
unknown committed
1298
  myheader("test_null");
unknown's avatar
unknown committed
1299

unknown's avatar
unknown committed
1300 1301 1302
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_null");
  myquery(rc);

unknown's avatar
unknown committed
1303
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1304
  myquery(rc);
unknown's avatar
unknown committed
1305 1306

  rc = mysql_query(mysql,"CREATE TABLE test_null(col1 int,col2 varchar(50))");
unknown's avatar
unknown committed
1307
  myquery(rc);
unknown's avatar
unknown committed
1308 1309

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

unknown's avatar
unknown committed
1314
  strmov(query,"INSERT INTO test_null(col1,col2) VALUES(?,?)");
1315
  stmt = mysql_prepare(mysql, query, strlen(query));
1316
  mystmt_init(stmt);
unknown's avatar
unknown committed
1317

1318
  verify_param_count(stmt,2);
unknown's avatar
unknown committed
1319

unknown's avatar
unknown committed
1320 1321 1322
  bind[0].buffer_type=MYSQL_TYPE_LONG;
  bind[0].is_null= &is_null[0];
  is_null[0]= 1;
1323
  bind[1]=bind[0];		
unknown's avatar
unknown committed
1324

unknown's avatar
unknown committed
1325 1326 1327 1328
  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  /* now, execute the prepared statement to insert 10 records.. */
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
  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
1344 1345 1346 1347
  {
    rc = mysql_execute(stmt);
    mystmt(stmt, rc);
  }
1348
  
unknown's avatar
unknown committed
1349 1350 1351 1352
  mysql_stmt_close(stmt);

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

1355 1356
  nData*= 2;
  myassert(nData == my_stmt_result("SELECT * FROM test_null", 30));
unknown's avatar
unknown committed
1357

1358 1359 1360 1361 1362 1363 1364
  /* 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
1365

1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
  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
1386 1387 1388 1389
}


/********************************************************
1390
* to test fetch null                                    *
unknown's avatar
unknown committed
1391
*********************************************************/
1392
static void test_fetch_null()
unknown's avatar
unknown committed
1393 1394
{
  MYSQL_STMT *stmt;
1395
  int        rc;
unknown's avatar
unknown committed
1396
  int        i, nData;
1397
  MYSQL_BIND bind[11];
unknown's avatar
unknown committed
1398 1399
  ulong	     length[11];
  my_bool    is_null[11];
unknown's avatar
unknown committed
1400

1401
  myheader("test_fetch_null");
unknown's avatar
unknown committed
1402

1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
  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
1421
  rc = mysql_query(mysql,"INSERT INTO test_fetch_null(col11) VALUES(1000),(88),(389789)");
1422 1423 1424 1425 1426 1427
  myquery(rc);

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

  /* fetch */
unknown's avatar
unknown committed
1428
  for (i= 0; i < (int) array_elements(bind); i++)
1429 1430
  {
    bind[i].buffer_type=FIELD_TYPE_LONG;
unknown's avatar
unknown committed
1431 1432
    bind[i].is_null= &is_null[i];
    bind[i].length= &length[i];
1433
  }
unknown's avatar
unknown committed
1434
  bind[i-1].buffer=(char *)&nData;		/* Last column is not null */
1435

unknown's avatar
unknown committed
1436
  strmov((char *)query , "SELECT * FROM test_fetch_null");
unknown's avatar
unknown committed
1437 1438 1439 1440

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

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

1443 1444
  rc = mysql_bind_result(stmt,bind);
  mystmt(stmt, rc);
unknown's avatar
unknown committed
1445 1446 1447

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

unknown's avatar
unknown committed
1449 1450
  rc= 0;
  while (mysql_fetch(stmt) != MYSQL_NO_DATA)
1451
  {
unknown's avatar
unknown committed
1452 1453 1454 1455
    rc++;
    for (i=0; i < 10; i++)
    {
      fprintf(stdout, "\n data[%d] : %s", i, 
unknown's avatar
unknown committed
1456 1457
              is_null[i] ? "NULL" : "NOT NULL");
      myassert(is_null[i]);
unknown's avatar
unknown committed
1458 1459 1460
    }
    fprintf(stdout, "\n data[%d]: %d", i, nData);
    myassert(nData == 1000 || nData == 88 || nData == 389789);
unknown's avatar
unknown committed
1461
    myassert(is_null[i] == 0);
unknown's avatar
unknown committed
1462
    myassert(length[i] == 4);
1463
  }
unknown's avatar
unknown committed
1464
  myassert(rc == 3);
unknown's avatar
unknown committed
1465
  mysql_stmt_close(stmt);
1466
}
unknown's avatar
unknown committed
1467

unknown's avatar
unknown committed
1468

1469 1470 1471 1472 1473 1474 1475
/********************************************************
* to test simple select                                 *
*********************************************************/
static void test_select_version()
{
  MYSQL_STMT *stmt;
  int        rc;
unknown's avatar
unknown committed
1476

1477
  myheader("test_select_version");
unknown's avatar
unknown committed
1478

1479
  stmt = mysql_prepare(mysql, "SELECT @@version", 30);
1480 1481 1482 1483 1484 1485
  mystmt_init(stmt);

  verify_param_count(stmt,0);

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

1487 1488 1489
  my_process_stmt_result(stmt);
  mysql_stmt_close(stmt);
}
unknown's avatar
unknown committed
1490 1491 1492 1493

/********************************************************
* to test simple select                                 *
*********************************************************/
1494
static void test_select_simple()
unknown's avatar
unknown committed
1495 1496
{
  MYSQL_STMT *stmt;
1497
  int        rc;
unknown's avatar
unknown committed
1498

1499
  myheader("test_select_simple");
unknown's avatar
unknown committed
1500

1501
  stmt = mysql_prepare(mysql, "SHOW TABLES FROM mysql", 50);
1502
  mystmt_init(stmt);
unknown's avatar
unknown committed
1503

1504 1505 1506 1507 1508 1509 1510 1511 1512
  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
1513

1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
/********************************************************
* 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
1524
  rc = mysql_autocommit(mysql,TRUE);
unknown's avatar
unknown committed
1525 1526 1527 1528
  myquery(rc);

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

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1531
  myquery(rc);
unknown's avatar
unknown committed
1532

1533 1534 1535 1536
  rc = mysql_query(mysql,"CREATE TABLE test_select(id int, id1 tinyint, \
                                                   id2 float, \
                                                   id3 double, \
                                                   name varchar(50))");
unknown's avatar
unknown committed
1537 1538
  myquery(rc);

unknown's avatar
unknown committed
1539
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1540
  myquery(rc);
unknown's avatar
unknown committed
1541 1542

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

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1547
  myquery(rc);
unknown's avatar
unknown committed
1548

1549
  rc = mysql_query(mysql,"SELECT * FROM test_select");
unknown's avatar
unknown committed
1550
  myquery(rc);
unknown's avatar
unknown committed
1551 1552 1553

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

1556
  my_process_result_set(result);
unknown's avatar
unknown committed
1557 1558 1559
  mysql_free_result(result);
}

unknown's avatar
unknown committed
1560

unknown's avatar
unknown committed
1561
/********************************************************
1562
* to test simple select with prepare                    *
unknown's avatar
unknown committed
1563
*********************************************************/
1564
static void test_select_prepare()
unknown's avatar
unknown committed
1565
{
1566
  int        rc;
unknown's avatar
unknown committed
1567 1568
  MYSQL_STMT *stmt;

1569 1570
  myheader("test_select_prepare");
  
unknown's avatar
unknown committed
1571
  rc = mysql_autocommit(mysql,TRUE);
unknown's avatar
unknown committed
1572 1573
  myquery(rc);

1574
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_select");
unknown's avatar
unknown committed
1575
  myquery(rc);
unknown's avatar
unknown committed
1576 1577

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1578
  myquery(rc);
unknown's avatar
unknown committed
1579

1580
  rc = mysql_query(mysql,"CREATE TABLE test_select(id int, name varchar(50))");
unknown's avatar
unknown committed
1581 1582
  myquery(rc);

unknown's avatar
unknown committed
1583
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1584
  myquery(rc);
unknown's avatar
unknown committed
1585

1586 1587
  /* insert a row and commit the transaction */
  rc = mysql_query(mysql,"INSERT INTO test_select VALUES(10,'venu')");
unknown's avatar
unknown committed
1588 1589
  myquery(rc);

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

1593 1594 1595 1596 1597
  stmt = mysql_prepare(mysql,"SELECT * FROM test_select",50);
  mystmt_init(stmt);
  
  rc = mysql_execute(stmt);
  mystmt(stmt,rc);
unknown's avatar
unknown committed
1598

1599 1600
  myassert(1 == my_process_stmt_result(stmt));
  mysql_stmt_close(stmt);
unknown's avatar
unknown committed
1601

1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
  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);

1629
  myassert(1 == my_process_stmt_result(stmt));
unknown's avatar
unknown committed
1630
  mysql_stmt_close(stmt);
1631 1632
}

unknown's avatar
unknown committed
1633

1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
/********************************************************
* 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
1644
  ulong length[2];
1645 1646 1647

  myheader("test_select");

unknown's avatar
unknown committed
1648
  rc = mysql_autocommit(mysql,TRUE);
1649 1650 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
  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);

    /* now insert the second row, and rollback the transaction */
  rc = mysql_query(mysql,"INSERT INTO test_select VALUES(20,'mysql')");
  myquery(rc);

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

unknown's avatar
unknown committed
1677
  strmov(query,"SELECT * FROM test_select WHERE id=? AND name=?");
1678 1679 1680 1681 1682 1683 1684
  stmt = mysql_prepare(mysql, query, strlen(query));
  mystmt_init(stmt);

  verify_param_count(stmt,2);

  /* string data */
  nData=10;
1685
  strmov(szData,(char *)"venu");
1686
  bind[1].buffer_type=FIELD_TYPE_STRING;
1687
  bind[1].buffer=(char *)szData;
1688
  bind[1].buffer_length= 4;
unknown's avatar
unknown committed
1689 1690 1691 1692 1693
  bind[1].length= &length[1];
  length[1]= 4;
  bind[1].is_null=0;

  bind[0].buffer=(char *)&nData;
1694
  bind[0].buffer_type=FIELD_TYPE_LONG;
unknown's avatar
unknown committed
1695
  bind[0].is_null= 0;
1696 1697 1698 1699 1700 1701 1702

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

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

1703
  myassert(my_process_stmt_result(stmt) == 1);
1704 1705 1706 1707

  mysql_stmt_close(stmt);
}

unknown's avatar
unknown committed
1708

1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
/********************************************************
* to test simple select show                            *
*********************************************************/
static void test_select_show()
{
  MYSQL_STMT *stmt;
  int        rc;

  myheader("test_select_show");

unknown's avatar
unknown committed
1719
  mysql_autocommit(mysql,TRUE);
1720

1721 1722 1723 1724 1725 1726 1727
  rc = mysql_query(mysql, "DROP TABLE IF EXISTS test_show");
  myquery(rc);
  
  rc = mysql_query(mysql, "CREATE TABLE test_show(id int(4) NOT NULL, name char(2))");
  myquery(rc);

  stmt = mysql_prepare(mysql, "show columns from test_show", 30);
1728 1729 1730 1731 1732 1733 1734
  mystmt_init(stmt);

  verify_param_count(stmt,0);

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

unknown's avatar
unknown committed
1735
  my_process_stmt_result(stmt);
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755
  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);
1756

1757
  my_process_stmt_result(stmt);
1758 1759 1760
  mysql_stmt_close(stmt);
}

unknown's avatar
unknown committed
1761

1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
/********************************************************
* 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
1773
  ulong	     length[2];
1774 1775 1776

  myheader("test_simple_update");

unknown's avatar
unknown committed
1777
  rc = mysql_autocommit(mysql,TRUE);
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
  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);

1796
  verify_affected_rows(1);
1797 1798 1799 1800 1801

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

  /* insert by prepare */
unknown's avatar
unknown committed
1802
  strmov(query,"UPDATE test_update SET col2=? WHERE col1=?");
1803 1804 1805 1806 1807 1808 1809 1810
  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
1811 1812 1813 1814 1815 1816
  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
1817
  bind[1].buffer_type=FIELD_TYPE_LONG;
unknown's avatar
unknown committed
1818
  bind[1].is_null= 0;
unknown's avatar
unknown committed
1819

unknown's avatar
unknown committed
1820 1821 1822 1823 1824
  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);
1825
  verify_affected_rows(1);
unknown's avatar
unknown committed
1826 1827 1828 1829 1830

  mysql_stmt_close(stmt);

  /* now fetch the results ..*/
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1831
  myquery(rc);
unknown's avatar
unknown committed
1832 1833 1834

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

unknown's avatar
unknown committed
1837 1838
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
1839
  mytest(result);
unknown's avatar
unknown committed
1840

1841
  myassert(1 == my_process_result_set(result));
unknown's avatar
unknown committed
1842 1843 1844 1845 1846 1847 1848
  mysql_free_result(result);
}


/********************************************************
* to test simple long data handling                     *
*********************************************************/
1849
static void test_long_data()
unknown's avatar
unknown committed
1850 1851
{
  MYSQL_STMT *stmt;
1852
  int        rc, int_data;
unknown's avatar
unknown committed
1853 1854
  char       *data=NullS;
  MYSQL_RES  *result;
unknown's avatar
unknown committed
1855
  MYSQL_BIND bind[3];
unknown's avatar
unknown committed
1856

unknown's avatar
unknown committed
1857
  myheader("test_long_data");
unknown's avatar
unknown committed
1858

unknown's avatar
unknown committed
1859
  rc = mysql_autocommit(mysql,TRUE);
unknown's avatar
unknown committed
1860 1861 1862 1863
  myquery(rc);

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

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1866
  myquery(rc);
unknown's avatar
unknown committed
1867 1868 1869

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

unknown's avatar
unknown committed
1872
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1873 1874
  myquery(rc);

unknown's avatar
unknown committed
1875
  strmov(query,"INSERT INTO test_long_data(col1,col2) VALUES(?)");
unknown's avatar
unknown committed
1876
  stmt = mysql_prepare(mysql, query, strlen(query));
1877
  mystmt_init_r(stmt);
unknown's avatar
unknown committed
1878

unknown's avatar
unknown committed
1879
  strmov(query,"INSERT INTO test_long_data(col1,col2,col3) VALUES(?,?,?)");
1880
  stmt = mysql_prepare(mysql, query, strlen(query));
1881
  mystmt_init(stmt);
unknown's avatar
unknown committed
1882

1883
  verify_param_count(stmt,3);
unknown's avatar
unknown committed
1884

unknown's avatar
unknown committed
1885 1886
  bind[0].buffer=(char *)&int_data;
  bind[0].buffer_type=FIELD_TYPE_LONG;
unknown's avatar
unknown committed
1887
  bind[0].is_null=0;
unknown's avatar
unknown committed
1888 1889

  bind[1].buffer_type=FIELD_TYPE_STRING;
unknown's avatar
unknown committed
1890 1891 1892 1893
  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
1894
  bind[2]=bind[1];
unknown's avatar
unknown committed
1895 1896 1897
  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

1898
  int_data= 999;
unknown's avatar
unknown committed
1899
  data = (char *)"Michael";
unknown's avatar
unknown committed
1900 1901

  /* supply data in pieces */
unknown's avatar
unknown committed
1902 1903 1904
  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
1905
  mystmt(stmt, rc);
unknown's avatar
unknown committed
1906
  rc = mysql_send_long_data(stmt,2,"Venu (venu@mysql.com)",4);
unknown's avatar
unknown committed
1907 1908 1909 1910
  mystmt(stmt, rc);

  /* execute */
  rc = mysql_execute(stmt);
1911
  fprintf(stdout," mysql_execute() returned %d\n",rc);
unknown's avatar
unknown committed
1912 1913 1914
  mystmt(stmt,rc);

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1915
  myquery(rc);
unknown's avatar
unknown committed
1916 1917

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

unknown's avatar
unknown committed
1921 1922
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
1923
  mytest(result);
unknown's avatar
unknown committed
1924

1925
  myassert(1 == my_process_result_set(result));
unknown's avatar
unknown committed
1926
  mysql_free_result(result);
1927 1928

  verify_col_data("test_long_data","col1","999");
unknown's avatar
unknown committed
1929
  verify_col_data("test_long_data","col2","Michael 'Monty' Widenius");
1930
  verify_col_data("test_long_data","col3","Venu");
1931
  mysql_stmt_close(stmt);
unknown's avatar
unknown committed
1932 1933
}

unknown's avatar
unknown committed
1934

unknown's avatar
unknown committed
1935 1936 1937
/********************************************************
* to test long data (string) handling                   *
*********************************************************/
1938
static void test_long_data_str()
unknown's avatar
unknown committed
1939 1940
{
  MYSQL_STMT *stmt;
1941
  int        rc, i;
unknown's avatar
unknown committed
1942
  char       data[255];
1943
  long       length, length1;
unknown's avatar
unknown committed
1944
  MYSQL_RES  *result;
unknown's avatar
unknown committed
1945
  MYSQL_BIND bind[2];
unknown's avatar
unknown committed
1946
  my_bool    is_null[2];
unknown's avatar
unknown committed
1947

unknown's avatar
unknown committed
1948
  myheader("test_long_data_str");
unknown's avatar
unknown committed
1949

unknown's avatar
unknown committed
1950
  rc = mysql_autocommit(mysql,TRUE);
unknown's avatar
unknown committed
1951 1952 1953 1954
  myquery(rc);

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

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1957
  myquery(rc);
unknown's avatar
unknown committed
1958 1959

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

unknown's avatar
unknown committed
1962
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
1963
  myquery(rc);
unknown's avatar
unknown committed
1964

unknown's avatar
unknown committed
1965
  strmov(query,"INSERT INTO test_long_data_str VALUES(?,?)");
1966
  stmt = mysql_prepare(mysql, query, strlen(query));
1967
  mystmt_init(stmt);
unknown's avatar
unknown committed
1968

1969
  verify_param_count(stmt,2);
unknown's avatar
unknown committed
1970

unknown's avatar
unknown committed
1971
  bind[0].buffer = (char *)&length;
unknown's avatar
unknown committed
1972
  bind[0].buffer_type = FIELD_TYPE_LONG;
unknown's avatar
unknown committed
1973 1974 1975
  bind[0].is_null= &is_null[0];
  is_null[0]=0;
  length= 0;
unknown's avatar
unknown committed
1976

unknown's avatar
unknown committed
1977
  bind[1].buffer=data;				/* string data */
unknown's avatar
unknown committed
1978
  bind[1].buffer_type=FIELD_TYPE_STRING;
1979
  bind[1].length= &length1;
unknown's avatar
unknown committed
1980 1981 1982
  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
1983 1984 1985 1986
  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  length = 40;
1987
  strmov(data,"MySQL AB");
unknown's avatar
unknown committed
1988 1989

  /* supply data in pieces */
1990
  for(i=0; i < 4; i++)
unknown's avatar
unknown committed
1991
  {
unknown's avatar
unknown committed
1992
    rc = mysql_send_long_data(stmt,1,(char *)data,5);
1993
    mystmt(stmt, rc);
unknown's avatar
unknown committed
1994 1995 1996
  }
  /* execute */
  rc = mysql_execute(stmt);
1997
  fprintf(stdout," mysql_execute() returned %d\n",rc);
unknown's avatar
unknown committed
1998 1999 2000 2001 2002
  mystmt(stmt,rc);

  mysql_stmt_close(stmt);

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2003
  myquery(rc);
unknown's avatar
unknown committed
2004 2005 2006

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

unknown's avatar
unknown committed
2009 2010
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
2011
  mytest(result);
unknown's avatar
unknown committed
2012

2013
  myassert(1 == my_process_result_set(result));
unknown's avatar
unknown committed
2014
  mysql_free_result(result);
2015

2016
  my_sprintf(data,(data,"%d", i*5));
2017 2018 2019
  verify_col_data("test_long_data_str","LENGTH(longstr)", data);
  data[0]='\0';
  while (i--)
2020
   strxmov(data,data,"MySQL",NullS);
2021
  verify_col_data("test_long_data_str","longstr", data);
unknown's avatar
unknown committed
2022 2023 2024 2025 2026 2027
}


/********************************************************
* to test long data (string) handling                   *
*********************************************************/
2028
static void test_long_data_str1()
unknown's avatar
unknown committed
2029 2030
{
  MYSQL_STMT *stmt;
2031
  int        rc, i;
2032
  char       data[255];
2033
  long       length, length1;
unknown's avatar
unknown committed
2034
  MYSQL_RES  *result;
unknown's avatar
unknown committed
2035 2036 2037
  MYSQL_BIND bind[2];

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

unknown's avatar
unknown committed
2039
  rc = mysql_autocommit(mysql,TRUE);
unknown's avatar
unknown committed
2040 2041 2042 2043
  myquery(rc);

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

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2046
  myquery(rc);
unknown's avatar
unknown committed
2047 2048

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

unknown's avatar
unknown committed
2051
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2052
  myquery(rc);
unknown's avatar
unknown committed
2053

unknown's avatar
unknown committed
2054
  strmov(query,"INSERT INTO test_long_data_str VALUES(?,?)");
2055
  stmt = mysql_prepare(mysql, query, strlen(query));
2056
  mystmt_init(stmt);
unknown's avatar
unknown committed
2057

2058
  verify_param_count(stmt,2);
unknown's avatar
unknown committed
2059 2060

  bind[0].buffer=data;		  /* string data */
unknown's avatar
unknown committed
2061
  bind[0].buffer_length= sizeof(data);
2062
  bind[0].length= &length1;
unknown's avatar
unknown committed
2063
  bind[0].buffer_type=FIELD_TYPE_STRING;
unknown's avatar
unknown committed
2064 2065
  bind[0].is_null= 0;
  length1= 0;
unknown's avatar
unknown committed
2066 2067 2068 2069 2070 2071

  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
2072
  length = my_sprintf(data, (data, "MySQL AB"));
unknown's avatar
unknown committed
2073 2074

  /* supply data in pieces */
unknown's avatar
unknown committed
2075
  for (i=0; i < 3; i++)
unknown's avatar
unknown committed
2076
  {
unknown's avatar
unknown committed
2077
    rc = mysql_send_long_data(stmt,0,data,length);
2078
    mystmt(stmt, rc);
unknown's avatar
unknown committed
2079

unknown's avatar
unknown committed
2080
    rc = mysql_send_long_data(stmt,1,data,2);
2081
    mystmt(stmt, rc);
unknown's avatar
unknown committed
2082
  }
unknown's avatar
unknown committed
2083

unknown's avatar
unknown committed
2084 2085
  /* execute */
  rc = mysql_execute(stmt);
2086
  fprintf(stdout," mysql_execute() returned %d\n",rc);
unknown's avatar
unknown committed
2087 2088 2089 2090 2091
  mystmt(stmt,rc);

  mysql_stmt_close(stmt);

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

  /* 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
2096 2097
  myquery(rc);

unknown's avatar
unknown committed
2098 2099
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
2100
  mytest(result);
unknown's avatar
unknown committed
2101

2102
  myassert(1 == my_process_result_set(result));
unknown's avatar
unknown committed
2103
  mysql_free_result(result);
2104

2105
  my_sprintf(data,(data,"%ld",(long)i*length));
2106 2107
  verify_col_data("test_long_data_str","length(longstr)",data);

2108
  my_sprintf(data,(data,"%d",i*2));
2109
  verify_col_data("test_long_data_str","length(blb)",data);
unknown's avatar
unknown committed
2110 2111 2112 2113 2114 2115
}


/********************************************************
* to test long data (binary) handling                   *
*********************************************************/
2116
static void test_long_data_bin()
unknown's avatar
unknown committed
2117 2118
{
  MYSQL_STMT *stmt;
2119
  int        rc;
unknown's avatar
unknown committed
2120
  char       data[255];
unknown's avatar
unknown committed
2121
  long       length;
unknown's avatar
unknown committed
2122
  MYSQL_RES  *result;
unknown's avatar
unknown committed
2123 2124
  MYSQL_BIND bind[2];

unknown's avatar
unknown committed
2125

unknown's avatar
unknown committed
2126
  myheader("test_long_data_bin");
unknown's avatar
unknown committed
2127

unknown's avatar
unknown committed
2128
  rc = mysql_autocommit(mysql,TRUE);
unknown's avatar
unknown committed
2129 2130 2131 2132
  myquery(rc);

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

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

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

unknown's avatar
unknown committed
2140
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2141
  myquery(rc);
unknown's avatar
unknown committed
2142

unknown's avatar
unknown committed
2143
  strmov(query,"INSERT INTO test_long_data_bin VALUES(?,?)");
2144
  stmt = mysql_prepare(mysql, query, strlen(query));
2145
  mystmt_init(stmt);
unknown's avatar
unknown committed
2146

2147
  verify_param_count(stmt,2);
unknown's avatar
unknown committed
2148

unknown's avatar
unknown committed
2149
  bind[0].buffer = (char *)&length;
unknown's avatar
unknown committed
2150
  bind[0].buffer_type = FIELD_TYPE_LONG;
unknown's avatar
unknown committed
2151 2152
  bind[0].is_null= 0;
  length= 0;
unknown's avatar
unknown committed
2153

unknown's avatar
unknown committed
2154
  bind[1].buffer=data;		 /* string data */
unknown's avatar
unknown committed
2155
  bind[1].buffer_type=FIELD_TYPE_LONG_BLOB;
unknown's avatar
unknown committed
2156 2157
  bind[1].length= 0;		/* Will not be used */
  bind[1].is_null= 0;
unknown's avatar
unknown committed
2158 2159 2160 2161
  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

  length = 10;
2162
  strmov(data,"MySQL AB");
unknown's avatar
unknown committed
2163 2164 2165 2166

  /* supply data in pieces */
  {
    int i;
unknown's avatar
unknown committed
2167
    for (i=0; i < 100; i++)
unknown's avatar
unknown committed
2168
    {
unknown's avatar
unknown committed
2169
      rc = mysql_send_long_data(stmt,1,(char *)data,4);
unknown's avatar
unknown committed
2170 2171 2172 2173 2174
      mystmt(stmt, rc);
    }
  }
  /* execute */
  rc = mysql_execute(stmt);
2175
  fprintf(stdout," mysql_execute() returned %d\n",rc);
unknown's avatar
unknown committed
2176 2177 2178 2179 2180
  mystmt(stmt,rc);

  mysql_stmt_close(stmt);

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

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

unknown's avatar
unknown committed
2187 2188
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
2189
  mytest(result);
unknown's avatar
unknown committed
2190

2191
  myassert(1 == my_process_result_set(result));
unknown's avatar
unknown committed
2192 2193 2194 2195 2196 2197 2198
  mysql_free_result(result);
}


/********************************************************
* to test simple delete                                 *
*********************************************************/
2199
static void test_simple_delete()
unknown's avatar
unknown committed
2200 2201
{
  MYSQL_STMT *stmt;
2202
  int        rc;
unknown's avatar
unknown committed
2203 2204 2205
  char       szData[30]={0};
  int        nData=1;
  MYSQL_RES  *result;
unknown's avatar
unknown committed
2206
  MYSQL_BIND bind[2];
unknown's avatar
unknown committed
2207
  ulong length[2];
unknown's avatar
unknown committed
2208 2209

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

unknown's avatar
unknown committed
2211
  rc = mysql_autocommit(mysql,TRUE);
unknown's avatar
unknown committed
2212 2213 2214 2215
  myquery(rc);

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

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2218
  myquery(rc);
unknown's avatar
unknown committed
2219 2220 2221

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

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

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

2230
  verify_affected_rows(1);
unknown's avatar
unknown committed
2231 2232

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2233
  myquery(rc);
unknown's avatar
unknown committed
2234 2235

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

2240
  verify_param_count(stmt,2);
unknown's avatar
unknown committed
2241 2242

  nData=1;
unknown's avatar
unknown committed
2243
  strmov(szData,"MySQL");
unknown's avatar
unknown committed
2244
  bind[1].buffer_type=FIELD_TYPE_STRING;
unknown's avatar
unknown committed
2245 2246 2247
  bind[1].buffer= szData;		/* string data */
  bind[1].buffer_length=sizeof(szData);
  bind[1].length= &length[1];
2248
  bind[1].is_null= 0;
unknown's avatar
unknown committed
2249 2250 2251
  length[1]= 5;

  bind[0].buffer=(char *)&nData;
unknown's avatar
unknown committed
2252
  bind[0].buffer_type=FIELD_TYPE_LONG;
unknown's avatar
unknown committed
2253
  bind[0].is_null= 0;
unknown's avatar
unknown committed
2254

unknown's avatar
unknown committed
2255 2256 2257 2258 2259
  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

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

  verify_affected_rows(1);
unknown's avatar
unknown committed
2262 2263 2264 2265 2266

  mysql_stmt_close(stmt);

  /* now fetch the results ..*/
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2267
  myquery(rc);
unknown's avatar
unknown committed
2268 2269 2270

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

unknown's avatar
unknown committed
2273 2274
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
2275
  mytest(result);
unknown's avatar
unknown committed
2276

2277
  myassert(0 == my_process_result_set(result));
unknown's avatar
unknown committed
2278 2279 2280 2281 2282 2283 2284 2285
  mysql_free_result(result);
}



/********************************************************
* to test simple update                                 *
*********************************************************/
2286
static void test_update()
unknown's avatar
unknown committed
2287 2288
{
  MYSQL_STMT *stmt;
2289
  int        rc;
unknown's avatar
unknown committed
2290
  char       szData[25];
unknown's avatar
unknown committed
2291 2292
  int        nData=1;
  MYSQL_RES  *result;
unknown's avatar
unknown committed
2293
  MYSQL_BIND bind[2];
unknown's avatar
unknown committed
2294
  ulong length[2];
unknown's avatar
unknown committed
2295

unknown's avatar
unknown committed
2296
  myheader("test_update");
unknown's avatar
unknown committed
2297

unknown's avatar
unknown committed
2298
  rc = mysql_autocommit(mysql,TRUE);
unknown's avatar
unknown committed
2299 2300 2301 2302
  myquery(rc);

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

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2305
  myquery(rc);
unknown's avatar
unknown committed
2306 2307 2308

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

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

unknown's avatar
unknown committed
2314
  strmov(query,"INSERT INTO test_update(col2,col3) VALUES(?,?)");
2315
  stmt = mysql_prepare(mysql, query, strlen(query));
2316
  mystmt_init(stmt);
unknown's avatar
unknown committed
2317

2318
  verify_param_count(stmt,2);
unknown's avatar
unknown committed
2319 2320 2321 2322

  /* string data */
  bind[0].buffer_type=FIELD_TYPE_STRING;
  bind[0].buffer=szData;
unknown's avatar
unknown committed
2323 2324 2325 2326 2327 2328
  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
2329
  bind[1].buffer_type=FIELD_TYPE_LONG;
unknown's avatar
unknown committed
2330
  bind[1].is_null= 0;
unknown's avatar
unknown committed
2331

unknown's avatar
unknown committed
2332 2333 2334 2335 2336 2337
  rc = mysql_bind_param(stmt,bind);
  mystmt(stmt, rc);

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

2339
  verify_affected_rows(1);
unknown's avatar
unknown committed
2340 2341
  mysql_stmt_close(stmt);

unknown's avatar
unknown committed
2342
  strmov(query,"UPDATE test_update SET col2=? WHERE col3=?");
2343
  stmt = mysql_prepare(mysql, query, strlen(query));
2344
  mystmt_init(stmt);
unknown's avatar
unknown committed
2345

2346
  verify_param_count(stmt,2);
unknown's avatar
unknown committed
2347 2348
  nData=100;

unknown's avatar
unknown committed
2349 2350
  bind[0].buffer_type=FIELD_TYPE_STRING;
  bind[0].buffer=szData;
unknown's avatar
unknown committed
2351 2352 2353 2354
  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
2355
  bind[1].buffer_type=FIELD_TYPE_LONG;
unknown's avatar
unknown committed
2356
  bind[1].is_null= 0;
unknown's avatar
unknown committed
2357 2358 2359 2360 2361 2362

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

  rc = mysql_execute(stmt);
  mystmt(stmt, rc);
2363
  verify_affected_rows(1);
unknown's avatar
unknown committed
2364 2365 2366 2367 2368

  mysql_stmt_close(stmt);

  /* now fetch the results ..*/
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2369
  myquery(rc);
unknown's avatar
unknown committed
2370 2371 2372

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

unknown's avatar
unknown committed
2375 2376
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
2377
  mytest(result);
unknown's avatar
unknown committed
2378

2379
  myassert(1 == my_process_result_set(result));
unknown's avatar
unknown committed
2380 2381 2382 2383 2384 2385 2386
  mysql_free_result(result);
}


/********************************************************
* to test simple prepare                                *
*********************************************************/
2387
static void test_prepare_noparam()
unknown's avatar
unknown committed
2388 2389
{
  MYSQL_STMT *stmt;
2390
  int        rc;
unknown's avatar
unknown committed
2391 2392
  MYSQL_RES  *result;

2393
  myheader("test_prepare_noparam");
unknown's avatar
unknown committed
2394 2395 2396 2397

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

unknown's avatar
unknown committed
2398
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2399
  myquery(rc);
unknown's avatar
unknown committed
2400 2401

  rc = mysql_query(mysql,"CREATE TABLE my_prepare(col1 int ,col2 varchar(50))");
unknown's avatar
unknown committed
2402 2403 2404
  myquery(rc);


unknown's avatar
unknown committed
2405
  /* insert by prepare */
unknown's avatar
unknown committed
2406
  strmov(query,"INSERT INTO my_prepare VALUES(10,'venu')");
2407
  stmt = mysql_prepare(mysql, query, strlen(query));
2408
  mystmt_init(stmt);
unknown's avatar
unknown committed
2409

2410
  verify_param_count(stmt,0);
unknown's avatar
unknown committed
2411 2412 2413 2414 2415 2416 2417 2418

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

  mysql_stmt_close(stmt);

  /* now fetch the results ..*/
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2419
  myquery(rc);
unknown's avatar
unknown committed
2420 2421 2422

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

unknown's avatar
unknown committed
2425 2426
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
2427
  mytest(result);
unknown's avatar
unknown committed
2428

2429
  myassert(1 == my_process_result_set(result));
unknown's avatar
unknown committed
2430 2431 2432 2433 2434 2435 2436
  mysql_free_result(result);
}


/********************************************************
* to test simple bind result                            *
*********************************************************/
2437
static void test_bind_result()
unknown's avatar
unknown committed
2438 2439 2440
{
  MYSQL_STMT *stmt;
  int        rc;
unknown's avatar
unknown committed
2441 2442
  int        nData;
  ulong	     length, length1;
unknown's avatar
unknown committed
2443
  char       szData[100];
unknown's avatar
unknown committed
2444
  MYSQL_BIND bind[2];
unknown's avatar
unknown committed
2445
  my_bool    is_null[2];
unknown's avatar
unknown committed
2446

unknown's avatar
unknown committed
2447
  myheader("test_bind_result");
unknown's avatar
unknown committed
2448

unknown's avatar
unknown committed
2449 2450 2451
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_bind_result");
  myquery(rc);

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

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

unknown's avatar
unknown committed
2458
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2459
  myquery(rc);
unknown's avatar
unknown committed
2460 2461

  rc = mysql_query(mysql,"INSERT INTO test_bind_result VALUES(10,'venu')");
unknown's avatar
unknown committed
2462
  myquery(rc);
unknown's avatar
unknown committed
2463 2464

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

2467 2468 2469
  rc = mysql_query(mysql,"INSERT INTO test_bind_result(col2) VALUES('monty')");
  myquery(rc);

unknown's avatar
unknown committed
2470
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2471
  myquery(rc);
unknown's avatar
unknown committed
2472

unknown's avatar
unknown committed
2473
  /* fetch */
unknown's avatar
unknown committed
2474 2475

  bind[0].buffer_type=FIELD_TYPE_LONG;
unknown's avatar
unknown committed
2476 2477 2478 2479
  bind[0].buffer= (char *) &nData;	/* integer data */
  bind[0].is_null= &is_null[0];
  bind[0].length= 0;

unknown's avatar
unknown committed
2480 2481
  bind[1].buffer_type=FIELD_TYPE_STRING;
  bind[1].buffer=szData;		/* string data */
unknown's avatar
unknown committed
2482
  bind[1].buffer_length=sizeof(szData);
unknown's avatar
unknown committed
2483 2484
  bind[1].length= &length1;
  bind[1].is_null= &is_null[1];
unknown's avatar
unknown committed
2485

2486
  stmt = mysql_prepare(mysql, "SELECT * FROM test_bind_result", 50);
2487
  mystmt_init(stmt);
unknown's avatar
unknown committed
2488

unknown's avatar
unknown committed
2489
  rc = mysql_bind_result(stmt,bind);
unknown's avatar
unknown committed
2490
  mystmt(stmt, rc);
unknown's avatar
unknown committed
2491 2492

  rc = mysql_execute(stmt);
unknown's avatar
unknown committed
2493
  mystmt(stmt, rc);
unknown's avatar
unknown committed
2494 2495 2496 2497

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

unknown's avatar
unknown committed
2498
  fprintf(stdout,"\n row 1: %d,%s(%lu)",nData, szData, length1);
2499 2500 2501
  myassert(nData == 10);
  myassert(strcmp(szData,"venu")==0);
  myassert(length1 == 4);
unknown's avatar
unknown committed
2502 2503 2504 2505

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

unknown's avatar
unknown committed
2506
  fprintf(stdout,"\n row 2: %d,%s(%lu)",nData, szData, length1);
2507 2508 2509 2510 2511 2512 2513 2514
  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
2515 2516 2517
  if (is_null[0])
    fprintf(stdout,"\n row 3: NULL,%s(%lu)", szData, length1);
  myassert(is_null[0]);
2518 2519
  myassert(strcmp(szData,"monty")==0);
  myassert(length1 == 5);
unknown's avatar
unknown committed
2520 2521

  rc = mysql_fetch(stmt);
2522
  myassert(rc == MYSQL_NO_DATA);
unknown's avatar
unknown committed
2523 2524 2525 2526

  mysql_stmt_close(stmt);
}

2527

unknown's avatar
unknown committed
2528
/********************************************************
2529
* to test ext bind result                               *
unknown's avatar
unknown committed
2530
*********************************************************/
2531
static void test_bind_result_ext()
unknown's avatar
unknown committed
2532 2533
{
  MYSQL_STMT *stmt;
unknown's avatar
unknown committed
2534
  int        rc, i;
2535 2536 2537 2538 2539 2540 2541
  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
2542
  ulong       szLength, bLength;
2543
  MYSQL_BIND bind[8];
unknown's avatar
unknown committed
2544 2545
  long	     length[8];
  my_bool    is_null[8];
unknown's avatar
unknown committed
2546

2547
  myheader("test_bind_result_ext");
unknown's avatar
unknown committed
2548

2549 2550
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_bind_result");
  myquery(rc);
unknown's avatar
unknown committed
2551

2552 2553 2554 2555 2556 2557 2558 2559
  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
2560 2561
  myquery(rc);

unknown's avatar
unknown committed
2562
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
2563
  myquery(rc);
unknown's avatar
unknown committed
2564

2565 2566 2567 2568 2569 2570 2571 2572
  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
2573 2574 2575 2576 2577 2578
  for (i= 0; i < (int) array_elements(bind); i++)
  {
    bind[i].length=  &length[i];
    bind[i].is_null= &is_null[i];
  }

2579
  bind[0].buffer_type=MYSQL_TYPE_TINY;
unknown's avatar
unknown committed
2580
  bind[0].buffer=(char *)&t_data;
2581 2582 2583 2584 2585

  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
2586
  bind[1].buffer=(char *)&s_data;
2587
  
unknown's avatar
unknown committed
2588 2589
  bind[2].buffer=(char *)&i_data;
  bind[3].buffer=(char *)&b_data;
2590 2591

  bind[4].buffer_type=MYSQL_TYPE_FLOAT;
unknown's avatar
unknown committed
2592
  bind[4].buffer=(char *)&f_data;
2593 2594

  bind[5].buffer_type=MYSQL_TYPE_DOUBLE;
unknown's avatar
unknown committed
2595
  bind[5].buffer=(char *)&d_data;
2596 2597

  bind[6].buffer_type=MYSQL_TYPE_STRING;
2598
  bind[6].buffer= (char *)szData;
unknown's avatar
unknown committed
2599 2600
  bind[6].buffer_length= sizeof(szData);
  bind[6].length= &szLength;
2601 2602

  bind[7].buffer_type=MYSQL_TYPE_TINY_BLOB;
unknown's avatar
unknown committed
2603 2604 2605
  bind[7].buffer=(char *)&bData;
  bind[7].length= &bLength;
  bind[7].buffer_length= sizeof(bData);
2606

2607
  stmt = mysql_prepare(mysql, "select * from test_bind_result", 50);
2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626
  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
2627 2628
  fprintf(stdout, "\n data (str)    : %s(%lu)", szData, szLength);
  fprintf(stdout, "\n data (bin)    : %s(%lu)", bData, bLength);
2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654


  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);
  myassert(strcmp(bData,"mysql")==0);
  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
2655
  uint	     i;
2656 2657 2658 2659
  int        rc;
  char       t_data[20];
  float      s_data;
  short      i_data;
2660
  uchar      b_data;
2661 2662 2663 2664 2665
  int        f_data;
  long       bData;
  char       d_data[20];
  double     szData;
  MYSQL_BIND bind[8];
unknown's avatar
unknown committed
2666 2667
  ulong      length[8];
  my_bool    is_null[8];
2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694
  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
2695 2696
  bind[0].buffer=(char *) t_data;
  bind[0].buffer_length= sizeof(t_data);
2697 2698

  bind[1].buffer_type=MYSQL_TYPE_FLOAT;
unknown's avatar
unknown committed
2699
  bind[1].buffer=(char *)&s_data;
2700 2701

  bind[2].buffer_type=MYSQL_TYPE_SHORT;
unknown's avatar
unknown committed
2702
  bind[2].buffer=(char *)&i_data;
2703 2704

  bind[3].buffer_type=MYSQL_TYPE_TINY;
unknown's avatar
unknown committed
2705
  bind[3].buffer=(char *)&b_data;
2706 2707

  bind[4].buffer_type=MYSQL_TYPE_LONG;
unknown's avatar
unknown committed
2708
  bind[4].buffer=(char *)&f_data;
2709 2710

  bind[5].buffer_type=MYSQL_TYPE_STRING;
unknown's avatar
unknown committed
2711
  bind[5].buffer=(char *)d_data;
2712 2713

  bind[6].buffer_type=MYSQL_TYPE_LONG;
unknown's avatar
unknown committed
2714
  bind[6].buffer=(char *)&bData;
2715 2716

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

unknown's avatar
unknown committed
2719 2720 2721 2722 2723 2724
  for (i= 0; i < array_elements(bind); i++)
  {
    bind[i].is_null= &is_null[i];
    bind[i].length= &length[i];
  }

2725
  stmt = mysql_prepare(mysql, "select * from test_bind_result", 50);
2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736
  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
2737 2738 2739 2740
  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]);
2741

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

unknown's avatar
unknown committed
2745 2746
  fprintf(stdout, "\n data (bin)    : %ld(%lu)", bData, length[6]);
  fprintf(stdout, "\n data (str)    : %g(%lu)", szData, length[7]);
2747 2748 2749 2750 2751

  myassert(strcmp(t_data,"120")==0);
  myassert(i_data == 3999);
  myassert(f_data == 2);
  myassert(strcmp(d_data,"58.89")==0);
2752
  myassert(b_data == 54);
2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768

  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);
}

2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901
/*
 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);
}

2902 2903 2904 2905 2906 2907
/********************************************************
* to test fetching of date, time and ts                 *
*********************************************************/
static void test_fetch_date()
{
  MYSQL_STMT *stmt;
unknown's avatar
unknown committed
2908
  uint	     i;
2909 2910
  int        rc, year;
  char       date[25], time[25], ts[25], ts_4[15], ts_6[20], dt[20];
unknown's avatar
unknown committed
2911
  ulong      d_length, t_length, ts_length, ts4_length, ts6_length, 
2912
             dt_length, y_length;
unknown's avatar
unknown committed
2913 2914 2915
  MYSQL_BIND bind[8];
  my_bool    is_null[8];
  ulong	     length[8];
2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946

  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
2947 2948 2949 2950 2951 2952
  for (i= 0; i < array_elements(bind); i++)
  {
    bind[i].is_null= &is_null[i];
    bind[i].length= &length[i];
  }

2953 2954 2955
  bind[0].buffer_type=MYSQL_TYPE_STRING;
  bind[1]=bind[2]=bind[0];

unknown's avatar
unknown committed
2956 2957 2958
  bind[0].buffer=(char *)&date;
  bind[0].buffer_length= sizeof(date);
  bind[0].length= &d_length;
2959

unknown's avatar
unknown committed
2960 2961 2962
  bind[1].buffer=(char *)&time;
  bind[1].buffer_length= sizeof(time);
  bind[1].length= &t_length;
2963

unknown's avatar
unknown committed
2964 2965 2966
  bind[2].buffer=(char *)&ts;
  bind[2].buffer_length= sizeof(ts);
  bind[2].length= &ts_length;
2967 2968

  bind[3].buffer_type=MYSQL_TYPE_LONG;
unknown's avatar
unknown committed
2969 2970
  bind[3].buffer=(char *)&year;
  bind[3].length= &y_length;
2971 2972

  bind[4].buffer_type=MYSQL_TYPE_STRING;
unknown's avatar
unknown committed
2973 2974 2975
  bind[4].buffer=(char *)&dt;
  bind[4].buffer_length= sizeof(dt);
  bind[4].length= &dt_length;
2976 2977

  bind[5].buffer_type=MYSQL_TYPE_STRING;
unknown's avatar
unknown committed
2978 2979 2980
  bind[5].buffer=(char *)&ts_4;
  bind[5].buffer_length= sizeof(ts_4);
  bind[5].length= &ts4_length;
2981 2982

  bind[6].buffer_type=MYSQL_TYPE_STRING;
unknown's avatar
unknown committed
2983 2984 2985
  bind[6].buffer=(char *)&ts_6;
  bind[6].buffer_length= sizeof(ts_6);
  bind[6].length= &ts6_length;
2986

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

2989 2990 2991 2992 2993 2994 2995 2996
  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
2997
  
2998 2999 3000 3001
  ts_4[0]='\0';
  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);

unknown's avatar
unknown committed
3002 3003 3004 3005 3006 3007 3008
  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);
3009 3010 3011 3012 3013

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

  myassert(strcmp(time,"12:49:00")==0);
unknown's avatar
unknown committed
3014
  myassert(t_length == 8);
3015 3016 3017 3018 3019 3020 3021

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

  myassert(year == 2010);
  myassert(y_length == 4);
  
3022 3023
  myassert(strcmp(dt,"2010-07-10 00:00:00")==0);
  myassert(dt_length == 19);
3024 3025 3026 3027

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

3028 3029
  myassert(strcmp(ts_6,"1999-12-29 00:00:00")==0);
  myassert(ts6_length == 19);
3030 3031 3032 3033 3034 3035 3036

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

  mysql_stmt_close(stmt);
}

unknown's avatar
unknown committed
3037 3038 3039 3040 3041
/********************************************************
* to test fetching of str to all types                  *
*********************************************************/
static void test_fetch_str()
{
3042
  int rc;
unknown's avatar
unknown committed
3043 3044 3045

  myheader("test_fetch_str");

3046
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_bind_fetch");
unknown's avatar
unknown committed
3047 3048 3049 3050 3051
  myquery(rc);

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

3052
  rc = mysql_query(mysql,"CREATE TABLE test_bind_fetch(c1 char(10),\
unknown's avatar
unknown committed
3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063
                                                     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);

3064
  bind_fetch(3);
unknown's avatar
unknown committed
3065 3066 3067 3068 3069 3070 3071
}

/********************************************************
* to test fetching of long to all types                 *
*********************************************************/
static void test_fetch_long()
{
3072
  int rc;
unknown's avatar
unknown committed
3073 3074 3075

  myheader("test_fetch_long");

3076
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_bind_fetch");
unknown's avatar
unknown committed
3077 3078 3079 3080 3081
  myquery(rc);

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

3082
  rc = mysql_query(mysql,"CREATE TABLE test_bind_fetch(c1 int unsigned,\
unknown's avatar
unknown committed
3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093
                                                     c2 int unsigned,\
                                                     c3 int,\
                                                     c4 int,\
                                                     c5 int,\
                                                     c6 int unsigned,\
                                                     c7 int)");
  myquery(rc);

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

3094
  bind_fetch(4);
unknown's avatar
unknown committed
3095 3096
}

unknown's avatar
unknown committed
3097

unknown's avatar
unknown committed
3098 3099 3100 3101 3102
/********************************************************
* to test fetching of short to all types                 *
*********************************************************/
static void test_fetch_short()
{
3103
  int rc;
unknown's avatar
unknown committed
3104 3105 3106

  myheader("test_fetch_short");

3107
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_bind_fetch");
unknown's avatar
unknown committed
3108 3109 3110 3111 3112
  myquery(rc);

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

3113
  rc = mysql_query(mysql,"CREATE TABLE test_bind_fetch(c1 smallint unsigned,\
unknown's avatar
unknown committed
3114 3115 3116 3117 3118 3119 3120 3121 3122 3123
                                                     c2 smallint,\
                                                     c3 smallint unsigned,\
                                                     c4 smallint,\
                                                     c5 smallint,\
                                                     c6 smallint,\
                                                     c7 smallint unsigned)");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);
3124 3125 3126
  
  bind_fetch(5);
}
unknown's avatar
unknown committed
3127

unknown's avatar
unknown committed
3128

unknown's avatar
unknown committed
3129 3130 3131 3132 3133
/********************************************************
* to test fetching of tiny to all types                 *
*********************************************************/
static void test_fetch_tiny()
{
3134
  int rc;
unknown's avatar
unknown committed
3135 3136 3137

  myheader("test_fetch_tiny");

3138
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_bind_fetch");
unknown's avatar
unknown committed
3139 3140 3141 3142 3143
  myquery(rc);

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

3144
  rc = mysql_query(mysql,"CREATE TABLE test_bind_fetch(c1 tinyint unsigned,\
unknown's avatar
unknown committed
3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155
                                                     c2 tinyint,\
                                                     c3 tinyint unsigned,\
                                                     c4 tinyint,\
                                                     c5 tinyint,\
                                                     c6 tinyint,\
                                                     c7 tinyint unsigned)");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);
  
3156
  bind_fetch(3);
unknown's avatar
unknown committed
3157 3158 3159

}

unknown's avatar
unknown committed
3160

unknown's avatar
unknown committed
3161 3162 3163 3164 3165
/********************************************************
* to test fetching of longlong to all types             *
*********************************************************/
static void test_fetch_bigint()
{
3166
  int rc;
unknown's avatar
unknown committed
3167 3168 3169

  myheader("test_fetch_bigint");

3170
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_bind_fetch");
unknown's avatar
unknown committed
3171 3172 3173 3174 3175
  myquery(rc);

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

3176
  rc = mysql_query(mysql,"CREATE TABLE test_bind_fetch(c1 bigint,\
unknown's avatar
unknown committed
3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187
                                                     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);
  
3188
  bind_fetch(2);
unknown's avatar
unknown committed
3189 3190 3191

}

unknown's avatar
unknown committed
3192

unknown's avatar
unknown committed
3193 3194 3195 3196 3197
/********************************************************
* to test fetching of float to all types                 *
*********************************************************/
static void test_fetch_float()
{
3198
  int rc;
unknown's avatar
unknown committed
3199 3200 3201

  myheader("test_fetch_float");

3202
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_bind_fetch");
unknown's avatar
unknown committed
3203 3204 3205 3206 3207
  myquery(rc);

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

3208
  rc = mysql_query(mysql,"CREATE TABLE test_bind_fetch(c1 float(3),\
unknown's avatar
unknown committed
3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219
                                                     c2 float,\
                                                     c3 float unsigned,\
                                                     c4 float,\
                                                     c5 float,\
                                                     c6 float,\
                                                     c7 float(10) unsigned)");
  myquery(rc);

  rc = mysql_commit(mysql);
  myquery(rc);
  
3220
  bind_fetch(2);
unknown's avatar
unknown committed
3221 3222 3223 3224 3225 3226 3227 3228

}

/********************************************************
* to test fetching of double to all types               *
*********************************************************/
static void test_fetch_double()
{
3229
  int rc;
unknown's avatar
unknown committed
3230 3231 3232

  myheader("test_fetch_double");

3233
  rc = mysql_query(mysql,"DROP TABLE IF EXISTS test_bind_fetch");
unknown's avatar
unknown committed
3234 3235 3236 3237 3238
  myquery(rc);

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

3239
  rc = mysql_query(mysql,"CREATE TABLE test_bind_fetch(c1 double(5,2),\
unknown's avatar
unknown committed
3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250
                                                     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);
  
3251
  bind_fetch(3);
unknown's avatar
unknown committed
3252 3253

}
3254 3255 3256 3257 3258 3259 3260

/********************************************************
* to test simple prepare with all possible types        *
*********************************************************/
static void test_prepare_ext()
{
  MYSQL_STMT *stmt;
unknown's avatar
unknown committed
3261
  uint	     i;
3262 3263 3264 3265 3266 3267
  int        rc;
  char       *sql;
  int        nData=1;
  char       tData=1;
  short      sData=10;
  longlong   bData=20;
unknown's avatar
unknown committed
3268
  MYSQL_BIND bind[6];
3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302
  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
3303 3304 3305 3306 3307 3308 3309 3310 3311
			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
3312 3313
  rc = mysql_query(mysql,sql);
  myquery(rc);
unknown's avatar
unknown committed
3314 3315

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

3320
  verify_param_count(stmt,6);
unknown's avatar
unknown committed
3321 3322

  /*tinyint*/
unknown's avatar
unknown committed
3323 3324
  bind[0].buffer_type=FIELD_TYPE_TINY;
  bind[0].buffer= (char *)&tData;
unknown's avatar
unknown committed
3325 3326

  /*smallint*/
unknown's avatar
unknown committed
3327 3328
  bind[1].buffer_type=FIELD_TYPE_SHORT;
  bind[1].buffer= (char *)&sData;
unknown's avatar
unknown committed
3329 3330

  /*mediumint*/
unknown's avatar
unknown committed
3331 3332
  bind[2].buffer_type=FIELD_TYPE_LONG;
  bind[2].buffer= (char *)&nData;
unknown's avatar
unknown committed
3333 3334

  /*int*/
unknown's avatar
unknown committed
3335 3336
  bind[3].buffer_type=FIELD_TYPE_LONG;
  bind[3].buffer= (char *)&nData;
unknown's avatar
unknown committed
3337 3338

  /*integer*/
unknown's avatar
unknown committed
3339 3340
  bind[4].buffer_type=FIELD_TYPE_LONG;
  bind[4].buffer= (char *)&nData;
unknown's avatar
unknown committed
3341 3342

  /*bigint*/
unknown's avatar
unknown committed
3343 3344
  bind[5].buffer_type=FIELD_TYPE_LONGLONG;
  bind[5].buffer= (char *)&bData;
unknown's avatar
unknown committed
3345

unknown's avatar
unknown committed
3346 3347 3348 3349
  for (i= 0; i < array_elements(bind); i++)
    bind[i].is_null=0;

  rc = mysql_bind_param(stmt,bind);
unknown's avatar
unknown committed
3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363
  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
3364
  myquery(rc);
unknown's avatar
unknown committed
3365

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

unknown's avatar
unknown committed
3369
  /* get the result */
3370 3371
  rc = mysql_execute(stmt);
  mystmt(stmt, rc);
unknown's avatar
unknown committed
3372

3373 3374 3375
  myassert(nData == (int)my_process_stmt_result(stmt));

  mysql_stmt_close(stmt);
unknown's avatar
unknown committed
3376 3377 3378 3379 3380 3381 3382
}



/********************************************************
* to test real and alias names                          *
*********************************************************/
3383
static void test_field_names()
unknown's avatar
unknown committed
3384 3385 3386
{
  int        rc;
  MYSQL_RES  *result;
unknown's avatar
unknown committed
3387 3388

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

3390
  fprintf(stdout,"\n %d,%d,%d",MYSQL_TYPE_DECIMAL,MYSQL_TYPE_NEWDATE,MYSQL_TYPE_ENUM);
unknown's avatar
unknown committed
3391 3392 3393 3394 3395
  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
3396 3397

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
3398
  myquery(rc);
unknown's avatar
unknown committed
3399 3400

  rc = mysql_query(mysql,"CREATE TABLE test_field_names1(id int,name varchar(50))");
unknown's avatar
unknown committed
3401
  myquery(rc);
unknown's avatar
unknown committed
3402 3403

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

unknown's avatar
unknown committed
3406
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
3407 3408
  myquery(rc);

unknown's avatar
unknown committed
3409
  /* with table name included with TRUE column name */
unknown's avatar
unknown committed
3410
  rc = mysql_query(mysql,"SELECT id as 'id-alias' FROM test_field_names1");
unknown's avatar
unknown committed
3411 3412
  myquery(rc);

unknown's avatar
unknown committed
3413
  result = mysql_use_result(mysql);
unknown's avatar
unknown committed
3414
  mytest(result);
unknown's avatar
unknown committed
3415

3416
  myassert(0 == my_process_result_set(result));
unknown's avatar
unknown committed
3417
  mysql_free_result(result);
unknown's avatar
unknown committed
3418

unknown's avatar
unknown committed
3419
  /* with table name included with TRUE column name */
unknown's avatar
unknown committed
3420
  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
3421 3422
  myquery(rc);

unknown's avatar
unknown committed
3423
  result = mysql_use_result(mysql);
unknown's avatar
unknown committed
3424
  mytest(result);
unknown's avatar
unknown committed
3425

3426
  myassert(0 == my_process_result_set(result));
unknown's avatar
unknown committed
3427 3428 3429 3430 3431 3432
  mysql_free_result(result);
}

/********************************************************
* to test warnings                                      *
*********************************************************/
3433
static void test_warnings()
unknown's avatar
unknown committed
3434 3435 3436 3437
{
  int        rc;
  MYSQL_RES  *result;

unknown's avatar
unknown committed
3438 3439
  myheader("test_warnings");

3440 3441 3442 3443 3444 3445
  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
3446 3447 3448
  rc = mysql_query(mysql,"SHOW WARNINGS");
  myquery(rc);

3449
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
3450
  mytest(result);
unknown's avatar
unknown committed
3451

3452
  myassert(1 == my_process_result_set(result));
unknown's avatar
unknown committed
3453 3454 3455 3456 3457 3458
  mysql_free_result(result);
}

/********************************************************
* to test errors                                        *
*********************************************************/
3459
static void test_errors()
unknown's avatar
unknown committed
3460 3461 3462
{
  int        rc;
  MYSQL_RES  *result;
unknown's avatar
unknown committed
3463 3464 3465

  myheader("test_errors");

3466 3467 3468 3469 3470
  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
3471 3472 3473
  rc = mysql_query(mysql,"SHOW ERRORS");
  myquery(rc);

3474
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
3475
  mytest(result);
unknown's avatar
unknown committed
3476

unknown's avatar
unknown committed
3477
  my_process_result_set(result);
unknown's avatar
unknown committed
3478 3479 3480 3481 3482 3483 3484 3485
  mysql_free_result(result);
}



/********************************************************
* to test simple prepare-insert                         *
*********************************************************/
3486
static void test_insert()
unknown's avatar
unknown committed
3487
{
unknown's avatar
unknown committed
3488
  MYSQL_STMT *stmt;
unknown's avatar
unknown committed
3489
  int        rc;
unknown's avatar
unknown committed
3490 3491 3492
  char       str_data[50];
  char       tiny_data;
  MYSQL_RES  *result;
unknown's avatar
unknown committed
3493
  MYSQL_BIND bind[2];
3494
  ulong	     length;
unknown's avatar
unknown committed
3495

unknown's avatar
unknown committed
3496
  myheader("test_insert");
unknown's avatar
unknown committed
3497

unknown's avatar
unknown committed
3498
  rc = mysql_autocommit(mysql, TRUE);
unknown's avatar
unknown committed
3499 3500 3501 3502
  myquery(rc);

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

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
3505
  myquery(rc);
unknown's avatar
unknown committed
3506 3507 3508

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

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

3515
  verify_param_count(stmt,2);
unknown's avatar
unknown committed
3516 3517 3518

  /* tinyint */
  bind[0].buffer_type=FIELD_TYPE_TINY;
unknown's avatar
unknown committed
3519 3520
  bind[0].buffer=(char *)&tiny_data;
  bind[0].is_null= 0;
3521
  bind[0].length= 0;
unknown's avatar
unknown committed
3522

unknown's avatar
unknown committed
3523 3524 3525
  /* string */
  bind[1].buffer_type=FIELD_TYPE_STRING;
  bind[1].buffer=str_data;
unknown's avatar
unknown committed
3526 3527
  bind[1].buffer_length=sizeof(str_data);;
  bind[1].is_null= 0;
3528
  bind[1].length= &length;
unknown's avatar
unknown committed
3529

unknown's avatar
unknown committed
3530 3531 3532 3533 3534 3535
  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++)
  {
3536
    length = my_sprintf(str_data, (str_data, "MySQL%d",tiny_data));
unknown's avatar
unknown committed
3537 3538 3539 3540 3541 3542 3543 3544
    rc = mysql_execute(stmt);
    mystmt(stmt, rc);
  }

  mysql_stmt_close(stmt);

  /* now fetch the results ..*/
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
3545
  myquery(rc);
unknown's avatar
unknown committed
3546 3547 3548

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

unknown's avatar
unknown committed
3551 3552
  /* get the result */
  result = mysql_store_result(mysql);
unknown's avatar
unknown committed
3553
  mytest(result);
unknown's avatar
unknown committed
3554

3555
  myassert((int)tiny_data == my_process_result_set(result));
unknown's avatar
unknown committed
3556 3557 3558 3559 3560 3561 3562
  mysql_free_result(result);

}

/********************************************************
* to test simple prepare-resultset info                 *
*********************************************************/
3563
static void test_prepare_resultset()
unknown's avatar
unknown committed
3564
{
unknown's avatar
unknown committed
3565
  MYSQL_STMT *stmt;
3566
  int        rc;
unknown's avatar
unknown committed
3567 3568
  MYSQL_RES  *result;

unknown's avatar
unknown committed
3569
  myheader("test_prepare_resultset");
unknown's avatar
unknown committed
3570

unknown's avatar
unknown committed
3571
  rc = mysql_autocommit(mysql, TRUE);
unknown's avatar
unknown committed
3572 3573 3574 3575
  myquery(rc);

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

  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
3578
  myquery(rc);
unknown's avatar
unknown committed
3579 3580 3581

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

3584
  stmt = mysql_prepare(mysql, "SELECT * FROM test_prepare_resultset", 60);
3585
  mystmt_init(stmt);
unknown's avatar
unknown committed
3586

3587
  verify_param_count(stmt,0);
unknown's avatar
unknown committed
3588 3589

  result = mysql_prepare_result(stmt);
unknown's avatar
unknown committed
3590 3591
  mytest(result);
  my_print_result_metadata(result);
unknown's avatar
unknown committed
3592 3593 3594 3595 3596 3597 3598
  mysql_stmt_close(stmt);
}

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

3599
static void test_field_flags()
unknown's avatar
unknown committed
3600 3601 3602 3603 3604 3605 3606
{
  int          rc;
  MYSQL_RES    *result;
  MYSQL_FIELD  *field;
  unsigned int i;


unknown's avatar
unknown committed
3607 3608 3609 3610 3611
  myheader("test_field_flags");

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

unknown's avatar
unknown committed
3612
  rc = mysql_commit(mysql);
unknown's avatar
unknown committed
3613
  myquery(rc);
unknown's avatar
unknown committed
3614 3615 3616 3617 3618 3619 3620 3621

  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
3622
  myquery(rc);
unknown's avatar
unknown committed
3623 3624

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

unknown's avatar
unknown committed
3627
  /* with table name included with TRUE column name */
unknown's avatar
unknown committed
3628
  rc = mysql_query(mysql,"SELECT * FROM test_field_flags");
unknown's avatar
unknown committed
3629 3630
  myquery(rc);

unknown's avatar
unknown committed
3631
  result = mysql_use_result(mysql);
unknown's avatar
unknown committed
3632
  mytest(result);
unknown's avatar
unknown committed
3633 3634 3635 3636 3637 3638 3639

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

  for(i=0; i< mysql_num_fields(result); i++)
  {
    field = mysql_fetch_field(result);
3640
    fprintf(stdout,"\n field:%d",i);
unknown's avatar
unknown committed
3641
    if (field->flags & NOT_NULL_FLAG)
3642
      fprintf(stdout,"\n  NOT_NULL_FLAG");
unknown's avatar
unknown committed
3643
    if (field->flags & PRI_KEY_FLAG)
3644
      fprintf(stdout,"\n  PRI_KEY_FLAG");
unknown's avatar
unknown committed
3645
    if (field->flags & UNIQUE_KEY_FLAG)
3646
      fprintf(stdout,"\n  UNIQUE_KEY_FLAG");
unknown's avatar
unknown committed
3647
    if (field->flags & MULTIPLE_KEY_FLAG)
3648
      fprintf(stdout,"\n  MULTIPLE_KEY_FLAG");
unknown's avatar
unknown committed
3649
    if (field->flags & AUTO_INCREMENT_FLAG)
3650 3651 3652 3653 3654
      fprintf(stdout,"\n  AUTO_INCREMENT_FLAG");

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

3656 3657 3658 3659 3660 3661 3662
/**************************************************************
 * 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
3663
  MYSQL_BIND  bind[1];
3664 3665 3666 3667 3668 3669
  MYSQL_RES   *result;
  unsigned int  count;
  int   rc;
  
  myheader("test_stmt_close");  

3670
  fprintf(stdout, "\n Establishing a test connection ...");
unknown's avatar
unknown committed
3671
  if (!(lmysql = mysql_init(NULL)))
3672 3673 3674
  { 
	  myerror("mysql_init() failed");
    exit(0);
unknown's avatar
unknown committed
3675
  }
3676
  if (!(mysql_real_connect(lmysql,opt_host,opt_user,
3677
			   opt_password, current_db, opt_port,
3678 3679 3680 3681 3682
			   opt_unix_socket, 0)))
  {
    myerror("connection failed");
    exit(0);
  }   
3683 3684
  fprintf(stdout," OK");
  
3685 3686

  /* set AUTOCOMMIT to ON*/
unknown's avatar
unknown committed
3687
  mysql_autocommit(lmysql, TRUE);
3688 3689 3690 3691 3692 3693
  
  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);
3694

unknown's avatar
unknown committed
3695
  strmov(query,"ALTER TABLE test_stmt_close ADD name varchar(20)");
3696
  stmt1= mysql_prepare(lmysql, query, 70);
3697
  mystmt_init(stmt1);
3698 3699 3700
  
  verify_param_count(stmt1, 0);
  
unknown's avatar
unknown committed
3701
  strmov(query,"INSERT INTO test_stmt_close(id) VALUES(?)");
3702
  stmt_x= mysql_prepare(mysql, query, 70);
3703
  mystmt_init(stmt_x);
3704 3705 3706

  verify_param_count(stmt_x, 1);
  
unknown's avatar
unknown committed
3707
  strmov(query,"UPDATE test_stmt_close SET id=? WHERE id=?");
3708
  stmt3= mysql_prepare(lmysql, query, 70);
3709
  mystmt_init(stmt3);
3710 3711 3712
  
  verify_param_count(stmt3, 2);
  
unknown's avatar
unknown committed
3713
  strmov(query,"SELECT * FROM test_stmt_close WHERE id=?");
3714
  stmt2= mysql_prepare(lmysql, query, 70);
3715
  mystmt_init(stmt2);
3716 3717

  verify_param_count(stmt2, 1);
3718 3719 3720 3721

  rc= mysql_stmt_close(stmt1);
  fprintf(stdout,"\n mysql_close_stmt(1) returned: %d", rc);
  myassert(rc == 0);
3722
  
3723 3724 3725
  mysql_close(lmysql); /* it should free all open stmts(stmt3, 2 and 1) */
 
#if NOT_VALID
3726 3727 3728
  rc= mysql_stmt_close(stmt3);
  fprintf(stdout,"\n mysql_close_stmt(3) returned: %d", rc);
  myassert( rc == 1);
3729
  
3730 3731 3732
  rc= mysql_stmt_close(stmt2);
  fprintf(stdout,"\n mysql_close_stmt(2) returned: %d", rc);
  myassert( rc == 1);
3733
#endif
3734 3735

  count= 100;
unknown's avatar
unknown committed
3736 3737 3738 3739 3740
  bind[0].buffer=(char *)&count;
  bind[0].buffer_type=MYSQL_TYPE_LONG;
  bind[0].is_null=0;

  rc = mysql_bind_param(stmt_x, bind);
3741
  mystmt(stmt_x, rc);
3742
  
3743 3744 3745
  rc = mysql_execute(stmt_x);
  mystmt(stmt_x, rc);

3746
  verify_st_affected_rows(stmt_x, 1);
3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761

  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
3762

3763 3764 3765 3766 3767
/********************************************************
 * To test simple set-variable prepare                   *
*********************************************************/
static void test_set_variable()
{
3768 3769 3770 3771 3772 3773
  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];
3774 3775 3776

  myheader("test_set_variable");

3777 3778 3779 3780
  mysql_autocommit(mysql, TRUE);
  
  stmt1 = mysql_prepare(mysql, "show variables like 'max_error_count'", 50);
  mystmt_init(stmt1);
3781

3782 3783 3784 3785 3786 3787
  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;
3788

3789 3790 3791 3792
  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;
3793

3794 3795 3796 3797 3798
  rc = mysql_execute(stmt1);
  mystmt(stmt1, rc);
  
  rc = mysql_bind_result(stmt1, get_bind);
  mystmt(stmt1, rc);
3799

3800 3801
  rc = mysql_fetch(stmt1);
  mystmt(stmt1, rc);
3802

3803 3804
  fprintf(stdout, "\n max_error_count(default): %d", get_count);
  def_count= get_count;
3805

3806 3807 3808
  myassert(strcmp(var,"max_error_count") == 0);
  rc = mysql_fetch(stmt1);
  myassert(rc == MYSQL_NO_DATA);
3809

3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821
  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;
3822 3823 3824
  rc= mysql_execute(stmt);
  mystmt(stmt,rc);

3825
  mysql_commit(mysql);
3826

3827 3828 3829 3830 3831
  rc = mysql_execute(stmt1);
  mystmt(stmt1, rc);
  
  rc = mysql_fetch(stmt1);
  mystmt(stmt1, rc);
3832

3833 3834
  fprintf(stdout, "\n max_error_count         : %d", get_count);
  myassert(get_count == set_count);
3835

3836 3837 3838 3839 3840
  rc = mysql_fetch(stmt1);
  myassert(rc == MYSQL_NO_DATA);
  
  /* restore back to default */
  set_count= def_count;
3841
  rc= mysql_execute(stmt);
3842 3843 3844 3845 3846 3847 3848
  mystmt(stmt, rc);
  
  rc = mysql_execute(stmt1);
  mystmt(stmt1, rc);
  
  rc = mysql_fetch(stmt1);
  mystmt(stmt1, rc);
3849

3850 3851
  fprintf(stdout, "\n max_error_count(default): %d", get_count);
  myassert(get_count == set_count);
3852

3853 3854
  rc = mysql_fetch(stmt1);
  myassert(rc == MYSQL_NO_DATA);
3855 3856
  
  mysql_stmt_close(stmt);
3857
  mysql_stmt_close(stmt1);
3858
}
unknown's avatar
unknown committed
3859

3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870
#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
3871
  rc = mysql_autocommit(mysql, TRUE);
3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883
  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
3884
  strmov(query,"INSERT INTO test_prep_insert VALUES(10,'venu1','test')");
3885
  stmt = mysql_prepare(mysql, query, 70);
3886 3887 3888 3889 3890 3891 3892
  mystmt_init(stmt);

  verify_param_count(stmt,0);

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

3893 3894
  mysql_stmt_close(stmt);

unknown's avatar
unknown committed
3895
  strmov(query,"INSERT INTO test_prep_insert VALUES(?,'venu',?)");
3896
  stmt = mysql_prepare(mysql, query, 70);
3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933
  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
3934
  rc = mysql_autocommit(mysql, TRUE);
3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946
  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
3947
  strmov(query,"UPDATE test_prep_update SET col1=10, col2='venu1' WHERE col3='test'");
3948
  stmt = mysql_prepare(mysql, query, 100);
3949 3950 3951 3952 3953 3954 3955
  mystmt_init(stmt);

  verify_param_count(stmt,0);

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

3956 3957
  mysql_stmt_close(stmt);

unknown's avatar
unknown committed
3958
  strmov(query,"UPDATE test_prep_update SET col1=?, col2='venu' WHERE col3=?");
3959
  stmt = mysql_prepare(mysql, query, 100);
3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000
  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
4001
  rc = mysql_autocommit(mysql, TRUE);
4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013
  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
4014
  strmov(query,"SELECT * FROM test_prep_select WHERE col1=10");
4015
  stmt = mysql_prepare(mysql, query, 70);
4016 4017 4018 4019 4020 4021 4022
  mystmt_init(stmt);

  verify_param_count(stmt,0);

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

unknown's avatar
unknown committed
4023
  strmov(query,"SELECT col1, col3 from test_prep_select WHERE col1=? AND col3='test' AND col2= ?");
4024
  stmt = mysql_prepare(mysql, query, 120);
4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050
  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);
4051

4052 4053 4054 4055 4056
  mysql_free_result(result);
  mysql_stmt_close(stmt);
}
#endif

unknown's avatar
unknown committed
4057

4058 4059 4060 4061 4062 4063 4064 4065 4066
/* 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
4067
  rc = mysql_autocommit(mysql, TRUE);
4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133
  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
4134 4135 4136
  mysql_free_result(result);
}

unknown's avatar
unknown committed
4137

unknown's avatar
unknown committed
4138 4139 4140
/* Multiple stmts .. */
static void test_multi_stmt()
{
4141 4142

  MYSQL_STMT  *stmt, *stmt1, *stmt2;
unknown's avatar
unknown committed
4143
  int         rc, id;
4144
  char        name[50];
unknown's avatar
unknown committed
4145
  MYSQL_BIND  bind[2];
unknown's avatar
unknown committed
4146 4147
  ulong       length[2];
  my_bool     is_null[2];
unknown's avatar
unknown committed
4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161
  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);

4162 4163 4164
  stmt2 = mysql_prepare(mysql, "UPDATE test_multi_table SET name='updated' WHERE id=10",100);
  mystmt_init(stmt2);

unknown's avatar
unknown committed
4165 4166
  verify_param_count(stmt,1);

unknown's avatar
unknown committed
4167 4168 4169
  bind[0].buffer_type= MYSQL_TYPE_SHORT;
  bind[0].buffer= (char *)&id;
  bind[0].is_null= &is_null[0];
4170 4171 4172
  bind[0].length= &length[0];
  is_null[0]= 0;
  length[0]= 0;
unknown's avatar
unknown committed
4173 4174

  bind[1].buffer_type = MYSQL_TYPE_STRING;
4175
  bind[1].buffer = (char *)name;
unknown's avatar
unknown committed
4176
  bind[1].length = &length[1];
4177 4178
  bind[1].is_null= &is_null[1];
    
unknown's avatar
unknown committed
4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192
  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);

4193 4194
  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
4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208
  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);
4209 4210 4211 4212
  
  rc = mysql_execute(stmt2);
  mystmt(stmt2, rc);

4213
  verify_st_affected_rows(stmt2, 1);
4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227

  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
4228 4229 4230 4231

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

4232
  verify_st_affected_rows(stmt1, 1);
unknown's avatar
unknown committed
4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244

  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);
4245 4246
  mysql_stmt_close(stmt2);

unknown's avatar
unknown committed
4247 4248
}

unknown's avatar
unknown committed
4249

4250 4251 4252 4253 4254 4255 4256 4257
/********************************************************
* to test simple sample - manual                        *
*********************************************************/
static void test_manual_sample()
{
  unsigned int param_count;
  MYSQL_STMT   *stmt;
  short        small_data;
4258 4259
  int          int_data;
  char         str_data[50];
4260
  ulonglong    affected_rows;
unknown's avatar
unknown committed
4261
  MYSQL_BIND   bind[3];
4262
  my_bool      is_null;
4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287

  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
4288 4289
  strmov(query, "INSERT INTO test_table(col1,col2,col3) values(?,?,?)");
  if (!(stmt = mysql_prepare(mysql,query,strlen(query))))
4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310
  {
    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
4311
  bind[0].buffer= (char *)&int_data;
4312 4313
  bind[0].is_null= 0;
  bind[0].length= 0;
unknown's avatar
unknown committed
4314

4315 4316
  /* STRING PART */
  bind[1].buffer_type= MYSQL_TYPE_VAR_STRING;
unknown's avatar
unknown committed
4317
  bind[1].buffer= (char *)str_data;
4318
  bind[1].buffer_length= sizeof(str_data);
4319 4320
  bind[1].is_null= 0;
  bind[1].length= 0;
4321 4322 4323
 
  /* SMALLINT PART */
  bind[2].buffer_type= MYSQL_TYPE_SHORT;
unknown's avatar
unknown committed
4324
  bind[2].buffer= (char *)&small_data;       
4325 4326 4327
  bind[2].is_null= &is_null;
  bind[2].length= 0;
  is_null= 0;
4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338

  /* 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
4339
  strmov(str_data,"MySQL"); /* string  */
4340
  
4341
  /* INSERT SMALLINT data as NULL */
4342
  is_null= 1;
4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363

  /* 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
4364
  strmov(str_data,"The most popular open source database"); 
4365
  small_data= 1000;         /* smallint */
4366
  is_null= 0;               /* reset */
4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404

  /* 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
4405

unknown's avatar
unknown committed
4406 4407 4408 4409 4410 4411 4412 4413 4414
/********************************************************
* 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
4415
  my_bool     is_null;
unknown's avatar
unknown committed
4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433

  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
4434 4435
  bind[0].buffer= (char *)&id;
  bind[0].is_null= &is_null;
unknown's avatar
unknown committed
4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446

  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
4447
  is_null=1;
unknown's avatar
unknown committed
4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465
  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;\
4466 4467 4468
                  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
4469 4470 4471 4472 4473 4474 4475 4476 4477
                  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;\
4478 4479 4480 4481 4482 4483
                  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
4484 4485 4486 4487 4488
  myheader("test_multi_query");

  rc = mysql_query(mysql, query); /* syntax error */
  myquery_r(rc);

4489 4490 4491
  myassert(0 == mysql_next_result(mysql));
  myassert(0 == mysql_more_results(mysql));

unknown's avatar
unknown committed
4492
  if (!(l_mysql = mysql_init(NULL)))
unknown's avatar
unknown committed
4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509
  { 
    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);

4510 4511
  count= exp_value= 0;
  while (mysql_more_results(mysql) && count < array_elements(rows))
unknown's avatar
unknown committed
4512
  {
4513
    fprintf(stdout,"\n Query %d: ", count);
unknown's avatar
unknown committed
4514
    if ((rc= mysql_next_result(mysql)))
4515 4516 4517 4518
    {
      exp_value= mysql_errno(mysql);
      fprintf(stdout, "ERROR %d: %s", exp_value, mysql_error(mysql));
    }
unknown's avatar
unknown committed
4519
    else
4520 4521 4522 4523
    {
      if ((result= mysql_store_result(mysql)))
        my_process_result_set(result);
      else
4524 4525
        fprintf(stdout,"OK, %lld row(s) affected, %d warning(s)",   
                        mysql_affected_rows(mysql),
4526 4527 4528 4529
                        mysql_warning_count(mysql));
      exp_value= (uint) mysql_affected_rows(mysql);
    }
    myassert(rows[count++] == exp_value);    
unknown's avatar
unknown committed
4530 4531 4532 4533
  }
  mysql= org_mysql;
}

unknown's avatar
unknown committed
4534

unknown's avatar
unknown committed
4535 4536 4537 4538 4539 4540 4541
/********************************************************
* to test simple bind store result                      *
*********************************************************/
static void test_store_result()
{
  MYSQL_STMT *stmt;
  int        rc;
unknown's avatar
unknown committed
4542
  long        nData;
unknown's avatar
unknown committed
4543 4544
  char       szData[100];
  MYSQL_BIND bind[2];
unknown's avatar
unknown committed
4545 4546
  ulong	     length, length1;
  my_bool    is_null[2];
unknown's avatar
unknown committed
4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572

  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
4573 4574
  bind[0].buffer= (char*) &nData;	/* integer data */
  bind[0].length= &length;
4575
  bind[0].is_null= &is_null[0];
unknown's avatar
unknown committed
4576 4577

  length= 0; 
unknown's avatar
unknown committed
4578 4579 4580
  bind[1].buffer_type=FIELD_TYPE_STRING;
  bind[1].buffer=szData;		/* string data */
  bind[1].buffer_length=sizeof(szData);
unknown's avatar
unknown committed
4581 4582 4583
  bind[1].length= &length1;
  bind[1].is_null= &is_null[1];
  length1= 0;
unknown's avatar
unknown committed
4584

4585
  stmt = mysql_prepare(mysql, "SELECT * FROM test_store_result", 50);
unknown's avatar
unknown committed
4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599
  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
4600
  fprintf(stdout,"\n row 1: %ld,%s(%lu)", nData, szData, length1);
unknown's avatar
unknown committed
4601 4602 4603 4604 4605 4606 4607
  myassert(nData == 10);
  myassert(strcmp(szData,"venu")==0);
  myassert(length1 == 4);

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

unknown's avatar
unknown committed
4608
  fprintf(stdout,"\n row 2: %ld,%s(%lu)",nData, szData, length1);
unknown's avatar
unknown committed
4609 4610 4611 4612 4613 4614 4615 4616
  myassert(nData == 20);
  myassert(strcmp(szData,"mysql")==0);
  myassert(length1 == 5);

  length=99;
  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);
 
4617
  if (is_null[0])
unknown's avatar
unknown committed
4618
    fprintf(stdout,"\n row 3: NULL,%s(%lu)", szData, length1);
4619
  myassert(is_null[0]);
unknown's avatar
unknown committed
4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634
  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
4635
  fprintf(stdout,"\n row 1: %ld,%s(%lu)",nData, szData, length1);
unknown's avatar
unknown committed
4636 4637 4638 4639 4640 4641 4642
  myassert(nData == 10);
  myassert(strcmp(szData,"venu")==0);
  myassert(length1 == 4);

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

unknown's avatar
unknown committed
4643
  fprintf(stdout,"\n row 2: %ld,%s(%lu)",nData, szData, length1);
unknown's avatar
unknown committed
4644 4645 4646 4647 4648 4649 4650 4651
  myassert(nData == 20);
  myassert(strcmp(szData,"mysql")==0);
  myassert(length1 == 5);

  length=99;
  rc = mysql_fetch(stmt);
  mystmt(stmt,rc);
 
4652
  if (is_null[0])
unknown's avatar
unknown committed
4653
    fprintf(stdout,"\n row 3: NULL,%s(%lu)", szData, length1);
4654
  myassert(is_null[0]);
unknown's avatar
unknown committed
4655 4656 4657 4658 4659 4660 4661 4662 4663
  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
4664

unknown's avatar
unknown committed
4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725
/********************************************************
* 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
4726

unknown's avatar
unknown committed
4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761
/********************************************************
* 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
4762
  bind[0].buffer= (char *) &nData;	/* integer data */
unknown's avatar
unknown committed
4763
  bind[0].length= &length;
unknown's avatar
unknown committed
4764
  bind[0].is_null= 0;
unknown's avatar
unknown committed
4765

unknown's avatar
unknown committed
4766
  strmov((char *)query , "SELECT col1 FROM test_store_result where col1= ?");
unknown's avatar
unknown committed
4767 4768 4769 4770 4771 4772 4773 4774
  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);
4775
  
unknown's avatar
unknown committed
4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811
  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
4812

unknown's avatar
unknown committed
4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852
/********************************************************
* to test simple subselect prepare                      *
*********************************************************/
static void test_subselect()
{
#if TO_BE_FIXED_IN_SERVER
  MYSQL_STMT *stmt;
  int        rc;
  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 */
4853
  bind[0].buffer_type= FIELD_TYPE_LONG;
unknown's avatar
unknown committed
4854
  bind[0].buffer= (char *) &id;	
4855
  bind[0].length= 0;
4856
  bind[0].is_null= 0;
4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881

  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
4882

unknown's avatar
unknown committed
4883
  strmov((char *)query , "SELECT ROW(1,7) IN (select id, id1 from test_sub2 WHERE id1=?)");
unknown's avatar
unknown committed
4884 4885
  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));
4886 4887

  stmt = mysql_prepare(mysql, query, 150);
unknown's avatar
unknown committed
4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902
  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);

4903
  fprintf(stdout,"\n row 1: %d",id);
unknown's avatar
unknown committed
4904 4905
  myassert(id == 1);

4906 4907 4908 4909 4910 4911 4912 4913 4914 4915
  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);

4916
  fprintf(stdout,"\n row 1: %d",id);
4917 4918
  myassert(id == 0);

unknown's avatar
unknown committed
4919 4920 4921 4922 4923 4924 4925
  rc = mysql_fetch(stmt);
  myassert(rc == MYSQL_NO_DATA);

  mysql_stmt_close(stmt);
#endif
}

4926 4927 4928 4929 4930 4931
/*
  Generalized conversion routine to handle DATE, TIME and DATETIME
  conversion using MYSQL_TIME structure
*/
static void test_bind_date_conv(uint row_count)
{ 
4932
  MYSQL_STMT   *stmt= 0;
4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 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 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017
  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");
5018
      fprintf(stdout," time[%d]: %02d-%02d-%02d %02d:%02d:%02d.%02lu",
5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 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 5074 5075 5076 5077 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 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173
                      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);
}

5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194
/*
  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
5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207
#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

5208 5209 5210
  stmt = mysql_prepare(mysql,"insert into test_pure(c2) values(?)",100);
  mystmt_init(stmt);

unknown's avatar
unknown committed
5211
#ifndef DBUG_OFF
5212 5213
  rc = mysql_execute(stmt);
  mystmt_r(stmt, rc);/* No parameters supplied */
unknown's avatar
unknown committed
5214
#endif
5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241

  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
5242 5243 5244
#ifndef DBUG_OFF
  rc = mysql_bind_result(stmt, (MYSQL_BIND *)0);
  mystmt_r(stmt, rc);
5245 5246 5247 5248
  
  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
5249 5250
#endif

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
  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
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
/* 
 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);

5378
  verify_st_affected_rows(stmt, 1);
5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390
  
  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);

5391
  verify_st_affected_rows(stmt, 1);
5392 5393 5394 5395 5396 5397 5398 5399 5400
  
  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);
5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438

  /* 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);
5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452
}

/*
  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");

5453 5454
  stmt = mysql_prepare(mysql,"SELECT DATABASE(), CURRENT_USER(), \
                              CURRENT_DATE(), CURRENT_TIME()",100);
5455 5456 5457 5458 5459 5460 5461 5462
  mystmt_init(stmt);

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

  rc = 0;
  while (mysql_fetch(stmt) != MYSQL_NO_DATA)
    rc++;
5463 5464

  fprintf(stdout, "\n total rows        : %d", rc);
5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486
  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++;
5487 5488 5489 5490
    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]);
5491
  }
5492
  fprintf(stdout, "\n total rows        : %d", rc);
5493 5494 5495 5496 5497
  myassert(rc == 1);

  mysql_stmt_close(stmt);
}

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
/*
  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);
  
5559 5560 5561 5562
  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);
5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581
  
  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);
}

5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 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 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 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
/*
  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);
}

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 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 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
/********************************************************
* 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 */
                       "",1);               /* db name, length(its bool flag)*/

  mysql_free_result(result);
  
  stmt = mysql_prepare(mysql,"SELECT @@autocommit",20);
  mystmt_init(stmt);
  
  rc = mysql_execute(stmt);
  mystmt(stmt,rc);

  result = mysql_prepare_result(stmt);
  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 */
                       "",1);               /* db name, length(its bool flag)*/

  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);

  result = mysql_prepare_result(stmt);
  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 */
                       "",type_length);     /* db name, length */

  mysql_free_result(result);
  mysql_stmt_close(stmt);

  stmt = mysql_prepare(mysql, "SELECT @@max_error_count", 30);
  mystmt_init(stmt);

  result = mysql_prepare_result(stmt);
  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 */
                       "",10);              /* db name, length */

  mysql_free_result(result);
  mysql_stmt_close(stmt);
  
  stmt = mysql_prepare(mysql, "SELECT @@max_allowed_packet", 30);
  mystmt_init(stmt);

  result = mysql_prepare_result(stmt);
  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 */
                       "",10);              /* db name, length */

  mysql_free_result(result);
  mysql_stmt_close(stmt);
5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902
  
  stmt = mysql_prepare(mysql, "SELECT @@sql_warnings", 30);
  mystmt_init(stmt);
  
  result = mysql_prepare_result(stmt);
  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 */
                       "",1);                 /* db name, length */

  mysql_free_result(result);
  mysql_stmt_close(stmt);
5903 5904
}

5905 5906 5907 5908 5909 5910 5911 5912
/*
  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
5913
{
unknown's avatar
unknown committed
5914 5915
  {"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
5916
  {"database", 'D', "Database to use", (char **) &opt_db, (char **) &opt_db,
unknown's avatar
unknown committed
5917
   0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
unknown's avatar
unknown committed
5918
  {"host", 'h', "Connect to host", (char **) &opt_host, (char **) &opt_host, 0, GET_STR_ALLOC,
unknown's avatar
unknown committed
5919 5920 5921 5922
   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
5923
#ifndef DONT_ALLOW_USER_CHANGE
unknown's avatar
unknown committed
5924 5925
  {"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
5926
#endif
unknown's avatar
unknown committed
5927 5928 5929 5930
  {"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},
5931
  {"count", 't', "Number of times test to be executed", (char **) &opt_count,
5932
   (char **) &opt_count, 0, GET_UINT, REQUIRED_ARG, 1, 0, 0, 0, 0, 0},
unknown's avatar
unknown committed
5933
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
unknown's avatar
unknown committed
5934 5935
};

5936 5937 5938 5939 5940 5941 5942 5943

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
5944 5945 5946 5947 5948
static void usage(void)
{
  /*
   *  show the usage string when the user asks for this
  */    
5949
  putc('\n',stdout);
unknown's avatar
unknown committed
5950 5951 5952 5953 5954
  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
5955
  puts("                 Copyright (C) 1995-2003 MySQL AB ");
unknown's avatar
unknown committed
5956
  puts("-----------------------------------------------------------------------\n");
5957 5958 5959 5960 5961 5962 5963
  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);

5964
  puts("***********************************************************************\n");
unknown's avatar
unknown committed
5965 5966
}

unknown's avatar
unknown committed
5967 5968 5969
static my_bool
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
	       char *argument)
unknown's avatar
unknown committed
5970
{
unknown's avatar
unknown committed
5971 5972 5973 5974
  switch (optid) {
  case 'p':
    if (argument)
    {
unknown's avatar
unknown committed
5975
      char *start=argument;
unknown's avatar
unknown committed
5976 5977 5978
      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
5979 5980
      if (*start)
        start[1]=0;
unknown's avatar
unknown committed
5981
    }
unknown's avatar
unknown committed
5982 5983 5984 5985 5986
    else
      tty_password= 1;
    break;
  case '?':
  case 'I':					/* Info */
unknown's avatar
unknown committed
5987
    usage();
unknown's avatar
unknown committed
5988
    exit(0);
unknown's avatar
unknown committed
5989
    break;
unknown's avatar
unknown committed
5990
  }
unknown's avatar
unknown committed
5991
  return 0;
unknown's avatar
unknown committed
5992 5993
}

unknown's avatar
unknown committed
5994 5995 5996
static void get_options(int argc, char **argv)
{
  int ho_error;
5997
  load_defaults("my",client_test_load_default_groups,&argc,&argv);
unknown's avatar
unknown committed
5998

5999 6000
  if ((ho_error= handle_options(&argc,&argv, client_test_long_options, 
                                get_one_option)))
unknown's avatar
unknown committed
6001 6002 6003 6004 6005 6006
    exit(ho_error);

  if (tty_password)
    opt_password=get_tty_password(NullS);
  return;
}
unknown's avatar
unknown committed
6007

6008 6009 6010 6011 6012 6013 6014 6015
/*
  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)", 
6016
          test_count-1, opt_count);
6017 6018 6019 6020 6021 6022 6023
  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
6024 6025 6026
/********************************************************
* main routine                                          *
*********************************************************/
unknown's avatar
unknown committed
6027
int main(int argc, char **argv)
unknown's avatar
unknown committed
6028
{  
unknown's avatar
unknown committed
6029
  MY_INIT(argv[0]);
6030 6031 6032

  load_defaults("my",client_test_load_default_groups,&argc,&argv);
  defaults_argv= argv;
unknown's avatar
unknown committed
6033
  get_options(argc,argv);
unknown's avatar
unknown committed
6034
    
unknown's avatar
unknown committed
6035
  client_connect();       /* connect to server */
unknown's avatar
unknown committed
6036
  
6037
  total_time= 0;
6038 6039 6040
  for (iter_count=1; iter_count <= opt_count; iter_count++) 
  {
    /* Start of tests */
6041 6042 6043
    test_count= 1;
   
    start_time= time((time_t *)0);
6044 6045 6046
   
    test_fetch_nobuffs();   /* to fecth without prior bound buffers */
    test_open_direct();     /* direct execution in the middle of open stmts */
6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061
    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 */
6062
    test_select_simple();   /* simple select prepare */
6063
#if NOT_USED
6064 6065 6066 6067 6068 6069 6070
  /* 
     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 */
6071
#endif
6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101
    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 */ 
    client_query();         /* simple client query test */
    client_store_result();  /* usage of mysql_store_result() */
    client_use_result();    /* usage of mysql_use_result() */  
    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 */
6102
    test_stmt_close();      /* mysql_stmt_close() test -- hangs */
6103
    test_prepare_field_result(); /* prepare meta info */
6104
    test_multi_stmt();      /* multi stmt test */
6105
    test_multi_query();     /* test multi query execution */
6106 6107 6108
    test_store_result();    /* test the store_result */
    test_store_result1();   /* test store result without buffers */
    test_store_result2();   /* test store result for misc case */
6109 6110 6111 6112 6113 6114
    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 */
6115 6116 6117 6118
    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 */
6119
    test_ushort_bug();      /* test a simple conv bug from php */
6120 6121
    test_sshort_bug();      /* test a simple conv bug from php */
    test_stiny_bug();       /* test a simple conv bug from php */
6122
    test_field_misc();      /* check the field info for misc case, bug: #74 */
6123 6124 6125

    end_time= time((time_t *)0);
    total_time+= difftime(end_time, start_time);
6126
    
6127 6128
    /* End of tests */
  }
unknown's avatar
unknown committed
6129 6130
  
  client_disconnect();    /* disconnect from server */
6131
  free_defaults(defaults_argv);
6132
  print_test_output();
6133
  
unknown's avatar
unknown committed
6134 6135
  return(0);
}
unknown's avatar
unknown committed
6136