~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzle.cc

  • Committer: Brian Aker
  • Date: 2009-03-30 18:13:06 UTC
  • mfrom: (968.1.1 lib-merge)
  • Revision ID: brian@tangent.org-20090330181306-hzodbge1b0v57puh
Merge of Eric's libdrizzle work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
99
99
    /* no history */
100
100
#endif /* HAVE_READLINE_HISTORY */
101
101
 
 
102
#define DRIZZLE_DEFAULT_INPUT_LINE 65536
 
103
 
102
104
/**
103
105
 Make the old readline interface look like the new one.
104
106
*/
161
163
enum enum_info_type { INFO_INFO,INFO_ERROR,INFO_RESULT};
162
164
typedef enum enum_info_type INFO_TYPE;
163
165
 
164
 
static DRIZZLE drizzle;      /* The connection */
 
166
static drizzle_st drizzle;      /* The library handle */
 
167
static drizzle_con_st con;      /* The connection */
165
168
static bool ignore_errors= false, quick= false,
166
169
  connected= false, opt_raw_data= false, unbuffered= false,
167
170
  output_tables= false, opt_rehash= true, skip_updates= false,
178
181
static bool debug_info_flag, debug_check_flag;
179
182
static bool column_types_flag;
180
183
static bool preserve_comments= false;
181
 
static uint32_t opt_max_allowed_packet, opt_net_buffer_length,
182
 
  opt_drizzle_port= 0;
 
184
static uint32_t opt_max_input_line, opt_drizzle_port= 0;
183
185
static int verbose= 0, opt_silent= 0, opt_local_infile= 0;
184
186
static uint32_t my_end_arg;
185
187
static char * opt_drizzle_unix_port= NULL;
186
 
static int connect_flag= 0;
 
188
static drizzle_capabilities_t connect_flag= DRIZZLE_CAPABILITIES_NONE;
187
189
static char *current_host, *current_db, *current_user= NULL,
188
190
  *opt_password= NULL, *delimiter_str= NULL, *current_prompt= NULL;
189
191
static char *histfile;
210
212
 
211
213
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
212
214
 
213
 
int drizzleclient_real_query_for_lazy(const char *buf, int length);
214
 
int drizzleclient_store_result_for_lazy(DRIZZLE_RES **result);
 
215
int drizzleclient_real_query_for_lazy(const char *buf, int length,
 
216
                                      drizzle_result_st *result,
 
217
                                      uint32_t *error_code);
 
218
int drizzleclient_store_result_for_lazy(drizzle_result_st *result);
215
219
 
216
220
 
217
221
void tee_fprintf(FILE *file, const char *fmt, ...);
238
242
static int read_and_execute(bool interactive);
239
243
static int sql_connect(char *host,char *database,char *user,char *password,
240
244
                       uint32_t silent);
241
 
static const char *server_version_string(DRIZZLE *drizzle);
 
245
static const char *server_version_string(drizzle_con_st *con);
242
246
static int put_info(const char *str,INFO_TYPE info,uint32_t error,
243
247
                    const char *sql_state);
244
 
static int put_error(DRIZZLE *drizzle);
 
248
static int put_error(drizzle_con_st *con, drizzle_result_st *res);
245
249
static void safe_put_field(const char *pos,uint32_t length);
246
250
static void init_pager(void);
247
251
static void end_pager(void);
251
255
static char *get_arg(char *line, bool get_next_arg);
252
256
static void init_username(void);
253
257
static void add_int_to_prompt(int toadd);
254
 
static int get_result_width(DRIZZLE_RES *res);
255
 
static int get_field_disp_length(DRIZZLE_FIELD * field);
 
258
static int get_result_width(drizzle_result_st *res);
 
259
static int get_field_disp_length(drizzle_column_st * field);
256
260
static const char * strcont(register const char *str, register const char *set);
257
261
 
258
262
/* A structure which contains information on the commands this program
1002
1006
static bool add_line(string *buffer,char *line,char *in_string,
1003
1007
                     bool *ml_comment);
1004
1008
static void remove_cntrl(string *buffer);
1005
 
static void print_table_data(DRIZZLE_RES *result);
1006
 
static void print_tab_data(DRIZZLE_RES *result);
1007
 
static void print_table_data_vertically(DRIZZLE_RES *result);
1008
 
static void print_warnings(void);
 
1009
static void print_table_data(drizzle_result_st *result);
 
1010
static void print_tab_data(drizzle_result_st *result);
 
1011
static void print_table_data_vertically(drizzle_result_st *result);
 
1012
static void print_warnings(uint32_t error_code);
1009
1013
static uint32_t start_timer(void);
1010
1014
static void end_timer(uint32_t start_time,char *buff);
1011
1015
static void drizzle_end_timer(uint32_t start_time,char *buff);
1094
1098
    exit(1);
1095
1099
  }
1096
1100
  if (status.batch && !status.line_buff &&
1097
 
      !(status.line_buff=batch_readline_init(opt_max_allowed_packet+512,stdin)))
 
1101
      !(status.line_buff=batch_readline_init(opt_max_input_line+512,stdin)))
1098
1102
  {
1099
1103
    free_defaults(defaults_argv);
1100
1104
    my_end(0);
1135
1139
 
1136
1140
  sprintf(output_buff,
1137
1141
          _("Your Drizzle connection id is %u\nServer version: %s\n"),
1138
 
          drizzleclient_thread_id(&drizzle),
1139
 
          server_version_string(&drizzle));
 
1142
          drizzle_con_thread_id(&con),
 
1143
          server_version_string(&con));
1140
1144
  put_info(output_buff, INFO_INFO, 0, 0);
1141
1145
 
1142
1146
  initialize_readline(current_prompt);
1186
1190
 
1187
1191
void drizzle_end(int sig)
1188
1192
{
1189
 
  drizzleclient_close(&drizzle);
 
1193
  drizzle_con_free(&con);
 
1194
  drizzle_free(&drizzle);
1190
1195
  if (!status.batch && !quick && histfile)
1191
1196
  {
1192
1197
    /* write-history */
1229
1234
void handle_sigint(int sig)
1230
1235
{
1231
1236
  char kill_buffer[40];
1232
 
  DRIZZLE *kill_drizzle= NULL;
 
1237
  drizzle_con_st kill_drizzle;
 
1238
  drizzle_result_st res;
 
1239
  drizzle_return_t ret;
1233
1240
 
1234
1241
  /* terminate if no query being executed, or we already tried interrupting */
1235
1242
  if (!executing_query || interrupted_query) {
1236
1243
    goto err;
1237
1244
  }
1238
1245
 
1239
 
  kill_drizzle= drizzleclient_create(kill_drizzle);
1240
 
  if (!drizzleclient_connect(kill_drizzle,current_host, current_user, opt_password,
1241
 
                          "", opt_drizzle_port, opt_drizzle_unix_port,0))
 
1246
  if (drizzle_con_add_tcp(&drizzle, &kill_drizzle, current_host,
 
1247
                          opt_drizzle_port, current_user, opt_password, NULL,
 
1248
                          DRIZZLE_CON_NONE) == NULL)
1242
1249
  {
1243
1250
    goto err;
1244
1251
  }
1245
1252
 
1246
1253
  /* kill_buffer is always big enough because max length of %lu is 15 */
1247
 
  sprintf(kill_buffer, "KILL /*!50000 QUERY */ %u", drizzleclient_thread_id(&drizzle));
1248
 
  drizzleclient_real_query(kill_drizzle, kill_buffer, strlen(kill_buffer));
1249
 
  drizzleclient_close(kill_drizzle);
 
1254
  sprintf(kill_buffer, "KILL /*!50000 QUERY */ %u",
 
1255
          drizzle_con_thread_id(&con));
 
1256
 
 
1257
  if (drizzle_query_str(&kill_drizzle, &res, kill_buffer, &ret) != NULL)
 
1258
    drizzle_result_free(&res);
 
1259
 
 
1260
  drizzle_con_free(&kill_drizzle);
1250
1261
  tee_fprintf(stdout, _("Query aborted by Ctrl+C\n"));
1251
1262
 
1252
1263
  interrupted_query= 1;
1279
1290
   (char**) &opt_rehash, (char**) &opt_rehash, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0,
1280
1291
   0, 0},
