diff --git a/Cargo.toml b/Cargo.toml index 3476c16..8facfce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ edition.workspace = true description = "Entity CRUD and change tracking for SQL databases with SQLx" [workspace.package] -version = "0.3.4" +version = "0.3.5" edition = "2021" [dependencies] @@ -28,7 +28,7 @@ members = [ [features] default = [] derive = ["dep:sqlx-record-derive"] -static-validation = ["sqlx-record-derive?/static-validation"] +static-check = ["sqlx-record-derive?/static-check"] decimal = ["dep:rust_decimal", "sqlx/rust_decimal"] # Database backends - user must enable at least one diff --git a/sqlx-record-derive/Cargo.toml b/sqlx-record-derive/Cargo.toml index fd03f6a..2c48368 100644 --- a/sqlx-record-derive/Cargo.toml +++ b/sqlx-record-derive/Cargo.toml @@ -13,7 +13,7 @@ futures = "0.3" [features] default = [] -static-validation = [] +static-check = [] mysql = [] postgres = [] sqlite = [] diff --git a/sqlx-record-derive/src/lib.rs b/sqlx-record-derive/src/lib.rs index 858e01b..78b2e5f 100644 --- a/sqlx-record-derive/src/lib.rs +++ b/sqlx-record-derive/src/lib.rs @@ -106,7 +106,7 @@ fn table_quote() -> &'static str { { "`" } } -/// Get compile-time placeholder for static-validation SQL +/// Get compile-time placeholder for static-check SQL fn static_placeholder(index: usize) -> String { #[cfg(feature = "postgres")] { format!("${}", index) } @@ -414,6 +414,22 @@ fn generate_insert_impl( ) }; + #[cfg(not(any(feature = "mysql", feature = "postgres", feature = "sqlite")))] + let upsert_stmt = { + // Fallback to MySQL syntax + let update_clause = non_pk_fields.iter() + .map(|f| format!("{} = VALUES({})", f, f)) + .collect::>() + .join(", "); + format!( + "INSERT INTO {}{}{} ({}) VALUES ({}) ON DUPLICATE KEY UPDATE {}", + #tq, #table_name, #tq, + vec![#(#db_names),*].join(", "), + placeholders, + update_clause + ) + }; + sqlx::query(&upsert_stmt) #(.bind(#bindings))* .execute(executor) @@ -573,8 +589,8 @@ fn generate_get_impl( let field_list = fields.iter().map(|f| f.db_name.clone()).collect::>(); - // Check if static-validation feature is enabled at macro expansion time - let use_static_validation = cfg!(feature = "static-validation"); + // Check if static-check feature is enabled at macro expansion time + let use_static_validation = cfg!(feature = "static-check"); let get_by_impl = if use_static_validation { // Static validation: use sqlx::query_as! with compile-time checked SQL @@ -1169,8 +1185,8 @@ fn generate_update_impl( /// Bind all form values to query in correct order. /// Handles both simple values and expression values, respecting expression precedence. - pub fn bind_all_values(&self, mut query: sqlx::query::Query<'_, #db, #db_args>) - -> sqlx::query::Query<'_, #db, #db_args> + pub fn bind_all_values<'q>(&'q self, mut query: sqlx::query::Query<'q, #db, #db_args>) + -> sqlx::query::Query<'q, #db, #db_args> { #( // Expression takes precedence over simple value