Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Changes In Branch schema2-vtab Excluding Merge-Ins
This is equivalent to a diff from 24ef16548e to d1fbc6ca18
2013-11-14
| ||
19:34 | Remove an unused local variable. (check-in: 10d5922638 user: drh tags: trunk) | |
19:18 | Merge the skip-scan optimization into the sessions branch. (check-in: 7596d1bf80 user: drh tags: sessions) | |
15:35 | An experimental virtual tables for showing the content of internal schema objects. (Leaf check-in: d1fbc6ca18 user: drh tags: schema2-vtab) | |
00:09 | Simplification to the progress callback check. One branch removed. (check-in: 24ef16548e user: drh tags: trunk) | |
2013-11-13
| ||
23:48 | Make sure the progress callback is invoked prior to an SQLITE_ROW return if it is overdue to be called. (check-in: 21f59b04f7 user: drh tags: trunk) | |
Changes to main.mk.
︙ | ︙ | |||
375 376 377 378 379 380 381 | # all: sqlite3.h libsqlite3.a sqlite3$(EXE) libsqlite3.a: $(LIBOBJ) $(AR) libsqlite3.a $(LIBOBJ) $(RANLIB) libsqlite3.a | | | | 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | # all: sqlite3.h libsqlite3.a sqlite3$(EXE) libsqlite3.a: $(LIBOBJ) $(AR) libsqlite3.a $(LIBOBJ) $(RANLIB) libsqlite3.a sqlite3$(EXE): $(TOP)/src/shell.c libsqlite3.a sqlite3.h $(TOP)/src/test_schema2.c $(TCCX) $(READLINE_FLAGS) -o sqlite3$(EXE) \ -I$(TOP)/src $(TOP)/src/shell.c \ libsqlite3.a $(LIBREADLINE) $(TLIBS) $(THREADLIB) mptester$(EXE): sqlite3.c $(TOP)/mptest/mptest.c $(TCCX) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \ $(TLIBS) $(THREADLIB) sqlite3.o: sqlite3.c |
︙ | ︙ |
Changes to src/shell.c.
︙ | ︙ | |||
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 | static char zTimerHelp[] = ".timer ON|OFF Turn the CPU timer measurement on or off\n" ; /* Forward reference */ static int process_input(struct callback_data *p, FILE *in); /* ** Make sure the database is open. If it is not, then open it. If ** the database fails to open, print an error message and exit. */ static void open_db(struct callback_data *p, int keepAlive){ if( p->db==0 ){ sqlite3_initialize(); | > > > > > | 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 | static char zTimerHelp[] = ".timer ON|OFF Turn the CPU timer measurement on or off\n" ; /* Forward reference */ static int process_input(struct callback_data *p, FILE *in); /* Include the source code for various extensions when in debug mode: */ #ifdef SQLITE_DEBUG # include "test_schema2.c" #endif /* ** Make sure the database is open. If it is not, then open it. If ** the database fails to open, print an error message and exit. */ static void open_db(struct callback_data *p, int keepAlive){ if( p->db==0 ){ sqlite3_initialize(); |
︙ | ︙ | |||
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 | p->zDbFilename, sqlite3_errmsg(db)); if( keepAlive ) return; exit(1); } #ifndef SQLITE_OMIT_LOAD_EXTENSION sqlite3_enable_load_extension(p->db, 1); #endif } } /* ** Do C-language style dequoting. ** ** \t -> tab | > > > | 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 | p->zDbFilename, sqlite3_errmsg(db)); if( keepAlive ) return; exit(1); } #ifndef SQLITE_OMIT_LOAD_EXTENSION sqlite3_enable_load_extension(p->db, 1); #endif #ifdef SQLITE_DEBUG sqlite3_schema2_register(p->db); #endif } } /* ** Do C-language style dequoting. ** ** \t -> tab |
︙ | ︙ |
Added src/test_schema2.c.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | /* ** 2013-11-14 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ****************************************************************************** ** ** This file contains an implementation of the "schema2" virtual table ** for displaying the content of various internal objects associated with ** the parsed schema. */ #ifndef SQLITE_AMALGAMATION # include "sqliteInt.h" #endif #ifndef SQLITE_OMIT_VIRTUALTABLE static const char azSchema2[] = "CREATE TABLE x(" " dbname STRING," /* Name of attached database */ " tblname STRING," /* Name of the table */ " idxname STRING," /* Index name or NULL if not applicable */ " cnum INT," /* Column number or NULL if not applicable */ " attr STRING," /* Attribute name */ " value STRING " /* Attribute value */ ");" ; typedef struct Schema2Table Schema2Table; typedef struct Schema2Cursor Schema2Cursor; typedef struct Schema2Row Schema2Row; /* ** A single row of the result */ struct Schema2Row { const char *zDb; /* Schema name. db->aDb[].zName */ const char *zTbl; /* Table name. Might be NULL */ const char *zIdx; /* Index name. Might be NULL */ int iCol; /* Column number */ const char *zAttr; /* Attribute */ char *zValue; /* Value of the attribute */ Schema2Row *pNext; /* Next row */ }; /* ** A cursor for iterating through internal schema information */ struct Schema2Cursor { sqlite3_vtab_cursor base; /* Base class. Must be first */ int iRowid; /* Current rowid */ Schema2Row *pAll; /* All rows */ Schema2Row *pCurrent; /* Current row */ Schema2Row *pLast; /* Last row */ }; /* ** The complete Schema2 virtual table */ struct Schema2Table { sqlite3_vtab base; /* Base class. Must be first */ sqlite3 *db; /* The database connection that owns this table */ }; /* ** Connect to or create an Schema2 virtual table. */ static int schema2Connect( sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVtab, char **pzErr ){ Schema2Table *pTab; pTab = (Schema2Table *)sqlite3_malloc(sizeof(Schema2Table)); memset(pTab, 0, sizeof(Schema2Table)); pTab->db = db; sqlite3_declare_vtab(db, azSchema2); *ppVtab = &pTab->base; return SQLITE_OK; } /* ** Disconnect from or destroy an Schema2 virtual table. */ static int schema2Disconnect(sqlite3_vtab *pVtab){ sqlite3_free(pVtab); return SQLITE_OK; } /* ** There is no "best-index". This virtual table always does a complete ** scan. */ static int schema2BestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ pIdxInfo->estimatedCost = 10.0; return SQLITE_OK; } /* ** Open a new Schema2 cursor. */ static int schema2Open(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ Schema2Cursor *pCsr; pCsr = (Schema2Cursor *)sqlite3_malloc(sizeof(Schema2Cursor)); memset(pCsr, 0, sizeof(Schema2Cursor)); pCsr->base.pVtab = pVTab; *ppCursor = (sqlite3_vtab_cursor *)pCsr; return SQLITE_OK; } static void schema2ResetCsr(Schema2Cursor *pCsr){ Schema2Row *pRow, *pNextRow; for(pRow=pCsr->pAll; pRow; pRow=pNextRow){ pNextRow = pRow->pNext; sqlite3_free(pRow->zValue); sqlite3_free(pRow); } pCsr->pAll = 0; pCsr->pCurrent = 0; pCsr->pLast = 0; pCsr->iRowid = 0; } /* ** Close a statvfs cursor. */ static int schema2Close(sqlite3_vtab_cursor *pCursor){ Schema2Cursor *pCsr = (Schema2Cursor *)pCursor; schema2ResetCsr(pCsr); sqlite3_free(pCsr); return SQLITE_OK; } /* ** Move a statvfs cursor to the next entry. */ static int schema2Next(sqlite3_vtab_cursor *pCursor){ Schema2Cursor *pCsr = (Schema2Cursor *)pCursor; if( pCsr->pCurrent==0 ){ pCsr->pCurrent = pCsr->pAll; }else{ pCsr->pCurrent = pCsr->pCurrent->pNext; } pCsr->iRowid++; return SQLITE_OK; } static int schema2Eof(sqlite3_vtab_cursor *pCursor){ Schema2Cursor *pCsr = (Schema2Cursor *)pCursor; return pCsr->pCurrent==0 && pCsr->iRowid>0; } /* Append a single to the cursor pCsr */ static void schema2AppendRow( Schema2Cursor *pCsr, const char *zDb, const char *zTbl, const char *zIdx, int iCol, const char *zAttr, const char *zValue, ... ){ Schema2Row *pRow = sqlite3_malloc( sizeof(*pRow) ); va_list ap; if( pRow==0 ); pRow->zDb = zDb; pRow->zTbl = zTbl; pRow->zIdx = zIdx; pRow->iCol = iCol; pRow->zAttr = zAttr; va_start(ap, zValue); pRow->zValue = sqlite3_vmprintf(zValue, ap); va_end(ap); if( pCsr->pLast==0 ){ pCsr->pAll = pRow; }else{ pCsr->pLast->pNext = pRow; } pCsr->pLast = pRow; } /* Append rows for index pIdx of table pTab which is part of the zDb schema */ static void schema2AppendIndex( Schema2Cursor *pCsr, const char *zDb, Table *pTab, Index *pIdx ){ const char *zTbl = pTab->zName; const char *zIdx = pIdx->zName; int i; schema2AppendRow(pCsr,zDb,zTbl,zIdx,-1,"zColAff","%s",pIdx->zColAff); schema2AppendRow(pCsr,zDb,zTbl,zIdx,-1,"tnum","%d",pIdx->tnum); schema2AppendRow(pCsr,zDb,zTbl,zIdx,-1,"szIdxRow","%d",pIdx->szIdxRow); schema2AppendRow(pCsr,zDb,zTbl,zIdx,-1,"nKeyCol","%d",pIdx->nKeyCol); schema2AppendRow(pCsr,zDb,zTbl,zIdx,-1,"nColumn","%d",pIdx->nColumn); schema2AppendRow(pCsr,zDb,zTbl,zIdx,-1,"onError","%d",pIdx->onError); schema2AppendRow(pCsr,zDb,zTbl,zIdx,-1,"autoIndex","%d",pIdx->autoIndex); schema2AppendRow(pCsr,zDb,zTbl,zIdx,-1,"bUnordered","%d",pIdx->bUnordered); schema2AppendRow(pCsr,zDb,zTbl,zIdx,-1,"uniqNotNull","%d",pIdx->uniqNotNull); schema2AppendRow(pCsr,zDb,zTbl,zIdx,-1,"isResized","%d",pIdx->isResized); schema2AppendRow(pCsr,zDb,zTbl,zIdx,-1,"isCovering","%d",pIdx->isCovering); for(i=0; i<pIdx->nColumn; i++){ i16 x = pIdx->aiColumn[i]; schema2AppendRow(pCsr,zDb,zTbl,zIdx,i,"zName","%s", x>=0?pTab->aCol[x].zName:"rowid"); schema2AppendRow(pCsr,zDb,zTbl,zIdx,i,"aiRowEst","%lld", (sqlite3_int64)pIdx->aiRowEst[i]); } } /* Append rows for table pTab which is part of the zDb schema */ static void schema2AppendTable( Schema2Cursor *pCsr, const char *zDb, Table *pTab ){ const char *zTbl = pTab->zName; int i; Index *pIdx; schema2AppendRow(pCsr,zDb,zTbl,0,-1,"zColAff","%s",pTab->zColAff); schema2AppendRow(pCsr,zDb,zTbl,0,-1,"nRowEst", "%lld",(sqlite3_int64)pTab->nRowEst); schema2AppendRow(pCsr,zDb,zTbl,0,-1,"tnum","%d",pTab->tnum); schema2AppendRow(pCsr,zDb,zTbl,0,-1,"iPKey","%d",pTab->iPKey); schema2AppendRow(pCsr,zDb,zTbl,0,-1,"nCol","%d",pTab->nCol); schema2AppendRow(pCsr,zDb,zTbl,0,-1,"nRef","%d",pTab->nRef); schema2AppendRow(pCsr,zDb,zTbl,0,-1,"szTabRow","%d",pTab->szTabRow); schema2AppendRow(pCsr,zDb,zTbl,0,-1,"tabFlags","%d",pTab->tabFlags); for(i=0; i<pTab->nCol; i++){ const Column *p = pTab->aCol + i; schema2AppendRow(pCsr,zDb,zTbl,0,i,"zName","%s",p->zName); schema2AppendRow(pCsr,zDb,zTbl,0,i,"zDflt","%s",p->zDflt); schema2AppendRow(pCsr,zDb,zTbl,0,i,"zType","%s",p->zType); schema2AppendRow(pCsr,zDb,zTbl,0,i,"zColl","%s",p->zColl); schema2AppendRow(pCsr,zDb,zTbl,0,i,"notNull","%d",p->notNull); schema2AppendRow(pCsr,zDb,zTbl,0,i,"affinity","%c",p->affinity); schema2AppendRow(pCsr,zDb,zTbl,0,i,"szEst","%d",p->szEst); schema2AppendRow(pCsr,zDb,zTbl,0,i,"colFlags","%04x",p->colFlags); } for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ schema2AppendIndex(pCsr, zDb, pTab, pIdx); } } /* Append rows for schema object pSchema */ static void schema2AppendSchema( Schema2Cursor *pCsr, const char *zDb, Schema *pSchema ){ HashElem *i; schema2AppendRow(pCsr,zDb,0,0,-1,"generation","%d", pSchema->iGeneration); schema2AppendRow(pCsr,zDb,0,0,-1,"file_format","%d", pSchema->file_format); schema2AppendRow(pCsr,zDb,0,0,-1,"enc","%d", pSchema->enc); schema2AppendRow(pCsr,zDb,0,0,-1,"flags","%d", pSchema->flags); schema2AppendRow(pCsr,zDb,0,0,-1,"cache_size","%d", pSchema->cache_size); for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){ schema2AppendTable(pCsr, zDb, (Table*)sqliteHashData(i)); } } static int schema2Filter( sqlite3_vtab_cursor *pCursor, int idxNum, const char *idxStr, int argc, sqlite3_value **argv ){ int iDb; Schema2Cursor *pCsr = (Schema2Cursor*)pCursor; Schema2Table *pTab = (Schema2Table*)(pCursor->pVtab); sqlite3 *db = pTab->db; schema2ResetCsr(pCsr); for(iDb=0; iDb<db->nDb; iDb++){ const char *zDb = db->aDb[iDb].zName; Schema *pSchema = db->aDb[iDb].pSchema; schema2AppendRow(pCsr,zDb,0,0,-1,"safety_level", "%d", db->aDb[iDb].safety_level); schema2AppendSchema(pCsr, zDb, pSchema); } return schema2Next(pCursor); } static int schema2Column( sqlite3_vtab_cursor *pCursor, sqlite3_context *ctx, int i ){ Schema2Cursor *pCsr = (Schema2Cursor *)pCursor; Schema2Row *pRow = pCsr->pCurrent; if( pRow==0 ) return SQLITE_OK; switch( i ){ case 0: /* dbname */ sqlite3_result_text(ctx, pRow->zDb, -1, SQLITE_STATIC); break; case 1: /* tblname */ if( pRow->zTbl ) sqlite3_result_text(ctx, pRow->zTbl, -1, SQLITE_STATIC); break; case 2: /* idxname */ if( pRow->zIdx ) sqlite3_result_text(ctx, pRow->zIdx, -1, SQLITE_STATIC); break; case 3: /* cnum */ if( pRow->iCol>=0 ) sqlite3_result_int(ctx, pRow->iCol); break; case 4: /* attr */ sqlite3_result_text(ctx, pRow->zAttr, -1, SQLITE_STATIC); break; case 5: /* value */ if( pRow->zValue ) sqlite3_result_text(ctx,pRow->zValue,-1,SQLITE_STATIC); break; } return SQLITE_OK; } static int schema2Rowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ Schema2Cursor *pCsr = (Schema2Cursor *)pCursor; *pRowid = pCsr->iRowid; return SQLITE_OK; } int sqlite3_schema2_register(sqlite3 *db){ static sqlite3_module schema2_module = { 0, /* iVersion */ schema2Connect, /* xCreate */ schema2Connect, /* xConnect */ schema2BestIndex, /* xBestIndex */ schema2Disconnect, /* xDisconnect */ schema2Disconnect, /* xDestroy */ schema2Open, /* xOpen - open a cursor */ schema2Close, /* xClose - close a cursor */ schema2Filter, /* xFilter - configure scan constraints */ schema2Next, /* xNext - advance a cursor */ schema2Eof, /* xEof - check for end of scan */ schema2Column, /* xColumn - read data */ schema2Rowid, /* xRowid - read data */ 0, /* xUpdate */ 0, /* xBegin */ 0, /* xSync */ 0, /* xCommit */ 0, /* xRollback */ 0, /* xFindMethod */ 0, /* xRename */ }; sqlite3_create_module(db, "schema2", &schema2_module, 0); return SQLITE_OK; } #endif #if defined(SQLITE_TEST) || TCLSH==2 #include <tcl.h> static int test_schema2( void *clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ #ifdef SQLITE_OMIT_VIRTUALTABLE Tcl_AppendResult(interp, "schema2 not available because of " "SQLITE_OMIT_VIRTUALTABLE", (void*)0); return TCL_ERROR; #else struct SqliteDb { sqlite3 *db; }; char *zDb; Tcl_CmdInfo cmdInfo; if( objc!=2 ){ Tcl_WrongNumArgs(interp, 1, objv, "DB"); return TCL_ERROR; } zDb = Tcl_GetString(objv[1]); if( Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){ sqlite3* db = ((struct SqliteDb*)cmdInfo.objClientData)->db; sqlite3_schema2_register(db); } return TCL_OK; #endif } int SqlitetestSchema2_Init(Tcl_Interp *interp){ Tcl_CreateObjCommand(interp, "register_schema2_vtab", test_schema2, 0, 0); return TCL_OK; } #endif /* if defined(SQLITE_TEST) || TCLSH==2 */ |