Added experimental APIs: sqlite_begin_hook() and sqlite_commit_hook(). (CVS 896)
FossilOrigin-Name: 5efbf62313519d0a6e8b8f5dbb29d3ef428d73e8
This commit is contained in:
+27
-1
@@ -14,7 +14,7 @@
|
||||
** other files are for internal use by SQLite and should not be
|
||||
** accessed by users of the library.
|
||||
**
|
||||
** $Id: main.c,v 1.119 2003/03/31 00:30:48 drh Exp $
|
||||
** $Id: main.c,v 1.120 2003/04/03 15:46:04 drh Exp $
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
#include "os.h"
|
||||
@@ -1023,3 +1023,29 @@ void *sqlite_trace(sqlite *db, void (*xTrace)(void*,const char*), void *pArg){
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
** Register functions to be invoked when a transaction is started or when
|
||||
** a transaction commits. If either function returns non-zero, then the
|
||||
** corresponding operation aborts with a constraint error.
|
||||
*/
|
||||
void *sqlite_begin_hook(
|
||||
sqlite *db,
|
||||
int (*xCallback)(void*),
|
||||
void *pArg
|
||||
){
|
||||
void *pOld = db->pBeginArg;
|
||||
db->xBeginCallback = xCallback;
|
||||
db->pBeginArg = pArg;
|
||||
return pOld;
|
||||
}
|
||||
void *sqlite_commit_hook(
|
||||
sqlite *db,
|
||||
int (*xCallback)(void*),
|
||||
void *pArg
|
||||
){
|
||||
void *pOld = db->pCommitArg;
|
||||
db->xCommitCallback = xCallback;
|
||||
db->pCommitArg = pArg;
|
||||
return pOld;
|
||||
}
|
||||
|
||||
+16
-1
@@ -12,7 +12,7 @@
|
||||
** This header file defines the interface that the SQLite library
|
||||
** presents to client programs.
|
||||
**
|
||||
** @(#) $Id: sqlite.h.in,v 1.43 2003/03/31 00:30:49 drh Exp $
|
||||
** @(#) $Id: sqlite.h.in,v 1.44 2003/04/03 15:46:04 drh Exp $
|
||||
*/
|
||||
#ifndef _SQLITE_H_
|
||||
#define _SQLITE_H_
|
||||
@@ -676,6 +676,21 @@ int sqlite_step(
|
||||
*/
|
||||
int sqlite_finalize(sqlite_vm*, char **pzErrMsg);
|
||||
|
||||
/*** EXPERIMENTAL ***
|
||||
**
|
||||
** Register a callback function to be invoked whenever a new transaction
|
||||
** is started or committed. The pArg argument is passed through to the
|
||||
** callback. If the callback function returns non-zero, then the operation
|
||||
** is aborted with a constraint error.
|
||||
**
|
||||
** If another function was previously registered, its pArg value is returned.
|
||||
** Otherwise NULL is returned.
|
||||
**
|
||||
** Registering a NULL function disables the callback.
|
||||
*/
|
||||
void *sqlite_begin_hook(sqlite*, int(*)(void*), void*);
|
||||
void *sqlite_commit_hook(sqlite*, int(*)(void*), void*);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* End of the 'extern "C"' block */
|
||||
#endif
|
||||
|
||||
+5
-1
@@ -11,7 +11,7 @@
|
||||
*************************************************************************
|
||||
** Internal interface definitions for SQLite.
|
||||
**
|
||||
** @(#) $Id: sqliteInt.h,v 1.169 2003/03/31 13:36:09 drh Exp $
|
||||
** @(#) $Id: sqliteInt.h,v 1.170 2003/04/03 15:46:04 drh Exp $
|
||||
*/
|
||||
#include "config.h"
|
||||
#include "sqlite.h"
|
||||
@@ -244,6 +244,10 @@ struct sqlite {
|
||||
int nTable; /* Number of tables in the database */
|
||||
void *pBusyArg; /* 1st Argument to the busy callback */
|
||||
int (*xBusyCallback)(void *,const char*,int); /* The busy callback */
|
||||
void *pBeginArg; /* Argument to the xBeginCallback() */
|
||||
int (*xBeginCallback)(void*); /* Invoked at every transaction start */
|
||||
void *pCommitArg; /* Argument to xCommitCallback() */
|
||||
int (*xCommitCallback)(void*);/* Invoked at every commit. */
|
||||
Hash aFunc; /* All functions that can be in SQL exprs */
|
||||
int lastRowid; /* ROWID of most recent insert */
|
||||
int priorNewRowid; /* Last randomly generated ROWID */
|
||||
|
||||
+124
-8
@@ -11,7 +11,7 @@
|
||||
*************************************************************************
|
||||
** A TCL Interface to SQLite
|
||||
**
|
||||
** $Id: tclsqlite.c,v 1.45 2003/03/30 19:17:03 drh Exp $
|
||||
** $Id: tclsqlite.c,v 1.46 2003/04/03 15:46:04 drh Exp $
|
||||
*/
|
||||
#ifndef NO_TCL /* Omit this whole file if TCL is unavailable */
|
||||
|
||||
@@ -51,6 +51,8 @@ struct SqliteDb {
|
||||
sqlite *db; /* The "real" database structure */
|
||||
Tcl_Interp *interp; /* The interpreter used for this database */
|
||||
char *zBusy; /* The busy callback routine */
|
||||
char *zBegin; /* The begin-transaction callback routine */
|
||||
char *zCommit; /* The commit-transaction callback routine */
|
||||
SqlFunc *pFunc; /* List of SQL functions */
|
||||
int rc; /* Return code of most recent sqlite_exec() */
|
||||
};
|
||||
@@ -260,6 +262,12 @@ static void DbDeleteCmd(void *db){
|
||||
if( pDb->zBusy ){
|
||||
Tcl_Free(pDb->zBusy);
|
||||
}
|
||||
if( pDb->zBegin ){
|
||||
Tcl_Free(pDb->zBegin);
|
||||
}
|
||||
if( pDb->zCommit ){
|
||||
Tcl_Free(pDb->zCommit);
|
||||
}
|
||||
Tcl_Free((char*)pDb);
|
||||
}
|
||||
|
||||
@@ -288,6 +296,39 @@ static int DbBusyHandler(void *cd, const char *zTable, int nTries){
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
** This routine is called when a new transaction is started. The
|
||||
** TCL script in pDb->zBegin is executed. If it returns non-zero or
|
||||
** if it throws an exception, the transaction is aborted.
|
||||
*/
|
||||
static int DbBeginHandler(void *cd){
|
||||
SqliteDb *pDb = (SqliteDb*)cd;
|
||||
int rc;
|
||||
|
||||
rc = Tcl_Eval(pDb->interp, pDb->zBegin);
|
||||
if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** This routine is called when a transaction is committed. The
|
||||
** TCL script in pDb->zCommit is executed. If it returns non-zero or
|
||||
** if it throws an exception, the transaction is rolled back instead
|
||||
** of being committed.
|
||||
*/
|
||||
static int DbCommitHandler(void *cd){
|
||||
SqliteDb *pDb = (SqliteDb*)cd;
|
||||
int rc;
|
||||
|
||||
rc = Tcl_Eval(pDb->interp, pDb->zCommit);
|
||||
if( rc!=TCL_OK || atoi(Tcl_GetStringResult(pDb->interp)) ){
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** This routine is called to evaluate an SQL function implemented
|
||||
** using TCL script.
|
||||
@@ -328,15 +369,16 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
|
||||
SqliteDb *pDb = (SqliteDb*)cd;
|
||||
int choice;
|
||||
static const char *DB_strs[] = {
|
||||
"busy", "changes", "close",
|
||||
"complete", "errorcode", "eval",
|
||||
"function", "last_insert_rowid", "timeout",
|
||||
0
|
||||
"begin_hook", "busy", "changes",
|
||||
"close", "commit_hook", "complete",
|
||||
"errorcode", "eval", "function",
|
||||
"last_insert_rowid", "timeout", 0
|
||||
};
|
||||
enum DB_enum {
|
||||
DB_BUSY, DB_CHANGES, DB_CLOSE,
|
||||
DB_COMPLETE, DB_ERRORCODE, DB_EVAL,
|
||||
DB_FUNCTION, DB_LAST_INSERT_ROWID,DB_TIMEOUT,
|
||||
DB_BEGIN_HOOK, DB_BUSY, DB_CHANGES,
|
||||
DB_CLOSE, DB_COMMIT_HOOK, DB_COMPLETE,
|
||||
DB_ERRORCODE, DB_EVAL, DB_FUNCTION,
|
||||
DB_LAST_INSERT_ROWID, DB_TIMEOUT,
|
||||
};
|
||||
|
||||
if( objc<2 ){
|
||||
@@ -349,6 +391,43 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
|
||||
|
||||
switch( (enum DB_enum)choice ){
|
||||
|
||||
/* $db begin_callback ?CALLBACK?
|
||||
**
|
||||
** Invoke the given callback at the beginning of every SQL transaction.
|
||||
** If the callback throws an exception or returns non-zero, then the
|
||||
** transaction is aborted. If CALLBACK is an empty string, the callback
|
||||
** is disabled.
|
||||
*/
|
||||
case DB_BEGIN_HOOK: {
|
||||
if( objc>3 ){
|
||||
Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
|
||||
}else if( objc==2 ){
|
||||
if( pDb->zBegin ){
|
||||
Tcl_AppendResult(interp, pDb->zBegin, 0);
|
||||
}
|
||||
}else{
|
||||
char *zBegin;
|
||||
int len;
|
||||
if( pDb->zBegin ){
|
||||
Tcl_Free(pDb->zBegin);
|
||||
}
|
||||
zBegin = Tcl_GetStringFromObj(objv[2], &len);
|
||||
if( zBegin && len>0 ){
|
||||
pDb->zBegin = Tcl_Alloc( len + 1 );
|
||||
strcpy(pDb->zBegin, zBegin);
|
||||
}else{
|
||||
pDb->zBegin = 0;
|
||||
}
|
||||
if( pDb->zBegin ){
|
||||
pDb->interp = interp;
|
||||
sqlite_begin_hook(pDb->db, DbBeginHandler, pDb);
|
||||
}else{
|
||||
sqlite_begin_hook(pDb->db, 0, 0);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/* $db busy ?CALLBACK?
|
||||
**
|
||||
** Invoke the given callback if an SQL statement attempts to open
|
||||
@@ -413,6 +492,43 @@ static int DbObjCmd(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
|
||||
break;
|
||||
}
|
||||
|
||||
/* $db commit_hook ?CALLBACK?
|
||||
**
|
||||
** Invoke the given callback just before committing every SQL transaction.
|
||||
** If the callback throws an exception or returns non-zero, then the
|
||||
** transaction is aborted. If CALLBACK is an empty string, the callback
|
||||
** is disabled.
|
||||
*/
|
||||
case DB_COMMIT_HOOK: {
|
||||
if( objc>3 ){
|
||||
Tcl_WrongNumArgs(interp, 2, objv, "?CALLBACK?");
|
||||
}else if( objc==2 ){
|
||||
if( pDb->zCommit ){
|
||||
Tcl_AppendResult(interp, pDb->zCommit, 0);
|
||||
}
|
||||
}else{
|
||||
char *zCommit;
|
||||
int len;
|
||||
if( pDb->zCommit ){
|
||||
Tcl_Free(pDb->zCommit);
|
||||
}
|
||||
zCommit = Tcl_GetStringFromObj(objv[2], &len);
|
||||
if( zCommit && len>0 ){
|
||||
pDb->zCommit = Tcl_Alloc( len + 1 );
|
||||
strcpy(pDb->zCommit, zCommit);
|
||||
}else{
|
||||
pDb->zCommit = 0;
|
||||
}
|
||||
if( pDb->zCommit ){
|
||||
pDb->interp = interp;
|
||||
sqlite_commit_hook(pDb->db, DbCommitHandler, pDb);
|
||||
}else{
|
||||
sqlite_commit_hook(pDb->db, 0, 0);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/* $db complete SQL
|
||||
**
|
||||
** Return TRUE if SQL is a complete SQL statement. Return FALSE if
|
||||
|
||||
+16
-1
@@ -36,7 +36,7 @@
|
||||
** in this file for details. If in doubt, do not deviate from existing
|
||||
** commenting and indentation practices when changing or adding code.
|
||||
**
|
||||
** $Id: vdbe.c,v 1.210 2003/04/03 01:50:45 drh Exp $
|
||||
** $Id: vdbe.c,v 1.211 2003/04/03 15:46:04 drh Exp $
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
#include <ctype.h>
|
||||
@@ -3233,6 +3233,13 @@ case OP_Transaction: {
|
||||
}
|
||||
db->aDb[i].inTrans = 1;
|
||||
p->undoTransOnError = 1;
|
||||
if( db->xBeginCallback!=0 && i==1 && rc==SQLITE_OK ){
|
||||
if( sqliteSafetyOff(db) ) goto abort_due_to_misuse;
|
||||
if( db->xBeginCallback(db->pBeginArg)!=0 ){
|
||||
rc = SQLITE_CONSTRAINT;
|
||||
}
|
||||
if( sqliteSafetyOn(db) ) goto abort_due_to_misuse;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -3246,6 +3253,14 @@ case OP_Transaction: {
|
||||
*/
|
||||
case OP_Commit: {
|
||||
int i;
|
||||
if( db->xCommitCallback!=0 ){
|
||||
if( sqliteSafetyOff(db) ) goto abort_due_to_misuse;
|
||||
if( db->xCommitCallback(db->pCommitArg)!=0 ){
|
||||
rc = SQLITE_CONSTRAINT;
|
||||
}
|
||||
if( sqliteSafetyOn(db) ) goto abort_due_to_misuse;
|
||||
if( rc ) break;
|
||||
}
|
||||
assert( rc==SQLITE_OK );
|
||||
for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
|
||||
if( db->aDb[i].inTrans ){
|
||||
|
||||
Reference in New Issue
Block a user