blob: 29402f5626152fa663bd6cfd162f9a6554c63785 [file] [log] [blame]
drhb9bb7c12006-06-11 23:41:55 +00001/*
2** 2006 June 10
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** Code for testing the virtual table interfaces. This code
13** is not included in the SQLite library. It is used for automated
14** testing of the SQLite library.
15**
drha967e882006-06-13 01:04:52 +000016** $Id: test8.c,v 1.6 2006/06/13 01:04:53 drh Exp $
drhb9bb7c12006-06-11 23:41:55 +000017*/
18#include "sqliteInt.h"
19#include "tcl.h"
20#include "os.h"
21#include <stdlib.h>
22#include <string.h>
23
danielk197778efaba2006-06-12 06:09:17 +000024/*
25** Global Tcl variable $echo_module is a list. This routine appends
26** the string element zArg to that list in interpreter interp.
27*/
drha967e882006-06-13 01:04:52 +000028static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
danielk197778efaba2006-06-12 06:09:17 +000029 int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
drha967e882006-06-13 01:04:52 +000030 Tcl_SetVar(interp, "echo_module", zArg, flags);
danielk197778efaba2006-06-12 06:09:17 +000031}
32
drha967e882006-06-13 01:04:52 +000033static void appendToEchoTable(Tcl_Interp *interp, const char *zArg){
danielk1977fe3fcbe22006-06-12 12:08:45 +000034 int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
drha967e882006-06-13 01:04:52 +000035 Tcl_SetVar(interp, "echo_module", zArg, flags);
danielk1977fe3fcbe22006-06-12 12:08:45 +000036}
37
danielk19777e6ebfb2006-06-12 11:24:37 +000038/*
39** This function is called from within the echo-modules xCreate and
40** xConnect methods. The argc and argv arguments are copies of those
41** passed to the calling method. This function is responsible for
42** calling sqlite3_declare_vtab() to declare the schema of the virtual
43** table being created or connected.
44**
45** If the constructor was passed just one argument, i.e.:
46**
47** CREATE TABLE t1 AS echo(t2);
48**
49** Then t2 is assumed to be the name of a *real* database table. The
50** schema of the virtual table is declared by passing a copy of the
51** CREATE TABLE statement for the real table to sqlite3_declare_vtab().
52** Hence, the virtual table should have exactly the same column names and
53** types as the real table.
54*/
55static int echoDeclareVtab(sqlite3 *db, int argc, char **argv){
56 int rc = SQLITE_OK;
57
58 if( argc==2 ){
59 sqlite3_stmt *pStmt = 0;
60 sqlite3_prepare(db,
61 "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
62 -1, &pStmt, 0);
63 sqlite3_bind_text(pStmt, 1, argv[1], -1, 0);
64 if( sqlite3_step(pStmt)==SQLITE_ROW ){
65 const char *zCreateTable = sqlite3_column_text(pStmt, 0);
drha75803d2006-06-12 12:50:23 +000066#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19777e6ebfb2006-06-12 11:24:37 +000067 sqlite3_declare_vtab(db, zCreateTable);
drha75803d2006-06-12 12:50:23 +000068#endif
danielk19777e6ebfb2006-06-12 11:24:37 +000069 } else {
70 rc = SQLITE_ERROR;
71 }
72 sqlite3_finalize(pStmt);
73 }
74
75 return rc;
76}
77
drha967e882006-06-13 01:04:52 +000078/* An echo vtab object */
79typedef struct echo_vtab echo_vtab;
80struct echo_vtab {
81 sqlite3_vtab base;
82 Tcl_Interp *interp;
83};
84
drhb9bb7c12006-06-11 23:41:55 +000085/* Methods for the echo module */
86static int echoCreate(
87 sqlite3 *db,
88 const sqlite3_module *pModule,
89 int argc, char **argv,
90 sqlite3_vtab **ppVtab
91){
92 int i;
drha967e882006-06-13 01:04:52 +000093 echo_vtab *pVtab;
drhb9bb7c12006-06-11 23:41:55 +000094
drha967e882006-06-13 01:04:52 +000095 pVtab = sqliteMalloc( sizeof(*pVtab) );
96 *ppVtab = &pVtab->base;
97 pVtab->base.pModule = pModule;
98 pVtab->interp = pModule->pAux;
99 appendToEchoModule(pVtab->interp, "xCreate");
drhb9bb7c12006-06-11 23:41:55 +0000100 for(i=0; i<argc; i++){
drha967e882006-06-13 01:04:52 +0000101 appendToEchoModule(pVtab->interp, argv[i]);
drhb9bb7c12006-06-11 23:41:55 +0000102 }
danielk197778efaba2006-06-12 06:09:17 +0000103
danielk19777e6ebfb2006-06-12 11:24:37 +0000104 echoDeclareVtab(db, argc, argv);
drhb9bb7c12006-06-11 23:41:55 +0000105 return 0;
106}
107static int echoConnect(
108 sqlite3 *db,
109 const sqlite3_module *pModule,
110 int argc, char **argv,
111 sqlite3_vtab **ppVtab
112){
113 int i;
114 Tcl_Interp *interp = pModule->pAux;
drha967e882006-06-13 01:04:52 +0000115 echo_vtab *pVtab;
drhb9bb7c12006-06-11 23:41:55 +0000116
drha967e882006-06-13 01:04:52 +0000117 pVtab = sqliteMalloc( sizeof(*pVtab) );
118 *ppVtab = &pVtab->base;
119 pVtab->base.pModule = pModule;
120 pVtab->interp = pModule->pAux;
drhb9bb7c12006-06-11 23:41:55 +0000121 Tcl_SetVar(interp, "echo_module", "xConnect", TCL_GLOBAL_ONLY);
122 for(i=0; i<argc; i++){
123 Tcl_SetVar(interp, "echo_module", argv[i],
124 TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
125 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000126
127 echoDeclareVtab(db, argc, argv);
drhb9bb7c12006-06-11 23:41:55 +0000128 return 0;
129}
130static int echoDisconnect(sqlite3_vtab *pVtab){
drha967e882006-06-13 01:04:52 +0000131 echo_vtab *p = (echo_vtab*)pVtab;
132 appendToEchoTable(p->interp, "xDisconnect");
133 sqliteFree(pVtab);
drhb9bb7c12006-06-11 23:41:55 +0000134 return 0;
135}
136static int echoDestroy(sqlite3_vtab *pVtab){
drha967e882006-06-13 01:04:52 +0000137 echo_vtab *p = (echo_vtab*)pVtab;
138 Tcl_SetVar(p->interp, "echo_module", "xDestroy",
drhb9bb7c12006-06-11 23:41:55 +0000139 TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
140 return 0;
141}
142
143/*
drha967e882006-06-13 01:04:52 +0000144** The xBestIndex method for the echo module always returns
145** an index of 123.
146*/
147static int echoBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
148 pIdxInfo->idxNum = 123;
149 return SQLITE_OK;
150}
151
152/*
drhb9bb7c12006-06-11 23:41:55 +0000153** A virtual table module that merely echos method calls into TCL
154** variables.
155*/
156static sqlite3_module echoModule = {
danielk197778efaba2006-06-12 06:09:17 +0000157 0, /* iVersion */
158 "echo", /* zName */
159 0, /* pAux */
drhb9bb7c12006-06-11 23:41:55 +0000160 echoCreate,
161 echoConnect,
drha967e882006-06-13 01:04:52 +0000162 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +0000163 echoDisconnect,
164 echoDestroy,
165};
166
167/*
168** Decode a pointer to an sqlite3 object.
169*/
170static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
171 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
172 return TCL_OK;
173}
174
175
176/*
177** Register the echo virtual table module.
178*/
179static int register_echo_module(
180 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
181 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
182 int objc, /* Number of arguments */
183 Tcl_Obj *CONST objv[] /* Command arguments */
184){
185 sqlite3 *db;
186 if( objc!=2 ){
187 Tcl_WrongNumArgs(interp, 1, objv, "DB");
188 return TCL_ERROR;
189 }
190 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
191 echoModule.pAux = interp;
192#ifndef SQLITE_OMIT_VIRTUALTABLE
193 sqlite3_create_module(db, "echo", &echoModule);
194#endif
195 return TCL_OK;
196}
197
198
199/*
200** Register commands with the TCL interpreter.
201*/
202int Sqlitetest8_Init(Tcl_Interp *interp){
203 static struct {
204 char *zName;
205 Tcl_ObjCmdProc *xProc;
206 void *clientData;
207 } aObjCmd[] = {
208 { "register_echo_module", register_echo_module, 0 },
209 };
210 int i;
211 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
212 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
213 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
214 }
215 return TCL_OK;
216}