~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzletest.cc

  • Committer: Brian Aker
  • Date: 2009-08-21 18:46:31 UTC
  • mto: This revision was merged to the branch mainline in revision 1123.
  • Revision ID: brian@gaz-20090821184631-e11gja79070fvhk4
Cleanup around page checksum removal

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
 
39
39
#define MTEST_VERSION "3.3"
40
40
 
41
 
#include "config.h"
 
41
#include "client_priv.h"
42
42
 
43
43
#include <queue>
44
44
#include <map>
45
45
#include <string>
46
 
 
47
 
#include <pcrecpp.h>
48
 
 
49
 
#include "client_priv.h"
 
46
#include <sstream>
 
47
#include <iostream>
 
48
#include <vector>
 
49
#include <algorithm>
 
50
#ifdef HAVE_SYS_WAIT_H
 
51
#include <sys/wait.h>
 
52
#endif
 
53
 
 
54
#include PCRE_HEADER
 
55
 
50
56
#include <mysys/hash.h>
51
57
#include <stdarg.h>
52
58
 
53
59
#include "errname.h"
54
60
 
 
61
/* Added this for string translation. */
 
62
#include <drizzled/gettext.h>
 
63
 
 
64
#ifndef DRIZZLE_RETURN_SERVER_GONE
 
65
#define DRIZZLE_RETURN_HANDSHAKE_FAILED DRIZZLE_RETURN_ERROR_CODE
 
66
#endif
 
67
 
55
68
using namespace std;
56
69
 
 
70
extern "C"
 
71
{
 
72
  unsigned char *get_var_key(const unsigned char* var, size_t *len, bool);
 
73
  bool get_one_option(int optid, const struct my_option *, char *argument);
 
74
}
 
75
 
57
76
#define MAX_VAR_NAME_LENGTH    256
58
77
#define MAX_COLUMNS            256
59
78
#define MAX_EMBEDDED_SERVER_ARGS 64
60
79
#define MAX_DELIMITER_LENGTH 16
61
 
 
62
80
/* Flags controlling send and reap */
63
81
#define QUERY_SEND_FLAG  1
64
82
#define QUERY_REAP_FLAG  2
65
83
 
 
84
ErrorCodes global_error_names;
 
85
 
66
86
enum {
67
87
  OPT_PS_PROTOCOL, OPT_SP_PROTOCOL, OPT_CURSOR_PROTOCOL, OPT_VIEW_PROTOCOL,
68
 
  OPT_MAX_CONNECT_RETRIES, OPT_MARK_PROGRESS, OPT_LOG_DIR, OPT_TAIL_LINES
 
88
  OPT_MAX_CONNECT_RETRIES, OPT_MARK_PROGRESS, OPT_LOG_DIR, OPT_TAIL_LINES,
 
89
  OPT_TESTDIR
69
90
};
70
91
 
71
92
static int record= 0, opt_sleep= -1;
72
 
static char *opt_db= 0, *opt_pass= 0;
73
 
const char *opt_user= 0, *opt_host= 0, *unix_sock= 0, *opt_basedir= "./";
 
93
static char *opt_db= NULL, *opt_pass= NULL;
 
94
const char *opt_user= NULL, *opt_host= NULL, *unix_sock= NULL,
 
95
           *opt_basedir= "./";
74
96
const char *opt_logdir= "";
75
 
const char *opt_include= 0, *opt_charsets_dir;
76
 
static int opt_port= 0;
 
97
const char *opt_include= NULL, *opt_charsets_dir;
 
98
const char *opt_testdir= NULL;
 
99
static uint32_t opt_port= 0;
77
100
static int opt_max_connect_retries;
78
 
static bool opt_compress= 0, silent= 0, verbose= 0;
79
 
static bool debug_info_flag= 0, debug_check_flag= 0;
80
 
static bool tty_password= 0;
81
 
static bool opt_mark_progress= 0;
82
 
static bool parsing_disabled= 0;
 
101
static bool opt_compress= false, silent= false, verbose= false;
 
102
static bool debug_info_flag= false, debug_check_flag= false;
 
103
static bool tty_password= false;
 
104
static bool opt_mark_progress= false;
 
105
static bool parsing_disabled= false;
83
106
static bool display_result_vertically= false,
84
107
  display_metadata= false, display_result_sorted= false;
85
 
static bool disable_query_log= 0, disable_result_log= 0;
86
 
static bool disable_warnings= 0;
87
 
static bool disable_info= 1;
88
 
static bool abort_on_error= 1;
89
 
static bool server_initialized= 0;
90
 
static bool is_windows= 0;
 
108
static bool disable_query_log= false, disable_result_log= false;
 
109
static bool disable_warnings= false;
 
110
static bool disable_info= true;
 
111
static bool abort_on_error= true;
 
112
static bool server_initialized= false;
 
113
static bool is_windows= false;
91
114
static char **default_argv;
92
115
static const char *load_default_groups[]= { "drizzletest", "client", 0 };
93
116
static char line_buffer[MAX_DELIMITER_LENGTH], *line_buffer_pos= line_buffer;
94
117
 
95
 
static uint start_lineno= 0; /* Start line of current command */
96
 
static uint my_end_arg= 0;
 
118
static uint32_t start_lineno= 0; /* Start line of current command */
 
119
static uint32_t my_end_arg= 0;
97
120
 
98
121
/* Number of lines of the result to include in failure report */
99
 
static uint opt_tail_lines= 0;
 
122
static uint32_t opt_tail_lines= 0;
100
123
 
101
124
static char delimiter[MAX_DELIMITER_LENGTH]= ";";
102
 
static uint delimiter_length= 1;
 
125
static uint32_t delimiter_length= 1;
103
126
 
104
127
static char TMPDIR[FN_REFLEN];
105
128
 
125
148
{
126
149
  FILE* file;
127
150
  const char *file_name;
128
 
  uint lineno; /* Current line in file */
 
151
  uint32_t lineno; /* Current line in file */
129
152
};
130
153
 
131
154
static struct st_test_file file_stack[16];
165
188
master_pos_st master_pos;
166
189
 
167
190
/* if set, all results are concated and compared against this file */
168
 
const char *result_file_name= 0;
 
191
const char *result_file_name= NULL;
169
192
 
170
193
typedef struct st_var
171
194
{
187
210
 
188
211
struct st_connection
189
212
{
190
 
  DRIZZLE drizzle;
 
213
  drizzle_st *drizzle;
 
214
  drizzle_con_st con;
191
215
  /* Used when creating views and sp, to avoid implicit commit */
192
 
  DRIZZLE *util_drizzle;
 
216
  drizzle_con_st *util_con;
193
217
  char *name;
194
218
};
195
219
struct st_connection connections[128];
227
251
  Q_DISPLAY_VERTICAL_RESULTS, Q_DISPLAY_HORIZONTAL_RESULTS,
228
252
  Q_QUERY_VERTICAL, Q_QUERY_HORIZONTAL, Q_SORTED_RESULT,
229
253
  Q_START_TIMER, Q_END_TIMER,
230
 
  Q_CHARACTER_SET, Q_DISABLE_PS_PROTOCOL, Q_ENABLE_PS_PROTOCOL,
 
254
  Q_CHARACTER_SET,
231
255
  Q_DISABLE_RECONNECT, Q_ENABLE_RECONNECT,
232
256
  Q_IF,
233
257
  Q_DISABLE_PARSING, Q_ENABLE_PARSING,
345
369
  enum match_err_type type;
346
370
  union
347
371
  {
348
 
    uint errnum;
349
 
    char sqlstate[SQLSTATE_LENGTH+1];  /* \0 terminated string */
 
372
    uint32_t errnum;
 
373
    char sqlstate[DRIZZLE_MAX_SQLSTATE_SIZE+1];  /* \0 terminated string */
350
374
  } code;
351
375
};
352
376
 
353
377
struct st_expected_errors
354
378
{
355
379
  struct st_match_err err[10];
356
 
  uint count;
 
380
  uint32_t count;
357
381
};
358
382
static struct st_expected_errors saved_expected_errors;
359
383
 
388
412
VAR* var_from_env(const char *, const char *);
389
413
VAR* var_init(VAR* v, const char *name, int name_len, const char *val,
390
414
              int val_len);
391
 
void var_free(void* v);
 
415
extern "C" void var_free(void* v);
392
416
VAR* var_get(const char *var_name, const char** var_name_end,
393
417
             bool raw, bool ignore_not_existing);
394
418
void eval_expr(VAR* v, const char *p, const char** p_end);
395
 
bool match_delimiter(int c, const char *delim, uint length);
 
419
bool match_delimiter(int c, const char *delim, uint32_t length);
396
420
void dump_result_to_reject_file(char *buf, int size);
397
421
void dump_result_to_log_file(const char *buf, int size);
398
422
void dump_warning_messages(void);
405
429
 
406
430
/* For replace_column */
407
431
static char *replace_column[MAX_COLUMNS];
408
 
static uint max_replace_column= 0;
 
432
static uint32_t max_replace_column= 0;
409
433
void do_get_replace_column(struct st_command*);
410
434
void free_replace_column(void);
411
435
 
430
454
void replace_append_mem(string *ds, const char *val,
431
455
                        int len);
432
456
void replace_append(string *ds, const char *val);
433
 
void replace_append_uint(string *ds, uint val);
 
457
void replace_append_uint(string *ds, uint32_t val);
434
458
void append_sorted(string* ds, string* ds_input);
435
459
 
