Trait Schema

Source
pub trait Schema {
    type Id: Copy + Send + Sync;

    // Required methods
    fn table_name() -> &'static str;
    fn id(&self) -> Self::Id;
    fn id_column() -> &'static str;
    fn columns() -> &'static [&'static str];
    fn select_sql() -> &'static str;
    fn select_by_id_sql() -> &'static str;
    fn insert_sql() -> &'static str;
    fn update_by_id_sql() -> &'static str;
    fn delete_by_id_sql() -> &'static str;
}
Expand description

Database schema information about a struct implementing sqlx FromRow. Schema defines methods for accessing the derived database schema and query information.

This trait is implemented by the [SqlxCrud] derive macro.

§Example

use sqlx::FromRow;
use sqlx_crud::SqlxCrud;

#[derive(FromRow, SqlxCrud)]
pub struct User {
    user_id: i32,
    name: String,
}

Required Associated Types§

Source

type Id: Copy + Send + Sync

Type of the table primary key column.

Required Methods§

Source

fn table_name() -> &'static str

Database name of the table. Used by the query generation code and available for introspection. This is generated by taking the plural snake_case of the struct’s name. See: Inflector to_table_case.

use sqlx::FromRow;
use sqlx_crud::{Schema, SqlxCrud};

#[derive(FromRow, SqlxCrud)]
struct GoogleIdToken {
    id: i32,
    audience: String,
}

assert_eq!("google_id_tokens", GoogleIdToken::table_name());
Source

fn id(&self) -> Self::Id

Returns the id of the current instance.

Source

fn id_column() -> &'static str

Returns the column name of the primary key.

Source

fn columns() -> &'static [&'static str]

Returns an array of column names.

Source

fn select_sql() -> &'static str

Returns the SQL string for a SELECT query against the table.

§Example
use sqlx_crud::Schema;

assert_eq!(r#"SELECT "users"."user_id", "users"."name" FROM "users""#, User::select_sql());
Source

fn select_by_id_sql() -> &'static str

Returns the SQL string for a SELECT query against the table with a WHERE clause for the primary key.

§Example
use sqlx_crud::Schema;

assert_eq!(
    r#"SELECT "users"."user_id", "users"."name" FROM "users" WHERE "users"."user_id" = ? LIMIT 1"#,
    User::select_by_id_sql()
);
Source

fn insert_sql() -> &'static str

Returns the SQL for inserting a new record in to the database. The #[external_id] attribute may be used to specify IDs are assigned outside of the database.

§Example
use sqlx::FromRow;
use sqlx_crud::{Schema, SqlxCrud};

#[derive(Debug, FromRow, SqlxCrud)]
#[external_id]
pub struct UserExternalId {
    pub user_id: i32,
    pub name: String,
}

assert_eq!(r#"INSERT INTO "users" ("name") VALUES (?) RETURNING "users"."user_id", "users"."name""#, User::insert_sql());
assert_eq!(r#"INSERT INTO "user_external_ids" ("user_id", "name") VALUES (?, ?) RETURNING "user_external_ids"."user_id", "user_external_ids"."name""#, UserExternalId::insert_sql());
Source

fn update_by_id_sql() -> &'static str

Returns the SQL for updating an existing record in the database.

§Example
use sqlx_crud::Schema;

assert_eq!(r#"UPDATE "users" SET "name" = ? WHERE "users"."user_id" = ? RETURNING "users"."user_id", "users"."name""#, User::update_by_id_sql());
Source

fn delete_by_id_sql() -> &'static str

Returns the SQL for deleting an existing record by ID from the database.

§Example
use sqlx_crud::Schema;

assert_eq!(r#"DELETE FROM "users" WHERE "users"."user_id" = ?"#, User::delete_by_id_sql());

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§