@@ -11424,61 +11424,69 @@ static int line_is_complete(char *zSql, int nSql){
1142411424**
1142511425** 0: Have not seen any SQL.
1142611426** 1: Have seen "PRAGMA foreign_keys=OFF;".
11427- ** 2: Currently assuming we are parsing ".dump" restore, defensive mode
11428- ** should be disabled following the current transaction .
11429- ** 3 : Nothing left to do.
11427+ ** 2-6 : Currently running .dump transaction. If the "2" bit is set,
11428+ ** disable DEFENSIVE when done. If "4" is set, disable DQS_DDL .
11429+ ** 7 : Nothing left to do. This function becomes a no-op .
1143011430*/
1143111431static int doAutoDetectRestore(ShellState *p, const char *zSql){
1143211432 int rc = SQLITE_OK;
1143311433
11434- switch( p->eRestoreState ){
11435- case 0: {
11436- int bDefense = 0; /* True if in defensive mode */
11437- const char *zExpect = "PRAGMA foreign_keys=OFF;";
11438- assert( strlen(zExpect)==24 );
11439- sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &bDefense);
11440- if( p->bSafeMode==0 && bDefense && memcmp(zSql, zExpect, 25)==0 ){
11441- p->eRestoreState = 1;
11442- }else{
11443- p->eRestoreState = 3;
11444- }
11445- break;
11446- };
11447-
11448- case 1: {
11449- const char *zExpect = "BEGIN TRANSACTION;";
11450- assert( strlen(zExpect)==18 );
11451- if( memcmp(zSql, zExpect, 19)==0 ){
11452- /* Now check if the database is empty. */
11453- const char *zQuery = "SELECT 1 FROM sqlite_schema LIMIT 1";
11454- sqlite3_stmt *pStmt = 0;
11455- int bEmpty = 1;
11456-
11457- shellPrepare(p->db, &rc, zQuery, &pStmt);
11458- if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
11459- bEmpty = 0;
11434+ if( p->eRestoreState<7 ){
11435+ switch( p->eRestoreState ){
11436+ case 0: {
11437+ const char *zExpect = "PRAGMA foreign_keys=OFF;";
11438+ assert( strlen(zExpect)==24 );
11439+ if( p->bSafeMode==0 && memcmp(zSql, zExpect, 25)==0 ){
11440+ p->eRestoreState = 1;
11441+ }else{
11442+ p->eRestoreState = 7;
1146011443 }
11461- shellFinalize(&rc, pStmt);
11462- if( bEmpty && rc==SQLITE_OK ){
11444+ break;
11445+ };
11446+
11447+ case 1: {
11448+ int bIsDump = 0;
11449+ const char *zExpect = "BEGIN TRANSACTION;";
11450+ assert( strlen(zExpect)==18 );
11451+ if( memcmp(zSql, zExpect, 19)==0 ){
11452+ /* Now check if the database is empty. */
11453+ const char *zQuery = "SELECT 1 FROM sqlite_schema LIMIT 1";
11454+ sqlite3_stmt *pStmt = 0;
11455+
11456+ bIsDump = 1;
11457+ shellPrepare(p->db, &rc, zQuery, &pStmt);
11458+ if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
11459+ bIsDump = 0;
11460+ }
11461+ shellFinalize(&rc, pStmt);
11462+ }
11463+ if( bIsDump && rc==SQLITE_OK ){
11464+ int bDefense = 0;
11465+ int bDqsDdl = 0;
11466+ sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &bDefense);
11467+ sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, -1, &bDqsDdl);
1146311468 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
11469+ sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, 1, 0);
11470+ p->eRestoreState = (bDefense ? 2 : 0) + (bDqsDdl ? 4 : 0);
1146411471 }else{
11465- p->eRestoreState = 3 ;
11472+ p->eRestoreState = 7 ;
1146611473 }
11474+ break;
1146711475 }
11468- break;
11469- }
11470-
11471- case 2: {
11472- if( sqlite3_get_autocommit(p->db) ){
11473- sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
11474- p->eRestoreState = 3;
11476+
11477+ default: {
11478+ if( sqlite3_get_autocommit(p->db) ){
11479+ if( (p->eRestoreState & 2) ){
11480+ sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 1, 0);
11481+ }
11482+ if( (p->eRestoreState & 4) ){
11483+ sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, 0, 0);
11484+ }
11485+ p->eRestoreState = 7;
11486+ }
11487+ break;
1147511488 }
11476- break;
1147711489 }
11478-
11479- default: /* Nothing to do */
11480- assert( p->eRestoreState==3 );
11481- break;
1148211490 }
1148311491
1148411492 return rc;
0 commit comments