Fossil SCM

Update to the latest SQLite 3.18.0 prerelease code, and especially the newest CLI code.

drh 2017-03-10 17:32 trunk
Commit d93679a1d75f2aea40067795b126d3dbae85161d8f2f9439f3debf62af7719a3
3 files changed +1411 -230 +59 -45 +1 -1
+1411 -230
--- src/shell.c
+++ src/shell.c
@@ -452,32 +452,10 @@
452452
while( IsDigit(*z) ){ z++; }
453453
if( realnum ) *realnum = 1;
454454
}
455455
return *z==0;
456456
}
457
-
458
-/*
459
-** A global char* and an SQL function to access its current value
460
-** from within an SQL statement. This program used to use the
461
-** sqlite_exec_printf() API to substitue a string into an SQL statement.
462
-** The correct way to do this with sqlite3 is to use the bind API, but
463
-** since the shell is built around the callback paradigm it would be a lot
464
-** of work. Instead just use this hack, which is quite harmless.
465
-*/
466
-static const char *zShellStatic = 0;
467
-static void shellstaticFunc(
468
- sqlite3_context *context,
469
- int argc,
470
- sqlite3_value **argv
471
-){
472
- assert( 0==argc );
473
- assert( zShellStatic );
474
- UNUSED_PARAMETER(argc);
475
- UNUSED_PARAMETER(argv);
476
- sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC);
477
-}
478
-
479457
480458
/*
481459
** Compute a string length that is limited to what can be stored in
482460
** lower 30 bits of a 32-bit signed integer.
483461
*/
@@ -575,10 +553,783 @@
575553
if( zResult && *zResult ) shell_add_history(zResult);
576554
#endif
577555
}
578556
return zResult;
579557
}
558
+/*
559
+** A variable length string to which one can append text.
560
+*/
561
+typedef struct ShellText ShellText;
562
+struct ShellText {
563
+ char *z;
564
+ int n;
565
+ int nAlloc;
566
+};
567
+
568
+/*
569
+** Initialize and destroy a ShellText object
570
+*/
571
+static void initText(ShellText *p){
572
+ memset(p, 0, sizeof(*p));
573
+}
574
+static void freeText(ShellText *p){
575
+ free(p->z);
576
+ initText(p);
577
+}
578
+
579
+/* zIn is either a pointer to a NULL-terminated string in memory obtained
580
+** from malloc(), or a NULL pointer. The string pointed to by zAppend is
581
+** added to zIn, and the result returned in memory obtained from malloc().
582
+** zIn, if it was not NULL, is freed.
583
+**
584
+** If the third argument, quote, is not '\0', then it is used as a
585
+** quote character for zAppend.
586
+*/
587
+static void appendText(ShellText *p, char const *zAppend, char quote){
588
+ int len;
589
+ int i;
590
+ int nAppend = strlen30(zAppend);
591
+
592
+ len = nAppend+p->n+1;
593
+ if( quote ){
594
+ len += 2;
595
+ for(i=0; i<nAppend; i++){
596
+ if( zAppend[i]==quote ) len++;
597
+ }
598
+ }
599
+
600
+ if( p->n+len>=p->nAlloc ){
601
+ p->nAlloc = p->nAlloc*2 + len + 20;
602
+ p->z = realloc(p->z, p->nAlloc);
603
+ if( p->z==0 ){
604
+ memset(p, 0, sizeof(*p));
605
+ return;
606
+ }
607
+ }
608
+
609
+ if( quote ){
610
+ char *zCsr = p->z+p->n;
611
+ *zCsr++ = quote;
612
+ for(i=0; i<nAppend; i++){
613
+ *zCsr++ = zAppend[i];
614
+ if( zAppend[i]==quote ) *zCsr++ = quote;
615
+ }
616
+ *zCsr++ = quote;
617
+ p->n = (int)(zCsr - p->z);
618
+ *zCsr = '\0';
619
+ }else{
620
+ memcpy(p->z+p->n, zAppend, nAppend);
621
+ p->n += nAppend;
622
+ p->z[p->n] = '\0';
623
+ }
624
+}
625
+
626
+/*
627
+** Attempt to determine if identifier zName needs to be quoted, either
628
+** because it contains non-alphanumeric characters, or because it is an
629
+** SQLite keyword. Be conservative in this estimate: When in doubt assume
630
+** that quoting is required.
631
+**
632
+** Return '"' if quoting is required. Return 0 if no quoting is required.
633
+*/
634
+static char quoteChar(const char *zName){
635
+ /* All SQLite keywords, in alphabetical order */
636
+ static const char *azKeywords[] = {
637
+ "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS",
638
+ "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY",
639
+ "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
640
+ "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE",
641
+ "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
642
+ "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH",
643
+ "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN",
644
+ "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF",
645
+ "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER",
646
+ "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY",
647
+ "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL",
648
+ "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA",
649
+ "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP",
650
+ "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT",
651
+ "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
652
+ "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE",
653
+ "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE",
654
+ "WITH", "WITHOUT",
655
+ };
656
+ int i, lwr, upr, mid, c;
657
+ if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
658
+ for(i=0; zName[i]; i++){
659
+ if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
660
+ }
661
+ lwr = 0;
662
+ upr = sizeof(azKeywords)/sizeof(azKeywords[0]) - 1;
663
+ while( lwr<=upr ){
664
+ mid = (lwr+upr)/2;
665
+ c = sqlite3_stricmp(azKeywords[mid], zName);
666
+ if( c==0 ) return '"';
667
+ if( c<0 ){
668
+ lwr = mid+1;
669
+ }else{
670
+ upr = mid-1;
671
+ }
672
+ }
673
+ return 0;
674
+}
675
+
676
+/******************************************************************************
677
+** SHA3 hash implementation copied from ../ext/misc/shathree.c
678
+*/
679
+typedef sqlite3_uint64 u64;
680
+/*
681
+** Macros to determine whether the machine is big or little endian,
682
+** and whether or not that determination is run-time or compile-time.
683
+**
684
+** For best performance, an attempt is made to guess at the byte-order
685
+** using C-preprocessor macros. If that is unsuccessful, or if
686
+** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
687
+** at run-time.
688
+*/
689
+#ifndef SHA3_BYTEORDER
690
+# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
691
+ defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
692
+ defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
693
+ defined(__arm__)
694
+# define SHA3_BYTEORDER 1234
695
+# elif defined(sparc) || defined(__ppc__)
696
+# define SHA3_BYTEORDER 4321
697
+# else
698
+# define SHA3_BYTEORDER 0
699
+# endif
700
+#endif
701
+
702
+
703
+/*
704
+** State structure for a SHA3 hash in progress
705
+*/
706
+typedef struct SHA3Context SHA3Context;
707
+struct SHA3Context {
708
+ union {
709
+ u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */
710
+ unsigned char x[1600]; /* ... or 1600 bytes */
711
+ } u;
712
+ unsigned nRate; /* Bytes of input accepted per Keccak iteration */
713
+ unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
714
+ unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
715
+};
716
+
717
+/*
718
+** A single step of the Keccak mixing function for a 1600-bit state
719
+*/
720
+static void KeccakF1600Step(SHA3Context *p){
721
+ int i;
722
+ u64 B0, B1, B2, B3, B4;
723
+ u64 C0, C1, C2, C3, C4;
724
+ u64 D0, D1, D2, D3, D4;
725
+ static const u64 RC[] = {
726
+ 0x0000000000000001ULL, 0x0000000000008082ULL,
727
+ 0x800000000000808aULL, 0x8000000080008000ULL,
728
+ 0x000000000000808bULL, 0x0000000080000001ULL,
729
+ 0x8000000080008081ULL, 0x8000000000008009ULL,
730
+ 0x000000000000008aULL, 0x0000000000000088ULL,
731
+ 0x0000000080008009ULL, 0x000000008000000aULL,
732
+ 0x000000008000808bULL, 0x800000000000008bULL,
733
+ 0x8000000000008089ULL, 0x8000000000008003ULL,
734
+ 0x8000000000008002ULL, 0x8000000000000080ULL,
735
+ 0x000000000000800aULL, 0x800000008000000aULL,
736
+ 0x8000000080008081ULL, 0x8000000000008080ULL,
737
+ 0x0000000080000001ULL, 0x8000000080008008ULL
738
+ };
739
+# define A00 (p->u.s[0])
740
+# define A01 (p->u.s[1])
741
+# define A02 (p->u.s[2])
742
+# define A03 (p->u.s[3])
743
+# define A04 (p->u.s[4])
744
+# define A10 (p->u.s[5])
745
+# define A11 (p->u.s[6])
746
+# define A12 (p->u.s[7])
747
+# define A13 (p->u.s[8])
748
+# define A14 (p->u.s[9])
749
+# define A20 (p->u.s[10])
750
+# define A21 (p->u.s[11])
751
+# define A22 (p->u.s[12])
752
+# define A23 (p->u.s[13])
753
+# define A24 (p->u.s[14])
754
+# define A30 (p->u.s[15])
755
+# define A31 (p->u.s[16])
756
+# define A32 (p->u.s[17])
757
+# define A33 (p->u.s[18])
758
+# define A34 (p->u.s[19])
759
+# define A40 (p->u.s[20])
760
+# define A41 (p->u.s[21])
761
+# define A42 (p->u.s[22])
762
+# define A43 (p->u.s[23])
763
+# define A44 (p->u.s[24])
764
+# define ROL64(a,x) ((a<<x)|(a>>(64-x)))
765
+
766
+ for(i=0; i<24; i+=4){
767
+ C0 = A00^A10^A20^A30^A40;
768
+ C1 = A01^A11^A21^A31^A41;
769
+ C2 = A02^A12^A22^A32^A42;
770
+ C3 = A03^A13^A23^A33^A43;
771
+ C4 = A04^A14^A24^A34^A44;
772
+ D0 = C4^ROL64(C1, 1);
773
+ D1 = C0^ROL64(C2, 1);
774
+ D2 = C1^ROL64(C3, 1);
775
+ D3 = C2^ROL64(C4, 1);
776
+ D4 = C3^ROL64(C0, 1);
777
+
778
+ B0 = (A00^D0);
779
+ B1 = ROL64((A11^D1), 44);
780
+ B2 = ROL64((A22^D2), 43);
781
+ B3 = ROL64((A33^D3), 21);
782
+ B4 = ROL64((A44^D4), 14);
783
+ A00 = B0 ^((~B1)& B2 );
784
+ A00 ^= RC[i];
785
+ A11 = B1 ^((~B2)& B3 );
786
+ A22 = B2 ^((~B3)& B4 );
787
+ A33 = B3 ^((~B4)& B0 );
788
+ A44 = B4 ^((~B0)& B1 );
789
+
790
+ B2 = ROL64((A20^D0), 3);
791
+ B3 = ROL64((A31^D1), 45);
792
+ B4 = ROL64((A42^D2), 61);
793
+ B0 = ROL64((A03^D3), 28);
794
+ B1 = ROL64((A14^D4), 20);
795
+ A20 = B0 ^((~B1)& B2 );
796
+ A31 = B1 ^((~B2)& B3 );
797
+ A42 = B2 ^((~B3)& B4 );
798
+ A03 = B3 ^((~B4)& B0 );
799
+ A14 = B4 ^((~B0)& B1 );
800
+
801
+ B4 = ROL64((A40^D0), 18);
802
+ B0 = ROL64((A01^D1), 1);
803
+ B1 = ROL64((A12^D2), 6);
804
+ B2 = ROL64((A23^D3), 25);
805
+ B3 = ROL64((A34^D4), 8);
806
+ A40 = B0 ^((~B1)& B2 );
807
+ A01 = B1 ^((~B2)& B3 );
808
+ A12 = B2 ^((~B3)& B4 );
809
+ A23 = B3 ^((~B4)& B0 );
810
+ A34 = B4 ^((~B0)& B1 );
811
+
812
+ B1 = ROL64((A10^D0), 36);
813
+ B2 = ROL64((A21^D1), 10);
814
+ B3 = ROL64((A32^D2), 15);
815
+ B4 = ROL64((A43^D3), 56);
816
+ B0 = ROL64((A04^D4), 27);
817
+ A10 = B0 ^((~B1)& B2 );
818
+ A21 = B1 ^((~B2)& B3 );
819
+ A32 = B2 ^((~B3)& B4 );
820
+ A43 = B3 ^((~B4)& B0 );
821
+ A04 = B4 ^((~B0)& B1 );
822
+
823
+ B3 = ROL64((A30^D0), 41);
824
+ B4 = ROL64((A41^D1), 2);
825
+ B0 = ROL64((A02^D2), 62);
826
+ B1 = ROL64((A13^D3), 55);
827
+ B2 = ROL64((A24^D4), 39);
828
+ A30 = B0 ^((~B1)& B2 );
829
+ A41 = B1 ^((~B2)& B3 );
830
+ A02 = B2 ^((~B3)& B4 );
831
+ A13 = B3 ^((~B4)& B0 );
832
+ A24 = B4 ^((~B0)& B1 );
833
+
834
+ C0 = A00^A20^A40^A10^A30;
835
+ C1 = A11^A31^A01^A21^A41;
836
+ C2 = A22^A42^A12^A32^A02;
837
+ C3 = A33^A03^A23^A43^A13;
838
+ C4 = A44^A14^A34^A04^A24;
839
+ D0 = C4^ROL64(C1, 1);
840
+ D1 = C0^ROL64(C2, 1);
841
+ D2 = C1^ROL64(C3, 1);
842
+ D3 = C2^ROL64(C4, 1);
843
+ D4 = C3^ROL64(C0, 1);
844
+
845
+ B0 = (A00^D0);
846
+ B1 = ROL64((A31^D1), 44);
847
+ B2 = ROL64((A12^D2), 43);
848
+ B3 = ROL64((A43^D3), 21);
849
+ B4 = ROL64((A24^D4), 14);
850
+ A00 = B0 ^((~B1)& B2 );
851
+ A00 ^= RC[i+1];
852
+ A31 = B1 ^((~B2)& B3 );
853
+ A12 = B2 ^((~B3)& B4 );
854
+ A43 = B3 ^((~B4)& B0 );
855
+ A24 = B4 ^((~B0)& B1 );
856
+
857
+ B2 = ROL64((A40^D0), 3);
858
+ B3 = ROL64((A21^D1), 45);
859
+ B4 = ROL64((A02^D2), 61);
860
+ B0 = ROL64((A33^D3), 28);
861
+ B1 = ROL64((A14^D4), 20);
862
+ A40 = B0 ^((~B1)& B2 );
863
+ A21 = B1 ^((~B2)& B3 );
864
+ A02 = B2 ^((~B3)& B4 );
865
+ A33 = B3 ^((~B4)& B0 );
866
+ A14 = B4 ^((~B0)& B1 );
867
+
868
+ B4 = ROL64((A30^D0), 18);
869
+ B0 = ROL64((A11^D1), 1);
870
+ B1 = ROL64((A42^D2), 6);
871
+ B2 = ROL64((A23^D3), 25);
872
+ B3 = ROL64((A04^D4), 8);
873
+ A30 = B0 ^((~B1)& B2 );
874
+ A11 = B1 ^((~B2)& B3 );
875
+ A42 = B2 ^((~B3)& B4 );
876
+ A23 = B3 ^((~B4)& B0 );
877
+ A04 = B4 ^((~B0)& B1 );
878
+
879
+ B1 = ROL64((A20^D0), 36);
880
+ B2 = ROL64((A01^D1), 10);
881
+ B3 = ROL64((A32^D2), 15);
882
+ B4 = ROL64((A13^D3), 56);
883
+ B0 = ROL64((A44^D4), 27);
884
+ A20 = B0 ^((~B1)& B2 );
885
+ A01 = B1 ^((~B2)& B3 );
886
+ A32 = B2 ^((~B3)& B4 );
887
+ A13 = B3 ^((~B4)& B0 );
888
+ A44 = B4 ^((~B0)& B1 );
889
+
890
+ B3 = ROL64((A10^D0), 41);
891
+ B4 = ROL64((A41^D1), 2);
892
+ B0 = ROL64((A22^D2), 62);
893
+ B1 = ROL64((A03^D3), 55);
894
+ B2 = ROL64((A34^D4), 39);
895
+ A10 = B0 ^((~B1)& B2 );
896
+ A41 = B1 ^((~B2)& B3 );
897
+ A22 = B2 ^((~B3)& B4 );
898
+ A03 = B3 ^((~B4)& B0 );
899
+ A34 = B4 ^((~B0)& B1 );
900
+
901
+ C0 = A00^A40^A30^A20^A10;
902
+ C1 = A31^A21^A11^A01^A41;
903
+ C2 = A12^A02^A42^A32^A22;
904
+ C3 = A43^A33^A23^A13^A03;
905
+ C4 = A24^A14^A04^A44^A34;
906
+ D0 = C4^ROL64(C1, 1);
907
+ D1 = C0^ROL64(C2, 1);
908
+ D2 = C1^ROL64(C3, 1);
909
+ D3 = C2^ROL64(C4, 1);
910
+ D4 = C3^ROL64(C0, 1);
911
+
912
+ B0 = (A00^D0);
913
+ B1 = ROL64((A21^D1), 44);
914
+ B2 = ROL64((A42^D2), 43);
915
+ B3 = ROL64((A13^D3), 21);
916
+ B4 = ROL64((A34^D4), 14);
917
+ A00 = B0 ^((~B1)& B2 );
918
+ A00 ^= RC[i+2];
919
+ A21 = B1 ^((~B2)& B3 );
920
+ A42 = B2 ^((~B3)& B4 );
921
+ A13 = B3 ^((~B4)& B0 );
922
+ A34 = B4 ^((~B0)& B1 );
923
+
924
+ B2 = ROL64((A30^D0), 3);
925
+ B3 = ROL64((A01^D1), 45);
926
+ B4 = ROL64((A22^D2), 61);
927
+ B0 = ROL64((A43^D3), 28);
928
+ B1 = ROL64((A14^D4), 20);
929
+ A30 = B0 ^((~B1)& B2 );
930
+ A01 = B1 ^((~B2)& B3 );
931
+ A22 = B2 ^((~B3)& B4 );
932
+ A43 = B3 ^((~B4)& B0 );
933
+ A14 = B4 ^((~B0)& B1 );
934
+
935
+ B4 = ROL64((A10^D0), 18);
936
+ B0 = ROL64((A31^D1), 1);
937
+ B1 = ROL64((A02^D2), 6);
938
+ B2 = ROL64((A23^D3), 25);
939
+ B3 = ROL64((A44^D4), 8);
940
+ A10 = B0 ^((~B1)& B2 );
941
+ A31 = B1 ^((~B2)& B3 );
942
+ A02 = B2 ^((~B3)& B4 );
943
+ A23 = B3 ^((~B4)& B0 );
944
+ A44 = B4 ^((~B0)& B1 );
945
+
946
+ B1 = ROL64((A40^D0), 36);
947
+ B2 = ROL64((A11^D1), 10);
948
+ B3 = ROL64((A32^D2), 15);
949
+ B4 = ROL64((A03^D3), 56);
950
+ B0 = ROL64((A24^D4), 27);
951
+ A40 = B0 ^((~B1)& B2 );
952
+ A11 = B1 ^((~B2)& B3 );
953
+ A32 = B2 ^((~B3)& B4 );
954
+ A03 = B3 ^((~B4)& B0 );
955
+ A24 = B4 ^((~B0)& B1 );
956
+
957
+ B3 = ROL64((A20^D0), 41);
958
+ B4 = ROL64((A41^D1), 2);
959
+ B0 = ROL64((A12^D2), 62);
960
+ B1 = ROL64((A33^D3), 55);
961
+ B2 = ROL64((A04^D4), 39);
962
+ A20 = B0 ^((~B1)& B2 );
963
+ A41 = B1 ^((~B2)& B3 );
964
+ A12 = B2 ^((~B3)& B4 );
965
+ A33 = B3 ^((~B4)& B0 );
966
+ A04 = B4 ^((~B0)& B1 );
967
+
968
+ C0 = A00^A30^A10^A40^A20;
969
+ C1 = A21^A01^A31^A11^A41;
970
+ C2 = A42^A22^A02^A32^A12;
971
+ C3 = A13^A43^A23^A03^A33;
972
+ C4 = A34^A14^A44^A24^A04;
973
+ D0 = C4^ROL64(C1, 1);
974
+ D1 = C0^ROL64(C2, 1);
975
+ D2 = C1^ROL64(C3, 1);
976
+ D3 = C2^ROL64(C4, 1);
977
+ D4 = C3^ROL64(C0, 1);
978
+
979
+ B0 = (A00^D0);
980
+ B1 = ROL64((A01^D1), 44);
981
+ B2 = ROL64((A02^D2), 43);
982
+ B3 = ROL64((A03^D3), 21);
983
+ B4 = ROL64((A04^D4), 14);
984
+ A00 = B0 ^((~B1)& B2 );
985
+ A00 ^= RC[i+3];
986
+ A01 = B1 ^((~B2)& B3 );
987
+ A02 = B2 ^((~B3)& B4 );
988
+ A03 = B3 ^((~B4)& B0 );
989
+ A04 = B4 ^((~B0)& B1 );
990
+
991
+ B2 = ROL64((A10^D0), 3);
992
+ B3 = ROL64((A11^D1), 45);
993
+ B4 = ROL64((A12^D2), 61);
994
+ B0 = ROL64((A13^D3), 28);
995
+ B1 = ROL64((A14^D4), 20);
996
+ A10 = B0 ^((~B1)& B2 );
997
+ A11 = B1 ^((~B2)& B3 );
998
+ A12 = B2 ^((~B3)& B4 );
999
+ A13 = B3 ^((~B4)& B0 );
1000
+ A14 = B4 ^((~B0)& B1 );
1001
+
1002
+ B4 = ROL64((A20^D0), 18);
1003
+ B0 = ROL64((A21^D1), 1);
1004
+ B1 = ROL64((A22^D2), 6);
1005
+ B2 = ROL64((A23^D3), 25);
1006
+ B3 = ROL64((A24^D4), 8);
1007
+ A20 = B0 ^((~B1)& B2 );
1008
+ A21 = B1 ^((~B2)& B3 );
1009
+ A22 = B2 ^((~B3)& B4 );
1010
+ A23 = B3 ^((~B4)& B0 );
1011
+ A24 = B4 ^((~B0)& B1 );
1012
+
1013
+ B1 = ROL64((A30^D0), 36);
1014
+ B2 = ROL64((A31^D1), 10);
1015
+ B3 = ROL64((A32^D2), 15);
1016
+ B4 = ROL64((A33^D3), 56);
1017
+ B0 = ROL64((A34^D4), 27);
1018
+ A30 = B0 ^((~B1)& B2 );
1019
+ A31 = B1 ^((~B2)& B3 );
1020
+ A32 = B2 ^((~B3)& B4 );
1021
+ A33 = B3 ^((~B4)& B0 );
1022
+ A34 = B4 ^((~B0)& B1 );
1023
+
1024
+ B3 = ROL64((A40^D0), 41);
1025
+ B4 = ROL64((A41^D1), 2);
1026
+ B0 = ROL64((A42^D2), 62);
1027
+ B1 = ROL64((A43^D3), 55);
1028
+ B2 = ROL64((A44^D4), 39);
1029
+ A40 = B0 ^((~B1)& B2 );
1030
+ A41 = B1 ^((~B2)& B3 );
1031
+ A42 = B2 ^((~B3)& B4 );
1032
+ A43 = B3 ^((~B4)& B0 );
1033
+ A44 = B4 ^((~B0)& B1 );
1034
+ }
1035
+}
1036
+
1037
+/*
1038
+** Initialize a new hash. iSize determines the size of the hash
1039
+** in bits and should be one of 224, 256, 384, or 512. Or iSize
1040
+** can be zero to use the default hash size of 256 bits.
1041
+*/
1042
+static void SHA3Init(SHA3Context *p, int iSize){
1043
+ memset(p, 0, sizeof(*p));
1044
+ if( iSize>=128 && iSize<=512 ){
1045
+ p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1046
+ }else{
1047
+ p->nRate = (1600 - 2*256)/8;
1048
+ }
1049
+#if SHA3_BYTEORDER==1234
1050
+ /* Known to be little-endian at compile-time. No-op */
1051
+#elif SHA3_BYTEORDER==4321
1052
+ p->ixMask = 7; /* Big-endian */
1053
+#else
1054
+ {
1055
+ static unsigned int one = 1;
1056
+ if( 1==*(unsigned char*)&one ){
1057
+ /* Little endian. No byte swapping. */
1058
+ p->ixMask = 0;
1059
+ }else{
1060
+ /* Big endian. Byte swap. */
1061
+ p->ixMask = 7;
1062
+ }
1063
+ }
1064
+#endif
1065
+}
1066
+
1067
+/*
1068
+** Make consecutive calls to the SHA3Update function to add new content
1069
+** to the hash
1070
+*/
1071
+static void SHA3Update(
1072
+ SHA3Context *p,
1073
+ const unsigned char *aData,
1074
+ unsigned int nData
1075
+){
1076
+ unsigned int i = 0;
1077
+#if SHA3_BYTEORDER==1234
1078
+ if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1079
+ for(; i+7<nData; i+=8){
1080
+ p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1081
+ p->nLoaded += 8;
1082
+ if( p->nLoaded>=p->nRate ){
1083
+ KeccakF1600Step(p);
1084
+ p->nLoaded = 0;
1085
+ }
1086
+ }
1087
+ }
1088
+#endif
1089
+ for(; i<nData; i++){
1090
+#if SHA3_BYTEORDER==1234
1091
+ p->u.x[p->nLoaded] ^= aData[i];
1092
+#elif SHA3_BYTEORDER==4321
1093
+ p->u.x[p->nLoaded^0x07] ^= aData[i];
1094
+#else
1095
+ p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1096
+#endif
1097
+ p->nLoaded++;
1098
+ if( p->nLoaded==p->nRate ){
1099
+ KeccakF1600Step(p);
1100
+ p->nLoaded = 0;
1101
+ }
1102
+ }
1103
+}
1104
+
1105
+/*
1106
+** After all content has been added, invoke SHA3Final() to compute
1107
+** the final hash. The function returns a pointer to the binary
1108
+** hash value.
1109
+*/
1110
+static unsigned char *SHA3Final(SHA3Context *p){
1111
+ unsigned int i;
1112
+ if( p->nLoaded==p->nRate-1 ){
1113
+ const unsigned char c1 = 0x86;
1114
+ SHA3Update(p, &c1, 1);
1115
+ }else{
1116
+ const unsigned char c2 = 0x06;
1117
+ const unsigned char c3 = 0x80;
1118
+ SHA3Update(p, &c2, 1);
1119
+ p->nLoaded = p->nRate - 1;
1120
+ SHA3Update(p, &c3, 1);
1121
+ }
1122
+ for(i=0; i<p->nRate; i++){
1123
+ p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1124
+ }
1125
+ return &p->u.x[p->nRate];
1126
+}
1127
+
1128
+/*
1129
+** Implementation of the sha3(X,SIZE) function.
1130
+**
1131
+** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
1132
+** size is 256. If X is a BLOB, it is hashed as is.
1133
+** For all other non-NULL types of input, X is converted into a UTF-8 string
1134
+** and the string is hashed without the trailing 0x00 terminator. The hash
1135
+** of a NULL value is NULL.
1136
+*/
1137
+static void sha3Func(
1138
+ sqlite3_context *context,
1139
+ int argc,
1140
+ sqlite3_value **argv
1141
+){
1142
+ SHA3Context cx;
1143
+ int eType = sqlite3_value_type(argv[0]);
1144
+ int nByte = sqlite3_value_bytes(argv[0]);
1145
+ int iSize;
1146
+ if( argc==1 ){
1147
+ iSize = 256;
1148
+ }else{
1149
+ iSize = sqlite3_value_int(argv[1]);
1150
+ if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1151
+ sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1152
+ "384 512", -1);
1153
+ return;
1154
+ }
1155
+ }
1156
+ if( eType==SQLITE_NULL ) return;
1157
+ SHA3Init(&cx, iSize);
1158
+ if( eType==SQLITE_BLOB ){
1159
+ SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1160
+ }else{
1161
+ SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1162
+ }
1163
+ sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1164
+}
1165
+
1166
+/* Compute a string using sqlite3_vsnprintf() with a maximum length
1167
+** of 50 bytes and add it to the hash.
1168
+*/
1169
+static void hash_step_vformat(
1170
+ SHA3Context *p, /* Add content to this context */
1171
+ const char *zFormat,
1172
+ ...
1173
+){
1174
+ va_list ap;
1175
+ int n;
1176
+ char zBuf[50];
1177
+ va_start(ap, zFormat);
1178
+ sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1179
+ va_end(ap);
1180
+ n = (int)strlen(zBuf);
1181
+ SHA3Update(p, (unsigned char*)zBuf, n);
1182
+}
1183
+
1184
+/*
1185
+** Implementation of the sha3_query(SQL,SIZE) function.
1186
+**
1187
+** This function compiles and runs the SQL statement(s) given in the
1188
+** argument. The results are hashed using a SIZE-bit SHA3. The default
1189
+** size is 256.
1190
+**
1191
+** The format of the byte stream that is hashed is summarized as follows:
1192
+**
1193
+** S<n>:<sql>
1194
+** R
1195
+** N
1196
+** I<int>
1197
+** F<ieee-float>
1198
+** B<size>:<bytes>
1199
+** T<size>:<text>
1200
+**
1201
+** <sql> is the original SQL text for each statement run and <n> is
1202
+** the size of that text. The SQL text is UTF-8. A single R character
1203
+** occurs before the start of each row. N means a NULL value.
1204
+** I mean an 8-byte little-endian integer <int>. F is a floating point
1205
+** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1206
+** B means blobs of <size> bytes. T means text rendered as <size>
1207
+** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII
1208
+** text integers.
1209
+**
1210
+** For each SQL statement in the X input, there is one S segment. Each
1211
+** S segment is followed by zero or more R segments, one for each row in the
1212
+** result set. After each R, there are one or more N, I, F, B, or T segments,
1213
+** one for each column in the result set. Segments are concatentated directly
1214
+** with no delimiters of any kind.
1215
+*/
1216
+static void sha3QueryFunc(
1217
+ sqlite3_context *context,
1218
+ int argc,
1219
+ sqlite3_value **argv
1220
+){
1221
+ sqlite3 *db = sqlite3_context_db_handle(context);
1222
+ const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1223
+ sqlite3_stmt *pStmt = 0;
1224
+ int nCol; /* Number of columns in the result set */
1225
+ int i; /* Loop counter */
1226
+ int rc;
1227
+ int n;
1228
+ const char *z;
1229
+ SHA3Context cx;
1230
+ int iSize;
1231
+
1232
+ if( argc==1 ){
1233
+ iSize = 256;
1234
+ }else{
1235
+ iSize = sqlite3_value_int(argv[1]);
1236
+ if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1237
+ sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1238
+ "384 512", -1);
1239
+ return;
1240
+ }
1241
+ }
1242
+ if( zSql==0 ) return;
1243
+ SHA3Init(&cx, iSize);
1244
+ while( zSql[0] ){
1245
+ rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
1246
+ if( rc ){
1247
+ char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
1248
+ zSql, sqlite3_errmsg(db));
1249
+ sqlite3_finalize(pStmt);
1250
+ sqlite3_result_error(context, zMsg, -1);
1251
+ sqlite3_free(zMsg);
1252
+ return;
1253
+ }
1254
+ if( !sqlite3_stmt_readonly(pStmt) ){
1255
+ char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
1256
+ sqlite3_finalize(pStmt);
1257
+ sqlite3_result_error(context, zMsg, -1);
1258
+ sqlite3_free(zMsg);
1259
+ return;
1260
+ }
1261
+ nCol = sqlite3_column_count(pStmt);
1262
+ z = sqlite3_sql(pStmt);
1263
+ if( z==0 ){
1264
+ sqlite3_finalize(pStmt);
1265
+ continue;
1266
+ }
1267
+ n = (int)strlen(z);
1268
+ hash_step_vformat(&cx,"S%d:",n);
1269
+ SHA3Update(&cx,(unsigned char*)z,n);
1270
+
1271
+ /* Compute a hash over the result of the query */
1272
+ while( SQLITE_ROW==sqlite3_step(pStmt) ){
1273
+ SHA3Update(&cx,(const unsigned char*)"R",1);
1274
+ for(i=0; i<nCol; i++){
1275
+ switch( sqlite3_column_type(pStmt,i) ){
1276
+ case SQLITE_NULL: {
1277
+ SHA3Update(&cx, (const unsigned char*)"N",1);
1278
+ break;
1279
+ }
1280
+ case SQLITE_INTEGER: {
1281
+ sqlite3_uint64 u;
1282
+ int j;
1283
+ unsigned char x[9];
1284
+ sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
1285
+ memcpy(&u, &v, 8);
1286
+ for(j=8; j>=1; j--){
1287
+ x[j] = u & 0xff;
1288
+ u >>= 8;
1289
+ }
1290
+ x[0] = 'I';
1291
+ SHA3Update(&cx, x, 9);
1292
+ break;
1293
+ }
1294
+ case SQLITE_FLOAT: {
1295
+ sqlite3_uint64 u;
1296
+ int j;
1297
+ unsigned char x[9];
1298
+ double r = sqlite3_column_double(pStmt,i);
1299
+ memcpy(&u, &r, 8);
1300
+ for(j=8; j>=1; j--){
1301
+ x[j] = u & 0xff;
1302
+ u >>= 8;
1303
+ }
1304
+ x[0] = 'F';
1305
+ SHA3Update(&cx,x,9);
1306
+ break;
1307
+ }
1308
+ case SQLITE_TEXT: {
1309
+ int n2 = sqlite3_column_bytes(pStmt, i);
1310
+ const unsigned char *z2 = sqlite3_column_text(pStmt, i);
1311
+ hash_step_vformat(&cx,"T%d:",n2);
1312
+ SHA3Update(&cx, z2, n2);
1313
+ break;
1314
+ }
1315
+ case SQLITE_BLOB: {
1316
+ int n2 = sqlite3_column_bytes(pStmt, i);
1317
+ const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
1318
+ hash_step_vformat(&cx,"B%d:",n2);
1319
+ SHA3Update(&cx, z2, n2);
1320
+ break;
1321
+ }
1322
+ }
1323
+ }
1324
+ }
1325
+ sqlite3_finalize(pStmt);
1326
+ }
1327
+ sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1328
+}
1329
+/* End of SHA3 hashing logic copy/pasted from ../ext/misc/shathree.c
1330
+********************************************************************************/
5801331
5811332
#if defined(SQLITE_ENABLE_SESSION)
5821333
/*
5831334
** State information for a single open session
5841335
*/
@@ -608,17 +1359,14 @@
6081359
** instance of the following structure.
6091360
*/
6101361
typedef struct ShellState ShellState;
6111362
struct ShellState {
6121363
sqlite3 *db; /* The database */
613
- int echoOn; /* True to echo input commands */
6141364
int autoExplain; /* Automatically turn on .explain mode */
6151365
int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
6161366
int statsOn; /* True to display memory stats before each finalize */
6171367
int scanstatsOn; /* True to display scan stats before each finalize */
618
- int countChanges; /* True to display change counts */
619
- int backslashOn; /* Resolve C-style \x escapes in SQL input text */
6201368
int outCount; /* Revert to stdout when reaching zero */
6211369
int cnt; /* Number of records displayed so far */
6221370
FILE *out; /* Write results here */
6231371
FILE *traceOut; /* Output for sqlite3_trace() */
6241372
int nErr; /* Number of errors seen */
@@ -653,13 +1401,24 @@
6531401
};
6541402
6551403
/*
6561404
** These are the allowed shellFlgs values
6571405
*/
658
-#define SHFLG_Scratch 0x00001 /* The --scratch option is used */
659
-#define SHFLG_Pagecache 0x00002 /* The --pagecache option is used */
660
-#define SHFLG_Lookaside 0x00004 /* Lookaside memory is used */
1406
+#define SHFLG_Scratch 0x00000001 /* The --scratch option is used */
1407
+#define SHFLG_Pagecache 0x00000002 /* The --pagecache option is used */
1408
+#define SHFLG_Lookaside 0x00000004 /* Lookaside memory is used */
1409
+#define SHFLG_Backslash 0x00000008 /* The --backslash option is used */
1410
+#define SHFLG_PreserveRowid 0x00000010 /* .dump preserves rowid values */
1411
+#define SHFLG_CountChanges 0x00000020 /* .changes setting */
1412
+#define SHFLG_Echo 0x00000040 /* .echo or --echo setting */
1413
+
1414
+/*
1415
+** Macros for testing and setting shellFlgs
1416
+*/
1417
+#define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
1418
+#define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
1419
+#define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
6611420
6621421
/*
6631422
** These are the allowed modes.
6641423
*/
6651424
#define MODE_Line 0 /* One column per line. Blank line between records */
@@ -1292,92 +2051,106 @@
12922051
*/
12932052
static int callback(void *pArg, int nArg, char **azArg, char **azCol){
12942053
/* since we don't have type info, call the shell_callback with a NULL value */
12952054
return shell_callback(pArg, nArg, azArg, azCol, NULL);
12962055
}
2056
+
2057
+/*
2058
+** This is the callback routine from sqlite3_exec() that appends all
2059
+** output onto the end of a ShellText object.
2060
+*/
2061
+static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2062
+ ShellText *p = (ShellText*)pArg;
2063
+ int i;
2064
+ if( p->n ) appendText(p, "|", 0);
2065
+ for(i=0; i<nArg; i++){
2066
+ if( i ) appendText(p, ",", 0);
2067
+ if( azArg[i] ) appendText(p, azArg[i], 0);
2068
+ }
2069
+ return 0;
2070
+}
2071
+
2072
+/*
2073
+** Generate an appropriate SELFTEST table in the main database.
2074
+*/
2075
+static void createSelftestTable(ShellState *p){
2076
+ char *zErrMsg = 0;
2077
+ sqlite3_exec(p->db,
2078
+ "SAVEPOINT selftest_init;\n"
2079
+ "CREATE TABLE IF NOT EXISTS selftest(\n"
2080
+ " tno INTEGER PRIMARY KEY,\n" /* Test number */
2081
+ " op TEXT,\n" /* Operator: memo run */
2082
+ " cmd TEXT,\n" /* Command text */
2083
+ " ans TEXT\n" /* Desired answer */
2084
+ ");"
2085
+ "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2086
+ "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2087
+ " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2088
+ " 'memo','Tests generated by --init');\n"
2089
+ "INSERT INTO [_shell$self]\n"
2090
+ " SELECT 'run',\n"
2091
+ " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2092
+ "FROM sqlite_master ORDER BY 2'',224))',\n"
2093
+ " hex(sha3_query('SELECT type,name,tbl_name,sql "
2094
+ "FROM sqlite_master ORDER BY 2',224));\n"
2095
+ "INSERT INTO [_shell$self]\n"
2096
+ " SELECT 'run',"
2097
+ " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2098
+ " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2099
+ " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2100
+ " FROM (\n"
2101
+ " SELECT name FROM sqlite_master\n"
2102
+ " WHERE type='table'\n"
2103
+ " AND name<>'selftest'\n"
2104
+ " AND coalesce(rootpage,0)>0\n"
2105
+ " )\n"
2106
+ " ORDER BY name;\n"
2107
+ "INSERT INTO [_shell$self]\n"
2108
+ " VALUES('run','PRAGMA integrity_check','ok');\n"
2109
+ "INSERT INTO selftest(tno,op,cmd,ans)"
2110
+ " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2111
+ "DROP TABLE [_shell$self];"
2112
+ ,0,0,&zErrMsg);
2113
+ if( zErrMsg ){
2114
+ utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2115
+ sqlite3_free(zErrMsg);
2116
+ }
2117
+ sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2118
+}
2119
+
12972120
12982121
/*
12992122
** Set the destination table field of the ShellState structure to
13002123
** the name of the table given. Escape any quote characters in the
13012124
** table name.
13022125
*/
13032126
static void set_table_name(ShellState *p, const char *zName){
13042127
int i, n;
1305
- int needQuote;
2128
+ int cQuote;
13062129
char *z;
13072130
13082131
if( p->zDestTable ){
13092132
free(p->zDestTable);
13102133
p->zDestTable = 0;
13112134
}
13122135
if( zName==0 ) return;
1313
- needQuote = !isalpha((unsigned char)*zName) && *zName!='_';
1314
- for(i=n=0; zName[i]; i++, n++){
1315
- if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){
1316
- needQuote = 1;
1317
- if( zName[i]=='\'' ) n++;
1318
- }
1319
- }
1320
- if( needQuote ) n += 2;
2136
+ cQuote = quoteChar(zName);
2137
+ n = strlen30(zName);
2138
+ if( cQuote ) n += 2;
13212139
z = p->zDestTable = malloc( n+1 );
13222140
if( z==0 ){
13232141
raw_printf(stderr,"Error: out of memory\n");
13242142
exit(1);
13252143
}
13262144
n = 0;
1327
- if( needQuote ) z[n++] = '\'';
2145
+ if( cQuote ) z[n++] = cQuote;
13282146
for(i=0; zName[i]; i++){
13292147
z[n++] = zName[i];
1330
- if( zName[i]=='\'' ) z[n++] = '\'';
1331
- }
1332
- if( needQuote ) z[n++] = '\'';
1333
- z[n] = 0;
1334
-}
1335
-
1336
-/* zIn is either a pointer to a NULL-terminated string in memory obtained
1337
-** from malloc(), or a NULL pointer. The string pointed to by zAppend is
1338
-** added to zIn, and the result returned in memory obtained from malloc().
1339
-** zIn, if it was not NULL, is freed.
1340
-**
1341
-** If the third argument, quote, is not '\0', then it is used as a
1342
-** quote character for zAppend.
1343
-*/
1344
-static char *appendText(char *zIn, char const *zAppend, char quote){
1345
- int len;
1346
- int i;
1347
- int nAppend = strlen30(zAppend);
1348
- int nIn = (zIn?strlen30(zIn):0);
1349
-
1350
- len = nAppend+nIn+1;
1351
- if( quote ){
1352
- len += 2;
1353
- for(i=0; i<nAppend; i++){
1354
- if( zAppend[i]==quote ) len++;
1355
- }
1356
- }
1357
-
1358
- zIn = (char *)realloc(zIn, len);
1359
- if( !zIn ){
1360
- return 0;
1361
- }
1362
-
1363
- if( quote ){
1364
- char *zCsr = &zIn[nIn];
1365
- *zCsr++ = quote;
1366
- for(i=0; i<nAppend; i++){
1367
- *zCsr++ = zAppend[i];
1368
- if( zAppend[i]==quote ) *zCsr++ = quote;
1369
- }
1370
- *zCsr++ = quote;
1371
- *zCsr++ = '\0';
1372
- assert( (zCsr-zIn)==len );
1373
- }else{
1374
- memcpy(&zIn[nIn], zAppend, nAppend);
1375
- zIn[len-1] = '\0';
1376
- }
1377
-
1378
- return zIn;
2148
+ if( zName[i]==cQuote ) z[n++] = cQuote;
2149
+ }
2150
+ if( cQuote ) z[n++] = cQuote;
2151
+ z[n] = 0;
13792152
}
13802153
13812154
13822155
/*
13832156
** Execute a query statement that will generate SQL output. Print
@@ -1484,10 +2257,35 @@
14842257
}
14852258
fclose(in);
14862259
}
14872260
#endif
14882261
2262
+/*
2263
+** Display a single line of status using 64-bit values.
2264
+*/
2265
+static void displayStatLine(
2266
+ ShellState *p, /* The shell context */
2267
+ char *zLabel, /* Label for this one line */
2268
+ char *zFormat, /* Format for the result */
2269
+ int iStatusCtrl, /* Which status to display */
2270
+ int bReset /* True to reset the stats */
2271
+){
2272
+ sqlite3_int64 iCur = -1;
2273
+ sqlite3_int64 iHiwtr = -1;
2274
+ int i, nPercent;
2275
+ char zLine[200];
2276
+ sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2277
+ for(i=0, nPercent=0; zFormat[i]; i++){
2278
+ if( zFormat[i]=='%' ) nPercent++;
2279
+ }
2280
+ if( nPercent>1 ){
2281
+ sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2282
+ }else{
2283
+ sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2284
+ }
2285
+ raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2286
+}
14892287
14902288
/*
14912289
** Display memory stats.
14922290
*/
14932291
static int display_stats(
@@ -1497,61 +2295,35 @@
14972295
){
14982296
int iCur;
14992297
int iHiwtr;
15002298
15012299
if( pArg && pArg->out ){
1502
-
1503
- iHiwtr = iCur = -1;
1504
- sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
1505
- raw_printf(pArg->out,
1506
- "Memory Used: %d (max %d) bytes\n",
1507
- iCur, iHiwtr);
1508
- iHiwtr = iCur = -1;
1509
- sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
1510
- raw_printf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n",
1511
- iCur, iHiwtr);
2300
+ displayStatLine(pArg, "Memory Used:",
2301
+ "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2302
+ displayStatLine(pArg, "Number of Outstanding Allocations:",
2303
+ "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
15122304
if( pArg->shellFlgs & SHFLG_Pagecache ){
1513
- iHiwtr = iCur = -1;
1514
- sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
1515
- raw_printf(pArg->out,
1516
- "Number of Pcache Pages Used: %d (max %d) pages\n",
1517
- iCur, iHiwtr);
1518
- }
1519
- iHiwtr = iCur = -1;
1520
- sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset);
1521
- raw_printf(pArg->out,
1522
- "Number of Pcache Overflow Bytes: %d (max %d) bytes\n",
1523
- iCur, iHiwtr);
2305
+ displayStatLine(pArg, "Number of Pcache Pages Used:",
2306
+ "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2307
+ }
2308
+ displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2309
+ "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
15242310
if( pArg->shellFlgs & SHFLG_Scratch ){
1525
- iHiwtr = iCur = -1;
1526
- sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
1527
- raw_printf(pArg->out,
1528
- "Number of Scratch Allocations Used: %d (max %d)\n",
1529
- iCur, iHiwtr);
1530
- }
1531
- iHiwtr = iCur = -1;
1532
- sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset);
1533
- raw_printf(pArg->out,
1534
- "Number of Scratch Overflow Bytes: %d (max %d) bytes\n",
1535
- iCur, iHiwtr);
1536
- iHiwtr = iCur = -1;
1537
- sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset);
1538
- raw_printf(pArg->out, "Largest Allocation: %d bytes\n",
1539
- iHiwtr);
1540
- iHiwtr = iCur = -1;
1541
- sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset);
1542
- raw_printf(pArg->out, "Largest Pcache Allocation: %d bytes\n",
1543
- iHiwtr);
1544
- iHiwtr = iCur = -1;
1545
- sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, bReset);
1546
- raw_printf(pArg->out, "Largest Scratch Allocation: %d bytes\n",
1547
- iHiwtr);
2311
+ displayStatLine(pArg, "Number of Scratch Allocations Used:",
2312
+ "%lld (max %lld)", SQLITE_STATUS_SCRATCH_USED, bReset);
2313
+ }
2314
+ displayStatLine(pArg, "Number of Scratch Overflow Bytes:",
2315
+ "%lld (max %lld) bytes", SQLITE_STATUS_SCRATCH_OVERFLOW, bReset);
2316
+ displayStatLine(pArg, "Largest Allocation:",
2317
+ "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2318
+ displayStatLine(pArg, "Largest Pcache Allocation:",
2319
+ "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2320
+ displayStatLine(pArg, "Largest Scratch Allocation:",
2321
+ "%lld bytes", SQLITE_STATUS_SCRATCH_SIZE, bReset);
15482322
#ifdef YYTRACKMAXSTACKDEPTH
1549
- iHiwtr = iCur = -1;
1550
- sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset);
1551
- raw_printf(pArg->out, "Deepest Parser Stack: %d (max %d)\n",
1552
- iCur, iHiwtr);
2323
+ displayStatLine(pArg, "Deepest Parser Stack:",
2324
+ "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
15532325
#endif
15542326
}
15552327
15562328
if( pArg && pArg->out && db ){
15572329
if( pArg->shellFlgs & SHFLG_Lookaside ){
@@ -1934,11 +2706,11 @@
19342706
pArg->pStmt = pStmt;
19352707
pArg->cnt = 0;
19362708
}
19372709
19382710
/* echo the sql statement if echo on */
1939
- if( pArg && pArg->echoOn ){
2711
+ if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
19402712
utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
19412713
}
19422714
19432715
/* Show the EXPLAIN QUERY PLAN if .eqp is on */
19442716
if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){
@@ -2022,10 +2794,142 @@
20222794
} /* end while */
20232795
20242796
return rc;
20252797
}
20262798
2799
+/*
2800
+** Release memory previously allocated by tableColumnList().
2801
+*/
2802
+static void freeColumnList(char **azCol){
2803
+ int i;
2804
+ for(i=1; azCol[i]; i++){
2805
+ sqlite3_free(azCol[i]);
2806
+ }
2807
+ /* azCol[0] is a static string */
2808
+ sqlite3_free(azCol);
2809
+}
2810
+
2811
+/*
2812
+** Return a list of pointers to strings which are the names of all
2813
+** columns in table zTab. The memory to hold the names is dynamically
2814
+** allocated and must be released by the caller using a subsequent call
2815
+** to freeColumnList().
2816
+**
2817
+** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
2818
+** value that needs to be preserved, then azCol[0] is filled in with the
2819
+** name of the rowid column.
2820
+**
2821
+** The first regular column in the table is azCol[1]. The list is terminated
2822
+** by an entry with azCol[i]==0.
2823
+*/
2824
+static char **tableColumnList(ShellState *p, const char *zTab){
2825
+ char **azCol = 0;
2826
+ sqlite3_stmt *pStmt;
2827
+ char *zSql;
2828
+ int nCol = 0;
2829
+ int nAlloc = 0;
2830
+ int nPK = 0; /* Number of PRIMARY KEY columns seen */
2831
+ int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
2832
+ int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
2833
+ int rc;
2834
+
2835
+ zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
2836
+ rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2837
+ sqlite3_free(zSql);
2838
+ if( rc ) return 0;
2839
+ while( sqlite3_step(pStmt)==SQLITE_ROW ){
2840
+ if( nCol>=nAlloc-2 ){
2841
+ nAlloc = nAlloc*2 + nCol + 10;
2842
+ azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
2843
+ if( azCol==0 ){
2844
+ raw_printf(stderr, "Error: out of memory\n");
2845
+ exit(1);
2846
+ }
2847
+ }
2848
+ azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
2849
+ if( sqlite3_column_int(pStmt, 5) ){
2850
+ nPK++;
2851
+ if( nPK==1
2852
+ && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
2853
+ "INTEGER")==0
2854
+ ){
2855
+ isIPK = 1;
2856
+ }else{
2857
+ isIPK = 0;
2858
+ }
2859
+ }
2860
+ }
2861
+ sqlite3_finalize(pStmt);
2862
+ azCol[0] = 0;
2863
+ azCol[nCol+1] = 0;
2864
+
2865
+ /* The decision of whether or not a rowid really needs to be preserved
2866
+ ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
2867
+ ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
2868
+ ** rowids on tables where the rowid is inaccessible because there are other
2869
+ ** columns in the table named "rowid", "_rowid_", and "oid".
2870
+ */
2871
+ if( preserveRowid && isIPK ){
2872
+ /* If a single PRIMARY KEY column with type INTEGER was seen, then it
2873
+ ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
2874
+ ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
2875
+ ** ROWID aliases. To distinguish these cases, check to see if
2876
+ ** there is a "pk" entry in "PRAGMA index_list". There will be
2877
+ ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
2878
+ */
2879
+ zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
2880
+ " WHERE origin='pk'", zTab);
2881
+ rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2882
+ sqlite3_free(zSql);
2883
+ if( rc ){
2884
+ freeColumnList(azCol);
2885
+ return 0;
2886
+ }
2887
+ rc = sqlite3_step(pStmt);
2888
+ sqlite3_finalize(pStmt);
2889
+ preserveRowid = rc==SQLITE_ROW;
2890
+ }
2891
+ if( preserveRowid ){
2892
+ /* Only preserve the rowid if we can find a name to use for the
2893
+ ** rowid */
2894
+ static char *azRowid[] = { "rowid", "_rowid_", "oid" };
2895
+ int i, j;
2896
+ for(j=0; j<3; j++){
2897
+ for(i=1; i<=nCol; i++){
2898
+ if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
2899
+ }
2900
+ if( i>nCol ){
2901
+ /* At this point, we know that azRowid[j] is not the name of any
2902
+ ** ordinary column in the table. Verify that azRowid[j] is a valid
2903
+ ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
2904
+ ** tables will fail this last check */
2905
+ int rc;
2906
+ rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
2907
+ if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
2908
+ break;
2909
+ }
2910
+ }
2911
+ }
2912
+ return azCol;
2913
+}
2914
+
2915
+/*
2916
+** Toggle the reverse_unordered_selects setting.
2917
+*/
2918
+static void toggleSelectOrder(sqlite3 *db){
2919
+ sqlite3_stmt *pStmt = 0;
2920
+ int iSetting = 0;
2921
+ char zStmt[100];
2922
+ sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
2923
+ if( sqlite3_step(pStmt)==SQLITE_ROW ){
2924
+ iSetting = sqlite3_column_int(pStmt, 0);
2925
+ }
2926
+ sqlite3_finalize(pStmt);
2927
+ sqlite3_snprintf(sizeof(zStmt), zStmt,
2928
+ "PRAGMA reverse_unordered_selects(%d)", !iSetting);
2929
+ sqlite3_exec(db, zStmt, 0, 0, 0);
2930
+}
20272931
20282932
/*
20292933
** This is a different callback routine used for dumping the database.
20302934
** Each row received by this callback consists of a table name,
20312935
** the table type ("index" or "table") and SQL to create the table.
@@ -2034,21 +2938,20 @@
20342938
static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
20352939
int rc;
20362940
const char *zTable;
20372941
const char *zType;
20382942
const char *zSql;
2039
- const char *zPrepStmt = 0;
20402943
ShellState *p = (ShellState *)pArg;
20412944
20422945
UNUSED_PARAMETER(azCol);
20432946
if( nArg!=3 ) return 1;
20442947
zTable = azArg[0];
20452948
zType = azArg[1];
20462949
zSql = azArg[2];
20472950
20482951
if( strcmp(zTable, "sqlite_sequence")==0 ){
2049
- zPrepStmt = "DELETE FROM sqlite_sequence;\n";
2952
+ raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
20502953
}else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
20512954
raw_printf(p->out, "ANALYZE sqlite_master;\n");
20522955
}else if( strncmp(zTable, "sqlite_", 7)==0 ){
20532956
return 0;
20542957
}else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
@@ -2067,62 +2970,74 @@
20672970
}else{
20682971
printSchemaLine(p->out, zSql, ";\n");
20692972
}
20702973
20712974
if( strcmp(zType, "table")==0 ){
2072
- sqlite3_stmt *pTableInfo = 0;
2073
- char *zSelect = 0;
2074
- char *zTableInfo = 0;
2075
- char *zTmp = 0;
2076
- int nRow = 0;
2077
-
2078
- zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);
2079
- zTableInfo = appendText(zTableInfo, zTable, '"');
2080
- zTableInfo = appendText(zTableInfo, ");", 0);
2081
-
2082
- rc = sqlite3_prepare_v2(p->db, zTableInfo, -1, &pTableInfo, 0);
2083
- free(zTableInfo);
2084
- if( rc!=SQLITE_OK || !pTableInfo ){
2085
- return 1;
2086
- }
2087
-
2088
- zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);
2975
+ ShellText sSelect;
2976
+ ShellText sTable;
2977
+ char **azCol;
2978
+ int i;
2979
+ char *savedDestTable;
2980
+ int savedMode;
2981
+
2982
+ azCol = tableColumnList(p, zTable);
2983
+ if( azCol==0 ){
2984
+ p->nErr++;
2985
+ return 0;
2986
+ }
2987
+
20892988
/* Always quote the table name, even if it appears to be pure ascii,
20902989
** in case it is a keyword. Ex: INSERT INTO "table" ... */
2091
- zTmp = appendText(zTmp, zTable, '"');
2092
- if( zTmp ){
2093
- zSelect = appendText(zSelect, zTmp, '\'');
2094
- free(zTmp);
2095
- }
2096
- zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
2097
- rc = sqlite3_step(pTableInfo);
2098
- while( rc==SQLITE_ROW ){
2099
- const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1);
2100
- zSelect = appendText(zSelect, "quote(", 0);
2101
- zSelect = appendText(zSelect, zText, '"');
2102
- rc = sqlite3_step(pTableInfo);
2103
- if( rc==SQLITE_ROW ){
2104
- zSelect = appendText(zSelect, "), ", 0);
2105
- }else{
2106
- zSelect = appendText(zSelect, ") ", 0);
2107
- }
2108
- nRow++;
2109
- }
2110
- rc = sqlite3_finalize(pTableInfo);
2111
- if( rc!=SQLITE_OK || nRow==0 ){
2112
- free(zSelect);
2113
- return 1;
2114
- }
2115
- zSelect = appendText(zSelect, "|| ')' FROM ", 0);
2116
- zSelect = appendText(zSelect, zTable, '"');
2117
-
2118
- rc = run_table_dump_query(p, zSelect, zPrepStmt);
2119
- if( rc==SQLITE_CORRUPT ){
2120
- zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);
2121
- run_table_dump_query(p, zSelect, 0);
2122
- }
2123
- free(zSelect);
2990
+ initText(&sTable);
2991
+ appendText(&sTable, zTable, quoteChar(zTable));
2992
+ /* If preserving the rowid, add a column list after the table name.
2993
+ ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
2994
+ ** instead of the usual "INSERT INTO tab VALUES(...)".
2995
+ */
2996
+ if( azCol[0] ){
2997
+ appendText(&sTable, "(", 0);
2998
+ appendText(&sTable, azCol[0], 0);
2999
+ for(i=1; azCol[i]; i++){
3000
+ appendText(&sTable, ",", 0);
3001
+ appendText(&sTable, azCol[i], quoteChar(azCol[i]));
3002
+ }
3003
+ appendText(&sTable, ")", 0);
3004
+ }
3005
+
3006
+ /* Build an appropriate SELECT statement */
3007
+ initText(&sSelect);
3008
+ appendText(&sSelect, "SELECT ", 0);
3009
+ if( azCol[0] ){
3010
+ appendText(&sSelect, azCol[0], 0);
3011
+ appendText(&sSelect, ",", 0);
3012
+ }
3013
+ for(i=1; azCol[i]; i++){
3014
+ appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
3015
+ if( azCol[i+1] ){
3016
+ appendText(&sSelect, ",", 0);
3017
+ }
3018
+ }
3019
+ freeColumnList(azCol);
3020
+ appendText(&sSelect, " FROM ", 0);
3021
+ appendText(&sSelect, zTable, quoteChar(zTable));
3022
+
3023
+ savedDestTable = p->zDestTable;
3024
+ savedMode = p->mode;
3025
+ p->zDestTable = sTable.z;
3026
+ p->mode = p->cMode = MODE_Insert;
3027
+ rc = shell_exec(p->db, sSelect.z, shell_callback, p, 0);
3028
+ if( (rc&0xff)==SQLITE_CORRUPT ){
3029
+ raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3030
+ toggleSelectOrder(p->db);
3031
+ shell_exec(p->db, sSelect.z, shell_callback, p, 0);
3032
+ toggleSelectOrder(p->db);
3033
+ }
3034
+ p->zDestTable = savedDestTable;
3035
+ p->mode = savedMode;
3036
+ freeText(&sTable);
3037
+ freeText(&sSelect);
3038
+ if( rc ) p->nErr++;
21243039
}
21253040
return 0;
21263041
}
21273042
21283043
/*
@@ -2233,10 +3148,11 @@
22333148
".separator COL ?ROW? Change the column separator and optionally the row\n"
22343149
" separator for both the output mode and .import\n"
22353150
#if defined(SQLITE_ENABLE_SESSION)
22363151
".session CMD ... Create or control sessions\n"
22373152
#endif
3153
+ ".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n"
22383154
".shell CMD ARGS... Run CMD ARGS... in a system shell\n"
22393155
".show Show the current values for various settings\n"
22403156
".stats ?on|off? Show stats or turn stats on or off\n"
22413157
".system CMD ARGS... Run CMD ARGS... in a system shell\n"
22423158
".tables ?TABLE? List names of tables\n"
@@ -2423,14 +3339,10 @@
24233339
static void open_db(ShellState *p, int keepAlive){
24243340
if( p->db==0 ){
24253341
sqlite3_initialize();
24263342
sqlite3_open(p->zDbFilename, &p->db);
24273343
globalDb = p->db;
2428
- if( p->db && sqlite3_errcode(p->db)==SQLITE_OK ){
2429
- sqlite3_create_function(p->db, "shellstatic", 0, SQLITE_UTF8, 0,
2430
- shellstaticFunc, 0, 0);
2431
- }
24323344
if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
24333345
utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
24343346
p->zDbFilename, sqlite3_errmsg(p->db));
24353347
if( keepAlive ) return;
24363348
exit(1);
@@ -2440,10 +3352,18 @@
24403352
#endif
24413353
sqlite3_create_function(p->db, "readfile", 1, SQLITE_UTF8, 0,
24423354
readfileFunc, 0, 0);
24433355
sqlite3_create_function(p->db, "writefile", 2, SQLITE_UTF8, 0,
24443356
writefileFunc, 0, 0);
3357
+ sqlite3_create_function(p->db, "sha3", 1, SQLITE_UTF8, 0,
3358
+ sha3Func, 0, 0);
3359
+ sqlite3_create_function(p->db, "sha3", 2, SQLITE_UTF8, 0,
3360
+ sha3Func, 0, 0);
3361
+ sqlite3_create_function(p->db, "sha3_query", 1, SQLITE_UTF8, 0,
3362
+ sha3QueryFunc, 0, 0);
3363
+ sqlite3_create_function(p->db, "sha3_query", 2, SQLITE_UTF8, 0,
3364
+ sha3QueryFunc, 0, 0);
24453365
}
24463366
}
24473367
24483368
/*
24493369
** Do C-language style dequoting.
@@ -2564,11 +3484,11 @@
25643484
25653485
/*
25663486
** Interpret zArg as either an integer or a boolean value. Return 1 or 0
25673487
** for TRUE and FALSE. Return the integer value if appropriate.
25683488
*/
2569
-static int booleanValue(char *zArg){
3489
+static int booleanValue(const char *zArg){
25703490
int i;
25713491
if( zArg[0]=='0' && zArg[1]=='x' ){
25723492
for(i=2; hexDigitValue(zArg[i])>=0; i++){}
25733493
}else{
25743494
for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
@@ -2582,10 +3502,21 @@
25823502
}
25833503
utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
25843504
zArg);
25853505
return 0;
25863506
}
3507
+
3508
+/*
3509
+** Set or clear a shell flag according to a boolean value.
3510
+*/
3511
+static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
3512
+ if( booleanValue(zArg) ){
3513
+ ShellSetFlag(p, mFlag);
3514
+ }else{
3515
+ ShellClearFlag(p, mFlag);
3516
+ }
3517
+}
25873518
25883519
/*
25893520
** Close an output file, assuming it is not stderr or stdout
25903521
*/
25913522
static void output_file_close(FILE *f){
@@ -3656,11 +4587,11 @@
36564587
test_breakpoint();
36574588
}else
36584589
36594590
if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
36604591
if( nArg==2 ){
3661
- p->countChanges = booleanValue(azArg[1]);
4592
+ setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
36624593
}else{
36634594
raw_printf(stderr, "Usage: .changes on|off\n");
36644595
rc = 1;
36654596
}
36664597
}else
@@ -3720,25 +4651,46 @@
37204651
if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){
37214652
rc = shell_dbinfo_command(p, nArg, azArg);
37224653
}else
37234654
37244655
if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
4656
+ const char *zLike = 0;
4657
+ int i;
4658
+ ShellClearFlag(p, SHFLG_PreserveRowid);
4659
+ for(i=1; i<nArg; i++){
4660
+ if( azArg[i][0]=='-' ){
4661
+ const char *z = azArg[i]+1;
4662
+ if( z[0]=='-' ) z++;
4663
+ if( strcmp(z,"preserve-rowids")==0 ){
4664
+ ShellSetFlag(p, SHFLG_PreserveRowid);
4665
+ }else
4666
+ {
4667
+ raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
4668
+ rc = 1;
4669
+ goto meta_command_exit;
4670
+ }
4671
+ }else if( zLike ){
4672
+ raw_printf(stderr, "Usage: .dump ?--preserve-rowids? ?LIKE-PATTERN?\n");
4673
+ rc = 1;
4674
+ goto meta_command_exit;
4675
+ }else{
4676
+ zLike = azArg[i];
4677
+ }
4678
+ }
37254679
open_db(p, 0);
37264680
/* When playing back a "dump", the content might appear in an order
37274681
** which causes immediate foreign key constraints to be violated.
37284682
** So disable foreign-key constraint enforcement to prevent problems. */
3729
- if( nArg!=1 && nArg!=2 ){
3730
- raw_printf(stderr, "Usage: .dump ?LIKE-PATTERN?\n");
3731
- rc = 1;
3732
- goto meta_command_exit;
3733
- }
37344683
raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
37354684
raw_printf(p->out, "BEGIN TRANSACTION;\n");
37364685
p->writableSchema = 0;
4686
+ /* Set writable_schema=ON since doing so forces SQLite to initialize
4687
+ ** as much of the schema as it can even if the sqlite_master table is
4688
+ ** corrupt. */
37374689
sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
37384690
p->nErr = 0;
3739
- if( nArg==1 ){
4691
+ if( zLike==0 ){
37404692
run_schema_dump_query(p,
37414693
"SELECT name, type, sql FROM sqlite_master "
37424694
"WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
37434695
);
37444696
run_schema_dump_query(p,
@@ -3748,25 +4700,24 @@
37484700
run_table_dump_query(p,
37494701
"SELECT sql FROM sqlite_master "
37504702
"WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
37514703
);
37524704
}else{
3753
- int i;
3754
- for(i=1; i<nArg; i++){
3755
- zShellStatic = azArg[i];
3756
- run_schema_dump_query(p,
3757
- "SELECT name, type, sql FROM sqlite_master "
3758
- "WHERE tbl_name LIKE shellstatic() AND type=='table'"
3759
- " AND sql NOT NULL");
3760
- run_table_dump_query(p,
3761
- "SELECT sql FROM sqlite_master "
3762
- "WHERE sql NOT NULL"
3763
- " AND type IN ('index','trigger','view')"
3764
- " AND tbl_name LIKE shellstatic()", 0
3765
- );
3766
- zShellStatic = 0;
3767
- }
4705
+ char *zSql;
4706
+ zSql = sqlite3_mprintf(
4707
+ "SELECT name, type, sql FROM sqlite_master "
4708
+ "WHERE tbl_name LIKE %Q AND type=='table'"
4709
+ " AND sql NOT NULL", zLike);
4710
+ run_schema_dump_query(p,zSql);
4711
+ sqlite3_free(zSql);
4712
+ zSql = sqlite3_mprintf(
4713
+ "SELECT sql FROM sqlite_master "
4714
+ "WHERE sql NOT NULL"
4715
+ " AND type IN ('index','trigger','view')"
4716
+ " AND tbl_name LIKE %Q", zLike);
4717
+ run_table_dump_query(p, zSql, 0);
4718
+ sqlite3_free(zSql);
37684719
}
37694720
if( p->writableSchema ){
37704721
raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
37714722
p->writableSchema = 0;
37724723
}
@@ -3775,11 +4726,11 @@
37754726
raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
37764727
}else
37774728
37784729
if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
37794730
if( nArg==2 ){
3780
- p->echoOn = booleanValue(azArg[1]);
4731
+ setOrClearFlag(p, SHFLG_Echo, azArg[1]);
37814732
}else{
37824733
raw_printf(stderr, "Usage: .echo on|off\n");
37834734
rc = 1;
37844735
}
37854736
}else
@@ -4572,21 +5523,21 @@
45725523
new_colv[0] = "sql";
45735524
new_colv[1] = 0;
45745525
callback(&data, 1, new_argv, new_colv);
45755526
rc = SQLITE_OK;
45765527
}else{
4577
- zShellStatic = azArg[1];
4578
- rc = sqlite3_exec(p->db,
5528
+ char *zSql;
5529
+ zSql = sqlite3_mprintf(
45795530
"SELECT sql FROM "
45805531
" (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
45815532
" FROM sqlite_master UNION ALL"
45825533
" SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
4583
- "WHERE lower(tbl_name) LIKE shellstatic()"
5534
+ "WHERE lower(tbl_name) LIKE %Q"
45845535
" AND type!='meta' AND sql NOTNULL "
4585
- "ORDER BY rowid",
4586
- callback, &data, &zErrMsg);
4587
- zShellStatic = 0;
5536
+ "ORDER BY rowid", azArg[1]);
5537
+ rc = sqlite3_exec(p->db, zSql, callback, &data, &zErrMsg);
5538
+ sqlite3_free(zSql);
45885539
}
45895540
}else if( nArg==1 ){
45905541
rc = sqlite3_exec(p->db,
45915542
"SELECT sql FROM "
45925543
" (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
@@ -4835,10 +5786,123 @@
48355786
utf8_printf(p->out, "%s", zBuf);
48365787
}
48375788
}
48385789
}else
48395790
#endif
5791
+
5792
+ if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
5793
+ int bIsInit = 0; /* True to initialize the SELFTEST table */
5794
+ int bVerbose = 0; /* Verbose output */
5795
+ int bSelftestExists; /* True if SELFTEST already exists */
5796
+ char **azTest = 0; /* Content of the SELFTEST table */
5797
+ int nRow = 0; /* Number of rows in the SELFTEST table */
5798
+ int nCol = 4; /* Number of columns in the SELFTEST table */
5799
+ int i; /* Loop counter */
5800
+ int nTest = 0; /* Number of tests runs */
5801
+ int nErr = 0; /* Number of errors seen */
5802
+ ShellText str; /* Answer for a query */
5803
+ static char *azDefaultTest[] = {
5804
+ 0, 0, 0, 0,
5805
+ "0", "memo", "Missing SELFTEST table - default checks only", "",
5806
+ "1", "run", "PRAGMA integrity_check", "ok"
5807
+ };
5808
+ static const int nDefaultRow = 2;
5809
+
5810
+ open_db(p,0);
5811
+ for(i=1; i<nArg; i++){
5812
+ const char *z = azArg[i];
5813
+ if( z[0]=='-' && z[1]=='-' ) z++;
5814
+ if( strcmp(z,"-init")==0 ){
5815
+ bIsInit = 1;
5816
+ }else
5817
+ if( strcmp(z,"-v")==0 ){
5818
+ bVerbose++;
5819
+ }else
5820
+ {
5821
+ utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
5822
+ azArg[i], azArg[0]);
5823
+ raw_printf(stderr, "Should be one of: --init -v\n");
5824
+ rc = 1;
5825
+ goto meta_command_exit;
5826
+ }
5827
+ }
5828
+ if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
5829
+ != SQLITE_OK ){
5830
+ bSelftestExists = 0;
5831
+ }else{
5832
+ bSelftestExists = 1;
5833
+ }
5834
+ if( bIsInit ){
5835
+ createSelftestTable(p);
5836
+ bSelftestExists = 1;
5837
+ }
5838
+ if( bSelftestExists ){
5839
+ rc = sqlite3_get_table(p->db,
5840
+ "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
5841
+ &azTest, &nRow, &nCol, 0);
5842
+ if( rc ){
5843
+ raw_printf(stderr, "Error querying the selftest table\n");
5844
+ rc = 1;
5845
+ sqlite3_free_table(azTest);
5846
+ goto meta_command_exit;
5847
+ }else if( nRow==0 ){
5848
+ sqlite3_free_table(azTest);
5849
+ azTest = azDefaultTest;
5850
+ nRow = nDefaultRow;
5851
+ }
5852
+ }else{
5853
+ azTest = azDefaultTest;
5854
+ nRow = nDefaultRow;
5855
+ }
5856
+ initText(&str);
5857
+ appendText(&str, "x", 0);
5858
+ for(i=1; i<=nRow; i++){
5859
+ int tno = atoi(azTest[i*nCol]);
5860
+ const char *zOp = azTest[i*nCol+1];
5861
+ const char *zSql = azTest[i*nCol+2];
5862
+ const char *zAns = azTest[i*nCol+3];
5863
+
5864
+ if( bVerbose>0 ){
5865
+ char *zQuote = sqlite3_mprintf("%q", zSql);
5866
+ printf("%d: %s %s\n", tno, zOp, zSql);
5867
+ sqlite3_free(zQuote);
5868
+ }
5869
+ if( strcmp(zOp,"memo")==0 ){
5870
+ utf8_printf(p->out, "%s\n", zSql);
5871
+ }else
5872
+ if( strcmp(zOp,"run")==0 ){
5873
+ char *zErrMsg = 0;
5874
+ str.n = 0;
5875
+ str.z[0] = 0;
5876
+ rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
5877
+ nTest++;
5878
+ if( bVerbose ){
5879
+ utf8_printf(p->out, "Result: %s\n", str.z);
5880
+ }
5881
+ if( rc || zErrMsg ){
5882
+ nErr++;
5883
+ rc = 1;
5884
+ utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
5885
+ sqlite3_free(zErrMsg);
5886
+ }else if( strcmp(zAns,str.z)!=0 ){
5887
+ nErr++;
5888
+ rc = 1;
5889
+ utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
5890
+ utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
5891
+ }
5892
+ }else
5893
+ {
5894
+ utf8_printf(stderr,
5895
+ "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
5896
+ rc = 1;
5897
+ break;
5898
+ }
5899
+ }
5900
+ freeText(&str);
5901
+ if( azTest!=azDefaultTest ) sqlite3_free_table(azTest);
5902
+ utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
5903
+ }else
48405904
48415905
if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
48425906
if( nArg<2 || nArg>3 ){
48435907
raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
48445908
rc = 1;
@@ -4850,10 +5914,126 @@
48505914
if( nArg>=3 ){
48515915
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
48525916
"%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
48535917
}
48545918
}else
5919
+
5920
+ if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
5921
+ const char *zLike = 0; /* Which table to checksum. 0 means everything */
5922
+ int i; /* Loop counter */
5923
+ int bSchema = 0; /* Also hash the schema */
5924
+ int bSeparate = 0; /* Hash each table separately */
5925
+ int iSize = 224; /* Hash algorithm to use */
5926
+ int bDebug = 0; /* Only show the query that would have run */
5927
+ sqlite3_stmt *pStmt; /* For querying tables names */
5928
+ char *zSql; /* SQL to be run */
5929
+ char *zSep; /* Separator */
5930
+ ShellText sSql; /* Complete SQL for the query to run the hash */
5931
+ ShellText sQuery; /* Set of queries used to read all content */
5932
+ open_db(p, 0);
5933
+ for(i=1; i<nArg; i++){
5934
+ const char *z = azArg[i];
5935
+ if( z[0]=='-' ){
5936
+ z++;
5937
+ if( z[0]=='-' ) z++;
5938
+ if( strcmp(z,"schema")==0 ){
5939
+ bSchema = 1;
5940
+ }else
5941
+ if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
5942
+ || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
5943
+ ){
5944
+ iSize = atoi(&z[5]);
5945
+ }else
5946
+ if( strcmp(z,"debug")==0 ){
5947
+ bDebug = 1;
5948
+ }else
5949
+ {
5950
+ utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
5951
+ azArg[i], azArg[0]);
5952
+ raw_printf(stderr, "Should be one of: --schema"
5953
+ " --sha3-224 --sha3-255 --sha3-384 --sha3-512\n");
5954
+ rc = 1;
5955
+ goto meta_command_exit;
5956
+ }
5957
+ }else if( zLike ){
5958
+ raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
5959
+ rc = 1;
5960
+ goto meta_command_exit;
5961
+ }else{
5962
+ zLike = z;
5963
+ bSeparate = 1;
5964
+ if( sqlite3_strlike("sqlite_%", zLike, 0)==0 ) bSchema = 1;
5965
+ }
5966
+ }
5967
+ if( bSchema ){
5968
+ zSql = "SELECT lower(name) FROM sqlite_master"
5969
+ " WHERE type='table' AND coalesce(rootpage,0)>1"
5970
+ " UNION ALL SELECT 'sqlite_master'"
5971
+ " ORDER BY 1 collate nocase";
5972
+ }else{
5973
+ zSql = "SELECT lower(name) FROM sqlite_master"
5974
+ " WHERE type='table' AND coalesce(rootpage,0)>1"
5975
+ " AND name NOT LIKE 'sqlite_%'"
5976
+ " ORDER BY 1 collate nocase";
5977
+ }
5978
+ sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5979
+ initText(&sQuery);
5980
+ initText(&sSql);
5981
+ appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
5982
+ zSep = "VALUES(";
5983
+ while( SQLITE_ROW==sqlite3_step(pStmt) ){
5984
+ const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
5985
+ if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
5986
+ if( strncmp(zTab, "sqlite_",7)!=0 ){
5987
+ appendText(&sQuery,"SELECT * FROM ", 0);
5988
+ appendText(&sQuery,zTab,'"');
5989
+ appendText(&sQuery," NOT INDEXED;", 0);
5990
+ }else if( strcmp(zTab, "sqlite_master")==0 ){
5991
+ appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
5992
+ " ORDER BY name;", 0);
5993
+ }else if( strcmp(zTab, "sqlite_sequence")==0 ){
5994
+ appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
5995
+ " ORDER BY name;", 0);
5996
+ }else if( strcmp(zTab, "sqlite_stat1")==0 ){
5997
+ appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
5998
+ " ORDER BY tbl,idx;", 0);
5999
+ }else if( strcmp(zTab, "sqlite_stat3")==0
6000
+ || strcmp(zTab, "sqlite_stat4")==0 ){
6001
+ appendText(&sQuery, "SELECT * FROM ", 0);
6002
+ appendText(&sQuery, zTab, 0);
6003
+ appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
6004
+ }
6005
+ appendText(&sSql, zSep, 0);
6006
+ appendText(&sSql, sQuery.z, '\'');
6007
+ sQuery.n = 0;
6008
+ appendText(&sSql, ",", 0);
6009
+ appendText(&sSql, zTab, '\'');
6010
+ zSep = "),(";
6011
+ }
6012
+ sqlite3_finalize(pStmt);
6013
+ if( bSeparate ){
6014
+ zSql = sqlite3_mprintf(
6015
+ "%s))"
6016
+ " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
6017
+ " FROM [sha3sum$query]",
6018
+ sSql.z, iSize);
6019
+ }else{
6020
+ zSql = sqlite3_mprintf(
6021
+ "%s))"
6022
+ " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
6023
+ " FROM [sha3sum$query]",
6024
+ sSql.z, iSize);
6025
+ }
6026
+ freeText(&sQuery);
6027
+ freeText(&sSql);
6028
+ if( bDebug ){
6029
+ utf8_printf(p->out, "%s\n", zSql);
6030
+ }else{
6031
+ shell_exec(p->db, zSql, shell_callback, p, 0);
6032
+ }
6033
+ sqlite3_free(zSql);
6034
+ }else
48556035
48566036
if( c=='s'
48576037
&& (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
48586038
){
48596039
char *zCmd;
@@ -4879,11 +6059,12 @@
48796059
if( nArg!=1 ){
48806060
raw_printf(stderr, "Usage: .show\n");
48816061
rc = 1;
48826062
goto meta_command_exit;
48836063
}
4884
- utf8_printf(p->out, "%12.12s: %s\n","echo", azBool[p->echoOn!=0]);
6064
+ utf8_printf(p->out, "%12.12s: %s\n","echo",
6065
+ azBool[ShellHasFlag(p, SHFLG_Echo)]);
48856066
utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
48866067
utf8_printf(p->out, "%12.12s: %s\n","explain",
48876068
p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
48886069
utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
48896070
utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
@@ -5444,11 +6625,11 @@
54446625
static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
54456626
int rc;
54466627
char *zErrMsg = 0;
54476628
54486629
open_db(p, 0);
5449
- if( p->backslashOn ) resolve_backslashes(zSql);
6630
+ if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
54506631
BEGIN_TIMER;
54516632
rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
54526633
END_TIMER;
54536634
if( rc || zErrMsg ){
54546635
char zPrefix[100];
@@ -5464,11 +6645,11 @@
54646645
zErrMsg = 0;
54656646
}else{
54666647
utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
54676648
}
54686649
return 1;
5469
- }else if( p->countChanges ){
6650
+ }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
54706651
raw_printf(p->out, "changes: %3d total_changes: %d\n",
54716652
sqlite3_changes(p->db), sqlite3_total_changes(p->db));
54726653
}
54736654
return 0;
54746655
}
@@ -5507,15 +6688,15 @@
55076688
if( in!=0 ) break;
55086689
seenInterrupt = 0;
55096690
}
55106691
lineno++;
55116692
if( nSql==0 && _all_whitespace(zLine) ){
5512
- if( p->echoOn ) printf("%s\n", zLine);
6693
+ if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
55136694
continue;
55146695
}
55156696
if( zLine && zLine[0]=='.' && nSql==0 ){
5516
- if( p->echoOn ) printf("%s\n", zLine);
6697
+ if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
55176698
rc = do_meta_command(zLine, p);
55186699
if( rc==2 ){ /* exit requested */
55196700
break;
55206701
}else if( rc ){
55216702
errCnt++;
@@ -5554,11 +6735,11 @@
55546735
if( p->outCount ){
55556736
output_reset(p);
55566737
p->outCount = 0;
55576738
}
55586739
}else if( nSql && _all_whitespace(zSql) ){
5559
- if( p->echoOn ) printf("%s\n", zSql);
6740
+ if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
55606741
nSql = 0;
55616742
}
55626743
}
55636744
if( nSql && !_all_whitespace(zSql) ){
55646745
runOneSqlLine(p, zSql, in, startline);
@@ -6023,11 +7204,11 @@
60237204
}else if( strcmp(z,"-header")==0 ){
60247205
data.showHeader = 1;
60257206
}else if( strcmp(z,"-noheader")==0 ){
60267207
data.showHeader = 0;
60277208
}else if( strcmp(z,"-echo")==0 ){
6028
- data.echoOn = 1;
7209
+ ShellSetFlag(&data, SHFLG_Echo);
60297210
}else if( strcmp(z,"-eqp")==0 ){
60307211
data.autoEQP = 1;
60317212
}else if( strcmp(z,"-eqpfull")==0 ){
60327213
data.autoEQP = 2;
60337214
}else if( strcmp(z,"-stats")==0 ){
@@ -6038,11 +7219,11 @@
60387219
/* Undocumented command-line option: -backslash
60397220
** Causes C-style backslash escapes to be evaluated in SQL statements
60407221
** prior to sending the SQL into SQLite. Useful for injecting
60417222
** crazy bytes in the middle of SQL statements for testing and debugging.
60427223
*/
6043
- data.backslashOn = 1;
7224
+ ShellSetFlag(&data, SHFLG_Backslash);
60447225
}else if( strcmp(z,"-bail")==0 ){
60457226
bail_on_error = 1;
60467227
}else if( strcmp(z,"-version")==0 ){
60477228
printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
60487229
return 0;
60497230
--- src/shell.c
+++ src/shell.c
@@ -452,32 +452,10 @@
452 while( IsDigit(*z) ){ z++; }
453 if( realnum ) *realnum = 1;
454 }
455 return *z==0;
456 }
457
458 /*
459 ** A global char* and an SQL function to access its current value
460 ** from within an SQL statement. This program used to use the
461 ** sqlite_exec_printf() API to substitue a string into an SQL statement.
462 ** The correct way to do this with sqlite3 is to use the bind API, but
463 ** since the shell is built around the callback paradigm it would be a lot
464 ** of work. Instead just use this hack, which is quite harmless.
465 */
466 static const char *zShellStatic = 0;
467 static void shellstaticFunc(
468 sqlite3_context *context,
469 int argc,
470 sqlite3_value **argv
471 ){
472 assert( 0==argc );
473 assert( zShellStatic );
474 UNUSED_PARAMETER(argc);
475 UNUSED_PARAMETER(argv);
476 sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC);
477 }
478
479
480 /*
481 ** Compute a string length that is limited to what can be stored in
482 ** lower 30 bits of a 32-bit signed integer.
483 */
@@ -575,10 +553,783 @@
575 if( zResult && *zResult ) shell_add_history(zResult);
576 #endif
577 }
578 return zResult;
579 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
580
581 #if defined(SQLITE_ENABLE_SESSION)
582 /*
583 ** State information for a single open session
584 */
@@ -608,17 +1359,14 @@
608 ** instance of the following structure.
609 */
610 typedef struct ShellState ShellState;
611 struct ShellState {
612 sqlite3 *db; /* The database */
613 int echoOn; /* True to echo input commands */
614 int autoExplain; /* Automatically turn on .explain mode */
615 int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
616 int statsOn; /* True to display memory stats before each finalize */
617 int scanstatsOn; /* True to display scan stats before each finalize */
618 int countChanges; /* True to display change counts */
619 int backslashOn; /* Resolve C-style \x escapes in SQL input text */
620 int outCount; /* Revert to stdout when reaching zero */
621 int cnt; /* Number of records displayed so far */
622 FILE *out; /* Write results here */
623 FILE *traceOut; /* Output for sqlite3_trace() */
624 int nErr; /* Number of errors seen */
@@ -653,13 +1401,24 @@
653 };
654
655 /*
656 ** These are the allowed shellFlgs values
657 */
658 #define SHFLG_Scratch 0x00001 /* The --scratch option is used */
659 #define SHFLG_Pagecache 0x00002 /* The --pagecache option is used */
660 #define SHFLG_Lookaside 0x00004 /* Lookaside memory is used */
 
 
 
 
 
 
 
 
 
 
 
