├── .gitignore ├── Cargo.toml ├── README.md ├── build.rs ├── migrations ├── .gitkeep └── 20160202154039_create_posts │ ├── down.sql │ └── up.sql └── src ├── bin ├── delete_post.rs ├── publish_post.rs ├── show_posts.rs └── write_post.rs ├── lib.in.rs ├── lib.rs ├── models.rs └── schema.rs /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "diesel_demo" 3 | version = "0.1.0" 4 | authors = ["Sean Griffin "] 5 | build = "build.rs" 6 | 7 | [build-dependencies] 8 | syntex = { version = "0.38.0", optional = true } 9 | diesel_codegen_syntex = { version = "0.7.0", features = ["postgres"], optional = true } 10 | dotenv_codegen = { version = "0.9.0", optional = true } 11 | 12 | [dependencies] 13 | diesel = "0.7.0" 14 | diesel_codegen = { version = "0.7.0", features = ["postgres"], optional = true } 15 | dotenv = "0.8.0" 16 | dotenv_macros = { version = "0.9.0", optional = true } 17 | 18 | [features] 19 | default = ["nightly"] 20 | with-syntex = ["syntex", "diesel_codegen_syntex", "dotenv_codegen"] 21 | nightly = ["diesel/unstable", "diesel_codegen", "dotenv_macros"] 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Diesel Demo 2 | =========== 3 | 4 | This repo is deprecated. Please use the code from https://github.com/diesel-rs/diesel/tree/e9ab20402df7b4979d73c908f7819d0993740667/examples instead. 5 | -------------------------------------------------------------------------------- /build.rs: -------------------------------------------------------------------------------- 1 | #[cfg(feature = "with-syntex")] 2 | fn main() { 3 | extern crate syntex; 4 | extern crate diesel_codegen; 5 | extern crate dotenv_codegen; 6 | 7 | use std::env; 8 | use std::path::Path; 9 | 10 | let out_dir = env::var_os("OUT_DIR").unwrap(); 11 | let mut registry = syntex::Registry::new(); 12 | diesel_codegen::register(&mut registry); 13 | dotenv_codegen::register(&mut registry); 14 | 15 | let src = Path::new("src/lib.in.rs"); 16 | let dst = Path::new(&out_dir).join("lib.rs"); 17 | 18 | registry.expand("", &src, &dst).unwrap(); 19 | } 20 | 21 | #[cfg(feature = "nightly")] 22 | fn main() {} 23 | -------------------------------------------------------------------------------- /migrations/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sgrif/diesel_demo/98b0ad08d9d41d3aafe6e4e9bc87a0b2e94febdc/migrations/.gitkeep -------------------------------------------------------------------------------- /migrations/20160202154039_create_posts/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE posts 2 | -------------------------------------------------------------------------------- /migrations/20160202154039_create_posts/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE posts ( 2 | id SERIAL PRIMARY KEY, 3 | title VARCHAR NOT NULL, 4 | body TEXT NOT NULL, 5 | published BOOLEAN NOT NULL DEFAULT 'f' 6 | ) 7 | -------------------------------------------------------------------------------- /src/bin/delete_post.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel_demo; 2 | extern crate diesel; 3 | 4 | use self::diesel::prelude::*; 5 | use self::diesel_demo::*; 6 | use std::env::args; 7 | 8 | fn main() { 9 | use diesel_demo::schema::posts::dsl::*; 10 | 11 | let target = args().nth(1).expect("Expected a target to match against"); 12 | let pattern = format!("%{}%", target); 13 | 14 | let connection = establish_connection(); 15 | let num_deleted = diesel::delete(posts.filter(title.like(pattern))) 16 | .execute(&connection) 17 | .expect("Error deleting posts"); 18 | 19 | println!("Deleted {} posts", num_deleted); 20 | } 21 | -------------------------------------------------------------------------------- /src/bin/publish_post.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel_demo; 2 | extern crate diesel; 3 | 4 | use self::diesel::prelude::*; 5 | use self::diesel_demo::*; 6 | use self::diesel_demo::models::Post; 7 | use std::env::args; 8 | 9 | fn main() { 10 | use diesel_demo::schema::posts::dsl::{posts, published}; 11 | 12 | let id = args().nth(1).expect("publish_post requires a post id") 13 | .parse::().expect("Invalid ID"); 14 | let connection = establish_connection(); 15 | 16 | let post = diesel::update(posts.find(id)) 17 | .set(published.eq(true)) 18 | .get_result::(&connection) 19 | .expect(&format!("Unable to find post {}", id)); 20 | println!("Published post {}", post.title); 21 | } 22 | -------------------------------------------------------------------------------- /src/bin/show_posts.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel_demo; 2 | extern crate diesel; 3 | 4 | use self::diesel_demo::*; 5 | use self::diesel_demo::models::*; 6 | use self::diesel::prelude::*; 7 | 8 | fn main() { 9 | use diesel_demo::schema::posts::dsl::*; 10 | 11 | let connection = establish_connection(); 12 | let results = posts.filter(published.eq(true)) 13 | .limit(5) 14 | .load::(&connection) 15 | .expect("Error loading posts"); 16 | 17 | println!("Displaying {} posts", results.len()); 18 | for post in results { 19 | println!("{}", post.title); 20 | println!("----------\n"); 21 | println!("{}", post.body); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/bin/write_post.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel_demo; 2 | extern crate diesel; 3 | 4 | use self::diesel_demo::*; 5 | use std::io::{stdin, Read}; 6 | 7 | fn main() { 8 | let connection = establish_connection(); 9 | 10 | println!("What would you like your title to be?"); 11 | let mut title = String::new(); 12 | stdin().read_line(&mut title).unwrap(); 13 | let title = &title[..(title.len() - 1)]; // Drop the newline character 14 | println!("\nOk! Let's write {} (Press {} when finished)\n", title, EOF); 15 | let mut body = String::new(); 16 | stdin().read_to_string(&mut body).unwrap(); 17 | 18 | let post = create_post(&connection, title, &body); 19 | println!("\nSaved draft {} with id {}", title, post.id); 20 | } 21 | 22 | #[cfg(not(windows))] 23 | const EOF: &'static str = "CTRL+D"; 24 | 25 | #[cfg(windows)] 26 | const EOF: &'static str = "CTRL+Z"; 27 | -------------------------------------------------------------------------------- /src/lib.in.rs: -------------------------------------------------------------------------------- 1 | pub mod schema; 2 | pub mod models; 3 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![cfg_attr(feature = "nightly", feature(custom_derive, custom_attribute, plugin))] 2 | #![cfg_attr(feature = "nightly", plugin(diesel_codegen, dotenv_macros))] 3 | 4 | #[macro_use] 5 | extern crate diesel; 6 | extern crate dotenv; 7 | 8 | #[cfg(feature = "nightly")] 9 | include!("lib.in.rs"); 10 | 11 | #[cfg(feature = "with-syntex")] 12 | include!(concat!(env!("OUT_DIR"), "/lib.rs")); 13 | 14 | use diesel::prelude::*; 15 | use diesel::pg::PgConnection; 16 | use dotenv::dotenv; 17 | use std::env; 18 | 19 | pub fn establish_connection() -> PgConnection { 20 | dotenv().ok(); 21 | 22 | let database_url = env::var("DATABASE_URL") 23 | .expect("DATABASE_URL must be set"); 24 | PgConnection::establish(&database_url) 25 | .expect(&format!("Error connecting to {}", database_url)) 26 | } 27 | 28 | use self::models::{Post, NewPost}; 29 | 30 | pub fn create_post<'a>(conn: &PgConnection, title: &'a str, body: &'a str) -> Post { 31 | use schema::posts; 32 | 33 | let new_post = NewPost { 34 | title: title, 35 | body: body, 36 | }; 37 | 38 | diesel::insert(&new_post).into(posts::table) 39 | .get_result(conn) 40 | .expect("Error saving new post") 41 | } 42 | -------------------------------------------------------------------------------- /src/models.rs: -------------------------------------------------------------------------------- 1 | #[derive(Queryable)] 2 | pub struct Post { 3 | pub id: i32, 4 | pub title: String, 5 | pub body: String, 6 | pub published: bool, 7 | } 8 | 9 | use super::schema::posts; 10 | 11 | #[insertable_into(posts)] 12 | pub struct NewPost<'a> { 13 | pub title: &'a str, 14 | pub body: &'a str, 15 | } 16 | -------------------------------------------------------------------------------- /src/schema.rs: -------------------------------------------------------------------------------- 1 | infer_schema!(dotenv!("DATABASE_URL")); 2 | --------------------------------------------------------------------------------