extensions/sqlite/sqlite-source/vtab.c
author Matt Woodrow <pred@alliedmods.net>
Mon Aug 17 10:53:54 2009 +1200 (2009-08-17)
changeset 2771 ea849fc91a9b
parent 1952 b7aa2b626807
permissions -rw-r--r--
Forgot makefiles patch
ds@1952
     1
/*
ds@1952
     2
** 2006 June 10
ds@1952
     3
**
ds@1952
     4
** The author disclaims copyright to this source code.  In place of
ds@1952
     5
** a legal notice, here is a blessing:
ds@1952
     6
**
ds@1952
     7
**    May you do good and not evil.
ds@1952
     8
**    May you find forgiveness for yourself and forgive others.
ds@1952
     9
**    May you share freely, never taking more than you give.
ds@1952
    10
**
ds@1952
    11
*************************************************************************
ds@1952
    12
** This file contains code used to help implement virtual tables.
ds@1952
    13
**
ds@1952
    14
** $Id$
ds@1952
    15
*/
ds@1952
    16
#ifndef SQLITE_OMIT_VIRTUALTABLE
ds@1952
    17
#include "sqliteInt.h"
ds@1952
    18
ds@1952
    19
static int createModule(
ds@1952
    20
  sqlite3 *db,                    /* Database in which module is registered */
ds@1952
    21
  const char *zName,              /* Name assigned to this module */
ds@1952
    22
  const sqlite3_module *pModule,  /* The definition of the module */
ds@1952
    23
  void *pAux,                     /* Context pointer for xCreate/xConnect */
ds@1952
    24
  void (*xDestroy)(void *)        /* Module destructor function */
ds@1952
    25
) {
ds@1952
    26
  int rc, nName;
ds@1952
    27
  Module *pMod;
ds@1952
    28
ds@1952
    29
  sqlite3_mutex_enter(db->mutex);
ds@1952
    30
  nName = strlen(zName);
ds@1952
    31
  pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
ds@1952
    32
  if( pMod ){
ds@1952
    33
    char *zCopy = (char *)(&pMod[1]);
ds@1952
    34
    memcpy(zCopy, zName, nName+1);
ds@1952
    35
    pMod->zName = zCopy;
ds@1952
    36
    pMod->pModule = pModule;
ds@1952
    37
    pMod->pAux = pAux;
ds@1952
    38
    pMod->xDestroy = xDestroy;
ds@1952
    39
    pMod = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
ds@1952
    40
    if( pMod && pMod->xDestroy ){
ds@1952
    41
      pMod->xDestroy(pMod->pAux);
ds@1952
    42
    }
ds@1952
    43
    sqlite3_free(pMod);
ds@1952
    44
    sqlite3ResetInternalSchema(db, 0);
ds@1952
    45
  }
ds@1952
    46
  rc = sqlite3ApiExit(db, SQLITE_OK);
ds@1952
    47
  sqlite3_mutex_leave(db->mutex);
ds@1952
    48
  return rc;
ds@1952
    49
}
ds@1952
    50
ds@1952
    51
ds@1952
    52
/*
ds@1952
    53
** External API function used to create a new virtual-table module.
ds@1952
    54
*/
ds@1952
    55
int sqlite3_create_module(
ds@1952
    56
  sqlite3 *db,                    /* Database in which module is registered */
ds@1952
    57
  const char *zName,              /* Name assigned to this module */
ds@1952
    58
  const sqlite3_module *pModule,  /* The definition of the module */
ds@1952
    59
  void *pAux                      /* Context pointer for xCreate/xConnect */
ds@1952
    60
){
ds@1952
    61
  return createModule(db, zName, pModule, pAux, 0);
ds@1952
    62
}
ds@1952
    63
ds@1952
    64
/*
ds@1952
    65
** External API function used to create a new virtual-table module.
ds@1952
    66
*/
ds@1952
    67
int sqlite3_create_module_v2(
ds@1952
    68
  sqlite3 *db,                    /* Database in which module is registered */
ds@1952
    69
  const char *zName,              /* Name assigned to this module */
ds@1952
    70
  const sqlite3_module *pModule,  /* The definition of the module */
ds@1952
    71
  void *pAux,                     /* Context pointer for xCreate/xConnect */
ds@1952
    72
  void (*xDestroy)(void *)        /* Module destructor function */
ds@1952
    73
){
ds@1952
    74
  return createModule(db, zName, pModule, pAux, xDestroy);
ds@1952
    75
}
ds@1952
    76
ds@1952
    77
/*
ds@1952
    78
** Lock the virtual table so that it cannot be disconnected.
ds@1952
    79
** Locks nest.  Every lock should have a corresponding unlock.
ds@1952
    80
** If an unlock is omitted, resources leaks will occur.  
ds@1952
    81
**
ds@1952
    82
** If a disconnect is attempted while a virtual table is locked,
ds@1952
    83
** the disconnect is deferred until all locks have been removed.
ds@1952
    84
*/
ds@1952
    85
void sqlite3VtabLock(sqlite3_vtab *pVtab){
ds@1952
    86
  pVtab->nRef++;
ds@1952
    87
}
ds@1952
    88
ds@1952
    89
/*
ds@1952
    90
** Unlock a virtual table.  When the last lock is removed,
ds@1952
    91
** disconnect the virtual table.
ds@1952
    92
*/
ds@1952
    93
