| 11593 | |
| 11594 | |
| 11595 | static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){ |
| 11596 | int rc = idxRegisterVtab(p); |
| 11597 | sqlite3_stmt *pSchema = 0; |
| 11598 | |
| 11599 | /* For each table in the main db schema: |
| 11600 | ** |
| 11601 | ** 1) Add an entry to the p->pTable list, and |
| 11602 | ** 2) Create the equivalent virtual table in dbv. |
| 11603 | */ |
| 11604 | rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg, |
| 11605 | "SELECT type, name, sql, 1 FROM sqlite_schema " |
| 11606 | "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' " |
| 11607 | " UNION ALL " |
| 11608 | "SELECT type, name, sql, 2 FROM sqlite_schema " |
| 11609 | "WHERE type = 'trigger'" |
| 11610 | " AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') " |
| 11611 | "ORDER BY 4, 1" |
| 11612 | ); |
| 11613 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){ |
| 11614 | const char *zType = (const char*)sqlite3_column_text(pSchema, 0); |
| 11615 | const char *zName = (const char*)sqlite3_column_text(pSchema, 1); |
| 11616 | const char *zSql = (const char*)sqlite3_column_text(pSchema, 2); |
| 11617 | |
| 11618 | if( zType==0 || zName==0 ) continue; |
| 11619 | if( zType[0]=='v' || zType[1]=='r' ){ |
| 11620 | if( zSql ) rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg); |
| 11621 | }else{ |
| 11622 | IdxTable *pTab; |
| 11623 | rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg); |
| 11624 | if( rc==SQLITE_OK ){ |
| 11625 | int i; |
| 11626 | char *zInner = 0; |
| 11627 | char *zOuter = 0; |
| 11628 | pTab->pNext = p->pTable; |
| 11629 | p->pTable = pTab; |
| 11630 | |
| 11631 | /* The statement the vtab will pass to sqlite3_declare_vtab() */ |
| 11632 | zInner = idxAppendText(&rc, 0, "CREATE TABLE x("); |
| 11633 | for(i=0; i<pTab->nCol; i++){ |
| 11634 | zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s", |
| 11635 | (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl |
| 11636 | ); |
| 11637 | } |
| 11638 | zInner = idxAppendText(&rc, zInner, ")"); |
| 11639 | |
| 11640 | /* The CVT statement to create the vtab */ |
| 11641 | zOuter = idxAppendText(&rc, 0, |
| 11642 | "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner |
| 11643 | ); |
| 11644 | if( rc==SQLITE_OK ){ |
| 11645 | rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg); |
| 11646 | } |
| 11647 | sqlite3_free(zInner); |
| 11648 | sqlite3_free(zOuter); |
| 11649 | } |
| 11650 | } |
| 11651 | } |
| 11652 | idxFinalize(&rc, pSchema); |
no test coverage detected