~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/mysqltest.c

  • Committer: Brian Aker
  • Date: 2008-07-02 04:17:52 UTC
  • Revision ID: brian@tangent.org-20080702041752-z6tikif3dquey6l4
First pass at cleaning up regex from mysqltest.c

Show diffs side-by-side

added added

removed removed

Lines of Context:
74
74
static my_bool debug_info_flag= 0, debug_check_flag= 0;
75
75
static my_bool tty_password= 0;
76
76
static my_bool opt_mark_progress= 0;
77
 
static my_bool ps_protocol= 0, ps_protocol_enabled= 0;
78
 
static my_bool sp_protocol= 0, sp_protocol_enabled= 0;
79
 
static my_bool view_protocol= 0, view_protocol_enabled= 0;
80
77
static my_bool cursor_protocol= 0, cursor_protocol_enabled= 0;
81
78
static my_bool parsing_disabled= 0;
82
79
static my_bool display_result_vertically= FALSE,
156
153
 
157
154
static ulonglong progress_start= 0;
158
155
 
159
 
/* Precompiled re's */
160
 
static my_regex_t ps_re;     /* the query can be run using PS protocol */
161
 
static my_regex_t sp_re;     /* the query can be run as a SP */
162
 
static my_regex_t view_re;   /* the query can be run as a view*/
163
 
 
164
 
static void init_re(void);
165
156
static int match_re(my_regex_t *, char *);
166
157
static void free_re(void);
167
158
 
320
311
  "start_timer",
321
312
  "end_timer",
322
313
  "character_set",
323
 
  "disable_ps_protocol",
324
 
  "enable_ps_protocol",
325
314
  "disable_reconnect",
326
315
  "enable_reconnect",
327
316
  "if",
