diff options
author | Melody Horn <melody@boringcactus.com> | 2021-06-30 15:58:48 -0600 |
---|---|---|
committer | Melody Horn <melody@boringcactus.com> | 2021-06-30 15:58:48 -0600 |
commit | b4c7c3cd480e626ac78b49ab91a95340ccfef26a (patch) | |
tree | c1966362001b67b80e8f54a7a9f94be8a47aaf7c /src | |
parent | bdfdcb98d39d1887b08748e1ea629f0a83f340a4 (diff) | |
download | tosin-b4c7c3cd480e626ac78b49ab91a95340ccfef26a.tar.gz tosin-b4c7c3cd480e626ac78b49ab91a95340ccfef26a.zip |
finish inserting save_mut for models
Diffstat (limited to 'src')
-rw-r--r-- | src/db/backend/sqlite.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/db/backend/sqlite.rs b/src/db/backend/sqlite.rs index 002d0d3..eb4468c 100644 --- a/src/db/backend/sqlite.rs +++ b/src/db/backend/sqlite.rs @@ -1,6 +1,7 @@ use diesel::connection::Connection as _; pub use barrel::backend::Sqlite as SqlGenerator; +pub use diesel::sqlite::Sqlite as Backend; pub use diesel::sqlite::SqliteConnection as Connection; pub struct Database { @@ -20,3 +21,47 @@ impl Default for Database { } } } + +use diesel::{ + no_arg_sql_function, no_arg_sql_function_body, no_arg_sql_function_body_except_to_sql, QueryId, +}; +no_arg_sql_function!( + last_insert_rowid, + diesel::sql_types::BigInt, + "Represents the SQL last_insert_rowid() function" +); + +use diesel::{ + query_builder::{AsQuery, InsertStatement, QueryFragment, QueryId}, + query_dsl::methods::{FilterDsl, LoadQuery}, + sql_types::BigInt, + Column, Expression, Insertable, Queryable, +}; + +pub fn insert_and_retrieve<'a, Row, Table, Id>( + to_insert: &'a Row, + table: Table, + connection: &Connection, + id: Id, +) -> Row +where + &'a Row: Insertable<Table>, + Row: Queryable<Table::SqlType, Backend>, + Table: diesel::Table + AsQuery + Copy, + InsertStatement<Table, <&'a Row as Insertable<Table>>::Values>: QueryFragment<Backend>, + <Table as AsQuery>::Query: QueryFragment<Backend> + + QueryId + + FilterDsl<diesel::expression::operators::Eq<Id, last_insert_rowid>>, + Id: Column + Expression<SqlType = BigInt>, + <Table as FilterDsl<diesel::expression::operators::Eq<Id, last_insert_rowid>>>::Output: + LoadQuery<Connection, Row>, +{ + use diesel::prelude::*; + to_insert + .insert_into(table) + .execute(connection) + .expect("error saving to database"); // TODO propagate error + diesel::QueryDsl::filter(table, id.eq(last_insert_rowid)) + .get_result(connection) + .expect("error loading from database") // TODO propagate error +} |