void sqlite3VtabUnlock(sqlite3 *db, sqlite3_vtab *pVtab){
ds@1952
    94
  pVtab->nRef--;
ds@1952
    95
  assert(db);
ds@1952
    96
  assert(!sqlite3SafetyCheck(db));
ds@1952
    97
  if( pVtab->nRef==0 ){
ds@1952
    98
    if( db->magic==SQLITE_MAGIC_BUSY ){
ds@1952
    99
      sqlite3SafetyOff(db);
ds@1952
   100
      pVtab->pModule->xDisconnect(pVtab);
ds@1952
   101
      sqlite3SafetyOn(db);
ds@1952
   102
    } else {
ds@1952
   103
      pVtab->pModule->xDisconnect(pVtab);
ds@1952
   104
    }
ds@1952
   105
  }
ds@1952
   106
}
ds@1952
   107
ds@1952
   108
/*
ds@1952
   109
** Clear any and all virtual-table information from the Table record.
ds@1952
   110
** This routine is called, for example, just before deleting the Table
ds@1952
   111
** record.
ds@1952
   112
*/
ds@1952
   113
void sqlite3VtabClear(Table *p){
ds@1952
   114
  sqlite3_vtab *pVtab = p->pVtab;
ds@1952
   115
  if( pVtab ){
ds@1952
   116
    assert( p->pMod && p->pMod->pModule );
ds@1952
   117
    sqlite3VtabUnlock(p->pSchema->db, pVtab);
ds@1952
   118
    p->pVtab = 0;
ds@1952
   119
  }
ds@1952
   120
  if( p->azModuleArg ){
ds@1952
   121
    int i;
ds@1952
   122
    for(i=0; i<p->nModuleArg; i++){
ds@1952
   123
      sqlite3_free(p->azModuleArg[i]);
ds@1952
   124
    }
ds@1952
   125
    sqlite3_free(p->azModuleArg);
ds@1952
   126
  }
ds@1952
   127
}
ds@1952
   128
ds@1952
   129
/*
ds@1952
   130
** Add a new module argument to pTable->azModuleArg[].
ds@1952
   131
** The string is not copied - the pointer is stored.  The
ds@1952
   132
** string will be freed automatically when the table is
ds@1952
   133
** deleted.
ds@1952
   134
*/
ds@1952
   135
static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
ds@1952
   136
  int i = pTable->nModuleArg++;
ds@1952
   137
  int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
ds@1952
   138
  char **azModuleArg;
ds@1952
   139
  azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
ds@1952
   140
  if( azModuleArg==0 ){
ds@1952
   141
    int j;
ds@1952
   142
    for(j=0; j<i; j++){
ds@1952
   143
      sqlite3_free(pTable->azModuleArg[j]);
ds@1952
   144
    }
ds@1952
   145
    sqlite3_free(zArg);
ds@1952
   146
    sqlite3_free(pTable->azModuleArg);
ds@1952
   147
    pTable->nModuleArg = 0;
ds@1952
   148
  }else{
ds@1952
   149
    azModuleArg[i] = zArg;
ds@1952
   150
    azModuleArg[i+1] = 0;
ds@1952
   151
  }
ds@1952
   152
  pTable->azModuleArg = azModuleArg;
ds@1952
   153
}
ds@1952
   154
ds@1952
   155
/*
ds@1952
   156
** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
ds@1952
   157
** statement.  The module name has been parsed, but the optional list
ds@1952
   158
** of parameters that follow the module name are still pending.
ds@1952
   159
*/
ds@1952
   160
void sqlite3VtabBeginParse(
ds@1952
   161
  Parse *pParse,        /* Parsing context */
ds@1952
   162
  Token *pName1,        /* Name of new table, or database name */
ds@1952
   163
  Token *pName2,        /* Name of new table or NULL */
ds@1952
   164
  Token *pModuleName    /* Name of the module for the virtual table */
ds@1952
   165
){
ds@1952
   166
  int iDb;              /* The database the table is being created in */
ds@1952
   167
  Table *pTable;        /* The new virtual table */
ds@1952
   168
  sqlite3 *db;          /* Database connection */
ds@1952
   169
ds@1952
   170
  if( pParse->db->flags & SQLITE_SharedCache ){
ds@1952
   171
    sqlite3ErrorMsg(pParse, "Cannot use virtual tables in shared-cache mode");
ds@1952
   172
    return;
ds@1952
   173
  }
ds@1952
   174
ds@1952
   175
  sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0);
ds@1952
   176
  pTable = pParse->pNewTable;
ds@1952
   177
  if( pTable==0 || pParse->nErr ) return;
ds@1952
   178
  assert( 0==pTable->pIndex );
ds@1952
   179
ds@1952
   180
  db = pParse->db;
ds@1952
   181
  iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
ds@1952
   182
  assert( iDb>=0 );
ds@1952
   183
ds@1952
   184
  pTable->isVirtual = 1;
ds@1952
   185
  pTable->nModuleArg = 0;
ds@1952
   186
  addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
ds@1952
   187
  addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName));
ds@1952
   188
  addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
ds@1952
   189
  pParse->sNameToken.n = pModuleName->z + pModuleName->n - pName1->z;
ds@1952
   190
ds@1952
   191
#ifndef SQLITE_OMIT_AUTHORIZATION
ds@1952
   192
  /* Creating a virtual table invokes the authorization callback twice.
ds@1952
   193
  ** The first invocation, to obtain permission to INSERT a row into the
ds@1952
   194
  ** sqlite_master table, has already been made by sqlite3StartTable().
ds@1952
   195
  ** The second call, to obtain permission to create the table, is made now.
ds@1952
   196
  */
