journio_postgres/dialect.rs
1//! Postgres dialect — ported 1:1 from `postgresDialect` in `journio/dialect.go`.
2//!
3//! CockroachDB (`cockroachDialect` in Go) is wire-compatible and shares this
4//! dialect; the only differences are `supports_listen_notify() == false` and a
5//! runtime probe, neither of which affects SQL generation. That split is added
6//! when the conductor / polling paths land.
7
8use journio_core::dialect::{Dialect, DialectName};
9
10/// Postgres-flavoured dialect. Ported from `postgresDialect` (`dialect.go:181`).
11#[derive(Debug, Default, Clone, Copy)]
12pub struct PostgresDialect;
13
14impl Dialect for PostgresDialect {
15 fn name(&self) -> DialectName {
16 DialectName::Postgres
17 }
18
19 /// `"journio".` — mirrors `pgx.Identifier{schema}.Sanitize() + "."`.
20 fn schema_prefix(&self, schema: &str) -> String {
21 format!("\"{}\".", schema)
22 }
23
24 /// Canonical queries are already Postgres syntax — no-op.
25 /// Ported from `postgresDialect.RewriteQuery`.
26 fn rewrite_query(&self, query: &str) -> String {
27 query.to_string()
28 }
29
30 fn lock_skip_locked(&self) -> &str {
31 "FOR UPDATE SKIP LOCKED"
32 }
33
34 fn lock_nowait(&self) -> &str {
35 "FOR UPDATE NOWAIT"
36 }
37
38 fn supports_listen_notify(&self) -> bool {
39 true
40 }
41
42 fn supports_array_parameters(&self) -> bool {
43 true
44 }
45
46 fn supports_data_modifying_cte(&self) -> bool {
47 true
48 }
49}
50
51/// Quote an identifier the way `pgx.Identifier{}.Sanitize()` does — used when
52/// rendering schema prefixes outside the `Dialect` trait (e.g. migrations).
53pub fn sanitize_identifier(ident: &str) -> String {
54 // pgx wraps in double quotes and doubles any embedded double-quote.
55 format!("\"{}\"", ident.replace('"', "\"\""))
56}