From b61f15156a000c3f156af3d0fc809ca32912af34 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Sun, 13 May 2018 23:58:30 -0700 Subject: add scratch rust stuff --- rust/src/bin/show_creators.rs | 24 +++++++++++++++ rust/src/lib.rs | 18 ++++++++++++ rust/src/schema.rs | 68 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 rust/src/bin/show_creators.rs create mode 100644 rust/src/lib.rs create mode 100644 rust/src/schema.rs (limited to 'rust/src') diff --git a/rust/src/bin/show_creators.rs b/rust/src/bin/show_creators.rs new file mode 100644 index 00000000..160ca3c7 --- /dev/null +++ b/rust/src/bin/show_creators.rs @@ -0,0 +1,24 @@ + +extern crate fc; +extern crate diesel; + +use self::fatcat_rs::*; +use self::models::*; +use self::diesel::prelude::*; + +fn main() { + use diesel_demo::schema::creators::dsl::*; + + let connection = establish_connection(); + let results = creators.filter(published.eq(true)) + .limit(5) + .load::(&connection) + .expect("Error loading creators"); + + println!("Displaying {} creators", results.len()); + for creator in results { + println!("{}", creator.title); + println!("----------\n"); + println!("{}", creator.body); + } +} diff --git a/rust/src/lib.rs b/rust/src/lib.rs new file mode 100644 index 00000000..679b9ed3 --- /dev/null +++ b/rust/src/lib.rs @@ -0,0 +1,18 @@ + +#[macro_use] +extern crate diesel; +extern crate dotenv; + +use diesel::prelude::*; +use diesel::pg::PgConnection; +use dotenv::dotenv; +use std::env; + +pub fn establish_connection() -> PgConnection { + dotenv().ok(); + + let database_url = env::var("DATABASE_URL") + .expect("DATABASE_URL must be set"); + PgConnection::establish(&database_url) + .expect(&format!("Error connecting to {}", database_url)) +} diff --git a/rust/src/schema.rs b/rust/src/schema.rs new file mode 100644 index 00000000..7bef7dcd --- /dev/null +++ b/rust/src/schema.rs @@ -0,0 +1,68 @@ +table! { + changelog (id) { + id -> Int8, + editgroup_id -> Int8, + timestamp -> Nullable, + } +} + +table! { + creator_edit (id) { + id -> Int8, + extra_json -> Nullable, + ident_id -> Uuid, + rev_id -> Nullable, + redirect_id -> Nullable, + editgroup_id -> Int8, + } +} + +table! { + creator_ident (id) { + id -> Uuid, + is_live -> Bool, + rev_id -> Nullable, + redirect_id -> Nullable, + } +} + +table! { + creator_rev (id) { + id -> Int8, + extra_json -> Nullable, + name -> Nullable, + orcid -> Nullable, + } +} + +table! { + editgroup (id) { + id -> Int8, + extra_json -> Nullable, + editor_id -> Int8, + description -> Nullable, + } +} + +table! { + editor (id) { + id -> Int8, + username -> Text, + is_admin -> Bool, + active_editgroup_id -> Nullable, + } +} + +joinable!(changelog -> editgroup (editgroup_id)); +joinable!(creator_edit -> creator_rev (rev_id)); +joinable!(creator_edit -> editgroup (editgroup_id)); +joinable!(creator_ident -> creator_rev (rev_id)); + +allow_tables_to_appear_in_same_query!( + changelog, + creator_edit, + creator_ident, + creator_rev, + editgroup, + editor, +); -- cgit v1.2.3