Trait diesel::backend::Backend

source ·
pub trait Backendwhere
    Self: Sized + SqlDialect + TypeMetadata + HasSqlType<SmallInt> + HasSqlType<Integer> + HasSqlType<BigInt> + HasSqlType<Float> + HasSqlType<Double> + HasSqlType<Text> + HasSqlType<Binary> + HasSqlType<Date> + HasSqlType<Time> + HasSqlType<Timestamp>,{
    type QueryBuilder: QueryBuilder<Self>;
    type RawValue<'a>;
    type BindCollector<'a>: BindCollector<'a, Self> + 'a;
Expand description

A database backend

This trait represents the concept of a backend (e.g. “MySQL” vs “SQLite”). It is separate from a Connection to that backend. One backend may have multiple concrete connection implementations.

Implementing a custom backend

Implementing a custom backend requires enabling the i-implement-a-third-party-backend-and-opt-into-breaking-changes crate feature to get access to all necessary type and trait implementations.

Implementations of this trait should not assume details about how the connection is implemented. For example, the Pg backend does not assume that libpq is being used. Implementations of this trait can and should care about details of the wire protocol used to communicate with the database.

Implementing support for a new backend is a complex topic and depends on the details how the newly implemented backend may communicate with diesel. As of this, we cannot provide concrete examples here and only present a general outline of the required steps. Existing backend implementations provide a good starting point to see how certain things are solved for other backend implementations.

Types implementing Backend should generally be zero sized structs.

To implement the Backend trait, you need to:

  • Specify how a query should be build from string parts by providing a QueryBuilder matching your backend
  • Specify the bind value format used by your database connection library by providing a BindCollector matching your backend
  • Specify how values are received from the database by providing a corresponding raw value definition
  • Control sql dialect specific parts of diesels query dsl implementation by providing a matching SqlDialect implementation
  • Implement TypeMetadata to specify how your backend identifies types
  • Specify support for common datatypes by implementing HasSqlType for the following sql types:

Additionally to the listed required trait bounds you may want to implement DieselReserveSpecialization to opt in existing wild card QueryFragment impls for large parts of the dsl.

Required Associated Types§


type QueryBuilder: QueryBuilder<Self>

The concrete QueryBuilder implementation for this backend.


type RawValue<'a>

The actual type given to FromSql, with lifetimes applied. This type should not be used directly.


type BindCollector<'a>: BindCollector<'a, Self> + 'a

The concrete BindCollector implementation for this backend.

Most backends should use RawBytesBindCollector.

Object Safety§

This trait is not object safe.



impl Backend for Mysql

Available on crate feature mysql_backend only.

impl Backend for Pg

Available on crate feature postgres_backend only.

impl Backend for Sqlite

Available on crate feature sqlite only.

type QueryBuilder = SqliteQueryBuilder


type RawValue<'a> = SqliteValue<'a, 'a, 'a>


type BindCollector<'a> = SqliteBindCollector<'a>