| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.7.0. By combining all the individual C code files into this |
| 3 | +** version 3.7.1. By combining all the individual C code files into this |
| 4 | 4 | ** single large file, the entire code can be compiled as a one translation |
| 5 | 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | 7 | ** of 5% are more are commonly seen when SQLite is compiled as a single |
| 8 | 8 | ** translation unit. |
| | @@ -641,13 +641,13 @@ |
| 641 | 641 | ** |
| 642 | 642 | ** See also: [sqlite3_libversion()], |
| 643 | 643 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 644 | 644 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 645 | 645 | */ |
| 646 | | -#define SQLITE_VERSION "3.7.0" |
| 647 | | -#define SQLITE_VERSION_NUMBER 3007000 |
| 648 | | -#define SQLITE_SOURCE_ID "2010-07-21 16:16:28 b36b105eab6fd3195f4bfba6cb5cda0f063b7460" |
| 646 | +#define SQLITE_VERSION "3.7.1" |
| 647 | +#define SQLITE_VERSION_NUMBER 3007001 |
| 648 | +#define SQLITE_SOURCE_ID "2010-08-05 03:21:40 fbe70e1106bcc5086ceb9d8f39cc39baf3643092" |
| 649 | 649 | |
| 650 | 650 | /* |
| 651 | 651 | ** CAPI3REF: Run-Time Library Version Numbers |
| 652 | 652 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 653 | 653 | ** |
| | @@ -2710,11 +2710,17 @@ |
| 2710 | 2710 | ** contain a UTF-8 SQL comment that identifies the trigger.)^ |
| 2711 | 2711 | ** |
| 2712 | 2712 | ** ^The callback function registered by sqlite3_profile() is invoked |
| 2713 | 2713 | ** as each SQL statement finishes. ^The profile callback contains |
| 2714 | 2714 | ** the original statement text and an estimate of wall-clock time |
| 2715 | | -** of how long that statement took to run. |
| 2715 | +** of how long that statement took to run. ^The profile callback |
| 2716 | +** time is in units of nanoseconds, however the current implementation |
| 2717 | +** is only capable of millisecond resolution so the six least significant |
| 2718 | +** digits in the time are meaningless. Future versions of SQLite |
| 2719 | +** might provide greater resolution on the profiler callback. The |
| 2720 | +** sqlite3_profile() function is considered experimental and is |
| 2721 | +** subject to change in future versions of SQLite. |
| 2716 | 2722 | */ |
| 2717 | 2723 | SQLITE_API void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*); |
| 2718 | 2724 | SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*, |
| 2719 | 2725 | void(*xProfile)(void*,const char*,sqlite3_uint64), void*); |
| 2720 | 2726 | |
| | @@ -4859,22 +4865,25 @@ |
| 4859 | 4865 | |
| 4860 | 4866 | /* |
| 4861 | 4867 | ** CAPI3REF: Virtual Table Indexing Information |
| 4862 | 4868 | ** KEYWORDS: sqlite3_index_info |
| 4863 | 4869 | ** |
| 4864 | | -** The sqlite3_index_info structure and its substructures is used to |
| 4870 | +** The sqlite3_index_info structure and its substructures is used as part |
| 4871 | +** of the [virtual table] interface to |
| 4865 | 4872 | ** pass information into and receive the reply from the [xBestIndex] |
| 4866 | 4873 | ** method of a [virtual table module]. The fields under **Inputs** are the |
| 4867 | 4874 | ** inputs to xBestIndex and are read-only. xBestIndex inserts its |
| 4868 | 4875 | ** results into the **Outputs** fields. |
| 4869 | 4876 | ** |
| 4870 | 4877 | ** ^(The aConstraint[] array records WHERE clause constraints of the form: |
| 4871 | 4878 | ** |
| 4872 | | -** <pre>column OP expr</pre> |
| 4879 | +** <blockquote>column OP expr</blockquote> |
| 4873 | 4880 | ** |
| 4874 | 4881 | ** where OP is =, <, <=, >, or >=.)^ ^(The particular operator is |
| 4875 | | -** stored in aConstraint[].op.)^ ^(The index of the column is stored in |
| 4882 | +** stored in aConstraint[].op using one of the |
| 4883 | +** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^ |
| 4884 | +** ^(The index of the column is stored in |
| 4876 | 4885 | ** aConstraint[].iColumn.)^ ^(aConstraint[].usable is TRUE if the |
| 4877 | 4886 | ** expr on the right-hand side can be evaluated (and thus the constraint |
| 4878 | 4887 | ** is usable) and false if it cannot.)^ |
| 4879 | 4888 | ** |
| 4880 | 4889 | ** ^The optimizer automatically inverts terms of the form "expr OP column" |
| | @@ -4930,10 +4939,19 @@ |
| 4930 | 4939 | char *idxStr; /* String, possibly obtained from sqlite3_malloc */ |
| 4931 | 4940 | int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */ |
| 4932 | 4941 | int orderByConsumed; /* True if output is already ordered */ |
| 4933 | 4942 | double estimatedCost; /* Estimated cost of using this index */ |
| 4934 | 4943 | }; |
| 4944 | + |
| 4945 | +/* |
| 4946 | +** CAPI3REF: Virtual Table Constraint Operator Codes |
| 4947 | +** |
| 4948 | +** These macros defined the allowed values for the |
| 4949 | +** [sqlite3_index_info].aConstraint[].op field. Each value represents |
| 4950 | +** an operator that is part of a constraint term in the wHERE clause of |
| 4951 | +** a query that uses a [virtual table]. |
| 4952 | +*/ |
| 4935 | 4953 | #define SQLITE_INDEX_CONSTRAINT_EQ 2 |
| 4936 | 4954 | #define SQLITE_INDEX_CONSTRAINT_GT 4 |
| 4937 | 4955 | #define SQLITE_INDEX_CONSTRAINT_LE 8 |
| 4938 | 4956 | #define SQLITE_INDEX_CONSTRAINT_LT 16 |
| 4939 | 4957 | #define SQLITE_INDEX_CONSTRAINT_GE 32 |
| | @@ -5670,10 +5688,13 @@ |
| 5670 | 5688 | ** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their |
| 5671 | 5689 | ** internal equivalents). Only the value returned in the |
| 5672 | 5690 | ** *pHighwater parameter to [sqlite3_status()] is of interest. |
| 5673 | 5691 | ** The value written into the *pCurrent parameter is undefined.</dd>)^ |
| 5674 | 5692 | ** |
| 5693 | +** ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt> |
| 5694 | +** <dd>This parameter records the number of separate memory allocations.</dd>)^ |
| 5695 | +** |
| 5675 | 5696 | ** ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt> |
| 5676 | 5697 | ** <dd>This parameter returns the number of pages used out of the |
| 5677 | 5698 | ** [pagecache memory allocator] that was configured using |
| 5678 | 5699 | ** [SQLITE_CONFIG_PAGECACHE]. The |
| 5679 | 5700 | ** value returned is in pages, not in bytes.</dd>)^ |
| | @@ -5731,10 +5752,11 @@ |
| 5731 | 5752 | #define SQLITE_STATUS_SCRATCH_OVERFLOW 4 |
| 5732 | 5753 | #define SQLITE_STATUS_MALLOC_SIZE 5 |
| 5733 | 5754 | #define SQLITE_STATUS_PARSER_STACK 6 |
| 5734 | 5755 | #define SQLITE_STATUS_PAGECACHE_SIZE 7 |
| 5735 | 5756 | #define SQLITE_STATUS_SCRATCH_SIZE 8 |
| 5757 | +#define SQLITE_STATUS_MALLOC_COUNT 9 |
| 5736 | 5758 | |
| 5737 | 5759 | /* |
| 5738 | 5760 | ** CAPI3REF: Database Connection Status |
| 5739 | 5761 | ** |
| 5740 | 5762 | ** ^This interface is used to retrieve runtime status information |
| | @@ -5770,20 +5792,37 @@ |
| 5770 | 5792 | ** <dl> |
| 5771 | 5793 | ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt> |
| 5772 | 5794 | ** <dd>This parameter returns the number of lookaside memory slots currently |
| 5773 | 5795 | ** checked out.</dd>)^ |
| 5774 | 5796 | ** |
| 5775 | | -** <dt>SQLITE_DBSTATUS_CACHE_USED</dt> |
| 5776 | | -** <dd>^This parameter returns the approximate number of of bytes of heap |
| 5777 | | -** memory used by all pager caches associated with the database connection. |
| 5797 | +** ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt> |
| 5798 | +** <dd>This parameter returns the approximate number of of bytes of heap |
| 5799 | +** memory used by all pager caches associated with the database connection.)^ |
| 5778 | 5800 | ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0. |
| 5801 | +** |
| 5802 | +** ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt> |
| 5803 | +** <dd>This parameter returns the approximate number of of bytes of heap |
| 5804 | +** memory used to store the schema for all databases associated |
| 5805 | +** with the connection - main, temp, and any [ATTACH]-ed databases.)^ |
| 5806 | +** ^The full amount of memory used by the schemas is reported, even if the |
| 5807 | +** schema memory is shared with other database connections due to |
| 5808 | +** [shared cache mode] being enabled. |
| 5809 | +** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0. |
| 5810 | +** |
| 5811 | +** ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt> |
| 5812 | +** <dd>This parameter returns the approximate number of of bytes of heap |
| 5813 | +** and lookaside memory used by all prepared statements associated with |
| 5814 | +** the database connection.)^ |
| 5815 | +** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0. |
| 5779 | 5816 | ** </dd> |
| 5780 | 5817 | ** </dl> |
| 5781 | 5818 | */ |
| 5782 | 5819 | #define SQLITE_DBSTATUS_LOOKASIDE_USED 0 |
| 5783 | 5820 | #define SQLITE_DBSTATUS_CACHE_USED 1 |
| 5784 | | -#define SQLITE_DBSTATUS_MAX 1 /* Largest defined DBSTATUS */ |
| 5821 | +#define SQLITE_DBSTATUS_SCHEMA_USED 2 |
| 5822 | +#define SQLITE_DBSTATUS_STMT_USED 3 |
| 5823 | +#define SQLITE_DBSTATUS_MAX 3 /* Largest defined DBSTATUS */ |
| 5785 | 5824 | |
| 5786 | 5825 | |
| 5787 | 5826 | /* |
| 5788 | 5827 | ** CAPI3REF: Prepared Statement Status |
| 5789 | 5828 | ** |
| | @@ -7257,10 +7296,14 @@ |
| 7257 | 7296 | #ifdef SQLITE_TEST |
| 7258 | 7297 | SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int); |
| 7259 | 7298 | SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*); |
| 7260 | 7299 | #endif |
| 7261 | 7300 | |
| 7301 | +#ifndef SQLITE_OMIT_WAL |
| 7302 | +SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*); |
| 7303 | +#endif |
| 7304 | + |
| 7262 | 7305 | /* |
| 7263 | 7306 | ** If we are not using shared cache, then there is no need to |
| 7264 | 7307 | ** use mutexes to access the BtShared structures. So make the |
| 7265 | 7308 | ** Enter and Leave procedures no-ops. |
| 7266 | 7309 | */ |
| | @@ -7386,12 +7429,12 @@ |
| 7386 | 7429 | struct SubProgram { |
| 7387 | 7430 | VdbeOp *aOp; /* Array of opcodes for sub-program */ |
| 7388 | 7431 | int nOp; /* Elements in aOp[] */ |
| 7389 | 7432 | int nMem; /* Number of memory cells required */ |
| 7390 | 7433 | int nCsr; /* Number of cursors required */ |
| 7391 | | - int nRef; /* Number of pointers to this structure */ |
| 7392 | 7434 | void *token; /* id that may be used to recursive triggers */ |
| 7435 | + SubProgram *pNext; /* Next sub-program already visited */ |
| 7393 | 7436 | }; |
| 7394 | 7437 | |
| 7395 | 7438 | /* |
| 7396 | 7439 | ** A smaller version of VdbeOp used for the VdbeAddOpList() function because |
| 7397 | 7440 | ** it takes up less space. |
| | @@ -7675,10 +7718,11 @@ |
| 7675 | 7718 | SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int); |
| 7676 | 7719 | SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int); |
| 7677 | 7720 | SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*); |
| 7678 | 7721 | SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*); |
| 7679 | 7722 | SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*); |
| 7723 | +SQLITE_PRIVATE void sqlite3VdbeDeleteObject(sqlite3*,Vdbe*); |
| 7680 | 7724 | SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,int,int,int,int,int,int); |
| 7681 | 7725 | SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*); |
| 7682 | 7726 | SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int); |
| 7683 | 7727 | SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*); |
| 7684 | 7728 | #ifdef SQLITE_DEBUG |
| | @@ -7692,20 +7736,23 @@ |
| 7692 | 7736 | SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*); |
| 7693 | 7737 | SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*); |
| 7694 | 7738 | SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int); |
| 7695 | 7739 | SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*); |
| 7696 | 7740 | SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*); |
| 7697 | | -SQLITE_PRIVATE void sqlite3VdbeProgramDelete(sqlite3 *, SubProgram *, int); |
| 7698 | 7741 | SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetValue(Vdbe*, int, u8); |
| 7699 | 7742 | SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int); |
| 7700 | 7743 | #ifndef SQLITE_OMIT_TRACE |
| 7701 | 7744 | SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*); |
| 7702 | 7745 | #endif |
| 7703 | 7746 | |
| 7704 | 7747 | SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,char*,int); |
| 7705 | 7748 | SQLITE_PRIVATE void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord*); |
| 7706 | 7749 | SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*); |
| 7750 | + |
| 7751 | +#ifndef SQLITE_OMIT_TRIGGER |
| 7752 | +SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *); |
| 7753 | +#endif |
| 7707 | 7754 | |
| 7708 | 7755 | |
| 7709 | 7756 | #ifndef NDEBUG |
| 7710 | 7757 | SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe*, const char*, ...); |
| 7711 | 7758 | # define VdbeComment(X) sqlite3VdbeComment X |
| | @@ -7851,10 +7898,11 @@ |
| 7851 | 7898 | |
| 7852 | 7899 | /* Functions used to manage pager transactions and savepoints. */ |
| 7853 | 7900 | SQLITE_PRIVATE int sqlite3PagerPagecount(Pager*, int*); |
| 7854 | 7901 | SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int); |
| 7855 | 7902 | SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int); |
| 7903 | +SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*); |
| 7856 | 7904 | SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager); |
| 7857 | 7905 | SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*); |
| 7858 | 7906 | SQLITE_PRIVATE int sqlite3PagerRollback(Pager*); |
| 7859 | 7907 | SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n); |
| 7860 | 7908 | SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint); |
| | @@ -8442,18 +8490,10 @@ |
| 8442 | 8490 | Schema *pSchema; /* Pointer to database schema (possibly shared) */ |
| 8443 | 8491 | }; |
| 8444 | 8492 | |
| 8445 | 8493 | /* |
| 8446 | 8494 | ** An instance of the following structure stores a database schema. |
| 8447 | | -** |
| 8448 | | -** If there are no virtual tables configured in this schema, the |
| 8449 | | -** Schema.db variable is set to NULL. After the first virtual table |
| 8450 | | -** has been added, it is set to point to the database connection |
| 8451 | | -** used to create the connection. Once a virtual table has been |
| 8452 | | -** added to the Schema structure and the Schema.db variable populated, |
| 8453 | | -** only that database connection may use the Schema to prepare |
| 8454 | | -** statements. |
| 8455 | 8495 | */ |
| 8456 | 8496 | struct Schema { |
| 8457 | 8497 | int schema_cookie; /* Database schema version number for this file */ |
| 8458 | 8498 | Hash tblHash; /* All tables indexed by name */ |
| 8459 | 8499 | Hash idxHash; /* All (named) indices indexed by name */ |
| | @@ -8462,13 +8502,10 @@ |
| 8462 | 8502 | Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */ |
| 8463 | 8503 | u8 file_format; /* Schema format version for this file */ |
| 8464 | 8504 | u8 enc; /* Text encoding used by this database */ |
| 8465 | 8505 | u16 flags; /* Flags associated with this schema */ |
| 8466 | 8506 | int cache_size; /* Number of pages to use in the cache */ |
| 8467 | | -#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 8468 | | - sqlite3 *db; /* "Owner" connection. See comment above */ |
| 8469 | | -#endif |
| 8470 | 8507 | }; |
| 8471 | 8508 | |
| 8472 | 8509 | /* |
| 8473 | 8510 | ** These macros can be used to test, set, or clear bits in the |
| 8474 | 8511 | ** Db.pSchema->flags field. |
| | @@ -8652,10 +8689,11 @@ |
| 8652 | 8689 | Savepoint *pSavepoint; /* List of active savepoints */ |
| 8653 | 8690 | int nSavepoint; /* Number of non-transaction savepoints */ |
| 8654 | 8691 | int nStatement; /* Number of nested statement-transactions */ |
| 8655 | 8692 | u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */ |
| 8656 | 8693 | i64 nDeferredCons; /* Net deferred constraints this transaction. */ |
| 8694 | + int *pnBytesFreed; /* If not NULL, increment this in DbFree() */ |
| 8657 | 8695 | |
| 8658 | 8696 | #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY |
| 8659 | 8697 | /* The following variables are all protected by the STATIC_MASTER |
| 8660 | 8698 | ** mutex, not by sqlite3.mutex. They are used by code in notify.c. |
| 8661 | 8699 | ** |
| | @@ -9009,11 +9047,10 @@ |
| 9009 | 9047 | ** page number. Transient tables are used to hold the results of a |
| 9010 | 9048 | ** sub-query that appears instead of a real table name in the FROM clause |
| 9011 | 9049 | ** of a SELECT statement. |
| 9012 | 9050 | */ |
| 9013 | 9051 | struct Table { |
| 9014 | | - sqlite3 *dbMem; /* DB connection used for lookaside allocations. */ |
| 9015 | 9052 | char *zName; /* Name of the table or view */ |
| 9016 | 9053 | int iPKey; /* If not negative, use aCol[iPKey] as the primary key */ |
| 9017 | 9054 | int nCol; /* Number of columns in this table */ |
| 9018 | 9055 | Column *aCol; /* Information about each column */ |
| 9019 | 9056 | Index *pIndex; /* List of SQL indexes on this table. */ |
| | @@ -10118,11 +10155,11 @@ |
| 10118 | 10155 | char *zText; /* The string collected so far */ |
| 10119 | 10156 | int nChar; /* Length of the string so far */ |
| 10120 | 10157 | int nAlloc; /* Amount of space allocated in zText */ |
| 10121 | 10158 | int mxAlloc; /* Maximum allowed string length */ |
| 10122 | 10159 | u8 mallocFailed; /* Becomes true if any memory allocation fails */ |
| 10123 | | - u8 useMalloc; /* True if zText is enlargeable using realloc */ |
| 10160 | + u8 useMalloc; /* 0: none, 1: sqlite3DbMalloc, 2: sqlite3_malloc */ |
| 10124 | 10161 | u8 tooBig; /* Becomes true if string size exceeds limits */ |
| 10125 | 10162 | }; |
| 10126 | 10163 | |
| 10127 | 10164 | /* |
| 10128 | 10165 | ** A pointer to this structure is used to communicate information |
| | @@ -10416,11 +10453,11 @@ |
| 10416 | 10453 | #else |
| 10417 | 10454 | # define sqlite3ViewGetColumnNames(A,B) 0 |
| 10418 | 10455 | #endif |
| 10419 | 10456 | |
| 10420 | 10457 | SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int); |
| 10421 | | -SQLITE_PRIVATE void sqlite3DeleteTable(Table*); |
| 10458 | +SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*); |
| 10422 | 10459 | #ifndef SQLITE_OMIT_AUTOINCREMENT |
| 10423 | 10460 | SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse); |
| 10424 | 10461 | SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse); |
| 10425 | 10462 | #else |
| 10426 | 10463 | # define sqlite3AutoincrementBegin(X) |
| | @@ -10650,11 +10687,12 @@ |
| 10650 | 10687 | SQLITE_PRIVATE const char *sqlite3ErrStr(int); |
| 10651 | 10688 | SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse); |
| 10652 | 10689 | SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int); |
| 10653 | 10690 | SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName); |
| 10654 | 10691 | SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr); |
| 10655 | | -SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Parse *pParse, Expr *, Token *); |
| 10692 | +SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Expr*, CollSeq*); |
| 10693 | +SQLITE_PRIVATE Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr*, Token*); |
| 10656 | 10694 | SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *); |
| 10657 | 10695 | SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *); |
| 10658 | 10696 | SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int); |
| 10659 | 10697 | |
| 10660 | 10698 | SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8); |
| | @@ -10700,11 +10738,11 @@ |
| 10700 | 10738 | SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*); |
| 10701 | 10739 | SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*); |
| 10702 | 10740 | SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*); |
| 10703 | 10741 | SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *); |
| 10704 | 10742 | SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB); |
| 10705 | | -SQLITE_PRIVATE void sqlite3DeleteIndexSamples(Index*); |
| 10743 | +SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*); |
| 10706 | 10744 | SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*); |
| 10707 | 10745 | SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int); |
| 10708 | 10746 | SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*); |
| 10709 | 10747 | SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int); |
| 10710 | 10748 | SQLITE_PRIVATE void sqlite3SchemaFree(void *); |
| | @@ -10762,11 +10800,11 @@ |
| 10762 | 10800 | # define sqlite3VtabInSync(db) 0 |
| 10763 | 10801 | # define sqlite3VtabLock(X) |
| 10764 | 10802 | # define sqlite3VtabUnlock(X) |
| 10765 | 10803 | # define sqlite3VtabUnlockList(X) |
| 10766 | 10804 | #else |
| 10767 | | -SQLITE_PRIVATE void sqlite3VtabClear(Table*); |
| 10805 | +SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table*); |
| 10768 | 10806 | SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **); |
| 10769 | 10807 | SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db); |
| 10770 | 10808 | SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db); |
| 10771 | 10809 | SQLITE_PRIVATE void sqlite3VtabLock(VTable *); |
| 10772 | 10810 | SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *); |
| | @@ -10815,13 +10853,13 @@ |
| 10815 | 10853 | #define sqlite3FkDropTable(a,b,c) |
| 10816 | 10854 | #define sqlite3FkOldmask(a,b) 0 |
| 10817 | 10855 | #define sqlite3FkRequired(a,b,c,d) 0 |
| 10818 | 10856 | #endif |
| 10819 | 10857 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 10820 | | -SQLITE_PRIVATE void sqlite3FkDelete(Table*); |
| 10858 | +SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *, Table*); |
| 10821 | 10859 | #else |
| 10822 | | - #define sqlite3FkDelete(a) |
| 10860 | + #define sqlite3FkDelete(a,b) |
| 10823 | 10861 | #endif |
| 10824 | 10862 | |
| 10825 | 10863 | |
| 10826 | 10864 | /* |
| 10827 | 10865 | ** Available fault injectors. Should be numbered beginning with 0. |
| | @@ -10910,36 +10948,40 @@ |
| 10910 | 10948 | ** a single bit set. |
| 10911 | 10949 | ** |
| 10912 | 10950 | ** sqlite3MemdebugHasType() returns true if any of the bits in its second |
| 10913 | 10951 | ** argument match the type set by the previous sqlite3MemdebugSetType(). |
| 10914 | 10952 | ** sqlite3MemdebugHasType() is intended for use inside assert() statements. |
| 10915 | | -** For example: |
| 10916 | 10953 | ** |
| 10917 | | -** assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 10954 | +** sqlite3MemdebugNoType() returns true if none of the bits in its second |
| 10955 | +** argument match the type set by the previous sqlite3MemdebugSetType(). |
| 10918 | 10956 | ** |
| 10919 | 10957 | ** Perhaps the most important point is the difference between MEMTYPE_HEAP |
| 10920 | | -** and MEMTYPE_DB. If an allocation is MEMTYPE_DB, that means it might have |
| 10921 | | -** been allocated by lookaside, except the allocation was too large or |
| 10922 | | -** lookaside was already full. It is important to verify that allocations |
| 10923 | | -** that might have been satisfied by lookaside are not passed back to |
| 10924 | | -** non-lookaside free() routines. Asserts such as the example above are |
| 10925 | | -** placed on the non-lookaside free() routines to verify this constraint. |
| 10958 | +** and MEMTYPE_LOOKASIDE. If an allocation is MEMTYPE_LOOKASIDE, that means |
| 10959 | +** it might have been allocated by lookaside, except the allocation was |
| 10960 | +** too large or lookaside was already full. It is important to verify |
| 10961 | +** that allocations that might have been satisfied by lookaside are not |
| 10962 | +** passed back to non-lookaside free() routines. Asserts such as the |
| 10963 | +** example above are placed on the non-lookaside free() routines to verify |
| 10964 | +** this constraint. |
| 10926 | 10965 | ** |
| 10927 | 10966 | ** All of this is no-op for a production build. It only comes into |
| 10928 | 10967 | ** play when the SQLITE_MEMDEBUG compile-time option is used. |
| 10929 | 10968 | */ |
| 10930 | 10969 | #ifdef SQLITE_MEMDEBUG |
| 10931 | 10970 | SQLITE_PRIVATE void sqlite3MemdebugSetType(void*,u8); |
| 10932 | 10971 | SQLITE_PRIVATE int sqlite3MemdebugHasType(void*,u8); |
| 10972 | +SQLITE_PRIVATE int sqlite3MemdebugNoType(void*,u8); |
| 10933 | 10973 | #else |
| 10934 | 10974 | # define sqlite3MemdebugSetType(X,Y) /* no-op */ |
| 10935 | 10975 | # define sqlite3MemdebugHasType(X,Y) 1 |
| 10976 | +# define sqlite3MemdebugNoType(X,Y) 1 |
| 10936 | 10977 | #endif |
| 10937 | | -#define MEMTYPE_HEAP 0x01 /* General heap allocations */ |
| 10938 | | -#define MEMTYPE_DB 0x02 /* Associated with a database connection */ |
| 10939 | | -#define MEMTYPE_SCRATCH 0x04 /* Scratch allocations */ |
| 10940 | | -#define MEMTYPE_PCACHE 0x08 /* Page cache allocations */ |
| 10978 | +#define MEMTYPE_HEAP 0x01 /* General heap allocations */ |
| 10979 | +#define MEMTYPE_LOOKASIDE 0x02 /* Might have been lookaside memory */ |
| 10980 | +#define MEMTYPE_SCRATCH 0x04 /* Scratch allocations */ |
| 10981 | +#define MEMTYPE_PCACHE 0x08 /* Page cache allocations */ |
| 10982 | +#define MEMTYPE_DB 0x10 /* Uses sqlite3DbMalloc, not sqlite_malloc */ |
| 10941 | 10983 | |
| 10942 | 10984 | #endif /* _SQLITEINT_H_ */ |
| 10943 | 10985 | |
| 10944 | 10986 | /************** End of sqliteInt.h *******************************************/ |
| 10945 | 10987 | /************** Begin file global.c ******************************************/ |
| | @@ -11566,18 +11608,442 @@ |
| 11566 | 11608 | ************************************************************************* |
| 11567 | 11609 | ** |
| 11568 | 11610 | ** This module implements the sqlite3_status() interface and related |
| 11569 | 11611 | ** functionality. |
| 11570 | 11612 | */ |
| 11613 | +/************** Include vdbeInt.h in the middle of status.c ******************/ |
| 11614 | +/************** Begin file vdbeInt.h *****************************************/ |
| 11615 | +/* |
| 11616 | +** 2003 September 6 |
| 11617 | +** |
| 11618 | +** The author disclaims copyright to this source code. In place of |
| 11619 | +** a legal notice, here is a blessing: |
| 11620 | +** |
| 11621 | +** May you do good and not evil. |
| 11622 | +** May you find forgiveness for yourself and forgive others. |
| 11623 | +** May you share freely, never taking more than you give. |
| 11624 | +** |
| 11625 | +************************************************************************* |
| 11626 | +** This is the header file for information that is private to the |
| 11627 | +** VDBE. This information used to all be at the top of the single |
| 11628 | +** source code file "vdbe.c". When that file became too big (over |
| 11629 | +** 6000 lines long) it was split up into several smaller files and |
| 11630 | +** this header information was factored out. |
| 11631 | +*/ |
| 11632 | +#ifndef _VDBEINT_H_ |
| 11633 | +#define _VDBEINT_H_ |
| 11634 | + |
| 11635 | +/* |
| 11636 | +** SQL is translated into a sequence of instructions to be |
| 11637 | +** executed by a virtual machine. Each instruction is an instance |
| 11638 | +** of the following structure. |
| 11639 | +*/ |
| 11640 | +typedef struct VdbeOp Op; |
| 11641 | + |
| 11642 | +/* |
| 11643 | +** Boolean values |
| 11644 | +*/ |
| 11645 | +typedef unsigned char Bool; |
| 11646 | + |
| 11647 | +/* |
| 11648 | +** A cursor is a pointer into a single BTree within a database file. |
| 11649 | +** The cursor can seek to a BTree entry with a particular key, or |
| 11650 | +** loop over all entries of the Btree. You can also insert new BTree |
| 11651 | +** entries or retrieve the key or data from the entry that the cursor |
| 11652 | +** is currently pointing to. |
| 11653 | +** |
| 11654 | +** Every cursor that the virtual machine has open is represented by an |
| 11655 | +** instance of the following structure. |
| 11656 | +** |
| 11657 | +** If the VdbeCursor.isTriggerRow flag is set it means that this cursor is |
| 11658 | +** really a single row that represents the NEW or OLD pseudo-table of |
| 11659 | +** a row trigger. The data for the row is stored in VdbeCursor.pData and |
| 11660 | +** the rowid is in VdbeCursor.iKey. |
| 11661 | +*/ |
| 11662 | +struct VdbeCursor { |
| 11663 | + BtCursor *pCursor; /* The cursor structure of the backend */ |
| 11664 | + int iDb; /* Index of cursor database in db->aDb[] (or -1) */ |
| 11665 | + i64 lastRowid; /* Last rowid from a Next or NextIdx operation */ |
| 11666 | + Bool zeroed; /* True if zeroed out and ready for reuse */ |
| 11667 | + Bool rowidIsValid; /* True if lastRowid is valid */ |
| 11668 | + Bool atFirst; /* True if pointing to first entry */ |
| 11669 | + Bool useRandomRowid; /* Generate new record numbers semi-randomly */ |
| 11670 | + Bool nullRow; /* True if pointing to a row with no data */ |
| 11671 | + Bool deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */ |
| 11672 | + Bool isTable; /* True if a table requiring integer keys */ |
| 11673 | + Bool isIndex; /* True if an index containing keys only - no data */ |
| 11674 | + i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */ |
| 11675 | + Btree *pBt; /* Separate file holding temporary table */ |
| 11676 | + int pseudoTableReg; /* Register holding pseudotable content. */ |
| 11677 | + KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */ |
| 11678 | + int nField; /* Number of fields in the header */ |
| 11679 | + i64 seqCount; /* Sequence counter */ |
| 11680 | + sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */ |
| 11681 | + const sqlite3_module *pModule; /* Module for cursor pVtabCursor */ |
| 11682 | + |
| 11683 | + /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or |
| 11684 | + ** OP_IsUnique opcode on this cursor. */ |
| 11685 | + int seekResult; |
| 11686 | + |
| 11687 | + /* Cached information about the header for the data record that the |
| 11688 | + ** cursor is currently pointing to. Only valid if cacheStatus matches |
| 11689 | + ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of |
| 11690 | + ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that |
| 11691 | + ** the cache is out of date. |
| 11692 | + ** |
| 11693 | + ** aRow might point to (ephemeral) data for the current row, or it might |
| 11694 | + ** be NULL. |
| 11695 | + */ |
| 11696 | + u32 cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */ |
| 11697 | + int payloadSize; /* Total number of bytes in the record */ |
| 11698 | + u32 *aType; /* Type values for all entries in the record */ |
| 11699 | + u32 *aOffset; /* Cached offsets to the start of each columns data */ |
| 11700 | + u8 *aRow; /* Data for the current row, if all on one page */ |
| 11701 | +}; |
| 11702 | +typedef struct VdbeCursor VdbeCursor; |
| 11703 | + |
| 11704 | +/* |
| 11705 | +** When a sub-program is executed (OP_Program), a structure of this type |
| 11706 | +** is allocated to store the current value of the program counter, as |
| 11707 | +** well as the current memory cell array and various other frame specific |
| 11708 | +** values stored in the Vdbe struct. When the sub-program is finished, |
| 11709 | +** these values are copied back to the Vdbe from the VdbeFrame structure, |
| 11710 | +** restoring the state of the VM to as it was before the sub-program |
| 11711 | +** began executing. |
| 11712 | +** |
| 11713 | +** Frames are stored in a linked list headed at Vdbe.pParent. Vdbe.pParent |
| 11714 | +** is the parent of the current frame, or zero if the current frame |
| 11715 | +** is the main Vdbe program. |
| 11716 | +*/ |
| 11717 | +typedef struct VdbeFrame VdbeFrame; |
| 11718 | +struct VdbeFrame { |
| 11719 | + Vdbe *v; /* VM this frame belongs to */ |
| 11720 | + int pc; /* Program Counter */ |
| 11721 | + Op *aOp; /* Program instructions */ |
| 11722 | + int nOp; /* Size of aOp array */ |
| 11723 | + Mem *aMem; /* Array of memory cells */ |
| 11724 | + int nMem; /* Number of entries in aMem */ |
| 11725 | + VdbeCursor **apCsr; /* Element of Vdbe cursors */ |
| 11726 | + u16 nCursor; /* Number of entries in apCsr */ |
| 11727 | + void *token; /* Copy of SubProgram.token */ |
| 11728 | + int nChildMem; /* Number of memory cells for child frame */ |
| 11729 | + int nChildCsr; /* Number of cursors for child frame */ |
| 11730 | + i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */ |
| 11731 | + int nChange; /* Statement changes (Vdbe.nChanges) */ |
| 11732 | + VdbeFrame *pParent; /* Parent of this frame */ |
| 11733 | +}; |
| 11734 | + |
| 11735 | +#define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))]) |
| 11736 | + |
| 11737 | +/* |
| 11738 | +** A value for VdbeCursor.cacheValid that means the cache is always invalid. |
| 11739 | +*/ |
| 11740 | +#define CACHE_STALE 0 |
| 11741 | + |
| 11742 | +/* |
| 11743 | +** Internally, the vdbe manipulates nearly all SQL values as Mem |
| 11744 | +** structures. Each Mem struct may cache multiple representations (string, |
| 11745 | +** integer etc.) of the same value. A value (and therefore Mem structure) |
| 11746 | +** has the following properties: |
| 11747 | +** |
| 11748 | +** Each value has a manifest type. The manifest type of the value stored |
| 11749 | +** in a Mem struct is returned by the MemType(Mem*) macro. The type is |
| 11750 | +** one of SQLITE_NULL, SQLITE_INTEGER, SQLITE_REAL, SQLITE_TEXT or |
| 11751 | +** SQLITE_BLOB. |
| 11752 | +*/ |
| 11753 | +struct Mem { |
| 11754 | + union { |
| 11755 | + i64 i; /* Integer value. */ |
| 11756 | + int nZero; /* Used when bit MEM_Zero is set in flags */ |
| 11757 | + FuncDef *pDef; /* Used only when flags==MEM_Agg */ |
| 11758 | + RowSet *pRowSet; /* Used only when flags==MEM_RowSet */ |
| 11759 | + VdbeFrame *pFrame; /* Used when flags==MEM_Frame */ |
| 11760 | + } u; |
| 11761 | + double r; /* Real value */ |
| 11762 | + sqlite3 *db; /* The associated database connection */ |
| 11763 | + char *z; /* String or BLOB value */ |
| 11764 | + int n; /* Number of characters in string value, excluding '\0' */ |
| 11765 | + u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */ |
| 11766 | + u8 type; /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */ |
| 11767 | + u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */ |
| 11768 | + void (*xDel)(void *); /* If not null, call this function to delete Mem.z */ |
| 11769 | + char *zMalloc; /* Dynamic buffer allocated by sqlite3_malloc() */ |
| 11770 | +}; |
| 11771 | + |
| 11772 | +/* One or more of the following flags are set to indicate the validOK |
| 11773 | +** representations of the value stored in the Mem struct. |
| 11774 | +** |
| 11775 | +** If the MEM_Null flag is set, then the value is an SQL NULL value. |
| 11776 | +** No other flags may be set in this case. |
| 11777 | +** |
| 11778 | +** If the MEM_Str flag is set then Mem.z points at a string representation. |
| 11779 | +** Usually this is encoded in the same unicode encoding as the main |
| 11780 | +** database (see below for exceptions). If the MEM_Term flag is also |
| 11781 | +** set, then the string is nul terminated. The MEM_Int and MEM_Real |
| 11782 | +** flags may coexist with the MEM_Str flag. |
| 11783 | +** |
| 11784 | +** Multiple of these values can appear in Mem.flags. But only one |
| 11785 | +** at a time can appear in Mem.type. |
| 11786 | +*/ |
| 11787 | +#define MEM_Null 0x0001 /* Value is NULL */ |
| 11788 | +#define MEM_Str 0x0002 /* Value is a string */ |
| 11789 | +#define MEM_Int 0x0004 /* Value is an integer */ |
| 11790 | +#define MEM_Real 0x0008 /* Value is a real number */ |
| 11791 | +#define MEM_Blob 0x0010 /* Value is a BLOB */ |
| 11792 | +#define MEM_RowSet 0x0020 /* Value is a RowSet object */ |
| 11793 | +#define MEM_Frame 0x0040 /* Value is a VdbeFrame object */ |
| 11794 | +#define MEM_TypeMask 0x00ff /* Mask of type bits */ |
| 11795 | + |
| 11796 | +/* Whenever Mem contains a valid string or blob representation, one of |
| 11797 | +** the following flags must be set to determine the memory management |
| 11798 | +** policy for Mem.z. The MEM_Term flag tells us whether or not the |
| 11799 | +** string is \000 or \u0000 terminated |
| 11800 | +*/ |
| 11801 | +#define MEM_Term 0x0200 /* String rep is nul terminated */ |
| 11802 | +#define MEM_Dyn 0x0400 /* Need to call sqliteFree() on Mem.z */ |
| 11803 | +#define MEM_Static 0x0800 /* Mem.z points to a static string */ |
| 11804 | +#define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */ |
| 11805 | +#define MEM_Agg 0x2000 /* Mem.z points to an agg function context */ |
| 11806 | +#define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */ |
| 11807 | + |
| 11808 | +#ifdef SQLITE_OMIT_INCRBLOB |
| 11809 | + #undef MEM_Zero |
| 11810 | + #define MEM_Zero 0x0000 |
| 11811 | +#endif |
| 11812 | + |
| 11813 | + |
| 11814 | +/* |
| 11815 | +** Clear any existing type flags from a Mem and replace them with f |
| 11816 | +*/ |
| 11817 | +#define MemSetTypeFlag(p, f) \ |
| 11818 | + ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f) |
| 11819 | + |
| 11820 | + |
| 11821 | +/* A VdbeFunc is just a FuncDef (defined in sqliteInt.h) that contains |
| 11822 | +** additional information about auxiliary information bound to arguments |
| 11823 | +** of the function. This is used to implement the sqlite3_get_auxdata() |
| 11824 | +** and sqlite3_set_auxdata() APIs. The "auxdata" is some auxiliary data |
| 11825 | +** that can be associated with a constant argument to a function. This |
| 11826 | +** allows functions such as "regexp" to compile their constant regular |
| 11827 | +** expression argument once and reused the compiled code for multiple |
| 11828 | +** invocations. |
| 11829 | +*/ |
| 11830 | +struct VdbeFunc { |
| 11831 | + FuncDef *pFunc; /* The definition of the function */ |
| 11832 | + int nAux; /* Number of entries allocated for apAux[] */ |
| 11833 | + struct AuxData { |
| 11834 | + void *pAux; /* Aux data for the i-th argument */ |
| 11835 | + void (*xDelete)(void *); /* Destructor for the aux data */ |
| 11836 | + } apAux[1]; /* One slot for each function argument */ |
| 11837 | +}; |
| 11838 | + |
| 11839 | +/* |
| 11840 | +** The "context" argument for a installable function. A pointer to an |
| 11841 | +** instance of this structure is the first argument to the routines used |
| 11842 | +** implement the SQL functions. |
| 11843 | +** |
| 11844 | +** There is a typedef for this structure in sqlite.h. So all routines, |
| 11845 | +** even the public interface to SQLite, can use a pointer to this structure. |
| 11846 | +** But this file is the only place where the internal details of this |
| 11847 | +** structure are known. |
| 11848 | +** |
| 11849 | +** This structure is defined inside of vdbeInt.h because it uses substructures |
| 11850 | +** (Mem) which are only defined there. |
| 11851 | +*/ |
| 11852 | +struct sqlite3_context { |
| 11853 | + FuncDef *pFunc; /* Pointer to function information. MUST BE FIRST */ |
| 11854 | + VdbeFunc *pVdbeFunc; /* Auxilary data, if created. */ |
| 11855 | + Mem s; /* The return value is stored here */ |
| 11856 | + Mem *pMem; /* Memory cell used to store aggregate context */ |
| 11857 | + int isError; /* Error code returned by the function. */ |
| 11858 | + CollSeq *pColl; /* Collating sequence */ |
| 11859 | +}; |
| 11860 | + |
| 11861 | +/* |
| 11862 | +** A Set structure is used for quick testing to see if a value |
| 11863 | +** is part of a small set. Sets are used to implement code like |
| 11864 | +** this: |
| 11865 | +** x.y IN ('hi','hoo','hum') |
| 11866 | +*/ |
| 11867 | +typedef struct Set Set; |
| 11868 | +struct Set { |
| 11869 | + Hash hash; /* A set is just a hash table */ |
| 11870 | + HashElem *prev; /* Previously accessed hash elemen */ |
| 11871 | +}; |
| 11872 | + |
| 11873 | +/* |
| 11874 | +** An instance of the virtual machine. This structure contains the complete |
| 11875 | +** state of the virtual machine. |
| 11876 | +** |
| 11877 | +** The "sqlite3_stmt" structure pointer that is returned by sqlite3_compile() |
| 11878 | +** is really a pointer to an instance of this structure. |
| 11879 | +** |
| 11880 | +** The Vdbe.inVtabMethod variable is set to non-zero for the duration of |
| 11881 | +** any virtual table method invocations made by the vdbe program. It is |
| 11882 | +** set to 2 for xDestroy method calls and 1 for all other methods. This |
| 11883 | +** variable is used for two purposes: to allow xDestroy methods to execute |
| 11884 | +** "DROP TABLE" statements and to prevent some nasty side effects of |
| 11885 | +** malloc failure when SQLite is invoked recursively by a virtual table |
| 11886 | +** method function. |
| 11887 | +*/ |
| 11888 | +struct Vdbe { |
| 11889 | + sqlite3 *db; /* The database connection that owns this statement */ |
| 11890 | + Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */ |
| 11891 | + int nOp; /* Number of instructions in the program */ |
| 11892 | + int nOpAlloc; /* Number of slots allocated for aOp[] */ |
| 11893 | + Op *aOp; /* Space to hold the virtual machine's program */ |
| 11894 | + int nLabel; /* Number of labels used */ |
| 11895 | + int nLabelAlloc; /* Number of slots allocated in aLabel[] */ |
| 11896 | + int *aLabel; /* Space to hold the labels */ |
| 11897 | + Mem **apArg; /* Arguments to currently executing user function */ |
| 11898 | + Mem *aColName; /* Column names to return */ |
| 11899 | + Mem *pResultSet; /* Pointer to an array of results */ |
| 11900 | + u16 nResColumn; /* Number of columns in one row of the result set */ |
| 11901 | + u16 nCursor; /* Number of slots in apCsr[] */ |
| 11902 | + VdbeCursor **apCsr; /* One element of this array for each open cursor */ |
| 11903 | + u8 errorAction; /* Recovery action to do in case of an error */ |
| 11904 | + u8 okVar; /* True if azVar[] has been initialized */ |
| 11905 | + ynVar nVar; /* Number of entries in aVar[] */ |
| 11906 | + Mem *aVar; /* Values for the OP_Variable opcode. */ |
| 11907 | + char **azVar; /* Name of variables */ |
| 11908 | + u32 magic; /* Magic number for sanity checking */ |
| 11909 | + int nMem; /* Number of memory locations currently allocated */ |
| 11910 | + Mem *aMem; /* The memory locations */ |
| 11911 | + u32 cacheCtr; /* VdbeCursor row cache generation counter */ |
| 11912 | + int pc; /* The program counter */ |
| 11913 | + int rc; /* Value to return */ |
| 11914 | + char *zErrMsg; /* Error message written here */ |
| 11915 | + u8 explain; /* True if EXPLAIN present on SQL command */ |
| 11916 | + u8 changeCntOn; /* True to update the change-counter */ |
| 11917 | + u8 expired; /* True if the VM needs to be recompiled */ |
| 11918 | + u8 runOnlyOnce; /* Automatically expire on reset */ |
| 11919 | + u8 minWriteFileFormat; /* Minimum file format for writable database files */ |
| 11920 | + u8 inVtabMethod; /* See comments above */ |
| 11921 | + u8 usesStmtJournal; /* True if uses a statement journal */ |
| 11922 | + u8 readOnly; /* True for read-only statements */ |
| 11923 | + u8 isPrepareV2; /* True if prepared with prepare_v2() */ |
| 11924 | + int nChange; /* Number of db changes made since last reset */ |
| 11925 | + int btreeMask; /* Bitmask of db->aDb[] entries referenced */ |
| 11926 | + i64 startTime; /* Time when query started - used for profiling */ |
| 11927 | + BtreeMutexArray aMutex; /* An array of Btree used here and needing locks */ |
| 11928 | + int aCounter[3]; /* Counters used by sqlite3_stmt_status() */ |
| 11929 | + char *zSql; /* Text of the SQL statement that generated this */ |
| 11930 | + void *pFree; /* Free this when deleting the vdbe */ |
| 11931 | + i64 nFkConstraint; /* Number of imm. FK constraints this VM */ |
| 11932 | + i64 nStmtDefCons; /* Number of def. constraints when stmt started */ |
| 11933 | + int iStatement; /* Statement number (or 0 if has not opened stmt) */ |
| 11934 | +#ifdef SQLITE_DEBUG |
| 11935 | + FILE *trace; /* Write an execution trace here, if not NULL */ |
| 11936 | +#endif |
| 11937 | + VdbeFrame *pFrame; /* Parent frame */ |
| 11938 | + int nFrame; /* Number of frames in pFrame list */ |
| 11939 | + u32 expmask; /* Binding to these vars invalidates VM */ |
| 11940 | + SubProgram *pProgram; /* Linked list of all sub-programs used by VM */ |
| 11941 | +}; |
| 11942 | + |
| 11943 | +/* |
| 11944 | +** The following are allowed values for Vdbe.magic |
| 11945 | +*/ |
| 11946 | +#define VDBE_MAGIC_INIT 0x26bceaa5 /* Building a VDBE program */ |
| 11947 | +#define VDBE_MAGIC_RUN 0xbdf20da3 /* VDBE is ready to execute */ |
| 11948 | +#define VDBE_MAGIC_HALT 0x519c2973 /* VDBE has completed execution */ |
| 11949 | +#define VDBE_MAGIC_DEAD 0xb606c3c8 /* The VDBE has been deallocated */ |
| 11950 | + |
| 11951 | +/* |
| 11952 | +** Function prototypes |
| 11953 | +*/ |
| 11954 | +SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*); |
| 11955 | +void sqliteVdbePopStack(Vdbe*,int); |
| 11956 | +SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*); |
| 11957 | +#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 11958 | +SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*); |
| 11959 | +#endif |
| 11960 | +SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32); |
| 11961 | +SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int); |
| 11962 | +SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, int, Mem*, int); |
| 11963 | +SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*); |
| 11964 | +SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc*, int); |
| 11965 | + |
| 11966 | +int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *); |
| 11967 | +SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*); |
| 11968 | +SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *); |
| 11969 | +SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*); |
| 11970 | +SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*); |
| 11971 | +SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*); |
| 11972 | +SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*); |
| 11973 | +SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int); |
| 11974 | +SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*); |
| 11975 | +SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*); |
| 11976 | +SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int); |
| 11977 | +SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*); |
| 11978 | +SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*); |
| 11979 | +SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*)); |
| 11980 | +SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64); |
| 11981 | +#ifdef SQLITE_OMIT_FLOATING_POINT |
| 11982 | +# define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64 |
| 11983 | +#else |
| 11984 | +SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem*, double); |
| 11985 | +#endif |
| 11986 | +SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*); |
| 11987 | +SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int); |
| 11988 | +SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*); |
| 11989 | +SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*); |
| 11990 | +SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, int); |
| 11991 | +SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*); |
| 11992 | +SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*); |
| 11993 | +SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*); |
| 11994 | +SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*); |
| 11995 | +SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*); |
| 11996 | +SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*); |
| 11997 | +SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*); |
| 11998 | +SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p); |
| 11999 | +SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p); |
| 12000 | +SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*); |
| 12001 | +SQLITE_PRIVATE const char *sqlite3OpcodeName(int); |
| 12002 | +SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve); |
| 12003 | +SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int); |
| 12004 | +SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*); |
| 12005 | +SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *); |
| 12006 | +SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem); |
| 12007 | + |
| 12008 | +#ifndef SQLITE_OMIT_FOREIGN_KEY |
| 12009 | +SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int); |
| 12010 | +#else |
| 12011 | +# define sqlite3VdbeCheckFk(p,i) 0 |
| 12012 | +#endif |
| 12013 | + |
| 12014 | +#ifndef SQLITE_OMIT_SHARED_CACHE |
| 12015 | +SQLITE_PRIVATE void sqlite3VdbeMutexArrayEnter(Vdbe *p); |
| 12016 | +#else |
| 12017 | +# define sqlite3VdbeMutexArrayEnter(p) |
| 12018 | +#endif |
| 12019 | + |
| 12020 | +SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8); |
| 12021 | +#ifdef SQLITE_DEBUG |
| 12022 | +SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe*); |
| 12023 | +SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf); |
| 12024 | +#endif |
| 12025 | +SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem); |
| 12026 | + |
| 12027 | +#ifndef SQLITE_OMIT_INCRBLOB |
| 12028 | +SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *); |
| 12029 | +#else |
| 12030 | + #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK |
| 12031 | +#endif |
| 12032 | + |
| 12033 | +#endif /* !defined(_VDBEINT_H_) */ |
| 12034 | + |
| 12035 | +/************** End of vdbeInt.h *********************************************/ |
| 12036 | +/************** Continuing where we left off in status.c *********************/ |
| 11571 | 12037 | |
| 11572 | 12038 | /* |
| 11573 | 12039 | ** Variables in which to record status information. |
| 11574 | 12040 | */ |
| 11575 | 12041 | typedef struct sqlite3StatType sqlite3StatType; |
| 11576 | 12042 | static SQLITE_WSD struct sqlite3StatType { |
| 11577 | | - int nowValue[9]; /* Current value */ |
| 11578 | | - int mxValue[9]; /* Maximum value */ |
| 12043 | + int nowValue[10]; /* Current value */ |
| 12044 | + int mxValue[10]; /* Maximum value */ |
| 11579 | 12045 | } sqlite3Stat = { {0,}, {0,} }; |
| 11580 | 12046 | |
| 11581 | 12047 | |
| 11582 | 12048 | /* The "wsdStat" macro will resolve to the status information |
| 11583 | 12049 | ** state vector. If writable static data is unsupported on the target, |
| | @@ -11655,10 +12121,12 @@ |
| 11655 | 12121 | int op, /* Status verb */ |
| 11656 | 12122 | int *pCurrent, /* Write current value here */ |
| 11657 | 12123 | int *pHighwater, /* Write high-water mark here */ |
| 11658 | 12124 | int resetFlag /* Reset high-water mark if true */ |
| 11659 | 12125 | ){ |
| 12126 | + int rc = SQLITE_OK; /* Return code */ |
| 12127 | + sqlite3_mutex_enter(db->mutex); |
| 11660 | 12128 | switch( op ){ |
| 11661 | 12129 | case SQLITE_DBSTATUS_LOOKASIDE_USED: { |
| 11662 | 12130 | *pCurrent = db->lookaside.nOut; |
| 11663 | 12131 | *pHighwater = db->lookaside.mxOut; |
| 11664 | 12132 | if( resetFlag ){ |
| | @@ -11673,26 +12141,92 @@ |
| 11673 | 12141 | ** highwater mark is meaningless and is returned as zero. |
| 11674 | 12142 | */ |
| 11675 | 12143 | case SQLITE_DBSTATUS_CACHE_USED: { |
| 11676 | 12144 | int totalUsed = 0; |
| 11677 | 12145 | int i; |
| 12146 | + sqlite3BtreeEnterAll(db); |
| 11678 | 12147 | for(i=0; i<db->nDb; i++){ |
| 11679 | 12148 | Btree *pBt = db->aDb[i].pBt; |
| 11680 | 12149 | if( pBt ){ |
| 11681 | 12150 | Pager *pPager = sqlite3BtreePager(pBt); |
| 11682 | 12151 | totalUsed += sqlite3PagerMemUsed(pPager); |
| 11683 | 12152 | } |
| 11684 | 12153 | } |
| 12154 | + sqlite3BtreeLeaveAll(db); |
| 11685 | 12155 | *pCurrent = totalUsed; |
| 11686 | 12156 | *pHighwater = 0; |
| 11687 | 12157 | break; |
| 11688 | 12158 | } |
| 12159 | + |
| 12160 | + /* |
| 12161 | + ** *pCurrent gets an accurate estimate of the amount of memory used |
| 12162 | + ** to store the schema for all databases (main, temp, and any ATTACHed |
| 12163 | + ** databases. *pHighwater is set to zero. |
| 12164 | + */ |
| 12165 | + case SQLITE_DBSTATUS_SCHEMA_USED: { |
| 12166 | + int i; /* Used to iterate through schemas */ |
| 12167 | + int nByte = 0; /* Used to accumulate return value */ |
| 12168 | + |
| 12169 | + db->pnBytesFreed = &nByte; |
| 12170 | + for(i=0; i<db->nDb; i++){ |
| 12171 | + Schema *pSchema = db->aDb[i].pSchema; |
| 12172 | + if( ALWAYS(pSchema!=0) ){ |
| 12173 | + HashElem *p; |
| 12174 | + |
| 12175 | + nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * ( |
| 12176 | + pSchema->tblHash.count |
| 12177 | + + pSchema->trigHash.count |
| 12178 | + + pSchema->idxHash.count |
| 12179 | + + pSchema->fkeyHash.count |
| 12180 | + ); |
| 12181 | + nByte += sqlite3MallocSize(pSchema->tblHash.ht); |
| 12182 | + nByte += sqlite3MallocSize(pSchema->trigHash.ht); |
| 12183 | + nByte += sqlite3MallocSize(pSchema->idxHash.ht); |
| 12184 | + nByte += sqlite3MallocSize(pSchema->fkeyHash.ht); |
| 12185 | + |
| 12186 | + for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){ |
| 12187 | + sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p)); |
| 12188 | + } |
| 12189 | + for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ |
| 12190 | + sqlite3DeleteTable(db, (Table *)sqliteHashData(p)); |
| 12191 | + } |
| 12192 | + } |
| 12193 | + } |
| 12194 | + db->pnBytesFreed = 0; |
| 12195 | + |
| 12196 | + *pHighwater = 0; |
| 12197 | + *pCurrent = nByte; |
| 12198 | + break; |
| 12199 | + } |
| 12200 | + |
| 12201 | + /* |
| 12202 | + ** *pCurrent gets an accurate estimate of the amount of memory used |
| 12203 | + ** to store all prepared statements. |
| 12204 | + ** *pHighwater is set to zero. |
| 12205 | + */ |
| 12206 | + case SQLITE_DBSTATUS_STMT_USED: { |
| 12207 | + struct Vdbe *pVdbe; /* Used to iterate through VMs */ |
| 12208 | + int nByte = 0; /* Used to accumulate return value */ |
| 12209 | + |
| 12210 | + db->pnBytesFreed = &nByte; |
| 12211 | + for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){ |
| 12212 | + sqlite3VdbeDeleteObject(db, pVdbe); |
| 12213 | + } |
| 12214 | + db->pnBytesFreed = 0; |
| 12215 | + |
| 12216 | + *pHighwater = 0; |
| 12217 | + *pCurrent = nByte; |
| 12218 | + |
| 12219 | + break; |
| 12220 | + } |
| 12221 | + |
| 11689 | 12222 | default: { |
| 11690 | | - return SQLITE_ERROR; |
| 12223 | + rc = SQLITE_ERROR; |
| 11691 | 12224 | } |
| 11692 | 12225 | } |
| 11693 | | - return SQLITE_OK; |
| 12226 | + sqlite3_mutex_leave(db->mutex); |
| 12227 | + return rc; |
| 11694 | 12228 | } |
| 11695 | 12229 | |
| 11696 | 12230 | /************** End of status.c **********************************************/ |
| 11697 | 12231 | /************** Begin file date.c ********************************************/ |
| 11698 | 12232 | /* |
| | @@ -13820,23 +14354,38 @@ |
| 13820 | 14354 | int rc = 1; |
| 13821 | 14355 | if( p ){ |
| 13822 | 14356 | struct MemBlockHdr *pHdr; |
| 13823 | 14357 | pHdr = sqlite3MemsysGetHeader(p); |
| 13824 | 14358 | assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ |
| 13825 | | - assert( (pHdr->eType & (pHdr->eType-1))==0 ); /* Only one type bit set */ |
| 13826 | 14359 | if( (pHdr->eType&eType)==0 ){ |
| 13827 | | - void **pBt; |
| 13828 | | - pBt = (void**)pHdr; |
| 13829 | | - pBt -= pHdr->nBacktraceSlots; |
| 13830 | | - backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(stderr)); |
| 13831 | | - fprintf(stderr, "\n"); |
| 14360 | + rc = 0; |
| 14361 | + } |
| 14362 | + } |
| 14363 | + return rc; |
| 14364 | +} |
| 14365 | + |
| 14366 | +/* |
| 14367 | +** Return TRUE if the mask of type in eType matches no bits of the type of the |
| 14368 | +** allocation p. Also return true if p==NULL. |
| 14369 | +** |
| 14370 | +** This routine is designed for use within an assert() statement, to |
| 14371 | +** verify the type of an allocation. For example: |
| 14372 | +** |
| 14373 | +** assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) ); |
| 14374 | +*/ |
| 14375 | +SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){ |
| 14376 | + int rc = 1; |
| 14377 | + if( p ){ |
| 14378 | + struct MemBlockHdr *pHdr; |
| 14379 | + pHdr = sqlite3MemsysGetHeader(p); |
| 14380 | + assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ |
| 14381 | + if( (pHdr->eType&eType)!=0 ){ |
| 13832 | 14382 | rc = 0; |
| 13833 | 14383 | } |
| 13834 | 14384 | } |
| 13835 | 14385 | return rc; |
| 13836 | 14386 | } |
| 13837 | | - |
| 13838 | 14387 | |
| 13839 | 14388 | /* |
| 13840 | 14389 | ** Set the number of backtrace levels kept for each allocation. |
| 13841 | 14390 | ** A value of zero turns off backtracing. The number is always rounded |
| 13842 | 14391 | ** up to a multiple of 2. |
| | @@ -16773,10 +17322,11 @@ |
| 16773 | 17322 | p = sqlite3GlobalConfig.m.xMalloc(nFull); |
| 16774 | 17323 | } |
| 16775 | 17324 | if( p ){ |
| 16776 | 17325 | nFull = sqlite3MallocSize(p); |
| 16777 | 17326 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull); |
| 17327 | + sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1); |
| 16778 | 17328 | } |
| 16779 | 17329 | *pp = p; |
| 16780 | 17330 | return nFull; |
| 16781 | 17331 | } |
| 16782 | 17332 | |
| | @@ -16890,16 +17440,18 @@ |
| 16890 | 17440 | if( p ){ |
| 16891 | 17441 | if( sqlite3GlobalConfig.pScratch==0 |
| 16892 | 17442 | || p<sqlite3GlobalConfig.pScratch |
| 16893 | 17443 | || p>=(void*)mem0.aScratchFree ){ |
| 16894 | 17444 | assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) ); |
| 17445 | + assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) ); |
| 16895 | 17446 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 16896 | 17447 | if( sqlite3GlobalConfig.bMemstat ){ |
| 16897 | 17448 | int iSize = sqlite3MallocSize(p); |
| 16898 | 17449 | sqlite3_mutex_enter(mem0.mutex); |
| 16899 | 17450 | sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize); |
| 16900 | 17451 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize); |
| 17452 | + sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1); |
| 16901 | 17453 | sqlite3GlobalConfig.m.xFree(p); |
| 16902 | 17454 | sqlite3_mutex_leave(mem0.mutex); |
| 16903 | 17455 | }else{ |
| 16904 | 17456 | sqlite3GlobalConfig.m.xFree(p); |
| 16905 | 17457 | } |
| | @@ -16930,11 +17482,11 @@ |
| 16930 | 17482 | /* |
| 16931 | 17483 | ** TRUE if p is a lookaside memory allocation from db |
| 16932 | 17484 | */ |
| 16933 | 17485 | #ifndef SQLITE_OMIT_LOOKASIDE |
| 16934 | 17486 | static int isLookaside(sqlite3 *db, void *p){ |
| 16935 | | - return db && p && p>=db->lookaside.pStart && p<db->lookaside.pEnd; |
| 17487 | + return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd; |
| 16936 | 17488 | } |
| 16937 | 17489 | #else |
| 16938 | 17490 | #define isLookaside(A,B) 0 |
| 16939 | 17491 | #endif |
| 16940 | 17492 | |
| | @@ -16942,32 +17494,36 @@ |
| 16942 | 17494 | ** Return the size of a memory allocation previously obtained from |
| 16943 | 17495 | ** sqlite3Malloc() or sqlite3_malloc(). |
| 16944 | 17496 | */ |
| 16945 | 17497 | SQLITE_PRIVATE int sqlite3MallocSize(void *p){ |
| 16946 | 17498 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 17499 | + assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) ); |
| 16947 | 17500 | return sqlite3GlobalConfig.m.xSize(p); |
| 16948 | 17501 | } |
| 16949 | 17502 | SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){ |
| 16950 | 17503 | assert( db==0 || sqlite3_mutex_held(db->mutex) ); |
| 16951 | | - if( isLookaside(db, p) ){ |
| 17504 | + if( db && isLookaside(db, p) ){ |
| 16952 | 17505 | return db->lookaside.sz; |
| 16953 | 17506 | }else{ |
| 16954 | | - assert( sqlite3MemdebugHasType(p, |
| 16955 | | - db ? (MEMTYPE_DB|MEMTYPE_HEAP) : MEMTYPE_HEAP) ); |
| 17507 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); |
| 17508 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) ); |
| 17509 | + assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); |
| 16956 | 17510 | return sqlite3GlobalConfig.m.xSize(p); |
| 16957 | 17511 | } |
| 16958 | 17512 | } |
| 16959 | 17513 | |
| 16960 | 17514 | /* |
| 16961 | 17515 | ** Free memory previously obtained from sqlite3Malloc(). |
| 16962 | 17516 | */ |
| 16963 | 17517 | SQLITE_API void sqlite3_free(void *p){ |
| 16964 | 17518 | if( p==0 ) return; |
| 17519 | + assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) ); |
| 16965 | 17520 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 16966 | 17521 | if( sqlite3GlobalConfig.bMemstat ){ |
| 16967 | 17522 | sqlite3_mutex_enter(mem0.mutex); |
| 16968 | 17523 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); |
| 17524 | + sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1); |
| 16969 | 17525 | sqlite3GlobalConfig.m.xFree(p); |
| 16970 | 17526 | sqlite3_mutex_leave(mem0.mutex); |
| 16971 | 17527 | }else{ |
| 16972 | 17528 | sqlite3GlobalConfig.m.xFree(p); |
| 16973 | 17529 | } |
| | @@ -16977,20 +17533,28 @@ |
| 16977 | 17533 | ** Free memory that might be associated with a particular database |
| 16978 | 17534 | ** connection. |
| 16979 | 17535 | */ |
| 16980 | 17536 | SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){ |
| 16981 | 17537 | assert( db==0 || sqlite3_mutex_held(db->mutex) ); |
| 16982 | | - if( isLookaside(db, p) ){ |
| 16983 | | - LookasideSlot *pBuf = (LookasideSlot*)p; |
| 16984 | | - pBuf->pNext = db->lookaside.pFree; |
| 16985 | | - db->lookaside.pFree = pBuf; |
| 16986 | | - db->lookaside.nOut--; |
| 16987 | | - }else{ |
| 16988 | | - assert( sqlite3MemdebugHasType(p, MEMTYPE_DB|MEMTYPE_HEAP) ); |
| 16989 | | - sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 16990 | | - sqlite3_free(p); |
| 16991 | | - } |
| 17538 | + if( db ){ |
| 17539 | + if( db->pnBytesFreed ){ |
| 17540 | + *db->pnBytesFreed += sqlite3DbMallocSize(db, p); |
| 17541 | + return; |
| 17542 | + } |
| 17543 | + if( isLookaside(db, p) ){ |
| 17544 | + LookasideSlot *pBuf = (LookasideSlot*)p; |
| 17545 | + pBuf->pNext = db->lookaside.pFree; |
| 17546 | + db->lookaside.pFree = pBuf; |
| 17547 | + db->lookaside.nOut--; |
| 17548 | + return; |
| 17549 | + } |
| 17550 | + } |
| 17551 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); |
| 17552 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) ); |
| 17553 | + assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); |
| 17554 | + sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 17555 | + sqlite3_free(p); |
| 16992 | 17556 | } |
| 16993 | 17557 | |
| 16994 | 17558 | /* |
| 16995 | 17559 | ** Change the size of an existing memory allocation |
| 16996 | 17560 | */ |
| | @@ -17018,10 +17582,11 @@ |
| 17018 | 17582 | if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED)+nNew-nOld >= |
| 17019 | 17583 | mem0.alarmThreshold ){ |
| 17020 | 17584 | sqlite3MallocAlarm(nNew-nOld); |
| 17021 | 17585 | } |
| 17022 | 17586 | assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); |
| 17587 | + assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) ); |
| 17023 | 17588 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 17024 | 17589 | if( pNew==0 && mem0.alarmCallback ){ |
| 17025 | 17590 | sqlite3MallocAlarm(nBytes); |
| 17026 | 17591 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 17027 | 17592 | } |
| | @@ -17090,10 +17655,11 @@ |
| 17090 | 17655 | ** that all prior mallocs (ex: "a") worked too. |
| 17091 | 17656 | */ |
| 17092 | 17657 | SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, int n){ |
| 17093 | 17658 | void *p; |
| 17094 | 17659 | assert( db==0 || sqlite3_mutex_held(db->mutex) ); |
| 17660 | + assert( db==0 || db->pnBytesFreed==0 ); |
| 17095 | 17661 | #ifndef SQLITE_OMIT_LOOKASIDE |
| 17096 | 17662 | if( db ){ |
| 17097 | 17663 | LookasideSlot *pBuf; |
| 17098 | 17664 | if( db->mallocFailed ){ |
| 17099 | 17665 | return 0; |
| | @@ -17115,12 +17681,12 @@ |
| 17115 | 17681 | #endif |
| 17116 | 17682 | p = sqlite3Malloc(n); |
| 17117 | 17683 | if( !p && db ){ |
| 17118 | 17684 | db->mallocFailed = 1; |
| 17119 | 17685 | } |
| 17120 | | - sqlite3MemdebugSetType(p, |
| 17121 | | - (db && db->lookaside.bEnabled) ? MEMTYPE_DB : MEMTYPE_HEAP); |
| 17686 | + sqlite3MemdebugSetType(p, MEMTYPE_DB | |
| 17687 | + ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); |
| 17122 | 17688 | return p; |
| 17123 | 17689 | } |
| 17124 | 17690 | |
| 17125 | 17691 | /* |
| 17126 | 17692 | ** Resize the block of memory pointed to by p to n bytes. If the |
| | @@ -17142,18 +17708,20 @@ |
| 17142 | 17708 | if( pNew ){ |
| 17143 | 17709 | memcpy(pNew, p, db->lookaside.sz); |
| 17144 | 17710 | sqlite3DbFree(db, p); |
| 17145 | 17711 | } |
| 17146 | 17712 | }else{ |
| 17147 | | - assert( sqlite3MemdebugHasType(p, MEMTYPE_DB|MEMTYPE_HEAP) ); |
| 17713 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); |
| 17714 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) ); |
| 17148 | 17715 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 17149 | 17716 | pNew = sqlite3_realloc(p, n); |
| 17150 | 17717 | if( !pNew ){ |
| 17718 | + sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP); |
| 17151 | 17719 | db->mallocFailed = 1; |
| 17152 | 17720 | } |
| 17153 | | - sqlite3MemdebugSetType(pNew, |
| 17154 | | - db->lookaside.bEnabled ? MEMTYPE_DB : MEMTYPE_HEAP); |
| 17721 | + sqlite3MemdebugSetType(pNew, MEMTYPE_DB | |
| 17722 | + (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); |
| 17155 | 17723 | } |
| 17156 | 17724 | } |
| 17157 | 17725 | return pNew; |
| 17158 | 17726 | } |
| 17159 | 17727 | |
| | @@ -18022,11 +18590,15 @@ |
| 18022 | 18590 | p->tooBig = 1; |
| 18023 | 18591 | return; |
| 18024 | 18592 | }else{ |
| 18025 | 18593 | p->nAlloc = (int)szNew; |
| 18026 | 18594 | } |
| 18027 | | - zNew = sqlite3DbMallocRaw(p->db, p->nAlloc ); |
| 18595 | + if( p->useMalloc==1 ){ |
| 18596 | + zNew = sqlite3DbMallocRaw(p->db, p->nAlloc ); |
| 18597 | + }else{ |
| 18598 | + zNew = sqlite3_malloc(p->nAlloc); |
| 18599 | + } |
| 18028 | 18600 | if( zNew ){ |
| 18029 | 18601 | memcpy(zNew, p->zText, p->nChar); |
| 18030 | 18602 | sqlite3StrAccumReset(p); |
| 18031 | 18603 | p->zText = zNew; |
| 18032 | 18604 | }else{ |
| | @@ -18047,11 +18619,15 @@ |
| 18047 | 18619 | */ |
| 18048 | 18620 | SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){ |
| 18049 | 18621 | if( p->zText ){ |
| 18050 | 18622 | p->zText[p->nChar] = 0; |
| 18051 | 18623 | if( p->useMalloc && p->zText==p->zBase ){ |
| 18052 | | - p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); |
| 18624 | + if( p->useMalloc==1 ){ |
| 18625 | + p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); |
| 18626 | + }else{ |
| 18627 | + p->zText = sqlite3_malloc(p->nChar+1); |
| 18628 | + } |
| 18053 | 18629 | if( p->zText ){ |
| 18054 | 18630 | memcpy(p->zText, p->zBase, p->nChar+1); |
| 18055 | 18631 | }else{ |
| 18056 | 18632 | p->mallocFailed = 1; |
| 18057 | 18633 | } |
| | @@ -18063,11 +18639,15 @@ |
| 18063 | 18639 | /* |
| 18064 | 18640 | ** Reset an StrAccum string. Reclaim all malloced memory. |
| 18065 | 18641 | */ |
| 18066 | 18642 | SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){ |
| 18067 | 18643 | if( p->zText!=p->zBase ){ |
| 18068 | | - sqlite3DbFree(p->db, p->zText); |
| 18644 | + if( p->useMalloc==1 ){ |
| 18645 | + sqlite3DbFree(p->db, p->zText); |
| 18646 | + }else{ |
| 18647 | + sqlite3_free(p->zText); |
| 18648 | + } |
| 18069 | 18649 | } |
| 18070 | 18650 | p->zText = 0; |
| 18071 | 18651 | } |
| 18072 | 18652 | |
| 18073 | 18653 | /* |
| | @@ -18145,10 +18725,11 @@ |
| 18145 | 18725 | StrAccum acc; |
| 18146 | 18726 | #ifndef SQLITE_OMIT_AUTOINIT |
| 18147 | 18727 | if( sqlite3_initialize() ) return 0; |
| 18148 | 18728 | #endif |
| 18149 | 18729 | sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); |
| 18730 | + acc.useMalloc = 2; |
| 18150 | 18731 | sqlite3VXPrintf(&acc, 0, zFormat, ap); |
| 18151 | 18732 | z = sqlite3StrAccumFinish(&acc); |
| 18152 | 18733 | return z; |
| 18153 | 18734 | } |
| 18154 | 18735 | |
| | @@ -18438,433 +19019,10 @@ |
| 18438 | 19019 | ** BOM or Byte Order Mark: |
| 18439 | 19020 | ** 0xff 0xfe little-endian utf-16 follows |
| 18440 | 19021 | ** 0xfe 0xff big-endian utf-16 follows |
| 18441 | 19022 | ** |
| 18442 | 19023 | */ |
| 18443 | | -/************** Include vdbeInt.h in the middle of utf.c *********************/ |
| 18444 | | -/************** Begin file vdbeInt.h *****************************************/ |
| 18445 | | -/* |
| 18446 | | -** 2003 September 6 |
| 18447 | | -** |
| 18448 | | -** The author disclaims copyright to this source code. In place of |
| 18449 | | -** a legal notice, here is a blessing: |
| 18450 | | -** |
| 18451 | | -** May you do good and not evil. |
| 18452 | | -** May you find forgiveness for yourself and forgive others. |
| 18453 | | -** May you share freely, never taking more than you give. |
| 18454 | | -** |
| 18455 | | -************************************************************************* |
| 18456 | | -** This is the header file for information that is private to the |
| 18457 | | -** VDBE. This information used to all be at the top of the single |
| 18458 | | -** source code file "vdbe.c". When that file became too big (over |
| 18459 | | -** 6000 lines long) it was split up into several smaller files and |
| 18460 | | -** this header information was factored out. |
| 18461 | | -*/ |
| 18462 | | -#ifndef _VDBEINT_H_ |
| 18463 | | -#define _VDBEINT_H_ |
| 18464 | | - |
| 18465 | | -/* |
| 18466 | | -** SQL is translated into a sequence of instructions to be |
| 18467 | | -** executed by a virtual machine. Each instruction is an instance |
| 18468 | | -** of the following structure. |
| 18469 | | -*/ |
| 18470 | | -typedef struct VdbeOp Op; |
| 18471 | | - |
| 18472 | | -/* |
| 18473 | | -** Boolean values |
| 18474 | | -*/ |
| 18475 | | -typedef unsigned char Bool; |
| 18476 | | - |
| 18477 | | -/* |
| 18478 | | -** A cursor is a pointer into a single BTree within a database file. |
| 18479 | | -** The cursor can seek to a BTree entry with a particular key, or |
| 18480 | | -** loop over all entries of the Btree. You can also insert new BTree |
| 18481 | | -** entries or retrieve the key or data from the entry that the cursor |
| 18482 | | -** is currently pointing to. |
| 18483 | | -** |
| 18484 | | -** Every cursor that the virtual machine has open is represented by an |
| 18485 | | -** instance of the following structure. |
| 18486 | | -** |
| 18487 | | -** If the VdbeCursor.isTriggerRow flag is set it means that this cursor is |
| 18488 | | -** really a single row that represents the NEW or OLD pseudo-table of |
| 18489 | | -** a row trigger. The data for the row is stored in VdbeCursor.pData and |
| 18490 | | -** the rowid is in VdbeCursor.iKey. |
| 18491 | | -*/ |
| 18492 | | -struct VdbeCursor { |
| 18493 | | - BtCursor *pCursor; /* The cursor structure of the backend */ |
| 18494 | | - int iDb; /* Index of cursor database in db->aDb[] (or -1) */ |
| 18495 | | - i64 lastRowid; /* Last rowid from a Next or NextIdx operation */ |
| 18496 | | - Bool zeroed; /* True if zeroed out and ready for reuse */ |
| 18497 | | - Bool rowidIsValid; /* True if lastRowid is valid */ |
| 18498 | | - Bool atFirst; /* True if pointing to first entry */ |
| 18499 | | - Bool useRandomRowid; /* Generate new record numbers semi-randomly */ |
| 18500 | | - Bool nullRow; /* True if pointing to a row with no data */ |
| 18501 | | - Bool deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */ |
| 18502 | | - Bool isTable; /* True if a table requiring integer keys */ |
| 18503 | | - Bool isIndex; /* True if an index containing keys only - no data */ |
| 18504 | | - i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */ |
| 18505 | | - Btree *pBt; /* Separate file holding temporary table */ |
| 18506 | | - int pseudoTableReg; /* Register holding pseudotable content. */ |
| 18507 | | - KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */ |
| 18508 | | - int nField; /* Number of fields in the header */ |
| 18509 | | - i64 seqCount; /* Sequence counter */ |
| 18510 | | - sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */ |
| 18511 | | - const sqlite3_module *pModule; /* Module for cursor pVtabCursor */ |
| 18512 | | - |
| 18513 | | - /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or |
| 18514 | | - ** OP_IsUnique opcode on this cursor. */ |
| 18515 | | - int seekResult; |
| 18516 | | - |
| 18517 | | - /* Cached information about the header for the data record that the |
| 18518 | | - ** cursor is currently pointing to. Only valid if cacheStatus matches |
| 18519 | | - ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of |
| 18520 | | - ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that |
| 18521 | | - ** the cache is out of date. |
| 18522 | | - ** |
| 18523 | | - ** aRow might point to (ephemeral) data for the current row, or it might |
| 18524 | | - ** be NULL. |
| 18525 | | - */ |
| 18526 | | - u32 cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */ |
| 18527 | | - int payloadSize; /* Total number of bytes in the record */ |
| 18528 | | - u32 *aType; /* Type values for all entries in the record */ |
| 18529 | | - u32 *aOffset; /* Cached offsets to the start of each columns data */ |
| 18530 | | - u8 *aRow; /* Data for the current row, if all on one page */ |
| 18531 | | -}; |
| 18532 | | -typedef struct VdbeCursor VdbeCursor; |
| 18533 | | - |
| 18534 | | -/* |
| 18535 | | -** When a sub-program is executed (OP_Program), a structure of this type |
| 18536 | | -** is allocated to store the current value of the program counter, as |
| 18537 | | -** well as the current memory cell array and various other frame specific |
| 18538 | | -** values stored in the Vdbe struct. When the sub-program is finished, |
| 18539 | | -** these values are copied back to the Vdbe from the VdbeFrame structure, |
| 18540 | | -** restoring the state of the VM to as it was before the sub-program |
| 18541 | | -** began executing. |
| 18542 | | -** |
| 18543 | | -** Frames are stored in a linked list headed at Vdbe.pParent. Vdbe.pParent |
| 18544 | | -** is the parent of the current frame, or zero if the current frame |
| 18545 | | -** is the main Vdbe program. |
| 18546 | | -*/ |
| 18547 | | -typedef struct VdbeFrame VdbeFrame; |
| 18548 | | -struct VdbeFrame { |
| 18549 | | - Vdbe *v; /* VM this frame belongs to */ |
| 18550 | | - int pc; /* Program Counter */ |
| 18551 | | - Op *aOp; /* Program instructions */ |
| 18552 | | - int nOp; /* Size of aOp array */ |
| 18553 | | - Mem *aMem; /* Array of memory cells */ |
| 18554 | | - int nMem; /* Number of entries in aMem */ |
| 18555 | | - VdbeCursor **apCsr; /* Element of Vdbe cursors */ |
| 18556 | | - u16 nCursor; /* Number of entries in apCsr */ |
| 18557 | | - void *token; /* Copy of SubProgram.token */ |
| 18558 | | - int nChildMem; /* Number of memory cells for child frame */ |
| 18559 | | - int nChildCsr; /* Number of cursors for child frame */ |
| 18560 | | - i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */ |
| 18561 | | - int nChange; /* Statement changes (Vdbe.nChanges) */ |
| 18562 | | - VdbeFrame *pParent; /* Parent of this frame */ |
| 18563 | | -}; |
| 18564 | | - |
| 18565 | | -#define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))]) |
| 18566 | | - |
| 18567 | | -/* |
| 18568 | | -** A value for VdbeCursor.cacheValid that means the cache is always invalid. |
| 18569 | | -*/ |
| 18570 | | -#define CACHE_STALE 0 |
| 18571 | | - |
| 18572 | | -/* |
| 18573 | | -** Internally, the vdbe manipulates nearly all SQL values as Mem |
| 18574 | | -** structures. Each Mem struct may cache multiple representations (string, |
| 18575 | | -** integer etc.) of the same value. A value (and therefore Mem structure) |
| 18576 | | -** has the following properties: |
| 18577 | | -** |
| 18578 | | -** Each value has a manifest type. The manifest type of the value stored |
| 18579 | | -** in a Mem struct is returned by the MemType(Mem*) macro. The type is |
| 18580 | | -** one of SQLITE_NULL, SQLITE_INTEGER, SQLITE_REAL, SQLITE_TEXT or |
| 18581 | | -** SQLITE_BLOB. |
| 18582 | | -*/ |
| 18583 | | -struct Mem { |
| 18584 | | - union { |
| 18585 | | - i64 i; /* Integer value. */ |
| 18586 | | - int nZero; /* Used when bit MEM_Zero is set in flags */ |
| 18587 | | - FuncDef *pDef; /* Used only when flags==MEM_Agg */ |
| 18588 | | - RowSet *pRowSet; /* Used only when flags==MEM_RowSet */ |
| 18589 | | - VdbeFrame *pFrame; /* Used when flags==MEM_Frame */ |
| 18590 | | - } u; |
| 18591 | | - double r; /* Real value */ |
| 18592 | | - sqlite3 *db; /* The associated database connection */ |
| 18593 | | - char *z; /* String or BLOB value */ |
| 18594 | | - int n; /* Number of characters in string value, excluding '\0' */ |
| 18595 | | - u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */ |
| 18596 | | - u8 type; /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */ |
| 18597 | | - u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */ |
| 18598 | | - void (*xDel)(void *); /* If not null, call this function to delete Mem.z */ |
| 18599 | | - char *zMalloc; /* Dynamic buffer allocated by sqlite3_malloc() */ |
| 18600 | | -}; |
| 18601 | | - |
| 18602 | | -/* One or more of the following flags are set to indicate the validOK |
| 18603 | | -** representations of the value stored in the Mem struct. |
| 18604 | | -** |
| 18605 | | -** If the MEM_Null flag is set, then the value is an SQL NULL value. |
| 18606 | | -** No other flags may be set in this case. |
| 18607 | | -** |
| 18608 | | -** If the MEM_Str flag is set then Mem.z points at a string representation. |
| 18609 | | -** Usually this is encoded in the same unicode encoding as the main |
| 18610 | | -** database (see below for exceptions). If the MEM_Term flag is also |
| 18611 | | -** set, then the string is nul terminated. The MEM_Int and MEM_Real |
| 18612 | | -** flags may coexist with the MEM_Str flag. |
| 18613 | | -** |
| 18614 | | -** Multiple of these values can appear in Mem.flags. But only one |
| 18615 | | -** at a time can appear in Mem.type. |
| 18616 | | -*/ |
| 18617 | | -#define MEM_Null 0x0001 /* Value is NULL */ |
| 18618 | | -#define MEM_Str 0x0002 /* Value is a string */ |
| 18619 | | -#define MEM_Int 0x0004 /* Value is an integer */ |
| 18620 | | -#define MEM_Real 0x0008 /* Value is a real number */ |
| 18621 | | -#define MEM_Blob 0x0010 /* Value is a BLOB */ |
| 18622 | | -#define MEM_RowSet 0x0020 /* Value is a RowSet object */ |
| 18623 | | -#define MEM_Frame 0x0040 /* Value is a VdbeFrame object */ |
| 18624 | | -#define MEM_TypeMask 0x00ff /* Mask of type bits */ |
| 18625 | | - |
| 18626 | | -/* Whenever Mem contains a valid string or blob representation, one of |
| 18627 | | -** the following flags must be set to determine the memory management |
| 18628 | | -** policy for Mem.z. The MEM_Term flag tells us whether or not the |
| 18629 | | -** string is \000 or \u0000 terminated |
| 18630 | | -*/ |
| 18631 | | -#define MEM_Term 0x0200 /* String rep is nul terminated */ |
| 18632 | | -#define MEM_Dyn 0x0400 /* Need to call sqliteFree() on Mem.z */ |
| 18633 | | -#define MEM_Static 0x0800 /* Mem.z points to a static string */ |
| 18634 | | -#define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */ |
| 18635 | | -#define MEM_Agg 0x2000 /* Mem.z points to an agg function context */ |
| 18636 | | -#define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */ |
| 18637 | | - |
| 18638 | | -#ifdef SQLITE_OMIT_INCRBLOB |
| 18639 | | - #undef MEM_Zero |
| 18640 | | - #define MEM_Zero 0x0000 |
| 18641 | | -#endif |
| 18642 | | - |
| 18643 | | - |
| 18644 | | -/* |
| 18645 | | -** Clear any existing type flags from a Mem and replace them with f |
| 18646 | | -*/ |
| 18647 | | -#define MemSetTypeFlag(p, f) \ |
| 18648 | | - ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f) |
| 18649 | | - |
| 18650 | | - |
| 18651 | | -/* A VdbeFunc is just a FuncDef (defined in sqliteInt.h) that contains |
| 18652 | | -** additional information about auxiliary information bound to arguments |
| 18653 | | -** of the function. This is used to implement the sqlite3_get_auxdata() |
| 18654 | | -** and sqlite3_set_auxdata() APIs. The "auxdata" is some auxiliary data |
| 18655 | | -** that can be associated with a constant argument to a function. This |
| 18656 | | -** allows functions such as "regexp" to compile their constant regular |
| 18657 | | -** expression argument once and reused the compiled code for multiple |
| 18658 | | -** invocations. |
| 18659 | | -*/ |
| 18660 | | -struct VdbeFunc { |
| 18661 | | - FuncDef *pFunc; /* The definition of the function */ |
| 18662 | | - int nAux; /* Number of entries allocated for apAux[] */ |
| 18663 | | - struct AuxData { |
| 18664 | | - void *pAux; /* Aux data for the i-th argument */ |
| 18665 | | - void (*xDelete)(void *); /* Destructor for the aux data */ |
| 18666 | | - } apAux[1]; /* One slot for each function argument */ |
| 18667 | | -}; |
| 18668 | | - |
| 18669 | | -/* |
| 18670 | | -** The "context" argument for a installable function. A pointer to an |
| 18671 | | -** instance of this structure is the first argument to the routines used |
| 18672 | | -** implement the SQL functions. |
| 18673 | | -** |
| 18674 | | -** There is a typedef for this structure in sqlite.h. So all routines, |
| 18675 | | -** even the public interface to SQLite, can use a pointer to this structure. |
| 18676 | | -** But this file is the only place where the internal details of this |
| 18677 | | -** structure are known. |
| 18678 | | -** |
| 18679 | | -** This structure is defined inside of vdbeInt.h because it uses substructures |
| 18680 | | -** (Mem) which are only defined there. |
| 18681 | | -*/ |
| 18682 | | -struct sqlite3_context { |
| 18683 | | - FuncDef *pFunc; /* Pointer to function information. MUST BE FIRST */ |
| 18684 | | - VdbeFunc *pVdbeFunc; /* Auxilary data, if created. */ |
| 18685 | | - Mem s; /* The return value is stored here */ |
| 18686 | | - Mem *pMem; /* Memory cell used to store aggregate context */ |
| 18687 | | - int isError; /* Error code returned by the function. */ |
| 18688 | | - CollSeq *pColl; /* Collating sequence */ |
| 18689 | | -}; |
| 18690 | | - |
| 18691 | | -/* |
| 18692 | | -** A Set structure is used for quick testing to see if a value |
| 18693 | | -** is part of a small set. Sets are used to implement code like |
| 18694 | | -** this: |
| 18695 | | -** x.y IN ('hi','hoo','hum') |
| 18696 | | -*/ |
| 18697 | | -typedef struct Set Set; |
| 18698 | | -struct Set { |
| 18699 | | - Hash hash; /* A set is just a hash table */ |
| 18700 | | - HashElem *prev; /* Previously accessed hash elemen */ |
| 18701 | | -}; |
| 18702 | | - |
| 18703 | | -/* |
| 18704 | | -** An instance of the virtual machine. This structure contains the complete |
| 18705 | | -** state of the virtual machine. |
| 18706 | | -** |
| 18707 | | -** The "sqlite3_stmt" structure pointer that is returned by sqlite3_compile() |
| 18708 | | -** is really a pointer to an instance of this structure. |
| 18709 | | -** |
| 18710 | | -** The Vdbe.inVtabMethod variable is set to non-zero for the duration of |
| 18711 | | -** any virtual table method invocations made by the vdbe program. It is |
| 18712 | | -** set to 2 for xDestroy method calls and 1 for all other methods. This |
| 18713 | | -** variable is used for two purposes: to allow xDestroy methods to execute |
| 18714 | | -** "DROP TABLE" statements and to prevent some nasty side effects of |
| 18715 | | -** malloc failure when SQLite is invoked recursively by a virtual table |
| 18716 | | -** method function. |
| 18717 | | -*/ |
| 18718 | | -struct Vdbe { |
| 18719 | | - sqlite3 *db; /* The database connection that owns this statement */ |
| 18720 | | - Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */ |
| 18721 | | - int nOp; /* Number of instructions in the program */ |
| 18722 | | - int nOpAlloc; /* Number of slots allocated for aOp[] */ |
| 18723 | | - Op *aOp; /* Space to hold the virtual machine's program */ |
| 18724 | | - int nLabel; /* Number of labels used */ |
| 18725 | | - int nLabelAlloc; /* Number of slots allocated in aLabel[] */ |
| 18726 | | - int *aLabel; /* Space to hold the labels */ |
| 18727 | | - Mem **apArg; /* Arguments to currently executing user function */ |
| 18728 | | - Mem *aColName; /* Column names to return */ |
| 18729 | | - Mem *pResultSet; /* Pointer to an array of results */ |
| 18730 | | - u16 nResColumn; /* Number of columns in one row of the result set */ |
| 18731 | | - u16 nCursor; /* Number of slots in apCsr[] */ |
| 18732 | | - VdbeCursor **apCsr; /* One element of this array for each open cursor */ |
| 18733 | | - u8 errorAction; /* Recovery action to do in case of an error */ |
| 18734 | | - u8 okVar; /* True if azVar[] has been initialized */ |
| 18735 | | - ynVar nVar; /* Number of entries in aVar[] */ |
| 18736 | | - Mem *aVar; /* Values for the OP_Variable opcode. */ |
| 18737 | | - char **azVar; /* Name of variables */ |
| 18738 | | - u32 magic; /* Magic number for sanity checking */ |
| 18739 | | - int nMem; /* Number of memory locations currently allocated */ |
| 18740 | | - Mem *aMem; /* The memory locations */ |
| 18741 | | - u32 cacheCtr; /* VdbeCursor row cache generation counter */ |
| 18742 | | - int pc; /* The program counter */ |
| 18743 | | - int rc; /* Value to return */ |
| 18744 | | - char *zErrMsg; /* Error message written here */ |
| 18745 | | - u8 explain; /* True if EXPLAIN present on SQL command */ |
| 18746 | | - u8 changeCntOn; /* True to update the change-counter */ |
| 18747 | | - u8 expired; /* True if the VM needs to be recompiled */ |
| 18748 | | - u8 runOnlyOnce; /* Automatically expire on reset */ |
| 18749 | | - u8 minWriteFileFormat; /* Minimum file format for writable database files */ |
| 18750 | | - u8 inVtabMethod; /* See comments above */ |
| 18751 | | - u8 usesStmtJournal; /* True if uses a statement journal */ |
| 18752 | | - u8 readOnly; /* True for read-only statements */ |
| 18753 | | - u8 isPrepareV2; /* True if prepared with prepare_v2() */ |
| 18754 | | - int nChange; /* Number of db changes made since last reset */ |
| 18755 | | - int btreeMask; /* Bitmask of db->aDb[] entries referenced */ |
| 18756 | | - i64 startTime; /* Time when query started - used for profiling */ |
| 18757 | | - BtreeMutexArray aMutex; /* An array of Btree used here and needing locks */ |
| 18758 | | - int aCounter[3]; /* Counters used by sqlite3_stmt_status() */ |
| 18759 | | - char *zSql; /* Text of the SQL statement that generated this */ |
| 18760 | | - void *pFree; /* Free this when deleting the vdbe */ |
| 18761 | | - i64 nFkConstraint; /* Number of imm. FK constraints this VM */ |
| 18762 | | - i64 nStmtDefCons; /* Number of def. constraints when stmt started */ |
| 18763 | | - int iStatement; /* Statement number (or 0 if has not opened stmt) */ |
| 18764 | | -#ifdef SQLITE_DEBUG |
| 18765 | | - FILE *trace; /* Write an execution trace here, if not NULL */ |
| 18766 | | -#endif |
| 18767 | | - VdbeFrame *pFrame; /* Parent frame */ |
| 18768 | | - int nFrame; /* Number of frames in pFrame list */ |
| 18769 | | - u32 expmask; /* Binding to these vars invalidates VM */ |
| 18770 | | -}; |
| 18771 | | - |
| 18772 | | -/* |
| 18773 | | -** The following are allowed values for Vdbe.magic |
| 18774 | | -*/ |
| 18775 | | -#define VDBE_MAGIC_INIT 0x26bceaa5 /* Building a VDBE program */ |
| 18776 | | -#define VDBE_MAGIC_RUN 0xbdf20da3 /* VDBE is ready to execute */ |
| 18777 | | -#define VDBE_MAGIC_HALT 0x519c2973 /* VDBE has completed execution */ |
| 18778 | | -#define VDBE_MAGIC_DEAD 0xb606c3c8 /* The VDBE has been deallocated */ |
| 18779 | | - |
| 18780 | | -/* |
| 18781 | | -** Function prototypes |
| 18782 | | -*/ |
| 18783 | | -SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*); |
| 18784 | | -void sqliteVdbePopStack(Vdbe*,int); |
| 18785 | | -SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*); |
| 18786 | | -#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 18787 | | -SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*); |
| 18788 | | -#endif |
| 18789 | | -SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32); |
| 18790 | | -SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int); |
| 18791 | | -SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, int, Mem*, int); |
| 18792 | | -SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*); |
| 18793 | | -SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc*, int); |
| 18794 | | - |
| 18795 | | -int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *); |
| 18796 | | -SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*); |
| 18797 | | -SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *); |
| 18798 | | -SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*); |
| 18799 | | -SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*); |
| 18800 | | -SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*); |
| 18801 | | -SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*); |
| 18802 | | -SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int); |
| 18803 | | -SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*); |
| 18804 | | -SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*); |
| 18805 | | -SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int); |
| 18806 | | -SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*); |
| 18807 | | -SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*); |
| 18808 | | -SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*)); |
| 18809 | | -SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64); |
| 18810 | | -#ifdef SQLITE_OMIT_FLOATING_POINT |
| 18811 | | -# define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64 |
| 18812 | | -#else |
| 18813 | | -SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem*, double); |
| 18814 | | -#endif |
| 18815 | | -SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*); |
| 18816 | | -SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int); |
| 18817 | | -SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*); |
| 18818 | | -SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*); |
| 18819 | | -SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, int); |
| 18820 | | -SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*); |
| 18821 | | -SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*); |
| 18822 | | -SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*); |
| 18823 | | -SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*); |
| 18824 | | -SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*); |
| 18825 | | -SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*); |
| 18826 | | -SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*); |
| 18827 | | -SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p); |
| 18828 | | -SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p); |
| 18829 | | -SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*); |
| 18830 | | -SQLITE_PRIVATE const char *sqlite3OpcodeName(int); |
| 18831 | | -SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve); |
| 18832 | | -SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int); |
| 18833 | | -SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*); |
| 18834 | | -SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *); |
| 18835 | | -SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem); |
| 18836 | | - |
| 18837 | | -#ifndef SQLITE_OMIT_FOREIGN_KEY |
| 18838 | | -SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int); |
| 18839 | | -#else |
| 18840 | | -# define sqlite3VdbeCheckFk(p,i) 0 |
| 18841 | | -#endif |
| 18842 | | - |
| 18843 | | -#ifndef SQLITE_OMIT_SHARED_CACHE |
| 18844 | | -SQLITE_PRIVATE void sqlite3VdbeMutexArrayEnter(Vdbe *p); |
| 18845 | | -#else |
| 18846 | | -# define sqlite3VdbeMutexArrayEnter(p) |
| 18847 | | -#endif |
| 18848 | | - |
| 18849 | | -SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8); |
| 18850 | | -#ifdef SQLITE_DEBUG |
| 18851 | | -SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe*); |
| 18852 | | -SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf); |
| 18853 | | -#endif |
| 18854 | | -SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem); |
| 18855 | | - |
| 18856 | | -#ifndef SQLITE_OMIT_INCRBLOB |
| 18857 | | -SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *); |
| 18858 | | -#else |
| 18859 | | - #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK |
| 18860 | | -#endif |
| 18861 | | - |
| 18862 | | -#endif /* !defined(_VDBEINT_H_) */ |
| 18863 | | - |
| 18864 | | -/************** End of vdbeInt.h *********************************************/ |
| 18865 | | -/************** Continuing where we left off in utf.c ************************/ |
| 18866 | 19024 | |
| 18867 | 19025 | #ifndef SQLITE_AMALGAMATION |
| 18868 | 19026 | /* |
| 18869 | 19027 | ** The following constant value is used by the SQLITE_BIGENDIAN and |
| 18870 | 19028 | ** SQLITE_LITTLEENDIAN macros. |
| | @@ -21891,11 +22049,11 @@ |
| 21891 | 22049 | } |
| 21892 | 22050 | |
| 21893 | 22051 | |
| 21894 | 22052 | memset( pFile, 0, sizeof(*pFile) ); |
| 21895 | 22053 | |
| 21896 | | - OSTRACE( "OPEN want %d\n", flags )); |
| 22054 | + OSTRACE(( "OPEN want %d\n", flags )); |
| 21897 | 22055 | |
| 21898 | 22056 | if( flags & SQLITE_OPEN_READWRITE ){ |
| 21899 | 22057 | ulOpenMode |= OPEN_ACCESS_READWRITE; |
| 21900 | 22058 | OSTRACE(( "OPEN read/write\n" )); |
| 21901 | 22059 | }else{ |
| | @@ -32701,15 +32859,15 @@ |
| 32701 | 32859 | ** back to sqlite3Malloc(). |
| 32702 | 32860 | */ |
| 32703 | 32861 | static void *pcache1Alloc(int nByte){ |
| 32704 | 32862 | void *p; |
| 32705 | 32863 | assert( sqlite3_mutex_held(pcache1.mutex) ); |
| 32864 | + sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte); |
| 32706 | 32865 | if( nByte<=pcache1.szSlot && pcache1.pFree ){ |
| 32707 | 32866 | assert( pcache1.isInit ); |
| 32708 | 32867 | p = (PgHdr1 *)pcache1.pFree; |
| 32709 | 32868 | pcache1.pFree = pcache1.pFree->pNext; |
| 32710 | | - sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte); |
| 32711 | 32869 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1); |
| 32712 | 32870 | }else{ |
| 32713 | 32871 | |
| 32714 | 32872 | /* Allocate a new buffer using sqlite3Malloc. Before doing so, exit the |
| 32715 | 32873 | ** global pcache mutex and unlock the pager-cache object pCache. This is |
| | @@ -34150,34 +34308,42 @@ |
| 34150 | 34308 | ** It is used when committing or otherwise ending a transaction. If |
| 34151 | 34309 | ** the dbModified flag is clear then less work has to be done. |
| 34152 | 34310 | ** |
| 34153 | 34311 | ** journalStarted |
| 34154 | 34312 | ** |
| 34155 | | -** This flag is set whenever the the main journal is opened and |
| 34156 | | -** initialized |
| 34313 | +** This flag is set during a write-transaction after the first |
| 34314 | +** journal-header is written and synced to disk. |
| 34157 | 34315 | ** |
| 34158 | | -** The point of this flag is that it must be set after the |
| 34159 | | -** first journal header in a journal file has been synced to disk. |
| 34160 | 34316 | ** After this has happened, new pages appended to the database |
| 34161 | 34317 | ** do not need the PGHDR_NEED_SYNC flag set, as they do not need |
| 34162 | 34318 | ** to wait for a journal sync before they can be written out to |
| 34163 | 34319 | ** the database file (see function pager_write()). |
| 34164 | 34320 | ** |
| 34165 | 34321 | ** setMaster |
| 34166 | 34322 | ** |
| 34167 | | -** This variable is used to ensure that the master journal file name |
| 34168 | | -** (if any) is only written into the journal file once. |
| 34323 | +** When PagerCommitPhaseOne() is called to commit a transaction, it may |
| 34324 | +** (or may not) specify a master-journal name to be written into the |
| 34325 | +** journal file before it is synced to disk. |
| 34169 | 34326 | ** |
| 34170 | | -** When committing a transaction, the master journal file name (if any) |
| 34171 | | -** may be written into the journal file while the pager is still in |
| 34172 | | -** PAGER_RESERVED state (see CommitPhaseOne() for the action). It |
| 34173 | | -** then attempts to upgrade to an exclusive lock. If this attempt |
| 34174 | | -** fails, then SQLITE_BUSY may be returned to the user and the user |
| 34175 | | -** may attempt to commit the transaction again later (calling |
| 34176 | | -** CommitPhaseOne() again). This flag is used to ensure that the |
| 34177 | | -** master journal name is only written to the journal file the first |
| 34178 | | -** time CommitPhaseOne() is called. |
| 34327 | +** Whether or not a journal file contains a master-journal pointer affects |
| 34328 | +** the way in which the journal file is finalized after the transaction is |
| 34329 | +** committed or rolled back when running in "journal_mode=PERSIST" mode. |
| 34330 | +** If a journal file does not contain a master-journal pointer, it is |
| 34331 | +** finalized by overwriting the first journal header with zeroes. If, |
| 34332 | +** on the other hand, it does contain a master-journal pointer, the |
| 34333 | +** journal file is finalized by truncating it to zero bytes, just as if |
| 34334 | +** the connection were running in "journal_mode=truncate" mode. |
| 34335 | +** |
| 34336 | +** Journal files that contain master journal pointers cannot be finalized |
| 34337 | +** simply by overwriting the first journal-header with zeroes, as the |
| 34338 | +** master journal pointer could interfere with hot-journal rollback of any |
| 34339 | +** subsequently interrupted transaction that reuses the journal file. |
| 34340 | +** |
| 34341 | +** The flag is cleared as soon as the journal file is finalized (either |
| 34342 | +** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the |
| 34343 | +** journal file from being successfully finalized, the setMaster flag |
| 34344 | +** is cleared anyway. |
| 34179 | 34345 | ** |
| 34180 | 34346 | ** doNotSpill, doNotSyncSpill |
| 34181 | 34347 | ** |
| 34182 | 34348 | ** When enabled, cache spills are prohibited. The doNotSpill variable |
| 34183 | 34349 | ** inhibits all cache spill and doNotSyncSpill inhibits those spills that |
| | @@ -34941,11 +35107,12 @@ |
| 34941 | 35107 | int nMaster; /* Length of string zMaster */ |
| 34942 | 35108 | i64 iHdrOff; /* Offset of header in journal file */ |
| 34943 | 35109 | i64 jrnlSize; /* Size of journal file on disk */ |
| 34944 | 35110 | u32 cksum = 0; /* Checksum of string zMaster */ |
| 34945 | 35111 | |
| 34946 | | - if( !zMaster || pPager->setMaster |
| 35112 | + assert( pPager->setMaster==0 ); |
| 35113 | + if( !zMaster |
| 34947 | 35114 | || pPager->journalMode==PAGER_JOURNALMODE_MEMORY |
| 34948 | 35115 | || pPager->journalMode==PAGER_JOURNALMODE_OFF |
| 34949 | 35116 | ){ |
| 34950 | 35117 | return SQLITE_OK; |
| 34951 | 35118 | } |
| | @@ -38928,10 +39095,30 @@ |
| 38928 | 39095 | }else{ |
| 38929 | 39096 | rc = sqlite3OsSync(pPager->fd, pPager->sync_flags); |
| 38930 | 39097 | } |
| 38931 | 39098 | return rc; |
| 38932 | 39099 | } |
| 39100 | + |
| 39101 | +/* |
| 39102 | +** This function may only be called while a write-transaction is active in |
| 39103 | +** rollback. If the connection is in WAL mode, this call is a no-op. |
| 39104 | +** Otherwise, if the connection does not already have an EXCLUSIVE lock on |
| 39105 | +** the database file, an attempt is made to obtain one. |
| 39106 | +** |
| 39107 | +** If the EXCLUSIVE lock is already held or the attempt to obtain it is |
| 39108 | +** successful, or the connection is in WAL mode, SQLITE_OK is returned. |
| 39109 | +** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is |
| 39110 | +** returned. |
| 39111 | +*/ |
| 39112 | +SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){ |
| 39113 | + int rc = SQLITE_OK; |
| 39114 | + assert( pPager->state>=PAGER_RESERVED ); |
| 39115 | + if( 0==pagerUseWal(pPager) ){ |
| 39116 | + rc = pager_wait_on_lock(pPager, PAGER_EXCLUSIVE); |
| 39117 | + } |
| 39118 | + return rc; |
| 39119 | +} |
| 38933 | 39120 | |
| 38934 | 39121 | /* |
| 38935 | 39122 | ** Sync the database file for the pager pPager. zMaster points to the name |
| 38936 | 39123 | ** of a master journal file that should be written into the individual |
| 38937 | 39124 | ** journal file. zMaster may be NULL, which is interpreted as no master |
| | @@ -38977,11 +39164,11 @@ |
| 38977 | 39164 | /* If this is an in-memory db, or no pages have been written to, or this |
| 38978 | 39165 | ** function has already been called, it is mostly a no-op. However, any |
| 38979 | 39166 | ** backup in progress needs to be restarted. |
| 38980 | 39167 | */ |
| 38981 | 39168 | sqlite3BackupRestart(pPager->pBackup); |
| 38982 | | - }else if( pPager->state!=PAGER_SYNCED && pPager->dbModified ){ |
| 39169 | + }else if( pPager->dbModified ){ |
| 38983 | 39170 | if( pagerUseWal(pPager) ){ |
| 38984 | 39171 | PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache); |
| 38985 | 39172 | if( pList ){ |
| 38986 | 39173 | rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1, |
| 38987 | 39174 | (pPager->fullSync ? pPager->sync_flags : 0) |
| | @@ -39115,10 +39302,11 @@ |
| 39115 | 39302 | rc = sqlite3OsSync(pPager->fd, pPager->sync_flags); |
| 39116 | 39303 | } |
| 39117 | 39304 | IOTRACE(("DBSYNC %p\n", pPager)) |
| 39118 | 39305 | } |
| 39119 | 39306 | |
| 39307 | + assert( pPager->state!=PAGER_SYNCED ); |
| 39120 | 39308 | pPager->state = PAGER_SYNCED; |
| 39121 | 39309 | } |
| 39122 | 39310 | |
| 39123 | 39311 | commit_phase_one_exit: |
| 39124 | 39312 | return rc; |
| | @@ -39282,13 +39470,15 @@ |
| 39282 | 39470 | /* |
| 39283 | 39471 | ** Return the approximate number of bytes of memory currently |
| 39284 | 39472 | ** used by the pager and its associated cache. |
| 39285 | 39473 | */ |
| 39286 | 39474 | SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){ |
| 39287 | | - int perPageSize = pPager->pageSize + pPager->nExtra + 20; |
| 39475 | + int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr) |
| 39476 | + + 5*sizeof(void*); |
| 39288 | 39477 | return perPageSize*sqlite3PcachePagecount(pPager->pPCache) |
| 39289 | | - + sqlite3MallocSize(pPager); |
| 39478 | + + sqlite3MallocSize(pPager) |
| 39479 | + + pPager->pageSize; |
| 39290 | 39480 | } |
| 39291 | 39481 | |
| 39292 | 39482 | /* |
| 39293 | 39483 | ** Return the number of references to the specified page. |
| 39294 | 39484 | */ |
| | @@ -45683,11 +45873,11 @@ |
| 45683 | 45873 | assert( !pBt->pCursor ); |
| 45684 | 45874 | sqlite3PagerClose(pBt->pPager); |
| 45685 | 45875 | if( pBt->xFreeSchema && pBt->pSchema ){ |
| 45686 | 45876 | pBt->xFreeSchema(pBt->pSchema); |
| 45687 | 45877 | } |
| 45688 | | - sqlite3_free(pBt->pSchema); |
| 45878 | + sqlite3DbFree(0, pBt->pSchema); |
| 45689 | 45879 | freeTempSpace(pBt); |
| 45690 | 45880 | sqlite3_free(pBt); |
| 45691 | 45881 | } |
| 45692 | 45882 | |
| 45693 | 45883 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| | @@ -46248,17 +46438,31 @@ |
| 46248 | 46438 | } |
| 46249 | 46439 | p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 46250 | 46440 | if( p->inTrans>pBt->inTransaction ){ |
| 46251 | 46441 | pBt->inTransaction = p->inTrans; |
| 46252 | 46442 | } |
| 46253 | | -#ifndef SQLITE_OMIT_SHARED_CACHE |
| 46254 | 46443 | if( wrflag ){ |
| 46444 | + MemPage *pPage1 = pBt->pPage1; |
| 46445 | +#ifndef SQLITE_OMIT_SHARED_CACHE |
| 46255 | 46446 | assert( !pBt->pWriter ); |
| 46256 | 46447 | pBt->pWriter = p; |
| 46257 | 46448 | pBt->isExclusive = (u8)(wrflag>1); |
| 46449 | +#endif |
| 46450 | + |
| 46451 | + /* If the db-size header field is incorrect (as it may be if an old |
| 46452 | + ** client has been writing the database file), update it now. Doing |
| 46453 | + ** this sooner rather than later means the database size can safely |
| 46454 | + ** re-read the database size from page 1 if a savepoint or transaction |
| 46455 | + ** rollback occurs within the transaction. |
| 46456 | + */ |
| 46457 | + if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){ |
| 46458 | + rc = sqlite3PagerWrite(pPage1->pDbPage); |
| 46459 | + if( rc==SQLITE_OK ){ |
| 46460 | + put4byte(&pPage1->aData[28], pBt->nPage); |
| 46461 | + } |
| 46462 | + } |
| 46258 | 46463 | } |
| 46259 | | -#endif |
| 46260 | 46464 | } |
| 46261 | 46465 | |
| 46262 | 46466 | |
| 46263 | 46467 | trans_begun: |
| 46264 | 46468 | if( rc==SQLITE_OK && wrflag ){ |
| | @@ -46993,13 +47197,15 @@ |
| 46993 | 47197 | rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint); |
| 46994 | 47198 | if( rc==SQLITE_OK ){ |
| 46995 | 47199 | if( iSavepoint<0 && pBt->initiallyEmpty ) pBt->nPage = 0; |
| 46996 | 47200 | rc = newDatabase(pBt); |
| 46997 | 47201 | pBt->nPage = get4byte(28 + pBt->pPage1->aData); |
| 46998 | | - if( pBt->nPage==0 ){ |
| 46999 | | - sqlite3PagerPagecount(pBt->pPager, (int*)&pBt->nPage); |
| 47000 | | - } |
| 47202 | + |
| 47203 | + /* The database size was written into the offset 28 of the header |
| 47204 | + ** when the transaction started, so we know that the value at offset |
| 47205 | + ** 28 is nonzero. */ |
| 47206 | + assert( pBt->nPage>0 ); |
| 47001 | 47207 | } |
| 47002 | 47208 | sqlite3BtreeLeave(p); |
| 47003 | 47209 | } |
| 47004 | 47210 | return rc; |
| 47005 | 47211 | } |
| | @@ -51428,10 +51634,11 @@ |
| 51428 | 51634 | i = PENDING_BYTE_PAGE(pBt); |
| 51429 | 51635 | if( i<=sCheck.nPage ){ |
| 51430 | 51636 | sCheck.anRef[i] = 1; |
| 51431 | 51637 | } |
| 51432 | 51638 | sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000); |
| 51639 | + sCheck.errMsg.useMalloc = 2; |
| 51433 | 51640 | |
| 51434 | 51641 | /* Check the integrity of the freelist |
| 51435 | 51642 | */ |
| 51436 | 51643 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 51437 | 51644 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
| | @@ -51526,10 +51733,33 @@ |
| 51526 | 51733 | SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){ |
| 51527 | 51734 | assert( p==0 || sqlite3_mutex_held(p->db->mutex) ); |
| 51528 | 51735 | return (p && (p->inTrans==TRANS_WRITE)); |
| 51529 | 51736 | } |
| 51530 | 51737 | |
| 51738 | +#ifndef SQLITE_OMIT_WAL |
| 51739 | +/* |
| 51740 | +** Run a checkpoint on the Btree passed as the first argument. |
| 51741 | +** |
| 51742 | +** Return SQLITE_LOCKED if this or any other connection has an open |
| 51743 | +** transaction on the shared-cache the argument Btree is connected to. |
| 51744 | +*/ |
| 51745 | +SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p){ |
| 51746 | + int rc = SQLITE_OK; |
| 51747 | + if( p ){ |
| 51748 | + BtShared *pBt = p->pBt; |
| 51749 | + sqlite3BtreeEnter(p); |
| 51750 | + if( pBt->inTransaction!=TRANS_NONE ){ |
| 51751 | + rc = SQLITE_LOCKED; |
| 51752 | + }else{ |
| 51753 | + rc = sqlite3PagerCheckpoint(pBt->pPager); |
| 51754 | + } |
| 51755 | + sqlite3BtreeLeave(p); |
| 51756 | + } |
| 51757 | + return rc; |
| 51758 | +} |
| 51759 | +#endif |
| 51760 | + |
| 51531 | 51761 | /* |
| 51532 | 51762 | ** Return non-zero if a read (or write) transaction is active. |
| 51533 | 51763 | */ |
| 51534 | 51764 | SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){ |
| 51535 | 51765 | assert( p ); |
| | @@ -51565,11 +51795,11 @@ |
| 51565 | 51795 | */ |
| 51566 | 51796 | SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ |
| 51567 | 51797 | BtShared *pBt = p->pBt; |
| 51568 | 51798 | sqlite3BtreeEnter(p); |
| 51569 | 51799 | if( !pBt->pSchema && nBytes ){ |
| 51570 | | - pBt->pSchema = sqlite3MallocZero(nBytes); |
| 51800 | + pBt->pSchema = sqlite3DbMallocZero(0, nBytes); |
| 51571 | 51801 | pBt->xFreeSchema = xFree; |
| 51572 | 51802 | } |
| 51573 | 51803 | sqlite3BtreeLeave(p); |
| 51574 | 51804 | return pBt->pSchema; |
| 51575 | 51805 | } |
| | @@ -53373,11 +53603,11 @@ |
| 53373 | 53603 | *ppVal = 0; |
| 53374 | 53604 | return SQLITE_OK; |
| 53375 | 53605 | } |
| 53376 | 53606 | op = pExpr->op; |
| 53377 | 53607 | |
| 53378 | | - /* op can only be TK_REGISTER is we have compiled with SQLITE_ENABLE_STAT2. |
| 53608 | + /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT2. |
| 53379 | 53609 | ** The ifdef here is to enable us to achieve 100% branch test coverage even |
| 53380 | 53610 | ** when SQLITE_ENABLE_STAT2 is omitted. |
| 53381 | 53611 | */ |
| 53382 | 53612 | #ifdef SQLITE_ENABLE_STAT2 |
| 53383 | 53613 | if( op==TK_REGISTER ) op = pExpr->op2; |
| | @@ -54051,47 +54281,55 @@ |
| 54051 | 54281 | if( ALWAYS(pDef) && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){ |
| 54052 | 54282 | sqlite3DbFree(db, pDef); |
| 54053 | 54283 | } |
| 54054 | 54284 | } |
| 54055 | 54285 | |
| 54286 | +static void vdbeFreeOpArray(sqlite3 *, Op *, int); |
| 54287 | + |
| 54056 | 54288 | /* |
| 54057 | 54289 | ** Delete a P4 value if necessary. |
| 54058 | 54290 | */ |
| 54059 | 54291 | static void freeP4(sqlite3 *db, int p4type, void *p4){ |
| 54060 | 54292 | if( p4 ){ |
| 54293 | + assert( db ); |
| 54061 | 54294 | switch( p4type ){ |
| 54062 | 54295 | case P4_REAL: |
| 54063 | 54296 | case P4_INT64: |
| 54064 | | - case P4_MPRINTF: |
| 54065 | 54297 | case P4_DYNAMIC: |
| 54066 | 54298 | case P4_KEYINFO: |
| 54067 | 54299 | case P4_INTARRAY: |
| 54068 | 54300 | case P4_KEYINFO_HANDOFF: { |
| 54069 | 54301 | sqlite3DbFree(db, p4); |
| 54070 | 54302 | break; |
| 54303 | + } |
| 54304 | + case P4_MPRINTF: { |
| 54305 | + if( db->pnBytesFreed==0 ) sqlite3_free(p4); |
| 54306 | + break; |
| 54071 | 54307 | } |
| 54072 | 54308 | case P4_VDBEFUNC: { |
| 54073 | 54309 | VdbeFunc *pVdbeFunc = (VdbeFunc *)p4; |
| 54074 | 54310 | freeEphemeralFunction(db, pVdbeFunc->pFunc); |
| 54075 | | - sqlite3VdbeDeleteAuxData(pVdbeFunc, 0); |
| 54311 | + if( db->pnBytesFreed==0 ) sqlite3VdbeDeleteAuxData(pVdbeFunc, 0); |
| 54076 | 54312 | sqlite3DbFree(db, pVdbeFunc); |
| 54077 | 54313 | break; |
| 54078 | 54314 | } |
| 54079 | 54315 | case P4_FUNCDEF: { |
| 54080 | 54316 | freeEphemeralFunction(db, (FuncDef*)p4); |
| 54081 | 54317 | break; |
| 54082 | 54318 | } |
| 54083 | 54319 | case P4_MEM: { |
| 54084 | | - sqlite3ValueFree((sqlite3_value*)p4); |
| 54320 | + if( db->pnBytesFreed==0 ){ |
| 54321 | + sqlite3ValueFree((sqlite3_value*)p4); |
| 54322 | + }else{ |
| 54323 | + Mem *p = (Mem*)p4; |
| 54324 | + sqlite3DbFree(db, p->zMalloc); |
| 54325 | + sqlite3DbFree(db, p); |
| 54326 | + } |
| 54085 | 54327 | break; |
| 54086 | 54328 | } |
| 54087 | 54329 | case P4_VTAB : { |
| 54088 | | - sqlite3VtabUnlock((VTable *)p4); |
| 54089 | | - break; |
| 54090 | | - } |
| 54091 | | - case P4_SUBPROGRAM : { |
| 54092 | | - sqlite3VdbeProgramDelete(db, (SubProgram *)p4, 1); |
| 54330 | + if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4); |
| 54093 | 54331 | break; |
| 54094 | 54332 | } |
| 54095 | 54333 | } |
| 54096 | 54334 | } |
| 54097 | 54335 | } |
| | @@ -54113,38 +54351,18 @@ |
| 54113 | 54351 | } |
| 54114 | 54352 | sqlite3DbFree(db, aOp); |
| 54115 | 54353 | } |
| 54116 | 54354 | |
| 54117 | 54355 | /* |
| 54118 | | -** Decrement the ref-count on the SubProgram structure passed as the |
| 54119 | | -** second argument. If the ref-count reaches zero, free the structure. |
| 54120 | | -** |
| 54121 | | -** The array of VDBE opcodes stored as SubProgram.aOp is freed if |
| 54122 | | -** either the ref-count reaches zero or parameter freeop is non-zero. |
| 54123 | | -** |
| 54124 | | -** Since the array of opcodes pointed to by SubProgram.aOp may directly |
| 54125 | | -** or indirectly contain a reference to the SubProgram structure itself. |
| 54126 | | -** By passing a non-zero freeop parameter, the caller may ensure that all |
| 54127 | | -** SubProgram structures and their aOp arrays are freed, even when there |
| 54128 | | -** are such circular references. |
| 54356 | +** Link the SubProgram object passed as the second argument into the linked |
| 54357 | +** list at Vdbe.pSubProgram. This list is used to delete all sub-program |
| 54358 | +** objects when the VM is no longer required. |
| 54129 | 54359 | */ |
| 54130 | | -SQLITE_PRIVATE void sqlite3VdbeProgramDelete(sqlite3 *db, SubProgram *p, int freeop){ |
| 54131 | | - if( p ){ |
| 54132 | | - assert( p->nRef>0 ); |
| 54133 | | - if( freeop || p->nRef==1 ){ |
| 54134 | | - Op *aOp = p->aOp; |
| 54135 | | - p->aOp = 0; |
| 54136 | | - vdbeFreeOpArray(db, aOp, p->nOp); |
| 54137 | | - p->nOp = 0; |
| 54138 | | - } |
| 54139 | | - p->nRef--; |
| 54140 | | - if( p->nRef==0 ){ |
| 54141 | | - sqlite3DbFree(db, p); |
| 54142 | | - } |
| 54143 | | - } |
| 54144 | | -} |
| 54145 | | - |
| 54360 | +SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){ |
| 54361 | + p->pNext = pVdbe->pProgram; |
| 54362 | + pVdbe->pProgram = p; |
| 54363 | +} |
| 54146 | 54364 | |
| 54147 | 54365 | /* |
| 54148 | 54366 | ** Change N opcodes starting at addr to No-ops. |
| 54149 | 54367 | */ |
| 54150 | 54368 | SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){ |
| | @@ -54217,11 +54435,11 @@ |
| 54217 | 54435 | KeyInfo *pKeyInfo; |
| 54218 | 54436 | int nField, nByte; |
| 54219 | 54437 | |
| 54220 | 54438 | nField = ((KeyInfo*)zP4)->nField; |
| 54221 | 54439 | nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField; |
| 54222 | | - pKeyInfo = sqlite3Malloc( nByte ); |
| 54440 | + pKeyInfo = sqlite3DbMallocRaw(0, nByte); |
| 54223 | 54441 | pOp->p4.pKeyInfo = pKeyInfo; |
| 54224 | 54442 | if( pKeyInfo ){ |
| 54225 | 54443 | u8 *aSortOrder; |
| 54226 | 54444 | memcpy((char*)pKeyInfo, zP4, nByte - nField); |
| 54227 | 54445 | aSortOrder = pKeyInfo->aSortOrder; |
| | @@ -54481,10 +54699,16 @@ |
| 54481 | 54699 | static void releaseMemArray(Mem *p, int N){ |
| 54482 | 54700 | if( p && N ){ |
| 54483 | 54701 | Mem *pEnd; |
| 54484 | 54702 | sqlite3 *db = p->db; |
| 54485 | 54703 | u8 malloc_failed = db->mallocFailed; |
| 54704 | + if( db->pnBytesFreed ){ |
| 54705 | + for(pEnd=&p[N]; p<pEnd; p++){ |
| 54706 | + sqlite3DbFree(db, p->zMalloc); |
| 54707 | + } |
| 54708 | + return; |
| 54709 | + } |
| 54486 | 54710 | for(pEnd=&p[N]; p<pEnd; p++){ |
| 54487 | 54711 | assert( (&p[1])==pEnd || p[0].db==p[1].db ); |
| 54488 | 54712 | |
| 54489 | 54713 | /* This block is really an inlined version of sqlite3VdbeMemRelease() |
| 54490 | 54714 | ** that takes advantage of the fact that the memory cell value is |
| | @@ -55130,26 +55354,27 @@ |
| 55130 | 55354 | ** be done before determining whether a master journal file is |
| 55131 | 55355 | ** required, as an xSync() callback may add an attached database |
| 55132 | 55356 | ** to the transaction. |
| 55133 | 55357 | */ |
| 55134 | 55358 | rc = sqlite3VtabSync(db, &p->zErrMsg); |
| 55135 | | - if( rc!=SQLITE_OK ){ |
| 55136 | | - return rc; |
| 55137 | | - } |
| 55138 | 55359 | |
| 55139 | 55360 | /* This loop determines (a) if the commit hook should be invoked and |
| 55140 | 55361 | ** (b) how many database files have open write transactions, not |
| 55141 | 55362 | ** including the temp database. (b) is important because if more than |
| 55142 | 55363 | ** one database file has an open write transaction, a master journal |
| 55143 | 55364 | ** file is required for an atomic commit. |
| 55144 | 55365 | */ |
| 55145 | | - for(i=0; i<db->nDb; i++){ |
| 55366 | + for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
| 55146 | 55367 | Btree *pBt = db->aDb[i].pBt; |
| 55147 | 55368 | if( sqlite3BtreeIsInTrans(pBt) ){ |
| 55148 | 55369 | needXcommit = 1; |
| 55149 | 55370 | if( i!=1 ) nTrans++; |
| 55371 | + rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt)); |
| 55150 | 55372 | } |
| 55373 | + } |
| 55374 | + if( rc!=SQLITE_OK ){ |
| 55375 | + return rc; |
| 55151 | 55376 | } |
| 55152 | 55377 | |
| 55153 | 55378 | /* If there are any write-transactions at all, invoke the commit hook */ |
| 55154 | 55379 | if( needXcommit && db->xCommitCallback ){ |
| 55155 | 55380 | rc = db->xCommitCallback(db->pCommitArg); |
| | @@ -55285,10 +55510,11 @@ |
| 55285 | 55510 | if( pBt ){ |
| 55286 | 55511 | rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster); |
| 55287 | 55512 | } |
| 55288 | 55513 | } |
| 55289 | 55514 | sqlite3OsCloseFree(pMaster); |
| 55515 | + assert( rc!=SQLITE_BUSY ); |
| 55290 | 55516 | if( rc!=SQLITE_OK ){ |
| 55291 | 55517 | sqlite3DbFree(db, zMaster); |
| 55292 | 55518 | return rc; |
| 55293 | 55519 | } |
| 55294 | 55520 | |
| | @@ -55812,10 +56038,34 @@ |
| 55812 | 56038 | } |
| 55813 | 56039 | pAux->pAux = 0; |
| 55814 | 56040 | } |
| 55815 | 56041 | } |
| 55816 | 56042 | } |
| 56043 | + |
| 56044 | +/* |
| 56045 | +** Free all memory associated with the Vdbe passed as the second argument. |
| 56046 | +** The difference between this function and sqlite3VdbeDelete() is that |
| 56047 | +** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with |
| 56048 | +** the database connection. |
| 56049 | +*/ |
| 56050 | +SQLITE_PRIVATE void sqlite3VdbeDeleteObject(sqlite3 *db, Vdbe *p){ |
| 56051 | + SubProgram *pSub, *pNext; |
| 56052 | + assert( p->db==0 || p->db==db ); |
| 56053 | + releaseMemArray(p->aVar, p->nVar); |
| 56054 | + releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); |
| 56055 | + for(pSub=p->pProgram; pSub; pSub=pNext){ |
| 56056 | + pNext = pSub->pNext; |
| 56057 | + vdbeFreeOpArray(db, pSub->aOp, pSub->nOp); |
| 56058 | + sqlite3DbFree(db, pSub); |
| 56059 | + } |
| 56060 | + vdbeFreeOpArray(db, p->aOp, p->nOp); |
| 56061 | + sqlite3DbFree(db, p->aLabel); |
| 56062 | + sqlite3DbFree(db, p->aColName); |
| 56063 | + sqlite3DbFree(db, p->zSql); |
| 56064 | + sqlite3DbFree(db, p->pFree); |
| 56065 | + sqlite3DbFree(db, p); |
| 56066 | +} |
| 55817 | 56067 | |
| 55818 | 56068 | /* |
| 55819 | 56069 | ** Delete an entire VDBE. |
| 55820 | 56070 | */ |
| 55821 | 56071 | SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){ |
| | @@ -55830,20 +56080,13 @@ |
| 55830 | 56080 | db->pVdbe = p->pNext; |
| 55831 | 56081 | } |
| 55832 | 56082 | if( p->pNext ){ |
| 55833 | 56083 | p->pNext->pPrev = p->pPrev; |
| 55834 | 56084 | } |
| 55835 | | - releaseMemArray(p->aVar, p->nVar); |
| 55836 | | - releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); |
| 55837 | | - vdbeFreeOpArray(db, p->aOp, p->nOp); |
| 55838 | | - sqlite3DbFree(db, p->aLabel); |
| 55839 | | - sqlite3DbFree(db, p->aColName); |
| 55840 | | - sqlite3DbFree(db, p->zSql); |
| 55841 | 56085 | p->magic = VDBE_MAGIC_DEAD; |
| 55842 | | - sqlite3DbFree(db, p->pFree); |
| 55843 | 56086 | p->db = 0; |
| 55844 | | - sqlite3DbFree(db, p); |
| 56087 | + sqlite3VdbeDeleteObject(db, p); |
| 55845 | 56088 | } |
| 55846 | 56089 | |
| 55847 | 56090 | /* |
| 55848 | 56091 | ** Make sure the cursor p is ready to read or write the row to which it |
| 55849 | 56092 | ** was last positioned. Return an error code if an OOM fault or I/O error |
| | @@ -55865,15 +56108,12 @@ |
| 55865 | 56108 | #endif |
| 55866 | 56109 | assert( p->isTable ); |
| 55867 | 56110 | rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res); |
| 55868 | 56111 | if( rc ) return rc; |
| 55869 | 56112 | p->lastRowid = p->movetoTarget; |
| 55870 | | - p->rowidIsValid = ALWAYS(res==0) ?1:0; |
| 55871 | | - if( NEVER(res<0) ){ |
| 55872 | | - rc = sqlite3BtreeNext(p->pCursor, &res); |
| 55873 | | - if( rc ) return rc; |
| 55874 | | - } |
| 56113 | + if( res!=0 ) return SQLITE_CORRUPT_BKPT; |
| 56114 | + p->rowidIsValid = 1; |
| 55875 | 56115 | #ifdef SQLITE_TEST |
| 55876 | 56116 | sqlite3_search_count++; |
| 55877 | 56117 | #endif |
| 55878 | 56118 | p->deferredMoveto = 0; |
| 55879 | 56119 | p->cacheStatus = CACHE_STALE; |
| | @@ -57013,11 +57253,11 @@ |
| 57013 | 57253 | /* Invoke the profile callback if there is one |
| 57014 | 57254 | */ |
| 57015 | 57255 | if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){ |
| 57016 | 57256 | sqlite3_int64 iNow; |
| 57017 | 57257 | sqlite3OsCurrentTimeInt64(db->pVfs, &iNow); |
| 57018 | | - db->xProfile(db->pProfileArg, p->zSql, iNow - p->startTime); |
| 57258 | + db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000); |
| 57019 | 57259 | } |
| 57020 | 57260 | #endif |
| 57021 | 57261 | |
| 57022 | 57262 | if( rc==SQLITE_DONE ){ |
| 57023 | 57263 | assert( p->rc==SQLITE_OK ); |
| | @@ -58628,10 +58868,24 @@ |
| 58628 | 58868 | for(p=db->pSavepoint; p; p=p->pNext) n++; |
| 58629 | 58869 | assert( n==(db->nSavepoint + db->isTransactionSavepoint) ); |
| 58630 | 58870 | return 1; |
| 58631 | 58871 | } |
| 58632 | 58872 | #endif |
| 58873 | + |
| 58874 | +/* |
| 58875 | +** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored |
| 58876 | +** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored |
| 58877 | +** in memory obtained from sqlite3DbMalloc). |
| 58878 | +*/ |
| 58879 | +static void importVtabErrMsg(Vdbe *p, sqlite3_vtab *pVtab){ |
| 58880 | + sqlite3 *db = p->db; |
| 58881 | + sqlite3DbFree(db, p->zErrMsg); |
| 58882 | + p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg); |
| 58883 | + sqlite3_free(pVtab->zErrMsg); |
| 58884 | + pVtab->zErrMsg = 0; |
| 58885 | +} |
| 58886 | + |
| 58633 | 58887 | |
| 58634 | 58888 | /* |
| 58635 | 58889 | ** Execute as much of a VDBE program as we can then return. |
| 58636 | 58890 | ** |
| 58637 | 58891 | ** sqlite3VdbeMakeReady() must be called before this routine in order to |
| | @@ -62627,13 +62881,11 @@ |
| 62627 | 62881 | }else if( u.bi.pC->pVtabCursor ){ |
| 62628 | 62882 | u.bi.pVtab = u.bi.pC->pVtabCursor->pVtab; |
| 62629 | 62883 | u.bi.pModule = u.bi.pVtab->pModule; |
| 62630 | 62884 | assert( u.bi.pModule->xRowid ); |
| 62631 | 62885 | rc = u.bi.pModule->xRowid(u.bi.pC->pVtabCursor, &u.bi.v); |
| 62632 | | - sqlite3DbFree(db, p->zErrMsg); |
| 62633 | | - p->zErrMsg = u.bi.pVtab->zErrMsg; |
| 62634 | | - u.bi.pVtab->zErrMsg = 0; |
| 62886 | + importVtabErrMsg(p, u.bi.pVtab); |
| 62635 | 62887 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 62636 | 62888 | }else{ |
| 62637 | 62889 | assert( u.bi.pC->pCursor!=0 ); |
| 62638 | 62890 | rc = sqlite3VdbeCursorMoveto(u.bi.pC); |
| 62639 | 62891 | if( rc ) goto abort_due_to_error; |
| | @@ -64018,15 +64270,11 @@ |
| 64018 | 64270 | #if 0 /* local variables moved into u.cf */ |
| 64019 | 64271 | VTable *pVTab; |
| 64020 | 64272 | #endif /* local variables moved into u.cf */ |
| 64021 | 64273 | u.cf.pVTab = pOp->p4.pVtab; |
| 64022 | 64274 | rc = sqlite3VtabBegin(db, u.cf.pVTab); |
| 64023 | | - if( u.cf.pVTab ){ |
| 64024 | | - sqlite3DbFree(db, p->zErrMsg); |
| 64025 | | - p->zErrMsg = u.cf.pVTab->pVtab->zErrMsg; |
| 64026 | | - u.cf.pVTab->pVtab->zErrMsg = 0; |
| 64027 | | - } |
| 64275 | + if( u.cf.pVTab ) importVtabErrMsg(p, u.cf.pVTab->pVtab); |
| 64028 | 64276 | break; |
| 64029 | 64277 | } |
| 64030 | 64278 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 64031 | 64279 | |
| 64032 | 64280 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| | @@ -64074,13 +64322,11 @@ |
| 64074 | 64322 | u.cg.pVtabCursor = 0; |
| 64075 | 64323 | u.cg.pVtab = pOp->p4.pVtab->pVtab; |
| 64076 | 64324 | u.cg.pModule = (sqlite3_module *)u.cg.pVtab->pModule; |
| 64077 | 64325 | assert(u.cg.pVtab && u.cg.pModule); |
| 64078 | 64326 | rc = u.cg.pModule->xOpen(u.cg.pVtab, &u.cg.pVtabCursor); |
| 64079 | | - sqlite3DbFree(db, p->zErrMsg); |
| 64080 | | - p->zErrMsg = u.cg.pVtab->zErrMsg; |
| 64081 | | - u.cg.pVtab->zErrMsg = 0; |
| 64327 | + importVtabErrMsg(p, u.cg.pVtab); |
| 64082 | 64328 | if( SQLITE_OK==rc ){ |
| 64083 | 64329 | /* Initialize sqlite3_vtab_cursor base class */ |
| 64084 | 64330 | u.cg.pVtabCursor->pVtab = u.cg.pVtab; |
| 64085 | 64331 | |
| 64086 | 64332 | /* Initialise vdbe cursor object */ |
| | @@ -64155,13 +64401,11 @@ |
| 64155 | 64401 | } |
| 64156 | 64402 | |
| 64157 | 64403 | p->inVtabMethod = 1; |
| 64158 | 64404 | rc = u.ch.pModule->xFilter(u.ch.pVtabCursor, u.ch.iQuery, pOp->p4.z, u.ch.nArg, u.ch.apArg); |
| 64159 | 64405 | p->inVtabMethod = 0; |
| 64160 | | - sqlite3DbFree(db, p->zErrMsg); |
| 64161 | | - p->zErrMsg = u.ch.pVtab->zErrMsg; |
| 64162 | | - u.ch.pVtab->zErrMsg = 0; |
| 64406 | + importVtabErrMsg(p, u.ch.pVtab); |
| 64163 | 64407 | if( rc==SQLITE_OK ){ |
| 64164 | 64408 | u.ch.res = u.ch.pModule->xEof(u.ch.pVtabCursor); |
| 64165 | 64409 | } |
| 64166 | 64410 | |
| 64167 | 64411 | if( u.ch.res ){ |
| | @@ -64209,13 +64453,11 @@ |
| 64209 | 64453 | */ |
| 64210 | 64454 | sqlite3VdbeMemMove(&u.ci.sContext.s, u.ci.pDest); |
| 64211 | 64455 | MemSetTypeFlag(&u.ci.sContext.s, MEM_Null); |
| 64212 | 64456 | |
| 64213 | 64457 | rc = u.ci.pModule->xColumn(pCur->pVtabCursor, &u.ci.sContext, pOp->p2); |
| 64214 | | - sqlite3DbFree(db, p->zErrMsg); |
| 64215 | | - p->zErrMsg = u.ci.pVtab->zErrMsg; |
| 64216 | | - u.ci.pVtab->zErrMsg = 0; |
| 64458 | + importVtabErrMsg(p, u.ci.pVtab); |
| 64217 | 64459 | if( u.ci.sContext.isError ){ |
| 64218 | 64460 | rc = u.ci.sContext.isError; |
| 64219 | 64461 | } |
| 64220 | 64462 | |
| 64221 | 64463 | /* Copy the result of the function to the P3 register. We |
| | @@ -64266,13 +64508,11 @@ |
| 64266 | 64508 | ** some other method is next invoked on the save virtual table cursor. |
| 64267 | 64509 | */ |
| 64268 | 64510 | p->inVtabMethod = 1; |
| 64269 | 64511 | rc = u.cj.pModule->xNext(u.cj.pCur->pVtabCursor); |
| 64270 | 64512 | p->inVtabMethod = 0; |
| 64271 | | - sqlite3DbFree(db, p->zErrMsg); |
| 64272 | | - p->zErrMsg = u.cj.pVtab->zErrMsg; |
| 64273 | | - u.cj.pVtab->zErrMsg = 0; |
| 64513 | + importVtabErrMsg(p, u.cj.pVtab); |
| 64274 | 64514 | if( rc==SQLITE_OK ){ |
| 64275 | 64515 | u.cj.res = u.cj.pModule->xEof(u.cj.pCur->pVtabCursor); |
| 64276 | 64516 | } |
| 64277 | 64517 | |
| 64278 | 64518 | if( !u.cj.res ){ |
| | @@ -64300,13 +64540,11 @@ |
| 64300 | 64540 | u.ck.pName = &aMem[pOp->p1]; |
| 64301 | 64541 | assert( u.ck.pVtab->pModule->xRename ); |
| 64302 | 64542 | REGISTER_TRACE(pOp->p1, u.ck.pName); |
| 64303 | 64543 | assert( u.ck.pName->flags & MEM_Str ); |
| 64304 | 64544 | rc = u.ck.pVtab->pModule->xRename(u.ck.pVtab, u.ck.pName->z); |
| 64305 | | - sqlite3DbFree(db, p->zErrMsg); |
| 64306 | | - p->zErrMsg = u.ck.pVtab->zErrMsg; |
| 64307 | | - u.ck.pVtab->zErrMsg = 0; |
| 64545 | + importVtabErrMsg(p, u.ck.pVtab); |
| 64308 | 64546 | |
| 64309 | 64547 | break; |
| 64310 | 64548 | } |
| 64311 | 64549 | #endif |
| 64312 | 64550 | |
| | @@ -64356,13 +64594,11 @@ |
| 64356 | 64594 | sqlite3VdbeMemStoreType(u.cl.pX); |
| 64357 | 64595 | u.cl.apArg[u.cl.i] = u.cl.pX; |
| 64358 | 64596 | u.cl.pX++; |
| 64359 | 64597 | } |
| 64360 | 64598 | rc = u.cl.pModule->xUpdate(u.cl.pVtab, u.cl.nArg, u.cl.apArg, &u.cl.rowid); |
| 64361 | | - sqlite3DbFree(db, p->zErrMsg); |
| 64362 | | - p->zErrMsg = u.cl.pVtab->zErrMsg; |
| 64363 | | - u.cl.pVtab->zErrMsg = 0; |
| 64599 | + importVtabErrMsg(p, u.cl.pVtab); |
| 64364 | 64600 | if( rc==SQLITE_OK && pOp->p1 ){ |
| 64365 | 64601 | assert( u.cl.nArg>1 && u.cl.apArg[0] && (u.cl.apArg[0]->flags&MEM_Null) ); |
| 64366 | 64602 | db->lastRowid = u.cl.rowid; |
| 64367 | 64603 | } |
| 64368 | 64604 | p->nChange++; |
| | @@ -66811,30 +67047,37 @@ |
| 66811 | 67047 | assert( pExpr->pTab && j<pExpr->pTab->nCol ); |
| 66812 | 67048 | return pExpr->pTab->aCol[j].affinity; |
| 66813 | 67049 | } |
| 66814 | 67050 | return pExpr->affinity; |
| 66815 | 67051 | } |
| 67052 | + |
| 67053 | +/* |
| 67054 | +** Set the explicit collating sequence for an expression to the |
| 67055 | +** collating sequence supplied in the second argument. |
| 67056 | +*/ |
| 67057 | +SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Expr *pExpr, CollSeq *pColl){ |
| 67058 | + if( pExpr && pColl ){ |
| 67059 | + pExpr->pColl = pColl; |
| 67060 | + pExpr->flags |= EP_ExpCollate; |
| 67061 | + } |
| 67062 | + return pExpr; |
| 67063 | +} |
| 66816 | 67064 | |
| 66817 | 67065 | /* |
| 66818 | 67066 | ** Set the collating sequence for expression pExpr to be the collating |
| 66819 | 67067 | ** sequence named by pToken. Return a pointer to the revised expression. |
| 66820 | 67068 | ** The collating sequence is marked as "explicit" using the EP_ExpCollate |
| 66821 | 67069 | ** flag. An explicit collating sequence will override implicit |
| 66822 | 67070 | ** collating sequences. |
| 66823 | 67071 | */ |
| 66824 | | -SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pCollName){ |
| 67072 | +SQLITE_PRIVATE Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr *pExpr, Token *pCollName){ |
| 66825 | 67073 | char *zColl = 0; /* Dequoted name of collation sequence */ |
| 66826 | 67074 | CollSeq *pColl; |
| 66827 | 67075 | sqlite3 *db = pParse->db; |
| 66828 | 67076 | zColl = sqlite3NameFromToken(db, pCollName); |
| 66829 | | - if( pExpr && zColl ){ |
| 66830 | | - pColl = sqlite3LocateCollSeq(pParse, zColl); |
| 66831 | | - if( pColl ){ |
| 66832 | | - pExpr->pColl = pColl; |
| 66833 | | - pExpr->flags |= EP_ExpCollate; |
| 66834 | | - } |
| 66835 | | - } |
| 67077 | + pColl = sqlite3LocateCollSeq(pParse, zColl); |
| 67078 | + sqlite3ExprSetColl(pExpr, pColl); |
| 66836 | 67079 | sqlite3DbFree(db, zColl); |
| 66837 | 67080 | return pExpr; |
| 66838 | 67081 | } |
| 66839 | 67082 | |
| 66840 | 67083 | /* |
| | @@ -68262,18 +68505,24 @@ |
| 68262 | 68505 | |
| 68263 | 68506 | if( eType==0 ){ |
| 68264 | 68507 | /* Could not found an existing table or index to use as the RHS b-tree. |
| 68265 | 68508 | ** We will have to generate an ephemeral table to do the job. |
| 68266 | 68509 | */ |
| 68510 | + double savedNQueryLoop = pParse->nQueryLoop; |
| 68267 | 68511 | int rMayHaveNull = 0; |
| 68268 | 68512 | eType = IN_INDEX_EPH; |
| 68269 | 68513 | if( prNotFound ){ |
| 68270 | 68514 | *prNotFound = rMayHaveNull = ++pParse->nMem; |
| 68271 | | - }else if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){ |
| 68272 | | - eType = IN_INDEX_ROWID; |
| 68515 | + }else{ |
| 68516 | + testcase( pParse->nQueryLoop>(double)1 ); |
| 68517 | + pParse->nQueryLoop = (double)1; |
| 68518 | + if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){ |
| 68519 | + eType = IN_INDEX_ROWID; |
| 68520 | + } |
| 68273 | 68521 | } |
| 68274 | 68522 | sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID); |
| 68523 | + pParse->nQueryLoop = savedNQueryLoop; |
| 68275 | 68524 | }else{ |
| 68276 | 68525 | pX->iTable = iTab; |
| 68277 | 68526 | } |
| 68278 | 68527 | return eType; |
| 68279 | 68528 | } |
| | @@ -71302,11 +71551,10 @@ |
| 71302 | 71551 | */ |
| 71303 | 71552 | pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table)); |
| 71304 | 71553 | if( !pNew ) goto exit_begin_add_column; |
| 71305 | 71554 | pParse->pNewTable = pNew; |
| 71306 | 71555 | pNew->nRef = 1; |
| 71307 | | - pNew->dbMem = pTab->dbMem; |
| 71308 | 71556 | pNew->nCol = pTab->nCol; |
| 71309 | 71557 | assert( pNew->nCol>0 ); |
| 71310 | 71558 | nAlloc = (((pNew->nCol-1)/8)*8)+8; |
| 71311 | 71559 | assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 ); |
| 71312 | 71560 | pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc); |
| | @@ -71831,23 +72079,21 @@ |
| 71831 | 72079 | |
| 71832 | 72080 | /* |
| 71833 | 72081 | ** If the Index.aSample variable is not NULL, delete the aSample[] array |
| 71834 | 72082 | ** and its contents. |
| 71835 | 72083 | */ |
| 71836 | | -SQLITE_PRIVATE void sqlite3DeleteIndexSamples(Index *pIdx){ |
| 72084 | +SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){ |
| 71837 | 72085 | #ifdef SQLITE_ENABLE_STAT2 |
| 71838 | 72086 | if( pIdx->aSample ){ |
| 71839 | 72087 | int j; |
| 71840 | | - sqlite3 *dbMem = pIdx->pTable->dbMem; |
| 71841 | 72088 | for(j=0; j<SQLITE_INDEX_SAMPLES; j++){ |
| 71842 | 72089 | IndexSample *p = &pIdx->aSample[j]; |
| 71843 | 72090 | if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){ |
| 71844 | | - sqlite3DbFree(pIdx->pTable->dbMem, p->u.z); |
| 72091 | + sqlite3DbFree(db, p->u.z); |
| 71845 | 72092 | } |
| 71846 | 72093 | } |
| 71847 | | - sqlite3DbFree(dbMem, pIdx->aSample); |
| 71848 | | - pIdx->aSample = 0; |
| 72094 | + sqlite3DbFree(db, pIdx->aSample); |
| 71849 | 72095 | } |
| 71850 | 72096 | #else |
| 71851 | 72097 | UNUSED_PARAMETER(pIdx); |
| 71852 | 72098 | #endif |
| 71853 | 72099 | } |
| | @@ -71884,11 +72130,12 @@ |
| 71884 | 72130 | |
| 71885 | 72131 | /* Clear any prior statistics */ |
| 71886 | 72132 | for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ |
| 71887 | 72133 | Index *pIdx = sqliteHashData(i); |
| 71888 | 72134 | sqlite3DefaultRowEst(pIdx); |
| 71889 | | - sqlite3DeleteIndexSamples(pIdx); |
| 72135 | + sqlite3DeleteIndexSamples(db, pIdx); |
| 72136 | + pIdx->aSample = 0; |
| 71890 | 72137 | } |
| 71891 | 72138 | |
| 71892 | 72139 | /* Check to make sure the sqlite_stat1 table exists */ |
| 71893 | 72140 | sInfo.db = db; |
| 71894 | 72141 | sInfo.zDatabase = db->aDb[iDb].zName; |
| | @@ -71928,22 +72175,21 @@ |
| 71928 | 72175 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 71929 | 72176 | char *zIndex = (char *)sqlite3_column_text(pStmt, 0); |
| 71930 | 72177 | Index *pIdx = sqlite3FindIndex(db, zIndex, sInfo.zDatabase); |
| 71931 | 72178 | if( pIdx ){ |
| 71932 | 72179 | int iSample = sqlite3_column_int(pStmt, 1); |
| 71933 | | - sqlite3 *dbMem = pIdx->pTable->dbMem; |
| 71934 | | - assert( dbMem==db || dbMem==0 ); |
| 71935 | 72180 | if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){ |
| 71936 | 72181 | int eType = sqlite3_column_type(pStmt, 2); |
| 71937 | 72182 | |
| 71938 | 72183 | if( pIdx->aSample==0 ){ |
| 71939 | 72184 | static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES; |
| 71940 | | - pIdx->aSample = (IndexSample *)sqlite3DbMallocZero(dbMem, sz); |
| 72185 | + pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz); |
| 71941 | 72186 | if( pIdx->aSample==0 ){ |
| 71942 | 72187 | db->mallocFailed = 1; |
| 71943 | 72188 | break; |
| 71944 | 72189 | } |
| 72190 | + memset(pIdx->aSample, 0, sz); |
| 71945 | 72191 | } |
| 71946 | 72192 | |
| 71947 | 72193 | assert( pIdx->aSample ); |
| 71948 | 72194 | { |
| 71949 | 72195 | IndexSample *pSample = &pIdx->aSample[iSample]; |
| | @@ -71962,14 +72208,12 @@ |
| 71962 | 72208 | } |
| 71963 | 72209 | pSample->nByte = (u8)n; |
| 71964 | 72210 | if( n < 1){ |
| 71965 | 72211 | pSample->u.z = 0; |
| 71966 | 72212 | }else{ |
| 71967 | | - pSample->u.z = sqlite3DbMallocRaw(dbMem, n); |
| 71968 | | - if( pSample->u.z ){ |
| 71969 | | - memcpy(pSample->u.z, z, n); |
| 71970 | | - }else{ |
| 72213 | + pSample->u.z = sqlite3DbStrNDup(0, z, n); |
| 72214 | + if( pSample->u.z==0 ){ |
| 71971 | 72215 | db->mallocFailed = 1; |
| 71972 | 72216 | break; |
| 71973 | 72217 | } |
| 71974 | 72218 | } |
| 71975 | 72219 | } |
| | @@ -73128,37 +73372,18 @@ |
| 73128 | 73372 | } |
| 73129 | 73373 | |
| 73130 | 73374 | /* |
| 73131 | 73375 | ** Reclaim the memory used by an index |
| 73132 | 73376 | */ |
| 73133 | | -static void freeIndex(Index *p){ |
| 73134 | | - sqlite3 *db = p->pTable->dbMem; |
| 73377 | +static void freeIndex(sqlite3 *db, Index *p){ |
| 73135 | 73378 | #ifndef SQLITE_OMIT_ANALYZE |
| 73136 | | - sqlite3DeleteIndexSamples(p); |
| 73379 | + sqlite3DeleteIndexSamples(db, p); |
| 73137 | 73380 | #endif |
| 73138 | 73381 | sqlite3DbFree(db, p->zColAff); |
| 73139 | 73382 | sqlite3DbFree(db, p); |
| 73140 | 73383 | } |
| 73141 | 73384 | |
| 73142 | | -/* |
| 73143 | | -** Remove the given index from the index hash table, and free |
| 73144 | | -** its memory structures. |
| 73145 | | -** |
| 73146 | | -** The index is removed from the database hash tables but |
| 73147 | | -** it is not unlinked from the Table that it indexes. |
| 73148 | | -** Unlinking from the Table must be done by the calling function. |
| 73149 | | -*/ |
| 73150 | | -static void sqlite3DeleteIndex(Index *p){ |
| 73151 | | - Index *pOld; |
| 73152 | | - const char *zName = p->zName; |
| 73153 | | - |
| 73154 | | - pOld = sqlite3HashInsert(&p->pSchema->idxHash, zName, |
| 73155 | | - sqlite3Strlen30(zName), 0); |
| 73156 | | - assert( pOld==0 || pOld==p ); |
| 73157 | | - freeIndex(p); |
| 73158 | | -} |
| 73159 | | - |
| 73160 | 73385 | /* |
| 73161 | 73386 | ** For the index called zIdxName which is found in the database iDb, |
| 73162 | 73387 | ** unlike that index from its Table then remove the index from |
| 73163 | 73388 | ** the index hash table and free all memory structures associated |
| 73164 | 73389 | ** with the index. |
| | @@ -73181,11 +73406,11 @@ |
| 73181 | 73406 | while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; } |
| 73182 | 73407 | if( ALWAYS(p && p->pNext==pIndex) ){ |
| 73183 | 73408 | p->pNext = pIndex->pNext; |
| 73184 | 73409 | } |
| 73185 | 73410 | } |
| 73186 | | - freeIndex(pIndex); |
| 73411 | + freeIndex(db, pIndex); |
| 73187 | 73412 | } |
| 73188 | 73413 | db->flags |= SQLITE_InternChanges; |
| 73189 | 73414 | } |
| 73190 | 73415 | |
| 73191 | 73416 | /* |
| | @@ -73252,17 +73477,16 @@ |
| 73252 | 73477 | SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){ |
| 73253 | 73478 | db->flags &= ~SQLITE_InternChanges; |
| 73254 | 73479 | } |
| 73255 | 73480 | |
| 73256 | 73481 | /* |
| 73257 | | -** Clear the column names from a table or view. |
| 73482 | +** Delete memory allocated for the column names of a table or view (the |
| 73483 | +** Table.aCol[] array). |
| 73258 | 73484 | */ |
| 73259 | | -static void sqliteResetColumnNames(Table *pTable){ |
| 73485 | +static void sqliteDeleteColumnNames(sqlite3 *db, Table *pTable){ |
| 73260 | 73486 | int i; |
| 73261 | 73487 | Column *pCol; |
| 73262 | | - sqlite3 *db = pTable->dbMem; |
| 73263 | | - testcase( db==0 ); |
| 73264 | 73488 | assert( pTable!=0 ); |
| 73265 | 73489 | if( (pCol = pTable->aCol)!=0 ){ |
| 73266 | 73490 | for(i=0; i<pTable->nCol; i++, pCol++){ |
| 73267 | 73491 | sqlite3DbFree(db, pCol->zName); |
| 73268 | 73492 | sqlite3ExprDelete(db, pCol->pDflt); |
| | @@ -73270,12 +73494,10 @@ |
| 73270 | 73494 | sqlite3DbFree(db, pCol->zType); |
| 73271 | 73495 | sqlite3DbFree(db, pCol->zColl); |
| 73272 | 73496 | } |
| 73273 | 73497 | sqlite3DbFree(db, pTable->aCol); |
| 73274 | 73498 | } |
| 73275 | | - pTable->aCol = 0; |
| 73276 | | - pTable->nCol = 0; |
| 73277 | 73499 | } |
| 73278 | 73500 | |
| 73279 | 73501 | /* |
| 73280 | 73502 | ** Remove the memory data structures associated with the given |
| 73281 | 73503 | ** Table. No changes are made to disk by this routine. |
| | @@ -73283,46 +73505,48 @@ |
| 73283 | 73505 | ** This routine just deletes the data structure. It does not unlink |
| 73284 | 73506 | ** the table data structure from the hash table. But it does destroy |
| 73285 | 73507 | ** memory structures of the indices and foreign keys associated with |
| 73286 | 73508 | ** the table. |
| 73287 | 73509 | */ |
| 73288 | | -SQLITE_PRIVATE void sqlite3DeleteTable(Table *pTable){ |
| 73510 | +SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){ |
| 73289 | 73511 | Index *pIndex, *pNext; |
| 73290 | | - sqlite3 *db; |
| 73291 | 73512 | |
| 73292 | | - if( pTable==0 ) return; |
| 73293 | | - db = pTable->dbMem; |
| 73294 | | - testcase( db==0 ); |
| 73513 | + assert( !pTable || pTable->nRef>0 ); |
| 73295 | 73514 | |
| 73296 | 73515 | /* Do not delete the table until the reference count reaches zero. */ |
| 73297 | | - pTable->nRef--; |
| 73298 | | - if( pTable->nRef>0 ){ |
| 73299 | | - return; |
| 73300 | | - } |
| 73301 | | - assert( pTable->nRef==0 ); |
| 73302 | | - |
| 73303 | | - /* Delete all indices associated with this table |
| 73304 | | - */ |
| 73516 | + if( !pTable ) return; |
| 73517 | + if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return; |
| 73518 | + |
| 73519 | + /* Delete all indices associated with this table. */ |
| 73305 | 73520 | for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ |
| 73306 | 73521 | pNext = pIndex->pNext; |
| 73307 | 73522 | assert( pIndex->pSchema==pTable->pSchema ); |
| 73308 | | - sqlite3DeleteIndex(pIndex); |
| 73523 | + if( !db || db->pnBytesFreed==0 ){ |
| 73524 | + char *zName = pIndex->zName; |
| 73525 | + TESTONLY ( Index *pOld = ) sqlite3HashInsert( |
| 73526 | + &pIndex->pSchema->idxHash, zName, sqlite3Strlen30(zName), 0 |
| 73527 | + ); |
| 73528 | + assert( pOld==pIndex || pOld==0 ); |
| 73529 | + } |
| 73530 | + freeIndex(db, pIndex); |
| 73309 | 73531 | } |
| 73310 | 73532 | |
| 73311 | 73533 | /* Delete any foreign keys attached to this table. */ |
| 73312 | | - sqlite3FkDelete(pTable); |
| 73534 | + sqlite3FkDelete(db, pTable); |
| 73313 | 73535 | |
| 73314 | 73536 | /* Delete the Table structure itself. |
| 73315 | 73537 | */ |
| 73316 | | - sqliteResetColumnNames(pTable); |
| 73538 | + sqliteDeleteColumnNames(db, pTable); |
| 73317 | 73539 | sqlite3DbFree(db, pTable->zName); |
| 73318 | 73540 | sqlite3DbFree(db, pTable->zColAff); |
| 73319 | 73541 | sqlite3SelectDelete(db, pTable->pSelect); |
| 73320 | 73542 | #ifndef SQLITE_OMIT_CHECK |
| 73321 | 73543 | sqlite3ExprDelete(db, pTable->pCheck); |
| 73322 | 73544 | #endif |
| 73323 | | - sqlite3VtabClear(pTable); |
| 73545 | +#ifndef SQLITE_OMIT_VIRTUALTABLE |
| 73546 | + sqlite3VtabClear(db, pTable); |
| 73547 | +#endif |
| 73324 | 73548 | sqlite3DbFree(db, pTable); |
| 73325 | 73549 | } |
| 73326 | 73550 | |
| 73327 | 73551 | /* |
| 73328 | 73552 | ** Unlink the given table from the hash tables and the delete the |
| | @@ -73337,11 +73561,11 @@ |
| 73337 | 73561 | assert( zTabName ); |
| 73338 | 73562 | testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */ |
| 73339 | 73563 | pDb = &db->aDb[iDb]; |
| 73340 | 73564 | p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, |
| 73341 | 73565 | sqlite3Strlen30(zTabName),0); |
| 73342 | | - sqlite3DeleteTable(p); |
| 73566 | + sqlite3DeleteTable(db, p); |
| 73343 | 73567 | db->flags |= SQLITE_InternChanges; |
| 73344 | 73568 | } |
| 73345 | 73569 | |
| 73346 | 73570 | /* |
| 73347 | 73571 | ** Given a token, return a string that consists of the text of that |
| | @@ -73605,11 +73829,10 @@ |
| 73605 | 73829 | } |
| 73606 | 73830 | pTable->zName = zName; |
| 73607 | 73831 | pTable->iPKey = -1; |
| 73608 | 73832 | pTable->pSchema = db->aDb[iDb].pSchema; |
| 73609 | 73833 | pTable->nRef = 1; |
| 73610 | | - pTable->dbMem = 0; |
| 73611 | 73834 | assert( pParse->pNewTable==0 ); |
| 73612 | 73835 | pParse->pNewTable = pTable; |
| 73613 | 73836 | |
| 73614 | 73837 | /* If this is the magic sqlite_sequence table used by autoincrement, |
| 73615 | 73838 | ** then record a pointer to this table in the main database structure |
| | @@ -74157,11 +74380,11 @@ |
| 74157 | 74380 | zSep = "\n "; |
| 74158 | 74381 | zSep2 = ",\n "; |
| 74159 | 74382 | zEnd = "\n)"; |
| 74160 | 74383 | } |
| 74161 | 74384 | n += 35 + 6*p->nCol; |
| 74162 | | - zStmt = sqlite3Malloc( n ); |
| 74385 | + zStmt = sqlite3DbMallocRaw(0, n); |
| 74163 | 74386 | if( zStmt==0 ){ |
| 74164 | 74387 | db->mallocFailed = 1; |
| 74165 | 74388 | return 0; |
| 74166 | 74389 | } |
| 74167 | 74390 | sqlite3_snprintf(n, zStmt, "CREATE TABLE "); |
| | @@ -74338,11 +74561,11 @@ |
| 74338 | 74561 | assert( p->aCol==0 ); |
| 74339 | 74562 | p->nCol = pSelTab->nCol; |
| 74340 | 74563 | p->aCol = pSelTab->aCol; |
| 74341 | 74564 | pSelTab->nCol = 0; |
| 74342 | 74565 | pSelTab->aCol = 0; |
| 74343 | | - sqlite3DeleteTable(pSelTab); |
| 74566 | + sqlite3DeleteTable(db, pSelTab); |
| 74344 | 74567 | } |
| 74345 | 74568 | } |
| 74346 | 74569 | |
| 74347 | 74570 | /* Compute the complete text of the CREATE statement */ |
| 74348 | 74571 | if( pSelect ){ |
| | @@ -74582,11 +74805,11 @@ |
| 74582 | 74805 | assert( pTable->aCol==0 ); |
| 74583 | 74806 | pTable->nCol = pSelTab->nCol; |
| 74584 | 74807 | pTable->aCol = pSelTab->aCol; |
| 74585 | 74808 | pSelTab->nCol = 0; |
| 74586 | 74809 | pSelTab->aCol = 0; |
| 74587 | | - sqlite3DeleteTable(pSelTab); |
| 74810 | + sqlite3DeleteTable(db, pSelTab); |
| 74588 | 74811 | pTable->pSchema->flags |= DB_UnresetViews; |
| 74589 | 74812 | }else{ |
| 74590 | 74813 | pTable->nCol = 0; |
| 74591 | 74814 | nErr++; |
| 74592 | 74815 | } |
| | @@ -74607,11 +74830,13 @@ |
| 74607 | 74830 | HashElem *i; |
| 74608 | 74831 | if( !DbHasProperty(db, idx, DB_UnresetViews) ) return; |
| 74609 | 74832 | for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){ |
| 74610 | 74833 | Table *pTab = sqliteHashData(i); |
| 74611 | 74834 | if( pTab->pSelect ){ |
| 74612 | | - sqliteResetColumnNames(pTab); |
| 74835 | + sqliteDeleteColumnNames(db, pTab); |
| 74836 | + pTab->aCol = 0; |
| 74837 | + pTab->nCol = 0; |
| 74613 | 74838 | } |
| 74614 | 74839 | } |
| 74615 | 74840 | DbClearProperty(db, idx, DB_UnresetViews); |
| 74616 | 74841 | } |
| 74617 | 74842 | #else |
| | @@ -75604,11 +75829,11 @@ |
| 75604 | 75829 | } |
| 75605 | 75830 | |
| 75606 | 75831 | /* Clean up before exiting */ |
| 75607 | 75832 | exit_create_index: |
| 75608 | 75833 | if( pIndex ){ |
| 75609 | | - sqlite3_free(pIndex->zColAff); |
| 75834 | + sqlite3DbFree(db, pIndex->zColAff); |
| 75610 | 75835 | sqlite3DbFree(db, pIndex); |
| 75611 | 75836 | } |
| 75612 | 75837 | sqlite3ExprListDelete(db, pList); |
| 75613 | 75838 | sqlite3SrcListDelete(db, pTblName); |
| 75614 | 75839 | sqlite3DbFree(db, zName); |
| | @@ -75983,11 +76208,11 @@ |
| 75983 | 76208 | for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){ |
| 75984 | 76209 | sqlite3DbFree(db, pItem->zDatabase); |
| 75985 | 76210 | sqlite3DbFree(db, pItem->zName); |
| 75986 | 76211 | sqlite3DbFree(db, pItem->zAlias); |
| 75987 | 76212 | sqlite3DbFree(db, pItem->zIndex); |
| 75988 | | - sqlite3DeleteTable(pItem->pTab); |
| 76213 | + sqlite3DeleteTable(db, pItem->pTab); |
| 75989 | 76214 | sqlite3SelectDelete(db, pItem->pSelect); |
| 75990 | 76215 | sqlite3ExprDelete(db, pItem->pOn); |
| 75991 | 76216 | sqlite3IdListDelete(db, pItem->pUsing); |
| 75992 | 76217 | } |
| 75993 | 76218 | sqlite3DbFree(db, pList); |
| | @@ -76914,12 +77139,11 @@ |
| 76914 | 77139 | } |
| 76915 | 77140 | sqlite3HashClear(&temp2); |
| 76916 | 77141 | sqlite3HashInit(&pSchema->tblHash); |
| 76917 | 77142 | for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){ |
| 76918 | 77143 | Table *pTab = sqliteHashData(pElem); |
| 76919 | | - assert( pTab->dbMem==0 ); |
| 76920 | | - sqlite3DeleteTable(pTab); |
| 77144 | + sqlite3DeleteTable(0, pTab); |
| 76921 | 77145 | } |
| 76922 | 77146 | sqlite3HashClear(&temp1); |
| 76923 | 77147 | sqlite3HashClear(&pSchema->fkeyHash); |
| 76924 | 77148 | pSchema->pSeqTab = 0; |
| 76925 | 77149 | pSchema->flags &= ~DB_SchemaLoaded; |
| | @@ -76932,11 +77156,11 @@ |
| 76932 | 77156 | SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){ |
| 76933 | 77157 | Schema * p; |
| 76934 | 77158 | if( pBt ){ |
| 76935 | 77159 | p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaFree); |
| 76936 | 77160 | }else{ |
| 76937 | | - p = (Schema *)sqlite3MallocZero(sizeof(Schema)); |
| 77161 | + p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema)); |
| 76938 | 77162 | } |
| 76939 | 77163 | if( !p ){ |
| 76940 | 77164 | db->mallocFailed = 1; |
| 76941 | 77165 | }else if ( 0==p->file_format ){ |
| 76942 | 77166 | sqlite3HashInit(&p->tblHash); |
| | @@ -76973,11 +77197,11 @@ |
| 76973 | 77197 | SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){ |
| 76974 | 77198 | struct SrcList_item *pItem = pSrc->a; |
| 76975 | 77199 | Table *pTab; |
| 76976 | 77200 | assert( pItem && pSrc->nSrc==1 ); |
| 76977 | 77201 | pTab = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase); |
| 76978 | | - sqlite3DeleteTable(pItem->pTab); |
| 77202 | + sqlite3DeleteTable(pParse->db, pItem->pTab); |
| 76979 | 77203 | pItem->pTab = pTab; |
| 76980 | 77204 | if( pTab ){ |
| 76981 | 77205 | pTab->nRef++; |
| 76982 | 77206 | } |
| 76983 | 77207 | if( sqlite3IndexedByLookup(pParse, pItem) ){ |
| | @@ -78370,12 +78594,14 @@ |
| 78370 | 78594 | sqlite3_value **argv |
| 78371 | 78595 | ){ |
| 78372 | 78596 | const char *zOptName; |
| 78373 | 78597 | assert( argc==1 ); |
| 78374 | 78598 | UNUSED_PARAMETER(argc); |
| 78375 | | - /* IMP: R-xxxx This function is an SQL wrapper around the |
| 78376 | | - ** sqlite3_compileoption_used() C interface. */ |
| 78599 | + /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL |
| 78600 | + ** function is a wrapper around the sqlite3_compileoption_used() C/C++ |
| 78601 | + ** function. |
| 78602 | + */ |
| 78377 | 78603 | if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){ |
| 78378 | 78604 | sqlite3_result_int(context, sqlite3_compileoption_used(zOptName)); |
| 78379 | 78605 | } |
| 78380 | 78606 | } |
| 78381 | 78607 | #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ |
| | @@ -78392,12 +78618,13 @@ |
| 78392 | 78618 | sqlite3_value **argv |
| 78393 | 78619 | ){ |
| 78394 | 78620 | int n; |
| 78395 | 78621 | assert( argc==1 ); |
| 78396 | 78622 | UNUSED_PARAMETER(argc); |
| 78397 | | - /* IMP: R-xxxx This function is an SQL wrapper around the |
| 78398 | | - ** sqlite3_compileoption_get() C interface. */ |
| 78623 | + /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function |
| 78624 | + ** is a wrapper around the sqlite3_compileoption_get() C/C++ function. |
| 78625 | + */ |
| 78399 | 78626 | n = sqlite3_value_int(argv[0]); |
| 78400 | 78627 | sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC); |
| 78401 | 78628 | } |
| 78402 | 78629 | #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ |
| 78403 | 78630 | |
| | @@ -78592,18 +78819,18 @@ |
| 78592 | 78819 | nOut += nRep - nPattern; |
| 78593 | 78820 | testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] ); |
| 78594 | 78821 | testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] ); |
| 78595 | 78822 | if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 78596 | 78823 | sqlite3_result_error_toobig(context); |
| 78597 | | - sqlite3DbFree(db, zOut); |
| 78824 | + sqlite3_free(zOut); |
| 78598 | 78825 | return; |
| 78599 | 78826 | } |
| 78600 | 78827 | zOld = zOut; |
| 78601 | 78828 | zOut = sqlite3_realloc(zOut, (int)nOut); |
| 78602 | 78829 | if( zOut==0 ){ |
| 78603 | 78830 | sqlite3_result_error_nomem(context); |
| 78604 | | - sqlite3DbFree(db, zOld); |
| 78831 | + sqlite3_free(zOld); |
| 78605 | 78832 | return; |
| 78606 | 78833 | } |
| 78607 | 78834 | memcpy(&zOut[j], zRep, nRep); |
| 78608 | 78835 | j += nRep; |
| 78609 | 78836 | i += nPattern-1; |
| | @@ -78960,11 +79187,11 @@ |
| 78960 | 79187 | pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum)); |
| 78961 | 79188 | |
| 78962 | 79189 | if( pAccum ){ |
| 78963 | 79190 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 78964 | 79191 | int firstTerm = pAccum->useMalloc==0; |
| 78965 | | - pAccum->useMalloc = 1; |
| 79192 | + pAccum->useMalloc = 2; |
| 78966 | 79193 | pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH]; |
| 78967 | 79194 | if( !firstTerm ){ |
| 78968 | 79195 | if( argc==2 ){ |
| 78969 | 79196 | zSep = (char*)sqlite3_value_text(argv[1]); |
| 78970 | 79197 | nSep = sqlite3_value_bytes(argv[1]); |
| | @@ -79677,11 +79904,12 @@ |
| 79677 | 79904 | /* Set the collation sequence and affinity of the LHS of each TK_EQ |
| 79678 | 79905 | ** expression to the parent key column defaults. */ |
| 79679 | 79906 | if( pIdx ){ |
| 79680 | 79907 | Column *pCol; |
| 79681 | 79908 | iCol = pIdx->aiColumn[i]; |
| 79682 | | - pCol = &pIdx->pTable->aCol[iCol]; |
| 79909 | + pCol = &pTab->aCol[iCol]; |
| 79910 | + if( pTab->iPKey==iCol ) iCol = -1; |
| 79683 | 79911 | pLeft->iTable = regData+iCol+1; |
| 79684 | 79912 | pLeft->affinity = pCol->affinity; |
| 79685 | 79913 | pLeft->pColl = sqlite3LocateCollSeq(pParse, pCol->zColl); |
| 79686 | 79914 | }else{ |
| 79687 | 79915 | pLeft->iTable = regData; |
| | @@ -80238,15 +80466,11 @@ |
| 80238 | 80466 | 0, 0, 0, 0, 0, 0 |
| 80239 | 80467 | ); |
| 80240 | 80468 | pWhere = 0; |
| 80241 | 80469 | } |
| 80242 | 80470 | |
| 80243 | | - /* In the current implementation, pTab->dbMem==0 for all tables except |
| 80244 | | - ** for temporary tables used to describe subqueries. And temporary |
| 80245 | | - ** tables do not have foreign key constraints. Hence, pTab->dbMem |
| 80246 | | - ** should always be 0 there. |
| 80247 | | - */ |
| 80471 | + /* Disable lookaside memory allocation */ |
| 80248 | 80472 | enableLookaside = db->lookaside.bEnabled; |
| 80249 | 80473 | db->lookaside.bEnabled = 0; |
| 80250 | 80474 | |
| 80251 | 80475 | pTrigger = (Trigger *)sqlite3DbMallocZero(db, |
| 80252 | 80476 | sizeof(Trigger) + /* struct Trigger */ |
| | @@ -80332,41 +80556,43 @@ |
| 80332 | 80556 | /* |
| 80333 | 80557 | ** Free all memory associated with foreign key definitions attached to |
| 80334 | 80558 | ** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash |
| 80335 | 80559 | ** hash table. |
| 80336 | 80560 | */ |
| 80337 | | -SQLITE_PRIVATE void sqlite3FkDelete(Table *pTab){ |
| 80561 | +SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){ |
| 80338 | 80562 | FKey *pFKey; /* Iterator variable */ |
| 80339 | 80563 | FKey *pNext; /* Copy of pFKey->pNextFrom */ |
| 80340 | 80564 | |
| 80341 | 80565 | for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){ |
| 80342 | 80566 | |
| 80343 | 80567 | /* Remove the FK from the fkeyHash hash table. */ |
| 80344 | | - if( pFKey->pPrevTo ){ |
| 80345 | | - pFKey->pPrevTo->pNextTo = pFKey->pNextTo; |
| 80346 | | - }else{ |
| 80347 | | - void *data = (void *)pFKey->pNextTo; |
| 80348 | | - const char *z = (data ? pFKey->pNextTo->zTo : pFKey->zTo); |
| 80349 | | - sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, sqlite3Strlen30(z), data); |
| 80350 | | - } |
| 80351 | | - if( pFKey->pNextTo ){ |
| 80352 | | - pFKey->pNextTo->pPrevTo = pFKey->pPrevTo; |
| 80353 | | - } |
| 80354 | | - |
| 80355 | | - /* Delete any triggers created to implement actions for this FK. */ |
| 80356 | | -#ifndef SQLITE_OMIT_TRIGGER |
| 80357 | | - fkTriggerDelete(pTab->dbMem, pFKey->apTrigger[0]); |
| 80358 | | - fkTriggerDelete(pTab->dbMem, pFKey->apTrigger[1]); |
| 80359 | | -#endif |
| 80568 | + if( !db || db->pnBytesFreed==0 ){ |
| 80569 | + if( pFKey->pPrevTo ){ |
| 80570 | + pFKey->pPrevTo->pNextTo = pFKey->pNextTo; |
| 80571 | + }else{ |
| 80572 | + void *p = (void *)pFKey->pNextTo; |
| 80573 | + const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo); |
| 80574 | + sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, sqlite3Strlen30(z), p); |
| 80575 | + } |
| 80576 | + if( pFKey->pNextTo ){ |
| 80577 | + pFKey->pNextTo->pPrevTo = pFKey->pPrevTo; |
| 80578 | + } |
| 80579 | + } |
| 80360 | 80580 | |
| 80361 | 80581 | /* EV: R-30323-21917 Each foreign key constraint in SQLite is |
| 80362 | 80582 | ** classified as either immediate or deferred. |
| 80363 | 80583 | */ |
| 80364 | 80584 | assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 ); |
| 80585 | + |
| 80586 | + /* Delete any triggers created to implement actions for this FK. */ |
| 80587 | +#ifndef SQLITE_OMIT_TRIGGER |
| 80588 | + fkTriggerDelete(db, pFKey->apTrigger[0]); |
| 80589 | + fkTriggerDelete(db, pFKey->apTrigger[1]); |
| 80590 | +#endif |
| 80365 | 80591 | |
| 80366 | 80592 | pNext = pFKey->pNextFrom; |
| 80367 | | - sqlite3DbFree(pTab->dbMem, pFKey); |
| 80593 | + sqlite3DbFree(db, pFKey); |
| 80368 | 80594 | } |
| 80369 | 80595 | } |
| 80370 | 80596 | #endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */ |
| 80371 | 80597 | |
| 80372 | 80598 | /************** End of fkey.c ************************************************/ |
| | @@ -80437,11 +80663,11 @@ |
| 80437 | 80663 | ** up. |
| 80438 | 80664 | */ |
| 80439 | 80665 | int n; |
| 80440 | 80666 | Table *pTab = pIdx->pTable; |
| 80441 | 80667 | sqlite3 *db = sqlite3VdbeDb(v); |
| 80442 | | - pIdx->zColAff = (char *)sqlite3Malloc(pIdx->nColumn+2); |
| 80668 | + pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+2); |
| 80443 | 80669 | if( !pIdx->zColAff ){ |
| 80444 | 80670 | db->mallocFailed = 1; |
| 80445 | 80671 | return 0; |
| 80446 | 80672 | } |
| 80447 | 80673 | for(n=0; n<pIdx->nColumn; n++){ |
| | @@ -80479,11 +80705,11 @@ |
| 80479 | 80705 | if( !pTab->zColAff ){ |
| 80480 | 80706 | char *zColAff; |
| 80481 | 80707 | int i; |
| 80482 | 80708 | sqlite3 *db = sqlite3VdbeDb(v); |
| 80483 | 80709 | |
| 80484 | | - zColAff = (char *)sqlite3Malloc(pTab->nCol+1); |
| 80710 | + zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1); |
| 80485 | 80711 | if( !zColAff ){ |
| 80486 | 80712 | db->mallocFailed = 1; |
| 80487 | 80713 | return; |
| 80488 | 80714 | } |
| 80489 | 80715 | |
| | @@ -81590,10 +81816,11 @@ |
| 81590 | 81816 | sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, SQLITE_JUMPIFNULL); |
| 81591 | 81817 | onError = overrideError!=OE_Default ? overrideError : OE_Abort; |
| 81592 | 81818 | if( onError==OE_Ignore ){ |
| 81593 | 81819 | sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); |
| 81594 | 81820 | }else{ |
| 81821 | + if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */ |
| 81595 | 81822 | sqlite3HaltConstraint(pParse, onError, 0, 0); |
| 81596 | 81823 | } |
| 81597 | 81824 | sqlite3VdbeResolveLabel(v, allOk); |
| 81598 | 81825 | } |
| 81599 | 81826 | #endif /* !defined(SQLITE_OMIT_CHECK) */ |
| | @@ -83104,32 +83331,28 @@ |
| 83104 | 83331 | } |
| 83105 | 83332 | |
| 83106 | 83333 | handle = sqlite3OsDlOpen(pVfs, zFile); |
| 83107 | 83334 | if( handle==0 ){ |
| 83108 | 83335 | if( pzErrMsg ){ |
| 83109 | | - zErrmsg = sqlite3StackAllocZero(db, nMsg); |
| 83336 | + *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg); |
| 83110 | 83337 | if( zErrmsg ){ |
| 83111 | 83338 | sqlite3_snprintf(nMsg, zErrmsg, |
| 83112 | 83339 | "unable to open shared library [%s]", zFile); |
| 83113 | 83340 | sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); |
| 83114 | | - *pzErrMsg = sqlite3DbStrDup(0, zErrmsg); |
| 83115 | | - sqlite3StackFree(db, zErrmsg); |
| 83116 | 83341 | } |
| 83117 | 83342 | } |
| 83118 | 83343 | return SQLITE_ERROR; |
| 83119 | 83344 | } |
| 83120 | 83345 | xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*)) |
| 83121 | 83346 | sqlite3OsDlSym(pVfs, handle, zProc); |
| 83122 | 83347 | if( xInit==0 ){ |
| 83123 | 83348 | if( pzErrMsg ){ |
| 83124 | | - zErrmsg = sqlite3StackAllocZero(db, nMsg); |
| 83349 | + *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg); |
| 83125 | 83350 | if( zErrmsg ){ |
| 83126 | 83351 | sqlite3_snprintf(nMsg, zErrmsg, |
| 83127 | 83352 | "no entry point [%s] in shared library [%s]", zProc,zFile); |
| 83128 | 83353 | sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); |
| 83129 | | - *pzErrMsg = sqlite3DbStrDup(0, zErrmsg); |
| 83130 | | - sqlite3StackFree(db, zErrmsg); |
| 83131 | 83354 | } |
| 83132 | 83355 | sqlite3OsDlClose(pVfs, handle); |
| 83133 | 83356 | } |
| 83134 | 83357 | return SQLITE_ERROR; |
| 83135 | 83358 | }else if( xInit(db, &zErrmsg, &sqlite3Apis) ){ |
| | @@ -84104,11 +84327,11 @@ |
| 84104 | 84327 | ){ |
| 84105 | 84328 | invalidateTempStorage(pParse); |
| 84106 | 84329 | } |
| 84107 | 84330 | sqlite3_free(sqlite3_temp_directory); |
| 84108 | 84331 | if( zRight[0] ){ |
| 84109 | | - sqlite3_temp_directory = sqlite3DbStrDup(0, zRight); |
| 84332 | + sqlite3_temp_directory = sqlite3_mprintf("%s", zRight); |
| 84110 | 84333 | }else{ |
| 84111 | 84334 | sqlite3_temp_directory = 0; |
| 84112 | 84335 | } |
| 84113 | 84336 | #endif /* SQLITE_OMIT_WSD */ |
| 84114 | 84337 | } |
| | @@ -85525,11 +85748,10 @@ |
| 85525 | 85748 | |
| 85526 | 85749 | /* Delete any TriggerPrg structures allocated while parsing this statement. */ |
| 85527 | 85750 | while( pParse->pTriggerPrg ){ |
| 85528 | 85751 | TriggerPrg *pT = pParse->pTriggerPrg; |
| 85529 | 85752 | pParse->pTriggerPrg = pT->pNext; |
| 85530 | | - sqlite3VdbeProgramDelete(db, pT->pProgram, 0); |
| 85531 | 85753 | sqlite3DbFree(db, pT); |
| 85532 | 85754 | } |
| 85533 | 85755 | |
| 85534 | 85756 | end_prepare: |
| 85535 | 85757 | |
| | @@ -87016,20 +87238,19 @@ |
| 87016 | 87238 | pTab = sqlite3DbMallocZero(db, sizeof(Table) ); |
| 87017 | 87239 | if( pTab==0 ){ |
| 87018 | 87240 | return 0; |
| 87019 | 87241 | } |
| 87020 | 87242 | /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside |
| 87021 | | - ** is disabled, so we might as well hard-code pTab->dbMem to NULL. */ |
| 87243 | + ** is disabled */ |
| 87022 | 87244 | assert( db->lookaside.bEnabled==0 ); |
| 87023 | | - pTab->dbMem = 0; |
| 87024 | 87245 | pTab->nRef = 1; |
| 87025 | 87246 | pTab->zName = 0; |
| 87026 | 87247 | selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol); |
| 87027 | 87248 | selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSelect); |
| 87028 | 87249 | pTab->iPKey = -1; |
| 87029 | 87250 | if( db->mallocFailed ){ |
| 87030 | | - sqlite3DeleteTable(pTab); |
| 87251 | + sqlite3DeleteTable(db, pTab); |
| 87031 | 87252 | return 0; |
| 87032 | 87253 | } |
| 87033 | 87254 | return pTab; |
| 87034 | 87255 | } |
| 87035 | 87256 | |
| | @@ -88814,11 +89035,10 @@ |
| 88814 | 89035 | assert( pSel!=0 ); |
| 88815 | 89036 | assert( pFrom->pTab==0 ); |
| 88816 | 89037 | sqlite3WalkSelect(pWalker, pSel); |
| 88817 | 89038 | pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table)); |
| 88818 | 89039 | if( pTab==0 ) return WRC_Abort; |
| 88819 | | - pTab->dbMem = db->lookaside.bEnabled ? db : 0; |
| 88820 | 89040 | pTab->nRef = 1; |
| 88821 | 89041 | pTab->zName = sqlite3MPrintf(db, "sqlite_subquery_%p_", (void*)pTab); |
| 88822 | 89042 | while( pSel->pPrior ){ pSel = pSel->pPrior; } |
| 88823 | 89043 | selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol); |
| 88824 | 89044 | pTab->iPKey = -1; |
| | @@ -91028,10 +91248,11 @@ |
| 91028 | 91248 | SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */ |
| 91029 | 91249 | Parse *pSubParse; /* Parse context for sub-vdbe */ |
| 91030 | 91250 | int iEndTrigger = 0; /* Label to jump to if WHEN is false */ |
| 91031 | 91251 | |
| 91032 | 91252 | assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) ); |
| 91253 | + assert( pTop->pVdbe ); |
| 91033 | 91254 | |
| 91034 | 91255 | /* Allocate the TriggerPrg and SubProgram objects. To ensure that they |
| 91035 | 91256 | ** are freed if an error occurs, link them into the Parse.pTriggerPrg |
| 91036 | 91257 | ** list of the top-level Parse object sooner rather than later. */ |
| 91037 | 91258 | pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg)); |
| | @@ -91038,11 +91259,11 @@ |
| 91038 | 91259 | if( !pPrg ) return 0; |
| 91039 | 91260 | pPrg->pNext = pTop->pTriggerPrg; |
| 91040 | 91261 | pTop->pTriggerPrg = pPrg; |
| 91041 | 91262 | pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram)); |
| 91042 | 91263 | if( !pProgram ) return 0; |
| 91043 | | - pProgram->nRef = 1; |
| 91264 | + sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram); |
| 91044 | 91265 | pPrg->pTrigger = pTrigger; |
| 91045 | 91266 | pPrg->orconf = orconf; |
| 91046 | 91267 | pPrg->aColmask[0] = 0xffffffff; |
| 91047 | 91268 | pPrg->aColmask[1] = 0xffffffff; |
| 91048 | 91269 | |
| | @@ -91172,22 +91393,23 @@ |
| 91172 | 91393 | assert( pPrg || pParse->nErr || pParse->db->mallocFailed ); |
| 91173 | 91394 | |
| 91174 | 91395 | /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program |
| 91175 | 91396 | ** is a pointer to the sub-vdbe containing the trigger program. */ |
| 91176 | 91397 | if( pPrg ){ |
| 91398 | + int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers)); |
| 91399 | + |
| 91177 | 91400 | sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem); |
| 91178 | | - pPrg->pProgram->nRef++; |
| 91179 | 91401 | sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM); |
| 91180 | 91402 | VdbeComment( |
| 91181 | 91403 | (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf))); |
| 91182 | 91404 | |
| 91183 | 91405 | /* Set the P5 operand of the OP_Program instruction to non-zero if |
| 91184 | 91406 | ** recursive invocation of this trigger program is disallowed. Recursive |
| 91185 | 91407 | ** invocation is disallowed if (a) the sub-program is really a trigger, |
| 91186 | 91408 | ** not a foreign key action, and (b) the flag to enable recursive triggers |
| 91187 | 91409 | ** is clear. */ |
| 91188 | | - sqlite3VdbeChangeP5(v, (u8)(p->zName && !(pParse->db->flags&SQLITE_RecTriggers))); |
| 91410 | + sqlite3VdbeChangeP5(v, (u8)bRecursive); |
| 91189 | 91411 | } |
| 91190 | 91412 | } |
| 91191 | 91413 | |
| 91192 | 91414 | /* |
| 91193 | 91415 | ** This is called to code the required FOR EACH ROW triggers for an operation |
| | @@ -91332,11 +91554,11 @@ |
| 91332 | 91554 | ** |
| 91333 | 91555 | ** May you do good and not evil. |
| 91334 | 91556 | ** May you find forgiveness for yourself and forgive others. |
| 91335 | 91557 | ** May you share freely, never taking more than you give. |
| 91336 | 91558 | ** |
| 91337 | | -sqlite************************************************************************* |
| 91559 | +************************************************************************* |
| 91338 | 91560 | ** This file contains C code routines that are called by the parser |
| 91339 | 91561 | ** to handle UPDATE statements. |
| 91340 | 91562 | */ |
| 91341 | 91563 | |
| 91342 | 91564 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| | @@ -92551,18 +92773,18 @@ |
| 92551 | 92773 | ** connection db is decremented immediately (which may lead to the |
| 92552 | 92774 | ** structure being xDisconnected and free). Any other VTable structures |
| 92553 | 92775 | ** in the list are moved to the sqlite3.pDisconnect list of the associated |
| 92554 | 92776 | ** database connection. |
| 92555 | 92777 | */ |
| 92556 | | -SQLITE_PRIVATE void sqlite3VtabClear(Table *p){ |
| 92557 | | - vtabDisconnectAll(0, p); |
| 92778 | +SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){ |
| 92779 | + if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p); |
| 92558 | 92780 | if( p->azModuleArg ){ |
| 92559 | 92781 | int i; |
| 92560 | 92782 | for(i=0; i<p->nModuleArg; i++){ |
| 92561 | | - sqlite3DbFree(p->dbMem, p->azModuleArg[i]); |
| 92783 | + sqlite3DbFree(db, p->azModuleArg[i]); |
| 92562 | 92784 | } |
| 92563 | | - sqlite3DbFree(p->dbMem, p->azModuleArg); |
| 92785 | + sqlite3DbFree(db, p->azModuleArg); |
| 92564 | 92786 | } |
| 92565 | 92787 | } |
| 92566 | 92788 | |
| 92567 | 92789 | /* |
| 92568 | 92790 | ** Add a new module argument to pTable->azModuleArg[]. |
| | @@ -92723,11 +92945,10 @@ |
| 92723 | 92945 | if( pOld ){ |
| 92724 | 92946 | db->mallocFailed = 1; |
| 92725 | 92947 | assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */ |
| 92726 | 92948 | return; |
| 92727 | 92949 | } |
| 92728 | | - pSchema->db = pParse->db; |
| 92729 | 92950 | pParse->pNewTable = 0; |
| 92730 | 92951 | } |
| 92731 | 92952 | } |
| 92732 | 92953 | |
| 92733 | 92954 | /* |
| | @@ -92797,11 +93018,11 @@ |
| 92797 | 93018 | if( SQLITE_OK!=rc ){ |
| 92798 | 93019 | if( zErr==0 ){ |
| 92799 | 93020 | *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName); |
| 92800 | 93021 | }else { |
| 92801 | 93022 | *pzErr = sqlite3MPrintf(db, "%s", zErr); |
| 92802 | | - sqlite3DbFree(db, zErr); |
| 93023 | + sqlite3_free(zErr); |
| 92803 | 93024 | } |
| 92804 | 93025 | sqlite3DbFree(db, pVTable); |
| 92805 | 93026 | }else if( ALWAYS(pVTable->pVtab) ){ |
| 92806 | 93027 | /* Justification of ALWAYS(): A correct vtab constructor must allocate |
| 92807 | 93028 | ** the sqlite3_vtab object if successful. */ |
| | @@ -93004,19 +93225,19 @@ |
| 93004 | 93225 | pParse->pNewTable->aCol = 0; |
| 93005 | 93226 | } |
| 93006 | 93227 | db->pVTab = 0; |
| 93007 | 93228 | }else{ |
| 93008 | 93229 | sqlite3Error(db, SQLITE_ERROR, zErr); |
| 93009 | | - sqlite3DbFree(db, zErr); |
| 93230 | + sqlite3_free(zErr); |
| 93010 | 93231 | rc = SQLITE_ERROR; |
| 93011 | 93232 | } |
| 93012 | 93233 | pParse->declareVtab = 0; |
| 93013 | 93234 | |
| 93014 | 93235 | if( pParse->pVdbe ){ |
| 93015 | 93236 | sqlite3VdbeFinalize(pParse->pVdbe); |
| 93016 | 93237 | } |
| 93017 | | - sqlite3DeleteTable(pParse->pNewTable); |
| 93238 | + sqlite3DeleteTable(db, pParse->pNewTable); |
| 93018 | 93239 | sqlite3StackFree(db, pParse); |
| 93019 | 93240 | } |
| 93020 | 93241 | |
| 93021 | 93242 | assert( (rc&0xff)==rc ); |
| 93022 | 93243 | rc = sqlite3ApiExit(db, rc); |
| | @@ -93099,12 +93320,12 @@ |
| 93099 | 93320 | int (*x)(sqlite3_vtab *); |
| 93100 | 93321 | sqlite3_vtab *pVtab = aVTrans[i]->pVtab; |
| 93101 | 93322 | if( pVtab && (x = pVtab->pModule->xSync)!=0 ){ |
| 93102 | 93323 | rc = x(pVtab); |
| 93103 | 93324 | sqlite3DbFree(db, *pzErrmsg); |
| 93104 | | - *pzErrmsg = pVtab->zErrMsg; |
| 93105 | | - pVtab->zErrMsg = 0; |
| 93325 | + *pzErrmsg = sqlite3DbStrDup(db, pVtab->zErrMsg); |
| 93326 | + sqlite3_free(pVtab->zErrMsg); |
| 93106 | 93327 | } |
| 93107 | 93328 | } |
| 93108 | 93329 | db->aVTrans = aVTrans; |
| 93109 | 93330 | return rc; |
| 93110 | 93331 | } |
| | @@ -93600,10 +93821,11 @@ |
| 93600 | 93821 | ** the pWC->a[] array. |
| 93601 | 93822 | */ |
| 93602 | 93823 | static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){ |
| 93603 | 93824 | WhereTerm *pTerm; |
| 93604 | 93825 | int idx; |
| 93826 | + testcase( wtFlags & TERM_VIRTUAL ); /* EV: R-00211-15100 */ |
| 93605 | 93827 | if( pWC->nTerm>=pWC->nSlot ){ |
| 93606 | 93828 | WhereTerm *pOld = pWC->a; |
| 93607 | 93829 | sqlite3 *db = pWC->pParse->db; |
| 93608 | 93830 | pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 ); |
| 93609 | 93831 | if( pWC->a==0 ){ |
| | @@ -93745,10 +93967,17 @@ |
| 93745 | 93967 | |
| 93746 | 93968 | /* |
| 93747 | 93969 | ** Return TRUE if the given operator is one of the operators that is |
| 93748 | 93970 | ** allowed for an indexable WHERE clause term. The allowed operators are |
| 93749 | 93971 | ** "=", "<", ">", "<=", ">=", and "IN". |
| 93972 | +** |
| 93973 | +** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be |
| 93974 | +** of one of the following forms: column = expression column > expression |
| 93975 | +** column >= expression column < expression column <= expression |
| 93976 | +** expression = column expression > column expression >= column |
| 93977 | +** expression < column expression <= column column IN |
| 93978 | +** (expression-list) column IN (subquery) column IS NULL |
| 93750 | 93979 | */ |
| 93751 | 93980 | static int allowedOp(int op){ |
| 93752 | 93981 | assert( TK_GT>TK_EQ && TK_GT<TK_GE ); |
| 93753 | 93982 | assert( TK_LT>TK_EQ && TK_LT<TK_GE ); |
| 93754 | 93983 | assert( TK_LE>TK_EQ && TK_LE<TK_GE ); |
| | @@ -93908,11 +94137,10 @@ |
| 93908 | 94137 | Expr *pRight, *pLeft; /* Right and left size of LIKE operator */ |
| 93909 | 94138 | ExprList *pList; /* List of operands to the LIKE operator */ |
| 93910 | 94139 | int c; /* One character in z[] */ |
| 93911 | 94140 | int cnt; /* Number of non-wildcard prefix characters */ |
| 93912 | 94141 | char wc[3]; /* Wildcard characters */ |
| 93913 | | - CollSeq *pColl; /* Collating sequence for LHS */ |
| 93914 | 94142 | sqlite3 *db = pParse->db; /* Database connection */ |
| 93915 | 94143 | sqlite3_value *pVal = 0; |
| 93916 | 94144 | int op; /* Opcode of pRight */ |
| 93917 | 94145 | |
| 93918 | 94146 | if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){ |
| | @@ -93927,23 +94155,10 @@ |
| 93927 | 94155 | /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must |
| 93928 | 94156 | ** be the name of an indexed column with TEXT affinity. */ |
| 93929 | 94157 | return 0; |
| 93930 | 94158 | } |
| 93931 | 94159 | assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */ |
| 93932 | | - pColl = sqlite3ExprCollSeq(pParse, pLeft); |
| 93933 | | - if( pColl==0 ) return 0; /* Happens when LHS has an undefined collation */ |
| 93934 | | - if( (pColl->type!=SQLITE_COLL_BINARY || *pnoCase) && |
| 93935 | | - (pColl->type!=SQLITE_COLL_NOCASE || !*pnoCase) ){ |
| 93936 | | - /* IMP: R-09003-32046 For the GLOB operator, the column must use the |
| 93937 | | - ** default BINARY collating sequence. |
| 93938 | | - ** IMP: R-41408-28306 For the LIKE operator, if case_sensitive_like mode |
| 93939 | | - ** is enabled then the column must use the default BINARY collating |
| 93940 | | - ** sequence, or if case_sensitive_like mode is disabled then the column |
| 93941 | | - ** must use the built-in NOCASE collating sequence. |
| 93942 | | - */ |
| 93943 | | - return 0; |
| 93944 | | - } |
| 93945 | 94160 | |
| 93946 | 94161 | pRight = pList->a[0].pExpr; |
| 93947 | 94162 | op = pRight->op; |
| 93948 | 94163 | if( op==TK_REGISTER ){ |
| 93949 | 94164 | op = pRight->op2; |
| | @@ -93962,13 +94177,13 @@ |
| 93962 | 94177 | if( z ){ |
| 93963 | 94178 | cnt = 0; |
| 93964 | 94179 | while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){ |
| 93965 | 94180 | cnt++; |
| 93966 | 94181 | } |
| 93967 | | - if( cnt!=0 && c!=0 && 255!=(u8)z[cnt-1] ){ |
| 94182 | + if( cnt!=0 && 255!=(u8)z[cnt-1] ){ |
| 93968 | 94183 | Expr *pPrefix; |
| 93969 | | - *pisComplete = z[cnt]==wc[0] && z[cnt+1]==0; |
| 94184 | + *pisComplete = c==wc[0] && z[cnt+1]==0; |
| 93970 | 94185 | pPrefix = sqlite3Expr(db, TK_STRING, z); |
| 93971 | 94186 | if( pPrefix ) pPrefix->u.zToken[cnt] = 0; |
| 93972 | 94187 | *ppPrefix = pPrefix; |
| 93973 | 94188 | if( op==TK_VARIABLE ){ |
| 93974 | 94189 | Vdbe *v = pParse->pVdbe; |
| | @@ -94303,10 +94518,12 @@ |
| 94303 | 94518 | } |
| 94304 | 94519 | |
| 94305 | 94520 | /* At this point, okToChngToIN is true if original pTerm satisfies |
| 94306 | 94521 | ** case 1. In that case, construct a new virtual term that is |
| 94307 | 94522 | ** pTerm converted into an IN operator. |
| 94523 | + ** |
| 94524 | + ** EV: R-00211-15100 |
| 94308 | 94525 | */ |
| 94309 | 94526 | if( okToChngToIN ){ |
| 94310 | 94527 | Expr *pDup; /* A transient duplicate expression */ |
| 94311 | 94528 | ExprList *pList = 0; /* The RHS of the IN operator */ |
| 94312 | 94529 | Expr *pLeft = 0; /* The LHS of the IN operator */ |
| | @@ -94519,10 +94736,11 @@ |
| 94519 | 94736 | Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */ |
| 94520 | 94737 | Expr *pNewExpr1; |
| 94521 | 94738 | Expr *pNewExpr2; |
| 94522 | 94739 | int idxNew1; |
| 94523 | 94740 | int idxNew2; |
| 94741 | + CollSeq *pColl; /* Collating sequence to use */ |
| 94524 | 94742 | |
| 94525 | 94743 | pLeft = pExpr->x.pList->a[1].pExpr; |
| 94526 | 94744 | pStr2 = sqlite3ExprDup(db, pStr1, 0); |
| 94527 | 94745 | if( !db->mallocFailed ){ |
| 94528 | 94746 | u8 c, *pC; /* Last character before the first wildcard */ |
| | @@ -94533,21 +94751,27 @@ |
| 94533 | 94751 | ** wildcard. But if we increment '@', that will push it into the |
| 94534 | 94752 | ** alphabetic range where case conversions will mess up the |
| 94535 | 94753 | ** inequality. To avoid this, make sure to also run the full |
| 94536 | 94754 | ** LIKE on all candidate expressions by clearing the isComplete flag |
| 94537 | 94755 | */ |
| 94538 | | - if( c=='A'-1 ) isComplete = 0; |
| 94756 | + if( c=='A'-1 ) isComplete = 0; /* EV: R-64339-08207 */ |
| 94757 | + |
| 94539 | 94758 | |
| 94540 | 94759 | c = sqlite3UpperToLower[c]; |
| 94541 | 94760 | } |
| 94542 | 94761 | *pC = c + 1; |
| 94543 | 94762 | } |
| 94544 | | - pNewExpr1 = sqlite3PExpr(pParse, TK_GE, sqlite3ExprDup(db,pLeft,0),pStr1,0); |
| 94763 | + pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, noCase ? "NOCASE" : "BINARY",0); |
| 94764 | + pNewExpr1 = sqlite3PExpr(pParse, TK_GE, |
| 94765 | + sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl), |
| 94766 | + pStr1, 0); |
| 94545 | 94767 | idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC); |
| 94546 | 94768 | testcase( idxNew1==0 ); |
| 94547 | 94769 | exprAnalyze(pSrc, pWC, idxNew1); |
| 94548 | | - pNewExpr2 = sqlite3PExpr(pParse, TK_LT, sqlite3ExprDup(db,pLeft,0),pStr2,0); |
| 94770 | + pNewExpr2 = sqlite3PExpr(pParse, TK_LT, |
| 94771 | + sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl), |
| 94772 | + pStr2, 0); |
| 94549 | 94773 | idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC); |
| 94550 | 94774 | testcase( idxNew2==0 ); |
| 94551 | 94775 | exprAnalyze(pSrc, pWC, idxNew2); |
| 94552 | 94776 | pTerm = &pWC->a[idxTerm]; |
| 94553 | 94777 | if( isComplete ){ |
| | @@ -95295,11 +95519,11 @@ |
| 95295 | 95519 | sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); |
| 95296 | 95520 | }else{ |
| 95297 | 95521 | sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg); |
| 95298 | 95522 | } |
| 95299 | 95523 | } |
| 95300 | | - sqlite3DbFree(pParse->db, pVtab->zErrMsg); |
| 95524 | + sqlite3_free(pVtab->zErrMsg); |
| 95301 | 95525 | pVtab->zErrMsg = 0; |
| 95302 | 95526 | |
| 95303 | 95527 | for(i=0; i<p->nConstraint; i++){ |
| 95304 | 95528 | if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){ |
| 95305 | 95529 | sqlite3ErrorMsg(pParse, |
| | @@ -96159,10 +96383,13 @@ |
| 96159 | 96383 | ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' |
| 96160 | 96384 | ** |
| 96161 | 96385 | ** The t2.z='ok' is disabled in the in (2) because it originates |
| 96162 | 96386 | ** in the ON clause. The term is disabled in (3) because it is not part |
| 96163 | 96387 | ** of a LEFT OUTER JOIN. In (1), the term is not disabled. |
| 96388 | +** |
| 96389 | +** IMPLEMENTATION-OF: R-24597-58655 No tests are done for terms that are |
| 96390 | +** completely satisfied by indices. |
| 96164 | 96391 | ** |
| 96165 | 96392 | ** Disabling a term causes that term to not be tested in the inner loop |
| 96166 | 96393 | ** of the join. Disabling is an optimization. When terms are satisfied |
| 96167 | 96394 | ** by indices, we disable them to prevent redundant tests in the inner |
| 96168 | 96395 | ** loop. We would get the correct results if nothing were ever disabled, |
| | @@ -96371,10 +96598,11 @@ |
| 96371 | 96598 | pTerm = findTerm(pWC, iCur, k, notReady, pLevel->plan.wsFlags, pIdx); |
| 96372 | 96599 | if( NEVER(pTerm==0) ) break; |
| 96373 | 96600 | /* The following true for indices with redundant columns. |
| 96374 | 96601 | ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ |
| 96375 | 96602 | testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); |
| 96603 | + testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
| 96376 | 96604 | r1 = codeEqualityTerm(pParse, pTerm, pLevel, regBase+j); |
| 96377 | 96605 | if( r1!=regBase+j ){ |
| 96378 | 96606 | if( nReg==1 ){ |
| 96379 | 96607 | sqlite3ReleaseTempReg(pParse, regBase); |
| 96380 | 96608 | regBase = r1; |
| | @@ -96514,10 +96742,11 @@ |
| 96514 | 96742 | pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0); |
| 96515 | 96743 | assert( pTerm!=0 ); |
| 96516 | 96744 | assert( pTerm->pExpr!=0 ); |
| 96517 | 96745 | assert( pTerm->leftCursor==iCur ); |
| 96518 | 96746 | assert( omitTable==0 ); |
| 96747 | + testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
| 96519 | 96748 | iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, iReleaseReg); |
| 96520 | 96749 | addrNxt = pLevel->addrNxt; |
| 96521 | 96750 | sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); |
| 96522 | 96751 | sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); |
| 96523 | 96752 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| | @@ -96554,10 +96783,11 @@ |
| 96554 | 96783 | }; |
| 96555 | 96784 | assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */ |
| 96556 | 96785 | assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */ |
| 96557 | 96786 | assert( TK_GE==TK_GT+3 ); /* ... is correcct. */ |
| 96558 | 96787 | |
| 96788 | + testcase( pStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
| 96559 | 96789 | pX = pStart->pExpr; |
| 96560 | 96790 | assert( pX!=0 ); |
| 96561 | 96791 | assert( pStart->leftCursor==iCur ); |
| 96562 | 96792 | r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp); |
| 96563 | 96793 | sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1); |
| | @@ -96571,10 +96801,11 @@ |
| 96571 | 96801 | if( pEnd ){ |
| 96572 | 96802 | Expr *pX; |
| 96573 | 96803 | pX = pEnd->pExpr; |
| 96574 | 96804 | assert( pX!=0 ); |
| 96575 | 96805 | assert( pEnd->leftCursor==iCur ); |
| 96806 | + testcase( pEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
| 96576 | 96807 | memEndValue = ++pParse->nMem; |
| 96577 | 96808 | sqlite3ExprCode(pParse, pX->pRight, memEndValue); |
| 96578 | 96809 | if( pX->op==TK_LT || pX->op==TK_GT ){ |
| 96579 | 96810 | testOp = bRev ? OP_Le : OP_Ge; |
| 96580 | 96811 | }else{ |
| | @@ -96738,10 +96969,11 @@ |
| 96738 | 96969 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){ |
| 96739 | 96970 | zStartAff[nEq] = SQLITE_AFF_NONE; |
| 96740 | 96971 | } |
| 96741 | 96972 | } |
| 96742 | 96973 | nConstraint++; |
| 96974 | + testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
| 96743 | 96975 | }else if( isMinQuery ){ |
| 96744 | 96976 | sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 96745 | 96977 | nConstraint++; |
| 96746 | 96978 | startEq = 0; |
| 96747 | 96979 | start_constraints = 1; |
| | @@ -96777,10 +97009,11 @@ |
| 96777 | 97009 | zEndAff[nEq] = SQLITE_AFF_NONE; |
| 96778 | 97010 | } |
| 96779 | 97011 | } |
| 96780 | 97012 | codeApplyAffinity(pParse, regBase, nEq+1, zEndAff); |
| 96781 | 97013 | nConstraint++; |
| 97014 | + testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
| 96782 | 97015 | } |
| 96783 | 97016 | sqlite3DbFree(pParse->db, zStartAff); |
| 96784 | 97017 | sqlite3DbFree(pParse->db, zEndAff); |
| 96785 | 97018 | |
| 96786 | 97019 | /* Top of the loop body */ |
| | @@ -96983,15 +97216,19 @@ |
| 96983 | 97216 | } |
| 96984 | 97217 | notReady &= ~getMask(pWC->pMaskSet, iCur); |
| 96985 | 97218 | |
| 96986 | 97219 | /* Insert code to test every subexpression that can be completely |
| 96987 | 97220 | ** computed using the current set of tables. |
| 97221 | + ** |
| 97222 | + ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through |
| 97223 | + ** the use of indices become tests that are evaluated against each row of |
| 97224 | + ** the relevant input tables. |
| 96988 | 97225 | */ |
| 96989 | 97226 | k = 0; |
| 96990 | 97227 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 96991 | 97228 | Expr *pE; |
| 96992 | | - testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 97229 | + testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */ |
| 96993 | 97230 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 96994 | 97231 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 96995 | 97232 | if( (pTerm->prereqAll & notReady)!=0 ){ |
| 96996 | 97233 | testcase( pWInfo->untestedTerms==0 |
| 96997 | 97234 | && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ); |
| | @@ -97015,11 +97252,11 @@ |
| 97015 | 97252 | pLevel->addrFirst = sqlite3VdbeCurrentAddr(v); |
| 97016 | 97253 | sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin); |
| 97017 | 97254 | VdbeComment((v, "record LEFT JOIN hit")); |
| 97018 | 97255 | sqlite3ExprCacheClear(pParse); |
| 97019 | 97256 | for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){ |
| 97020 | | - testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 97257 | + testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */ |
| 97021 | 97258 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 97022 | 97259 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 97023 | 97260 | if( (pTerm->prereqAll & notReady)!=0 ){ |
| 97024 | 97261 | assert( pWInfo->untestedTerms ); |
| 97025 | 97262 | continue; |
| | @@ -97233,11 +97470,11 @@ |
| 97233 | 97470 | ** subexpression is separated by an AND operator. |
| 97234 | 97471 | */ |
| 97235 | 97472 | initMaskSet(pMaskSet); |
| 97236 | 97473 | whereClauseInit(pWC, pParse, pMaskSet); |
| 97237 | 97474 | sqlite3ExprCodeConstants(pParse, pWhere); |
| 97238 | | - whereSplit(pWC, pWhere, TK_AND); |
| 97475 | + whereSplit(pWC, pWhere, TK_AND); /* IMP: R-15842-53296 */ |
| 97239 | 97476 | |
| 97240 | 97477 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 97241 | 97478 | ** expression and either jump over all of the code or fall thru. |
| 97242 | 97479 | */ |
| 97243 | 97480 | if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){ |
| | @@ -97321,10 +97558,12 @@ |
| 97321 | 97558 | Index *pIdx; /* Index for FROM table at pTabItem */ |
| 97322 | 97559 | int j; /* For looping over FROM tables */ |
| 97323 | 97560 | int bestJ = -1; /* The value of j */ |
| 97324 | 97561 | Bitmask m; /* Bitmask value for j or bestJ */ |
| 97325 | 97562 | int isOptimal; /* Iterator for optimal/non-optimal search */ |
| 97563 | + int nUnconstrained; /* Number tables without INDEXED BY */ |
| 97564 | + Bitmask notIndexed; /* Mask of tables that cannot use an index */ |
| 97326 | 97565 | |
| 97327 | 97566 | memset(&bestPlan, 0, sizeof(bestPlan)); |
| 97328 | 97567 | bestPlan.rCost = SQLITE_BIG_DBL; |
| 97329 | 97568 | |
| 97330 | 97569 | /* Loop through the remaining entries in the FROM clause to find the |
| | @@ -97362,12 +97601,14 @@ |
| 97362 | 97601 | ** However, since the cost of a linear scan through table t2 is the same |
| 97363 | 97602 | ** as the cost of a linear scan through table t1, a simple greedy |
| 97364 | 97603 | ** algorithm may choose to use t2 for the outer loop, which is a much |
| 97365 | 97604 | ** costlier approach. |
| 97366 | 97605 | */ |
| 97606 | + nUnconstrained = 0; |
| 97607 | + notIndexed = 0; |
| 97367 | 97608 | for(isOptimal=(iFrom<nTabList-1); isOptimal>=0; isOptimal--){ |
| 97368 | | - Bitmask mask; /* Mask of tables not yet ready */ |
| 97609 | + Bitmask mask; /* Mask of tables not yet ready */ |
| 97369 | 97610 | for(j=iFrom, pTabItem=&pTabList->a[j]; j<nTabList; j++, pTabItem++){ |
| 97370 | 97611 | int doNotReorder; /* True if this table should not be reordered */ |
| 97371 | 97612 | WhereCost sCost; /* Cost information from best[Virtual]Index() */ |
| 97372 | 97613 | ExprList *pOrderBy; /* ORDER BY clause for index to optimize */ |
| 97373 | 97614 | |
| | @@ -97378,10 +97619,11 @@ |
| 97378 | 97619 | if( j==iFrom ) iFrom++; |
| 97379 | 97620 | continue; |
| 97380 | 97621 | } |
| 97381 | 97622 | mask = (isOptimal ? m : notReady); |
| 97382 | 97623 | pOrderBy = ((i==0 && ppOrderBy )?*ppOrderBy:0); |
| 97624 | + if( pTabItem->pIndex==0 ) nUnconstrained++; |
| 97383 | 97625 | |
| 97384 | 97626 | assert( pTabItem->pTab ); |
| 97385 | 97627 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 97386 | 97628 | if( IsVirtual(pTabItem->pTab) ){ |
| 97387 | 97629 | sqlite3_index_info **pp = &pWInfo->a[j].pIdxInfo; |
| | @@ -97391,13 +97633,47 @@ |
| 97391 | 97633 | { |
| 97392 | 97634 | bestBtreeIndex(pParse, pWC, pTabItem, mask, pOrderBy, &sCost); |
| 97393 | 97635 | } |
| 97394 | 97636 | assert( isOptimal || (sCost.used¬Ready)==0 ); |
| 97395 | 97637 | |
| 97396 | | - if( (sCost.used¬Ready)==0 |
| 97397 | | - && (bestJ<0 || sCost.rCost<bestPlan.rCost |
| 97398 | | - || (sCost.rCost<=bestPlan.rCost && sCost.nRow<bestPlan.nRow)) |
| 97638 | + /* If an INDEXED BY clause is present, then the plan must use that |
| 97639 | + ** index if it uses any index at all */ |
| 97640 | + assert( pTabItem->pIndex==0 |
| 97641 | + || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 |
| 97642 | + || sCost.plan.u.pIdx==pTabItem->pIndex ); |
| 97643 | + |
| 97644 | + if( isOptimal && (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){ |
| 97645 | + notIndexed |= m; |
| 97646 | + } |
| 97647 | + |
| 97648 | + /* Conditions under which this table becomes the best so far: |
| 97649 | + ** |
| 97650 | + ** (1) The table must not depend on other tables that have not |
| 97651 | + ** yet run. |
| 97652 | + ** |
| 97653 | + ** (2) A full-table-scan plan cannot supercede another plan unless |
| 97654 | + ** it is an "optimal" plan as defined above. |
| 97655 | + ** |
| 97656 | + ** (3) All tables have an INDEXED BY clause or this table lacks an |
| 97657 | + ** INDEXED BY clause or this table uses the specific |
| 97658 | + ** index specified by its INDEXED BY clause. This rule ensures |
| 97659 | + ** that a best-so-far is always selected even if an impossible |
| 97660 | + ** combination of INDEXED BY clauses are given. The error |
| 97661 | + ** will be detected and relayed back to the application later. |
| 97662 | + ** The NEVER() comes about because rule (2) above prevents |
| 97663 | + ** An indexable full-table-scan from reaching rule (3). |
| 97664 | + ** |
| 97665 | + ** (4) The plan cost must be lower than prior plans or else the |
| 97666 | + ** cost must be the same and the number of rows must be lower. |
| 97667 | + */ |
| 97668 | + if( (sCost.used¬Ready)==0 /* (1) */ |
| 97669 | + && (bestJ<0 || (notIndexed&m)!=0 /* (2) */ |
| 97670 | + || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0) |
| 97671 | + && (nUnconstrained==0 || pTabItem->pIndex==0 /* (3) */ |
| 97672 | + || NEVER((sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0)) |
| 97673 | + && (bestJ<0 || sCost.rCost<bestPlan.rCost /* (4) */ |
| 97674 | + || (sCost.rCost<=bestPlan.rCost && sCost.nRow<bestPlan.nRow)) |
| 97399 | 97675 | ){ |
| 97400 | 97676 | WHERETRACE(("... best so far with cost=%g and nRow=%g\n", |
| 97401 | 97677 | sCost.rCost, sCost.nRow)); |
| 97402 | 97678 | bestPlan = sCost; |
| 97403 | 97679 | bestJ = j; |
| | @@ -100408,11 +100684,11 @@ |
| 100408 | 100684 | spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0); |
| 100409 | 100685 | } |
| 100410 | 100686 | break; |
| 100411 | 100687 | case 195: /* expr ::= expr COLLATE ids */ |
| 100412 | 100688 | { |
| 100413 | | - yygotominor.yy118.pExpr = sqlite3ExprSetColl(pParse, yymsp[-2].minor.yy118.pExpr, &yymsp[0].minor.yy0); |
| 100689 | + yygotominor.yy118.pExpr = sqlite3ExprSetCollByToken(pParse, yymsp[-2].minor.yy118.pExpr, &yymsp[0].minor.yy0); |
| 100414 | 100690 | yygotominor.yy118.zStart = yymsp[-2].minor.yy118.zStart; |
| 100415 | 100691 | yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n]; |
| 100416 | 100692 | } |
| 100417 | 100693 | break; |
| 100418 | 100694 | case 196: /* expr ::= CAST LP expr AS typetoken RP */ |
| | @@ -100664,11 +100940,11 @@ |
| 100664 | 100940 | case 248: /* idxlist ::= idxlist COMMA nm collate sortorder */ |
| 100665 | 100941 | { |
| 100666 | 100942 | Expr *p = 0; |
| 100667 | 100943 | if( yymsp[-1].minor.yy0.n>0 ){ |
| 100668 | 100944 | p = sqlite3Expr(pParse->db, TK_COLUMN, 0); |
| 100669 | | - sqlite3ExprSetColl(pParse, p, &yymsp[-1].minor.yy0); |
| 100945 | + sqlite3ExprSetCollByToken(pParse, p, &yymsp[-1].minor.yy0); |
| 100670 | 100946 | } |
| 100671 | 100947 | yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, p); |
| 100672 | 100948 | sqlite3ExprListSetName(pParse,yygotominor.yy322,&yymsp[-2].minor.yy0,1); |
| 100673 | 100949 | sqlite3ExprListCheckLength(pParse, yygotominor.yy322, "index"); |
| 100674 | 100950 | if( yygotominor.yy322 ) yygotominor.yy322->a[yygotominor.yy322->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy4; |
| | @@ -100677,11 +100953,11 @@ |
| 100677 | 100953 | case 249: /* idxlist ::= nm collate sortorder */ |
| 100678 | 100954 | { |
| 100679 | 100955 | Expr *p = 0; |
| 100680 | 100956 | if( yymsp[-1].minor.yy0.n>0 ){ |
| 100681 | 100957 | p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0); |
| 100682 | | - sqlite3ExprSetColl(pParse, p, &yymsp[-1].minor.yy0); |
| 100958 | + sqlite3ExprSetCollByToken(pParse, p, &yymsp[-1].minor.yy0); |
| 100683 | 100959 | } |
| 100684 | 100960 | yygotominor.yy322 = sqlite3ExprListAppend(pParse,0, p); |
| 100685 | 100961 | sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1); |
| 100686 | 100962 | sqlite3ExprListCheckLength(pParse, yygotominor.yy322, "index"); |
| 100687 | 100963 | if( yygotominor.yy322 ) yygotominor.yy322->a[yygotominor.yy322->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy4; |
| | @@ -101957,19 +102233,19 @@ |
| 101957 | 102233 | pParse->aTableLock = 0; |
| 101958 | 102234 | pParse->nTableLock = 0; |
| 101959 | 102235 | } |
| 101960 | 102236 | #endif |
| 101961 | 102237 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 101962 | | - sqlite3DbFree(db, pParse->apVtabLock); |
| 102238 | + sqlite3_free(pParse->apVtabLock); |
| 101963 | 102239 | #endif |
| 101964 | 102240 | |
| 101965 | 102241 | if( !IN_DECLARE_VTAB ){ |
| 101966 | 102242 | /* If the pParse->declareVtab flag is set, do not delete any table |
| 101967 | 102243 | ** structure built up in pParse->pNewTable. The calling code (see vtab.c) |
| 101968 | 102244 | ** will take responsibility for freeing the Table structure. |
| 101969 | 102245 | */ |
| 101970 | | - sqlite3DeleteTable(pParse->pNewTable); |
| 102246 | + sqlite3DeleteTable(db, pParse->pNewTable); |
| 101971 | 102247 | } |
| 101972 | 102248 | |
| 101973 | 102249 | sqlite3DeleteTrigger(db, pParse->pNewTrigger); |
| 101974 | 102250 | sqlite3DbFree(db, pParse->apVarExpr); |
| 101975 | 102251 | sqlite3DbFree(db, pParse->aAlias); |
| | @@ -101979,11 +102255,11 @@ |
| 101979 | 102255 | sqlite3DbFree(db, p); |
| 101980 | 102256 | } |
| 101981 | 102257 | while( pParse->pZombieTab ){ |
| 101982 | 102258 | Table *p = pParse->pZombieTab; |
| 101983 | 102259 | pParse->pZombieTab = p->pNextZombie; |
| 101984 | | - sqlite3DeleteTable(p); |
| 102260 | + sqlite3DeleteTable(db, p); |
| 101985 | 102261 | } |
| 101986 | 102262 | if( nErr>0 && pParse->rc==SQLITE_OK ){ |
| 101987 | 102263 | pParse->rc = SQLITE_ERROR; |
| 101988 | 102264 | } |
| 101989 | 102265 | return nErr; |
| | @@ -103671,20 +103947,11 @@ |
| 103671 | 103947 | |
| 103672 | 103948 | assert( sqlite3_mutex_held(db->mutex) ); |
| 103673 | 103949 | |
| 103674 | 103950 | for(i=0; i<db->nDb && rc==SQLITE_OK; i++){ |
| 103675 | 103951 | if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){ |
| 103676 | | - Btree *pBt = db->aDb[i].pBt; |
| 103677 | | - if( pBt ){ |
| 103678 | | - if( sqlite3BtreeIsInReadTrans(pBt) ){ |
| 103679 | | - rc = SQLITE_LOCKED; |
| 103680 | | - }else{ |
| 103681 | | - sqlite3BtreeEnter(pBt); |
| 103682 | | - rc = sqlite3PagerCheckpoint(sqlite3BtreePager(pBt)); |
| 103683 | | - sqlite3BtreeLeave(pBt); |
| 103684 | | - } |
| 103685 | | - } |
| 103952 | + rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt); |
| 103686 | 103953 | } |
| 103687 | 103954 | } |
| 103688 | 103955 | |
| 103689 | 103956 | return rc; |
| 103690 | 103957 | } |
| | @@ -105129,11 +105396,11 @@ |
| 105129 | 105396 | |
| 105130 | 105397 | sqlite3BeginBenignMalloc(); |
| 105131 | 105398 | assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) ); |
| 105132 | 105399 | assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn ); |
| 105133 | 105400 | if( (!aDyn && nArg==(int)ArraySize(aStatic)) |
| 105134 | | - || (aDyn && nArg==(int)(sqlite3DbMallocSize(db, aDyn)/sizeof(void*))) |
| 105401 | + || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*))) |
| 105135 | 105402 | ){ |
| 105136 | 105403 | /* The aArg[] array needs to grow. */ |
| 105137 | 105404 | void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2); |
| 105138 | 105405 | if( pNew ){ |
| 105139 | 105406 | memcpy(pNew, aArg, nArg*sizeof(void *)); |
| 105140 | 105407 | |