Trait diesel::query_builder::QueryId[][src]

pub trait QueryId {
    type QueryId: Any;

    const HAS_STATIC_QUERY_ID: bool;
    fn query_id() -> Option<TypeId> { ... }
Expand description

Uniquely identifies queries by their type for the purpose of prepared statement caching.

All types which implement QueryFragment should also implement this trait (It is not an actual supertrait of QueryFragment for boxing purposes).

See the documentation of the QueryId type and HAS_STATIC_QUERY_ID for more details.


This trait can be automatically derived by Diesel. For example, given this struct:

pub struct And<Left, Right> {
    left: Left,
    right: Right,

the following implementation will be generated

impl<Left, Right> QueryId for And<Left, Right>
    Left: QueryId,
    Right: QueryId,
    type QueryId = And<Left::QueryId, Right::QueryId>;


If the SQL generated by a struct is not uniquely identifiable by its type, meaning that HAS_STATIC_QUERY_ID should always be false, you should not derive this trait. In that case you should manually implement it instead.

Associated Types

A type which uniquely represents Self in a SQL query.

Typically this will be a re-construction of Self using the QueryId type of each of your type parameters. For example, the type And<Left, Right> would have type QueryId = And<Left::QueryId, Right::QueryId>.

The exception to this is when one of your type parameters does not affect whether the same prepared statement can be used or not. For example, a bind parameter is represented as Bound<SqlType, RustType>. The actual Rust type we are serializing does not matter for the purposes of prepared statement reuse, but a query which has identical SQL but different types for its bind parameters requires a new prepared statement. For this reason, Bound would have type QueryId = Bound<SqlType::QueryId, ()>.

If HAS_STATIC_QUERY_ID is false, you can put any type here (typically ()).

Associated Constants

Can the SQL generated by Self be uniquely identified by its type?

Typically this question can be answered by looking at whether unsafe_to_cache_prepared is called in your implementation of QueryFragment::walk_ast. In Diesel itself, the only type which has false here, but is potentially safe to store in the prepared statement cache is a boxed query.

Provided methods

Returns the type id of Self::QueryId if Self::HAS_STATIC_QUERY_ID. Returns None otherwise.

You should never need to override this method.

Implementations on Foreign Types