blob: 97e848346052d7d1204cbd156f8d68db6fe6c7ea [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**
danielk197778efaba2006-06-12 06:09:17 +000016** $Id: test8.c,v 1.2 2006/06/12 06:09:19 danielk1977 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*/
28static void appendToEchoModule(const sqlite3_module *pModule, const char *zArg){
29 int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
30 Tcl_SetVar((Tcl_Interp *)(pModule->pAux), "echo_module", zArg, flags);
31}
32
drhb9bb7c12006-06-11 23:41:55 +000033/* Methods for the echo module */
34static int echoCreate(
35 sqlite3 *db,
36 const sqlite3_module *pModule,
37 int argc, char **argv,
38 sqlite3_vtab **ppVtab
39){
40 int i;
41 Tcl_Interp *interp = pModule->pAux;
42 *ppVtab = pModule->pAux;
43
danielk197778efaba2006-06-12 06:09:17 +000044 appendToEchoModule(pModule, "xCreate");
drhb9bb7c12006-06-11 23:41:55 +000045 for(i=0; i<argc; i++){
danielk197778efaba2006-06-12 06:09:17 +000046 appendToEchoModule(pModule, argv[i]);
drhb9bb7c12006-06-11 23:41:55 +000047 }
danielk197778efaba2006-06-12 06:09:17 +000048
drhb9bb7c12006-06-11 23:41:55 +000049 return 0;
50}
51static int echoConnect(
52 sqlite3 *db,
53 const sqlite3_module *pModule,
54 int argc, char **argv,
55 sqlite3_vtab **ppVtab
56){
57 int i;
58 Tcl_Interp *interp = pModule->pAux;
59 *ppVtab = pModule->pAux;
60
61 Tcl_SetVar(interp, "echo_module", "xConnect", TCL_GLOBAL_ONLY);
62 for(i=0; i<argc; i++){
63 Tcl_SetVar(interp, "echo_module", argv[i],
64 TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
65 }
66 return 0;
67}
68static int echoDisconnect(sqlite3_vtab *pVtab){
69 Tcl_Interp *interp = (Tcl_Interp*)pVtab;
70 Tcl_SetVar(interp, "echo_module", "xDisconnect",
71 TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
72 return 0;
73}
74static int echoDestroy(sqlite3_vtab *pVtab){
75 Tcl_Interp *interp = (Tcl_Interp*)pVtab;
76 Tcl_SetVar(interp, "echo_module", "xDestroy",
77 TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
78 return 0;
79}
80
81/*
82** A virtual table module that merely echos method calls into TCL
83** variables.
84*/
85static sqlite3_module echoModule = {
danielk197778efaba2006-06-12 06:09:17 +000086 0, /* iVersion */
87 "echo", /* zName */
88 0, /* pAux */
drhb9bb7c12006-06-11 23:41:55 +000089 echoCreate,
90 echoConnect,
danielk197778efaba2006-06-12 06:09:17 +000091 0, /* xBestIndex */
drhb9bb7c12006-06-11 23:41:55 +000092 echoDisconnect,
93 echoDestroy,
94};
95
96/*
97** Decode a pointer to an sqlite3 object.
98*/
99static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
100 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
101 return TCL_OK;
102}
103
104
105/*
106** Register the echo virtual table module.
107*/
108static int register_echo_module(
109 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
110 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
111 int objc, /* Number of arguments */
112 Tcl_Obj *CONST objv[] /* Command arguments */
113){
114 sqlite3 *db;
115 if( objc!=2 ){
116 Tcl_WrongNumArgs(interp, 1, objv, "DB");
117 return TCL_ERROR;
118 }
119 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
120 echoModule.pAux = interp;
121#ifndef SQLITE_OMIT_VIRTUALTABLE
122 sqlite3_create_module(db, "echo", &echoModule);
123#endif
124 return TCL_OK;
125}
126
127
128/*
129** Register commands with the TCL interpreter.
130*/
131int Sqlitetest8_Init(Tcl_Interp *interp){
132 static struct {
133 char *zName;
134 Tcl_ObjCmdProc *xProc;
135 void *clientData;
136 } aObjCmd[] = {
137 { "register_echo_module", register_echo_module, 0 },
138 };
139 int i;
140 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
141 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
142 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
143 }
144 return TCL_OK;
145}