/ Check-in [790ea39a]
Login

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

Overview
Comment:Add support for the SQLITE_PREPARE_NORMALIZED flag and the sqlite3_normalized_sql() when compiling with SQLITE_ENABLE_NORMALIZE. Also remove unnecessary whitespace from Makefiles.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 790ea39a6585ea9f4dad9e132e1fb0447ac1558f728196580d2c3edee84823f7
User & Date: drh 2018-10-31 19:01:13
Context
2018-10-31
20:52
Deploy the sqlite3Strlen30NN() function (argument guaranteed to be non-NULL) for a small performance improvement. check-in: 4a6ad519 user: drh tags: trunk
19:01
Add support for the SQLITE_PREPARE_NORMALIZED flag and the sqlite3_normalized_sql() when compiling with SQLITE_ENABLE_NORMALIZE. Also remove unnecessary whitespace from Makefiles. check-in: 790ea39a user: drh tags: trunk
18:24
Tweaks to the test_intarray documentation and tests. check-in: 7107f0da user: drh tags: trunk
2018-10-29
17:53
Add the sqlite3_normalized_sql() API. Closed-Leaf check-in: 592b66e8 user: mistachkin tags: normalized_sql
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

    18     18   TOP = @abs_srcdir@
    19     19   
    20     20   # C Compiler and options for use in building executables that
    21     21   # will run on the platform that is doing the build.
    22     22   #
    23     23   BCC = @BUILD_CC@ @BUILD_CFLAGS@
    24     24   
    25         -# TCC is the C Compile and options for use in building executables that 
           25  +# TCC is the C Compile and options for use in building executables that
    26     26   # will run on the target platform.  (BCC and TCC are usually the
    27     27   # same unless your are cross-compiling.)  Separate CC and CFLAGS macros
    28     28   # are provide so that these aspects of the build process can be changed
    29     29   # on the "make" command-line.  Ex:  "make CC=clang CFLAGS=-fsanitize=undefined"
    30     30   #
    31     31   CC = @CC@
    32     32   CFLAGS = @CPPFLAGS@ @CFLAGS@
    33     33   TCC = ${CC} ${CFLAGS} -I. -I${TOP}/src -I${TOP}/ext/rtree -I${TOP}/ext/icu
    34     34   TCC += -I${TOP}/ext/fts3 -I${TOP}/ext/async -I${TOP}/ext/session
    35     35   
    36     36   # Define this for the autoconf-based build, so that the code knows it can
    37     37   # include the generated config.h
    38         -# 
           38  +#
    39     39   TCC += -D_HAVE_SQLITE_CONFIG_H -DBUILD_sqlite
    40     40   
    41     41   # Define -DNDEBUG to compile without debugging (i.e., for production usage)
    42     42   # Omitting the define will cause extra debugging code to be inserted and
    43     43   # includes extra comments when "EXPLAIN stmt" is used.
    44     44   #
    45     45   TCC += @TARGET_DEBUG@
................................................................................
    62     62   LIBREADLINE = @TARGET_READLINE_LIBS@
    63     63   
    64     64   # Should the database engine be compiled threadsafe
    65     65   #
    66     66   TCC += -DSQLITE_THREADSAFE=@SQLITE_THREADSAFE@
    67     67   
    68     68   # Any target libraries which libsqlite must be linked against
    69         -# 
           69  +#
    70     70   TLIBS = @LIBS@ $(LIBS)
    71     71   
    72     72   # Flags controlling use of the in memory btree implementation
    73     73   #
    74     74   # SQLITE_TEMP_STORE is 0 to force temporary tables to be in a file, 1 to
    75     75   # default to file, 2 to default to memory, and 3 to force temporary
    76     76   # tables to always be in memory.
    77     77   #
    78     78   TEMP_STORE = -DSQLITE_TEMP_STORE=@TEMP_STORE@
    79     79   
    80     80   # Enable/disable loadable extensions, and other optional features
    81         -# based on configuration. (-DSQLITE_OMIT*, -DSQLITE_ENABLE*).  
    82         -# The same set of OMIT and ENABLE flags should be passed to the 
           81  +# based on configuration. (-DSQLITE_OMIT*, -DSQLITE_ENABLE*).
           82  +# The same set of OMIT and ENABLE flags should be passed to the
    83     83   # LEMON parser generator and the mkkeywordhash tool as well.
    84     84   OPT_FEATURE_FLAGS = @OPT_FEATURE_FLAGS@
    85     85   
    86     86   TCC += $(OPT_FEATURE_FLAGS)
    87     87   
    88     88   # Add in any optional parameters specified on the make commane line
    89     89   # ie.  make "OPTS=-DSQLITE_ENABLE_FOO=1 -DSQLITE_OMIT_FOO=1".
................................................................................
   122    122   # The suffix used on shared libraries.  Ex:  ".dll", ".so", ".dylib"
   123    123   #
   124    124   SHLIB_SUFFIX = @TCL_SHLIB_SUFFIX@
   125    125   
   126    126   # If gcov support was enabled by the configure script, add the appropriate
   127    127   # flags here.  It's not always as easy as just having the user add the right
   128    128   # CFLAGS / LDFLAGS, because libtool wants to use CFLAGS when linking, which
   129         -# causes build errors with -fprofile-arcs -ftest-coverage with some GCCs.  
   130         -# Supposedly GCC does the right thing if you use --coverage, but in 
          129  +# causes build errors with -fprofile-arcs -ftest-coverage with some GCCs.
          130  +# Supposedly GCC does the right thing if you use --coverage, but in
   131    131   # practice it still fails.  See:
   132    132   #
   133    133   # http://www.mail-archive.com/debian-gcc@lists.debian.org/msg26197.html
   134    134   #
   135    135   # for more info.
   136    136   #
   137    137   GCOV_CFLAGS1 = -DSQLITE_COVERAGE_TEST=1 -fprofile-arcs -ftest-coverage
................................................................................
   421    421     $(TOP)/src/test_vfs.c \
   422    422     $(TOP)/src/test_windirent.c \
   423    423     $(TOP)/src/test_window.c \
   424    424     $(TOP)/src/test_wsd.c       \
   425    425     $(TOP)/ext/fts3/fts3_term.c \
   426    426     $(TOP)/ext/fts3/fts3_test.c  \
   427    427     $(TOP)/ext/session/test_session.c \
   428         -  $(TOP)/ext/rbu/test_rbu.c 
          428  +  $(TOP)/ext/rbu/test_rbu.c
   429    429   
   430    430   # Statically linked extensions
   431    431   #
   432    432   TESTSRC += \
   433    433     $(TOP)/ext/expert/sqlite3expert.c \
   434    434     $(TOP)/ext/expert/test_expert.c \
   435    435     $(TOP)/ext/misc/amatch.c \
................................................................................
   503    503     $(TOP)/ext/fts3/fts3_aux.c \
   504    504     $(TOP)/ext/fts3/fts3_expr.c \
   505    505     $(TOP)/ext/fts3/fts3_term.c \
   506    506     $(TOP)/ext/fts3/fts3_tokenizer.c \
   507    507     $(TOP)/ext/fts3/fts3_write.c \
   508    508     $(TOP)/ext/async/sqlite3async.c \
   509    509     $(TOP)/ext/session/sqlite3session.c \
   510         -  $(TOP)/ext/misc/stmt.c 
          510  +  $(TOP)/ext/misc/stmt.c
   511    511   
   512    512   # Header files used by all library source files.
   513    513   #
   514    514   HDR = \
   515    515      $(TOP)/src/btree.h \
   516    516      $(TOP)/src/btreeInt.h \
   517    517      $(TOP)/src/hash.h \
................................................................................
   600    600   SHELL_OPT += -DSQLITE_ENABLE_DESERIALIZE
   601    601   SHELL_OPT += -DSQLITE_INTROSPECTION_PRAGMAS
   602    602   FUZZERSHELL_OPT = -DSQLITE_ENABLE_JSON1
   603    603   FUZZCHECK_OPT = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MEMSYS5 -DSQLITE_OSS_FUZZ
   604    604   FUZZCHECK_OPT += -DSQLITE_MAX_MEMORY=50000000
   605    605   FUZZCHECK_OPT += -DSQLITE_PRINTF_PRECISION_LIMIT=1000
   606    606   FUZZCHECK_SRC = $(TOP)/test/fuzzcheck.c $(TOP)/test/ossfuzz.c
   607         -DBFUZZ_OPT = 
          607  +DBFUZZ_OPT =
   608    608   
   609    609   # This is the default Makefile target.  The objects listed here
   610    610   # are what get build when you type just "make" with no arguments.
   611    611   #
   612    612   all:	sqlite3.h libsqlite3.la sqlite3$(TEXE) $(HAVE_TCL:1=libtclsqlite3.la)
   613    613   
   614    614   Makefile: $(TOP)/Makefile.in
................................................................................
  1141   1141      fts5parse.c fts5parse.h \
  1142   1142      $(TOP)/ext/fts5/fts5_storage.c \
  1143   1143      $(TOP)/ext/fts5/fts5_tokenize.c \
  1144   1144      $(TOP)/ext/fts5/fts5_unicode2.c \
  1145   1145      $(TOP)/ext/fts5/fts5_varint.c \
  1146   1146      $(TOP)/ext/fts5/fts5_vocab.c  \
  1147   1147   
  1148         -fts5parse.c:	$(TOP)/ext/fts5/fts5parse.y lemon 
         1148  +fts5parse.c:	$(TOP)/ext/fts5/fts5parse.y lemon
  1149   1149   	cp $(TOP)/ext/fts5/fts5parse.y .
  1150   1150   	rm -f fts5parse.h
  1151   1151   	./lemon$(BEXE) $(OPTS) fts5parse.y
  1152   1152   
  1153   1153   fts5parse.h: fts5parse.c
  1154   1154   
  1155   1155   fts5.c: $(FTS5_SRC)
................................................................................
  1168   1168   # If using the amalgamation, use sqlite3.c directly to build the test
  1169   1169   # fixture.  Otherwise link against libsqlite3.la.  (This distinction is
  1170   1170   # necessary because the test fixture requires non-API symbols which are
  1171   1171   # hidden when the library is built via the amalgamation).
  1172   1172   #
  1173   1173   TESTFIXTURE_FLAGS  = -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1
  1174   1174   TESTFIXTURE_FLAGS += -DTCLSH_INIT_PROC=sqlite3TestInit
  1175         -TESTFIXTURE_FLAGS += -DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE 
         1175  +TESTFIXTURE_FLAGS += -DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE
  1176   1176   TESTFIXTURE_FLAGS += -DBUILD_sqlite
  1177   1177   TESTFIXTURE_FLAGS += -DSQLITE_SERIES_CONSTRAINT_VERIFY=1
  1178   1178   TESTFIXTURE_FLAGS += -DSQLITE_DEFAULT_PAGE_SIZE=1024
  1179   1179   TESTFIXTURE_FLAGS += -DSQLITE_ENABLE_STMTVTAB
  1180   1180   TESTFIXTURE_FLAGS += -DSQLITE_ENABLE_DBPAGE_VTAB
  1181   1181   
  1182   1182   TESTFIXTURE_SRC0 = $(TESTSRC2) libsqlite3.la
................................................................................
  1184   1184   TESTFIXTURE_SRC = $(TESTSRC) $(TOP)/src/tclsqlite.c
  1185   1185   TESTFIXTURE_SRC += $(TESTFIXTURE_SRC$(USE_AMALGAMATION))
  1186   1186   
  1187   1187   testfixture$(TEXE):	$(TESTFIXTURE_SRC)
  1188   1188   	$(LTLINK) -DSQLITE_NO_SYNC=1 $(TEMP_STORE) $(TESTFIXTURE_FLAGS) \
  1189   1189   		-o $@ $(TESTFIXTURE_SRC) $(LIBTCL) $(TLIBS)
  1190   1190   
         1191  +coretestprogs:	$(TESTPROGS)
         1192  +
         1193  +testprogs:	coretestprogs srcck1$(BEXE) fuzzcheck$(TEXE) sessionfuzz$(TEXE)
         1194  +
  1191   1195   # A very detailed test running most or all test cases
  1192   1196   fulltest:	$(TESTPROGS) fuzztest
  1193   1197   	./testfixture$(TEXE) $(TOP)/test/all.test $(TESTOPTS)
  1194   1198   
  1195   1199   # Really really long testing
  1196   1200   soaktest:	$(TESTPROGS)
  1197   1201   	./testfixture$(TEXE) $(TOP)/test/all.test -soak=1 $(TESTOPTS)
