~ubuntu-branches/ubuntu/maverick/sqlite3/maverick-updates

« back to all changes in this revision

Viewing changes to src/test8.c

  • Committer: Bazaar Package Importer
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2008-10-01 20:16:18 UTC
  • mfrom: (3.1.20 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081001201618-yfvqqj1qs29wdtcc
Tags: 3.5.9-5
Backport fix for distinct on indexes (closes: #500792).

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
** is not included in the SQLite library.  It is used for automated
14
14
** testing of the SQLite library.
15
15
**
16
 
** $Id: test8.c,v 1.43 2006/10/08 18:56:57 drh Exp $
 
16
** $Id: test8.c,v 1.64 2008/05/13 13:27:34 drh Exp $
17
17
*/
18
18
#include "sqliteInt.h"
19
19
#include "tcl.h"
20
 
#include "os.h"
21
20
#include <stdlib.h>
22
21
#include <string.h>
23
22
 
27
26
typedef struct echo_cursor echo_cursor;
28
27
 
29
28
/*
30
 
** The test module defined in this file uses two global Tcl variables to
 
29
** The test module defined in this file uses four global Tcl variables to
31
30
** commicate with test-scripts:
32
31
**
33
32
**     $::echo_module
34
33
**     $::echo_module_sync_fail
35
34
**     $::echo_module_begin_fail
 
35
**     $::echo_module_cost
36
36
**
37
37
** The variable ::echo_module is a list. Each time one of the following
38
38
** methods is called, one or more elements are appended to the list.
60
60
  Tcl_Interp *interp;     /* Tcl interpreter containing debug variables */
61
61
  sqlite3 *db;            /* Database connection */
62
62
 
 
63
  int isPattern;
 
64
  int inTransaction;      /* True if within a transaction */
 
65
  char *zThis;            /* Name of the echo table */
63
66
  char *zTableName;       /* Name of the real table */
64
67
  char *zLogName;         /* Name of the log table */
65
68
  int nCol;               /* Number of columns in the real table */
74
77
};
75
78
 
76
79
/*
 
80
** Convert an SQL-style quoted string into a normal string by removing
 
81
** the quote characters.  The conversion is done in-place.  If the
 
82
** input does not begin with a quote character, then this routine
 
83
** is a no-op.
 
84
**
 
85
** Examples:
 
86
**
 
87
**     "abc"   becomes   abc
 
88
**     'xyz'   becomes   xyz
 
89
**     [pqr]   becomes   pqr
 
90
**     `mno`   becomes   mno
 
91
*/
 
92
static void dequoteString(char *z){
 
93
  int quote;
 
94
  int i, j;
 
95
  if( z==0 ) return;
 
96
  quote = z[0];
 
97
  switch( quote ){
 
98
    case '\'':  break;
 
99
    case '"':   break;
 
100
    case '`':   break;                /* For MySQL compatibility */
 
101
    case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
 
102
    default:    return;
 
103
  }
 
104
  for(i=1, j=0; z[i]; i++){
 
105
    if( z[i]==quote ){
 
106
      if( z[i+1]==quote ){
 
107
        z[j++] = quote;
 
108
        i++;
 
109
      }else{
 
110
        z[j++] = 0;
 
111
        break;
 
112
      }
 
113
    }else{
 
114
      z[j++] = z[i];
 
115
    }
 
116
  }
 
117
}
 
118
 
 
119
/*
77
120
** Retrieve the column names for the table named zTab via database
78
121
** connection db. SQLITE_OK is returned on success, or an sqlite error
79
122
** code otherwise.
80
123
**
81
124
** If successful, the number of columns is written to *pnCol. *paCol is
82
 
** set to point at sqliteMalloc()'d space containing the array of
83
 
** nCol column names. The caller is responsible for calling sqliteFree
 
125
** set to point at sqlite3_malloc()'d space containing the array of
 
126
** nCol column names. The caller is responsible for calling sqlite3_free
84
127
** on *paCol.
85
128
*/
86
129
static int getColumnNames(
99
142
  ** of the result set of the compiled SELECT will be the same as
100
143
  ** the column names of table <tbl>.
101
144
  */
102
 
  zSql = sqlite3MPrintf("SELECT * FROM %Q", zTab);
 
145
  zSql = sqlite3_mprintf("SELECT * FROM %Q", zTab);
103
146
  if( !zSql ){
104
147
    rc = SQLITE_NOMEM;
105
148
    goto out;
106
149
  }
107
150
  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
108
 
  sqliteFree(zSql);
 
151
  sqlite3_free(zSql);
109
152
 
110
153
  if( rc==SQLITE_OK ){
111
154
    int ii;
118
161
    */
119
162
    nBytes = sizeof(char *) * nCol;
120
163
    for(ii=0; ii<nCol; ii++){
121
 
      nBytes += (strlen(sqlite3_column_name(pStmt, ii)) + 1);
 
164
      const char *zName = sqlite3_column_name(pStmt, ii);
 
165
      if( !zName ){
 
166
        rc = SQLITE_NOMEM;
 
167
        goto out;
 
168
      }
 
169
      nBytes += strlen(zName)+1;
122
170
    }
123
 
    aCol = (char **)sqliteMalloc(nBytes);
 
171
    aCol = (char **)sqlite3MallocZero(nBytes);
124
172
    if( !aCol ){
125
173
      rc = SQLITE_NOMEM;
126
174
      goto out;
170
218
  char *zSql;
171
219
 
172
220
  /* Allocate space for the index array */
173
 
  aIndex = (int *)sqliteMalloc(sizeof(int) * nCol);
 
221
  aIndex = (int *)sqlite3MallocZero(sizeof(int) * nCol);
174
222
  if( !aIndex ){
175
223
    rc = SQLITE_NOMEM;
176
224
    goto get_index_array_out;
177
225
  }
178
226
 
179
227
  /* Compile an sqlite pragma to loop through all indices on table zTab */
180
 
  zSql = sqlite3MPrintf("PRAGMA index_list(%s)", zTab);
 
228
  zSql = sqlite3MPrintf(0, "PRAGMA index_list(%s)", zTab);
181
229
  if( !zSql ){
182
230
    rc = SQLITE_NOMEM;
183
231
    goto get_index_array_out;
184
232
  }
185
233
  rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
186
 
  sqliteFree(zSql);
 
234
  sqlite3_free(zSql);
187
235
 
188
236
  /* For each index, figure out the left-most column and set the 
189
237
  ** corresponding entry in aIndex[] to 1.
191
239
  while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
192
240
    const char *zIdx = (const char *)sqlite3_column_text(pStmt, 1);
193
241
    sqlite3_stmt *pStmt2 = 0;
194
 
    zSql = sqlite3MPrintf("PRAGMA index_info(%s)", zIdx);
 
242
    zSql = sqlite3MPrintf(0, "PRAGMA index_info(%s)", zIdx);
195
243
    if( !zSql ){
196
244
      rc = SQLITE_NOMEM;
197
245
      goto get_index_array_out;
198
246
    }
199
247
    rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0);
200
 
    sqliteFree(zSql);
 
248
    sqlite3_free(zSql);
201
249
    if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){
202
250
      int cid = sqlite3_column_int(pStmt2, 1);
203
251
      assert( cid>=0 && cid<nCol );
220
268
    }
221
269
  }
222
270
  if( rc!=SQLITE_OK ){
223
 
    sqliteFree(aIndex);
 
271
    sqlite3_free(aIndex);
224
272
    aIndex = 0;
225
273
  }
226
274
  *paIndex = aIndex;
255
303
*/
256
304
static int echoDeclareVtab(
257
305
  echo_vtab *pVtab, 
258
 
  sqlite3 *db, 
259
 
  int argc, 
260
 
  const char *const*argv
 
306
  sqlite3 *db 
261
307
){
262
308
  int rc = SQLITE_OK;
263
309
 
264
 
  if( argc>=4 ){
 
310
  if( pVtab->zTableName ){
265
311
    sqlite3_stmt *pStmt = 0;
266
 
    sqlite3_prepare(db, 
 
312
    rc = sqlite3_prepare(db, 
267
313
        "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
268
314
        -1, &pStmt, 0);
269
 
    sqlite3_bind_text(pStmt, 1, argv[3], -1, 0);
270
 
    if( sqlite3_step(pStmt)==SQLITE_ROW ){
271
 
      const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0);
272
 
      sqlite3_declare_vtab(db, zCreateTable);
273
 
      rc = sqlite3_finalize(pStmt);
274
 
    } else {
275
 
      rc = sqlite3_finalize(pStmt);
276
 
      if( rc==SQLITE_OK ){ 
277
 
        rc = SQLITE_ERROR;
278
 
      }
279
 
    }
280
 
 
281
 
    if( rc==SQLITE_OK ){
282
 
      rc = getColumnNames(db, argv[3], &pVtab->aCol, &pVtab->nCol);
283
 
    }
284
 
    if( rc==SQLITE_OK ){
285
 
      rc = getIndexArray(db, argv[3], pVtab->nCol, &pVtab->aIndex);
 
315
    if( rc==SQLITE_OK ){
 
316
      sqlite3_bind_text(pStmt, 1, pVtab->zTableName, -1, 0);
 
317
      if( sqlite3_step(pStmt)==SQLITE_ROW ){
 
318
        int rc2;
 
319
        const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0);
 
320
        rc = sqlite3_declare_vtab(db, zCreateTable);
 
321
        rc2 = sqlite3_finalize(pStmt);
 
322
        if( rc==SQLITE_OK ){
 
323
          rc = rc2;
 
324
        }
 
325
      } else {
 
326
        rc = sqlite3_finalize(pStmt);
 
327
        if( rc==SQLITE_OK ){ 
 
328
          rc = SQLITE_ERROR;
 
329
        }
 
330
      }
 
331
      if( rc==SQLITE_OK ){
 
332
        rc = getColumnNames(db, pVtab->zTableName, &pVtab->aCol, &pVtab->nCol);
 
333
      }
 
334
      if( rc==SQLITE_OK ){
 
335
        rc = getIndexArray(db, pVtab->zTableName, pVtab->nCol, &pVtab->aIndex);
 
336
      }
286
337
    }
287
338
  }
288
339
 
295
346
*/
296
347
static int echoDestructor(sqlite3_vtab *pVtab){
297
348
  echo_vtab *p = (echo_vtab*)pVtab;
298
 
  sqliteFree(p->aIndex);
299
 
  sqliteFree(p->aCol);
300
 
  sqliteFree(p->zTableName);
301
 
  sqliteFree(p->zLogName);
302
 
  sqliteFree(p);
 
349
  sqlite3_free(p->aIndex);
 
350
  sqlite3_free(p->aCol);
 
351
  sqlite3_free(p->zThis);
 
352
  sqlite3_free(p->zTableName);
 
353
  sqlite3_free(p->zLogName);
 
354
  sqlite3_free(p);
303
355
  return 0;
304
356
}
305
357
 
 
358
typedef struct EchoModule EchoModule;
 
359
struct EchoModule {
 
360
  Tcl_Interp *interp;
 
361
};
 
362
 
306
363
/*
307
364
** This function is called to do the work of the xConnect() method -
308
365
** to allocate the required in-memory structures for a newly connected
315
372
  sqlite3_vtab **ppVtab,
316
373
  char **pzErr
317
374
){
 
375
  int rc;
318
376
  int i;
319
377
  echo_vtab *pVtab;
320
378
 
321
379
  /* Allocate the sqlite3_vtab/echo_vtab structure itself */
322
 
  pVtab = sqliteMalloc( sizeof(*pVtab) );
 
380
  pVtab = sqlite3MallocZero( sizeof(*pVtab) );
323
381
  if( !pVtab ){
324
382
    return SQLITE_NOMEM;
325
383
  }
326
 
  pVtab->interp = (Tcl_Interp *)pAux;
 
384
  pVtab->interp = ((EchoModule *)pAux)->interp;
327
385
  pVtab->db = db;
328
386
 
 
387
  /* Allocate echo_vtab.zThis */
 
388
  pVtab->zThis = sqlite3MPrintf(0, "%s", argv[2]);
 
389
  if( !pVtab->zThis ){
 
390
    echoDestructor((sqlite3_vtab *)pVtab);
 
391
    return SQLITE_NOMEM;
 
392
  }
 
393
 
329
394
  /* Allocate echo_vtab.zTableName */
330
 
  pVtab->zTableName = sqlite3MPrintf("%s", argv[3]);
331
 
  if( !pVtab->zTableName ){
332
 
    echoDestructor((sqlite3_vtab *)pVtab);
333
 
    return SQLITE_NOMEM;
 
395
  if( argc>3 ){
 
396
    pVtab->zTableName = sqlite3MPrintf(0, "%s", argv[3]);
 
397
    dequoteString(pVtab->zTableName);
 
398
    if( pVtab->zTableName && pVtab->zTableName[0]=='*' ){
 
399
      char *z = sqlite3MPrintf(0, "%s%s", argv[2], &(pVtab->zTableName[1]));
 
400
      sqlite3_free(pVtab->zTableName);
 
401
      pVtab->zTableName = z;
 
402
      pVtab->isPattern = 1;
 
403
    }
 
404
    if( !pVtab->zTableName ){
 
405
      echoDestructor((sqlite3_vtab *)pVtab);
 
406
      return SQLITE_NOMEM;
 
407
    }
334
408
  }
335
409
 
336
410
  /* Log the arguments to this function to Tcl var ::echo_module */
342
416
  ** structure. If an error occurs, delete the sqlite3_vtab structure and
343
417
  ** return an error code.
344
418
  */
345
 
  if( echoDeclareVtab(pVtab, db, argc, argv) ){
 
419
  rc = echoDeclareVtab(pVtab, db);
 
420
  if( rc!=SQLITE_OK ){
346
421
    echoDestructor((sqlite3_vtab *)pVtab);
347
 
    return SQLITE_ERROR;
 
422
    return rc;
348
423
  }
349
424
 
350
425
  /* Success. Set *ppVtab and return */
363
438
  char **pzErr
364
439
){
365
440
  int rc = SQLITE_OK;
366
 
  appendToEchoModule((Tcl_Interp *)(pAux), "xCreate");
 
441
  appendToEchoModule(((EchoModule *)pAux)->interp, "xCreate");
367
442
  rc = echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
368
443
 
369
444
  /* If there were two arguments passed to the module at the SQL level 
379
454
  if( rc==SQLITE_OK && argc==5 ){
380
455
    char *zSql;
381
456
    echo_vtab *pVtab = *(echo_vtab **)ppVtab;
382
 
    pVtab->zLogName = sqlite3MPrintf("%s", argv[4]);
383
 
    zSql = sqlite3MPrintf("CREATE TABLE %Q(logmsg)", pVtab->zLogName);
 
457
    pVtab->zLogName = sqlite3MPrintf(0, "%s", argv[4]);
 
458
    zSql = sqlite3MPrintf(0, "CREATE TABLE %Q(logmsg)", pVtab->zLogName);
384
459
    rc = sqlite3_exec(db, zSql, 0, 0, 0);
385
 
    sqliteFree(zSql);
 
460
    sqlite3_free(zSql);
 
461
    if( rc!=SQLITE_OK ){
 
462
      *pzErr = sqlite3StrDup(sqlite3_errmsg(db));
 
463
    }
 
464
  }
 
465
 
 
466
  if( *ppVtab && rc!=SQLITE_OK ){
 
467
    echoDestructor(*ppVtab);
 
468
    *ppVtab = 0;
 
469
  }
 
470
 
 
471
  if( rc==SQLITE_OK ){
 
472
    (*(echo_vtab**)ppVtab)->inTransaction = 1;
386
473
  }
387
474
 
388
475
  return rc;
398
485
  sqlite3_vtab **ppVtab,
399
486
  char **pzErr
400
487
){
401
 
  appendToEchoModule((Tcl_Interp *)(pAux), "xConnect");
 
488
  appendToEchoModule(((EchoModule *)pAux)->interp, "xConnect");
402
489
  return echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
403
490
}
404
491
 
421
508
  /* Drop the "log" table, if one exists (see echoCreate() for details) */
422
509
  if( p && p->zLogName ){
423
510
    char *zSql;
424
 
    zSql = sqlite3MPrintf("DROP TABLE %Q", p->zLogName);
 
511
    zSql = sqlite3MPrintf(0, "DROP TABLE %Q", p->zLogName);
425
512
    rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
426
 
    sqliteFree(zSql);
 
513
    sqlite3_free(zSql);
427
514
  }
428
515
 
429
516
  if( rc==SQLITE_OK ){
437
524
*/
438
525
static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
439
526
  echo_cursor *pCur;
440
 
  pCur = sqliteMalloc(sizeof(echo_cursor));
 
527
  pCur = sqlite3MallocZero(sizeof(echo_cursor));
441
528
  *ppCursor = (sqlite3_vtab_cursor *)pCur;
442
529
  return (pCur ? SQLITE_OK : SQLITE_NOMEM);
443
530
}
450
537
  echo_cursor *pCur = (echo_cursor *)cur;
451
538
  sqlite3_stmt *pStmt = pCur->pStmt;
452
539
  pCur->pStmt = 0;
453
 
  sqliteFree(pCur);
 
540
  sqlite3_free(pCur);
454
541
  rc = sqlite3_finalize(pStmt);
455
542
  return rc;
456
543
}
467
554
** Echo virtual table module xNext method.
468
555
*/
469
556
static int echoNext(sqlite3_vtab_cursor *cur){
470
 
  int rc;
 
557
  int rc = SQLITE_OK;
471
558
  echo_cursor *pCur = (echo_cursor *)cur;
472
 
  rc = sqlite3_step(pCur->pStmt);
473
559
 
474
 
  if( rc==SQLITE_ROW ){
475
 
    rc = SQLITE_OK;
476
 
  }else{
477
 
    rc = sqlite3_finalize(pCur->pStmt);
478
 
    pCur->pStmt = 0;
 
560
  if( pCur->pStmt ){
 
561
    rc = sqlite3_step(pCur->pStmt);
 
562
    if( rc==SQLITE_ROW ){
 
563
      rc = SQLITE_OK;
 
564
    }else{
 
565
      rc = sqlite3_finalize(pCur->pStmt);
 
566
      pCur->pStmt = 0;
 
567
    }
479
568
  }
480
569
 
481
570
  return rc;
582
671
** If the third argument, doFree, is true, then sqlite3_free() is
583
672
** also called to free the buffer pointed to by zAppend.
584
673
*/
585
 
static void string_concat(char **pzStr, char *zAppend, int doFree){
 
674
static void string_concat(char **pzStr, char *zAppend, int doFree, int *pRc){
586
675
  char *zIn = *pzStr;
587
 
  if( zIn ){
588
 
    char *zTemp = zIn;
589
 
    zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
590
 
    sqlite3_free(zTemp);
 
676
  if( !zAppend && doFree && *pRc==SQLITE_OK ){
 
677
    *pRc = SQLITE_NOMEM;
 
678
  }
 
679
  if( *pRc!=SQLITE_OK ){
 
680
    sqlite3_free(zIn);
 
681
    zIn = 0;
591
682
  }else{
592
 
    zIn = sqlite3_mprintf("%s", zAppend);
 
683
    if( zIn ){
 
684
      char *zTemp = zIn;
 
685
      zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
 
686
      sqlite3_free(zTemp);
 
687
    }else{
 
688
      zIn = sqlite3_mprintf("%s", zAppend);
 
689
    }
 
690
    if( !zIn ){
 
691
      *pRc = SQLITE_NOMEM;
 
692
    }
593
693
  }
594
694
  *pzStr = zIn;
595
695
  if( doFree ){
607
707
**
608
708
** then the echo module handles WHERE or ORDER BY clauses that refer
609
709
** to the column "b", but not "a" or "c". If a multi-column index is
610
 
** present, only it's left most column is considered. 
 
710
** present, only its left most column is considered. 
611
711
**
612
712
** This xBestIndex method encodes the proposed search strategy as
613
713
** an SQL query on the real table underlying the virtual echo module 
627
727
  const char *zSep = "WHERE";
628
728
  echo_vtab *pVtab = (echo_vtab *)tab;
629
729
  sqlite3_stmt *pStmt = 0;
 
730
  Tcl_Interp *interp = pVtab->interp;
630
731
 
631
732
  int nRow;
632
733
  int useIdx = 0;
633
734
  int rc = SQLITE_OK;
 
735
  int useCost = 0;
 
736
  double cost;
 
737
 
 
738
  int isIgnoreUsable = 0;
 
739
  if( Tcl_GetVar(interp, "echo_module_ignore_usable", TCL_GLOBAL_ONLY) ){
 
740
    isIgnoreUsable = 1;
 
741
  }
634
742
 
635
743
  /* Determine the number of rows in the table and store this value in local
636
744
  ** variable nRow. The 'estimated-cost' of the scan will be the number of
637
745
  ** rows in the table for a linear scan, or the log (base 2) of the 
638
746
  ** number of rows if the proposed scan uses an index.  
639
747
  */
640
 
  zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
641
 
  rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
642
 
  if( rc!=SQLITE_OK ){
643
 
    return rc;
644
 
  }
645
 
  sqlite3_step(pStmt);
646
 
  nRow = sqlite3_column_int(pStmt, 0);
647
 
  rc = sqlite3_finalize(pStmt);
648
 
  if( rc!=SQLITE_OK ){
649
 
    return rc;
 
748
  if( Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY) ){
 
749
    cost = atof(Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY));
 
750
    useCost = 1;
 
751
  } else {
 
752
    zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
 
753
    if( !zQuery ){
 
754
      return SQLITE_NOMEM;
 
755
    }
 
756
    rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
 
757
    sqlite3_free(zQuery);
 
758
    if( rc!=SQLITE_OK ){
 
759
      return rc;
 
760
    }
 
761
    sqlite3_step(pStmt);
 
762
    nRow = sqlite3_column_int(pStmt, 0);
 
763
    rc = sqlite3_finalize(pStmt);
 
764
    if( rc!=SQLITE_OK ){
 
765
      return rc;
 
766
    }
650
767
  }
651
768
 
652
769
  zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
 
770
  if( !zQuery ){
 
771
    return SQLITE_NOMEM;
 
772
  }
653
773
  for(ii=0; ii<pIdxInfo->nConstraint; ii++){
654
774
    const struct sqlite3_index_constraint *pConstraint;
655
775
    struct sqlite3_index_constraint_usage *pUsage;
658
778
    pConstraint = &pIdxInfo->aConstraint[ii];
659
779
    pUsage = &pIdxInfo->aConstraintUsage[ii];
660
780
 
 
781
    if( !isIgnoreUsable && !pConstraint->usable ) continue;
 
782
 
661
783
    iCol = pConstraint->iColumn;
662
784
    if( pVtab->aIndex[iCol] ){
663
785
      char *zCol = pVtab->aCol[iCol];
686
808
      } else {
687
809
        zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp);
688
810
      }
689
 
      string_concat(&zQuery, zNew, 1);
 
811
      string_concat(&zQuery, zNew, 1, &rc);
690
812
 
691
813
      zSep = "AND";
692
814
      pUsage->argvIndex = ++nArg;
706
828
      zCol = "rowid";
707
829
    }
708
830
    zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir);
709
 
    string_concat(&zQuery, zNew, 1);
 
831
    string_concat(&zQuery, zNew, 1, &rc);
710
832
    pIdxInfo->orderByConsumed = 1;
711
833
  }
712
834
 
713
835
  appendToEchoModule(pVtab->interp, "xBestIndex");;
714
836
  appendToEchoModule(pVtab->interp, zQuery);
715
837
 
 
838
  if( !zQuery ){
 
839
    return rc;
 
840
  }
716
841
  pIdxInfo->idxNum = hashString(zQuery);
717
842
  pIdxInfo->idxStr = zQuery;
718
843
  pIdxInfo->needToFreeIdxStr = 1;
719
 
  if( useIdx ){
 
844
  if (useCost) {
 
845
    pIdxInfo->estimatedCost = cost;
 
846
  } else if( useIdx ){
720
847
    /* Approximation of log2(nRow). */
721
848
    for( ii=0; ii<(sizeof(int)*8); ii++ ){
722
849
      if( nRow & (1<<ii) ){
761
888
 
762
889
  assert( nData==pVtab->nCol+2 || nData==1 );
763
890
 
 
891
  /* Ticket #3083 - make sure we always start a transaction prior to
 
892
  ** making any changes to a virtual table */
 
893
  assert( pVtab->inTransaction );
 
894
 
764
895
  /* If apData[0] is an integer and nData>1 then do an UPDATE */
765
896
  if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
766
897
    char *zSep = " SET";
767
898
    z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
 
899
    if( !z ){
 
900
      rc = SQLITE_NOMEM;
 
901
    }
768
902
 
769
903
    bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
770
904
    bindArgZero = 1;
771
905
 
772
906
    if( bindArgOne ){
773
 
       string_concat(&z, " SET rowid=?1 ", 0);
 
907
       string_concat(&z, " SET rowid=?1 ", 0, &rc);
774
908
       zSep = ",";
775
909
    }
776
910
    for(i=2; i<nData; i++){
777
911
      if( apData[i]==0 ) continue;
778
912
      string_concat(&z, sqlite3_mprintf(
779
 
          "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1);
 
913
          "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1, &rc);
780
914
      zSep = ",";
781
915
    }
782
 
    string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 0);
 
916
    string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 1, &rc);
783
917
  }
784
918
 
785
919
  /* If apData[0] is an integer and nData==1 then do a DELETE */
786
920
  else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
787
921
    z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
 
922
    if( !z ){
 
923
      rc = SQLITE_NOMEM;
 
924
    }
788
925
    bindArgZero = 1;
789
926
  }
790
927
 
795
932
    char *zValues = 0;
796
933
  
797
934
    zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
 
935
    if( !zInsert ){
 
936
      rc = SQLITE_NOMEM;
 
937
    }
798
938
    if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
799
939
      bindArgOne = 1;
800
940
      zValues = sqlite3_mprintf("?");
801
 
      string_concat(&zInsert, "rowid", 0);
 
941
      string_concat(&zInsert, "rowid", 0, &rc);
802
942
    }
803
943
 
804
944
    assert((pVtab->nCol+2)==nData);
805
945
    for(ii=2; ii<nData; ii++){
806
946
      string_concat(&zInsert, 
807
 
          sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1);
 
947
          sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1, &rc);
808
948
      string_concat(&zValues, 
809
 
          sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1);
 
949
          sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1, &rc);
810
950
    }
811
951
 
812
 
    string_concat(&z, zInsert, 1);
813
 
    string_concat(&z, ") VALUES(", 0);
814
 
    string_concat(&z, zValues, 1);
815
 
    string_concat(&z, ")", 0);
 
952
    string_concat(&z, zInsert, 1, &rc);
 
953
    string_concat(&z, ") VALUES(", 0, &rc);
 
954
    string_concat(&z, zValues, 1, &rc);
 
955
    string_concat(&z, ")", 0, &rc);
816
956
  }
817
957
 
818
958
  /* Anything else is an error */
821
961
    return SQLITE_ERROR;
822
962
  }
823
963
 
824
 
  rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
 
964
  if( rc==SQLITE_OK ){
 
965
    rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
 
966
  }
825
967
  assert( rc!=SQLITE_OK || pStmt );
826
968
  sqlite3_free(z);
827
969
  if( rc==SQLITE_OK ) {
831
973
    if( bindArgOne ){
832
974
      sqlite3_bind_value(pStmt, 1, apData[1]);
833
975
    }
834
 
    for(i=2; i<nData; i++){
835
 
      if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]);
836
 
    }
837
 
    sqlite3_step(pStmt);
838
 
    rc = sqlite3_finalize(pStmt);
 
976
    for(i=2; i<nData && rc==SQLITE_OK; i++){
 
977
      if( apData[i] ) rc = sqlite3_bind_value(pStmt, i, apData[i]);
 
978
    }
 
979
    if( rc==SQLITE_OK ){
 
980
      sqlite3_step(pStmt);
 
981
      rc = sqlite3_finalize(pStmt);
 
982
    }else{
 
983
      sqlite3_finalize(pStmt);
 
984
    }
839
985
  }
840
986
 
841
987
  if( pRowid && rc==SQLITE_OK ){
854
1000
  char *z;
855
1001
  echo_vtab *pVtab = (echo_vtab *)tab;
856
1002
  z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
 
1003
  if( z==0 ) return SQLITE_NOMEM;
857
1004
  appendToEchoModule(pVtab->interp, zCall);
858
1005
  appendToEchoModule(pVtab->interp, z);
859
1006
  sqlite3_free(z);
860
1007
  return SQLITE_OK;
861
1008
}
862
1009
static int echoBegin(sqlite3_vtab *tab){
 
1010
  int rc;
863
1011
  echo_vtab *pVtab = (echo_vtab *)tab;
864
1012
  Tcl_Interp *interp = pVtab->interp;
865
1013
  const char *zVal; 
866
1014
 
867
 
  echoTransactionCall(tab, "xBegin");
868
 
 
869
 
  /* Check if the $::echo_module_begin_fail variable is defined. If it is,
870
 
  ** and it is set to the name of the real table underlying this virtual
871
 
  ** echo module table, then cause this xSync operation to fail.
872
 
  */
873
 
  zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
874
 
  if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
875
 
    return SQLITE_ERROR;
876
 
  }
877
 
  return SQLITE_OK;
 
1015
  /* Ticket #3083 - do not start a transaction if we are already in
 
1016
  ** a transaction */
 
1017
  assert( !pVtab->inTransaction );
 
1018
 
 
1019
  rc = echoTransactionCall(tab, "xBegin");
 
1020
 
 
1021
  if( rc==SQLITE_OK ){
 
1022
    /* Check if the $::echo_module_begin_fail variable is defined. If it is,
 
1023
    ** and it is set to the name of the real table underlying this virtual
 
1024
    ** echo module table, then cause this xSync operation to fail.
 
1025
    */
 
1026
    zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
 
1027
    if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
 
1028
      rc = SQLITE_ERROR;
 
1029
    }
 
1030
  }
 
1031
  if( rc==SQLITE_OK ){
 
1032
    pVtab->inTransaction = 1;
 
1033
  }
 
1034
  return rc;
878
1035
}
879
1036
static int echoSync(sqlite3_vtab *tab){
 
1037
  int rc;
880
1038
  echo_vtab *pVtab = (echo_vtab *)tab;
881
1039
  Tcl_Interp *interp = pVtab->interp;
882
1040
  const char *zVal; 
883
1041
 
884
 
  echoTransactionCall(tab, "xSync");
885
 
 
886
 
  /* Check if the $::echo_module_sync_fail variable is defined. If it is,
887
 
  ** and it is set to the name of the real table underlying this virtual
888
 
  ** echo module table, then cause this xSync operation to fail.
889
 
  */
890
 
  zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
891
 
  if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
892
 
    return -1;
 
1042
  /* Ticket #3083 - Only call xSync if we have previously started a
 
1043
  ** transaction */
 
1044
  assert( pVtab->inTransaction );
 
1045
 
 
1046
  rc = echoTransactionCall(tab, "xSync");
 
1047
 
 
1048
  if( rc==SQLITE_OK ){
 
1049
    /* Check if the $::echo_module_sync_fail variable is defined. If it is,
 
1050
    ** and it is set to the name of the real table underlying this virtual
 
1051
    ** echo module table, then cause this xSync operation to fail.
 
1052
    */
 
1053
    zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
 
1054
    if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
 
1055
      rc = -1;
 
1056
    }
893
1057
  }
894
 
  return SQLITE_OK;
 
1058
  return rc;
895
1059
}
896
1060
static int echoCommit(sqlite3_vtab *tab){
897
 
  return echoTransactionCall(tab, "xCommit");
 
1061
  echo_vtab *pVtab = (echo_vtab*)tab;
 
1062
  int rc;
 
1063
 
 
1064
  /* Ticket #3083 - Only call xCommit if we have previously started
 
1065
  ** a transaction */
 
1066
  assert( pVtab->inTransaction );
 
1067
 
 
1068
  sqlite3FaultBeginBenign(SQLITE_FAULTINJECTOR_MALLOC);
 
1069
  rc = echoTransactionCall(tab, "xCommit");
 
1070
  sqlite3FaultEndBenign(SQLITE_FAULTINJECTOR_MALLOC);
 
1071
  pVtab->inTransaction = 0;
 
1072
  return rc;
898
1073
}
899
1074
static int echoRollback(sqlite3_vtab *tab){
900
 
  return echoTransactionCall(tab, "xRollback");
 
1075
  int rc;
 
1076
  echo_vtab *pVtab = (echo_vtab*)tab;
 
1077
 
 
1078
  /* Ticket #3083 - Only call xRollback if we have previously started
 
1079
  ** a transaction */
 
1080
  assert( pVtab->inTransaction );
 
1081
 
 
1082
  rc = echoTransactionCall(tab, "xRollback");
 
1083
  pVtab->inTransaction = 0;
 
1084
  return rc;
901
1085
}
902
1086
 
903
1087
/*
959
1143
  return 1;
960
1144
}
961
1145
 
 
1146
static int echoRename(sqlite3_vtab *vtab, const char *zNewName){
 
1147
  int rc = SQLITE_OK;
 
1148
  echo_vtab *p = (echo_vtab *)vtab;
 
1149
 
 
1150
  if( p->isPattern ){
 
1151
    int nThis = strlen(p->zThis);
 
1152
    char *zSql = sqlite3MPrintf(0, "ALTER TABLE %s RENAME TO %s%s", 
 
1153
        p->zTableName, zNewName, &p->zTableName[nThis]
 
1154
    );
 
1155
    rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
 
1156
    sqlite3_free(zSql);
 
1157
  }
 
1158
 
 
1159
  return rc;
 
1160
}
 
1161
 
962
1162
/*
963
1163
** A virtual table module that merely "echos" the contents of another
964
1164
** table (like an SQL VIEW).
983
1183
  echoCommit,                /* xCommit - commit transaction */
984
1184
  echoRollback,              /* xRollback - rollback transaction */
985
1185
  echoFindFunction,          /* xFindFunction - function overloading */
 
1186
  echoRename,                /* xRename - rename the table */
986
1187
};
987
1188
 
988
1189
/*
993
1194
  return TCL_OK;
994
1195
}
995
1196
 
 
1197
static void moduleDestroy(void *p){
 
1198
  sqlite3_free(p);
 
1199
}
 
1200
 
996
1201
/*
997
1202
** Register the echo virtual table module.
998
1203
*/
1003
1208
  Tcl_Obj *CONST objv[]  /* Command arguments */
1004
1209
){
1005
1210
  sqlite3 *db;
 
1211
  EchoModule *pMod;
1006
1212
  if( objc!=2 ){
1007
1213
    Tcl_WrongNumArgs(interp, 1, objv, "DB");
1008
1214
    return TCL_ERROR;
1009
1215
  }
1010
1216
  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1011
 
  sqlite3_create_module(db, "echo", &echoModule, (void *)interp);
 
1217
  pMod = sqlite3_malloc(sizeof(EchoModule));
 
1218
  pMod->interp = interp;
 
1219
  sqlite3_create_module_v2(db, "echo", &echoModule, (void*)pMod, moduleDestroy);
1012
1220
  return TCL_OK;
1013
1221
}
1014
1222