- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试使用 SQLite 的新 C 接口(interface)预更新 Hook :
https://www.sqlite.org/c3ref/preupdate_count.html
现在回答我的问题:pre_update API 签名如下:
void *sqlite3_preupdate_hook(
sqlite3 *db,
void(*xPreUpdate)(
void *pCtx, /* Copy of third arg to preupdate_hook() */
sqlite3 *db, /* Database handle */
int op, /* SQLITE_UPDATE, DELETE or INSERT */
char const *zDb, /* Database name */
char const *zName, /* Table name */
sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
),
void*
);
如您所见,它向回调注入(inject)一个指向注册 Hook 的数据库连接的指针。根据我的经验和 SQLite 文档,我知道更新/提交/回滚 Hook 不可重入,这意味着它们无法修改导致 Hook 调用的连接。
我想使用这个 pre_update 回调来读取和写入数据库。
现在我有 2 个问题:
1) SQLite pre_update 回调是否可重入并支持从回调范围修改和读取数据库?
2) 如果是这样,谁能解释一下为什么会这样?
我创建了一个新数据库并使用 SQLite shell 运行以下命令:
sqlite> CREATE TABLE Parent(_index INTEGER PRIMARY KEY);
sqlite> INSERT INTO "Parent" VALUES(1);
sqlite> CREATE TABLE Child(_index INTEGER PRIMARY KEY, CONSTRAINT ch_fk FOREIGN KEY(_index) REFERENCES Parent(_index) DEFERRABLE INITIALLY DEFERRED);
sqlite> INSERT INTO "Child" VALUES(1);
现在我正在尝试创建一个自动操作,它应该在事务内部工作,并在每次它的父亲更改它的 key 时更新一个 child ,这样它就不会违反约束。
注意:我知道我可以使用触发器/SQLite 外键更新机制实现此行为,但我愿意测试此 API 的稳定性。
所以代码:
int main(int argc, char *argv[]){
int rc;
char *err_msg;
sqlite3 *sqlite_connection;
rc = sqlite3_open("__database__.db",&sqlite_connection);
rc += rc = sqlite3_exec(sqlite_connection, "PRAGMA foreign_keys=ON;", 0, 0, &err_msg);/* set foreign keys mechanism on */
if(rc != SQLITE_OK){
printf("Error initializing connection : %s\n",err_msg);
exit(rc);
}
sqlite3_preupdate_hook(sqlite_connection,pre_hook,NULL);
//Watch tables content before
rc = sqlite3_exec(sqlite_connection, "SELECT * FROM PARENT;", callback, (void*)NULL, &err_msg);
rc = sqlite3_exec(sqlite_connection, "SELECT * FROM CHILD;", callback, (void*)NULL, &err_msg);
//BEGIN TRANSACTION
rc = sqlite3_exec(sqlite_connection, "BEGIN", 0, 0, &err_msg);
/* Update table to invoke callback */
rc += sqlite3_exec(sqlite_connection, "UPDATE Parent SET _index = 2 WHERE _index = 1", 0, 0, &err_msg); /* Update table to invoke callback */
//Watch tables content after
rc = sqlite3_exec(sqlite_connection, "SELECT * FROM PARENT;", callback, (void*)NULL, &err_msg);
rc = sqlite3_exec(sqlite_connection, "SELECT * FROM CHILD;", callback, (void*)NULL, &err_msg);
rc += sqlite3_exec(sqlite_connection, "COMMIT;", 0, 0, &err_msg);
if(rc != SQLITE_OK){
printf("Error updating the database : %s\n",err_msg);
rc = sqlite3_exec(sqlite_connection, "ROLLBACK", 0, 0, &err_msg);
if(rc != SQLITE_OK){
printf("Error updating the database : %s\n",err_msg);
}
}
sqlite3_close(sqlite_connection);
return rc;
}
还有 pre_hook :
void pre_hook(
void *pCtx, /* Copy of third arg to preupdate_hook() */
sqlite3 *db, /* Database handle */
int op, /* SQLITE_UPDATE, DELETE or INSERT */
char const *zDb, /* Database name */
char const *zName, /* Table name */
sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
sqlite3_int64 iKey2){
char query_buffer[100];
char *err_msg;
if((strcmp("Parent",zName) == 0) && (SQLITE_UPDATE == op)){
sprintf(query_buffer,"UPDATE Child SET _index = %d WHERE _index = %d",(int)iKey2,(int)iKey1);
if(sqlite3_exec(db,query_buffer , 0, 0, &err_msg) != SQLITE_OK){
printf("Error executin trigger\n");
}
}
}
然后我得到输出:
//运行前: parent :_索引:1
child :_索引:1
//运行后: parent :_索引:2
child :_索引:2更新数据库时出错:违反 FOREIGN KEY 约束
如您所见,根本没有违规!
然而,当我如下更改主要功能时:
1) 取消对 pre_hook 回调的签名。
2) 在回调之外操作子更新(在我们的例子中是在父更新之后)。
我突然得到了相同的输出,只是没有错误。
我认为这意味着 pre_update 回调不可重入,但我寻求对这个问题的专业解答。
最佳答案
来自documentation :
The update hook implementation must not do anything that will modifythe database connection that invoked the update hook. Any actions tomodify the database connection must be deferred until after thecompletion of the sqlite3_step() call that triggered the update hook.Note that sqlite3_prepare_v2() and sqlite3_step() both modify theirdatabase connections for the meaning of "modify" in this paragraph.
关于c++ - SQLite 预更新 Hook 重入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37545766/
我创建了一个简单的钩子(Hook),我安装了它 SetWindowsHookEx(WH_CBT, addr, dll, 0); 完成后,我卸载 UnhookWindowsHookEx(0); 然后我可
我正在使用 React Hooks,当我用 mobx 的观察者包装我的组件时,我收到了这个错误。可能是什么问题?是否可以将 mobx 与 React Hooks 一起使用? import classn
我知道这个问题已经被回答过很多次了。我只是找不到解决我的问题的答案,让我相信,我要么是愚蠢的,要么是我的问题没有被解决,因为它比我更愚蠢。除此之外,这是我的问题: 我正在尝试创建一个功能组件,它从 r
我正在使用 React Navigation 的 useNavigation 钩子(Hook): 在 MyComponent.js 中: import { useNavigation } from "
我想在 gitlab 中使用预提交钩子(Hook)。我做的一切都像文档中一样:https://docs.gitlab.com/ce/administration/custom_hooks.html 在
我最近在和一些人谈论我正在编写的程序时听到了“hook”这个词。尽管我从对话中推断出钩子(Hook)是一种函数,但我不确定这个术语到底意味着什么。我搜索了定义,但找不到好的答案。有人可以让我了解这个术
我正在寻找一个在页面创建或页面更改后调用的钩子(Hook),例如“在导航中隐藏页面”、“停用页面”或“移动/删除页面“ 有人知道吗? 谢谢! 最佳答案 这些 Hook 位于 t3lib/class.t
我正在使用钩子(Hook)将新方法添加到 CalEventLocalServiceImpl 中... 我的代码是.. public class MyCalendarLocalServiceImpl e
编译器将所有 SCSS 文件编译为 STANDALONE(无 Rails)项目中的 CSS 后,我需要一个 Compass Hook 。 除了编辑“compiler.rb”(这不是好的解决方案,因为
我“.get”一个请求并像这样处理响应: resp = requests.get('url') resp = resp.text .. # do stuff with resp 阅读包的文档后,我看到
我们想在外部数据库中存储一些关于提交的元信息。在克隆或 checkout 期间,应引用此数据库,我们将元信息复制到克隆的存储库中的文件中。需要数据库而不是仅仅使用文件是为了索引和搜索等...... 我
我有一个 react 钩子(Hook)useDbReadTable,用于从接受tablename和query初始数据的数据库读取数据。它返回一个对象,除了数据库中的数据之外,还包含 isLoading
在下面的代码中,当我调用 _toggleSearch 时,我同时更新 2 个钩子(Hook)。 toggleSearchIsVisible 是一个简单的 bool 值,但是,setActiveFilt
问题 我想在可由用户添加的表单中实现输入字段的键/值对。 参见 animated gif on dynamic fields . 此外,我想在用户提交表单并再次显示页面时显示保存的数据。 参见 ani
当状态处于 Hook 状态时,它可能会变得陈旧并泄漏内存: function App() { const [greeting, setGreeting] = useState("hello");
const shouldHide = useHideOnScroll(); return shouldHide ? null : something useHideOnScroll 行为应该返回更新后
我正在使用 React-native,在其中,我有一个名为 useUser 的自定义 Hook,它使用 Auth.getUserInfro 方法从 AWS Amplify 获取用户信息,并且然后获取返
我正在添加一个 gitolite 更新 Hook 作为 VREF,并且想知道是否有办法将它应用于除 gitolite-admin 之外的所有存储库。 有一个更简单的方法而不是列出我想要应用 Hook
如何使用带有 react-apollo-hooks 的 2 个 graphql 查询,其中第二个查询取决于从第一个查询中检索到的参数? 我尝试使用如下所示的 2 个查询: const [o, setO
我是 hooks 的新手,到目前为止印象还不错,但是,如果我尝试在函数内部使用 hooks,它似乎会提示(无效的 hook 调用。Hooks can only be called inside o
我是一名优秀的程序员,十分优秀!