~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzle.c

MergeĀ fromĀ Jim

Show diffs side-by-side

added added

removed removed

Lines of Context:
147
147
  connected=0,opt_raw_data=0,unbuffered=0,output_tables=0,
148
148
  opt_rehash=1,skip_updates=0,safe_updates=0,one_database=0,
149
149
  opt_compress=0, using_opt_local_infile=0,
150
 
  vertical=0, line_numbers=1, column_names=1,opt_html=0,
151
 
  opt_xml=0,opt_nopager=1, opt_outfile=0, named_cmds= 0,
 
150
  vertical=0, line_numbers=1, column_names=1,
 
151
  opt_nopager=1, opt_outfile=0, named_cmds= 0,
152
152
  tty_password= 0, opt_nobeep=0, opt_reconnect=1,
153
153
  default_charset_used= 0, opt_secure_auth= 0,
154
154
  default_pager_set= 0, opt_sigint_ignore= 0,
176
176
static uint32_t max_join_size;
177
177
static ulong opt_connect_timeout= 0;
178
178
static char drizzle_charsets_dir[FN_REFLEN+1];
179
 
static const char *xmlmeta[] = {
180
 
  "&", "&",
181
 
  "<", "&lt;",
182
 
  ">", "&gt;",
183
 
  "\"", "&quot;",
184
 
  0, 0
185
 
};
186
179
// TODO: Need to i18n these
187
180
static const char *day_names[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
188
181
static const char *month_names[]={"Jan","Feb","Mar","Apr","May","Jun","Jul",
199
192
static uint opt_protocol= DRIZZLE_PROTOCOL_TCP;
200
193
static const CHARSET_INFO *charset_info= &my_charset_latin1;
201
194
 
202
 
const char *default_dbug_option="d:t:o,/tmp/drizzle.trace";
203
195
int drizzle_real_query_for_lazy(const char *buf, int length);
204
196
int drizzle_store_result_for_lazy(DRIZZLE_RES **result);
205
197
 
233
225
                    const char *sql_state);
234
226
static int put_error(DRIZZLE *drizzle);
235
227
static void safe_put_field(const char *pos,ulong length);
236
 
static void xmlencode_print(const char *src, uint length);
237
228
static void init_pager(void);
238
229
static void end_pager(void);
239
230
static void init_tee(const char *);
1001
992
 
1002
993
static const char *load_default_groups[]= { "drizzle","client",0 };
1003
994
 
1004
 
static int         embedded_server_arg_count= 0;
1005
 
static char       *embedded_server_args[MAX_SERVER_ARGS];
1006
 
 
1007
995
int history_length;
1008
996
static int not_in_history(const char *line);
1009
997
static void initialize_readline (const char *name);
1014
1002
                     bool *ml_comment);
1015
1003
static void remove_cntrl(DYNAMIC_STRING *buffer);
1016
1004
static void print_table_data(DRIZZLE_RES *result);
1017
 
static void print_table_data_html(DRIZZLE_RES *result);
1018
 
static void print_table_data_xml(DRIZZLE_RES *result);
1019
1005
static void print_tab_data(DRIZZLE_RES *result);
1020
1006
static void print_table_data_vertically(DRIZZLE_RES *result);
1021
1007
static void print_warnings(void);
1141
1127
  dynstr_set(glob_buffer, NULL);
1142
1128
 
1143
1129
  initialize_readline(my_progname);
1144
 
  if (!status.batch && !quick && !opt_html && !opt_xml)
 
1130
  if (!status.batch && !quick)
