Macro diesel::sql_function [] [src]

macro_rules! sql_function {
    ($(#$meta:tt)* fn $fn_name:ident $args:tt $(;)*) => { ... };
    ($(#$meta:tt)* fn $fn_name:ident $args:tt -> $return_type:ty $(;)*) => { ... };
        fn $fn_name:ident
    ) => { ... };
    ($fn_name:ident, $struct_name:ident, $args:tt -> $return_type:ty) => { ... };
    ($fn_name:ident, $struct_name:ident, $args:tt -> $return_type:ty, $docs:expr) => { ... };
    ($fn_name:ident, $struct_name:ident, ($($arg_name:ident: $arg_type:ty),*)) => { ... };
        $args:tt -> $return_type:ty,
    ) => { ... };

Declare a sql function for use in your code.

Diesel only provides support for a very small number of SQL functions. This macro enables you to add additional functions from the SQL standard, as well as any custom functions your application might have.

The syntax for this macro is very similar to that of a normal Rust function, except the argument and return types will be the SQL types being used. Typically these types will come from [diesel::sql_types].

This macro will generate two items. A function with the name that you've given, and a module with a helper type representing the return type of your function. For example, this invocation:

This example is not tested
sql_function!(fn lower(x: Text) -> Text);

will generate this code:

This example is not tested
pub fn lower<X>(x: X) -> lower::HelperType<X> {

pub(crate) mod lower {
    pub type HelperType<X> = ...;

If you are using this macro for part of a library, where the function is part of your public API, it is highly recommended that you re-export this helper type with the same name as your function. This is the standard structure:

This example is not tested
pub mod functions {
    use super::types::*;
    use diesel::sql_types::*;

    sql_function! {
        /// Represents the Pg `LENGTH` function used with `tsvector`s.
        fn length(x: TsVector) -> Integer;

pub mod helper_types {
    /// The return type of `length(expr)`
    pub type Length<Expr> = functions::length::HelperType<Expr>;

pub mod dsl {
    pub use functions::*;
    pub use helper_types::*;

Most attributes given to this macro will be put on the generated function (including doc comments).

Adding Doc Comments

use diesel::sql_types::Text;

sql_function! {
    /// Represents the `canon_crate_name` SQL function, created in
    /// migration ....
    fn canon_crate_name(a: Text) -> Text;

let target_name = "diesel";
// This will generate the following SQL
// SELECT * FROM crates WHERE canon_crate_name( = canon_crate_name($1)

Special Attributes

There are a handful of special attributes that Diesel will recognize. They are:

Functions can also be generic. Take the definition of sum for an example of all of this:

use diesel::sql_types::Foldable;

sql_function! {
    #[sql_name = "SUM"]
    fn sum<ST: Foldable>(expr: ST) -> ST::Sum;

Use with SQLite

On most backends, the implementation of the function is defined in a migration using CREATE FUNCTION. On SQLite, the function is implemented in Rust instead. You must call register_impl or register_nondeterministic_impl with every connection before you can use the function.

These functions will only be generated if the sqlite feature is enabled, and the function is not generic. Generic functions and variadic functions are not supported on SQLite.

use diesel::sql_types::{Integer, Double};
sql_function!(fn add_mul(x: Integer, y: Integer, z: Double) -> Double);

let connection = SqliteConnection::establish(":memory:")?;

add_mul::register_impl(&connection, |x: i32, y: i32, z: f64| {
    (x + y) as f64 * z

let result = select(add_mul(1, 2, 1.5))
assert_eq!(4.5, result);