--- mysql-5.1.36/client/client_priv.h 2009-06-16 21:22:50.000000000 +0900 +++ mysql-5.1.36-init-command/client/client_priv.h 2009-07-03 12:45:11.000000000 +0900 @@ -80,5 +80,6 @@ OPT_FIX_TABLE_NAMES, OPT_FIX_DB_NAMES, OPT_SSL_VERIFY_SERVER_CERT, OPT_DEBUG_INFO, OPT_DEBUG_CHECK, OPT_COLUMN_TYPES, OPT_ERROR_LOG_FILE, OPT_WRITE_BINLOG, OPT_DUMP_DATE, + OPT_INIT_COMMAND, OPT_MAX_CLIENT_OPTION }; --- mysql-5.1.36/client/mysql.cc 2009-06-16 21:22:52.000000000 +0900 +++ mysql-5.1.36-init-command/client/mysql.cc 2009-07-03 14:32:42.000000000 +0900 @@ -158,6 +158,7 @@ *default_charset= (char*) MYSQL_DEFAULT_CHARSET_NAME; static char *histfile; static char *histfile_tmp; +static char *opt_init_command=0, *init_command=0; static String glob_buffer,old_buffer; static String processed_prompt; static char *full_username=0,*part_username=0,*default_prompt=0; @@ -1391,6 +1392,8 @@ (uchar**) ¤t_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, {"html", 'H', "Produce HTML output.", (uchar**) &opt_html, (uchar**) &opt_html, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0}, + {"init-command", OPT_INIT_COMMAND, "Commands executed right after login to the server.", (uchar**) &opt_init_command, + (uchar**) &opt_init_command, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, {"xml", 'X', "Produce XML output", (uchar**) &opt_xml, (uchar**) &opt_xml, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0}, {"line-numbers", OPT_LINE_NUMBERS, "Write line numbers for errors.", @@ -4146,6 +4149,23 @@ return valid_arg ? start : NullS; } +static void +add_init_command(char *command) +{ + if(!command || !strlen(command)) return; + ulong str_pos = init_command ? strlen(init_command) : 0; + ulong length= init_command ? str_pos : 0; + length += strlen(command) + 1; + if(init_command) { + init_command= (char*)my_realloc((void*)init_command, length, MYF(MY_WME)); + init_command[str_pos] = ';'; + str_pos++; + } + else + init_command= (char*)my_malloc(length, MYF(MY_WME)); + memmove(init_command + str_pos, command, strlen(command)); + init_command[length-1]= 0; +} static int sql_real_connect(char *host,char *database,char *user,char *password, @@ -4182,13 +4202,23 @@ if (shared_memory_base_name) mysql_options(&mysql,MYSQL_SHARED_MEMORY_BASE_NAME,shared_memory_base_name); #endif + if (opt_init_command) + { + add_init_command(opt_init_command); + } if (safe_updates) { - char init_command[100]; - sprintf(init_command, + char safe_updates_command[100]; + sprintf(safe_updates_command, "SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=%lu,SQL_MAX_JOIN_SIZE=%lu", select_limit,max_join_size); + add_init_command(safe_updates_command); + } + if (init_command) + { mysql_options(&mysql, MYSQL_INIT_COMMAND, init_command); + my_free(init_command, MYF(MY_ALLOW_ZERO_PTR)); + init_command= 0; } if (default_charset_used) mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, default_charset); --- mysql-5.1.36/sql-common/client.c 2009-06-16 21:30:00.000000000 +0900 +++ mysql-5.1.36-init-command/sql-common/client.c 2009-07-03 15:06:03.000000000 +0900 @@ -1851,6 +1851,7 @@ in_addr_t ip_addr; struct sockaddr_in sock_addr; ulong pkt_length; + int res_status; NET *net= &mysql->net; #ifdef MYSQL_SERVER thr_alarm_t alarmed; @@ -2438,12 +2439,23 @@ MYSQL_RES *res; if (mysql_real_query(mysql,*ptr, (ulong) strlen(*ptr))) goto error; - if (mysql->fields) - { - if (!(res= cli_use_result(mysql))) - goto error; - mysql_free_result(res); - } + do { + res_status= -1; + if (mysql->fields) + { + puts(ptr); + if (!(res= cli_use_result(mysql))) + goto error; + mysql_free_result(res); + } + if (mysql->last_used_con->server_status & SERVER_MORE_RESULTS_EXISTS) { + net_clear_error(&mysql->net); + mysql->affected_rows= ~(my_ulonglong) 0; + res_status= cli_read_query_result(mysql); + if(res_status>0) + goto error; + } + } while(res_status==0); } mysql->reconnect=reconnect; }