Skip to content

Commit

Permalink
comdb2_systables: system table to list all system tables (bloomberg#1111
Browse files Browse the repository at this point in the history
)

* Also added '@ls systables' to cdb2sql as shortcut
  • Loading branch information
nirbhayc authored Sep 13, 2018
1 parent 4a9fa6b commit c29df69
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 2 deletions.
1 change: 1 addition & 0 deletions sqlite/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ add_library(sqlite
ext/comdb2/timeseries.c
ext/comdb2/repl_stats.c
ext/comdb2/sqlpoolqueue.c
ext/comdb2/systables.c
ext/misc/completion.c
ext/misc/json1.c
ext/expert/sqlite3expert.c
Expand Down
1 change: 1 addition & 0 deletions sqlite/ext/comdb2/comdb2systblInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const sqlite3_module systblTimeseriesModule;
const sqlite3_module systblReplStatsModule;
const sqlite3_module systblActivelocksModule;
const sqlite3_module systblLogicalOpsModule;
const sqlite3_module systblSystabsModule;

int systblTypeSamplesInit(sqlite3 *db);
int systblRepNetQueueStatInit(sqlite3 *db);
Expand Down
175 changes: 175 additions & 0 deletions sqlite/ext/comdb2/systables.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
Copyright 2018 Bloomberg Finance L.P.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#if (!defined(SQLITE_CORE) || defined(SQLITE_BUILDING_FOR_COMDB2)) && \
!defined(SQLITE_OMIT_VIRTUALTABLE)

#if defined(SQLITE_BUILDING_FOR_COMDB2) && !defined(SQLITE_CORE)
#define SQLITE_CORE 1
#endif

#include <assert.h>
#include "sqliteInt.h"
#include "hash.h"
#include "comdb2systbl.h"
#include "comdb2systblInt.h"

/*
comdb2_systabs: System tables
*/

typedef struct {
sqlite3_vtab_cursor base; /* Base class - must be first */
sqlite3_int64 rowid; /* Row ID */
Hash *tabs; /* Registered system tables */
HashElem *current; /* Current system table */
} systbl_systabs_cursor;

typedef struct {
sqlite3_vtab base; /* Base class - must be first */
sqlite3 *db; /* To access registered system tables */
} systbl_systabs_vtab; /* such name !! */

/* Column numbers (always keep the below table definition in sync). */
enum { SYSTABS_COLUMN_NAME };

static int systblSystabsConnect(sqlite3 *db, void *pAux, int argc,
const char *const *argv, sqlite3_vtab **ppVtab,
char **pErr)
{
int rc;
systbl_systabs_vtab *systabs;

rc = sqlite3_declare_vtab(db, "CREATE TABLE comdb2_systabs(\"name\")");

if (rc == SQLITE_OK) {
if ((systabs = sqlite3_malloc(sizeof(systbl_systabs_vtab))) == 0) {
return SQLITE_NOMEM;
}
memset(systabs, 0, sizeof(systbl_systabs_vtab));
systabs->db = db;
}

*ppVtab = (sqlite3_vtab *)systabs;

return rc;
}

static int systblSystabsBestIndex(sqlite3_vtab *tab,
sqlite3_index_info *pIdxInfo)
{
return SQLITE_OK;
}

static int systblSystabsDisconnect(sqlite3_vtab *pVtab)
{
sqlite3_free(pVtab);
return SQLITE_OK;
}

static int systblSystabsOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor)
{
systbl_systabs_vtab *tab = (systbl_systabs_vtab *)p;

systbl_systabs_cursor *cur = sqlite3_malloc(sizeof(systbl_systabs_cursor));
if (cur == 0) {
return SQLITE_NOMEM;
}
memset(cur, 0, sizeof(*cur));
cur->tabs = &tab->db->aModule;
cur->current = sqliteHashFirst(cur->tabs);

*ppCursor = &cur->base;
return SQLITE_OK;
}

static int systblSystabsClose(sqlite3_vtab_cursor *cur)
{
sqlite3_free(cur);
return SQLITE_OK;
}