1281
1292
  {"no-auto-rehash", 'A',
1282
 
   N_("No automatic rehashing. One has to use 'rehash' to get table and field completion. This gives a quicker start of DRIZZLE and disables rehashing on reconnect. WARNING: options deprecated; use --disable-auto-rehash instead."),
 
1293
   N_("No automatic rehashing. One has to use 'rehash' to get table and field completion. This gives a quicker start of drizzle_st and disables rehashing on reconnect. WARNING: options deprecated; use --disable-auto-rehash instead."),
1283
1294
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1284
1295
  {"auto-vertical-output", OPT_AUTO_VERTICAL_OUTPUT,
1285
1296
   N_("Automatically switch to vertical output mode if the result is wider than the terminal width."),
1409
1420
   (char**) &opt_connect_timeout,
1410
1421
   (char**) &opt_connect_timeout, 0, GET_UINT32, REQUIRED_ARG, 0, 0, 3600*12, 0,
1411
1422
   0, 0},
1412
 
  {"max_allowed_packet", OPT_MAX_ALLOWED_PACKET,
1413
 
   N_("Max packet length to send to, or receive from server"),
1414
 
   (char**) &opt_max_allowed_packet, (char**) &opt_max_allowed_packet, 0,
 
1423
  {"max_input_line", OPT_MAX_INPUT_LINE,
 
1424
   N_("Max length of input line"),
 
1425
   (char**) &opt_max_input_line, (char**) &opt_max_input_line, 0,
1415
1426
   GET_UINT32, REQUIRED_ARG, 16 *1024L*1024L, 4096,
1416
1427
   (int64_t) 2*1024L*1024L*1024L, MALLOC_OVERHEAD, 1024, 0},
1417
 
  {"net_buffer_length", OPT_NET_BUFFER_LENGTH,
1418
 
   N_("Buffer for TCP/IP and socket communication"),
1419
 
   (char**) &opt_net_buffer_length, (char**) &opt_net_buffer_length, 0, GET_UINT32,
1420
 
   REQUIRED_ARG, 16384, 1024, 512*1024*1024L, MALLOC_OVERHEAD, 1024, 0},
1421
1428
  {"select_limit", OPT_SELECT_LIMIT,
1422
1429
   N_("Automatic limit for SELECT when using --safe-updates"),
1423
1430
   (char**) &select_limit,
1445
1452
  const char* readline= "readline";
1446
1453
 
1447
1454
  printf(_("%s  Ver %s Distrib %s, for %s (%s) using %s %s\n"),
1448
 
         my_progname, VER.c_str(), drizzleclient_get_client_info(),
 
1455
         my_progname, VER.c_str(), drizzle_version(),
1449
1456
         SYSTEM_TYPE, MACHINE_TYPE,
1450
1457
         readline, rl_library_version);
1451
1458
 
1635
1642
{
1636
1643
  char *tmp, *pagpoint;
1637
1644
  int ho_error;
1638
 
  const DRIZZLE_PARAMETERS *drizzle_params= drizzleclient_get_parameters();
1639
1645
 
1640
1646
  tmp= (char *) getenv("DRIZZLE_HOST");
1641
1647
  if (tmp)
1651
1657
    strcpy(pager, pagpoint);
1652
1658
  strcpy(default_pager, pager);
1653
1659
 
1654
 
  opt_max_allowed_packet= *drizzle_params->p_max_allowed_packet;
1655
 
  opt_net_buffer_length= *drizzle_params->p_net_buffer_length;
1656
 
 
1657
1660
  if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
1658
1661
    exit(ho_error);
1659
1662
 
1660
 
  *drizzle_params->p_max_allowed_packet= opt_max_allowed_packet;
1661
 
  *drizzle_params->p_net_buffer_length= opt_net_buffer_length;
1662
 
 
1663
1663
  if (status.batch) /* disable pager and outfile in this case */
1664
1664
  {
1665
1665
    strcpy(default_pager, "stdout");
1668
1668
    default_pager_set= 0;
1669
1669
    opt_outfile= 0;
1670
1670
    opt_reconnect= 0;
1671
 
    connect_flag= 0; /* Not in interactive mode */
 
1671
    connect_flag= DRIZZLE_CAPABILITIES_NONE; /* Not in interactive mode */
1672
1672
  }
1673
1673
 
1674
1674
  if (argc > 1)
1683
1683
    current_db= strdup(*argv);
1684
1684
  }
1685
1685
  if (tty_password)
1686
 
    opt_password= drizzleclient_get_tty_password(NULL);
 
1686
    opt_password= client_get_tty_password(NULL);
1687
1687
  if (debug_info_flag)
1688
1688
    my_end_arg= MY_CHECK_ERROR | MY_GIVE_INFO;
1689
1689
  if (debug_check_flag)
1885
1885
    }
1886
1886
#endif
1887
1887
        if (!*ml_comment && inchar == '\\' &&
1888
 
            !(*in_string && (drizzle.server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES)))
 
1888
            !(*in_string && (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_NO_BACKSLASH_ESCAPES)))
1889
1889
    {
1890
1890
      // Found possbile one character command like \c
1891
1891
 
2286
2286
static void build_completion_hash(bool rehash, bool write_info)
2287
2287
{
2288
2288
  COMMANDS *cmd=commands;
2289
 
  DRIZZLE_RES *databases=0,*tables=0;
2290
 
  DRIZZLE_RES *fields;
2291
 
  DRIZZLE_ROW database_row,table_row;
2292
 
  DRIZZLE_FIELD *sql_field;
 
2289
  drizzle_return_t ret;
 
2290
  drizzle_result_st databases,tables,fields;
 
2291
  drizzle_row_t database_row,table_row;
 
2292
  drizzle_column_st *sql_field;
2293
2293
  string tmp_str, tmp_str_lower;
2294
2294
 
2295
 
 
2296
2295
  if (status.batch || quick || !current_db)
2297
2296
    return;      // We don't need completion in batches
2298
2297
  if (!rehash)
2311
2310
  /* hash Drizzle functions (to be implemented) */
2312
2311
 
2313
2312
  /* hash all database names */
2314
 
  if (drizzleclient_query(&drizzle,"show databases") == 0)
 
2313
  if (drizzle_query_str(&con, &databases, "show databases", &ret) != NULL)
2315
2314
  {
2316
 
    if (!(databases = drizzleclient_store_result(&drizzle)))
2317
 
      put_info(drizzleclient_error(&drizzle),INFO_INFO,0,0);
2318
 
    else
 
2315
    if (ret == DRIZZLE_RETURN_OK)
2319
2316
    {
2320
 
      while ((database_row=drizzleclient_fetch_row(databases)))
 
2317
      if (drizzle_result_buffer(&databases) != DRIZZLE_RETURN_OK)
 
2318
        put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
 
2319
      else
2321
2320
      {
2322
 
        tmp_str= database_row[0];
2323
 
        tmp_str_lower= lower_string(tmp_str);
2324
 
        completion_map[tmp_str_lower]= tmp_str;
 
2321
        while ((database_row=drizzle_row_next(&databases)))
 
2322
        {
 
2323
          tmp_str= database_row[0];
 
2324
          tmp_str_lower= lower_string(tmp_str);
 
2325
          completion_map[tmp_str_lower]= tmp_str;
 
2326
        }
2325
2327
      }
2326
 
      drizzleclient_free_result(databases);
2327
2328
    }
 
2329
 
 
2330
    drizzle_result_free(&databases);
2328
2331
  }
 
2332
 
2329
2333
  /* hash all table names */
2330
 
  if (drizzleclient_query(&drizzle,"show tables")==0)
 
2334
  if (drizzle_query_str(&con, &tables, "show tables", &ret) != NULL)
2331
2335
  {
2332
 
    if (!(tables = drizzleclient_store_result(&drizzle)))
2333
 
      put_info(drizzleclient_error(&drizzle),INFO_INFO,0,0);
 
2336
    if (ret != DRIZZLE_RETURN_OK)
 
2337
    {
 
2338
      drizzle_result_free(&tables);
 
2339
      return;
 
2340
    }
 
2341
 
 
2342
    if (drizzle_result_buffer(&tables) != DRIZZLE_RETURN_OK)
 
2343
      put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2334
2344
    else
2335
2345
    {
2336
 
      if (drizzleclient_num_rows(tables) > 0 && !opt_silent && write_info)
 
2346
      if (drizzle_result_row_count(&tables) > 0 && !opt_silent && write_info)
2337
2347
      {
2338
2348
        tee_fprintf(stdout, _("\
2339
2349
Reading table information for completion of table and column names\n    \
2340
2350
You can turn off this feature to get a quicker startup with -A\n\n"));
2341
2351
      }
2342
 
      while ((table_row=drizzleclient_fetch_row(tables)))
 
2352
      while ((table_row=drizzle_row_next(&tables)))
2343
2353
      {
2344
2354
        tmp_str= table_row[0];
2345
2355
        tmp_str_lower= lower_string(tmp_str);
2347
2357
      }
2348
2358
    }
2349
2359
  }
 
2360
  else
 
2361
    return;
2350
2362
 
2351
2363
  /* hash all field names, both with the table prefix and without it */
2352
 
  if (!tables)          /* no tables */
 
2364
  if (drizzle_result_row_count(&tables) == 0)
2353
2365
  {
 
2366
    drizzle_result_free(&tables);
2354
2367
    return;
2355
2368
  }
2356
 
  drizzleclient_data_seek(tables,0);
2357
 
 
2358
 
  while ((table_row=drizzleclient_fetch_row(tables)))
 
2369
 
 
2370
  drizzle_row_seek(&tables, 0);
 
2371
 
 
2372
  while ((table_row=drizzle_row_next(&tables)))
2359
2373
  {
2360
2374
    string query;
2361
2375
 
2362
 
    query.append("show fields in `");
 
2376
    query.append("show fields in '");
2363
2377
    query.append(table_row[0]);
2364
 
    query.append("`");
 
2378
    query.append("'");
2365
2379
    
2366
 
    if (drizzleclient_query(&drizzle, query.c_str()) == 0)
 
2380
    if (drizzle_query(&con, &fields, query.c_str(), query.length(),
 
2381
                      &ret) != NULL)
2367
2382
    {
2368
 
      fields= drizzleclient_store_result(&drizzle);
2369
 
      if (fields) 
 
2383
      if (ret == DRIZZLE_RETURN_OK &&
 
2384
          drizzle_result_buffer(&fields) == DRIZZLE_RETURN_OK)
2370
2385
      {
2371
 
        while ((sql_field=drizzleclient_fetch_field(fields)))
 
2386
        while ((sql_field=drizzle_column_next(&fields)))
2372
2387
        {
2373
2388
          tmp_str=table_row[0];
2374
2389
          tmp_str.append(".");
2375
 
          tmp_str.append(sql_field->name);
2376
 
          tmp_str_lower= lower_string(tmp_str);
2377
 
          completion_map[tmp_str_lower]= tmp_str;
2378
 
 
2379
 
          tmp_str=sql_field->name;
2380
 
          tmp_str_lower= lower_string(tmp_str);
2381
 
          completion_map[tmp_str_lower]= tmp_str;
2382
 
 
 
2390
          tmp_str.append(drizzle_column_name(sql_field));
 
2391
          tmp_str_lower= lower_string(tmp_str);
 
2392
          completion_map[tmp_str_lower]= tmp_str;
 
2393
 
 
2394
          tmp_str=drizzle_column_name(sql_field);
 
2395
          tmp_str_lower= lower_string(tmp_str);
 
2396
          completion_map[tmp_str_lower]= tmp_str;
2383
2397
        }
2384
 
        drizzleclient_free_result(fields);
2385
2398
      }
 
2399
      drizzle_result_free(&fields);
2386
2400
    }
2387
2401
  }
2388
 
  drizzleclient_free_result(tables);
 
2402
  drizzle_result_free(&tables);
2389
2403
  completion_iter= completion_map.begin();
2390
2404
}
2391
2405
 
2434
2448
 
2435
2449
static void get_current_db(void)
2436
2450
{
2437
 
  DRIZZLE_RES *res;
 
2451
  drizzle_return_t ret;
 
2452
  drizzle_result_st res;
2438
2453
 
2439
2454
  free(current_db);
2440
2455
  current_db= NULL;
2441
2456
  /* In case of error below current_db will be NULL */
2442
 
  if (!drizzleclient_query(&drizzle, "SELECT DATABASE()") &&
2443
 
      (res= drizzleclient_use_result(&drizzle)))
 
2457
  if (drizzle_query_str(&con, &res, "SELECT DATABASE()", &ret) != NULL)
2444
2458
  {
2445
 
    DRIZZLE_ROW row= drizzleclient_fetch_row(res);
2446
 
    if (row[0])
2447
 
      current_db= strdup(row[0]);
2448
 
    drizzleclient_free_result(res);
 
2459
    if (ret == DRIZZLE_RETURN_OK &&
 
2460
        drizzle_result_buffer(&res) == DRIZZLE_RETURN_OK)
 
2461
    {
 
2462
      drizzle_row_t row= drizzle_row_next(&res);
 
2463
      if (row[0])
 
2464
        current_db= strdup(row[0]);
 
2465
    }
 
2466
    drizzle_result_free(&res);
2449
2467
  }
2450
2468
}
2451
2469
 
2453
2471
 The different commands
2454
2472
***************************************************************************/
2455
2473
 
2456
 
int drizzleclient_real_query_for_lazy(const char *buf, int length)
 
2474
int drizzleclient_real_query_for_lazy(const char *buf, int length,
 
2475
                                      drizzle_result_st *result,
 
2476
                                      uint32_t *error_code)
2457
2477
{
 
2478
  drizzle_return_t ret;
 
2479
 
2458
2480
  for (uint32_t retry=0;; retry++)
2459
2481
  {
2460
2482
    int error;
2461
 
    if (!drizzleclient_real_query(&drizzle,buf,length))
 
2483
    if (drizzle_query(&con,result,buf,length,&ret) != NULL &&
 
2484
        ret == DRIZZLE_RETURN_OK)
 
2485
    {
2462
2486
      return 0;
2463
 
    error= put_error(&drizzle);
2464
 
    if (drizzleclient_errno(&drizzle) != CR_SERVER_GONE_ERROR || retry > 1 ||
 
2487
    }
 
2488
    error= put_error(&con, result);
 
2489
 
 
2490
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
2491
    {
 
2492
      *error_code= drizzle_result_error_code(result);
 
2493
      drizzle_result_free(result);
 
2494
    }
 
2495
 
 
2496
    if (ret != DRIZZLE_RETURN_SERVER_GONE || retry > 1 ||
2465
2497
        !opt_reconnect)
 
2498
    {
2466
2499
      return error;
 
2500
    }
 
2501
 
2467
2502
    if (reconnect())
2468
2503
      return error;
2469
2504
  }
2470
2505
}
2471
2506
 
2472
 
int drizzleclient_store_result_for_lazy(DRIZZLE_RES **result)
 
2507
int drizzleclient_store_result_for_lazy(drizzle_result_st *result)
2473
2508
{
2474
 
  if ((*result=drizzleclient_store_result(&drizzle)))
 
2509
  if (drizzle_result_buffer(result) == DRIZZLE_RETURN_OK)
2475
2510
    return 0;
2476
2511
 
2477
 
  if (drizzleclient_error(&drizzle)[0])
2478
 
    return put_error(&drizzle);
 
2512
  if (drizzle_con_error(&con)[0])
 
2513
    return put_error(&con, result);
2479
2514
  return 0;
2480
2515
}
2481
2516
 
2525
2560
{
2526
2561
  char          buff[200]; /* about 110 chars used so far */
2527
2562
  char          time_buff[52+3+1]; /* time max + space&parens + NUL */
2528
 
  DRIZZLE_RES     *result;
2529
 
  uint32_t         timer, warnings= 0;
2530
 
  uint32_t          error= 0;
 
2563
  drizzle_result_st result;
 
2564
  drizzle_return_t ret;
 
2565
  uint32_t      timer, warnings= 0;
 
2566
  uint32_t      error= 0;
 
2567
  uint32_t      error_code= 0;
2531
2568
  int           err= 0;
2532
2569
 
2533
2570
  interrupted_query= 0;
2561
2598
 
2562
2599
  timer=start_timer();
2563
2600
  executing_query= 1;
2564
 
  error= drizzleclient_real_query_for_lazy(buffer->c_str(),buffer->length());
 
2601
  error= drizzleclient_real_query_for_lazy(buffer->c_str(),buffer->length(),&result, &error_code);
2565
2602
 
2566
2603
  if (status.add_to_history)
2567
2604
  {
2581
2618
 
2582
2619
    if (quick)
2583
2620
    {
2584
 
      if (!(result=drizzleclient_use_result(&drizzle)) && drizzleclient_field_count(&drizzle))
 
2621
      if (drizzle_column_buffer(&result) != DRIZZLE_RETURN_OK)
2585
2622
      {
2586
 
        error= put_error(&drizzle);
 
2623
        error= put_error(&con, &result);
2587
2624
        goto end;
2588
2625
      }
2589
2626
    }
2600
2637
      time_buff[0]= '\0';
2601
2638
 
2602
2639
    /* Every branch must truncate  buff . */
2603
 
    if (result)
 
2640
    if (drizzle_result_column_count(&result) > 0)
2604
2641
    {
2605
 
      if (!drizzleclient_num_rows(result) && ! quick && !column_types_flag)
 
2642
      if (!quick && drizzle_result_row_count(&result) == 0 &&
 
2643
          !column_types_flag)
2606
2644
      {
2607
2645
        strcpy(buff, _("Empty set"));
2608
2646
      }
2610
2648
      {
2611
2649
        init_pager();
2612
2650
        if (vertical || (auto_vertical_output &&
2613
 
                         (terminal_width < get_result_width(result))))
2614
 
          print_table_data_vertically(result);
 
2651
                         (terminal_width < get_result_width(&result))))
 
2652
          print_table_data_vertically(&result);
2615
2653
        else if (opt_silent && verbose <= 2 && !output_tables)
2616
 
          print_tab_data(result);
 
2654
          print_tab_data(&result);
2617
2655
        else
2618
 
          print_table_data(result);
 
2656
          print_table_data(&result);
2619
2657
        sprintf(buff,
2620
2658
                ngettext("%ld row in set","%ld rows in set",
2621
 
                         (long) drizzleclient_num_rows(result)),
2622
 
                (long) drizzleclient_num_rows(result));
 
2659
                         (long) drizzle_result_row_count(&result)),
 
2660
                (long) drizzle_result_row_count(&result));
2623
2661
        end_pager();
2624
 
        if (drizzleclient_errno(&drizzle))
2625
 
          error= put_error(&drizzle);
 
2662
        if (drizzle_result_error_code(&result))
 
2663
          error= put_error(&con, &result);
2626
2664
      }
2627
2665
    }
2628
 
    else if (drizzleclient_affected_rows(&drizzle) == ~(uint64_t) 0)
 
2666
    else if (drizzle_result_affected_rows(&result) == ~(uint64_t) 0)
2629
2667
      strcpy(buff,_("Query OK"));
2630
2668
    else
2631
2669
      sprintf(buff, ngettext("Query OK, %ld row affected",
2632
2670
                             "Query OK, %ld rows affected",
2633
 
                             (long) drizzleclient_affected_rows(&drizzle)),
2634
 
              (long) drizzleclient_affected_rows(&drizzle));
 
2671
                             (long) drizzle_result_affected_rows(&result)),
 
2672
              (long) drizzle_result_affected_rows(&result));
2635
2673
 
2636
2674
    pos= strchr(buff, '\0');
2637
 
    if ((warnings= drizzleclient_warning_count(&drizzle)))
 
2675
    if ((warnings= drizzle_result_warning_count(&result)))
2638
2676
    {
2639
2677
      *pos++= ',';
2640
2678
      *pos++= ' ';
2645
2683
    }
2646
2684
    strcpy(pos, time_buff);
2647
2685
    put_info(buff,INFO_RESULT,0,0);
2648
 
    if (drizzleclient_info(&drizzle))
2649
 
      put_info(drizzleclient_info(&drizzle),INFO_RESULT,0,0);
 
2686
    if (strcmp(drizzle_result_info(&result), ""))
 
2687
      put_info(drizzle_result_info(&result),INFO_RESULT,0,0);
2650
2688
    put_info("",INFO_RESULT,0,0);      // Empty row
2651
2689
 
2652
 
    if (result && !drizzleclient_eof(result))  /* Something wrong when using quick */
2653
 
      error= put_error(&drizzle);
2654
 
    else if (unbuffered)
 
2690
    if (unbuffered)
2655
2691
      fflush(stdout);
2656
 
    drizzleclient_free_result(result);
2657
 
  } while (!(err= drizzleclient_next_result(&drizzle)));
 
