2014-09-02 23:59:40 +04:00
|
|
|
/*
|
|
|
|
** 2014 August 30
|
|
|
|
**
|
|
|
|
** The author disclaims copyright to this source code. In place of
|
|
|
|
** a legal notice, here is a blessing:
|
|
|
|
**
|
|
|
|
** May you do good and not evil.
|
|
|
|
** May you find forgiveness for yourself and forgive others.
|
|
|
|
** May you share freely, never taking more than you give.
|
|
|
|
**
|
|
|
|
*************************************************************************
|
2015-02-19 21:06:40 +03:00
|
|
|
**
|
|
|
|
**
|
|
|
|
** OVERVIEW
|
|
|
|
**
|
|
|
|
** The OTA extension requires that the OTA update be packaged as an
|
|
|
|
** SQLite database. The tables it expects to find are described in
|
|
|
|
** sqlite3ota.h. Essentially, for each table xyz in the target database
|
|
|
|
** that the user wishes to write to, a corresponding data_xyz table is
|
|
|
|
** created in the OTA database and populated with one row for each row to
|
|
|
|
** update, insert or delete from the target table.
|
|
|
|
**
|
|
|
|
** The update proceeds in three stages:
|
|
|
|
**
|
|
|
|
** 1) The database is updated. The modified database pages are written
|
|
|
|
** to a *-oal file. A *-oal file is just like a *-wal file, except
|
|
|
|
** that it is named "<database>-oal" instead of "<database>-wal".
|
|
|
|
** Because regular SQLite clients do not look for file named
|
|
|
|
** "<database>-oal", they go on using the original database in
|
|
|
|
** rollback mode while the *-oal file is being generated.
|
|
|
|
**
|
|
|
|
** During this stage OTA does not update the database by writing
|
|
|
|
** directly to the target tables. Instead it creates "imposter"
|
|
|
|
** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
|
|
|
|
** to update each b-tree individually. All updates required by each
|
|
|
|
** b-tree are completed before moving on to the next, and all
|
|
|
|
** updates are done in sorted key order.
|
|
|
|
**
|
|
|
|
** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
|
|
|
|
** location using a call to rename(2). Before doing this the OTA
|
|
|
|
** module takes an EXCLUSIVE lock on the database file, ensuring
|
|
|
|
** that there are no other active readers.
|
|
|
|
**
|
|
|
|
** Once the EXCLUSIVE lock is released, any other database readers
|
|
|
|
** detect the new *-wal file and read the database in wal mode. At
|
|
|
|
** this point they see the new version of the database - including
|
|
|
|
** the updates made as part of the OTA update.
|
|
|
|
**
|
|
|
|
** 3) The new *-wal file is checkpointed. This proceeds in the same way
|
|
|
|
** as a regular database checkpoint, except that a single frame is
|
|
|
|
** checkpointed each time sqlite3ota_step() is called. If the OTA
|
|
|
|
** handle is closed before the entire *-wal file is checkpointed,
|
|
|
|
** the checkpoint progress is saved in the OTA database and the
|
|
|
|
** checkpoint can be resumed by another OTA client at some point in
|
|
|
|
** the future.
|
|
|
|
**
|
|
|
|
** POTENTIAL PROBLEMS
|
|
|
|
**
|
|
|
|
** The rename() call might not be portable. And OTA is not currently
|
|
|
|
** syncing the directory after renaming the file.
|
|
|
|
**
|
|
|
|
** When state is saved, any commit to the *-oal file and the commit to
|
|
|
|
** the OTA update database are not atomic. So if the power fails at the
|
|
|
|
** wrong moment they might get out of sync. As the main database will be
|
|
|
|
** committed before the OTA update database this will likely either just
|
|
|
|
** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
|
|
|
|
** constraint violations).
|
|
|
|
**
|
|
|
|
** If some client does modify the target database mid OTA update, or some
|
|
|
|
** other error occurs, the OTA extension will keep throwing errors. It's
|
|
|
|
** not really clear how to get out of this state. The system could just
|
|
|
|
** by delete the OTA update database and *-oal file and have the device
|
|
|
|
** download the update again and start over.
|
|
|
|
**
|
|
|
|
** At present, for an UPDATE, both the new.* and old.* records are
|
|
|
|
** collected in the ota_xyz table. And for both UPDATEs and DELETEs all
|
|
|
|
** fields are collected. This means we're probably writing a lot more
|
|
|
|
** data to disk when saving the state of an ongoing update to the OTA
|
|
|
|
** update database than is strictly necessary.
|
|
|
|
**
|
2014-09-02 23:59:40 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2014-09-03 23:30:32 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2014-09-02 23:59:40 +04:00
|
|
|
|
|
|
|
#include "sqlite3.h"
|
2014-11-22 12:09:50 +03:00
|
|
|
|
|
|
|
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_OTA)
|
2014-09-02 23:59:40 +04:00
|
|
|
#include "sqlite3ota.h"
|
|
|
|
|
2015-02-04 14:08:47 +03:00
|
|
|
/*
|
|
|
|
** Swap two objects of type TYPE.
|
|
|
|
*/
|
|
|
|
#if !defined(SQLITE_AMALGAMATION)
|
|
|
|
# define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
|
|
|
|
#endif
|
2014-09-02 23:59:40 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** The ota_state table is used to save the state of a partially applied
|
2014-10-20 20:24:23 +04:00
|
|
|
** update so that it can be resumed later. The table consists of integer
|
|
|
|
** keys mapped to values as follows:
|
2014-09-02 23:59:40 +04:00
|
|
|
**
|
2014-10-20 20:24:23 +04:00
|
|
|
** OTA_STATE_STAGE:
|
2015-02-19 22:59:35 +03:00
|
|
|
** May be set to integer values 1, 2, 4 or 5. As follows:
|
2014-10-20 20:24:23 +04:00
|
|
|
** 1: the *-ota file is currently under construction.
|
|
|
|
** 2: the *-ota file has been constructed, but not yet moved
|
|
|
|
** to the *-wal path.
|
2015-02-19 22:59:35 +03:00
|
|
|
** 4: the checkpoint is underway.
|
|
|
|
** 5: the ota update has been checkpointed.
|
2014-09-02 23:59:40 +04:00
|
|
|
**
|
2014-10-20 20:24:23 +04:00
|
|
|
** OTA_STATE_TBL:
|
|
|
|
** Only valid if STAGE==1. The target database name of the table
|
|
|
|
** currently being written.
|
2014-09-02 23:59:40 +04:00
|
|
|
**
|
2014-10-20 20:24:23 +04:00
|
|
|
** OTA_STATE_IDX:
|
|
|
|
** Only valid if STAGE==1. The target database name of the index
|
|
|
|
** currently being written, or NULL if the main table is currently being
|
|
|
|
** updated.
|
|
|
|
**
|
|
|
|
** OTA_STATE_ROW:
|
|
|
|
** Only valid if STAGE==1. Number of rows already processed for the current
|
|
|
|
** table/index.
|
|
|
|
**
|
|
|
|
** OTA_STATE_PROGRESS:
|
|
|
|
** Total number of sqlite3ota_step() calls made so far as part of this
|
|
|
|
** ota update.
|
|
|
|
**
|
|
|
|
** OTA_STATE_CKPT:
|
2015-02-19 22:59:35 +03:00
|
|
|
** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
|
|
|
|
** header created by recovering the *-wal file. This is used to detect
|
|
|
|
** cases when another client appends frames to the *-wal file in the
|
|
|
|
** middle of an incremental checkpoint (an incremental checkpoint cannot
|
|
|
|
** be continued if this happens).
|
2014-09-02 23:59:40 +04:00
|
|
|
**
|
2015-02-07 22:17:36 +03:00
|
|
|
** OTA_STATE_COOKIE:
|
|
|
|
** Valid if STAGE==1. The current change-counter cookie value in the
|
|
|
|
** target db file.
|
2015-02-21 23:08:25 +03:00
|
|
|
**
|
|
|
|
** OTA_STATE_OALSZ:
|
|
|
|
** Valid if STAGE==1. The size in bytes of the *-oal file.
|
2014-09-02 23:59:40 +04:00
|
|
|
*/
|
2014-10-20 20:24:23 +04:00
|
|
|
#define OTA_STATE_STAGE 1
|
|
|
|
#define OTA_STATE_TBL 2
|
|
|
|
#define OTA_STATE_IDX 3
|
|
|
|
#define OTA_STATE_ROW 4
|
|
|
|
#define OTA_STATE_PROGRESS 5
|
|
|
|
#define OTA_STATE_CKPT 6
|
2015-02-07 22:17:36 +03:00
|
|
|
#define OTA_STATE_COOKIE 7
|
2015-02-21 23:08:25 +03:00
|
|
|
#define OTA_STATE_OALSZ 8
|
2014-10-20 20:24:23 +04:00
|
|
|
|
|
|
|
#define OTA_STAGE_OAL 1
|
2015-02-18 20:40:05 +03:00
|
|
|
#define OTA_STAGE_MOVE 2
|
|
|
|
#define OTA_STAGE_CAPTURE 3
|
|
|
|
#define OTA_STAGE_CKPT 4
|
|
|
|
#define OTA_STAGE_DONE 5
|
2014-10-20 20:24:23 +04:00
|
|
|
|
|
|
|
|
2015-02-21 23:08:25 +03:00
|
|
|
#define OTA_CREATE_STATE "CREATE TABLE IF NOT EXISTS ota_state" \
|
2014-10-20 20:24:23 +04:00
|
|
|
"(k INTEGER PRIMARY KEY, v)"
|
2014-09-02 23:59:40 +04:00
|
|
|
|
2015-02-19 22:59:35 +03:00
|
|
|
typedef struct OtaFrame OtaFrame;
|
2014-09-05 23:31:15 +04:00
|
|
|
typedef struct OtaObjIter OtaObjIter;
|
2015-02-19 22:59:35 +03:00
|
|
|
typedef struct OtaState OtaState;
|
2015-02-09 23:07:35 +03:00
|
|
|
typedef struct ota_vfs ota_vfs;
|
|
|
|
typedef struct ota_file ota_file;
|
2014-09-02 23:59:40 +04:00
|
|
|
|
2015-02-14 21:58:22 +03:00
|
|
|
#if !defined(SQLITE_AMALGAMATION)
|
|
|
|
typedef unsigned int u32;
|
|
|
|
typedef unsigned char u8;
|
|
|
|
typedef sqlite3_int64 i64;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
** These values must match the values defined in wal.c for the equivalent
|
|
|
|
** locks. These are not magic numbers as they are part of the SQLite file
|
|
|
|
** format.
|
|
|
|
*/
|
|
|
|
#define WAL_LOCK_WRITE 0
|
|
|
|
#define WAL_LOCK_CKPT 1
|
|
|
|
#define WAL_LOCK_READ0 3
|
|
|
|
|
2014-09-02 23:59:40 +04:00
|
|
|
/*
|
2014-09-05 23:31:15 +04:00
|
|
|
** A structure to store values read from the ota_state table in memory.
|
2014-09-02 23:59:40 +04:00
|
|
|
*/
|
2014-09-05 23:31:15 +04:00
|
|
|
struct OtaState {
|
2014-10-20 20:24:23 +04:00
|
|
|
int eStage;
|
2014-09-05 23:31:15 +04:00
|
|
|
char *zTbl;
|
|
|
|
char *zIdx;
|
2015-02-14 21:58:22 +03:00
|
|
|
i64 iWalCksum;
|
2014-09-05 23:31:15 +04:00
|
|
|
int nRow;
|
2015-02-14 21:58:22 +03:00
|
|
|
i64 nProgress;
|
2015-02-16 14:48:34 +03:00
|
|
|
u32 iCookie;
|
2015-02-21 23:08:25 +03:00
|
|
|
i64 iOalSz;
|
2014-09-02 23:59:40 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2014-09-05 23:31:15 +04:00
|
|
|
** An iterator of this type is used to iterate through all objects in
|
|
|
|
** the target database that require updating. For each such table, the
|
|
|
|
** iterator visits, in order:
|
2014-09-02 23:59:40 +04:00
|
|
|
**
|
2014-09-05 23:31:15 +04:00
|
|
|
** * the table itself,
|
|
|
|
** * each index of the table (zero or more points to visit), and
|
2015-01-31 23:42:04 +03:00
|
|
|
** * a special "cleanup table" state.
|
2015-03-24 21:03:29 +03:00
|
|
|
**
|
|
|
|
** abIndexed:
|
|
|
|
** If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
|
|
|
|
** it points to an array of flags nTblCol elements in size. The flag is
|
|
|
|
** set for each column that is either a part of the PK or a part of an
|
|
|
|
** index. Or clear otherwise.
|
|
|
|
**
|
2014-09-02 23:59:40 +04:00
|
|
|
*/
|
2014-09-05 23:31:15 +04:00
|
|
|
struct OtaObjIter {
|
|
|
|
sqlite3_stmt *pTblIter; /* Iterate through tables */
|
|
|
|
sqlite3_stmt *pIdxIter; /* Index iterator */
|
|
|
|
int nTblCol; /* Size of azTblCol[] array */
|
2015-02-04 14:08:47 +03:00
|
|
|
char **azTblCol; /* Array of unquoted target column names */
|
|
|
|
char **azTblType; /* Array of target column types */
|
|
|
|
int *aiSrcOrder; /* src table col -> target table col */
|
2015-02-14 21:58:22 +03:00
|
|
|
u8 *abTblPk; /* Array of flags, set on target PK columns */
|
|
|
|
u8 *abNotNull; /* Array of flags, set on NOT NULL columns */
|
2015-03-24 21:03:29 +03:00
|
|
|
u8 *abIndexed; /* Array of flags, set on indexed & PK cols */
|
2015-02-04 14:08:47 +03:00
|
|
|
int eType; /* Table type - an OTA_PK_XXX value */
|
2014-09-05 23:31:15 +04:00
|
|
|
|
|
|
|
/* Output variables. zTbl==0 implies EOF. */
|
|
|
|
int bCleanup; /* True in "cleanup" state */
|
|
|
|
const char *zTbl; /* Name of target db table */
|
|
|
|
const char *zIdx; /* Name of target db index (or null) */
|
2015-02-04 19:32:47 +03:00
|
|
|
int iTnum; /* Root page of current object */
|
|
|
|
int iPkTnum; /* If eType==EXTERNAL, root of PK index */
|
2015-01-31 23:42:04 +03:00
|
|
|
int bUnique; /* Current index is unique */
|
2014-09-05 23:31:15 +04:00
|
|
|
|
|
|
|
/* Statements created by otaObjIterPrepareAll() */
|
|
|
|
int nCol; /* Number of columns in current object */
|
|
|
|
sqlite3_stmt *pSelect; /* Source data */
|
|
|
|
sqlite3_stmt *pInsert; /* Statement for INSERT operations */
|
2014-09-07 00:19:38 +04:00
|
|
|
sqlite3_stmt *pDelete; /* Statement for DELETE ops */
|
2015-02-21 23:08:25 +03:00
|
|
|
sqlite3_stmt *pTmpInsert; /* Insert into ota_tmp_$zTbl */
|
2014-09-08 21:50:35 +04:00
|
|
|
|
|
|
|
/* Last UPDATE used (for PK b-tree updates only), or NULL. */
|
|
|
|
char *zMask; /* Copy of update mask used with pUpdate */
|
|
|
|
sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */
|
2014-09-03 23:30:32 +04:00
|
|
|
};
|
|
|
|
|
2014-12-06 22:30:41 +03:00
|
|
|
/*
|
|
|
|
** Values for OtaObjIter.eType
|
2015-02-03 18:56:08 +03:00
|
|
|
**
|
2015-02-05 04:49:31 +03:00
|
|
|
** 0: Table does not exist (error)
|
2015-02-03 18:56:08 +03:00
|
|
|
** 1: Table has an implicit rowid.
|
|
|
|
** 2: Table has an explicit IPK column.
|
|
|
|
** 3: Table has an external PK index.
|
|
|
|
** 4: Table is WITHOUT ROWID.
|
|
|
|
** 5: Table is a virtual table.
|
2014-12-06 22:30:41 +03:00
|
|
|
*/
|
2015-02-05 04:49:31 +03:00
|
|
|
#define OTA_PK_NOTABLE 0
|
2015-02-03 18:56:08 +03:00
|
|
|
#define OTA_PK_NONE 1
|
|
|
|
#define OTA_PK_IPK 2
|
|
|
|
#define OTA_PK_EXTERNAL 3
|
|
|
|
#define OTA_PK_WITHOUT_ROWID 4
|
|
|
|
#define OTA_PK_VTAB 5
|
2014-12-06 22:30:41 +03:00
|
|
|
|
2015-02-09 23:07:35 +03:00
|
|
|
|
2015-02-19 22:59:35 +03:00
|
|
|
/*
|
|
|
|
** Within the OTA_STAGE_OAL stage, each call to sqlite3ota_step() performs
|
|
|
|
** one of the following operations.
|
|
|
|
*/
|
|
|
|
#define OTA_INSERT 1 /* Insert on a main table b-tree */
|
|
|
|
#define OTA_DELETE 2 /* Delete a row from a main table b-tree */
|
|
|
|
#define OTA_IDX_DELETE 3 /* Delete a row from an aux. index b-tree */
|
|
|
|
#define OTA_IDX_INSERT 4 /* Insert on an aux. index b-tree */
|
|
|
|
#define OTA_UPDATE 5 /* Update a row in a main table b-tree */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** A single step of an incremental checkpoint - frame iWalFrame of the wal
|
|
|
|
** file should be copied to page iDbPage of the database file.
|
|
|
|
*/
|
2015-02-14 21:58:22 +03:00
|
|
|
struct OtaFrame {
|
|
|
|
u32 iDbPage;
|
|
|
|
u32 iWalFrame;
|
|
|
|
};
|
|
|
|
|
2014-09-05 23:31:15 +04:00
|
|
|
/*
|
|
|
|
** OTA handle.
|
|
|
|
*/
|
2014-09-02 23:59:40 +04:00
|
|
|
struct sqlite3ota {
|
2014-10-20 20:24:23 +04:00
|
|
|
int eStage; /* Value of OTA_STATE_STAGE field */
|
2015-02-21 23:08:25 +03:00
|
|
|
sqlite3 *dbMain; /* target database handle */
|
|
|
|
sqlite3 *dbOta; /* ota database handle */
|
2014-09-03 23:30:32 +04:00
|
|
|
char *zTarget; /* Path to target db */
|
2014-10-20 20:24:23 +04:00
|
|
|
char *zOta; /* Path to ota db */
|
2014-09-02 23:59:40 +04:00
|
|
|
int rc; /* Value returned by last ota_step() call */
|
|
|
|
char *zErrmsg; /* Error message if rc!=SQLITE_OK */
|
2014-09-05 23:31:15 +04:00
|
|
|
int nStep; /* Rows processed for current object */
|
2014-09-18 18:48:38 +04:00
|
|
|
int nProgress; /* Rows processed for all objects */
|
2014-10-20 20:24:23 +04:00
|
|
|
OtaObjIter objiter; /* Iterator for skipping through tbl/idx */
|
2015-02-09 23:07:35 +03:00
|
|
|
const char *zVfsName; /* Name of automatically created ota vfs */
|
2015-02-14 21:58:22 +03:00
|
|
|
ota_file *pTargetFd; /* File handle open on target db */
|
2015-02-21 23:08:25 +03:00
|
|
|
i64 iOalSz;
|
2015-02-14 21:58:22 +03:00
|
|
|
|
|
|
|
/* The following state variables are used as part of the incremental
|
2015-02-20 17:36:16 +03:00
|
|
|
** checkpoint stage (eStage==OTA_STAGE_CKPT). See comments surrounding
|
|
|
|
** function otaSetupCheckpoint() for details. */
|
2015-02-14 21:58:22 +03:00
|
|
|
u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
|
|
|
|
u32 mLock;
|
|
|
|
int nFrame; /* Entries in aFrame[] array */
|
|
|
|
int nFrameAlloc; /* Allocated size of aFrame[] array */
|
|
|
|
OtaFrame *aFrame;
|
|
|
|
int pgsz;
|
|
|
|
u8 *aBuf;
|
|
|
|
i64 iWalCksum;
|
2014-09-02 23:59:40 +04:00
|
|
|
};
|
|
|
|
|
2015-02-19 22:59:35 +03:00
|
|
|
/*
|
|
|
|
** An ota VFS is implemented using an instance of this structure.
|
|
|
|
*/
|
2015-02-09 23:07:35 +03:00
|
|
|
struct ota_vfs {
|
2015-02-14 21:58:22 +03:00
|
|
|
sqlite3_vfs base; /* ota VFS shim methods */
|
|
|
|
sqlite3_vfs *pRealVfs; /* Underlying VFS */
|
|
|
|
sqlite3_mutex *mutex; /* Mutex to protect pMain */
|
|
|
|
ota_file *pMain; /* Linked list of main db files */
|
2015-02-09 23:07:35 +03:00
|
|
|
};
|
|
|
|
|
2015-02-19 22:59:35 +03:00
|
|
|
/*
|
|
|
|
** Each file opened by an ota VFS is represented by an instance of
|
|
|
|
** the following structure.
|
|
|
|
*/
|
2015-02-09 23:07:35 +03:00
|
|
|
struct ota_file {
|
|
|
|
sqlite3_file base; /* sqlite3_file methods */
|
|
|
|
sqlite3_file *pReal; /* Underlying file handle */
|
|
|
|
ota_vfs *pOtaVfs; /* Pointer to the ota_vfs object */
|
|
|
|
sqlite3ota *pOta; /* Pointer to ota object (ota target only) */
|
2015-02-11 19:25:27 +03:00
|
|
|
|
2015-02-10 23:00:38 +03:00
|
|
|
int openFlags; /* Flags this file was opened with */
|
2015-02-14 21:58:22 +03:00
|
|
|
u32 iCookie; /* Cookie value for main db files */
|
|
|
|
u8 iWriteVer; /* "write-version" value for main db files */
|
2015-02-09 23:07:35 +03:00
|
|
|
|
|
|
|
int nShm; /* Number of entries in apShm[] array */
|
|
|
|
char **apShm; /* Array of mmap'd *-shm regions */
|
|
|
|
char *zDel; /* Delete this when closing file */
|
2015-02-14 21:58:22 +03:00
|
|
|
|
|
|
|
const char *zWal; /* Wal filename for this main db file */
|
|
|
|
ota_file *pWalFd; /* Wal file descriptor for this main db */
|
|
|
|
ota_file *pMainNext; /* Next MAIN_DB file */
|
2014-09-02 23:59:40 +04:00
|
|
|
};
|
|
|
|
|
2015-02-09 23:07:35 +03:00
|
|
|
|
2014-09-05 23:31:15 +04:00
|
|
|
/*
|
|
|
|
** Prepare the SQL statement in buffer zSql against database handle db.
|
|
|
|
** If successful, set *ppStmt to point to the new statement and return
|
|
|
|
** SQLITE_OK.
|
|
|
|
**
|
|
|
|
** Otherwise, if an error does occur, set *ppStmt to NULL and return
|
|
|
|
** an SQLite error code. Additionally, set output variable *pzErrmsg to
|
|
|
|
** point to a buffer containing an error message. It is the responsibility
|
|
|
|
** of the caller to (eventually) free this buffer using sqlite3_free().
|
|
|
|
*/
|
2014-09-02 23:59:40 +04:00
|
|
|
static int prepareAndCollectError(
|
|
|
|
sqlite3 *db,
|
|
|
|
sqlite3_stmt **ppStmt,
|
2014-09-05 23:31:15 +04:00
|
|
|
char **pzErrmsg,
|
|
|
|
const char *zSql
|
2014-09-02 23:59:40 +04:00
|
|
|
){
|
|
|
|
int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
|
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
*pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
|
|
|
|
*ppStmt = 0;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-09-05 23:31:15 +04:00
|
|
|
/*
|
|
|
|
** Reset the SQL statement passed as the first argument. Return a copy
|
|
|
|
** of the value returned by sqlite3_reset().
|
|
|
|
**
|
|
|
|
** If an error has occurred, then set *pzErrmsg to point to a buffer
|
|
|
|
** containing an error message. It is the responsibility of the caller
|
|
|
|
** to eventually free this buffer using sqlite3_free().
|
|
|
|
*/
|
|
|
|
static int resetAndCollectError(sqlite3_stmt *pStmt, char **pzErrmsg){
|
|
|
|
int rc = sqlite3_reset(pStmt);
|
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
*pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt)));
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-09-02 23:59:40 +04:00
|
|
|
/*
|
|
|
|
** Unless it is NULL, argument zSql points to a buffer allocated using
|
|
|
|
** sqlite3_malloc containing an SQL statement. This function prepares the SQL
|
|
|
|
** statement against database db and frees the buffer. If statement
|
|
|
|
** compilation is successful, *ppStmt is set to point to the new statement
|
|
|
|
** handle and SQLITE_OK is returned.
|
|
|
|
**
|
|
|
|
** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
|
|
|
|
** returned. In this case, *pzErrmsg may also be set to point to an error
|
|
|
|
** message. It is the responsibility of the caller to free this error message
|
|
|
|
** buffer using sqlite3_free().
|
|
|
|
**
|
|
|
|
** If argument zSql is NULL, this function assumes that an OOM has occurred.
|
|
|
|
** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
|
|
|
|
*/
|
|
|
|
static int prepareFreeAndCollectError(
|
|
|
|
sqlite3 *db,
|
|
|
|
sqlite3_stmt **ppStmt,
|
2014-09-05 23:31:15 +04:00
|
|
|
char **pzErrmsg,
|
|
|
|
char *zSql
|
2014-09-02 23:59:40 +04:00
|
|
|
){
|
|
|
|
int rc;
|
|
|
|
assert( *pzErrmsg==0 );
|
|
|
|
if( zSql==0 ){
|
|
|
|
rc = SQLITE_NOMEM;
|
|
|
|
*ppStmt = 0;
|
|
|
|
}else{
|
2014-09-05 23:31:15 +04:00
|
|
|
rc = prepareAndCollectError(db, ppStmt, pzErrmsg, zSql);
|
2014-09-02 23:59:40 +04:00
|
|
|
sqlite3_free(zSql);
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-09-05 23:31:15 +04:00
|
|
|
/*
|
|
|
|
** Free the OtaObjIter.azTblCol[] and OtaObjIter.abTblPk[] arrays allocated
|
2015-02-03 18:56:08 +03:00
|
|
|
** by an earlier call to otaObjIterCacheTableInfo().
|
2014-09-05 23:31:15 +04:00
|
|
|
*/
|
|
|
|
static void otaObjIterFreeCols(OtaObjIter *pIter){
|
|
|
|
int i;
|
|
|
|
for(i=0; i<pIter->nTblCol; i++){
|
|
|
|
sqlite3_free(pIter->azTblCol[i]);
|
2015-01-31 23:42:04 +03:00
|
|
|
sqlite3_free(pIter->azTblType[i]);
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
2014-09-05 23:31:15 +04:00
|
|
|
sqlite3_free(pIter->azTblCol);
|
|
|
|
pIter->azTblCol = 0;
|
2015-01-31 23:42:04 +03:00
|
|
|
pIter->azTblType = 0;
|
2015-02-04 14:08:47 +03:00
|
|
|
pIter->aiSrcOrder = 0;
|
2014-09-05 23:31:15 +04:00
|
|
|
pIter->abTblPk = 0;
|
2015-02-05 20:36:30 +03:00
|
|
|
pIter->abNotNull = 0;
|
2014-09-05 23:31:15 +04:00
|
|
|
pIter->nTblCol = 0;
|
2014-09-08 21:50:35 +04:00
|
|
|
sqlite3_free(pIter->zMask);
|
|
|
|
pIter->zMask = 0;
|
2014-12-06 22:30:41 +03:00
|
|
|
pIter->eType = 0; /* Invalid value */
|
2014-09-08 21:50:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Finalize all statements and free all allocations that are specific to
|
|
|
|
** the current object (table/index pair).
|
|
|
|
*/
|
|
|
|
static void otaObjIterClearStatements(OtaObjIter *pIter){
|
|
|
|
sqlite3_finalize(pIter->pSelect);
|
|
|
|
sqlite3_finalize(pIter->pInsert);
|
|
|
|
sqlite3_finalize(pIter->pDelete);
|
|
|
|
sqlite3_finalize(pIter->pUpdate);
|
2015-02-21 23:08:25 +03:00
|
|
|
sqlite3_finalize(pIter->pTmpInsert);
|
2014-09-08 21:50:35 +04:00
|
|
|
pIter->pSelect = 0;
|
|
|
|
pIter->pInsert = 0;
|
|
|
|
pIter->pDelete = 0;
|
|
|
|
pIter->pUpdate = 0;
|
2015-02-21 23:08:25 +03:00
|
|
|
pIter->pTmpInsert = 0;
|
2014-09-08 21:50:35 +04:00
|
|
|
pIter->nCol = 0;
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
|
|
|
|
2014-09-05 23:31:15 +04:00
|
|
|
/*
|
|
|
|
** Clean up any resources allocated as part of the iterator object passed
|
|
|
|
** as the only argument.
|
|
|
|
*/
|
|
|
|
static void otaObjIterFinalize(OtaObjIter *pIter){
|
2014-09-08 21:50:35 +04:00
|
|
|
otaObjIterClearStatements(pIter);
|
2014-09-05 23:31:15 +04:00
|
|
|
sqlite3_finalize(pIter->pTblIter);
|
|
|
|
sqlite3_finalize(pIter->pIdxIter);
|
|
|
|
otaObjIterFreeCols(pIter);
|
|
|
|
memset(pIter, 0, sizeof(OtaObjIter));
|
|
|
|
}
|
2014-09-02 23:59:40 +04:00
|
|
|
|
2014-09-05 23:31:15 +04:00
|
|
|
/*
|
|
|
|
** Advance the iterator to the next position.
|
|
|
|
**
|
|
|
|
** If no error occurs, SQLITE_OK is returned and the iterator is left
|
|
|
|
** pointing to the next entry. Otherwise, an error code and message is
|
|
|
|
** left in the OTA handle passed as the first argument. A copy of the
|
|
|
|
** error code is returned.
|
|
|
|
*/
|
|
|
|
static int otaObjIterNext(sqlite3ota *p, OtaObjIter *pIter){
|
|
|
|
int rc = p->rc;
|
2014-09-02 23:59:40 +04:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
|
2014-09-05 23:31:15 +04:00
|
|
|
/* Free any SQLite statements used while processing the previous object */
|
2014-09-08 21:50:35 +04:00
|
|
|
otaObjIterClearStatements(pIter);
|
2015-02-03 18:56:08 +03:00
|
|
|
if( pIter->zIdx==0 ){
|
2015-02-21 23:08:25 +03:00
|
|
|
rc = sqlite3_exec(p->dbMain,
|
2015-02-03 18:56:08 +03:00
|
|
|
"DROP TRIGGER IF EXISTS temp.ota_insert_tr;"
|
|
|
|
"DROP TRIGGER IF EXISTS temp.ota_update1_tr;"
|
|
|
|
"DROP TRIGGER IF EXISTS temp.ota_update2_tr;"
|
|
|
|
"DROP TRIGGER IF EXISTS temp.ota_delete_tr;"
|
|
|
|
, 0, 0, &p->zErrmsg
|
|
|
|
);
|
|
|
|
}
|
2014-09-05 23:31:15 +04:00
|
|
|
|
2015-02-03 18:56:08 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
if( pIter->bCleanup ){
|
|
|
|
otaObjIterFreeCols(pIter);
|
|
|
|
pIter->bCleanup = 0;
|
|
|
|
rc = sqlite3_step(pIter->pTblIter);
|
|
|
|
if( rc!=SQLITE_ROW ){
|
2015-02-19 22:59:35 +03:00
|
|
|
rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
|
2015-02-03 18:56:08 +03:00
|
|
|
pIter->zTbl = 0;
|
|
|
|
}else{
|
|
|
|
pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
|
2015-02-17 00:13:19 +03:00
|
|
|
rc = pIter->zTbl ? SQLITE_OK : SQLITE_NOMEM;
|
2015-02-03 18:56:08 +03:00
|
|
|
}
|
2014-09-05 23:31:15 +04:00
|
|
|
}else{
|
2015-02-03 18:56:08 +03:00
|
|
|
if( pIter->zIdx==0 ){
|
2015-02-17 00:13:19 +03:00
|
|
|
sqlite3_stmt *pIdx = pIter->pIdxIter;
|
|
|
|
rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC);
|
2015-02-03 18:56:08 +03:00
|
|
|
}
|
2015-02-17 00:13:19 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = sqlite3_step(pIter->pIdxIter);
|
|
|
|
if( rc!=SQLITE_ROW ){
|
2015-02-19 22:59:35 +03:00
|
|
|
rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
|
2015-02-17 00:13:19 +03:00
|
|
|
pIter->bCleanup = 1;
|
|
|
|
pIter->zIdx = 0;
|
|
|
|
}else{
|
|
|
|
pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
|
|
|
|
pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
|
|
|
|
pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
|
|
|
|
rc = pIter->zIdx ? SQLITE_OK : SQLITE_NOMEM;
|
|
|
|
}
|
2015-02-03 18:56:08 +03:00
|
|
|
}
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 23:31:15 +04:00
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
otaObjIterFinalize(pIter);
|
|
|
|
p->rc = rc;
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-09-05 23:31:15 +04:00
|
|
|
/*
|
|
|
|
** Initialize the iterator structure passed as the second argument.
|
|
|
|
**
|
|
|
|
** If no error occurs, SQLITE_OK is returned and the iterator is left
|
|
|
|
** pointing to the first entry. Otherwise, an error code and message is
|
|
|
|
** left in the OTA handle passed as the first argument. A copy of the
|
|
|
|
** error code is returned.
|
|
|
|
*/
|
|
|
|
static int otaObjIterFirst(sqlite3ota *p, OtaObjIter *pIter){
|
2014-09-02 23:59:40 +04:00
|
|
|
int rc;
|
2014-09-05 23:31:15 +04:00
|
|
|
memset(pIter, 0, sizeof(OtaObjIter));
|
2014-09-02 23:59:40 +04:00
|
|
|
|
2015-02-21 23:08:25 +03:00
|
|
|
rc = prepareAndCollectError(p->dbOta, &pIter->pTblIter, &p->zErrmsg,
|
|
|
|
"SELECT substr(name, 6) FROM sqlite_master "
|
|
|
|
"WHERE type='table' AND name LIKE 'data_%'"
|
2014-09-02 23:59:40 +04:00
|
|
|
);
|
2014-09-05 23:31:15 +04:00
|
|
|
|
2014-09-02 23:59:40 +04:00
|
|
|
if( rc==SQLITE_OK ){
|
2015-02-21 23:08:25 +03:00
|
|
|
rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
|
2015-01-31 23:42:04 +03:00
|
|
|
"SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
|
|
|
|
" FROM main.sqlite_master "
|
|
|
|
" WHERE type='index' AND tbl_name = ?"
|
2014-09-05 23:31:15 +04:00
|
|
|
);
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
|
|
|
|
2014-09-05 23:31:15 +04:00
|
|
|
pIter->bCleanup = 1;
|
|
|
|
p->rc = rc;
|
|
|
|
return otaObjIterNext(p, pIter);
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
|
|
|
|
2015-02-19 22:59:35 +03:00
|
|
|
/*
|
|
|
|
** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
|
|
|
|
** an error code is stored in the OTA handle passed as the first argument.
|
|
|
|
**
|
|
|
|
** If an error has already occurred (p->rc is already set to something other
|
|
|
|
** than SQLITE_OK), then this function returns NULL without modifying the
|
|
|
|
** stored error code. In this case it still calls sqlite3_free() on any
|
|
|
|
** printf() parameters associated with %z conversions.
|
|
|
|
*/
|
|
|
|
static char *otaMPrintf(sqlite3ota *p, const char *zFmt, ...){
|
|
|
|
char *zSql = 0;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, zFmt);
|
|
|
|
zSql = sqlite3_vmprintf(zFmt, ap);
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
if( zSql==0 ) p->rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
|
|
|
sqlite3_free(zSql);
|
|
|
|
zSql = 0;
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
return zSql;
|
|
|
|
}
|
|
|
|
|
2014-09-07 00:19:38 +04:00
|
|
|
/*
|
|
|
|
** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
|
|
|
|
** arguments are the usual subsitution values. This function performs
|
|
|
|
** the printf() style substitutions and executes the result as an SQL
|
|
|
|
** statement on the OTA handles database.
|
|
|
|
**
|
|
|
|
** If an error occurs, an error code and error message is stored in the
|
|
|
|
** OTA handle. If an error has already occurred when this function is
|
|
|
|
** called, it is a no-op.
|
|
|
|
*/
|
2015-02-21 23:08:25 +03:00
|
|
|
static int otaMPrintfExec(sqlite3ota *p, sqlite3 *db, const char *zFmt, ...){
|
2014-09-07 00:19:38 +04:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, zFmt);
|
2015-02-19 22:59:35 +03:00
|
|
|
char *zSql = sqlite3_vmprintf(zFmt, ap);
|
2014-09-07 00:19:38 +04:00
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
if( zSql==0 ){
|
|
|
|
p->rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
|
2014-09-07 00:19:38 +04:00
|
|
|
}
|
|
|
|
}
|
2015-02-19 22:59:35 +03:00
|
|
|
sqlite3_free(zSql);
|
2014-09-07 00:19:38 +04:00
|
|
|
va_end(ap);
|
|
|
|
return p->rc;
|
|
|
|
}
|
|
|
|
|
2015-02-19 22:59:35 +03:00
|
|
|
/*
|
|
|
|
** Attempt to allocate and return a pointer to a zeroed block of nByte
|
|
|
|
** bytes.
|
|
|
|
**
|
|
|
|
** If an error (i.e. an OOM condition) occurs, return NULL and leave an
|
|
|
|
** error code in the ota handle passed as the first argument. Or, if an
|
|
|
|
** error has already occurred when this function is called, return NULL
|
|
|
|
** immediately without attempting the allocation or modifying the stored
|
|
|
|
** error code.
|
|
|
|
*/
|
2015-02-09 23:07:35 +03:00
|
|
|
static void *otaMalloc(sqlite3ota *p, int nByte){
|
|
|
|
void *pRet = 0;
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
pRet = sqlite3_malloc(nByte);
|
|
|
|
if( pRet==0 ){
|
|
|
|
p->rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
|
|
|
memset(pRet, 0, nByte);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-21 13:46:23 +03:00
|
|
|
/*
|
2014-11-27 21:09:46 +03:00
|
|
|
** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
|
2014-11-21 13:46:23 +03:00
|
|
|
** there is room for at least nCol elements. If an OOM occurs, store an
|
|
|
|
** error code in the OTA handle passed as the first argument.
|
|
|
|
*/
|
2014-11-27 21:09:46 +03:00
|
|
|
static void otaAllocateIterArrays(sqlite3ota *p, OtaObjIter *pIter, int nCol){
|
2015-03-24 21:03:29 +03:00
|
|
|
int nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
|
2014-11-27 21:09:46 +03:00
|
|
|
char **azNew;
|
|
|
|
|
2015-02-09 23:07:35 +03:00
|
|
|
azNew = (char**)otaMalloc(p, nByte);
|
2014-11-27 21:09:46 +03:00
|
|
|
if( azNew ){
|
|
|
|
pIter->azTblCol = azNew;
|
2015-01-31 23:42:04 +03:00
|
|
|
pIter->azTblType = &azNew[nCol];
|
2015-02-04 14:08:47 +03:00
|
|
|
pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
|
2015-02-14 21:58:22 +03:00
|
|
|
pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
|
|
|
|
pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
|
2015-03-24 21:03:29 +03:00
|
|
|
pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
|
2014-11-21 13:46:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 22:59:35 +03:00
|
|
|
/*
|
|
|
|
** The first argument must be a nul-terminated string. This function
|
|
|
|
** returns a copy of the string in memory obtained from sqlite3_malloc().
|
|
|
|
** It is the responsibility of the caller to eventually free this memory
|
|
|
|
** using sqlite3_free().
|
|
|
|
**
|
|
|
|
** If an OOM condition is encountered when attempting to allocate memory,
|
|
|
|
** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
|
|
|
|
** if the allocation succeeds, (*pRc) is left unchanged.
|
|
|
|
*/
|
2015-02-16 09:27:37 +03:00
|
|
|
static char *otaStrndup(const char *zStr, int *pRc){
|
2015-01-31 23:42:04 +03:00
|
|
|
char *zRet = 0;
|
|
|
|
|
2015-02-16 09:27:37 +03:00
|
|
|
assert( *pRc==SQLITE_OK );
|
2015-01-31 23:42:04 +03:00
|
|
|
if( zStr ){
|
2015-02-16 09:27:37 +03:00
|
|
|
int nCopy = strlen(zStr) + 1;
|
2015-01-31 23:42:04 +03:00
|
|
|
zRet = (char*)sqlite3_malloc(nCopy);
|
|
|
|
if( zRet ){
|
|
|
|
memcpy(zRet, zStr, nCopy);
|
|
|
|
}else{
|
|
|
|
*pRc = SQLITE_NOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return zRet;
|
|
|
|
}
|
|
|
|
|
2015-02-19 22:59:35 +03:00
|
|
|
/*
|
|
|
|
** Finalize the statement passed as the second argument.
|
|
|
|
**
|
|
|
|
** If the sqlite3_finalize() call indicates that an error occurs, and the
|
|
|
|
** ota handle error code is not already set, set the error code and error
|
|
|
|
** message accordingly.
|
|
|
|
*/
|
|
|
|
static void otaFinalize(sqlite3ota *p, sqlite3_stmt *pStmt){
|
|
|
|
sqlite3 *db = sqlite3_db_handle(pStmt);
|
|
|
|
int rc = sqlite3_finalize(pStmt);
|
|
|
|
if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
|
|
|
|
p->rc = rc;
|
|
|
|
p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-05 04:49:31 +03:00
|
|
|
/* Determine the type of a table.
|
|
|
|
**
|
|
|
|
** peType is of type (int*), a pointer to an output parameter of type
|
|
|
|
** (int). This call sets the output parameter as follows, depending
|
|
|
|
** on the type of the table specified by parameters dbName and zTbl.
|
|
|
|
**
|
|
|
|
** OTA_PK_NOTABLE: No such table.
|
|
|
|
** OTA_PK_NONE: Table has an implicit rowid.
|
|
|
|
** OTA_PK_IPK: Table has an explicit IPK column.
|
|
|
|
** OTA_PK_EXTERNAL: Table has an external PK index.
|
|
|
|
** OTA_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
|
|
|
|
** OTA_PK_VTAB: Table is a virtual table.
|
|
|
|
**
|
|
|
|
** Argument *piPk is also of type (int*), and also points to an output
|
|
|
|
** parameter. Unless the table has an external primary key index
|
|
|
|
** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
|
|
|
|
** if the table does have an external primary key index, then *piPk
|
|
|
|
** is set to the root page number of the primary key index before
|
|
|
|
** returning.
|
|
|
|
**
|
|
|
|
** ALGORITHM:
|
|
|
|
**
|
|
|
|
** if( no entry exists in sqlite_master ){
|
|
|
|
** return OTA_PK_NOTABLE
|
|
|
|
** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
|
|
|
|
** return OTA_PK_VTAB
|
|
|
|
** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
|
|
|
|
** if( the index that is the pk exists in sqlite_master ){
|
|
|
|
** *piPK = rootpage of that index.
|
|
|
|
** return OTA_PK_EXTERNAL
|
|
|
|
** }else{
|
|
|
|
** return OTA_PK_WITHOUT_ROWID
|
|
|
|
** }
|
|
|
|
** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
|
|
|
|
** return OTA_PK_IPK
|
|
|
|
** }else{
|
|
|
|
** return OTA_PK_NONE
|
|
|
|
** }
|
|
|
|
*/
|
2015-02-17 23:49:42 +03:00
|
|
|
static void otaTableType(
|
|
|
|
sqlite3ota *p,
|
2015-02-05 04:49:31 +03:00
|
|
|
const char *zTab,
|
|
|
|
int *peType,
|
2015-02-21 23:08:25 +03:00
|
|
|
int *piTnum,
|
2015-02-05 04:49:31 +03:00
|
|
|
int *piPk
|
|
|
|
){
|
2015-02-17 23:49:42 +03:00
|
|
|
/*
|
|
|
|
** 0) SELECT count(*) FROM sqlite_master where name=%Q AND IsVirtual(%Q)
|
|
|
|
** 1) PRAGMA index_list = ?
|
|
|
|
** 2) SELECT count(*) FROM sqlite_master where name=%Q
|
|
|
|
** 3) PRAGMA table_info = ?
|
|
|
|
*/
|
|
|
|
sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
|
2015-02-05 04:49:31 +03:00
|
|
|
|
|
|
|
*peType = OTA_PK_NOTABLE;
|
|
|
|
*piPk = 0;
|
2015-02-17 23:49:42 +03:00
|
|
|
|
|
|
|
assert( p->rc==SQLITE_OK );
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
|
2015-02-17 23:49:42 +03:00
|
|
|
sqlite3_mprintf(
|
2015-02-21 23:08:25 +03:00
|
|
|
"SELECT (sql LIKE 'create virtual%%'), rootpage"
|
2015-02-17 23:49:42 +03:00
|
|
|
" FROM sqlite_master"
|
|
|
|
" WHERE name=%Q", zTab
|
|
|
|
));
|
|
|
|
if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
|
|
|
|
/* Either an error, or no such table. */
|
|
|
|
goto otaTableType_end;
|
2015-02-05 04:49:31 +03:00
|
|
|
}
|
2015-02-17 23:49:42 +03:00
|
|
|
if( sqlite3_column_int(aStmt[0], 0) ){
|
2015-02-05 04:49:31 +03:00
|
|
|
*peType = OTA_PK_VTAB; /* virtual table */
|
|
|
|
goto otaTableType_end;
|
|
|
|
}
|
2015-02-21 23:08:25 +03:00
|
|
|
*piTnum = sqlite3_column_int(aStmt[0], 1);
|
2015-02-17 23:49:42 +03:00
|
|
|
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
|
2015-02-17 23:49:42 +03:00
|
|
|
sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
|
|
|
|
);
|
|
|
|
if( p->rc ) goto otaTableType_end;
|
|
|
|
while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
|
|
|
|
const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
|
|
|
|
const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
|
|
|
|
if( zOrig && zIdx && zOrig[0]=='p' ){
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
|
2015-02-17 23:49:42 +03:00
|
|
|
sqlite3_mprintf(
|
|
|
|
"SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
|
|
|
|
));
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
|
|
|
|
*piPk = sqlite3_column_int(aStmt[2], 0);
|
|
|
|
*peType = OTA_PK_EXTERNAL;
|
|
|
|
}else{
|
|
|
|
*peType = OTA_PK_WITHOUT_ROWID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto otaTableType_end;
|
2015-02-05 04:49:31 +03:00
|
|
|
}
|
|
|
|
}
|
2015-02-17 23:49:42 +03:00
|
|
|
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
|
2015-02-17 23:49:42 +03:00
|
|
|
sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
|
|
|
|
);
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
|
|
|
|
if( sqlite3_column_int(aStmt[3],5)>0 ){
|
2015-02-05 04:49:31 +03:00
|
|
|
*peType = OTA_PK_IPK; /* explicit IPK column */
|
2015-02-17 23:49:42 +03:00
|
|
|
goto otaTableType_end;
|
2015-02-05 04:49:31 +03:00
|
|
|
}
|
|
|
|
}
|
2015-02-17 23:49:42 +03:00
|
|
|
*peType = OTA_PK_NONE;
|
2015-02-05 04:49:31 +03:00
|
|
|
}
|
|
|
|
|
2015-02-17 23:49:42 +03:00
|
|
|
otaTableType_end: {
|
|
|
|
int i;
|
|
|
|
for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
|
2015-02-19 22:59:35 +03:00
|
|
|
otaFinalize(p, aStmt[i]);
|
2015-02-17 23:49:42 +03:00
|
|
|
}
|
|
|
|
}
|
2015-02-05 04:49:31 +03:00
|
|
|
}
|
|
|
|
|
2015-03-24 21:03:29 +03:00
|
|
|
/*
|
|
|
|
** This is a helper function for otaObjIterCacheTableInfo(). It populates
|
|
|
|
** the pIter->abIndexed[] array.
|
|
|
|
*/
|
|
|
|
static void otaObjIterCacheIndexedCols(sqlite3ota *p, OtaObjIter *pIter){
|
|
|
|
sqlite3_stmt *pList = 0;
|
|
|
|
int bIndex = 0;
|
|
|
|
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
|
|
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
|
|
|
|
sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pList) ){
|
|
|
|
const char *zIdx = (const char*)sqlite3_column_text(pList, 1);
|
|
|
|
sqlite3_stmt *pXInfo = 0;
|
|
|
|
if( zIdx==0 ) break;
|
|
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
|
|
|
|
sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
|
|
|
|
);
|
|
|
|
while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
|
|
|
|
int iCid = sqlite3_column_int(pXInfo, 1);
|
|
|
|
if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
|
|
|
|
}
|
|
|
|
otaFinalize(p, pXInfo);
|
|
|
|
bIndex = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
otaFinalize(p, pList);
|
|
|
|
if( bIndex==0 ) pIter->abIndexed = 0;
|
|
|
|
}
|
|
|
|
|
2015-01-31 23:42:04 +03:00
|
|
|
|
2014-09-05 23:31:15 +04:00
|
|
|
/*
|
|
|
|
** If they are not already populated, populate the pIter->azTblCol[],
|
2014-11-20 20:37:08 +03:00
|
|
|
** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
|
2015-02-03 18:56:08 +03:00
|
|
|
** the table (not index) that the iterator currently points to.
|
2014-09-05 23:31:15 +04:00
|
|
|
**
|
|
|
|
** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
|
|
|
|
** an error does occur, an error code and error message are also left in
|
|
|
|
** the OTA handle.
|
|
|
|
*/
|
2015-02-03 18:56:08 +03:00
|
|
|
static int otaObjIterCacheTableInfo(sqlite3ota *p, OtaObjIter *pIter){
|
2014-09-05 23:31:15 +04:00
|
|
|
if( pIter->azTblCol==0 ){
|
2014-11-27 21:09:46 +03:00
|
|
|
sqlite3_stmt *pStmt = 0;
|
2014-09-05 23:31:15 +04:00
|
|
|
int nCol = 0;
|
2014-11-27 21:09:46 +03:00
|
|
|
int i; /* for() loop iterator variable */
|
2014-12-06 22:30:41 +03:00
|
|
|
int bOtaRowid = 0; /* If input table has column "ota_rowid" */
|
2015-02-03 18:56:08 +03:00
|
|
|
int iOrder = 0;
|
2015-02-21 23:08:25 +03:00
|
|
|
int iTnum = 0;
|
2014-09-05 23:31:15 +04:00
|
|
|
|
2015-02-03 18:56:08 +03:00
|
|
|
/* Figure out the type of table this step will deal with. */
|
2014-12-06 22:30:41 +03:00
|
|
|
assert( pIter->eType==0 );
|
2015-02-21 23:08:25 +03:00
|
|
|
otaTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
|
2015-03-05 19:21:20 +03:00
|
|
|
if( p->rc==SQLITE_OK && pIter->eType==OTA_PK_NOTABLE ){
|
|
|
|
p->rc = SQLITE_ERROR;
|
|
|
|
p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
|
|
|
|
}
|
2015-02-05 04:49:31 +03:00
|
|
|
if( p->rc ) return p->rc;
|
2015-02-21 23:08:25 +03:00
|
|
|
if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
|
2015-02-05 04:49:31 +03:00
|
|
|
|
2015-02-03 18:56:08 +03:00
|
|
|
assert( pIter->eType==OTA_PK_NONE || pIter->eType==OTA_PK_IPK
|
|
|
|
|| pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_WITHOUT_ROWID
|
|
|
|
|| pIter->eType==OTA_PK_VTAB
|
|
|
|
);
|
2014-11-21 13:46:23 +03:00
|
|
|
|
2014-11-27 21:09:46 +03:00
|
|
|
/* Populate the azTblCol[] and nTblCol variables based on the columns
|
|
|
|
** of the input table. Ignore any input table columns that begin with
|
|
|
|
** "ota_". */
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = prepareFreeAndCollectError(p->dbOta, &pStmt, &p->zErrmsg,
|
2014-11-27 21:09:46 +03:00
|
|
|
sqlite3_mprintf("SELECT * FROM 'data_%q'", pIter->zTbl)
|
|
|
|
);
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
nCol = sqlite3_column_count(pStmt);
|
|
|
|
otaAllocateIterArrays(p, pIter, nCol);
|
|
|
|
}
|
|
|
|
for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
|
|
|
|
const char *zName = (const char*)sqlite3_column_name(pStmt, i);
|
|
|
|
if( sqlite3_strnicmp("ota_", zName, 4) ){
|
2015-02-16 09:27:37 +03:00
|
|
|
char *zCopy = otaStrndup(zName, &p->rc);
|
2015-02-04 14:08:47 +03:00
|
|
|
pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
|
2014-11-27 21:09:46 +03:00
|
|
|
pIter->azTblCol[pIter->nTblCol++] = zCopy;
|
|
|
|
}
|
2014-12-06 22:30:41 +03:00
|
|
|
else if( 0==sqlite3_stricmp("ota_rowid", zName) ){
|
|
|
|
bOtaRowid = 1;
|
|
|
|
}
|
2014-11-27 21:09:46 +03:00
|
|
|
}
|
|
|
|
sqlite3_finalize(pStmt);
|
|
|
|
pStmt = 0;
|
|
|
|
|
2015-02-03 18:56:08 +03:00
|
|
|
if( p->rc==SQLITE_OK
|
|
|
|
&& bOtaRowid!=(pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
|
|
|
|
){
|
|
|
|
p->rc = SQLITE_ERROR;
|
|
|
|
p->zErrmsg = sqlite3_mprintf(
|
|
|
|
"table data_%q %s ota_rowid column", pIter->zTbl,
|
|
|
|
(bOtaRowid ? "may not have" : "requires")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-11-27 21:09:46 +03:00
|
|
|
/* Check that all non-HIDDEN columns in the destination table are also
|
2015-02-03 18:56:08 +03:00
|
|
|
** present in the input table. Populate the abTblPk[], azTblType[] and
|
|
|
|
** aiTblOrder[] arrays at the same time. */
|
2014-11-27 21:09:46 +03:00
|
|
|
if( p->rc==SQLITE_OK ){
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
|
|
|
|
sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
|
2014-11-27 21:09:46 +03:00
|
|
|
);
|
|
|
|
}
|
2014-09-05 23:31:15 +04:00
|
|
|
while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
|
2014-11-27 21:09:46 +03:00
|
|
|
const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
|
2015-02-17 00:13:19 +03:00
|
|
|
if( zName==0 ) break; /* An OOM - finalize() below returns S_NOMEM */
|
2015-02-04 14:08:47 +03:00
|
|
|
for(i=iOrder; i<pIter->nTblCol; i++){
|
2015-02-03 18:56:08 +03:00
|
|
|
if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
|
2014-11-27 21:09:46 +03:00
|
|
|
}
|
|
|
|
if( i==pIter->nTblCol ){
|
|
|
|
p->rc = SQLITE_ERROR;
|
|
|
|
p->zErrmsg = sqlite3_mprintf("column missing from data_%q: %s",
|
|
|
|
pIter->zTbl, zName
|
|
|
|
);
|
|
|
|
}else{
|
2014-11-20 20:37:08 +03:00
|
|
|
int iPk = sqlite3_column_int(pStmt, 5);
|
2015-02-05 20:36:30 +03:00
|
|
|
int bNotNull = sqlite3_column_int(pStmt, 3);
|
2015-01-31 23:42:04 +03:00
|
|
|
const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
|
2015-02-04 14:08:47 +03:00
|
|
|
|
|
|
|
if( i!=iOrder ){
|
|
|
|
SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
|
|
|
|
SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
|
2014-12-06 22:30:41 +03:00
|
|
|
}
|
2015-02-03 18:56:08 +03:00
|
|
|
|
2015-02-16 09:27:37 +03:00
|
|
|
pIter->azTblType[iOrder] = otaStrndup(zType, &p->rc);
|
2015-02-04 14:08:47 +03:00
|
|
|
pIter->abTblPk[iOrder] = (iPk!=0);
|
2015-02-14 21:58:22 +03:00
|
|
|
pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
|
2015-02-04 14:08:47 +03:00
|
|
|
iOrder++;
|
|
|
|
}
|
2015-02-03 18:56:08 +03:00
|
|
|
}
|
|
|
|
|
2015-02-19 22:59:35 +03:00
|
|
|
otaFinalize(p, pStmt);
|
2015-03-24 21:03:29 +03:00
|
|
|
otaObjIterCacheIndexedCols(p, pIter);
|
|
|
|
assert( pIter->eType!=OTA_PK_VTAB || pIter->abIndexed==0 );
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
|
|
|
|
2014-09-05 23:31:15 +04:00
|
|
|
return p->rc;
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
|
|
|
|
2014-09-15 18:54:07 +04:00
|
|
|
/*
|
|
|
|
** This function constructs and returns a pointer to a nul-terminated
|
|
|
|
** string containing some SQL clause or list based on one or more of the
|
|
|
|
** column names currently stored in the pIter->azTblCol[] array.
|
|
|
|
*/
|
2014-09-05 23:31:15 +04:00
|
|
|
static char *otaObjIterGetCollist(
|
2014-09-15 18:54:07 +04:00
|
|
|
sqlite3ota *p, /* OTA object */
|
2015-02-03 18:56:08 +03:00
|
|
|
OtaObjIter *pIter /* Object iterator for column names */
|
2014-09-05 23:31:15 +04:00
|
|
|
){
|
|
|
|
char *zList = 0;
|
2015-02-03 18:56:08 +03:00
|
|
|
const char *zSep = "";
|
|
|
|
int i;
|
|
|
|
for(i=0; i<pIter->nTblCol; i++){
|
|
|
|
const char *z = pIter->azTblCol[i];
|
2015-02-04 14:08:47 +03:00
|
|
|
zList = otaMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
|
2015-02-03 18:56:08 +03:00
|
|
|
zSep = ", ";
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
2014-09-05 23:31:15 +04:00
|
|
|
return zList;
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
|
|
|
|
2015-01-31 23:42:04 +03:00
|
|
|
/*
|
|
|
|
** This function is used to create a SELECT list (the list of SQL
|
|
|
|
** expressions that follows a SELECT keyword) for a SELECT statement
|
2015-03-24 21:03:29 +03:00
|
|
|
** used to read from an data_xxx or ota_tmp_xxx table while updating the
|
|
|
|
** index object currently indicated by the iterator object passed as the
|
|
|
|
** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
|
|
|
|
** to obtain the required information.
|
2015-01-31 23:42:04 +03:00
|
|
|
**
|
|
|
|
** If the index is of the following form:
|
|
|
|
**
|
|
|
|
** CREATE INDEX i1 ON t1(c, b COLLATE nocase);
|
|
|
|
**
|
|
|
|
** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
|
|
|
|
** "ipk", the returned string is:
|
|
|
|
**
|
|
|
|
** "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
|
|
|
|
**
|
|
|
|
** As well as the returned string, three other malloc'd strings are
|
|
|
|
** returned via output parameters. As follows:
|
|
|
|
**
|
|
|
|
** pzImposterCols: ...
|
|
|
|
** pzImposterPk: ...
|
|
|
|
** pzWhere: ...
|
|
|
|
*/
|
|
|
|
static char *otaObjIterGetIndexCols(
|
|
|
|
sqlite3ota *p, /* OTA object */
|
|
|
|
OtaObjIter *pIter, /* Object iterator for column names */
|
|
|
|
char **pzImposterCols, /* OUT: Columns for imposter table */
|
|
|
|
char **pzImposterPk, /* OUT: Imposter PK clause */
|
|
|
|
char **pzWhere, /* OUT: WHERE clause */
|
|
|
|
int *pnBind /* OUT: Total number of columns */
|
|
|
|
){
|
|
|
|
int rc = p->rc; /* Error code */
|
|
|
|
int rc2; /* sqlite3_finalize() return code */
|
|
|
|
char *zRet = 0; /* String to return */
|
|
|
|
char *zImpCols = 0; /* String to return via *pzImposterCols */
|
|
|
|
char *zImpPK = 0; /* String to return via *pzImposterPK */
|
|
|
|
char *zWhere = 0; /* String to return via *pzWhere */
|
|
|
|
int nBind = 0; /* Value to return via *pnBind */
|
2015-02-03 18:56:08 +03:00
|
|
|
const char *zCom = ""; /* Set to ", " later on */
|
2015-01-31 23:42:04 +03:00
|
|
|
const char *zAnd = ""; /* Set to " AND " later on */
|
|
|
|
sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = ? */
|
|
|
|
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
assert( p->zErrmsg==0 );
|
2015-02-21 23:08:25 +03:00
|
|
|
rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
|
2015-01-31 23:42:04 +03:00
|
|
|
sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
|
|
|
|
int iCid = sqlite3_column_int(pXInfo, 1);
|
2015-02-03 21:43:42 +03:00
|
|
|
int bDesc = sqlite3_column_int(pXInfo, 3);
|
|
|
|
const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
|
2015-01-31 23:42:04 +03:00
|
|
|
const char *zCol;
|
|
|
|
const char *zType;
|
|
|
|
|
|
|
|
if( iCid<0 ){
|
|
|
|
/* An integer primary key. If the table has an explicit IPK, use
|
|
|
|
** its name. Otherwise, use "ota_rowid". */
|
2015-02-03 18:56:08 +03:00
|
|
|
if( pIter->eType==OTA_PK_IPK ){
|
2015-01-31 23:42:04 +03:00
|
|
|
int i;
|
2015-02-17 00:13:19 +03:00
|
|
|
for(i=0; pIter->abTblPk[i]==0; i++);
|
2015-01-31 23:42:04 +03:00
|
|
|
assert( i<pIter->nTblCol );
|
|
|
|
zCol = pIter->azTblCol[i];
|
|
|
|
}else{
|
|
|
|
zCol = "ota_rowid";
|
|
|
|
}
|
|
|
|
zType = "INTEGER";
|
|
|
|
}else{
|
|
|
|
zCol = pIter->azTblCol[iCid];
|
|
|
|
zType = pIter->azTblType[iCid];
|
|
|
|
}
|
|
|
|
|
2015-02-03 18:56:08 +03:00
|
|
|
zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom, zCol, zCollate);
|
2015-01-31 23:42:04 +03:00
|
|
|
if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
|
2015-02-03 21:43:42 +03:00
|
|
|
const char *zOrder = (bDesc ? " DESC" : "");
|
2015-02-04 22:20:42 +03:00
|
|
|
zImpPK = sqlite3_mprintf("%z%s\"ota_imp_%d%w\"%s",
|
|
|
|
zImpPK, zCom, nBind, zCol, zOrder
|
|
|
|
);
|
2015-01-31 23:42:04 +03:00
|
|
|
}
|
2015-02-04 22:20:42 +03:00
|
|
|
zImpCols = sqlite3_mprintf("%z%s\"ota_imp_%d%w\" %s COLLATE %Q",
|
|
|
|
zImpCols, zCom, nBind, zCol, zType, zCollate
|
|
|
|
);
|
|
|
|
zWhere = sqlite3_mprintf(
|
|
|
|
"%z%s\"ota_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
|
2015-01-31 23:42:04 +03:00
|
|
|
);
|
|
|
|
if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
|
2015-02-03 18:56:08 +03:00
|
|
|
zCom = ", ";
|
2015-01-31 23:42:04 +03:00
|
|
|
zAnd = " AND ";
|
|
|
|
nBind++;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc2 = sqlite3_finalize(pXInfo);
|
|
|
|
if( rc==SQLITE_OK ) rc = rc2;
|
|
|
|
|
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
sqlite3_free(zRet);
|
|
|
|
sqlite3_free(zImpCols);
|
|
|
|
sqlite3_free(zImpPK);
|
|
|
|
sqlite3_free(zWhere);
|
|
|
|
zRet = 0;
|
|
|
|
zImpCols = 0;
|
|
|
|
zImpPK = 0;
|
|
|
|
zWhere = 0;
|
|
|
|
p->rc = rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pzImposterCols = zImpCols;
|
|
|
|
*pzImposterPk = zImpPK;
|
|
|
|
*pzWhere = zWhere;
|
|
|
|
*pnBind = nBind;
|
|
|
|
return zRet;
|
|
|
|
}
|
|
|
|
|
2014-12-06 22:30:41 +03:00
|
|
|
/*
|
|
|
|
** Assuming the current table columns are "a", "b" and "c", and the zObj
|
|
|
|
** paramter is passed "old", return a string of the form:
|
|
|
|
**
|
|
|
|
** "old.a, old.b, old.b"
|
|
|
|
**
|
|
|
|
** With the column names escaped.
|
|
|
|
**
|
|
|
|
** For tables with implicit rowids - OTA_PK_EXTERNAL and OTA_PK_NONE, append
|
|
|
|
** the text ", old._rowid_" to the returned value.
|
|
|
|
*/
|
2014-09-07 00:19:38 +04:00
|
|
|
static char *otaObjIterGetOldlist(
|
|
|
|
sqlite3ota *p,
|
2014-09-08 21:50:35 +04:00
|
|
|
OtaObjIter *pIter,
|
|
|
|
const char *zObj
|
2014-09-07 00:19:38 +04:00
|
|
|
){
|
|
|
|
char *zList = 0;
|
2015-03-24 21:03:29 +03:00
|
|
|
if( p->rc==SQLITE_OK && pIter->abIndexed ){
|
2014-09-08 21:50:35 +04:00
|
|
|
const char *zS = "";
|
2014-09-07 00:19:38 +04:00
|
|
|
int i;
|
|
|
|
for(i=0; i<pIter->nTblCol; i++){
|
2015-03-24 21:03:29 +03:00
|
|
|
if( pIter->abIndexed[i] ){
|
|
|
|
const char *zCol = pIter->azTblCol[i];
|
|
|
|
zList = sqlite3_mprintf("%z%s%s.\"%w\"", zList, zS, zObj, zCol);
|
|
|
|
}else{
|
|
|
|
zList = sqlite3_mprintf("%z%sNULL", zList, zS);
|
|
|
|
}
|
2014-09-08 21:50:35 +04:00
|
|
|
zS = ", ";
|
2014-09-07 00:19:38 +04:00
|
|
|
if( zList==0 ){
|
|
|
|
p->rc = SQLITE_NOMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-20 20:37:08 +03:00
|
|
|
|
|
|
|
/* For a table with implicit rowids, append "old._rowid_" to the list. */
|
2014-12-06 22:30:41 +03:00
|
|
|
if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
|
2015-02-18 20:40:05 +03:00
|
|
|
zList = otaMPrintf(p, "%z, %s._rowid_", zList, zObj);
|
2014-11-20 20:37:08 +03:00
|
|
|
}
|
2014-09-07 00:19:38 +04:00
|
|
|
}
|
|
|
|
return zList;
|
|
|
|
}
|
|
|
|
|
2014-12-06 22:30:41 +03:00
|
|
|
/*
|
|
|
|
** Return an expression that can be used in a WHERE clause to match the
|
|
|
|
** primary key of the current table. For example, if the table is:
|
|
|
|
**
|
|
|
|
** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
|
|
|
|
**
|
|
|
|
** Return the string:
|
|
|
|
**
|
|
|
|
** "b = ?1 AND c = ?2"
|
|
|
|
*/
|
2014-09-07 00:19:38 +04:00
|
|
|
static char *otaObjIterGetWhere(
|
|
|
|
sqlite3ota *p,
|
|
|
|
OtaObjIter *pIter
|
|
|
|
){
|
|
|
|
char *zList = 0;
|
2015-02-03 18:56:08 +03:00
|
|
|
if( pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE ){
|
2015-02-04 14:08:47 +03:00
|
|
|
zList = otaMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
|
2015-02-04 19:32:47 +03:00
|
|
|
}else if( pIter->eType==OTA_PK_EXTERNAL ){
|
|
|
|
const char *zSep = "";
|
|
|
|
int i;
|
|
|
|
for(i=0; i<pIter->nTblCol; i++){
|
|
|
|
if( pIter->abTblPk[i] ){
|
|
|
|
zList = otaMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
|
|
|
|
zSep = " AND ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
zList = otaMPrintf(p,
|
|
|
|
"_rowid_ = (SELECT id FROM ota_imposter2 WHERE %z)", zList
|
|
|
|
);
|
|
|
|
|
2015-02-03 18:56:08 +03:00
|
|
|
}else{
|
|
|
|
const char *zSep = "";
|
|
|
|
int i;
|
|
|
|
for(i=0; i<pIter->nTblCol; i++){
|
|
|
|
if( pIter->abTblPk[i] ){
|
|
|
|
const char *zCol = pIter->azTblCol[i];
|
2015-02-04 14:08:47 +03:00
|
|
|
zList = otaMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
|
2015-02-03 18:56:08 +03:00
|
|
|
zSep = " AND ";
|
2014-09-07 00:19:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return zList;
|
|
|
|
}
|
|
|
|
|
2014-09-08 21:50:35 +04:00
|
|
|
/*
|
|
|
|
** The SELECT statement iterating through the keys for the current object
|
|
|
|
** (p->objiter.pSelect) currently points to a valid row. However, there
|
|
|
|
** is something wrong with the ota_control value in the ota_control value
|
|
|
|
** stored in the (p->nCol+1)'th column. Set the error code and error message
|
|
|
|
** of the OTA handle to something reflecting this.
|
|
|
|
*/
|
|
|
|
static void otaBadControlError(sqlite3ota *p){
|
|
|
|
p->rc = SQLITE_ERROR;
|
2015-02-17 00:13:19 +03:00
|
|
|
p->zErrmsg = sqlite3_mprintf("invalid ota_control value");
|
2014-09-08 21:50:35 +04:00
|
|
|
}
|
|
|
|
|
2014-11-20 22:19:02 +03:00
|
|
|
|
2015-02-19 22:59:35 +03:00
|
|
|
/*
|
|
|
|
** Return a nul-terminated string containing the comma separated list of
|
|
|
|
** assignments that should be included following the "SET" keyword of
|
|
|
|
** an UPDATE statement used to update the table object that the iterator
|
|
|
|
** passed as the second argument currently points to if the ota_control
|
|
|
|
** column of the data_xxx table entry is set to zMask.
|
|
|
|
**
|
|
|
|
** The memory for the returned string is obtained from sqlite3_malloc().
|
|
|
|
** It is the responsibility of the caller to eventually free it using
|
|
|
|
** sqlite3_free().
|
|
|
|
**
|
|
|
|
** If an OOM error is encountered when allocating space for the new
|
|
|
|
** string, an error code is left in the ota handle passed as the first
|
|
|
|
** argument and NULL is returned. Or, if an error has already occurred
|
|
|
|
** when this function is called, NULL is returned immediately, without
|
|
|
|
** attempting the allocation or modifying the stored error code.
|
|
|
|
*/
|
2014-09-08 21:50:35 +04:00
|
|
|
static char *otaObjIterGetSetlist(
|
|
|
|
sqlite3ota *p,
|
|
|
|
OtaObjIter *pIter,
|
|
|
|
const char *zMask
|
|
|
|
){
|
|
|
|
char *zList = 0;
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if( strlen(zMask)!=pIter->nTblCol ){
|
|
|
|
otaBadControlError(p);
|
|
|
|
}else{
|
|
|
|
const char *zSep = "";
|
|
|
|
for(i=0; i<pIter->nTblCol; i++){
|
2015-02-04 14:08:47 +03:00
|
|
|
char c = zMask[pIter->aiSrcOrder[i]];
|
2014-11-20 22:19:02 +03:00
|
|
|
if( c=='x' ){
|
2015-02-04 14:08:47 +03:00
|
|
|
zList = otaMPrintf(p, "%z%s\"%w\"=?%d",
|
2014-09-08 21:50:35 +04:00
|
|
|
zList, zSep, pIter->azTblCol[i], i+1
|
|
|
|
);
|
2014-11-20 22:19:02 +03:00
|
|
|
zSep = ", ";
|
|
|
|
}
|
|
|
|
if( c=='d' ){
|
2015-02-04 14:08:47 +03:00
|
|
|
zList = otaMPrintf(p, "%z%s\"%w\"=ota_delta(\"%w\", ?%d)",
|
2014-11-20 22:19:02 +03:00
|
|
|
zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
|
|
|
|
);
|
2014-09-08 21:50:35 +04:00
|
|
|
zSep = ", ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return zList;
|
|
|
|
}
|
|
|
|
|
2015-02-19 22:59:35 +03:00
|
|
|
/*
|
|
|
|
** Return a nul-terminated string consisting of nByte comma separated
|
|
|
|
** "?" expressions. For example, if nByte is 3, return a pointer to
|
|
|
|
** a buffer containing the string "?,?,?".
|
|
|
|
**
|
|
|
|
** The memory for the returned string is obtained from sqlite3_malloc().
|
|
|
|
** It is the responsibility of the caller to eventually free it using
|
|
|
|
** sqlite3_free().
|
|
|
|
**
|
|
|
|
** If an OOM error is encountered when allocating space for the new
|
|
|
|
** string, an error code is left in the ota handle passed as the first
|
|
|
|
** argument and NULL is returned. Or, if an error has already occurred
|
|
|
|
** when this function is called, NULL is returned immediately, without
|
|
|
|
** attempting the allocation or modifying the stored error code.
|
|
|
|
*/
|
2014-09-05 23:31:15 +04:00
|
|
|
static char *otaObjIterGetBindlist(sqlite3ota *p, int nBind){
|
|
|
|
char *zRet = 0;
|
2015-02-09 23:07:35 +03:00
|
|
|
int nByte = nBind*2 + 1;
|
|
|
|
|
|
|
|
zRet = (char*)otaMalloc(p, nByte);
|
|
|
|
if( zRet ){
|
|
|
|
int i;
|
|
|
|
for(i=0; i<nBind; i++){
|
|
|
|
zRet[i*2] = '?';
|
|
|
|
zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
|
2014-09-05 23:31:15 +04:00
|
|
|
}
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
2014-09-05 23:31:15 +04:00
|
|
|
return zRet;
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
|
|
|
|
2015-02-03 21:43:42 +03:00
|
|
|
/*
|
|
|
|
** The iterator currently points to a table (not index) of type
|
|
|
|
** OTA_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
|
|
|
|
** declaration for the corresponding imposter table. For example,
|
|
|
|
** if the iterator points to a table created as:
|
|
|
|
**
|
|
|
|
** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
|
|
|
|
**
|
|
|
|
** this function returns:
|
|
|
|
**
|
|
|
|
** PRIMARY KEY("b", "a" DESC)
|
|
|
|
*/
|
|
|
|
static char *otaWithoutRowidPK(sqlite3ota *p, OtaObjIter *pIter){
|
|
|
|
char *z = 0;
|
|
|
|
assert( pIter->zIdx==0 );
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
const char *zSep = "PRIMARY KEY(";
|
2015-02-06 03:31:45 +03:00
|
|
|
sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
|
|
|
|
sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = <pk-index> */
|
|
|
|
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
|
2015-02-06 03:31:45 +03:00
|
|
|
sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
|
2015-02-03 21:43:42 +03:00
|
|
|
);
|
2015-02-06 03:31:45 +03:00
|
|
|
while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXList) ){
|
|
|
|
const char *zOrig = (const char*)sqlite3_column_text(pXList,3);
|
2015-02-17 00:13:19 +03:00
|
|
|
if( zOrig && strcmp(zOrig, "pk")==0 ){
|
|
|
|
const char *zIdx = (const char*)sqlite3_column_text(pXList,1);
|
|
|
|
if( zIdx ){
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
|
2015-02-17 00:13:19 +03:00
|
|
|
sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
|
|
|
|
);
|
|
|
|
}
|
2015-02-06 03:31:45 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-02-19 22:59:35 +03:00
|
|
|
otaFinalize(p, pXList);
|
2015-02-17 00:13:19 +03:00
|
|
|
|
2015-02-17 23:49:42 +03:00
|
|
|
while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
|
2015-02-03 21:43:42 +03:00
|
|
|
if( sqlite3_column_int(pXInfo, 5) ){
|
|
|
|
/* int iCid = sqlite3_column_int(pXInfo, 0); */
|
|
|
|
const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
|
|
|
|
const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
|
2015-02-04 14:08:47 +03:00
|
|
|
z = otaMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
|
2015-02-03 21:43:42 +03:00
|
|
|
zSep = ", ";
|
|
|
|
}
|
|
|
|
}
|
2015-02-04 14:08:47 +03:00
|
|
|
z = otaMPrintf(p, "%z)", z);
|
2015-02-19 22:59:35 +03:00
|
|
|
otaFinalize(p, pXInfo);
|
2015-02-03 21:43:42 +03:00
|
|
|
}
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
2015-02-17 23:49:42 +03:00
|
|
|
/*
|
|
|
|
** This function creates the second imposter table used when writing to
|
|
|
|
** a table b-tree where the table has an external primary key. If the
|
|
|
|
** iterator passed as the second argument does not currently point to
|
|
|
|
** a table (not index) with an external primary key, this function is a
|
|
|
|
** no-op.
|
|
|
|
**
|
|
|
|
** Assuming the iterator does point to a table with an external PK, this
|
|
|
|
** function creates a WITHOUT ROWID imposter table named "ota_imposter2"
|
|
|
|
** used to access that PK index. For example, if the target table is
|
|
|
|
** declared as follows:
|
|
|
|
**
|
|
|
|
** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
|
|
|
|
**
|
|
|
|
** then the imposter table schema is:
|
|
|
|
**
|
|
|
|
** CREATE TABLE ota_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
|
|
|
|
**
|
|
|
|
*/
|
2015-02-04 19:32:47 +03:00
|
|
|
static void otaCreateImposterTable2(sqlite3ota *p, OtaObjIter *pIter){
|
|
|
|
if( p->rc==SQLITE_OK && pIter->eType==OTA_PK_EXTERNAL ){
|
|
|
|
int tnum = pIter->iPkTnum; /* Root page of PK index */
|
|
|
|
sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */
|
|
|
|
const char *zIdx = 0; /* Name of PK index */
|
|
|
|
sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */
|
|
|
|
const char *zComma = "";
|
|
|
|
char *zCols = 0; /* Used to build up list of table cols */
|
|
|
|
char *zPk = 0; /* Used to build up table PK declaration */
|
|
|
|
|
|
|
|
/* Figure out the name of the primary key index for the current table.
|
|
|
|
** This is needed for the argument to "PRAGMA index_xinfo". Set
|
|
|
|
** zIdx to point to a nul-terminated string containing this name. */
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg,
|
2015-02-04 19:32:47 +03:00
|
|
|
"SELECT name FROM sqlite_master WHERE rootpage = ?"
|
|
|
|
);
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
sqlite3_bind_int(pQuery, 1, tnum);
|
|
|
|
if( SQLITE_ROW==sqlite3_step(pQuery) ){
|
|
|
|
zIdx = (const char*)sqlite3_column_text(pQuery, 0);
|
|
|
|
}
|
|
|
|
}
|
2015-02-18 20:40:05 +03:00
|
|
|
if( zIdx ){
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
|
2015-02-04 19:32:47 +03:00
|
|
|
sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
|
|
|
|
);
|
|
|
|
}
|
2015-02-18 20:40:05 +03:00
|
|
|
otaFinalize(p, pQuery);
|
2015-02-04 19:32:47 +03:00
|
|
|
|
|
|
|
while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
|
|
|
|
int bKey = sqlite3_column_int(pXInfo, 5);
|
|
|
|
if( bKey ){
|
|
|
|
int iCid = sqlite3_column_int(pXInfo, 1);
|
|
|
|
int bDesc = sqlite3_column_int(pXInfo, 3);
|
|
|
|
const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
|
|
|
|
zCols = otaMPrintf(p, "%z%sc%d %s COLLATE %s", zCols, zComma,
|
|
|
|
iCid, pIter->azTblType[iCid], zCollate
|
|
|
|
);
|
|
|
|
zPk = otaMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
|
|
|
|
zComma = ", ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
zCols = otaMPrintf(p, "%z, id INTEGER", zCols);
|
2015-02-19 22:59:35 +03:00
|
|
|
otaFinalize(p, pXInfo);
|
2015-02-04 19:32:47 +03:00
|
|
|
|
2015-02-21 23:08:25 +03:00
|
|
|
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
|
|
|
|
otaMPrintfExec(p, p->dbMain,
|
2015-02-04 19:32:47 +03:00
|
|
|
"CREATE TABLE ota_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
|
|
|
|
zCols, zPk
|
|
|
|
);
|
2015-02-21 23:08:25 +03:00
|
|
|
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
|
2015-02-04 19:32:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-03 18:56:08 +03:00
|
|
|
/*
|
|
|
|
** If an error has already occurred when this function is called, it
|
|
|
|
** immediately returns zero (without doing any work). Or, if an error
|
|
|
|
** occurs during the execution of this function, it sets the error code
|
|
|
|
** in the sqlite3ota object indicated by the first argument and returns
|
|
|
|
** zero.
|
|
|
|
**
|
|
|
|
** The iterator passed as the second argument is guaranteed to point to
|
|
|
|
** a table (not an index) when this function is called. This function
|
2015-02-19 22:59:35 +03:00
|
|
|
** attempts to create any imposter table required to write to the main
|
2015-02-03 18:56:08 +03:00
|
|
|
** table b-tree of the table before returning. Non-zero is returned if
|
2015-02-19 22:59:35 +03:00
|
|
|
** an imposter table are created, or zero otherwise.
|
2015-02-03 18:56:08 +03:00
|
|
|
**
|
2015-02-19 22:59:35 +03:00
|
|
|
** An imposter table is required in all cases except OTA_PK_VTAB. Only
|
|
|
|
** virtual tables are written to directly. The imposter table has the
|
|
|
|
** same schema as the actual target table (less any UNIQUE constraints).
|
|
|
|
** More precisely, the "same schema" means the same columns, types,
|
|
|
|
** collation sequences. For tables that do not have an external PRIMARY
|
|
|
|
** KEY, it also means the same PRIMARY KEY declaration.
|
2015-02-03 18:56:08 +03:00
|
|
|
*/
|
|
|
|
static void otaCreateImposterTable(sqlite3ota *p, OtaObjIter *pIter){
|
|
|
|
if( p->rc==SQLITE_OK && pIter->eType!=OTA_PK_VTAB ){
|
2015-02-04 19:32:47 +03:00
|
|
|
int tnum = pIter->iTnum;
|
2015-02-03 18:56:08 +03:00
|
|
|
const char *zComma = "";
|
|
|
|
char *zSql = 0;
|
|
|
|
int iCol;
|
2015-02-21 23:08:25 +03:00
|
|
|
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
|
2015-02-03 18:56:08 +03:00
|
|
|
|
|
|
|
for(iCol=0; p->rc==SQLITE_OK && iCol<pIter->nTblCol; iCol++){
|
2015-02-03 21:43:42 +03:00
|
|
|
const char *zPk = "";
|
2015-02-04 14:08:47 +03:00
|
|
|
const char *zCol = pIter->azTblCol[iCol];
|
2015-02-03 18:56:08 +03:00
|
|
|
const char *zColl = 0;
|
2015-02-03 21:43:42 +03:00
|
|
|
|
2015-02-03 18:56:08 +03:00
|
|
|
p->rc = sqlite3_table_column_metadata(
|
2015-02-21 23:08:25 +03:00
|
|
|
p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
|
2015-02-03 18:56:08 +03:00
|
|
|
);
|
2015-02-03 21:43:42 +03:00
|
|
|
|
|
|
|
if( pIter->eType==OTA_PK_IPK && pIter->abTblPk[iCol] ){
|
|
|
|
/* If the target table column is an "INTEGER PRIMARY KEY", add
|
|
|
|
** "PRIMARY KEY" to the imposter table column declaration. */
|
|
|
|
zPk = "PRIMARY KEY ";
|
|
|
|
}
|
2015-02-05 20:36:30 +03:00
|
|
|
zSql = otaMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s",
|
|
|
|
zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
|
|
|
|
(pIter->abNotNull[iCol] ? " NOT NULL" : "")
|
2015-02-03 18:56:08 +03:00
|
|
|
);
|
|
|
|
zComma = ", ";
|
|
|
|
}
|
|
|
|
|
2015-02-03 21:43:42 +03:00
|
|
|
if( pIter->eType==OTA_PK_WITHOUT_ROWID ){
|
|
|
|
char *zPk = otaWithoutRowidPK(p, pIter);
|
|
|
|
if( zPk ){
|
2015-02-04 14:08:47 +03:00
|
|
|
zSql = otaMPrintf(p, "%z, %z", zSql, zPk);
|
2015-02-03 18:56:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-21 23:08:25 +03:00
|
|
|
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
|
|
|
|
otaMPrintfExec(p, p->dbMain, "CREATE TABLE \"ota_imp_%w\"(%z)%s",
|
2015-02-04 22:20:42 +03:00
|
|
|
pIter->zTbl, zSql,
|
|
|
|
(pIter->eType==OTA_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
|
2015-02-03 18:56:08 +03:00
|
|
|
);
|
2015-02-21 23:08:25 +03:00
|
|
|
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Prepare a statement used to insert rows into the "ota_tmp_xxx" table.
|
|
|
|
** Specifically a statement of the form:
|
|
|
|
**
|
|
|
|
** INSERT INTO ota_tmp_xxx VALUES(?, ?, ? ...);
|
|
|
|
**
|
|
|
|
** The number of bound variables is equal to the number of columns in
|
|
|
|
** the target table, plus one (for the ota_control column), plus one more
|
|
|
|
** (for the ota_rowid column) if the target table is an implicit IPK or
|
|
|
|
** virtual table.
|
|
|
|
*/
|
|
|
|
static void otaObjIterPrepareTmpInsert(
|
|
|
|
sqlite3ota *p,
|
|
|
|
OtaObjIter *pIter,
|
|
|
|
const char *zCollist,
|
|
|
|
const char *zOtaRowid
|
|
|
|
){
|
|
|
|
int bOtaRowid = (pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE);
|
|
|
|
char *zBind = otaObjIterGetBindlist(p, pIter->nTblCol + 1 + bOtaRowid);
|
|
|
|
if( zBind ){
|
|
|
|
assert( pIter->pTmpInsert==0 );
|
|
|
|
p->rc = prepareFreeAndCollectError(
|
|
|
|
p->dbOta, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
|
|
|
|
"INSERT INTO 'ota_tmp_%q'(ota_control,%s%s) VALUES(%z)",
|
|
|
|
pIter->zTbl, zCollist, zOtaRowid, zBind
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void otaTmpInsertFunc(
|
|
|
|
sqlite3_context *pCtx,
|
|
|
|
int nVal,
|
|
|
|
sqlite3_value **apVal
|
|
|
|
){
|
|
|
|
sqlite3ota *p = sqlite3_user_data(pCtx);
|
|
|
|
int rc = SQLITE_OK;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i=0; rc==SQLITE_OK && i<nVal; i++){
|
|
|
|
rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
|
|
|
|
}
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
sqlite3_step(p->objiter.pTmpInsert);
|
|
|
|
rc = sqlite3_reset(p->objiter.pTmpInsert);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
sqlite3_result_error_code(pCtx, rc);
|
2015-02-03 18:56:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-02 23:59:40 +04:00
|
|
|
/*
|
2014-09-05 23:31:15 +04:00
|
|
|
** Ensure that the SQLite statement handles required to update the
|
|
|
|
** target database object currently indicated by the iterator passed
|
|
|
|
** as the second argument are available.
|
2014-09-02 23:59:40 +04:00
|
|
|
*/
|
2014-09-05 23:31:15 +04:00
|
|
|
static int otaObjIterPrepareAll(
|
|
|
|
sqlite3ota *p,
|
|
|
|
OtaObjIter *pIter,
|
|
|
|
int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
|
|
|
|
){
|
|
|
|
assert( pIter->bCleanup==0 );
|
2015-02-03 18:56:08 +03:00
|
|
|
if( pIter->pSelect==0 && otaObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
|
2015-02-04 19:32:47 +03:00
|
|
|
const int tnum = pIter->iTnum;
|
2014-09-05 23:31:15 +04:00
|
|
|
char *zCollist = 0; /* List of indexed columns */
|
|
|
|
char **pz = &p->zErrmsg;
|
|
|
|
const char *zIdx = pIter->zIdx;
|
|
|
|
char *zLimit = 0;
|
|
|
|
|
|
|
|
if( nOffset ){
|
|
|
|
zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
|
|
|
|
if( !zLimit ) p->rc = SQLITE_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( zIdx ){
|
2015-02-04 22:20:42 +03:00
|
|
|
const char *zTbl = pIter->zTbl;
|
2015-02-03 18:56:08 +03:00
|
|
|
char *zImposterCols = 0; /* Columns for imposter table */
|
|
|
|
char *zImposterPK = 0; /* Primary key declaration for imposter */
|
|
|
|
char *zWhere = 0; /* WHERE clause on PK columns */
|
2015-01-31 23:42:04 +03:00
|
|
|
char *zBind = 0;
|
|
|
|
int nBind = 0;
|
2014-09-05 23:31:15 +04:00
|
|
|
|
2014-12-06 22:30:41 +03:00
|
|
|
assert( pIter->eType!=OTA_PK_VTAB );
|
2015-01-31 23:42:04 +03:00
|
|
|
zCollist = otaObjIterGetIndexCols(
|
|
|
|
p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
|
|
|
|
);
|
|
|
|
zBind = otaObjIterGetBindlist(p, nBind);
|
|
|
|
|
|
|
|
/* Create the imposter table used to write to this index. */
|
2015-02-21 23:08:25 +03:00
|
|
|
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
|
|
|
|
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
|
|
|
|
otaMPrintfExec(p, p->dbMain,
|
2015-02-04 22:20:42 +03:00
|
|
|
"CREATE TABLE \"ota_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
|
|
|
|
zTbl, zImposterCols, zImposterPK
|
2015-01-31 23:42:04 +03:00
|
|
|
);
|
2015-02-21 23:08:25 +03:00
|
|
|
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
|
2014-11-21 13:46:23 +03:00
|
|
|
|
2015-01-31 23:42:04 +03:00
|
|
|
/* Create the statement to insert index entries */
|
|
|
|
pIter->nCol = nBind;
|
2014-09-05 23:31:15 +04:00
|
|
|
if( p->rc==SQLITE_OK ){
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = prepareFreeAndCollectError(
|
|
|
|
p->dbMain, &pIter->pInsert, &p->zErrmsg,
|
2015-02-04 22:20:42 +03:00
|
|
|
sqlite3_mprintf("INSERT INTO \"ota_imp_%w\" VALUES(%s)", zTbl, zBind)
|
2014-09-05 23:31:15 +04:00
|
|
|
);
|
|
|
|
}
|
2015-01-31 23:42:04 +03:00
|
|
|
|
|
|
|
/* And to delete index entries */
|
2014-09-07 00:19:38 +04:00
|
|
|
if( p->rc==SQLITE_OK ){
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = prepareFreeAndCollectError(
|
|
|
|
p->dbMain, &pIter->pDelete, &p->zErrmsg,
|
2015-02-04 22:20:42 +03:00
|
|
|
sqlite3_mprintf("DELETE FROM \"ota_imp_%w\" WHERE %s", zTbl, zWhere)
|
2014-09-07 00:19:38 +04:00
|
|
|
);
|
|
|
|
}
|
2014-09-05 23:31:15 +04:00
|
|
|
|
|
|
|
/* Create the SELECT statement to read keys in sorted order */
|
|
|
|
if( p->rc==SQLITE_OK ){
|
2014-11-20 20:37:08 +03:00
|
|
|
char *zSql;
|
2014-12-06 22:30:41 +03:00
|
|
|
if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
|
2014-11-20 20:37:08 +03:00
|
|
|
zSql = sqlite3_mprintf(
|
2015-02-21 23:08:25 +03:00
|
|
|
"SELECT %s, ota_control FROM 'ota_tmp_%q' ORDER BY %s%s",
|
2014-12-06 22:30:41 +03:00
|
|
|
zCollist, pIter->zTbl,
|
2014-11-20 20:37:08 +03:00
|
|
|
zCollist, zLimit
|
|
|
|
);
|
|
|
|
}else{
|
|
|
|
zSql = sqlite3_mprintf(
|
2015-02-21 23:08:25 +03:00
|
|
|
"SELECT %s, ota_control FROM 'data_%q' "
|
2014-09-08 21:50:35 +04:00
|
|
|
"WHERE typeof(ota_control)='integer' AND ota_control!=1 "
|
2014-11-20 20:37:08 +03:00
|
|
|
"UNION ALL "
|
2015-02-21 23:08:25 +03:00
|
|
|
"SELECT %s, ota_control FROM 'ota_tmp_%q' "
|
2014-09-07 00:19:38 +04:00
|
|
|
"ORDER BY %s%s",
|
|
|
|
zCollist, pIter->zTbl,
|
|
|
|
zCollist, pIter->zTbl,
|
|
|
|
zCollist, zLimit
|
2014-11-20 20:37:08 +03:00
|
|
|
);
|
|
|
|
}
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = prepareFreeAndCollectError(p->dbOta, &pIter->pSelect, pz, zSql);
|
2014-09-05 23:31:15 +04:00
|
|
|
}
|
2015-01-31 23:42:04 +03:00
|
|
|
|
|
|
|
sqlite3_free(zImposterCols);
|
|
|
|
sqlite3_free(zImposterPK);
|
|
|
|
sqlite3_free(zWhere);
|
|
|
|
sqlite3_free(zBind);
|
2014-09-05 23:31:15 +04:00
|
|
|
}else{
|
2014-12-06 22:30:41 +03:00
|
|
|
int bOtaRowid = (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE);
|
2015-02-03 18:56:08 +03:00
|
|
|
const char *zTbl = pIter->zTbl; /* Table this step applies to */
|
|
|
|
const char *zWrite; /* Imposter table name */
|
|
|
|
|
|
|
|
char *zBindings = otaObjIterGetBindlist(p, pIter->nTblCol + bOtaRowid);
|
2014-09-07 00:19:38 +04:00
|
|
|
char *zWhere = otaObjIterGetWhere(p, pIter);
|
2014-09-08 21:50:35 +04:00
|
|
|
char *zOldlist = otaObjIterGetOldlist(p, pIter, "old");
|
|
|
|
char *zNewlist = otaObjIterGetOldlist(p, pIter, "new");
|
2014-12-06 22:30:41 +03:00
|
|
|
|
2015-02-03 18:56:08 +03:00
|
|
|
zCollist = otaObjIterGetCollist(p, pIter);
|
2014-09-05 23:31:15 +04:00
|
|
|
pIter->nCol = pIter->nTblCol;
|
|
|
|
|
|
|
|
/* Create the SELECT statement to read keys from data_xxx */
|
|
|
|
if( p->rc==SQLITE_OK ){
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = prepareFreeAndCollectError(p->dbOta, &pIter->pSelect, pz,
|
2014-09-05 23:31:15 +04:00
|
|
|
sqlite3_mprintf(
|
2015-02-21 23:08:25 +03:00
|
|
|
"SELECT %s, ota_control%s FROM 'data_%q'%s",
|
2014-12-06 22:30:41 +03:00
|
|
|
zCollist, (bOtaRowid ? ", ota_rowid" : ""), zTbl, zLimit
|
2014-11-21 13:46:23 +03:00
|
|
|
)
|
2014-09-05 23:31:15 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-02-03 18:56:08 +03:00
|
|
|
/* Create the imposter table or tables (if required). */
|
|
|
|
otaCreateImposterTable(p, pIter);
|
2015-02-04 19:32:47 +03:00
|
|
|
otaCreateImposterTable2(p, pIter);
|
2015-02-04 22:20:42 +03:00
|
|
|
zWrite = (pIter->eType==OTA_PK_VTAB ? "" : "ota_imp_");
|
2015-02-03 18:56:08 +03:00
|
|
|
|
2014-09-05 23:31:15 +04:00
|
|
|
/* Create the INSERT statement to write to the target PK b-tree */
|
|
|
|
if( p->rc==SQLITE_OK ){
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
|
2014-09-05 23:31:15 +04:00
|
|
|
sqlite3_mprintf(
|
2015-02-21 23:08:25 +03:00
|
|
|
"INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
|
2015-02-04 22:20:42 +03:00
|
|
|
zWrite, zTbl, zCollist, (bOtaRowid ? ", _rowid_" : ""), zBindings
|
2014-09-05 23:31:15 +04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2014-09-07 00:19:38 +04:00
|
|
|
|
|
|
|
/* Create the DELETE statement to write to the target PK b-tree */
|
|
|
|
if( p->rc==SQLITE_OK ){
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
|
2014-09-07 00:19:38 +04:00
|
|
|
sqlite3_mprintf(
|
2015-02-21 23:08:25 +03:00
|
|
|
"DELETE FROM \"%s%w\" WHERE %s", zWrite, zTbl, zWhere
|
2014-09-07 00:19:38 +04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-03-24 21:03:29 +03:00
|
|
|
if( pIter->abIndexed ){
|
2014-12-06 22:30:41 +03:00
|
|
|
const char *zOtaRowid = "";
|
|
|
|
if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
|
|
|
|
zOtaRowid = ", ota_rowid";
|
|
|
|
}
|
2014-11-21 13:46:23 +03:00
|
|
|
|
|
|
|
/* Create the ota_tmp_xxx table and the triggers to populate it. */
|
2015-02-21 23:08:25 +03:00
|
|
|
otaMPrintfExec(p, p->dbOta,
|
|
|
|
"CREATE TABLE IF NOT EXISTS 'ota_tmp_%q' AS "
|
|
|
|
"SELECT *%s FROM 'data_%q' WHERE 0;"
|
|
|
|
, zTbl, (pIter->eType==OTA_PK_EXTERNAL ? ", 0 AS ota_rowid" : "")
|
|
|
|
, zTbl
|
|
|
|
);
|
2014-11-21 13:46:23 +03:00
|
|
|
|
2015-02-21 23:08:25 +03:00
|
|
|
otaMPrintfExec(p, p->dbMain,
|
2015-02-04 22:20:42 +03:00
|
|
|
"CREATE TEMP TRIGGER ota_delete_tr BEFORE DELETE ON \"%s%w\" "
|
2014-09-08 21:50:35 +04:00
|
|
|
"BEGIN "
|
2015-02-21 23:08:25 +03:00
|
|
|
" SELECT ota_tmp_insert(2, %s);"
|
2014-09-08 21:50:35 +04:00
|
|
|
"END;"
|
2014-09-07 00:19:38 +04:00
|
|
|
|
2015-02-04 22:20:42 +03:00
|
|
|
"CREATE TEMP TRIGGER ota_update1_tr BEFORE UPDATE ON \"%s%w\" "
|
2014-11-21 13:46:23 +03:00
|
|
|
"BEGIN "
|
2015-02-21 23:08:25 +03:00
|
|
|
" SELECT ota_tmp_insert(2, %s);"
|
2014-11-21 13:46:23 +03:00
|
|
|
"END;"
|
2014-11-20 20:37:08 +03:00
|
|
|
|
2015-02-04 22:20:42 +03:00
|
|
|
"CREATE TEMP TRIGGER ota_update2_tr AFTER UPDATE ON \"%s%w\" "
|
2014-11-21 13:46:23 +03:00
|
|
|
"BEGIN "
|
2015-02-21 23:08:25 +03:00
|
|
|
" SELECT ota_tmp_insert(3, %s);"
|
|
|
|
"END;",
|
|
|
|
zWrite, zTbl, zOldlist,
|
|
|
|
zWrite, zTbl, zOldlist,
|
|
|
|
zWrite, zTbl, zNewlist
|
2014-11-21 13:46:23 +03:00
|
|
|
);
|
2015-02-21 23:08:25 +03:00
|
|
|
|
2014-12-06 22:30:41 +03:00
|
|
|
if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
|
2015-02-21 23:08:25 +03:00
|
|
|
otaMPrintfExec(p, p->dbMain,
|
2015-02-04 22:20:42 +03:00
|
|
|
"CREATE TEMP TRIGGER ota_insert_tr AFTER INSERT ON \"%s%w\" "
|
2014-11-21 13:46:23 +03:00
|
|
|
"BEGIN "
|
2015-02-21 23:08:25 +03:00
|
|
|
" SELECT ota_tmp_insert(0, %s);"
|
2015-02-04 22:20:42 +03:00
|
|
|
"END;",
|
2015-02-21 23:08:25 +03:00
|
|
|
zWrite, zTbl, zNewlist
|
2014-11-21 13:46:23 +03:00
|
|
|
);
|
|
|
|
}
|
2015-02-21 23:08:25 +03:00
|
|
|
|
|
|
|
otaObjIterPrepareTmpInsert(p, pIter, zCollist, zOtaRowid);
|
2014-11-21 13:46:23 +03:00
|
|
|
}
|
2014-11-20 20:37:08 +03:00
|
|
|
|
2014-09-08 21:50:35 +04:00
|
|
|
/* Allocate space required for the zMask field. */
|
2015-02-16 09:27:37 +03:00
|
|
|
pIter->zMask = (char*)otaMalloc(p, pIter->nTblCol+1);
|
2014-09-08 21:50:35 +04:00
|
|
|
|
2014-09-07 00:19:38 +04:00
|
|
|
sqlite3_free(zWhere);
|
|
|
|
sqlite3_free(zOldlist);
|
2014-09-08 21:50:35 +04:00
|
|
|
sqlite3_free(zNewlist);
|
2014-09-05 23:31:15 +04:00
|
|
|
sqlite3_free(zBindings);
|
|
|
|
}
|
|
|
|
sqlite3_free(zCollist);
|
|
|
|
sqlite3_free(zLimit);
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
2014-09-05 23:31:15 +04:00
|
|
|
|
|
|
|
return p->rc;
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
|
|
|
|
2015-02-19 22:59:35 +03:00
|
|
|
/*
|
|
|
|
** Set output variable *ppStmt to point to an UPDATE statement that may
|
|
|
|
** be used to update the imposter table for the main table b-tree of the
|
|
|
|
** table object that pIter currently points to, assuming that the
|
|
|
|
** ota_control column of the data_xyz table contains zMask.
|
|
|
|
*/
|
2014-09-08 21:50:35 +04:00
|
|
|
static int otaGetUpdateStmt(
|
2015-02-19 22:59:35 +03:00
|
|
|
sqlite3ota *p, /* OTA handle */
|
|
|
|
OtaObjIter *pIter, /* Object iterator */
|
|
|
|
const char *zMask, /* ota_control value ('x.x.') */
|
|
|
|
sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */
|
2014-09-08 21:50:35 +04:00
|
|
|
){
|
|
|
|
if( pIter->pUpdate && strcmp(zMask, pIter->zMask)==0 ){
|
|
|
|
*ppStmt = pIter->pUpdate;
|
|
|
|
}else{
|
|
|
|
char *zWhere = otaObjIterGetWhere(p, pIter);
|
|
|
|
char *zSet = otaObjIterGetSetlist(p, pIter, zMask);
|
|
|
|
char *zUpdate = 0;
|
|
|
|
sqlite3_finalize(pIter->pUpdate);
|
|
|
|
pIter->pUpdate = 0;
|
|
|
|
if( p->rc==SQLITE_OK ){
|
2015-02-04 22:20:42 +03:00
|
|
|
const char *zPrefix = "";
|
|
|
|
|
|
|
|
if( pIter->eType!=OTA_PK_VTAB ) zPrefix = "ota_imp_";
|
|
|
|
zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
|
|
|
|
zPrefix, pIter->zTbl, zSet, zWhere
|
2014-09-08 21:50:35 +04:00
|
|
|
);
|
|
|
|
p->rc = prepareFreeAndCollectError(
|
2015-02-21 23:08:25 +03:00
|
|
|
p->dbMain, &pIter->pUpdate, &p->zErrmsg, zUpdate
|
2014-09-08 21:50:35 +04:00
|
|
|
);
|
|
|
|
*ppStmt = pIter->pUpdate;
|
|
|
|
}
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
memcpy(pIter->zMask, zMask, pIter->nTblCol);
|
|
|
|
}
|
|
|
|
sqlite3_free(zWhere);
|
|
|
|
sqlite3_free(zSet);
|
|
|
|
}
|
|
|
|
return p->rc;
|
2014-09-07 00:19:38 +04:00
|
|
|
}
|
|
|
|
|
2015-02-21 23:08:25 +03:00
|
|
|
static sqlite3 *otaOpenDbhandle(sqlite3ota *p, const char *zName){
|
|
|
|
sqlite3 *db = 0;
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
const int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
|
|
|
|
p->rc = sqlite3_open_v2(zName, &db, flags, p->zVfsName);
|
|
|
|
if( p->rc ){
|
|
|
|
p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
|
|
|
|
sqlite3_close(db);
|
|
|
|
db = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
2014-10-21 23:35:03 +04:00
|
|
|
/*
|
|
|
|
** Open the database handle and attach the OTA database as "ota". If an
|
|
|
|
** error occurs, leave an error code and message in the OTA handle.
|
|
|
|
*/
|
2014-10-20 20:24:23 +04:00
|
|
|
static void otaOpenDatabase(sqlite3ota *p){
|
|
|
|
assert( p->rc==SQLITE_OK );
|
2015-02-21 23:08:25 +03:00
|
|
|
assert( p->dbMain==0 && p->dbOta==0 );
|
2014-10-20 20:24:23 +04:00
|
|
|
|
2015-02-14 21:58:22 +03:00
|
|
|
p->eStage = 0;
|
2015-02-21 23:08:25 +03:00
|
|
|
p->dbMain = otaOpenDbhandle(p, p->zTarget);
|
|
|
|
p->dbOta = otaOpenDbhandle(p, p->zOta);
|
2015-02-10 23:00:38 +03:00
|
|
|
|
2015-02-21 23:08:25 +03:00
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
p->rc = sqlite3_create_function(p->dbMain,
|
|
|
|
"ota_tmp_insert", -1, SQLITE_UTF8, (void*)p, otaTmpInsertFunc, 0, 0
|
|
|
|
);
|
|
|
|
}
|
2015-02-14 21:58:22 +03:00
|
|
|
|
2015-02-21 23:08:25 +03:00
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_OTA, (void*)p);
|
|
|
|
}
|
|
|
|
otaMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
|
|
|
|
|
|
|
|
/* Mark the database file just opened as an OTA target database. If
|
|
|
|
** this call returns SQLITE_NOTFOUND, then the OTA vfs is not in use.
|
|
|
|
** This is an error. */
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_OTA, (void*)p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( p->rc==SQLITE_NOTFOUND ){
|
|
|
|
p->rc = SQLITE_ERROR;
|
|
|
|
p->zErrmsg = sqlite3_mprintf("ota vfs not found");
|
2014-10-20 20:24:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
|
|
|
|
** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
|
|
|
|
**
|
|
|
|
** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
|
|
|
|
** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
|
|
|
|
** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
|
|
|
|
** three characters, then shorten the suffix on z[] to be the last three
|
|
|
|
** characters of the original suffix.
|
|
|
|
**
|
|
|
|
** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
|
|
|
|
** do the suffix shortening regardless of URI parameter.
|
|
|
|
**
|
|
|
|
** Examples:
|
|
|
|
**
|
|
|
|
** test.db-journal => test.nal
|
|
|
|
** test.db-wal => test.wal
|
|
|
|
** test.db-shm => test.shm
|
|
|
|
** test.db-mj7f3319fa => test.9fa
|
|
|
|
*/
|
|
|
|
static void otaFileSuffix3(const char *zBase, char *z){
|
|
|
|
#ifdef SQLITE_ENABLE_8_3_NAMES
|
|
|
|
#if SQLITE_ENABLE_8_3_NAMES<2
|
|
|
|
if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
int i, sz;
|
|
|
|
sz = sqlite3Strlen30(z);
|
|
|
|
for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
|
|
|
|
if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-02-14 21:58:22 +03:00
|
|
|
/*
|
|
|
|
** Return the current wal-index header checksum for the target database
|
|
|
|
** as a 64-bit integer.
|
|
|
|
**
|
|
|
|
** The checksum is store in the first page of xShmMap memory as an 8-byte
|
|
|
|
** blob starting at byte offset 40.
|
|
|
|
*/
|
|
|
|
static i64 otaShmChecksum(sqlite3ota *p){
|
|
|
|
i64 iRet;
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
sqlite3_file *pDb = p->pTargetFd->pReal;
|
|
|
|
u32 volatile *ptr;
|
|
|
|
p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
iRet = ((i64)ptr[10] << 32) + ptr[11];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return iRet;
|
|
|
|
}
|
|
|
|
|
2015-02-20 17:36:16 +03:00
|
|
|
/*
|
|
|
|
** This function is called as part of initializing or reinitializing an
|
|
|
|
** incremental checkpoint.
|
|
|
|
**
|
|
|
|
** It populates the sqlite3ota.aFrame[] array with the set of
|
|
|
|
** (wal frame -> db page) copy operations required to checkpoint the
|
|
|
|
** current wal file, and obtains the set of shm locks required to safely
|
|
|
|
** perform the copy operations directly on the file-system.
|
|
|
|
**
|
|
|
|
** If argument pState is not NULL, then the incremental checkpoint is
|
|
|
|
** being resumed. In this case, if the checksum of the wal-index-header
|
|
|
|
** following recovery is not the same as the checksum saved in the OtaState
|
|
|
|
** object, then the ota handle is set to DONE state. This occurs if some
|
|
|
|
** other client appends a transaction to the wal file in the middle of
|
|
|
|
** an incremental checkpoint.
|
|
|
|
*/
|
2015-02-14 21:58:22 +03:00
|
|
|
static void otaSetupCheckpoint(sqlite3ota *p, OtaState *pState){
|
|
|
|
|
2015-02-20 17:36:16 +03:00
|
|
|
/* If pState is NULL, then the wal file may not have been opened and
|
|
|
|
** recovered. Running a read-statement here to ensure that doing so
|
|
|
|
** does not interfere with the "capture" process below. */
|
2015-02-14 21:58:22 +03:00
|
|
|
if( pState==0 ){
|
|
|
|
p->eStage = 0;
|
|
|
|
if( p->rc==SQLITE_OK ){
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0);
|
2015-02-14 21:58:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 17:36:16 +03:00
|
|
|
/* Assuming no error has occurred, run a "restart" checkpoint with the
|
|
|
|
** sqlite3ota.eStage variable set to CAPTURE. This turns on the following
|
|
|
|
** special behaviour in the ota VFS:
|
|
|
|
**
|
|
|
|
** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
|
|
|
|
** the checkpoint fails with SQLITE_BUSY (normally SQLite would
|
|
|
|
** proceed with running a passive checkpoint instead of failing).
|
|
|
|
**
|
|
|
|
** * Attempts to read from the *-wal file or write to the database file
|
|
|
|
** do not perform any IO. Instead, the frame/page combinations that
|
|
|
|
** would be read/written are recorded in the sqlite3ota.aFrame[]
|
|
|
|
** array.
|
|
|
|
**
|
|
|
|
** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
|
|
|
|
** READ0 and CHECKPOINT locks taken as part of the checkpoint are
|
|
|
|
** no-ops. These locks will not be released until the connection
|
|
|
|
** is closed.
|
|
|
|
**
|
|
|
|
** * Attempting to xSync() the database file causes an SQLITE_INTERNAL
|
|
|
|
** error.
|
|
|
|
**
|
|
|
|
** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
|
|
|
|
** checkpoint below fails with SQLITE_INTERNAL, and leaves the aFrame[]
|
|
|
|
** array populated with a set of (frame -> page) mappings. Because the
|
|
|
|
** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy
|
|
|
|
** data from the wal file into the database file according to the
|
|
|
|
** contents of aFrame[].
|
|
|
|
*/
|
2015-02-14 21:58:22 +03:00
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
int rc2;
|
|
|
|
p->eStage = OTA_STAGE_CAPTURE;
|
2015-02-21 23:08:25 +03:00
|
|
|
rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
|
2015-02-14 21:58:22 +03:00
|
|
|
if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
p->eStage = OTA_STAGE_CKPT;
|
2015-02-20 17:36:16 +03:00
|
|
|
p->nStep = (pState ? pState->nRow : 0);
|
2015-02-14 21:58:22 +03:00
|
|
|
p->aBuf = otaMalloc(p, p->pgsz);
|
|
|
|
p->iWalCksum = otaShmChecksum(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( p->rc==SQLITE_OK && pState && pState->iWalCksum!=p->iWalCksum ){
|
|
|
|
p->rc = SQLITE_DONE;
|
|
|
|
p->eStage = OTA_STAGE_DONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 17:36:16 +03:00
|
|
|
/*
|
|
|
|
** Called when iAmt bytes are read from offset iOff of the wal file while
|
|
|
|
** the ota object is in capture mode. Record the frame number of the frame
|
|
|
|
** being read in the aFrame[] array.
|
|
|
|
*/
|
2015-02-14 21:58:22 +03:00
|
|
|
static int otaCaptureWalRead(sqlite3ota *pOta, i64 iOff, int iAmt){
|
|
|
|
const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
|
|
|
|
u32 iFrame;
|
|
|
|
|
|
|
|
if( pOta->mLock!=mReq ){
|
2015-02-18 23:16:15 +03:00
|
|
|
pOta->rc = SQLITE_BUSY;
|
|
|
|
return SQLITE_INTERNAL;
|
2015-02-14 21:58:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pOta->pgsz = iAmt;
|
|
|
|
if( pOta->nFrame==pOta->nFrameAlloc ){
|
|
|
|
int nNew = (pOta->nFrameAlloc ? pOta->nFrameAlloc : 64) * 2;
|
|
|
|
OtaFrame *aNew;
|
|
|
|
aNew = (OtaFrame*)sqlite3_realloc(pOta->aFrame, nNew * sizeof(OtaFrame));
|
|
|
|
if( aNew==0 ) return SQLITE_NOMEM;
|
|
|
|
pOta->aFrame = aNew;
|
|
|
|
pOta->nFrameAlloc = nNew;
|
|
|
|
}
|
|
|
|
|
|
|
|
iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
|
|
|
|
if( pOta->iMaxFrame<iFrame ) pOta->iMaxFrame = iFrame;
|
|
|
|
pOta->aFrame[pOta->nFrame].iWalFrame = iFrame;
|
|
|
|
pOta->aFrame[pOta->nFrame].iDbPage = 0;
|
|
|
|
pOta->nFrame++;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-20 17:36:16 +03:00
|
|
|
/*
|
|
|
|
** Called when a page of data is written to offset iOff of the database
|
|
|
|
** file while the ota handle is in capture mode. Record the page number
|
|
|
|
** of the page being written in the aFrame[] array.
|
|
|
|
*/
|
2015-02-14 21:58:22 +03:00
|
|
|
static int otaCaptureDbWrite(sqlite3ota *pOta, i64 iOff){
|
|
|
|
pOta->aFrame[pOta->nFrame-1].iDbPage = (u32)(iOff / pOta->pgsz) + 1;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-20 17:36:16 +03:00
|
|
|
/*
|
|
|
|
** This is called as part of an incremental checkpoint operation. Copy
|
|
|
|
** a single frame of data from the wal file into the database file, as
|
|
|
|
** indicated by the OtaFrame object.
|
|
|
|
*/
|
2015-02-14 21:58:22 +03:00
|
|
|
static void otaCheckpointFrame(sqlite3ota *p, OtaFrame *pFrame){
|
2015-02-18 23:16:15 +03:00
|
|
|
sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
|
|
|
|
sqlite3_file *pDb = p->pTargetFd->pReal;
|
|
|
|
i64 iOff;
|
2015-02-14 21:58:22 +03:00
|
|
|
|
2015-02-18 23:16:15 +03:00
|
|
|
assert( p->rc==SQLITE_OK );
|
|
|
|
iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
|
|
|
|
p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
|
|
|
|
if( p->rc ) return;
|
2015-02-14 21:58:22 +03:00
|
|
|
|
2015-02-18 23:16:15 +03:00
|
|
|
iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
|
|
|
|
p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
|
2015-02-14 21:58:22 +03:00
|
|
|
}
|
|
|
|
|
2015-02-18 20:40:05 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Take an EXCLUSIVE lock on the database file.
|
|
|
|
*/
|
|
|
|
static void otaLockDatabase(sqlite3ota *p){
|
2015-02-18 23:16:15 +03:00
|
|
|
sqlite3_file *pReal = p->pTargetFd->pReal;
|
|
|
|
assert( p->rc==SQLITE_OK );
|
|
|
|
p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED);
|
2015-02-18 20:40:05 +03:00
|
|
|
if( p->rc==SQLITE_OK ){
|
2015-02-18 23:16:15 +03:00
|
|
|
p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE);
|
2015-02-18 20:40:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-20 20:24:23 +04:00
|
|
|
/*
|
2014-10-21 23:35:03 +04:00
|
|
|
** The OTA handle is currently in OTA_STAGE_OAL state, with a SHARED lock
|
|
|
|
** on the database file. This proc moves the *-oal file to the *-wal path,
|
|
|
|
** then reopens the database file (this time in vanilla, non-oal, WAL mode).
|
|
|
|
** If an error occurs, leave an error code and error message in the ota
|
|
|
|
** handle.
|
2014-10-20 20:24:23 +04:00
|
|
|
*/
|
|
|
|
static void otaMoveOalFile(sqlite3ota *p){
|
2015-02-21 23:08:25 +03:00
|
|
|
const char *zBase = sqlite3_db_filename(p->dbMain, "main");
|
2014-10-20 20:24:23 +04:00
|
|
|
|
|
|
|
char *zWal = sqlite3_mprintf("%s-wal", zBase);
|
|
|
|
char *zOal = sqlite3_mprintf("%s-oal", zBase);
|
|
|
|
|
2015-02-18 20:40:05 +03:00
|
|
|
assert( p->eStage==OTA_STAGE_MOVE );
|
2014-10-20 20:24:23 +04:00
|
|
|
assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
|
|
|
|
if( zWal==0 || zOal==0 ){
|
|
|
|
p->rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
|
|
|
/* Move the *-oal file to *-wal. At this point connection p->db is
|
|
|
|
** holding a SHARED lock on the target database file (because it is
|
2015-02-18 20:40:05 +03:00
|
|
|
** in WAL mode). So no other connection may be writing the db.
|
|
|
|
**
|
|
|
|
** In order to ensure that there are no database readers, an EXCLUSIVE
|
|
|
|
** lock is obtained here before the *-oal is moved to *-wal.
|
|
|
|
*/
|
|
|
|
otaLockDatabase(p);
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
otaFileSuffix3(zBase, zWal);
|
|
|
|
otaFileSuffix3(zBase, zOal);
|
|
|
|
|
|
|
|
/* Re-open the databases. */
|
|
|
|
otaObjIterFinalize(&p->objiter);
|
2015-02-21 23:08:25 +03:00
|
|
|
sqlite3_close(p->dbMain);
|
|
|
|
sqlite3_close(p->dbOta);
|
2015-04-16 13:55:58 +03:00
|
|
|
p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
p->dbMain = 0;
|
|
|
|
p->dbOta = 0;
|
|
|
|
otaOpenDatabase(p);
|
|
|
|
otaSetupCheckpoint(p, 0);
|
|
|
|
}
|
2015-02-18 20:40:05 +03:00
|
|
|
}
|
2014-10-20 20:24:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
sqlite3_free(zWal);
|
|
|
|
sqlite3_free(zOal);
|
|
|
|
}
|
|
|
|
|
2014-09-07 00:19:38 +04:00
|
|
|
/*
|
|
|
|
** The SELECT statement iterating through the keys for the current object
|
|
|
|
** (p->objiter.pSelect) currently points to a valid row. This function
|
|
|
|
** determines the type of operation requested by this row and returns
|
|
|
|
** one of the following values to indicate the result:
|
|
|
|
**
|
|
|
|
** * OTA_INSERT
|
|
|
|
** * OTA_DELETE
|
|
|
|
** * OTA_IDX_DELETE
|
|
|
|
** * OTA_UPDATE
|
|
|
|
**
|
|
|
|
** If OTA_UPDATE is returned, then output variable *pzMask is set to
|
|
|
|
** point to the text value indicating the columns to update.
|
|
|
|
**
|
|
|
|
** If the ota_control field contains an invalid value, an error code and
|
|
|
|
** message are left in the OTA handle and zero returned.
|
|
|
|
*/
|
|
|
|
static int otaStepType(sqlite3ota *p, const char **pzMask){
|
|
|
|
int iCol = p->objiter.nCol; /* Index of ota_control column */
|
|
|
|
int res = 0; /* Return value */
|
|
|
|
|
|
|
|
switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
|
|
|
|
case SQLITE_INTEGER: {
|
|
|
|
int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
|
|
|
|
if( iVal==0 ){
|
|
|
|
res = OTA_INSERT;
|
|
|
|
}else if( iVal==1 ){
|
|
|
|
res = OTA_DELETE;
|
|
|
|
}else if( iVal==2 ){
|
|
|
|
res = OTA_IDX_DELETE;
|
2014-09-08 21:50:35 +04:00
|
|
|
}else if( iVal==3 ){
|
|
|
|
res = OTA_IDX_INSERT;
|
2014-09-07 00:19:38 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-02-17 00:13:19 +03:00
|
|
|
case SQLITE_TEXT: {
|
|
|
|
const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
|
|
|
|
if( z==0 ){
|
|
|
|
p->rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
|
|
|
*pzMask = (const char*)z;
|
|
|
|
}
|
2014-09-07 00:19:38 +04:00
|
|
|
res = OTA_UPDATE;
|
2015-02-17 00:13:19 +03:00
|
|
|
|
2014-09-07 00:19:38 +04:00
|
|
|
break;
|
2015-02-17 00:13:19 +03:00
|
|
|
}
|
2014-09-07 00:19:38 +04:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( res==0 ){
|
|
|
|
otaBadControlError(p);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-12-06 22:30:41 +03:00
|
|
|
#ifdef SQLITE_DEBUG
|
2015-02-20 17:36:16 +03:00
|
|
|
/*
|
|
|
|
** Assert that column iCol of statement pStmt is named zName.
|
|
|
|
*/
|
2014-12-06 22:30:41 +03:00
|
|
|
static void assertColumnName(sqlite3_stmt *pStmt, int iCol, const char *zName){
|
|
|
|
const char *zCol = sqlite3_column_name(pStmt, iCol);
|
|
|
|
assert( 0==sqlite3_stricmp(zName, zCol) );
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define assertColumnName(x,y,z)
|
|
|
|
#endif
|
|
|
|
|
2014-09-05 23:31:15 +04:00
|
|
|
/*
|
|
|
|
** This function does the work for an sqlite3ota_step() call.
|
|
|
|
**
|
|
|
|
** The object-iterator (p->objiter) currently points to a valid object,
|
|
|
|
** and the input cursor (p->objiter.pSelect) currently points to a valid
|
|
|
|
** input row. Perform whatever processing is required and return.
|
|
|
|
**
|
|
|
|
** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
|
|
|
|
** and message is left in the OTA handle and a copy of the error code
|
|
|
|
** returned.
|
2014-09-02 23:59:40 +04:00
|
|
|
*/
|
2014-09-05 23:31:15 +04:00
|
|
|
static int otaStep(sqlite3ota *p){
|
|
|
|
OtaObjIter *pIter = &p->objiter;
|
2014-09-07 00:19:38 +04:00
|
|
|
const char *zMask = 0;
|
2014-09-05 23:31:15 +04:00
|
|
|
int i;
|
2014-09-07 00:19:38 +04:00
|
|
|
int eType = otaStepType(p, &zMask);
|
|
|
|
|
|
|
|
if( eType ){
|
|
|
|
assert( eType!=OTA_UPDATE || pIter->zIdx==0 );
|
2014-09-02 23:59:40 +04:00
|
|
|
|
2014-09-07 00:19:38 +04:00
|
|
|
if( pIter->zIdx==0 && eType==OTA_IDX_DELETE ){
|
|
|
|
otaBadControlError(p);
|
|
|
|
}
|
2014-09-08 21:50:35 +04:00
|
|
|
else if(
|
|
|
|
eType==OTA_INSERT
|
|
|
|
|| eType==OTA_DELETE
|
|
|
|
|| eType==OTA_IDX_DELETE
|
|
|
|
|| eType==OTA_IDX_INSERT
|
|
|
|
){
|
2014-11-21 13:46:23 +03:00
|
|
|
sqlite3_value *pVal;
|
2014-09-07 00:19:38 +04:00
|
|
|
sqlite3_stmt *pWriter;
|
2014-09-08 21:50:35 +04:00
|
|
|
|
2014-09-07 00:19:38 +04:00
|
|
|
assert( eType!=OTA_UPDATE );
|
2014-09-08 21:50:35 +04:00
|
|
|
assert( eType!=OTA_DELETE || pIter->zIdx==0 );
|
|
|
|
|
|
|
|
if( eType==OTA_IDX_DELETE || eType==OTA_DELETE ){
|
|
|
|
pWriter = pIter->pDelete;
|
|
|
|
}else{
|
|
|
|
pWriter = pIter->pInsert;
|
|
|
|
}
|
2014-09-07 00:19:38 +04:00
|
|
|
|
|
|
|
for(i=0; i<pIter->nCol; i++){
|
2015-02-03 18:56:08 +03:00
|
|
|
/* If this is an INSERT into a table b-tree and the table has an
|
|
|
|
** explicit INTEGER PRIMARY KEY, check that this is not an attempt
|
|
|
|
** to write a NULL into the IPK column. That is not permitted. */
|
|
|
|
if( eType==OTA_INSERT
|
|
|
|
&& pIter->zIdx==0 && pIter->eType==OTA_PK_IPK && pIter->abTblPk[i]
|
|
|
|
&& sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
|
|
|
|
){
|
|
|
|
p->rc = SQLITE_MISMATCH;
|
|
|
|
p->zErrmsg = sqlite3_mprintf("datatype mismatch");
|
|
|
|
goto step_out;
|
|
|
|
}
|
|
|
|
|
2015-02-18 23:16:15 +03:00
|
|
|
if( eType==OTA_DELETE && pIter->abTblPk[i]==0 ){
|
2014-09-08 21:50:35 +04:00
|
|
|
continue;
|
|
|
|
}
|
2015-02-03 18:56:08 +03:00
|
|
|
|
2014-09-18 19:57:22 +04:00
|
|
|
pVal = sqlite3_column_value(pIter->pSelect, i);
|
2015-02-17 00:13:19 +03:00
|
|
|
p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
|
|
|
|
if( p->rc ) goto step_out;
|
2014-09-07 00:19:38 +04:00
|
|
|
}
|
2014-12-06 22:30:41 +03:00
|
|
|
if( pIter->zIdx==0
|
|
|
|
&& (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
|
|
|
|
){
|
|
|
|
/* For a virtual table, or a table with no primary key, the
|
|
|
|
** SELECT statement is:
|
2014-11-21 13:46:23 +03:00
|
|
|
**
|
|
|
|
** SELECT <cols>, ota_control, ota_rowid FROM ....
|
|
|
|
**
|
|
|
|
** Hence column_value(pIter->nCol+1).
|
|
|
|
*/
|
2014-12-06 22:30:41 +03:00
|
|
|
assertColumnName(pIter->pSelect, pIter->nCol+1, "ota_rowid");
|
2014-11-21 13:46:23 +03:00
|
|
|
pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
|
2015-02-17 00:13:19 +03:00
|
|
|
p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
|
|
|
|
}
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
sqlite3_step(pWriter);
|
|
|
|
p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
|
2014-11-21 13:46:23 +03:00
|
|
|
}
|
2015-02-18 23:16:15 +03:00
|
|
|
}else{
|
2014-11-21 13:46:23 +03:00
|
|
|
sqlite3_value *pVal;
|
2014-09-08 21:50:35 +04:00
|
|
|
sqlite3_stmt *pUpdate = 0;
|
2015-02-18 23:16:15 +03:00
|
|
|
assert( eType==OTA_UPDATE );
|
2014-09-08 21:50:35 +04:00
|
|
|
otaGetUpdateStmt(p, pIter, zMask, &pUpdate);
|
|
|
|
if( pUpdate ){
|
2015-01-31 23:42:04 +03:00
|
|
|
for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
|
2015-02-17 00:13:19 +03:00
|
|
|
char c = zMask[pIter->aiSrcOrder[i]];
|
2014-11-21 13:46:23 +03:00
|
|
|
pVal = sqlite3_column_value(pIter->pSelect, i);
|
2015-02-17 00:13:19 +03:00
|
|
|
if( pIter->abTblPk[i] || c=='x' || c=='d' ){
|
|
|
|
p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
|
|
|
|
}
|
2014-09-07 00:19:38 +04:00
|
|
|
}
|
2015-02-17 00:13:19 +03:00
|
|
|
if( p->rc==SQLITE_OK
|
|
|
|
&& (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
|
|
|
|
){
|
2014-12-06 22:30:41 +03:00
|
|
|
/* Bind the ota_rowid value to column _rowid_ */
|
|
|
|
assertColumnName(pIter->pSelect, pIter->nCol+1, "ota_rowid");
|
2014-11-21 13:46:23 +03:00
|
|
|
pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
|
2015-02-17 00:13:19 +03:00
|
|
|
p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
|
|
|
|
}
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
sqlite3_step(pUpdate);
|
|
|
|
p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
|
2014-11-21 13:46:23 +03:00
|
|
|
}
|
2014-09-07 00:19:38 +04:00
|
|
|
}
|
|
|
|
}
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
2014-09-05 23:31:15 +04:00
|
|
|
|
2015-02-03 18:56:08 +03:00
|
|
|
step_out:
|
2014-09-02 23:59:40 +04:00
|
|
|
return p->rc;
|
|
|
|
}
|
|
|
|
|
2014-10-20 20:24:23 +04:00
|
|
|
/*
|
2015-02-21 23:08:25 +03:00
|
|
|
** Increment the schema cookie of the main database opened by p->dbMain.
|
2014-10-20 20:24:23 +04:00
|
|
|
*/
|
|
|
|
static void otaIncrSchemaCookie(sqlite3ota *p){
|
|
|
|
if( p->rc==SQLITE_OK ){
|
2015-02-18 20:40:05 +03:00
|
|
|
int iCookie = 1000000;
|
|
|
|
sqlite3_stmt *pStmt;
|
|
|
|
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
|
2015-02-18 20:40:05 +03:00
|
|
|
"PRAGMA schema_version"
|
|
|
|
);
|
|
|
|
if( p->rc==SQLITE_OK ){
|
2015-02-19 16:36:02 +03:00
|
|
|
/* Coverage: it may be that this sqlite3_step() cannot fail. There
|
|
|
|
** is already a transaction open, so the prepared statement cannot
|
|
|
|
** throw an SQLITE_SCHEMA exception. The only database page the
|
|
|
|
** statement reads is page 1, which is guaranteed to be in the cache.
|
|
|
|
** And no memory allocations are required. */
|
2015-02-18 20:40:05 +03:00
|
|
|
if( SQLITE_ROW==sqlite3_step(pStmt) ){
|
|
|
|
iCookie = sqlite3_column_int(pStmt, 0);
|
|
|
|
}
|
2015-02-19 22:59:35 +03:00
|
|
|
otaFinalize(p, pStmt);
|
2015-02-18 20:40:05 +03:00
|
|
|
}
|
|
|
|
if( p->rc==SQLITE_OK ){
|
2015-02-21 23:08:25 +03:00
|
|
|
otaMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
|
2014-10-20 20:24:23 +04:00
|
|
|
}
|
|
|
|
}
|
2015-02-18 20:40:05 +03:00
|
|
|
}
|
|
|
|
|
2015-02-20 17:36:16 +03:00
|
|
|
/*
|
|
|
|
** Update the contents of the ota_state table within the ota database. The
|
|
|
|
** value stored in the OTA_STATE_STAGE column is eStage. All other values
|
|
|
|
** are determined by inspecting the ota handle passed as the first argument.
|
|
|
|
*/
|
2015-02-18 20:40:05 +03:00
|
|
|
static void otaSaveState(sqlite3ota *p, int eStage){
|
|
|
|
if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
|
|
|
|
sqlite3_stmt *pInsert = 0;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert( p->zErrmsg==0 );
|
2015-02-21 23:08:25 +03:00
|
|
|
rc = prepareFreeAndCollectError(p->dbOta, &pInsert, &p->zErrmsg,
|
2015-02-18 20:40:05 +03:00
|
|
|
sqlite3_mprintf(
|
2015-02-21 23:08:25 +03:00
|
|
|
"INSERT OR REPLACE INTO ota_state(k, v) VALUES "
|
2015-02-18 20:40:05 +03:00
|
|
|
"(%d, %d), "
|
|
|
|
"(%d, %Q), "
|
|
|
|
"(%d, %Q), "
|
|
|
|
"(%d, %d), "
|
|
|
|
"(%d, %lld), "
|
|
|
|
"(%d, %lld), "
|
2015-02-21 23:08:25 +03:00
|
|
|
"(%d, %lld), "
|
2015-02-18 20:40:05 +03:00
|
|
|
"(%d, %lld) ",
|
|
|
|
OTA_STATE_STAGE, eStage,
|
|
|
|
OTA_STATE_TBL, p->objiter.zTbl,
|
|
|
|
OTA_STATE_IDX, p->objiter.zIdx,
|
|
|
|
OTA_STATE_ROW, p->nStep,
|
|
|
|
OTA_STATE_PROGRESS, p->nProgress,
|
|
|
|
OTA_STATE_CKPT, p->iWalCksum,
|
2015-02-21 23:08:25 +03:00
|
|
|
OTA_STATE_COOKIE, (i64)p->pTargetFd->iCookie,
|
|
|
|
OTA_STATE_OALSZ, p->iOalSz
|
|
|
|
)
|
2015-02-19 22:59:35 +03:00
|
|
|
);
|
2015-02-18 20:40:05 +03:00
|
|
|
assert( pInsert==0 || rc==SQLITE_OK );
|
|
|
|
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
sqlite3_step(pInsert);
|
|
|
|
rc = sqlite3_finalize(pInsert);
|
|
|
|
}
|
2015-02-19 22:59:35 +03:00
|
|
|
if( rc!=SQLITE_OK ) p->rc = rc;
|
2014-10-20 20:24:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 20:40:05 +03:00
|
|
|
|
2014-09-05 23:31:15 +04:00
|
|
|
/*
|
|
|
|
** Step the OTA object.
|
|
|
|
*/
|
2014-09-02 23:59:40 +04:00
|
|
|
int sqlite3ota_step(sqlite3ota *p){
|
|
|
|
if( p ){
|
2014-10-20 20:24:23 +04:00
|
|
|
switch( p->eStage ){
|
|
|
|
case OTA_STAGE_OAL: {
|
|
|
|
OtaObjIter *pIter = &p->objiter;
|
2015-02-18 23:16:15 +03:00
|
|
|
while( p->rc==SQLITE_OK && pIter->zTbl ){
|
2014-10-20 20:24:23 +04:00
|
|
|
|
|
|
|
if( pIter->bCleanup ){
|
|
|
|
/* Clean up the ota_tmp_xxx table for the previous table. It
|
|
|
|
** cannot be dropped as there are currently active SQL statements.
|
|
|
|
** But the contents can be deleted. */
|
2015-03-24 21:03:29 +03:00
|
|
|
if( pIter->abIndexed ){
|
2015-02-21 23:08:25 +03:00
|
|
|
const char *zTbl = pIter->zTbl;
|
|
|
|
otaMPrintfExec(p, p->dbOta, "DELETE FROM 'ota_tmp_%q'", zTbl);
|
2014-11-21 13:46:23 +03:00
|
|
|
}
|
2014-10-20 20:24:23 +04:00
|
|
|
}else{
|
|
|
|
otaObjIterPrepareAll(p, pIter, 0);
|
|
|
|
|
|
|
|
/* Advance to the next row to process. */
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
int rc = sqlite3_step(pIter->pSelect);
|
|
|
|
if( rc==SQLITE_ROW ){
|
|
|
|
p->nProgress++;
|
|
|
|
p->nStep++;
|
|
|
|
return otaStep(p);
|
|
|
|
}
|
|
|
|
p->rc = sqlite3_reset(pIter->pSelect);
|
|
|
|
p->nStep = 0;
|
|
|
|
}
|
|
|
|
}
|
2014-09-02 23:59:40 +04:00
|
|
|
|
2014-10-20 20:24:23 +04:00
|
|
|
otaObjIterNext(p, pIter);
|
|
|
|
}
|
|
|
|
|
2015-02-18 23:16:15 +03:00
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
assert( pIter->zTbl==0 );
|
2015-02-18 20:40:05 +03:00
|
|
|
otaSaveState(p, OTA_STAGE_MOVE);
|
2014-10-20 20:24:23 +04:00
|
|
|
otaIncrSchemaCookie(p);
|
|
|
|
if( p->rc==SQLITE_OK ){
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
|
|
|
|
}
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
p->rc = sqlite3_exec(p->dbOta, "COMMIT", 0, 0, &p->zErrmsg);
|
2014-10-20 20:24:23 +04:00
|
|
|
}
|
2015-02-18 20:40:05 +03:00
|
|
|
p->eStage = OTA_STAGE_MOVE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OTA_STAGE_MOVE: {
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
otaMoveOalFile(p);
|
|
|
|
p->nProgress++;
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
2014-10-20 20:24:23 +04:00
|
|
|
break;
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
|
|
|
|
2014-10-20 20:24:23 +04:00
|
|
|
case OTA_STAGE_CKPT: {
|
2015-02-18 23:16:15 +03:00
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
if( p->nStep>=p->nFrame ){
|
|
|
|
sqlite3_file *pDb = p->pTargetFd->pReal;
|
|
|
|
|
|
|
|
/* Sync the db file */
|
|
|
|
p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
|
|
|
|
|
|
|
|
/* Update nBackfill */
|
2014-10-20 20:24:23 +04:00
|
|
|
if( p->rc==SQLITE_OK ){
|
2015-02-18 23:16:15 +03:00
|
|
|
void volatile *ptr;
|
|
|
|
p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
((u32*)ptr)[12] = p->iMaxFrame;
|
|
|
|
}
|
2014-10-20 20:24:23 +04:00
|
|
|
}
|
2015-02-18 23:16:15 +03:00
|
|
|
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
p->eStage = OTA_STAGE_DONE;
|
|
|
|
p->rc = SQLITE_DONE;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
OtaFrame *pFrame = &p->aFrame[p->nStep];
|
|
|
|
otaCheckpointFrame(p, pFrame);
|
|
|
|
p->nStep++;
|
2014-10-20 20:24:23 +04:00
|
|
|
}
|
2015-02-18 23:16:15 +03:00
|
|
|
p->nProgress++;
|
2014-10-20 20:24:23 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-09-02 23:59:40 +04:00
|
|
|
|
2014-10-20 20:24:23 +04:00
|
|
|
default:
|
|
|
|
break;
|
2014-09-05 23:31:15 +04:00
|
|
|
}
|
2014-10-22 19:33:12 +04:00
|
|
|
return p->rc;
|
|
|
|
}else{
|
|
|
|
return SQLITE_NOMEM;
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 17:36:16 +03:00
|
|
|
/*
|
|
|
|
** Free an OtaState object allocated by otaLoadState().
|
|
|
|
*/
|
2014-10-20 20:24:23 +04:00
|
|
|
static void otaFreeState(OtaState *p){
|
2014-10-22 19:33:12 +04:00
|
|
|
if( p ){
|
|
|
|
sqlite3_free(p->zTbl);
|
|
|
|
sqlite3_free(p->zIdx);
|
|
|
|
sqlite3_free(p);
|
|
|
|
}
|
2014-09-03 23:30:32 +04:00
|
|
|
}
|
2014-09-02 23:59:40 +04:00
|
|
|
|
2014-09-03 23:30:32 +04:00
|
|
|
/*
|
|
|
|
** Allocate an OtaState object and load the contents of the ota_state
|
|
|
|
** table into it. Return a pointer to the new object. It is the
|
|
|
|
** responsibility of the caller to eventually free the object using
|
|
|
|
** sqlite3_free().
|
|
|
|
**
|
|
|
|
** If an error occurs, leave an error code and message in the ota handle
|
|
|
|
** and return NULL.
|
|
|
|
*/
|
|
|
|
static OtaState *otaLoadState(sqlite3ota *p){
|
2015-02-21 23:08:25 +03:00
|
|
|
const char *zSelect = "SELECT k, v FROM ota_state";
|
2014-09-03 23:30:32 +04:00
|
|
|
OtaState *pRet = 0;
|
2015-02-10 23:00:38 +03:00
|
|
|
sqlite3_stmt *pStmt = 0;
|
2014-09-03 23:30:32 +04:00
|
|
|
int rc;
|
2014-10-20 20:24:23 +04:00
|
|
|
int rc2;
|
2014-09-03 23:30:32 +04:00
|
|
|
|
2015-02-16 09:27:37 +03:00
|
|
|
pRet = (OtaState*)otaMalloc(p, sizeof(OtaState));
|
|
|
|
if( pRet==0 ) return 0;
|
2014-10-20 20:24:23 +04:00
|
|
|
|
2015-02-21 23:08:25 +03:00
|
|
|
rc = prepareAndCollectError(p->dbOta, &pStmt, &p->zErrmsg, zSelect);
|
2014-10-20 20:24:23 +04:00
|
|
|
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
|
|
|
|
switch( sqlite3_column_int(pStmt, 0) ){
|
|
|
|
case OTA_STATE_STAGE:
|
|
|
|
pRet->eStage = sqlite3_column_int(pStmt, 1);
|
|
|
|
if( pRet->eStage!=OTA_STAGE_OAL
|
2015-02-18 20:40:05 +03:00
|
|
|
&& pRet->eStage!=OTA_STAGE_MOVE
|
2014-10-20 20:24:23 +04:00
|
|
|
&& pRet->eStage!=OTA_STAGE_CKPT
|
|
|
|
){
|
|
|
|
p->rc = SQLITE_CORRUPT;
|
2014-09-03 23:30:32 +04:00
|
|
|
}
|
2014-10-20 20:24:23 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OTA_STATE_TBL:
|
2015-02-16 09:27:37 +03:00
|
|
|
pRet->zTbl = otaStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
|
2014-10-20 20:24:23 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OTA_STATE_IDX:
|
2015-02-16 09:27:37 +03:00
|
|
|
pRet->zIdx = otaStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
|
2014-10-20 20:24:23 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OTA_STATE_ROW:
|
|
|
|
pRet->nRow = sqlite3_column_int(pStmt, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OTA_STATE_PROGRESS:
|
|
|
|
pRet->nProgress = sqlite3_column_int64(pStmt, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OTA_STATE_CKPT:
|
2015-02-14 21:58:22 +03:00
|
|
|
pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
|
2014-10-20 20:24:23 +04:00
|
|
|
break;
|
|
|
|
|
2015-02-07 22:17:36 +03:00
|
|
|
case OTA_STATE_COOKIE:
|
2015-02-16 14:48:34 +03:00
|
|
|
pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
|
2015-02-07 22:17:36 +03:00
|
|
|
break;
|
|
|
|
|
2015-02-21 23:08:25 +03:00
|
|
|
case OTA_STATE_OALSZ:
|
|
|
|
pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
|
|
|
|
break;
|
|
|
|
|
2014-10-20 20:24:23 +04:00
|
|
|
default:
|
|
|
|
rc = SQLITE_CORRUPT;
|
|
|
|
break;
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
|
|
|
}
|
2014-10-20 20:24:23 +04:00
|
|
|
rc2 = sqlite3_finalize(pStmt);
|
|
|
|
if( rc==SQLITE_OK ) rc = rc2;
|
2014-09-03 23:30:32 +04:00
|
|
|
|
2014-09-02 23:59:40 +04:00
|
|
|
p->rc = rc;
|
2014-09-03 23:30:32 +04:00
|
|
|
return pRet;
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
|
|
|
|
2015-02-20 17:36:16 +03:00
|
|
|
/*
|
|
|
|
** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
|
|
|
|
** otherwise. Either or both argument may be NULL. Two NULL values are
|
|
|
|
** considered equal, and NULL is considered distinct from all other values.
|
|
|
|
*/
|
2014-09-05 23:31:15 +04:00
|
|
|
static int otaStrCompare(const char *z1, const char *z2){
|
|
|
|
if( z1==0 && z2==0 ) return 0;
|
|
|
|
if( z1==0 || z2==0 ) return 1;
|
|
|
|
return (sqlite3_stricmp(z1, z2)!=0);
|
|
|
|
}
|
|
|
|
|
2015-02-20 17:36:16 +03:00
|
|
|
/*
|
|
|
|
** This function is called as part of sqlite3ota_open() when initializing
|
|
|
|
** an ota handle in OAL stage. If the ota update has not started (i.e.
|
|
|
|
** the ota_state table was empty) it is a no-op. Otherwise, it arranges
|
|
|
|
** things so that the next call to sqlite3ota_step() continues on from
|
|
|
|
** where the previous ota handle left off.
|
|
|
|
**
|
|
|
|
** If an error occurs, an error code and error message are left in the
|
|
|
|
** ota handle passed as the first argument.
|
|
|
|
*/
|
|
|
|
static void otaSetupOal(sqlite3ota *p, OtaState *pState){
|
2014-09-03 23:30:32 +04:00
|
|
|
assert( p->rc==SQLITE_OK );
|
|
|
|
if( pState->zTbl ){
|
2014-09-05 23:31:15 +04:00
|
|
|
OtaObjIter *pIter = &p->objiter;
|
2014-09-17 23:05:46 +04:00
|
|
|
int rc = SQLITE_OK;
|
2014-09-05 23:31:15 +04:00
|
|
|
|
|
|
|
while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
|
|
|
|
|| otaStrCompare(pIter->zIdx, pState->zIdx)
|
2015-02-18 23:16:15 +03:00
|
|
|
|| otaStrCompare(pIter->zTbl, pState->zTbl)
|
2014-09-05 23:31:15 +04:00
|
|
|
)){
|
2015-02-18 23:16:15 +03:00
|
|
|
rc = otaObjIterNext(p, pIter);
|
2014-09-03 23:30:32 +04:00
|
|
|
}
|
2014-09-05 23:31:15 +04:00
|
|
|
|
2015-02-18 23:16:15 +03:00
|
|
|
if( rc==SQLITE_OK && !pIter->zTbl ){
|
2014-09-03 23:30:32 +04:00
|
|
|
rc = SQLITE_ERROR;
|
|
|
|
p->zErrmsg = sqlite3_mprintf("ota_state mismatch error");
|
|
|
|
}
|
2014-09-02 23:59:40 +04:00
|
|
|
|
2014-09-03 23:30:32 +04:00
|
|
|
if( rc==SQLITE_OK ){
|
2014-09-05 23:31:15 +04:00
|
|
|
p->nStep = pState->nRow;
|
|
|
|
rc = otaObjIterPrepareAll(p, &p->objiter, p->nStep);
|
2014-09-03 23:30:32 +04:00
|
|
|
}
|
2014-09-02 23:59:40 +04:00
|
|
|
|
2014-09-03 23:30:32 +04:00
|
|
|
p->rc = rc;
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-03 23:30:32 +04:00
|
|
|
/*
|
|
|
|
** If there is a "*-oal" file in the file-system corresponding to the
|
|
|
|
** target database in the file-system, delete it. If an error occurs,
|
|
|
|
** leave an error code and error message in the ota handle.
|
|
|
|
*/
|
|
|
|
static void otaDeleteOalFile(sqlite3ota *p){
|
|
|
|
char *zOal = sqlite3_mprintf("%s-oal", p->zTarget);
|
|
|
|
assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
|
|
|
|
unlink(zOal);
|
|
|
|
sqlite3_free(zOal);
|
|
|
|
}
|
2014-09-02 23:59:40 +04:00
|
|
|
|
2015-02-20 17:36:16 +03:00
|
|
|
/*
|
|
|
|
** Allocate a private ota VFS for the ota handle passed as the only
|
|
|
|
** argument. This VFS will be used unless the call to sqlite3ota_open()
|
|
|
|
** specified a URI with a vfs=? option in place of a target database
|
|
|
|
** file name.
|
|
|
|
*/
|
2015-02-16 14:48:34 +03:00
|
|
|
static void otaCreateVfs(sqlite3ota *p){
|
|
|
|
int rnd;
|
|
|
|
char zRnd[64];
|
|
|
|
|
|
|
|
assert( p->rc==SQLITE_OK );
|
|
|
|
sqlite3_randomness(sizeof(int), (void*)&rnd);
|
|
|
|
sprintf(zRnd, "ota_vfs_%d", rnd);
|
|
|
|
p->rc = sqlite3ota_create_vfs(zRnd, 0);
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
|
|
|
|
assert( pVfs );
|
|
|
|
p->zVfsName = pVfs->zName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 17:36:16 +03:00
|
|
|
/*
|
|
|
|
** Destroy the private VFS created for the ota handle passed as the only
|
|
|
|
** argument by an earlier call to otaCreateVfs().
|
|
|
|
*/
|
2015-02-16 14:48:34 +03:00
|
|
|
static void otaDeleteVfs(sqlite3ota *p){
|
|
|
|
if( p->zVfsName ){
|
|
|
|
sqlite3ota_destroy_vfs(p->zVfsName);
|
|
|
|
p->zVfsName = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-02 23:59:40 +04:00
|
|
|
/*
|
|
|
|
** Open and return a new OTA handle.
|
|
|
|
*/
|
|
|
|
sqlite3ota *sqlite3ota_open(const char *zTarget, const char *zOta){
|
|
|
|
sqlite3ota *p;
|
2014-09-03 23:30:32 +04:00
|
|
|
int nTarget = strlen(zTarget);
|
2014-10-20 20:24:23 +04:00
|
|
|
int nOta = strlen(zOta);
|
2014-09-02 23:59:40 +04:00
|
|
|
|
2014-10-20 20:24:23 +04:00
|
|
|
p = (sqlite3ota*)sqlite3_malloc(sizeof(sqlite3ota)+nTarget+1+nOta+1);
|
2014-09-02 23:59:40 +04:00
|
|
|
if( p ){
|
2014-09-03 23:30:32 +04:00
|
|
|
OtaState *pState = 0;
|
2014-09-02 23:59:40 +04:00
|
|
|
|
2015-02-14 21:58:22 +03:00
|
|
|
/* Create the custom VFS. */
|
2014-09-02 23:59:40 +04:00
|
|
|
memset(p, 0, sizeof(sqlite3ota));
|
2015-02-16 14:48:34 +03:00
|
|
|
otaCreateVfs(p);
|
2015-02-07 22:17:36 +03:00
|
|
|
|
|
|
|
/* Open the target database */
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
p->zTarget = (char*)&p[1];
|
|
|
|
memcpy(p->zTarget, zTarget, nTarget+1);
|
|
|
|
p->zOta = &p->zTarget[nTarget+1];
|
|
|
|
memcpy(p->zOta, zOta, nOta+1);
|
|
|
|
otaOpenDatabase(p);
|
|
|
|
}
|
2014-09-02 23:59:40 +04:00
|
|
|
|
|
|
|
/* If it has not already been created, create the ota_state table */
|
|
|
|
if( p->rc==SQLITE_OK ){
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = sqlite3_exec(p->dbOta, OTA_CREATE_STATE, 0, 0, &p->zErrmsg);
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if( p->rc==SQLITE_OK ){
|
2014-09-03 23:30:32 +04:00
|
|
|
pState = otaLoadState(p);
|
2014-10-20 20:24:23 +04:00
|
|
|
assert( pState || p->rc!=SQLITE_OK );
|
2014-10-22 19:33:12 +04:00
|
|
|
if( p->rc==SQLITE_OK ){
|
2015-02-16 14:48:34 +03:00
|
|
|
|
2014-10-20 20:24:23 +04:00
|
|
|
if( pState->eStage==0 ){
|
|
|
|
otaDeleteOalFile(p);
|
2015-02-14 21:58:22 +03:00
|
|
|
p->eStage = OTA_STAGE_OAL;
|
2014-10-20 20:24:23 +04:00
|
|
|
}else{
|
|
|
|
p->eStage = pState->eStage;
|
|
|
|
}
|
|
|
|
p->nProgress = pState->nProgress;
|
2015-02-21 23:08:25 +03:00
|
|
|
p->iOalSz = pState->iOalSz;
|
2014-09-03 23:30:32 +04:00
|
|
|
}
|
|
|
|
}
|
2014-10-20 20:24:23 +04:00
|
|
|
assert( p->rc!=SQLITE_OK || p->eStage!=0 );
|
2014-09-03 23:30:32 +04:00
|
|
|
|
2015-02-23 15:22:55 +03:00
|
|
|
if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
|
|
|
|
if( p->eStage==OTA_STAGE_OAL ){
|
2015-02-18 20:40:05 +03:00
|
|
|
p->rc = SQLITE_ERROR;
|
|
|
|
p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
|
2015-02-23 15:22:55 +03:00
|
|
|
}else if( p->eStage==OTA_STAGE_MOVE ){
|
|
|
|
p->eStage = OTA_STAGE_CKPT;
|
|
|
|
p->nStep = 0;
|
2015-02-18 20:40:05 +03:00
|
|
|
}
|
2015-02-23 15:22:55 +03:00
|
|
|
}
|
2015-02-17 00:13:19 +03:00
|
|
|
|
2015-02-23 15:22:55 +03:00
|
|
|
if( p->rc==SQLITE_OK
|
|
|
|
&& (p->eStage==OTA_STAGE_OAL || p->eStage==OTA_STAGE_MOVE)
|
|
|
|
&& pState->eStage!=0 && p->pTargetFd->iCookie!=pState->iCookie
|
|
|
|
){
|
2015-02-18 20:40:05 +03:00
|
|
|
/* At this point (pTargetFd->iCookie) contains the value of the
|
|
|
|
** change-counter cookie (the thing that gets incremented when a
|
|
|
|
** transaction is committed in rollback mode) currently stored on
|
|
|
|
** page 1 of the database file. */
|
2015-02-23 15:22:55 +03:00
|
|
|
p->rc = SQLITE_BUSY;
|
|
|
|
p->zErrmsg = sqlite3_mprintf("database modified during ota update");
|
2015-02-18 20:40:05 +03:00
|
|
|
}
|
2015-02-16 14:48:34 +03:00
|
|
|
|
2015-02-18 20:40:05 +03:00
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
if( p->eStage==OTA_STAGE_OAL ){
|
2015-02-14 21:58:22 +03:00
|
|
|
|
2015-02-21 23:08:25 +03:00
|
|
|
/* Open transactions both databases. The *-oal file is opened or
|
|
|
|
** created at this point. */
|
|
|
|
p->rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
p->rc = sqlite3_exec(p->dbOta, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
|
|
|
|
}
|
2014-10-21 22:09:58 +04:00
|
|
|
|
|
|
|
/* Point the object iterator at the first object */
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
p->rc = otaObjIterFirst(p, &p->objiter);
|
|
|
|
}
|
2015-03-05 19:21:20 +03:00
|
|
|
|
|
|
|
/* If the OTA database contains no data_xxx tables, declare the OTA
|
|
|
|
** update finished. */
|
|
|
|
if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
|
|
|
|
p->rc = SQLITE_DONE;
|
|
|
|
}
|
|
|
|
|
2014-10-21 22:09:58 +04:00
|
|
|
if( p->rc==SQLITE_OK ){
|
2015-02-20 17:36:16 +03:00
|
|
|
otaSetupOal(p, pState);
|
2014-10-21 22:09:58 +04:00
|
|
|
}
|
2015-03-05 19:21:20 +03:00
|
|
|
|
2015-02-18 20:40:05 +03:00
|
|
|
}else if( p->eStage==OTA_STAGE_MOVE ){
|
|
|
|
/* no-op */
|
2014-10-21 22:09:58 +04:00
|
|
|
}else if( p->eStage==OTA_STAGE_CKPT ){
|
2015-02-14 21:58:22 +03:00
|
|
|
otaSetupCheckpoint(p, pState);
|
2014-10-21 22:09:58 +04:00
|
|
|
}else if( p->eStage==OTA_STAGE_DONE ){
|
|
|
|
p->rc = SQLITE_DONE;
|
2015-02-17 00:13:19 +03:00
|
|
|
}else{
|
|
|
|
p->rc = SQLITE_CORRUPT;
|
2014-10-20 20:24:23 +04:00
|
|
|
}
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
2014-09-03 23:30:32 +04:00
|
|
|
|
2014-10-20 20:24:23 +04:00
|
|
|
otaFreeState(pState);
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2014-11-20 22:19:02 +03:00
|
|
|
/*
|
|
|
|
** Return the database handle used by pOta.
|
|
|
|
*/
|
2015-02-23 18:02:13 +03:00
|
|
|
sqlite3 *sqlite3ota_db(sqlite3ota *pOta, int bOta){
|
|
|
|
sqlite3 *db = 0;
|
|
|
|
if( pOta ){
|
|
|
|
db = (bOta ? pOta->dbOta : pOta->dbMain);
|
|
|
|
}
|
|
|
|
return db;
|
2014-11-20 22:19:02 +03:00
|
|
|
}
|
|
|
|
|
2015-02-04 22:20:42 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** If the error code currently stored in the OTA handle is SQLITE_CONSTRAINT,
|
|
|
|
** then edit any error message string so as to remove all occurrences of
|
|
|
|
** the pattern "ota_imp_[0-9]*".
|
|
|
|
*/
|
|
|
|
static void otaEditErrmsg(sqlite3ota *p){
|
|
|
|
if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
|
|
|
|
int i;
|
|
|
|
int nErrmsg = strlen(p->zErrmsg);
|
|
|
|
for(i=0; i<(nErrmsg-8); i++){
|
|
|
|
if( memcmp(&p->zErrmsg[i], "ota_imp_", 8)==0 ){
|
|
|
|
int nDel = 8;
|
|
|
|
while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
|
|
|
|
memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
|
|
|
|
nErrmsg -= nDel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 23:31:15 +04:00
|
|
|
/*
|
|
|
|
** Close the OTA handle.
|
|
|
|
*/
|
2014-09-02 23:59:40 +04:00
|
|
|
int sqlite3ota_close(sqlite3ota *p, char **pzErrmsg){
|
|
|
|
int rc;
|
|
|
|
if( p ){
|
|
|
|
|
2015-02-21 23:08:25 +03:00
|
|
|
/* Commit the transaction to the *-oal file. */
|
|
|
|
if( p->rc==SQLITE_OK && p->eStage==OTA_STAGE_OAL ){
|
|
|
|
p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
|
|
|
|
}
|
2014-09-02 23:59:40 +04:00
|
|
|
|
2015-02-21 23:08:25 +03:00
|
|
|
otaSaveState(p, p->eStage);
|
2014-09-02 23:59:40 +04:00
|
|
|
|
2014-10-20 20:24:23 +04:00
|
|
|
if( p->rc==SQLITE_OK && p->eStage==OTA_STAGE_OAL ){
|
2015-02-21 23:08:25 +03:00
|
|
|
p->rc = sqlite3_exec(p->dbOta, "COMMIT", 0, 0, &p->zErrmsg);
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
2014-09-03 23:30:32 +04:00
|
|
|
|
2015-02-21 23:08:25 +03:00
|
|
|
/* Close any open statement handles. */
|
|
|
|
otaObjIterFinalize(&p->objiter);
|
|
|
|
|
2015-02-07 22:17:36 +03:00
|
|
|
/* Close the open database handle and VFS object. */
|
2015-02-21 23:08:25 +03:00
|
|
|
sqlite3_close(p->dbMain);
|
|
|
|
sqlite3_close(p->dbOta);
|
2015-02-07 22:17:36 +03:00
|
|
|
otaDeleteVfs(p);
|
2015-02-14 21:58:22 +03:00
|
|
|
sqlite3_free(p->aBuf);
|
|
|
|
sqlite3_free(p->aFrame);
|
2014-10-20 20:24:23 +04:00
|
|
|
|
2015-02-04 22:20:42 +03:00
|
|
|
otaEditErrmsg(p);
|
2014-09-02 23:59:40 +04:00
|
|
|
rc = p->rc;
|
|
|
|
*pzErrmsg = p->zErrmsg;
|
|
|
|
sqlite3_free(p);
|
|
|
|
}else{
|
|
|
|
rc = SQLITE_NOMEM;
|
|
|
|
*pzErrmsg = 0;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-09-18 18:48:38 +04:00
|
|
|
/*
|
|
|
|
** Return the total number of key-value operations (inserts, deletes or
|
|
|
|
** updates) that have been performed on the target database since the
|
|
|
|
** current OTA update was started.
|
|
|
|
*/
|
|
|
|
sqlite3_int64 sqlite3ota_progress(sqlite3ota *pOta){
|
|
|
|
return pOta->nProgress;
|
|
|
|
}
|
|
|
|
|
2015-02-07 22:17:36 +03:00
|
|
|
/**************************************************************************
|
|
|
|
** Beginning of OTA VFS shim methods. The VFS shim modifies the behaviour
|
|
|
|
** of a standard VFS in the following ways:
|
|
|
|
**
|
2015-02-19 21:06:40 +03:00
|
|
|
** 1. Whenever the first page of a main database file is read or
|
|
|
|
** written, the value of the change-counter cookie is stored in
|
|
|
|
** ota_file.iCookie. Similarly, the value of the "write-version"
|
|
|
|
** database header field is stored in ota_file.iWriteVer. This ensures
|
|
|
|
** that the values are always trustworthy within an open transaction.
|
|
|
|
**
|
|
|
|
** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (ota_file.pWalFd)
|
|
|
|
** member variable of the associated database file descriptor is set
|
|
|
|
** to point to the new file. A mutex protected linked list of all main
|
|
|
|
** db fds opened using a particular OTA VFS is maintained at
|
|
|
|
** ota_vfs.pMain to facilitate this.
|
|
|
|
**
|
|
|
|
** 3. Using a new file-control "SQLITE_FCNTL_OTA", a main db ota_file
|
|
|
|
** object can be marked as the target database of an OTA update. This
|
|
|
|
** turns on the following extra special behaviour:
|
|
|
|
**
|
|
|
|
** 3a. If xAccess() is called to check if there exists a *-wal file
|
|
|
|
** associated with an OTA target database currently in OTA_STAGE_OAL
|
|
|
|
** stage (preparing the *-oal file), the following special handling
|
|
|
|
** applies:
|
|
|
|
**
|
|
|
|
** * if the *-wal file does exist, return SQLITE_CANTOPEN. An OTA
|
|
|
|
** target database may not be in wal mode already.
|
|
|
|
**
|
|
|
|
** * if the *-wal file does not exist, set the output parameter to
|
|
|
|
** non-zero (to tell SQLite that it does exist) anyway.
|
|
|
|
**
|
|
|
|
** Then, when xOpen() is called to open the *-wal file associated with
|
|
|
|
** the OTA target in OTA_STAGE_OAL stage, instead of opening the *-wal
|
|
|
|
** file, the ota vfs opens the corresponding *-oal file instead.
|
|
|
|
**
|
|
|
|
** 3b. The *-shm pages returned by xShmMap() for a target db file in
|
|
|
|
** OTA_STAGE_OAL mode are actually stored in heap memory. This is to
|
|
|
|
** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
|
|
|
|
** are no-ops on target database files in OTA_STAGE_OAL mode. This is
|
|
|
|
** because assert() statements in some VFS implementations fail if
|
|
|
|
** xShmLock() is called before xShmMap().
|
|
|
|
**
|
|
|
|
** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
|
|
|
|
** mode except OTA_STAGE_DONE (all work completed and checkpointed), it
|
|
|
|
** fails with an SQLITE_BUSY error. This is to stop OTA connections
|
|
|
|
** from automatically checkpointing a *-wal (or *-oal) file from within
|
|
|
|
** sqlite3_close().
|
|
|
|
**
|
|
|
|
** 3d. In OTA_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
|
|
|
|
** all xWrite() calls on the target database file perform no IO.
|
|
|
|
** Instead the frame and page numbers that would be read and written
|
|
|
|
** are recorded. Additionally, successful attempts to obtain exclusive
|
|
|
|
** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
|
|
|
|
** database file are recorded. xShmLock() calls to unlock the same
|
|
|
|
** locks are no-ops (so that once obtained, these locks are never
|
|
|
|
** relinquished). Finally, calls to xSync() on the target database
|
|
|
|
** file fail with SQLITE_INTERNAL errors.
|
2015-02-07 22:17:36 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Close an ota file.
|
|
|
|
*/
|
|
|
|
static int otaVfsClose(sqlite3_file *pFile){
|
|
|
|
ota_file *p = (ota_file*)pFile;
|
|
|
|
int rc;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Free the contents of the apShm[] array. And the array itself. */
|
|
|
|
for(i=0; i<p->nShm; i++){
|
|
|
|
sqlite3_free(p->apShm[i]);
|
|
|
|
}
|
|
|
|
sqlite3_free(p->apShm);
|
|
|
|
p->apShm = 0;
|
2015-02-09 23:07:35 +03:00
|
|
|
sqlite3_free(p->zDel);
|
2015-02-07 22:17:36 +03:00
|
|
|
|
2015-02-14 21:58:22 +03:00
|
|
|
if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
|
|
|
|
ota_file **pp;
|
|
|
|
sqlite3_mutex_enter(p->pOtaVfs->mutex);
|
|
|
|
for(pp=&p->pOtaVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
|
|
|
|
*pp = p->pMainNext;
|
|
|
|
sqlite3_mutex_leave(p->pOtaVfs->mutex);
|
2015-02-16 14:48:34 +03:00
|
|
|
p->pReal->pMethods->xShmUnmap(p->pReal, 0);
|
2015-02-14 21:58:22 +03:00
|
|
|
}
|
|
|
|
|
2015-02-11 19:54:48 +03:00
|
|
|
/* Close the underlying file handle */
|
2015-02-07 22:17:36 +03:00
|
|
|
rc = p->pReal->pMethods->xClose(p->pReal);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Read and return an unsigned 32-bit big-endian integer from the buffer
|
|
|
|
** passed as the only argument.
|
|
|
|
*/
|
2015-02-14 21:58:22 +03:00
|
|
|
static u32 otaGetU32(u8 *aBuf){
|
|
|
|
return ((u32)aBuf[0] << 24)
|
|
|
|
+ ((u32)aBuf[1] << 16)
|
|
|
|
+ ((u32)aBuf[2] << 8)
|
|
|
|
+ ((u32)aBuf[3]);
|
2015-02-07 22:17:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Read data from an otaVfs-file.
|
|
|
|
*/
|
|
|
|
static int otaVfsRead(
|
|
|
|
sqlite3_file *pFile,
|
|
|
|
void *zBuf,
|
|
|
|
int iAmt,
|
|
|
|
sqlite_int64 iOfst
|
|
|
|
){
|
|
|
|
ota_file *p = (ota_file*)pFile;
|
2015-02-21 23:08:25 +03:00
|
|
|
sqlite3ota *pOta = p->pOta;
|
2015-02-14 21:58:22 +03:00
|
|
|
int rc;
|
|
|
|
|
2015-02-21 23:08:25 +03:00
|
|
|
if( pOta && pOta->eStage==OTA_STAGE_CAPTURE ){
|
2015-02-17 00:13:19 +03:00
|
|
|
assert( p->openFlags & SQLITE_OPEN_WAL );
|
2015-02-14 21:58:22 +03:00
|
|
|
rc = otaCaptureWalRead(p->pOta, iOfst, iAmt);
|
|
|
|
}else{
|
2015-02-21 23:08:25 +03:00
|
|
|
if( pOta && pOta->eStage==OTA_STAGE_OAL
|
|
|
|
&& (p->openFlags & SQLITE_OPEN_WAL)
|
|
|
|
&& iOfst>=pOta->iOalSz
|
|
|
|
){
|
|
|
|
rc = SQLITE_OK;
|
|
|
|
memset(zBuf, 0, iAmt);
|
|
|
|
}else{
|
|
|
|
rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
|
|
|
|
}
|
2015-02-14 21:58:22 +03:00
|
|
|
if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
|
|
|
|
/* These look like magic numbers. But they are stable, as they are part
|
|
|
|
** of the definition of the SQLite file format, which may not change. */
|
|
|
|
u8 *pBuf = (u8*)zBuf;
|
|
|
|
p->iCookie = otaGetU32(&pBuf[24]);
|
|
|
|
p->iWriteVer = pBuf[19];
|
|
|
|
}
|
2015-02-07 22:17:36 +03:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Write data to an otaVfs-file.
|
|
|
|
*/
|
|
|
|
static int otaVfsWrite(
|
|
|
|
sqlite3_file *pFile,
|
|
|
|
const void *zBuf,
|
|
|
|
int iAmt,
|
|
|
|
sqlite_int64 iOfst
|
|
|
|
){
|
|
|
|
ota_file *p = (ota_file*)pFile;
|
2015-02-21 23:08:25 +03:00
|
|
|
sqlite3ota *pOta = p->pOta;
|
2015-02-14 21:58:22 +03:00
|
|
|
int rc;
|
2015-02-21 23:08:25 +03:00
|
|
|
|
|
|
|
if( pOta && pOta->eStage==OTA_STAGE_CAPTURE ){
|
2015-02-17 00:13:19 +03:00
|
|
|
assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
|
2015-02-14 21:58:22 +03:00
|
|
|
rc = otaCaptureDbWrite(p->pOta, iOfst);
|
|
|
|
}else{
|
2015-02-21 23:08:25 +03:00
|
|
|
if( pOta && pOta->eStage==OTA_STAGE_OAL
|
|
|
|
&& (p->openFlags & SQLITE_OPEN_WAL)
|
|
|
|
&& iOfst>=pOta->iOalSz
|
|
|
|
){
|
|
|
|
pOta->iOalSz = iAmt + iOfst;
|
|
|
|
}
|
2015-02-14 21:58:22 +03:00
|
|
|
rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
|
|
|
|
if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
|
|
|
|
/* These look like magic numbers. But they are stable, as they are part
|
|
|
|
** of the definition of the SQLite file format, which may not change. */
|
|
|
|
u8 *pBuf = (u8*)zBuf;
|
|
|
|
p->iCookie = otaGetU32(&pBuf[24]);
|
|
|
|
p->iWriteVer = pBuf[19];
|
|
|
|
}
|
2015-02-07 22:17:36 +03:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Truncate an otaVfs-file.
|
|
|
|
*/
|
|
|
|
static int otaVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
|
|
|
|
ota_file *p = (ota_file*)pFile;
|
|
|
|
return p->pReal->pMethods->xTruncate(p->pReal, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Sync an otaVfs-file.
|
|
|
|
*/
|
|
|
|
static int otaVfsSync(sqlite3_file *pFile, int flags){
|
|
|
|
ota_file *p = (ota_file *)pFile;
|
2015-02-14 21:58:22 +03:00
|
|
|
if( p->pOta && p->pOta->eStage==OTA_STAGE_CAPTURE ){
|
|
|
|
if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
|
|
|
|
return SQLITE_INTERNAL;
|
|
|
|
}
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
2015-02-07 22:17:36 +03:00
|
|
|
return p->pReal->pMethods->xSync(p->pReal, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return the current file-size of an otaVfs-file.
|
|
|
|
*/
|
|
|
|
static int otaVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
|
|
|
|
ota_file *p = (ota_file *)pFile;
|
|
|
|
return p->pReal->pMethods->xFileSize(p->pReal, pSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Lock an otaVfs-file.
|
|
|
|
*/
|
|
|
|
static int otaVfsLock(sqlite3_file *pFile, int eLock){
|
|
|
|
ota_file *p = (ota_file*)pFile;
|
2015-02-09 23:07:35 +03:00
|
|
|
sqlite3ota *pOta = p->pOta;
|
2015-02-07 22:17:36 +03:00
|
|
|
int rc = SQLITE_OK;
|
|
|
|
|
2015-02-14 21:58:22 +03:00
|
|
|
assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
|
|
|
|
if( pOta && eLock==SQLITE_LOCK_EXCLUSIVE && pOta->eStage!=OTA_STAGE_DONE ){
|
2015-02-07 22:17:36 +03:00
|
|
|
/* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
|
|
|
|
** prevents it from checkpointing the database from sqlite3_close(). */
|
|
|
|
rc = SQLITE_BUSY;
|
|
|
|
}else{
|
|
|
|
rc = p->pReal->pMethods->xLock(p->pReal, eLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Unlock an otaVfs-file.
|
|
|
|
*/
|
|
|
|
static int otaVfsUnlock(sqlite3_file *pFile, int eLock){
|
|
|
|
ota_file *p = (ota_file *)pFile;
|
|
|
|
return p->pReal->pMethods->xUnlock(p->pReal, eLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Check if another file-handle holds a RESERVED lock on an otaVfs-file.
|
|
|
|
*/
|
|
|
|
static int otaVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
|
|
|
|
ota_file *p = (ota_file *)pFile;
|
|
|
|
return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** File control method. For custom operations on an otaVfs-file.
|
|
|
|
*/
|
|
|
|
static int otaVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
|
|
|
|
ota_file *p = (ota_file *)pFile;
|
2015-02-10 20:08:17 +03:00
|
|
|
int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
|
2015-03-05 19:21:20 +03:00
|
|
|
int rc;
|
2015-02-10 20:08:17 +03:00
|
|
|
|
2015-03-23 20:10:51 +03:00
|
|
|
assert( p->openFlags &
|
|
|
|
(SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB|SQLITE_OPEN_TRANSIENT_DB)
|
|
|
|
);
|
2015-02-09 23:07:35 +03:00
|
|
|
if( op==SQLITE_FCNTL_OTA ){
|
|
|
|
sqlite3ota *pOta = (sqlite3ota*)pArg;
|
2015-02-10 20:08:17 +03:00
|
|
|
|
|
|
|
/* First try to find another OTA vfs lower down in the vfs stack. If
|
|
|
|
** one is found, this vfs will operate in pass-through mode. The lower
|
|
|
|
** level vfs will do the special OTA handling. */
|
|
|
|
rc = xControl(p->pReal, op, pArg);
|
|
|
|
|
|
|
|
if( rc==SQLITE_NOTFOUND ){
|
|
|
|
/* Now search for a zipvfs instance lower down in the VFS stack. If
|
|
|
|
** one is found, this is an error. */
|
|
|
|
void *dummy = 0;
|
2015-02-23 18:41:48 +03:00
|
|
|
rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
|
2015-02-10 20:08:17 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = SQLITE_ERROR;
|
|
|
|
pOta->zErrmsg = sqlite3_mprintf("ota/zipvfs setup error");
|
|
|
|
}else if( rc==SQLITE_NOTFOUND ){
|
|
|
|
pOta->pTargetFd = p;
|
|
|
|
p->pOta = pOta;
|
2015-02-14 21:58:22 +03:00
|
|
|
if( p->pWalFd ) p->pWalFd->pOta = pOta;
|
2015-02-10 20:08:17 +03:00
|
|
|
rc = SQLITE_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc;
|
2015-02-09 23:07:35 +03:00
|
|
|
}
|
2015-03-05 19:21:20 +03:00
|
|
|
|
|
|
|
rc = xControl(p->pReal, op, pArg);
|
|
|
|
if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
|
|
|
|
ota_vfs *pOtaVfs = p->pOtaVfs;
|
|
|
|
char *zIn = *(char**)pArg;
|
|
|
|
char *zOut = sqlite3_mprintf("ota(%s)/%z", pOtaVfs->base.zName, zIn);
|
|
|
|
*(char**)pArg = zOut;
|
|
|
|
if( zOut==0 ) rc = SQLITE_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
2015-02-07 22:17:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return the sector-size in bytes for an otaVfs-file.
|
|
|
|
*/
|
|
|
|
static int otaVfsSectorSize(sqlite3_file *pFile){
|
|
|
|
ota_file *p = (ota_file *)pFile;
|
|
|
|
return p->pReal->pMethods->xSectorSize(p->pReal);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return the device characteristic flags supported by an otaVfs-file.
|
|
|
|
*/
|
|
|
|
static int otaVfsDeviceCharacteristics(sqlite3_file *pFile){
|
|
|
|
ota_file *p = (ota_file *)pFile;
|
|
|
|
return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-02-20 17:36:16 +03:00
|
|
|
** Take or release a shared-memory lock.
|
2015-02-07 22:17:36 +03:00
|
|
|
*/
|
|
|
|
static int otaVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
|
|
|
|
ota_file *p = (ota_file*)pFile;
|
2015-02-14 21:58:22 +03:00
|
|
|
sqlite3ota *pOta = p->pOta;
|
2015-02-07 22:17:36 +03:00
|
|
|
int rc = SQLITE_OK;
|
|
|
|
|
|
|
|
#ifdef SQLITE_AMALGAMATION
|
2015-02-10 23:00:38 +03:00
|
|
|
assert( WAL_CKPT_LOCK==1 );
|
2015-02-07 22:17:36 +03:00
|
|
|
#endif
|
|
|
|
|
2015-02-14 21:58:22 +03:00
|
|
|
assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
|
2015-02-18 20:40:05 +03:00
|
|
|
if( pOta && (pOta->eStage==OTA_STAGE_OAL || pOta->eStage==OTA_STAGE_MOVE) ){
|
2015-02-10 23:00:38 +03:00
|
|
|
/* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
|
2015-02-07 22:17:36 +03:00
|
|
|
** taking this lock also prevents any checkpoints from occurring.
|
|
|
|
** todo: really, it's not clear why this might occur, as
|
|
|
|
** wal_autocheckpoint ought to be turned off. */
|
2015-02-14 21:58:22 +03:00
|
|
|
if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
|
2015-02-07 22:17:36 +03:00
|
|
|
}else{
|
2015-02-14 21:58:22 +03:00
|
|
|
int bCapture = 0;
|
|
|
|
if( n==1 && (flags & SQLITE_SHM_EXCLUSIVE)
|
2015-02-18 20:40:05 +03:00
|
|
|
&& pOta && pOta->eStage==OTA_STAGE_CAPTURE
|
2015-02-14 21:58:22 +03:00
|
|
|
&& (ofst==WAL_LOCK_WRITE || ofst==WAL_LOCK_CKPT || ofst==WAL_LOCK_READ0)
|
|
|
|
){
|
|
|
|
bCapture = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
|
|
|
|
rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
|
|
|
|
if( bCapture && rc==SQLITE_OK ){
|
2015-02-18 20:40:05 +03:00
|
|
|
pOta->mLock |= (1 << ofst);
|
2015-02-14 21:58:22 +03:00
|
|
|
}
|
|
|
|
}
|
2015-02-07 22:17:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-02-20 17:36:16 +03:00
|
|
|
/*
|
|
|
|
** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
|
|
|
|
*/
|
2015-02-07 22:17:36 +03:00
|
|
|
static int otaVfsShmMap(
|
|
|
|
sqlite3_file *pFile,
|
|
|
|
int iRegion,
|
|
|
|
int szRegion,
|
|
|
|
int isWrite,
|
|
|
|
void volatile **pp
|
|
|
|
){
|
|
|
|
ota_file *p = (ota_file*)pFile;
|
|
|
|
int rc = SQLITE_OK;
|
2015-02-18 20:40:05 +03:00
|
|
|
int eStage = (p->pOta ? p->pOta->eStage : 0);
|
2015-02-07 22:17:36 +03:00
|
|
|
|
|
|
|
/* If not in OTA_STAGE_OAL, allow this call to pass through. Or, if this
|
|
|
|
** ota is in the OTA_STAGE_OAL state, use heap memory for *-shm space
|
|
|
|
** instead of a file on disk. */
|
2015-02-14 21:58:22 +03:00
|
|
|
assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
|
2015-02-18 20:40:05 +03:00
|
|
|
if( eStage==OTA_STAGE_OAL || eStage==OTA_STAGE_MOVE ){
|
2015-02-07 22:17:36 +03:00
|
|
|
if( iRegion<=p->nShm ){
|
|
|
|
int nByte = (iRegion+1) * sizeof(char*);
|
|
|
|
char **apNew = (char**)sqlite3_realloc(p->apShm, nByte);
|
|
|
|
if( apNew==0 ){
|
|
|
|
rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
|
|
|
memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
|
|
|
|
p->apShm = apNew;
|
|
|
|
p->nShm = iRegion+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( rc==SQLITE_OK && p->apShm[iRegion]==0 ){
|
|
|
|
char *pNew = (char*)sqlite3_malloc(szRegion);
|
|
|
|
if( pNew==0 ){
|
|
|
|
rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
2015-02-10 23:00:38 +03:00
|
|
|
memset(pNew, 0, szRegion);
|
2015-02-07 22:17:36 +03:00
|
|
|
p->apShm[iRegion] = pNew;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
*pp = p->apShm[iRegion];
|
|
|
|
}else{
|
|
|
|
*pp = 0;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
assert( p->apShm==0 );
|
|
|
|
rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Memory barrier.
|
|
|
|
*/
|
|
|
|
static void otaVfsShmBarrier(sqlite3_file *pFile){
|
|
|
|
ota_file *p = (ota_file *)pFile;
|
|
|
|
p->pReal->pMethods->xShmBarrier(p->pReal);
|
|
|
|
}
|
|
|
|
|
2015-02-20 17:36:16 +03:00
|
|
|
/*
|
|
|
|
** The xShmUnmap method.
|
|
|
|
*/
|
2015-02-07 22:17:36 +03:00
|
|
|
static int otaVfsShmUnmap(sqlite3_file *pFile, int delFlag){
|
|
|
|
ota_file *p = (ota_file*)pFile;
|
|
|
|
int rc = SQLITE_OK;
|
2015-02-18 20:40:05 +03:00
|
|
|
int eStage = (p->pOta ? p->pOta->eStage : 0);
|
2015-02-07 22:17:36 +03:00
|
|
|
|
2015-02-14 21:58:22 +03:00
|
|
|
assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
|
2015-02-18 20:40:05 +03:00
|
|
|
if( eStage==OTA_STAGE_OAL || eStage==OTA_STAGE_MOVE ){
|
2015-02-07 22:17:36 +03:00
|
|
|
/* no-op */
|
|
|
|
}else{
|
|
|
|
rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-02-20 17:36:16 +03:00
|
|
|
/*
|
|
|
|
** Given that zWal points to a buffer containing a wal file name passed to
|
|
|
|
** either the xOpen() or xAccess() VFS method, return a pointer to the
|
|
|
|
** file-handle opened by the same database connection on the corresponding
|
|
|
|
** database file.
|
|
|
|
*/
|
2015-02-14 21:58:22 +03:00
|
|
|
static ota_file *otaFindMaindb(ota_vfs *pOtaVfs, const char *zWal){
|
|
|
|
ota_file *pDb;
|
|
|
|
sqlite3_mutex_enter(pOtaVfs->mutex);
|
|
|
|
for(pDb=pOtaVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext);
|
|
|
|
sqlite3_mutex_leave(pOtaVfs->mutex);
|
|
|
|
return pDb;
|
|
|
|
}
|
|
|
|
|
2015-02-07 22:17:36 +03:00
|
|
|
/*
|
|
|
|
** Open an ota file handle.
|
|
|
|
*/
|
|
|
|
static int otaVfsOpen(
|
|
|
|
sqlite3_vfs *pVfs,
|
|
|
|
const char *zName,
|
|
|
|
sqlite3_file *pFile,
|
|
|
|
int flags,
|
|
|
|
int *pOutFlags
|
|
|
|
){
|
|
|
|
static sqlite3_io_methods otavfs_io_methods = {
|
|
|
|
2, /* iVersion */
|
|
|
|
otaVfsClose, /* xClose */
|
|
|
|
otaVfsRead, /* xRead */
|
|
|
|
otaVfsWrite, /* xWrite */
|
|
|
|
otaVfsTruncate, /* xTruncate */
|
|
|
|
otaVfsSync, /* xSync */
|
|
|
|
otaVfsFileSize, /* xFileSize */
|
|
|
|
otaVfsLock, /* xLock */
|
|
|
|
otaVfsUnlock, /* xUnlock */
|
|
|
|
otaVfsCheckReservedLock, /* xCheckReservedLock */
|
|
|
|
otaVfsFileControl, /* xFileControl */
|
|
|
|
otaVfsSectorSize, /* xSectorSize */
|
|
|
|
otaVfsDeviceCharacteristics, /* xDeviceCharacteristics */
|
|
|
|
otaVfsShmMap, /* xShmMap */
|
|
|
|
otaVfsShmLock, /* xShmLock */
|
|
|
|
otaVfsShmBarrier, /* xShmBarrier */
|
|
|
|
otaVfsShmUnmap /* xShmUnmap */
|
|
|
|
};
|
|
|
|
ota_vfs *pOtaVfs = (ota_vfs*)pVfs;
|
|
|
|
sqlite3_vfs *pRealVfs = pOtaVfs->pRealVfs;
|
|
|
|
ota_file *pFd = (ota_file *)pFile;
|
|
|
|
int rc = SQLITE_OK;
|
|
|
|
const char *zOpen = zName;
|
|
|
|
|
|
|
|
memset(pFd, 0, sizeof(ota_file));
|
|
|
|
pFd->pReal = (sqlite3_file*)&pFd[1];
|
|
|
|
pFd->pOtaVfs = pOtaVfs;
|
2015-02-10 23:00:38 +03:00
|
|
|
pFd->openFlags = flags;
|
2015-02-09 23:07:35 +03:00
|
|
|
if( zName ){
|
|
|
|
if( flags & SQLITE_OPEN_MAIN_DB ){
|
|
|
|
/* A main database has just been opened. The following block sets
|
|
|
|
** (pFd->zWal) to point to a buffer owned by SQLite that contains
|
|
|
|
** the name of the *-wal file this db connection will use. SQLite
|
|
|
|
** happens to pass a pointer to this buffer when using xAccess()
|
|
|
|
** or xOpen() to operate on the *-wal file. */
|
|
|
|
int n = strlen(zName);
|
|
|
|
const char *z = &zName[n];
|
|
|
|
if( flags & SQLITE_OPEN_URI ){
|
|
|
|
int odd = 0;
|
|
|
|
while( 1 ){
|
|
|
|
if( z[0]==0 ){
|
|
|
|
odd = 1 - odd;
|
|
|
|
if( odd && z[1]==0 ) break;
|
|
|
|
}
|
|
|
|
z++;
|
|
|
|
}
|
|
|
|
z += 2;
|
|
|
|
}else{
|
|
|
|
while( *z==0 ) z++;
|
|
|
|
}
|
|
|
|
z += (n + 8 + 1);
|
|
|
|
pFd->zWal = z;
|
|
|
|
}
|
2015-02-14 21:58:22 +03:00
|
|
|
else if( flags & SQLITE_OPEN_WAL ){
|
|
|
|
ota_file *pDb = otaFindMaindb(pOtaVfs, zName);
|
|
|
|
if( pDb ){
|
|
|
|
if( pDb->pOta && pDb->pOta->eStage==OTA_STAGE_OAL ){
|
2015-02-16 09:27:37 +03:00
|
|
|
char *zCopy = otaStrndup(zName, &rc);
|
2015-02-14 21:58:22 +03:00
|
|
|
if( zCopy ){
|
|
|
|
int nCopy = strlen(zCopy);
|
|
|
|
zCopy[nCopy-3] = 'o';
|
|
|
|
zOpen = (const char*)(pFd->zDel = zCopy);
|
|
|
|
}
|
|
|
|
pFd->pOta = pDb->pOta;
|
|
|
|
}
|
|
|
|
pDb->pWalFd = pFd;
|
2015-02-09 23:07:35 +03:00
|
|
|
}
|
2015-02-07 22:17:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, flags, pOutFlags);
|
|
|
|
}
|
|
|
|
if( pFd->pReal->pMethods ){
|
2015-02-14 21:58:22 +03:00
|
|
|
/* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
|
|
|
|
** pointer and, if the file is a main database file, link it into the
|
|
|
|
** mutex protected linked list of all such files. */
|
2015-02-07 22:17:36 +03:00
|
|
|
pFile->pMethods = &otavfs_io_methods;
|
2015-02-14 21:58:22 +03:00
|
|
|
if( flags & SQLITE_OPEN_MAIN_DB ){
|
|
|
|
sqlite3_mutex_enter(pOtaVfs->mutex);
|
|
|
|
pFd->pMainNext = pOtaVfs->pMain;
|
|
|
|
pOtaVfs->pMain = pFd;
|
|
|
|
sqlite3_mutex_leave(pOtaVfs->mutex);
|
|
|
|
}
|
2015-02-07 22:17:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Delete the file located at zPath.
|
|
|
|
*/
|
|
|
|
static int otaVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
|
|
|
|
sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
|
|
|
|
return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Test for access permissions. Return true if the requested permission
|
|
|
|
** is available, or false otherwise.
|
|
|
|
*/
|
|
|
|
static int otaVfsAccess(
|
|
|
|
sqlite3_vfs *pVfs,
|
|
|
|
const char *zPath,
|
|
|
|
int flags,
|
|
|
|
int *pResOut
|
|
|
|
){
|
|
|
|
ota_vfs *pOtaVfs = (ota_vfs*)pVfs;
|
|
|
|
sqlite3_vfs *pRealVfs = pOtaVfs->pRealVfs;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
|
|
|
|
|
2015-02-14 21:58:22 +03:00
|
|
|
/* If this call is to check if a *-wal file associated with an OTA target
|
|
|
|
** database connection exists, and the OTA update is in OTA_STAGE_OAL,
|
|
|
|
** the following special handling is activated:
|
|
|
|
**
|
|
|
|
** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
|
|
|
|
** ensures that the OTA extension never tries to update a database
|
|
|
|
** in wal mode, even if the first page of the database file has
|
|
|
|
** been damaged.
|
|
|
|
**
|
|
|
|
** b) if the *-wal file does not exist, claim that it does anyway,
|
|
|
|
** causing SQLite to call xOpen() to open it. This call will also
|
|
|
|
** be intercepted (see the otaVfsOpen() function) and the *-oal
|
|
|
|
** file opened instead.
|
|
|
|
*/
|
|
|
|
if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
|
|
|
|
ota_file *pDb = otaFindMaindb(pOtaVfs, zPath);
|
|
|
|
if( pDb && pDb->pOta && pDb->pOta->eStage==OTA_STAGE_OAL ){
|
|
|
|
if( *pResOut ){
|
|
|
|
rc = SQLITE_CANTOPEN;
|
|
|
|
}else{
|
|
|
|
*pResOut = 1;
|
|
|
|
}
|
2015-02-07 22:17:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Populate buffer zOut with the full canonical pathname corresponding
|
|
|
|
** to the pathname in zPath. zOut is guaranteed to point to a buffer
|
|
|
|
** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
|
|
|
|
*/
|
|
|
|
static int otaVfsFullPathname(
|
|
|
|
sqlite3_vfs *pVfs,
|
|
|
|
const char *zPath,
|
|
|
|
int nOut,
|
|
|
|
char *zOut
|
|
|
|
){
|
|
|
|
sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
|
|
|
|
return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef SQLITE_OMIT_LOAD_EXTENSION
|
|
|
|
/*
|
|
|
|
** Open the dynamic library located at zPath and return a handle.
|
|
|
|
*/
|
|
|
|
static void *otaVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
|
|
|
|
sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
|
|
|
|
return pRealVfs->xDlOpen(pRealVfs, zPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Populate the buffer zErrMsg (size nByte bytes) with a human readable
|
|
|
|
** utf-8 string describing the most recent error encountered associated
|
|
|
|
** with dynamic libraries.
|
|
|
|
*/
|
|
|
|
static void otaVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
|
|
|
|
sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
|
|
|
|
pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
|
|
|
|
*/
|
|
|
|
static void (*otaVfsDlSym(
|
|
|
|
sqlite3_vfs *pVfs,
|
|
|
|
void *pArg,
|
|
|
|
const char *zSym
|
|
|
|
))(void){
|
|
|
|
sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
|
|
|
|
return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Close the dynamic library handle pHandle.
|
|
|
|
*/
|
|
|
|
static void otaVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
|
|
|
|
sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
|
|
|
|
return pRealVfs->xDlClose(pRealVfs, pHandle);
|
|
|
|
}
|
|
|
|
#endif /* SQLITE_OMIT_LOAD_EXTENSION */
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Populate the buffer pointed to by zBufOut with nByte bytes of
|
|
|
|
** random data.
|
|
|
|
*/
|
|
|
|
static int otaVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
|
|
|
|
sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
|
|
|
|
return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Sleep for nMicro microseconds. Return the number of microseconds
|
|
|
|
** actually slept.
|
|
|
|
*/
|
|
|
|
static int otaVfsSleep(sqlite3_vfs *pVfs, int nMicro){
|
|
|
|
sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
|
|
|
|
return pRealVfs->xSleep(pRealVfs, nMicro);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return the current time as a Julian Day number in *pTimeOut.
|
|
|
|
*/
|
|
|
|
static int otaVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
|
|
|
|
sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
|
|
|
|
return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
|
|
|
|
}
|
|
|
|
|
2015-02-20 17:36:16 +03:00
|
|
|
/*
|
|
|
|
** No-op.
|
|
|
|
*/
|
2015-02-07 22:17:36 +03:00
|
|
|
static int otaVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-20 17:36:16 +03:00
|
|
|
/*
|
|
|
|
** Deregister and destroy an OTA vfs created by an earlier call to
|
|
|
|
** sqlite3ota_create_vfs().
|
|
|
|
*/
|
2015-02-09 23:07:35 +03:00
|
|
|
void sqlite3ota_destroy_vfs(const char *zName){
|
|
|
|
sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
|
2015-02-16 14:48:34 +03:00
|
|
|
if( pVfs && pVfs->xOpen==otaVfsOpen ){
|
2015-02-19 22:59:35 +03:00
|
|
|
sqlite3_mutex_free(((ota_vfs*)pVfs)->mutex);
|
2015-02-09 23:07:35 +03:00
|
|
|
sqlite3_vfs_unregister(pVfs);
|
|
|
|
sqlite3_free(pVfs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 17:36:16 +03:00
|
|
|
/*
|
|
|
|
** Create an OTA VFS named zName that accesses the underlying file-system
|
|
|
|
** via existing VFS zParent. The new object is registered as a non-default
|
|
|
|
** VFS with SQLite before returning.
|
|
|
|
*/
|
2015-02-09 23:07:35 +03:00
|
|
|
int sqlite3ota_create_vfs(const char *zName, const char *zParent){
|
2015-02-07 22:17:36 +03:00
|
|
|
|
|
|
|
/* Template for VFS */
|
|
|
|
static sqlite3_vfs vfs_template = {
|
|
|
|
1, /* iVersion */
|
|
|
|
0, /* szOsFile */
|
|
|
|
0, /* mxPathname */
|
|
|
|
0, /* pNext */
|
|
|
|
0, /* zName */
|
|
|
|
0, /* pAppData */
|
|
|
|
otaVfsOpen, /* xOpen */
|
|
|
|
otaVfsDelete, /* xDelete */
|
|
|
|
otaVfsAccess, /* xAccess */
|
|
|
|
otaVfsFullPathname, /* xFullPathname */
|
|
|
|
|
2015-02-23 23:09:50 +03:00
|
|
|
#ifndef SQLITE_OMIT_LOAD_EXTENSION
|
2015-02-07 22:17:36 +03:00
|
|
|
otaVfsDlOpen, /* xDlOpen */
|
|
|
|
otaVfsDlError, /* xDlError */
|
|
|
|
otaVfsDlSym, /* xDlSym */
|
|
|
|
otaVfsDlClose, /* xDlClose */
|
2015-02-23 23:09:50 +03:00
|
|
|
#else
|
|
|
|
0, 0, 0, 0,
|
|
|
|
#endif
|
2015-02-07 22:17:36 +03:00
|
|
|
|
|
|
|
otaVfsRandomness, /* xRandomness */
|
|
|
|
otaVfsSleep, /* xSleep */
|
|
|
|
otaVfsCurrentTime, /* xCurrentTime */
|
|
|
|
otaVfsGetLastError, /* xGetLastError */
|
|
|
|
0, /* xCurrentTimeInt64 (version 2) */
|
|
|
|
0, 0, 0 /* Unimplemented version 3 methods */
|
|
|
|
};
|
|
|
|
|
|
|
|
ota_vfs *pNew = 0; /* Newly allocated VFS */
|
2015-02-09 23:07:35 +03:00
|
|
|
int nName;
|
|
|
|
int rc = SQLITE_OK;
|
2015-02-07 22:17:36 +03:00
|
|
|
|
2015-02-16 14:48:34 +03:00
|
|
|
int nByte;
|
2015-02-09 23:07:35 +03:00
|
|
|
nName = strlen(zName);
|
2015-02-16 14:48:34 +03:00
|
|
|
nByte = sizeof(ota_vfs) + nName + 1;
|
|
|
|
pNew = (ota_vfs*)sqlite3_malloc(nByte);
|
|
|
|
if( pNew==0 ){
|
|
|
|
rc = SQLITE_NOMEM;
|
2015-02-07 22:17:36 +03:00
|
|
|
}else{
|
2015-02-16 14:48:34 +03:00
|
|
|
sqlite3_vfs *pParent; /* Parent VFS */
|
|
|
|
memset(pNew, 0, nByte);
|
|
|
|
pParent = sqlite3_vfs_find(zParent);
|
|
|
|
if( pParent==0 ){
|
|
|
|
rc = SQLITE_NOTFOUND;
|
2015-02-09 23:07:35 +03:00
|
|
|
}else{
|
2015-02-16 14:48:34 +03:00
|
|
|
char *zSpace;
|
|
|
|
memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
|
|
|
|
pNew->base.mxPathname = pParent->mxPathname;
|
|
|
|
pNew->base.szOsFile = sizeof(ota_file) + pParent->szOsFile;
|
|
|
|
pNew->pRealVfs = pParent;
|
|
|
|
pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
|
|
|
|
memcpy(zSpace, zName, nName);
|
2015-02-07 22:17:36 +03:00
|
|
|
|
2015-02-19 22:59:35 +03:00
|
|
|
/* Allocate the mutex and register the new VFS (not as the default) */
|
|
|
|
pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
|
|
|
|
if( pNew->mutex==0 ){
|
|
|
|
rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
|
|
|
rc = sqlite3_vfs_register(&pNew->base, 0);
|
|
|
|
}
|
2015-02-07 22:17:36 +03:00
|
|
|
}
|
2015-02-09 23:07:35 +03:00
|
|
|
|
2015-02-19 22:59:35 +03:00
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
sqlite3_mutex_free(pNew->mutex);
|
|
|
|
sqlite3_free(pNew);
|
|
|
|
}
|
2015-02-07 22:17:36 +03:00
|
|
|
}
|
2015-02-19 22:59:35 +03:00
|
|
|
|
2015-02-16 14:48:34 +03:00
|
|
|
return rc;
|
2015-02-07 22:17:36 +03:00
|
|
|
}
|
|
|
|
|
2014-09-02 23:59:40 +04:00
|
|
|
|
|
|
|
/**************************************************************************/
|
|
|
|
|
2015-02-16 09:27:37 +03:00
|
|
|
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_OTA) */
|