39
39
#define MTEST_VERSION "3.3"
41
#include "client_priv.h"
49
#include "client_priv.h"
50
#ifdef HAVE_SYS_WAIT_H
50
56
#include <mysys/hash.h>
51
57
#include <stdarg.h>
53
59
#include "errname.h"
61
/* Added this for string translation. */
62
#include <drizzled/gettext.h>
64
#ifndef DRIZZLE_RETURN_SERVER_GONE
65
#define DRIZZLE_RETURN_HANDSHAKE_FAILED DRIZZLE_RETURN_ERROR_CODE
55
68
using namespace std;
72
unsigned char *get_var_key(const unsigned char* var, size_t *len, bool);
73
bool get_one_option(int optid, const struct my_option *, char *argument);
57
76
#define MAX_VAR_NAME_LENGTH 256
58
77
#define MAX_COLUMNS 256
59
78
#define MAX_EMBEDDED_SERVER_ARGS 64
60
79
#define MAX_DELIMITER_LENGTH 16
62
80
/* Flags controlling send and reap */
63
81
#define QUERY_SEND_FLAG 1
64
82
#define QUERY_REAP_FLAG 2
84
ErrorCodes global_error_names;
67
87
OPT_PS_PROTOCOL, OPT_SP_PROTOCOL, OPT_CURSOR_PROTOCOL, OPT_VIEW_PROTOCOL,
68
OPT_MAX_CONNECT_RETRIES, OPT_MARK_PROGRESS, OPT_LOG_DIR, OPT_TAIL_LINES
88
OPT_MAX_CONNECT_RETRIES, OPT_MARK_PROGRESS, OPT_LOG_DIR, OPT_TAIL_LINES,
71
92
static int record= 0, opt_sleep= -1;
72
static char *opt_db= 0, *opt_pass= 0;
73
const char *opt_user= 0, *opt_host= 0, *unix_sock= 0, *opt_basedir= "./";
93
static char *opt_db= NULL, *opt_pass= NULL;
94
const char *opt_user= NULL, *opt_host= NULL, *unix_sock= NULL,
74
96
const char *opt_logdir= "";
75
const char *opt_include= 0, *opt_charsets_dir;
76
static int opt_port= 0;
97
const char *opt_include= NULL, *opt_charsets_dir;
98
const char *opt_testdir= NULL;
99
static uint32_t opt_port= 0;
77
100
static int opt_max_connect_retries;
78
static bool opt_compress= 0, silent= 0, verbose= 0;
79
static bool debug_info_flag= 0, debug_check_flag= 0;
80
static bool tty_password= 0;
81
static bool opt_mark_progress= 0;
82
static bool parsing_disabled= 0;
101
static bool opt_compress= false, silent= false, verbose= false;
102
static bool debug_info_flag= false, debug_check_flag= false;
103
static bool tty_password= false;
104
static bool opt_mark_progress= false;
105
static bool parsing_disabled= false;
83
106
static bool display_result_vertically= false,
84
107
display_metadata= false, display_result_sorted= false;
85
static bool disable_query_log= 0, disable_result_log= 0;
86
static bool disable_warnings= 0;
87
static bool disable_info= 1;
88
static bool abort_on_error= 1;
89
static bool server_initialized= 0;
90
static bool is_windows= 0;
108
static bool disable_query_log= false, disable_result_log= false;
109
static bool disable_warnings= false;
110
static bool disable_info= true;
111
static bool abort_on_error= true;
112
static bool server_initialized= false;
113
static bool is_windows= false;
91
114
static char **default_argv;
92
115
static const char *load_default_groups[]= { "drizzletest", "client", 0 };
93
116
static char line_buffer[MAX_DELIMITER_LENGTH], *line_buffer_pos= line_buffer;
95
static uint start_lineno= 0; /* Start line of current command */
96
static uint my_end_arg= 0;
118
static uint32_t start_lineno= 0; /* Start line of current command */
119
static uint32_t my_end_arg= 0;
98
121
/* Number of lines of the result to include in failure report */
99
static uint opt_tail_lines= 0;
122
static uint32_t opt_tail_lines= 0;
101
124
static char delimiter[MAX_DELIMITER_LENGTH]= ";";
102
static uint delimiter_length= 1;
125
static uint32_t delimiter_length= 1;
104
127
static char TMPDIR[FN_REFLEN];
388
412
VAR* var_from_env(const char *, const char *);
389
413
VAR* var_init(VAR* v, const char *name, int name_len, const char *val,
391
void var_free(void* v);
415
extern "C" void var_free(void* v);
392
416
VAR* var_get(const char *var_name, const char** var_name_end,
393
417
bool raw, bool ignore_not_existing);
394
418
void eval_expr(VAR* v, const char *p, const char** p_end);
395
bool match_delimiter(int c, const char *delim, uint length);
419
bool match_delimiter(int c, const char *delim, uint32_t length);
396
420
void dump_result_to_reject_file(char *buf, int size);
397
421
void dump_result_to_log_file(const char *buf, int size);
398
422
void dump_warning_messages(void);
557
static void show_query(DRIZZLE *drizzle, const char* query)
579
static void show_query(drizzle_con_st *con, const char* query)
581
drizzle_result_st res;
582
drizzle_return_t ret;
565
if (drizzle_query(drizzle, query))
587
if (drizzle_query_str(con, &res, query, &ret) == NULL ||
588
ret != DRIZZLE_RETURN_OK)
567
log_msg("Error running query '%s': %d %s",
568
query, drizzle_errno(drizzle), drizzle_error(drizzle));
590
if (ret == DRIZZLE_RETURN_ERROR_CODE)
592
log_msg("Error running query '%s': %d %s",
593
query, drizzle_result_error_code(&res),
594
drizzle_result_error(&res));
595
drizzle_result_free(&res);
599
log_msg("Error running query '%s': %d %s",
600
query, ret, drizzle_con_error(con));
572
if ((res= drizzle_store_result(drizzle)) == NULL)
605
if (drizzle_result_column_count(&res) == 0 ||
606
drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
574
608
/* No result set returned */
609
drizzle_result_free(&res);
581
616
unsigned int row_num= 0;
582
unsigned int num_fields= drizzle_num_fields(res);
583
const DRIZZLE_FIELD *fields= drizzle_fetch_fields(res);
617
unsigned int num_fields= drizzle_result_column_count(&res);
618
drizzle_column_st *column;
585
620
fprintf(stderr, "=== %s ===\n", query);
586
while ((row= drizzle_fetch_row(res)))
621
while ((row= drizzle_row_next(&res)))
588
uint32_t *lengths= drizzle_fetch_lengths(res);
623
size_t *lengths= drizzle_row_field_sizes(&res);
591
626
fprintf(stderr, "---- %d. ----\n", row_num);
627
drizzle_column_seek(&res, 0);
592
628
for(i= 0; i < num_fields; i++)
630
column= drizzle_column_next(&res);
594
631
fprintf(stderr, "%s\t%.*s\n",
632
drizzle_column_name(column),
596
633
(int)lengths[i], row[i] ? row[i] : "NULL");
622
static void show_warnings_before_error(DRIZZLE *drizzle)
659
static void show_warnings_before_error(drizzle_con_st *con)
661
drizzle_result_st res;
662
drizzle_return_t ret;
625
663
const char* query= "SHOW WARNINGS";
631
if (drizzle_query(drizzle, query))
668
if (drizzle_query_str(con, &res, query, &ret) == NULL ||
669
ret != DRIZZLE_RETURN_OK)
633
log_msg("Error running query '%s': %d %s",
634
query, drizzle_errno(drizzle), drizzle_error(drizzle));
671
if (ret == DRIZZLE_RETURN_ERROR_CODE)
673
log_msg("Error running query '%s': %d %s",
674
query, drizzle_result_error_code(&res),
675
drizzle_result_error(&res));
676
drizzle_result_free(&res);
680
log_msg("Error running query '%s': %d %s",
681
query, ret, drizzle_con_error(con));
638
if ((res= drizzle_store_result(drizzle)) == NULL)
686
if (drizzle_result_column_count(&res) == 0 ||
687
drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
640
689
/* No result set returned */
690
drizzle_result_free(&res);
644
if (drizzle_num_rows(res) <= 1)
694
if (drizzle_result_row_count(&res) <= 1)
646
696
/* Don't display the last row, it's "last error" */
651
701
unsigned int row_num= 0;
652
unsigned int num_fields= drizzle_num_fields(res);
702
unsigned int num_fields= drizzle_result_column_count(&res);
654
704
fprintf(stderr, "\nWarnings from just before the error:\n");
655
while ((row= drizzle_fetch_row(res)))
705
while ((row= drizzle_row_next(&res)))
658
uint32_t *lengths= drizzle_fetch_lengths(res);
708
size_t *lengths= drizzle_row_field_sizes(&res);
660
if (++row_num >= drizzle_num_rows(res))
710
if (++row_num >= drizzle_result_row_count(&res))
662
712
/* Don't display the last row, it's "last error" */
1283
1334
int error= RESULT_OK;
1286
1337
char buff[512], buff2[512];
1338
const char *fname= filename2;
1288
if ((fd2= my_open(filename2, O_RDONLY, MYF(0))) < 0)
1341
if ((fd2= my_open(fname, O_RDONLY, MYF(0))) < 0)
1290
1343
my_close(fd, MYF(0));
1291
die("Failed to open second file: '%s'", filename2);
1344
if (opt_testdir != NULL)
1346
tmpfile= opt_testdir;
1347
if (tmpfile[tmpfile.length()] != '/')
1348
tmpfile.append("/");
1349
tmpfile.append(filename2);
1350
fname= tmpfile.c_str();
1352
if ((fd2= my_open(fname, O_RDONLY, MYF(0))) < 0)
1354
my_close(fd, MYF(0));
1356
die("Failed to open second file: '%s'", fname);
1293
1359
while((len= my_read(fd, (unsigned char*)&buff,
1294
1360
sizeof(buff), MYF(0))) > 0)
1374
1440
char temp_file_path[FN_REFLEN];
1376
1442
if ((fd= create_temp_file(temp_file_path, NULL,
1377
"tmp", O_CREAT | O_SHARE | O_RDWR,
1443
"tmp", MYF(MY_WME))) < 0)
1379
1444
die("Failed to create temporary file for ds");
1381
1446
/* Write ds to temporary file and set file pos to beginning*/
1382
1447
if (my_write(fd, (unsigned char *) ds->c_str(), ds->length(),
1383
1448
MYF(MY_FNABP | MY_WME)) ||
1384
my_seek(fd, 0, SEEK_SET, MYF(0)) == MY_FILEPOS_ERROR)
1449
lseek(fd, 0, SEEK_SET) == MY_FILEPOS_ERROR)
1386
1451
my_close(fd, MYF(0));
1387
1452
/* Remove the temporary file */
1738
1803
Store an integer (typically the returncode of the last SQL)
1739
statement in the drizzletest builtin variable $drizzle_errno
1804
statement in the drizzletest builtin variable $drizzleclient_errno
1742
1807
static void var_set_errno(int sql_errno)
1744
var_set_int("$drizzle_errno", sql_errno);
1809
var_set_int("$drizzleclient_errno", sql_errno);
1749
Update $drizzle_get_server_version variable with version
1814
Update $drizzleclient_get_server_version variable with version
1750
1815
of the currently connected server
1753
static void var_set_drizzle_get_server_version(DRIZZLE *drizzle)
1818
static void var_set_drizzleclient_get_server_version(drizzle_con_st *con)
1755
var_set_int("$drizzle_get_server_version", drizzle_get_server_version(drizzle));
1820
var_set_int("$drizzle_con_server_version", drizzle_con_server_version_number(con));
1798
1864
/* Eval the query, thus replacing all environment variables */
1799
1865
do_eval(&ds_query, query, end, false);
1801
if (drizzle_real_query(drizzle, ds_query.c_str(), ds_query.length()))
1802
die("Error running query '%s': %d %s", ds_query.c_str(),
1803
drizzle_errno(drizzle), drizzle_error(drizzle));
1804
if (!(res= drizzle_store_result(drizzle)))
1867
if (drizzle_query(con, &res, ds_query.c_str(), ds_query.length(),
1869
ret != DRIZZLE_RETURN_OK)
1871
if (ret == DRIZZLE_RETURN_ERROR_CODE)
1873
die("Error running query '%s': %d %s", ds_query.c_str(),
1874
drizzle_result_error_code(&res), drizzle_result_error(&res));
1875
drizzle_result_free(&res);
1879
die("Error running query '%s': %d %s", ds_query.c_str(), ret,
1880
drizzle_con_error(con));
1883
if (drizzle_result_column_count(&res) == 0 ||
1884
drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
1805
1885
die("Query '%s' didn't return a result set", ds_query.c_str());
1807
if ((row= drizzle_fetch_row(res)) && row[0])
1887
if ((row= drizzle_row_next(&res)) && row[0])
1810
1890
Concatenate all fields in the first row with tab in between
1889
1973
char * unstripped_query= strdup(ds_query.c_str());
1890
1974
if (strip_surrounding(unstripped_query, '"', '"'))
1891
1975
die("Mismatched \"'s around query '%s'", ds_query.c_str());
1892
ds_query= unstripped_query;
1977
ds_query.append(unstripped_query);
1894
1979
/* Run the query */
1895
if (drizzle_real_query(drizzle, ds_query.c_str(), ds_query.length()))
1896
die("Error running query '%s': %d %s", ds_query.c_str(),
1897
drizzle_errno(drizzle), drizzle_error(drizzle));
1898
if (!(res= drizzle_store_result(drizzle)))
1980
if (drizzle_query(con, &res, ds_query.c_str(), ds_query.length(),
1982
ret != DRIZZLE_RETURN_OK)
1984
if (ret == DRIZZLE_RETURN_ERROR_CODE)
1986
die("Error running query '%s': %d %s", ds_query.c_str(),
1987
drizzle_result_error_code(&res), drizzle_result_error(&res));
1988
drizzle_result_free(&res);
1992
die("Error running query '%s': %d %s", ds_query.c_str(), ret,
1993
drizzle_con_error(con));
1996
if (drizzle_result_column_count(&res) == 0 ||
1997
drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
1899
1998
die("Query '%s' didn't return a result set", ds_query.c_str());
1902
2001
/* Find column number from the given column name */
1904
uint num_fields= drizzle_num_fields(res);
1905
const DRIZZLE_FIELD *fields= drizzle_fetch_fields(res);
2003
uint32_t num_fields= drizzle_result_column_count(&res);
2004
drizzle_column_st *column;
1907
2006
for (i= 0; i < num_fields; i++)
1909
if (strcmp(fields[i].name, ds_col.c_str()) == 0 &&
1910
strlen(fields[i].name) == ds_col.length())
2008
column= drizzle_column_next(&res);
2009
if (strcmp(drizzle_column_name(column), ds_col.c_str()) == 0 &&
2010
strlen(drizzle_column_name(column)) == ds_col.length())
2817
static void do_change_user(struct st_command *command)
2931
static void do_change_user(struct st_command *)
2819
DRIZZLE *drizzle= &cur_con->drizzle;
2820
/* static keyword to make the NetWare compiler happy. */
2821
string ds_user, ds_passwd, ds_db;
2822
const struct command_arg change_user_args[] = {
2823
{ "user", ARG_STRING, false, &ds_user, "User to connect as" },
2824
{ "password", ARG_STRING, false, &ds_passwd, "Password used when connecting" },
2825
{ "database", ARG_STRING, false, &ds_db, "Database to select after connect" },
2830
check_command_args(command, command->first_argument,
2832
sizeof(change_user_args)/sizeof(struct command_arg),
2835
if (!ds_user.length())
2836
ds_user= drizzle->user;
2838
if (!ds_passwd.length())
2839
ds_passwd= drizzle->passwd;
2841
if (!ds_db.length())
2844
if (drizzle_change_user(drizzle, ds_user.c_str(),
2845
ds_passwd.c_str(), ds_db.c_str()))
2846
die("change user failed: %s", drizzle_error(drizzle));
2968
do_wait_for_slave_to_stop(struct st_command *c __attribute__((unused)))
3050
do_wait_for_slave_to_stop(struct st_command *)
2970
3052
static int SLAVE_POLL_INTERVAL= 300000;
2971
DRIZZLE *drizzle= &cur_con->drizzle;
3053
drizzle_con_st *con= &cur_con->con;
2974
DRIZZLE_RES *res= NULL;
3056
drizzle_result_st res;
3057
drizzle_return_t ret;
2978
if (drizzle_query(drizzle,"show status like 'Slave_running'") ||
2979
!(res=drizzle_store_result(drizzle)))
3061
if (drizzle_query_str(con,&res,"show status like 'Slave_running'",
3062
&ret) == NULL || ret != DRIZZLE_RETURN_OK)
3064
if (ret == DRIZZLE_RETURN_ERROR_CODE)
3066
die("Query failed while probing slave for stop: %s",
3067
drizzle_result_error(&res));
3068
drizzle_result_free(&res);
3072
die("Query failed while probing slave for stop: %s",
3073
drizzle_con_error(con));
3077
if (drizzle_result_column_count(&res) == 0 ||
3078
drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
2980
3080
die("Query failed while probing slave for stop: %s",
2981
drizzle_error(drizzle));
2982
if (!(row=drizzle_fetch_row(res)) || !row[1])
3081
drizzle_con_error(con));
3084
if (!(row=drizzle_row_next(&res)) || !row[1])
2984
drizzle_free_result(res);
3086
drizzle_result_free(&res);
2985
3087
die("Strange result from query while probing slave for stop");
2987
3089
done = !strcmp(row[1],"OFF");
2988
drizzle_free_result(res);
3090
drizzle_result_free(&res);
2991
my_sleep(SLAVE_POLL_INTERVAL);
3093
usleep(SLAVE_POLL_INTERVAL);
3011
3114
wait_for_position:
3013
if (drizzle_query(drizzle, query_buf))
3014
die("failed in '%s': %d: %s", query_buf, drizzle_errno(drizzle),
3015
drizzle_error(drizzle));
3017
if (!(res= drizzle_store_result(drizzle)))
3018
die("drizzle_store_result() returned NULL for '%s'", query_buf);
3019
if (!(row= drizzle_fetch_row(res)))
3021
drizzle_free_result(res);
3116
if (drizzle_query_str(con, &res, query_buf, &ret) == NULL ||
3117
ret != DRIZZLE_RETURN_OK)
3119
if (ret == DRIZZLE_RETURN_ERROR_CODE)
3121
die("failed in '%s': %d: %s", query_buf, drizzle_result_error_code(&res),
3122
drizzle_result_error(&res));
3123
drizzle_result_free(&res);
3126
die("failed in '%s': %d: %s", query_buf, ret, drizzle_con_error(con));
3129
if (drizzle_result_column_count(&res) == 0 ||
3130
drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
3131
die("drizzle_result_buffer() returned NULL for '%s'", query_buf);
3133
if (!(row= drizzle_row_next(&res)))
3135
drizzle_result_free(&res);
3022
3136
die("empty result in %s", query_buf);
3068
3182
static int do_save_master_pos(void)
3072
DRIZZLE *drizzle= &cur_con->drizzle;
3184
drizzle_result_st res;
3185
drizzle_return_t ret;
3187
drizzle_con_st *con= &cur_con->con;
3073
3188
const char *query;
3076
if (drizzle_query(drizzle, query= "show master status"))
3077
die("failed in 'show master status': %d %s",
3078
drizzle_errno(drizzle), drizzle_error(drizzle));
3191
if (drizzle_query_str(con, &res, query= "show master status", &ret) == NULL ||
3192
ret != DRIZZLE_RETURN_OK)
3194
if (ret == DRIZZLE_RETURN_ERROR_CODE)
3196
die("failed in '%s': %d: %s", query, drizzle_result_error_code(&res),
3197
drizzle_result_error(&res));
3198
drizzle_result_free(&res);
3201
die("failed in '%s': %d: %s", query, ret, drizzle_con_error(con));
3080
if (!(res = drizzle_store_result(drizzle)))
3081
die("drizzle_store_result() retuned NULL for '%s'", query);
3082
if (!(row = drizzle_fetch_row(res)))
3204
if (drizzle_result_column_count(&res) == 0 ||
3205
drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
3206
die("drizzleclient_store_result() retuned NULL for '%s'", query);
3207
if (!(row = drizzle_row_next(&res)))
3083
3208
die("empty result in show master status");
3084
my_stpncpy(master_pos.file, row[0], sizeof(master_pos.file)-1);
3209
strncpy(master_pos.file, row[0], sizeof(master_pos.file)-1);
3085
3210
master_pos.pos = strtoul(row[1], (char**) 0, 10);
3086
drizzle_free_result(res);
3211
drizzle_result_free(&res);
3229
3356
command->last_argument= p;
3230
charset_info= get_charset_by_csname(charset_name,MY_CS_PRIMARY,MYF(MY_WME));
3357
charset_info= get_charset_by_csname(charset_name, MY_CS_PRIMARY);
3231
3358
if (!charset_info)
3232
3359
abort_not_supported_test("Test requires charset '%s'", charset_name);
3235
static uint get_errcode_from_name(char *error_name, char *error_end)
3362
static uint32_t get_errcode_from_name(char *error_name, char *error_end)
3237
/* SQL error as string */
3238
st_error *e= global_error_names;
3240
/* Loop through the array of known error names */
3241
for (; e->name; e++)
3244
If we get a match, we need to check the length of the name we
3245
matched against in case it was longer than what we are checking
3246
(as in ER_WRONG_VALUE vs. ER_WRONG_VALUE_COUNT).
3248
if (!strncmp(error_name, e->name, (int) (error_end - error_name)) &&
3249
(uint) strlen(e->name) == (uint) (error_end - error_name))
3255
die("Unknown SQL error name '%s'", error_name);
3364
size_t err_name_len= error_end - error_name;
3365
string error_name_s(error_name, err_name_len);
3367
uint32_t code= global_error_names.getErrorCode(error_name_s);
3370
die("Unknown SQL error name '%s'", error_name_s.c_str());
3259
3375
static void do_get_errcodes(struct st_command *command)
3261
3377
struct st_match_err *to= saved_expected_errors.err;
3262
3378
char *p= command->first_argument;
3504
3623
if (!(con= find_connection_by_name(name)))
3505
3624
die("connection '%s' not found in connection pool", name);
3507
if (command->type == Q_DIRTY_CLOSE)
3626
if (con->drizzle != NULL)
3509
if (con->drizzle.net.vio)
3511
net_close(&(con->drizzle.net));
3628
drizzle_free(con->drizzle);
3515
drizzle_close(&con->drizzle);
3517
if (con->util_drizzle)
3518
drizzle_close(con->util_drizzle);
3519
con->util_drizzle= 0;
3521
3631
free(con->name);
3524
3634
When the connection is closed set name to "-closed_connection-"
3525
3635
to make it possible to reuse the connection name.
3527
if (!(con->name = my_strdup("-closed_connection-", MYF(MY_WME))))
3637
if (!(con->name = strdup("-closed_connection-")))
3528
3638
die("Out of memory");
3559
static void safe_connect(DRIZZLE *drizzle, const char *name, const char *host,
3560
const char *user, const char *pass, const char *db,
3669
static void safe_connect(drizzle_con_st *con, const char *name,
3670
const char *host, const char *user, const char *pass,
3671
const char *db, int port)
3563
3673
int failed_attempts= 0;
3564
3674
static uint32_t connection_retry_sleep= 100000; /* Microseconds */
3567
while(!drizzle_connect(drizzle, host, user, pass, db, port, NULL,
3568
CLIENT_MULTI_STATEMENTS | CLIENT_REMEMBER_OPTIONS))
3675
drizzle_return_t ret;
3677
drizzle_con_set_tcp(con, host, port);
3678
drizzle_con_set_auth(con, user, pass);
3679
drizzle_con_set_db(con, db);
3680
while((ret= drizzle_con_connect(con)) != DRIZZLE_RETURN_OK)
3575
3687
on protocol/connection type
3578
if ((drizzle_errno(drizzle) == CR_CONN_HOST_ERROR ||
3579
drizzle_errno(drizzle) == CR_CONNECTION_ERROR) &&
3690
if ((ret == DRIZZLE_RETURN_GETADDRINFO ||
3691
ret == DRIZZLE_RETURN_COULD_NOT_CONNECT) &&
3580
3692
failed_attempts < opt_max_connect_retries)
3582
3694
verbose_msg("Connect attempt %d/%d failed: %d: %s", failed_attempts,
3583
opt_max_connect_retries, drizzle_errno(drizzle),
3584
drizzle_error(drizzle));
3585
my_sleep(connection_retry_sleep);
3695
opt_max_connect_retries, ret, drizzle_con_error(con));
3696
usleep(connection_retry_sleep);
3589
3700
if (failed_attempts > 0)
3590
3701
die("Could not open connection '%s' after %d attempts: %d %s", name,
3591
failed_attempts, drizzle_errno(drizzle), drizzle_error(drizzle));
3702
failed_attempts, ret, drizzle_con_error(con));
3593
die("Could not open connection '%s': %d %s", name,
3594
drizzle_errno(drizzle), drizzle_error(drizzle));
3704
die("Could not open connection '%s': %d %s", name, ret,
3705
drizzle_con_error(con));
3596
3707
failed_attempts++;
3653
3765
ds_res.append(delimiter);
3654
3766
ds_res.append("\n");
3656
if (!drizzle_connect(con, host, user, pass, db, port, 0,
3657
CLIENT_MULTI_STATEMENTS))
3768
drizzle_con_set_tcp(con, host, port);
3769
drizzle_con_set_auth(con, user, pass);
3770
drizzle_con_set_db(con, db);
3771
if ((ret= drizzle_con_connect(con)) != DRIZZLE_RETURN_OK)
3659
var_set_errno(drizzle_errno(con));
3660
handle_error(command, drizzle_errno(con), drizzle_error(con),
3661
drizzle_sqlstate(con), &ds_res);
3773
if (ret == DRIZZLE_RETURN_HANDSHAKE_FAILED)
3775
var_set_errno(drizzle_con_error_code(con));
3776
handle_error(command, drizzle_con_error_code(con), drizzle_con_error(con),
3777
drizzle_con_sqlstate(con), &ds_res);
3782
handle_error(command, ret, drizzle_con_error(con), "", &ds_res);
3662
3785
return 0; /* Not connected */
3785
3909
(int) (sizeof(connections)/sizeof(struct st_connection)));
3788
if (!drizzle_create(&con_slot->drizzle))
3912
if ((con_slot->drizzle= drizzle_create(NULL)) == NULL)
3789
3913
die("Failed on drizzle_create()");
3790
if (opt_compress || con_compress)
3791
drizzle_options(&con_slot->drizzle, DRIZZLE_OPT_COMPRESS, NULL);
3792
drizzle_options(&con_slot->drizzle, DRIZZLE_OPT_LOCAL_INFILE, 0);
3914
if (!drizzle_con_create(con_slot->drizzle, &con_slot->con))
3915
die("Failed on drizzle_con_create()");
3794
3917
/* Use default db name */
3795
3918
if (ds_database.length() == 0)
3796
ds_database= opt_db;
3919
ds_database.append(opt_db);
3798
3921
/* Special database to allow one to connect without a database name */
3799
3922
if (ds_database.length() && !strcmp(ds_database.c_str(),"*NO-ONE*"))
3923
ds_database.clear();
3802
if (connect_n_handle_errors(command, &con_slot->drizzle,
3925
if (connect_n_handle_errors(command, &con_slot->con,
3803
3926
ds_host.c_str(),ds_user.c_str(),
3804
3927
ds_password.c_str(), ds_database.c_str(),
3805
3928
con_port, ds_sock.c_str()))
4067
4192
else if ((c == '{' &&
4068
4193
(!my_strnncoll_simple(charset_info, (const unsigned char*) "while", 5,
4069
(unsigned char*) buf, cmin((long)5, p - buf), 0) ||
4194
(unsigned char*) buf, min((ptrdiff_t)5, p - buf), 0) ||
4070
4195
!my_strnncoll_simple(charset_info, (const unsigned char*) "if", 2,
4071
(unsigned char*) buf, cmin((long)2, p - buf), 0))))
4196
(unsigned char*) buf, min((ptrdiff_t)2, p - buf), 0))))
4073
4198
/* Only if and while commands can be terminated by { */
4440
4563
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
4441
4564
{"include", 'i', "Include SQL before each test case.", (char**) &opt_include,
4442
4565
(char**) &opt_include, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
4566
{"testdir", OPT_TESTDIR, "Path to use to search for test files",
4567
(char**) &opt_testdir,
4568
(char**) &opt_testdir, 0,GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
4443
4569
{"logdir", OPT_LOG_DIR, "Directory for log files", (char**) &opt_logdir,
4444
4570
(char**) &opt_logdir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
4445
4571
{"mark-progress", OPT_MARK_PROGRESS,
4450
4576
"Max number of connection attempts when connecting to server",
4451
4577
(char**) &opt_max_connect_retries, (char**) &opt_max_connect_retries, 0,
4452
4578
GET_INT, REQUIRED_ARG, 500, 1, 10000, 0, 0, 0},
4453
{"password", 'p', "Password to use when connecting to server.",
4579
{"password", 'P', "Password to use when connecting to server.",
4454
4580
0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
4455
{"port", 'P', "Port number to use for connection or 0 for default to, in "
4456
"order of preference, my.cnf, $DRIZZLE_TCP_PORT, "
4581
{"port", 'p', "Port number to use for connection or 0 for default to, in "
4582
"order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, "
4457
4583
"built-in default (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
4459
(char**) &opt_port, 0, GET_INT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
4584
0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
4460
4585
{"quiet", 's', "Suppress all normal output.", (char**) &silent,
4461
4586
(char**) &silent, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
4462
4587
{"record", 'r', "Record output of test_file into result file.",
4570
4696
char buff[FN_REFLEN];
4571
4697
if (!test_if_hard_path(argument))
4573
strxmov(buff, opt_basedir, argument, NULL);
4699
sprintf(buff,"%s%s",opt_basedir,argument);
4574
4700
argument= buff;
4576
4702
fn_format(buff, argument, "", "", MY_UNPACK_FILENAME);
4577
4703
assert(cur_file == file_stack && cur_file->file == 0);
4578
if (!(cur_file->file=
4579
my_fopen(buff, O_RDONLY | FILE_BINARY, MYF(0))))
4704
if (!(cur_file->file= fopen(buff, "r")))
4580
4705
die("Could not open '%s' for reading: errno = %d", buff, errno);
4581
cur_file->file_name= my_strdup(buff, MYF(MY_FAE));
4706
if (!(cur_file->file_name= strdup(buff)))
4707
die("Out of memory");
4582
4708
cur_file->lineno= 1;
4725
temp_drizzle_port= (uint64_t) strtoul(argument, &endchar, 10);
4726
/* if there is an alpha character this is not a valid port */
4727
if (strlen(endchar) != 0)
4729
fprintf(stderr, _("Non-integer value supplied for port. If you are trying to enter a password please use --password instead.\n"));
4732
/* If the port number is > 65535 it is not a valid port
4733
This also helps with potential data loss casting unsigned long to a
4735
if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
4737
fprintf(stderr, _("Value supplied for port is not valid.\n"));
4742
opt_port= (uint32_t) temp_drizzle_port;
4602
opt_pass= my_strdup(argument, MYF(MY_FAE));
4603
while (*argument) *argument++= 'x'; /* Destroy argument */
4750
opt_pass = strdup(argument);
4751
if (opt_pass == NULL)
4752
die("Out of memory");
4755
/* Overwriting password with 'x' */
4604
4758
tty_password= 0;
4607
4761
tty_password= 1;
4610
my_stpncpy(TMPDIR, argument, sizeof(TMPDIR));
4764
strncpy(TMPDIR, argument, sizeof(TMPDIR));
4613
4767
if (!embedded_server_arg_count)
4783
4937
Values may be converted with 'replace_column'
4786
static void append_result(string *ds, DRIZZLE_RES *res)
4940
static void append_result(string *ds, drizzle_result_st *res)
4789
uint32_t num_fields= drizzle_num_fields(res);
4790
const DRIZZLE_FIELD *fields= drizzle_fetch_fields(res);
4943
uint32_t num_fields= drizzle_result_column_count(res);
4944
drizzle_column_st *column;
4793
while ((row = drizzle_fetch_row(res)))
4947
while ((row = drizzle_row_next(res)))
4796
lengths = drizzle_fetch_lengths(res);
4950
lengths = drizzle_row_field_sizes(res);
4951
drizzle_column_seek(res, 0);
4797
4952
for (i = 0; i < num_fields; i++)
4798
append_field(ds, i, &fields[i],
4954
column= drizzle_column_next(res);
4955
append_field(ds, i, column,
4799
4956
(const char*)row[i], lengths[i], !row[i]);
4800
4958
if (!display_result_vertically)
4801
4959
ds->append("\n");
4808
4966
Append metadata for fields to output
4811
static void append_metadata(string *ds,
4812
const DRIZZLE_FIELD *field,
4969
static void append_metadata(string *ds, drizzle_result_st *res)
4815
const DRIZZLE_FIELD *field_end;
4971
drizzle_column_st *column;
4816
4972
ds->append("Catalog\tDatabase\tTable\tTable_alias\tColumn\t"
4817
4973
"Column_alias\tType\tLength\tMax length\tIs_null\t"
4818
4974
"Flags\tDecimals\tCharsetnr\n");
4820
for (field_end= field+num_fields ;
4976
drizzle_column_seek(res, 0);
4977
while ((column= drizzle_column_next(res)))
4824
ds->append(field->catalog,
4825
field->catalog_length);
4826
ds->append("\t", 1);
4827
ds->append(field->db, field->db_length);
4828
ds->append("\t", 1);
4829
ds->append(field->org_table,
4830
field->org_table_length);
4831
ds->append("\t", 1);
4832
ds->append(field->table,
4833
field->table_length);
4834
ds->append("\t", 1);
4835
ds->append(field->org_name,
4836
field->org_name_length);
4837
ds->append("\t", 1);
4838
ds->append(field->name, field->name_length);
4839
ds->append("\t", 1);
4840
replace_append_uint(ds, field->type);
4841
ds->append("\t", 1);
4842
replace_append_uint(ds, field->length);
4843
ds->append("\t", 1);
4844
replace_append_uint(ds, field->max_length);
4845
ds->append("\t", 1);
4846
ds->append((char*) ((field->flags & NOT_NULL_FLAG) ?
4848
ds->append("\t", 1);
4849
replace_append_uint(ds, field->flags);
4850
ds->append("\t", 1);
4851
replace_append_uint(ds, field->decimals);
4852
ds->append("\t", 1);
4853
replace_append_uint(ds, field->charsetnr);
4979
ds->append(drizzle_column_catalog(column),
4980
strlen(drizzle_column_catalog(column)));
4981
ds->append("\t", 1);
4982
ds->append(drizzle_column_db(column), strlen(drizzle_column_db(column)));
4983
ds->append("\t", 1);
4984
ds->append(drizzle_column_orig_table(column),
4985
strlen(drizzle_column_orig_table(column)));
4986
ds->append("\t", 1);
4987
ds->append(drizzle_column_table(column),
4988
strlen(drizzle_column_table(column)));
4989
ds->append("\t", 1);
4990
ds->append(drizzle_column_orig_name(column),
4991
strlen(drizzle_column_orig_name(column)));
4992
ds->append("\t", 1);
4993
ds->append(drizzle_column_name(column),
4994
strlen(drizzle_column_name(column)));
4995
ds->append("\t", 1);
4996
replace_append_uint(ds, drizzle_column_type_drizzle(column));
4997
ds->append("\t", 1);
4998
replace_append_uint(ds, drizzle_column_size(column));
4999
ds->append("\t", 1);
5000
replace_append_uint(ds, drizzle_column_max_size(column));
5001
ds->append("\t", 1);
5002
ds->append((char*) ((drizzle_column_flags(column) & DRIZZLE_COLUMN_FLAGS_NOT_NULL) ? "N" : "Y"), 1);
5003
ds->append("\t", 1);
5004
replace_append_uint(ds, drizzle_column_flags(column));
5005
ds->append("\t", 1);
5006
replace_append_uint(ds, drizzle_column_decimals(column));
5007
ds->append("\t", 1);
5008
replace_append_uint(ds, drizzle_column_charset(column));
4854
5009
ds->append("\n", 1);
4900
5056
Number of warnings appended to ds
4903
static int append_warnings(string *ds, DRIZZLE *drizzle)
5059
static int append_warnings(string *ds, drizzle_con_st *con,
5060
drizzle_result_st *res)
4906
DRIZZLE_RES *warn_res;
4909
if (!(count= drizzle_warning_count(drizzle)))
5063
drizzle_result_st warn_res;
5064
drizzle_return_t ret;
5067
if (!(count= drizzle_result_warning_count(res)))
4913
If one day we will support execution of multi-statements
4914
through PS API we should not issue SHOW WARNINGS until
4915
we have not read all results...
4917
assert(!drizzle_more_results(drizzle));
4919
if (drizzle_real_query(drizzle, "SHOW WARNINGS", 13))
4920
die("Error running query \"SHOW WARNINGS\": %s", drizzle_error(drizzle));
4922
if (!(warn_res= drizzle_store_result(drizzle)))
4923
die("Warning count is %u but didn't get any warnings",
4926
append_result(ds, warn_res);
5070
if (drizzle_query_str(con, &warn_res, "SHOW WARNINGS", &ret) == NULL ||
5071
ret != DRIZZLE_RETURN_OK)
5073
if (ret == DRIZZLE_RETURN_ERROR_CODE)
5074
die("Error running query \"SHOW WARNINGS\": %s", drizzle_result_error(&warn_res));
5076
die("Error running query \"SHOW WARNINGS\": %s", drizzle_con_error(con));
5079
if (drizzle_result_column_count(&warn_res) == 0 ||
5080
drizzle_result_buffer(&warn_res) != DRIZZLE_RETURN_OK)
5081
die("Warning count is %u but didn't get any warnings", count);
5083
append_result(ds, &warn_res);
5084
drizzle_result_free(&warn_res);
4947
5105
int flags, char *query, int query_len,
4948
5106
string *ds, string *ds_warnings)
4950
DRIZZLE_RES *res= 0;
4951
DRIZZLE *drizzle= &cn->drizzle;
4952
int err= 0, counter= 0;
5108
drizzle_result_st res;
5109
drizzle_return_t ret;
5110
drizzle_con_st *con= &cn->con;
5113
drizzle_con_add_options(con, DRIZZLE_CON_NO_RESULT_READ);
4954
5115
if (flags & QUERY_SEND_FLAG)
4957
5118
* Send the query
4959
if (do_send_query(cn, query, query_len, flags))
5121
(void) drizzle_query(con, &res, query, query_len, &ret);
5122
if (ret != DRIZZLE_RETURN_OK)
4961
handle_error(command, drizzle_errno(drizzle), drizzle_error(drizzle),
4962
drizzle_sqlstate(drizzle), ds);
5124
if (ret == DRIZZLE_RETURN_ERROR_CODE ||
5125
ret == DRIZZLE_RETURN_HANDSHAKE_FAILED)
5127
err= drizzle_result_error_code(&res);
5128
handle_error(command, err, drizzle_result_error(&res),
5129
drizzle_result_sqlstate(&res), ds);
5130
if (ret == DRIZZLE_RETURN_ERROR_CODE)
5131
drizzle_result_free(&res);
5135
handle_error(command, ret, drizzle_con_error(con), "", ds);
4966
5141
if (!(flags & QUERY_REAP_FLAG))
4972
When on first result set, call drizzle_read_query_result to retrieve
4973
answer to the query sent earlier
4975
if ((counter==0) && drizzle_read_query_result(drizzle))
5146
* Read the result packet
5148
if (drizzle_result_read(con, &res, &ret) == NULL ||
5149
ret != DRIZZLE_RETURN_OK)
4977
handle_error(command, drizzle_errno(drizzle), drizzle_error(drizzle),
4978
drizzle_sqlstate(drizzle), ds);
5151
if (ret == DRIZZLE_RETURN_ERROR_CODE)
5153
handle_error(command, drizzle_result_error_code(&res),
5154
drizzle_result_error(&res), drizzle_result_sqlstate(&res),
5158
handle_error(command, ret, drizzle_con_error(con), "", ds);
5159
drizzle_result_free(&res);
4984
5165
Store the result of the query if it will return any fields
4986
if (drizzle_field_count(drizzle) && ((res= drizzle_store_result(drizzle)) == 0))
5167
if (drizzle_result_column_count(&res) &&
5168
(ret= drizzle_result_buffer(&res)) != DRIZZLE_RETURN_OK)
4988
handle_error(command, drizzle_errno(drizzle), drizzle_error(drizzle),
4989
drizzle_sqlstate(drizzle), ds);
5170
if (ret == DRIZZLE_RETURN_ERROR_CODE)
5172
handle_error(command, drizzle_result_error_code(&res),
5173
drizzle_result_error(&res), drizzle_result_sqlstate(&res),
5177
handle_error(command, ret, drizzle_con_error(con), "", ds);
5178
drizzle_result_free(&res);
4995
5185
uint64_t affected_rows= 0; /* Ok to be undef if 'disable_info' is set */
5187
if (drizzle_result_column_count(&res))
4999
const DRIZZLE_FIELD *fields= drizzle_fetch_fields(res);
5000
uint num_fields= drizzle_num_fields(res);
5002
5189
if (display_metadata)
5003
append_metadata(ds, fields, num_fields);
5190
append_metadata(ds, &res);
5005
5192
if (!display_result_vertically)
5006
append_table_headings(ds, fields, num_fields);
5193
append_table_headings(ds, &res);
5008
append_result(ds, res);
5195
append_result(ds, &res);
5012
Need to call drizzle_affected_rows() before the "new"
5199
Need to call drizzleclient_affected_rows() before the "new"
5013
5200
query to find the warnings
5015
5202
if (!disable_info)
5016
affected_rows= drizzle_affected_rows(drizzle);
5203
affected_rows= drizzle_result_affected_rows(&res);
5019
5206
Add all warnings to the result. We can't do this if we are in
5020
5207
the middle of processing results from multi-statement, because
5021
5208
this will break protocol.
5023
if (!disable_warnings && !drizzle_more_results(drizzle))
5210
if (!disable_warnings)
5025
if (append_warnings(ds_warnings, drizzle) || ds_warnings->length())
5212
drizzle_con_remove_options(con, DRIZZLE_CON_NO_RESULT_READ);
5213
if (append_warnings(ds_warnings, con, &res) || ds_warnings->length())
5027
5215
ds->append("Warnings:\n", 10);
5028
5216
ds->append(ds_warnings->c_str(), ds_warnings->length());
5032
5220
if (!disable_info)
5033
append_info(ds, affected_rows, drizzle_info(drizzle));
5221
append_info(ds, affected_rows, drizzle_result_info(&res));
5038
drizzle_free_result(res);
5042
} while (!(err= drizzle_next_result(drizzle)));
5045
/* We got an error from drizzle_next_result, maybe expected */
5046
handle_error(command, drizzle_errno(drizzle), drizzle_error(drizzle),
5047
drizzle_sqlstate(drizzle), ds);
5224
drizzle_result_free(&res);
5050
assert(err == -1); /* Successful and there are no more results */
5052
5227
/* If we come here the query is both executed and read successfully */
5053
5228
handle_no_error(command);
5394
static void mark_progress(struct st_command* command __attribute__((unused)),
5569
static void mark_progress(struct st_command*, int line)
5398
5571
uint64_t timer= timer_now();
5399
5572
if (!progress_start)
5400
5573
progress_start= timer;
5401
5574
timer-= progress_start;
5403
5577
/* Milliseconds since start */
5404
end= int64_t2str(timer, buf, 10);
5405
ds_progress.append(buf, (int)(end-buf));
5406
ds_progress.append("\t", 1);
5578
buf << timer << "\t";
5408
5580
/* Parser line number */
5409
end= int10_to_str(line, buf, 10);
5410
ds_progress.append(buf, (int)(end-buf));
5411
ds_progress.append("\t", 1);
5581
buf << line << "\t";
5414
ds_progress.append(cur_file->file_name);
5415
ds_progress.append(":", 1);
5584
buf << cur_file->file_name << ":";
5417
5586
/* Line in file */
5418
end= int10_to_str(cur_file->lineno, buf, 10);
5419
ds_progress.append(buf, (int)(end-buf));
5422
ds_progress.append("\n", 1);
5587
buf << cur_file->lineno << endl;
5589
ds_progress.append(buf.str());
5482
5649
if (cur_file == file_stack && cur_file->file == 0)
5484
5651
cur_file->file= stdin;
5485
cur_file->file_name= my_strdup("<stdin>", MYF(MY_WME));
5652
cur_file->file_name= strdup("<stdin>");
5653
if (cur_file->file_name == NULL)
5654
die("Out of memory");
5486
5655
cur_file->lineno= 1;
5488
5657
cur_con= connections;
5489
if (!( drizzle_create(&cur_con->drizzle)))
5658
if ((cur_con->drizzle= drizzle_create(NULL)) == NULL)
5490
5659
die("Failed in drizzle_create()");
5492
drizzle_options(&cur_con->drizzle,DRIZZLE_OPT_COMPRESS,NULL);
5493
drizzle_options(&cur_con->drizzle, DRIZZLE_OPT_LOCAL_INFILE, 0);
5660
if (!( drizzle_con_create(cur_con->drizzle, &cur_con->con)))
5661
die("Failed in drizzle_con_create()");
5495
if (!(cur_con->name = my_strdup("default", MYF(MY_WME))))
5663
if (!(cur_con->name = strdup("default")))
5496
5664
die("Out of memory");
5498
safe_connect(&cur_con->drizzle, cur_con->name, opt_host, opt_user, opt_pass,
5666
safe_connect(&cur_con->con, cur_con->name, opt_host, opt_user, opt_pass,
5499
5667
opt_db, opt_port);
5501
5669
/* Use all time until exit if no explicit 'start_timer' */
5502
5670
timer_start= timer_now();
5505
Initialize $drizzle_errno with -1, so we can
5673
Initialize $drizzleclient_errno with -1, so we can
5506
5674
- distinguish it from valid values ( >= 0 ) and
5507
5675
- detect if there was never a command sent to the server
5509
5677
var_set_errno(-1);
5511
/* Update $drizzle_get_server_version to that of current connection */
5512
var_set_drizzle_get_server_version(&cur_con->drizzle);
5679
/* Update $drizzleclient_get_server_version to that of current connection */
5680
var_set_drizzleclient_get_server_version(&cur_con->con);
5514
5682
if (opt_include)
5974
6150
TYPELIB typelib; /* Pointer to strings */
5975
6151
unsigned char *str; /* Strings is here */
5976
6152
uint8_t *flag; /* Flag about each var. */
5977
uint array_allocs,max_count,length,max_length;
6153
uint32_t array_allocs,max_count,length,max_length;
5978
6154
} POINTER_ARRAY;
5980
6156
struct st_replace;
5981
struct st_replace *init_replace(char * *from, char * *to, uint count,
6157
struct st_replace *init_replace(char * *from, char * *to, uint32_t count,
5982
6158
char * word_end_chars);
5983
6159
int insert_pointer_name(POINTER_ARRAY *pa,char * name);
5984
6160
void replace_strings_append(struct st_replace *rep, string* ds,
5985
6161
const char *from, int len);
5986
6162
void free_pointer_array(POINTER_ARRAY *pa);
5988
struct st_replace *glob_replace;
6164
struct st_replace *glob_replace= NULL;
5991
6167
Get arguments for replace. The syntax is:
6384
6548
char *replace, char *in_string, int icase)
6386
6550
string string_to_match(in_string);
6387
pcrecpp::RE_Options opt;
6390
opt.set_caseless(true);
6392
if (!pcrecpp::RE(pattern, opt).Replace(replace,&string_to_match)){
6551
const char *error= NULL;
6554
pcre *re= pcre_compile(pattern,
6555
icase ? PCRE_CASELESS : 0,
6556
&error, &erroffset, NULL);
6560
int rc= pcre_exec(re, NULL, in_string, (int)strlen(in_string),
6396
const char * new_str= string_to_match.c_str();
6397
*buf_len_p= strlen(new_str);
6568
char *substring_to_replace= in_string + ovector[0];
6569
int substring_length= ovector[1] - ovector[0];
6570
*buf_len_p= strlen(in_string) - substring_length + strlen(replace);
6398
6571
char * new_buf = (char *)malloc(*buf_len_p+1);
6399
6572
if (new_buf == NULL)
6403
strcpy(new_buf, new_str);
6578
memset(new_buf, 0, *buf_len_p+1);
6579
strncpy(new_buf, in_string, substring_to_replace-in_string);
6580
strncpy(new_buf+(substring_to_replace-in_string), replace, strlen(replace));
6581
strncpy(new_buf+(substring_to_replace-in_string)+strlen(replace),
6582
substring_to_replace + substring_length,
6585
- (substring_to_replace-in_string));
6410
6593
#ifndef WORD_BIT
6411
#define WORD_BIT (8*sizeof(uint))
6594
#define WORD_BIT (8*sizeof(uint32_t))
6414
6597
#define SET_MALLOC_HUNC 64
6415
6598
#define LAST_CHAR_CODE 259
6417
6600
typedef struct st_rep_set {
6418
uint *bits; /* Pointer to used sets */
6601
uint32_t *bits; /* Pointer to used sets */
6419
6602
short next[LAST_CHAR_CODE]; /* Pointer to next sets */
6420
uint found_len; /* Best match to date */
6603
uint32_t found_len; /* Best match to date */
6421
6604
int found_offset;
6423
uint size_of_bits; /* For convinience */
6605
uint32_t table_offset;
6606
uint32_t size_of_bits; /* For convinience */
6426
6609
typedef struct st_rep_sets {
6427
uint count; /* Number of sets */
6428
uint extra; /* Extra sets in buffer */
6429
uint invisible; /* Sets not chown */
6610
uint32_t count; /* Number of sets */
6611
uint32_t extra; /* Extra sets in buffer */
6612
uint32_t invisible; /* Sets not chown */
6613
uint32_t size_of_bits;
6431
6614
REP_SET *set,*set_buffer;
6615
uint32_t *bit_buffer;
6435
6618
typedef struct st_found_set {
6619
uint32_t table_offset;
6437
6620
int found_offset;
6440
6623
typedef struct st_follow {
6625
uint32_t table_offset;
6447
int init_sets(REP_SETS *sets,uint states);
6630
int init_sets(REP_SETS *sets,uint32_t states);
6448
6631
REP_SET *make_new_set(REP_SETS *sets);
6449
6632
void make_sets_invisible(REP_SETS *sets);
6450
6633
void free_last_set(REP_SETS *sets);
6451
6634
void free_sets(REP_SETS *sets);
6452
void internal_set_bit(REP_SET *set, uint bit);
6453
void internal_clear_bit(REP_SET *set, uint bit);
6635
void internal_set_bit(REP_SET *set, uint32_t bit);
6636
void internal_clear_bit(REP_SET *set, uint32_t bit);
6454
6637
void or_bits(REP_SET *to,REP_SET *from);
6455
6638
void copy_bits(REP_SET *to,REP_SET *from);
6456
6639
int cmp_bits(REP_SET *set1,REP_SET *set2);
6457
int get_next_bit(REP_SET *set,uint lastpos);
6640
int get_next_bit(REP_SET *set,uint32_t lastpos);
6458
6641
int find_set(REP_SETS *sets,REP_SET *find);
6459
int find_found(FOUND_SET *found_set,uint table_offset,
6642
int find_found(FOUND_SET *found_set,uint32_t table_offset,
6460
6643
int found_offset);
6461
uint start_at_word(char * pos);
6462
uint end_of_word(char * pos);
6464
static uint found_sets=0;
6467
static uint replace_len(char * str)
6644
uint32_t start_at_word(char * pos);
6645
uint32_t end_of_word(char * pos);
6647
static uint32_t found_sets=0;
6650
static uint32_t replace_len(char * str)
6472
6655
if (str[0] == '\\' && str[1])
6480
6663
/* Init a replace structure for further calls */
6482
REPLACE *init_replace(char * *from, char * *to,uint count,
6665
REPLACE *init_replace(char * *from, char * *to,uint32_t count,
6483
6666
char * word_end_chars)
6485
6668
static const int SPACE_CHAR= 256;
6486
6669
static const int START_OF_LINE= 257;
6487
6670
static const int END_OF_LINE= 258;
6489
uint i,j,states,set_nr,len,result_len,max_length,found_end,bits_set,bit_nr;
6672
uint32_t i,j,states,set_nr,len,result_len,max_length,found_end,bits_set,bit_nr;
6490
6673
int used_sets,chr,default_state;
6491
6674
char used_chars[LAST_CHAR_CODE],is_word_end[256];
6492
6675
char * pos, *to_pos, **to_array;
6723
6906
/* Alloc replace structure for the replace-state-machine */
6725
if ((replace=(REPLACE*) my_malloc(sizeof(REPLACE)*(sets.count)+
6726
sizeof(REPLACE_STRING)*(found_sets+1)+
6727
sizeof(char *)*count+result_len,
6728
MYF(MY_WME | MY_ZEROFILL))))
6908
if ((replace=(REPLACE*) malloc(sizeof(REPLACE)*(sets.count)+
6909
sizeof(REPLACE_STRING)*(found_sets+1)+
6910
sizeof(char *)*count+result_len)))
6912
memset(replace, 0, sizeof(REPLACE)*(sets.count)+
6913
sizeof(REPLACE_STRING)*(found_sets+1)+
6914
sizeof(char *)*count+result_len);
6730
6915
rep_str=(REPLACE_STRING*) (replace+sets.count);
6731
6916
to_array= (char **) (rep_str+found_sets+1);
6732
6917
to_pos=(char *) (to_array+count);
6733
6918
for (i=0 ; i < count ; i++)
6735
6920
to_array[i]=to_pos;
6736
to_pos=my_stpcpy(to_pos,to[i])+1;
6921
to_pos=strcpy(to_pos,to[i])+strlen(to[i])+1;
6738
6923
rep_str[0].found=1;
6739
6924
rep_str[0].replace_string=0;
6862
7044
void copy_bits(REP_SET *to,REP_SET *from)
6864
7046
memcpy(to->bits,from->bits,
6865
(size_t) (sizeof(uint) * to->size_of_bits));
7047
(size_t) (sizeof(uint32_t) * to->size_of_bits));
6868
7050
int cmp_bits(REP_SET *set1,REP_SET *set2)
6870
return memcmp(set1->bits,set2->bits, sizeof(uint) * set1->size_of_bits);
7052
return memcmp(set1->bits,set2->bits, sizeof(uint32_t) * set1->size_of_bits);
6874
7056
/* Get next set bit from set. */
6876
int get_next_bit(REP_SET *set,uint lastpos)
7058
int get_next_bit(REP_SET *set,uint32_t lastpos)
6878
uint pos,*start,*end,bits;
7060
uint32_t pos,*start,*end,bits;
6880
7062
start=set->bits+ ((lastpos+1) / WORD_BIT);
6881
7063
end=set->bits + set->size_of_bits;
7075
7255
replace_append_mem(ds, val, strlen(val));
7078
/* Append uint to ds, with optional replace */
7079
void replace_append_uint(string *ds, uint val)
7258
/* Append uint32_t to ds, with optional replace */
7259
void replace_append_uint(string *ds, uint32_t val)
7081
char buff[22]; /* This should be enough for any int */
7082
char *end= int64_t10_to_str(val, buff, 10);
7083
replace_append_mem(ds, buff, end - buff);
7263
replace_append_mem(ds, buff.str().c_str(), buff.str().size());