2692
    drizzle_result_free(&result);
 
2693
 
 
2694
    if (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS)
 
2695
    {
 
2696
      if (drizzle_result_read(&con, &result, &ret) == NULL ||
 
2697
          ret != DRIZZLE_RETURN_OK)
 
2698
      {
 
2699
        if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
2700
        {
 
2701
          error_code= drizzle_result_error_code(&result);
 
2702
          drizzle_result_free(&result);
 
2703
        }
 
2704
 
 
2705
        error= put_error(&con, NULL);
 
2706
        goto end;
 
2707
      }
 
2708
    }
 
2709
 
 
2710
  } while (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS);
2658
2711
  if (err >= 1)
2659
 
    error= put_error(&drizzle);
 
2712
    error= put_error(&con, NULL);
2660
2713
 
2661
2714
end:
2662
2715
 
2663
2716
  /* Show warnings if any or error occured */
2664
2717
  if (show_warnings == 1 && (warnings >= 1 || error))
2665
 
    print_warnings();
 
2718
    print_warnings(error_code);
2666
2719
 
2667
2720
  if (!error && !status.batch &&
2668
 
      (drizzle.server_status & SERVER_STATUS_DB_DROPPED))
 
2721
      drizzle_con_status(&con) & DRIZZLE_CON_STATUS_DB_DROPPED)
 