661
662 /*
663 ** These are the allowed modes.
664 */
665 #define MODE_Line 0 /* One column per line. Blank line between records */
@@ -1292,92 +2051,106 @@
1292 */
1293 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
1294 /* since we don't have type info, call the shell_callback with a NULL value */
1295 return shell_callback(pArg, nArg, azArg, azCol, NULL);
1296 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1297
1298 /*
1299 ** Set the destination table field of the ShellState structure to
1300 ** the name of the table given. Escape any quote characters in the
1301 ** table name.
1302 */
1303 static void set_table_name(ShellState *p, const char *zName){
1304 int i, n;
1305 int needQuote;
1306 char *z;
1307
1308 if( p->zDestTable ){
1309 free(p->zDestTable);
1310 p->zDestTable = 0;
1311 }
1312 if( zName==0 ) return;
1313 needQuote = !isalpha((unsigned char)*zName) && *zName!='_';
1314 for(i=n=0; zName[i]; i++, n++){
1315 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){
1316 needQuote = 1;
1317 if( zName[i]=='\'' ) n++;
1318 }
1319 }
1320 if( needQuote ) n += 2;
1321 z = p->zDestTable = malloc( n+1 );
1322 if( z==0 ){
1323 raw_printf(stderr,"Error: out of memory\n");
1324 exit(1);
1325 }
1326 n = 0;
1327 if( needQuote ) z[n++] = '\'';
1328 for(i=0; zName[i]; i++){
1329 z[n++] = zName[i];
1330 if( zName[i]=='\'' ) z[n++] = '\'';
1331 }
1332 if( needQuote ) z[n++] = '\'';
1333 z[n] = 0;
1334 }
1335
1336 /* zIn is either a pointer to a NULL-terminated string in memory obtained
1337 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
1338 ** added to zIn, and the result returned in memory obtained from malloc().
1339 ** zIn, if it was not NULL, is freed.
1340 **
1341 ** If the third argument, quote, is not '\0', then it is used as a
1342 ** quote character for zAppend.
1343 */
1344 static char *appendText(char *zIn, char const *zAppend, char quote){
1345 int len;
1346 int i;
1347 int nAppend = strlen30(zAppend);
1348 int nIn = (zIn?strlen30(zIn):0);
1349
1350 len = nAppend+nIn+1;
1351 if( quote ){
1352 len += 2;
1353 for(i=0; i<nAppend; i++){
1354 if( zAppend[i]==quote ) len++;
1355 }
1356 }
1357
1358 zIn = (char *)realloc(zIn, len);
1359 if( !zIn ){
1360 return 0;
1361 }
1362
1363 if( quote ){
1364 char *zCsr = &zIn[nIn];
1365 *zCsr++ = quote;
1366 for(i=0; i<nAppend; i++){
1367 *zCsr++ = zAppend[i];
1368 if( zAppend[i]==quote ) *zCsr++ = quote;
1369 }
1370 *zCsr++ = quote;
1371 *zCsr++ = '\0';
1372 assert( (zCsr-zIn)==len );
1373 }else{
1374 memcpy(&zIn[nIn], zAppend, nAppend);
1375 zIn[len-1] = '\0';
1376 }
1377
1378 return zIn;
1379 }
1380
1381
1382 /*
1383 ** Execute a query statement that will generate SQL output. Print
@@ -1484,10 +2257,35 @@
1484 }
1485 fclose(in);
1486 }
1487 #endif
1488
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1489
1490 /*
1491 ** Display memory stats.
1492 */
1493 static int display_stats(
@@ -1497,61 +2295,35 @@
1497 ){
1498 int iCur;
1499 int iHiwtr;
1500
1501 if( pArg && pArg->out ){
1502
1503 iHiwtr = iCur = -1;
1504 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
1505 raw_printf(pArg->out,
1506 "Memory Used: %d (max %d) bytes\n",
1507 iCur, iHiwtr);
1508 iHiwtr = iCur = -1;
1509 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
1510 raw_printf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n",
1511 iCur, iHiwtr);
1512 if( pArg->shellFlgs & SHFLG_Pagecache ){
1513 iHiwtr = iCur = -1;
1514 sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
1515 raw_printf(pArg->out,
1516 "Number of Pcache Pages Used: %d (max %d) pages\n",
1517 iCur, iHiwtr);
1518 }
1519 iHiwtr = iCur = -1;
1520 sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset);
1521 raw_printf(pArg->out,
1522 "Number of Pcache Overflow Bytes: %d (max %d) bytes\n",
1523 iCur, iHiwtr);
1524 if( pArg->shellFlgs & SHFLG_Scratch ){
1525 iHiwtr = iCur = -1;
1526 sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
1527 raw_printf(pArg->out,
1528 "Number of Scratch Allocations Used: %d (max %d)\n",
1529 iCur, iHiwtr);
1530 }
1531 iHiwtr = iCur = -1;
1532 sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset);
1533 raw_printf(pArg->out,
1534 "Number of Scratch Overflow Bytes: %d (max %d) bytes\n",
1535 iCur, iHiwtr);
1536 iHiwtr = iCur = -1;
1537 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset);
1538 raw_printf(pArg->out, "Largest Allocation: %d bytes\n",
1539 iHiwtr);
1540 iHiwtr = iCur = -1;
1541 sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset);
1542 raw_printf(pArg->out, "Largest Pcache Allocation: %d bytes\n",
1543 iHiwtr);
1544 iHiwtr = iCur = -1;
1545 sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, bReset);
1546 raw_printf(pArg->out, "Largest Scratch Allocation: %d bytes\n",
1547 iHiwtr);
1548 #ifdef YYTRACKMAXSTACKDEPTH
1549 iHiwtr = iCur = -1;
1550 sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset);
1551 raw_printf(pArg->out, "Deepest Parser Stack: %d (max %d)\n",
1552 iCur, iHiwtr);
1553 #endif
1554 }
1555
1556 if( pArg && pArg->out && db ){
1557 if( pArg->shellFlgs & SHFLG_Lookaside ){
@@ -1934,11 +2706,11 @@
1934 pArg->pStmt = pStmt;
1935 pArg->cnt = 0;
1936 }
1937
1938 /* echo the sql statement if echo on */
1939 if( pArg && pArg->echoOn ){
1940 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
1941 }
1942
1943 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
1944 if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){
@@ -2022,10 +2794,142 @@
2022 } /* end while */
2023
2024 return rc;
2025 }
2026
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2027
2028 /*
2029 ** This is a different callback routine used for dumping the database.
2030 ** Each row received by this callback consists of a table name,
2031 ** the table type ("index" or "table") and SQL to create the table.
@@ -2034,21 +2938,20 @@
2034 static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
2035 int rc;
2036 const char *zTable;
2037 const char *zType;
2038 const char *zSql;
2039 const char *zPrepStmt = 0;
2040 ShellState *p = (ShellState *)pArg;
2041
2042 UNUSED_PARAMETER(azCol);
2043 if( nArg!=3 ) return 1;
2044 zTable = azArg[0];
2045 zType = azArg[1];
2046 zSql = azArg[2];
2047
2048 if( strcmp(zTable, "sqlite_sequence")==0 ){
2049 zPrepStmt = "DELETE FROM sqlite_sequence;\n";
2050 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
2051 raw_printf(p->out, "ANALYZE sqlite_master;\n");
2052 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
2053 return 0;
2054 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
@@ -2067,62 +2970,74 @@
2067 }else{
2068 printSchemaLine(p->out, zSql, ";\n");
2069 }
2070
2071 if( strcmp(zType, "table")==0 ){
2072 sqlite3_stmt *pTableInfo = 0;
2073 char *zSelect = 0;
2074 char *zTableInfo = 0;
2075 char *zTmp = 0;
2076 int nRow = 0;
2077
2078 zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);
2079 zTableInfo = appendText(zTableInfo, zTable, '"');
2080 zTableInfo = appendText(zTableInfo, ");", 0);
2081
2082 rc = sqlite3_prepare_v2(p->db, zTableInfo, -1, &pTableInfo, 0);
2083 free(zTableInfo);
2084 if( rc!=SQLITE_OK || !pTableInfo ){
2085 return 1;
2086 }
2087
2088 zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);
2089 /* Always quote the table name, even if it appears to be pure ascii,
2090 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
2091 zTmp = appendText(zTmp, zTable, '"');
2092 if( zTmp ){
2093 zSelect = appendText(zSelect, zTmp, '\'');
2094 free(zTmp);
2095 }
2096 zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
2097 rc = sqlite3_step(pTableInfo);
2098 while( rc==SQLITE_ROW ){
2099 const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1);
2100 zSelect = appendText(zSelect, "quote(", 0);
2101 zSelect = appendText(zSelect, zText, '"');
2102 rc = sqlite3_step(pTableInfo);
2103 if( rc==SQLITE_ROW ){
2104 zSelect = appendText(zSelect, "), ", 0);
2105 }else{
2106 zSelect = appendText(zSelect, ") ", 0);
2107 }
2108 nRow++;
2109 }
2110 rc = sqlite3_finalize(pTableInfo);
2111 if( rc!=SQLITE_OK || nRow==0 ){
2112 free(zSelect);
2113 return 1;
2114 }
2115 zSelect = appendText(zSelect, "|| ')' FROM ", 0);
2116 zSelect = appendText(zSelect, zTable, '"');
2117
2118 rc = run_table_dump_query(p, zSelect, zPrepStmt);
2119 if( rc==SQLITE_CORRUPT ){
2120 zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);
2121 run_table_dump_query(p, zSelect, 0);
2122 }
2123 free(zSelect);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2124 }
2125 return 0;
2126 }
2127
2128 /*
@@ -2233,10 +3148,11 @@
2233 ".separator COL ?ROW? Change the column separator and optionally the row\n"
2234 " separator for both the output mode and .import\n"
2235 #if defined(SQLITE_ENABLE_SESSION)
2236 ".session CMD ... Create or control sessions\n"
2237 #endif
 
2238 ".shell CMD ARGS... Run CMD ARGS... in a system shell\n"
2239 ".show Show the current values for various settings\n"
2240 ".stats ?on|off? Show stats or turn stats on or off\n"
2241 ".system CMD ARGS... Run CMD ARGS... in a system shell\n"
2242 ".tables ?TABLE? List names of tables\n"
@@ -2423,14 +3339,10 @@
2423 static void open_db(ShellState *p, int keepAlive){
2424 if( p->db==0 ){
2425 sqlite3_initialize();
2426 sqlite3_open(p->zDbFilename, &p->db);
2427 globalDb = p->db;
2428 if( p->db && sqlite3_errcode(p->db)==SQLITE_OK ){
2429 sqlite3_create_function(p->db, "shellstatic", 0, SQLITE_UTF8, 0,
2430 shellstaticFunc, 0, 0);
2431 }
2432 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
2433 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
2434 p->zDbFilename, sqlite3_errmsg(p->db));
2435 if( keepAlive ) return;
2436 exit(1);
@@ -2440,10 +3352,18 @@
2440 #endif
2441 sqlite3_create_function(p->db, "readfile", 1, SQLITE_UTF8, 0,
2442 readfileFunc, 0, 0);
2443 sqlite3_create_function(p->db, "writefile", 2, SQLITE_UTF8, 0,
2444 writefileFunc, 0, 0);
 
 
 
 
 
 
 
 
2445 }
2446 }
2447
2448 /*
2449 ** Do C-language style dequoting.
@@ -2564,11 +3484,11 @@
2564
2565 /*
2566 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
2567 ** for TRUE and FALSE. Return the integer value if appropriate.
2568 */
2569 static int booleanValue(char *zArg){
2570 int i;
2571 if( zArg[0]=='0' && zArg[1]=='x' ){
2572 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
2573 }else{
2574 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
@@ -2582,10 +3502,21 @@
2582 }
2583 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
2584 zArg);
2585 return 0;
2586 }
 
 
 
 
 
 
 
 
 
 
 