ds@1952
   197
  if( pTable->azModuleArg ){
ds@1952
   198
    sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, 
ds@1952
   199
            pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
ds@1952
   200
  }
ds@1952
   201
#endif
ds@1952
   202
}
ds@1952
   203
ds@1952
   204
/*
ds@1952
   205
** This routine takes the module argument that has been accumulating
ds@1952
   206
** in pParse->zArg[] and appends it to the list of arguments on the
ds@1952
   207
** virtual table currently under construction in pParse->pTable.
ds@1952
   208
*/
ds@1952
   209
static void addArgumentToVtab(Parse *pParse){
ds@1952
   210
  if( pParse->sArg.z && pParse->pNewTable ){
ds@1952
   211
    const char *z = (const char*)pParse->sArg.z;
ds@1952
   212
    int n = pParse->sArg.n;
ds@1952
   213
    sqlite3 *db = pParse->db;
ds@1952
   214
    addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
ds@1952
   215
  }
ds@1952
   216
}
ds@1952
   217
ds@1952
   218
/*
ds@1952
   219
** The parser calls this routine after the CREATE VIRTUAL TABLE statement
ds@1952
   220
** has been completely parsed.
ds@1952
   221
*/
ds@1952
   222
void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
ds@1952
   223
  Table *pTab;        /* The table being constructed */
ds@1952
   224
  sqlite3 *db;        /* The database connection */
ds@1952
   225
  char *zModule;      /* The module name of the table: USING modulename */
ds@1952
   226
  Module *pMod = 0;
ds@1952
   227
ds@1952
   228
  addArgumentToVtab(pParse);
ds@1952
   229
  pParse->sArg.z = 0;
ds@1952
   230
ds@1952
   231
  /* Lookup the module name. */
ds@1952
   232
  pTab = pParse->pNewTable;
ds@1952
   233
  if( pTab==0 ) return;
ds@1952
   234
  db = pParse->db;
ds@1952
   235
  if( pTab->nModuleArg<1 ) return;
ds@1952
   236
  zModule = pTab->azModuleArg[0];
ds@1952
   237
  pMod = (Module *)sqlite3HashFind(&db->aModule, zModule, strlen(zModule));
ds@1952
   238
  pTab->pMod = pMod;
ds@1952
   239
  
ds@1952
   240
  /* If the CREATE VIRTUAL TABLE statement is being entered for the
ds@1952
   241
  ** first time (in other words if the virtual table is actually being
ds@1952
   242
  ** created now instead of just being read out of sqlite_master) then
ds@1952
   243
  ** do additional initialization work and store the statement text
ds@1952
   244
  ** in the sqlite_master table.
ds@1952
   245
  */
ds@1952
   246
  if( !db->init.busy ){
ds@1952
   247
    char *zStmt;
ds@1952
   248
    char *zWhere;
ds@1952
   249
    int iDb;
ds@1952
   250
    Vdbe *v;
ds@1952
   251
ds@1952
   252
    /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
ds@1952
   253
    if( pEnd ){
ds@1952
   254
      pParse->sNameToken.n = pEnd->z - pParse->sNameToken.z + pEnd->n;
ds@1952
   255
    }
ds@1952
   256
    zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
ds@1952
   257
ds@1952
   258
    /* A slot for the record has already been allocated in the 
ds@1952
   259
    ** SQLITE_MASTER table.  We just need to update that slot with all
ds@1952
   260
    ** the information we've collected.  
ds@1952
   261
    **
ds@1952
   262
    ** The top of the stack is the rootpage allocated by sqlite3StartTable().
ds@1952
   263
    ** This value is always 0 and is ignored, a virtual table does not have a
ds@1952
   264
    ** rootpage. The next entry on the stack is the rowid of the record
ds@1952
   265
    ** in the sqlite_master table.
ds@1952
   266
    */
ds@1952
   267
    iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
ds@1952
   268
    sqlite3NestedParse(pParse,
ds@1952
   269
      "UPDATE %Q.%s "
ds@1952
   270
         "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
ds@1952
   271
       "WHERE rowid=#1",
ds@1952
   272
      db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
ds@1952
   273
      pTab->zName,
ds@1952
   274
      pTab->zName,
ds@1952
   275
      zStmt
ds@1952
   276
    );
ds@1952
   277
    sqlite3_free(zStmt);
ds@1952
   278
    v = sqlite3GetVdbe(pParse);
ds@1952
   279
    sqlite3ChangeCookie(db, v, iDb);
ds@1952
   280
ds@1952
   281
    sqlite3VdbeAddOp(v, OP_Expire, 0, 0);
ds@1952
   282
    zWhere = sqlite3MPrintf(db, "name='%q'", pTab->zName);
ds@1952
   283
    sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 1, zWhere, P3_DYNAMIC);
ds@1952
   284
    sqlite3VdbeOp3(v, OP_VCreate, iDb, 0, pTab->zName, strlen(pTab->zName) + 1);
ds@1952
   285
  }
ds@1952
   286
ds@1952
   287
  /* If we are rereading the sqlite_master table create the in-memory
ds@1952
   288
  ** record of the table. If the module has already been registered,
ds@1952
   289
  ** also call the xConnect method here.
ds@1952
   290
  */
