** This function is called after recoverCacheSchema() has cached those parts ** of the input database schema that could be recovered in temporary table ** "recovery.schema". This function creates in the output database copies ** of all parts of that schema that must be created before the tables can ** be populated. Specifically, this means: ** ** * all tables that are not VIRTUAL, and ** *
| 14609 | ** with equivalent SQL statements. |
| 14610 | */ |
| 14611 | static int recoverWriteSchema1(sqlite3_recover *p){ |
| 14612 | sqlite3_stmt *pSelect = 0; |
| 14613 | sqlite3_stmt *pTblname = 0; |
| 14614 | |
| 14615 | pSelect = recoverPrepare(p, p->dbOut, |
| 14616 | "WITH dbschema(rootpage, name, sql, tbl, isVirtual, isIndex) AS (" |
| 14617 | " SELECT rootpage, name, sql, " |
| 14618 | " type='table', " |
| 14619 | " sql LIKE 'create virtual%'," |
| 14620 | " (type='index' AND (sql LIKE '%unique%' OR ?1))" |
| 14621 | " FROM recovery.schema" |
| 14622 | ")" |
| 14623 | "SELECT rootpage, tbl, isVirtual, name, sql" |
| 14624 | " FROM dbschema " |
| 14625 | " WHERE tbl OR isIndex" |
| 14626 | " ORDER BY tbl DESC, name=='sqlite_sequence' DESC" |
| 14627 | ); |
| 14628 | |
| 14629 | pTblname = recoverPrepare(p, p->dbOut, |
| 14630 | "SELECT name FROM sqlite_schema " |
| 14631 | "WHERE type='table' ORDER BY rowid DESC LIMIT 1" |
| 14632 | ); |
| 14633 | |
| 14634 | if( pSelect ){ |
| 14635 | sqlite3_bind_int(pSelect, 1, p->bSlowIndexes); |
| 14636 | while( sqlite3_step(pSelect)==SQLITE_ROW ){ |
| 14637 | i64 iRoot = sqlite3_column_int64(pSelect, 0); |
| 14638 | int bTable = sqlite3_column_int(pSelect, 1); |
| 14639 | int bVirtual = sqlite3_column_int(pSelect, 2); |
| 14640 | const char *zName = (const char*)sqlite3_column_text(pSelect, 3); |
| 14641 | const char *zSql = (const char*)sqlite3_column_text(pSelect, 4); |
| 14642 | char *zFree = 0; |
| 14643 | int rc = SQLITE_OK; |
| 14644 | |
| 14645 | if( bVirtual ){ |
| 14646 | zSql = (const char*)(zFree = recoverMPrintf(p, |
| 14647 | "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)", |
| 14648 | zName, zName, zSql |
| 14649 | )); |
| 14650 | } |
| 14651 | rc = sqlite3_exec(p->dbOut, zSql, 0, 0, 0); |
| 14652 | if( rc==SQLITE_OK ){ |
| 14653 | recoverSqlCallback(p, zSql); |
| 14654 | if( bTable && !bVirtual ){ |
| 14655 | if( SQLITE_ROW==sqlite3_step(pTblname) ){ |
| 14656 | const char *zTbl = (const char*)sqlite3_column_text(pTblname, 0); |
| 14657 | recoverAddTable(p, zTbl, iRoot); |
| 14658 | } |
| 14659 | recoverReset(p, pTblname); |
| 14660 | } |
| 14661 | }else if( rc!=SQLITE_ERROR ){ |
| 14662 | recoverDbError(p, p->dbOut); |
| 14663 | } |
| 14664 | sqlite3_free(zFree); |
| 14665 | } |
| 14666 | } |
| 14667 | recoverFinalize(p, pSelect); |
| 14668 | recoverFinalize(p, pTblname); |
no test coverage detected