~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzletest.cc

  • Committer: Eric Herman
  • Date: 2008-12-07 15:29:44 UTC
  • mto: (656.1.14 devel)
  • mto: This revision was merged to the branch mainline in revision 670.
  • Revision ID: eric@mysql.com-20081207152944-cq1nx1cyi0huqj0f
Added pointer to online version of the FAQ

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 * 
4
 
 *  Copyright (C) 2010 Vijay Samuel
 
3
 *
5
4
 *  Copyright (C) 2008 MySQL
6
5
 *
7
6
 *  This program is free software; you can redistribute it and/or modify
44
43
#include <queue>
45
44
#include <map>
46
45
#include <string>
47
 
#include <sstream>
48
 
#include <fstream>
49
 
#include <iostream>
50
46
#include <vector>
51
 
#include <algorithm>
52
 
#ifdef HAVE_SYS_WAIT_H
53
 
#include <sys/wait.h>
54
 
#endif
55
 
#include <cassert>
56
 
#include <sys/stat.h>
57
 
#include <sys/types.h>
58
 
#include <fcntl.h>
59
 
#include <boost/program_options.hpp>
60
 
 
61
 
#include PCRE_HEADER
62
 
 
 
47
 
 
48
#include <pcre.h>
 
49
 
 
50
#include <mysys/hash.h>
63
51
#include <stdarg.h>
64
 
#include <boost/unordered_map.hpp>
65
 
 
66
 
/* Added this for string translation. */
67
 
#include "drizzled/gettext.h"
68
 
#include "drizzled/drizzle_time.h"
69
 
#include "drizzled/charset.h"
70
 
#include <drizzled/configmake.h>
71
 
 
72
 
#ifndef DRIZZLE_RETURN_SERVER_GONE
73
 
#define DRIZZLE_RETURN_HANDSHAKE_FAILED DRIZZLE_RETURN_ERROR_CODE
74
 
#endif
75
 
namespace po= boost::program_options;
 
52
 
 
53
#include "errname.h"
 
54
 
76
55
using namespace std;
77
 
using namespace drizzled;
78
 
 
79
 
extern "C"
80
 
unsigned char *get_var_key(const unsigned char* var, size_t *len, bool);
81
 
 
82
 
int get_one_option(int optid, const struct option *, char *argument);
83
56
 
84
57
#define MAX_VAR_NAME_LENGTH    256
85
58
#define MAX_COLUMNS            256
 
59
#define MAX_EMBEDDED_SERVER_ARGS 64
86
60
#define MAX_DELIMITER_LENGTH 16
87
61
/* Flags controlling send and reap */
88
62
#define QUERY_SEND_FLAG  1
89
63
#define QUERY_REAP_FLAG  2
90
64
 
91
 
typedef boost::unordered_map<std::string, uint32_t> ErrorCodes;
92
 
ErrorCodes global_error_names;
93
 
 
94
65
enum {
95
66
  OPT_PS_PROTOCOL, OPT_SP_PROTOCOL, OPT_CURSOR_PROTOCOL, OPT_VIEW_PROTOCOL,
96
 
  OPT_MAX_CONNECT_RETRIES, OPT_MARK_PROGRESS, OPT_LOG_DIR, OPT_TAIL_LINES,
97
 
  OPT_TESTDIR
 
67
  OPT_MAX_CONNECT_RETRIES, OPT_MARK_PROGRESS, OPT_LOG_DIR, OPT_TAIL_LINES
98
68
};
99
69
 
100
70
static int record= 0, opt_sleep= -1;
101
 
static char *opt_pass= NULL;
102
 
const char *unix_sock= NULL;
103
 
static uint32_t opt_port= 0;
104
 
static uint32_t opt_max_connect_retries;
105
 
static bool silent= false, verbose= false;
106
 
static bool opt_mark_progress= false;
107
 
static bool parsing_disabled= false;
 
71
static char *opt_db= 0, *opt_pass= 0;
 
72
const char *opt_user= 0, *opt_host= 0, *unix_sock= 0, *opt_basedir= "./";
 
73
const char *opt_logdir= "";
 
74
const char *opt_include= 0, *opt_charsets_dir;
 
75
static int opt_port= 0;
 
76
static int opt_max_connect_retries;
 
77
static bool opt_compress= 0, silent= 0, verbose= 0;
 
78
static bool debug_info_flag= 0, debug_check_flag= 0;
 
79
static bool tty_password= 0;
 
80
static bool opt_mark_progress= 0;
 
81
static bool parsing_disabled= 0;
108
82
static bool display_result_vertically= false,
109
83
  display_metadata= false, display_result_sorted= false;
110
 
static bool disable_query_log= false, disable_result_log= false;
111
 
static bool disable_warnings= false;
112
 
static bool disable_info= true;
113
 
static bool abort_on_error= true;
114
 
static bool server_initialized= false;
115
 
static bool is_windows= false;
116
 
static bool use_drizzle_protocol= false;
 
84
static bool disable_query_log= 0, disable_result_log= 0;
 
85
static bool disable_warnings= 0;
 
86
static bool disable_info= 1;
 
87
static bool abort_on_error= 1;
 
88
static bool server_initialized= 0;
 
89
static bool is_windows= 0;
 
90
static char **default_argv;
 
91
static const char *load_default_groups[]= { "drizzletest", "client", 0 };
117
92
static char line_buffer[MAX_DELIMITER_LENGTH], *line_buffer_pos= line_buffer;
118
93
 
119
 
std::string opt_basedir,
120
 
  opt_charsets_dir,
121
 
  opt_db,
122
 
  opt_host,
123
 
  opt_include,
124
 
  opt_testdir,
125
 
  opt_logdir,
126
 
  password,
127
 
  opt_password,
128
 
  result_file_name,
129
 
  opt_user,
130
 
  opt_protocol;
131
 
 
132
 
static uint32_t start_lineno= 0; /* Start line of current command */
 
94
static uint start_lineno= 0; /* Start line of current command */
 
95
static uint my_end_arg= 0;
133
96
 
134
97
/* Number of lines of the result to include in failure report */
135
 
static uint32_t opt_tail_lines= 0;
 
98
static uint opt_tail_lines= 0;
136
99
 
137
100
static char delimiter[MAX_DELIMITER_LENGTH]= ";";
138
 
static uint32_t delimiter_length= 1;
 
101
static uint delimiter_length= 1;
139
102
 
140
103
static char TMPDIR[FN_REFLEN];
141
104
 
161
124
{
162
125
  FILE* file;
163
126
  const char *file_name;
164
 
  uint32_t lineno; /* Current line in file */
 
127
  uint lineno; /* Current line in file */
165
128
};
166
129
 
167
130
static struct st_test_file file_stack[16];
171
134
 
172
135
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci; /* Default charset */
173
136
 
 
137
static int embedded_server_arg_count=0;
 
138
static char *embedded_server_args[MAX_EMBEDDED_SERVER_ARGS];
 
139
 
174
140
/*
175
141
  Timer related variables
176
142
  See the timer_output() definition for details
198
164
master_pos_st master_pos;
199
165
 
200
166
/* if set, all results are concated and compared against this file */
 
167
const char *result_file_name= 0;
201
168
 
202
169
typedef struct st_var
203
170
{
215
182
/*Perl/shell-like variable registers */
216
183
VAR var_reg[10];
217
184
 
218
 
 
219
 
boost::unordered_map<string, VAR *> var_hash;
 
185
HASH var_hash;
220
186
 
221
187
struct st_connection
222
188
{
223
 
  drizzle_st *drizzle;
224
 
  drizzle_con_st con;
 
189
  DRIZZLE drizzle;
225
190
  /* Used when creating views and sp, to avoid implicit commit */
226
 
  drizzle_con_st *util_con;
 
191
  DRIZZLE *util_drizzle;
227
192
  char *name;
228
193
};
229
194
struct st_connection connections[128];
379
344
  enum match_err_type type;
380
345
  union
381
346
  {
382
 
    uint32_t errnum;
383
 
    char sqlstate[DRIZZLE_MAX_SQLSTATE_SIZE+1];  /* \0 terminated string */
 
347
    uint errnum;
 
348
    char sqlstate[SQLSTATE_LENGTH+1];  /* \0 terminated string */
384
349
  } code;
385
350
};
386
351
 
387
352
struct st_expected_errors
388
353
{
389
354
  struct st_match_err err[10];
390
 
  uint32_t count;
 
355
  uint count;
391
356
};
392
357
static struct st_expected_errors saved_expected_errors;
393
358
 
396
361
  char *query, *query_buf,*first_argument,*last_argument,*end;
397
362
  int first_word_len, query_len;
398
363
  bool abort_on_error;
399
 
  st_expected_errors expected_errors;
400
 
  string require_file;
 
364
  struct st_expected_errors expected_errors;
 
365
  char require_file[FN_REFLEN];
401
366
  enum enum_commands type;
402
 
 
403
 
  st_command()
404
 
    : query(NULL), query_buf(NULL), first_argument(NULL), last_argument(NULL),
405
 
      end(NULL), first_word_len(0), query_len(0), abort_on_error(false),
406
 
      require_file(""), type(Q_CONNECTION)
407
 
  {
408
 
    memset(&expected_errors, 0, sizeof(st_expected_errors));
409
 
  }
410
 
 
411
 
  ~st_command()
412
 
  {
413
 
    if (query_buf != NULL)
414
 
    {
415
 
      free(query_buf);
416
 
    }
417
 
  }
418
367
};
419
368
 