2587
2588 /*
2589 ** Close an output file, assuming it is not stderr or stdout
2590 */
2591 static void output_file_close(FILE *f){
@@ -3656,11 +4587,11 @@
3656 test_breakpoint();
3657 }else
3658
3659 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
3660 if( nArg==2 ){
3661 p->countChanges = booleanValue(azArg[1]);
3662 }else{
3663 raw_printf(stderr, "Usage: .changes on|off\n");
3664 rc = 1;
3665 }
3666 }else
@@ -3720,25 +4651,46 @@
3720 if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){
3721 rc = shell_dbinfo_command(p, nArg, azArg);
3722 }else
3723
3724 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3725 open_db(p, 0);
3726 /* When playing back a "dump", the content might appear in an order
3727 ** which causes immediate foreign key constraints to be violated.
3728 ** So disable foreign-key constraint enforcement to prevent problems. */
3729 if( nArg!=1 && nArg!=2 ){
3730 raw_printf(stderr, "Usage: .dump ?LIKE-PATTERN?\n");
3731 rc = 1;
3732 goto meta_command_exit;
3733 }
3734 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
3735 raw_printf(p->out, "BEGIN TRANSACTION;\n");
3736 p->writableSchema = 0;
 
 
 
3737 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
3738 p->nErr = 0;
3739 if( nArg==1 ){
3740 run_schema_dump_query(p,
3741 "SELECT name, type, sql FROM sqlite_master "
3742 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
3743 );
3744 run_schema_dump_query(p,
@@ -3748,25 +4700,24 @@
3748 run_table_dump_query(p,
3749 "SELECT sql FROM sqlite_master "
3750 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
3751 );
3752 }else{
3753 int i;
3754 for(i=1; i<nArg; i++){
3755 zShellStatic = azArg[i];
3756 run_schema_dump_query(p,
3757 "SELECT name, type, sql FROM sqlite_master "
3758 "WHERE tbl_name LIKE shellstatic() AND type=='table'"
3759 " AND sql NOT NULL");
3760 run_table_dump_query(p,
3761 "SELECT sql FROM sqlite_master "
3762 "WHERE sql NOT NULL"
3763 " AND type IN ('index','trigger','view')"
3764 " AND tbl_name LIKE shellstatic()", 0
3765 );
3766 zShellStatic = 0;
3767 }
3768 }
3769 if( p->writableSchema ){
3770 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
3771 p->writableSchema = 0;
3772 }
@@ -3775,11 +4726,11 @@
3775 raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
3776 }else
3777
3778 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
3779 if( nArg==2 ){
3780 p->echoOn = booleanValue(azArg[1]);
3781 }else{
3782 raw_printf(stderr, "Usage: .echo on|off\n");
3783 rc = 1;
3784 }
3785 }else
@@ -4572,21 +5523,21 @@
4572 new_colv[0] = "sql";
4573 new_colv[1] = 0;
4574 callback(&data, 1, new_argv, new_colv);
4575 rc = SQLITE_OK;
4576 }else{
4577 zShellStatic = azArg[1];
4578 rc = sqlite3_exec(p->db,
4579 "SELECT sql FROM "
4580 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
4581 " FROM sqlite_master UNION ALL"
4582 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
4583 "WHERE lower(tbl_name) LIKE shellstatic()"
4584 " AND type!='meta' AND sql NOTNULL "
4585 "ORDER BY rowid",
4586 callback, &data, &zErrMsg);
4587 zShellStatic = 0;
4588 }
4589 }else if( nArg==1 ){
4590 rc = sqlite3_exec(p->db,
4591 "SELECT sql FROM "
4592 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
@@ -4835,10 +5786,123 @@
4835 utf8_printf(p->out, "%s", zBuf);
4836 }
4837 }
4838 }else
4839 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4840
4841 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
4842 if( nArg<2 || nArg>3 ){
4843 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
4844 rc = 1;
@@ -4850,10 +5914,126 @@
4850 if( nArg>=3 ){
4851 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
4852 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
4853 }
4854 }else
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4855
4856 if( c=='s'
4857 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
4858 ){
4859 char *zCmd;
@@ -4879,11 +6059,12 @@
4879 if( nArg!=1 ){
4880 raw_printf(stderr, "Usage: .show\n");
4881 rc = 1;
4882 goto meta_command_exit;
4883 }
4884 utf8_printf(p->out, "%12.12s: %s\n","echo", azBool[p->echoOn!=0]);
 
4885 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
4886 utf8_printf(p->out, "%12.12s: %s\n","explain",
4887 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
4888 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
4889 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
@@ -5444,11 +6625,11 @@
5444 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
5445 int rc;
5446 char *zErrMsg = 0;
5447
5448 open_db(p, 0);
5449 if( p->backslashOn ) resolve_backslashes(zSql);
5450 BEGIN_TIMER;
5451 rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
5452 END_TIMER;
5453 if( rc || zErrMsg ){
5454 char zPrefix[100];
@@ -5464,11 +6645,11 @@
5464 zErrMsg = 0;
5465 }else{
5466 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
5467 }
5468 return 1;
5469 }else if( p->countChanges ){
5470 raw_printf(p->out, "changes: %3d total_changes: %d\n",
5471 sqlite3_changes(p->db), sqlite3_total_changes(p->db));
5472 }
5473 return 0;
5474 }
@@ -5507,15 +6688,15 @@
5507 if( in!=0 ) break;
5508 seenInterrupt = 0;
5509 }
5510 lineno++;
5511 if( nSql==0 && _all_whitespace(zLine) ){
5512 if( p->echoOn ) printf("%s\n", zLine);
5513 continue;
5514 }
5515 if( zLine && zLine[0]=='.' && nSql==0 ){
5516 if( p->echoOn ) printf("%s\n", zLine);
5517 rc = do_meta_command(zLine, p);
5518 if( rc==2 ){ /* exit requested */
5519 break;
5520 }else if( rc ){
5521 errCnt++;
@@ -5554,11 +6735,11 @@
5554 if( p->outCount ){
5555 output_reset(p);
5556 p->outCount = 0;
5557 }
5558 }else if( nSql && _all_whitespace(zSql) ){
5559 if( p->echoOn ) printf("%s\n", zSql);
5560 nSql = 0;
5561 }
5562 }
5563 if( nSql && !_all_whitespace(zSql) ){
5564 runOneSqlLine(p, zSql, in, startline);
@@ -6023,11 +7204,11 @@
6023 }else if( strcmp(z,"-header")==0 ){
6024 data.showHeader = 1;
6025 }else if( strcmp(z,"-noheader")==0 ){
6026 data.showHeader = 0;
6027 }else if( strcmp(z,"-echo")==0 ){
6028 data.echoOn = 1;
6029 }else if( strcmp(z,"-eqp")==0 ){
6030 data.autoEQP = 1;
6031 }else if( strcmp(z,"-eqpfull")==0 ){
6032 data.autoEQP = 2;
6033 }else if( strcmp(z,"-stats")==0 ){
@@ -6038,11 +7219,11 @@
6038 /* Undocumented command-line option: -backslash
6039 ** Causes C-style backslash escapes to be evaluated in SQL statements
6040 ** prior to sending the SQL into SQLite. Useful for injecting
6041 ** crazy bytes in the middle of SQL statements for testing and debugging.
6042 */
6043 data.backslashOn = 1;
6044 }else if( strcmp(z,"-bail")==0 ){
6045 bail_on_error = 1;
6046 }else if( strcmp(z,"-version")==0 ){
6047 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
6048 return 0;
6049
--- src/shell.c
+++ src/shell.c
@@ -452,32 +452,10 @@
452 while( IsDigit(*z) ){ z++; }
453 if( realnum ) *realnum = 1;
454 }
455 return *z==0;
456 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
457
458 /*
459 ** Compute a string length that is limited to what can be stored in
460 ** lower 30 bits of a 32-bit signed integer.
461 */
@@ -575,10 +553,783 @@
553 if( zResult && *zResult ) shell_add_history(zResult);
554 #endif
555 }
556 return zResult;
557 }
558 /*
559 ** A variable length string to which one can append text.
560 */
561 typedef struct ShellText ShellText;
562 struct ShellText {
563 char *z;
564 int n;
565 int nAlloc;
566 };
567
568 /*
569 ** Initialize and destroy a ShellText object
570 */
571 static void initText(ShellText *p){
572 memset(p, 0, sizeof(*p));
573 }
574 static void freeText(ShellText *p){
575 free(p->z);
576 initText(p);
577 }
578
579 /* zIn is either a pointer to a NULL-terminated string in memory obtained
580 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
581 ** added to zIn, and the result returned in memory obtained from malloc().
582 ** zIn, if it was not NULL, is freed.
583 **
584 ** If the third argument, quote, is not '\0', then it is used as a
585 ** quote character for zAppend.
586 */
587 static void appendText(ShellText *p, char const *zAppend, char quote){
588 int len;
589 int i;
590 int nAppend = strlen30(zAppend);
591
592 len = nAppend+p->n+1;
593 if( quote ){
594 len += 2;
595 for(i=0; i<nAppend; i++){
596 if( zAppend[i]==quote ) len++;
597 }
598 }
599
600 if( p->n+len>=p->nAlloc ){
601 p->nAlloc = p->nAlloc*2 + len + 20;
602 p->z = realloc(p->z, p->nAlloc);
603 if( p->z==0 ){
604 memset(p, 0, sizeof(*p));
605 return;
606 }
607 }
608
609 if( quote ){
610 char *zCsr = p->z+p->n;
611 *zCsr++ = quote;
612 for(i=0; i<nAppend; i++){
613 *zCsr++ = zAppend[i];
614 if( zAppend[i]==quote ) *zCsr++ = quote;
615 }
616 *zCsr++ = quote;
617 p->n = (int)(zCsr - p->z);
618 *zCsr = '\0';
619 }else{
620 memcpy(p->z+p->n, zAppend, nAppend);
621 p->n += nAppend;
622 p->z[p->n] = '\0';
623 }
624 }
625
626 /*
627 ** Attempt to determine if identifier zName needs to be quoted, either
628 ** because it contains non-alphanumeric characters, or because it is an
629 ** SQLite keyword. Be conservative in this estimate: When in doubt assume
630 ** that quoting is required.
631 **
632 ** Return '"' if quoting is required. Return 0 if no quoting is required.
633 */
634 static char quoteChar(const char *zName){
635 /* All SQLite keywords, in alphabetical order */
636 static const char *azKeywords[] = {
637 "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS",
638 "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY",
639 "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
640 "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE",
641 "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
642 "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH",
643 "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN",
644 "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF",
645 "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER",
646 "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY",
647 "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL",
648 "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA",
649 "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP",
650 "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT",
651 "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
652 "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE",
653 "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE",
654 "WITH", "WITHOUT",
655 };
656 int i, lwr, upr, mid, c;
657 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
658 for(i=0; zName[i]; i++){
659 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
660 }
661 lwr = 0;
662 upr = sizeof(azKeywords)/sizeof(azKeywords[0]) - 1;
663 while( lwr<=upr ){
664 mid = (lwr+upr)/2;
665 c = sqlite3_stricmp(azKeywords[mid], zName);
666 if( c==0 ) return '"';
667 if( c<0 ){
668 lwr = mid+1;
669 }else{
670 upr = mid-1;
671 }
672 }
673 return 0;
674 }
675
676 /******************************************************************************
677 ** SHA3 hash implementation copied from ../ext/misc/shathree.c
678 */
679 typedef sqlite3_uint64 u64;
680 /*
681 ** Macros to determine whether the machine is big or little endian,
682 ** and whether or not that determination is run-time or compile-time.
683 **
684 ** For best performance, an attempt is made to guess at the byte-order
685 ** using C-preprocessor macros. If that is unsuccessful, or if
686 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
687 ** at run-time.
688 */
689 #ifndef SHA3_BYTEORDER
690 # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
691 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
692 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
693 defined(__arm__)
694 # define SHA3_BYTEORDER 1234
695 # elif defined(sparc) || defined(__ppc__)
696 # define SHA3_BYTEORDER 4321
697 # else
698 # define SHA3_BYTEORDER 0
699 # endif
700 #endif
701
702
703 /*
704 ** State structure for a SHA3 hash in progress
705 */
706 typedef struct SHA3Context SHA3Context;
707 struct SHA3Context {
708 union {
709 u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */
710 unsigned char x[1600]; /* ... or 1600 bytes */
711 } u;
712 unsigned nRate; /* Bytes of input accepted per Keccak iteration */
713 unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
714 unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
715 };
716
717 /*
718 ** A single step of the Keccak mixing function for a 1600-bit state
719 */
720 static void KeccakF1600Step(SHA3Context *p){
721 int i;
722 u64 B0, B1, B2, B3, B4;
723 u64 C0, C1, C2, C3, C4;
724 u64 D0, D1, D2, D3, D4;
725 static const u64 RC[] = {
726 0x0000000000000001ULL, 0x0000000000008082ULL,
727 0x800000000000808aULL, 0x8000000080008000ULL,
728 0x000000000000808bULL, 0x0000000080000001ULL,
729 0x8000000080008081ULL, 0x8000000000008009ULL,
730 0x000000000000008aULL, 0x0000000000000088ULL,
731 0x0000000080008009ULL, 0x000000008000000aULL,
732 0x000000008000808bULL, 0x800000000000008bULL,
733 0x8000000000008089ULL, 0x8000000000008003ULL,
734 0x8000000000008002ULL, 0x8000000000000080ULL,
735 0x000000000000800aULL, 0x800000008000000aULL,
736 0x8000000080008081ULL, 0x8000000000008080ULL,
737 0x0000000080000001ULL, 0x8000000080008008ULL
738 };
739 # define A00 (p->u.s[0])
740 # define A01 (p->u.s[1])
741 # define A02 (p->u.s[2])
742 # define A03 (p->u.s[3])
743 # define A04 (p->u.s[4])
744 # define A10 (p->u.s[5])
745 # define A11 (p->u.s[6])
746 # define A12 (p->u.s[7])
747 # define A13 (p->u.s[8])
748 # define A14 (p->u.s[9])
749 # define A20 (p->u.s[10])
750 # define A21 (p->u.s[11])
751 # define A22 (p->u.s[12])
752 # define A23 (p->u.s[13])
753 # define A24 (p->u.s[14])
754 # define A30 (p->u.s[15])
755 # define A31 (p->u.s[16])
756 # define A32 (p->u.s[17])
757 # define A33 (p->u.s[18])
758 # define A34 (p->u.s[19])
759 # define A40 (p->u.s[20])
760 # define A41 (p->u.s[21])
761 # define A42 (p->u.s[22])
762 # define A43 (p->u.s[23])
763 # define A44 (p->u.s[24])
764 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
765
766 for(i=0; i<24; i+=4){
767 C0 = A00^A10^A20^A30^A40;
768 C1 = A01^A11^A21^A31^A41;
769 C2 = A02^A12^A22^A32^A42;
770 C3 = A03^A13^A23^A33^A43;
771 C4 = A04^A14^A24^A34^A44;
772 D0 = C4^ROL64(C1, 1);
773 D1 = C0^ROL64(C2, 1);
774 D2 = C1^ROL64(C3, 1);
775 D3 = C2^ROL64(C4, 1);
776 D4 = C3^ROL64(C0, 1);
777
778 B0 = (A00^D0);
779 B1 = ROL64((A11^D1), 44);
780 B2 = ROL64((A22^D2), 43);
781 B3 = ROL64((A33^D3), 21);
782 B4 = ROL64((A44^D4), 14);
783 A00 = B0 ^((~B1)& B2 );
784 A00 ^= RC[i];
785 A11 = B1 ^((~B2)& B3 );
786 A22 = B2 ^((~B3)& B4 );
787 A33 = B3 ^((~B4)& B0 );
788 A44 = B4 ^((~B0)& B1 );
789
790 B2 = ROL64((A20^D0), 3);
791 B3 = ROL64((A31^D1), 45);
792 B4 = ROL64((A42^D2), 61);
793 B0 = ROL64((A03^D3), 28);
794 B1 = ROL64((A14^D4), 20);
795 A20 = B0 ^((~B1)& B2 );
796 A31 = B1 ^((~B2)& B3 );
797 A42 = B2 ^((~B3)& B4 );
798 A03 = B3 ^((~B4)& B0 );
799 A14 = B4 ^((~B0)& B1 );
800
801 B4 = ROL64((A40^D0), 18);
802 B0 = ROL64((A01^D1), 1);
803 B1 = ROL64((A12^D2), 6);
804 B2 = ROL64((A23^D3), 25);
805 B3 = ROL64((A34^D4), 8);
806 A40 = B0 ^((~B1)& B2 );
807 A01 = B1 ^((~B2)& B3 );
808 A12 = B2 ^((~B3)& B4 );
809 A23 = B3 ^((~B4)& B0 );
810 A34 = B4 ^((~B0)& B1 );
811
812 B1 = ROL64((A10^D0), 36);
813 B2 = ROL64((A21^D1), 10);
814 B3 = ROL64((A32^D2), 15);
815 B4 = ROL64((A43^D3), 56);
816 B0 = ROL64((A04^D4), 27);
817 A10 = B0 ^((~B1)& B2 );
818 A21 = B1 ^((~B2)& B3 );
819 A32 = B2 ^((~B3)& B4 );
820 A43 = B3 ^((~B4)& B0 );
821 A04 = B4 ^((~B0)& B1 );
822
823 B3 = ROL64((A30^D0), 41);
824 B4 = ROL64((A41^D1), 2);
825 B0 = ROL64((A02^D2), 62);
826 B1 = ROL64((A13^D3), 55);
827 B2 = ROL64((A24^D4), 39);
828 A30 = B0 ^((~B1)& B2 );
829 A41 = B1 ^((~B2)& B3 );
830 A02 = B2 ^((~B3)& B4 );
831 A13 = B3 ^((~B4)& B0 );
832 A24 = B4 ^((~B0)& B1 );
833
834 C0 = A00^A20^A40^A10^A30;
835 C1 = A11^A31^A01^A21^A41;
836 C2 = A22^A42^A12^A32^A02;
837 C3 = A33^A03^A23^A43^A13;
838 C4 = A44^A14^A34^A04^A24;
839 D0 = C4^ROL64(C1, 1);
840 D1 = C0^ROL64(C2, 1);
841 D2 = C1^ROL64(C3, 1);
842 D3 = C2^ROL64(C4, 1);
843 D4 = C3^ROL64(C0, 1);
844
845 B0 = (A00^D0);
846 B1 = ROL64((A31^D1), 44);
847 B2 = ROL64((A12^D2), 43);
848 B3 = ROL64((A43^D3), 21);
849 B4 = ROL64((A24^D4), 14);
850 A00 = B0 ^((~B1)& B2 );
851 A00 ^= RC[i+1];
852 A31 = B1 ^((~B2)& B3 );
853 A12 = B2 ^((~B3)& B4 );
854 A43 = B3 ^((~B4)& B0 );
855 A24 = B4 ^((~B0)& B1 );
856
857 B2 = ROL64((A40^D0), 3);
858 B3 = ROL64((A21^D1), 45);
859 B4 = ROL64((A02^D2), 61);
860 B0 = ROL64((A33^D3), 28);
861 B1 = ROL64((A14^D4), 20);
862 A40 = B0 ^((~B1)& B2 );
863 A21 = B1 ^((~B2)& B3 );
864 A02 = B2 ^((~B3)& B4 );
865 A33 = B3 ^((~B4)& B0 );
866 A14 = B4 ^((~B0)& B1 );
867
868 B4 = ROL64((A30^D0), 18);
869 B0 = ROL64((A11^D1), 1);
870 B1 = ROL64((A42^D2), 6);
871 B2 = ROL64((A23^D3), 25);
872 B3 = ROL64((A04^D4), 8);
873 A30 = B0 ^((~B1)& B2 );
874 A11 = B1 ^((~B2)& B3 );
875 A42 = B2 ^((~B3)& B4 );
876 A23 = B3 ^((~B4)& B0 );
877 A04 = B4 ^((~B0)& B1 );
878
879 B1 = ROL64((A20^D0), 36);
880 B2 = ROL64((A01^D1), 10);
881 B3 = ROL64((A32^D2), 15);
882 B4 = ROL64((A13^D3), 56);
883 B0 = ROL64((A44^D4), 27);
884 A20 = B0 ^((~B1)& B2 );
885 A01 = B1 ^((~B2)& B3 );
886 A32 = B2 ^((~B3)& B4 );
887 A13 = B3 ^((~B4)& B0 );
888 A44 = B4 ^((~B0)& B1 );
889
890 B3 = ROL64((A10^D0), 41);
891 B4 = ROL64((A41^D1), 2);
892 B0 = ROL64((A22^D2), 62);
893 B1 = ROL64((A03^D3), 55);
894 B2 = ROL64((A34^D4), 39);
895 A10 = B0 ^((~B1)& B2 );
896 A41 = B1 ^((~B2)& B3 );
897 A22 = B2 ^((~B3)& B4 );
898 A03 = B3 ^((~B4)& B0 );
899 A34 = B4 ^((~B0)& B1 );
900
901 C0 = A00^A40^A30^A20^A10;
902 C1 = A31^A21^A11^A01^A41;
903 C2 = A12^A02^A42^A32^A22;
904 C3 = A43^A33^A23^A13^A03;
905 C4 = A24^A14^A04^A44^A34;
906 D0 = C4^ROL64(C1, 1);
907 D1 = C0^ROL64(C2, 1);
908 D2 = C1^ROL64(C3, 1);
909 D3 = C2^ROL64(C4, 1);
910 D4 = C3^ROL64(C0, 1);
911
912 B0 = (A00^D0);
913 B1 = ROL64((A21^D1), 44);
914 B2 = ROL64((A42^D2), 43);
915 B3 = ROL64((A13^D3), 21);
916 B4 = ROL64((A34^D4), 14);
917 A00 = B0 ^((~B1)& B2 );
918 A00 ^= RC[i+2];
919 A21 = B1 ^((~B2)& B3 );
920 A42 = B2 ^((~B3)& B4 );
921 A13 = B3 ^((~B4)& B0 );
922 A34 = B4 ^((~B0)& B1 );
923
924 B2 = ROL64((A30^D0), 3);
925 B3 = ROL64((A01^D1), 45);
926 B4 = ROL64((A22^D2), 61);
927 B0 = ROL64((A43^D3), 28);
928 B1 = ROL64((A14^D4), 20);
929 A30 = B0 ^((~B1)& B2 );
930 A01 = B1 ^((~B2)& B3 );
931 A22 = B2 ^((~B3)& B4 );
932 A43 = B3 ^((~B4)& B0 );
933 A14 = B4 ^((~B0)& B1 );
934
935 B4 = ROL64((A10^D0), 18);
936 B0 = ROL64((A31^D1), 1);
937 B1 = ROL64((A02^D2), 6);
938 B2 = ROL64((A23^D3), 25);
939 B3 = ROL64((A44^D4), 8);
940 A10 = B0 ^((~B1)& B2 );
941 A31 = B1 ^((~B2)& B3 );
942 A02 = B2 ^((~B3)& B4 );
943 A23 = B3 ^((~B4)& B0 );
944 A44 = B4 ^((~B0)& B1 );
945
946 B1 = ROL64((A40^D0), 36);
947 B2 = ROL64((A11^D1), 10);
948 B3 = ROL64((A32^D2), 15);
949 B4 = ROL64((A03^D3), 56);
950 B0 = ROL64((A24^D4), 27);
951 A40 = B0 ^((~B1)& B2 );
952 A11 = B1 ^((~B2)& B3 );
953 A32 = B2 ^((~B3)& B4 );
954 A03 = B3 ^((~B4)& B0 );
955 A24 = B4 ^((~B0)& B1 );
956
957 B3 = ROL64((A20^D0), 41);
958 B4 = ROL64((A41^D1), 2);
959 B0 = ROL64((A12^D2), 62);
960 B1 = ROL64((A33^D3), 55);
961 B2 = ROL64((A04^D4), 39);
962 A20 = B0 ^((~B1)& B2 );
963 A41 = B1 ^((~B2)& B3 );
964 A12 = B2 ^((~B3)& B4 );
965 A33 = B3 ^((~B4)& B0 );
966 A04 = B4 ^((~B0)& B1 );
967
968 C0 = A00^A30^A10^A40^A20;
969 C1 = A21^A01^A31^A11^A41;
970 C2 = A42^A22^A02^A32^A12;
971 C3 = A13^A43^A23^A03^A33;
972 C4 = A34^A14^A44^A24^A04;
973 D0 = C4^ROL64(C1, 1);
974 D1 = C0^ROL64(C2, 1);
975 D2 = C1^ROL64(C3, 1);
976 D3 = C2^ROL64(C4, 1);
977 D4 = C3^ROL64(C0, 1);
978
979 B0 = (A00^D0);
980 B1 = ROL64((A01^D1), 44);
981 B2 = ROL64((A02^D2), 43);
982 B3 = ROL64((A03^D3), 21);
983 B4 = ROL64((A04^D4), 14);
984 A00 = B0 ^((~B1)& B2 );
985 A00 ^= RC[i+3];
986 A01 = B1 ^((~B2)& B3 );
987 A02 = B2 ^((~B3)& B4 );
988 A03 = B3 ^((~B4)& B0 );
989 A04 = B4 ^((~B0)& B1 );
990
991 B2 = ROL64((A10^D0), 3);
992 B3 = ROL64((A11^D1), 45);
993 B4 = ROL64((A12^D2), 61);
994 B0 = ROL64((A13^D3), 28);
995 B1 = ROL64((A14^D4), 20);
996 A10 = B0 ^((~B1)& B2 );
997 A11 = B1 ^((~B2)& B3 );
998 A12 = B2 ^((~B3)& B4 );
999 A13 = B3 ^((~B4)& B0 );
1000 A14 = B4 ^((~B0)& B1 );
1001
1002 B4 = ROL64((A20^D0), 18);
1003 B0 = ROL64((A21^D1), 1);
1004 B1 = ROL64((A22^D2), 6);
1005 B2 = ROL64((A23^D3), 25);
1006 B3 = ROL64((A24^D4), 8);
1007 A20 = B0 ^((~B1)& B2 );
1008 A21 = B1 ^((~B2)& B3 );
1009 A22 = B2 ^((~B3)& B4 );
1010 A23 = B3 ^((~B4)& B0 );
1011 A24 = B4 ^((~B0)& B1 );
1012
1013 B1 = ROL64((A30^D0), 36);
1014 B2 = ROL64((A31^D1), 10);
1015 B3 = ROL64((A32^D2), 15);
1016 B4 = ROL64((A33^D3), 56);
1017 B0 = ROL64((A34^D4), 27);
1018 A30 = B0 ^((~B1)& B2 );
1019 A31 = B1 ^((~B2)& B3 );
1020 A32 = B2 ^((~B3)& B4 );
1021 A33 = B3 ^((~B4)& B0 );
1022 A34 = B4 ^((~B0)& B1 );
1023
1024 B3 = ROL64((A40^D0), 41);
1025 B4 = ROL64((A41^D1), 2);
1026 B0 = ROL64((A42^D2), 62);
1027 B1 = ROL64((A43^D3), 55);
1028 B2 = ROL64((A44^D4), 39);
1029 A40 = B0 ^((~B1)& B2 );
1030 A41 = B1 ^((~B2)& B3 );
1031 A42 = B2 ^((~B3)& B4 );
1032 A43 = B3 ^((~B4)& B0 );
1033 A44 = B4 ^((~B0)& B1 );
1034 }
1035 }
1036
1037 /*
1038 ** Initialize a new hash. iSize determines the size of the hash
1039 ** in bits and should be one of 224, 256, 384, or 512. Or iSize
1040 ** can be zero to use the default hash size of 256 bits.
1041 */
1042 static void SHA3Init(SHA3Context *p, int iSize){
1043 memset(p, 0, sizeof(*p));
1044 if( iSize>=128 && iSize<=512 ){
1045 p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1046 }else{
1047 p->nRate = (1600 - 2*256)/8;
1048 }
1049 #if SHA3_BYTEORDER==1234
1050 /* Known to be little-endian at compile-time. No-op */
1051 #elif SHA3_BYTEORDER==4321
1052 p->ixMask = 7; /* Big-endian */
1053 #else
1054 {
1055 static unsigned int one = 1;
1056 if( 1==*(unsigned char*)&one ){
1057 /* Little endian. No byte swapping. */
1058 p->ixMask = 0;
1059 }else{
1060 /* Big endian. Byte swap. */
1061 p->ixMask = 7;
1062 }
1063 }
1064 #endif
1065 }
1066
1067 /*
1068 ** Make consecutive calls to the SHA3Update function to add new content
1069 ** to the hash
1070 */
1071 static void SHA3Update(
1072 SHA3Context *p,
1073 const unsigned char *aData,
1074 unsigned int nData
1075 ){
1076 unsigned int i = 0;
1077 #if SHA3_BYTEORDER==1234
1078 if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1079 for(; i+7<nData; i+=8){
1080 p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1081 p->nLoaded += 8;
1082 if( p->nLoaded>=p->nRate ){
1083 KeccakF1600Step(p);
1084 p->nLoaded = 0;
1085 }
1086 }
1087 }
1088 #endif
1089 for(; i<nData; i++){
1090 #if SHA3_BYTEORDER==1234
1091 p->u.x[p->nLoaded] ^= aData[i];
1092 #elif SHA3_BYTEORDER==4321
1093 p->u.x[p->nLoaded^0x07] ^= aData[i];
1094 #else
1095 p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1096 #endif
1097 p->nLoaded++;
1098 if( p->nLoaded==p->nRate ){
1099 KeccakF1600Step(p);
1100 p->nLoaded = 0;
1101 }
1102 }
1103 }
1104
1105 /*
1106 ** After all content has been added, invoke SHA3Final() to compute
1107 ** the final hash. The function returns a pointer to the binary
1108 ** hash value.
1109 */
1110 static unsigned char *SHA3Final(SHA3Context *p){
1111 unsigned int i;
1112 if( p->nLoaded==p->nRate-1 ){
1113 const unsigned char c1 = 0x86;
1114 SHA3Update(p, &c1, 1);
1115 }else{
1116 const unsigned char c2 = 0x06;
1117 const unsigned char c3 = 0x80;
1118 SHA3Update(p, &c2, 1);
1119 p->nLoaded = p->nRate - 1;
1120 SHA3Update(p, &c3, 1);
1121 }
1122 for(i=0; i<p->nRate; i++){
1123 p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1124 }
1125 return &p->u.x[p->nRate];
1126 }
1127
1128 /*
1129 ** Implementation of the sha3(X,SIZE) function.
1130 **
1131 ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
1132 ** size is 256. If X is a BLOB, it is hashed as is.
1133 ** For all other non-NULL types of input, X is converted into a UTF-8 string
1134 ** and the string is hashed without the trailing 0x00 terminator. The hash
1135 ** of a NULL value is NULL.
1136 */
1137 static void sha3Func(
1138 sqlite3_context *context,
1139 int argc,
1140 sqlite3_value **argv
1141 ){
1142 SHA3Context cx;
1143 int eType = sqlite3_value_type(argv[0]);
1144 int nByte = sqlite3_value_bytes(argv[0]);
1145 int iSize;
1146 if( argc==1 ){
1147 iSize = 256;
1148 }else{
1149 iSize = sqlite3_value_int(argv[1]);
1150 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1151 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1152 "384 512", -1);
1153 return;
1154 }
1155 }
1156 if( eType==SQLITE_NULL ) return;
1157 SHA3Init(&cx, iSize);
1158 if( eType==SQLITE_BLOB ){
1159 SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1160 }else{
1161 SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1162 }
1163 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1164 }
1165
1166 /* Compute a string using sqlite3_vsnprintf() with a maximum length
1167 ** of 50 bytes and add it to the hash.
1168 */
1169 static void hash_step_vformat(
1170 SHA3Context *p, /* Add content to this context */
1171 const char *zFormat,
1172 ...
1173 ){
1174 va_list ap;
1175 int n;
1176 char zBuf[50];
1177 va_start(ap, zFormat);
1178 sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1179 va_end(ap);
1180 n = (int)strlen(zBuf);
1181 SHA3Update(p, (unsigned char*)zBuf, n);
1182 }
1183
1184 /*
1185 ** Implementation of the sha3_query(SQL,SIZE) function.
1186 **
1187 ** This function compiles and runs the SQL statement(s) given in the
1188 ** argument. The results are hashed using a SIZE-bit SHA3. The default
1189 ** size is 256.
1190 **
1191 ** The format of the byte stream that is hashed is summarized as follows:
1192 **
1193 ** S<n>:<sql>
1194 ** R
1195 ** N
1196 ** I<int>
1197 ** F<ieee-float>
1198 ** B<size>:<bytes>
1199 ** T<size>:<text>
1200 **
1201 ** <sql> is the original SQL text for each statement run and <n> is
1202 ** the size of that text. The SQL text is UTF-8. A single R character
1203 ** occurs before the start of each row. N means a NULL value.
1204 ** I mean an 8-byte little-endian integer <int>. F is a floating point
1205 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1206 ** B means blobs of <size> bytes. T means text rendered as <size>
1207 ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII
1208 ** text integers.
1209 **
1210 ** For each SQL statement in the X input, there is one S segment. Each
1211 ** S segment is followed by zero or more R segments, one for each row in the
1212 ** result set. After each R, there are one or more N, I, F, B, or T segments,
1213 ** one for each column in the result set. Segments are concatentated directly
1214 ** with no delimiters of any kind.
1215 */
1216 static void sha3QueryFunc(
1217 sqlite3_context *context,
1218 int argc,
1219 sqlite3_value **argv
1220 ){
1221 sqlite3 *db = sqlite3_context_db_handle(context);
1222 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1223 sqlite3_stmt *pStmt = 0;
1224 int nCol; /* Number of columns in the result set */
1225 int i; /* Loop counter */
1226 int rc;
1227 int n;
1228 const char *z;
1229 SHA3Context cx;
1230 int iSize;
1231
1232 if( argc==1 ){
1233 iSize = 256;
1234 }else{
1235 iSize = sqlite3_value_int(argv[1]);
1236 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1237 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1238 "384 512", -1);
1239 return;
1240 }
1241 }
1242 if( zSql==0 ) return;
1243 SHA3Init(&cx, iSize);
1244 while( zSql[0] ){
1245 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
1246 if( rc ){
1247 char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
1248 zSql, sqlite3_errmsg(db));
1249 sqlite3_finalize(pStmt);
1250 sqlite3_result_error(context, zMsg, -1);
1251 sqlite3_free(zMsg);
1252 return;
1253 }
1254 if( !sqlite3_stmt_readonly(pStmt) ){
1255 char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
1256 sqlite3_finalize(pStmt);
1257 sqlite3_result_error(context, zMsg, -1);
1258 sqlite3_free(zMsg);
1259 return;
1260 }
1261 nCol = sqlite3_column_count(pStmt);
1262 z = sqlite3_sql(pStmt);
1263 if( z==0 ){
1264 sqlite3_finalize(pStmt);
1265 continue;
1266 }
1267 n = (int)strlen(z);
1268 hash_step_vformat(&cx,"S%d:",n);
1269 SHA3Update(&cx,(unsigned char*)z,n);
1270
1271 /* Compute a hash over the result of the query */
1272 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1273 SHA3Update(&cx,(const unsigned char*)"R",1);
1274 for(i=0; i<nCol; i++){
1275 switch( sqlite3_column_type(pStmt,i) ){
1276 case SQLITE_NULL: {
1277 SHA3Update(&cx, (const unsigned char*)"N",1);
1278 break;
1279 }
1280 case SQLITE_INTEGER: {
1281 sqlite3_uint64 u;
1282 int j;
1283 unsigned char x[9];
1284 sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
1285 memcpy(&u, &v, 8);
1286 for(j=8; j>=1; j--){
1287 x[j] = u & 0xff;
1288 u >>= 8;
1289 }
1290 x[0] = 'I';
1291 SHA3Update(&cx, x, 9);
1292 break;
1293 }
1294 case SQLITE_FLOAT: {
1295 sqlite3_uint64 u;
1296 int j;
1297 unsigned char x[9];
1298 double r = sqlite3_column_double(pStmt,i);
1299 memcpy(&u, &r, 8);
1300 for(j=8; j>=1; j--){
1301 x[j] = u & 0xff;
1302 u >>= 8;
1303 }
1304 x[0] = 'F';
1305 SHA3Update(&cx,x,9);
1306 break;
1307 }
1308 case SQLITE_TEXT: {
1309 int n2 = sqlite3_column_bytes(pStmt, i);
1310 const unsigned char *z2 = sqlite3_column_text(pStmt, i);
1311 hash_step_vformat(&cx,"T%d:",n2);
1312 SHA3Update(&cx, z2, n2);
1313 break;
1314 }
1315 case SQLITE_BLOB: {
1316 int n2 = sqlite3_column_bytes(pStmt, i);
1317 const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
1318 hash_step_vformat(&cx,"B%d:",n2);
1319 SHA3Update(&cx, z2, n2);
1320 break;
1321 }
1322 }
1323 }
1324 }
1325 sqlite3_finalize(pStmt);
1326 }
1327 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1328 }
1329 /* End of SHA3 hashing logic copy/pasted from ../ext/misc/shathree.c
1330 ********************************************************************************/
1331
1332 #if defined(SQLITE_ENABLE_SESSION)
1333 /*
1334 ** State information for a single open session
1335 */
@@ -608,17 +1359,14 @@
1359 ** instance of the following structure.
1360 */
1361 typedef struct ShellState ShellState;
1362 struct ShellState {
1363 sqlite3 *db; /* The database */
 
1364 int autoExplain; /* Automatically turn on .explain mode */
1365 int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1366 int statsOn; /* True to display memory stats before each finalize */
1367 int scanstatsOn; /* True to display scan stats before each finalize */
 
 
1368 int outCount; /* Revert to stdout when reaching zero */
1369 int cnt; /* Number of records displayed so far */
1370 FILE *out; /* Write results here */
1371 FILE *traceOut; /* Output for sqlite3_trace() */
1372 int nErr; /* Number of errors seen */
@@ -653,13 +1401,24 @@
1401 };
1402
1403 /*
1404 ** These are the allowed shellFlgs values
1405 */
1406 #define SHFLG_Scratch 0x00000001 /* The --scratch option is used */
1407 #define SHFLG_Pagecache 0x00000002 /* The --pagecache option is used */
1408 #define SHFLG_Lookaside 0x00000004 /* Lookaside memory is used */
1409 #define SHFLG_Backslash 0x00000008 /* The --backslash option is used */
1410 #define SHFLG_PreserveRowid 0x00000010 /* .dump preserves rowid values */
1411 #define SHFLG_CountChanges 0x00000020 /* .changes setting */
1412 #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */
1413
1414 /*
1415 ** Macros for testing and setting shellFlgs
1416 */
1417 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
1418 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
1419 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
1420
1421 /*
1422 ** These are the allowed modes.
1423 */
1424 #define MODE_Line 0 /* One column per line. Blank line between records */
@@ -1292,92 +2051,106 @@
2051 */
2052 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2053 /* since we don't have type info, call the shell_callback with a NULL value */
2054 return shell_callback(pArg, nArg, azArg, azCol, NULL);
2055 }
2056
2057 /*
2058 ** This is the callback routine from sqlite3_exec() that appends all
2059 ** output onto the end of a ShellText object.
2060 */
2061 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2062 ShellText *p = (ShellText*)pArg;
2063 int i;
2064 if( p->n ) appendText(p, "|", 0);
2065 for(i=0; i<nArg; i++){
2066 if( i ) appendText(p, ",", 0);
2067 if( azArg[i] ) appendText(p, azArg[i], 0);
2068 }
2069 return 0;
2070 }
2071
2072 /*
2073 ** Generate an appropriate SELFTEST table in the main database.
2074 */
2075 static void createSelftestTable(ShellState *p){
2076 char *zErrMsg = 0;
2077 sqlite3_exec(p->db,
2078 "SAVEPOINT selftest_init;\n"
2079 "CREATE TABLE IF NOT EXISTS selftest(\n"
2080 " tno INTEGER PRIMARY KEY,\n" /* Test number */
2081 " op TEXT,\n" /* Operator: memo run */
2082 " cmd TEXT,\n" /* Command text */
2083 " ans TEXT\n" /* Desired answer */
2084 ");"
2085 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2086 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2087 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2088 " 'memo','Tests generated by --init');\n"
2089 "INSERT INTO [_shell$self]\n"
2090 " SELECT 'run',\n"
2091 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2092 "FROM sqlite_master ORDER BY 2'',224))',\n"
2093 " hex(sha3_query('SELECT type,name,tbl_name,sql "
2094 "FROM sqlite_master ORDER BY 2',224));\n"
2095 "INSERT INTO [_shell$self]\n"
2096 " SELECT 'run',"
2097 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2098 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2099 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2100 " FROM (\n"
2101 " SELECT name FROM sqlite_master\n"
2102 " WHERE type='table'\n"
2103 " AND name<>'selftest'\n"
2104 " AND coalesce(rootpage,0)>0\n"
2105 " )\n"
2106 " ORDER BY name;\n"
2107 "INSERT INTO [_shell$self]\n"
2108 " VALUES('run','PRAGMA integrity_check','ok');\n"
2109 "INSERT INTO selftest(tno,op,cmd,ans)"
2110 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2111 "DROP TABLE [_shell$self];"
2112 ,0,0,&zErrMsg);
2113 if( zErrMsg ){
2114 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2115 sqlite3_free(zErrMsg);
2116 }
2117 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2118 }
2119
2120
2121 /*
2122 ** Set the destination table field of the ShellState structure to
2123 ** the name of the table given. Escape any quote characters in the
2124 ** table name.
2125 */
2126 static void set_table_name(ShellState *p, const char *zName){
2127 int i, n;
2128 int cQuote;
2129 char *z;
2130
2131 if( p->zDestTable ){
2132 free(p->zDestTable);
2133 p->zDestTable = 0;
2134 }
2135 if( zName==0 ) return;
2136 cQuote = quoteChar(zName);
2137 n = strlen30(zName);
2138 if( cQuote ) n += 2;
 
 
 
 
 
2139 z = p->zDestTable = malloc( n+1 );
2140 if( z==0 ){
2141 raw_printf(stderr,"Error: out of memory\n");
2142 exit(1);
2143 }
2144 n = 0;
2145 if( cQuote ) z[n++] = cQuote;
2146 for(i=0; zName[i]; i++){
2147 z[n++] = zName[i];
2148 if( zName[i]==cQuote ) z[n++] = cQuote;
2149 }
2150 if( cQuote ) z[n++] = cQuote;
2151 z[n] = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2152 }
2153
2154
2155 /*
2156 ** Execute a query statement that will generate SQL output. Print
@@ -1484,10 +2257,35 @@
2257 }
2258 fclose(in);
2259 }
2260 #endif
2261
2262 /*
2263 ** Display a single line of status using 64-bit values.
2264 */
2265 static void displayStatLine(
2266 ShellState *p, /* The shell context */
2267 char *zLabel, /* Label for this one line */
2268 char *zFormat, /* Format for the result */
2269 int iStatusCtrl, /* Which status to display */
2270 int bReset /* True to reset the stats */
2271 ){
2272 sqlite3_int64 iCur = -1;
2273 sqlite3_int64 iHiwtr = -1;
2274 int i, nPercent;
2275 char zLine[200];
2276 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2277 for(i=0, nPercent=0; zFormat[i]; i++){
2278 if( zFormat[i]=='%' ) nPercent++;
2279 }
2280 if( nPercent>1 ){
2281 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2282 }else{
2283 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2284 }
2285 raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2286 }
2287
2288 /*
2289 ** Display memory stats.
2290 */
2291 static int display_stats(
@@ -1497,61 +2295,35 @@
2295 ){
2296 int iCur;
2297 int iHiwtr;
2298
2299 if( pArg && pArg->out ){
2300 displayStatLine(pArg, "Memory Used:",
2301 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2302 displayStatLine(pArg, "Number of Outstanding Allocations:",
2303 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
 
 
 
 
 
 
2304 if( pArg->shellFlgs & SHFLG_Pagecache ){
2305 displayStatLine(pArg, "Number of Pcache Pages Used:",
2306 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2307 }
2308 displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2309 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
 
 
 
 
 
 
2310 if( pArg->shellFlgs & SHFLG_Scratch ){
2311 displayStatLine(pArg, "Number of Scratch Allocations Used:",
2312 "%lld (max %lld)", SQLITE_STATUS_SCRATCH_USED, bReset);
2313 }
2314 displayStatLine(pArg, "Number of Scratch Overflow Bytes:",
2315 "%lld (max %lld) bytes", SQLITE_STATUS_SCRATCH_OVERFLOW, bReset);
2316 displayStatLine(pArg, "Largest Allocation:",
2317 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2318 displayStatLine(pArg, "Largest Pcache Allocation:",
2319 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2320 displayStatLine(pArg, "Largest Scratch Allocation:",
2321 "%lld bytes", SQLITE_STATUS_SCRATCH_SIZE, bReset);
 
 
 
 
 
 
 
 
 
 
 
 
2322 #ifdef YYTRACKMAXSTACKDEPTH
2323 displayStatLine(pArg, "Deepest Parser Stack:",
2324 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
 
 
2325 #endif
2326 }
2327
2328 if( pArg && pArg->out && db ){
2329 if( pArg->shellFlgs & SHFLG_Lookaside ){
@@ -1934,11 +2706,11 @@
2706 pArg->pStmt = pStmt;
2707 pArg->cnt = 0;
2708 }
2709
2710 /* echo the sql statement if echo on */
2711 if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
2712 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
2713 }
2714
2715 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
2716 if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){
@@ -2022,10 +2794,142 @@
2794 } /* end while */
2795
2796 return rc;
2797 }
2798
2799 /*
2800 ** Release memory previously allocated by tableColumnList().
2801 */
2802 static void freeColumnList(char **azCol){
2803 int i;
2804 for(i=1; azCol[i]; i++){
2805 sqlite3_free(azCol[i]);
2806 }
2807 /* azCol[0] is a static string */
2808 sqlite3_free(azCol);
2809 }
2810
2811 /*
2812 ** Return a list of pointers to strings which are the names of all
2813 ** columns in table zTab. The memory to hold the names is dynamically
2814 ** allocated and must be released by the caller using a subsequent call
2815 ** to freeColumnList().
2816 **
2817 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
2818 ** value that needs to be preserved, then azCol[0] is filled in with the
2819 ** name of the rowid column.
2820 **
2821 ** The first regular column in the table is azCol[1]. The list is terminated
2822 ** by an entry with azCol[i]==0.
2823 */
2824 static char **tableColumnList(ShellState *p, const char *zTab){
2825 char **azCol = 0;
2826 sqlite3_stmt *pStmt;
2827 char *zSql;
2828 int nCol = 0;
2829 int nAlloc = 0;
2830 int nPK = 0; /* Number of PRIMARY KEY columns seen */
2831 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
2832 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
2833 int rc;
2834
2835 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
2836 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2837 sqlite3_free(zSql);
2838 if( rc ) return 0;
2839 while( sqlite3_step(pStmt)==SQLITE_ROW ){
2840 if( nCol>=nAlloc-2 ){
2841 nAlloc = nAlloc*2 + nCol + 10;
2842 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
2843 if( azCol==0 ){
2844 raw_printf(stderr, "Error: out of memory\n");
2845 exit(1);
2846 }
2847 }
2848 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
2849 if( sqlite3_column_int(pStmt, 5) ){
2850 nPK++;
2851 if( nPK==1
2852 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
2853 "INTEGER")==0
2854 ){
2855 isIPK = 1;
2856 }else{
2857 isIPK = 0;
2858 }
2859 }
2860 }
2861 sqlite3_finalize(pStmt);
2862 azCol[0] = 0;
2863 azCol[nCol+1] = 0;
2864
2865 /* The decision of whether or not a rowid really needs to be preserved
2866 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
2867 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
2868 ** rowids on tables where the rowid is inaccessible because there are other
2869 ** columns in the table named "rowid", "_rowid_", and "oid".
2870 */
2871 if( preserveRowid && isIPK ){
2872 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
2873 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
2874 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
2875 ** ROWID aliases. To distinguish these cases, check to see if
2876 ** there is a "pk" entry in "PRAGMA index_list". There will be
2877 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
2878 */
2879 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
2880 " WHERE origin='pk'", zTab);
2881 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2882 sqlite3_free(zSql);
2883 if( rc ){
2884 freeColumnList(azCol);
2885 return 0;
2886 }
2887 rc = sqlite3_step(pStmt);
2888 sqlite3_finalize(pStmt);
2889 preserveRowid = rc==SQLITE_ROW;
2890 }
2891 if( preserveRowid ){
2892 /* Only preserve the rowid if we can find a name to use for the
2893 ** rowid */
2894 static char *azRowid[] = { "rowid", "_rowid_", "oid" };
2895 int i, j;
2896 for(j=0; j<3; j++){
2897 for(i=1; i<=nCol; i++){
2898 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
2899 }
2900 if( i>nCol ){
2901 /* At this point, we know that azRowid[j] is not the name of any
2902 ** ordinary column in the table. Verify that azRowid[j] is a valid
2903 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
2904 ** tables will fail this last check */
2905 int rc;
2906 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
2907 if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
2908 break;
2909 }
2910 }
2911 }
2912 return azCol;
2913 }
2914
2915 /*
2916 ** Toggle the reverse_unordered_selects setting.
2917 */
2918 static void toggleSelectOrder(sqlite3 *db){
2919 sqlite3_stmt *pStmt = 0;
2920 int iSetting = 0;
2921 char zStmt[100];
2922 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
2923 if( sqlite3_step(pStmt)==SQLITE_ROW ){
2924 iSetting = sqlite3_column_int(pStmt, 0);
2925 }
2926 sqlite3_finalize(pStmt);
2927 sqlite3_snprintf(sizeof(zStmt), zStmt,
2928 "PRAGMA reverse_unordered_selects(%d)", !iSetting);
2929 sqlite3_exec(db, zStmt, 0, 0, 0);
2930 }
2931
2932 /*
2933 ** This is a different callback routine used for dumping the database.
2934 ** Each row received by this callback consists of a table name,
2935 ** the table type ("index" or "table") and SQL to create the table.
@@ -2034,21 +2938,20 @@
2938 static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
2939 int rc;
2940 const char *zTable;
2941 const char *zType;
2942 const char *zSql;
 
2943 ShellState *p = (ShellState *)pArg;
2944
2945 UNUSED_PARAMETER(azCol);
2946 if( nArg!=3 ) return 1;
2947 zTable = azArg[0];
2948 zType = azArg[1];
2949 zSql = azArg[2];
2950
2951 if( strcmp(zTable, "sqlite_sequence")==0 ){
2952 raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
2953 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
2954 raw_printf(p->out, "ANALYZE sqlite_master;\n");
2955 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
2956 return 0;
2957 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
@@ -2067,62 +2970,74 @@
2970 }else{
2971 printSchemaLine(p->out, zSql, ";\n");
2972 }
2973
2974 if( strcmp(zType, "table")==0 ){
2975 ShellText sSelect;
2976 ShellText sTable;
2977 char **azCol;
2978 int i;
2979 char *savedDestTable;
2980 int savedMode;
2981
2982 azCol = tableColumnList(p, zTable);
2983 if( azCol==0 ){
2984 p->nErr++;
2985 return 0;
2986 }
2987
 
 
 
 
2988 /* Always quote the table name, even if it appears to be pure ascii,
2989 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
2990 initText(&sTable);
2991 appendText(&sTable, zTable, quoteChar(zTable));
2992 /* If preserving the rowid, add a column list after the table name.
2993 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
2994 ** instead of the usual "INSERT INTO tab VALUES(...)".
2995 */
2996 if( azCol[0] ){
2997 appendText(&sTable, "(", 0);
2998 appendText(&sTable, azCol[0], 0);
2999 for(i=1; azCol[i]; i++){
3000 appendText(&sTable, ",", 0);
3001 appendText(&sTable, azCol[i], quoteChar(azCol[i]));
3002 }
3003 appendText(&sTable, ")", 0);
3004 }
3005
3006 /* Build an appropriate SELECT statement */
3007 initText(&sSelect);
3008 appendText(&sSelect, "SELECT ", 0);
3009 if( azCol[0] ){
3010 appendText(&sSelect, azCol[0], 0);
3011 appendText(&sSelect, ",", 0);
3012 }
3013 for(i=1; azCol[i]; i++){
3014 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
3015 if( azCol[i+1] ){
3016 appendText(&sSelect, ",", 0);
3017 }
3018 }
3019 freeColumnList(azCol);
3020 appendText(&sSelect, " FROM ", 0);
3021 appendText(&sSelect, zTable, quoteChar(zTable));
3022
3023 savedDestTable = p->zDestTable;
3024 savedMode = p->mode;
3025 p->zDestTable = sTable.z;
3026 p->mode = p->cMode = MODE_Insert;
3027 rc = shell_exec(p->db, sSelect.z, shell_callback, p, 0);
3028 if( (rc&0xff)==SQLITE_CORRUPT ){
3029 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3030 toggleSelectOrder(p->db);
3031 shell_exec(p->db, sSelect.z, shell_callback, p, 0);
3032 toggleSelectOrder(p->db);
3033 }
3034 p->zDestTable = savedDestTable;
3035 p->mode = savedMode;
3036 freeText(&sTable);
3037 freeText(&sSelect);
3038 if( rc ) p->nErr++;
3039 }
3040 return 0;
3041 }
3042
3043 /*
@@ -2233,10 +3148,11 @@
3148 ".separator COL ?ROW? Change the column separator and optionally the row\n"
3149 " separator for both the output mode and .import\n"
3150 #if defined(SQLITE_ENABLE_SESSION)
3151 ".session CMD ... Create or control sessions\n"
3152 #endif
3153 ".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n"
3154 ".shell CMD ARGS... Run CMD ARGS... in a system shell\n"
3155 ".show Show the current values for various settings\n"
3156 ".stats ?on|off? Show stats or turn stats on or off\n"
3157 ".system CMD ARGS... Run CMD ARGS... in a system shell\n"
3158 ".tables ?TABLE? List names of tables\n"
@@ -2423,14 +3339,10 @@
3339 static void open_db(ShellState *p, int keepAlive){
3340 if( p->db==0 ){
3341 sqlite3_initialize();
3342 sqlite3_open(p->zDbFilename, &p->db);
3343 globalDb = p->db;
 
 
 
 
3344 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
3345 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
3346 p->zDbFilename, sqlite3_errmsg(p->db));
3347 if( keepAlive ) return;
3348 exit(1);
@@ -2440,10 +3352,18 @@
3352 #endif
3353 sqlite3_create_function(p->db, "readfile", 1, SQLITE_UTF8, 0,
3354 readfileFunc, 0, 0);
3355 sqlite3_create_function(p->db, "writefile", 2, SQLITE_UTF8, 0,
3356 writefileFunc, 0, 0);
3357 sqlite3_create_function(p->db, "sha3", 1, SQLITE_UTF8, 0,
3358 sha3Func, 0, 0);
3359 sqlite3_create_function(p->db, "sha3", 2, SQLITE_UTF8, 0,
3360 sha3Func, 0, 0);
3361 sqlite3_create_function(p->db, "sha3_query", 1, SQLITE_UTF8, 0,
3362 sha3QueryFunc, 0, 0);
3363 sqlite3_create_function(p->db, "sha3_query", 2, SQLITE_UTF8, 0,
3364 sha3QueryFunc, 0, 0);
3365 }
3366 }
3367
3368 /*
3369 ** Do C-language style dequoting.
@@ -2564,11 +3484,11 @@
3484
3485 /*
3486 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
3487 ** for TRUE and FALSE. Return the integer value if appropriate.
3488 */
3489 static int booleanValue(const char *zArg){
3490 int i;
3491 if( zArg[0]=='0' && zArg[1]=='x' ){
3492 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
3493 }else{
3494 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
@@ -2582,10 +3502,21 @@
3502 }
3503 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
3504 zArg);
3505 return 0;
3506 }
3507
3508 /*
3509 ** Set or clear a shell flag according to a boolean value.
3510 */
3511 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
3512 if( booleanValue(zArg) ){
3513 ShellSetFlag(p, mFlag);
3514 }else{
3515 ShellClearFlag(p, mFlag);
3516 }
3517 }
3518
3519 /*
3520 ** Close an output file, assuming it is not stderr or stdout
3521 */
3522 static void output_file_close(FILE *f){
@@ -3656,11 +4587,11 @@
4587 test_breakpoint();
4588 }else
4589
4590 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
4591 if( nArg==2 ){
4592 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
4593 }else{
4594 raw_printf(stderr, "Usage: .changes on|off\n");
4595 rc = 1;
4596 }
4597 }else
@@ -3720,25 +4651,46 @@
4651 if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){
4652 rc = shell_dbinfo_command(p, nArg, azArg);
4653 }else
4654
4655 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
4656 const char *zLike = 0;
4657 int i;
4658 ShellClearFlag(p, SHFLG_PreserveRowid);
4659 for(i=1; i<nArg; i++){
4660 if( azArg[i][0]=='-' ){
4661 const char *z = azArg[i]+1;
4662 if( z[0]=='-' ) z++;
4663 if( strcmp(z,"preserve-rowids")==0 ){
4664 ShellSetFlag(p, SHFLG_PreserveRowid);
4665 }else
4666 {
4667 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
4668 rc = 1;
4669 goto meta_command_exit;
4670 }
4671 }else if( zLike ){
4672 raw_printf(stderr, "Usage: .dump ?--preserve-rowids? ?LIKE-PATTERN?\n");
4673 rc = 1;
4674 goto meta_command_exit;
4675 }else{
4676 zLike = azArg[i];
4677 }
4678 }
4679 open_db(p, 0);
4680 /* When playing back a "dump", the content might appear in an order
4681 ** which causes immediate foreign key constraints to be violated.
4682 ** So disable foreign-key constraint enforcement to prevent problems. */
 
 
 
 
 
