~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzle.cc

  • Committer: Brian Aker
  • Date: 2009-04-07 20:09:30 UTC
  • mfrom: (971.1.17 mordred)
  • Revision ID: brian@gaz-20090407200930-27jkul7lkwkjs2to
Merge from Monty

Show diffs side-by-side

added added

removed removed

Lines of Context:
169
169
  connected= false, opt_raw_data= false, unbuffered= false,
170
170
  output_tables= false, opt_rehash= true, skip_updates= false,
171
171
  safe_updates= false, one_database= false,
172
 
  opt_compress= false,
 
172
  opt_compress= false, opt_shutdown= false, opt_ping= false,
173
173
  vertical= false, line_numbers= true, column_names= true,
174
174
  opt_nopager= true, opt_outfile= false, named_cmds= false,
175
175
  tty_password= false, opt_nobeep= false, opt_reconnect= true,
1020
1020
static void window_resize(int sig);
1021
1021
#endif
1022
1022
 
 
1023
/**
 
1024
  Shutdown the server that we are currently connected to.
 
1025
 
 
1026
  @retval
 
1027
    true success
 
1028
  @retval
 
1029
    false failure
 
1030
*/
 
1031
static bool server_shutdown(void)
 
1032
{
 
1033
  drizzle_result_st result;
 
1034
  drizzle_return_t ret;
 
1035
 
 
1036
  if (verbose)
 
1037
  {
 
1038
    printf("shutting down drizzled");
 
1039
    if (opt_drizzle_port > 0)
 
1040
      printf(" on port %d", opt_drizzle_port);
 
1041
    printf("... ");
 
1042
  }
 
1043
 
 
1044
  if (drizzle_shutdown(&con, &result, DRIZZLE_SHUTDOWN_DEFAULT,
 
1045
                       &ret) == NULL || ret != DRIZZLE_RETURN_OK)
 
1046
  {
 
1047
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
1048
    {
 
1049
      fprintf(stderr, "shutdown failed; error: '%s'",
 
1050
              drizzle_result_error(&result));
 
1051
      drizzle_result_free(&result);
 
1052
    }
 
1053
    else
 
1054
    {
 
1055
      fprintf(stderr, "shutdown failed; error: '%s'",
 
1056
              drizzle_con_error(&con));
 
1057
    }
 
1058
    return false;
 
1059
  }
 
1060
 
 
1061
  drizzle_result_free(&result);
 
1062
 
 
1063
  if (verbose)
 
1064
    printf("done\n");
 
1065
 
 
1066
  return true;
 
1067
}
 
1068
 
 
1069
/**
 
1070
  Ping the server that we are currently connected to.
 
1071
 
 
1072
  @retval
 
1073
    true success
 
1074
  @retval
 
1075
    false failure
 
1076
*/
 
1077
static bool server_ping(void)
 
1078
{
 
1079
  drizzle_result_st result;
 
1080
  drizzle_return_t ret;
 
1081
 
 
1082
  if (drizzle_ping(&con, &result, &ret) != NULL && ret == DRIZZLE_RETURN_OK)
 
1083
  {
 
1084
    if (opt_silent < 2)
 
1085
      printf("drizzled is alive\n");
 
1086
  }
 
1087
  else
 
1088
  {
 
1089
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
1090
    {
 
1091
      fprintf(stderr, "ping failed; error: '%s'",
 
1092
              drizzle_result_error(&result));
 
1093
      drizzle_result_free(&result);
 
1094
    }
 
1095
    else
 
1096
    {
 
1097
      fprintf(stderr, "drizzled won't answer to ping, error: '%s'",
 
1098
              drizzle_con_error(&con));
 
1099
    }
 
1100
    return false;
 
1101
  }
 
1102
  drizzle_result_free(&result);
 
1103
  return true;
 
1104
}
 
