** 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
| 24533 | ** by an entry with azCol[i]==0. |
| 24534 | */ |
| 24535 | static char **tableColumnList(ShellState *p, const char *zTab){ |
| 24536 | char **azCol = 0; |
| 24537 | sqlite3_stmt *pStmt; |
| 24538 | char *zSql; |
| 24539 | int nCol = 0; |
| 24540 | int nAlloc = 0; |
| 24541 | int nPK = 0; /* Number of PRIMARY KEY columns seen */ |
| 24542 | int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */ |
| 24543 | int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid); |
| 24544 | int rc; |
| 24545 | |
| 24546 | zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab); |
| 24547 | shell_check_oom(zSql); |
| 24548 | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| 24549 | sqlite3_free(zSql); |
| 24550 | if( rc ) return 0; |
| 24551 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 24552 | if( nCol>=nAlloc-2 ){ |
| 24553 | nAlloc = nAlloc*2 + nCol + 10; |
| 24554 | azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0])); |
| 24555 | shell_check_oom(azCol); |
| 24556 | } |
| 24557 | azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); |
| 24558 | shell_check_oom(azCol[nCol]); |
| 24559 | if( sqlite3_column_int(pStmt, 5) ){ |
| 24560 | nPK++; |
| 24561 | if( nPK==1 |
| 24562 | && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2), |
| 24563 | "INTEGER")==0 |
| 24564 | ){ |
| 24565 | isIPK = 1; |
| 24566 | }else{ |
| 24567 | isIPK = 0; |
| 24568 | } |
| 24569 | } |
| 24570 | } |
| 24571 | sqlite3_finalize(pStmt); |
| 24572 | if( azCol==0 ) return 0; |
| 24573 | azCol[0] = 0; |
| 24574 | azCol[nCol+1] = 0; |
| 24575 | |
| 24576 | /* The decision of whether or not a rowid really needs to be preserved |
| 24577 | ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table |
| 24578 | ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve |
| 24579 | ** rowids on tables where the rowid is inaccessible because there are other |
| 24580 | ** columns in the table named "rowid", "_rowid_", and "oid". |
| 24581 | */ |
| 24582 | if( preserveRowid && isIPK ){ |
| 24583 | /* If a single PRIMARY KEY column with type INTEGER was seen, then it |
| 24584 | ** might be an alias for the ROWID. But it might also be a WITHOUT ROWID |
| 24585 | ** table or a INTEGER PRIMARY KEY DESC column, neither of which are |
| 24586 | ** ROWID aliases. To distinguish these cases, check to see if |
| 24587 | ** there is a "pk" entry in "PRAGMA index_list". There will be |
| 24588 | ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID. |
| 24589 | */ |
| 24590 | zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)" |
| 24591 | " WHERE origin='pk'", zTab); |
| 24592 | shell_check_oom(zSql); |
no test coverage detected