4683 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
4684 raw_printf(p->out, "BEGIN TRANSACTION;\n");
4685 p->writableSchema = 0;
4686 /* Set writable_schema=ON since doing so forces SQLite to initialize
4687 ** as much of the schema as it can even if the sqlite_master table is
4688 ** corrupt. */
4689 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
4690 p->nErr = 0;
4691 if( zLike==0 ){
4692 run_schema_dump_query(p,
4693 "SELECT name, type, sql FROM sqlite_master "
4694 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
4695 );
4696 run_schema_dump_query(p,
@@ -3748,25 +4700,24 @@
4700 run_table_dump_query(p,
4701 "SELECT sql FROM sqlite_master "
4702 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
4703 );
4704 }else{
4705 char *zSql;
4706 zSql = sqlite3_mprintf(
4707 "SELECT name, type, sql FROM sqlite_master "
4708 "WHERE tbl_name LIKE %Q AND type=='table'"
4709 " AND sql NOT NULL", zLike);
4710 run_schema_dump_query(p,zSql);
4711 sqlite3_free(zSql);
4712 zSql = sqlite3_mprintf(
4713 "SELECT sql FROM sqlite_master "
4714 "WHERE sql NOT NULL"
4715 " AND type IN ('index','trigger','view')"
4716 " AND tbl_name LIKE %Q", zLike);
4717 run_table_dump_query(p, zSql, 0);
4718 sqlite3_free(zSql);
 
4719 }
4720 if( p->writableSchema ){
4721 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
4722 p->writableSchema = 0;
4723 }
@@ -3775,11 +4726,11 @@
4726 raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
4727 }else
4728
4729 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
4730 if( nArg==2 ){
4731 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
4732 }else{
4733 raw_printf(stderr, "Usage: .echo on|off\n");
4734 rc = 1;
4735 }
4736 }else
@@ -4572,21 +5523,21 @@
5523 new_colv[0] = "sql";
5524 new_colv[1] = 0;
5525 callback(&data, 1, new_argv, new_colv);
5526 rc = SQLITE_OK;
5527 }else{
5528 char *zSql;
5529 zSql = sqlite3_mprintf(
5530 "SELECT sql FROM "
5531 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
5532 " FROM sqlite_master UNION ALL"
5533 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
5534 "WHERE lower(tbl_name) LIKE %Q"
5535 " AND type!='meta' AND sql NOTNULL "
5536 "ORDER BY rowid", azArg[1]);
5537 rc = sqlite3_exec(p->db, zSql, callback, &data, &zErrMsg);
5538 sqlite3_free(zSql);
5539 }
5540 }else if( nArg==1 ){
5541 rc = sqlite3_exec(p->db,
5542 "SELECT sql FROM "
5543 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
@@ -4835,10 +5786,123 @@
5786 utf8_printf(p->out, "%s", zBuf);
5787 }
5788 }
5789 }else
5790 #endif
5791
5792 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
5793 int bIsInit = 0; /* True to initialize the SELFTEST table */
5794 int bVerbose = 0; /* Verbose output */
5795 int bSelftestExists; /* True if SELFTEST already exists */
5796 char **azTest = 0; /* Content of the SELFTEST table */
5797 int nRow = 0; /* Number of rows in the SELFTEST table */
5798 int nCol = 4; /* Number of columns in the SELFTEST table */
5799 int i; /* Loop counter */
5800 int nTest = 0; /* Number of tests runs */
5801 int nErr = 0; /* Number of errors seen */
5802 ShellText str; /* Answer for a query */
5803 static char *azDefaultTest[] = {
5804 0, 0, 0, 0,
5805 "0", "memo", "Missing SELFTEST table - default checks only", "",
5806 "1", "run", "PRAGMA integrity_check", "ok"
5807 };
5808 static const int nDefaultRow = 2;
5809
5810 open_db(p,0);
5811 for(i=1; i<nArg; i++){
5812 const char *z = azArg[i];
5813 if( z[0]=='-' && z[1]=='-' ) z++;
5814 if( strcmp(z,"-init")==0 ){
5815 bIsInit = 1;
5816 }else
5817 if( strcmp(z,"-v")==0 ){
5818 bVerbose++;
5819 }else
5820 {
5821 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
5822 azArg[i], azArg[0]);
5823 raw_printf(stderr, "Should be one of: --init -v\n");
5824 rc = 1;
5825 goto meta_command_exit;
5826 }
5827 }
5828 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
5829 != SQLITE_OK ){
5830 bSelftestExists = 0;
5831 }else{
5832 bSelftestExists = 1;
5833 }
5834 if( bIsInit ){
5835 createSelftestTable(p);
5836 bSelftestExists = 1;
5837 }
5838 if( bSelftestExists ){
5839 rc = sqlite3_get_table(p->db,
5840 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
5841 &azTest, &nRow, &nCol, 0);
5842 if( rc ){
5843 raw_printf(stderr, "Error querying the selftest table\n");
5844 rc = 1;
5845 sqlite3_free_table(azTest);
5846 goto meta_command_exit;
5847 }else if( nRow==0 ){
5848 sqlite3_free_table(azTest);
5849 azTest = azDefaultTest;
5850 nRow = nDefaultRow;
5851 }
5852 }else{
5853 azTest = azDefaultTest;
5854 nRow = nDefaultRow;
5855 }
5856 initText(&str);
5857 appendText(&str, "x", 0);
5858 for(i=1; i<=nRow; i++){
5859 int tno = atoi(azTest[i*nCol]);
5860 const char *zOp = azTest[i*nCol+1];
5861 const char *zSql = azTest[i*nCol+2];
5862 const char *zAns = azTest[i*nCol+3];
5863
5864 if( bVerbose>0 ){
5865 char *zQuote = sqlite3_mprintf("%q", zSql);
5866 printf("%d: %s %s\n", tno, zOp, zSql);
5867 sqlite3_free(zQuote);
5868 }
5869 if( strcmp(zOp,"memo")==0 ){
5870 utf8_printf(p->out, "%s\n", zSql);
5871 }else
5872 if( strcmp(zOp,"run")==0 ){
5873 char *zErrMsg = 0;
5874 str.n = 0;
5875 str.z[0] = 0;
5876 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
5877 nTest++;
5878 if( bVerbose ){
5879 utf8_printf(p->out, "Result: %s\n", str.z);
5880 }
5881 if( rc || zErrMsg ){
5882 nErr++;
5883 rc = 1;
5884 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
5885 sqlite3_free(zErrMsg);
5886 }else if( strcmp(zAns,str.z)!=0 ){
5887 nErr++;
5888 rc = 1;
5889 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
5890 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
5891 }
5892 }else
5893 {
5894 utf8_printf(stderr,
5895 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
5896 rc = 1;
5897 break;
5898 }
5899 }
5900 freeText(&str);
5901 if( azTest!=azDefaultTest ) sqlite3_free_table(azTest);
5902 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
5903 }else
5904
5905 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
5906 if( nArg<2 || nArg>3 ){
5907 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
5908 rc = 1;
@@ -4850,10 +5914,126 @@
5914 if( nArg>=3 ){
5915 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
5916 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
5917 }
5918 }else
5919
5920 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
5921 const char *zLike = 0; /* Which table to checksum. 0 means everything */
5922 int i; /* Loop counter */
5923 int bSchema = 0; /* Also hash the schema */
5924 int bSeparate = 0; /* Hash each table separately */
5925 int iSize = 224; /* Hash algorithm to use */
5926 int bDebug = 0; /* Only show the query that would have run */
5927 sqlite3_stmt *pStmt; /* For querying tables names */
5928 char *zSql; /* SQL to be run */
5929 char *zSep; /* Separator */
5930 ShellText sSql; /* Complete SQL for the query to run the hash */
5931 ShellText sQuery; /* Set of queries used to read all content */
5932 open_db(p, 0);
5933 for(i=1; i<nArg; i++){
5934 const char *z = azArg[i];
5935 if( z[0]=='-' ){
5936 z++;
5937 if( z[0]=='-' ) z++;
5938 if( strcmp(z,"schema")==0 ){
5939 bSchema = 1;
5940 }else
5941 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
5942 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
5943 ){
5944 iSize = atoi(&z[5]);
5945 }else
5946 if( strcmp(z,"debug")==0 ){
5947 bDebug = 1;
5948 }else
5949 {
5950 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
5951 azArg[i], azArg[0]);
5952 raw_printf(stderr, "Should be one of: --schema"
5953 " --sha3-224 --sha3-255 --sha3-384 --sha3-512\n");
5954 rc = 1;
5955 goto meta_command_exit;
5956 }
5957 }else if( zLike ){
5958 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
5959 rc = 1;
5960 goto meta_command_exit;
5961 }else{
5962 zLike = z;
5963 bSeparate = 1;
5964 if( sqlite3_strlike("sqlite_%", zLike, 0)==0 ) bSchema = 1;
5965 }
5966 }
5967 if( bSchema ){
5968 zSql = "SELECT lower(name) FROM sqlite_master"
5969 " WHERE type='table' AND coalesce(rootpage,0)>1"
5970 " UNION ALL SELECT 'sqlite_master'"
5971 " ORDER BY 1 collate nocase";
5972 }else{
5973 zSql = "SELECT lower(name) FROM sqlite_master"
5974 " WHERE type='table' AND coalesce(rootpage,0)>1"
5975 " AND name NOT LIKE 'sqlite_%'"
5976 " ORDER BY 1 collate nocase";
5977 }
5978 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5979 initText(&sQuery);
5980 initText(&sSql);
5981 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
5982 zSep = "VALUES(";
5983 while( SQLITE_ROW==sqlite3_step(pStmt) ){
5984 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
5985 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
5986 if( strncmp(zTab, "sqlite_",7)!=0 ){
5987 appendText(&sQuery,"SELECT * FROM ", 0);
5988 appendText(&sQuery,zTab,'"');
5989 appendText(&sQuery," NOT INDEXED;", 0);
5990 }else if( strcmp(zTab, "sqlite_master")==0 ){
5991 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
5992 " ORDER BY name;", 0);
5993 }else if( strcmp(zTab, "sqlite_sequence")==0 ){
5994 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
5995 " ORDER BY name;", 0);
5996 }else if( strcmp(zTab, "sqlite_stat1")==0 ){
5997 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
5998 " ORDER BY tbl,idx;", 0);
5999 }else if( strcmp(zTab, "sqlite_stat3")==0
6000 || strcmp(zTab, "sqlite_stat4")==0 ){
6001 appendText(&sQuery, "SELECT * FROM ", 0);
6002 appendText(&sQuery, zTab, 0);
6003 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
6004 }
6005 appendText(&sSql, zSep, 0);
6006 appendText(&sSql, sQuery.z, '\'');
6007 sQuery.n = 0;
6008 appendText(&sSql, ",", 0);
6009 appendText(&sSql, zTab, '\'');
6010 zSep = "),(";
6011 }
6012 sqlite3_finalize(pStmt);
6013 if( bSeparate ){
6014 zSql = sqlite3_mprintf(
6015 "%s))"
6016 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
6017 " FROM [sha3sum$query]",
6018 sSql.z, iSize);
6019 }else{
6020 zSql = sqlite3_mprintf(
6021 "%s))"
6022 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
6023 " FROM [sha3sum$query]",
6024 sSql.z, iSize);
6025 }
6026 freeText(&sQuery);
6027 freeText(&sSql);
6028 if( bDebug ){
6029 utf8_printf(p->out, "%s\n", zSql);
6030 }else{
6031 shell_exec(p->db, zSql, shell_callback, p, 0);
6032 }
6033 sqlite3_free(zSql);
6034 }else
6035
6036 if( c=='s'
6037 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
6038 ){
6039 char *zCmd;
@@ -4879,11 +6059,12 @@
6059 if( nArg!=1 ){
6060 raw_printf(stderr, "Usage: .show\n");
6061 rc = 1;
6062 goto meta_command_exit;
6063 }
6064 utf8_printf(p->out, "%12.12s: %s\n","echo",
6065 azBool[ShellHasFlag(p, SHFLG_Echo)]);
6066 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
6067 utf8_printf(p->out, "%12.12s: %s\n","explain",
6068 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
6069 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
6070 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
@@ -5444,11 +6625,11 @@
6625 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
6626 int rc;
6627 char *zErrMsg = 0;
6628
6629 open_db(p, 0);
6630 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
6631 BEGIN_TIMER;
6632 rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
6633 END_TIMER;
6634 if( rc || zErrMsg ){
6635 char zPrefix[100];
@@ -5464,11 +6645,11 @@
6645 zErrMsg = 0;
6646 }else{
6647 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
6648 }
6649 return 1;
6650 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
6651 raw_printf(p->out, "changes: %3d total_changes: %d\n",
6652 sqlite3_changes(p->db), sqlite3_total_changes(p->db));
6653 }
6654 return 0;
6655 }
@@ -5507,15 +6688,15 @@
6688 if( in!=0 ) break;
6689 seenInterrupt = 0;
6690 }
6691 lineno++;
6692 if( nSql==0 && _all_whitespace(zLine) ){
6693 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
6694 continue;
6695 }
6696 if( zLine && zLine[0]=='.' && nSql==0 ){
6697 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
6698 rc = do_meta_command(zLine, p);
6699 if( rc==2 ){ /* exit requested */
6700 break;
6701 }else if( rc ){
6702 errCnt++;
@@ -5554,11 +6735,11 @@
6735 if( p->outCount ){
6736 output_reset(p);
6737 p->outCount = 0;
6738 }
6739 }else if( nSql && _all_whitespace(zSql) ){
6740 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
6741 nSql = 0;
6742 }
6743 }
6744 if( nSql && !_all_whitespace(zSql) ){
6745 runOneSqlLine(p, zSql, in, startline);
@@ -6023,11 +7204,11 @@
7204 }else if( strcmp(z,"-header")==0 ){
7205 data.showHeader = 1;
7206 }else if( strcmp(z,"-noheader")==0 ){
7207 data.showHeader = 0;
7208 }else if( strcmp(z,"-echo")==0 ){
7209 ShellSetFlag(&data, SHFLG_Echo);
7210 }else if( strcmp(z,"-eqp")==0 ){
7211 data.autoEQP = 1;
7212 }else if( strcmp(z,"-eqpfull")==0 ){
7213 data.autoEQP = 2;
7214 }else if( strcmp(z,"-stats")==0 ){
@@ -6038,11 +7219,11 @@
7219 /* Undocumented command-line option: -backslash
7220 ** Causes C-style backslash escapes to be evaluated in SQL statements
7221 ** prior to sending the SQL into SQLite. Useful for injecting
7222 ** crazy bytes in the middle of SQL statements for testing and debugging.
7223 */
7224 ShellSetFlag(&data, SHFLG_Backslash);
7225 }else if( strcmp(z,"-bail")==0 ){
7226 bail_on_error = 1;
7227 }else if( strcmp(z,"-version")==0 ){
7228 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
7229 return 0;
7230
+59 -45
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -398,11 +398,11 @@
398398
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
399399
** [sqlite_version()] and [sqlite_source_id()].
400400
*/
401401
#define SQLITE_VERSION "3.18.0"
402402
#define SQLITE_VERSION_NUMBER 3018000
403
-#define SQLITE_SOURCE_ID "2017-03-06 17:33:58 137aeb2b160888100bc1e871b00860149e5f6196"
403
+#define SQLITE_SOURCE_ID "2017-03-10 17:03:11 f8560c60d10c0365b33342ab05b5a953987b0471"
404404
405405
/*
406406
** CAPI3REF: Run-Time Library Version Numbers
407407
** KEYWORDS: sqlite3_version sqlite3_sourceid
408408
**
@@ -24412,10 +24412,17 @@
2441224412
** implementation of malloc_good_size(), which must be called in debug
2441324413
** mode and specifically when the DMD "Dark Matter Detector" is enabled
2441424414
** or else a crash results. Hence, do not attempt to optimize out the
2441524415
** following xRoundup() call. */
2441624416
nFull = sqlite3GlobalConfig.m.xRoundup(n);
24417
+
24418
+#ifdef SQLITE_MAX_MEMORY
24419
+ if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nFull>SQLITE_MAX_MEMORY ){
24420
+ *pp = 0;
24421
+ return;
24422
+ }
24423
+#endif
2441724424
2441824425
sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
2441924426
if( mem0.alarmThreshold>0 ){
2442024427
sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
2442124428
if( nUsed >= mem0.alarmThreshold - nFull ){
@@ -88985,15 +88992,15 @@
8898588992
** The return value from the callback should be one of the WRC_*
8898688993
** constants to specify how to proceed with the walk.
8898788994
**
8898888995
** WRC_Continue Continue descending down the tree.
8898988996
**
88990
-** WRC_Prune Do not descend into child nodes. But allow
88997
+** WRC_Prune Do not descend into child nodes, but allow
8899188998
** the walk to continue with sibling nodes.
8899288999
**
8899389000
** WRC_Abort Do no more callbacks. Unwind the stack and
88994
-** return the top-level walk call.
89001
+** return from the top-level walk call.
8899589002
**
8899689003
** The return value from this routine is WRC_Abort to abandon the tree walk
8899789004
** and WRC_Continue to continue.
8899889005
*/
8899989006
static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){
@@ -89351,11 +89358,12 @@
8935189358
}
8935289359
}
8935389360
}
8935489361
8935589362
/* Start at the inner-most context and move outward until a match is found */
89356
- while( pNC && cnt==0 ){
89363
+ assert( pNC && cnt==0 );
89364
+ do{
8935789365
ExprList *pEList;
8935889366
SrcList *pSrcList = pNC->pSrcList;
8935989367
8936089368
if( pSrcList ){
8936189369
for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
@@ -89536,15 +89544,15 @@
8953689544
}
8953789545
8953889546
/* Advance to the next name context. The loop will exit when either
8953989547
** we have a match (cnt>0) or when we run out of name contexts.
8954089548
*/
89541
- if( cnt==0 ){
89542
- pNC = pNC->pNext;
89543
- nSubquery++;
89544
- }
89545
- }
89549
+ if( cnt ) break;
89550
+ pNC = pNC->pNext;
89551
+ nSubquery++;
89552
+ }while( pNC );
89553
+
8954689554
8954789555
/*
8954889556
** If X and Y are NULL (in other words if only the column name Z is
8954989557
** supplied) and the value of Z is enclosed in double-quotes, then
8955089558
** Z is a string literal if it doesn't match any column names. In that
@@ -89730,37 +89738,42 @@
8973089738
break;
8973189739
}
8973289740
#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
8973389741
&& !defined(SQLITE_OMIT_SUBQUERY) */
8973489742
89735
- /* A lone identifier is the name of a column.
89736
- */
89737
- case TK_ID: {
89738
- return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
89739
- }
89740
-
89741
- /* A table name and column name: ID.ID
89743
+ /* A column name: ID
89744
+ ** Or table name and column name: ID.ID
8974289745
** Or a database, table and column: ID.ID.ID
89746
+ **
89747
+ ** The TK_ID and TK_OUT cases are combined so that there will only
89748
+ ** be one call to lookupName(). Then the compiler will in-line
89749
+ ** lookupName() for a size reduction and performance increase.
8974389750
*/
89751
+ case TK_ID:
8974489752
case TK_DOT: {
8974589753
const char *zColumn;
8974689754
const char *zTable;
8974789755
const char *zDb;
8974889756
Expr *pRight;
8974989757
89750
- /* if( pSrcList==0 ) break; */
89751
- notValid(pParse, pNC, "the \".\" operator", NC_IdxExpr);
89752
- pRight = pExpr->pRight;
89753
- if( pRight->op==TK_ID ){
89758
+ if( pExpr->op==TK_ID ){
8975489759
zDb = 0;
89755
- zTable = pExpr->pLeft->u.zToken;
89756
- zColumn = pRight->u.zToken;
89760
+ zTable = 0;
89761
+ zColumn = pExpr->u.zToken;
8975789762
}else{
89758
- assert( pRight->op==TK_DOT );
89759
- zDb = pExpr->pLeft->u.zToken;
89760
- zTable = pRight->pLeft->u.zToken;
89761
- zColumn = pRight->pRight->u.zToken;
89763
+ notValid(pParse, pNC, "the \".\" operator", NC_IdxExpr);
89764
+ pRight = pExpr->pRight;
89765
+ if( pRight->op==TK_ID ){
89766
+ zDb = 0;
89767
+ zTable = pExpr->pLeft->u.zToken;
89768
+ zColumn = pRight->u.zToken;
89769
+ }else{
89770
+ assert( pRight->op==TK_DOT );
89771
+ zDb = pExpr->pLeft->u.zToken;
89772
+ zTable = pRight->pLeft->u.zToken;
89773
+ zColumn = pRight->pRight->u.zToken;
89774
+ }
8976289775
}
8976389776
return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
8976489777
}
8976589778
8976689779
/* Resolve function names
@@ -114759,11 +114772,11 @@
114759114772
** PRAGMA schema.optimize(MASK)
114760114773
**
114761114774
** Attempt to optimize the database. All schemas are optimized in the first
114762114775
** two forms, and only the specified schema is optimized in the latter two.
114763114776
**
114764
- ** The details of optimizations performed by this pragma does are expected
114777
+ ** The details of optimizations performed by this pragma are expected
114765114778
** to change and improve over time. Applications should anticipate that
114766114779
** this pragma will perform new optimizations in future releases.
114767114780
**
114768114781
** The optional argument is a bitmask of optimizations to perform:
114769114782
**
@@ -114780,14 +114793,14 @@
114780114793
** pragmas run by future database connections.
114781114794
**
114782114795
** 0x0008 (Not yet implemented) Create indexes that might have
114783114796
** been helpful to recent queries
114784114797
**
114785
- ** The default MASK is 0x000e, which means perform all of the optimizations
114786
- ** listed above except do not set Debug Mode. New optimizations may be
114787
- ** added in future releases but they will be turned off by default. The
114788
- ** default MASK will always be 0x0e.
114798
+ ** The default MASK is and always shall be 0xfffe. 0xfffe means perform all ** of the optimizations listed above except Debug Mode, including new
114799
+ ** optimizations that have not yet been invented. If new optimizations are
114800
+ ** ever added that should be off by default, those off-by-default
114801
+ ** optimizations will have bitmasks of 0x10000 or larger.
114789114802
**
114790114803
** DETERMINATION OF WHEN TO RUN ANALYZE
114791114804
**
114792114805
** In the current implementation, a table is analyzed if only if all of
114793114806
** the following are true:
@@ -114818,11 +114831,11 @@
114818114831
114819114832
if( zRight ){
114820114833
opMask = (u32)sqlite3Atoi(zRight);
114821114834
if( (opMask & 0x02)==0 ) break;
114822114835
}else{
114823
- opMask = 0xe;
114836
+ opMask = 0xfffe;
114824114837
}
114825114838
iTabCur = pParse->nTab++;
114826114839
for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
114827114840
if( iDb==1 ) continue;
114828114841
sqlite3CodeVerifySchema(pParse, iDb);
@@ -137673,11 +137686,11 @@
137673137686
case 158: /* term ::= INTEGER */
137674137687
{
137675137688
yylhsminor.yy190.pExpr = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &yymsp[0].minor.yy0, 1);
137676137689
yylhsminor.yy190.zStart = yymsp[0].minor.yy0.z;
137677137690
yylhsminor.yy190.zEnd = yymsp[0].minor.yy0.z + yymsp[0].minor.yy0.n;
137678
- if( yylhsminor.yy190.pExpr ) yylhsminor.yy190.pExpr->flags |= EP_Leaf;
137691
+ if( yylhsminor.yy190.pExpr ) yylhsminor.yy190.pExpr->flags |= EP_Leaf|EP_Resolved;
137679137692
}
137680137693
yymsp[0].minor.yy190 = yylhsminor.yy190;
137681137694
break;
137682137695
case 159: /* expr ::= VARIABLE */
137683137696
{
@@ -139295,12 +139308,12 @@
139295139308
** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
139296139309
** error message.
139297139310
*/
139298139311
SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
139299139312
int nErr = 0; /* Number of errors encountered */
139300
- int i; /* Loop counter */
139301139313
void *pEngine; /* The LEMON-generated LALR(1) parser */
139314
+ int n = 0; /* Length of the next token token */
139302139315
int tokenType; /* type of the next token */
139303139316
int lastTokenParsed = -1; /* type of the previous token */
139304139317
sqlite3 *db = pParse->db; /* The database connection */
139305139318
int mxSqlLen; /* Max length of an SQL string */
139306139319
#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
@@ -139312,11 +139325,10 @@
139312139325
if( db->nVdbeActive==0 ){
139313139326
db->u1.isInterrupted = 0;
139314139327
}
139315139328
pParse->rc = SQLITE_OK;
139316139329
pParse->zTail = zSql;
139317
- i = 0;
139318139330
assert( pzErrMsg!=0 );
139319139331
/* sqlite3ParserTrace(stdout, "parser: "); */
139320139332
#ifdef sqlite3Parser_ENGINEALWAYSONSTACK
139321139333
pEngine = zSpace;
139322139334
sqlite3ParserInit(pEngine);
@@ -139330,16 +139342,14 @@
139330139342
assert( pParse->pNewTable==0 );
139331139343
assert( pParse->pNewTrigger==0 );
139332139344
assert( pParse->nVar==0 );
139333139345
assert( pParse->pVList==0 );
139334139346
while( 1 ){
139335
- assert( i>=0 );
139336
- if( zSql[i]!=0 ){
139337
- pParse->sLastToken.z = &zSql[i];
139338
- pParse->sLastToken.n = sqlite3GetToken((u8*)&zSql[i],&tokenType);
139339
- i += pParse->sLastToken.n;
139340
- if( i>mxSqlLen ){
139347
+ if( zSql[0]!=0 ){
139348
+ n = sqlite3GetToken((u8*)zSql, &tokenType);
139349
+ mxSqlLen -= n;
139350
+ if( mxSqlLen<0 ){
139341139351
pParse->rc = SQLITE_TOOBIG;
139342139352
break;
139343139353
}
139344139354
}else{
139345139355
/* Upon reaching the end of input, call the parser two more times
@@ -139349,30 +139359,34 @@
139349139359
}else if( lastTokenParsed==0 ){
139350139360
break;
139351139361
}else{
139352139362
tokenType = TK_SEMI;
139353139363
}
139364
+ zSql -= n;
139354139365
}
139355139366
if( tokenType>=TK_SPACE ){
139356139367
assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
139357139368
if( db->u1.isInterrupted ){
139358139369
pParse->rc = SQLITE_INTERRUPT;
139359139370
break;
139360139371
}
139361139372
if( tokenType==TK_ILLEGAL ){
139362
- sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"",
139363
- &pParse->sLastToken);
139373
+ sqlite3ErrorMsg(pParse, "unrecognized token: \"%.*s\"", n, zSql);
139364139374
break;
139365139375
}
139376
+ zSql += n;
139366139377
}else{
139378
+ pParse->sLastToken.z = zSql;
139379
+ pParse->sLastToken.n = n;
139367139380
sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
139368139381
lastTokenParsed = tokenType;
139382
+ zSql += n;
139369139383
if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break;
139370139384
}
139371139385
}
139372139386
assert( nErr==0 );
139373
- pParse->zTail = &zSql[i];
139387
+ pParse->zTail = zSql;
139374139388
#ifdef YYTRACKMAXSTACKDEPTH
139375139389
sqlite3_mutex_enter(sqlite3MallocMutex());
139376139390
sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
139377139391
sqlite3ParserStackPeak(pEngine)
139378139392
);
@@ -198053,11 +198067,11 @@
198053198067
int nArg, /* Number of args */
198054198068
sqlite3_value **apUnused /* Function arguments */
198055198069
){
198056198070
assert( nArg==0 );
198057198071
UNUSED_PARAM2(nArg, apUnused);
198058
- sqlite3_result_text(pCtx, "fts5: 2017-03-03 16:51:46 915a9a28783fbb2f4c0794eb4264ce8c0b9d42f7", -1, SQLITE_TRANSIENT);
198072
+ sqlite3_result_text(pCtx, "fts5: 2017-03-10 17:03:11 f8560c60d10c0365b33342ab05b5a953987b0471", -1, SQLITE_TRANSIENT);
198059198073
}
198060198074
198061198075
static int fts5Init(sqlite3 *db){
198062198076
static const sqlite3_module fts5Mod = {
198063198077
/* iVersion */ 2,
198064198078
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -398,11 +398,11 @@
398 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
399 ** [sqlite_version()] and [sqlite_source_id()].
400 */
401 #define SQLITE_VERSION "3.18.0"
402 #define SQLITE_VERSION_NUMBER 3018000
403 #define SQLITE_SOURCE_ID "2017-03-06 17:33:58 137aeb2b160888100bc1e871b00860149e5f6196"
404
405 /*
406 ** CAPI3REF: Run-Time Library Version Numbers
407 ** KEYWORDS: sqlite3_version sqlite3_sourceid
408 **
@@ -24412,10 +24412,17 @@
24412 ** implementation of malloc_good_size(), which must be called in debug
24413 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
24414 ** or else a crash results. Hence, do not attempt to optimize out the
24415 ** following xRoundup() call. */
24416 nFull = sqlite3GlobalConfig.m.xRoundup(n);
 
 
 
 
 
 
 
24417
24418 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
24419 if( mem0.alarmThreshold>0 ){
24420 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
24421 if( nUsed >= mem0.alarmThreshold - nFull ){
@@ -88985,15 +88992,15 @@
88985 ** The return value from the callback should be one of the WRC_*
88986 ** constants to specify how to proceed with the walk.
88987 **
88988 ** WRC_Continue Continue descending down the tree.
88989 **
88990 ** WRC_Prune Do not descend into child nodes. But allow
88991 ** the walk to continue with sibling nodes.
88992 **
88993 ** WRC_Abort Do no more callbacks. Unwind the stack and
88994 ** return the top-level walk call.
88995 **
88996 ** The return value from this routine is WRC_Abort to abandon the tree walk
88997 ** and WRC_Continue to continue.
88998 */
88999 static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){
@@ -89351,11 +89358,12 @@
89351 }
89352 }
89353 }
89354
89355 /* Start at the inner-most context and move outward until a match is found */
89356 while( pNC && cnt==0 ){
 
89357 ExprList *pEList;
89358 SrcList *pSrcList = pNC->pSrcList;
89359
89360 if( pSrcList ){
89361 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
@@ -89536,15 +89544,15 @@
89536 }
89537
89538 /* Advance to the next name context. The loop will exit when either
89539 ** we have a match (cnt>0) or when we run out of name contexts.
89540 */
89541 if( cnt==0 ){
89542 pNC = pNC->pNext;
89543 nSubquery++;
89544 }
89545 }
89546
89547 /*
89548 ** If X and Y are NULL (in other words if only the column name Z is
89549 ** supplied) and the value of Z is enclosed in double-quotes, then
89550 ** Z is a string literal if it doesn't match any column names. In that
@@ -89730,37 +89738,42 @@
89730 break;
89731 }
89732 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
89733 && !defined(SQLITE_OMIT_SUBQUERY) */
89734
89735 /* A lone identifier is the name of a column.
89736 */
89737 case TK_ID: {
89738 return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
89739 }
89740
89741 /* A table name and column name: ID.ID
89742 ** Or a database, table and column: ID.ID.ID
 
 
 
 
89743 */
 
89744 case TK_DOT: {
89745 const char *zColumn;
89746 const char *zTable;
89747 const char *zDb;
89748 Expr *pRight;
89749
89750 /* if( pSrcList==0 ) break; */
89751 notValid(pParse, pNC, "the \".\" operator", NC_IdxExpr);
89752 pRight = pExpr->pRight;
89753 if( pRight->op==TK_ID ){
89754 zDb = 0;
89755 zTable = pExpr->pLeft->u.zToken;
89756 zColumn = pRight->u.zToken;
89757 }else{
89758 assert( pRight->op==TK_DOT );
89759 zDb = pExpr->pLeft->u.zToken;
89760 zTable = pRight->pLeft->u.zToken;
89761 zColumn = pRight->pRight->u.zToken;
 
 
 
 
 
 
 
 
89762 }
89763 return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
89764 }
89765
89766 /* Resolve function names
@@ -114759,11 +114772,11 @@
114759 ** PRAGMA schema.optimize(MASK)
114760 **
114761 ** Attempt to optimize the database. All schemas are optimized in the first
114762 ** two forms, and only the specified schema is optimized in the latter two.
114763 **
114764 ** The details of optimizations performed by this pragma does are expected
114765 ** to change and improve over time. Applications should anticipate that
114766 ** this pragma will perform new optimizations in future releases.
114767 **
114768 ** The optional argument is a bitmask of optimizations to perform:
114769 **
@@ -114780,14 +114793,14 @@
114780 ** pragmas run by future database connections.
114781 **
114782 ** 0x0008 (Not yet implemented) Create indexes that might have
114783 ** been helpful to recent queries
114784 **
114785 ** The default MASK is 0x000e, which means perform all of the optimizations
114786 ** listed above except do not set Debug Mode. New optimizations may be
114787 ** added in future releases but they will be turned off by default. The
114788 ** default MASK will always be 0x0e.
114789 **
114790 ** DETERMINATION OF WHEN TO RUN ANALYZE
114791 **
114792 ** In the current implementation, a table is analyzed if only if all of
114793 ** the following are true:
@@ -114818,11 +114831,11 @@
114818
114819 if( zRight ){
114820 opMask = (u32)sqlite3Atoi(zRight);
114821 if( (opMask & 0x02)==0 ) break;
114822 }else{
114823 opMask = 0xe;
114824 }
114825 iTabCur = pParse->nTab++;
114826 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
114827 if( iDb==1 ) continue;
114828 sqlite3CodeVerifySchema(pParse, iDb);
@@ -137673,11 +137686,11 @@
137673 case 158: /* term ::= INTEGER */
137674 {
137675 yylhsminor.yy190.pExpr = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &yymsp[0].minor.yy0, 1);
137676 yylhsminor.yy190.zStart = yymsp[0].minor.yy0.z;
137677 yylhsminor.yy190.zEnd = yymsp[0].minor.yy0.z + yymsp[0].minor.yy0.n;
137678 if( yylhsminor.yy190.pExpr ) yylhsminor.yy190.pExpr->flags |= EP_Leaf;
137679 }
137680 yymsp[0].minor.yy190 = yylhsminor.yy190;
137681 break;
137682 case 159: /* expr ::= VARIABLE */
137683 {
@@ -139295,12 +139308,12 @@
139295 ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
139296 ** error message.
139297 */
139298 SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
139299 int nErr = 0; /* Number of errors encountered */
139300 int i; /* Loop counter */
139301 void *pEngine; /* The LEMON-generated LALR(1) parser */
 
139302 int tokenType; /* type of the next token */
139303 int lastTokenParsed = -1; /* type of the previous token */
139304 sqlite3 *db = pParse->db; /* The database connection */
139305 int mxSqlLen; /* Max length of an SQL string */
139306 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
@@ -139312,11 +139325,10 @@
139312 if( db->nVdbeActive==0 ){
139313 db->u1.isInterrupted = 0;
139314 }
139315 pParse->rc = SQLITE_OK;
139316 pParse->zTail = zSql;
139317 i = 0;
139318 assert( pzErrMsg!=0 );
139319 /* sqlite3ParserTrace(stdout, "parser: "); */
139320 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
139321 pEngine = zSpace;
139322 sqlite3ParserInit(pEngine);
@@ -139330,16 +139342,14 @@
139330 assert( pParse->pNewTable==0 );
139331 assert( pParse->pNewTrigger==0 );
139332 assert( pParse->nVar==0 );
139333 assert( pParse->pVList==0 );
139334 while( 1 ){
139335 assert( i>=0 );
139336 if( zSql[i]!=0 ){
139337 pParse->sLastToken.z = &zSql[i];
139338 pParse->sLastToken.n = sqlite3GetToken((u8*)&zSql[i],&tokenType);
139339 i += pParse->sLastToken.n;
139340 if( i>mxSqlLen ){
139341 pParse->rc = SQLITE_TOOBIG;
139342 break;
139343 }
139344 }else{
139345 /* Upon reaching the end of input, call the parser two more times
@@ -139349,30 +139359,34 @@
139349 }else if( lastTokenParsed==0 ){
139350 break;
139351 }else{
139352 tokenType = TK_SEMI;
139353 }
 
139354 }
139355 if( tokenType>=TK_SPACE ){
139356 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
139357 if( db->u1.isInterrupted ){
139358 pParse->rc = SQLITE_INTERRUPT;
139359 break;
139360 }
139361 if( tokenType==TK_ILLEGAL ){
139362 sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"",
139363 &pParse->sLastToken);
139364 break;
139365 }
 
139366 }else{
 
 
139367 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
139368 lastTokenParsed = tokenType;
 
139369 if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break;
139370 }
139371 }
139372 assert( nErr==0 );
139373 pParse->zTail = &zSql[i];
139374 #ifdef YYTRACKMAXSTACKDEPTH
139375 sqlite3_mutex_enter(sqlite3MallocMutex());
139376 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
139377 sqlite3ParserStackPeak(pEngine)
139378 );
@@ -198053,11 +198067,11 @@
198053 int nArg, /* Number of args */
198054 sqlite3_value **apUnused /* Function arguments */
198055 ){
198056 assert( nArg==0 );
198057 UNUSED_PARAM2(nArg, apUnused);
198058 sqlite3_result_text(pCtx, "fts5: 2017-03-03 16:51:46 915a9a28783fbb2f4c0794eb4264ce8c0b9d42f7", -1, SQLITE_TRANSIENT);
198059 }
198060
198061 static int fts5Init(sqlite3 *db){
198062 static const sqlite3_module fts5Mod = {
198063 /* iVersion */ 2,
198064
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -398,11 +398,11 @@
398 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
399 ** [sqlite_version()] and [sqlite_source_id()].
400 */
401 #define SQLITE_VERSION "3.18.0"
402 #define SQLITE_VERSION_NUMBER 3018000
403 #define SQLITE_SOURCE_ID "2017-03-10 17:03:11 f8560c60d10c0365b33342ab05b5a953987b0471"
404
405 /*
406 ** CAPI3REF: Run-Time Library Version Numbers
407 ** KEYWORDS: sqlite3_version sqlite3_sourceid
408 **
@@ -24412,10 +24412,17 @@
24412 ** implementation of malloc_good_size(), which must be called in debug
24413 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
24414 ** or else a crash results. Hence, do not attempt to optimize out the
24415 ** following xRoundup() call. */
24416 nFull = sqlite3GlobalConfig.m.xRoundup(n);
24417
24418 #ifdef SQLITE_MAX_MEMORY
24419 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nFull>SQLITE_MAX_MEMORY ){
24420 *pp = 0;
24421 return;
24422 }
24423 #endif
24424
24425 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
24426 if( mem0.alarmThreshold>0 ){
24427 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
24428 if( nUsed >= mem0.alarmThreshold - nFull ){
@@ -88985,15 +88992,15 @@
88992 ** The return value from the callback should be one of the WRC_*
88993 ** constants to specify how to proceed with the walk.
88994 **
88995 ** WRC_Continue Continue descending down the tree.
88996 **
88997 ** WRC_Prune Do not descend into child nodes, but allow
88998 ** the walk to continue with sibling nodes.
88999 **
89000 ** WRC_Abort Do no more callbacks. Unwind the stack and
89001 ** return from the top-level walk call.
89002 **
89003 ** The return value from this routine is WRC_Abort to abandon the tree walk
89004 ** and WRC_Continue to continue.
89005 */
89006 static SQLITE_NOINLINE int walkExpr(Walker *pWalker, Expr *pExpr){
@@ -89351,11 +89358,12 @@
89358 }
89359 }
89360 }
89361
89362 /* Start at the inner-most context and move outward until a match is found */
89363 assert( pNC && cnt==0 );
89364 do{
89365 ExprList *pEList;
89366 SrcList *pSrcList = pNC->pSrcList;
89367
89368 if( pSrcList ){
89369 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
@@ -89536,15 +89544,15 @@
89544 }
89545
89546 /* Advance to the next name context. The loop will exit when either
89547 ** we have a match (cnt>0) or when we run out of name contexts.
89548 */
89549 if( cnt ) break;
89550 pNC = pNC->pNext;
89551 nSubquery++;
89552 }while( pNC );
89553
89554
89555 /*
89556 ** If X and Y are NULL (in other words if only the column name Z is
89557 ** supplied) and the value of Z is enclosed in double-quotes, then
89558 ** Z is a string literal if it doesn't match any column names. In that
@@ -89730,37 +89738,42 @@
89738 break;
89739 }
89740 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
89741 && !defined(SQLITE_OMIT_SUBQUERY) */
89742
89743 /* A column name: ID
89744 ** Or table name and column name: ID.ID
 
 
 
 
 
89745 ** Or a database, table and column: ID.ID.ID
89746 **
89747 ** The TK_ID and TK_OUT cases are combined so that there will only
89748 ** be one call to lookupName(). Then the compiler will in-line
89749 ** lookupName() for a size reduction and performance increase.
89750 */
89751 case TK_ID:
89752 case TK_DOT: {
89753 const char *zColumn;
89754 const char *zTable;
89755 const char *zDb;
89756 Expr *pRight;
89757
89758 if( pExpr->op==TK_ID ){
 
 
 
89759 zDb = 0;
89760 zTable = 0;
89761 zColumn = pExpr->u.zToken;
89762 }else{
89763 notValid(pParse, pNC, "the \".\" operator", NC_IdxExpr);
89764 pRight = pExpr->pRight;
89765 if( pRight->op==TK_ID ){
89766 zDb = 0;
89767 zTable = pExpr->pLeft->u.zToken;
89768 zColumn = pRight->u.zToken;
89769 }else{
89770 assert( pRight->op==TK_DOT );
89771 zDb = pExpr->pLeft->u.zToken;
89772 zTable = pRight->pLeft->u.zToken;
89773 zColumn = pRight->pRight->u.zToken;
89774 }
89775 }
89776 return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
89777 }
89778
89779 /* Resolve function names
@@ -114759,11 +114772,11 @@
114772 ** PRAGMA schema.optimize(MASK)
114773 **
114774 ** Attempt to optimize the database. All schemas are optimized in the first
114775 ** two forms, and only the specified schema is optimized in the latter two.
114776 **
114777 ** The details of optimizations performed by this pragma are expected
114778 ** to change and improve over time. Applications should anticipate that
114779 ** this pragma will perform new optimizations in future releases.
114780 **
114781 ** The optional argument is a bitmask of optimizations to perform:
114782 **
@@ -114780,14 +114793,14 @@
114793 ** pragmas run by future database connections.
114794 **
114795 ** 0x0008 (Not yet implemented) Create indexes that might have
114796 ** been helpful to recent queries
114797 **
114798 ** The default MASK is and always shall be 0xfffe. 0xfffe means perform all ** of the optimizations listed above except Debug Mode, including new
114799 ** optimizations that have not yet been invented. If new optimizations are
114800 ** ever added that should be off by default, those off-by-default
114801 ** optimizations will have bitmasks of 0x10000 or larger.
114802 **
114803 ** DETERMINATION OF WHEN TO RUN ANALYZE
114804 **
114805 ** In the current implementation, a table is analyzed if only if all of
114806 ** the following are true:
@@ -114818,11 +114831,11 @@
114831
114832 if( zRight ){
114833 opMask = (u32)sqlite3Atoi(zRight);
114834 if( (opMask & 0x02)==0 ) break;
114835 }else{
114836 opMask = 0xfffe;
114837 }
114838 iTabCur = pParse->nTab++;
114839 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
114840 if( iDb==1 ) continue;
114841 sqlite3CodeVerifySchema(pParse, iDb);
@@ -137673,11 +137686,11 @@
137686 case 158: /* term ::= INTEGER */
137687 {
137688 yylhsminor.yy190.pExpr = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &yymsp[0].minor.yy0, 1);
137689 yylhsminor.yy190.zStart = yymsp[0].minor.yy0.z;
137690 yylhsminor.yy190.zEnd = yymsp[0].minor.yy0.z + yymsp[0].minor.yy0.n;
137691 if( yylhsminor.yy190.pExpr ) yylhsminor.yy190.pExpr->flags |= EP_Leaf|EP_Resolved;
137692 }
137693 yymsp[0].minor.yy190 = yylhsminor.yy190;
137694 break;
137695 case 159: /* expr ::= VARIABLE */
137696 {
@@ -139295,12 +139308,12 @@
139308 ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
139309 ** error message.
139310 */
139311 SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
139312 int nErr = 0; /* Number of errors encountered */
 
139313 void *pEngine; /* The LEMON-generated LALR(1) parser */
139314 int n = 0; /* Length of the next token token */
139315 int tokenType; /* type of the next token */
139316 int lastTokenParsed = -1; /* type of the previous token */
139317 sqlite3 *db = pParse->db; /* The database connection */
139318 int mxSqlLen; /* Max length of an SQL string */
139319 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
@@ -139312,11 +139325,10 @@
139325 if( db->nVdbeActive==0 ){
139326 db->u1.isInterrupted = 0;
139327 }
139328 pParse->rc = SQLITE_OK;
139329 pParse->zTail = zSql;
 
139330 assert( pzErrMsg!=0 );
139331 /* sqlite3ParserTrace(stdout, "parser: "); */
139332 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
139333 pEngine = zSpace;
139334 sqlite3ParserInit(pEngine);
@@ -139330,16 +139342,14 @@
139342 assert( pParse->pNewTable==0 );
139343 assert( pParse->pNewTrigger==0 );
139344 assert( pParse->nVar==0 );
139345 assert( pParse->pVList==0 );
139346 while( 1 ){
139347 if( zSql[0]!=0 ){
139348 n = sqlite3GetToken((u8*)zSql, &tokenType);
139349 mxSqlLen -= n;
139350 if( mxSqlLen<0 ){
 
 
139351 pParse->rc = SQLITE_TOOBIG;
139352 break;
139353 }
139354 }else{
139355 /* Upon reaching the end of input, call the parser two more times
@@ -139349,30 +139359,34 @@
139359 }else if( lastTokenParsed==0 ){
139360 break;
139361 }else{
139362 tokenType = TK_SEMI;
139363 }
139364 zSql -= n;
139365 }
139366 if( tokenType>=TK_SPACE ){
139367 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
139368 if( db->u1.isInterrupted ){
139369 pParse->rc = SQLITE_INTERRUPT;
139370 break;
139371 }
139372 if( tokenType==TK_ILLEGAL ){
139373 sqlite3ErrorMsg(pParse, "unrecognized token: \"%.*s\"", n, zSql);
 
139374 break;
139375 }
139376 zSql += n;
139377 }else{
139378 pParse->sLastToken.z = zSql;
139379 pParse->sLastToken.n = n;
139380 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
139381 lastTokenParsed = tokenType;
139382 zSql += n;
139383 if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break;
139384 }
139385 }
139386 assert( nErr==0 );
139387 pParse->zTail = zSql;
139388 #ifdef YYTRACKMAXSTACKDEPTH
139389 sqlite3_mutex_enter(sqlite3MallocMutex());
139390 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
139391 sqlite3ParserStackPeak(pEngine)
139392 );
@@ -198053,11 +198067,11 @@
198067 int nArg, /* Number of args */
198068 sqlite3_value **apUnused /* Function arguments */
198069 ){
198070 assert( nArg==0 );
198071 UNUSED_PARAM2(nArg, apUnused);
198072 sqlite3_result_text(pCtx, "fts5: 2017-03-10 17:03:11 f8560c60d10c0365b33342ab05b5a953987b0471", -1, SQLITE_TRANSIENT);
198073 }
198074
198075 static int fts5Init(sqlite3 *db){
198076 static const sqlite3_module fts5Mod = {
198077 /* iVersion */ 2,
198078
+1 -1
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -121,11 +121,11 @@
121121
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
122122
** [sqlite_version()] and [sqlite_source_id()].
123123
*/
124124
#define SQLITE_VERSION "3.18.0"
125125
#define SQLITE_VERSION_NUMBER 3018000
126
-#define SQLITE_SOURCE_ID "2017-03-06 17:33:58 137aeb2b160888100bc1e871b00860149e5f6196"
126
+#define SQLITE_SOURCE_ID "2017-03-10 17:03:11 f8560c60d10c0365b33342ab05b5a953987b0471"
127127
128128
/*
129129
** CAPI3REF: Run-Time Library Version Numbers
130130
** KEYWORDS: sqlite3_version sqlite3_sourceid
131131
**
132132
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -121,11 +121,11 @@
121 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
122 ** [sqlite_version()] and [sqlite_source_id()].
123 */
124 #define SQLITE_VERSION "3.18.0"
125 #define SQLITE_VERSION_NUMBER 3018000
126 #define SQLITE_SOURCE_ID "2017-03-06 17:33:58 137aeb2b160888100bc1e871b00860149e5f6196"
127
128 /*
129 ** CAPI3REF: Run-Time Library Version Numbers
130 ** KEYWORDS: sqlite3_version sqlite3_sourceid
131 **
132
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -121,11 +121,11 @@
121 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
122 ** [sqlite_version()] and [sqlite_source_id()].
123 */
124 #define SQLITE_VERSION "3.18.0"
125 #define SQLITE_VERSION_NUMBER 3018000
126 #define SQLITE_SOURCE_ID "2017-03-10 17:03:11 f8560c60d10c0365b33342ab05b5a953987b0471"
127
128 /*
129 ** CAPI3REF: Run-Time Library Version Numbers
130 ** KEYWORDS: sqlite3_version sqlite3_sourceid
131 **
132

Keyboard Shortcuts

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