OLD | NEW |
(Empty) | |
| 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 RBU |
| 14 ** extension. See the usage() function below for an explanation. |
| 15 */ |
| 16 |
| 17 #include "sqlite3rbu.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 RBU-DB\n" |
| 28 "\n" |
| 29 " Argument RBU-DB must be an RBU 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 sqlite3rbu_step(). If the RBU update has not been completely applied\n" |
| 36 " after the NSTEP'th call is made, the state is saved in the database RBU-DB\n" |
| 37 " and the program exits. Subsequent invocations of this (or any other RBU)\n" |
| 38 " application will use this state to resume applying the RBU update to the\n" |
| 39 " target db.\n" |
| 40 "\n" |
| 41 , zArgv0); |
| 42 exit(1); |
| 43 } |
| 44 |
| 45 void report_default_vfs(){ |
| 46 sqlite3_vfs *pVfs = sqlite3_vfs_find(0); |
| 47 fprintf(stdout, "default vfs is \"%s\"\n", pVfs->zName); |
| 48 } |
| 49 |
| 50 void report_rbu_vfs(sqlite3rbu *pRbu){ |
| 51 sqlite3 *db = sqlite3rbu_db(pRbu, 0); |
| 52 if( db ){ |
| 53 char *zName = 0; |
| 54 sqlite3_file_control(db, "main", SQLITE_FCNTL_VFSNAME, &zName); |
| 55 if( zName ){ |
| 56 fprintf(stdout, "using vfs \"%s\"\n", zName); |
| 57 }else{ |
| 58 fprintf(stdout, "vfs name not available\n"); |
| 59 } |
| 60 sqlite3_free(zName); |
| 61 } |
| 62 } |
| 63 |
| 64 int main(int argc, char **argv){ |
| 65 int i; |
| 66 const char *zTarget; /* Target database to apply RBU to */ |
| 67 const char *zRbu; /* Database containing RBU */ |
| 68 char zBuf[200]; /* Buffer for printf() */ |
| 69 char *zErrmsg; /* Error message, if any */ |
| 70 sqlite3rbu *pRbu; /* RBU handle */ |
| 71 int nStep = 0; /* Maximum number of step() calls */ |
| 72 int rc; |
| 73 sqlite3_int64 nProgress = 0; |
| 74 |
| 75 /* Process command line arguments. Following this block local variables |
| 76 ** zTarget, zRbu and nStep are all set. */ |
| 77 if( argc==5 ){ |
| 78 int nArg1 = strlen(argv[1]); |
| 79 if( nArg1>5 || nArg1<2 || memcmp("-step", argv[1], nArg1) ) usage(argv[0]); |
| 80 nStep = atoi(argv[2]); |
| 81 }else if( argc!=3 ){ |
| 82 usage(argv[0]); |
| 83 } |
| 84 zTarget = argv[argc-2]; |
| 85 zRbu = argv[argc-1]; |
| 86 |
| 87 report_default_vfs(); |
| 88 |
| 89 /* Open an RBU handle. If nStep is less than or equal to zero, call |
| 90 ** sqlite3rbu_step() until either the RBU has been completely applied |
| 91 ** or an error occurs. Or, if nStep is greater than zero, call |
| 92 ** sqlite3rbu_step() a maximum of nStep times. */ |
| 93 pRbu = sqlite3rbu_open(zTarget, zRbu, 0); |
| 94 report_rbu_vfs(pRbu); |
| 95 for(i=0; (nStep<=0 || i<nStep) && sqlite3rbu_step(pRbu)==SQLITE_OK; i++); |
| 96 nProgress = sqlite3rbu_progress(pRbu); |
| 97 rc = sqlite3rbu_close(pRbu, &zErrmsg); |
| 98 |
| 99 /* Let the user know what happened. */ |
| 100 switch( rc ){ |
| 101 case SQLITE_OK: |
| 102 sqlite3_snprintf(sizeof(zBuf), zBuf, |
| 103 "SQLITE_OK: rbu update incomplete (%lld operations so far)\n", |
| 104 nProgress |
| 105 ); |
| 106 fprintf(stdout, zBuf); |
| 107 break; |
| 108 |
| 109 case SQLITE_DONE: |
| 110 sqlite3_snprintf(sizeof(zBuf), zBuf, |
| 111 "SQLITE_DONE: rbu update completed (%lld operations)\n", |
| 112 nProgress |
| 113 ); |
| 114 fprintf(stdout, zBuf); |
| 115 break; |
| 116 |
| 117 default: |
| 118 fprintf(stderr, "error=%d: %s\n", rc, zErrmsg); |
| 119 break; |
| 120 } |
| 121 |
| 122 sqlite3_free(zErrmsg); |
| 123 return (rc==SQLITE_OK || rc==SQLITE_DONE) ? 0 : 1; |
| 124 } |
| 125 |
OLD | NEW |