keelson-gen 0.1.0

keelson's code generator: introspect a live schema, emit readable model .rs files against keelson-models.
Documentation
// @generated by keelson-gen. DO NOT EDIT.
// Regenerate from the schema instead; hand-written code (hooks included)
// belongs outside this directory.

/// The model marker `users::table()` hangs off.
#[derive(Debug, Clone, Copy)]
pub struct Users;
/// One row of `users`.
#[derive(Debug, Clone, PartialEq)]
pub struct User {
    pub id: i64,
    pub name: String,
    pub email: Option<String>,
    pub age: Option<i64>,
    pub is_active: bool,
    pub created_at: chrono::NaiveDateTime,
    /// Relations, filled by `preload`/`then_load` mods; empty otherwise.
    pub rel: Rel,
}
/// `users`' relations.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Rel {
    /// Has-many `comments`, via `comments.user_id`.
    pub comments: Vec<super::comments::Comment>,
    /// Has-many `post_authors`, via `post_authors.user_id`.
    pub post_authors: Vec<super::post_authors::PostAuthor>,
    /// Has-many `posts`, via `posts.user_id`.
    pub posts: Vec<super::posts::Post>,
    /// Has-one `user_emails`, via `user_emails.id`.
    pub user_emails: Option<Box<super::user_emails::UserEmail>>,
}
impl keelson_exec::FromRow for User {
    fn from_row(row: &mut keelson_exec::Row) -> Result<Self, keelson_exec::ExecError> {
        Ok(User {
            id: row.take("id")?,
            name: row.take("name")?,
            email: row.take("email")?,
            age: row.take("age")?,
            is_active: row.take("is_active")?,
            created_at: row.take("created_at")?,
            rel: Rel::default(),
        })
    }
}
/// The three-state setter: unset fields stay out of the statement.
#[derive(Debug, Clone, Default)]
pub struct Setter {
    pub id: keelson_models::Set<i64>,
    pub name: keelson_models::Set<String>,
    pub email: keelson_models::Set<String>,
    pub age: keelson_models::Set<i64>,
    pub is_active: keelson_models::Set<bool>,
    pub created_at: keelson_models::Set<chrono::NaiveDateTime>,
}
/// The entry point: `users::table().query(…)` / `.insert(…)` / ….
pub fn table() -> keelson_models::ModelTable<Users> {
    keelson_models::ModelTable::new()
}
pub fn id() -> keelson_models::Column<i64> {
    keelson_models::Column::new("users", "id")
}
pub fn name() -> keelson_models::Column<String> {
    keelson_models::Column::new("users", "name")
}
pub fn email() -> keelson_models::Column<String> {
    keelson_models::Column::new("users", "email")
}
pub fn age() -> keelson_models::Column<i64> {
    keelson_models::Column::new("users", "age")
}
pub fn is_active() -> keelson_models::Column<bool> {
    keelson_models::Column::new("users", "is_active")
}
pub fn created_at() -> keelson_models::Column<chrono::NaiveDateTime> {
    keelson_models::Column::new("users", "created_at")
}
#[allow(clippy::type_complexity)]
fn all_columns() -> (
    keelson_models::Column<i64>,
    keelson_models::Column<String>,
    keelson_models::Column<String>,
    keelson_models::Column<i64>,
    keelson_models::Column<bool>,
    keelson_models::Column<chrono::NaiveDateTime>,
) {
    (id(), name(), email(), age(), is_active(), created_at())
}
impl keelson_models::View for Users {
    type Row = User;
    type Select = keelson_sqlite::SelectQuery;
    fn base_select() -> Self::Select {
        keelson_sqlite::select((
            keelson_sqlite::select::columns(all_columns()),
            keelson_sqlite::select::from(keelson_sqlite::quote("users")),
        ))
    }
}
impl keelson_models::Table for Users {
    type Pk = i64;
    type Setter = Setter;
    type Insert = keelson_sqlite::InsertQuery;
    type Update = keelson_sqlite::UpdateQuery;
    type Delete = keelson_sqlite::DeleteQuery;
    fn insert_query(s: Setter) -> Self::Insert {
        let mut cols: Vec<&'static str> = Vec::new();
        let mut vals: Vec<keelson_core::expr::Expr> = Vec::new();
        s.id.push_into("id", &mut cols, &mut vals);
        s.name.push_into("name", &mut cols, &mut vals);
        s.email.push_into("email", &mut cols, &mut vals);
        s.age.push_into("age", &mut cols, &mut vals);
        s.is_active.push_into("is_active", &mut cols, &mut vals);
        s.created_at.push_into("created_at", &mut cols, &mut vals);
        let mut q = keelson_sqlite::insert((
            keelson_sqlite::insert::into(keelson_sqlite::quote("users")).columns(cols),
            keelson_sqlite::insert::returning(all_columns()),
        ));
        if !vals.is_empty() {
            q.apply(keelson_sqlite::insert::values(vals));
        }
        q
    }
    fn update_query() -> Self::Update {
        keelson_sqlite::update(
            keelson_sqlite::update::table(keelson_sqlite::quote("users")),
        )
    }
    fn apply_setter(s: Setter, q: &mut Self::Update) {
        if let Some(v) = s.id.into_expr() {
            q.apply(keelson_sqlite::update::set_col("id").to(v));
        }
        if let Some(v) = s.name.into_expr() {
            q.apply(keelson_sqlite::update::set_col("name").to(v));
        }
        if let Some(v) = s.email.into_expr() {
            q.apply(keelson_sqlite::update::set_col("email").to(v));
        }
        if let Some(v) = s.age.into_expr() {
            q.apply(keelson_sqlite::update::set_col("age").to(v));
        }
        if let Some(v) = s.is_active.into_expr() {
            q.apply(keelson_sqlite::update::set_col("is_active").to(v));
        }
        if let Some(v) = s.created_at.into_expr() {
            q.apply(keelson_sqlite::update::set_col("created_at").to(v));
        }
    }
    fn delete_query() -> Self::Delete {
        keelson_sqlite::delete(
            keelson_sqlite::delete::from(keelson_sqlite::quote("users")),
        )
    }
    fn pk(row: &User) -> Self::Pk {
        row.id
    }
    /// Delegates to the application's `crate::hooks::users::before_insert` (configured in `[tables.users] hooks`).
    fn before_insert<'a>(
        db: &'a dyn keelson_exec::Executor,
        setter: &'a mut Setter,
    ) -> keelson_exec::ExecFuture<'a, Result<(), keelson_exec::ExecError>> {
        crate::hooks::users::before_insert(db, setter)
    }
    /// Delegates to the application's `crate::hooks::users::after_insert` (configured in `[tables.users] hooks`).
    fn after_insert<'a>(
        db: &'a dyn keelson_exec::Executor,
        rows: &'a [User],
    ) -> keelson_exec::ExecFuture<'a, Result<(), keelson_exec::ExecError>> {
        crate::hooks::users::after_insert(db, rows)
    }
}
/// Then-load mods: one keyed, batched query per level of a load path — `then_load::a().then(b::then_load::c())` is two levels, two queries, checked by the compiler.
pub mod then_load {
    /// Load each row's `comments` (to-many), one keyed query per batch of keys — `.then(…)` hangs the next level of the path off this one.
    pub fn comments() -> keelson_models::ThenLoad<
        super::Users,
        super::super::comments::Comments,
        i64,
    > {
        keelson_models::ThenLoad::new(
            |rows: &[super::User]| rows.iter().map(|r| r.id).collect(),
            |keys, q| keelson_core::Mod::apply(
                super::super::comments::user_id().in_(keys),
                q,
            ),
            |rows: &mut [super::User], related| {
                keelson_models::attach_to_many(
                    rows,
                    related,
                    |r| Some(r.id),
                    |c| c.user_id,
                    |r, cs| {
                        r.rel.comments = cs;
                    },
                );
            },
        )
    }
    /// Load each row's `post_authors` (to-many), one keyed query per batch of keys — `.then(…)` hangs the next level of the path off this one.
    pub fn post_authors() -> keelson_models::ThenLoad<
        super::Users,
        super::super::post_authors::PostAuthors,
        i64,
    > {
        keelson_models::ThenLoad::new(
            |rows: &[super::User]| rows.iter().map(|r| r.id).collect(),
            |keys, q| keelson_core::Mod::apply(
                super::super::post_authors::user_id().in_(keys),
                q,
            ),
            |rows: &mut [super::User], related| {
                keelson_models::attach_to_many(
                    rows,
                    related,
                    |r| Some(r.id),
                    |c| c.user_id,
                    |r, cs| {
                        r.rel.post_authors = cs;
                    },
                );
            },
        )
    }
    /// Load each row's `posts` (to-many), one keyed query per batch of keys — `.then(…)` hangs the next level of the path off this one.
    pub fn posts() -> keelson_models::ThenLoad<
        super::Users,
        super::super::posts::Posts,
        i64,
    > {
        keelson_models::ThenLoad::new(
            |rows: &[super::User]| rows.iter().map(|r| r.id).collect(),
            |keys, q| keelson_core::Mod::apply(
                super::super::posts::user_id().in_(keys),
                q,
            ),
            |rows: &mut [super::User], related| {
                keelson_models::attach_to_many(
                    rows,
                    related,
                    |r| r.id,
                    |c| c.user_id,
                    |r, cs| {
                        r.rel.posts = cs;
                    },
                );
            },
        )
    }
    /// Load each row's `user_emails` (to-one), one keyed query per batch of keys — `.then(…)` hangs the next level of the path off this one.
    pub fn user_emails() -> keelson_models::ThenLoad<
        super::Users,
        super::super::user_emails::UserEmails,
        i64,
    > {
        keelson_models::ThenLoad::new(
            |rows: &[super::User]| rows.iter().map(|r| r.id).collect(),
            |keys, q| keelson_core::Mod::apply(
                super::super::user_emails::id().in_(keys),
                q,
            ),
            |rows: &mut [super::User], related| {
                keelson_models::attach_to_one(
                    rows,
                    related,
                    |r| Some(r.id),
                    |c| c.id,
                    |r, c| {
                        r.rel.user_emails = c.map(Box::new);
                    },
                );
            },
        )
    }
}