pub trait ToSqlParam: Send + Sync {
// Required methods
fn encode_param(&self) -> Option<Vec<u8>>;
fn to_sql_literal(&self) -> String;
// Provided method
fn sql_oid(&self) -> Oid { ... }
}Expand description
Trait for types that can be used as parameters in parameterized SQL queries.
This trait enables type-safe parameter encoding for use with
Connection::query_params and
Connection::command_params.
§Implementing for Custom Types
You can implement this trait for custom types:
impl ToSqlParam for MyType {
fn encode_param(&self) -> Option<Vec<u8>> {
Some(self.to_bytes())
}
fn to_sql_literal(&self) -> String {
format!("'{}'", self.to_string().replace('\'', "''"))
}
}Required Methods§
Sourcefn encode_param(&self) -> Option<Vec<u8>>
fn encode_param(&self) -> Option<Vec<u8>>
Encodes this value as binary bytes for use in parameterized queries.
Returns None to represent a SQL NULL value.
Returns Some(bytes) with the binary-encoded value otherwise.
Sourcefn to_sql_literal(&self) -> String
fn to_sql_literal(&self) -> String
Returns the SQL literal representation of this value.
Retained for building DDL statement strings that cannot use
parameterized queries (e.g. escape_sql_path in catalog code).
The parameterized-query path in
Connection::query_params
no longer uses this method — parameters travel as binary bytes
via encode_param.
Provided Methods§
Sourcefn sql_oid(&self) -> Oid
fn sql_oid(&self) -> Oid
Returns the SQL type OID this parameter should bind as.
The default returns Oid(0) (unspecified) which asks the server
to infer the type from surrounding SQL context. That works for
clauses like WHERE column = $1 where the column type is known,
but not for INSERT INTO t VALUES ($1, $2) — those require the
caller (or the trait impl) to return a concrete OID.
All built-in ToSqlParam impls override this with a concrete
value from hyperdb_api_core::types::oids.