1145
1131
  {
1146
1132
    /* read-history from file, default ~/.drizzle_history*/
1147
 
    if (getenv("MYSQL_HISTFILE"))
1148
 
      histfile= strdup(getenv("MYSQL_HISTFILE"));
 
1133
    if (getenv("DRIZZLE_HISTFILE"))
 
1134
      histfile= strdup(getenv("DRIZZLE_HISTFILE"));
1149
1135
    else if (getenv("HOME"))
1150
1136
    {
1151
1137
      histfile=(char*) my_malloc((uint) strlen(getenv("HOME"))
1192
1178
sig_handler drizzle_end(int sig)
1193
1179
{
1194
1180
  drizzle_close(&drizzle);
1195
 
  if (!status.batch && !quick && !opt_html && !opt_xml && histfile)
 
1181
  if (!status.batch && !quick && histfile)
1196
1182
  {
1197
1183
    /* write-history */
1198
1184
    if (verbose)
1223
1209
  my_free(part_username,MYF(MY_ALLOW_ZERO_PTR));
1224
1210
  my_free(default_prompt,MYF(MY_ALLOW_ZERO_PTR));
1225
1211
  my_free(current_prompt,MYF(MY_ALLOW_ZERO_PTR));
1226
 
  while (embedded_server_arg_count > 1)
1227
 
    my_free(embedded_server_args[--embedded_server_arg_count],MYF(0));
1228
1212
  drizzle_server_end();
1229
1213
  free_defaults(defaults_argv);
1230
1214
  my_end(my_end_arg);
1345
1329
   (char**) &opt_nobeep, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1346
1330
  {"host", 'h', N_("Connect to host."), (char**) &current_host,
1347
1331
   (char**) &current_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1348
 
  {"html", 'H', N_("Produce HTML output."), (char**) &opt_html, (char**) &opt_html,
1349
 
   0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1350
 
  {"xml", 'X', N_("Produce XML output"), (char**) &opt_xml, (char**) &opt_xml, 0,
1351
 
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1352
1332
  {"line-numbers", OPT_LINE_NUMBERS, N_("Write line numbers for errors."),
1353
1333
   (char**) &line_numbers, (char**) &line_numbers, 0, GET_BOOL,
1354
1334
   NO_ARG, 1, 0, 0, 0, 0, 0},
1380
1360
  {"password", 'p',
1381
1361
   N_("Password to use when connecting to server. If password is not given it's asked from the tty."),
1382
1362
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
1383
 
  {"port", 'P', N_("Port number to use for connection or 0 for default to, in order of preference, my.cnf, $MYSQL_TCP_PORT, ")
 
1363
  {"port", 'P', N_("Port number to use for connection or 0 for default to, in order of preference, my.cnf, $DRIZZLE_TCP_PORT, ")
1384
1364
   N_("built-in default") " (" STRINGIFY_ARG(MYSQL_PORT) ").",
1385
1365
   (char**) &opt_drizzle_port,
1386
1366
   (char**) &opt_drizzle_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0,  0},
1451
1431
   0, 1, 0},
1452
1432
  {"secure-auth", OPT_SECURE_AUTH, N_("Refuse client connecting to server if it uses old (pre-4.1.1) protocol"), (char**) &opt_secure_auth,
1453
1433
   (char**) &opt_secure_auth, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1454
 
  {"server-arg", OPT_SERVER_ARG, N_("Send embedded server this as a parameter."),
1455
 
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1456
1434
  {"show-warnings", OPT_SHOW_WARNINGS, N_("Show warnings after every statement."),
1457
1435
   (char**) &show_warnings, (char**) &show_warnings, 0, GET_BOOL, NO_ARG,
1458
1436
   0, 0, 0, 0, 0, 0},
1630
1608
  int ho_error;
1631
1609
  const DRIZZLE_PARAMETERS *drizzle_params= drizzle_get_parameters();
1632
1610
 
1633
 
  tmp= (char *) getenv("MYSQL_HOST");
 
1611
  tmp= (char *) getenv("DRIZZLE_HOST");
1634
1612
  if (tmp)
1635
1613
    current_host= strdup(tmp);
1636
1614
 
2773
2751
      if (!drizzle_num_rows(result) && ! quick && !column_types_flag)
2774
2752
      {
2775
2753
        strmov(buff, _("Empty set"));
2776
 
        if (opt_xml)
2777
 
        {
2778
 
          /*
2779
 
            We must print XML header and footer
2780
 
            to produce a well-formed XML even if
2781
 
            the result set is empty (Bug#27608).
2782
 
          */
2783
 
          init_pager();
2784
 
          print_table_data_xml(result);
2785
 
          end_pager();
2786
 
        }
2787
2754
      }
2788
2755
      else
2789
2756
      {
2790
2757
        init_pager();
2791
 
        if (opt_html)
2792
 
          print_table_data_html(result);
2793
 
        else if (opt_xml)
2794
 
          print_table_data_xml(result);
2795
 
        else if (vertical || (auto_vertical_output && (terminal_width < get_result_width(result))))
 
2758
        if (vertical || (auto_vertical_output &&
 
2759
                         (terminal_width < get_result_width(result))))
2796
2760
          print_table_data_vertically(result);
2797
2761
        else if (opt_silent && verbose <= 2 && !output_tables)
2798
2762
          print_tab_data(result);
3199
3163
 
3200
3164
 
3201
3165
static void
3202
 
print_table_data_html(DRIZZLE_RES *result)
3203
 
{
3204
 
  DRIZZLE_ROW  cur;
3205
 
  DRIZZLE_FIELD  *field;
3206
 
 
3207
 
  drizzle_field_seek(result,0);
3208
 
  (void) tee_fputs("<TABLE BORDER=1><TR>", PAGER);
3209
 
  if (column_names)
3210
 
  {
3211
 
    while((field = drizzle_fetch_field(result)))
3212
 
    {
3213
 
      tee_fprintf(PAGER, "<TH>%s</TH>", (field->name ?
3214
 
                                         (field->name[0] ? field->name :
3215
 
                                          " &nbsp; ") : "NULL"));
3216
 
    }
3217
 
    (void) tee_fputs("</TR>", PAGER);
3218
 
  }
3219
 
  while ((cur = drizzle_fetch_row(result)))
3220
 
  {
3221
 
    if (interrupted_query)
3222
 
      break;
3223
 
    uint32_t *lengths=drizzle_fetch_lengths(result);
3224
 
    (void) tee_fputs("<TR>", PAGER);
3225
 
    for (uint32_t i= 0; i < drizzle_num_fields(result); i++)
3226
 
    {
3227
 
      (void) tee_fputs("<TD>", PAGER);
3228
 
      safe_put_field(cur[i],lengths[i]);
3229
 
      (void) tee_fputs("</TD>", PAGER);
3230
 
    }
3231
 
    (void) tee_fputs("</TR>", PAGER);
3232
 
  }
3233
 
  (void) tee_fputs("</TABLE>", PAGER);
3234
 
}
3235
 
 
3236
 
 
3237
 
static void
3238
 
print_table_data_xml(DRIZZLE_RES *result)
3239
 
{
3240
 
  DRIZZLE_ROW   cur;
3241
 
  const DRIZZLE_FIELD *fields;
3242
 
 
3243
 
  drizzle_field_seek(result,0);
3244
 
 
3245
 
  tee_fputs("<?xml version=\"1.0\"?>\n\n<resultset statement=\"", PAGER);
3246
 
  xmlencode_print(glob_buffer->str, glob_buffer->length);
3247
 
  tee_fputs("\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">",
3248
 
            PAGER);
3249
 
 
3250
 
  fields = drizzle_fetch_fields(result);
3251
 
  while ((cur = drizzle_fetch_row(result)))
3252
 
  {
3253
 
    if (interrupted_query)
3254
 
      break;
3255
 
    uint32_t *lengths=drizzle_fetch_lengths(result);
3256
 
    (void) tee_fputs("\n  <row>\n", PAGER);
3257
 
    for (uint i=0; i < drizzle_num_fields(result); i++)
3258
 
    {
3259
 
      tee_fprintf(PAGER, "\t<field name=\"");
3260
 
      xmlencode_print(fields[i].name, (uint) strlen(fields[i].name));
3261
 
      if (cur[i])
3262
 
      {
3263
 
        tee_fprintf(PAGER, "\">");
3264
 
        xmlencode_print(cur[i], lengths[i]);
3265
 
        tee_fprintf(PAGER, "</field>\n");
3266
 
      }
3267
 
      else
3268
 
        tee_fprintf(PAGER, "\" xsi:nil=\"true\" />\n");
3269
 
    }
3270
 
    (void) tee_fputs("  </row>\n", PAGER);
3271
 
  }
3272
 
  (void) tee_fputs("</resultset>\n", PAGER);
3273
 
}
3274
 
 
3275
 
 
3276
 
static void
3277
3166
print_table_data_vertically(DRIZZLE_RES *result)
3278
3167
{
3279
3168
  DRIZZLE_ROW  cur;
3351
3240
}
3352
3241
 
3353
3242
 
3354
 
static const char *array_value(const char **array, char key)
3355
 
{
3356
 
  for (; *array; array+= 2)
3357
 
    if (**array == key)
3358
 
      return array[1];
3359
 
  return 0;
3360
 
}
3361
 
 
3362
 
 
3363
 
static void
3364
 
xmlencode_print(const char *src, uint length)
3365
 
{
3366
 
  if (!src)
3367
 
    tee_fputs("NULL", PAGER);
3368
 
  else
3369
 
  {
3370
 
    for (const char *p = src; *p && length; length--)
3371
 
    {
3372
 
      const char *t;
3373
 
      if ((t = array_value(xmlmeta, *p++)))
3374
 
        tee_fputs(t, PAGER);
3375
 
      else
3376
 
        tee_putc(*p, PAGER);
3377
 
    }
3378
 
  }
3379
 
}
3380
 
 
3381
 
 
3382
3243
static void
3383
3244
safe_put_field(const char *pos,ulong length)
3384
3245
{