197
200
static char default_pager[FN_REFLEN];
198
201
static char pager[FN_REFLEN], outfile[FN_REFLEN];
199
202
static FILE *PAGER, *OUTFILE;
200
static MEM_ROOT hash_mem_root;
201
203
static uint32_t prompt_counter;
202
204
static char delimiter[16]= DEFAULT_DELIMITER;
203
205
static uint32_t delimiter_length= 1;
1098
completion_hash_init(&ht, 128);
1099
init_alloc_root(&hash_mem_root, 16384, 0);
1100
1100
memset(&drizzle, 0, sizeof(drizzle));
1101
1101
if (sql_connect(current_host,current_db,current_user,opt_password,
1193
1193
my_rename(histfile_tmp, histfile, MYF(MY_WME));
1195
1195
batch_readline_end(status.line_buff);
1196
completion_hash_free(&ht);
1197
free_root(&hash_mem_root,MYF(0));
1200
1198
put_info(sig ? _("Aborted") : _("Bye"), INFO_RESULT,0,0);
2221
2219
return (char**) 0;
2222
inline string lower_string(const string &from_string)
2224
string to_string= from_string;
2225
transform(to_string.begin(), to_string.end(),
2226
to_string.begin(), ::tolower);
2230
class CompletionMatch :
2231
public unary_function<const string&, bool>
2235
CompletionMatch(string text) : match_text(text)
2237
transform(match_text.begin(), match_text.end(),
2238
match_text.begin(), ::tolower);
2240
inline bool operator() (const pair<string,string> &match_against) const
2243
lower_string(match_against.first.substr(0,match_text.size()));
2244
return (sub_match == match_text);
2245
/*(cout << "Matching " << match_text << " against " << match_against.first <<end;
2246
if (match_text.compare(0, match_text.size(),
2247
match_against.first, 0,
2248
match_text.size()) == 0)
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
cout << "trying to find: " << text << endl;
2263
string match_text(text);
2264
if (match_text.size() == 0)
2266
/*cout << "doing the big one " << endl;
2267
for(completion_iter= completion_map.begin();
2268
completion_iter!= completion_map.end();
2270
cout << "SET CONTAINS: " << (*completion_iter).second << endl; */
2271
completion_iter= completion_map.begin();
2274
completion_iter= find_if(completion_map.begin(), completion_map.end(),
2275
CompletionMatch(match_text));
2277
if (completion_iter == completion_map.end())
2279
char *result= (char *)malloc((*completion_iter).second.size()+1);
2280
strcpy(result, (*completion_iter).second.c_str());
2304
2285
/* Build up the completion hash */
2306
2287
static void build_completion_hash(bool rehash, bool write_info)
2308
2289
COMMANDS *cmd=commands;
2309
2290
DRIZZLE_RES *databases=0,*tables=0;
2310
2291
DRIZZLE_RES *fields;
2311
static char ***field_names= 0;
2312
2292
DRIZZLE_ROW database_row,table_row;
2313
2293
DRIZZLE_FIELD *sql_field;
2314
char buf[NAME_LEN*2+2]; // table name plus field name plus 2
2294
string tmp_str, tmp_str_lower;
2318
2297
if (status.batch || quick || !current_db)
2323
/* Free old used memory */
2326
completion_hash_clean(&ht);
2327
free_root(&hash_mem_root,MYF(0));
2302
completion_map.clear();
2329
2304
/* hash this file's known subset of SQL commands */
2330
2305
while (cmd->name) {
2331
add_word(&ht,(char*) cmd->name);
2307
tmp_str_lower= lower_string(tmp_str);
2308
completion_map[tmp_str_lower]= tmp_str;
2344
2321
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);
2323
tmp_str= database_row[0];
2324
tmp_str_lower= lower_string(tmp_str);
2325
completion_map[tmp_str_lower]= tmp_str;
2350
2327
drizzleclient_free_result(databases);
2399
2369
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
2372
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]);
2374
tmp_str=table_row[0];
2375
tmp_str.append(".");
2376
tmp_str.append(sql_field->name);
2377
tmp_str_lower= lower_string(tmp_str);
2378
completion_map[tmp_str_lower]= tmp_str;
2380
tmp_str=sql_field->name;
2381
tmp_str_lower= lower_string(tmp_str);
2382
completion_map[tmp_str_lower]= tmp_str;
2424
2385
drizzleclient_free_result(fields);
2432
2389
drizzleclient_free_result(tables);
2433
field_names[i]=0; // End pointer
2390
completion_iter= completion_map.begin();
2437
2393
/* for gnu readline */