/ Check-in [ffa1524e]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add a command line program that uses the extension. This serves as example code and is also useful for performance testing.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | ota-update
Files: files | file ages | folders
SHA1: ffa1524ef2a4c32652183eb4745685f0d1c93af2
User & Date: dan 2014-09-03 08:25:09
Context
2014-09-03
19:30
Split part of "PRAGMA ota_mode" off into "PRAGMA pager_ota_mode". This allows some specialized custom VFS implementations to intercept and implement the expected pager-related effects of this pragma. check-in: 209f672e user: dan tags: ota-update
08:25
Add a command line program that uses the extension. This serves as example code and is also useful for performance testing. check-in: ffa1524e user: dan tags: ota-update
2014-09-02
19:59
Add an experimental extension for applying bulk updates to databases. check-in: 2954ab50 user: dan tags: ota-update
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Added ext/ota/ota.c.

            1  +/*
            2  +** 2014 August 30
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +**
           13  +** This file contains a command-line application that uses the OTA 
           14  +** extension. See the usage() function below for an explanation.
           15  +*/
           16  +
           17  +#include "sqlite3ota.h"
           18  +#include <stdio.h>
           19  +#include <stdlib.h>
           20  +#include <string.h>
           21  +
           22  +/*
           23  +** Print a usage message and exit.
           24  +*/
           25  +void usage(const char *zArgv0){
           26  +  fprintf(stderr, 
           27  +"Usage: %s [-step NSTEP] TARGET-DB OTA-DB\n"
           28  +"\n"
           29  +"  Argument OTA-DB must be an OTA database containing an update suitable for\n"
           30  +"  target database TARGET-DB. If NSTEP is set to less than or equal to zero\n"
           31  +"  (the default value), this program attempts to apply the entire update to\n"
           32  +"  the target database.\n"
           33  +"\n"
           34  +"  If NSTEP is greater than zero, then a maximum of NSTEP calls are made\n"
           35  +"  to sqlite3ota_step(). If the OTA update has not been completely applied\n"
           36  +"  after the NSTEP'th call is made, the state is saved in the database OTA-DB\n"
           37  +"  and the program exits. Subsequent invocations of this (or any other OTA)\n"
           38  +"  application will use this state to resume applying the OTA update to the\n"
           39  +"  target db.\n"
           40  +"\n"
           41  +, zArgv0);
           42  +  exit(1);
           43  +}
           44  +
           45  +int main(int argc, char **argv){
           46  +  int i;
           47  +  const char *zTarget;            /* Target database to apply OTA to */
           48  +  const char *zOta;               /* Database containing OTA */
           49  +  char *zErrmsg;                  /* Error message, if any */
           50  +  sqlite3ota *pOta;               /* OTA handle */
           51  +  int nStep = 0;                  /* Maximum number of step() calls */
           52  +  int rc;
           53  +
           54  +  /* Process command line arguments. Following this block local variables 
           55  +  ** zTarget, zOta and nStep are all set. */
           56  +  if( argc==5 ){
           57  +    int nArg1 = strlen(argv[1]);
           58  +    if( nArg1>5 || nArg1<2 || memcmp("-step", argv[1], nArg1) ) usage(argv[0]);
           59  +    nStep = atoi(argv[2]);
           60  +  }else if( argc!=3 ){
           61  +    usage(argv[0]);
           62  +  }
           63  +  zTarget = argv[argc-2];
           64  +  zOta = argv[argc-1];
           65  +
           66  +  /* Open an OTA handle. If nStep is less than or equal to zero, call
           67  +  ** sqlite3ota_step() until either the OTA has been completely applied
           68  +  ** or an error occurs. Or, if nStep is greater than zero, call
           69  +  ** sqlite3ota_step() a maximum of nStep times.  */
           70  +  pOta = sqlite3ota_open(zTarget, zOta);
           71  +  for(i=0; (nStep<=0 || i<nStep) && sqlite3ota_step(pOta)==SQLITE_OK; i++);
           72  +  rc = sqlite3ota_close(pOta, &zErrmsg);
           73  +
           74  +  /* Let the user know what happened. */
           75  +  switch( rc ){
           76  +    case SQLITE_OK:
           77  +      fprintf(stdout, "SQLITE_OK: ota update incomplete\n");
           78  +      break;
           79  +
           80  +    case SQLITE_DONE:
           81  +      fprintf(stdout, "SQLITE_DONE: ota update completed\n");
           82  +      break;
           83  +
           84  +    default:
           85  +      fprintf(stderr, "error=%d: %s\n", rc, zErrmsg);
           86  +      break;
           87  +  }
           88  +
           89  +  sqlite3_free(zErrmsg);
           90  +  return (rc==SQLITE_OK || rc==SQLITE_DONE) ? 0 : 1;
           91  +}
           92  +

