Test case for writing to a WITHOUT ROWID virtual table. The TCLVAR virtual

table is modified to add a "fullname" column which is the primary key, and
to accept update operations against the primary key.

FossilOrigin-Name: 6997e00c3221f266f4d9187501d8a9e5bafb85551e88a744cdc8ffe3b75ec2a4
This commit is contained in:
drh 2017-08-10 17:53:11 +00:00
parent e3740f272b
commit 4dd176eaab
7 changed files with 207 additions and 19 deletions

View File

@ -1,5 +1,5 @@
C Experimental\schanges\sthat\sallow\sa\sWITHOUT\sROWID\svirtual\stable\sto\sbe\swritable\nas\slong\sas\sit\shas\sonly\sa\ssingle-column\sPRIMARY\sKEY. C Test\scase\sfor\swriting\sto\sa\sWITHOUT\sROWID\svirtual\stable.\s\sThe\sTCLVAR\svirtual\ntable\sis\smodified\sto\sadd\sa\s"fullname"\scolumn\swhich\sis\sthe\sprimary\skey,\sand\nto\saccept\supdate\soperations\sagainst\sthe\sprimary\skey.
D 2017-08-10T15:19:39.810 D 2017-08-10T17:53:11.866
F Makefile.in d9873c9925917cca9990ee24be17eb9613a668012c85a343aef7e5536ae266e8 F Makefile.in d9873c9925917cca9990ee24be17eb9613a668012c85a343aef7e5536ae266e8
F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
F Makefile.msc 02b469e9dcd5b7ee63fc1fb05babc174260ee4cfa4e0ef2e48c3c6801567a016 F Makefile.msc 02b469e9dcd5b7ee63fc1fb05babc174260ee4cfa4e0ef2e48c3c6801567a016
@ -506,7 +506,7 @@ F src/test_server.c a2615049954cbb9cfb4a62e18e2f0616e4dc38fe
F src/test_sqllog.c 11e6ce7575f489155c604ac4b439f2ac1d3d5aef F src/test_sqllog.c 11e6ce7575f489155c604ac4b439f2ac1d3d5aef
F src/test_superlock.c 4839644b9201da822f181c5bc406c0b2385f672e F src/test_superlock.c 4839644b9201da822f181c5bc406c0b2385f672e
F src/test_syscall.c 1073306ba2e9bfc886771871a13d3de281ed3939 F src/test_syscall.c 1073306ba2e9bfc886771871a13d3de281ed3939
F src/test_tclvar.c df9fe1213c2634687a9ca0b0bec0d2119d359ae3 F src/test_tclvar.c 33ff42149494a39c5fbb0df3d25d6fafb2f668888e41c0688d07273dcb268dfc
F src/test_thread.c 911d15fb14e19c0c542bdc8aabf981c2f10a4858 F src/test_thread.c 911d15fb14e19c0c542bdc8aabf981c2f10a4858
F src/test_vfs.c f0186261a24de2671d080bcd8050732f0cb64f6e F src/test_vfs.c f0186261a24de2671d080bcd8050732f0cb64f6e
F src/test_vfstrace.c bab9594adc976cbe696ff3970728830b4c5ed698 F src/test_vfstrace.c bab9594adc976cbe696ff3970728830b4c5ed698
@ -1462,7 +1462,7 @@ F test/varint.test ab7b110089a08b9926ed7390e7e97bdefeb74102
F test/veryquick.test 57ab846bacf7b90cf4e9a672721ea5c5b669b661 F test/veryquick.test 57ab846bacf7b90cf4e9a672721ea5c5b669b661
F test/view.test 765802c7a66d37fabd5ac8e2f2dbe572b43eb9ab F test/view.test 765802c7a66d37fabd5ac8e2f2dbe572b43eb9ab
F test/vtab1.test 8f91b9538d1404c3932293a588c4344218a0c94792d4289bb55e41020e7b3fff F test/vtab1.test 8f91b9538d1404c3932293a588c4344218a0c94792d4289bb55e41020e7b3fff
F test/vtab2.test f8cd1bb9aba7143eba97812d9617880a36d247ad F test/vtab2.test 14d4ab26cee13ba6cf5c5601b158e4f57552d3b055cdd9406cf7f711e9c84082
F test/vtab3.test b45f47d20f225ccc9c28dc915d92740c2dee311e F test/vtab3.test b45f47d20f225ccc9c28dc915d92740c2dee311e
F test/vtab4.test 8e73ed268f3d596bc3590f45fc948fb40f28e9c3 F test/vtab4.test 8e73ed268f3d596bc3590f45fc948fb40f28e9c3
F test/vtab5.test 889f444970393c73f1e077e2bdc5d845e157a391 F test/vtab5.test 889f444970393c73f1e077e2bdc5d845e157a391
@ -1474,10 +1474,11 @@ F test/vtabA.test 1317f06a03597eee29f40a49b6c21e1aaba4285f
F test/vtabB.test 04df5dc531b9f44d9ca65b9c1b79f12b5922a796 F test/vtabB.test 04df5dc531b9f44d9ca65b9c1b79f12b5922a796
F test/vtabC.test 4528f459a13136f982e75614d120aef165f17292 F test/vtabC.test 4528f459a13136f982e75614d120aef165f17292
F test/vtabD.test 05b3f1d77117271671089e48719524b676842e96 F test/vtabD.test 05b3f1d77117271671089e48719524b676842e96
F test/vtabE.test d5024aa42754962f6bb0afd261681686488e7afe F test/vtabE.test 2a143fe75a11275781d1fd1988d86b66a3f69cb98f4add62e3da8fd0f637b45f
F test/vtabF.test 1918844c7c902f6a16c8dacf1ec8f84886d6e78b F test/vtabF.test 1918844c7c902f6a16c8dacf1ec8f84886d6e78b
F test/vtabH.test 26d54e8b5407f797638b787a55f9c88323850a58dd142de02d06b9a1159bd283 F test/vtabH.test 3cf9aa1c1c4381b3b3ac33f933376f06fbb99d2294a83c79b7562d3ed87be450
F test/vtabI.test 751b07636700dbdea328e4265b6077ccd6811a3f F test/vtabI.test 751b07636700dbdea328e4265b6077ccd6811a3f
F test/vtabJ.test 8b20f50791a31a962ca14ed47bfd24acef06496b26ec448736cf4e142f06c985
F test/vtab_alter.test 736e66fb5ec7b4fee58229aa3ada2f27ec58bc58c00edae4836890c3784c6783 F test/vtab_alter.test 736e66fb5ec7b4fee58229aa3ada2f27ec58bc58c00edae4836890c3784c6783
F test/vtab_err.test 0d4d8eb4def1d053ac7c5050df3024fd47a3fbd8 F test/vtab_err.test 0d4d8eb4def1d053ac7c5050df3024fd47a3fbd8
F test/vtab_shared.test 5253bff2355a9a3f014c15337da7e177ab0ef8ad F test/vtab_shared.test 5253bff2355a9a3f014c15337da7e177ab0ef8ad
@ -1645,10 +1646,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P e49279e65169a939b6058a0960dc1fe09ce4ee2d78992a1969773cbc7ce1043b P ab9ee4c1e64c09c7130e385a23d043d78bad95dff5509c7adc9b992350a4a537
R fc0428bfd3fb49d6ae76636976efdfdc R b1f21065b486178daa712ffc826c4b63
T *branch * writable-vtab-without-rowid
T *sym-writable-vtab-without-rowid *
T -sym-trunk *
U drh U drh
Z 4eb753989223a630f69c7bf5598d0be4 Z 99e72c7ab8dd29c2570ed425789c9b9c