2722
  {
2669
2723
    get_current_db();
 
2724
  }
2670
2725
 
2671
2726
  executing_query= 0;
2672
2727
  return error;        /* New command follows */
2734
2789
}
2735
2790
 
2736
2791
 
2737
 
static const char *fieldtype2str(enum enum_field_types type)
 
2792
static const char *fieldtype2str(drizzle_column_type_t type)
2738
2793
{
2739
2794
  switch (type) {
2740
 
    case DRIZZLE_TYPE_BLOB:        return "BLOB";
2741
 
    case DRIZZLE_TYPE_DATE:        return "DATE";
2742
 
    case DRIZZLE_TYPE_DATETIME:    return "DATETIME";
2743
 
    case DRIZZLE_TYPE_NEWDECIMAL:  return "DECIMAL";
2744
 
    case DRIZZLE_TYPE_DOUBLE:      return "DOUBLE";
2745
 
    case DRIZZLE_TYPE_ENUM:        return "ENUM";
2746
 
    case DRIZZLE_TYPE_LONG:        return "LONG";
2747
 
    case DRIZZLE_TYPE_LONGLONG:    return "LONGLONG";
2748
 
    case DRIZZLE_TYPE_NULL:        return "NULL";
2749
 
    case DRIZZLE_TYPE_TIMESTAMP:   return "TIMESTAMP";
2750
 
    case DRIZZLE_TYPE_TINY:        return "TINY";
2751
 
    case DRIZZLE_TYPE_VIRTUAL:     return "VIRTUAL";
 
2795
    case DRIZZLE_COLUMN_TYPE_BLOB:        return "BLOB";
 
2796
    case DRIZZLE_COLUMN_TYPE_DATE:        return "DATE";
 
2797
    case DRIZZLE_COLUMN_TYPE_DATETIME:    return "DATETIME";
 
2798
    case DRIZZLE_COLUMN_TYPE_NEWDECIMAL:  return "DECIMAL";
 
2799
    case DRIZZLE_COLUMN_TYPE_DOUBLE:      return "DOUBLE";
 
2800
    case DRIZZLE_COLUMN_TYPE_ENUM:        return "ENUM";
 
2801
    case DRIZZLE_COLUMN_TYPE_LONG:        return "LONG";
 
2802
    case DRIZZLE_COLUMN_TYPE_LONGLONG:    return "LONGLONG";
 
2803
    case DRIZZLE_COLUMN_TYPE_NULL:        return "NULL";
 
2804
    case DRIZZLE_COLUMN_TYPE_TIMESTAMP:   return "TIMESTAMP";
 
2805
    case DRIZZLE_COLUMN_TYPE_TINY:        return "TINY";
 
2806
    case DRIZZLE_COLUMN_TYPE_VIRTUAL:     return "VIRTUAL";
2752
2807
    default:                     return "?-unknown-?";
2753
2808
  }
2754
2809
}
2758
2813
  char *s=buf;
2759
2814
  *s=0;
2760
2815
#define ff2s_check_flag(X)                                              \
2761
 
  if (f & X ## _FLAG) { s=strcpy(s, # X " ")+strlen(# X " "); \
2762
 
                        f &= ~ X ## _FLAG; }
 
2816
  if (f & DRIZZLE_COLUMN_FLAGS_ ## X) { s=strcpy(s, # X " ")+strlen(# X " "); \
 
2817
                        f &= ~ DRIZZLE_COLUMN_FLAGS_ ## X; }
2763
2818
  ff2s_check_flag(NOT_NULL);
2764
2819
  ff2s_check_flag(PRI_KEY);
2765
2820
  ff2s_check_flag(UNIQUE_KEY);
2785
2840
}
2786
2841
 
2787
2842
static void
2788
 
print_field_types(DRIZZLE_RES *result)
 
2843
print_field_types(drizzle_result_st *result)
2789
2844
{
2790
 
  DRIZZLE_FIELD   *field;
 
2845
  drizzle_column_st   *field;
2791
2846
  uint32_t i=0;
2792
2847
 
2793
 
  while ((field = drizzleclient_fetch_field(result)))
 
2848
  while ((field = drizzle_column_next(result)))
2794
2849
  {
2795
2850
    tee_fprintf(PAGER, "Field %3u:  `%s`\n"
2796
2851
                "Catalog:    `%s`\n"
2804
2859
                "Decimals:   %u\n"
2805
2860
                "Flags:      %s\n\n",
2806
2861
                ++i,
2807
 
                field->name, field->catalog, field->db, field->table,
2808
 
                field->org_table, fieldtype2str(field->type),
2809
 
                get_charset_name(field->charsetnr), field->charsetnr,
2810
 
                field->length, field->max_length, field->decimals,
2811
 
                fieldflags2str(field->flags));
 
2862
                drizzle_column_name(field), drizzle_column_catalog(field),
 
2863
                drizzle_column_db(field), drizzle_column_table(field),
 
2864
                drizzle_column_orig_table(field),
 
2865
                fieldtype2str(drizzle_column_type(field)),
 
2866
                get_charset_name(drizzle_column_charset(field)),
 
2867
                drizzle_column_charset(field), drizzle_column_size(field),
 
2868
                drizzle_column_max_size(field), drizzle_column_decimals(field),
 
2869
                fieldflags2str(drizzle_column_flags(field)));
2812
2870
  }
2813
2871
  tee_puts("", PAGER);
2814
2872
}
2815
2873
 
