Repository navigation
Expand file tree
/
Copy pathchanges.diff
More file actions
5470 lines (5392 loc) · 200 KB
/
Copy pathchanges.diff
File metadata and controls
5470 lines (5392 loc) · 200 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000000..c3df22eca5
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,29 @@
+/.DS_Store
+/.idea/
+/.main.mk.checks
+/.target_source
+/.tclenv.sh
+/Makefile
+/config.log
+/*.a
+/*.c
+/*.dylib
+/*.dSYM/
+/*.h
+/*.o
+/*.out
+/*.pc
+/*.sql
+/*.y
+/has_tclsh*
+/jimsh
+/lemon
+/mkkeywordhash
+/mksourceid
+/srcck1
+/src-verify
+/testdir*/
+/testfixture
+/testrun_*/
+/testrunner.*
+/tsrc/
diff --git a/Makefile.msc b/Makefile.msc
index 220367810c..2f851f13fc 100644
--- a/Makefile.msc
+++ b/Makefile.msc
@@ -1291,7 +1291,7 @@ LIBOBJS0 = vdbe.lo parse.lo alter.lo analyze.lo attach.lo auth.lo \
fts5.lo \
func.lo global.lo hash.lo \
icu.lo insert.lo json.lo legacy.lo loadext.lo \
- main.lo malloc.lo mem0.lo mem1.lo mem2.lo mem3.lo mem5.lo \
+ main.lo malloc.lo matchertext.lo mem0.lo mem1.lo mem2.lo mem3.lo mem5.lo \
memdb.lo memjournal.lo \
mutex.lo mutex_noop.lo mutex_unix.lo mutex_w32.lo \
notify.lo opcodes.lo os.lo os_kv.lo os_unix.lo os_win.lo \
@@ -1363,6 +1363,7 @@ SRC00 = \
$(TOP)\src\loadext.c \
$(TOP)\src\main.c \
$(TOP)\src\malloc.c \
+ $(TOP)\src\matchertext.c \
$(TOP)\src\mem0.c \
$(TOP)\src\mem1.c \
$(TOP)\src\mem2.c \
@@ -1547,6 +1548,7 @@ TESTSRC = \
$(TOP)\src\test_intarray.c \
$(TOP)\src\test_journal.c \
$(TOP)\src\test_malloc.c \
+ $(TOP)\src\test_matchertext.c \
$(TOP)\src\test_md5.c \
$(TOP)\src\test_multiplex.c \
$(TOP)\src\test_mutex.c \
@@ -2123,6 +2125,9 @@ main.lo: $(TOP)\src\main.c $(HDR)
malloc.lo: $(TOP)\src\malloc.c $(HDR)
$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\malloc.c
+matchertext.lo: $(TOP)\src\matchertext.c $(HDR)
+ $(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\matchertext.c
+
mem0.lo: $(TOP)\src\mem0.c $(HDR)
$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\mem0.c
diff --git a/README.md b/README.md
index 990befcd2d..e07837336f 100644
--- a/README.md
+++ b/README.md
@@ -1,428 +1,275 @@
-<h1 align="center">SQLite Source Repository</h1>
-
-This repository contains the complete source code for the
-[SQLite database engine](https://sqlite.org/) going back
-to 2000-05-29. The tree includes many tests and some
-documentation, though additional tests and most documentation
-are managed separately.
-
-See the [on-line documentation](https://sqlite.org/) for more information
-about what SQLite is and how it works from a user's perspective. This
-README file is about the source code that goes into building SQLite,
-not about how SQLite is used.
-
-## Version Control
-
-SQLite sources are managed using
-[Fossil](https://fossil-scm.org/), a distributed version control system
-that was specifically designed and written to support SQLite development.
-The [Fossil repository](https://sqlite.org/src/timeline) contains the urtext.
-
-If you are reading this on GitHub or some other Git repository or service,
-then you are looking at a mirror. The names of check-ins and
-other artifacts in a Git mirror are different from the official
-names for those objects. The official names for check-ins are
-found in a footer on the check-in comment for authorized mirrors.
-The official check-in name can also be seen in the `manifest.uuid` file
-in the root of the tree. Always use the official name, not the
-Git-name, when communicating about an SQLite check-in.
-
-If you pulled your SQLite source code from a secondary source and want to
-verify its integrity, there are hints on how to do that in the
-[Verifying Code Authenticity](#vauth) section below.
-
-## Contacting The SQLite Developers
-
-The preferred way to ask questions or make comments about SQLite is to
-visit the [SQLite Forum](https://sqlite.org/forum) at
-<https://sqlite.org/forum/>. Anonymous postings are permitted. For
-bug reports, please use the [SQLite bugs
-forum](https://sqlite.org/bugs) at <https://sqlite.org/bugs>.
-
-If you think you have found a bug that has security implications and
-you do not want to report it on the public forum, you can send a private
-email to drh at sqlite dot org.
-
-## Public Domain
-
-The SQLite source code is in the public domain. See
-<https://sqlite.org/copyright.html> for details.
-
-Because SQLite is in the public domain, we do not normally accept pull
-requests, because if we did take a pull request, the changes in that
-pull request might carry a copyright and the SQLite source code would
-then no longer be fully in the public domain.
-
-## Obtaining The SQLite Source Code
-
-Source code tarballs or ZIP archives are available at:
-
- * [Latest trunk check-in](https://sqlite.org/src/rchvdwnld/trunk).
-
- * [Latest release](https://sqlite.org/src/rchvdwnld/release)
-
- * For other check-ins, browse the
- [project timeline](https://sqlite.org/src/timeline?y=ci) and
- click on the check-in hash of the check-in you want to download.
- On the resulting "info" page, click one of the options to the
- right of the "**Downloads:**" label in the "**Overview**" section
- near the top.
-
-To access sources directly using [Fossil](https://fossil-scm.org/home),
-first install Fossil version 2.0 or later.
-Source tarballs and precompiled binaries for Fossil are available at
-<https://fossil-scm.org/home/uv/download.html>. Fossil is
-a stand-alone program. To install, simply download or build the single
-executable file and put that file someplace on your $PATH or %PATH%.
-Then run commands like this:
-
- mkdir -p ~/sqlite
- cd ~/sqlite
- fossil open https://sqlite.org/src
-
-The initial "fossil open" command will take two or three minutes. Afterwards,
-you can do fast, bandwidth-efficient updates to whatever versions of SQLite you
-like. Some examples:
-
- fossil update trunk ;# latest trunk check-in
- fossil update release ;# latest official release
- fossil update trunk:2024-01-01 ;# First trunk check-in after 2024-01-01
- fossil update version-3.39.0 ;# Version 3.39.0
-
-Or type "fossil ui" to get a web-based user interface.
-
-## Compiling for Unix-like systems
-
-First create a directory in which to place
-the build products. It is recommended, but not required, that the
-build directory be separate from the source directory. Cd into the
-build directory and then from the build directory run the configure
-script found at the root of the source tree. Then run "make".
-See the [compile-for-unix.md](doc/compile-for-unix.md) document for
-more detail.
-
-For example:
-
- apt install gcc make tcl-dev ;# Install the necessary build tools
- tar xzf sqlite.tar.gz ;# Unpack the source tree into "sqlite"
- mkdir bld ;# Build happens in a sibling directory
- cd bld ;# Change to the build directory
- ../sqlite/configure ;# Run the configure script
- make sqlite3 ;# The "sqlite3" command-line tool
- make sqlite3.c ;# The "amalgamation" source file
- make sqldiff ;# The "sqldiff" command-line tool
- #### Targets below require tcl-dev ####
- make tclextension-install ;# Install the SQLite TCL extension
- make devtest ;# Run development tests
- make releasetest ;# Run full release tests
- make sqlite3_analyzer ;# Builds the "sqlite3_analyzer" tool
-
-See the makefile for additional targets. For debugging builds, the
-core developers typically run "configure" with options like this:
-
- ../sqlite/configure --all --debug CFLAGS='-O0 -g'
-
-For release builds, the core developers usually do:
-
- ../sqlite/configure --all
-
-Core deliverables (sqlite3.c, sqlite3) can be built without a TCL, but
-many makefile targets require a "tclsh" TCL interpreter version 8.6
-or later. The "tclextension-install" target and the test targets that follow
-all require TCL development libraries too. ("apt install tcl-dev"). It is
-helpful, but is not required, to install the SQLite TCL extension (the
-"tclextension-install" target) prior to running tests. The "releasetest"
-target has additional requirements, such as "valgrind".
-
-On "make" command-lines, one can add "OPTIONS=..." to specify additional
-compile-time options over and above those set by ./configure. For example,
-to compile with the SQLITE_OMIT_DEPRECATED compile-time option, one could say:
-
- ./configure --all
- make OPTIONS=-DSQLITE_OMIT_DEPRECATED sqlite3
-
-The configure script uses [autosetup](https://msteveb.github.io/autosetup/).
-If the configure script does not work out for you, there is a generic
-makefile named "Makefile.linux-gcc" in the top directory of the source tree
-that you can copy and edit to suit your needs. Comments on the generic
-makefile show what changes are needed.
-
-## Compiling for Windows Using MSVC
-
-On Windows, everything can be compiled with MSVC.
-You will also need a working installation of TCL if you want to run tests,
-though TCL is not required if you just want to build SQLite itself.
-See the [compile-for-windows.md](doc/compile-for-windows.md) document for
-additional information about how to install MSVC and TCL and configure your
-build environment.
-
-If you want to run tests, you need to let SQLite know the location of your
-TCL library, using a command like this:
-
- set TCLDIR=c:\Tcl
-
-SQLite itself does not contain any TCL code, but it does use TCL to run
-tests. You may need to install TCL development libraries in order to
-successfully complete some makefile targets. It is helpful, but is not
-required, to install the SQLite TCL extension (the "tclextension-install"
-target) prior to running tests.
-
-The source tree contains a "make.bat" file that allows the same "make"
-commands of Unix to work on Windows. In the following, you can substitute
-"nmake /f Makefile.msc" in place of "make", if you prefer to avoid this BAT
-file:
-
- make sqlite3.exe
- make sqlite3.c
- make sqldiff.exe
- #### Targets below require TCL development libraries ####
- make tclextension-install
- make devtest
- make releasetest
- make sqlite3_analyzer.exe
-
-There are many other makefile targets. See comments in Makefile.msc for
-details.
-
-As with the unix Makefile, the OPTIONS=... argument can be passed on the nmake
-command-line to enable new compile-time options. For example:
-
- make OPTIONS=-DSQLITE_OMIT_DEPRECATED sqlite3.exe
-
-## Source Tree Map
-
- * **src/** - This directory contains the primary source code for the
- SQLite core. For historical reasons, C-code used for testing is
- also found here. Source files intended for testing begin with "`test`".
- The `tclsqlite3.c` and `tclsqlite3.h` files are the TCL interface
- for SQLite and are also not part of the core.
-
- * **test/** - This directory and its subdirectories contains code used
- for testing. Files that end in "`.test`" are TCL scripts that run
- tests using an augmented TCL interpreter named "testfixture". Use
- a command like "`make testfixture`" to build that
- augmented TCL interpreter, then run individual tests using commands like
- "`testfixture test/main.test`". This test/ subdirectory also contains
- additional C code modules and scripts for other kinds of testing.
-
- * **tool/** - This directory contains programs and scripts used to
- build some of the machine-generated code that goes into the SQLite
- core, as well as to build and run tests and perform diagnostics.
- The source code to [the Lemon parser generator](./doc/lemon.html) is
- found here. There are also TCL scripts used to build and/or transform
- source code files. For example, the tool/mksqlite3h.tcl script reads
- the src/sqlite.h.in file and uses it as a template to construct
- the deliverable "sqlite3.h" file that defines the SQLite interface.
-
- * **ext/** - Various extensions to SQLite are found under this
- directory. For example, the FTS5 subsystem is in "ext/fts5/".
- Some of these extensions (ex: FTS3/4, FTS5, RTREE) might get built
- into the SQLite amalgamation, but not all of them. The
- "ext/misc/" subdirectory contains an assortment of one-file extensions,
- many of which are omitted from the SQLite core, but which are included
- in the [SQLite CLI](https://sqlite.org/cli.html).
-
- * **doc/** - Some documentation files about SQLite internals are found
- here. Note, however, that the primary documentation designed for
- application developers and users of SQLite is in a completely separate
- repository. Note also that the primary API documentation is derived
- from specially constructed comments in the src/sqlite.h.in file.
-
-### Generated Source Code Files
-
-Several of the C-language source files used by SQLite are generated from
-other sources rather than being typed in manually by a programmer. This
-section will summarize those automatically-generated files. To create all
-of the automatically-generated files, simply run "make target_source".
-The "target_source" make target will create a subdirectory "tsrc/" and
-fill it with all the source files needed to build SQLite, both
-manually-edited files and automatically-generated files.
-
-The SQLite interface is defined by the **sqlite3.h** header file, which is
-generated from src/sqlite.h.in, ./manifest.uuid, and ./VERSION. The
-[Tcl script](https://www.tcl.tk) at tool/mksqlite3h.tcl does the conversion.
-The manifest.uuid file contains the SHA3 hash of the particular check-in
-and is used to generate the SQLITE\_SOURCE\_ID macro. The VERSION file
-contains the current SQLite version number. The sqlite3.h header is really
-just a copy of src/sqlite.h.in with the source-id and version number inserted
-at just the right spots. Note that comment text in the sqlite3.h file is
-used to generate much of the SQLite API documentation. The Tcl scripts
-used to generate that documentation are in a separate source repository.
-
-The SQL language parser is **parse.c** which is generated from a grammar in
-the src/parse.y file. The conversion of "parse.y" into "parse.c" is done
-by the [lemon](./doc/lemon.html) LALR(1) parser generator. The source code
-for lemon is at tool/lemon.c. Lemon uses the tool/lempar.c file as a
-template for generating its parser.
-Lemon also generates the **parse.h** header file, at the same time it
-generates parse.c.
-
-The **opcodes.h** header file contains macros that define the numbers
-corresponding to opcodes in the "VDBE" virtual machine. The opcodes.h
-file is generated by scanning the src/vdbe.c source file. The
-Tcl script at ./mkopcodeh.tcl does this scan and generates opcodes.h.
-A second Tcl script, ./mkopcodec.tcl, then scans opcodes.h to generate
-the **opcodes.c** source file, which contains a reverse mapping from
-opcode-number to opcode-name that is used for EXPLAIN output.
-
-The **keywordhash.h** header file contains the definition of a hash table
-that maps SQL language keywords (ex: "CREATE", "SELECT", "INDEX", etc.) into
-the numeric codes used by the parse.c parser. The keywordhash.h file is
-generated by a C-language program at tool mkkeywordhash.c.
-
-The **pragma.h** header file contains various definitions used to parse
-and implement the PRAGMA statements. The header is generated by a
-script **tool/mkpragmatab.tcl**. If you want to add a new PRAGMA, edit
-the **tool/mkpragmatab.tcl** file to insert the information needed by the
-parser for your new PRAGMA, then run the script to regenerate the
-**pragma.h** header file.
-
-### The Amalgamation
-
-All of the individual C source code and header files (both manually-edited
-and automatically-generated) can be combined into a single big source file
-**sqlite3.c** called "the amalgamation". The amalgamation is the recommended
-way of using SQLite in a larger application. Combining all individual
-source code files into a single big source code file allows the C compiler
-to perform more cross-procedure analysis and generate better code. SQLite
-runs about 5% faster when compiled from the amalgamation versus when compiled
-from individual source files.
-
-The amalgamation is generated from the tool/mksqlite3c.tcl Tcl script.
-First, all of the individual source files must be gathered into the tsrc/
-subdirectory (using the equivalent of "make target_source") then the
-tool/mksqlite3c.tcl script is run to copy them all together in just the
-right order while resolving internal "#include" references.
-
-The amalgamation source file is more than 200K lines long. Some symbolic
-debuggers (most notably MSVC) are unable to deal with files longer than 64K
-lines. To work around this, a separate Tcl script, tool/split-sqlite3c.tcl,
-can be run on the amalgamation to break it up into a single small C file
-called **sqlite3-all.c** that does #include on about seven other files
-named **sqlite3-1.c**, **sqlite3-2.c**, ..., **sqlite3-7.c**. In this way,
-all of the source code is contained within a single translation unit so
-that the compiler can do extra cross-procedure optimization, but no
-individual source file exceeds 32K lines in length.
-
-## How It All Fits Together
-
-SQLite is modular in design.
-See the [architectural description](https://sqlite.org/arch.html)
-for details. Other documents that are useful in
-helping to understand how SQLite works include the
-[file format](https://sqlite.org/fileformat2.html) description,
-the [virtual machine](https://sqlite.org/opcode.html) that runs
-prepared statements, the description of
-[how transactions work](https://sqlite.org/atomiccommit.html), and
-the [overview of the query planner](https://sqlite.org/optoverview.html).
-
-Decades of effort have gone into optimizing SQLite, both
-for small size and high performance. And optimizations tend to result in
-complex code. So there is a lot of complexity in the current SQLite
-implementation. It will not be the easiest library in the world to hack.
-
-### Key source code files
-
- * **sqlite.h.in** - This file defines the public interface to the SQLite
- library. Readers will need to be familiar with this interface before
- trying to understand how the library works internally. This file is
- really a template that is transformed into the "sqlite3.h" deliverable
- using a script invoked by the makefile.
-
- * **sqliteInt.h** - this header file defines many of the data objects
- used internally by SQLite. In addition to "sqliteInt.h", some
- subsystems inside of sQLite have their own header files. These internal
- interfaces are not for use by applications. They can and do change
- from one release of SQLite to the next.
-
- * **parse.y** - This file describes the LALR(1) grammar that SQLite uses
- to parse SQL statements, and the actions that are taken at each step
- in the parsing process. The file is processed by the
- [Lemon Parser Generator](./doc/lemon.html) to produce the actual C code
- used for parsing.
-
- * **vdbe.c** - This file implements the virtual machine that runs
- prepared statements. There are various helper files whose names
- begin with "vdbe". The VDBE has access to the vdbeInt.h header file
- which defines internal data objects. The rest of SQLite interacts
- with the VDBE through an interface defined by vdbe.h.
-
- * **where.c** - This file (together with its helper files named
- by "where*.c") analyzes the WHERE clause and generates
- virtual machine code to run queries efficiently. This file is
- sometimes called the "query optimizer". It has its own private
- header file, whereInt.h, that defines data objects used internally.
-
- * **btree.c** - This file contains the implementation of the B-Tree
- storage engine used by SQLite. The interface to the rest of the system
- is defined by "btree.h". The "btreeInt.h" header defines objects
- used internally by btree.c and not published to the rest of the system.
-
- * **pager.c** - This file contains the "pager" implementation, the
- module that implements transactions. The "pager.h" header file
- defines the interface between pager.c and the rest of the system.
-
- * **os_unix.c** and **os_win.c** - These two files implement the interface
- between SQLite and the underlying operating system using the run-time
- pluggable VFS interface.
-
- * **shell.c.in** - This file is not part of the core SQLite library. This
- is the file that, when linked against sqlite3.a, generates the
- "sqlite3.exe" command-line shell. The "shell.c.in" file is transformed
- into "shell.c" as part of the build process.
-
- * **tclsqlite.c** - This file implements the Tcl bindings for SQLite. It
- is not part of the core SQLite library. But as most of the tests in this
- repository are written in Tcl, the Tcl language bindings are important.
-
- * **test\*.c** - Files in the src/ folder that begin with "test" go into
- building the "testfixture.exe" program. The testfixture.exe program is
- an enhanced Tcl shell. The testfixture.exe program runs scripts in the
- test/ folder to validate the core SQLite code. The testfixture program
- (and some other test programs too) is built and run when you type
- "make test".
-
- * **VERSION**, **manifest**, **manifest.tags**, and **manifest.uuid** -
- These files define the current SQLite version number. The "VERSION" file
- is human generated, but the "manifest", "manifest.tags", and
- "manifest.uuid" files are automatically generated by the
- [Fossil version control system](https://fossil-scm.org/).
-
-There are many other source files. Each has a succinct header comment that
-describes its purpose and role within the larger system.
-
-<a name="vauth"></a>
-## Verifying Code Authenticity
-
-The `manifest` file at the root directory of the source tree
-contains either a SHA3-256 hash or a SHA1 hash
-for every source file in the repository.
-The name of the version of the entire source tree is just the
-SHA3-256 hash of the `manifest` file itself, possibly with the
-last line of that file omitted if the last line begins with
-"`# Remove this line`".
-The `manifest.uuid` file should contain the SHA3-256 hash of the
-`manifest` file. If all of the above hash comparisons are correct, then
-you can be confident that your source tree is authentic and unadulterated.
-Details on the format for the `manifest` files are available
-[on the Fossil website](https://fossil-scm.org/home/doc/trunk/www/fileformat.wiki#manifest).
-
-The process of checking source code authenticity is automated by the
-makefile:
-
-> make verify-source
-
-Using the makefile to verify source integrity is good for detecting
-accidental changes to the source tree, but malicious changes could be
-hidden by also modifying the makefiles.
-
-## Contacts
-
-The main SQLite website is [https://sqlite.org/](https://sqlite.org/)
-with geographically distributed backups at
-[https://www2.sqlite.org/](https://www2.sqlite.org) and
-[https://www3.sqlite.org/](https://www3.sqlite.org).
-
-Contact the SQLite developers through the
-[SQLite Forum](https://sqlite.org/forum/). In an emergency, you
-can send private email to the lead developer at drh at sqlite dot org.
+# MatcherText additions to the SQLite driver
+
+This SQLite tree adds an opt-in MatcherText input discipline. The additions
+check the balance of `()`, `[]`, and `{}` before an external value can change
+an SQL boundary. All other bytes are nonmatchers.
+
+The additions do not change the SQLite database file format. The schema
+stores each statement as stock SQL, so a build without the additions can read
+a database that this build writes.
+
+Note: all changes can be easily read [here](changes.diff)
+
+## Build modes
+
+Build the additive mode with `SQLITE_ENABLE_MATCHERTEXT`:
+
+```sh
+./configure --dev
+make clean
+make OPTIONS="-DSQLITE_ENABLE_MATCHERTEXT" sqlite3 sqlite3.c
+```
+
+This mode adds the MatcherText syntax and APIs. The standard SQLite prepare
+and execution APIs remain available.
+
+Add `SQLITE_MATCHERTEXT_STRICT` to require the checked preparation API for
+external SQL:
+
+```sh
+make clean
+make OPTIONS="-DSQLITE_ENABLE_MATCHERTEXT -DSQLITE_MATCHERTEXT_STRICT" \
+ sqlite3 sqlite3.c testfixture
+```
+
+A build without `SQLITE_ENABLE_MATCHERTEXT` has the standard SQLite behavior.
+
+## MatcherText rule
+
+A value is MatcherText when its ASCII matcher pairs are correctly nested:
+
+```text
+valid: text
+valid: call(item[index])
+invalid: text]
+invalid: ([)]
+```
+
+The scanner is byte-based and linear. UTF-8 continuation bytes cannot be
+ASCII matchers. The default maximum nesting depth is 1,000. Set
+`SQLITE_MAX_MATCHER_DEPTH` at compile time to use another limit.
+
+SQL template checks do not count matcher bytes inside SQL comments. Matcher
+bytes in literals, identifiers, templates, and input arguments remain checked.
+
+## Checked preparation API
+
+`sqlite3_matchertext_prepare_v3()` composes one checked SQL template. It adds
+two typed placeholders:
+
+- `?V` is a text value. SQLite verifies it and binds it as a parameter.
+- `?I` is an identifier. SQLite verifies it and adds identifier delimiters.
+
+Example:
+
+```c
+const char *name = "alice";
+sqlite3_matchertext_arg args[2] = {
+ {SQLITE_MATCHERTEXT_IDENTIFIER, "users", 5},
+ {SQLITE_MATCHERTEXT_VALUE, name, (sqlite3_uint64)strlen(name)}
+};
+sqlite3_stmt *stmt = 0;
+int rc = sqlite3_matchertext_prepare_v3(
+ db,
+ "SELECT * FROM ?I WHERE name=?V",
+ -1,
+ 0,
+ args,
+ 2,
+ &stmt
+);
+```
+
+The API enforces these rules before SQLite parses the composed SQL:
+
+- The template must be NUL-free and valid MatcherText.
+- Each placeholder must have one argument of the correct type.
+- Every argument must be valid MatcherText.
+- An identifier must be non-NULL and NUL-free.
+- The template must contain one SQL statement.
+
+The output statement uses the standard `sqlite3_step()`, `sqlite3_reset()`,
+and `sqlite3_finalize()` lifecycle. The `prepFlags` argument accepts the normal
+`sqlite3_prepare_v3()` flags.
+
+### Constant SQL, run-time composition
+
+The SQL outside the placeholders must come from the program, never from input.
+`sqlite3_matchertext_prepare_v3()` is a macro that accepts only a string
+literal template: any other expression fails to compile.
+
+A statement built at run time, such as one with optional filters or an `IN`
+list of variable length, uses the `sqlite3_matchertext_sql` builder.
+`sqlite3_matchertext_sql_append()` is a macro that adds a literal piece of SQL.
+`sqlite3_matchertext_sql_arg()` adds a value or an identifier and its
+placeholder. The prepare applies the same checks as
+`sqlite3_matchertext_prepare_v3()`.
+
+```c
+sqlite3_matchertext_sql *q = sqlite3_matchertext_sql_new();
+sqlite3_matchertext_sql_append(q, "SELECT * FROM users WHERE 1");
+if( name ){
+ sqlite3_matchertext_sql_append(q, " AND name=");
+ sqlite3_matchertext_sql_arg(q, SQLITE_MATCHERTEXT_VALUE, name, strlen(name));
+}
+rc = sqlite3_matchertext_sql_prepare(db, q, 0, &stmt);
+sqlite3_matchertext_sql_free(q);
+```
+
+Text that is not a literal enters SQL only as an argument. The functions
+`sqlite3_matchertext_prepare_unchecked()` and
+`sqlite3_matchertext_sql_append_unchecked()` accept any string. A caller of
+either asserts that the text holds no input, and so opts out of this
+guarantee.
+
+## Value and identifier holes
+
+The tokenizer adds a value literal whose boundary is found by matcher balance:
+
+```sql
+SELECT M'(Alice (admin))';
+```
+
+The value of this expression is `Alice (admin)`. Quotes and SQL text inside
+the hole remain data because they do not define its end. A hole that opens and
+does not close, such as `M'(abc'`, is an `unrecognized token` error. It is not
+read as the identifier `M` and a quoted string.
+
+Square-bracket identifiers also use matcher balance:
+
+```sql
+SELECT [display(name)] FROM [users];
+```
+
+The outer `[]` delimit the identifier. Matcher pairs inside the name must be
+balanced. The name reading decodes the matcher escapes and keeps every other
+backslash. Use `sqlite3_matchertext_encode()` to place a name in a hole.
+
+When a statement is stored in the schema, a value hole becomes a `'...'`
+literal of its value. A name hole stays `[...]` when it holds no bracket and
+no backslash, and becomes `"..."` otherwise. The module arguments of
+`CREATE VIRTUAL TABLE` are stored as written, because SQLite passes them to the
+module as they are, when the table is created and again when it is opened.
+The built-in modules refuse holes in their arguments. Stored schema text from a build
+without the additions can hold a `[name]` with unbalanced brackets. That text
+is read the stock way, to the first `]`.
+
+## Verify-or-refuse and encode-and-embed
+
+`?V` is the verify-or-refuse path. It preserves the value bytes and rejects a
+value with unmatched matchers.
+
+The new `%m` conversion in `sqlite3_mprintf()` is the encode-and-embed path.
+It accepts arbitrary NUL-terminated text, escapes only unmatched matchers and
+the matched pair of any text that already reads as an escape, and writes an
+`M'(...)'` value literal. The parser decodes the literal to the original value.
+
+```c
+char *sql = sqlite3_mprintf("SELECT %m", "smile :]");
+/* sql is: SELECT M'(smile :\c[])' */
+
+sqlite3_stmt *stmt = 0;
+int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0); /* not in strict mode */
+sqlite3_free(sql);
+```
+
+Balanced values keep their original spelling inside the literal, backslashes
+included, unless they hold text that reads as an escape. The encoder changes only the representation required to preserve the
+hole boundary. The SQL value after decoding is unchanged.
+
+## Public verification and encoding APIs
+
+The build adds these C interfaces:
+
+```c
+int sqlite3_matchertext_verify(
+ const char *value,
+ sqlite3_int64 length
+);
+
+char *sqlite3_matchertext_encode(
+ const char *value,
+ sqlite3_int64 length,
+ sqlite3_int64 *output_length
+);
+
+char *sqlite3_matchertext_decode(
+ const char *value,
+ sqlite3_int64 length,
+ sqlite3_int64 *output_length
+);
+```
+
+A negative input length means that the input is NUL-terminated. The encoder
+and decoder return memory from `sqlite3_malloc64()`. Release it with
+`sqlite3_free()`.
+
+The escape alphabet is:
+
+| Input | Encoded form |
+| --- | --- |
+| `(` | `\o()` |
+| `)` | `\c()` |
+| `[` | `\o[]` |
+| `]` | `\c[]` |
+| `{` | `\o{}` |
+| `}` | `\c{}` |
+
+Only unmatched matcher characters use these forms. A matched pair remains
+unchanged, and a backslash is an ordinary byte. Where the input already reads
+as an escape, such as `\o()`, the encoder escapes that matched pair:
+`\o()` becomes `\o\o()\c()`. The value or name that SQLite reads is then
+exactly the input. `%m` and `?I` use this encoder for both holes.
+
+## Strict mode
+
+`SQLITE_MATCHERTEXT_STRICT` refuses external SQL passed through the legacy
+UTF-8 and UTF-16 `sqlite3_prepare*()` APIs and through `sqlite3_exec()`. The
+error directs the caller to `sqlite3_matchertext_prepare_v3()`.
+
+SQLite can still prepare its own internal SQL for schema work, `ANALYZE`,
+`VACUUM`, attached databases, and statement recompilation. The SQL of the
+built-in extensions (FTS3, FTS5, R-Tree, RBU, and sessions), the table-valued
+pragmas, `dbstat`, and `bytecode` uses internal entry points that strict mode
+admits. An extension loaded at run time uses the public API, so strict mode
+refuses its SQL. Strict mode has no run-time PRAGMA or database-configuration
+switch.
+
+## Implementation files
+
+The principal additions are:
+
+- `src/matchertext.c`: verification, boundary scan, encoder, and decoder
+- `src/tokenize.c` and `src/parse.y`: value and identifier hole syntax
+- `src/prepare.c`: typed checked preparation
+- `src/printf.c`: the `%m` conversion
+- `src/sqlite.h.in`: public API declarations
+- `src/complete.c`: complete-statement scanning for MatcherText literals
+- `src/legacy.c`: strict-mode entry-point guard
+- `test/matchertext.test`: scanner, parser, API, strict-mode, and regression tests
+
+## Tests
+
+Build the strict test fixture and run the dedicated suite. RBU is enabled so
+that its strict-mode tests run:
+
+```sh
+make clean
+make OPTIONS="-DSQLITE_ENABLE_MATCHERTEXT -DSQLITE_MATCHERTEXT_STRICT \
+ -DSQLITE_ENABLE_RBU" testfixture
+./testfixture test/matchertext.test
+```
+
+The strict-mode cases of `test/matchertext.test` run only in a strict build
+(`ifcapable matchertext_strict`). The additive build must pass the stock suite:
+
+```sh
+make clean
+make OPTIONS="-DSQLITE_ENABLE_MATCHERTEXT" testfixture
+./testfixture test/testrunner.tcl full
+```
+
+Compile the SQLite library or amalgamation with `SQLITE_ENABLE_MATCHERTEXT`.
+Client code that uses the new declarations must also define this flag when it
+includes `sqlite3.h`. Only the library build needs
+`SQLITE_MATCHERTEXT_STRICT`.
diff --git a/ext/fts3/fts3.c b/ext/fts3/fts3.c
index 89f0bea3b8..5ca8572d6f 100644
--- a/ext/fts3/fts3.c
+++ b/ext/fts3/fts3.c
@@ -585,7 +585,7 @@ static void fts3DbExec(
if( zSql==0 ){
*pRc = SQLITE_NOMEM;
}else{
- *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
+ *pRc = sqlite3InternalExec(db, zSql, 0, 0, 0);
sqlite3_free(zSql);
}
}
@@ -761,7 +761,7 @@ static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
if( !zSql ){
rc = SQLITE_NOMEM;
}else{
- rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
+ rc = sqlite3InternalPrepareV2(p->db, zSql, -1, &pStmt, 0);
if( rc==SQLITE_OK ){
sqlite3_step(pStmt);
p->nPgsz = sqlite3_column_int(pStmt, 0);
@@ -1112,7 +1112,7 @@ static int fts3ContentColumns(
if( !zSql ){
rc = SQLITE_NOMEM;
}else{
- rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
+ rc = sqlite3InternalPrepareV2(db, zSql, -1, &pStmt, 0);
if( rc!=SQLITE_OK ){
sqlite3Fts3ErrMsg(pzErr, "%s", sqlite3_errmsg(db));
}
@@ -3953,7 +3953,7 @@ static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
);
if( zSql ){
pTab->bIgnoreSavepoint = 1;
- rc = sqlite3_exec(pTab->db, zSql, 0, 0, 0);
+ rc = sqlite3InternalExec(pTab->db, zSql, 0, 0, 0);
pTab->bIgnoreSavepoint = 0;
sqlite3_free(zSql);
}else{
diff --git a/ext/fts3/fts3Int.h b/ext/fts3/fts3Int.h
index fea31aae84..03f5abc1d0 100644
--- a/ext/fts3/fts3Int.h
+++ b/ext/fts3/fts3Int.h
@@ -55,6 +55,22 @@ SQLITE_EXTENSION_INIT3
#include "fts3_tokenizer.h"
#include "fts3_hash.h"
+/* SQL that the extension composes itself. Built into the library, it runs
+** through the internal entry points of sqliteInt.h, which a strict
+** matchertext build admits while it refuses the plain ones. */
+#if defined(SQLITE_CORE) && defined(SQLITE_ENABLE_MATCHERTEXT) \
+ && defined(SQLITE_MATCHERTEXT_STRICT)
+int sqlite3InternalExec(sqlite3*, const char*, sqlite3_callback, void*, char**);
+int sqlite3InternalPrepareV2(sqlite3*, const char*, int, sqlite3_stmt**,
+ const char**);
+int sqlite3InternalPrepareV3(sqlite3*, const char*, int, unsigned int,
+ sqlite3_stmt**, const char**);
+#elif !defined(sqlite3InternalExec)
+# define sqlite3InternalExec sqlite3_exec
+# define sqlite3InternalPrepareV2 sqlite3_prepare_v2
+# define sqlite3InternalPrepareV3 sqlite3_prepare_v3
+#endif
+
/*
** This constant determines the maximum depth of an FTS expression tree
** that the library will create and use. FTS uses recursion to perform
diff --git a/ext/fts3/fts3_write.c b/ext/fts3/fts3_write.c
index 9688393c64..1d14f58f13 100644
--- a/ext/fts3/fts3_write.c
+++ b/ext/fts3/fts3_write.c
@@ -288,7 +288,7 @@ int sqlite3Fts3PrepareStmt(
|((bAllowVtab==0) ? SQLITE_PREPARE_NO_VTAB : 0)
|(bPersist ? SQLITE_PREPARE_PERSISTENT : 0);
- return sqlite3_prepare_v3(p->db, zSql, -1, f, pp, NULL);
+ return sqlite3InternalPrepareV3(p->db, zSql, -1, f, pp, NULL);
}
/*
@@ -5842,15 +5842,15 @@ int sqlite3Fts3UpdateMethod(
*/
int sqlite3Fts3Optimize(Fts3Table *p){
int rc;
- rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
+ rc = sqlite3InternalExec(p->db, "SAVEPOINT fts3", 0, 0, 0);
if( rc==SQLITE_OK ){
rc = fts3DoOptimize(p, 1);
if( rc==SQLITE_OK || rc==SQLITE_DONE ){
- int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
+ int rc2 = sqlite3InternalExec(p->db, "RELEASE fts3", 0, 0, 0);
if( rc2!=SQLITE_OK ) rc = rc2;
}else{
- sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
- sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
+ sqlite3InternalExec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
+ sqlite3InternalExec(p->db, "RELEASE fts3", 0, 0, 0);
}
}
sqlite3Fts3SegmentsClose(p);
diff --git a/ext/fts5/fts5Int.h b/ext/fts5/fts5Int.h
index d5404535cc..b63a92cd78 100644
--- a/ext/fts5/fts5Int.h
+++ b/ext/fts5/fts5Int.h
@@ -22,6 +22,22 @@ SQLITE_EXTENSION_INIT1
#include <assert.h>
#include <stddef.h>
+/* SQL that the extension composes itself. Built into the library, it runs
+** through the internal entry points of sqliteInt.h, which a strict
+** matchertext build admits while it refuses the plain ones. */
+#if defined(SQLITE_CORE) && defined(SQLITE_ENABLE_MATCHERTEXT) \
+ && defined(SQLITE_MATCHERTEXT_STRICT)
+int sqlite3InternalExec(sqlite3*, const char*, sqlite3_callback, void*, char**);
+int sqlite3InternalPrepareV2(sqlite3*, const char*, int, sqlite3_stmt**,
+ const char**);
+int sqlite3InternalPrepareV3(sqlite3*, const char*, int, unsigned int,
+ sqlite3_stmt**, const char**);
+#elif !defined(sqlite3InternalExec)
+# define sqlite3InternalExec sqlite3_exec
+# define sqlite3InternalPrepareV2 sqlite3_prepare_v2
+# define sqlite3InternalPrepareV3 sqlite3_prepare_v3
+#endif
+
#ifndef SQLITE_AMALGAMATION
typedef unsigned char u8;
diff --git a/ext/fts5/fts5_config.c b/ext/fts5/fts5_config.c
index cea14b500b..8de40d5ad5 100644
--- a/ext/fts5/fts5_config.c
+++ b/ext/fts5/fts5_config.c
@@ -1065,7 +1065,7 @@ int sqlite3Fts5ConfigLoad(Fts5Config *pConfig, int iCookie){
zSql = sqlite3Fts5Mprintf(&rc, zSelect, pConfig->zDb, pConfig->zName);
if( zSql ){
- rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &p, 0);
+ rc = sqlite3InternalPrepareV2(pConfig->db, zSql, -1, &p, 0);
sqlite3_free(zSql);
}
diff --git a/ext/fts5/fts5_index.c b/ext/fts5/fts5_index.c
index 05a61aeb9a..feeb926181 100644
--- a/ext/fts5/fts5_index.c
+++ b/ext/fts5/fts5_index.c
@@ -945,7 +945,7 @@ static int fts5IndexPrepareStmt(
){
if( p->rc==SQLITE_OK ){
if( zSql ){
- int rc = sqlite3_prepare_v3(p->pConfig->db, zSql, -1,
+ int rc = sqlite3InternalPrepareV3(p->pConfig->db, zSql, -1,
SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB,
ppStmt, 0);
/* If this prepare() call fails with SQLITE_ERROR, then one of the
diff --git a/ext/fts5/fts5_main.c b/ext/fts5/fts5_main.c
index 8faae3c93c..52f7d1ff07 100644
--- a/ext/fts5/fts5_main.c
+++ b/ext/fts5/fts5_main.c
@@ -1068,7 +1068,7 @@ static int fts5PrepareStatement(
if( zSql==0 ){
rc = SQLITE_NOMEM;
}else{
- rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
+ rc = sqlite3InternalPrepareV3(pConfig->db, zSql, -1,
SQLITE_PREPARE_PERSISTENT, &pRet, 0);
if( rc!=SQLITE_OK ){
sqlite3Fts5ConfigErrmsg(pConfig, "%s", sqlite3_errmsg(pConfig->db));
@@ -1211,7 +1211,7 @@ static int fts5FindRankFunction(Fts5Cursor *pCsr){
char *zSql = sqlite3Fts5Mprintf(&rc, "SELECT %s", zRankArgs);
if( zSql ){
sqlite3_stmt *pStmt = 0;
- rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
+ rc = sqlite3InternalPrepareV3(pConfig->db, zSql, -1,
SQLITE_PREPARE_PERSISTENT, &pStmt, 0);
sqlite3_free(zSql);
assert( rc==SQLITE_OK || pCsr->pRankArgStmt==0 );
diff --git a/ext/fts5/fts5_storage.c b/ext/fts5/fts5_storage.c
index 58bfcd4c4a..fc54b8f00f 100644
--- a/ext/fts5/fts5_storage.c
+++ b/ext/fts5/fts5_storage.c
@@ -199,7 +199,7 @@ static int fts5StorageGetStmt(
int f = SQLITE_PREPARE_PERSISTENT;
if( eStmt>FTS5_STMT_LOOKUP2 ) f |= SQLITE_PREPARE_NO_VTAB;
p->pConfig->bLock++;
- rc = sqlite3_prepare_v3(pC->db, zSql, -1, f, &p->aStmt[eStmt], 0);
+ rc = sqlite3InternalPrepareV3(pC->db, zSql, -1, f, &p->aStmt[eStmt], 0);
p->pConfig->bLock--;
sqlite3_free(zSql);
if( rc!=SQLITE_OK && pzErrMsg ){
@@ -235,7 +235,7 @@ static int fts5ExecPrintf(
if( zSql==0 ){
rc = SQLITE_NOMEM;
}else{
- rc = sqlite3_exec(db, zSql, 0, 0, pzErr);
+ rc = sqlite3InternalExec(db, zSql, 0, 0, pzErr);
sqlite3_free(zSql);
}
@@ -1110,7 +1110,7 @@ static int fts5StorageCount(Fts5Storage *p, const char *zSuffix, i64 *pnRow){
rc = SQLITE_NOMEM;
}else{
sqlite3_stmt *pCnt = 0;
- rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &pCnt, 0);
+ rc = sqlite3InternalPrepareV2(pConfig->db, zSql, -1, &pCnt, 0);
if( rc==SQLITE_OK ){
if( SQLITE_ROW==sqlite3_step(pCnt) ){
*pnRow = sqlite3_column_int64(pCnt, 0);
diff --git a/ext/fts5/fts5_vocab.c b/ext/fts5/fts5_vocab.c
index 295ace6ba9..c459c9c162 100644
--- a/ext/fts5/fts5_vocab.c
+++ b/ext/fts5/fts5_vocab.c
@@ -350,7 +350,7 @@ static int fts5VocabOpenMethod(
pTab->zFts5Tbl, pTab->zFts5Db, pTab->zFts5Tbl, pTab->zFts5Tbl
);
if( zSql ){
- rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
+ rc = sqlite3InternalPrepareV2(pTab->db, zSql, -1, &pStmt, 0);
}
sqlite3_free(zSql);