From: drh Date: Mon, 26 Jun 2017 16:13:20 +0000 (+0000) Subject: Add the "-unsetnull 1" option to the "sqlite3" command in the TCL interface. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fheads%2Funsetnull-option;p=thirdparty%2Fsqlite.git Add the "-unsetnull 1" option to the "sqlite3" command in the TCL interface. FossilOrigin-Name: cbe441b231397a9b775df71373ed5df9b1934a4bc428f9e09274446f9c86b859 --- diff --git a/manifest b/manifest index 915d31b911..1b4d83d42b 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Disable\sshell\stests\sfor\sthe\s.schema\scommand\sif\svirtual\stables\sare\snot\savailable. -D 2017-06-24T19:21:48.519 +C Add\sthe\s"-unsetnull\s1"\soption\sto\sthe\s"sqlite3"\scommand\sin\sthe\sTCL\sinterface. +D 2017-06-26T16:13:20.034 F Makefile.in 1cc758ce3374a32425e4d130c2fe7b026b20de5b8843243de75f087c0a2661fb F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 8eeb80162074004e906b53d7340a12a14c471a83743aab975947e95ce061efcc @@ -414,7 +414,7 @@ F src/sqliteInt.h 34a54fb47de2da1465f3d3ba1cd373db880bd5d588b0fe862a073ecacd6dda F src/sqliteLimit.h 1513bfb7b20378aa0041e7022d04acb73525de35b80b252f1b83fedb4de6a76b F src/status.c a9e66593dfb28a9e746cba7153f84d49c1ddc4b1 F src/table.c b46ad567748f24a326d9de40e5b9659f96ffff34 -F src/tclsqlite.c 1ac29f18b1b3787a30b45dbbdf6fdc4aa4f1a2f8c7c8fe586beba1b177eba97d +F src/tclsqlite.c bdf3b4386f15cfcf9ec91e9345e10a3a1a26cb9b1f17109dd0c88ac69f66828d F src/test1.c c99f0442918a7a5d5b68a95d6024c211989e6c782c15ced5a558994baaf76a5e F src/test2.c 3efb99ab7f1fc8d154933e02ae1378bac9637da5 F src/test3.c b8434949dfb8aff8dfa082c8b592109e77844c2135ed3c492113839b6956255b @@ -1187,7 +1187,7 @@ F test/tabfunc01.test 699251cb99651415218a891384510a685c7ab012 F test/table.test b708f3e5fa2542fa51dfab21fc07b36ea445cb2f F test/tableapi.test 2674633fa95d80da917571ebdd759a14d9819126 F test/tableopts.test dba698ba97251017b7c80d738c198d39ab747930 -F test/tclsqlite.test 1d73b9203b1ca8798d7d7310742b8d3febc0d56e +F test/tclsqlite.test 464d820179bb34da9126a15c61b3a26d32fb3bfd01efa77f66fbdf4d5c02e34c F test/tempdb.test bd92eba8f20e16a9136e434e20b280794de3cdb6 F test/tempdb2.test 27e41ed540b2f9b056c2e77e9bddc1b875358507 F test/tempfault.test 0c0d349c9a99bf5f374655742577f8712c647900 @@ -1583,7 +1583,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P c2ea62937ec8fabec72d3c7cd38d8e2cabbb5ce48638f8ce7ebefd6cd2716fd3 -R 9f5da0e29f079e233ed155fcb887700c +P c8186874b3fec737445ad5c4ba3eaecd922af664b387d89dc31eea60476a0294 +R b62dc4f8740bb059f00fcbad381d5bd0 U drh -Z e393ac92ea73930e7d0783c651bf561b +Z 6d4208a1d8ce288a47a0e77bfe0eac01 diff --git a/manifest.uuid b/manifest.uuid index 0bae5da8dd..9ee3ff36d2 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -c8186874b3fec737445ad5c4ba3eaecd922af664b387d89dc31eea60476a0294 \ No newline at end of file +cbe441b231397a9b775df71373ed5df9b1934a4bc428f9e09274446f9c86b859 \ No newline at end of file diff --git a/src/tclsqlite.c b/src/tclsqlite.c index bb7c9e5b82..31ad81a18b 100644 --- a/src/tclsqlite.c +++ b/src/tclsqlite.c @@ -164,11 +164,18 @@ struct SqliteDb { int nVMStep; /* Another statistic for most recent operation */ int nTransaction; /* Number of nested [transaction] methods */ int openFlags; /* Flags used to open. (SQLITE_OPEN_URI) */ + unsigned int modeFlags; /* Operating mode flags */ #ifdef SQLITE_TEST int bLegacyPrepare; /* True to use sqlite3_prepare() */ #endif }; +/* +** Valid values for SqliteDb.modeFlags +*/ +#define SQLITE_TCLMODE_UNSETNULL 0x0001 /* Unset NULL array values in */ + /* db eval ARRAY SQL */ + struct IncrblobChannel { sqlite3_blob *pBlob; /* sqlite3 blob handle */ SqliteDb *pDb; /* Associated database connection */ @@ -1730,11 +1737,15 @@ static int SQLITE_TCLAPI DbEvalNextCmd( Tcl_Obj **apColName; dbEvalRowInfo(p, &nCol, &apColName); for(i=0; ipDb->modeFlags & SQLITE_TCLMODE_UNSETNULL)!=0 + && sqlite3_column_type(p->pPreStmt->pStmt, i)==SQLITE_NULL + ){ + Tcl_UnsetVar2(interp, Tcl_GetString(pArray), + Tcl_GetString(apColName[i]), 0); }else{ - Tcl_ObjSetVar2(interp, pArray, apColName[i], pVal, 0); + Tcl_ObjSetVar2(interp, pArray, apColName[i], dbEvalColumnValue(p,i), 0); } } @@ -3309,6 +3320,7 @@ static int SQLITE_TCLAPI DbMain( const char *zFile; const char *zVfs = 0; int flags; + unsigned int modeFlags = 0; Tcl_DString translatedFilename; #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) void *pKey = 0; @@ -3399,6 +3411,14 @@ static int SQLITE_TCLAPI DbMain( }else{ flags &= ~SQLITE_OPEN_URI; } + }else if( strcmp(zArg, "-unsetnull")==0 ){ + int b; + if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR; + if( b ){ + modeFlags |= SQLITE_TCLMODE_UNSETNULL; + }else{ + modeFlags &= ~SQLITE_TCLMODE_UNSETNULL; + } }else{ Tcl_AppendResult(interp, "unknown option: ", zArg, (char*)0); return TCL_ERROR; @@ -3408,6 +3428,7 @@ static int SQLITE_TCLAPI DbMain( Tcl_WrongNumArgs(interp, 1, objv, "HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?" " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?" + " ?-unsetnull BOOLEAN?" #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) " ?-key CODECKEY?" #endif @@ -3443,6 +3464,7 @@ static int SQLITE_TCLAPI DbMain( } p->maxStmt = NUM_PREPARED_STMTS; p->openFlags = flags & SQLITE_OPEN_URI; + p->modeFlags = modeFlags; p->interp = interp; zArg = Tcl_GetStringFromObj(objv[1], 0); if( DbUseNre() ){ diff --git a/test/tclsqlite.test b/test/tclsqlite.test index bdb0fc56d1..bce6b25e77 100644 --- a/test/tclsqlite.test +++ b/test/tclsqlite.test @@ -22,7 +22,7 @@ source $testdir/tester.tcl # Check the error messages generated by tclsqlite # -set r "sqlite_orig HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN? ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?" +set r "sqlite_orig HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN? ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN? ?-unsetnull BOOLEAN?" if {[sqlite3 -has-codec]} { append r " ?-key CODECKEY?" } @@ -664,10 +664,45 @@ do_test tcl-15.5 { db exists {SELECT a FROM t1 WHERE a>3} } {0} - - - - - +# 2017-06-26: Different behavior with "-unsetnull 1" versus "-unsetnull 0" +# +# In the "db eval SQL ARRAY" form, NULL results cause the corresponding +# array entry to be unset with -unsetnull 1. With -unsetnull 0, the NULL +# results cause the array entry to be filled with the [db nullvalue] string. +# The -unsetnull 0 version is the default, for legacy compatibility. +# +catch {db close} +forcedelete test.db +sqlite3 db test.db +do_execsql_test tcl-16.100 { + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1,2),(2,NULL),(3,'xyz'); +} +do_test tcl-16.101 { + set res {} + unset -nocomplain x + db eval {SELECT * FROM t1} x { + lappend res $x(a) [array names x] + } + set res +} {1 {a b *} 2 {a b *} 3 {a b *}} +sqlite3 db test.db -unsetnull 0 +do_test tcl-16.102 { + set res {} + unset -nocomplain x + db eval {SELECT * FROM t1} x { + lappend res $x(a) [array names x] + } + set res +} {1 {a b *} 2 {a b *} 3 {a b *}} +sqlite3 db test.db -unsetnull 1 +do_test tcl-16.103 { + set res {} + unset -nocomplain x + db eval {SELECT * FROM t1} x { + lappend res $x(a) [array names x] + } + set res +} {1 {a b *} 2 {a *} 3 {a b *}} finish_test