Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add the sqlite3session_table_filter API to the sessions extension. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | sessions |
Files: | files | file ages | folders |
SHA1: |
b7e4dd889d37c8f57c2d3c7900e802f6 |
User & Date: | dan 2013-08-23 17:43:32.715 |
Context
2013-08-23
| ||
17:54 | Merge recent trunk changes. (check-in: 6cc54de88b user: dan tags: sessions) | |
17:43 | Add the sqlite3session_table_filter API to the sessions extension. (check-in: b7e4dd889d user: dan tags: sessions) | |
2013-08-22
| ||
15:07 | Merge in minor bug fixes and performance tweaks from trunk leading up to the version 3.8.0 release. (check-in: 831492dca8 user: drh tags: sessions) | |
Changes
Added ext/session/sessionA.test.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | # 2013 July 04 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # # This file tests that the sessions module handles foreign key constraint # violations when applying changesets as required. # if {![info exists testdir]} { set testdir [file join [file dirname [info script]] .. .. test] } source [file join [file dirname [info script]] session_common.tcl] source $testdir/tester.tcl ifcapable !session {finish_test; return} set testprefix sessionA forcedelete test.db2 sqlite3 db2 test.db2 foreach {tn db} {1 db 2 db2} { do_test 1.$tn.1 { execsql { CREATE TABLE t1(a PRIMARY KEY, b); CREATE TABLE t2(a PRIMARY KEY, b); CREATE TABLE t3(a PRIMARY KEY, b); } $db } {} } proc tbl_filter {zTbl} { return $::table_filter($zTbl) } do_test 2.1 { set ::table_filter(t1) 1 set ::table_filter(t2) 0 set ::table_filter(t3) 1 sqlite3session S db main S table_filter tbl_filter execsql { INSERT INTO t1 VALUES('a', 'b'); INSERT INTO t2 VALUES('c', 'd'); INSERT INTO t3 VALUES('e', 'f'); } set changeset [S changeset] S delete sqlite3changeset_apply db2 $changeset xConflict execsql { SELECT * FROM t1; SELECT * FROM t2; SELECT * FROM t3; } db2 } {a b e f} finish_test |
Changes to ext/session/sqlite3session.c.
︙ | ︙ | |||
19 20 21 22 23 24 25 26 27 28 29 30 31 32 | struct sqlite3_session { sqlite3 *db; /* Database handle session is attached to */ char *zDb; /* Name of database session is attached to */ int bEnable; /* True if currently recording */ int bIndirect; /* True if all changes are indirect */ int bAutoAttach; /* True to auto-attach tables */ int rc; /* Non-zero if an error has occurred */ sqlite3_session *pNext; /* Next session object on same db. */ SessionTable *pTable; /* List of attached tables */ }; /* ** Structure for changeset iterators. */ | > > | 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | struct sqlite3_session { sqlite3 *db; /* Database handle session is attached to */ char *zDb; /* Name of database session is attached to */ int bEnable; /* True if currently recording */ int bIndirect; /* True if all changes are indirect */ int bAutoAttach; /* True to auto-attach tables */ int rc; /* Non-zero if an error has occurred */ void *pFilterCtx; /* First argument to pass to xTableFilter */ int (*xTableFilter)(void *pCtx, const char *zTab); sqlite3_session *pNext; /* Next session object on same db. */ SessionTable *pTable; /* List of attached tables */ }; /* ** Structure for changeset iterators. */ |
︙ | ︙ | |||
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 | if( pSession->rc ) continue; if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue; for(pTab=pSession->pTable; pTab || pSession->bAutoAttach; pTab=pTab->pNext){ if( !pTab ){ /* This branch is taken if table zName has not yet been attached to ** this session and the auto-attach flag is set. */ pSession->rc = sqlite3session_attach(pSession,zName); if( pSession->rc ) break; pTab = pSession->pTable; assert( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ); } if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ){ | > > > > > > > > > > | 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 | if( pSession->rc ) continue; if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue; for(pTab=pSession->pTable; pTab || pSession->bAutoAttach; pTab=pTab->pNext){ if( !pTab ){ /* This branch is taken if table zName has not yet been attached to ** this session and the auto-attach flag is set. */ /* If there is a table-filter configured, invoke it. If it returns 0, ** this change will not be recorded. Break out of the loop early in ** this case. */ if( pSession->xTableFilter && pSession->xTableFilter(pSession->pFilterCtx, zName)==0 ){ break; } pSession->rc = sqlite3session_attach(pSession,zName); if( pSession->rc ) break; pTab = pSession->pTable; assert( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ); } if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ){ |
︙ | ︙ | |||
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 | /* Delete all attached table objects. And the contents of their ** associated hash-tables. */ sessionDeleteTable(pSession->pTable); /* Free the session object itself. */ sqlite3_free(pSession); } /* ** Attach a table to a session. All subsequent changes made to the table ** while the session object is enabled will be recorded. ** ** Only tables that have a PRIMARY KEY defined may be attached. It does ** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) | > > > > > > > > > > > > > | 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 | /* Delete all attached table objects. And the contents of their ** associated hash-tables. */ sessionDeleteTable(pSession->pTable); /* Free the session object itself. */ sqlite3_free(pSession); } /* ** Set a table filter on a Session Object. */ void sqlite3session_table_filter( sqlite3_session *pSession, int(*xFilter)(void*, const char*), void *pCtx /* First argument passed to xFilter */ ){ pSession->bAutoAttach = 1; pSession->pFilterCtx = pCtx; pSession->xTableFilter = xFilter; } /* ** Attach a table to a session. All subsequent changes made to the table ** while the session object is enabled will be recorded. ** ** Only tables that have a PRIMARY KEY defined may be attached. It does ** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) |
︙ | ︙ |
Changes to ext/session/sqlite3session.h.
︙ | ︙ | |||
66 67 68 69 70 71 72 73 74 75 76 77 78 79 | ** function are undefined. ** ** Session objects must be deleted before the database handle to which they ** are attached is closed. Refer to the documentation for ** [sqlite3session_create()] for details. */ void sqlite3session_delete(sqlite3_session *pSession); /* ** CAPI3REF: Enable Or Disable A Session Object ** ** Enable or disable the recording of changes by a session object. When ** enabled, a session object records changes made to the database. When ** disabled - it does not. A newly created session object is enabled. | > | 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | ** function are undefined. ** ** Session objects must be deleted before the database handle to which they ** are attached is closed. Refer to the documentation for ** [sqlite3session_create()] for details. */ void sqlite3session_delete(sqlite3_session *pSession); /* ** CAPI3REF: Enable Or Disable A Session Object ** ** Enable or disable the recording of changes by a session object. When ** enabled, a session object records changes made to the database. When ** disabled - it does not. A newly created session object is enabled. |
︙ | ︙ | |||
147 148 149 150 151 152 153 154 155 156 157 158 159 160 | ** SQLITE_OK is returned if the call completes without error. Or, if an error ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned. */ int sqlite3session_attach( sqlite3_session *pSession, /* Session object */ const char *zTab /* Table name */ ); /* ** CAPI3REF: Generate A Changeset From A Session Object ** ** Obtain a changeset containing changes to the tables attached to the ** session object passed as the first argument. If successful, ** set *ppChangeset to point to a buffer containing the changeset | > > > > > > > > > > > > > > > > > > | 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | ** SQLITE_OK is returned if the call completes without error. Or, if an error ** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned. */ int sqlite3session_attach( sqlite3_session *pSession, /* Session object */ const char *zTab /* Table name */ ); /* ** CAPI3REF: Set a table filter on a Session Object. ** ** The second argument (xFilter) is the "filter callback". For changes to rows ** in tables that are not attached to the Session oject, the filter is called ** to determine whether changes to the table's rows should be tracked or not. ** If xFilter returns 0, changes is not tracked. Note that once a table is ** attached, xFilter will not be called again. */ void sqlite3session_table_filter( sqlite3_session *pSession, /* Session object */ int(*xFilter)( void *pCtx, /* Copy of third arg to _filter_table() */ const char *zTab /* Table name */ ), void *pCtx /* First argument passed to xFilter */ ); /* ** CAPI3REF: Generate A Changeset From A Session Object ** ** Obtain a changeset containing changes to the tables attached to the ** session object passed as the first argument. If successful, ** set *ppChangeset to point to a buffer containing the changeset |
︙ | ︙ |
Changes to ext/session/test_session.c.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #if defined(SQLITE_TEST) && defined(SQLITE_ENABLE_SESSION) \ && defined(SQLITE_ENABLE_PREUPDATE_HOOK) #include "sqlite3session.h" #include <assert.h> #include <string.h> #include <tcl.h> static int test_session_error(Tcl_Interp *interp, int rc){ extern const char *sqlite3ErrName(int); Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); return TCL_ERROR; } /* ** Tclcmd: $session attach TABLE ** $session changeset ** $session delete ** $session enable BOOL ** $session indirect INTEGER */ static int test_session_cmd( void *clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | | | | | | > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | #if defined(SQLITE_TEST) && defined(SQLITE_ENABLE_SESSION) \ && defined(SQLITE_ENABLE_PREUPDATE_HOOK) #include "sqlite3session.h" #include <assert.h> #include <string.h> #include <tcl.h> typedef struct TestSession TestSession; struct TestSession { sqlite3_session *pSession; Tcl_Interp *interp; Tcl_Obj *pFilterScript; }; static int test_session_error(Tcl_Interp *interp, int rc){ extern const char *sqlite3ErrName(int); Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); return TCL_ERROR; } static int test_table_filter(void *pCtx, const char *zTbl){ TestSession *p = (TestSession*)pCtx; Tcl_Obj *pEval; int rc; int bRes = 0; pEval = Tcl_DuplicateObj(p->pFilterScript); Tcl_IncrRefCount(pEval); rc = Tcl_ListObjAppendElement(p->interp, pEval, Tcl_NewStringObj(zTbl, -1)); if( rc==TCL_OK ){ rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL); } if( rc==TCL_OK ){ rc = Tcl_GetBooleanFromObj(p->interp, Tcl_GetObjResult(p->interp), &bRes); } if( rc!=TCL_OK ){ /* printf("error: %s\n", Tcl_GetStringResult(p->interp)); */ Tcl_BackgroundError(p->interp); } Tcl_DecrRefCount(pEval); return bRes; } /* ** Tclcmd: $session attach TABLE ** $session changeset ** $session delete ** $session enable BOOL ** $session indirect INTEGER ** $session table_filter SCRIPT */ static int test_session_cmd( void *clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ TestSession *p = (TestSession*)clientData; sqlite3_session *pSession = p->pSession; struct SessionSubcmd { const char *zSub; int nArg; const char *zMsg; int iSub; } aSub[] = { { "attach", 1, "TABLE", }, /* 0 */ { "changeset", 0, "", }, /* 1 */ { "delete", 0, "", }, /* 2 */ { "enable", 1, "BOOL", }, /* 3 */ { "indirect", 1, "BOOL", }, /* 4 */ { "isempty", 0, "", }, /* 5 */ { "table_filter", 1, "SCRIPT", }, /* 6 */ { 0 } }; int iSub; int rc; if( objc<2 ){ Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ..."); |
︙ | ︙ | |||
61 62 63 64 65 66 67 | case 0: { /* attach */ char *zArg = Tcl_GetString(objv[2]); if( zArg[0]=='*' && zArg[1]=='\0' ) zArg = 0; rc = sqlite3session_attach(pSession, zArg); if( rc!=SQLITE_OK ){ return test_session_error(interp, rc); } | < > | 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | case 0: { /* attach */ char *zArg = Tcl_GetString(objv[2]); if( zArg[0]=='*' && zArg[1]=='\0' ) zArg = 0; rc = sqlite3session_attach(pSession, zArg); if( rc!=SQLITE_OK ){ return test_session_error(interp, rc); } break; } case 1: { /* changeset */ int nChange; void *pChange; rc = sqlite3session_changeset(pSession, &nChange, &pChange); if( rc==SQLITE_OK ){ Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pChange, nChange)); |
︙ | ︙ | |||
103 104 105 106 107 108 109 110 111 112 113 114 115 | case 5: { /* isempty */ int val; val = sqlite3session_isempty(pSession); Tcl_SetObjResult(interp, Tcl_NewBooleanObj(val)); break; } } return TCL_OK; } static void test_session_del(void *clientData){ | > > > > > > > > > | > | > | > > | > | | 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | case 5: { /* isempty */ int val; val = sqlite3session_isempty(pSession); Tcl_SetObjResult(interp, Tcl_NewBooleanObj(val)); break; } case 6: { /* table_filter */ if( p->pFilterScript ) Tcl_DecrRefCount(p->pFilterScript); p->interp = interp; p->pFilterScript = Tcl_DuplicateObj(objv[2]); Tcl_IncrRefCount(p->pFilterScript); sqlite3session_table_filter(pSession, test_table_filter, clientData); break; } } return TCL_OK; } static void test_session_del(void *clientData){ TestSession *p = (TestSession*)clientData; if( p->pFilterScript ) Tcl_DecrRefCount(p->pFilterScript); sqlite3session_delete(p->pSession); ckfree(p); } /* ** Tclcmd: sqlite3session CMD DB-HANDLE DB-NAME */ static int test_sqlite3session( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ sqlite3 *db; Tcl_CmdInfo info; int rc; /* sqlite3session_create() return code */ TestSession *p; /* New wrapper object */ if( objc!=4 ){ Tcl_WrongNumArgs(interp, 1, objv, "CMD DB-HANDLE DB-NAME"); return TCL_ERROR; } if( 0==Tcl_GetCommandInfo(interp, Tcl_GetString(objv[2]), &info) ){ Tcl_AppendResult(interp, "no such handle: ", Tcl_GetString(objv[2]), 0); return TCL_ERROR; } db = *(sqlite3 **)info.objClientData; p = (TestSession*)ckalloc(sizeof(TestSession)); memset(p, 0, sizeof(TestSession)); rc = sqlite3session_create(db, Tcl_GetString(objv[3]), &p->pSession); if( rc!=SQLITE_OK ){ ckfree(p); return test_session_error(interp, rc); } Tcl_CreateObjCommand( interp, Tcl_GetString(objv[1]), test_session_cmd, (ClientData)p, test_session_del ); Tcl_SetObjResult(interp, objv[1]); return TCL_OK; } static void test_append_value(Tcl_Obj *pList, sqlite3_value *pVal){ |
︙ | ︙ |