Changes to ext/ota/sqlite3ota.h.

   103    103   ** For each row to DELETE from the target database as part of the OTA 
   104    104   ** update, the corresponding data_% table should contain a single record
   105    105   ** with the "ota_control" column set to contain integer value 1. The
   106    106   ** real primary key values of the row to delete should be stored in the
   107    107   ** corresponding columns of the data_% table. The values stored in the
   108    108   ** other columns are not used.
   109    109   **
   110         -** For each row to DELETE from the target database as part of the OTA 
          110  +** For each row to UPDATE from the target database as part of the OTA 
   111    111   ** update, the corresponding data_% table should contain a single record
   112    112   ** with the "ota_control" column set to contain a value of type text.
   113    113   ** The real primary key values identifying the row to update should be 
   114    114   ** stored in the corresponding columns of the data_% table row, as should
   115    115   ** the new values of all columns being update. The text value in the 
   116    116   ** "ota_control" column must contain the same number of characters as
   117    117   ** there are column in the target database table, and must consist entirely
................................................................................
   158    158   ** To remove all OTA extension state information, returning an OTA database 
   159    159   ** to its original contents, it is sufficient to drop all tables that begin
   160    160   ** with the prefix "ota_"
   161    161   */
   162    162   
   163    163   #ifndef _SQLITE3OTA_H
   164    164   #define _SQLITE3OTA_H
          165  +
          166  +#include <sqlite3.h>              /* Required for error code definitions */
   165    167   
   166    168   typedef struct sqlite3ota sqlite3ota;
   167    169   
   168    170   /*
   169    171   ** Open an OTA handle.
   170    172   **
   171    173   ** Argument zTarget is the path to the target database. Argument zOta is

Changes to main.mk.

   660    660   
   661    661   wordcount$(EXE):	$(TOP)/test/wordcount.c sqlite3.c
   662    662   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o wordcount$(EXE) \
   663    663   		$(TOP)/test/wordcount.c sqlite3.c
   664    664   
   665    665   speedtest1$(EXE):	$(TOP)/test/speedtest1.c sqlite3.o
   666    666   	$(TCC) -I. -o speedtest1$(EXE) $(TOP)/test/speedtest1.c sqlite3.o $(THREADLIB)
          667  +
          668  +ota$(EXE): $(TOP)/ext/ota/ota.c $(TOP)/ext/ota/sqlite3ota.c sqlite3.o 
          669  +	$(TCC) -I. -o ota$(EXE) \
          670  +	  $(TOP)/ext/ota/ota.c $(TOP)/ext/ota/sqlite3ota.c sqlite3.o \
          671  +	  $(THREADLIB)
   667    672   
   668    673   # This target will fail if the SQLite amalgamation contains any exported
   669    674   # symbols that do not begin with "sqlite3_". It is run as part of the
   670    675   # releasetest.tcl script.
   671    676   #
   672    677   checksymbols: sqlite3.o
   673    678   	nm -g --defined-only sqlite3.o | grep -v " sqlite3_" ; test $$? -ne 0