** 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
| 19387 | ** by an entry with azCol[i]==0. |
| 19388 | */ |
| 19389 | static char **tableColumnList(ShellState *p, const char *zTab){ |
| 19390 | char **azCol = 0; |
| 19391 | sqlite3_stmt *pStmt; |
| 19392 | char *zSql; |
| 19393 | int nCol = 0; |
| 19394 | int nAlloc = 0; |
| 19395 | int nPK = 0; /* Number of PRIMARY KEY columns seen */ |
| 19396 | int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */ |
| 19397 | int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid); |
| 19398 | int rc; |
| 19399 | |
| 19400 | zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab); |
| 19401 | shell_check_oom(zSql); |
| 19402 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 19403 | sqlite3_free(zSql); |
| 19404 | if( rc ) return 0; |
| 19405 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 19406 | if( nCol>=nAlloc-2 ){ |
| 19407 | nAlloc = nAlloc*2 + nCol + 10; |
| 19408 | azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0])); |
| 19409 | shell_check_oom(azCol); |
| 19410 | } |
| 19411 | azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); |
| 19412 | shell_check_oom(azCol[nCol]); |
| 19413 | if( sqlite3_column_int(pStmt, 5) ){ |
| 19414 | nPK++; |
| 19415 | if( nPK==1 |
| 19416 | && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2), |
| 19417 | "INTEGER")==0 |
| 19418 | ){ |
| 19419 | isIPK = 1; |
| 19420 | }else{ |
| 19421 | isIPK = 0; |
| 19422 | } |
| 19423 | } |
| 19424 | } |
| 19425 | sqlite3_finalize(pStmt); |
| 19426 | if( azCol==0 ) return 0; |
| 19427 | azCol[0] = 0; |
| 19428 | azCol[nCol+1] = 0; |
| 19429 | |
| 19430 | /* The decision of whether or not a rowid really needs to be preserved |
| 19431 | ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table |
| 19432 | ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve |
| 19433 | ** rowids on tables where the rowid is inaccessible because there are other |
| 19434 | ** columns in the table named "rowid", "_rowid_", and "oid". |
| 19435 | */ |
| 19436 | if( preserveRowid && isIPK ){ |
| 19437 | /* If a single PRIMARY KEY column with type INTEGER was seen, then it |
| 19438 | ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID |
| 19439 | ** table or a INTEGER PRIMARY KEY DESC column, neither of which are |
| 19440 | ** ROWID aliases. To distinguish these cases, check to see if |
| 19441 | ** there is a "pk" entry in "PRAGMA index_list". There will be |
| 19442 | ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID. |
| 19443 | */ |
| 19444 | zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)" |
| 19445 | " WHERE origin='pk'", zTab); |
| 19446 | shell_check_oom(zSql); |
no test coverage detected