Skip to main content

ToSqlParam

Trait ToSqlParam 

Source
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§

Source

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.

Source

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§

Source

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.

Implementations on Foreign Types§

Source§

impl ToSqlParam for &str

Source§

impl ToSqlParam for bool

Source§

impl ToSqlParam for f32

Source§

impl ToSqlParam for f64

Source§

impl ToSqlParam for i16

Source§

impl ToSqlParam for i32

Source§

impl ToSqlParam for i64

Source§

impl ToSqlParam for str

Source§

impl ToSqlParam for String

Source§

impl ToSqlParam for Vec<u8>

Source§

impl ToSqlParam for [u8]

Source§

impl<T: ToSqlParam> ToSqlParam for Option<T>

Source§

impl<T: ToSqlParam> ToSqlParam for &T

Implementors§