................................................................................
  1243   1247   
  1244   1248   sqlite3_analyzer.c: sqlite3.c $(TOP)/src/tclsqlite.c $(TOP)/tool/spaceanal.tcl $(TOP)/tool/mkccode.tcl $(TOP)/tool/sqlite3_analyzer.c.in
  1245   1249   	$(TCLSH_CMD) $(TOP)/tool/mkccode.tcl $(TOP)/tool/sqlite3_analyzer.c.in >sqlite3_analyzer.c
  1246   1250   
  1247   1251   sqlite3_analyzer$(TEXE): sqlite3_analyzer.c
  1248   1252   	$(LTLINK) sqlite3_analyzer.c -o $@ $(LIBTCL) $(TLIBS)
  1249   1253   
  1250         -sqltclsh.c: sqlite3.c $(TOP)/src/tclsqlite.c $(TOP)/tool/sqltclsh.tcl $(TOP)/ext/misc/appendvfs.c $(TOP)/tool/mkccode.tcl $(TOP)/tool/sqltclsh.c.in	
         1254  +sqltclsh.c: sqlite3.c $(TOP)/src/tclsqlite.c $(TOP)/tool/sqltclsh.tcl $(TOP)/ext/misc/appendvfs.c $(TOP)/tool/mkccode.tcl $(TOP)/tool/sqltclsh.c.in
  1251   1255   	$(TCLSH_CMD) $(TOP)/tool/mkccode.tcl $(TOP)/tool/sqltclsh.c.in >sqltclsh.c
  1252   1256   
  1253   1257   sqltclsh$(TEXE): sqltclsh.c
  1254   1258   	$(LTLINK) sqltclsh.c -o $@ $(LIBTCL) $(TLIBS)
  1255   1259   
  1256   1260   sqlite3_expert$(TEXE): $(TOP)/ext/expert/sqlite3expert.h $(TOP)/ext/expert/sqlite3expert.c $(TOP)/ext/expert/expert.c sqlite3.c
  1257   1261   	$(LTLINK)	$(TOP)/ext/expert/sqlite3expert.h $(TOP)/ext/expert/sqlite3expert.c $(TOP)/ext/expert/expert.c sqlite3.c -o sqlite3_expert $(TLIBS)
................................................................................
  1310   1314   	$(LTLINK) $(ST_OPT) -o $@ $(TOP)/test/speedtest1.c sqlite3.c $(TLIBS)
  1311   1315   
  1312   1316   KV_OPT += -DSQLITE_DIRECT_OVERFLOW_READ
  1313   1317   
  1314   1318   kvtest$(TEXE):	$(TOP)/test/kvtest.c sqlite3.c
  1315   1319   	$(LTLINK) $(KV_OPT) -o $@ $(TOP)/test/kvtest.c sqlite3.c $(TLIBS)
  1316   1320   
  1317         -rbu$(EXE): $(TOP)/ext/rbu/rbu.c $(TOP)/ext/rbu/sqlite3rbu.c sqlite3.lo 
         1321  +rbu$(EXE): $(TOP)/ext/rbu/rbu.c $(TOP)/ext/rbu/sqlite3rbu.c sqlite3.lo
  1318   1322   	$(LTLINK) -I. -o $@ $(TOP)/ext/rbu/rbu.c sqlite3.lo $(TLIBS)
  1319   1323   
  1320   1324   loadfts$(EXE): $(TOP)/tool/loadfts.c libsqlite3.la
  1321   1325   	$(LTLINK) $(TOP)/tool/loadfts.c libsqlite3.la -o $@ $(TLIBS)
  1322   1326   
  1323   1327   # This target will fail if the SQLite amalgamation contains any exported
  1324   1328   # symbols that do not begin with "sqlite3_". It is run as part of the
................................................................................
  1338   1342   
  1339   1343   snapshot-tarball: sqlite3.c
  1340   1344   	TOP=$(TOP) sh $(TOP)/tool/mkautoconfamal.sh --snapshot
  1341   1345   
  1342   1346   # The next two rules are used to support the "threadtest" target. Building
  1343   1347   # threadtest runs a few thread-safety tests that are implemented in C. This
  1344   1348   # target is invoked by the releasetest.tcl script.
  1345         -# 
         1349  +#
  1346   1350   THREADTEST3_SRC = $(TOP)/test/threadtest3.c    \
  1347   1351                     $(TOP)/test/tt3_checkpoint.c \
  1348   1352                     $(TOP)/test/tt3_index.c      \
  1349   1353                     $(TOP)/test/tt3_vacuum.c      \
  1350   1354                     $(TOP)/test/tt3_stress.c      \
  1351   1355                     $(TOP)/test/tt3_lookaside1.c
  1352   1356   
  1353   1357   threadtest3$(TEXE): sqlite3.lo $(THREADTEST3_SRC)
  1354   1358   	$(LTLINK) $(TOP)/test/threadtest3.c $(TOP)/src/test_multiplex.c sqlite3.lo -o $@ $(TLIBS)
  1355   1359   
  1356   1360   threadtest: threadtest3$(TEXE)
  1357   1361   	./threadtest3$(TEXE)
  1358   1362   
  1359         -releasetest:	
         1363  +releasetest:
  1360   1364   	$(TCLSH_CMD) $(TOP)/test/releasetest.tcl
  1361   1365   
  1362   1366   # Standard install and cleanup targets
  1363   1367   #
  1364   1368   lib_install:	libsqlite3.la
  1365   1369   	$(INSTALL) -d $(DESTDIR)$(libdir)
  1366   1370   	$(LTINSTALL) libsqlite3.la $(DESTDIR)$(libdir)
  1367         -	
         1371  +
  1368   1372   install:	sqlite3$(TEXE) lib_install sqlite3.h sqlite3.pc ${HAVE_TCL:1=tcl_install}
  1369   1373   	$(INSTALL) -d $(DESTDIR)$(bindir)
  1370   1374   	$(LTINSTALL) sqlite3$(TEXE) $(DESTDIR)$(bindir)
  1371   1375   	$(INSTALL) -d $(DESTDIR)$(includedir)
  1372   1376   	$(INSTALL) -m 0644 sqlite3.h $(DESTDIR)$(includedir)
  1373   1377   	$(INSTALL) -m 0644 $(TOP)/src/sqlite3ext.h $(DESTDIR)$(includedir)
  1374   1378   	$(INSTALL) -d $(DESTDIR)$(pkgconfigdir)
................................................................................
  1378   1382   	echo 'package ifneeded sqlite3 $(RELEASE) [list load $(TCLLIBDIR)/libtclsqlite3$(SHLIB_SUFFIX) sqlite3]' > $@
  1379   1383   tcl_install:	lib_install libtclsqlite3.la pkgIndex.tcl
  1380   1384   	$(INSTALL) -d $(DESTDIR)$(TCLLIBDIR)
  1381   1385   	$(LTINSTALL) libtclsqlite3.la $(DESTDIR)$(TCLLIBDIR)
  1382   1386   	rm -f $(DESTDIR)$(TCLLIBDIR)/libtclsqlite3.la $(DESTDIR)$(TCLLIBDIR)/libtclsqlite3.a
  1383   1387   	$(INSTALL) -m 0644 pkgIndex.tcl $(DESTDIR)$(TCLLIBDIR)
  1384   1388   
  1385         -clean:	
         1389  +clean:
  1386   1390   	rm -f *.lo *.la *.o sqlite3$(TEXE) libsqlite3.la
  1387   1391   	rm -f sqlite3.h opcodes.*
  1388   1392   	rm -rf .libs .deps
  1389   1393   	rm -f lemon$(BEXE) lempar.c parse.* sqlite*.tar.gz
  1390   1394   	rm -f mkkeywordhash$(BEXE) keywordhash.h
  1391   1395   	rm -f *.da *.bb *.bbg gmon.out
  1392   1396   	rm -rf tsrc .target_source

Changes to Makefile.msc.

  2333   2333   		$(TESTFIXTURE_SRC) \
  2334   2334   		/link $(LDFLAGS) $(LTLINKOPTS) $(TCLLIBPATHS) $(LTLIBPATHS) $(LIBRESOBJS) $(TCLLIBS) $(LTLIBS) $(TLIBS)
  2335   2335   
  2336   2336   extensiontest:	testfixture.exe testloadext.dll
  2337   2337   	@set PATH=$(LIBTCLPATH);$(PATH)
  2338   2338   	.\testfixture.exe $(TOP)\test\loadext.test $(TESTOPTS)
  2339   2339   
         2340  +coretestprogs:	$(TESTPROGS)
         2341  +
         2342  +testprogs:	coretestprogs srcck1.exe fuzzcheck.exe sessionfuzz.exe
         2343  +
  2340   2344   fulltest:	$(TESTPROGS) fuzztest
  2341   2345   	@set PATH=$(LIBTCLPATH);$(PATH)
  2342   2346   	.\testfixture.exe $(TOP)\test\all.test $(TESTOPTS)
  2343   2347   
  2344   2348   soaktest:	$(TESTPROGS)
  2345   2349   	@set PATH=$(LIBTCLPATH);$(PATH)
  2346   2350   	.\testfixture.exe $(TOP)\test\all.test -soak=1 $(TESTOPTS)

Changes to main.mk.

    15     15   # LIBS             Extra libraries options
    16     16   #
    17     17   # OPTS             Extra compiler command-line options.
    18     18   #
    19     19   # EXE              The suffix to add to executable files.  ".exe" for windows
    20     20   #                  and "" for Unix.
    21     21   #
    22         -# TCC              C Compiler and options for use in building executables that 
           22  +# TCC              C Compiler and options for use in building executables that
    23     23   #                  will run on the target platform.  This is usually the same
    24     24   #                  as BCC, unless you are cross-compiling.
    25     25   #
    26     26   # AR               Tools used to build a static library.
    27     27   # RANLIB
    28     28   #
    29     29   # TCL_FLAGS        Extra compiler options needed for programs that use the
................................................................................
    39     39   #
    40     40   # Once the macros above are defined, the rest of this make script will
    41     41   # build the SQLite library and testing tools.
    42     42   ################################################################################
    43     43   
    44     44   # This is how we compile
    45     45   #
    46         -TCCX =  $(TCC) $(OPTS) -I. -I$(TOP)/src -I$(TOP) 
           46  +TCCX =  $(TCC) $(OPTS) -I. -I$(TOP)/src -I$(TOP)
    47     47   TCCX += -I$(TOP)/ext/rtree -I$(TOP)/ext/icu -I$(TOP)/ext/fts3
    48     48   TCCX += -I$(TOP)/ext/async -I$(TOP)/ext/userauth
    49     49   TCCX += -I$(TOP)/ext/session
    50     50   TCCX += -I$(TOP)/ext/fts5
    51     51   THREADLIB += $(LIBS)
    52     52   
    53     53   # Object files for the SQLite library.
................................................................................
   232    232     $(TOP)/ext/rtree/rtree.c \
   233    233     $(TOP)/ext/rtree/geopoly.c
   234    234   SRC += \
   235    235     $(TOP)/ext/session/sqlite3session.c \
   236    236     $(TOP)/ext/session/sqlite3session.h
   237    237   SRC += \
   238    238     $(TOP)/ext/userauth/userauth.c \
   239         -  $(TOP)/ext/userauth/sqlite3userauth.h 
          239  +  $(TOP)/ext/userauth/sqlite3userauth.h
   240    240   SRC += \
   241    241     $(TOP)/ext/rbu/sqlite3rbu.c \
   242    242     $(TOP)/ext/rbu/sqlite3rbu.h
   243    243   SRC += \
   244    244     $(TOP)/ext/misc/json1.c \
   245    245     $(TOP)/ext/misc/stmt.c
   246    246   
................................................................................
   247    247   
   248    248   # FTS5 things
   249    249   #
   250    250   FTS5_HDR = \
   251    251      $(TOP)/ext/fts5/fts5.h \
   252    252      $(TOP)/ext/fts5/fts5Int.h \
   253    253      fts5parse.h
   254         -	   
          254  +
   255    255   FTS5_SRC = \
   256    256      $(TOP)/ext/fts5/fts5_aux.c \
   257    257      $(TOP)/ext/fts5/fts5_buffer.c \
   258    258      $(TOP)/ext/fts5/fts5_main.c \
   259    259      $(TOP)/ext/fts5/fts5_config.c \
   260    260      $(TOP)/ext/fts5/fts5_expr.c \
   261    261      $(TOP)/ext/fts5/fts5_hash.c \
................................................................................
   427    427     $(TOP)/ext/fts3/fts3_aux.c \
   428    428     $(TOP)/ext/fts3/fts3_expr.c \
   429    429     $(TOP)/ext/fts3/fts3_tokenizer.c \
   430    430     $(TOP)/ext/fts3/fts3_write.c \
   431    431     $(TOP)/ext/async/sqlite3async.c \
   432    432     $(TOP)/ext/misc/stmt.c \
   433    433     $(TOP)/ext/session/sqlite3session.c \
   434         -  $(TOP)/ext/session/test_session.c 
          434  +  $(TOP)/ext/session/test_session.c
   435    435   
   436    436   # Header files used by all library source files.
   437    437   #
   438    438   HDR = \
   439    439      $(TOP)/src/btree.h \
   440    440      $(TOP)/src/btreeInt.h \
   441    441      $(TOP)/src/hash.h \
................................................................................
   480    480     $(TOP)/ext/rtree/rtree.h \
   481    481     $(TOP)/ext/rtree/geopoly.c
   482    482   EXTHDR += \
   483    483     $(TOP)/ext/icu/sqliteicu.h
   484    484   EXTHDR += \
   485    485     $(TOP)/ext/fts5/fts5Int.h  \
   486    486     fts5parse.h                \
   487         -  $(TOP)/ext/fts5/fts5.h 
          487  +  $(TOP)/ext/fts5/fts5.h
   488    488   EXTHDR += \
   489    489     $(TOP)/ext/userauth/sqlite3userauth.h
   490    490   
   491    491   # executables needed for testing
   492    492   #
   493    493   TESTPROGS = \
   494    494     testfixture$(EXE) \
