blob: fc474bb70fc81ca619a6b2017b4e8da456a331b2 [file] [log] [blame]
drh4be8b512006-06-13 23:51:34 +00001/*
2** 2006 June 13
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**
16** The emphasis of this file is a virtual table that provides
17** access to TCL variables.
18**
danielk19779da9d472006-06-14 06:58:15 +000019** $Id: test_tclvar.c,v 1.2 2006/06/14 06:58:16 danielk1977 Exp $
drh4be8b512006-06-13 23:51:34 +000020*/
21#include "sqliteInt.h"
22#include "tcl.h"
23#include "os.h"
24#include <stdlib.h>
25#include <string.h>
26
27typedef struct tclvar_vtab tclvar_vtab;
28typedef struct tclvar_cursor tclvar_cursor;
29
30/*
31** A tclvar virtual-table object
32*/
33struct tclvar_vtab {
34 sqlite3_vtab base;
35 Tcl_Interp *interp;
36};
37
38/* A tclvar cursor object */
39struct tclvar_cursor {
40 sqlite3_vtab_cursor base;
41 Tcl_Obj *pList1, *pList2;
42 int i, j;
43};
44
45/* Methods for the tclvar module */
46static int tclvarConnect(
47 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +000048 void *pAux,
drh4be8b512006-06-13 23:51:34 +000049 int argc, char **argv,
50 sqlite3_vtab **ppVtab
51){
52 tclvar_vtab *pVtab;
53 static const char zSchema[] =
54 "CREATE TABLE whatever(name TEXT, arrayname TEXT, value TEXT)";
55 pVtab = sqliteMalloc( sizeof(*pVtab) );
56 if( pVtab==0 ) return SQLITE_NOMEM;
57 *ppVtab = &pVtab->base;
danielk19779da9d472006-06-14 06:58:15 +000058 pVtab->interp = (Tcl_Interp *)pAux;
drh4be8b512006-06-13 23:51:34 +000059#ifndef SQLITE_OMIT_VIRTUALTABLE
60 sqlite3_declare_vtab(db, zSchema);
61#endif
62 return SQLITE_OK;
63}
64/* Note that for this virtual table, the xCreate and xConnect
65** methods are identical. */
66static int tclvarDisconnect(sqlite3_vtab *pVtab){
67 free(pVtab);
68}
69/* The xDisconnect and xDestroy methods are also the same */
70
71static int tclvarOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
72 tclvar_cursor *pCur;
73 pCur = sqliteMalloc(sizeof(tclvar_cursor));
74 *ppCursor = &pCur->base;
75 return SQLITE_OK;
76}
77
78static int tclvarClose(sqlite3_vtab_cursor *cur){
79 tclvar_cursor *pCur = (tclvar_cursor *)cur;
80 if( pCur->pList1 ){
81 Tcl_DecrRefCount(pCur->pList1);
82 }
83 if( pCur->pList2 ){
84 Tcl_DecrRefCount(pCur->pList2);
85 }
86 sqliteFree(pCur);
87 return SQLITE_OK;
88}
89
90static int tclvarNext(sqlite3_vtab_cursor *cur){
91 tclvar_cursor *pCur = (tclvar_cursor *)cur;
92 return 0;
93}
94
95static int tclvarColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
96 tclvar_cursor *pCur = (tclvar_cursor*)cur;
97 return SQLITE_OK;
98}
99
100static int tclvarRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
101 tclvar_cursor *pCur = (tclvar_cursor*)cur;
102 return SQLITE_OK;
103}
104
105static int tclvarFilter(
106 sqlite3_vtab_cursor *pVtabCursor,
107 int idxNum, const char *idxStr,
108 int argc, sqlite3_value **argv
109){
110 tclvar_cursor *pCur = (tclvar_cursor *)pVtabCursor;
111 tclvar_vtab *pVtab = (tclvar_vtab *)pCur->base.pVtab;
112 return 0;
113}
114
115/*
116*/
117static int tclvarBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
118 tclvar_vtab *pVtab = (tclvar_vtab *)tab;
119 return SQLITE_OK;
120}
121
122/*
123** A virtual table module that merely echos method calls into TCL
124** variables.
125*/
126static sqlite3_module tclvarModule = {
127 0, /* iVersion */
128 "tclvar", /* zName */
129 0, /* pAux */
130 tclvarConnect,
131 tclvarConnect,
132 tclvarBestIndex,
133 tclvarDisconnect,
134 tclvarDisconnect,
135 tclvarOpen, /* xOpen - open a cursor */
136 tclvarClose, /* xClose - close a cursor */
137 tclvarFilter, /* xFilter - configure scan constraints */
138 tclvarNext, /* xNext - advance a cursor */
139 tclvarColumn, /* xColumn - read data */
140 tclvarRowid /* xRowid - read data */
141};
142
143/*
144** Decode a pointer to an sqlite3 object.
145*/
146static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
147 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
148 return TCL_OK;
149}
150
151
152/*
153** Register the echo virtual table module.
154*/
155static int register_tclvar_module(
156 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
157 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
158 int objc, /* Number of arguments */
159 Tcl_Obj *CONST objv[] /* Command arguments */
160){
161 sqlite3 *db;
162 if( objc!=2 ){
163 Tcl_WrongNumArgs(interp, 1, objv, "DB");
164 return TCL_ERROR;
165 }
166 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
167 tclvarModule.pAux = interp;
168#ifndef SQLITE_OMIT_VIRTUALTABLE
169 sqlite3_create_module(db, "tclvar", &tclvarModule);
170#endif
171 return TCL_OK;
172}
173
174
175/*
176** Register commands with the TCL interpreter.
177*/
178int Sqlitetesttclvar_Init(Tcl_Interp *interp){
179 static struct {
180 char *zName;
181 Tcl_ObjCmdProc *xProc;
182 void *clientData;
183 } aObjCmd[] = {
184 { "register_tclvar_module", register_tclvar_module, 0 },
185 };
186 int i;
187 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
188 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
189 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
190 }
191 return TCL_OK;
192}