~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzletest.cc

  • Committer: Monty Taylor
  • Date: 2009-03-06 03:33:24 UTC
  • mfrom: (916.1.2 merge)
  • Revision ID: mordred@inaugust.com-20090306033324-dcedf80g9qzywbvu
Merged Brian's merge... re-rotate the tree.

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
47
 
61
48
#include PCRE_HEADER
62
49
 
 
50
#include <mysys/hash.h>
63
51
#include <stdarg.h>
64
 
#include <boost/unordered_map.hpp>
 
52
 
 
53
#include "errname.h"
65
54
 
66
55
/* 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>
 
56
#include <drizzled/gettext.h>
71
57
 
72
 
#ifndef DRIZZLE_RETURN_SERVER_GONE
73
 
#define DRIZZLE_RETURN_HANDSHAKE_FAILED DRIZZLE_RETURN_ERROR_CODE
74
 
#endif
75
 
namespace po= boost::program_options;
76
58
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
59
 
84
60
#define MAX_VAR_NAME_LENGTH    256
85
61
#define MAX_COLUMNS            256
 
62
#define MAX_EMBEDDED_SERVER_ARGS 64
86
63
#define MAX_DELIMITER_LENGTH 16
87
64
/* Flags controlling send and reap */
88
65
#define QUERY_SEND_FLAG  1
89
66
#define QUERY_REAP_FLAG  2
90
67
 
91
 
typedef boost::unordered_map<std::string, uint32_t> ErrorCodes;
92
 
ErrorCodes global_error_names;
93
 
 
94
68
enum {
95
69
  OPT_PS_PROTOCOL, OPT_SP_PROTOCOL, OPT_CURSOR_PROTOCOL, OPT_VIEW_PROTOCOL,
96
70
  OPT_MAX_CONNECT_RETRIES, OPT_MARK_PROGRESS, OPT_LOG_DIR, OPT_TAIL_LINES,
98
72
};
99
73
 
100
74
static int record= 0, opt_sleep= -1;
101
 
static char *opt_pass= NULL;
102
 
const char *unix_sock= NULL;
 
75
static char *opt_db= NULL, *opt_pass= NULL;
 
76
const char *opt_user= NULL, *opt_host= NULL, *unix_sock= NULL,
 
77
           *opt_basedir= "./";
 
78
const char *opt_logdir= "";
 
79
const char *opt_include= NULL, *opt_charsets_dir;
 
80
const char *opt_testdir= NULL;
103
81
static uint32_t opt_port= 0;
104
 
static uint32_t opt_max_connect_retries;
105
 
static bool silent= false, verbose= false;
 
82
static int opt_max_connect_retries;
 
83
static bool opt_compress= false, silent= false, verbose= false;
 
84
static bool debug_info_flag= false, debug_check_flag= false;
 
85
static bool tty_password= false;
106
86
static bool opt_mark_progress= false;
107
87
static bool parsing_disabled= false;
108
88
static bool display_result_vertically= false,
113
93
static bool abort_on_error= true;
114
94
static bool server_initialized= false;
115
95
static bool is_windows= false;
116
 
static bool use_drizzle_protocol= false;
 
96
static char **default_argv;
 
97
static const char *load_default_groups[]= { "drizzletest", "client", 0 };
117
98
static char line_buffer[MAX_DELIMITER_LENGTH], *line_buffer_pos= line_buffer;
118
99
 
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
100
static uint32_t start_lineno= 0; /* Start line of current command */
 
101
static uint32_t my_end_arg= 0;
133
102
 
134
103
/* Number of lines of the result to include in failure report */
135
104
static uint32_t opt_tail_lines= 0;
171
140
 
172
141
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci; /* Default charset */
173
142
 
 
143
static int embedded_server_arg_count=0;
 
144
static char *embedded_server_args[MAX_EMBEDDED_SERVER_ARGS];
 
145
 
174
146
/*
175
147
  Timer related variables
176
148
  See the timer_output() definition for details
198
170
master_pos_st master_pos;
199
171
 
200
172
/* if set, all results are concated and compared against this file */
 
173
const char *result_file_name= NULL;
201
174
 
202
175
typedef struct st_var
203
176
{
215
188
/*Perl/shell-like variable registers */
216
189
VAR var_reg[10];
217
190
 
218
 
 
219
 
boost::unordered_map<string, VAR *> var_hash;
 
191
HASH var_hash;
220
192
 
221
193
struct st_connection
222
194
{
223
 
  drizzle_st *drizzle;
224
 
  drizzle_con_st con;
 
195
  DRIZZLE drizzle;
225
196
  /* Used when creating views and sp, to avoid implicit commit */
226
 
  drizzle_con_st *util_con;
 
197
  DRIZZLE *util_drizzle;
227
198
  char *name;
228
199
};
229
200
struct st_connection connections[128];
380
351
  union
381
352
  {
382
353
    uint32_t errnum;
383
 
    char sqlstate[DRIZZLE_MAX_SQLSTATE_SIZE+1];  /* \0 terminated string */
 
354
    char sqlstate[SQLSTATE_LENGTH+1];  /* \0 terminated string */
384
355
  } code;
385
356
};
386
357
 
396
367
  char *query, *query_buf,*first_argument,*last_argument,*end;
397
368
  int first_word_len, query_len;
398
369
  bool abort_on_error;
399
 
  st_expected_errors expected_errors;
400
 
  string require_file;
 
370
  struct st_expected_errors expected_errors;
 
371
  char require_file[FN_REFLEN];
401
372
  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
373
};
419
374
 
