OLD | NEW |
1 /* | 1 /* |
2 ** 2015-04-17 | 2 ** 2015-04-17 |
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 ** |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 return; | 188 return; |
189 } | 189 } |
190 | 190 |
191 /* | 191 /* |
192 ** This callback is invoked by sqlite3_exec() to return query results. | 192 ** This callback is invoked by sqlite3_exec() to return query results. |
193 */ | 193 */ |
194 static int execCallback(void *NotUsed, int argc, char **argv, char **colv){ | 194 static int execCallback(void *NotUsed, int argc, char **argv, char **colv){ |
195 int i; | 195 int i; |
196 static unsigned cnt = 0; | 196 static unsigned cnt = 0; |
197 printf("ROW #%u:\n", ++cnt); | 197 printf("ROW #%u:\n", ++cnt); |
198 for(i=0; i<argc; i++){ | 198 if( argv ){ |
199 printf(" %s=", colv[i]); | 199 for(i=0; i<argc; i++){ |
200 if( argv[i] ){ | 200 printf(" %s=", colv[i]); |
201 printf("[%s]\n", argv[i]); | 201 if( argv[i] ){ |
202 }else{ | 202 printf("[%s]\n", argv[i]); |
203 printf("NULL\n"); | 203 }else{ |
| 204 printf("NULL\n"); |
| 205 } |
204 } | 206 } |
205 } | 207 } |
206 fflush(stdout); | 208 fflush(stdout); |
207 return 0; | 209 return 0; |
208 } | 210 } |
209 static int execNoop(void *NotUsed, int argc, char **argv, char **colv){ | 211 static int execNoop(void *NotUsed, int argc, char **argv, char **colv){ |
210 return 0; | 212 return 0; |
211 } | 213 } |
212 | 214 |
213 #ifndef SQLITE_OMIT_TRACE | 215 #ifndef SQLITE_OMIT_TRACE |
214 /* | 216 /* |
215 ** This callback is invoked by sqlite3_trace() as each SQL statement | 217 ** This callback is invoked by sqlite3_trace() as each SQL statement |
216 ** starts. | 218 ** starts. |
217 */ | 219 */ |
218 static void traceCallback(void *NotUsed, const char *zMsg){ | 220 static void traceCallback(void *NotUsed, const char *zMsg){ |
219 printf("TRACE: %s\n", zMsg); | 221 printf("TRACE: %s\n", zMsg); |
220 fflush(stdout); | 222 fflush(stdout); |
221 } | 223 } |
222 static void traceNoop(void *NotUsed, const char *zMsg){ | 224 static void traceNoop(void *NotUsed, const char *zMsg){ |
223 return; | 225 return; |
224 } | 226 } |
225 #endif | 227 #endif |
226 | 228 |
227 /*************************************************************************** | 229 /*************************************************************************** |
| 230 ** String accumulator object |
| 231 */ |
| 232 typedef struct Str Str; |
| 233 struct Str { |
| 234 char *z; /* The string. Memory from malloc() */ |
| 235 sqlite3_uint64 n; /* Bytes of input used */ |
| 236 sqlite3_uint64 nAlloc; /* Bytes allocated to z[] */ |
| 237 int oomErr; /* OOM error has been seen */ |
| 238 }; |
| 239 |
| 240 /* Initialize a Str object */ |
| 241 static void StrInit(Str *p){ |
| 242 memset(p, 0, sizeof(*p)); |
| 243 } |
| 244 |
| 245 /* Append text to the end of a Str object */ |
| 246 static void StrAppend(Str *p, const char *z){ |
| 247 sqlite3_uint64 n = strlen(z); |
| 248 if( p->n + n >= p->nAlloc ){ |
| 249 char *zNew; |
| 250 sqlite3_uint64 nNew; |
| 251 if( p->oomErr ) return; |
| 252 nNew = p->nAlloc*2 + 100 + n; |
| 253 zNew = sqlite3_realloc(p->z, nNew); |
| 254 if( zNew==0 ){ |
| 255 sqlite3_free(p->z); |
| 256 memset(p, 0, sizeof(*p)); |
| 257 p->oomErr = 1; |
| 258 return; |
| 259 } |
| 260 p->z = zNew; |
| 261 p->nAlloc = nNew; |
| 262 } |
| 263 memcpy(p->z + p->n, z, n); |
| 264 p->n += n; |
| 265 p->z[p->n] = 0; |
| 266 } |
| 267 |
| 268 /* Return the current string content */ |
| 269 static char *StrStr(Str *p){ |
| 270 return p->z; |
| 271 } |
| 272 |
| 273 /* Free the string */ |
| 274 static void StrFree(Str *p){ |
| 275 sqlite3_free(p->z); |
| 276 StrInit(p); |
| 277 } |
| 278 |
| 279 /*************************************************************************** |
228 ** eval() implementation copied from ../ext/misc/eval.c | 280 ** eval() implementation copied from ../ext/misc/eval.c |
229 */ | 281 */ |
230 /* | 282 /* |
231 ** Structure used to accumulate the output | 283 ** Structure used to accumulate the output |
232 */ | 284 */ |
233 struct EvalResult { | 285 struct EvalResult { |
234 char *z; /* Accumulated output */ | 286 char *z; /* Accumulated output */ |
235 const char *zSep; /* Separator */ | 287 const char *zSep; /* Separator */ |
236 int szSep; /* Size of the separator string */ | 288 int szSep; /* Size of the separator string */ |
237 sqlite3_int64 nAlloc; /* Number of bytes allocated for z[] */ | 289 sqlite3_int64 nAlloc; /* Number of bytes allocated for z[] */ |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 }else if( x.zSep==0 ){ | 357 }else if( x.zSep==0 ){ |
306 sqlite3_result_error_nomem(context); | 358 sqlite3_result_error_nomem(context); |
307 sqlite3_free(x.z); | 359 sqlite3_free(x.z); |
308 }else{ | 360 }else{ |
309 sqlite3_result_text(context, x.z, (int)x.nUsed, sqlite3_free); | 361 sqlite3_result_text(context, x.z, (int)x.nUsed, sqlite3_free); |
310 } | 362 } |
311 } | 363 } |
312 /* End of the eval() implementation | 364 /* End of the eval() implementation |
313 ******************************************************************************/ | 365 ******************************************************************************/ |
314 | 366 |
| 367 /****************************************************************************** |
| 368 ** The generate_series(START,END,STEP) eponymous table-valued function. |
| 369 ** |
| 370 ** This code is copy/pasted from ext/misc/series.c in the SQLite source tree. |
| 371 */ |
| 372 /* series_cursor is a subclass of sqlite3_vtab_cursor which will |
| 373 ** serve as the underlying representation of a cursor that scans |
| 374 ** over rows of the result |
| 375 */ |
| 376 typedef struct series_cursor series_cursor; |
| 377 struct series_cursor { |
| 378 sqlite3_vtab_cursor base; /* Base class - must be first */ |
| 379 int isDesc; /* True to count down rather than up */ |
| 380 sqlite3_int64 iRowid; /* The rowid */ |
| 381 sqlite3_int64 iValue; /* Current value ("value") */ |
| 382 sqlite3_int64 mnValue; /* Mimimum value ("start") */ |
| 383 sqlite3_int64 mxValue; /* Maximum value ("stop") */ |
| 384 sqlite3_int64 iStep; /* Increment ("step") */ |
| 385 }; |
| 386 |
| 387 /* |
| 388 ** The seriesConnect() method is invoked to create a new |
| 389 ** series_vtab that describes the generate_series virtual table. |
| 390 ** |
| 391 ** Think of this routine as the constructor for series_vtab objects. |
| 392 ** |
| 393 ** All this routine needs to do is: |
| 394 ** |
| 395 ** (1) Allocate the series_vtab object and initialize all fields. |
| 396 ** |
| 397 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the |
| 398 ** result set of queries against generate_series will look like. |
| 399 */ |
| 400 static int seriesConnect( |
| 401 sqlite3 *db, |
| 402 void *pAux, |
| 403 int argc, const char *const*argv, |
| 404 sqlite3_vtab **ppVtab, |
| 405 char **pzErr |
| 406 ){ |
| 407 sqlite3_vtab *pNew; |
| 408 int rc; |
| 409 |
| 410 /* Column numbers */ |
| 411 #define SERIES_COLUMN_VALUE 0 |
| 412 #define SERIES_COLUMN_START 1 |
| 413 #define SERIES_COLUMN_STOP 2 |
| 414 #define SERIES_COLUMN_STEP 3 |
| 415 |
| 416 rc = sqlite3_declare_vtab(db, |
| 417 "CREATE TABLE x(value,start hidden,stop hidden,step hidden)"); |
| 418 if( rc==SQLITE_OK ){ |
| 419 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) ); |
| 420 if( pNew==0 ) return SQLITE_NOMEM; |
| 421 memset(pNew, 0, sizeof(*pNew)); |
| 422 } |
| 423 return rc; |
| 424 } |
| 425 |
| 426 /* |
| 427 ** This method is the destructor for series_cursor objects. |
| 428 */ |
| 429 static int seriesDisconnect(sqlite3_vtab *pVtab){ |
| 430 sqlite3_free(pVtab); |
| 431 return SQLITE_OK; |
| 432 } |
| 433 |
| 434 /* |
| 435 ** Constructor for a new series_cursor object. |
| 436 */ |
| 437 static int seriesOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
| 438 series_cursor *pCur; |
| 439 pCur = sqlite3_malloc( sizeof(*pCur) ); |
| 440 if( pCur==0 ) return SQLITE_NOMEM; |
| 441 memset(pCur, 0, sizeof(*pCur)); |
| 442 *ppCursor = &pCur->base; |
| 443 return SQLITE_OK; |
| 444 } |
| 445 |
| 446 /* |
| 447 ** Destructor for a series_cursor. |
| 448 */ |
| 449 static int seriesClose(sqlite3_vtab_cursor *cur){ |
| 450 sqlite3_free(cur); |
| 451 return SQLITE_OK; |
| 452 } |
| 453 |
| 454 |
| 455 /* |
| 456 ** Advance a series_cursor to its next row of output. |
| 457 */ |
| 458 static int seriesNext(sqlite3_vtab_cursor *cur){ |
| 459 series_cursor *pCur = (series_cursor*)cur; |
| 460 if( pCur->isDesc ){ |
| 461 pCur->iValue -= pCur->iStep; |
| 462 }else{ |
| 463 pCur->iValue += pCur->iStep; |
| 464 } |
| 465 pCur->iRowid++; |
| 466 return SQLITE_OK; |
| 467 } |
| 468 |
| 469 /* |
| 470 ** Return values of columns for the row at which the series_cursor |
| 471 ** is currently pointing. |
| 472 */ |
| 473 static int seriesColumn( |
| 474 sqlite3_vtab_cursor *cur, /* The cursor */ |
| 475 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ |
| 476 int i /* Which column to return */ |
| 477 ){ |
| 478 series_cursor *pCur = (series_cursor*)cur; |
| 479 sqlite3_int64 x = 0; |
| 480 switch( i ){ |
| 481 case SERIES_COLUMN_START: x = pCur->mnValue; break; |
| 482 case SERIES_COLUMN_STOP: x = pCur->mxValue; break; |
| 483 case SERIES_COLUMN_STEP: x = pCur->iStep; break; |
| 484 default: x = pCur->iValue; break; |
| 485 } |
| 486 sqlite3_result_int64(ctx, x); |
| 487 return SQLITE_OK; |
| 488 } |
| 489 |
| 490 /* |
| 491 ** Return the rowid for the current row. In this implementation, the |
| 492 ** rowid is the same as the output value. |
| 493 */ |
| 494 static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
| 495 series_cursor *pCur = (series_cursor*)cur; |
| 496 *pRowid = pCur->iRowid; |
| 497 return SQLITE_OK; |
| 498 } |
| 499 |
| 500 /* |
| 501 ** Return TRUE if the cursor has been moved off of the last |
| 502 ** row of output. |
| 503 */ |
| 504 static int seriesEof(sqlite3_vtab_cursor *cur){ |
| 505 series_cursor *pCur = (series_cursor*)cur; |
| 506 if( pCur->isDesc ){ |
| 507 return pCur->iValue < pCur->mnValue; |
| 508 }else{ |
| 509 return pCur->iValue > pCur->mxValue; |
| 510 } |
| 511 } |
| 512 |
| 513 /* True to cause run-time checking of the start=, stop=, and/or step= |
| 514 ** parameters. The only reason to do this is for testing the |
| 515 ** constraint checking logic for virtual tables in the SQLite core. |
| 516 */ |
| 517 #ifndef SQLITE_SERIES_CONSTRAINT_VERIFY |
| 518 # define SQLITE_SERIES_CONSTRAINT_VERIFY 0 |
| 519 #endif |
| 520 |
| 521 /* |
| 522 ** This method is called to "rewind" the series_cursor object back |
| 523 ** to the first row of output. This method is always called at least |
| 524 ** once prior to any call to seriesColumn() or seriesRowid() or |
| 525 ** seriesEof(). |
| 526 ** |
| 527 ** The query plan selected by seriesBestIndex is passed in the idxNum |
| 528 ** parameter. (idxStr is not used in this implementation.) idxNum |
| 529 ** is a bitmask showing which constraints are available: |
| 530 ** |
| 531 ** 1: start=VALUE |
| 532 ** 2: stop=VALUE |
| 533 ** 4: step=VALUE |
| 534 ** |
| 535 ** Also, if bit 8 is set, that means that the series should be output |
| 536 ** in descending order rather than in ascending order. |
| 537 ** |
| 538 ** This routine should initialize the cursor and position it so that it |
| 539 ** is pointing at the first row, or pointing off the end of the table |
| 540 ** (so that seriesEof() will return true) if the table is empty. |
| 541 */ |
| 542 static int seriesFilter( |
| 543 sqlite3_vtab_cursor *pVtabCursor, |
| 544 int idxNum, const char *idxStr, |
| 545 int argc, sqlite3_value **argv |
| 546 ){ |
| 547 series_cursor *pCur = (series_cursor *)pVtabCursor; |
| 548 int i = 0; |
| 549 if( idxNum & 1 ){ |
| 550 pCur->mnValue = sqlite3_value_int64(argv[i++]); |
| 551 }else{ |
| 552 pCur->mnValue = 0; |
| 553 } |
| 554 if( idxNum & 2 ){ |
| 555 pCur->mxValue = sqlite3_value_int64(argv[i++]); |
| 556 }else{ |
| 557 pCur->mxValue = 0xffffffff; |
| 558 } |
| 559 if( idxNum & 4 ){ |
| 560 pCur->iStep = sqlite3_value_int64(argv[i++]); |
| 561 if( pCur->iStep<1 ) pCur->iStep = 1; |
| 562 }else{ |
| 563 pCur->iStep = 1; |
| 564 } |
| 565 if( idxNum & 8 ){ |
| 566 pCur->isDesc = 1; |
| 567 pCur->iValue = pCur->mxValue; |
| 568 if( pCur->iStep>0 ){ |
| 569 pCur->iValue -= (pCur->mxValue - pCur->mnValue)%pCur->iStep; |
| 570 } |
| 571 }else{ |
| 572 pCur->isDesc = 0; |
| 573 pCur->iValue = pCur->mnValue; |
| 574 } |
| 575 pCur->iRowid = 1; |
| 576 return SQLITE_OK; |
| 577 } |
| 578 |
| 579 /* |
| 580 ** SQLite will invoke this method one or more times while planning a query |
| 581 ** that uses the generate_series virtual table. This routine needs to create |
| 582 ** a query plan for each invocation and compute an estimated cost for that |
| 583 ** plan. |
| 584 ** |
| 585 ** In this implementation idxNum is used to represent the |
| 586 ** query plan. idxStr is unused. |
| 587 ** |
| 588 ** The query plan is represented by bits in idxNum: |
| 589 ** |
| 590 ** (1) start = $value -- constraint exists |
| 591 ** (2) stop = $value -- constraint exists |
| 592 ** (4) step = $value -- constraint exists |
| 593 ** (8) output in descending order |
| 594 */ |
| 595 static int seriesBestIndex( |
| 596 sqlite3_vtab *tab, |
| 597 sqlite3_index_info *pIdxInfo |
| 598 ){ |
| 599 int i; /* Loop over constraints */ |
| 600 int idxNum = 0; /* The query plan bitmask */ |
| 601 int startIdx = -1; /* Index of the start= constraint, or -1 if none */ |
| 602 int stopIdx = -1; /* Index of the stop= constraint, or -1 if none */ |
| 603 int stepIdx = -1; /* Index of the step= constraint, or -1 if none */ |
| 604 int nArg = 0; /* Number of arguments that seriesFilter() expects */ |
| 605 |
| 606 const struct sqlite3_index_constraint *pConstraint; |
| 607 pConstraint = pIdxInfo->aConstraint; |
| 608 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ |
| 609 if( pConstraint->usable==0 ) continue; |
| 610 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; |
| 611 switch( pConstraint->iColumn ){ |
| 612 case SERIES_COLUMN_START: |
| 613 startIdx = i; |
| 614 idxNum |= 1; |
| 615 break; |
| 616 case SERIES_COLUMN_STOP: |
| 617 stopIdx = i; |
| 618 idxNum |= 2; |
| 619 break; |
| 620 case SERIES_COLUMN_STEP: |
| 621 stepIdx = i; |
| 622 idxNum |= 4; |
| 623 break; |
| 624 } |
| 625 } |
| 626 if( startIdx>=0 ){ |
| 627 pIdxInfo->aConstraintUsage[startIdx].argvIndex = ++nArg; |
| 628 pIdxInfo->aConstraintUsage[startIdx].omit= !SQLITE_SERIES_CONSTRAINT_VERIFY; |
| 629 } |
| 630 if( stopIdx>=0 ){ |
| 631 pIdxInfo->aConstraintUsage[stopIdx].argvIndex = ++nArg; |
| 632 pIdxInfo->aConstraintUsage[stopIdx].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY; |
| 633 } |
| 634 if( stepIdx>=0 ){ |
| 635 pIdxInfo->aConstraintUsage[stepIdx].argvIndex = ++nArg; |
| 636 pIdxInfo->aConstraintUsage[stepIdx].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY; |
| 637 } |
| 638 if( (idxNum & 3)==3 ){ |
| 639 /* Both start= and stop= boundaries are available. This is the |
| 640 ** the preferred case */ |
| 641 pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0)); |
| 642 pIdxInfo->estimatedRows = 1000; |
| 643 if( pIdxInfo->nOrderBy==1 ){ |
| 644 if( pIdxInfo->aOrderBy[0].desc ) idxNum |= 8; |
| 645 pIdxInfo->orderByConsumed = 1; |
| 646 } |
| 647 }else{ |
| 648 /* If either boundary is missing, we have to generate a huge span |
| 649 ** of numbers. Make this case very expensive so that the query |
| 650 ** planner will work hard to avoid it. */ |
| 651 pIdxInfo->estimatedCost = (double)2147483647; |
| 652 pIdxInfo->estimatedRows = 2147483647; |
| 653 } |
| 654 pIdxInfo->idxNum = idxNum; |
| 655 return SQLITE_OK; |
| 656 } |
| 657 |
| 658 /* |
| 659 ** This following structure defines all the methods for the |
| 660 ** generate_series virtual table. |
| 661 */ |
| 662 static sqlite3_module seriesModule = { |
| 663 0, /* iVersion */ |
| 664 0, /* xCreate */ |
| 665 seriesConnect, /* xConnect */ |
| 666 seriesBestIndex, /* xBestIndex */ |
| 667 seriesDisconnect, /* xDisconnect */ |
| 668 0, /* xDestroy */ |
| 669 seriesOpen, /* xOpen - open a cursor */ |
| 670 seriesClose, /* xClose - close a cursor */ |
| 671 seriesFilter, /* xFilter - configure scan constraints */ |
| 672 seriesNext, /* xNext - advance a cursor */ |
| 673 seriesEof, /* xEof - check for end of scan */ |
| 674 seriesColumn, /* xColumn - read data */ |
| 675 seriesRowid, /* xRowid - read data */ |
| 676 0, /* xUpdate */ |
| 677 0, /* xBegin */ |
| 678 0, /* xSync */ |
| 679 0, /* xCommit */ |
| 680 0, /* xRollback */ |
| 681 0, /* xFindMethod */ |
| 682 0, /* xRename */ |
| 683 }; |
| 684 /* END the generate_series(START,END,STEP) implementation |
| 685 ********************************************************************************
*/ |
| 686 |
315 /* | 687 /* |
316 ** Print sketchy documentation for this utility program | 688 ** Print sketchy documentation for this utility program |
317 */ | 689 */ |
318 static void showHelp(void){ | 690 static void showHelp(void){ |
319 printf("Usage: %s [options] ?FILE...?\n", g.zArgv0); | 691 printf("Usage: %s [options] ?FILE...?\n", g.zArgv0); |
320 printf( | 692 printf( |
321 "Read SQL text from FILE... (or from standard input if FILE... is omitted)\n" | 693 "Read SQL text from FILE... (or from standard input if FILE... is omitted)\n" |
322 "and then evaluate each block of SQL contained therein.\n" | 694 "and then evaluate each block of SQL contained therein.\n" |
323 "Options:\n" | 695 "Options:\n" |
324 " --autovacuum Enable AUTOVACUUM mode\n" | 696 " --autovacuum Enable AUTOVACUUM mode\n" |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
699 g.nOomFault = 0; | 1071 g.nOomFault = 0; |
700 g.bOomOnce = 1; | 1072 g.bOomOnce = 1; |
701 if( verboseFlag ){ | 1073 if( verboseFlag ){ |
702 printf("Once.%d\n", oomCnt); | 1074 printf("Once.%d\n", oomCnt); |
703 fflush(stdout); | 1075 fflush(stdout); |
704 } | 1076 } |
705 }else{ | 1077 }else{ |
706 oomCnt = 0; | 1078 oomCnt = 0; |
707 } | 1079 } |
708 do{ | 1080 do{ |
| 1081 Str sql; |
| 1082 StrInit(&sql); |
709 if( zDbName ){ | 1083 if( zDbName ){ |
710 rc = sqlite3_open_v2(zDbName, &db, SQLITE_OPEN_READWRITE, 0); | 1084 rc = sqlite3_open_v2(zDbName, &db, SQLITE_OPEN_READWRITE, 0); |
711 if( rc!=SQLITE_OK ){ | 1085 if( rc!=SQLITE_OK ){ |
712 abendError("Cannot open database file %s", zDbName); | 1086 abendError("Cannot open database file %s", zDbName); |
713 } | 1087 } |
714 }else{ | 1088 }else{ |
715 rc = sqlite3_open_v2( | 1089 rc = sqlite3_open_v2( |
716 "main.db", &db, | 1090 "main.db", &db, |
717 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY, | 1091 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY, |
718 0); | 1092 0); |
719 if( rc!=SQLITE_OK ){ | 1093 if( rc!=SQLITE_OK ){ |
720 abendError("Unable to open the in-memory database"); | 1094 abendError("Unable to open the in-memory database"); |
721 } | 1095 } |
722 } | 1096 } |
723 if( pLook ){ | 1097 if( pLook ){ |
724 rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE,pLook,szLook,nLoo
k); | 1098 rc = sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE,pLook,szLook,nLoo
k); |
725 if( rc!=SQLITE_OK ) abendError("lookaside configuration filed: %d", rc
); | 1099 if( rc!=SQLITE_OK ) abendError("lookaside configuration filed: %d", rc
); |
726 } | 1100 } |
727 #ifndef SQLITE_OMIT_TRACE | 1101 #ifndef SQLITE_OMIT_TRACE |
728 sqlite3_trace(db, verboseFlag ? traceCallback : traceNoop, 0); | 1102 sqlite3_trace(db, verboseFlag ? traceCallback : traceNoop, 0); |
729 #endif | 1103 #endif |
730 sqlite3_create_function(db, "eval", 1, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0
); | 1104 sqlite3_create_function(db, "eval", 1, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0
); |
731 sqlite3_create_function(db, "eval", 2, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0
); | 1105 sqlite3_create_function(db, "eval", 2, SQLITE_UTF8, 0, sqlEvalFunc, 0, 0
); |
| 1106 sqlite3_create_module(db, "generate_series", &seriesModule, 0); |
732 sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 1000000); | 1107 sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 1000000); |
733 if( zEncoding ) sqlexec(db, "PRAGMA encoding=%s", zEncoding); | 1108 if( zEncoding ) sqlexec(db, "PRAGMA encoding=%s", zEncoding); |
734 if( pageSize ) sqlexec(db, "PRAGMA pagesize=%d", pageSize); | 1109 if( pageSize ) sqlexec(db, "PRAGMA pagesize=%d", pageSize); |
735 if( doAutovac ) sqlexec(db, "PRAGMA auto_vacuum=FULL"); | 1110 if( doAutovac ) sqlexec(db, "PRAGMA auto_vacuum=FULL"); |
736 iStart = timeOfDay(); | 1111 iStart = timeOfDay(); |
| 1112 |
| 1113 /* If using an input database file and that database contains a table |
| 1114 ** named "autoexec" with a column "sql", then replace the input SQL |
| 1115 ** with the concatenated text of the autoexec table. In this way, |
| 1116 ** if the database file is the input being fuzzed, the SQL text is |
| 1117 ** fuzzed at the same time. */ |
| 1118 if( sqlite3_table_column_metadata(db,0,"autoexec","sql",0,0,0,0,0)==0 ){ |
| 1119 sqlite3_stmt *pStmt; |
| 1120 rc = sqlite3_prepare_v2(db, "SELECT sql FROM autoexec", -1, &pStmt, 0)
; |
| 1121 if( rc==SQLITE_OK ){ |
| 1122 while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 1123 StrAppend(&sql, (const char*)sqlite3_column_text(pStmt, 0)); |
| 1124 StrAppend(&sql, "\n"); |
| 1125 } |
| 1126 } |
| 1127 sqlite3_finalize(pStmt); |
| 1128 zSql = StrStr(&sql); |
| 1129 } |
| 1130 |
737 g.bOomEnable = 1; | 1131 g.bOomEnable = 1; |
738 if( verboseFlag ){ | 1132 if( verboseFlag ){ |
739 zErrMsg = 0; | 1133 zErrMsg = 0; |
740 rc = sqlite3_exec(db, zSql, execCallback, 0, &zErrMsg); | 1134 rc = sqlite3_exec(db, zSql, execCallback, 0, &zErrMsg); |
741 if( zErrMsg ){ | 1135 if( zErrMsg ){ |
742 sqlite3_snprintf(sizeof(zErrBuf),zErrBuf,"%z", zErrMsg); | 1136 sqlite3_snprintf(sizeof(zErrBuf),zErrBuf,"%z", zErrMsg); |
743 zErrMsg = 0; | 1137 zErrMsg = 0; |
744 } | 1138 } |
745 }else { | 1139 }else { |
746 rc = sqlite3_exec(db, zSql, execNoop, 0, 0); | 1140 rc = sqlite3_exec(db, zSql, execNoop, 0, 0); |
747 } | 1141 } |
748 g.bOomEnable = 0; | 1142 g.bOomEnable = 0; |
749 iEnd = timeOfDay(); | 1143 iEnd = timeOfDay(); |
| 1144 StrFree(&sql); |
750 rc = sqlite3_close(db); | 1145 rc = sqlite3_close(db); |
751 if( rc ){ | 1146 if( rc ){ |
752 abendError("sqlite3_close() failed with rc=%d", rc); | 1147 abendError("sqlite3_close() failed with rc=%d", rc); |
753 } | 1148 } |
754 if( !zDataOut && sqlite3_memory_used()>0 ){ | 1149 if( !zDataOut && sqlite3_memory_used()>0 ){ |
755 abendError("memory in use after close: %lld bytes",sqlite3_memory_used
()); | 1150 abendError("memory in use after close: %lld bytes",sqlite3_memory_used
()); |
756 } | 1151 } |
757 if( oomFlag ){ | 1152 if( oomFlag ){ |
758 /* Limit the number of iterations of the OOM loop to OOM_MAX. If the | 1153 /* Limit the number of iterations of the OOM loop to OOM_MAX. If the |
759 ** first pass (single failure) exceeds 2/3rds of OOM_MAX this skip the | 1154 ** first pass (single failure) exceeds 2/3rds of OOM_MAX this skip the |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
858 /* Clean up and exit. | 1253 /* Clean up and exit. |
859 */ | 1254 */ |
860 free(azInFile); | 1255 free(azInFile); |
861 free(zIn); | 1256 free(zIn); |
862 free(pHeap); | 1257 free(pHeap); |
863 free(pLook); | 1258 free(pLook); |
864 free(pScratch); | 1259 free(pScratch); |
865 free(pPCache); | 1260 free(pPCache); |
866 return 0; | 1261 return 0; |
867 } | 1262 } |
OLD | NEW |