Fossil SCM

merge trunk (without SQLite update to 3.20.0 beta, but WITH support for tab-completion in the SQL shell)

jan.nijtmans 2017-07-12 11:03 UTC fossil-2.3-with-older-SQLite merge
Commit 107cfe0204929e615d1d1b2bd1e3c36054b65d0dd829959b20029b84b947ece3
--- skins/xekri/css.txt
+++ skins/xekri/css.txt
@@ -376,11 +376,11 @@
376376
377377
/**************************************
378378
* File List : Age
379379
*/
380380
381
-.fileage tr:hover td {
381
+.fileage tr:hover {
382382
background-color: #225;
383383
}
384384
385385
386386
/**************************************
387387
--- skins/xekri/css.txt
+++ skins/xekri/css.txt
@@ -376,11 +376,11 @@
376
377 /**************************************
378 * File List : Age
379 */
380
381 .fileage tr:hover td {
382 background-color: #225;
383 }
384
385
386 /**************************************
387
--- skins/xekri/css.txt
+++ skins/xekri/css.txt
@@ -376,11 +376,11 @@
376
377 /**************************************
378 * File List : Age
379 */
380
381 .fileage tr:hover {
382 background-color: #225;
383 }
384
385
386 /**************************************
387
+4 -2
--- src/doc.c
+++ src/doc.c
@@ -576,10 +576,11 @@
576576
void doc_page(void){
577577
const char *zName; /* Argument to the /doc page */
578578
const char *zOrigName = "?"; /* Original document name */
579579
const char *zMime; /* Document MIME type */
580580
char *zCheckin = "tip"; /* The check-in holding the document */
581
+ char *zPathSuffix = ""; /* Text to append to g.zPath */
581582
int vid = 0; /* Artifact of check-in */
582583
int rid = 0; /* Artifact of file */
583584
int i; /* Loop counter */
584585
Blob filebody; /* Content of the documentation file */
585586
Blob title; /* Document title */
@@ -619,13 +620,13 @@
619620
}else if( !isUV ){
620621
zName += i;
621622
}
622623
while( zName[0]=='/' ){ zName++; }
623624
if( isUV ){
624
- g.zPath = mprintf("%s/%s", g.zPath, zName);
625
+ zPathSuffix = fossil_strdup(zName);
625626
}else{
626
- g.zPath = mprintf("%s/%s/%s", g.zPath, zCheckin, zName);
627
+ zPathSuffix = mprintf("%s/%s", zCheckin, zName);
627628
}
628629
if( nMiss==0 ) zOrigName = zName;
629630
if( !file_is_simple_pathname(zName, 1) ){
630631
if( sqlite3_strglob("*/", zName)==0 ){
631632
assert( nMiss>=0 && nMiss<count(azSuffix) );
@@ -657,10 +658,11 @@
657658
}else{
658659
vid = name_to_typed_rid(zCheckin, "ci");
659660
rid = doc_load_content(vid, zName, &filebody);
660661
}
661662
}
663
+ g.zPath = mprintf("%s/%s", g.zPath, zPathSuffix);
662664
if( rid==0 ) goto doc_not_found;
663665
blob_to_utf8_no_bom(&filebody, 0);
664666
665667
/* The file is now contained in the filebody blob. Deliver the
666668
** file to the user
667669
--- src/doc.c
+++ src/doc.c
@@ -576,10 +576,11 @@
576 void doc_page(void){
577 const char *zName; /* Argument to the /doc page */
578 const char *zOrigName = "?"; /* Original document name */
579 const char *zMime; /* Document MIME type */
580 char *zCheckin = "tip"; /* The check-in holding the document */
 
581 int vid = 0; /* Artifact of check-in */
582 int rid = 0; /* Artifact of file */
583 int i; /* Loop counter */
584 Blob filebody; /* Content of the documentation file */
585 Blob title; /* Document title */
@@ -619,13 +620,13 @@
619 }else if( !isUV ){
620 zName += i;
621 }
622 while( zName[0]=='/' ){ zName++; }
623 if( isUV ){
624 g.zPath = mprintf("%s/%s", g.zPath, zName);
625 }else{
626 g.zPath = mprintf("%s/%s/%s", g.zPath, zCheckin, zName);
627 }
628 if( nMiss==0 ) zOrigName = zName;
629 if( !file_is_simple_pathname(zName, 1) ){
630 if( sqlite3_strglob("*/", zName)==0 ){
631 assert( nMiss>=0 && nMiss<count(azSuffix) );
@@ -657,10 +658,11 @@
657 }else{
658 vid = name_to_typed_rid(zCheckin, "ci");
659 rid = doc_load_content(vid, zName, &filebody);
660 }
661 }
 