View File

@ -1 +1 @@
ab9ee4c1e64c09c7130e385a23d043d78bad95dff5509c7adc9b992350a4a537 6997e00c3221f266f4d9187501d8a9e5bafb85551e88a744cdc8ffe3b75ec2a4

View File

@ -15,6 +15,25 @@
** **
** The emphasis of this file is a virtual table that provides ** The emphasis of this file is a virtual table that provides
** access to TCL variables. ** 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" #include "sqliteInt.h"
#if defined(INCLUDE_SQLITE_TCL_H) #if defined(INCLUDE_SQLITE_TCL_H)
@ -67,7 +86,12 @@ static int tclvarConnect(
){ ){
tclvar_vtab *pVtab; tclvar_vtab *pVtab;
static const char zSchema[] = static const char zSchema[] =
"CREATE TABLE whatever(name TEXT, arrayname TEXT, value TEXT)"; "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) ); pVtab = sqlite3MallocZero( sizeof(*pVtab) );
if( pVtab==0 ) return SQLITE_NOMEM; if( pVtab==0 ) return SQLITE_NOMEM;
*ppVtab = &pVtab->base; *ppVtab = &pVtab->base;
@ -251,6 +275,16 @@ static int tclvarColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
sqlite3_result_text(ctx, Tcl_GetString(pVal), -1, SQLITE_TRANSIENT); sqlite3_result_text(ctx, Tcl_GetString(pVal), -1, SQLITE_TRANSIENT);
break; 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; return SQLITE_OK;
} }
@ -376,6 +410,58 @@ static int tclvarBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
return SQLITE_OK; 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 ** A virtual table module that provides read-only access to a
** Tcl global variable namespace. ** Tcl global variable namespace.
@ -394,7 +480,7 @@ static sqlite3_module tclvarModule = {
tclvarEof, /* xEof - check for end of scan */ tclvarEof, /* xEof - check for end of scan */
tclvarColumn, /* xColumn - read data */ tclvarColumn, /* xColumn - read data */
tclvarRowid, /* xRowid - read data */ tclvarRowid, /* xRowid - read data */
0, /* xUpdate */ tclvarUpdate, /* xUpdate */
0, /* xBegin */ 0, /* xBegin */
0, /* xSync */ 0, /* xSync */
0, /* xCommit */ 0, /* xCommit */

View File

@ -60,7 +60,7 @@ do_test vtab2-2.1 {
set ::abc 123 set ::abc 123
execsql { execsql {
CREATE VIRTUAL TABLE vars USING tclvar; CREATE VIRTUAL TABLE vars USING tclvar;
SELECT * FROM vars WHERE name='abc'; SELECT name, arrayname, value FROM vars WHERE name='abc';
} }
} [list abc "" 123] } [list abc "" 123]
do_test vtab2-2.2 { do_test vtab2-2.2 {
@ -68,7 +68,7 @@ do_test vtab2-2.2 {
set A(2) 4 set A(2) 4
set A(3) 9 set A(3) 9
execsql { execsql {
SELECT * FROM vars WHERE name='A'; SELECT name, arrayname, value FROM vars WHERE name='A';
} }
} [list A 1 1 A 2 4 A 3 9] } [list A 1 1 A 2 4 A 3 9]
unset -nocomplain result unset -nocomplain result

View File

@ -39,7 +39,9 @@ do_test vtabE-1 {
CREATE VIRTUAL TABLE t1 USING tclvar; CREATE VIRTUAL TABLE t1 USING tclvar;
CREATE VIRTUAL TABLE t2 USING tclvar; CREATE VIRTUAL TABLE t2 USING tclvar;
CREATE TABLE t3(a INTEGER PRIMARY KEY, b); CREATE TABLE t3(a INTEGER PRIMARY KEY, b);
SELECT t1.*, t2.*, abs(t3.b + abs(t2.value + abs(t1.value))) SELECT t1.name, t1.arrayname, t1.value,
t2.name, t2.arrayname, t2.value,
abs(t3.b + abs(t2.value + abs(t1.value)))
FROM t1 LEFT JOIN t2 ON t2.name = t1.arrayname FROM t1 LEFT JOIN t2 ON t2.name = t1.arrayname
LEFT JOIN t3 ON t3.a=t2.value LEFT JOIN t3 ON t3.a=t2.value
WHERE t1.name = 'vtabE' WHERE t1.name = 'vtabE'

View File

@ -55,7 +55,7 @@ register_tclvar_module db
set ::xyz 10 set ::xyz 10
do_execsql_test 2.0 { do_execsql_test 2.0 {
CREATE VIRTUAL TABLE vars USING tclvar; CREATE VIRTUAL TABLE vars USING tclvar;
SELECT * FROM vars WHERE name = 'xyz'; SELECT name, arrayname, value FROM vars WHERE name = 'xyz';
} {xyz {} 10} } {xyz {} 10}
set x1 aback set x1 aback

102
test/vtabJ.test Normal file
View File

@ -0,0 +1,102 @@
# 2017-08-10
#
# 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.
#
#***********************************************************************
# This file implements tests of writing to WITHOUT ROWID virtual tables
# using the tclvar eponymous virtual table.
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix vtabJ
ifcapable !vtab {
finish_test
return
}
register_tclvar_module db
unset -nocomplain vtabJ
do_test 100 {
set vtabJ(1) this
set vtabJ(two) is
set vtabJ(3) {a test}
db eval {
SELECT fullname, value FROM tclvar WHERE name='vtabJ' ORDER BY fullname;
}
} {vtabJ(1) this vtabJ(3) {a test} vtabJ(two) is}
do_execsql_test 110 {
INSERT INTO tclvar(fullname, value)
VALUES('vtabJ(4)',4),('vtabJ(five)',555);
SELECT fullname, value FROM tclvar WHERE name='vtabJ' ORDER BY fullname;
} {vtabJ(1) this vtabJ(3) {a test} vtabJ(4) 4 vtabJ(five) 555 vtabJ(two) is}
do_test 111 {
set res {}
foreach vname [lsort [array names vtabJ]] {
lappend res vtabJ($vname) $vtabJ($vname)
}
set res
} {vtabJ(1) this vtabJ(3) {a test} vtabJ(4) 4 vtabJ(five) 555 vtabJ(two) is}
do_test 120 {
db eval {
INSERT INTO tclvar(fullname, value) VALUES('vtabJ(4)',444);
}
set vtabJ(4)
} {444}
do_test 130 {
db eval {
INSERT INTO tclvar(fullname, value) VALUES('vtabJ(4)',NULL);
}
info exists vtabJ(4)
} {0}
do_test 140 {
db eval {
UPDATE tclvar SET value=55 WHERE fullname='vtabJ(five)';
}
set vtabJ(five)
} {55}
do_test 150 {
db eval {
UPDATE tclvar SET fullname='vtabJ(5)' WHERE fullname='vtabJ(five)';
}
set vtabJ(5)
} {55}
do_test 151 {
info exists vtabJ(five)
} {0}
do_test 152 {
set res {}
foreach vname [lsort [array names vtabJ]] {
lappend res vtabJ($vname) $vtabJ($vname)
}
set res
} {vtabJ(1) this vtabJ(3) {a test} vtabJ(5) 55 vtabJ(two) is}
do_execsql_test 160 {
SELECT fullname FROM tclvar WHERE arrayname='two'
} {vtabJ(two)}
do_execsql_test 161 {
DELETE FROM tclvar WHERE arrayname='two';
SELECT fullname, value FROM tclvar WHERE name='vtabJ' ORDER BY fullname;
} {vtabJ(1) this vtabJ(3) {a test} vtabJ(5) 55}
do_test 162 {
set res {}
foreach vname [lsort [array names vtabJ]] {
lappend res vtabJ($vname) $vtabJ($vname)
}
set res
} {vtabJ(1) this vtabJ(3) {a test} vtabJ(5) 55}
finish_test