** This function attempts to create a lost and found table within the ** output db. If successful, it returns a pointer to a buffer containing ** the name of the new table. It is the responsibility of the caller to ** eventually free this buffer using sqlite3_free(). ** ** If an error occurs, NULL is returned and an error code and error ** message left in the recover handle. */
| 14836 | ** message left in the recover handle. |
| 14837 | */ |
| 14838 | static char *recoverLostAndFoundCreate( |
| 14839 | sqlite3_recover *p, /* Recover object */ |
| 14840 | int nField /* Number of column fields in new table */ |
| 14841 | ){ |
| 14842 | char *zTbl = 0; |
| 14843 | sqlite3_stmt *pProbe = 0; |
| 14844 | int ii = 0; |
| 14845 | |
| 14846 | pProbe = recoverPrepare(p, p->dbOut, |
| 14847 | "SELECT 1 FROM sqlite_schema WHERE name=?" |
| 14848 | ); |
| 14849 | for(ii=-1; zTbl==0 && p->errCode==SQLITE_OK && ii<1000; ii++){ |
| 14850 | int bFail = 0; |
| 14851 | if( ii<0 ){ |
| 14852 | zTbl = recoverMPrintf(p, "%s", p->zLostAndFound); |
| 14853 | }else{ |
| 14854 | zTbl = recoverMPrintf(p, "%s_%d", p->zLostAndFound, ii); |
| 14855 | } |
| 14856 | |
| 14857 | if( p->errCode==SQLITE_OK ){ |
| 14858 | sqlite3_bind_text(pProbe, 1, zTbl, -1, SQLITE_STATIC); |
| 14859 | if( SQLITE_ROW==sqlite3_step(pProbe) ){ |
| 14860 | bFail = 1; |
| 14861 | } |
| 14862 | recoverReset(p, pProbe); |
| 14863 | } |
| 14864 | |
| 14865 | if( bFail ){ |
| 14866 | sqlite3_clear_bindings(pProbe); |
| 14867 | sqlite3_free(zTbl); |
| 14868 | zTbl = 0; |
| 14869 | } |
| 14870 | } |
| 14871 | recoverFinalize(p, pProbe); |
| 14872 | |
| 14873 | if( zTbl ){ |
| 14874 | const char *zSep = 0; |
| 14875 | char *zField = 0; |
| 14876 | char *zSql = 0; |
| 14877 | |
| 14878 | zSep = "rootpgno INTEGER, pgno INTEGER, nfield INTEGER, id INTEGER, "; |
| 14879 | for(ii=0; p->errCode==SQLITE_OK && ii<nField; ii++){ |
| 14880 | zField = recoverMPrintf(p, "%z%sc%d", zField, zSep, ii); |
| 14881 | zSep = ", "; |
| 14882 | } |
| 14883 | |
| 14884 | zSql = recoverMPrintf(p, "CREATE TABLE %s(%s)", zTbl, zField); |
| 14885 | sqlite3_free(zField); |
| 14886 | |
| 14887 | recoverExec(p, p->dbOut, zSql); |
| 14888 | recoverSqlCallback(p, zSql); |
| 14889 | sqlite3_free(zSql); |
| 14890 | }else if( p->errCode==SQLITE_OK ){ |
| 14891 | recoverError( |
| 14892 | p, SQLITE_ERROR, "failed to create %s output table", p->zLostAndFound |
| 14893 | ); |
| 14894 | } |
| 14895 |
no test coverage detected