1use super::{HostSql, Policy};
5
6impl<P: Policy> HostSql<P> {
7 pub fn fail_at(&self, name: &str, event: &str, message: &str) {
10 self.batch(&format!(
11 "CREATE TRIGGER {} {event} BEGIN SELECT RAISE(ABORT,'{}'); END",
12 crate::sql::identifier(name),
13 message.replace('\'', "''")
14 ))
15 .unwrap();
16 }
17
18 pub fn set_value_limit(&self, bytes: i32) -> i32 {
21 self.connection.set_limit(rusqlite::limits::Limit::SQLITE_LIMIT_LENGTH, bytes).unwrap()
22 }
23
24 pub fn crash_after_next_commit(&mut self) {
26 self.connection
27 .wal_hook(Some(|_: &mut rusqlite::hooks::Wal, _| std::process::exit(86)))
28 .unwrap();
29 }
30
31 pub fn panic_next_function(&self) {
34 self.authorization.panic_next_function();
35 }
36
37 pub fn panic_next_authorization(&self) {
39 self.authorization.panic_next();
40 }
41
42 pub fn panic_in_next_hook(&self) {
45 self.hook_panics.store(true, std::sync::atomic::Ordering::SeqCst);
46 }
47
48 pub fn refuse_transaction_end(&mut self, refuse: bool) {
51 use rusqlite::hooks::{AuthAction, AuthContext, Authorization, TransactionOperation};
52 let mut authorize = self.authorization.callback();
53 self.connection
54 .authorizer(Some(move |context: AuthContext<'_>| {
55 if refuse
56 && matches!(
57 context.action,
58 AuthAction::Transaction { operation }
59 if !matches!(operation, TransactionOperation::Begin)
60 )
61 {
62 Authorization::Deny
63 } else {
64 authorize(context)
65 }
66 }))
67 .unwrap();
68 }
69
70 pub fn crash_after(&self, table: &str, action: &str) {
72 self.connection
73 .create_scalar_function(
74 "test_crash",
75 0,
76 rusqlite::functions::FunctionFlags::SQLITE_UTF8
77 | rusqlite::functions::FunctionFlags::SQLITE_INNOCUOUS,
78 |_| -> rusqlite::Result<i64> { std::process::exit(86) },
79 )
80 .unwrap();
81 self.batch(&format!(
82 "CREATE TRIGGER _coven_crash AFTER {action} ON {table} BEGIN SELECT test_crash(); END"
83 ))
84 .unwrap();
85 }
86}