#[derive(Debug, Clone, Copy)]
pub struct Comments;
#[derive(Debug, Clone, PartialEq)]
pub struct Comment {
pub id: i64,
pub post_id: i64,
pub user_id: Option<i64>,
pub body: String,
pub created_at: chrono::NaiveDateTime,
pub rel: Rel,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Rel {
pub post: Option<Box<super::posts::Post>>,
pub user: Option<Box<super::users::User>>,
}
impl keelson_exec::FromRow for Comment {
fn from_row(row: &mut keelson_exec::Row) -> Result<Self, keelson_exec::ExecError> {
Ok(Comment {
id: row.take("id")?,
post_id: row.take("post_id")?,
user_id: row.take("user_id")?,
body: row.take("body")?,
created_at: row.take("created_at")?,
rel: Rel::default(),
})
}
}
#[derive(Debug, Clone, Default)]
pub struct Setter {
pub id: keelson_models::Set<i64>,
pub post_id: keelson_models::Set<i64>,
pub user_id: keelson_models::Set<i64>,
pub body: keelson_models::Set<String>,
pub created_at: keelson_models::Set<chrono::NaiveDateTime>,
}
pub fn table() -> keelson_models::ModelTable<Comments> {
keelson_models::ModelTable::new()
}
pub fn id() -> keelson_models::Column<i64> {
keelson_models::Column::new("comments", "id")
}
pub fn post_id() -> keelson_models::Column<i64> {
keelson_models::Column::new("comments", "post_id")
}
pub fn user_id() -> keelson_models::Column<i64> {
keelson_models::Column::new("comments", "user_id")
}
pub fn body() -> keelson_models::Column<String> {
keelson_models::Column::new("comments", "body")
}
pub fn created_at() -> keelson_models::Column<chrono::NaiveDateTime> {
keelson_models::Column::new("comments", "created_at")
}
#[allow(clippy::type_complexity)]
fn all_columns() -> (
keelson_models::Column<i64>,
keelson_models::Column<i64>,
keelson_models::Column<i64>,
keelson_models::Column<String>,
keelson_models::Column<chrono::NaiveDateTime>,
) {
(id(), post_id(), user_id(), body(), created_at())
}
impl keelson_models::View for Comments {
type Row = Comment;
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("comments")),
))
}
}
impl keelson_models::Table for Comments {
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.post_id.push_into("post_id", &mut cols, &mut vals);
s.user_id.push_into("user_id", &mut cols, &mut vals);
s.body.push_into("body", &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("comments"))
.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("comments")),
)
}
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.post_id.into_expr() {
q.apply(keelson_sqlite::update::set_col("post_id").to(v));
}
if let Some(v) = s.user_id.into_expr() {
q.apply(keelson_sqlite::update::set_col("user_id").to(v));
}
if let Some(v) = s.body.into_expr() {
q.apply(keelson_sqlite::update::set_col("body").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("comments")),
)
}
fn pk(row: &Comment) -> Self::Pk {
row.id
}
}
pub mod preload {
pub fn post() -> impl keelson_core::Mod<
keelson_models::ModelSelect<super::Comments>,
> {
keelson_core::mod_fn(|q: &mut keelson_models::ModelSelect<super::Comments>| {
use keelson_sqlite::Chain as _;
use keelson_sqlite::Mod as _;
(
keelson_sqlite::select::left_join(keelson_sqlite::quote("posts"))
.on(
keelson_sqlite::quote(("posts", "id"))
.eq(keelson_sqlite::quote(("comments", "post_id"))),
),
keelson_sqlite::select::preload_columns((
keelson_sqlite::quote(("posts", "id")).as_("post.id"),
keelson_sqlite::quote(("posts", "user_id")).as_("post.user_id"),
keelson_sqlite::quote(("posts", "title")).as_("post.title"),
keelson_sqlite::quote(("posts", "status")).as_("post.status"),
keelson_sqlite::quote(("posts", "views")).as_("post.views"),
keelson_sqlite::quote(("posts", "published_at"))
.as_("post.published_at"),
)),
)
.apply(q);
q.add_mapper_mod(
keelson_models::mapper_mod(|row, parent: &mut super::Comment| {
parent.rel.post = post_from_preload(row)?.map(Box::new);
Ok(())
}),
);
})
}
pub fn post_from_preload(
row: &mut keelson_exec::Row,
) -> Result<Option<super::super::posts::Post>, keelson_exec::ExecError> {
if matches!(row.value("post.id"), None | Some(keelson_sqlite::Value::Null)) {
return Ok(None);
}
Ok(
Some(super::super::posts::Post {
id: row.take("post.id")?,
user_id: row.take("post.user_id")?,
title: row.take("post.title")?,
status: row.take("post.status")?,
views: row.take("post.views")?,
published_at: row.take("post.published_at")?,
rel: super::super::posts::Rel::default(),
}),
)
}
pub fn user() -> impl keelson_core::Mod<
keelson_models::ModelSelect<super::Comments>,
> {
keelson_core::mod_fn(|q: &mut keelson_models::ModelSelect<super::Comments>| {
use keelson_sqlite::Chain as _;
use keelson_sqlite::Mod as _;
(
keelson_sqlite::select::left_join(keelson_sqlite::quote("users"))
.on(
keelson_sqlite::quote(("users", "id"))
.eq(keelson_sqlite::quote(("comments", "user_id"))),
),
keelson_sqlite::select::preload_columns((
keelson_sqlite::quote(("users", "id")).as_("user.id"),
keelson_sqlite::quote(("users", "name")).as_("user.name"),
keelson_sqlite::quote(("users", "email")).as_("user.email"),
keelson_sqlite::quote(("users", "age")).as_("user.age"),
keelson_sqlite::quote(("users", "is_active")).as_("user.is_active"),
keelson_sqlite::quote(("users", "created_at")).as_("user.created_at"),
)),
)
.apply(q);
q.add_mapper_mod(
keelson_models::mapper_mod(|row, parent: &mut super::Comment| {
parent.rel.user = user_from_preload(row)?.map(Box::new);
Ok(())
}),
);
})
}
pub fn user_from_preload(
row: &mut keelson_exec::Row,
) -> Result<Option<super::super::users::User>, keelson_exec::ExecError> {
if matches!(row.value("user.id"), None | Some(keelson_sqlite::Value::Null)) {
return Ok(None);
}
Ok(
Some(super::super::users::User {
id: row.take("user.id")?,
name: row.take("user.name")?,
email: row.take("user.email")?,
age: row.take("user.age")?,
is_active: row.take("user.is_active")?,
created_at: row.take("user.created_at")?,
rel: super::super::users::Rel::default(),
}),
)
}
}
pub mod then_load {
pub fn post() -> keelson_models::ThenLoad<
super::Comments,
super::super::posts::Posts,
i64,
> {
keelson_models::ThenLoad::new(
|rows: &[super::Comment]| rows.iter().map(|r| r.post_id).collect(),
|keys, q| keelson_core::Mod::apply(super::super::posts::id().in_(keys), q),
|rows: &mut [super::Comment], related| {
keelson_models::attach_to_one(
rows,
related,
|r| r.post_id,
|c| c.id,
|r, c| {
r.rel.post = c.map(Box::new);
},
);
},
)
}
pub fn user() -> keelson_models::ThenLoad<
super::Comments,
super::super::users::Users,
i64,
> {
keelson_models::ThenLoad::new(
|rows: &[super::Comment]| rows.iter().filter_map(|r| r.user_id).collect(),
|keys, q| keelson_core::Mod::apply(super::super::users::id().in_(keys), q),
|rows: &mut [super::Comment], related| {
keelson_models::attach_to_one(
rows,
related,
|r| r.user_id,
|c| Some(c.id),
|r, c| {
r.rel.user = c.map(Box::new);
},
);
},
)
}
}