Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

225 changes: 225 additions & 0 deletions src-tauri/src/drivers/postgres/binding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
use super::helpers::{
escape_identifier, extract_base_type, is_raw_sql_function, is_wkt_geometry,
json_array_to_pg_literal, try_parse_pg_array,
};
use tokio_postgres::types::ToSql;

pub(super) type PgParam = Box<dyn ToSql + Send + Sync>;

pub(super) struct BoundValue {
pub sql: String,
pub param: Option<PgParam>,
}

pub(super) struct PgValueOptions<'a> {
pub column_type: Option<&'a str>,
pub max_blob_size: u64,
pub allow_default: bool,
}

/// Build a parameterized "<pk_col> = $N" predicate plus the boxed parameter for the
/// given JSON pk_val. Numeric values are cast through bigint/double precision so the
/// bind succeeds against int2/int4/int8/real columns; UUID strings are bound as the
/// `Uuid` type so PostgreSQL receives the matching OID.
pub(super) fn build_pk_predicate(
pk_col: &str,
pk_val: serde_json::Value,
placeholder_idx: usize,
) -> Result<(String, PgParam), String> {
let col = format!("\"{}\"", escape_identifier(pk_col));
match pk_val {
serde_json::Value::Number(n) => {
let bound = bind_pg_number(&n, placeholder_idx)?;
let param = bound
.param
.ok_or_else(|| "Internal PostgreSQL numeric binding error".to_string())?;
Ok((format!("{} = {}", col, bound.sql), param))
}
serde_json::Value::String(s) => {
if let Ok(uuid) = s.parse::<uuid::Uuid>() {
Ok((format!("{} = ${}", col, placeholder_idx), Box::new(uuid)))
} else {
Ok((format!("{} = ${}", col, placeholder_idx), Box::new(s)))
}
}
_ => Err("Unsupported PK type".into()),
}
}

pub(super) fn bind_pg_value(
value: serde_json::Value,
placeholder_idx: usize,
options: PgValueOptions<'_>,
) -> Result<BoundValue, String> {
match value {
serde_json::Value::Number(n) => bind_pg_number(&n, placeholder_idx),
serde_json::Value::String(s) => bind_pg_string(&s, placeholder_idx, options),
serde_json::Value::Bool(b) => Ok(BoundValue {
sql: format!("${}", placeholder_idx),
param: Some(Box::new(b)),
}),
serde_json::Value::Null => Ok(BoundValue {
sql: "NULL".to_string(),
param: None,
}),
serde_json::Value::Array(arr) => Ok(BoundValue {
sql: json_array_to_pg_literal(&arr)?,
param: None,
}),
_ => Err("Unsupported Value type".into()),
}
}

/// SQL fragment + boxed parameter for a JSON Number bound to PostgreSQL.
///
/// tokio-postgres binds Rust `i64` as INT8 and `f64` as FLOAT8, and rejects the
/// bind when the column is INT2/INT4/REAL with "error serializing parameter X".
/// Wrapping the placeholder in `CAST($N AS bigint)` / `CAST($N AS double precision)`
/// lets PostgreSQL convert to the actual column width via its assignment / implicit
/// comparison casts.
pub(super) fn bind_pg_number(
n: &serde_json::Number,
placeholder_idx: usize,
) -> Result<BoundValue, String> {
if let Some(v) = n.as_i64() {
Ok(BoundValue {
sql: format!("CAST(${} AS bigint)", placeholder_idx),
param: Some(Box::new(v)),
})
} else if let Some(v) = n.as_f64() {
Ok(BoundValue {
sql: format!("CAST(${} AS double precision)", placeholder_idx),
param: Some(Box::new(v)),
})
} else {
Err(format!("Unsupported numeric value: {}", n))
}
}

pub(super) fn bind_pg_numeric_string(
s: &str,
column_type: &str,
placeholder_idx: usize,
) -> Option<Result<BoundValue, String>> {
let trimmed = s.trim();
let normalized = extract_base_type(column_type).to_lowercase();

if matches!(
normalized.as_str(),
"smallint" | "integer" | "bigint" | "int2" | "int4" | "int8" | "serial" | "bigserial"
) {
return Some(trimmed.parse::<i64>().map_or_else(
|e| {
Err(format!(
"Cannot convert value {:?} to PostgreSQL numeric column type {}: {}",
s, column_type, e
))
},
|v| {
Ok(BoundValue {
sql: format!("CAST(${} AS bigint)", placeholder_idx),
param: Some(Box::new(v) as PgParam),
})
},
));
}

if matches!(normalized.as_str(), "numeric" | "decimal") {
return Some(trimmed.parse::<rust_decimal::Decimal>().map_or_else(
|e| {
Err(format!(
"Cannot convert value {:?} to PostgreSQL numeric column type {}: {}",
s, column_type, e
))
},
|v| {
Ok(BoundValue {
sql: format!("CAST(${} AS numeric)", placeholder_idx),
param: Some(Box::new(v) as PgParam),
})
},
));
}

if matches!(
normalized.as_str(),
"real" | "double precision" | "float4" | "float8"
) {
return Some(trimmed.parse::<f64>().map_or_else(
|e| {
Err(format!(
"Cannot convert value {:?} to PostgreSQL numeric column type {}: {}",
s, column_type, e
))
},
|v| {
Ok(BoundValue {
sql: format!("CAST(${} AS double precision)", placeholder_idx),
param: Some(Box::new(v) as PgParam),
})
},
));
}

None
}

fn bind_pg_string(
s: &str,
placeholder_idx: usize,
options: PgValueOptions<'_>,
) -> Result<BoundValue, String> {
if options.allow_default && s == "__USE_DEFAULT__" {
return Ok(BoundValue {
sql: "DEFAULT".to_string(),
param: None,
});
}

if let Some(bytes) = crate::drivers::common::decode_blob_wire_format(s, options.max_blob_size) {
return Ok(BoundValue {
sql: format!("${}", placeholder_idx),
param: Some(Box::new(bytes)),
});
}

if let Some(binding) = options
.column_type
.and_then(|data_type| bind_pg_numeric_string(s, data_type, placeholder_idx))
{
return binding;
}

if is_raw_sql_function(s) {
return Ok(BoundValue {
sql: s.to_string(),
param: None,
});
}

if is_wkt_geometry(s) {
return Ok(BoundValue {
sql: format!("ST_GeomFromText(${})", placeholder_idx),
param: Some(Box::new(s.to_string())),
});
}

if s.parse::<uuid::Uuid>().is_ok() {
return Ok(BoundValue {
sql: format!("CAST(${} AS uuid)", placeholder_idx),
param: Some(Box::new(s.to_string())),
});
}

if let Some(pg_arr) = try_parse_pg_array(s) {
return Ok(BoundValue {
sql: pg_arr?,
param: None,
});
}

Ok(BoundValue {
sql: format!("${}", placeholder_idx),
param: Some(Box::new(s.to_string())),
})
}
Loading