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

Function idxCreateVtabSchema

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

Source from the content-addressed store, hash-verified

9690
9691
9692static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
9693 int rc = idxRegisterVtab(p);
9694 sqlite3_stmt *pSchema = 0;
9695
9696 /* For each table in the main db schema:
9697 **
9698 ** 1) Add an entry to the p->pTable list, and
9699 ** 2) Create the equivalent virtual table in dbv.
9700 */
9701 rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
9702 "SELECT type, name, sql, 1 FROM sqlite_schema "
9703 "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
9704 " UNION ALL "
9705 "SELECT type, name, sql, 2 FROM sqlite_schema "
9706 "WHERE type = 'trigger'"
9707 " AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') "
9708 "ORDER BY 4, 1"
9709 );
9710 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
9711 const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
9712 const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
9713 const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
9714
9715 if( zType[0]=='v' || zType[1]=='r' ){
9716 rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
9717 }else{
9718 IdxTable *pTab;
9719 rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
9720 if( rc==SQLITE_OK ){
9721 int i;
9722 char *zInner = 0;
9723 char *zOuter = 0;
9724 pTab->pNext = p->pTable;
9725 p->pTable = pTab;
9726
9727 /* The statement the vtab will pass to sqlite3_declare_vtab() */
9728 zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
9729 for(i=0; i<pTab->nCol; i++){
9730 zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s",
9731 (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
9732 );
9733 }
9734 zInner = idxAppendText(&rc, zInner, ")");
9735
9736 /* The CVT statement to create the vtab */
9737 zOuter = idxAppendText(&rc, 0,
9738 "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
9739 );
9740 if( rc==SQLITE_OK ){
9741 rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
9742 }
9743 sqlite3_free(zInner);
9744 sqlite3_free(zOuter);
9745 }
9746 }
9747 }
9748 idxFinalize(&rc, pSchema);
9749 return rc;

Callers 1

sqlite3_expert_newFunction · 0.85

Calls 5

idxRegisterVtabFunction · 0.85
idxPrepareStmtFunction · 0.85
idxGetTableInfoFunction · 0.85
idxAppendTextFunction · 0.85
idxFinalizeFunction · 0.85

Tested by

no test coverage detected