| 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 |
"abcdefghijklmnopqrstuvwyz"[i], pCur->iRowid); |
| 365 |
}else{ |
| 366 |
sqlite3_snprintf(sizeof(zVal),zVal,"{%d}%d", i, pCur->iRowid); |
| 367 |
} |
| 368 |
printf("%s.%s.xColumn(cursor=%d, i=%d): [%s]\n", |
| 369 |
pTab->zDb, pTab->zName, pCur->iCursor, i, zVal); |
| 370 |
sqlite3_result_text(ctx, zVal, -1, SQLITE_TRANSIENT); |
| 371 |
return SQLITE_OK; |
| 372 |
} |
| 373 |
|
| 374 |
/* |
| 375 |
** Return the rowid for the current row. In this implementation, the |
| 376 |
** rowid is the same as the output value. |
| 377 |
*/ |
| 378 |
static int vtablogRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
| 379 |
vtablog_cursor *pCur = (vtablog_cursor*)cur; |
| 380 |
vtablog_vtab *pTab = (vtablog_vtab*)cur->pVtab; |
| 381 |
printf("%s.%s.xRowid(cursor=%d): %d\n", |
| 382 |
pTab->zDb, pTab->zName, pCur->iCursor, (int)pCur->iRowid); |
| 383 |
*pRowid = pCur->iRowid; |
| 384 |
return SQLITE_OK; |
| 385 |
} |
| 386 |
|
| 387 |
/* |
| 388 |
** Return TRUE if the cursor has been moved off of the last |
| 389 |
** row of output. |
| 390 |
*/ |
| 391 |
static int vtablogEof(sqlite3_vtab_cursor *cur){ |
| 392 |
vtablog_cursor *pCur = (vtablog_cursor*)cur; |
| 393 |
vtablog_vtab *pTab = (vtablog_vtab*)cur->pVtab; |
| 394 |
int rc = pCur->iRowid >= pTab->nRow; |
| 395 |
printf("%s.%s.xEof(cursor=%d): %d\n", |
| 396 |
pTab->zDb, pTab->zName, pCur->iCursor, rc); |
| 397 |
return rc; |
| 398 |
} |
| 399 |
|
| 400 |
/* |
| 401 |
** Output an sqlite3_value object's value as an SQL literal. |
| 402 |
*/ |
| 403 |
static void vtablogQuote(sqlite3_value *p){ |
| 404 |
char z[50]; |
| 405 |
switch( sqlite3_value_type(p) ){ |
| 406 |
case SQLITE_NULL: { |
| 407 |
printf("NULL"); |
| 408 |
break; |
| 409 |
} |
| 410 |
case SQLITE_INTEGER: { |
| 411 |
sqlite3_snprintf(50,z,"%lld", sqlite3_value_int64(p)); |
| 412 |
printf("%s", z); |
| 413 |
break; |
| 414 |
} |
| 415 |
case SQLITE_FLOAT: { |
| 416 |
sqlite3_snprintf(50,z,"%!.20g", sqlite3_value_double(p)); |
| 417 |
printf("%s", z); |
| 418 |
break; |
| 419 |
} |
| 420 |
case SQLITE_BLOB: { |
| 421 |
int n = sqlite3_value_bytes(p); |
| 422 |
const unsigned char *z = (const unsigned char*)sqlite3_value_blob(p); |
| 423 |
int i; |
| 424 |
printf("x'"); |
| 425 |
for(i=0; i<n; i++) printf("%02x", z[i]); |
| 426 |
printf("'"); |
| 427 |
break; |
| 428 |
} |
| 429 |
case SQLITE_TEXT: { |
| 430 |
const char *z = (const char*)sqlite3_value_text(p); |
| 431 |
int i; |
| 432 |
char c; |
| 433 |
for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 434 |
if( c==0 ){ |
| 435 |
printf("'%s'",z); |
| 436 |
}else{ |
| 437 |
printf("'"); |
| 438 |
while( *z ){ |
| 439 |
for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
| 440 |
if( c=='\'' ) i++; |
| 441 |
if( i ){ |
| 442 |
printf("%.*s", i, z); |
| 443 |
z += i; |
| 444 |
} |
| 445 |
if( c=='\'' ){ |
| 446 |
printf("'"); |
| 447 |
continue; |
| 448 |
} |
| 449 |
if( c==0 ){ |
| 450 |
break; |
| 451 |
} |
| 452 |
z++; |
| 453 |
} |
| 454 |
printf("'"); |
| 455 |
} |
| 456 |
break; |
| 457 |
} |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
/* |
| 463 |
** This method is called to "rewind" the vtablog_cursor object back |
| 464 |
** to the first row of output. This method is always called at least |
| 465 |
** once prior to any call to vtablogColumn() or vtablogRowid() or |
| 466 |
** vtablogEof(). |
| 467 |
*/ |
| 468 |
static int vtablogFilter( |
| 469 |
sqlite3_vtab_cursor *cur, |
| 470 |
int idxNum, const char *idxStr, |
| 471 |
int argc, sqlite3_value **argv |
| 472 |
){ |
| 473 |
vtablog_cursor *pCur = (vtablog_cursor *)cur; |
| 474 |
vtablog_vtab *pTab = (vtablog_vtab*)cur->pVtab; |
| 475 |
printf("%s.%s.xFilter(cursor=%d):\n", pTab->zDb, pTab->zName, pCur->iCursor); |
| 476 |
pCur->iRowid = 0; |
| 477 |
return SQLITE_OK; |
| 478 |
} |
| 479 |
|
| 480 |
/* |
| 481 |
** Return an sqlite3_index_info operator name in static space. |
| 482 |
** The name is possibly overwritten on subsequent calls. |
| 483 |
*/ |
| 484 |
static char *vtablogOpName(unsigned char op){ |
| 485 |
static char zUnknown[30]; |
| 486 |
char *zOut; |
| 487 |
switch( op ){ |
| 488 |
case SQLITE_INDEX_CONSTRAINT_EQ: zOut = "EQ"; break; |
| 489 |
case SQLITE_INDEX_CONSTRAINT_GT: zOut = "GT"; break; |
| 490 |
case SQLITE_INDEX_CONSTRAINT_LE: zOut = "LE"; break; |
| 491 |
case SQLITE_INDEX_CONSTRAINT_LT: zOut = "LT"; break; |
| 492 |
case SQLITE_INDEX_CONSTRAINT_GE: zOut = "GE"; break; |
| 493 |
case SQLITE_INDEX_CONSTRAINT_MATCH: zOut = "MATCH"; break; |
| 494 |
case SQLITE_INDEX_CONSTRAINT_LIKE: zOut = "LIKE"; break; |
| 495 |
case SQLITE_INDEX_CONSTRAINT_GLOB: zOut = "GLOB"; break; |
| 496 |
case SQLITE_INDEX_CONSTRAINT_REGEXP: zOut = "REGEXP"; break; |
| 497 |
case SQLITE_INDEX_CONSTRAINT_NE: zOut = "NE"; break; |
| 498 |
case SQLITE_INDEX_CONSTRAINT_ISNOT: zOut = "ISNOT"; break; |
| 499 |
case SQLITE_INDEX_CONSTRAINT_ISNOTNULL: zOut = "ISNOTNULL"; break; |
| 500 |
case SQLITE_INDEX_CONSTRAINT_ISNULL: zOut = "ISNULL"; break; |
| 501 |
case SQLITE_INDEX_CONSTRAINT_IS: zOut = "IS"; break; |
| 502 |
case SQLITE_INDEX_CONSTRAINT_LIMIT: zOut = "LIMIT"; break; |
| 503 |
case SQLITE_INDEX_CONSTRAINT_OFFSET: zOut = "OFFSET"; break; |
| 504 |
case SQLITE_INDEX_CONSTRAINT_FUNCTION: zOut = "FUNCTION"; break; |
| 505 |
default: |
| 506 |
sqlite3_snprintf(sizeof(zUnknown),zUnknown,"%d",op); |
| 507 |
zOut = zUnknown; |
| 508 |
break; |
| 509 |
} |
| 510 |
return zOut; |
| 511 |
} |
| 512 |
|
| 513 |
/* |
| 514 |
** SQLite will invoke this method one or more times while planning a query |
| 515 |
** that uses the vtablog virtual table. This routine needs to create |
| 516 |
** a query plan for each invocation and compute an estimated cost for that |
| 517 |
** plan. |
| 518 |
*/ |
| 519 |
static int vtablogBestIndex( |
| 520 |
sqlite3_vtab *tab, |
| 521 |
sqlite3_index_info *p |
| 522 |
){ |
| 523 |
vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 524 |
int i; |
| 525 |
printf("%s.%s.xBestIndex():\n", pTab->zDb, pTab->zName); |
| 526 |
printf(" colUsed: 0x%016llx\n", p->colUsed); |
| 527 |
printf(" nConstraint: %d\n", p->nConstraint); |
| 528 |
for(i=0; i<p->nConstraint; i++){ |
| 529 |
sqlite3_value *pVal = 0; |
| 530 |
int rc = sqlite3_vtab_rhs_value(p, i, &pVal); |
| 531 |
printf( |
| 532 |
" constraint[%d]: col=%d termid=%d op=%s usabled=%d coll=%s rhs=", |
| 533 |
i, |
| 534 |
p->aConstraint[i].iColumn, |
| 535 |
p->aConstraint[i].iTermOffset, |
| 536 |
vtablogOpName(p->aConstraint[i].op), |
| 537 |
p->aConstraint[i].usable, |
| 538 |
sqlite3_vtab_collation(p,i) |
| 539 |
); |
| 540 |
if( rc==SQLITE_OK ){ |
| 541 |
vtablogQuote(pVal); |
| 542 |
printf("\n"); |
| 543 |
}else{ |
| 544 |
printf("N/A\n"); |
| 545 |
} |
| 546 |
} |
| 547 |
printf(" nOrderBy: %d\n", p->nOrderBy); |
| 548 |
if( p->nOrderBy ){ |
| 549 |
for(i=0; i<p->nOrderBy; i++){ |
| 550 |
printf(" orderby[%d]: col=%d desc=%d\n", |
| 551 |
i, |
| 552 |
p->aOrderBy[i].iColumn, |
| 553 |
p->aOrderBy[i].desc); |
| 554 |
} |
| 555 |
if( pTab->iConsumeOB ){ |
| 556 |
int N = p->aOrderBy[0].iColumn+1; |
| 557 |
if( (p->aOrderBy[0].desc && N==-pTab->iConsumeOB) |
| 558 |
|| (!p->aOrderBy[0].desc && N==pTab->iConsumeOB) |
| 559 |
){ |
| 560 |
p->orderByConsumed = 1; |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
p->estimatedCost = (double)500; |
| 565 |
p->estimatedRows = 500; |
| 566 |
printf(" idxNum=%d\n", p->idxNum); |
| 567 |
printf(" idxStr=NULL\n"); |
| 568 |
printf(" sqlite3_vtab_distinct()=%d\n", sqlite3_vtab_distinct(p)); |
| 569 |
printf(" orderByConsumed=%d\n", p->orderByConsumed); |
| 570 |
printf(" estimatedCost=%g\n", p->estimatedCost); |
| 571 |
printf(" estimatedRows=%lld\n", p->estimatedRows); |
| 572 |
return SQLITE_OK; |
| 573 |
} |
| 574 |
|
| 575 |
/* |
| 576 |
** SQLite invokes this method to INSERT, UPDATE, or DELETE content from |
| 577 |
** the table. |
| 578 |
** |
| 579 |
** This implementation does not actually make any changes to the table |
| 580 |
** content. It merely logs the fact that the method was invoked |
| 581 |
*/ |
| 582 |
static int vtablogUpdate( |
| 583 |
sqlite3_vtab *tab, |
| 584 |
int argc, |
| 585 |
sqlite3_value **argv, |
| 586 |
sqlite_int64 *pRowid |
| 587 |
){ |
| 588 |
vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 589 |
int i; |
| 590 |
printf("%s.%s.xUpdate():\n", pTab->zDb, pTab->zName); |
| 591 |
printf(" argc=%d\n", argc); |
| 592 |
for(i=0; i<argc; i++){ |
| 593 |
printf(" argv[%d]=", i); |
| 594 |
vtablogQuote(argv[i]); |
| 595 |
printf("\n"); |
| 596 |
} |
| 597 |
return SQLITE_OK; |
| 598 |
} |
| 599 |
|
| 600 |
static int vtablogBegin(sqlite3_vtab *tab){ |
| 601 |
vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 602 |
printf("%s.%s.xBegin()\n", pTab->zDb, pTab->zName); |
| 603 |
return SQLITE_OK; |
| 604 |
} |
| 605 |
static int vtablogSync(sqlite3_vtab *tab){ |
| 606 |
vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 607 |
printf("%s.%s.xSync()\n", pTab->zDb, pTab->zName); |
| 608 |
return SQLITE_OK; |
| 609 |
} |
| 610 |
static int vtablogCommit(sqlite3_vtab *tab){ |
| 611 |
vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 612 |
printf("%s.%s.xCommit()\n", pTab->zDb, pTab->zName); |
| 613 |
return SQLITE_OK; |
| 614 |
} |
| 615 |
static int vtablogRollback(sqlite3_vtab *tab){ |
| 616 |
vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 617 |
printf("%s.%s.xRollback()\n", pTab->zDb, pTab->zName); |
| 618 |
return SQLITE_OK; |
| 619 |
} |
| 620 |
static int vtablogSavepoint(sqlite3_vtab *tab, int N){ |
| 621 |
vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 622 |
printf("%s.%s.xSavepoint(%d)\n", pTab->zDb, pTab->zName, N); |
| 623 |
return SQLITE_OK; |
| 624 |
} |
| 625 |
static int vtablogRelease(sqlite3_vtab *tab, int N){ |
| 626 |
vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 627 |
printf("%s.%s.xRelease(%d)\n", pTab->zDb, pTab->zName, N); |
| 628 |
return SQLITE_OK; |
| 629 |
} |
| 630 |
static int vtablogRollbackTo(sqlite3_vtab *tab, int N){ |
| 631 |
vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 632 |
printf("%s.%s.xRollbackTo(%d)\n", pTab->zDb, pTab->zName, N); |
| 633 |
return SQLITE_OK; |
| 634 |
} |
| 635 |
|
| 636 |
static int vtablogFindMethod( |
| 637 |
sqlite3_vtab *tab, |
| 638 |
int nArg, |
| 639 |
const char *zName, |
| 640 |
void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), |
| 641 |
void **ppArg |
| 642 |
){ |
| 643 |
vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 644 |
printf("%s.%s.xFindMethod(nArg=%d, zName=%s)\n", |
| 645 |
pTab->zDb, pTab->zName, nArg, zName); |
| 646 |
return SQLITE_OK; |
| 647 |
} |
| 648 |
static int vtablogRename(sqlite3_vtab *tab, const char *zNew){ |
| 649 |
vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 650 |
printf("%s.%s.xRename('%s')\n", pTab->zDb, pTab->zName, zNew); |
| 651 |
sqlite3_free(pTab->zName); |
| 652 |
pTab->zName = sqlite3_mprintf("%s", zNew); |
| 653 |
return SQLITE_OK; |
| 654 |
} |
| 655 |
|
| 656 |
/* Any table name that contains the text "shadow" is seen as a |
| 657 |
** shadow table. Nothing else is. |
| 658 |
*/ |
| 659 |
static int vtablogShadowName(const char *zName){ |
| 660 |
printf("vtablog.xShadowName('%s')\n", zName); |
| 661 |
return sqlite3_strglob("*shadow*", zName)==0; |
| 662 |
} |
| 663 |
|
| 664 |
static int vtablogIntegrity( |
| 665 |
sqlite3_vtab *tab, |
| 666 |
const char *zSchema, |
| 667 |
const char *zTabName, |
| 668 |
int mFlags, |
| 669 |
char **pzErr |
| 670 |
){ |
| 671 |
vtablog_vtab *pTab = (vtablog_vtab*)tab; |
| 672 |
printf("%s.%s.xIntegrity(mFlags=0x%x)\n", pTab->zDb, pTab->zName, mFlags); |
| 673 |
return 0; |
| 674 |
} |
| 675 |
|
| 676 |
/* |
| 677 |
** This following structure defines all the methods for the |
| 678 |
** vtablog virtual table. |
| 679 |
*/ |
| 680 |
static sqlite3_module vtablogModule = { |
| 681 |
4, /* iVersion */ |
| 682 |
vtablogCreate, /* xCreate */ |
| 683 |
vtablogConnect, /* xConnect */ |
| 684 |
vtablogBestIndex, /* xBestIndex */ |
| 685 |
vtablogDisconnect, /* xDisconnect */ |
| 686 |
vtablogDestroy, /* xDestroy */ |
| 687 |
vtablogOpen, /* xOpen - open a cursor */ |
| 688 |
vtablogClose, /* xClose - close a cursor */ |
| 689 |
vtablogFilter, /* xFilter - configure scan constraints */ |
| 690 |
vtablogNext, /* xNext - advance a cursor */ |
| 691 |
vtablogEof, /* xEof - check for end of scan */ |
| 692 |
vtablogColumn, /* xColumn - read data */ |
| 693 |
vtablogRowid, /* xRowid - read data */ |
| 694 |
vtablogUpdate, /* xUpdate */ |
| 695 |
vtablogBegin, /* xBegin */ |
| 696 |
vtablogSync, /* xSync */ |
| 697 |
vtablogCommit, /* xCommit */ |
| 698 |
vtablogRollback, /* xRollback */ |
| 699 |
vtablogFindMethod, /* xFindMethod */ |
| 700 |
vtablogRename, /* xRename */ |
| 701 |
vtablogSavepoint, /* xSavepoint */ |
| 702 |
vtablogRelease, /* xRelease */ |
| 703 |
vtablogRollbackTo, /* xRollbackTo */ |
| 704 |
vtablogShadowName, /* xShadowName */ |
| 705 |
vtablogIntegrity /* xIntegrity */ |
| 706 |
}; |
| 707 |
|
| 708 |
#ifdef _WIN32 |
| 709 |
__declspec(dllexport) |
| 710 |
#endif |
| 711 |
int sqlite3_vtablog_init( |
| 712 |
sqlite3 *db, |
| 713 |
char **pzErrMsg, |
| 714 |
const sqlite3_api_routines *pApi |
| 715 |
){ |
| 716 |
int rc; |
| 717 |
SQLITE_EXTENSION_INIT2(pApi); |
| 718 |
rc = sqlite3_create_module(db, "vtablog", &vtablogModule, 0); |
| 719 |
return rc; |
| 720 |
} |