................................................................................
   797    797   	$(TCCX) -DSQLITE_CORE -c $(TOP)/ext/misc/stmt.c
   798    798   
   799    799   rtree.o:	$(TOP)/ext/rtree/rtree.c $(HDR) $(EXTHDR)
   800    800   	$(TCCX) -DSQLITE_CORE -c $(TOP)/ext/rtree/rtree.c
   801    801   
   802    802   
   803    803   
   804         -fts5parse.c:	$(TOP)/ext/fts5/fts5parse.y lemon 
          804  +fts5parse.c:	$(TOP)/ext/fts5/fts5parse.y lemon
   805    805   	cp $(TOP)/ext/fts5/fts5parse.y .
   806    806   	rm -f fts5parse.h
   807    807   	./lemon $(OPTS) fts5parse.y
   808    808   
   809    809   fts5parse.h: fts5parse.c
   810    810   
   811    811   fts5.c: $(FTS5_SRC) $(FTS5_HDR)
................................................................................
   831    831   	$(TCCX) $(TCL_FLAGS) -DTCLSH -o tclsqlite3 \
   832    832   		$(TOP)/src/tclsqlite.c libsqlite3.a $(LIBTCL) $(THREADLIB)
   833    833   
   834    834   sqlite3_analyzer.c: sqlite3.c $(TOP)/src/tclsqlite.c $(TOP)/tool/spaceanal.tcl $(TOP)/tool/sqlite3_analyzer.c.in $(TOP)/tool/mkccode.tcl
   835    835   	tclsh $(TOP)/tool/mkccode.tcl $(TOP)/tool/sqlite3_analyzer.c.in >sqlite3_analyzer.c
   836    836   
   837    837   sqlite3_analyzer$(EXE): sqlite3_analyzer.c
   838         -	$(TCCX) $(TCL_FLAGS) sqlite3_analyzer.c -o $@ $(LIBTCL) $(THREADLIB) 
          838  +	$(TCCX) $(TCL_FLAGS) sqlite3_analyzer.c -o $@ $(LIBTCL) $(THREADLIB)
   839    839   
   840    840   sqltclsh.c: sqlite3.c $(TOP)/src/tclsqlite.c $(TOP)/tool/sqltclsh.tcl $(TOP)/ext/misc/appendvfs.c $(TOP)/tool/mkccode.tcl
   841    841   	tclsh $(TOP)/tool/mkccode.tcl $(TOP)/tool/sqltclsh.c.in >sqltclsh.c
   842    842   
   843    843   sqltclsh$(EXE): sqltclsh.c
   844         -	$(TCCX) $(TCL_FLAGS) sqltclsh.c -o $@ $(LIBTCL) $(THREADLIB) 
          844  +	$(TCCX) $(TCL_FLAGS) sqltclsh.c -o $@ $(LIBTCL) $(THREADLIB)
   845    845   
   846    846   sqlite3_expert$(EXE): $(TOP)/ext/expert/sqlite3expert.h $(TOP)/ext/expert/sqlite3expert.c $(TOP)/ext/expert/expert.c sqlite3.c
   847    847   	$(TCCX) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION $(TOP)/ext/expert/sqlite3expert.c $(TOP)/ext/expert/expert.c sqlite3.c -o sqlite3_expert$(EXE) $(THREADLIB)
   848    848   
   849    849   CHECKER_DEPS =\
   850    850     $(TOP)/tool/mkccode.tcl \
   851    851     sqlite3.c \
................................................................................
   890    890   
   891    891   fts3-testfixture$(EXE): sqlite3.c fts3amal.c $(TESTSRC) $(TOP)/src/tclsqlite.c
   892    892   	$(TCCX) $(TCL_FLAGS) $(TESTFIXTURE_FLAGS)                            \
   893    893   	-DSQLITE_ENABLE_FTS3=1                                               \
   894    894   		$(TESTSRC) $(TOP)/src/tclsqlite.c sqlite3.c fts3amal.c       \
   895    895   		-o testfixture$(EXE) $(LIBTCL) $(THREADLIB)
   896    896   
          897  +coretestprogs:	$(TESTPROGS)
          898  +
          899  +testprogs:	coretestprogs srcck1$(EXE) fuzzcheck$(EXE) sessionfuzz$(EXE)
          900  +
   897    901   fulltest:	$(TESTPROGS) fuzztest
   898    902   	./testfixture$(EXE) $(TOP)/test/all.test $(TESTOPTS)
   899    903   
   900    904   soaktest:	$(TESTPROGS)
   901    905   	./testfixture$(EXE) $(TOP)/test/all.test -soak=1 $(TESTOPTS)
   902    906   
   903    907   fulltestonly:	$(TESTPROGS) fuzztest
................................................................................
   946    950   #
   947    951   smoketest:	$(TESTPROGS) fuzzcheck$(EXE)
   948    952   	./testfixture$(EXE) $(TOP)/test/main.test $(TESTOPTS)
   949    953   
   950    954   # The next two rules are used to support the "threadtest" target. Building
   951    955   # threadtest runs a few thread-safety tests that are implemented in C. This
   952    956   # target is invoked by the releasetest.tcl script.
   953         -# 
          957  +#
   954    958   THREADTEST3_SRC = $(TOP)/test/threadtest3.c    \
   955    959                     $(TOP)/test/tt3_checkpoint.c \
   956    960                     $(TOP)/test/tt3_index.c      \
   957    961                     $(TOP)/test/tt3_vacuum.c      \
   958    962                     $(TOP)/test/tt3_stress.c      \
   959    963                     $(TOP)/test/tt3_lookaside1.c
   960    964   
................................................................................
  1010   1014   	$(TCC) -o LogEst$(EXE) $(TOP)/tool/logest.c
  1011   1015   
  1012   1016   wordcount$(EXE):	$(TOP)/test/wordcount.c sqlite3.c
  1013   1017   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o wordcount$(EXE) \
  1014   1018   		$(TOP)/test/wordcount.c sqlite3.c
  1015   1019   
  1016   1020   speedtest1$(EXE):	$(TOP)/test/speedtest1.c sqlite3.c
  1017         -	$(TCCX) -I. $(ST_OPT) -o speedtest1$(EXE) $(TOP)/test/speedtest1.c sqlite3.c $(THREADLIB) 
         1021  +	$(TCCX) -I. $(ST_OPT) -o speedtest1$(EXE) $(TOP)/test/speedtest1.c sqlite3.c $(THREADLIB)
  1018   1022   
  1019   1023   kvtest$(EXE):	$(TOP)/test/kvtest.c sqlite3.c
  1020         -	$(TCCX) -I. $(KV_OPT) -o kvtest$(EXE) $(TOP)/test/kvtest.c sqlite3.c $(THREADLIB) 
         1024  +	$(TCCX) -I. $(KV_OPT) -o kvtest$(EXE) $(TOP)/test/kvtest.c sqlite3.c $(THREADLIB)
  1021   1025   
  1022         -rbu$(EXE): $(TOP)/ext/rbu/rbu.c $(TOP)/ext/rbu/sqlite3rbu.c sqlite3.o 
         1026  +rbu$(EXE): $(TOP)/ext/rbu/rbu.c $(TOP)/ext/rbu/sqlite3rbu.c sqlite3.o
  1023   1027   	$(TCC) -I. -o rbu$(EXE) $(TOP)/ext/rbu/rbu.c sqlite3.o \
  1024   1028   	  $(THREADLIB)
  1025   1029   
  1026   1030   loadfts: $(TOP)/tool/loadfts.c libsqlite3.a
  1027   1031   	$(TCC) $(TOP)/tool/loadfts.c libsqlite3.a -o loadfts $(THREADLIB)
  1028   1032   
  1029   1033   # This target will fail if the SQLite amalgamation contains any exported
................................................................................
  1047   1051   # Standard install and cleanup targets
  1048   1052   #
  1049   1053   install:	sqlite3 libsqlite3.a sqlite3.h
  1050   1054   	mv sqlite3 /usr/bin
  1051   1055   	mv libsqlite3.a /usr/lib
  1052   1056   	mv sqlite3.h /usr/include
  1053   1057   
  1054         -clean:	
         1058  +clean:
  1055   1059   	rm -f *.o sqlite3 sqlite3.exe libsqlite3.a sqlite3.h opcodes.*
  1056   1060   	rm -f lemon lemon.exe lempar.c parse.* sqlite*.tar.gz
  1057   1061   	rm -f mkkeywordhash mkkeywordhash.exe keywordhash.h
  1058   1062   	rm -f $(PUBLISH)
  1059   1063   	rm -f *.da *.bb *.bbg gmon.out
  1060   1064   	rm -rf tsrc target_source
  1061   1065   	rm -f testloadext.dll libtestloadext.so
................................................................................
  1075   1079   	rm -f wordcount wordcount.exe
  1076   1080   	rm -f rbu rbu.exe
  1077   1081   	rm -f srcck1 srcck1.exe
  1078   1082   	rm -f sqlite3.c sqlite3-*.c fts?amal.c tclsqlite3.c
  1079   1083   	rm -f sqlite3rc.h
  1080   1084   	rm -f shell.c sqlite3ext.h
  1081   1085   	rm -f sqlite3_analyzer sqlite3_analyzer.exe sqlite3_analyzer.c
  1082         -	rm -f sqlite3_expert sqlite3_expert.exe 
         1086  +	rm -f sqlite3_expert sqlite3_expert.exe
  1083   1087   	rm -f sqlite-*-output.vsix
  1084   1088   	rm -f mptester mptester.exe
  1085   1089   	rm -f fuzzershell fuzzershell.exe
  1086   1090   	rm -f fuzzcheck fuzzcheck.exe
  1087   1091   	rm -f sessionfuzz
  1088   1092   	rm -f sqldiff sqldiff.exe
  1089   1093   	rm -f fts5.* fts5parse.*
  1090   1094   	rm -f lsm.h lsm1.c

Changes to src/build.c.

   629    629     }
   630    630   
   631    631     /* Delete any foreign keys attached to this table. */
   632    632     sqlite3FkDelete(db, pTable);
   633    633   
   634    634     /* Delete the Table structure itself.
   635    635     */
          636  +#ifdef SQLITE_ENABLE_NORMALIZE
          637  +  if( pTable->pColHash ){
          638  +    sqlite3HashClear(pTable->pColHash);
          639  +    sqlite3_free(pTable->pColHash);
          640  +  }
          641  +#endif
   636    642     sqlite3DeleteColumnNames(db, pTable);
   637    643     sqlite3DbFree(db, pTable->zName);
   638    644     sqlite3DbFree(db, pTable->zColAff);
   639    645     sqlite3SelectDelete(db, pTable->pSelect);
   640    646     sqlite3ExprListDelete(db, pTable->pCheck);
   641    647   #ifndef SQLITE_OMIT_VIRTUALTABLE
   642    648     sqlite3VtabClear(db, pTable);

