Skip to main content

function

Macro function 

Source
macro_rules! function {
    (
        $func_name:ident($($param_name:ident: $param_type:ident),* $(,)?) -> $return_type:ident {
            $(language: $language:ident,)?
            $(volatility: $volatility:ident,)?
            $(security: $security:ident,)?
            $(strict: $strict:ident,)?
            body: $body:expr
        }
    ) => { ... };
    (
        $func_name:ident($($param_name:ident: $param_type:ident),* $(,)?) -> Record {
            out: {
                $($out_param_name:ident: $out_param_type:ident),* $(,)?
            },
            $(language: $language:ident,)?
            $(volatility: $volatility:ident,)?
            $(security: $security:ident,)?
            $(strict: $strict:ident,)?
            body: $body:expr
        }
    ) => { ... };
    (@pg_type Integer) => { ... };
    (@pg_type Int4) => { ... };
    (@pg_type Int8) => { ... };
    (@pg_type Int2) => { ... };
    (@pg_type Text) => { ... };
    (@pg_type Varchar) => { ... };
    (@pg_type Bool) => { ... };
    (@pg_type Date) => { ... };
    (@pg_type Timestamptz) => { ... };
    (@pg_type Timestamp) => { ... };
    (@pg_type Numeric) => { ... };
    (@pg_type Real) => { ... };
    (@pg_type DoublePrecision) => { ... };
    (@pg_type Uuid) => { ... };
    (@pg_type Json) => { ... };
    (@pg_type Jsonb) => { ... };
    (@pg_type Record) => { ... };
    (@language) => { ... };
    (@language plpgsql) => { ... };
    (@language sql) => { ... };
    (@language python) => { ... };
    (@language c) => { ... };
    (@volatility) => { ... };
    (@volatility volatile) => { ... };
    (@volatility stable) => { ... };
    (@volatility immutable) => { ... };
    (@security) => { ... };
    (@security invoker) => { ... };
    (@security definer) => { ... };
    (@strict) => { ... };
    (@strict true) => { ... };
    (@strict false) => { ... };
}
Expand description

Macro for defining PostgreSQL stored functions

ยงExample

use ignis_db::function;

// Simple function with return type
function! {
    calculate_age(birth_date: Date) -> Integer {
        language: plpgsql,
        volatility: immutable,
        body: r#"
            BEGIN
                RETURN EXTRACT(YEAR FROM AGE(birth_date));
            END;
        "#
    }
}

// Function with multiple OUT parameters
function! {
    get_user_stats(user_id: Integer) -> Record {
        out: {
            total_posts: Integer,
            last_login: Timestamptz
        },
        language: plpgsql,
        body: r#"
            BEGIN
                SELECT COUNT(*), MAX(login_time)
                INTO total_posts, last_login
                FROM user_activity
                WHERE user_id = user_id;
            END;
        "#
    }
}