static int systblSystabsFilter(sqlite3_vtab_cursor *pVtabCursor, int idxNum,
const char *idxStr, int argc,
sqlite3_value **argv)
{
systbl_systabs_cursor *pCur = (systbl_systabs_cursor *)pVtabCursor;
pCur->rowid = 0;
return SQLITE_OK;
}

static int systblSystabsNext(sqlite3_vtab_cursor *cur)
{
systbl_systabs_cursor *pCur = (systbl_systabs_cursor *)cur;
pCur->current = sqliteHashNext(pCur->current);
pCur->rowid++;
return SQLITE_OK;
}

static int systblSystabsEof(sqlite3_vtab_cursor *cur)
{
systbl_systabs_cursor *pCur = (systbl_systabs_cursor *)cur;
return (pCur->rowid >= pCur->tabs->count) ? 1 : 0;
}

static int systblSystabsColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx,
int pos)
{
systbl_systabs_cursor *pCur = (systbl_systabs_cursor *)cur;
switch (pos) {
case SYSTABS_COLUMN_NAME:
sqlite3_result_text(ctx, pCur->current->pKey, -1, NULL);
break;
default:
assert(0);
};

return SQLITE_OK;
}

static int systblSystabsRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid)
{
systbl_systabs_cursor *pCur = (systbl_systabs_cursor *)cur;
*pRowid = pCur->rowid;

return SQLITE_OK;
}

const sqlite3_module systblSystabsModule = {
0, /* iVersion */
0, /* xCreate */
systblSystabsConnect, /* xConnect */
systblSystabsBestIndex, /* xBestIndex */
systblSystabsDisconnect, /* xDisconnect */
0, /* xDestroy */
systblSystabsOpen, /* xOpen - open a cursor */
systblSystabsClose, /* xClose - close a cursor */
systblSystabsFilter, /* xFilter - configure scan constraints */
systblSystabsNext, /* xNext - advance a cursor */
systblSystabsEof, /* xEof - check for end of scan */
systblSystabsColumn, /* xColumn - read data */
systblSystabsRowid, /* xRowid - read data */
0, /* xUpdate */
0, /* xBegin */
0, /* xSync */
0, /* xCommit */
0, /* xRollback */
0, /* xFindMethod */
0, /* xRename */
};

