Fix up the test tclvar virtual module. (CVS 3307)

FossilOrigin-Name: a20bfa46316b9d8f884f147960620fc8e56a7c7f
This commit is contained in:
danielk1977 2006-06-27 12:24:59 +00:00
parent baaa7f409d
commit 169f8a0c7a
4 changed files with 198 additions and 35 deletions

View File

@ -1,5 +1,5 @@
C Additional\sdocumentation\son\sthe\s3.3.0\sfile\sformat\schange\sadded\sto\nformatchng.html.\s(CVS\s3306)
D 2006-06-27T12:24:14
C Fix\sup\sthe\stest\stclvar\svirtual\smodule.\s(CVS\s3307)
D 2006-06-27T12:25:00
F Makefile.in f839b470345d3cb4b0644068474623fe2464b5d3
F Makefile.linux-gcc 2d8574d1ba75f129aba2019f0b959db380a90935
F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@ -90,7 +90,7 @@ F src/test_loadext.c 22065d601a18878e5542191001f0eaa5d77c0ed8
F src/test_md5.c 6c42bc0a3c0b54be34623ff77a0eec32b2fa96e3
F src/test_schema.c 02f182f156e8e431a9508184df5638b370850495
F src/test_server.c a6460daed0b92ecbc2531b6dc73717470e7a648c
F src/test_tclvar.c 5242e36b29f18f178f0770ff6fd1dafbc5ddabe0
F src/test_tclvar.c 0fe60a8a7358f99d5d0c08d6822bd5b1b39b4d58
F src/tokenize.c 7b448440dfd6e984d6bef7ac7fc60f1d26eaf8e7
F src/trigger.c 0fc40125820409a6274834a6e04ad804d96e2793
F src/update.c e3f5b7e9e1008e809248ea98bc76844f9a610f7a
@ -293,7 +293,7 @@ F test/vacuum2.test 5aea8c88a65cb29f7d175296e7c819c6158d838c
F test/varint.test ab7b110089a08b9926ed7390e7e97bdefeb74102
F test/view.test 16e2774fe35e47a07ac4471b7f0bcc948b1aa6d5
F test/vtab1.test d78c94f5ebe6947f1509280a533ac1ffe7a84dba
F test/vtab2.test e57f9865368df26ef5eb8bc630962d82086f174b
F test/vtab2.test bd6d6f3dc0403f42a63a63e3319e0a689d27dd4f
F test/vtab3.test f38d6d7d19f08bffdadce4d5b8cba078f8118587
F test/vtab4.test 4b4293341443839ef6dc02f8d9e614702a6c67ff
F test/vtab5.test 9fb8f335651afe8f870011e2f68e5b00c5ad03cd
@ -374,7 +374,7 @@ F www/tclsqlite.tcl bb0d1357328a42b1993d78573e587c6dcbc964b9
F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b
F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513
P cf41f2a33f94c6c94fb39ae6ffcd58bbf9ea88c8
R d795e51bfb50cf8f5991b3a004ce6875
U drh
Z 9d94388a6945b56f01bf4b619ff75d29
P 955551ca2e66a1f21dae21aa0265906887d7138d
R a75be91ad4b96784be02c1fe703749d2
U danielk1977
Z fcc7fcb84d664455e9b0b827f3f6b723

View File

@ -1 +1 @@
955551ca2e66a1f21dae21aa0265906887d7138d
a20bfa46316b9d8f884f147960620fc8e56a7c7f

View File

