sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / misc / wholenumber.c
| 1 | /* |
| 2 | ** 2011 April 02 |
| 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 | ** |
| 13 | ** This file implements a virtual table that returns the whole numbers |
| 14 | ** between 1 and 4294967295, inclusive. |
| 15 | ** |
| 16 | ** TESTING AND DEBUG USE ONLY -> This is not production code. This is |
| 17 | ** not a deliverable. Use the generate_series() virtual table for |
| 18 | ** real-world applications instead of this extension. THIS EXTENSION |
| 19 | ** MAY CONTAIN BUGS. |
| 20 | ** |
| 21 | ** Example: |
| 22 | ** |
| 23 | ** CREATE VIRTUAL TABLE nums USING wholenumber; |
| 24 | ** SELECT value FROM nums WHERE value<10; |
| 25 | ** |
| 26 | ** Results in: |
| 27 | ** |
| 28 | ** 1 2 3 4 5 6 7 8 9 |
| 29 | */ |
| 30 | #include "sqlite3ext.h" |
| 31 | SQLITE_EXTENSION_INIT1 |
| 32 | #include <assert.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 36 | |
| 37 | |
| 38 | /* A wholenumber cursor object */ |
| 39 | typedef struct wholenumber_cursor wholenumber_cursor; |
| 40 | struct wholenumber_cursor { |
| 41 | sqlite3_vtab_cursor base; /* Base class - must be first */ |
| 42 | sqlite3_int64 iValue; /* Current value */ |
| 43 | sqlite3_int64 mxValue; /* Maximum value */ |
| 44 | }; |
| 45 | |
| 46 | /* Methods for the wholenumber module */ |
| 47 | static int wholenumberConnect( |
| 48 | sqlite3 *db, |
| 49 | void *pAux, |
| 50 | int argc, const char *const*argv, |
| 51 | sqlite3_vtab **ppVtab, |
| 52 | char **pzErr |
| 53 | ){ |
| 54 | sqlite3_vtab *pNew; |
| 55 | pNew = *ppVtab = sqlite3_malloc64( sizeof(*pNew) ); |
| 56 | if( pNew==0 ) return SQLITE_NOMEM; |
| 57 | sqlite3_declare_vtab(db, "CREATE TABLE x(value)"); |
| 58 | sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); |
| 59 | memset(pNew, 0, sizeof(*pNew)); |
| 60 | return SQLITE_OK; |
| 61 | } |
| 62 | /* Note that for this virtual table, the xCreate and xConnect |
| 63 | ** methods are identical. */ |
| 64 | |
| 65 | static int wholenumberDisconnect(sqlite3_vtab *pVtab){ |
| 66 | sqlite3_free(pVtab); |
| 67 | return SQLITE_OK; |
| 68 | } |
| 69 | /* The xDisconnect and xDestroy methods are also the same */ |
| 70 | |
| 71 | |
| 72 | /* |
| 73 | ** Open a new wholenumber cursor. |
| 74 | */ |
| 75 | static int wholenumberOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
| 76 | wholenumber_cursor *pCur; |
| 77 | pCur = sqlite3_malloc64( sizeof(*pCur) ); |
| 78 | if( pCur==0 ) return SQLITE_NOMEM; |
| 79 | memset(pCur, 0, sizeof(*pCur)); |
| 80 | *ppCursor = &pCur->base; |
| 81 | return SQLITE_OK; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | ** Close a wholenumber cursor. |
| 86 | */ |
| 87 | static int wholenumberClose(sqlite3_vtab_cursor *cur){ |
| 88 | sqlite3_free(cur); |
| 89 | return SQLITE_OK; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | /* |
| 94 | ** Advance a cursor to its next row of output |
| 95 | */ |
| 96 | static int wholenumberNext(sqlite3_vtab_cursor *cur){ |
| 97 | wholenumber_cursor *pCur = (wholenumber_cursor*)cur; |
| 98 | pCur->iValue++; |
| 99 | return SQLITE_OK; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | ** Return the value associated with a wholenumber. |
| 104 | */ |
| 105 | static int wholenumberColumn( |
| 106 | sqlite3_vtab_cursor *cur, |
| 107 | sqlite3_context *ctx, |
| 108 | int i |
| 109 | ){ |
| 110 | wholenumber_cursor *pCur = (wholenumber_cursor*)cur; |
| 111 | sqlite3_result_int64(ctx, pCur->iValue); |
| 112 | return SQLITE_OK; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | ** The rowid. |
| 117 | */ |
| 118 | static int wholenumberRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
| 119 | wholenumber_cursor *pCur = (wholenumber_cursor*)cur; |
| 120 | *pRowid = pCur->iValue; |
| 121 | return SQLITE_OK; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | ** When the wholenumber_cursor.rLimit value is 0 or less, that is a signal |
| 126 | ** that the cursor has nothing more to output. |
| 127 | */ |
| 128 | static int wholenumberEof(sqlite3_vtab_cursor *cur){ |
| 129 | wholenumber_cursor *pCur = (wholenumber_cursor*)cur; |
| 130 | return pCur->iValue>pCur->mxValue || pCur->iValue==0; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | ** Called to "rewind" a cursor back to the beginning so that |
| 135 | ** it starts its output over again. Always called at least once |
| 136 | ** prior to any wholenumberColumn, wholenumberRowid, or wholenumberEof call. |
| 137 | ** |
| 138 | ** idxNum Constraints |
| 139 | ** ------ --------------------- |
| 140 | ** 0 (none) |
| 141 | ** 1 value > $argv0 |
| 142 | ** 2 value >= $argv0 |
| 143 | ** 4 value < $argv0 |
| 144 | ** 8 value <= $argv0 |
| 145 | ** |
| 146 | ** 5 value > $argv0 AND value < $argv1 |
| 147 | ** 6 value >= $argv0 AND value < $argv1 |
| 148 | ** 9 value > $argv0 AND value <= $argv1 |
| 149 | ** 10 value >= $argv0 AND value <= $argv1 |
| 150 | */ |
| 151 | static int wholenumberFilter( |
| 152 | sqlite3_vtab_cursor *pVtabCursor, |
| 153 | int idxNum, const char *idxStr, |
| 154 | int argc, sqlite3_value **argv |
| 155 | ){ |
| 156 | wholenumber_cursor *pCur = (wholenumber_cursor *)pVtabCursor; |
| 157 | sqlite3_int64 v; |
| 158 | int i = 0; |
| 159 | pCur->iValue = 1; |
| 160 | pCur->mxValue = 0xffffffff; /* 4294967295 */ |
| 161 | if( idxNum & 3 ){ |
| 162 | v = sqlite3_value_int64(argv[0]); |
| 163 | if( v<=pCur->mxValue && (idxNum&1)!=0 ) v++; |
| 164 | if( v>pCur->iValue && v<=pCur->mxValue ) pCur->iValue = v; |
| 165 | i++; |
| 166 | } |
| 167 | if( idxNum & 12 ){ |
| 168 | v = sqlite3_value_int64(argv[i]); |
| 169 | if( v>0 && ((idxNum>>2)&1)!=0 ) v--; |
| 170 | if( v>=pCur->iValue && v<pCur->mxValue ) pCur->mxValue = v; |
| 171 | } |
| 172 | return SQLITE_OK; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | ** Search for terms of these forms: |
| 177 | ** |
| 178 | ** (1) value > $value |
| 179 | ** (2) value >= $value |
| 180 | ** (4) value < $value |
| 181 | ** (8) value <= $value |
| 182 | ** |
| 183 | ** idxNum is an ORed combination of 1 or 2 with 4 or 8. |
| 184 | */ |
| 185 | static int wholenumberBestIndex( |
| 186 | sqlite3_vtab *tab, |
| 187 | sqlite3_index_info *pIdxInfo |
| 188 | ){ |
| 189 | int i; |
| 190 | int idxNum = 0; |
| 191 | int argvIdx = 1; |
| 192 | int ltIdx = -1; |
| 193 | int gtIdx = -1; |
| 194 | const struct sqlite3_index_constraint *pConstraint; |
| 195 | pConstraint = pIdxInfo->aConstraint; |
| 196 | for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ |
| 197 | if( pConstraint->usable==0 ) continue; |
| 198 | if( (idxNum & 3)==0 && pConstraint->op==SQLITE_INDEX_CONSTRAINT_GT ){ |
| 199 | idxNum |= 1; |
| 200 | ltIdx = i; |
| 201 | } |
| 202 | if( (idxNum & 3)==0 && pConstraint->op==SQLITE_INDEX_CONSTRAINT_GE ){ |
| 203 | idxNum |= 2; |
| 204 | ltIdx = i; |
| 205 | } |
| 206 | if( (idxNum & 12)==0 && pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT ){ |
| 207 | idxNum |= 4; |
| 208 | gtIdx = i; |
| 209 | } |
| 210 | if( (idxNum & 12)==0 && pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE ){ |
| 211 | idxNum |= 8; |
| 212 | gtIdx = i; |
| 213 | } |
| 214 | } |
| 215 | pIdxInfo->idxNum = idxNum; |
| 216 | if( ltIdx>=0 ){ |
| 217 | pIdxInfo->aConstraintUsage[ltIdx].argvIndex = argvIdx++; |
| 218 | pIdxInfo->aConstraintUsage[ltIdx].omit = 1; |
| 219 | } |
| 220 | if( gtIdx>=0 ){ |
| 221 | pIdxInfo->aConstraintUsage[gtIdx].argvIndex = argvIdx; |
| 222 | pIdxInfo->aConstraintUsage[gtIdx].omit = 1; |
| 223 | } |
| 224 | if( pIdxInfo->nOrderBy==1 |
| 225 | && pIdxInfo->aOrderBy[0].desc==0 |
| 226 | ){ |
| 227 | pIdxInfo->orderByConsumed = 1; |
| 228 | } |
| 229 | if( (idxNum & 12)==0 ){ |
| 230 | pIdxInfo->estimatedCost = 1e99; |
| 231 | }else if( (idxNum & 3)==0 ){ |
| 232 | pIdxInfo->estimatedCost = (double)5; |
| 233 | }else{ |
| 234 | pIdxInfo->estimatedCost = (double)1; |
| 235 | } |
| 236 | return SQLITE_OK; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | ** A virtual table module that provides read-only access to a |
| 241 | ** Tcl global variable namespace. |
| 242 | */ |
| 243 | static sqlite3_module wholenumberModule = { |
| 244 | 0, /* iVersion */ |
| 245 | wholenumberConnect, |
| 246 | wholenumberConnect, |
| 247 | wholenumberBestIndex, |
| 248 | wholenumberDisconnect, |
| 249 | wholenumberDisconnect, |
| 250 | wholenumberOpen, /* xOpen - open a cursor */ |
| 251 | wholenumberClose, /* xClose - close a cursor */ |
| 252 | wholenumberFilter, /* xFilter - configure scan constraints */ |
| 253 | wholenumberNext, /* xNext - advance a cursor */ |
| 254 | wholenumberEof, /* xEof - check for end of scan */ |
| 255 | wholenumberColumn, /* xColumn - read data */ |
| 256 | wholenumberRowid, /* xRowid - read data */ |
| 257 | 0, /* xUpdate */ |
| 258 | 0, /* xBegin */ |
| 259 | 0, /* xSync */ |
| 260 | 0, /* xCommit */ |
| 261 | 0, /* xRollback */ |
| 262 | 0, /* xFindMethod */ |
| 263 | 0, /* xRename */ |
| 264 | 0, /* xSavepoint */ |
| 265 | 0, /* xRelease */ |
| 266 | 0, /* xRollbackTo */ |
| 267 | 0, /* xShadowName */ |
| 268 | 0 /* xIntegrity */ |
| 269 | }; |
| 270 | |
| 271 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 272 | |
| 273 | #ifdef _WIN32 |
| 274 | __declspec(dllexport) |
| 275 | #endif |
| 276 | int sqlite3_wholenumber_init( |
| 277 | sqlite3 *db, |
| 278 | char **pzErrMsg, |
| 279 | const sqlite3_api_routines *pApi |
| 280 | ){ |
| 281 | int rc = SQLITE_OK; |
| 282 | SQLITE_EXTENSION_INIT2(pApi); |
| 283 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 284 | rc = sqlite3_create_module(db, "wholenumber", &wholenumberModule, 0); |
| 285 | #endif |
| 286 | return rc; |
| 287 | } |