420
375
TYPELIB command_typelib= {array_elements(command_names),"",
438
393
VAR* var_from_env(const char *, const char *);
439
394
VAR* var_init(VAR* v, const char *name, int name_len, const char *val,
440
395
              int val_len);
441
 
void var_free(pair<string, VAR*> v);
 
396
extern "C" void var_free(void* v);
442
397
VAR* var_get(const char *var_name, const char** var_name_end,
443
398
             bool raw, bool ignore_not_existing);
444
399
void eval_expr(VAR* v, const char *p, const char** p_end);
489
444
void handle_no_error(struct st_command*);
490
445
 
491
446
 
 
447
#define do_send_query(cn,q,q_len,flags) drizzleclient_send_query(&cn->drizzle, q, q_len)
 
448
 
492
449
void do_eval(string *query_eval, const char *query,
493
450
             const char *query_end, bool pass_through_escape_chars)
494
451
{
559
516
  options are passed.
560
517
*/
561
518
 
562
 
static void append_os_quoted(string *str, const char *append, ...)
 
519
void append_os_quoted(string *str, const char *append, ...)
563
520
{
564
521
  const char *quote_str= "\'";
565
522
  const uint32_t  quote_len= 1;
602
559
 
603
560
*/
604
561
 
605
 
static void show_query(drizzle_con_st *con, const char* query)
 
562
static void show_query(DRIZZLE *drizzle, const char* query)
606
563
{
607
 
  drizzle_result_st res;
608
 
  drizzle_return_t ret;
609
 
 
610
 
  if (!con)
 
564
  DRIZZLE_RES* res;
 
565
 
 
566
 
 
567
  if (!drizzle)
611
568
    return;
612
569
 
613
 
  if (drizzle_query_str(con, &res, query, &ret) == NULL ||
614
 
      ret != DRIZZLE_RETURN_OK)
 
570
  if (drizzleclient_query(drizzle, query))
615
571
  {
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
 
    }
 
572
    log_msg("Error running query '%s': %d %s",
 
573
            query, drizzleclient_errno(drizzle), drizzleclient_error(drizzle));
628
574
    return;
629
575
  }
630
576
 
631
 
  if (drizzle_result_column_count(&res) == 0 ||
632
 
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
 
577
  if ((res= drizzleclient_store_result(drizzle)) == NULL)
633
578
  {
634
579
    /* No result set returned */
635
 
    drizzle_result_free(&res);
636
580
    return;
637
581
  }
638
582
 
639
583
  {
640
 
    drizzle_row_t row;
 
584
    DRIZZLE_ROW row;
641
585
    unsigned int i;
642
586
    unsigned int row_num= 0;
643
 
    unsigned int num_fields= drizzle_result_column_count(&res);
644
 
    drizzle_column_st *column;
 
587
    unsigned int num_fields= drizzleclient_num_fields(res);
 
588
    const DRIZZLE_FIELD *fields= drizzleclient_fetch_fields(res);
645
589
 
646
590
    fprintf(stderr, "=== %s ===\n", query);
647
 
    while ((row= drizzle_row_next(&res)))
 
591
    while ((row= drizzleclient_fetch_row(res)))
648
592
    {
649
 
      size_t *lengths= drizzle_row_field_sizes(&res);
 
593
      uint32_t *lengths= drizzleclient_fetch_lengths(res);
650
594
      row_num++;
651
595
 
652
596
      fprintf(stderr, "---- %d. ----\n", row_num);
653
 
      drizzle_column_seek(&res, 0);
654
597
      for(i= 0; i < num_fields; i++)
655
598
      {
656
 
        column= drizzle_column_next(&res);
657
599
        fprintf(stderr, "%s\t%.*s\n",
658
 
                drizzle_column_name(column),
 
600
                fields[i].name,
659
601
                (int)lengths[i], row[i] ? row[i] : "NULL");
660
602
      }
661
603
    }
663
605
      fprintf(stderr, "=");
664
606
    fprintf(stderr, "\n\n");
665
607
  }
666
 
  drizzle_result_free(&res);
 
608
  drizzleclient_free_result(res);
667
609
 
668
610
  return;
669
611
}
682
624
 
683
625
*/
684
626
 
685
 
static void show_warnings_before_error(drizzle_con_st *con)
 
627
static void show_warnings_before_error(DRIZZLE *drizzle)
686
628
{
687
 
  drizzle_result_st res;
688
 
  drizzle_return_t ret;
 
629
  DRIZZLE_RES* res;
689
630
  const char* query= "SHOW WARNINGS";
690
631
 
691
 
  if (!con)
 
632
 
 
633
  if (!drizzle)
692
634
    return;
693
635
 
694
 
  if (drizzle_query_str(con, &res, query, &ret) == NULL ||
695
 
      ret != DRIZZLE_RETURN_OK)
 
636
  if (drizzleclient_query(drizzle, query))
696
637
  {
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
 
    }
 
638
    log_msg("Error running query '%s': %d %s",
 
639
            query, drizzleclient_errno(drizzle), drizzleclient_error(drizzle));
709
640
    return;
710
641
  }
711
642
 
712
 
  if (drizzle_result_column_count(&res) == 0 ||
713
 
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
 
643
  if ((res= drizzleclient_store_result(drizzle)) == NULL)
714
644
  {
715
645
    /* No result set returned */
716
 
    drizzle_result_free(&res);
717
646
    return;
718
647
  }
719
648
 
720
 
  if (drizzle_result_row_count(&res) <= 1)
 
649
  if (drizzleclient_num_rows(res) <= 1)
721
650
  {
722
651
    /* Don't display the last row, it's "last error" */
723
652
  }
724
653
  else
725
654
  {
726
 
    drizzle_row_t row;
 
655
    DRIZZLE_ROW row;
727
656
    unsigned int row_num= 0;
728
 
    unsigned int num_fields= drizzle_result_column_count(&res);
 
657
    unsigned int num_fields= drizzleclient_num_fields(res);
729
658
 
730
659
    fprintf(stderr, "\nWarnings from just before the error:\n");
731
 
    while ((row= drizzle_row_next(&res)))
 
660
    while ((row= drizzleclient_fetch_row(res)))
732
661
    {
733
662
      uint32_t i;
734
 
      size_t *lengths= drizzle_row_field_sizes(&res);
 
663
      uint32_t *lengths= drizzleclient_fetch_lengths(res);
735
664
 
736
 
      if (++row_num >= drizzle_result_row_count(&res))
 
665
      if (++row_num >= drizzleclient_num_rows(res))
737
666
      {
738
667
        /* Don't display the last row, it's "last error" */
739
668
        break;
747
676
      fprintf(stderr, "\n");
748
677
    }
749
678
  }
750
 
  drizzle_result_free(&res);
 
679
  drizzleclient_free_result(res);
751
680
 
752
681
  return;
753
682
}
780
709
  for (i= 0; i < num_args; i++)
781
710
  {
782
711
    const struct command_arg *arg= &args[i];
783
 
    arg->ds->clear();
784
712
 
785
 
    bool known_arg_type= true;
786
713
    switch (arg->type) {
787
714
      /* A string */
788
715
    case ARG_STRING:
817
744
      break;
818
745
 
819
746
    default:
820
 
      known_arg_type= false;
 
747
      assert("Unknown argument type");
821
748
      break;
822
749
    }
823
 
    assert(known_arg_type);
824
750
 
825
751
    /* Check required arg */
826
752
    if (arg->ds->length() == 0 && arg->required)
876
802
 
877
803
static void close_connections(void)
878
804
{
 
805
 
879
806
  for (--next_con; next_con >= connections; --next_con)
880
807
  {
881
 
    if (next_con->drizzle != NULL)
882
 
    {
883
 
      drizzle_free(next_con->drizzle);
884
 
      next_con->drizzle= NULL;
885
 
    }
 
808
    drizzleclient_close(&next_con->drizzle);
 
809
    if (next_con->util_drizzle)
 
810
      drizzleclient_close(next_con->util_drizzle);
886
811
    free(next_con->name);
887
812
  }
888
813
  return;
912
837
 
913
838
  close_connections();
914
839
  close_files();
915
 
  for_each(var_hash.begin(), var_hash.end(), var_free);
916
 
  var_hash.clear();
 
840
  hash_free(&var_hash);
917
841
 
918
 
  vector<st_command *>::iterator iter;
 
842
  vector<struct st_command *>::iterator iter;
919
843
  for (iter= q_lines.begin() ; iter < q_lines.end() ; iter++)
920
844
  {
921
845
    struct st_command * q_line= *iter;
922
 
    delete q_line;
 
846
    if (q_line->query_buf != NULL)
 
847
    {
 
848
      free(q_line->query_buf);
 
849
    }
 
850
    free(q_line);
923
851
  }
924
852
 
925
853
  for (i= 0; i < 10; i++)
927
855
    if (var_reg[i].alloced_len)
928
856
      free(var_reg[i].str_val);
929
857
  }
 
858
  while (embedded_server_arg_count > 1)
 
859
    free(embedded_server_args[--embedded_server_arg_count]);
930
860
 
931
861
  free_all_replace();
932
862
  free(opt_pass);
 
863
  free_defaults(default_argv);
933
864
 
934
865
  return;
935
866
}
938
869
static void cleanup_and_exit(int exit_code)
939
870
{
940
871
  free_used_memory();
941
 
  internal::my_end();
 
872
  my_end(my_end_arg);
942
873
 
943
874
  if (!silent) {
944
875
    switch (exit_code) {
1011
942
  }
1012
943
 
1013
944
  /* Dump the result that has been accumulated so far to .log file */
1014
 
  if (! result_file_name.empty() && ds_res.length())
 
945
  if (result_file_name && ds_res.length())
1015
946
    dump_result_to_log_file(ds_res.c_str(), ds_res.length());
1016
947
 
1017
948
  /* Dump warning messages */
1018
 
  if (! result_file_name.empty() && ds_warning_messages.length())
 
949
  if (result_file_name && ds_warning_messages.length())
1019
950
    dump_warning_messages();
1020
951
 
1021
952
  /*
1023
954
    been produced prior to the error
1024
955
  */
1025
956
  if (cur_con)
1026
 
    show_warnings_before_error(&cur_con->con);
 
957
    show_warnings_before_error(&cur_con->drizzle);
1027
958
 
1028
959
  cleanup_and_exit(1);
1029
960
}
1151
1082
  uint32_t len;
1152
1083
  char buff[512];
1153
1084
 
1154
 
  if ((fd= internal::my_open(filename, O_RDONLY, MYF(0))) < 0)
 
1085
  if ((fd= my_open(filename, O_RDONLY, MYF(0))) < 0)
1155
1086
    die("Failed to open file '%s'", filename);
1156
 
  while((len= internal::my_read(fd, (unsigned char*)&buff,
 
1087
  while((len= my_read(fd, (unsigned char*)&buff,
1157
1088
                      sizeof(buff), MYF(0))) > 0)
1158
1089
  {
1159
1090
    char *p= buff, *start= buff;
1175
1106
    /* Output any chars that might be left */
1176
1107
    ds->append(start, p-start);
1177
1108
  }
1178
 
  internal::my_close(fd, MYF(0));
 
1109
  my_close(fd, MYF(0));
1179
1110
}
1180
1111
 
1181
1112
 
1351
1282
 
1352
1283
*/
1353
1284
 
1354
 
static int compare_files2(int fd, const char* filename2)
 
1285
static int compare_files2(File fd, const char* filename2)
1355
1286
{
1356
1287
  int error= RESULT_OK;
1357
 
  int fd2;
 
1288
  File fd2;
1358
1289
  uint32_t len, len2;
1359
1290
  char buff[512], buff2[512];
1360
1291
  const char *fname= filename2;
1361
1292
  string tmpfile;
1362
1293
 
1363
 
  if ((fd2= internal::my_open(fname, O_RDONLY, MYF(0))) < 0)
 
1294
  if ((fd2= my_open(fname, O_RDONLY, MYF(0))) < 0)
1364
1295
  {
1365
 
    internal::my_close(fd, MYF(0));
1366
 
    if (! opt_testdir.empty())
 
1296
    my_close(fd, MYF(0));
 
1297
    if (opt_testdir != NULL)
1367
1298
    {
1368
1299
      tmpfile= opt_testdir;
1369
1300
      if (tmpfile[tmpfile.length()] != '/')
1371
1302
      tmpfile.append(filename2);
1372
1303
      fname= tmpfile.c_str();
1373
1304
    }
1374
 
    if ((fd2= internal::my_open(fname, O_RDONLY, MYF(0))) < 0)
 
1305
    if ((fd2= my_open(fname, O_RDONLY, MYF(0))) < 0)
1375
1306
    {
1376
 
      internal::my_close(fd, MYF(0));
 
1307
      my_close(fd, MYF(0));
1377
1308
    
1378
1309
      die("Failed to open second file: '%s'", fname);
1379
1310
    }
1380
1311
  }
1381
 
  while((len= internal::my_read(fd, (unsigned char*)&buff,
 
1312
  while((len= my_read(fd, (unsigned char*)&buff,
1382
1313
                      sizeof(buff), MYF(0))) > 0)
1383
1314
  {
1384
 
    if ((len2= internal::my_read(fd2, (unsigned char*)&buff2,
 
1315
    if ((len2= my_read(fd2, (unsigned char*)&buff2,
1385
1316
                       sizeof(buff2), MYF(0))) < len)
1386
1317
    {
1387
1318
      /* File 2 was smaller */
1401
1332
      break;
1402
1333
    }
1403
1334
  }
1404
 
  if (!error && internal::my_read(fd2, (unsigned char*)&buff2,
 
1335
  if (!error && my_read(fd2, (unsigned char*)&buff2,
1405
1336
                        sizeof(buff2), MYF(0)) > 0)
1406
1337
  {
1407
1338
    /* File 1 was smaller */
1408
1339
    error= RESULT_LENGTH_MISMATCH;
1409
1340
  }
1410
1341
 
1411
 
  internal::my_close(fd2, MYF(0));
 
1342
  my_close(fd2, MYF(0));
1412
1343
 
1413
1344
  return error;
1414
1345
}
1429
1360
 
1430
1361
static int compare_files(const char* filename1, const char* filename2)
1431
1362
{
1432
 
  int fd;
 
1363
  File fd;
1433
1364
  int error;
1434
1365
 
1435
 
  if ((fd= internal::my_open(filename1, O_RDONLY, MYF(0))) < 0)
 
1366
  if ((fd= my_open(filename1, O_RDONLY, MYF(0))) < 0)
1436
1367
    die("Failed to open first file: '%s'", filename1);
1437
1368
 
1438
1369
  error= compare_files2(fd, filename2);
1439
1370
 
1440
 
  internal::my_close(fd, MYF(0));
 
1371
  my_close(fd, MYF(0));
1441
1372
 
1442
1373
  return error;
1443
1374
}
1458
1389
static int string_cmp(string* ds, const char *fname)
1459
1390
{
1460
1391
  int error;
1461
 
  int fd;
 
1392
  File fd;
1462
1393
  char temp_file_path[FN_REFLEN];
1463
1394
 
1464
 
  if ((fd= internal::create_temp_file(temp_file_path, TMPDIR,
1465
 
                            "tmp", MYF(MY_WME))) < 0)
 
1395
  if ((fd= create_temp_file(temp_file_path, NULL,
 
1396
                            "tmp", O_CREAT | O_RDWR,
 
1397
                            MYF(MY_WME))) < 0)
1466
1398
    die("Failed to create temporary file for ds");
1467
1399
 
1468
1400
  /* Write ds to temporary file and set file pos to beginning*/
1469
 
  if (internal::my_write(fd, (unsigned char *) ds->c_str(), ds->length(),
 
1401
  if (my_write(fd, (unsigned char *) ds->c_str(), ds->length(),
1470
1402
               MYF(MY_FNABP | MY_WME)) ||
1471
1403
      lseek(fd, 0, SEEK_SET) == MY_FILEPOS_ERROR)
1472
1404
  {
1473
 
    internal::my_close(fd, MYF(0));
 
1405
    my_close(fd, MYF(0));
1474
1406
    /* Remove the temporary file */
1475
 
    internal::my_delete(temp_file_path, MYF(0));
 
1407
    my_delete(temp_file_path, MYF(0));
1476
1408
    die("Failed to write file '%s'", temp_file_path);
1477
1409
  }
1478
1410
 
1479
1411
  error= compare_files2(fd, fname);
1480
1412
 
1481
 
  internal::my_close(fd, MYF(0));
 
1413
  my_close(fd, MYF(0));
1482
1414
  /* Remove the temporary file */
1483
 
  internal::my_delete(temp_file_path, MYF(0));
 
1415
  my_delete(temp_file_path, MYF(0));
1484
1416
 
1485
1417
  return(error);
1486
1418
}
1503
1435
  const char* mess= "Result content mismatch\n";
1504
1436
 
1505
1437
 
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())) {
 
1438
  assert(result_file_name);
 
1439
 
 
1440
  if (access(result_file_name, F_OK) != 0)
 
1441
    die("The specified result file does not exist: '%s'", result_file_name);
 
1442
 
 
1443
  switch (string_cmp(ds, result_file_name)) {
1512
1444
  case RESULT_OK:
1513
1445
    break; /* ok */
1514
1446
  case RESULT_LENGTH_MISMATCH:
1522
1454
    */
1523
1455
    char reject_file[FN_REFLEN];
1524
1456
    size_t reject_length;
1525
 
    internal::dirname_part(reject_file, result_file_name.c_str(), &reject_length);
 
1457
    dirname_part(reject_file, result_file_name, &reject_length);
1526
1458
 
1527
1459
    if (access(reject_file, W_OK) == 0)
1528
1460
    {
1529
1461
      /* Result file directory is writable, save reject file there */
1530
 
      internal::fn_format(reject_file, result_file_name.c_str(), NULL,
 
1462
      fn_format(reject_file, result_file_name, NULL,
1531
1463
                ".reject", MY_REPLACE_EXT);
1532
1464
    }
1533
1465
    else
1534
1466
    {
1535
1467
      /* Put reject file in opt_logdir */
1536
 
      internal::fn_format(reject_file, result_file_name.c_str(), opt_logdir.c_str(),
 
1468
      fn_format(reject_file, result_file_name, opt_logdir,
1537
1469
                ".reject", MY_REPLACE_DIR | MY_REPLACE_EXT);
1538
1470
    }
1539
1471
    str_to_file(reject_file, ds->c_str(), ds->length());
1540
1472
 
1541
1473
    ds->erase(); /* Don't create a .log file */
1542
1474
 
1543
 
    show_diff(NULL, result_file_name.c_str(), reject_file);
 
1475
    show_diff(NULL, result_file_name, reject_file);
1544
1476
    die("%s",mess);
1545
1477
    break;
1546
1478
  }
1567
1499
 
1568
1500
*/
1569
1501
 
1570
 
static void check_require(string* ds, const string &fname)
 
1502
static void check_require(string* ds, const char *fname)
1571
1503
{
1572
1504
 
1573
1505
 
1574
 
  if (string_cmp(ds, fname.c_str()))
 
1506
  if (string_cmp(ds, fname))
1575
1507
  {
1576
1508
    char reason[FN_REFLEN];
1577
 
    internal::fn_format(reason, fname.c_str(), "", "", MY_REPLACE_EXT | MY_REPLACE_DIR);
 
1509
    fn_format(reason, fname, "", "", MY_REPLACE_EXT | MY_REPLACE_DIR);
1578
1510
    abort_not_supported_test("Test requires: '%s'", reason);
1579
1511
  }
1580
1512
  return;
1625
1557
}
1626
1558
 
1627
1559
 
 
1560
extern "C"
 
1561
unsigned char *get_var_key(const unsigned char* var, size_t *len, bool)
 
1562
{
 
1563
  register char* key;
 
1564
  key = ((VAR*)var)->name;
 
1565
  *len = ((VAR*)var)->name_len;
 
1566
  return (unsigned char*)key;
 
1567
}
 
1568
 
1628
1569
 
1629
1570
VAR *var_init(VAR *v, const char *name, int name_len, const char *val,
1630
1571
              int val_len)
1662
1603
}
1663
1604
 
1664
1605
 
1665
 
void var_free(pair<string, VAR *> v)
 
1606
void var_free(void *v)
1666
1607
{
1667
 
  free(v.second->str_val);
1668
 
  free(v.second->env_s);
1669
 
  if (v.second->alloced)
1670
 
    free(v.second);
 
1608
  free(((VAR*) v)->str_val);
 
1609
  free(((VAR*) v)->env_s);
 
1610
  if (((VAR*)v)->alloced)
 
1611
    free(v);
1671
1612
}
1672
1613
 
1673
1614
 
1679
1620
    tmp = def_val;
1680
1621
 
1681
1622
  v = var_init(0, name, strlen(name), tmp, strlen(tmp));
1682
 
  string var_name(name);
1683
 
  var_hash.insert(make_pair(var_name, v));
 
1623
  my_hash_insert(&var_hash, (unsigned char*)v);
1684
1624
  return v;
1685
1625
}
1686
1626
 
1711
1651
    if (length >= MAX_VAR_NAME_LENGTH)
1712
1652
      die("Too long variable name: %s", save_var_name);
1713
1653
 
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())
 
1654
    if (!(v = (VAR*) hash_search(&var_hash, (const unsigned char*) save_var_name,
 
1655
                                 length)))
1718
1656
    {
1719
1657
      char buff[MAX_VAR_NAME_LENGTH+1];
1720
1658
      strncpy(buff, save_var_name, length);
1721
1659
      buff[length]= '\0';
1722
1660
      v= var_from_env(buff, "");
1723
1661
    }
1724
 
    else
1725
 
    {
1726
 
      v= (*iter).second;
1727
 
    }
1728
1662
    var_name--;  /* Point at last character */
1729
1663
  }
1730
1664
  else
1749
1683
 
1750
1684
static VAR *var_obtain(const char *name, int len)
1751
1685
{
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));
 
1686
  VAR* v;
 
1687
  if ((v = (VAR*)hash_search(&var_hash, (const unsigned char *) name, len)))
 
1688
    return v;
 
1689
  v = var_init(0, name, len, "", 0);
 
1690
  my_hash_insert(&var_hash, (unsigned char*)v);
1759
1691
  return v;
1760
1692
}
1761
1693
 
1838
1770
  of the currently connected server
1839
1771
*/
1840
1772
 
1841
 
static void var_set_drizzleclient_get_server_version(drizzle_con_st *con)
 
1773
static void var_set_drizzleclient_get_server_version(DRIZZLE *drizzle)
1842
1774
{
1843
 
  var_set_int("$drizzle_con_server_version", drizzle_con_server_version_number(con));
 
1775
  var_set_int("$drizzleclient_get_server_version", drizzleclient_get_server_version(drizzle));
1844
1776
}
1845
1777
 
1846
1778
 
1871
1803
{
1872
1804
  const char *end = (char*)((query_end && *query_end) ?
1873
1805
                            *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;
 
1806
  DRIZZLE_RES *res;
 
1807
  DRIZZLE_ROW row;
 
1808
  DRIZZLE *drizzle= &cur_con->drizzle;
1878
1809
  string ds_query;
1879
1810
 
1880
1811
 
1887
1818
  /* Eval the query, thus replacing all environment variables */
1888
1819
  do_eval(&ds_query, query, end, false);
1889
1820
 
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)
 
1821
  if (drizzleclient_real_query(drizzle, ds_query.c_str(), ds_query.length()))
 
1822
    die("Error running query '%s': %d %s", ds_query.c_str(),
 
1823
        drizzleclient_errno(drizzle), drizzleclient_error(drizzle));
 
1824
  if (!(res= drizzleclient_store_result(drizzle)))
1908
1825
    die("Query '%s' didn't return a result set", ds_query.c_str());
1909
1826
 
1910
 
  if ((row= drizzle_row_next(&res)) && row[0])
 
1827
  if ((row= drizzleclient_fetch_row(res)) && row[0])
1911
1828
  {
1912
1829
    /*
1913
1830
      Concatenate all fields in the first row with tab in between
1915
1832
    */
1916
1833
    string result;
1917
1834
    uint32_t i;
1918
 
    size_t *lengths;
 
1835
    uint32_t *lengths;
1919
1836
 
1920
 
    lengths= drizzle_row_field_sizes(&res);
1921
 
    for (i= 0; i < drizzle_result_column_count(&res); i++)
 
1837
    lengths= drizzleclient_fetch_lengths(res);
 
1838
    for (i= 0; i < drizzleclient_num_fields(res); i++)
1922
1839
    {
1923
1840
      if (row[i])
1924
1841
      {
1933
1850
  else
1934
1851
    eval_expr(var, "", 0);
1935
1852
 
1936
 
  drizzle_result_free(&res);
 
1853
  drizzleclient_free_result(res);
1937
1854
  return;
1938
1855
}
1939
1856
 
1964
1881
{
1965
1882
  long row_no;
1966
1883
  int col_no= -1;
1967
 
  drizzle_result_st res;
1968
 
  drizzle_return_t ret;
1969
 
  drizzle_con_st *con= &cur_con->con;
 
1884
  DRIZZLE_RES* res;
 
1885
  DRIZZLE *drizzle= &cur_con->drizzle;
1970
1886
 
1971
1887
  string ds_query;
1972
1888
  string ds_col;
1985
1901
                     ',');
1986
1902
 
1987
1903
  /* 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())
 
1904
  if (!str2int(ds_row.c_str(), 10, (long) 0, (long) INT_MAX, &row_no))
1992
1905
    die("Invalid row number: '%s'", ds_row.c_str());
1993
1906
 
1994
1907
  /* Remove any surrounding "'s from the query - if there is any */
2000
1913
  ds_query.append(unstripped_query);
2001
1914
 
2002
1915
  /* 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)
 
1916
  if (drizzleclient_real_query(drizzle, ds_query.c_str(), ds_query.length()))
 
1917
    die("Error running query '%s': %d %s", ds_query.c_str(),
 
1918
        drizzleclient_errno(drizzle), drizzleclient_error(drizzle));
 
1919
  if (!(res= drizzleclient_store_result(drizzle)))
2021
1920
    die("Query '%s' didn't return a result set", ds_query.c_str());
2022
1921
 
2023
1922
  {
2024
1923
    /* Find column number from the given column name */
2025
1924
    uint32_t i;
2026
 
    uint32_t num_fields= drizzle_result_column_count(&res);
2027
 
    drizzle_column_st *column;
 
1925
    uint32_t num_fields= drizzleclient_num_fields(res);
 
1926
    const DRIZZLE_FIELD *fields= drizzleclient_fetch_fields(res);
2028
1927
 
2029
1928
    for (i= 0; i < num_fields; i++)
2030
1929
    {
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())
 
1930
      if (strcmp(fields[i].name, ds_col.c_str()) == 0 &&
 
1931
          strlen(fields[i].name) == ds_col.length())
2034
1932
      {
2035
1933
        col_no= i;
2036
1934
        break;
2038
1936
    }
2039
1937
    if (col_no == -1)
2040
1938
    {
2041
 
      drizzle_result_free(&res);
 
1939
      drizzleclient_free_result(res);
2042
1940
      die("Could not find column '%s' in the result of '%s'",
2043
1941
          ds_col.c_str(), ds_query.c_str());
2044
1942
    }
2046
1944
 
2047
1945
  {
2048
1946
    /* Get the value */
2049
 
    drizzle_row_t row;
 
1947
    DRIZZLE_ROW row;
2050
1948
    long rows= 0;
2051
1949
    const char* value= "No such row";
2052
1950
 
2053
 
    while ((row= drizzle_row_next(&res)))
 
1951
    while ((row= drizzleclient_fetch_row(res)))
2054
1952
    {
2055
1953
      if (++rows == row_no)
2056
1954
      {
2066
1964
    }
2067
1965
    eval_expr(var, value, 0);
2068
1966
  }
2069
 
  drizzle_result_free(&res);
 
1967
  drizzleclient_free_result(res);
2070
1968
 
2071
1969
  return;
2072
1970
}
2117
2015
    const size_t len= strlen(get_value_str);
2118
2016
    if (strncmp(p, get_value_str, len)==0)
2119
2017
    {
2120
 
      st_command command;
 
2018
      struct st_command command;
 
2019
      memset(&command, 0, sizeof(command));
2121
2020
      command.query= (char*)p;
2122
2021
      command.first_word_len= len;
2123
2022
      command.first_argument= command.query + len;
2154
2053
{
2155
2054
  char buff[FN_REFLEN];
2156
2055
 
2157
 
  if (!internal::test_if_hard_path(name))
 
2056
  if (!test_if_hard_path(name))
2158
2057
  {
2159
 
    snprintf(buff, sizeof(buff), "%s%s",opt_basedir.c_str(),name);
 
2058
    sprintf(buff,"%s%s",opt_basedir,name);
2160
2059
    name=buff;
2161
2060
  }
2162
 
  internal::fn_format(buff, name, "", "", MY_UNPACK_FILENAME);
 
2061
  fn_format(buff, name, "", "", MY_UNPACK_FILENAME);
2163
2062
 
2164
2063
  if (cur_file == file_stack_end)
2165
2064
    die("Source directives are nesting too deep");
2210
2109
    ; /* Do nothing */
2211
2110
  else
2212
2111
  {
2213
 
    if (! opt_testdir.empty())
 
2112
    if (opt_testdir != NULL)
2214
2113
    {
2215
2114
      string testdir(opt_testdir);
2216
2115
      if (testdir[testdir.length()] != '/')
2478
2377
                     rm_args, sizeof(rm_args)/sizeof(struct command_arg),
2479
2378
                     ' ');
2480
2379
 
2481
 
  error= internal::my_delete(ds_filename.c_str(), MYF(0)) != 0;
 
2380
  error= my_delete(ds_filename.c_str(), MYF(0)) != 0;
2482
2381
  handle_command_error(command, error);
2483
2382
  return;
2484
2383
}
2512
2411
                     sizeof(copy_file_args)/sizeof(struct command_arg),
2513
2412
                     ' ');
2514
2413
 
2515
 
  error= (internal::my_copy(ds_from_file.c_str(), ds_to_file.c_str(),
 
2414
  error= (my_copy(ds_from_file.c_str(), ds_to_file.c_str(),
2516
2415
                  MYF(MY_DONT_OVERWRITE_FILE)) != 0);
2517
2416
  handle_command_error(command, error);
2518
2417
  return;
2547
2446
                     ' ');
2548
2447
 
2549
2448
  /* Parse what mode to set */
2550
 
  istringstream buff(ds_mode);
2551
2449
  if (ds_mode.length() != 4 ||
2552
 
      (buff >> oct >> mode).fail())
 
2450
      str2int(ds_mode.c_str(), 8, 0, INT_MAX, &mode) == NULL)
2553
2451
    die("You must write a 4 digit octal number for mode");
2554
2452
 
2555
2453
  handle_command_error(command, chmod(ds_file.c_str(), mode));
2610
2508
                     mkdir_args, sizeof(mkdir_args)/sizeof(struct command_arg),
2611
2509
                     ' ');
2612
2510
 
2613
 
  error= mkdir(ds_dirname.c_str(), (0777 & internal::my_umask_dir)) != 0;
 
2511
  error= mkdir(ds_dirname.c_str(), (0777 & my_umask_dir)) != 0;
2614
2512
  handle_command_error(command, error);
2615
2513
  return;
2616
2514
}
2911
2809
{
2912
2810
  char *p= command->first_argument, *name;
2913
2811
  struct st_connection *con;
2914
 
  drizzle_result_st result;
2915
 
  drizzle_return_t ret;
2916
2812
 
2917
2813
  if (!*p)
2918
2814
    die("Missing connection name in send_quit");
2927
2823
  if (!(con= find_connection_by_name(name)))
2928
2824
    die("connection '%s' not found in connection pool", name);
2929
2825
 
2930
 
  if (drizzle_quit(&con->con,&result, &ret))
2931
 
    drizzle_result_free(&result);
 
2826
  simple_command(&con->drizzle,COM_QUIT,0,0,1);
2932
2827
 
2933
2828
  return;
2934
2829
}
2950
2845
 
2951
2846
*/
2952
2847
 
2953
 
static void do_change_user(struct st_command *)
 
2848
static void do_change_user(struct st_command *command)
2954
2849
{
2955
 
  assert(0);
 
2850
  DRIZZLE *drizzle= &cur_con->drizzle;
 
2851
  /* static keyword to make the NetWare compiler happy. */
 
2852
  string ds_user, ds_passwd, ds_db;
 
2853
  const struct command_arg change_user_args[] = {
 
2854
    { "user", ARG_STRING, false, &ds_user, "User to connect as" },
 
2855
    { "password", ARG_STRING, false, &ds_passwd, "Password used when connecting" },
 
2856
    { "database", ARG_STRING, false, &ds_db, "Database to select after connect" },
 
2857
  };
 
2858
 
 
2859
 
 
2860
 
 
2861
  check_command_args(command, command->first_argument,
 
2862
                     change_user_args,
 
2863
                     sizeof(change_user_args)/sizeof(struct command_arg),
 
2864
                     ',');
 
2865
 
 
2866
  if (!ds_user.length())
 
2867
    ds_user.append(drizzle->user);
 
2868
 
 
2869
  if (!ds_passwd.length())
 
2870
    ds_passwd.append(drizzle->passwd);
 
2871
 
 
2872
  if (!ds_db.length())
 
2873
    ds_db.append(drizzle->db);
 
2874
 
 
2875
  if (drizzleclient_change_user(drizzle, ds_user.c_str(),
 
2876
                          ds_passwd.c_str(), ds_db.c_str()))
 
2877
    die("change user failed: %s", drizzleclient_error(drizzle));
 
2878
 
 
2879
 
 
2880
  return;
2956
2881
}
2957
2882
 
 
2883
 
2958
2884
/*
2959
2885
  SYNOPSIS
2960
2886
  do_perl
2977
2903
static void do_perl(struct st_command *command)
2978
2904
{
2979
2905
  int error;
2980
 
  int fd;
 
2906
  File fd;
2981
2907
  FILE *res_file;
2982
2908
  char buf[FN_REFLEN];
2983
2909
  char temp_file_path[FN_REFLEN];
3001
2927
  read_until_delimiter(&ds_script, &ds_delimiter);
3002
2928
 
3003
2929
  /* Create temporary file name */
3004
 
  if ((fd= internal::create_temp_file(temp_file_path, getenv("MYSQLTEST_VARDIR"),
3005
 
                            "tmp", MYF(MY_WME))) < 0)
 
2930
  if ((fd= create_temp_file(temp_file_path, getenv("MYSQLTEST_VARDIR"),
 
2931
                            "tmp", O_CREAT | O_RDWR,
 
2932
                            MYF(MY_WME))) < 0)
3006
2933
    die("Failed to create temporary file for perl command");
3007
 
  internal::my_close(fd, MYF(0));
 
2934
  my_close(fd, MYF(0));
3008
2935
 
3009
2936
  str_to_file(temp_file_path, ds_script.c_str(), ds_script.length());
3010
2937
 
3024
2951
  error= pclose(res_file);
3025
2952
 
3026
2953
  /* Remove the temporary file */
3027
 
  internal::my_delete(temp_file_path, MYF(0));
 
2954
  my_delete(temp_file_path, MYF(0));
3028
2955
 
3029
2956
  handle_command_error(command, WEXITSTATUS(error));
3030
2957
  return;
3072
2999
do_wait_for_slave_to_stop(struct st_command *)
3073
3000
{
3074
3001
  static int SLAVE_POLL_INTERVAL= 300000;
3075
 
  drizzle_con_st *con= &cur_con->con;
 
3002
  DRIZZLE *drizzle= &cur_con->drizzle;
3076
3003
  for (;;)
3077
3004
  {
3078
 
    drizzle_result_st res;
3079
 
    drizzle_return_t ret;
3080
 
    drizzle_row_t row;
 
3005
    DRIZZLE_RES *res= NULL;
 
3006
    DRIZZLE_ROW row;
3081
3007
    int done;
3082
3008
 
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
 
    {
 
3009
    if (drizzleclient_query(drizzle,"show status like 'Slave_running'") ||
 
3010
        !(res=drizzleclient_store_result(drizzle)))
3102
3011
      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])
 
3012
          drizzleclient_error(drizzle));
 
3013
    if (!(row=drizzleclient_fetch_row(res)) || !row[1])
3107
3014
    {
3108
 
      drizzle_result_free(&res);
 
3015
      drizzleclient_free_result(res);
3109
3016
      die("Strange result from query while probing slave for stop");
3110
3017
    }
3111
3018
    done = !strcmp(row[1],"OFF");
3112
 
    drizzle_result_free(&res);
 
3019
    drizzleclient_free_result(res);
3113
3020
    if (done)
3114
3021
      break;
3115
3022
    usleep(SLAVE_POLL_INTERVAL);
3120
3027
 
3121
3028
static void do_sync_with_master2(long offset)
3122
3029
{
3123
 
  drizzle_result_st res;
3124
 
  drizzle_return_t ret;
3125
 
  drizzle_row_t row;
3126
 
  drizzle_con_st *con= &cur_con->con;
 
3030
  DRIZZLE_RES *res;
 
3031
  DRIZZLE_ROW row;
 
3032
  DRIZZLE *drizzle= &cur_con->drizzle;
3127
3033
  char query_buf[FN_REFLEN+128];
3128
3034
  int tries= 0;
3129
3035
 
3130
3036
  if (!master_pos.file[0])
3131
3037
    die("Calling 'sync_with_master' without calling 'save_master_pos'");
3132
3038
 
3133
 
  snprintf(query_buf, sizeof(query_buf), "select master_pos_wait('%s', %ld)", master_pos.file,
 
3039
  sprintf(query_buf, "select master_pos_wait('%s', %ld)", master_pos.file,
3134
3040
          master_pos.pos + offset);
3135
3041
 
3136
3042
wait_for_position:
3137
3043
 
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);
 
3044
  if (drizzleclient_query(drizzle, query_buf))
 
3045
    die("failed in '%s': %d: %s", query_buf, drizzleclient_errno(drizzle),
 
3046
        drizzleclient_error(drizzle));
 
3047
 
 
3048
  if (!(res= drizzleclient_store_result(drizzle)))
 
3049
    die("drizzleclient_store_result() returned NULL for '%s'", query_buf);
 
3050
  if (!(row= drizzleclient_fetch_row(res)))
 
3051
  {
 
3052
    drizzleclient_free_result(res);
3158
3053
    die("empty result in %s", query_buf);
3159
3054
  }
3160
3055
  if (!row[0])
3163
3058
      It may be that the slave SQL thread has not started yet, though START
3164
3059
      SLAVE has been issued ?
3165
3060
    */
3166
 
    drizzle_result_free(&res);
 
3061
    drizzleclient_free_result(res);
3167
3062
    if (tries++ == 30)
3168
3063
    {
3169
 
      show_query(con, "SHOW MASTER STATUS");
3170
 
      show_query(con, "SHOW SLAVE STATUS");
 
3064
      show_query(drizzle, "SHOW MASTER STATUS");
 
3065
      show_query(drizzle, "SHOW SLAVE STATUS");
3171
3066
      die("could not sync with master ('%s' returned NULL)", query_buf);
3172
3067
    }
3173
3068
    sleep(1); /* So at most we will wait 30 seconds and make 31 tries */
3174
3069
    goto wait_for_position;
3175
3070
  }
3176
 
  drizzle_result_free(&res);
 
3071
  drizzleclient_free_result(res);
3177
3072
  return;
3178
3073
}
3179
3074
 
3203
3098
*/
3204
3099
static int do_save_master_pos(void)
3205
3100
{
3206
 
  drizzle_result_st res;
3207
 
  drizzle_return_t ret;
3208
 
  drizzle_row_t row;
3209
 
  drizzle_con_st *con= &cur_con->con;
 
3101
  DRIZZLE_RES *res;
 
3102
  DRIZZLE_ROW row;
 
3103
  DRIZZLE *drizzle= &cur_con->drizzle;
3210
3104
  const char *query;
3211
3105
 
3212
3106
 
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
 
  }
 
3107
  if (drizzleclient_query(drizzle, query= "show master status"))
 
3108
    die("failed in 'show master status': %d %s",
 
3109
        drizzleclient_errno(drizzle), drizzleclient_error(drizzle));
3225
3110
 
3226
 
  if (drizzle_result_column_count(&res) == 0 ||
3227
 
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
 
3111
  if (!(res = drizzleclient_store_result(drizzle)))
3228
3112
    die("drizzleclient_store_result() retuned NULL for '%s'", query);
3229
 
  if (!(row = drizzle_row_next(&res)))
 
3113
  if (!(row = drizzleclient_fetch_row(res)))
3230
3114
    die("empty result in show master status");
3231
3115
  strncpy(master_pos.file, row[0], sizeof(master_pos.file)-1);
3232
3116
  master_pos.pos = strtoul(row[1], (char**) 0, 10);
3233
 
  drizzle_result_free(&res);
 
3117
  drizzleclient_free_result(res);
3234
3118
  return(0);
3235
3119
}
3236
3120
 
3314
3198
 
3315
3199
static int do_sleep(struct st_command *command, bool real_sleep)
3316
3200
{
3317
 
  bool error= false;
 
3201
  int error= 0;
3318
3202
  char *p= command->first_argument;
3319
3203
  char *sleep_start, *sleep_end= command->end;
3320
 
  double sleep_val= 0;
 
3204
  double sleep_val;
3321
3205
 
3322
3206
  while (my_isspace(charset_info, *p))
3323
3207
    p++;
3324
3208
  if (!*p)
3325
3209
    die("Missing argument to %.*s", command->first_word_len, command->query);
3326
3210
  sleep_start= p;
3327
 
  /* Check that arg starts with a digit, not handled by internal::my_strtod */
 
3211
  /* Check that arg starts with a digit, not handled by my_strtod */
3328
3212
  if (!my_isdigit(charset_info, *sleep_start))
3329
3213
    die("Invalid argument to %.*s \"%s\"", command->first_word_len,
3330
3214
        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();
 
3215
  sleep_val= my_strtod(sleep_start, &sleep_end, &error);
3334
3216
  if (error)
3335
3217
    die("Invalid argument to %.*s \"%s\"", command->first_word_len,
3336
3218
        command->query, command->first_argument);
3346
3228
}
3347
3229
 
3348
3230
 
3349
 
static void do_get_file_name(struct st_command *command, string &dest)
 
3231
static void do_get_file_name(struct st_command *command,
 
3232
                             char* dest, uint32_t dest_max_len)
3350
3233
{
3351
3234
  char *p= command->first_argument, *name;
3352
3235
  if (!*p)
3357
3240
  if (*p)
3358
3241
    *p++= 0;
3359
3242
  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);
 
3243
  strncpy(dest, name, dest_max_len - 1);
3367
3244
}
3368
3245
 
3369
3246
 
3386
3263
    abort_not_supported_test("Test requires charset '%s'", charset_name);
3387
3264
}
3388
3265
 
3389
 
static void fill_global_error_names()
3390
 
{
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])
3423
 
  {
3424
 
    /*
3425
 
      Concatenate all fields in the first row with tab in between
3426
 
      and assign that string to the $variable
3427
 
    */
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
3266
static uint32_t get_errcode_from_name(char *error_name, char *error_end)
3449
3267
{
3450
 
  size_t err_name_len= error_end - error_name;
3451
 
  string error_name_s(error_name, err_name_len);
 
3268
  /* SQL error as string */
 
3269
  st_error *e= global_error_names;
3452
3270
 
3453
 
  ErrorCodes::iterator it= global_error_names.find(error_name_s);
3454
 
  if (it != global_error_names.end())
 
3271
  /* Loop through the array of known error names */
 
3272
  for (; e->name; e++)
3455
3273
  {
3456
 
    return (*it).second;
 
3274
    /*
 
3275
      If we get a match, we need to check the length of the name we
 
3276
      matched against in case it was longer than what we are checking
 
3277
      (as in ER_WRONG_VALUE vs. ER_WRONG_VALUE_COUNT).
 
3278
    */
 
3279
    if (!strncmp(error_name, e->name, (int) (error_end - error_name)) &&
 
3280
        (uint32_t) strlen(e->name) == (uint32_t) (error_end - error_name))
 
3281
    {
 
3282
      return(e->code);
 
3283
    }
3457
3284
  }
3458
 
 
3459
 
  die("Unknown SQL error name '%s'", error_name_s.c_str());
3460
 
  return 0;
 
3285
  if (!e->name)
 
3286
    die("Unknown SQL error name '%s'", error_name);
 
3287
  return(0);
3461
3288
}
3462
3289
 
3463
3290
static void do_get_errcodes(struct st_command *command)
3490
3317
 
3491
3318
      /*
3492
3319
        SQLSTATE string
3493
 
        - Must be DRIZZLE_MAX_SQLSTATE_SIZE long
 
3320
        - Must be SQLSTATE_LENGTH long
3494
3321
        - May contain only digits[0-9] and _uppercase_ letters
3495
3322
      */
3496
3323
      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);
 
3324
      if ((end - p) != SQLSTATE_LENGTH)
 
3325
        die("The sqlstate must be exactly %d chars long", SQLSTATE_LENGTH);
3499
3326
 
3500
3327
      /* Check sqlstate string validity */
3501
3328
      while (*p && p < end)
3540
3367
      }
3541
3368
 
3542
3369
      /* Convert the sting to int */
3543
 
      istringstream buff(start);
3544
 
      if ((buff >> val).fail())
 
3370
      if (!str2int(start, 10, (long) INT_MIN, (long) INT_MAX, &val))
3545
3371
        die("Invalid argument to error: '%s'", command->first_argument);
3546
3372
 
3547
3373
      to->code.errnum= (uint32_t) val;
3651
3477
}
3652
3478
 
3653
3479
 
3654
 
static void set_reconnect(drizzle_con_st *con, int val)
 
3480
static void set_reconnect(DRIZZLE *drizzle, int val)
3655
3481
{
3656
 
  (void) con;
3657
 
  (void) val;
3658
 
/* XXX
3659
3482
  bool reconnect= val;
3660
3483
 
3661
3484
  drizzleclient_options(drizzle, DRIZZLE_OPT_RECONNECT, (char *)&reconnect);
3662
 
*/
 
3485
 
 
3486
  return;
3663
3487
}
3664
3488
 
3665
3489
 
3669
3493
    die("connection '%s' not found in connection pool", name);
3670
3494
 
3671
3495
  /* Update $drizzleclient_get_server_version to that of current connection */
3672
 
  var_set_drizzleclient_get_server_version(&cur_con->con);
 
3496
  var_set_drizzleclient_get_server_version(&cur_con->drizzle);
3673
3497
 
3674
3498
  return(0);
3675
3499
}
3711
3535
  if (!(con= find_connection_by_name(name)))
3712
3536
    die("connection '%s' not found in connection pool", name);
3713
3537
 
3714
 
  if (con->drizzle != NULL)
 
3538
  if (command->type == Q_DIRTY_CLOSE)
3715
3539
  {
3716
 
    drizzle_free(con->drizzle);
3717
 
    con->drizzle= NULL;
 
3540
    if (con->drizzle.net.vio)
 
3541
    {
 
3542
      drizzleclient_net_close(&(con->drizzle.net));
 
3543
    }
3718
3544
  }
 
3545
 
 
3546
  drizzleclient_close(&con->drizzle);
 
3547
 
 
3548
  if (con->util_drizzle)
 
3549
    drizzleclient_close(con->util_drizzle);
 
3550
  con->util_drizzle= 0;
 
3551
 
3719
3552
  free(con->name);
3720
3553
 
3721
3554
  /*
3754
3587
 
3755
3588
*/
3756
3589
 
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)
 
3590
static void safe_connect(DRIZZLE *drizzle, const char *name, const char *host,
 
3591
                         const char *user, const char *pass, const char *db,
 
3592
                         int port)
3760
3593
{
3761
 
  uint32_t failed_attempts= 0;
 
3594
  int failed_attempts= 0;
3762
3595
  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)
 
3596
 
 
3597
 
 
3598
  while(!drizzleclient_connect(drizzle, host, user, pass, db, port, NULL,
 
3599
                         CLIENT_MULTI_STATEMENTS | CLIENT_REMEMBER_OPTIONS))
3769
3600
  {
3770
3601
    /*
3771
3602
      Connect failed
3775
3606
      on protocol/connection type
3776
3607
    */
3777
3608
 
3778
 
    if ((ret == DRIZZLE_RETURN_GETADDRINFO ||
3779
 
         ret == DRIZZLE_RETURN_COULD_NOT_CONNECT) &&
 
3609
    if ((drizzleclient_errno(drizzle) == CR_CONN_HOST_ERROR ||
 
3610
         drizzleclient_errno(drizzle) == CR_CONNECTION_ERROR) &&
3780
3611
        failed_attempts < opt_max_connect_retries)
3781
3612
    {
3782
3613
      verbose_msg("Connect attempt %d/%d failed: %d: %s", failed_attempts,
3783
 
                  opt_max_connect_retries, ret, drizzle_con_error(con));
 
3614
                  opt_max_connect_retries, drizzleclient_errno(drizzle),
 
3615
                  drizzleclient_error(drizzle));
3784
3616
      usleep(connection_retry_sleep);
3785
3617
    }
3786
3618
    else
3787
3619
    {
3788
3620
      if (failed_attempts > 0)
3789
3621
        die("Could not open connection '%s' after %d attempts: %d %s", name,
3790
 
            failed_attempts, ret, drizzle_con_error(con));
 
3622
            failed_attempts, drizzleclient_errno(drizzle), drizzleclient_error(drizzle));
3791
3623
      else
3792
 
        die("Could not open connection '%s': %d %s", name, ret,
3793
 
            drizzle_con_error(con));
 
3624
        die("Could not open connection '%s': %d %s", name,
 
3625
            drizzleclient_errno(drizzle), drizzleclient_error(drizzle));
3794
3626
    }
3795
3627
    failed_attempts++;
3796
3628
  }
3822
3654
*/
3823
3655
 
3824
3656
static int connect_n_handle_errors(struct st_command *command,
3825
 
                                   drizzle_con_st *con, const char* host,
 
3657
                                   DRIZZLE *con, const char* host,
3826
3658
                                   const char* user, const char* pass,
3827
3659
                                   const char* db, int port, const char* sock)
3828
3660
{
3829
 
  drizzle_return_t ret;
3830
3661
 
3831
3662
  /* Only log if an error is expected */
3832
3663
  if (!command->abort_on_error &&
3853
3684
    ds_res.append(delimiter);
3854
3685
    ds_res.append("\n");
3855
3686
  }
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)
 
3687
  if (!drizzleclient_connect(con, host, user, pass, db, port, 0,
 
3688
                       CLIENT_MULTI_STATEMENTS))
3860
3689
  {
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
 
 
 
3690
    var_set_errno(drizzleclient_errno(con));
 
3691
    handle_error(command, drizzleclient_errno(con), drizzleclient_error(con),
 
3692
                 drizzleclient_sqlstate(con), &ds_res);
3873
3693
    return 0; /* Not connected */
3874
3694
  }
3875
3695
 
3906
3726
 
3907
3727
static void do_connect(struct st_command *command)
3908
3728
{
3909
 
  uint32_t con_port= opt_port;
 
3729
  int con_port= opt_port;
3910
3730
  const char *con_options;
3911
 
  bool con_ssl= 0;
 
3731
  bool con_ssl= 0, con_compress= 0;
3912
3732
  struct st_connection* con_slot;
3913
3733
 
3914
3734
  string ds_connection_name;
3954
3774
    if (*ds_sock.c_str() != FN_LIBCHAR)
3955
3775
    {
3956
3776
      char buff[FN_REFLEN];
3957
 
      internal::fn_format(buff, ds_sock.c_str(), TMPDIR, "", 0);
 
3777
      fn_format(buff, ds_sock.c_str(), TMPDIR, "", 0);
3958
3778
      ds_sock.clear();
3959
3779
      ds_sock.append(buff);
3960
3780
    }
3974
3794
      end++;
3975
3795
    if (!strncmp(con_options, "SSL", 3))
3976
3796
      con_ssl= 1;
 
3797
    else if (!strncmp(con_options, "COMPRESS", 8))
 
3798
      con_compress= 1;
3977
3799
    else
3978
3800
      die("Illegal option to connect: %.*s",
3979
3801
          (int) (end - con_options), con_options);
3995
3817
          (int) (sizeof(connections)/sizeof(struct st_connection)));
3996
3818
  }
3997
3819
 
3998
 
  if ((con_slot->drizzle= drizzle_create(NULL)) == NULL)
3999
 
    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);
 
3820
  if (!drizzleclient_create(&con_slot->drizzle))
 
3821
    die("Failed on drizzleclient_create()");
 
3822
  if (opt_compress || con_compress)
 
3823
    drizzleclient_options(&con_slot->drizzle, DRIZZLE_OPT_COMPRESS, NULL);
 
3824
  drizzleclient_options(&con_slot->drizzle, DRIZZLE_OPT_LOCAL_INFILE, 0);
4003
3825
 
4004
3826
  /* Use default db name */
4005
3827
  if (ds_database.length() == 0)
4009
3831
  if (ds_database.length() && !strcmp(ds_database.c_str(),"*NO-ONE*"))
4010
3832
    ds_database.clear();
4011
3833
 
4012
 
  if (connect_n_handle_errors(command, &con_slot->con,
 
3834
  if (connect_n_handle_errors(command, &con_slot->drizzle,
4013
3835
                              ds_host.c_str(),ds_user.c_str(),
4014
3836
                              ds_password.c_str(), ds_database.c_str(),
4015
3837
                              con_port, ds_sock.c_str()))
4023
3845
  }
4024
3846
 
4025
3847
  /* Update $drizzleclient_get_server_version to that of current connection */
4026
 
  var_set_drizzleclient_get_server_version(&cur_con->con);
 
3848
  var_set_drizzleclient_get_server_version(&cur_con->drizzle);
4027
3849
 
4028
3850
  return;
4029
3851
}
4140
3962
  if (not_expr)
4141
3963
    cur_block->ok = !cur_block->ok;
4142
3964
 
4143
 
  free(v.str_val);
4144
 
  free(v.env_s);
4145
 
 
 
3965
  var_free(&v);
4146
3966
  return;
4147
3967
}
4148
3968
 
4219
4039
 
4220
4040
*/
4221
4041
 
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
4042
static int read_line(char *buf, int size)
4244
4043
{
4245
4044
  char c, last_quote= 0;
4299
4098
      }
4300
4099
      else if ((c == '{' &&
4301
4100
                (!my_strnncoll_simple(charset_info, (const unsigned char*) "while", 5,
4302
 
                                      (unsigned char*) buf, min((ptrdiff_t)5, p - buf), 0) ||
 
4101
                                      (unsigned char*) buf, cmin((long)5, p - buf), 0) ||
4303
4102
                 !my_strnncoll_simple(charset_info, (const unsigned char*) "if", 2,
4304
 
                                      (unsigned char*) buf, min((ptrdiff_t)2, p - buf), 0))))
 
4103
                                      (unsigned char*) buf, cmin((long)2, p - buf), 0))))
