MCPcopy Create free account
hub / github.com/bloomberg/comdb2 / db_generator

Function db_generator

tools/cdb2sql/cdb2sql.cpp:304–383  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

302}
303
304static char *db_generator(const int state, const char *sql)
305{
306 static char **db_words;
307 static int list_index, len;
308 const char *name;
309 if (!state) { //if state is 0 get the completions from the db
310 cdb2_hndl_tp *cdb2h_2 = NULL; // use a new db handle
311 if (db_words) {
312 char *wrd;
313 list_index = 0;
314 while ((wrd = db_words[list_index])) {
315 free(wrd);
316 db_words[list_index] = NULL;
317 list_index++;
318 }
319 free(db_words);
320 db_words = NULL;
321 }
322
323 list_index = 0;
324 int rc;
325 int flags = 0;
326 char *type = dbtype;
327
328 if (dbhostname) {
329 type = dbhostname;
330 flags |= CDB2_DIRECT_CPU;
331 }
332
333 if (isadmin)
334 flags |= CDB2_ADMIN;
335
336 rc = cdb2_open(&cdb2h_2, dbname, type, flags);
337 if (rc) {
338 if (debug_trace)
339 fprintf(stderr, "cdb2_open rc %d %s\n", rc, cdb2_errstr(cdb2h));
340 cdb2_close(cdb2h_2);
341 return ((char *) NULL);
342 }
343 rc = cdb2_run_statement(cdb2h_2, sql);
344 if (rc) {
345 if (debug_trace)
346 fprintf(stderr, "failed to run sql '%s'\n", sql);
347 return ((char *) NULL);
348 }
349
350 int ncols = cdb2_numcolumns(cdb2h_2);
351 assert(ncols == 1);
352 int count = 0;
353 int sz = 0;
354 while ((rc = cdb2_next_record(cdb2h_2)) == CDB2_OK) {
355 if ( sz < count + 1 ) {
356 sz = (sz == 0) ? 32 : sz * 2;
357 char **m = (char**) realloc(db_words, sz * sizeof(char *));
358 if (!m) {
359 fprintf(stderr, "error with malloc/realloc\n");
360 abort();
361 break;

Callers 4

tunables_generatorFunction · 0.85
tables_generatorFunction · 0.85
generic_generatorFunction · 0.85

Calls 10

freeFunction · 0.85
cdb2_openFunction · 0.85
cdb2_errstrFunction · 0.85
cdb2_closeFunction · 0.85
cdb2_run_statementFunction · 0.85
cdb2_numcolumnsFunction · 0.85
cdb2_next_recordFunction · 0.85
reallocFunction · 0.85
cdb2_column_valueFunction · 0.85
strdupFunction · 0.50

Tested by

no test coverage detected