Skip to main content

IntoParam

Trait IntoParam 

Source
pub trait IntoParam {
    // Required method
    fn into_param(self) -> Param;
}
Expand description

Types that can be bound as D1 query parameters.

Implemented for text (&str, String, &String), bool (INTEGER 1/0), f64, integers up to 32 bits, i64 (see MAX_SAFE_INTEGER), serde_json::Value (stored as compact JSON text), Option<T> (None is NULL) and Param itself. params! calls it on each value; implement it for your own types (e.g. an enum stored as text) by delegating to one of these.

§Examples

use ocre::{IntoParam, Param, params};

enum Status {
    Draft,
    Published,
}

impl IntoParam for Status {
    fn into_param(self) -> Param {
        match self {
            Status::Draft => "draft",
            Status::Published => "published",
        }
        .into_param()
    }
}

assert_eq!(params![Status::Published], params!["published"]);

Required Methods§

Source

fn into_param(self) -> Param

Converts the value into a bound parameter.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl IntoParam for &String

Source§

impl IntoParam for &str

Source§

impl IntoParam for String

Source§

impl IntoParam for Value

JSON values are stored as their compact text in a TEXT column (the generators add CHECK (json_valid(<column>))). Read them back with #[serde(deserialize_with = "ocre::json_from_sql")].

Source§

impl IntoParam for bool

SQLite has no boolean type: true/false are stored as INTEGER 1/0. Read them back with #[serde(deserialize_with = "ocre::bool_from_sql")].

Source§

impl IntoParam for f64

Source§

impl IntoParam for i8

Source§

impl IntoParam for i16

Source§

impl IntoParam for i32

Source§

impl IntoParam for i64

Integers within ±(2^53 - 1) are bound as numbers. Larger values are bound as decimal text, exact for comparisons in WHERE clauses.

Source§

impl IntoParam for u8

Source§

impl IntoParam for u16

Source§

impl IntoParam for u32

Source§

impl<T: IntoParam> IntoParam for Option<T>

Implementors§

Source§

impl IntoParam for Deterministic

Binds the encrypted text. Without installed keys (no valid SECRET_KEY_BASE) it binds NULL, which a NOT NULL column refuses; the error is logged.

Source§

impl IntoParam for Encrypted

Binds the encrypted text. Without installed keys (no valid SECRET_KEY_BASE) it binds NULL, which a NOT NULL column refuses; the error is logged.

Source§

impl IntoParam for Param