4939
4928
  {"compress", 'C', "Use the compressed server/client protocol.",
4940
4929
   (uchar**) &opt_compress, (uchar**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
4941
4930
   0, 0, 0},
4942
 
  {"cursor-protocol", OPT_CURSOR_PROTOCOL, "Use cursors for prepared statements.",
4943
 
   (uchar**) &cursor_protocol, (uchar**) &cursor_protocol, 0,
4944
 
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
4945
4931
  {"database", 'D', "Database to use.", (uchar**) &opt_db, (uchar**) &opt_db, 0,
4946
4932
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
4947
4933
#ifdef DBUG_OFF
4981
4967
   "built-in default (" STRINGIFY_ARG(MYSQL_PORT) ").",
4982
4968
   (uchar**) &opt_port,
4983
4969
   (uchar**) &opt_port, 0, GET_INT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
4984
 
  {"protocol", OPT_MYSQL_PROTOCOL,
4985
 
    "The protocol of connection (tcp,socket,pipe,memory).",
4986
 
    0, 0, 0, GET_STR,  REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
4987
 
  {"ps-protocol", OPT_PS_PROTOCOL, "Use prepared statements protocol for communication",
4988
 
   (uchar**) &ps_protocol, (uchar**) &ps_protocol, 0,
4989
 
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
4990
4970
  {"quiet", 's', "Suppress all normal output.", (uchar**) &silent,
4991
4971
   (uchar**) &silent, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
4992
4972
  {"record", 'r', "Record output of test_file into result file.",
5003
4983
  {"sleep", 'T', "Sleep always this many seconds on sleep commands.",
5004
4984
   (uchar**) &opt_sleep, (uchar**) &opt_sleep, 0, GET_INT, REQUIRED_ARG, -1, -1, 0,
5005
4985
   0, 0, 0},
5006
 
  {"socket", 'S', "Socket file to use for connection.",
5007
 
   (uchar**) &unix_sock, (uchar**) &unix_sock, 0, GET_STR, REQUIRED_ARG, 0, 0, 0,
5008
 
   0, 0, 0},
5009
 
  {"sp-protocol", OPT_SP_PROTOCOL, "Use stored procedures for select",
5010
 
   (uchar**) &sp_protocol, (uchar**) &sp_protocol, 0,
5011
 
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
5012
4986
  {"tail-lines", OPT_TAIL_LINES,
5013
4987
   "Number of lines of the resul to include in a failure report",
5014
4988
   (uchar**) &opt_tail_lines, (uchar**) &opt_tail_lines, 0,
5025
4999
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
5026
5000
  {"version", 'V', "Output version information and exit.",
5027
5001
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
5028
 
  {"view-protocol", OPT_VIEW_PROTOCOL, "Use views for select",
5029
 
   (uchar**) &view_protocol, (uchar**) &view_protocol, 0,
5030
 
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
5031
5002
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
5032
5003
};
5033
5004
 
6106
6077
  DYNAMIC_STRING eval_query;
6107
6078
  char *query;
6108
6079
  int query_len;
6109
 
  my_bool view_created= 0, sp_created= 0;
6110
6080
  my_bool complete_query= ((flags & QUERY_SEND_FLAG) &&
6111
6081
                           (flags & QUERY_REAP_FLAG));
6112
6082
  DBUG_ENTER("run_query");
6156
6126
    dynstr_append_mem(ds, "\n", 1);
6157
6127
  }
6158
6128
 
6159
 
  if (view_protocol_enabled &&
6160
 
      complete_query &&
6161
 
      match_re(&view_re, query))
6162
 
  {
6163
 
    /*
6164
 
      Create the query as a view.
6165
 
      Use replace since view can exist from a failed mysqltest run
6166
 
    */
6167
 
    DYNAMIC_STRING query_str;
6168
 
    init_dynamic_string(&query_str,
6169
 
                        "CREATE OR REPLACE VIEW mysqltest_tmp_v AS ",
6170
 
                        query_len+64, 256);
6171
 
    dynstr_append_mem(&query_str, query, query_len);
6172
 
    if (util_query(mysql, query_str.str))
6173
 
    {
6174
 
      /*
6175
 
        Failed to create the view, this is not fatal
6176
 
        just run the query the normal way
6177
 
      */
6178
 
      DBUG_PRINT("view_create_error",
6179
 
                 ("Failed to create view '%s': %d: %s", query_str.str,
6180
 
                  mysql_errno(mysql), mysql_error(mysql)));
6181
 
 
6182
 
      /* Log error to create view */
6183
 
      verbose_msg("Failed to create view '%s' %d: %s", query_str.str,
6184
 
                  mysql_errno(mysql), mysql_error(mysql));
6185
 
    }
6186
 
    else
6187
 
    {
6188
 
      /*
6189
 
        Yes, it was possible to create this query as a view
6190
 
      */
6191
 
      view_created= 1;
6192
 
      query= (char*)"SELECT * FROM mysqltest_tmp_v";
6193
 
      query_len = strlen(query);
6194
 
 
6195
 
      /*
6196
 
        Collect warnings from create of the view that should otherwise
6197
 
        have been produced when the SELECT was executed
6198
 
      */
6199
 
      append_warnings(&ds_warnings, cur_con->util_mysql);
6200
 
    }
6201
 
 
6202
 
    dynstr_free(&query_str);
6203
 
 
6204
 
  }
6205
 
 
6206
 
  if (sp_protocol_enabled &&
6207
 
      complete_query &&
6208
 
      match_re(&sp_re, query))
6209
 
  {
6210
 
    /*
6211
 
      Create the query as a stored procedure
6212
 
      Drop first since sp can exist from a failed mysqltest run
6213
 
    */
6214
 
    DYNAMIC_STRING query_str;
6215
 
    init_dynamic_string(&query_str,
6216
 
                        "DROP PROCEDURE IF EXISTS mysqltest_tmp_sp;",
6217
 
                        query_len+64, 256);
6218
 
    util_query(mysql, query_str.str);
6219
 
    dynstr_set(&query_str, "CREATE PROCEDURE mysqltest_tmp_sp()\n");
6220
 
    dynstr_append_mem(&query_str, query, query_len);
6221
 
    if (util_query(mysql, query_str.str))
6222
 
    {
6223
 
      /*
6224
 
        Failed to create the stored procedure for this query,
6225
 
        this is not fatal just run the query the normal way
6226
 
      */
6227
 
      DBUG_PRINT("sp_create_error",
6228
 
                 ("Failed to create sp '%s': %d: %s", query_str.str,
6229
 
                  mysql_errno(mysql), mysql_error(mysql)));
6230
 
 
6231
 
      /* Log error to create sp */
6232
 
      verbose_msg("Failed to create sp '%s' %d: %s", query_str.str,
6233
 
                  mysql_errno(mysql), mysql_error(mysql));
6234
 
 
6235
 
    }
6236
 
    else
6237
 
    {
6238
 
      sp_created= 1;
6239
 
 
6240
 
      query= (char*)"CALL mysqltest_tmp_sp()";
6241
 
      query_len = strlen(query);
6242
 
    }
6243
 
    dynstr_free(&query_str);
6244
 
  }
6245
 
 
6246
6129
  if (display_result_sorted)
6247
6130
  {
6248
6131
    /*
6256
6139
  }
6257
6140
 
6258
6141
  /*
6259
 
    Find out how to run this query
6260
 
 
6261
6142
    Always run with normal C API if it's not a complete
6262
6143
    SEND + REAP
6263
 
 
6264
 
    If it is a '?' in the query it may be a SQL level prepared
6265
 
    statement already and we can't do it twice
6266
6144
  */
6267
 
  if (ps_protocol_enabled &&
6268
 
      complete_query &&
6269
 
      match_re(&ps_re, query))
6270
 
    run_query_stmt(mysql, command, query, query_len, ds, &ds_warnings);
6271
 
  else
6272
 
    run_query_normal(cn, command, flags, query, query_len,
6273
 
                     ds, &ds_warnings);
 
6145
  run_query_normal(cn, command, flags, query, query_len,
 
6146
                   ds, &ds_warnings);
6274
6147
 
6275
6148
  if (display_result_sorted)
6276
6149
  {
6280
6153
    dynstr_free(&ds_sorted);
6281
6154
  }
6282
6155
 
6283
 
  if (sp_created)
6284
 
  {
6285
 
    if (util_query(mysql, "DROP PROCEDURE mysqltest_tmp_sp "))
6286
 
      die("Failed to drop sp: %d: %s", mysql_errno(mysql), mysql_error(mysql));
6287
 
  }
6288
 
 
6289
 
  if (view_created)
6290
 
  {
6291
 
    if (util_query(mysql, "DROP VIEW mysqltest_tmp_v "))
6292
 
      die("Failed to drop view: %d: %s",
6293
 
          mysql_errno(mysql), mysql_error(mysql));
6294
 
  }
6295
 
 
6296
6156
  if (command->require_file[0])
6297
6157
  {
6298
6158
    /* A result file was specified for _this_ query
6337
6197
  }
6338
6198
}
6339
6199
 
6340
 
void init_re(void)
6341
 
{
6342
 
  /*
6343
 
    Filter for queries that can be run using the
6344
 
    MySQL Prepared Statements C API
6345
 
  */
6346
 
  const char *ps_re_str =
6347
 
    "^("
6348
 
    "[[:space:]]*REPLACE[[:space:]]|"
6349
 
    "[[:space:]]*INSERT[[:space:]]|"
6350
 
    "[[:space:]]*UPDATE[[:space:]]|"
6351
 
    "[[:space:]]*DELETE[[:space:]]|"
6352
 
    "[[:space:]]*SELECT[[:space:]]|"
6353
 
    "[[:space:]]*CREATE[[:space:]]+TABLE[[:space:]]|"
6354
 
    "[[:space:]]*DO[[:space:]]|"
6355
 
    "[[:space:]]*SET[[:space:]]+OPTION[[:space:]]|"
6356
 
    "[[:space:]]*DELETE[[:space:]]+MULTI[[:space:]]|"
6357
 
    "[[:space:]]*UPDATE[[:space:]]+MULTI[[:space:]]|"
6358
 
    "[[:space:]]*INSERT[[:space:]]+SELECT[[:space:]])";
6359
 
 
6360
 
  /*
6361
 
    Filter for queries that can be run using the
6362
 
    Stored procedures
6363
 
  */
6364
 
  const char *sp_re_str =ps_re_str;
6365
 
 
6366
 
  /*
6367
 
    Filter for queries that can be run as views
6368
 
  */
6369
 
  const char *view_re_str =
6370
 
    "^("
6371
 
    "[[:space:]]*SELECT[[:space:]])";
6372
 
 
6373
 
  init_re_comp(&ps_re, ps_re_str);
6374
 
  init_re_comp(&sp_re, sp_re_str);
6375
 
  init_re_comp(&view_re, view_re_str);
6376
 
}
6377
 
 
6378
 
 
6379
6200
int match_re(my_regex_t *re, char *str)
6380
6201
{
6381
6202
  int err= my_regexec(re, str, (size_t)0, NULL, 0);
6396
6217
 
6397
6218
void free_re(void)
6398
6219
{
6399
 
  my_regfree(&ps_re);
6400
 
  my_regfree(&sp_re);
6401
 
  my_regfree(&view_re);
6402
6220
  my_regex_end();
6403
6221
}
6404
6222
 
6586
6404
  init_dynamic_string(&ds_warning_messages, "", 0, 2048);
6587
6405
  parse_args(argc, argv);
6588
6406
 
6589
 
  var_set_int("$PS_PROTOCOL", ps_protocol);
6590
 
  var_set_int("$SP_PROTOCOL", sp_protocol);
6591
 
  var_set_int("$VIEW_PROTOCOL", view_protocol);
6592
 
  var_set_int("$CURSOR_PROTOCOL", cursor_protocol);
6593
 
 
6594
6407
  DBUG_PRINT("info",("result_file: '%s'",
6595
6408
                     result_file_name ? result_file_name : ""));
6596
6409
  if (mysql_server_init(embedded_server_arg_count,
6604
6417
    cur_file->file_name= my_strdup("<stdin>", MYF(MY_WME));
6605
6418
    cur_file->lineno= 1;
6606
6419
  }
6607
 
  init_re();
6608
 
  ps_protocol_enabled= ps_protocol;
6609
 
  sp_protocol_enabled= sp_protocol;
6610
 
  view_protocol_enabled= view_protocol;
6611
 
  cursor_protocol_enabled= cursor_protocol;
6612
 
  /* Cursor protcol implies ps protocol */
6613
 
  if (cursor_protocol_enabled)
6614
 
    ps_protocol_enabled= 1;
6615
 
 
6616
6420
  cur_con= connections;
6617
6421
  if (!( mysql_init(&cur_con->mysql)))
6618
6422
    die("Failed in mysql_init()");
6862
6666
      case Q_CHARACTER_SET:
6863
6667
        do_set_charset(command);
6864
6668
        break;
6865
 
      case Q_DISABLE_PS_PROTOCOL:
6866
 
        ps_protocol_enabled= 0;
6867
 
        /* Close any open statements */
6868
 
        close_statements();
6869
 
        break;
6870
 
      case Q_ENABLE_PS_PROTOCOL:
6871
 
        ps_protocol_enabled= ps_protocol;
6872
 
        break;
6873
6669
      case Q_DISABLE_RECONNECT:
6874
6670
        set_reconnect(&cur_con->mysql, 0);
6875
6671
        break;