From cdf46b6a5246b8d750bb7aba60acebca11507540 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Fri, 7 Sep 2018 14:21:38 -0700 Subject: generic create (multiple inserts) This does away entirely with the fancy CTE-based custom SQL inserts. Because performance is currently only important for batch inserts, I think this is acceptable, and will refactor to do batch inserts as actual batch SQL operations next. --- rust/src/database_entity_crud.rs | 50 +++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 26 deletions(-) (limited to 'rust/src') diff --git a/rust/src/database_entity_crud.rs b/rust/src/database_entity_crud.rs index 7a5f333d..24d5277c 100644 --- a/rust/src/database_entity_crud.rs +++ b/rust/src/database_entity_crud.rs @@ -92,6 +92,26 @@ macro_rules! generic_db_get_rev { } } +macro_rules! generic_db_create { + ($ident_table: ident, $edit_table: ident) => { + fn db_create(&self, conn: &DbConn, edit_context: &EditContext) -> Result { + let rev_id = self.db_insert_rev(conn)?; + let ident: Uuid = insert_into($ident_table::table) + .values($ident_table::rev_id.eq(&rev_id)) + .returning($ident_table::id) + .get_result(conn)?; + let edit: Self::EditRow = insert_into($edit_table::table) + .values(( + $edit_table::editgroup_id.eq(edit_context.editgroup_id.to_uuid()), + $edit_table::rev_id.eq(&rev_id), + $edit_table::ident_id.eq(&ident), + )) + .get_result(conn)?; + Ok(edit) + } + } +} + macro_rules! generic_db_create_batch { () => { fn db_create_batch(conn: &DbConn, edit_context: &EditContext, models: &[Self]) -> Result> { @@ -196,31 +216,12 @@ impl EntityCrud for WorkEntity { generic_parse_editgroup_id!(); generic_db_get!(work_ident, work_rev); generic_db_get_rev!(work_rev); + generic_db_create!(work_ident, work_edit); generic_db_create_batch!(); generic_db_update!(work_ident, work_edit); generic_db_delete!(work_ident, work_edit); generic_db_get_history!(work_edit); - fn db_create(&self, conn: &DbConn, edit_context: &EditContext) -> Result { - - let rev_id = self.db_insert_rev(conn)?; - let edit: WorkEditRow = - diesel::sql_query( - "WITH ident AS ( - INSERT INTO work_ident (rev_id) - VALUES ($1) - RETURNING id ) - INSERT INTO work_edit (editgroup_id, ident_id, rev_id, extra) VALUES - ($2, (SELECT ident.id FROM ident), $1, $3) - RETURNING *", - ).bind::(rev_id) - .bind::(edit_context.editgroup_id.to_uuid()) - .bind::, _>(&edit_context.extra_json) - .get_result(conn)?; - - Ok(edit) - } - fn db_from_row(conn: &DbConn, rev_row: Self::RevRow, ident_row: Option) -> Result { let (state, ident_id, redirect_id) = match ident_row { @@ -243,16 +244,13 @@ impl EntityCrud for WorkEntity { } fn db_insert_rev(&self, conn: &DbConn) -> Result { - let rev_row: WorkRevRow = insert_into(work_rev::table) + let rev_row: Uuid = insert_into(work_rev::table) .values(( work_rev::extra_json.eq(&self.extra) )) - .get_result(conn)?; - /* TODO: only return id .returning(work_rev::id) - .first(conn)?; - */ - Ok(rev_row.id) + .get_result(conn)?; + Ok(rev_row) } } -- cgit v1.2.3