#endif /* (!defined(SQLITE_CORE) || defined(SQLITE_BUILDING_FOR_COMDB2)) \
&& !defined(SQLITE_OMIT_VIRTUALTABLE) */
2 changes: 2 additions & 0 deletions sqlite/ext/comdb2/tables.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ int comdb2SystblInit(
rc = sqlite3_create_module(db, "comdb2_repl_stats", &systblReplStatsModule, 0);
if (rc == SQLITE_OK)
rc = sqlite3_create_module(db, "comdb2_logical_operations", &systblLogicalOpsModule, 0);
if (rc == SQLITE_OK)
rc = sqlite3_create_module(db, "comdb2_systables", &systblSystabsModule, 0);
if (rc == SQLITE_OK)
rc = systblTypeSamplesInit(db);
if (rc == SQLITE_OK)
Expand Down
38 changes: 38 additions & 0 deletions tests/cdb2sql.test/t00.expected
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,44 @@
(tablename='t1')
(tablename='t2')
unknown @ls sub-command foo
(name='comdb2_appsock_handlers')
(name='comdb2_clientstats')
(name='comdb2_cluster')
(name='comdb2_columns')
(name='comdb2_completion')
(name='comdb2_constraints')
(name='comdb2_keycomponents')
(name='comdb2_keys')
(name='comdb2_keywords')
(name='comdb2_limits')
(name='comdb2_locks')
(name='comdb2_logical_operations')
(name='comdb2_metrics')
(name='comdb2_net_userfuncs')
(name='comdb2_opcode_handlers')
(name='comdb2_plugins')
(name='comdb2_procedures')
(name='comdb2_queues')
(name='comdb2_repl_stats')
(name='comdb2_replication_netqueue')
(name='comdb2_sqlpool_queue')
(name='comdb2_systables')
(name='comdb2_tablepermissions')
(name='comdb2_tables')
(name='comdb2_tablesizes')
(name='comdb2_threadpools')
(name='comdb2_timepartevents')
(name='comdb2_timepartitions')
(name='comdb2_timepartshards')
(name='comdb2_timeseries')
(name='comdb2_transaction_logs')
(name='comdb2_triggers')
(name='comdb2_tunables')
(name='comdb2_type_samples')
(name='comdb2_users')
(name='generate_series')
(name='json_each')
(name='json_tree')
table name required
table name required
Columns:
Expand Down
1 change: 1 addition & 0 deletions tests/cdb2sql.test/t00.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CREATE TABLE t2(i INT PRIMARY KEY, FOREIGN KEY (i) REFERENCES t1(i)) $$
@ls tables
@list tables
@ls foo
@ls systables

@desc
@describe
Expand Down
39 changes: 39 additions & 0 deletions tests/comdb2sys.test/comdb2sys.expected
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,42 @@
[SELECT COUNT(*)=1 FROM comdb2_appsock_handlers WHERE name = 'newsql'] rc 0
(COUNT(*)=1=1)
[SELECT COUNT(*)=1 FROM comdb2_opcode_handlers WHERE name = 'blockop'] rc 0
(name='comdb2_appsock_handlers')
(name='comdb2_clientstats')
(name='comdb2_cluster')
(name='comdb2_columns')
(name='comdb2_completion')
(name='comdb2_constraints')
(name='comdb2_keycomponents')
(name='comdb2_keys')
(name='comdb2_keywords')
(name='comdb2_limits')
(name='comdb2_locks')
(name='comdb2_logical_operations')
(name='comdb2_metrics')
(name='comdb2_net_userfuncs')
(name='comdb2_opcode_handlers')
(name='comdb2_plugins')
(name='comdb2_procedures')
(name='comdb2_queues')
(name='comdb2_repl_stats')
(name='comdb2_replication_netqueue')
(name='comdb2_sqlpool_queue')
(name='comdb2_systables')
(name='comdb2_tablepermissions')
(name='comdb2_tables')
(name='comdb2_tablesizes')
(name='comdb2_threadpools')
(name='comdb2_timepartevents')
(name='comdb2_timepartitions')
(name='comdb2_timepartshards')
(name='comdb2_timeseries')
(name='comdb2_transaction_logs')
(name='comdb2_triggers')
(name='comdb2_tunables')
(name='comdb2_type_samples')
(name='comdb2_users')
(name='generate_series')
(name='json_each')
(name='json_tree')
[SELECT * FROM comdb2_systables ORDER BY name] rc 0
1 change: 1 addition & 0 deletions tests/comdb2sys.test/comdb2sys.req
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ SELECT name FROM comdb2_threadpools ORDER BY name;
SELECT COUNT(*)=1 FROM comdb2_plugins WHERE name = 'newsql';
SELECT COUNT(*)=1 FROM comdb2_appsock_handlers WHERE name = 'newsql';
SELECT COUNT(*)=1 FROM comdb2_opcode_handlers WHERE name = 'blockop';
SELECT * FROM comdb2_systables ORDER BY name;
8 changes: 6 additions & 2 deletions tools/cdb2sql/cdb2sql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,12 @@ static int process_escape(const char *cmdstr)
tok = strtok_r(NULL, delims, &lasts);
if (!tok || strcasecmp(tok, "tables") == 0) {
int start_time_ms, run_time_ms;
const char *sql = "SELECT tablename FROM comdb2_tables";

const char *sql =
"SELECT tablename FROM comdb2_tables order by tablename";
run_statement(sql, 0, NULL, &start_time_ms, &run_time_ms);
} else if (strcasecmp(tok, "systables") == 0) {
int start_time_ms, run_time_ms;
const char *sql = "SELECT name FROM comdb2_systables order by name";
run_statement(sql, 0, NULL, &start_time_ms, &run_time_ms);
} else {
fprintf(stderr, "unknown @ls sub-command %s\n", tok);
Expand Down

0 comments on commit c29df69

Please sign in to comment.