@ -16,7 +16,7 @@
** The emphasis of this file is a virtual table that provides
** access to TCL variables.
**
** $Id: test_tclvar.c,v 1.5 2006/06/26 19:10:32 drh Exp $
** $Id: test_tclvar.c,v 1.6 2006/06/27 12:25:00 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "tcl.h"
@ -24,6 +24,8 @@
#include <stdlib.h>
#include <string.h>
#ifndef SQLITE_OMIT_VIRTUALTABLE
typedef struct tclvar_vtab tclvar_vtab;
typedef struct tclvar_cursor tclvar_cursor;
@ -38,8 +40,11 @@ struct tclvar_vtab {
/* A tclvar cursor object */
struct tclvar_cursor {
sqlite3_vtab_cursor base;
Tcl_Obj *pList1, *pList2;
int i, j;
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 */
@ -56,19 +61,21 @@ static int tclvarConnect(
if( pVtab==0 ) return SQLITE_NOMEM;
*ppVtab = &pVtab->base;
pVtab->interp = (Tcl_Interp *)pAux;
#ifndef SQLITE_OMIT_VIRTUALTABLE
sqlite3_declare_vtab(db, zSchema);
#endif
return SQLITE_OK;
}
/* Note that for this virtual table, the xCreate and xConnect
** methods are identical. */
static int tclvarDisconnect(sqlite3_vtab *pVtab){
free(pVtab);
sqliteFree(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 = sqliteMalloc(sizeof(tclvar_cursor));
@ -76,6 +83,9 @@ static int tclvarOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
return SQLITE_OK;
}
/*
** Close a tclvar cursor.
*/
static int tclvarClose(sqlite3_vtab_cursor *cur){
tclvar_cursor *pCur = (tclvar_cursor *)cur;
if( pCur->pList1 ){
@ -88,19 +98,56 @@ static int tclvarClose(sqlite3_vtab_cursor *cur){
return SQLITE_OK;
}
static int tclvarNext(sqlite3_vtab_cursor *cur){
tclvar_cursor *pCur = (tclvar_cursor *)cur;
/*
** 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;
}
static int tclvarColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
tclvar_cursor *pCur = (tclvar_cursor*)cur;
return SQLITE_OK;
}
}
static int tclvarRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
return 1;
}
static int tclvarNext(sqlite3_vtab_cursor *cur){
Tcl_Obj *pObj;
int n = 0;
int ok = 0;
tclvar_cursor *pCur = (tclvar_cursor *)cur;
return SQLITE_OK;
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(
@ -109,20 +156,98 @@ static int tclvarFilter(
int argc, sqlite3_value **argv
){
tclvar_cursor *pCur = (tclvar_cursor *)pVtabCursor;
tclvar_vtab *pVtab = (tclvar_vtab *)pCur->base.pVtab;
return 0;
Tcl_Interp *interp = ((tclvar_vtab *)(pVtabCursor->pVtab))->interp;
Tcl_Obj *p = Tcl_NewStringObj("info vars", -1);
Tcl_IncrRefCount(p);
assert( argc==0 || argc==1 );
if( argc==1 ){
Tcl_Obj *pArg = Tcl_NewStringObj(sqlite3_value_text(argv[0]), -1);
Tcl_ListObjAppendElement(0, p, pArg);
}
Tcl_EvalObjEx(interp, p, TCL_EVAL_GLOBAL);
pCur->pList1 = Tcl_GetObjResult(interp);
Tcl_IncrRefCount(pCur->pList1);
assert( pCur->i1==0 && pCur->i2==0 && pCur->pList2==0 );
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;
}
}
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);
}
/*
*/
static int tclvarBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
tclvar_vtab *pVtab = (tclvar_vtab *)tab;
int ii;
for(ii=0; ii<pIdxInfo->nConstraint; ii++){
struct sqlite3_index_constraint const *pCons = &pIdxInfo->aConstraint[ii];
if( pCons->iColumn==0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
struct sqlite3_index_constraint_usage *pUsage;
pUsage = &pIdxInfo->aConstraintUsage[ii];
pUsage->omit = 0;
pUsage->argvIndex = 1;
return SQLITE_OK;
}
}
for(ii=0; ii<pIdxInfo->nConstraint; ii++){
struct sqlite3_index_constraint const *pCons = &pIdxInfo->aConstraint[ii];
if( pCons->iColumn==0 && pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
struct sqlite3_index_constraint_usage *pUsage;
pUsage = &pIdxInfo->aConstraintUsage[ii];
pUsage->omit = 1;
pUsage->argvIndex = 1;
return SQLITE_OK;
}
}
return SQLITE_OK;
}
/*
** A virtual table module that merely echos method calls into TCL
** variables.
** A virtual table module that provides read-only access to a
** Tcl global variable namespace.
*/
static sqlite3_module tclvarModule = {
0, /* iVersion */
@ -135,9 +260,14 @@ static sqlite3_module tclvarModule = {
tclvarClose, /* xClose - close a cursor */
tclvarFilter, /* xFilter - configure scan constraints */
tclvarNext, /* xNext - advance a cursor */
0, /* xEof - check for end of scan */
tclvarEof, /* xEof - check for end of scan */
tclvarColumn, /* xColumn - read data */
tclvarRowid /* xRowid - read data */
tclvarRowid, /* xRowid - read data */
0, /* xUpdate */
0, /* xBegin */
0, /* xSync */
0, /* xCommit */
0 /* xRollback */
};
/*
@ -170,6 +300,8 @@ static int register_tclvar_module(
return TCL_OK;
}
#endif
/*
** Register commands with the TCL interpreter.
@ -180,7 +312,9 @@ int Sqlitetesttclvar_Init(Tcl_Interp *interp){
Tcl_ObjCmdProc *xProc;
void *clientData;
} aObjCmd[] = {
#ifndef SQLITE_OMIT_VIRTUALTABLE
{ "register_tclvar_module", register_tclvar_module, 0 },
#endif
};
int i;
for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){

View File

@ -10,7 +10,7 @@
#***********************************************************************
# This file implements regression tests for SQLite library.
#
# $Id: vtab2.test,v 1.3 2006/06/20 11:01:09 danielk1977 Exp $
# $Id: vtab2.test,v 1.4 2006/06/27 12:25:00 danielk1977 Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
@ -37,5 +37,34 @@ do_test vtab2-1.1 {
main schema 7 pk {} 0 {} 0 \
]
register_tclvar_module [sqlite3_connection_pointer db]
do_test vtab2-2.1 {
set ::abc 123
execsql {
CREATE VIRTUAL TABLE vars USING tclvar;
SELECT * FROM vars WHERE name='abc';
}
} [list abc "" 123]
do_test vtab2-2.2 {
set A(1) 1
set A(2) 4
set A(3) 9
execsql {
SELECT * FROM vars WHERE name='A';
}
} [list A 1 1 A 2 4 A 3 9]
do_test vtab2-2.3 {
execsql {
SELECT name, value FROM vars
WHERE name MATCH 'tcl_*' AND arrayname = ''
ORDER BY name;
}
} [list \
tcl_patchLevel $tcl_patchLevel \
tcl_pkgPath $tcl_pkgPath \
tcl_precision $tcl_precision \
tcl_version $tcl_version \
]
finish_test