Changes to src/callback.c.

   291    291     for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
   292    292       if( sqlite3StrICmp(p->zName, zFunc)==0 ){
   293    293         return p;
   294    294       }
   295    295     }
   296    296     return 0;
   297    297   }
          298  +#ifdef SQLITE_ENABLE_NORMALIZE
          299  +FuncDef *sqlite3FunctionSearchN(
          300  +  int h,               /* Hash of the name */
          301  +  const char *zFunc,   /* Name of function */
          302  +  int nFunc            /* Length of the name */
          303  +){
          304  +  FuncDef *p;
          305  +  for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
          306  +    if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 ){
          307  +      return p;
          308  +    }
          309  +  }
          310  +  return 0;
          311  +}
          312  +#endif /* SQLITE_ENABLE_NORMALIZE */
   298    313   
   299    314   /*
   300    315   ** Insert a new FuncDef into a FuncDefHash hash table.
   301    316   */
   302    317   void sqlite3InsertBuiltinFuncs(
   303    318     FuncDef *aDef,      /* List of global functions to be inserted */
   304    319     int nDef            /* Length of the apDef[] list */
   305    320   ){
   306    321     int i;
   307    322     for(i=0; i<nDef; i++){
   308    323       FuncDef *pOther;
   309    324       const char *zName = aDef[i].zName;
   310    325       int nName = sqlite3Strlen30(zName);
   311         -    int h = (zName[0] + nName) % SQLITE_FUNC_HASH_SZ;
          326  +    int h = SQLITE_FUNC_HASH(zName[0], nName);
   312    327       assert( zName[0]>='a' && zName[0]<='z' );
   313    328       pOther = functionSearch(h, zName);
   314    329       if( pOther ){
   315    330         assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
   316    331         aDef[i].pNext = pOther->pNext;
   317    332         pOther->pNext = &aDef[i];
   318    333       }else{
................................................................................
   383    398     ** install a new function.  Whatever FuncDef structure is returned it will
   384    399     ** have fields overwritten with new information appropriate for the
   385    400     ** new function.  But the FuncDefs for built-in functions are read-only.
   386    401     ** So we must not search for built-ins when creating a new function.
   387    402     */ 
   388    403     if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
   389    404       bestScore = 0;
   390         -    h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % SQLITE_FUNC_HASH_SZ;
          405  +    h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName);
   391    406       p = functionSearch(h, zName);
   392    407       while( p ){
   393    408         int score = matchQuality(p, nArg, enc);
   394    409         if( score>bestScore ){
   395    410           pBest = p;
   396    411           bestScore = score;
   397    412         }

Changes to src/ctime.c.

   263    263     "ENABLE_MEMSYS3",
   264    264   #endif
   265    265   #if SQLITE_ENABLE_MEMSYS5
   266    266     "ENABLE_MEMSYS5",
   267    267   #endif
   268    268   #if SQLITE_ENABLE_MULTIPLEX
   269    269     "ENABLE_MULTIPLEX",
          270  +#endif
          271  +#if SQLITE_ENABLE_NORMALIZE
          272  +  "ENABLE_NORMALIZE",
   270    273   #endif
   271    274   #if SQLITE_ENABLE_NULL_TRIM
   272    275     "ENABLE_NULL_TRIM",
   273    276   #endif
   274    277   #if SQLITE_ENABLE_OVERSIZE_CELL_CHECK
   275    278     "ENABLE_OVERSIZE_CELL_CHECK",
   276    279   #endif

Changes to src/expr.c.

  2145   2145   */
  2146   2146   int sqlite3IsRowid(const char *z){
  2147   2147     if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
  2148   2148     if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
  2149   2149     if( sqlite3StrICmp(z, "OID")==0 ) return 1;
  2150   2150     return 0;
  2151   2151   }
         2152  +#ifdef SQLITE_ENABLE_NORMALIZE
         2153  +int sqlite3IsRowidN(const char *z, int n){
         2154  +  if( sqlite3StrNICmp(z, "_ROWID_", n)==0 ) return 1;
         2155  +  if( sqlite3StrNICmp(z, "ROWID", n)==0 ) return 1;
         2156  +  if( sqlite3StrNICmp(z, "OID", n)==0 ) return 1;
         2157  +  return 0;
         2158  +}
         2159  +#endif
  2152   2160   
  2153   2161   /*
  2154   2162   ** pX is the RHS of an IN operator.  If pX is a SELECT statement 
  2155   2163   ** that can be simplified to a direct table access, then return
  2156   2164   ** a pointer to the SELECT statement.  If pX is not a SELECT statement,
  2157   2165   ** or if the SELECT statement needs to be manifested into a transient
  2158   2166   ** table, then return NULL.

Changes to src/hash.c.

    60     60       ** 0x9e3779b1 is 2654435761 which is the closest prime number to
    61     61       ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
    62     62       h += sqlite3UpperToLower[c];
    63     63       h *= 0x9e3779b1;
    64     64     }
    65     65     return h;
    66     66   }
           67  +#ifdef SQLITE_ENABLE_NORMALIZE
           68  +static unsigned int strHashN(const char *z, int n){
           69  +  unsigned int h = 0;
           70  +  int i;
           71  +  for(i=0; i<n; i++){
           72  +    /* Knuth multiplicative hashing.  (Sorting & Searching, p. 510).
           73  +    ** 0x9e3779b1 is 2654435761 which is the closest prime number to
           74  +    ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
           75  +    h += sqlite3UpperToLower[z[i]];
           76  +    h *= 0x9e3779b1;
           77  +  }
           78  +  return h;
           79  +}
           80  +#endif /* SQLITE_ENABLE_NORMALIZE */
    67     81   
    68     82   
    69     83   /* Link pNew element into the hash table pH.  If pEntry!=0 then also
    70     84   ** insert pNew into the pEntry hash bucket.
    71     85   */
    72     86   static void insertElement(
    73     87     Hash *pH,              /* The complete hash table */
................................................................................
   171    185       if( sqlite3StrICmp(elem->pKey,pKey)==0 ){ 
   172    186         return elem;
   173    187       }
   174    188       elem = elem->next;
   175    189     }
   176    190     return &nullElement;
   177    191   }
          192  +#ifdef SQLITE_ENABLE_NORMALIZE
          193  +static HashElem *findElementWithHashN(
          194  +  const Hash *pH,     /* The pH to be searched */
          195  +  const char *pKey,   /* The key we are searching for */
          196  +  int nKey,           /* Number of key bytes to use */
          197  +  unsigned int *pHash /* Write the hash value here */
          198  +){
          199  +  HashElem *elem;                /* Used to loop thru the element list */
          200  +  int count;                     /* Number of elements left to test */
          201  +  unsigned int h;                /* The computed hash */
          202  +  static HashElem nullElement = { 0, 0, 0, 0 };
          203  +
          204  +  if( pH->ht ){   /*OPTIMIZATION-IF-TRUE*/
          205  +    struct _ht *pEntry;
          206  +    h = strHashN(pKey, nKey) % pH->htsize;
          207  +    pEntry = &pH->ht[h];
          208  +    elem = pEntry->chain;
          209  +    count = pEntry->count;
          210  +  }else{
          211  +    h = 0;
          212  +    elem = pH->first;
          213  +    count = pH->count;
          214  +  }
          215  +  if( pHash ) *pHash = h;
          216  +  while( count-- ){
          217  +    assert( elem!=0 );
          218  +    if( sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){ 
          219  +      return elem;
          220  +    }
          221  +    elem = elem->next;
          222  +  }
          223  +  return &nullElement;
          224  +}
          225  +#endif /* SQLITE_ENABLE_NORMALIZE */
   178    226   
   179    227   /* Remove a single entry from the hash table given a pointer to that
   180    228   ** element and a hash on the element's key.
   181    229   */
   182    230   static void removeElementGivenHash(
   183    231     Hash *pH,         /* The pH containing "elem" */
   184    232     HashElem* elem,   /* The element to be removed from the pH */
................................................................................
   215    263   ** found, or NULL if there is no match.
   216    264   */
   217    265   void *sqlite3HashFind(const Hash *pH, const char *pKey){
   218    266     assert( pH!=0 );
   219    267     assert( pKey!=0 );
   220    268     return findElementWithHash(pH, pKey, 0)->data;
   221    269   }
          270  +#ifdef SQLITE_ENABLE_NORMALIZE
          271  +void *sqlite3HashFindN(const Hash *pH, const char *pKey, int nKey){
          272  +  assert( pH!=0 );
          273  +  assert( pKey!=0 );
          274  +  assert( nKey>=0 );
          275  +  return findElementWithHashN(pH, pKey, nKey, 0)->data;
          276  +}
          277  +#endif /* SQLITE_ENABLE_NORMALIZE */
   222    278   
   223    279   /* Insert an element into the hash table pH.  The key is pKey
   224    280   ** and the data is "data".
   225    281   **
   226    282   ** If no element exists with a matching key, then a new
   227    283   ** element is created and NULL is returned.
   228    284   **

Changes to src/loadext.c.

   447    447     sqlite3_str_appendall,
   448    448     sqlite3_str_appendchar,
   449    449     sqlite3_str_reset,
   450    450     sqlite3_str_errcode,
   451    451     sqlite3_str_length,
   452    452     sqlite3_str_value,
   453    453     /* Version 3.25.0 and later */
   454         -  sqlite3_create_window_function
          454  +  sqlite3_create_window_function,
          455  +  /* Version 3.26.0 and later */
          456  +#ifdef SQLITE_ENABLE_NORMALIZE
          457  +  sqlite3_normalized_sql
          458  +#else
          459  +  0
          460  +#endif
   455    461   };
   456    462   
   457    463   /*
   458    464   ** Attempt to load an SQLite extension library contained in the file
   459    465   ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
   460    466   ** default entry point name (sqlite3_extension_init) is used.  Use
   461    467   ** of the default name is recommended.

Changes to src/prepare.c.

   705    705     sqlite3BtreeLeaveAll(db);
   706    706     rc = sqlite3ApiExit(db, rc);
   707    707     assert( (rc&db->errMask)==rc );
   708    708     sqlite3_mutex_leave(db->mutex);
   709    709     return rc;
   710    710   }
   711    711   
          712  +#ifdef SQLITE_ENABLE_NORMALIZE
          713  +/*
          714  +** Checks if the specified token is a table, column, or function name,
          715  +** based on the databases associated with the statement being prepared.
          716  +** If the function fails, zero is returned and pRc is filled with the
          717  +** error code.
          718  +*/
          719  +static int shouldTreatAsIdentifier(
          720  +  sqlite3 *db,        /* Database handle. */
          721  +  const char *zToken, /* Pointer to start of token to be checked */
          722  +  int nToken,         /* Length of token to be checked */
          723  +  int *pRc            /* Pointer to error code upon failure */
          724  +){
          725  +  int bFound = 0;     /* Non-zero if token is an identifier name. */
          726  +  int i, j;           /* Database and column loop indexes. */
          727  +  Schema *pSchema;    /* Schema for current database. */
          728  +  Hash *pHash;        /* Hash table of tables for current database. */
          729  +  HashElem *e;        /* Hash element for hash table iteration. */
          730  +  Table *pTab;        /* Database table for columns being checked. */
          731  +
          732  +  if( sqlite3IsRowidN(zToken, nToken) ){
          733  +    return 1;
          734  +  }
          735  +  if( nToken>0 ){
          736  +    int hash = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zToken[0]], nToken);
          737  +    if( sqlite3FunctionSearchN(hash, zToken, nToken) ) return 1;
          738  +  }
          739  +  assert( db!=0 );
          740  +  sqlite3_mutex_enter(db->mutex);
          741  +  sqlite3BtreeEnterAll(db);
          742  +  for(i=0; i<db->nDb; i++){
          743  +    pHash = &db->aFunc;
          744  +    if( sqlite3HashFindN(pHash, zToken, nToken) ){
          745  +      bFound = 1;
          746  +      break;
          747  +    }
          748  +    pSchema = db->aDb[i].pSchema;
          749  +    if( pSchema==0 ) continue;
          750  +    pHash = &pSchema->tblHash;
          751  +    if( sqlite3HashFindN(pHash, zToken, nToken) ){
          752  +      bFound = 1;
          753  +      break;
          754  +    }
          755  +    for(e=sqliteHashFirst(pHash); e; e=sqliteHashNext(e)){
          756  +      pTab = sqliteHashData(e);
          757  +      if( pTab==0 ) continue;
          758  +      pHash = pTab->pColHash;
          759  +      if( pHash==0 ){
          760  +        pTab->pColHash = pHash = sqlite3_malloc(sizeof(Hash));
          761  +        if( pHash ){
          762  +          sqlite3HashInit(pHash);
          763  +          for(j=0; j<pTab->nCol; j++){
          764  +            Column *pCol = &pTab->aCol[j];
          765  +            sqlite3HashInsert(pHash, pCol->zName, pCol);
          766  +          }
          767  +        }else{
          768  +          *pRc = SQLITE_NOMEM_BKPT;
          769  +          bFound = 0;
          770  +          goto done;
          771  +        }
          772  +      }
          773  +      if( pHash && sqlite3HashFindN(pHash, zToken, nToken) ){
          774  +        bFound = 1;
          775  +        goto done;
          776  +      }
          777  +    }
          778  +  }
          779  +done:
          780  +  sqlite3BtreeLeaveAll(db);
          781  +  sqlite3_mutex_leave(db->mutex);
          782  +  return bFound;
          783  +}
          784  +
          785  +/*
          786  +** Attempt to estimate the final output buffer size needed for the fully
          787  +** normalized version of the specified SQL string.  This should take into
          788  +** account any potential expansion that could occur (e.g. via IN clauses
          789  +** being expanded, etc).  This size returned is the total number of bytes
          790  +** including the NUL terminator.
          791  +*/
          792  +static int estimateNormalizedSize(
          793  +  const char *zSql, /* The original SQL string */
          794  +  int nSql,         /* Length of original SQL string */
          795  +  u8 prepFlags      /* The flags passed to sqlite3_prepare_v3() */
          796  +){
          797  +  int nOut = nSql + 4;
          798  +  const char *z = zSql;
          799  +  while( nOut<nSql*5 ){
          800  +    while( z[0]!=0 && z[0]!='I' && z[0]!='i' ){ z++; }
          801  +    if( z[0]==0 ) break;
          802  +    z++;
          803  +    if( z[0]!='N' && z[0]!='n' ) break;
          804  +    z++;
          805  +    while( sqlite3Isspace(z[0]) ){ z++; }
          806  +    if( z[0]!='(' ) break;
          807  +    z++;
          808  +    nOut += 5; /* ?,?,? */
          809  +  }
          810  +  return nOut;
          811  +}
          812  +
          813  +/*
          814  +** Copy the current token into the output buffer while dealing with quoted
          815  +** identifiers.  By default, all letters will be converted into lowercase.
          816  +** If the bUpper flag is set, uppercase will be used.  The piOut argument
          817  +** will be used to update the target index into the output string.
          818  +*/
          819  +static void copyNormalizedToken(
          820  +  const char *zSql, /* The original SQL string */
          821  +  int iIn,          /* Current index into the original SQL string */
          822  +  int nToken,       /* Number of bytes in the current token */
          823  +  int tokenFlags,   /* Flags returned by the tokenizer */
          824  +  char *zOut,       /* The output string */
          825  +  int *piOut        /* Pointer to target index into the output string */
          826  +){
          827  +  int bQuoted = tokenFlags & SQLITE_TOKEN_QUOTED;
          828  +  int bKeyword = tokenFlags & SQLITE_TOKEN_KEYWORD;
          829  +  int j = *piOut, k = 0;
          830  +  for(; k<nToken; k++){
          831  +    if( bQuoted ){
          832  +      if( k==0 && iIn>0 ){
          833  +        zOut[j++] = '"';
          834  +        continue;
          835  +      }else if( k==nToken-1 ){
          836  +        zOut[j++] = '"';
          837  +        continue;
          838  +      }
          839  +    }
          840  +    if( bKeyword ){
          841  +      zOut[j++] = sqlite3Toupper(zSql[iIn+k]);
          842  +    }else{
          843  +      zOut[j++] = sqlite3Tolower(zSql[iIn+k]);
          844  +    }
          845  +  }
          846  +  *piOut = j;
          847  +}
          848  +
          849  +/*
          850  +** Perform normalization of the SQL contained in the prepared statement and
          851  +** store the result in the zNormSql field.  The schema for the associated
          852  +** databases are consulted while performing the normalization in order to
          853  +** determine if a token appears to be an identifier.  All identifiers are
          854  +** left intact in the normalized SQL and all literals are replaced with a
          855  +** single '?'.
          856  +*/
          857  +void sqlite3Normalize(
          858  +  Vdbe *pVdbe,      /* VM being reprepared */
          859  +  const char *zSql, /* The original SQL string */
          860  +  int nSql,         /* Size of the input string in bytes */
          861  +  u8 prepFlags      /* The flags passed to sqlite3_prepare_v3() */
          862  +){
          863  +  sqlite3 *db;           /* Database handle. */
          864  +  char *z;               /* The output string */
          865  +  int nZ;                /* Size of the output string in bytes */
          866  +  int i;                 /* Next character to read from zSql[] */
          867  +  int j;                 /* Next character to fill in on z[] */
          868  +  int tokenType = 0;     /* Type of the next token */
          869  +  int prevTokenType = 0; /* Type of the previous token, except spaces */
          870  +  int n;                 /* Size of the next token */
          871  +  int nParen = 0;        /* Nesting level of parenthesis */
          872  +  Hash inHash;           /* Table of parenthesis levels to output index. */
          873  +
          874  +  db = sqlite3VdbeDb(pVdbe);
          875  +  assert( db!=0 );
          876  +  assert( pVdbe->zNormSql==0 );
          877  +  if( zSql==0 ) return;
          878  +  nZ = estimateNormalizedSize(zSql, nSql, prepFlags);
          879  +  z = sqlite3DbMallocRawNN(db, nZ);
          880  +  if( z==0 ) return;
          881  +  sqlite3HashInit(&inHash);
          882  +  for(i=j=0; i<nSql && zSql[i]; i+=n){
          883  +    int flags = 0;
          884  +    if( tokenType!=TK_SPACE ) prevTokenType = tokenType;
          885  +    n = sqlite3GetTokenNormalized((unsigned char*)zSql+i, &tokenType, &flags);
          886  +    switch( tokenType ){
          887  +      case TK_SPACE: {
          888  +        break;
          889  +      }
          890  +      case TK_ILLEGAL: {
          891  +        sqlite3DbFree(db, z);
          892  +        sqlite3HashClear(&inHash);
          893  +        return;
          894  +      }
          895  +      case TK_STRING:
          896  +      case TK_INTEGER:
          897  +      case TK_FLOAT:
          898  +      case TK_VARIABLE:
          899  +      case TK_BLOB: {
          900  +        z[j++] = '?';
          901  +        break;
          902  +      }
          903  +      case TK_LP:
          904  +      case TK_RP: {
          905  +        if( tokenType==TK_LP ){
          906  +          nParen++;
          907  +          if( prevTokenType==TK_IN ){
          908  +            assert( nParen<nSql );
          909  +            sqlite3HashInsert(&inHash, zSql+nParen, SQLITE_INT_TO_PTR(j));
          910  +          }
          911  +        }else{
          912  +          int jj;
          913  +          assert( nParen<nSql );
          914  +          jj = SQLITE_PTR_TO_INT(sqlite3HashFind(&inHash, zSql+nParen));
          915  +          if( jj>0 ){
          916  +            sqlite3HashInsert(&inHash, zSql+nParen, 0);
          917  +            assert( jj+6<nZ );
          918  +            memcpy(z+jj+1, "?,?,?", 5);
          919  +            j = jj+6;
          920  +            assert( nZ-1-j>=0 );
          921  +            assert( nZ-1-j<nZ );
          922  +            memset(z+j, 0, nZ-1-j);
          923  +          }
          924  +          nParen--;
          925  +        }
          926  +        assert( nParen>=0 );
          927  +        /* Fall through */
          928  +      }
          929  +      case TK_MINUS:
          930  +      case TK_SEMI:
          931  +      case TK_PLUS:
          932  +      case TK_STAR:
          933  +      case TK_SLASH:
          934  +      case TK_REM:
          935  +      case TK_EQ:
          936  +      case TK_LE:
          937  +      case TK_NE:
          938  +      case TK_LSHIFT:
          939  +      case TK_LT:
          940  +      case TK_RSHIFT:
          941  +      case TK_GT:
          942  +      case TK_GE:
          943  +      case TK_BITOR:
          944  +      case TK_CONCAT:
          945  +      case TK_COMMA:
          946  +      case TK_BITAND:
          947  +      case TK_BITNOT:
          948  +      case TK_DOT:
          949  +      case TK_IN:
          950  +      case TK_IS:
          951  +      case TK_NOT:
          952  +      case TK_NULL:
          953  +      case TK_ID: {
          954  +        if( tokenType==TK_NULL ){
          955  +          if( prevTokenType==TK_IS || prevTokenType==TK_NOT ){
          956  +            /* NULL is a keyword in this case, not a literal value */
          957  +          }else{
          958  +            /* Here the NULL is a literal value */
          959  +            z[j++] = '?';
          960  +            break;
          961  +          }
          962  +        }
          963  +        if( j>0 && sqlite3IsIdChar(z[j-1]) && sqlite3IsIdChar(zSql[i]) ){
          964  +          z[j++] = ' ';
          965  +        }
          966  +        if( tokenType==TK_ID ){
          967  +          int i2 = i, n2 = n, rc = SQLITE_OK;
          968  +          if( nParen>0 ){
          969  +            assert( nParen<nSql );
          970  +            sqlite3HashInsert(&inHash, zSql+nParen, 0);
          971  +          }
          972  +          if( flags&SQLITE_TOKEN_QUOTED ){ i2++; n2-=2; }
          973  +          if( shouldTreatAsIdentifier(db, zSql+i2, n2, &rc)==0 ){
          974  +            if( rc!=SQLITE_OK ){
          975  +              sqlite3DbFree(db, z);
          976  +              sqlite3HashClear(&inHash);
          977  +              return;
          978  +            }
          979  +            if( sqlite3_keyword_check(zSql+i2, n2)==0 ){
          980  +              z[j++] = '?';
          981  +              break;
          982  +            }
          983  +          }
          984  +        }
          985  +        copyNormalizedToken(zSql, i, n, flags, z, &j);
          986  +        break;
          987  +      }
          988  +    }
          989  +  }
          990  +  assert( j<nZ && "one" );
          991  +  while( j>0 && z[j-1]==' ' ){ j--; }
          992  +  if( j>0 && z[j-1]!=';' ){ z[j++] = ';'; }
          993  +  z[j] = 0;
          994  +  assert( j<nZ && "two" );
          995  +  pVdbe->zNormSql = z;
          996  +  sqlite3HashClear(&inHash);
          997  +}
          998  +#endif /* SQLITE_ENABLE_NORMALIZE */
          999  +
   712   1000   /*
   713   1001   ** Rerun the compilation of a statement after a schema change.
   714   1002   **
   715   1003   ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
   716   1004   ** if the statement cannot be recompiled because another connection has
   717   1005   ** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
   718   1006   ** occurs, return SQLITE_SCHEMA.

Changes to src/sqlite.h.in.

  3605   3605   ** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()]
  3606   3606   ** and [sqlite3_prepare16_v3()] assume that the prepared statement will 
  3607   3607   ** be used just once or at most a few times and then destroyed using
  3608   3608   ** [sqlite3_finalize()] relatively soon. The current implementation acts
  3609   3609   ** on this hint by avoiding the use of [lookaside memory] so as not to
  3610   3610   ** deplete the limited store of lookaside memory. Future versions of
  3611   3611   ** SQLite may act on this hint differently.
         3612  +**
         3613  +** [[SQLITE_PREPARE_NORMALIZE]] ^(<dt>SQLITE_PREPARE_NORMALIZE</dt>
         3614  +** <dd>The SQLITE_PREPARE_NORMALIZE flag indicates that a normalized
         3615  +** representation of the SQL statement should be calculated and then
         3616  +** associated with the prepared statement, which can be obtained via
         3617  +** the [sqlite3_normalized_sql()] interface.  The semantics used to
         3618  +** normalize a SQL statement are unspecified and subject to change.
         3619  +** At a minimum, literal values will be replaced with suitable
         3620  +** placeholders.
  3612   3621   ** </dl>
  3613   3622   */
  3614   3623   #define SQLITE_PREPARE_PERSISTENT              0x01
         3624  +#define SQLITE_PREPARE_NORMALIZE               0x02
  3615   3625   
  3616   3626   /*
  3617   3627   ** CAPI3REF: Compiling An SQL Statement
  3618   3628   ** KEYWORDS: {SQL statement compiler}
  3619   3629   ** METHOD: sqlite3
  3620   3630   ** CONSTRUCTOR: sqlite3_stmt
  3621   3631   **
................................................................................
  3765   3775   ** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
  3766   3776   ** SQL text used to create [prepared statement] P if P was
  3767   3777   ** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()],
  3768   3778   ** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
  3769   3779   ** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
  3770   3780   ** string containing the SQL text of prepared statement P with
  3771   3781   ** [bound parameters] expanded.
         3782  +** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
         3783  +** string containing the normalized SQL text of prepared statement P.  The
         3784  +** semantics used to normalize a SQL statement are unspecified and subject
         3785  +** to change.  At a minimum, literal values will be replaced with suitable
         3786  +** placeholders.
  3772   3787   **
  3773   3788   ** ^(For example, if a prepared statement is created using the SQL
  3774   3789   ** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
  3775   3790   ** and parameter :xyz is unbound, then sqlite3_sql() will return
  3776   3791   ** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql()
  3777   3792   ** will return "SELECT 2345,NULL".)^
  3778   3793   **
................................................................................
  3780   3795   ** is available to hold the result, or if the result would exceed the
  3781   3796   ** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
  3782   3797   **
  3783   3798   ** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
  3784   3799   ** bound parameter expansions.  ^The [SQLITE_OMIT_TRACE] compile-time
  3785   3800   ** option causes sqlite3_expanded_sql() to always return NULL.
  3786   3801   **
  3787         -** ^The string returned by sqlite3_sql(P) is managed by SQLite and is
  3788         -** automatically freed when the prepared statement is finalized.
         3802  +** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P)
         3803  +** are managed by SQLite and are automatically freed when the prepared
         3804  +** statement is finalized.
  3789   3805   ** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
  3790   3806   ** is obtained from [sqlite3_malloc()] and must be free by the application
  3791   3807   ** by passing it to [sqlite3_free()].
  3792   3808   */
  3793   3809   const char *sqlite3_sql(sqlite3_stmt *pStmt);
  3794   3810   char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
         3811  +const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt);
  3795   3812   
  3796   3813   /*
  3797   3814   ** CAPI3REF: Determine If An SQL Statement Writes The Database
  3798   3815   ** METHOD: sqlite3_stmt
  3799   3816   **
  3800   3817   ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
  3801   3818   ** and only if the [prepared statement] X makes no direct changes to

Changes to src/sqlite3ext.h.

   306    306     void (*str_append)(sqlite3_str*, const char *zIn, int N);
   307    307     void (*str_appendall)(sqlite3_str*, const char *zIn);
   308    308     void (*str_appendchar)(sqlite3_str*, int N, char C);
   309    309     void (*str_reset)(sqlite3_str*);
   310    310     int (*str_errcode)(sqlite3_str*);
   311    311     int (*str_length)(sqlite3_str*);
   312    312     char *(*str_value)(sqlite3_str*);
          313  +  /* Version 3.25.0 and later */
   313    314     int (*create_window_function)(sqlite3*,const char*,int,int,void*,
   314    315                               void (*xStep)(sqlite3_context*,int,sqlite3_value**),
   315    316                               void (*xFinal)(sqlite3_context*),
   316    317                               void (*xValue)(sqlite3_context*),
   317    318                               void (*xInv)(sqlite3_context*,int,sqlite3_value**),
   318    319                               void(*xDestroy)(void*));
          320  +  /* Version 3.26.0 and later */
          321  +  const char *(*normalized_sql)(sqlite3_stmt*);
   319    322   };
   320    323   
   321    324   /*
   322    325   ** This is the function signature used for all extension entry points.  It
   323    326   ** is also defined in the file "loadext.c".
   324    327   */
   325    328   typedef int (*sqlite3_loadext_entry)(
................................................................................
   599    602   #define sqlite3_str_appendchar         sqlite3_api->str_appendchar
   600    603   #define sqlite3_str_reset              sqlite3_api->str_reset
   601    604   #define sqlite3_str_errcode            sqlite3_api->str_errcode
   602    605   #define sqlite3_str_length             sqlite3_api->str_length
   603    606   #define sqlite3_str_value              sqlite3_api->str_value
   604    607   /* Version 3.25.0 and later */
   605    608   #define sqlite3_create_window_function sqlite3_api->create_window_function
          609  +/* Version 3.26.0 and later */
          610  +#define sqlite3_normalized_sql         sqlite3_api->normalized_sql
   606    611   #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
   607    612   
   608    613   #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
   609    614     /* This case when the file really is being compiled as a loadable 
   610    615     ** extension */
   611    616   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
   612    617   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;

Changes to src/sqliteInt.h.

  1301   1301   };
  1302   1302   
  1303   1303   /*
  1304   1304   ** A hash table for built-in function definitions.  (Application-defined
  1305   1305   ** functions use a regular table table from hash.h.)
  1306   1306   **
  1307   1307   ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
  1308         -** Collisions are on the FuncDef.u.pHash chain.
         1308  +** Collisions are on the FuncDef.u.pHash chain.  Use the SQLITE_FUNC_HASH()
         1309  +** macro to compute a hash on the function name.
  1309   1310   */
  1310   1311   #define SQLITE_FUNC_HASH_SZ 23
  1311   1312   struct FuncDefHash {
  1312   1313     FuncDef *a[SQLITE_FUNC_HASH_SZ];       /* Hash table for functions */
  1313   1314   };
         1315  +#define SQLITE_FUNC_HASH(C,L) (((C)+(L))%SQLITE_FUNC_HASH_SZ)
  1314   1316   
  1315   1317   #ifdef SQLITE_USER_AUTHENTICATION
  1316   1318   /*
  1317   1319   ** Information held in the "sqlite3" database connection object and used
  1318   1320   ** to manage user authentication.
  1319   1321   */
  1320   1322   typedef struct sqlite3_userauth sqlite3_userauth;
