| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2011 April 02 | 2 ** 2011 April 02 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| 11 ************************************************************************* | 11 ************************************************************************* |
| 12 ** | 12 ** |
| 13 ** This file implements a virtual table that returns the whole numbers | 13 ** This file implements a virtual table that returns the whole numbers |
| 14 ** between 1 and 4294967295, inclusive. | 14 ** between 1 and 4294967295, inclusive. |
| 15 ** | 15 ** |
| 16 ** Example: | 16 ** Example: |
| 17 ** | 17 ** |
| 18 ** CREATE VIRTUAL TABLE nums USING wholenumber; | 18 ** CREATE VIRTUAL TABLE nums USING wholenumber; |
| 19 ** SELECT value FROM nums WHERE value<10; | 19 ** SELECT value FROM nums WHERE value<10; |
| 20 ** | 20 ** |
| 21 ** Results in: | 21 ** Results in: |
| 22 ** | 22 ** |
| 23 ** 1 2 3 4 5 6 7 8 9 | 23 ** 1 2 3 4 5 6 7 8 9 |
| 24 */ | 24 */ |
| 25 #include "sqlite3.h" | 25 #include "sqlite3ext.h" |
| 26 SQLITE_EXTENSION_INIT1 |
| 26 #include <assert.h> | 27 #include <assert.h> |
| 27 #include <string.h> | 28 #include <string.h> |
| 28 | 29 |
| 29 #ifndef SQLITE_OMIT_VIRTUALTABLE | 30 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 30 | 31 |
| 31 | 32 |
| 32 /* A wholenumber cursor object */ | 33 /* A wholenumber cursor object */ |
| 33 typedef struct wholenumber_cursor wholenumber_cursor; | 34 typedef struct wholenumber_cursor wholenumber_cursor; |
| 34 struct wholenumber_cursor { | 35 struct wholenumber_cursor { |
| 35 sqlite3_vtab_cursor base; /* Base class - must be first */ | 36 sqlite3_vtab_cursor base; /* Base class - must be first */ |
| 36 unsigned iValue; /* Current value */ | 37 sqlite3_int64 iValue; /* Current value */ |
| 37 unsigned mxValue; /* Maximum value */ | 38 sqlite3_int64 mxValue; /* Maximum value */ |
| 38 }; | 39 }; |
| 39 | 40 |
| 40 /* Methods for the wholenumber module */ | 41 /* Methods for the wholenumber module */ |
| 41 static int wholenumberConnect( | 42 static int wholenumberConnect( |
| 42 sqlite3 *db, | 43 sqlite3 *db, |
| 43 void *pAux, | 44 void *pAux, |
| 44 int argc, const char *const*argv, | 45 int argc, const char *const*argv, |
| 45 sqlite3_vtab **ppVtab, | 46 sqlite3_vtab **ppVtab, |
| 46 char **pzErr | 47 char **pzErr |
| 47 ){ | 48 ){ |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 } | 211 } |
| 211 if( gtIdx>=0 ){ | 212 if( gtIdx>=0 ){ |
| 212 pIdxInfo->aConstraintUsage[gtIdx].argvIndex = argvIdx; | 213 pIdxInfo->aConstraintUsage[gtIdx].argvIndex = argvIdx; |
| 213 pIdxInfo->aConstraintUsage[gtIdx].omit = 1; | 214 pIdxInfo->aConstraintUsage[gtIdx].omit = 1; |
| 214 } | 215 } |
| 215 if( pIdxInfo->nOrderBy==1 | 216 if( pIdxInfo->nOrderBy==1 |
| 216 && pIdxInfo->aOrderBy[0].desc==0 | 217 && pIdxInfo->aOrderBy[0].desc==0 |
| 217 ){ | 218 ){ |
| 218 pIdxInfo->orderByConsumed = 1; | 219 pIdxInfo->orderByConsumed = 1; |
| 219 } | 220 } |
| 220 pIdxInfo->estimatedCost = (double)1; | 221 if( (idxNum & 12)==0 ){ |
| 222 pIdxInfo->estimatedCost = (double)100000000; |
| 223 }else if( (idxNum & 3)==0 ){ |
| 224 pIdxInfo->estimatedCost = (double)5; |
| 225 }else{ |
| 226 pIdxInfo->estimatedCost = (double)1; |
| 227 } |
| 221 return SQLITE_OK; | 228 return SQLITE_OK; |
| 222 } | 229 } |
| 223 | 230 |
| 224 /* | 231 /* |
| 225 ** A virtual table module that provides read-only access to a | 232 ** A virtual table module that provides read-only access to a |
| 226 ** Tcl global variable namespace. | 233 ** Tcl global variable namespace. |
| 227 */ | 234 */ |
| 228 static sqlite3_module wholenumberModule = { | 235 static sqlite3_module wholenumberModule = { |
| 229 0, /* iVersion */ | 236 0, /* iVersion */ |
| 230 wholenumberConnect, | 237 wholenumberConnect, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 243 0, /* xBegin */ | 250 0, /* xBegin */ |
| 244 0, /* xSync */ | 251 0, /* xSync */ |
| 245 0, /* xCommit */ | 252 0, /* xCommit */ |
| 246 0, /* xRollback */ | 253 0, /* xRollback */ |
| 247 0, /* xFindMethod */ | 254 0, /* xFindMethod */ |
| 248 0, /* xRename */ | 255 0, /* xRename */ |
| 249 }; | 256 }; |
| 250 | 257 |
| 251 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | 258 #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 252 | 259 |
| 253 | 260 #ifdef _WIN32 |
| 254 /* | 261 __declspec(dllexport) |
| 255 ** Register the wholenumber virtual table | 262 #endif |
| 256 */ | 263 int sqlite3_wholenumber_init( |
| 257 int wholenumber_register(sqlite3 *db){ | 264 sqlite3 *db, |
| 265 char **pzErrMsg, |
| 266 const sqlite3_api_routines *pApi |
| 267 ){ |
| 258 int rc = SQLITE_OK; | 268 int rc = SQLITE_OK; |
| 269 SQLITE_EXTENSION_INIT2(pApi); |
| 259 #ifndef SQLITE_OMIT_VIRTUALTABLE | 270 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 260 rc = sqlite3_create_module(db, "wholenumber", &wholenumberModule, 0); | 271 rc = sqlite3_create_module(db, "wholenumber", &wholenumberModule, 0); |
| 261 #endif | 272 #endif |
| 262 return rc; | 273 return rc; |
| 263 } | 274 } |
| 264 | |
| 265 #ifdef SQLITE_TEST | |
| 266 #include <tcl.h> | |
| 267 /* | |
| 268 ** Decode a pointer to an sqlite3 object. | |
| 269 */ | |
| 270 extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb); | |
| 271 | |
| 272 /* | |
| 273 ** Register the echo virtual table module. | |
| 274 */ | |
| 275 static int register_wholenumber_module( | |
| 276 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ | |
| 277 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ | |
| 278 int objc, /* Number of arguments */ | |
| 279 Tcl_Obj *CONST objv[] /* Command arguments */ | |
| 280 ){ | |
| 281 sqlite3 *db; | |
| 282 if( objc!=2 ){ | |
| 283 Tcl_WrongNumArgs(interp, 1, objv, "DB"); | |
| 284 return TCL_ERROR; | |
| 285 } | |
| 286 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; | |
| 287 wholenumber_register(db); | |
| 288 return TCL_OK; | |
| 289 } | |
| 290 | |
| 291 | |
| 292 /* | |
| 293 ** Register commands with the TCL interpreter. | |
| 294 */ | |
| 295 int Sqlitetestwholenumber_Init(Tcl_Interp *interp){ | |
| 296 static struct { | |
| 297 char *zName; | |
| 298 Tcl_ObjCmdProc *xProc; | |
| 299 void *clientData; | |
| 300 } aObjCmd[] = { | |
| 301 { "register_wholenumber_module", register_wholenumber_module, 0 }, | |
| 302 }; | |
| 303 int i; | |
| 304 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ | |
| 305 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, | |
| 306 aObjCmd[i].xProc, aObjCmd[i].clientData, 0); | |
| 307 } | |
| 308 return TCL_OK; | |
| 309 } | |
| 310 | |
| 311 #endif /* SQLITE_TEST */ | |
| OLD | NEW |