436
460
void handle_error(struct st_command*,
439
463
void handle_no_error(struct st_command*);
440
464
 
441
465
 
442
 
#define do_send_query(cn,q,q_len,flags) drizzle_send_query(&cn->drizzle, q, q_len)
443
 
 
444
466
void do_eval(string *query_eval, const char *query,
445
467
             const char *query_end, bool pass_through_escape_chars)
446
468
{
511
533
  options are passed.
512
534
*/
513
535
 
514
 
void append_os_quoted(string *str, const char *append, ...)
 
536
static void append_os_quoted(string *str, const char *append, ...)
515
537
{
516
538
  const char *quote_str= "\'";
517
 
  const uint  quote_len= 1;
 
539
  const uint32_t  quote_len= 1;
518
540
 
519
541
  va_list dirty_text;
520
542
 
533
555
      str->append(quote_str, quote_len);
534
556
      cur_pos= next_pos + 1;
535
557
    }
536
 
    str->append(cur_pos, next_pos - cur_pos);
 
558
    str->append(cur_pos);
537
559
    append= va_arg(dirty_text, char *);
538
560
  }
539
561
  va_end(dirty_text);
554
576
 
555
577
*/
556
578
 
557
 
static void show_query(DRIZZLE *drizzle, const char* query)
 
579
static void show_query(drizzle_con_st *con, const char* query)
558
580
{
559
 
  DRIZZLE_RES* res;
560
 
 
561
 
 
562
 
  if (!drizzle)
 
581
  drizzle_result_st res;
 
582
  drizzle_return_t ret;
 
583
 
 
584
  if (!con)
563
585
    return;
564
586
 
565
 
  if (drizzle_query(drizzle, query))
 
587
  if (drizzle_query_str(con, &res, query, &ret) == NULL ||
 
588
      ret != DRIZZLE_RETURN_OK)
566
589
  {
567
 
    log_msg("Error running query '%s': %d %s",
568
 
            query, drizzle_errno(drizzle), drizzle_error(drizzle));
 
590
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
591
    {
 
592
      log_msg("Error running query '%s': %d %s",
 
593
              query, drizzle_result_error_code(&res),
 
594
              drizzle_result_error(&res));
 
595
      drizzle_result_free(&res);
 
596
    }
 
597
    else
 
598
    {
 
599
      log_msg("Error running query '%s': %d %s",
 
600
              query, ret, drizzle_con_error(con));
 
601
    }
569
602
    return;
570
603
  }
571
604
 
572
 
  if ((res= drizzle_store_result(drizzle)) == NULL)
 
605
  if (drizzle_result_column_count(&res) == 0 ||
 
606
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
573
607
  {
574
608
    /* No result set returned */
 
609
    drizzle_result_free(&res);
575
610
    return;
576
611
  }
577
612
 
578
613
  {
579
 
    DRIZZLE_ROW row;
 
614
    drizzle_row_t row;
580
615
    unsigned int i;
581
616
    unsigned int row_num= 0;
582
 
    unsigned int num_fields= drizzle_num_fields(res);
583
 
    const DRIZZLE_FIELD *fields= drizzle_fetch_fields(res);
 
617
    unsigned int num_fields= drizzle_result_column_count(&res);
 
618
    drizzle_column_st *column;
584
619
 
585
620
    fprintf(stderr, "=== %s ===\n", query);
586
 
    while ((row= drizzle_fetch_row(res)))
 
621
    while ((row= drizzle_row_next(&res)))
587
622
    {
588
 
      uint32_t *lengths= drizzle_fetch_lengths(res);
 
623
      size_t *lengths= drizzle_row_field_sizes(&res);
589
624
      row_num++;
590
625
 
591
626
      fprintf(stderr, "---- %d. ----\n", row_num);
 
627
      drizzle_column_seek(&res, 0);
592
628
      for(i= 0; i < num_fields; i++)
593
629
      {
 
630
        column= drizzle_column_next(&res);
594
631
        fprintf(stderr, "%s\t%.*s\n",
595
 
                fields[i].name,
 
632
                drizzle_column_name(column),
596
633
                (int)lengths[i], row[i] ? row[i] : "NULL");
597
634
      }
598
635
    }
600
637
      fprintf(stderr, "=");
601
638
    fprintf(stderr, "\n\n");
602
639
  }
603
 
  drizzle_free_result(res);
 
640
  drizzle_result_free(&res);
604
641
 
605
642
  return;
606
643
}
619
656
 
620
657
*/
621
658
 
622
 
static void show_warnings_before_error(DRIZZLE *drizzle)
 
659
static void show_warnings_before_error(drizzle_con_st *con)
623
660
{
624
 
  DRIZZLE_RES* res;
 
661
  drizzle_result_st res;
 
662
  drizzle_return_t ret;
625
663
  const char* query= "SHOW WARNINGS";
626
664
 
627
 
 
628
 
  if (!drizzle)
 
665
  if (!con)
629
666
    return;
630
667
 
631
 
  if (drizzle_query(drizzle, query))
 
668
  if (drizzle_query_str(con, &res, query, &ret) == NULL ||
 
669
      ret != DRIZZLE_RETURN_OK)
632
670
  {
633
 
    log_msg("Error running query '%s': %d %s",
634
 
            query, drizzle_errno(drizzle), drizzle_error(drizzle));
 
671
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
672
    {
 
673
      log_msg("Error running query '%s': %d %s",
 
674
              query, drizzle_result_error_code(&res),
 
675
              drizzle_result_error(&res));
 
676
      drizzle_result_free(&res);
 
677
    }
 
678
    else
 
679
    {
 
680
      log_msg("Error running query '%s': %d %s",
 
681
              query, ret, drizzle_con_error(con));
 
682
    }
635
683
    return;
636
684
  }
637
685
 
638
 
  if ((res= drizzle_store_result(drizzle)) == NULL)
 
686
  if (drizzle_result_column_count(&res) == 0 ||
 
687
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
639
688
  {
640
689
    /* No result set returned */
 
690
    drizzle_result_free(&res);
641
691
    return;
642
692
  }
643
693
 
644
 
  if (drizzle_num_rows(res) <= 1)
 
694
  if (drizzle_result_row_count(&res) <= 1)
645
695
  {
646
696
    /* Don't display the last row, it's "last error" */
647
697
  }
648
698
  else
649
699
  {
650
 
    DRIZZLE_ROW row;
 
700
    drizzle_row_t row;
651
701
    unsigned int row_num= 0;
652
 
    unsigned int num_fields= drizzle_num_fields(res);
 
702
    unsigned int num_fields= drizzle_result_column_count(&res);
653
703
 
654
704
    fprintf(stderr, "\nWarnings from just before the error:\n");
655
 
    while ((row= drizzle_fetch_row(res)))
 
705
    while ((row= drizzle_row_next(&res)))
656
706
    {
657
707
      uint32_t i;
658
 
      uint32_t *lengths= drizzle_fetch_lengths(res);
 
708
      size_t *lengths= drizzle_row_field_sizes(&res);
659
709
 
660
 
      if (++row_num >= drizzle_num_rows(res))
 
710
      if (++row_num >= drizzle_result_row_count(&res))
661
711
      {
662
712
        /* Don't display the last row, it's "last error" */
663
713
        break;
671
721
      fprintf(stderr, "\n");
672
722
    }
673
723
  }
674
 
  drizzle_free_result(res);
 
724
  drizzle_result_free(&res);
675
725
 
676
726
  return;
677
727
}
704
754
  for (i= 0; i < num_args; i++)
705
755
  {
706
756
    const struct command_arg *arg= &args[i];
 
757
    arg->ds->clear();
707
758
 
708
759
    switch (arg->type) {
709
760
      /* A string */
762
813
}
763
814
 
764
815
 
765
 
static void handle_command_error(struct st_command *command, uint error)
 
816
static void handle_command_error(struct st_command *command, uint32_t error)
766
817
{
767
818
 
768
819
  if (error != 0)
769
820
  {
770
 
    uint i;
 
821
    uint32_t i;
771
822
 
772
823
    if (command->abort_on_error)
773
824
      die("command \"%.*s\" failed with error %d",
797
848
 
798
849
static void close_connections(void)
799
850
{
800
 
 
801
851
  for (--next_con; next_con >= connections; --next_con)
802
852
  {
803
 
    drizzle_close(&next_con->drizzle);
804
 
    if (next_con->util_drizzle)
805
 
      drizzle_close(next_con->util_drizzle);
 
853
    if (next_con->drizzle != NULL)
 
854
    {
 
855
      drizzle_free(next_con->drizzle);
 
856
      next_con->drizzle= NULL;
 
857
    }
806
858
    free(next_con->name);
807
859
  }
808
860
  return;
816
868
  {
817
869
    if (cur_file->file && cur_file->file != stdin)
818
870
    {
819
 
      my_fclose(cur_file->file, MYF(0));
 
871
      fclose(cur_file->file);
820
872
    }
821
873
    free((unsigned char*) cur_file->file_name);
822
874
    cur_file->file_name= 0;
827
879
 
828
880
static void free_used_memory(void)
829
881
{
830
 
  uint i;
 
882
  uint32_t i;
831
883
 
832
884
 
833
885
  close_connections();
837
889
  vector<struct st_command *>::iterator iter;
838
890
  for (iter= q_lines.begin() ; iter < q_lines.end() ; iter++)
839
891
  {
840
 
    struct st_command * q_line= *(iter.base());
 
892
    struct st_command * q_line= *iter;
841
893
    if (q_line->query_buf != NULL)
842
894
    {
843
895
      free(q_line->query_buf);
949
1001
    been produced prior to the error
950
1002
  */
951
1003
  if (cur_con)
952
 
    show_warnings_before_error(&cur_con->drizzle);
 
1004
    show_warnings_before_error(&cur_con->con);
953
1005
 
954
1006
  cleanup_and_exit(1);
955
1007
}
1074
1126
static void cat_file(string* ds, const char* filename)
1075
1127
{
1076
1128
  int fd;
1077
 
  uint len;
 
1129
  uint32_t len;
1078
1130
  char buff[512];
1079
1131
 
1080
1132
  if ((fd= my_open(filename, O_RDONLY, MYF(0))) < 0)
1156
1208
  string ds_cmdline;
1157
1209
 
1158
1210
 
1159
 
  append_os_quoted(&ds_cmdline, tool_path);
 
1211
  append_os_quoted(&ds_cmdline, tool_path, NULL);
1160
1212
  ds_cmdline.append(" ");
1161
1213
 
1162
1214
  va_start(args, result);
1206
1258
               "2>&1",
1207
1259
               NULL) > 1) /* Most "diff" tools return >1 if error */
1208
1260
  {
1209
 
    ds_tmp= "";
1210
1261
 
1211
1262
    /* Fallback to context diff with "diff -c" */
1212
1263
    if (run_tool("diff",
1221
1272
        Fallback to dump both files to result file and inform
1222
1273
        about installing "diff"
1223
1274
      */
1224
 
      ds_tmp= "";
 
1275
      ds_tmp.clear();
1225
1276
 
1226
1277
      ds_tmp.append(
1227
1278
                    "\n"
1282
1333
{
1283
1334
  int error= RESULT_OK;
1284
1335
  File fd2;
1285
 
  uint len, len2;
 
1336
  uint32_t len, len2;
1286
1337
  char buff[512], buff2[512];
 
1338
  const char *fname= filename2;
 
1339
  string tmpfile;
1287
1340
 
1288
 
  if ((fd2= my_open(filename2, O_RDONLY, MYF(0))) < 0)
 
1341
  if ((fd2= my_open(fname, O_RDONLY, MYF(0))) < 0)
1289
1342
  {
1290
1343
    my_close(fd, MYF(0));
1291
 
    die("Failed to open second file: '%s'", filename2);
 
1344
    if (opt_testdir != NULL)
 
1345
    {
 
1346
      tmpfile= opt_testdir;
 
1347
      if (tmpfile[tmpfile.length()] != '/')
 
1348
        tmpfile.append("/");
 
1349
      tmpfile.append(filename2);
 
1350
      fname= tmpfile.c_str();
 
1351
    }
 
1352
    if ((fd2= my_open(fname, O_RDONLY, MYF(0))) < 0)
 
1353
    {
 
1354
      my_close(fd, MYF(0));
 
1355
    
 
1356
      die("Failed to open second file: '%s'", fname);
 
1357
    }
1292
1358
  }
1293
1359
  while((len= my_read(fd, (unsigned char*)&buff,
1294
1360
                      sizeof(buff), MYF(0))) > 0)
1374
1440
  char temp_file_path[FN_REFLEN];
1375
1441
 
1376
1442
  if ((fd= create_temp_file(temp_file_path, NULL,
1377
 
                            "tmp", O_CREAT | O_SHARE | O_RDWR,
1378
 
                            MYF(MY_WME))) < 0)
 
1443
                            "tmp", MYF(MY_WME))) < 0)
1379
1444
    die("Failed to create temporary file for ds");
1380
1445
 
1381
1446
  /* Write ds to temporary file and set file pos to beginning*/
1382
1447
  if (my_write(fd, (unsigned char *) ds->c_str(), ds->length(),
1383
1448
               MYF(MY_FNABP | MY_WME)) ||
1384
 
      my_seek(fd, 0, SEEK_SET, MYF(0)) == MY_FILEPOS_ERROR)
 
1449
      lseek(fd, 0, SEEK_SET) == MY_FILEPOS_ERROR)
1385
1450
  {
1386
1451
    my_close(fd, MYF(0));
1387
1452
    /* Remove the temporary file */
1454
1519
    ds->erase(); /* Don't create a .log file */
1455
1520
 
1456
1521
    show_diff(NULL, result_file_name, reject_file);
1457
 
    die(mess);
 
1522
    die("%s",mess);
1458
1523
    break;
1459
1524
  }
1460
1525
  default: /* impossible */
1538
1603
}
1539
1604
 
1540
1605
 
1541
 
static unsigned char *get_var_key(const unsigned char* var, size_t *len,
1542
 
                          bool __attribute__((unused)) t)
 
1606
unsigned char *get_var_key(const unsigned char* var, size_t *len, bool)
1543
1607
{
1544
1608
  register char* key;
1545
1609
  key = ((VAR*)var)->name;
1558
1622
  if (!val_len && val)
1559
1623
    val_len = strlen(val) ;
1560
1624
  val_alloc_len = val_len + 16; /* room to grow */
1561
 
  if (!(tmp_var=v) && !(tmp_var = (VAR*)my_malloc(sizeof(*tmp_var)
1562
 
                                                  + name_len+1, MYF(MY_WME))))
 
1625
  if (!(tmp_var=v) && !(tmp_var = (VAR*)malloc(sizeof(*tmp_var)
 
1626
                                               + name_len+1)))
1563
1627
    die("Out of memory");
1564
1628
 
1565
1629
  tmp_var->name = (name) ? (char*) tmp_var + sizeof(*tmp_var) : 0;
1566
1630
  tmp_var->alloced = (v == 0);
1567
1631
 
1568
 
  if (!(tmp_var->str_val = (char *)my_malloc(val_alloc_len+1, MYF(MY_WME))))
 
1632
  if (!(tmp_var->str_val = (char *)malloc(val_alloc_len+1)))
1569
1633
    die("Out of memory");
1570
1634
 
1571
1635
  memcpy(tmp_var->name, name, name_len);
1618
1682
  if (digit < 0 || digit >= 10)
1619
1683
  {
1620
1684
    const char *save_var_name = var_name, *end;
1621
 
    uint length;
 
1685
    uint32_t length;
1622
1686
    end = (var_name_end) ? *var_name_end : 0;
1623
1687
    while (my_isvar(charset_info,*var_name) && var_name != end)
1624
1688
      var_name++;
1628
1692
        return(0);
1629
1693
      die("Empty variable");
1630
1694
    }
1631
 
    length= (uint) (var_name - save_var_name);
 
1695
    length= (uint32_t) (var_name - save_var_name);
1632
1696
    if (length >= MAX_VAR_NAME_LENGTH)
1633
1697
      die("Too long variable name: %s", save_var_name);
1634
1698
 
1636
1700
                                 length)))
1637
1701
    {
1638
1702
      char buff[MAX_VAR_NAME_LENGTH+1];
1639
 
      strmake(buff, save_var_name, length);
 
1703
      strncpy(buff, save_var_name, length);
 
1704
      buff[length]= '\0';
1640
1705
      v= var_from_env(buff, "");
1641
1706
    }
1642
1707
    var_name--;  /* Point at last character */
1692
1757
  digit= *var_name - '0';
1693
1758
  if (!(digit < 10 && digit >= 0))
1694
1759
  {
1695
 
    v= var_obtain(var_name, (uint) (var_name_end - var_name));
 
1760
    v= var_obtain(var_name, (uint32_t) (var_name_end - var_name));
1696
1761
  }
1697
1762
  else
1698
1763
    v= var_reg + digit;
1711
1776
    snprintf(buf, sizeof(buf), "%.*s=%.*s",
1712
1777
             v->name_len, v->name,
1713
1778
             v->str_val_len, v->str_val);
1714
 
    if (!(v->env_s= my_strdup(buf, MYF(MY_WME))))
 
1779
    if (!(v->env_s= strdup(buf)))
1715
1780
      die("Out of memory");
1716
1781
    putenv(v->env_s);
1717
1782
    free(old_env_s);
1736
1801
 
1737
1802
/*
1738
1803
  Store an integer (typically the returncode of the last SQL)
1739
 
  statement in the drizzletest builtin variable $drizzle_errno
 
1804
  statement in the drizzletest builtin variable $drizzleclient_errno
1740
1805
*/
1741
1806
 
1742
1807
static void var_set_errno(int sql_errno)
1743
1808
{
1744
 
  var_set_int("$drizzle_errno", sql_errno);
 
1809
  var_set_int("$drizzleclient_errno", sql_errno);
1745
1810
}
1746
1811
 
1747
1812
 
1748
1813
/*
1749
 
  Update $drizzle_get_server_version variable with version
 
1814
  Update $drizzleclient_get_server_version variable with version
1750
1815
  of the currently connected server
1751
1816
*/
1752
1817
 
1753
 
static void var_set_drizzle_get_server_version(DRIZZLE *drizzle)
 
1818
static void var_set_drizzleclient_get_server_version(drizzle_con_st *con)
1754
1819
{
1755
 
  var_set_int("$drizzle_get_server_version", drizzle_get_server_version(drizzle));
 
1820
  var_set_int("$drizzle_con_server_version", drizzle_con_server_version_number(con));
1756
1821
}
1757
1822
 
1758
1823
 
1783
1848
{
1784
1849
  const char *end = (char*)((query_end && *query_end) ?
1785
1850
                            *query_end : query + strlen(query));
1786
 
  DRIZZLE_RES *res;
1787
 
  DRIZZLE_ROW row;
1788
 
  DRIZZLE *drizzle= &cur_con->drizzle;
 
1851
  drizzle_result_st res;
 
1852
  drizzle_return_t ret;
 
1853
  drizzle_row_t row;
 
1854
  drizzle_con_st *con= &cur_con->con;
1789
1855
  string ds_query;
1790
1856
 
1791
1857
 
1798
1864
  /* Eval the query, thus replacing all environment variables */
1799
1865
  do_eval(&ds_query, query, end, false);
1800
1866
 
1801
 
  if (drizzle_real_query(drizzle, ds_query.c_str(), ds_query.length()))
1802
 
    die("Error running query '%s': %d %s", ds_query.c_str(),
1803
 
        drizzle_errno(drizzle), drizzle_error(drizzle));
1804
 
  if (!(res= drizzle_store_result(drizzle)))
 
1867
  if (drizzle_query(con, &res, ds_query.c_str(), ds_query.length(),
 
1868
                    &ret) == NULL ||
 
1869
      ret != DRIZZLE_RETURN_OK)
 
1870
  {
 
1871
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
1872
    {
 
1873
      die("Error running query '%s': %d %s", ds_query.c_str(),
 
1874
          drizzle_result_error_code(&res), drizzle_result_error(&res));
 
1875
      drizzle_result_free(&res);
 
1876
    }
 
1877
    else
 
1878
    {
 
1879
      die("Error running query '%s': %d %s", ds_query.c_str(), ret,
 
1880
          drizzle_con_error(con));
 
1881
    }
 
1882
  }
 
1883
  if (drizzle_result_column_count(&res) == 0 ||
 
1884
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
1805
1885
    die("Query '%s' didn't return a result set", ds_query.c_str());
1806
1886
 
1807
 
  if ((row= drizzle_fetch_row(res)) && row[0])
 
1887
  if ((row= drizzle_row_next(&res)) && row[0])
1808
1888
  {
1809
1889
    /*
1810
1890
      Concatenate all fields in the first row with tab in between
1812
1892
    */
1813
1893
    string result;
1814
1894
    uint32_t i;
1815
 
    uint32_t *lengths;
 
1895
    size_t *lengths;
1816
1896
 
1817
 
    lengths= drizzle_fetch_lengths(res);
1818
 
    for (i= 0; i < drizzle_num_fields(res); i++)
 
1897
    lengths= drizzle_row_field_sizes(&res);
 
1898
    for (i= 0; i < drizzle_result_column_count(&res); i++)
1819
1899
    {
1820
1900
      if (row[i])
1821
1901
      {
1830
1910
  else
1831
1911
    eval_expr(var, "", 0);
1832
1912
 
1833
 
  drizzle_free_result(res);
 
1913
  drizzle_result_free(&res);
1834
1914
  return;
1835
1915
}
1836
1916
 
1861
1941
{
1862
1942
  long row_no;
1863
1943
  int col_no= -1;
1864
 
  DRIZZLE_RES* res;
1865
 
  DRIZZLE *drizzle= &cur_con->drizzle;
 
1944
  drizzle_result_st res;
 
1945
  drizzle_return_t ret;
 
1946
  drizzle_con_st *con= &cur_con->con;
1866
1947
 
1867
1948
  string ds_query;
1868
1949
  string ds_col;
1881
1962
                     ',');
1882
1963
 
1883
1964
  /* Convert row number to int */
1884
 
  if (!str2int(ds_row.c_str(), 10, (long) 0, (long) INT_MAX, &row_no))
 
1965
  row_no= atoi(ds_row.c_str());
 
1966
  
 
1967
  istringstream buff(ds_row);
 
1968
  if ((buff >> row_no).fail())
1885
1969
    die("Invalid row number: '%s'", ds_row.c_str());
1886
1970
 
1887
1971
  /* Remove any surrounding "'s from the query - if there is any */
1889
1973
  char * unstripped_query= strdup(ds_query.c_str());
1890
1974
  if (strip_surrounding(unstripped_query, '"', '"'))
1891
1975
    die("Mismatched \"'s around query '%s'", ds_query.c_str());
1892
 
  ds_query= unstripped_query;
 
1976
  ds_query.clear();
 
1977
  ds_query.append(unstripped_query);
1893
1978
 
1894
1979
  /* Run the query */
1895
 
  if (drizzle_real_query(drizzle, ds_query.c_str(), ds_query.length()))
1896
 
    die("Error running query '%s': %d %s", ds_query.c_str(),
1897
 
        drizzle_errno(drizzle), drizzle_error(drizzle));
1898
 
  if (!(res= drizzle_store_result(drizzle)))
 
1980
  if (drizzle_query(con, &res, ds_query.c_str(), ds_query.length(),
 
1981
                    &ret) == NULL ||
 
1982
      ret != DRIZZLE_RETURN_OK)
 
1983
  {
 
1984
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
1985
    {
 
1986
      die("Error running query '%s': %d %s", ds_query.c_str(),
 
1987
          drizzle_result_error_code(&res), drizzle_result_error(&res));
 
1988
      drizzle_result_free(&res);
 
1989
    }
 
1990
    else
 
1991
    {
 
1992
      die("Error running query '%s': %d %s", ds_query.c_str(), ret,
 
1993
          drizzle_con_error(con));
 
1994
    }
 
1995
  }
 
1996
  if (drizzle_result_column_count(&res) == 0 ||
 
1997
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
1899
1998
    die("Query '%s' didn't return a result set", ds_query.c_str());
1900
1999
 
1901
2000
  {
1902
2001
    /* Find column number from the given column name */
1903
 
    uint i;
1904
 
    uint num_fields= drizzle_num_fields(res);
1905
 
    const DRIZZLE_FIELD *fields= drizzle_fetch_fields(res);
 
2002
    uint32_t i;
 
2003
    uint32_t num_fields= drizzle_result_column_count(&res);
 
2004
    drizzle_column_st *column;
1906
2005
 
1907
2006
    for (i= 0; i < num_fields; i++)
1908
2007
    {
1909
 
      if (strcmp(fields[i].name, ds_col.c_str()) == 0 &&
1910
 
          strlen(fields[i].name) == ds_col.length())
 
2008
      column= drizzle_column_next(&res);
 
2009
      if (strcmp(drizzle_column_name(column), ds_col.c_str()) == 0 &&
 
2010
          strlen(drizzle_column_name(column)) == ds_col.length())
1911
2011
      {
1912
2012
        col_no= i;
1913
2013
        break;
1915
2015
    }
1916
2016
    if (col_no == -1)
1917
2017
    {
1918
 
      drizzle_free_result(res);
 
2018
      drizzle_result_free(&res);
1919
2019
      die("Could not find column '%s' in the result of '%s'",
1920
2020
          ds_col.c_str(), ds_query.c_str());
1921
2021
    }
1923
2023
 
1924
2024
  {
1925
2025
    /* Get the value */
1926
 
    DRIZZLE_ROW row;
 
2026
    drizzle_row_t row;
1927
2027
    long rows= 0;
1928
2028
    const char* value= "No such row";
1929
2029
 
1930
 
    while ((row= drizzle_fetch_row(res)))
 
2030
    while ((row= drizzle_row_next(&res)))
1931
2031
    {
1932
2032
      if (++rows == row_no)
1933
2033
      {
1943
2043
    }
1944
2044
    eval_expr(var, value, 0);
1945
2045
  }
1946
 
  drizzle_free_result(res);
 
2046
  drizzle_result_free(&res);
1947
2047
 
1948
2048
  return;
1949
2049
}
1955
2055
  dest->int_dirty= src->int_dirty;
1956
2056
 
1957
2057
  /* Alloc/realloc data for str_val in dest */
1958
 
  if (dest->alloced_len < src->alloced_len &&
1959
 
      !(dest->str_val= dest->str_val
1960
 
        ? (char *)my_realloc(dest->str_val, src->alloced_len, MYF(MY_WME))
1961
 
        : (char *)my_malloc(src->alloced_len, MYF(MY_WME))))
1962
 
    die("Out of memory");
 
2058
  if (dest->alloced_len < src->alloced_len)
 
2059
  {
 
2060
    char *tmpptr= (char *)realloc(dest->str_val, src->alloced_len);
 
2061
    if (tmpptr == NULL)
 
2062
      die("Out of memory");
 
2063
    dest->str_val= tmpptr;
 
2064
  }
1963
2065
  else
1964
2066
    dest->alloced_len= src->alloced_len;
1965
2067
 
2011
2113
      static int MIN_VAR_ALLOC= 32;
2012
2114
      v->alloced_len = (new_val_len < MIN_VAR_ALLOC - 1) ?
2013
2115
        MIN_VAR_ALLOC : new_val_len + 1;
2014
 
      if (!(v->str_val =
2015
 
            v->str_val ? (char *)my_realloc(v->str_val, v->alloced_len+1,
2016
 
                                            MYF(MY_WME)) :
2017
 
            (char *)my_malloc(v->alloced_len+1, MYF(MY_WME))))
 
2116
      char *tmpptr= (char *)realloc(v->str_val, v->alloced_len+1);
 
2117
      if (tmpptr == NULL)
2018
2118
        die("Out of memory");
 
2119
      v->str_val= tmpptr;
2019
2120
    }
2020
2121
    v->str_val_len = new_val_len;
2021
2122
    memcpy(v->str_val, p, new_val_len);
2033
2134
 
2034
2135
  if (!test_if_hard_path(name))
2035
2136
  {
2036
 
    strxmov(buff, opt_basedir, name, NULL);
 
2137
    sprintf(buff,"%s%s",opt_basedir,name);
2037
2138
    name=buff;
2038
2139
  }
2039
2140
  fn_format(buff, name, "", "", MY_UNPACK_FILENAME);
2041
2142
  if (cur_file == file_stack_end)
2042
2143
    die("Source directives are nesting too deep");
2043
2144
  cur_file++;
2044
 
  if (!(cur_file->file = my_fopen(buff, O_RDONLY | FILE_BINARY, MYF(0))))
 
2145
  if (!(cur_file->file= fopen(buff, "r")))
2045
2146
  {
2046
2147
    cur_file--;
2047
2148
    die("Could not open '%s' for reading", buff);
2048
2149
  }
2049
 
  cur_file->file_name= my_strdup(buff, MYF(MY_FAE));
 
2150
  if (!(cur_file->file_name= strdup(buff)))
 
2151
    die("Out of memory");
2050
2152
  cur_file->lineno=1;
2051
2153
  return(0);
2052
2154
}
2086
2188
    ; /* Do nothing */
2087
2189
  else
2088
2190
  {
 
2191
    if (opt_testdir != NULL)
 
2192
    {
 
2193
      string testdir(opt_testdir);
 
2194
      if (testdir[testdir.length()] != '/')
 
2195
        testdir.append("/");
 
2196
      testdir.append(ds_filename);
 
2197
      ds_filename.swap(testdir);
 
2198
    }
2089
2199
    open_file(ds_filename.c_str());
2090
2200
  }
2091
2201
 
2197
2307
  error= pclose(res_file);
2198
2308
  if (error > 0)
2199
2309
  {
2200
 
    uint status= WEXITSTATUS(error), i;
 
2310
    uint32_t status= WEXITSTATUS(error), i;
2201
2311
    bool ok= 0;
2202
2312
 
2203
2313
    if (command->abort_on_error)
2415
2525
                     ' ');
2416
2526
 
2417
2527
  /* Parse what mode to set */
 
2528
  istringstream buff(ds_mode);
2418
2529
  if (ds_mode.length() != 4 ||
2419
 
      str2int(ds_mode.c_str(), 8, 0, INT_MAX, &mode) == NULL)
 
2530
      (buff >> mode).fail())
2420
2531
    die("You must write a 4 digit octal number for mode");
2421
2532
 
2422
2533
  handle_command_error(command, chmod(ds_file.c_str(), mode));
2466
2577
 
2467
2578
static void do_mkdir(struct st_command *command)
2468
2579
{
 
2580
  string ds_dirname;
2469
2581
  int error;
2470
 
  string ds_dirname;
2471
2582
  const struct command_arg mkdir_args[] = {
2472
2583
    {"dirname", ARG_STRING, true, &ds_dirname, "Directory to create"}
2473
2584
  };
2477
2588
                     mkdir_args, sizeof(mkdir_args)/sizeof(struct command_arg),
2478
2589
                     ' ');
2479
2590
 
2480
 
  error= my_mkdir(ds_dirname.c_str(), 0777, MYF(0)) != 0;
 
2591
  error= mkdir(ds_dirname.c_str(), (0777 & my_umask_dir)) != 0;
2481
2592
  handle_command_error(command, error);
2482
2593
  return;
2483
2594
}
2597
2708
 
2598
2709
  /* If no delimiter was provided, use EOF */
2599
2710
  if (ds_delimiter.length() == 0)
2600
 
    ds_delimiter= "EOF";
 
2711
    ds_delimiter.append("EOF");
2601
2712
 
2602
2713
  if (!append && access(ds_filename.c_str(), F_OK) == 0)
2603
2714
  {
2778
2889
{
2779
2890
  char *p= command->first_argument, *name;
2780
2891
  struct st_connection *con;
 
2892
  drizzle_result_st result;
 
2893
  drizzle_return_t ret;
2781
2894
 
2782
2895
  if (!*p)
2783
2896
    die("Missing connection name in send_quit");
2792
2905
  if (!(con= find_connection_by_name(name)))
2793
2906
    die("connection '%s' not found in connection pool", name);
2794
2907
 
2795
 
  simple_command(&con->drizzle,COM_QUIT,0,0,1);
 
2908
  if (drizzle_quit(&con->con,&result, &ret))
 
2909
    drizzle_result_free(&result);
2796
2910
 
2797
2911
  return;
2798
2912
}
2814
2928
 
2815
2929
*/
2816
2930
 
2817
 
static void do_change_user(struct st_command *command)
 
2931
static void do_change_user(struct st_command *)
2818
2932
{
2819
 
  DRIZZLE *drizzle= &cur_con->drizzle;
2820
 
  /* static keyword to make the NetWare compiler happy. */
2821
 
  string ds_user, ds_passwd, ds_db;
2822
 
  const struct command_arg change_user_args[] = {
2823
 
    { "user", ARG_STRING, false, &ds_user, "User to connect as" },
2824
 
    { "password", ARG_STRING, false, &ds_passwd, "Password used when connecting" },
2825
 
    { "database", ARG_STRING, false, &ds_db, "Database to select after connect" },
2826
 
  };
2827
 
 
2828
 
 
2829
 
 
2830
 
  check_command_args(command, command->first_argument,
2831
 
                     change_user_args,
2832
 
                     sizeof(change_user_args)/sizeof(struct command_arg),
2833
 
                     ',');
2834
 
 
2835
 
  if (!ds_user.length())
2836
 
    ds_user= drizzle->user;
2837
 
 
2838
 
  if (!ds_passwd.length())
2839
 
    ds_passwd= drizzle->passwd;
2840
 
 
2841
 
  if (!ds_db.length())
2842
 
    ds_db= drizzle->db;
2843
 
 
2844
 
  if (drizzle_change_user(drizzle, ds_user.c_str(),
2845
 
                          ds_passwd.c_str(), ds_db.c_str()))
2846
 
    die("change user failed: %s", drizzle_error(drizzle));
2847
 
 
2848
 
 
2849
 
  return;
 
2933
  assert(0);
2850
2934
}
2851
2935
 
2852
 
 
2853
2936
/*
2854
2937
  SYNOPSIS
2855
2938
  do_perl
2891
2974
 
2892
2975
  /* If no delimiter was provided, use EOF */
2893
2976
  if (ds_delimiter.length() == 0)
2894
 
    ds_delimiter= "EOF";
 
2977
    ds_delimiter.append("EOF");
2895
2978
 
2896
2979
  read_until_delimiter(&ds_script, &ds_delimiter);
2897
2980
 
2898
2981
  /* Create temporary file name */
2899
2982
  if ((fd= create_temp_file(temp_file_path, getenv("MYSQLTEST_VARDIR"),
2900
 
                            "tmp", O_CREAT | O_SHARE | O_RDWR,
2901
 
                            MYF(MY_WME))) < 0)
 
2983
                            "tmp", MYF(MY_WME))) < 0)
2902
2984
    die("Failed to create temporary file for perl command");
2903
2985
  my_close(fd, MYF(0));
2904
2986
 
2965
3047
 
2966
3048
 
2967
3049
static void
2968
 
do_wait_for_slave_to_stop(struct st_command *c __attribute__((unused)))
 
3050
do_wait_for_slave_to_stop(struct st_command *)
2969
3051
{
2970
3052
  static int SLAVE_POLL_INTERVAL= 300000;
2971
 
  DRIZZLE *drizzle= &cur_con->drizzle;
 
3053
  drizzle_con_st *con= &cur_con->con;
2972
3054
  for (;;)
2973
3055
  {
2974
 
    DRIZZLE_RES *res= NULL;
2975
 
    DRIZZLE_ROW row;
 
3056
    drizzle_result_st res;
 
3057
    drizzle_return_t ret;
 
3058
    drizzle_row_t row;
2976
3059
    int done;
2977
3060
 
2978
 
    if (drizzle_query(drizzle,"show status like 'Slave_running'") ||
2979
 
        !(res=drizzle_store_result(drizzle)))
 
3061
    if (drizzle_query_str(con,&res,"show status like 'Slave_running'",
 
3062
                          &ret) == NULL || ret != DRIZZLE_RETURN_OK)
 
3063
    {
 
3064
      if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
3065
      {
 
3066
        die("Query failed while probing slave for stop: %s",
 
3067
            drizzle_result_error(&res));
 
3068
        drizzle_result_free(&res);
 
3069
      }
 
3070
      else
 
3071
      {
 
3072
        die("Query failed while probing slave for stop: %s",
 
3073
            drizzle_con_error(con));
 
3074
      }
 
3075
    }
 
3076
 
 
3077
    if (drizzle_result_column_count(&res) == 0 ||
 
3078
        drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
 
3079
    {
2980
3080
      die("Query failed while probing slave for stop: %s",
2981
 
          drizzle_error(drizzle));
2982
 
    if (!(row=drizzle_fetch_row(res)) || !row[1])
 
3081
          drizzle_con_error(con));
 
3082
    }
 
3083
 
 
3084
    if (!(row=drizzle_row_next(&res)) || !row[1])
2983
3085
    {
2984
 
      drizzle_free_result(res);
 
3086
      drizzle_result_free(&res);
2985
3087
      die("Strange result from query while probing slave for stop");
2986
3088
    }
2987
3089
    done = !strcmp(row[1],"OFF");
2988
 
    drizzle_free_result(res);
 
3090
    drizzle_result_free(&res);
2989
3091
    if (done)
2990
3092
      break;
2991
 
    my_sleep(SLAVE_POLL_INTERVAL);
 
3093
    usleep(SLAVE_POLL_INTERVAL);
2992
3094
  }
2993
3095
  return;
2994
3096
}
2996
3098
 
2997
3099
static void do_sync_with_master2(long offset)
2998
3100
{
2999
 
  DRIZZLE_RES *res;
3000
 
  DRIZZLE_ROW row;
3001
 
  DRIZZLE *drizzle= &cur_con->drizzle;
 
3101
  drizzle_result_st res;
 
3102
  drizzle_return_t ret;
 
3103
  drizzle_row_t row;
 
3104
  drizzle_con_st *con= &cur_con->con;
3002
3105
  char query_buf[FN_REFLEN+128];
3003
3106
  int tries= 0;
3004
3107
 
3010
3113
 
3011
3114
wait_for_position:
3012
3115
 
3013
 
  if (drizzle_query(drizzle, query_buf))
3014
 
    die("failed in '%s': %d: %s", query_buf, drizzle_errno(drizzle),
3015
 
        drizzle_error(drizzle));
3016
 
 
3017
 
  if (!(res= drizzle_store_result(drizzle)))
3018
 
    die("drizzle_store_result() returned NULL for '%s'", query_buf);
3019
 
  if (!(row= drizzle_fetch_row(res)))
3020
 
  {
3021
 
    drizzle_free_result(res);
 
3116
  if (drizzle_query_str(con, &res, query_buf, &ret) == NULL ||
 
3117
      ret != DRIZZLE_RETURN_OK)
 
3118
  {
 
3119
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
3120
    {
 
3121
      die("failed in '%s': %d: %s", query_buf, drizzle_result_error_code(&res),
 
3122
           drizzle_result_error(&res));
 
3123
      drizzle_result_free(&res);
 
3124
    }
 
3125
    else
 
3126
      die("failed in '%s': %d: %s", query_buf, ret, drizzle_con_error(con));
 
3127
  }
 
3128
 
 
3129
  if (drizzle_result_column_count(&res) == 0 ||
 
3130
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
 
3131
    die("drizzle_result_buffer() returned NULL for '%s'", query_buf);
 
3132
 
 
3133
  if (!(row= drizzle_row_next(&res)))
 
3134
  {
 
3135
    drizzle_result_free(&res);
3022
3136
    die("empty result in %s", query_buf);
3023
3137
  }
3024
3138
  if (!row[0])
3027
3141
      It may be that the slave SQL thread has not started yet, though START
3028
3142
      SLAVE has been issued ?
3029
3143
    */
3030
 
    drizzle_free_result(res);
 
3144
    drizzle_result_free(&res);
3031
3145
    if (tries++ == 30)
3032
3146
    {
3033
 
      show_query(drizzle, "SHOW MASTER STATUS");
3034
 
      show_query(drizzle, "SHOW SLAVE STATUS");
 
3147
      show_query(con, "SHOW MASTER STATUS");
 
3148
      show_query(con, "SHOW SLAVE STATUS");
3035
3149
      die("could not sync with master ('%s' returned NULL)", query_buf);
3036
3150
    }
3037
3151
    sleep(1); /* So at most we will wait 30 seconds and make 31 tries */
3038
3152
    goto wait_for_position;
3039
3153
  }
3040
 
  drizzle_free_result(res);
 
3154
  drizzle_result_free(&res);
3041
3155
  return;
3042
3156
}
3043
3157
 
3067
3181
*/
3068
3182
static int do_save_master_pos(void)
3069
3183
{
3070
 
  DRIZZLE_RES *res;
3071
 
  DRIZZLE_ROW row;
3072
 
  DRIZZLE *drizzle= &cur_con->drizzle;
 
3184
  drizzle_result_st res;
 
3185
  drizzle_return_t ret;
 
3186
  drizzle_row_t row;
 
3187
  drizzle_con_st *con= &cur_con->con;
3073
3188
  const char *query;
3074
3189
 
3075
3190
 
3076
 
  if (drizzle_query(drizzle, query= "show master status"))
3077
 
    die("failed in 'show master status': %d %s",
3078
 
        drizzle_errno(drizzle), drizzle_error(drizzle));
 
3191
  if (drizzle_query_str(con, &res, query= "show master status", &ret) == NULL ||
 
3192
      ret != DRIZZLE_RETURN_OK)
 
3193
  {
 
3194
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
3195
    {
 
3196
      die("failed in '%s': %d: %s", query, drizzle_result_error_code(&res),
 
3197
           drizzle_result_error(&res));
 
3198
      drizzle_result_free(&res);
 
3199
    }
 
3200
    else
 
3201
      die("failed in '%s': %d: %s", query, ret, drizzle_con_error(con));
 
3202
  }
3079
3203
 
3080
 
  if (!(res = drizzle_store_result(drizzle)))
3081
 
    die("drizzle_store_result() retuned NULL for '%s'", query);
3082
 
  if (!(row = drizzle_fetch_row(res)))
 
3204
  if (drizzle_result_column_count(&res) == 0 ||
 
3205
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
 
3206
    die("drizzleclient_store_result() retuned NULL for '%s'", query);
 
3207
  if (!(row = drizzle_row_next(&res)))
3083
3208
    die("empty result in show master status");
3084
 
  my_stpncpy(master_pos.file, row[0], sizeof(master_pos.file)-1);
 
3209
  strncpy(master_pos.file, row[0], sizeof(master_pos.file)-1);
3085
3210
  master_pos.pos = strtoul(row[1], (char**) 0, 10);
3086
 
  drizzle_free_result(res);
 
3211
  drizzle_result_free(&res);
3087
3212
  return(0);
3088
3213
}
3089
3214
 
3167
3292
 
3168
3293
static int do_sleep(struct st_command *command, bool real_sleep)
3169
3294
{
3170
 
  int error= 0;
 
3295
  bool error= false;
3171
3296
  char *p= command->first_argument;
3172
3297
  char *sleep_start, *sleep_end= command->end;
3173
3298
  double sleep_val;
3181
3306
  if (!my_isdigit(charset_info, *sleep_start))
3182
3307
    die("Invalid argument to %.*s \"%s\"", command->first_word_len,
3183
3308
        command->query,command->first_argument);
3184
 
  sleep_val= my_strtod(sleep_start, &sleep_end, &error);
 
3309
  string buff_str(sleep_start, sleep_end-sleep_start);
 
3310
  istringstream buff(buff_str);
 
3311
  error= (buff >> sleep_val).fail();
3185
3312
  if (error)
3186
3313
    die("Invalid argument to %.*s \"%s\"", command->first_word_len,
3187
3314
        command->query, command->first_argument);
3191
3318
    sleep_val= opt_sleep;
3192
3319
 
3193
3320
  if (sleep_val)
3194
 
    my_sleep((uint32_t) (sleep_val * 1000000L));
 
3321
    usleep((uint32_t) (sleep_val * 1000000L));
3195
3322
  command->last_argument= sleep_end;
3196
3323
  return 0;
3197
3324
}
3198
3325
 
3199
3326
 
3200
3327
static void do_get_file_name(struct st_command *command,
3201
 
                             char* dest, uint dest_max_len)
 
3328
                             char* dest, uint32_t dest_max_len)
3202
3329
{
3203
3330
  char *p= command->first_argument, *name;
3204
3331
  if (!*p)
3209
3336
  if (*p)
3210
3337
    *p++= 0;
3211
3338
  command->last_argument= p;
3212
 
  strmake(dest, name, dest_max_len - 1);
 
3339
  strncpy(dest, name, dest_max_len - 1);
3213
3340
}
3214
3341
 
3215
3342
 
3227
3354
  if(*p)
3228
3355
    *p++= 0;
3229
3356
  command->last_argument= p;
3230
 
  charset_info= get_charset_by_csname(charset_name,MY_CS_PRIMARY,MYF(MY_WME));
 
3357
  charset_info= get_charset_by_csname(charset_name, MY_CS_PRIMARY);
3231
3358
  if (!charset_info)
3232
3359
    abort_not_supported_test("Test requires charset '%s'", charset_name);
3233
3360
}
3234
3361
 
3235
 
static uint get_errcode_from_name(char *error_name, char *error_end)
 
3362
static uint32_t get_errcode_from_name(char *error_name, char *error_end)
3236
3363
{
3237
 
  /* SQL error as string */
3238
 
  st_error *e= global_error_names;
3239
 
 
3240
 
  /* Loop through the array of known error names */
3241
 
  for (; e->name; e++)
3242
 
  {
3243
 
    /*
3244
 
      If we get a match, we need to check the length of the name we
3245
 
      matched against in case it was longer than what we are checking
3246
 
      (as in ER_WRONG_VALUE vs. ER_WRONG_VALUE_COUNT).
3247
 
    */
3248
 
    if (!strncmp(error_name, e->name, (int) (error_end - error_name)) &&
3249
 
        (uint) strlen(e->name) == (uint) (error_end - error_name))
3250
 
    {
3251
 
      return(e->code);
3252
 
    }
3253
 
  }
3254
 
  if (!e->name)
3255
 
    die("Unknown SQL error name '%s'", error_name);
3256
 
  return(0);
 
3364
  size_t err_name_len= error_end - error_name;
 
3365
  string error_name_s(error_name, err_name_len);
 
3366
 
 
3367
  uint32_t code= global_error_names.getErrorCode(error_name_s);
 
3368
 
 
3369
  if (!code)
 
3370
    die("Unknown SQL error name '%s'", error_name_s.c_str());
 
3371
 
 
3372
  return(code);
3257
3373
}
3258
3374
 
3259
3375
static void do_get_errcodes(struct st_command *command)
3260
3376
{
3261
3377
  struct st_match_err *to= saved_expected_errors.err;
3262
3378
  char *p= command->first_argument;
3263
 
  uint count= 0;
 
3379
  uint32_t count= 0;
3264
3380
 
3265
3381
 
3266
3382
 
3286
3402
 
3287
3403
      /*
3288
3404
        SQLSTATE string
3289
 
        - Must be SQLSTATE_LENGTH long
 
3405
        - Must be DRIZZLE_MAX_SQLSTATE_SIZE long
3290
3406
        - May contain only digits[0-9] and _uppercase_ letters
3291
3407
      */
3292
3408
      p++; /* Step past the S */
3293
 
      if ((end - p) != SQLSTATE_LENGTH)
3294
 
        die("The sqlstate must be exactly %d chars long", SQLSTATE_LENGTH);
 
3409
      if ((end - p) != DRIZZLE_MAX_SQLSTATE_SIZE)
 
3410
        die("The sqlstate must be exactly %d chars long", DRIZZLE_MAX_SQLSTATE_SIZE);
3295
3411
 
3296
3412
      /* Check sqlstate string validity */
3297
3413
      while (*p && p < end)
3336
3452
      }
3337
3453
 
3338
3454
      /* Convert the sting to int */
3339
 
      if (!str2int(start, 10, (long) INT_MIN, (long) INT_MAX, &val))
 
3455
      istringstream buff(start);
 
3456
      if ((buff >> val).fail())
3340
3457
        die("Invalid argument to error: '%s'", command->first_argument);
3341
3458
 
3342
 
      to->code.errnum= (uint) val;
 
3459
      to->code.errnum= (uint32_t) val;
3343
3460
      to->type= ERR_ERRNO;
3344
3461
    }
3345
3462
    to++;
3446
3563
}
3447
3564
 
3448
3565
 
3449
 
static void set_reconnect(DRIZZLE *drizzle, int val)
 
3566
static void set_reconnect(drizzle_con_st *con, int val)
3450
3567
{
 
3568
  (void) con;
 
3569
  (void) val;
 
3570
/* XXX
3451
3571
  bool reconnect= val;
3452
3572
 
3453
 
  drizzle_options(drizzle, DRIZZLE_OPT_RECONNECT, (char *)&reconnect);
3454
 
 
3455
 
  return;
 
3573
  drizzleclient_options(drizzle, DRIZZLE_OPT_RECONNECT, (char *)&reconnect);
 
3574
*/
3456
3575
}
3457
3576
 
3458
3577
 
3461
3580
  if (!(cur_con= find_connection_by_name(name)))
3462
3581
    die("connection '%s' not found in connection pool", name);
3463
3582
 
3464
 
  /* Update $drizzle_get_server_version to that of current connection */
3465
 
  var_set_drizzle_get_server_version(&cur_con->drizzle);
 
3583
  /* Update $drizzleclient_get_server_version to that of current connection */
 
3584
  var_set_drizzleclient_get_server_version(&cur_con->con);
3466
3585
 
3467
3586
  return(0);
3468
3587
}
3504
3623
  if (!(con= find_connection_by_name(name)))
3505
3624
    die("connection '%s' not found in connection pool", name);
3506
3625
 
3507
 
  if (command->type == Q_DIRTY_CLOSE)
 
3626
  if (con->drizzle != NULL)
3508
3627
  {
3509
 
    if (con->drizzle.net.vio)
3510
 
    {
3511
 
      net_close(&(con->drizzle.net));
3512
 
    }
 
3628
    drizzle_free(con->drizzle);
 
3629
    con->drizzle= NULL;
3513
3630
  }
3514
 
 
3515
 
  drizzle_close(&con->drizzle);
3516
 
 
3517
 
  if (con->util_drizzle)
3518
 
    drizzle_close(con->util_drizzle);
3519
 
  con->util_drizzle= 0;
3520
 
 
3521
3631
  free(con->name);
3522
3632
 
3523
3633
  /*
3524
3634
    When the connection is closed set name to "-closed_connection-"
3525
3635
    to make it possible to reuse the connection name.
3526
3636
  */
3527
 
  if (!(con->name = my_strdup("-closed_connection-", MYF(MY_WME))))
 
3637
  if (!(con->name = strdup("-closed_connection-")))
3528
3638
    die("Out of memory");
3529
3639
 
3530
3640
  return;
3556
3666
 
3557
3667
*/
3558
3668
 
3559
 
static void safe_connect(DRIZZLE *drizzle, const char *name, const char *host,
3560
 
                         const char *user, const char *pass, const char *db,
3561
 
                         int port)
 
3669
static void safe_connect(drizzle_con_st *con, const char *name,
 
3670
                         const char *host, const char *user, const char *pass,
 
3671
                         const char *db, int port)
3562
3672
{
3563
3673
  int failed_attempts= 0;
3564
3674
  static uint32_t connection_retry_sleep= 100000; /* Microseconds */
3565
 
 
3566
 
 
3567
 
  while(!drizzle_connect(drizzle, host, user, pass, db, port, NULL,
3568
 
                         CLIENT_MULTI_STATEMENTS | CLIENT_REMEMBER_OPTIONS))
 
3675
  drizzle_return_t ret;
 
3676
 
 
3677
  drizzle_con_set_tcp(con, host, port);
 
3678
  drizzle_con_set_auth(con, user, pass);
 
3679
  drizzle_con_set_db(con, db);
 
3680
  while((ret= drizzle_con_connect(con)) != DRIZZLE_RETURN_OK)
3569
3681
  {
3570
3682
    /*
3571
3683
      Connect failed
3575
3687
      on protocol/connection type
3576
3688
    */
3577
3689
 
3578
 
    if ((drizzle_errno(drizzle) == CR_CONN_HOST_ERROR ||
3579
 
         drizzle_errno(drizzle) == CR_CONNECTION_ERROR) &&
 
3690
    if ((ret == DRIZZLE_RETURN_GETADDRINFO ||
 
3691
         ret == DRIZZLE_RETURN_COULD_NOT_CONNECT) &&
3580
3692
        failed_attempts < opt_max_connect_retries)
3581
3693
    {
3582
3694
      verbose_msg("Connect attempt %d/%d failed: %d: %s", failed_attempts,
3583
 
                  opt_max_connect_retries, drizzle_errno(drizzle),
3584
 
                  drizzle_error(drizzle));
3585
 
      my_sleep(connection_retry_sleep);
 
3695
                  opt_max_connect_retries, ret, drizzle_con_error(con));
 
3696
      usleep(connection_retry_sleep);
3586
3697
    }
3587
3698
    else
3588
3699
    {
3589
3700
      if (failed_attempts > 0)
3590
3701
        die("Could not open connection '%s' after %d attempts: %d %s", name,
3591
 
            failed_attempts, drizzle_errno(drizzle), drizzle_error(drizzle));
 
3702
            failed_attempts, ret, drizzle_con_error(con));
3592
3703
      else
3593
 
        die("Could not open connection '%s': %d %s", name,
3594
 
            drizzle_errno(drizzle), drizzle_error(drizzle));
 
3704
        die("Could not open connection '%s': %d %s", name, ret,
 
3705
            drizzle_con_error(con));
3595
3706
    }
3596
3707
    failed_attempts++;
3597
3708
  }
3623
3734
*/
3624
3735
 
3625
3736
static int connect_n_handle_errors(struct st_command *command,
3626
 
                                   DRIZZLE *con, const char* host,
 
3737
                                   drizzle_con_st *con, const char* host,
3627
3738
                                   const char* user, const char* pass,
3628
3739
                                   const char* db, int port, const char* sock)
3629
3740
{
 
3741
  drizzle_return_t ret;
3630
3742
 
3631
3743
  /* Only log if an error is expected */
3632
3744
  if (!command->abort_on_error &&
3653
3765
    ds_res.append(delimiter);
3654
3766
    ds_res.append("\n");
3655
3767
  }
3656
 
  if (!drizzle_connect(con, host, user, pass, db, port, 0,
3657
 
                       CLIENT_MULTI_STATEMENTS))
 
3768
  drizzle_con_set_tcp(con, host, port);
 
3769
  drizzle_con_set_auth(con, user, pass);
 
3770
  drizzle_con_set_db(con, db);
 
3771
  if ((ret= drizzle_con_connect(con)) != DRIZZLE_RETURN_OK)
3658
3772
  {
3659
 
    var_set_errno(drizzle_errno(con));
3660
 
    handle_error(command, drizzle_errno(con), drizzle_error(con),
3661
 
                 drizzle_sqlstate(con), &ds_res);
 
3773
    if (ret == DRIZZLE_RETURN_HANDSHAKE_FAILED)
 
3774
    {
 
3775
      var_set_errno(drizzle_con_error_code(con));
 
3776
      handle_error(command, drizzle_con_error_code(con), drizzle_con_error(con),
 
3777
                   drizzle_con_sqlstate(con), &ds_res);
 
3778
    }
 
3779
    else
 
3780
    {
 
3781
      var_set_errno(ret);
 
3782
      handle_error(command, ret, drizzle_con_error(con), "", &ds_res);
 
3783
    }
 
3784
 
3662
3785
    return 0; /* Not connected */
3663
3786
  }
3664
3787
 
3744
3867
    {
3745
3868
      char buff[FN_REFLEN];
3746
3869
      fn_format(buff, ds_sock.c_str(), TMPDIR, "", 0);
3747
 
      ds_sock= buff;
 
3870
      ds_sock.clear();
 
3871
      ds_sock.append(buff);
3748
3872
    }
3749
3873
  }
3750
3874
 
3785
3909
          (int) (sizeof(connections)/sizeof(struct st_connection)));
3786
3910
  }
3787
3911
 
3788
 
  if (!drizzle_create(&con_slot->drizzle))
 
3912
  if ((con_slot->drizzle= drizzle_create(NULL)) == NULL)
3789
3913
    die("Failed on drizzle_create()");
3790
 
  if (opt_compress || con_compress)
3791
 
    drizzle_options(&con_slot->drizzle, DRIZZLE_OPT_COMPRESS, NULL);
3792
 
  drizzle_options(&con_slot->drizzle, DRIZZLE_OPT_LOCAL_INFILE, 0);
 
3914
  if (!drizzle_con_create(con_slot->drizzle, &con_slot->con))
 
3915
    die("Failed on drizzle_con_create()");
3793
3916
 
3794
3917
  /* Use default db name */
3795
3918
  if (ds_database.length() == 0)
3796
 
    ds_database= opt_db;
 
3919
    ds_database.append(opt_db);
3797
3920
 
3798
3921
  /* Special database to allow one to connect without a database name */
3799
3922
  if (ds_database.length() && !strcmp(ds_database.c_str(),"*NO-ONE*"))
3800
 
    ds_database= "";
 
3923
    ds_database.clear();
3801
3924
 
3802
 
  if (connect_n_handle_errors(command, &con_slot->drizzle,
 
3925
  if (connect_n_handle_errors(command, &con_slot->con,
3803
3926
                              ds_host.c_str(),ds_user.c_str(),
3804
3927
                              ds_password.c_str(), ds_database.c_str(),
3805
3928
                              con_port, ds_sock.c_str()))
3812
3935
      next_con++; /* if we used the next_con slot, advance the pointer */
3813
3936
  }
3814
3937
 
3815
 
  /* Update $drizzle_get_server_version to that of current connection */
3816
 
  var_set_drizzle_get_server_version(&cur_con->drizzle);
 
3938
  /* Update $drizzleclient_get_server_version to that of current connection */
 
3939
  var_set_drizzleclient_get_server_version(&cur_con->con);
3817
3940
 
3818
3941
  return;
3819
3942
}
3930
4053
  if (not_expr)
3931
4054
    cur_block->ok = !cur_block->ok;
3932
4055
 
3933
 
  var_free(&v);
 
4056
  free(v.str_val);
 
4057
  free(v.env_s);
 
4058
 
3934
4059
  return;
3935
4060
}
3936
4061
 
3945
4070
  if (!(*p))
3946
4071
    die("Can't set empty delimiter");
3947
4072
 
3948
 
  strmake(delimiter, p, sizeof(delimiter) - 1);
 
4073
  strncpy(delimiter, p, sizeof(delimiter) - 1);
3949
4074
  delimiter_length= strlen(delimiter);
3950
4075
 
3951
4076
  command->last_argument= p + delimiter_length;
3953
4078
}
3954
4079
 
3955
4080
 
3956
 
bool match_delimiter(int c, const char *delim, uint length)
 
4081
bool match_delimiter(int c, const char *delim, uint32_t length)
3957
4082
{
3958
 
  uint i;
 
4083
  uint32_t i;
3959
4084
  char tmp[MAX_DELIMITER_LENGTH];
3960
4085
 
3961
4086
  if (c != *delim)
4026
4151
  found_eof:
4027
4152
      if (cur_file->file != stdin)
4028
4153
      {
4029
 
        my_fclose(cur_file->file, MYF(0));
 
4154
        fclose(cur_file->file);
4030
4155
        cur_file->file= 0;
4031
4156
      }
4032
4157
      free((unsigned char*) cur_file->file_name);
4066
4191
      }
4067
4192
      else if ((c == '{' &&
4068
4193
                (!my_strnncoll_simple(charset_info, (const unsigned char*) "while", 5,
4069
 
                                      (unsigned char*) buf, cmin((long)5, p - buf), 0) ||
 
4194
                                      (unsigned char*) buf, min((ptrdiff_t)5, p - buf), 0) ||
4070
4195
                 !my_strnncoll_simple(charset_info, (const unsigned char*) "if", 2,
4071
 
                                      (unsigned char*) buf, cmin((long)2, p - buf), 0))))
 
4196
                                      (unsigned char*) buf, min((ptrdiff_t)2, p - buf), 0))))