4305
4104
      {
4306
4105
        /* Only if and while commands can be terminated by { */
4307
4106
        *p++= c;
4378
4177
    {
4379
4178
      /* Could be a multibyte character */
4380
4179
      /* This code is based on the code in "sql_load.cc" */
 
4180
#ifdef USE_MB
4381
4181
      int charlen = my_mbcharlen(charset_info, c);
4382
4182
      /* We give up if multibyte character is started but not */
4383
4183
      /* completed before we pass buf_end */
4404
4204
        }
4405
4205
      }
4406
4206
      else
 
4207
#endif
4407
4208
        *p++= c;
4408
4209
    }
4409
4210
  }
4563
4364
  return;
4564
4365
}
4565
4366
 
 
4367
 
 
4368
 
4566
4369
/*
4567
4370
  Create a command from a set of lines
4568
4371
 
4580
4383
  terminated by new line '\n' regardless how many "delimiter" it contain.
4581
4384
*/
4582
4385
 
4583
 
#define MAX_QUERY (768*1024*2) /* 256K -- a test in sp-big is >128K */
 
4386
#define MAX_QUERY (256*1024*2) /* 256K -- a test in sp-big is >128K */
4584
4387
static char read_command_buf[MAX_QUERY];
4585
4388
 
4586
4389
static int read_command(struct st_command** command_ptr)
4594
4397
    *command_ptr= q_lines[parser.current_line];
