feat: add SQLite support behind a sqlite Cargo feature

Adds a SqliteDialect alongside PostgresDialect, gated by mutually exclusive
postgres (default) / sqlite Cargo features, plus a feature-selected
ActiveDatabase type alias used throughout the public Georm/Defaultable
traits. Basic CRUD and Defaultable are fully covered by a new SQLite test
suite (tests/sqlite/*) running against a real SQLite database, alongside
split migrations/sqlite/*.

Two real dialect differences surfaced beyond placeholder syntax:
- SQLite's INTEGER columns type-infer as i64 in sqlx's compile-time query
  macros (unlike Postgres SERIAL -> i32), so the SQLite test models use i64
  for integer PK/FK fields.
- sqlx's SQLite query macros need bind arguments bound to a place rather
  than passed as an inline expression (fixed once, centrally, in
  SqlDialect::generate_relation_lookup).

Composite-key entities with chrono::DateTime columns (tests/composite_key.rs)
are not yet ported to SQLite: SQLite has no TIMESTAMPTZ equivalent, so sqlx
infers TEXT date/time columns as String rather than DateTime<Utc> unless the
query explicitly overrides the column type. That needs per-field type
overrides threaded through the generated queries, left as follow-up.

CI now matrixes over both backends; formatting/audit only run once since
they don't vary by backend. examples/postgres/* stays out of
--no-default-features builds by scoping sqlite lint/test just recipes to
`-p georm` (it depends on georm's default postgres feature via Cargo
feature unification, so building it under --features sqlite at the
workspace level double-activates both dialects).
This commit is contained in:
Claude
2026-07-21 19:06:35 +00:00
parent 0d43a3fe13
commit 4c314c2f0b
31 changed files with 802 additions and 35 deletions
+4
View File
@@ -10,6 +10,10 @@ repository.workspace = true
[lib]
proc-macro = true
[features]
postgres = []
sqlite = []
[dependencies]
deluxe = "0.5.0"
proc-macro2 = "1.0.93"
+7 -2
View File
@@ -10,6 +10,7 @@
//! `Defaultable` trait.
use crate::georm::ir::GeneratedType;
use crate::georm::sql::{self, SqlDialect};
use super::ir::{GeormField, GeormStructAttributes};
use quote::quote;
@@ -92,11 +93,15 @@ fn generate_defaultable_trait_impl(
});
}
let database = sql::DIALECT.database_type();
let index_var = syn::Ident::new("i", proc_macro2::Span::call_site());
let placeholder_expr = sql::DIALECT.runtime_placeholder(&index_var);
quote! {
impl ::georm::Defaultable<#id_type, #struct_name> for #defaultable_struct_name {
async fn create<'e, E>(&self, mut executor: E) -> ::sqlx::Result<#struct_name>
where
E: ::sqlx::Executor<'e, Database = ::sqlx::Postgres>
E: ::sqlx::Executor<'e, Database = #database>
{
let mut dynamic_fields = Vec::new();
@@ -106,7 +111,7 @@ fn generate_defaultable_trait_impl(
all_fields.extend(dynamic_fields);
let placeholders: Vec<String> = (1..=all_fields.len())
.map(|i| format!("${}", i))
.map(|#index_var| #placeholder_expr)
.collect();
let query = format!(
+25 -2
View File
@@ -4,10 +4,22 @@ use crate::georm::ir::GeormField;
use quote::quote;
mod postgres;
pub use postgres::PostgresDialect;
mod sqlite;
#[cfg(feature = "postgres")]
pub use postgres::PostgresDialect;
#[cfg(feature = "sqlite")]
pub use sqlite::SqliteDialect;
#[cfg(feature = "postgres")]
pub type ActiveDialect = PostgresDialect;
#[cfg(feature = "sqlite")]
pub type ActiveDialect = SqliteDialect;
#[cfg(feature = "postgres")]
pub const DIALECT: ActiveDialect = PostgresDialect;
#[cfg(feature = "sqlite")]
pub const DIALECT: ActiveDialect = SqliteDialect;
/// How a relation-lookup query should fetch its result.
pub enum FetchKind {
@@ -29,6 +41,13 @@ pub trait SqlDialect {
fn database_type(&self) -> proc_macro2::TokenStream;
fn row_type(&self) -> proc_macro2::TokenStream;
/// Like [`SqlDialect::placeholder`], but for placeholder lists whose
/// length is only known at runtime (the defaultable-struct insert, which
/// varies its column count based on which `Option` fields are `Some`).
/// `index_var` names the runtime `usize` loop variable holding the 1-based
/// position of the placeholder being built.
fn runtime_placeholder(&self, index_var: &syn::Ident) -> proc_macro2::TokenStream;
fn generate_from_row(
&self,
struct_name: &syn::Ident,
@@ -325,7 +344,11 @@ pub trait SqlDialect {
where
E: ::sqlx::Executor<'e, Database = #database>
{
::sqlx::query_as!(#entity, #query, #arg).#fetch_method(executor).await
// Bound to a place rather than passed inline: sqlx's SQLite
// query macros need the bind argument to outlive the
// temporary produced by expressions like `self.get_id()`.
let arg = #arg;
::sqlx::query_as!(#entity, #query, arg).#fetch_method(executor).await
}
}
}
+4
View File
@@ -15,4 +15,8 @@ impl SqlDialect for PostgresDialect {
fn row_type(&self) -> proc_macro2::TokenStream {
quote! { ::sqlx::postgres::PgRow }
}
fn runtime_placeholder(&self, index_var: &syn::Ident) -> proc_macro2::TokenStream {
quote! { format!("${}", #index_var) }
}
}
+29
View File
@@ -0,0 +1,29 @@
use super::SqlDialect;
use quote::quote;
pub struct SqliteDialect;
impl SqlDialect for SqliteDialect {
fn placeholder(&self, _index: usize) -> String {
// SQLite accepts unnumbered `?` placeholders resolved in positional
// (left-to-right) order, which matches the order Georm binds values in.
"?".to_string()
}
fn database_type(&self) -> proc_macro2::TokenStream {
quote! { ::sqlx::Sqlite }
}
fn row_type(&self) -> proc_macro2::TokenStream {
quote! { ::sqlx::sqlite::SqliteRow }
}
fn runtime_placeholder(&self, index_var: &syn::Ident) -> proc_macro2::TokenStream {
quote! {
{
let _ = #index_var;
"?".to_string()
}
}
}
}