drizzle_core/dialect.rs
1//! Dialect type re-exported from drizzle-types with core-specific extensions.
2
3use crate::prelude::*;
4
5/// Re-export the unified Dialect enum from drizzle-types
6pub use drizzle_types::Dialect;
7
8/// Extension trait for Dialect-specific placeholder rendering
9pub trait DialectExt {
10 /// Renders a placeholder for this dialect with the given 1-based index.
11 ///
12 /// Returns `Cow::Borrowed("?")` for SQLite/MySQL (zero allocation),
13 /// `Cow::Owned` for PostgreSQL numbered placeholders.
14 ///
15 /// # Examples
16 /// - PostgreSQL: `$1`, `$2`, `$3`
17 /// - SQLite/MySQL: `?`
18 fn render_placeholder(&self, index: usize) -> Cow<'static, str>;
19
20 /// Appends a placeholder directly into an output buffer.
21 #[inline]
22 fn write_placeholder(&self, index: usize, out: &mut String) {
23 match self.render_placeholder(index) {
24 Cow::Borrowed(v) => out.push_str(v),
25 Cow::Owned(v) => out.push_str(&v),
26 }
27 }
28}
29
30impl DialectExt for Dialect {
31 #[inline]
32 fn render_placeholder(&self, index: usize) -> Cow<'static, str> {
33 match self {
34 Dialect::PostgreSQL => Cow::Owned(format!("${}", index)),
35 Dialect::SQLite | Dialect::MySQL => Cow::Borrowed("?"),
36 }
37 }
38}