#[derive(Debug, Clone, Copy)]
pub struct Threads;
#[derive(Debug, Clone, PartialEq)]
pub struct Thread {
pub id: i64,
pub title: String,
pub first_message_id: Option<i64>,
pub rel: Rel,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Rel {
pub first_message: Option<Box<super::messages::Message>>,
pub messages: Vec<super::messages::Message>,
}
impl keelson_exec::FromRow for Thread {
fn from_row(row: &mut keelson_exec::Row) -> Result<Self, keelson_exec::ExecError> {
Ok(Thread {
id: row.take("id")?,
title: row.take("title")?,
first_message_id: row.take("first_message_id")?,
rel: Rel::default(),
})
}
}
#[derive(Debug, Clone, Default)]
pub struct Setter {
pub id: keelson_models::Set<i64>,
pub title: keelson_models::Set<String>,
pub first_message_id: keelson_models::Set<i64>,
}
pub fn table() -> keelson_models::ModelTable<Threads> {
keelson_models::ModelTable::new()
}
pub fn id() -> keelson_models::Column<i64> {
keelson_models::Column::new("threads", "id")
}
pub fn title() -> keelson_models::Column<String> {
keelson_models::Column::new("threads", "title")
}
pub fn first_message_id() -> keelson_models::Column<i64> {
keelson_models::Column::new("threads", "first_message_id")
}
#[allow(clippy::type_complexity)]
fn all_columns() -> (
keelson_models::Column<i64>,
keelson_models::Column<String>,
keelson_models::Column<i64>,
) {
(id(), title(), first_message_id())
}
impl keelson_models::View for Threads {
type Row = Thread;
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("threads")),
))
}
}
impl keelson_models::Table for Threads {
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.title.push_into("title", &mut cols, &mut vals);
s.first_message_id.push_into("first_message_id", &mut cols, &mut vals);
let mut q = keelson_sqlite::insert((
keelson_sqlite::insert::into(keelson_sqlite::quote("threads")).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("threads")),
)
}
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.title.into_expr() {
q.apply(keelson_sqlite::update::set_col("title").to(v));
}
if let Some(v) = s.first_message_id.into_expr() {
q.apply(keelson_sqlite::update::set_col("first_message_id").to(v));
}
}
fn delete_query() -> Self::Delete {
keelson_sqlite::delete(
keelson_sqlite::delete::from(keelson_sqlite::quote("threads")),
)
}
fn pk(row: &Thread) -> Self::Pk {
row.id
}
}
pub mod preload {
pub fn first_message() -> impl keelson_core::Mod<
keelson_models::ModelSelect<super::Threads>,
> {
keelson_core::mod_fn(|q: &mut keelson_models::ModelSelect<super::Threads>| {
use keelson_sqlite::Chain as _;
use keelson_sqlite::Mod as _;
(
keelson_sqlite::select::left_join(keelson_sqlite::quote("messages"))
.on(
keelson_sqlite::quote(("messages", "id"))
.eq(keelson_sqlite::quote(("threads", "first_message_id"))),
),
keelson_sqlite::select::preload_columns((
keelson_sqlite::quote(("messages", "id")).as_("first_message.id"),
keelson_sqlite::quote(("messages", "thread_id"))
.as_("first_message.thread_id"),
keelson_sqlite::quote(("messages", "body")).as_("first_message.body"),
)),
)
.apply(q);
q.add_mapper_mod(
keelson_models::mapper_mod(|row, parent: &mut super::Thread| {
parent.rel.first_message = first_message_from_preload(row)?
.map(Box::new);
Ok(())
}),
);
})
}
pub fn first_message_from_preload(
row: &mut keelson_exec::Row,
) -> Result<Option<super::super::messages::Message>, keelson_exec::ExecError> {
if matches!(
row.value("first_message.id"), None | Some(keelson_sqlite::Value::Null)
) {
return Ok(None);
}
Ok(
Some(super::super::messages::Message {
id: row.take("first_message.id")?,
thread_id: row.take("first_message.thread_id")?,
body: row.take("first_message.body")?,
rel: super::super::messages::Rel::default(),
}),
)
}
}
pub mod then_load {
pub fn first_message() -> keelson_models::ThenLoad<
super::Threads,
super::super::messages::Messages,
i64,
> {
keelson_models::ThenLoad::new(
|rows: &[super::Thread]| {
rows.iter().filter_map(|r| r.first_message_id).collect()
},
|keys, q| keelson_core::Mod::apply(
super::super::messages::id().in_(keys),
q,
),
|rows: &mut [super::Thread], related| {
keelson_models::attach_to_one(
rows,
related,
|r| r.first_message_id,
|c| Some(c.id),
|r, c| {
r.rel.first_message = c.map(Box::new);
},
);
},
)
}
pub fn messages() -> keelson_models::ThenLoad<
super::Threads,
super::super::messages::Messages,
i64,
> {
keelson_models::ThenLoad::new(
|rows: &[super::Thread]| rows.iter().map(|r| r.id).collect(),
|keys, q| keelson_core::Mod::apply(
super::super::messages::thread_id().in_(keys),
q,
),
|rows: &mut [super::Thread], related| {
keelson_models::attach_to_many(
rows,
related,
|r| r.id,
|c| c.thread_id,
|r, cs| {
r.rel.messages = cs;
},
);
},
)
}
}