** 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
| 10890 | ** IdxTable object or error message using sqlite3_free(). |
| 10891 | */ |
| 10892 | static int idxGetTableInfo( |
| 10893 | sqlite3 *db, /* Database connection to read details from */ |
| 10894 | const char *zTab, /* Table name */ |
| 10895 | IdxTable **ppOut, /* OUT: New object (if successful) */ |
| 10896 | char **pzErrmsg /* OUT: Error message (if not) */ |
| 10897 | ){ |
| 10898 | sqlite3_stmt *p1 = 0; |
| 10899 | int nCol = 0; |
| 10900 | int nTab; |
| 10901 | int nByte; |
| 10902 | IdxTable *pNew = 0; |
| 10903 | int rc, rc2; |
| 10904 | char *pCsr = 0; |
| 10905 | int nPk = 0; |
| 10906 | |
| 10907 | *ppOut = 0; |
| 10908 | if( zTab==0 ) return SQLITE_ERROR; |
| 10909 | nTab = STRLEN(zTab); |
| 10910 | nByte = sizeof(IdxTable) + nTab + 1; |
| 10911 | rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_xinfo=%Q", zTab); |
| 10912 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){ |
| 10913 | const char *zCol = (const char*)sqlite3_column_text(p1, 1); |
| 10914 | const char *zColSeq = 0; |
| 10915 | if( zCol==0 ){ |
| 10916 | rc = SQLITE_ERROR; |
| 10917 | break; |
| 10918 | } |
| 10919 | nByte += 1 + STRLEN(zCol); |
| 10920 | rc = sqlite3_table_column_metadata( |
| 10921 | db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0 |
| 10922 | ); |
| 10923 | if( zColSeq==0 ) zColSeq = "binary"; |
| 10924 | nByte += 1 + STRLEN(zColSeq); |
| 10925 | nCol++; |
| 10926 | nPk += (sqlite3_column_int(p1, 5)>0); |
| 10927 | } |
| 10928 | rc2 = sqlite3_reset(p1); |
| 10929 | if( rc==SQLITE_OK ) rc = rc2; |
| 10930 | |
| 10931 | nByte += sizeof(IdxColumn) * nCol; |
| 10932 | if( rc==SQLITE_OK ){ |
| 10933 | pNew = idxMalloc(&rc, nByte); |
| 10934 | } |
| 10935 | if( rc==SQLITE_OK ){ |
| 10936 | pNew->aCol = (IdxColumn*)&pNew[1]; |
| 10937 | pNew->nCol = nCol; |
| 10938 | pCsr = (char*)&pNew->aCol[nCol]; |
| 10939 | } |
| 10940 | |
| 10941 | nCol = 0; |
| 10942 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){ |
| 10943 | const char *zCol = (const char*)sqlite3_column_text(p1, 1); |
| 10944 | const char *zColSeq = 0; |
| 10945 | int nCopy; |
| 10946 | if( zCol==0 ) continue; |
| 10947 | nCopy = STRLEN(zCol) + 1; |
| 10948 | pNew->aCol[nCol].zName = pCsr; |
| 10949 | pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1); |
no test coverage detected