** Return a list of pointers to strings which are the names of all ** columns in table zTab. The memory to hold the names is dynamically ** allocated and must be released by the caller using a subsequent call ** to freeColumnList(). ** ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid ** value that needs to be preserved, then azCol[0] is filled in with the ** name of the
| 13744 | ** by an entry with azCol[i]==0. |
| 13745 | */ |
| 13746 | static char **tableColumnList(ShellState *p, const char *zTab){ |
| 13747 | char **azCol = 0; |
| 13748 | sqlite3_stmt *pStmt; |
| 13749 | char *zSql; |
| 13750 | int nCol = 0; |
| 13751 | int nAlloc = 0; |
| 13752 | int nPK = 0; /* Number of PRIMARY KEY columns seen */ |
| 13753 | int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */ |
| 13754 | int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid); |
| 13755 | int rc; |
| 13756 | |
| 13757 | zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab); |
| 13758 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 13759 | sqlite3_free(zSql); |
| 13760 | if( rc ) return 0; |
| 13761 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 13762 | if( nCol>=nAlloc-2 ){ |
| 13763 | nAlloc = nAlloc*2 + nCol + 10; |
| 13764 | azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0])); |
| 13765 | if( azCol==0 ) shell_out_of_memory(); |
| 13766 | } |
| 13767 | azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); |
| 13768 | if( sqlite3_column_int(pStmt, 5) ){ |
| 13769 | nPK++; |
| 13770 | if( nPK==1 |
| 13771 | && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2), |
| 13772 | "INTEGER")==0 |
| 13773 | ){ |
| 13774 | isIPK = 1; |
| 13775 | }else{ |
| 13776 | isIPK = 0; |
| 13777 | } |
| 13778 | } |
| 13779 | } |
| 13780 | sqlite3_finalize(pStmt); |
| 13781 | if( azCol==0 ) return 0; |
| 13782 | azCol[0] = 0; |
| 13783 | azCol[nCol+1] = 0; |
| 13784 | |
| 13785 | /* The decision of whether or not a rowid really needs to be preserved |
| 13786 | ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table |
| 13787 | ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve |
| 13788 | ** rowids on tables where the rowid is inaccessible because there are other |
| 13789 | ** columns in the table named "rowid", "_rowid_", and "oid". |
| 13790 | */ |
| 13791 | if( preserveRowid && isIPK ){ |
| 13792 | /* If a single PRIMARY KEY column with type INTEGER was seen, then it |
| 13793 | ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID |
| 13794 | ** table or a INTEGER PRIMARY KEY DESC column, neither of which are |
| 13795 | ** ROWID aliases. To distinguish these cases, check to see if |
| 13796 | ** there is a "pk" entry in "PRAGMA index_list". There will be |
| 13797 | ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID. |
| 13798 | */ |
| 13799 | zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)" |
| 13800 | " WHERE origin='pk'", zTab); |
| 13801 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 13802 | sqlite3_free(zSql); |
| 13803 | if( rc ){ |
no test coverage detected