** This function is called after processing each line of SQL in the ** runOneSqlLine() function. Its purpose is to detect scenarios where ** defensive mode should be automatically turned off. Specifically, when ** ** 1. The first line of input is "PRAGMA foreign_keys=OFF;", ** 2. The second line of input is "BEGIN TRANSACTION;", ** 3. The database is empty, and ** 4. The shell is not runni
| 31976 | ** 7: Nothing left to do. This function becomes a no-op. |
| 31977 | */ |
| 31978 | static int doAutoDetectRestore(ShellState *p, const char *zSql){ |
| 31979 | int rc = SQLITE_OK; |
| 31980 | |
| 31981 | if( p->eRestoreState<7 ){ |
| 31982 | switch( p->eRestoreState ){ |
| 31983 | case 0: { |
| 31984 | const char *zExpect = "PRAGMA foreign_keys=OFF;"; |
| 31985 | assert( strlen(zExpect)==24 ); |
| 31986 | if( p->bSafeMode==0 |
| 31987 | && strlen(zSql)>=24 |
| 31988 | && memcmp(zSql, zExpect, 25)==0 |
| 31989 | ){ |
| 31990 | p->eRestoreState = 1; |
| 31991 | }else{ |
| 31992 | p->eRestoreState = 7; |
| 31993 | } |
| 31994 | break; |
| 31995 | }; |
| 31996 | |
| 31997 | case 1: { |
| 31998 | int bIsDump = 0; |
| 31999 | const char *zExpect = "BEGIN TRANSACTION;"; |
| 32000 | assert( strlen(zExpect)==18 ); |
| 32001 | if( memcmp(zSql, zExpect, 19)==0 ){ |
| 32002 | /* Now check if the database is empty. */ |
| 32003 | const char *zQuery = "SELECT 1 FROM sqlite_schema LIMIT 1"; |
| 32004 | sqlite3_stmt *pStmt = 0; |
| 32005 | |
| 32006 | bIsDump = 1; |
| 32007 | shellPrepare(p->db, &rc, zQuery, &pStmt); |
| 32008 | if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 32009 | bIsDump = 0; |
| 32010 | } |
| 32011 | shellFinalize(&rc, pStmt); |
| 32012 | } |
| 32013 | if( bIsDump && rc==SQLITE_OK ){ |
| 32014 | int bDefense = 0; |
| 32015 | int bDqsDdl = 0; |
| 32016 | sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &bDefense); |
| 32017 | sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, -1, &bDqsDdl); |
| 32018 | sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0); |
| 32019 | sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, 1, 0); |
| 32020 | p->eRestoreState = (bDefense ? 2 : 0) + (bDqsDdl ? 4 : 0); |
| 32021 | }else{ |
| 32022 | p->eRestoreState = 7; |
| 32023 | } |
| 32024 | break; |
| 32025 | } |
| 32026 | |
| 32027 | default: { |
| 32028 | if( sqlite3_get_autocommit(p->db) ){ |
| 32029 | if( (p->eRestoreState & 2) ){ |
| 32030 | sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 1, 0); |
| 32031 | } |
| 32032 | if( (p->eRestoreState & 4) ){ |
| 32033 | sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, 0, 0); |
| 32034 | } |
| 32035 | p->eRestoreState = 7; |
no test coverage detected