/* ** 2006 June 10 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** Code for testing the virtual table interfaces. This code ** is not included in the SQLite library. It is used for automated ** testing of the SQLite library. ** ** $Id: test8.c,v 1.4 2006/06/12 12:08:45 danielk1977 Exp $ */ #include "sqliteInt.h" #include "tcl.h" #include "os.h" #include #include /* ** Global Tcl variable $echo_module is a list. This routine appends ** the string element zArg to that list in interpreter interp. */ static void appendToEchoModule(const sqlite3_module *pModule, const char *zArg){ int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY); Tcl_SetVar((Tcl_Interp *)(pModule->pAux), "echo_module", zArg, flags); } static void appendToEchoTable(const sqlite3_vtab *pTab, const char *zArg){ int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY); Tcl_SetVar((Tcl_Interp *)(pTab), "echo_module", zArg, flags); } /* ** This function is called from within the echo-modules xCreate and ** xConnect methods. The argc and argv arguments are copies of those ** passed to the calling method. This function is responsible for ** calling sqlite3_declare_vtab() to declare the schema of the virtual ** table being created or connected. ** ** If the constructor was passed just one argument, i.e.: ** ** CREATE TABLE t1 AS echo(t2); ** ** Then t2 is assumed to be the name of a *real* database table. The ** schema of the virtual table is declared by passing a copy of the ** CREATE TABLE statement for the real table to sqlite3_declare_vtab(). ** Hence, the virtual table should have exactly the same column names and ** types as the real table. */ static int echoDeclareVtab(sqlite3 *db, int argc, char **argv){ int rc = SQLITE_OK; if( argc==2 ){ sqlite3_stmt *pStmt = 0; sqlite3_prepare(db, "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?", -1, &pStmt, 0); sqlite3_bind_text(pStmt, 1, argv[1], -1, 0); if( sqlite3_step(pStmt)==SQLITE_ROW ){ const char *zCreateTable = sqlite3_column_text(pStmt, 0); sqlite3_declare_vtab(db, zCreateTable); } else { rc = SQLITE_ERROR; } sqlite3_finalize(pStmt); } return rc; } /* Methods for the echo module */ static int echoCreate( sqlite3 *db, const sqlite3_module *pModule, int argc, char **argv, sqlite3_vtab **ppVtab ){ int i; *ppVtab = pModule->pAux; appendToEchoModule(pModule, "xCreate"); for(i=0; ipAux; *ppVtab = pModule->pAux; Tcl_SetVar(interp, "echo_module", "xConnect", TCL_GLOBAL_ONLY); for(i=0; i