4072
4197
      {
4073
4198
        /* Only if and while commands can be terminated by { */
4074
4199
        *p++= c;
4145
4270
    {
4146
4271
      /* Could be a multibyte character */
4147
4272
      /* This code is based on the code in "sql_load.cc" */
4148
 
#ifdef USE_MB
4149
4273
      int charlen = my_mbcharlen(charset_info, c);
4150
4274
      /* We give up if multibyte character is started but not */
4151
4275
      /* completed before we pass buf_end */
4172
4296
        }
4173
4297
      }
4174
4298
      else
4175
 
#endif
4176
4299
        *p++= c;
4177
4300
    }
4178
4301
  }
4255
4378
        ptr[2] && ptr[2] == '-' &&
4256
4379
        ptr[3])
4257
4380
    {
4258
 
      uint type;
 
4381
      uint32_t type;
4259
4382
      char save;
4260
4383
      char *end, *start= (char*)ptr+3;
4261
4384
      /* Skip leading spaces */
4366
4489
    return(0);
4367
4490
  }
4368
4491
  if (!(*command_ptr= command=
4369
 
        (struct st_command*) my_malloc(sizeof(*command),
4370
 
                                       MYF(MY_WME|MY_ZEROFILL))))
4371
 
    die(NULL);
 
4492
        (struct st_command*) malloc(sizeof(*command))))
 
