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 / vtablog.c
| 1 | /* |
| 2 | ** 2017-08-10 |
| 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 prints diagnostic information |
| 14 | ** on stdout when its key interfaces are called. This is intended for |
| 15 | ** interactive analysis and debugging of virtual table interfaces. |
| 16 | ** |
| 17 | ** HOW TO COMPILE: |
| 18 | ** |
| 19 | ** To build this extension as a separately loaded shared library or |
| 20 | ** DLL, use compiler command-lines similar to the following: |
| 21 | ** |
| 22 | ** (linux) gcc -fPIC -shared vtablog.c -o vtablog.so |
| 23 | ** (mac) clang -fPIC -dynamiclib vtablog.c -o vtablog.dylib |
| 24 | ** (windows) cl vtablog.c -link -dll -out:vtablog.dll |
| 25 | ** |
| 26 | ** USAGE EXAMPLE: |
| 27 | ** |
| 28 | ** .load ./vtablog |
| 29 | ** CREATE VIRTUAL TABLE temp.log USING vtablog( |
| 30 | ** schema='CREATE TABLE x(a,b,c)', |
| 31 | ** rows=25 |
| 32 | ** ); |
| 33 | ** SELECT * FROM log; |
| 34 | ** |
| 35 | ** ARGUMENTS TO CREATE VIRTUAL TABLE: |
| 36 | ** |
| 37 | ** In "CREATE VIRTUAL TABLE temp.log AS vtablog(ARGS....)" statement, the |
| 38 | ** ARGS argument is a list of key-value pairs that can be any of the |
| 39 | ** following. |
| 40 | ** |
| 41 | ** schema=TEXT Text is a CREATE TABLE statement that defines |
| 42 | ** the schema of the new virtual table. |
| 43 | ** |
| 44 | ** rows=N The table as N rows. |
| 45 | ** |
| 46 | ** consume_order_by=N If the left-most ORDER BY terms is ASC and |
| 47 | ** against column N (where the leftmost column |
| 48 | ** is #1) then set the orderByConsumed=1 flag in |
| 49 | ** xBestIndex. Or if the left-most ORDER BY is |
| 50 | ** DESC and against column -N, do likewise. |
| 51 | */ |
| 52 | #include "sqlite3ext.h" |
| 53 | SQLITE_EXTENSION_INIT1 |
| 54 | #include <stdio.h> |
| 55 | #include <stdlib.h> |
| 56 | #include <assert.h> |
| 57 | #include <string.h> |
| 58 | #include <ctype.h> |
| 59 | |
| 60 | |
| 61 | /* vtablog_vtab is a subclass of sqlite3_vtab which will |
| 62 | ** serve as the underlying representation of a vtablog virtual table |
| 63 | */ |
| 64 | typedef struct vtablog_vtab vtablog_vtab; |
| 65 | struct vtablog_vtab { |
| 66 | sqlite3_vtab base; /* Base class - must be first */ |
| 67 | char *zDb; /* Schema name. argv[1] of xConnect/xCreate */ |
| 68 | char *zName; /* Table name. argv[2] of xConnect/xCreate */ |
| 69 | int nRow; /* Number of rows in the table */ |
| 70 | int nCursor; /* Number of cursors created */ |
| 71 | int iConsumeOB; /* Consume the ORDER BY clause if on column N-th |
| 72 | ** and consumeOB=N or consumeOB=(-N) and DESC */ |
| 73 | }; |
| 74 | |
| 75 | /* vtablog_cursor is a subclass of sqlite3_vtab_cursor which will |
| 76 | ** serve as the underlying representation of a cursor that scans |
| 77 | ** over rows of the result |
| 78 | */ |
| 79 | typedef struct vtablog_cursor vtablog_cursor; |
| 80 | struct vtablog_cursor { |
| 81 | sqlite3_vtab_cursor base; /* Base class - must be first */ |
| 82 | int iCursor; /* Cursor number */ |
| 83 | sqlite3_int64 iRowid; /* The rowid */ |
| 84 | }; |
| 85 | |
| 86 | /* Skip leading whitespace. Return a pointer to the first non-whitespace |
| 87 | ** character, or to the zero terminator if the string has only whitespace */ |
| 88 | static const char *vtablog_skip_whitespace(const char *z){ |
| 89 | while( isspace((unsigned char)z[0]) ) z++; |
| 90 | return z; |
| 91 | } |
| 92 | |
| 93 | /* Remove trailing whitespace from the end of string z[] */ |
| 94 | static void vtablog_trim_whitespace(char *z){ |
| 95 | size_t n = strlen(z); |
| 96 | while( n>0 && isspace((unsigned char)z[n]) ) n--; |
| 97 | z[n] = 0; |
| 98 | } |
| 99 | |
| 100 | /* Dequote the string */ |
| 101 | static void vtablog_dequote(char *z){ |
| 102 | int j; |
| 103 | char cQuote = z[0]; |
| 104 | size_t i, n; |
| 105 | |
| 106 | if( cQuote!='\'' && cQuote!='"' ) return; |
| 107 | n = strlen(z); |
| 108 | if( n<2 || z[n-1]!=z[0] ) return; |
| 109 | for(i=1, j=0; i<n-1; i++){ |
| 110 | if( z[i]==cQuote && z[i+1]==cQuote ) i++; |
| 111 | z[j++] = z[i]; |
| 112 | } |
| 113 | z[j] = 0; |
| 114 | } |
| 115 | |
| 116 | /* Check to see if the string is of the form: "TAG = VALUE" with optional |
| 117 | ** whitespace before and around tokens. If it is, return a pointer to the |
| 118 | ** first character of VALUE. If it is not, return NULL. |
| 119 | */ |
| 120 | static const char *vtablog_parameter(const char *zTag, int nTag, const char *z){ |
| 121 | z = vtablog_skip_whitespace(z); |
| 122 | if( strncmp(zTag, z, nTag)!=0 ) return 0; |
| 123 | z = vtablog_skip_whitespace(z+nTag); |
| 124 | if( z[0]!='=' ) return 0; |
| 125 | return vtablog_skip_whitespace(z+1); |
| 126 | } |
| 127 | |
| 128 | /* Decode a parameter that requires a dequoted string. |
| 129 | ** |
| 130 | ** Return non-zero on an error. |
| 131 | */ |
| 132 | static int vtablog_string_parameter( |
| 133 | char **pzErr, /* Leave the error message here, if there is one */ |
| 134 | const char *zParam, /* Parameter we are checking for */ |
| 135 | const char *zArg, /* Raw text of the virtual table argment */ |
| 136 | char **pzVal /* Write the dequoted string value here */ |
| 137 | ){ |
| 138 | const char *zValue; |
| 139 | zValue = vtablog_parameter(zParam,(int)strlen(zParam),zArg); |
| 140 | if( zValue==0 ) return 0; |
| 141 | if( *pzVal ){ |
| 142 | *pzErr = sqlite3_mprintf("more than one '%s' parameter", zParam); |
| 143 | return 1; |
| 144 | } |
| 145 | *pzVal = sqlite3_mprintf("%s", zValue); |
| 146 | if( *pzVal==0 ){ |
| 147 | *pzErr = sqlite3_mprintf("out of memory"); |
| 148 | return 1; |
| 149 | } |
| 150 | vtablog_trim_whitespace(*pzVal); |
| 151 | vtablog_dequote(*pzVal); |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | #if 0 /* not used - yet */ |
| 156 | /* Return 0 if the argument is false and 1 if it is true. Return -1 if |
| 157 | ** we cannot really tell. |
| 158 | */ |
| 159 | static int vtablog_boolean(const char *z){ |
| 160 | if( sqlite3_stricmp("yes",z)==0 |
| 161 | || sqlite3_stricmp("on",z)==0 |
| 162 | || sqlite3_stricmp("true",z)==0 |
| 163 | || (z[0]=='1' && z[1]==0) |
| 164 | ){ |
| 165 | return 1; |
| 166 | } |
| 167 | if( sqlite3_stricmp("no",z)==0 |
| 168 | || sqlite3_stricmp("off",z)==0 |
| 169 | || sqlite3_stricmp("false",z)==0 |
| 170 | || (z[0]=='0' && z[1]==0) |
| 171 | ){ |
| 172 | return 0; |
| 173 | } |
| 174 | return -1; |
| 175 | } |
| 176 | #endif |
| 177 | |
| 178 | /* |
| 179 | ** The vtablogConnect() method is invoked to create a new |
| 180 | ** vtablog_vtab that describes the vtablog virtual table. |
| 181 | ** |
| 182 | ** Think of this routine as the constructor for vtablog_vtab objects. |
| 183 | ** |
| 184 | ** All this routine needs to do is: |
| 185 | ** |
| 186 | ** (1) Allocate the vtablog_vtab object and initialize all fields. |
| 187 | ** |
| 188 | ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the |
| 189 | ** result set of queries against vtablog will look like. |
| 190 | */ |
| 191 | static int vtablogConnectCreate( |
| 192 | sqlite3 *db, |
| 193 | void *pAux, |
| 194 | int argc, const char *const*argv, |
| 195 | sqlite3_vtab **ppVtab, |
| 196 | char **pzErr, |
| 197 | int isCreate |
| 198 | ){ |
| 199 | vtablog_vtab *pNew; |
| 200 | int i; |
| 201 | int rc; |
| 202 | char *zSchema = 0; |
| 203 | char *zNRow = 0; |
| 204 | char *zConsumeOB = 0; |
| 205 | |
| 206 | printf("%s.%s.%s():\n", argv[1], argv[2], |
| 207 | isCreate ? "xCreate" : "xConnect"); |
| 208 | printf(" argc=%d\n", argc); |
| 209 | for(i=0; i<argc; i++){ |
| 210 | printf(" argv[%d] = ", i); |
| 211 | if( argv[i] ){ |
| 212 | printf("[%s]\n", argv[i]); |
| 213 | }else{ |
| 214 | printf("NULL\n"); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | for(i=3; i<argc; i++){ |
| 219 | const char *z = argv[i]; |
| 220 | if( vtablog_string_parameter(pzErr, "schema", z, &zSchema) ){ |
| 221 | rc = SQLITE_ERROR; |
| 222 | goto vtablog_end_connect; |
| 223 | } |
| 224 | if( vtablog_string_parameter(pzErr, "rows", z, &zNRow) ){ |
| 225 | rc = SQLITE_ERROR; |
| 226 | goto vtablog_end_connect; |
| 227 | } |
| 228 | if( vtablog_string_parameter(pzErr, "consume_order_by", z, &zConsumeOB) ){ |
| 229 | rc = SQLITE_ERROR; |
| 230 | goto vtablog_end_connect; |
| 231 | } |
| 232 | } |
| 233 | if( zSchema==0 ){ |
| 234 | zSchema = sqlite3_mprintf("%s","CREATE TABLE x(a,b);"); |
| 235 | if( zSchema==0 ){ |
| 236 | rc = SQLITE_NOMEM; |
| 237 | goto vtablog_end_connect; |
| 238 | } |
| 239 | } |
| 240 | printf(" schema = '%s'\n", zSchema); |
| 241 | rc = sqlite3_declare_vtab(db, zSchema); |
| 242 | if( rc==SQLITE_OK ){ |
| 243 | pNew = sqlite3_malloc64( sizeof(*pNew) ); |
| 244 | *ppVtab = (sqlite3_vtab*)pNew; |
| 245 | if( pNew==0 ) return SQLITE_NOMEM; |
| 246 | memset(pNew, 0, sizeof(*pNew)); |
| 247 | pNew->nRow = 10; |
| 248 | if( zNRow ) pNew->nRow = atoi(zNRow); |
| 249 | printf(" nrow = %d\n", pNew->nRow); |
| 250 | if( zConsumeOB ) pNew->iConsumeOB = atoi(zConsumeOB); |
| 251 | if( pNew->iConsumeOB ){ |
| 252 | printf(" consume_order_by = %d\n", pNew->iConsumeOB); |
| 253 | } |
| 254 | pNew->zDb = sqlite3_mprintf("%s", argv[1]); |
| 255 | pNew->zName = sqlite3_mprintf("%s", argv[2]); |
| 256 | } |
| 257 | |
| 258 | vtablog_end_connect: |
| 259 | sqlite3_free(zSchema); |
| 260 | sqlite3_free(zNRow); |
| 261 | sqlite3_free(zConsumeOB); |
| 262 | return rc; |
| 263 | } |
| 264 | static int vtablogCreate( |
| 265 | sqlite3 *db, |
| 266 | void *pAux, |
| 267 | int argc, const char *const*argv, |
| 268 | sqlite3_vtab **ppVtab, |
| 269 | char **pzErr |
| 270 | ){ |
| 271 | return vtablogConnectCreate(db,pAux,argc,argv,ppVtab,pzErr,1); |
| 272 | } |
| 273 | static int vtablogConnect( |
| 274 | sqlite3 *db, |
| 275 | void *pAux, |
| 276 | int argc, const char *const*argv, |
| 277 | sqlite3_vtab **ppVtab, |
| 278 | char **pzErr |
| 279 | ){ |
| 280 | return vtablogConnectCreate(db,pAux,argc,argv,ppVtab,pzErr,0); |
| 281 | } |
| 282 | |
| 283 | |
| 284 | /* |
| 285 | ** This method is the destructor for vtablog_vtab objects. |
| 286 | */ |
| 287 | static int vtablogDisconnect(sqlite3_vtab *pVtab){ |
| 288 | vtablog_vtab *pTab = (vtablog_vtab*)pVtab; |
| 289 | printf("%s.%s.xDisconnect()\n", pTab->zDb, pTab->zName); |
| 290 | sqlite3_free(pTab->zDb); |
| 291 | sqlite3_free(pTab->zName); |
| 292 | sqlite3_free(pVtab); |
| 293 | return SQLITE_OK; |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | ** This method is (also) the destructor for vtablog_vtab objects. |
| 298 | */ |
| 299 | static int vtablogDestroy(sqlite3_vtab *pVtab){ |
| 300 | vtablog_vtab *pTab = (vtablog_vtab*)pVtab; |
| 301 | printf("%s.%s.xDestroy()\n", pTab->zDb, pTab->zName); |
| 302 | sqlite3_free(pTab->zDb); |
| 303 | sqlite3_free(pTab->zName); |
| 304 | sqlite3_free(pVtab); |
| 305 | return SQLITE_OK; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | ** Constructor for a new vtablog_cursor object. |
| 310 | */ |
| 311 | static int vtablogOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
| 312 | vtablog_vtab *pTab = (vtablog_vtab*)p; |
| 313 | vtablog_cursor *pCur; |
| 314 | printf("%s.%s.xOpen(cursor=%d)\n", pTab->zDb, pTab->zName, |
| 315 | ++pTab->nCursor); |
| 316 | pCur = sqlite3_malloc64( sizeof(*pCur) ); |
| 317 | if( pCur==0 ) return SQLITE_NOMEM; |
| 318 | memset(pCur, 0, sizeof(*pCur)); |
| 319 | pCur->iCursor = pTab->nCursor; |
| 320 | *ppCursor = &pCur->base; |
| 321 | return SQLITE_OK; |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | ** Destructor for a vtablog_cursor. |
| 326 | */ |
| 327 | static int vtablogClose(sqlite3_vtab_cursor *cur){ |
| 328 | vtablog_cursor *pCur = (vtablog_cursor*)cur; |
| 329 | vtablog_vtab *pTab = (vtablog_vtab*)cur->pVtab; |
| 330 | printf("%s.%s.xClose(cursor=%d)\n", pTab->zDb, pTab->zName, pCur->iCursor); |
| 331 | sqlite3_free(cur); |
| 332 | return SQLITE_OK; |
| 333 | } |
| 334 | |
| 335 | |
| 336 | /* |
| 337 | ** Advance a vtablog_cursor to its next row of output. |
| 338 | */ |
| 339 | static int vtablogNext(sqlite3_vtab_cursor *cur){ |
| 340 | vtablog_cursor *pCur = (vtablog_cursor*)cur; |
| 341 | vtablog_vtab *pTab = (vtablog_vtab*)cur->pVtab; |
| 342 | printf("%s.%s.xNext(cursor=%d) rowid %d -> %d\n", |
| 343 | pTab->zDb, pTab->zName, pCur->iCursor, |
| 344 | (int)pCur->iRowid, (int)pCur->iRowid+1); |
| 345 | pCur->iRowid++; |
| 346 | return SQLITE_OK; |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | ** Return values of columns for the row at which the vtablog_cursor |
| 351 | ** is currently pointing. |
| 352 | */ |
| 353 | static int vtablogColumn( |
| 354 | sqlite3_vtab_cursor *cur, /* The cursor */ |
| 355 | sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ |
| 356 | int i /* Which column to return */ |
| 357 | ){ |
| 358 | vtablog_cursor *pCur = (vtablog_cursor*)cur; |
| 359 | vtablog_vtab *pTab = (vtablog_vtab*)cur->pVtab; |
| 360 | char zVal[50]; |
| 361 | |
| 362 | if( i<26 ){ |
| 363 | sqlite3_snprintf(sizeof(zVal),zVal,"%c%d", |
| 364 | "abcdefghijklmnopqrstuvwxyz"[i], pCur->iRowid); |
| 365 | /* 0123456789 123456789 12345 */ |
| 366 | }else{ |
| 367 | sqlite3_snprintf(sizeof(zVal),zVal,"{%d}%d", i, pCur->iRowid); |
| 368 | } |
| 369 | printf("%s.%s.xColumn(cursor=%d, i=%d): [%s]\n", |
| 370 | pTab->zDb, pTab->zName, pCur->iCursor, i, zVal); |
| 371 | sqlite3_result_text(ctx, zVal, -1, SQLITE_TRANSIENT); |
| 372 | return SQLITE_OK; |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | ** Return the rowid for the current row. In this implementation, the |
| 377 | ** rowid is the same as the output value. |
| 378 | */ |
| 379 | static int vtablogRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
| 380 | vtablog_cursor *pCur = (vtablog_cursor*)cur; |
| 381 | vtablog_vtab *pTab = (vtablog_vtab*)cur->pVtab; |
| 382 | printf("%s.%s.xRowid(cursor=%d): %d\n", |
| 383 | pTab->zDb, pTab->zName, pCur->iCursor, (int)pCur->iRowid); |
| 384 | *pRowid = pCur->iRowid; |
| 385 | return SQLITE_OK; |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | ** Return TRUE if the cursor has been moved off of the last |
| 390 | ** row of output. |
| 391 | */ |
| 392 | static int vtablogEof(sqlite3_vtab_cursor *cur){ |
| 393 | vtablog_cursor *pCur = (vtablog_cursor*)cur; |
| 394 | vtablog_vtab *pTab = (vtablog_vtab*)cur->pVtab; |
| 395 | int rc = pCur->iRowid >= pTab->nRow; |
| 396 | printf("%s.%s.xEof(cursor=%d): %d\n", |
| 397 | pTab->zDb, pTab->zName, pCur->iCursor, rc); |
| 398 | return rc; |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | ** Output an sqlite3_value object's value as an SQL literal. |
| 403 | */ |
| 404 | static void vtablogQuote(sqlite3_value *p){ |
| 405 | char z[50]; |
| 406 | switch( sqlite3_value_type(p) ){ |
| 407 | case SQLITE_NULL: { |
| 408 | printf("NULL"); |
| 409 | break; |
| 410 | } |
| 411 | case SQLITE_INTEGER: { |
| 412 | sqlite3_snprintf(50,z,"%lld", sqlite3_value_int64(p)); |
| 413 | printf("%s", z); |
| 414 | break; |
| 415 | } |
| 416 | case SQLITE_FLOAT: { |
| 417 | sqlite3_snprintf(50,z,"%!.20g", sqlite3_value_double(p)); |
| 418 | printf("%s", z); |
| 419 | break; |
| 420 | } |
| 421 | case SQLITE_BLOB: { |
| 422 | int n = sqlite3_value_bytes(p); |
| 423 | const unsigned char *z = (const unsigned char*)sqlite3_value_blob(p); |
| 424 | int i; |
| 425 | if( z==0 ){ |
| 426 | printf("/*OOM-fault*/"); |
| 427 | break; |
| 428 | } |
| 429 | printf("x'"); |
| 430 | for(i=0; i<n; i++) printf("%02x", z[i]); |
| 431 | printf("'"); |
| 432 | break; |
| 433 | } |
| 434 | case SQLITE_TEXT: { |
| 435 | const char *z = (const char*)sqlite3_value_text(p); |
| 436 | int i; |
| 437 | char c; |
| 438 | if( z==0 ) z = ""; |
| 439 | for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 440 | if( c==0 ){ |
| 441 | printf("'%s'",z); |
| 442 | }else{ |
| 443 | printf("'"); |
| 444 | while( *z ){ |
| 445 | for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 446 | if( c=='\'' ) i++; |
| 447 | if( i ){ |
| 448 | printf("%.*s", i, z); |
| 449 | z += i; |
| 450 | } |
| 451 | if( c=='\'' ){ |
| 452 | printf("'"); |
| 453 | continue; |
| 454 | } |
| 455 | if( c==0 ){ |
| 456 | break; |
| 457 | } |
| 458 | z++; |
| 459 | } |
| 460 | printf("'"); |
| 461 | } |
| 462 | break; |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | |
| 468 | /* |
| 469 | ** This method is called to "rewind" the vtablog_cursor object back |
| 470 | ** to the first row of output. This method is always called at least |
| 471 | ** once prior to any call to vtablogColumn() or vtablogRowid() or |
| 472 | ** vtablogEof(). |
| 473 | */ |
| 474 | static int vtablogFilter( |
| 475 | sqlite3_vtab_cursor *cur, |
| 476 | int idxNum, const char *idxStr, |
| 477 | int argc, sqlite3_value **argv |
| 478 | ){ |
| 479 | vtablog_cursor *pCur = (vtablog_cursor *)cur; |
| 480 | vtablog_vtab *pTab = (vtablog_vtab*)cur->pVtab; |
| 481 | printf("%s.%s.xFilter(cursor=%d):\n", pTab->zDb, pTab->zName, pCur->iCursor); |
| 482 | pCur->iRowid = 0; |
| 483 | return SQLITE_OK; |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | ** Return an sqlite3_index_info operator name in static space. |
| 488 | ** The name is possibly overwritten on subsequent calls. |
| 489 | */ |
| 490 | static char *vtablogOpName(unsigned char op){ |
| 491 | static char zUnknown[30]; |
| 492 | char *zOut; |
| 493 | switch( op ){ |
| 494 | case SQLITE_INDEX_CONSTRAINT_EQ: zOut = "EQ"; break; |
| 495 | case SQLITE_INDEX_CONSTRAINT_GT: zOut = "GT"; break; |
| 496 | case SQLITE_INDEX_CONSTRAINT_LE: zOut = "LE"; break; |
| 497 | case SQLITE_INDEX_CONSTRAINT_LT: zOut = "LT"; break; |
| 498 | case SQLITE_INDEX_CONSTRAINT_GE: zOut = "GE"; break; |
| 499 | case SQLITE_INDEX_CONSTRAINT_MATCH: zOut = "MATCH"; break; |
| 500 | case SQLITE_INDEX_CONSTRAINT_LIKE: zOut = "LIKE"; break; |
| 501 | case SQLITE_INDEX_CONSTRAINT_GLOB: zOut = "GLOB"; break; |
| 502 | case SQLITE_INDEX_CONSTRAINT_REGEXP: zOut = "REGEXP"; break; |
| 503 | case SQLITE_INDEX_CONSTRAINT_NE: zOut = "NE"; break; |
| 504 | case SQLITE_INDEX_CONSTRAINT_ISNOT: zOut = "ISNOT"; break; |
| 505 | case SQLITE_INDEX_CONSTRAINT_ISNOTNULL: zOut = "ISNOTNULL"; break; |
| 506 | case SQLITE_INDEX_CONSTRAINT_ISNULL: zOut = "ISNULL"; break; |
| 507 | case SQLITE_INDEX_CONSTRAINT_IS: zOut = "IS"; break; |
| 508 | case SQLITE_INDEX_CONSTRAINT_LIMIT: zOut = "LIMIT"; break; |
| 509 | case SQLITE_INDEX_CONSTRAINT_OFFSET: zOut = "OFFSET"; break; |
| 510 | case SQLITE_INDEX_CONSTRAINT_FUNCTION: zOut = "FUNCTION"; break; |
| 511 | default: |
| 512 | sqlite3_snprintf(sizeof(zUnknown),zUnknown,"%d",op); |
| 513 | zOut = zUnknown; |
| 514 | break; |
| 515 | } |
| 516 | return zOut; |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | ** SQLite will invoke this method one or more times while planning a query |
| 521 | ** that uses the vtablog virtual table. This routine needs to create |
| 522 | ** a query plan for each invocation and compute an estimated cost for that |
| 523 | ** plan. |
| 524 | */ |
| 525 | static int vtablogBestIndex( |
| 526 | sqlite3_vtab *tab, |
| 527 | sqlite3_index_info *p |
| 528 | ){ |
| 529 | vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 530 | int i; |
| 531 | printf("%s.%s.xBestIndex():\n", pTab->zDb, pTab->zName); |
| 532 | printf(" colUsed: 0x%016llx\n", p->colUsed); |
| 533 | printf(" nConstraint: %d\n", p->nConstraint); |
| 534 | for(i=0; i<p->nConstraint; i++){ |
| 535 | sqlite3_value *pVal = 0; |
| 536 | int rc = sqlite3_vtab_rhs_value(p, i, &pVal); |
| 537 | printf( |
| 538 | " constraint[%d]: col=%d termid=%d op=%s usabled=%d coll=%s rhs=", |
| 539 | i, |
| 540 | p->aConstraint[i].iColumn, |
| 541 | p->aConstraint[i].iTermOffset, |
| 542 | vtablogOpName(p->aConstraint[i].op), |
| 543 | p->aConstraint[i].usable, |
| 544 | sqlite3_vtab_collation(p,i) |
| 545 | ); |
| 546 | if( rc==SQLITE_OK ){ |
| 547 | vtablogQuote(pVal); |
| 548 | printf("\n"); |
| 549 | }else{ |
| 550 | printf("N/A\n"); |
| 551 | } |
| 552 | } |
| 553 | printf(" nOrderBy: %d\n", p->nOrderBy); |
| 554 | if( p->nOrderBy ){ |
| 555 | for(i=0; i<p->nOrderBy; i++){ |
| 556 | printf(" orderby[%d]: col=%d desc=%d\n", |
| 557 | i, |
| 558 | p->aOrderBy[i].iColumn, |
| 559 | p->aOrderBy[i].desc); |
| 560 | } |
| 561 | if( pTab->iConsumeOB ){ |
| 562 | int N = p->aOrderBy[0].iColumn+1; |
| 563 | if( (p->aOrderBy[0].desc && N==-pTab->iConsumeOB) |
| 564 | || (!p->aOrderBy[0].desc && N==pTab->iConsumeOB) |
| 565 | ){ |
| 566 | p->orderByConsumed = 1; |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | p->estimatedCost = (double)500; |
| 571 | p->estimatedRows = 500; |
| 572 | printf(" idxNum=%d\n", p->idxNum); |
| 573 | printf(" idxStr=NULL\n"); |
| 574 | printf(" sqlite3_vtab_distinct()=%d\n", sqlite3_vtab_distinct(p)); |
| 575 | printf(" orderByConsumed=%d\n", p->orderByConsumed); |
| 576 | printf(" estimatedCost=%g\n", p->estimatedCost); |
| 577 | printf(" estimatedRows=%lld\n", p->estimatedRows); |
| 578 | return SQLITE_OK; |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | ** SQLite invokes this method to INSERT, UPDATE, or DELETE content from |
| 583 | ** the table. |
| 584 | ** |
| 585 | ** This implementation does not actually make any changes to the table |
| 586 | ** content. It merely logs the fact that the method was invoked |
| 587 | */ |
| 588 | static int vtablogUpdate( |
| 589 | sqlite3_vtab *tab, |
| 590 | int argc, |
| 591 | sqlite3_value **argv, |
| 592 | sqlite_int64 *pRowid |
| 593 | ){ |
| 594 | vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 595 | int i; |
| 596 | printf("%s.%s.xUpdate():\n", pTab->zDb, pTab->zName); |
| 597 | printf(" argc=%d\n", argc); |
| 598 | for(i=0; i<argc; i++){ |
| 599 | printf(" argv[%d]=", i); |
| 600 | vtablogQuote(argv[i]); |
| 601 | printf("\n"); |
| 602 | } |
| 603 | return SQLITE_OK; |
| 604 | } |
| 605 | |
| 606 | static int vtablogBegin(sqlite3_vtab *tab){ |
| 607 | vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 608 | printf("%s.%s.xBegin()\n", pTab->zDb, pTab->zName); |
| 609 | return SQLITE_OK; |
| 610 | } |
| 611 | static int vtablogSync(sqlite3_vtab *tab){ |
| 612 | vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 613 | printf("%s.%s.xSync()\n", pTab->zDb, pTab->zName); |
| 614 | return SQLITE_OK; |
| 615 | } |
| 616 | static int vtablogCommit(sqlite3_vtab *tab){ |
| 617 | vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 618 | printf("%s.%s.xCommit()\n", pTab->zDb, pTab->zName); |
| 619 | return SQLITE_OK; |
| 620 | } |
| 621 | static int vtablogRollback(sqlite3_vtab *tab){ |
| 622 | vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 623 | printf("%s.%s.xRollback()\n", pTab->zDb, pTab->zName); |
| 624 | return SQLITE_OK; |
| 625 | } |
| 626 | static int vtablogSavepoint(sqlite3_vtab *tab, int N){ |
| 627 | vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 628 | printf("%s.%s.xSavepoint(%d)\n", pTab->zDb, pTab->zName, N); |
| 629 | return SQLITE_OK; |
| 630 | } |
| 631 | static int vtablogRelease(sqlite3_vtab *tab, int N){ |
| 632 | vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 633 | printf("%s.%s.xRelease(%d)\n", pTab->zDb, pTab->zName, N); |
| 634 | return SQLITE_OK; |
| 635 | } |
| 636 | static int vtablogRollbackTo(sqlite3_vtab *tab, int N){ |
| 637 | vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 638 | printf("%s.%s.xRollbackTo(%d)\n", pTab->zDb, pTab->zName, N); |
| 639 | return SQLITE_OK; |
| 640 | } |
| 641 | |
| 642 | static int vtablogFindMethod( |
| 643 | sqlite3_vtab *tab, |
| 644 | int nArg, |
| 645 | const char *zName, |
| 646 | void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), |
| 647 | void **ppArg |
| 648 | ){ |
| 649 | vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 650 | printf("%s.%s.xFindMethod(nArg=%d, zName=%s)\n", |
| 651 | pTab->zDb, pTab->zName, nArg, zName); |
| 652 | return SQLITE_OK; |
| 653 | } |
| 654 | static int vtablogRename(sqlite3_vtab *tab, const char *zNew){ |
| 655 | vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 656 | printf("%s.%s.xRename('%s')\n", pTab->zDb, pTab->zName, zNew); |
| 657 | sqlite3_free(pTab->zName); |
| 658 | pTab->zName = sqlite3_mprintf("%s", zNew); |
| 659 | return SQLITE_OK; |
| 660 | } |
| 661 | |
| 662 | /* Any table name that contains the text "shadow" is seen as a |
| 663 | ** shadow table. Nothing else is. |
| 664 | */ |
| 665 | static int vtablogShadowName(const char *zName){ |
| 666 | printf("vtablog.xShadowName('%s')\n", zName); |
| 667 | return sqlite3_strglob("*shadow*", zName)==0; |
| 668 | } |
| 669 | |
| 670 | static int vtablogIntegrity( |
| 671 | sqlite3_vtab *tab, |
| 672 | const char *zSchema, |
| 673 | const char *zTabName, |
| 674 | int mFlags, |
| 675 | char **pzErr |
| 676 | ){ |
| 677 | vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 678 | printf("%s.%s.xIntegrity(mFlags=0x%x)\n", pTab->zDb, pTab->zName, mFlags); |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | /* |
| 683 | ** This following structure defines all the methods for the |
| 684 | ** vtablog virtual table. |
| 685 | */ |
| 686 | static sqlite3_module vtablogModule = { |
| 687 | 4, /* iVersion */ |
| 688 | vtablogCreate, /* xCreate */ |
| 689 | vtablogConnect, /* xConnect */ |
| 690 | vtablogBestIndex, /* xBestIndex */ |
| 691 | vtablogDisconnect, /* xDisconnect */ |
| 692 | vtablogDestroy, /* xDestroy */ |
| 693 | vtablogOpen, /* xOpen - open a cursor */ |
| 694 | vtablogClose, /* xClose - close a cursor */ |
| 695 | vtablogFilter, /* xFilter - configure scan constraints */ |
| 696 | vtablogNext, /* xNext - advance a cursor */ |
| 697 | vtablogEof, /* xEof - check for end of scan */ |
| 698 | vtablogColumn, /* xColumn - read data */ |
| 699 | vtablogRowid, /* xRowid - read data */ |
| 700 | vtablogUpdate, /* xUpdate */ |
| 701 | vtablogBegin, /* xBegin */ |
| 702 | vtablogSync, /* xSync */ |
| 703 | vtablogCommit, /* xCommit */ |
| 704 | vtablogRollback, /* xRollback */ |
| 705 | vtablogFindMethod, /* xFindMethod */ |
| 706 | vtablogRename, /* xRename */ |
| 707 | vtablogSavepoint, /* xSavepoint */ |
| 708 | vtablogRelease, /* xRelease */ |
| 709 | vtablogRollbackTo, /* xRollbackTo */ |
| 710 | vtablogShadowName, /* xShadowName */ |
| 711 | vtablogIntegrity /* xIntegrity */ |
| 712 | }; |
| 713 | |
| 714 | #ifdef _WIN32 |
| 715 | __declspec(dllexport) |
| 716 | #endif |
| 717 | int sqlite3_vtablog_init( |
| 718 | sqlite3 *db, |
| 719 | char **pzErrMsg, |
| 720 | const sqlite3_api_routines *pApi |
| 721 | ){ |
| 722 | int rc; |
| 723 | SQLITE_EXTENSION_INIT2(pApi); |
| 724 | rc = sqlite3_create_module(db, "vtablog", &vtablogModule, 0); |
| 725 | return rc; |
| 726 | } |