ds@1952
   291
  else {
ds@1952
   292
    Table *pOld;
ds@1952
   293
    Schema *pSchema = pTab->pSchema;
ds@1952
   294
    const char *zName = pTab->zName;
ds@1952
   295
    int nName = strlen(zName) + 1;
ds@1952
   296
    pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
ds@1952
   297
    if( pOld ){
ds@1952
   298
      db->mallocFailed = 1;
ds@1952
   299
      assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
ds@1952
   300
      return;
ds@1952
   301
    }
ds@1952
   302
    pSchema->db = pParse->db;
ds@1952
   303
    pParse->pNewTable = 0;
ds@1952
   304
  }
ds@1952
   305
}
ds@1952
   306
ds@1952
   307
/*
ds@1952
   308
** The parser calls this routine when it sees the first token
ds@1952
   309
** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
ds@1952
   310
*/
ds@1952
   311
void sqlite3VtabArgInit(Parse *pParse){
ds@1952
   312
  addArgumentToVtab(pParse);
ds@1952
   313
  pParse->sArg.z = 0;
ds@1952
   314
  pParse->sArg.n = 0;
ds@1952
   315
}
ds@1952
   316
ds@1952
   317
/*
ds@1952
   318
** The parser calls this routine for each token after the first token
ds@1952
   319
** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
ds@1952
   320
*/
ds@1952
   321
void sqlite3VtabArgExtend(Parse *pParse, Token *p){
ds@1952
   322
  Token *pArg = &pParse->sArg;
ds@1952
   323
  if( pArg->z==0 ){
ds@1952
   324
    pArg->z = p->z;
ds@1952
   325
    pArg->n = p->n;
ds@1952
   326
  }else{
ds@1952
   327
    assert(pArg->z < p->z);
ds@1952
   328
    pArg->n = (p->z + p->n - pArg->z);
ds@1952
   329
  }
ds@1952
   330
}
ds@1952
   331
ds@1952
   332
/*
ds@1952
   333
** Invoke a virtual table constructor (either xCreate or xConnect). The
ds@1952
   334
** pointer to the function to invoke is passed as the fourth parameter
ds@1952
   335
** to this procedure.
ds@1952
   336
*/
ds@1952
   337
static int vtabCallConstructor(
ds@1952
   338
  sqlite3 *db, 
ds@1952
   339
  Table *pTab,
ds@1952
   340
  Module *pMod,
ds@1952
   341
  int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
ds@1952
   342
  char **pzErr
ds@1952
   343
){
ds@1952
   344
  int rc;
ds@1952
   345
  int rc2;
ds@1952
   346
  sqlite3_vtab *pVtab = 0;
ds@1952
   347
  const char *const*azArg = (const char *const*)pTab->azModuleArg;
ds@1952
   348
  int nArg = pTab->nModuleArg;
ds@1952
   349
  char *zErr = 0;
ds@1952
   350
  char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
ds@1952
   351
ds@1952
   352
  if( !zModuleName ){
ds@1952
   353
    return SQLITE_NOMEM;
ds@1952
   354
  }
ds@1952
   355
ds@1952
   356
  assert( !db->pVTab );
ds@1952
   357
  assert( xConstruct );
ds@1952
   358
ds@1952
   359
  db->pVTab = pTab;
ds@1952
   360
  rc = sqlite3SafetyOff(db);
ds@1952
   361
  assert( rc==SQLITE_OK );
ds@1952
   362
  rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVtab, &zErr);
ds@1952
   363
  rc2 = sqlite3SafetyOn(db);
ds@1952
   364
  if( rc==SQLITE_OK && pVtab ){
ds@1952
   365
    pVtab->pModule = pMod->pModule;
ds@1952
   366
    pVtab->nRef = 1;
ds@1952
   367
    pTab->pVtab = pVtab;
ds@1952
   368
  }
ds@1952
   369
ds@1952
   370
  if( SQLITE_OK!=rc ){
ds@1952
   371
    if( zErr==0 ){
ds@1952
   372
      *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
ds@1952
   373
    }else {
ds@1952
   374
      *pzErr = sqlite3MPrintf(db, "%s", zErr);
ds@1952
   375
      sqlite3_free(zErr);
ds@1952
   376
    }
ds@1952
   377
  }else if( db->pVTab ){
ds@1952
   378
    const char *zFormat = "vtable constructor did not declare schema: %s";
ds@1952
   379
    *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
ds@1952
   380
    rc = SQLITE_ERROR;
ds@1952
   381
  } 
ds@1952
   382
  if( rc==SQLITE_OK ){
ds@1952
   383
    rc = rc2;
ds@1952
   384
  }
ds@1952
   385
  db->pVTab = 0;
ds@1952
   386
  sqlite3_free(zModuleName);
ds@1952
   387
ds@1952
   388
  /* If everything went according to plan, loop through the columns
ds@1952
   389
  ** of the table to see if any of them contain the token "hidden".
ds@1952
   390
  ** If so, set the Column.isHidden flag and remove the token from
ds@1952
   391
  ** the type string.
ds@1952
   392
  */