662 if( rid==0 ) goto doc_not_found;
663 blob_to_utf8_no_bom(&filebody, 0);
664
665 /* The file is now contained in the filebody blob. Deliver the
666 ** file to the user
667
--- src/doc.c
+++ src/doc.c
@@ -576,10 +576,11 @@
576 void doc_page(void){
577 const char *zName; /* Argument to the /doc page */
578 const char *zOrigName = "?"; /* Original document name */
579 const char *zMime; /* Document MIME type */
580 char *zCheckin = "tip"; /* The check-in holding the document */
581 char *zPathSuffix = ""; /* Text to append to g.zPath */
582 int vid = 0; /* Artifact of check-in */
583 int rid = 0; /* Artifact of file */
584 int i; /* Loop counter */
585 Blob filebody; /* Content of the documentation file */
586 Blob title; /* Document title */
@@ -619,13 +620,13 @@
620 }else if( !isUV ){
621 zName += i;
622 }
623 while( zName[0]=='/' ){ zName++; }
624 if( isUV ){
625 zPathSuffix = fossil_strdup(zName);
626 }else{
627 zPathSuffix = mprintf("%s/%s", zCheckin, zName);
628 }
629 if( nMiss==0 ) zOrigName = zName;
630 if( !file_is_simple_pathname(zName, 1) ){
631 if( sqlite3_strglob("*/", zName)==0 ){
632 assert( nMiss>=0 && nMiss<count(azSuffix) );
@@ -657,10 +658,11 @@
658 }else{
659 vid = name_to_typed_rid(zCheckin, "ci");
660 rid = doc_load_content(vid, zName, &filebody);
661 }
662 }
663 g.zPath = mprintf("%s/%s", g.zPath, zPathSuffix);
664 if( rid==0 ) goto doc_not_found;
665 blob_to_utf8_no_bom(&filebody, 0);
666
667 /* The file is now contained in the filebody blob. Deliver the
668 ** file to the user
669
+8 -2
--- src/info.c
+++ src/info.c
@@ -162,11 +162,12 @@
162162
*/
163163
static void showParentProject(void){
164164
const char *zParentCode;
165165
zParentCode = db_get("parent-project-code",0);
166166
if( zParentCode ){
167
- fossil_print("derived-from: %s %s\n", zParentCode, db_get("parent-project-name",""));
167
+ fossil_print("derived-from: %s %s\n", zParentCode,
168
+ db_get("parent-project-name",""));
168169
}
169170
}
170171
171172
172173
/*
@@ -178,17 +179,22 @@
178179
** If an argument is specified, provide information about the object
179180
** in the repository of the current tree that the argument refers
180181
** to. Or if the argument is the name of a repository, show
181182
** information about that repository.
182183
**
184
+** If the argument is a repository name, then the --verbose option shows
185
+** known the check-out locations for that repository and all URLs used
186
+** to access the repository. The --verbose is (currently) a no-op if
187
+** the argument is the name of a object within the repository.
188
+**
183189
** Use the "finfo" command to get information about a specific
184190
** file in a checkout.
185191
**
186192
** Options:
187193
**
188194
** -R|--repository FILE Extract info from repository FILE
189
-** -v|--verbose Show extra information
195
+** -v|--verbose Show extra information about repositories
190196
**
191197
** See also: annotate, artifact, finfo, timeline
192198
*/
193199
void info_cmd(void){
194200
i64 fsize;
195201
--- src/info.c
+++ src/info.c
@@ -162,11 +162,12 @@
162 */
163 static void showParentProject(void){
164 const char *zParentCode;
165 zParentCode = db_get("parent-project-code",0);
166 if( zParentCode ){
167 fossil_print("derived-from: %s %s\n", zParentCode, db_get("parent-project-name",""));
 
168 }
169 }
170
171
172 /*
@@ -178,17 +179,22 @@
178 ** If an argument is specified, provide information about the object
179 ** in the repository of the current tree that the argument refers
180 ** to. Or if the argument is the name of a repository, show
181 ** information about that repository.
182 **
 
 
 
 
 
183 ** Use the "finfo" command to get information about a specific
184 ** file in a checkout.
185 **
186 ** Options:
187 **
188 ** -R|--repository FILE Extract info from repository FILE
189 ** -v|--verbose Show extra information
190 **
191 ** See also: annotate, artifact, finfo, timeline
192 */
193 void info_cmd(void){
194 i64 fsize;
195
--- src/info.c
+++ src/info.c
@@ -162,11 +162,12 @@
162 */
163 static void showParentProject(void){
164 const char *zParentCode;
165 zParentCode = db_get("parent-project-code",0);
166 if( zParentCode ){
167 fossil_print("derived-from: %s %s\n", zParentCode,
168 db_get("parent-project-name",""));
169 }
170 }
171
172
173 /*
@@ -178,17 +179,22 @@
179 ** If an argument is specified, provide information about the object
180 ** in the repository of the current tree that the argument refers
181 ** to. Or if the argument is the name of a repository, show
182 ** information about that repository.
183 **
184 ** If the argument is a repository name, then the --verbose option shows
185 ** known the check-out locations for that repository and all URLs used
186 ** to access the repository. The --verbose is (currently) a no-op if
187 ** the argument is the name of a object within the repository.
188 **
189 ** Use the "finfo" command to get information about a specific
190 ** file in a checkout.
191 **
192 ** Options:
193 **
194 ** -R|--repository FILE Extract info from repository FILE
195 ** -v|--verbose Show extra information about repositories
196 **
197 ** See also: annotate, artifact, finfo, timeline
198 */
199 void info_cmd(void){
200 i64 fsize;
201
+1 -1
--- src/manifest.c
+++ src/manifest.c
@@ -804,11 +804,11 @@
804804
** If the user name is omitted, take that to be "anonymous".
805805
*/
806806
case 'U': {
807807
if( p->zUser!=0 ) SYNTAX("more than one U-card");
808808
p->zUser = next_token(&x, 0);
809
- if( p->zUser==0 ){
809
+ if( p->zUser==0 || p->zUser[0]==0 ){
810810
p->zUser = "anonymous";
811811
}else{
812812
defossilize(p->zUser);
813813
}
814814
break;
815815
--- src/manifest.c
+++ src/manifest.c
@@ -804,11 +804,11 @@
804 ** If the user name is omitted, take that to be "anonymous".
805 */
806 case 'U': {
807 if( p->zUser!=0 ) SYNTAX("more than one U-card");
808 p->zUser = next_token(&x, 0);
809 if( p->zUser==0 ){
810 p->zUser = "anonymous";
811 }else{
812 defossilize(p->zUser);
813 }
814 break;
815
--- src/manifest.c
+++ src/manifest.c
@@ -804,11 +804,11 @@
804 ** If the user name is omitted, take that to be "anonymous".
805 */
806 case 'U': {
807 if( p->zUser!=0 ) SYNTAX("more than one U-card");
808 p->zUser = next_token(&x, 0);
809 if( p->zUser==0 || p->zUser[0]==0 ){
810 p->zUser = "anonymous";
811 }else{
812 defossilize(p->zUser);
813 }
814 break;
815
+43 -18
--- src/markdown.md
+++ src/markdown.md
@@ -1,71 +1,96 @@
11
# Markdown Overview #
22
33
## Paragraphs ##
44
5
-> Paragraphs are divided by blank lines.
5
+> Paragraphs are divided by blank lines.
6
+> End a line with two or more spaces to force a mid-paragraph line break.
67
78
## Headings ##
89
910
>
1011
# Top-level Heading Alternative Top Level Heading
11
- =============================
12
+ # Top-level Heading Variant # =============================
1213
>
1314
## Second-level Heading Alternative 2nd Level Heading
14
- -----------------------------
15
+ ## Second-level Heading Variant ## -----------------------------
1516
1617
## Links ##
1718
1819
> 1. **\[display text\]\(URL\)**
1920
> 2. **\[display text\]\(URL "Title"\)**
20
-> 3. **\<URL\>**
21
-> 4. **\[display text\]\[label\]**
21
+> 3. **\[display text\]\(URL 'Title'\)**
22
+> 4. **\<URL\>**
23
+> 5. **\[display text\]\[label\]**
24
+> 6. **\[display text\]\[\]**
25
+
26
+> **URL** may optionally be written **\<URL\>**. With link formats 5 and 6
27
+> ("reference links"), the URL is supplied elsewhere in the document, as shown
28
+> below. Link format 6 reuses the display text as the label. Labels are
29
+> case-insensitive. The title may be split onto the next line with optional
30
+> indenting.
2231
23
-> With link format 4 ("reference links") the label must be resolved by
24
-> including a line of the form **\[label\]:&nbsp;URL** or
25
-> **\[label\]:&nbsp;URL&nbsp;"Title"** somewhere else
26
-> in the document.
32
+> * **\[label\]:&nbsp;URL**
33
+> * **\[label\]:&nbsp;URL&nbsp;"Title"**
34
+> * **\[label\]:&nbsp;URL&nbsp;'Title'**
35
+> * **\[label\]:&nbsp;URL&nbsp;(Title)**
2736
2837
## Fonts ##
2938
3039
> * _\*italic\*_
3140
> * *\_italic\_*
3241
> * __\*\*bold\*\*__
3342
> * **\_\_bold\_\_**
34
-> * `` `code` ``
43
+> * ___\*\*\*italic+bold\*\*\*___
44
+> * ***\_\_\_italic+bold\_\_\_***
45
+> * \``code`\`
3546
36
-> Note that the \`...\` construct disables HTML markup, so one can write,
37
-> for example, **\``<html>`\`** to yield **`<html>`**.
47
+> The **\`code\`** construct disables HTML markup, so one can write, for
48
+> example, **\`\<html\>\`** to yield **`<html>`**.
3849
3950
## Lists ##
4051
4152
>
4253
* bullet item
4354
+ bullet item
4455
- bullet item
4556
1. numbered item
4657
58
+> A two-level list is created by placing additional whitespace before the
59
+> **\***/**+**/**-**/**1.** of the secondary items.
60
+
61
+>
62
+ * top-level item
63
+ * secondary item
64
+
4765
## Block Quotes ##
4866
49
-> Begin each line of a paragraph with ">" to block quote that paragraph.
67
+> Begin each line of a paragraph with **>** to block quote that paragraph.
5068
5169
> >
5270
> This paragraph is indented
5371
> >
5472
> > Double-indented paragraph
73
+
74
+> Begin each line with at least four spaces or one tab to produce a verbatim
75
+> code block.
5576
5677
## Miscellaneous ##
5778
58
-> * In-line images using **\!\[alt-text\]\(image-URL\)**
59
-> * Use HTML for complex formatting issues.
79
+> * In-line images are made using **\!\[alt-text\]\(image-URL\)**.
80
+> * Use HTML for complex formatting such as tables and forms.
6081
> * Escape special characters (ex: "\[", "\(", "\*")
6182
> using backslash (ex: "\\\[", "\\\(", "\\\*").
62
-> * See [daringfireball.net](http://daringfireball.net/projects/markdown/syntax)
63
-> for additional information.
83
+> * A line consisting of **---**, **\*\*\***, or **\_\_\_** is a horizontal
84
+> rule. Spaces and extra **-**/**\***/**_** are allowed.
85
+> * See [daringfireball.net][] for additional information.
86
+> * See this page's [Markdown source](/md_rules?txt=1) for more examples.
6487
6588
## Special Features For Fossil ##
6689
6790
> * In hyperlinks, if the URL begins with "/" then the root of the Fossil
6891
> repository is prepended. This allows for repository-relative hyperlinks.
69
-> * For documents that begin with top-level heading (ex: "# heading #"), the
92
+> * For documents that begin with a top-level heading (ex: "# heading #"), the
7093
> heading is omitted from the body of the document and becomes the document
7194
> title displayed at the top of the Fossil page.
95
+
96
+[daringfireball.net]: http://daringfireball.net/projects/markdown/syntax
7297
--- src/markdown.md
+++ src/markdown.md
@@ -1,71 +1,96 @@
1 # Markdown Overview #
2
3 ## Paragraphs ##
4
5 > Paragraphs are divided by blank lines.
 
6
7 ## Headings ##
8
9 >
10 # Top-level Heading Alternative Top Level Heading
11 =============================
12 >
13 ## Second-level Heading Alternative 2nd Level Heading
14 -----------------------------
15
16 ## Links ##
17
18 > 1. **\[display text\]\(URL\)**
19 > 2. **\[display text\]\(URL "Title"\)**
20 > 3. **\<URL\>**
21 > 4. **\[display text\]\[label\]**
 
 
 
 
 
 
 
 
22
23 > With link format 4 ("reference links") the label must be resolved by
24 > including a line of the form **\[label\]:&nbsp;URL** or
25 > **\[label\]:&nbsp;URL&nbsp;"Title"** somewhere else
26 > in the document.
27
28 ## Fonts ##
29
30 > * _\*italic\*_
31 > * *\_italic\_*
32 > * __\*\*bold\*\*__
33 > * **\_\_bold\_\_**
34 > * `` `code` ``
 
 
35
36 > Note that the \`...\` construct disables HTML markup, so one can write,
37 > for example, **\``<html>`\`** to yield **`<html>`**.
38
39 ## Lists ##
40
41 >
42 * bullet item
43 + bullet item
44 - bullet item
45 1. numbered item
46
 
 
 
 
 
 
 
47 ## Block Quotes ##
48
49 > Begin each line of a paragraph with ">" to block quote that paragraph.
50
51 > >
52 > This paragraph is indented
53 > >
54 > > Double-indented paragraph
 
 
 
55
56 ## Miscellaneous ##
57
58 > * In-line images using **\!\[alt-text\]\(image-URL\)**
59 > * Use HTML for complex formatting issues.
60 > * Escape special characters (ex: "\[", "\(", "\*")
61 > using backslash (ex: "\\\[", "\\\(", "\\\*").
62 > * See [daringfireball.net](http://daringfireball.net/projects/markdown/syntax)
63 > for additional information.
 
 
64
65 ## Special Features For Fossil ##
66
67 > * In hyperlinks, if the URL begins with "/" then the root of the Fossil
68 > repository is prepended. This allows for repository-relative hyperlinks.
69 > * For documents that begin with top-level heading (ex: "# heading #"), the
70 > heading is omitted from the body of the document and becomes the document
71 > title displayed at the top of the Fossil page.
 
 
72
--- src/markdown.md
+++ src/markdown.md
@@ -1,71 +1,96 @@
1 # Markdown Overview #
2
3 ## Paragraphs ##
4
5 > Paragraphs are divided by blank lines.
6 > End a line with two or more spaces to force a mid-paragraph line break.
7
8 ## Headings ##
9
10 >
11 # Top-level Heading Alternative Top Level Heading
12 # Top-level Heading Variant # =============================
13 >
14 ## Second-level Heading Alternative 2nd Level Heading
15 ## Second-level Heading Variant ## -----------------------------
16
17 ## Links ##
18
19 > 1. **\[display text\]\(URL\)**
20 > 2. **\[display text\]\(URL "Title"\)**
21 > 3. **\[display text\]\(URL 'Title'\)**
22 > 4. **\<URL\>**
23 > 5. **\[display text\]\[label\]**
24 > 6. **\[display text\]\[\]**
25
26 > **URL** may optionally be written **\<URL\>**. With link formats 5 and 6
27 > ("reference links"), the URL is supplied elsewhere in the document, as shown
28 > below. Link format 6 reuses the display text as the label. Labels are
29 > case-insensitive. The title may be split onto the next line with optional
30 > indenting.
31
32 > * **\[label\]:&nbsp;URL**
33 > * **\[label\]:&nbsp;URL&nbsp;"Title"**
34 > * **\[label\]:&nbsp;URL&nbsp;'Title'**
35 > * **\[label\]:&nbsp;URL&nbsp;(Title)**
36
37 ## Fonts ##
38
39 > * _\*italic\*_
40 > * *\_italic\_*
41 > * __\*\*bold\*\*__
42 > * **\_\_bold\_\_**
43 > * ___\*\*\*italic+bold\*\*\*___
44 > * ***\_\_\_italic+bold\_\_\_***
45 > * \``code`\`
46
47 > The **\`code\`** construct disables HTML markup, so one can write, for
48 > example, **\`\<html\>\`** to yield **`<html>`**.
49
50 ## Lists ##
51
52 >
53 * bullet item
54 + bullet item
55 - bullet item
56 1. numbered item
57
58 > A two-level list is created by placing additional whitespace before the
59 > **\***/**+**/**-**/**1.** of the secondary items.
60
61 >
62 * top-level item
63 * secondary item
64
65 ## Block Quotes ##
66
67 > Begin each line of a paragraph with **>** to block quote that paragraph.
68
69 > >
70 > This paragraph is indented
71 > >
72 > > Double-indented paragraph
73
74 > Begin each line with at least four spaces or one tab to produce a verbatim
75 > code block.
76
77 ## Miscellaneous ##
78
79 > * In-line images are made using **\!\[alt-text\]\(image-URL\)**.
80 > * Use HTML for complex formatting such as tables and forms.
81 > * Escape special characters (ex: "\[", "\(", "\*")
82 > using backslash (ex: "\\\[", "\\\(", "\\\*").
83 > * A line consisting of **---**, **\*\*\***, or **\_\_\_** is a horizontal
84 > rule. Spaces and extra **-**/**\***/**_** are allowed.
85 > * See [daringfireball.net][] for additional information.
86 > * See this page's [Markdown source](/md_rules?txt=1) for more examples.
87
88 ## Special Features For Fossil ##
89
90 > * In hyperlinks, if the URL begins with "/" then the root of the Fossil
91 > repository is prepended. This allows for repository-relative hyperlinks.
92 > * For documents that begin with a top-level heading (ex: "# heading #"), the
93 > heading is omitted from the body of the document and becomes the document
94 > title displayed at the top of the Fossil page.
95
96 [daringfireball.net]: http://daringfireball.net/projects/markdown/syntax
97
--- src/security_audit.c
+++ src/security_audit.c
@@ -96,11 +96,11 @@
9696
@ <li> Tickets
9797
}
9898
if( zPubPages && zPubPages[0] ){
9999
Glob *pGlob = glob_create(zPubPages);
100100
int i;
101
- @ <li> URLs that matches any of these GLOB patterns:
101
+ @ <li> URLs that match any of these GLOB patterns:
102102
@ <ul>
103103
for(i=0; i<pGlob->nPattern; i++){
104104
@ <li> %h(pGlob->azPattern[i])
105105
}
106106
@ </ul>
107107
--- src/security_audit.c
+++ src/security_audit.c
@@ -96,11 +96,11 @@
96 @ <li> Tickets
97 }
98 if( zPubPages && zPubPages[0] ){
99 Glob *pGlob = glob_create(zPubPages);
100 int i;
101 @ <li> URLs that matches any of these GLOB patterns:
102 @ <ul>
103 for(i=0; i<pGlob->nPattern; i++){
104 @ <li> %h(pGlob->azPattern[i])
105 }
106 @ </ul>
107
--- src/security_audit.c
+++ src/security_audit.c
@@ -96,11 +96,11 @@
96 @ <li> Tickets
97 }
98 if( zPubPages && zPubPages[0] ){
99 Glob *pGlob = glob_create(zPubPages);
100 int i;
101 @ <li> URLs that match any of these GLOB patterns:
102 @ <ul>
103 for(i=0; i<pGlob->nPattern; i++){
104 @ <li> %h(pGlob->azPattern[i])
105 }
106 @ </ul>
107
+819 -87
--- src/shell.c
+++ src/shell.c
@@ -1,5 +1,23 @@
1
+/* DO NOT EDIT!
2
+** This file is automatically generated by the script in the canonical
3
+** SQLite source tree at tool/mkshellc.tcl. That script combines source
4
+** code from various constituent source files of SQLite into this single
5
+** "shell.c" file used to implement the SQLite command-line shell.
6
+**
7
+** Most of the code found below comes from the "src/shell.c.in" file in
8
+** the canonical SQLite source tree. That main file contains "INCLUDE"
9
+** lines that specify other files in the canonical source tree that are
10
+** inserted to getnerate this complete program source file.
11
+**
12
+** The code from multiple files is combined into this single "shell.c"
13
+** source file to help make the command-line program easier to compile.
14
+**
15
+** To modify this program, get a copy of the canonical SQLite source tree,
16
+** edit the src/shell.c.in" and/or some of the other files that are included
17
+** by "src/shell.c.in", then rerun the tool/mkshellc.tcl script.
18
+*/
119
/*
220
** 2001 September 15
321
**
422
** The author disclaims copyright to this source code. In place of
523
** a legal notice, here is a blessing:
@@ -782,14 +800,57 @@
782800
}
783801
}
784802
sqlite3_result_value(pCtx, apVal[0]);
785803
}
786804
787
-/******************************************************************************
788
-** SHA3 hash implementation copied from ../ext/misc/shathree.c
805
+/*
806
+** The source code for several run-time loadable extensions is inserted
807
+** below by the ../tool/mkshellc.tcl script. Before processing that included
808
+** code, we need to override some macros to make the included program code
809
+** work here in the middle of this regular program.
789810
*/
811
+#define SQLITE_EXTENSION_INIT1
812
+#define SQLITE_EXTENSION_INIT2(X)
813
+
814
+/************************* Begin ../ext/misc/shathree.c ******************/
815
+/*
816
+** 2017-03-08
817
+**
818
+** The author disclaims copyright to this source code. In place of
819
+** a legal notice, here is a blessing:
820
+**
821
+** May you do good and not evil.
822
+** May you find forgiveness for yourself and forgive others.
823
+** May you share freely, never taking more than you give.
824
+**
825
+******************************************************************************
826
+**
827
+** This SQLite extension implements a functions that compute SHA1 hashes.
828
+** Two SQL functions are implemented:
829
+**
830
+** sha3(X,SIZE)
831
+** sha3_query(Y,SIZE)
832
+**
833
+** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
834
+** X is NULL.
835
+**
836
+** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
837
+** and returns a hash of their results.
838
+**
839
+** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm
840
+** is used. If SIZE is included it must be one of the integers 224, 256,
841
+** 384, or 512, to determine SHA3 hash variant that is computed.
842
+*/
843
+SQLITE_EXTENSION_INIT1
844
+#include <assert.h>
845
+#include <string.h>
846
+#include <stdarg.h>
790847
typedef sqlite3_uint64 u64;
848
+
849
+/******************************************************************************
850
+** The Hash Engine
851
+*/
791852
/*
792853
** Macros to determine whether the machine is big or little endian,
793854
** and whether or not that determination is run-time or compile-time.
794855
**
795856
** For best performance, an attempt is made to guess at the byte-order
@@ -823,14 +884,10 @@
823884
unsigned nRate; /* Bytes of input accepted per Keccak iteration */
824885
unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
825886
unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
826887
};
827888
828
-/* Allow the following routine to use the B0 variable, which is also
829
-** a macro in the termios.h header file */
830
-#undef B0
831
-
832889
/*
833890
** A single step of the Keccak mixing function for a 1600-bit state
834891
*/
835892
static void KeccakF1600Step(SHA3Context *p){
836893
int i;
@@ -1237,16 +1294,18 @@
12371294
for(i=0; i<p->nRate; i++){
12381295
p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
12391296
}
12401297
return &p->u.x[p->nRate];
12411298
}
1299
+/* End of the hashing logic
1300
+*****************************************************************************/
12421301
12431302
/*
12441303
** Implementation of the sha3(X,SIZE) function.
12451304
**
12461305
** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
1247
-** size is 256. If X is a BLOB, it is hashed as is.
1306
+** size is 256. If X is a BLOB, it is hashed as is.
12481307
** For all other non-NULL types of input, X is converted into a UTF-8 string
12491308
** and the string is hashed without the trailing 0x00 terminator. The hash
12501309
** of a NULL value is NULL.
12511310
*/
12521311
static void sha3Func(
@@ -1373,14 +1432,10 @@
13731432
sqlite3_free(zMsg);
13741433
return;
13751434
}
13761435
nCol = sqlite3_column_count(pStmt);
13771436
z = sqlite3_sql(pStmt);
1378
- if( z==0 ){
1379
- sqlite3_finalize(pStmt);
1380
- continue;
1381
- }
13821437
n = (int)strlen(z);
13831438
hash_step_vformat(&cx,"S%d:",n);
13841439
SHA3Update(&cx,(unsigned char*)z,n);
13851440
13861441
/* Compute a hash over the result of the query */
@@ -1439,12 +1494,654 @@
14391494
}
14401495
sqlite3_finalize(pStmt);
14411496
}
14421497
sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
14431498
}
1444
-/* End of SHA3 hashing logic copy/pasted from ../ext/misc/shathree.c
1445
-********************************************************************************/
1499
+
1500
+
1501
+#ifdef _WIN32
1502
+__declspec(dllexport)
1503
+#endif
1504
+int sqlite3_shathree_init(
1505
+ sqlite3 *db,
1506
+ char **pzErrMsg,
1507
+ const sqlite3_api_routines *pApi
1508
+){
1509
+ int rc = SQLITE_OK;
1510
+ SQLITE_EXTENSION_INIT2(pApi);
1511
+ (void)pzErrMsg; /* Unused parameter */
1512
+ rc = sqlite3_create_function(db, "sha3", 1, SQLITE_UTF8, 0,
1513
+ sha3Func, 0, 0);
1514
+ if( rc==SQLITE_OK ){
1515
+ rc = sqlite3_create_function(db, "sha3", 2, SQLITE_UTF8, 0,
1516
+ sha3Func, 0, 0);
1517
+ }
1518
+ if( rc==SQLITE_OK ){
1519
+ rc = sqlite3_create_function(db, "sha3_query", 1, SQLITE_UTF8, 0,
1520
+ sha3QueryFunc, 0, 0);
1521
+ }
1522
+ if( rc==SQLITE_OK ){
1523
+ rc = sqlite3_create_function(db, "sha3_query", 2, SQLITE_UTF8, 0,
1524
+ sha3QueryFunc, 0, 0);
1525
+ }
1526
+ return rc;
1527
+}
1528
+
1529
+/************************* End ../ext/misc/shathree.c ********************/
1530
+/************************* Begin ../ext/misc/fileio.c ******************/
1531
+/*
1532
+** 2014-06-13
1533
+**
1534
+** The author disclaims copyright to this source code. In place of
1535
+** a legal notice, here is a blessing:
1536
+**
1537
+** May you do good and not evil.
1538
+** May you find forgiveness for yourself and forgive others.
1539
+** May you share freely, never taking more than you give.
1540
+**
1541
+******************************************************************************
1542
+**
1543
+** This SQLite extension implements SQL functions readfile() and
1544
+** writefile().
1545
+*/
1546
+SQLITE_EXTENSION_INIT1
1547
+#include <stdio.h>
1548
+
1549
+/*
1550
+** Implementation of the "readfile(X)" SQL function. The entire content
1551
+** of the file named X is read and returned as a BLOB. NULL is returned
1552
+** if the file does not exist or is unreadable.
1553
+*/
1554
+static void readfileFunc(
1555
+ sqlite3_context *context,
1556
+ int argc,
1557
+ sqlite3_value **argv
1558
+){
1559
+ const char *zName;
1560
+ FILE *in;
1561
+ long nIn;
1562
+ void *pBuf;
1563
+
1564
+ zName = (const char*)sqlite3_value_text(argv[0]);
1565
+ if( zName==0 ) return;
1566
+ in = fopen(zName, "rb");
1567
+ if( in==0 ) return;
1568
+ fseek(in, 0, SEEK_END);
1569
+ nIn = ftell(in);
1570
+ rewind(in);
1571
+ pBuf = sqlite3_malloc( nIn );
1572
+ if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
1573
+ sqlite3_result_blob(context, pBuf, nIn, sqlite3_free);
1574
+ }else{
1575
+ sqlite3_free(pBuf);
1576
+ }
1577
+ fclose(in);
1578
+}
1579
+
1580
+/*
1581
+** Implementation of the "writefile(X,Y)" SQL function. The argument Y
1582
+** is written into file X. The number of bytes written is returned. Or
1583
+** NULL is returned if something goes wrong, such as being unable to open
1584
+** file X for writing.
1585
+*/
1586
+static void writefileFunc(
1587
+ sqlite3_context *context,
1588
+ int argc,
1589
+ sqlite3_value **argv
1590
+){
1591
+ FILE *out;
1592
+ const char *z;
1593
+ sqlite3_int64 rc;
1594
+ const char *zFile;
1595
+
1596
+ zFile = (const char*)sqlite3_value_text(argv[0]);
1597
+ if( zFile==0 ) return;
1598
+ out = fopen(zFile, "wb");
1599
+ if( out==0 ) return;
1600
+ z = (const char*)sqlite3_value_blob(argv[1]);
1601
+ if( z==0 ){
1602
+ rc = 0;
1603
+ }else{
1604
+ rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out);
1605
+ }
1606
+ fclose(out);
1607
+ sqlite3_result_int64(context, rc);
1608
+}
1609
+
1610
+
1611
+#ifdef _WIN32
1612
+__declspec(dllexport)
1613
+#endif
1614
+int sqlite3_fileio_init(
1615
+ sqlite3 *db,
1616
+ char **pzErrMsg,
1617
+ const sqlite3_api_routines *pApi
1618
+){
1619
+ int rc = SQLITE_OK;
1620
+ SQLITE_EXTENSION_INIT2(pApi);
1621
+ (void)pzErrMsg; /* Unused parameter */
1622
+ rc = sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
1623
+ readfileFunc, 0, 0);
1624
+ if( rc==SQLITE_OK ){
1625
+ rc = sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0,
1626
+ writefileFunc, 0, 0);
1627
+ }
1628
+ return rc;
1629
+}
1630
+
1631
+/************************* End ../ext/misc/fileio.c ********************/
1632
+/************************* Begin ../ext/misc/completion.c ******************/
1633
+/*
1634
+** 2017-07-10
1635
+**
1636
+** The author disclaims copyright to this source code. In place of
1637
+** a legal notice, here is a blessing:
1638
+**
1639
+** May you do good and not evil.
1640
+** May you find forgiveness for yourself and forgive others.
1641
+** May you share freely, never taking more than you give.
1642
+**
1643
+*************************************************************************
1644
+**
1645
+** This file implements an eponymous virtual table that returns suggested
1646
+** completions for a partial SQL input.
1647
+**
1648
+** Suggested usage:
1649
+**
1650
+** SELECT DISTINCT candidate COLLATE nocase
1651
+** FROM completion($prefix,$wholeline)
1652
+** ORDER BY 1;
1653
+**
1654
+** The two query parameters are optional. $prefix is the text of the
1655
+** current word being typed and that is to be completed. $wholeline is
1656
+** the complete input line, used for context.
1657
+**
1658
+** The raw completion() table might return the same candidate multiple
1659
+** times, for example if the same column name is used to two or more
1660
+** tables. And the candidates are returned in an arbitrary order. Hence,
1661
+** the DISTINCT and ORDER BY are recommended.
1662
+**
1663
+** This virtual table operates at the speed of human typing, and so there
1664
+** is no attempt to make it fast. Even a slow implementation will be much
1665
+** faster than any human can type.
1666
+**
1667
+*/
1668
+SQLITE_EXTENSION_INIT1
1669
+#include <assert.h>
1670
+#include <string.h>
1671
+#include <ctype.h>
1672
+
1673
+#ifndef SQLITE_OMIT_VIRTUALTABLE
1674
+
1675
+/* completion_vtab is a subclass of sqlite3_vtab which will
1676
+** serve as the underlying representation of a completion virtual table
1677
+*/
1678
+typedef struct completion_vtab completion_vtab;
1679
+struct completion_vtab {
1680
+ sqlite3_vtab base; /* Base class - must be first */
1681
+ sqlite3 *db; /* Database connection for this completion vtab */
1682
+};
1683
+
1684
+/* completion_cursor is a subclass of sqlite3_vtab_cursor which will
1685
+** serve as the underlying representation of a cursor that scans
1686
+** over rows of the result
1687
+*/
1688
+typedef struct completion_cursor completion_cursor;
1689
+struct completion_cursor {
1690
+ sqlite3_vtab_cursor base; /* Base class - must be first */
1691
+ sqlite3 *db; /* Database connection for this cursor */
1692
+ int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */
1693
+ char *zPrefix; /* The prefix for the word we want to complete */
1694
+ char *zLine; /* The whole that we want to complete */
1695
+ const char *zCurrentRow; /* Current output row */
1696
+ sqlite3_stmt *pStmt; /* Current statement */
1697
+ sqlite3_int64 iRowid; /* The rowid */
1698
+ int ePhase; /* Current phase */
1699
+ int j; /* inter-phase counter */
1700
+};
1701
+
1702
+/* Values for ePhase:
1703
+*/
1704
+#define COMPLETION_FIRST_PHASE 1
1705
+#define COMPLETION_KEYWORDS 1
1706
+#define COMPLETION_PRAGMAS 2
1707
+#define COMPLETION_FUNCTIONS 3
1708
+#define COMPLETION_COLLATIONS 4
1709
+#define COMPLETION_INDEXES 5
1710
+#define COMPLETION_TRIGGERS 6
1711
+#define COMPLETION_DATABASES 7
1712
+#define COMPLETION_TABLES 8
1713
+#define COMPLETION_COLUMNS 9
1714
+#define COMPLETION_MODULES 10
1715
+#define COMPLETION_EOF 11
1716
+
1717
+/*
1718
+** The completionConnect() method is invoked to create a new
1719
+** completion_vtab that describes the completion virtual table.
1720
+**
1721
+** Think of this routine as the constructor for completion_vtab objects.
1722
+**
1723
+** All this routine needs to do is:
1724
+**
1725
+** (1) Allocate the completion_vtab object and initialize all fields.
1726
+**
1727
+** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
1728
+** result set of queries against completion will look like.
1729
+*/
1730
+static int completionConnect(
1731
+ sqlite3 *db,
1732
+ void *pAux,
1733
+ int argc, const char *const*argv,
1734
+ sqlite3_vtab **ppVtab,
1735
+ char **pzErr
1736
+){
1737
+ completion_vtab *pNew;
1738
+ int rc;
1739
+
1740
+/* Column numbers */
1741
+#define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */
1742
+#define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */
1743
+#define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */
1744
+#define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */
1745
+
1746
+ rc = sqlite3_declare_vtab(db,
1747
+ "CREATE TABLE x("
1748
+ " candidate TEXT,"
1749
+ " prefix TEXT HIDDEN,"
1750
+ " wholeline TEXT HIDDEN,"
1751
+ " phase INT HIDDEN" /* Used for debugging only */
1752
+ ")");
1753
+ if( rc==SQLITE_OK ){
1754
+ pNew = sqlite3_malloc( sizeof(*pNew) );
1755
+ *ppVtab = (sqlite3_vtab*)pNew;
1756
+ if( pNew==0 ) return SQLITE_NOMEM;
1757
+ memset(pNew, 0, sizeof(*pNew));
1758
+ pNew->db = db;
1759
+ }
1760
+ return rc;
1761
+}
1762
+
1763
+/*
1764
+** This method is the destructor for completion_cursor objects.
1765
+*/
1766
+static int completionDisconnect(sqlite3_vtab *pVtab){
1767
+ sqlite3_free(pVtab);
1768
+ return SQLITE_OK;
1769
+}
1770
+
1771
+/*
1772
+** Constructor for a new completion_cursor object.
1773
+*/
1774
+static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
1775
+ completion_cursor *pCur;
1776
+ pCur = sqlite3_malloc( sizeof(*pCur) );
1777
+ if( pCur==0 ) return SQLITE_NOMEM;
1778
+ memset(pCur, 0, sizeof(*pCur));
1779
+ pCur->db = ((completion_vtab*)p)->db;
1780
+ *ppCursor = &pCur->base;
1781
+ return SQLITE_OK;
1782
+}
1783
+
1784
+/*
1785
+** Reset the completion_cursor.
1786
+*/
1787
+static void completionCursorReset(completion_cursor *pCur){
1788
+ sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0;
1789
+ sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0;
1790
+ sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
1791
+ pCur->j = 0;
1792
+}
1793
+
1794
+/*
1795
+** Destructor for a completion_cursor.
1796
+*/
1797
+static int completionClose(sqlite3_vtab_cursor *cur){
1798
+ completionCursorReset((completion_cursor*)cur);
1799
+ sqlite3_free(cur);
1800
+ return SQLITE_OK;
1801
+}
1802
+
1803
+/*
1804
+** All SQL keywords understood by SQLite
1805
+*/
1806
+static const char *completionKwrds[] = {
1807
+ "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS",
1808
+ "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY",
1809
+ "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
1810
+ "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE",
1811
+ "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
1812
+ "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH",
1813
+ "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN",
1814
+ "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF",
1815
+ "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER",
1816
+ "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY",
1817
+ "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL",
1818
+ "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA",
1819
+ "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP",
1820
+ "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT",
1821
+ "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
1822
+ "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE",
1823
+ "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE",
1824
+ "WITH", "WITHOUT",
1825
+};
1826
+
1827
+/*
1828
+** Advance a completion_cursor to its next row of output.
1829
+**
1830
+** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
1831
+** record the current state of the scan. This routine sets ->zCurrentRow
1832
+** to the current row of output and then returns. If no more rows remain,
1833
+** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
1834
+** table that has reached the end of its scan.
1835
+**
1836
+** The current implementation just lists potential identifiers and
1837
+** keywords and filters them by zPrefix. Future enhancements should
1838
+** take zLine into account to try to restrict the set of identifiers and
1839
+** keywords based on what would be legal at the current point of input.
1840
+*/
1841
+static int completionNext(sqlite3_vtab_cursor *cur){
1842
+ completion_cursor *pCur = (completion_cursor*)cur;
1843
+ int eNextPhase = 0; /* Next phase to try if current phase reaches end */
1844
+ int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */
1845
+ pCur->iRowid++;
1846
+ while( pCur->ePhase!=COMPLETION_EOF ){
1847
+ switch( pCur->ePhase ){
1848
+ case COMPLETION_KEYWORDS: {
1849
+ if( pCur->j >= sizeof(completionKwrds)/sizeof(completionKwrds[0]) ){
1850
+ pCur->zCurrentRow = 0;
1851
+ pCur->ePhase = COMPLETION_DATABASES;
1852
+ }else{
1853
+ pCur->zCurrentRow = completionKwrds[pCur->j++];
1854
+ }
1855
+ iCol = -1;
1856
+ break;
1857
+ }
1858
+ case COMPLETION_DATABASES: {
1859
+ if( pCur->pStmt==0 ){
1860
+ sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
1861
+ &pCur->pStmt, 0);
1862
+ }
1863
+ iCol = 1;
1864
+ eNextPhase = COMPLETION_TABLES;
1865
+ break;
1866
+ }
1867
+ case COMPLETION_TABLES: {
1868
+ if( pCur->pStmt==0 ){
1869
+ sqlite3_stmt *pS2;
1870
+ char *zSql = 0;
1871
+ const char *zSep = "";
1872
+ sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
1873
+ while( sqlite3_step(pS2)==SQLITE_ROW ){
1874
+ const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
1875
+ zSql = sqlite3_mprintf(
1876
+ "%z%s"
1877
+ "SELECT name FROM \"%w\".sqlite_master"
1878
+ " WHERE type='table'",
1879
+ zSql, zSep, zDb
1880
+ );
1881
+ if( zSql==0 ) return SQLITE_NOMEM;
1882
+ zSep = " UNION ";
1883
+ }
1884
+ sqlite3_finalize(pS2);
1885
+ sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
1886
+ sqlite3_free(zSql);
1887
+ }
1888
+ iCol = 0;
1889
+ eNextPhase = COMPLETION_COLUMNS;
1890
+ break;
1891
+ }
1892
+ case COMPLETION_COLUMNS: {
1893
+ if( pCur->pStmt==0 ){
1894
+ sqlite3_stmt *pS2;
1895
+ char *zSql = 0;
1896
+ const char *zSep = "";
1897
+ sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
1898
+ while( sqlite3_step(pS2)==SQLITE_ROW ){
1899
+ const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
1900
+ zSql = sqlite3_mprintf(
1901
+ "%z%s"
1902
+ "SELECT pti.name FROM \"%w\".sqlite_master AS sm"
1903
+ " JOIN pragma_table_info(sm.name,%Q) AS pti"
1904
+ " WHERE sm.type='table'",
1905
+ zSql, zSep, zDb, zDb
1906
+ );
1907
+ if( zSql==0 ) return SQLITE_NOMEM;
1908
+ zSep = " UNION ";
1909
+ }
1910
+ sqlite3_finalize(pS2);
1911
+ sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
1912
+ sqlite3_free(zSql);
1913
+ }
1914
+ iCol = 0;
1915
+ eNextPhase = COMPLETION_EOF;
1916
+ break;
1917
+ }
1918
+ }
1919
+ if( iCol<0 ){
1920
+ /* This case is when the phase presets zCurrentRow */
1921
+ if( pCur->zCurrentRow==0 ) continue;
1922
+ }else{
1923
+ if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
1924
+ /* Extract the next row of content */
1925
+ pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
1926
+ }else{
1927
+ /* When all rows are finished, advance to the next phase */
1928
+ sqlite3_finalize(pCur->pStmt);
1929
+ pCur->pStmt = 0;
1930
+ pCur->ePhase = eNextPhase;
1931
+ continue;
1932
+ }
1933
+ }
1934
+ if( pCur->nPrefix==0 ) break;
1935
+ if( sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0 ){
1936
+ break;
1937
+ }
1938
+ }
1939
+
1940
+ return SQLITE_OK;
1941
+}
1942
+
1943
+/*
1944
+** Return values of columns for the row at which the completion_cursor
1945
+** is currently pointing.
1946
+*/
1947
+static int completionColumn(
1948
+ sqlite3_vtab_cursor *cur, /* The cursor */
1949
+ sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
1950
+ int i /* Which column to return */
1951
+){
1952
+ completion_cursor *pCur = (completion_cursor*)cur;
1953
+ switch( i ){
1954
+ case COMPLETION_COLUMN_CANDIDATE: {
1955
+ sqlite3_result_text(ctx, pCur->zCurrentRow, -1, SQLITE_TRANSIENT);
1956
+ break;
1957
+ }
1958
+ case COMPLETION_COLUMN_PREFIX: {
1959
+ sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
1960
+ break;
1961
+ }
1962
+ case COMPLETION_COLUMN_WHOLELINE: {
1963
+ sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
1964
+ break;
1965
+ }
1966
+ case COMPLETION_COLUMN_PHASE: {
1967
+ sqlite3_result_int(ctx, pCur->ePhase);
1968
+ break;
1969
+ }
1970
+ }
1971
+ return SQLITE_OK;
1972
+}
1973
+
1974
+/*
1975
+** Return the rowid for the current row. In this implementation, the
1976
+** rowid is the same as the output value.
1977
+*/
1978
+static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
1979
+ completion_cursor *pCur = (completion_cursor*)cur;
1980
+ *pRowid = pCur->iRowid;
1981
+ return SQLITE_OK;
1982
+}
1983
+
1984
+/*
1985
+** Return TRUE if the cursor has been moved off of the last
1986
+** row of output.
1987
+*/
1988
+static int completionEof(sqlite3_vtab_cursor *cur){
1989
+ completion_cursor *pCur = (completion_cursor*)cur;
1990
+ return pCur->ePhase >= COMPLETION_EOF;
1991
+}
1992
+
1993
+/*
1994
+** This method is called to "rewind" the completion_cursor object back
1995
+** to the first row of output. This method is always called at least
1996
+** once prior to any call to completionColumn() or completionRowid() or
1997
+** completionEof().
1998
+*/
1999
+static int completionFilter(
2000
+ sqlite3_vtab_cursor *pVtabCursor,
2001
+ int idxNum, const char *idxStr,
2002
+ int argc, sqlite3_value **argv
2003
+){
2004
+ completion_cursor *pCur = (completion_cursor *)pVtabCursor;
2005
+ int iArg = 0;
2006
+ completionCursorReset(pCur);
2007
+ if( idxNum & 1 ){
2008
+ pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
2009
+ if( pCur->nPrefix>0 ){
2010
+ pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
2011
+ if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
2012
+ }
2013
+ iArg++;
2014
+ }
2015
+ if( idxNum & 2 ){
2016
+ pCur->nLine = sqlite3_value_bytes(argv[iArg]);
2017
+ if( pCur->nLine>0 ){
2018
+ pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
2019
+ if( pCur->zLine==0 ) return SQLITE_NOMEM;
2020
+ }
2021
+ iArg++;
2022
+ }
2023
+ if( pCur->zLine!=0 && pCur->zPrefix==0 ){
2024
+ int i = pCur->nLine;
2025
+ while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
2026
+ i--;
2027
+ }
2028
+ pCur->nPrefix = pCur->nLine - i;
2029
+ if( pCur->nPrefix>0 ){
2030
+ pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
2031
+ if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
2032
+ }
2033
+ }
2034
+ pCur->iRowid = 0;
2035
+ pCur->ePhase = COMPLETION_FIRST_PHASE;
2036
+ return completionNext(pVtabCursor);
2037
+}
2038
+
2039
+/*
2040
+** SQLite will invoke this method one or more times while planning a query
2041
+** that uses the completion virtual table. This routine needs to create
2042
+** a query plan for each invocation and compute an estimated cost for that
2043
+** plan.
2044
+**
2045
+** There are two hidden parameters that act as arguments to the table-valued
2046
+** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix"
2047
+** is available and bit 1 is set if "wholeline" is available.
2048
+*/
2049
+static int completionBestIndex(
2050
+ sqlite3_vtab *tab,
2051
+ sqlite3_index_info *pIdxInfo
2052
+){
2053
+ int i; /* Loop over constraints */
2054
+ int idxNum = 0; /* The query plan bitmask */
2055
+ int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */
2056
+ int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
2057
+ int nArg = 0; /* Number of arguments that completeFilter() expects */
2058
+ const struct sqlite3_index_constraint *pConstraint;
2059
+
2060
+ pConstraint = pIdxInfo->aConstraint;
2061
+ for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2062
+ if( pConstraint->usable==0 ) continue;
2063
+ if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2064
+ switch( pConstraint->iColumn ){
2065
+ case COMPLETION_COLUMN_PREFIX:
2066
+ prefixIdx = i;
2067
+ idxNum |= 1;
2068
+ break;
2069
+ case COMPLETION_COLUMN_WHOLELINE:
2070
+ wholelineIdx = i;
2071
+ idxNum |= 2;
2072
+ break;
2073
+ }
2074
+ }
2075
+ if( prefixIdx>=0 ){
2076
+ pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
2077
+ pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
2078
+ }
2079
+ if( wholelineIdx>=0 ){
2080
+ pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
2081
+ pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
2082
+ }
2083
+ pIdxInfo->idxNum = idxNum;
2084
+ pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
2085
+ pIdxInfo->estimatedRows = 500 - 100*nArg;
2086
+ return SQLITE_OK;
2087
+}
2088
+
2089
+/*
2090
+** This following structure defines all the methods for the
2091
+** completion virtual table.
2092
+*/
2093
+static sqlite3_module completionModule = {
2094
+ 0, /* iVersion */
2095
+ 0, /* xCreate */
2096
+ completionConnect, /* xConnect */
2097
+ completionBestIndex, /* xBestIndex */
2098
+ completionDisconnect, /* xDisconnect */
2099
+ 0, /* xDestroy */
2100
+ completionOpen, /* xOpen - open a cursor */
2101
+ completionClose, /* xClose - close a cursor */
2102
+ completionFilter, /* xFilter - configure scan constraints */
2103
+ completionNext, /* xNext - advance a cursor */
2104
+ completionEof, /* xEof - check for end of scan */
2105
+ completionColumn, /* xColumn - read data */
2106
+ completionRowid, /* xRowid - read data */
2107
+ 0, /* xUpdate */
2108
+ 0, /* xBegin */
2109
+ 0, /* xSync */
2110
+ 0, /* xCommit */
2111
+ 0, /* xRollback */
2112
+ 0, /* xFindMethod */
2113
+ 0, /* xRename */
2114
+};
2115
+
2116
+#endif /* SQLITE_OMIT_VIRTUALTABLE */
2117
+
2118
+int sqlite3CompletionVtabInit(sqlite3 *db){
2119
+ int rc = SQLITE_OK;
2120
+#ifndef SQLITE_OMIT_VIRTUALTABLE
2121
+ rc = sqlite3_create_module(db, "completion", &completionModule, 0);
2122
+#endif
2123
+ return rc;
2124
+}
2125
+
2126
+#ifdef _WIN32
2127
+__declspec(dllexport)
2128
+#endif
2129
+int sqlite3_completion_init(
2130
+ sqlite3 *db,
2131
+ char **pzErrMsg,
2132
+ const sqlite3_api_routines *pApi
2133
+){
2134
+ int rc = SQLITE_OK;
2135
+ SQLITE_EXTENSION_INIT2(pApi);
2136
+#ifndef SQLITE_OMIT_VIRTUALTABLE
2137
+ rc = sqlite3CompletionVtabInit(db);
2138
+#endif
2139
+ return rc;
2140
+}
2141
+
2142
+/************************* End ../ext/misc/completion.c ********************/
14462143
14472144
#if defined(SQLITE_ENABLE_SESSION)
14482145
/*
14492146
** State information for a single open session
14502147
*/
@@ -1521,12 +2218,13 @@
15212218
#define SHFLG_Scratch 0x00000001 /* The --scratch option is used */
15222219
#define SHFLG_Pagecache 0x00000002 /* The --pagecache option is used */
15232220
#define SHFLG_Lookaside 0x00000004 /* Lookaside memory is used */
15242221
#define SHFLG_Backslash 0x00000008 /* The --backslash option is used */
15252222
#define SHFLG_PreserveRowid 0x00000010 /* .dump preserves rowid values */
1526
-#define SHFLG_CountChanges 0x00000020 /* .changes setting */
1527
-#define SHFLG_Echo 0x00000040 /* .echo or --echo setting */
2223
+#define SHFLG_Newlines 0x00000020 /* .dump --newline flag */
2224
+#define SHFLG_CountChanges 0x00000040 /* .changes setting */
2225
+#define SHFLG_Echo 0x00000080 /* .echo or --echo setting */
15282226
15292227
/*
15302228
** Macros for testing and setting shellFlgs
15312229
*/
15322230
#define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
@@ -2193,11 +2891,15 @@
21932891
for(i=0; i<nArg; i++){
21942892
raw_printf(p->out, i>0 ? "," : " VALUES(");
21952893
if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
21962894
utf8_printf(p->out,"NULL");
21972895
}else if( aiType && aiType[i]==SQLITE_TEXT ){
2198
- output_quoted_escaped_string(p->out, azArg[i]);
2896
+ if( ShellHasFlag(p, SHFLG_Newlines) ){
2897
+ output_quoted_string(p->out, azArg[i]);
2898
+ }else{
2899
+ output_quoted_escaped_string(p->out, azArg[i]);
2900
+ }
21992901
}else if( aiType && aiType[i]==SQLITE_INTEGER ){
22002902
utf8_printf(p->out,"%s", azArg[i]);
22012903
}else if( aiType && aiType[i]==SQLITE_FLOAT ){
22022904
char z[50];
22032905
double r = sqlite3_column_double(p->pStmt, i);
@@ -2207,10 +2909,12 @@
22072909
const void *pBlob = sqlite3_column_blob(p->pStmt, i);
22082910
int nBlob = sqlite3_column_bytes(p->pStmt, i);
22092911
output_hex_blob(p->out, pBlob, nBlob);
22102912
}else if( isNumber(azArg[i], 0) ){
22112913
utf8_printf(p->out,"%s", azArg[i]);
2914
+ }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2915
+ output_quoted_string(p->out, azArg[i]);
22122916
}else{
22132917
output_quoted_escaped_string(p->out, azArg[i]);
22142918
}
22152919
}
22162920
raw_printf(p->out,");\n");
@@ -3325,11 +4029,13 @@
33254029
" If TABLE specified, only dump tables matching\n"
33264030
" LIKE pattern TABLE.\n"
33274031
".echo on|off Turn command echo on or off\n"
33284032
".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n"
33294033
".exit Exit this program\n"
3330
- ".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"
4034
+/* Because explain mode comes on automatically now, the ".explain" mode
4035
+** is removed from the help screen. It is still supported for legacy, however */
4036
+/*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"*/
33314037
".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n"
33324038
".headers on|off Turn display of headers on or off\n"
33334039
".help Show this message\n"
33344040
".import FILE TABLE Import data from FILE into TABLE\n"
33354041
#ifndef SQLITE_OMIT_TEST_CONTROL
@@ -3460,62 +4166,10 @@
34604166
pBuf[nIn] = 0;
34614167
if( pnByte ) *pnByte = nIn;
34624168
return pBuf;
34634169
}
34644170
3465
-/*
3466
-** Implementation of the "readfile(X)" SQL function. The entire content
3467
-** of the file named X is read and returned as a BLOB. NULL is returned
3468
-** if the file does not exist or is unreadable.
3469
-*/
3470
-static void readfileFunc(
3471
- sqlite3_context *context,
3472
- int argc,
3473
- sqlite3_value **argv
3474
-){
3475
- const char *zName;
3476
- void *pBuf;
3477
- int nBuf;
3478
-
3479
- UNUSED_PARAMETER(argc);
3480
- zName = (const char*)sqlite3_value_text(argv[0]);
3481
- if( zName==0 ) return;
3482
- pBuf = readFile(zName, &nBuf);
3483
- if( pBuf ) sqlite3_result_blob(context, pBuf, nBuf, sqlite3_free);
3484
-}
3485
-
3486
-/*
3487
-** Implementation of the "writefile(X,Y)" SQL function. The argument Y
3488
-** is written into file X. The number of bytes written is returned. Or
3489
-** NULL is returned if something goes wrong, such as being unable to open
3490
-** file X for writing.
3491
-*/
3492
-static void writefileFunc(
3493
- sqlite3_context *context,
3494
- int argc,
3495
- sqlite3_value **argv
3496
-){
3497
- FILE *out;
3498
- const char *z;
3499
- sqlite3_int64 rc;
3500
- const char *zFile;
3501
-
3502
- UNUSED_PARAMETER(argc);
3503
- zFile = (const char*)sqlite3_value_text(argv[0]);
3504
- if( zFile==0 ) return;
3505
- out = fopen(zFile, "wb");
3506
- if( out==0 ) return;
3507
- z = (const char*)sqlite3_value_blob(argv[1]);
3508
- if( z==0 ){
3509
- rc = 0;
3510
- }else{
3511
- rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out);
3512
- }
3513
- fclose(out);
3514
- sqlite3_result_int64(context, rc);
3515
-}
3516
-
35174171
#if defined(SQLITE_ENABLE_SESSION)
35184172
/*
35194173
** Close a single OpenSession object and release all of its associated
35204174
** resources.
35214175
*/
@@ -3577,22 +4231,76 @@
35774231
exit(1);
35784232
}
35794233
#ifndef SQLITE_OMIT_LOAD_EXTENSION
35804234
sqlite3_enable_load_extension(p->db, 1);
35814235
#endif
3582
- sqlite3_create_function(p->db, "readfile", 1, SQLITE_UTF8, 0,
3583
- readfileFunc, 0, 0);
3584
- sqlite3_create_function(p->db, "writefile", 2, SQLITE_UTF8, 0,
3585
- writefileFunc, 0, 0);
3586
- sqlite3_create_function(p->db, "sha3", 1, SQLITE_UTF8, 0,
3587
- sha3Func, 0, 0);
3588
- sqlite3_create_function(p->db, "sha3", 2, SQLITE_UTF8, 0,
3589
- sha3Func, 0, 0);
3590
- sqlite3_create_function(p->db, "sha3_query", 1, SQLITE_UTF8, 0,
3591
- sha3QueryFunc, 0, 0);
3592
- sqlite3_create_function(p->db, "sha3_query", 2, SQLITE_UTF8, 0,
3593
- sha3QueryFunc, 0, 0);
4236
+ sqlite3_fileio_init(p->db, 0, 0);
4237
+ sqlite3_shathree_init(p->db, 0, 0);
4238
+ sqlite3_completion_init(p->db, 0, 0);
35944239
sqlite3_create_function(p->db, "shell_add_schema", 2, SQLITE_UTF8, 0,
35954240
shellAddSchemaName, 0, 0);
3596
-
35974241
}
35984242
}
4243
+
4244
+#if HAVE_READLINE || HAVE_EDITLINE
4245
+/*
4246
+** Readline completion callbacks
4247
+*/
4248
+static char *readline_completion_generator(const char *text, int state){
4249
+ static sqlite3_stmt *pStmt = 0;
4250
+ char *zRet;
4251
+ if( state==0 ){
4252
+ char *zSql;
4253
+ int rc;
4254
+ sqlite3_finalize(pStmt);
4255
+ zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4256
+ " FROM completion(%Q) ORDER BY 1", text);
4257
+ sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4258
+ sqlite3_free(zSql);
4259
+ }
4260
+ if( sqlite3_step(pStmt)==SQLITE_ROW ){
4261
+ zRet = strdup(sqlite3_column_text(pStmt, 0));
4262
+ }else{
4263
+ sqlite3_finalize(pStmt);
4264
+ pStmt = 0;
4265
+ zRet = 0;
4266
+ }
4267
+ return zRet;
4268
+}
4269
+static char **readline_completion(const char *zText, int iStart, int iEnd){
4270
+ rl_attempted_completion_over = 1;
4271
+ return rl_completion_matches(zText, readline_completion_generator);
4272
+}
4273
+
4274
+#elif HAVE_LINENOISE
4275
+/*
4276
+** Linenoise completion callback
4277
+*/
4278
+static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
4279
+ int nLine = (int)strlen(zLine);
4280
+ int i, iStart;
4281
+ sqlite3_stmt *pStmt = 0;
4282
+ char *zSql;
4283
+ char zBuf[1000];
4284
+
4285
+ if( nLine>sizeof(zBuf)-30 ) return;
4286
+ if( zLine[0]=='.' ) return;
4287
+ for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
4288
+ if( i==nLine-1 ) return;
4289
+ iStart = i+1;
4290
+ memcpy(zBuf, zLine, iStart);
4291
+ zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4292
+ " FROM completion(%Q,%Q) ORDER BY 1",
4293
+ &zLine[iStart], zLine);
4294
+ sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4295
+ sqlite3_free(zSql);
4296
+ sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
4297
+ while( sqlite3_step(pStmt)==SQLITE_ROW ){
4298
+ const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
4299
+ int nCompletion = sqlite3_column_bytes(pStmt, 0);
4300
+ if( iStart+nCompletion < sizeof(zBuf)-1 ){
4301
+ memcpy(zBuf+iStart, zCompletion, nCompletion+1);
4302
+ linenoiseAddCompletion(lc, zBuf);
4303
+ }
4304
+ }
4305
+ sqlite3_finalize(pStmt);
4306
+}
@@ -3599,5 +4307,6 @@
4307
+#endif
35994308
36004309
/*
36014310
** Do C-language style dequoting.
36024311
**
36034312
** \a -> alarm
@@ -4926,11 +5635,11 @@
49265635
49275636
if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
49285637
const char *zLike = 0;
49295638
int i;
49305639
int savedShowHeader = p->showHeader;
4931
- ShellClearFlag(p, SHFLG_PreserveRowid);
5640
+ ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines);
49325641
for(i=1; i<nArg; i++){
49335642
if( azArg[i][0]=='-' ){
49345643
const char *z = azArg[i]+1;
49355644
if( z[0]=='-' ) z++;
49365645
if( strcmp(z,"preserve-rowids")==0 ){
@@ -4941,17 +5650,21 @@
49415650
goto meta_command_exit;
49425651
#else
49435652
ShellSetFlag(p, SHFLG_PreserveRowid);
49445653
#endif
49455654
}else
5655
+ if( strcmp(z,"newlines")==0 ){
5656
+ ShellSetFlag(p, SHFLG_Newlines);
5657
+ }else
49465658
{
49475659
raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
49485660
rc = 1;
49495661
goto meta_command_exit;
49505662
}
49515663
}else if( zLike ){
4952
- raw_printf(stderr, "Usage: .dump ?--preserve-rowids? ?LIKE-PATTERN?\n");
5664
+ raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
5665
+ "?--newlines? ?LIKE-PATTERN?\n");
49535666
rc = 1;
49545667
goto meta_command_exit;
49555668
}else{
49565669
zLike = azArg[i];
49575670
}
@@ -5033,10 +5746,12 @@
50335746
if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
50345747
if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
50355748
rc = 2;
50365749
}else
50375750
5751
+ /* The ".explain" command is automatic now. It is largely pointless. It
5752
+ ** retained purely for backwards compatibility */
50385753
if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
50395754
int val = 1;
50405755
if( nArg>=2 ){
50415756
if( strcmp(azArg[1],"auto")==0 ){
50425757
val = 99;
@@ -5549,11 +6264,13 @@
55496264
p->mode = MODE_Quote;
55506265
}else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
55516266
p->mode = MODE_Ascii;
55526267
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
55536268
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
5554
- }else {
6269
+ }else if( nArg==1 ){
6270
+ raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
6271
+ }else{
55556272
raw_printf(stderr, "Error: mode should be one of: "
55566273
"ascii column csv html insert line list quote tabs tcl\n");
55576274
rc = 1;
55586275
}
55596276
p->cMode = p->mode;
@@ -5821,12 +6538,18 @@
58216538
rc = 1;
58226539
goto meta_command_exit;
58236540
}
58246541
if( zDiv ){
58256542
sqlite3_stmt *pStmt = 0;
5826
- sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
5827
- -1, &pStmt, 0);
6543
+ rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
6544
+ -1, &pStmt, 0);
6545
+ if( rc ){
6546
+ utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
6547
+ sqlite3_finalize(pStmt);
6548
+ rc = 1;
6549
+ goto meta_command_exit;
6550
+ }
58286551
appendText(&sSelect, "SELECT sql FROM", 0);
58296552
iSchema = 0;
58306553
while( sqlite3_step(pStmt)==SQLITE_ROW ){
58316554
const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
58326555
char zScNum[30];
@@ -7179,10 +7902,11 @@
71797902
" -multiplex enable the multiplexor VFS\n"
71807903
#endif
71817904
" -newline SEP set output row separator. Default: '\\n'\n"
71827905
" -nullvalue TEXT set text string for NULL values. Default ''\n"
71837906
" -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
7907
+ " -quote set output mode to 'quote'\n"
71847908
" -scratch SIZE N use N slots of SZ bytes each for scratch memory\n"
71857909
" -separator SEP set output column separator. Default: '|'\n"
71867910
" -stats print memory stats before each finalize\n"
71877911
" -version show SQLite version\n"
71887912
" -vfs NAME use NAME as the default VFS\n"
@@ -7474,10 +8198,12 @@
74748198
i++;
74758199
}else if( strcmp(z,"-html")==0 ){
74768200
data.mode = MODE_Html;
74778201
}else if( strcmp(z,"-list")==0 ){
74788202
data.mode = MODE_List;
8203
+ }else if( strcmp(z,"-quote")==0 ){
8204
+ data.mode = MODE_Quote;
74798205
}else if( strcmp(z,"-line")==0 ){
74808206
data.mode = MODE_Line;
74818207
}else if( strcmp(z,"-column")==0 ){
74828208
data.mode = MODE_Column;
74838209
}else if( strcmp(z,"-csv")==0 ){
@@ -7625,10 +8351,15 @@
76258351
if( (zHistory = malloc(nHistory))!=0 ){
76268352
sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
76278353
}
76288354
}
76298355
if( zHistory ){ shell_read_history(zHistory); }
8356
+#if HAVE_READLINE || HAVE_EDITLINE
8357
+ rl_attempted_completion_function = readline_completion;
8358
+#elif HAVE_LINENOISE
8359
+ linenoiseSetCompletionCallback(linenoise_completion);
8360
+#endif
76308361
rc = process_input(&data, 0);
76318362
if( zHistory ){
76328363
shell_stifle_history(100);
76338364
shell_write_history(zHistory);
76348365
free(zHistory);
@@ -7648,5 +8379,6 @@
76488379
for(i=0; i<argc; i++) sqlite3_free(argv[i]);
76498380
sqlite3_free(argv);
76508381
#endif
76518382
return rc;
76528383
}
8384
+
76538385
--- src/shell.c
+++ src/shell.c
@@ -1,5 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
@@ -782,14 +800,57 @@
782 }
783 }
784 sqlite3_result_value(pCtx, apVal[0]);
785 }
786
787 /******************************************************************************
788 ** SHA3 hash implementation copied from ../ext/misc/shathree.c
 
 
 
789 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
790 typedef sqlite3_uint64 u64;
 
 
 
 
791 /*
792 ** Macros to determine whether the machine is big or little endian,
793 ** and whether or not that determination is run-time or compile-time.
794 **
795 ** For best performance, an attempt is made to guess at the byte-order
@@ -823,14 +884,10 @@
823 unsigned nRate; /* Bytes of input accepted per Keccak iteration */
824 unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
825 unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
826 };
827
828 /* Allow the following routine to use the B0 variable, which is also
829 ** a macro in the termios.h header file */
830 #undef B0
831
832 /*
833 ** A single step of the Keccak mixing function for a 1600-bit state
834 */
835 static void KeccakF1600Step(SHA3Context *p){
836 int i;
@@ -1237,16 +1294,18 @@
1237 for(i=0; i<p->nRate; i++){
1238 p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1239 }
1240 return &p->u.x[p->nRate];
1241 }
 
 
1242
1243 /*
1244 ** Implementation of the sha3(X,SIZE) function.
1245 **
1246 ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
1247 ** size is 256. If X is a BLOB, it is hashed as is.
1248 ** For all other non-NULL types of input, X is converted into a UTF-8 string
1249 ** and the string is hashed without the trailing 0x00 terminator. The hash
1250 ** of a NULL value is NULL.
1251 */
1252 static void sha3Func(
@@ -1373,14 +1432,10 @@
1373 sqlite3_free(zMsg);
1374 return;
1375 }
1376 nCol = sqlite3_column_count(pStmt);
1377 z = sqlite3_sql(pStmt);
1378 if( z==0 ){
1379 sqlite3_finalize(pStmt);
1380 continue;
1381 }
1382 n = (int)strlen(z);
1383 hash_step_vformat(&cx,"S%d:",n);
1384 SHA3Update(&cx,(unsigned char*)z,n);
1385
1386 /* Compute a hash over the result of the query */
@@ -1439,12 +1494,654 @@
1439 }
1440 sqlite3_finalize(pStmt);
1441 }
1442 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1443 }
1444 /* End of SHA3 hashing logic copy/pasted from ../ext/misc/shathree.c
1445 ********************************************************************************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1446
1447 #if defined(SQLITE_ENABLE_SESSION)
1448 /*
1449 ** State information for a single open session
1450 */
@@ -1521,12 +2218,13 @@
1521 #define SHFLG_Scratch 0x00000001 /* The --scratch option is used */
1522 #define SHFLG_Pagecache 0x00000002 /* The --pagecache option is used */
1523 #define SHFLG_Lookaside 0x00000004 /* Lookaside memory is used */
1524 #define SHFLG_Backslash 0x00000008 /* The --backslash option is used */
1525 #define SHFLG_PreserveRowid 0x00000010 /* .dump preserves rowid values */
1526 #define SHFLG_CountChanges 0x00000020 /* .changes setting */
1527 #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */
 
1528
1529 /*
1530 ** Macros for testing and setting shellFlgs
1531 */
1532 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
@@ -2193,11 +2891,15 @@
2193 for(i=0; i<nArg; i++){
2194 raw_printf(p->out, i>0 ? "," : " VALUES(");
2195 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2196 utf8_printf(p->out,"NULL");
2197 }else if( aiType && aiType[i]==SQLITE_TEXT ){
2198 output_quoted_escaped_string(p->out, azArg[i]);
 
 
 
 
2199 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2200 utf8_printf(p->out,"%s", azArg[i]);
2201 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2202 char z[50];
2203 double r = sqlite3_column_double(p->pStmt, i);
@@ -2207,10 +2909,12 @@
2207 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2208 int nBlob = sqlite3_column_bytes(p->pStmt, i);
2209 output_hex_blob(p->out, pBlob, nBlob);
2210 }else if( isNumber(azArg[i], 0) ){
2211 utf8_printf(p->out,"%s", azArg[i]);
 
 
2212 }else{
2213 output_quoted_escaped_string(p->out, azArg[i]);
2214 }
2215 }
2216 raw_printf(p->out,");\n");
@@ -3325,11 +4029,13 @@
3325 " If TABLE specified, only dump tables matching\n"
3326 " LIKE pattern TABLE.\n"
3327 ".echo on|off Turn command echo on or off\n"
3328 ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n"
3329 ".exit Exit this program\n"
3330 ".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"
 
 
3331 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n"
3332 ".headers on|off Turn display of headers on or off\n"
3333 ".help Show this message\n"
3334 ".import FILE TABLE Import data from FILE into TABLE\n"
3335 #ifndef SQLITE_OMIT_TEST_CONTROL
@@ -3460,62 +4166,10 @@
3460 pBuf[nIn] = 0;
3461 if( pnByte ) *pnByte = nIn;
3462 return pBuf;
3463 }
3464
3465 /*
3466 ** Implementation of the "readfile(X)" SQL function. The entire content
3467 ** of the file named X is read and returned as a BLOB. NULL is returned
3468 ** if the file does not exist or is unreadable.
3469 */
3470 static void readfileFunc(
3471 sqlite3_context *context,
3472 int argc,
3473 sqlite3_value **argv
3474 ){
3475 const char *zName;
3476 void *pBuf;
3477 int nBuf;
3478
3479 UNUSED_PARAMETER(argc);
3480 zName = (const char*)sqlite3_value_text(argv[0]);
3481 if( zName==0 ) return;
3482 pBuf = readFile(zName, &nBuf);
3483 if( pBuf ) sqlite3_result_blob(context, pBuf, nBuf, sqlite3_free);
3484 }
3485
3486 /*
3487 ** Implementation of the "writefile(X,Y)" SQL function. The argument Y
3488 ** is written into file X. The number of bytes written is returned. Or
3489 ** NULL is returned if something goes wrong, such as being unable to open
3490 ** file X for writing.
3491 */
3492 static void writefileFunc(
3493 sqlite3_context *context,
3494 int argc,
3495 sqlite3_value **argv
3496 ){
3497 FILE *out;
3498 const char *z;
3499 sqlite3_int64 rc;
3500 const char *zFile;
3501
3502 UNUSED_PARAMETER(argc);
3503 zFile = (const char*)sqlite3_value_text(argv[0]);
3504 if( zFile==0 ) return;
3505 out = fopen(zFile, "wb");
3506 if( out==0 ) return;
3507 z = (const char*)sqlite3_value_blob(argv[1]);
3508 if( z==0 ){
3509 rc = 0;
3510 }else{
3511 rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out);
3512 }
3513 fclose(out);
3514 sqlite3_result_int64(context, rc);
3515 }
3516
3517 #if defined(SQLITE_ENABLE_SESSION)
3518 /*
3519 ** Close a single OpenSession object and release all of its associated
3520 ** resources.
3521 */
@@ -3577,22 +4231,76 @@
3577 exit(1);
3578 }
3579 #ifndef SQLITE_OMIT_LOAD_EXTENSION
3580 sqlite3_enable_load_extension(p->db, 1);
3581 #endif
3582 sqlite3_create_function(p->db, "readfile", 1, SQLITE_UTF8, 0,
3583 readfileFunc, 0, 0);
3584 sqlite3_create_function(p->db, "writefile", 2, SQLITE_UTF8, 0,
3585 writefileFunc, 0, 0);
3586 sqlite3_create_function(p->db, "sha3", 1, SQLITE_UTF8, 0,
3587 sha3Func, 0, 0);
3588 sqlite3_create_function(p->db, "sha3", 2, SQLITE_UTF8, 0,
3589 sha3Func, 0, 0);
3590 sqlite3_create_function(p->db, "sha3_query", 1, SQLITE_UTF8, 0,
3591 sha3QueryFunc, 0, 0);
3592 sqlite3_create_function(p->db, "sha3_query", 2, SQLITE_UTF8, 0,
3593 sha3QueryFunc, 0, 0);
3594 sqlite3_create_function(p->db, "shell_add_schema", 2, SQLITE_UTF8, 0,
3595 shellAddSchemaName, 0, 0);
3596
3597 }
3598 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
@@ -3599,5 +4307,6 @@
 
3599
3600 /*
3601 ** Do C-language style dequoting.
3602 **
3603 ** \a -> alarm
@@ -4926,11 +5635,11 @@
4926
4927 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
4928 const char *zLike = 0;
4929 int i;
4930 int savedShowHeader = p->showHeader;
4931 ShellClearFlag(p, SHFLG_PreserveRowid);
4932 for(i=1; i<nArg; i++){
4933 if( azArg[i][0]=='-' ){
4934 const char *z = azArg[i]+1;
4935 if( z[0]=='-' ) z++;
4936 if( strcmp(z,"preserve-rowids")==0 ){
@@ -4941,17 +5650,21 @@
4941 goto meta_command_exit;
4942 #else
4943 ShellSetFlag(p, SHFLG_PreserveRowid);
4944 #endif
4945 }else
 
 
 
4946 {
4947 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
4948 rc = 1;
4949 goto meta_command_exit;
4950 }
4951 }else if( zLike ){
4952 raw_printf(stderr, "Usage: .dump ?--preserve-rowids? ?LIKE-PATTERN?\n");
 
4953 rc = 1;
4954 goto meta_command_exit;
4955 }else{
4956 zLike = azArg[i];
4957 }
@@ -5033,10 +5746,12 @@
5033 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
5034 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
5035 rc = 2;
5036 }else
5037
 
 
5038 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
5039 int val = 1;
5040 if( nArg>=2 ){
5041 if( strcmp(azArg[1],"auto")==0 ){
5042 val = 99;
@@ -5549,11 +6264,13 @@
5549 p->mode = MODE_Quote;
5550 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
5551 p->mode = MODE_Ascii;
5552 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
5553 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
5554 }else {
 
 
5555 raw_printf(stderr, "Error: mode should be one of: "
5556 "ascii column csv html insert line list quote tabs tcl\n");
5557 rc = 1;
5558 }
5559 p->cMode = p->mode;
@@ -5821,12 +6538,18 @@
5821 rc = 1;
5822 goto meta_command_exit;
5823 }
5824 if( zDiv ){
5825 sqlite3_stmt *pStmt = 0;
5826 sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
5827 -1, &pStmt, 0);
 
 
 
 
 
 
5828 appendText(&sSelect, "SELECT sql FROM", 0);
5829 iSchema = 0;
5830 while( sqlite3_step(pStmt)==SQLITE_ROW ){
5831 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
5832 char zScNum[30];
@@ -7179,10 +7902,11 @@
7179 " -multiplex enable the multiplexor VFS\n"
7180 #endif
7181 " -newline SEP set output row separator. Default: '\\n'\n"
7182 " -nullvalue TEXT set text string for NULL values. Default ''\n"
7183 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
 
7184 " -scratch SIZE N use N slots of SZ bytes each for scratch memory\n"
7185 " -separator SEP set output column separator. Default: '|'\n"
7186 " -stats print memory stats before each finalize\n"
7187 " -version show SQLite version\n"
7188 " -vfs NAME use NAME as the default VFS\n"
@@ -7474,10 +8198,12 @@
7474 i++;
7475 }else if( strcmp(z,"-html")==0 ){
7476 data.mode = MODE_Html;
7477 }else if( strcmp(z,"-list")==0 ){
7478 data.mode = MODE_List;
 
 
7479 }else if( strcmp(z,"-line")==0 ){
7480 data.mode = MODE_Line;
7481 }else if( strcmp(z,"-column")==0 ){
7482 data.mode = MODE_Column;
7483 }else if( strcmp(z,"-csv")==0 ){
@@ -7625,10 +8351,15 @@
7625 if( (zHistory = malloc(nHistory))!=0 ){
7626 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
7627 }
7628 }
7629 if( zHistory ){ shell_read_history(zHistory); }
 
 
 
 
 
7630 rc = process_input(&data, 0);
7631 if( zHistory ){
7632 shell_stifle_history(100);
7633 shell_write_history(zHistory);
7634 free(zHistory);
@@ -7648,5 +8379,6 @@
7648 for(i=0; i<argc; i++) sqlite3_free(argv[i]);
7649 sqlite3_free(argv);
7650 #endif
7651 return rc;
7652 }
 
7653
--- src/shell.c
+++ src/shell.c
@@ -1,5 +1,23 @@
1 /* DO NOT EDIT!
2 ** This file is automatically generated by the script in the canonical
3 ** SQLite source tree at tool/mkshellc.tcl. That script combines source
4 ** code from various constituent source files of SQLite into this single
5 ** "shell.c" file used to implement the SQLite command-line shell.
6 **
7 ** Most of the code found below comes from the "src/shell.c.in" file in
8 ** the canonical SQLite source tree. That main file contains "INCLUDE"
9 ** lines that specify other files in the canonical source tree that are
10 ** inserted to getnerate this complete program source file.
11 **
12 ** The code from multiple files is combined into this single "shell.c"
13 ** source file to help make the command-line program easier to compile.
14 **
15 ** To modify this program, get a copy of the canonical SQLite source tree,
16 ** edit the src/shell.c.in" and/or some of the other files that are included
17 ** by "src/shell.c.in", then rerun the tool/mkshellc.tcl script.
18 */
19 /*
20 ** 2001 September 15
21 **
22 ** The author disclaims copyright to this source code. In place of
23 ** a legal notice, here is a blessing:
@@ -782,14 +800,57 @@
800 }
801 }
802 sqlite3_result_value(pCtx, apVal[0]);
803 }
804
805 /*
806 ** The source code for several run-time loadable extensions is inserted
807 ** below by the ../tool/mkshellc.tcl script. Before processing that included
808 ** code, we need to override some macros to make the included program code
809 ** work here in the middle of this regular program.
810 */
811 #define SQLITE_EXTENSION_INIT1
812 #define SQLITE_EXTENSION_INIT2(X)
813
814 /************************* Begin ../ext/misc/shathree.c ******************/
815 /*
816 ** 2017-03-08
817 **
818 ** The author disclaims copyright to this source code. In place of
819 ** a legal notice, here is a blessing:
820 **
821 ** May you do good and not evil.
822 ** May you find forgiveness for yourself and forgive others.
823 ** May you share freely, never taking more than you give.
824 **
825 ******************************************************************************
826 **
827 ** This SQLite extension implements a functions that compute SHA1 hashes.
828 ** Two SQL functions are implemented:
829 **
830 ** sha3(X,SIZE)
831 ** sha3_query(Y,SIZE)
832 **
833 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
834 ** X is NULL.
835 **
836 ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
837 ** and returns a hash of their results.
838 **
839 ** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm
840 ** is used. If SIZE is included it must be one of the integers 224, 256,
841 ** 384, or 512, to determine SHA3 hash variant that is computed.
842 */
843 SQLITE_EXTENSION_INIT1
844 #include <assert.h>
845 #include <string.h>
846 #include <stdarg.h>
847 typedef sqlite3_uint64 u64;
848
849 /******************************************************************************
850 ** The Hash Engine
851 */
852 /*
853 ** Macros to determine whether the machine is big or little endian,
854 ** and whether or not that determination is run-time or compile-time.
855 **
856 ** For best performance, an attempt is made to guess at the byte-order
@@ -823,14 +884,10 @@
884 unsigned nRate; /* Bytes of input accepted per Keccak iteration */
885 unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
886 unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
887 };
888
 
 
 
 
889 /*
890 ** A single step of the Keccak mixing function for a 1600-bit state
891 */
892 static void KeccakF1600Step(SHA3Context *p){
893 int i;
@@ -1237,16 +1294,18 @@
1294 for(i=0; i<p->nRate; i++){
1295 p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1296 }
1297 return &p->u.x[p->nRate];
1298 }
1299 /* End of the hashing logic
1300 *****************************************************************************/
1301
1302 /*
1303 ** Implementation of the sha3(X,SIZE) function.
1304 **
1305 ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
1306 ** size is 256. If X is a BLOB, it is hashed as is.
1307 ** For all other non-NULL types of input, X is converted into a UTF-8 string
1308 ** and the string is hashed without the trailing 0x00 terminator. The hash
1309 ** of a NULL value is NULL.
1310 */
1311 static void sha3Func(
@@ -1373,14 +1432,10 @@
1432 sqlite3_free(zMsg);
1433 return;
1434 }
1435 nCol = sqlite3_column_count(pStmt);
1436 z = sqlite3_sql(pStmt);
 
 
 
 
1437 n = (int)strlen(z);
1438 hash_step_vformat(&cx,"S%d:",n);
1439 SHA3Update(&cx,(unsigned char*)z,n);
1440
1441 /* Compute a hash over the result of the query */
@@ -1439,12 +1494,654 @@
1494 }
1495 sqlite3_finalize(pStmt);
1496 }
1497 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1498 }
1499
1500
1501 #ifdef _WIN32
1502 __declspec(dllexport)
1503 #endif
1504 int sqlite3_shathree_init(
1505 sqlite3 *db,
1506 char **pzErrMsg,
1507 const sqlite3_api_routines *pApi
1508 ){
1509 int rc = SQLITE_OK;
1510 SQLITE_EXTENSION_INIT2(pApi);
1511 (void)pzErrMsg; /* Unused parameter */
1512 rc = sqlite3_create_function(db, "sha3", 1, SQLITE_UTF8, 0,
1513 sha3Func, 0, 0);
1514 if( rc==SQLITE_OK ){
1515 rc = sqlite3_create_function(db, "sha3", 2, SQLITE_UTF8, 0,
1516 sha3Func, 0, 0);
1517 }
1518 if( rc==SQLITE_OK ){
1519 rc = sqlite3_create_function(db, "sha3_query", 1, SQLITE_UTF8, 0,
1520 sha3QueryFunc, 0, 0);
1521 }
1522 if( rc==SQLITE_OK ){
1523 rc = sqlite3_create_function(db, "sha3_query", 2, SQLITE_UTF8, 0,
1524 sha3QueryFunc, 0, 0);
1525 }
1526 return rc;
1527 }
1528
1529 /************************* End ../ext/misc/shathree.c ********************/
1530 /************************* Begin ../ext/misc/fileio.c ******************/
1531 /*
1532 ** 2014-06-13
1533 **
1534 ** The author disclaims copyright to this source code. In place of
1535 ** a legal notice, here is a blessing:
1536 **
1537 ** May you do good and not evil.
1538 ** May you find forgiveness for yourself and forgive others.
1539 ** May you share freely, never taking more than you give.
1540 **
1541 ******************************************************************************
1542 **
1543 ** This SQLite extension implements SQL functions readfile() and
1544 ** writefile().
1545 */
1546 SQLITE_EXTENSION_INIT1
1547 #include <stdio.h>
1548
1549 /*
1550 ** Implementation of the "readfile(X)" SQL function. The entire content
1551 ** of the file named X is read and returned as a BLOB. NULL is returned
1552 ** if the file does not exist or is unreadable.
1553 */
1554 static void readfileFunc(
1555 sqlite3_context *context,
1556 int argc,
1557 sqlite3_value **argv
1558 ){
1559 const char *zName;
1560 FILE *in;
1561 long nIn;
1562 void *pBuf;
1563
1564 zName = (const char*)sqlite3_value_text(argv[0]);
1565 if( zName==0 ) return;
1566 in = fopen(zName, "rb");
1567 if( in==0 ) return;
1568 fseek(in, 0, SEEK_END);
1569 nIn = ftell(in);
1570 rewind(in);
1571 pBuf = sqlite3_malloc( nIn );
1572 if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
1573 sqlite3_result_blob(context, pBuf, nIn, sqlite3_free);
1574 }else{
1575 sqlite3_free(pBuf);
1576 }
1577 fclose(in);
1578 }
1579
1580 /*
1581 ** Implementation of the "writefile(X,Y)" SQL function. The argument Y
1582 ** is written into file X. The number of bytes written is returned. Or
1583 ** NULL is returned if something goes wrong, such as being unable to open
1584 ** file X for writing.
1585 */
1586 static void writefileFunc(
1587 sqlite3_context *context,
1588 int argc,
1589 sqlite3_value **argv
1590 ){
1591 FILE *out;
1592 const char *z;
1593 sqlite3_int64 rc;
1594 const char *zFile;
1595
1596 zFile = (const char*)sqlite3_value_text(argv[0]);
1597 if( zFile==0 ) return;
1598 out = fopen(zFile, "wb");
1599 if( out==0 ) return;
1600 z = (const char*)sqlite3_value_blob(argv[1]);
1601 if( z==0 ){
1602 rc = 0;
1603 }else{
1604 rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out);
1605 }
1606 fclose(out);
1607 sqlite3_result_int64(context, rc);
1608 }
1609
1610
1611 #ifdef _WIN32
1612 __declspec(dllexport)
1613 #endif
1614 int sqlite3_fileio_init(
1615 sqlite3 *db,
1616 char **pzErrMsg,
1617 const sqlite3_api_routines *pApi
1618 ){
1619 int rc = SQLITE_OK;
1620 SQLITE_EXTENSION_INIT2(pApi);
1621 (void)pzErrMsg; /* Unused parameter */
1622 rc = sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
1623 readfileFunc, 0, 0);
1624 if( rc==SQLITE_OK ){
1625 rc = sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0,
1626 writefileFunc, 0, 0);
1627 }
1628 return rc;
1629 }
1630
1631 /************************* End ../ext/misc/fileio.c ********************/
1632 /************************* Begin ../ext/misc/completion.c ******************/
1633 /*
1634 ** 2017-07-10
1635 **
1636 ** The author disclaims copyright to this source code. In place of
1637 ** a legal notice, here is a blessing:
1638 **
1639 ** May you do good and not evil.
1640 ** May you find forgiveness for yourself and forgive others.
1641 ** May you share freely, never taking more than you give.
1642 **
1643 *************************************************************************
1644 **
1645 ** This file implements an eponymous virtual table that returns suggested
1646 ** completions for a partial SQL input.
1647 **
1648 ** Suggested usage:
1649 **
1650 ** SELECT DISTINCT candidate COLLATE nocase
1651 ** FROM completion($prefix,$wholeline)
1652 ** ORDER BY 1;
1653 **
1654 ** The two query parameters are optional. $prefix is the text of the
1655 ** current word being typed and that is to be completed. $wholeline is
1656 ** the complete input line, used for context.
1657 **
1658 ** The raw completion() table might return the same candidate multiple
1659 ** times, for example if the same column name is used to two or more
1660 ** tables. And the candidates are returned in an arbitrary order. Hence,
1661 ** the DISTINCT and ORDER BY are recommended.
1662 **
1663 ** This virtual table operates at the speed of human typing, and so there
1664 ** is no attempt to make it fast. Even a slow implementation will be much
1665 ** faster than any human can type.
1666 **
1667 */
1668 SQLITE_EXTENSION_INIT1
1669 #include <assert.h>
1670 #include <string.h>
1671 #include <ctype.h>
1672
1673 #ifndef SQLITE_OMIT_VIRTUALTABLE
1674
1675 /* completion_vtab is a subclass of sqlite3_vtab which will
1676 ** serve as the underlying representation of a completion virtual table
1677 */
1678 typedef struct completion_vtab completion_vtab;
1679 struct completion_vtab {
1680 sqlite3_vtab base; /* Base class - must be first */
1681 sqlite3 *db; /* Database connection for this completion vtab */
1682 };
1683
1684 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
1685 ** serve as the underlying representation of a cursor that scans
1686 ** over rows of the result
1687 */
1688 typedef struct completion_cursor completion_cursor;
1689 struct completion_cursor {
1690 sqlite3_vtab_cursor base; /* Base class - must be first */
1691 sqlite3 *db; /* Database connection for this cursor */
1692 int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */
1693 char *zPrefix; /* The prefix for the word we want to complete */
1694 char *zLine; /* The whole that we want to complete */
1695 const char *zCurrentRow; /* Current output row */
1696 sqlite3_stmt *pStmt; /* Current statement */
1697 sqlite3_int64 iRowid; /* The rowid */
1698 int ePhase; /* Current phase */
1699 int j; /* inter-phase counter */
1700 };
1701
1702 /* Values for ePhase:
1703 */
1704 #define COMPLETION_FIRST_PHASE 1
1705 #define COMPLETION_KEYWORDS 1
1706 #define COMPLETION_PRAGMAS 2
1707 #define COMPLETION_FUNCTIONS 3
1708 #define COMPLETION_COLLATIONS 4
1709 #define COMPLETION_INDEXES 5
1710 #define COMPLETION_TRIGGERS 6
1711 #define COMPLETION_DATABASES 7
1712 #define COMPLETION_TABLES 8
1713 #define COMPLETION_COLUMNS 9
1714 #define COMPLETION_MODULES 10
1715 #define COMPLETION_EOF 11
1716
1717 /*
1718 ** The completionConnect() method is invoked to create a new
1719 ** completion_vtab that describes the completion virtual table.
1720 **
1721 ** Think of this routine as the constructor for completion_vtab objects.
1722 **
1723 ** All this routine needs to do is:
1724 **
1725 ** (1) Allocate the completion_vtab object and initialize all fields.
1726 **
1727 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
1728 ** result set of queries against completion will look like.
1729 */
1730 static int completionConnect(
1731 sqlite3 *db,
1732 void *pAux,
1733 int argc, const char *const*argv,
1734 sqlite3_vtab **ppVtab,
1735 char **pzErr
1736 ){
1737 completion_vtab *pNew;
1738 int rc;
1739
1740 /* Column numbers */
1741 #define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */
1742 #define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */
1743 #define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */
1744 #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */
1745
1746 rc = sqlite3_declare_vtab(db,
1747 "CREATE TABLE x("
1748 " candidate TEXT,"
1749 " prefix TEXT HIDDEN,"
1750 " wholeline TEXT HIDDEN,"
1751 " phase INT HIDDEN" /* Used for debugging only */
1752 ")");
1753 if( rc==SQLITE_OK ){
1754 pNew = sqlite3_malloc( sizeof(*pNew) );
1755 *ppVtab = (sqlite3_vtab*)pNew;
1756 if( pNew==0 ) return SQLITE_NOMEM;
1757 memset(pNew, 0, sizeof(*pNew));
1758 pNew->db = db;
1759 }
1760 return rc;
1761 }
1762
1763 /*
1764 ** This method is the destructor for completion_cursor objects.
1765 */
1766 static int completionDisconnect(sqlite3_vtab *pVtab){
1767 sqlite3_free(pVtab);
1768 return SQLITE_OK;
1769 }
1770
1771 /*
1772 ** Constructor for a new completion_cursor object.
1773 */
1774 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
1775 completion_cursor *pCur;
1776 pCur = sqlite3_malloc( sizeof(*pCur) );
1777 if( pCur==0 ) return SQLITE_NOMEM;
1778 memset(pCur, 0, sizeof(*pCur));
1779 pCur->db = ((completion_vtab*)p)->db;
1780 *ppCursor = &pCur->base;
1781 return SQLITE_OK;
1782 }
1783
1784 /*
1785 ** Reset the completion_cursor.
1786 */
1787 static void completionCursorReset(completion_cursor *pCur){
1788 sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0;
1789 sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0;
1790 sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
1791 pCur->j = 0;
1792 }
1793
1794 /*
1795 ** Destructor for a completion_cursor.
1796 */
1797 static int completionClose(sqlite3_vtab_cursor *cur){
1798 completionCursorReset((completion_cursor*)cur);
1799 sqlite3_free(cur);
1800 return SQLITE_OK;
1801 }
1802
1803 /*
1804 ** All SQL keywords understood by SQLite
1805 */
1806 static const char *completionKwrds[] = {
1807 "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS",
1808 "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY",
1809 "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
1810 "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE",
1811 "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
1812 "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH",
1813 "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN",
1814 "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF",
1815 "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER",
1816 "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY",
1817 "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL",
1818 "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA",
1819 "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP",
1820 "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT",
1821 "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
1822 "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE",
1823 "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE",
1824 "WITH", "WITHOUT",
1825 };
1826
1827 /*
1828 ** Advance a completion_cursor to its next row of output.
1829 **
1830 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
1831 ** record the current state of the scan. This routine sets ->zCurrentRow
1832 ** to the current row of output and then returns. If no more rows remain,
1833 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
1834 ** table that has reached the end of its scan.
1835 **
1836 ** The current implementation just lists potential identifiers and
1837 ** keywords and filters them by zPrefix. Future enhancements should
1838 ** take zLine into account to try to restrict the set of identifiers and
1839 ** keywords based on what would be legal at the current point of input.
1840 */
1841 static int completionNext(sqlite3_vtab_cursor *cur){
1842 completion_cursor *pCur = (completion_cursor*)cur;
1843 int eNextPhase = 0; /* Next phase to try if current phase reaches end */
1844 int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */
1845 pCur->iRowid++;
1846 while( pCur->ePhase!=COMPLETION_EOF ){
1847 switch( pCur->ePhase ){
1848 case COMPLETION_KEYWORDS: {
1849 if( pCur->j >= sizeof(completionKwrds)/sizeof(completionKwrds[0]) ){
1850 pCur->zCurrentRow = 0;
1851 pCur->ePhase = COMPLETION_DATABASES;
1852 }else{
1853 pCur->zCurrentRow = completionKwrds[pCur->j++];
1854 }
1855 iCol = -1;
1856 break;
1857 }
1858 case COMPLETION_DATABASES: {
1859 if( pCur->pStmt==0 ){
1860 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
1861 &pCur->pStmt, 0);
1862 }
1863 iCol = 1;
1864 eNextPhase = COMPLETION_TABLES;
1865 break;
1866 }
1867 case COMPLETION_TABLES: {
1868 if( pCur->pStmt==0 ){
1869 sqlite3_stmt *pS2;
1870 char *zSql = 0;
1871 const char *zSep = "";
1872 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
1873 while( sqlite3_step(pS2)==SQLITE_ROW ){
1874 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
1875 zSql = sqlite3_mprintf(
1876 "%z%s"
1877 "SELECT name FROM \"%w\".sqlite_master"
1878 " WHERE type='table'",
1879 zSql, zSep, zDb
1880 );
1881 if( zSql==0 ) return SQLITE_NOMEM;
1882 zSep = " UNION ";
1883 }
1884 sqlite3_finalize(pS2);
1885 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
1886 sqlite3_free(zSql);
1887 }
1888 iCol = 0;
1889 eNextPhase = COMPLETION_COLUMNS;
1890 break;
1891 }
1892 case COMPLETION_COLUMNS: {
1893 if( pCur->pStmt==0 ){
1894 sqlite3_stmt *pS2;
1895 char *zSql = 0;
1896 const char *zSep = "";
1897 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
1898 while( sqlite3_step(pS2)==SQLITE_ROW ){
1899 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
1900 zSql = sqlite3_mprintf(
1901 "%z%s"
1902 "SELECT pti.name FROM \"%w\".sqlite_master AS sm"
1903 " JOIN pragma_table_info(sm.name,%Q) AS pti"
1904 " WHERE sm.type='table'",
1905 zSql, zSep, zDb, zDb
1906 );
1907 if( zSql==0 ) return SQLITE_NOMEM;
1908 zSep = " UNION ";
1909 }
1910 sqlite3_finalize(pS2);
1911 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
1912 sqlite3_free(zSql);
1913 }
1914 iCol = 0;
1915 eNextPhase = COMPLETION_EOF;
1916 break;
1917 }
1918 }
1919 if( iCol<0 ){
1920 /* This case is when the phase presets zCurrentRow */
1921 if( pCur->zCurrentRow==0 ) continue;
1922 }else{
1923 if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
1924 /* Extract the next row of content */
1925 pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
1926 }else{
1927 /* When all rows are finished, advance to the next phase */
1928 sqlite3_finalize(pCur->pStmt);
1929 pCur->pStmt = 0;
1930 pCur->ePhase = eNextPhase;
1931 continue;
1932 }
1933 }
1934 if( pCur->nPrefix==0 ) break;
1935 if( sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0 ){
1936 break;
1937 }
1938 }
1939
1940 return SQLITE_OK;
1941 }
1942
1943 /*
1944 ** Return values of columns for the row at which the completion_cursor
1945 ** is currently pointing.
1946 */
1947 static int completionColumn(
1948 sqlite3_vtab_cursor *cur, /* The cursor */
1949 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
1950 int i /* Which column to return */
1951 ){
1952 completion_cursor *pCur = (completion_cursor*)cur;
1953 switch( i ){
1954 case COMPLETION_COLUMN_CANDIDATE: {
1955 sqlite3_result_text(ctx, pCur->zCurrentRow, -1, SQLITE_TRANSIENT);
1956 break;
1957 }
1958 case COMPLETION_COLUMN_PREFIX: {
1959 sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
1960 break;
1961 }
1962 case COMPLETION_COLUMN_WHOLELINE: {
1963 sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
1964 break;
1965 }
1966 case COMPLETION_COLUMN_PHASE: {
1967 sqlite3_result_int(ctx, pCur->ePhase);
1968 break;
1969 }
1970 }
1971 return SQLITE_OK;
1972 }
1973
1974 /*
1975 ** Return the rowid for the current row. In this implementation, the
1976 ** rowid is the same as the output value.
1977 */
1978 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
1979 completion_cursor *pCur = (completion_cursor*)cur;
1980 *pRowid = pCur->iRowid;
1981 return SQLITE_OK;
1982 }
1983
1984 /*
1985 ** Return TRUE if the cursor has been moved off of the last
1986 ** row of output.
1987 */
1988 static int completionEof(sqlite3_vtab_cursor *cur){
1989 completion_cursor *pCur = (completion_cursor*)cur;
1990 return pCur->ePhase >= COMPLETION_EOF;
1991 }
1992
1993 /*
1994 ** This method is called to "rewind" the completion_cursor object back
1995 ** to the first row of output. This method is always called at least
1996 ** once prior to any call to completionColumn() or completionRowid() or
1997 ** completionEof().
1998 */
1999 static int completionFilter(
2000 sqlite3_vtab_cursor *pVtabCursor,
2001 int idxNum, const char *idxStr,
2002 int argc, sqlite3_value **argv
2003 ){
2004 completion_cursor *pCur = (completion_cursor *)pVtabCursor;
2005 int iArg = 0;
2006 completionCursorReset(pCur);
2007 if( idxNum & 1 ){
2008 pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
2009 if( pCur->nPrefix>0 ){
2010 pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
2011 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
2012 }
2013 iArg++;
2014 }
2015 if( idxNum & 2 ){
2016 pCur->nLine = sqlite3_value_bytes(argv[iArg]);
2017 if( pCur->nLine>0 ){
2018 pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
2019 if( pCur->zLine==0 ) return SQLITE_NOMEM;
2020 }
2021 iArg++;
2022 }
2023 if( pCur->zLine!=0 && pCur->zPrefix==0 ){
2024 int i = pCur->nLine;
2025 while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
2026 i--;
2027 }
2028 pCur->nPrefix = pCur->nLine - i;
2029 if( pCur->nPrefix>0 ){
2030 pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
2031 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
2032 }
2033 }
2034 pCur->iRowid = 0;
2035 pCur->ePhase = COMPLETION_FIRST_PHASE;
2036 return completionNext(pVtabCursor);
2037 }
2038
2039 /*
2040 ** SQLite will invoke this method one or more times while planning a query
2041 ** that uses the completion virtual table. This routine needs to create
2042 ** a query plan for each invocation and compute an estimated cost for that
2043 ** plan.
2044 **
2045 ** There are two hidden parameters that act as arguments to the table-valued
2046 ** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix"
2047 ** is available and bit 1 is set if "wholeline" is available.
2048 */
2049 static int completionBestIndex(
2050 sqlite3_vtab *tab,
2051 sqlite3_index_info *pIdxInfo
2052 ){
2053 int i; /* Loop over constraints */
2054 int idxNum = 0; /* The query plan bitmask */
2055 int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */
2056 int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
2057 int nArg = 0; /* Number of arguments that completeFilter() expects */
2058 const struct sqlite3_index_constraint *pConstraint;
2059
2060 pConstraint = pIdxInfo->aConstraint;
2061 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2062 if( pConstraint->usable==0 ) continue;
2063 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2064 switch( pConstraint->iColumn ){
2065 case COMPLETION_COLUMN_PREFIX:
2066 prefixIdx = i;
2067 idxNum |= 1;
2068 break;
2069 case COMPLETION_COLUMN_WHOLELINE:
2070 wholelineIdx = i;
2071 idxNum |= 2;
2072 break;
2073 }
2074 }
2075 if( prefixIdx>=0 ){
2076 pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
2077 pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
2078 }
2079 if( wholelineIdx>=0 ){
2080 pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
2081 pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
2082 }
2083 pIdxInfo->idxNum = idxNum;
2084 pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
2085 pIdxInfo->estimatedRows = 500 - 100*nArg;
2086 return SQLITE_OK;
2087 }
2088
2089 /*
2090 ** This following structure defines all the methods for the
2091 ** completion virtual table.
2092 */
2093 static sqlite3_module completionModule = {
2094 0, /* iVersion */
2095 0, /* xCreate */
2096 completionConnect, /* xConnect */
2097 completionBestIndex, /* xBestIndex */
2098 completionDisconnect, /* xDisconnect */
2099 0, /* xDestroy */
2100 completionOpen, /* xOpen - open a cursor */
2101 completionClose, /* xClose - close a cursor */
2102 completionFilter, /* xFilter - configure scan constraints */
2103 completionNext, /* xNext - advance a cursor */
2104 completionEof, /* xEof - check for end of scan */
2105 completionColumn, /* xColumn - read data */
2106 completionRowid, /* xRowid - read data */
2107 0, /* xUpdate */
2108 0, /* xBegin */
2109 0, /* xSync */
2110 0, /* xCommit */
2111 0, /* xRollback */
2112 0, /* xFindMethod */
2113 0, /* xRename */
2114 };
2115
2116 #endif /* SQLITE_OMIT_VIRTUALTABLE */
2117
2118 int sqlite3CompletionVtabInit(sqlite3 *db){
2119 int rc = SQLITE_OK;
2120 #ifndef SQLITE_OMIT_VIRTUALTABLE
2121 rc = sqlite3_create_module(db, "completion", &completionModule, 0);
2122 #endif
2123 return rc;
2124 }
2125
2126 #ifdef _WIN32
2127 __declspec(dllexport)
2128 #endif
2129 int sqlite3_completion_init(
2130 sqlite3 *db,
2131 char **pzErrMsg,
2132 const sqlite3_api_routines *pApi
2133 ){
2134 int rc = SQLITE_OK;
2135 SQLITE_EXTENSION_INIT2(pApi);
2136 #ifndef SQLITE_OMIT_VIRTUALTABLE
2137 rc = sqlite3CompletionVtabInit(db);
2138 #endif
2139 return rc;
2140 }
2141
2142 /************************* End ../ext/misc/completion.c ********************/
2143
2144 #if defined(SQLITE_ENABLE_SESSION)
2145 /*
2146 ** State information for a single open session
2147 */
@@ -1521,12 +2218,13 @@
2218 #define SHFLG_Scratch 0x00000001 /* The --scratch option is used */
2219 #define SHFLG_Pagecache 0x00000002 /* The --pagecache option is used */
2220 #define SHFLG_Lookaside 0x00000004 /* Lookaside memory is used */
2221 #define SHFLG_Backslash 0x00000008 /* The --backslash option is used */
2222 #define SHFLG_PreserveRowid 0x00000010 /* .dump preserves rowid values */
2223 #define SHFLG_Newlines 0x00000020 /* .dump --newline flag */
2224 #define SHFLG_CountChanges 0x00000040 /* .changes setting */
2225 #define SHFLG_Echo 0x00000080 /* .echo or --echo setting */
2226
2227 /*
2228 ** Macros for testing and setting shellFlgs
2229 */
2230 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
@@ -2193,11 +2891,15 @@
2891 for(i=0; i<nArg; i++){
2892 raw_printf(p->out, i>0 ? "," : " VALUES(");
2893 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2894 utf8_printf(p->out,"NULL");
2895 }else if( aiType && aiType[i]==SQLITE_TEXT ){
2896 if( ShellHasFlag(p, SHFLG_Newlines) ){
2897 output_quoted_string(p->out, azArg[i]);
2898 }else{
2899 output_quoted_escaped_string(p->out, azArg[i]);
2900 }
2901 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2902 utf8_printf(p->out,"%s", azArg[i]);
2903 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2904 char z[50];
2905 double r = sqlite3_column_double(p->pStmt, i);
@@ -2207,10 +2909,12 @@
2909 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2910 int nBlob = sqlite3_column_bytes(p->pStmt, i);
2911 output_hex_blob(p->out, pBlob, nBlob);
2912 }else if( isNumber(azArg[i], 0) ){
2913 utf8_printf(p->out,"%s", azArg[i]);
2914 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2915 output_quoted_string(p->out, azArg[i]);
2916 }else{
2917 output_quoted_escaped_string(p->out, azArg[i]);
2918 }
2919 }
2920 raw_printf(p->out,");\n");
@@ -3325,11 +4029,13 @@
4029 " If TABLE specified, only dump tables matching\n"
4030 " LIKE pattern TABLE.\n"
4031 ".echo on|off Turn command echo on or off\n"
4032 ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n"
4033 ".exit Exit this program\n"
4034 /* Because explain mode comes on automatically now, the ".explain" mode
4035 ** is removed from the help screen. It is still supported for legacy, however */
4036 /*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"*/
4037 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n"
4038 ".headers on|off Turn display of headers on or off\n"
4039 ".help Show this message\n"
4040 ".import FILE TABLE Import data from FILE into TABLE\n"
4041 #ifndef SQLITE_OMIT_TEST_CONTROL
@@ -3460,62 +4166,10 @@
4166 pBuf[nIn] = 0;
4167 if( pnByte ) *pnByte = nIn;
4168 return pBuf;
4169 }
4170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4171 #if defined(SQLITE_ENABLE_SESSION)
4172 /*
4173 ** Close a single OpenSession object and release all of its associated
4174 ** resources.
4175 */
@@ -3577,22 +4231,76 @@
4231 exit(1);
4232 }
4233 #ifndef SQLITE_OMIT_LOAD_EXTENSION
4234 sqlite3_enable_load_extension(p->db, 1);
4235 #endif
4236 sqlite3_fileio_init(p->db, 0, 0);
4237 sqlite3_shathree_init(p->db, 0, 0);
4238 sqlite3_completion_init(p->db, 0, 0);
 
 
 
 
 
 
 
 
 
4239 sqlite3_create_function(p->db, "shell_add_schema", 2, SQLITE_UTF8, 0,
4240 shellAddSchemaName, 0, 0);
 
4241 }
4242 }
4243
4244 #if HAVE_READLINE || HAVE_EDITLINE
4245 /*
4246 ** Readline completion callbacks
4247 */
4248 static char *readline_completion_generator(const char *text, int state){
4249 static sqlite3_stmt *pStmt = 0;
4250 char *zRet;
4251 if( state==0 ){
4252 char *zSql;
4253 int rc;
4254 sqlite3_finalize(pStmt);
4255 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4256 " FROM completion(%Q) ORDER BY 1", text);
4257 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4258 sqlite3_free(zSql);
4259 }
4260 if( sqlite3_step(pStmt)==SQLITE_ROW ){
4261 zRet = strdup(sqlite3_column_text(pStmt, 0));
4262 }else{
4263 sqlite3_finalize(pStmt);
4264 pStmt = 0;
4265 zRet = 0;
4266 }
4267 return zRet;
4268 }
4269 static char **readline_completion(const char *zText, int iStart, int iEnd){
4270 rl_attempted_completion_over = 1;
4271 return rl_completion_matches(zText, readline_completion_generator);
4272 }
4273
4274 #elif HAVE_LINENOISE
4275 /*
4276 ** Linenoise completion callback
4277 */
4278 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
4279 int nLine = (int)strlen(zLine);
4280 int i, iStart;
4281 sqlite3_stmt *pStmt = 0;
4282 char *zSql;
4283 char zBuf[1000];
4284
4285 if( nLine>sizeof(zBuf)-30 ) return;
4286 if( zLine[0]=='.' ) return;
4287 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
4288 if( i==nLine-1 ) return;
4289 iStart = i+1;
4290 memcpy(zBuf, zLine, iStart);
4291 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4292 " FROM completion(%Q,%Q) ORDER BY 1",
4293 &zLine[iStart], zLine);
4294 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4295 sqlite3_free(zSql);
4296 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
4297 while( sqlite3_step(pStmt)==SQLITE_ROW ){
4298 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
4299 int nCompletion = sqlite3_column_bytes(pStmt, 0);
4300 if( iStart+nCompletion < sizeof(zBuf)-1 ){
4301 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
4302 linenoiseAddCompletion(lc, zBuf);
4303 }
4304 }
4305 sqlite3_finalize(pStmt);
4306 }
@@ -3599,5 +4307,6 @@
4307 #endif
4308
4309 /*
4310 ** Do C-language style dequoting.
4311 **
4312 ** \a -> alarm
@@ -4926,11 +5635,11 @@
5635
5636 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
5637 const char *zLike = 0;
5638 int i;
5639 int savedShowHeader = p->showHeader;
5640 ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines);
5641 for(i=1; i<nArg; i++){
5642 if( azArg[i][0]=='-' ){
5643 const char *z = azArg[i]+1;
5644 if( z[0]=='-' ) z++;
5645 if( strcmp(z,"preserve-rowids")==0 ){
@@ -4941,17 +5650,21 @@
5650 goto meta_command_exit;
5651 #else
5652 ShellSetFlag(p, SHFLG_PreserveRowid);
5653 #endif
5654 }else
5655 if( strcmp(z,"newlines")==0 ){
5656 ShellSetFlag(p, SHFLG_Newlines);
5657 }else
5658 {
5659 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
5660 rc = 1;
5661 goto meta_command_exit;
5662 }
5663 }else if( zLike ){
5664 raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
5665 "?--newlines? ?LIKE-PATTERN?\n");
5666 rc = 1;
5667 goto meta_command_exit;
5668 }else{
5669 zLike = azArg[i];
5670 }
@@ -5033,10 +5746,12 @@
5746 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
5747 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
5748 rc = 2;
5749 }else
5750
5751 /* The ".explain" command is automatic now. It is largely pointless. It
5752 ** retained purely for backwards compatibility */
5753 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
5754 int val = 1;
5755 if( nArg>=2 ){
5756 if( strcmp(azArg[1],"auto")==0 ){
5757 val = 99;
@@ -5549,11 +6264,13 @@
6264 p->mode = MODE_Quote;
6265 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
6266 p->mode = MODE_Ascii;
6267 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
6268 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
6269 }else if( nArg==1 ){
6270 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
6271 }else{
6272 raw_printf(stderr, "Error: mode should be one of: "
6273 "ascii column csv html insert line list quote tabs tcl\n");
6274 rc = 1;
6275 }
6276 p->cMode = p->mode;
@@ -5821,12 +6538,18 @@
6538 rc = 1;
6539 goto meta_command_exit;
6540 }
6541 if( zDiv ){
6542 sqlite3_stmt *pStmt = 0;
6543 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
6544 -1, &pStmt, 0);
6545 if( rc ){
6546 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
6547 sqlite3_finalize(pStmt);
6548 rc = 1;
6549 goto meta_command_exit;
6550 }
6551 appendText(&sSelect, "SELECT sql FROM", 0);
6552 iSchema = 0;
6553 while( sqlite3_step(pStmt)==SQLITE_ROW ){
6554 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
6555 char zScNum[30];
@@ -7179,10 +7902,11 @@
7902 " -multiplex enable the multiplexor VFS\n"
7903 #endif
7904 " -newline SEP set output row separator. Default: '\\n'\n"
7905 " -nullvalue TEXT set text string for NULL values. Default ''\n"
7906 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
7907 " -quote set output mode to 'quote'\n"
7908 " -scratch SIZE N use N slots of SZ bytes each for scratch memory\n"
7909 " -separator SEP set output column separator. Default: '|'\n"
7910 " -stats print memory stats before each finalize\n"
7911 " -version show SQLite version\n"
7912 " -vfs NAME use NAME as the default VFS\n"
@@ -7474,10 +8198,12 @@
8198 i++;
8199 }else if( strcmp(z,"-html")==0 ){
8200 data.mode = MODE_Html;
8201 }else if( strcmp(z,"-list")==0 ){
8202 data.mode = MODE_List;
8203 }else if( strcmp(z,"-quote")==0 ){
8204 data.mode = MODE_Quote;
8205 }else if( strcmp(z,"-line")==0 ){
8206 data.mode = MODE_Line;
8207 }else if( strcmp(z,"-column")==0 ){
8208 data.mode = MODE_Column;
8209 }else if( strcmp(z,"-csv")==0 ){
@@ -7625,10 +8351,15 @@
8351 if( (zHistory = malloc(nHistory))!=0 ){
8352 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
8353 }
8354 }
8355 if( zHistory ){ shell_read_history(zHistory); }
8356 #if HAVE_READLINE || HAVE_EDITLINE
8357 rl_attempted_completion_function = readline_completion;
8358 #elif HAVE_LINENOISE
8359 linenoiseSetCompletionCallback(linenoise_completion);
8360 #endif
8361 rc = process_input(&data, 0);
8362 if( zHistory ){
8363 shell_stifle_history(100);
8364 shell_write_history(zHistory);
8365 free(zHistory);
@@ -7648,5 +8379,6 @@
8379 for(i=0; i<argc; i++) sqlite3_free(argv[i]);
8380 sqlite3_free(argv);
8381 #endif
8382 return rc;
8383 }
8384
8385
-1
--- src/wiki.c
+++ src/wiki.c
@@ -1425,11 +1425,10 @@
14251425
** Render markdown wiki from FILE to stdout.
14261426
**
14271427
*/
14281428
void test_markdown_render(void){
14291429
Blob in, out;
1430
- db_find_and_open_repository(0,0);
14311430
verify_all_options();
14321431
if( g.argc!=3 ) usage("FILE");
14331432
blob_zero(&out);
14341433
blob_read_from_file(&in, g.argv[2]);
14351434
markdown_to_html(&in, 0, &out);
14361435
--- src/wiki.c
+++ src/wiki.c
@@ -1425,11 +1425,10 @@
1425 ** Render markdown wiki from FILE to stdout.
1426 **
1427 */
1428 void test_markdown_render(void){
1429 Blob in, out;
1430 db_find_and_open_repository(0,0);
1431 verify_all_options();
1432 if( g.argc!=3 ) usage("FILE");
1433 blob_zero(&out);
1434 blob_read_from_file(&in, g.argv[2]);
1435 markdown_to_html(&in, 0, &out);
1436
--- src/wiki.c
+++ src/wiki.c
@@ -1425,11 +1425,10 @@
1425 ** Render markdown wiki from FILE to stdout.
1426 **
1427 */
1428 void test_markdown_render(void){
1429 Blob in, out;
 
1430 verify_all_options();
1431 if( g.argc!=3 ) usage("FILE");
1432 blob_zero(&out);
1433 blob_read_from_file(&in, g.argv[2]);
1434 markdown_to_html(&in, 0, &out);
1435
--- src/wikiformat.c
+++ src/wikiformat.c
@@ -1738,11 +1738,10 @@
17381738
if( find_option("htmlonly",0,0)!=0 ) flags |= WIKI_HTMLONLY;
17391739
if( find_option("linksonly",0,0)!=0 ) flags |= WIKI_LINKSONLY;
17401740
if( find_option("nobadlinks",0,0)!=0 ) flags |= WIKI_NOBADLINKS;
17411741
if( find_option("inline",0,0)!=0 ) flags |= WIKI_INLINE;
17421742
if( find_option("noblock",0,0)!=0 ) flags |= WIKI_NOBLOCK;
1743
- db_find_and_open_repository(0,0);
17441743
verify_all_options();
17451744
if( g.argc!=3 ) usage("FILE");
17461745
blob_zero(&out);
17471746
blob_read_from_file(&in, g.argv[2]);
17481747
wiki_convert(&in, &out, flags);
17491748
--- src/wikiformat.c
+++ src/wikiformat.c
@@ -1738,11 +1738,10 @@
1738 if( find_option("htmlonly",0,0)!=0 ) flags |= WIKI_HTMLONLY;
1739 if( find_option("linksonly",0,0)!=0 ) flags |= WIKI_LINKSONLY;
1740 if( find_option("nobadlinks",0,0)!=0 ) flags |= WIKI_NOBADLINKS;
1741 if( find_option("inline",0,0)!=0 ) flags |= WIKI_INLINE;
1742 if( find_option("noblock",0,0)!=0 ) flags |= WIKI_NOBLOCK;
1743 db_find_and_open_repository(0,0);
1744 verify_all_options();
1745 if( g.argc!=3 ) usage("FILE");
1746 blob_zero(&out);
1747 blob_read_from_file(&in, g.argv[2]);
1748 wiki_convert(&in, &out, flags);
1749
--- src/wikiformat.c
+++ src/wikiformat.c
@@ -1738,11 +1738,10 @@
1738 if( find_option("htmlonly",0,0)!=0 ) flags |= WIKI_HTMLONLY;
1739 if( find_option("linksonly",0,0)!=0 ) flags |= WIKI_LINKSONLY;
1740 if( find_option("nobadlinks",0,0)!=0 ) flags |= WIKI_NOBADLINKS;
1741 if( find_option("inline",0,0)!=0 ) flags |= WIKI_INLINE;
1742 if( find_option("noblock",0,0)!=0 ) flags |= WIKI_NOBLOCK;
 
1743 verify_all_options();
1744 if( g.argc!=3 ) usage("FILE");
1745 blob_zero(&out);
1746 blob_read_from_file(&in, g.argv[2]);
1747 wiki_convert(&in, &out, flags);
1748

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button