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
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
use std::mem::take;
use sqlx::{
database::HasArguments, Database, Execute, Executor,
};
use tracing::debug;
/// like sqlx::Query* but allow binding via &mut self
mod _private {
use sqlx::{database::HasArguments, Database};
pub struct InnerExecutable<'s, 'q, DB: Database> {
pub stmt: &'s str,
pub buffer: <DB as HasArguments<'q>>::Arguments,
pub persistent: bool,
}
}
#[cfg(feature = "export_inner_executable")]
pub use _private::InnerExecutable;
#[cfg(not(feature = "export_inner_executable"))]
pub(crate) use _private::InnerExecutable;
impl<'q, DB: Database> Execute<'q, DB>
for InnerExecutable<'q, 'q, DB>
{
fn sql(&self) -> &'q str {
self.stmt
}
fn persistent(&self) -> bool {
self.persistent
}
fn statement(
&self,
) -> Option<
&<DB as sqlx::database::HasStatement<'q>>::Statement,
> {
None
}
fn take_arguments(
&mut self,
) -> Option<<DB as HasArguments<'q>>::Arguments> {
Some(take(&mut self.buffer))
}
}
impl<'s, 'q, S: Database> InnerExecutable<'s, 'q, S> {
pub fn as_str(&self) -> &str {
self.stmt
}
pub async fn execute<E>(
self,
executor: E,
) -> Result<S::QueryResult, sqlx::Error>
where
for<'c> E: Executor<'q, Database = S>,
{
debug!("execute: {}", self.stmt);
executor
.execute(InnerExecutable {
// SAFETY: the output of execute is free of
// any reference of self, which means that
// self can drop after the await, and the
// result can live longer
//
// I tried Self: 'q, and &'q mut self can
// be used to solve this issue
//
// I saw the same issue before, this is
// either a problem in sqlx or rust is not
// advanced enough to catch this pattern, but
// i'm sure this code is 100% safe
stmt: unsafe { &*(self.stmt as *const _) },
..self
})
.await
}
pub async fn fetch_one_with<E, O, F>(
self,
executor: E,
with: F,
) -> Result<O, sqlx::Error>
where
F: FnOnce(S::Row) -> Result<O, sqlx::Error>,
for<'c> E: Executor<'c, Database = S>,
{
debug!("fetch one: {}", self.stmt);
let execute = InnerExecutable {
// SAFETY: the output of execute is free of
// any reference of self, which means that
// self can drop after the await, and the
// result can live longer
//
// I tried Self: 'q, and &'q mut self can
// be used to solve this issue
//
// I saw the same issue before, this is
// either a problem in sqlx or rust is not
// advanced enough to catch this pattern, but
// i'm sure this code is 100% safe
stmt: unsafe { &*(self.stmt as *const _) },
..self
};
let res = executor.fetch_one(execute).await;
match res {
Ok(r) => Ok(with(r)?),
Err(e) => Err(e),
}
}
pub async fn fetch_all_with<E, O, F>(
self,
executor: E,
mut with: F,
) -> Result<Vec<O>, sqlx::Error>
where
F: FnMut(S::Row) -> Result<O, sqlx::Error>,
for<'c> E: Executor<'c, Database = S>,
{
debug!("fetch all: {}", self.stmt);
let execute = InnerExecutable {
// SAFETY: the output of execute is free of
// any reference of self, which means that
// self can drop after the await, and the
// result can live longer
//
// I tried Self: 'q, and &'q mut self can
// be used to solve this issue
//
// I saw the same issue before, this is
// either a problem in sqlx or rust is not
// advanced enough to catch this pattern, but
// i'm sure this code is 100% safe
stmt: unsafe { &*(self.stmt as *const _) },
..self
};
executor.fetch_all(execute).await.map(|r| {
r.into_iter()
.map(|r| with(r).expect("failed to decode"))
.collect::<Vec<_>>()
})
}
pub async fn fetch_optional_with<E, O, F>(
self,
executor: E,
with: F,
) -> Result<Option<O>, sqlx::Error>
where
F: FnOnce(S::Row) -> Result<O, sqlx::Error>,
for<'c> E: Executor<'c, Database = S>,
{
debug!("fetch optional: {}", self.stmt);
let execute = InnerExecutable {
// SAFETY: the output of execute is free of
// any reference of self, which means that
// self can drop after the await, and the
// result can live longer
//
// I tried Self: 'q, and &'q mut self can
// be used to solve this issue
//
// I saw the same issue before, this is
// either a problem in sqlx or rust is not
// advanced enough to catch this pattern, but
// i'm sure this code is 100% safe
stmt: unsafe { &*(self.stmt as *const _) },
..self
};
let op = executor.fetch_optional(execute).await;
match op {
Ok(Some(r)) => Ok(Some(with(r)?)),
_ => Ok(None),
}
}
}