ds@1952
   393
  if( rc==SQLITE_OK ){
ds@1952
   394
    int iCol;
ds@1952
   395
    for(iCol=0; iCol<pTab->nCol; iCol++){
ds@1952
   396
      char *zType = pTab->aCol[iCol].zType;
ds@1952
   397
      int nType;
ds@1952
   398
      int i = 0;
ds@1952
   399
      if( !zType ) continue;
ds@1952
   400
      nType = strlen(zType);
ds@1952
   401
      if( sqlite3StrNICmp("hidden", zType, 6) || (zType[6] && zType[6]!=' ') ){
ds@1952
   402
        for(i=0; i<nType; i++){
ds@1952
   403
          if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
ds@1952
   404
           && (zType[i+7]=='\0' || zType[i+7]==' ')
ds@1952
   405
          ){
ds@1952
   406
            i++;
ds@1952
   407
            break;
ds@1952
   408
          }
ds@1952
   409
        }
ds@1952
   410
      }
ds@1952
   411
      if( i<nType ){
ds@1952
   412
        int j;
ds@1952
   413
        int nDel = 6 + (zType[i+6] ? 1 : 0);
ds@1952
   414
        for(j=i; (j+nDel)<=nType; j++){
ds@1952
   415
          zType[j] = zType[j+nDel];
ds@1952
   416
        }
ds@1952
   417
        if( zType[i]=='\0' && i>0 ){
ds@1952
   418
          assert(zType[i-1]==' ');
ds@1952
   419
          zType[i-1] = '\0';
ds@1952
   420
        }
ds@1952
   421
        pTab->aCol[iCol].isHidden = 1;
ds@1952
   422
      }
ds@1952
   423
    }
ds@1952
   424
  }
ds@1952
   425
  return rc;
ds@1952
   426
}
ds@1952
   427
ds@1952
   428
/*
ds@1952
   429
** This function is invoked by the parser to call the xConnect() method
ds@1952
   430
** of the virtual table pTab. If an error occurs, an error code is returned 
ds@1952
   431
** and an error left in pParse.
ds@1952
   432
**
ds@1952
   433
** This call is a no-op if table pTab is not a virtual table.
ds@1952
   434
*/
ds@1952
   435
int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
ds@1952
   436
  Module *pMod;
ds@1952
   437
  int rc = SQLITE_OK;
ds@1952
   438
ds@1952
   439
  if( !pTab || !pTab->isVirtual || pTab->pVtab ){
ds@1952
   440
    return SQLITE_OK;
ds@1952
   441
  }
ds@1952
   442
ds@1952
   443
  pMod = pTab->pMod;
ds@1952
   444
  if( !pMod ){
ds@1952
   445
    const char *zModule = pTab->azModuleArg[0];
ds@1952
   446
    sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
ds@1952
   447
    rc = SQLITE_ERROR;
ds@1952
   448
  } else {
ds@1952
   449
    char *zErr = 0;
ds@1952
   450
    sqlite3 *db = pParse->db;
ds@1952
   451
    rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
ds@1952
   452
    if( rc!=SQLITE_OK ){
ds@1952
   453
      sqlite3ErrorMsg(pParse, "%s", zErr);
ds@1952
   454
    }
ds@1952
   455
    sqlite3_free(zErr);
ds@1952
   456
  }
ds@1952
   457
ds@1952
   458
  return rc;
ds@1952
   459
}
ds@1952
   460
ds@1952
   461
/*
ds@1952
   462
** Add the virtual table pVtab to the array sqlite3.aVTrans[].
ds@1952
   463
*/
ds@1952
   464
static int addToVTrans(sqlite3 *db, sqlite3_vtab *pVtab){
ds@1952
   465
  const int ARRAY_INCR = 5;
ds@1952
   466
ds@1952
   467
  /* Grow the sqlite3.aVTrans array if required */
ds@1952
   468
  if( (db->nVTrans%ARRAY_INCR)==0 ){
ds@1952
   469
    sqlite3_vtab **aVTrans;
ds@1952
   470
    int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
ds@1952
   471
    aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
ds@1952
   472
    if( !aVTrans ){
ds@1952
   473
      return SQLITE_NOMEM;
ds@1952
   474
    }
ds@1952
   475
    memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
ds@1952
   476
    db->aVTrans = aVTrans;
ds@1952
   477
  }
ds@1952
   478
ds@1952
   479
  /* Add pVtab to the end of sqlite3.aVTrans */
ds@1952
   480
  db->aVTrans[db->nVTrans++] = pVtab;
ds@1952
   481
  sqlite3VtabLock(pVtab);
ds@1952
   482
  return SQLITE_OK;
ds@1952
   483
}
ds@1952
   484
ds@1952
   485
/*
ds@1952
   486
** This function is invoked by the vdbe to call the xCreate method
ds@1952
   487
** of the virtual table named zTab in database iDb. 
ds@1952
   488
**
ds@1952
   489
** If an error occurs, *pzErr is set to point an an English language
ds@1952
   490
** description of the error and an SQLITE_XXX error code is returned.
ds@1952
   491
** In this case the caller must call sqlite3_free() on *pzErr.
ds@1952
   492
*/
ds@1952
   493
int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
ds@1952
   494
  int rc = SQLITE_OK;
ds@1952
   495
  Table *pTab;
ds@1952
   496
  Module *pMod;
ds@1952
   497
  const char *zModule;
ds@1952
   498
ds@1952
   499
  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
ds@1952
   500
  assert(pTab && pTab->isVirtual && !pTab->pVtab);
ds@1952
   501
  pMod = pTab->pMod;
ds@1952
   502
  zModule = pTab->azModuleArg[0];
ds@1952
   503
