2021-07-23 08:14:45 +08:00
|
|
|
use crate::{
|
2021-09-23 15:49:10 +08:00
|
|
|
errors::{internal_error, DocError},
|
2021-07-23 08:14:45 +08:00
|
|
|
sql_tables::doc::{DocTable, DocTableChangeset},
|
|
|
|
};
|
|
|
|
use flowy_database::{
|
|
|
|
prelude::*,
|
|
|
|
schema::{doc_table, doc_table::dsl},
|
2021-09-23 15:49:10 +08:00
|
|
|
ConnectionPool,
|
2021-09-09 15:43:05 +08:00
|
|
|
SqliteConnection,
|
2021-07-23 08:14:45 +08:00
|
|
|
};
|
2021-09-23 15:49:10 +08:00
|
|
|
use std::sync::Arc;
|
2021-09-09 17:34:01 +08:00
|
|
|
|
2021-09-09 15:43:05 +08:00
|
|
|
pub struct DocTableSql {}
|
2021-07-23 08:14:45 +08:00
|
|
|
|
|
|
|
impl DocTableSql {
|
2021-09-09 15:43:05 +08:00
|
|
|
pub(crate) fn create_doc_table(&self, doc_table: DocTable, conn: &SqliteConnection) -> Result<(), DocError> {
|
|
|
|
let _ = diesel::insert_into(doc_table::table).values(doc_table).execute(conn)?;
|
2021-07-23 08:14:45 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-26 20:04:47 +08:00
|
|
|
pub(crate) fn update_doc_table(
|
|
|
|
&self,
|
|
|
|
changeset: DocTableChangeset,
|
|
|
|
conn: &SqliteConnection,
|
|
|
|
) -> Result<(), DocError> {
|
2021-09-09 15:43:05 +08:00
|
|
|
diesel_update_table!(doc_table, changeset, conn);
|
2021-07-23 08:14:45 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-23 15:49:10 +08:00
|
|
|
pub(crate) fn read_doc_table(&self, doc_id: &str, pool: Arc<ConnectionPool>) -> Result<DocTable, DocError> {
|
|
|
|
let conn = &*pool.get().map_err(internal_error)?;
|
2021-09-26 20:04:47 +08:00
|
|
|
let doc_table = dsl::doc_table
|
|
|
|
.filter(doc_table::id.eq(doc_id))
|
|
|
|
.first::<DocTable>(conn)?;
|
2021-07-23 08:14:45 +08:00
|
|
|
|
|
|
|
Ok(doc_table)
|
|
|
|
}
|
|
|
|
|
2021-08-30 16:18:58 +08:00
|
|
|
#[allow(dead_code)]
|
2021-09-09 15:43:05 +08:00
|
|
|
pub(crate) fn delete_doc(&self, doc_id: &str, conn: &SqliteConnection) -> Result<DocTable, DocError> {
|
2021-09-26 20:04:47 +08:00
|
|
|
let doc_table = dsl::doc_table
|
|
|
|
.filter(doc_table::id.eq(doc_id))
|
|
|
|
.first::<DocTable>(conn)?;
|
2021-09-09 15:43:05 +08:00
|
|
|
diesel_delete_table!(doc_table, doc_id, conn);
|
|
|
|
Ok(doc_table)
|
|
|
|
}
|
2021-07-23 08:14:45 +08:00
|
|
|
}
|