Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/ext/recover/test_recover.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / ext / recover / test_recover.c` 309 lines ยท 8445 bytes Raw Blame History 1 /* 2 ** 2022-08-27 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 */ 14 15 #include "sqlite3recover.h" 16 #include "sqliteInt.h" 17 #include "tclsqlite.h" 18 #include 19 20 #ifndef SQLITE_OMIT_VIRTUALTABLE 21 22 typedef struct TestRecover TestRecover; 23 struct TestRecover { 24 sqlite3_recover *p; 25 Tcl_Interp *interp; 26 Tcl_Obj *pScript; 27 }; 28 29 static int xSqlCallback(void *pSqlArg, const char *zSql){ 30 TestRecover *p = (TestRecover*)pSqlArg; 31 Tcl_Obj *pEval = 0; 32 int res = 0; 33 34 pEval = Tcl_DuplicateObj(p->pScript); 35 Tcl_IncrRefCount(pEval); 36 37 res = Tcl_ListObjAppendElement(p->interp, pEval, Tcl_NewStringObj(zSql, -1)); 38 if( res==TCL_OK ){ 39 res = Tcl_EvalObjEx(p->interp, pEval, 0); 40 } 41 42 Tcl_DecrRefCount(pEval); 43 if( res ){ 44 Tcl_BackgroundError(p->interp); 45 return TCL_ERROR; 46 }else{ 47 Tcl_Obj *pObj = Tcl_GetObjResult(p->interp); 48 if( Tcl_GetCharLength(pObj)==0 ){ 49 res = 0; 50 }else if( Tcl_GetIntFromObj(p->interp, pObj, &res) ){ 51 Tcl_BackgroundError(p->interp); 52 return TCL_ERROR; 53 } 54 } 55 return res; 56 } 57 58 static int getDbPointer(Tcl_Interp *interp, Tcl_Obj *pObj, sqlite3 **pDb){ 59 Tcl_CmdInfo info; 60 if( 0==Tcl_GetCommandInfo(interp, Tcl_GetString(pObj), &info) ){ 61 Tcl_AppendResult(interp, "no such handle: ", Tcl_GetString(pObj), NULL); 62 return TCL_ERROR; 63 } 64 *pDb = *(sqlite3 **)info.objClientData; 65 return TCL_OK; 66 } 67 68 /* 69 ** Implementation of the command created by [sqlite3_recover_init]: 70 ** 71 ** $cmd config OP ARG 72 ** $cmd run 73 ** $cmd errmsg 74 ** $cmd errcode 75 ** $cmd finalize 76 */ 77 static int testRecoverCmd( 78 void *clientData, 79 Tcl_Interp *interp, 80 int objc, 81 Tcl_Obj *CONST objv[] 82 ){ 83 static struct RecoverSub { 84 const char *zSub; 85 int nArg; 86 const char *zMsg; 87 } aSub[] = { 88 { "config", 2, "ARG" }, /* 0 */ 89 { "run", 0, "" }, /* 1 */ 90 { "errmsg", 0, "" }, /* 2 */ 91 { "errcode", 0, "" }, /* 3 */ 92 { "finish", 0, "" }, /* 4 */ 93 { "step", 0, "" }, /* 5 */ 94 { 0 } 95 }; 96 int rc = TCL_OK; 97 int iSub = 0; 98 TestRecover *pTest = (TestRecover*)clientData; 99 100 if( objc<2 ){ 101 Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); 102 return TCL_ERROR; 103 } 104 rc = Tcl_GetIndexFromObjStruct(interp, 105 objv[1], aSub, sizeof(aSub[0]), "sub-command", 0, &iSub 106 ); 107 if( rc!=TCL_OK ) return rc; 108 if( (objc-2)!=aSub[iSub].nArg ){ 109 Tcl_WrongNumArgs(interp, 2, objv, aSub[iSub].zMsg); 110 return TCL_ERROR; 111 } 112 113 switch( iSub ){ 114 case 0: assert( sqlite3_stricmp("config", aSub[iSub].zSub)==0 ); { 115 const char *aOp[] = { 116 "testdb", /* 0 */ 117 "lostandfound", /* 1 */ 118 "freelistcorrupt", /* 2 */ 119 "rowids", /* 3 */ 120 "slowindexes", /* 4 */ 121 "invalid", /* 5 */ 122 0 123 }; 124 int iOp = 0; 125 int res = 0; 126 if( Tcl_GetIndexFromObj(interp, objv[2], aOp, "option", 0, &iOp) ){ 127 return TCL_ERROR; 128 } 129 switch( iOp ){ 130 case 0: 131 res = sqlite3_recover_config(pTest->p, 132 789, (void*)Tcl_GetString(objv[3]) /* MAGIC NUMBER! */ 133 ); 134 break; 135 case 1: { 136 const char *zStr = Tcl_GetString(objv[3]); 137 res = sqlite3_recover_config(pTest->p, 138 SQLITE_RECOVER_LOST_AND_FOUND, (void*)(zStr[0] ? zStr : 0) 139 ); 140 break; 141 } 142 case 2: { 143 int iVal = 0; 144 if( Tcl_GetBooleanFromObj(interp, objv[3], &iVal) ) return TCL_ERROR; 145 res = sqlite3_recover_config(pTest->p, 146 SQLITE_RECOVER_FREELIST_CORRUPT, (void*)&iVal 147 ); 148 break; 149 } 150 case 3: { 151 int iVal = 0; 152 if( Tcl_GetBooleanFromObj(interp, objv[3], &iVal) ) return TCL_ERROR; 153 res = sqlite3_recover_config(pTest->p, 154 SQLITE_RECOVER_ROWIDS, (void*)&iVal 155 ); 156 break; 157 } 158 case 4: { 159 int iVal = 0; 160 if( Tcl_GetBooleanFromObj(interp, objv[3], &iVal) ) return TCL_ERROR; 161 res = sqlite3_recover_config(pTest->p, 162 SQLITE_RECOVER_SLOWINDEXES, (void*)&iVal 163 ); 164 break; 165 } 166 case 5: { 167 res = sqlite3_recover_config(pTest->p, 12345, 0); 168 break; 169 } 170 } 171 Tcl_SetObjResult(interp, Tcl_NewIntObj(res)); 172 break; 173 } 174 case 1: assert( sqlite3_stricmp("run", aSub[iSub].zSub)==0 ); { 175 int res = sqlite3_recover_run(pTest->p); 176 Tcl_SetObjResult(interp, Tcl_NewIntObj(res)); 177 break; 178 } 179 case 2: assert( sqlite3_stricmp("errmsg", aSub[iSub].zSub)==0 ); { 180 const char *zErr = sqlite3_recover_errmsg(pTest->p); 181 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1)); 182 break; 183 } 184 case 3: assert( sqlite3_stricmp("errcode", aSub[iSub].zSub)==0 ); { 185 int errCode = sqlite3_recover_errcode(pTest->p); 186 Tcl_SetObjResult(interp, Tcl_NewIntObj(errCode)); 187 break; 188 } 189 case 4: assert( sqlite3_stricmp("finish", aSub[iSub].zSub)==0 ); { 190 int res = sqlite3_recover_errcode(pTest->p); 191 int res2; 192 if( res!=SQLITE_OK ){ 193 const char *zErr = sqlite3_recover_errmsg(pTest->p); 194 Tcl_SetObjResult(interp, Tcl_NewStringObj(zErr, -1)); 195 } 196 res2 = sqlite3_recover_finish(pTest->p); 197 assert( res2==res ); 198 if( res ) return TCL_ERROR; 199 break; 200 } 201 case 5: assert( sqlite3_stricmp("step", aSub[iSub].zSub)==0 ); { 202 int res = sqlite3_recover_step(pTest->p); 203 Tcl_SetObjResult(interp, Tcl_NewIntObj(res)); 204 break; 205 } 206 } 207 208 return TCL_OK; 209 } 210 211 /* 212 ** sqlite3_recover_init DB DBNAME URI 213 */ 214 static int test_sqlite3_recover_init( 215 void *clientData, 216 Tcl_Interp *interp, 217 int objc, 218 Tcl_Obj *CONST objv[] 219 ){ 220 static int iTestRecoverCmd = 1; 221 222 TestRecover *pNew = 0; 223 sqlite3 *db = 0; 224 const char *zDb = 0; 225 const char *zUri = 0; 226 char zCmd[128]; 227 int bSql = clientData ? 1 : 0; 228 229 if( objc!=4 ){ 230 const char *zErr = (bSql ? "DB DBNAME SCRIPT" : "DB DBNAME URI"); 231 Tcl_WrongNumArgs(interp, 1, objv, zErr); 232 return TCL_ERROR; 233 } 234 if( getDbPointer(interp, objv[1], &db) ) return TCL_ERROR; 235 zDb = Tcl_GetString(objv[2]); 236 if( zDb[0]=='\0' ) zDb = 0; 237 238 pNew = (TestRecover*)ckalloc(sizeof(TestRecover)); 239 if( bSql==0 ){ 240 zUri = Tcl_GetString(objv[3]); 241 pNew->p = sqlite3_recover_init(db, zDb, zUri); 242 }else{ 243 pNew->interp = interp; 244 pNew->pScript = objv[3]; 245 Tcl_IncrRefCount(pNew->pScript); 246 pNew->p = sqlite3_recover_init_sql(db, zDb, xSqlCallback, (void*)pNew); 247 } 248 249 sprintf(zCmd, "sqlite_recover%d", iTestRecoverCmd++); 250 Tcl_CreateObjCommand(interp, zCmd, testRecoverCmd, (void*)pNew, 0); 251 252 Tcl_SetObjResult(interp, Tcl_NewStringObj(zCmd, -1)); 253 return TCL_OK; 254 } 255 256 /* 257 ** Declaration for public API function in file dbdata.c. This may be called 258 ** with NULL as the final two arguments to register the sqlite_dbptr and 259 ** sqlite_dbdata virtual tables with a database handle. 260 */ 261 #ifdef _WIN32 262 __declspec(dllexport) 263 #endif 264 int sqlite3_dbdata_init(sqlite3*, char**, const sqlite3_api_routines*); 265 266 /* 267 ** sqlite3_recover_init DB DBNAME URI 268 */ 269 static int test_sqlite3_dbdata_init( 270 void *clientData, 271 Tcl_Interp *interp, 272 int objc, 273 Tcl_Obj *CONST objv[] 274 ){ 275 sqlite3 *db = 0; 276 277 if( objc!=2 ){ 278 Tcl_WrongNumArgs(interp, 1, objv, "DB"); 279 return TCL_ERROR; 280 } 281 if( getDbPointer(interp, objv[1], &db) ) return TCL_ERROR; 282 sqlite3_dbdata_init(db, 0, 0); 283 284 Tcl_ResetResult(interp); 285 return TCL_OK; 286 } 287 288 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 289 290 int TestRecover_Init(Tcl_Interp *interp){ 291 #ifndef SQLITE_OMIT_VIRTUALTABLE 292 struct Cmd { 293 const char *zCmd; 294 Tcl_ObjCmdProc *xProc; 295 void *pArg; 296 } aCmd[] = { 297 { "sqlite3_recover_init", test_sqlite3_recover_init, 0 }, 298 { "sqlite3_recover_init_sql", test_sqlite3_recover_init, (void*)1 }, 299 { "sqlite3_dbdata_init", test_sqlite3_dbdata_init, (void*)1 }, 300 }; 301 int i; 302 303 for(i=0; izCmd, p->xProc, p->pArg, 0); 306 } 307 #endif 308 return TCL_OK; 309 }