/ Check-in [11ea98e4]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Merge latest trunk changes.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | fts3-changes
Files: files | file ages | folders
SHA1: 11ea98e454ad35b2606e81dec9ac866e4dd9f7e1
User & Date: dan 2011-06-27 19:25:10
Context
2011-06-28
09:51
Merge latest trunk changes with fts3-changes branch. check-in: 22668647 user: dan tags: fts3-changes
2011-06-27
19:25
Merge latest trunk changes. check-in: 11ea98e4 user: dan tags: fts3-changes
19:12
Remove an unnecessary assignment from vdbeapi.c. check-in: 25e5b768 user: dan tags: fts3-changes
16:55
Add a case to permutations.test to run the veryquick test suite using sqlite3_prepare() instead of sqlite3_prepare_v2(). This helps to test the fix for bug [25ee812710]. check-in: d9f7993b user: dan tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.msc.

    39     39   
    40     40   # Define -DNDEBUG to compile without debugging (i.e., for production usage)
    41     41   # Omitting the define will cause extra debugging code to be inserted and
    42     42   # includes extra comments when "EXPLAIN stmt" is used.
    43     43   #
    44     44   TCC = $(TCC) -DNDEBUG
    45     45   
    46         -# The library that programs using TCL must link against.
           46  +# The locations of the Tcl header and library files.  Also, the library that
           47  +# non-stubs enabled programs using Tcl must link against.  These variables
           48  +# (TCLINCDIR, TCLLIBDIR, and LIBTCL) may be overridden via the environment
           49  +# prior to running nmake in order to match the actual installed location and
           50  +# version on this machine.
    47     51   #
    48         -LIBTCL = tcl85.lib
           52  +!if "$(TCLINCDIR)" == ""
    49     53   TCLINCDIR = c:\tcl\include
           54  +!endif
           55  +
           56  +!if "$(TCLLIBDIR)" == ""
    50     57   TCLLIBDIR = c:\tcl\lib
           58  +!endif
           59  +
           60  +!if "$(LIBTCL)" == ""
           61  +LIBTCL = tcl85.lib
           62  +!endif
    51     63   
    52     64   # This is the command to use for tclsh - normally just "tclsh", but we may
    53         -# know the specific version we want to use
           65  +# know the specific version we want to use.  This variable (TCLSH_CMD) may be
           66  +# overridden via the environment prior to running nmake in order to select a
           67  +# specific Tcl shell to use.
    54     68   #
           69  +!if "$(TCLSH_CMD)" == ""
    55     70   TCLSH_CMD = tclsh85
           71  +!endif
    56     72   
    57     73   # Compiler options needed for programs that use the readline() library.
    58     74   #
    59     75   READLINE_FLAGS = -DHAVE_READLINE=0
    60     76   
    61     77   # The library that programs using readline() must link against.
    62     78   #
................................................................................
   114    130   # to deduce the binary type based on the object files.
   115    131   !IF "$(PLATFORM)"!=""
   116    132   LTLINKOPTS = /MACHINE:$(PLATFORM)
   117    133   LTLIBOPTS = /MACHINE:$(PLATFORM)
   118    134   !ENDIF
   119    135   
   120    136   # nawk compatible awk.
   121         -NAWK = .\gawk.exe
          137  +NAWK = gawk.exe
   122    138   
   123    139   # You should not have to change anything below this line
   124    140   ###############################################################################
   125    141   
   126    142   # Object files for the SQLite library (non-amalgamation).
   127    143   #
   128    144   LIBOBJS0 = alter.lo analyze.lo attach.lo auth.lo \
................................................................................
   435    451     $(TOP)\ext\icu\sqliteicu.h
   436    452   EXTHDR = $(EXTHDR) \
   437    453     $(TOP)\ext\rtree\sqlite3rtree.h
   438    454   
   439    455   # This is the default Makefile target.  The objects listed here
   440    456   # are what get build when you type just "make" with no arguments.
   441    457   #
   442         -all:	libsqlite3.lib sqlite3.exe libtclsqlite3.lib
          458  +all:	dll libsqlite3.lib sqlite3.exe libtclsqlite3.lib
   443    459   
   444    460   libsqlite3.lib:	$(LIBOBJ)
   445    461   	$(LTLIB) $(LTLIBOPTS) /OUT:$@ $(LIBOBJ) $(TLIBS)
   446    462   
   447    463   libtclsqlite3.lib:	tclsqlite.lo libsqlite3.lib
   448    464   	$(LTLIB) $(LTLIBOPTS) /LIBPATH:$(TCLLIBDIR) /OUT:$@ tclsqlite.lo libsqlite3.lib $(LIBTCL:tcl=tclstub) $(TLIBS)
   449    465   
................................................................................
   459    475   # all that automatic generation.
   460    476   #
   461    477   .target_source:	$(SRC) $(TOP)\tool\vdbe-compress.tcl
   462    478   	-rmdir /S/Q tsrc
   463    479   	-mkdir tsrc
   464    480   	for %i in ($(SRC)) do copy /Y %i tsrc
   465    481   	del /Q tsrc\sqlite.h.in tsrc\parse.y
   466         -	$(TCLSH_CMD) $(TOP)\tool\vdbe-compress.tcl <tsrc\vdbe.c >vdbe.new
          482  +	$(TCLSH_CMD) $(TOP)\tool\vdbe-compress.tcl < tsrc\vdbe.c > vdbe.new
   467    483   	move vdbe.new tsrc\vdbe.c
   468    484   	echo > .target_source
   469    485   
   470    486   sqlite3.c:	.target_source $(TOP)\tool\mksqlite3c.tcl
   471    487   	$(TCLSH_CMD) $(TOP)\tool\mksqlite3c.tcl
   472    488   
   473    489   # Rule to build the amalgamation
