1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
|
/*
** 2006 June 13
**
** 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.
**
** The emphasis of this file is a virtual table that provides
** access to TCL variables.
**
** The TCLVAR eponymous virtual table has a schema like this:
**
** CREATE TABLE tclvar(
** name TEXT, -- base name of the variable: "x" in "$x(y)"
** arrayname TEXT, -- array index name: "y" in "$x(y)"
** value TEXT, -- the value of the variable
** fullname TEXT, -- the full name of the variable
** PRIMARY KEY(fullname)
** ) WITHOUT ROWID;
**
** DELETE, INSERT, and UPDATE operations use the "fullname" field to
** determine the variable to be modified. Changing "value" to NULL
** deletes the variable.
**
** For SELECT operations, the "name" and "arrayname" fields will always
** match the "fullname" field. For DELETE, INSERT, and UPDATE, the
** "name" and "arrayname" fields are ignored and the variable is modified
** according to "fullname" and "value" only.
*/
#include "sqliteInt.h"
#if defined(INCLUDE_SQLITE_TCL_H)
# include "sqlite_tcl.h"
#else
# include "tcl.h"
#endif
#include <stdlib.h>
#include <string.h>
#ifndef SQLITE_OMIT_VIRTUALTABLE
/*
** Characters that make up the idxStr created by xBestIndex for xFilter.
*/
#define TCLVAR_NAME_EQ 'e'
#define TCLVAR_NAME_MATCH 'm'
#define TCLVAR_VALUE_GLOB 'g'
#define TCLVAR_VALUE_REGEXP 'r'
#define TCLVAR_VALUE_LIKE 'l'
typedef struct tclvar_vtab tclvar_vtab;
typedef struct tclvar_cursor tclvar_cursor;
/*
** A tclvar virtual-table object
*/
struct tclvar_vtab {
sqlite3_vtab base;
Tcl_Interp *interp;
};
/* A tclvar cursor object */
struct tclvar_cursor {
sqlite3_vtab_cursor base;
Tcl_Obj *pList1; /* Result of [info vars ?pattern?] */
Tcl_Obj *pList2; /* Result of [array names [lindex $pList1 $i1]] */
int i1; /* Current item in pList1 */
int i2; /* Current item (if any) in pList2 */
};
/* Methods for the tclvar module */
static int tclvarConnect(
sqlite3 *db,
void *pAux,
int argc, const char *const*argv,
sqlite3_vtab **ppVtab,
char **pzErr
){
tclvar_vtab *pVtab;
static const char zSchema[] =
"CREATE TABLE x("
" name TEXT," /* Base name */
" arrayname TEXT," /* Array index */
" value TEXT," /* Value */
" fullname TEXT PRIMARY KEY" /* base(index) name */
") WITHOUT ROWID";
pVtab = sqlite3MallocZero( sizeof(*pVtab) );
if( pVtab==0 ) return SQLITE_NOMEM;
*ppVtab = &pVtab->base;
pVtab->interp = (Tcl_Interp *)pAux;
sqlite3_declare_vtab(db, zSchema);
return SQLITE_OK;
}
/* Note that for this virtual table, the xCreate and xConnect
** methods are identical. */
static int tclvarDisconnect(sqlite3_vtab *pVtab){
sqlite3_free(pVtab);
return SQLITE_OK;
}
/* The xDisconnect and xDestroy methods are also the same */
/*
** Open a new tclvar cursor.
*/
static int tclvarOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
tclvar_cursor *pCur;
pCur = sqlite3MallocZero(sizeof(tclvar_cursor));
*ppCursor = &pCur->base;
return SQLITE_OK;
}
/*
** Close a tclvar cursor.
*/
static int tclvarClose(sqlite3_vtab_cursor *cur){
tclvar_cursor *pCur = (tclvar_cursor *)cur;
if( pCur->pList1 ){
Tcl_DecrRefCount(pCur->pList1);
}
if( pCur->pList2 ){
Tcl_DecrRefCount(pCur->pList2);
}
sqlite3_free(pCur);
return SQLITE_OK;
}
/*
** Returns 1 if data is ready, or 0 if not.
*/
static int next2(Tcl_Interp *interp, tclvar_cursor *pCur, Tcl_Obj *pObj){
Tcl_Obj *p;
if( pObj ){
if( !pCur->pList2 ){
p = Tcl_NewStringObj("array names", -1);
Tcl_IncrRefCount(p);
Tcl_ListObjAppendElement(0, p, pObj);
Tcl_EvalObjEx(interp, p, TCL_EVAL_GLOBAL);
Tcl_DecrRefCount(p);
pCur->pList2 = Tcl_GetObjResult(interp);
Tcl_IncrRefCount(pCur->pList2);
assert( pCur->i2==0 );
}else{
int n = 0;
pCur->i2++;
Tcl_ListObjLength(0, pCur->pList2, &n);
if( pCur->i2>=n ){
Tcl_DecrRefCount(pCur->pList2);
pCur->pList2 = 0;
pCur->i2 = 0;
return 0;
}
}
}
return 1;
}
static int tclvarNext(sqlite3_vtab_cursor *cur){
Tcl_Obj *pObj;
int n = 0;
int ok = 0;
tclvar_cursor *pCur = (tclvar_cursor *)cur;
Tcl_Interp *interp = ((tclvar_vtab *)(cur->pVtab))->interp;
Tcl_ListObjLength(0, pCur->pList1, &n);
while( !ok && pCur->i1<n ){
Tcl_ListObjIndex(0, pCur->pList1, pCur->i1, &pObj);
ok = next2(interp, pCur, pObj);
if( !ok ){
pCur->i1++;
}
}
return 0;
}
static int tclvarFilter(
sqlite3_vtab_cursor *pVtabCursor,
int idxNum, const char *idxStr,
int argc, sqlite3_value **argv
){
tclvar_cursor *pCur = (tclvar_cursor *)pVtabCursor;
Tcl_Interp *interp = ((tclvar_vtab *)(pVtabCursor->pVtab))->interp;
Tcl_Obj *p = Tcl_NewStringObj("tclvar_filter_cmd", -1);
const char *zEq = "";
const char *zMatch = "";
const char *zGlob = "";
const char *zRegexp = "";
const char *zLike = "";
int i;
for(i=0; idxStr[i]; i++){
switch( idxStr[i] ){
case TCLVAR_NAME_EQ:
zEq = (const char*)sqlite3_value_text(argv[i]);
break;
case TCLVAR_NAME_MATCH:
zMatch = (const char*)sqlite3_value_text(argv[i]);
break;
case TCLVAR_VALUE_GLOB:
zGlob = (const char*)sqlite3_value_text(argv[i]);
break;
case TCLVAR_VALUE_REGEXP:
zRegexp = (const char*)sqlite3_value_text(argv[i]);
break;
case TCLVAR_VALUE_LIKE:
zLike = (const char*)sqlite3_value_text(argv[i]);
break;
default:
assert( 0 );
}
}
Tcl_IncrRefCount(p);
Tcl_ListObjAppendElement(0, p, Tcl_NewStringObj(zEq, -1));
Tcl_ListObjAppendElement(0, p, Tcl_NewStringObj(zMatch, -1));
Tcl_ListObjAppendElement(0, p, Tcl_NewStringObj(zGlob, -1));
Tcl_ListObjAppendElement(0, p, Tcl_NewStringObj(zRegexp, -1));
Tcl_ListObjAppendElement(0, p, Tcl_NewStringObj(zLike, -1));
Tcl_EvalObjEx(interp, p, TCL_EVAL_GLOBAL);
if( pCur->pList1 ){
Tcl_DecrRefCount(pCur->pList1);
}
if( pCur->pList2 ){
Tcl_DecrRefCount(pCur->pList2);
pCur->pList2 = 0;
}
pCur->i1 = 0;
pCur->i2 = 0;
pCur->pList1 = Tcl_GetObjResult(interp);
Tcl_IncrRefCount(pCur->pList1);
Tcl_DecrRefCount(p);
return tclvarNext(pVtabCursor);
}
static int tclvarColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
Tcl_Obj *p1;
Tcl_Obj *p2;
const char *z1;
const char *z2 = "";
tclvar_cursor *pCur = (tclvar_cursor*)cur;
Tcl_Interp *interp = ((tclvar_vtab *)cur->pVtab)->interp;
Tcl_ListObjIndex(interp, pCur->pList1, pCur->i1, &p1);
Tcl_ListObjIndex(interp, pCur->pList2, pCur->i2, &p2);
z1 = Tcl_GetString(p1);
if( p2 ){
z2 = Tcl_GetString(p2);
}
switch (i) {
case 0: {
sqlite3_result_text(ctx, z1, -1, SQLITE_TRANSIENT);
break;
}
case 1: {
sqlite3_result_text(ctx, z2, -1, SQLITE_TRANSIENT);
break;
}
case 2: {
Tcl_Obj *pVal = Tcl_GetVar2Ex(interp, z1, *z2?z2:0, TCL_GLOBAL_ONLY);
sqlite3_result_text(ctx, Tcl_GetString(pVal), -1, SQLITE_TRANSIENT);
break;
}
case 3: {
char *z3;
if( p2 ){
z3 = sqlite3_mprintf("%s(%s)", z1, z2);
sqlite3_result_text(ctx, z3, -1, sqlite3_free);
}else{
sqlite3_result_text(ctx, z1, -1, SQLITE_TRANSIENT);
}
break;
}
}
return SQLITE_OK;
}
static int tclvarRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
*pRowid = 0;
return SQLITE_OK;
}
static int tclvarEof(sqlite3_vtab_cursor *cur){
tclvar_cursor *pCur = (tclvar_cursor*)cur;
return (pCur->pList2?0:1);
}
/*
** If nul-terminated string zStr does not already contain the character
** passed as the second argument, append it and return 0. Or, if there is
** already an instance of x in zStr, do nothing return 1;
**
** There is guaranteed to be enough room in the buffer pointed to by zStr
** for the new character and nul-terminator.
*/
static int tclvarAddToIdxstr(char *zStr, char x){
int i;
for(i=0; zStr[i]; i++){
if( zStr[i]==x ) return 1;
}
zStr[i] = x;
zStr[i+1] = '\0';
return 0;
}
/*
** Return true if variable $::tclvar_set_omit exists and is set to true.
** False otherwise.
*/
static int tclvarSetOmit(Tcl_Interp *interp){
int rc;
int res = 0;
Tcl_Obj *pRes;
rc = Tcl_Eval(interp,
"expr {[info exists ::tclvar_set_omit] && $::tclvar_set_omit}"
);
if( rc==TCL_OK ){
pRes = Tcl_GetObjResult(interp);
rc = Tcl_GetBooleanFromObj(0, pRes, &res);
}
return (rc==TCL_OK && res);
}
/*
** The xBestIndex() method. This virtual table supports the following
** operators:
**
** name = ? (omit flag clear)
** name MATCH ? (omit flag set)
** value GLOB ? (omit flag set iff $::tclvar_set_omit)
** value REGEXP ? (omit flag set iff $::tclvar_set_omit)
** value LIKE ? (omit flag set iff $::tclvar_set_omit)
**
** For each constraint present, the corresponding TCLVAR_XXX character is
** appended to the idxStr value.
*/
static int tclvarBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
tclvar_vtab *pTab = (tclvar_vtab*)tab;
int ii;
char *zStr = sqlite3_malloc(32);
int iStr = 0;
if( zStr==0 ) return SQLITE_NOMEM;
zStr[0] = '\0';
for(ii=0; ii<pIdxInfo->nConstraint; ii++){
struct sqlite3_index_constraint const *pCons = &pIdxInfo->aConstraint[ii];
struct sqlite3_index_constraint_usage *pUsage;
pUsage = &pIdxInfo->aConstraintUsage[ii];
if( pCons->usable ){
/* name = ? */
if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ && pCons->iColumn==0 ){
if( 0==tclvarAddToIdxstr(zStr, TCLVAR_NAME_EQ) ){
pUsage->argvIndex = ++iStr;
pUsage->omit = 0;
}
}
/* name MATCH ? */
if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH && pCons->iColumn==0 ){
if( 0==tclvarAddToIdxstr(zStr, TCLVAR_NAME_MATCH) ){
pUsage->argvIndex = ++iStr;
pUsage->omit = 1;
}
}
/* value GLOB ? */
if( pCons->op==SQLITE_INDEX_CONSTRAINT_GLOB && pCons->iColumn==2 ){
if( 0==tclvarAddToIdxstr(zStr, TCLVAR_VALUE_GLOB) ){
pUsage->argvIndex = ++iStr;
pUsage->omit = tclvarSetOmit(pTab->interp);
}
}
/* value REGEXP ? */
if( pCons->op==SQLITE_INDEX_CONSTRAINT_REGEXP && pCons->iColumn==2 ){
if( 0==tclvarAddToIdxstr(zStr, TCLVAR_VALUE_REGEXP) ){
pUsage->argvIndex = ++iStr;
pUsage->omit = tclvarSetOmit(pTab->interp);
}
}
/* value LIKE ? */
if( pCons->op==SQLITE_INDEX_CONSTRAINT_LIKE && pCons->iColumn==2 ){
if( 0==tclvarAddToIdxstr(zStr, TCLVAR_VALUE_LIKE) ){
pUsage->argvIndex = ++iStr;
pUsage->omit = tclvarSetOmit(pTab->interp);
}
}
}
}
pIdxInfo->idxStr = zStr;
pIdxInfo->needToFreeIdxStr = 1;
return SQLITE_OK;
}
/*
** Invoked for any UPDATE, INSERT, or DELETE against a tclvar table
*/
static int tclvarUpdate(
sqlite3_vtab *tab,
int argc,
sqlite3_value **argv,
sqlite_int64 *pRowid
){
tclvar_vtab *pTab = (tclvar_vtab*)tab;
if( argc==1 ){
/* A DELETE operation. The variable to be deleted is stored in argv[0] */
const char *zVar = (const char*)sqlite3_value_text(argv[0]);
Tcl_UnsetVar(pTab->interp, zVar, TCL_GLOBAL_ONLY);
return SQLITE_OK;
}
if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
/* An INSERT operation */
const char *zValue = (const char*)sqlite3_value_text(argv[4]);
const char *zName;
if( sqlite3_value_type(argv[5])!=SQLITE_TEXT ){
tab->zErrMsg = sqlite3_mprintf("the 'fullname' column must be TEXT");
return SQLITE_ERROR;
}
zName = (const char*)sqlite3_value_text(argv[5]);
if( zValue ){
Tcl_SetVar(pTab->interp, zName, zValue, TCL_GLOBAL_ONLY);
}else{
Tcl_UnsetVar(pTab->interp, zName, TCL_GLOBAL_ONLY);
}
return SQLITE_OK;
}
if( sqlite3_value_type(argv[0])==SQLITE_TEXT
&& sqlite3_value_type(argv[1])==SQLITE_TEXT
){
/* An UPDATE operation */
const char *zOldName = (const char*)sqlite3_value_text(argv[0]);
const char *zNewName = (const char*)sqlite3_value_text(argv[1]);
const char *zValue = (const char*)sqlite3_value_text(argv[4]);
if( strcmp(zOldName, zNewName)!=0 || zValue==0 ){
Tcl_UnsetVar(pTab->interp, zOldName, TCL_GLOBAL_ONLY);
}
if( zValue!=0 ){
Tcl_SetVar(pTab->interp, zNewName, zValue, TCL_GLOBAL_ONLY);
}
return SQLITE_OK;
}
tab->zErrMsg = sqlite3_mprintf("prohibited TCL variable change");
return SQLITE_ERROR;
}
/*
** A virtual table module that provides read-only access to a
** Tcl global variable namespace.
*/
static sqlite3_module tclvarModule = {
0, /* iVersion */
tclvarConnect,
tclvarConnect,
tclvarBestIndex,
tclvarDisconnect,
tclvarDisconnect,
tclvarOpen, /* xOpen - open a cursor */
tclvarClose, /* xClose - close a cursor */
tclvarFilter, /* xFilter - configure scan constraints */
tclvarNext, /* xNext - advance a cursor */
tclvarEof, /* xEof - check for end of scan */
tclvarColumn, /* xColumn - read data */
tclvarRowid, /* xRowid - read data */
tclvarUpdate, /* xUpdate */
0, /* xBegin */
0, /* xSync */
0, /* xCommit */
0, /* xRollback */
0, /* xFindMethod */
0, /* xRename */
0, /* xSavepoint */
0, /* xRelease */
0, /* xRollbackTo */
0, /* xShadowName */
0 /* xIntegrity */
};
/*
** Decode a pointer to an sqlite3 object.
*/
extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
/*
** Register the echo virtual table module.
*/
static int SQLITE_TCLAPI register_tclvar_module(
ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
int objc, /* Number of arguments */
Tcl_Obj *CONST objv[] /* Command arguments */
){
int rc = TCL_OK;
sqlite3 *db;
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB");
return TCL_ERROR;
}
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
#ifndef SQLITE_OMIT_VIRTUALTABLE
sqlite3_create_module(db, "tclvar", &tclvarModule, (void*)interp);
rc = Tcl_Eval(interp,
"proc like {pattern str} {\n"
" set p [string map {% * _ ?} $pattern]\n"
" string match $p $str\n"
"}\n"
"proc tclvar_filter_cmd {eq match glob regexp like} {\n"
" set res {}\n"
" set pattern $eq\n"
" if {$pattern=={}} { set pattern $match }\n"
" if {$pattern=={}} { set pattern * }\n"
" foreach v [uplevel #0 info vars $pattern] {\n"
" if {($glob=={} || [string match $glob [uplevel #0 set $v]])\n"
" && ($like=={} || [like $like [uplevel #0 set $v]])\n"
" && ($regexp=={} || [regexp $regexp [uplevel #0 set $v]])\n"
" } {\n"
" lappend res $v\n"
" }\n"
" }\n"
" set res\n"
"}\n"
);
#endif
return rc;
}
#endif
/*
** Register commands with the TCL interpreter.
*/
int Sqlitetesttclvar_Init(Tcl_Interp *interp){
#ifndef SQLITE_OMIT_VIRTUALTABLE
static struct {
char *zName;
Tcl_ObjCmdProc *xProc;
void *clientData;
} aObjCmd[] = {
{ "register_tclvar_module", register_tclvar_module, 0 },
};
int i;
for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
}
#endif
return TCL_OK;
}
|