pub trait ToSqlParam: Send + Sync {
// Required methods
fn encode_param(&self) -> Option<Vec<u8>>;
fn to_sql_literal(&self) -> String;
// Provided methods
fn param_format(&self) -> ParamFormat { ... }
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, and with
the struct-mapping variants
fetch_one_as_params,
fetch_all_as_params, and
stream_as_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 the wire bytes for a bound parameter.
Returns None to represent a SQL NULL value.
Returns Some(bytes) with the encoded value otherwise.
The bytes must match whatever Self::param_format returns —
big-endian PostgreSQL binary for ParamFormat::Binary (the
default), UTF-8 SQL text without surrounding quotes for
ParamFormat::Text.
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 param_format(&self) -> ParamFormat
fn param_format(&self) -> ParamFormat
Returns the wire format Self::encode_param produced.
Defaults to ParamFormat::Binary. Override it only for types
Hyper cannot read in binary — a scaled Numeric and Geography
are the two built-in cases.
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.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".