2816
2874
 
2817
2875
static void
2818
 
print_table_data(DRIZZLE_RES *result)
 
2876
print_table_data(drizzle_result_st *result)
2819
2877
{
2820
 
  DRIZZLE_ROW     cur;
2821
 
  DRIZZLE_FIELD   *field;
2822
 
  bool          *num_flag;
 
2878
  drizzle_row_t cur;
 
2879
  drizzle_return_t ret;
 
2880
  drizzle_column_st *field;
 
2881
  bool *num_flag;
2823
2882
  string separator;
2824
2883
 
2825
2884
  separator.reserve(256);
2826
2885
 
2827
 
  num_flag=(bool*) malloc(sizeof(bool)*drizzleclient_num_fields(result));
 
2886
  num_flag=(bool*) malloc(sizeof(bool)*drizzle_result_column_count(result));
2828
2887
  if (column_types_flag)
2829
2888
  {
2830
2889
    print_field_types(result);
2831
 
    if (!drizzleclient_num_rows(result))
 
2890
    if (!drizzle_result_row_count(result))
2832
2891
      return;
2833
 
    drizzleclient_field_seek(result,0);
 
2892
    drizzle_column_seek(result,0);
2834
2893
  }
2835
2894
  separator.append("+");
2836
 
  while ((field = drizzleclient_fetch_field(result)))
 
2895
  while ((field = drizzle_column_next(result)))
2837
2896
  {
2838
2897
    uint32_t x, length= 0;
2839
2898
 
2840
2899
    if (column_names)
2841
2900
    {
 
2901
      uint32_t name_length= strlen(drizzle_column_name(field));
 
2902
 
2842
2903
      /* Check if the max_byte value is really the maximum in terms
2843
2904
         of visual length since multibyte characters can affect the
2844
2905
         length of the separator. */
2845
2906
      length= charset_info->cset->numcells(charset_info,
2846
 
                                           field->name,
2847
 
                                           field->name+field->name_length);
 
2907
                                           drizzle_column_name(field),
 
2908
                                           drizzle_column_name(field) +
 
2909
                                           name_length);
2848
2910
 
2849
 
      if (field->name_length == field->max_length)
 
2911
      if (name_length == drizzle_column_max_size(field))
2850
2912
      {
2851
 
        if (length < field->max_length)
2852
 
          field->max_length= length;
 
2913
        if (length < drizzle_column_max_size(field))
 
2914
          drizzle_column_set_max_size(field, length);
2853
2915
      }
2854
2916
      else
2855
2917
      {
2856
 
        length= field->name_length;
 
2918
        length= name_length;
2857
2919
      }
2858
2920
    }
2859
2921
  
2860
2922
    if (quick)
2861
 
      length=max(length,field->length);
 
2923
      length=max(length,drizzle_column_size(field));
2862
2924
    else
2863
 
      length=max(length,field->max_length);
2864
 
    if (length < 4 && !(field->flags & NOT_NULL_FLAG))
 
2925
      length=max(length,(uint32_t)drizzle_column_max_size(field));
 
2926
    if (length < 4 &&
 
2927
        !(drizzle_column_flags(field) & DRIZZLE_COLUMN_FLAGS_NOT_NULL))
 
2928
    {
2865
2929
      // Room for "NULL"
2866
2930
      length=4;
2867
 
    field->max_length=length;
 
2931
    }
 
2932
    drizzle_column_set_max_size(field, length);
2868
2933
 
2869
2934
    for (x=0; x< (length+2); x++)
2870
2935
      separator.append("-");
2874
2939
  tee_puts((char*) separator.c_str(), PAGER);
2875
2940
  if (column_names)
2876
2941
  {
2877
 
    drizzleclient_field_seek(result,0);
 
2942
    drizzle_column_seek(result,0);
2878
2943
    (void) tee_fputs("|", PAGER);
2879
 
    for (uint32_t off=0; (field = drizzleclient_fetch_field(result)) ; off++)
 
2944
    for (uint32_t off=0; (field = drizzle_column_next(result)) ; off++)
2880
2945
    {
2881
 
      uint32_t name_length= (uint32_t) strlen(field->name);
 
2946
      uint32_t name_length= (uint32_t) strlen(drizzle_column_name(field));
2882
2947
      uint32_t numcells= charset_info->cset->numcells(charset_info,
2883
 
                                                  field->name,
2884
 
                                                  field->name + name_length);
2885
 
      uint32_t display_length= field->max_length + name_length - numcells;
 
2948
                                                  drizzle_column_name(field),
 
2949
                                                  drizzle_column_name(field) +
 
2950
                                                  name_length);
 
2951
      uint32_t display_length= drizzle_column_max_size(field) + name_length -
 
2952
                               numcells;
2886
2953
      tee_fprintf(PAGER, " %-*s |",(int) min(display_length,
2887
2954
                                             MAX_COLUMN_LENGTH),
2888
 
                  field->name);
2889
 
      num_flag[off]= ((field->type <= DRIZZLE_TYPE_LONGLONG) ||
2890
 
                      (field->type == DRIZZLE_TYPE_NEWDECIMAL));
 
2955
                  drizzle_column_name(field));
 
2956
      num_flag[off]= ((drizzle_column_type(field) <= DRIZZLE_COLUMN_TYPE_LONGLONG) ||
 
2957
                      (drizzle_column_type(field) == DRIZZLE_COLUMN_TYPE_NEWDECIMAL));
2891
2958
    }
2892
2959
    (void) tee_fputs("\n", PAGER);
2893
2960
    tee_puts((char*) separator.c_str(), PAGER);
2894
2961
  }
2895
2962
 
2896
 
  while ((cur= drizzleclient_fetch_row(result)))
 
2963
  while (1)
