@@ -1016,8 +1016,9 @@ static int sqlcipher_execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql)
10161016void sqlcipher_exportFunc (sqlite3_context * context , int argc , sqlite3_value * * argv ) {
10171017 sqlite3 * db = sqlite3_context_db_handle (context );
10181018 const char * targetDb , * sourceDb ;
1019-
1020- int saved_flags = db -> flags ; /* Saved value of the db->flags */
1019+ int targetDb_idx = 0 ;
1020+ u64 saved_flags = db -> flags ; /* Saved value of the db->flags */
1021+ u32 saved_mDbFlags = db -> mDbFlags ; /* Saved value of the db->mDbFlags */
10211022 int saved_nChange = db -> nChange ; /* Saved value of db->nChange */
10221023 int saved_nTotalChange = db -> nTotalChange ; /* Saved value of db->nTotalChange */
10231024 u8 saved_mTrace = db -> mTrace ; /* Saved value of db->mTrace */
@@ -1035,36 +1036,46 @@ void sqlcipher_exportFunc(sqlite3_context *context, int argc, sqlite3_value **ar
10351036 targetDb = (const char * ) sqlite3_value_text (argv [0 ]);
10361037 sourceDb = (argc == 2 ) ? (char * ) sqlite3_value_text (argv [1 ]) : "main" ;
10371038
1039+ /* if the name of the target is not main, but the index returned is zero
1040+ there is a mismatch and we should not proceed */
1041+ targetDb_idx = sqlcipher_find_db_index (db , targetDb );
1042+ if (targetDb_idx == 0 && sqlite3StrICmp ("main" , targetDb ) != 0 ) {
1043+ rc = SQLITE_ERROR ;
1044+ pzErrMsg = sqlite3_mprintf ("unknown database %s" , targetDb );
1045+ goto end_of_export ;
1046+ }
1047+ db -> init .iDb = targetDb_idx ;
1048+
10381049 db -> flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks ;
10391050 db -> mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum ;
1040- db -> flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder );
1051+ db -> flags &= ~(u64 )( SQLITE_ForeignKeys | SQLITE_ReverseOrder | SQLITE_Defensive | SQLITE_CountRows );
10411052 db -> xTrace = 0 ;
10421053 db -> mTrace = 0 ;
10431054
10441055 /* Query the schema of the main database. Create a mirror schema
10451056 ** in the temporary database.
10461057 */
10471058 zSql = sqlite3_mprintf (
1048- "SELECT 'CREATE TABLE %s.' || substr( sql,14) "
1059+ "SELECT sql "
10491060 " FROM %s.sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
10501061 " AND rootpage>0"
1051- , targetDb , sourceDb );
1062+ , sourceDb );
10521063 rc = (zSql == NULL ) ? SQLITE_NOMEM : sqlcipher_execExecSql (db , & pzErrMsg , zSql );
10531064 if ( rc != SQLITE_OK ) goto end_of_export ;
10541065 sqlite3_free (zSql );
10551066
10561067 zSql = sqlite3_mprintf (
1057- "SELECT 'CREATE INDEX %s.' || substr( sql,14) "
1068+ "SELECT sql "
10581069 " FROM %s.sqlite_master WHERE sql LIKE 'CREATE INDEX %%' "
1059- , targetDb , sourceDb );
1070+ , sourceDb );
10601071 rc = (zSql == NULL ) ? SQLITE_NOMEM : sqlcipher_execExecSql (db , & pzErrMsg , zSql );
10611072 if ( rc != SQLITE_OK ) goto end_of_export ;
10621073 sqlite3_free (zSql );
10631074
10641075 zSql = sqlite3_mprintf (
1065- "SELECT 'CREATE UNIQUE INDEX %s.' || substr( sql,21) "
1076+ "SELECT sql "
10661077 " FROM %s.sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %%'"
1067- , targetDb , sourceDb );
1078+ , sourceDb );
10681079 rc = (zSql == NULL ) ? SQLITE_NOMEM : sqlcipher_execExecSql (db , & pzErrMsg , zSql );
10691080 if ( rc != SQLITE_OK ) goto end_of_export ;
10701081 sqlite3_free (zSql );
@@ -1084,16 +1095,8 @@ void sqlcipher_exportFunc(sqlite3_context *context, int argc, sqlite3_value **ar
10841095 if ( rc != SQLITE_OK ) goto end_of_export ;
10851096 sqlite3_free (zSql );
10861097
1087- /* Copy over the sequence table
1098+ /* Copy over the contents of the sequence table
10881099 */
1089- zSql = sqlite3_mprintf (
1090- "SELECT 'DELETE FROM %s.' || quote(name) || ';' "
1091- "FROM %s.sqlite_master WHERE name='sqlite_sequence' "
1092- , targetDb , targetDb );
1093- rc = (zSql == NULL ) ? SQLITE_NOMEM : sqlcipher_execExecSql (db , & pzErrMsg , zSql );
1094- if ( rc != SQLITE_OK ) goto end_of_export ;
1095- sqlite3_free (zSql );
1096-
10971100 zSql = sqlite3_mprintf (
10981101 "SELECT 'INSERT INTO %s.' || quote(name) "
10991102 "|| ' SELECT * FROM %s.' || quote(name) || ';' "
@@ -1121,7 +1124,9 @@ void sqlcipher_exportFunc(sqlite3_context *context, int argc, sqlite3_value **ar
11211124
11221125 zSql = NULL ;
11231126end_of_export :
1127+ db -> init .iDb = 0 ;
11241128 db -> flags = saved_flags ;
1129+ db -> mDbFlags = saved_mDbFlags ;
11251130 db -> nChange = saved_nChange ;
11261131 db -> nTotalChange = saved_nTotalChange ;
11271132 db -> xTrace = saved_xTrace ;
0 commit comments