................................................................................
   716    732   tclsqlite3.exe:	tclsqlite-shell.lo libsqlite3.lib
   717    733   	$(LTLINK) tclsqlite-shell.lo \
   718    734   		/link $(LTLINKOPTS) /LIBPATH:$(TCLLIBDIR) libsqlite3.lib $(LIBTCL)
   719    735   
   720    736   # Rules to build opcodes.c and opcodes.h
   721    737   #
   722    738   opcodes.c:	opcodes.h $(TOP)\mkopcodec.awk
   723         -	$(NAWK) "/#define OP_/ { print }" opcodes.h | sort /+45 | $(NAWK) -f $(TOP)\mkopcodec.awk >opcodes.c
          739  +	$(NAWK) "/#define OP_/ { print }" opcodes.h | sort /+45 | $(NAWK) -f $(TOP)\mkopcodec.awk > opcodes.c
   724    740   
   725    741   opcodes.h:	parse.h $(TOP)\src\vdbe.c $(TOP)\mkopcodeh.awk
   726         -	type parse.h $(TOP)\src\vdbe.c | $(NAWK) -f $(TOP)\mkopcodeh.awk >opcodes.h
          742  +	type parse.h $(TOP)\src\vdbe.c | $(NAWK) -f $(TOP)\mkopcodeh.awk > opcodes.h
   727    743   
   728    744   # Rules to build parse.c and parse.h - the outputs of lemon.
   729    745   #
   730    746   parse.h:	parse.c
   731    747   
   732    748   parse.c:	$(TOP)\src\parse.y lemon.exe $(TOP)\addopcodes.awk
   733    749   	del /Q parse.y parse.h parse.h.temp
   734    750   	copy $(TOP)\src\parse.y .
   735    751   	.\lemon.exe $(OPT_FEATURE_FLAGS) $(OPTS) parse.y
   736    752   	move parse.h parse.h.temp
   737         -	$(NAWK) -f $(TOP)\addopcodes.awk parse.h.temp >parse.h
          753  +	$(NAWK) -f $(TOP)\addopcodes.awk parse.h.temp > parse.h
   738    754   
   739    755   sqlite3.h:	$(TOP)\src\sqlite.h.in $(TOP)\manifest.uuid $(TOP)\VERSION
   740         -	$(TCLSH_CMD) $(TOP)\tool\mksqlite3h.tcl $(TOP) >sqlite3.h
          756  +	$(TCLSH_CMD) $(TOP)\tool\mksqlite3h.tcl $(TOP) > sqlite3.h
   741    757   
   742    758   mkkeywordhash.exe:	$(TOP)\tool\mkkeywordhash.c
   743    759   	$(BCC) -Femkkeywordhash.exe $(OPT_FEATURE_FLAGS) $(OPTS) $(TOP)\tool\mkkeywordhash.c
   744    760   
   745    761   keywordhash.h:	$(TOP)\tool\mkkeywordhash.c mkkeywordhash.exe
   746         -	.\mkkeywordhash.exe >keywordhash.h
          762  +	.\mkkeywordhash.exe > keywordhash.h
   747    763   
   748    764   
   749    765   
   750    766   # Rules to build the extension objects.
   751    767   #
   752    768   icu.lo:	$(TOP)\ext\icu\icu.c $(HDR) $(EXTHDR)
   753    769   	$(LTCOMPILE) -DSQLITE_CORE -c $(TOP)\ext\icu\icu.c
................................................................................
   835    851   	.\testfixture.exe $(TOP)\test\all.test -soak=1
   836    852   
   837    853   test:	testfixture.exe sqlite3.exe
   838    854   	.\testfixture.exe $(TOP)\test\veryquick.test
   839    855   
   840    856   spaceanal_tcl.h:	$(TOP)\tool\spaceanal.tcl
   841    857   	$(NAWK) -f $(TOP)/tool/tostr.awk \
   842         -		$(TOP)\tool\spaceanal.tcl >spaceanal_tcl.h
          858  +		$(TOP)\tool\spaceanal.tcl > spaceanal_tcl.h
   843    859   
   844    860   sqlite3_analyzer.exe:	$(TESTFIXTURE_SRC) spaceanal_tcl.h
   845    861   	$(LTLINK) -DTCLSH=2 -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1 \
   846    862   		-DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE \
   847    863   		-DBUILD_sqlite -I$(TCLINCDIR) \
   848    864   		$(TESTFIXTURE_SRC) \
   849    865   		/link $(LTLINKOPTS) /LIBPATH:$(TCLLIBDIR) $(LIBTCL) $(TLIBS)
................................................................................
   862    878   
   863    879   #
   864    880   # Windows section
   865    881   #
   866    882   dll: sqlite3.dll
   867    883   
   868    884   sqlite3.def: libsqlite3.lib
   869         -	echo EXPORTS >sqlite3.def
          885  +	echo EXPORTS > sqlite3.def
   870    886   	dumpbin /all libsqlite3.lib \
   871    887   		| $(NAWK) "/ 1 _sqlite3_/ { sub(/^.* _/,\"\");print }" \
   872         -		| sort >>sqlite3.def
          888  +		| sort >> sqlite3.def
   873    889   
   874    890   sqlite3.dll: $(LIBOBJ) sqlite3.def
   875    891   	link $(LTLINKOPTS) /DLL /DEF:sqlite3.def /OUT:$@ $(LIBOBJ)

