** Try to transfer all rows of the schema that match zWhere. For ** each row, invoke xForEach() on the object defined by that row. ** If an error is encountered while moving forward through the ** sqlite_schema table, try again moving backwards. */
| 15381 | ** sqlite_schema table, try again moving backwards. |
| 15382 | */ |
| 15383 | static void tryToCloneSchema( |
| 15384 | ShellState *p, |
| 15385 | sqlite3 *newDb, |
| 15386 | const char *zWhere, |
| 15387 | void (*xForEach)(ShellState*,sqlite3*,const char*) |
| 15388 | ){ |
| 15389 | sqlite3_stmt *pQuery = 0; |
| 15390 | char *zQuery = 0; |
| 15391 | int rc; |
| 15392 | const unsigned char *zName; |
| 15393 | const unsigned char *zSql; |
| 15394 | char *zErrMsg = 0; |
| 15395 | |
| 15396 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema" |
| 15397 | " WHERE %s", zWhere); |
| 15398 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 15399 | if( rc ){ |
| 15400 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
| 15401 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 15402 | zQuery); |
| 15403 | goto end_schema_xfer; |
| 15404 | } |
| 15405 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 15406 | zName = sqlite3_column_text(pQuery, 0); |
| 15407 | zSql = sqlite3_column_text(pQuery, 1); |
| 15408 | printf("%s... ", zName); fflush(stdout); |
| 15409 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 15410 | if( zErrMsg ){ |
| 15411 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
| 15412 | sqlite3_free(zErrMsg); |
| 15413 | zErrMsg = 0; |
| 15414 | } |
| 15415 | if( xForEach ){ |
| 15416 | xForEach(p, newDb, (const char*)zName); |
| 15417 | } |
| 15418 | printf("done\n"); |
| 15419 | } |
| 15420 | if( rc!=SQLITE_DONE ){ |
| 15421 | sqlite3_finalize(pQuery); |
| 15422 | sqlite3_free(zQuery); |
| 15423 | zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema" |
| 15424 | " WHERE %s ORDER BY rowid DESC", zWhere); |
| 15425 | rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
| 15426 | if( rc ){ |
| 15427 | utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
| 15428 | sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
| 15429 | zQuery); |
| 15430 | goto end_schema_xfer; |
| 15431 | } |
| 15432 | while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
| 15433 | zName = sqlite3_column_text(pQuery, 0); |
| 15434 | zSql = sqlite3_column_text(pQuery, 1); |
| 15435 | printf("%s... ", zName); fflush(stdout); |
| 15436 | sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
| 15437 | if( zErrMsg ){ |
| 15438 | utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
| 15439 | sqlite3_free(zErrMsg); |
| 15440 | zErrMsg = 0; |
no test coverage detected