# # # add_file "sqlite/btreeInt.h" # content [ac1ab1fb624ffbe571786cd2bd9559f9ae336355] # # add_file "sqlite/limits.h" # content [71ab25f17e35e0a9f3f6f234b8ed49cc56731d35] # # add_file "sqlite/malloc.c" # content [fa9bbccc4e6d099cd04c2518d238a1669c9d1020] # # add_file "sqlite/vdbeblob.c" # content [96f3572fdc45eda5be06e6372b612bc30742d9f0] # # patch "Makefile.am" # from [48daf094e8337fabfe244d6c9f8deea847f0380d] # to [a0c1a322d2d4e4fe18f32f7a48d3eb1ee447a94b] # # patch "database.cc" # from [5087d1394d533e4daddc1122c55f9be269eccf7e] # to [46acb01357e625f61a20b3c5899ae885b29b213a] # # patch "schema_migration.cc" # from [222733a13d7a3eb204c67af2123f5a242957b415] # to [769321d3bb854322d9de25354e9e240396cf1096] # # patch "sqlite/alter.c" # from [2c79ec40f65e33deaf90ca493422c74586e481a3] # to [1b1deeb97446ed87f2fa17a3eb6236548841a348] # # patch "sqlite/analyze.c" # from [4bbf5ddf9680587c6d4917e02e378b6037be3651] # to [e8fcb1c35ace8418615eb18d9601f321ac86b2ec] # # patch "sqlite/attach.c" # from [a16ada4a4654a0d126b8223ec9494ebb81bc5c3c] # to [ba628db0c2b6a362f036d017bf1196cdfe4ebb37] # # patch "sqlite/auth.c" # from [902f4722661c796b97f007d9606bd7529c02597f] # to [5ea90bc93dfea46e9fe4bf531e14c7cd98219ecb] # # patch "sqlite/btree.c" # from [960bf64baa4d2bdb96019698e60d0b7763bf4e7e] # to [bde03df933f8295e984b831779a318cc7c38f339] # # patch "sqlite/btree.h" # from [9b2cc0d113c0bc2d37d244b9a394d56948c9acbf] # to [1d527bf61ed176f980c34999d5793a0fd45dcf8c] # # patch "sqlite/build.c" # from [1880da163d9aa404016242b8b76d69907f682cd8] # to [50992d92e131a9aa9aa6657fb1ddc13e176fd70c] # # patch "sqlite/callback.c" # from [6414ed32d55859d0f65067aa5b88d2da27b3af9e] # to [9c12535669a638f90a67e10440b99c7b93c0fbf4] # # patch "sqlite/date.c" # from [74b76691bddf58b634f6bf4a77c8c58234268c6e] # to [6049db7d5a8fdf2c677ff7d58fa31d4f6593c988] # # patch "sqlite/expr.c" # from [2f0f9f89efe9170e5e6ca5d5e93a9d5896fff5ac] # to [b68f1b208be50413b407da92bd7b0fce840999cf] # # patch "sqlite/func.c" # from [007d957c057bb42b0d37aa6ad4be0e1c67a8871b] # to [4668843e6f0d27653939e12fc32276fb8e38d21a] # # patch "sqlite/insert.c" # from [413cc06990cb3c401e64e596776c1e43934f8841] # to [e595ca26805dfb3a9ebaabc28e7947c479f3b14d] # # patch "sqlite/keywordhash.h" # from [65fe072d2fa7cf2cfece3c7550f9085ec2988717] # to [1224129789df7a9d09fbef62e56cb5254915b78f] # # patch "sqlite/legacy.c" # from [c05a599a37f703ed1e66fdb5df60c2db65f29e71] # to [388c71ad7fbcd898ba1bcbfc98a3ac954bfa5d01] # # patch "sqlite/main.c" # from [e6eb036c3580ba9116fedfe4a8b58ed63d5abb37] # to [797dc983716c1480f6af78a36be3add8806211a1] # # patch "sqlite/opcodes.c" # from [d21a21fc60da3874a0970a6e67c81c56ec507d6c] # to [ecbfbd2a8f3a4e4a5b68075a4b15616b063cd461] # # patch "sqlite/opcodes.h" # from [a58b9a527965930bd27502dd8b1f101d326aa08f] # to [f6c44489cc16671f6f7702d99b6eefa64d08b5cd] # # patch "sqlite/os.c" # from [4650e98aadd27abfe1698ff58edf6893c58d4881] # to [1f10b47acc1177fb9225edb4f5f0d25ed716f9cb] # # patch "sqlite/os.h" # from [9240adf088fd55732f8926f9017601ba3430ead8] # to [213dd1f8f7ab9159c04a893eadc0cdd52515df16] # # patch "sqlite/os_unix.c" # from [426b4c03c304ad78746d65d9ba101e0b72e18e23] # to [f2ccf2e9a925fc679faf7a8fe85700e0f13cf0e1] # # patch "sqlite/os_win.c" # from [e94903c7dc1c0599c8ddce42efa0b6928068ddc5] # to [d868d5f9e95ec9c1b9e2a30c54c996053db6dddd] # # patch "sqlite/pager.c" # from [33c632ce9c228d87f14879a139fa123d43e4bf25] # to [39352b58ee840cae715a4f0d20e446aa5e1445fe] # # patch "sqlite/pager.h" # from [d652ddf092d2318d00e41f8539760fe8e57c157c] # to [94110a5570dca30d54a883e880a3633b2e4c05ae] # # patch "sqlite/parse.c" # from [b29e3203362be291200999d2e91d01187473f551] # to [5fda95dfe0c8391ffc7826da7d5cea986bd66bee] # # patch "sqlite/pragma.c" # from [3b992b5b2640d6ae25cef05aa6a42cd1d6c43234] # to [0d25dad58bdfd6789943a10f1b9663c2eb85b96d] # # patch "sqlite/prepare.c" # from [4cb9c9eb926e8baf5652ca4b4f2416f53f5b5370] # to [87c23644986b5e41a58bc76f05abebd899e00089] # # patch "sqlite/printf.c" # from [0c6f40648770831341ac45ab32423a80b4c87f05] # to [711908d6ff3385f9882df8ff405b9e0a2a4d04df] # # patch "sqlite/select.c" # from [b914abca0ba28893e7fb7c7fb97a05e240e2ce8b] # to [33a258fc9c9dccb28ae2d3a02f1e1148d6433148] # # patch "sqlite/sqlite3.h" # from [6dcafd84bc51a2510ec2d8ef94f024d36b0e1a00] # to [2d972843072bbc136f0b0e5398e7a7aeedee9f87] # # patch "sqlite/sqliteInt.h" # from [047af0e4c38bbb8652836f72adc9e9199c51a1ba] # to [208c40b6e11925a321ec159d889e0ec06b618359] # # patch "sqlite/table.c" # from [6d0da66dde26ee75614ed8f584a1996467088d06] # to [a8de75bcedf84d4060d804264b067ab3b1a3561d] # # patch "sqlite/tokenize.c" # from [7d611fc942ca0b12514eea2e1fbb148a65af23f2] # to [6cef9e6fc454d789a32c5b509ccb193a2b01977b] # # patch "sqlite/utf.c" # from [e64a48bc21aa973eb622dd47da87d56a4cdcf528] # to [01b2aba02b10d12903e9e1ff897215c9faf6b662] # # patch "sqlite/util.c" # from [b6344325378e75b9e18175d8b6aed1723d73dad9] # to [80cdf6b68d03b8f0ab3237a390842e039cff66c6] # # patch "sqlite/vdbe.c" # from [814dab208a156250bc5e77f827f4e0c8ad734820] # to [4f3e83218359fd51ae0b6efc445a97bd5f658ae4] # # patch "sqlite/vdbe.h" # from [0025259af1939fb264a545816c69e4b5b8d52691] # to [001c5b257567c1d3de7feb2203aac71d0d7b16a3] # # patch "sqlite/vdbeInt.h" # from [4b19fd8febad3fd14c4c97adaefc06754d323132] # to [7d2bf163d6d4e815724a457f2216dd8e38c3955c] # # patch "sqlite/vdbeapi.c" # from [1fca7ff056d03f131caa6b1296bb221da65ed7f4] # to [3747e4c3bc3139ff688bb3df462b10e42c084d16] # # patch "sqlite/vdbeaux.c" # from [ef59545f53f90394283f2fd003375d3ebbf0bd6e] # to [b4eda47b713aa8fbe70dce4922852fd48b919555] # # patch "sqlite/vdbemem.c" # from [981a113405bd9b80aeb71fe246a2f01708e8a8f7] # to [d86c25bbfe8102499ff7505fca44a779c68694d8] # # patch "sqlite/vtab.c" # from [89a0d5f39c1beba65a77fdb4d507b831fc5e6baf] # to [c5ebebf615b2f29499fbe97a584c4bb342632aa0] # # patch "sqlite/where.c" # from [0f17b7bed2ce50ba450e8f436d5ec8b420c4ab3f] # to [12387641659605318ae03d87f0687f223dfc9568] # ============================================================ --- sqlite/btreeInt.h ac1ab1fb624ffbe571786cd2bd9559f9ae336355 +++ sqlite/btreeInt.h ac1ab1fb624ffbe571786cd2bd9559f9ae336355 @@ -0,0 +1,588 @@ +/* +** 2004 April 6 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** $Id: btreeInt.h,v 1.5 2007/06/15 12:06:59 drh Exp $ +** +** This file implements a external (disk-based) database using BTrees. +** For a detailed discussion of BTrees, refer to +** +** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3: +** "Sorting And Searching", pages 473-480. Addison-Wesley +** Publishing Company, Reading, Massachusetts. +** +** The basic idea is that each page of the file contains N database +** entries and N+1 pointers to subpages. +** +** ---------------------------------------------------------------- +** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) | +** ---------------------------------------------------------------- +** +** All of the keys on the page that Ptr(0) points to have values less +** than Key(0). All of the keys on page Ptr(1) and its subpages have +** values greater than Key(0) and less than Key(1). All of the keys +** on Ptr(N) and its subpages have values greater than Key(N-1). And +** so forth. +** +** Finding a particular key requires reading O(log(M)) pages from the +** disk where M is the number of entries in the tree. +** +** In this implementation, a single file can hold one or more separate +** BTrees. Each BTree is identified by the index of its root page. The +** key and data for any entry are combined to form the "payload". A +** fixed amount of payload can be carried directly on the database +** page. If the payload is larger than the preset amount then surplus +** bytes are stored on overflow pages. The payload for an entry +** and the preceding pointer are combined to form a "Cell". Each +** page has a small header which contains the Ptr(N) pointer and other +** information such as the size of key and data. +** +** FORMAT DETAILS +** +** The file is divided into pages. The first page is called page 1, +** the second is page 2, and so forth. A page number of zero indicates +** "no such page". The page size can be anything between 512 and 65536. +** Each page can be either a btree page, a freelist page or an overflow +** page. +** +** The first page is always a btree page. The first 100 bytes of the first +** page contain a special header (the "file header") that describes the file. +** The format of the file header is as follows: +** +** OFFSET SIZE DESCRIPTION +** 0 16 Header string: "SQLite format 3\000" +** 16 2 Page size in bytes. +** 18 1 File format write version +** 19 1 File format read version +** 20 1 Bytes of unused space at the end of each page +** 21 1 Max embedded payload fraction +** 22 1 Min embedded payload fraction +** 23 1 Min leaf payload fraction +** 24 4 File change counter +** 28 4 Reserved for future use +** 32 4 First freelist page +** 36 4 Number of freelist pages in the file +** 40 60 15 4-byte meta values passed to higher layers +** +** All of the integer values are big-endian (most significant byte first). +** +** The file change counter is incremented when the database is changed +** This counter allows other processes to know when the file has changed +** and thus when they need to flush their cache. +** +** The max embedded payload fraction is the amount of the total usable +** space in a page that can be consumed by a single cell for standard +** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default +** is to limit the maximum cell size so that at least 4 cells will fit +** on one page. Thus the default max embedded payload fraction is 64. +** +** If the payload for a cell is larger than the max payload, then extra +** payload is spilled to overflow pages. Once an overflow page is allocated, +** as many bytes as possible are moved into the overflow pages without letting +** the cell size drop below the min embedded payload fraction. +** +** The min leaf payload fraction is like the min embedded payload fraction +** except that it applies to leaf nodes in a LEAFDATA tree. The maximum +** payload fraction for a LEAFDATA tree is always 100% (or 255) and it +** not specified in the header. +** +** Each btree pages is divided into three sections: The header, the +** cell pointer array, and the cell content area. Page 1 also has a 100-byte +** file header that occurs before the page header. +** +** |----------------| +** | file header | 100 bytes. Page 1 only. +** |----------------| +** | page header | 8 bytes for leaves. 12 bytes for interior nodes +** |----------------| +** | cell pointer | | 2 bytes per cell. Sorted order. +** | array | | Grows downward +** | | v +** |----------------| +** | unallocated | +** | space | +** |----------------| ^ Grows upwards +** | cell content | | Arbitrary order interspersed with freeblocks. +** | area | | and free space fragments. +** |----------------| +** +** The page headers looks like this: +** +** OFFSET SIZE DESCRIPTION +** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf +** 1 2 byte offset to the first freeblock +** 3 2 number of cells on this page +** 5 2 first byte of the cell content area +** 7 1 number of fragmented free bytes +** 8 4 Right child (the Ptr(N) value). Omitted on leaves. +** +** The flags define the format of this btree page. The leaf flag means that +** this page has no children. The zerodata flag means that this page carries +** only keys and no data. The intkey flag means that the key is a integer +** which is stored in the key size entry of the cell header rather than in +** the payload area. +** +** The cell pointer array begins on the first byte after the page header. +** The cell pointer array contains zero or more 2-byte numbers which are +** offsets from the beginning of the page to the cell content in the cell +** content area. The cell pointers occur in sorted order. The system strives +** to keep free space after the last cell pointer so that new cells can +** be easily added without having to defragment the page. +** +** Cell content is stored at the very end of the page and grows toward the +** beginning of the page. +** +** Unused space within the cell content area is collected into a linked list of +** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset +** to the first freeblock is given in the header. Freeblocks occur in +** increasing order. Because a freeblock must be at least 4 bytes in size, +** any group of 3 or fewer unused bytes in the cell content area cannot +** exist on the freeblock chain. A group of 3 or fewer free bytes is called +** a fragment. The total number of bytes in all fragments is recorded. +** in the page header at offset 7. +** +** SIZE DESCRIPTION +** 2 Byte offset of the next freeblock +** 2 Bytes in this freeblock +** +** Cells are of variable length. Cells are stored in the cell content area at +** the end of the page. Pointers to the cells are in the cell pointer array +** that immediately follows the page header. Cells is not necessarily +** contiguous or in order, but cell pointers are contiguous and in order. +** +** Cell content makes use of variable length integers. A variable +** length integer is 1 to 9 bytes where the lower 7 bits of each +** byte are used. The integer consists of all bytes that have bit 8 set and +** the first byte with bit 8 clear. The most significant byte of the integer +** appears first. A variable-length integer may not be more than 9 bytes long. +** As a special case, all 8 bytes of the 9th byte are used as data. This +** allows a 64-bit integer to be encoded in 9 bytes. +** +** 0x00 becomes 0x00000000 +** 0x7f becomes 0x0000007f +** 0x81 0x00 becomes 0x00000080 +** 0x82 0x00 becomes 0x00000100 +** 0x80 0x7f becomes 0x0000007f +** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678 +** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081 +** +** Variable length integers are used for rowids and to hold the number of +** bytes of key and data in a btree cell. +** +** The content of a cell looks like this: +** +** SIZE DESCRIPTION +** 4 Page number of the left child. Omitted if leaf flag is set. +** var Number of bytes of data. Omitted if the zerodata flag is set. +** var Number of bytes of key. Or the key itself if intkey flag is set. +** * Payload +** 4 First page of the overflow chain. Omitted if no overflow +** +** Overflow pages form a linked list. Each page except the last is completely +** filled with data (pagesize - 4 bytes). The last page can have as little +** as 1 byte of data. +** +** SIZE DESCRIPTION +** 4 Page number of next overflow page +** * Data +** +** Freelist pages come in two subtypes: trunk pages and leaf pages. The +** file header points to the first in a linked list of trunk page. Each trunk +** page points to multiple leaf pages. The content of a leaf page is +** unspecified. A trunk page looks like this: +** +** SIZE DESCRIPTION +** 4 Page number of next trunk page +** 4 Number of leaf pointers on this page +** * zero or more pages numbers of leaves +*/ +#include "sqliteInt.h" +#include "pager.h" +#include "btree.h" +#include "os.h" +#include + +/* Round up a number to the next larger multiple of 8. This is used +** to force 8-byte alignment on 64-bit architectures. +*/ +#define ROUND8(x) ((x+7)&~7) + + +/* The following value is the maximum cell size assuming a maximum page +** size give above. +*/ +#define MX_CELL_SIZE(pBt) (pBt->pageSize-8) + +/* The maximum number of cells on a single page of the database. This +** assumes a minimum cell size of 3 bytes. Such small cells will be +** exceedingly rare, but they are possible. +*/ +#define MX_CELL(pBt) ((pBt->pageSize-8)/3) + +/* Forward declarations */ +typedef struct MemPage MemPage; +typedef struct BtLock BtLock; + +/* +** This is a magic string that appears at the beginning of every +** SQLite database in order to identify the file as a real database. +** +** You can change this value at compile-time by specifying a +** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The +** header must be exactly 16 bytes including the zero-terminator so +** the string itself should be 15 characters long. If you change +** the header, then your custom library will not be able to read +** databases generated by the standard tools and the standard tools +** will not be able to read databases created by your custom library. +*/ +#ifndef SQLITE_FILE_HEADER /* 123456789 123456 */ +# define SQLITE_FILE_HEADER "SQLite format 3" +#endif + +/* +** Page type flags. An ORed combination of these flags appear as the +** first byte of every BTree page. +*/ +#define PTF_INTKEY 0x01 +#define PTF_ZERODATA 0x02 +#define PTF_LEAFDATA 0x04 +#define PTF_LEAF 0x08 + +/* +** As each page of the file is loaded into memory, an instance of the following +** structure is appended and initialized to zero. This structure stores +** information about the page that is decoded from the raw file page. +** +** The pParent field points back to the parent page. This allows us to +** walk up the BTree from any leaf to the root. Care must be taken to +** unref() the parent page pointer when this page is no longer referenced. +** The pageDestructor() routine handles that chore. +*/ +struct MemPage { + u8 isInit; /* True if previously initialized. MUST BE FIRST! */ + u8 idxShift; /* True if Cell indices have changed */ + u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ + u8 intKey; /* True if intkey flag is set */ + u8 leaf; /* True if leaf flag is set */ + u8 zeroData; /* True if table stores keys only */ + u8 leafData; /* True if tables stores data on leaves only */ + u8 hasData; /* True if this page stores data */ + u8 hdrOffset; /* 100 for page 1. 0 otherwise */ + u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */ + u16 maxLocal; /* Copy of Btree.maxLocal or Btree.maxLeaf */ + u16 minLocal; /* Copy of Btree.minLocal or Btree.minLeaf */ + u16 cellOffset; /* Index in aData of first cell pointer */ + u16 idxParent; /* Index in parent of this node */ + u16 nFree; /* Number of free bytes on the page */ + u16 nCell; /* Number of cells on this page, local and ovfl */ + struct _OvflCell { /* Cells that will not fit on aData[] */ + u8 *pCell; /* Pointers to the body of the overflow cell */ + u16 idx; /* Insert this cell before idx-th non-overflow cell */ + } aOvfl[5]; + BtShared *pBt; /* Pointer back to BTree structure */ + u8 *aData; /* Pointer back to the start of the page */ + DbPage *pDbPage; /* Pager page handle */ + Pgno pgno; /* Page number for this page */ + MemPage *pParent; /* The parent of this page. NULL for root */ +}; + +/* +** The in-memory image of a disk page has the auxiliary information appended +** to the end. EXTRA_SIZE is the number of bytes of space needed to hold +** that extra information. +*/ +#define EXTRA_SIZE sizeof(MemPage) + +/* Btree handle */ +struct Btree { + sqlite3 *pSqlite; + BtShared *pBt; + u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */ +}; + +/* +** Btree.inTrans may take one of the following values. +** +** If the shared-data extension is enabled, there may be multiple users +** of the Btree structure. At most one of these may open a write transaction, +** but any number may have active read transactions. Variable Btree.pDb +** points to the handle that owns any current write-transaction. +*/ +#define TRANS_NONE 0 +#define TRANS_READ 1 +#define TRANS_WRITE 2 + +/* +** Everything we need to know about an open database +*/ +struct BtShared { + Pager *pPager; /* The page cache */ + BtCursor *pCursor; /* A list of all open cursors */ + MemPage *pPage1; /* First page of the database */ + u8 inStmt; /* True if we are in a statement subtransaction */ + u8 readOnly; /* True if the underlying file is readonly */ + u8 maxEmbedFrac; /* Maximum payload as % of total page size */ + u8 minEmbedFrac; /* Minimum payload as % of total page size */ + u8 minLeafFrac; /* Minimum leaf payload as % of total page size */ + u8 pageSizeFixed; /* True if the page size can no longer be changed */ +#ifndef SQLITE_OMIT_AUTOVACUUM + u8 autoVacuum; /* True if auto-vacuum is enabled */ + u8 incrVacuum; /* True if incr-vacuum is enabled */ + Pgno nTrunc; /* Non-zero if the db will be truncated (incr vacuum) */ +#endif + u16 pageSize; /* Total number of bytes on a page */ + u16 usableSize; /* Number of usable bytes on each page */ + int maxLocal; /* Maximum local payload in non-LEAFDATA tables */ + int minLocal; /* Minimum local payload in non-LEAFDATA tables */ + int maxLeaf; /* Maximum local payload in a LEAFDATA table */ + int minLeaf; /* Minimum local payload in a LEAFDATA table */ + BusyHandler *pBusyHandler; /* Callback for when there is lock contention */ + u8 inTransaction; /* Transaction state */ + int nRef; /* Number of references to this structure */ + int nTransaction; /* Number of open transactions (read + write) */ + void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */ + void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */ +#ifndef SQLITE_OMIT_SHARED_CACHE + BtLock *pLock; /* List of locks held on this shared-btree struct */ + BtShared *pNext; /* Next in ThreadData.pBtree linked list */ +#endif +}; + +/* +** An instance of the following structure is used to hold information +** about a cell. The parseCellPtr() function fills in this structure +** based on information extract from the raw disk page. +*/ +typedef struct CellInfo CellInfo; +struct CellInfo { + u8 *pCell; /* Pointer to the start of cell content */ + i64 nKey; /* The key for INTKEY tables, or number of bytes in key */ + u32 nData; /* Number of bytes of data */ + u32 nPayload; /* Total amount of payload */ + u16 nHeader; /* Size of the cell content header in bytes */ + u16 nLocal; /* Amount of payload held locally */ + u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */ + u16 nSize; /* Size of the cell content on the main b-tree page */ +}; + +/* +** A cursor is a pointer to a particular entry in the BTree. +** The entry is identified by its MemPage and the index in +** MemPage.aCell[] of the entry. +*/ +struct BtCursor { + Btree *pBtree; /* The Btree to which this cursor belongs */ + BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */ + int (*xCompare)(void*,int,const void*,int,const void*); /* Key comp func */ + void *pArg; /* First arg to xCompare() */ + Pgno pgnoRoot; /* The root page of this tree */ + MemPage *pPage; /* Page that contains the entry */ + int idx; /* Index of the entry in pPage->aCell[] */ + CellInfo info; /* A parse of the cell we are pointing at */ + u8 wrFlag; /* True if writable */ + u8 eState; /* One of the CURSOR_XXX constants (see below) */ + void *pKey; /* Saved key that was cursor's last known position */ + i64 nKey; /* Size of pKey, or last integer key */ + int skip; /* (skip<0) -> Prev() is a no-op. (skip>0) -> Next() is */ +#ifndef SQLITE_OMIT_INCRBLOB + u8 isIncrblobHandle; /* True if this cursor is an incr. io handle */ + Pgno *aOverflow; /* Cache of overflow page locations */ +#endif +}; + +/* +** Potential values for BtCursor.eState. +** +** CURSOR_VALID: +** Cursor points to a valid entry. getPayload() etc. may be called. +** +** CURSOR_INVALID: +** Cursor does not point to a valid entry. This can happen (for example) +** because the table is empty or because BtreeCursorFirst() has not been +** called. +** +** CURSOR_REQUIRESEEK: +** The table that this cursor was opened on still exists, but has been +** modified since the cursor was last used. The cursor position is saved +** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in +** this state, restoreOrClearCursorPosition() can be called to attempt to +** seek the cursor to the saved position. +*/ +#define CURSOR_INVALID 0 +#define CURSOR_VALID 1 +#define CURSOR_REQUIRESEEK 2 + +/* +** The TRACE macro will print high-level status information about the +** btree operation when the global variable sqlite3_btree_trace is +** enabled. +*/ +#if SQLITE_TEST +# define TRACE(X) if( sqlite3_btree_trace ){ printf X; fflush(stdout); } +#else +# define TRACE(X) +#endif + +/* +** Routines to read and write variable-length integers. These used to +** be defined locally, but now we use the varint routines in the util.c +** file. +*/ +#define getVarint sqlite3GetVarint +#define getVarint32(A,B) ((*B=*(A))<=0x7f?1:sqlite3GetVarint32(A,B)) +#define putVarint sqlite3PutVarint + +/* The database page the PENDING_BYTE occupies. This page is never used. +** TODO: This macro is very similary to PAGER_MJ_PGNO() in pager.c. They +** should possibly be consolidated (presumably in pager.h). +** +** If disk I/O is omitted (meaning that the database is stored purely +** in memory) then there is no pending byte. +*/ +#ifdef SQLITE_OMIT_DISKIO +# define PENDING_BYTE_PAGE(pBt) 0x7fffffff +#else +# define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1) +#endif + +/* +** A linked list of the following structures is stored at BtShared.pLock. +** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor +** is opened on the table with root page BtShared.iTable. Locks are removed +** from this list when a transaction is committed or rolled back, or when +** a btree handle is closed. +*/ +struct BtLock { + Btree *pBtree; /* Btree handle holding this lock */ + Pgno iTable; /* Root page of table */ + u8 eLock; /* READ_LOCK or WRITE_LOCK */ + BtLock *pNext; /* Next in BtShared.pLock list */ +}; + +/* Candidate values for BtLock.eLock */ +#define READ_LOCK 1 +#define WRITE_LOCK 2 + +/* +** These macros define the location of the pointer-map entry for a +** database page. The first argument to each is the number of usable +** bytes on each page of the database (often 1024). The second is the +** page number to look up in the pointer map. +** +** PTRMAP_PAGENO returns the database page number of the pointer-map +** page that stores the required pointer. PTRMAP_PTROFFSET returns +** the offset of the requested map entry. +** +** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page, +** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be +** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements +** this test. +*/ +#define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno) +#define PTRMAP_PTROFFSET(pBt, pgno) (5*(pgno-ptrmapPageno(pBt, pgno)-1)) +#define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno)) + +/* +** The pointer map is a lookup table that identifies the parent page for +** each child page in the database file. The parent page is the page that +** contains a pointer to the child. Every page in the database contains +** 0 or 1 parent pages. (In this context 'database page' refers +** to any page that is not part of the pointer map itself.) Each pointer map +** entry consists of a single byte 'type' and a 4 byte parent page number. +** The PTRMAP_XXX identifiers below are the valid types. +** +** The purpose of the pointer map is to facility moving pages from one +** position in the file to another as part of autovacuum. When a page +** is moved, the pointer in its parent must be updated to point to the +** new location. The pointer map is used to locate the parent page quickly. +** +** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not +** used in this case. +** +** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number +** is not used in this case. +** +** PTRMAP_OVERFLOW1: The database page is the first page in a list of +** overflow pages. The page number identifies the page that +** contains the cell with a pointer to this overflow page. +** +** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of +** overflow pages. The page-number identifies the previous +** page in the overflow page list. +** +** PTRMAP_BTREE: The database page is a non-root btree page. The page number +** identifies the parent page in the btree. +*/ +#define PTRMAP_ROOTPAGE 1 +#define PTRMAP_FREEPAGE 2 +#define PTRMAP_OVERFLOW1 3 +#define PTRMAP_OVERFLOW2 4 +#define PTRMAP_BTREE 5 + +/* A bunch of assert() statements to check the transaction state variables +** of handle p (type Btree*) are internally consistent. +*/ +#define btreeIntegrity(p) \ + assert( p->inTrans!=TRANS_NONE || p->pBt->nTransactionpBt->nRef ); \ + assert( p->pBt->nTransaction<=p->pBt->nRef ); \ + assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \ + assert( p->pBt->inTransaction>=p->inTrans ); + + +/* +** The ISAUTOVACUUM macro is used within balance_nonroot() to determine +** if the database supports auto-vacuum or not. Because it is used +** within an expression that is an argument to another macro +** (sqliteMallocRaw), it is not possible to use conditional compilation. +** So, this macro is defined instead. +*/ +#ifndef SQLITE_OMIT_AUTOVACUUM +#define ISAUTOVACUUM (pBt->autoVacuum) +#else +#define ISAUTOVACUUM 0 +#endif + + +/* +** This structure is passed around through all the sanity checking routines +** in order to keep track of some global state information. +*/ +typedef struct IntegrityCk IntegrityCk; +struct IntegrityCk { + BtShared *pBt; /* The tree being checked out */ + Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */ + int nPage; /* Number of pages in the database */ + int *anRef; /* Number of times each page is referenced */ + int mxErr; /* Stop accumulating errors when this reaches zero */ + char *zErrMsg; /* An error message. NULL if no errors seen. */ + int nErr; /* Number of messages written to zErrMsg so far */ +}; + +/* +** Read or write a two- and four-byte big-endian integer values. +*/ +#define get2byte(x) ((x)[0]<<8 | (x)[1]) +#define put2byte(p,v) ((p)[0] = (v)>>8, (p)[1] = (v)) +#define get4byte sqlite3Get4byte +#define put4byte sqlite3Put4byte + +/* +** Internal routines that should be accessed by the btree layer only. +*/ +int sqlite3BtreeGetPage(BtShared*, Pgno, MemPage**, int); +int sqlite3BtreeInitPage(MemPage *pPage, MemPage *pParent); +void sqlite3BtreeParseCellPtr(MemPage*, u8*, CellInfo*); +void sqlite3BtreeParseCell(MemPage*, int, CellInfo*); +u8 *sqlite3BtreeFindCell(MemPage *pPage, int iCell); +int sqlite3BtreeRestoreOrClearCursorPosition(BtCursor *pCur); +void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur); +void sqlite3BtreeReleaseTempCursor(BtCursor *pCur); +int sqlite3BtreeIsRootPage(MemPage *pPage); +void sqlite3BtreeMoveToParent(BtCursor *pCur); ============================================================ --- sqlite/limits.h 71ab25f17e35e0a9f3f6f234b8ed49cc56731d35 +++ sqlite/limits.h 71ab25f17e35e0a9f3f6f234b8ed49cc56731d35 @@ -0,0 +1,158 @@ +/* +** 2007 May 7 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file defines various limits of what SQLite can process. +** +** @(#) $Id: limits.h,v 1.9 2007/06/09 09:53:51 drh Exp $ +*/ + +/* +** The maximum length of a TEXT or BLOB in bytes. This also +** limits the size of a row in a table or index. +** +** The hard limit is the ability of a 32-bit signed integer +** to count the size: 2^31-1 or 2147483647. +*/ +#ifndef SQLITE_MAX_LENGTH +# define SQLITE_MAX_LENGTH 1000000000 +#endif + +/* +** This is the maximum number of +** +** * Columns in a table +** * Columns in an index +** * Columns in a view +** * Terms in the SET clause of an UPDATE statement +** * Terms in the result set of a SELECT statement +** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement. +** * Terms in the VALUES clause of an INSERT statement +** +** The hard upper limit here is 32676. Most database people will +** tell you that in a well-normalized database, you usually should +** not have more than a dozen or so columns in any table. And if +** that is the case, there is no point in having more than a few +** dozen values in any of the other situations described above. +*/ +#ifndef SQLITE_MAX_COLUMN +# define SQLITE_MAX_COLUMN 2000 +#endif + +/* +** The maximum length of a single SQL statement in bytes. +** The hard limit here is the same as SQLITE_MAX_LENGTH. +*/ +#ifndef SQLITE_MAX_SQL_LENGTH +# define SQLITE_MAX_SQL_LENGTH 1000000 +#endif + +/* +** The maximum depth of an expression tree. This is limited to +** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might +** want to place more severe limits on the complexity of an +** expression. A value of 0 (the default) means do not enforce +** any limitation on expression tree depth. +*/ +#ifndef SQLITE_MAX_EXPR_DEPTH +# define SQLITE_MAX_EXPR_DEPTH 1000 +#endif + +/* +** The maximum number of terms in a compound SELECT statement. +** The code generator for compound SELECT statements does one +** level of recursion for each term. A stack overflow can result +** if the number of terms is too large. In practice, most SQL +** never has more than 3 or 4 terms. Use a value of 0 to disable +** any limit on the number of terms in a compount SELECT. +*/ +#ifndef SQLITE_MAX_COMPOUND_SELECT +# define SQLITE_MAX_COMPOUND_SELECT 500 +#endif + +/* +** The maximum number of opcodes in a VDBE program. +** Not currently enforced. +*/ +#ifndef SQLITE_MAX_VDBE_OP +# define SQLITE_MAX_VDBE_OP 25000 +#endif + +/* +** The maximum number of arguments to an SQL function. +*/ +#ifndef SQLITE_MAX_FUNCTION_ARG +# define SQLITE_MAX_FUNCTION_ARG 100 +#endif + +/* +** The maximum number of in-memory pages to use for the main database +** table and for temporary tables. The SQLITE_DEFAULT_CACHE_SIZE +*/ +#ifndef SQLITE_DEFAULT_CACHE_SIZE +# define SQLITE_DEFAULT_CACHE_SIZE 2000 +#endif +#ifndef SQLITE_DEFAULT_TEMP_CACHE_SIZE +# define SQLITE_DEFAULT_TEMP_CACHE_SIZE 500 +#endif + +/* +** The maximum number of attached databases. This must be at least 2 +** in order to support the main database file (0) and the file used to +** hold temporary tables (1). And it must be less than 32 because +** we use a bitmask of databases with a u32 in places (for example +** the Parse.cookieMask field). +*/ +#ifndef SQLITE_MAX_ATTACHED +# define SQLITE_MAX_ATTACHED 10 +#endif + + +/* +** The maximum value of a ?nnn wildcard that the parser will accept. +*/ +#ifndef SQLITE_MAX_VARIABLE_NUMBER +# define SQLITE_MAX_VARIABLE_NUMBER 999 +#endif + +/* +** The default size of a database page. +*/ +#ifndef SQLITE_DEFAULT_PAGE_SIZE +# define SQLITE_DEFAULT_PAGE_SIZE 1024 +#endif + +/* Maximum page size. The upper bound on this value is 32768. This a limit +** imposed by the necessity of storing the value in a 2-byte unsigned integer +** and the fact that the page size must be a power of 2. +*/ +#ifndef SQLITE_MAX_PAGE_SIZE +# define SQLITE_MAX_PAGE_SIZE 32768 +#endif + +/* +** Maximum number of pages in one database file. +** +** This is really just the default value for the max_page_count pragma. +** This value can be lowered (or raised) at run-time using that the +** max_page_count macro. +*/ +#ifndef SQLITE_MAX_PAGE_COUNT +# define SQLITE_MAX_PAGE_COUNT 1073741823 +#endif + +/* +** Maximum length (in bytes) of the pattern in a LIKE or GLOB +** operator. +*/ +#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH +# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000 +#endif ============================================================ --- sqlite/malloc.c fa9bbccc4e6d099cd04c2518d238a1669c9d1020 +++ sqlite/malloc.c fa9bbccc4e6d099cd04c2518d238a1669c9d1020 @@ -0,0 +1,835 @@ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** Memory allocation functions used throughout sqlite. +** +** +** $Id: malloc.c,v 1.3 2007/06/15 20:29:20 drh Exp $ +*/ +#include "sqliteInt.h" +#include "os.h" +#include +#include + +/* +** MALLOC WRAPPER ARCHITECTURE +** +** The sqlite code accesses dynamic memory allocation/deallocation by invoking +** the following six APIs (which may be implemented as macros). +** +** sqlite3Malloc() +** sqlite3MallocRaw() +** sqlite3Realloc() +** sqlite3ReallocOrFree() +** sqlite3Free() +** sqlite3AllocSize() +** +** The function sqlite3FreeX performs the same task as sqlite3Free and is +** guaranteed to be a real function. The same holds for sqlite3MallocX +** +** The above APIs are implemented in terms of the functions provided in the +** operating-system interface. The OS interface is never accessed directly +** by code outside of this file. +** +** sqlite3OsMalloc() +** sqlite3OsRealloc() +** sqlite3OsFree() +** sqlite3OsAllocationSize() +** +** Functions sqlite3MallocRaw() and sqlite3Realloc() may invoke +** sqlite3_release_memory() if a call to sqlite3OsMalloc() or +** sqlite3OsRealloc() fails (or if the soft-heap-limit for the thread is +** exceeded). Function sqlite3Malloc() usually invokes +** sqlite3MallocRaw(). +** +** MALLOC TEST WRAPPER ARCHITECTURE +** +** The test wrapper provides extra test facilities to ensure the library +** does not leak memory and handles the failure of the underlying OS level +** allocation system correctly. It is only present if the library is +** compiled with the SQLITE_MEMDEBUG macro set. +** +** * Guardposts to detect overwrites. +** * Ability to cause a specific Malloc() or Realloc() to fail. +** * Audit outstanding memory allocations (i.e check for leaks). +*/ + +#define MAX(x,y) ((x)>(y)?(x):(y)) + +#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO) +/* +** Set the soft heap-size limit for the current thread. Passing a negative +** value indicates no limit. +*/ +void sqlite3_soft_heap_limit(int n){ + ThreadData *pTd = sqlite3ThreadData(); + if( pTd ){ + pTd->nSoftHeapLimit = n; + } + sqlite3ReleaseThreadData(); +} + +/* +** Release memory held by SQLite instances created by the current thread. +*/ +int sqlite3_release_memory(int n){ + return sqlite3PagerReleaseMemory(n); +} +#else +/* If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, then define a version +** of sqlite3_release_memory() to be used by other code in this file. +** This is done for no better reason than to reduce the number of +** pre-processor #ifndef statements. +*/ +#define sqlite3_release_memory(x) 0 /* 0 == no memory freed */ +#endif + +#ifdef SQLITE_MEMDEBUG +/*-------------------------------------------------------------------------- +** Begin code for memory allocation system test layer. +** +** Memory debugging is turned on by defining the SQLITE_MEMDEBUG macro. +** +** SQLITE_MEMDEBUG==1 -> Fence-posting only (thread safe) +** SQLITE_MEMDEBUG==2 -> Fence-posting + linked list of allocations (not ts) +** SQLITE_MEMDEBUG==3 -> Above + backtraces (not thread safe, req. glibc) +*/ + +/* Figure out whether or not to store backtrace() information for each malloc. +** The backtrace() function is only used if SQLITE_MEMDEBUG is set to 2 or +** greater and glibc is in use. If we don't want to use backtrace(), then just +** define it as an empty macro and set the amount of space reserved to 0. +*/ +#if defined(__GLIBC__) && SQLITE_MEMDEBUG>2 + extern int backtrace(void **, int); + #define TESTALLOC_STACKSIZE 128 + #define TESTALLOC_STACKFRAMES ((TESTALLOC_STACKSIZE-8)/sizeof(void*)) +#else + #define backtrace(x, y) + #define TESTALLOC_STACKSIZE 0 + #define TESTALLOC_STACKFRAMES 0 +#endif + +/* +** Number of 32-bit guard words. This should probably be a multiple of +** 2 since on 64-bit machines we want the value returned by sqliteMalloc() +** to be 8-byte aligned. +*/ +#ifndef TESTALLOC_NGUARD +# define TESTALLOC_NGUARD 2 +#endif + +/* +** Size reserved for storing file-name along with each malloc()ed blob. +*/ +#define TESTALLOC_FILESIZE 64 + +/* +** Size reserved for storing the user string. Each time a Malloc() or Realloc() +** call succeeds, up to TESTALLOC_USERSIZE bytes of the string pointed to by +** sqlite3_malloc_id are stored along with the other test system metadata. +*/ +#define TESTALLOC_USERSIZE 64 +const char *sqlite3_malloc_id = 0; + +/* +** Blocks used by the test layer have the following format: +** +** +** +** +** +** +** <32-bit line number> +** +** +*/ + +#define TESTALLOC_OFFSET_GUARD1(p) (sizeof(void *) * 2) +#define TESTALLOC_OFFSET_DATA(p) ( \ + TESTALLOC_OFFSET_GUARD1(p) + sizeof(u32) * TESTALLOC_NGUARD \ +) +#define TESTALLOC_OFFSET_GUARD2(p) ( \ + TESTALLOC_OFFSET_DATA(p) + sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD \ +) +#define TESTALLOC_OFFSET_LINENUMBER(p) ( \ + TESTALLOC_OFFSET_GUARD2(p) + sizeof(u32) * TESTALLOC_NGUARD \ +) +#define TESTALLOC_OFFSET_FILENAME(p) ( \ + TESTALLOC_OFFSET_LINENUMBER(p) + sizeof(u32) \ +) +#define TESTALLOC_OFFSET_USER(p) ( \ + TESTALLOC_OFFSET_FILENAME(p) + TESTALLOC_FILESIZE \ +) +#define TESTALLOC_OFFSET_STACK(p) ( \ + TESTALLOC_OFFSET_USER(p) + TESTALLOC_USERSIZE + 8 - \ + (TESTALLOC_OFFSET_USER(p) % 8) \ +) + +#define TESTALLOC_OVERHEAD ( \ + sizeof(void *)*2 + /* pPrev and pNext pointers */ \ + TESTALLOC_NGUARD*sizeof(u32)*2 + /* Guard words */ \ + sizeof(u32) + TESTALLOC_FILESIZE + /* File and line number */ \ + TESTALLOC_USERSIZE + /* User string */ \ + TESTALLOC_STACKSIZE /* backtrace() stack */ \ +) + + +/* +** For keeping track of the number of mallocs and frees. This +** is used to check for memory leaks. The iMallocFail and iMallocReset +** values are used to simulate malloc() failures during testing in +** order to verify that the library correctly handles an out-of-memory +** condition. +*/ +int sqlite3_nMalloc; /* Number of sqliteMalloc() calls */ +int sqlite3_nFree; /* Number of sqliteFree() calls */ +int sqlite3_memUsed; /* TODO Total memory obtained from malloc */ +int sqlite3_memMax; /* TODO Mem usage high-water mark */ +int sqlite3_iMallocFail; /* Fail sqliteMalloc() after this many calls */ +int sqlite3_iMallocReset = -1; /* When iMallocFail reaches 0, set to this */ + +void *sqlite3_pFirst = 0; /* Pointer to linked list of allocations */ +int sqlite3_nMaxAlloc = 0; /* High water mark of ThreadData.nAlloc */ +int sqlite3_mallocDisallowed = 0; /* assert() in sqlite3Malloc() if set */ +int sqlite3_isFail = 0; /* True if all malloc calls should fail */ +const char *sqlite3_zFile = 0; /* Filename to associate debug info with */ +int sqlite3_iLine = 0; /* Line number for debug info */ +int sqlite3_mallocfail_trace = 0; /* Print a msg on malloc fail if true */ + +/* +** Check for a simulated memory allocation failure. Return true if +** the failure should be simulated. Return false to proceed as normal. +*/ +int sqlite3TestMallocFail(){ + if( sqlite3_isFail ){ + return 1; + } + if( sqlite3_iMallocFail>=0 ){ + sqlite3_iMallocFail--; + if( sqlite3_iMallocFail==0 ){ + sqlite3_iMallocFail = sqlite3_iMallocReset; + sqlite3_isFail = 1; + if( sqlite3_mallocfail_trace ){ + sqlite3DebugPrintf("###_malloc_fails_###\n"); + } + return 1; + } + } + return 0; +} + +/* +** The argument is a pointer returned by sqlite3OsMalloc() or xRealloc(). +** assert() that the first and last (TESTALLOC_NGUARD*4) bytes are set to the +** values set by the applyGuards() function. +*/ +static void checkGuards(u32 *p) +{ + int i; + char *zAlloc = (char *)p; + char *z; + + /* First set of guard words */ + z = &zAlloc[TESTALLOC_OFFSET_GUARD1(p)]; + for(i=0; i1 +/* +** The argument points to an Os level allocation. Link it into the threads list +** of allocations. +*/ +static void linkAlloc(void *p){ + void **pp = (void **)p; + pp[0] = 0; + pp[1] = sqlite3_pFirst; + if( sqlite3_pFirst ){ + ((void **)sqlite3_pFirst)[0] = p; + } + sqlite3_pFirst = p; +} + +/* +** The argument points to an Os level allocation. Unlinke it from the threads +** list of allocations. +*/ +static void unlinkAlloc(void *p) +{ + void **pp = (void **)p; + if( p==sqlite3_pFirst ){ + assert(!pp[0]); + assert(!pp[1] || ((void **)(pp[1]))[0]==p); + sqlite3_pFirst = pp[1]; + if( sqlite3_pFirst ){ + ((void **)sqlite3_pFirst)[0] = 0; + } + }else{ + void **pprev = pp[0]; + void **pnext = pp[1]; + assert(pprev); + assert(pprev[1]==p); + pprev[1] = (void *)pnext; + if( pnext ){ + assert(pnext[0]==p); + pnext[0] = (void *)pprev; + } + } +} + +/* +** Pointer p is a pointer to an OS level allocation that has just been +** realloc()ed. Set the list pointers that point to this entry to it's new +** location. +*/ +static void relinkAlloc(void *p) +{ + void **pp = (void **)p; + if( pp[0] ){ + ((void **)(pp[0]))[1] = p; + }else{ + sqlite3_pFirst = p; + } + if( pp[1] ){ + ((void **)(pp[1]))[0] = p; + } +} +#else +#define linkAlloc(x) +#define relinkAlloc(x) +#define unlinkAlloc(x) +#endif + +/* +** This function sets the result of the Tcl interpreter passed as an argument +** to a list containing an entry for each currently outstanding call made to +** sqliteMalloc and friends by the current thread. Each list entry is itself a +** list, consisting of the following (in order): +** +** * The number of bytes allocated +** * The __FILE__ macro at the time of the sqliteMalloc() call. +** * The __LINE__ macro ... +** * The value of the sqlite3_malloc_id variable ... +** * The output of backtrace() (if available) ... +** +** Todo: We could have a version of this function that outputs to stdout, +** to debug memory leaks when Tcl is not available. +*/ +#if defined(TCLSH) && defined(SQLITE_DEBUG) && SQLITE_MEMDEBUG>1 +#include +int sqlite3OutstandingMallocs(Tcl_Interp *interp){ + void *p; + Tcl_Obj *pRes = Tcl_NewObj(); + Tcl_IncrRefCount(pRes); + + + for(p=sqlite3_pFirst; p; p=((void **)p)[1]){ + Tcl_Obj *pEntry = Tcl_NewObj(); + Tcl_Obj *pStack = Tcl_NewObj(); + char *z; + u32 iLine; + int nBytes = sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD; + char *zAlloc = (char *)p; + int i; + + Tcl_ListObjAppendElement(0, pEntry, Tcl_NewIntObj(nBytes)); + + z = &zAlloc[TESTALLOC_OFFSET_FILENAME(p)]; + Tcl_ListObjAppendElement(0, pEntry, Tcl_NewStringObj(z, -1)); + + z = &zAlloc[TESTALLOC_OFFSET_LINENUMBER(p)]; + memcpy(&iLine, z, sizeof(u32)); + Tcl_ListObjAppendElement(0, pEntry, Tcl_NewIntObj(iLine)); + + z = &zAlloc[TESTALLOC_OFFSET_USER(p)]; + Tcl_ListObjAppendElement(0, pEntry, Tcl_NewStringObj(z, -1)); + + z = &zAlloc[TESTALLOC_OFFSET_STACK(p)]; + for(i=0; inAlloc); +#endif + assert( !sqlite3_mallocDisallowed ); + if( !sqlite3TestMallocFail() ){ + u32 *p; + p = (u32 *)sqlite3OsMalloc(n + TESTALLOC_OVERHEAD); + assert(p); + sqlite3_nMalloc++; + applyGuards(p); + linkAlloc(p); + sqlite3OsLeaveMutex(); + return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]); + } + sqlite3OsLeaveMutex(); + return 0; +} + +static int OSSIZEOF(void *p){ + if( p ){ + u32 *pOs = (u32 *)getOsPointer(p); + return sqlite3OsAllocationSize(pOs) - TESTALLOC_OVERHEAD; + } + return 0; +} + +/* +** This is the test layer's wrapper around sqlite3OsFree(). The argument is a +** pointer to the space allocated for the application to use. +*/ +static void OSFREE(void *pFree){ + u32 *p; /* Pointer to the OS-layer allocation */ + sqlite3OsEnterMutex(); + p = (u32 *)getOsPointer(pFree); + checkGuards(p); + unlinkAlloc(p); + memset(pFree, 0x55, OSSIZEOF(pFree)); + sqlite3OsFree(p); + sqlite3_nFree++; + sqlite3OsLeaveMutex(); +} + +/* +** This is the test layer's wrapper around sqlite3OsRealloc(). +*/ +static void * OSREALLOC(void *pRealloc, int n){ +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT + sqlite3_nMaxAlloc = + MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc); +#endif + assert( !sqlite3_mallocDisallowed ); + if( !sqlite3TestMallocFail() ){ + u32 *p = (u32 *)getOsPointer(pRealloc); + checkGuards(p); + p = sqlite3OsRealloc(p, n + TESTALLOC_OVERHEAD); + applyGuards(p); + relinkAlloc(p); + return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]); + } + return 0; +} + +static void OSMALLOC_FAILED(){ + sqlite3_isFail = 0; +} + +#else +/* Define macros to call the sqlite3OsXXX interface directly if +** the SQLITE_MEMDEBUG macro is not defined. +*/ +#define OSMALLOC(x) sqlite3OsMalloc(x) +#define OSREALLOC(x,y) sqlite3OsRealloc(x,y) +#define OSFREE(x) sqlite3OsFree(x) +#define OSSIZEOF(x) sqlite3OsAllocationSize(x) +#define OSMALLOC_FAILED() + +#endif /* SQLITE_MEMDEBUG */ +/* +** End code for memory allocation system test layer. +**--------------------------------------------------------------------------*/ + +/* +** This routine is called when we are about to allocate n additional bytes +** of memory. If the new allocation will put is over the soft allocation +** limit, then invoke sqlite3_release_memory() to try to release some +** memory before continuing with the allocation. +** +** This routine also makes sure that the thread-specific-data (TSD) has +** be allocated. If it has not and can not be allocated, then return +** false. The updateMemoryUsedCount() routine below will deallocate +** the TSD if it ought to be. +** +** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is +** a no-op +*/ +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT +static int enforceSoftLimit(int n){ + ThreadData *pTsd = sqlite3ThreadData(); + if( pTsd==0 ){ + return 0; + } + assert( pTsd->nAlloc>=0 ); + if( n>0 && pTsd->nSoftHeapLimit>0 ){ + while( pTsd->nAlloc+n>pTsd->nSoftHeapLimit && sqlite3_release_memory(n) ){} + } + return 1; +} +#else +# define enforceSoftLimit(X) 1 +#endif + +/* +** Update the count of total outstanding memory that is held in +** thread-specific-data (TSD). If after this update the TSD is +** no longer being used, then deallocate it. +** +** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is +** a no-op +*/ +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT +static void updateMemoryUsedCount(int n){ + ThreadData *pTsd = sqlite3ThreadData(); + if( pTsd ){ + pTsd->nAlloc += n; + assert( pTsd->nAlloc>=0 ); + if( pTsd->nAlloc==0 && pTsd->nSoftHeapLimit==0 ){ + sqlite3ReleaseThreadData(); + } + } +} +#else +#define updateMemoryUsedCount(x) /* no-op */ +#endif + +/* +** Allocate and return N bytes of uninitialised memory by calling +** sqlite3OsMalloc(). If the Malloc() call fails, attempt to free memory +** by calling sqlite3_release_memory(). +*/ +void *sqlite3MallocRaw(int n, int doMemManage){ + void *p = 0; + if( n>0 && !sqlite3MallocFailed() && (!doMemManage || enforceSoftLimit(n)) ){ + while( (p = OSMALLOC(n))==0 && sqlite3_release_memory(n) ){} + if( !p ){ + sqlite3FailedMalloc(); + OSMALLOC_FAILED(); + }else if( doMemManage ){ + updateMemoryUsedCount(OSSIZEOF(p)); + } + } + return p; +} + +/* +** Resize the allocation at p to n bytes by calling sqlite3OsRealloc(). The +** pointer to the new allocation is returned. If the Realloc() call fails, +** attempt to free memory by calling sqlite3_release_memory(). +*/ +void *sqlite3Realloc(void *p, int n){ + if( sqlite3MallocFailed() ){ + return 0; + } + + if( !p ){ + return sqlite3Malloc(n, 1); + }else{ + void *np = 0; +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT + int origSize = OSSIZEOF(p); +#endif + if( enforceSoftLimit(n - origSize) ){ + while( (np = OSREALLOC(p, n))==0 && sqlite3_release_memory(n) ){} + if( !np ){ + sqlite3FailedMalloc(); + OSMALLOC_FAILED(); + }else{ + updateMemoryUsedCount(OSSIZEOF(np) - origSize); + } + } + return np; + } +} + +/* +** Free the memory pointed to by p. p must be either a NULL pointer or a +** value returned by a previous call to sqlite3Malloc() or sqlite3Realloc(). +*/ +void sqlite3FreeX(void *p){ + if( p ){ + updateMemoryUsedCount(0 - OSSIZEOF(p)); + OSFREE(p); + } +} + +/* +** A version of sqliteMalloc() that is always a function, not a macro. +** Currently, this is used only to alloc to allocate the parser engine. +*/ +void *sqlite3MallocX(int n){ + return sqliteMalloc(n); +} + +/* +** sqlite3Malloc +** sqlite3ReallocOrFree +** +** These two are implemented as wrappers around sqlite3MallocRaw(), +** sqlite3Realloc() and sqlite3Free(). +*/ +void *sqlite3Malloc(int n, int doMemManage){ + void *p = sqlite3MallocRaw(n, doMemManage); + if( p ){ + memset(p, 0, n); + } + return p; +} +void *sqlite3ReallocOrFree(void *p, int n){ + void *pNew; + pNew = sqlite3Realloc(p, n); + if( !pNew ){ + sqlite3FreeX(p); + } + return pNew; +} + +/* +** sqlite3ThreadSafeMalloc() and sqlite3ThreadSafeFree() are used in those +** rare scenarios where sqlite may allocate memory in one thread and free +** it in another. They are exactly the same as sqlite3Malloc() and +** sqlite3Free() except that: +** +** * The allocated memory is not included in any calculations with +** respect to the soft-heap-limit, and +** +** * sqlite3ThreadSafeMalloc() must be matched with ThreadSafeFree(), +** not sqlite3Free(). Calling sqlite3Free() on memory obtained from +** ThreadSafeMalloc() will cause an error somewhere down the line. +*/ +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT +void *sqlite3ThreadSafeMalloc(int n){ + (void)ENTER_MALLOC; + return sqlite3Malloc(n, 0); +} +void sqlite3ThreadSafeFree(void *p){ + (void)ENTER_MALLOC; + if( p ){ + OSFREE(p); + } +} +#endif + + +/* +** Return the number of bytes allocated at location p. p must be either +** a NULL pointer (in which case 0 is returned) or a pointer returned by +** sqlite3Malloc(), sqlite3Realloc() or sqlite3ReallocOrFree(). +** +** The number of bytes allocated does not include any overhead inserted by +** any malloc() wrapper functions that may be called. So the value returned +** is the number of bytes that were available to SQLite using pointer p, +** regardless of how much memory was actually allocated. +*/ +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT +int sqlite3AllocSize(void *p){ + return OSSIZEOF(p); +} +#endif + +/* +** Make a copy of a string in memory obtained from sqliteMalloc(). These +** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This +** is because when memory debugging is turned on, these two functions are +** called via macros that record the current file and line number in the +** ThreadData structure. +*/ +char *sqlite3StrDup(const char *z){ + char *zNew; + int n; + if( z==0 ) return 0; + n = strlen(z)+1; + zNew = sqlite3MallocRaw(n, 1); + if( zNew ) memcpy(zNew, z, n); + return zNew; +} +char *sqlite3StrNDup(const char *z, int n){ + char *zNew; + if( z==0 ) return 0; + zNew = sqlite3MallocRaw(n+1, 1); + if( zNew ){ + memcpy(zNew, z, n); + zNew[n] = 0; + } + return zNew; +} + +/* +** Create a string from the 2nd and subsequent arguments (up to the +** first NULL argument), store the string in memory obtained from +** sqliteMalloc() and make the pointer indicated by the 1st argument +** point to that string. The 1st argument must either be NULL or +** point to memory obtained from sqliteMalloc(). +*/ +void sqlite3SetString(char **pz, ...){ + va_list ap; + int nByte; + const char *z; + char *zResult; + + assert( pz!=0 ); + nByte = 1; + va_start(ap, pz); + while( (z = va_arg(ap, const char*))!=0 ){ + nByte += strlen(z); + } + va_end(ap); + sqliteFree(*pz); + *pz = zResult = sqliteMallocRaw( nByte ); + if( zResult==0 ){ + return; + } + *zResult = 0; + va_start(ap, pz); + while( (z = va_arg(ap, const char*))!=0 ){ + int n = strlen(z); + memcpy(zResult, z, n); + zResult += n; + } + zResult[0] = 0; + va_end(ap); +} + + +/* +** This function must be called before exiting any API function (i.e. +** returning control to the user) that has called sqlite3Malloc or +** sqlite3Realloc. +** +** The returned value is normally a copy of the second argument to this +** function. However, if a malloc() failure has occured since the previous +** invocation SQLITE_NOMEM is returned instead. +** +** If the first argument, db, is not NULL and a malloc() error has occured, +** then the connection error-code (the value returned by sqlite3_errcode()) +** is set to SQLITE_NOMEM. +*/ +int sqlite3_mallocHasFailed = 0; +int sqlite3ApiExit(sqlite3* db, int rc){ + if( sqlite3MallocFailed() ){ + sqlite3_mallocHasFailed = 0; + sqlite3OsLeaveMutex(); + sqlite3Error(db, SQLITE_NOMEM, 0); + rc = SQLITE_NOMEM; + } + return rc & (db ? db->errMask : 0xff); +} + +/* +** Set the "malloc has failed" condition to true for this thread. +*/ +void sqlite3FailedMalloc(){ + if( !sqlite3MallocFailed() ){ + sqlite3OsEnterMutex(); + assert( sqlite3_mallocHasFailed==0 ); + sqlite3_mallocHasFailed = 1; + } +} + +#ifdef SQLITE_MEMDEBUG +/* +** This function sets a flag in the thread-specific-data structure that will +** cause an assert to fail if sqliteMalloc() or sqliteRealloc() is called. +*/ +void sqlite3MallocDisallow(){ + assert( sqlite3_mallocDisallowed>=0 ); + sqlite3_mallocDisallowed++; +} + +/* +** This function clears the flag set in the thread-specific-data structure set +** by sqlite3MallocDisallow(). +*/ +void sqlite3MallocAllow(){ + assert( sqlite3_mallocDisallowed>0 ); + sqlite3_mallocDisallowed--; +} +#endif ============================================================ --- sqlite/vdbeblob.c 96f3572fdc45eda5be06e6372b612bc30742d9f0 +++ sqlite/vdbeblob.c 96f3572fdc45eda5be06e6372b612bc30742d9f0 @@ -0,0 +1,309 @@ +/* +** 2007 May 1 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains code used to implement incremental BLOB I/O. +** +** $Id: vdbeblob.c,v 1.10 2007/05/08 20:37:40 drh Exp $ +*/ + +#include "sqliteInt.h" +#include "vdbeInt.h" + +#ifndef SQLITE_OMIT_INCRBLOB + +/* +** Valid sqlite3_blob* handles point to Incrblob structures. +*/ +typedef struct Incrblob Incrblob; +struct Incrblob { + int flags; /* Copy of "flags" passed to sqlite3_blob_open() */ + int nByte; /* Size of open blob, in bytes */ + int iOffset; /* Byte offset of blob in cursor data */ + BtCursor *pCsr; /* Cursor pointing at blob row */ + sqlite3_stmt *pStmt; /* Statement holding cursor open */ +}; + +/* +** Open a blob handle. +*/ +int sqlite3_blob_open( + sqlite3* db, /* The database connection */ + const char *zDb, /* The attached database containing the blob */ + const char *zTable, /* The table containing the blob */ + const char *zColumn, /* The column containing the blob */ + sqlite_int64 iRow, /* The row containing the glob */ + int flags, /* True -> read/write access, false -> read-only */ + sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */ +){ + int nAttempt = 0; + int iCol; /* Index of zColumn in row-record */ + + /* This VDBE program seeks a btree cursor to the identified + ** db/table/row entry. The reason for using a vdbe program instead + ** of writing code to use the b-tree layer directly is that the + ** vdbe program will take advantage of the various transaction, + ** locking and error handling infrastructure built into the vdbe. + ** + ** After seeking the cursor, the vdbe executes an OP_Callback. + ** Code external to the Vdbe then "borrows" the b-tree cursor and + ** uses it to implement the blob_read(), blob_write() and + ** blob_bytes() functions. + ** + ** The sqlite3_blob_close() function finalizes the vdbe program, + ** which closes the b-tree cursor and (possibly) commits the + ** transaction. + */ + static const VdbeOpList openBlob[] = { + {OP_Transaction, 0, 0, 0}, /* 0: Start a transaction */ + {OP_VerifyCookie, 0, 0, 0}, /* 1: Check the schema cookie */ + {OP_Integer, 0, 0, 0}, /* 2: Database number */ + + /* One of the following two instructions is replaced by an + ** OP_Noop before exection. + */ + {OP_OpenRead, 0, 0, 0}, /* 3: Open cursor 0 for reading */ + {OP_OpenWrite, 0, 0, 0}, /* 4: Open cursor 0 for read/write */ + {OP_SetNumColumns, 0, 0, 0}, /* 5: Num cols for cursor */ + + {OP_Variable, 1, 0, 0}, /* 6: Push the rowid to the stack */ + {OP_NotExists, 0, 10, 0}, /* 7: Seek the cursor */ + {OP_Column, 0, 0, 0}, /* 8 */ + {OP_Callback, 0, 0, 0}, /* 9 */ + {OP_Close, 0, 0, 0}, /* 10 */ + {OP_Halt, 0, 0, 0}, /* 11 */ + }; + + Vdbe *v = 0; + int rc = SQLITE_OK; + char zErr[128]; + + zErr[0] = 0; + do { + Parse sParse; + Table *pTab; + + memset(&sParse, 0, sizeof(Parse)); + sParse.db = db; + + rc = sqlite3SafetyOn(db); + if( rc!=SQLITE_OK ){ + return rc; + } + + pTab = sqlite3LocateTable(&sParse, zTable, zDb); + if( !pTab ){ + if( sParse.zErrMsg ){ + sqlite3_snprintf(sizeof(zErr), zErr, "%s", sParse.zErrMsg); + } + sqliteFree(sParse.zErrMsg); + rc = SQLITE_ERROR; + sqlite3SafetyOff(db); + goto blob_open_out; + } + + /* Now search pTab for the exact column. */ + for(iCol=0; iCol < pTab->nCol; iCol++) { + if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){ + break; + } + } + if( iCol==pTab->nCol ){ + sqlite3_snprintf(sizeof(zErr), zErr, "no such column: \"%s\"", zColumn); + rc = SQLITE_ERROR; + sqlite3SafetyOff(db); + goto blob_open_out; + } + + /* If the value is being opened for writing, check that the + ** column is not indexed. It is against the rules to open an + ** indexed column for writing. + */ + if( flags ){ + Index *pIdx; + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + int j; + for(j=0; jnColumn; j++){ + if( pIdx->aiColumn[j]==iCol ){ + sqlite3_snprintf(sizeof(zErr), zErr, + "cannot open indexed column for writing"); + rc = SQLITE_ERROR; + sqlite3SafetyOff(db); + goto blob_open_out; + } + } + } + } + + v = sqlite3VdbeCreate(db); + if( v ){ + int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob); + + /* Configure the OP_Transaction */ + sqlite3VdbeChangeP1(v, 0, iDb); + sqlite3VdbeChangeP2(v, 0, (flags ? 1 : 0)); + + /* Configure the OP_VerifyCookie */ + sqlite3VdbeChangeP1(v, 1, iDb); + sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie); + + /* Configure the db number pushed onto the stack */ + sqlite3VdbeChangeP1(v, 2, iDb); + + /* Remove either the OP_OpenWrite or OpenRead. Set the P2 + ** parameter of the other to pTab->tnum. + */ + sqlite3VdbeChangeToNoop(v, (flags ? 3 : 4), 1); + sqlite3VdbeChangeP2(v, (flags ? 4 : 3), pTab->tnum); + + /* Configure the OP_SetNumColumns. Configure the cursor to + ** think that the table has one more column than it really + ** does. An OP_Column to retrieve this imaginary column will + ** always return an SQL NULL. This is useful because it means + ** we can invoke OP_Column to fill in the vdbe cursors type + ** and offset cache without causing any IO. + */ + sqlite3VdbeChangeP2(v, 5, pTab->nCol+1); + if( !sqlite3MallocFailed() ){ + sqlite3VdbeMakeReady(v, 1, 0, 1, 0); + } + } + + rc = sqlite3SafetyOff(db); + if( rc!=SQLITE_OK || sqlite3MallocFailed() ){ + goto blob_open_out; + } + + sqlite3_bind_int64((sqlite3_stmt *)v, 1, iRow); + rc = sqlite3_step((sqlite3_stmt *)v); + if( rc!=SQLITE_ROW ){ + nAttempt++; + rc = sqlite3_finalize((sqlite3_stmt *)v); + sqlite3_snprintf(sizeof(zErr), zErr, sqlite3_errmsg(db)); + v = 0; + } + } while( nAttempt<5 && rc==SQLITE_SCHEMA ); + + if( rc==SQLITE_ROW ){ + /* The row-record has been opened successfully. Check that the + ** column in question contains text or a blob. If it contains + ** text, it is up to the caller to get the encoding right. + */ + Incrblob *pBlob; + u32 type = v->apCsr[0]->aType[iCol]; + + if( type<12 ){ + sqlite3_snprintf(sizeof(zErr), zErr, "cannot open value of type %s", + type==0?"null": type==7?"real": "integer" + ); + rc = SQLITE_ERROR; + goto blob_open_out; + } + pBlob = (Incrblob *)sqliteMalloc(sizeof(Incrblob)); + if( sqlite3MallocFailed() ){ + sqliteFree(pBlob); + goto blob_open_out; + } + pBlob->flags = flags; + pBlob->pCsr = v->apCsr[0]->pCursor; + sqlite3BtreeCacheOverflow(pBlob->pCsr); + pBlob->pStmt = (sqlite3_stmt *)v; + pBlob->iOffset = v->apCsr[0]->aOffset[iCol]; + pBlob->nByte = sqlite3VdbeSerialTypeLen(type); + *ppBlob = (sqlite3_blob *)pBlob; + rc = SQLITE_OK; + }else if( rc==SQLITE_OK ){ + sqlite3_snprintf(sizeof(zErr), zErr, "no such rowid: %lld", iRow); + rc = SQLITE_ERROR; + } + +blob_open_out: + zErr[sizeof(zErr)-1] = '\0'; + if( rc!=SQLITE_OK || sqlite3MallocFailed() ){ + sqlite3_finalize((sqlite3_stmt *)v); + } + sqlite3Error(db, rc, (rc==SQLITE_OK?0:zErr)); + return sqlite3ApiExit(db, rc); +} + +/* +** Close a blob handle that was previously created using +** sqlite3_blob_open(). +*/ +int sqlite3_blob_close(sqlite3_blob *pBlob){ + Incrblob *p = (Incrblob *)pBlob; + sqlite3_stmt *pStmt = p->pStmt; + sqliteFree(p); + return sqlite3_finalize(pStmt); +} + + +static int blobReadWrite( + sqlite3_blob *pBlob, + void *z, + int n, + int iOffset, + int (*xCall)(BtCursor*, u32, u32, void*) +){ + int rc; + Incrblob *p = (Incrblob *)pBlob; + Vdbe *v = (Vdbe *)(p->pStmt); + sqlite3 *db; + + /* If there is no statement handle, then the blob-handle has + ** already been invalidated. Return SQLITE_ABORT in this case. + */ + if( !v ) return SQLITE_ABORT; + + /* Request is out of range. Return a transient error. */ + if( (iOffset+n)>p->nByte ){ + return SQLITE_ERROR; + } + + /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is + ** returned, clean-up the statement handle. + */ + db = v->db; + rc = xCall(p->pCsr, iOffset+p->iOffset, n, z); + if( rc==SQLITE_ABORT ){ + sqlite3VdbeFinalize(v); + p->pStmt = 0; + }else{ + v->rc = rc; + } + + return sqlite3ApiExit(db, rc); +} + +/* +** Read data from a blob handle. +*/ +int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){ + return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData); +} + +/* +** Write data to a blob handle. +*/ +int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){ + return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData); +} + +/* +** Query a blob handle for the size of the data. +*/ +int sqlite3_blob_bytes(sqlite3_blob *pBlob){ + Incrblob *p = (Incrblob *)pBlob; + return p->nByte; +} + +#endif /* #ifndef SQLITE_OMIT_INCRBLOB */ ============================================================ --- Makefile.am 48daf094e8337fabfe244d6c9f8deea847f0380d +++ Makefile.am a0c1a322d2d4e4fe18f32f7a48d3eb1ee447a94b @@ -221,7 +221,7 @@ SQLITE_SOURCES = \ sqlite/vdbemem.c sqlite/where.c \ sqlite/prepare.c sqlite/callback.c \ sqlite/analyze.c sqlite/vdbefifo.c \ - sqlite/complete.c \ + sqlite/complete.c sqlite/malloc.c sqlite/vdbeblob.c \ sqlite/loadext.c sqlite/vtab.c \ \ sqlite/btree.h sqlite/hash.h sqlite/opcodes.h sqlite/os.h \ @@ -229,7 +229,7 @@ SQLITE_SOURCES = \ sqlite/parse.h sqlite/sqlite3.h sqlite/sqlite3ext.h \ sqlite/sqliteInt.h \ sqlite/vdbe.h sqlite/vdbeInt.h \ - sqlite/pager.h + sqlite/pager.h sqlite/btreeInt.h sqlite/limits.h LUA_SOURCES = \ lua/lapi.cc lua/lapi.h \ @@ -354,7 +354,7 @@ mtn_LDFLAGS = lib3rdparty_a_CXXFLAGS = $(AM_CXXFLAGS) $(LIB3RDPARTY_CXXFLAGS) mtn_LDFLAGS = -mtn_CPPFLAGS = -I$(top_srcdir)/lua -I$(top_srcdir)/sqlite +mtn_CPPFLAGS = -I$(top_srcdir)/lua mtn_CFLAGS = $(AM_CFLAGS) $(MTN_CFLAGS) mtn_CXXFLAGS = $(AM_CXXFLAGS) $(PCH_FLAGS) $(MTN_CXXFLAGS) mtn_LDADD = lib3rdparty.a $(BOOSTLIBS) libplatform.a $(LIBICONV) $(LIBINTL) ============================================================ --- database.cc 5087d1394d533e4daddc1122c55f9be269eccf7e +++ database.cc 46acb01357e625f61a20b3c5899ae885b29b213a @@ -22,7 +22,7 @@ #include #include "lexical_cast.hh" -#include +#include "sqlite/sqlite3.h" #include "app_state.hh" #include "cert.hh" ============================================================ --- schema_migration.cc 222733a13d7a3eb204c67af2123f5a242957b415 +++ schema_migration.cc 769321d3bb854322d9de25354e9e240396cf1096 @@ -10,7 +10,7 @@ #include "base.hh" #include #include "lexical_cast.hh" -#include +#include "sqlite/sqlite3.h" #include #include "sanity.hh" ============================================================ --- sqlite/alter.c 2c79ec40f65e33deaf90ca493422c74586e481a3 +++ sqlite/alter.c 1b1deeb97446ed87f2fa17a3eb6236548841a348 @@ -12,7 +12,7 @@ ** This file contains C code routines that used to generate VDBE code ** that implements the ALTER TABLE command. ** -** $Id: alter.c,v 1.22 2006/09/08 12:27:37 drh Exp $ +** $Id: alter.c,v 1.25 2007/05/15 14:34:32 drh Exp $ */ #include "sqliteInt.h" #include @@ -57,6 +57,11 @@ static void renameTableFunc( */ if( zSql ){ do { + if( !*zCsr ){ + /* Ran out of input before finding an opening bracket. Return NULL. */ + return; + } + /* Store the token that zCsr points to in tname. */ tname.z = zCsr; tname.n = len; @@ -107,6 +112,12 @@ static void renameTriggerFunc( */ if( zSql ){ do { + + if( !*zCsr ){ + /* Ran out of input before finding the table name. Return NULL. */ + return; + } + /* Store the token that zCsr points to in tname. */ tname.z = zCsr; tname.n = len; @@ -262,6 +273,8 @@ void sqlite3AlterRenameTable( Table *pTab; /* Table being renamed */ char *zName = 0; /* NULL-terminated version of pName */ sqlite3 *db = pParse->db; /* Database connection */ + int nTabName; /* Number of UTF-8 characters in zTabName */ + const char *zTabName; /* Original name of the table */ Vdbe *v; #ifndef SQLITE_OMIT_TRIGGER char *zWhere = 0; /* Where clause to locate temp triggers */ @@ -323,6 +336,10 @@ void sqlite3AlterRenameTable( sqlite3BeginWriteOperation(pParse, 0, iDb); sqlite3ChangeCookie(db, v, iDb); + /* figure out how many UTF-8 characters are in zName */ + zTabName = pTab->zName; + nTabName = sqlite3Utf8CharLen(zTabName, -1); + /* Modify the sqlite_master table to use the new table name. */ sqlite3NestedParse(pParse, "UPDATE %Q.%s SET " @@ -337,7 +354,7 @@ void sqlite3AlterRenameTable( "name = CASE " "WHEN type='table' THEN %Q " "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN " - "'sqlite_autoindex_' || %Q || substr(name, %d+18,10) " + "'sqlite_autoindex_' || %Q || substr(name,%d+18,10) " "ELSE name END " "WHERE tbl_name=%Q AND " "(type='table' OR type='index' OR type='trigger');", @@ -345,7 +362,7 @@ void sqlite3AlterRenameTable( #ifndef SQLITE_OMIT_TRIGGER zName, #endif - zName, strlen(pTab->zName), pTab->zName + zName, nTabName, zTabName ); #ifndef SQLITE_OMIT_AUTOINCREMENT ============================================================ --- sqlite/analyze.c 4bbf5ddf9680587c6d4917e02e378b6037be3651 +++ sqlite/analyze.c e8fcb1c35ace8418615eb18d9601f321ac86b2ec @@ -11,7 +11,7 @@ ************************************************************************* ** This file contains code associated with the ANALYZE command. ** -** @(#) $Id: analyze.c,v 1.17 2007/03/29 05:51:49 drh Exp $ +** @(#) $Id: analyze.c,v 1.18 2007/05/12 12:08:51 drh Exp $ */ #ifndef SQLITE_OMIT_ANALYZE #include "sqliteInt.h" @@ -36,6 +36,7 @@ static void openStatTable( Table *pStat; Vdbe *v = sqlite3GetVdbe(pParse); + if( v==0 ) return; pDb = &db->aDb[iDb]; if( (pStat = sqlite3FindTable(db, "sqlite_stat1", pDb->zName))==0 ){ /* The sqlite_stat1 tables does not exist. Create it. @@ -95,7 +96,7 @@ static void analyzeOneTable( int iDb; /* Index of database containing pTab */ v = sqlite3GetVdbe(pParse); - if( pTab==0 || pTab->pIndex==0 ){ + if( v==0 || pTab==0 || pTab->pIndex==0 ){ /* Do no analysis for tables that have no indices */ return; } @@ -222,7 +223,9 @@ static void loadAnalysis(Parse *pParse, */ static void loadAnalysis(Parse *pParse, int iDb){ Vdbe *v = sqlite3GetVdbe(pParse); - sqlite3VdbeAddOp(v, OP_LoadAnalysis, iDb, 0); + if( v ){ + sqlite3VdbeAddOp(v, OP_LoadAnalysis, iDb, 0); + } } /* @@ -314,10 +317,12 @@ void sqlite3Analyze(Parse *pParse, Token if( iDb>=0 ){ zDb = db->aDb[iDb].zName; z = sqlite3NameFromToken(pTableName); - pTab = sqlite3LocateTable(pParse, z, zDb); - sqliteFree(z); - if( pTab ){ - analyzeTable(pParse, pTab); + if( z ){ + pTab = sqlite3LocateTable(pParse, z, zDb); + sqliteFree(z); + if( pTab ){ + analyzeTable(pParse, pTab); + } } } } @@ -371,10 +376,11 @@ static int analysisLoader(void *pData, i /* ** Load the content of the sqlite_stat1 table into the index hash tables. */ -void sqlite3AnalysisLoad(sqlite3 *db, int iDb){ +int sqlite3AnalysisLoad(sqlite3 *db, int iDb){ analysisInfo sInfo; HashElem *i; char *zSql; + int rc; /* Clear any prior statistics */ for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ @@ -386,7 +392,7 @@ void sqlite3AnalysisLoad(sqlite3 *db, in sInfo.db = db; sInfo.zDatabase = db->aDb[iDb].zName; if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){ - return; + return SQLITE_ERROR; } @@ -394,9 +400,10 @@ void sqlite3AnalysisLoad(sqlite3 *db, in zSql = sqlite3MPrintf("SELECT idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase); sqlite3SafetyOff(db); - sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); + rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); sqlite3SafetyOn(db); sqliteFree(zSql); + return rc; } ============================================================ --- sqlite/attach.c a16ada4a4654a0d126b8223ec9494ebb81bc5c3c +++ sqlite/attach.c ba628db0c2b6a362f036d017bf1196cdfe4ebb37 @@ -11,7 +11,7 @@ ************************************************************************* ** This file contains code used to implement the ATTACH and DETACH commands. ** -** $Id: attach.c,v 1.57 2007/03/27 21:47:07 drh Exp $ +** $Id: attach.c,v 1.60 2007/05/09 20:31:30 drh Exp $ */ #include "sqliteInt.h" @@ -40,6 +40,10 @@ static int resolveAttachExpr(NameContext if( pExpr ){ if( pExpr->op!=TK_ID ){ rc = sqlite3ExprResolveNames(pName, pExpr); + if( rc==SQLITE_OK && !sqlite3ExprIsConstant(pExpr) ){ + sqlite3ErrorMsg(pName->pParse, "invalid name: \"%T\"", &pExpr->span); + return SQLITE_ERROR; + } }else{ pExpr->op = TK_STRING; } @@ -83,14 +87,16 @@ static void attachFunc( ** * Transaction currently open ** * Specified database name already being used. */ - if( db->nDb>=MAX_ATTACHED+2 ){ + if( db->nDb>=SQLITE_MAX_ATTACHED+2 ){ sqlite3_snprintf( - sizeof(zErr), zErr, "too many attached databases - max %d", MAX_ATTACHED + sizeof(zErr), zErr, "too many attached databases - max %d", + SQLITE_MAX_ATTACHED ); goto attach_error; } if( !db->autoCommit ){ - strcpy(zErr, "cannot ATTACH database within transaction"); + sqlite3_snprintf(sizeof(zErr), zErr, + "cannot ATTACH database within transaction"); goto attach_error; } for(i=0; inDb; i++){ @@ -124,13 +130,13 @@ static void attachFunc( ** it to obtain the database schema. At this point the schema may ** or may not be initialised. */ - rc = sqlite3BtreeFactory(db, zFile, 0, MAX_PAGES, &aNew->pBt); + rc = sqlite3BtreeFactory(db, zFile, 0, SQLITE_DEFAULT_CACHE_SIZE, &aNew->pBt); if( rc==SQLITE_OK ){ aNew->pSchema = sqlite3SchemaGet(aNew->pBt); if( !aNew->pSchema ){ rc = SQLITE_NOMEM; }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){ - strcpy(zErr, + sqlite3_snprintf(sizeof(zErr), zErr, "attached databases must use the same text encoding as main database"); goto attach_error; } @@ -246,7 +252,8 @@ static void detachFunc( goto detach_error; } if( !db->autoCommit ){ - strcpy(zErr, "cannot DETACH database within transaction"); + sqlite3_snprintf(sizeof(zErr), zErr, + "cannot DETACH database within transaction"); goto detach_error; } if( sqlite3BtreeIsInReadTrans(pDb->pBt) ){ ============================================================ --- sqlite/auth.c 902f4722661c796b97f007d9606bd7529c02597f +++ sqlite/auth.c 5ea90bc93dfea46e9fe4bf531e14c7cd98219ecb @@ -14,7 +14,7 @@ ** systems that do not need this facility may omit it by recompiling ** the library with -DSQLITE_OMIT_AUTHORIZATION=1 ** -** $Id: auth.c,v 1.25 2006/06/16 08:01:03 danielk1977 Exp $ +** $Id: auth.c,v 1.26 2007/05/14 11:34:47 drh Exp $ */ #include "sqliteInt.h" @@ -115,8 +115,7 @@ void sqlite3AuthRead( int iDb; /* The index of the database the expression refers to */ if( db->xAuth==0 ) return; - if( pExpr->op==TK_AS ) return; - assert( pExpr->op==TK_COLUMN ); + if( pExpr->op!=TK_COLUMN ) return; iDb = sqlite3SchemaToIndex(pParse->db, pExpr->pSchema); if( iDb<0 ){ /* An attempt to read a column out of a subquery or other ============================================================ --- sqlite/btree.c 960bf64baa4d2bdb96019698e60d0b7763bf4e7e +++ sqlite/btree.c bde03df933f8295e984b831779a318cc7c38f339 @@ -9,425 +9,27 @@ ** May you share freely, never taking more than you give. ** ************************************************************************* -** $Id: btree.c,v 1.358 2007/04/24 17:35:59 drh Exp $ +** $Id: btree.c,v 1.388 2007/05/24 09:20:16 danielk1977 Exp $ ** ** This file implements a external (disk-based) database using BTrees. -** For a detailed discussion of BTrees, refer to -** -** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3: -** "Sorting And Searching", pages 473-480. Addison-Wesley -** Publishing Company, Reading, Massachusetts. -** -** The basic idea is that each page of the file contains N database -** entries and N+1 pointers to subpages. -** -** ---------------------------------------------------------------- -** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) | -** ---------------------------------------------------------------- -** -** All of the keys on the page that Ptr(0) points to have values less -** than Key(0). All of the keys on page Ptr(1) and its subpages have -** values greater than Key(0) and less than Key(1). All of the keys -** on Ptr(N) and its subpages have values greater than Key(N-1). And -** so forth. -** -** Finding a particular key requires reading O(log(M)) pages from the -** disk where M is the number of entries in the tree. -** -** In this implementation, a single file can hold one or more separate -** BTrees. Each BTree is identified by the index of its root page. The -** key and data for any entry are combined to form the "payload". A -** fixed amount of payload can be carried directly on the database -** page. If the payload is larger than the preset amount then surplus -** bytes are stored on overflow pages. The payload for an entry -** and the preceding pointer are combined to form a "Cell". Each -** page has a small header which contains the Ptr(N) pointer and other -** information such as the size of key and data. -** -** FORMAT DETAILS -** -** The file is divided into pages. The first page is called page 1, -** the second is page 2, and so forth. A page number of zero indicates -** "no such page". The page size can be anything between 512 and 65536. -** Each page can be either a btree page, a freelist page or an overflow -** page. -** -** The first page is always a btree page. The first 100 bytes of the first -** page contain a special header (the "file header") that describes the file. -** The format of the file header is as follows: -** -** OFFSET SIZE DESCRIPTION -** 0 16 Header string: "SQLite format 3\000" -** 16 2 Page size in bytes. -** 18 1 File format write version -** 19 1 File format read version -** 20 1 Bytes of unused space at the end of each page -** 21 1 Max embedded payload fraction -** 22 1 Min embedded payload fraction -** 23 1 Min leaf payload fraction -** 24 4 File change counter -** 28 4 Reserved for future use -** 32 4 First freelist page -** 36 4 Number of freelist pages in the file -** 40 60 15 4-byte meta values passed to higher layers -** -** All of the integer values are big-endian (most significant byte first). -** -** The file change counter is incremented when the database is changed more -** than once within the same second. This counter, together with the -** modification time of the file, allows other processes to know -** when the file has changed and thus when they need to flush their -** cache. -** -** The max embedded payload fraction is the amount of the total usable -** space in a page that can be consumed by a single cell for standard -** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default -** is to limit the maximum cell size so that at least 4 cells will fit -** on one page. Thus the default max embedded payload fraction is 64. -** -** If the payload for a cell is larger than the max payload, then extra -** payload is spilled to overflow pages. Once an overflow page is allocated, -** as many bytes as possible are moved into the overflow pages without letting -** the cell size drop below the min embedded payload fraction. -** -** The min leaf payload fraction is like the min embedded payload fraction -** except that it applies to leaf nodes in a LEAFDATA tree. The maximum -** payload fraction for a LEAFDATA tree is always 100% (or 255) and it -** not specified in the header. -** -** Each btree pages is divided into three sections: The header, the -** cell pointer array, and the cell area area. Page 1 also has a 100-byte -** file header that occurs before the page header. -** -** |----------------| -** | file header | 100 bytes. Page 1 only. -** |----------------| -** | page header | 8 bytes for leaves. 12 bytes for interior nodes -** |----------------| -** | cell pointer | | 2 bytes per cell. Sorted order. -** | array | | Grows downward -** | | v -** |----------------| -** | unallocated | -** | space | -** |----------------| ^ Grows upwards -** | cell content | | Arbitrary order interspersed with freeblocks. -** | area | | and free space fragments. -** |----------------| -** -** The page headers looks like this: -** -** OFFSET SIZE DESCRIPTION -** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf -** 1 2 byte offset to the first freeblock -** 3 2 number of cells on this page -** 5 2 first byte of the cell content area -** 7 1 number of fragmented free bytes -** 8 4 Right child (the Ptr(N) value). Omitted on leaves. -** -** The flags define the format of this btree page. The leaf flag means that -** this page has no children. The zerodata flag means that this page carries -** only keys and no data. The intkey flag means that the key is a integer -** which is stored in the key size entry of the cell header rather than in -** the payload area. -** -** The cell pointer array begins on the first byte after the page header. -** The cell pointer array contains zero or more 2-byte numbers which are -** offsets from the beginning of the page to the cell content in the cell -** content area. The cell pointers occur in sorted order. The system strives -** to keep free space after the last cell pointer so that new cells can -** be easily added without having to defragment the page. -** -** Cell content is stored at the very end of the page and grows toward the -** beginning of the page. -** -** Unused space within the cell content area is collected into a linked list of -** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset -** to the first freeblock is given in the header. Freeblocks occur in -** increasing order. Because a freeblock must be at least 4 bytes in size, -** any group of 3 or fewer unused bytes in the cell content area cannot -** exist on the freeblock chain. A group of 3 or fewer free bytes is called -** a fragment. The total number of bytes in all fragments is recorded. -** in the page header at offset 7. -** -** SIZE DESCRIPTION -** 2 Byte offset of the next freeblock -** 2 Bytes in this freeblock -** -** Cells are of variable length. Cells are stored in the cell content area at -** the end of the page. Pointers to the cells are in the cell pointer array -** that immediately follows the page header. Cells is not necessarily -** contiguous or in order, but cell pointers are contiguous and in order. -** -** Cell content makes use of variable length integers. A variable -** length integer is 1 to 9 bytes where the lower 7 bits of each -** byte are used. The integer consists of all bytes that have bit 8 set and -** the first byte with bit 8 clear. The most significant byte of the integer -** appears first. A variable-length integer may not be more than 9 bytes long. -** As a special case, all 8 bytes of the 9th byte are used as data. This -** allows a 64-bit integer to be encoded in 9 bytes. -** -** 0x00 becomes 0x00000000 -** 0x7f becomes 0x0000007f -** 0x81 0x00 becomes 0x00000080 -** 0x82 0x00 becomes 0x00000100 -** 0x80 0x7f becomes 0x0000007f -** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678 -** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081 -** -** Variable length integers are used for rowids and to hold the number of -** bytes of key and data in a btree cell. -** -** The content of a cell looks like this: -** -** SIZE DESCRIPTION -** 4 Page number of the left child. Omitted if leaf flag is set. -** var Number of bytes of data. Omitted if the zerodata flag is set. -** var Number of bytes of key. Or the key itself if intkey flag is set. -** * Payload -** 4 First page of the overflow chain. Omitted if no overflow -** -** Overflow pages form a linked list. Each page except the last is completely -** filled with data (pagesize - 4 bytes). The last page can have as little -** as 1 byte of data. -** -** SIZE DESCRIPTION -** 4 Page number of next overflow page -** * Data -** -** Freelist pages come in two subtypes: trunk pages and leaf pages. The -** file header points to first in a linked list of trunk page. Each trunk -** page points to multiple leaf pages. The content of a leaf page is -** unspecified. A trunk page looks like this: -** -** SIZE DESCRIPTION -** 4 Page number of next trunk page -** 4 Number of leaf pointers on this page -** * zero or more pages numbers of leaves +** See the header comment on "btreeInt.h" for additional information. +** Including a description of file format and an overview of operation. */ -#include "sqliteInt.h" -#include "pager.h" -#include "btree.h" -#include "os.h" -#include +#include "btreeInt.h" -/* Round up a number to the next larger multiple of 8. This is used -** to force 8-byte alignment on 64-bit architectures. -*/ -#define ROUND8(x) ((x+7)&~7) - - -/* The following value is the maximum cell size assuming a maximum page -** size give above. -*/ -#define MX_CELL_SIZE(pBt) (pBt->pageSize-8) - -/* The maximum number of cells on a single page of the database. This -** assumes a minimum cell size of 3 bytes. Such small cells will be -** exceedingly rare, but they are possible. -*/ -#define MX_CELL(pBt) ((pBt->pageSize-8)/3) - -/* Forward declarations */ -typedef struct MemPage MemPage; -typedef struct BtLock BtLock; - /* -** This is a magic string that appears at the beginning of every -** SQLite database in order to identify the file as a real database. -** -** You can change this value at compile-time by specifying a -** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The -** header must be exactly 16 bytes including the zero-terminator so -** the string itself should be 15 characters long. If you change -** the header, then your custom library will not be able to read -** databases generated by the standard tools and the standard tools -** will not be able to read databases created by your custom library. +** The header string that appears at the beginning of every +** SQLite database. */ -#ifndef SQLITE_FILE_HEADER /* 123456789 123456 */ -# define SQLITE_FILE_HEADER "SQLite format 3" -#endif static const char zMagicHeader[] = SQLITE_FILE_HEADER; -/* -** Page type flags. An ORed combination of these flags appear as the -** first byte of every BTree page. -*/ -#define PTF_INTKEY 0x01 -#define PTF_ZERODATA 0x02 -#define PTF_LEAFDATA 0x04 -#define PTF_LEAF 0x08 /* -** As each page of the file is loaded into memory, an instance of the following -** structure is appended and initialized to zero. This structure stores -** information about the page that is decoded from the raw file page. -** -** The pParent field points back to the parent page. This allows us to -** walk up the BTree from any leaf to the root. Care must be taken to -** unref() the parent page pointer when this page is no longer referenced. -** The pageDestructor() routine handles that chore. +** Set this global variable to 1 to enable tracing using the TRACE +** macro. */ -struct MemPage { - u8 isInit; /* True if previously initialized. MUST BE FIRST! */ - u8 idxShift; /* True if Cell indices have changed */ - u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ - u8 intKey; /* True if intkey flag is set */ - u8 leaf; /* True if leaf flag is set */ - u8 zeroData; /* True if table stores keys only */ - u8 leafData; /* True if tables stores data on leaves only */ - u8 hasData; /* True if this page stores data */ - u8 hdrOffset; /* 100 for page 1. 0 otherwise */ - u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */ - u16 maxLocal; /* Copy of Btree.maxLocal or Btree.maxLeaf */ - u16 minLocal; /* Copy of Btree.minLocal or Btree.minLeaf */ - u16 cellOffset; /* Index in aData of first cell pointer */ - u16 idxParent; /* Index in parent of this node */ - u16 nFree; /* Number of free bytes on the page */ - u16 nCell; /* Number of cells on this page, local and ovfl */ - struct _OvflCell { /* Cells that will not fit on aData[] */ - u8 *pCell; /* Pointers to the body of the overflow cell */ - u16 idx; /* Insert this cell before idx-th non-overflow cell */ - } aOvfl[5]; - BtShared *pBt; /* Pointer back to BTree structure */ - u8 *aData; /* Pointer back to the start of the page */ - DbPage *pDbPage; /* Pager page handle */ - Pgno pgno; /* Page number for this page */ - MemPage *pParent; /* The parent of this page. NULL for root */ -}; - -/* -** The in-memory image of a disk page has the auxiliary information appended -** to the end. EXTRA_SIZE is the number of bytes of space needed to hold -** that extra information. -*/ -#define EXTRA_SIZE sizeof(MemPage) - -/* Btree handle */ -struct Btree { - sqlite3 *pSqlite; - BtShared *pBt; - u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */ -}; - -/* -** Btree.inTrans may take one of the following values. -** -** If the shared-data extension is enabled, there may be multiple users -** of the Btree structure. At most one of these may open a write transaction, -** but any number may have active read transactions. Variable Btree.pDb -** points to the handle that owns any current write-transaction. -*/ -#define TRANS_NONE 0 -#define TRANS_READ 1 -#define TRANS_WRITE 2 - -/* -** Everything we need to know about an open database -*/ -struct BtShared { - Pager *pPager; /* The page cache */ - BtCursor *pCursor; /* A list of all open cursors */ - MemPage *pPage1; /* First page of the database */ - u8 inStmt; /* True if we are in a statement subtransaction */ - u8 readOnly; /* True if the underlying file is readonly */ - u8 maxEmbedFrac; /* Maximum payload as % of total page size */ - u8 minEmbedFrac; /* Minimum payload as % of total page size */ - u8 minLeafFrac; /* Minimum leaf payload as % of total page size */ - u8 pageSizeFixed; /* True if the page size can no longer be changed */ -#ifndef SQLITE_OMIT_AUTOVACUUM - u8 autoVacuum; /* True if database supports auto-vacuum */ -#endif - u16 pageSize; /* Total number of bytes on a page */ - u16 usableSize; /* Number of usable bytes on each page */ - int maxLocal; /* Maximum local payload in non-LEAFDATA tables */ - int minLocal; /* Minimum local payload in non-LEAFDATA tables */ - int maxLeaf; /* Maximum local payload in a LEAFDATA table */ - int minLeaf; /* Minimum local payload in a LEAFDATA table */ - BusyHandler *pBusyHandler; /* Callback for when there is lock contention */ - u8 inTransaction; /* Transaction state */ - int nRef; /* Number of references to this structure */ - int nTransaction; /* Number of open transactions (read + write) */ - void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */ - void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */ -#ifndef SQLITE_OMIT_SHARED_CACHE - BtLock *pLock; /* List of locks held on this shared-btree struct */ - BtShared *pNext; /* Next in ThreadData.pBtree linked list */ -#endif -}; - -/* -** An instance of the following structure is used to hold information -** about a cell. The parseCellPtr() function fills in this structure -** based on information extract from the raw disk page. -*/ -typedef struct CellInfo CellInfo; -struct CellInfo { - u8 *pCell; /* Pointer to the start of cell content */ - i64 nKey; /* The key for INTKEY tables, or number of bytes in key */ - u32 nData; /* Number of bytes of data */ - u32 nPayload; /* Total amount of payload */ - u16 nHeader; /* Size of the cell content header in bytes */ - u16 nLocal; /* Amount of payload held locally */ - u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */ - u16 nSize; /* Size of the cell content on the main b-tree page */ -}; - -/* -** A cursor is a pointer to a particular entry in the BTree. -** The entry is identified by its MemPage and the index in -** MemPage.aCell[] of the entry. -*/ -struct BtCursor { - Btree *pBtree; /* The Btree to which this cursor belongs */ - BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */ - int (*xCompare)(void*,int,const void*,int,const void*); /* Key comp func */ - void *pArg; /* First arg to xCompare() */ - Pgno pgnoRoot; /* The root page of this tree */ - MemPage *pPage; /* Page that contains the entry */ - int idx; /* Index of the entry in pPage->aCell[] */ - CellInfo info; /* A parse of the cell we are pointing at */ - u8 wrFlag; /* True if writable */ - u8 eState; /* One of the CURSOR_XXX constants (see below) */ - void *pKey; /* Saved key that was cursor's last known position */ - i64 nKey; /* Size of pKey, or last integer key */ - int skip; /* (skip<0) -> Prev() is a no-op. (skip>0) -> Next() is */ -}; - -/* -** Potential values for BtCursor.eState. -** -** CURSOR_VALID: -** Cursor points to a valid entry. getPayload() etc. may be called. -** -** CURSOR_INVALID: -** Cursor does not point to a valid entry. This can happen (for example) -** because the table is empty or because BtreeCursorFirst() has not been -** called. -** -** CURSOR_REQUIRESEEK: -** The table that this cursor was opened on still exists, but has been -** modified since the cursor was last used. The cursor position is saved -** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in -** this state, restoreOrClearCursorPosition() can be called to attempt to -** seek the cursor to the saved position. -*/ -#define CURSOR_INVALID 0 -#define CURSOR_VALID 1 -#define CURSOR_REQUIRESEEK 2 - -/* -** The TRACE macro will print high-level status information about the -** btree operation when the global variable sqlite3_btree_trace is -** enabled. -*/ #if SQLITE_TEST -# define TRACE(X) if( sqlite3_btree_trace )\ -/* { sqlite3DebugPrintf X; fflush(stdout); } */ \ -{ printf X; fflush(stdout); } int sqlite3_btree_trace=0; /* True to enable tracing */ -#else -# define TRACE(X) #endif /* @@ -435,67 +37,7 @@ static int checkReadLocks(Btree*,Pgno,Bt */ static int checkReadLocks(Btree*,Pgno,BtCursor*); -/* -** Read or write a two- and four-byte big-endian integer values. -*/ -static u32 get2byte(unsigned char *p){ - return (p[0]<<8) | p[1]; -} -static u32 get4byte(unsigned char *p){ - return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; -} -static void put2byte(unsigned char *p, u32 v){ - p[0] = v>>8; - p[1] = v; -} -static void put4byte(unsigned char *p, u32 v){ - p[0] = v>>24; - p[1] = v>>16; - p[2] = v>>8; - p[3] = v; -} -/* -** Routines to read and write variable-length integers. These used to -** be defined locally, but now we use the varint routines in the util.c -** file. -*/ -#define getVarint sqlite3GetVarint -/* #define getVarint32 sqlite3GetVarint32 */ -#define getVarint32(A,B) ((*B=*(A))<=0x7f?1:sqlite3GetVarint32(A,B)) -#define putVarint sqlite3PutVarint - -/* The database page the PENDING_BYTE occupies. This page is never used. -** TODO: This macro is very similary to PAGER_MJ_PGNO() in pager.c. They -** should possibly be consolidated (presumably in pager.h). -** -** If disk I/O is omitted (meaning that the database is stored purely -** in memory) then there is no pending byte. -*/ -#ifdef SQLITE_OMIT_DISKIO -# define PENDING_BYTE_PAGE(pBt) 0x7fffffff -#else -# define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1) -#endif - -/* -** A linked list of the following structures is stored at BtShared.pLock. -** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor -** is opened on the table with root page BtShared.iTable. Locks are removed -** from this list when a transaction is committed or rolled back, or when -** a btree handle is closed. -*/ -struct BtLock { - Btree *pBtree; /* Btree handle holding this lock */ - Pgno iTable; /* Root page of table */ - u8 eLock; /* READ_LOCK or WRITE_LOCK */ - BtLock *pNext; /* Next in BtShared.pLock list */ -}; - -/* Candidate values for BtLock.eLock */ -#define READ_LOCK 1 -#define WRITE_LOCK 2 - #ifdef SQLITE_OMIT_SHARED_CACHE /* ** The functions queryTableLock(), lockTable() and unlockAllTables() @@ -510,7 +52,6 @@ struct BtLock { #define unlockAllTables(a) #else - /* ** Query to see if btree handle p may obtain a lock of type eLock ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return @@ -651,7 +192,31 @@ static void releasePage(MemPage *pPage); static void releasePage(MemPage *pPage); /* Forward reference */ +#ifndef SQLITE_OMIT_INCRBLOB /* +** Invalidate the overflow page-list cache for cursor pCur, if any. +*/ +static void invalidateOverflowCache(BtCursor *pCur){ + sqliteFree(pCur->aOverflow); + pCur->aOverflow = 0; +} + +/* +** Invalidate the overflow page-list cache for all cursors opened +** on the shared btree structure pBt. +*/ +static void invalidateAllOverflowCache(BtShared *pBt){ + BtCursor *p; + for(p=pBt->pCursor; p; p=p->pNext){ + invalidateOverflowCache(p); + } +} +#else + #define invalidateOverflowCache(x) + #define invalidateAllOverflowCache(x) +#endif + +/* ** Save the current cursor position in the variables BtCursor.nKey ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. */ @@ -690,6 +255,7 @@ static int saveCursorPosition(BtCursor * pCur->eState = CURSOR_REQUIRESEEK; } + invalidateOverflowCache(pCur); return rc; } @@ -732,9 +298,14 @@ static void clearCursorPosition(BtCursor ** returning the cursor to it's saved position, any saved position is deleted ** and the cursor state set to CURSOR_INVALID. */ -static int restoreOrClearCursorPositionX(BtCursor *pCur){ +int sqlite3BtreeRestoreOrClearCursorPosition(BtCursor *pCur){ int rc; assert( pCur->eState==CURSOR_REQUIRESEEK ); +#ifndef SQLITE_OMIT_INCRBLOB + if( pCur->isIncrblobHandle ){ + return SQLITE_ABORT; + } +#endif pCur->eState = CURSOR_INVALID; rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skip); if( rc==SQLITE_OK ){ @@ -746,28 +317,16 @@ static int restoreOrClearCursorPositionX } #define restoreOrClearCursorPosition(p) \ - (p->eState==CURSOR_REQUIRESEEK?restoreOrClearCursorPositionX(p):SQLITE_OK) + (p->eState==CURSOR_REQUIRESEEK ? \ + sqlite3BtreeRestoreOrClearCursorPosition(p) : \ + SQLITE_OK) #ifndef SQLITE_OMIT_AUTOVACUUM /* -** These macros define the location of the pointer-map entry for a -** database page. The first argument to each is the number of usable -** bytes on each page of the database (often 1024). The second is the -** page number to look up in the pointer map. -** -** PTRMAP_PAGENO returns the database page number of the pointer-map -** page that stores the required pointer. PTRMAP_PTROFFSET returns -** the offset of the requested map entry. -** -** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page, -** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be -** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements -** this test. +** Given a page number of a regular database page, return the page +** number for the pointer-map page that contains the entry for the +** input page number. */ -#define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno) -#define PTRMAP_PTROFFSET(pBt, pgno) (5*(pgno-ptrmapPageno(pBt, pgno)-1)) -#define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno)) - static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ int nPagesPerMapPage = (pBt->usableSize/5)+1; int iPtrMap = (pgno-2)/nPagesPerMapPage; @@ -779,43 +338,6 @@ static Pgno ptrmapPageno(BtShared *pBt, } /* -** The pointer map is a lookup table that identifies the parent page for -** each child page in the database file. The parent page is the page that -** contains a pointer to the child. Every page in the database contains -** 0 or 1 parent pages. (In this context 'database page' refers -** to any page that is not part of the pointer map itself.) Each pointer map -** entry consists of a single byte 'type' and a 4 byte parent page number. -** The PTRMAP_XXX identifiers below are the valid types. -** -** The purpose of the pointer map is to facility moving pages from one -** position in the file to another as part of autovacuum. When a page -** is moved, the pointer in its parent must be updated to point to the -** new location. The pointer map is used to locate the parent page quickly. -** -** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not -** used in this case. -** -** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number -** is not used in this case. -** -** PTRMAP_OVERFLOW1: The database page is the first page in a list of -** overflow pages. The page number identifies the page that -** contains the cell with a pointer to this overflow page. -** -** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of -** overflow pages. The page-number identifies the previous -** page in the overflow page list. -** -** PTRMAP_BTREE: The database page is a non-root btree page. The page number -** identifies the parent page in the btree. -*/ -#define PTRMAP_ROOTPAGE 1 -#define PTRMAP_FREEPAGE 2 -#define PTRMAP_OVERFLOW1 3 -#define PTRMAP_OVERFLOW2 4 -#define PTRMAP_BTREE 5 - -/* ** Write an entry into the pointer map. ** ** This routine updates the pointer map entry for page number 'key' @@ -897,15 +419,17 @@ static int ptrmapGet(BtShared *pBt, Pgno ** ** This routine works only for pages that do not contain overflow cells. */ -static u8 *findCell(MemPage *pPage, int iCell){ +#define findCell(pPage, iCell) \ + ((pPage)->aData + get2byte(&(pPage)->aData[(pPage)->cellOffset+2*(iCell)])) +u8 *sqlite3BtreeFindCell(MemPage *pPage, int iCell){ u8 *data = pPage->aData; assert( iCell>=0 ); assert( iCellhdrOffset+3]) ); - return data + get2byte(&data[pPage->cellOffset+2*iCell]); + return findCell(pPage, iCell); } /* -** This a more complex version of findCell() that works for +** This a more complex version of sqlite3BtreeFindCell() that works for ** pages that do contain overflow cells. See insert */ static u8 *findOverflowCell(MemPage *pPage, int iCell){ @@ -927,11 +451,14 @@ static u8 *findOverflowCell(MemPage *pPa /* ** Parse a cell content block and fill in the CellInfo structure. There -** are two versions of this function. parseCell() takes a cell index -** as the second argument and parseCellPtr() takes a pointer to the -** body of the cell as its second argument. +** are two versions of this function. sqlite3BtreeParseCell() takes a +** cell index as the second argument and sqlite3BtreeParseCellPtr() +** takes a pointer to the body of the cell as its second argument. +** +** Within this file, the parseCell() macro can be called instead of +** sqlite3BtreeParseCellPtr(). Using some compilers, this will be faster. */ -static void parseCellPtr( +void sqlite3BtreeParseCellPtr( MemPage *pPage, /* Page containing the cell */ u8 *pCell, /* Pointer to the cell text. */ CellInfo *pInfo /* Fill in this structure */ @@ -997,12 +524,14 @@ static void parseCellPtr( pInfo->nSize = pInfo->iOverflow + 4; } } -static void parseCell( +#define parseCell(pPage, iCell, pInfo) \ + sqlite3BtreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo)) +void sqlite3BtreeParseCell( MemPage *pPage, /* Page containing the cell */ int iCell, /* The cell index. First cell is 0 */ CellInfo *pInfo /* Fill in this structure */ ){ - parseCellPtr(pPage, findCell(pPage, iCell), pInfo); + parseCell(pPage, iCell, pInfo); } /* @@ -1014,13 +543,13 @@ static int cellSize(MemPage *pPage, int #ifndef NDEBUG static int cellSize(MemPage *pPage, int iCell){ CellInfo info; - parseCell(pPage, iCell, &info); + sqlite3BtreeParseCell(pPage, iCell, &info); return info.nSize; } #endif static int cellSizePtr(MemPage *pPage, u8 *pCell){ CellInfo info; - parseCellPtr(pPage, pCell, &info); + sqlite3BtreeParseCellPtr(pPage, pCell, &info); return info.nSize; } @@ -1033,7 +562,7 @@ static int ptrmapPutOvflPtr(MemPage *pPa static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){ if( pCell ){ CellInfo info; - parseCellPtr(pPage, pCell, &info); + sqlite3BtreeParseCellPtr(pPage, pCell, &info); assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ Pgno ovfl = get4byte(&pCell[info.iOverflow]); @@ -1055,15 +584,6 @@ static int ptrmapPutOvfl(MemPage *pPage, #endif -/* A bunch of assert() statements to check the transaction state variables -** of handle p (type Btree*) are internally consistent. -*/ -#define btreeIntegrity(p) \ - assert( p->inTrans!=TRANS_NONE || p->pBt->nTransactionpBt->nRef ); \ - assert( p->pBt->nTransaction<=p->pBt->nRef ); \ - assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \ - assert( p->pBt->inTransaction>=p->inTrans ); - /* ** Defragment the page given. All Cells are moved to the ** end of the page and all free space is collected into one @@ -1291,7 +811,7 @@ static void decodeFlags(MemPage *pPage, ** guarantee that the page is well-formed. It only shows that ** we failed to detect any corruption. */ -static int initPage( +int sqlite3BtreeInitPage( MemPage *pPage, /* The page to be initialized */ MemPage *pParent /* The parent. Might be NULL */ ){ @@ -1404,7 +924,12 @@ static void zeroPage(MemPage *pPage, int ** means we have started to be concerned about content and the disk ** read should occur at that point. */ -static int getPage(BtShared *pBt, Pgno pgno, MemPage **ppPage, int noContent){ +int sqlite3BtreeGetPage( + BtShared *pBt, /* The btree */ + Pgno pgno, /* Number of the page to fetch */ + MemPage **ppPage, /* Return the page in this parameter */ + int noContent /* Do not load page content if true */ +){ int rc; MemPage *pPage; DbPage *pDbPage; @@ -1424,7 +949,7 @@ static int getPage(BtShared *pBt, Pgno p /* ** Get a page from the pager and initialize it. This routine ** is just a convenience wrapper around separate calls to -** getPage() and initPage(). +** sqlite3BtreeGetPage() and sqlite3BtreeInitPage(). */ static int getAndInitPage( BtShared *pBt, /* The database file */ @@ -1436,16 +961,16 @@ static int getAndInitPage( if( pgno==0 ){ return SQLITE_CORRUPT_BKPT; } - rc = getPage(pBt, pgno, ppPage, 0); + rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0); if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){ - rc = initPage(*ppPage, pParent); + rc = sqlite3BtreeInitPage(*ppPage, pParent); } return rc; } /* ** Release a MemPage. This should be called once for each prior -** call to getPage. +** call to sqlite3BtreeGetPage. */ static void releasePage(MemPage *pPage){ if( pPage ){ @@ -1487,7 +1012,7 @@ static void pageReinit(DbPage *pData, in pPage = (MemPage *)sqlite3PagerGetExtra(pData); if( pPage->isInit ){ pPage->isInit = 0; - initPage(pPage, pPage->pParent); + sqlite3BtreeInitPage(pPage, pPage->pParent); } } @@ -1506,7 +1031,7 @@ int sqlite3BtreeOpen( ){ BtShared *pBt; /* Shared part of btree structure */ Btree *p; /* Handle to return */ - int rc; + int rc = SQLITE_OK; int nReserve; unsigned char zDbHeader[100]; #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) @@ -1569,22 +1094,15 @@ int sqlite3BtreeOpen( pBt = sqliteMalloc( sizeof(*pBt) ); if( pBt==0 ){ - *ppBtree = 0; - sqliteFree(p); - return SQLITE_NOMEM; + rc = SQLITE_NOMEM; + goto btree_open_out; } rc = sqlite3PagerOpen(&pBt->pPager, zFilename, EXTRA_SIZE, flags); if( rc==SQLITE_OK ){ rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); } if( rc!=SQLITE_OK ){ - if( pBt->pPager ){ - sqlite3PagerClose(pBt->pPager); - } - sqliteFree(pBt); - sqliteFree(p); - *ppBtree = 0; - return rc; + goto btree_open_out; } p->pBt = pBt; @@ -1602,13 +1120,14 @@ int sqlite3BtreeOpen( pBt->minLeafFrac = 32; /* 12.5% */ #ifndef SQLITE_OMIT_AUTOVACUUM /* If the magic name ":memory:" will create an in-memory database, then - ** do not set the auto-vacuum flag, even if SQLITE_DEFAULT_AUTOVACUUM - ** is true. On the other hand, if SQLITE_OMIT_MEMORYDB has been defined, - ** then ":memory:" is just a regular file-name. Respect the auto-vacuum - ** default in this case. + ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if + ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if + ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a + ** regular file-name. In this case the auto-vacuum applies as per normal. */ if( zFilename && !isMemdb ){ - pBt->autoVacuum = SQLITE_DEFAULT_AUTOVACUUM; + pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0); + pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); } #endif nReserve = 0; @@ -1639,7 +1158,17 @@ int sqlite3BtreeOpen( #endif pBt->nRef = 1; *ppBtree = p; - return SQLITE_OK; + +btree_open_out: + if( rc!=SQLITE_OK ){ + if( pBt && pBt->pPager ){ + sqlite3PagerClose(pBt->pPager); + } + sqliteFree(pBt); + sqliteFree(p); + *ppBtree = 0; + } + return rc; } /* @@ -1810,6 +1339,15 @@ int sqlite3BtreeGetReserve(Btree *p){ int sqlite3BtreeGetReserve(Btree *p){ return p->pBt->pageSize - p->pBt->usableSize; } + +/* +** Set the maximum page count for a database if mxPage is positive. +** No changes are made if mxPage is 0 or negative. +** Regardless of the value of mxPage, return the maximum page count. +*/ +int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ + return sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); +} #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ /* @@ -1819,14 +1357,17 @@ int sqlite3BtreeSetAutoVacuum(Btree *p, ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. */ int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ - BtShared *pBt = p->pBt;; #ifdef SQLITE_OMIT_AUTOVACUUM return SQLITE_READONLY; #else - if( pBt->pageSizeFixed ){ + BtShared *pBt = p->pBt; + int av = (autoVacuum?1:0); + int iv = (autoVacuum==BTREE_AUTOVACUUM_INCR?1:0); + if( pBt->pageSizeFixed && av!=pBt->autoVacuum ){ return SQLITE_READONLY; } - pBt->autoVacuum = (autoVacuum?1:0); + pBt->autoVacuum = av; + pBt->incrVacuum = iv; return SQLITE_OK; #endif } @@ -1837,9 +1378,13 @@ int sqlite3BtreeGetAutoVacuum(Btree *p){ */ int sqlite3BtreeGetAutoVacuum(Btree *p){ #ifdef SQLITE_OMIT_AUTOVACUUM - return 0; + return BTREE_AUTOVACUUM_NONE; #else - return p->pBt->autoVacuum; + return ( + (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE: + (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL: + BTREE_AUTOVACUUM_INCR + ); #endif } @@ -1857,7 +1402,7 @@ static int lockBtree(BtShared *pBt){ int rc, pageSize; MemPage *pPage1; if( pBt->pPage1 ) return SQLITE_OK; - rc = getPage(pBt, 1, &pPage1, 0); + rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0); if( rc!=SQLITE_OK ) return rc; @@ -1998,9 +1543,8 @@ static int newDatabase(BtShared *pBt){ zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); pBt->pageSizeFixed = 1; #ifndef SQLITE_OMIT_AUTOVACUUM - if( pBt->autoVacuum ){ - put4byte(&data[36 + 4*4], 1); - } + assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); + put4byte(&data[36 + 4*4], pBt->autoVacuum); #endif return SQLITE_OK; } @@ -2115,12 +1659,15 @@ static int setChildPtrmaps(MemPage *pPag static int setChildPtrmaps(MemPage *pPage){ int i; /* Counter variable */ int nCell; /* Number of cells in page pPage */ - int rc = SQLITE_OK; /* Return code */ + int rc; /* Return code */ BtShared *pBt = pPage->pBt; int isInitOrig = pPage->isInit; Pgno pgno = pPage->pgno; - initPage(pPage, 0); + rc = sqlite3BtreeInitPage(pPage, pPage->pParent); + if( rc!=SQLITE_OK ){ + goto set_child_ptrmaps_out; + } nCell = pPage->nCell; for(i=0; inCell; for(i=0; inTrunc; + if( iLastPg==0 ){ + iLastPg = sqlite3PagerPagecount(pBt->pPager); + } + + if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ + int rc; + u8 eType; + Pgno iPtrPage; + + nFreeList = get4byte(&pBt->pPage1->aData[36]); + if( nFreeList==0 || nFin==iLastPg ){ + return SQLITE_DONE; + } + + rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage); + if( rc!=SQLITE_OK ){ + return rc; + } + if( eType==PTRMAP_ROOTPAGE ){ + return SQLITE_CORRUPT_BKPT; + } + + if( eType==PTRMAP_FREEPAGE ){ + if( nFin==0 ){ + /* Remove the page from the files free-list. This is not required + ** if nFin is non-zero. In that case, the free-list will be + ** truncated to zero after this function returns, so it doesn't + ** matter if it still contains some garbage entries. + */ + Pgno iFreePg; + MemPage *pFreePg; + rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1); + if( rc!=SQLITE_OK ){ + return rc; + } + assert( iFreePg==iLastPg ); + releasePage(pFreePg); + } + } else { + Pgno iFreePg; /* Index of free page to move pLastPg to */ + MemPage *pLastPg; + + rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + + /* If nFin is zero, this loop runs exactly once and page pLastPg + ** is swapped with the first free page pulled off the free list. + ** + ** On the other hand, if nFin is greater than zero, then keep + ** looping until a free-page located within the first nFin pages + ** of the file is found. + */ + do { + MemPage *pFreePg; + rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0); + if( rc!=SQLITE_OK ){ + releasePage(pLastPg); + return rc; + } + releasePage(pFreePg); + }while( nFin!=0 && iFreePg>nFin ); + assert( iFreePgpDbPage); + if( rc!=SQLITE_OK ){ + return rc; + } + rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg); + releasePage(pLastPg); + if( rc!=SQLITE_OK ){ + return rc; + } + } + } + + pBt->nTrunc = iLastPg - 1; + while( pBt->nTrunc==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, pBt->nTrunc) ){ + pBt->nTrunc--; + } + return SQLITE_OK; +} + +/* +** A write-transaction must be opened before calling this function. +** It performs a single unit of work towards an incremental vacuum. +** +** If the incremental vacuum is finished after this function has run, +** SQLITE_DONE is returned. If it is not finished, but no error occured, +** SQLITE_OK is returned. Otherwise an SQLite error code. +*/ +int sqlite3BtreeIncrVacuum(Btree *p){ + BtShared *pBt = p->pBt; + assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); + if( !pBt->autoVacuum ){ + return SQLITE_DONE; + } + invalidateAllOverflowCache(pBt); + return incrVacuumStep(pBt, 0); +} + +/* ** This routine is called prior to sqlite3PagerCommit when a transaction ** is commited for an auto-vacuum database. ** @@ -2298,135 +1967,66 @@ static int autoVacuumCommit(BtShared *pB ** pages are in use. */ static int autoVacuumCommit(BtShared *pBt, Pgno *pnTrunc){ + int rc = SQLITE_OK; Pager *pPager = pBt->pPager; - Pgno nFreeList; /* Number of pages remaining on the free-list. */ - int nPtrMap; /* Number of pointer-map pages deallocated */ - Pgno origSize; /* Pages in the database file */ - Pgno finSize; /* Pages in the database file after truncation */ - int rc; /* Return code */ - u8 eType; - int pgsz = pBt->pageSize; /* Page size for this database */ - Pgno iDbPage; /* The database page to move */ - MemPage *pDbMemPage = 0; /* "" */ - Pgno iPtrPage; /* The page that contains a pointer to iDbPage */ - Pgno iFreePage; /* The free-list page to move iDbPage to */ - MemPage *pFreeMemPage = 0; /* "" */ - #ifndef NDEBUG int nRef = sqlite3PagerRefcount(pPager); #endif - assert( pBt->autoVacuum ); - if( PTRMAP_ISPAGE(pBt, sqlite3PagerPagecount(pPager)) ){ - return SQLITE_CORRUPT_BKPT; - } + invalidateAllOverflowCache(pBt); + assert(pBt->autoVacuum); + if( !pBt->incrVacuum ){ + Pgno nFin = 0; - /* Figure out how many free-pages are in the database. If there are no - ** free pages, then auto-vacuum is a no-op. - */ - nFreeList = get4byte(&pBt->pPage1->aData[36]); - if( nFreeList==0 ){ - *pnTrunc = 0; - return SQLITE_OK; - } + if( pBt->nTrunc==0 ){ + Pgno nFree; + Pgno nPtrmap; + const int pgsz = pBt->pageSize; + Pgno nOrig = sqlite3PagerPagecount(pBt->pPager); - /* This block figures out how many pages there are in the database - ** now (variable origSize), and how many there will be after the - ** truncation (variable finSize). - ** - ** The final size is the original size, less the number of free pages - ** in the database, less any pointer-map pages that will no longer - ** be required, less 1 if the pending-byte page was part of the database - ** but is not after the truncation. - **/ - origSize = sqlite3PagerPagecount(pPager); - if( origSize==PENDING_BYTE_PAGE(pBt) ){ - origSize--; - } - nPtrMap = (nFreeList-origSize+PTRMAP_PAGENO(pBt, origSize)+pgsz/5)/(pgsz/5); - finSize = origSize - nFreeList - nPtrMap; - if( origSize>PENDING_BYTE_PAGE(pBt) && finSize<=PENDING_BYTE_PAGE(pBt) ){ - finSize--; - } - while( PTRMAP_ISPAGE(pBt, finSize) || finSize==PENDING_BYTE_PAGE(pBt) ){ - finSize--; - } - TRACE(("AUTOVACUUM: Begin (db size %d->%d)\n", origSize, finSize)); - - /* Variable 'finSize' will be the size of the file in pages after - ** the auto-vacuum has completed (the current file size minus the number - ** of pages on the free list). Loop through the pages that lie beyond - ** this mark, and if they are not already on the free list, move them - ** to a free page earlier in the file (somewhere before finSize). - */ - for( iDbPage=finSize+1; iDbPage<=origSize; iDbPage++ ){ - /* If iDbPage is a pointer map page, or the pending-byte page, skip it. */ - if( PTRMAP_ISPAGE(pBt, iDbPage) || iDbPage==PENDING_BYTE_PAGE(pBt) ){ - continue; + if( PTRMAP_ISPAGE(pBt, nOrig) ){ + return SQLITE_CORRUPT_BKPT; + } + if( nOrig==PENDING_BYTE_PAGE(pBt) ){ + nOrig--; + } + nFree = get4byte(&pBt->pPage1->aData[36]); + nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5); + nFin = nOrig - nFree - nPtrmap; + if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){ + nFin--; + } + while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){ + nFin--; + } } - rc = ptrmapGet(pBt, iDbPage, &eType, &iPtrPage); - if( rc!=SQLITE_OK ) goto autovacuum_out; - if( eType==PTRMAP_ROOTPAGE ){ - rc = SQLITE_CORRUPT_BKPT; - goto autovacuum_out; + while( rc==SQLITE_OK ){ + rc = incrVacuumStep(pBt, nFin); } - - /* If iDbPage is free, do not swap it. */ - if( eType==PTRMAP_FREEPAGE ){ - continue; - } - rc = getPage(pBt, iDbPage, &pDbMemPage, 0); - if( rc!=SQLITE_OK ) goto autovacuum_out; - - /* Find the next page in the free-list that is not already at the end - ** of the file. A page can be pulled off the free list using the - ** allocateBtreePage() routine. - */ - do{ - if( pFreeMemPage ){ - releasePage(pFreeMemPage); - pFreeMemPage = 0; + if( rc==SQLITE_DONE ){ + assert(nFin==0 || pBt->nTrunc==0 || nFin<=pBt->nTrunc); + rc = SQLITE_OK; + if( pBt->nTrunc ){ + sqlite3PagerWrite(pBt->pPage1->pDbPage); + put4byte(&pBt->pPage1->aData[32], 0); + put4byte(&pBt->pPage1->aData[36], 0); + pBt->nTrunc = nFin; } - rc = allocateBtreePage(pBt, &pFreeMemPage, &iFreePage, 0, 0); - if( rc!=SQLITE_OK ){ - releasePage(pDbMemPage); - goto autovacuum_out; - } - assert( iFreePage<=origSize ); - }while( iFreePage>finSize ); - releasePage(pFreeMemPage); - pFreeMemPage = 0; - - /* Relocate the page into the body of the file. Note that although the - ** page has moved within the database file, the pDbMemPage pointer - ** remains valid. This means that this function can run without - ** invalidating cursors open on the btree. This is important in - ** shared-cache mode. - */ - rc = relocatePage(pBt, pDbMemPage, eType, iPtrPage, iFreePage); - releasePage(pDbMemPage); - if( rc!=SQLITE_OK ) goto autovacuum_out; + } + if( rc!=SQLITE_OK ){ + sqlite3PagerRollback(pPager); + } } - /* The entire free-list has been swapped to the end of the file. So - ** truncate the database file to finSize pages and consider the - ** free-list empty. - */ - rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); - if( rc!=SQLITE_OK ) goto autovacuum_out; - put4byte(&pBt->pPage1->aData[32], 0); - put4byte(&pBt->pPage1->aData[36], 0); - *pnTrunc = finSize; - assert( finSize!=PENDING_BYTE_PAGE(pBt) ); - -autovacuum_out: + if( rc==SQLITE_OK ){ + *pnTrunc = pBt->nTrunc; + pBt->nTrunc = 0; + } assert( nRef==sqlite3PagerRefcount(pPager) ); - if( rc!=SQLITE_OK ){ - sqlite3PagerRollback(pPager); - } return rc; } + #endif /* @@ -2558,26 +2158,7 @@ static int countWriteCursors(BtShared *p } #endif -#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) /* -** Print debugging information about all cursors to standard output. -*/ -void sqlite3BtreeCursorList(Btree *p){ - BtCursor *pCur; - BtShared *pBt = p->pBt; - for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ - MemPage *pPage = pCur->pPage; - char *zMode = pCur->wrFlag ? "rw" : "ro"; - sqlite3DebugPrintf("CURSOR %p rooted at %4d(%s) currently at %d.%d%s\n", - pCur, pCur->pgnoRoot, zMode, - pPage ? pPage->pgno : 0, pCur->idx, - (pCur->eState==CURSOR_VALID) ? "" : " eof" - ); - } -} -#endif - -/* ** Rollback the transaction in progress. All cursors will be ** invalided by this operation. Any attempt to use a cursor ** that was open at the beginning of this operation will result @@ -2615,6 +2196,10 @@ int sqlite3BtreeRollback(Btree *p){ if( p->inTrans==TRANS_WRITE ){ int rc2; +#ifndef SQLITE_OMIT_AUTOVACUUM + pBt->nTrunc = 0; +#endif + assert( TRANS_WRITE==pBt->inTransaction ); rc2 = sqlite3PagerRollback(pBt->pPager); if( rc2!=SQLITE_OK ){ @@ -2622,9 +2207,9 @@ int sqlite3BtreeRollback(Btree *p){ } /* The rollback may have destroyed the pPage1->aData value. So - ** call getPage() on page 1 again to make sure pPage1->aData is - ** set correctly. */ - if( getPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ + ** call sqlite3BtreeGetPage() on page 1 again to make + ** sure pPage1->aData is set correctly. */ + if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ releasePage(pPage1); } assert( countWriteCursors(pBt)==0 ); @@ -2834,21 +2419,7 @@ create_cursor_exception: return rc; } -#if 0 /* Not Used */ /* -** Change the value of the comparison function used by a cursor. -*/ -void sqlite3BtreeSetCompare( - BtCursor *pCur, /* The cursor to whose comparison function is changed */ - int(*xCmp)(void*,int,const void*,int,const void*), /* New comparison func */ - void *pArg /* First argument to xCmp() */ -){ - pCur->xCompare = xCmp ? xCmp : dfltCompare; - pCur->pArg = pArg; -} -#endif - -/* ** Close a cursor. The read lock on the database file is released ** when the last cursor is closed. */ @@ -2865,6 +2436,7 @@ int sqlite3BtreeCloseCursor(BtCursor *pC } releasePage(pCur->pPage); unlockBtreeIfUnused(pBt); + invalidateOverflowCache(pCur); sqliteFree(pCur); return SQLITE_OK; } @@ -2873,7 +2445,7 @@ int sqlite3BtreeCloseCursor(BtCursor *pC ** Make a temporary cursor by filling in the fields of pTempCur. ** The temporary cursor is not on the cursor list for the Btree. */ -static void getTempCursor(BtCursor *pCur, BtCursor *pTempCur){ +void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){ memcpy(pTempCur, pCur, sizeof(*pCur)); pTempCur->pNext = 0; pTempCur->pPrev = 0; @@ -2886,32 +2458,39 @@ static void getTempCursor(BtCursor *pCur ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() ** function above. */ -static void releaseTempCursor(BtCursor *pCur){ +void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){ if( pCur->pPage ){ sqlite3PagerUnref(pCur->pPage->pDbPage); } } /* -** Make sure the BtCursor.info field of the given cursor is valid. -** If it is not already valid, call parseCell() to fill it in. +** The GET_CELL_INFO() macro. Takes one argument, a pointer to a valid +** btree cursor (type BtCursor*). This macro makes sure the BtCursor.info +** field of the given cursor is valid. If it is not already valid, call +** sqlite3BtreeParseCell() to fill it in. ** ** BtCursor.info is a cache of the information in the current cell. -** Using this cache reduces the number of calls to parseCell(). +** Using this cache reduces the number of calls to sqlite3BtreeParseCell(). */ -static void getCellInfo(BtCursor *pCur){ - if( pCur->info.nSize==0 ){ - parseCell(pCur->pPage, pCur->idx, &pCur->info); - }else{ #ifndef NDEBUG + static void assertCellInfo(BtCursor *pCur){ CellInfo info; memset(&info, 0, sizeof(info)); - parseCell(pCur->pPage, pCur->idx, &info); + sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &info); assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); + } +#else + #define assertCellInfo(x) #endif - } -} +#define GET_CELL_INFO(pCur) \ + if( pCur->info.nSize==0 ) \ + sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info); \ + else \ + assertCellInfo(pCur); + + /* ** Set *pSize to the size of the buffer needed to hold the value of ** the key for the current entry. If the cursor is not pointing @@ -2927,7 +2506,7 @@ int sqlite3BtreeKeySize(BtCursor *pCur, if( pCur->eState==CURSOR_INVALID ){ *pSize = 0; }else{ - getCellInfo(pCur); + GET_CELL_INFO(pCur); *pSize = pCur->info.nKey; } } @@ -2949,7 +2528,7 @@ int sqlite3BtreeDataSize(BtCursor *pCur, /* Not pointing at a valid entry - set *pSize to 0. */ *pSize = 0; }else{ - getCellInfo(pCur); + GET_CELL_INFO(pCur); *pSize = pCur->info.nData; } } @@ -2957,94 +2536,281 @@ int sqlite3BtreeDataSize(BtCursor *pCur, } /* -** Read payload information from the entry that the pCur cursor is -** pointing to. Begin reading the payload at "offset" and read -** a total of "amt" bytes. Put the result in zBuf. +** Given the page number of an overflow page in the database (parameter +** ovfl), this function finds the page number of the next page in the +** linked list of overflow pages. If possible, it uses the auto-vacuum +** pointer-map data instead of reading the content of page ovfl to do so. ** +** If an error occurs an SQLite error code is returned. Otherwise: +** +** Unless pPgnoNext is NULL, the page number of the next overflow +** page in the linked list is written to *pPgnoNext. If page ovfl +** is the last page in it's linked list, *pPgnoNext is set to zero. +** +** If ppPage is not NULL, *ppPage is set to the MemPage* handle +** for page ovfl. The underlying pager page may have been requested +** with the noContent flag set, so the page data accessable via +** this handle may not be trusted. +*/ +static int getOverflowPage( + BtShared *pBt, + Pgno ovfl, /* Overflow page */ + MemPage **ppPage, /* OUT: MemPage handle */ + Pgno *pPgnoNext /* OUT: Next overflow page number */ +){ + Pgno next = 0; + int rc; + + /* One of these must not be NULL. Otherwise, why call this function? */ + assert(ppPage || pPgnoNext); + + /* If pPgnoNext is NULL, then this function is being called to obtain + ** a MemPage* reference only. No page-data is required in this case. + */ + if( !pPgnoNext ){ + return sqlite3BtreeGetPage(pBt, ovfl, ppPage, 1); + } + +#ifndef SQLITE_OMIT_AUTOVACUUM + /* Try to find the next page in the overflow list using the + ** autovacuum pointer-map pages. Guess that the next page in + ** the overflow list is page number (ovfl+1). If that guess turns + ** out to be wrong, fall back to loading the data of page + ** number ovfl to determine the next page number. + */ + if( pBt->autoVacuum ){ + Pgno pgno; + Pgno iGuess = ovfl+1; + u8 eType; + + while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ + iGuess++; + } + + if( iGuess<=sqlite3PagerPagecount(pBt->pPager) ){ + rc = ptrmapGet(pBt, iGuess, &eType, &pgno); + if( rc!=SQLITE_OK ){ + return rc; + } + if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ + next = iGuess; + } + } + } +#endif + + if( next==0 || ppPage ){ + MemPage *pPage = 0; + + rc = sqlite3BtreeGetPage(pBt, ovfl, &pPage, next!=0); + assert(rc==SQLITE_OK || pPage==0); + if( next==0 && rc==SQLITE_OK ){ + next = get4byte(pPage->aData); + } + + if( ppPage ){ + *ppPage = pPage; + }else{ + releasePage(pPage); + } + } + *pPgnoNext = next; + + return rc; +} + +/* +** Copy data from a buffer to a page, or from a page to a buffer. +** +** pPayload is a pointer to data stored on database page pDbPage. +** If argument eOp is false, then nByte bytes of data are copied +** from pPayload to the buffer pointed at by pBuf. If eOp is true, +** then sqlite3PagerWrite() is called on pDbPage and nByte bytes +** of data are copied from the buffer pBuf to pPayload. +** +** SQLITE_OK is returned on success, otherwise an error code. +*/ +static int copyPayload( + void *pPayload, /* Pointer to page data */ + void *pBuf, /* Pointer to buffer */ + int nByte, /* Number of bytes to copy */ + int eOp, /* 0 -> copy from page, 1 -> copy to page */ + DbPage *pDbPage /* Page containing pPayload */ +){ + if( eOp ){ + /* Copy data from buffer to page (a write operation) */ + int rc = sqlite3PagerWrite(pDbPage); + if( rc!=SQLITE_OK ){ + return rc; + } + memcpy(pPayload, pBuf, nByte); + }else{ + /* Copy data from page to buffer (a read operation) */ + memcpy(pBuf, pPayload, nByte); + } + return SQLITE_OK; +} + +/* +** This function is used to read or overwrite payload information +** for the entry that the pCur cursor is pointing to. If the eOp +** parameter is 0, this is a read operation (data copied into +** buffer pBuf). If it is non-zero, a write (data copied from +** buffer pBuf). +** +** A total of "amt" bytes are read or written beginning at "offset". +** Data is read to or from the buffer pBuf. +** ** This routine does not make a distinction between key and data. -** It just reads bytes from the payload area. Data might appear -** on the main page or be scattered out on multiple overflow pages. +** It just reads or writes bytes from the payload area. Data might +** appear on the main page or be scattered out on multiple overflow +** pages. +** +** If the BtCursor.isIncrblobHandle flag is set, and the current +** cursor entry uses one or more overflow pages, this function +** allocates space for and lazily popluates the overflow page-list +** cache array (BtCursor.aOverflow). Subsequent calls use this +** cache to make seeking to the supplied offset more efficient. +** +** Once an overflow page-list cache has been allocated, it may be +** invalidated if some other cursor writes to the same table, or if +** the cursor is moved to a different row. Additionally, in auto-vacuum +** mode, the following events may invalidate an overflow page-list cache. +** +** * An incremental vacuum, +** * A commit in auto_vacuum="full" mode, +** * Creating a table (may require moving an overflow page). */ -static int getPayload( +static int accessPayload( BtCursor *pCur, /* Cursor pointing to entry to read from */ int offset, /* Begin reading this far into payload */ int amt, /* Read this many bytes */ unsigned char *pBuf, /* Write the bytes into this buffer */ - int skipKey /* offset begins at data if this is true */ + int skipKey, /* offset begins at data if this is true */ + int eOp /* zero to read. non-zero to write. */ ){ unsigned char *aPayload; - Pgno nextPage; - int rc; - MemPage *pPage; - BtShared *pBt; - int ovflSize; + int rc = SQLITE_OK; u32 nKey; + int iIdx = 0; + MemPage *pPage = pCur->pPage; /* Btree page of current cursor entry */ + BtShared *pBt = pCur->pBtree->pBt; /* Btree this cursor belongs to */ - assert( pCur!=0 && pCur->pPage!=0 ); + assert( pPage ); assert( pCur->eState==CURSOR_VALID ); - pBt = pCur->pBtree->pBt; - pPage = pCur->pPage; assert( pCur->idx>=0 && pCur->idxnCell ); - getCellInfo(pCur); + assert( offset>=0 ); + + GET_CELL_INFO(pCur); aPayload = pCur->info.pCell + pCur->info.nHeader; - if( pPage->intKey ){ - nKey = 0; - }else{ - nKey = pCur->info.nKey; - } - assert( offset>=0 ); + nKey = (pPage->intKey ? 0 : pCur->info.nKey); + if( skipKey ){ offset += nKey; } if( offset+amt > nKey+pCur->info.nData ){ + /* Trying to read or write past the end of the data is an error */ return SQLITE_ERROR; } + + /* Check if data must be read/written to/from the btree page itself. */ if( offsetinfo.nLocal ){ int a = amt; if( a+offset>pCur->info.nLocal ){ a = pCur->info.nLocal - offset; } - memcpy(pBuf, &aPayload[offset], a); - if( a==amt ){ - return SQLITE_OK; - } + rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage); offset = 0; pBuf += a; amt -= a; }else{ offset -= pCur->info.nLocal; } - ovflSize = pBt->usableSize - 4; - if( amt>0 ){ + + if( rc==SQLITE_OK && amt>0 ){ + const int ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ + Pgno nextPage; + nextPage = get4byte(&aPayload[pCur->info.nLocal]); - while( amt>0 && nextPage ){ - DbPage *pDbPage; - rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage); - if( rc!=0 ){ - return rc; + +#ifndef SQLITE_OMIT_INCRBLOB + /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[] + ** has not been allocated, allocate it now. The array is sized at + ** one entry for each overflow page in the overflow chain. The + ** page number of the first overflow page is stored in aOverflow[0], + ** etc. A value of 0 in the aOverflow[] array means "not yet known" + ** (the cache is lazily populated). + */ + if( pCur->isIncrblobHandle && !pCur->aOverflow ){ + int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; + pCur->aOverflow = (Pgno *)sqliteMalloc(sizeof(Pgno)*nOvfl); + if( nOvfl && !pCur->aOverflow ){ + rc = SQLITE_NOMEM; } - aPayload = sqlite3PagerGetData(pDbPage); - nextPage = get4byte(aPayload); - if( offsetaOverflow && pCur->aOverflow[offset/ovflSize] ){ + iIdx = (offset/ovflSize); + nextPage = pCur->aOverflow[iIdx]; + offset = (offset%ovflSize); + } +#endif + + for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ + +#ifndef SQLITE_OMIT_INCRBLOB + /* If required, populate the overflow page-list cache. */ + if( pCur->aOverflow ){ + assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage); + pCur->aOverflow[iIdx] = nextPage; + } +#endif + + if( offset>=ovflSize ){ + /* The only reason to read this page is to obtain the page + ** number for the next page in the overflow chain. The page + ** data is not required. So first try to lookup the overflow + ** page-list cache, if any, then fall back to the getOverflowPage() + ** function. + */ +#ifndef SQLITE_OMIT_INCRBLOB + if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){ + nextPage = pCur->aOverflow[iIdx+1]; + } else +#endif + rc = getOverflowPage(pBt, nextPage, 0, &nextPage); + offset -= ovflSize; + }else{ + /* Need to read this page properly. It contains some of the + ** range of data that is being read (eOp==0) or written (eOp!=0). + */ + DbPage *pDbPage; int a = amt; - if( a + offset > ovflSize ){ - a = ovflSize - offset; + rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage); + if( rc==SQLITE_OK ){ + aPayload = sqlite3PagerGetData(pDbPage); + nextPage = get4byte(aPayload); + if( a + offset > ovflSize ){ + a = ovflSize - offset; + } + rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); + sqlite3PagerUnref(pDbPage); + offset = 0; + amt -= a; + pBuf += a; } - memcpy(pBuf, &aPayload[offset+4], a); - offset = 0; - amt -= a; - pBuf += a; - }else{ - offset -= ovflSize; } - sqlite3PagerUnref(pDbPage); } } - if( amt>0 ){ + if( rc==SQLITE_OK && amt>0 ){ return SQLITE_CORRUPT_BKPT; } - return SQLITE_OK; + return rc; } /* @@ -3066,7 +2832,7 @@ int sqlite3BtreeKey(BtCursor *pCur, u32 } assert( pCur->pPage->intKey==0 ); assert( pCur->idx>=0 && pCur->idxpPage->nCell ); - rc = getPayload(pCur, offset, amt, (unsigned char*)pBuf, 0); + rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0, 0); } return rc; } @@ -3086,7 +2852,7 @@ int sqlite3BtreeData(BtCursor *pCur, u32 assert( pCur->eState==CURSOR_VALID ); assert( pCur->pPage!=0 ); assert( pCur->idx>=0 && pCur->idxpPage->nCell ); - rc = getPayload(pCur, offset, amt, pBuf, 1); + rc = accessPayload(pCur, offset, amt, pBuf, 1, 0); } return rc; } @@ -3103,7 +2869,7 @@ int sqlite3BtreeData(BtCursor *pCur, u32 ** and data to fit on the local page and for there to be no overflow ** pages. When that is so, this routine can be used to access the ** key and data without making a copy. If the key and/or data spills -** onto overflow pages, then getPayload() must be used to reassembly +** onto overflow pages, then accessPayload() must be used to reassembly ** the key/data and copy it into a preallocated buffer. ** ** The pointer returned by this routine looks directly into the cached @@ -3124,7 +2890,7 @@ static const unsigned char *fetchPayload assert( pCur->eState==CURSOR_VALID ); pPage = pCur->pPage; assert( pCur->idx>=0 && pCur->idxnCell ); - getCellInfo(pCur); + GET_CELL_INFO(pCur); aPayload = pCur->info.pCell; aPayload += pCur->info.nHeader; if( pPage->intKey ){ @@ -3206,7 +2972,7 @@ static int moveToChild(BtCursor *pCur, u ** virtual root page is the page that the right-pointer of page ** 1 is pointing to. */ -static int isRootPage(MemPage *pPage){ +int sqlite3BtreeIsRootPage(MemPage *pPage){ MemPage *pParent = pPage->pParent; if( pParent==0 ) return 1; if( pParent->pgno>1 ) return 0; @@ -3222,7 +2988,7 @@ static int isRootPage(MemPage *pPage){ ** right-most child page then pCur->idx is set to one more than ** the largest cell index. */ -static void moveToParent(BtCursor *pCur){ +void sqlite3BtreeMoveToParent(BtCursor *pCur){ MemPage *pParent; MemPage *pPage; int idxParent; @@ -3230,7 +2996,7 @@ static void moveToParent(BtCursor *pCur) assert( pCur->eState==CURSOR_VALID ); pPage = pCur->pPage; assert( pPage!=0 ); - assert( !isRootPage(pPage) ); + assert( !sqlite3BtreeIsRootPage(pPage) ); pParent = pPage->pParent; assert( pParent!=0 ); idxParent = pPage->idxParent; @@ -3560,12 +3326,12 @@ int sqlite3BtreeNext(BtCursor *pCur, int return rc; } do{ - if( isRootPage(pPage) ){ + if( sqlite3BtreeIsRootPage(pPage) ){ *pRes = 1; pCur->eState = CURSOR_INVALID; return SQLITE_OK; } - moveToParent(pCur); + sqlite3BtreeMoveToParent(pCur); pPage = pCur->pPage; }while( pCur->idx>=pPage->nCell ); *pRes = 0; @@ -3620,12 +3386,12 @@ int sqlite3BtreePrevious(BtCursor *pCur, rc = moveToRightmost(pCur); }else{ while( pCur->idx==0 ){ - if( isRootPage(pPage) ){ + if( sqlite3BtreeIsRootPage(pPage) ){ pCur->eState = CURSOR_INVALID; *pRes = 1; return SQLITE_OK; } - moveToParent(pCur); + sqlite3BtreeMoveToParent(pCur); pPage = pCur->pPage; } pCur->idx--; @@ -3687,7 +3453,7 @@ static int allocateBtreePage( ** the entire-list will be searched for that page. */ #ifndef SQLITE_OMIT_AUTOVACUUM - if( exact ){ + if( exact && nearby<=sqlite3PagerPagecount(pBt->pPager) ){ u8 eType; assert( nearby>0 ); assert( pBt->autoVacuum ); @@ -3718,7 +3484,7 @@ static int allocateBtreePage( }else{ iTrunk = get4byte(&pPage1->aData[32]); } - rc = getPage(pBt, iTrunk, &pTrunk, 0); + rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0); if( rc ){ pTrunk = 0; goto end_allocate_page; @@ -3768,7 +3534,7 @@ static int allocateBtreePage( */ MemPage *pNewTrunk; Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); - rc = getPage(pBt, iNewTrunk, &pNewTrunk, 0); + rc = sqlite3BtreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); if( rc!=SQLITE_OK ){ goto end_allocate_page; } @@ -3834,7 +3600,7 @@ static int allocateBtreePage( memcpy(&aData[8+closest*4], &aData[4+k*4], 4); } put4byte(&aData[4], k-1); - rc = getPage(pBt, *pPgno, ppPage, 1); + rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 1); if( rc==SQLITE_OK ){ sqlite3PagerDontRollback((*ppPage)->pDbPage); rc = sqlite3PagerWrite((*ppPage)->pDbPage); @@ -3854,6 +3620,16 @@ static int allocateBtreePage( *pPgno = sqlite3PagerPagecount(pBt->pPager) + 1; #ifndef SQLITE_OMIT_AUTOVACUUM + if( pBt->nTrunc ){ + /* An incr-vacuum has already run within this transaction. So the + ** page to allocate is not from the physical end of the file, but + ** at pBt->nTrunc. + */ + *pPgno = pBt->nTrunc+1; + if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ + (*pPgno)++; + } + } if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){ /* If *pPgno refers to a pointer-map page, allocate two new pages ** at the end of the file instead of one. The first allocated page @@ -3863,10 +3639,13 @@ static int allocateBtreePage( assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); (*pPgno)++; } + if( pBt->nTrunc ){ + pBt->nTrunc = *pPgno; + } #endif assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); - rc = getPage(pBt, *pPgno, ppPage, 0); + rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 0); if( rc ) return rc; rc = sqlite3PagerWrite((*ppPage)->pDbPage); if( rc!=SQLITE_OK ){ @@ -3935,7 +3714,7 @@ static int freePage(MemPage *pPage){ /* Other free pages already exist. Retrive the first trunk page ** of the freelist and find out how many leaves it has. */ MemPage *pTrunk; - rc = getPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0); + rc = sqlite3BtreeGetPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0); if( rc ) return rc; k = get4byte(&pTrunk->aData[4]); if( k>=pBt->usableSize/4 - 8 ){ @@ -3976,7 +3755,7 @@ static int clearCell(MemPage *pPage, uns int nOvfl; int ovflPageSize; - parseCellPtr(pPage, pCell, &info); + sqlite3BtreeParseCellPtr(pPage, pCell, &info); if( info.iOverflow==0 ){ return SQLITE_OK; /* No overflow pages. Return without doing anything */ } @@ -3989,11 +3768,9 @@ static int clearCell(MemPage *pPage, uns if( ovflPgno==0 || ovflPgno>sqlite3PagerPagecount(pBt->pPager) ){ return SQLITE_CORRUPT_BKPT; } - rc = getPage(pBt, ovflPgno, &pOvfl, nOvfl==0); + + rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno); if( rc ) return rc; - if( nOvfl ){ - ovflPgno = get4byte(pOvfl->aData); - } rc = freePage(pOvfl); sqlite3PagerUnref(pOvfl->pDbPage); if( rc ) return rc; @@ -4018,6 +3795,7 @@ static int fillInCell( unsigned char *pCell, /* Complete text of the cell */ const void *pKey, i64 nKey, /* The key */ const void *pData,int nData, /* The data */ + int nZero, /* Extra zero bytes to append to pData */ int *pnSize /* Write cell size here */ ){ int nPayload; @@ -4039,18 +3817,18 @@ static int fillInCell( nHeader += 4; } if( pPage->hasData ){ - nHeader += putVarint(&pCell[nHeader], nData); + nHeader += putVarint(&pCell[nHeader], nData+nZero); }else{ - nData = 0; + nData = nZero = 0; } nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); - parseCellPtr(pPage, pCell, &info); + sqlite3BtreeParseCellPtr(pPage, pCell, &info); assert( info.nHeader==nHeader ); assert( info.nKey==nKey ); - assert( info.nData==nData ); + assert( info.nData==nData+nZero ); /* Fill in the payload */ - nPayload = nData; + nPayload = nData + nZero; if( pPage->intKey ){ pSrc = pData; nSrc = nData; @@ -4067,18 +3845,38 @@ static int fillInCell( while( nPayload>0 ){ if( spaceLeft==0 ){ + int isExact = 0; #ifndef SQLITE_OMIT_AUTOVACUUM Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ + if( pBt->autoVacuum ){ + do{ + pgnoOvfl++; + } while( + PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) + ); + if( pgnoOvfl>1 ){ + /* isExact = 1; */ + } + } #endif - rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0); + rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, isExact); #ifndef SQLITE_OMIT_AUTOVACUUM /* If the database supports auto-vacuum, and the second or subsequent ** overflow page is being allocated, add an entry to the pointer-map - ** for that page now. The entry for the first overflow page will be - ** added later, by the insertCell() routine. + ** for that page now. + ** + ** If this is the first overflow page, then write a partial entry + ** to the pointer-map. If we write nothing to this pointer-map slot, + ** then the optimistic overflow chain processing in clearCell() + ** may misinterpret the uninitialised values and delete the + ** wrong pages from the database. */ - if( pBt->autoVacuum && pgnoPtrmap!=0 && rc==SQLITE_OK ){ - rc = ptrmapPut(pBt, pgnoOvfl, PTRMAP_OVERFLOW2, pgnoPtrmap); + if( pBt->autoVacuum && rc==SQLITE_OK ){ + u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1); + rc = ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap); + if( rc ){ + releasePage(pOvfl); + } } #endif if( rc ){ @@ -4095,9 +3893,13 @@ static int fillInCell( } n = nPayload; if( n>spaceLeft ) n = spaceLeft; - if( n>nSrc ) n = nSrc; - assert( pSrc ); - memcpy(pPayload, pSrc, n); + if( nSrc>0 ){ + if( n>nSrc ) n = nSrc; + assert( pSrc ); + memcpy(pPayload, pSrc, n); + }else{ + memset(pPayload, 0, n); + } nPayload -= n; pPayload += n; pSrc += n; @@ -4293,7 +4095,7 @@ static int insertCell( ** the entry for the overflow page into the pointer map. */ CellInfo info; - parseCellPtr(pPage, pCell, &info); + sqlite3BtreeParseCellPtr(pPage, pCell, &info); assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); @@ -4421,8 +4223,9 @@ static int balance_quick(MemPage *pPage, ** pPage is the next-to-right child. */ assert( pPage->nCell>0 ); - parseCellPtr(pPage, findCell(pPage, pPage->nCell-1), &info); - rc = fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, &parentSize); + pCell = findCell(pPage, pPage->nCell-1); + sqlite3BtreeParseCellPtr(pPage, pCell, &info); + rc = fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, 0, &parentSize); if( rc!=SQLITE_OK ){ return rc; } @@ -4441,11 +4244,11 @@ static int balance_quick(MemPage *pPage, */ if( pBt->autoVacuum ){ rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno); - if( rc!=SQLITE_OK ){ - return rc; + if( rc==SQLITE_OK ){ + rc = ptrmapPutOvfl(pNew, 0); } - rc = ptrmapPutOvfl(pNew, 0); if( rc!=SQLITE_OK ){ + releasePage(pNew); return rc; } } @@ -4460,19 +4263,6 @@ static int balance_quick(MemPage *pPage, #endif /* SQLITE_OMIT_QUICKBALANCE */ /* -** The ISAUTOVACUUM macro is used within balance_nonroot() to determine -** if the database supports auto-vacuum or not. Because it is used -** within an expression that is an argument to another macro -** (sqliteMallocRaw), it is not possible to use conditional compilation. -** So, this macro is defined instead. -*/ -#ifndef SQLITE_OMIT_AUTOVACUUM -#define ISAUTOVACUUM (pBt->autoVacuum) -#else -#define ISAUTOVACUUM 0 -#endif - -/* ** This routine redistributes Cells on pPage and up to NN*2 siblings ** of pPage so that all pages have about the same amount of free space. ** Usually NN siblings on either side of pPage is used in the balancing, @@ -4503,7 +4293,7 @@ static int balance_nonroot(MemPage *pPag */ static int balance_nonroot(MemPage *pPage){ MemPage *pParent; /* The parent of pPage */ - BtShared *pBt; /* The whole database */ + BtShared *pBt; /* The whole database */ int nCell = 0; /* Number of cells in apCell[] */ int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ int nOld; /* Number of pages in apOld[] */ @@ -4756,6 +4546,10 @@ static int balance_nonroot(MemPage *pPag memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4); }else{ assert( leafCorrection==4 ); + if( szCell[nCell]<4 ){ + /* Do not allow any cells smaller than 4 bytes. */ + szCell[nCell] = 4; + } } nCell++; } @@ -4966,9 +4760,9 @@ static int balance_nonroot(MemPage *pPag */ CellInfo info; j--; - parseCellPtr(pNew, apCell[j], &info); + sqlite3BtreeParseCellPtr(pNew, apCell[j], &info); pCell = &aSpace[iSpace]; - fillInCell(pParent, pCell, 0, info.nKey, 0, 0, &sz); + fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz); iSpace += sz; assert( iSpace<=pBt->pageSize*5 ); pTemp = 0; @@ -4977,6 +4771,21 @@ static int balance_nonroot(MemPage *pPag pTemp = &aSpace[iSpace]; iSpace += sz; assert( iSpace<=pBt->pageSize*5 ); + /* Obscure case for non-leaf-data trees: If the cell at pCell was + ** previously stored on a leaf node, and it's reported size was 4 + ** bytes, then it may actually be smaller than this + ** (see sqlite3BtreeParseCellPtr(), 4 bytes is the minimum size of + ** any cell). But it's important to pass the correct size to + ** insertCell(), so reparse the cell now. + ** + ** Note that this can never happen in an SQLite data file, as all + ** cells are at least 4 bytes. It only happens in b-trees used + ** to evaluate "IN (SELECT ...)" and similar clauses. + */ + if( szCell[j]==4 ){ + assert(leafCorrection==4); + sz = cellSizePtr(pParent, pCell); + } } rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4); if( rc!=SQLITE_OK ) goto balance_cleanup; @@ -5087,10 +4896,10 @@ static int balance_shallower(MemPage *pP pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]); assert( pgnoChild>0 ); assert( pgnoChild<=sqlite3PagerPagecount(pPage->pBt->pPager) ); - rc = getPage(pPage->pBt, pgnoChild, &pChild, 0); + rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0); if( rc ) goto end_shallow_balance; if( pPage->pgno==1 ){ - rc = initPage(pChild, pPage); + rc = sqlite3BtreeInitPage(pChild, pPage); if( rc ) goto end_shallow_balance; assert( pChild->nOverflow==0 ); if( pChild->nFree>=100 ){ @@ -5117,7 +4926,7 @@ static int balance_shallower(MemPage *pP memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize); pPage->isInit = 0; pPage->pParent = 0; - rc = initPage(pPage, 0); + rc = sqlite3BtreeInitPage(pPage, 0); assert( rc==SQLITE_OK ); freePage(pChild); TRACE(("BALANCE: transfer child %d into root %d\n", @@ -5179,7 +4988,7 @@ static int balance_deeper(MemPage *pPage memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr); memcpy(&cdata[brk], &data[brk], usableSize-brk); assert( pChild->isInit==0 ); - rc = initPage(pChild, pPage); + rc = sqlite3BtreeInitPage(pChild, pPage); if( rc ) goto balancedeeper_out; memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0])); pChild->nOverflow = pPage->nOverflow; @@ -5242,7 +5051,7 @@ static int balance(MemPage *pPage, int i ** ** In addition to checking for read-locks (where a read-lock ** means a cursor opened with wrFlag==0) this routine also moves -** all cursors write cursors so that they are pointing to the +** all write cursors so that they are pointing to the ** first Cell on the root page. This is necessary because an insert ** or delete might change the number of cells on a page or delete ** a page entirely and we do not want to leave any cursors @@ -5282,6 +5091,7 @@ int sqlite3BtreeInsert( BtCursor *pCur, /* Insert data into the table of this cursor */ const void *pKey, i64 nKey, /* The key of the new record */ const void *pData, int nData, /* The data of the new record */ + int nZero, /* Number of extra 0 bytes to append to data */ int appendBias /* True if this is likely an append */ ){ int rc; @@ -5324,7 +5134,7 @@ int sqlite3BtreeInsert( if( rc ) return rc; newCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) ); if( newCell==0 ) return SQLITE_NOMEM; - rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, &szNew); + rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); if( rc ) goto end_insert; assert( szNew==cellSizePtr(pPage, newCell) ); assert( szNew<=MX_CELL_SIZE(pBt) ); @@ -5426,7 +5236,7 @@ int sqlite3BtreeDelete(BtCursor *pCur){ int notUsed; unsigned char *tempCell = 0; assert( !pPage->leafData ); - getTempCursor(pCur, &leafCur); + sqlite3BtreeGetTempCursor(pCur, &leafCur); rc = sqlite3BtreeNext(&leafCur, ¬Used); if( rc==SQLITE_OK ){ rc = sqlite3PagerWrite(leafCur.pPage->pDbPage); @@ -5455,7 +5265,7 @@ int sqlite3BtreeDelete(BtCursor *pCur){ rc = balance(leafCur.pPage, 0); } sqliteFree(tempCell); - releaseTempCursor(&leafCur); + sqlite3BtreeReleaseTempCursor(&leafCur); }else{ TRACE(("DELETE: table=%d delete from leaf %d\n", pCur->pgnoRoot, pPage->pgno)); @@ -5490,15 +5300,6 @@ int sqlite3BtreeCreateTable(Btree *p, in } assert( !pBt->readOnly ); - /* It is illegal to create a table if any cursors are open on the - ** database. This is because in auto-vacuum mode the backend may - ** need to move a database page to make room for the new root-page. - ** If an open cursor was using the page a problem would occur. - */ - if( pBt->pCursor ){ - return SQLITE_LOCKED; - } - #ifdef SQLITE_OMIT_AUTOVACUUM rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); if( rc ) return rc; @@ -5507,6 +5308,13 @@ int sqlite3BtreeCreateTable(Btree *p, in Pgno pgnoMove; /* Move a page here to make room for the root-page */ MemPage *pPageMove; /* The page to move to. */ + /* Creating a new table may probably require moving an existing database + ** to make room for the new tables root page. In case this page turns + ** out to be an overflow page, delete all overflow page-map caches + ** held by open cursors. + */ + invalidateAllOverflowCache(pBt); + /* Read the value of meta[3] from the database to determine where the ** root page of the new table should go. meta[3] is the largest root-page ** created so far, so the new root-page is (meta[3]+1). @@ -5546,7 +5354,7 @@ int sqlite3BtreeCreateTable(Btree *p, in releasePage(pPageMove); /* Move the page currently at pgnoRoot to pgnoMove. */ - rc = getPage(pBt, pgnoRoot, &pRoot, 0); + rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); if( rc!=SQLITE_OK ){ return rc; } @@ -5569,7 +5377,7 @@ int sqlite3BtreeCreateTable(Btree *p, in if( rc!=SQLITE_OK ){ return rc; } - rc = getPage(pBt, pgnoRoot, &pRoot, 0); + rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); if( rc!=SQLITE_OK ){ return rc; } @@ -5718,7 +5526,7 @@ int sqlite3BtreeDropTable(Btree *p, int return SQLITE_LOCKED; } - rc = getPage(pBt, (Pgno)iTable, &pPage, 0); + rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0); if( rc ) return rc; rc = sqlite3BtreeClearTable(p, iTable); if( rc ){ @@ -5757,7 +5565,7 @@ int sqlite3BtreeDropTable(Btree *p, int */ MemPage *pMove; releasePage(pPage); - rc = getPage(pBt, maxRootPgno, &pMove, 0); + rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); if( rc!=SQLITE_OK ){ return rc; } @@ -5766,7 +5574,7 @@ int sqlite3BtreeDropTable(Btree *p, int if( rc!=SQLITE_OK ){ return rc; } - rc = getPage(pBt, maxRootPgno, &pMove, 0); + rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); if( rc!=SQLITE_OK ){ return rc; } @@ -5884,187 +5692,8 @@ int sqlite3BtreeFlags(BtCursor *pCur){ return pPage ? pPage->aData[pPage->hdrOffset] : 0; } -#ifdef SQLITE_DEBUG -/* -** Print a disassembly of the given page on standard output. This routine -** is used for debugging and testing only. -*/ -static int btreePageDump(BtShared *pBt, int pgno, int recursive, MemPage *pParent){ - int rc; - MemPage *pPage; - int i, j, c; - int nFree; - u16 idx; - int hdr; - int nCell; - int isInit; - unsigned char *data; - char range[20]; - unsigned char payload[20]; - rc = getPage(pBt, (Pgno)pgno, &pPage, 0); - isInit = pPage->isInit; - if( pPage->isInit==0 ){ - initPage(pPage, pParent); - } - if( rc ){ - return rc; - } - hdr = pPage->hdrOffset; - data = pPage->aData; - c = data[hdr]; - pPage->intKey = (c & (PTF_INTKEY|PTF_LEAFDATA))!=0; - pPage->zeroData = (c & PTF_ZERODATA)!=0; - pPage->leafData = (c & PTF_LEAFDATA)!=0; - pPage->leaf = (c & PTF_LEAF)!=0; - pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); - nCell = get2byte(&data[hdr+3]); - sqlite3DebugPrintf("PAGE %d: flags=0x%02x frag=%d parent=%d\n", pgno, - data[hdr], data[hdr+7], - (pPage->isInit && pPage->pParent) ? pPage->pParent->pgno : 0); - assert( hdr == (pgno==1 ? 100 : 0) ); - idx = hdr + 12 - pPage->leaf*4; - for(i=0; ileaf ){ - child = 0; - }else{ - child = get4byte(pCell); - } - sz = info.nData; - if( !pPage->intKey ) sz += info.nKey; - if( sz>sizeof(payload)-1 ) sz = sizeof(payload)-1; - memcpy(payload, &pCell[info.nHeader], sz); - for(j=0; j0x7f ) payload[j] = '.'; - } - payload[sz] = 0; - sqlite3DebugPrintf( - "cell %2d: i=%-10s chld=%-4d nk=%-4lld nd=%-4d payload=%s\n", - i, range, child, info.nKey, info.nData, payload - ); - } - if( !pPage->leaf ){ - sqlite3DebugPrintf("right_child: %d\n", get4byte(&data[hdr+8])); - } - nFree = 0; - i = 0; - idx = get2byte(&data[hdr+1]); - while( idx>0 && idxpBt->usableSize ){ - int sz = get2byte(&data[idx+2]); - sprintf(range,"%d..%d", idx, idx+sz-1); - nFree += sz; - sqlite3DebugPrintf("freeblock %2d: i=%-10s size=%-4d total=%d\n", - i, range, sz, nFree); - idx = get2byte(&data[idx]); - i++; - } - if( idx!=0 ){ - sqlite3DebugPrintf("ERROR: next freeblock index out of range: %d\n", idx); - } - if( recursive && !pPage->leaf ){ - for(i=0; iisInit = isInit; - sqlite3PagerUnref(pPage->pDbPage); - fflush(stdout); - return SQLITE_OK; -} -int sqlite3BtreePageDump(Btree *p, int pgno, int recursive){ - return btreePageDump(p->pBt, pgno, recursive, 0); -} -#endif - -#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) /* -** Fill aResult[] with information about the entry and page that the -** cursor is pointing to. -** -** aResult[0] = The page number -** aResult[1] = The entry number -** aResult[2] = Total number of entries on this page -** aResult[3] = Cell size (local payload + header) -** aResult[4] = Number of free bytes on this page -** aResult[5] = Number of free blocks on the page -** aResult[6] = Total payload size (local + overflow) -** aResult[7] = Header size in bytes -** aResult[8] = Local payload size -** aResult[9] = Parent page number -** aResult[10]= Page number of the first overflow page -** -** This routine is used for testing and debugging only. -*/ -int sqlite3BtreeCursorInfo(BtCursor *pCur, int *aResult, int upCnt){ - int cnt, idx; - MemPage *pPage = pCur->pPage; - BtCursor tmpCur; - - int rc = restoreOrClearCursorPosition(pCur); - if( rc!=SQLITE_OK ){ - return rc; - } - - assert( pPage->isInit ); - getTempCursor(pCur, &tmpCur); - while( upCnt-- ){ - moveToParent(&tmpCur); - } - pPage = tmpCur.pPage; - aResult[0] = sqlite3PagerPagenumber(pPage->pDbPage); - assert( aResult[0]==pPage->pgno ); - aResult[1] = tmpCur.idx; - aResult[2] = pPage->nCell; - if( tmpCur.idx>=0 && tmpCur.idxnCell ){ - getCellInfo(&tmpCur); - aResult[3] = tmpCur.info.nSize; - aResult[6] = tmpCur.info.nData; - aResult[7] = tmpCur.info.nHeader; - aResult[8] = tmpCur.info.nLocal; - }else{ - aResult[3] = 0; - aResult[6] = 0; - aResult[7] = 0; - aResult[8] = 0; - } - aResult[4] = pPage->nFree; - cnt = 0; - idx = get2byte(&pPage->aData[pPage->hdrOffset+1]); - while( idx>0 && idxpBt->usableSize ){ - cnt++; - idx = get2byte(&pPage->aData[idx]); - } - aResult[5] = cnt; - if( pPage->pParent==0 || isRootPage(pPage) ){ - aResult[9] = 0; - }else{ - aResult[9] = pPage->pParent->pgno; - } - if( tmpCur.info.iOverflow ){ - aResult[10] = get4byte(&tmpCur.info.pCell[tmpCur.info.iOverflow]); - }else{ - aResult[10] = 0; - } - releaseTempCursor(&tmpCur); - return SQLITE_OK; -} -#endif - -/* ** Return the pager associated with a BTree. This routine is used for ** testing and debugging only. */ @@ -6072,21 +5701,6 @@ Pager *sqlite3BtreePager(Btree *p){ return p->pBt->pPager; } -/* -** This structure is passed around through all the sanity checking routines -** in order to keep track of some global state information. -*/ -typedef struct IntegrityCk IntegrityCk; -struct IntegrityCk { - BtShared *pBt; /* The tree being checked out */ - Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */ - int nPage; /* Number of pages in the database */ - int *anRef; /* Number of times each page is referenced */ - int mxErr; /* Stop accumulating errors when this reaches zero */ - char *zErrMsg; /* An error message. NULL if no errors seen. */ - int nErr; /* Number of messages written to zErrMsg so far */ -}; - #ifndef SQLITE_OMIT_INTEGRITY_CHECK /* ** Append a message to the error message string. @@ -6277,7 +5891,7 @@ static int checkTreePage( char zContext[100]; char *hit; - sprintf(zContext, "Page %d: ", iPage); + sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage); /* Check that the page exists */ @@ -6285,13 +5899,14 @@ static int checkTreePage( usableSize = pBt->usableSize; if( iPage==0 ) return 0; if( checkRef(pCheck, iPage, zParentContext) ) return 0; - if( (rc = getPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ + if( (rc = sqlite3BtreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ checkAppendMsg(pCheck, zContext, "unable to get the page. error code=%d", rc); return 0; } - if( (rc = initPage(pPage, pParent))!=0 ){ - checkAppendMsg(pCheck, zContext, "initPage() returns error code %d", rc); + if( (rc = sqlite3BtreeInitPage(pPage, pParent))!=0 ){ + checkAppendMsg(pCheck, zContext, + "sqlite3BtreeInitPage() returns error code %d", rc); releasePage(pPage); return 0; } @@ -6306,9 +5921,10 @@ static int checkTreePage( /* Check payload overflow pages */ - sprintf(zContext, "On tree page %d cell %d: ", iPage, i); + sqlite3_snprintf(sizeof(zContext), zContext, + "On tree page %d cell %d: ", iPage, i); pCell = findCell(pPage,i); - parseCellPtr(pPage, pCell, &info); + sqlite3BtreeParseCellPtr(pPage, pCell, &info); sz = info.nData; if( !pPage->intKey ) sz += info.nKey; assert( sz==info.nPayload ); @@ -6341,7 +5957,8 @@ static int checkTreePage( } if( !pPage->leaf ){ pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); - sprintf(zContext, "On page %d at right child: ", iPage); + sqlite3_snprintf(sizeof(zContext), zContext, + "On page %d at right child: ", iPage); #ifndef SQLITE_OMIT_AUTOVACUUM if( pBt->autoVacuum ){ checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0); @@ -6437,6 +6054,11 @@ char *sqlite3BtreeIntegrityCheck( sCheck.mxErr = mxErr; sCheck.nErr = 0; *pnErr = 0; +#ifndef SQLITE_OMIT_AUTOVACUUM + if( pBt->nTrunc!=0 ){ + sCheck.nPage = pBt->nTrunc; + } +#endif if( sCheck.nPage==0 ){ unlockBtreeIfUnused(pBt); return 0; @@ -6672,32 +6294,54 @@ int sqlite3BtreeLockTable(Btree *p, int } #endif +#ifndef SQLITE_OMIT_INCRBLOB /* -** The following debugging interface has to be in this file (rather -** than in, for example, test1.c) so that it can get access to -** the definition of BtShared. +** Argument pCsr must be a cursor opened for writing on an +** INTKEY table currently pointing at a valid table entry. +** This function modifies the data stored as part of that entry. +** Only the data content may only be modified, it is not possible +** to change the length of the data stored. */ -#if defined(SQLITE_DEBUG) && defined(TCLSH) -#include -int sqlite3_shared_cache_report( - void * clientData, - Tcl_Interp *interp, - int objc, - Tcl_Obj *CONST objv[] -){ -#ifndef SQLITE_OMIT_SHARED_CACHE - const ThreadData *pTd = sqlite3ThreadDataReadOnly(); - if( pTd->useSharedData ){ - BtShared *pBt; - Tcl_Obj *pRet = Tcl_NewObj(); - for(pBt=pTd->pBtree; pBt; pBt=pBt->pNext){ - const char *zFile = sqlite3PagerFilename(pBt->pPager); - Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(zFile, -1)); - Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(pBt->nRef)); - } - Tcl_SetObjResult(interp, pRet); +int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ + + assert(pCsr->isIncrblobHandle); + if( pCsr->eState==CURSOR_REQUIRESEEK ){ + return SQLITE_ABORT; } -#endif - return TCL_OK; + + /* Check some preconditions: + ** (a) the cursor is open for writing, + ** (b) there is no read-lock on the table being modified and + ** (c) the cursor points at a valid row of an intKey table. + */ + if( !pCsr->wrFlag ){ + return SQLITE_READONLY; + } + assert( !pCsr->pBtree->pBt->readOnly + && pCsr->pBtree->pBt->inTransaction==TRANS_WRITE ); + if( checkReadLocks(pCsr->pBtree, pCsr->pgnoRoot, pCsr) ){ + return SQLITE_LOCKED; /* The table pCur points to has a read lock */ + } + if( pCsr->eState==CURSOR_INVALID || !pCsr->pPage->intKey ){ + return SQLITE_ERROR; + } + + return accessPayload(pCsr, offset, amt, (unsigned char *)z, 0, 1); } + +/* +** Set a flag on this cursor to cache the locations of pages from the +** overflow list for the current row. This is used by cursors opened +** for incremental blob IO only. +** +** This function sets a flag only. The actual page location cache +** (stored in BtCursor.aOverflow[]) is allocated and used by function +** accessPayload() (the worker function for sqlite3BtreeData() and +** sqlite3BtreePutData()). +*/ +void sqlite3BtreeCacheOverflow(BtCursor *pCur){ + assert(!pCur->isIncrblobHandle); + assert(!pCur->aOverflow); + pCur->isIncrblobHandle = 1; +} #endif ============================================================ --- sqlite/btree.h 9b2cc0d113c0bc2d37d244b9a394d56948c9acbf +++ sqlite/btree.h 1d527bf61ed176f980c34999d5793a0fd45dcf8c @@ -13,7 +13,7 @@ ** subsystem. See comments in the source code for a detailed description ** of what each interface routine does. ** -** @(#) $Id: btree.h,v 1.74 2007/03/30 14:06:34 drh Exp $ +** @(#) $Id: btree.h,v 1.82 2007/05/08 21:45:27 drh Exp $ */ #ifndef _BTREE_H_ #define _BTREE_H_ @@ -31,6 +31,10 @@ #define SQLITE_DEFAULT_AUTOVACUUM 0 #endif +#define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */ +#define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */ +#define BTREE_AUTOVACUUM_INCR 2 /* Incremental vacuum */ + /* ** Forward declarations of structure */ @@ -63,6 +67,7 @@ int sqlite3BtreeGetPageSize(Btree*); int sqlite3BtreeSyncDisabled(Btree*); int sqlite3BtreeSetPageSize(Btree*,int,int); int sqlite3BtreeGetPageSize(Btree*); +int sqlite3BtreeMaxPageCount(Btree*,int); int sqlite3BtreeGetReserve(Btree*); int sqlite3BtreeSetAutoVacuum(Btree *, int); int sqlite3BtreeGetAutoVacuum(Btree *); @@ -87,6 +92,8 @@ int sqlite3BtreeCopyFile(Btree *, Btree const char *sqlite3BtreeGetJournalname(Btree *); int sqlite3BtreeCopyFile(Btree *, Btree *); +int sqlite3BtreeIncrVacuum(Btree *); + /* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR ** of the following flags: */ @@ -108,17 +115,12 @@ int sqlite3BtreeCursor( BtCursor **ppCursor /* Returned cursor */ ); -void sqlite3BtreeSetCompare( - BtCursor *, - int(*)(void*,int,const void*,int,const void*), - void* -); - int sqlite3BtreeCloseCursor(BtCursor*); int sqlite3BtreeMoveto(BtCursor*,const void *pKey,i64 nKey,int bias,int *pRes); int sqlite3BtreeDelete(BtCursor*); int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey, - const void *pData, int nData, int bias); + const void *pData, int nData, + int nZero, int bias); int sqlite3BtreeFirst(BtCursor*, int *pRes); int sqlite3BtreeLast(BtCursor*, int *pRes); int sqlite3BtreeNext(BtCursor*, int *pRes); @@ -135,16 +137,13 @@ struct Pager *sqlite3BtreePager(Btree*); char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*); struct Pager *sqlite3BtreePager(Btree*); +int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*); +void sqlite3BtreeCacheOverflow(BtCursor *); #ifdef SQLITE_TEST int sqlite3BtreeCursorInfo(BtCursor*, int*, int); void sqlite3BtreeCursorList(Btree*); -#endif - -#ifdef SQLITE_DEBUG int sqlite3BtreePageDump(Btree*, int, int recursive); -#else -#define sqlite3BtreePageDump(X,Y,Z) SQLITE_OK #endif #endif /* _BTREE_H_ */ ============================================================ --- sqlite/build.c 1880da163d9aa404016242b8b76d69907f682cd8 +++ sqlite/build.c 50992d92e131a9aa9aa6657fb1ddc13e176fd70c @@ -22,7 +22,7 @@ ** COMMIT ** ROLLBACK ** -** $Id: build.c,v 1.421 2007/04/18 14:47:24 danielk1977 Exp $ +** $Id: build.c,v 1.432 2007/05/15 14:34:32 drh Exp $ */ #include "sqliteInt.h" #include @@ -194,8 +194,10 @@ void sqlite3FinishCoding(Parse *pParse){ /* Get the VDBE program ready for execution */ if( v && pParse->nErr==0 && !sqlite3MallocFailed() ){ +#ifdef SQLITE_DEBUG FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0; sqlite3VdbeTrace(v, trace); +#endif sqlite3VdbeMakeReady(v, pParse->nVar, pParse->nMem+3, pParse->nTab+3, pParse->explain); pParse->rc = SQLITE_DONE; @@ -910,6 +912,10 @@ void sqlite3AddColumn(Parse *pParse, Tok char *z; Column *pCol; if( (p = pParse->pNewTable)==0 ) return; + if( p->nCol+1>SQLITE_MAX_COLUMN ){ + sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName); + return; + } z = sqlite3NameFromToken(pName); if( z==0 ) return; for(i=0; inCol; i++){ @@ -1322,22 +1328,24 @@ static char *createTableStmt(Table *p, i n += 35 + 6*p->nCol; zStmt = sqliteMallocRaw( n ); if( zStmt==0 ) return 0; - strcpy(zStmt, !OMIT_TEMPDB&&isTemp ? "CREATE TEMP TABLE ":"CREATE TABLE "); + sqlite3_snprintf(n, zStmt, + !OMIT_TEMPDB&&isTemp ? "CREATE TEMP TABLE ":"CREATE TABLE "); k = strlen(zStmt); identPut(zStmt, &k, p->zName); zStmt[k++] = '('; for(pCol=p->aCol, i=0; inCol; i++, pCol++){ - strcpy(&zStmt[k], zSep); + sqlite3_snprintf(n-k, &zStmt[k], zSep); k += strlen(&zStmt[k]); zSep = zSep2; identPut(zStmt, &k, pCol->zName); if( (z = pCol->zType)!=0 ){ zStmt[k++] = ' '; - strcpy(&zStmt[k], z); + assert( strlen(z)+k+1<=n ); + sqlite3_snprintf(n-k, &zStmt[k], "%s", z); k += strlen(z); } } - strcpy(&zStmt[k], zEnd); + sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd); return zStmt; } @@ -1560,7 +1568,7 @@ void sqlite3EndTable( pCons = pEnd; } nName = (const char *)pCons->z - zName; - p->addColOffset = 13 + sqlite3utf8CharLen(zName, nName); + p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName); } #endif } @@ -2355,7 +2363,7 @@ void sqlite3CreateIndex( int n; Index *pLoop; for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} - sprintf(zBuf,"_%d",n); + sqlite3_snprintf(sizeof(zBuf),zBuf,"_%d",n); zName = 0; sqlite3SetString(&zName, "sqlite_autoindex_", pTab->zName, zBuf, (char*)0); if( zName==0 ) goto exit_create_index; @@ -2420,7 +2428,7 @@ void sqlite3CreateIndex( pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]); pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]); zExtra = (char *)(&pIndex->zName[nName+1]); - strcpy(pIndex->zName, zName); + memcpy(pIndex->zName, zName, nName+1); pIndex->pTable = pTab; pIndex->nColumn = pList->nExpr; pIndex->onError = onError; @@ -2463,7 +2471,7 @@ void sqlite3CreateIndex( if( pListItem->pExpr ){ assert( pListItem->pExpr->pColl ); zColl = zExtra; - strcpy(zExtra, pListItem->pExpr->pColl->zName); + sqlite3_snprintf(nExtra, zExtra, "%s", pListItem->pExpr->pColl->zName); zExtra += (strlen(zColl) + 1); }else{ zColl = pTab->aCol[j].zColl; @@ -3091,7 +3099,8 @@ int sqlite3OpenTempDatabase(Parse *pPars int sqlite3OpenTempDatabase(Parse *pParse){ sqlite3 *db = pParse->db; if( db->aDb[1].pBt==0 && !pParse->explain ){ - int rc = sqlite3BtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt); + int rc = sqlite3BtreeFactory(db, 0, 0, SQLITE_DEFAULT_CACHE_SIZE, + &db->aDb[1].pBt); if( rc!=SQLITE_OK ){ sqlite3ErrorMsg(pParse, "unable to open a temporary database " "file for storing temporary tables"); @@ -3148,7 +3157,7 @@ void sqlite3CodeVerifySchema(Parse *pPar if( iDb>=0 ){ assert( iDbnDb ); assert( db->aDb[iDb].pBt!=0 || iDb==1 ); - assert( iDbcookieMask & mask)==0 ){ pParse->cookieMask |= mask; @@ -3297,6 +3306,7 @@ void sqlite3Reindex(Parse *pParse, Token iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName); if( iDb<0 ) return; z = sqlite3NameFromToken(pObjName); + if( z==0 ) return; zDb = db->aDb[iDb].zName; pTab = sqlite3FindTable(db, z, zDb); if( pTab ){ ============================================================ --- sqlite/callback.c 6414ed32d55859d0f65067aa5b88d2da27b3af9e +++ sqlite/callback.c 9c12535669a638f90a67e10440b99c7b93c0fbf4 @@ -13,7 +13,7 @@ ** This file contains functions used to access the internal hash tables ** of user defined functions and collation sequences. ** -** $Id: callback.c,v 1.17 2007/04/16 15:06:25 danielk1977 Exp $ +** $Id: callback.c,v 1.18 2007/05/07 09:32:45 danielk1977 Exp $ */ #include "sqliteInt.h" @@ -63,6 +63,7 @@ static int synthCollSeq(sqlite3 *db, Col pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, n, 0); if( pColl2->xCmp!=0 ){ memcpy(pColl, pColl2, sizeof(CollSeq)); + pColl->xDel = 0; /* Do not copy the destructor */ return SQLITE_OK; } } ============================================================ --- sqlite/date.c 74b76691bddf58b634f6bf4a77c8c58234268c6e +++ sqlite/date.c 6049db7d5a8fdf2c677ff7d58fa31d4f6593c988 @@ -16,10 +16,8 @@ ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file. ** All other code has file scope. ** -** $Id: date.c,v 1.62 2007/04/06 02:32:34 drh Exp $ +** $Id: date.c,v 1.66 2007/05/08 21:56:00 drh Exp $ ** -** NOTES: -** ** SQLite processes all times and dates as Julian Day numbers. The ** dates and times are stored as the number of days since noon ** in Greenwich on November 24, 4714 B.C. according to the Gregorian @@ -655,12 +653,15 @@ static int isDate(int argc, sqlite3_valu */ static int isDate(int argc, sqlite3_value **argv, DateTime *p){ int i; + const unsigned char *z; if( argc==0 ) return 1; - if( SQLITE_NULL==sqlite3_value_type(argv[0]) || - parseDateOrTime((char*)sqlite3_value_text(argv[0]), p) ) return 1; + if( (z = sqlite3_value_text(argv[0]))==0 || parseDateOrTime((char*)z, p) ){ + return 1; + } for(i=1; iSQLITE_MAX_LENGTH ){ + sqlite3_result_error_toobig(context); + return; }else{ z = sqliteMalloc( n ); if( z==0 ) return; @@ -823,7 +828,7 @@ static void strftimeFunc( }else{ i++; switch( zFmt[i] ){ - case 'd': sprintf(&z[j],"%02d",x.D); j+=2; break; + case 'd': sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break; case 'f': { double s = x.s; if( s>59.999 ) s = 59.999; @@ -831,7 +836,7 @@ static void strftimeFunc( j += strlen(&z[j]); break; } - case 'H': sprintf(&z[j],"%02d",x.h); j+=2; break; + case 'H': sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break; case 'W': /* Fall thru */ case 'j': { int nDay; /* Number of days since 1st day of year */ @@ -844,25 +849,30 @@ static void strftimeFunc( if( zFmt[i]=='W' ){ int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */ wd = ((int)(x.rJD+0.5)) % 7; - sprintf(&z[j],"%02d",(nDay+7-wd)/7); + sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7); j += 2; }else{ - sprintf(&z[j],"%03d",nDay+1); + sqlite3_snprintf(4, &z[j],"%03d",nDay+1); j += 3; } break; } - case 'J': sprintf(&z[j],"%.16g",x.rJD); j+=strlen(&z[j]); break; - case 'm': sprintf(&z[j],"%02d",x.M); j+=2; break; - case 'M': sprintf(&z[j],"%02d",x.m); j+=2; break; + case 'J': { + sqlite3_snprintf(20, &z[j],"%.16g",x.rJD); + j+=strlen(&z[j]); + break; + } + case 'm': sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break; + case 'M': sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break; case 's': { - sprintf(&z[j],"%d",(int)((x.rJD-2440587.5)*86400.0 + 0.5)); + sqlite3_snprintf(30,&z[j],"%d", + (int)((x.rJD-2440587.5)*86400.0 + 0.5)); j += strlen(&z[j]); break; } - case 'S': sprintf(&z[j],"%02d",(int)x.s); j+=2; break; + case 'S': sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break; case 'w': z[j++] = (((int)(x.rJD+1.5)) % 7) + '0'; break; - case 'Y': sprintf(&z[j],"%04d",x.Y); j+=strlen(&z[j]); break; + case 'Y': sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=strlen(&z[j]);break; case '%': z[j++] = '%'; break; } } ============================================================ --- sqlite/expr.c 2f0f9f89efe9170e5e6ca5d5e93a9d5896fff5ac +++ sqlite/expr.c b68f1b208be50413b407da92bd7b0fce840999cf @@ -12,7 +12,7 @@ ** This file contains routines used for analyzing expressions and ** for generating VDBE code that evaluates expressions in SQLite. ** -** $Id: expr.c,v 1.285 2007/04/18 17:07:58 drh Exp $ +** $Id: expr.c,v 1.298 2007/06/15 16:37:29 danielk1977 Exp $ */ #include "sqliteInt.h" #include @@ -35,9 +35,6 @@ char sqlite3ExprAffinity(Expr *pExpr){ */ char sqlite3ExprAffinity(Expr *pExpr){ int op = pExpr->op; - if( op==TK_AS ){ - return sqlite3ExprAffinity(pExpr->pLeft); - } if( op==TK_SELECT ){ return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr); } @@ -75,7 +72,7 @@ CollSeq *sqlite3ExprCollSeq(Parse *pPars CollSeq *pColl = 0; if( pExpr ){ pColl = pExpr->pColl; - if( (pExpr->op==TK_AS || pExpr->op==TK_CAST) && !pColl ){ + if( pExpr->op==TK_CAST && !pColl ){ return sqlite3ExprCollSeq(pParse, pExpr->pLeft); } } @@ -174,15 +171,21 @@ static int binaryCompareP1(Expr *pExpr1, ** used. Otherwise the collation sequence for the right hand expression ** is used, or the default (BINARY) if neither expression has a collating ** type. +** +** Argument pRight (but not pLeft) may be a null pointer. In this case, +** it is not considered. */ -static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){ +CollSeq* sqlite3BinaryCompareCollSeq( + Parse *pParse, + Expr *pLeft, + Expr *pRight +){ CollSeq *pColl; assert( pLeft ); - assert( pRight ); if( pLeft->flags & EP_ExpCollate ){ assert( pLeft->pColl ); pColl = pLeft->pColl; - }else if( pRight->flags & EP_ExpCollate ){ + }else if( pRight && pRight->flags & EP_ExpCollate ){ assert( pRight->pColl ); pColl = pRight->pColl; }else{ @@ -206,7 +209,7 @@ static int codeCompare( int jumpIfNull /* If true, jump if either operand is NULL */ ){ int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull); - CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight); + CollSeq *p3 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight); return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ); } @@ -247,6 +250,8 @@ Expr *sqlite3Expr(int op, Expr *pLeft, E pNew->pColl = pLeft->pColl; } } + + sqlite3ExprSetHeight(pNew); return pNew; } @@ -280,7 +285,7 @@ Expr *sqlite3RegisterExpr(Parse *pParse, int depth; if( pParse->nested==0 ){ sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken); - return 0; + return sqlite3Expr(TK_NULL, 0, 0, 0); } if( v==0 ) return 0; p = sqlite3Expr(TK_REGISTER, 0, 0, pToken); @@ -343,6 +348,8 @@ Expr *sqlite3ExprFunction(ExprList *pLis assert( pToken->dyn==0 ); pNew->token = *pToken; pNew->span = pNew->token; + + sqlite3ExprSetHeight(pNew); return pNew; } @@ -413,6 +420,9 @@ void sqlite3ExprAssignVarNumber(Parse *p } } } + if( !pParse->nErr && pParse->nVar>SQLITE_MAX_VARIABLE_NUMBER ){ + sqlite3ErrorMsg(pParse, "too many SQL variables"); + } } /* @@ -474,7 +484,6 @@ Expr *sqlite3ExprDup(Expr *p){ pNew->pRight = sqlite3ExprDup(p->pRight); pNew->pList = sqlite3ExprListDup(p->pList); pNew->pSelect = sqlite3SelectDup(p->pSelect); - pNew->pTab = p->pTab; return pNew; } void sqlite3TokenCopy(Token *pTo, Token *pFrom){ @@ -654,6 +663,87 @@ no_mem: } /* +** If the expression list pEList contains more than iLimit elements, +** leave an error message in pParse. +*/ +void sqlite3ExprListCheckLength( + Parse *pParse, + ExprList *pEList, + int iLimit, + const char *zObject +){ + if( pEList && pEList->nExpr>iLimit ){ + sqlite3ErrorMsg(pParse, "too many columns in %s", zObject); + } +} + + +#if SQLITE_MAX_EXPR_DEPTH>0 +/* The following three functions, heightOfExpr(), heightOfExprList() +** and heightOfSelect(), are used to determine the maximum height +** of any expression tree referenced by the structure passed as the +** first argument. +** +** If this maximum height is greater than the current value pointed +** to by pnHeight, the second parameter, then set *pnHeight to that +** value. +*/ +static void heightOfExpr(Expr *p, int *pnHeight){ + if( p ){ + if( p->nHeight>*pnHeight ){ + *pnHeight = p->nHeight; + } + } +} +static void heightOfExprList(ExprList *p, int *pnHeight){ + if( p ){ + int i; + for(i=0; inExpr; i++){ + heightOfExpr(p->a[i].pExpr, pnHeight); + } + } +} +static void heightOfSelect(Select *p, int *pnHeight){ + if( p ){ + heightOfExpr(p->pWhere, pnHeight); + heightOfExpr(p->pHaving, pnHeight); + heightOfExpr(p->pLimit, pnHeight); + heightOfExpr(p->pOffset, pnHeight); + heightOfExprList(p->pEList, pnHeight); + heightOfExprList(p->pGroupBy, pnHeight); + heightOfExprList(p->pOrderBy, pnHeight); + heightOfSelect(p->pPrior, pnHeight); + } +} + +/* +** Set the Expr.nHeight variable in the structure passed as an +** argument. An expression with no children, Expr.pList or +** Expr.pSelect member has a height of 1. Any other expression +** has a height equal to the maximum height of any other +** referenced Expr plus one. +*/ +void sqlite3ExprSetHeight(Expr *p){ + int nHeight = 0; + heightOfExpr(p->pLeft, &nHeight); + heightOfExpr(p->pRight, &nHeight); + heightOfExprList(p->pList, &nHeight); + heightOfSelect(p->pSelect, &nHeight); + p->nHeight = nHeight + 1; +} + +/* +** Return the maximum height of any expression tree referenced +** by the select statement passed as an argument. +*/ +int sqlite3SelectExprHeight(Select *p){ + int nHeight = 0; + heightOfSelect(p, &nHeight); + return nHeight; +} +#endif + +/* ** Delete an entire expression list. */ void sqlite3ExprListDelete(ExprList *pList){ @@ -720,6 +810,9 @@ static int walkSelectExpr(Select *p, int walkExprList(p->pGroupBy, xFunc, pArg); walkExprTree(p->pHaving, xFunc, pArg); walkExprList(p->pOrderBy, xFunc, pArg); + if( p->pPrior ){ + walkSelectExpr(p->pPrior, xFunc, pArg); + } return 0; } @@ -738,11 +831,21 @@ static int exprNodeIsConstant(void *pArg ** is constant. See sqlite3ExprIsConstant() for additional information. */ static int exprNodeIsConstant(void *pArg, Expr *pExpr){ + int *pN = (int*)pArg; + + /* If *pArg is 3 then any term of the expression that comes from + ** the ON or USING clauses of a join disqualifies the expression + ** from being considered constant. */ + if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){ + *pN = 0; + return 2; + } + switch( pExpr->op ){ /* Consider functions to be constant if all their arguments are constant ** and *pArg==2 */ case TK_FUNCTION: - if( *((int*)pArg)==2 ) return 0; + if( (*pN)==2 ) return 0; /* Fall through */ case TK_ID: case TK_COLUMN: @@ -753,11 +856,11 @@ static int exprNodeIsConstant(void *pArg case TK_SELECT: case TK_EXISTS: #endif - *((int*)pArg) = 0; + *pN = 0; return 2; case TK_IN: if( pExpr->pSelect ){ - *((int*)pArg) = 0; + *pN = 0; return 2; } default: @@ -781,6 +884,18 @@ int sqlite3ExprIsConstant(Expr *p){ /* ** Walk an expression tree. Return 1 if the expression is constant +** that does no originate from the ON or USING clauses of a join. +** Return 0 if it involves variables or function calls or terms from +** an ON or USING clause. +*/ +int sqlite3ExprIsConstantNotJoin(Expr *p){ + int isConst = 3; + walkExprTree(p, exprNodeIsConstant, &isConst); + return isConst!=0; +} + +/* +** Walk an expression tree. Return 1 if the expression is constant ** or a function call with constant arguments. Return and 0 if there ** are any variables. ** @@ -1023,10 +1138,19 @@ static int lookupName( for(j=0; jnExpr; j++){ char *zAs = pEList->a[j].zName; if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ + Expr *pDup; assert( pExpr->pLeft==0 && pExpr->pRight==0 ); - pExpr->op = TK_AS; - pExpr->iColumn = j; - pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr); + assert( pExpr->pList==0 ); + assert( pExpr->pSelect==0 ); + pDup = sqlite3ExprDup(pEList->a[j].pExpr); + if( pExpr->flags & EP_ExpCollate ){ + pDup->pColl = pExpr->pColl; + pDup->flags |= EP_ExpCollate; + } + if( pExpr->span.dyn ) sqliteFree((char*)pExpr->span.z); + if( pExpr->token.dyn ) sqliteFree((char*)pExpr->token.z); + memcpy(pExpr, pDup, sizeof(*pExpr)); + sqliteFree(pDup); cnt = 1; assert( zTab==0 && zDb==0 ); goto lookupname_end_2; @@ -1317,15 +1441,28 @@ static int nameResolverStep(void *pArg, ** If the expression contains aggregate functions then set the EP_Agg ** property on the expression. */ -int sqlite3ExprResolveNames( +int sqlite3ExprResolveNames( NameContext *pNC, /* Namespace to resolve expressions in. */ Expr *pExpr /* The expression to be analyzed. */ ){ int savedHasAgg; if( pExpr==0 ) return 0; +#if SQLITE_MAX_EXPR_DEPTH>0 + if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){ + sqlite3ErrorMsg(pNC->pParse, + "Expression tree is too large (maximum depth %d)", + SQLITE_MAX_EXPR_DEPTH + ); + return 1; + } + pNC->pParse->nHeight += pExpr->nHeight; +#endif savedHasAgg = pNC->hasAgg; pNC->hasAgg = 0; walkExprTree(pExpr, nameResolverStep, pNC); +#if SQLITE_MAX_EXPR_DEPTH>0 + pNC->pParse->nHeight -= pExpr->nHeight; +#endif if( pNC->nErr>0 ){ ExprSetProperty(pExpr, EP_Error); } @@ -1366,6 +1503,7 @@ void sqlite3CodeSubselect(Parse *pParse, Vdbe *v = sqlite3GetVdbe(pParse); if( v==0 ) return; + /* This code must be run in its entirety every time it is encountered ** if any of the following is true: ** @@ -1425,7 +1563,7 @@ void sqlite3CodeSubselect(Parse *pParse, } pEList = pExpr->pSelect->pEList; if( pEList && pEList->nExpr>0 ){ - keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft, + keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pEList->a[0].pExpr); } }else if( pExpr->pList ){ @@ -1503,6 +1641,7 @@ void sqlite3CodeSubselect(Parse *pParse, if( testAddr ){ sqlite3VdbeJumpHere(v, testAddr); } + return; } #endif /* SQLITE_OMIT_SUBQUERY */ @@ -1512,13 +1651,16 @@ static void codeInteger(Vdbe *v, const c ** text z[0..n-1] on the stack. */ static void codeInteger(Vdbe *v, const char *z, int n){ - int i; - if( sqlite3GetInt32(z, &i) ){ - sqlite3VdbeAddOp(v, OP_Integer, i, 0); - }else if( sqlite3FitsIn64Bits(z) ){ - sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n); - }else{ - sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n); + assert( z || sqlite3MallocFailed() ); + if( z ){ + int i; + if( sqlite3GetInt32(z, &i) ){ + sqlite3VdbeAddOp(v, OP_Integer, i, 0); + }else if( sqlite3FitsIn64Bits(z) ){ + sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n); + }else{ + sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n); + } } } @@ -1820,7 +1962,7 @@ void sqlite3ExprCode(Parse *pParse, Expr affinity = comparisonAffinity(pExpr); sqlite3VdbeAddOp(v, OP_Integer, 1, 0); - pParse->ckOffset = ckOffset+1; + pParse->ckOffset = (ckOffset ? (ckOffset+1) : 0); /* Code the from " IN (...)". The temporary table ** pExpr->iTable contains the values that make up the (...) set. @@ -1854,8 +1996,7 @@ void sqlite3ExprCode(Parse *pParse, Expr sqlite3VdbeAddOp(v, OP_And, 0, 0); break; } - case TK_UPLUS: - case TK_AS: { + case TK_UPLUS: { sqlite3ExprCode(pParse, pExpr->pLeft); stackChng = 0; break; ============================================================ --- sqlite/func.c 007d957c057bb42b0d37aa6ad4be0e1c67a8871b +++ sqlite/func.c 4668843e6f0d27653939e12fc32276fb8e38d21a @@ -16,7 +16,7 @@ ** sqliteRegisterBuildinFunctions() found at the bottom of the file. ** All other code has file scope. ** -** $Id: func.c,v 1.139 2007/04/10 13:51:18 drh Exp $ +** $Id: func.c,v 1.160 2007/06/07 19:08:33 drh Exp $ */ #include "sqliteInt.h" #include @@ -102,7 +102,12 @@ static void lengthFunc( } case SQLITE_TEXT: { const unsigned char *z = sqlite3_value_text(argv[0]); - for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; } + if( z==0 ) return; + len = 0; + while( *z ){ + len++; + SQLITE_SKIP_UTF8(z); + } sqlite3_result_int(context, len); break; } @@ -145,7 +150,14 @@ static void absFunc(sqlite3_context *con } /* -** Implementation of the substr() function +** Implementation of the substr() function. +** +** substr(x,p1,p2) returns p2 characters of x[] beginning with p1. +** p1 is 1-indexed. So substr(x,1,1) returns the first character +** of x. If x is text, then we actually count UTF-8 characters. +** If x is a blob, then we count bytes. +** +** If p1 is negative, then we begin abs(p1) from the end of x[]. */ static void substrFunc( sqlite3_context *context, @@ -154,15 +166,27 @@ static void substrFunc( ){ const unsigned char *z; const unsigned char *z2; - int i; - int p1, p2, len; + int len; + int p0type; + i64 p1, p2; assert( argc==3 ); - z = sqlite3_value_text(argv[0]); - if( z==0 ) return; + p0type = sqlite3_value_type(argv[0]); + if( p0type==SQLITE_BLOB ){ + len = sqlite3_value_bytes(argv[0]); + z = sqlite3_value_blob(argv[0]); + if( z==0 ) return; + assert( len==sqlite3_value_bytes(argv[0]) ); + }else{ + z = sqlite3_value_text(argv[0]); + if( z==0 ) return; + len = 0; + for(z2=z; *z2; len++){ + SQLITE_SKIP_UTF8(z2); + } + } p1 = sqlite3_value_int(argv[1]); p2 = sqlite3_value_int(argv[2]); - for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; } if( p1<0 ){ p1 += len; if( p1<0 ){ @@ -175,16 +199,19 @@ static void substrFunc( if( p1+p2>len ){ p2 = len-p1; } - for(i=0; iSQLITE_MAX_LENGTH ){ + sqlite3_result_error_toobig(context); + return; + } + p = sqliteMalloc(n); + if( p ){ + sqlite3Randomness(n, p); + sqlite3_result_blob(context, (char*)p, n, sqlite3FreeX); + } } /* @@ -349,14 +398,14 @@ static const struct compareInfo likeInfo static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; /* -** X is a pointer to the first byte of a UTF-8 character. Increment -** X so that it points to the next character. This only works right -** if X points to a well-formed UTF-8 string. +** Read a single UTF-8 character and return its value. */ -#define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){} -#define sqliteCharVal(X) sqlite3ReadUtf8(X) +u32 sqlite3ReadUtf8(const unsigned char *z){ + u32 c; + SQLITE_READ_UTF8(z, c); + return c; +} - /* ** Compare two UTF-8 strings for equality where the first string can ** potentially be a "glob" expression. Return true (1) if they @@ -406,20 +455,20 @@ static int patternCompare( while( (c=zPattern[1]) == matchAll || c == matchOne ){ if( c==matchOne ){ if( *zString==0 ) return 0; - sqliteNextChar(zString); + SQLITE_SKIP_UTF8(zString); } zPattern++; } if( c && esc && sqlite3ReadUtf8(&zPattern[1])==esc ){ u8 const *zTemp = &zPattern[1]; - sqliteNextChar(zTemp); + SQLITE_SKIP_UTF8(zTemp); c = *zTemp; } if( c==0 ) return 1; if( c==matchSet ){ assert( esc==0 ); /* This is GLOB, not LIKE */ while( *zString && patternCompare(&zPattern[1],zString,pInfo,esc)==0 ){ - sqliteNextChar(zString); + SQLITE_SKIP_UTF8(zString); } return *zString!=0; }else{ @@ -433,20 +482,20 @@ static int patternCompare( } if( c2==0 ) return 0; if( patternCompare(&zPattern[1],zString,pInfo,esc) ) return 1; - sqliteNextChar(zString); + SQLITE_SKIP_UTF8(zString); } return 0; } }else if( !prevEscape && c==matchOne ){ if( *zString==0 ) return 0; - sqliteNextChar(zString); + SQLITE_SKIP_UTF8(zString); zPattern++; }else if( c==matchSet ){ int prior_c = 0; assert( esc==0 ); /* This only occurs for GLOB, not LIKE */ seen = 0; invert = 0; - c = sqliteCharVal(zString); + c = sqlite3ReadUtf8(zString); if( c==0 ) return 0; c2 = *++zPattern; if( c2=='^' ){ invert = 1; c2 = *++zPattern; } @@ -454,10 +503,10 @@ static int patternCompare( if( c==']' ) seen = 1; c2 = *++zPattern; } - while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){ + while( (c2 = sqlite3ReadUtf8(zPattern))!=0 && c2!=']' ){ if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){ zPattern++; - c2 = sqliteCharVal(zPattern); + c2 = sqlite3ReadUtf8(zPattern); if( c>=prior_c && c<=c2 ) seen = 1; prior_c = 0; }else if( c==c2 ){ @@ -466,14 +515,14 @@ static int patternCompare( }else{ prior_c = c2; } - sqliteNextChar(zPattern); + SQLITE_SKIP_UTF8(zPattern); } if( c2==0 || (seen ^ invert)==0 ) return 0; - sqliteNextChar(zString); + SQLITE_SKIP_UTF8(zString); zPattern++; }else if( esc && !prevEscape && sqlite3ReadUtf8(zPattern)==esc){ prevEscape = 1; - sqliteNextChar(zPattern); + SQLITE_SKIP_UTF8(zPattern); }else{ if( noCase ){ if( sqlite3UpperToLower[c] != sqlite3UpperToLower[*zString] ) return 0; @@ -515,15 +564,28 @@ static void likeFunc( int argc, sqlite3_value **argv ){ - const unsigned char *zA = sqlite3_value_text(argv[0]); - const unsigned char *zB = sqlite3_value_text(argv[1]); + const unsigned char *zA, *zB; int escape = 0; + + zB = sqlite3_value_text(argv[0]); + zA = sqlite3_value_text(argv[1]); + + /* Limit the length of the LIKE or GLOB pattern to avoid problems + ** of deep recursion and N*N behavior in patternCompare(). + */ + if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){ + sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1); + return; + } + assert( zB==sqlite3_value_text(argv[0]) ); /* Encoding did not change */ + if( argc==3 ){ /* The escape character string must consist of a single UTF-8 character. ** Otherwise, return an error. */ const unsigned char *zEsc = sqlite3_value_text(argv[2]); - if( sqlite3utf8CharLen((char*)zEsc, -1)!=1 ){ + if( zEsc==0 ) return; + if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){ sqlite3_result_error(context, "ESCAPE expression must be a single character", -1); return; @@ -535,7 +597,8 @@ static void likeFunc( #ifdef SQLITE_TEST sqlite3_like_count++; #endif - sqlite3_result_int(context, patternCompare(zA, zB, pInfo, escape)); + + sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape)); } } @@ -599,9 +662,14 @@ static void quoteFunc(sqlite3_context *c } case SQLITE_BLOB: { char *zText = 0; + char const *zBlob = sqlite3_value_blob(argv[0]); int nBlob = sqlite3_value_bytes(argv[0]); - char const *zBlob = sqlite3_value_blob(argv[0]); + assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ + if( 2*nBlob+4>SQLITE_MAX_LENGTH ){ + sqlite3_result_error_toobig(context); + return; + } zText = (char *)sqliteMalloc((2*nBlob)+4); if( !zText ){ sqlite3_result_error(context, "out of memory", -1); @@ -621,11 +689,17 @@ static void quoteFunc(sqlite3_context *c break; } case SQLITE_TEXT: { - int i,j,n; + int i,j; + u64 n; const unsigned char *zArg = sqlite3_value_text(argv[0]); char *z; - for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } + if( zArg==0 ) return; + for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } + if( i+n+3>SQLITE_MAX_LENGTH ){ + sqlite3_result_error_toobig(context); + return; + } z = sqliteMalloc( i+n+3 ); if( z==0 ) return; z[0] = '\''; @@ -656,8 +730,13 @@ static void hexFunc( const unsigned char *pBlob; char *zHex, *z; assert( argc==1 ); + pBlob = sqlite3_value_blob(argv[0]); n = sqlite3_value_bytes(argv[0]); - pBlob = sqlite3_value_blob(argv[0]); + if( n*2+1>SQLITE_MAX_LENGTH ){ + sqlite3_result_error_toobig(context); + return; + } + assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ z = zHex = sqlite3_malloc(n*2 + 1); if( zHex==0 ) return; for(i=0; iSQLITE_MAX_LENGTH ){ + sqlite3_result_error_toobig(context); + }else{ + sqlite3_result_zeroblob(context, n); + } +} + +/* ** The replace() function. Three arguments are all strings: call ** them A, B, and C. The result is also a string which is derived ** from A by replacing every occurance of B with C. The match @@ -687,39 +784,50 @@ static void replaceFunc( int nStr; /* Size of zStr */ int nPattern; /* Size of zPattern */ int nRep; /* Size of zRep */ - int nOut; /* Maximum size of zOut */ + i64 nOut; /* Maximum size of zOut */ int loopLimit; /* Last zStr[] that might match zPattern[] */ int i, j; /* Loop counters */ assert( argc==3 ); - if( sqlite3_value_type(argv[0])==SQLITE_NULL || - sqlite3_value_type(argv[1])==SQLITE_NULL || - sqlite3_value_type(argv[2])==SQLITE_NULL ){ - return; - } zStr = sqlite3_value_text(argv[0]); + if( zStr==0 ) return; nStr = sqlite3_value_bytes(argv[0]); + assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */ zPattern = sqlite3_value_text(argv[1]); + if( zPattern==0 || zPattern[0]==0 ) return; nPattern = sqlite3_value_bytes(argv[1]); + assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */ zRep = sqlite3_value_text(argv[2]); + if( zRep==0 ) return; nRep = sqlite3_value_bytes(argv[2]); - if( nPattern>=nRep ){ - nOut = nStr; - }else{ - nOut = (nStr/nPattern + 1)*nRep; + assert( zRep==sqlite3_value_text(argv[2]) ); + nOut = nStr + 1; + assert( nOut=SQLITE_MAX_LENGTH ){ + sqlite3_result_error_toobig(context); + sqlite3_free(zOut); + return; + } + zOut = sqlite3_realloc(zOut, (int)nOut); + if( zOut==0 ){ + return; + } memcpy(&zOut[j], zRep, nRep); j += nRep; i += nPattern-1; } } + assert( j+nStr-i+1==nOut ); memcpy(&zOut[j], &zStr[i], nStr-i); j += nStr - i; assert( j<=nOut ); @@ -739,40 +847,74 @@ static void trimFunc( const unsigned char *zIn; /* Input string */ const unsigned char *zCharSet; /* Set of characters to trim */ int nIn; /* Number of bytes in input */ - int flags; - int i; - unsigned char cFirst, cNext; + int flags; /* 1: trimleft 2: trimright 3: trim */ + int i; /* Loop counter */ + unsigned char *aLen; /* Length of each character in zCharSet */ + const unsigned char **azChar; /* Individual characters in zCharSet */ + int nChar; /* Number of characters in zCharSet */ + if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ return; } zIn = sqlite3_value_text(argv[0]); + if( zIn==0 ) return; nIn = sqlite3_value_bytes(argv[0]); + assert( zIn==sqlite3_value_text(argv[0]) ); if( argc==1 ){ - static const unsigned char zSpace[] = " "; - zCharSet = zSpace; - }else if( sqlite3_value_type(argv[1])==SQLITE_NULL ){ + static const unsigned char lenOne[] = { 1 }; + static const unsigned char *azOne[] = { (u8*)" " }; + nChar = 1; + aLen = (u8*)lenOne; + azChar = azOne; + zCharSet = 0; + }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){ return; }else{ - zCharSet = sqlite3_value_text(argv[1]); + const unsigned char *z; + for(z=zCharSet, nChar=0; *z; nChar++){ + SQLITE_SKIP_UTF8(z); + } + if( nChar>0 ){ + azChar = sqlite3_malloc( nChar*(sizeof(char*)+1) ); + if( azChar==0 ){ + return; + } + aLen = (unsigned char*)&azChar[nChar]; + for(z=zCharSet, nChar=0; *z; nChar++){ + azChar[nChar] = z; + SQLITE_SKIP_UTF8(z); + aLen[nChar] = z - azChar[nChar]; + } + } } - cFirst = zCharSet[0]; - if( cFirst ){ + if( nChar>0 ){ flags = (int)sqlite3_user_data(context); if( flags & 1 ){ - for(; nIn>0; nIn--, zIn++){ - if( cFirst==zIn[0] ) continue; - for(i=1; zCharSet[i] && zCharSet[i]!=zIn[0]; i++){} - if( zCharSet[i]==0 ) break; + while( nIn>0 ){ + int len; + for(i=0; i=nChar ) break; + zIn += len; + nIn -= len; } } if( flags & 2 ){ - for(; nIn>0; nIn--){ - cNext = zIn[nIn-1]; - if( cFirst==cNext ) continue; - for(i=1; zCharSet[i] && zCharSet[i]!=cNext; i++){} - if( zCharSet[i]==0 ) break; + while( nIn>0 ){ + int len; + for(i=0; i=nChar ) break; + nIn -= len; } } + if( zCharSet ){ + sqlite3_free(azChar); + } } sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT); } @@ -834,14 +976,16 @@ static void loadExt(sqlite3_context *con */ static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){ const char *zFile = (const char *)sqlite3_value_text(argv[0]); - const char *zProc = 0; + const char *zProc; sqlite3 *db = sqlite3_user_data(context); char *zErrMsg = 0; if( argc==2 ){ zProc = (const char *)sqlite3_value_text(argv[1]); + }else{ + zProc = 0; } - if( sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){ + if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){ sqlite3_result_error(context, zErrMsg, -1); sqlite3_free(zErrMsg); } @@ -1170,9 +1314,6 @@ void sqlite3RegisterBuiltinFunctions(sql { "typeof", 1, 0, SQLITE_UTF8, 0, typeofFunc }, { "length", 1, 0, SQLITE_UTF8, 0, lengthFunc }, { "substr", 3, 0, SQLITE_UTF8, 0, substrFunc }, -#ifndef SQLITE_OMIT_UTF16 - { "substr", 3, 0, SQLITE_UTF16LE, 0, sqlite3utf16Substr }, -#endif { "abs", 1, 0, SQLITE_UTF8, 0, absFunc }, { "round", 1, 0, SQLITE_UTF8, 0, roundFunc }, { "round", 2, 0, SQLITE_UTF8, 0, roundFunc }, @@ -1198,6 +1339,7 @@ void sqlite3RegisterBuiltinFunctions(sql { "rtrim", 2, 2, SQLITE_UTF8, 0, trimFunc }, { "trim", 1, 3, SQLITE_UTF8, 0, trimFunc }, { "trim", 2, 3, SQLITE_UTF8, 0, trimFunc }, + { "zeroblob", 1, 0, SQLITE_UTF8, 0, zeroblobFunc }, #ifdef SQLITE_SOUNDEX { "soundex", 1, 0, SQLITE_UTF8, 0, soundexFunc}, #endif @@ -1320,7 +1462,7 @@ int sqlite3IsLikeFunction(sqlite3 *db, E */ int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ FuncDef *pDef; - if( pExpr->op!=TK_FUNCTION ){ + if( pExpr->op!=TK_FUNCTION || !pExpr->pList ){ return 0; } if( pExpr->pList->nExpr!=2 ){ ============================================================ --- sqlite/insert.c 413cc06990cb3c401e64e596776c1e43934f8841 +++ sqlite/insert.c e595ca26805dfb3a9ebaabc28e7947c479f3b14d @@ -12,7 +12,7 @@ ** This file contains C code routines that are called by the parser ** to handle INSERT statements in SQLite. ** -** $Id: insert.c,v 1.185 2007/04/18 14:24:33 danielk1977 Exp $ +** $Id: insert.c,v 1.186 2007/05/04 13:15:56 drh Exp $ */ #include "sqliteInt.h" @@ -1122,25 +1122,26 @@ void sqlite3GenerateConstraintChecks( case OE_Fail: { int j, n1, n2; char zErrMsg[200]; - strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column "); + sqlite3_snprintf(sizeof(zErrMsg), zErrMsg, + pIdx->nColumn>1 ? "columns " : "column "); n1 = strlen(zErrMsg); for(j=0; jnColumn && n1aCol[pIdx->aiColumn[j]].zName; n2 = strlen(zCol); if( j>0 ){ - strcpy(&zErrMsg[n1], ", "); + sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], ", "); n1 += 2; } if( n1+n2>sizeof(zErrMsg)-30 ){ - strcpy(&zErrMsg[n1], "..."); + sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], "..."); n1 += 3; break; }else{ - strcpy(&zErrMsg[n1], zCol); + sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], "%s", zCol); n1 += n2; } } - strcpy(&zErrMsg[n1], + sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], pIdx->nColumn>1 ? " are not unique" : " is not unique"); sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0); break; ============================================================ --- sqlite/keywordhash.h 65fe072d2fa7cf2cfece3c7550f9085ec2988717 +++ sqlite/keywordhash.h 1224129789df7a9d09fbef62e56cb5254915b78f @@ -1,8 +1,8 @@ /***** This file contains automatically generated code ****** ** ** The code in this file has been automatically generated by ** -** $Header: /sqlite/sqlite/tool/mkkeywordhash.c,v 1.27 2007/04/06 11:26:00 drh Exp $ +** $Header: /sqlite/sqlite/tool/mkkeywordhash.c,v 1.30 2007/05/04 18:30:41 drh Exp $ ** ** The code in this file implements a function that determines whether ** or not a given identifier is really an SQL keyword. The same thing @@ -13,86 +13,87 @@ static int keywordCode(const char *z, in */ /* Hash score: 165 */ static int keywordCode(const char *z, int n){ - static const char zText[536] = - "ABORTABLEFTEMPORARYADDATABASELECTHENDEFAULTRANSACTIONATURALTER" - "AISEACHECKEYAFTEREFERENCESCAPELSEXCEPTRIGGEREGEXPLAINITIALLYANALYZE" - "XCLUSIVEXISTSANDEFERRABLEATTACHAVINGLOBEFOREIGNOREINDEXAUTOINCREMENT" - "BEGINNERENAMEBETWEENOTNULLIKEBYCASCADEFERREDELETECASECASTCOLLATE" - "COLUMNCOMMITCONFLICTCONSTRAINTERSECTCREATECROSSCURRENT_DATECURRENT_TIMESTAMP" - "LANDESCDETACHDISTINCTDROPRAGMATCHFAILIMITFROMFULLGROUPDATEIFIMMEDIATE" - "INSERTINSTEADINTOFFSETISNULLJOINORDEREPLACEOUTERESTRICTPRIMARY" - "QUERYRIGHTROLLBACKROWHENUNIONUNIQUEUSINGVACUUMVALUESVIEWHEREVIRTUAL" - ; + /* zText[] encodes 775 bytes of keywords in 526 bytes */ + static const char zText[526] = + "BEFOREIGNOREGEXPLAINSTEADDESCAPEACHECKEYCONSTRAINTERSECTABLEFT" + "HENDATABASELECTRANSACTIONATURALTERAISELSEXCEPTRIGGEREFERENCES" + "UNIQUERYATTACHAVINGROUPDATEMPORARYBEGINNEREINDEXCLUSIVEXISTSBETWEEN" + "OTNULLIKECASCADEFERRABLECASECOLLATECREATECURRENT_DATEDELETEDETACH" + "IMMEDIATEJOINSERTMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMITWHEN" + "WHERENAMEAFTEREPLACEANDEFAULTAUTOINCREMENTCASTCOLUMNCOMMITCONFLICT" + "CROSSCURRENT_TIMESTAMPRIMARYDEFERREDISTINCTDROPFAILFROMFULLGLOB" + "YIFINTOFFSETISNULLORDERESTRICTOUTERIGHTROLLBACKROWUNIONUSINGVACUUM" + "VIEWINITIALLY"; static const unsigned char aHash[127] = { - 91, 79, 106, 90, 0, 4, 0, 0, 113, 0, 82, 0, 0, - 94, 43, 75, 92, 0, 105, 108, 96, 89, 0, 10, 0, 0, - 112, 0, 116, 102, 0, 28, 47, 0, 40, 0, 0, 64, 70, - 0, 62, 19, 0, 104, 35, 103, 0, 107, 73, 0, 0, 33, - 0, 60, 36, 0, 8, 0, 114, 37, 12, 0, 76, 39, 25, - 65, 0, 0, 31, 80, 52, 30, 49, 20, 87, 0, 34, 0, - 74, 26, 0, 71, 0, 0, 0, 63, 46, 66, 22, 86, 29, - 68, 85, 0, 1, 0, 9, 100, 57, 18, 0, 111, 81, 98, - 53, 6, 84, 0, 0, 48, 93, 0, 101, 0, 69, 0, 0, - 15, 0, 115, 50, 55, 0, 2, 54, 0, 110, + 63, 92, 109, 61, 0, 38, 0, 0, 69, 0, 64, 0, 0, + 102, 4, 65, 7, 0, 108, 72, 103, 99, 0, 22, 0, 0, + 113, 0, 111, 106, 0, 18, 80, 0, 1, 0, 0, 56, 57, + 0, 55, 11, 0, 33, 77, 89, 0, 110, 88, 0, 0, 45, + 0, 90, 54, 0, 20, 0, 114, 34, 19, 0, 10, 97, 28, + 83, 0, 0, 116, 93, 47, 115, 41, 12, 44, 0, 78, 0, + 87, 29, 0, 86, 0, 0, 0, 82, 79, 84, 75, 96, 6, + 14, 95, 0, 68, 0, 21, 76, 98, 27, 0, 112, 67, 104, + 49, 40, 71, 0, 0, 81, 100, 0, 107, 0, 15, 0, 0, + 24, 0, 73, 42, 50, 0, 16, 48, 0, 37, }; static const unsigned char aNext[116] = { - 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 17, 0, 0, 0, 0, - 0, 11, 0, 0, 0, 0, 5, 13, 7, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, - 0, 16, 0, 23, 51, 0, 0, 0, 0, 44, 0, 58, 0, - 0, 0, 0, 0, 0, 0, 0, 72, 41, 0, 24, 59, 21, - 0, 78, 0, 0, 67, 0, 0, 83, 45, 0, 0, 0, 0, - 0, 0, 0, 0, 38, 95, 97, 0, 0, 99, 0, 32, 0, - 14, 27, 77, 0, 56, 88, 0, 0, 0, 61, 0, 109, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, + 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, + 17, 0, 0, 0, 36, 39, 0, 0, 25, 0, 0, 31, 0, + 0, 0, 43, 52, 0, 0, 0, 53, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 51, 0, 0, 0, 0, 26, 0, 8, 46, + 2, 0, 0, 0, 0, 0, 0, 0, 3, 58, 66, 0, 13, + 0, 91, 85, 0, 94, 0, 74, 0, 0, 62, 0, 35, 101, + 0, 0, 105, 23, 30, 60, 70, 0, 0, 59, 0, 0, }; static const unsigned char aLen[116] = { - 5, 5, 4, 4, 9, 2, 3, 8, 2, 6, 4, 3, 7, - 11, 2, 7, 5, 5, 4, 5, 3, 5, 10, 6, 4, 6, - 7, 6, 7, 9, 3, 7, 9, 6, 3, 10, 6, 6, 4, - 6, 3, 7, 6, 7, 5, 13, 2, 2, 5, 5, 6, 7, - 3, 7, 4, 4, 2, 7, 3, 8, 6, 4, 4, 7, 6, - 6, 8, 10, 9, 6, 5, 12, 12, 17, 4, 4, 6, 8, - 2, 4, 6, 5, 4, 5, 4, 4, 5, 6, 2, 9, 6, - 7, 4, 2, 6, 3, 6, 4, 5, 7, 5, 8, 7, 5, - 5, 8, 3, 4, 5, 6, 5, 6, 6, 4, 5, 7, + 6, 7, 3, 6, 6, 7, 7, 3, 4, 6, 4, 5, 3, + 10, 9, 5, 4, 4, 3, 8, 2, 6, 11, 2, 7, 5, + 5, 4, 6, 7, 10, 6, 5, 6, 6, 5, 6, 4, 9, + 2, 5, 5, 7, 5, 9, 6, 7, 7, 3, 4, 4, 7, + 3, 10, 4, 7, 6, 12, 6, 6, 9, 4, 6, 5, 4, + 7, 6, 5, 6, 7, 5, 4, 5, 6, 5, 7, 3, 7, + 13, 2, 2, 4, 6, 6, 8, 5, 17, 12, 7, 8, 8, + 2, 4, 4, 4, 4, 4, 2, 2, 4, 6, 2, 3, 6, + 5, 8, 5, 5, 8, 3, 5, 5, 6, 4, 9, 3, }; static const unsigned short int aOffset[116] = { - 0, 4, 7, 10, 10, 14, 19, 21, 26, 27, 32, 34, 36, - 42, 51, 52, 57, 61, 65, 67, 71, 74, 78, 86, 91, 94, - 99, 105, 108, 113, 118, 122, 128, 136, 142, 144, 154, 159, 164, - 167, 169, 169, 173, 177, 179, 184, 186, 188, 197, 200, 204, 210, - 216, 216, 219, 222, 226, 228, 229, 233, 240, 246, 250, 254, 261, - 267, 273, 281, 288, 297, 303, 308, 320, 320, 336, 340, 344, 350, - 351, 358, 361, 365, 370, 373, 378, 382, 386, 389, 395, 397, 406, - 412, 419, 422, 422, 425, 428, 434, 438, 442, 449, 453, 461, 468, - 473, 478, 486, 488, 492, 497, 503, 508, 514, 520, 523, 528, + 0, 2, 2, 6, 10, 13, 18, 23, 25, 26, 31, 33, 37, + 40, 47, 55, 58, 61, 63, 65, 70, 71, 76, 85, 86, 91, + 95, 99, 102, 107, 113, 123, 126, 131, 136, 141, 144, 148, 148, + 152, 157, 160, 164, 166, 169, 177, 183, 189, 189, 192, 195, 199, + 200, 204, 214, 218, 225, 231, 243, 249, 255, 264, 266, 272, 277, + 279, 286, 291, 296, 302, 308, 313, 317, 320, 326, 330, 337, 339, + 346, 348, 350, 359, 363, 369, 375, 383, 388, 388, 404, 411, 418, + 419, 426, 430, 434, 438, 442, 445, 447, 449, 452, 452, 455, 458, + 464, 468, 476, 480, 485, 493, 496, 501, 506, 512, 516, 521, }; static const unsigned char aCode[116] = { - TK_ABORT, TK_TABLE, TK_JOIN_KW, TK_TEMP, TK_TEMP, - TK_OR, TK_ADD, TK_DATABASE, TK_AS, TK_SELECT, - TK_THEN, TK_END, TK_DEFAULT, TK_TRANSACTION,TK_ON, - TK_JOIN_KW, TK_ALTER, TK_RAISE, TK_EACH, TK_CHECK, - TK_KEY, TK_AFTER, TK_REFERENCES, TK_ESCAPE, TK_ELSE, - TK_EXCEPT, TK_TRIGGER, TK_LIKE_KW, TK_EXPLAIN, TK_INITIALLY, - TK_ALL, TK_ANALYZE, TK_EXCLUSIVE, TK_EXISTS, TK_AND, - TK_DEFERRABLE, TK_ATTACH, TK_HAVING, TK_LIKE_KW, TK_BEFORE, - TK_FOR, TK_FOREIGN, TK_IGNORE, TK_REINDEX, TK_INDEX, - TK_AUTOINCR, TK_TO, TK_IN, TK_BEGIN, TK_JOIN_KW, - TK_RENAME, TK_BETWEEN, TK_NOT, TK_NOTNULL, TK_NULL, - TK_LIKE_KW, TK_BY, TK_CASCADE, TK_ASC, TK_DEFERRED, - TK_DELETE, TK_CASE, TK_CAST, TK_COLLATE, TK_COLUMNKW, - TK_COMMIT, TK_CONFLICT, TK_CONSTRAINT, TK_INTERSECT, TK_CREATE, - TK_JOIN_KW, TK_CTIME_KW, TK_CTIME_KW, TK_CTIME_KW, TK_PLAN, - TK_DESC, TK_DETACH, TK_DISTINCT, TK_IS, TK_DROP, - TK_PRAGMA, TK_MATCH, TK_FAIL, TK_LIMIT, TK_FROM, - TK_JOIN_KW, TK_GROUP, TK_UPDATE, TK_IF, TK_IMMEDIATE, - TK_INSERT, TK_INSTEAD, TK_INTO, TK_OF, TK_OFFSET, - TK_SET, TK_ISNULL, TK_JOIN, TK_ORDER, TK_REPLACE, - TK_JOIN_KW, TK_RESTRICT, TK_PRIMARY, TK_QUERY, TK_JOIN_KW, - TK_ROLLBACK, TK_ROW, TK_WHEN, TK_UNION, TK_UNIQUE, - TK_USING, TK_VACUUM, TK_VALUES, TK_VIEW, TK_WHERE, - TK_VIRTUAL, + TK_BEFORE, TK_FOREIGN, TK_FOR, TK_IGNORE, TK_LIKE_KW, + TK_EXPLAIN, TK_INSTEAD, TK_ADD, TK_DESC, TK_ESCAPE, + TK_EACH, TK_CHECK, TK_KEY, TK_CONSTRAINT, TK_INTERSECT, + TK_TABLE, TK_JOIN_KW, TK_THEN, TK_END, TK_DATABASE, + TK_AS, TK_SELECT, TK_TRANSACTION,TK_ON, TK_JOIN_KW, + TK_ALTER, TK_RAISE, TK_ELSE, TK_EXCEPT, TK_TRIGGER, + TK_REFERENCES, TK_UNIQUE, TK_QUERY, TK_ATTACH, TK_HAVING, + TK_GROUP, TK_UPDATE, TK_TEMP, TK_TEMP, TK_OR, + TK_BEGIN, TK_JOIN_KW, TK_REINDEX, TK_INDEX, TK_EXCLUSIVE, + TK_EXISTS, TK_BETWEEN, TK_NOTNULL, TK_NOT, TK_NULL, + TK_LIKE_KW, TK_CASCADE, TK_ASC, TK_DEFERRABLE, TK_CASE, + TK_COLLATE, TK_CREATE, TK_CTIME_KW, TK_DELETE, TK_DETACH, + TK_IMMEDIATE, TK_JOIN, TK_INSERT, TK_MATCH, TK_PLAN, + TK_ANALYZE, TK_PRAGMA, TK_ABORT, TK_VALUES, TK_VIRTUAL, + TK_LIMIT, TK_WHEN, TK_WHERE, TK_RENAME, TK_AFTER, + TK_REPLACE, TK_AND, TK_DEFAULT, TK_AUTOINCR, TK_TO, + TK_IN, TK_CAST, TK_COLUMNKW, TK_COMMIT, TK_CONFLICT, + TK_JOIN_KW, TK_CTIME_KW, TK_CTIME_KW, TK_PRIMARY, TK_DEFERRED, + TK_DISTINCT, TK_IS, TK_DROP, TK_FAIL, TK_FROM, + TK_JOIN_KW, TK_LIKE_KW, TK_BY, TK_IF, TK_INTO, + TK_OFFSET, TK_OF, TK_SET, TK_ISNULL, TK_ORDER, + TK_RESTRICT, TK_JOIN_KW, TK_JOIN_KW, TK_ROLLBACK, TK_ROW, + TK_UNION, TK_USING, TK_VACUUM, TK_VIEW, TK_INITIALLY, + TK_ALL, }; int h, i; if( n<2 ) return TK_ID; ============================================================ --- sqlite/legacy.c c05a599a37f703ed1e66fdb5df60c2db65f29e71 +++ sqlite/legacy.c 388c71ad7fbcd898ba1bcbfc98a3ac954bfa5d01 @@ -14,7 +14,7 @@ ** other files are for internal use by SQLite and should not be ** accessed by users of the library. ** -** $Id: legacy.c,v 1.17 2007/04/25 11:28:17 drh Exp $ +** $Id: legacy.c,v 1.18 2007/05/04 13:15:56 drh Exp $ */ #include "sqliteInt.h" @@ -118,9 +118,10 @@ exec_out: rc = sqlite3ApiExit(0, rc); if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){ - *pzErrMsg = sqlite3_malloc(1+strlen(sqlite3_errmsg(db))); + int nErrMsg = 1 + strlen(sqlite3_errmsg(db)); + *pzErrMsg = sqlite3_malloc(nErrMsg); if( *pzErrMsg ){ - strcpy(*pzErrMsg, sqlite3_errmsg(db)); + memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg); } }else if( pzErrMsg ){ *pzErrMsg = 0; ============================================================ --- sqlite/main.c e6eb036c3580ba9116fedfe4a8b58ed63d5abb37 +++ sqlite/main.c 797dc983716c1480f6af78a36be3add8806211a1 @@ -14,7 +14,7 @@ ** other files are for internal use by SQLite and should not be ** accessed by users of the library. ** -** $Id: main.c,v 1.370 2007/04/18 14:24:33 danielk1977 Exp $ +** $Id: main.c,v 1.376 2007/05/08 20:37:39 drh Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -182,6 +182,12 @@ int sqlite3_close(sqlite3 *db){ for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){ CollSeq *pColl = (CollSeq *)sqliteHashData(i); + /* Invoke any destructors registered for collation sequence user data. */ + for(j=0; j<3; j++){ + if( pColl[j].xDel ){ + pColl[j].xDel(pColl[j].pUser); + } + } sqliteFree(pColl); } sqlite3HashClear(&db->aCollSeq); @@ -264,6 +270,7 @@ const char *sqlite3ErrStr(int rc){ case SQLITE_CANTOPEN: z = "unable to open database file"; break; case SQLITE_EMPTY: z = "table contains no data"; break; case SQLITE_SCHEMA: z = "database schema has changed"; break; + case SQLITE_TOOBIG: z = "String or BLOB exceeded size limit"; break; case SQLITE_CONSTRAINT: z = "constraint failed"; break; case SQLITE_MISMATCH: z = "datatype mismatch"; break; case SQLITE_MISUSE: z = "library routine called out of sequence";break; @@ -555,7 +562,7 @@ int sqlite3_create_function16( char *zFunc8; assert( !sqlite3MallocFailed() ); - zFunc8 = sqlite3utf16to8(zFunctionName, -1); + zFunc8 = sqlite3Utf16to8(zFunctionName, -1); rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal); sqliteFree(zFunc8); @@ -825,7 +832,8 @@ static int createCollation( const char *zName, int enc, void* pCtx, - int(*xCompare)(void*,int,const void*,int,const void*) + int(*xCompare)(void*,int,const void*,int,const void*), + void(*xDel)(void*) ){ CollSeq *pColl; int enc2; @@ -860,12 +868,33 @@ static int createCollation( return SQLITE_BUSY; } sqlite3ExpirePreparedStatements(db); + + /* If collation sequence pColl was created directly by a call to + ** sqlite3_create_collation, and not generated by synthCollSeq(), + ** then any copies made by synthCollSeq() need to be invalidated. + ** Also, collation destructor - CollSeq.xDel() - function may need + ** to be called. + */ + if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){ + CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, strlen(zName)); + int j; + for(j=0; j<3; j++){ + CollSeq *p = &aColl[j]; + if( p->enc==pColl->enc ){ + if( p->xDel ){ + p->xDel(p->pUser); + } + p->xCmp = 0; + } + } + } } pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, strlen(zName), 1); if( pColl ){ pColl->xCmp = xCompare; pColl->pUser = pCtx; + pColl->xDel = xDel; pColl->enc = enc2 | (enc & SQLITE_UTF16_ALIGNED); } sqlite3Error(db, SQLITE_OK, 0); @@ -915,9 +944,9 @@ static int openDatabase( ** and UTF-16, so add a version for each to avoid any unnecessary ** conversions. The only error that can occur here is a malloc() failure. */ - if( createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc) || - createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc) || - createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc) || + if( createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0) || + createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0) || + createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0) || (db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0))==0 ){ assert( sqlite3MallocFailed() ); @@ -926,7 +955,7 @@ static int openDatabase( } /* Also add a UTF-8 case-insensitive collation sequence. */ - createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc); + createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0); /* Set flags on the built-in collating sequences */ db->pDfltColl->type = SQLITE_COLL_BINARY; @@ -936,7 +965,8 @@ static int openDatabase( } /* Open the backend database driver */ - rc = sqlite3BtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt); + rc = sqlite3BtreeFactory(db, zFilename, 0, SQLITE_DEFAULT_CACHE_SIZE, + &db->aDb[0].pBt); if( rc!=SQLITE_OK ){ sqlite3Error(db, rc, 0); db->magic = SQLITE_MAGIC_CLOSED; @@ -985,6 +1015,13 @@ static int openDatabase( } #endif +#ifdef SQLITE_ENABLE_ICU + if( !sqlite3MallocFailed() ){ + extern int sqlite3IcuInit(sqlite3*); + sqlite3IcuInit(db); + } +#endif + /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking ** mode. Doing nothing at all also makes NORMAL the default. @@ -1099,10 +1136,27 @@ int sqlite3_create_collation( ){ int rc; assert( !sqlite3MallocFailed() ); - rc = createCollation(db, zName, enc, pCtx, xCompare); + rc = createCollation(db, zName, enc, pCtx, xCompare, 0); return sqlite3ApiExit(db, rc); } +/* +** Register a new collation sequence with the database handle db. +*/ +int sqlite3_create_collation_v2( + sqlite3* db, + const char *zName, + int enc, + void* pCtx, + int(*xCompare)(void*,int,const void*,int,const void*), + void(*xDel)(void*) +){ + int rc; + assert( !sqlite3MallocFailed() ); + rc = createCollation(db, zName, enc, pCtx, xCompare, xDel); + return sqlite3ApiExit(db, rc); +} + #ifndef SQLITE_OMIT_UTF16 /* ** Register a new collation sequence with the database handle db. @@ -1117,9 +1171,9 @@ int sqlite3_create_collation16( int rc = SQLITE_OK; char *zName8; assert( !sqlite3MallocFailed() ); - zName8 = sqlite3utf16to8(zName, -1); + zName8 = sqlite3Utf16to8(zName, -1); if( zName8 ){ - rc = createCollation(db, zName8, enc, pCtx, xCompare); + rc = createCollation(db, zName8, enc, pCtx, xCompare, 0); sqliteFree(zName8); } return sqlite3ApiExit(db, rc); ============================================================ --- sqlite/opcodes.c d21a21fc60da3874a0970a6e67c81c56ec507d6c +++ sqlite/opcodes.c ecbfbd2a8f3a4e4a5b68075a4b15616b063cd461 @@ -54,18 +54,18 @@ const char *const sqlite3OpcodeNames[] = /* 50 */ "VColumn", /* 51 */ "CreateTable", /* 52 */ "Last", - /* 53 */ "IdxRowid", - /* 54 */ "MakeIdxRec", - /* 55 */ "ResetCount", - /* 56 */ "FifoWrite", - /* 57 */ "Callback", - /* 58 */ "ContextPush", - /* 59 */ "DropTrigger", + /* 53 */ "IncrVacuum", + /* 54 */ "IdxRowid", + /* 55 */ "MakeIdxRec", + /* 56 */ "ResetCount", + /* 57 */ "FifoWrite", + /* 58 */ "Callback", + /* 59 */ "ContextPush", /* 60 */ "Or", /* 61 */ "And", - /* 62 */ "DropIndex", - /* 63 */ "IdxGE", - /* 64 */ "IdxDelete", + /* 62 */ "DropTrigger", + /* 63 */ "DropIndex", + /* 64 */ "IdxGE", /* 65 */ "IsNull", /* 66 */ "NotNull", /* 67 */ "Ne", @@ -74,7 +74,7 @@ const char *const sqlite3OpcodeNames[] = /* 70 */ "Le", /* 71 */ "Lt", /* 72 */ "Ge", - /* 73 */ "Vacuum", + /* 73 */ "IdxDelete", /* 74 */ "BitAnd", /* 75 */ "BitOr", /* 76 */ "ShiftLeft", @@ -85,58 +85,58 @@ const char *const sqlite3OpcodeNames[] = /* 81 */ "Divide", /* 82 */ "Remainder", /* 83 */ "Concat", - /* 84 */ "MoveLe", + /* 84 */ "Vacuum", /* 85 */ "Negative", - /* 86 */ "IfNot", + /* 86 */ "MoveLe", /* 87 */ "BitNot", /* 88 */ "String8", - /* 89 */ "DropTable", - /* 90 */ "MakeRecord", - /* 91 */ "Delete", - /* 92 */ "AggFinal", - /* 93 */ "Dup", - /* 94 */ "Goto", - /* 95 */ "TableLock", - /* 96 */ "FifoRead", - /* 97 */ "Clear", - /* 98 */ "IdxGT", - /* 99 */ "MoveLt", - /* 100 */ "VerifyCookie", - /* 101 */ "AggStep", - /* 102 */ "Pull", - /* 103 */ "SetNumColumns", - /* 104 */ "AbsValue", - /* 105 */ "Transaction", - /* 106 */ "VFilter", - /* 107 */ "VDestroy", - /* 108 */ "ContextPop", - /* 109 */ "Next", - /* 110 */ "IdxInsert", - /* 111 */ "Distinct", - /* 112 */ "Insert", - /* 113 */ "Destroy", - /* 114 */ "ReadCookie", - /* 115 */ "ForceInt", - /* 116 */ "LoadAnalysis", - /* 117 */ "Explain", - /* 118 */ "IfMemZero", - /* 119 */ "OpenPseudo", - /* 120 */ "OpenEphemeral", - /* 121 */ "Null", - /* 122 */ "Blob", - /* 123 */ "MemStore", - /* 124 */ "Rewind", + /* 89 */ "IfNot", + /* 90 */ "DropTable", + /* 91 */ "MakeRecord", + /* 92 */ "Delete", + /* 93 */ "AggFinal", + /* 94 */ "Dup", + /* 95 */ "Goto", + /* 96 */ "TableLock", + /* 97 */ "FifoRead", + /* 98 */ "Clear", + /* 99 */ "IdxGT", + /* 100 */ "MoveLt", + /* 101 */ "VerifyCookie", + /* 102 */ "AggStep", + /* 103 */ "Pull", + /* 104 */ "SetNumColumns", + /* 105 */ "AbsValue", + /* 106 */ "Transaction", + /* 107 */ "VFilter", + /* 108 */ "VDestroy", + /* 109 */ "ContextPop", + /* 110 */ "Next", + /* 111 */ "IdxInsert", + /* 112 */ "Distinct", + /* 113 */ "Insert", + /* 114 */ "Destroy", + /* 115 */ "ReadCookie", + /* 116 */ "ForceInt", + /* 117 */ "LoadAnalysis", + /* 118 */ "Explain", + /* 119 */ "IfMemZero", + /* 120 */ "OpenPseudo", + /* 121 */ "OpenEphemeral", + /* 122 */ "Null", + /* 123 */ "Blob", + /* 124 */ "MemStore", /* 125 */ "Real", /* 126 */ "HexBlob", - /* 127 */ "MoveGe", - /* 128 */ "VBegin", - /* 129 */ "VUpdate", - /* 130 */ "VCreate", - /* 131 */ "MemMove", - /* 132 */ "MemNull", - /* 133 */ "Found", - /* 134 */ "NullRow", - /* 135 */ "NotUsed_135", + /* 127 */ "Rewind", + /* 128 */ "MoveGe", + /* 129 */ "VBegin", + /* 130 */ "VUpdate", + /* 131 */ "VCreate", + /* 132 */ "MemMove", + /* 133 */ "MemNull", + /* 134 */ "Found", + /* 135 */ "NullRow", /* 136 */ "NotUsed_136", /* 137 */ "NotUsed_137", /* 138 */ "ToText", ============================================================ --- sqlite/opcodes.h a58b9a527965930bd27502dd8b1f101d326aa08f +++ sqlite/opcodes.h f6c44489cc16671f6f7702d99b6eefa64d08b5cd @@ -69,79 +69,79 @@ #define OP_CreateTable 51 #define OP_Last 52 #define OP_IsNull 65 /* same as TK_ISNULL */ -#define OP_IdxRowid 53 -#define OP_MakeIdxRec 54 +#define OP_IncrVacuum 53 +#define OP_IdxRowid 54 +#define OP_MakeIdxRec 55 #define OP_ShiftRight 77 /* same as TK_RSHIFT */ -#define OP_ResetCount 55 -#define OP_FifoWrite 56 -#define OP_Callback 57 -#define OP_ContextPush 58 -#define OP_DropTrigger 59 -#define OP_DropIndex 62 -#define OP_IdxGE 63 -#define OP_IdxDelete 64 -#define OP_Vacuum 73 -#define OP_MoveLe 84 -#define OP_IfNot 86 -#define OP_DropTable 89 -#define OP_MakeRecord 90 +#define OP_ResetCount 56 +#define OP_FifoWrite 57 +#define OP_Callback 58 +#define OP_ContextPush 59 +#define OP_DropTrigger 62 +#define OP_DropIndex 63 +#define OP_IdxGE 64 +#define OP_IdxDelete 73 +#define OP_Vacuum 84 +#define OP_MoveLe 86 +#define OP_IfNot 89 +#define OP_DropTable 90 +#define OP_MakeRecord 91 #define OP_ToBlob 139 /* same as TK_TO_BLOB */ -#define OP_Delete 91 -#define OP_AggFinal 92 +#define OP_Delete 92 +#define OP_AggFinal 93 #define OP_ShiftLeft 76 /* same as TK_LSHIFT */ -#define OP_Dup 93 -#define OP_Goto 94 -#define OP_TableLock 95 -#define OP_FifoRead 96 -#define OP_Clear 97 -#define OP_IdxGT 98 -#define OP_MoveLt 99 +#define OP_Dup 94 +#define OP_Goto 95 +#define OP_TableLock 96 +#define OP_FifoRead 97 +#define OP_Clear 98 +#define OP_IdxGT 99 +#define OP_MoveLt 100 #define OP_Le 70 /* same as TK_LE */ -#define OP_VerifyCookie 100 -#define OP_AggStep 101 -#define OP_Pull 102 +#define OP_VerifyCookie 101 +#define OP_AggStep 102 +#define OP_Pull 103 #define OP_ToText 138 /* same as TK_TO_TEXT */ #define OP_Not 16 /* same as TK_NOT */ #define OP_ToReal 142 /* same as TK_TO_REAL */ -#define OP_SetNumColumns 103 -#define OP_AbsValue 104 -#define OP_Transaction 105 -#define OP_VFilter 106 +#define OP_SetNumColumns 104 +#define OP_AbsValue 105 +#define OP_Transaction 106 +#define OP_VFilter 107 #define OP_Negative 85 /* same as TK_UMINUS */ #define OP_Ne 67 /* same as TK_NE */ -#define OP_VDestroy 107 -#define OP_ContextPop 108 +#define OP_VDestroy 108 +#define OP_ContextPop 109 #define OP_BitOr 75 /* same as TK_BITOR */ -#define OP_Next 109 -#define OP_IdxInsert 110 -#define OP_Distinct 111 +#define OP_Next 110 +#define OP_IdxInsert 111 +#define OP_Distinct 112 #define OP_Lt 71 /* same as TK_LT */ -#define OP_Insert 112 -#define OP_Destroy 113 -#define OP_ReadCookie 114 -#define OP_ForceInt 115 -#define OP_LoadAnalysis 116 -#define OP_Explain 117 -#define OP_IfMemZero 118 -#define OP_OpenPseudo 119 -#define OP_OpenEphemeral 120 -#define OP_Null 121 -#define OP_Blob 122 +#define OP_Insert 113 +#define OP_Destroy 114 +#define OP_ReadCookie 115 +#define OP_ForceInt 116 +#define OP_LoadAnalysis 117 +#define OP_Explain 118 +#define OP_IfMemZero 119 +#define OP_OpenPseudo 120 +#define OP_OpenEphemeral 121 +#define OP_Null 122 +#define OP_Blob 123 #define OP_Add 78 /* same as TK_PLUS */ -#define OP_MemStore 123 -#define OP_Rewind 124 -#define OP_MoveGe 127 -#define OP_VBegin 128 -#define OP_VUpdate 129 +#define OP_MemStore 124 +#define OP_Rewind 127 +#define OP_MoveGe 128 +#define OP_VBegin 129 +#define OP_VUpdate 130 #define OP_BitNot 87 /* same as TK_BITNOT */ -#define OP_VCreate 130 -#define OP_MemMove 131 -#define OP_MemNull 132 -#define OP_Found 133 -#define OP_NullRow 134 +#define OP_VCreate 131 +#define OP_MemMove 132 +#define OP_MemNull 133 +#define OP_Found 134 +#define OP_NullRow 135 /* The following opcode values are never used */ -#define OP_NotUsed_135 135 #define OP_NotUsed_136 136 #define OP_NotUsed_137 137 @@ -151,10 +151,10 @@ #define NOPUSH_MASK_0 0xeeb4 #define NOPUSH_MASK_1 0x796b #define NOPUSH_MASK_2 0x7ddb -#define NOPUSH_MASK_3 0xff92 +#define NOPUSH_MASK_3 0xff32 #define NOPUSH_MASK_4 0xffff -#define NOPUSH_MASK_5 0xdaf7 -#define NOPUSH_MASK_6 0xfefe -#define NOPUSH_MASK_7 0x99d9 -#define NOPUSH_MASK_8 0x7c67 +#define NOPUSH_MASK_5 0xb6f7 +#define NOPUSH_MASK_6 0xfdfd +#define NOPUSH_MASK_7 0x93b3 +#define NOPUSH_MASK_8 0x7ccf #define NOPUSH_MASK_9 0x0000 ============================================================ --- sqlite/os.c 4650e98aadd27abfe1698ff58edf6893c58d4881 +++ sqlite/os.c 1f10b47acc1177fb9225edb4f5f0d25ed716f9cb @@ -53,14 +53,6 @@ void sqlite3OsSetFullSync(OsFile *id, in void sqlite3OsSetFullSync(OsFile *id, int value){ id->pMethod->xSetFullSync(id, value); } -#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) -/* This method is currently only used while interactively debugging the -** pager. More specificly, it can only be used when sqlite3DebugPrintf() is -** included in the build. */ -int sqlite3OsFileHandle(OsFile *id){ - return id->pMethod->xFileHandle(id); -} -#endif int sqlite3OsFileSize(OsFile *id, i64 *pSize){ return id->pMethod->xFileSize(id, pSize); } @@ -70,9 +62,6 @@ int sqlite3OsUnlock(OsFile *id, int lock int sqlite3OsUnlock(OsFile *id, int lockType){ return id->pMethod->xUnlock(id, lockType); } -int sqlite3OsLockState(OsFile *id){ - return id->pMethod->xLockState(id); -} int sqlite3OsCheckReservedLock(OsFile *id){ return id->pMethod->xCheckReservedLock(id); } @@ -81,6 +70,16 @@ int sqlite3OsSectorSize(OsFile *id){ return xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE; } +#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) + /* These methods are currently only used for testing and debugging. */ + int sqlite3OsFileHandle(OsFile *id){ + return id->pMethod->xFileHandle(id); + } + int sqlite3OsLockState(OsFile *id){ + return id->pMethod->xLockState(id); + } +#endif + #ifdef SQLITE_ENABLE_REDEF_IO /* ** A function to return a pointer to the virtual function table. ============================================================ --- sqlite/os.h 9240adf088fd55732f8926f9017601ba3430ead8 +++ sqlite/os.h 213dd1f8f7ab9159c04a893eadc0cdd52515df16 @@ -352,11 +352,9 @@ void sqlite3OsSetFullSync(OsFile *id, in int sqlite3OsTruncate(OsFile*, i64 size); int sqlite3OsSync(OsFile*, int); void sqlite3OsSetFullSync(OsFile *id, int setting); -int sqlite3OsFileHandle(OsFile *id); int sqlite3OsFileSize(OsFile*, i64 *pSize); int sqlite3OsLock(OsFile*, int); int sqlite3OsUnlock(OsFile*, int); -int sqlite3OsLockState(OsFile *id); int sqlite3OsCheckReservedLock(OsFile *id); int sqlite3OsOpenReadWrite(const char*, OsFile**, int*); int sqlite3OsOpenExclusive(const char*, OsFile**, int); @@ -383,6 +381,11 @@ int sqlite3OsDlclose(void*); void *sqlite3OsDlsym(void*, const char*); int sqlite3OsDlclose(void*); +#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) + int sqlite3OsFileHandle(OsFile *id); + int sqlite3OsLockState(OsFile *id); +#endif + /* ** If the SQLITE_ENABLE_REDEF_IO macro is defined, then the OS-layer ** interface routines are not called directly but are invoked using ============================================================ --- sqlite/os_unix.c 426b4c03c304ad78746d65d9ba101e0b72e18e23 +++ sqlite/os_unix.c f2ccf2e9a925fc679faf7a8fe85700e0f13cf0e1 @@ -619,7 +619,7 @@ static sqlite3LockingStyle sqlite3Detect return unsupportedLockingStyle; return sqlite3TestLockingStyle(filePath, fd); -#endif // SQLITE_FIXED_LOCKING_STYLE +#endif /* SQLITE_FIXED_LOCKING_STYLE */ } #endif /* SQLITE_ENABLE_LOCKING_STYLE */ @@ -959,7 +959,7 @@ int sqlite3UnixTempFileName(char *zBuf){ break; } do{ - sprintf(zBuf, "%s/"TEMP_FILE_PREFIX, zDir); + sqlite3_snprintf(SQLITE_TEMPNAME_SIZE, zBuf, "%s/"TEMP_FILE_PREFIX, zDir); j = strlen(zBuf); sqlite3Randomness(15, &zBuf[j]); for(i=0; i<15; i++, j++){ @@ -1733,7 +1733,7 @@ static int _AFPFSSetLock(const char *pat if ( err==-1 ) { OSTRACE4("AFPLOCK failed to fsctl() '%s' %d %s\n", path, errno, strerror(errno)); - return 1; // error + return 1; /* error */ } else { return 0; } @@ -1760,7 +1760,7 @@ static int afpUnixCheckReservedLock(OsFi /* Otherwise see if some other process holds it. */ if ( !r ) { - // lock the byte + /* lock the byte */ int failed = _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1,1); if (failed) { /* if we failed to get the lock then someone else must have it */ @@ -2015,36 +2015,36 @@ static int flockUnixCheckReservedLock(Os unixFile *pFile = (unixFile*)id; if (pFile->locktype == RESERVED_LOCK) { - return 1; // already have a reserved lock + return 1; /* already have a reserved lock */ } else { - // attempt to get the lock + /* attempt to get the lock */ int rc = flock(pFile->h, LOCK_EX | LOCK_NB); if (!rc) { - // got the lock, unlock it + /* got the lock, unlock it */ flock(pFile->h, LOCK_UN); - return 0; // no one has it reserved + return 0; /* no one has it reserved */ } - return 1; // someone else might have it reserved + return 1; /* someone else might have it reserved */ } } static int flockUnixLock(OsFile *id, int locktype) { unixFile *pFile = (unixFile*)id; - // if we already have a lock, it is exclusive. - // Just adjust level and punt on outta here. + /* if we already have a lock, it is exclusive. + ** Just adjust level and punt on outta here. */ if (pFile->locktype > NO_LOCK) { pFile->locktype = locktype; return SQLITE_OK; } - // grab an exclusive lock + /* grab an exclusive lock */ int rc = flock(pFile->h, LOCK_EX | LOCK_NB); if (rc) { - // didn't get, must be busy + /* didn't get, must be busy */ return SQLITE_BUSY; } else { - // got it, set the type and return ok + /* got it, set the type and return ok */ pFile->locktype = locktype; return SQLITE_OK; } @@ -2055,18 +2055,18 @@ static int flockUnixUnlock(OsFile *id, i assert( locktype<=SHARED_LOCK ); - // no-op if possible + /* no-op if possible */ if( pFile->locktype==locktype ){ return SQLITE_OK; } - // shared can just be set because we always have an exclusive + /* shared can just be set because we always have an exclusive */ if (locktype==SHARED_LOCK) { pFile->locktype = locktype; return SQLITE_OK; } - // no, really, unlock. + /* no, really, unlock. */ int rc = flock(pFile->h, LOCK_UN); if (rc) return SQLITE_IOERR_UNLOCK; @@ -2117,14 +2117,14 @@ static int dotlockUnixCheckReservedLock( (dotlockLockingContext *) pFile->lockingContext; if (pFile->locktype == RESERVED_LOCK) { - return 1; // already have a reserved lock + return 1; /* already have a reserved lock */ } else { struct stat statBuf; if (lstat(context->lockPath,&statBuf) == 0) - // file exists, someone else has the lock + /* file exists, someone else has the lock */ return 1; else - // file does not exist, we could have it if we want it + /* file does not exist, we could have it if we want it */ return 0; } } @@ -2134,8 +2134,8 @@ static int dotlockUnixLock(OsFile *id, i dotlockLockingContext *context = (dotlockLockingContext *) pFile->lockingContext; - // if we already have a lock, it is exclusive. - // Just adjust level and punt on outta here. + /* if we already have a lock, it is exclusive. + ** Just adjust level and punt on outta here. */ if (pFile->locktype > NO_LOCK) { pFile->locktype = locktype; @@ -2144,21 +2144,21 @@ static int dotlockUnixLock(OsFile *id, i return SQLITE_OK; } - // check to see if lock file already exists + /* check to see if lock file already exists */ struct stat statBuf; if (lstat(context->lockPath,&statBuf) == 0){ - return SQLITE_BUSY; // it does, busy + return SQLITE_BUSY; /* it does, busy */ } - // grab an exclusive lock + /* grab an exclusive lock */ int fd = open(context->lockPath,O_RDONLY|O_CREAT|O_EXCL,0600); if (fd < 0) { - // failed to open/create the file, someone else may have stolen the lock + /* failed to open/create the file, someone else may have stolen the lock */ return SQLITE_BUSY; } close(fd); - // got it, set the type and return ok + /* got it, set the type and return ok */ pFile->locktype = locktype; return SQLITE_OK; } @@ -2170,18 +2170,18 @@ static int dotlockUnixUnlock(OsFile *id, assert( locktype<=SHARED_LOCK ); - // no-op if possible + /* no-op if possible */ if( pFile->locktype==locktype ){ return SQLITE_OK; } - // shared can just be set because we always have an exclusive + /* shared can just be set because we always have an exclusive */ if (locktype==SHARED_LOCK) { pFile->locktype = locktype; return SQLITE_OK; } - // no, really, unlock. + /* no, really, unlock. */ unlink(context->lockPath); pFile->locktype = NO_LOCK; return SQLITE_OK; @@ -2496,7 +2496,7 @@ static int allocateUnixFile( return SQLITE_NOMEM; } } else { - // pLock and pOpen are only used for posix advisory locking + /* pLock and pOpen are only used for posix advisory locking */ f.pLock = NULL; f.pOpen = NULL; } @@ -2518,33 +2518,40 @@ static int allocateUnixFile( }else{ *pNew = f; switch(lockingStyle) { - case afpLockingStyle: + case afpLockingStyle: { /* afp locking uses the file path so it needs to be included in ** the afpLockingContext */ + int nFilename; pNew->pMethod = &sqlite3AFPLockingUnixIoMethod; pNew->lockingContext = sqlite3ThreadSafeMalloc(sizeof(afpLockingContext)); + nFilename = strlen(zFilename)+1; ((afpLockingContext *)pNew->lockingContext)->filePath = - sqlite3ThreadSafeMalloc(strlen(zFilename) + 1); - strcpy(((afpLockingContext *)pNew->lockingContext)->filePath, - zFilename); + sqlite3ThreadSafeMalloc(nFilename); + memcpy(((afpLockingContext *)pNew->lockingContext)->filePath, + zFilename, nFilename); srandomdev(); break; + } case flockLockingStyle: /* flock locking doesn't need additional lockingContext information */ pNew->pMethod = &sqlite3FlockLockingUnixIoMethod; break; - case dotlockLockingStyle: + case dotlockLockingStyle: { /* dotlock locking uses the file path so it needs to be included in ** the dotlockLockingContext */ + int nFilename; pNew->pMethod = &sqlite3DotlockLockingUnixIoMethod; pNew->lockingContext = sqlite3ThreadSafeMalloc( sizeof(dotlockLockingContext)); + nFilename = strlen(zFilename) + 6; ((dotlockLockingContext *)pNew->lockingContext)->lockPath = - sqlite3ThreadSafeMalloc(strlen(zFilename) + strlen(".lock") + 1); - sprintf(((dotlockLockingContext *)pNew->lockingContext)->lockPath, + sqlite3ThreadSafeMalloc( nFilename ); + sqlite3_snprintf(nFilename, + ((dotlockLockingContext *)pNew->lockingContext)->lockPath, "%s.lock", zFilename); break; + } case posixLockingStyle: /* posix locking doesn't need additional lockingContext information */ pNew->pMethod = &sqlite3UnixIoMethod; ============================================================ --- sqlite/os_win.c e94903c7dc1c0599c8ddce42efa0b6928068ddc5 +++ sqlite/os_win.c d868d5f9e95ec9c1b9e2a30c54c996053db6dddd @@ -946,7 +946,8 @@ int sqlite3WinTempFileName(char *zBuf){ for(i=strlen(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){} zTempPath[i] = 0; for(;;){ - sprintf(zBuf, "%s\\"TEMP_FILE_PREFIX, zTempPath); + sqlite3_snprintf(SQLITE_TEMPNAME_SIZE, zBuf, + "%s\\"TEMP_FILE_PREFIX, zTempPath); j = strlen(zBuf); sqlite3Randomness(15, &zBuf[j]); for(i=0; i<15; i++, j++){ @@ -1289,6 +1290,7 @@ static int winLock(OsFile *id, int lockt newLocktype = EXCLUSIVE_LOCK; }else{ OSTRACE2("error-code = %d\n", GetLastError()); + getReadLock(pFile); } } ============================================================ --- sqlite/pager.c 33c632ce9c228d87f14879a139fa123d43e4bf25 +++ sqlite/pager.c 39352b58ee840cae715a4f0d20e446aa5e1445fe @@ -18,7 +18,7 @@ ** file simultaneously, or one process from reading the database while ** another is writing. ** -** @(#) $Id: pager.c,v 1.329 2007/04/16 15:02:19 drh Exp $ +** @(#) $Id: pager.c,v 1.348 2007/06/18 17:25:18 drh Exp $ */ #ifndef SQLITE_OMIT_DISKIO #include "sqliteInt.h" @@ -150,6 +150,78 @@ ** The PgHdr.dirty flag is set when sqlite3PagerWrite() is called and ** is cleared again when the page content is written back to the original ** database file. +** +** Details of important structure elements: +** +** needSync +** +** If this is true, this means that it is not safe to write the page +** content to the database because the original content needed +** for rollback has not by synced to the main rollback journal. +** The original content may have been written to the rollback journal +** but it has not yet been synced. So we cannot write to the database +** file because power failure might cause the page in the journal file +** to never reach the disk. It is as if the write to the journal file +** does not occur until the journal file is synced. +** +** This flag is false if the page content exactly matches what +** currently exists in the database file. The needSync flag is also +** false if the original content has been written to the main rollback +** journal and synced. If the page represents a new page that has +** been added onto the end of the database during the current +** transaction, the needSync flag is true until the original database +** size in the journal header has been synced to disk. +** +** inJournal +** +** This is true if the original page has been written into the main +** rollback journal. This is always false for new pages added to +** the end of the database file during the current transaction. +** And this flag says nothing about whether or not the journal +** has been synced to disk. For pages that are in the original +** database file, the following expression should always be true: +** +** inJournal = (pPager->aInJournal[(pgno-1)/8] & (1<<((pgno-1)%8))!=0 +** +** The pPager->aInJournal[] array is only valid for the original +** pages of the database, not new pages that are added to the end +** of the database, so obviously the above expression cannot be +** valid for new pages. For new pages inJournal is always 0. +** +** dirty +** +** When true, this means that the content of the page has been +** modified and needs to be written back to the database file. +** If false, it means that either the content of the page is +** unchanged or else the content is unimportant and we do not +** care whether or not it is preserved. +** +** alwaysRollback +** +** This means that the sqlite3PagerDontRollback() API should be +** ignored for this page. The DontRollback() API attempts to say +** that the content of the page on disk is unimportant (it is an +** unused page on the freelist) so that it is unnecessary to +** rollback changes to this page because the content of the page +** can change without changing the meaning of the database. This +** flag overrides any DontRollback() attempt. This flag is set +** when a page that originally contained valid data is added to +** the freelist. Later in the same transaction, this page might +** be pulled from the freelist and reused for something different +** and at that point the DontRollback() API will be called because +** pages taken from the freelist do not need to be protected by +** the rollback journal. But this flag says that the page was +** not originally part of the freelist so that it still needs to +** be rolled back in spite of any subsequent DontRollback() calls. +** +** needRead +** +** This flag means (when true) that the content of the page has +** not yet been loaded from disk. The in-memory content is just +** garbage. (Actually, we zero the content, but you should not +** make any assumptions about the content nevertheless.) If the +** content is needed in the future, it should be read from the +** original database file. */ typedef struct PgHdr PgHdr; struct PgHdr { @@ -256,9 +328,9 @@ struct Pager { int nExtra; /* Add this many bytes to each in-memory page */ int pageSize; /* Number of bytes in a page */ int nPage; /* Total number of in-memory pages */ - int nMaxPage; /* High water mark of nPage */ int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */ int mxPage; /* Maximum number of pages to hold in cache */ + Pgno mxPgno; /* Maximum allowed size of the database */ u8 *aInJournal; /* One bit for each page in the database file */ u8 *aInStmt; /* One bit for each page in the database */ char *zFilename; /* Name of the database file */ @@ -458,7 +530,7 @@ static int read32bits(OsFile *fd, u32 *p unsigned char ac[4]; int rc = sqlite3OsRead(fd, ac, sizeof(ac)); if( rc==SQLITE_OK ){ - *pRes = (ac[0]<<24) | (ac[1]<<16) | (ac[2]<<8) | ac[3]; + *pRes = sqlite3Get4byte(ac); } return rc; } @@ -466,12 +538,7 @@ static int read32bits(OsFile *fd, u32 *p /* ** Write a 32-bit integer into a string buffer in big-endian byte order. */ -static void put32bits(char *ac, u32 val){ - ac[0] = (val>>24) & 0xff; - ac[1] = (val>>16) & 0xff; - ac[2] = (val>>8) & 0xff; - ac[3] = val & 0xff; -} +#define put32bits(A,B) sqlite3Put4byte((u8*)A,B) /* ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK @@ -490,7 +557,7 @@ static u32 retrieve32bits(PgHdr *p, int static u32 retrieve32bits(PgHdr *p, int offset){ unsigned char *ac; ac = &((unsigned char*)PGHDR_TO_DATA(p))[offset]; - return (ac[0]<<24) | (ac[1]<<16) | (ac[2]<<8) | ac[3]; + return sqlite3Get4byte(ac); } @@ -517,19 +584,27 @@ static int pager_error(Pager *pPager, in return rc; } +/* +** If SQLITE_CHECK_PAGES is defined then we do some sanity checking +** on the cache using a hash function. This is used for testing +** and debugging only. +*/ #ifdef SQLITE_CHECK_PAGES /* ** Return a 32-bit hash of the page data for pPage. */ -static u32 pager_pagehash(PgHdr *pPage){ +static u32 pager_datahash(int nByte, unsigned char *pData){ u32 hash = 0; int i; - unsigned char *pData = (unsigned char *)PGHDR_TO_DATA(pPage); - for(i=0; ipPager->pageSize; i++){ - hash = (hash+i)^pData[i]; + for(i=0; ipPager->pageSize, + (unsigned char *)PGHDR_TO_DATA(pPage)); +} /* ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES @@ -544,6 +619,8 @@ static void checkPage(PgHdr *pPg){ } #else +#define pager_datahash(X,Y) 0 +#define pager_pagehash(X) 0 #define CHECK_PAGE(x) #endif @@ -1086,6 +1163,13 @@ static int pager_playback_one_page(Pager ** page in the pager cache. In this case just update the pager cache, ** not the database file. The page is left marked dirty in this case. ** + ** An exception to the above rule: If the database is in no-sync mode + ** and a page is moved during an incremental vacuum then the page may + ** not be in the pager cache. Later: if a malloc() or IO error occurs + ** during a Movepage() call, then the page may not be in the cache + ** either. So the condition described in the above paragraph is not + ** assert()able. + ** ** If in EXCLUSIVE state, then we update the pager cache if it exists ** and the main file. The page is then marked not dirty. ** @@ -1094,17 +1178,18 @@ static int pager_playback_one_page(Pager ** This occurs when a page is changed prior to the start of a statement ** then changed again within the statement. When rolling back such a ** statement we must not write to the original database unless we know - ** for certain that original page contents are in the main rollback - ** journal. Otherwise, if a full ROLLBACK occurs after the statement - ** rollback the full ROLLBACK will not restore the page to its original - ** content. Two conditions must be met before writing to the database - ** files. (1) the database must be locked. (2) we know that the original - ** page content is in the main journal either because the page is not in - ** cache or else it is marked as needSync==0. + ** for certain that original page contents are synced into the main rollback + ** journal. Otherwise, a power loss might leave modified data in the + ** database file without an entry in the rollback journal that can + ** restore the database to its original form. Two conditions must be + ** met before writing to the database files. (1) the database must be + ** locked. (2) we know that the original page content is fully synced + ** in the main journal either because the page is not in cache or else + ** the page is marked as needSync==0. */ pPg = pager_lookup(pPager, pgno); - assert( pPager->state>=PAGER_EXCLUSIVE || pPg!=0 ); - PAGERTRACE3("PLAYBACK %d page %d\n", PAGERID(pPager), pgno); + PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n", + PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData)); if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) ){ rc = sqlite3OsSeek(pPager->fd, (pgno-1)*(i64)pPager->pageSize); if( rc==SQLITE_OK ){ @@ -1251,6 +1336,19 @@ static int pager_truncate(Pager *pPager, } /* +** Set the sectorSize for the given pager. +** +** The sector size is the larger of the sector size reported +** by sqlite3OsSectorSize() and the pageSize. +*/ +static void setSectorSize(Pager *pPager){ + pPager->sectorSize = sqlite3OsSectorSize(pPager->fd); + if( pPager->sectorSizepageSize ){ + pPager->sectorSize = pPager->pageSize; + } +} + +/* ** Playback the journal and thus restore the database file to ** the state it was in before we started making changes. ** @@ -1417,7 +1515,7 @@ end_playback: ** back a journal created by a process with a different sector size ** value. Reset it to the correct value for this process. */ - pPager->sectorSize = sqlite3OsSectorSize(pPager->fd); + setSectorSize(pPager); return rc; } @@ -1723,14 +1821,14 @@ int sqlite3PagerOpen( pPager->zFilename = (char*)&pPager[1]; pPager->zDirectory = &pPager->zFilename[nameLen+1]; pPager->zJournal = &pPager->zDirectory[nameLen+1]; - strcpy(pPager->zFilename, zFullPathname); - strcpy(pPager->zDirectory, zFullPathname); + memcpy(pPager->zFilename, zFullPathname, nameLen+1); + memcpy(pPager->zDirectory, zFullPathname, nameLen+1); for(i=nameLen; i>0 && pPager->zDirectory[i-1]!='/'; i--){} if( i>0 ) pPager->zDirectory[i-1] = 0; - strcpy(pPager->zJournal, zFullPathname); + memcpy(pPager->zJournal, zFullPathname,nameLen); sqliteFree(zFullPathname); - strcpy(&pPager->zJournal[nameLen], "-journal"); + memcpy(&pPager->zJournal[nameLen], "-journal",sizeof("-journal")); pPager->fd = fd; /* pPager->journalOpen = 0; */ pPager->useJournal = useJournal && !memDb; @@ -1743,8 +1841,8 @@ int sqlite3PagerOpen( /* pPager->stmtSize = 0; */ /* pPager->stmtJSize = 0; */ /* pPager->nPage = 0; */ - /* pPager->nMaxPage = 0; */ pPager->mxPage = 100; + pPager->mxPgno = SQLITE_MAX_PAGE_COUNT; assert( PAGER_UNLOCK==0 ); /* pPager->state = PAGER_UNLOCK; */ /* pPager->errMask = 0; */ @@ -1764,7 +1862,7 @@ int sqlite3PagerOpen( pPager->nExtra = FORCE_ALIGNMENT(nExtra); assert(fd||memDb); if( !memDb ){ - pPager->sectorSize = sqlite3OsSectorSize(fd); + setSectorSize(pPager); } /* pPager->pBusyHandler = 0; */ /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */ @@ -1822,6 +1920,21 @@ int sqlite3PagerSetPagesize(Pager *pPage } /* +** Attempt to set the maximum database page count if mxPage is positive. +** Make no changes if mxPage is zero or negative. And never reduce the +** maximum page count below the current size of the database. +** +** Regardless of mxPage, return the current maximum page count. +*/ +int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){ + if( mxPage>0 ){ + pPager->mxPgno = mxPage; + } + sqlite3PagerPagecount(pPager); + return pPager->mxPgno; +} + +/* ** The following set of routines are used to disable the simulated ** I/O error mechanism. These routines are used to avoid simulated ** errors in places where we do not care about errors. @@ -1906,6 +2019,9 @@ int sqlite3PagerPagecount(Pager *pPager) if( n==(PENDING_BYTE/pPager->pageSize) ){ n++; } + if( n>pPager->mxPgno ){ + pPager->mxPgno = n; + } return n; } @@ -2160,12 +2276,14 @@ int sqlite3PagerClose(Pager *pPager){ return SQLITE_OK; } +#if !defined(NDEBUG) || defined(SQLITE_TEST) /* ** Return the page number for the given page data. */ Pgno sqlite3PagerPagenumber(DbPage *p){ return p->pgno; } +#endif /* ** The page_ref() function increments the reference count for a page. @@ -2288,7 +2406,7 @@ static int syncJournal(Pager *pPager){ if( rc ) return rc; } PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager)); - IOTRACE(("JSYNC %d\n", pPager)) + IOTRACE(("JSYNC %p\n", pPager)) rc = sqlite3OsSync(pPager->jfd, pPager->full_fsync); if( rc!=0 ) return rc; pPager->journalStarted = 1; @@ -2438,7 +2556,8 @@ static int pager_write_pagelist(PgHdr *p */ if( pList->pgno<=pPager->dbSize ){ char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6); - PAGERTRACE3("STORE %d page %d\n", PAGERID(pPager), pList->pgno); + PAGERTRACE4("STORE %d page %d hash(%08x)\n", + PAGERID(pPager), pList->pgno, pager_pagehash(pList)); IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno)); rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize); PAGER_INCR(sqlite3_pager_writedb_count); @@ -2592,7 +2711,7 @@ static int pager_recycle(Pager *pPager, ** free as much memory as possible. The return value is the total number ** of bytes of memory released. */ -#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT +#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO) int sqlite3PagerReleaseMemory(int nReq){ const ThreadData *pTsdro = sqlite3ThreadDataReadOnly(); int nReleased = 0; @@ -2668,7 +2787,7 @@ int sqlite3PagerReleaseMemory(int nReq){ return nReleased; } -#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */ +#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT && !SQLITE_OMIT_DISKIO */ /* ** Read the content of page pPg out of the database file. @@ -2684,12 +2803,13 @@ static int readDbPage(Pager *pPager, PgH PAGER_INCR(sqlite3_pager_readdb_count); PAGER_INCR(pPager->nRead); IOTRACE(("PGIN %p %d\n", pPager, pgno)); - PAGERTRACE3("FETCH %d page %d\n", PAGERID(pPager), pPg->pgno); if( pgno==1 ){ memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24], sizeof(pPager->dbFileVers)); } CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3); + PAGERTRACE4("FETCH %d page %d hash(%08x)\n", + PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)); return rc; } @@ -2800,7 +2920,7 @@ static int pagerSharedLock(Pager *pPager ** a codec is in use. ** ** There is a vanishingly small chance that a change will not be - ** deteched. The chance of an undetected change is so small that + ** detected. The chance of an undetected change is so small that ** it can be neglected. */ char dbFileVers[sizeof(pPager->dbFileVers)]; @@ -2811,6 +2931,7 @@ static int pagerSharedLock(Pager *pPager } if( pPager->dbSize>0 ){ + IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers))); rc = sqlite3OsSeek(pPager->fd, 24); if( rc!=SQLITE_OK ){ return rc; @@ -2903,13 +3024,12 @@ static int pagerAllocatePage(Pager *pPag pPg->pNextAll = pPager->pAll; pPager->pAll = pPg; pPager->nPage++; - if( pPager->nPage>pPager->nMaxPage ){ - assert( pPager->nMaxPage==(pPager->nPage-1) ); - pPager->nMaxPage++; - } }else{ /* Recycle an existing page with a zero ref-count. */ rc = pager_recycle(pPager, 1, &pPg); + if( rc==SQLITE_BUSY ){ + rc = SQLITE_IOERR_BLOCKED; + } if( rc!=SQLITE_OK ){ goto pager_allocate_out; } @@ -2923,12 +3043,31 @@ pager_allocate_out: } /* +** Make sure we have the content for a page. If the page was +** previously acquired with noContent==1, then the content was +** just initialized to zeros instead of being read from disk. +** But now we need the real data off of disk. So make sure we +** have it. Read it in if we do not have it already. +*/ +static int pager_get_content(PgHdr *pPg){ + if( pPg->needRead ){ + int rc = readDbPage(pPg->pPager, pPg, pPg->pgno); + if( rc==SQLITE_OK ){ + pPg->needRead = 0; + }else{ + return rc; + } + } + return SQLITE_OK; +} + +/* ** Acquire a page. ** ** A read lock on the disk file is obtained when the first page is acquired. ** This read lock is dropped when the last page is released. ** -** A _get works for any page number greater than 0. If the database +** This routine works for any page number greater than 0. If the database ** file is smaller than the requested page, then no actual disk ** read occurs and the memory image of the page is initialized to ** all zeros. The extra data appended to a page is always initialized @@ -2937,12 +3076,12 @@ pager_allocate_out: ** The acquisition might fail for several reasons. In all cases, ** an appropriate error code is returned and *ppPage is set to NULL. ** -** See also sqlite3PagerLookup(). Both this routine and _lookup() attempt +** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt ** to find a page in the in-memory cache first. If the page is not already -** in memory, this routine goes to disk to read it in whereas _lookup() +** in memory, this routine goes to disk to read it in whereas Lookup() ** just returns 0. This routine acquires a read-lock the first time it ** has to go to disk, and could also playback an old journal if necessary. -** Since _lookup() never goes to disk, it never has to deal with locks +** Since Lookup() never goes to disk, it never has to deal with locks ** or journal files. ** ** If noContent is false, the page contents are actually read from disk. @@ -2950,8 +3089,9 @@ pager_allocate_out: ** of the page at this time, so do not do a disk read. Just fill in the ** page content with zeros. But mark the fact that we have not read the ** content by setting the PgHdr.needRead flag. Later on, if -** sqlite3PagerWrite() is called on this page, that means that the -** content is needed and the disk read should occur at that point. +** sqlite3PagerWrite() is called on this page or if this routine is +** called again with noContent==0, that means that the content is needed +** and the disk read should occur at that point. */ int sqlite3PagerAcquire( Pager *pPager, /* The pager open on the database file */ @@ -3031,6 +3171,10 @@ int sqlite3PagerAcquire( ** file, or by setting the entire page to zero. */ if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){ + if( pgno>pPager->mxPgno ){ + sqlite3PagerUnref(pPg); + return SQLITE_FULL; + } memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize); pPg->needRead = noContent && !pPager->alwaysRollback; IOTRACE(("ZERO %p %d\n", pPager, pgno)); @@ -3041,6 +3185,7 @@ int sqlite3PagerAcquire( sqlite3PagerUnref(pPg); return rc; } + pPg->needRead = 0; } /* Link the page into the page hash table */ @@ -3060,6 +3205,12 @@ int sqlite3PagerAcquire( /* The requested page is in the page cache. */ assert(pPager->nRef>0 || pgno==1); PAGER_INCR(pPager->nHit); + if( !noContent ){ + rc = pager_get_content(pPg); + if( rc ){ + return rc; + } + } page_ref(pPg); } *ppPage = pPg; @@ -3367,13 +3518,9 @@ static int pager_write(PgHdr *pPg){ ** can be stored in the rollback journal. So do the read at this ** time. */ - if( pPg->needRead ){ - rc = readDbPage(pPager, pPg, pPg->pgno); - if( rc==SQLITE_OK ){ - pPg->needRead = 0; - }else{ - return rc; - } + rc = pager_get_content(pPg); + if( rc ){ + return rc; } /* Mark the page as dirty. If the page has already been written @@ -3439,8 +3586,8 @@ static int pager_write(PgHdr *pPg){ pPager->journalOff, szPg)); PAGER_INCR(sqlite3_pager_writej_count); pPager->journalOff += szPg; - PAGERTRACE4("JOURNAL %d page %d needSync=%d\n", - PAGERID(pPager), pPg->pgno, pPg->needSync); + PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n", + PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg)); *(u32*)pEnd = saved; /* An error has occured writing to the journal file. The @@ -3828,6 +3975,14 @@ sync_exit: } sync_exit: + if( rc==SQLITE_IOERR_BLOCKED ){ + /* pager_incr_changecounter() may attempt to obtain an exclusive + * lock to spill the cache and return IOERR_BLOCKED. But since + * there is no chance the cache is inconsistent, it's + * better to return SQLITE_BUSY. + */ + rc = SQLITE_BUSY; + } return rc; } @@ -4161,15 +4316,15 @@ void sqlite3PagerSetCodec( #ifndef SQLITE_OMIT_AUTOVACUUM /* -** Move the page identified by pData to location pgno in the file. +** Move the page pPg to location pgno in the file. ** -** There must be no references to the current page pgno. If current page -** pgno is not already in the rollback journal, it is not written there by -** by this routine. The same applies to the page pData refers to on entry to -** this routine. +** There must be no references to the page previously located at +** pgno (which we call pPgOld) though that page is allowed to be +** in cache. If the page previous located at pgno is not already +** in the rollback journal, it is not put there by by this routine. ** -** References to the page refered to by pData remain valid. Updating any -** meta-data associated with page pData (i.e. data stored in the nExtra bytes +** References to the page pPg remain valid. Updating any +** meta-data associated with pPg (i.e. data stored in the nExtra bytes ** allocated along with the page) is the responsibility of the caller. ** ** A transaction must be active when this routine is called. It used to be @@ -4178,7 +4333,7 @@ int sqlite3PagerMovepage(Pager *pPager, ** transaction is active). */ int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){ - PgHdr *pPgOld; + PgHdr *pPgOld; /* The page being overwritten. */ int h; Pgno needSyncPgno = 0; @@ -4188,9 +4343,10 @@ int sqlite3PagerMovepage(Pager *pPager, PAGERID(pPager), pPg->pgno, pPg->needSync, pgno); IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno)) + pager_get_content(pPg); if( pPg->needSync ){ needSyncPgno = pPg->pgno; - assert( pPg->inJournal ); + assert( pPg->inJournal || (int)pgno>pPager->origDbSize ); assert( pPg->dirty ); assert( pPager->needSync ); } @@ -4203,18 +4359,22 @@ int sqlite3PagerMovepage(Pager *pPager, ** page pgno before the 'move' operation, it needs to be retained ** for the page moved there. */ + pPg->needSync = 0; pPgOld = pager_lookup(pPager, pgno); if( pPgOld ){ assert( pPgOld->nRef==0 ); unlinkHashChain(pPager, pPgOld); makeClean(pPgOld); - if( pPgOld->needSync ){ - assert( pPgOld->inJournal ); - pPg->inJournal = 1; - pPg->needSync = 1; - assert( pPager->needSync ); - } + pPg->needSync = pPgOld->needSync; + }else{ + pPg->needSync = 0; } + if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){ + pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0; + }else{ + pPg->inJournal = 0; + assert( pPg->needSync==0 || (int)pgno>pPager->origDbSize ); + } /* Change the page number for pPg and insert it into the new hash-chain. */ assert( pgno!=0 ); ============================================================ --- sqlite/pager.h d652ddf092d2318d00e41f8539760fe8e57c157c +++ sqlite/pager.h 94110a5570dca30d54a883e880a3633b2e4c05ae @@ -13,39 +13,13 @@ ** subsystem. The page cache subsystem reads and writes a file a page ** at a time and provides a journal for rollback. ** -** @(#) $Id: pager.h,v 1.58 2007/04/13 02:14:30 drh Exp $ +** @(#) $Id: pager.h,v 1.61 2007/05/08 21:45:28 drh Exp $ */ #ifndef _PAGER_H_ #define _PAGER_H_ /* -** The default size of a database page. -*/ -#ifndef SQLITE_DEFAULT_PAGE_SIZE -# define SQLITE_DEFAULT_PAGE_SIZE 1024 -#endif - -/* Maximum page size. The upper bound on this value is 32768. This a limit -** imposed by necessity of storing the value in a 2-byte unsigned integer -** and the fact that the page size must be a power of 2. -** -** This value is used to initialize certain arrays on the stack at -** various places in the code. On embedded machines where stack space -** is limited and the flexibility of having large pages is not needed, -** it makes good sense to reduce the maximum page size to something more -** reasonable, like 1024. -*/ -#ifndef SQLITE_MAX_PAGE_SIZE -# define SQLITE_MAX_PAGE_SIZE 32768 -#endif - -/* -** Maximum number of pages in one database. -*/ -#define SQLITE_MAX_PAGE 1073741823 - -/* ** The type used to represent a page number. The first page in a file ** is called page 1. 0 is used to represent "not a page". */ @@ -86,6 +60,7 @@ int sqlite3PagerSetPagesize(Pager*, int) void sqlite3PagerSetDestructor(Pager*, void(*)(DbPage*,int)); void sqlite3PagerSetReiniter(Pager*, void(*)(DbPage*,int)); int sqlite3PagerSetPagesize(Pager*, int); +int sqlite3PagerMaxPageCount(Pager*, int); int sqlite3PagerReadFileheader(Pager*, int, unsigned char*); void sqlite3PagerSetCachesize(Pager*, int); int sqlite3PagerClose(Pager *pPager); @@ -94,9 +69,7 @@ int sqlite3PagerUnref(DbPage*); DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno); int sqlite3PagerRef(DbPage*); int sqlite3PagerUnref(DbPage*); -Pgno sqlite3PagerPagenumber(DbPage*); int sqlite3PagerWrite(DbPage*); -int sqlite3PagerIswriteable(DbPage*); int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void*); int sqlite3PagerPagecount(Pager*); int sqlite3PagerTruncate(Pager*,Pgno); @@ -111,29 +84,37 @@ int sqlite3PagerRefcount(Pager*); void sqlite3PagerDontRollback(DbPage*); void sqlite3PagerDontWrite(DbPage*); int sqlite3PagerRefcount(Pager*); -int *sqlite3PagerStats(Pager*); void sqlite3PagerSetSafetyLevel(Pager*,int,int); const char *sqlite3PagerFilename(Pager*); const char *sqlite3PagerDirname(Pager*); const char *sqlite3PagerJournalname(Pager*); int sqlite3PagerNosync(Pager*); -int sqlite3PagerRename(Pager*, const char *zNewName); -void sqlite3PagerSetCodec(Pager*,void*(*)(void*,void*,Pgno,int),void*); int sqlite3PagerMovepage(Pager*,DbPage*,Pgno); -int sqlite3PagerReset(Pager*); -int sqlite3PagerReleaseMemory(int); - void *sqlite3PagerGetData(DbPage *); void *sqlite3PagerGetExtra(DbPage *); int sqlite3PagerLockingMode(Pager *, int); +#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO) + int sqlite3PagerReleaseMemory(int); +#endif + +#ifdef SQLITE_HAS_CODEC + void sqlite3PagerSetCodec(Pager*,void*(*)(void*,void*,Pgno,int),void*); +#endif + +#if !defined(NDEBUG) || defined(SQLITE_TEST) + Pgno sqlite3PagerPagenumber(DbPage*); + int sqlite3PagerIswriteable(DbPage*); +#endif + #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) -int sqlite3PagerLockstate(Pager*); + int sqlite3PagerLockstate(Pager*); #endif #ifdef SQLITE_TEST -void sqlite3PagerRefdump(Pager*); -int pager3_refinfo_enable; + int *sqlite3PagerStats(Pager*); + void sqlite3PagerRefdump(Pager*); + int pager3_refinfo_enable; #endif #ifdef SQLITE_TEST ============================================================ --- sqlite/parse.c b29e3203362be291200999d2e91d01187473f551 +++ sqlite/parse.c 5fda95dfe0c8391ffc7826da7d5cea986bd66bee @@ -1282,7 +1282,7 @@ static void yy_destructor(YYCODETYPE yym case 222: case 223: case 235: -#line 623 "parse.y" +#line 627 "parse.y" {sqlite3ExprDelete((yypminor->yy172));} #line 1289 "parse.c" break; @@ -1297,7 +1297,7 @@ static void yy_destructor(YYCODETYPE yym case 212: case 215: case 221: -#line 863 "parse.y" +#line 876 "parse.y" {sqlite3ExprListDelete((yypminor->yy174));} #line 1304 "parse.c" break; @@ -1305,30 +1305,30 @@ static void yy_destructor(YYCODETYPE yym case 193: case 201: case 202: -#line 487 "parse.y" +#line 489 "parse.y" {sqlite3SrcListDelete((yypminor->yy373));} #line 1312 "parse.c" break; case 205: case 208: case 214: -#line 504 "parse.y" +#line 506 "parse.y" {sqlite3IdListDelete((yypminor->yy432));} #line 1319 "parse.c" break; case 231: case 236: -#line 964 "parse.y" +#line 979 "parse.y" {sqlite3DeleteTriggerStep((yypminor->yy243));} #line 1325 "parse.c" break; case 233: -#line 950 "parse.y" +#line 965 "parse.y" {sqlite3IdListDelete((yypminor->yy370).b);} #line 1330 "parse.c" break; case 238: -#line 1037 "parse.y" +#line 1052 "parse.y" {sqlite3ExprDelete((yypminor->yy386));} #line 1335 "parse.c" break; @@ -2329,142 +2329,144 @@ static void yy_reduce( if( yymsp[0].minor.yy219 ){ yymsp[0].minor.yy219->op = yymsp[-1].minor.yy46; yymsp[0].minor.yy219->pPrior = yymsp[-2].minor.yy219; + }else{ + sqlite3SelectDelete(yymsp[-2].minor.yy219); } yygotominor.yy219 = yymsp[0].minor.yy219; } -#line 2338 "parse.c" +#line 2340 "parse.c" break; case 108: -#line 389 "parse.y" +#line 391 "parse.y" {yygotominor.yy46 = TK_ALL;} -#line 2343 "parse.c" +#line 2345 "parse.c" break; case 110: -#line 393 "parse.y" +#line 395 "parse.y" { yygotominor.yy219 = sqlite3SelectNew(yymsp[-6].minor.yy174,yymsp[-5].minor.yy373,yymsp[-4].minor.yy172,yymsp[-3].minor.yy174,yymsp[-2].minor.yy172,yymsp[-1].minor.yy174,yymsp[-7].minor.yy46,yymsp[0].minor.yy234.pLimit,yymsp[0].minor.yy234.pOffset); } -#line 2350 "parse.c" +#line 2352 "parse.c" break; case 114: case 237: -#line 414 "parse.y" +#line 416 "parse.y" {yygotominor.yy174 = yymsp[-1].minor.yy174;} -#line 2356 "parse.c" +#line 2358 "parse.c" break; case 115: case 141: case 149: case 236: -#line 415 "parse.y" +#line 417 "parse.y" {yygotominor.yy174 = 0;} -#line 2364 "parse.c" +#line 2366 "parse.c" break; case 116: -#line 416 "parse.y" +#line 418 "parse.y" { yygotominor.yy174 = sqlite3ExprListAppend(yymsp[-2].minor.yy174,yymsp[-1].minor.yy172,yymsp[0].minor.yy410.n?&yymsp[0].minor.yy410:0); } -#line 2371 "parse.c" +#line 2373 "parse.c" break; case 117: -#line 419 "parse.y" +#line 421 "parse.y" { yygotominor.yy174 = sqlite3ExprListAppend(yymsp[-1].minor.yy174, sqlite3Expr(TK_ALL, 0, 0, 0), 0); } -#line 2378 "parse.c" +#line 2380 "parse.c" break; case 118: -#line 422 "parse.y" +#line 424 "parse.y" { Expr *pRight = sqlite3Expr(TK_ALL, 0, 0, 0); Expr *pLeft = sqlite3Expr(TK_ID, 0, 0, &yymsp[-2].minor.yy410); yygotominor.yy174 = sqlite3ExprListAppend(yymsp[-3].minor.yy174, sqlite3Expr(TK_DOT, pLeft, pRight, 0), 0); } -#line 2387 "parse.c" +#line 2389 "parse.c" break; case 121: -#line 434 "parse.y" +#line 436 "parse.y" {yygotominor.yy410.n = 0;} -#line 2392 "parse.c" +#line 2394 "parse.c" break; case 122: -#line 446 "parse.y" +#line 448 "parse.y" {yygotominor.yy373 = sqliteMalloc(sizeof(*yygotominor.yy373));} -#line 2397 "parse.c" +#line 2399 "parse.c" break; case 123: -#line 447 "parse.y" +#line 449 "parse.y" { yygotominor.yy373 = yymsp[0].minor.yy373; sqlite3SrcListShiftJoinType(yygotominor.yy373); } -#line 2405 "parse.c" +#line 2407 "parse.c" break; case 124: -#line 455 "parse.y" +#line 457 "parse.y" { yygotominor.yy373 = yymsp[-1].minor.yy373; if( yygotominor.yy373 && yygotominor.yy373->nSrc>0 ) yygotominor.yy373->a[yygotominor.yy373->nSrc-1].jointype = yymsp[0].minor.yy46; } -#line 2413 "parse.c" +#line 2415 "parse.c" break; case 125: -#line 459 "parse.y" +#line 461 "parse.y" {yygotominor.yy373 = 0;} -#line 2418 "parse.c" +#line 2420 "parse.c" break; case 126: -#line 460 "parse.y" +#line 462 "parse.y" { yygotominor.yy373 = sqlite3SrcListAppendFromTerm(yymsp[-5].minor.yy373,&yymsp[-4].minor.yy410,&yymsp[-3].minor.yy410,&yymsp[-2].minor.yy410,0,yymsp[-1].minor.yy172,yymsp[0].minor.yy432); } -#line 2425 "parse.c" +#line 2427 "parse.c" break; case 127: -#line 465 "parse.y" +#line 467 "parse.y" { yygotominor.yy373 = sqlite3SrcListAppendFromTerm(yymsp[-6].minor.yy373,0,0,&yymsp[-2].minor.yy410,yymsp[-4].minor.yy219,yymsp[-1].minor.yy172,yymsp[0].minor.yy432); } -#line 2432 "parse.c" +#line 2434 "parse.c" break; case 129: -#line 476 "parse.y" +#line 478 "parse.y" { sqlite3SrcListShiftJoinType(yymsp[0].minor.yy373); yygotominor.yy219 = sqlite3SelectNew(0,yymsp[0].minor.yy373,0,0,0,0,0,0,0); } -#line 2440 "parse.c" +#line 2442 "parse.c" break; case 130: -#line 483 "parse.y" +#line 485 "parse.y" {yygotominor.yy410.z=0; yygotominor.yy410.n=0;} -#line 2445 "parse.c" +#line 2447 "parse.c" break; case 132: -#line 488 "parse.y" +#line 490 "parse.y" {yygotominor.yy373 = sqlite3SrcListAppend(0,&yymsp[-1].minor.yy410,&yymsp[0].minor.yy410);} -#line 2450 "parse.c" +#line 2452 "parse.c" break; case 133: -#line 492 "parse.y" +#line 494 "parse.y" { yygotominor.yy46 = JT_INNER; } -#line 2455 "parse.c" +#line 2457 "parse.c" break; case 134: -#line 493 "parse.y" +#line 495 "parse.y" { yygotominor.yy46 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); } -#line 2460 "parse.c" +#line 2462 "parse.c" break; case 135: -#line 494 "parse.y" +#line 496 "parse.y" { yygotominor.yy46 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy410,0); } -#line 2465 "parse.c" +#line 2467 "parse.c" break; case 136: -#line 496 "parse.y" +#line 498 "parse.y" { yygotominor.yy46 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy410,&yymsp[-1].minor.yy410); } -#line 2470 "parse.c" +#line 2472 "parse.c" break; case 137: case 145: @@ -2475,9 +2477,9 @@ static void yy_reduce( case 225: case 227: case 231: -#line 500 "parse.y" +#line 502 "parse.y" {yygotominor.yy172 = yymsp[0].minor.yy172;} -#line 2483 "parse.c" +#line 2485 "parse.c" break; case 138: case 151: @@ -2486,161 +2488,164 @@ static void yy_reduce( case 226: case 228: case 232: -#line 501 "parse.y" +#line 503 "parse.y" {yygotominor.yy172 = 0;} -#line 2494 "parse.c" +#line 2496 "parse.c" break; case 139: case 171: -#line 505 "parse.y" +#line 507 "parse.y" {yygotominor.yy432 = yymsp[-1].minor.yy432;} -#line 2500 "parse.c" +#line 2502 "parse.c" break; case 140: case 170: -#line 506 "parse.y" +#line 508 "parse.y" {yygotominor.yy432 = 0;} -#line 2506 "parse.c" +#line 2508 "parse.c" break; case 142: case 150: -#line 517 "parse.y" +#line 519 "parse.y" {yygotominor.yy174 = yymsp[0].minor.yy174;} -#line 2512 "parse.c" +#line 2514 "parse.c" break; case 143: -#line 518 "parse.y" +#line 520 "parse.y" { yygotominor.yy174 = sqlite3ExprListAppend(yymsp[-3].minor.yy174,yymsp[-1].minor.yy172,0); if( yygotominor.yy174 ) yygotominor.yy174->a[yygotominor.yy174->nExpr-1].sortOrder = yymsp[0].minor.yy46; } -#line 2520 "parse.c" +#line 2522 "parse.c" break; case 144: -#line 522 "parse.y" +#line 524 "parse.y" { yygotominor.yy174 = sqlite3ExprListAppend(0,yymsp[-1].minor.yy172,0); if( yygotominor.yy174 && yygotominor.yy174->a ) yygotominor.yy174->a[0].sortOrder = yymsp[0].minor.yy46; } -#line 2528 "parse.c" +#line 2530 "parse.c" break; case 146: case 148: -#line 530 "parse.y" +#line 532 "parse.y" {yygotominor.yy46 = SQLITE_SO_ASC;} -#line 2534 "parse.c" +#line 2536 "parse.c" break; case 147: -#line 531 "parse.y" +#line 533 "parse.y" {yygotominor.yy46 = SQLITE_SO_DESC;} -#line 2539 "parse.c" +#line 2541 "parse.c" break; case 153: -#line 557 "parse.y" +#line 559 "parse.y" {yygotominor.yy234.pLimit = 0; yygotominor.yy234.pOffset = 0;} -#line 2544 "parse.c" +#line 2546 "parse.c" break; case 154: -#line 558 "parse.y" +#line 560 "parse.y" {yygotominor.yy234.pLimit = yymsp[0].minor.yy172; yygotominor.yy234.pOffset = 0;} -#line 2549 "parse.c" +#line 2551 "parse.c" break; case 155: -#line 560 "parse.y" +#line 562 "parse.y" {yygotominor.yy234.pLimit = yymsp[-2].minor.yy172; yygotominor.yy234.pOffset = yymsp[0].minor.yy172;} -#line 2554 "parse.c" +#line 2556 "parse.c" break; case 156: -#line 562 "parse.y" +#line 564 "parse.y" {yygotominor.yy234.pOffset = yymsp[-2].minor.yy172; yygotominor.yy234.pLimit = yymsp[0].minor.yy172;} -#line 2559 "parse.c" +#line 2561 "parse.c" break; case 157: -#line 566 "parse.y" +#line 568 "parse.y" {sqlite3DeleteFrom(pParse,yymsp[-1].minor.yy373,yymsp[0].minor.yy172);} -#line 2564 "parse.c" +#line 2566 "parse.c" break; case 160: -#line 577 "parse.y" -{sqlite3Update(pParse,yymsp[-3].minor.yy373,yymsp[-1].minor.yy174,yymsp[0].minor.yy172,yymsp[-4].minor.yy46);} -#line 2569 "parse.c" +#line 578 "parse.y" +{ + sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy174,SQLITE_MAX_COLUMN,"set list"); + sqlite3Update(pParse,yymsp[-3].minor.yy373,yymsp[-1].minor.yy174,yymsp[0].minor.yy172,yymsp[-4].minor.yy46); +} +#line 2574 "parse.c" break; case 161: -#line 583 "parse.y" +#line 587 "parse.y" {yygotominor.yy174 = sqlite3ExprListAppend(yymsp[-4].minor.yy174,yymsp[0].minor.yy172,&yymsp[-2].minor.yy410);} -#line 2574 "parse.c" +#line 2579 "parse.c" break; case 162: -#line 584 "parse.y" +#line 588 "parse.y" {yygotominor.yy174 = sqlite3ExprListAppend(0,yymsp[0].minor.yy172,&yymsp[-2].minor.yy410);} -#line 2579 "parse.c" +#line 2584 "parse.c" break; case 163: -#line 590 "parse.y" +#line 594 "parse.y" {sqlite3Insert(pParse, yymsp[-5].minor.yy373, yymsp[-1].minor.yy174, 0, yymsp[-4].minor.yy432, yymsp[-7].minor.yy46);} -#line 2584 "parse.c" +#line 2589 "parse.c" break; case 164: -#line 592 "parse.y" +#line 596 "parse.y" {sqlite3Insert(pParse, yymsp[-2].minor.yy373, 0, yymsp[0].minor.yy219, yymsp[-1].minor.yy432, yymsp[-4].minor.yy46);} -#line 2589 "parse.c" +#line 2594 "parse.c" break; case 165: -#line 594 "parse.y" +#line 598 "parse.y" {sqlite3Insert(pParse, yymsp[-3].minor.yy373, 0, 0, yymsp[-2].minor.yy432, yymsp[-5].minor.yy46);} -#line 2594 "parse.c" +#line 2599 "parse.c" break; case 168: case 229: -#line 604 "parse.y" +#line 608 "parse.y" {yygotominor.yy174 = sqlite3ExprListAppend(yymsp[-2].minor.yy174,yymsp[0].minor.yy172,0);} -#line 2600 "parse.c" +#line 2605 "parse.c" break; case 169: case 230: -#line 605 "parse.y" +#line 609 "parse.y" {yygotominor.yy174 = sqlite3ExprListAppend(0,yymsp[0].minor.yy172,0);} -#line 2606 "parse.c" +#line 2611 "parse.c" break; case 172: -#line 614 "parse.y" +#line 618 "parse.y" {yygotominor.yy432 = sqlite3IdListAppend(yymsp[-2].minor.yy432,&yymsp[0].minor.yy410);} -#line 2611 "parse.c" +#line 2616 "parse.c" break; case 173: -#line 615 "parse.y" +#line 619 "parse.y" {yygotominor.yy432 = sqlite3IdListAppend(0,&yymsp[0].minor.yy410);} -#line 2616 "parse.c" +#line 2621 "parse.c" break; case 175: -#line 626 "parse.y" +#line 630 "parse.y" {yygotominor.yy172 = yymsp[-1].minor.yy172; sqlite3ExprSpan(yygotominor.yy172,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); } -#line 2621 "parse.c" +#line 2626 "parse.c" break; case 176: case 181: case 182: -#line 627 "parse.y" +#line 631 "parse.y" {yygotominor.yy172 = sqlite3Expr(yymsp[0].major, 0, 0, &yymsp[0].minor.yy0);} -#line 2628 "parse.c" +#line 2633 "parse.c" break; case 177: case 178: -#line 628 "parse.y" +#line 632 "parse.y" {yygotominor.yy172 = sqlite3Expr(TK_ID, 0, 0, &yymsp[0].minor.yy0);} -#line 2634 "parse.c" +#line 2639 "parse.c" break; case 179: -#line 630 "parse.y" +#line 634 "parse.y" { Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &yymsp[-2].minor.yy410); Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &yymsp[0].minor.yy410); yygotominor.yy172 = sqlite3Expr(TK_DOT, temp1, temp2, 0); } -#line 2643 "parse.c" +#line 2648 "parse.c" break; case 180: -#line 635 "parse.y" +#line 639 "parse.y" { Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &yymsp[-4].minor.yy410); Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &yymsp[-2].minor.yy410); @@ -2648,58 +2653,61 @@ static void yy_reduce( Expr *temp4 = sqlite3Expr(TK_DOT, temp2, temp3, 0); yygotominor.yy172 = sqlite3Expr(TK_DOT, temp1, temp4, 0); } -#line 2654 "parse.c" +#line 2659 "parse.c" break; case 183: -#line 644 "parse.y" +#line 648 "parse.y" {yygotominor.yy172 = sqlite3RegisterExpr(pParse, &yymsp[0].minor.yy0);} -#line 2659 "parse.c" +#line 2664 "parse.c" break; case 184: -#line 645 "parse.y" +#line 649 "parse.y" { Token *pToken = &yymsp[0].minor.yy0; Expr *pExpr = yygotominor.yy172 = sqlite3Expr(TK_VARIABLE, 0, 0, pToken); sqlite3ExprAssignVarNumber(pParse, pExpr); } -#line 2668 "parse.c" +#line 2673 "parse.c" break; case 185: -#line 650 "parse.y" +#line 654 "parse.y" { yygotominor.yy172 = sqlite3ExprSetColl(pParse, yymsp[-2].minor.yy172, &yymsp[0].minor.yy410); } -#line 2675 "parse.c" +#line 2680 "parse.c" break; case 186: -#line 654 "parse.y" +#line 658 "parse.y" { yygotominor.yy172 = sqlite3Expr(TK_CAST, yymsp[-3].minor.yy172, 0, &yymsp[-1].minor.yy410); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0); } -#line 2683 "parse.c" +#line 2688 "parse.c" break; case 187: -#line 659 "parse.y" +#line 663 "parse.y" { + if( yymsp[-1].minor.yy174 && yymsp[-1].minor.yy174->nExpr>SQLITE_MAX_FUNCTION_ARG ){ + sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0); + } yygotominor.yy172 = sqlite3ExprFunction(yymsp[-1].minor.yy174, &yymsp[-4].minor.yy0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0); if( yymsp[-2].minor.yy46 && yygotominor.yy172 ){ yygotominor.yy172->flags |= EP_Distinct; } } -#line 2694 "parse.c" +#line 2702 "parse.c" break; case 188: -#line 666 "parse.y" +#line 673 "parse.y" { yygotominor.yy172 = sqlite3ExprFunction(0, &yymsp[-3].minor.yy0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0); } -#line 2702 "parse.c" +#line 2710 "parse.c" break; case 189: -#line 670 "parse.y" +#line 677 "parse.y" { /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are ** treated as functions that return constants */ @@ -2709,7 +2717,7 @@ static void yy_reduce( yygotominor.yy172->span = yymsp[0].minor.yy0; } } -#line 2715 "parse.c" +#line 2723 "parse.c" break; case 190: case 191: @@ -2719,24 +2727,24 @@ static void yy_reduce( case 195: case 196: case 197: -#line 679 "parse.y" +#line 686 "parse.y" {yygotominor.yy172 = sqlite3Expr(yymsp[-1].major, yymsp[-2].minor.yy172, yymsp[0].minor.yy172, 0);} -#line 2727 "parse.c" +#line 2735 "parse.c" break; case 198: case 200: -#line 689 "parse.y" +#line 696 "parse.y" {yygotominor.yy72.eOperator = yymsp[0].minor.yy0; yygotominor.yy72.not = 0;} -#line 2733 "parse.c" +#line 2741 "parse.c" break; case 199: case 201: -#line 690 "parse.y" +#line 697 "parse.y" {yygotominor.yy72.eOperator = yymsp[0].minor.yy0; yygotominor.yy72.not = 1;} -#line 2739 "parse.c" +#line 2747 "parse.c" break; case 204: -#line 697 "parse.y" +#line 704 "parse.y" { ExprList *pList; pList = sqlite3ExprListAppend(0, yymsp[-1].minor.yy172, 0); @@ -2749,66 +2757,66 @@ static void yy_reduce( sqlite3ExprSpan(yygotominor.yy172, &yymsp[-3].minor.yy172->span, &yymsp[-1].minor.yy172->span); if( yygotominor.yy172 ) yygotominor.yy172->flags |= EP_InfixFunc; } -#line 2755 "parse.c" +#line 2763 "parse.c" break; case 205: -#line 710 "parse.y" +#line 717 "parse.y" { yygotominor.yy172 = sqlite3Expr(yymsp[0].major, yymsp[-1].minor.yy172, 0, 0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-1].minor.yy172->span,&yymsp[0].minor.yy0); } -#line 2763 "parse.c" +#line 2771 "parse.c" break; case 206: -#line 714 "parse.y" +#line 721 "parse.y" { yygotominor.yy172 = sqlite3Expr(TK_ISNULL, yymsp[-2].minor.yy172, 0, 0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-2].minor.yy172->span,&yymsp[0].minor.yy0); } -#line 2771 "parse.c" +#line 2779 "parse.c" break; case 207: -#line 718 "parse.y" +#line 725 "parse.y" { yygotominor.yy172 = sqlite3Expr(TK_NOTNULL, yymsp[-2].minor.yy172, 0, 0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-2].minor.yy172->span,&yymsp[0].minor.yy0); } -#line 2779 "parse.c" +#line 2787 "parse.c" break; case 208: -#line 722 "parse.y" +#line 729 "parse.y" { yygotominor.yy172 = sqlite3Expr(TK_NOTNULL, yymsp[-3].minor.yy172, 0, 0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-3].minor.yy172->span,&yymsp[0].minor.yy0); } -#line 2787 "parse.c" +#line 2795 "parse.c" break; case 209: -#line 726 "parse.y" +#line 733 "parse.y" { yygotominor.yy172 = sqlite3Expr(yymsp[-1].major, yymsp[0].minor.yy172, 0, 0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy172->span); } -#line 2795 "parse.c" +#line 2803 "parse.c" break; case 210: -#line 730 "parse.y" +#line 737 "parse.y" { yygotominor.yy172 = sqlite3Expr(TK_UMINUS, yymsp[0].minor.yy172, 0, 0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy172->span); } -#line 2803 "parse.c" +#line 2811 "parse.c" break; case 211: -#line 734 "parse.y" +#line 741 "parse.y" { yygotominor.yy172 = sqlite3Expr(TK_UPLUS, yymsp[0].minor.yy172, 0, 0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy172->span); } -#line 2811 "parse.c" +#line 2819 "parse.c" break; case 214: -#line 741 "parse.y" +#line 748 "parse.y" { ExprList *pList = sqlite3ExprListAppend(0, yymsp[-2].minor.yy172, 0); pList = sqlite3ExprListAppend(pList, yymsp[0].minor.yy172, 0); @@ -2821,127 +2829,133 @@ static void yy_reduce( if( yymsp[-3].minor.yy46 ) yygotominor.yy172 = sqlite3Expr(TK_NOT, yygotominor.yy172, 0, 0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-4].minor.yy172->span,&yymsp[0].minor.yy172->span); } -#line 2827 "parse.c" +#line 2835 "parse.c" break; case 217: -#line 757 "parse.y" +#line 764 "parse.y" { yygotominor.yy172 = sqlite3Expr(TK_IN, yymsp[-4].minor.yy172, 0, 0); if( yygotominor.yy172 ){ yygotominor.yy172->pList = yymsp[-1].minor.yy174; + sqlite3ExprSetHeight(yygotominor.yy172); }else{ sqlite3ExprListDelete(yymsp[-1].minor.yy174); } if( yymsp[-3].minor.yy46 ) yygotominor.yy172 = sqlite3Expr(TK_NOT, yygotominor.yy172, 0, 0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-4].minor.yy172->span,&yymsp[0].minor.yy0); } -#line 2841 "parse.c" +#line 2850 "parse.c" break; case 218: -#line 767 "parse.y" +#line 775 "parse.y" { yygotominor.yy172 = sqlite3Expr(TK_SELECT, 0, 0, 0); if( yygotominor.yy172 ){ yygotominor.yy172->pSelect = yymsp[-1].minor.yy219; + sqlite3ExprSetHeight(yygotominor.yy172); }else{ sqlite3SelectDelete(yymsp[-1].minor.yy219); } sqlite3ExprSpan(yygotominor.yy172,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); } -#line 2854 "parse.c" +#line 2864 "parse.c" break; case 219: -#line 776 "parse.y" +#line 785 "parse.y" { yygotominor.yy172 = sqlite3Expr(TK_IN, yymsp[-4].minor.yy172, 0, 0); if( yygotominor.yy172 ){ yygotominor.yy172->pSelect = yymsp[-1].minor.yy219; + sqlite3ExprSetHeight(yygotominor.yy172); }else{ sqlite3SelectDelete(yymsp[-1].minor.yy219); } if( yymsp[-3].minor.yy46 ) yygotominor.yy172 = sqlite3Expr(TK_NOT, yygotominor.yy172, 0, 0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-4].minor.yy172->span,&yymsp[0].minor.yy0); } -#line 2868 "parse.c" +#line 2879 "parse.c" break; case 220: -#line 786 "parse.y" +#line 796 "parse.y" { SrcList *pSrc = sqlite3SrcListAppend(0,&yymsp[-1].minor.yy410,&yymsp[0].minor.yy410); yygotominor.yy172 = sqlite3Expr(TK_IN, yymsp[-3].minor.yy172, 0, 0); if( yygotominor.yy172 ){ yygotominor.yy172->pSelect = sqlite3SelectNew(0,pSrc,0,0,0,0,0,0,0); + sqlite3ExprSetHeight(yygotominor.yy172); }else{ sqlite3SrcListDelete(pSrc); } if( yymsp[-2].minor.yy46 ) yygotominor.yy172 = sqlite3Expr(TK_NOT, yygotominor.yy172, 0, 0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-3].minor.yy172->span,yymsp[0].minor.yy410.z?&yymsp[0].minor.yy410:&yymsp[-1].minor.yy410); } -#line 2883 "parse.c" +#line 2895 "parse.c" break; case 221: -#line 797 "parse.y" +#line 808 "parse.y" { Expr *p = yygotominor.yy172 = sqlite3Expr(TK_EXISTS, 0, 0, 0); if( p ){ p->pSelect = yymsp[-1].minor.yy219; sqlite3ExprSpan(p,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0); + sqlite3ExprSetHeight(yygotominor.yy172); }else{ sqlite3SelectDelete(yymsp[-1].minor.yy219); } } -#line 2896 "parse.c" +#line 2909 "parse.c" break; case 222: -#line 809 "parse.y" +#line 821 "parse.y" { yygotominor.yy172 = sqlite3Expr(TK_CASE, yymsp[-3].minor.yy172, yymsp[-1].minor.yy172, 0); if( yygotominor.yy172 ){ yygotominor.yy172->pList = yymsp[-2].minor.yy174; + sqlite3ExprSetHeight(yygotominor.yy172); }else{ sqlite3ExprListDelete(yymsp[-2].minor.yy174); } sqlite3ExprSpan(yygotominor.yy172, &yymsp[-4].minor.yy0, &yymsp[0].minor.yy0); } -#line 2909 "parse.c" +#line 2923 "parse.c" break; case 223: -#line 820 "parse.y" +#line 833 "parse.y" { yygotominor.yy174 = sqlite3ExprListAppend(yymsp[-4].minor.yy174, yymsp[-2].minor.yy172, 0); yygotominor.yy174 = sqlite3ExprListAppend(yygotominor.yy174, yymsp[0].minor.yy172, 0); } -#line 2917 "parse.c" +#line 2931 "parse.c" break; case 224: -#line 824 "parse.y" +#line 837 "parse.y" { yygotominor.yy174 = sqlite3ExprListAppend(0, yymsp[-2].minor.yy172, 0); yygotominor.yy174 = sqlite3ExprListAppend(yygotominor.yy174, yymsp[0].minor.yy172, 0); } -#line 2925 "parse.c" +#line 2939 "parse.c" break; case 233: -#line 851 "parse.y" +#line 864 "parse.y" { sqlite3CreateIndex(pParse, &yymsp[-6].minor.yy410, &yymsp[-5].minor.yy410, sqlite3SrcListAppend(0,&yymsp[-3].minor.yy410,0), yymsp[-1].minor.yy174, yymsp[-9].minor.yy46, &yymsp[-10].minor.yy0, &yymsp[0].minor.yy0, SQLITE_SO_ASC, yymsp[-7].minor.yy46); } -#line 2933 "parse.c" +#line 2947 "parse.c" break; case 234: case 281: -#line 857 "parse.y" +#line 870 "parse.y" {yygotominor.yy46 = OE_Abort;} -#line 2939 "parse.c" +#line 2953 "parse.c" break; case 235: -#line 858 "parse.y" +#line 871 "parse.y" {yygotominor.yy46 = OE_None;} -#line 2944 "parse.c" +#line 2958 "parse.c" break; case 238: -#line 868 "parse.y" +#line 881 "parse.y" { Expr *p = 0; if( yymsp[-1].minor.yy410.n>0 ){ @@ -2949,12 +2963,13 @@ static void yy_reduce( if( p ) p->pColl = sqlite3LocateCollSeq(pParse, (char*)yymsp[-1].minor.yy410.z, yymsp[-1].minor.yy410.n); } yygotominor.yy174 = sqlite3ExprListAppend(yymsp[-4].minor.yy174, p, &yymsp[-2].minor.yy410); + sqlite3ExprListCheckLength(pParse, yygotominor.yy174, SQLITE_MAX_COLUMN, "index"); if( yygotominor.yy174 ) yygotominor.yy174->a[yygotominor.yy174->nExpr-1].sortOrder = yymsp[0].minor.yy46; } -#line 2957 "parse.c" +#line 2972 "parse.c" break; case 239: -#line 877 "parse.y" +#line 891 "parse.y" { Expr *p = 0; if( yymsp[-1].minor.yy410.n>0 ){ @@ -2962,110 +2977,111 @@ static void yy_reduce( if( p ) p->pColl = sqlite3LocateCollSeq(pParse, (char*)yymsp[-1].minor.yy410.z, yymsp[-1].minor.yy410.n); } yygotominor.yy174 = sqlite3ExprListAppend(0, p, &yymsp[-2].minor.yy410); + sqlite3ExprListCheckLength(pParse, yygotominor.yy174, SQLITE_MAX_COLUMN, "index"); if( yygotominor.yy174 ) yygotominor.yy174->a[yygotominor.yy174->nExpr-1].sortOrder = yymsp[0].minor.yy46; } -#line 2970 "parse.c" +#line 2986 "parse.c" break; case 241: -#line 889 "parse.y" +#line 904 "parse.y" {yygotominor.yy410.z = 0; yygotominor.yy410.n = 0;} -#line 2975 "parse.c" +#line 2991 "parse.c" break; case 243: -#line 895 "parse.y" +#line 910 "parse.y" {sqlite3DropIndex(pParse, yymsp[0].minor.yy373, yymsp[-1].minor.yy46);} -#line 2980 "parse.c" +#line 2996 "parse.c" break; case 244: case 245: -#line 901 "parse.y" +#line 916 "parse.y" {sqlite3Vacuum(pParse);} -#line 2986 "parse.c" +#line 3002 "parse.c" break; case 246: -#line 909 "parse.y" +#line 924 "parse.y" {sqlite3Pragma(pParse,&yymsp[-3].minor.yy410,&yymsp[-2].minor.yy410,&yymsp[0].minor.yy410,0);} -#line 2991 "parse.c" +#line 3007 "parse.c" break; case 247: -#line 910 "parse.y" +#line 925 "parse.y" {sqlite3Pragma(pParse,&yymsp[-3].minor.yy410,&yymsp[-2].minor.yy410,&yymsp[0].minor.yy0,0);} -#line 2996 "parse.c" +#line 3012 "parse.c" break; case 248: -#line 911 "parse.y" +#line 926 "parse.y" { sqlite3Pragma(pParse,&yymsp[-3].minor.yy410,&yymsp[-2].minor.yy410,&yymsp[0].minor.yy410,1); } -#line 3003 "parse.c" +#line 3019 "parse.c" break; case 249: -#line 914 "parse.y" +#line 929 "parse.y" {sqlite3Pragma(pParse,&yymsp[-4].minor.yy410,&yymsp[-3].minor.yy410,&yymsp[-1].minor.yy410,0);} -#line 3008 "parse.c" +#line 3024 "parse.c" break; case 250: -#line 915 "parse.y" +#line 930 "parse.y" {sqlite3Pragma(pParse,&yymsp[-1].minor.yy410,&yymsp[0].minor.yy410,0,0);} -#line 3013 "parse.c" +#line 3029 "parse.c" break; case 258: -#line 929 "parse.y" +#line 944 "parse.y" { Token all; all.z = yymsp[-3].minor.yy410.z; all.n = (yymsp[0].minor.yy0.z - yymsp[-3].minor.yy410.z) + yymsp[0].minor.yy0.n; sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy243, &all); } -#line 3023 "parse.c" +#line 3039 "parse.c" break; case 259: -#line 938 "parse.y" +#line 953 "parse.y" { sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy410, &yymsp[-6].minor.yy410, yymsp[-5].minor.yy46, yymsp[-4].minor.yy370.a, yymsp[-4].minor.yy370.b, yymsp[-2].minor.yy373, yymsp[0].minor.yy172, yymsp[-10].minor.yy46, yymsp[-8].minor.yy46); yygotominor.yy410 = (yymsp[-6].minor.yy410.n==0?yymsp[-7].minor.yy410:yymsp[-6].minor.yy410); } -#line 3031 "parse.c" +#line 3047 "parse.c" break; case 260: case 263: -#line 944 "parse.y" +#line 959 "parse.y" { yygotominor.yy46 = TK_BEFORE; } -#line 3037 "parse.c" +#line 3053 "parse.c" break; case 261: -#line 945 "parse.y" +#line 960 "parse.y" { yygotominor.yy46 = TK_AFTER; } -#line 3042 "parse.c" +#line 3058 "parse.c" break; case 262: -#line 946 "parse.y" +#line 961 "parse.y" { yygotominor.yy46 = TK_INSTEAD;} -#line 3047 "parse.c" +#line 3063 "parse.c" break; case 264: case 265: -#line 951 "parse.y" +#line 966 "parse.y" {yygotominor.yy370.a = yymsp[0].major; yygotominor.yy370.b = 0;} -#line 3053 "parse.c" +#line 3069 "parse.c" break; case 266: -#line 953 "parse.y" +#line 968 "parse.y" {yygotominor.yy370.a = TK_UPDATE; yygotominor.yy370.b = yymsp[0].minor.yy432;} -#line 3058 "parse.c" +#line 3074 "parse.c" break; case 269: -#line 960 "parse.y" +#line 975 "parse.y" { yygotominor.yy172 = 0; } -#line 3063 "parse.c" +#line 3079 "parse.c" break; case 270: -#line 961 "parse.y" +#line 976 "parse.y" { yygotominor.yy172 = yymsp[0].minor.yy172; } -#line 3068 "parse.c" +#line 3084 "parse.c" break; case 271: -#line 965 "parse.y" +#line 980 "parse.y" { if( yymsp[-2].minor.yy243 ){ yymsp[-2].minor.yy243->pLast->pNext = yymsp[-1].minor.yy243; @@ -3075,40 +3091,40 @@ static void yy_reduce( yymsp[-2].minor.yy243->pLast = yymsp[-1].minor.yy243; yygotominor.yy243 = yymsp[-2].minor.yy243; } -#line 3081 "parse.c" +#line 3097 "parse.c" break; case 272: -#line 974 "parse.y" +#line 989 "parse.y" { yygotominor.yy243 = 0; } -#line 3086 "parse.c" +#line 3102 "parse.c" break; case 273: -#line 980 "parse.y" +#line 995 "parse.y" { yygotominor.yy243 = sqlite3TriggerUpdateStep(&yymsp[-3].minor.yy410, yymsp[-1].minor.yy174, yymsp[0].minor.yy172, yymsp[-4].minor.yy46); } -#line 3091 "parse.c" +#line 3107 "parse.c" break; case 274: -#line 985 "parse.y" +#line 1000 "parse.y" {yygotominor.yy243 = sqlite3TriggerInsertStep(&yymsp[-5].minor.yy410, yymsp[-4].minor.yy432, yymsp[-1].minor.yy174, 0, yymsp[-7].minor.yy46);} -#line 3096 "parse.c" +#line 3112 "parse.c" break; case 275: -#line 988 "parse.y" +#line 1003 "parse.y" {yygotominor.yy243 = sqlite3TriggerInsertStep(&yymsp[-2].minor.yy410, yymsp[-1].minor.yy432, 0, yymsp[0].minor.yy219, yymsp[-4].minor.yy46);} -#line 3101 "parse.c" +#line 3117 "parse.c" break; case 276: -#line 992 "parse.y" +#line 1007 "parse.y" {yygotominor.yy243 = sqlite3TriggerDeleteStep(&yymsp[-1].minor.yy410, yymsp[0].minor.yy172);} -#line 3106 "parse.c" +#line 3122 "parse.c" break; case 277: -#line 995 "parse.y" +#line 1010 "parse.y" {yygotominor.yy243 = sqlite3TriggerSelectStep(yymsp[0].minor.yy219); } -#line 3111 "parse.c" +#line 3127 "parse.c" break; case 278: -#line 998 "parse.y" +#line 1013 "parse.y" { yygotominor.yy172 = sqlite3Expr(TK_RAISE, 0, 0, 0); if( yygotominor.yy172 ){ @@ -3116,10 +3132,10 @@ static void yy_reduce( sqlite3ExprSpan(yygotominor.yy172, &yymsp[-3].minor.yy0, &yymsp[0].minor.yy0); } } -#line 3122 "parse.c" +#line 3138 "parse.c" break; case 279: -#line 1005 "parse.y" +#line 1020 "parse.y" { yygotominor.yy172 = sqlite3Expr(TK_RAISE, 0, 0, &yymsp[-1].minor.yy410); if( yygotominor.yy172 ) { @@ -3127,119 +3143,119 @@ static void yy_reduce( sqlite3ExprSpan(yygotominor.yy172, &yymsp[-5].minor.yy0, &yymsp[0].minor.yy0); } } -#line 3133 "parse.c" +#line 3149 "parse.c" break; case 280: -#line 1015 "parse.y" +#line 1030 "parse.y" {yygotominor.yy46 = OE_Rollback;} -#line 3138 "parse.c" +#line 3154 "parse.c" break; case 282: -#line 1017 "parse.y" +#line 1032 "parse.y" {yygotominor.yy46 = OE_Fail;} -#line 3143 "parse.c" +#line 3159 "parse.c" break; case 283: -#line 1022 "parse.y" +#line 1037 "parse.y" { sqlite3DropTrigger(pParse,yymsp[0].minor.yy373,yymsp[-1].minor.yy46); } -#line 3150 "parse.c" +#line 3166 "parse.c" break; case 284: -#line 1029 "parse.y" +#line 1044 "parse.y" { sqlite3Attach(pParse, yymsp[-3].minor.yy172, yymsp[-1].minor.yy172, yymsp[0].minor.yy386); } -#line 3157 "parse.c" +#line 3173 "parse.c" break; case 285: -#line 1032 "parse.y" +#line 1047 "parse.y" { sqlite3Detach(pParse, yymsp[0].minor.yy172); } -#line 3164 "parse.c" +#line 3180 "parse.c" break; case 286: -#line 1038 "parse.y" +#line 1053 "parse.y" { yygotominor.yy386 = 0; } -#line 3169 "parse.c" +#line 3185 "parse.c" break; case 287: -#line 1039 "parse.y" +#line 1054 "parse.y" { yygotominor.yy386 = yymsp[0].minor.yy172; } -#line 3174 "parse.c" +#line 3190 "parse.c" break; case 290: -#line 1047 "parse.y" +#line 1062 "parse.y" {sqlite3Reindex(pParse, 0, 0);} -#line 3179 "parse.c" +#line 3195 "parse.c" break; case 291: -#line 1048 "parse.y" +#line 1063 "parse.y" {sqlite3Reindex(pParse, &yymsp[-1].minor.yy410, &yymsp[0].minor.yy410);} -#line 3184 "parse.c" +#line 3200 "parse.c" break; case 292: -#line 1053 "parse.y" +#line 1068 "parse.y" {sqlite3Analyze(pParse, 0, 0);} -#line 3189 "parse.c" +#line 3205 "parse.c" break; case 293: -#line 1054 "parse.y" +#line 1069 "parse.y" {sqlite3Analyze(pParse, &yymsp[-1].minor.yy410, &yymsp[0].minor.yy410);} -#line 3194 "parse.c" +#line 3210 "parse.c" break; case 294: -#line 1059 "parse.y" +#line 1074 "parse.y" { sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy373,&yymsp[0].minor.yy410); } -#line 3201 "parse.c" +#line 3217 "parse.c" break; case 295: -#line 1062 "parse.y" +#line 1077 "parse.y" { sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy410); } -#line 3208 "parse.c" +#line 3224 "parse.c" break; case 296: -#line 1065 "parse.y" +#line 1080 "parse.y" { sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy373); } -#line 3215 "parse.c" +#line 3231 "parse.c" break; case 299: -#line 1074 "parse.y" +#line 1089 "parse.y" {sqlite3VtabFinishParse(pParse,0);} -#line 3220 "parse.c" +#line 3236 "parse.c" break; case 300: -#line 1075 "parse.y" +#line 1090 "parse.y" {sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);} -#line 3225 "parse.c" +#line 3241 "parse.c" break; case 301: -#line 1076 "parse.y" +#line 1091 "parse.y" { sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy410, &yymsp[-2].minor.yy410, &yymsp[0].minor.yy410); } -#line 3232 "parse.c" +#line 3248 "parse.c" break; case 304: -#line 1081 "parse.y" +#line 1096 "parse.y" {sqlite3VtabArgInit(pParse);} -#line 3237 "parse.c" +#line 3253 "parse.c" break; case 306: case 307: case 308: case 310: -#line 1083 "parse.y" +#line 1098 "parse.y" {sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);} -#line 3245 "parse.c" +#line 3261 "parse.c" break; }; yygoto = yyRuleInfo[yyruleno].lhs; @@ -3306,7 +3322,7 @@ static void yy_syntax_error( } pParse->parseError = 1; } -#line 3313 "parse.c" +#line 3329 "parse.c" sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */ } ============================================================ --- sqlite/pragma.c 3b992b5b2640d6ae25cef05aa6a42cd1d6c43234 +++ sqlite/pragma.c 0d25dad58bdfd6789943a10f1b9663c2eb85b96d @@ -11,7 +11,7 @@ ************************************************************************* ** This file contains code used to implement the PRAGMA command. ** -** $Id: pragma.c,v 1.132 2007/03/30 17:11:13 danielk1977 Exp $ +** $Id: pragma.c,v 1.139 2007/05/23 13:50:24 danielk1977 Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -73,6 +73,23 @@ static int getLockingMode(const char *z) return PAGER_LOCKINGMODE_QUERY; } +#ifndef SQLITE_OMIT_AUTOVACUUM +/* +** Interpret the given string as an auto-vacuum mode value. +** +** The following strings, "none", "full" and "incremental" are +** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively. +*/ +static int getAutoVacuum(const char *z){ + int i; + if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE; + if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL; + if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR; + i = atoi(z); + return ((i>=0&&i<=2)?i:0); +} +#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */ + #ifndef SQLITE_OMIT_PAGER_PRAGMAS /* ** Interpret the given string as a temp db location. Return 1 for file @@ -155,15 +172,17 @@ static int flagPragma(Parse *pParse, con const char *zName; /* Name of the pragma */ int mask; /* Mask for the db->flags value */ } aPragma[] = { - { "vdbe_trace", SQLITE_VdbeTrace }, - { "sql_trace", SQLITE_SqlTrace }, - { "vdbe_listing", SQLITE_VdbeListing }, { "full_column_names", SQLITE_FullColNames }, { "short_column_names", SQLITE_ShortColNames }, { "count_changes", SQLITE_CountRows }, { "empty_result_callbacks", SQLITE_NullCallback }, { "legacy_file_format", SQLITE_LegacyFileFmt }, { "fullfsync", SQLITE_FullFSync }, +#ifdef SQLITE_DEBUG + { "sql_trace", SQLITE_SqlTrace }, + { "vdbe_listing", SQLITE_VdbeListing }, + { "vdbe_trace", SQLITE_VdbeTrace }, +#endif #ifndef SQLITE_OMIT_CHECK { "ignore_check_constraints", SQLITE_IgnoreChecks }, #endif @@ -292,7 +311,7 @@ void sqlite3Pragma( sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", P3_STATIC); addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize); sqlite3VdbeChangeP1(v, addr, iDb); - sqlite3VdbeChangeP1(v, addr+5, MAX_PAGES); + sqlite3VdbeChangeP1(v, addr+5, SQLITE_DEFAULT_CACHE_SIZE); }else{ int size = atoi(zRight); if( size<0 ) size = -size; @@ -328,6 +347,27 @@ void sqlite3Pragma( }else /* + ** PRAGMA [database.]max_page_count + ** PRAGMA [database.]max_page_count=N + ** + ** The first form reports the current setting for the + ** maximum number of pages in the database file. The + ** second form attempts to change this setting. Both + ** forms return the current setting. + */ + if( sqlite3StrICmp(zLeft,"max_page_count")==0 ){ + Btree *pBt = pDb->pBt; + int newMax = 0; + if( zRight ){ + newMax = atoi(zRight); + } + if( pBt ){ + newMax = sqlite3BtreeMaxPageCount(pBt, newMax); + } + returnSingleInt(pParse, "max_page_count", newMax); + }else + + /* ** PRAGMA [database.]locking_mode ** PRAGMA [database.]locking_mode = (normal|exclusive) */ @@ -389,11 +429,38 @@ void sqlite3Pragma( pBt ? sqlite3BtreeGetAutoVacuum(pBt) : SQLITE_DEFAULT_AUTOVACUUM; returnSingleInt(pParse, "auto_vacuum", auto_vacuum); }else{ - sqlite3BtreeSetAutoVacuum(pBt, getBoolean(zRight)); + int eAuto = getAutoVacuum(zRight); + if( eAuto>=0 ){ + sqlite3BtreeSetAutoVacuum(pBt, eAuto); + } } }else #endif + /* + ** PRAGMA [database.]incremental_vacuum(N) + ** + ** Do N steps of incremental vacuuming on a database. + */ +#ifndef SQLITE_OMIT_AUTOVACUUM + if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){ + int iLimit, addr; + if( sqlite3ReadSchema(pParse) ){ + goto pragma_out; + } + if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){ + iLimit = 0x7fffffff; + } + sqlite3BeginWriteOperation(pParse, 0, iDb); + sqlite3VdbeAddOp(v, OP_MemInt, iLimit, 0); + addr = sqlite3VdbeAddOp(v, OP_IncrVacuum, iDb, 0); + sqlite3VdbeAddOp(v, OP_Callback, 0, 0); + sqlite3VdbeAddOp(v, OP_MemIncr, -1, 0); + sqlite3VdbeAddOp(v, OP_IfMemPos, 0, addr); + sqlite3VdbeJumpHere(v, addr); + }else +#endif + #ifndef SQLITE_OMIT_PAGER_PRAGMAS /* ** PRAGMA [database.]cache_size @@ -678,7 +745,6 @@ void sqlite3Pragma( #ifndef NDEBUG if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){ - extern void sqlite3ParserTrace(FILE*, char *); if( zRight ){ if( getBoolean(zRight) ){ sqlite3ParserTrace(stderr, "parser: "); ============================================================ --- sqlite/prepare.c 4cb9c9eb926e8baf5652ca4b4f2416f53f5b5370 +++ sqlite/prepare.c 87c23644986b5e41a58bc76f05abebd899e00089 @@ -13,7 +13,7 @@ ** interface, and routines that contribute to loading the database schema ** from disk. ** -** $Id: prepare.c,v 1.46 2007/04/19 11:09:01 danielk1977 Exp $ +** $Id: prepare.c,v 1.50 2007/05/08 20:37:39 drh Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -261,7 +261,7 @@ static int sqlite3InitOne(sqlite3 *db, i pDb->pSchema->enc = ENC(db); size = meta[2]; - if( size==0 ){ size = MAX_PAGES; } + if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; } pDb->pSchema->cache_size = size; sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); @@ -490,10 +490,16 @@ int sqlite3Prepare( memset(&sParse, 0, sizeof(sParse)); sParse.db = db; if( nBytes>=0 && zSql[nBytes]!=0 ){ - char *zSqlCopy = sqlite3StrNDup(zSql, nBytes); - sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg); - sParse.zTail += zSql - zSqlCopy; - sqliteFree(zSqlCopy); + char *zSqlCopy; + if( nBytes>SQLITE_MAX_SQL_LENGTH ){ + return SQLITE_TOOBIG; + } + zSqlCopy = sqlite3StrNDup(zSql, nBytes); + if( zSqlCopy ){ + sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg); + sqliteFree(zSqlCopy); + } + sParse.zTail = &zSql[nBytes]; }else{ sqlite3RunParser(&sParse, zSql, &zErrMsg); } @@ -643,7 +649,7 @@ static int sqlite3Prepare16( if( sqlite3SafetyCheck(db) ){ return SQLITE_MISUSE; } - zSql8 = sqlite3utf16to8(zSql, nBytes); + zSql8 = sqlite3Utf16to8(zSql, nBytes); if( zSql8 ){ rc = sqlite3Prepare(db, zSql8, -1, saveSqlFlag, ppStmt, &zTail8); } @@ -654,8 +660,8 @@ static int sqlite3Prepare16( ** characters between zSql8 and zTail8, and then returning a pointer ** the same number of characters into the UTF-16 string. */ - int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8); - *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed); + int chars_parsed = sqlite3Utf8CharLen(zSql8, zTail8-zSql8); + *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed); } sqliteFree(zSql8); return sqlite3ApiExit(db, rc); ============================================================ --- sqlite/printf.c 0c6f40648770831341ac45ab32423a80b4c87f05 +++ sqlite/printf.c 711908d6ff3385f9882df8ff405b9e0a2a4d04df @@ -51,6 +51,7 @@ ** */ #include "sqliteInt.h" +#include /* ** Conversion types fall into various categories as defined by the @@ -453,6 +454,11 @@ static int vxprintf( if( xtype==etFLOAT ) realvalue += rounder; /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ exp = 0; + if( isnan(realvalue) ){ + bufpt = "NaN"; + length = 3; + break; + } if( realvalue>0.0 ){ while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; } while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; } @@ -460,8 +466,14 @@ static int vxprintf( while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; } while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; } if( exp>350 || exp<-350 ){ - bufpt = "NaN"; - length = 3; + if( prefix=='-' ){ + bufpt = "-Inf"; + }else if( prefix=='+' ){ + bufpt = "+Inf"; + }else{ + bufpt = "Inf"; + } + length = strlen(bufpt); break; } } @@ -717,19 +729,22 @@ static void mout(void *arg, const char * if( pM->xRealloc==0 ){ nNewChar = pM->nAlloc - pM->nChar - 1; }else{ - pM->nAlloc = pM->nChar + nNewChar*2 + 1; + int nAlloc = pM->nChar + nNewChar*2 + 1; if( pM->zText==pM->zBase ){ - pM->zText = pM->xRealloc(0, pM->nAlloc); + pM->zText = pM->xRealloc(0, nAlloc); if( pM->zText && pM->nChar ){ memcpy(pM->zText, pM->zBase, pM->nChar); } }else{ char *zNew; - zNew = pM->xRealloc(pM->zText, pM->nAlloc); + zNew = pM->xRealloc(pM->zText, nAlloc); if( zNew ){ pM->zText = zNew; + }else{ + return; } } + pM->nAlloc = nAlloc; } } if( pM->zText ){ @@ -837,13 +852,17 @@ char *sqlite3_snprintf(int n, char *zBuf char *z; va_list ap; + if( n<=0 ){ + return zBuf; + } + zBuf[0] = 0; va_start(ap,zFormat); z = base_vprintf(0, 0, zBuf, n, zFormat, ap); va_end(ap); return z; } -#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) +#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) || defined(SQLITE_MEMDEBUG) /* ** A version of printf() that understands %lld. Used for debugging. ** The printf() built into some versions of windows does not understand %lld ============================================================ --- sqlite/select.c b914abca0ba28893e7fb7c7fb97a05e240e2ce8b +++ sqlite/select.c 33a258fc9c9dccb28ae2d3a02f1e1148d6433148 @@ -12,7 +12,7 @@ ** This file contains C code routines that are called by the parser ** to handle SELECT statements in SQLite. ** -** $Id: select.c,v 1.338 2007/04/16 17:07:55 drh Exp $ +** $Id: select.c,v 1.351 2007/06/15 15:31:50 drh Exp $ */ #include "sqliteInt.h" @@ -434,6 +434,21 @@ static void codeDistinct( sqlite3VdbeAddOp(v, OP_IdxInsert, iTab, 0); } +/* +** Generate an error message when a SELECT is used within a subexpression +** (example: "a IN (SELECT * FROM table)") but it has more than 1 result +** column. We do this in a subroutine because the error occurs in multiple +** places. +*/ +static int checkForMultiColumnSelectError(Parse *pParse, int eDest, int nExpr){ + if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){ + sqlite3ErrorMsg(pParse, "only a single result allowed for " + "a SELECT that is part of an expression"); + return 1; + }else{ + return 0; + } +} /* ** This routine generates the code for the inside of the inner loop @@ -497,6 +512,10 @@ static int selectInnerLoop( } } + if( checkForMultiColumnSelectError(pParse, eDest, pEList->nExpr) ){ + return 0; + } + switch( eDest ){ /* In this mode, write each query result to the key of the temporary ** table iParm. @@ -803,12 +822,6 @@ static const char *columnType( int j; if( pExpr==0 || pNC->pSrcList==0 ) return 0; - /* The TK_AS operator can only occur in ORDER BY, GROUP BY, HAVING, - ** and LIMIT clauses. But pExpr originates in the result set of a - ** SELECT. So pExpr can never contain an AS operator. - */ - assert( pExpr->op!=TK_AS ); - switch( pExpr->op ){ case TK_AGG_COLUMN: case TK_COLUMN: { @@ -1012,7 +1025,7 @@ static void generateColumnNames( }else{ char zName[30]; assert( p->op!=TK_COLUMN || pTabList==0 ); - sprintf(zName, "column%d", i+1); + sqlite3_snprintf(sizeof(zName), zName, "column%d", i+1); sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, 0); } } @@ -1359,6 +1372,10 @@ static int prepSelectStmt(Parse *pParse, sqlite3ExprListDelete(pEList); p->pEList = pNew; } + if( p->pEList && p->pEList->nExpr>SQLITE_MAX_COLUMN ){ + sqlite3ErrorMsg(pParse, "too many columns in result set"); + rc = SQLITE_ERROR; + } return rc; } @@ -1503,9 +1520,10 @@ static void computeLimitRegisters(Parse if( v==0 ) return; sqlite3ExprCode(pParse, p->pLimit); sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); - sqlite3VdbeAddOp(v, OP_MemStore, iLimit, 0); + sqlite3VdbeAddOp(v, OP_MemStore, iLimit, 1); VdbeComment((v, "# LIMIT counter")); sqlite3VdbeAddOp(v, OP_IfMemZero, iLimit, iBreak); + sqlite3VdbeAddOp(v, OP_MemLoad, iLimit, 0); } if( p->pOffset ){ p->iOffset = iOffset = pParse->nMem++; @@ -1753,6 +1771,9 @@ static int multiSelect( pOffset = p->pOffset; p->pOffset = 0; rc = sqlite3Select(pParse, p, op, unionTab, 0, 0, 0, aff); + /* Query flattening in sqlite3Select() might refill p->pOrderBy. + ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */ + sqlite3ExprListDelete(p->pOrderBy); p->pPrior = pPrior; p->pOrderBy = pOrderBy; sqlite3ExprDelete(p->pLimit); @@ -1909,8 +1930,8 @@ static int multiSelect( KeyInfo *pKeyInfo; /* Collating sequence for the result set */ Select *pLoop; /* For looping through SELECT statements */ int nKeyCol; /* Number of entries in pKeyInfo->aCol[] */ - CollSeq **apColl; - CollSeq **aCopy; + CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */ + CollSeq **aCopy; /* A copy of pKeyInfo->aColl[] */ assert( p->pRightmost==p ); nKeyCol = nCol + (pOrderBy ? pOrderBy->nExpr : 0); @@ -1951,9 +1972,23 @@ static int multiSelect( int addr; u8 *pSortOrder; + /* Reuse the same pKeyInfo for the ORDER BY as was used above for + ** the compound select statements. Except we have to change out the + ** pKeyInfo->aColl[] values. Some of the aColl[] values will be + ** reused when constructing the pKeyInfo for the ORDER BY, so make + ** a copy. Sufficient space to hold both the nCol entries for + ** the compound select and the nOrderbyExpr entries for the ORDER BY + ** was allocated above. But we need to move the compound select + ** entries out of the way before constructing the ORDER BY entries. + ** Move the compound select entries into aCopy[] where they can be + ** accessed and reused when constructing the ORDER BY entries. + ** Because nCol might be greater than or less than nOrderByExpr + ** we have to use memmove() when doing the copy. + */ aCopy = &pKeyInfo->aColl[nOrderByExpr]; pSortOrder = pKeyInfo->aSortOrder = (u8*)&aCopy[nCol]; - memcpy(aCopy, pKeyInfo->aColl, nCol*sizeof(CollSeq*)); + memmove(aCopy, pKeyInfo->aColl, nCol*sizeof(CollSeq*)); + apColl = pKeyInfo->aColl; for(i=0; ipExpr; @@ -1968,7 +2003,7 @@ static int multiSelect( assert( p->pRightmost==p ); assert( p->addrOpenEphm[2]>=0 ); addr = p->addrOpenEphm[2]; - sqlite3VdbeChangeP2(v, addr, p->pEList->nExpr+2); + sqlite3VdbeChangeP2(v, addr, p->pOrderBy->nExpr+2); pKeyInfo->nField = nOrderByExpr; sqlite3VdbeChangeP3(v, addr, (char*)pKeyInfo, P3_KEYINFO_HANDOFF); pKeyInfo = 0; @@ -2047,6 +2082,7 @@ static void substSelect(Select *p, int i substExprList(p->pOrderBy, iTable, pEList); substExpr(p->pHaving, iTable, pEList); substExpr(p->pWhere, iTable, pEList); + substSelect(p->pPrior, iTable, pEList); } #endif /* !defined(SQLITE_OMIT_VIEW) */ @@ -2114,6 +2150,10 @@ static void substSelect(Select *p, int i ** ** (14) The subquery does not use OFFSET ** +** (15) The outer query is not part of a compound select or the +** subquery does not have both an ORDER BY and a LIMIT clause. +** (See ticket #2339) +** ** In this routine, the "p" parameter is a pointer to the outer query. ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates. @@ -2158,6 +2198,9 @@ static int flattenSubquery( ** and (14). */ if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */ if( pSub->pOffset ) return 0; /* Restriction (14) */ + if( p->pRightmost && pSub->pLimit && pSub->pOrderBy ){ + return 0; /* Restriction (15) */ + } if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */ if( (pSub->isDistinct || pSub->pLimit) && (pSrc->nSrc>1 || isAgg) ){ /* Restrictions (4)(5)(8)(9) */ @@ -2479,6 +2522,10 @@ static int processOrderGroupBy( assert( pEList ); if( pOrderBy==0 ) return 0; + if( pOrderBy->nExpr>SQLITE_MAX_COLUMN ){ + sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType); + return 1; + } for(i=0; inExpr; i++){ int iCol; Expr *pE = pOrderBy->a[i].pExpr; @@ -2606,6 +2653,10 @@ int sqlite3SelectResolve( } } + if( sqlite3MallocFailed() ){ + return SQLITE_NOMEM; + } + /* Make sure the GROUP BY clause does not contain aggregate functions. */ if( pGroupBy ){ @@ -2822,9 +2873,14 @@ int sqlite3Select( if( p->pPrior ){ if( p->pRightmost==0 ){ Select *pLoop; - for(pLoop=p; pLoop; pLoop=pLoop->pPrior){ + int cnt = 0; + for(pLoop=p; pLoop; pLoop=pLoop->pPrior, cnt++){ pLoop->pRightmost = p; } + if( SQLITE_MAX_COMPOUND_SELECT>0 && cnt>SQLITE_MAX_COMPOUND_SELECT ){ + sqlite3ErrorMsg(pParse, "too many terms in compound SELECT"); + return 1; + } } return multiSelect(pParse, p, eDest, iParm, aff); } @@ -2860,9 +2916,7 @@ int sqlite3Select( ** only a single column may be output. */ #ifndef SQLITE_OMIT_SUBQUERY - if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){ - sqlite3ErrorMsg(pParse, "only a single result allowed for " - "a SELECT that is part of an expression"); + if( checkForMultiColumnSelectError(pParse, eDest, pEList->nExpr) ){ goto select_end; } #endif @@ -2894,8 +2948,21 @@ int sqlite3Select( }else{ needRestoreContext = 0; } +#if SQLITE_MAX_EXPR_DEPTH>0 + /* Increment Parse.nHeight by the height of the largest expression + ** tree refered to by this, the parent select. The child select + ** may contain expression trees of at most + ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit + ** more conservative than necessary, but much easier than enforcing + ** an exact limit. + */ + pParse->nHeight += sqlite3SelectExprHeight(p); +#endif sqlite3Select(pParse, pItem->pSelect, SRT_EphemTab, pItem->iCursor, p, i, &isAgg, 0); +#if SQLITE_MAX_EXPR_DEPTH>0 + pParse->nHeight -= sqlite3SelectExprHeight(p); +#endif if( needRestoreContext ){ pParse->zAuthContext = zSavedAuthContext; } ============================================================ --- sqlite/sqlite3.h 6dcafd84bc51a2510ec2d8ef94f024d36b0e1a00 +++ sqlite/sqlite3.h 2d972843072bbc136f0b0e5398e7a7aeedee9f87 @@ -10,9 +10,27 @@ ** ************************************************************************* ** This header file defines the interface that the SQLite library -** presents to client programs. +** presents to client programs. If a C-function, structure, datatype, +** or constant definition does not appear in this file, then it is +** not a published API of SQLite, is subject to change without +** notice, and should not be referenced by programs that use SQLite. ** -** @(#) $Id: sqlite.h.in,v 1.201 2007/03/30 20:43:42 drh Exp $ +** Some of the definitions that are in this file are marked as +** "experimental". Experimental interfaces are normally new +** features recently added to SQLite. We do not anticipate changes +** to experimental interfaces but reserve to make minor changes if +** experience from use "in the wild" suggest such changes are prudent. +** +** The official C-language API documentation for SQLite is derived +** from comments in this file. This file is the authoritative source +** on how SQLite interfaces are suppose to operate. +** +** The name of this file under configuration management is "sqlite.h.in". +** The makefile makes some minor changes to this file (such as inserting +** the version number) and changes its name to "sqlite3.h" as +** part of the build process. +** +** @(#) $Id: sqlite.h.in,v 1.212 2007/06/14 20:57:19 drh Exp $ */ #ifndef _SQLITE3_H_ #define _SQLITE3_H_ @@ -26,57 +44,88 @@ extern "C" { #endif /* -** The version of the SQLite library. +** Make sure these symbols where not defined by some previous header +** file. */ #ifdef SQLITE_VERSION # undef SQLITE_VERSION #endif -#define SQLITE_VERSION "3.3.17" +#ifdef SQLITE_VERSION_NUMBER +# undef SQLITE_VERSION_NUMBER +#endif /* -** The format of the version string is "X.Y.Z", where +** CAPI3REF: Compile-Time Library Version Numbers +** +** The version of the SQLite library is contained in the sqlite3.h +** header file in a #define named SQLITE_VERSION. The SQLITE_VERSION +** macro resolves to a string constant. +** +** The format of the version string is "X.Y.Z", where ** X is the major version number, Y is the minor version number and Z -** is the release number. The trailing string is often "alpha" or "beta". +** is the release number. The X.Y.Z might be followed by "alpha" or "beta". ** For example "3.1.1beta". ** +** The X value is always 3 in SQLite. The X value only changes when +** backwards compatibility is broken and we intend to never break +** backwards compatibility. The Y value only changes when +** there are major feature enhancements that are forwards compatible +** but not backwards compatible. The Z value is incremented with +** each release but resets back to 0 when Y is incremented. +** ** The SQLITE_VERSION_NUMBER is an integer with the value -** (X*100000 + Y*1000 + Z). For example, for version "3.1.1beta", +** (X*1000000 + Y*1000 + Z). For example, for version "3.1.1beta", ** SQLITE_VERSION_NUMBER is set to 3001001. To detect if they are using ** version 3.1.1 or greater at compile time, programs may use the test ** (SQLITE_VERSION_NUMBER>=3001001). +** +** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()]. */ -#ifdef SQLITE_VERSION_NUMBER -# undef SQLITE_VERSION_NUMBER -#endif -#define SQLITE_VERSION_NUMBER 3003017 +#define SQLITE_VERSION "3.4.0" +#define SQLITE_VERSION_NUMBER 3004000 /* -** The version string is also compiled into the library so that a program -** can check to make sure that the lib*.a file and the *.h file are from -** the same version. The sqlite3_libversion() function returns a pointer -** to the sqlite3_version variable - useful in DLLs which cannot access -** global variables. +** CAPI3REF: Run-Time Library Version Numbers +** +** These routines return values equivalent to the header constants +** [SQLITE_VERSION] and [SQLITE_VERSION_NUMBER]. The values returned +** by this routines should only be different from the header values +** if you compile your program using an sqlite3.h header from a +** different version of SQLite that the version of the library you +** link against. +** +** The sqlite3_version[] string constant contains the text of the +** [SQLITE_VERSION] string. The sqlite3_libversion() function returns +** a poiner to the sqlite3_version[] string constant. The function +** is provided for DLL users who can only access functions and not +** constants within the DLL. */ extern const char sqlite3_version[]; const char *sqlite3_libversion(void); - -/* -** Return the value of the SQLITE_VERSION_NUMBER macro when the -** library was compiled. -*/ int sqlite3_libversion_number(void); /* -** Each open sqlite database is represented by an instance of the -** following opaque structure. +** CAPI3REF: Database Connection Handle +** +** Each open SQLite database is represented by pointer to an instance of the +** opaque structure named "sqlite3". It is useful to think of an sqlite3 +** pointer as an object. The [sqlite3_open] interface is its constructor +** and [sqlite3_close] is its destructor. There are many other interfaces +** (such as [sqlite3_prepare_v2], [sqlite3_create_function], and +** [sqlite3_busy_timeout] to name but three) that are methods on this +** object. */ typedef struct sqlite3 sqlite3; /* +** CAPI3REF: 64-Bit Integer Types +** ** Some compilers do not support the "long long" datatype. So we have -** to do a typedef that for 64-bit integers that depends on what compiler -** is being used. +** to do compiler-specific typedefs for 64-bit signed and unsigned integers. +** +** Many SQLite interface functions require a 64-bit integer arguments. +** Those interfaces are declared using this typedef. */ #ifdef SQLITE_INT64_TYPE typedef SQLITE_INT64_TYPE sqlite_int64; @@ -98,26 +147,35 @@ typedef struct sqlite3 sqlite3; #endif /* -** A function to close the database. +** CAPI3REF: Closing A Database Connection ** ** Call this function with a pointer to a structure that was previously -** returned from sqlite3_open() and the corresponding database will by closed. +** returned from [sqlite3_open()] and the corresponding database will by +** closed. ** -** All SQL statements prepared using sqlite3_prepare() or -** sqlite3_prepare16() must be deallocated using sqlite3_finalize() before -** this routine is called. Otherwise, SQLITE_BUSY is returned and the +** All SQL statements prepared using [sqlite3_prepare_v2()] or +** [sqlite3_prepare16_v2()] must be destroyed using [sqlite3_finalize()] +** before this routine is called. Otherwise, SQLITE_BUSY is returned and the ** database connection remains open. */ int sqlite3_close(sqlite3 *); /* ** The type for a callback function. +** This is legacy and deprecated. It is included for historical +** compatibility and is not documented. */ typedef int (*sqlite3_callback)(void*,int,char**, char**); /* -** A function to executes one or more statements of SQL. +** CAPI3REF: One-Step Query Execution Interface ** +** This interface is used to do a one-time evaluatation of zero +** or more SQL statements. UTF-8 text of the SQL statements to +** be evaluted is passed in as the second parameter. The statements +** are prepared one by one using [sqlite3_prepare()], evaluated +** using [sqlite3_step()], then destroyed using [sqlite3_finalize()]. +** ** If one or more of the SQL statements are queries, then ** the callback function specified by the 3rd parameter is ** invoked once for each row of the query result. This callback @@ -125,13 +183,15 @@ typedef int (*sqlite3_callback)(void*,in ** value then the query is aborted, all subsequent SQL statements ** are skipped and the sqlite3_exec() function returns the SQLITE_ABORT. ** -** The 1st parameter is an arbitrary pointer that is passed -** to the callback function as its first parameter. +** The 4th parameter to this interface is an arbitrary pointer that is +** passed through to the callback function as its first parameter. ** ** The 2nd parameter to the callback function is the number of ** columns in the query result. The 3rd parameter to the callback -** is an array of strings holding the values for each column. -** The 4th parameter to the callback is an array of strings holding +** is an array of strings holding the values for each column +** as extracted using [sqlite3_column_text()]. +** The 4th parameter to the callback is an array of strings +** obtained using [sqlite3_column_name()] and holding ** the names of each column. ** ** The callback function may be NULL, even for queries. A NULL @@ -140,31 +200,39 @@ typedef int (*sqlite3_callback)(void*,in ** ** If an error occurs while parsing or evaluating the SQL (but ** not while executing the callback) then an appropriate error -** message is written into memory obtained from malloc() and +** message is written into memory obtained from [sqlite3_malloc()] and ** *errmsg is made to point to that message. The calling function ** is responsible for freeing the memory that holds the error -** message. Use sqlite3_free() for this. If errmsg==NULL, +** message. Use [sqlite3_free()] for this. If errmsg==NULL, ** then no error message is ever written. ** ** The return value is is SQLITE_OK if there are no errors and -** some other return code if there is an error. The particular -** return value depends on the type of error. +** some other [SQLITE_OK | return code] if there is an error. +** The particular return value depends on the type of error. ** -** If the query could not be executed because a database file is -** locked or busy, then this function returns SQLITE_BUSY. (This -** behavior can be modified somewhat using the sqlite3_busy_handler() -** and sqlite3_busy_timeout() functions below.) */ int sqlite3_exec( - sqlite3*, /* An open database */ - const char *sql, /* SQL to be executed */ - sqlite3_callback, /* Callback function */ - void *, /* 1st argument to callback function */ - char **errmsg /* Error msg written here */ + sqlite3*, /* An open database */ + const char *sql, /* SQL to be evaluted */ + int (*callback)(void*,int,char**,char**), /* Callback function */ + void *, /* 1st argument to callback */ + char **errmsg /* Error msg written here */ ); /* -** Return values for sqlite3_exec() and sqlite3_step() +** CAPI3REF: Result Codes +** KEYWORDS: SQLITE_OK +** +** Many SQLite functions return an integer result code from the set shown +** above in order to indicates success or failure. +** +** The result codes above are the only ones returned by SQLite in its +** default configuration. However, the [sqlite3_extended_result_codes()] +** API can be used to set a database connectoin to return more detailed +** result codes. +** +** See also: [SQLITE_IOERR_READ | extended result codes] +** */ #define SQLITE_OK 0 /* Successful result */ /* beginning-of-error-codes */ @@ -185,7 +253,7 @@ int sqlite3_exec( #define SQLITE_PROTOCOL 15 /* NOT USED. Database lock protocol error */ #define SQLITE_EMPTY 16 /* Database is empty */ #define SQLITE_SCHEMA 17 /* The database schema changed */ -#define SQLITE_TOOBIG 18 /* NOT USED. Too much data for one row */ +#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */ #define SQLITE_CONSTRAINT 19 /* Abort due to contraint violation */ #define SQLITE_MISMATCH 20 /* Data type mismatch */ #define SQLITE_MISUSE 21 /* Library used incorrectly */ @@ -199,27 +267,31 @@ int sqlite3_exec( /* end-of-error-codes */ /* -** Using the sqlite3_extended_result_codes() API, you can cause -** SQLite to return result codes with additional information in -** their upper bits. The lower 8 bits will be the same as the -** primary result codes above. But the upper bits might contain -** more specific error information. +** CAPI3REF: Extended Result Codes ** -** To extract the primary result code from an extended result code, -** simply mask off the lower 8 bits. +** In its default configuration, SQLite API routines return one of 26 integer +** result codes described at result-codes. However, experience has shown that +** many of these result codes are too course-grained. They do not provide as +** much information about problems as users might like. In an effort to +** address this, newer versions of SQLite (version 3.3.8 and later) include +** support for additional result codes that provide more detailed information +** about errors. The extended result codes are enabled (or disabled) for +** each database +** connection using the [sqlite3_extended_result_codes()] API. +** +** Some of the available extended result codes are listed above. +** We expect the number of extended result codes will be expand +** over time. Software that uses extended result codes should expect +** to see new result codes in future releases of SQLite. +** +** The symbolic name for an extended result code always contains a related +** primary result code as a prefix. Primary result codes contain a single +** "_" character. Extended result codes contain two or more "_" characters. +** The numeric value of an extended result code can be converted to its +** corresponding primary result code by masking off the lower 8 bytes. ** -** primary = extended & 0xff; -** -** New result error codes may be added from time to time. Software -** that uses the extended result codes should plan accordingly and be -** sure to always handle new unknown codes gracefully. -** ** The SQLITE_OK result code will never be extended. It will always ** be exactly zero. -** -** The extended result codes always have the primary result code -** as a prefix. Primary result codes only contain a single "_" -** character. Extended result codes contain two or more "_" characters. */ #define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8)) #define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8)) @@ -231,28 +303,59 @@ int sqlite3_exec( #define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8)) #define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8)) #define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8)) +#define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8)) /* -** Enable or disable the extended result codes. +** CAPI3REF: Enable Or Disable Extended Result Codes +** +** This routine enables or disables the +** [SQLITE_IOERR_READ | extended result codes] feature. +** By default, SQLite API routines return one of only 26 integer +** [SQLITE_OK | result codes]. When extended result codes +** are enabled by this routine, the repetoire of result codes can be +** much larger and can (hopefully) provide more detailed information +** about the cause of an error. +** +** The second argument is a boolean value that turns extended result +** codes on and off. Extended result codes are off by default for +** backwards compatibility with older versions of SQLite. */ int sqlite3_extended_result_codes(sqlite3*, int onoff); /* -** Each entry in an SQLite table has a unique integer key. (The key is -** the value of the INTEGER PRIMARY KEY column if there is such a column, -** otherwise the key is generated automatically. The unique key is always -** available as the ROWID, OID, or _ROWID_ column.) The following routine -** returns the integer key of the most recent insert in the database. +** CAPI3REF: Last Insert Rowid +** +** Each entry in an SQLite table has a unique 64-bit signed integer key +** called the "rowid". The rowid is always available as an undeclared +** column named ROWID, OID, or _ROWID_. If the table has a column of +** type INTEGER PRIMARY KEY then that column is another an alias for the +** rowid. +** +** This routine returns the rowid of the most recent INSERT into +** the database from the database connection given in the first +** argument. If no inserts have ever occurred on this database +** connection, zero is returned. +** +** If an INSERT occurs within a trigger, then the rowid of the +** inserted row is returned by this routine as long as the trigger +** is running. But once the trigger terminates, the value returned +** by this routine reverts to the last value inserted before the +** trigger fired. */ sqlite_int64 sqlite3_last_insert_rowid(sqlite3*); /* +** CAPI3REF: Count The Number Of Rows Modified +** ** This function returns the number of database rows that were changed ** (or inserted or deleted) by the most recent SQL statement. Only ** changes that are directly specified by the INSERT, UPDATE, or ** DELETE statement are counted. Auxiliary changes caused by -** triggers are not counted. Within the body of a trigger, however, -** the sqlite3_changes() API can be called to find the number of +** triggers are not counted. Use the [sqlite3_total_changes()] function +** to find the total number of changes including changes caused by triggers. +** +** Within the body of a trigger, the sqlite3_changes() interface can be +** called to find the number of ** changes in the most recently completed INSERT, UPDATE, or DELETE ** statement within the body of the trigger. ** @@ -260,7 +363,7 @@ sqlite_int64 sqlite3_last_insert_rowid(s ** ROLLBACK or ABORT. Except, changes associated with creating and ** dropping tables are not counted. ** -** If a callback invokes sqlite3_exec() or sqlite3_step() recursively, +** If a callback invokes [sqlite3_exec()] or [sqlite3_step()] recursively, ** then the changes in the inner, recursive call are counted together ** with the changes in the outer call. ** @@ -275,13 +378,17 @@ int sqlite3_changes(sqlite3*); int sqlite3_changes(sqlite3*); /* +** CAPI3REF: Total Number Of Rows Modified +*** ** This function returns the number of database rows that have been ** modified by INSERT, UPDATE or DELETE statements since the database handle ** was opened. This includes UPDATE, INSERT and DELETE statements executed ** as part of trigger programs. All changes are counted as soon as the ** statement that makes them is completed (when the statement handle is -** passed to sqlite3_reset() or sqlite_finalise()). +** passed to [sqlite3_reset()] or [sqlite_finalise()]). ** +** See also the [sqlite3_change()] interface. +** ** SQLite implements the command "DELETE FROM table" without a WHERE clause ** by dropping and recreating the table. (This is much faster than going ** through and deleting individual elements form the table.) Because of @@ -292,27 +399,38 @@ int sqlite3_total_changes(sqlite3*); */ int sqlite3_total_changes(sqlite3*); -/* This function causes any pending database operation to abort and +/* +** CAPI3REF: Interrupt A Long-Running Query +** +** This function causes any pending database operation to abort and ** return at its earliest opportunity. This routine is typically ** called in response to a user action such as pressing "Cancel" ** or Ctrl-C where the user wants a long query operation to halt ** immediately. ** -** It is safe to call this routine from a different thread that the -** thread that is currently running the database operation. +** It is safe to call this routine from a thread different from the +** thread that is currently running the database operation. +** +** The SQL operation that is interrupted will return [SQLITE_INTERRUPT]. +** If an interrupted operation was an update that is inside an +** explicit transaction, then the entire transaction will be rolled +** back automatically. */ void sqlite3_interrupt(sqlite3*); - -/* These functions return true if the given input string comprises +/* +** CAPI3REF: Determine If An SQL Statement Is Complete +** +** These functions return true if the given input string comprises ** one or more complete SQL statements. For the sqlite3_complete() call, ** the parameter must be a nul-terminated UTF-8 string. For ** sqlite3_complete16(), a nul-terminated machine byte order UTF-16 string ** is required. ** -** This routine is useful for command-line input to see of the user has -** entered a complete statement of SQL or if the current statement needs -** to be continued on the next line. The algorithm is simple. If the +** These routines are useful for command-line input to determine if the +** currently entered text forms one or more complete SQL statements or +** if additional input is needed before sending the statements into +** SQLite for parsing. The algorithm is simple. If the ** last token other than spaces and comments is a semicolon, then return ** true. Actually, the algorithm is a little more complicated than that ** in order to deal with triggers, but the basic idea is the same: the @@ -322,24 +440,29 @@ int sqlite3_complete16(const void *sql); int sqlite3_complete16(const void *sql); /* -** This routine identifies a callback function that is invoked -** whenever an attempt is made to open a database table that is -** currently locked by another process or thread. If the busy callback -** is NULL, then sqlite3_exec() returns SQLITE_BUSY immediately if -** it finds a locked table. If the busy callback is not NULL, then -** sqlite3_exec() invokes the callback with two arguments. The +** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors +** +** This routine identifies a callback function that might be invoked +** whenever an attempt is made to open a database table +** that another thread or process has locked. +** If the busy callback is NULL, then [SQLITE_BUSY] +** (or sometimes [SQLITE_IOERR_BLOCKED]) +** is returned immediately upon encountering the lock. +** If the busy callback is not NULL, then the +** callback will be invoked with two arguments. The ** first argument to the handler is a copy of the void* pointer which ** is the third argument to this routine. The second argument to ** the handler is the number of times that the busy handler has -** been invoked for this locking event. If the -** busy callback returns 0, then sqlite3_exec() immediately returns -** SQLITE_BUSY. If the callback returns non-zero, then sqlite3_exec() -** tries to open the table again and the cycle repeats. +** been invoked for this locking event. If the +** busy callback returns 0, then no additional attempts are made to +** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned. +** If the callback returns non-zero, then another attempt is made to open the +** database for reading and the cycle repeats. ** ** The presence of a busy handler does not guarantee that ** it will be invoked when there is lock contention. ** If SQLite determines that invoking the busy handler could result in -** a deadlock, it will return SQLITE_BUSY instead. +** a deadlock, it will return [SQLITE_BUSY] instead. ** Consider a scenario where one process is holding a read lock that ** it is trying to promote to a reserved lock and ** a second process is holding a reserved lock that it is trying @@ -347,51 +470,83 @@ int sqlite3_complete16(const void *sql); ** because it is blocked by the second and the second process cannot ** proceed because it is blocked by the first. If both processes ** invoke the busy handlers, neither will make any progress. Therefore, -** SQLite returns SQLITE_BUSY for the first process, hoping that this +** SQLite returns [SQLITE_BUSY] for the first process, hoping that this ** will induce the first process to release its read lock and allow ** the second process to proceed. ** ** The default busy callback is NULL. ** +** The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED] when +** SQLite is in the middle of a large transaction where all the +** changes will not fit into the in-memory cache. SQLite will +** already hold a RESERVED lock on the database file, but it needs +** to promote this lock to EXCLUSIVE so that it can spill cache +** pages into the database file without harm to concurrent +** readers. If it is unable to promote the lock, then the in-memory +** cache will be left in an inconsistent state and so the error +** code is promoted from the relatively benign [SQLITE_BUSY] to +** the more severe [SQLITE_IOERR_BLOCKED]. This error code promotion +** forces an automatic rollback of the changes. See the +** +** CorruptionFollowingBusyError wiki page for a discussion of why +** this is important. +** ** Sqlite is re-entrant, so the busy handler may start a new query. ** (It is not clear why anyone would every want to do this, but it ** is allowed, in theory.) But the busy handler may not close the ** database. Closing the database from a busy handler will delete ** data structures out from under the executing query and will -** probably result in a coredump. +** probably result in a segmentation fault or other runtime error. +** +** There can only be a single busy handler defined for each database +** connection. Setting a new busy handler clears any previous one. +** Note that calling [sqlite3_busy_timeout()] will also set or clear +** the busy handler. */ int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*); /* +** CAPI3REF: Set A Busy Timeout +** ** This routine sets a busy handler that sleeps for a while when a ** table is locked. The handler will sleep multiple times until -** at least "ms" milleseconds of sleeping have been done. After -** "ms" milleseconds of sleeping, the handler returns 0 which -** causes sqlite3_exec() to return SQLITE_BUSY. +** at least "ms" milliseconds of sleeping have been done. After +** "ms" milliseconds of sleeping, the handler returns 0 which +** causes [sqlite3_step()] to return [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]. ** ** Calling this routine with an argument less than or equal to zero ** turns off all busy handlers. +** +** There can only be a single busy handler for a particular database +** connection. If another busy handler was defined +** (using [sqlite3_busy_handler()]) prior to calling +** this routine, that other busy handler is cleared. */ int sqlite3_busy_timeout(sqlite3*, int ms); /* -** This next routine is really just a wrapper around sqlite3_exec(). +** CAPI3REF: Convenience Routines For Running Queries +** +** This next routine is a convenience wrapper around [sqlite3_exec()]. ** Instead of invoking a user-supplied callback for each row of the ** result, this routine remembers each row of the result in memory -** obtained from malloc(), then returns all of the result after the +** obtained from [sqlite3_malloc()], then returns all of the result after the ** query has finished. ** ** As an example, suppose the query result where this table: ** +**
 **        Name        | Age
 **        -----------------------
 **        Alice       | 43
 **        Bob         | 28
 **        Cindy       | 21
+** 
** ** If the 3rd argument were &azResult then after the function returns ** azResult will contain the following data: ** +**
 **        azResult[0] = "Name";
 **        azResult[1] = "Age";
 **        azResult[2] = "Alice";
@@ -400,6 +555,7 @@ int sqlite3_busy_timeout(sqlite3*, int m
 **        azResult[5] = "28";
 **        azResult[6] = "Cindy";
 **        azResult[7] = "21";
+** 
** ** Notice that there is an extra row of data containing the column ** headers. But the *nrow return value is still 3. *ncolumn is @@ -409,38 +565,60 @@ int sqlite3_busy_timeout(sqlite3*, int m ** After the calling function has finished using the result, it should ** pass the result data pointer to sqlite3_free_table() in order to ** release the memory that was malloc-ed. Because of the way the -** malloc() happens, the calling function must not try to call -** free() directly. Only sqlite3_free_table() is able to release +** [sqlite3_malloc()] happens, the calling function must not try to call +** [sqlite3_free()] directly. Only [sqlite3_free_table()] is able to release ** the memory properly and safely. ** -** The return value of this routine is the same as from sqlite3_exec(). +** The return value of this routine is the same as from [sqlite3_exec()]. */ int sqlite3_get_table( - sqlite3*, /* An open database */ + sqlite3*, /* An open database */ const char *sql, /* SQL to be executed */ char ***resultp, /* Result written to a char *[] that this points to */ int *nrow, /* Number of result rows written here */ int *ncolumn, /* Number of result columns written here */ char **errmsg /* Error msg written here */ ); - -/* -** Call this routine to free the memory that sqlite3_get_table() allocated. -*/ void sqlite3_free_table(char **result); /* -** The following routines are variants of the "sprintf()" from the -** standard C library. The resulting string is written into memory -** obtained from malloc() so that there is never a possiblity of buffer -** overflow. These routines also implement some additional formatting -** options that are useful for constructing SQL statements. +** CAPI3REF: Formatted String Printing Functions ** -** The strings returned by these routines should be freed by calling -** sqlite3_free(). +** These routines are workalikes of the "printf()" family of functions +** from the standard C library. ** +** The sqlite3_mprintf() and sqlite3_vmprintf() routines write their +** results into memory obtained from [sqlite_malloc()]. +** The strings returned by these two routines should be +** released by [sqlite3_free()]. Both routines return a +** NULL pointer if [sqlite3_malloc()] is unable to allocate enough +** memory to hold the resulting string. +** +** In sqlite3_snprintf() routine is similar to "snprintf()" from +** the standard C library. The result is written into the +** buffer supplied as the second parameter whose size is given by +** the first parameter. Note that the order of the +** first two parameters is reversed from snprintf(). This is an +** historical accident that cannot be fixed without breaking +** backwards compatibility. Note also that sqlite3_snprintf() +** returns a pointer to its buffer instead of the number of +** characters actually written into the buffer. We admit that +** the number of characters written would be a more useful return +** value but we cannot change the implementation of sqlite3_snprintf() +** now without breaking compatibility. +** +** As long as the buffer size is greater than zero, sqlite3_snprintf() +** guarantees that the buffer is always zero-terminated. The first +** parameter "n" is the total size of the buffer, including space for +** the zero terminator. So the longest string that can be completely +** written will be n-1 characters. +** +** These routines all implement some additional formatting +** options that are useful for constructing SQL statements. ** All of the usual printf formatting options apply. In addition, there -** is a "%q" option. %q works like %s in that it substitutes a null-terminated +** is are "%q" and "%Q" options. +** +** The %q option works like %s in that it substitutes a null-terminated ** string from the argument list. But %q also doubles every '\'' character. ** %q is designed for use inside a string literal. By doubling each '\'' ** character it escapes that character and allows it to be inserted into @@ -448,34 +626,58 @@ void sqlite3_free_table(char **result); ** ** For example, so some string variable contains text as follows: ** -** char *zText = "It's a happy day!"; +**
+**  char *zText = "It's a happy day!";
+** 
** -** We can use this text in an SQL statement as follows: +** One can use this text in an SQL statement as follows: ** -** char *z = sqlite3_mprintf("INSERT INTO TABLES('%q')", zText); -** sqlite3_exec(db, z, callback1, 0, 0); -** sqlite3_free(z); +**
+**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
+**  sqlite3_exec(db, zSQL, 0, 0, 0);
+**  sqlite3_free(zSQL);
+** 
** ** Because the %q format string is used, the '\'' character in zText ** is escaped and the SQL generated is as follows: ** -** INSERT INTO table1 VALUES('It''s a happy day!') +**
+**  INSERT INTO table1 VALUES('It''s a happy day!')
+** 
** ** This is correct. Had we used %s instead of %q, the generated SQL ** would have looked like this: ** -** INSERT INTO table1 VALUES('It's a happy day!'); +**
+**  INSERT INTO table1 VALUES('It's a happy day!');
+** 
** ** This second example is an SQL syntax error. As a general rule you ** should always use %q instead of %s when inserting text into a string ** literal. +** +** The %Q option works like %q except it also adds single quotes around +** the outside of the total string. Or if the parameter in the argument +** list is a NULL pointer, %Q substitutes the text "NULL" (without single +** quotes) in place of the %Q option. So, for example, one could say: +** +**
+**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
+**  sqlite3_exec(db, zSQL, 0, 0, 0);
+**  sqlite3_free(zSQL);
+** 
+** +** The code above will render a correct SQL statement in the zSQL +** variable even if the zText variable is a NULL pointer. */ char *sqlite3_mprintf(const char*,...); char *sqlite3_vmprintf(const char*, va_list); char *sqlite3_snprintf(int,char*,const char*, ...); /* -** SQLite uses its own memory allocator. On many installations, this +** CAPI3REF: Memory Allocation Functions +** +** SQLite uses its own memory allocator. On some installations, this ** memory allocator is identical to the standard malloc()/realloc()/free() ** and can be used interchangable. On others, the implementations are ** different. For maximum portability, it is best not to mix calls @@ -485,36 +687,97 @@ void sqlite3_free(void*); void *sqlite3_realloc(void*, int); void sqlite3_free(void*); -#ifndef SQLITE_OMIT_AUTHORIZATION /* -** This routine registers a callback with the SQLite library. The -** callback is invoked (at compile-time, not at run-time) for each -** attempt to access a column of a table in the database. The callback -** returns SQLITE_OK if access is allowed, SQLITE_DENY if the entire -** SQL statement should be aborted with an error and SQLITE_IGNORE -** if the column should be treated as a NULL value. +** CAPI3REF: Compile-Time Authorization Callbacks +*** +** This routine registers a authorizer callback with the SQLite library. +** The authorizer callback is invoked as SQL statements are being compiled +** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()], +** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. At various +** points during the compilation process, as logic is being created +** to perform various actions, the authorizer callback is invoked to +** see if those actions are allowed. The authorizer callback should +** return SQLITE_OK to allow the action, [SQLITE_IGNORE] to disallow the +** specific action but allow the SQL statement to continue to be +** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be +** rejected with an error. +** +** Depending on the action, the [SQLITE_IGNORE] and [SQLITE_DENY] return +** codes might mean something different or they might mean the same +** thing. If the action is, for example, to perform a delete opertion, +** then [SQLITE_IGNORE] and [SQLITE_DENY] both cause the statement compilation +** to fail with an error. But if the action is to read a specific column +** from a specific table, then [SQLITE_DENY] will cause the entire +** statement to fail but [SQLITE_IGNORE] will cause a NULL value to be +** read instead of the actual column value. +** +** The first parameter to the authorizer callback is a copy of +** the third parameter to the sqlite3_set_authorizer() interface. +** The second parameter to the callback is an integer +** [SQLITE_COPY | action code] that specifies the particular action +** to be authorized. The available action codes are +** [SQLITE_COPY | documented separately]. The third through sixth +** parameters to the callback are strings that contain additional +** details about the action to be authorized. +** +** An authorizer is used when preparing SQL statements from an untrusted +** source, to ensure that the SQL statements do not try to access data +** that they are not allowed to see, or that they do not try to +** execute malicious statements that damage the database. For +** example, an application may allow a user to enter arbitrary +** SQL queries for evaluation by a database. But the application does +** not want the user to be able to make arbitrary changes to the +** database. An authorizer could then be put in place while the +** user-entered SQL is being prepared that disallows everything +** except SELECT statements. +** +** Only a single authorizer can be in place on a database connection +** at a time. Each call to sqlite3_set_authorizer overrides the +** previous call. A NULL authorizer means that no authorization +** callback is invoked. The default authorizer is NULL. +** +** Note that the authorizer callback is invoked only during +** [sqlite3_prepare()] or its variants. Authorization is not +** performed during statement evaluation in [sqlite3_step()]. */ int sqlite3_set_authorizer( sqlite3*, int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), void *pUserData ); -#endif /* -** The second parameter to the access authorization function above will -** be one of the values below. These values signify what kind of operation -** is to be authorized. The 3rd and 4th parameters to the authorization -** function will be parameters or NULL depending on which of the following -** codes is used as the second parameter. The 5th parameter is the name -** of the database ("main", "temp", etc.) if applicable. The 6th parameter +** CAPI3REF: Authorizer Return Codes +** +** The [sqlite3_set_authorizer | authorizer callback function] must +** return either [SQLITE_OK] or one of these two constants in order +** to signal SQLite whether or not the action is permitted. See the +** [sqlite3_set_authorizer | authorizer documentation] for additional +** information. +*/ +#define SQLITE_DENY 1 /* Abort the SQL statement with an error */ +#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */ + +/* +** CAPI3REF: Authorizer Action Codes +** +** The [sqlite3_set_authorizer()] interface registers a callback function +** that is invoked to authorizer certain SQL statement actions. The +** second parameter to the callback is an integer code that specifies +** what action is being authorized. These are the integer action codes that +** the authorizer callback may be passed. +** +** These action code values signify what kind of operation is to be +** authorized. The 3rd and 4th parameters to the authorization callback +** function will be parameters or NULL depending on which of these +** codes is used as the second parameter. The 5th parameter to the +** authorizer callback is the name of the database ("main", "temp", +** etc.) if applicable. The 6th parameter to the authorizer callback ** is the name of the inner-most trigger or view that is responsible for ** the access attempt or NULL if this access attempt is directly from -** input SQL code. -** -** Arg-3 Arg-4 +** top-level SQL code. */ -#define SQLITE_COPY 0 /* Table Name File Name */ +/******************************************* 3rd ************ 4th ***********/ #define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */ #define SQLITE_CREATE_TABLE 2 /* Table Name NULL */ #define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */ @@ -546,20 +809,17 @@ int sqlite3_set_authorizer( #define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */ #define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */ #define SQLITE_FUNCTION 31 /* Function Name NULL */ +#define SQLITE_COPY 0 /* No longer used */ /* -** The return value of the authorization function should be one of the -** following constants: -*/ -/* #define SQLITE_OK 0 // Allow access (This is actually defined above) */ -#define SQLITE_DENY 1 /* Abort the SQL statement with an error */ -#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */ - -/* -** Register a function for tracing SQL command evaluation. The function -** registered by sqlite3_trace() is invoked at the first sqlite3_step() -** for the evaluation of an SQL statement. The function registered by -** sqlite3_profile() runs at the end of each SQL statement and includes +** CAPI3REF: Tracing And Profiling Functions +** +** These routines register callback functions that can be used for +** tracing and profiling the execution of SQL statements. +** The callback function registered by sqlite3_trace() is invoked +** at the first [sqlite3_step()] for the evaluation of an SQL statement. +** The callback function registered by sqlite3_profile() is invoked +** as each SQL statement finishes and includes ** information on how long that statement ran. ** ** The sqlite3_profile() API is currently considered experimental and @@ -570,10 +830,12 @@ void *sqlite3_profile(sqlite3*, void(*xProfile)(void*,const char*,sqlite_uint64), void*); /* +** CAPI3REF: Query Progress Callbacks +** ** This routine configures a callback function - the progress callback - that -** is invoked periodically during long running calls to sqlite3_exec(), -** sqlite3_step() and sqlite3_get_table(). An example use for this API is to -** keep a GUI updated during a large query. +** is invoked periodically during long running calls to [sqlite3_exec()], +** [sqlite3_step()] and [sqlite3_get_table()]. An example use for this +** interface is to keep a GUI updated during a large query. ** ** The progress callback is invoked once for every N virtual machine opcodes, ** where N is the second argument to this function. The progress callback @@ -581,53 +843,48 @@ void *sqlite3_profile(sqlite3*, ** argument to this function is a void pointer passed to the progress callback ** function each time it is invoked. ** -** If a call to sqlite3_exec(), sqlite3_step() or sqlite3_get_table() results -** in less than N opcodes being executed, then the progress callback is not -** invoked. +** If a call to [sqlite3_exec()], [sqlite3_step()], or [sqlite3_get_table()] +** results in fewer than N opcodes being executed, then the progress +** callback is never invoked. ** +** Only a single progress callback function may be registered for each +** open database connection. Every call to sqlite3_progress_handler() +** overwrites the results of the previous call. ** To remove the progress callback altogether, pass NULL as the third ** argument to this function. ** ** If the progress callback returns a result other than 0, then the current -** query is immediately terminated and any database changes rolled back. If the -** query was part of a larger transaction, then the transaction is not rolled -** back and remains active. The sqlite3_exec() call returns SQLITE_ABORT. -** -******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ****** +** query is immediately terminated and any database changes rolled back. +** The containing [sqlite3_exec()], [sqlite3_step()], or +** [sqlite3_get_table()] call returns SQLITE_INTERRUPT. This feature +** can be used, for example, to implement the "Cancel" button on a +** progress dialog box in a GUI. */ void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); /* -** Register a callback function to be invoked whenever a new transaction -** is committed. The pArg argument is passed through to the callback. -** callback. If the callback function returns non-zero, then the commit -** is converted into a rollback. +** CAPI3REF: Opening A New Database Connection ** -** If another function was previously registered, its pArg value is returned. -** Otherwise NULL is returned. -** -** Registering a NULL function disables the callback. -** -******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ****** -*/ -void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); - -/* ** Open the sqlite database file "filename". The "filename" is UTF-8 ** encoded for sqlite3_open() and UTF-16 encoded in the native byte order -** for sqlite3_open16(). An sqlite3* handle is returned in *ppDb, even +** for sqlite3_open16(). An [sqlite3*] handle is returned in *ppDb, even ** if an error occurs. If the database is opened (or created) successfully, ** then SQLITE_OK is returned. Otherwise an error code is returned. The ** sqlite3_errmsg() or sqlite3_errmsg16() routines can be used to obtain ** an English language description of the error. ** -** If the database file does not exist, then a new database is created. -** The encoding for the database is UTF-8 if sqlite3_open() is called and -** UTF-16 if sqlite3_open16 is used. +** If the database file does not exist, then a new database will be created +** as needed. The default encoding for the database will be UTF-8 if +** sqlite3_open() is called and UTF-16 if sqlite3_open16 is used. ** ** Whether or not an error occurs when it is opened, resources associated -** with the sqlite3* handle should be released by passing it to +** with the [sqlite3*] handle should be released by passing it to ** sqlite3_close() when it is no longer required. +** +** Note to windows users: The encoding used for the filename argument +** of sqlite3_open() must be UTF-8, not whatever codepage is currently +** defined. Filenames containing international characters must be converted +** to UTF-8 prior to passing them into sqlite3_open(). */ int sqlite3_open( const char *filename, /* Database filename (UTF-8) */ @@ -639,59 +896,75 @@ int sqlite3_open16( ); /* -** Return the error code for the most recent sqlite3_* API call associated -** with sqlite3 handle 'db'. SQLITE_OK is returned if the most recent -** API call was successful. +** CAPI3REF: Error Codes And Messages ** +** The sqlite3_errcode() interface returns the numeric +** [SQLITE_OK | result code] or [SQLITE_IOERR_READ | extended result code] +** for the most recent failed sqlite3_* API call associated +** with [sqlite3] handle 'db'. If a prior API call failed but the +** most recent API call succeeded, the return value from sqlite3_errcode() +** is undefined. +** +** The sqlite3_errmsg() and sqlite3_errmsg16() return English-langauge +** text that describes the error, as either UTF8 or UTF16 respectively. +** Memory to hold the error message string is managed internally. The +** string may be overwritten or deallocated by subsequent calls to SQLite +** interface functions. +** ** Calls to many sqlite3_* functions set the error code and string returned -** by sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16() -** (overwriting the previous values). Note that calls to sqlite3_errcode(), -** sqlite3_errmsg() and sqlite3_errmsg16() themselves do not affect the -** results of future invocations. +** by [sqlite3_errcode()], [sqlite3_errmsg()], and [sqlite3_errmsg16()] +** (overwriting the previous values). Note that calls to [sqlite3_errcode()], +** [sqlite3_errmsg()], and [sqlite3_errmsg16()] themselves do not affect the +** results of future invocations. Calls to API routines that do not return +** an error code (examples: [sqlite3_data_count()] or [sqlite3_mprintf()]) do +** not change the error code returned by this routine. ** ** Assuming no other intervening sqlite3_* API calls are made, the error ** code returned by this function is associated with the same error as -** the strings returned by sqlite3_errmsg() and sqlite3_errmsg16(). +** the strings returned by [sqlite3_errmsg()] and [sqlite3_errmsg16()]. */ int sqlite3_errcode(sqlite3 *db); +const char *sqlite3_errmsg(sqlite3*); +const void *sqlite3_errmsg16(sqlite3*); /* -** Return a pointer to a UTF-8 encoded string describing in english the -** error condition for the most recent sqlite3_* API call. The returned -** string is always terminated by an 0x00 byte. +** CAPI3REF: SQL Statement Object ** -** The string "not an error" is returned when the most recent API call was -** successful. -*/ -const char *sqlite3_errmsg(sqlite3*); - -/* -** Return a pointer to a UTF-16 native byte order encoded string describing -** in english the error condition for the most recent sqlite3_* API call. -** The returned string is always terminated by a pair of 0x00 bytes. +** Instance of this object represent single SQL statements. This +** is variously known as a "prepared statement" or a +** "compiled SQL statement" or simply as a "statement". +** +** The life of a statement object goes something like this: ** -** The string "not an error" is returned when the most recent API call was -** successful. +**
    +**
  1. Create the object using [sqlite3_prepare_v2()] or a related +** function. +**
  2. Bind values to host parameters using +** [sqlite3_bind_blob | sqlite3_bind_* interfaces]. +**
  3. Run the SQL by calling [sqlite3_step()] one or more times. +**
  4. Reset the statement using [sqlite3_reset()] then go back +** to step 2. Do this zero or more times. +**
  5. Destroy the object using [sqlite3_finalize()]. +**
+** +** Refer to documentation on individual methods above for additional +** information. */ -const void *sqlite3_errmsg16(sqlite3*); - -/* -** An instance of the following opaque structure is used to represent -** a compiled SQL statment. -*/ typedef struct sqlite3_stmt sqlite3_stmt; /* +** CAPI3REF: Compiling An SQL Statement +** ** To execute an SQL query, it must first be compiled into a byte-code -** program using one of the following routines. The only difference between -** them is that the second argument, specifying the SQL statement to -** compile, is assumed to be encoded in UTF-8 for the sqlite3_prepare() -** function and UTF-16 for sqlite3_prepare16(). +** program using one of these routines. ** -** The first parameter "db" is an SQLite database handle. The second -** parameter "zSql" is the statement to be compiled, encoded as either -** UTF-8 or UTF-16 (see above). If the next parameter, "nBytes", is less -** than zero, then zSql is read up to the first nul terminator. If +** The first argument "db" is an [sqlite3 | SQLite database handle] +** obtained from a prior call to [sqlite3_open()] or [sqlite3_open16()]. +** The second argument "zSql" is the statement to be compiled, encoded +** as either UTF-8 or UTF-16. The sqlite3_prepare() and sqlite3_prepare_v2() +** interfaces uses UTF-8 and sqlite3_prepare16() and sqlite3_prepare16_v2() +** use UTF-16. If the next argument, "nBytes", is less +** than zero, then zSql is read up to the first zero terminator. If ** "nBytes" is not less than zero, then it is the length of the string zSql ** in bytes (not characters). ** @@ -699,12 +972,49 @@ typedef struct sqlite3_stmt sqlite3_stmt ** SQL statement in zSql. This routine only compiles the first statement ** in zSql, so *pzTail is left pointing to what remains uncompiled. ** -** *ppStmt is left pointing to a compiled SQL statement that can be -** executed using sqlite3_step(). Or if there is an error, *ppStmt may be +** *ppStmt is left pointing to a compiled +** [sqlite3_stmt | SQL statement structure] that can be +** executed using [sqlite3_step()]. Or if there is an error, *ppStmt may be ** set to NULL. If the input text contained no SQL (if the input is and -** empty string or a comment) then *ppStmt is set to NULL. +** empty string or a comment) then *ppStmt is set to NULL. The calling +** procedure is responsible for deleting the compiled SQL statement +** using [sqlite3_finalize()] after it has finished with it. ** -** On success, SQLITE_OK is returned. Otherwise an error code is returned. +** On success, [SQLITE_OK] is returned. Otherwise an +** [SQLITE_ERROR | error code] is returned. +** +** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are +** recommended for all new programs. The two older interfaces are retained +** for backwards compatibility, but their use is discouraged. +** In the "v2" interfaces, the prepared statement +** that is returned (the [sqlite3_stmt] object) contains a copy of the +** original SQL text. This causes the [sqlite3_step()] interface to +** behave a differently in two ways: +** +**
    +**
  1. +** If the database schema changes, instead of returning [SQLITE_SCHEMA] as it +** always used to do, [sqlite3_step()] will automatically recompile the SQL +** statement and try to run it again. If the schema has changed in a way +** that makes the statement no longer valid, [sqlite3_step()] will still +** return [SQLITE_SCHEMA]. But unlike the legacy behavior, [SQLITE_SCHEMA] is +** now a fatal error. Calling [sqlite3_prepare_v2()] again will not make the +** error go away. Note: use [sqlite3_errmsg()] to find the text of the parsing +** error that results in an [SQLITE_SCHEMA] return. +**
  2. +** +**
  3. +** When an error occurs, +** [sqlite3_step()] will return one of the detailed +** [SQLITE_ERROR | result codes] or +** [SQLITE_IOERR_READ | extended result codes] such as directly. +** The legacy behavior was that [sqlite3_step()] would only return a generic +** [SQLITE_ERROR] result code and you would have to make a second call to +** [sqlite3_reset()] in order to find the underlying cause of the problem. +** With the "v2" prepare interfaces, the underlying reason for the error is +** returned immediately. +**
  4. +**
*/ int sqlite3_prepare( sqlite3 *db, /* Database handle */ @@ -713,30 +1023,19 @@ int sqlite3_prepare( sqlite3_stmt **ppStmt, /* OUT: Statement handle */ const char **pzTail /* OUT: Pointer to unused portion of zSql */ ); -int sqlite3_prepare16( +int sqlite3_prepare_v2( sqlite3 *db, /* Database handle */ - const void *zSql, /* SQL statement, UTF-16 encoded */ + const char *zSql, /* SQL statement, UTF-8 encoded */ int nBytes, /* Length of zSql in bytes. */ sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const void **pzTail /* OUT: Pointer to unused portion of zSql */ + const char **pzTail /* OUT: Pointer to unused portion of zSql */ ); - -/* -** Newer versions of the prepare API work just like the legacy versions -** but with one exception: The a copy of the SQL text is saved in the -** sqlite3_stmt structure that is returned. If this copy exists, it -** modifieds the behavior of sqlite3_step() slightly. First, sqlite3_step() -** will no longer return an SQLITE_SCHEMA error but will instead automatically -** rerun the compiler to rebuild the prepared statement. Secondly, -** sqlite3_step() now turns a full result code - the result code that -** use used to have to call sqlite3_reset() to get. -*/ -int sqlite3_prepare_v2( +int sqlite3_prepare16( sqlite3 *db, /* Database handle */ - const char *zSql, /* SQL statement, UTF-8 encoded */ + const void *zSql, /* SQL statement, UTF-16 encoded */ int nBytes, /* Length of zSql in bytes. */ sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const char **pzTail /* OUT: Pointer to unused portion of zSql */ + const void **pzTail /* OUT: Pointer to unused portion of zSql */ ); int sqlite3_prepare16_v2( sqlite3 *db, /* Database handle */ @@ -747,40 +1046,96 @@ int sqlite3_prepare16_v2( ); /* -** Pointers to the following two opaque structures are used to communicate -** with the implementations of user-defined functions. +** CAPI3REF: Dynamically Typed Value Object +** +** SQLite uses dynamic typing for the values it stores. Values can +** be integers, floating point values, strings, BLOBs, or NULL. When +** passing around values internally, each value is represented as +** an instance of the sqlite3_value object. */ -typedef struct sqlite3_context sqlite3_context; typedef struct Mem sqlite3_value; /* -** In the SQL strings input to sqlite3_prepare() and sqlite3_prepare16(), -** one or more literals can be replace by parameters "?" or "?NNN" or -** ":AAA" or "@AAA" or "$VVV" where NNN is a integer, AAA is an identifer, -** and VVV is a variable name according to the syntax rules of the -** TCL programming language. The value of these parameters (also called -** "host parameter names") can be set using the routines listed below. +** CAPI3REF: SQL Function Context Object ** -** In every case, the first argument is a pointer to the sqlite3_stmt -** structure returned from sqlite3_prepare(). The second argument is the -** index of the host parameter name. The first host parameter as an index -** of 1. For named host parameters (":AAA" or "$VVV") you can use -** sqlite3_bind_parameter_index() to get the correct index value given -** the parameter name. If the same named parameter occurs more than -** once, it is assigned the same index each time. +** The context in which an SQL function executes is stored in an +** sqlite3_context object. A pointer to such an object is the +** first parameter to user-defined SQL functions. +*/ +typedef struct sqlite3_context sqlite3_context; + +/* +** CAPI3REF: Binding Values To Prepared Statements ** +** In the SQL strings input to [sqlite3_prepare_v2()] and its variants, +** one or more literals can be replace by a parameter in one of these +** forms: +** +**
    +**
  • ? +**
  • ?NNN +**
  • :AAA +**
  • @AAA +**
  • $VVV +**
+** +** In the parameter forms shown above NNN is an integer literal, +** AAA is an alphanumeric identifier and VVV is a variable name according +** to the syntax rules of the TCL programming language. +** The values of these parameters (also called "host parameter names") +** can be set using the sqlite3_bind_*() routines defined here. +** +** The first argument to the sqlite3_bind_*() routines always is a pointer +** to the [sqlite3_stmt] object returned from [sqlite3_prepare_v2()] or +** its variants. The second +** argument is the index of the parameter to be set. The first parameter has +** an index of 1. When the same named parameter is used more than once, second +** and subsequent +** occurrences have the same index as the first occurrence. The index for +** named parameters can be looked up using the +** [sqlite3_bind_parameter_name()] API if desired. The index for "?NNN" +** parametes is the value of NNN. +** The NNN value must be between 1 and the compile-time +** parameter SQLITE_MAX_VARIABLE_NUMBER (default value: 999). +** See limits.html for additional information. +** +** The third argument is the value to bind to the parameter. +** +** In those +** routines that have a fourth argument, its value is the number of bytes +** in the parameter. To be clear: the value is the number of bytes in the +** string, not the number of characters. The number +** of bytes does not include the zero-terminator at the end of strings. +** If the fourth parameter is negative, the length of the string is +** number of bytes up to the first zero terminator. +** ** The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or ** text after SQLite has finished with it. If the fifth argument is the -** special value SQLITE_STATIC, then the library assumes that the information +** special value [SQLITE_STATIC], then the library assumes that the information ** is in static, unmanaged space and does not need to be freed. If the -** fifth argument has the value SQLITE_TRANSIENT, then SQLite makes its -** own private copy of the data before the sqlite3_bind_* routine returns. +** fifth argument has the value [SQLITE_TRANSIENT], then SQLite makes its +** own private copy of the data immediately, before the sqlite3_bind_*() +** routine returns. ** -** The sqlite3_bind_* routine must be called before sqlite3_step() and after -** an sqlite3_prepare() or sqlite3_reset(). Bindings persist across -** multiple calls to sqlite3_reset() and sqlite3_step(). Unbound parameters -** are interpreted as NULL. +** The sqlite3_bind_zeroblob() routine binds a BLOB of length n that +** is filled with zeros. A zeroblob uses a fixed amount of memory +** (just an integer to hold it size) while it is being processed. +** Zeroblobs are intended to serve as place-holders for BLOBs whose +** content is later written using +** [sqlite3_blob_open | increment BLOB I/O] routines. +** +** The sqlite3_bind_*() routines must be called after +** [sqlite3_prepare_v2()] (and its variants) or [sqlite3_reset()] and +** before [sqlite3_step()]. +** Bindings are not cleared by the [sqlite3_reset()] routine. +** Unbound parameters are interpreted as NULL. +** +** These routines return [SQLITE_OK] on success or an error code if +** anything goes wrong. [SQLITE_RANGE] is returned if the parameter +** index is out of range. [SQLITE_NOMEM] is returned if malloc fails. +** [SQLITE_MISUSE] is returned if these routines are called on a virtual +** machine that is the wrong state or which has already been finalized. */ int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); int sqlite3_bind_double(sqlite3_stmt*, int, double); @@ -790,62 +1145,116 @@ int sqlite3_bind_value(sqlite3_stmt*, in int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*)); int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*)); int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); +int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); /* -** Return the number of host parameters in a compiled SQL statement. This -** routine was added to support DBD::SQLite. +** CAPI3REF: Number Of Host Parameters +** +** Return the largest host parameter index in the precompiled statement given +** as the argument. When the host parameters are of the forms like ":AAA" +** or "?", then they are assigned sequential increasing numbers beginning +** with one, so the value returned is the number of parameters. However +** if the same host parameter name is used multiple times, each occurrance +** is given the same number, so the value returned in that case is the number +** of unique host parameter names. If host parameters of the form "?NNN" +** are used (where NNN is an integer) then there might be gaps in the +** numbering and the value returned by this interface is the index of the +** host parameter with the largest index value. */ int sqlite3_bind_parameter_count(sqlite3_stmt*); /* -** Return the name of the i-th name parameter. Ordinary parameters "?" are -** nameless and a NULL is returned. For parameters of the form :AAA or -** $VVV the complete text of the parameter name is returned, including -** the initial ":" or "$". NULL is returned if the index is out of range. +** CAPI3REF: Name Of A Host Parameter +** +** This routine returns a pointer to the name of the n-th parameter in a +** [sqlite3_stmt | prepared statement]. +** Host parameters of the form ":AAA" or "@AAA" or "$VVV" have a name +** which is the string ":AAA" or "@AAA" or "$VVV". +** In other words, the initial ":" or "$" or "@" +** is included as part of the name. +** Parameters of the form "?" or "?NNN" have no name. +** +** The first bound parameter has an index of 1, not 0. +** +** If the value n is out of range or if the n-th parameter is nameless, +** then NULL is returned. The returned string is always in the +** UTF-8 encoding even if the named parameter was originally specified +** as UTF-16 in [sqlite3_prepare16()] or [sqlite3_prepare16_v2()]. */ const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int); /* -** Return the index of a parameter with the given name. The name -** must match exactly. If no parameter with the given name is found, -** return 0. +** CAPI3REF: Index Of A Parameter With A Given Name +** +** This routine returns the index of a host parameter with the given name. +** The name must match exactly. If no parameter with the given name is +** found, return 0. Parameter names must be UTF8. */ int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName); /* -** Set all the parameters in the compiled SQL statement to NULL. +** CAPI3REF: Reset All Bindings On A Prepared Statement +** +** Contrary to the intuition of many, [sqlite3_reset()] does not +** reset the [sqlite3_bind_blob | bindings] on a +** [sqlite3_stmt | prepared statement]. Use this routine to +** reset all host parameters to NULL. */ int sqlite3_clear_bindings(sqlite3_stmt*); /* -** Return the number of columns in the result set returned by the compiled -** SQL statement. This routine returns 0 if pStmt is an SQL statement -** that does not return data (for example an UPDATE). +** CAPI3REF: Number Of Columns In A Result Set +** +** Return the number of columns in the result set returned by the +** [sqlite3_stmt | compiled SQL statement]. This routine returns 0 +** if pStmt is an SQL statement that does not return data (for +** example an UPDATE). */ int sqlite3_column_count(sqlite3_stmt *pStmt); /* -** The first parameter is a compiled SQL statement. This function returns -** the column heading for the Nth column of that statement, where N is the -** second function parameter. The string returned is UTF-8 for -** sqlite3_column_name() and UTF-16 for sqlite3_column_name16(). +** CAPI3REF: Column Names In A Result Set +** +** These routines return the name assigned to a particular column +** in the result set of a SELECT statement. The sqlite3_column_name() +** interface returns a pointer to a UTF8 string and sqlite3_column_name16() +** returns a pointer to a UTF16 string. The first parameter is the +** [sqlite_stmt | prepared statement] that implements the SELECT statement. +** The second parameter is the column number. The left-most column is +** number 0. +** +** The returned string pointer is valid until either the +** [sqlite_stmt | prepared statement] is destroyed by [sqlite3_finalize()] +** or until the next call sqlite3_column_name() or sqlite3_column_name16() +** on the same column. */ -const char *sqlite3_column_name(sqlite3_stmt*,int); -const void *sqlite3_column_name16(sqlite3_stmt*,int); +const char *sqlite3_column_name(sqlite3_stmt*, int N); +const void *sqlite3_column_name16(sqlite3_stmt*, int N); /* -** The first argument to the following calls is a compiled SQL statement. +** CAPI3REF: Source Of Data In A Query Result +** +** These routines provide a means to determine what column of what +** table in which database a result of a SELECT statement comes from. +** The name of the database or table or column can be returned as +** either a UTF8 or UTF16 string. The returned string is valid until +** the [sqlite3_stmt | prepared statement] is destroyed using +** [sqlite3_finalize()] or until the same information is requested +** again about the same column. +** +** The first argument to the following calls is a +** [sqlite3_stmt | compiled SQL statement]. ** These functions return information about the Nth column returned by ** the statement, where N is the second function argument. ** -** If the Nth column returned by the statement is not a column value, -** then all of the functions return NULL. Otherwise, the return the -** name of the attached database, table and column that the expression -** extracts a value from. +** If the Nth column returned by the statement is an expression +** or subquery and is not a column value, then all of these functions +** return NULL. Otherwise, they return the +** name of the attached database, table and column that query result +** column was extracted from. ** ** As with all other SQLite APIs, those postfixed with "16" return UTF-16 -** encoded strings, the other functions return UTF-8. The memory containing -** the returned strings is valid until the statement handle is finalized(). +** encoded strings, the other functions return UTF-8. ** ** These APIs are only available if the library was compiled with the ** SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol defined. @@ -858,13 +1267,17 @@ const void *sqlite3_column_origin_name16 const void *sqlite3_column_origin_name16(sqlite3_stmt*,int); /* -** The first parameter is a compiled SQL statement. If this statement -** is a SELECT statement, the Nth column of the returned result set -** of the SELECT is a table column then the declared type of the table -** column is returned. If the Nth column of the result set is not at table -** column, then a NULL pointer is returned. The returned string is always -** UTF-8 encoded. For example, in the database schema: +** CAPI3REF: Declared Datatype Of A Query Result ** +** The first parameter is a [sqlite3_stmt | compiled SQL statement]. +** If this statement is a SELECT statement and the Nth column of the +** returned result set of that SELECT is a table column (not an +** expression or subquery) then the declared type of the table +** column is returned. If the Nth column of the result set is an +** expression or subquery, then a NULL pointer is returned. +** The returned string is always UTF-8 encoded. For example, in +** the database schema: +** ** CREATE TABLE t1(c1 VARIANT); ** ** And the following statement compiled: @@ -874,89 +1287,127 @@ const void *sqlite3_column_origin_name16 ** Then this routine would return the string "VARIANT" for the second ** result column (i==1), and a NULL pointer for the first result column ** (i==0). +** +** SQLite uses dynamic run-time typing. So just because a column +** is declared to contain a particular type does not mean that the +** data stored in that column is of the declared type. SQLite is +** strongly typed, but the typing is dynamic not static. Type +** is associated with individual values, not with the containers +** used to hold those values. */ const char *sqlite3_column_decltype(sqlite3_stmt *, int i); +const void *sqlite3_column_decltype16(sqlite3_stmt*,int); -/* -** The first parameter is a compiled SQL statement. If this statement -** is a SELECT statement, the Nth column of the returned result set -** of the SELECT is a table column then the declared type of the table -** column is returned. If the Nth column of the result set is not at table -** column, then a NULL pointer is returned. The returned string is always -** UTF-16 encoded. For example, in the database schema: +/* +** CAPI3REF: Evaluate An SQL Statement ** -** CREATE TABLE t1(c1 INTEGER); +** After an [sqlite3_stmt | SQL statement] has been prepared with a call +** to either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or to one of +** the legacy interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], +** then this function must be called one or more times to evaluate the +** statement. ** -** And the following statement compiled: +** The details of the behavior of this sqlite3_step() interface depend +** on whether the statement was prepared using the newer "v2" interface +** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy +** interface [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the +** new "v2" interface is recommended for new applications but the legacy +** interface will continue to be supported. ** -** SELECT c1 + 1, c1 FROM t1; +** In the lagacy interface, the return value will be either [SQLITE_BUSY], +** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE]. +** With the "v2" interface, any of the other [SQLITE_OK | result code] +** or [SQLITE_IOERR_READ | extended result code] might be returned as +** well. ** -** Then this routine would return the string "INTEGER" for the second -** result column (i==1), and a NULL pointer for the first result column -** (i==0). -*/ -const void *sqlite3_column_decltype16(sqlite3_stmt*,int); - -/* -** After an SQL query has been compiled with a call to either -** sqlite3_prepare() or sqlite3_prepare16(), then this function must be -** called one or more times to execute the statement. +** [SQLITE_BUSY] means that the database engine was unable to acquire the +** database locks it needs to do its job. If the statement is a COMMIT +** or occurs outside of an explicit transaction, then you can retry the +** statement. If the statement is not a COMMIT and occurs within a +** explicit transaction then you should rollback the transaction before +** continuing. ** -** The return value will be either SQLITE_BUSY, SQLITE_DONE, -** SQLITE_ROW, SQLITE_ERROR, or SQLITE_MISUSE. -** -** SQLITE_BUSY means that the database engine attempted to open -** a locked database and there is no busy callback registered. -** Call sqlite3_step() again to retry the open. -** -** SQLITE_DONE means that the statement has finished executing +** [SQLITE_DONE] means that the statement has finished executing ** successfully. sqlite3_step() should not be called again on this virtual -** machine. +** machine without first calling [sqlite3_reset()] to reset the virtual +** machine back to its initial state. ** ** If the SQL statement being executed returns any data, then -** SQLITE_ROW is returned each time a new row of data is ready +** [SQLITE_ROW] is returned each time a new row of data is ready ** for processing by the caller. The values may be accessed using -** the sqlite3_column_*() functions described below. sqlite3_step() -** is called again to retrieve the next row of data. +** the [sqlite3_column_int | column access functions]. +** sqlite3_step() is called again to retrieve the next row of data. ** -** SQLITE_ERROR means that a run-time error (such as a constraint +** [SQLITE_ERROR] means that a run-time error (such as a constraint ** violation) has occurred. sqlite3_step() should not be called again on -** the VM. More information may be found by calling sqlite3_errmsg(). +** the VM. More information may be found by calling [sqlite3_errmsg()]. +** With the legacy interface, a more specific error code (example: +** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth) +** can be obtained by calling [sqlite3_reset()] on the +** [sqlite_stmt | prepared statement]. In the "v2" interface, +** the more specific error code is returned directly by sqlite3_step(). ** -** SQLITE_MISUSE means that the this routine was called inappropriately. -** Perhaps it was called on a virtual machine that had already been -** finalized or on one that had previously returned SQLITE_ERROR or -** SQLITE_DONE. Or it could be the case the the same database connection -** is being used simulataneously by two or more threads. +** [SQLITE_MISUSE] means that the this routine was called inappropriately. +** Perhaps it was called on a [sqlite_stmt | prepared statement] that has +** already been [sqlite3_finalize | finalized] or on one that had +** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could +** be the case that the same database connection is being used by two or +** more threads at the same moment in time. +** +** Goofy Interface Alert: +** In the legacy interface, +** the sqlite3_step() API always returns a generic error code, +** [SQLITE_ERROR], following any error other than [SQLITE_BUSY] +** and [SQLITE_MISUSE]. You must call [sqlite3_reset()] or +** [sqlite3_finalize()] in order to find one of the specific +** [SQLITE_ERROR | result codes] that better describes the error. +** We admit that this is a goofy design. The problem has been fixed +** with the "v2" interface. If you prepare all of your SQL statements +** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead +** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()], then the +** more specific [SQLITE_ERROR | result codes] are returned directly +** by sqlite3_step(). The use of the "v2" interface is recommended. */ int sqlite3_step(sqlite3_stmt*); /* +** CAPI3REF: +** ** Return the number of values in the current row of the result set. ** -** After a call to sqlite3_step() that returns SQLITE_ROW, this routine -** will return the same value as the sqlite3_column_count() function. -** After sqlite3_step() has returned an SQLITE_DONE, SQLITE_BUSY or -** error code, or before sqlite3_step() has been called on a -** compiled SQL statement, this routine returns zero. +** After a call to [sqlite3_step()] that returns [SQLITE_ROW], this routine +** will return the same value as the [sqlite3_column_count()] function. +** After [sqlite3_step()] has returned an [SQLITE_DONE], [SQLITE_BUSY], or +** a [SQLITE_ERROR | error code], or before [sqlite3_step()] has been +** called on the [sqlite_stmt | prepared statement] for the first time, +** this routine returns zero. */ int sqlite3_data_count(sqlite3_stmt *pStmt); /* -** Values are stored in the database in one of the following fundamental -** types. +** CAPI3REF: Fundamental Datatypes +** +** Every value in SQLite has one of five fundamental datatypes: +** +**
    +**
  • 64-bit signed integer +**
  • 64-bit IEEE floating point number +**
  • string +**
  • BLOB +**
  • NULL +**
+** +** These constants are codes for each of those types. +** +** Note that the SQLITE_TEXT constant was also used in SQLite version 2 +** for a completely different meaning. Software that links against both +** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT not +** SQLITE_TEXT. */ #define SQLITE_INTEGER 1 #define SQLITE_FLOAT 2 -/* #define SQLITE_TEXT 3 // See below */ #define SQLITE_BLOB 4 #define SQLITE_NULL 5 - -/* -** SQLite version 2 defines SQLITE_TEXT differently. To allow both -** version 2 and version 3 to be included, undefine them both if a -** conflict is seen. Define SQLITE3_TEXT to be the version 3 value. -*/ #ifdef SQLITE_TEXT # undef SQLITE_TEXT #else @@ -965,61 +1416,124 @@ int sqlite3_data_count(sqlite3_stmt *pSt #define SQLITE3_TEXT 3 /* -** The next group of routines returns information about the information +** CAPI3REF: Results Values From A Query +** +** These routines return information about the information ** in a single column of the current result row of a query. In every -** case the first parameter is a pointer to the SQL statement that is being -** executed (the sqlite_stmt* that was returned from sqlite3_prepare()) and +** case the first argument is a pointer to the +** [sqlite3_stmt | SQL statement] that is being +** evaluate (the [sqlite_stmt*] that was returned from +** [sqlite3_prepare_v2()] or one of its variants) and ** the second argument is the index of the column for which information -** should be returned. iCol is zero-indexed. The left-most column as an -** index of 0. +** should be returned. The left-most column has an index of 0. ** ** If the SQL statement is not currently point to a valid row, or if the -** the colulmn index is out of range, the result is undefined. +** the column index is out of range, the result is undefined. ** +** The sqlite3_column_type() routine returns +** [SQLITE_INTEGER | datatype code] for the initial data type +** of the result column. The returned value is one of [SQLITE_INTEGER], +** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL]. The value +** returned by sqlite3_column_type() is only meaningful if no type +** conversions have occurred as described below. After a type conversion, +** the value returned by sqlite3_column_type() is undefined. Future +** versions of SQLite may change the behavior of sqlite3_column_type() +** following a type conversion. +** +*** The sqlite3_column_nm +** +** If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes() +** routine returns the number of bytes in that BLOB or string. +** If the result is a UTF-16 string, then sqlite3_column_bytes() converts +** the string to UTF-8 and then returns the number of bytes. +** If the result is a numeric value then sqlite3_column_bytes() uses +** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns +** the number of bytes in that string. +** The value returned does not include the zero terminator at the end +** of the string. For clarity: the value returned is the number of +** bytes in the string, not the number of characters. +** +** The sqlite3_column_bytes16() routine is similar to sqlite3_column_bytes() +** but leaves the result in UTF-16 instead of UTF-8. +** The zero terminator is not included in this count. +** ** These routines attempt to convert the value where appropriate. For ** example, if the internal representation is FLOAT and a text result -** is requested, sprintf() is used internally to do the conversion +** is requested, [sqlite3_snprintf()] is used internally to do the conversion ** automatically. The following table details the conversions that ** are applied: ** -** Internal Type Requested Type Conversion -** ------------- -------------- -------------------------- -** NULL INTEGER Result is 0 -** NULL FLOAT Result is 0.0 -** NULL TEXT Result is an empty string -** NULL BLOB Result is a zero-length BLOB -** INTEGER FLOAT Convert from integer to float -** INTEGER TEXT ASCII rendering of the integer -** INTEGER BLOB Same as for INTEGER->TEXT -** FLOAT INTEGER Convert from float to integer -** FLOAT TEXT ASCII rendering of the float -** FLOAT BLOB Same as FLOAT->TEXT -** TEXT INTEGER Use atoi() -** TEXT FLOAT Use atof() -** TEXT BLOB No change -** BLOB INTEGER Convert to TEXT then use atoi() -** BLOB FLOAT Convert to TEXT then use atof() -** BLOB TEXT Add a \000 terminator if needed +**
+** +**
Internal Requested +**
Type Type Conversion ** -** The following access routines are provided: +**
NULL INTEGER Result is 0 +**
NULL FLOAT Result is 0.0 +**
NULL TEXT Result is NULL pointer +**
NULL BLOB Result is NULL pointer +**
INTEGER FLOAT Convert from integer to float +**
INTEGER TEXT ASCII rendering of the integer +**
INTEGER BLOB Same as for INTEGER->TEXT +**
FLOAT INTEGER Convert from float to integer +**
FLOAT TEXT ASCII rendering of the float +**
FLOAT BLOB Same as FLOAT->TEXT +**
TEXT INTEGER Use atoi() +**
TEXT FLOAT Use atof() +**
TEXT BLOB No change +**
BLOB INTEGER Convert to TEXT then use atoi() +**
BLOB FLOAT Convert to TEXT then use atof() +**
BLOB TEXT Add a zero terminator if needed +**
+**
** -** _type() Return the datatype of the result. This is one of -** SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, -** or SQLITE_NULL. -** _blob() Return the value of a BLOB. -** _bytes() Return the number of bytes in a BLOB value or the number -** of bytes in a TEXT value represented as UTF-8. The \000 -** terminator is included in the byte count for TEXT values. -** _bytes16() Return the number of bytes in a BLOB value or the number -** of bytes in a TEXT value represented as UTF-16. The \u0000 -** terminator is included in the byte count for TEXT values. -** _double() Return a FLOAT value. -** _int() Return an INTEGER value in the host computer's native -** integer representation. This might be either a 32- or 64-bit -** integer depending on the host. -** _int64() Return an INTEGER value as a 64-bit signed integer. -** _text() Return the value as UTF-8 text. -** _text16() Return the value as UTF-16 text. +** The table above makes reference to standard C library functions atoi() +** and atof(). SQLite does not really use these functions. It has its +** on equavalent internal routines. The atoi() and atof() names are +** used in the table for brevity and because they are familiar to most +** C programmers. +** +** Note that when type conversions occur, pointers returned by prior +** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or +** sqlite3_column_text16() may be invalidated. +** Type conversions and pointer invalidations might occur +** in the following cases: +** +**
    +**
  • The initial content is a BLOB and sqlite3_column_text() +** or sqlite3_column_text16() is called. A zero-terminator might +** need to be added to the string.

  • +** +**
  • The initial content is UTF-8 text and sqlite3_column_bytes16() or +** sqlite3_column_text16() is called. The content must be converted +** to UTF-16.

  • +** +**
  • The initial content is UTF-16 text and sqlite3_column_bytes() or +** sqlite3_column_text() is called. The content must be converted +** to UTF-8.

  • +**
+** +** Conversions between UTF-16be and UTF-16le are always done in place and do +** not invalidate a prior pointer, though of course the content of the buffer +** that the prior pointer points to will have been modified. Other kinds +** of conversion are done in place when it is possible, but sometime it is +** not possible and in those cases prior pointers are invalidated. +** +** The safest and easiest to remember policy is to invoke these routines +** in one of the following ways: +** +**
    +**
  • sqlite3_column_text() followed by sqlite3_column_bytes()
  • +**
  • sqlite3_column_blob() followed by sqlite3_column_bytes()
  • +**
  • sqlite3_column_text16() followed by sqlite3_column_bytes16()
  • +**
+** +** In other words, you should call sqlite3_column_text(), sqlite3_column_blob(), +** or sqlite3_column_text16() first to force the result into the desired +** format, then invoke sqlite3_column_bytes() or sqlite3_column_bytes16() to +** find the size of the result. Do not mix call to sqlite3_column_text() or +** sqlite3_column_blob() with calls to sqlite3_column_bytes16(). And do not +** mix calls to sqlite3_column_text16() with calls to sqlite3_column_bytes(). */ const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); int sqlite3_column_bytes(sqlite3_stmt*, int iCol); @@ -1030,68 +1544,97 @@ int sqlite3_column_type(sqlite3_stmt*, i const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol); const void *sqlite3_column_text16(sqlite3_stmt*, int iCol); int sqlite3_column_type(sqlite3_stmt*, int iCol); -int sqlite3_column_numeric_type(sqlite3_stmt*, int iCol); sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol); /* -** The sqlite3_finalize() function is called to delete a compiled -** SQL statement obtained by a previous call to sqlite3_prepare() -** or sqlite3_prepare16(). If the statement was executed successfully, or -** not executed at all, then SQLITE_OK is returned. If execution of the -** statement failed then an error code is returned. +** CAPI3REF: Destroy A Prepared Statement Object ** +** The sqlite3_finalize() function is called to delete a +** [sqlite3_stmt | compiled SQL statement]. If the statement was +** executed successfully, or not executed at all, then SQLITE_OK is returned. +** If execution of the statement failed then an +** [SQLITE_ERROR | error code] or [SQLITE_IOERR_READ | extended error code] +** is returned. +** ** This routine can be called at any point during the execution of the -** virtual machine. If the virtual machine has not completed execution -** when this routine is called, that is like encountering an error or -** an interrupt. (See sqlite3_interrupt().) Incomplete updates may be -** rolled back and transactions cancelled, depending on the circumstances, -** and the result code returned will be SQLITE_ABORT. +** [sqlite3_stmt | virtual machine]. If the virtual machine has not +** completed execution when this routine is called, that is like +** encountering an error or an interrupt. (See [sqlite3_interrupt()].) +** Incomplete updates may be rolled back and transactions cancelled, +** depending on the circumstances, and the +** [SQLITE_ERROR | result code] returned will be [SQLITE_ABORT]. */ int sqlite3_finalize(sqlite3_stmt *pStmt); /* -** The sqlite3_reset() function is called to reset a compiled SQL -** statement obtained by a previous call to sqlite3_prepare() or -** sqlite3_prepare16() back to it's initial state, ready to be re-executed. +** CAPI3REF: Reset A Prepared Statement Object +** +** The sqlite3_reset() function is called to reset a +** [sqlite_stmt | compiled SQL statement] object. +** back to it's initial state, ready to be re-executed. ** Any SQL statement variables that had values bound to them using -** the sqlite3_bind_*() API retain their values. +** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values. +** Use [sqlite3_clear_bindings()] to reset the bindings. */ int sqlite3_reset(sqlite3_stmt *pStmt); /* -** The following two functions are used to add user functions or aggregates -** implemented in C to the SQL langauge interpreted by SQLite. The +** CAPI3REF: Create Or Redefine SQL Functions +** +** The following two functions are used to add SQL functions or aggregates +** or to redefine the behavior of existing SQL functions or aggregates. The ** difference only between the two is that the second parameter, the ** name of the (scalar) function or aggregate, is encoded in UTF-8 for ** sqlite3_create_function() and UTF-16 for sqlite3_create_function16(). ** -** The first argument is the database handle that the new function or -** aggregate is to be added to. If a single program uses more than one -** database handle internally, then user functions or aggregates must -** be added individually to each database handle with which they will be -** used. +** The first argument is the [sqlite3 | database handle] that holds the +** SQL function or aggregate is to be added or redefined. If a single +** program uses more than one database handle internally, then SQL +** functions or aggregates must be added individually to each database +** handle with which they will be used. ** -** The third parameter is the number of arguments that the function or -** aggregate takes. If this parameter is negative, then the function or +** The second parameter is the name of the SQL function to be created +** or redefined. +** The length of the name is limited to 255 bytes, exclusive of the +** zero-terminator. Note that the name length limit is in bytes, not +** characters. Any attempt to create a function with a longer name +** will result in an SQLITE_ERROR error. +** +** The third parameter is the number of arguments that the SQL function or +** aggregate takes. If this parameter is negative, then the SQL function or ** aggregate may take any number of arguments. ** -** The fourth parameter is one of SQLITE_UTF* values defined below, -** indicating the encoding that the function is most likely to handle -** values in. This does not change the behaviour of the programming -** interface. However, if two versions of the same function are registered -** with different encoding values, SQLite invokes the version likely to -** minimize conversions between text encodings. +** The fourth parameter, eTextRep, specifies what +** [SQLITE_UTF8 | text encoding] this SQL function prefers for +** its parameters. Any SQL function implementation should be able to work +** work with UTF-8, UTF-16le, or UTF-16be. But some implementations may be +** more efficient with one encoding than another. It is allowed to +** invoke sqlite_create_function() or sqlite3_create_function16() multiple +** times with the same function but with different values of eTextRep. +** When multiple implementations of the same function are available, SQLite +** will pick the one that involves the least amount of data conversion. +** If there is only a single implementation which does not care what +** text encoding is used, then the fourth argument should be +** [SQLITE_ANY]. ** +** The fifth parameter is an arbitrary pointer. The implementation +** of the function can gain access to this pointer using +** [sqlite_user_data()]. +** ** The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are -** pointers to user implemented C functions that implement the user -** function or aggregate. A scalar function requires an implementation of +** pointers to C-language functions that implement the SQL +** function or aggregate. A scalar SQL function requires an implementation of ** the xFunc callback only, NULL pointers should be passed as the xStep -** and xFinal parameters. An aggregate function requires an implementation -** of xStep and xFinal, but NULL should be passed for xFunc. To delete an -** existing user function or aggregate, pass NULL for all three function -** callback. Specifying an inconstent set of callback values, such as an -** xFunc and an xFinal, or an xStep but no xFinal, SQLITE_ERROR is -** returned. +** and xFinal parameters. An aggregate SQL function requires an implementation +** of xStep and xFinal and NULL should be passed for xFunc. To delete an +** existing SQL function or aggregate, pass NULL for all three function +** callback. +** +** It is permitted to register multiple implementations of the same +** functions with the same name but with either differing numbers of +** arguments or differing perferred text encodings. SQLite will use +** the implementation most closely matches the way in which the +** SQL function is used. */ int sqlite3_create_function( sqlite3 *, @@ -1115,18 +1658,71 @@ int sqlite3_create_function16( ); /* -** This function is deprecated. Do not use it. It continues to exist -** so as not to break legacy code. But new code should avoid using it. +** CAPI3REF: Text Encodings +** +** These constant define integer codes that represent the various +** text encodings supported by SQLite. */ +#define SQLITE_UTF8 1 +#define SQLITE_UTF16LE 2 +#define SQLITE_UTF16BE 3 +#define SQLITE_UTF16 4 /* Use native byte order */ +#define SQLITE_ANY 5 /* sqlite3_create_function only */ +#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ + +/* +** CAPI3REF: Obsolete Functions +** +** These functions are all now obsolete. In order to maintain +** backwards compatibility with older code, we continue to support +** these functions. However, new development projects should avoid +** the use of these functions. To help encourage people to avoid +** using these functions, we are not going to tell you want they do. +*/ int sqlite3_aggregate_count(sqlite3_context*); +int sqlite3_expired(sqlite3_stmt*); +int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*); +int sqlite3_global_recover(void); + /* -** The next group of routines returns information about parameters to -** a user-defined function. Function implementations use these routines -** to access their parameters. These routines are the same as the -** sqlite3_column_* routines except that these routines take a single -** sqlite3_value* pointer instead of an sqlite3_stmt* and an integer -** column number. +** CAPI3REF: Obtaining SQL Function Parameter Values +** +** The C-language implementation of SQL functions and aggregates uses +** this set of interface routines to access the parameter values on +** the function or aggregate. +** +** The xFunc (for scalar functions) or xStep (for aggregates) parameters +** to [sqlite3_create_function()] and [sqlite3_create_function16()] +** define callbacks that implement the SQL functions and aggregates. +** The 4th parameter to these callbacks is an array of pointers to +** [sqlite3_value] objects. There is one [sqlite3_value] object for +** each parameter to the SQL function. These routines are used to +** extract values from the [sqlite3_value] objects. +** +** These routines work just like the corresponding +** [sqlite3_column_blob | sqlite3_column_* routines] except that +** these routines take a single [sqlite3_value*] pointer instead +** of an [sqlite3_stmt*] pointer and an integer column number. +** +** The sqlite3_value_text16() interface extracts a UTF16 string +** in the native byte-order of the host machine. The +** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces +** extract UTF16 strings as big-endian and little-endian respectively. +** +** The sqlite3_value_numeric_type() interface attempts to apply +** numeric affinity to the value. This means that an attempt is +** made to convert the value to an integer or floating point. If +** such a conversion is possible without loss of information (in order +** words if the value is original a string that looks like a number) +** then it is done. Otherwise no conversion occurs. The +** [SQLITE_INTEGER | datatype] after conversion is returned. +** +** Please pay particular attention to the fact that the pointer that +** is returned from [sqlite3_value_blob()], [sqlite3_value_text()], or +** [sqlite3_value_text16()] can be invalidated by a subsequent call to +** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite_value_text()], +** or [sqlite3_value_text16()]. */ const void *sqlite3_value_blob(sqlite3_value*); int sqlite3_value_bytes(sqlite3_value*); @@ -1142,45 +1738,60 @@ int sqlite3_value_numeric_type(sqlite3_v int sqlite3_value_numeric_type(sqlite3_value*); /* -** Aggregate functions use the following routine to allocate +** CAPI3REF: Obtain Aggregate Function Context +** +** The implementation of aggregate SQL functions use this routine to allocate ** a structure for storing their state. The first time this routine ** is called for a particular aggregate, a new structure of size nBytes ** is allocated, zeroed, and returned. On subsequent calls (for the ** same aggregate instance) the same buffer is returned. The implementation ** of the aggregate can use the returned buffer to accumulate data. ** -** The buffer allocated is freed automatically by SQLite. +** The buffer allocated is freed automatically by SQLite whan the aggregate +** query concludes. +** +** The first parameter should be a copy of the +** [sqlite3_context | SQL function context] that is the first +** parameter to the callback routine that implements the aggregate +** function. */ void *sqlite3_aggregate_context(sqlite3_context*, int nBytes); /* -** The pUserData parameter to the sqlite3_create_function() -** routine used to register user functions is available to +** CAPI3REF: User Data For Functions +** +** The pUserData parameter to the [sqlite3_create_function()] +** and [sqlite3_create_function16()] routines +** used to register user functions is available to ** the implementation of the function using this call. */ void *sqlite3_user_data(sqlite3_context*); /* -** The following two functions may be used by scalar user functions to +** CAPI3REF: Function Auxiliary Data +** +** The following two functions may be used by scalar SQL functions to ** associate meta-data with argument values. If the same value is passed to -** multiple invocations of the user-function during query execution, under +** multiple invocations of the same SQL function during query execution, under ** some circumstances the associated meta-data may be preserved. This may ** be used, for example, to add a regular-expression matching scalar ** function. The compiled version of the regular expression is stored as ** meta-data associated with the SQL value passed as the regular expression -** pattern. +** pattern. The compiled regular expression can be reused on multiple +** invocations of the same function so that the original pattern string +** does not need to be recompiled on each invocation. ** -** Calling sqlite3_get_auxdata() returns a pointer to the meta data -** associated with the Nth argument value to the current user function +** The sqlite3_get_auxdata() interface returns a pointer to the meta-data +** associated with the Nth argument value to the current SQL function ** call, where N is the second parameter. If no meta-data has been set for ** that value, then a NULL pointer is returned. ** -** The sqlite3_set_auxdata() is used to associate meta data with a user -** function argument. The third parameter is a pointer to the meta data +** The sqlite3_set_auxdata() is used to associate meta-data with an SQL +** function argument. The third parameter is a pointer to the meta-data ** to be associated with the Nth user function argument value. The fourth -** parameter specifies a 'delete function' that will be called on the meta -** data pointer to release it when it is no longer required. If the delete -** function pointer is NULL, it is not invoked. +** parameter specifies a destructor that will be called on the meta- +** data pointer to release it when it is no longer required. If the +** destructor is NULL, it is not invoked. ** ** In practice, meta-data is preserved between function calls for ** expressions that are constant at compile time. This includes literal @@ -1191,8 +1802,10 @@ void sqlite3_set_auxdata(sqlite3_context /* +** CAPI3REF: Constants Defining Special Destructor Behavior +** ** These are special value for the destructor that is passed in as the -** final argument to routines like sqlite3_result_blob(). If the destructor +** final argument to routines like [sqlite3_result_blob()]. If the destructor ** argument is SQLITE_STATIC, it means that the content pointer is constant ** and will never change. It does not need to be destroyed. The ** SQLITE_TRANSIENT value means that the content will likely change in @@ -1207,13 +1820,34 @@ typedef void (*sqlite3_destructor_type)( #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1) /* -** User-defined functions invoke the following routines in order to -** set their return value. +** CAPI3REF: Setting The Result Of An SQL Function +** +** These routines are used by the xFunc or xFinal callbacks that +** implement SQL functions and aggregates. See +** [sqlite3_create_function()] and [sqlite3_create_function16()] +** for additional information. +** +** These functions work very much like the +** [sqlite3_bind_blob | sqlite3_bind_*] family of functions used +** to bind values to host parameters in prepared statements. +** Refer to the +** [sqlite3_bind_blob | sqlite3_bind_* documentation] for +** additional information. +** +** The sqlite3_result_error() and sqlite3_result_error16() functions +** cause the implemented SQL function to throw an exception. The +** parameter to sqlite3_result_error() or sqlite3_result_error16() +** is the text of an error message. +** +** The sqlite3_result_toobig() cause the function implementation +** to throw and error indicating that a string or BLOB is to long +** to represent. */ void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*)); void sqlite3_result_double(sqlite3_context*, double); void sqlite3_result_error(sqlite3_context*, const char*, int); void sqlite3_result_error16(sqlite3_context*, const void*, int); +void sqlite3_result_error_toobig(sqlite3_context*); void sqlite3_result_int(sqlite3_context*, int); void sqlite3_result_int64(sqlite3_context*, sqlite_int64); void sqlite3_result_null(sqlite3_context*); @@ -1222,29 +1856,21 @@ void sqlite3_result_value(sqlite3_contex void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*)); void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*)); void sqlite3_result_value(sqlite3_context*, sqlite3_value*); +void sqlite3_result_zeroblob(sqlite3_context*, int n); /* -** These are the allowed values for the eTextRep argument to -** sqlite3_create_collation and sqlite3_create_function. -*/ -#define SQLITE_UTF8 1 -#define SQLITE_UTF16LE 2 -#define SQLITE_UTF16BE 3 -#define SQLITE_UTF16 4 /* Use native byte order */ -#define SQLITE_ANY 5 /* sqlite3_create_function only */ -#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ - -/* -** These two functions are used to add new collation sequences to the -** sqlite3 handle specified as the first argument. +** CAPI3REF: Define New Collating Sequences ** +** These functions are used to add new collation sequences to the +** [sqlite3*] handle specified as the first argument. +** ** The name of the new collation sequence is specified as a UTF-8 string -** for sqlite3_create_collation() and a UTF-16 string for -** sqlite3_create_collation16(). In both cases the name is passed as the -** second function argument. +** for sqlite3_create_collation() and sqlite3_create_collation_v2() +** and a UTF-16 string for sqlite3_create_collation16(). In all cases +** the name is passed as the second function argument. ** -** The third argument must be one of the constants SQLITE_UTF8, -** SQLITE_UTF16LE or SQLITE_UTF16BE, indicating that the user-supplied +** The third argument must be one of the constants [SQLITE_UTF8], +** [SQLITE_UTF16LE] or [SQLITE_UTF16BE], indicating that the user-supplied ** routine expects to be passed pointers to strings encoded using UTF-8, ** UTF-16 little-endian or UTF-16 big-endian respectively. ** @@ -1261,6 +1887,18 @@ void sqlite3_result_value(sqlite3_contex ** registered. The user routine should return negative, zero or positive if ** the first string is less than, equal to, or greater than the second ** string. i.e. (STRING1 - STRING2). +** +** The sqlite3_create_collation_v2() works like sqlite3_create_collation() +** excapt that it takes an extra argument which is a destructor for +** the collation. The destructor is called when the collation is +** destroyed and is passed a copy of the fourth parameter void* pointer +** of the sqlite3_create_collation_v2(). Collations are destroyed when +** they are overridden by later calls to the collation creation functions +** or when the [sqlite3*] database handle is closed using [sqlite3_close()]. +** +** The sqlite3_create_collation_v2() interface is experimental and +** subject to change in future releases. The other collation creation +** functions are stable. */ int sqlite3_create_collation( sqlite3*, @@ -1269,6 +1907,14 @@ int sqlite3_create_collation( void*, int(*xCompare)(void*,int,const void*,int,const void*) ); +int sqlite3_create_collation_v2( + sqlite3*, + const char *zName, + int eTextRep, + void*, + int(*xCompare)(void*,int,const void*,int,const void*), + void(*xDestroy)(void*) +); int sqlite3_create_collation16( sqlite3*, const char *zName, @@ -1278,6 +1924,8 @@ int sqlite3_create_collation16( ); /* +** CAPI3REF: Collation Needed Callbacks +** ** To avoid having to register all collation sequences before a database ** can be used, a single callback function may be registered with the ** database handle to be called whenever an undefined collation sequence is @@ -1289,17 +1937,17 @@ int sqlite3_create_collation16( ** are passed as UTF-16 in machine native byte order. A call to either ** function replaces any existing callback. ** -** When the user-function is invoked, the first argument passed is a copy +** When the callback is invoked, the first argument passed is a copy ** of the second argument to sqlite3_collation_needed() or ** sqlite3_collation_needed16(). The second argument is the database -** handle. The third argument is one of SQLITE_UTF8, SQLITE_UTF16BE or -** SQLITE_UTF16LE, indicating the most desirable form of the collation +** handle. The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE], or +** [SQLITE_UTF16LE], indicating the most desirable form of the collation ** sequence function required. The fourth parameter is the name of the ** required collation sequence. ** -** The collation sequence is returned to SQLite by a collation-needed -** callback using the sqlite3_create_collation() or -** sqlite3_create_collation16() APIs, described above. +** The callback function should register the desired collation using +** [sqlite3_create_collation()], [sqlite3_create_collation16()], or +** [sqlite3_create_collation_v2()]. */ int sqlite3_collation_needed( sqlite3*, @@ -1338,70 +1986,37 @@ int sqlite3_rekey( ); /* -** Sleep for a little while. The second parameter is the number of -** miliseconds to sleep for. +** CAPI3REF: Suspend Execution For A Short Time ** +** This function causes the current thread to suspect execution +** a number of milliseconds specified in its parameter. +** ** If the operating system does not support sleep requests with -** milisecond time resolution, then the time will be rounded up to -** the nearest second. The number of miliseconds of sleep actually +** millisecond time resolution, then the time will be rounded up to +** the nearest second. The number of milliseconds of sleep actually ** requested from the operating system is returned. */ int sqlite3_sleep(int); /* -** Return TRUE (non-zero) if the statement supplied as an argument needs -** to be recompiled. A statement needs to be recompiled whenever the -** execution environment changes in a way that would alter the program -** that sqlite3_prepare() generates. For example, if new functions or -** collating sequences are registered or if an authorizer function is -** added or changed. +** CAPI3REF: Name Of The Folder Holding Temporary Files ** -*/ -int sqlite3_expired(sqlite3_stmt*); - -/* -** Move all bindings from the first prepared statement over to the second. -** This routine is useful, for example, if the first prepared statement -** fails with an SQLITE_SCHEMA error. The same SQL can be prepared into -** the second prepared statement then all of the bindings transfered over -** to the second statement before the first statement is finalized. -*/ -int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*); - -/* -** If the following global variable is made to point to a -** string which is the name of a directory, then all temporary files +** If this global variable is made to point to a string which is +** the name of a folder (a.ka. directory), then all temporary files ** created by SQLite will be placed in that directory. If this variable ** is NULL pointer, then SQLite does a search for an appropriate temporary ** file directory. ** -** Once sqlite3_open() has been called, changing this variable will invalidate -** the current temporary database, if any. +** Once [sqlite3_open()] has been called, changing this variable will +** invalidate the current temporary database, if any. Generally speaking, +** it is not safe to invoke this routine after [sqlite3_open()] has +** been called. */ extern char *sqlite3_temp_directory; /* -** This function is called to recover from a malloc() failure that occured -** within the SQLite library. Normally, after a single malloc() fails the -** library refuses to function (all major calls return SQLITE_NOMEM). -** This function restores the library state so that it can be used again. +** CAPI3REF: Test To See If The Databse Is In Auto-Commit Mode ** -** All existing statements (sqlite3_stmt pointers) must be finalized or -** reset before this call is made. Otherwise, SQLITE_BUSY is returned. -** If any in-memory databases are in use, either as a main or TEMP -** database, SQLITE_ERROR is returned. In either of these cases, the -** library is not reset and remains unusable. -** -** This function is *not* threadsafe. Calling this from within a threaded -** application when threads other than the caller have used SQLite is -** dangerous and will almost certainly result in malfunctions. -** -** This functionality can be omitted from a build by defining the -** SQLITE_OMIT_GLOBALRECOVER at compile time. -*/ -int sqlite3_global_recover(void); - -/* ** Test to see whether or not the database connection is in autocommit ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on ** by default. Autocommit is disabled by a BEGIN statement and reenabled @@ -1410,14 +2025,45 @@ int sqlite3_get_autocommit(sqlite3*); int sqlite3_get_autocommit(sqlite3*); /* -** Return the sqlite3* database handle to which the prepared statement given -** in the argument belongs. This is the same database handle that was -** the first argument to the sqlite3_prepare() that was used to create -** the statement in the first place. +** CAPI3REF: Find The Database Handle Associated With A Prepared Statement +** +** Return the [sqlite3*] database handle to which a +** [sqlite3_stmt | prepared statement] belongs. +** This is the same database handle that was +** the first argument to the [sqlite3_prepare_v2()] or its variants +** that was used to create the statement in the first place. */ sqlite3 *sqlite3_db_handle(sqlite3_stmt*); + /* +** CAPI3REF: Commit And Rollback Notification Callbacks +** +** These routines +** register callback functions to be invoked whenever a transaction +** is committed or rolled back. The pArg argument is passed through +** to the callback. If the callback on a commit hook function +** returns non-zero, then the commit is converted into a rollback. +** +** If another function was previously registered, its pArg value is returned. +** Otherwise NULL is returned. +** +** Registering a NULL function disables the callback. +** +** For the purposes of this API, a transaction is said to have been +** rolled back if an explicit "ROLLBACK" statement is executed, or +** an error or constraint causes an implicit rollback to occur. The +** callback is not invoked if a transaction is automatically rolled +** back because the database connection is closed. +** +** These are experimental interfaces and are subject to change. +*/ +void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); +void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); + +/* +** CAPI3REF: Data Change Notification Callbacks +** ** Register a callback function with the database connection identified by the ** first argument to be invoked whenever a row is updated, inserted or deleted. ** Any callback set by a previous call to this function for the same @@ -1425,7 +2071,7 @@ sqlite3 *sqlite3_db_handle(sqlite3_stmt* ** ** The second argument is a pointer to the function to invoke when a ** row is updated, inserted or deleted. The first argument to the callback is -** a copy of the third argument to sqlite3_update_hook. The second callback +** a copy of the third argument to sqlite3_update_hook(). The second callback ** argument is one of SQLITE_INSERT, SQLITE_DELETE or SQLITE_UPDATE, depending ** on the operation that caused the callback to be invoked. The third and ** fourth arguments to the callback contain pointers to the database and @@ -1446,31 +2092,51 @@ void *sqlite3_update_hook( ); /* -** Register a callback to be invoked whenever a transaction is rolled -** back. +** CAPI3REF: Enable Or Disable Shared Pager Cache ** -** The new callback function overrides any existing rollback-hook -** callback. If there was an existing callback, then it's pArg value -** (the third argument to sqlite3_rollback_hook() when it was registered) -** is returned. Otherwise, NULL is returned. +** This routine enables or disables the sharing of the database cache +** and schema data structures between connections to the same database. +** Sharing is enabled if the argument is true and disabled if the argument +** is false. ** -** For the purposes of this API, a transaction is said to have been -** rolled back if an explicit "ROLLBACK" statement is executed, or -** an error or constraint causes an implicit rollback to occur. The -** callback is not invoked if a transaction is automatically rolled -** back because the database connection is closed. +** Cache sharing is enabled and disabled on a thread-by-thread basis. +** Each call to this routine enables or disables cache sharing only for +** connections created in the same thread in which this routine is called. +** There is no mechanism for sharing cache between database connections +** running in different threads. +** +** Sharing must be disabled prior to shutting down a thread or else +** the thread will leak memory. Call this routine with an argument of +** 0 to turn off sharing. Or use the sqlite3_thread_cleanup() API. +** +** This routine must not be called when any database connections +** are active in the current thread. Enabling or disabling shared +** cache while there are active database connections will result +** in memory corruption. +** +** When the shared cache is enabled, the +** following routines must always be called from the same thread: +** [sqlite3_open()], [sqlite3_prepare_v2()], [sqlite3_step()], +** [sqlite3_reset()], [sqlite3_finalize()], and [sqlite3_close()]. +** This is due to the fact that the shared cache makes use of +** thread-specific storage so that it will be available for sharing +** with other connections. +** +** Virtual tables cannot be used with a shared cache. When shared +** cache is enabled, the sqlite3_create_module() API used to register +** virtual tables will always return an error. +** +** This routine returns [SQLITE_OK] if shared cache was +** enabled or disabled successfully. An [SQLITE_ERROR | error code] +** is returned otherwise. +** +** Shared cache is disabled by default for backward compatibility. */ -void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); - -/* -** This function is only available if the library is compiled without -** the SQLITE_OMIT_SHARED_CACHE macro defined. It is used to enable or -** disable (if the argument is true or false, respectively) the -** "shared pager" feature. -*/ int sqlite3_enable_shared_cache(int); /* +** CAPI3REF: Attempt To Free Heap Memory +** ** Attempt to free N bytes of heap memory by deallocating non-essential ** memory allocations held by the database library (example: memory ** used to cache database pages to improve performance). @@ -1481,15 +2147,30 @@ int sqlite3_release_memory(int); int sqlite3_release_memory(int); /* +** CAPI3REF: Impose A Limit On Heap Size +** ** Place a "soft" limit on the amount of heap memory that may be allocated by ** SQLite within the current thread. If an internal allocation is requested -** that would exceed the specified limit, sqlite3_release_memory() is invoked +** that would exceed the specified limit, [sqlite3_release_memory()] is invoked ** one or more times to free up some space before the allocation is made. ** -** The limit is called "soft", because if sqlite3_release_memory() cannot free +** The limit is called "soft", because if [sqlite3_release_memory()] cannot free ** sufficient memory to prevent the limit from being exceeded, the memory is ** allocated anyway and the current operation proceeds. ** +** Prior to shutting down a thread sqlite3_soft_heap_limit() must be set to +** zero (the default) or else the thread will leak memory. Alternatively, use +** the [sqlite3_thread_cleanup()] API. +** +** A negative or zero value for N means that there is no soft heap limit and +** [sqlite3_release_memory()] will only be called when memory is exhaused. +** The default value for the soft heap limit is zero. +** +** SQLite makes a best effort to honor the soft heap limit. But if it +** is unable to reduce memory usage below the soft limit, execution will +** continue without error or notification. This is why the limit is +** called a "soft" limit. It is advisory only. +** ** This function is only available if the library was compiled with the ** SQLITE_ENABLE_MEMORY_MANAGEMENT option set. ** memory-management has been enabled. @@ -1497,6 +2178,8 @@ void sqlite3_soft_heap_limit(int); void sqlite3_soft_heap_limit(int); /* +** CAPI3REF: Clean Up Thread Local Storage +** ** This routine makes sure that all thread-local storage has been ** deallocated for the current thread. ** @@ -1510,7 +2193,10 @@ void sqlite3_thread_cleanup(void); void sqlite3_thread_cleanup(void); /* -** Return meta information about a specific column of a specific database +** CAPI3REF: Extract Metadata About A Column Of A Table +** +** This routine +** returns meta-data about a specific column of a specific database ** table accessible using the connection handle passed as the first function ** argument. ** @@ -1530,6 +2216,7 @@ void sqlite3_thread_cleanup(void); ** arguments may be NULL, in which case the corresponding element of meta ** information is ommitted. ** +**
 ** Parameter     Output Type      Description
 ** -----------------------------------
 **
@@ -1538,6 +2225,7 @@ void sqlite3_thread_cleanup(void);
 **   7th         int              True if the column has a NOT NULL constraint
 **   8th         int              True if the column is part of the PRIMARY KEY
 **   9th         int              True if the column is AUTOINCREMENT
+** 
** ** ** The memory pointed to by the character pointers returned for the @@ -1552,11 +2240,13 @@ void sqlite3_thread_cleanup(void); ** explicitly declared IPK column, then the output parameters are set as ** follows: ** +**
 **     data type: "INTEGER"
 **     collation sequence: "BINARY"
 **     not null: 0
 **     primary key: 1
 **     auto increment: 0
+** 
** ** This function may load one or more schemas from database files. If an ** error occurs during this process, or if the requested table or column @@ -1579,22 +2269,20 @@ int sqlite3_table_column_metadata( ); /* -****** EXPERIMENTAL - subject to change without notice ************** +** CAPI3REF: Load An Extension ** ** Attempt to load an SQLite extension library contained in the file ** zFile. The entry point is zProc. zProc may be 0 in which case the ** name of the entry point defaults to "sqlite3_extension_init". ** -** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong. +** Return [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong. ** ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with ** error message text. The calling function should free this memory -** by calling sqlite3_free(). +** by calling [sqlite3_free()]. ** -** Extension loading must be enabled using sqlite3_enable_load_extension() +** Extension loading must be enabled using [sqlite3_enable_load_extension()] ** prior to calling this API or an error will be returned. -** -****** EXPERIMENTAL - subject to change without notice ************** */ int sqlite3_load_extension( sqlite3 *db, /* Load the extension into this database connection */ @@ -1604,10 +2292,12 @@ int sqlite3_load_extension( ); /* +** CAPI3REF: Enable Or Disable Extension Loading +** ** So as not to open security holes in older applications that are -** unprepared to deal with extension load, and as a means of disabling -** extension loading while executing user-entered SQL, the following -** API is provided to turn the extension loading mechanism on and +** unprepared to deal with extension loading, and as a means of disabling +** extension loading while evaluating user-entered SQL, the following +** API is provided to turn the [sqlite3_load_extension()] mechanism on and ** off. It is off by default. See ticket #1863. ** ** Call this routine with onoff==1 to turn extension loading on @@ -1616,10 +2306,11 @@ int sqlite3_enable_load_extension(sqlite int sqlite3_enable_load_extension(sqlite3 *db, int onoff); /* -****** EXPERIMENTAL - subject to change without notice ************** +** CAPI3REF: Make Arrangements To Automatically Load An Extension ** ** Register an extension entry point that is automatically invoked -** whenever a new database connection is opened. +** whenever a new database connection is opened using +** [sqlite3_open()] or [sqlite3_open16()]. ** ** This API can be invoked at program startup in order to register ** one or more statically linked extensions that will be available @@ -1631,22 +2322,28 @@ int sqlite3_enable_load_extension(sqlite ** This routine stores a pointer to the extension in an array ** that is obtained from malloc(). If you run a memory leak ** checker on your program and it reports a leak because of this -** array, then invoke sqlite3_automatic_extension_reset() prior +** array, then invoke [sqlite3_automatic_extension_reset()] prior ** to shutdown to free the memory. ** ** Automatic extensions apply across all threads. +** +** This interface is experimental and is subject to change or +** removal in future releases of SQLite. */ int sqlite3_auto_extension(void *xEntryPoint); /* -****** EXPERIMENTAL - subject to change without notice ************** +** CAPI3REF: Reset Automatic Extension Loading ** ** Disable all previously registered automatic extensions. This -** routine undoes the effect of all prior sqlite3_automatic_extension() +** routine undoes the effect of all prior [sqlite3_automatic_extension()] ** calls. ** ** This call disabled automatic extensions in all threads. +** +** This interface is experimental and is subject to change or +** removal in future releases of SQLite. */ void sqlite3_reset_auto_extension(void); @@ -1872,6 +2569,103 @@ int sqlite3_overload_function(sqlite3*, */ /* +** CAPI3REF: A Handle To An Open BLOB +** +** An instance of the following opaque structure is used to +** represent an blob-handle. A blob-handle is created by +** [sqlite3_blob_open()] and destroyed by [sqlite3_blob_close()]. +** The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces +** can be used to read or write small subsections of the blob. +** The [sqltie3_blob_size()] interface returns the size of the +** blob in bytes. +*/ +typedef struct sqlite3_blob sqlite3_blob; + +/* +** CAPI3REF: Open A BLOB For Incremental I/O +** +** Open a handle to the blob located in row iRow,, column zColumn, +** table zTable in database zDb. i.e. the same blob that would +** be selected by: +** +**
+**     SELECT zColumn FROM zDb.zTable WHERE rowid = iRow;
+** 
+** +** If the flags parameter is non-zero, the blob is opened for +** read and write access. If it is zero, the blob is opened for read +** access. +** +** On success, [SQLITE_OK] is returned and the new +** [sqlite3_blob | blob handle] is written to *ppBlob. +** Otherwise an error code is returned and +** any value written to *ppBlob should not be used by the caller. +** This function sets the database-handle error code and message +** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()]. +*/ +int sqlite3_blob_open( + sqlite3*, + const char *zDb, + const char *zTable, + const char *zColumn, + sqlite_int64 iRow, + int flags, + sqlite3_blob **ppBlob +); + +/* +** CAPI3REF: Close A BLOB Handle +** +** Close an open [sqlite3_blob | blob handle]. +*/ +int sqlite3_blob_close(sqlite3_blob *); + +/* +** CAPI3REF: Return The Size Of An Open BLOB +** +** Return the size in bytes of the blob accessible via the open +** [sqlite3_blob | blob-handle] passed as an argument. +*/ +int sqlite3_blob_bytes(sqlite3_blob *); + +/* +** CAPI3REF: Read Data From A BLOB Incrementally +** +** This function is used to read data from an open +** [sqlite3_blob | blob-handle] into a caller supplied buffer. +** n bytes of data are copied into buffer +** z from the open blob, starting at offset iOffset. +** +** On success, SQLITE_OK is returned. Otherwise, an +** [SQLITE_ERROR | SQLite error code] or an +** [SQLITE_IOERR_READ | extended error code] is returned. +*/ +int sqlite3_blob_read(sqlite3_blob *, void *z, int n, int iOffset); + +/* +** CAPI3REF: Write Data Into A BLOB Incrementally +** +** This function is used to write data into an open +** [sqlite3_blob | blob-handle] from a user supplied buffer. +** n bytes of data are copied from the buffer +** pointed to by z into the open blob, starting at offset iOffset. +** +** If the [sqlite3_blob | blob-handle] passed as the first argument +** was not opened for writing (the flags parameter to [sqlite3_blob_open()] +*** was zero), this function returns [SQLITE_READONLY]. +** +** This function may only modify the contents of the blob, it is +** not possible to increase the size of a blob using this API. If +** offset iOffset is less than n bytes from the end of the blob, +** [SQLITE_ERROR] is returned and no data is written. +** +** On success, SQLITE_OK is returned. Otherwise, an +** [SQLITE_ERROR | SQLite error code] or an +** [SQLITE_IOERR_READ | extended error code] is returned. +*/ +int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset); + +/* ** Undo the hack that converts floating point types to integer for ** builds on processors without floating point support. */ ============================================================ --- sqlite/sqliteInt.h 047af0e4c38bbb8652836f72adc9e9199c51a1ba +++ sqlite/sqliteInt.h 208c40b6e11925a321ec159d889e0ec06b618359 @@ -11,11 +11,13 @@ ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.552 2007/04/16 15:06:25 danielk1977 Exp $ +** @(#) $Id: sqliteInt.h,v 1.572 2007/06/10 22:57:33 drh Exp $ */ #ifndef _SQLITEINT_H_ #define _SQLITEINT_H_ +#include "limits.h" + #if defined(SQLITE_TCL) || defined(TCLSH) # include #endif @@ -63,6 +65,10 @@ #include #include +#if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__) +# define isnan(X) ((X)!=(X)) +#endif + /* ** If compiling for a processor that lacks floating point support, ** substitute integer for floating-point @@ -75,30 +81,13 @@ # endif # define SQLITE_OMIT_DATETIME_FUNCS 1 # define SQLITE_OMIT_TRACE 1 +# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT #endif #ifndef SQLITE_BIG_DBL # define SQLITE_BIG_DBL (1e99) #endif /* -** The maximum number of in-memory pages to use for the main database -** table and for temporary tables. Internally, the MAX_PAGES and -** TEMP_PAGES macros are used. To override the default values at -** compilation time, the SQLITE_DEFAULT_CACHE_SIZE and -** SQLITE_DEFAULT_TEMP_CACHE_SIZE macros should be set. -*/ -#ifdef SQLITE_DEFAULT_CACHE_SIZE -# define MAX_PAGES SQLITE_DEFAULT_CACHE_SIZE -#else -# define MAX_PAGES 2000 -#endif -#ifdef SQLITE_DEFAULT_TEMP_CACHE_SIZE -# define TEMP_PAGES SQLITE_DEFAULT_TEMP_CACHE_SIZE -#else -# define TEMP_PAGES 500 -#endif - -/* ** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0 ** afterward. Having this macro allows us to cause the C compiler ** to omit code used by TEMP tables without messy #ifndef statements. @@ -124,20 +113,6 @@ #define NULL_DISTINCT_FOR_UNIQUE 1 /* -** The maximum number of attached databases. This must be at least 2 -** in order to support the main database file (0) and the file used to -** hold temporary tables (1). And it must be less than 32 because -** we use a bitmask of databases with a u32 in places (for example -** the Parse.cookieMask field). -*/ -#define MAX_ATTACHED 10 - -/* -** The maximum value of a ?nnn wildcard that the parser will accept. -*/ -#define SQLITE_MAX_VARIABLE_NUMBER 999 - -/* ** The "file format" number is an integer that is incremented whenever ** the VDBE-level file format changes. The following macros define the ** the default file format for new databases and the maximum file format @@ -283,10 +258,19 @@ extern int sqlite3_iLine; /* #endif +/* Variable sqlite3_mallocHasFailed is set to true after a malloc() +** failure occurs. +** +** The sqlite3MallocFailed() macro returns true if a malloc has failed +** in this thread since the last call to sqlite3ApiExit(), or false +** otherwise. +*/ +extern int sqlite3_mallocHasFailed; +#define sqlite3MallocFailed() (sqlite3_mallocHasFailed && sqlite3OsInMutex(1)) + #define sqliteFree(x) sqlite3FreeX(x) #define sqliteAllocSize(x) sqlite3AllocSize(x) - /* ** An instance of this structure might be allocated to store information ** specific to a single thread. @@ -362,6 +346,8 @@ typedef struct WhereLevel WhereLevel; typedef struct WhereInfo WhereInfo; typedef struct WhereLevel WhereLevel; +#include "os.h" + /* ** Each database file to be accessed by the system is an instance ** of the following structure. There are normally two of these structures @@ -645,11 +631,12 @@ struct CollSeq { ** collating sequence may not be read or written. */ struct CollSeq { - char *zName; /* Name of the collating sequence, UTF-8 encoded */ - u8 enc; /* Text encoding handled by xCmp() */ - u8 type; /* One of the SQLITE_COLL_... values below */ - void *pUser; /* First argument to xCmp() */ + char *zName; /* Name of the collating sequence, UTF-8 encoded */ + u8 enc; /* Text encoding handled by xCmp() */ + u8 type; /* One of the SQLITE_COLL_... values below */ + void *pUser; /* First argument to xCmp() */ int (*xCmp)(void*,int, const void*, int, const void*); + void (*xDel)(void*); /* Destructor for pUser */ }; /* @@ -1026,6 +1013,9 @@ struct Expr { ** right side of " IN (