blob: daa86e780776a1e9bf7cc9efcda7032b299ef85d [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**
danielk1977d1ab1ba2006-06-15 04:28:13 +000019** $Id: test_tclvar.c,v 1.3 2006/06/15 04:28:13 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);
danielk1977d1ab1ba2006-06-15 04:28:13 +000068 return SQLITE_OK;
drh4be8b512006-06-13 23:51:34 +000069}
70/* The xDisconnect and xDestroy methods are also the same */
71
72static int tclvarOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
73 tclvar_cursor *pCur;
74 pCur = sqliteMalloc(sizeof(tclvar_cursor));
75 *ppCursor = &pCur->base;
76 return SQLITE_OK;
77}
78
79static int tclvarClose(sqlite3_vtab_cursor *cur){
80 tclvar_cursor *pCur = (tclvar_cursor *)cur;
81 if( pCur->pList1 ){
82 Tcl_DecrRefCount(pCur->pList1);
83 }
84 if( pCur->pList2 ){
85 Tcl_DecrRefCount(pCur->pList2);
86 }
87 sqliteFree(pCur);
88 return SQLITE_OK;
89}
90
91static int tclvarNext(sqlite3_vtab_cursor *cur){
92 tclvar_cursor *pCur = (tclvar_cursor *)cur;
93 return 0;
94}
95
96static int tclvarColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
97 tclvar_cursor *pCur = (tclvar_cursor*)cur;
98 return SQLITE_OK;
99}
100
101static int tclvarRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
102 tclvar_cursor *pCur = (tclvar_cursor*)cur;
103 return SQLITE_OK;
104}
105
106static int tclvarFilter(
107 sqlite3_vtab_cursor *pVtabCursor,
108 int idxNum, const char *idxStr,
109 int argc, sqlite3_value **argv
110){
111 tclvar_cursor *pCur = (tclvar_cursor *)pVtabCursor;
112 tclvar_vtab *pVtab = (tclvar_vtab *)pCur->base.pVtab;
113 return 0;
114}
115
116/*
117*/
118static int tclvarBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
119 tclvar_vtab *pVtab = (tclvar_vtab *)tab;
120 return SQLITE_OK;
121}
122
123/*
124** A virtual table module that merely echos method calls into TCL
125** variables.
126*/
127static sqlite3_module tclvarModule = {
128 0, /* iVersion */
129 "tclvar", /* zName */
drh4be8b512006-06-13 23:51:34 +0000130 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;
drh4be8b512006-06-13 23:51:34 +0000167#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977d1ab1ba2006-06-15 04:28:13 +0000168 sqlite3_create_module(db, "tclvar", &tclvarModule, (void *)interp);
drh4be8b512006-06-13 23:51:34 +0000169#endif
170 return TCL_OK;
171}
172
173
174/*
175** Register commands with the TCL interpreter.
176*/
177int Sqlitetesttclvar_Init(Tcl_Interp *interp){
178 static struct {
179 char *zName;
180 Tcl_ObjCmdProc *xProc;
181 void *clientData;
182 } aObjCmd[] = {
183 { "register_tclvar_module", register_tclvar_module, 0 },
184 };
185 int i;
186 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
187 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
188 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
189 }
190 return TCL_OK;
191}