Skip to content

Commit da105e8

Browse files
authored
Merge pull request #4795 from weiznich/fix/bind_params
Try to fix binds for multiconnection
2 parents 68f37f5 + 1ce67cf commit da105e8

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

diesel/src/query_builder/ast_pass.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,15 @@ where
385385
/// This function allows to access the inner bind collector if
386386
/// this `AstPass` represents a collect binds pass.
387387
fn bind_collector(&mut self) -> Option<(&mut DB::BindCollector<'b>, &mut DB::MetadataLookup)>;
388+
389+
/// This function allows to access the inner debug bind collector pass
390+
fn debug_binds(&mut self) -> Option<(&mut Vec<Box<dyn fmt::Debug + 'b>>, &'b DB)>;
391+
392+
/// Construct a new AstPass for collecting bind values into the provided buffer
393+
fn collect_debug_binds_pass(
394+
formatter: &'a mut Vec<Box<dyn fmt::Debug + 'b>>,
395+
backend: &'b DB,
396+
) -> AstPass<'a, 'b, DB>;
388397
}
389398

390399
impl<'a, 'b, DB> AstPassHelper<'a, 'b, DB> for AstPass<'a, 'b, DB>
@@ -442,4 +451,19 @@ where
442451
None
443452
}
444453
}
454+
455+
fn debug_binds(&mut self) -> Option<(&mut Vec<Box<dyn fmt::Debug + 'b>>, &'b DB)> {
456+
if let AstPassInternals::DebugBinds(formatter) = &mut self.internals {
457+
Some((formatter, self.backend))
458+
} else {
459+
None
460+
}
461+
}
462+
463+
fn collect_debug_binds_pass(
464+
formatter: &'a mut Vec<Box<dyn fmt::Debug + 'b>>,
465+
backend: &'b DB,
466+
) -> AstPass<'a, 'b, DB> {
467+
AstPass::debug_binds(formatter, backend)
468+
}
445469
}

diesel_derives/src/multiconnection.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,10 @@ fn generate_connection_impl(
317317
if let Some((outer_collector, lookup)) = pass.bind_collector() {
318318
C::handle_inner_pass(outer_collector, lookup, &self.backend, &self.inner)?;
319319
}
320+
if let Some((formatter, _backend)) = pass.debug_binds() {
321+
let pass = diesel::query_builder::AstPass::<MultiBackend>::collect_debug_binds_pass(formatter, &self.backend);
322+
self.inner.walk_ast(pass)?;
323+
}
320324
Ok(())
321325
}
322326
}

diesel_derives/src/tests/snapshots/diesel_derives__tests__multiconnection_1.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,12 @@ mod multi_connection_impl {
17471747
&self.inner,
17481748
)?;
17491749
}
1750+
if let Some((formatter, _backend)) = pass.debug_binds() {
1751+
let pass = diesel::query_builder::AstPass::<
1752+
MultiBackend,
1753+
>::collect_debug_binds_pass(formatter, &self.backend);
1754+
self.inner.walk_ast(pass)?;
1755+
}
17501756
Ok(())
17511757
}
17521758
}

diesel_derives/tests/multiconnection.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,3 +457,24 @@ fn nullable_type_checks() {
457457
assert!(result.12.is_none());
458458
assert!(result.13.is_none());
459459
}
460+
461+
#[test]
462+
#[cfg(not(feature = "mysql"))] // such binds are broken for mysql + multiconnection
463+
fn contains_binds() {
464+
use diesel::connection::InstrumentationEvent;
465+
466+
let mut conn = establish_connection();
467+
conn.set_instrumentation(|event: InstrumentationEvent<'_>| {
468+
if let InstrumentationEvent::StartQuery { query, .. } = event {
469+
#[cfg(any(feature = "sqlite", feature = "mysql"))]
470+
assert_eq!(query.to_string(), "SELECT ? -- binds: [1]");
471+
#[cfg(feature = "postgres")]
472+
assert_eq!(query.to_string(), "SELECT $1 -- binds: [1]");
473+
}
474+
});
475+
476+
let res = diesel::select(1.into_sql::<diesel::sql_types::Integer>())
477+
.get_result::<i32>(&mut conn)
478+
.unwrap();
479+
assert_eq!(res, 1);
480+
}

0 commit comments

Comments
 (0)