154
static map<string, string>::iterator completion_iter;
155
static map<string, string>::iterator completion_end;
156
static map<string, string> completion_map;
157
static string completion_string;
153
159
static char **defaults_argv;
155
161
enum enum_info_type { INFO_INFO,INFO_ERROR,INFO_RESULT};
197
203
static char default_pager[FN_REFLEN];
198
204
static char pager[FN_REFLEN], outfile[FN_REFLEN];
199
205
static FILE *PAGER, *OUTFILE;
200
static MEM_ROOT hash_mem_root;
201
206
static uint32_t prompt_counter;
202
207
static char delimiter[16]= DEFAULT_DELIMITER;
203
208
static uint32_t delimiter_length= 1;
1098
completion_hash_init(&ht, 128);
1099
init_alloc_root(&hash_mem_root, 16384, 0);
1100
1103
memset(&drizzle, 0, sizeof(drizzle));
1101
1104
if (sql_connect(current_host,current_db,current_user,opt_password,
1193
1196
my_rename(histfile_tmp, histfile, MYF(MY_WME));
1195
1198
batch_readline_end(status.line_buff);
1196
completion_hash_free(&ht);
1197
free_root(&hash_mem_root,MYF(0));
1200
1201
put_info(sig ? _("Aborted") : _("Bye"), INFO_RESULT,0,0);
2221
2222
return (char**) 0;
2225
inline string lower_string(const string &from_string)
2227
string to_string= from_string;
2228
transform(to_string.begin(), to_string.end(),
2229
to_string.begin(), ::tolower);
2232
inline string lower_string(const char * from_string)
2234
string to_string= from_string;
2235
return lower_string(to_string);
2239
class CompletionMatch :
2240
public unary_function<const string&, bool>
2245
CompletionMatch(string text) : match_text(text) {}
2246
inline bool operator() (const pair<string,string> &match_against) const
2249
lower_string(match_against.first.substr(0,match_text.size()));
2250
return match_func(sub_match,match_text);
2225
char *new_command_generator(const char *text,int state)
2257
char *new_command_generator(const char *text, int state)
2234
textlen=(uint32_t) strlen(text);
2237
{ /* lookup in the hash */
2242
b = find_all_matches(&ht,text,(uint32_t) strlen(text),&len);
2250
ptr= strdup(e->str);
2256
{ /* traverse the entire hash, ugly but works */
2260
/* find the first used bucket */
2261
for (i=0 ; i < ht.nTableSize ; i++)
2263
if (ht.arBuckets[i])
2265
b = ht.arBuckets[i];
2273
{ /* find valid entry in bucket */
2274
if ((uint32_t) strlen(e->str) == b->nKeyLength)
2275
ptr = strdup(e->str);
2276
/* find the next used entry */
2279
{ /* find the next used bucket */
2283
for (i++ ; i<ht.nTableSize; i++)
2285
if (ht.arBuckets[i])
2287
b = ht.arBuckets[i];
2262
completion_string= lower_string(text);
2263
if (completion_string.size() == 0)
2265
completion_iter= completion_map.begin();
2266
completion_end= completion_map.end();
2270
completion_iter= find_if(completion_map.begin(), completion_map.end(),
2271
CompletionMatch<equal_to<string> >(completion_string));
2272
completion_end= find_if(completion_iter, completion_map.end(),
2273
CompletionMatch<not_equal_to<string> >(completion_string));
2276
if (completion_iter == completion_end || (size_t)state > completion_map.size())
2278
char *result= (char *)malloc((*completion_iter).second.size()+1);
2279
strcpy(result, (*completion_iter).second.c_str());
2304
2284
/* Build up the completion hash */
2306
2286
static void build_completion_hash(bool rehash, bool write_info)
2308
2288
COMMANDS *cmd=commands;
2309
2289
DRIZZLE_RES *databases=0,*tables=0;
2310
2290
DRIZZLE_RES *fields;
2311
static char ***field_names= 0;
2312
2291
DRIZZLE_ROW database_row,table_row;
2313
2292
DRIZZLE_FIELD *sql_field;
2314
char buf[NAME_LEN*2+2]; // table name plus field name plus 2
2293
string tmp_str, tmp_str_lower;
2318
2296
if (status.batch || quick || !current_db)
2323
/* Free old used memory */
2326
completion_hash_clean(&ht);
2327
free_root(&hash_mem_root,MYF(0));
2301
completion_map.clear();
2329
2303
/* hash this file's known subset of SQL commands */
2330
2304
while (cmd->name) {
2331
add_word(&ht,(char*) cmd->name);
2306
tmp_str_lower= lower_string(tmp_str);
2307
completion_map[tmp_str_lower]= tmp_str;
2344
2320
while ((database_row=drizzleclient_fetch_row(databases)))
2346
char *str=strdup_root(&hash_mem_root, (char*) database_row[0]);
2348
add_word(&ht,(char*) str);
2322
tmp_str= database_row[0];
2323
tmp_str_lower= lower_string(tmp_str);
2324
completion_map[tmp_str_lower]= tmp_str;
2350
2326
drizzleclient_free_result(databases);
2366
2342
while ((table_row=drizzleclient_fetch_row(tables)))
2368
char *str=strdup_root(&hash_mem_root, (char*) table_row[0]);
2370
!completion_hash_exists(&ht,(char*) str, (uint32_t) strlen(str)))
2344
tmp_str= table_row[0];
2345
tmp_str_lower= lower_string(tmp_str);
2346
completion_map[tmp_str_lower]= tmp_str;
2381
2356
drizzleclient_data_seek(tables,0);
2382
if (!(field_names= (char ***) alloc_root(&hash_mem_root,sizeof(char **) *
2383
(uint32_t) (drizzleclient_num_rows(tables)+1))))
2385
drizzleclient_free_result(tables);
2389
2358
while ((table_row=drizzleclient_fetch_row(tables)))
2393
query.append("show fields in '");
2362
query.append("show fields in `");
2394
2363
query.append(table_row[0]);
2397
if ((drizzleclient_real_query(&drizzle, query.c_str(), query.length()) == 0))
2366
if (drizzleclient_query(&drizzle, query.c_str()) == 0)
2399
2368
fields= drizzleclient_store_result(&drizzle);
2402
num_fields=drizzleclient_num_fields(fields);
2403
if (!(field_names[i] = (char **) alloc_root(&hash_mem_root,
2407
drizzleclient_free_result(fields);
2410
field_names[i][num_fields*2]= '\0';
2412
2371
while ((sql_field=drizzleclient_fetch_field(fields)))
2414
sprintf(buf,"%.64s.%.64s",table_row[0],sql_field->name);
2415
field_names[i][j] = strdup_root(&hash_mem_root,buf);
2416
add_word(&ht,field_names[i][j]);
2417
field_names[i][num_fields+j] = strdup_root(&hash_mem_root,
2419
if (!completion_hash_exists(&ht,field_names[i][num_fields+j],
2420
(uint32_t) strlen(field_names[i][num_fields+j])))
2421
add_word(&ht,field_names[i][num_fields+j]);
2373
tmp_str=table_row[0];
2374
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;
2379
tmp_str=sql_field->name;
2380
tmp_str_lower= lower_string(tmp_str);
2381
completion_map[tmp_str_lower]= tmp_str;
2424
2384
drizzleclient_free_result(fields);
2432
2388
drizzleclient_free_result(tables);
2433
field_names[i]=0; // End pointer
2389
completion_iter= completion_map.begin();
2437
2392
/* for gnu readline */