4595
4398
    return(0);
4596
4399
  }
4597
 
  if (!(*command_ptr= command= new st_command))
4598
 
    die("command construction failed");
 
4400
  if (!(*command_ptr= command=
 
4401
        (struct st_command*) malloc(sizeof(*command))))
 
4402
    die(NULL);
 
4403
  memset(command, 0, sizeof(*command));
4599
4404
  q_lines.push_back(command);
4600
4405
  command->type= Q_UNKNOWN;
4601
4406
 
4642
4447
  return(0);
4643
4448
}
4644
4449
 
 
4450
 
 
4451
static struct my_option my_long_options[] =
 
4452
{
 
4453
  {"help", '?', "Display this help and exit.", 0, 0, 0, GET_NO_ARG, NO_ARG,
 
4454
   0, 0, 0, 0, 0, 0},
 
4455
  {"basedir", 'b', "Basedir for tests.", (char**) &opt_basedir,
 
4456
   (char**) &opt_basedir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4457
  {"character-sets-dir", OPT_CHARSETS_DIR,
 
4458
   "Directory where character sets are.", (char**) &opt_charsets_dir,
 
4459
   (char**) &opt_charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4460
  {"compress", 'C', "Use the compressed server/client protocol.",
 
4461
   (char**) &opt_compress, (char**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
 
4462
   0, 0, 0},
 
4463
  {"database", 'D', "Database to use.", (char**) &opt_db, (char**) &opt_db, 0,
 
4464
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4465
  {"debug-check", OPT_DEBUG_CHECK, "Check memory and open file usage at exit.",
 
4466
   (char**) &debug_check_flag, (char**) &debug_check_flag, 0,
 
4467
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4468
  {"debug-info", OPT_DEBUG_INFO, "Print some debug info at exit.",
 
4469
   (char**) &debug_info_flag, (char**) &debug_info_flag,
 
4470
   0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4471
  {"host", 'h', "Connect to host.", (char**) &opt_host, (char**) &opt_host, 0,
 
4472
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4473
  {"include", 'i', "Include SQL before each test case.", (char**) &opt_include,
 
4474
   (char**) &opt_include, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4475
  {"testdir", OPT_TESTDIR, "Path to use to search for test files",
 
4476
   (char**) &opt_testdir,
 
4477
   (char**) &opt_testdir, 0,GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4478
  {"logdir", OPT_LOG_DIR, "Directory for log files", (char**) &opt_logdir,
 
4479
   (char**) &opt_logdir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4480
  {"mark-progress", OPT_MARK_PROGRESS,
 
4481
   "Write linenumber and elapsed time to <testname>.progress ",
 
4482
   (char**) &opt_mark_progress, (char**) &opt_mark_progress, 0,
 
4483
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4484
  {"max-connect-retries", OPT_MAX_CONNECT_RETRIES,
 
4485
   "Max number of connection attempts when connecting to server",
 
4486
   (char**) &opt_max_connect_retries, (char**) &opt_max_connect_retries, 0,
 
4487
   GET_INT, REQUIRED_ARG, 500, 1, 10000, 0, 0, 0},
 
4488
  {"password", 'P', "Password to use when connecting to server.",
 
4489
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
4490
  {"port", 'p', "Port number to use for connection or 0 for default to, in "
 
4491
   "order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, "
 
4492
   "built-in default (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
 
4493
   0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4494
  {"quiet", 's', "Suppress all normal output.", (char**) &silent,
 
4495
   (char**) &silent, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4496
  {"record", 'r', "Record output of test_file into result file.",
 
4497
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4498
  {"result-file", 'R', "Read/Store result from/in this file.",
 
4499
   (char**) &result_file_name, (char**) &result_file_name, 0,
 
4500
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4501
  {"server-arg", 'A', "Send option value to embedded server as a parameter.",
 
4502
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4503
  {"server-file", 'F', "Read embedded server arguments from file.",
 
4504
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4505
  {"silent", 's', "Suppress all normal output. Synonym for --quiet.",
 
4506
   (char**) &silent, (char**) &silent, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4507
  {"sleep", 'T', "Sleep always this many seconds on sleep commands.",
 
4508
   (char**) &opt_sleep, (char**) &opt_sleep, 0, GET_INT, REQUIRED_ARG, -1, -1, 0,
 
4509
   0, 0, 0},
 
4510
  {"tail-lines", OPT_TAIL_LINES,
 
4511
   "Number of lines of the resul to include in a failure report",
 
4512
   (char**) &opt_tail_lines, (char**) &opt_tail_lines, 0,
 
4513
   GET_INT, REQUIRED_ARG, 0, 0, 10000, 0, 0, 0},
 
4514
  {"test-file", 'x', "Read test from/in this file (default stdin).",
 
4515
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4516
  {"timer-file", 'm', "File where the timing in micro seconds is stored.",
 
4517
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4518
  {"tmpdir", 't', "Temporary directory where sockets are put.",
 
4519
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4520
  {"user", 'u', "User for login.", (char**) &opt_user, (char**) &opt_user, 0,
 
4521
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4522
  {"verbose", 'v', "Write more.", (char**) &verbose, (char**) &verbose, 0,
 
4523
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4524
  {"version", 'V', "Output version information and exit.",
 
4525
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4526
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
 
4527
};
 
4528
 
 
4529
 
 
4530
static void print_version(void)
 
4531
{
 
4532
  printf("%s  Ver %s Distrib %s, for %s (%s)\n",my_progname,MTEST_VERSION,
 
4533
         drizzleclient_get_client_info(),SYSTEM_TYPE,MACHINE_TYPE);
 
4534
}
 
4535
 
 
4536
static void usage(void)
 
4537
{
 
4538
  print_version();
 
4539
  printf("MySQL AB, by Sasha, Matt, Monty & Jani\n");
 
4540
  printf("This software comes with ABSOLUTELY NO WARRANTY\n\n");
 
4541
  printf("Runs a test against the DRIZZLE server and compares output with a results file.\n\n");
 
4542
  printf("Usage: %s [OPTIONS] [database] < test_file\n", my_progname);
 
4543
  my_print_help(my_long_options);
 
4544
  printf("  --no-defaults       Don't read default options from any options file.\n");
 
4545
  my_print_variables(my_long_options);
 
4546
}
 
4547
 
 
4548
/*
 
4549
  Read arguments for embedded server and put them into
 
4550
  embedded_server_args[]
 
4551
*/
 
4552
 
 
4553
static void read_embedded_server_arguments(const char *name)
 
4554
{
 
4555
  char argument[1024],buff[FN_REFLEN], *str=0;
 
4556
  FILE *file;
 
4557
 
 
4558
  if (!test_if_hard_path(name))
 
4559
  {
 
4560
    sprintf(buff,"%s%s",opt_basedir,name);
 
4561
    name=buff;
 
4562
  }
 
4563
  fn_format(buff, name, "", "", MY_UNPACK_FILENAME);
 
4564
 
 
4565
  if (!embedded_server_arg_count)
 
4566
  {
 
4567
    embedded_server_arg_count=1;
 
4568
    embedded_server_args[0]= (char*) "";    /* Progname */
 
4569
  }
 
4570
  if (!(file= fopen(buff, "r")))
 
4571
    die("Failed to open file '%s'", buff);
 
4572
 
 
4573
  while (embedded_server_arg_count < MAX_EMBEDDED_SERVER_ARGS &&
 
4574
         (str=fgets(argument,sizeof(argument), file)))
 
4575
  {
 
4576
    *(strchr(str, '\0')-1)=0;        /* Remove end newline */
 
4577
    if (!(embedded_server_args[embedded_server_arg_count]=
 
4578
          (char*) strdup(str)))
 
4579
    {
 
4580
      fclose(file);
 
4581
      die("Out of memory");
 
4582
 
 
4583
    }
 
4584
    embedded_server_arg_count++;
 
4585
  }
 
4586
  fclose(file);
 
4587
  if (str)
 
4588
    die("Too many arguments in option file: %s",name);
 
4589
 
 
4590
  return;
 
4591
}
 
4592
 
 
4593
 
 
4594
extern "C" bool
 
4595
get_one_option(int optid, const struct my_option *, char *argument)
 
4596
{
 
4597
  char *endchar= NULL;
 
4598
  uint64_t temp_drizzle_port= 0;
 
4599
 
 
4600
  switch(optid) {
 
4601
  case 'r':
 
4602
    record = 1;
 
4603
    break;
 
4604
  case 'x':
 
4605
  {
 
4606
    char buff[FN_REFLEN];
 
4607
    if (!test_if_hard_path(argument))
 
4608
    {
 
4609
      sprintf(buff,"%s%s",opt_basedir,argument);
 
4610
      argument= buff;
 
4611
    }
 
4612
    fn_format(buff, argument, "", "", MY_UNPACK_FILENAME);
 
4613
    assert(cur_file == file_stack && cur_file->file == 0);
 
4614
    if (!(cur_file->file= fopen(buff, "r")))
 
4615
      die("Could not open '%s' for reading: errno = %d", buff, errno);
 
4616
    if (!(cur_file->file_name= strdup(buff)))
 
4617
      die("Out of memory");
 
4618
    cur_file->lineno= 1;
 
4619
    break;
 
4620
  }
 
4621
  case 'm':
 
4622
  {
 
4623
    static char buff[FN_REFLEN];
 
4624
    if (!test_if_hard_path(argument))
 
4625
    {
 
4626
      sprintf(buff,"%s%s",opt_basedir,argument);
 
4627
      argument= buff;
 
4628
    }
 
4629
    fn_format(buff, argument, "", "", MY_UNPACK_FILENAME);
 
4630
    timer_file= buff;
 
4631
    unlink(timer_file);       /* Ignore error, may not exist */
 
4632
    break;
 
4633
  }
 
4634
  case 'p':
 
4635
    temp_drizzle_port= (uint64_t) strtoul(argument, &endchar, 10);
 
4636
    /* if there is an alpha character this is not a valid port */
 
4637
    if (strlen(endchar) != 0)
 
4638
    {
 
4639
      fprintf(stderr, _("Non-integer value supplied for port.  If you are trying to enter a password please use --password instead.\n"));
 
4640
      exit(1);
 
4641
    }
 
4642
    /* If the port number is > 65535 it is not a valid port
 
4643
       This also helps with potential data loss casting unsigned long to a
 
4644
       uint32_t. */
 
4645
    if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
 
4646
    {
 
4647
      fprintf(stderr, _("Value supplied for port is not valid.\n"));
 
4648
      exit(1);
 
4649
    }
 
4650
    else
 
4651
    {
 
4652
      opt_port= (uint32_t) temp_drizzle_port;
 
4653
    }
 
4654
    break;
 
4655
  case 'P':
 
4656
    if (argument)
 
4657
    {
 
4658
      if (opt_pass)
 
4659
        free(opt_pass);
 
4660
      opt_pass = strdup(argument);
 
4661
      if (opt_pass == NULL)
 
4662
        die("Out of memory");
 
4663
      while (*argument)
 
4664
      {
 
4665
        /* Overwriting password with 'x' */
 
4666
        *argument++= 'x';
 
4667
      }
 
4668
      tty_password= 0;
 
4669
    }
 
4670
    else
 
4671
      tty_password= 1;
 
4672
    break;
 
4673
  case 't':
 
4674
    strncpy(TMPDIR, argument, sizeof(TMPDIR));
 
4675
    break;
 
4676
  case 'A':
 
4677
    if (!embedded_server_arg_count)
 
4678
    {
 
4679
      embedded_server_arg_count=1;
 
4680
      embedded_server_args[0]= (char*) "";
 
4681
    }
 
4682
    if (embedded_server_arg_count == MAX_EMBEDDED_SERVER_ARGS-1 ||
 
4683
        !(embedded_server_args[embedded_server_arg_count++]=
 
4684
          strdup(argument)))
 
4685
    {
 
4686
      die("Can't use server argument");
 
4687
    }
 
4688
    break;
 
4689
  case 'F':
 
4690
    read_embedded_server_arguments(argument);
 
4691
    break;
 
4692
  case 'V':
 
4693
    print_version();
 
4694
    exit(0);
 
4695
  case '?':
 
4696
    usage();
 
4697
    exit(0);
 
4698
  }
 
4699
  return 0;
 
4700
}
 
4701
 
 
4702
 
 
4703
static int parse_args(int argc, char **argv)
 
4704
{
 
4705
  load_defaults("drizzle",load_default_groups,&argc,&argv);
 
4706
  default_argv= argv;
 
4707
 
 
4708
  if ((handle_options(&argc, &argv, my_long_options, get_one_option)))
 
4709
    exit(1);
 
4710
 
 
4711
  if (argc > 1)
 
4712
  {
 
4713
    usage();
 
4714
    exit(1);
 
4715
  }
 
4716
  if (argc == 1)
 
4717
    opt_db= *argv;
 
4718
  if (tty_password)
 
4719
    opt_pass= drizzleclient_get_tty_password(NULL);          /* purify tested */
 
4720
  if (debug_info_flag)
 
4721
    my_end_arg= MY_CHECK_ERROR | MY_GIVE_INFO;
 
4722
  if (debug_check_flag)
 
4723
    my_end_arg= MY_CHECK_ERROR;
 
4724
 
 
4725
  return 0;
 
4726
}
 
4727
 
4645
4728
/*
4646
4729
  Write the content of str into file
4647
4730
 
4658
4741
  int fd;
4659
4742
  char buff[FN_REFLEN];
4660
4743
  int flags= O_WRONLY | O_CREAT;
4661
 
  if (!internal::test_if_hard_path(fname))
 
4744
  if (!test_if_hard_path(fname))
4662
4745
  {
4663
 
    snprintf(buff, sizeof(buff), "%s%s",opt_basedir.c_str(),fname);
 
4746
    sprintf(buff,"%s%s",opt_basedir,fname);
4664
4747
    fname= buff;
4665
4748
  }
4666
 
  internal::fn_format(buff, fname, "", "", MY_UNPACK_FILENAME);
 
4749
  fn_format(buff, fname, "", "", MY_UNPACK_FILENAME);
4667
4750
 
4668
4751
  if (!append)
4669
4752
    flags|= O_TRUNC;
4670
 
  if ((fd= internal::my_open(buff, flags,
 
4753
  if ((fd= my_open(buff, flags,
4671
4754
                   MYF(MY_WME | MY_FFNF))) < 0)
4672
4755
    die("Could not open '%s' for writing: errno = %d", buff, errno);
4673
4756
  if (append && lseek(fd, 0, SEEK_END) == MY_FILEPOS_ERROR)
4674
4757
    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)))
 
4758
  if (my_write(fd, (unsigned char*)str, size, MYF(MY_WME|MY_FNABP)))
4676
4759
    die("write failed");
4677
 
  internal::my_close(fd, MYF(0));
 
4760
  my_close(fd, MYF(0));
4678
4761
}
4679
4762
 
4680
4763
/*
4696
4779
void dump_result_to_log_file(const char *buf, int size)
4697
4780
{
4698
4781
  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 :
 
4782
  str_to_file(fn_format(log_file, result_file_name, opt_logdir, ".log",
 
4783
                        *opt_logdir ? MY_REPLACE_DIR | MY_REPLACE_EXT :
4701
4784
                        MY_REPLACE_EXT),
4702
4785
              buf, size);
4703
4786
  fprintf(stderr, "\nMore results from queries before failure can be found in %s\n",
4707
4790
void dump_progress(void)
4708
4791
{
4709
4792
  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 :
 
4793
  str_to_file(fn_format(progress_file, result_file_name,
 
4794
                        opt_logdir, ".progress",
 
4795
                        *opt_logdir ? MY_REPLACE_DIR | MY_REPLACE_EXT :
4713
4796
                        MY_REPLACE_EXT),
4714
4797
              ds_progress.c_str(), ds_progress.length());
4715
4798
}
4718
4801
{
4719
4802
  char warn_file[FN_REFLEN];
4720
4803
 
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 :
 
4804
  str_to_file(fn_format(warn_file, result_file_name, opt_logdir, ".warnings",
 
4805
                        *opt_logdir ? MY_REPLACE_DIR | MY_REPLACE_EXT :
4723
4806
                        MY_REPLACE_EXT),
4724
4807
              ds_warning_messages.c_str(), ds_warning_messages.length());
4725
4808
}
4729
4812
  Append the result for one field to the dynamic string ds
4730
4813
*/
4731
4814
 
4732
 
static void append_field(string *ds, uint32_t col_idx, drizzle_column_st *column,
 
4815
static void append_field(string *ds, uint32_t col_idx, const DRIZZLE_FIELD* field,
4733
4816
                         const char* val, uint64_t len, bool is_null)
4734
4817
{
4735
4818
  if (col_idx < max_replace_column && replace_column[col_idx])
4751
4834
  }
4752
4835
  else
4753
4836
  {
4754
 
    ds->append(drizzle_column_name(column));
 
4837
    ds->append(field->name);
4755
4838
    ds->append("\t");
4756
4839
    replace_append_mem(ds, val, (int)len);
4757
4840
    ds->append("\n");
4764
4847
  Values may be converted with 'replace_column'
4765
4848
*/
4766
4849
 
4767
 
static void append_result(string *ds, drizzle_result_st *res)
 
4850
static void append_result(string *ds, DRIZZLE_RES *res)
4768
4851
{
4769
 
  drizzle_row_t row;
4770
 
  uint32_t num_fields= drizzle_result_column_count(res);
4771
 
  drizzle_column_st *column;
4772
 
  size_t *lengths;
 
4852
  DRIZZLE_ROW row;
 
4853
  uint32_t num_fields= drizzleclient_num_fields(res);
 
4854
  const DRIZZLE_FIELD *fields= drizzleclient_fetch_fields(res);
 
4855
  uint32_t *lengths;
4773
4856
 
4774
 
  while ((row = drizzle_row_next(res)))
 
4857
  while ((row = drizzleclient_fetch_row(res)))
4775
4858
  {
4776
4859
    uint32_t i;
4777
 
    lengths = drizzle_row_field_sizes(res);
4778
 
    drizzle_column_seek(res, 0);
 
4860
    lengths = drizzleclient_fetch_lengths(res);
4779
4861
    for (i = 0; i < num_fields; i++)
4780
 
    {
4781
 
      column= drizzle_column_next(res);
4782
 
      append_field(ds, i, column,
 
4862
      append_field(ds, i, &fields[i],
4783
4863
                   (const char*)row[i], lengths[i], !row[i]);
4784
 
    }
4785
4864
    if (!display_result_vertically)
4786
4865
      ds->append("\n");
4787
4866
 
4793
4872
  Append metadata for fields to output
4794
4873
*/
4795
4874
 
4796
 
static void append_metadata(string *ds, drizzle_result_st *res)
 
4875
static void append_metadata(string *ds,
 
4876
                            const DRIZZLE_FIELD *field,
 
4877
                            uint32_t num_fields)
4797
4878
{
4798
 
  drizzle_column_st *column;
 
4879
  const DRIZZLE_FIELD *field_end;
4799
4880
  ds->append("Catalog\tDatabase\tTable\tTable_alias\tColumn\t"
4800
4881
             "Column_alias\tType\tLength\tMax length\tIs_null\t"
4801
4882
             "Flags\tDecimals\tCharsetnr\n");
4802
4883
 
4803
 
  drizzle_column_seek(res, 0);
4804
 
  while ((column= drizzle_column_next(res)))
 
4884
  for (field_end= field+num_fields ;
 
4885
       field < field_end ;
 
4886
       field++)
4805
4887
  {
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));
 
4888
    ds->append(field->catalog,
 
4889
               field->catalog_length);
 
4890
    ds->append("\t", 1);
 
4891
    ds->append(field->db, field->db_length);
 
4892
    ds->append("\t", 1);
 
4893
    ds->append(field->org_table,
 
4894
               field->org_table_length);
 
4895
    ds->append("\t", 1);
 
4896
    ds->append(field->table,
 
4897
               field->table_length);
 
4898
    ds->append("\t", 1);
 
4899
    ds->append(field->org_name,
 
4900
               field->org_name_length);
 
4901
    ds->append("\t", 1);
 
4902
    ds->append(field->name, field->name_length);
 
4903
    ds->append("\t", 1);
 
4904
    replace_append_uint(ds, field->type);
 
4905
    ds->append("\t", 1);
 
4906
    replace_append_uint(ds, field->length);
 
4907
    ds->append("\t", 1);
 
4908
    replace_append_uint(ds, field->max_length);
 
4909
    ds->append("\t", 1);
 
4910
    ds->append((char*) ((field->flags & NOT_NULL_FLAG) ?
 
4911
                        "N" : "Y"), 1);
 
4912
    ds->append("\t", 1);
 
4913
    replace_append_uint(ds, field->flags);
 
4914
    ds->append("\t", 1);
 
4915
    replace_append_uint(ds, field->decimals);
 
4916
    ds->append("\t", 1);
 
4917
    replace_append_uint(ds, field->charsetnr);
4836
4918
    ds->append("\n", 1);
4837
4919
  }
4838
4920
}
4845
4927
static void append_info(string *ds, uint64_t affected_rows,
4846
4928
                        const char *info)
4847
4929
{
4848
 
  ostringstream buf;
4849
 
  buf << "affected rows: " << affected_rows << endl;
4850
 
  ds->append(buf.str());
4851
 
  if (info && strcmp(info, ""))
 
4930
  char buf[40], buff2[21];
 
4931
  sprintf(buf,"affected rows: %s\n", llstr(affected_rows, buff2));
 
4932
  ds->append(buf);
 
4933
  if (info)
4852
4934
  {
4853
4935
    ds->append("info: ");
4854
4936
    ds->append(info);
4861
4943
  Display the table headings with the names tab separated
4862
4944
*/
4863
4945
 
4864
 
static void append_table_headings(string *ds, drizzle_result_st *res)
 
4946
static void append_table_headings(string *ds,
 
4947
                                  const DRIZZLE_FIELD *field,
 
4948
                                  uint32_t num_fields)
4865
4949
{
4866
 
  uint32_t col_idx= 0;
4867
 
  drizzle_column_st *column;
4868
 
  drizzle_column_seek(res, 0);
4869
 
  while ((column= drizzle_column_next(res)))
 
4950
  uint32_t col_idx;
 
4951
  for (col_idx= 0; col_idx < num_fields; col_idx++)
4870
4952
  {
4871
4953
    if (col_idx)
4872
4954
      ds->append("\t", 1);
4873
 
    replace_append(ds, drizzle_column_name(column));
4874
 
    col_idx++;
 
4955
    replace_append(ds, field[col_idx].name);
4875
4956
  }
4876
4957
  ds->append("\n", 1);
4877
4958
}
4883
4964
  Number of warnings appended to ds
4884
4965
*/
4885
4966
 
4886
 
static int append_warnings(string *ds, drizzle_con_st *con,
4887
 
                           drizzle_result_st *res)
 
4967
static int append_warnings(string *ds, DRIZZLE *drizzle)
4888
4968
{
4889
4969
  uint32_t count;
4890
 
  drizzle_result_st warn_res;
4891
 
  drizzle_return_t ret;
4892
 
 
4893
 
 
4894
 
  if (!(count= drizzle_result_warning_count(res)))
 
4970
  DRIZZLE_RES *warn_res;
 
4971
 
 
4972
 
 
4973
  if (!(count= drizzleclient_warning_count(drizzle)))
4895
4974
    return(0);
4896
4975
 
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);
 
4976
  /*
 
4977
    If one day we will support execution of multi-statements
 
4978
    through PS API we should not issue SHOW WARNINGS until
 
4979
    we have not read all results...
 
4980
  */
 
4981
  assert(!drizzleclient_more_results(drizzle));
 
4982
 
 
4983
  if (drizzleclient_real_query(drizzle, "SHOW WARNINGS", 13))
 
4984
    die("Error running query \"SHOW WARNINGS\": %s", drizzleclient_error(drizzle));
 
4985
 
 
4986
  if (!(warn_res= drizzleclient_store_result(drizzle)))
 
4987
    die("Warning count is %u but didn't get any warnings",
 
4988
        count);
 
4989
 
 
4990
  append_result(ds, warn_res);
4912
4991
 
4913
4992
  return(count);
4914
4993
}
4932
5011
                             int flags, char *query, int query_len,
4933
5012
                             string *ds, string *ds_warnings)
4934
5013
{
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);
 
5014
  DRIZZLE_RES *res= 0;
 
5015
  DRIZZLE *drizzle= &cn->drizzle;
 
5016
  int err= 0, counter= 0;
4941
5017
 
4942
5018
  if (flags & QUERY_SEND_FLAG)
4943
5019
  {
4944
5020
    /*
4945
5021
     * Send the query
4946
5022
     */
4947
 
 
4948
 
    (void) drizzle_query(con, &res, query, query_len, &ret);
4949
 
    if (ret != DRIZZLE_RETURN_OK)
 
5023
    if (do_send_query(cn, query, query_len, flags))
4950
5024
    {
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
 
      }
 
5025
      handle_error(command, drizzleclient_errno(drizzle), drizzleclient_error(drizzle),
 
5026
                   drizzleclient_sqlstate(drizzle), ds);
4965
5027
      goto end;
4966
5028
    }
4967
5029
  }
4968
5030
  if (!(flags & QUERY_REAP_FLAG))
4969
5031
    return;
4970
5032
 
 
5033
  do
4971
5034
  {
4972
5035
    /*
4973
 
     * Read the result packet
4974
 
     */
4975
 
    if (drizzle_result_read(con, &res, &ret) == NULL ||
4976
 
        ret != DRIZZLE_RETURN_OK)
 
5036
      When  on first result set, call drizzleclient_read_query_result to retrieve
 
5037
      answer to the query sent earlier
 
5038
    */
 
5039
    if ((counter==0) && drizzleclient_read_query_result(drizzle))
4977
5040
    {
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;
 
5041
      handle_error(command, drizzleclient_errno(drizzle), drizzleclient_error(drizzle),
 
5042
                   drizzleclient_sqlstate(drizzle), ds);
4988
5043
      goto end;
 
5044
 
4989
5045
    }
4990
5046
 
4991
5047
    /*
4992
5048
      Store the result of the query if it will return any fields
4993
5049
    */
4994
 
    if (drizzle_result_column_count(&res) &&
4995
 
        (ret= drizzle_result_buffer(&res)) != DRIZZLE_RETURN_OK)
 
5050
    if (drizzleclient_field_count(drizzle) && ((res= drizzleclient_store_result(drizzle)) == 0))
4996
5051
    {
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;
 
5052
      handle_error(command, drizzleclient_errno(drizzle), drizzleclient_error(drizzle),
 
5053
                   drizzleclient_sqlstate(drizzle), ds);
5007
5054
      goto end;
5008
5055
    }
5009
5056
 
5011
5058
    {
5012
5059
      uint64_t affected_rows= 0;    /* Ok to be undef if 'disable_info' is set */
5013
5060
 
5014
 
      if (drizzle_result_column_count(&res))
 
5061
      if (res)
5015
5062
      {
 
5063
        const DRIZZLE_FIELD *fields= drizzleclient_fetch_fields(res);
 
5064
        uint32_t num_fields= drizzleclient_num_fields(res);
 
5065
 
5016
5066
        if (display_metadata)
5017
 
          append_metadata(ds, &res);
 
5067
          append_metadata(ds, fields, num_fields);
5018
5068
 
5019
5069
        if (!display_result_vertically)
5020
 
          append_table_headings(ds, &res);
 
5070
          append_table_headings(ds, fields, num_fields);
5021
5071
 
5022
 
        append_result(ds, &res);
 
5072
        append_result(ds, res);
5023
5073
      }
5024
5074
 
5025
5075
      /*
5026
 
        Need to call drizzle_result_affected_rows() before the "new"
 
5076
        Need to call drizzleclient_affected_rows() before the "new"
5027
5077
        query to find the warnings
5028
5078
      */
5029
5079
      if (!disable_info)
5030
 
        affected_rows= drizzle_result_affected_rows(&res);
 
5080
        affected_rows= drizzleclient_affected_rows(drizzle);
5031
5081
 
5032
5082
      /*
5033
5083
        Add all warnings to the result. We can't do this if we are in
5034
5084
        the middle of processing results from multi-statement, because
5035
5085
        this will break protocol.
5036
5086
      */
5037
 
      if (!disable_warnings)
 
5087
      if (!disable_warnings && !drizzleclient_more_results(drizzle))
5038
5088
      {
5039
 
        drizzle_con_remove_options(con, DRIZZLE_CON_NO_RESULT_READ);
5040
 
        if (append_warnings(ds_warnings, con, &res) || ds_warnings->length())
 
5089
        if (append_warnings(ds_warnings, drizzle) || ds_warnings->length())
5041
5090
        {
5042
5091
          ds->append("Warnings:\n", 10);
5043
5092
          ds->append(ds_warnings->c_str(), ds_warnings->length());
5045
5094
      }
5046
5095
 
5047
5096
      if (!disable_info)
5048
 
        append_info(ds, affected_rows, drizzle_result_info(&res));
 
5097
        append_info(ds, affected_rows, drizzleclient_info(drizzle));
5049
5098
    }
5050
5099
 
5051
 
    drizzle_result_free(&res);
 
5100
    if (res)
 
5101
    {
 
5102
      drizzleclient_free_result(res);
 
5103
      res= 0;
 
5104
    }
 
5105
    counter++;
 
5106
  } while (!(err= drizzleclient_next_result(drizzle)));
 
5107
  if (err > 0)
 
5108
  {
 
5109
    /* We got an error from drizzleclient_next_result, maybe expected */
 
5110
    handle_error(command, drizzleclient_errno(drizzle), drizzleclient_error(drizzle),
 
5111
                 drizzleclient_sqlstate(drizzle), ds);
 
5112
    goto end;
5052
5113
  }
 
5114
  assert(err == -1); /* Successful and there are no more results */
5053
5115
 
5054
5116
  /* If we come here the query is both executed and read successfully */
5055
5117
  handle_no_error(command);
5061
5123
    to the server into the drizzletest builtin variable $drizzleclient_errno. This
5062
5124
    variable then can be used from the test case itself.
5063
5125
  */
5064
 
  drizzle_con_remove_options(con, DRIZZLE_CON_NO_RESULT_READ);
5065
 
  var_set_errno(err);
 
5126
  var_set_errno(drizzleclient_errno(drizzle));
5066
5127
  return;
5067
5128
}
5068
5129
 
5090
5151
  uint32_t i;
5091
5152
 
5092
5153
 
5093
 
  if (! command->require_file.empty())
 
5154
  if (command->require_file[0])
5094
5155
  {
5095
5156
    /*
5096
5157
      The query after a "--require" failed. This is fine as long the server
5097
5158
      returned a valid reponse. Don't allow 2013 or 2006 to trigger an
5098
5159
      abort_not_supported_test
5099
5160
    */
5100
 
    if (err_errno == DRIZZLE_RETURN_SERVER_GONE)
 
5161
    if (err_errno == CR_SERVER_LOST ||
 
5162
        err_errno == CR_SERVER_GONE_ERROR)
5101
5163
      die("require query '%s' failed: %d: %s", command->query,
5102
5164
          err_errno, err_error);
5103
5165
 
5115
5177
         (command->expected_errors.err[i].code.errnum == err_errno)) ||
5116
5178
        ((command->expected_errors.err[i].type == ERR_SQLSTATE) &&
5117
5179
         (strncmp(command->expected_errors.err[i].code.sqlstate,
5118
 
                  err_sqlstate, DRIZZLE_MAX_SQLSTATE_SIZE) == 0)))
 
5180
                  err_sqlstate, SQLSTATE_LENGTH) == 0)))
5119
5181
    {
5120
5182
      if (!disable_result_log)
5121
5183
      {
5249
5311
    Create a temporary dynamic string to contain the output from
5250
5312
    this query.
5251
5313
  */
5252
 
  if (! command->require_file.empty())
 
5314
  if (command->require_file[0])
5253
5315
  {
5254
5316
    ds= &ds_result;
5255
5317
  }
5292
5354
    ds= save_ds;
5293
5355
  }
5294
5356
 
5295
 
  if (! command->require_file.empty())
 
5357
  if (command->require_file[0])
5296
5358
  {
5297
5359
    /* A result file was specified for _this_ query
5298
5360
       and the output should be checked against an already
5395
5457
 
5396
5458
static void mark_progress(struct st_command*, int line)
5397
5459
{
 
5460
  char buf[32], *end;
5398
5461
  uint64_t timer= timer_now();
5399
5462
  if (!progress_start)
5400
5463
    progress_start= timer;
5401
5464
  timer-= progress_start;
5402
5465
 
5403
 
  ostringstream buf;
5404
5466
  /* Milliseconds since start */
5405
 
  buf << timer << "\t";
 
5467
  end= int64_t2str(timer, buf, 10);
 
5468
  ds_progress.append(buf, (int)(end-buf));
 
5469
  ds_progress.append("\t", 1);
5406
5470
 
5407
5471
  /* Parser line number */
5408
 
  buf << line << "\t";
 
5472
  end= int10_to_str(line, buf, 10);
 
5473
  ds_progress.append(buf, (int)(end-buf));
 
5474
  ds_progress.append("\t", 1);
5409
5475
 
5410
5476
  /* Filename */
5411
 
  buf << cur_file->file_name << ":";
 
5477
  ds_progress.append(cur_file->file_name);
 
5478
  ds_progress.append(":", 1);
5412
5479
 
5413
5480
  /* 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
 
}
 
5481
  end= int10_to_str(cur_file->lineno, buf, 10);
 
5482
  ds_progress.append(buf, (int)(end-buf));
 
5483
 
 
5484
 
 
5485
  ds_progress.append("\n", 1);
 
5486
 
 
5487
}
 
5488
 
5449
5489
 
5450
5490
int main(int argc, char **argv)
5451
5491
{
5452
 
try
5453
 
{
5454
5492
  struct st_command *command;
5455
5493
  bool q_send_flag= 0, abort_flag= 0;
5456
5494
  uint32_t command_executed= 0, last_command_executed= 0;
5457
 
  string save_file("");
 
5495
  char save_file[FN_REFLEN];
5458
5496
  struct stat res_info;
 
5497
  MY_INIT(argv[0]);
5459
5498
 
 
5499
  save_file[0]= 0;
5460
5500
  TMPDIR[0]= 0;
5461
5501
 
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
5502
  /* Init expected errors */
5581
5503
  memset(&saved_expected_errors, 0, sizeof(saved_expected_errors));
5582
5504
 
5600
5522
  cur_block->ok= true; /* Outer block should always be executed */
5601
5523
  cur_block->cmd= cmd_none;
5602
5524
 
5603
 
  var_set_string("$DRIZZLE_SERVER_VERSION", drizzle_version());
 
5525
  if (hash_init(&var_hash, charset_info,
 
5526
                1024, 0, 0, get_var_key, var_free, MYF(0)))
 
5527
    die("Variable hash initialization failed");
 
5528
 
 
5529
  var_set_string("$DRIZZLE_SERVER_VERSION", drizzleclient_get_client_info());
5604
5530
 
5605
5531
  memset(&master_pos, 0, sizeof(master_pos));
5606
5532
 
5613
5539
  ds_progress.reserve(2048);
5614
5540
  ds_warning_messages.reserve(2048);
5615
5541
 
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
 
  }
 
5542
  parse_args(argc, argv);
5735
5543
 
5736
5544
  server_initialized= 1;
5737
5545
  if (cur_file == file_stack && cur_file->file == 0)
5743
5551
    cur_file->lineno= 1;
5744
5552
  }
5745
5553
  cur_con= connections;
5746
 
  if ((cur_con->drizzle= drizzle_create(NULL)) == NULL)
5747
 
    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);
 
5554
  if (!( drizzleclient_create(&cur_con->drizzle)))
 
5555
    die("Failed in drizzleclient_create()");
 
5556
  if (opt_compress)
 
5557
    drizzleclient_options(&cur_con->drizzle,DRIZZLE_OPT_COMPRESS,NULL);
 
5558
  drizzleclient_options(&cur_con->drizzle, DRIZZLE_OPT_LOCAL_INFILE, 0);
5751
5559
 
5752
5560
  if (!(cur_con->name = strdup("default")))
5753
5561
    die("Out of memory");
5754
 
  safe_connect(&cur_con->con, cur_con->name, opt_host, opt_user, opt_pass,
 
5562
 
 
5563
  safe_connect(&cur_con->drizzle, cur_con->name, opt_host, opt_user, opt_pass,
5755
5564
               opt_db, opt_port);
5756
5565
 
5757
 
  fill_global_error_names();
5758
 
 
5759
5566
  /* Use all time until exit if no explicit 'start_timer' */
5760
5567
  timer_start= timer_now();
5761
5568
 
5767
5574
  var_set_errno(-1);
5768
5575
 
5769
5576
  /* Update $drizzleclient_get_server_version to that of current connection */
5770
 
  var_set_drizzleclient_get_server_version(&cur_con->con);
 
5577
  var_set_drizzleclient_get_server_version(&cur_con->drizzle);
5771
5578
 
5772
 
  if (! opt_include.empty())
 
5579
  if (opt_include)
5773
5580
  {
5774
 
    open_file(opt_include.c_str());
 
5581
    open_file(opt_include);
5775
5582
  }
5776
5583
 
5777
5584
  while (!read_command(&command) && !abort_flag)
5885
5692
        /* Check for special property for this query */
5886
5693
        display_result_vertically|= (command->type == Q_QUERY_VERTICAL);
5887
5694
 
5888
 
        if (! save_file.empty())
 
5695
        if (save_file[0])
5889
5696
        {
5890
 
          command->require_file= save_file;
5891
 
          save_file.clear();
 
5697
          strncpy(command->require_file, save_file, sizeof(save_file) - 1);
 
5698
          save_file[0]= 0;
5892
5699
        }
5893
5700
        run_query(cur_con, command, flags);
5894
5701
        command_executed++;
5925
5732
        command->last_argument= command->end;
5926
5733
        break;
5927
5734
      case Q_REQUIRE:
5928
 
        do_get_file_name(command, save_file);
 
5735
        do_get_file_name(command, save_file, sizeof(save_file));
5929
5736
        break;
5930
5737
      case Q_ERROR:
5931
5738
        do_get_errcodes(command);
5955
5762
        command->last_argument= command->end;
5956
5763
        break;
5957
5764
      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
 
        }
 
5765
        (void) drizzleclient_ping(&cur_con->drizzle);
5965
5766
        break;
5966
5767
      case Q_EXEC:
5967
5768
        do_exec(command);
5979
5780
        do_set_charset(command);
5980
5781
        break;
5981
5782
      case Q_DISABLE_RECONNECT:
5982
 
        set_reconnect(&cur_con->con, 0);
 
5783
        set_reconnect(&cur_con->drizzle, 0);
5983
5784
        break;
5984
5785
      case Q_ENABLE_RECONNECT:
5985
 
        set_reconnect(&cur_con->con, 1);
 
5786
        set_reconnect(&cur_con->drizzle, 1);
5986
5787
        break;
5987
5788
      case Q_DISABLE_PARSING:
5988
5789
        if (parsing_disabled == 0)
6075
5876
  */
6076
5877
  if (ds_res.length())
6077
5878
  {
6078
 
    if (! result_file_name.empty())
 
5879
    if (result_file_name)
6079
5880
    {
6080
5881
      /* A result file has been specified */
6081
5882
 
6082
5883
      if (record)
6083
5884
      {
6084
5885
        /* 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());
 
5886
        str_to_file(result_file_name, ds_res.c_str(), ds_res.length());
6086
5887
      }
6087
5888
      else
6088
5889
      {
6105
5906
  }
6106
5907
 
6107
5908
  if (!command_executed &&
6108
 
      ! result_file_name.empty() && !stat(result_file_name.c_str(), &res_info))
 
5909
      result_file_name && !stat(result_file_name, &res_info))
6109
5910
  {
6110
5911
    /*
6111
5912
      my_stat() successful on result file. Check if we have not run a
6117
5918
    die("No queries executed but result file found!");
6118
5919
  }
6119
5920
 
6120
 
  if ( opt_mark_progress && ! result_file_name.empty() )
 
5921
  if ( opt_mark_progress && result_file_name )
6121
5922
    dump_progress();
6122
5923
 
6123
5924
  /* Dump warning messages */
6124
 
  if (! result_file_name.empty() && ds_warning_messages.length())
 
5925
  if (result_file_name && ds_warning_messages.length())
6125
5926
    dump_warning_messages();
6126
5927
 
6127
5928
  timer_output();
6128
5929
  /* Yes, if we got this far the test has suceeded! Sakila smiles */
6129
5930
  cleanup_and_exit(0);
6130
 
}
6131
 
 
6132
 
  catch(exception &err)
6133
 
  {
6134
 
    cerr<<err.what()<<endl;
6135
 
  }
6136
 
 
6137
5931
  return 0; /* Keep compiler happy too */
6138
5932
}
6139
5933
 
6163
5957
{
6164
5958
  if (timer_file)
6165
5959
  {
6166
 
    ostringstream buf;
 
5960
    char buf[32], *end;
6167
5961
    uint64_t timer= timer_now() - timer_start;
6168
 
    buf << timer;
6169
 
    str_to_file(timer_file,buf.str().c_str(), buf.str().size() );
 
5962
    end= int64_t2str(timer, buf, 10);
 
5963
    str_to_file(timer_file,buf, (int) (end-buf));
6170
5964
    /* Timer has been written to the file, don't use it anymore */
6171
5965
    timer_file= 0;
6172
5966
  }
6175
5969
 
6176
5970
uint64_t timer_now(void)
6177
5971
{
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) */
 
5972
  return my_micro_time() / 1000;
6191
5973
}
6192
5974
 
6193
5975
 
6270
6052
                            const char *from, int len);
6271
6053
void free_pointer_array(POINTER_ARRAY *pa);
6272
6054
 
6273
 
struct st_replace *glob_replace= NULL;
 
6055
struct st_replace *glob_replace;
6274
6056
 
6275
6057
/*
6276
6058
  Get arguments for replace. The syntax is:
6402
6184
  char* pattern; /* Pattern to be replaced */
6403
6185
  char* replace; /* String or expression to replace the pattern with */
6404
6186
  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
6187
};
6408
6188
 
6409
6189
struct st_replace_regex
6427
6207
struct st_replace_regex *glob_replace_regex= 0;
6428
6208
 
6429
6209
int reg_replace(char** buf_p, int* buf_len_p, char *pattern, char *replace,
6430
 
                char *string, int icase, int global);
 
6210
                char *string, int icase);
6431
6211
 
6432
6212
 
6433
6213
 
6528
6308
 
6529
6309
    /* Check if we should do matching case insensitive */
6530
6310
    if (p < expr_end && *p == 'i')
6531
 
    {
6532
 
      p++;
6533
6311
      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
6312
 
6543
6313
    /* done parsing the statement, now place it in regex_arr */
6544
6314
    if (insert_dynamic(&res->regex_arr,(unsigned char*) &reg))
6596
6366
    get_dynamic(&r->regex_arr,(unsigned char*)&re,i);
6597
6367
 
6598
6368
    if (!reg_replace(&out_buf, buf_len_p, re.pattern, re.replace,
6599
 
                     in_buf, re.icase, re.global))
 
6369
                     in_buf, re.icase))
6600
6370
    {
6601
6371
      /* if the buffer has been reallocated, make adjustements */
6602
6372
      if (save_out_buf != out_buf)
6666
6436
  icase - flag, if set to 1 the match is case insensitive
6667
6437
*/
6668
6438
int reg_replace(char** buf_p, int* buf_len_p, char *pattern,
6669
 
                char *replace, char *in_string, int icase, int global)
 
6439
                char *replace, char *in_string, int icase)
6670
6440
{
 
6441
  string string_to_match(in_string);
6671
6442
  const char *error= NULL;
6672
6443
  int erroffset;
6673
6444
  int ovector[3];
6674
6445
  pcre *re= pcre_compile(pattern,
6675
 
                         icase ? PCRE_CASELESS | PCRE_MULTILINE : PCRE_MULTILINE,
 
6446
                         icase ? PCRE_CASELESS : 0,
6676
6447
                         &error, &erroffset, NULL);
6677
6448
  if (re == NULL)
6678
6449
    return 1;
6679
6450
 
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
 
  }
 
6451
  int rc= pcre_exec(re, NULL, in_string, (int)strlen(in_string),
 
6452
                    0, 0, ovector, 3);
 
6453
  if (rc < 0)
 
6454
  {
 
6455
    pcre_free(re);
 
6456
    return 1;
 
6457
  }
 
6458
 
 
6459
  char *substring_to_replace= in_string + ovector[0];
 
6460
  int substring_length= ovector[1] - ovector[0];
 
6461
  *buf_len_p= strlen(in_string) - substring_length + strlen(replace);
 
6462
  char * new_buf = (char *)malloc(*buf_len_p+1);
 
6463
  if (new_buf == NULL)
 
6464
  {
 
6465
    pcre_free(re);
 
6466
    return 1;
 
6467
  }
 
6468
 
 
6469
  memset(new_buf, 0, *buf_len_p+1);
 
6470
  strncpy(new_buf, in_string, substring_to_replace-in_string);
 
6471
  strncpy(new_buf+(substring_to_replace-in_string), replace, strlen(replace));
 
6472
  strncpy(new_buf+(substring_to_replace-in_string)+strlen(replace),
 
6473
          substring_to_replace + substring_length,
 
6474
          strlen(in_string)
 
6475
            - substring_length
 
6476
            - (substring_to_replace-in_string));
 
6477
  *buf_p= new_buf;
 
6478
 
 
6479
  pcre_free(re);
 
6480
  return 0;
6752
6481
}
6753
6482
 
6754
6483
 
7332
7061
      return(1);
7333
7062
    if (new_pos != pa->str)
7334
7063
    {
7335
 
      ptrdiff_t diff= PTR_BYTE_DIFF(new_pos,pa->str);
 
7064
      my_ptrdiff_t diff=PTR_BYTE_DIFF(new_pos,pa->str);
7336
7065
      for (i=0 ; i < pa->typelib.count ; i++)
7337
7066
        pa->typelib.type_names[i]= ADD_TO_PTR(pa->typelib.type_names[i],diff,
7338
7067
                                              char*);
7420
7149
/* Append uint32_t to ds, with optional replace */
7421
7150
void replace_append_uint(string *ds, uint32_t val)
7422
7151
{
7423
 
  ostringstream buff;
7424
 
  buff << val;
7425
 
  replace_append_mem(ds, buff.str().c_str(), buff.str().size());
 
7152
  char buff[22]; /* This should be enough for any int */
 
7153
  char *end= int64_t10_to_str(val, buff, 10);
 
7154
  replace_append_mem(ds, buff, end - buff);
7426
7155
 
7427
7156
}
7428
7157