| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 ** 2013 Jan 11 | |
| 3 ** | |
| 4 ** The author disclaims copyright to this source code. In place of | |
| 5 ** a legal notice, here is a blessing: | |
| 6 ** | |
| 7 ** May you do good and not evil. | |
| 8 ** May you find forgiveness for yourself and forgive others. | |
| 9 ** May you share freely, never taking more than you give. | |
| 10 ** | |
| 11 ************************************************************************* | |
| 12 ** Code for testing the virtual table interfaces. This code | |
| 13 ** is not included in the SQLite library. It is used for automated | |
| 14 ** testing of the SQLite library. | |
| 15 ** | |
| 16 ** The FS virtual table is created as follows: | |
| 17 ** | |
| 18 ** CREATE VIRTUAL TABLE tbl USING fs(idx); | |
| 19 ** | |
| 20 ** where idx is the name of a table in the db with 2 columns. The virtual | |
| 21 ** table also has two columns - file path and file contents. | |
| 22 ** | |
| 23 ** The first column of table idx must be an IPK, and the second contains file | |
| 24 ** paths. For example: | |
| 25 ** | |
| 26 ** CREATE TABLE idx(id INTEGER PRIMARY KEY, path TEXT); | |
| 27 ** INSERT INTO idx VALUES(4, '/etc/passwd'); | |
| 28 ** | |
| 29 ** Adding the row to the idx table automatically creates a row in the | |
| 30 ** virtual table with rowid=4, path=/etc/passwd and a text field that | |
| 31 ** contains data read from file /etc/passwd on disk. | |
| 32 */ | |
| 33 #include "sqliteInt.h" | |
| 34 #include "tcl.h" | |
| 35 | |
| 36 #include <stdlib.h> | |
| 37 #include <string.h> | |
| 38 #include <sys/types.h> | |
| 39 #include <sys/stat.h> | |
| 40 #include <fcntl.h> | |
| 41 | |
| 42 #if SQLITE_OS_UNIX | |
| 43 # include <unistd.h> | |
| 44 #endif | |
| 45 #if SQLITE_OS_WIN | |
| 46 # include <io.h> | |
| 47 #endif | |
| 48 | |
| 49 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
| 50 | |
| 51 typedef struct fs_vtab fs_vtab; | |
| 52 typedef struct fs_cursor fs_cursor; | |
| 53 | |
| 54 /* | |
| 55 ** A fs virtual-table object | |
| 56 */ | |
| 57 struct fs_vtab { | |
| 58 sqlite3_vtab base; | |
| 59 sqlite3 *db; | |
| 60 char *zDb; /* Name of db containing zTbl */ | |
| 61 char *zTbl; /* Name of docid->file map table */ | |
| 62 }; | |
| 63 | |
| 64 /* A fs cursor object */ | |
| 65 struct fs_cursor { | |
| 66 sqlite3_vtab_cursor base; | |
| 67 sqlite3_stmt *pStmt; | |
| 68 char *zBuf; | |
| 69 int nBuf; | |
| 70 int nAlloc; | |
| 71 }; | |
| 72 | |
| 73 /* | |
| 74 ** This function is the implementation of both the xConnect and xCreate | |
| 75 ** methods of the fs virtual table. | |
| 76 ** | |
| 77 ** The argv[] array contains the following: | |
| 78 ** | |
| 79 ** argv[0] -> module name ("fs") | |
| 80 ** argv[1] -> database name | |
| 81 ** argv[2] -> table name | |
| 82 ** argv[...] -> other module argument fields. | |
| 83 */ | |
| 84 static int fsConnect( | |
| 85 sqlite3 *db, | |
| 86 void *pAux, | |
| 87 int argc, const char *const*argv, | |
| 88 sqlite3_vtab **ppVtab, | |
| 89 char **pzErr | |
| 90 ){ | |
| 91 fs_vtab *pVtab; | |
| 92 int nByte; | |
| 93 const char *zTbl; | |
| 94 const char *zDb = argv[1]; | |
| 95 | |
| 96 if( argc!=4 ){ | |
| 97 *pzErr = sqlite3_mprintf("wrong number of arguments"); | |
| 98 return SQLITE_ERROR; | |
| 99 } | |
| 100 zTbl = argv[3]; | |
| 101 | |
| 102 nByte = sizeof(fs_vtab) + (int)strlen(zTbl) + 1 + (int)strlen(zDb) + 1; | |
| 103 pVtab = (fs_vtab *)sqlite3MallocZero( nByte ); | |
| 104 if( !pVtab ) return SQLITE_NOMEM; | |
| 105 | |
| 106 pVtab->zTbl = (char *)&pVtab[1]; | |
| 107 pVtab->zDb = &pVtab->zTbl[strlen(zTbl)+1]; | |
| 108 pVtab->db = db; | |
| 109 memcpy(pVtab->zTbl, zTbl, strlen(zTbl)); | |
| 110 memcpy(pVtab->zDb, zDb, strlen(zDb)); | |
| 111 *ppVtab = &pVtab->base; | |
| 112 sqlite3_declare_vtab(db, "CREATE TABLE xyz(path TEXT, data TEXT)"); | |
| 113 | |
| 114 return SQLITE_OK; | |
| 115 } | |
| 116 /* Note that for this virtual table, the xCreate and xConnect | |
| 117 ** methods are identical. */ | |
| 118 | |
| 119 static int fsDisconnect(sqlite3_vtab *pVtab){ | |
| 120 sqlite3_free(pVtab); | |
| 121 return SQLITE_OK; | |
| 122 } | |
| 123 /* The xDisconnect and xDestroy methods are also the same */ | |
| 124 | |
| 125 /* | |
| 126 ** Open a new fs cursor. | |
| 127 */ | |
| 128 static int fsOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ | |
| 129 fs_cursor *pCur; | |
| 130 pCur = sqlite3MallocZero(sizeof(fs_cursor)); | |
| 131 *ppCursor = &pCur->base; | |
| 132 return SQLITE_OK; | |
| 133 } | |
| 134 | |
| 135 /* | |
| 136 ** Close a fs cursor. | |
| 137 */ | |
| 138 static int fsClose(sqlite3_vtab_cursor *cur){ | |
| 139 fs_cursor *pCur = (fs_cursor *)cur; | |
| 140 sqlite3_finalize(pCur->pStmt); | |
| 141 sqlite3_free(pCur->zBuf); | |
| 142 sqlite3_free(pCur); | |
| 143 return SQLITE_OK; | |
| 144 } | |
| 145 | |
| 146 static int fsNext(sqlite3_vtab_cursor *cur){ | |
| 147 fs_cursor *pCur = (fs_cursor *)cur; | |
| 148 int rc; | |
| 149 | |
| 150 rc = sqlite3_step(pCur->pStmt); | |
| 151 if( rc==SQLITE_ROW || rc==SQLITE_DONE ) rc = SQLITE_OK; | |
| 152 | |
| 153 return rc; | |
| 154 } | |
| 155 | |
| 156 static int fsFilter( | |
| 157 sqlite3_vtab_cursor *pVtabCursor, | |
| 158 int idxNum, const char *idxStr, | |
| 159 int argc, sqlite3_value **argv | |
| 160 ){ | |
| 161 int rc; | |
| 162 fs_cursor *pCur = (fs_cursor *)pVtabCursor; | |
| 163 fs_vtab *p = (fs_vtab *)(pVtabCursor->pVtab); | |
| 164 | |
| 165 assert( (idxNum==0 && argc==0) || (idxNum==1 && argc==1) ); | |
| 166 if( idxNum==1 ){ | |
| 167 char *zStmt = sqlite3_mprintf( | |
| 168 "SELECT * FROM %Q.%Q WHERE rowid=?", p->zDb, p->zTbl); | |
| 169 if( !zStmt ) return SQLITE_NOMEM; | |
| 170 rc = sqlite3_prepare_v2(p->db, zStmt, -1, &pCur->pStmt, 0); | |
| 171 sqlite3_free(zStmt); | |
| 172 if( rc==SQLITE_OK ){ | |
| 173 sqlite3_bind_value(pCur->pStmt, 1, argv[0]); | |
| 174 } | |
| 175 }else{ | |
| 176 char *zStmt = sqlite3_mprintf("SELECT * FROM %Q.%Q", p->zDb, p->zTbl); | |
| 177 if( !zStmt ) return SQLITE_NOMEM; | |
| 178 rc = sqlite3_prepare_v2(p->db, zStmt, -1, &pCur->pStmt, 0); | |
| 179 sqlite3_free(zStmt); | |
| 180 } | |
| 181 | |
| 182 if( rc==SQLITE_OK ){ | |
| 183 rc = fsNext(pVtabCursor); | |
| 184 } | |
| 185 return rc; | |
| 186 } | |
| 187 | |
| 188 static int fsColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ | |
| 189 fs_cursor *pCur = (fs_cursor*)cur; | |
| 190 | |
| 191 assert( i==0 || i==1 ); | |
| 192 if( i==0 ){ | |
| 193 sqlite3_result_value(ctx, sqlite3_column_value(pCur->pStmt, 0)); | |
| 194 }else{ | |
| 195 const char *zFile = (const char *)sqlite3_column_text(pCur->pStmt, 1); | |
| 196 struct stat sbuf; | |
| 197 int fd; | |
| 198 int n; | |
| 199 | |
| 200 fd = open(zFile, O_RDONLY); | |
| 201 if( fd<0 ) return SQLITE_IOERR; | |
| 202 fstat(fd, &sbuf); | |
| 203 | |
| 204 if( sbuf.st_size>=pCur->nAlloc ){ | |
| 205 int nNew = sbuf.st_size*2; | |
| 206 char *zNew; | |
| 207 if( nNew<1024 ) nNew = 1024; | |
| 208 | |
| 209 zNew = sqlite3Realloc(pCur->zBuf, nNew); | |
| 210 if( zNew==0 ){ | |
| 211 close(fd); | |
| 212 return SQLITE_NOMEM; | |
| 213 } | |
| 214 pCur->zBuf = zNew; | |
| 215 pCur->nAlloc = nNew; | |
| 216 } | |
| 217 | |
| 218 n = (int)read(fd, pCur->zBuf, sbuf.st_size); | |
| 219 close(fd); | |
| 220 if( n!=sbuf.st_size ) return SQLITE_ERROR; | |
| 221 pCur->nBuf = sbuf.st_size; | |
| 222 pCur->zBuf[pCur->nBuf] = '\0'; | |
| 223 | |
| 224 sqlite3_result_text(ctx, pCur->zBuf, -1, SQLITE_TRANSIENT); | |
| 225 } | |
| 226 return SQLITE_OK; | |
| 227 } | |
| 228 | |
| 229 static int fsRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ | |
| 230 fs_cursor *pCur = (fs_cursor*)cur; | |
| 231 *pRowid = sqlite3_column_int64(pCur->pStmt, 0); | |
| 232 return SQLITE_OK; | |
| 233 } | |
| 234 | |
| 235 static int fsEof(sqlite3_vtab_cursor *cur){ | |
| 236 fs_cursor *pCur = (fs_cursor*)cur; | |
| 237 return (sqlite3_data_count(pCur->pStmt)==0); | |
| 238 } | |
| 239 | |
| 240 static int fsBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ | |
| 241 int ii; | |
| 242 | |
| 243 for(ii=0; ii<pIdxInfo->nConstraint; ii++){ | |
| 244 struct sqlite3_index_constraint const *pCons = &pIdxInfo->aConstraint[ii]; | |
| 245 if( pCons->iColumn<0 && pCons->usable | |
| 246 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){ | |
| 247 struct sqlite3_index_constraint_usage *pUsage; | |
| 248 pUsage = &pIdxInfo->aConstraintUsage[ii]; | |
| 249 pUsage->omit = 0; | |
| 250 pUsage->argvIndex = 1; | |
| 251 pIdxInfo->idxNum = 1; | |
| 252 pIdxInfo->estimatedCost = 1.0; | |
| 253 break; | |
| 254 } | |
| 255 } | |
| 256 | |
| 257 return SQLITE_OK; | |
| 258 } | |
| 259 | |
| 260 /* | |
| 261 ** A virtual table module that provides read-only access to a | |
| 262 ** Tcl global variable namespace. | |
| 263 */ | |
| 264 static sqlite3_module fsModule = { | |
| 265 0, /* iVersion */ | |
| 266 fsConnect, | |
| 267 fsConnect, | |
| 268 fsBestIndex, | |
| 269 fsDisconnect, | |
| 270 fsDisconnect, | |
| 271 fsOpen, /* xOpen - open a cursor */ | |
| 272 fsClose, /* xClose - close a cursor */ | |
| 273 fsFilter, /* xFilter - configure scan constraints */ | |
| 274 fsNext, /* xNext - advance a cursor */ | |
| 275 fsEof, /* xEof - check for end of scan */ | |
| 276 fsColumn, /* xColumn - read data */ | |
| 277 fsRowid, /* xRowid - read data */ | |
| 278 0, /* xUpdate */ | |
| 279 0, /* xBegin */ | |
| 280 0, /* xSync */ | |
| 281 0, /* xCommit */ | |
| 282 0, /* xRollback */ | |
| 283 0, /* xFindMethod */ | |
| 284 0, /* xRename */ | |
| 285 }; | |
| 286 | |
| 287 /* | |
| 288 ** Decode a pointer to an sqlite3 object. | |
| 289 */ | |
| 290 extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb); | |
| 291 | |
| 292 /* | |
| 293 ** Register the echo virtual table module. | |
| 294 */ | |
| 295 static int register_fs_module( | |
| 296 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ | |
| 297 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ | |
| 298 int objc, /* Number of arguments */ | |
| 299 Tcl_Obj *CONST objv[] /* Command arguments */ | |
| 300 ){ | |
| 301 sqlite3 *db; | |
| 302 if( objc!=2 ){ | |
| 303 Tcl_WrongNumArgs(interp, 1, objv, "DB"); | |
| 304 return TCL_ERROR; | |
| 305 } | |
| 306 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; | |
| 307 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
| 308 sqlite3_create_module(db, "fs", &fsModule, (void *)interp); | |
| 309 #endif | |
| 310 return TCL_OK; | |
| 311 } | |
| 312 | |
| 313 #endif | |
| 314 | |
| 315 | |
| 316 /* | |
| 317 ** Register commands with the TCL interpreter. | |
| 318 */ | |
| 319 int Sqlitetestfs_Init(Tcl_Interp *interp){ | |
| 320 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
| 321 static struct { | |
| 322 char *zName; | |
| 323 Tcl_ObjCmdProc *xProc; | |
| 324 void *clientData; | |
| 325 } aObjCmd[] = { | |
| 326 { "register_fs_module", register_fs_module, 0 }, | |
| 327 }; | |
| 328 int i; | |
| 329 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ | |
| 330 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, | |
| 331 aObjCmd[i].xProc, aObjCmd[i].clientData, 0); | |
| 332 } | |
| 333 #endif | |
| 334 return TCL_OK; | |
| 335 } | |
| OLD | NEW |