Skip to content

Commit 35e368c

Browse files
committed
rebase main
1 parent 3eeb165 commit 35e368c

File tree

5 files changed

+203
-101
lines changed

5 files changed

+203
-101
lines changed

sqlx-core/src/executor.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,76 @@ pub trait Executor<'c>: Send + Debug + Sized {
182182
where
183183
'c: 'e;
184184
}
185+
186+
/// A type that may be executed against a database connection.
187+
///
188+
/// Implemented for the following:
189+
///
190+
/// * [`&str`](std::str)
191+
/// * [`Query`](super::query::Query)
192+
///
193+
pub trait Execute<'q, DB: Database>: Send + Sized {
194+
/// Gets the SQL that will be executed.
195+
fn sql(&self) -> &'q str;
196+
197+
/// Gets the previously cached statement, if available.
198+
fn statement(&self) -> Option<&DB::Statement<'q>>;
199+
200+
/// Returns the arguments to be bound against the query string.
201+
///
202+
/// Returning `Ok(None)` for `Arguments` indicates to use a "simple" query protocol and to not
203+
/// prepare the query. Returning `Ok(Some(Default::default()))` is an empty arguments object that
204+
/// will be prepared (and cached) before execution.
205+
///
206+
/// Returns `Err` if encoding any of the arguments failed.
207+
fn take_arguments(&mut self) -> Result<Option<<DB as Database>::Arguments<'q>>, BoxDynError>;
208+
209+
/// Returns `true` if the statement should be cached.
210+
fn persistent(&self) -> bool;
211+
}
212+
213+
// NOTE: `Execute` is explicitly not implemented for String and &String to make it slightly more
214+
// involved to write `conn.execute(format!("SELECT {val}"))`
215+
impl<'q, DB: Database> Execute<'q, DB> for &'q str {
216+
#[inline]
217+
fn sql(&self) -> &'q str {
218+
self
219+
}
220+
221+
#[inline]
222+
fn statement(&self) -> Option<&DB::Statement<'q>> {
223+
None
224+
}
225+
226+
#[inline]
227+
fn take_arguments(&mut self) -> Result<Option<<DB as Database>::Arguments<'q>>, BoxDynError> {
228+
Ok(None)
229+
}
230+
231+
#[inline]
232+
fn persistent(&self) -> bool {
233+
true
234+
}
235+
}
236+
237+
impl<'q, DB: Database> Execute<'q, DB> for (&'q str, Option<<DB as Database>::Arguments<'q>>) {
238+
#[inline]
239+
fn sql(&self) -> &'q str {
240+
self.0
241+
}
242+
243+
#[inline]
244+
fn statement(&self) -> Option<&DB::Statement<'q>> {
245+
None
246+
}
247+
248+
#[inline]
249+
fn take_arguments(&mut self) -> Result<Option<<DB as Database>::Arguments<'q>>, BoxDynError> {
250+
Ok(self.1.take())
251+
}
252+
253+
#[inline]
254+
fn persistent(&self) -> bool {
255+
true
256+
}
257+
}

0 commit comments

Comments
 (0)