................................................................................
  1941   1943   /*
  1942   1944   ** The schema for each SQL table and view is represented in memory
  1943   1945   ** by an instance of the following structure.
  1944   1946   */
  1945   1947   struct Table {
  1946   1948     char *zName;         /* Name of the table or view */
  1947   1949     Column *aCol;        /* Information about each column */
         1950  +#ifdef SQLITE_ENABLE_NORMALIZE
         1951  +  Hash *pColHash;      /* All columns indexed by name */
         1952  +#endif
  1948   1953     Index *pIndex;       /* List of SQL indexes on this table. */
  1949   1954     Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
  1950   1955     FKey *pFKey;         /* Linked list of all foreign keys in this table */
  1951   1956     char *zColAff;       /* String defining the affinity of each column */
  1952   1957     ExprList *pCheck;    /* All CHECK constraints */
  1953   1958                          /*   ... also used as column name list in a VIEW */
  1954   1959     int tnum;            /* Root BTree page for this table */
................................................................................
  2277   2282     void *p;          /* Pointer to sampled record */
  2278   2283     int n;            /* Size of record in bytes */
  2279   2284     tRowcnt *anEq;    /* Est. number of rows where the key equals this sample */
  2280   2285     tRowcnt *anLt;    /* Est. number of rows where key is less than this sample */
  2281   2286     tRowcnt *anDLt;   /* Est. number of distinct keys less than this sample */
  2282   2287   };
  2283   2288   
         2289  +/*
         2290  +** Possible values to use within the flags argument to sqlite3GetToken().
         2291  +*/
         2292  +#define SQLITE_TOKEN_QUOTED    0x1 /* Token is a quoted identifier. */
         2293  +#define SQLITE_TOKEN_KEYWORD   0x2 /* Token is a keyword. */
         2294  +
  2284   2295   /*
  2285   2296   ** Each token coming out of the lexer is an instance of
  2286   2297   ** this structure.  Tokens are also used as part of an expression.
  2287   2298   **
  2288   2299   ** The memory that "z" points to is owned by other objects.  Take care
  2289   2300   ** that the owner of the "z" string does not deallocate the string before
  2290   2301   ** the Token goes out of scope!  Very often, the "z" points to some place
................................................................................
  3998   4009   #ifdef SQLITE_ENABLE_CURSOR_HINTS
  3999   4010   int sqlite3ExprContainsSubquery(Expr*);
  4000   4011   #endif
  4001   4012   int sqlite3ExprIsInteger(Expr*, int*);
  4002   4013   int sqlite3ExprCanBeNull(const Expr*);
  4003   4014   int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
  4004   4015   int sqlite3IsRowid(const char*);
         4016  +#ifdef SQLITE_ENABLE_NORMALIZE
         4017  +int sqlite3IsRowidN(const char*, int);
         4018  +#endif
  4005   4019   void sqlite3GenerateRowDelete(
  4006   4020       Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
  4007   4021   void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
  4008   4022   int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
  4009   4023   void sqlite3ResolvePartIdxLabel(Parse*,int);
  4010   4024   int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int);
  4011   4025   void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
................................................................................
  4024   4038   void sqlite3UniqueConstraint(Parse*, int, Index*);
  4025   4039   void sqlite3RowidConstraint(Parse*, int, Table*);
  4026   4040   Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
  4027   4041   ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
  4028   4042   SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
  4029   4043   IdList *sqlite3IdListDup(sqlite3*,IdList*);
  4030   4044   Select *sqlite3SelectDup(sqlite3*,Select*,int);
         4045  +#ifdef SQLITE_ENABLE_NORMALIZE
         4046  +FuncDef *sqlite3FunctionSearchN(int,const char*,int);
         4047  +#endif
  4031   4048   void sqlite3InsertBuiltinFuncs(FuncDef*,int);
  4032   4049   FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
  4033   4050   void sqlite3RegisterBuiltinFunctions(void);
  4034   4051   void sqlite3RegisterDateTimeFunctions(void);
  4035   4052   void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
  4036   4053   int sqlite3SafetyCheckOk(sqlite3*);
  4037   4054   int sqlite3SafetyCheckSickOrOk(sqlite3*);
................................................................................
  4227   4244   #endif
  4228   4245   void sqlite3RootPageMoved(sqlite3*, int, int, int);
  4229   4246   void sqlite3Reindex(Parse*, Token*, Token*);
  4230   4247   void sqlite3AlterFunctions(void);
  4231   4248   void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
  4232   4249   void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
  4233   4250   int sqlite3GetToken(const unsigned char *, int *);
         4251  +#ifdef SQLITE_ENABLE_NORMALIZE
         4252  +int sqlite3GetTokenNormalized(const unsigned char *, int *, int *);
         4253  +#endif
  4234   4254   void sqlite3NestedParse(Parse*, const char*, ...);
  4235   4255   void sqlite3ExpirePreparedStatements(sqlite3*, int);
  4236   4256   int sqlite3CodeSubselect(Parse*, Expr *, int, int);
  4237   4257   void sqlite3SelectPrep(Parse*, Select*, NameContext*);
  4238   4258   void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
  4239   4259   int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
  4240   4260   int sqlite3ResolveExprNames(NameContext*, Expr*);
................................................................................
  4384   4404   int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
  4385   4405   int sqlite3VtabBegin(sqlite3 *, VTable *);
  4386   4406   FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
  4387   4407   sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
  4388   4408   int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
  4389   4409   int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
  4390   4410   void sqlite3ParserReset(Parse*);
         4411  +#ifdef SQLITE_ENABLE_NORMALIZE
         4412  +void sqlite3Normalize(Vdbe*, const char*, int, u8);
         4413  +#endif
  4391   4414   int sqlite3Reprepare(Vdbe*);
  4392   4415   void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
  4393   4416   CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
  4394   4417   int sqlite3TempInMemory(const sqlite3*);
  4395   4418   const char *sqlite3JournalModename(int);
  4396   4419   #ifndef SQLITE_OMIT_WAL
  4397   4420     int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);

Changes to src/test1.c.

  4214   4214     Tcl_Obj *CONST objv[]
  4215   4215   ){
  4216   4216     sqlite3 *db;
  4217   4217     const char *zSql;
  4218   4218     char *zCopy = 0;                /* malloc() copy of zSql */
  4219   4219     int bytes;
  4220   4220     const char *zTail = 0;
         4221  +  const char **pzTail;
  4221   4222     sqlite3_stmt *pStmt = 0;
  4222   4223     char zBuf[50];
  4223   4224     int rc;
  4224   4225   
  4225   4226     if( objc!=5 && objc!=4 ){
  4226   4227       Tcl_AppendResult(interp, "wrong # args: should be \"", 
  4227   4228          Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
................................................................................
  4238   4239       zCopy = malloc(bytes);
  4239   4240       memcpy(zCopy, zSql, bytes);
  4240   4241     }else{
  4241   4242       int n = (int)strlen(zSql) + 1;
  4242   4243       zCopy = malloc(n);
  4243   4244       memcpy(zCopy, zSql, n);
  4244   4245     }
  4245         -  rc = sqlite3_prepare_v2(db, zCopy, bytes, &pStmt, objc>=5 ? &zTail : 0);
         4246  +  pzTail = objc>=5 ? &zTail : 0;
         4247  +  rc = sqlite3_prepare_v2(db, zCopy, bytes, &pStmt, pzTail);
  4246   4248     free(zCopy);
  4247   4249     zTail = &zSql[(zTail - zCopy)];
  4248   4250   
  4249   4251     assert(rc==SQLITE_OK || pStmt==0);
  4250   4252     Tcl_ResetResult(interp);
  4251   4253     if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  4252   4254     if( rc==SQLITE_OK && zTail && objc>=5 ){
  4253   4255       if( bytes>=0 ){
  4254   4256         bytes = bytes - (int)(zTail-zSql);
  4255   4257       }
  4256   4258       Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
  4257   4259     }
         4260  +  if( rc!=SQLITE_OK ){
         4261  +    assert( pStmt==0 );
         4262  +    sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
         4263  +    Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
         4264  +    return TCL_ERROR;
         4265  +  }
         4266  +
         4267  +  if( pStmt ){
         4268  +    if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
         4269  +    Tcl_AppendResult(interp, zBuf, 0);
         4270  +  }
         4271  +  return TCL_OK;
         4272  +}
         4273  +
         4274  +/*
         4275  +** Usage: sqlite3_prepare_v3 DB sql bytes flags ?tailvar?
         4276  +**
         4277  +** Compile up to <bytes> bytes of the supplied SQL string <sql> using
         4278  +** database handle <DB> and flags <flags>. The parameter <tailval> is
         4279  +** the name of a global variable that is set to the unused portion of
         4280  +** <sql> (if any). A STMT handle is returned.
         4281  +*/
         4282  +static int SQLITE_TCLAPI test_prepare_v3(
         4283  +  void * clientData,
         4284  +  Tcl_Interp *interp,
         4285  +  int objc,
         4286  +  Tcl_Obj *CONST objv[]
         4287  +){
         4288  +  sqlite3 *db;
         4289  +  const char *zSql;
         4290  +  char *zCopy = 0;                /* malloc() copy of zSql */
         4291  +  int bytes, flags;
         4292  +  const char *zTail = 0;
         4293  +  const char **pzTail;
         4294  +  sqlite3_stmt *pStmt = 0;
         4295  +  char zBuf[50];
         4296  +  int rc;
         4297  +
         4298  +  if( objc!=6 && objc!=5 ){
         4299  +    Tcl_AppendResult(interp, "wrong # args: should be \"", 
         4300  +       Tcl_GetString(objv[0]), " DB sql bytes flags tailvar", 0);
         4301  +    return TCL_ERROR;
         4302  +  }
         4303  +  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
         4304  +  zSql = Tcl_GetString(objv[2]);
         4305  +  if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
         4306  +  if( Tcl_GetIntFromObj(interp, objv[4], &flags) ) return TCL_ERROR;
         4307  +
         4308  +  /* Instead of using zSql directly, make a copy into a buffer obtained
         4309  +  ** directly from malloc(). The idea is to make it easier for valgrind
         4310  +  ** to spot buffer overreads.  */
         4311  +  if( bytes>=0 ){
         4312  +    zCopy = malloc(bytes);
         4313  +    memcpy(zCopy, zSql, bytes);
         4314  +  }else{
         4315  +    int n = (int)strlen(zSql) + 1;
         4316  +    zCopy = malloc(n);
         4317  +    memcpy(zCopy, zSql, n);
         4318  +  }
         4319  +  pzTail = objc>=6 ? &zTail : 0;
         4320  +  rc = sqlite3_prepare_v3(db, zCopy, bytes, (unsigned int)flags,&pStmt,pzTail);
         4321  +  free(zCopy);
         4322  +  zTail = &zSql[(zTail - zCopy)];
         4323  +
         4324  +  assert(rc==SQLITE_OK || pStmt==0);
         4325  +  Tcl_ResetResult(interp);
         4326  +  if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
         4327  +  if( rc==SQLITE_OK && zTail && objc>=6 ){
         4328  +    if( bytes>=0 ){
         4329  +      bytes = bytes - (int)(zTail-zSql);
         4330  +    }
         4331  +    Tcl_ObjSetVar2(interp, objv[5], 0, Tcl_NewStringObj(zTail, bytes), 0);
         4332  +  }
  4258   4333     if( rc!=SQLITE_OK ){
  4259   4334       assert( pStmt==0 );
  4260   4335       sqlite3_snprintf(sizeof(zBuf), zBuf, "(%d) ", rc);
  4261   4336       Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
  4262   4337       return TCL_ERROR;
  4263   4338     }
  4264   4339   
................................................................................
  4672   4747   
  4673   4748     if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
  4674   4749     z = sqlite3_expanded_sql(pStmt);
  4675   4750     Tcl_SetResult(interp, z, TCL_VOLATILE);
  4676   4751     sqlite3_free(z);
  4677   4752     return TCL_OK;
  4678   4753   }
         4754  +#ifdef SQLITE_ENABLE_NORMALIZE
         4755  +static int SQLITE_TCLAPI test_norm_sql(
         4756  +  void * clientData,
         4757  +  Tcl_Interp *interp,
         4758  +  int objc,
         4759  +  Tcl_Obj *CONST objv[]
         4760  +){
         4761  +  sqlite3_stmt *pStmt;
         4762  +
         4763  +  if( objc!=2 ){
         4764  +    Tcl_WrongNumArgs(interp, 1, objv, "STMT");
         4765  +    return TCL_ERROR;
         4766  +  }
         4767  +
         4768  +  if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
         4769  +  Tcl_SetResult(interp, (char *)sqlite3_normalized_sql(pStmt), TCL_VOLATILE);
         4770  +  return TCL_OK;
         4771  +}
         4772  +#endif /* SQLITE_ENABLE_NORMALIZE */
  4679   4773   
  4680   4774   /*
  4681   4775   ** Usage: sqlite3_column_count STMT 
  4682   4776   **
  4683   4777   ** Return the number of columns returned by the sql statement STMT.
  4684   4778   */
  4685   4779   static int SQLITE_TCLAPI test_column_count(
................................................................................
  7642   7736        { "sqlite3_open_v2",               test_open_v2       ,0 },
  7643   7737        { "sqlite3_complete16",            test_complete16    ,0 },
  7644   7738        { "sqlite3_normalize",             test_normalize     ,0 },
  7645   7739   
  7646   7740        { "sqlite3_prepare",               test_prepare       ,0 },
  7647   7741        { "sqlite3_prepare16",             test_prepare16     ,0 },
  7648   7742        { "sqlite3_prepare_v2",            test_prepare_v2    ,0 },
         7743  +     { "sqlite3_prepare_v3",            test_prepare_v3    ,0 },
  7649   7744        { "sqlite3_prepare_tkt3134",       test_prepare_tkt3134, 0},
  7650   7745        { "sqlite3_prepare16_v2",          test_prepare16_v2  ,0 },
  7651   7746        { "sqlite3_finalize",              test_finalize      ,0 },
  7652   7747        { "sqlite3_stmt_status",           test_stmt_status   ,0 },
  7653   7748        { "sqlite3_reset",                 test_reset         ,0 },
  7654   7749        { "sqlite3_expired",               test_expired       ,0 },
  7655   7750        { "sqlite3_transfer_bindings",     test_transfer_bind ,0 },
  7656   7751        { "sqlite3_changes",               test_changes       ,0 },
  7657   7752        { "sqlite3_step",                  test_step          ,0 },
  7658   7753        { "sqlite3_sql",                   test_sql           ,0 },
  7659   7754        { "sqlite3_expanded_sql",          test_ex_sql        ,0 },
         7755  +#ifdef SQLITE_ENABLE_NORMALIZE
         7756  +     { "sqlite3_normalized_sql",        test_norm_sql      ,0 },
         7757  +#endif
  7660   7758        { "sqlite3_next_stmt",             test_next_stmt     ,0 },
  7661   7759        { "sqlite3_stmt_readonly",         test_stmt_readonly ,0 },
  7662   7760        { "sqlite3_stmt_busy",             test_stmt_busy     ,0 },
  7663   7761        { "uses_stmt_journal",             uses_stmt_journal ,0 },
  7664   7762   
  7665   7763        { "sqlite3_release_memory",        test_release_memory,     0},
  7666   7764        { "sqlite3_db_release_memory",     test_db_release_memory,  0},

Changes to src/test_config.c.

   757    757   #endif
   758    758   
   759    759   #ifdef SQLITE_ENABLE_URI_00_ERROR
   760    760     Tcl_SetVar2(interp, "sqlite_options", "uri_00_error", "1", TCL_GLOBAL_ONLY);
   761    761   #else
   762    762     Tcl_SetVar2(interp, "sqlite_options", "uri_00_error", "0", TCL_GLOBAL_ONLY);
   763    763   #endif
          764  +
          765  +#if defined(SQLITE_ENABLE_NORMALIZE)
          766  +  Tcl_SetVar2(interp, "sqlite_options", "normalize", "1", TCL_GLOBAL_ONLY);
          767  +#else
          768  +  Tcl_SetVar2(interp, "sqlite_options", "normalize", "0", TCL_GLOBAL_ONLY);
          769  +#endif
   764    770   
   765    771   #ifdef SQLITE_OMIT_WINDOWFUNC
   766    772     Tcl_SetVar2(interp, "sqlite_options", "windowfunc", "0", TCL_GLOBAL_ONLY);
   767    773   #else
   768    774     Tcl_SetVar2(interp, "sqlite_options", "windowfunc", "1", TCL_GLOBAL_ONLY);
   769    775   #endif
   770    776   

Changes to src/tokenize.c.

   541    541       }
   542    542     }
   543    543     while( IdChar(z[i]) ){ i++; }
   544    544     *tokenType = TK_ID;
   545    545     return i;
   546    546   }
   547    547   
          548  +#ifdef SQLITE_ENABLE_NORMALIZE
          549  +/*
          550  +** Return the length (in bytes) of the token that begins at z[0].
          551  +** Store the token type in *tokenType before returning.  If flags has
          552  +** SQLITE_TOKEN_NORMALIZE flag enabled, use the identifier token type
          553  +** for keywords.  Add SQLITE_TOKEN_QUOTED to flags if the token was
          554  +** actually a quoted identifier.  Add SQLITE_TOKEN_KEYWORD to flags
          555  +** if the token was recognized as a keyword; this is useful when the
          556  +** SQLITE_TOKEN_NORMALIZE flag is used, because it enables the caller
          557  +** to differentiate between a keyword being treated as an identifier
          558  +** (for normalization purposes) and an actual identifier.
          559  +*/
          560  +int sqlite3GetTokenNormalized(
          561  +  const unsigned char *z,
          562  +  int *tokenType,
          563  +  int *flags
          564  +){
          565  +  int n;
          566  +  unsigned char iClass = aiClass[*z];
          567  +  if( iClass==CC_KYWD ){
          568  +    int i;
          569  +    for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
          570  +    if( IdChar(z[i]) ){
          571  +      /* This token started out using characters that can appear in keywords,
          572  +      ** but z[i] is a character not allowed within keywords, so this must
          573  +      ** be an identifier instead */
          574  +      i++;
          575  +      while( IdChar(z[i]) ){ i++; }
          576  +      *tokenType = TK_ID;
          577  +      return i;
          578  +    }
          579  +    *tokenType = TK_ID;
          580  +    n = keywordCode((char*)z, i, tokenType);
          581  +    /* If the token is no longer considered to be an identifier, then it is a
          582  +    ** keyword of some kind.  Make the token back into an identifier and then
          583  +    ** set the SQLITE_TOKEN_KEYWORD flag.  Several non-identifier tokens are
          584  +    ** used verbatim, including IN, IS, NOT, and NULL. */
          585  +    switch( *tokenType ){
          586  +      case TK_ID: {
          587  +        /* do nothing, handled by caller */
          588  +        break;
          589  +      }
          590  +      case TK_IN:
          591  +      case TK_IS:
          592  +      case TK_NOT:
          593  +      case TK_NULL: {
          594  +        *flags |= SQLITE_TOKEN_KEYWORD;
          595  +        break;
          596  +      }
          597  +      default: {
          598  +        *tokenType = TK_ID;
          599  +        *flags |= SQLITE_TOKEN_KEYWORD;
          600  +        break;
          601  +      }
          602  +    }
          603  +  }else{
          604  +    n = sqlite3GetToken(z, tokenType);
          605  +    /* If the token is considered to be an identifier and the character class
          606  +    ** of the first character is a quote, set the SQLITE_TOKEN_QUOTED flag. */
          607  +    if( *tokenType==TK_ID && (iClass==CC_QUOTE || iClass==CC_QUOTE2) ){
          608  +      *flags |= SQLITE_TOKEN_QUOTED;
          609  +    }
          610  +  }
          611  +  return n;
          612  +}
          613  +#endif /* SQLITE_ENABLE_NORMALIZE */
          614  +
   548    615   /*
   549    616   ** Run the parser on the given SQL string.  The parser structure is
   550    617   ** passed in.  An SQLITE_ status code is returned.  If an error occurs
   551    618   ** then an and attempt is made to write an error message into 
   552    619   ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
   553    620   ** error message.
   554    621   */

