diff options
Diffstat (limited to 'client/mysql.cc')
-rw-r--r-- | client/mysql.cc | 76 |
1 files changed, 40 insertions, 36 deletions
diff --git a/client/mysql.cc b/client/mysql.cc index d56f64289c4..67b97e30ea5 100644 --- a/client/mysql.cc +++ b/client/mysql.cc @@ -137,6 +137,7 @@ static const char *xmlmeta[] = { static char default_pager[FN_REFLEN]; char pager[FN_REFLEN], outfile[FN_REFLEN]; FILE *PAGER, *OUTFILE; +MEM_ROOT hash_mem_root; #include "sslopt-vars.h" @@ -302,8 +303,9 @@ int main(int argc,char *argv[]) !(status.line_buff=batch_readline_init(max_allowed_packet+512,stdin))) exit(1); glob_buffer.realloc(512); - mysql_server_init(0, NULL, server_default_groups); - completion_hash_init(&ht,50); + mysql_server_init(0, NULL, (char**) server_default_groups); + completion_hash_init(&ht, 128); + init_alloc_root(&hash_mem_root, 16384, 0); bzero((char*) &mysql, sizeof(mysql)); if (sql_connect(current_host,current_db,current_user,opt_password, opt_silent)) @@ -387,6 +389,8 @@ sig_handler mysql_end(int sig) } batch_readline_end(status.line_buff); completion_hash_free(&ht); + free_root(&hash_mem_root,MYF(0)); + #endif if (sig >= 0) put_info(sig ? "Aborted" : "Bye", INFO_RESULT); @@ -1170,7 +1174,8 @@ static char *new_command_generator(char *text,int state) static void build_completion_hash(bool skip_rehash,bool write_info) { COMMANDS *cmd=commands; - static MYSQL_RES *databases=0,*tables=0,*fields; + MYSQL_RES *databases=0,*tables=0; + MYSQL_RES *fields; static char ***field_names= 0; MYSQL_ROW database_row,table_row; MYSQL_FIELD *sql_field; @@ -1181,16 +1186,11 @@ static void build_completion_hash(bool skip_rehash,bool write_info) if (status.batch || quick || !current_db) DBUG_VOID_RETURN; // We don't need completion in batches - completion_hash_clean(&ht); if (tables) { mysql_free_result(tables); tables=0; } - if (databases) { - mysql_free_result(databases); - databases=0; - } /* hash SQL commands */ while (cmd->name) { @@ -1200,16 +1200,28 @@ static void build_completion_hash(bool skip_rehash,bool write_info) if (skip_rehash) DBUG_VOID_RETURN; + /* Free old used memory */ + if (field_names) + field_names=0; + completion_hash_clean(&ht); + free_root(&hash_mem_root,MYF(0)); + /* hash MySQL functions (to be implemented) */ /* hash all database names */ - if (mysql_query(&mysql,"show databases")==0) { + if (mysql_query(&mysql,"show databases") == 0) + { if (!(databases = mysql_store_result(&mysql))) put_info(mysql_error(&mysql),INFO_INFO); else { while ((database_row=mysql_fetch_row(databases))) - add_word(&ht,(char*) database_row[0]); + { + char *str=strdup_root(&hash_mem_root, (char*) database_row[0]); + if (str) + add_word(&ht,(char*) str); + } + mysql_free_result(databases); } } /* hash all table names */ @@ -1227,23 +1239,13 @@ You can turn off this feature to get a quicker startup with -A\n\n"); } while ((table_row=mysql_fetch_row(tables))) { - if (!completion_hash_exists(&ht,(char*) table_row[0], - (uint) strlen((const char*) table_row[0]))) - add_word(&ht,table_row[0]); - } - } - } - /* FIXME: free() on small chunks is sloooowwww. glibc bug */ - if (field_names) { - for (i=0; field_names[i]; i++) { - for (j=0; field_names[i][j]; j++) { - my_free(field_names[i][j],MYF(0)); + char *str=strdup_root(&hash_mem_root, (char*) table_row[0]); + if (str && + !completion_hash_exists(&ht,(char*) str, (uint) strlen(str))) + add_word(&ht,str); } - my_free((gptr) field_names[i],MYF(0)); } - my_free((gptr) field_names,MYF(0)); } - field_names=0; /* hash all field names, both with the table prefix and without it */ if (!tables) /* no tables */ @@ -1251,36 +1253,37 @@ You can turn off this feature to get a quicker startup with -A\n\n"); DBUG_VOID_RETURN; } mysql_data_seek(tables,0); - field_names = (char ***) my_malloc(sizeof(char **) * - (uint) (mysql_num_rows(tables)+1), - MYF(MY_WME)); - if (!field_names) + if (!(field_names= (char ***) alloc_root(&hash_mem_root,sizeof(char **) * + (uint) (mysql_num_rows(tables)+1)))) + { + mysql_free_result(tables); DBUG_VOID_RETURN; + } i=0; while ((table_row=mysql_fetch_row(tables))) { if ((fields=mysql_list_fields(&mysql,(const char*) table_row[0],NullS))) { num_fields=mysql_num_fields(fields); - field_names[i] = (char **) my_malloc(sizeof(char *)*(num_fields*2+1), - MYF(0)); - if (!field_names[i]) - { - continue; - } + if (!(field_names[i] = (char **) alloc_root(&hash_mem_root, + sizeof(char *) * + (num_fields*2+1)))) + break; field_names[i][num_fields*2]='\0'; j=0; while ((sql_field=mysql_fetch_field(fields))) { sprintf(buf,"%s.%s",table_row[0],sql_field->name); - field_names[i][j] = my_strdup(buf,MYF(0)); + field_names[i][j] = strdup_root(&hash_mem_root,buf); add_word(&ht,field_names[i][j]); - field_names[i][num_fields+j] = my_strdup(sql_field->name,MYF(0)); + field_names[i][num_fields+j] = strdup_root(&hash_mem_root, + sql_field->name); if (!completion_hash_exists(&ht,field_names[i][num_fields+j], (uint) strlen(field_names[i][num_fields+j]))) add_word(&ht,field_names[i][num_fields+j]); j++; } + mysql_free_result(fields); } else { @@ -1290,6 +1293,7 @@ You can turn off this feature to get a quicker startup with -A\n\n"); } i++; } + mysql_free_result(tables); field_names[i]=0; // End pointer DBUG_VOID_RETURN; } |