-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchecked.rs
136 lines (122 loc) · 4.06 KB
/
checked.rs
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
pub use pgx::pg_sys::panic::CaughtError;
use pgx::PgTryBuilder;
use pgx::{
pg_sys::Datum,
spi::{self, SpiClient, SpiTupleTable},
PgOid,
};
use std::ops::{Deref, DerefMut};
use std::panic::{RefUnwindSafe, UnwindSafe};
use crate::subtxn::*;
#[derive(thiserror::Error, Debug)]
pub enum CheckedError {
#[error("caught error: {0:?}")]
// TODO: CaughtError currently doesn't implement `Error` and thus can't be made `#[from]`
CaughtError(CaughtError),
#[error("SPI error: {0:?}")]
SpiError(#[from] spi::Error),
}
/// Commands for SPI interface
pub trait CheckedCommands<'a>: Deref<Target = SpiClient<'a>> {
type Result<A>
where
A: 'a;
/// Execute a read-only command, returning an error if one occurred.
fn checked_select(
self,
query: &str,
limit: Option<i64>,
args: Option<Vec<(PgOid, Option<Datum>)>>,
) -> Result<Self::Result<SpiTupleTable>, CheckedError>;
}
/// Mutable commands for SPI interface
pub trait CheckedMutCommands<'a>: DerefMut<Target = SpiClient<'a>> {
type Result<A>
where
A: 'a;
/// Execute a mutable command, returning an error if one occurred.
fn checked_update(
self,
query: &str,
limit: Option<i64>,
args: Option<Vec<(PgOid, Option<Datum>)>>,
) -> Result<Self::Result<SpiTupleTable>, CheckedError>;
}
impl<'a, Parent: SubTransactionExt + RefUnwindSafe + UnwindSafe> CheckedCommands<'a>
for SubTransaction<Parent, RollbackOnDrop>
where
SubTransaction<Parent, RollbackOnDrop>: Deref<Target = SpiClient<'a>>,
{
type Result<A: 'a> = (A, Self);
fn checked_select(
self,
query: &str,
limit: Option<i64>,
args: Option<Vec<(PgOid, Option<Datum>)>>,
) -> Result<Self::Result<SpiTupleTable>, CheckedError> {
PgTryBuilder::new(move || {
self.select(query, limit, args)
.map(|res| (res, self))
.map_err(CheckedError::SpiError)
})
.catch_others(|e| Err(CheckedError::CaughtError(e)))
.execute()
}
}
impl<'a, Parent: SubTransactionExt + RefUnwindSafe + UnwindSafe> CheckedCommands<'a>
for SubTransaction<Parent, CommitOnDrop>
where
SubTransaction<Parent, CommitOnDrop>: Deref<Target = SpiClient<'a>>,
SubTransaction<Parent, RollbackOnDrop>: Deref<Target = SpiClient<'a>>,
{
type Result<A: 'a> = (A, Self);
fn checked_select(
self,
query: &str,
limit: Option<i64>,
args: Option<Vec<(PgOid, Option<Datum>)>>,
) -> Result<Self::Result<SpiTupleTable>, CheckedError> {
self.rollback_on_drop()
.checked_select(query, limit, args)
.map(|(v, s)| (v, s.commit_on_drop()))
}
}
impl<'a, Parent: SubTransactionExt + RefUnwindSafe + UnwindSafe> CheckedMutCommands<'a>
for SubTransaction<Parent, RollbackOnDrop>
where
SubTransaction<Parent, RollbackOnDrop>: DerefMut<Target = SpiClient<'a>>,
{
type Result<A: 'a> = (A, Self);
fn checked_update(
mut self,
query: &str,
limit: Option<i64>,
args: Option<Vec<(PgOid, Option<Datum>)>>,
) -> Result<Self::Result<SpiTupleTable>, CheckedError> {
PgTryBuilder::new(move || {
self.update(query, limit, args)
.map(|res| (res, self))
.map_err(CheckedError::SpiError)
})
.catch_others(|e| Err(CheckedError::CaughtError(e)))
.execute()
}
}
impl<'a, Parent: SubTransactionExt + RefUnwindSafe + UnwindSafe> CheckedMutCommands<'a>
for SubTransaction<Parent, CommitOnDrop>
where
SubTransaction<Parent, CommitOnDrop>: DerefMut<Target = SpiClient<'a>>,
SubTransaction<Parent, RollbackOnDrop>: DerefMut<Target = SpiClient<'a>>,
{
type Result<A: 'a> = (A, Self);
fn checked_update(
self,
query: &str,
limit: Option<i64>,
args: Option<Vec<(PgOid, Option<Datum>)>>,
) -> Result<Self::Result<SpiTupleTable>, CheckedError> {
self.rollback_on_drop()
.checked_update(query, limit, args)
.map(|(v, s)| (v, s.commit_on_drop()))
}
}