File tree Expand file tree Collapse file tree
examples/getting_started_step_3
migrations/20160815133237_create_posts Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ [package ]
2+ name = " diesel_demo"
3+ version = " 0.1.0"
4+ authors = [" Sean Griffin <sean@seantheprogrammer.com>" ]
5+
6+ [dependencies ]
7+ diesel = " 0.7.1"
8+ diesel_codegen = { version = " 0.7.2" , features = [" postgres" ] }
9+ dotenv = " 0.8.0"
10+ dotenv_macros = " 0.9.0"
Original file line number Diff line number Diff line change 1+ DROP TABLE posts
Original file line number Diff line number Diff line change 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+ )
Original file line number Diff line number Diff line change 1+ extern crate diesel_demo;
2+ extern crate diesel;
3+
4+ use diesel:: prelude:: * ;
5+ use 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+ }
Original file line number Diff line number Diff line change 1+ extern crate diesel_demo;
2+ extern crate diesel;
3+
4+ use diesel:: prelude:: * ;
5+ use diesel_demo:: * ;
6+ use 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 :: < i32 > ( ) . 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 :: < Post > ( & connection)
19+ . expect ( & format ! ( "Unable to find post {}" , id) ) ;
20+ println ! ( "Published post {}" , post. title) ;
21+ }
Original file line number Diff line number Diff line change 1+ extern crate diesel_demo;
2+ extern crate diesel;
3+
4+ use diesel_demo:: * ;
5+ use diesel_demo:: models:: * ;
6+ use 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 :: < Post > ( & 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+ }
Original file line number Diff line number Diff line change 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+ let mut title = String :: new ( ) ;
11+ let mut body = String :: new ( ) ;
12+
13+ println ! ( "What would you like your title to be?" ) ;
14+ stdin ( ) . read_line ( & mut title) . unwrap ( ) ;
15+ let title = title. trim_right ( ) ; // Remove the trailing newline
16+
17+ println ! ( "\n Ok! Let's write {} (Press {} when finished)\n " , title, EOF ) ;
18+ stdin ( ) . read_to_string ( & mut body) . unwrap ( ) ;
19+
20+ let post = create_post ( & connection, title, & body) ;
21+ println ! ( "\n Saved draft {} with id {}" , title, post. id) ;
22+ }
23+
24+ #[ cfg( not( windows) ) ]
25+ const EOF : & ' static str = "CTRL+D" ;
26+
27+ #[ cfg( windows) ]
28+ const EOF : & ' static str = "CTRL+Z" ;
Original file line number Diff line number Diff line change 1+ #![ feature( custom_derive, custom_attribute, plugin) ]
2+ #![ plugin( diesel_codegen, dotenv_macros) ]
3+
4+ #[ macro_use] extern crate diesel;
5+ extern crate dotenv;
6+
7+ pub mod schema;
8+ pub mod models;
9+
10+ use diesel:: prelude:: * ;
11+ use diesel:: pg:: PgConnection ;
12+ use dotenv:: dotenv;
13+ use std:: env;
14+
15+ use self :: models:: { Post , NewPost } ;
16+
17+ pub fn establish_connection ( ) -> PgConnection {
18+ dotenv ( ) . ok ( ) ;
19+
20+ let database_url = env:: var ( "DATABASE_URL" )
21+ . expect ( "DATABASE_URL must be set" ) ;
22+ PgConnection :: establish ( & database_url)
23+ . expect ( & format ! ( "Error connecting to {}" , database_url) )
24+ }
25+
26+ pub fn create_post ( conn : & PgConnection , title : & str , body : & str ) -> Post {
27+ use schema:: posts;
28+
29+ let new_post = NewPost {
30+ title : title,
31+ body : body,
32+ } ;
33+
34+ diesel:: insert ( & new_post) . into ( posts:: table)
35+ . get_result ( conn)
36+ . expect ( "Error saving new post" )
37+ }
Original file line number Diff line number Diff line change 1+ use schema:: posts;
2+
3+ #[ derive( Queryable ) ]
4+ pub struct Post {
5+ pub id : i32 ,
6+ pub title : String ,
7+ pub body : String ,
8+ pub published : bool ,
9+ }
10+
11+ #[ insertable_into( posts) ]
12+ pub struct NewPost < ' a > {
13+ pub title : & ' a str ,
14+ pub body : & ' a str ,
15+ }
Original file line number Diff line number Diff line change 1+ infer_schema ! ( dotenv!( "DATABASE_URL" ) ) ;
You can’t perform that action at this time.
0 commit comments