2897
2964
  {
2898
 
    if (interrupted_query)
 
2965
    if (quick)
 
2966
    {
 
2967
      cur= drizzle_row_buffer(result, &ret);
 
2968
      if (ret != DRIZZLE_RETURN_OK)
 
2969
      {
 
2970
        (void)put_error(&con, result);
 
2971
        break;
 
2972
      }
 
2973
    }
 
2974
    else
 
2975
      cur= drizzle_row_next(result);
 
2976
 
 
2977
    if (cur == NULL || interrupted_query)
2899
2978
      break;
2900
 
    uint32_t *lengths= drizzleclient_fetch_lengths(result);
 
2979
 
 
2980
    size_t *lengths= drizzle_row_field_sizes(result);
2901
2981
    (void) tee_fputs("| ", PAGER);
2902
 
    drizzleclient_field_seek(result, 0);
2903
 
    for (uint32_t off= 0; off < drizzleclient_num_fields(result); off++)
 
2982
    drizzle_column_seek(result, 0);
 
2983
    for (uint32_t off= 0; off < drizzle_result_column_count(result); off++)
2904
2984
    {
2905
2985
      const char *buffer;
2906
2986
      uint32_t data_length;
2919
2999
        data_length= (uint32_t) lengths[off];
2920
3000
      }
2921
3001
 
2922
 
      field= drizzleclient_fetch_field(result);
2923
 
      field_max_length= field->max_length;
 
3002
      field= drizzle_column_next(result);
 
3003
      field_max_length= drizzle_column_max_size(field);
2924
3004
 
2925
3005
      /*
2926
3006
        How many text cells on the screen will this string span?  If it contains
2946
3026
      tee_fputs(" | ", PAGER);
2947
3027
    }
2948
3028
    (void) tee_fputs("\n", PAGER);
 
3029
    if (quick)
 
3030
      drizzle_row_free(result, cur);
2949
3031
  }
2950
3032
  tee_puts(separator.c_str(), PAGER);
2951
3033
  free(num_flag);
2967
3049
 
2968
3050
   @returns  number of character positions to be used, at most
2969
3051
*/
2970
 
static int get_field_disp_length(DRIZZLE_FIELD *field)
 
3052
static int get_field_disp_length(drizzle_column_st *field)
2971
3053
{
2972
 
  uint32_t length= column_names ? field->name_length : 0;
 
3054
  uint32_t length= column_names ? strlen(drizzle_column_name(field)) : 0;
2973
3055
 
2974
3056
  if (quick)
2975
 
    length= max(length, field->length);
 
3057
    length= max(length, drizzle_column_size(field));
2976
3058
  else
2977
 
    length= max(length, field->max_length);
 
3059
    length= max(length, (uint32_t)drizzle_column_max_size(field));
2978
3060
 
2979
 
  if (length < 4 && !(field->flags & NOT_NULL_FLAG))
 
3061
  if (length < 4 &&
 
3062
    !(drizzle_column_flags(field) & DRIZZLE_COLUMN_FLAGS_NOT_NULL))
 
3063
  {
2980
3064
    length= 4;        /* Room for "NULL" */
 
3065
  }
2981
3066
 
2982
3067
  return length;
2983
3068
}
2990
3075
 
2991
3076
   @returns  The max number of characters in any row of this result
2992
3077
*/
2993
 
static int get_result_width(DRIZZLE_RES *result)
 
3078
static int get_result_width(drizzle_result_st *result)
2994
3079
{
2995
3080
  unsigned int len= 0;
2996
 
  DRIZZLE_FIELD *field;
2997
 
  DRIZZLE_FIELD_OFFSET offset;
 
3081
  drizzle_column_st *field;
 
3082
  uint16_t offset;
2998
3083
 
2999
 
  offset= drizzleclient_field_tell(result);
 
3084
  offset= drizzle_column_current(result);
3000
3085
  assert(offset == 0);
3001
3086
 
3002
 
  while ((field= drizzleclient_fetch_field(result)) != NULL)
 
3087
  while ((field= drizzle_column_next(result)) != NULL)
3003
3088
    len+= get_field_disp_length(field) + 3; /* plus bar, space, & final space */
3004
3089
 
3005
 
  (void) drizzleclient_field_seek(result, offset);
 
3090
  (void) drizzle_column_seek(result, offset);
3006
3091
 
3007
3092
  return len + 1; /* plus final bar. */
3008
3093
}
3038
3123
 
3039
3124
 
3040
3125
static void
3041
 
print_table_data_vertically(DRIZZLE_RES *result)
 
3126
print_table_data_vertically(drizzle_result_st *result)
3042
3127
{
3043
 
  DRIZZLE_ROW  cur;
3044
 
  uint32_t    max_length=0;
3045
 
  DRIZZLE_FIELD  *field;
 
3128
  drizzle_row_t cur;
 
3129
  drizzle_return_t ret;
 
3130
  uint32_t max_length=0;
 
3131
  drizzle_column_st *field;
3046
3132
 
3047
 
  while ((field = drizzleclient_fetch_field(result)))
 
3133
  while ((field = drizzle_column_next(result)))
3048
3134
  {
3049
 
    uint32_t length= field->name_length;
 
3135
    uint32_t length= strlen(drizzle_column_name(field));
3050
3136
    if (length > max_length)
3051
3137
      max_length= length;
3052
 
    field->max_length=length;
 
3138
    drizzle_column_set_max_size(field, length);
3053
3139
  }
3054
3140
 
3055
 
  drizzleclient_field_seek(result,0);
3056
 
  for (uint32_t row_count=1; (cur= drizzleclient_fetch_row(result)); row_count++)
 
3141
  for (uint32_t row_count=1;; row_count++)
3057
3142
  {
3058
 
    if (interrupted_query)
 
3143
    if (quick)
 
3144
    {
 
3145
      cur= drizzle_row_buffer(result, &ret);
 
3146
      if (ret != DRIZZLE_RETURN_OK)
 
3147
      {
 
3148
        (void)put_error(&con, result);
 
3149
        break;
 
3150
      }
 
3151
    }
 
3152
    else
 
3153
      cur= drizzle_row_next(result);
 
3154
 
 
3155
    if (cur == NULL || interrupted_query)
3059
3156
      break;
3060
 
    drizzleclient_field_seek(result,0);
 
3157
    drizzle_column_seek(result,0);
3061
3158
    tee_fprintf(PAGER,
3062
3159
                "*************************** %d. row ***************************\n", row_count);
3063
 
    for (uint32_t off=0; off < drizzleclient_num_fields(result); off++)
 
3160
    for (uint32_t off=0; off < drizzle_result_column_count(result); off++)
3064
3161
    {
3065
 
      field= drizzleclient_fetch_field(result);
3066
 
      tee_fprintf(PAGER, "%*s: ",(int) max_length,field->name);
 
3162
      field= drizzle_column_next(result);
 
3163
      tee_fprintf(PAGER, "%*s: ",(int) max_length,drizzle_column_name(field));
3067
3164
      tee_fprintf(PAGER, "%s\n",cur[off] ? (char*) cur[off] : "NULL");
3068
3165
    }
 
3166
    if (quick)
 
3167
      drizzle_row_free(result, cur);
3069
3168
  }
3070
3169
}
3071
3170
 
3072
3171
 
3073
3172
/* print_warnings should be called right after executing a statement */
3074
3173
 
3075
 
static void print_warnings()
 
3174
static void print_warnings(uint32_t error_code)
3076
3175
{
3077
 
  const char   *query;
3078
 
  DRIZZLE_RES    *result;
3079
 
  DRIZZLE_ROW    cur;
 
3176
  const char *query;
 
3177
  drizzle_result_st result;
 
3178
  drizzle_row_t cur;
3080
3179
  uint64_t num_rows;
3081
 
 
3082
 
  /* Save current error before calling "show warnings" */
3083
 
  uint32_t error= drizzleclient_errno(&drizzle);
 
3180
  uint32_t new_code= 0;
3084
3181
 
3085
3182
  /* Get the warnings */
3086
3183
  query= "show warnings";
3087
 
  drizzleclient_real_query_for_lazy(query, strlen(query));
 
3184
  drizzleclient_real_query_for_lazy(query, strlen(query),&result,&new_code);
3088
3185
  drizzleclient_store_result_for_lazy(&result);
3089
3186
 
3090
3187
  /* Bail out when no warnings */
3091
 
  if (!(num_rows= drizzleclient_num_rows(result)))
 
3188
  if (!(num_rows= drizzle_result_row_count(&result)))
3092
3189
    goto end;
3093
3190
 
3094
 
  cur= drizzleclient_fetch_row(result);
 
3191
  cur= drizzle_row_next(&result);
3095
3192
 
3096
3193
  /*
3097
3194
    Don't print a duplicate of the current error.  It is possible for SHOW
3099
3196
    messages.  To be safe, skip printing the duplicate only if it is the only
3100
3197
    warning.
3101
3198
  */
3102
 
  if (!cur || (num_rows == 1 && error == (uint32_t) strtoul(cur[1], NULL, 10)))
 
3199
  if (!cur || (num_rows == 1 &&
 
3200
      error_code == (uint32_t) strtoul(cur[1], NULL, 10)))
 
3201
  {
3103
3202
    goto end;
 
3203
  }
3104
3204
 
3105
3205
  /* Print the warnings */
3106
3206
  init_pager();
3107
3207
  do
3108
3208
  {
3109
3209
    tee_fprintf(PAGER, "%s (Code %s): %s\n", cur[0], cur[1], cur[2]);
3110
 
  } while ((cur= drizzleclient_fetch_row(result)));
 
3210
  } while ((cur= drizzle_row_next(&result)));
3111
3211
  end_pager();
3112
3212
 
3113
3213
end:
3114
 
  drizzleclient_free_result(result);
 
3214
  drizzle_result_free(&result);
3115
3215
}
3116
3216
 
3117
3217
 
3153
3253
 
3154
3254
 
3155
3255
static void
3156
 
print_tab_data(DRIZZLE_RES *result)
 
3256
print_tab_data(drizzle_result_st *result)
3157
3257
{
3158
 
  DRIZZLE_ROW  cur;
3159
 
  DRIZZLE_FIELD  *field;
3160
 
  uint32_t    *lengths;
 
3258
  drizzle_row_t cur;
 
3259
  drizzle_return_t ret;
 
3260
  drizzle_column_st *field;
 
3261
  size_t *lengths;
3161
3262
 
3162
3263
  if (opt_silent < 2 && column_names)
3163
3264
  {
3164
3265
    int first=0;
3165
 
    while ((field = drizzleclient_fetch_field(result)))
 
3266
    while ((field = drizzle_column_next(result)))
3166
3267
    {
3167
3268
      if (first++)
3168
3269
        (void) tee_fputs("\t", PAGER);
3169
 
      (void) tee_fputs(field->name, PAGER);
 
3270
      (void) tee_fputs(drizzle_column_name(field), PAGER);
3170
3271
    }
3171
3272
    (void) tee_fputs("\n", PAGER);
3172
3273
  }
3173
 
  while ((cur = drizzleclient_fetch_row(result)))
 
3274
  while (1)
3174
3275
  {
3175
 
    lengths= drizzleclient_fetch_lengths(result);
 
3276
    if (quick)
 
3277
    {
 
3278
      cur= drizzle_row_buffer(result, &ret);
 
3279
      if (ret != DRIZZLE_RETURN_OK)
 
3280
      {
 
3281
        (void)put_error(&con, result);
 
3282
        break;
 
3283
      }
 
3284
    }
 
3285
    else
 
3286
      cur= drizzle_row_next(result);
 
3287
 
 
3288
    if (cur == NULL)
 
3289
      break;
 
3290
 
 
3291
    lengths= drizzle_row_field_sizes(result);
3176
3292
    safe_put_field(cur[0],lengths[0]);
3177
 
    for (uint32_t off=1 ; off < drizzleclient_num_fields(result); off++)
 
3293
    for (uint32_t off=1 ; off < drizzle_result_column_count(result); off++)
3178
3294
    {
3179
3295
      (void) tee_fputs("\t", PAGER);
3180
3296
      safe_put_field(cur[off], lengths[off]);
3181
3297
    }
3182
3298
    (void) tee_fputs("\n", PAGER);
 
3299
    if (quick)
 
3300
      drizzle_row_free(result, cur);
3183
3301
  }
3184
3302
}
3185
3303
 