Changes to src/build.c.

  2787   2787       ** the zStmt variable
  2788   2788       */
  2789   2789       if( pStart ){
  2790   2790         assert( pEnd!=0 );
  2791   2791         /* A named index with an explicit CREATE INDEX statement */
  2792   2792         zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
  2793   2793           onError==OE_None ? "" : " UNIQUE",
  2794         -        pEnd->z - pName->z + 1,
         2794  +        (int)(pEnd->z - pName->z) + 1,
  2795   2795           pName->z);
  2796   2796       }else{
  2797   2797         /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
  2798   2798         /* zStmt = sqlite3MPrintf(""); */
  2799   2799         zStmt = 0;
  2800   2800       }
  2801   2801   

Changes to src/date.c.

   423    423   ** routine will always fail.
   424    424   */
   425    425   static int osLocaltime(time_t *t, struct tm *pTm){
   426    426     int rc;
   427    427   #if (!defined(HAVE_LOCALTIME_R) || !HAVE_LOCALTIME_R) \
   428    428         && (!defined(HAVE_LOCALTIME_S) || !HAVE_LOCALTIME_S)
   429    429     struct tm *pX;
          430  +#if SQLITE_THREADSAFE>0
   430    431     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
          432  +#endif
   431    433     sqlite3_mutex_enter(mutex);
   432    434     pX = localtime(t);
   433    435   #ifndef SQLITE_OMIT_BUILTIN_TEST
   434    436     if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0;
   435    437   #endif
   436    438     if( pX ) *pTm = *pX;
   437    439     sqlite3_mutex_leave(mutex);

Changes to src/delete.c.

   632    632         sqlite3VdbeAddOp2(v, OP_SCopy, regBase+nCol, regBase+j);
   633    633       }else{
   634    634         sqlite3VdbeAddOp3(v, OP_Column, iCur, idx, regBase+j);
   635    635         sqlite3ColumnDefault(v, pTab, idx, -1);
   636    636       }
   637    637     }
   638    638     if( doMakeRec ){
   639         -    const char *zAff = pTab->pSelect ? 0 : sqlite3IndexAffinityStr(v, pIdx);
          639  +    const char *zAff;
          640  +    if( pTab->pSelect || (pParse->db->flags & SQLITE_IdxRealAsInt)!=0 ){
          641  +      zAff = 0;
          642  +    }else{
          643  +      zAff = sqlite3IndexAffinityStr(v, pIdx);
          644  +    }
   640    645       sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut);
   641    646       sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
   642    647     }
   643    648     sqlite3ReleaseTempRange(pParse, regBase, nCol+1);
   644    649     return regBase;
   645    650   }