ds@1952
   504
  /* If the module has been registered and includes a Create method, 
ds@1952
   505
  ** invoke it now. If the module has not been registered, return an 
ds@1952
   506
  ** error. Otherwise, do nothing.
ds@1952
   507
  */
ds@1952
   508
  if( !pMod ){
ds@1952
   509
    *pzErr = sqlite3MPrintf(db, "no such module: %s", zModule);
ds@1952
   510
    rc = SQLITE_ERROR;
ds@1952
   511
  }else{
ds@1952
   512
    rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
ds@1952
   513
  }
ds@1952
   514
ds@1952
   515
  if( rc==SQLITE_OK && pTab->pVtab ){
ds@1952
   516
      rc = addToVTrans(db, pTab->pVtab);
ds@1952
   517
  }
ds@1952
   518
ds@1952
   519
  return rc;
ds@1952
   520
}
ds@1952
   521
ds@1952
   522
/*
ds@1952
   523
** This function is used to set the schema of a virtual table.  It is only
ds@1952
   524
** valid to call this function from within the xCreate() or xConnect() of a
ds@1952
   525
** virtual table module.
ds@1952
   526
*/
ds@1952
   527
int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
ds@1952
   528
  Parse sParse;
ds@1952
   529
ds@1952
   530
  int rc = SQLITE_OK;
ds@1952
   531
  Table *pTab;
ds@1952
   532
  char *zErr = 0;
ds@1952
   533
ds@1952
   534
  sqlite3_mutex_enter(db->mutex);
ds@1952
   535
  pTab = db->pVTab;
ds@1952
   536
  if( !pTab ){
ds@1952
   537
    sqlite3Error(db, SQLITE_MISUSE, 0);
ds@1952
   538
    sqlite3_mutex_leave(db->mutex);
ds@1952
   539
    return SQLITE_MISUSE;
ds@1952
   540
  }
ds@1952
   541
  assert(pTab->isVirtual && pTab->nCol==0 && pTab->aCol==0);
ds@1952
   542
ds@1952
   543
  memset(&sParse, 0, sizeof(Parse));
ds@1952
   544
  sParse.declareVtab = 1;
ds@1952
   545
  sParse.db = db;
ds@1952
   546
ds@1952
   547
  if( 
ds@1952
   548
      SQLITE_OK == sqlite3RunParser(&sParse, zCreateTable, &zErr) && 
ds@1952
   549
      sParse.pNewTable && 
ds@1952
   550
      !sParse.pNewTable->pSelect && 
ds@1952
   551
      !sParse.pNewTable->isVirtual 
ds@1952
   552
  ){
ds@1952
   553
    pTab->aCol = sParse.pNewTable->aCol;
ds@1952
   554
    pTab->nCol = sParse.pNewTable->nCol;
ds@1952
   555
    sParse.pNewTable->nCol = 0;
ds@1952
   556
    sParse.pNewTable->aCol = 0;
ds@1952
   557
    db->pVTab = 0;
ds@1952
   558
  } else {
ds@1952
   559
    sqlite3Error(db, SQLITE_ERROR, zErr);
ds@1952
   560
    sqlite3_free(zErr);
ds@1952
   561
    rc = SQLITE_ERROR;
ds@1952
   562
  }
ds@1952
   563
  sParse.declareVtab = 0;
ds@1952
   564
ds@1952
   565
  sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
ds@1952
   566
  sqlite3DeleteTable(sParse.pNewTable);
ds@1952
   567
  sParse.pNewTable = 0;
ds@1952
   568
ds@1952
   569
  assert( (rc&0xff)==rc );
ds@1952
   570
  rc = sqlite3ApiExit(db, rc);
ds@1952
   571
  sqlite3_mutex_leave(db->mutex);
ds@1952
   572
  return rc;
ds@1952
   573
}
ds@1952
   574
ds@1952
   575
/*
ds@1952
   576
** This function is invoked by the vdbe to call the xDestroy method
ds@1952
   577
** of the virtual table named zTab in database iDb. This occurs
ds@1952
   578
** when a DROP TABLE is mentioned.
ds@1952
   579
**
ds@1952
   580
** This call is a no-op if zTab is not a virtual table.
ds@1952
   581
*/
ds@1952
   582
int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab)
ds@1952
   583
{
ds@1952
   584
  int rc = SQLITE_OK;
ds@1952
   585
  Table *pTab;
ds@1952
   586
ds@1952
   587
  pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
ds@1952
   588
  assert(pTab);
ds@1952
   589
  if( pTab->pVtab ){
ds@1952
   590
    int (*xDestroy)(sqlite3_vtab *pVTab) = pTab->pMod->pModule->xDestroy;
ds@1952
   591
    rc = sqlite3SafetyOff(db);
ds@1952
   592
    assert( rc==SQLITE_OK );
ds@1952
   593
    if( xDestroy ){
ds@1952
   594
      rc = xDestroy(pTab->pVtab);
ds@1952
   595
    }
ds@1952
   596
    sqlite3SafetyOn(db);
ds@1952
   597
    if( rc==SQLITE_OK ){
ds@1952
   598
      pTab->pVtab = 0;
ds@1952
   599
    }
ds@1952
   600
  }
ds@1952
   601
ds@1952
   602
  return rc;
ds@1952
   603
}
ds@1952
   604
ds@1952
   605