1105
 
 
1106
/**
 
1107
  Execute command(s) specified by the user.
 
1108
 
 
1109
  @param error  error status of command execution.
 
1110
                If an error had occurred, this variable will be set
 
1111
                to 1 whereas on success, it shall be set to 0. This
 
1112
                value will be supplied to the exit() function used
 
1113
                by the caller.
 
1114
 
 
1115
  @retval
 
1116
    false no commands were executed
 
1117
  @retval
 
1118
    true  at least one command was executed
 
1119
*/
 
1120
static bool execute_commands(int *error)
 
1121
{
 
1122
  bool executed= false;
 
1123
  *error= 0;
 
1124
 
 
1125
  if (opt_ping)
 
1126
  {
 
1127
    if (server_ping() == false)
 
1128
      *error= 1;
 
1129
    executed= true;
 
1130
  }
 
1131
 
 
1132
  if (opt_shutdown)
 
1133
  {
 
1134
    if (server_shutdown() == false)
 
1135
      *error= 1;
 
1136
    executed= true;
 
1137
  }
 
1138
  return executed;
 
1139
}
 
1140
 
1023
1141
int main(int argc,char *argv[])
1024
1142
{
1025
1143
  char buff[80];
1097
1215
    my_end(0);
1098
1216
    exit(1);
1099
1217
  }
1100
 
  if (status.batch && !status.line_buff &&
1101
 
      !(status.line_buff=batch_readline_init(opt_max_input_line+512,stdin)))
1102
 
  {
1103
 
    free_defaults(defaults_argv);
1104
 
    my_end(0);
1105
 
    exit(1);
1106
 
  }
 
1218
 
1107
1219
  memset(&drizzle, 0, sizeof(drizzle));
1108
1220
  if (sql_connect(current_host,current_db,current_user,opt_password,
1109
1221
                  opt_silent))
1112
1224
    status.exit_status= 1;
1113
1225
    drizzle_end(-1);
1114
1226
  }
 
1227
 
 
1228
  int command_error;
 
1229
  if (execute_commands(&command_error) != false)
 
1230
  {
 
1231
    /* we've executed a command so exit before we go into readline mode */
 
1232
    free_defaults(defaults_argv);
 
1233
    my_end(0);
 
1234
    exit(command_error);
 
1235
  }
 
1236
 
 
1237
  if (status.batch && !status.line_buff)
 
1238
  {
 
1239
    status.line_buff =batch_readline_init(opt_max_input_line+512, stdin);
 
1240
    if (status.line_buff == NULL)
 
1241
    {
 
1242
      free_defaults(defaults_argv);
 
1243
      my_end(0);
 
1244
      exit(1);
 
1245
    }
 
1246
  }
 
1247
 
1115
1248
  if (!status.batch)
1116
1249
    ignore_errors=1;        // Don't abort monitor
1117
1250
 
1387
1520
   0, 0, 0},
1388
1521
  {"reconnect", OPT_RECONNECT, N_("Reconnect if the connection is lost. Disable with --disable-reconnect. This option is enabled by default."),
1389
1522
   (char**) &opt_reconnect, (char**) &opt_reconnect, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
 
1523
  {"shutdown", OPT_SHUTDOWN, N_("Shutdown the server."),
 
1524
   (char**) &opt_shutdown, (char**) &opt_shutdown, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1390
1525
  {"silent", 's', N_("Be more silent. Print results with a tab as separator, each row on new line."), 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0,
1391
1526
   0, 0},
1392
1527
  {"socket", 'S', N_("Socket file to use for connection."),
1443
1578
  {"show-progress-size", OPT_SHOW_PROGRESS_SIZE, N_("Number of lines before each import progress report."),
1444
1579
   (char**) &show_progress_size, (char**) &show_progress_size, 0, GET_UINT32, REQUIRED_ARG,
1445
1580
   0, 0, 0, 0, 0, 0},
 
1581
  {"ping", OPT_PING, N_("Ping the server to check if it's alive."),
 
1582
   (char**) &opt_ping, (char**) &opt_ping, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1446
1583
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
1447
1584
};
1448
1585