Changes to src/vdbeInt.h.

   402    402     bft usesStmtJournal:1;  /* True if uses a statement journal */
   403    403     bft readOnly:1;         /* True for statements that do not write */
   404    404     bft bIsReader:1;        /* True for statements that read */
   405    405     yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
   406    406     yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
   407    407     u32 aCounter[7];        /* Counters used by sqlite3_stmt_status() */
   408    408     char *zSql;             /* Text of the SQL statement that generated this */
          409  +#ifdef SQLITE_ENABLE_NORMALIZE
          410  +  char *zNormSql;         /* Normalization of the associated SQL statement */
          411  +#endif
   409    412     void *pFree;            /* Free this when deleting the vdbe */
   410    413     VdbeFrame *pFrame;      /* Parent frame */
   411    414     VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
   412    415     int nFrame;             /* Number of frames in pFrame list */
   413    416     u32 expmask;            /* Binding to these vars invalidates VM */
   414    417     SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
   415    418     AuxData *pAuxData;      /* Linked list of auxdata allocations */

Changes to src/vdbeapi.c.

  1698   1698       z = sqlite3VdbeExpandSql(p, zSql);
  1699   1699       sqlite3_mutex_leave(p->db->mutex);
  1700   1700     }
  1701   1701     return z;
  1702   1702   #endif
  1703   1703   }
  1704   1704   
         1705  +#ifdef SQLITE_ENABLE_NORMALIZE
         1706  +/*
         1707  +** Return the normalized SQL associated with a prepared statement.
         1708  +*/
         1709  +const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){
         1710  +  Vdbe *p = (Vdbe *)pStmt;
         1711  +  return p ? p->zNormSql : 0;
         1712  +}
         1713  +#endif /* SQLITE_ENABLE_NORMALIZE */
         1714  +
  1705   1715   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
  1706   1716   /*
  1707   1717   ** Allocate and populate an UnpackedRecord structure based on the serialized
  1708   1718   ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
  1709   1719   ** if successful, or a NULL pointer if an OOM error is encountered.
  1710   1720   */
  1711   1721   static UnpackedRecord *vdbeUnpackRecord(

Changes to src/vdbeaux.c.

    60     60     if( p==0 ) return;
    61     61     p->prepFlags = prepFlags;
    62     62     if( (prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){
    63     63       p->expmask = 0;
    64     64     }
    65     65     assert( p->zSql==0 );
    66     66     p->zSql = sqlite3DbStrNDup(p->db, z, n);
           67  +#ifdef SQLITE_ENABLE_NORMALIZE
           68  +  assert( p->zNormSql==0 );
           69  +  if( p->zSql && (prepFlags & SQLITE_PREPARE_NORMALIZE)!=0 ){
           70  +    sqlite3Normalize(p, p->zSql, n, prepFlags);
           71  +    assert( p->zNormSql!=0 || p->db->mallocFailed );
           72  +  }
           73  +#endif
    67     74   }
    68     75   
    69     76   /*
    70     77   ** Swap all content between two VDBE structures.
    71     78   */
    72     79   void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
    73     80     Vdbe tmp, *pTmp;
................................................................................
    81     88     pB->pNext = pTmp;
    82     89     pTmp = pA->pPrev;
    83     90     pA->pPrev = pB->pPrev;
    84     91     pB->pPrev = pTmp;
    85     92     zTmp = pA->zSql;
    86     93     pA->zSql = pB->zSql;
    87     94     pB->zSql = zTmp;
           95  +#ifdef SQLITE_ENABLE_NORMALIZE
           96  +  zTmp = pA->zNormSql;
           97  +  pA->zNormSql = pB->zNormSql;
           98  +  pB->zNormSql = zTmp;
           99  +#endif
    88    100     pB->expmask = pA->expmask;
    89    101     pB->prepFlags = pA->prepFlags;
    90    102     memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
    91    103     pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
    92    104   }
    93    105   
    94    106   /*
................................................................................
  3152   3164       releaseMemArray(p->aVar, p->nVar);
  3153   3165       sqlite3DbFree(db, p->pVList);
  3154   3166       sqlite3DbFree(db, p->pFree);
  3155   3167     }
  3156   3168     vdbeFreeOpArray(db, p->aOp, p->nOp);
  3157   3169     sqlite3DbFree(db, p->aColName);
  3158   3170     sqlite3DbFree(db, p->zSql);
         3171  +#ifdef SQLITE_ENABLE_NORMALIZE
         3172  +  sqlite3DbFree(db, p->zNormSql);
         3173  +#endif
  3159   3174   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
  3160   3175     {
  3161   3176       int i;
  3162   3177       for(i=0; i<p->nScan; i++){
  3163   3178         sqlite3DbFree(db, p->aScan[i].zName);
  3164   3179       }
  3165   3180       sqlite3DbFree(db, p->aScan);

Changes to test/normalize.test.

    67     67     {select*from t1 where x in(?,?,?);}
    68     68     180
    69     69     {    }
    70     70     {}
    71     71   } {
    72     72     do_test $tnum [list sqlite3_normalize $sql] $norm
    73     73   }
           74  +
           75  +ifcapable normalize {
           76  +do_test 200 {
           77  +  execsql {
           78  +    CREATE TABLE t1(a,b);
           79  +  }
           80  +} {}
           81  +do_test 201 {
           82  +  set STMT [sqlite3_prepare_v3 $DB \
           83  +      "SELECT a, b FROM t1 WHERE b = ? ORDER BY a;" -1 0 TAIL]
           84  +
           85  +  sqlite3_bind_null $STMT 1
           86  +} {}
           87  +do_test 202 {
           88  +  sqlite3_normalized_sql $STMT
           89  +} {}
           90  +do_test 203 {
           91  +  sqlite3_finalize $STMT
           92  +} {SQLITE_OK}
           93  +
           94  +do_test 210 {
           95  +  set STMT [sqlite3_prepare_v3 $DB \
           96  +      "SELECT a, b FROM t1 WHERE b = ? ORDER BY a;" -1 2 TAIL]
           97  +
           98  +  sqlite3_bind_null $STMT 1
           99  +} {}
          100  +do_test 211 {
          101  +  sqlite3_normalized_sql $STMT
          102  +} {SELECT a,b FROM t1 WHERE b=?ORDER BY a;}
          103  +do_test 212 {
          104  +  sqlite3_finalize $STMT
          105  +} {SQLITE_OK}
          106  +
          107  +do_test 220 {
          108  +  set STMT [sqlite3_prepare_v3 $DB \
          109  +      "SELECT a, b FROM t1 WHERE b = 'a' ORDER BY a;" -1 2 TAIL]
          110  +} {/^[0-9A-Fa-f]+$/}
          111  +do_test 221 {
          112  +  sqlite3_normalized_sql $STMT
          113  +} {SELECT a,b FROM t1 WHERE b=?ORDER BY a;}
          114  +do_test 222 {
          115  +  sqlite3_finalize $STMT
          116  +} {SQLITE_OK}
          117  +
          118  +do_test 297 {
          119  +  execsql {
          120  +    DROP TABLE t1;
          121  +  }
          122  +} {}
          123  +do_test 298 {
          124  +  execsql {
          125  +    CREATE TABLE t1(a,b,c,d,e,"col f",w,x,y,z);
          126  +    CREATE TABLE t2(x,"col y");
          127  +  }
          128  +} {}
          129  +do_test 299 {
          130  +  sqlite3_create_function db
          131  +} {SQLITE_OK}
          132  +
          133  +foreach {tnum sql flags norm} {
          134  +  300
          135  +  {SELECT * FROM t1 WHERE a IN (1) AND b=51.42}
          136  +  0x2
          137  +  {0 {SELECT*FROM t1 WHERE a IN(?,?,?)AND b=?;}}
          138  +
          139  +  310
          140  +  {SELECT a, b+15, c FROM t1 WHERE d NOT IN (SELECT x FROM t2);}
          141  +  0x2
          142  +  {0 {SELECT a,b+?,c FROM t1 WHERE d NOT IN(SELECT x FROM t2);}}
          143  +
          144  +  320
          145  +  { SELECT NULL, b FROM t1 -- comment text
          146  +     WHERE d IN (WITH t(a) AS (VALUES(5)) /* CTE */
          147  +                 SELECT a FROM t)
          148  +        OR e='hello';
          149  +  }
          150  +  0x2
          151  +  {0 {SELECT?,b FROM t1 WHERE d IN(WITH t(a)AS(VALUES(?))SELECT a FROM t)OR e=?;}}
          152  +
          153  +  321
          154  +  {/*Initial comment*/
          155  +   -- another comment line
          156  +   SELECT NULL  /* comment */ , b FROM t1 -- comment text
          157  +     WHERE d IN (WITH t(a) AS (VALUES(5)) /* CTE */
          158  +                 SELECT a FROM t)
          159  +        OR e='hello';
          160  +  }
          161  +  0x2
          162  +  {0 {SELECT?,b FROM t1 WHERE d IN(WITH t(a)AS(VALUES(?))SELECT a FROM t)OR e=?;}}
          163  +
          164  +  330
          165  +  {/* Query containing parameters */
          166  +   SELECT x,$::abc(15),y,@abc,z,?99,w FROM t1 /* Trailing comment */}
          167  +  0x2
          168  +  {0 {SELECT x,?,y,?,z,?,w FROM t1;}}
          169  +
          170  +  340
          171  +  {/* Long list on the RHS of IN */
          172  +   SELECT 15 IN (1,2,3,(SELECT * FROM t1),'xyz',x'abcd',22*(x+5),null);}
          173  +  0x2
          174  +  {1 {(1) no such column: x}}
          175  +
          176  +  350
          177  +  {SELECT x'abc'; -- illegal token}
          178  +  0x2
          179  +  {1 {(1) unrecognized token: "x'abc'"}}
          180  +
          181  +  360
          182  +  {SELECT a,NULL,b FROM t1 WHERE c IS NOT NULL or D is null or e=5}
          183  +  0x2
          184  +  {0 {SELECT a,?,b FROM t1 WHERE c IS NOT NULL OR d IS NULL OR e=?;}}
          185  +
          186  +  370
          187  +  {/* IN list exactly 5 bytes long */
          188  +   SELECT * FROM t1 WHERE x IN (1,2,3);}
          189  +  0x2
          190  +  {0 {SELECT*FROM t1 WHERE x IN(?,?,?);}}
          191  +
          192  +  400
          193  +  {SELECT a FROM t1 WHERE x IN (1,2,3) AND sqlite_version();}
          194  +  0x2
          195  +  {0 {SELECT a FROM t1 WHERE x IN(?,?,?)AND sqlite_version();}}
          196  +
          197  +  410
          198  +  {SELECT a FROM t1 WHERE x IN (1,2,3) AND hex8();}
          199  +  0x2
          200  +  {1 {(1) wrong number of arguments to function hex8()}}
          201  +
          202  +  420
          203  +  {SELECT a FROM t1 WHERE x IN (1,2,3) AND hex8('abc');}
          204  +  0x2
          205  +  {0 {SELECT a FROM t1 WHERE x IN(?,?,?)AND hex8(?);}}
          206  +
          207  +  430
          208  +  {SELECT "a" FROM t1 WHERE "x" IN ("1","2",'3');}
          209  +  0x2
          210  +  {0 {SELECT"a"FROM t1 WHERE"x"IN(?,?,?);}}
          211  +
          212  +  440
          213  +  {SELECT 'a' FROM t1 WHERE 'x';}
          214  +  0x2
          215  +  {0 {SELECT?FROM t1 WHERE?;}}
          216  +
          217  +  450
          218  +  {SELECT [a] FROM t1 WHERE [x];}
          219  +  0x2
          220  +  {0 {SELECT"a"FROM t1 WHERE"x";}}
          221  +
          222  +  460
          223  +  {SELECT * FROM t1 WHERE x IN (x);}
          224  +  0x2
          225  +  {0 {SELECT*FROM t1 WHERE x IN(x);}}
          226  +
          227  +  470
          228  +  {SELECT * FROM t1 WHERE x IN (x,a);}
          229  +  0x2
          230  +  {0 {SELECT*FROM t1 WHERE x IN(x,a);}}
          231  +
          232  +  480
          233  +  {SELECT * FROM t1 WHERE x IN ([x],"a");}
          234  +  0x2
          235  +  {0 {SELECT*FROM t1 WHERE x IN("x","a");}}
          236  +
          237  +  500
          238  +  {SELECT * FROM t1 WHERE x IN ([x],"a",'b',sqlite_version());}
          239  +  0x2
          240  +  {0 {SELECT*FROM t1 WHERE x IN("x","a",?,sqlite_version());}}
          241  +
          242  +  520
          243  +  {SELECT * FROM t1 WHERE x IN (SELECT x FROM t1);}
          244  +  0x2
          245  +  {0 {SELECT*FROM t1 WHERE x IN(SELECT x FROM t1);}}
          246  +
          247  +  540
          248  +  {SELECT * FROM t1 WHERE x IN ((SELECT x FROM t1));}
          249  +  0x2
          250  +  {0 {SELECT*FROM t1 WHERE x IN(?,?,?);}}
          251  +
          252  +  550
          253  +  {SELECT a, a+1, a||'b', a+"b" FROM t1;}
          254  +  0x2
          255  +  {0 {SELECT a,a+?,a||?,a+"b"FROM t1;}}
          256  +
          257  +  570
          258  +  {SELECT * FROM t1 WHERE x IN (1);}
          259  +  0x2
          260  +  {0 {SELECT*FROM t1 WHERE x IN(?,?,?);}}
          261  +
          262  +  580
          263  +  {SELECT * FROM t1 WHERE x IN (1,2);}
          264  +  0x2
          265  +  {0 {SELECT*FROM t1 WHERE x IN(?,?,?);}}
          266  +
          267  +  590
          268  +  {SELECT * FROM t1 WHERE x IN (1,2,3);}
          269  +  0x2
          270  +  {0 {SELECT*FROM t1 WHERE x IN(?,?,?);}}
          271  +
          272  +  600
          273  +  {SELECT * FROM t1 WHERE x IN (1,2,3,4);}
          274  +  0x2
          275  +  {0 {SELECT*FROM t1 WHERE x IN(?,?,?);}}
          276  +
          277  +  610
          278  +  {SELECT * FROM t1 WHERE x IN (SELECT x FROM t1);}
          279  +  0x2
          280  +  {0 {SELECT*FROM t1 WHERE x IN(SELECT x FROM t1);}}
          281  +
          282  +  620
          283  +  {SELECT * FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN (1,2,3));}
          284  +  0x2
          285  +  {0 {SELECT*FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(?,?,?));}}
          286  +
          287  +  630
          288  +  {SELECT * FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN (x));}
          289  +  0x2
          290  +  {0 {SELECT*FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(x));}}
          291  +
          292  +  640
          293  +  {SELECT x FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN (
          294  +   SELECT x FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN (
          295  +   SELECT x FROM t1 WHERE x IN (x)))));}
          296  +  0x2
          297  +  {0 {SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(x)))));}}
          298  +
          299  +  650
          300  +  {SELECT x FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN (
          301  +   SELECT x FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN (
          302  +   SELECT x FROM t1 WHERE x IN (1)))));}
          303  +  0x2
          304  +  {0 {SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(?,?,?)))));}}
          305  +
          306  +  660
          307  +  {SELECT x FROM t1 WHERE x IN (1) UNION ALL SELECT x FROM t1 WHERE x IN (1);}
          308  +  0x2
          309  +  {0 {SELECT x FROM t1 WHERE x IN(?,?,?)UNION ALL SELECT x FROM t1 WHERE x IN(?,?,?);}}
          310  +
          311  +  670
          312  +  {SELECT "col f", [col f] FROM t1;}
          313  +  0x2
          314  +  {0 {SELECT"col f","col f"FROM t1;}}
          315  +
          316  +  680
          317  +  {SELECT a, "col f" FROM t1 LEFT OUTER JOIN t2 ON [t1].[col f] == [t2].[col y];}
          318  +  0x2
          319  +  {0 {SELECT a,"col f"FROM t1 LEFT OUTER JOIN t2 ON"t1"."col f"=="t2"."col y";}}
          320  +
          321  +  690
          322  +  {SELECT * FROM ( WITH x AS ( SELECT * FROM t1 WHERE x IN ( 1)) SELECT 10);}
          323  +  0x2
          324  +  {0 {SELECT*FROM(WITH x AS(SELECT*FROM t1 WHERE x IN(?,?,?))SELECT?);}}
          325  +
          326  +  700
          327  +  {SELECT rowid, oid, _rowid_ FROM t1;}
          328  +  0x2
          329  +  {0 {SELECT rowid,oid,_rowid_ FROM t1;}}
          330  +
          331  +  710
          332  +  {SELECT x FROM t1 WHERE x IS NULL;}
          333  +  0x2
          334  +  {0 {SELECT x FROM t1 WHERE x IS NULL;}}
          335  +
          336  +  740
          337  +  {SELECT x FROM t1 WHERE x IS NOT NULL;}
          338  +  0x2
          339  +  {0 {SELECT x FROM t1 WHERE x IS NOT NULL;}}
          340  +
          341  +  750
          342  +  {SELECT x FROM t1 WHERE x = NULL;}
          343  +  0x2
          344  +  {0 {SELECT x FROM t1 WHERE x=?;}}
          345  +
          346  +  760
          347  +  {SELECT x FROM t1 WHERE x IN ([x] IS NOT NULL, NULL, 1, 'a', "b", x'00');}
          348  +  0x2
          349  +  {0 {SELECT x FROM t1 WHERE x IN("x"IS NOT NULL,?,?,?,"b",?);}}
          350  +} {
          351  +  do_test $tnum {
          352  +    set code [catch {
          353  +      set STMT [sqlite3_prepare_v3 $DB $sql -1 $flags TAIL]
          354  +      sqlite3_normalized_sql $STMT
          355  +    } res]
          356  +    if {[info exists STMT]} {
          357  +      sqlite3_finalize $STMT; unset STMT
          358  +    }
          359  +    list $code $res
          360  +  } $norm
          361  +}
          362  +}
    74    363   
    75    364   finish_test