3376
3494
 
3377
3495
  if (connected)
3378
3496
  {
3379
 
    sprintf(buff,"Connection id:    %u",drizzleclient_thread_id(&drizzle));
 
3497
    sprintf(buff,"Connection id:    %u",drizzle_con_thread_id(&con));
3380
3498
    put_info(buff,INFO_INFO,0,0);
3381
3499
    sprintf(buff,"Current database: %.128s\n",
3382
3500
            current_db ? current_db : "*** NONE ***");
3418
3536
    return put_info(buff, INFO_ERROR, 0 ,0);
3419
3537
  }
3420
3538
 
3421
 
  if (!(line_buff=batch_readline_init(opt_max_allowed_packet+512,sql_file)))
 
3539
  if (!(line_buff=batch_readline_init(opt_max_input_line+512,sql_file)))
3422
3540
  {
3423
3541
    fclose(sql_file);
3424
3542
    return put_info("Can't initialize batch_readline", INFO_ERROR, 0 ,0);
3480
3598
{
3481
3599
  char *tmp, buff[FN_REFLEN + 1];
3482
3600
  int select_db;
 
3601
  drizzle_result_st result;
 
3602
  drizzle_return_t ret;
3483
3603
 
3484
3604
  memset(buff, 0, sizeof(buff));
3485
3605
  strncpy(buff, line, sizeof(buff) - 1);
3527
3647
    */
3528
3648
    if (!connected && reconnect())
3529
3649
      return opt_reconnect ? -1 : 1;                        // Fatal error
3530
 
    if (drizzleclient_select_db(&drizzle,tmp))
 
3650
    for (bool try_again= true; try_again; try_again= false)
3531
3651
    {
3532
 
      if (drizzleclient_errno(&drizzle) != CR_SERVER_GONE_ERROR)
3533
 
        return put_error(&drizzle);
3534
 
 
3535
 
      if (reconnect())
3536
 
        return opt_reconnect ? -1 : 1;                      // Fatal error
3537
 
      if (drizzleclient_select_db(&drizzle,tmp))
3538
 
        return put_error(&drizzle);
 
3652
      if (drizzle_select_db(&con,&result,tmp,&ret) == NULL ||
 
3653
          ret != DRIZZLE_RETURN_OK)
 
3654
      {
 
3655
        if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
3656
        {
 
3657
          int error= put_error(&con, &result);
 
3658
          drizzle_result_free(&result);
 
3659
          return error;
 
3660
        }
 
3661
 
 
3662
        if (ret != DRIZZLE_RETURN_SERVER_GONE || !try_again)
 
3663
          return put_error(&con, NULL);
 
3664
 
 
3665
        if (reconnect())
 
3666
          return opt_reconnect ? -1 : 1;                      // Fatal error
 
3667
      }
 
3668
      else
 
3669
        drizzle_result_free(&result);
3539
3670
    }
3540
3671
    free(current_db);
3541
3672
    current_db= strdup(tmp);
3629
3760
sql_connect(char *host,char *database,char *user,char *password,
3630
3761
                 uint32_t silent)
3631
3762
{
 
3763
  drizzle_return_t ret;
 
3764
 
3632
3765
  if (connected)
3633
3766
  {
3634
3767
    connected= 0;
3635
 
    drizzleclient_close(&drizzle);
3636
 
  }
3637
 
  drizzleclient_create(&drizzle);
 
3768
    drizzle_con_free(&con);
 
3769
    drizzle_free(&drizzle);
 
3770
  }
 
3771
  drizzle_create(&drizzle);
 
3772
  if (drizzle_con_add_tcp(&drizzle, &con, host, opt_drizzle_port, user,
 
3773
                          password, database, DRIZZLE_CON_NONE) == NULL)
 
3774
  {
 
3775
    (void) put_error(&con, NULL);
 
3776
    (void) fflush(stdout);
 
3777
    return 1;
 
3778
  }
 
3779
 
 
3780
/* XXX add this back in
3638
3781
  if (opt_connect_timeout)
3639
3782
  {
3640
3783
    uint32_t timeout=opt_connect_timeout;
3641
3784
    drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
3642
3785
                  (char*) &timeout);
3643
3786
  }
3644
 
  if (opt_compress)
3645
 
    drizzleclient_options(&drizzle,DRIZZLE_OPT_COMPRESS,NULL);
3646
 
  if (opt_secure_auth)
3647
 
    drizzleclient_options(&drizzle, DRIZZLE_SECURE_AUTH, (char *) &opt_secure_auth);
 
3787
*/
 
3788
 
 
3789
/* XXX Do we need this?
3648
3790
  if (safe_updates)
3649
3791
  {
3650
3792
    char init_command[100];
3654
3796
            select_limit, max_join_size);
3655
3797
    drizzleclient_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
3656
3798
  }
3657
 
  if (!drizzleclient_connect(&drizzle, host, user, password,
3658
 
                          database, opt_drizzle_port, opt_drizzle_unix_port,
3659
 
                          connect_flag | CLIENT_MULTI_STATEMENTS))
 
3799
*/
 
3800
  if ((ret= drizzle_con_connect(&con)) != DRIZZLE_RETURN_OK)
3660
3801
  {
3661
 
    if (!silent ||
3662
 
        (drizzleclient_errno(&drizzle) != CR_CONN_HOST_ERROR &&
3663
 
         drizzleclient_errno(&drizzle) != CR_CONNECTION_ERROR))
 
3802
    if (!silent || (ret != DRIZZLE_RETURN_GETADDRINFO &&
 
3803
                    ret != DRIZZLE_RETURN_COULD_NOT_CONNECT))
3664
3804
    {
3665
 
      (void) put_error(&drizzle);
 
3805
      (void) put_error(&con, NULL);
3666
3806
      (void) fflush(stdout);
3667
3807
      return ignore_errors ? -1 : 1;    // Abort
3668
3808
    }
3669
3809
    return -1;          // Retryable
3670
3810
  }
3671
3811
  connected=1;
 
3812
/* XXX hmm?
3672
3813
  drizzle.reconnect= debug_info_flag; // We want to know if this happens
 
3814
*/
3673
3815
  build_completion_hash(opt_rehash, 1);
3674
3816
  return 0;
3675
3817
}
3678
3820
static int
3679
3821
com_status(string *, const char *)
3680
3822
{
 
3823
/*
3681
3824
  char buff[40];
3682
3825
  uint64_t id;
3683
 
  DRIZZLE_RES *result;
 
3826
*/
 
3827
  drizzle_result_st result;
 
3828
  drizzle_return_t ret;
3684
3829
 
3685
3830
  tee_puts("--------------", stdout);
3686
3831
  usage(1);          /* Print version */
3687
3832
  if (connected)
3688
3833
  {
3689
 
    tee_fprintf(stdout, "\nConnection id:\t\t%lu\n",drizzleclient_thread_id(&drizzle));
 
3834
    tee_fprintf(stdout, "\nConnection id:\t\t%lu\n",drizzle_con_thread_id(&con));
3690
3835
    /*
3691
3836
      Don't remove "limit 1",
3692
3837
      it is protection againts SQL_SELECT_LIMIT=0
3693
3838
    */
3694
 
    if (!drizzleclient_query(&drizzle,"select DATABASE(), USER() limit 1") &&
3695
 
        (result=drizzleclient_use_result(&drizzle)))
 
3839
    if (drizzle_query_str(&con,&result,"select DATABASE(), USER() limit 1",
 
3840
                          &ret) != NULL && ret == DRIZZLE_RETURN_OK &&
 
3841
        drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
3696
3842
    {
3697
 
      DRIZZLE_ROW cur=drizzleclient_fetch_row(result);
 
3843
      drizzle_row_t cur=drizzle_row_next(&result);
3698
3844
      if (cur)
3699
3845
      {
3700
3846
        tee_fprintf(stdout, "Current database:\t%s\n", cur[0] ? cur[0] : "");
3701
3847
        tee_fprintf(stdout, "Current user:\t\t%s\n", cur[1]);
3702
3848
      }
3703
 
      drizzleclient_free_result(result);
 
3849
      drizzle_result_free(&result);
3704
3850
    }
 
3851
    else if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
3852
      drizzle_result_free(&result);
3705
3853
    tee_puts("SSL:\t\t\tNot in use", stdout);
3706
3854
  }
3707
3855
  else
3720
3868
  tee_fprintf(stdout, "Current pager:\t\t%s\n", pager);
3721
3869
  tee_fprintf(stdout, "Using outfile:\t\t'%s'\n", opt_outfile ? outfile : "");
3722
3870
  tee_fprintf(stdout, "Using delimiter:\t%s\n", delimiter);
3723
 
  tee_fprintf(stdout, "Server version:\t\t%s\n", server_version_string(&drizzle));
3724
 
  tee_fprintf(stdout, "Protocol version:\t%d\n", drizzleclient_get_proto_info(&drizzle));
3725
 
  tee_fprintf(stdout, "Connection:\t\t%s\n", drizzleclient_get_host_info(&drizzle));
 
3871
  tee_fprintf(stdout, "Server version:\t\t%s\n", server_version_string(&con));
 
3872
  tee_fprintf(stdout, "Protocol version:\t%d\n", drizzle_con_protocol_version(&con));
 
3873
  tee_fprintf(stdout, "Connection:\t\t%s\n", drizzle_con_host(&con));
 
3874
/* XXX need to save this from result
3726
3875
  if ((id= drizzleclient_insert_id(&drizzle)))
3727
3876
    tee_fprintf(stdout, "Insert id:\t\t%s\n", llstr(id, buff));
3728
 
 
3729
 
  /* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
3730
 
  if (!drizzleclient_query(&drizzle,"select @@character_set_client, @@character_set_connection, @@character_set_server, @@character_set_database limit 1") &&
3731
 
      (result=drizzleclient_use_result(&drizzle)))
3732
 
  {
3733
 
    DRIZZLE_ROW cur=drizzleclient_fetch_row(result);
3734
 
    if (cur)
3735
 
    {
3736
 
      tee_fprintf(stdout, "Server characterset:\t%s\n", cur[2] ? cur[2] : "");
3737
 
      tee_fprintf(stdout, "Db     characterset:\t%s\n", cur[3] ? cur[3] : "");
3738
 
      tee_fprintf(stdout, "Client characterset:\t%s\n", cur[0] ? cur[0] : "");
3739
 
      tee_fprintf(stdout, "Conn.  characterset:\t%s\n", cur[1] ? cur[1] : "");
3740
 
    }
3741
 
    drizzleclient_free_result(result);
3742
 
  }
3743
 
 
3744
 
  if (strstr(drizzleclient_get_host_info(&drizzle),"TCP/IP") || ! drizzle.unix_socket)
3745
 
    tee_fprintf(stdout, "TCP port:\t\t%d\n", drizzle.port);
 
3877
*/
 
3878
 
 
3879
  if (strcmp(drizzle_con_uds(&con), ""))
 
3880
    tee_fprintf(stdout, "UNIX socket:\t\t%s\n", drizzle_con_uds(&con));
3746
3881
  else
3747
 
    tee_fprintf(stdout, "UNIX socket:\t\t%s\n", drizzle.unix_socket);
3748
 
  if (drizzle.net.compress)
3749
 
    tee_fprintf(stdout, "Protocol:\t\tCompressed\n");
 
3882
    tee_fprintf(stdout, "TCP port:\t\t%d\n", drizzle_con_port(&con));
3750
3883
 
3751
3884
  if (safe_updates)
3752
3885
  {
3765
3898
}
3766
3899
 
3767
3900
static const char *
3768
 
server_version_string(DRIZZLE *con)
 
3901
server_version_string(drizzle_con_st *local_con)
3769
3902
{
3770
3903
  static string buf("");
3771
3904
  static bool server_version_string_reserved= false;
3778
3911
  /* Only one thread calls this, so no synchronization is needed */
3779
3912
  if (buf[0] == '\0')
3780
3913
  {
3781
 
    DRIZZLE_RES *result;
 
3914
    drizzle_result_st result;
 
3915
    drizzle_return_t ret;
3782
3916
 
3783
 
    buf.append(drizzleclient_get_server_info(con));
 
3917
    buf.append(drizzle_con_server_version(local_con));
3784
3918
 
3785
3919
    /* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
3786
 
    if (!drizzleclient_query(con, "select @@version_comment limit 1") &&
3787
 
        (result = drizzleclient_use_result(con)))
 
3920
    (void)drizzle_query_str(local_con, &result,
 
3921
                            "select @@version_comment limit 1", &ret);
 
3922
    if (ret == DRIZZLE_RETURN_OK &&
 
3923
        drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
3788
3924
    {
3789
 
      DRIZZLE_ROW cur = drizzleclient_fetch_row(result);
 
3925
      drizzle_row_t cur = drizzle_row_next(&result);
3790
3926
      if (cur && cur[0])
3791
3927
      {
3792
3928
        buf.append(" ");
3793
3929
        buf.append(cur[0]);
3794
3930
      }
3795
 
      drizzleclient_free_result(result);
 
3931
      drizzle_result_free(&result);
3796
3932
    }
 
3933
    else if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
3934
      drizzle_result_free(&result);
3797
3935
  }
3798
3936
 
3799
3937
  return buf.c_str();
3872
4010
 
3873
4011
 
3874
4012
static int
3875
 
put_error(DRIZZLE *con)
 
4013
put_error(drizzle_con_st *local_con, drizzle_result_st *res)
3876
4014
{
3877
 
  return put_info(drizzleclient_error(con), INFO_ERROR, drizzleclient_errno(con),
3878
 
                  drizzleclient_sqlstate(con));
 
4015
  const char *error;
 
4016
 
 
4017
  if (res != NULL)
 
4018
  {
 
4019
    error= drizzle_result_error(res);
 
4020
    if (!strcmp(error, ""))
 
4021
      error= drizzle_con_error(local_con);
 
4022
  }
 
4023
  else
 
4024
    error= drizzle_con_error(local_con);
 
4025
 
 
4026
  return put_info(error, INFO_ERROR,
 
4027
                  res == NULL ? drizzle_con_error_code(local_con) :
 
4028
                                drizzle_result_error_code(res),
 
4029
                  res == NULL ? drizzle_con_sqlstate(local_con) :
 
4030
                                drizzle_result_sqlstate(res));
3879
4031
}
3880
4032
 
3881
4033
 
4040
4192
        break;
4041
4193
      case 'v':
4042
4194
        if (connected)
4043
 
          processed_prompt->append(drizzleclient_get_server_info(&drizzle));
 
4195
          processed_prompt->append(drizzle_con_server_version(&con));
4044
4196
        else
4045
4197
          processed_prompt->append("not_connected");
4046
4198
        break;
4050
4202
      case 'h':
4051
4203
      {
4052
4204
        const char *prompt;
4053
 
        prompt= connected ? drizzleclient_get_host_info(&drizzle) : "not_connected";
 
4205
        prompt= connected ? drizzle_con_host(&con) : "not_connected";
4054
4206
        if (strstr(prompt, "Localhost"))
4055
4207
          processed_prompt->append("localhost");
4056
4208
        else
4069
4221
          break;
4070
4222
        }
4071
4223
 
4072
 
        const char *host_info = drizzleclient_get_host_info(&drizzle);
4073
 
        if (strstr(host_info, "memory"))
 
4224
        if (strcmp(drizzle_con_uds(&con), ""))
4074
4225
        {
4075
 
          processed_prompt->append(drizzle.host);
 
4226
          const char *pos=strrchr(drizzle_con_uds(&con),'/');
 
4227
          processed_prompt->append(pos ? pos+1 : drizzle_con_uds(&con));
4076
4228
        }
4077
 
        else if (strstr(host_info,"TCP/IP") ||
4078
 
                 !drizzle.unix_socket)
4079
 
          add_int_to_prompt(drizzle.port);
4080
4229
        else
4081
 
        {
4082
 
          char *pos=strrchr(drizzle.unix_socket,'/');
4083
 
          processed_prompt->append(pos ? pos+1 : drizzle.unix_socket);
4084
 
        }
 
4230
          add_int_to_prompt(drizzle_con_port(&con));
4085
4231
      }
4086
4232
      break;
4087
4233
      case 'U':
4193
4339
 
4194
4340
static void init_username()
4195
4341
{
 
4342
/* XXX need this?
4196
4343
  free(full_username);
4197
4344
  free(part_username);
4198
4345
 
4199
 
  DRIZZLE_RES *result;
 
4346
  drizzle_result_st *result;
4200
4347
  if (!drizzleclient_query(&drizzle,"select USER()") &&
4201
4348
      (result=drizzleclient_use_result(&drizzle)))
4202
4349
  {
4203
 
    DRIZZLE_ROW cur=drizzleclient_fetch_row(result);
 
4350
    drizzle_row_t cur=drizzleclient_fetch_row(result);
4204
4351
    full_username= strdup(cur[0]);
4205
4352
    part_username= strdup(strtok(cur[0],"@"));
4206
4353
    (void) drizzleclient_fetch_row(result);        // Read eof
4207
4354
  }
 
4355
*/
4208
4356
}
4209
4357
 
4210
4358
static int com_prompt(string *, const char *line)