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.
|
|
|
|
**
|
|
|
|
*************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** 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:
|
|
|
|
** May be set to integer values 1, 2 or 3. As follows:
|
|
|
|
** 0: Nothing at all has been done.
|
|
|
|
** 1: the *-ota file is currently under construction.
|
|
|
|
** 2: the *-ota file has been constructed, but not yet moved
|
|
|
|
** to the *-wal path.
|
|
|
|
** 3: the checkpoint is underway.
|
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:
|
|
|
|
** Valid if STAGE==3. The blob to pass to sqlite3ckpt_start() to resume
|
|
|
|
** the incremental checkpoint.
|
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
|
|
|
|
|
|
|
|
#define OTA_STAGE_OAL 1
|
|
|
|
#define OTA_STAGE_COPY 2
|
|
|
|
#define OTA_STAGE_CKPT 3
|
|
|
|
#define OTA_STAGE_DONE 4
|
|
|
|
|
|
|
|
|
2014-09-05 23:52:42 +04:00
|
|
|
#define OTA_CREATE_STATE "CREATE TABLE IF NOT EXISTS ota.ota_state" \
|
2014-10-20 20:24:23 +04:00
|
|
|
"(k INTEGER PRIMARY KEY, v)"
|
2014-09-02 23:59:40 +04:00
|
|
|
|
2014-09-03 23:30:32 +04:00
|
|
|
typedef struct OtaState OtaState;
|
2014-09-05 23:31:15 +04:00
|
|
|
typedef struct OtaObjIter OtaObjIter;
|
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;
|
2014-10-20 20:24:23 +04:00
|
|
|
unsigned char *pCkptState;
|
|
|
|
int nCkptState;
|
2014-09-05 23:31:15 +04:00
|
|
|
int nRow;
|
2014-09-18 18:48:38 +04:00
|
|
|
sqlite3_int64 nProgress;
|
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
|
|
|
|
** * a special "cleanup table" point.
|
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 */
|
|
|
|
char **azTblCol; /* Array of quoted column names */
|
2014-09-18 19:57:22 +04:00
|
|
|
unsigned char *abTblPk; /* Array of flags - true for PK columns */
|
2014-12-06 22:30:41 +03:00
|
|
|
int eType;
|
|
|
|
#if 0
|
2014-11-20 20:37:08 +03:00
|
|
|
unsigned char bRowid; /* True for implicit IPK tables */
|
2014-11-21 13:46:23 +03:00
|
|
|
unsigned char bVtab; /* True for a virtual table */
|
2014-12-06 22:30:41 +03:00
|
|
|
#endif
|
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) */
|
|
|
|
int iVisit; /* Number of points visited, incl. current */
|
|
|
|
|
|
|
|
/* 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 */
|
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
|
|
|
|
*/
|
|
|
|
#define OTA_PK_REAL 1 /* Table has a real primary key */
|
|
|
|
#define OTA_PK_EXTERNAL 2 /* Table has an external primary key index */
|
|
|
|
#define OTA_PK_NONE 3 /* Table has no PK (use rowid) */
|
|
|
|
#define OTA_PK_VTAB 4 /* Table is a virtual table (use rowid) */
|
|
|
|
|
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 */
|
2014-09-05 23:52:42 +04:00
|
|
|
sqlite3 *db; /* "main" -> target db, "ota" -> ota db */
|
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 */
|
|
|
|
sqlite3_ckpt *pCkpt; /* Incr-checkpoint handle */
|
2014-09-02 23:59:40 +04: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
|
|
|
|
** by an earlier call to otaObjIterGetCols().
|
|
|
|
*/
|
|
|
|
static void otaObjIterFreeCols(OtaObjIter *pIter){
|
|
|
|
int i;
|
|
|
|
for(i=0; i<pIter->nTblCol; i++){
|
|
|
|
sqlite3_free(pIter->azTblCol[i]);
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
2014-09-05 23:31:15 +04:00
|
|
|
sqlite3_free(pIter->azTblCol);
|
|
|
|
pIter->azTblCol = 0;
|
|
|
|
pIter->abTblPk = 0;
|
|
|
|
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);
|
|
|
|
pIter->pSelect = 0;
|
|
|
|
pIter->pInsert = 0;
|
|
|
|
pIter->pDelete = 0;
|
|
|
|
pIter->pUpdate = 0;
|
|
|
|
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);
|
2014-09-05 23:31:15 +04:00
|
|
|
|
|
|
|
if( pIter->bCleanup ){
|
|
|
|
otaObjIterFreeCols(pIter);
|
|
|
|
pIter->bCleanup = 0;
|
|
|
|
rc = sqlite3_step(pIter->pTblIter);
|
|
|
|
if( rc!=SQLITE_ROW ){
|
|
|
|
rc = sqlite3_reset(pIter->pTblIter);
|
|
|
|
pIter->zTbl = 0;
|
|
|
|
}else{
|
|
|
|
pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
|
|
|
|
rc = SQLITE_OK;
|
|
|
|
}
|
2014-09-02 23:59:40 +04:00
|
|
|
}else{
|
2014-09-05 23:31:15 +04:00
|
|
|
if( pIter->zIdx==0 ){
|
|
|
|
sqlite3_bind_text(pIter->pIdxIter, 1, pIter->zTbl, -1, SQLITE_STATIC);
|
|
|
|
}
|
|
|
|
rc = sqlite3_step(pIter->pIdxIter);
|
|
|
|
if( rc!=SQLITE_ROW ){
|
|
|
|
rc = sqlite3_reset(pIter->pIdxIter);
|
|
|
|
pIter->bCleanup = 1;
|
|
|
|
pIter->zIdx = 0;
|
|
|
|
}else{
|
|
|
|
pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
|
|
|
|
rc = SQLITE_OK;
|
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
|
|
|
}
|
2014-09-05 23:31:15 +04:00
|
|
|
pIter->iVisit++;
|
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
|
|
|
|
2014-09-05 23:52:42 +04:00
|
|
|
rc = prepareAndCollectError(p->db, &pIter->pTblIter, &p->zErrmsg,
|
|
|
|
"SELECT substr(name, 6) FROM ota.sqlite_master "
|
2014-09-05 23:31:15 +04:00
|
|
|
"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 ){
|
2014-09-05 23:52:42 +04:00
|
|
|
rc = prepareAndCollectError(p->db, &pIter->pIdxIter, &p->zErrmsg,
|
|
|
|
"SELECT name FROM main.sqlite_master "
|
2014-09-05 23:31:15 +04:00
|
|
|
"WHERE type='index' AND tbl_name = ?"
|
|
|
|
);
|
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
|
|
|
}
|
|
|
|
|
2014-09-05 23:31:15 +04:00
|
|
|
/*
|
|
|
|
** Allocate a buffer and populate it with the double-quoted version of the
|
|
|
|
** string in the argument buffer, suitable for use as an SQL identifier.
|
|
|
|
** For example:
|
|
|
|
**
|
2014-09-15 19:22:32 +04:00
|
|
|
** [quick `brown` fox] -> [`quick ``brown`` fox`]
|
2014-09-05 23:31:15 +04:00
|
|
|
**
|
|
|
|
** Assuming the allocation is successful, a pointer to the new buffer is
|
|
|
|
** returned. It is the responsibility of the caller to free it using
|
|
|
|
** sqlite3_free() at some point in the future. Or, if the allocation fails,
|
|
|
|
** a NULL pointer is returned.
|
|
|
|
*/
|
|
|
|
static char *otaQuoteName(const char *zName){
|
|
|
|
int nName = strlen(zName);
|
|
|
|
char *zRet = sqlite3_malloc(nName * 2 + 2 + 1);
|
|
|
|
if( zRet ){
|
|
|
|
int i;
|
|
|
|
char *p = zRet;
|
2014-09-15 19:22:32 +04:00
|
|
|
*p++ = '`';
|
2014-09-05 23:31:15 +04:00
|
|
|
for(i=0; i<nName; i++){
|
2014-09-15 19:22:32 +04:00
|
|
|
if( zName[i]=='`' ) *p++ = '`';
|
2014-09-05 23:31:15 +04:00
|
|
|
*p++ = zName[i];
|
|
|
|
}
|
2014-09-15 19:22:32 +04:00
|
|
|
*p++ = '`';
|
2014-09-05 23:31:15 +04:00
|
|
|
*p++ = '\0';
|
|
|
|
}
|
|
|
|
return zRet;
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
|
|
|
|
2014-11-27 21:09:46 +03:00
|
|
|
/*
|
|
|
|
** Argument zName points to a column name. Argument zQuoted also points
|
|
|
|
** to a column name, but one that has been quoted using otaQuoteName().
|
|
|
|
** Return true if the column names are the same, or false otherwise.
|
|
|
|
*/
|
|
|
|
static int otaMatchName(const char *zName, const char *zQuoted){
|
|
|
|
const char *p = zName;
|
|
|
|
const char *q = &zQuoted[1];
|
|
|
|
while( 1 ){
|
|
|
|
if( *q=='`' ) q++;
|
|
|
|
if( sqlite3_strnicmp(q, p, 1) ) return 0;
|
|
|
|
if( !*q ) break;
|
|
|
|
p++;
|
|
|
|
q++;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
static int otaMPrintfExec(sqlite3ota *p, const char *zFmt, ...){
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, zFmt);
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
char *zSql = sqlite3_vmprintf(zFmt, ap);
|
|
|
|
if( zSql==0 ){
|
|
|
|
p->rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
|
|
|
p->rc = sqlite3_exec(p->db, zSql, 0, 0, &p->zErrmsg);
|
|
|
|
sqlite3_free(zSql);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
return p->rc;
|
|
|
|
}
|
|
|
|
|
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){
|
|
|
|
int nByte = sizeof(char*) * nCol + sizeof(unsigned char*) * nCol;
|
|
|
|
char **azNew;
|
|
|
|
|
2014-11-21 13:46:23 +03:00
|
|
|
assert( p->rc==SQLITE_OK );
|
2014-11-27 21:09:46 +03:00
|
|
|
azNew = (char**)sqlite3_malloc(nByte);
|
|
|
|
if( azNew ){
|
|
|
|
memset(azNew, 0, nByte);
|
|
|
|
pIter->azTblCol = azNew;
|
|
|
|
pIter->abTblPk = (unsigned char*)&pIter->azTblCol[nCol];
|
|
|
|
}else{
|
|
|
|
p->rc = SQLITE_NOMEM;
|
2014-11-21 13:46:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return true if zTab is the name of a virtual table within the target
|
|
|
|
** database.
|
|
|
|
*/
|
|
|
|
static int otaIsVtab(sqlite3ota *p, const char *zTab){
|
|
|
|
int res = 0;
|
|
|
|
sqlite3_stmt *pSelect = 0;
|
|
|
|
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
p->rc = prepareAndCollectError(p->db, &pSelect, &p->zErrmsg,
|
|
|
|
"SELECT count(*) FROM sqlite_master WHERE name = ? AND type='table' "
|
|
|
|
"AND sql LIKE 'CREATE VIRTUAL TABLE%'"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
sqlite3_bind_text(pSelect, 1, zTab, -1, SQLITE_STATIC);
|
|
|
|
if( sqlite3_step(pSelect)==SQLITE_ROW ){
|
|
|
|
res = sqlite3_column_int(pSelect, 0);
|
|
|
|
}
|
|
|
|
p->rc = sqlite3_finalize(pSelect);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
** the table 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.
|
|
|
|
*/
|
|
|
|
static int otaObjIterGetCols(sqlite3ota *p, OtaObjIter *pIter){
|
|
|
|
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-09-05 23:31:15 +04:00
|
|
|
int rc2; /* sqlite3_finalize() return value */
|
2014-12-06 22:30:41 +03:00
|
|
|
int bOtaRowid = 0; /* If input table has column "ota_rowid" */
|
2014-09-05 23:31:15 +04:00
|
|
|
|
2014-12-06 22:30:41 +03:00
|
|
|
assert( pIter->eType==0 );
|
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_". */
|
|
|
|
p->rc = prepareFreeAndCollectError(p->db, &pStmt, &p->zErrmsg,
|
|
|
|
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) ){
|
|
|
|
char *zCopy = otaQuoteName(zName);
|
|
|
|
pIter->azTblCol[pIter->nTblCol++] = zCopy;
|
|
|
|
if( zCopy==0 ) p->rc = SQLITE_NOMEM;
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
/* Check that all non-HIDDEN columns in the destination table are also
|
|
|
|
** present in the input table. Populate the abTblPk[] array at the
|
|
|
|
** same time. */
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
p->rc = prepareFreeAndCollectError(p->db, &pStmt, &p->zErrmsg,
|
|
|
|
sqlite3_mprintf("PRAGMA main.table_info(%Q)", pIter->zTbl)
|
|
|
|
);
|
|
|
|
}
|
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);
|
|
|
|
for(i=0; i<pIter->nTblCol; i++){
|
|
|
|
if( otaMatchName(zName, pIter->azTblCol[i]) ) break;
|
|
|
|
}
|
|
|
|
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);
|
2014-11-27 21:09:46 +03:00
|
|
|
pIter->abTblPk[i] = (iPk!=0);
|
2014-12-06 22:30:41 +03:00
|
|
|
if( iPk ){
|
|
|
|
pIter->eType = (iPk<0) ? OTA_PK_EXTERNAL : OTA_PK_REAL;
|
|
|
|
}
|
2014-09-05 23:31:15 +04:00
|
|
|
}
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
2014-09-05 23:31:15 +04:00
|
|
|
rc2 = sqlite3_finalize(pStmt);
|
|
|
|
if( p->rc==SQLITE_OK ) p->rc = rc2;
|
|
|
|
|
2014-12-06 22:30:41 +03:00
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
if( pIter->eType==0 ){
|
|
|
|
/* This must either be a virtual table, or a regular table with no
|
|
|
|
** PRIMARY KEY declaration whatsoever. */
|
|
|
|
if( bOtaRowid==0 ){
|
|
|
|
p->rc = SQLITE_ERROR;
|
|
|
|
p->zErrmsg = sqlite3_mprintf(
|
|
|
|
"table data_%q requires ota_rowid column", pIter->zTbl
|
|
|
|
);
|
|
|
|
}else if( otaIsVtab(p, pIter->zTbl) ){
|
|
|
|
pIter->eType = OTA_PK_VTAB;
|
|
|
|
}else{
|
|
|
|
pIter->eType = OTA_PK_NONE;
|
|
|
|
}
|
|
|
|
}else if( bOtaRowid ){
|
2014-11-21 13:46:23 +03:00
|
|
|
p->rc = SQLITE_ERROR;
|
2014-12-06 22:30:41 +03:00
|
|
|
p->zErrmsg = sqlite3_mprintf(
|
|
|
|
"table data_%q may not have ota_rowid column", pIter->zTbl
|
|
|
|
);
|
2014-11-21 13:46:23 +03:00
|
|
|
}
|
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-11-20 22:19:02 +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.
|
|
|
|
*/
|
|
|
|
static char *otaMPrintfAndCollectError(sqlite3ota *p, const char *zFmt, ...){
|
|
|
|
char *zSql = 0;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, zFmt);
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
zSql = sqlite3_vmprintf(zFmt, ap);
|
|
|
|
if( zSql==0 ) p->rc = SQLITE_NOMEM;
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
return zSql;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
**
|
|
|
|
** If an OOM error is encountered, NULL is returned and an error code
|
|
|
|
** left in the OTA handle passed as the first argument. Otherwise, a pointer
|
|
|
|
** to the allocated string buffer is returned. It is the responsibility
|
|
|
|
** of the caller to eventually free this buffer using sqlite3_free().
|
|
|
|
**
|
|
|
|
** The number of column names to include in the returned string is passed
|
|
|
|
** as the third argument.
|
|
|
|
**
|
|
|
|
** If arguments aiCol and azCollate are both NULL, then the returned string
|
|
|
|
** contains the first nCol column names as a comma-separated list. For
|
|
|
|
** example:
|
|
|
|
**
|
|
|
|
** "a", "b", "c"
|
|
|
|
**
|
|
|
|
** If argument aiCol is not NULL, it must point to an array containing nCol
|
|
|
|
** entries - the index of each column name to include in the comma-separated
|
|
|
|
** list. For example, if aiCol[] contains {2, 0, 1), then the returned
|
|
|
|
** string is changed to:
|
|
|
|
**
|
|
|
|
** "c", "a", "b"
|
|
|
|
**
|
|
|
|
** If azCollate is not NULL, it must also point to an array containing nCol
|
|
|
|
** entries - collation sequence names to associated with each element of
|
|
|
|
** the comma separated list. For example, ef azCollate[] contains
|
|
|
|
** {"BINARY", "NOCASE", "REVERSE"}, then the retuned string is:
|
|
|
|
**
|
|
|
|
** "c" COLLATE "BINARY", "a" COLLATE "NOCASE", "b" COLLATE "REVERSE"
|
|
|
|
**
|
|
|
|
*/
|
2014-09-05 23:31:15 +04:00
|
|
|
static char *otaObjIterGetCollist(
|
2014-09-15 18:54:07 +04:00
|
|
|
sqlite3ota *p, /* OTA object */
|
|
|
|
OtaObjIter *pIter, /* Object iterator for column names */
|
|
|
|
int nCol, /* Number of column names */
|
|
|
|
int *aiCol, /* Array of nCol column indexes */
|
|
|
|
const char **azCollate /* Array of nCol collation sequence names */
|
2014-09-05 23:31:15 +04:00
|
|
|
){
|
|
|
|
char *zList = 0;
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
const char *zSep = "";
|
|
|
|
int i;
|
|
|
|
for(i=0; i<nCol; i++){
|
|
|
|
int iCol = aiCol ? aiCol[i] : i;
|
2014-11-20 20:37:08 +03:00
|
|
|
char *zCol = (iCol>=0 ? pIter->azTblCol[iCol] : "ota_rowid");
|
|
|
|
zList = sqlite3_mprintf("%z%s%s", zList, zSep, zCol);
|
2014-09-15 18:54:07 +04:00
|
|
|
if( zList && azCollate ){
|
|
|
|
zList = sqlite3_mprintf("%z COLLATE %Q", zList, azCollate[i]);
|
|
|
|
}
|
2014-09-05 23:31:15 +04:00
|
|
|
zSep = ", ";
|
|
|
|
if( zList==0 ){
|
|
|
|
p->rc = SQLITE_NOMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
if( p->rc==SQLITE_OK ){
|
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++){
|
2014-09-08 21:50:35 +04:00
|
|
|
zList = sqlite3_mprintf("%z%s%s.%s", zList, zS, zObj, pIter->azTblCol[i]);
|
|
|
|
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 ){
|
2014-11-20 20:37:08 +03:00
|
|
|
zList = sqlite3_mprintf("%z, %s._rowid_", zList, zObj);
|
|
|
|
}
|
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;
|
|
|
|
if( p->rc==SQLITE_OK ){
|
2014-12-06 22:30:41 +03:00
|
|
|
if( pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE ){
|
|
|
|
zList = otaMPrintfAndCollectError(p, "_rowid_ = ?%d", pIter->nTblCol+1);
|
2014-11-21 13:46:23 +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];
|
|
|
|
zList = otaMPrintfAndCollectError(
|
|
|
|
p, "%z%s%s=?%d", zList, zSep, zCol, i+1
|
|
|
|
);
|
|
|
|
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;
|
|
|
|
p->zErrmsg = sqlite3_mprintf("Invalid ota_control value");
|
|
|
|
}
|
|
|
|
|
2014-11-20 22:19:02 +03:00
|
|
|
|
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++){
|
2014-11-20 22:19:02 +03:00
|
|
|
char c = zMask[i];
|
|
|
|
if( c=='x' ){
|
|
|
|
zList = otaMPrintfAndCollectError(p, "%z%s%s=?%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' ){
|
|
|
|
zList = otaMPrintfAndCollectError(p, "%z%s%s=ota_delta(%s, ?%d)",
|
|
|
|
zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
|
|
|
|
);
|
2014-09-08 21:50:35 +04:00
|
|
|
zSep = ", ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return zList;
|
|
|
|
}
|
|
|
|
|
2014-09-05 23:31:15 +04:00
|
|
|
static char *otaObjIterGetBindlist(sqlite3ota *p, int nBind){
|
|
|
|
char *zRet = 0;
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
int nByte = nBind*2 + 1;
|
|
|
|
zRet = sqlite3_malloc(nByte);
|
|
|
|
if( zRet==0 ){
|
|
|
|
p->rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
|
|
|
int i;
|
|
|
|
for(i=0; i<nBind; i++){
|
|
|
|
zRet[i*2] = '?';
|
|
|
|
zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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 );
|
|
|
|
if( pIter->pSelect==0 && otaObjIterGetCols(p, pIter)==SQLITE_OK ){
|
|
|
|
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 ){
|
|
|
|
int *aiCol; /* Column map */
|
2014-09-15 18:54:07 +04:00
|
|
|
const char **azColl; /* Collation sequences */
|
2014-09-05 23:31:15 +04:00
|
|
|
|
2014-12-06 22:30:41 +03:00
|
|
|
assert( pIter->eType!=OTA_PK_VTAB );
|
2014-11-21 13:46:23 +03:00
|
|
|
|
2014-09-07 00:19:38 +04:00
|
|
|
/* Create the index writers */
|
2014-09-05 23:31:15 +04:00
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
p->rc = sqlite3_index_writer(
|
2014-09-15 18:54:07 +04:00
|
|
|
p->db, 0, zIdx, &pIter->pInsert, &azColl, &aiCol, &pIter->nCol
|
2014-09-05 23:31:15 +04:00
|
|
|
);
|
|
|
|
}
|
2014-09-07 00:19:38 +04:00
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
p->rc = sqlite3_index_writer(
|
2014-09-15 18:54:07 +04:00
|
|
|
p->db, 1, zIdx, &pIter->pDelete, &azColl, &aiCol, &pIter->nCol
|
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 */
|
2014-09-15 18:54:07 +04:00
|
|
|
zCollist = otaObjIterGetCollist(p, pIter, pIter->nCol, aiCol, azColl);
|
2014-09-05 23:31:15 +04:00
|
|
|
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(
|
|
|
|
"SELECT %s, ota_control FROM ota.'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(
|
2014-09-07 00:19:38 +04:00
|
|
|
"SELECT %s, ota_control FROM ota.'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 "
|
2014-09-07 00:19:38 +04:00
|
|
|
"SELECT %s, ota_control FROM ota.'ota_tmp_%q' "
|
|
|
|
"ORDER BY %s%s",
|
|
|
|
zCollist, pIter->zTbl,
|
|
|
|
zCollist, pIter->zTbl,
|
|
|
|
zCollist, zLimit
|
2014-11-20 20:37:08 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
p->rc = prepareFreeAndCollectError(p->db, &pIter->pSelect, pz, zSql);
|
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);
|
2014-11-21 13:46:23 +03:00
|
|
|
const char *zTbl = pIter->zTbl;
|
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
|
|
|
char *zBindings = otaObjIterGetBindlist(p, pIter->nTblCol + bOtaRowid);
|
|
|
|
|
2014-09-15 18:54:07 +04:00
|
|
|
zCollist = otaObjIterGetCollist(p, pIter, pIter->nTblCol, 0, 0);
|
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 ){
|
2014-09-05 23:52:42 +04:00
|
|
|
p->rc = prepareFreeAndCollectError(p->db, &pIter->pSelect, pz,
|
2014-09-05 23:31:15 +04:00
|
|
|
sqlite3_mprintf(
|
2014-11-21 13:46:23 +03:00
|
|
|
"SELECT %s, ota_control%s FROM ota.'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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the INSERT statement to write to the target PK b-tree */
|
|
|
|
if( p->rc==SQLITE_OK ){
|
2014-09-05 23:52:42 +04:00
|
|
|
p->rc = prepareFreeAndCollectError(p->db, &pIter->pInsert, pz,
|
2014-09-05 23:31:15 +04:00
|
|
|
sqlite3_mprintf(
|
2014-11-21 13:46:23 +03:00
|
|
|
"INSERT INTO main.%Q(%s%s) VALUES(%s)",
|
2014-12-06 22:30:41 +03:00
|
|
|
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 ){
|
|
|
|
p->rc = prepareFreeAndCollectError(p->db, &pIter->pDelete, pz,
|
|
|
|
sqlite3_mprintf(
|
2014-11-21 13:46:23 +03:00
|
|
|
"DELETE FROM main.%Q WHERE %s", zTbl, zWhere
|
2014-09-07 00:19:38 +04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-12-06 22:30:41 +03:00
|
|
|
if( pIter->eType!=OTA_PK_VTAB ){
|
|
|
|
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. */
|
2014-09-07 00:19:38 +04:00
|
|
|
otaMPrintfExec(p,
|
2014-11-21 13:46:23 +03:00
|
|
|
"PRAGMA ota_mode = 1;"
|
|
|
|
"CREATE TABLE IF NOT EXISTS ota.'ota_tmp_%q' AS "
|
|
|
|
"SELECT *%s FROM ota.'data_%q' WHERE 0;"
|
|
|
|
|
|
|
|
"CREATE TEMP TRIGGER ota_delete_%q BEFORE DELETE ON main.%Q "
|
2014-09-08 21:50:35 +04:00
|
|
|
"BEGIN "
|
2014-11-21 13:46:23 +03:00
|
|
|
" INSERT INTO 'ota_tmp_%q'(ota_control, %s%s) VALUES(2, %s);"
|
2014-09-08 21:50:35 +04:00
|
|
|
"END;"
|
2014-09-07 00:19:38 +04:00
|
|
|
|
2014-11-21 13:46:23 +03:00
|
|
|
"CREATE TEMP TRIGGER ota_update1_%q BEFORE UPDATE ON main.%Q "
|
|
|
|
"BEGIN "
|
|
|
|
" INSERT INTO 'ota_tmp_%q'(ota_control, %s%s) VALUES(2, %s);"
|
|
|
|
"END;"
|
2014-11-20 20:37:08 +03:00
|
|
|
|
2014-11-21 13:46:23 +03:00
|
|
|
"CREATE TEMP TRIGGER ota_update2_%q AFTER UPDATE ON main.%Q "
|
|
|
|
"BEGIN "
|
|
|
|
" INSERT INTO 'ota_tmp_%q'(ota_control, %s%s) VALUES(3, %s);"
|
|
|
|
"END;"
|
2014-12-06 22:30:41 +03:00
|
|
|
, zTbl, (pIter->eType==OTA_PK_EXTERNAL ? ", 0 AS ota_rowid" : "")
|
|
|
|
, zTbl,
|
2014-11-21 13:46:23 +03:00
|
|
|
zTbl, zTbl, zTbl, zCollist, zOtaRowid, zOldlist,
|
|
|
|
zTbl, zTbl, zTbl, zCollist, zOtaRowid, zOldlist,
|
|
|
|
zTbl, zTbl, zTbl, zCollist, zOtaRowid, zNewlist
|
|
|
|
);
|
2014-12-06 22:30:41 +03:00
|
|
|
if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
|
2014-11-21 13:46:23 +03:00
|
|
|
otaMPrintfExec(p,
|
|
|
|
"CREATE TEMP TRIGGER ota_insert_%q AFTER INSERT ON main.%Q "
|
|
|
|
"BEGIN "
|
|
|
|
" INSERT INTO 'ota_tmp_%q'(ota_control, %s, ota_rowid)"
|
|
|
|
" VALUES(0, %s);"
|
|
|
|
"END;"
|
|
|
|
, zTbl, zTbl, zTbl, zCollist, zNewlist
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}else if( p->rc==SQLITE_OK ){
|
|
|
|
p->rc = sqlite3_exec(p->db, "PRAGMA ota_mode = 0", 0, 0, &p->zErrmsg);
|
|
|
|
}
|
2014-11-20 20:37:08 +03:00
|
|
|
|
2014-09-08 21:50:35 +04:00
|
|
|
/* Allocate space required for the zMask field. */
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
int nMask = pIter->nTblCol+1;
|
|
|
|
pIter->zMask = (char*)sqlite3_malloc(nMask);
|
|
|
|
if( pIter->zMask==0 ){
|
|
|
|
p->rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
|
|
|
memset(pIter->zMask, 0, nMask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-09-07 00:19:38 +04:00
|
|
|
#define OTA_INSERT 1
|
|
|
|
#define OTA_DELETE 2
|
|
|
|
#define OTA_IDX_DELETE 3
|
2014-09-08 21:50:35 +04:00
|
|
|
#define OTA_IDX_INSERT 4
|
|
|
|
#define OTA_UPDATE 5
|
2014-09-07 00:19:38 +04:00
|
|
|
|
2014-09-08 21:50:35 +04:00
|
|
|
static int otaGetUpdateStmt(
|
|
|
|
sqlite3ota *p,
|
|
|
|
OtaObjIter *pIter,
|
|
|
|
const char *zMask,
|
|
|
|
sqlite3_stmt **ppStmt
|
|
|
|
){
|
|
|
|
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 ){
|
|
|
|
zUpdate = sqlite3_mprintf("UPDATE %Q SET %s WHERE %s",
|
|
|
|
pIter->zTbl, zSet, zWhere
|
|
|
|
);
|
|
|
|
p->rc = prepareFreeAndCollectError(
|
|
|
|
p->db, &pIter->pUpdate, &p->zErrmsg, zUpdate
|
|
|
|
);
|
|
|
|
*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
|
|
|
}
|
|
|
|
|
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 );
|
|
|
|
sqlite3_close(p->db);
|
|
|
|
p->db = 0;
|
|
|
|
|
|
|
|
p->rc = sqlite3_open(p->zTarget, &p->db);
|
|
|
|
if( p->rc ){
|
|
|
|
p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db));
|
|
|
|
}
|
|
|
|
otaMPrintfExec(p, "ATTACH %Q AS ota", p->zOta);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** 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
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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){
|
|
|
|
const char *zBase = sqlite3_db_filename(p->db, "main");
|
|
|
|
|
|
|
|
char *zWal = sqlite3_mprintf("%s-wal", zBase);
|
|
|
|
char *zOal = sqlite3_mprintf("%s-oal", zBase);
|
|
|
|
|
2014-10-21 23:35:03 +04:00
|
|
|
assert( p->eStage==OTA_STAGE_OAL );
|
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
|
|
|
|
** in WAL mode). So no other connection may be writing the db. */
|
|
|
|
otaFileSuffix3(zBase, zWal);
|
|
|
|
otaFileSuffix3(zBase, zOal);
|
|
|
|
rename(zOal, zWal);
|
|
|
|
|
|
|
|
/* Re-open the databases. */
|
|
|
|
otaObjIterFinalize(&p->objiter);
|
|
|
|
otaOpenDatabase(p);
|
|
|
|
p->eStage = OTA_STAGE_CKPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SQLITE_TEXT:
|
|
|
|
*pzMask = (const char*)sqlite3_column_text(p->objiter.pSelect, iCol);
|
|
|
|
res = OTA_UPDATE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( res==0 ){
|
|
|
|
otaBadControlError(p);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-12-06 22:30:41 +03:00
|
|
|
#ifdef SQLITE_DEBUG
|
|
|
|
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++){
|
2014-09-08 21:50:35 +04:00
|
|
|
if( eType==SQLITE_DELETE && pIter->zIdx==0 && pIter->abTblPk[i]==0 ){
|
|
|
|
continue;
|
|
|
|
}
|
2014-09-18 19:57:22 +04:00
|
|
|
pVal = sqlite3_column_value(pIter->pSelect, i);
|
2014-09-07 00:19:38 +04:00
|
|
|
sqlite3_bind_value(pWriter, i+1, pVal);
|
|
|
|
}
|
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);
|
|
|
|
sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
|
|
|
|
}
|
2014-09-07 00:19:38 +04:00
|
|
|
sqlite3_step(pWriter);
|
|
|
|
p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
|
2014-09-08 21:50:35 +04:00
|
|
|
}else if( eType==OTA_UPDATE ){
|
2014-11-21 13:46:23 +03:00
|
|
|
sqlite3_value *pVal;
|
2014-09-08 21:50:35 +04:00
|
|
|
sqlite3_stmt *pUpdate = 0;
|
|
|
|
otaGetUpdateStmt(p, pIter, zMask, &pUpdate);
|
|
|
|
if( pUpdate ){
|
|
|
|
for(i=0; i<pIter->nCol; i++){
|
2014-11-21 13:46:23 +03:00
|
|
|
pVal = sqlite3_column_value(pIter->pSelect, i);
|
2014-09-08 21:50:35 +04:00
|
|
|
sqlite3_bind_value(pUpdate, i+1, pVal);
|
2014-09-07 00:19:38 +04:00
|
|
|
}
|
2014-12-06 22:30:41 +03:00
|
|
|
if( pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE ){
|
|
|
|
/* 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);
|
|
|
|
sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
|
|
|
|
}
|
2014-09-08 21:50:35 +04:00
|
|
|
sqlite3_step(pUpdate);
|
|
|
|
p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
|
2014-09-07 00:19:38 +04:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
/* no-op */
|
|
|
|
assert( eType==OTA_DELETE && pIter->zIdx );
|
|
|
|
}
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
2014-09-05 23:31:15 +04:00
|
|
|
|
2014-09-02 23:59:40 +04:00
|
|
|
return p->rc;
|
|
|
|
}
|
|
|
|
|
2014-10-20 20:24:23 +04:00
|
|
|
/*
|
|
|
|
** Increment the schema cookie of the main database opened by p->db.
|
|
|
|
*/
|
|
|
|
static void otaIncrSchemaCookie(sqlite3ota *p){
|
|
|
|
int iCookie = 1000000;
|
|
|
|
sqlite3_stmt *pStmt;
|
|
|
|
|
|
|
|
assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
|
|
|
|
p->rc = prepareAndCollectError(p->db, &pStmt, &p->zErrmsg,
|
|
|
|
"PRAGMA schema_version"
|
|
|
|
);
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
if( SQLITE_ROW==sqlite3_step(pStmt) ){
|
|
|
|
iCookie = sqlite3_column_int(pStmt, 0);
|
|
|
|
}
|
|
|
|
p->rc = sqlite3_finalize(pStmt);
|
|
|
|
}
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
otaMPrintfExec(p, "PRAGMA schema_version = %d", iCookie+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
while( p && p->rc==SQLITE_OK && pIter->zTbl ){
|
|
|
|
|
|
|
|
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. */
|
2014-12-06 22:30:41 +03:00
|
|
|
if( pIter->eType!=OTA_PK_VTAB ){
|
2014-11-21 13:46:23 +03:00
|
|
|
otaMPrintfExec(p, "DELETE FROM ota.'ota_tmp_%q'", pIter->zTbl);
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( p->rc==SQLITE_OK && pIter->zTbl==0 ){
|
|
|
|
p->nProgress++;
|
|
|
|
otaIncrSchemaCookie(p);
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
p->rc = sqlite3_exec(p->db, "COMMIT", 0, 0, &p->zErrmsg);
|
|
|
|
}
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
otaMoveOalFile(p);
|
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: {
|
|
|
|
|
|
|
|
if( p->rc==SQLITE_OK && p->pCkpt==0 ){
|
|
|
|
p->rc = sqlite3_ckpt_open(p->db, 0, 0, &p->pCkpt);
|
|
|
|
}
|
|
|
|
if( p->rc==SQLITE_OK ){
|
2014-11-20 18:11:12 +03:00
|
|
|
if( p->pCkpt==0 ){
|
|
|
|
p->eStage = OTA_STAGE_DONE;
|
|
|
|
p->rc = SQLITE_DONE;
|
|
|
|
}else if( SQLITE_OK!=sqlite3_ckpt_step(p->pCkpt) ){
|
2014-10-20 20:24:23 +04:00
|
|
|
p->rc = sqlite3_ckpt_close(p->pCkpt, 0, 0);
|
|
|
|
p->pCkpt = 0;
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
p->eStage = OTA_STAGE_DONE;
|
|
|
|
p->rc = SQLITE_DONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p->nProgress++;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void otaSaveTransactionState(sqlite3ota *p){
|
2014-10-20 20:24:23 +04:00
|
|
|
sqlite3_stmt *pInsert;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert( (p->rc==SQLITE_OK || p->rc==SQLITE_DONE) && p->zErrmsg==0 );
|
|
|
|
rc = prepareFreeAndCollectError(p->db, &pInsert, &p->zErrmsg,
|
|
|
|
sqlite3_mprintf(
|
|
|
|
"INSERT OR REPLACE INTO ota.ota_state(k, v) VALUES "
|
|
|
|
"(%d, %d), "
|
|
|
|
"(%d, %Q), "
|
|
|
|
"(%d, %Q), "
|
|
|
|
"(%d, %d), "
|
|
|
|
"(%d, %lld), "
|
|
|
|
"(%d, ?) ",
|
|
|
|
OTA_STATE_STAGE, p->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
|
|
|
|
)
|
2014-09-03 23:30:32 +04:00
|
|
|
);
|
2014-10-20 20:24:23 +04:00
|
|
|
assert( pInsert==0 || rc==SQLITE_OK );
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
if( p->pCkpt ){
|
|
|
|
unsigned char *pCkptState = 0;
|
|
|
|
int nCkptState = 0;
|
|
|
|
rc = sqlite3_ckpt_close(p->pCkpt, &pCkptState, &nCkptState);
|
|
|
|
p->pCkpt = 0;
|
|
|
|
sqlite3_bind_blob(pInsert, 1, pCkptState, nCkptState, SQLITE_TRANSIENT);
|
|
|
|
sqlite3_free(pCkptState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
sqlite3_step(pInsert);
|
|
|
|
rc = sqlite3_finalize(pInsert);
|
|
|
|
}else{
|
|
|
|
sqlite3_finalize(pInsert);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
p->rc = rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *otaStrndup(char *zStr, int nStr, int *pRc){
|
|
|
|
char *zRet = 0;
|
|
|
|
assert( *pRc==SQLITE_OK );
|
|
|
|
|
|
|
|
if( zStr ){
|
|
|
|
int nCopy = nStr;
|
|
|
|
if( nCopy<0 ) nCopy = strlen(zStr) + 1;
|
|
|
|
zRet = (char*)sqlite3_malloc(nCopy);
|
|
|
|
if( zRet ){
|
|
|
|
memcpy(zRet, zStr, nCopy);
|
|
|
|
}else{
|
|
|
|
*pRc = SQLITE_NOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return zRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
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->pCkptState);
|
|
|
|
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){
|
2014-10-20 20:24:23 +04:00
|
|
|
const char *zSelect = "SELECT k, v FROM ota.ota_state";
|
2014-09-03 23:30:32 +04:00
|
|
|
OtaState *pRet = 0;
|
|
|
|
sqlite3_stmt *pStmt;
|
|
|
|
int rc;
|
2014-10-20 20:24:23 +04:00
|
|
|
int rc2;
|
2014-09-03 23:30:32 +04:00
|
|
|
|
|
|
|
assert( p->rc==SQLITE_OK );
|
2014-10-20 20:24:23 +04:00
|
|
|
pRet = (OtaState*)sqlite3_malloc(sizeof(OtaState));
|
|
|
|
if( pRet==0 ){
|
|
|
|
rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
|
|
|
memset(pRet, 0, sizeof(OtaState));
|
|
|
|
rc = prepareAndCollectError(p->db, &pStmt, &p->zErrmsg, zSelect);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
&& pRet->eStage!=OTA_STAGE_COPY
|
|
|
|
&& 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:
|
|
|
|
pRet->zTbl = otaStrndup((char*)sqlite3_column_text(pStmt, 1), -1, &rc);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OTA_STATE_IDX:
|
|
|
|
pRet->zIdx = otaStrndup((char*)sqlite3_column_text(pStmt, 1), -1, &rc);
|
|
|
|
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:
|
|
|
|
pRet->nCkptState = sqlite3_column_bytes(pStmt, 1);
|
|
|
|
pRet->pCkptState = (unsigned char*)otaStrndup(
|
|
|
|
(char*)sqlite3_column_blob(pStmt, 1), pRet->nCkptState, &rc
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-09-03 23:30:32 +04:00
|
|
|
static void otaLoadTransactionState(sqlite3ota *p, OtaState *pState){
|
|
|
|
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->zTbl, pState->zTbl)
|
|
|
|
|| otaStrCompare(pIter->zIdx, pState->zIdx)
|
|
|
|
)){
|
|
|
|
rc = otaObjIterNext(p, &p->objiter);
|
2014-09-03 23:30:32 +04:00
|
|
|
}
|
2014-09-05 23:31:15 +04:00
|
|
|
|
|
|
|
if( rc==SQLITE_OK && !p->objiter.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
|
|
|
|
|
|
|
/*
|
|
|
|
** 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
|
|
|
|
|
|
|
/* Open the target database */
|
|
|
|
memset(p, 0, sizeof(sqlite3ota));
|
2014-09-03 23:30:32 +04:00
|
|
|
p->zTarget = (char*)&p[1];
|
|
|
|
memcpy(p->zTarget, zTarget, nTarget+1);
|
2014-10-20 20:24:23 +04:00
|
|
|
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 ){
|
2014-09-05 23:52:42 +04:00
|
|
|
p->rc = sqlite3_exec(p->db, 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 ){
|
2014-10-20 20:24:23 +04:00
|
|
|
if( pState->eStage==0 ){
|
|
|
|
otaDeleteOalFile(p);
|
|
|
|
p->eStage = 1;
|
|
|
|
}else{
|
|
|
|
p->eStage = pState->eStage;
|
|
|
|
}
|
|
|
|
p->nProgress = pState->nProgress;
|
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
|
|
|
|
2014-10-21 22:09:58 +04:00
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
if( p->eStage==OTA_STAGE_OAL ){
|
2014-10-20 20:24:23 +04:00
|
|
|
const char *zScript =
|
|
|
|
"PRAGMA journal_mode=off;"
|
|
|
|
"PRAGMA pager_ota_mode=1;"
|
|
|
|
"PRAGMA ota_mode=1;"
|
|
|
|
"BEGIN IMMEDIATE;"
|
2014-10-21 22:09:58 +04:00
|
|
|
;
|
2014-10-20 20:24:23 +04:00
|
|
|
p->rc = sqlite3_exec(p->db, zScript, 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
otaLoadTransactionState(p, pState);
|
|
|
|
}
|
|
|
|
}else if( p->eStage==OTA_STAGE_CKPT ){
|
|
|
|
p->rc = sqlite3_ckpt_open(
|
|
|
|
p->db, pState->pCkptState, pState->nCkptState, &p->pCkpt
|
|
|
|
);
|
2014-11-20 18:11:12 +03:00
|
|
|
if( p->rc==SQLITE_MISMATCH || (p->rc==SQLITE_OK && p->pCkpt==0) ){
|
2014-10-21 22:09:58 +04:00
|
|
|
p->eStage = OTA_STAGE_DONE;
|
|
|
|
p->rc = SQLITE_DONE;
|
|
|
|
}
|
|
|
|
}else if( p->eStage==OTA_STAGE_DONE ){
|
|
|
|
p->rc = SQLITE_DONE;
|
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.
|
|
|
|
*/
|
|
|
|
sqlite3 *sqlite3ota_db(sqlite3ota *pOta){
|
|
|
|
return (pOta ? pOta->db : 0);
|
|
|
|
}
|
|
|
|
|
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 ){
|
|
|
|
|
|
|
|
/* If the update has not been fully applied, save the state in
|
|
|
|
** the ota db. If successful, this call also commits the open
|
|
|
|
** transaction on the ota db. */
|
|
|
|
assert( p->rc!=SQLITE_ROW );
|
2014-10-20 20:24:23 +04:00
|
|
|
if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
|
2014-09-02 23:59:40 +04:00
|
|
|
assert( p->zErrmsg==0 );
|
|
|
|
otaSaveTransactionState(p);
|
|
|
|
}
|
|
|
|
|
2014-10-20 20:24:23 +04:00
|
|
|
/* Close any open statement handles. */
|
2014-09-05 23:31:15 +04:00
|
|
|
otaObjIterFinalize(&p->objiter);
|
2014-09-02 23:59:40 +04:00
|
|
|
|
2014-09-03 23:30:32 +04:00
|
|
|
/* Commit the transaction to the *-oal file. */
|
2014-10-20 20:24:23 +04:00
|
|
|
if( p->rc==SQLITE_OK && p->eStage==OTA_STAGE_OAL ){
|
|
|
|
p->rc = sqlite3_exec(p->db, "COMMIT", 0, 0, &p->zErrmsg);
|
2014-09-02 23:59:40 +04:00
|
|
|
}
|
2014-09-03 23:30:32 +04:00
|
|
|
|
2014-10-20 20:24:23 +04:00
|
|
|
if( p->rc==SQLITE_OK && p->eStage==OTA_STAGE_CKPT ){
|
|
|
|
p->rc = sqlite3_exec(p->db, "PRAGMA pager_ota_mode=2", 0, 0, &p->zErrmsg);
|
2014-09-03 23:30:32 +04:00
|
|
|
}
|
2014-09-02 23:59:40 +04:00
|
|
|
|
2014-10-20 20:24:23 +04:00
|
|
|
/* Close the open database handle */
|
|
|
|
if( p->pCkpt ) sqlite3_ckpt_close(p->pCkpt, 0, 0);
|
|
|
|
sqlite3_close(p->db);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-09-02 23:59:40 +04:00
|
|
|
|
|
|
|
/**************************************************************************/
|
|
|
|
|
|
|
|
#ifdef SQLITE_TEST
|
|
|
|
|
|
|
|
#include <tcl.h>
|
|
|
|
|
|
|
|
/* From main.c (apparently...) */
|
|
|
|
extern const char *sqlite3ErrName(int);
|
|
|
|
|
2014-11-20 22:19:02 +03:00
|
|
|
void test_ota_delta(sqlite3_context *pCtx, int nArg, sqlite3_value **apVal){
|
|
|
|
Tcl_Interp *interp = (Tcl_Interp*)sqlite3_user_data(pCtx);
|
|
|
|
Tcl_Obj *pScript;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
pScript = Tcl_NewObj();
|
|
|
|
Tcl_IncrRefCount(pScript);
|
|
|
|
Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj("ota_delta", -1));
|
|
|
|
for(i=0; i<nArg; i++){
|
|
|
|
sqlite3_value *pIn = apVal[i];
|
|
|
|
const char *z = (const char*)sqlite3_value_text(pIn);
|
|
|
|
Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(z, -1));
|
|
|
|
}
|
|
|
|
|
|
|
|
if( TCL_OK==Tcl_EvalObjEx(interp, pScript, TCL_GLOBAL_ONLY) ){
|
|
|
|
const char *z = Tcl_GetStringResult(interp);
|
|
|
|
sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
|
|
|
|
}else{
|
|
|
|
Tcl_BackgroundError(interp);
|
|
|
|
}
|
|
|
|
|
|
|
|
Tcl_DecrRefCount(pScript);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-02 23:59:40 +04:00
|
|
|
static int test_sqlite3ota_cmd(
|
|
|
|
ClientData clientData,
|
|
|
|
Tcl_Interp *interp,
|
|
|
|
int objc,
|
|
|
|
Tcl_Obj *CONST objv[]
|
|
|
|
){
|
|
|
|
int ret = TCL_OK;
|
|
|
|
sqlite3ota *pOta = (sqlite3ota*)clientData;
|
2014-11-20 22:19:02 +03:00
|
|
|
const char *azMethod[] = { "step", "close", "create_ota_delta", 0 };
|
2014-09-02 23:59:40 +04:00
|
|
|
int iMethod;
|
|
|
|
|
|
|
|
if( objc!=2 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 1, objv, "METHOD");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
if( Tcl_GetIndexFromObj(interp, objv[1], azMethod, "method", 0, &iMethod) ){
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch( iMethod ){
|
|
|
|
case 0: /* step */ {
|
|
|
|
int rc = sqlite3ota_step(pOta);
|
|
|
|
Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 1: /* close */ {
|
|
|
|
char *zErrmsg = 0;
|
|
|
|
int rc;
|
|
|
|
Tcl_DeleteCommand(interp, Tcl_GetString(objv[0]));
|
|
|
|
rc = sqlite3ota_close(pOta, &zErrmsg);
|
|
|
|
if( rc==SQLITE_OK || rc==SQLITE_DONE ){
|
|
|
|
Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
|
|
|
|
assert( zErrmsg==0 );
|
|
|
|
}else{
|
|
|
|
Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
|
|
|
|
if( zErrmsg ){
|
|
|
|
Tcl_AppendResult(interp, " - ", zErrmsg, 0);
|
|
|
|
sqlite3_free(zErrmsg);
|
|
|
|
}
|
|
|
|
ret = TCL_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-20 22:19:02 +03:00
|
|
|
case 2: /* create_ota_delta */ {
|
|
|
|
sqlite3 *db = sqlite3ota_db(pOta);
|
|
|
|
int rc = sqlite3_create_function(
|
|
|
|
db, "ota_delta", -1, SQLITE_UTF8, (void*)interp, test_ota_delta, 0, 0
|
|
|
|
);
|
|
|
|
Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
|
|
|
|
ret = (rc==SQLITE_OK ? TCL_OK : TCL_ERROR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-09-02 23:59:40 +04:00
|
|
|
default: /* seems unlikely */
|
|
|
|
assert( !"cannot happen" );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Tclcmd: sqlite3ota CMD <target-db> <ota-db>
|
|
|
|
*/
|
|
|
|
static int test_sqlite3ota(
|
|
|
|
ClientData clientData,
|
|
|
|
Tcl_Interp *interp,
|
|
|
|
int objc,
|
|
|
|
Tcl_Obj *CONST objv[]
|
|
|
|
){
|
|
|
|
sqlite3ota *pOta = 0;
|
|
|
|
const char *zCmd;
|
|
|
|
const char *zTarget;
|
|
|
|
const char *zOta;
|
|
|
|
|
|
|
|
if( objc!=4 ){
|
|
|
|
Tcl_WrongNumArgs(interp, 1, objv, "NAME TARGET-DB OTA-DB");
|
|
|
|
return TCL_ERROR;
|
|
|
|
}
|
|
|
|
zCmd = Tcl_GetString(objv[1]);
|
|
|
|
zTarget = Tcl_GetString(objv[2]);
|
|
|
|
zOta = Tcl_GetString(objv[3]);
|
|
|
|
|
|
|
|
pOta = sqlite3ota_open(zTarget, zOta);
|
|
|
|
Tcl_CreateObjCommand(interp, zCmd, test_sqlite3ota_cmd, (ClientData)pOta, 0);
|
|
|
|
Tcl_SetObjResult(interp, objv[1]);
|
|
|
|
return TCL_OK;
|
|
|
|
}
|
|
|
|
|
2014-11-22 12:09:50 +03:00
|
|
|
|
2014-09-02 23:59:40 +04:00
|
|
|
int SqliteOta_Init(Tcl_Interp *interp){
|
|
|
|
Tcl_CreateObjCommand(interp, "sqlite3ota", test_sqlite3ota, 0, 0);
|
|
|
|
return TCL_OK;
|
|
|
|
}
|
|
|
|
#endif /* ifdef SQLITE_TEST */
|
2014-11-22 12:09:50 +03:00
|
|
|
#else /* !SQLITE_CORE || SQLITE_ENABLE_OTA */
|
|
|
|
# ifdef SQLITE_TEST
|
|
|
|
#include <tcl.h>
|
|
|
|
int SqliteOta_Init(Tcl_Interp *interp){ return TCL_OK; }
|
|
|
|
# endif
|
|
|
|
#endif
|
2014-09-02 23:59:40 +04:00
|
|
|
|
|
|
|
|
|
|
|
|