This source file includes following definitions.
- simulateVtabError
- dequoteString
- getColumnNames
- getIndexArray
- appendToEchoModule
- echoDeclareVtab
- echoDestructor
- echoConstructor
- echoCreate
- echoConnect
- echoDisconnect
- echoDestroy
- echoOpen
- echoClose
- echoEof
- echoNext
- echoColumn
- echoRowid
- hashString
- echoFilter
- string_concat
- echoBestIndex
- echoUpdate
- echoTransactionCall
- echoBegin
- echoSync
- echoCommit
- echoRollback
- overloadedGlobFunction
- echoFindFunction
- echoRename
- moduleDestroy
- register_echo_module
- declare_vtab
- Sqlitetest8_Init
#include "sqliteInt.h"
#include "tcl.h"
#include <stdlib.h>
#include <string.h>
#ifndef SQLITE_OMIT_VIRTUALTABLE
typedef struct echo_vtab echo_vtab;
typedef struct echo_cursor echo_cursor;
struct echo_vtab {
sqlite3_vtab base;
Tcl_Interp *interp;
sqlite3 *db;
int isPattern;
int inTransaction;
char *zThis;
char *zTableName;
char *zLogName;
int nCol;
int *aIndex;
char **aCol;
};
struct echo_cursor {
sqlite3_vtab_cursor base;
sqlite3_stmt *pStmt;
};
static int simulateVtabError(echo_vtab *p, const char *zMethod){
const char *zErr;
char zVarname[128];
zVarname[127] = '\0';
sqlite3_snprintf(127, zVarname, "echo_module_fail(%s,%s)", zMethod, p->zTableName);
zErr = Tcl_GetVar(p->interp, zVarname, TCL_GLOBAL_ONLY);
if( zErr ){
p->base.zErrMsg = sqlite3_mprintf("echo-vtab-error: %s", zErr);
}
return (zErr!=0);
}
static void dequoteString(char *z){
int quote;
int i, j;
if( z==0 ) return;
quote = z[0];
switch( quote ){
case '\'': break;
case '"': break;
case '`': break;
case '[': quote = ']'; break;
default: return;
}
for(i=1, j=0; z[i]; i++){
if( z[i]==quote ){
if( z[i+1]==quote ){
z[j++] = quote;
i++;
}else{
z[j++] = 0;
break;
}
}else{
z[j++] = z[i];
}
}
}
static int getColumnNames(
sqlite3 *db,
const char *zTab,
char ***paCol,
int *pnCol
){
char **aCol = 0;
char *zSql;
sqlite3_stmt *pStmt = 0;
int rc = SQLITE_OK;
int nCol = 0;
zSql = sqlite3_mprintf("SELECT * FROM %Q", zTab);
if( !zSql ){
rc = SQLITE_NOMEM;
goto out;
}
rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
sqlite3_free(zSql);
if( rc==SQLITE_OK ){
int ii;
int nBytes;
char *zSpace;
nCol = sqlite3_column_count(pStmt);
nBytes = sizeof(char *) * nCol;
for(ii=0; ii<nCol; ii++){
const char *zName = sqlite3_column_name(pStmt, ii);
if( !zName ){
rc = SQLITE_NOMEM;
goto out;
}
nBytes += strlen(zName)+1;
}
aCol = (char **)sqlite3MallocZero(nBytes);
if( !aCol ){
rc = SQLITE_NOMEM;
goto out;
}
zSpace = (char *)(&aCol[nCol]);
for(ii=0; ii<nCol; ii++){
aCol[ii] = zSpace;
zSpace += sprintf(zSpace, "%s", sqlite3_column_name(pStmt, ii));
zSpace++;
}
assert( (zSpace-nBytes)==(char *)aCol );
}
*paCol = aCol;
*pnCol = nCol;
out:
sqlite3_finalize(pStmt);
return rc;
}
static int getIndexArray(
sqlite3 *db,
const char *zTab,
int nCol,
int **paIndex
){
sqlite3_stmt *pStmt = 0;
int *aIndex = 0;
int rc;
char *zSql;
aIndex = (int *)sqlite3MallocZero(sizeof(int) * nCol);
if( !aIndex ){
rc = SQLITE_NOMEM;
goto get_index_array_out;
}
zSql = sqlite3_mprintf("PRAGMA index_list(%s)", zTab);
if( !zSql ){
rc = SQLITE_NOMEM;
goto get_index_array_out;
}
rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
sqlite3_free(zSql);
while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
const char *zIdx = (const char *)sqlite3_column_text(pStmt, 1);
sqlite3_stmt *pStmt2 = 0;
zSql = sqlite3_mprintf("PRAGMA index_info(%s)", zIdx);
if( !zSql ){
rc = SQLITE_NOMEM;
goto get_index_array_out;
}
rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0);
sqlite3_free(zSql);
if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){
int cid = sqlite3_column_int(pStmt2, 1);
assert( cid>=0 && cid<nCol );
aIndex[cid] = 1;
}
if( pStmt2 ){
rc = sqlite3_finalize(pStmt2);
}
if( rc!=SQLITE_OK ){
goto get_index_array_out;
}
}
get_index_array_out:
if( pStmt ){
int rc2 = sqlite3_finalize(pStmt);
if( rc==SQLITE_OK ){
rc = rc2;
}
}
if( rc!=SQLITE_OK ){
sqlite3_free(aIndex);
aIndex = 0;
}
*paIndex = aIndex;
return rc;
}
static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags);
}
static int echoDeclareVtab(
echo_vtab *pVtab,
sqlite3 *db
){
int rc = SQLITE_OK;
if( pVtab->zTableName ){
sqlite3_stmt *pStmt = 0;
rc = sqlite3_prepare(db,
"SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
-1, &pStmt, 0);
if( rc==SQLITE_OK ){
sqlite3_bind_text(pStmt, 1, pVtab->zTableName, -1, 0);
if( sqlite3_step(pStmt)==SQLITE_ROW ){
int rc2;
const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0);
rc = sqlite3_declare_vtab(db, zCreateTable);
rc2 = sqlite3_finalize(pStmt);
if( rc==SQLITE_OK ){
rc = rc2;
}
} else {
rc = sqlite3_finalize(pStmt);
if( rc==SQLITE_OK ){
rc = SQLITE_ERROR;
}
}
if( rc==SQLITE_OK ){
rc = getColumnNames(db, pVtab->zTableName, &pVtab->aCol, &pVtab->nCol);
}
if( rc==SQLITE_OK ){
rc = getIndexArray(db, pVtab->zTableName, pVtab->nCol, &pVtab->aIndex);
}
}
}
return rc;
}
static int echoDestructor(sqlite3_vtab *pVtab){
echo_vtab *p = (echo_vtab*)pVtab;
sqlite3_free(p->aIndex);
sqlite3_free(p->aCol);
sqlite3_free(p->zThis);
sqlite3_free(p->zTableName);
sqlite3_free(p->zLogName);
sqlite3_free(p);
return 0;
}
typedef struct EchoModule EchoModule;
struct EchoModule {
Tcl_Interp *interp;
};
static int echoConstructor(
sqlite3 *db,
void *pAux,
int argc, const char *const*argv,
sqlite3_vtab **ppVtab,
char **pzErr
){
int rc;
int i;
echo_vtab *pVtab;
pVtab = sqlite3MallocZero( sizeof(*pVtab) );
if( !pVtab ){
return SQLITE_NOMEM;
}
pVtab->interp = ((EchoModule *)pAux)->interp;
pVtab->db = db;
pVtab->zThis = sqlite3_mprintf("%s", argv[2]);
if( !pVtab->zThis ){
echoDestructor((sqlite3_vtab *)pVtab);
return SQLITE_NOMEM;
}
if( argc>3 ){
pVtab->zTableName = sqlite3_mprintf("%s", argv[3]);
dequoteString(pVtab->zTableName);
if( pVtab->zTableName && pVtab->zTableName[0]=='*' ){
char *z = sqlite3_mprintf("%s%s", argv[2], &(pVtab->zTableName[1]));
sqlite3_free(pVtab->zTableName);
pVtab->zTableName = z;
pVtab->isPattern = 1;
}
if( !pVtab->zTableName ){
echoDestructor((sqlite3_vtab *)pVtab);
return SQLITE_NOMEM;
}
}
for(i=0; i<argc; i++){
appendToEchoModule(pVtab->interp, argv[i]);
}
rc = echoDeclareVtab(pVtab, db);
if( rc!=SQLITE_OK ){
echoDestructor((sqlite3_vtab *)pVtab);
return rc;
}
*ppVtab = &pVtab->base;
return SQLITE_OK;
}
static int echoCreate(
sqlite3 *db,
void *pAux,
int argc, const char *const*argv,
sqlite3_vtab **ppVtab,
char **pzErr
){
int rc = SQLITE_OK;
appendToEchoModule(((EchoModule *)pAux)->interp, "xCreate");
rc = echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
if( rc==SQLITE_OK && argc==5 ){
char *zSql;
echo_vtab *pVtab = *(echo_vtab **)ppVtab;
pVtab->zLogName = sqlite3_mprintf("%s", argv[4]);
zSql = sqlite3_mprintf("CREATE TABLE %Q(logmsg)", pVtab->zLogName);
rc = sqlite3_exec(db, zSql, 0, 0, 0);
sqlite3_free(zSql);
if( rc!=SQLITE_OK ){
*pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
}
}
if( *ppVtab && rc!=SQLITE_OK ){
echoDestructor(*ppVtab);
*ppVtab = 0;
}
if( rc==SQLITE_OK ){
(*(echo_vtab**)ppVtab)->inTransaction = 1;
}
return rc;
}
static int echoConnect(
sqlite3 *db,
void *pAux,
int argc, const char *const*argv,
sqlite3_vtab **ppVtab,
char **pzErr
){
appendToEchoModule(((EchoModule *)pAux)->interp, "xConnect");
return echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
}
static int echoDisconnect(sqlite3_vtab *pVtab){
appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
return echoDestructor(pVtab);
}
static int echoDestroy(sqlite3_vtab *pVtab){
int rc = SQLITE_OK;
echo_vtab *p = (echo_vtab *)pVtab;
appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
if( p && p->zLogName ){
char *zSql;
zSql = sqlite3_mprintf("DROP TABLE %Q", p->zLogName);
rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
sqlite3_free(zSql);
}
if( rc==SQLITE_OK ){
rc = echoDestructor(pVtab);
}
return rc;
}
static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
echo_cursor *pCur;
if( simulateVtabError((echo_vtab *)pVTab, "xOpen") ){
return SQLITE_ERROR;
}
pCur = sqlite3MallocZero(sizeof(echo_cursor));
*ppCursor = (sqlite3_vtab_cursor *)pCur;
return (pCur ? SQLITE_OK : SQLITE_NOMEM);
}
static int echoClose(sqlite3_vtab_cursor *cur){
int rc;
echo_cursor *pCur = (echo_cursor *)cur;
sqlite3_stmt *pStmt = pCur->pStmt;
pCur->pStmt = 0;
sqlite3_free(pCur);
rc = sqlite3_finalize(pStmt);
return rc;
}
static int echoEof(sqlite3_vtab_cursor *cur){
return (((echo_cursor *)cur)->pStmt ? 0 : 1);
}
static int echoNext(sqlite3_vtab_cursor *cur){
int rc = SQLITE_OK;
echo_cursor *pCur = (echo_cursor *)cur;
if( simulateVtabError((echo_vtab *)(cur->pVtab), "xNext") ){
return SQLITE_ERROR;
}
if( pCur->pStmt ){
rc = sqlite3_step(pCur->pStmt);
if( rc==SQLITE_ROW ){
rc = SQLITE_OK;
}else{
rc = sqlite3_finalize(pCur->pStmt);
pCur->pStmt = 0;
}
}
return rc;
}
static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
int iCol = i + 1;
sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
if( simulateVtabError((echo_vtab *)(cur->pVtab), "xColumn") ){
return SQLITE_ERROR;
}
if( !pStmt ){
sqlite3_result_null(ctx);
}else{
assert( sqlite3_data_count(pStmt)>iCol );
sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
}
return SQLITE_OK;
}
static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
if( simulateVtabError((echo_vtab *)(cur->pVtab), "xRowid") ){
return SQLITE_ERROR;
}
*pRowid = sqlite3_column_int64(pStmt, 0);
return SQLITE_OK;
}
static int hashString(const char *zString){
int val = 0;
int ii;
for(ii=0; zString[ii]; ii++){
val = (val << 3) + (int)zString[ii];
}
return val;
}
static int echoFilter(
sqlite3_vtab_cursor *pVtabCursor,
int idxNum, const char *idxStr,
int argc, sqlite3_value **argv
){
int rc;
int i;
echo_cursor *pCur = (echo_cursor *)pVtabCursor;
echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
sqlite3 *db = pVtab->db;
if( simulateVtabError(pVtab, "xFilter") ){
return SQLITE_ERROR;
}
assert( idxNum==hashString(idxStr) );
appendToEchoModule(pVtab->interp, "xFilter");
appendToEchoModule(pVtab->interp, idxStr);
for(i=0; i<argc; i++){
appendToEchoModule(pVtab->interp, (const char*)sqlite3_value_text(argv[i]));
}
sqlite3_finalize(pCur->pStmt);
pCur->pStmt = 0;
rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
assert( pCur->pStmt || rc!=SQLITE_OK );
for(i=0; rc==SQLITE_OK && i<argc; i++){
rc = sqlite3_bind_value(pCur->pStmt, i+1, argv[i]);
}
if( rc==SQLITE_OK ){
rc = echoNext(pVtabCursor);
}
return rc;
}
static void string_concat(char **pzStr, char *zAppend, int doFree, int *pRc){
char *zIn = *pzStr;
if( !zAppend && doFree && *pRc==SQLITE_OK ){
*pRc = SQLITE_NOMEM;
}
if( *pRc!=SQLITE_OK ){
sqlite3_free(zIn);
zIn = 0;
}else{
if( zIn ){
char *zTemp = zIn;
zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
sqlite3_free(zTemp);
}else{
zIn = sqlite3_mprintf("%s", zAppend);
}
if( !zIn ){
*pRc = SQLITE_NOMEM;
}
}
*pzStr = zIn;
if( doFree ){
sqlite3_free(zAppend);
}
}
static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
int ii;
char *zQuery = 0;
char *zNew;
int nArg = 0;
const char *zSep = "WHERE";
echo_vtab *pVtab = (echo_vtab *)tab;
sqlite3_stmt *pStmt = 0;
Tcl_Interp *interp = pVtab->interp;
int nRow;
int useIdx = 0;
int rc = SQLITE_OK;
int useCost = 0;
double cost;
int isIgnoreUsable = 0;
if( Tcl_GetVar(interp, "echo_module_ignore_usable", TCL_GLOBAL_ONLY) ){
isIgnoreUsable = 1;
}
if( simulateVtabError(pVtab, "xBestIndex") ){
return SQLITE_ERROR;
}
if( Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY) ){
cost = atof(Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY));
useCost = 1;
} else {
zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
if( !zQuery ){
return SQLITE_NOMEM;
}
rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
sqlite3_free(zQuery);
if( rc!=SQLITE_OK ){
return rc;
}
sqlite3_step(pStmt);
nRow = sqlite3_column_int(pStmt, 0);
rc = sqlite3_finalize(pStmt);
if( rc!=SQLITE_OK ){
return rc;
}
}
zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
if( !zQuery ){
return SQLITE_NOMEM;
}
for(ii=0; ii<pIdxInfo->nConstraint; ii++){
const struct sqlite3_index_constraint *pConstraint;
struct sqlite3_index_constraint_usage *pUsage;
int iCol;
pConstraint = &pIdxInfo->aConstraint[ii];
pUsage = &pIdxInfo->aConstraintUsage[ii];
if( !isIgnoreUsable && !pConstraint->usable ) continue;
iCol = pConstraint->iColumn;
if( pVtab->aIndex[iCol] || iCol<0 ){
char *zCol = pVtab->aCol[iCol];
char *zOp = 0;
useIdx = 1;
if( iCol<0 ){
zCol = "rowid";
}
switch( pConstraint->op ){
case SQLITE_INDEX_CONSTRAINT_EQ:
zOp = "="; break;
case SQLITE_INDEX_CONSTRAINT_LT:
zOp = "<"; break;
case SQLITE_INDEX_CONSTRAINT_GT:
zOp = ">"; break;
case SQLITE_INDEX_CONSTRAINT_LE:
zOp = "<="; break;
case SQLITE_INDEX_CONSTRAINT_GE:
zOp = ">="; break;
case SQLITE_INDEX_CONSTRAINT_MATCH:
zOp = "LIKE"; break;
}
if( zOp[0]=='L' ){
zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')",
zSep, zCol);
} else {
zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp);
}
string_concat(&zQuery, zNew, 1, &rc);
zSep = "AND";
pUsage->argvIndex = ++nArg;
pUsage->omit = 1;
}
}
if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
int iCol = pIdxInfo->aOrderBy->iColumn;
char *zCol = pVtab->aCol[iCol];
char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
if( iCol<0 ){
zCol = "rowid";
}
zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir);
string_concat(&zQuery, zNew, 1, &rc);
pIdxInfo->orderByConsumed = 1;
}
appendToEchoModule(pVtab->interp, "xBestIndex");;
appendToEchoModule(pVtab->interp, zQuery);
if( !zQuery ){
return rc;
}
pIdxInfo->idxNum = hashString(zQuery);
pIdxInfo->idxStr = zQuery;
pIdxInfo->needToFreeIdxStr = 1;
if( useCost ){
pIdxInfo->estimatedCost = cost;
}else if( useIdx ){
for( ii=0; ii<(sizeof(int)*8); ii++ ){
if( nRow & (1<<ii) ){
pIdxInfo->estimatedCost = (double)ii;
}
}
}else{
pIdxInfo->estimatedCost = (double)nRow;
}
return rc;
}
int echoUpdate(
sqlite3_vtab *tab,
int nData,
sqlite3_value **apData,
sqlite_int64 *pRowid
){
echo_vtab *pVtab = (echo_vtab *)tab;
sqlite3 *db = pVtab->db;
int rc = SQLITE_OK;
sqlite3_stmt *pStmt;
char *z = 0;
int bindArgZero = 0;
int bindArgOne = 0;
int i;
assert( nData==pVtab->nCol+2 || nData==1 );
assert( pVtab->inTransaction );
if( simulateVtabError(pVtab, "xUpdate") ){
return SQLITE_ERROR;
}
if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
char *zSep = " SET";
z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
if( !z ){
rc = SQLITE_NOMEM;
}
bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
bindArgZero = 1;
if( bindArgOne ){
string_concat(&z, " SET rowid=?1 ", 0, &rc);
zSep = ",";
}
for(i=2; i<nData; i++){
if( apData[i]==0 ) continue;
string_concat(&z, sqlite3_mprintf(
"%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1, &rc);
zSep = ",";
}
string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 1, &rc);
}
else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
if( !z ){
rc = SQLITE_NOMEM;
}
bindArgZero = 1;
}
else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
int ii;
char *zInsert = 0;
char *zValues = 0;
zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
if( !zInsert ){
rc = SQLITE_NOMEM;
}
if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
bindArgOne = 1;
zValues = sqlite3_mprintf("?");
string_concat(&zInsert, "rowid", 0, &rc);
}
assert((pVtab->nCol+2)==nData);
for(ii=2; ii<nData; ii++){
string_concat(&zInsert,
sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1, &rc);
string_concat(&zValues,
sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1, &rc);
}
string_concat(&z, zInsert, 1, &rc);
string_concat(&z, ") VALUES(", 0, &rc);
string_concat(&z, zValues, 1, &rc);
string_concat(&z, ")", 0, &rc);
}
else{
assert(0);
return SQLITE_ERROR;
}
if( rc==SQLITE_OK ){
rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
}
assert( rc!=SQLITE_OK || pStmt );
sqlite3_free(z);
if( rc==SQLITE_OK ) {
if( bindArgZero ){
sqlite3_bind_value(pStmt, nData, apData[0]);
}
if( bindArgOne ){
sqlite3_bind_value(pStmt, 1, apData[1]);
}
for(i=2; i<nData && rc==SQLITE_OK; i++){
if( apData[i] ) rc = sqlite3_bind_value(pStmt, i, apData[i]);
}
if( rc==SQLITE_OK ){
sqlite3_step(pStmt);
rc = sqlite3_finalize(pStmt);
}else{
sqlite3_finalize(pStmt);
}
}
if( pRowid && rc==SQLITE_OK ){
*pRowid = sqlite3_last_insert_rowid(db);
}
if( rc!=SQLITE_OK ){
tab->zErrMsg = sqlite3_mprintf("echo-vtab-error: %s", sqlite3_errmsg(db));
}
return rc;
}
static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
char *z;
echo_vtab *pVtab = (echo_vtab *)tab;
z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
if( z==0 ) return SQLITE_NOMEM;
appendToEchoModule(pVtab->interp, zCall);
appendToEchoModule(pVtab->interp, z);
sqlite3_free(z);
return SQLITE_OK;
}
static int echoBegin(sqlite3_vtab *tab){
int rc;
echo_vtab *pVtab = (echo_vtab *)tab;
Tcl_Interp *interp = pVtab->interp;
const char *zVal;
assert( !pVtab->inTransaction );
if( simulateVtabError(pVtab, "xBegin") ){
return SQLITE_ERROR;
}
rc = echoTransactionCall(tab, "xBegin");
if( rc==SQLITE_OK ){
zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
rc = SQLITE_ERROR;
}
}
if( rc==SQLITE_OK ){
pVtab->inTransaction = 1;
}
return rc;
}
static int echoSync(sqlite3_vtab *tab){
int rc;
echo_vtab *pVtab = (echo_vtab *)tab;
Tcl_Interp *interp = pVtab->interp;
const char *zVal;
assert( pVtab->inTransaction );
if( simulateVtabError(pVtab, "xSync") ){
return SQLITE_ERROR;
}
rc = echoTransactionCall(tab, "xSync");
if( rc==SQLITE_OK ){
zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
rc = -1;
}
}
return rc;
}
static int echoCommit(sqlite3_vtab *tab){
echo_vtab *pVtab = (echo_vtab*)tab;
int rc;
assert( pVtab->inTransaction );
if( simulateVtabError(pVtab, "xCommit") ){
return SQLITE_ERROR;
}
sqlite3BeginBenignMalloc();
rc = echoTransactionCall(tab, "xCommit");
sqlite3EndBenignMalloc();
pVtab->inTransaction = 0;
return rc;
}
static int echoRollback(sqlite3_vtab *tab){
int rc;
echo_vtab *pVtab = (echo_vtab*)tab;
assert( pVtab->inTransaction );
rc = echoTransactionCall(tab, "xRollback");
pVtab->inTransaction = 0;
return rc;
}
static void overloadedGlobFunction(
sqlite3_context *pContext,
int nArg,
sqlite3_value **apArg
){
Tcl_Interp *interp = sqlite3_user_data(pContext);
Tcl_DString str;
int i;
int rc;
Tcl_DStringInit(&str);
Tcl_DStringAppendElement(&str, "::echo_glob_overload");
for(i=0; i<nArg; i++){
Tcl_DStringAppendElement(&str, (char*)sqlite3_value_text(apArg[i]));
}
rc = Tcl_Eval(interp, Tcl_DStringValue(&str));
Tcl_DStringFree(&str);
if( rc ){
sqlite3_result_error(pContext, Tcl_GetStringResult(interp), -1);
}else{
sqlite3_result_text(pContext, Tcl_GetStringResult(interp),
-1, SQLITE_TRANSIENT);
}
Tcl_ResetResult(interp);
}
static int echoFindFunction(
sqlite3_vtab *vtab,
int nArg,
const char *zFuncName,
void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
void **ppArg
){
echo_vtab *pVtab = (echo_vtab *)vtab;
Tcl_Interp *interp = pVtab->interp;
Tcl_CmdInfo info;
if( strcmp(zFuncName,"glob")!=0 ){
return 0;
}
if( Tcl_GetCommandInfo(interp, "::echo_glob_overload", &info)==0 ){
return 0;
}
*pxFunc = overloadedGlobFunction;
*ppArg = interp;
return 1;
}
static int echoRename(sqlite3_vtab *vtab, const char *zNewName){
int rc = SQLITE_OK;
echo_vtab *p = (echo_vtab *)vtab;
if( simulateVtabError(p, "xRename") ){
return SQLITE_ERROR;
}
if( p->isPattern ){
int nThis = strlen(p->zThis);
char *zSql = sqlite3_mprintf("ALTER TABLE %s RENAME TO %s%s",
p->zTableName, zNewName, &p->zTableName[nThis]
);
rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
sqlite3_free(zSql);
}
return rc;
}
static sqlite3_module echoModule = {
0,
echoCreate,
echoConnect,
echoBestIndex,
echoDisconnect,
echoDestroy,
echoOpen,
echoClose,
echoFilter,
echoNext,
echoEof,
echoColumn,
echoRowid,
echoUpdate,
echoBegin,
echoSync,
echoCommit,
echoRollback,
echoFindFunction,
echoRename,
};
extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
static void moduleDestroy(void *p){
sqlite3_free(p);
}
static int register_echo_module(
ClientData clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
sqlite3 *db;
EchoModule *pMod;
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB");
return TCL_ERROR;
}
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
pMod = sqlite3_malloc(sizeof(EchoModule));
pMod->interp = interp;
sqlite3_create_module_v2(db, "echo", &echoModule, (void*)pMod, moduleDestroy);
return TCL_OK;
}
static int declare_vtab(
ClientData clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
sqlite3 *db;
int rc;
if( objc!=3 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB SQL");
return TCL_ERROR;
}
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2]));
if( rc!=SQLITE_OK ){
Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
return TCL_ERROR;
}
return TCL_OK;
}
#endif
int Sqlitetest8_Init(Tcl_Interp *interp){
#ifndef SQLITE_OMIT_VIRTUALTABLE
static struct {
char *zName;
Tcl_ObjCmdProc *xProc;
void *clientData;
} aObjCmd[] = {
{ "register_echo_module", register_echo_module, 0 },
{ "sqlite3_declare_vtab", declare_vtab, 0 },
};
int i;
for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
}
#endif
return TCL_OK;
}