MCPcopy Create free account
hub / github.com/audacity/audacity / idxGetTableInfo

Function idxGetTableInfo

lib-src/sqlite/shell.c:9049–9121  ·  view source on GitHub ↗

** Attempt to allocate an IdxTable structure corresponding to table zTab ** in the main database of connection db. If successful, set (*ppOut) to ** point to the new object and return SQLITE_OK. Otherwise, return an ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be ** set to point to an error string. ** ** It is the responsibility of the caller to eventually free either

Source from the content-addressed store, hash-verified

9047** IdxTable object or error message using sqlite3_free().
9048*/
9049static int idxGetTableInfo(
9050 sqlite3 *db, /* Database connection to read details from */
9051 const char *zTab, /* Table name */
9052 IdxTable **ppOut, /* OUT: New object (if successful) */
9053 char **pzErrmsg /* OUT: Error message (if not) */
9054){
9055 sqlite3_stmt *p1 = 0;
9056 int nCol = 0;
9057 int nTab = STRLEN(zTab);
9058 int nByte = sizeof(IdxTable) + nTab + 1;
9059 IdxTable *pNew = 0;
9060 int rc, rc2;
9061 char *pCsr = 0;
9062 int nPk = 0;
9063
9064 rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_xinfo=%Q", zTab);
9065 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
9066 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
9067 nByte += 1 + STRLEN(zCol);
9068 rc = sqlite3_table_column_metadata(
9069 db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
9070 );
9071 nByte += 1 + STRLEN(zCol);
9072 nCol++;
9073 nPk += (sqlite3_column_int(p1, 5)>0);
9074 }
9075 rc2 = sqlite3_reset(p1);
9076 if( rc==SQLITE_OK ) rc = rc2;
9077
9078 nByte += sizeof(IdxColumn) * nCol;
9079 if( rc==SQLITE_OK ){
9080 pNew = idxMalloc(&rc, nByte);
9081 }
9082 if( rc==SQLITE_OK ){
9083 pNew->aCol = (IdxColumn*)&pNew[1];
9084 pNew->nCol = nCol;
9085 pCsr = (char*)&pNew->aCol[nCol];
9086 }
9087
9088 nCol = 0;
9089 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
9090 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
9091 int nCopy = STRLEN(zCol) + 1;
9092 pNew->aCol[nCol].zName = pCsr;
9093 pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1);
9094 memcpy(pCsr, zCol, nCopy);
9095 pCsr += nCopy;
9096
9097 rc = sqlite3_table_column_metadata(
9098 db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
9099 );
9100 if( rc==SQLITE_OK ){
9101 nCopy = STRLEN(zCol) + 1;
9102 pNew->aCol[nCol].zColl = pCsr;
9103 memcpy(pCsr, zCol, nCopy);
9104 pCsr += nCopy;
9105 }
9106

Callers 1

idxCreateVtabSchemaFunction · 0.85

Calls 3

idxPrintfPrepareStmtFunction · 0.85
idxMallocFunction · 0.85
idxFinalizeFunction · 0.85

Tested by

no test coverage detected