| | @@ -452,32 +452,10 @@ |
| 452 | 452 | while( IsDigit(*z) ){ z++; } |
| 453 | 453 | if( realnum ) *realnum = 1; |
| 454 | 454 | } |
| 455 | 455 | return *z==0; |
| 456 | 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 | 457 | |
| 480 | 458 | /* |
| 481 | 459 | ** Compute a string length that is limited to what can be stored in |
| 482 | 460 | ** lower 30 bits of a 32-bit signed integer. |
| 483 | 461 | */ |
| | @@ -575,10 +553,783 @@ |
| 575 | 553 | if( zResult && *zResult ) shell_add_history(zResult); |
| 576 | 554 | #endif |
| 577 | 555 | } |
| 578 | 556 | return zResult; |
| 579 | 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 | +********************************************************************************/ |
| 580 | 1331 | |
| 581 | 1332 | #if defined(SQLITE_ENABLE_SESSION) |
| 582 | 1333 | /* |
| 583 | 1334 | ** State information for a single open session |
| 584 | 1335 | */ |
| | @@ -608,17 +1359,14 @@ |
| 608 | 1359 | ** instance of the following structure. |
| 609 | 1360 | */ |
| 610 | 1361 | typedef struct ShellState ShellState; |
| 611 | 1362 | struct ShellState { |
| 612 | 1363 | sqlite3 *db; /* The database */ |
| 613 | | - int echoOn; /* True to echo input commands */ |
| 614 | 1364 | int autoExplain; /* Automatically turn on .explain mode */ |
| 615 | 1365 | int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ |
| 616 | 1366 | int statsOn; /* True to display memory stats before each finalize */ |
| 617 | 1367 | 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 | 1368 | int outCount; /* Revert to stdout when reaching zero */ |
| 621 | 1369 | int cnt; /* Number of records displayed so far */ |
| 622 | 1370 | FILE *out; /* Write results here */ |
| 623 | 1371 | FILE *traceOut; /* Output for sqlite3_trace() */ |
| 624 | 1372 | int nErr; /* Number of errors seen */ |
| | @@ -653,13 +1401,24 @@ |
| 653 | 1401 | }; |
| 654 | 1402 | |
| 655 | 1403 | /* |
| 656 | 1404 | ** These are the allowed shellFlgs values |
| 657 | 1405 | */ |
| 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))) |
| 661 | 1420 | |
| 662 | 1421 | /* |
| 663 | 1422 | ** These are the allowed modes. |
| 664 | 1423 | */ |
| 665 | 1424 | #define MODE_Line 0 /* One column per line. Blank line between records */ |
| | @@ -1292,92 +2051,106 @@ |
| 1292 | 2051 | */ |
| 1293 | 2052 | static int callback(void *pArg, int nArg, char **azArg, char **azCol){ |
| 1294 | 2053 | /* since we don't have type info, call the shell_callback with a NULL value */ |
| 1295 | 2054 | return shell_callback(pArg, nArg, azArg, azCol, NULL); |
| 1296 | 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 | + |
| 1297 | 2120 | |
| 1298 | 2121 | /* |
| 1299 | 2122 | ** Set the destination table field of the ShellState structure to |
| 1300 | 2123 | ** the name of the table given. Escape any quote characters in the |
| 1301 | 2124 | ** table name. |
| 1302 | 2125 | */ |
| 1303 | 2126 | static void set_table_name(ShellState *p, const char *zName){ |
| 1304 | 2127 | int i, n; |
| 1305 | | - int needQuote; |
| 2128 | + int cQuote; |
| 1306 | 2129 | char *z; |
| 1307 | 2130 | |
| 1308 | 2131 | if( p->zDestTable ){ |
| 1309 | 2132 | free(p->zDestTable); |
| 1310 | 2133 | p->zDestTable = 0; |
| 1311 | 2134 | } |
| 1312 | 2135 | 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; |
| 1321 | 2139 | z = p->zDestTable = malloc( n+1 ); |
| 1322 | 2140 | if( z==0 ){ |
| 1323 | 2141 | raw_printf(stderr,"Error: out of memory\n"); |
| 1324 | 2142 | exit(1); |
| 1325 | 2143 | } |
| 1326 | 2144 | n = 0; |
| 1327 | | - if( needQuote ) z[n++] = '\''; |
| 2145 | + if( cQuote ) z[n++] = cQuote; |
| 1328 | 2146 | for(i=0; zName[i]; i++){ |
| 1329 | 2147 | 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; |
| 1379 | 2152 | } |
| 1380 | 2153 | |
| 1381 | 2154 | |
| 1382 | 2155 | /* |
| 1383 | 2156 | ** Execute a query statement that will generate SQL output. Print |
| | @@ -1484,10 +2257,35 @@ |
| 1484 | 2257 | } |
| 1485 | 2258 | fclose(in); |
| 1486 | 2259 | } |
| 1487 | 2260 | #endif |
| 1488 | 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 | +} |
| 1489 | 2287 | |
| 1490 | 2288 | /* |
| 1491 | 2289 | ** Display memory stats. |
| 1492 | 2290 | */ |
| 1493 | 2291 | static int display_stats( |
| | @@ -1497,61 +2295,35 @@ |
| 1497 | 2295 | ){ |
| 1498 | 2296 | int iCur; |
| 1499 | 2297 | int iHiwtr; |
| 1500 | 2298 | |
| 1501 | 2299 | 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); |
| 1512 | 2304 | 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); |
| 1524 | 2310 | 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); |
| 1548 | 2322 | #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); |
| 1553 | 2325 | #endif |
| 1554 | 2326 | } |
| 1555 | 2327 | |
| 1556 | 2328 | if( pArg && pArg->out && db ){ |
| 1557 | 2329 | if( pArg->shellFlgs & SHFLG_Lookaside ){ |
| | @@ -1934,11 +2706,11 @@ |
| 1934 | 2706 | pArg->pStmt = pStmt; |
| 1935 | 2707 | pArg->cnt = 0; |
| 1936 | 2708 | } |
| 1937 | 2709 | |
| 1938 | 2710 | /* echo the sql statement if echo on */ |
| 1939 | | - if( pArg && pArg->echoOn ){ |
| 2711 | + if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){ |
| 1940 | 2712 | utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); |
| 1941 | 2713 | } |
| 1942 | 2714 | |
| 1943 | 2715 | /* Show the EXPLAIN QUERY PLAN if .eqp is on */ |
| 1944 | 2716 | if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){ |
| | @@ -2022,10 +2794,142 @@ |
| 2022 | 2794 | } /* end while */ |
| 2023 | 2795 | |
| 2024 | 2796 | return rc; |
| 2025 | 2797 | } |
| 2026 | 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 | +} |
| 2027 | 2931 | |
| 2028 | 2932 | /* |
| 2029 | 2933 | ** This is a different callback routine used for dumping the database. |
| 2030 | 2934 | ** Each row received by this callback consists of a table name, |
| 2031 | 2935 | ** the table type ("index" or "table") and SQL to create the table. |
| | @@ -2034,21 +2938,20 @@ |
| 2034 | 2938 | static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){ |
| 2035 | 2939 | int rc; |
| 2036 | 2940 | const char *zTable; |
| 2037 | 2941 | const char *zType; |
| 2038 | 2942 | const char *zSql; |
| 2039 | | - const char *zPrepStmt = 0; |
| 2040 | 2943 | ShellState *p = (ShellState *)pArg; |
| 2041 | 2944 | |
| 2042 | 2945 | UNUSED_PARAMETER(azCol); |
| 2043 | 2946 | if( nArg!=3 ) return 1; |
| 2044 | 2947 | zTable = azArg[0]; |
| 2045 | 2948 | zType = azArg[1]; |
| 2046 | 2949 | zSql = azArg[2]; |
| 2047 | 2950 | |
| 2048 | 2951 | if( strcmp(zTable, "sqlite_sequence")==0 ){ |
| 2049 | | - zPrepStmt = "DELETE FROM sqlite_sequence;\n"; |
| 2952 | + raw_printf(p->out, "DELETE FROM sqlite_sequence;\n"); |
| 2050 | 2953 | }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){ |
| 2051 | 2954 | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
| 2052 | 2955 | }else if( strncmp(zTable, "sqlite_", 7)==0 ){ |
| 2053 | 2956 | return 0; |
| 2054 | 2957 | }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ |
| | @@ -2067,62 +2970,74 @@ |
| 2067 | 2970 | }else{ |
| 2068 | 2971 | printSchemaLine(p->out, zSql, ";\n"); |
| 2069 | 2972 | } |
| 2070 | 2973 | |
| 2071 | 2974 | 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 | + |
| 2089 | 2988 | /* Always quote the table name, even if it appears to be pure ascii, |
| 2090 | 2989 | ** 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++; |
| 2124 | 3039 | } |
| 2125 | 3040 | return 0; |
| 2126 | 3041 | } |
| 2127 | 3042 | |
| 2128 | 3043 | /* |
| | @@ -2233,10 +3148,11 @@ |
| 2233 | 3148 | ".separator COL ?ROW? Change the column separator and optionally the row\n" |
| 2234 | 3149 | " separator for both the output mode and .import\n" |
| 2235 | 3150 | #if defined(SQLITE_ENABLE_SESSION) |
| 2236 | 3151 | ".session CMD ... Create or control sessions\n" |
| 2237 | 3152 | #endif |
| 3153 | + ".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n" |
| 2238 | 3154 | ".shell CMD ARGS... Run CMD ARGS... in a system shell\n" |
| 2239 | 3155 | ".show Show the current values for various settings\n" |
| 2240 | 3156 | ".stats ?on|off? Show stats or turn stats on or off\n" |
| 2241 | 3157 | ".system CMD ARGS... Run CMD ARGS... in a system shell\n" |
| 2242 | 3158 | ".tables ?TABLE? List names of tables\n" |
| | @@ -2423,14 +3339,10 @@ |
| 2423 | 3339 | static void open_db(ShellState *p, int keepAlive){ |
| 2424 | 3340 | if( p->db==0 ){ |
| 2425 | 3341 | sqlite3_initialize(); |
| 2426 | 3342 | sqlite3_open(p->zDbFilename, &p->db); |
| 2427 | 3343 | 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 | 3344 | if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ |
| 2433 | 3345 | utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", |
| 2434 | 3346 | p->zDbFilename, sqlite3_errmsg(p->db)); |
| 2435 | 3347 | if( keepAlive ) return; |
| 2436 | 3348 | exit(1); |
| | @@ -2440,10 +3352,18 @@ |
| 2440 | 3352 | #endif |
| 2441 | 3353 | sqlite3_create_function(p->db, "readfile", 1, SQLITE_UTF8, 0, |
| 2442 | 3354 | readfileFunc, 0, 0); |
| 2443 | 3355 | sqlite3_create_function(p->db, "writefile", 2, SQLITE_UTF8, 0, |
| 2444 | 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); |
| 2445 | 3365 | } |
| 2446 | 3366 | } |
| 2447 | 3367 | |
| 2448 | 3368 | /* |
| 2449 | 3369 | ** Do C-language style dequoting. |
| | @@ -2564,11 +3484,11 @@ |
| 2564 | 3484 | |
| 2565 | 3485 | /* |
| 2566 | 3486 | ** Interpret zArg as either an integer or a boolean value. Return 1 or 0 |
| 2567 | 3487 | ** for TRUE and FALSE. Return the integer value if appropriate. |
| 2568 | 3488 | */ |
| 2569 | | -static int booleanValue(char *zArg){ |
| 3489 | +static int booleanValue(const char *zArg){ |
| 2570 | 3490 | int i; |
| 2571 | 3491 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 2572 | 3492 | for(i=2; hexDigitValue(zArg[i])>=0; i++){} |
| 2573 | 3493 | }else{ |
| 2574 | 3494 | for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} |
| | @@ -2582,10 +3502,21 @@ |
| 2582 | 3502 | } |
| 2583 | 3503 | utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", |
| 2584 | 3504 | zArg); |
| 2585 | 3505 | return 0; |
| 2586 | 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 | +} |
| 2587 | 3518 | |
| 2588 | 3519 | /* |
| 2589 | 3520 | ** Close an output file, assuming it is not stderr or stdout |
| 2590 | 3521 | */ |
| 2591 | 3522 | static void output_file_close(FILE *f){ |
| | @@ -3656,11 +4587,11 @@ |
| 3656 | 4587 | test_breakpoint(); |
| 3657 | 4588 | }else |
| 3658 | 4589 | |
| 3659 | 4590 | if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){ |
| 3660 | 4591 | if( nArg==2 ){ |
| 3661 | | - p->countChanges = booleanValue(azArg[1]); |
| 4592 | + setOrClearFlag(p, SHFLG_CountChanges, azArg[1]); |
| 3662 | 4593 | }else{ |
| 3663 | 4594 | raw_printf(stderr, "Usage: .changes on|off\n"); |
| 3664 | 4595 | rc = 1; |
| 3665 | 4596 | } |
| 3666 | 4597 | }else |
| | @@ -3720,25 +4651,46 @@ |
| 3720 | 4651 | if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){ |
| 3721 | 4652 | rc = shell_dbinfo_command(p, nArg, azArg); |
| 3722 | 4653 | }else |
| 3723 | 4654 | |
| 3724 | 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 | + } |
| 3725 | 4679 | open_db(p, 0); |
| 3726 | 4680 | /* When playing back a "dump", the content might appear in an order |
| 3727 | 4681 | ** which causes immediate foreign key constraints to be violated. |
| 3728 | 4682 | ** 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 | 4683 | raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); |
| 3735 | 4684 | raw_printf(p->out, "BEGIN TRANSACTION;\n"); |
| 3736 | 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. */ |
| 3737 | 4689 | sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); |
| 3738 | 4690 | p->nErr = 0; |
| 3739 | | - if( nArg==1 ){ |
| 4691 | + if( zLike==0 ){ |
| 3740 | 4692 | run_schema_dump_query(p, |
| 3741 | 4693 | "SELECT name, type, sql FROM sqlite_master " |
| 3742 | 4694 | "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'" |
| 3743 | 4695 | ); |
| 3744 | 4696 | run_schema_dump_query(p, |
| | @@ -3748,25 +4700,24 @@ |
| 3748 | 4700 | run_table_dump_query(p, |
| 3749 | 4701 | "SELECT sql FROM sqlite_master " |
| 3750 | 4702 | "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0 |
| 3751 | 4703 | ); |
| 3752 | 4704 | }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); |
| 3768 | 4719 | } |
| 3769 | 4720 | if( p->writableSchema ){ |
| 3770 | 4721 | raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); |
| 3771 | 4722 | p->writableSchema = 0; |
| 3772 | 4723 | } |
| | @@ -3775,11 +4726,11 @@ |
| 3775 | 4726 | raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n"); |
| 3776 | 4727 | }else |
| 3777 | 4728 | |
| 3778 | 4729 | if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){ |
| 3779 | 4730 | if( nArg==2 ){ |
| 3780 | | - p->echoOn = booleanValue(azArg[1]); |
| 4731 | + setOrClearFlag(p, SHFLG_Echo, azArg[1]); |
| 3781 | 4732 | }else{ |
| 3782 | 4733 | raw_printf(stderr, "Usage: .echo on|off\n"); |
| 3783 | 4734 | rc = 1; |
| 3784 | 4735 | } |
| 3785 | 4736 | }else |
| | @@ -4572,21 +5523,21 @@ |
| 4572 | 5523 | new_colv[0] = "sql"; |
| 4573 | 5524 | new_colv[1] = 0; |
| 4574 | 5525 | callback(&data, 1, new_argv, new_colv); |
| 4575 | 5526 | rc = SQLITE_OK; |
| 4576 | 5527 | }else{ |
| 4577 | | - zShellStatic = azArg[1]; |
| 4578 | | - rc = sqlite3_exec(p->db, |
| 5528 | + char *zSql; |
| 5529 | + zSql = sqlite3_mprintf( |
| 4579 | 5530 | "SELECT sql FROM " |
| 4580 | 5531 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
| 4581 | 5532 | " FROM sqlite_master UNION ALL" |
| 4582 | 5533 | " 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" |
| 4584 | 5535 | " 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); |
| 4588 | 5539 | } |
| 4589 | 5540 | }else if( nArg==1 ){ |
| 4590 | 5541 | rc = sqlite3_exec(p->db, |
| 4591 | 5542 | "SELECT sql FROM " |
| 4592 | 5543 | " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
| | @@ -4835,10 +5786,123 @@ |
| 4835 | 5786 | utf8_printf(p->out, "%s", zBuf); |
| 4836 | 5787 | } |
| 4837 | 5788 | } |
| 4838 | 5789 | }else |
| 4839 | 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 |
| 4840 | 5904 | |
| 4841 | 5905 | if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){ |
| 4842 | 5906 | if( nArg<2 || nArg>3 ){ |
| 4843 | 5907 | raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); |
| 4844 | 5908 | rc = 1; |
| | @@ -4850,10 +5914,126 @@ |
| 4850 | 5914 | if( nArg>=3 ){ |
| 4851 | 5915 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, |
| 4852 | 5916 | "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); |
| 4853 | 5917 | } |
| 4854 | 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 |
| 4855 | 6035 | |
| 4856 | 6036 | if( c=='s' |
| 4857 | 6037 | && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) |
| 4858 | 6038 | ){ |
| 4859 | 6039 | char *zCmd; |
| | @@ -4879,11 +6059,12 @@ |
| 4879 | 6059 | if( nArg!=1 ){ |
| 4880 | 6060 | raw_printf(stderr, "Usage: .show\n"); |
| 4881 | 6061 | rc = 1; |
| 4882 | 6062 | goto meta_command_exit; |
| 4883 | 6063 | } |
| 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)]); |
| 4885 | 6066 | utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); |
| 4886 | 6067 | utf8_printf(p->out, "%12.12s: %s\n","explain", |
| 4887 | 6068 | p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); |
| 4888 | 6069 | utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); |
| 4889 | 6070 | utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); |
| | @@ -5444,11 +6625,11 @@ |
| 5444 | 6625 | static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ |
| 5445 | 6626 | int rc; |
| 5446 | 6627 | char *zErrMsg = 0; |
| 5447 | 6628 | |
| 5448 | 6629 | open_db(p, 0); |
| 5449 | | - if( p->backslashOn ) resolve_backslashes(zSql); |
| 6630 | + if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql); |
| 5450 | 6631 | BEGIN_TIMER; |
| 5451 | 6632 | rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg); |
| 5452 | 6633 | END_TIMER; |
| 5453 | 6634 | if( rc || zErrMsg ){ |
| 5454 | 6635 | char zPrefix[100]; |
| | @@ -5464,11 +6645,11 @@ |
| 5464 | 6645 | zErrMsg = 0; |
| 5465 | 6646 | }else{ |
| 5466 | 6647 | utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); |
| 5467 | 6648 | } |
| 5468 | 6649 | return 1; |
| 5469 | | - }else if( p->countChanges ){ |
| 6650 | + }else if( ShellHasFlag(p, SHFLG_CountChanges) ){ |
| 5470 | 6651 | raw_printf(p->out, "changes: %3d total_changes: %d\n", |
| 5471 | 6652 | sqlite3_changes(p->db), sqlite3_total_changes(p->db)); |
| 5472 | 6653 | } |
| 5473 | 6654 | return 0; |
| 5474 | 6655 | } |
| | @@ -5507,15 +6688,15 @@ |
| 5507 | 6688 | if( in!=0 ) break; |
| 5508 | 6689 | seenInterrupt = 0; |
| 5509 | 6690 | } |
| 5510 | 6691 | lineno++; |
| 5511 | 6692 | if( nSql==0 && _all_whitespace(zLine) ){ |
| 5512 | | - if( p->echoOn ) printf("%s\n", zLine); |
| 6693 | + if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); |
| 5513 | 6694 | continue; |
| 5514 | 6695 | } |
| 5515 | 6696 | if( zLine && zLine[0]=='.' && nSql==0 ){ |
| 5516 | | - if( p->echoOn ) printf("%s\n", zLine); |
| 6697 | + if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); |
| 5517 | 6698 | rc = do_meta_command(zLine, p); |
| 5518 | 6699 | if( rc==2 ){ /* exit requested */ |
| 5519 | 6700 | break; |
| 5520 | 6701 | }else if( rc ){ |
| 5521 | 6702 | errCnt++; |
| | @@ -5554,11 +6735,11 @@ |
| 5554 | 6735 | if( p->outCount ){ |
| 5555 | 6736 | output_reset(p); |
| 5556 | 6737 | p->outCount = 0; |
| 5557 | 6738 | } |
| 5558 | 6739 | }else if( nSql && _all_whitespace(zSql) ){ |
| 5559 | | - if( p->echoOn ) printf("%s\n", zSql); |
| 6740 | + if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql); |
| 5560 | 6741 | nSql = 0; |
| 5561 | 6742 | } |
| 5562 | 6743 | } |
| 5563 | 6744 | if( nSql && !_all_whitespace(zSql) ){ |
| 5564 | 6745 | runOneSqlLine(p, zSql, in, startline); |
| | @@ -6023,11 +7204,11 @@ |
| 6023 | 7204 | }else if( strcmp(z,"-header")==0 ){ |
| 6024 | 7205 | data.showHeader = 1; |
| 6025 | 7206 | }else if( strcmp(z,"-noheader")==0 ){ |
| 6026 | 7207 | data.showHeader = 0; |
| 6027 | 7208 | }else if( strcmp(z,"-echo")==0 ){ |
| 6028 | | - data.echoOn = 1; |
| 7209 | + ShellSetFlag(&data, SHFLG_Echo); |
| 6029 | 7210 | }else if( strcmp(z,"-eqp")==0 ){ |
| 6030 | 7211 | data.autoEQP = 1; |
| 6031 | 7212 | }else if( strcmp(z,"-eqpfull")==0 ){ |
| 6032 | 7213 | data.autoEQP = 2; |
| 6033 | 7214 | }else if( strcmp(z,"-stats")==0 ){ |
| | @@ -6038,11 +7219,11 @@ |
| 6038 | 7219 | /* Undocumented command-line option: -backslash |
| 6039 | 7220 | ** Causes C-style backslash escapes to be evaluated in SQL statements |
| 6040 | 7221 | ** prior to sending the SQL into SQLite. Useful for injecting |
| 6041 | 7222 | ** crazy bytes in the middle of SQL statements for testing and debugging. |
| 6042 | 7223 | */ |
| 6043 | | - data.backslashOn = 1; |
| 7224 | + ShellSetFlag(&data, SHFLG_Backslash); |
| 6044 | 7225 | }else if( strcmp(z,"-bail")==0 ){ |
| 6045 | 7226 | bail_on_error = 1; |
| 6046 | 7227 | }else if( strcmp(z,"-version")==0 ){ |
| 6047 | 7228 | printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid()); |
| 6048 | 7229 | return 0; |
| 6049 | 7230 | |