420
369
TYPELIB command_typelib= {array_elements(command_names),"",
438
387
VAR* var_from_env(const char *, const char *);
439
388
VAR* var_init(VAR* v, const char *name, int name_len, const char *val,
440
389
              int val_len);
441
 
void var_free(pair<string, VAR*> v);
 
390
extern "C" void var_free(void* v);
442
391
VAR* var_get(const char *var_name, const char** var_name_end,
443
392
             bool raw, bool ignore_not_existing);
444
393
void eval_expr(VAR* v, const char *p, const char** p_end);
445
 
bool match_delimiter(int c, const char *delim, uint32_t length);
 
394
bool match_delimiter(int c, const char *delim, uint length);
446
395
void dump_result_to_reject_file(char *buf, int size);
447
396
void dump_result_to_log_file(const char *buf, int size);
448
397
void dump_warning_messages(void);
455
404
 
456
405
/* For replace_column */
457
406
static char *replace_column[MAX_COLUMNS];
458
 
static uint32_t max_replace_column= 0;
 
407
static uint max_replace_column= 0;
459
408
void do_get_replace_column(struct st_command*);
460
409
void free_replace_column(void);
461
410
 
480
429
void replace_append_mem(string *ds, const char *val,
481
430
                        int len);
482
431
void replace_append(string *ds, const char *val);
483
 
void replace_append_uint(string *ds, uint32_t val);
 
432
void replace_append_uint(string *ds, uint val);
484
433
void append_sorted(string* ds, string* ds_input);
485
434
 
486
435
void handle_error(struct st_command*,
489
438
void handle_no_error(struct st_command*);
490
439
 
491
440
 
 
441
#define do_send_query(cn,q,q_len,flags) drizzle_send_query(&cn->drizzle, q, q_len)
 
442
 
492
443
void do_eval(string *query_eval, const char *query,
493
444
             const char *query_end, bool pass_through_escape_chars)
494
445
{
559
510
  options are passed.
560
511
*/
561
512
 
562
 
static void append_os_quoted(string *str, const char *append, ...)
 
513
void append_os_quoted(string *str, const char *append, ...)
563
514
{
564
515
  const char *quote_str= "\'";
565
 
  const uint32_t  quote_len= 1;
 
516
  const uint  quote_len= 1;
566
517
 
567
518
  va_list dirty_text;
568
519
 
602
553
 
603
554
*/
604
555
 
605
 
static void show_query(drizzle_con_st *con, const char* query)
 
556
static void show_query(DRIZZLE *drizzle, const char* query)
606
557
{
607
 
  drizzle_result_st res;
608
 
  drizzle_return_t ret;
609
 
 
610
 
  if (!con)
 
558
  DRIZZLE_RES* res;
 
559
 
 
560
 
 
561
  if (!drizzle)
611
562
    return;
612
563
 
613
 
  if (drizzle_query_str(con, &res, query, &ret) == NULL ||
614
 
      ret != DRIZZLE_RETURN_OK)
 
564
  if (drizzle_query(drizzle, query))
615
565
  {
616
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
617
 
    {
618
 
      log_msg("Error running query '%s': %d %s",
619
 
              query, drizzle_result_error_code(&res),
620
 
              drizzle_result_error(&res));
621
 
      drizzle_result_free(&res);
622
 
    }
623
 
    else
624
 
    {
625
 
      log_msg("Error running query '%s': %d %s",
626
 
              query, ret, drizzle_con_error(con));
627
 
    }
 
566
    log_msg("Error running query '%s': %d %s",
 
567
            query, drizzle_errno(drizzle), drizzle_error(drizzle));
628
568
    return;
629
569
  }
630
570
 
631
 
  if (drizzle_result_column_count(&res) == 0 ||
632
 
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
 
571
  if ((res= drizzle_store_result(drizzle)) == NULL)
633
572
  {
634
573
    /* No result set returned */
635
 
    drizzle_result_free(&res);
636
574
    return;
637
575
  }
638
576
 
639
577
  {
640
 
    drizzle_row_t row;
 
578
    DRIZZLE_ROW row;
641
579
    unsigned int i;
642
580
    unsigned int row_num= 0;
643
 
    unsigned int num_fields= drizzle_result_column_count(&res);
644
 
    drizzle_column_st *column;
 
581
    unsigned int num_fields= drizzle_num_fields(res);
 
582
    const DRIZZLE_FIELD *fields= drizzle_fetch_fields(res);
645
583
 
646
584
    fprintf(stderr, "=== %s ===\n", query);
647
 
    while ((row= drizzle_row_next(&res)))
 
585
    while ((row= drizzle_fetch_row(res)))
648
586
    {
649
 
      size_t *lengths= drizzle_row_field_sizes(&res);
 
587
      uint32_t *lengths= drizzle_fetch_lengths(res);
650
588
      row_num++;
651
589
 
652
590
      fprintf(stderr, "---- %d. ----\n", row_num);
653
 
      drizzle_column_seek(&res, 0);
654
591
      for(i= 0; i < num_fields; i++)
655
592
      {
656
 
        column= drizzle_column_next(&res);
657
593
        fprintf(stderr, "%s\t%.*s\n",
658
 
                drizzle_column_name(column),
 
594
                fields[i].name,
659
595
                (int)lengths[i], row[i] ? row[i] : "NULL");
660
596
      }
661
597
    }
663
599
      fprintf(stderr, "=");
664
600
    fprintf(stderr, "\n\n");
665
601
  }
666
 
  drizzle_result_free(&res);
 
602
  drizzle_free_result(res);
667
603
 
668
604
  return;
669
605
}
682
618
 
683
619
*/
684
620
 
685
 
static void show_warnings_before_error(drizzle_con_st *con)
 
621
static void show_warnings_before_error(DRIZZLE *drizzle)
686
622
{
687
 
  drizzle_result_st res;
688
 
  drizzle_return_t ret;
 
623
  DRIZZLE_RES* res;
689
624
  const char* query= "SHOW WARNINGS";
690
625
 
691
 
  if (!con)
 
626
 
 
627
  if (!drizzle)
692
628
    return;
693
629
 
694
 
  if (drizzle_query_str(con, &res, query, &ret) == NULL ||
695
 
      ret != DRIZZLE_RETURN_OK)
 
630
  if (drizzle_query(drizzle, query))
696
631
  {
697
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
698
 
    {
699
 
      log_msg("Error running query '%s': %d %s",
700
 
              query, drizzle_result_error_code(&res),
701
 
              drizzle_result_error(&res));
702
 
      drizzle_result_free(&res);
703
 
    }
704
 
    else
705
 
    {
706
 
      log_msg("Error running query '%s': %d %s",
707
 
              query, ret, drizzle_con_error(con));
708
 
    }
 
632
    log_msg("Error running query '%s': %d %s",
 
633
            query, drizzle_errno(drizzle), drizzle_error(drizzle));
709
634
    return;
710
635
  }
711
636
 
712
 
  if (drizzle_result_column_count(&res) == 0 ||
713
 
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
 
637
  if ((res= drizzle_store_result(drizzle)) == NULL)
714
638
  {
715
639
    /* No result set returned */
716
 
    drizzle_result_free(&res);
717
640
    return;
718
641
  }
719
642
 
720
 
  if (drizzle_result_row_count(&res) <= 1)
 
643
  if (drizzle_num_rows(res) <= 1)
721
644
  {
722
645
    /* Don't display the last row, it's "last error" */
723
646
  }
724
647
  else
725
648
  {
726
 
    drizzle_row_t row;
 
649
    DRIZZLE_ROW row;
727
650
    unsigned int row_num= 0;
728
 
    unsigned int num_fields= drizzle_result_column_count(&res);
 
651
    unsigned int num_fields= drizzle_num_fields(res);
729
652
 
730
653
    fprintf(stderr, "\nWarnings from just before the error:\n");
731
 
    while ((row= drizzle_row_next(&res)))
 
654
    while ((row= drizzle_fetch_row(res)))
732
655
    {
733
656
      uint32_t i;
734
 
      size_t *lengths= drizzle_row_field_sizes(&res);
 
657
      uint32_t *lengths= drizzle_fetch_lengths(res);
735
658
 
736
 
      if (++row_num >= drizzle_result_row_count(&res))
 
659
      if (++row_num >= drizzle_num_rows(res))
737
660
      {
738
661
        /* Don't display the last row, it's "last error" */
739
662
        break;
747
670
      fprintf(stderr, "\n");
748
671
    }
749
672
  }
750
 
  drizzle_result_free(&res);
 
673
  drizzle_free_result(res);
751
674
 
752
675
  return;
753
676
}
780
703
  for (i= 0; i < num_args; i++)
781
704
  {
782
705
    const struct command_arg *arg= &args[i];
783
 
    arg->ds->clear();
784
706
 
785
 
    bool known_arg_type= true;
786
707
    switch (arg->type) {
787
708
      /* A string */
788
709
    case ARG_STRING:
817
738
      break;
818
739
 
819
740
    default:
820
 
      known_arg_type= false;
 
741
      assert("Unknown argument type");
821
742
      break;
822
743
    }
823
 
    assert(known_arg_type);
824
744
 
825
745
    /* Check required arg */
826
746
    if (arg->ds->length() == 0 && arg->required)
841
761
}
842
762
 
843
763
 
844
 
static void handle_command_error(struct st_command *command, uint32_t error)
 
764
static void handle_command_error(struct st_command *command, uint error)
845
765
{
846
766
 
847
767
  if (error != 0)
848
768
  {
849
 
    uint32_t i;
 
769
    uint i;
850
770
 
851
771
    if (command->abort_on_error)
852
772
      die("command \"%.*s\" failed with error %d",
876
796
 
877
797
static void close_connections(void)
878
798
{
 
799
 
879
800
  for (--next_con; next_con >= connections; --next_con)
880
801
  {
881
 
    if (next_con->drizzle != NULL)
882
 
    {
883
 
      drizzle_free(next_con->drizzle);
884
 
      next_con->drizzle= NULL;
885
 
    }
 
802
    drizzle_close(&next_con->drizzle);
 
803
    if (next_con->util_drizzle)
 
804
      drizzle_close(next_con->util_drizzle);
886
805
    free(next_con->name);
887
806
  }
888
807
  return;
896
815
  {
897
816
    if (cur_file->file && cur_file->file != stdin)
898
817
    {
899
 
      fclose(cur_file->file);
 
818
      my_fclose(cur_file->file, MYF(0));
900
819
    }
901
820
    free((unsigned char*) cur_file->file_name);
902
821
    cur_file->file_name= 0;
907
826
 
908
827
static void free_used_memory(void)
909
828
{
910
 
  uint32_t i;
 
829
  uint i;
911
830
 
912
831
 
913
832
  close_connections();
914
833
  close_files();
915
 
  for_each(var_hash.begin(), var_hash.end(), var_free);
916
 
  var_hash.clear();
 
834
  hash_free(&var_hash);
917
835
 
918
 
  vector<st_command *>::iterator iter;
 
836
  vector<struct st_command *>::iterator iter;
919
837
  for (iter= q_lines.begin() ; iter < q_lines.end() ; iter++)
920
838
  {
921
839
    struct st_command * q_line= *iter;
922
 
    delete q_line;
 
840
    if (q_line->query_buf != NULL)
 
841
    {
 
842
      free(q_line->query_buf);
 
843
    }
 
844
    free(q_line);
923
845
  }
924
846
 
925
847
  for (i= 0; i < 10; i++)
927
849
    if (var_reg[i].alloced_len)
928
850
      free(var_reg[i].str_val);
929
851
  }
 
852
  while (embedded_server_arg_count > 1)
 
853
    free(embedded_server_args[--embedded_server_arg_count]);
930
854
 
931
855
  free_all_replace();
932
856
  free(opt_pass);
 
857
  free_defaults(default_argv);
933
858
 
934
859
  return;
935
860
}
938
863
static void cleanup_and_exit(int exit_code)
939
864
{
940
865
  free_used_memory();
941
 
  internal::my_end();
 
866
  my_end(my_end_arg);
942
867
 
943
868
  if (!silent) {
944
869
    switch (exit_code) {
1011
936
  }
1012
937
 
1013
938
  /* Dump the result that has been accumulated so far to .log file */
1014
 
  if (! result_file_name.empty() && ds_res.length())
 
939
  if (result_file_name && ds_res.length())
1015
940
    dump_result_to_log_file(ds_res.c_str(), ds_res.length());
1016
941
 
1017
942
  /* Dump warning messages */
1018
 
  if (! result_file_name.empty() && ds_warning_messages.length())
 
943
  if (result_file_name && ds_warning_messages.length())
1019
944
    dump_warning_messages();
1020
945
 
1021
946
  /*
1023
948
    been produced prior to the error
1024
949
  */
1025
950
  if (cur_con)
1026
 
    show_warnings_before_error(&cur_con->con);
 
951
    show_warnings_before_error(&cur_con->drizzle);
1027
952
 
1028
953
  cleanup_and_exit(1);
1029
954
}
1148
1073
static void cat_file(string* ds, const char* filename)
1149
1074
{
1150
1075
  int fd;
1151
 
  uint32_t len;
 
1076
  uint len;
1152
1077
  char buff[512];
1153
1078
 
1154
 
  if ((fd= internal::my_open(filename, O_RDONLY, MYF(0))) < 0)
 
1079
  if ((fd= my_open(filename, O_RDONLY, MYF(0))) < 0)
1155
1080
    die("Failed to open file '%s'", filename);
1156
 
  while((len= internal::my_read(fd, (unsigned char*)&buff,
 
1081
  while((len= my_read(fd, (unsigned char*)&buff,
1157
1082
                      sizeof(buff), MYF(0))) > 0)
1158
1083
  {
1159
1084
    char *p= buff, *start= buff;
1175
1100
    /* Output any chars that might be left */
1176
1101
    ds->append(start, p-start);
1177
1102
  }
1178
 
  internal::my_close(fd, MYF(0));
 
1103
  my_close(fd, MYF(0));
1179
1104
}
1180
1105
 
1181
1106
 
1351
1276
 
1352
1277
*/
1353
1278
 
1354
 
static int compare_files2(int fd, const char* filename2)
 
1279
static int compare_files2(File fd, const char* filename2)
1355
1280
{
1356
1281
  int error= RESULT_OK;
1357
 
  int fd2;
1358
 
  uint32_t len, len2;
 
1282
  File fd2;
 
1283
  uint len, len2;
1359
1284
  char buff[512], buff2[512];
1360
 
  const char *fname= filename2;
1361
 
  string tmpfile;
1362
1285
 
1363
 
  if ((fd2= internal::my_open(fname, O_RDONLY, MYF(0))) < 0)
 
1286
  if ((fd2= my_open(filename2, O_RDONLY, MYF(0))) < 0)
1364
1287
  {
1365
 
    internal::my_close(fd, MYF(0));
1366
 
    if (! opt_testdir.empty())
1367
 
    {
1368
 
      tmpfile= opt_testdir;
1369
 
      if (tmpfile[tmpfile.length()] != '/')
1370
 
        tmpfile.append("/");
1371
 
      tmpfile.append(filename2);
1372
 
      fname= tmpfile.c_str();
1373
 
    }
1374
 
    if ((fd2= internal::my_open(fname, O_RDONLY, MYF(0))) < 0)
1375
 
    {
1376
 
      internal::my_close(fd, MYF(0));
1377
 
    
1378
 
      die("Failed to open second file: '%s'", fname);
1379
 
    }
 
1288
    my_close(fd, MYF(0));
 
1289
    die("Failed to open second file: '%s'", filename2);
1380
1290
  }
1381
 
  while((len= internal::my_read(fd, (unsigned char*)&buff,
 
1291
  while((len= my_read(fd, (unsigned char*)&buff,
1382
1292
                      sizeof(buff), MYF(0))) > 0)
1383
1293
  {
1384
 
    if ((len2= internal::my_read(fd2, (unsigned char*)&buff2,
 
1294
    if ((len2= my_read(fd2, (unsigned char*)&buff2,
1385
1295
                       sizeof(buff2), MYF(0))) < len)
1386
1296
    {
1387
1297
      /* File 2 was smaller */
1401
1311
      break;
1402
1312
    }
1403
1313
  }
1404
 
  if (!error && internal::my_read(fd2, (unsigned char*)&buff2,
 
1314
  if (!error && my_read(fd2, (unsigned char*)&buff2,
1405
1315
                        sizeof(buff2), MYF(0)) > 0)
1406
1316
  {
1407
1317
    /* File 1 was smaller */
1408
1318
    error= RESULT_LENGTH_MISMATCH;
1409
1319
  }
1410
1320
 
1411
 
  internal::my_close(fd2, MYF(0));
 
1321
  my_close(fd2, MYF(0));
1412
1322
 
1413
1323
  return error;
1414
1324
}
1429
1339
 
1430
1340
static int compare_files(const char* filename1, const char* filename2)
1431
1341
{
1432
 
  int fd;
 
1342
  File fd;
1433
1343
  int error;
1434
1344
 
1435
 
  if ((fd= internal::my_open(filename1, O_RDONLY, MYF(0))) < 0)
 
1345
  if ((fd= my_open(filename1, O_RDONLY, MYF(0))) < 0)
1436
1346
    die("Failed to open first file: '%s'", filename1);
1437
1347
 
1438
1348
  error= compare_files2(fd, filename2);
1439
1349
 
1440
 
  internal::my_close(fd, MYF(0));
 
1350
  my_close(fd, MYF(0));
1441
1351
 
1442
1352
  return error;
1443
1353
}
1458
1368
static int string_cmp(string* ds, const char *fname)
1459
1369
{
1460
1370
  int error;
1461
 
  int fd;
 
1371
  File fd;
1462
1372
  char temp_file_path[FN_REFLEN];
1463
1373
 
1464
 
  if ((fd= internal::create_temp_file(temp_file_path, TMPDIR,
1465
 
                            "tmp", MYF(MY_WME))) < 0)
 
1374
  if ((fd= create_temp_file(temp_file_path, NULL,
 
1375
                            "tmp", O_CREAT | O_RDWR,
 
1376
                            MYF(MY_WME))) < 0)
1466
1377
    die("Failed to create temporary file for ds");
1467
1378
 
1468
1379
  /* Write ds to temporary file and set file pos to beginning*/
1469
 
  if (internal::my_write(fd, (unsigned char *) ds->c_str(), ds->length(),
 
1380
  if (my_write(fd, (unsigned char *) ds->c_str(), ds->length(),
1470
1381
               MYF(MY_FNABP | MY_WME)) ||
1471
 
      lseek(fd, 0, SEEK_SET) == MY_FILEPOS_ERROR)
 
1382
      my_seek(fd, 0, SEEK_SET, MYF(0)) == MY_FILEPOS_ERROR)
1472
1383
  {
1473
 
    internal::my_close(fd, MYF(0));
 
1384
    my_close(fd, MYF(0));
1474
1385
    /* Remove the temporary file */
1475
 
    internal::my_delete(temp_file_path, MYF(0));
 
1386
    my_delete(temp_file_path, MYF(0));
1476
1387
    die("Failed to write file '%s'", temp_file_path);
1477
1388
  }
1478
1389
 
1479
1390
  error= compare_files2(fd, fname);
1480
1391
 
1481
 
  internal::my_close(fd, MYF(0));
 
1392
  my_close(fd, MYF(0));
1482
1393
  /* Remove the temporary file */
1483
 
  internal::my_delete(temp_file_path, MYF(0));
 
1394
  my_delete(temp_file_path, MYF(0));
1484
1395
 
1485
1396
  return(error);
1486
1397
}
1503
1414
  const char* mess= "Result content mismatch\n";
1504
1415
 
1505
1416
 
1506
 
  assert(result_file_name.c_str());
1507
 
 
1508
 
  if (access(result_file_name.c_str(), F_OK) != 0)
1509
 
    die("The specified result file does not exist: '%s'", result_file_name.c_str());
1510
 
 
1511
 
  switch (string_cmp(ds, result_file_name.c_str())) {
 
1417
  assert(result_file_name);
 
1418
 
 
1419
  if (access(result_file_name, F_OK) != 0)
 
1420
    die("The specified result file does not exist: '%s'", result_file_name);
 
1421
 
 
1422
  switch (string_cmp(ds, result_file_name)) {
1512
1423
  case RESULT_OK:
1513
1424
    break; /* ok */
1514
1425
  case RESULT_LENGTH_MISMATCH:
1522
1433
    */
1523
1434
    char reject_file[FN_REFLEN];
1524
1435
    size_t reject_length;
1525
 
    internal::dirname_part(reject_file, result_file_name.c_str(), &reject_length);
 
1436
    dirname_part(reject_file, result_file_name, &reject_length);
1526
1437
 
1527
1438
    if (access(reject_file, W_OK) == 0)
1528
1439
    {
1529
1440
      /* Result file directory is writable, save reject file there */
1530
 
      internal::fn_format(reject_file, result_file_name.c_str(), NULL,
 
1441
      fn_format(reject_file, result_file_name, NULL,
1531
1442
                ".reject", MY_REPLACE_EXT);
1532
1443
    }
1533
1444
    else
1534
1445
    {
1535
1446
      /* Put reject file in opt_logdir */
1536
 
      internal::fn_format(reject_file, result_file_name.c_str(), opt_logdir.c_str(),
 
1447
      fn_format(reject_file, result_file_name, opt_logdir,
1537
1448
                ".reject", MY_REPLACE_DIR | MY_REPLACE_EXT);
1538
1449
    }
1539
1450
    str_to_file(reject_file, ds->c_str(), ds->length());
1540
1451
 
1541
1452
    ds->erase(); /* Don't create a .log file */
1542
1453
 
1543
 
    show_diff(NULL, result_file_name.c_str(), reject_file);
 
1454
    show_diff(NULL, result_file_name, reject_file);
1544
1455
    die("%s",mess);
1545
1456
    break;
1546
1457
  }
1567
1478
 
1568
1479
*/
1569
1480
 
1570
 
static void check_require(string* ds, const string &fname)
 
1481
static void check_require(string* ds, const char *fname)
1571
1482
{
1572
1483
 
1573
1484
 
1574
 
  if (string_cmp(ds, fname.c_str()))
 
1485
  if (string_cmp(ds, fname))
1575
1486
  {
1576
1487
    char reason[FN_REFLEN];
1577
 
    internal::fn_format(reason, fname.c_str(), "", "", MY_REPLACE_EXT | MY_REPLACE_DIR);
 
1488
    fn_format(reason, fname, "", "", MY_REPLACE_EXT | MY_REPLACE_DIR);
1578
1489
    abort_not_supported_test("Test requires: '%s'", reason);
1579
1490
  }
1580
1491
  return;
1625
1536
}
1626
1537
 
1627
1538
 
 
1539
extern "C"
 
1540
unsigned char *get_var_key(const unsigned char* var, size_t *len, bool)
 
1541
{
 
1542
  register char* key;
 
1543
  key = ((VAR*)var)->name;
 
1544
  *len = ((VAR*)var)->name_len;
 
1545
  return (unsigned char*)key;
 
1546
}
 
1547
 
1628
1548
 
1629
1549
VAR *var_init(VAR *v, const char *name, int name_len, const char *val,
1630
1550
              int val_len)
1636
1556
  if (!val_len && val)
1637
1557
    val_len = strlen(val) ;
1638
1558
  val_alloc_len = val_len + 16; /* room to grow */
1639
 
  if (!(tmp_var=v) && !(tmp_var = (VAR*)malloc(sizeof(*tmp_var)
1640
 
                                               + name_len+1)))
 
1559
  if (!(tmp_var=v) && !(tmp_var = (VAR*)my_malloc(sizeof(*tmp_var)
 
1560
                                                  + name_len+1, MYF(MY_WME))))
1641
1561
    die("Out of memory");
1642
1562
 
1643
1563
  tmp_var->name = (name) ? (char*) tmp_var + sizeof(*tmp_var) : 0;
1644
1564
  tmp_var->alloced = (v == 0);
1645
1565
 
1646
 
  if (!(tmp_var->str_val = (char *)malloc(val_alloc_len+1)))
 
1566
  if (!(tmp_var->str_val = (char *)my_malloc(val_alloc_len+1, MYF(MY_WME))))
1647
1567
    die("Out of memory");
1648
1568
 
1649
1569
  memcpy(tmp_var->name, name, name_len);
1662
1582
}
1663
1583
 
1664
1584
 
1665
 
void var_free(pair<string, VAR *> v)
 
1585
void var_free(void *v)
1666
1586
{
1667
 
  free(v.second->str_val);
1668
 
  free(v.second->env_s);
1669
 
  if (v.second->alloced)
1670
 
    free(v.second);
 
1587
  free(((VAR*) v)->str_val);
 
1588
  free(((VAR*) v)->env_s);
 
1589
  if (((VAR*)v)->alloced)
 
1590
    free(v);
1671
1591
}
1672
1592
 
1673
1593
 
1679
1599
    tmp = def_val;
1680
1600
 
1681
1601
  v = var_init(0, name, strlen(name), tmp, strlen(tmp));
1682
 
  string var_name(name);
1683
 
  var_hash.insert(make_pair(var_name, v));
 
1602
  my_hash_insert(&var_hash, (unsigned char*)v);
1684
1603
  return v;
1685
1604
}
1686
1605
 
1697
1616
  if (digit < 0 || digit >= 10)
1698
1617
  {
1699
1618
    const char *save_var_name = var_name, *end;
1700
 
    uint32_t length;
 
1619
    uint length;
1701
1620
    end = (var_name_end) ? *var_name_end : 0;
1702
1621
    while (my_isvar(charset_info,*var_name) && var_name != end)
1703
1622
      var_name++;
1707
1626
        return(0);
1708
1627
      die("Empty variable");
1709
1628
    }
1710
 
    length= (uint32_t) (var_name - save_var_name);
 
1629
    length= (uint) (var_name - save_var_name);
1711
1630
    if (length >= MAX_VAR_NAME_LENGTH)
1712
1631
      die("Too long variable name: %s", save_var_name);
1713
1632
 
1714
 
    string save_var_name_str(save_var_name, length);
1715
 
    boost::unordered_map<string, VAR*>::iterator iter=
1716
 
      var_hash.find(save_var_name_str);
1717
 
    if (iter == var_hash.end())
 
1633
    if (!(v = (VAR*) hash_search(&var_hash, (const unsigned char*) save_var_name,
 
1634
                                 length)))
1718
1635
    {
1719
1636
      char buff[MAX_VAR_NAME_LENGTH+1];
1720
1637
      strncpy(buff, save_var_name, length);
1721
1638
      buff[length]= '\0';
1722
1639
      v= var_from_env(buff, "");
1723
1640
    }
1724
 
    else
1725
 
    {
1726
 
      v= (*iter).second;
1727
 
    }
1728
1641
    var_name--;  /* Point at last character */
1729
1642
  }
1730
1643
  else
1749
1662
 
1750
1663
static VAR *var_obtain(const char *name, int len)
1751
1664
{
1752
 
  string var_name(name, len);
1753
 
  boost::unordered_map<string, VAR*>::iterator iter=
1754
 
    var_hash.find(var_name);
1755
 
  if (iter != var_hash.end())
1756
 
    return (*iter).second;
1757
 
  VAR *v = var_init(0, name, len, "", 0);
1758
 
  var_hash.insert(make_pair(var_name, v));
 
1665
  VAR* v;
 
1666
  if ((v = (VAR*)hash_search(&var_hash, (const unsigned char *) name, len)))
 
1667
    return v;
 
1668
  v = var_init(0, name, len, "", 0);
 
1669
  my_hash_insert(&var_hash, (unsigned char*)v);
1759
1670
  return v;
1760
1671
}
1761
1672
 
1780
1691
  digit= *var_name - '0';
1781
1692
  if (!(digit < 10 && digit >= 0))
1782
1693
  {
1783
 
    v= var_obtain(var_name, (uint32_t) (var_name_end - var_name));
 
1694
    v= var_obtain(var_name, (uint) (var_name_end - var_name));
1784
1695
  }
1785
1696
  else
1786
1697
    v= var_reg + digit;
1799
1710
    snprintf(buf, sizeof(buf), "%.*s=%.*s",
1800
1711
             v->name_len, v->name,
1801
1712
             v->str_val_len, v->str_val);
1802
 
    if (!(v->env_s= strdup(buf)))
 
1713
    if (!(v->env_s= my_strdup(buf, MYF(MY_WME))))
1803
1714
      die("Out of memory");
1804
1715
    putenv(v->env_s);
1805
1716
    free(old_env_s);
1824
1735
 
1825
1736
/*
1826
1737
  Store an integer (typically the returncode of the last SQL)
1827
 
  statement in the drizzletest builtin variable $drizzleclient_errno
 
1738
  statement in the drizzletest builtin variable $drizzle_errno
1828
1739
*/
1829
1740
 
1830
1741
static void var_set_errno(int sql_errno)
1831
1742
{
1832
 
  var_set_int("$drizzleclient_errno", sql_errno);
 
1743
  var_set_int("$drizzle_errno", sql_errno);
1833
1744
}
1834
1745
 
1835
1746
 
1836
1747
/*
1837
 
  Update $drizzleclient_get_server_version variable with version
 
1748
  Update $drizzle_get_server_version variable with version
1838
1749
  of the currently connected server
1839
1750
*/
1840
1751
 
1841
 
static void var_set_drizzleclient_get_server_version(drizzle_con_st *con)
 
1752
static void var_set_drizzle_get_server_version(DRIZZLE *drizzle)
1842
1753
{
1843
 
  var_set_int("$drizzle_con_server_version", drizzle_con_server_version_number(con));
 
1754
  var_set_int("$drizzle_get_server_version", drizzle_get_server_version(drizzle));
1844
1755
}
1845
1756
 
1846
1757
 
1871
1782
{
1872
1783
  const char *end = (char*)((query_end && *query_end) ?
1873
1784
                            *query_end : query + strlen(query));
1874
 
  drizzle_result_st res;
1875
 
  drizzle_return_t ret;
1876
 
  drizzle_row_t row;
1877
 
  drizzle_con_st *con= &cur_con->con;
 
1785
  DRIZZLE_RES *res;
 
1786
  DRIZZLE_ROW row;
 
1787
  DRIZZLE *drizzle= &cur_con->drizzle;
1878
1788
  string ds_query;
1879
1789
 
1880
1790
 
1887
1797
  /* Eval the query, thus replacing all environment variables */
1888
1798
  do_eval(&ds_query, query, end, false);
1889
1799
 
1890
 
  if (drizzle_query(con, &res, ds_query.c_str(), ds_query.length(),
1891
 
                    &ret) == NULL ||
1892
 
      ret != DRIZZLE_RETURN_OK)
1893
 
  {
1894
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
1895
 
    {
1896
 
      die("Error running query '%s': %d %s", ds_query.c_str(),
1897
 
          drizzle_result_error_code(&res), drizzle_result_error(&res));
1898
 
      drizzle_result_free(&res);
1899
 
    }
1900
 
    else
1901
 
    {
1902
 
      die("Error running query '%s': %d %s", ds_query.c_str(), ret,
1903
 
          drizzle_con_error(con));
1904
 
    }
1905
 
  }
1906
 
  if (drizzle_result_column_count(&res) == 0 ||
1907
 
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
 
1800
  if (drizzle_real_query(drizzle, ds_query.c_str(), ds_query.length()))
 
1801
    die("Error running query '%s': %d %s", ds_query.c_str(),
 
1802
        drizzle_errno(drizzle), drizzle_error(drizzle));
 
1803
  if (!(res= drizzle_store_result(drizzle)))
1908
1804
    die("Query '%s' didn't return a result set", ds_query.c_str());
1909
1805
 
1910
 
  if ((row= drizzle_row_next(&res)) && row[0])
 
1806
  if ((row= drizzle_fetch_row(res)) && row[0])
1911
1807
  {
1912
1808
    /*
1913
1809
      Concatenate all fields in the first row with tab in between
1915
1811
    */
1916
1812
    string result;
1917
1813
    uint32_t i;
1918
 
    size_t *lengths;
 
1814
    uint32_t *lengths;
1919
1815
 
1920
 
    lengths= drizzle_row_field_sizes(&res);
1921
 
    for (i= 0; i < drizzle_result_column_count(&res); i++)
 
1816
    lengths= drizzle_fetch_lengths(res);
 
1817
    for (i= 0; i < drizzle_num_fields(res); i++)
1922
1818
    {
1923
1819
      if (row[i])
1924
1820
      {
1933
1829
  else
1934
1830
    eval_expr(var, "", 0);
1935
1831
 
1936
 
  drizzle_result_free(&res);
 
1832
  drizzle_free_result(res);
1937
1833
  return;
1938
1834
}
1939
1835
 
1964
1860
{
1965
1861
  long row_no;
1966
1862
  int col_no= -1;
1967
 
  drizzle_result_st res;
1968
 
  drizzle_return_t ret;
1969
 
  drizzle_con_st *con= &cur_con->con;
 
1863
  DRIZZLE_RES* res;
 
1864
  DRIZZLE *drizzle= &cur_con->drizzle;
1970
1865
 
1971
1866
  string ds_query;
1972
1867
  string ds_col;
1985
1880
                     ',');
1986
1881
 
1987
1882
  /* Convert row number to int */
1988
 
  row_no= atoi(ds_row.c_str());
1989
 
  
1990
 
  istringstream buff(ds_row);
1991
 
  if ((buff >> row_no).fail())
 
1883
  if (!str2int(ds_row.c_str(), 10, (long) 0, (long) INT_MAX, &row_no))
1992
1884
    die("Invalid row number: '%s'", ds_row.c_str());
1993
1885
 
1994
1886
  /* Remove any surrounding "'s from the query - if there is any */
2000
1892
  ds_query.append(unstripped_query);
2001
1893
 
2002
1894
  /* Run the query */
2003
 
  if (drizzle_query(con, &res, ds_query.c_str(), ds_query.length(),
2004
 
                    &ret) == NULL ||
2005
 
      ret != DRIZZLE_RETURN_OK)
2006
 
  {
2007
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
2008
 
    {
2009
 
      die("Error running query '%s': %d %s", ds_query.c_str(),
2010
 
          drizzle_result_error_code(&res), drizzle_result_error(&res));
2011
 
      drizzle_result_free(&res);
2012
 
    }
2013
 
    else
2014
 
    {
2015
 
      die("Error running query '%s': %d %s", ds_query.c_str(), ret,
2016
 
          drizzle_con_error(con));
2017
 
    }
2018
 
  }
2019
 
  if (drizzle_result_column_count(&res) == 0 ||
2020
 
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
 
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)))
2021
1899
    die("Query '%s' didn't return a result set", ds_query.c_str());
2022
1900
 
2023
1901
  {
2024
1902
    /* Find column number from the given column name */
2025
 
    uint32_t i;
2026
 
    uint32_t num_fields= drizzle_result_column_count(&res);
2027
 
    drizzle_column_st *column;
 
1903
    uint i;
 
1904
    uint num_fields= drizzle_num_fields(res);
 
1905
    const DRIZZLE_FIELD *fields= drizzle_fetch_fields(res);
2028
1906
 
2029
1907
    for (i= 0; i < num_fields; i++)
2030
1908
    {
2031
 
      column= drizzle_column_next(&res);
2032
 
      if (strcmp(drizzle_column_name(column), ds_col.c_str()) == 0 &&
2033
 
          strlen(drizzle_column_name(column)) == ds_col.length())
 
1909
      if (strcmp(fields[i].name, ds_col.c_str()) == 0 &&
 
1910
          strlen(fields[i].name) == ds_col.length())
2034
1911
      {
2035
1912
        col_no= i;
2036
1913
        break;
2038
1915
    }
2039
1916
    if (col_no == -1)
2040
1917
    {
2041
 
      drizzle_result_free(&res);
 
1918
      drizzle_free_result(res);
2042
1919
      die("Could not find column '%s' in the result of '%s'",
2043
1920
          ds_col.c_str(), ds_query.c_str());
2044
1921
    }
2046
1923
 
2047
1924
  {
2048
1925
    /* Get the value */
2049
 
    drizzle_row_t row;
 
1926
    DRIZZLE_ROW row;
2050
1927
    long rows= 0;
2051
1928
    const char* value= "No such row";
2052
1929
 
2053
 
    while ((row= drizzle_row_next(&res)))
 
1930
    while ((row= drizzle_fetch_row(res)))
2054
1931
    {
2055
1932
      if (++rows == row_no)
2056
1933
      {
2066
1943
    }
2067
1944
    eval_expr(var, value, 0);
2068
1945
  }
2069
 
  drizzle_result_free(&res);
 
1946
  drizzle_free_result(res);
2070
1947
 
2071
1948
  return;
2072
1949
}
2078
1955
  dest->int_dirty= src->int_dirty;
2079
1956
 
2080
1957
  /* Alloc/realloc data for str_val in dest */
2081
 
  if (dest->alloced_len < src->alloced_len)
2082
 
  {
2083
 
    char *tmpptr= (char *)realloc(dest->str_val, src->alloced_len);
2084
 
    if (tmpptr == NULL)
2085
 
      die("Out of memory");
2086
 
    dest->str_val= tmpptr;
2087
 
  }
 
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");
2088
1963
  else
2089
1964
    dest->alloced_len= src->alloced_len;
2090
1965
 
2117
1992
    const size_t len= strlen(get_value_str);
2118
1993
    if (strncmp(p, get_value_str, len)==0)
2119
1994
    {
2120
 
      st_command command;
 
1995
      struct st_command command;
 
1996
      memset(&command, 0, sizeof(command));
2121
1997
      command.query= (char*)p;
2122
1998
      command.first_word_len= len;
2123
1999
      command.first_argument= command.query + len;
2135
2011
      static int MIN_VAR_ALLOC= 32;
2136
2012
      v->alloced_len = (new_val_len < MIN_VAR_ALLOC - 1) ?
2137
2013
        MIN_VAR_ALLOC : new_val_len + 1;
2138
 
      char *tmpptr= (char *)realloc(v->str_val, v->alloced_len+1);
2139
 
      if (tmpptr == NULL)
 
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))))
2140
2018
        die("Out of memory");
2141
 
      v->str_val= tmpptr;
2142
2019
    }
2143
2020
    v->str_val_len = new_val_len;
2144
2021
    memcpy(v->str_val, p, new_val_len);
2154
2031
{
2155
2032
  char buff[FN_REFLEN];
2156
2033
 
2157
 
  if (!internal::test_if_hard_path(name))
 
2034
  if (!test_if_hard_path(name))
2158
2035
  {
2159
 
    snprintf(buff, sizeof(buff), "%s%s",opt_basedir.c_str(),name);
 
2036
    strxmov(buff, opt_basedir, name, NULL);
2160
2037
    name=buff;
2161
2038
  }
2162
 
  internal::fn_format(buff, name, "", "", MY_UNPACK_FILENAME);
 
2039
  fn_format(buff, name, "", "", MY_UNPACK_FILENAME);
2163
2040
 
2164
2041
  if (cur_file == file_stack_end)
2165
2042
    die("Source directives are nesting too deep");
2166
2043
  cur_file++;
2167
 
  if (!(cur_file->file= fopen(buff, "r")))
 
2044
  if (!(cur_file->file = my_fopen(buff, O_RDONLY, MYF(0))))
2168
2045
  {
2169
2046
    cur_file--;
2170
2047
    die("Could not open '%s' for reading", buff);
2171
2048
  }
2172
 
  if (!(cur_file->file_name= strdup(buff)))
2173
 
    die("Out of memory");
 
2049
  cur_file->file_name= my_strdup(buff, MYF(MY_FAE));
2174
2050
  cur_file->lineno=1;
2175
2051
  return(0);
2176
2052
}
2210
2086
    ; /* Do nothing */
2211
2087
  else
2212
2088
  {
2213
 
    if (! opt_testdir.empty())
2214
 
    {
2215
 
      string testdir(opt_testdir);
2216
 
      if (testdir[testdir.length()] != '/')
2217
 
        testdir.append("/");
2218
 
      testdir.append(ds_filename);
2219
 
      ds_filename.swap(testdir);
2220
 
    }
2221
2089
    open_file(ds_filename.c_str());
2222
2090
  }
2223
2091
 
2329
2197
  error= pclose(res_file);
2330
2198
  if (error > 0)
2331
2199
  {
2332
 
    uint32_t status= WEXITSTATUS(error), i;
 
2200
    uint status= WEXITSTATUS(error), i;
2333
2201
    bool ok= 0;
2334
2202
 
2335
2203
    if (command->abort_on_error)
2478
2346
                     rm_args, sizeof(rm_args)/sizeof(struct command_arg),
2479
2347
                     ' ');
2480
2348
 
2481
 
  error= internal::my_delete(ds_filename.c_str(), MYF(0)) != 0;
 
2349
  error= my_delete(ds_filename.c_str(), MYF(0)) != 0;
2482
2350
  handle_command_error(command, error);
2483
2351
  return;
2484
2352
}
2512
2380
                     sizeof(copy_file_args)/sizeof(struct command_arg),
2513
2381
                     ' ');
2514
2382
 
2515
 
  error= (internal::my_copy(ds_from_file.c_str(), ds_to_file.c_str(),
 
2383
  error= (my_copy(ds_from_file.c_str(), ds_to_file.c_str(),
2516
2384
                  MYF(MY_DONT_OVERWRITE_FILE)) != 0);
2517
2385
  handle_command_error(command, error);
2518
2386
  return;
2547
2415
                     ' ');
2548
2416
 
2549
2417
  /* Parse what mode to set */
2550
 
  istringstream buff(ds_mode);
2551
2418
  if (ds_mode.length() != 4 ||
2552
 
      (buff >> oct >> mode).fail())
 
2419
      str2int(ds_mode.c_str(), 8, 0, INT_MAX, &mode) == NULL)
2553
2420
    die("You must write a 4 digit octal number for mode");
2554
2421
 
2555
2422
  handle_command_error(command, chmod(ds_file.c_str(), mode));
2599
2466
 
2600
2467
static void do_mkdir(struct st_command *command)
2601
2468
{
 
2469
  int error;
2602
2470
  string ds_dirname;
2603
 
  int error;
2604
2471
  const struct command_arg mkdir_args[] = {
2605
2472
    {"dirname", ARG_STRING, true, &ds_dirname, "Directory to create"}
2606
2473
  };
2610
2477
                     mkdir_args, sizeof(mkdir_args)/sizeof(struct command_arg),
2611
2478
                     ' ');
2612
2479
 
2613
 
  error= mkdir(ds_dirname.c_str(), (0777 & internal::my_umask_dir)) != 0;
 
2480
  error= my_mkdir(ds_dirname.c_str(), 0777, MYF(0)) != 0;
2614
2481
  handle_command_error(command, error);
2615
2482
  return;
2616
2483
}
2911
2778
{
2912
2779
  char *p= command->first_argument, *name;
2913
2780
  struct st_connection *con;
2914
 
  drizzle_result_st result;
2915
 
  drizzle_return_t ret;
2916
2781
 
2917
2782
  if (!*p)
2918
2783
    die("Missing connection name in send_quit");
2927
2792
  if (!(con= find_connection_by_name(name)))
2928
2793
    die("connection '%s' not found in connection pool", name);
2929
2794
 
2930
 
  if (drizzle_quit(&con->con,&result, &ret))
2931
 
    drizzle_result_free(&result);
 
2795
  simple_command(&con->drizzle,COM_QUIT,0,0,1);
2932
2796
 
2933
2797
  return;
2934
2798
}
2950
2814
 
2951
2815
*/
2952
2816
 
2953
 
static void do_change_user(struct st_command *)
 
2817
static void do_change_user(struct st_command *command)
2954
2818
{
2955
 
  assert(0);
 
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.append(drizzle->user);
 
2837
 
 
2838
  if (!ds_passwd.length())
 
2839
    ds_passwd.append(drizzle->passwd);
 
2840
 
 
2841
  if (!ds_db.length())
 
2842
    ds_db.append(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;
2956
2850
}
2957
2851
 
 
2852
 
2958
2853
/*
2959
2854
  SYNOPSIS
2960
2855
  do_perl
2977
2872
static void do_perl(struct st_command *command)
2978
2873
{
2979
2874
  int error;
2980
 
  int fd;
 
2875
  File fd;
2981
2876
  FILE *res_file;
2982
2877
  char buf[FN_REFLEN];
2983
2878
  char temp_file_path[FN_REFLEN];
3001
2896
  read_until_delimiter(&ds_script, &ds_delimiter);
3002
2897
 
3003
2898
  /* Create temporary file name */
3004
 
  if ((fd= internal::create_temp_file(temp_file_path, getenv("MYSQLTEST_VARDIR"),
3005
 
                            "tmp", MYF(MY_WME))) < 0)
 
2899
  if ((fd= create_temp_file(temp_file_path, getenv("MYSQLTEST_VARDIR"),
 
2900
                            "tmp", O_CREAT | O_RDWR,
 
2901
                            MYF(MY_WME))) < 0)
3006
2902
    die("Failed to create temporary file for perl command");
3007
 
  internal::my_close(fd, MYF(0));
 
2903
  my_close(fd, MYF(0));
3008
2904
 
3009
2905
  str_to_file(temp_file_path, ds_script.c_str(), ds_script.length());
3010
2906
 
3024
2920
  error= pclose(res_file);
3025
2921
 
3026
2922
  /* Remove the temporary file */
3027
 
  internal::my_delete(temp_file_path, MYF(0));
 
2923
  my_delete(temp_file_path, MYF(0));
3028
2924
 
3029
2925
  handle_command_error(command, WEXITSTATUS(error));
3030
2926
  return;
3072
2968
do_wait_for_slave_to_stop(struct st_command *)
3073
2969
{
3074
2970
  static int SLAVE_POLL_INTERVAL= 300000;
3075
 
  drizzle_con_st *con= &cur_con->con;
 
2971
  DRIZZLE *drizzle= &cur_con->drizzle;
3076
2972
  for (;;)
3077
2973
  {
3078
 
    drizzle_result_st res;
3079
 
    drizzle_return_t ret;
3080
 
    drizzle_row_t row;
 
2974
    DRIZZLE_RES *res= NULL;
 
2975
    DRIZZLE_ROW row;
3081
2976
    int done;
3082
2977
 
3083
 
    if (drizzle_query_str(con,&res,"show status like 'Slave_running'",
3084
 
                          &ret) == NULL || ret != DRIZZLE_RETURN_OK)
3085
 
    {
3086
 
      if (ret == DRIZZLE_RETURN_ERROR_CODE)
3087
 
      {
3088
 
        die("Query failed while probing slave for stop: %s",
3089
 
            drizzle_result_error(&res));
3090
 
        drizzle_result_free(&res);
3091
 
      }
3092
 
      else
3093
 
      {
3094
 
        die("Query failed while probing slave for stop: %s",
3095
 
            drizzle_con_error(con));
3096
 
      }
3097
 
    }
3098
 
 
3099
 
    if (drizzle_result_column_count(&res) == 0 ||
3100
 
        drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
3101
 
    {
 
2978
    if (drizzle_query(drizzle,"show status like 'Slave_running'") ||
 
2979
        !(res=drizzle_store_result(drizzle)))
3102
2980
      die("Query failed while probing slave for stop: %s",
3103
 
          drizzle_con_error(con));
3104
 
    }
3105
 
 
3106
 
    if (!(row=drizzle_row_next(&res)) || !row[1])
 
2981
          drizzle_error(drizzle));
 
2982
    if (!(row=drizzle_fetch_row(res)) || !row[1])
3107
2983
    {
3108
 
      drizzle_result_free(&res);
 
2984
      drizzle_free_result(res);
3109
2985
      die("Strange result from query while probing slave for stop");
3110
2986
    }
3111
2987
    done = !strcmp(row[1],"OFF");
3112
 
    drizzle_result_free(&res);
 
2988
    drizzle_free_result(res);
3113
2989
    if (done)
3114
2990
      break;
3115
 
    usleep(SLAVE_POLL_INTERVAL);
 
2991
    my_sleep(SLAVE_POLL_INTERVAL);
3116
2992
  }
3117
2993
  return;
3118
2994
}
3120
2996
 
3121
2997
static void do_sync_with_master2(long offset)
3122
2998
{
3123
 
  drizzle_result_st res;
3124
 
  drizzle_return_t ret;
3125
 
  drizzle_row_t row;
3126
 
  drizzle_con_st *con= &cur_con->con;
 
2999
  DRIZZLE_RES *res;
 
3000
  DRIZZLE_ROW row;
 
3001
  DRIZZLE *drizzle= &cur_con->drizzle;
3127
3002
  char query_buf[FN_REFLEN+128];
3128
3003
  int tries= 0;
3129
3004
 
3130
3005
  if (!master_pos.file[0])
3131
3006
    die("Calling 'sync_with_master' without calling 'save_master_pos'");
3132
3007
 
3133
 
  snprintf(query_buf, sizeof(query_buf), "select master_pos_wait('%s', %ld)", master_pos.file,
 
3008
  sprintf(query_buf, "select master_pos_wait('%s', %ld)", master_pos.file,
3134
3009
          master_pos.pos + offset);
3135
3010
 
3136
3011
wait_for_position:
3137
3012
 
3138
 
  if (drizzle_query_str(con, &res, query_buf, &ret) == NULL ||
3139
 
      ret != DRIZZLE_RETURN_OK)
3140
 
  {
3141
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
3142
 
    {
3143
 
      die("failed in '%s': %d: %s", query_buf, drizzle_result_error_code(&res),
3144
 
           drizzle_result_error(&res));
3145
 
      drizzle_result_free(&res);
3146
 
    }
3147
 
    else
3148
 
      die("failed in '%s': %d: %s", query_buf, ret, drizzle_con_error(con));
3149
 
  }
3150
 
 
3151
 
  if (drizzle_result_column_count(&res) == 0 ||
3152
 
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
3153
 
    die("drizzle_result_buffer() returned NULL for '%s'", query_buf);
3154
 
 
3155
 
  if (!(row= drizzle_row_next(&res)))
3156
 
  {
3157
 
    drizzle_result_free(&res);
 
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);
3158
3022
    die("empty result in %s", query_buf);
3159
3023
  }
3160
3024
  if (!row[0])
3163
3027
      It may be that the slave SQL thread has not started yet, though START
3164
3028
      SLAVE has been issued ?
3165
3029
    */
3166
 
    drizzle_result_free(&res);
 
3030
    drizzle_free_result(res);
3167
3031
    if (tries++ == 30)
3168
3032
    {
3169
 
      show_query(con, "SHOW MASTER STATUS");
3170
 
      show_query(con, "SHOW SLAVE STATUS");
 
3033
      show_query(drizzle, "SHOW MASTER STATUS");
 
3034
      show_query(drizzle, "SHOW SLAVE STATUS");
3171
3035
      die("could not sync with master ('%s' returned NULL)", query_buf);
3172
3036
    }
3173
3037
    sleep(1); /* So at most we will wait 30 seconds and make 31 tries */
3174
3038
    goto wait_for_position;
3175
3039
  }
3176
 
  drizzle_result_free(&res);
 
3040
  drizzle_free_result(res);
3177
3041
  return;
3178
3042
}
3179
3043
 
3203
3067
*/
3204
3068
static int do_save_master_pos(void)
3205
3069
{
3206
 
  drizzle_result_st res;
3207
 
  drizzle_return_t ret;
3208
 
  drizzle_row_t row;
3209
 
  drizzle_con_st *con= &cur_con->con;
 
3070
  DRIZZLE_RES *res;
 
3071
  DRIZZLE_ROW row;
 
3072
  DRIZZLE *drizzle= &cur_con->drizzle;
3210
3073
  const char *query;
3211
3074
 
3212
3075
 
3213
 
  if (drizzle_query_str(con, &res, query= "show master status", &ret) == NULL ||
3214
 
      ret != DRIZZLE_RETURN_OK)
3215
 
  {
3216
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
3217
 
    {
3218
 
      die("failed in '%s': %d: %s", query, drizzle_result_error_code(&res),
3219
 
           drizzle_result_error(&res));
3220
 
      drizzle_result_free(&res);
3221
 
    }
3222
 
    else
3223
 
      die("failed in '%s': %d: %s", query, ret, drizzle_con_error(con));
3224
 
  }
 
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));
3225
3079
 
3226
 
  if (drizzle_result_column_count(&res) == 0 ||
3227
 
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
3228
 
    die("drizzleclient_store_result() retuned NULL for '%s'", query);
3229
 
  if (!(row = drizzle_row_next(&res)))
 
3080
  if (!(res = drizzle_store_result(drizzle)))
 
3081
    die("drizzle_store_result() retuned NULL for '%s'", query);
 
3082
  if (!(row = drizzle_fetch_row(res)))
3230
3083
    die("empty result in show master status");
3231
 
  strncpy(master_pos.file, row[0], sizeof(master_pos.file)-1);
 
3084
  my_stpncpy(master_pos.file, row[0], sizeof(master_pos.file)-1);
3232
3085
  master_pos.pos = strtoul(row[1], (char**) 0, 10);
3233
 
  drizzle_result_free(&res);
 
3086
  drizzle_free_result(res);
3234
3087
  return(0);
3235
3088
}
3236
3089
 
3314
3167
 
3315
3168
static int do_sleep(struct st_command *command, bool real_sleep)
3316
3169
{
3317
 
  bool error= false;
 
3170
  int error= 0;
3318
3171
  char *p= command->first_argument;
3319
3172
  char *sleep_start, *sleep_end= command->end;
3320
 
  double sleep_val= 0;
 
3173
  double sleep_val;
3321
3174
 
3322
3175
  while (my_isspace(charset_info, *p))
3323
3176
    p++;
3324
3177
  if (!*p)
3325
3178
    die("Missing argument to %.*s", command->first_word_len, command->query);
3326
3179
  sleep_start= p;
3327
 
  /* Check that arg starts with a digit, not handled by internal::my_strtod */
 
3180
  /* Check that arg starts with a digit, not handled by my_strtod */
3328
3181
  if (!my_isdigit(charset_info, *sleep_start))
3329
3182
    die("Invalid argument to %.*s \"%s\"", command->first_word_len,
3330
3183
        command->query,command->first_argument);
3331
 
  string buff_str(sleep_start, sleep_end-sleep_start);
3332
 
  istringstream buff(buff_str);
3333
 
  error= (buff >> sleep_val).fail();
 
3184
  sleep_val= my_strtod(sleep_start, &sleep_end, &error);
3334
3185
  if (error)
3335
3186
    die("Invalid argument to %.*s \"%s\"", command->first_word_len,
3336
3187
        command->query, command->first_argument);
3340
3191
    sleep_val= opt_sleep;
3341
3192
 
3342
3193
  if (sleep_val)
3343
 
    usleep((uint32_t) (sleep_val * 1000000L));
 
3194
    my_sleep((uint32_t) (sleep_val * 1000000L));
3344
3195
  command->last_argument= sleep_end;
3345
3196
  return 0;
3346
3197
}
3347
3198
 
3348
3199
 
3349
 
static void do_get_file_name(struct st_command *command, string &dest)
 
3200
static void do_get_file_name(struct st_command *command,
 
3201
                             char* dest, uint dest_max_len)
3350
3202
{
3351
3203
  char *p= command->first_argument, *name;
3352
3204
  if (!*p)
3357
3209
  if (*p)
3358
3210
    *p++= 0;
3359
3211
  command->last_argument= p;
3360
 
  if (! opt_testdir.empty())
3361
 
  {
3362
 
    dest= opt_testdir;
3363
 
    if (dest[dest.length()] != '/')
3364
 
      dest.append("/");
3365
 
  }
3366
 
  dest.append(name);
 
3212
  strncpy(dest, name, dest_max_len - 1);
3367
3213
}
3368
3214
 
3369
3215
 
3381
3227
  if(*p)
3382
3228
    *p++= 0;
3383
3229
  command->last_argument= p;
3384
 
  charset_info= get_charset_by_csname(charset_name, MY_CS_PRIMARY);
 
3230
  charset_info= get_charset_by_csname(charset_name,MY_CS_PRIMARY,MYF(MY_WME));
3385
3231
  if (!charset_info)
3386
3232
    abort_not_supported_test("Test requires charset '%s'", charset_name);
3387
3233
}
3388
3234
 
3389
 
static void fill_global_error_names()
 
3235
static uint get_errcode_from_name(char *error_name, char *error_end)
3390
3236
{
3391
 
  drizzle_result_st res;
3392
 
  drizzle_return_t ret;
3393
 
  drizzle_row_t row;
3394
 
  drizzle_con_st *con= &cur_con->con;
3395
 
 
3396
 
  global_error_names.clear();
3397
 
 
3398
 
  const std::string ds_query("select error_name, error_code "
3399
 
                             "from data_dictionary.errors");
3400
 
  if (drizzle_query_str(con, &res, ds_query.c_str(), &ret) == NULL ||
3401
 
      ret != DRIZZLE_RETURN_OK)
3402
 
  {
3403
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
3404
 
    {
3405
 
      die("Error running query '%s': %d %s", ds_query.c_str(),
3406
 
          drizzle_result_error_code(&res), drizzle_result_error(&res));
3407
 
      drizzle_result_free(&res);
3408
 
    }
3409
 
    else
3410
 
    {
3411
 
      die("Error running query '%s': %d %s", ds_query.c_str(), ret,
3412
 
          drizzle_con_error(con));
3413
 
    }
3414
 
  }
3415
 
  if (drizzle_result_column_count(&res) == 0 ||
3416
 
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
3417
 
  {
3418
 
    drizzle_result_free(&res);
3419
 
    die("Query '%s' didn't return a result set", ds_query.c_str());
3420
 
  }
3421
 
 
3422
 
  while ((row= drizzle_row_next(&res)) && row[0])
 
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++)
3423
3242
  {
3424
3243
    /*
3425
 
      Concatenate all fields in the first row with tab in between
3426
 
      and assign that string to the $variable
 
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).
3427
3247
    */
3428
 
    size_t *lengths= drizzle_row_field_sizes(&res);
3429
 
    const std::string error_name(row[0], lengths[0]);
3430
 
    const std::string error_code(row[1], lengths[1]);
3431
 
 
3432
 
    try
3433
 
    {
3434
 
      global_error_names.insert(ErrorCodes::value_type(error_name,
3435
 
                                                       boost::lexical_cast<uint32_t>(error_code)));
3436
 
    }
3437
 
    catch (boost::bad_lexical_cast &ex)
3438
 
    {
3439
 
      drizzle_result_free(&res);
3440
 
      die("Invalid error_code from Drizzle: %s", ex.what());
3441
 
    }
3442
 
 
3443
 
  }
3444
 
 
3445
 
  drizzle_result_free(&res);
3446
 
}
3447
 
 
3448
 
static uint32_t get_errcode_from_name(char *error_name, char *error_end)
3449
 
{
3450
 
  size_t err_name_len= error_end - error_name;
3451
 
  string error_name_s(error_name, err_name_len);
3452
 
 
3453
 
  ErrorCodes::iterator it= global_error_names.find(error_name_s);
3454
 
  if (it != global_error_names.end())
3455
 
  {
3456
 
    return (*it).second;
3457
 
  }
3458
 
 
3459
 
  die("Unknown SQL error name '%s'", error_name_s.c_str());
3460
 
  return 0;
 
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);
3461
3257
}
3462
3258
 
3463
3259
static void do_get_errcodes(struct st_command *command)
3464
3260
{
3465
3261
  struct st_match_err *to= saved_expected_errors.err;
3466
3262
  char *p= command->first_argument;
3467
 
  uint32_t count= 0;
 
3263
  uint count= 0;
3468
3264
 
3469
3265
 
3470
3266
 
3490
3286
 
3491
3287
      /*
3492
3288
        SQLSTATE string
3493
 
        - Must be DRIZZLE_MAX_SQLSTATE_SIZE long
 
3289
        - Must be SQLSTATE_LENGTH long
3494
3290
        - May contain only digits[0-9] and _uppercase_ letters
3495
3291
      */
3496
3292
      p++; /* Step past the S */
3497
 
      if ((end - p) != DRIZZLE_MAX_SQLSTATE_SIZE)
3498
 
        die("The sqlstate must be exactly %d chars long", DRIZZLE_MAX_SQLSTATE_SIZE);
 
3293
      if ((end - p) != SQLSTATE_LENGTH)
 
3294
        die("The sqlstate must be exactly %d chars long", SQLSTATE_LENGTH);
3499
3295
 
3500
3296
      /* Check sqlstate string validity */
3501
3297
      while (*p && p < end)
3540
3336
      }
3541
3337
 
3542
3338
      /* Convert the sting to int */
3543
 
      istringstream buff(start);
3544
 
      if ((buff >> val).fail())
 
3339
      if (!str2int(start, 10, (long) INT_MIN, (long) INT_MAX, &val))
3545
3340
        die("Invalid argument to error: '%s'", command->first_argument);
3546
3341
 
3547
 
      to->code.errnum= (uint32_t) val;
 
3342
      to->code.errnum= (uint) val;
3548
3343
      to->type= ERR_ERRNO;
3549
3344
    }
3550
3345
    to++;
3651
3446
}
3652
3447
 
3653
3448
 
3654
 
static void set_reconnect(drizzle_con_st *con, int val)
 
3449
static void set_reconnect(DRIZZLE *drizzle, int val)
3655
3450
{
3656
 
  (void) con;
3657
 
  (void) val;
3658
 
/* XXX
3659
3451
  bool reconnect= val;
3660
3452
 
3661
 
  drizzleclient_options(drizzle, DRIZZLE_OPT_RECONNECT, (char *)&reconnect);
3662
 
*/
 
3453
  drizzle_options(drizzle, DRIZZLE_OPT_RECONNECT, (char *)&reconnect);
 
3454
 
 
3455
  return;
3663
3456
}
3664
3457
 
3665
3458
 
3668
3461
  if (!(cur_con= find_connection_by_name(name)))
3669
3462
    die("connection '%s' not found in connection pool", name);
3670
3463
 
3671
 
  /* Update $drizzleclient_get_server_version to that of current connection */
3672
 
  var_set_drizzleclient_get_server_version(&cur_con->con);
 
3464
  /* Update $drizzle_get_server_version to that of current connection */
 
3465
  var_set_drizzle_get_server_version(&cur_con->drizzle);
3673
3466
 
3674
3467
  return(0);
3675
3468
}
3711
3504
  if (!(con= find_connection_by_name(name)))
3712
3505
    die("connection '%s' not found in connection pool", name);
3713
3506
 
3714
 
  if (con->drizzle != NULL)
 
3507
  if (command->type == Q_DIRTY_CLOSE)
3715
3508
  {
3716
 
    drizzle_free(con->drizzle);
3717
 
    con->drizzle= NULL;
 
3509
    if (con->drizzle.net.vio)
 
3510
    {
 
3511
      net_close(&(con->drizzle.net));
 
3512
    }
3718
3513
  }
 
3514
 
 
3515
  drizzle_close(&con->drizzle);
 
3516
 
 
3517
  if (con->util_drizzle)
 
3518
    drizzle_close(con->util_drizzle);
 
3519
  con->util_drizzle= 0;
 
3520
 
3719
3521
  free(con->name);
3720
3522
 
3721
3523
  /*
3722
3524
    When the connection is closed set name to "-closed_connection-"
3723
3525
    to make it possible to reuse the connection name.
3724
3526
  */
3725
 
  if (!(con->name = strdup("-closed_connection-")))
 
3527
  if (!(con->name = my_strdup("-closed_connection-", MYF(MY_WME))))
3726
3528
    die("Out of memory");
3727
3529
 
3728
3530
  return;
3754
3556
 
3755
3557
*/
3756
3558
 
3757
 
static void safe_connect(drizzle_con_st *con, const char *name,
3758
 
                         const string host, const string user, const char *pass,
3759
 
                         const string db, uint32_t port)
 
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)
3760
3562
{
3761
 
  uint32_t failed_attempts= 0;
 
3563
  int failed_attempts= 0;
3762
3564
  static uint32_t connection_retry_sleep= 100000; /* Microseconds */
3763
 
  drizzle_return_t ret;
3764
 
 
3765
 
  drizzle_con_set_tcp(con, host.c_str(), port);
3766
 
  drizzle_con_set_auth(con, user.c_str(), pass);
3767
 
  drizzle_con_set_db(con, db.c_str());
3768
 
  while((ret= drizzle_con_connect(con)) != DRIZZLE_RETURN_OK)
 
3565
 
 
3566
 
 
3567
  while(!drizzle_connect(drizzle, host, user, pass, db, port, NULL,
 
3568
                         CLIENT_MULTI_STATEMENTS | CLIENT_REMEMBER_OPTIONS))
3769
3569
  {
3770
3570
    /*
3771
3571
      Connect failed
3775
3575
      on protocol/connection type
3776
3576
    */
3777
3577
 
3778
 
    if ((ret == DRIZZLE_RETURN_GETADDRINFO ||
3779
 
         ret == DRIZZLE_RETURN_COULD_NOT_CONNECT) &&
 
3578
    if ((drizzle_errno(drizzle) == CR_CONN_HOST_ERROR ||
 
3579
         drizzle_errno(drizzle) == CR_CONNECTION_ERROR) &&
3780
3580
        failed_attempts < opt_max_connect_retries)
3781
3581
    {
3782
3582
      verbose_msg("Connect attempt %d/%d failed: %d: %s", failed_attempts,
3783
 
                  opt_max_connect_retries, ret, drizzle_con_error(con));
3784
 
      usleep(connection_retry_sleep);
 
3583
                  opt_max_connect_retries, drizzle_errno(drizzle),
 
3584
                  drizzle_error(drizzle));
 
3585
      my_sleep(connection_retry_sleep);
3785
3586
    }
3786
3587
    else
3787
3588
    {
3788
3589
      if (failed_attempts > 0)
3789
3590
        die("Could not open connection '%s' after %d attempts: %d %s", name,
3790
 
            failed_attempts, ret, drizzle_con_error(con));
 
3591
            failed_attempts, drizzle_errno(drizzle), drizzle_error(drizzle));
3791
3592
      else
3792
 
        die("Could not open connection '%s': %d %s", name, ret,
3793
 
            drizzle_con_error(con));
 
3593
        die("Could not open connection '%s': %d %s", name,
 
3594
            drizzle_errno(drizzle), drizzle_error(drizzle));
3794
3595
    }
3795
3596
    failed_attempts++;
3796
3597
  }
3822
3623
*/
3823
3624
 
3824
3625
static int connect_n_handle_errors(struct st_command *command,
3825
 
                                   drizzle_con_st *con, const char* host,
 
3626
                                   DRIZZLE *con, const char* host,
3826
3627
                                   const char* user, const char* pass,
3827
3628
                                   const char* db, int port, const char* sock)
3828
3629
{
3829
 
  drizzle_return_t ret;
3830
3630
 
3831
3631
  /* Only log if an error is expected */
3832
3632
  if (!command->abort_on_error &&
3853
3653
    ds_res.append(delimiter);
3854
3654
    ds_res.append("\n");
3855
3655
  }
3856
 
  drizzle_con_set_tcp(con, host, port);
3857
 
  drizzle_con_set_auth(con, user, pass);
3858
 
  drizzle_con_set_db(con, db);
3859
 
  if ((ret= drizzle_con_connect(con)) != DRIZZLE_RETURN_OK)
 
3656
  if (!drizzle_connect(con, host, user, pass, db, port, 0,
 
3657
                       CLIENT_MULTI_STATEMENTS))
3860
3658
  {
3861
 
    if (ret == DRIZZLE_RETURN_HANDSHAKE_FAILED)
3862
 
    {
3863
 
      var_set_errno(drizzle_con_error_code(con));
3864
 
      handle_error(command, drizzle_con_error_code(con), drizzle_con_error(con),
3865
 
                   drizzle_con_sqlstate(con), &ds_res);
3866
 
    }
3867
 
    else
3868
 
    {
3869
 
      var_set_errno(ret);
3870
 
      handle_error(command, ret, drizzle_con_error(con), "", &ds_res);
3871
 
    }
3872
 
 
 
3659
    var_set_errno(drizzle_errno(con));
 
3660
    handle_error(command, drizzle_errno(con), drizzle_error(con),
 
3661
                 drizzle_sqlstate(con), &ds_res);
3873
3662
    return 0; /* Not connected */
3874
3663
  }
3875
3664
 
3906
3695
 
3907
3696
static void do_connect(struct st_command *command)
3908
3697
{
3909
 
  uint32_t con_port= opt_port;
 
3698
  int con_port= opt_port;
3910
3699
  const char *con_options;
3911
 
  bool con_ssl= 0;
 
3700
  bool con_ssl= 0, con_compress= 0;
3912
3701
  struct st_connection* con_slot;
3913
3702
 
3914
3703
  string ds_connection_name;
3954
3743
    if (*ds_sock.c_str() != FN_LIBCHAR)
3955
3744
    {
3956
3745
      char buff[FN_REFLEN];
3957
 
      internal::fn_format(buff, ds_sock.c_str(), TMPDIR, "", 0);
 
3746
      fn_format(buff, ds_sock.c_str(), TMPDIR, "", 0);
3958
3747
      ds_sock.clear();
3959
3748
      ds_sock.append(buff);
3960
3749
    }
3974
3763
      end++;
3975
3764
    if (!strncmp(con_options, "SSL", 3))
3976
3765
      con_ssl= 1;
 
3766
    else if (!strncmp(con_options, "COMPRESS", 8))
 
3767
      con_compress= 1;
3977
3768
    else
3978
3769
      die("Illegal option to connect: %.*s",
3979
3770
          (int) (end - con_options), con_options);
3995
3786
          (int) (sizeof(connections)/sizeof(struct st_connection)));
3996
3787
  }
3997
3788
 
3998
 
  if ((con_slot->drizzle= drizzle_create(NULL)) == NULL)
 
3789
  if (!drizzle_create(&con_slot->drizzle))
3999
3790
    die("Failed on drizzle_create()");
4000
 
  if (!drizzle_con_create(con_slot->drizzle, &con_slot->con))
4001
 
    die("Failed on drizzle_con_create()");
4002
 
  drizzle_con_add_options(&con_slot->con, use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL);
 
3791
  if (opt_compress || con_compress)
 
3792
    drizzle_options(&con_slot->drizzle, DRIZZLE_OPT_COMPRESS, NULL);
 
3793
  drizzle_options(&con_slot->drizzle, DRIZZLE_OPT_LOCAL_INFILE, 0);
4003
3794
 
4004
3795
  /* Use default db name */
4005
3796
  if (ds_database.length() == 0)
4009
3800
  if (ds_database.length() && !strcmp(ds_database.c_str(),"*NO-ONE*"))
4010
3801
    ds_database.clear();
4011
3802
 
4012
 
  if (connect_n_handle_errors(command, &con_slot->con,
 
3803
  if (connect_n_handle_errors(command, &con_slot->drizzle,
4013
3804
                              ds_host.c_str(),ds_user.c_str(),
4014
3805
                              ds_password.c_str(), ds_database.c_str(),
4015
3806
                              con_port, ds_sock.c_str()))
4022
3813
      next_con++; /* if we used the next_con slot, advance the pointer */
4023
3814
  }
4024
3815
 
4025
 
  /* Update $drizzleclient_get_server_version to that of current connection */
4026
 
  var_set_drizzleclient_get_server_version(&cur_con->con);
 
3816
  /* Update $drizzle_get_server_version to that of current connection */
 
3817
  var_set_drizzle_get_server_version(&cur_con->drizzle);
4027
3818
 
4028
3819
  return;
4029
3820
}
4140
3931
  if (not_expr)
4141
3932
    cur_block->ok = !cur_block->ok;
4142
3933
 
4143
 
  free(v.str_val);
4144
 
  free(v.env_s);
4145
 
 
 
3934
  var_free(&v);
4146
3935
  return;
4147
3936
}
4148
3937
 
4165
3954
}
4166
3955
 
4167
3956
 
4168
 
bool match_delimiter(int c, const char *delim, uint32_t length)
 
3957
bool match_delimiter(int c, const char *delim, uint length)
4169
3958
{
4170
 
  uint32_t i;
 
3959
  uint i;
4171
3960
  char tmp[MAX_DELIMITER_LENGTH];
4172
3961
 
4173
3962
  if (c != *delim)
4219
4008
 
4220
4009
*/
4221
4010
 
4222
 
 
4223
 
static int my_strnncoll_simple(const CHARSET_INFO * const  cs, const unsigned char *s, size_t slen,
4224
 
                               const unsigned char *t, size_t tlen,
4225
 
                               bool t_is_prefix)
4226
 
{
4227
 
  size_t len = ( slen > tlen ) ? tlen : slen;
4228
 
  unsigned char *map= cs->sort_order;
4229
 
  if (t_is_prefix && slen > tlen)
4230
 
    slen=tlen;
4231
 
  while (len--)
4232
 
  {
4233
 
    if (map[*s++] != map[*t++])
4234
 
      return ((int) map[s[-1]] - (int) map[t[-1]]);
4235
 
  }
4236
 
  /*
4237
 
    We can't use (slen - tlen) here as the result may be outside of the
4238
 
    precision of a signed int
4239
 
  */
4240
 
  return slen > tlen ? 1 : slen < tlen ? -1 : 0 ;
4241
 
}
4242
 
 
4243
4011
static int read_line(char *buf, int size)
4244
4012
{
4245
4013
  char c, last_quote= 0;
4259
4027
  found_eof:
4260
4028
      if (cur_file->file != stdin)
4261
4029
      {
4262
 
        fclose(cur_file->file);
 
4030
        my_fclose(cur_file->file, MYF(0));
4263
4031
        cur_file->file= 0;
4264
4032
      }
4265
4033
      free((unsigned char*) cur_file->file_name);
4299
4067
      }
4300
4068
      else if ((c == '{' &&
4301
4069
                (!my_strnncoll_simple(charset_info, (const unsigned char*) "while", 5,
4302
 
                                      (unsigned char*) buf, min((ptrdiff_t)5, p - buf), 0) ||
 
4070
                                      (unsigned char*) buf, cmin((long)5, p - buf), 0) ||
4303
4071
                 !my_strnncoll_simple(charset_info, (const unsigned char*) "if", 2,
4304
 
                                      (unsigned char*) buf, min((ptrdiff_t)2, p - buf), 0))))
 
4072
                                      (unsigned char*) buf, cmin((long)2, p - buf), 0))))
4305
4073
      {
4306
4074
        /* Only if and while commands can be terminated by { */
4307
4075
        *p++= c;
4378
4146
    {
4379
4147
      /* Could be a multibyte character */
4380
4148
      /* This code is based on the code in "sql_load.cc" */
 
4149
#ifdef USE_MB
4381
4150
      int charlen = my_mbcharlen(charset_info, c);
4382
4151
      /* We give up if multibyte character is started but not */
4383
4152
      /* completed before we pass buf_end */
4404
4173
        }
4405
4174
      }
4406
4175
      else
 
4176
#endif
4407
4177
        *p++= c;
4408
4178
    }
4409
4179
  }
4486
4256
        ptr[2] && ptr[2] == '-' &&
4487
4257
        ptr[3])
4488
4258
    {
4489
 
      uint32_t type;
 
4259
      uint type;
4490
4260
      char save;
4491
4261
      char *end, *start= (char*)ptr+3;
4492
4262
      /* Skip leading spaces */
4563
4333
  return;
4564
4334
}
4565
4335
 
 
4336
 
 
4337
 
4566
4338
/*
4567
4339
  Create a command from a set of lines
4568
4340
 
4580
4352
  terminated by new line '\n' regardless how many "delimiter" it contain.
4581
4353
*/
4582
4354
 
4583
 
#define MAX_QUERY (768*1024*2) /* 256K -- a test in sp-big is >128K */
 
4355
#define MAX_QUERY (256*1024*2) /* 256K -- a test in sp-big is >128K */
4584
4356
static char read_command_buf[MAX_QUERY];
4585
4357
 
4586
4358
static int read_command(struct st_command** command_ptr)
4594
4366
    *command_ptr= q_lines[parser.current_line];
4595
4367
    return(0);
4596
4368
  }
4597
 
  if (!(*command_ptr= command= new st_command))
4598
 
    die("command construction failed");
 
4369
  if (!(*command_ptr= command=
 
4370
        (struct st_command*) my_malloc(sizeof(*command),
 
4371
                                       MYF(MY_WME|MY_ZEROFILL))))
 
4372
    die(NULL);
4599
4373
  q_lines.push_back(command);
4600
4374
  command->type= Q_UNKNOWN;
4601
4375
 
4622
4396
  while (*p && my_isspace(charset_info, *p))
4623
4397
    p++;
4624
4398
 
4625
 
  if (!(command->query_buf= command->query= strdup(p)))
 
4399
  if (!(command->query_buf= command->query= my_strdup(p, MYF(MY_WME))))
4626
4400
    die("Out of memory");
4627
4401
 
4628
4402
  /* Calculate first word length(the command), terminated by space or ( */
4629
4403
  p= command->query;
4630
4404
  while (*p && !my_isspace(charset_info, *p) && *p != '(')
4631
4405
    p++;
4632
 
  command->first_word_len= (uint32_t) (p - command->query);
 
4406
  command->first_word_len= (uint) (p - command->query);
4633
4407
 
4634
4408
  /* Skip spaces between command and first argument */
4635
4409
  while (*p && my_isspace(charset_info, *p))
4642
4416
  return(0);
4643
4417
}
4644
4418
 
 
4419
 
 
4420
static struct my_option my_long_options[] =
 
4421
{
 
4422
  {"help", '?', "Display this help and exit.", 0, 0, 0, GET_NO_ARG, NO_ARG,
 
4423
   0, 0, 0, 0, 0, 0},
 
4424
  {"basedir", 'b', "Basedir for tests.", (char**) &opt_basedir,
 
4425
   (char**) &opt_basedir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4426
  {"character-sets-dir", OPT_CHARSETS_DIR,
 
4427
   "Directory where character sets are.", (char**) &opt_charsets_dir,
 
4428
   (char**) &opt_charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4429
  {"compress", 'C', "Use the compressed server/client protocol.",
 
4430
   (char**) &opt_compress, (char**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
 
4431
   0, 0, 0},
 
4432
  {"database", 'D', "Database to use.", (char**) &opt_db, (char**) &opt_db, 0,
 
4433
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4434
  {"debug-check", OPT_DEBUG_CHECK, "Check memory and open file usage at exit.",
 
4435
   (char**) &debug_check_flag, (char**) &debug_check_flag, 0,
 
4436
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4437
  {"debug-info", OPT_DEBUG_INFO, "Print some debug info at exit.",
 
4438
   (char**) &debug_info_flag, (char**) &debug_info_flag,
 
4439
   0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4440
  {"host", 'h', "Connect to host.", (char**) &opt_host, (char**) &opt_host, 0,
 
4441
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4442
  {"include", 'i', "Include SQL before each test case.", (char**) &opt_include,
 
4443
   (char**) &opt_include, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4444
  {"logdir", OPT_LOG_DIR, "Directory for log files", (char**) &opt_logdir,
 
4445
   (char**) &opt_logdir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4446
  {"mark-progress", OPT_MARK_PROGRESS,
 
4447
   "Write linenumber and elapsed time to <testname>.progress ",
 
4448
   (char**) &opt_mark_progress, (char**) &opt_mark_progress, 0,
 
4449
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4450
  {"max-connect-retries", OPT_MAX_CONNECT_RETRIES,
 
4451
   "Max number of connection attempts when connecting to server",
 
4452
   (char**) &opt_max_connect_retries, (char**) &opt_max_connect_retries, 0,
 
4453
   GET_INT, REQUIRED_ARG, 500, 1, 10000, 0, 0, 0},
 
4454
  {"password", 'p', "Password to use when connecting to server.",
 
4455
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
4456
  {"port", 'P', "Port number to use for connection or 0 for default to, in "
 
4457
   "order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, "
 
4458
   "built-in default (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
 
4459
   (char**) &opt_port,
 
4460
   (char**) &opt_port, 0, GET_INT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4461
  {"quiet", 's', "Suppress all normal output.", (char**) &silent,
 
4462
   (char**) &silent, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4463
  {"record", 'r', "Record output of test_file into result file.",
 
4464
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4465
  {"result-file", 'R', "Read/Store result from/in this file.",
 
4466
   (char**) &result_file_name, (char**) &result_file_name, 0,
 
4467
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4468
  {"server-arg", 'A', "Send option value to embedded server as a parameter.",
 
4469
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4470
  {"server-file", 'F', "Read embedded server arguments from file.",
 
4471
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4472
  {"silent", 's', "Suppress all normal output. Synonym for --quiet.",
 
4473
   (char**) &silent, (char**) &silent, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4474
  {"sleep", 'T', "Sleep always this many seconds on sleep commands.",
 
4475
   (char**) &opt_sleep, (char**) &opt_sleep, 0, GET_INT, REQUIRED_ARG, -1, -1, 0,
 
4476
   0, 0, 0},
 
4477
  {"tail-lines", OPT_TAIL_LINES,
 
4478
   "Number of lines of the resul to include in a failure report",
 
4479
   (char**) &opt_tail_lines, (char**) &opt_tail_lines, 0,
 
4480
   GET_INT, REQUIRED_ARG, 0, 0, 10000, 0, 0, 0},
 
4481
  {"test-file", 'x', "Read test from/in this file (default stdin).",
 
4482
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4483
  {"timer-file", 'm', "File where the timing in micro seconds is stored.",
 
4484
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4485
  {"tmpdir", 't', "Temporary directory where sockets are put.",
 
4486
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4487
  {"user", 'u', "User for login.", (char**) &opt_user, (char**) &opt_user, 0,
 
4488
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4489
  {"verbose", 'v', "Write more.", (char**) &verbose, (char**) &verbose, 0,
 
4490
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4491
  {"version", 'V', "Output version information and exit.",
 
4492
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4493
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
 
4494
};
 
4495
 
 
4496
 
 
4497
static void print_version(void)
 
4498
{
 
4499
  printf("%s  Ver %s Distrib %s, for %s (%s)\n",my_progname,MTEST_VERSION,
 
4500
         drizzle_get_client_info(),SYSTEM_TYPE,MACHINE_TYPE);
 
4501
}
 
4502
 
 
4503
static void usage(void)
 
4504
{
 
4505
  print_version();
 
4506
  printf("MySQL AB, by Sasha, Matt, Monty & Jani\n");
 
4507
  printf("This software comes with ABSOLUTELY NO WARRANTY\n\n");
 
4508
  printf("Runs a test against the DRIZZLE server and compares output with a results file.\n\n");
 
4509
  printf("Usage: %s [OPTIONS] [database] < test_file\n", my_progname);
 
4510
  my_print_help(my_long_options);
 
4511
  printf("  --no-defaults       Don't read default options from any options file.\n");
 
4512
  my_print_variables(my_long_options);
 
4513
}
 
4514
 
 
4515
/*
 
4516
  Read arguments for embedded server and put them into
 
4517
  embedded_server_args[]
 
4518
*/
 
4519
 
 
4520
static void read_embedded_server_arguments(const char *name)
 
4521
{
 
4522
  char argument[1024],buff[FN_REFLEN], *str=0;
 
4523
  FILE *file;
 
4524
 
 
4525
  if (!test_if_hard_path(name))
 
4526
  {
 
4527
    strxmov(buff, opt_basedir, name, NULL);
 
4528
    name=buff;
 
4529
  }
 
4530
  fn_format(buff, name, "", "", MY_UNPACK_FILENAME);
 
4531
 
 
4532
  if (!embedded_server_arg_count)
 
4533
  {
 
4534
    embedded_server_arg_count=1;
 
4535
    embedded_server_args[0]= (char*) "";    /* Progname */
 
4536
  }
 
4537
  if (!(file=my_fopen(buff, O_RDONLY, MYF(MY_WME))))
 
4538
    die("Failed to open file '%s'", buff);
 
4539
 
 
4540
  while (embedded_server_arg_count < MAX_EMBEDDED_SERVER_ARGS &&
 
4541
         (str=fgets(argument,sizeof(argument), file)))
 
4542
  {
 
4543
    *(strchr(str, '\0')-1)=0;        /* Remove end newline */
 
4544
    if (!(embedded_server_args[embedded_server_arg_count]=
 
4545
          (char*) my_strdup(str,MYF(MY_WME))))
 
4546
    {
 
4547
      my_fclose(file,MYF(0));
 
4548
      die("Out of memory");
 
4549
 
 
4550
    }
 
4551
    embedded_server_arg_count++;
 
4552
  }
 
4553
  my_fclose(file,MYF(0));
 
4554
  if (str)
 
4555
    die("Too many arguments in option file: %s",name);
 
4556
 
 
4557
  return;
 
4558
}
 
4559
 
 
4560
 
 
4561
extern "C" bool
 
4562
get_one_option(int optid, const struct my_option *, char *argument)
 
4563
{
 
4564
  switch(optid) {
 
4565
  case 'r':
 
4566
    record = 1;
 
4567
    break;
 
4568
  case 'x':
 
4569
  {
 
4570
    char buff[FN_REFLEN];
 
4571
    if (!test_if_hard_path(argument))
 
4572
    {
 
4573
      strxmov(buff, opt_basedir, argument, NULL);
 
4574
      argument= buff;
 
4575
    }
 
4576
    fn_format(buff, argument, "", "", MY_UNPACK_FILENAME);
 
4577
    assert(cur_file == file_stack && cur_file->file == 0);
 
4578
    if (!(cur_file->file=
 
4579
          my_fopen(buff, O_RDONLY, MYF(0))))
 
4580
      die("Could not open '%s' for reading: errno = %d", buff, errno);
 
4581
    cur_file->file_name= my_strdup(buff, MYF(MY_FAE));
 
4582
    cur_file->lineno= 1;
 
4583
    break;
 
4584
  }
 
4585
  case 'm':
 
4586
  {
 
4587
    static char buff[FN_REFLEN];
 
4588
    if (!test_if_hard_path(argument))
 
4589
    {
 
4590
      strxmov(buff, opt_basedir, argument, NULL);
 
4591
      argument= buff;
 
4592
    }
 
4593
    fn_format(buff, argument, "", "", MY_UNPACK_FILENAME);
 
4594
    timer_file= buff;
 
4595
    unlink(timer_file);       /* Ignore error, may not exist */
 
4596
    break;
 
4597
  }
 
4598
  case 'p':
 
4599
    if (argument)
 
4600
    {
 
4601
      free(opt_pass);
 
4602
      opt_pass= my_strdup(argument, MYF(MY_FAE));
 
4603
      while (*argument) *argument++= 'x';    /* Destroy argument */
 
4604
      tty_password= 0;
 
4605
    }
 
4606
    else
 
4607
      tty_password= 1;
 
4608
    break;
 
4609
  case 't':
 
4610
    my_stpncpy(TMPDIR, argument, sizeof(TMPDIR));
 
4611
    break;
 
4612
  case 'A':
 
4613
    if (!embedded_server_arg_count)
 
4614
    {
 
4615
      embedded_server_arg_count=1;
 
4616
      embedded_server_args[0]= (char*) "";
 
4617
    }
 
4618
    if (embedded_server_arg_count == MAX_EMBEDDED_SERVER_ARGS-1 ||
 
4619
        !(embedded_server_args[embedded_server_arg_count++]=
 
4620
          my_strdup(argument, MYF(MY_FAE))))
 
4621
    {
 
4622
      die("Can't use server argument");
 
4623
    }
 
4624
    break;
 
4625
  case 'F':
 
4626
    read_embedded_server_arguments(argument);
 
4627
    break;
 
4628
  case 'V':
 
4629
    print_version();
 
4630
    exit(0);
 
4631
  case '?':
 
4632
    usage();
 
4633
    exit(0);
 
4634
  }
 
4635
  return 0;
 
4636
}
 
4637
 
 
4638
 
 
4639
static int parse_args(int argc, char **argv)
 
4640
{
 
4641
  load_defaults("drizzle",load_default_groups,&argc,&argv);
 
4642
  default_argv= argv;
 
4643
 
 
4644
  if ((handle_options(&argc, &argv, my_long_options, get_one_option)))
 
4645
    exit(1);
 
4646
 
 
4647
  if (argc > 1)
 
4648
  {
 
4649
    usage();
 
4650
    exit(1);
 
4651
  }
 
4652
  if (argc == 1)
 
4653
    opt_db= *argv;
 
4654
  if (tty_password)
 
4655
    opt_pass= get_tty_password(NULL);          /* purify tested */
 
4656
  if (debug_info_flag)
 
4657
    my_end_arg= MY_CHECK_ERROR | MY_GIVE_INFO;
 
4658
  if (debug_check_flag)
 
4659
    my_end_arg= MY_CHECK_ERROR;
 
4660
 
 
4661
  return 0;
 
4662
}
 
4663
 
4645
4664
/*
4646
4665
  Write the content of str into file
4647
4666
 
4658
4677
  int fd;
4659
4678
  char buff[FN_REFLEN];
4660
4679
  int flags= O_WRONLY | O_CREAT;
4661
 
  if (!internal::test_if_hard_path(fname))
 
4680
  if (!test_if_hard_path(fname))
4662
4681
  {
4663
 
    snprintf(buff, sizeof(buff), "%s%s",opt_basedir.c_str(),fname);
 
4682
    strxmov(buff, opt_basedir, fname, NULL);
4664
4683
    fname= buff;
4665
4684
  }
4666
 
  internal::fn_format(buff, fname, "", "", MY_UNPACK_FILENAME);
 
4685
  fn_format(buff, fname, "", "", MY_UNPACK_FILENAME);
4667
4686
 
4668
4687
  if (!append)
4669
4688
    flags|= O_TRUNC;
4670
 
  if ((fd= internal::my_open(buff, flags,
 
4689
  if ((fd= my_open(buff, flags,
4671
4690
                   MYF(MY_WME | MY_FFNF))) < 0)
4672
4691
    die("Could not open '%s' for writing: errno = %d", buff, errno);
4673
 
  if (append && lseek(fd, 0, SEEK_END) == MY_FILEPOS_ERROR)
 
4692
  if (append && my_seek(fd, 0, SEEK_END, MYF(0)) == MY_FILEPOS_ERROR)
4674
4693
    die("Could not find end of file '%s': errno = %d", buff, errno);
4675
 
  if (internal::my_write(fd, (unsigned char*)str, size, MYF(MY_WME|MY_FNABP)))
 
4694
  if (my_write(fd, (unsigned char*)str, size, MYF(MY_WME|MY_FNABP)))
4676
4695
    die("write failed");
4677
 
  internal::my_close(fd, MYF(0));
 
4696
  my_close(fd, MYF(0));
4678
4697
}
4679
4698
 
4680
4699
/*
4696
4715
void dump_result_to_log_file(const char *buf, int size)
4697
4716
{
4698
4717
  char log_file[FN_REFLEN];
4699
 
  str_to_file(internal::fn_format(log_file, result_file_name.c_str(), opt_logdir.c_str(), ".log",
4700
 
                        ! opt_logdir.empty() ? MY_REPLACE_DIR | MY_REPLACE_EXT :
 
4718
  str_to_file(fn_format(log_file, result_file_name, opt_logdir, ".log",
 
4719
                        *opt_logdir ? MY_REPLACE_DIR | MY_REPLACE_EXT :
4701
4720
                        MY_REPLACE_EXT),
4702
4721
              buf, size);
4703
4722
  fprintf(stderr, "\nMore results from queries before failure can be found in %s\n",
4707
4726
void dump_progress(void)
4708
4727
{
4709
4728
  char progress_file[FN_REFLEN];
4710
 
  str_to_file(internal::fn_format(progress_file, result_file_name.c_str(),
4711
 
                        opt_logdir.c_str(), ".progress",
4712
 
                        ! opt_logdir.empty() ? MY_REPLACE_DIR | MY_REPLACE_EXT :
 
4729
  str_to_file(fn_format(progress_file, result_file_name,
 
4730
                        opt_logdir, ".progress",
 
4731
                        *opt_logdir ? MY_REPLACE_DIR | MY_REPLACE_EXT :
4713
4732
                        MY_REPLACE_EXT),
4714
4733
              ds_progress.c_str(), ds_progress.length());
4715
4734
}
4718
4737
{
4719
4738
  char warn_file[FN_REFLEN];
4720
4739
 
4721
 
  str_to_file(internal::fn_format(warn_file, result_file_name.c_str(), opt_logdir.c_str(), ".warnings",
4722
 
                        ! opt_logdir.empty() ? MY_REPLACE_DIR | MY_REPLACE_EXT :
 
4740
  str_to_file(fn_format(warn_file, result_file_name, opt_logdir, ".warnings",
 
4741
                        *opt_logdir ? MY_REPLACE_DIR | MY_REPLACE_EXT :
4723
4742
                        MY_REPLACE_EXT),
4724
4743
              ds_warning_messages.c_str(), ds_warning_messages.length());
4725
4744
}
4729
4748
  Append the result for one field to the dynamic string ds
4730
4749
*/
4731
4750
 
4732
 
static void append_field(string *ds, uint32_t col_idx, drizzle_column_st *column,
 
4751
static void append_field(string *ds, uint col_idx, const DRIZZLE_FIELD* field,
4733
4752
                         const char* val, uint64_t len, bool is_null)
4734
4753
{
4735
4754
  if (col_idx < max_replace_column && replace_column[col_idx])
4751
4770
  }
4752
4771
  else
4753
4772
  {
4754
 
    ds->append(drizzle_column_name(column));
 
4773
    ds->append(field->name);
4755
4774
    ds->append("\t");
4756
4775
    replace_append_mem(ds, val, (int)len);
4757
4776
    ds->append("\n");
4764
4783
  Values may be converted with 'replace_column'
4765
4784
*/
4766
4785
 
4767
 
static void append_result(string *ds, drizzle_result_st *res)
 
4786
static void append_result(string *ds, DRIZZLE_RES *res)
4768
4787
{
4769
 
  drizzle_row_t row;
4770
 
  uint32_t num_fields= drizzle_result_column_count(res);
4771
 
  drizzle_column_st *column;
4772
 
  size_t *lengths;
 
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;
4773
4792
 
4774
 
  while ((row = drizzle_row_next(res)))
 
4793
  while ((row = drizzle_fetch_row(res)))
4775
4794
  {
4776
4795
    uint32_t i;
4777
 
    lengths = drizzle_row_field_sizes(res);
4778
 
    drizzle_column_seek(res, 0);
 
4796
    lengths = drizzle_fetch_lengths(res);
4779
4797
    for (i = 0; i < num_fields; i++)
4780
 
    {
4781
 
      column= drizzle_column_next(res);
4782
 
      append_field(ds, i, column,
 
4798
      append_field(ds, i, &fields[i],
4783
4799
                   (const char*)row[i], lengths[i], !row[i]);
4784
 
    }
4785
4800
    if (!display_result_vertically)
4786
4801
      ds->append("\n");
4787
4802
 
4793
4808
  Append metadata for fields to output
4794
4809
*/
4795
4810
 
4796
 
static void append_metadata(string *ds, drizzle_result_st *res)
 
4811
static void append_metadata(string *ds,
 
4812
                            const DRIZZLE_FIELD *field,
 
4813
                            uint num_fields)
4797
4814
{
4798
 
  drizzle_column_st *column;
 
4815
  const DRIZZLE_FIELD *field_end;
4799
4816
  ds->append("Catalog\tDatabase\tTable\tTable_alias\tColumn\t"
4800
4817
             "Column_alias\tType\tLength\tMax length\tIs_null\t"
4801
4818
             "Flags\tDecimals\tCharsetnr\n");
4802
4819
 
4803
 
  drizzle_column_seek(res, 0);
4804
 
  while ((column= drizzle_column_next(res)))
 
4820
  for (field_end= field+num_fields ;
 
4821
       field < field_end ;
 
4822
       field++)
4805
4823
  {
4806
 
    ds->append(drizzle_column_catalog(column),
4807
 
               strlen(drizzle_column_catalog(column)));
4808
 
    ds->append("\t", 1);
4809
 
    ds->append(drizzle_column_db(column), strlen(drizzle_column_db(column)));
4810
 
    ds->append("\t", 1);
4811
 
    ds->append(drizzle_column_orig_table(column),
4812
 
               strlen(drizzle_column_orig_table(column)));
4813
 
    ds->append("\t", 1);
4814
 
    ds->append(drizzle_column_table(column),
4815
 
               strlen(drizzle_column_table(column)));
4816
 
    ds->append("\t", 1);
4817
 
    ds->append(drizzle_column_orig_name(column),
4818
 
               strlen(drizzle_column_orig_name(column)));
4819
 
    ds->append("\t", 1);
4820
 
    ds->append(drizzle_column_name(column),
4821
 
               strlen(drizzle_column_name(column)));
4822
 
    ds->append("\t", 1);
4823
 
    replace_append_uint(ds, drizzle_column_type_drizzle(column));
4824
 
    ds->append("\t", 1);
4825
 
    replace_append_uint(ds, drizzle_column_size(column));
4826
 
    ds->append("\t", 1);
4827
 
    replace_append_uint(ds, drizzle_column_max_size(column));
4828
 
    ds->append("\t", 1);
4829
 
    ds->append((char*) ((drizzle_column_flags(column) & DRIZZLE_COLUMN_FLAGS_NOT_NULL) ? "N" : "Y"), 1);
4830
 
    ds->append("\t", 1);
4831
 
    replace_append_uint(ds, drizzle_column_flags(column));
4832
 
    ds->append("\t", 1);
4833
 
    replace_append_uint(ds, drizzle_column_decimals(column));
4834
 
    ds->append("\t", 1);
4835
 
    replace_append_uint(ds, drizzle_column_charset(column));
 
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);
4836
4854
    ds->append("\n", 1);
4837
4855
  }
4838
4856
}
4845
4863
static void append_info(string *ds, uint64_t affected_rows,
4846
4864
                        const char *info)
4847
4865
{
4848
 
  ostringstream buf;
4849
 
  buf << "affected rows: " << affected_rows << endl;
4850
 
  ds->append(buf.str());
4851
 
  if (info && strcmp(info, ""))
 
4866
  char buf[40], buff2[21];
 
4867
  sprintf(buf,"affected rows: %s\n", llstr(affected_rows, buff2));
 
4868
  ds->append(buf);
 
4869
  if (info)
4852
4870
  {
4853
4871
    ds->append("info: ");
4854
4872
    ds->append(info);
4861
4879
  Display the table headings with the names tab separated
4862
4880
*/
4863
4881
 
4864
 
static void append_table_headings(string *ds, drizzle_result_st *res)
 
4882
static void append_table_headings(string *ds,
 
4883
                                  const DRIZZLE_FIELD *field,
 
4884
                                  uint num_fields)
4865
4885
{
4866
 
  uint32_t col_idx= 0;
4867
 
  drizzle_column_st *column;
4868
 
  drizzle_column_seek(res, 0);
4869
 
  while ((column= drizzle_column_next(res)))
 
4886
  uint col_idx;
 
4887
  for (col_idx= 0; col_idx < num_fields; col_idx++)
4870
4888
  {
4871
4889
    if (col_idx)
4872
4890
      ds->append("\t", 1);
4873
 
    replace_append(ds, drizzle_column_name(column));
4874
 
    col_idx++;
 
4891
    replace_append(ds, field[col_idx].name);
4875
4892
  }
4876
4893
  ds->append("\n", 1);
4877
4894
}
4883
4900
  Number of warnings appended to ds
4884
4901
*/
4885
4902
 
4886
 
static int append_warnings(string *ds, drizzle_con_st *con,
4887
 
                           drizzle_result_st *res)
 
4903
static int append_warnings(string *ds, DRIZZLE *drizzle)
4888
4904
{
4889
 
  uint32_t count;
4890
 
  drizzle_result_st warn_res;
4891
 
  drizzle_return_t ret;
4892
 
 
4893
 
 
4894
 
  if (!(count= drizzle_result_warning_count(res)))
 
4905
  uint count;
 
4906
  DRIZZLE_RES *warn_res;
 
4907
 
 
4908
 
 
4909
  if (!(count= drizzle_warning_count(drizzle)))
4895
4910
    return(0);
4896
4911
 
4897
 
  if (drizzle_query_str(con, &warn_res, "SHOW WARNINGS", &ret) == NULL ||
4898
 
      ret != DRIZZLE_RETURN_OK)
4899
 
  {
4900
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
4901
 
      die("Error running query \"SHOW WARNINGS\": %s", drizzle_result_error(&warn_res));
4902
 
    else
4903
 
      die("Error running query \"SHOW WARNINGS\": %s", drizzle_con_error(con));
4904
 
  }
4905
 
 
4906
 
  if (drizzle_result_column_count(&warn_res) == 0 ||
4907
 
      drizzle_result_buffer(&warn_res) != DRIZZLE_RETURN_OK)
4908
 
    die("Warning count is %u but didn't get any warnings", count);
4909
 
 
4910
 
  append_result(ds, &warn_res);
4911
 
  drizzle_result_free(&warn_res);
 
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);
4912
4927
 
4913
4928
  return(count);
4914
4929
}
4932
4947
                             int flags, char *query, int query_len,
4933
4948
                             string *ds, string *ds_warnings)
4934
4949
{
4935
 
  drizzle_result_st res;
4936
 
  drizzle_return_t ret;
4937
 
  drizzle_con_st *con= &cn->con;
4938
 
  int err= 0;
4939
 
 
4940
 
  drizzle_con_add_options(con, DRIZZLE_CON_NO_RESULT_READ);
 
4950
  DRIZZLE_RES *res= 0;
 
4951
  DRIZZLE *drizzle= &cn->drizzle;
 
4952
  int err= 0, counter= 0;
4941
4953
 
4942
4954
  if (flags & QUERY_SEND_FLAG)
4943
4955
  {
4944
4956
    /*
4945
4957
     * Send the query
4946
4958
     */
4947
 
 
4948
 
    (void) drizzle_query(con, &res, query, query_len, &ret);
4949
 
    if (ret != DRIZZLE_RETURN_OK)
 
4959
    if (do_send_query(cn, query, query_len, flags))
4950
4960
    {
4951
 
      if (ret == DRIZZLE_RETURN_ERROR_CODE ||
4952
 
          ret == DRIZZLE_RETURN_HANDSHAKE_FAILED)
4953
 
      {
4954
 
        err= drizzle_result_error_code(&res);
4955
 
        handle_error(command, err, drizzle_result_error(&res),
4956
 
                     drizzle_result_sqlstate(&res), ds);
4957
 
        if (ret == DRIZZLE_RETURN_ERROR_CODE)
4958
 
          drizzle_result_free(&res);
4959
 
      }
4960
 
      else
4961
 
      {
4962
 
        handle_error(command, ret, drizzle_con_error(con), "", ds);
4963
 
        err= ret;
4964
 
      }
 
4961
      handle_error(command, drizzle_errno(drizzle), drizzle_error(drizzle),
 
4962
                   drizzle_sqlstate(drizzle), ds);
4965
4963
      goto end;
4966
4964
    }
4967
4965
  }
4968
4966
  if (!(flags & QUERY_REAP_FLAG))
4969
4967
    return;
4970
4968
 
 
4969
  do
4971
4970
  {
4972
4971
    /*
4973
 
     * Read the result packet
4974
 
     */
4975
 
    if (drizzle_result_read(con, &res, &ret) == NULL ||
4976
 
        ret != DRIZZLE_RETURN_OK)
 
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))
4977
4976
    {
4978
 
      if (ret == DRIZZLE_RETURN_ERROR_CODE)
4979
 
      {
4980
 
        handle_error(command, drizzle_result_error_code(&res),
4981
 
                     drizzle_result_error(&res), drizzle_result_sqlstate(&res),
4982
 
                     ds);
4983
 
      }
4984
 
      else
4985
 
        handle_error(command, ret, drizzle_con_error(con), "", ds);
4986
 
      drizzle_result_free(&res);
4987
 
      err= ret;
 
4977
      handle_error(command, drizzle_errno(drizzle), drizzle_error(drizzle),
 
4978
                   drizzle_sqlstate(drizzle), ds);
4988
4979
      goto end;
 
4980
 
4989
4981
    }
4990
4982
 
4991
4983
    /*
4992
4984
      Store the result of the query if it will return any fields
4993
4985
    */
4994
 
    if (drizzle_result_column_count(&res) &&
4995
 
        (ret= drizzle_result_buffer(&res)) != DRIZZLE_RETURN_OK)
 
4986
    if (drizzle_field_count(drizzle) && ((res= drizzle_store_result(drizzle)) == 0))
4996
4987
    {
4997
 
      if (ret == DRIZZLE_RETURN_ERROR_CODE)
4998
 
      {
4999
 
        handle_error(command, drizzle_result_error_code(&res),
5000
 
                     drizzle_result_error(&res), drizzle_result_sqlstate(&res),
5001
 
                     ds);
5002
 
      }
5003
 
      else
5004
 
        handle_error(command, ret, drizzle_con_error(con), "", ds);
5005
 
      drizzle_result_free(&res);
5006
 
      err= ret;
 
4988
      handle_error(command, drizzle_errno(drizzle), drizzle_error(drizzle),
 
4989
                   drizzle_sqlstate(drizzle), ds);
5007
4990
      goto end;
5008
4991
    }
5009
4992
 
5011
4994
    {
5012
4995
      uint64_t affected_rows= 0;    /* Ok to be undef if 'disable_info' is set */
5013
4996
 
5014
 
      if (drizzle_result_column_count(&res))
 
4997
      if (res)
5015
4998
      {
 
4999
        const DRIZZLE_FIELD *fields= drizzle_fetch_fields(res);
 
5000
        uint num_fields= drizzle_num_fields(res);
 
5001
 
5016
5002
        if (display_metadata)
5017
 
          append_metadata(ds, &res);
 
5003
          append_metadata(ds, fields, num_fields);
5018
5004
 
5019
5005
        if (!display_result_vertically)
5020
 
          append_table_headings(ds, &res);
 
5006
          append_table_headings(ds, fields, num_fields);
5021
5007
 
5022
 
        append_result(ds, &res);
 
5008
        append_result(ds, res);
5023
5009
      }
5024
5010
 
5025
5011
      /*
5026
 
        Need to call drizzle_result_affected_rows() before the "new"
 
5012
        Need to call drizzle_affected_rows() before the "new"
5027
5013
        query to find the warnings
5028
5014
      */
5029
5015
      if (!disable_info)
5030
 
        affected_rows= drizzle_result_affected_rows(&res);
 
5016
        affected_rows= drizzle_affected_rows(drizzle);
5031
5017
 
5032
5018
      /*
5033
5019
        Add all warnings to the result. We can't do this if we are in
5034
5020
        the middle of processing results from multi-statement, because
5035
5021
        this will break protocol.
5036
5022
      */
5037
 
      if (!disable_warnings)
 
5023
      if (!disable_warnings && !drizzle_more_results(drizzle))
5038
5024
      {
5039
 
        drizzle_con_remove_options(con, DRIZZLE_CON_NO_RESULT_READ);
5040
 
        if (append_warnings(ds_warnings, con, &res) || ds_warnings->length())
 
5025
        if (append_warnings(ds_warnings, drizzle) || ds_warnings->length())
5041
5026
        {
5042
5027
          ds->append("Warnings:\n", 10);
5043
5028
          ds->append(ds_warnings->c_str(), ds_warnings->length());
5045
5030
      }
5046
5031
 
5047
5032
      if (!disable_info)
5048
 
        append_info(ds, affected_rows, drizzle_result_info(&res));
 
5033
        append_info(ds, affected_rows, drizzle_info(drizzle));
5049
5034
    }
5050
5035
 
5051
 
    drizzle_result_free(&res);
 
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;
5052
5049
  }
 
5050
  assert(err == -1); /* Successful and there are no more results */
5053
5051
 
5054
5052
  /* If we come here the query is both executed and read successfully */
5055
5053
  handle_no_error(command);
5057
5055
end:
5058
5056
 
5059
5057
  /*
5060
 
    We save the return code (drizzleclient_errno(drizzle)) from the last call sent
5061
 
    to the server into the drizzletest builtin variable $drizzleclient_errno. This
 
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
5062
5060
    variable then can be used from the test case itself.
5063
5061
  */
5064
 
  drizzle_con_remove_options(con, DRIZZLE_CON_NO_RESULT_READ);
5065
 
  var_set_errno(err);
 
5062
  var_set_errno(drizzle_errno(drizzle));
5066
5063
  return;
5067
5064
}
5068
5065
 
5087
5084
                  unsigned int err_errno, const char *err_error,
5088
5085
                  const char *err_sqlstate, string *ds)
5089
5086
{
5090
 
  uint32_t i;
5091
 
 
5092
 
 
5093
 
  if (! command->require_file.empty())
 
5087
  uint i;
 
5088
 
 
5089
 
 
5090
  if (command->require_file[0])
5094
5091
  {
5095
5092
    /*
5096
5093
      The query after a "--require" failed. This is fine as long the server
5097
5094
      returned a valid reponse. Don't allow 2013 or 2006 to trigger an
5098
5095
      abort_not_supported_test
5099
5096
    */
5100
 
    if (err_errno == DRIZZLE_RETURN_SERVER_GONE)
 
5097
    if (err_errno == CR_SERVER_LOST ||
 
5098
        err_errno == CR_SERVER_GONE_ERROR)
5101
5099
      die("require query '%s' failed: %d: %s", command->query,
5102
5100
          err_errno, err_error);
5103
5101
 
5109
5107
  if (command->abort_on_error)
5110
5108
    die("query '%s' failed: %d: %s", command->query, err_errno, err_error);
5111
5109
 
5112
 
  for (i= 0 ; (uint32_t) i < command->expected_errors.count ; i++)
 
5110
  for (i= 0 ; (uint) i < command->expected_errors.count ; i++)
5113
5111
  {
5114
5112
    if (((command->expected_errors.err[i].type == ERR_ERRNO) &&
5115
5113
         (command->expected_errors.err[i].code.errnum == err_errno)) ||
5116
5114
        ((command->expected_errors.err[i].type == ERR_SQLSTATE) &&
5117
5115
         (strncmp(command->expected_errors.err[i].code.sqlstate,
5118
 
                  err_sqlstate, DRIZZLE_MAX_SQLSTATE_SIZE) == 0)))
 
5116
                  err_sqlstate, SQLSTATE_LENGTH) == 0)))
5119
5117
    {
5120
5118
      if (!disable_result_log)
5121
5119
      {
5249
5247
    Create a temporary dynamic string to contain the output from
5250
5248
    this query.
5251
5249
  */
5252
 
  if (! command->require_file.empty())
 
5250
  if (command->require_file[0])
5253
5251
  {
5254
5252
    ds= &ds_result;
5255
5253
  }
5292
5290
    ds= save_ds;
5293
5291
  }
5294
5292
 
5295
 
  if (! command->require_file.empty())
 
5293
  if (command->require_file[0])
5296
5294
  {
5297
5295
    /* A result file was specified for _this_ query
5298
5296
       and the output should be checked against an already
5310
5308
static void get_command_type(struct st_command* command)
5311
5309
{
5312
5310
  char save;
5313
 
  uint32_t type;
 
5311
  uint type;
5314
5312
 
5315
5313
 
5316
5314
  if (*command->query == '}')
5395
5393
 
5396
5394
static void mark_progress(struct st_command*, int line)
5397
5395
{
 
5396
  char buf[32], *end;
5398
5397
  uint64_t timer= timer_now();
5399
5398
  if (!progress_start)
5400
5399
    progress_start= timer;
5401
5400
  timer-= progress_start;
5402
5401
 
5403
 
  ostringstream buf;
5404
5402
  /* Milliseconds since start */
5405
 
  buf << timer << "\t";
 
5403
  end= int64_t2str(timer, buf, 10);
 
5404
  ds_progress.append(buf, (int)(end-buf));
 
5405
  ds_progress.append("\t", 1);
5406
5406
 
5407
5407
  /* Parser line number */
5408
 
  buf << line << "\t";
 
5408
  end= int10_to_str(line, buf, 10);
 
5409
  ds_progress.append(buf, (int)(end-buf));
 
5410
  ds_progress.append("\t", 1);
5409
5411
 
5410
5412
  /* Filename */
5411
 
  buf << cur_file->file_name << ":";
 
5413
  ds_progress.append(cur_file->file_name);
 
5414
  ds_progress.append(":", 1);
5412
5415
 
5413
5416
  /* Line in file */
5414
 
  buf << cur_file->lineno << endl;
5415
 
 
5416
 
  ds_progress.append(buf.str());
5417
 
 
5418
 
}
5419
 
 
5420
 
static void check_retries(uint32_t in_opt_max_connect_retries)
5421
 
{
5422
 
  if (in_opt_max_connect_retries > 10000 || opt_max_connect_retries<1)
5423
 
  {
5424
 
    cout << N_("Error: Invalid Value for opt_max_connect_retries"); 
5425
 
    exit(-1);
5426
 
  }
5427
 
  opt_max_connect_retries= in_opt_max_connect_retries;
5428
 
}
5429
 
 
5430
 
static void check_tail_lines(uint32_t in_opt_tail_lines)
5431
 
{
5432
 
  if (in_opt_tail_lines > 10000)
5433
 
  {
5434
 
    cout << N_("Error: Invalid Value for opt_tail_lines"); 
5435
 
    exit(-1);
5436
 
  }
5437
 
  opt_tail_lines= in_opt_tail_lines;
5438
 
}
5439
 
 
5440
 
static void check_sleep(int32_t in_opt_sleep)
5441
 
{
5442
 
  if (in_opt_sleep < -1)
5443
 
  {
5444
 
    cout << N_("Error: Invalid Value for opt_sleep"); 
5445
 
    exit(-1);
5446
 
  }
5447
 
  opt_sleep= in_opt_sleep;
5448
 
}
 
5417
  end= int10_to_str(cur_file->lineno, buf, 10);
 
5418
  ds_progress.append(buf, (int)(end-buf));
 
5419
 
 
5420
 
 
5421
  ds_progress.append("\n", 1);
 
5422
 
 
5423
}
 
5424
 
5449
5425
 
5450
5426
int main(int argc, char **argv)
5451
5427
{
5452
 
try
5453
 
{
5454
5428
  struct st_command *command;
5455
5429
  bool q_send_flag= 0, abort_flag= 0;
5456
 
  uint32_t command_executed= 0, last_command_executed= 0;
5457
 
  string save_file("");
 
5430
  uint command_executed= 0, last_command_executed= 0;
 
5431
  char save_file[FN_REFLEN];
5458
5432
  struct stat res_info;
 
5433
  MY_INIT(argv[0]);
5459
5434
 
 
5435
  save_file[0]= 0;
5460
5436
  TMPDIR[0]= 0;
5461
5437
 
5462
 
  internal::my_init();
5463
 
 
5464
 
  po::options_description commandline_options("Options used only in command line");
5465
 
  commandline_options.add_options()
5466
 
  ("help,?", "Display this help and exit.")
5467
 
  ("mark-progress", po::value<bool>(&opt_mark_progress)->default_value(false)->zero_tokens(),
5468
 
  "Write linenumber and elapsed time to <testname>.progress ")
5469
 
  ("sleep,T", po::value<int32_t>(&opt_sleep)->default_value(-1)->notifier(&check_sleep),
5470
 
  "Sleep always this many seconds on sleep commands.")
5471
 
  ("test-file,x", po::value<string>(),
5472
 
  "Read test from/in this file (default stdin).")
5473
 
  ("timer-file,f", po::value<string>(),
5474
 
  "File where the timing in micro seconds is stored.")
5475
 
  ("tmpdir,t", po::value<string>(),
5476
 
  "Temporary directory where sockets are put.")
5477
 
  ("verbose,v", po::value<bool>(&verbose)->default_value(false),
5478
 
  "Write more.")
5479
 
  ("version,V", "Output version information and exit.")
5480
 
  ("no-defaults", po::value<bool>()->default_value(false)->zero_tokens(),
5481
 
  "Configuration file defaults are not used if no-defaults is set")
5482
 
  ;
5483
 
 
5484
 
  po::options_description test_options("Options specific to the drizzleimport");
5485
 
  test_options.add_options()
5486
 
  ("basedir,b", po::value<string>(&opt_basedir)->default_value(""),
5487
 
  "Basedir for tests.")
5488
 
  ("character-sets-dir", po::value<string>(&opt_charsets_dir)->default_value(""),
5489
 
  "Directory where character sets are.")
5490
 
  ("database,D", po::value<string>(&opt_db)->default_value(""),
5491
 
  "Database to use.")
5492
 
  ("include,i", po::value<string>(&opt_include)->default_value(""),
5493
 
  "Include SQL before each test case.")  
5494
 
  ("testdir", po::value<string>(&opt_testdir)->default_value(""),
5495
 
  "Path to use to search for test files")
5496
 
  ("logdir", po::value<string>(&opt_logdir)->default_value(""),
5497
 
  "Directory for log files")
5498
 
  ("max-connect-retries", po::value<uint32_t>(&opt_max_connect_retries)->default_value(500)->notifier(&check_retries),
5499
 
  "Max number of connection attempts when connecting to server")
5500
 
  ("quiet,s", po::value<bool>(&silent)->default_value(false)->zero_tokens(),
5501
 
  "Suppress all normal output.")
5502
 
  ("record,r", "Record output of test_file into result file.")
5503
 
  ("result-file,R", po::value<string>(&result_file_name)->default_value(""),
5504
 
  "Read/Store result from/in this file.")
5505
 
  ("silent,s", po::value<bool>(&silent)->default_value(false)->zero_tokens(),
5506
 
  "Suppress all normal output. Synonym for --quiet.")
5507
 
  ("tail-lines", po::value<uint32_t>(&opt_tail_lines)->default_value(0)->notifier(&check_tail_lines),
5508
 
  "Number of lines of the resul to include in a failure report")
5509
 
  ;
5510
 
 
5511
 
  po::options_description client_options("Options specific to the client");
5512
 
  client_options.add_options()
5513
 
 
5514
 
  ("host,h", po::value<string>(&opt_host)->default_value("localhost"),
5515
 
  "Connect to host.")
5516
 
  ("password,P", po::value<string>(&password)->default_value("PASSWORD_SENTINEL"),
5517
 
  "Password to use when connecting to server.")
5518
 
  ("port,p", po::value<uint32_t>(&opt_port)->default_value(0),
5519
 
  "Port number to use for connection or 0 for default")
5520
 
  ("protocol", po::value<string>(&opt_protocol),
5521
 
  "The protocol of connection (mysql or drizzle).")
5522
 
  ("user,u", po::value<string>(&opt_user)->default_value(""),
5523
 
  "User for login.")
5524
 
  ;
5525
 
 
5526
 
  po::positional_options_description p;
5527
 
  p.add("database", 1);
5528
 
 
5529
 
  po::options_description long_options("Allowed Options");
5530
 
  long_options.add(commandline_options).add(test_options).add(client_options);
5531
 
 
5532
 
  std::string system_config_dir_test(SYSCONFDIR); 
5533
 
  system_config_dir_test.append("/drizzle/drizzletest.cnf");
5534
 
 
5535
 
  std::string system_config_dir_client(SYSCONFDIR); 
5536
 
  system_config_dir_client.append("/drizzle/client.cnf");
5537
 
 
5538
 
  std::string user_config_dir((getenv("XDG_CONFIG_HOME")? getenv("XDG_CONFIG_HOME"):"~/.config"));
5539
 
 
5540
 
  if (user_config_dir.compare(0, 2, "~/") == 0)
5541
 
  {
5542
 
    char *homedir;
5543
 
    homedir= getenv("HOME");
5544
 
    if (homedir != NULL)
5545
 
      user_config_dir.replace(0, 1, homedir);
5546
 
  }
5547
 
 
5548
 
  po::variables_map vm;
5549
 
 
5550
 
  // Disable allow_guessing
5551
 
  int style = po::command_line_style::default_style & ~po::command_line_style::allow_guessing;
5552
 
 
5553
 
  po::store(po::command_line_parser(argc, argv).options(long_options).
5554
 
            style(style).positional(p).extra_parser(parse_password_arg).run(),
5555
 
            vm);
5556
 
 
5557
 
  if (! vm["no-defaults"].as<bool>())
5558
 
  {
5559
 
    std::string user_config_dir_test(user_config_dir);
5560
 
    user_config_dir_test.append("/drizzle/drizzletest.cnf"); 
5561
 
 
5562
 
    std::string user_config_dir_client(user_config_dir);
5563
 
    user_config_dir_client.append("/drizzle/client.cnf");
5564
 
 
5565
 
    ifstream user_test_ifs(user_config_dir_test.c_str());
5566
 
    po::store(parse_config_file(user_test_ifs, test_options), vm);
5567
 
 
5568
 
    ifstream user_client_ifs(user_config_dir_client.c_str());
5569
 
    po::store(parse_config_file(user_client_ifs, client_options), vm);
5570
 
 
5571
 
    ifstream system_test_ifs(system_config_dir_test.c_str());
5572
 
    store(parse_config_file(system_test_ifs, test_options), vm);
5573
 
 
5574
 
    ifstream system_client_ifs(system_config_dir_client.c_str());
5575
 
    po::store(parse_config_file(system_client_ifs, client_options), vm);
5576
 
  }
5577
 
 
5578
 
  po::notify(vm);
5579
 
 
5580
5438
  /* Init expected errors */
5581
5439
  memset(&saved_expected_errors, 0, sizeof(saved_expected_errors));
5582
5440
 
5600
5458
  cur_block->ok= true; /* Outer block should always be executed */
5601
5459
  cur_block->cmd= cmd_none;
5602
5460
 
5603
 
  var_set_string("$DRIZZLE_SERVER_VERSION", drizzle_version());
 
5461
  if (hash_init(&var_hash, charset_info,
 
5462
                1024, 0, 0, get_var_key, var_free, MYF(0)))
 
5463
    die("Variable hash initialization failed");
 
5464
 
 
5465
  var_set_string("$DRIZZLE_SERVER_VERSION", drizzle_get_client_info());
5604
5466
 
5605
5467
  memset(&master_pos, 0, sizeof(master_pos));
5606
5468
 
5613
5475
  ds_progress.reserve(2048);
5614
5476
  ds_warning_messages.reserve(2048);
5615
5477
 
5616
 
 
5617
 
  if (vm.count("record"))
5618
 
  {
5619
 
    record = 1;
5620
 
  }
5621
 
 
5622
 
  if (vm.count("test-file"))
5623
 
  {
5624
 
    string tmp= vm["test-file"].as<string>();
5625
 
    char buff[FN_REFLEN];
5626
 
    if (!internal::test_if_hard_path(tmp.c_str()))
5627
 
    {
5628
 
      snprintf(buff, sizeof(buff), "%s%s",opt_basedir.c_str(),tmp.c_str());
5629
 
      tmp= buff;
5630
 
    }
5631
 
    internal::fn_format(buff, tmp.c_str(), "", "", MY_UNPACK_FILENAME);
5632
 
    assert(cur_file == file_stack && cur_file->file == 0);
5633
 
    if (!(cur_file->file= fopen(buff, "r")))
5634
 
    {
5635
 
      fprintf(stderr, _("Could not open '%s' for reading: errno = %d"), buff, errno);
5636
 
      return EXIT_ARGUMENT_INVALID;
5637
 
    }
5638
 
    if (!(cur_file->file_name= strdup(buff)))
5639
 
    {
5640
 
      fprintf(stderr, _("Out of memory"));
5641
 
      return EXIT_OUT_OF_MEMORY;
5642
 
    }
5643
 
    cur_file->lineno= 1;
5644
 
  }
5645
 
 
5646
 
  if (vm.count("timer-file"))
5647
 
  {
5648
 
    string tmp= vm["timer-file"].as<string>().c_str();
5649
 
    static char buff[FN_REFLEN];
5650
 
    if (!internal::test_if_hard_path(tmp.c_str()))
5651
 
    {
5652
 
      snprintf(buff, sizeof(buff), "%s%s",opt_basedir.c_str(),tmp.c_str());
5653
 
      tmp= buff;
5654
 
    }
5655
 
    internal::fn_format(buff, tmp.c_str(), "", "", MY_UNPACK_FILENAME);
5656
 
    timer_file= buff;
5657
 
    unlink(timer_file);       /* Ignore error, may not exist */
5658
 
  }
5659
 
 
5660
 
  if (vm.count("protocol"))
5661
 
  {
5662
 
    std::transform(opt_protocol.begin(), opt_protocol.end(),
5663
 
      opt_protocol.begin(), ::tolower);
5664
 
 
5665
 
    if (not opt_protocol.compare("mysql"))
5666
 
      use_drizzle_protocol=false;
5667
 
    else if (not opt_protocol.compare("drizzle"))
5668
 
      use_drizzle_protocol=true;
5669
 
    else
5670
 
    {
5671
 
      cout << _("Error: Unknown protocol") << " '" << opt_protocol << "'" << endl;
5672
 
      exit(-1);
5673
 
    }
5674
 
  }
5675
 
 
5676
 
  if (vm.count("port"))
5677
 
  {
5678
 
    /* If the port number is > 65535 it is not a valid port
5679
 
       This also helps with potential data loss casting unsigned long to a
5680
 
       uint32_t. */
5681
 
    if (opt_port > 65535)
5682
 
    {
5683
 
      fprintf(stderr, _("Value supplied for port is not valid.\n"));
5684
 
      exit(EXIT_ARGUMENT_INVALID);
5685
 
    }
5686
 
  }
5687
 
 
5688
 
  if( vm.count("password") )
5689
 
  {
5690
 
    if (!opt_password.empty())
5691
 
      opt_password.erase();
5692
 
    if (password == PASSWORD_SENTINEL)
5693
 
    {
5694
 
      opt_password= "";
5695
 
    }
5696
 
    else
5697
 
    {
5698
 
      opt_password= password;
5699
 
      tty_password= false;
5700
 
    }
5701
 
  }
5702
 
  else
5703
 
  {
5704
 
      tty_password= true;
5705
 
  }
5706
 
 
5707
 
  if (vm.count("tmpdir"))
5708
 
  {
5709
 
    strncpy(TMPDIR, vm["tmpdir"].as<string>().c_str(), sizeof(TMPDIR));
5710
 
  }
5711
 
 
5712
 
  if (vm.count("version"))
5713
 
  {
5714
 
    printf("%s  Ver %s Distrib %s, for %s-%s (%s)\n",internal::my_progname,MTEST_VERSION,
5715
 
    drizzle_version(),HOST_VENDOR,HOST_OS,HOST_CPU);
5716
 
    exit(0);
5717
 
  }
5718
 
  
5719
 
  if (vm.count("help"))
5720
 
  {
5721
 
    printf("%s  Ver %s Distrib %s, for %s-%s (%s)\n",internal::my_progname,MTEST_VERSION,
5722
 
    drizzle_version(),HOST_VENDOR,HOST_OS,HOST_CPU);
5723
 
    printf("MySQL AB, by Sasha, Matt, Monty & Jani\n");
5724
 
    printf("Drizzle version modified by Brian, Jay, Monty Taylor, PatG and Stewart\n");
5725
 
    printf("This software comes with ABSOLUTELY NO WARRANTY\n\n");
5726
 
    printf("Runs a test against the DRIZZLE server and compares output with a results file.\n\n");
5727
 
    printf("Usage: %s [OPTIONS] [database] < test_file\n", internal::my_progname);
5728
 
    exit(0);
5729
 
  }
5730
 
 
5731
 
  if (tty_password)
5732
 
  {
5733
 
    opt_pass= client_get_tty_password(NULL);          /* purify tested */
5734
 
  }
 
5478
  parse_args(argc, argv);
5735
5479
 
5736
5480
  server_initialized= 1;
5737
5481
  if (cur_file == file_stack && cur_file->file == 0)
5738
5482
  {
5739
5483
    cur_file->file= stdin;
5740
 
    cur_file->file_name= strdup("<stdin>");
5741
 
    if (cur_file->file_name == NULL)
5742
 
      die("Out of memory");
 
5484
    cur_file->file_name= my_strdup("<stdin>", MYF(MY_WME));
5743
5485
    cur_file->lineno= 1;
5744
5486
  }
5745
5487
  cur_con= connections;
5746
 
  if ((cur_con->drizzle= drizzle_create(NULL)) == NULL)
 
5488
  if (!( drizzle_create(&cur_con->drizzle)))
5747
5489
    die("Failed in drizzle_create()");
5748
 
  if (!( drizzle_con_create(cur_con->drizzle, &cur_con->con)))
5749
 
    die("Failed in drizzle_con_create()");
5750
 
  drizzle_con_add_options(&cur_con->con, use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL);
 
5490
  if (opt_compress)
 
5491
    drizzle_options(&cur_con->drizzle,DRIZZLE_OPT_COMPRESS,NULL);
 
5492
  drizzle_options(&cur_con->drizzle, DRIZZLE_OPT_LOCAL_INFILE, 0);
5751
5493
 
5752
 
  if (!(cur_con->name = strdup("default")))
 
5494
  if (!(cur_con->name = my_strdup("default", MYF(MY_WME))))
5753
5495
    die("Out of memory");
5754
 
  safe_connect(&cur_con->con, cur_con->name, opt_host, opt_user, opt_pass,
 
5496
 
 
5497
  safe_connect(&cur_con->drizzle, cur_con->name, opt_host, opt_user, opt_pass,
5755
5498
               opt_db, opt_port);
5756
5499
 
5757
 
  fill_global_error_names();
5758
 
 
5759
5500
  /* Use all time until exit if no explicit 'start_timer' */
5760
5501
  timer_start= timer_now();
5761
5502
 
5762
5503
  /*
5763
 
    Initialize $drizzleclient_errno with -1, so we can
 
5504
    Initialize $drizzle_errno with -1, so we can
5764
5505
    - distinguish it from valid values ( >= 0 ) and
5765
5506
    - detect if there was never a command sent to the server
5766
5507
  */
5767
5508
  var_set_errno(-1);
5768
5509
 
5769
 
  /* Update $drizzleclient_get_server_version to that of current connection */
5770
 
  var_set_drizzleclient_get_server_version(&cur_con->con);
 
5510
  /* Update $drizzle_get_server_version to that of current connection */
 
5511
  var_set_drizzle_get_server_version(&cur_con->drizzle);
5771
5512
 
5772
 
  if (! opt_include.empty())
 
5513
  if (opt_include)
5773
5514
  {
5774
 
    open_file(opt_include.c_str());
 
5515
    open_file(opt_include);
5775
5516
  }
5776
5517
 
5777
5518
  while (!read_command(&command) && !abort_flag)
5885
5626
        /* Check for special property for this query */
5886
5627
        display_result_vertically|= (command->type == Q_QUERY_VERTICAL);
5887
5628
 
5888
 
        if (! save_file.empty())
 
5629
        if (save_file[0])
5889
5630
        {
5890
 
          command->require_file= save_file;
5891
 
          save_file.clear();
 
5631
          strncpy(command->require_file, save_file, sizeof(save_file) - 1);
 
5632
          save_file[0]= 0;
5892
5633
        }
5893
5634
        run_query(cur_con, command, flags);
5894
5635
        command_executed++;
5925
5666
        command->last_argument= command->end;
5926
5667
        break;
5927
5668
      case Q_REQUIRE:
5928
 
        do_get_file_name(command, save_file);
 
5669
        do_get_file_name(command, save_file, sizeof(save_file));
5929
5670
        break;
5930
5671
      case Q_ERROR:
5931
5672
        do_get_errcodes(command);
5955
5696
        command->last_argument= command->end;
5956
5697
        break;
5957
5698
      case Q_PING:
5958
 
        {
5959
 
          drizzle_result_st result;
5960
 
          drizzle_return_t ret;
5961
 
          (void) drizzle_ping(&cur_con->con, &result, &ret);
5962
 
          if (ret == DRIZZLE_RETURN_OK || ret == DRIZZLE_RETURN_ERROR_CODE)
5963
 
            drizzle_result_free(&result);
5964
 
        }
 
5699
        (void) drizzle_ping(&cur_con->drizzle);
5965
5700
        break;
5966
5701
      case Q_EXEC:
5967
5702
        do_exec(command);
5979
5714
        do_set_charset(command);
5980
5715
        break;
5981
5716
      case Q_DISABLE_RECONNECT:
5982
 
        set_reconnect(&cur_con->con, 0);
 
5717
        set_reconnect(&cur_con->drizzle, 0);
5983
5718
        break;
5984
5719
      case Q_ENABLE_RECONNECT:
5985
 
        set_reconnect(&cur_con->con, 1);
 
5720
        set_reconnect(&cur_con->drizzle, 1);
5986
5721
        break;
5987
5722
      case Q_DISABLE_PARSING:
5988
5723
        if (parsing_disabled == 0)
6075
5810
  */
6076
5811
  if (ds_res.length())
6077
5812
  {
6078
 
    if (! result_file_name.empty())
 
5813
    if (result_file_name)
6079
5814
    {
6080
5815
      /* A result file has been specified */
6081
5816
 
6082
5817
      if (record)
6083
5818
      {
6084
5819
        /* Recording - dump the output from test to result file */
6085
 
        str_to_file(result_file_name.c_str(), ds_res.c_str(), ds_res.length());
 
5820
        str_to_file(result_file_name, ds_res.c_str(), ds_res.length());
6086
5821
      }
6087
5822
      else
6088
5823
      {
6105
5840
  }
6106
5841
 
6107
5842
  if (!command_executed &&
6108
 
      ! result_file_name.empty() && !stat(result_file_name.c_str(), &res_info))
 
5843
      result_file_name && !stat(result_file_name, &res_info))
6109
5844
  {
6110
5845
    /*
6111
5846
      my_stat() successful on result file. Check if we have not run a
6117
5852
    die("No queries executed but result file found!");
6118
5853
  }
6119
5854
 
6120
 
  if ( opt_mark_progress && ! result_file_name.empty() )
 
5855
  if ( opt_mark_progress && result_file_name )
6121
5856
    dump_progress();
6122
5857
 
6123
5858
  /* Dump warning messages */
6124
 
  if (! result_file_name.empty() && ds_warning_messages.length())
 
5859
  if (result_file_name && ds_warning_messages.length())
6125
5860
    dump_warning_messages();
6126
5861
 
6127
5862
  timer_output();
6128
5863
  /* Yes, if we got this far the test has suceeded! Sakila smiles */
6129
5864
  cleanup_and_exit(0);
6130
 
}
6131
 
 
6132
 
  catch(exception &err)
6133
 
  {
6134
 
    cerr<<err.what()<<endl;
6135
 
  }
6136
 
 
6137
5865
  return 0; /* Keep compiler happy too */
6138
5866
}
6139
5867
 
6163
5891
{
6164
5892
  if (timer_file)
6165
5893
  {
6166
 
    ostringstream buf;
 
5894
    char buf[32], *end;
6167
5895
    uint64_t timer= timer_now() - timer_start;
6168
 
    buf << timer;
6169
 
    str_to_file(timer_file,buf.str().c_str(), buf.str().size() );
 
5896
    end= int64_t2str(timer, buf, 10);
 
5897
    str_to_file(timer_file,buf, (int) (end-buf));
6170
5898
    /* Timer has been written to the file, don't use it anymore */
6171
5899
    timer_file= 0;
6172
5900
  }
6175
5903
 
6176
5904
uint64_t timer_now(void)
6177
5905
{
6178
 
#if defined(HAVE_GETHRTIME)
6179
 
  return gethrtime()/1000/1000;
6180
 
#else
6181
 
  uint64_t newtime;
6182
 
  struct timeval t;
6183
 
  /*
6184
 
    The following loop is here because gettimeofday may fail on some systems
6185
 
  */
6186
 
  while (gettimeofday(&t, NULL) != 0)
6187
 
  {}
6188
 
  newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
6189
 
  return newtime/1000;
6190
 
#endif  /* defined(HAVE_GETHRTIME) */
 
5906
  return my_micro_time() / 1000;
6191
5907
}
6192
5908
 
6193
5909
 
6210
5926
    die("Missing argument in %s", command->query);
6211
5927
 
6212
5928
  /* Allocate a buffer for results */
6213
 
  start= buff= (char *)malloc(strlen(from)+1);
 
5929
  start= buff= (char *)my_malloc(strlen(from)+1,MYF(MY_WME | MY_FAE));
6214
5930
  while (*from)
6215
5931
  {
6216
5932
    char *to;
6217
 
    uint32_t column_number;
 
5933
    uint column_number;
6218
5934
 
6219
5935
    to= get_string(&buff, &from, command);
6220
5936
    if (!(column_number= atoi(to)) || column_number > MAX_COLUMNS)
6223
5939
      die("Wrong number of arguments to replace_column in '%s'", command->query);
6224
5940
    to= get_string(&buff, &from, command);
6225
5941
    free(replace_column[column_number-1]);
6226
 
    replace_column[column_number-1]= strdup(to);
6227
 
    if (replace_column[column_number-1] == NULL)
6228
 
      die("Out of memory");
 
5942
    replace_column[column_number-1]= my_strdup(to, MYF(MY_WME | MY_FAE));
6229
5943
    set_if_bigger(max_replace_column, column_number);
6230
5944
  }
6231
5945
  free(start);
6235
5949
 
6236
5950
void free_replace_column()
6237
5951
{
6238
 
  uint32_t i;
 
5952
  uint i;
6239
5953
  for (i=0 ; i < max_replace_column ; i++)
6240
5954
  {
6241
5955
    if (replace_column[i])
6259
5973
  TYPELIB typelib;        /* Pointer to strings */
6260
5974
  unsigned char  *str;          /* Strings is here */
6261
5975
  uint8_t *flag;          /* Flag about each var. */
6262
 
  uint32_t  array_allocs,max_count,length,max_length;
 
5976
  uint  array_allocs,max_count,length,max_length;
6263
5977
} POINTER_ARRAY;
6264
5978
 
6265
5979
struct st_replace;
6266
 
struct st_replace *init_replace(char * *from, char * *to, uint32_t count,
 
5980
struct st_replace *init_replace(char * *from, char * *to, uint count,
6267
5981
                                char * word_end_chars);
6268
5982
int insert_pointer_name(POINTER_ARRAY *pa,char * name);
6269
5983
void replace_strings_append(struct st_replace *rep, string* ds,
6270
5984
                            const char *from, int len);
6271
5985
void free_pointer_array(POINTER_ARRAY *pa);
6272
5986
 
6273
 
struct st_replace *glob_replace= NULL;
 
5987
struct st_replace *glob_replace;
6274
5988
 
6275
5989
/*
6276
5990
  Get arguments for replace. The syntax is:
6282
5996
 
6283
5997
void do_get_replace(struct st_command *command)
6284
5998
{
6285
 
  uint32_t i;
 
5999
  uint i;
6286
6000
  char *from= command->first_argument;
6287
6001
  char *buff, *start;
6288
6002
  char word_end_chars[256], *pos;
6295
6009
  memset(&from_array, 0, sizeof(from_array));
6296
6010
  if (!*from)
6297
6011
    die("Missing argument in %s", command->query);
6298
 
  start= buff= (char *)malloc(strlen(from)+1);
 
6012
  start= buff= (char *)my_malloc(strlen(from)+1,MYF(MY_WME | MY_FAE));
6299
6013
  while (*from)
6300
6014
  {
6301
6015
    char *to= buff;
6313
6027
  *pos=0;          /* End pointer */
6314
6028
  if (!(glob_replace= init_replace((char**) from_array.typelib.type_names,
6315
6029
                                   (char**) to_array.typelib.type_names,
6316
 
                                   (uint32_t) from_array.typelib.count,
 
6030
                                   (uint) from_array.typelib.count,
6317
6031
                                   word_end_chars)))
6318
6032
    die("Can't initialize replace from '%s'", command->query);
6319
6033
  free_pointer_array(&from_array);
6344
6058
typedef struct st_replace_found {
6345
6059
  bool found;
6346
6060
  char *replace_string;
6347
 
  uint32_t to_offset;
 
6061
  uint to_offset;
6348
6062
  int from_offset;
6349
6063
} REPLACE_STRING;
6350
6064
 
6402
6116
  char* pattern; /* Pattern to be replaced */
6403
6117
  char* replace; /* String or expression to replace the pattern with */
6404
6118
  int icase; /* true if the match is case insensitive */
6405
 
  int global; /* true if the match should be global -- 
6406
 
                 i.e. repeat the matching until the end of the string */
6407
6119
};
6408
6120
 
6409
6121
struct st_replace_regex
6427
6139
struct st_replace_regex *glob_replace_regex= 0;
6428
6140
 
6429
6141
int reg_replace(char** buf_p, int* buf_len_p, char *pattern, char *replace,
6430
 
                char *string, int icase, int global);
 
6142
                char *string, int icase);
6431
6143
 
6432
6144
 
6433
6145
 
6472
6184
  char* buf,*expr_end;
6473
6185
  char* p;
6474
6186
  char* buf_p;
6475
 
  uint32_t expr_len= strlen(expr);
 
6187
  uint expr_len= strlen(expr);
6476
6188
  char last_c = 0;
6477
6189
  struct st_regex reg;
6478
6190
 
6479
 
  res=(st_replace_regex*)malloc(sizeof(*res)+expr_len);
6480
 
  if (!res)
6481
 
    return NULL;
 
6191
  /* my_malloc() will die on fail with MY_FAE */
 
6192
  res=(struct st_replace_regex*)my_malloc(
 
6193
                                          sizeof(*res)+expr_len ,MYF(MY_FAE+MY_WME));
6482
6194
  my_init_dynamic_array(&res->regex_arr,sizeof(struct st_regex),128,128);
6483
6195
 
6484
6196
  buf= (char*)res + sizeof(*res);
6528
6240
 
6529
6241
    /* Check if we should do matching case insensitive */
6530
6242
    if (p < expr_end && *p == 'i')
6531
 
    {
6532
 
      p++;
6533
6243
      reg.icase= 1;
6534
 
    }
6535
 
 
6536
 
    /* Check if we should do matching globally */
6537
 
    if (p < expr_end && *p == 'g')
6538
 
    {
6539
 
      p++;
6540
 
      reg.global= 1;
6541
 
    }
6542
6244
 
6543
6245
    /* done parsing the statement, now place it in regex_arr */
6544
6246
    if (insert_dynamic(&res->regex_arr,(unsigned char*) &reg))
6545
6247
      die("Out of memory");
6546
6248
  }
6547
6249
  res->odd_buf_len= res->even_buf_len= 8192;
6548
 
  res->even_buf= (char*)malloc(res->even_buf_len);
6549
 
  res->odd_buf= (char*)malloc(res->odd_buf_len);
 
6250
  res->even_buf= (char*)my_malloc(res->even_buf_len,MYF(MY_WME+MY_FAE));
 
6251
  res->odd_buf= (char*)my_malloc(res->odd_buf_len,MYF(MY_WME+MY_FAE));
6550
6252
  res->buf= res->even_buf;
6551
6253
 
6552
6254
  return res;
6578
6280
 
6579
6281
static int multi_reg_replace(struct st_replace_regex* r,char* val)
6580
6282
{
6581
 
  uint32_t i;
 
6283
  uint i;
6582
6284
  char* in_buf, *out_buf;
6583
6285
  int* buf_len_p;
6584
6286
 
6596
6298
    get_dynamic(&r->regex_arr,(unsigned char*)&re,i);
6597
6299
 
6598
6300
    if (!reg_replace(&out_buf, buf_len_p, re.pattern, re.replace,
6599
 
                     in_buf, re.icase, re.global))
 
6301
                     in_buf, re.icase))
6600
6302
    {
6601
6303
      /* if the buffer has been reallocated, make adjustements */
6602
6304
      if (save_out_buf != out_buf)
6666
6368
  icase - flag, if set to 1 the match is case insensitive
6667
6369
*/
6668
6370
int reg_replace(char** buf_p, int* buf_len_p, char *pattern,
6669
 
                char *replace, char *in_string, int icase, int global)
 
6371
                char *replace, char *in_string, int icase)
6670
6372
{
 
6373
  string string_to_match(in_string);
6671
6374
  const char *error= NULL;
6672
6375
  int erroffset;
6673
6376
  int ovector[3];
6674
6377
  pcre *re= pcre_compile(pattern,
6675
 
                         icase ? PCRE_CASELESS | PCRE_MULTILINE : PCRE_MULTILINE,
 
6378
                         icase ? PCRE_CASELESS : 0,
6676
6379
                         &error, &erroffset, NULL);
6677
6380
  if (re == NULL)
6678
6381
    return 1;
6679
6382
 
6680
 
  if (! global)
6681
 
  {
6682
 
 
6683
 
    int rc= pcre_exec(re, NULL, in_string, (int)strlen(in_string),
6684
 
                      0, 0, ovector, 3);
6685
 
    if (rc < 0)
6686
 
    {
6687
 
      pcre_free(re);
6688
 
      return 1;
6689
 
    }
6690
 
 
6691
 
    char *substring_to_replace= in_string + ovector[0];
6692
 
    int substring_length= ovector[1] - ovector[0];
6693
 
    *buf_len_p= strlen(in_string) - substring_length + strlen(replace);
6694
 
    char * new_buf = (char *)malloc(*buf_len_p+1);
6695
 
    if (new_buf == NULL)
6696
 
    {
6697
 
      pcre_free(re);
6698
 
      return 1;
6699
 
    }
6700
 
 
6701
 
    memset(new_buf, 0, *buf_len_p+1);
6702
 
    strncpy(new_buf, in_string, substring_to_replace-in_string);
6703
 
    strncpy(new_buf+(substring_to_replace-in_string), replace, strlen(replace));
6704
 
    strncpy(new_buf+(substring_to_replace-in_string)+strlen(replace),
6705
 
            substring_to_replace + substring_length,
6706
 
            strlen(in_string)
6707
 
              - substring_length
6708
 
              - (substring_to_replace-in_string));
6709
 
    *buf_p= new_buf;
6710
 
 
6711
 
    pcre_free(re);
6712
 
    return 0;
6713
 
  }
6714
 
  else
6715
 
  {
6716
 
    /* Repeatedly replace the string with the matched regex */
6717
 
    string subject(in_string);
6718
 
    size_t replace_length= strlen(replace);
6719
 
    size_t length_of_replacement= strlen(replace);
6720
 
    size_t current_position= 0;
6721
 
    int rc= 0;
6722
 
 
6723
 
    while (true) 
6724
 
    {
6725
 
      rc= pcre_exec(re, NULL, subject.c_str(), subject.length(), 
6726
 
                    current_position, 0, ovector, 3);
6727
 
      if (rc < 0)
6728
 
      {
6729
 
        break;
6730
 
      }
6731
 
 
6732
 
      current_position= static_cast<size_t>(ovector[0]);
6733
 
      replace_length= static_cast<size_t>(ovector[1] - ovector[0]);
6734
 
      subject.replace(current_position, replace_length, replace, length_of_replacement);
6735
 
      current_position= current_position + length_of_replacement;
6736
 
    }
6737
 
 
6738
 
    char *new_buf = (char *) malloc(subject.length() + 1);
6739
 
    if (new_buf == NULL)
6740
 
    {
6741
 
      pcre_free(re);
6742
 
      return 1;
6743
 
    }
6744
 
    memset(new_buf, 0, subject.length() + 1);
6745
 
    strncpy(new_buf, subject.c_str(), subject.length());
6746
 
    *buf_len_p= subject.length() + 1;
6747
 
    *buf_p= new_buf;
6748
 
          
6749
 
    pcre_free(re);
6750
 
    return 0;
6751
 
  }
 
6383
  int rc= pcre_exec(re, NULL, in_string, (int)strlen(in_string),
 
6384
                    0, 0, ovector, 3);
 
6385
  if (rc < 0)
 
6386
  {
 
6387
    pcre_free(re);
 
6388
    return 1;
 
6389
  }
 
6390
 
 
6391
  char *substring_to_replace= in_string + ovector[0];
 
6392
  int substring_length= ovector[1] - ovector[0];
 
6393
  *buf_len_p= strlen(in_string) - substring_length + strlen(replace);
 
6394
  char * new_buf = (char *)malloc(*buf_len_p+1);
 
6395
  if (new_buf == NULL)
 
6396
  {
 
6397
    pcre_free(re);
 
6398
    return 1;
 
6399
  }
 
6400
 
 
6401
  memset(new_buf, 0, *buf_len_p+1);
 
6402
  strncpy(new_buf, in_string, substring_to_replace-in_string);
 
6403
  strncpy(new_buf+(substring_to_replace-in_string), replace, strlen(replace));
 
6404
  strncpy(new_buf+(substring_to_replace-in_string)+strlen(replace),
 
6405
          substring_to_replace + substring_length,
 
6406
          strlen(in_string)
 
6407
            - substring_length
 
6408
            - (substring_to_replace-in_string));
 
6409
  *buf_p= new_buf;
 
6410
 
 
6411
  pcre_free(re);
 
6412
  return 0;
6752
6413
}
6753
6414
 
6754
6415
 
6755
6416
#ifndef WORD_BIT
6756
 
#define WORD_BIT (8*sizeof(uint32_t))
 
6417
#define WORD_BIT (8*sizeof(uint))
6757
6418
#endif
6758
6419
 
6759
6420
#define SET_MALLOC_HUNC 64
6760
6421
#define LAST_CHAR_CODE 259
6761
6422
 
6762
6423
typedef struct st_rep_set {
6763
 
  uint32_t  *bits;        /* Pointer to used sets */
 
6424
  uint  *bits;        /* Pointer to used sets */
6764
6425
  short next[LAST_CHAR_CODE];    /* Pointer to next sets */
6765
 
  uint32_t  found_len;      /* Best match to date */
 
6426
  uint  found_len;      /* Best match to date */
6766
6427
  int  found_offset;
6767
 
  uint32_t  table_offset;
6768
 
  uint32_t  size_of_bits;      /* For convinience */
 
6428
  uint  table_offset;
 
6429
  uint  size_of_bits;      /* For convinience */
6769
6430
} REP_SET;
6770
6431
 
6771
6432
typedef struct st_rep_sets {
6772
 
  uint32_t    count;      /* Number of sets */
6773
 
  uint32_t    extra;      /* Extra sets in buffer */
6774
 
  uint32_t    invisible;    /* Sets not chown */
6775
 
  uint32_t    size_of_bits;
 
6433
  uint    count;      /* Number of sets */
 
6434
  uint    extra;      /* Extra sets in buffer */
 
6435
  uint    invisible;    /* Sets not chown */
 
6436
  uint    size_of_bits;
6776
6437
  REP_SET  *set,*set_buffer;
6777
 
  uint32_t    *bit_buffer;
 
6438
  uint    *bit_buffer;
6778
6439
} REP_SETS;
6779
6440
 
6780
6441
typedef struct st_found_set {
6781
 
  uint32_t table_offset;
 
6442
  uint table_offset;
6782
6443
  int found_offset;
6783
6444
} FOUND_SET;
6784
6445
 
6785
6446
typedef struct st_follow {
6786
6447
  int chr;
6787
 
  uint32_t table_offset;
6788
 
  uint32_t len;
 
6448
  uint table_offset;
 
6449
  uint len;
6789
6450
} FOLLOWS;
6790
6451
 
6791
6452
 
6792
 
int init_sets(REP_SETS *sets,uint32_t states);
 
6453
int init_sets(REP_SETS *sets,uint states);
6793
6454
REP_SET *make_new_set(REP_SETS *sets);
6794
6455
void make_sets_invisible(REP_SETS *sets);
6795
6456
void free_last_set(REP_SETS *sets);
6796
6457
void free_sets(REP_SETS *sets);
6797
 
void internal_set_bit(REP_SET *set, uint32_t bit);
6798
 
void internal_clear_bit(REP_SET *set, uint32_t bit);
 
6458
void internal_set_bit(REP_SET *set, uint bit);
 
6459
void internal_clear_bit(REP_SET *set, uint bit);
6799
6460
void or_bits(REP_SET *to,REP_SET *from);
6800
6461
void copy_bits(REP_SET *to,REP_SET *from);
6801
6462
int cmp_bits(REP_SET *set1,REP_SET *set2);
6802
 
int get_next_bit(REP_SET *set,uint32_t lastpos);
 
6463
int get_next_bit(REP_SET *set,uint lastpos);
6803
6464
int find_set(REP_SETS *sets,REP_SET *find);
6804
 
int find_found(FOUND_SET *found_set,uint32_t table_offset,
 
6465
int find_found(FOUND_SET *found_set,uint table_offset,
6805
6466
               int found_offset);
6806
 
uint32_t start_at_word(char * pos);
6807
 
uint32_t end_of_word(char * pos);
6808
 
 
6809
 
static uint32_t found_sets=0;
6810
 
 
6811
 
 
6812
 
static uint32_t replace_len(char * str)
 
6467
uint start_at_word(char * pos);
 
6468
uint end_of_word(char * pos);
 
6469
 
 
6470
static uint found_sets=0;
 
6471
 
 
6472
 
 
6473
static uint replace_len(char * str)
6813
6474
{
6814
 
  uint32_t len=0;
 
6475
  uint len=0;
6815
6476
  while (*str)
6816
6477
  {
6817
6478
    if (str[0] == '\\' && str[1])
6824
6485
 
6825
6486
/* Init a replace structure for further calls */
6826
6487
 
6827
 
REPLACE *init_replace(char * *from, char * *to,uint32_t count,
 
6488
REPLACE *init_replace(char * *from, char * *to,uint count,
6828
6489
                      char * word_end_chars)
6829
6490
{
6830
6491
  static const int SPACE_CHAR= 256;
6831
6492
  static const int START_OF_LINE= 257;
6832
6493
  static const int END_OF_LINE= 258;
6833
6494
 
6834
 
  uint32_t i,j,states,set_nr,len,result_len,max_length,found_end,bits_set,bit_nr;
 
6495
  uint i,j,states,set_nr,len,result_len,max_length,found_end,bits_set,bit_nr;
6835
6496
  int used_sets,chr,default_state;
6836
6497
  char used_chars[LAST_CHAR_CODE],is_word_end[256];
6837
6498
  char * pos, *to_pos, **to_array;
6853
6514
      return(0);
6854
6515
    }
6855
6516
    states+=len+1;
6856
 
    result_len+=(uint32_t) strlen(to[i])+1;
 
6517
    result_len+=(uint) strlen(to[i])+1;
6857
6518
    if (len > max_length)
6858
6519
      max_length=len;
6859
6520
  }
6864
6525
  if (init_sets(&sets,states))
6865
6526
    return(0);
6866
6527
  found_sets=0;
6867
 
  if (!(found_set= (FOUND_SET*) malloc(sizeof(FOUND_SET)*max_length*count)))
6868
 
                                
 
6528
  if (!(found_set= (FOUND_SET*) my_malloc(sizeof(FOUND_SET)*max_length*count,
 
6529
                                          MYF(MY_WME))))
6869
6530
  {
6870
6531
    free_sets(&sets);
6871
6532
    return(0);
6875
6536
  used_sets=-1;
6876
6537
  word_states=make_new_set(&sets);    /* Start of new word */
6877
6538
  start_states=make_new_set(&sets);    /* This is first state */
6878
 
  if (!(follow=(FOLLOWS*) malloc((states+2)*sizeof(FOLLOWS))))
 
6539
  if (!(follow=(FOLLOWS*) my_malloc((states+2)*sizeof(FOLLOWS),MYF(MY_WME))))
6879
6540
  {
6880
6541
    free_sets(&sets);
6881
6542
    free(found_set);
6951
6612
    follow_ptr->table_offset=i;
6952
6613
    follow_ptr->len=len;
6953
6614
    follow_ptr++;
6954
 
    states+=(uint32_t) len+1;
 
6615
    states+=(uint) len+1;
6955
6616
  }
6956
6617
 
6957
6618
 
7067
6728
 
7068
6729
  /* Alloc replace structure for the replace-state-machine */
7069
6730
 
7070
 
  if ((replace=(REPLACE*) malloc(sizeof(REPLACE)*(sets.count)+
7071
 
                                 sizeof(REPLACE_STRING)*(found_sets+1)+
7072
 
                                 sizeof(char *)*count+result_len)))
 
6731
  if ((replace=(REPLACE*) my_malloc(sizeof(REPLACE)*(sets.count)+
 
6732
                                    sizeof(REPLACE_STRING)*(found_sets+1)+
 
6733
                                    sizeof(char *)*count+result_len,
 
6734
                                    MYF(MY_WME | MY_ZEROFILL))))
7073
6735
  {
7074
 
    memset(replace, 0, sizeof(REPLACE)*(sets.count)+
7075
 
                       sizeof(REPLACE_STRING)*(found_sets+1)+
7076
 
                       sizeof(char *)*count+result_len);
7077
6736
    rep_str=(REPLACE_STRING*) (replace+sets.count);
7078
6737
    to_array= (char **) (rep_str+found_sets+1);
7079
6738
    to_pos=(char *) (to_array+count);
7109
6768
}
7110
6769
 
7111
6770
 
7112
 
int init_sets(REP_SETS *sets,uint32_t states)
 
6771
int init_sets(REP_SETS *sets,uint states)
7113
6772
{
7114
6773
  memset(sets, 0, sizeof(*sets));
7115
6774
  sets->size_of_bits=((states+7)/8);
7116
 
  if (!(sets->set_buffer=(REP_SET*) malloc(sizeof(REP_SET)*SET_MALLOC_HUNC)))
 
6775
  if (!(sets->set_buffer=(REP_SET*) my_malloc(sizeof(REP_SET)*SET_MALLOC_HUNC,
 
6776
                                              MYF(MY_WME))))
7117
6777
    return 1;
7118
 
  if (!(sets->bit_buffer=(uint*) malloc(sizeof(uint32_t)*sets->size_of_bits*
7119
 
                                        SET_MALLOC_HUNC)))
 
6778
  if (!(sets->bit_buffer=(uint*) my_malloc(sizeof(uint)*sets->size_of_bits*
 
6779
                                           SET_MALLOC_HUNC,MYF(MY_WME))))
7120
6780
  {
7121
6781
    free(sets->set);
7122
6782
    return 1;
7135
6795
 
7136
6796
REP_SET *make_new_set(REP_SETS *sets)
7137
6797
{
7138
 
  uint32_t i,count,*bit_buffer;
 
6798
  uint i,count,*bit_buffer;
7139
6799
  REP_SET *set;
7140
6800
  if (sets->extra)
7141
6801
  {
7142
6802
    sets->extra--;
7143
6803
    set=sets->set+ sets->count++;
7144
 
    memset(set->bits, 0, sizeof(uint32_t)*sets->size_of_bits);
 
6804
    memset(set->bits, 0, sizeof(uint)*sets->size_of_bits);
7145
6805
    memset(&set->next[0], 0, sizeof(set->next[0])*LAST_CHAR_CODE);
7146
6806
    set->found_offset=0;
7147
6807
    set->found_len=0;
7150
6810
    return set;
7151
6811
  }
7152
6812
  count=sets->count+sets->invisible+SET_MALLOC_HUNC;
7153
 
  if (!(set=(REP_SET*) realloc((unsigned char*) sets->set_buffer,
7154
 
                                  sizeof(REP_SET)*count)))
 
6813
  if (!(set=(REP_SET*) my_realloc((unsigned char*) sets->set_buffer,
 
6814
                                  sizeof(REP_SET)*count,
 
6815
                                  MYF(MY_WME))))
7155
6816
    return 0;
7156
6817
  sets->set_buffer=set;
7157
6818
  sets->set=set+sets->invisible;
7158
 
  if (!(bit_buffer=(uint*) realloc((unsigned char*) sets->bit_buffer,
7159
 
                                   (sizeof(uint32_t)*sets->size_of_bits)*count)))
 
6819
  if (!(bit_buffer=(uint*) my_realloc((unsigned char*) sets->bit_buffer,
 
6820
                                      (sizeof(uint)*sets->size_of_bits)*count,
 
6821
                                      MYF(MY_WME))))
7160
6822
    return 0;
7161
6823
  sets->bit_buffer=bit_buffer;
7162
6824
  for (i=0 ; i < count ; i++)
7182
6844
  return;
7183
6845
}
7184
6846
 
7185
 
void internal_set_bit(REP_SET *set, uint32_t bit)
 
6847
void internal_set_bit(REP_SET *set, uint bit)
7186
6848
{
7187
6849
  set->bits[bit / WORD_BIT] |= 1 << (bit % WORD_BIT);
7188
6850
  return;
7189
6851
}
7190
6852
 
7191
 
void internal_clear_bit(REP_SET *set, uint32_t bit)
 
6853
void internal_clear_bit(REP_SET *set, uint bit)
7192
6854
{
7193
6855
  set->bits[bit / WORD_BIT] &= ~ (1 << (bit % WORD_BIT));
7194
6856
  return;
7197
6859
 
7198
6860
void or_bits(REP_SET *to,REP_SET *from)
7199
6861
{
7200
 
  register uint32_t i;
 
6862
  register uint i;
7201
6863
  for (i=0 ; i < to->size_of_bits ; i++)
7202
6864
    to->bits[i]|=from->bits[i];
7203
6865
  return;
7206
6868
void copy_bits(REP_SET *to,REP_SET *from)
7207
6869
{
7208
6870
  memcpy(to->bits,from->bits,
7209
 
         (size_t) (sizeof(uint32_t) * to->size_of_bits));
 
6871
         (size_t) (sizeof(uint) * to->size_of_bits));
7210
6872
}
7211
6873
 
7212
6874
int cmp_bits(REP_SET *set1,REP_SET *set2)
7213
6875
{
7214
 
  return memcmp(set1->bits,set2->bits, sizeof(uint32_t) * set1->size_of_bits);
 
6876
  return memcmp(set1->bits,set2->bits, sizeof(uint) * set1->size_of_bits);
7215
6877
}
7216
6878
 
7217
6879
 
7218
6880
/* Get next set bit from set. */
7219
6881
 
7220
 
int get_next_bit(REP_SET *set,uint32_t lastpos)
 
6882
int get_next_bit(REP_SET *set,uint lastpos)
7221
6883
{
7222
 
  uint32_t pos,*start,*end,bits;
 
6884
  uint pos,*start,*end,bits;
7223
6885
 
7224
6886
  start=set->bits+ ((lastpos+1) / WORD_BIT);
7225
6887
  end=set->bits + set->size_of_bits;
7229
6891
    bits=start[0];
7230
6892
  if (!bits)
7231
6893
    return 0;
7232
 
  pos=(uint32_t) (start-set->bits)*WORD_BIT;
 
6894
  pos=(uint) (start-set->bits)*WORD_BIT;
7233
6895
  while (! (bits & 1))
7234
6896
  {
7235
6897
    bits>>=1;
7244
6906
 
7245
6907
int find_set(REP_SETS *sets,REP_SET *find)
7246
6908
{
7247
 
  uint32_t i;
 
6909
  uint i;
7248
6910
  for (i=0 ; i < sets->count-1 ; i++)
7249
6911
  {
7250
6912
    if (!cmp_bits(sets->set+i,find))
7263
6925
   set->next[] == -1 is reserved for end without replaces.
7264
6926
*/
7265
6927
 
7266
 
int find_found(FOUND_SET *found_set,uint32_t table_offset, int found_offset)
 
6928
int find_found(FOUND_SET *found_set,uint table_offset, int found_offset)
7267
6929
{
7268
6930
  int i;
7269
 
  for (i=0 ; (uint32_t) i < found_sets ; i++)
 
6931
  for (i=0 ; (uint) i < found_sets ; i++)
7270
6932
    if (found_set[i].table_offset == table_offset &&
7271
6933
        found_set[i].found_offset == found_offset)
7272
6934
      return -i-2;
7278
6940
 
7279
6941
/* Return 1 if regexp starts with \b or ends with \b*/
7280
6942
 
7281
 
uint32_t start_at_word(char * pos)
 
6943
uint start_at_word(char * pos)
7282
6944
{
7283
6945
  return (((!memcmp(pos, "\\b",2) && pos[2]) ||
7284
6946
           !memcmp(pos, "\\^", 2)) ? 1 : 0);
7285
6947
}
7286
6948
 
7287
 
uint32_t end_of_word(char * pos)
 
6949
uint end_of_word(char * pos)
7288
6950
{
7289
6951
  char * end= strchr(pos, '\0');
7290
6952
  return ((end > pos+2 && !memcmp(end-2, "\\b", 2)) ||
7300
6962
 
7301
6963
int insert_pointer_name(POINTER_ARRAY *pa,char * name)
7302
6964
{
7303
 
  uint32_t i,length,old_count;
 
6965
  uint i,length,old_count;
7304
6966
  unsigned char *new_pos;
7305
6967
  const char **new_array;
7306
6968
 
7308
6970
  if (! pa->typelib.count)
7309
6971
  {
7310
6972
    if (!(pa->typelib.type_names=(const char **)
7311
 
          malloc(((PC_MALLOC-MALLOC_OVERHEAD)/
 
6973
          my_malloc(((PC_MALLOC-MALLOC_OVERHEAD)/
7312
6974
                     (sizeof(char *)+sizeof(*pa->flag))*
7313
 
                     (sizeof(char *)+sizeof(*pa->flag))))))
 
6975
                     (sizeof(char *)+sizeof(*pa->flag))),MYF(MY_WME))))
7314
6976
      return(-1);
7315
 
    if (!(pa->str= (unsigned char*) malloc(PS_MALLOC-MALLOC_OVERHEAD)))
 
6977
    if (!(pa->str= (unsigned char*) my_malloc((uint) (PS_MALLOC-MALLOC_OVERHEAD),
 
6978
                                      MYF(MY_WME))))
7316
6979
    {
7317
6980
      free((char*) pa->typelib.type_names);
7318
6981
      return (-1);
7324
6987
    pa->max_length=PS_MALLOC-MALLOC_OVERHEAD;
7325
6988
    pa->array_allocs=1;
7326
6989
  }
7327
 
  length=(uint32_t) strlen(name)+1;
 
6990
  length=(uint) strlen(name)+1;
7328
6991
  if (pa->length+length >= pa->max_length)
7329
6992
  {
7330
 
    if (!(new_pos= (unsigned char*)realloc((unsigned char*)pa->str,
7331
 
                                           (size_t)(pa->max_length+PS_MALLOC))))
 
6993
    if (!(new_pos= (unsigned char*) my_realloc((unsigned char*) pa->str,
 
6994
                                       (uint) (pa->max_length+PS_MALLOC),
 
6995
                                       MYF(MY_WME))))
7332
6996
      return(1);
7333
6997
    if (new_pos != pa->str)
7334
6998
    {
7335
 
      ptrdiff_t diff= PTR_BYTE_DIFF(new_pos,pa->str);
 
6999
      my_ptrdiff_t diff=PTR_BYTE_DIFF(new_pos,pa->str);
7336
7000
      for (i=0 ; i < pa->typelib.count ; i++)
7337
7001
        pa->typelib.type_names[i]= ADD_TO_PTR(pa->typelib.type_names[i],diff,
7338
7002
                                              char*);
7342
7006
  }
7343
7007
  if (pa->typelib.count >= pa->max_count-1)
7344
7008
  {
7345
 
    size_t len;
 
7009
    int len;
7346
7010
    pa->array_allocs++;
7347
7011
    len=(PC_MALLOC*pa->array_allocs - MALLOC_OVERHEAD);
7348
 
    if (!(new_array=
7349
 
         (const char **)realloc((unsigned char*) pa->typelib.type_names,
7350
 
                                 len/
7351
 
                                  (sizeof(unsigned char*)+sizeof(*pa->flag))*
7352
 
                                  (sizeof(unsigned char*)+sizeof(*pa->flag)))))
 
7012
    if (!(new_array=(const char **) my_realloc((unsigned char*) pa->typelib.type_names,
 
7013
                                               (uint) len/
 
7014
                                               (sizeof(unsigned char*)+sizeof(*pa->flag))*
 
7015
                                               (sizeof(unsigned char*)+sizeof(*pa->flag)),
 
7016
                                               MYF(MY_WME))))
7353
7017
      return(1);
7354
7018
    pa->typelib.type_names=new_array;
7355
7019
    old_count=pa->max_count;
7417
7081
  replace_append_mem(ds, val, strlen(val));
7418
7082
}
7419
7083
 
7420
 
/* Append uint32_t to ds, with optional replace */
7421
 
void replace_append_uint(string *ds, uint32_t val)
 
7084
/* Append uint to ds, with optional replace */
 
7085
void replace_append_uint(string *ds, uint val)
7422
7086
{
7423
 
  ostringstream buff;
7424
 
  buff << val;
7425
 
  replace_append_mem(ds, buff.str().c_str(), buff.str().size());
 
7087
  char buff[22]; /* This should be enough for any int */
 
7088
  char *end= int64_t10_to_str(val, buff, 10);
 
7089
  replace_append_mem(ds, buff, end - buff);
7426
7090
 
7427
7091
}
7428
7092