/*
ds@1952
   606
** This function invokes either the xRollback or xCommit method
ds@1952
   607
** of each of the virtual tables in the sqlite3.aVTrans array. The method
ds@1952
   608
** called is identified by the second argument, "offset", which is
ds@1952
   609
** the offset of the method to call in the sqlite3_module structure.
ds@1952
   610
**
ds@1952
   611
** The array is cleared after invoking the callbacks. 
ds@1952
   612
*/
ds@1952
   613
static void callFinaliser(sqlite3 *db, int offset){
ds@1952
   614
  int i;
ds@1952
   615
  if( db->aVTrans ){
ds@1952
   616
    for(i=0; i<db->nVTrans && db->aVTrans[i]; i++){
ds@1952
   617
      sqlite3_vtab *pVtab = db->aVTrans[i];
ds@1952
   618
      int (*x)(sqlite3_vtab *);
ds@1952
   619
      x = *(int (**)(sqlite3_vtab *))((char *)pVtab->pModule + offset);
ds@1952
   620
      if( x ) x(pVtab);
ds@1952
   621
      sqlite3VtabUnlock(db, pVtab);
ds@1952
   622
    }
ds@1952
   623
    sqlite3_free(db->aVTrans);
ds@1952
   624
    db->nVTrans = 0;
ds@1952
   625
    db->aVTrans = 0;
ds@1952
   626
  }
ds@1952
   627
}
ds@1952
   628
ds@1952
   629
/*
ds@1952
   630
** If argument rc2 is not SQLITE_OK, then return it and do nothing. 
ds@1952
   631
** Otherwise, invoke the xSync method of all virtual tables in the 
ds@1952
   632
** sqlite3.aVTrans array. Return the error code for the first error 
ds@1952
   633
** that occurs, or SQLITE_OK if all xSync operations are successful.
ds@1952
   634
*/
ds@1952
   635
int sqlite3VtabSync(sqlite3 *db, int rc2){
ds@1952
   636
  int i;
ds@1952
   637
  int rc = SQLITE_OK;
ds@1952
   638
  int rcsafety;
ds@1952
   639
  sqlite3_vtab **aVTrans = db->aVTrans;
ds@1952
   640
  if( rc2!=SQLITE_OK ) return rc2;
ds@1952
   641
ds@1952
   642
  rc = sqlite3SafetyOff(db);
ds@1952
   643
  db->aVTrans = 0;
ds@1952
   644
  for(i=0; rc==SQLITE_OK && i<db->nVTrans && aVTrans[i]; i++){
ds@1952
   645
    sqlite3_vtab *pVtab = aVTrans[i];
ds@1952
   646
    int (*x)(sqlite3_vtab *);
ds@1952
   647
    x = pVtab->pModule->xSync;
ds@1952
   648
    if( x ){
ds@1952
   649
      rc = x(pVtab);
ds@1952
   650
    }
ds@1952
   651
  }
ds@1952
   652
  db->aVTrans = aVTrans;
ds@1952
   653
  rcsafety = sqlite3SafetyOn(db);
ds@1952
   654
ds@1952
   655
  if( rc==SQLITE_OK ){
ds@1952
   656
    rc = rcsafety;
ds@1952
   657
  }
ds@1952
   658
  return rc;
ds@1952
   659
}
ds@1952
   660
ds@1952
   661
/*
ds@1952
   662
** Invoke the xRollback method of all virtual tables in the 
ds@1952
   663
** sqlite3.aVTrans array. Then clear the array itself.
ds@1952
   664
*/
ds@1952
   665
int sqlite3VtabRollback(sqlite3 *db){
ds@1952
   666
  callFinaliser(db, (int)(&((sqlite3_module *)0)->xRollback));
ds@1952
   667
  return SQLITE_OK;
ds@1952
   668
}
ds@1952
   669
ds@1952
   670
/*
ds@1952
   671
** Invoke the xCommit method of all virtual tables in the 
ds@1952
   672
** sqlite3.aVTrans array. Then clear the array itself.
ds@1952
   673
*/
ds@1952
   674
int sqlite3VtabCommit(sqlite3 *db){
ds@1952
   675
  callFinaliser(db, (int)(&((sqlite3_module *)0)->xCommit));
ds@1952
   676
  return SQLITE_OK;
ds@1952
   677
}
ds@1952
   678
ds@1952
   679
/*
ds@1952
   680
** If the virtual table pVtab supports the transaction interface
ds@1952
   681
** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
ds@1952
   682
** not currently open, invoke the xBegin method now.
ds@1952
   683
**
ds@1952
   684
** If the xBegin call is successful, place the sqlite3_vtab pointer
ds@1952
   685
** in the sqlite3.aVTrans array.
ds@1952
   686
*/
ds@1952
   687