Changes to src/os_unix.c.

  3668   3668   */
  3669   3669   static void unixShmPurge(unixFile *pFd){
  3670   3670     unixShmNode *p = pFd->pInode->pShmNode;
  3671   3671     assert( unixMutexHeld() );
  3672   3672     if( p && p->nRef==0 ){
  3673   3673       int i;
  3674   3674       assert( p->pInode==pFd->pInode );
  3675         -    if( p->mutex ) sqlite3_mutex_free(p->mutex);
         3675  +    sqlite3_mutex_free(p->mutex);
  3676   3676       for(i=0; i<p->nRegion; i++){
  3677   3677         if( p->h>=0 ){
  3678   3678           munmap(p->apRegion[i], p->szRegion);
  3679   3679         }else{
  3680   3680           sqlite3_free(p->apRegion[i]);
  3681   3681         }
  3682   3682       }

Changes to src/sqliteInt.h.

   951    951   #define SQLITE_QueryFlattener 0x01        /* Disable query flattening */
   952    952   #define SQLITE_ColumnCache    0x02        /* Disable the column cache */
   953    953   #define SQLITE_IndexSort      0x04        /* Disable indexes for sorting */
   954    954   #define SQLITE_IndexSearch    0x08        /* Disable indexes for searching */
   955    955   #define SQLITE_IndexCover     0x10        /* Disable index covering table */
   956    956   #define SQLITE_GroupByOrder   0x20        /* Disable GROUPBY cover of ORDERBY */
   957    957   #define SQLITE_FactorOutConst 0x40        /* Disable factoring out constants */
          958  +#define SQLITE_IdxRealAsInt   0x80        /* Store REAL as INT in indices */
   958    959   #define SQLITE_OptMask        0xff        /* Mask of all disablable opts */
   959    960   
   960    961   /*
   961    962   ** Possible values for the sqlite.magic field.
   962    963   ** The numbers are obtained at random and have no special meaning, other
   963    964   ** than being distinct from one another.
   964    965   */

Changes to src/tclsqlite.c.

   103    103   };
   104    104   
   105    105   typedef struct IncrblobChannel IncrblobChannel;
   106    106   
   107    107   /*
   108    108   ** There is one instance of this structure for each SQLite database
   109    109   ** that has been opened by the SQLite TCL interface.
          110  +**
          111  +** If this module is built with SQLITE_TEST defined (to create the SQLite
          112  +** testfixture executable), then it may be configured to use either
          113  +** sqlite3_prepare_v2() or sqlite3_prepare() to prepare SQL statements.
          114  +** If SqliteDb.bLegacyPrepare is true, sqlite3_prepare() is used.
   110    115   */
   111    116   typedef struct SqliteDb SqliteDb;
   112    117   struct SqliteDb {
   113    118     sqlite3 *db;               /* The "real" database structure. MUST BE FIRST */
   114    119     Tcl_Interp *interp;        /* The interpreter used for this database */
   115    120     char *zBusy;               /* The busy callback routine */
   116    121     char *zCommit;             /* The commit hook callback routine */
................................................................................
   131    136     SqlPreparedStmt *stmtList; /* List of prepared statements*/
   132    137     SqlPreparedStmt *stmtLast; /* Last statement in the list */
   133    138     int maxStmt;               /* The next maximum number of stmtList */
   134    139     int nStmt;                 /* Number of statements in stmtList */
   135    140     IncrblobChannel *pIncrblob;/* Linked list of open incrblob channels */
   136    141     int nStep, nSort, nIndex;  /* Statistics for most recent operation */
   137    142     int nTransaction;          /* Number of nested [transaction] methods */
          143  +#ifdef SQLITE_TEST
          144  +  int bLegacyPrepare;        /* True to use sqlite3_prepare() */
          145  +#endif
   138    146   };
   139    147   
   140    148   struct IncrblobChannel {
   141    149     sqlite3_blob *pBlob;      /* sqlite3 blob handle */
   142    150     SqliteDb *pDb;            /* Associated database connection */
   143    151     int iSeek;                /* Current seek offset */
   144    152     Tcl_Channel channel;      /* Channel identifier */
................................................................................
   424    432     }
   425    433     pNew->interp = pDb->interp;
   426    434     pNew->pScript = 0;
   427    435     pNew->pNext = pDb->pFunc;
   428    436     pDb->pFunc = pNew;
   429    437     return pNew;
   430    438   }
          439  +
          440  +/*
          441  +** Free a single SqlPreparedStmt object.
          442  +*/
          443  +static void dbFreeStmt(SqlPreparedStmt *pStmt){
          444  +#ifdef SQLITE_TEST
          445  +  if( sqlite3_sql(pStmt->pStmt)==0 ){
          446  +    Tcl_Free((char *)pStmt->zSql);
          447  +  }
          448  +#endif
          449  +  sqlite3_finalize(pStmt->pStmt);
          450  +  Tcl_Free((char *)pStmt);
          451  +}
   431    452   
   432    453   /*
   433    454   ** Finalize and free a list of prepared statements
   434    455   */
   435         -static void flushStmtCache( SqliteDb *pDb ){
          456  +static void flushStmtCache(SqliteDb *pDb){
   436    457     SqlPreparedStmt *pPreStmt;
          458  +  SqlPreparedStmt *pNext;
   437    459   
   438         -  while(  pDb->stmtList ){
   439         -    sqlite3_finalize( pDb->stmtList->pStmt );
   440         -    pPreStmt = pDb->stmtList;
   441         -    pDb->stmtList = pDb->stmtList->pNext;
   442         -    Tcl_Free( (char*)pPreStmt );
          460  +  for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pNext){
          461  +    pNext = pPreStmt->pNext;
          462  +    dbFreeStmt(pPreStmt);
   443    463     }
   444    464     pDb->nStmt = 0;
   445    465     pDb->stmtLast = 0;
          466  +  pDb->stmtList = 0;
   446    467   }
   447    468   
   448    469   /*
   449    470   ** TCL calls this procedure when an sqlite3 database command is
   450    471   ** deleted.
   451    472   */
   452    473   static void DbDeleteCmd(void *db){
................................................................................
  1025   1046       }
  1026   1047       sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0);
  1027   1048     }
  1028   1049     pDb->disableAuth--;
  1029   1050   
  1030   1051     return rc;
  1031   1052   }
         1053  +
         1054  +/*
         1055  +** Unless SQLITE_TEST is defined, this function is a simple wrapper around
         1056  +** sqlite3_prepare_v2(). If SQLITE_TEST is defined, then it uses either
         1057  +** sqlite3_prepare_v2() or legacy interface sqlite3_prepare(), depending
         1058  +** on whether or not the [db_use_legacy_prepare] command has been used to 
         1059  +** configure the connection.
         1060  +*/
         1061  +static int dbPrepare(
         1062  +  SqliteDb *pDb,                  /* Database object */
         1063  +  const char *zSql,               /* SQL to compile */
         1064  +  sqlite3_stmt **ppStmt,          /* OUT: Prepared statement */
         1065  +  const char **pzOut              /* OUT: Pointer to next SQL statement */
         1066  +){
         1067  +#ifdef SQLITE_TEST
         1068  +  if( pDb->bLegacyPrepare ){
         1069  +    return sqlite3_prepare(pDb->db, zSql, -1, ppStmt, pzOut);
         1070  +  }
         1071  +#endif
         1072  +  return sqlite3_prepare_v2(pDb->db, zSql, -1, ppStmt, pzOut);
         1073  +}
  1032   1074   
  1033   1075   /*
  1034   1076   ** Search the cache for a prepared-statement object that implements the
  1035   1077   ** first SQL statement in the buffer pointed to by parameter zIn. If
  1036   1078   ** no such prepared-statement can be found, allocate and prepare a new
  1037   1079   ** one. In either case, bind the current values of the relevant Tcl
  1038   1080   ** variables to any $var, :var or @var variables in the statement. Before
................................................................................
  1096   1138     }
  1097   1139     
  1098   1140     /* If no prepared statement was found. Compile the SQL text. Also allocate
  1099   1141     ** a new SqlPreparedStmt structure.  */
  1100   1142     if( pPreStmt==0 ){
  1101   1143       int nByte;
  1102   1144   
  1103         -    if( SQLITE_OK!=sqlite3_prepare_v2(pDb->db, zSql, -1, &pStmt, pzOut) ){
         1145  +    if( SQLITE_OK!=dbPrepare(pDb, zSql, &pStmt, pzOut) ){
  1104   1146         Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
  1105   1147         return TCL_ERROR;
  1106   1148       }
  1107   1149       if( pStmt==0 ){
  1108   1150         if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){
  1109   1151           /* A compile-time error in the statement. */
  1110   1152           Tcl_SetObjResult(interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
................................................................................
  1123   1165       pPreStmt = (SqlPreparedStmt*)Tcl_Alloc(nByte);
  1124   1166       memset(pPreStmt, 0, nByte);
  1125   1167   
  1126   1168       pPreStmt->pStmt = pStmt;
  1127   1169       pPreStmt->nSql = (*pzOut - zSql);
  1128   1170       pPreStmt->zSql = sqlite3_sql(pStmt);
  1129   1171       pPreStmt->apParm = (Tcl_Obj **)&pPreStmt[1];
         1172  +#ifdef SQLITE_TEST
         1173  +    if( pPreStmt->zSql==0 ){
         1174  +      char *zCopy = Tcl_Alloc(pPreStmt->nSql + 1);
         1175  +      memcpy(zCopy, zSql, pPreStmt->nSql);
         1176  +      zCopy[pPreStmt->nSql] = '\0';
         1177  +      pPreStmt->zSql = zCopy;
         1178  +    }
         1179  +#endif
  1130   1180     }
  1131   1181     assert( pPreStmt );
  1132   1182     assert( strlen30(pPreStmt->zSql)==pPreStmt->nSql );
  1133   1183     assert( 0==memcmp(pPreStmt->zSql, zSql, pPreStmt->nSql) );
  1134   1184   
  1135   1185     /* Bind values to parameters that begin with $ or : */  
  1136   1186     for(i=1; i<=nVar; i++){
................................................................................
  1176   1226     }
  1177   1227     pPreStmt->nParm = iParm;
  1178   1228     *ppPreStmt = pPreStmt;
  1179   1229   
  1180   1230     return TCL_OK;
  1181   1231   }
  1182   1232   
  1183         -
  1184   1233   /*
  1185   1234   ** Release a statement reference obtained by calling dbPrepareAndBind().
  1186   1235   ** There should be exactly one call to this function for each call to
  1187   1236   ** dbPrepareAndBind().
  1188   1237   **
  1189   1238   ** If the discard parameter is non-zero, then the statement is deleted
  1190   1239   ** immediately. Otherwise it is added to the LRU list and may be returned
................................................................................
  1201   1250     for(i=0; i<pPreStmt->nParm; i++){
  1202   1251       Tcl_DecrRefCount(pPreStmt->apParm[i]);
  1203   1252     }
  1204   1253     pPreStmt->nParm = 0;
  1205   1254   
  1206   1255     if( pDb->maxStmt<=0 || discard ){
  1207   1256       /* If the cache is turned off, deallocated the statement */
  1208         -    sqlite3_finalize(pPreStmt->pStmt);
  1209         -    Tcl_Free((char *)pPreStmt);
         1257  +    dbFreeStmt(pPreStmt);
  1210   1258     }else{
  1211   1259       /* Add the prepared statement to the beginning of the cache list. */
  1212   1260       pPreStmt->pNext = pDb->stmtList;
  1213   1261       pPreStmt->pPrev = 0;
  1214   1262       if( pDb->stmtList ){
  1215   1263        pDb->stmtList->pPrev = pPreStmt;
  1216   1264       }
................................................................................
  1222   1270         assert( pDb->nStmt>0 );
  1223   1271       }
  1224   1272       pDb->nStmt++;
  1225   1273      
  1226   1274       /* If we have too many statement in cache, remove the surplus from 
  1227   1275       ** the end of the cache list.  */
  1228   1276       while( pDb->nStmt>pDb->maxStmt ){
  1229         -      sqlite3_finalize(pDb->stmtLast->pStmt);
  1230         -      pDb->stmtLast = pDb->stmtLast->pPrev;
  1231         -      Tcl_Free((char*)pDb->stmtLast->pNext);
         1277  +      SqlPreparedStmt *pLast = pDb->stmtLast;
         1278  +      pDb->stmtLast = pLast->pPrev;
  1232   1279         pDb->stmtLast->pNext = 0;
  1233   1280         pDb->nStmt--;
         1281  +      dbFreeStmt(pLast);
  1234   1282       }
  1235   1283     }
  1236   1284   }
  1237   1285   
  1238   1286   /*
  1239   1287   ** Structure used with dbEvalXXX() functions:
  1240   1288   **
................................................................................
  1359   1407   ** A return value of TCL_OK means there is a row of data available. The
  1360   1408   ** data may be accessed using dbEvalRowInfo() and dbEvalColumnValue(). This
  1361   1409   ** is analogous to a return of SQLITE_ROW from sqlite3_step(). If TCL_BREAK
  1362   1410   ** is returned, then the SQL script has finished executing and there are
  1363   1411   ** no further rows available. This is similar to SQLITE_DONE.
  1364   1412   */
  1365   1413   static int dbEvalStep(DbEvalContext *p){
         1414  +  const char *zPrevSql = 0;       /* Previous value of p->zSql */
         1415  +
  1366   1416     while( p->zSql[0] || p->pPreStmt ){
  1367   1417       int rc;
  1368   1418       if( p->pPreStmt==0 ){
         1419  +      zPrevSql = (p->zSql==zPrevSql ? 0 : p->zSql);
  1369   1420         rc = dbPrepareAndBind(p->pDb, p->zSql, &p->zSql, &p->pPreStmt);
  1370   1421         if( rc!=TCL_OK ) return rc;
  1371   1422       }else{
  1372   1423         int rcs;
  1373   1424         SqliteDb *pDb = p->pDb;
  1374   1425         SqlPreparedStmt *pPreStmt = p->pPreStmt;
  1375   1426         sqlite3_stmt *pStmt = pPreStmt->pStmt;
................................................................................
  1388   1439         pDb->nIndex = sqlite3_stmt_status(pStmt,SQLITE_STMTSTATUS_AUTOINDEX,1);
  1389   1440         dbReleaseColumnNames(p);
  1390   1441         p->pPreStmt = 0;
  1391   1442   
  1392   1443         if( rcs!=SQLITE_OK ){
  1393   1444           /* If a run-time error occurs, report the error and stop reading
  1394   1445           ** the SQL.  */
  1395         -        Tcl_SetObjResult(pDb->interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
  1396   1446           dbReleaseStmt(pDb, pPreStmt, 1);
         1447  +#if SQLITE_TEST
         1448  +        if( p->pDb->bLegacyPrepare && rcs==SQLITE_SCHEMA && zPrevSql ){
         1449  +          /* If the runtime error was an SQLITE_SCHEMA, and the database
         1450  +          ** handle is configured to use the legacy sqlite3_prepare() 
         1451  +          ** interface, retry prepare()/step() on the same SQL statement.
         1452  +          ** This only happens once. If there is a second SQLITE_SCHEMA
         1453  +          ** error, the error will be returned to the caller. */
         1454  +          p->zSql = zPrevSql;
         1455  +          continue;
         1456  +        }
         1457  +#endif
         1458  +        Tcl_SetObjResult(pDb->interp, dbTextToObj(sqlite3_errmsg(pDb->db)));
  1397   1459           return TCL_ERROR;
  1398   1460         }else{
  1399   1461           dbReleaseStmt(pDb, pPreStmt, 0);
  1400   1462         }
  1401   1463       }
  1402   1464     }
  1403   1465   
................................................................................
  2922   2984         if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR;
  2923   2985         if( b ){
  2924   2986           flags |= SQLITE_OPEN_NOMUTEX;
  2925   2987           flags &= ~SQLITE_OPEN_FULLMUTEX;
  2926   2988         }else{
  2927   2989           flags &= ~SQLITE_OPEN_NOMUTEX;
  2928   2990         }
  2929         -   }else if( strcmp(zArg, "-fullmutex")==0 ){
         2991  +    }else if( strcmp(zArg, "-fullmutex")==0 ){
  2930   2992         int b;
  2931   2993         if( Tcl_GetBooleanFromObj(interp, objv[i+1], &b) ) return TCL_ERROR;
  2932   2994         if( b ){
  2933   2995           flags |= SQLITE_OPEN_FULLMUTEX;
  2934   2996           flags &= ~SQLITE_OPEN_NOMUTEX;
  2935   2997         }else{
  2936   2998           flags &= ~SQLITE_OPEN_FULLMUTEX;
................................................................................
  3523   3585     if( !slave ){
  3524   3586       return TCL_ERROR;
  3525   3587     }
  3526   3588   
  3527   3589     init_all(slave);
  3528   3590     return TCL_OK;
  3529   3591   }
         3592  +
         3593  +/*
         3594  +** Tclcmd: db_use_legacy_prepare DB BOOLEAN
         3595  +**
         3596  +**   The first argument to this command must be a database command created by
         3597  +**   [sqlite3]. If the second argument is true, then the handle is configured
         3598  +**   to use the sqlite3_prepare_v2() function to prepare statements. If it
         3599  +**   is false, sqlite3_prepare().
         3600  +*/
         3601  +static int db_use_legacy_prepare_cmd(
         3602  +  ClientData cd,
         3603  +  Tcl_Interp *interp,
         3604  +  int objc,
         3605  +  Tcl_Obj *CONST objv[]
         3606  +){
         3607  +  Tcl_CmdInfo cmdInfo;
         3608  +  SqliteDb *pDb;
         3609  +  int bPrepare;
         3610  +
         3611  +  if( objc!=3 ){
         3612  +    Tcl_WrongNumArgs(interp, 1, objv, "DB BOOLEAN");
         3613  +    return TCL_ERROR;
         3614  +  }
         3615  +
         3616  +  if( !Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
         3617  +    Tcl_AppendResult(interp, "no such db: ", Tcl_GetString(objv[1]), (char*)0);
         3618  +    return TCL_ERROR;
         3619  +  }
         3620  +  pDb = (SqliteDb*)cmdInfo.objClientData;
         3621  +  if( Tcl_GetBooleanFromObj(interp, objv[2], &bPrepare) ){
         3622  +    return TCL_ERROR;
         3623  +  }
         3624  +
         3625  +  pDb->bLegacyPrepare = bPrepare;
         3626  +
         3627  +  Tcl_ResetResult(interp);
         3628  +  return TCL_OK;
         3629  +}
  3530   3630   #endif
  3531   3631   
  3532   3632   /*
  3533   3633   ** Configure the interpreter passed as the first argument to have access
  3534   3634   ** to the commands and linked variables that make up:
  3535   3635   **
  3536   3636   **   * the [sqlite3] extension itself, 
................................................................................
  3629   3729       Sqlitetestfuzzer_Init(interp);
  3630   3730       Sqlitetestwholenumber_Init(interp);
  3631   3731   
  3632   3732   #if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
  3633   3733       Sqlitetestfts3_Init(interp);
  3634   3734   #endif
  3635   3735   
  3636         -    Tcl_CreateObjCommand(interp,"load_testfixture_extensions",init_all_cmd,0,0);
         3736  +    Tcl_CreateObjCommand(
         3737  +        interp, "load_testfixture_extensions", init_all_cmd, 0, 0
         3738  +    );
         3739  +    Tcl_CreateObjCommand(
         3740  +        interp, "db_use_legacy_prepare", db_use_legacy_prepare_cmd, 0, 0
         3741  +    );
  3637   3742   
  3638   3743   #ifdef SQLITE_SSE
  3639   3744       Sqlitetestsse_Init(interp);
  3640   3745   #endif
  3641   3746     }
  3642   3747   #endif
  3643   3748   }

Changes to src/test1.c.

  5601   5601       { "query-flattener",  SQLITE_QueryFlattener },
  5602   5602       { "column-cache",     SQLITE_ColumnCache    },
  5603   5603       { "index-sort",       SQLITE_IndexSort      },
  5604   5604       { "index-search",     SQLITE_IndexSearch    },
  5605   5605       { "index-cover",      SQLITE_IndexCover     },
  5606   5606       { "groupby-order",    SQLITE_GroupByOrder   },
  5607   5607       { "factor-constants", SQLITE_FactorOutConst },
         5608  +    { "real-as-int",      SQLITE_IdxRealAsInt   },
  5608   5609     };
  5609   5610   
  5610   5611     if( objc!=4 ){
  5611   5612       Tcl_WrongNumArgs(interp, 1, objv, "DB OPT BOOLEAN");
  5612   5613       return TCL_ERROR;
  5613   5614     }
  5614   5615     if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;

Changes to src/vdbeaux.c.

  1501   1501     p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
  1502   1502     if( pParse->explain && nMem<10 ){
  1503   1503       nMem = 10;
  1504   1504     }
  1505   1505     memset(zCsr, 0, zEnd-zCsr);
  1506   1506     zCsr += (zCsr - (u8*)0)&7;
  1507   1507     assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
         1508  +  p->expired = 0;
  1508   1509   
  1509   1510     /* Memory for registers, parameters, cursor, etc, is allocated in two
  1510   1511     ** passes.  On the first pass, we try to reuse unused space at the 
  1511   1512     ** end of the opcode array.  If we are unable to satisfy all memory
  1512   1513     ** requirements by reusing the opcode array tail, then the second
  1513   1514     ** pass will fill in the rest using a fresh allocation.  
  1514   1515     **

Changes to test/all.test.

    34     34   run_test_suite journaltest 
    35     35   run_test_suite inmemory_journal
    36     36   run_test_suite pcache0 
    37     37   run_test_suite pcache10 
    38     38   run_test_suite pcache50 
    39     39   run_test_suite pcache90 
    40     40   run_test_suite pcache100
           41  +run_test_suite prepare
    41     42   
    42     43   if {$::tcl_platform(platform)=="unix"} {
    43     44     ifcapable !default_autovacuum {
    44     45       run_test_suite autovacuum_crash
    45     46     }
    46     47   }
    47     48   
    48     49   finish_test
    49     50   
    50     51   

Changes to test/alter2.test.

   133    133     execsql {
   134    134       SELECT sum(a), c FROM abc GROUP BY c;
   135    135     }
   136    136   } {8 {} 1 10}
   137    137   do_test alter2-1.9 {
   138    138     # ALTER TABLE abc ADD COLUMN d;
   139    139     alter_table abc {CREATE TABLE abc(a, b, c, d);}
          140  +  if {[permutation] == "prepare"} { db cache flush }
   140    141     execsql { SELECT * FROM abc; }
   141    142     execsql {
   142    143       UPDATE abc SET d = 11 WHERE c IS NULL AND a<4;
   143    144       SELECT * FROM abc;
   144    145     }
   145    146   } {1 2 10 {} 3 4 {} 11 5 6 {} {}}
   146    147   do_test alter2-1.10 {

Changes to test/exists.test.

   155    155         sql1 { DROP INDEX IF EXISTS aux.i2 }
   156    156         sql2 { SELECT name FROM aux.sqlite_master WHERE type = 'index' }
   157    157       } {}
   158    158       do_test 3.$tn.2.2 {
   159    159         sql1 { DROP INDEX IF EXISTS i2 }
   160    160         sql2 { CREATE INDEX aux.i2 ON t2(x) }
   161    161         sql1 { DROP INDEX IF EXISTS i2 }
   162         -      sql2 { SELECT name FROM aux.sqlite_master WHERE type = 'index' }
          162  +      sql2 { SELECT * FROM aux.sqlite_master WHERE type = 'index' }
   163    163       } {}
   164    164   
   165    165       # VIEW objects.
   166    166       #
   167    167       do_test 3.$tn.3.1 {
   168    168         sql1 { DROP VIEW IF EXISTS aux.v1 }
   169    169         sql2 { CREATE VIEW aux.v1 AS SELECT * FROM t2 }

Changes to test/incrblob_err.test.

    31     31     set bytes [file size [info script]]
    32     32     execsql {
    33     33       CREATE TABLE blobs(k, v BLOB);
    34     34       INSERT INTO blobs VALUES(1, zeroblob($::bytes));
    35     35     }
    36     36   } -tclbody {
    37     37     set ::blob [db incrblob blobs v 1]
           38  +  fconfigure $::blob -translation binary
    38     39     set rc [catch {puts -nonewline $::blob $::data}]
    39     40     if {$rc} { error "out of memory" }
    40     41   } 
    41     42   
    42     43   do_malloc_test 2 -tclprep {
    43     44     execsql {
    44     45       CREATE TABLE blobs(k, v BLOB);
................................................................................
    67     68     } elseif {$::r ne $::data} {
    68     69       error "Bad data read..."
    69     70     }
    70     71     set rc [catch {close $::blob}]
    71     72     if {$rc} { 
    72     73       error "out of memory" 
    73     74     }
    74         -} 
    75         -
           75  +}
    76     76   
    77     77   do_ioerr_test incrblob_err-4 -cksum 1 -sqlprep {
    78     78     CREATE TABLE blobs(k, v BLOB);
    79     79     INSERT INTO blobs VALUES(1, $::data);
    80     80   } -tclbody {
    81     81     set ::blob [db incrblob blobs v 1]
    82     82     read $::blob
................................................................................
    83     83   }
    84     84   
    85     85   do_ioerr_test incrblob_err-5 -cksum 1 -sqlprep {
    86     86     CREATE TABLE blobs(k, v BLOB);
    87     87     INSERT INTO blobs VALUES(1, zeroblob(length(CAST($::data AS BLOB))));
    88     88   } -tclbody {
    89     89     set ::blob [db incrblob blobs v 1]
           90  +  fconfigure $::blob -translation binary
    90     91     puts -nonewline $::blob $::data
    91     92     close $::blob
    92     93   }
    93     94   
    94     95   do_ioerr_test incrblob_err-6 -cksum 1 -sqlprep {
    95     96     CREATE TABLE blobs(k, v BLOB);
    96     97     INSERT INTO blobs VALUES(1, $::data || $::data || $::data);
    97     98   } -tclbody {
    98     99     set ::blob [db incrblob blobs v 1]
          100  +  fconfigure $::blob -translation binary
    99    101     seek $::blob -20 end
   100    102     puts -nonewline $::blob "12345678900987654321"
   101    103     close $::blob
   102    104   }
   103    105   
   104    106   do_ioerr_test incrblob_err-7 -cksum 1 -sqlprep {
   105    107     PRAGMA auto_vacuum = 1;

Changes to test/like.test.

    66     66   } {ABC abc}
    67     67   do_test like-1.4 {
    68     68     execsql {
    69     69       SELECT x FROM t1 WHERE x LIKE 'aBc' ORDER BY 1;
    70     70     }
    71     71   } {ABC abc}
    72     72   do_test like-1.5.1 {
           73  +  # Use sqlite3_exec() to verify fix for ticket [25ee81271091] 2011-06-26
           74  +  sqlite3_exec db {PRAGMA case_sensitive_like=on}
           75  +} {0 {}}
           76  +do_test like-1.5.2 {
    73     77     execsql {
    74         -    PRAGMA case_sensitive_like=on;
    75     78       SELECT x FROM t1 WHERE x LIKE 'abc' ORDER BY 1;
    76     79     }
    77     80   } {abc}
    78         -do_test like-1.5.2 {
           81  +do_test like-1.5.3 {
    79     82     execsql {
    80     83       PRAGMA case_sensitive_like; -- no argument; does not change setting
    81     84       SELECT x FROM t1 WHERE x LIKE 'abc' ORDER BY 1;
    82     85     }
    83     86   } {abc}
    84     87   do_test like-1.6 {
    85     88     execsql {

Changes to test/permutations.test.

   771    771     where6.test where7.test where8.test where9.test
   772    772     whereA.test whereB.test wherelimit.test
   773    773     select1.test select2.test select3.test select4.test select5.test
   774    774     select7.test select8.test selectA.test selectC.test
   775    775   } -dbconfig {
   776    776     optimization_control $::dbhandle all 0
   777    777   }
          778  +
          779  +test_suite "prepare" -description {
          780  +  Run tests with the db connection using sqlite3_prepare() instead of _v2().
          781  +} -dbconfig {
          782  +  db_use_legacy_prepare $::dbhandle 1
          783  +  #$::dbhandle cache size 0
          784  +} -files [
          785  +  test_set $allquicktests -exclude *malloc* *ioerr* *fault*
          786  +]
   778    787   
   779    788   # End of tests
   780    789   #############################################################################
   781    790   
   782    791   # run_tests NAME OPTIONS
   783    792   #
   784    793   # where available options are:  

Changes to test/temptable.test.

   288    288   # but the permanent index should still be accessible and should still
   289    289   # be updated when its corresponding table changes.
   290    290   #
   291    291   do_test temptable-5.1 {
   292    292     execsql {
   293    293       CREATE TEMP TABLE mask(a,b,c)
   294    294     } db2
          295  +  if {[permutation]=="prepare"} { db2 cache flush }
   295    296     execsql {
   296    297       CREATE INDEX mask ON t2(x);
   297    298       SELECT * FROM t2;
   298    299     }
   299    300   } {3 4}
   300    301   #do_test temptable-5.2 {
   301    302   #  catchsql {

Changes to tool/warnings.sh.

     4      4   # compiler warnings in SQLite.
     5      5   #
     6      6   make sqlite3.c
     7      7   echo '********** No optimizations.  Includes FTS4 and RTREE *********'
     8      8   gcc -c -Wshadow -Wall -Wextra -pedantic-errors -Wno-long-long -std=c89 \
     9      9         -ansi -DHAVE_STDINT_H -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_RTREE \
    10     10         sqlite3.c
    11         -echo '********** Optimized -O3.  Includes FTS4 and RTREE *********'
           11  +echo '********** No optimizations. ENABLE_STAT2. THREADSAFE=0 *******'
           12  +gcc -c -Wshadow -Wall -Wextra -pedantic-errors -Wno-long-long -std=c89 \
           13  +      -ansi -DSQLITE_ENABLE_STAT2 -DSQLITE_THREADSAFE=0 \
           14  +      sqlite3.c
           15  +echo '********** Optimized -O3.  Includes FTS4 and RTREE ************'
    12     16   gcc -O3 -c -Wshadow -Wall -Wextra -pedantic-errors -Wno-long-long -std=c89 \
    13     17         -ansi -DHAVE_STDINT_H -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_RTREE \
    14     18         sqlite3.c