| | @@ -1,5 +1,23 @@ |
| 1 | +/* DO NOT EDIT! |
| 2 | +** This file is automatically generated by the script in the canonical |
| 3 | +** SQLite source tree at tool/mkshellc.tcl. That script combines source |
| 4 | +** code from various constituent source files of SQLite into this single |
| 5 | +** "shell.c" file used to implement the SQLite command-line shell. |
| 6 | +** |
| 7 | +** Most of the code found below comes from the "src/shell.c.in" file in |
| 8 | +** the canonical SQLite source tree. That main file contains "INCLUDE" |
| 9 | +** lines that specify other files in the canonical source tree that are |
| 10 | +** inserted to getnerate this complete program source file. |
| 11 | +** |
| 12 | +** The code from multiple files is combined into this single "shell.c" |
| 13 | +** source file to help make the command-line program easier to compile. |
| 14 | +** |
| 15 | +** To modify this program, get a copy of the canonical SQLite source tree, |
| 16 | +** edit the src/shell.c.in" and/or some of the other files that are included |
| 17 | +** by "src/shell.c.in", then rerun the tool/mkshellc.tcl script. |
| 18 | +*/ |
| 1 | 19 | /* |
| 2 | 20 | ** 2001 September 15 |
| 3 | 21 | ** |
| 4 | 22 | ** The author disclaims copyright to this source code. In place of |
| 5 | 23 | ** a legal notice, here is a blessing: |
| | @@ -782,14 +800,57 @@ |
| 782 | 800 | } |
| 783 | 801 | } |
| 784 | 802 | sqlite3_result_value(pCtx, apVal[0]); |
| 785 | 803 | } |
| 786 | 804 | |
| 787 | | -/****************************************************************************** |
| 788 | | -** SHA3 hash implementation copied from ../ext/misc/shathree.c |
| 805 | +/* |
| 806 | +** The source code for several run-time loadable extensions is inserted |
| 807 | +** below by the ../tool/mkshellc.tcl script. Before processing that included |
| 808 | +** code, we need to override some macros to make the included program code |
| 809 | +** work here in the middle of this regular program. |
| 789 | 810 | */ |
| 811 | +#define SQLITE_EXTENSION_INIT1 |
| 812 | +#define SQLITE_EXTENSION_INIT2(X) |
| 813 | + |
| 814 | +/************************* Begin ../ext/misc/shathree.c ******************/ |
| 815 | +/* |
| 816 | +** 2017-03-08 |
| 817 | +** |
| 818 | +** The author disclaims copyright to this source code. In place of |
| 819 | +** a legal notice, here is a blessing: |
| 820 | +** |
| 821 | +** May you do good and not evil. |
| 822 | +** May you find forgiveness for yourself and forgive others. |
| 823 | +** May you share freely, never taking more than you give. |
| 824 | +** |
| 825 | +****************************************************************************** |
| 826 | +** |
| 827 | +** This SQLite extension implements a functions that compute SHA1 hashes. |
| 828 | +** Two SQL functions are implemented: |
| 829 | +** |
| 830 | +** sha3(X,SIZE) |
| 831 | +** sha3_query(Y,SIZE) |
| 832 | +** |
| 833 | +** The sha3(X) function computes the SHA3 hash of the input X, or NULL if |
| 834 | +** X is NULL. |
| 835 | +** |
| 836 | +** The sha3_query(Y) function evalutes all queries in the SQL statements of Y |
| 837 | +** and returns a hash of their results. |
| 838 | +** |
| 839 | +** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm |
| 840 | +** is used. If SIZE is included it must be one of the integers 224, 256, |
| 841 | +** 384, or 512, to determine SHA3 hash variant that is computed. |
| 842 | +*/ |
| 843 | +SQLITE_EXTENSION_INIT1 |
| 844 | +#include <assert.h> |
| 845 | +#include <string.h> |
| 846 | +#include <stdarg.h> |
| 790 | 847 | typedef sqlite3_uint64 u64; |
| 848 | + |
| 849 | +/****************************************************************************** |
| 850 | +** The Hash Engine |
| 851 | +*/ |
| 791 | 852 | /* |
| 792 | 853 | ** Macros to determine whether the machine is big or little endian, |
| 793 | 854 | ** and whether or not that determination is run-time or compile-time. |
| 794 | 855 | ** |
| 795 | 856 | ** For best performance, an attempt is made to guess at the byte-order |
| | @@ -823,14 +884,10 @@ |
| 823 | 884 | unsigned nRate; /* Bytes of input accepted per Keccak iteration */ |
| 824 | 885 | unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */ |
| 825 | 886 | unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */ |
| 826 | 887 | }; |
| 827 | 888 | |
| 828 | | -/* Allow the following routine to use the B0 variable, which is also |
| 829 | | -** a macro in the termios.h header file */ |
| 830 | | -#undef B0 |
| 831 | | - |
| 832 | 889 | /* |
| 833 | 890 | ** A single step of the Keccak mixing function for a 1600-bit state |
| 834 | 891 | */ |
| 835 | 892 | static void KeccakF1600Step(SHA3Context *p){ |
| 836 | 893 | int i; |
| | @@ -1237,16 +1294,18 @@ |
| 1237 | 1294 | for(i=0; i<p->nRate; i++){ |
| 1238 | 1295 | p->u.x[i+p->nRate] = p->u.x[i^p->ixMask]; |
| 1239 | 1296 | } |
| 1240 | 1297 | return &p->u.x[p->nRate]; |
| 1241 | 1298 | } |
| 1299 | +/* End of the hashing logic |
| 1300 | +*****************************************************************************/ |
| 1242 | 1301 | |
| 1243 | 1302 | /* |
| 1244 | 1303 | ** Implementation of the sha3(X,SIZE) function. |
| 1245 | 1304 | ** |
| 1246 | 1305 | ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default |
| 1247 | | -** size is 256. If X is a BLOB, it is hashed as is. |
| 1306 | +** size is 256. If X is a BLOB, it is hashed as is. |
| 1248 | 1307 | ** For all other non-NULL types of input, X is converted into a UTF-8 string |
| 1249 | 1308 | ** and the string is hashed without the trailing 0x00 terminator. The hash |
| 1250 | 1309 | ** of a NULL value is NULL. |
| 1251 | 1310 | */ |
| 1252 | 1311 | static void sha3Func( |
| | @@ -1373,14 +1432,10 @@ |
| 1373 | 1432 | sqlite3_free(zMsg); |
| 1374 | 1433 | return; |
| 1375 | 1434 | } |
| 1376 | 1435 | nCol = sqlite3_column_count(pStmt); |
| 1377 | 1436 | z = sqlite3_sql(pStmt); |
| 1378 | | - if( z==0 ){ |
| 1379 | | - sqlite3_finalize(pStmt); |
| 1380 | | - continue; |
| 1381 | | - } |
| 1382 | 1437 | n = (int)strlen(z); |
| 1383 | 1438 | hash_step_vformat(&cx,"S%d:",n); |
| 1384 | 1439 | SHA3Update(&cx,(unsigned char*)z,n); |
| 1385 | 1440 | |
| 1386 | 1441 | /* Compute a hash over the result of the query */ |
| | @@ -1439,12 +1494,654 @@ |
| 1439 | 1494 | } |
| 1440 | 1495 | sqlite3_finalize(pStmt); |
| 1441 | 1496 | } |
| 1442 | 1497 | sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT); |
| 1443 | 1498 | } |
| 1444 | | -/* End of SHA3 hashing logic copy/pasted from ../ext/misc/shathree.c |
| 1445 | | -********************************************************************************/ |
| 1499 | + |
| 1500 | + |
| 1501 | +#ifdef _WIN32 |
| 1502 | +__declspec(dllexport) |
| 1503 | +#endif |
| 1504 | +int sqlite3_shathree_init( |
| 1505 | + sqlite3 *db, |
| 1506 | + char **pzErrMsg, |
| 1507 | + const sqlite3_api_routines *pApi |
| 1508 | +){ |
| 1509 | + int rc = SQLITE_OK; |
| 1510 | + SQLITE_EXTENSION_INIT2(pApi); |
| 1511 | + (void)pzErrMsg; /* Unused parameter */ |
| 1512 | + rc = sqlite3_create_function(db, "sha3", 1, SQLITE_UTF8, 0, |
| 1513 | + sha3Func, 0, 0); |
| 1514 | + if( rc==SQLITE_OK ){ |
| 1515 | + rc = sqlite3_create_function(db, "sha3", 2, SQLITE_UTF8, 0, |
| 1516 | + sha3Func, 0, 0); |
| 1517 | + } |
| 1518 | + if( rc==SQLITE_OK ){ |
| 1519 | + rc = sqlite3_create_function(db, "sha3_query", 1, SQLITE_UTF8, 0, |
| 1520 | + sha3QueryFunc, 0, 0); |
| 1521 | + } |
| 1522 | + if( rc==SQLITE_OK ){ |
| 1523 | + rc = sqlite3_create_function(db, "sha3_query", 2, SQLITE_UTF8, 0, |
| 1524 | + sha3QueryFunc, 0, 0); |
| 1525 | + } |
| 1526 | + return rc; |
| 1527 | +} |
| 1528 | + |
| 1529 | +/************************* End ../ext/misc/shathree.c ********************/ |
| 1530 | +/************************* Begin ../ext/misc/fileio.c ******************/ |
| 1531 | +/* |
| 1532 | +** 2014-06-13 |
| 1533 | +** |
| 1534 | +** The author disclaims copyright to this source code. In place of |
| 1535 | +** a legal notice, here is a blessing: |
| 1536 | +** |
| 1537 | +** May you do good and not evil. |
| 1538 | +** May you find forgiveness for yourself and forgive others. |
| 1539 | +** May you share freely, never taking more than you give. |
| 1540 | +** |
| 1541 | +****************************************************************************** |
| 1542 | +** |
| 1543 | +** This SQLite extension implements SQL functions readfile() and |
| 1544 | +** writefile(). |
| 1545 | +*/ |
| 1546 | +SQLITE_EXTENSION_INIT1 |
| 1547 | +#include <stdio.h> |
| 1548 | + |
| 1549 | +/* |
| 1550 | +** Implementation of the "readfile(X)" SQL function. The entire content |
| 1551 | +** of the file named X is read and returned as a BLOB. NULL is returned |
| 1552 | +** if the file does not exist or is unreadable. |
| 1553 | +*/ |
| 1554 | +static void readfileFunc( |
| 1555 | + sqlite3_context *context, |
| 1556 | + int argc, |
| 1557 | + sqlite3_value **argv |
| 1558 | +){ |
| 1559 | + const char *zName; |
| 1560 | + FILE *in; |
| 1561 | + long nIn; |
| 1562 | + void *pBuf; |
| 1563 | + |
| 1564 | + zName = (const char*)sqlite3_value_text(argv[0]); |
| 1565 | + if( zName==0 ) return; |
| 1566 | + in = fopen(zName, "rb"); |
| 1567 | + if( in==0 ) return; |
| 1568 | + fseek(in, 0, SEEK_END); |
| 1569 | + nIn = ftell(in); |
| 1570 | + rewind(in); |
| 1571 | + pBuf = sqlite3_malloc( nIn ); |
| 1572 | + if( pBuf && 1==fread(pBuf, nIn, 1, in) ){ |
| 1573 | + sqlite3_result_blob(context, pBuf, nIn, sqlite3_free); |
| 1574 | + }else{ |
| 1575 | + sqlite3_free(pBuf); |
| 1576 | + } |
| 1577 | + fclose(in); |
| 1578 | +} |
| 1579 | + |
| 1580 | +/* |
| 1581 | +** Implementation of the "writefile(X,Y)" SQL function. The argument Y |
| 1582 | +** is written into file X. The number of bytes written is returned. Or |
| 1583 | +** NULL is returned if something goes wrong, such as being unable to open |
| 1584 | +** file X for writing. |
| 1585 | +*/ |
| 1586 | +static void writefileFunc( |
| 1587 | + sqlite3_context *context, |
| 1588 | + int argc, |
| 1589 | + sqlite3_value **argv |
| 1590 | +){ |
| 1591 | + FILE *out; |
| 1592 | + const char *z; |
| 1593 | + sqlite3_int64 rc; |
| 1594 | + const char *zFile; |
| 1595 | + |
| 1596 | + zFile = (const char*)sqlite3_value_text(argv[0]); |
| 1597 | + if( zFile==0 ) return; |
| 1598 | + out = fopen(zFile, "wb"); |
| 1599 | + if( out==0 ) return; |
| 1600 | + z = (const char*)sqlite3_value_blob(argv[1]); |
| 1601 | + if( z==0 ){ |
| 1602 | + rc = 0; |
| 1603 | + }else{ |
| 1604 | + rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out); |
| 1605 | + } |
| 1606 | + fclose(out); |
| 1607 | + sqlite3_result_int64(context, rc); |
| 1608 | +} |
| 1609 | + |
| 1610 | + |
| 1611 | +#ifdef _WIN32 |
| 1612 | +__declspec(dllexport) |
| 1613 | +#endif |
| 1614 | +int sqlite3_fileio_init( |
| 1615 | + sqlite3 *db, |
| 1616 | + char **pzErrMsg, |
| 1617 | + const sqlite3_api_routines *pApi |
| 1618 | +){ |
| 1619 | + int rc = SQLITE_OK; |
| 1620 | + SQLITE_EXTENSION_INIT2(pApi); |
| 1621 | + (void)pzErrMsg; /* Unused parameter */ |
| 1622 | + rc = sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0, |
| 1623 | + readfileFunc, 0, 0); |
| 1624 | + if( rc==SQLITE_OK ){ |
| 1625 | + rc = sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0, |
| 1626 | + writefileFunc, 0, 0); |
| 1627 | + } |
| 1628 | + return rc; |
| 1629 | +} |
| 1630 | + |
| 1631 | +/************************* End ../ext/misc/fileio.c ********************/ |
| 1632 | +/************************* Begin ../ext/misc/completion.c ******************/ |
| 1633 | +/* |
| 1634 | +** 2017-07-10 |
| 1635 | +** |
| 1636 | +** The author disclaims copyright to this source code. In place of |
| 1637 | +** a legal notice, here is a blessing: |
| 1638 | +** |
| 1639 | +** May you do good and not evil. |
| 1640 | +** May you find forgiveness for yourself and forgive others. |
| 1641 | +** May you share freely, never taking more than you give. |
| 1642 | +** |
| 1643 | +************************************************************************* |
| 1644 | +** |
| 1645 | +** This file implements an eponymous virtual table that returns suggested |
| 1646 | +** completions for a partial SQL input. |
| 1647 | +** |
| 1648 | +** Suggested usage: |
| 1649 | +** |
| 1650 | +** SELECT DISTINCT candidate COLLATE nocase |
| 1651 | +** FROM completion($prefix,$wholeline) |
| 1652 | +** ORDER BY 1; |
| 1653 | +** |
| 1654 | +** The two query parameters are optional. $prefix is the text of the |
| 1655 | +** current word being typed and that is to be completed. $wholeline is |
| 1656 | +** the complete input line, used for context. |
| 1657 | +** |
| 1658 | +** The raw completion() table might return the same candidate multiple |
| 1659 | +** times, for example if the same column name is used to two or more |
| 1660 | +** tables. And the candidates are returned in an arbitrary order. Hence, |
| 1661 | +** the DISTINCT and ORDER BY are recommended. |
| 1662 | +** |
| 1663 | +** This virtual table operates at the speed of human typing, and so there |
| 1664 | +** is no attempt to make it fast. Even a slow implementation will be much |
| 1665 | +** faster than any human can type. |
| 1666 | +** |
| 1667 | +*/ |
| 1668 | +SQLITE_EXTENSION_INIT1 |
| 1669 | +#include <assert.h> |
| 1670 | +#include <string.h> |
| 1671 | +#include <ctype.h> |
| 1672 | + |
| 1673 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1674 | + |
| 1675 | +/* completion_vtab is a subclass of sqlite3_vtab which will |
| 1676 | +** serve as the underlying representation of a completion virtual table |
| 1677 | +*/ |
| 1678 | +typedef struct completion_vtab completion_vtab; |
| 1679 | +struct completion_vtab { |
| 1680 | + sqlite3_vtab base; /* Base class - must be first */ |
| 1681 | + sqlite3 *db; /* Database connection for this completion vtab */ |
| 1682 | +}; |
| 1683 | + |
| 1684 | +/* completion_cursor is a subclass of sqlite3_vtab_cursor which will |
| 1685 | +** serve as the underlying representation of a cursor that scans |
| 1686 | +** over rows of the result |
| 1687 | +*/ |
| 1688 | +typedef struct completion_cursor completion_cursor; |
| 1689 | +struct completion_cursor { |
| 1690 | + sqlite3_vtab_cursor base; /* Base class - must be first */ |
| 1691 | + sqlite3 *db; /* Database connection for this cursor */ |
| 1692 | + int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */ |
| 1693 | + char *zPrefix; /* The prefix for the word we want to complete */ |
| 1694 | + char *zLine; /* The whole that we want to complete */ |
| 1695 | + const char *zCurrentRow; /* Current output row */ |
| 1696 | + sqlite3_stmt *pStmt; /* Current statement */ |
| 1697 | + sqlite3_int64 iRowid; /* The rowid */ |
| 1698 | + int ePhase; /* Current phase */ |
| 1699 | + int j; /* inter-phase counter */ |
| 1700 | +}; |
| 1701 | + |
| 1702 | +/* Values for ePhase: |
| 1703 | +*/ |
| 1704 | +#define COMPLETION_FIRST_PHASE 1 |
| 1705 | +#define COMPLETION_KEYWORDS 1 |
| 1706 | +#define COMPLETION_PRAGMAS 2 |
| 1707 | +#define COMPLETION_FUNCTIONS 3 |
| 1708 | +#define COMPLETION_COLLATIONS 4 |
| 1709 | +#define COMPLETION_INDEXES 5 |
| 1710 | +#define COMPLETION_TRIGGERS 6 |
| 1711 | +#define COMPLETION_DATABASES 7 |
| 1712 | +#define COMPLETION_TABLES 8 |
| 1713 | +#define COMPLETION_COLUMNS 9 |
| 1714 | +#define COMPLETION_MODULES 10 |
| 1715 | +#define COMPLETION_EOF 11 |
| 1716 | + |
| 1717 | +/* |
| 1718 | +** The completionConnect() method is invoked to create a new |
| 1719 | +** completion_vtab that describes the completion virtual table. |
| 1720 | +** |
| 1721 | +** Think of this routine as the constructor for completion_vtab objects. |
| 1722 | +** |
| 1723 | +** All this routine needs to do is: |
| 1724 | +** |
| 1725 | +** (1) Allocate the completion_vtab object and initialize all fields. |
| 1726 | +** |
| 1727 | +** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the |
| 1728 | +** result set of queries against completion will look like. |
| 1729 | +*/ |
| 1730 | +static int completionConnect( |
| 1731 | + sqlite3 *db, |
| 1732 | + void *pAux, |
| 1733 | + int argc, const char *const*argv, |
| 1734 | + sqlite3_vtab **ppVtab, |
| 1735 | + char **pzErr |
| 1736 | +){ |
| 1737 | + completion_vtab *pNew; |
| 1738 | + int rc; |
| 1739 | + |
| 1740 | +/* Column numbers */ |
| 1741 | +#define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */ |
| 1742 | +#define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */ |
| 1743 | +#define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */ |
| 1744 | +#define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */ |
| 1745 | + |
| 1746 | + rc = sqlite3_declare_vtab(db, |
| 1747 | + "CREATE TABLE x(" |
| 1748 | + " candidate TEXT," |
| 1749 | + " prefix TEXT HIDDEN," |
| 1750 | + " wholeline TEXT HIDDEN," |
| 1751 | + " phase INT HIDDEN" /* Used for debugging only */ |
| 1752 | + ")"); |
| 1753 | + if( rc==SQLITE_OK ){ |
| 1754 | + pNew = sqlite3_malloc( sizeof(*pNew) ); |
| 1755 | + *ppVtab = (sqlite3_vtab*)pNew; |
| 1756 | + if( pNew==0 ) return SQLITE_NOMEM; |
| 1757 | + memset(pNew, 0, sizeof(*pNew)); |
| 1758 | + pNew->db = db; |
| 1759 | + } |
| 1760 | + return rc; |
| 1761 | +} |
| 1762 | + |
| 1763 | +/* |
| 1764 | +** This method is the destructor for completion_cursor objects. |
| 1765 | +*/ |
| 1766 | +static int completionDisconnect(sqlite3_vtab *pVtab){ |
| 1767 | + sqlite3_free(pVtab); |
| 1768 | + return SQLITE_OK; |
| 1769 | +} |
| 1770 | + |
| 1771 | +/* |
| 1772 | +** Constructor for a new completion_cursor object. |
| 1773 | +*/ |
| 1774 | +static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
| 1775 | + completion_cursor *pCur; |
| 1776 | + pCur = sqlite3_malloc( sizeof(*pCur) ); |
| 1777 | + if( pCur==0 ) return SQLITE_NOMEM; |
| 1778 | + memset(pCur, 0, sizeof(*pCur)); |
| 1779 | + pCur->db = ((completion_vtab*)p)->db; |
| 1780 | + *ppCursor = &pCur->base; |
| 1781 | + return SQLITE_OK; |
| 1782 | +} |
| 1783 | + |
| 1784 | +/* |
| 1785 | +** Reset the completion_cursor. |
| 1786 | +*/ |
| 1787 | +static void completionCursorReset(completion_cursor *pCur){ |
| 1788 | + sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0; |
| 1789 | + sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0; |
| 1790 | + sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0; |
| 1791 | + pCur->j = 0; |
| 1792 | +} |
| 1793 | + |
| 1794 | +/* |
| 1795 | +** Destructor for a completion_cursor. |
| 1796 | +*/ |
| 1797 | +static int completionClose(sqlite3_vtab_cursor *cur){ |
| 1798 | + completionCursorReset((completion_cursor*)cur); |
| 1799 | + sqlite3_free(cur); |
| 1800 | + return SQLITE_OK; |
| 1801 | +} |
| 1802 | + |
| 1803 | +/* |
| 1804 | +** All SQL keywords understood by SQLite |
| 1805 | +*/ |
| 1806 | +static const char *completionKwrds[] = { |
| 1807 | + "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS", |
| 1808 | + "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY", |
| 1809 | + "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT", |
| 1810 | + "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE", |
| 1811 | + "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE", |
| 1812 | + "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH", |
| 1813 | + "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN", |
| 1814 | + "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF", |
| 1815 | + "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER", |
| 1816 | + "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY", |
| 1817 | + "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL", |
| 1818 | + "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA", |
| 1819 | + "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP", |
| 1820 | + "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT", |
| 1821 | + "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP", |
| 1822 | + "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE", |
| 1823 | + "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE", |
| 1824 | + "WITH", "WITHOUT", |
| 1825 | +}; |
| 1826 | + |
| 1827 | +/* |
| 1828 | +** Advance a completion_cursor to its next row of output. |
| 1829 | +** |
| 1830 | +** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object |
| 1831 | +** record the current state of the scan. This routine sets ->zCurrentRow |
| 1832 | +** to the current row of output and then returns. If no more rows remain, |
| 1833 | +** then ->ePhase is set to COMPLETION_EOF which will signal the virtual |
| 1834 | +** table that has reached the end of its scan. |
| 1835 | +** |
| 1836 | +** The current implementation just lists potential identifiers and |
| 1837 | +** keywords and filters them by zPrefix. Future enhancements should |
| 1838 | +** take zLine into account to try to restrict the set of identifiers and |
| 1839 | +** keywords based on what would be legal at the current point of input. |
| 1840 | +*/ |
| 1841 | +static int completionNext(sqlite3_vtab_cursor *cur){ |
| 1842 | + completion_cursor *pCur = (completion_cursor*)cur; |
| 1843 | + int eNextPhase = 0; /* Next phase to try if current phase reaches end */ |
| 1844 | + int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */ |
| 1845 | + pCur->iRowid++; |
| 1846 | + while( pCur->ePhase!=COMPLETION_EOF ){ |
| 1847 | + switch( pCur->ePhase ){ |
| 1848 | + case COMPLETION_KEYWORDS: { |
| 1849 | + if( pCur->j >= sizeof(completionKwrds)/sizeof(completionKwrds[0]) ){ |
| 1850 | + pCur->zCurrentRow = 0; |
| 1851 | + pCur->ePhase = COMPLETION_DATABASES; |
| 1852 | + }else{ |
| 1853 | + pCur->zCurrentRow = completionKwrds[pCur->j++]; |
| 1854 | + } |
| 1855 | + iCol = -1; |
| 1856 | + break; |
| 1857 | + } |
| 1858 | + case COMPLETION_DATABASES: { |
| 1859 | + if( pCur->pStmt==0 ){ |
| 1860 | + sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, |
| 1861 | + &pCur->pStmt, 0); |
| 1862 | + } |
| 1863 | + iCol = 1; |
| 1864 | + eNextPhase = COMPLETION_TABLES; |
| 1865 | + break; |
| 1866 | + } |
| 1867 | + case COMPLETION_TABLES: { |
| 1868 | + if( pCur->pStmt==0 ){ |
| 1869 | + sqlite3_stmt *pS2; |
| 1870 | + char *zSql = 0; |
| 1871 | + const char *zSep = ""; |
| 1872 | + sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0); |
| 1873 | + while( sqlite3_step(pS2)==SQLITE_ROW ){ |
| 1874 | + const char *zDb = (const char*)sqlite3_column_text(pS2, 1); |
| 1875 | + zSql = sqlite3_mprintf( |
| 1876 | + "%z%s" |
| 1877 | + "SELECT name FROM \"%w\".sqlite_master" |
| 1878 | + " WHERE type='table'", |
| 1879 | + zSql, zSep, zDb |
| 1880 | + ); |
| 1881 | + if( zSql==0 ) return SQLITE_NOMEM; |
| 1882 | + zSep = " UNION "; |
| 1883 | + } |
| 1884 | + sqlite3_finalize(pS2); |
| 1885 | + sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0); |
| 1886 | + sqlite3_free(zSql); |
| 1887 | + } |
| 1888 | + iCol = 0; |
| 1889 | + eNextPhase = COMPLETION_COLUMNS; |
| 1890 | + break; |
| 1891 | + } |
| 1892 | + case COMPLETION_COLUMNS: { |
| 1893 | + if( pCur->pStmt==0 ){ |
| 1894 | + sqlite3_stmt *pS2; |
| 1895 | + char *zSql = 0; |
| 1896 | + const char *zSep = ""; |
| 1897 | + sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0); |
| 1898 | + while( sqlite3_step(pS2)==SQLITE_ROW ){ |
| 1899 | + const char *zDb = (const char*)sqlite3_column_text(pS2, 1); |
| 1900 | + zSql = sqlite3_mprintf( |
| 1901 | + "%z%s" |
| 1902 | + "SELECT pti.name FROM \"%w\".sqlite_master AS sm" |
| 1903 | + " JOIN pragma_table_info(sm.name,%Q) AS pti" |
| 1904 | + " WHERE sm.type='table'", |
| 1905 | + zSql, zSep, zDb, zDb |
| 1906 | + ); |
| 1907 | + if( zSql==0 ) return SQLITE_NOMEM; |
| 1908 | + zSep = " UNION "; |
| 1909 | + } |
| 1910 | + sqlite3_finalize(pS2); |
| 1911 | + sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0); |
| 1912 | + sqlite3_free(zSql); |
| 1913 | + } |
| 1914 | + iCol = 0; |
| 1915 | + eNextPhase = COMPLETION_EOF; |
| 1916 | + break; |
| 1917 | + } |
| 1918 | + } |
| 1919 | + if( iCol<0 ){ |
| 1920 | + /* This case is when the phase presets zCurrentRow */ |
| 1921 | + if( pCur->zCurrentRow==0 ) continue; |
| 1922 | + }else{ |
| 1923 | + if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){ |
| 1924 | + /* Extract the next row of content */ |
| 1925 | + pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol); |
| 1926 | + }else{ |
| 1927 | + /* When all rows are finished, advance to the next phase */ |
| 1928 | + sqlite3_finalize(pCur->pStmt); |
| 1929 | + pCur->pStmt = 0; |
| 1930 | + pCur->ePhase = eNextPhase; |
| 1931 | + continue; |
| 1932 | + } |
| 1933 | + } |
| 1934 | + if( pCur->nPrefix==0 ) break; |
| 1935 | + if( sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0 ){ |
| 1936 | + break; |
| 1937 | + } |
| 1938 | + } |
| 1939 | + |
| 1940 | + return SQLITE_OK; |
| 1941 | +} |
| 1942 | + |
| 1943 | +/* |
| 1944 | +** Return values of columns for the row at which the completion_cursor |
| 1945 | +** is currently pointing. |
| 1946 | +*/ |
| 1947 | +static int completionColumn( |
| 1948 | + sqlite3_vtab_cursor *cur, /* The cursor */ |
| 1949 | + sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ |
| 1950 | + int i /* Which column to return */ |
| 1951 | +){ |
| 1952 | + completion_cursor *pCur = (completion_cursor*)cur; |
| 1953 | + switch( i ){ |
| 1954 | + case COMPLETION_COLUMN_CANDIDATE: { |
| 1955 | + sqlite3_result_text(ctx, pCur->zCurrentRow, -1, SQLITE_TRANSIENT); |
| 1956 | + break; |
| 1957 | + } |
| 1958 | + case COMPLETION_COLUMN_PREFIX: { |
| 1959 | + sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT); |
| 1960 | + break; |
| 1961 | + } |
| 1962 | + case COMPLETION_COLUMN_WHOLELINE: { |
| 1963 | + sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT); |
| 1964 | + break; |
| 1965 | + } |
| 1966 | + case COMPLETION_COLUMN_PHASE: { |
| 1967 | + sqlite3_result_int(ctx, pCur->ePhase); |
| 1968 | + break; |
| 1969 | + } |
| 1970 | + } |
| 1971 | + return SQLITE_OK; |
| 1972 | +} |
| 1973 | + |
| 1974 | +/* |
| 1975 | +** Return the rowid for the current row. In this implementation, the |
| 1976 | +** rowid is the same as the output value. |
| 1977 | +*/ |
| 1978 | +static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
| 1979 | + completion_cursor *pCur = (completion_cursor*)cur; |
| 1980 | + *pRowid = pCur->iRowid; |
| 1981 | + return SQLITE_OK; |
| 1982 | +} |
| 1983 | + |
| 1984 | +/* |
| 1985 | +** Return TRUE if the cursor has been moved off of the last |
| 1986 | +** row of output. |
| 1987 | +*/ |
| 1988 | +static int completionEof(sqlite3_vtab_cursor *cur){ |
| 1989 | + completion_cursor *pCur = (completion_cursor*)cur; |
| 1990 | + return pCur->ePhase >= COMPLETION_EOF; |
| 1991 | +} |
| 1992 | + |
| 1993 | +/* |
| 1994 | +** This method is called to "rewind" the completion_cursor object back |
| 1995 | +** to the first row of output. This method is always called at least |
| 1996 | +** once prior to any call to completionColumn() or completionRowid() or |
| 1997 | +** completionEof(). |
| 1998 | +*/ |
| 1999 | +static int completionFilter( |
| 2000 | + sqlite3_vtab_cursor *pVtabCursor, |
| 2001 | + int idxNum, const char *idxStr, |
| 2002 | + int argc, sqlite3_value **argv |
| 2003 | +){ |
| 2004 | + completion_cursor *pCur = (completion_cursor *)pVtabCursor; |
| 2005 | + int iArg = 0; |
| 2006 | + completionCursorReset(pCur); |
| 2007 | + if( idxNum & 1 ){ |
| 2008 | + pCur->nPrefix = sqlite3_value_bytes(argv[iArg]); |
| 2009 | + if( pCur->nPrefix>0 ){ |
| 2010 | + pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg])); |
| 2011 | + if( pCur->zPrefix==0 ) return SQLITE_NOMEM; |
| 2012 | + } |
| 2013 | + iArg++; |
| 2014 | + } |
| 2015 | + if( idxNum & 2 ){ |
| 2016 | + pCur->nLine = sqlite3_value_bytes(argv[iArg]); |
| 2017 | + if( pCur->nLine>0 ){ |
| 2018 | + pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg])); |
| 2019 | + if( pCur->zLine==0 ) return SQLITE_NOMEM; |
| 2020 | + } |
| 2021 | + iArg++; |
| 2022 | + } |
| 2023 | + if( pCur->zLine!=0 && pCur->zPrefix==0 ){ |
| 2024 | + int i = pCur->nLine; |
| 2025 | + while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){ |
| 2026 | + i--; |
| 2027 | + } |
| 2028 | + pCur->nPrefix = pCur->nLine - i; |
| 2029 | + if( pCur->nPrefix>0 ){ |
| 2030 | + pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i); |
| 2031 | + if( pCur->zPrefix==0 ) return SQLITE_NOMEM; |
| 2032 | + } |
| 2033 | + } |
| 2034 | + pCur->iRowid = 0; |
| 2035 | + pCur->ePhase = COMPLETION_FIRST_PHASE; |
| 2036 | + return completionNext(pVtabCursor); |
| 2037 | +} |
| 2038 | + |
| 2039 | +/* |
| 2040 | +** SQLite will invoke this method one or more times while planning a query |
| 2041 | +** that uses the completion virtual table. This routine needs to create |
| 2042 | +** a query plan for each invocation and compute an estimated cost for that |
| 2043 | +** plan. |
| 2044 | +** |
| 2045 | +** There are two hidden parameters that act as arguments to the table-valued |
| 2046 | +** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix" |
| 2047 | +** is available and bit 1 is set if "wholeline" is available. |
| 2048 | +*/ |
| 2049 | +static int completionBestIndex( |
| 2050 | + sqlite3_vtab *tab, |
| 2051 | + sqlite3_index_info *pIdxInfo |
| 2052 | +){ |
| 2053 | + int i; /* Loop over constraints */ |
| 2054 | + int idxNum = 0; /* The query plan bitmask */ |
| 2055 | + int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */ |
| 2056 | + int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */ |
| 2057 | + int nArg = 0; /* Number of arguments that completeFilter() expects */ |
| 2058 | + const struct sqlite3_index_constraint *pConstraint; |
| 2059 | + |
| 2060 | + pConstraint = pIdxInfo->aConstraint; |
| 2061 | + for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ |
| 2062 | + if( pConstraint->usable==0 ) continue; |
| 2063 | + if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; |
| 2064 | + switch( pConstraint->iColumn ){ |
| 2065 | + case COMPLETION_COLUMN_PREFIX: |
| 2066 | + prefixIdx = i; |
| 2067 | + idxNum |= 1; |
| 2068 | + break; |
| 2069 | + case COMPLETION_COLUMN_WHOLELINE: |
| 2070 | + wholelineIdx = i; |
| 2071 | + idxNum |= 2; |
| 2072 | + break; |
| 2073 | + } |
| 2074 | + } |
| 2075 | + if( prefixIdx>=0 ){ |
| 2076 | + pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg; |
| 2077 | + pIdxInfo->aConstraintUsage[prefixIdx].omit = 1; |
| 2078 | + } |
| 2079 | + if( wholelineIdx>=0 ){ |
| 2080 | + pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg; |
| 2081 | + pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1; |
| 2082 | + } |
| 2083 | + pIdxInfo->idxNum = idxNum; |
| 2084 | + pIdxInfo->estimatedCost = (double)5000 - 1000*nArg; |
| 2085 | + pIdxInfo->estimatedRows = 500 - 100*nArg; |
| 2086 | + return SQLITE_OK; |
| 2087 | +} |
| 2088 | + |
| 2089 | +/* |
| 2090 | +** This following structure defines all the methods for the |
| 2091 | +** completion virtual table. |
| 2092 | +*/ |
| 2093 | +static sqlite3_module completionModule = { |
| 2094 | + 0, /* iVersion */ |
| 2095 | + 0, /* xCreate */ |
| 2096 | + completionConnect, /* xConnect */ |
| 2097 | + completionBestIndex, /* xBestIndex */ |
| 2098 | + completionDisconnect, /* xDisconnect */ |
| 2099 | + 0, /* xDestroy */ |
| 2100 | + completionOpen, /* xOpen - open a cursor */ |
| 2101 | + completionClose, /* xClose - close a cursor */ |
| 2102 | + completionFilter, /* xFilter - configure scan constraints */ |
| 2103 | + completionNext, /* xNext - advance a cursor */ |
| 2104 | + completionEof, /* xEof - check for end of scan */ |
| 2105 | + completionColumn, /* xColumn - read data */ |
| 2106 | + completionRowid, /* xRowid - read data */ |
| 2107 | + 0, /* xUpdate */ |
| 2108 | + 0, /* xBegin */ |
| 2109 | + 0, /* xSync */ |
| 2110 | + 0, /* xCommit */ |
| 2111 | + 0, /* xRollback */ |
| 2112 | + 0, /* xFindMethod */ |
| 2113 | + 0, /* xRename */ |
| 2114 | +}; |
| 2115 | + |
| 2116 | +#endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 2117 | + |
| 2118 | +int sqlite3CompletionVtabInit(sqlite3 *db){ |
| 2119 | + int rc = SQLITE_OK; |
| 2120 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2121 | + rc = sqlite3_create_module(db, "completion", &completionModule, 0); |
| 2122 | +#endif |
| 2123 | + return rc; |
| 2124 | +} |
| 2125 | + |
| 2126 | +#ifdef _WIN32 |
| 2127 | +__declspec(dllexport) |
| 2128 | +#endif |
| 2129 | +int sqlite3_completion_init( |
| 2130 | + sqlite3 *db, |
| 2131 | + char **pzErrMsg, |
| 2132 | + const sqlite3_api_routines *pApi |
| 2133 | +){ |
| 2134 | + int rc = SQLITE_OK; |
| 2135 | + SQLITE_EXTENSION_INIT2(pApi); |
| 2136 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2137 | + rc = sqlite3CompletionVtabInit(db); |
| 2138 | +#endif |
| 2139 | + return rc; |
| 2140 | +} |
| 2141 | + |
| 2142 | +/************************* End ../ext/misc/completion.c ********************/ |
| 1446 | 2143 | |
| 1447 | 2144 | #if defined(SQLITE_ENABLE_SESSION) |
| 1448 | 2145 | /* |
| 1449 | 2146 | ** State information for a single open session |
| 1450 | 2147 | */ |
| | @@ -1521,12 +2218,13 @@ |
| 1521 | 2218 | #define SHFLG_Scratch 0x00000001 /* The --scratch option is used */ |
| 1522 | 2219 | #define SHFLG_Pagecache 0x00000002 /* The --pagecache option is used */ |
| 1523 | 2220 | #define SHFLG_Lookaside 0x00000004 /* Lookaside memory is used */ |
| 1524 | 2221 | #define SHFLG_Backslash 0x00000008 /* The --backslash option is used */ |
| 1525 | 2222 | #define SHFLG_PreserveRowid 0x00000010 /* .dump preserves rowid values */ |
| 1526 | | -#define SHFLG_CountChanges 0x00000020 /* .changes setting */ |
| 1527 | | -#define SHFLG_Echo 0x00000040 /* .echo or --echo setting */ |
| 2223 | +#define SHFLG_Newlines 0x00000020 /* .dump --newline flag */ |
| 2224 | +#define SHFLG_CountChanges 0x00000040 /* .changes setting */ |
| 2225 | +#define SHFLG_Echo 0x00000080 /* .echo or --echo setting */ |
| 1528 | 2226 | |
| 1529 | 2227 | /* |
| 1530 | 2228 | ** Macros for testing and setting shellFlgs |
| 1531 | 2229 | */ |
| 1532 | 2230 | #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0) |
| | @@ -2193,11 +2891,15 @@ |
| 2193 | 2891 | for(i=0; i<nArg; i++){ |
| 2194 | 2892 | raw_printf(p->out, i>0 ? "," : " VALUES("); |
| 2195 | 2893 | if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
| 2196 | 2894 | utf8_printf(p->out,"NULL"); |
| 2197 | 2895 | }else if( aiType && aiType[i]==SQLITE_TEXT ){ |
| 2198 | | - output_quoted_escaped_string(p->out, azArg[i]); |
| 2896 | + if( ShellHasFlag(p, SHFLG_Newlines) ){ |
| 2897 | + output_quoted_string(p->out, azArg[i]); |
| 2898 | + }else{ |
| 2899 | + output_quoted_escaped_string(p->out, azArg[i]); |
| 2900 | + } |
| 2199 | 2901 | }else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
| 2200 | 2902 | utf8_printf(p->out,"%s", azArg[i]); |
| 2201 | 2903 | }else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
| 2202 | 2904 | char z[50]; |
| 2203 | 2905 | double r = sqlite3_column_double(p->pStmt, i); |
| | @@ -2207,10 +2909,12 @@ |
| 2207 | 2909 | const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
| 2208 | 2910 | int nBlob = sqlite3_column_bytes(p->pStmt, i); |
| 2209 | 2911 | output_hex_blob(p->out, pBlob, nBlob); |
| 2210 | 2912 | }else if( isNumber(azArg[i], 0) ){ |
| 2211 | 2913 | utf8_printf(p->out,"%s", azArg[i]); |
| 2914 | + }else if( ShellHasFlag(p, SHFLG_Newlines) ){ |
| 2915 | + output_quoted_string(p->out, azArg[i]); |
| 2212 | 2916 | }else{ |
| 2213 | 2917 | output_quoted_escaped_string(p->out, azArg[i]); |
| 2214 | 2918 | } |
| 2215 | 2919 | } |
| 2216 | 2920 | raw_printf(p->out,");\n"); |
| | @@ -3325,11 +4029,13 @@ |
| 3325 | 4029 | " If TABLE specified, only dump tables matching\n" |
| 3326 | 4030 | " LIKE pattern TABLE.\n" |
| 3327 | 4031 | ".echo on|off Turn command echo on or off\n" |
| 3328 | 4032 | ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n" |
| 3329 | 4033 | ".exit Exit this program\n" |
| 3330 | | - ".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n" |
| 4034 | +/* Because explain mode comes on automatically now, the ".explain" mode |
| 4035 | +** is removed from the help screen. It is still supported for legacy, however */ |
| 4036 | +/*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"*/ |
| 3331 | 4037 | ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n" |
| 3332 | 4038 | ".headers on|off Turn display of headers on or off\n" |
| 3333 | 4039 | ".help Show this message\n" |
| 3334 | 4040 | ".import FILE TABLE Import data from FILE into TABLE\n" |
| 3335 | 4041 | #ifndef SQLITE_OMIT_TEST_CONTROL |
| | @@ -3460,62 +4166,10 @@ |
| 3460 | 4166 | pBuf[nIn] = 0; |
| 3461 | 4167 | if( pnByte ) *pnByte = nIn; |
| 3462 | 4168 | return pBuf; |
| 3463 | 4169 | } |
| 3464 | 4170 | |
| 3465 | | -/* |
| 3466 | | -** Implementation of the "readfile(X)" SQL function. The entire content |
| 3467 | | -** of the file named X is read and returned as a BLOB. NULL is returned |
| 3468 | | -** if the file does not exist or is unreadable. |
| 3469 | | -*/ |
| 3470 | | -static void readfileFunc( |
| 3471 | | - sqlite3_context *context, |
| 3472 | | - int argc, |
| 3473 | | - sqlite3_value **argv |
| 3474 | | -){ |
| 3475 | | - const char *zName; |
| 3476 | | - void *pBuf; |
| 3477 | | - int nBuf; |
| 3478 | | - |
| 3479 | | - UNUSED_PARAMETER(argc); |
| 3480 | | - zName = (const char*)sqlite3_value_text(argv[0]); |
| 3481 | | - if( zName==0 ) return; |
| 3482 | | - pBuf = readFile(zName, &nBuf); |
| 3483 | | - if( pBuf ) sqlite3_result_blob(context, pBuf, nBuf, sqlite3_free); |
| 3484 | | -} |
| 3485 | | - |
| 3486 | | -/* |
| 3487 | | -** Implementation of the "writefile(X,Y)" SQL function. The argument Y |
| 3488 | | -** is written into file X. The number of bytes written is returned. Or |
| 3489 | | -** NULL is returned if something goes wrong, such as being unable to open |
| 3490 | | -** file X for writing. |
| 3491 | | -*/ |
| 3492 | | -static void writefileFunc( |
| 3493 | | - sqlite3_context *context, |
| 3494 | | - int argc, |
| 3495 | | - sqlite3_value **argv |
| 3496 | | -){ |
| 3497 | | - FILE *out; |
| 3498 | | - const char *z; |
| 3499 | | - sqlite3_int64 rc; |
| 3500 | | - const char *zFile; |
| 3501 | | - |
| 3502 | | - UNUSED_PARAMETER(argc); |
| 3503 | | - zFile = (const char*)sqlite3_value_text(argv[0]); |
| 3504 | | - if( zFile==0 ) return; |
| 3505 | | - out = fopen(zFile, "wb"); |
| 3506 | | - if( out==0 ) return; |
| 3507 | | - z = (const char*)sqlite3_value_blob(argv[1]); |
| 3508 | | - if( z==0 ){ |
| 3509 | | - rc = 0; |
| 3510 | | - }else{ |
| 3511 | | - rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out); |
| 3512 | | - } |
| 3513 | | - fclose(out); |
| 3514 | | - sqlite3_result_int64(context, rc); |
| 3515 | | -} |
| 3516 | | - |
| 3517 | 4171 | #if defined(SQLITE_ENABLE_SESSION) |
| 3518 | 4172 | /* |
| 3519 | 4173 | ** Close a single OpenSession object and release all of its associated |
| 3520 | 4174 | ** resources. |
| 3521 | 4175 | */ |
| | @@ -3577,22 +4231,76 @@ |
| 3577 | 4231 | exit(1); |
| 3578 | 4232 | } |
| 3579 | 4233 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 3580 | 4234 | sqlite3_enable_load_extension(p->db, 1); |
| 3581 | 4235 | #endif |
| 3582 | | - sqlite3_create_function(p->db, "readfile", 1, SQLITE_UTF8, 0, |
| 3583 | | - readfileFunc, 0, 0); |
| 3584 | | - sqlite3_create_function(p->db, "writefile", 2, SQLITE_UTF8, 0, |
| 3585 | | - writefileFunc, 0, 0); |
| 3586 | | - sqlite3_create_function(p->db, "sha3", 1, SQLITE_UTF8, 0, |
| 3587 | | - sha3Func, 0, 0); |
| 3588 | | - sqlite3_create_function(p->db, "sha3", 2, SQLITE_UTF8, 0, |
| 3589 | | - sha3Func, 0, 0); |
| 3590 | | - sqlite3_create_function(p->db, "sha3_query", 1, SQLITE_UTF8, 0, |
| 3591 | | - sha3QueryFunc, 0, 0); |
| 3592 | | - sqlite3_create_function(p->db, "sha3_query", 2, SQLITE_UTF8, 0, |
| 3593 | | - sha3QueryFunc, 0, 0); |
| 4236 | + sqlite3_fileio_init(p->db, 0, 0); |
| 4237 | + sqlite3_shathree_init(p->db, 0, 0); |
| 4238 | + sqlite3_completion_init(p->db, 0, 0); |
| 3594 | 4239 | sqlite3_create_function(p->db, "shell_add_schema", 2, SQLITE_UTF8, 0, |
| 3595 | 4240 | shellAddSchemaName, 0, 0); |
| 3596 | | - |
| 3597 | 4241 | } |
| 3598 | 4242 | } |
| 4243 | + |
| 4244 | +#if HAVE_READLINE || HAVE_EDITLINE |
| 4245 | +/* |
| 4246 | +** Readline completion callbacks |
| 4247 | +*/ |
| 4248 | +static char *readline_completion_generator(const char *text, int state){ |
| 4249 | + static sqlite3_stmt *pStmt = 0; |
| 4250 | + char *zRet; |
| 4251 | + if( state==0 ){ |
| 4252 | + char *zSql; |
| 4253 | + int rc; |
| 4254 | + sqlite3_finalize(pStmt); |
| 4255 | + zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" |
| 4256 | + " FROM completion(%Q) ORDER BY 1", text); |
| 4257 | + sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); |
| 4258 | + sqlite3_free(zSql); |
| 4259 | + } |
| 4260 | + if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 4261 | + zRet = strdup(sqlite3_column_text(pStmt, 0)); |
| 4262 | + }else{ |
| 4263 | + sqlite3_finalize(pStmt); |
| 4264 | + pStmt = 0; |
| 4265 | + zRet = 0; |
| 4266 | + } |
| 4267 | + return zRet; |
| 4268 | +} |
| 4269 | +static char **readline_completion(const char *zText, int iStart, int iEnd){ |
| 4270 | + rl_attempted_completion_over = 1; |
| 4271 | + return rl_completion_matches(zText, readline_completion_generator); |
| 4272 | +} |
| 4273 | + |
| 4274 | +#elif HAVE_LINENOISE |
| 4275 | +/* |
| 4276 | +** Linenoise completion callback |
| 4277 | +*/ |
| 4278 | +static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){ |
| 4279 | + int nLine = (int)strlen(zLine); |
| 4280 | + int i, iStart; |
| 4281 | + sqlite3_stmt *pStmt = 0; |
| 4282 | + char *zSql; |
| 4283 | + char zBuf[1000]; |
| 4284 | + |
| 4285 | + if( nLine>sizeof(zBuf)-30 ) return; |
| 4286 | + if( zLine[0]=='.' ) return; |
| 4287 | + for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){} |
| 4288 | + if( i==nLine-1 ) return; |
| 4289 | + iStart = i+1; |
| 4290 | + memcpy(zBuf, zLine, iStart); |
| 4291 | + zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" |
| 4292 | + " FROM completion(%Q,%Q) ORDER BY 1", |
| 4293 | + &zLine[iStart], zLine); |
| 4294 | + sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); |
| 4295 | + sqlite3_free(zSql); |
| 4296 | + sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */ |
| 4297 | + while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 4298 | + const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0); |
| 4299 | + int nCompletion = sqlite3_column_bytes(pStmt, 0); |
| 4300 | + if( iStart+nCompletion < sizeof(zBuf)-1 ){ |
| 4301 | + memcpy(zBuf+iStart, zCompletion, nCompletion+1); |
| 4302 | + linenoiseAddCompletion(lc, zBuf); |
| 4303 | + } |
| 4304 | + } |
| 4305 | + sqlite3_finalize(pStmt); |
| 4306 | +} |
| | @@ -3599,5 +4307,6 @@ |
| 4307 | +#endif |
| 3599 | 4308 | |
| 3600 | 4309 | /* |
| 3601 | 4310 | ** Do C-language style dequoting. |
| 3602 | 4311 | ** |
| 3603 | 4312 | ** \a -> alarm |
| | @@ -4926,11 +5635,11 @@ |
| 4926 | 5635 | |
| 4927 | 5636 | if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ |
| 4928 | 5637 | const char *zLike = 0; |
| 4929 | 5638 | int i; |
| 4930 | 5639 | int savedShowHeader = p->showHeader; |
| 4931 | | - ShellClearFlag(p, SHFLG_PreserveRowid); |
| 5640 | + ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines); |
| 4932 | 5641 | for(i=1; i<nArg; i++){ |
| 4933 | 5642 | if( azArg[i][0]=='-' ){ |
| 4934 | 5643 | const char *z = azArg[i]+1; |
| 4935 | 5644 | if( z[0]=='-' ) z++; |
| 4936 | 5645 | if( strcmp(z,"preserve-rowids")==0 ){ |
| | @@ -4941,17 +5650,21 @@ |
| 4941 | 5650 | goto meta_command_exit; |
| 4942 | 5651 | #else |
| 4943 | 5652 | ShellSetFlag(p, SHFLG_PreserveRowid); |
| 4944 | 5653 | #endif |
| 4945 | 5654 | }else |
| 5655 | + if( strcmp(z,"newlines")==0 ){ |
| 5656 | + ShellSetFlag(p, SHFLG_Newlines); |
| 5657 | + }else |
| 4946 | 5658 | { |
| 4947 | 5659 | raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]); |
| 4948 | 5660 | rc = 1; |
| 4949 | 5661 | goto meta_command_exit; |
| 4950 | 5662 | } |
| 4951 | 5663 | }else if( zLike ){ |
| 4952 | | - raw_printf(stderr, "Usage: .dump ?--preserve-rowids? ?LIKE-PATTERN?\n"); |
| 5664 | + raw_printf(stderr, "Usage: .dump ?--preserve-rowids? " |
| 5665 | + "?--newlines? ?LIKE-PATTERN?\n"); |
| 4953 | 5666 | rc = 1; |
| 4954 | 5667 | goto meta_command_exit; |
| 4955 | 5668 | }else{ |
| 4956 | 5669 | zLike = azArg[i]; |
| 4957 | 5670 | } |
| | @@ -5033,10 +5746,12 @@ |
| 5033 | 5746 | if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
| 5034 | 5747 | if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); |
| 5035 | 5748 | rc = 2; |
| 5036 | 5749 | }else |
| 5037 | 5750 | |
| 5751 | + /* The ".explain" command is automatic now. It is largely pointless. It |
| 5752 | + ** retained purely for backwards compatibility */ |
| 5038 | 5753 | if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
| 5039 | 5754 | int val = 1; |
| 5040 | 5755 | if( nArg>=2 ){ |
| 5041 | 5756 | if( strcmp(azArg[1],"auto")==0 ){ |
| 5042 | 5757 | val = 99; |
| | @@ -5549,11 +6264,13 @@ |
| 5549 | 6264 | p->mode = MODE_Quote; |
| 5550 | 6265 | }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){ |
| 5551 | 6266 | p->mode = MODE_Ascii; |
| 5552 | 6267 | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); |
| 5553 | 6268 | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); |
| 5554 | | - }else { |
| 6269 | + }else if( nArg==1 ){ |
| 6270 | + raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]); |
| 6271 | + }else{ |
| 5555 | 6272 | raw_printf(stderr, "Error: mode should be one of: " |
| 5556 | 6273 | "ascii column csv html insert line list quote tabs tcl\n"); |
| 5557 | 6274 | rc = 1; |
| 5558 | 6275 | } |
| 5559 | 6276 | p->cMode = p->mode; |
| | @@ -5821,12 +6538,18 @@ |
| 5821 | 6538 | rc = 1; |
| 5822 | 6539 | goto meta_command_exit; |
| 5823 | 6540 | } |
| 5824 | 6541 | if( zDiv ){ |
| 5825 | 6542 | sqlite3_stmt *pStmt = 0; |
| 5826 | | - sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list", |
| 5827 | | - -1, &pStmt, 0); |
| 6543 | + rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list", |
| 6544 | + -1, &pStmt, 0); |
| 6545 | + if( rc ){ |
| 6546 | + utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 6547 | + sqlite3_finalize(pStmt); |
| 6548 | + rc = 1; |
| 6549 | + goto meta_command_exit; |
| 6550 | + } |
| 5828 | 6551 | appendText(&sSelect, "SELECT sql FROM", 0); |
| 5829 | 6552 | iSchema = 0; |
| 5830 | 6553 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 5831 | 6554 | const char *zDb = (const char*)sqlite3_column_text(pStmt, 0); |
| 5832 | 6555 | char zScNum[30]; |
| | @@ -7179,10 +7902,11 @@ |
| 7179 | 7902 | " -multiplex enable the multiplexor VFS\n" |
| 7180 | 7903 | #endif |
| 7181 | 7904 | " -newline SEP set output row separator. Default: '\\n'\n" |
| 7182 | 7905 | " -nullvalue TEXT set text string for NULL values. Default ''\n" |
| 7183 | 7906 | " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" |
| 7907 | + " -quote set output mode to 'quote'\n" |
| 7184 | 7908 | " -scratch SIZE N use N slots of SZ bytes each for scratch memory\n" |
| 7185 | 7909 | " -separator SEP set output column separator. Default: '|'\n" |
| 7186 | 7910 | " -stats print memory stats before each finalize\n" |
| 7187 | 7911 | " -version show SQLite version\n" |
| 7188 | 7912 | " -vfs NAME use NAME as the default VFS\n" |
| | @@ -7474,10 +8198,12 @@ |
| 7474 | 8198 | i++; |
| 7475 | 8199 | }else if( strcmp(z,"-html")==0 ){ |
| 7476 | 8200 | data.mode = MODE_Html; |
| 7477 | 8201 | }else if( strcmp(z,"-list")==0 ){ |
| 7478 | 8202 | data.mode = MODE_List; |
| 8203 | + }else if( strcmp(z,"-quote")==0 ){ |
| 8204 | + data.mode = MODE_Quote; |
| 7479 | 8205 | }else if( strcmp(z,"-line")==0 ){ |
| 7480 | 8206 | data.mode = MODE_Line; |
| 7481 | 8207 | }else if( strcmp(z,"-column")==0 ){ |
| 7482 | 8208 | data.mode = MODE_Column; |
| 7483 | 8209 | }else if( strcmp(z,"-csv")==0 ){ |
| | @@ -7625,10 +8351,15 @@ |
| 7625 | 8351 | if( (zHistory = malloc(nHistory))!=0 ){ |
| 7626 | 8352 | sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); |
| 7627 | 8353 | } |
| 7628 | 8354 | } |
| 7629 | 8355 | if( zHistory ){ shell_read_history(zHistory); } |
| 8356 | +#if HAVE_READLINE || HAVE_EDITLINE |
| 8357 | + rl_attempted_completion_function = readline_completion; |
| 8358 | +#elif HAVE_LINENOISE |
| 8359 | + linenoiseSetCompletionCallback(linenoise_completion); |
| 8360 | +#endif |
| 7630 | 8361 | rc = process_input(&data, 0); |
| 7631 | 8362 | if( zHistory ){ |
| 7632 | 8363 | shell_stifle_history(100); |
| 7633 | 8364 | shell_write_history(zHistory); |
| 7634 | 8365 | free(zHistory); |
| | @@ -7648,5 +8379,6 @@ |
| 7648 | 8379 | for(i=0; i<argc; i++) sqlite3_free(argv[i]); |
| 7649 | 8380 | sqlite3_free(argv); |
| 7650 | 8381 | #endif |
| 7651 | 8382 | return rc; |
| 7652 | 8383 | } |
| 8384 | + |
| 7653 | 8385 | |