int sqlite3VtabBegin(sqlite3 *db, sqlite3_vtab *pVtab){
ds@1952
   688
  int rc = SQLITE_OK;
ds@1952
   689
  const sqlite3_module *pModule;
ds@1952
   690
ds@1952
   691
  /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
ds@1952
   692
  ** than zero, then this function is being called from within a
ds@1952
   693
  ** virtual module xSync() callback. It is illegal to write to 
ds@1952
   694
  ** virtual module tables in this case, so return SQLITE_LOCKED.
ds@1952
   695
  */
ds@1952
   696
  if( 0==db->aVTrans && db->nVTrans>0 ){
ds@1952
   697
    return SQLITE_LOCKED;
ds@1952
   698
  }
ds@1952
   699
  if( !pVtab ){
ds@1952
   700
    return SQLITE_OK;
ds@1952
   701
  } 
ds@1952
   702
  pModule = pVtab->pModule;
ds@1952
   703
ds@1952
   704
  if( pModule->xBegin ){
ds@1952
   705
    int i;
ds@1952
   706
ds@1952
   707
ds@1952
   708
    /* If pVtab is already in the aVTrans array, return early */
ds@1952
   709
    for(i=0; (i<db->nVTrans) && 0!=db->aVTrans[i]; i++){
ds@1952
   710
      if( db->aVTrans[i]==pVtab ){
ds@1952
   711
        return SQLITE_OK;
ds@1952
   712
      }
ds@1952
   713
    }
ds@1952
   714
ds@1952
   715
    /* Invoke the xBegin method */
ds@1952
   716
    rc = pModule->xBegin(pVtab);
ds@1952
   717
    if( rc!=SQLITE_OK ){
ds@1952
   718
      return rc;
ds@1952
   719
    }
ds@1952
   720
ds@1952
   721
    rc = addToVTrans(db, pVtab);
ds@1952
   722
  }
ds@1952
   723
  return rc;
ds@1952
   724
}
ds@1952
   725
ds@1952
   726
/*
ds@1952
   727
** The first parameter (pDef) is a function implementation.  The
ds@1952
   728
** second parameter (pExpr) is the first argument to this function.
ds@1952
   729
** If pExpr is a column in a virtual table, then let the virtual
ds@1952
   730
** table implementation have an opportunity to overload the function.
ds@1952
   731
**
ds@1952
   732
** This routine is used to allow virtual table implementations to
ds@1952
   733
** overload MATCH, LIKE, GLOB, and REGEXP operators.
ds@1952
   734
**
ds@1952
   735
** Return either the pDef argument (indicating no change) or a 
ds@1952
   736
** new FuncDef structure that is marked as ephemeral using the
ds@1952
   737
** SQLITE_FUNC_EPHEM flag.
ds@1952
   738
*/
ds@1952
   739
FuncDef *sqlite3VtabOverloadFunction(
ds@1952
   740
  sqlite3 *db,    /* Database connection for reporting malloc problems */
ds@1952
   741
  FuncDef *pDef,  /* Function to possibly overload */
ds@1952
   742
  int nArg,       /* Number of arguments to the function */
ds@1952
   743
  Expr *pExpr     /* First argument to the function */
ds@1952
   744
){
ds@1952
   745
  Table *pTab;
ds@1952
   746
  sqlite3_vtab *pVtab;
ds@1952
   747
  sqlite3_module *pMod;
ds@1952
   748
  void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
ds@1952
   749
  void *pArg;
ds@1952
   750
  FuncDef *pNew;
ds@1952
   751
  int rc = 0;
ds@1952
   752
  char *zLowerName;
ds@1952
   753
  unsigned char *z;
ds@1952
   754
ds@1952
   755
ds@1952
   756
  /* Check to see the left operand is a column in a virtual table */
ds@1952
   757
  if( pExpr==0 ) return pDef;
ds@1952
   758
  if( pExpr->op!=TK_COLUMN ) return pDef;
ds@1952
   759
  pTab = pExpr->pTab;
ds@1952
   760
  if( pTab==0 ) return pDef;
ds@1952
   761
  if( !pTab->isVirtual ) return pDef;
ds@1952
   762
  pVtab = pTab->pVtab;
ds@1952
   763
  assert( pVtab!=0 );
ds@1952
   764
  assert( pVtab->pModule!=0 );
ds@1952
   765
  pMod = (sqlite3_module *)pVtab->pModule;
ds@1952
   766
  if( pMod->xFindFunction==0 ) return pDef;
ds@1952
   767
 
ds@1952
   768
  /* Call the xFindFunction method on the virtual table implementation
ds@1952
   769
  ** to see if the implementation wants to overload this function 
ds@1952
   770
  */
ds@1952
   771
  zLowerName = sqlite3DbStrDup(db, pDef->zName);
ds@1952
   772
  if( zLowerName ){
ds@1952
   773
    for(z=(unsigned char*)zLowerName; *z; z++){
ds@1952
   774
      *z = sqlite3UpperToLower[*z];
ds@1952
   775
    }
ds@1952
   776
    rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
ds@1952
   777
    sqlite3_free(zLowerName);
ds@1952
   778
  }
ds@1952
   779
  if( rc==0 ){
ds@1952
   780
    return pDef;
ds@1952
   781
  }
ds@1952
   782
ds@1952
   783
  /* Create a new ephemeral function definition for the overloaded
ds@1952
   784
  ** function */
ds@1952
   785
  pNew = sqlite3DbMallocZero(db, sizeof(*pNew) + strlen(pDef->zName) );
ds@1952
   786
  if( pNew==0 ){
ds@1952
   787
    return pDef;
ds@1952
   788
  }
ds@1952
   789
  *pNew = *pDef;
ds@1952
   790
  memcpy(pNew->zName, pDef->zName, strlen(pDef->zName)+1);
ds@1952
   791
  pNew->xFunc = xFunc;
ds@1952
   792
  pNew->pUserData = pArg;
ds@1952
   793
  pNew->flags |= SQLITE_FUNC_EPHEM;
ds@1952
   794
  return pNew;
ds@1952
   795
}
ds@1952
   796
ds@1952
   797
#endif /* SQLITE_OMIT_VIRTUALTABLE */