4493
    die("command malloc failed");
 
4494
  memset(command, 0, sizeof(*command));
4372
4495
  q_lines.push_back(command);
4373
4496
  command->type= Q_UNKNOWN;
4374
4497
 
4395
4518
  while (*p && my_isspace(charset_info, *p))
4396
4519
    p++;
4397
4520
 
4398
 
  if (!(command->query_buf= command->query= my_strdup(p, MYF(MY_WME))))
 
4521
  if (!(command->query_buf= command->query= strdup(p)))
4399
4522
    die("Out of memory");
4400
4523
 
4401
4524
  /* Calculate first word length(the command), terminated by space or ( */
4402
4525
  p= command->query;
4403
4526
  while (*p && !my_isspace(charset_info, *p) && *p != '(')
4404
4527
    p++;
4405
 
  command->first_word_len= (uint) (p - command->query);
 
4528
  command->first_word_len= (uint32_t) (p - command->query);
4406
4529
 
4407
4530
  /* Skip spaces between command and first argument */
4408
4531
  while (*p && my_isspace(charset_info, *p))
4440
4563
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
4441
4564
  {"include", 'i', "Include SQL before each test case.", (char**) &opt_include,
4442
4565
   (char**) &opt_include, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4566
  {"testdir", OPT_TESTDIR, "Path to use to search for test files",
 
4567
   (char**) &opt_testdir,
 
4568
   (char**) &opt_testdir, 0,GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
4443
4569
  {"logdir", OPT_LOG_DIR, "Directory for log files", (char**) &opt_logdir,
4444
4570
   (char**) &opt_logdir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
4445
4571
  {"mark-progress", OPT_MARK_PROGRESS,
4450
4576
   "Max number of connection attempts when connecting to server",
4451
4577
   (char**) &opt_max_connect_retries, (char**) &opt_max_connect_retries, 0,
4452
4578
   GET_INT, REQUIRED_ARG, 500, 1, 10000, 0, 0, 0},
4453
 
  {"password", 'p', "Password to use when connecting to server.",
 
4579
  {"password", 'P', "Password to use when connecting to server.",
4454
4580
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
4455
 
  {"port", 'P', "Port number to use for connection or 0 for default to, in "
4456
 
   "order of preference, my.cnf, $DRIZZLE_TCP_PORT, "
 
4581
  {"port", 'p', "Port number to use for connection or 0 for default to, in "
 
4582
   "order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, "
4457
4583
   "built-in default (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
4458
 
   (char**) &opt_port,
4459
 
   (char**) &opt_port, 0, GET_INT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4584
   0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
4460
4585
  {"quiet", 's', "Suppress all normal output.", (char**) &silent,
4461
4586
   (char**) &silent, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
4462
4587
  {"record", 'r', "Record output of test_file into result file.",
4495
4620
 
4496
4621
static void print_version(void)
4497
4622
{
4498
 
  printf("%s  Ver %s Distrib %s, for %s (%s)\n",my_progname,MTEST_VERSION,
4499
 
         drizzle_get_client_info(),SYSTEM_TYPE,MACHINE_TYPE);
 
4623
  printf("%s  Ver %s Distrib %s, for %s-%s (%s)\n",my_progname,MTEST_VERSION,
 
4624
         drizzle_version(),HOST_VENDOR,HOST_OS,HOST_CPU);
4500
4625
}
4501
4626
 
4502
4627
static void usage(void)
4523
4648
 
4524
4649
  if (!test_if_hard_path(name))
4525
4650
  {
4526
 
    strxmov(buff, opt_basedir, name, NULL);
 
4651
    sprintf(buff,"%s%s",opt_basedir,name);
4527
4652
    name=buff;
4528
4653
  }
4529
4654
  fn_format(buff, name, "", "", MY_UNPACK_FILENAME);
4533
4658
    embedded_server_arg_count=1;
4534
4659
    embedded_server_args[0]= (char*) "";    /* Progname */
4535
4660
  }
4536
 
  if (!(file=my_fopen(buff, O_RDONLY | FILE_BINARY, MYF(MY_WME))))
 
4661
  if (!(file= fopen(buff, "r")))
4537
4662
    die("Failed to open file '%s'", buff);
4538
4663
 
4539
4664
  while (embedded_server_arg_count < MAX_EMBEDDED_SERVER_ARGS &&
4541
4666
  {
4542
4667
    *(strchr(str, '\0')-1)=0;        /* Remove end newline */
4543
4668
    if (!(embedded_server_args[embedded_server_arg_count]=
4544
 
          (char*) my_strdup(str,MYF(MY_WME))))
 
4669
          (char*) strdup(str)))
4545
4670
    {
4546
 
      my_fclose(file,MYF(0));
 
4671
      fclose(file);
4547
4672
      die("Out of memory");
4548
4673
 
4549
4674
    }
4550
4675
    embedded_server_arg_count++;
4551
4676
  }
4552
 
  my_fclose(file,MYF(0));
 
4677
  fclose(file);
4553
4678
  if (str)
4554
4679
    die("Too many arguments in option file: %s",name);
4555
4680
 
4557
4682
}
4558
4683
 
4559
4684
 
4560
 
static bool
4561
 
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
4562
 
               char *argument)
 
4685
bool get_one_option(int optid, const struct my_option *, char *argument)
4563
4686
{
 
4687
  char *endchar= NULL;
 
4688
  uint64_t temp_drizzle_port= 0;
 
4689
 
4564
4690
  switch(optid) {
4565
4691
  case 'r':
4566
4692
    record = 1;
4570
4696
    char buff[FN_REFLEN];
4571
4697
    if (!test_if_hard_path(argument))
4572
4698
    {
4573
 
      strxmov(buff, opt_basedir, argument, NULL);
 
4699
      sprintf(buff,"%s%s",opt_basedir,argument);
4574
4700
      argument= buff;
4575
4701
    }
4576
4702
    fn_format(buff, argument, "", "", MY_UNPACK_FILENAME);
4577
4703
    assert(cur_file == file_stack && cur_file->file == 0);
4578
 
    if (!(cur_file->file=
4579
 
          my_fopen(buff, O_RDONLY | FILE_BINARY, MYF(0))))
 
4704
    if (!(cur_file->file= fopen(buff, "r")))
4580
4705
      die("Could not open '%s' for reading: errno = %d", buff, errno);
4581
 
    cur_file->file_name= my_strdup(buff, MYF(MY_FAE));
 
4706
    if (!(cur_file->file_name= strdup(buff)))
 
4707
      die("Out of memory");
4582
4708
    cur_file->lineno= 1;
4583
4709
    break;
4584
4710
  }
4587
4713
    static char buff[FN_REFLEN];
4588
4714
    if (!test_if_hard_path(argument))
4589
4715
    {
4590
 
      strxmov(buff, opt_basedir, argument, NULL);
 
4716
      sprintf(buff,"%s%s",opt_basedir,argument);
4591
4717
      argument= buff;
4592
4718
    }
4593
4719
    fn_format(buff, argument, "", "", MY_UNPACK_FILENAME);
4596
4722
    break;
4597
4723
  }
4598
4724
  case 'p':
 
4725
    temp_drizzle_port= (uint64_t) strtoul(argument, &endchar, 10);
 
4726
    /* if there is an alpha character this is not a valid port */
 
4727
    if (strlen(endchar) != 0)
 
4728
    {
 
4729
      fprintf(stderr, _("Non-integer value supplied for port.  If you are trying to enter a password please use --password instead.\n"));
 
4730
      exit(1);
 
4731
    }
 
4732
    /* If the port number is > 65535 it is not a valid port
 
4733
       This also helps with potential data loss casting unsigned long to a
 
4734
       uint32_t. */
 
4735
    if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
 
4736
    {
 
4737
      fprintf(stderr, _("Value supplied for port is not valid.\n"));
 
4738
      exit(1);
 
4739
    }
 
4740
    else
 
4741
    {
 
4742
      opt_port= (uint32_t) temp_drizzle_port;
 
4743
    }
 
4744
    break;
 
4745
  case 'P':
4599
4746
    if (argument)
4600
4747
    {
4601
 
      free(opt_pass);
4602
 
      opt_pass= my_strdup(argument, MYF(MY_FAE));
4603
 
      while (*argument) *argument++= 'x';    /* Destroy argument */
 
4748
      if (opt_pass)
 
4749
        free(opt_pass);
 
4750
      opt_pass = strdup(argument);
 
4751
      if (opt_pass == NULL)
 
4752
        die("Out of memory");
 
4753
      while (*argument)
 
4754
      {
 
4755
        /* Overwriting password with 'x' */
 
4756
        *argument++= 'x';
 
4757
      }
4604
4758
      tty_password= 0;
4605
4759
    }
4606
4760
    else
4607
4761
      tty_password= 1;
4608
4762
    break;
4609
4763
  case 't':
4610
 
    my_stpncpy(TMPDIR, argument, sizeof(TMPDIR));
 
4764
    strncpy(TMPDIR, argument, sizeof(TMPDIR));
4611
4765
    break;
4612
4766
  case 'A':
4613
4767
    if (!embedded_server_arg_count)
4617
4771
    }
4618
4772
    if (embedded_server_arg_count == MAX_EMBEDDED_SERVER_ARGS-1 ||
4619
4773
        !(embedded_server_args[embedded_server_arg_count++]=
4620
 
          my_strdup(argument, MYF(MY_FAE))))
 
4774
          strdup(argument)))
4621
4775
    {
4622
4776
      die("Can't use server argument");
4623
4777
    }
4638
4792
 
4639
4793
static int parse_args(int argc, char **argv)
4640
4794
{
4641
 
  load_defaults("my",load_default_groups,&argc,&argv);
 
4795
  load_defaults("drizzle",load_default_groups,&argc,&argv);
4642
4796
  default_argv= argv;
4643
4797
 
4644
4798
  if ((handle_options(&argc, &argv, my_long_options, get_one_option)))
4652
4806
  if (argc == 1)
4653
4807
    opt_db= *argv;
4654
4808
  if (tty_password)
4655
 
    opt_pass= get_tty_password(NULL);          /* purify tested */
 
4809
    opt_pass= client_get_tty_password(NULL);          /* purify tested */
4656
4810
  if (debug_info_flag)
4657
4811
    my_end_arg= MY_CHECK_ERROR | MY_GIVE_INFO;
4658
4812
  if (debug_check_flag)
4679
4833
  int flags= O_WRONLY | O_CREAT;
4680
4834
  if (!test_if_hard_path(fname))
4681
4835
  {
4682
 
    strxmov(buff, opt_basedir, fname, NULL);
 
4836
    sprintf(buff,"%s%s",opt_basedir,fname);
4683
4837
    fname= buff;
4684
4838
  }
4685
4839
  fn_format(buff, fname, "", "", MY_UNPACK_FILENAME);
4689
4843
  if ((fd= my_open(buff, flags,
4690
4844
                   MYF(MY_WME | MY_FFNF))) < 0)
4691
4845
    die("Could not open '%s' for writing: errno = %d", buff, errno);
4692
 
  if (append && my_seek(fd, 0, SEEK_END, MYF(0)) == MY_FILEPOS_ERROR)
 
4846
  if (append && lseek(fd, 0, SEEK_END) == MY_FILEPOS_ERROR)
4693
4847
    die("Could not find end of file '%s': errno = %d", buff, errno);
4694
4848
  if (my_write(fd, (unsigned char*)str, size, MYF(MY_WME|MY_FNABP)))
4695
4849
    die("write failed");
4748
4902
  Append the result for one field to the dynamic string ds
4749
4903
*/
4750
4904
 
4751
 
static void append_field(string *ds, uint col_idx, const DRIZZLE_FIELD* field,
 
4905
static void append_field(string *ds, uint32_t col_idx, drizzle_column_st *column,
4752
4906
                         const char* val, uint64_t len, bool is_null)
4753
4907
{
4754
4908
  if (col_idx < max_replace_column && replace_column[col_idx])
4770
4924
  }
4771
4925
  else
4772
4926
  {
4773
 
    ds->append(field->name);
 
4927
    ds->append(drizzle_column_name(column));
4774
4928
    ds->append("\t");
4775
4929
    replace_append_mem(ds, val, (int)len);
4776
4930
    ds->append("\n");
4783
4937
  Values may be converted with 'replace_column'
4784
4938
*/
4785
4939
 
4786
 
static void append_result(string *ds, DRIZZLE_RES *res)
 
4940
static void append_result(string *ds, drizzle_result_st *res)
4787
4941
{
4788
 
  DRIZZLE_ROW row;
4789
 
  uint32_t num_fields= drizzle_num_fields(res);
4790
 
  const DRIZZLE_FIELD *fields= drizzle_fetch_fields(res);
4791
 
  uint32_t *lengths;
 
4942
  drizzle_row_t row;
 
4943
  uint32_t num_fields= drizzle_result_column_count(res);
 
4944
  drizzle_column_st *column;
 
4945
  size_t *lengths;
4792
4946
 
4793
 
  while ((row = drizzle_fetch_row(res)))
 
4947
  while ((row = drizzle_row_next(res)))
4794
4948
  {
4795
4949
    uint32_t i;
4796
 
    lengths = drizzle_fetch_lengths(res);
 
4950
    lengths = drizzle_row_field_sizes(res);
 
4951
    drizzle_column_seek(res, 0);
4797
4952
    for (i = 0; i < num_fields; i++)
4798
 
      append_field(ds, i, &fields[i],
 
4953
    {
 
4954
      column= drizzle_column_next(res);
 
4955
      append_field(ds, i, column,
4799
4956
                   (const char*)row[i], lengths[i], !row[i]);
 
4957
    }
4800
4958
    if (!display_result_vertically)
4801
4959
      ds->append("\n");
4802
4960
 
4808
4966
  Append metadata for fields to output
4809
4967
*/
4810
4968
 
4811
 
static void append_metadata(string *ds,
4812
 
                            const DRIZZLE_FIELD *field,
4813
 
                            uint num_fields)
 
4969
static void append_metadata(string *ds, drizzle_result_st *res)
4814
4970
{
4815
 
  const DRIZZLE_FIELD *field_end;
 
4971
  drizzle_column_st *column;
4816
4972
  ds->append("Catalog\tDatabase\tTable\tTable_alias\tColumn\t"
4817
4973
             "Column_alias\tType\tLength\tMax length\tIs_null\t"
4818
4974
             "Flags\tDecimals\tCharsetnr\n");
4819
4975
 
4820
 
  for (field_end= field+num_fields ;
4821
 
       field < field_end ;
4822
 
       field++)
 
4976
  drizzle_column_seek(res, 0);
 
4977
  while ((column= drizzle_column_next(res)))
4823
4978
  {
4824
 
    ds->append(field->catalog,
4825
 
               field->catalog_length);
4826
 
    ds->append("\t", 1);
4827
 
    ds->append(field->db, field->db_length);
4828
 
    ds->append("\t", 1);
4829
 
    ds->append(field->org_table,
4830
 
               field->org_table_length);
4831
 
    ds->append("\t", 1);
4832
 
    ds->append(field->table,
4833
 
               field->table_length);
4834
 
    ds->append("\t", 1);
4835
 
    ds->append(field->org_name,
4836
 
               field->org_name_length);
4837
 
    ds->append("\t", 1);
4838
 
    ds->append(field->name, field->name_length);
4839
 
    ds->append("\t", 1);
4840
 
    replace_append_uint(ds, field->type);
4841
 
    ds->append("\t", 1);
4842
 
    replace_append_uint(ds, field->length);
4843
 
    ds->append("\t", 1);
4844
 
    replace_append_uint(ds, field->max_length);
4845
 
    ds->append("\t", 1);
4846
 
    ds->append((char*) ((field->flags & NOT_NULL_FLAG) ?
4847
 
                        "N" : "Y"), 1);
4848
 
    ds->append("\t", 1);
4849
 
    replace_append_uint(ds, field->flags);
4850
 
    ds->append("\t", 1);
4851
 
    replace_append_uint(ds, field->decimals);
4852
 
    ds->append("\t", 1);
4853
 
    replace_append_uint(ds, field->charsetnr);
 
4979
    ds->append(drizzle_column_catalog(column),
 
4980
               strlen(drizzle_column_catalog(column)));
 
4981
    ds->append("\t", 1);
 
4982
    ds->append(drizzle_column_db(column), strlen(drizzle_column_db(column)));
 
4983
    ds->append("\t", 1);
 
4984
    ds->append(drizzle_column_orig_table(column),
 
4985
               strlen(drizzle_column_orig_table(column)));
 
4986
    ds->append("\t", 1);
 
4987
    ds->append(drizzle_column_table(column),
 
4988
               strlen(drizzle_column_table(column)));
 
4989
    ds->append("\t", 1);
 
4990
    ds->append(drizzle_column_orig_name(column),
 
4991
               strlen(drizzle_column_orig_name(column)));
 
4992
    ds->append("\t", 1);
 
4993
    ds->append(drizzle_column_name(column),
 
4994
               strlen(drizzle_column_name(column)));
 
4995
    ds->append("\t", 1);
 
4996
    replace_append_uint(ds, drizzle_column_type_drizzle(column));
 
4997
    ds->append("\t", 1);
 
4998
    replace_append_uint(ds, drizzle_column_size(column));
 
4999
    ds->append("\t", 1);
 
5000
    replace_append_uint(ds, drizzle_column_max_size(column));
 
5001
    ds->append("\t", 1);
 
5002
    ds->append((char*) ((drizzle_column_flags(column) & DRIZZLE_COLUMN_FLAGS_NOT_NULL) ? "N" : "Y"), 1);
 
5003
    ds->append("\t", 1);
 
5004
    replace_append_uint(ds, drizzle_column_flags(column));
 
5005
    ds->append("\t", 1);
 
5006
    replace_append_uint(ds, drizzle_column_decimals(column));
 
5007
    ds->append("\t", 1);
 
5008
    replace_append_uint(ds, drizzle_column_charset(column));
4854
5009
    ds->append("\n", 1);
4855
5010
  }
4856
5011
}
4863
5018
static void append_info(string *ds, uint64_t affected_rows,
4864
5019
                        const char *info)
4865
5020
{
4866
 
  char buf[40], buff2[21];
4867
 
  sprintf(buf,"affected rows: %s\n", llstr(affected_rows, buff2));
4868
 
  ds->append(buf);
4869
 
  if (info)
 
5021
  ostringstream buf;
 
5022
  buf << "affected rows: " << affected_rows << endl;
 
5023
  ds->append(buf.str());
 
5024
  if (info && strcmp(info, ""))
4870
5025
  {
4871
5026
    ds->append("info: ");
4872
5027
    ds->append(info);
4879
5034
  Display the table headings with the names tab separated
4880
5035
*/
4881
5036
 
4882
 
static void append_table_headings(string *ds,
4883
 
                                  const DRIZZLE_FIELD *field,
4884
 
                                  uint num_fields)
 
5037
static void append_table_headings(string *ds, drizzle_result_st *res)
4885
5038
{
4886
 
  uint col_idx;
4887
 
  for (col_idx= 0; col_idx < num_fields; col_idx++)
 
5039
  uint32_t col_idx= 0;
 
5040
  drizzle_column_st *column;
 
5041
  drizzle_column_seek(res, 0);
 
5042
  while ((column= drizzle_column_next(res)))
4888
5043
  {
4889
5044
    if (col_idx)
4890
5045
      ds->append("\t", 1);
4891
 
    replace_append(ds, field[col_idx].name);
 
5046
    replace_append(ds, drizzle_column_name(column));
 
5047
    col_idx++;
4892
5048
  }
4893
5049
  ds->append("\n", 1);
4894
5050
}
4900
5056
  Number of warnings appended to ds
4901
5057
*/
4902
5058
 
4903
 
static int append_warnings(string *ds, DRIZZLE *drizzle)
 
5059
static int append_warnings(string *ds, drizzle_con_st *con,
 
5060
                           drizzle_result_st *res)
4904
5061
{
4905
 
  uint count;
4906
 
  DRIZZLE_RES *warn_res;
4907
 
 
4908
 
 
4909
 
  if (!(count= drizzle_warning_count(drizzle)))
 
5062
  uint32_t count;
 
5063
  drizzle_result_st warn_res;
 
5064
  drizzle_return_t ret;
 
5065
 
 
5066
 
 
5067
  if (!(count= drizzle_result_warning_count(res)))
4910
5068
    return(0);
4911
5069
 
4912
 
  /*
4913
 
    If one day we will support execution of multi-statements
4914
 
    through PS API we should not issue SHOW WARNINGS until
4915
 
    we have not read all results...
4916
 
  */
4917
 
  assert(!drizzle_more_results(drizzle));
4918
 
 
4919
 
  if (drizzle_real_query(drizzle, "SHOW WARNINGS", 13))
4920
 
    die("Error running query \"SHOW WARNINGS\": %s", drizzle_error(drizzle));
4921
 
 
4922
 
  if (!(warn_res= drizzle_store_result(drizzle)))
4923
 
    die("Warning count is %u but didn't get any warnings",
4924
 
        count);
4925
 
 
4926
 
  append_result(ds, warn_res);
 
5070
  if (drizzle_query_str(con, &warn_res, "SHOW WARNINGS", &ret) == NULL ||
 
5071
      ret != DRIZZLE_RETURN_OK)
 
5072
  {
 
5073
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
5074
      die("Error running query \"SHOW WARNINGS\": %s", drizzle_result_error(&warn_res));
 
5075
    else
 
5076
      die("Error running query \"SHOW WARNINGS\": %s", drizzle_con_error(con));
 
5077
  }
 
5078
 
 
5079
  if (drizzle_result_column_count(&warn_res) == 0 ||
 
5080
      drizzle_result_buffer(&warn_res) != DRIZZLE_RETURN_OK)
 
5081
    die("Warning count is %u but didn't get any warnings", count);
 
5082
 
 
5083
  append_result(ds, &warn_res);
 
5084
  drizzle_result_free(&warn_res);
4927
5085
 
4928
5086
  return(count);
4929
5087
}
4947
5105
                             int flags, char *query, int query_len,
4948
5106
                             string *ds, string *ds_warnings)
4949
5107
{
4950
 
  DRIZZLE_RES *res= 0;
4951
 
  DRIZZLE *drizzle= &cn->drizzle;
4952
 
  int err= 0, counter= 0;
 
5108
  drizzle_result_st res;
 
5109
  drizzle_return_t ret;
 
5110
  drizzle_con_st *con= &cn->con;
 
5111
  int err= 0;
 
5112
 
 
5113
  drizzle_con_add_options(con, DRIZZLE_CON_NO_RESULT_READ);
4953
5114
 
4954
5115
  if (flags & QUERY_SEND_FLAG)
4955
5116
  {
4956
5117
    /*
4957
5118
     * Send the query
4958
5119
     */
4959
 
    if (do_send_query(cn, query, query_len, flags))
 
5120
 
 
5121
    (void) drizzle_query(con, &res, query, query_len, &ret);
 
5122
    if (ret != DRIZZLE_RETURN_OK)
4960
5123
    {
4961
 
      handle_error(command, drizzle_errno(drizzle), drizzle_error(drizzle),
4962
 
                   drizzle_sqlstate(drizzle), ds);
 
5124
      if (ret == DRIZZLE_RETURN_ERROR_CODE ||
 
5125
          ret == DRIZZLE_RETURN_HANDSHAKE_FAILED)
 
5126
      {
 
5127
        err= drizzle_result_error_code(&res);
 
5128
        handle_error(command, err, drizzle_result_error(&res),
 
5129
                     drizzle_result_sqlstate(&res), ds);
 
5130
        if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
5131
          drizzle_result_free(&res);
 
5132
      }
 
5133
      else
 
5134
      {
 
5135
        handle_error(command, ret, drizzle_con_error(con), "", ds);
 
5136
        err= ret;
 
5137
      }
4963
5138
      goto end;
4964
5139
    }
4965
5140
  }
4966
5141
  if (!(flags & QUERY_REAP_FLAG))
4967
5142
    return;
4968
5143
 
4969
 
  do
4970
5144
  {
4971
5145
    /*
4972
 
      When  on first result set, call drizzle_read_query_result to retrieve
4973
 
      answer to the query sent earlier
4974
 
    */
4975
 
    if ((counter==0) && drizzle_read_query_result(drizzle))
 
5146
     * Read the result packet
 
5147
     */
 
5148
    if (drizzle_result_read(con, &res, &ret) == NULL ||
 
5149
        ret != DRIZZLE_RETURN_OK)
4976
5150
    {
4977
 
      handle_error(command, drizzle_errno(drizzle), drizzle_error(drizzle),
4978
 
                   drizzle_sqlstate(drizzle), ds);
 
5151
      if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
5152
      {
 
5153
        handle_error(command, drizzle_result_error_code(&res),
 
5154
                     drizzle_result_error(&res), drizzle_result_sqlstate(&res),
 
5155
                     ds);
 
5156
      }
 
5157
      else
 
5158
        handle_error(command, ret, drizzle_con_error(con), "", ds);
 
5159
      drizzle_result_free(&res);
 
5160
      err= ret;
4979
5161
      goto end;
4980
 
 
4981
5162
    }
4982
5163
 
4983
5164
    /*
4984
5165
      Store the result of the query if it will return any fields
4985
5166
    */
4986
 
    if (drizzle_field_count(drizzle) && ((res= drizzle_store_result(drizzle)) == 0))
 
5167
    if (drizzle_result_column_count(&res) &&
 
5168
        (ret= drizzle_result_buffer(&res)) != DRIZZLE_RETURN_OK)
4987
5169
    {
4988
 
      handle_error(command, drizzle_errno(drizzle), drizzle_error(drizzle),
4989
 
                   drizzle_sqlstate(drizzle), ds);
 
5170
      if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
5171
      {
 
5172
        handle_error(command, drizzle_result_error_code(&res),
 
5173
                     drizzle_result_error(&res), drizzle_result_sqlstate(&res),
 
5174
                     ds);
 
5175
      }
 
5176
      else
 
5177
        handle_error(command, ret, drizzle_con_error(con), "", ds);
 
5178
      drizzle_result_free(&res);
 
5179
      err= ret;
4990
5180
      goto end;
4991
5181
    }
4992
5182
 
4994
5184
    {
4995
5185
      uint64_t affected_rows= 0;    /* Ok to be undef if 'disable_info' is set */
4996
5186
 
4997
 
      if (res)
 
5187
      if (drizzle_result_column_count(&res))
4998
5188
      {
4999
 
        const DRIZZLE_FIELD *fields= drizzle_fetch_fields(res);
5000
 
        uint num_fields= drizzle_num_fields(res);
5001
 
 
5002
5189
        if (display_metadata)
5003
 
          append_metadata(ds, fields, num_fields);
 
5190
          append_metadata(ds, &res);
5004
5191
 
5005
5192
        if (!display_result_vertically)
5006
 
          append_table_headings(ds, fields, num_fields);
 
5193
          append_table_headings(ds, &res);
5007
5194
 
5008
 
        append_result(ds, res);
 
5195
        append_result(ds, &res);
5009
5196
      }
5010
5197
 
5011
5198
      /*
5012
 
        Need to call drizzle_affected_rows() before the "new"
 
5199
        Need to call drizzleclient_affected_rows() before the "new"
5013
5200
        query to find the warnings
5014
5201
      */
5015
5202
      if (!disable_info)
5016
 
        affected_rows= drizzle_affected_rows(drizzle);
 
5203
        affected_rows= drizzle_result_affected_rows(&res);
5017
5204
 
5018
5205
      /*
5019
5206
        Add all warnings to the result. We can't do this if we are in
5020
5207
        the middle of processing results from multi-statement, because
5021
5208
        this will break protocol.
5022
5209
      */
5023
 
      if (!disable_warnings && !drizzle_more_results(drizzle))
 
5210
      if (!disable_warnings)
5024
5211
      {
5025
 
        if (append_warnings(ds_warnings, drizzle) || ds_warnings->length())
 
5212
        drizzle_con_remove_options(con, DRIZZLE_CON_NO_RESULT_READ);
 
5213
        if (append_warnings(ds_warnings, con, &res) || ds_warnings->length())
5026
5214
        {
5027
5215
          ds->append("Warnings:\n", 10);
5028
5216
          ds->append(ds_warnings->c_str(), ds_warnings->length());
5030
5218
      }
5031
5219
 
5032
5220
      if (!disable_info)
5033
 
        append_info(ds, affected_rows, drizzle_info(drizzle));
 
5221
        append_info(ds, affected_rows, drizzle_result_info(&res));
5034
5222
    }
5035
5223
 
5036
 
    if (res)
5037
 
    {
5038
 
      drizzle_free_result(res);
5039
 
      res= 0;
5040
 
    }
5041
 
    counter++;
5042
 
  } while (!(err= drizzle_next_result(drizzle)));
5043
 
  if (err > 0)
5044
 
  {
5045
 
    /* We got an error from drizzle_next_result, maybe expected */
5046
 
    handle_error(command, drizzle_errno(drizzle), drizzle_error(drizzle),
5047
 
                 drizzle_sqlstate(drizzle), ds);
5048
 
    goto end;
 
5224
    drizzle_result_free(&res);
5049
5225
  }
5050
 
  assert(err == -1); /* Successful and there are no more results */
5051
5226
 
5052
5227
  /* If we come here the query is both executed and read successfully */
5053
5228
  handle_no_error(command);
5055
5230
end:
5056
5231
 
5057
5232
  /*
5058
 
    We save the return code (drizzle_errno(drizzle)) from the last call sent
5059
 
    to the server into the drizzletest builtin variable $drizzle_errno. This
 
5233
    We save the return code (drizzleclient_errno(drizzle)) from the last call sent
 
5234
    to the server into the drizzletest builtin variable $drizzleclient_errno. This
5060
5235
    variable then can be used from the test case itself.
5061
5236
  */
5062
 
  var_set_errno(drizzle_errno(drizzle));
 
5237
  drizzle_con_remove_options(con, DRIZZLE_CON_NO_RESULT_READ);
 
5238
  var_set_errno(err);
5063
5239
  return;
5064
5240
}
5065
5241
 
5084
5260
                  unsigned int err_errno, const char *err_error,
5085
5261
                  const char *err_sqlstate, string *ds)
5086
5262
{
5087
 
  uint i;
 
5263
  uint32_t i;
5088
5264
 
5089
5265
 
5090
5266
  if (command->require_file[0])
5094
5270
      returned a valid reponse. Don't allow 2013 or 2006 to trigger an
5095
5271
      abort_not_supported_test
5096
5272
    */
5097
 
    if (err_errno == CR_SERVER_LOST ||
5098
 
        err_errno == CR_SERVER_GONE_ERROR)
 
5273
    if (err_errno == DRIZZLE_RETURN_SERVER_GONE)
5099
5274
      die("require query '%s' failed: %d: %s", command->query,
5100
5275
          err_errno, err_error);
5101
5276
 
5107
5282
  if (command->abort_on_error)
5108
5283
    die("query '%s' failed: %d: %s", command->query, err_errno, err_error);
5109
5284
 
5110
 
  for (i= 0 ; (uint) i < command->expected_errors.count ; i++)
 
5285
  for (i= 0 ; (uint32_t) i < command->expected_errors.count ; i++)
5111
5286
  {
5112
5287
    if (((command->expected_errors.err[i].type == ERR_ERRNO) &&
5113
5288
         (command->expected_errors.err[i].code.errnum == err_errno)) ||
5114
5289
        ((command->expected_errors.err[i].type == ERR_SQLSTATE) &&
5115
5290
         (strncmp(command->expected_errors.err[i].code.sqlstate,
5116
 
                  err_sqlstate, SQLSTATE_LENGTH) == 0)))
 
5291
                  err_sqlstate, DRIZZLE_MAX_SQLSTATE_SIZE) == 0)))
5117
5292
    {
5118
5293
      if (!disable_result_log)
5119
5294
      {
5308
5483
static void get_command_type(struct st_command* command)
5309
5484
{
5310
5485
  char save;
5311
 
  uint type;
 
5486
  uint32_t type;
5312
5487
 
5313
5488
 
5314
5489
  if (*command->query == '}')
5391
5566
 
5392
5567
*/
5393
5568
 
5394
 
static void mark_progress(struct st_command* command __attribute__((unused)),
5395
 
                          int line)
 
5569
static void mark_progress(struct st_command*, int line)
5396
5570
{
5397
 
  char buf[32], *end;
5398
5571
  uint64_t timer= timer_now();
5399
5572
  if (!progress_start)
5400
5573
    progress_start= timer;
5401
5574
  timer-= progress_start;
5402
5575
 
 
5576
  ostringstream buf;
5403
5577
  /* Milliseconds since start */
5404
 
  end= int64_t2str(timer, buf, 10);
5405
 
  ds_progress.append(buf, (int)(end-buf));
5406
 
  ds_progress.append("\t", 1);
 
5578
  buf << timer << "\t";
5407
5579
 
5408
5580
  /* Parser line number */
5409
 
  end= int10_to_str(line, buf, 10);
5410
 
  ds_progress.append(buf, (int)(end-buf));
5411
 
  ds_progress.append("\t", 1);
 
5581
  buf << line << "\t";
5412
5582
 
5413
5583
  /* Filename */
5414
 
  ds_progress.append(cur_file->file_name);
5415
 
  ds_progress.append(":", 1);
 
5584
  buf << cur_file->file_name << ":";
5416
5585
 
5417
5586
  /* Line in file */
5418
 
  end= int10_to_str(cur_file->lineno, buf, 10);
5419
 
  ds_progress.append(buf, (int)(end-buf));
5420
 
 
5421
 
 
5422
 
  ds_progress.append("\n", 1);
 
5587
  buf << cur_file->lineno << endl;
 
5588
 
 
5589
  ds_progress.append(buf.str());
5423
5590
 
5424
5591
}
5425
5592
 
5428
5595
{
5429
5596
  struct st_command *command;
5430
5597
  bool q_send_flag= 0, abort_flag= 0;
5431
 
  uint command_executed= 0, last_command_executed= 0;
 
5598
  uint32_t command_executed= 0, last_command_executed= 0;
5432
5599
  char save_file[FN_REFLEN];
5433
5600
  struct stat res_info;
5434
5601
  MY_INIT(argv[0]);
5463
5630
                1024, 0, 0, get_var_key, var_free, MYF(0)))
5464
5631
    die("Variable hash initialization failed");
5465
5632
 
5466
 
  var_set_string("$DRIZZLE_SERVER_VERSION", drizzle_get_client_info());
 
5633
  var_set_string("$DRIZZLE_SERVER_VERSION", drizzle_version());
5467
5634
 
5468
5635
  memset(&master_pos, 0, sizeof(master_pos));
5469
5636
 
5482
5649
  if (cur_file == file_stack && cur_file->file == 0)
5483
5650
  {
5484
5651
    cur_file->file= stdin;
5485
 
    cur_file->file_name= my_strdup("<stdin>", MYF(MY_WME));
 
5652
    cur_file->file_name= strdup("<stdin>");
 
5653
    if (cur_file->file_name == NULL)
 
5654
      die("Out of memory");
5486
5655
    cur_file->lineno= 1;
5487
5656
  }
5488
5657
  cur_con= connections;
5489
 
  if (!( drizzle_create(&cur_con->drizzle)))
 
5658
  if ((cur_con->drizzle= drizzle_create(NULL)) == NULL)
5490
5659
    die("Failed in drizzle_create()");
5491
 
  if (opt_compress)
5492
 
    drizzle_options(&cur_con->drizzle,DRIZZLE_OPT_COMPRESS,NULL);
5493
 
  drizzle_options(&cur_con->drizzle, DRIZZLE_OPT_LOCAL_INFILE, 0);
 
5660
  if (!( drizzle_con_create(cur_con->drizzle, &cur_con->con)))
 
5661
    die("Failed in drizzle_con_create()");
5494
5662
 
5495
 
  if (!(cur_con->name = my_strdup("default", MYF(MY_WME))))
 
5663
  if (!(cur_con->name = strdup("default")))
5496
5664
    die("Out of memory");
5497
5665
 
5498
 
  safe_connect(&cur_con->drizzle, cur_con->name, opt_host, opt_user, opt_pass,
 
5666
  safe_connect(&cur_con->con, cur_con->name, opt_host, opt_user, opt_pass,
5499
5667
               opt_db, opt_port);
5500
5668
 
5501
5669
  /* Use all time until exit if no explicit 'start_timer' */
5502
5670
  timer_start= timer_now();
5503
5671
 
5504
5672
  /*
5505
 
    Initialize $drizzle_errno with -1, so we can
 
5673
    Initialize $drizzleclient_errno with -1, so we can
5506
5674
    - distinguish it from valid values ( >= 0 ) and
5507
5675
    - detect if there was never a command sent to the server
5508
5676
  */
5509
5677
  var_set_errno(-1);
5510
5678
 
5511
 
  /* Update $drizzle_get_server_version to that of current connection */
5512
 
  var_set_drizzle_get_server_version(&cur_con->drizzle);
 
5679
  /* Update $drizzleclient_get_server_version to that of current connection */
 
5680
  var_set_drizzleclient_get_server_version(&cur_con->con);
5513
5681
 
5514
5682
  if (opt_include)
5515
5683
  {
5629
5797
 
5630
5798
        if (save_file[0])
5631
5799
        {
5632
 
          strmake(command->require_file, save_file, sizeof(save_file) - 1);
 
5800
          strncpy(command->require_file, save_file, sizeof(save_file) - 1);
5633
5801
          save_file[0]= 0;
5634
5802
        }
5635
5803
        run_query(cur_con, command, flags);
5697
5865
        command->last_argument= command->end;
5698
5866
        break;
5699
5867
      case Q_PING:
5700
 
        (void) drizzle_ping(&cur_con->drizzle);
 
5868
        {
 
5869
          drizzle_result_st result;
 
5870
          drizzle_return_t ret;
 
5871
          (void) drizzle_ping(&cur_con->con, &result, &ret);
 
5872
          if (ret == DRIZZLE_RETURN_OK || ret == DRIZZLE_RETURN_ERROR_CODE)
 
5873
            drizzle_result_free(&result);
 
5874
        }
5701
5875
        break;
5702
5876
      case Q_EXEC:
5703
5877
        do_exec(command);
5715
5889
        do_set_charset(command);
5716
5890
        break;
5717
5891
      case Q_DISABLE_RECONNECT:
5718
 
        set_reconnect(&cur_con->drizzle, 0);
 
5892
        set_reconnect(&cur_con->con, 0);
5719
5893
        break;
5720
5894
      case Q_ENABLE_RECONNECT:
5721
 
        set_reconnect(&cur_con->drizzle, 1);
 
5895
        set_reconnect(&cur_con->con, 1);
5722
5896
        break;
5723
5897
      case Q_DISABLE_PARSING:
5724
5898
        if (parsing_disabled == 0)
5892
6066
{
5893
6067
  if (timer_file)
5894
6068
  {
5895
 
    char buf[32], *end;
 
6069
    ostringstream buf;
5896
6070
    uint64_t timer= timer_now() - timer_start;
5897
 
    end= int64_t2str(timer, buf, 10);
5898
 
    str_to_file(timer_file,buf, (int) (end-buf));
 
6071
    buf << timer;
 
6072
    str_to_file(timer_file,buf.str().c_str(), buf.str().size() );
5899
6073
    /* Timer has been written to the file, don't use it anymore */
5900
6074
    timer_file= 0;
5901
6075
  }
5927
6101
    die("Missing argument in %s", command->query);
5928
6102
 
5929
6103
  /* Allocate a buffer for results */
5930
 
  start= buff= (char *)my_malloc(strlen(from)+1,MYF(MY_WME | MY_FAE));
 
6104
  start= buff= (char *)malloc(strlen(from)+1);
5931
6105
  while (*from)
5932
6106
  {
5933
6107
    char *to;
5934
 
    uint column_number;
 
6108
    uint32_t column_number;
5935
6109
 
5936
6110
    to= get_string(&buff, &from, command);
5937
6111
    if (!(column_number= atoi(to)) || column_number > MAX_COLUMNS)
5940
6114
      die("Wrong number of arguments to replace_column in '%s'", command->query);
5941
6115
    to= get_string(&buff, &from, command);
5942
6116
    free(replace_column[column_number-1]);
5943
 
    replace_column[column_number-1]= my_strdup(to, MYF(MY_WME | MY_FAE));
 
6117
    replace_column[column_number-1]= strdup(to);
 
6118
    if (replace_column[column_number-1] == NULL)
 
6119
      die("Out of memory");
5944
6120
    set_if_bigger(max_replace_column, column_number);
5945
6121
  }
5946
6122
  free(start);
5950
6126
 
5951
6127
void free_replace_column()
5952
6128
{
5953
 
  uint i;
 
6129
  uint32_t i;
5954
6130
  for (i=0 ; i < max_replace_column ; i++)
5955
6131
  {
5956
6132
    if (replace_column[i])
5974
6150
  TYPELIB typelib;        /* Pointer to strings */
5975
6151
  unsigned char  *str;          /* Strings is here */
5976
6152
  uint8_t *flag;          /* Flag about each var. */
5977
 
  uint  array_allocs,max_count,length,max_length;
 
6153
  uint32_t  array_allocs,max_count,length,max_length;
5978
6154
} POINTER_ARRAY;
5979
6155
 
5980
6156
struct st_replace;
5981
 
struct st_replace *init_replace(char * *from, char * *to, uint count,
 
6157
struct st_replace *init_replace(char * *from, char * *to, uint32_t count,
5982
6158
                                char * word_end_chars);
5983
6159
int insert_pointer_name(POINTER_ARRAY *pa,char * name);
5984
6160
void replace_strings_append(struct st_replace *rep, string* ds,
5985
6161
                            const char *from, int len);
5986
6162
void free_pointer_array(POINTER_ARRAY *pa);
5987
6163
 
5988
 
struct st_replace *glob_replace;
 
6164
struct st_replace *glob_replace= NULL;
5989
6165
 
5990
6166
/*
5991
6167
  Get arguments for replace. The syntax is:
5997
6173
 
5998
6174
void do_get_replace(struct st_command *command)
5999
6175
{
6000
 
  uint i;
 
6176
  uint32_t i;
6001
6177
  char *from= command->first_argument;
6002
6178
  char *buff, *start;
6003
6179
  char word_end_chars[256], *pos;
6010
6186
  memset(&from_array, 0, sizeof(from_array));
6011
6187
  if (!*from)
6012
6188
    die("Missing argument in %s", command->query);
6013
 
  start= buff= (char *)my_malloc(strlen(from)+1,MYF(MY_WME | MY_FAE));
 
6189
  start= buff= (char *)malloc(strlen(from)+1);
6014
6190
  while (*from)
6015
6191
  {
6016
6192
    char *to= buff;
6028
6204
  *pos=0;          /* End pointer */
6029
6205
  if (!(glob_replace= init_replace((char**) from_array.typelib.type_names,
6030
6206
                                   (char**) to_array.typelib.type_names,
6031
 
                                   (uint) from_array.typelib.count,
 
6207
                                   (uint32_t) from_array.typelib.count,
6032
6208
                                   word_end_chars)))
6033
6209
    die("Can't initialize replace from '%s'", command->query);
6034
6210
  free_pointer_array(&from_array);
6059
6235
typedef struct st_replace_found {
6060
6236
  bool found;
6061
6237
  char *replace_string;
6062
 
  uint to_offset;
 
6238
  uint32_t to_offset;
6063
6239
  int from_offset;
6064
6240
} REPLACE_STRING;
6065
6241
 
6185
6361
  char* buf,*expr_end;
6186
6362
  char* p;
6187
6363
  char* buf_p;
6188
 
  uint expr_len= strlen(expr);
 
6364
  uint32_t expr_len= strlen(expr);
6189
6365
  char last_c = 0;
6190
6366
  struct st_regex reg;
6191
6367
 
6192
 
  /* my_malloc() will die on fail with MY_FAE */
6193
 
  res=(struct st_replace_regex*)my_malloc(
6194
 
                                          sizeof(*res)+expr_len ,MYF(MY_FAE+MY_WME));
 
6368
  res=(st_replace_regex*)malloc(sizeof(*res)+expr_len);
 
6369
  if (!res)
 
6370
    return NULL;
6195
6371
  my_init_dynamic_array(&res->regex_arr,sizeof(struct st_regex),128,128);
6196
6372
 
6197
6373
  buf= (char*)res + sizeof(*res);
6248
6424
      die("Out of memory");
6249
6425
  }
6250
6426
  res->odd_buf_len= res->even_buf_len= 8192;
6251
 
  res->even_buf= (char*)my_malloc(res->even_buf_len,MYF(MY_WME+MY_FAE));
6252
 
  res->odd_buf= (char*)my_malloc(res->odd_buf_len,MYF(MY_WME+MY_FAE));
 
6427
  res->even_buf= (char*)malloc(res->even_buf_len);
 
6428
  res->odd_buf= (char*)malloc(res->odd_buf_len);
6253
6429
  res->buf= res->even_buf;
6254
6430
 
6255
6431
  return res;
6281
6457
 
6282
6458
static int multi_reg_replace(struct st_replace_regex* r,char* val)
6283
6459
{
6284
 
  uint i;
 
6460
  uint32_t i;
6285
6461
  char* in_buf, *out_buf;
6286
6462
  int* buf_len_p;
6287
6463
 
6357
6533
 
6358
6534
 
6359
6535
/*
6360
 
  auxiluary macro used by reg_replace
6361
 
  makes sure the result buffer has sufficient length
6362
 
*/
6363
 
#define SECURE_REG_BUF   if (buf_len < need_buf_len)                    \
6364
 
  {                                                                     \
6365
 
    int off= res_p - buf;                                               \
6366
 
    buf= (char*)my_realloc(buf,need_buf_len,MYF(MY_WME+MY_FAE));        \
6367
 
    res_p= buf + off;                                                   \
6368
 
    buf_len= need_buf_len;                                              \
6369
 
  }                                                                     \
6370
 
                                                                        \
6371
 
/*
6372
6536
  Performs a regex substitution
6373
6537
 
6374
6538
  IN:
6384
6548
                char *replace, char *in_string, int icase)
6385
6549
{
6386
6550
  string string_to_match(in_string);
6387
 
  pcrecpp::RE_Options opt;
6388
 
 
6389
 
  if (icase)
6390
 
    opt.set_caseless(true);
6391
 
 
6392
 
  if (!pcrecpp::RE(pattern, opt).Replace(replace,&string_to_match)){
 
6551
  const char *error= NULL;
 
6552
  int erroffset;
 
6553
  int ovector[3];
 
6554
  pcre *re= pcre_compile(pattern,
 
6555
                         icase ? PCRE_CASELESS : 0,
 
6556
                         &error, &erroffset, NULL);
 
6557
  if (re == NULL)
 
6558
    return 1;
 
6559
 
 
6560
  int rc= pcre_exec(re, NULL, in_string, (int)strlen(in_string),
 
6561
                    0, 0, ovector, 3);
 
6562
  if (rc < 0)
 
6563
  {
 
6564
    pcre_free(re);
6393
6565
    return 1;
6394
6566
  }
6395
6567
 
6396
 
  const char * new_str= string_to_match.c_str();
6397
 
  *buf_len_p= strlen(new_str);
 
6568
  char *substring_to_replace= in_string + ovector[0];
 
6569
  int substring_length= ovector[1] - ovector[0];
 
6570
  *buf_len_p= strlen(in_string) - substring_length + strlen(replace);
6398
6571
  char * new_buf = (char *)malloc(*buf_len_p+1);
6399
6572
  if (new_buf == NULL)
6400
6573
  {
 
6574
    pcre_free(re);
6401
6575
    return 1;
6402
6576
  }
6403
 
  strcpy(new_buf, new_str);
6404
 
  buf_p= &new_buf;
6405
 
 
 
6577
 
 
6578
  memset(new_buf, 0, *buf_len_p+1);
 
6579
  strncpy(new_buf, in_string, substring_to_replace-in_string);
 
6580
  strncpy(new_buf+(substring_to_replace-in_string), replace, strlen(replace));
 
6581
  strncpy(new_buf+(substring_to_replace-in_string)+strlen(replace),
 
6582
          substring_to_replace + substring_length,
 
6583
          strlen(in_string)
 
6584
            - substring_length
 
6585
            - (substring_to_replace-in_string));
 
6586
  *buf_p= new_buf;
 
6587
 
 
6588
  pcre_free(re);
6406
6589
  return 0;
6407
6590
}
6408
6591
 
6409
6592
 
6410
6593
#ifndef WORD_BIT
6411
 
#define WORD_BIT (8*sizeof(uint))
 
6594
#define WORD_BIT (8*sizeof(uint32_t))
6412
6595
#endif
6413
6596
 
6414
6597
#define SET_MALLOC_HUNC 64
6415
6598
#define LAST_CHAR_CODE 259
6416
6599
 
6417
6600
typedef struct st_rep_set {
6418
 
  uint  *bits;        /* Pointer to used sets */
 
6601
  uint32_t  *bits;        /* Pointer to used sets */
6419
6602
  short next[LAST_CHAR_CODE];    /* Pointer to next sets */
6420
 
  uint  found_len;      /* Best match to date */
 
6603
  uint32_t  found_len;      /* Best match to date */
6421
6604
  int  found_offset;
6422
 
  uint  table_offset;
6423
 
  uint  size_of_bits;      /* For convinience */
 
6605
  uint32_t  table_offset;
 
6606
  uint32_t  size_of_bits;      /* For convinience */
6424
6607
} REP_SET;
6425
6608
 
6426
6609
typedef struct st_rep_sets {
6427
 
  uint    count;      /* Number of sets */
6428
 
  uint    extra;      /* Extra sets in buffer */
6429
 
  uint    invisible;    /* Sets not chown */
6430
 
  uint    size_of_bits;
 
6610
  uint32_t    count;      /* Number of sets */
 
6611
  uint32_t    extra;      /* Extra sets in buffer */
 
6612
  uint32_t    invisible;    /* Sets not chown */
 
6613
  uint32_t    size_of_bits;
6431
6614
  REP_SET  *set,*set_buffer;
6432
 
  uint    *bit_buffer;
 
6615
  uint32_t    *bit_buffer;
6433
6616
} REP_SETS;
6434
6617
 
6435
6618
typedef struct st_found_set {
6436
 
  uint table_offset;
 
6619
  uint32_t table_offset;
6437
6620
  int found_offset;
6438
6621
} FOUND_SET;
6439
6622
 
6440
6623
typedef struct st_follow {
6441
6624
  int chr;
6442
 
  uint table_offset;
6443
 
  uint len;
 
6625
  uint32_t table_offset;
 
6626
  uint32_t len;
6444
6627
} FOLLOWS;
6445
6628
 
6446
6629
 
6447
 
int init_sets(REP_SETS *sets,uint states);
 
6630
int init_sets(REP_SETS *sets,uint32_t states);
6448
6631
REP_SET *make_new_set(REP_SETS *sets);
6449
6632
void make_sets_invisible(REP_SETS *sets);
6450
6633
void free_last_set(REP_SETS *sets);
6451
6634
void free_sets(REP_SETS *sets);
6452
 
void internal_set_bit(REP_SET *set, uint bit);
6453
 
void internal_clear_bit(REP_SET *set, uint bit);
 
6635
void internal_set_bit(REP_SET *set, uint32_t bit);
 
6636
void internal_clear_bit(REP_SET *set, uint32_t bit);
6454
6637
void or_bits(REP_SET *to,REP_SET *from);
6455
6638
void copy_bits(REP_SET *to,REP_SET *from);
6456
6639
int cmp_bits(REP_SET *set1,REP_SET *set2);
6457
 
int get_next_bit(REP_SET *set,uint lastpos);
 
6640
int get_next_bit(REP_SET *set,uint32_t lastpos);
6458
6641
int find_set(REP_SETS *sets,REP_SET *find);
6459
 
int find_found(FOUND_SET *found_set,uint table_offset,
 
6642
int find_found(FOUND_SET *found_set,uint32_t table_offset,
6460
6643
               int found_offset);
6461
 
uint start_at_word(char * pos);
6462
 
uint end_of_word(char * pos);
6463
 
 
6464
 
static uint found_sets=0;
6465
 
 
6466
 
 
6467
 
static uint replace_len(char * str)
 
6644
uint32_t start_at_word(char * pos);
 
6645
uint32_t end_of_word(char * pos);
 
6646
 
 
6647
static uint32_t found_sets=0;
 
6648
 
 
6649
 
 
6650
static uint32_t replace_len(char * str)
6468
6651
{
6469
 
  uint len=0;
 
6652
  uint32_t len=0;
6470
6653
  while (*str)
6471
6654
  {
6472
6655
    if (str[0] == '\\' && str[1])
6479
6662
 
6480
6663
/* Init a replace structure for further calls */
6481
6664
 
6482
 
REPLACE *init_replace(char * *from, char * *to,uint count,
 
6665
REPLACE *init_replace(char * *from, char * *to,uint32_t count,
6483
6666
                      char * word_end_chars)
6484
6667
{
6485
6668
  static const int SPACE_CHAR= 256;
6486
6669
  static const int START_OF_LINE= 257;
6487
6670
  static const int END_OF_LINE= 258;
6488
6671
 
6489
 
  uint i,j,states,set_nr,len,result_len,max_length,found_end,bits_set,bit_nr;
 
6672
  uint32_t i,j,states,set_nr,len,result_len,max_length,found_end,bits_set,bit_nr;
6490
6673
  int used_sets,chr,default_state;
6491
6674
  char used_chars[LAST_CHAR_CODE],is_word_end[256];
6492
6675
  char * pos, *to_pos, **to_array;
6508
6691
      return(0);
6509
6692
    }
6510
6693
    states+=len+1;
6511
 
    result_len+=(uint) strlen(to[i])+1;
 
6694
    result_len+=(uint32_t) strlen(to[i])+1;
6512
6695
    if (len > max_length)
6513
6696
      max_length=len;
6514
6697
  }
6519
6702
  if (init_sets(&sets,states))
6520
6703
    return(0);
6521
6704
  found_sets=0;
6522
 
  if (!(found_set= (FOUND_SET*) my_malloc(sizeof(FOUND_SET)*max_length*count,
6523
 
                                          MYF(MY_WME))))
 
6705
  if (!(found_set= (FOUND_SET*) malloc(sizeof(FOUND_SET)*max_length*count)))
 
6706
                                
6524
6707
  {
6525
6708
    free_sets(&sets);
6526
6709
    return(0);
6530
6713
  used_sets=-1;
6531
6714
  word_states=make_new_set(&sets);    /* Start of new word */
6532
6715
  start_states=make_new_set(&sets);    /* This is first state */
6533
 
  if (!(follow=(FOLLOWS*) my_malloc((states+2)*sizeof(FOLLOWS),MYF(MY_WME))))
 
6716
  if (!(follow=(FOLLOWS*) malloc((states+2)*sizeof(FOLLOWS))))
6534
6717
  {
6535
6718
    free_sets(&sets);
6536
6719
    free(found_set);
6606
6789
    follow_ptr->table_offset=i;
6607
6790
    follow_ptr->len=len;
6608
6791
    follow_ptr++;
6609
 
    states+=(uint) len+1;
 
6792
    states+=(uint32_t) len+1;
6610
6793
  }
6611
6794
 
6612
6795
 
6722
6905
 
6723
6906
  /* Alloc replace structure for the replace-state-machine */
6724
6907
 
6725
 
  if ((replace=(REPLACE*) my_malloc(sizeof(REPLACE)*(sets.count)+
6726
 
                                    sizeof(REPLACE_STRING)*(found_sets+1)+
6727
 
                                    sizeof(char *)*count+result_len,
6728
 
                                    MYF(MY_WME | MY_ZEROFILL))))
 
6908
  if ((replace=(REPLACE*) malloc(sizeof(REPLACE)*(sets.count)+
 
6909
                                 sizeof(REPLACE_STRING)*(found_sets+1)+
 
6910
                                 sizeof(char *)*count+result_len)))
6729
6911
  {
 
6912
    memset(replace, 0, sizeof(REPLACE)*(sets.count)+
 
6913
                       sizeof(REPLACE_STRING)*(found_sets+1)+
 
6914
                       sizeof(char *)*count+result_len);
6730
6915
    rep_str=(REPLACE_STRING*) (replace+sets.count);
6731
6916
    to_array= (char **) (rep_str+found_sets+1);
6732
6917
    to_pos=(char *) (to_array+count);
6733
6918
    for (i=0 ; i < count ; i++)
6734
6919
    {
6735
6920
      to_array[i]=to_pos;
6736
 
      to_pos=my_stpcpy(to_pos,to[i])+1;
 
6921
      to_pos=strcpy(to_pos,to[i])+strlen(to[i])+1;
6737
6922
    }
6738
6923
    rep_str[0].found=1;
6739
6924
    rep_str[0].replace_string=0;
6762
6947
}
6763
6948
 
6764
6949
 
6765
 
int init_sets(REP_SETS *sets,uint states)
 
6950
int init_sets(REP_SETS *sets,uint32_t states)
6766
6951
{
6767
6952
  memset(sets, 0, sizeof(*sets));
6768
6953
  sets->size_of_bits=((states+7)/8);
6769
 
  if (!(sets->set_buffer=(REP_SET*) my_malloc(sizeof(REP_SET)*SET_MALLOC_HUNC,
6770
 
                                              MYF(MY_WME))))
 
6954
  if (!(sets->set_buffer=(REP_SET*) malloc(sizeof(REP_SET)*SET_MALLOC_HUNC)))
6771
6955
    return 1;
6772
 
  if (!(sets->bit_buffer=(uint*) my_malloc(sizeof(uint)*sets->size_of_bits*
6773
 
                                           SET_MALLOC_HUNC,MYF(MY_WME))))
 
6956
  if (!(sets->bit_buffer=(uint*) malloc(sizeof(uint32_t)*sets->size_of_bits*
 
6957
                                        SET_MALLOC_HUNC)))
6774
6958
  {
6775
6959
    free(sets->set);
6776
6960
    return 1;
6789
6973
 
6790
6974
REP_SET *make_new_set(REP_SETS *sets)
6791
6975
{
6792
 
  uint i,count,*bit_buffer;
 
6976
  uint32_t i,count,*bit_buffer;
6793
6977
  REP_SET *set;
6794
6978
  if (sets->extra)
6795
6979
  {
6796
6980
    sets->extra--;
6797
6981
    set=sets->set+ sets->count++;
6798
 
    memset(set->bits, 0, sizeof(uint)*sets->size_of_bits);
 
6982
    memset(set->bits, 0, sizeof(uint32_t)*sets->size_of_bits);
6799
6983
    memset(&set->next[0], 0, sizeof(set->next[0])*LAST_CHAR_CODE);
6800
6984
    set->found_offset=0;
6801
6985
    set->found_len=0;
6804
6988
    return set;
6805
6989
  }
6806
6990
  count=sets->count+sets->invisible+SET_MALLOC_HUNC;
6807
 
  if (!(set=(REP_SET*) my_realloc((unsigned char*) sets->set_buffer,
6808
 
                                  sizeof(REP_SET)*count,
6809
 
                                  MYF(MY_WME))))
 
6991
  if (!(set=(REP_SET*) realloc((unsigned char*) sets->set_buffer,
 
6992
                                  sizeof(REP_SET)*count)))
6810
6993
    return 0;
6811
6994
  sets->set_buffer=set;
6812
6995
  sets->set=set+sets->invisible;
6813
 
  if (!(bit_buffer=(uint*) my_realloc((unsigned char*) sets->bit_buffer,
6814
 
                                      (sizeof(uint)*sets->size_of_bits)*count,
6815
 
                                      MYF(MY_WME))))
 
6996
  if (!(bit_buffer=(uint*) realloc((unsigned char*) sets->bit_buffer,
 
6997
                                   (sizeof(uint32_t)*sets->size_of_bits)*count)))
6816
6998
    return 0;
6817
6999
  sets->bit_buffer=bit_buffer;
6818
7000
  for (i=0 ; i < count ; i++)
6838
7020
  return;
6839
7021
}
6840
7022
 
6841
 
void internal_set_bit(REP_SET *set, uint bit)
 
7023
void internal_set_bit(REP_SET *set, uint32_t bit)
6842
7024
{
6843
7025
  set->bits[bit / WORD_BIT] |= 1 << (bit % WORD_BIT);
6844
7026
  return;
6845
7027
}
6846
7028
 
6847
 
void internal_clear_bit(REP_SET *set, uint bit)
 
7029
void internal_clear_bit(REP_SET *set, uint32_t bit)
6848
7030
{
6849
7031
  set->bits[bit / WORD_BIT] &= ~ (1 << (bit % WORD_BIT));
6850
7032
  return;
6853
7035
 
6854
7036
void or_bits(REP_SET *to,REP_SET *from)
6855
7037
{
6856
 
  register uint i;
 
7038
  register uint32_t i;
6857
7039
  for (i=0 ; i < to->size_of_bits ; i++)
6858
7040
    to->bits[i]|=from->bits[i];
6859
7041
  return;
6862
7044
void copy_bits(REP_SET *to,REP_SET *from)
6863
7045
{
6864
7046
  memcpy(to->bits,from->bits,
6865
 
         (size_t) (sizeof(uint) * to->size_of_bits));
 
7047
         (size_t) (sizeof(uint32_t) * to->size_of_bits));
6866
7048
}
6867
7049
 
6868
7050
int cmp_bits(REP_SET *set1,REP_SET *set2)
6869
7051
{
6870
 
  return memcmp(set1->bits,set2->bits, sizeof(uint) * set1->size_of_bits);
 
7052
  return memcmp(set1->bits,set2->bits, sizeof(uint32_t) * set1->size_of_bits);
6871
7053
}
6872
7054
 
6873
7055
 
6874
7056
/* Get next set bit from set. */
6875
7057
 
6876
 
int get_next_bit(REP_SET *set,uint lastpos)
 
7058
int get_next_bit(REP_SET *set,uint32_t lastpos)
6877
7059
{
6878
 
  uint pos,*start,*end,bits;
 
7060
  uint32_t pos,*start,*end,bits;
6879
7061
 
6880
7062
  start=set->bits+ ((lastpos+1) / WORD_BIT);
6881
7063
  end=set->bits + set->size_of_bits;
6885
7067
    bits=start[0];
6886
7068
  if (!bits)
6887
7069
    return 0;
6888
 
  pos=(uint) (start-set->bits)*WORD_BIT;
 
7070
  pos=(uint32_t) (start-set->bits)*WORD_BIT;
6889
7071
  while (! (bits & 1))
6890
7072
  {
6891
7073
    bits>>=1;
6900
7082
 
6901
7083
int find_set(REP_SETS *sets,REP_SET *find)
6902
7084
{
6903
 
  uint i;
 
7085
  uint32_t i;
6904
7086
  for (i=0 ; i < sets->count-1 ; i++)
6905
7087
  {
6906
7088
    if (!cmp_bits(sets->set+i,find))
6919
7101
   set->next[] == -1 is reserved for end without replaces.
6920
7102
*/
6921
7103
 
6922
 
int find_found(FOUND_SET *found_set,uint table_offset, int found_offset)
 
7104
int find_found(FOUND_SET *found_set,uint32_t table_offset, int found_offset)
6923
7105
{
6924
7106
  int i;
6925
 
  for (i=0 ; (uint) i < found_sets ; i++)
 
7107
  for (i=0 ; (uint32_t) i < found_sets ; i++)
6926
7108
    if (found_set[i].table_offset == table_offset &&
6927
7109
        found_set[i].found_offset == found_offset)
6928
7110
      return -i-2;
6934
7116
 
6935
7117
/* Return 1 if regexp starts with \b or ends with \b*/
6936
7118
 
6937
 
uint start_at_word(char * pos)
 
7119
uint32_t start_at_word(char * pos)
6938
7120
{
6939
7121
  return (((!memcmp(pos, "\\b",2) && pos[2]) ||
6940
7122
           !memcmp(pos, "\\^", 2)) ? 1 : 0);
6941
7123
}
6942
7124
 
6943
 
uint end_of_word(char * pos)
 
7125
uint32_t end_of_word(char * pos)
6944
7126
{
6945
7127
  char * end= strchr(pos, '\0');
6946
7128
  return ((end > pos+2 && !memcmp(end-2, "\\b", 2)) ||
6956
7138
 
6957
7139
int insert_pointer_name(POINTER_ARRAY *pa,char * name)
6958
7140
{
6959
 
  uint i,length,old_count;
 
7141
  uint32_t i,length,old_count;
6960
7142
  unsigned char *new_pos;
6961
7143
  const char **new_array;
6962
7144
 
6964
7146
  if (! pa->typelib.count)
6965
7147
  {
6966
7148
    if (!(pa->typelib.type_names=(const char **)
6967
 
          my_malloc(((PC_MALLOC-MALLOC_OVERHEAD)/
 
7149
          malloc(((PC_MALLOC-MALLOC_OVERHEAD)/
6968
7150
                     (sizeof(char *)+sizeof(*pa->flag))*
6969
 
                     (sizeof(char *)+sizeof(*pa->flag))),MYF(MY_WME))))
 
7151
                     (sizeof(char *)+sizeof(*pa->flag))))))
6970
7152
      return(-1);
6971
 
    if (!(pa->str= (unsigned char*) my_malloc((uint) (PS_MALLOC-MALLOC_OVERHEAD),
6972
 
                                      MYF(MY_WME))))
 
7153
    if (!(pa->str= (unsigned char*) malloc(PS_MALLOC-MALLOC_OVERHEAD)))
6973
7154
    {
6974
7155
      free((char*) pa->typelib.type_names);
6975
7156
      return (-1);
6981
7162
    pa->max_length=PS_MALLOC-MALLOC_OVERHEAD;
6982
7163
    pa->array_allocs=1;
6983
7164
  }
6984
 
  length=(uint) strlen(name)+1;
 
7165
  length=(uint32_t) strlen(name)+1;
6985
7166
  if (pa->length+length >= pa->max_length)
6986
7167
  {
6987
 
    if (!(new_pos= (unsigned char*) my_realloc((unsigned char*) pa->str,
6988
 
                                       (uint) (pa->max_length+PS_MALLOC),
6989
 
                                       MYF(MY_WME))))
 
7168
    if (!(new_pos= (unsigned char*)realloc((unsigned char*)pa->str,
 
7169
                                           (size_t)(pa->max_length+PS_MALLOC))))
6990
7170
      return(1);
6991
7171
    if (new_pos != pa->str)
6992
7172
    {
7000
7180
  }
7001
7181
  if (pa->typelib.count >= pa->max_count-1)
7002
7182
  {
7003
 
    int len;
 
7183
    size_t len;
7004
7184
    pa->array_allocs++;
7005
7185
    len=(PC_MALLOC*pa->array_allocs - MALLOC_OVERHEAD);
7006
 
    if (!(new_array=(const char **) my_realloc((unsigned char*) pa->typelib.type_names,
7007
 
                                               (uint) len/
7008
 
                                               (sizeof(unsigned char*)+sizeof(*pa->flag))*
7009
 
                                               (sizeof(unsigned char*)+sizeof(*pa->flag)),
7010
 
                                               MYF(MY_WME))))
 
7186
    if (!(new_array=
 
7187
         (const char **)realloc((unsigned char*) pa->typelib.type_names,
 
7188
                                 len/
 
7189
                                  (sizeof(unsigned char*)+sizeof(*pa->flag))*
 
7190
                                  (sizeof(unsigned char*)+sizeof(*pa->flag)))))
7011
7191
      return(1);
7012
7192
    pa->typelib.type_names=new_array;
7013
7193
    old_count=pa->max_count;
7019
7199
  pa->flag[pa->typelib.count]=0;      /* Reset flag */
7020
7200
  pa->typelib.type_names[pa->typelib.count++]= (char*) pa->str+pa->length;
7021
7201
  pa->typelib.type_names[pa->typelib.count]= NULL;  /* Put end-mark */
7022
 
  my_stpcpy((char*) pa->str+pa->length,name);
 
7202
  strcpy((char*) pa->str+pa->length,name);
7023
7203
  pa->length+=length;
7024
7204
  return(0);
7025
7205
} /* insert_pointer_name */
7075
7255
  replace_append_mem(ds, val, strlen(val));
7076
7256
}
7077
7257
 
7078
 
/* Append uint to ds, with optional replace */
7079
 
void replace_append_uint(string *ds, uint val)
 
7258
/* Append uint32_t to ds, with optional replace */
 
7259
void replace_append_uint(string *ds, uint32_t val)
7080
7260
{
7081
 
  char buff[22]; /* This should be enough for any int */
7082
 
  char *end= int64_t10_to_str(val, buff, 10);
7083
 
  replace_append_mem(ds, buff, end - buff);
 
7261
  ostringstream buff;
 
7262
  buff << val;
 
7263
  replace_append_mem(ds, buff.str().c_str(), buff.str().size());
7084
7264
 
7085
7265
}
7086
7266
 
7101
7281
 
7102
7282
void append_sorted(string* ds, string *ds_input)
7103
7283
{
7104
 
  priority_queue<string> lines;
 
7284
  priority_queue<string, vector<string>, greater<string> > lines;
7105
7285
 
7106
7286
  if (ds_input->empty())
7107
7287
    return;  /* No input */
7112
7292
  if (eol_pos == string::npos)
7113
7293
    return; // We should have at least one header here
7114
7294
 
7115
 
  ds->append(ds_input->substr(0, eol_pos));
 
7295
  ds->append(ds_input->substr(0, eol_pos+1));
7116
7296
 
7117
7297
  unsigned long start_pos= eol_pos+1;
7118
7298
 
7121
7301
 
7122
7302
    eol_pos= ds_input->find_first_of('\n', start_pos);
7123
7303
    /* Find end of line */
7124
 
    lines.push(ds_input->substr(start_pos, eol_pos-start_pos));
 
7304
    lines.push(ds_input->substr(start_pos, eol_pos-start_pos+1));
7125
7305
    start_pos= eol_pos+1;
7126
7306
 
7127
7307
  } while ( eol_pos != string::npos);