Skip to content

Commit

Permalink
Merge pull request #4347 from sharma-shray/master
Browse files Browse the repository at this point in the history
Type Aliases for Complex Result Types in examples
  • Loading branch information
weiznich authored Nov 19, 2024
2 parents 5a2940e + 9e671f0 commit 19c0405
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
22 changes: 10 additions & 12 deletions examples/postgres/relations/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub mod schema;
use crate::model::*;
use crate::schema::*;

type DbResult<T> = Result<T, Box<dyn Error + Send + Sync>>;

fn establish_connection() -> PgConnection {
dotenv().ok();

Expand All @@ -18,27 +20,23 @@ fn establish_connection() -> PgConnection {
.unwrap_or_else(|_| panic!("Error connecting to {database_url}"))
}

fn new_author(conn: &mut PgConnection, name: &str) -> Result<Author, Box<dyn Error + Send + Sync>> {
fn new_author(conn: &mut PgConnection, name: &str) -> DbResult<Author> {
let author = diesel::insert_into(authors::table)
.values(authors::name.eq(name))
.returning(Author::as_returning())
.get_result(conn)?;
Ok(author)
}

fn new_book(conn: &mut PgConnection, title: &str) -> Result<Book, Box<dyn Error + Send + Sync>> {
fn new_book(conn: &mut PgConnection, title: &str) -> DbResult<Book> {
let book = diesel::insert_into(books::table)
.values(books::title.eq(title))
.returning(Book::as_returning())
.get_result(conn)?;
Ok(book)
}

fn new_books_author(
conn: &mut PgConnection,
book_id: i32,
author_id: i32,
) -> Result<BookAuthor, Box<dyn Error + Send + Sync>> {
fn new_books_author(conn: &mut PgConnection, book_id: i32, author_id: i32) -> DbResult<BookAuthor> {
let book_author = diesel::insert_into(books_authors::table)
.values((
books_authors::book_id.eq(book_id),
Expand All @@ -54,7 +52,7 @@ fn new_page(
page_number: i32,
content: &str,
book_id: i32,
) -> Result<Page, Box<dyn Error + Send + Sync>> {
) -> DbResult<Page> {
let page = diesel::insert_into(pages::table)
.values((
pages::page_number.eq(page_number),
Expand All @@ -66,7 +64,7 @@ fn new_page(
Ok(page)
}

fn joins(conn: &mut PgConnection) -> Result<(), Box<dyn Error + Send + Sync>> {
fn joins(conn: &mut PgConnection) -> DbResult<()> {
let page_with_book = pages::table
.inner_join(books::table)
.filter(books::title.eq("Momo"))
Expand All @@ -84,7 +82,7 @@ fn joins(conn: &mut PgConnection) -> Result<(), Box<dyn Error + Send + Sync>> {
Ok(())
}

fn one_to_n_relations(conn: &mut PgConnection) -> Result<(), Box<dyn Error + Send + Sync>> {
fn one_to_n_relations(conn: &mut PgConnection) -> DbResult<()> {
let momo = books::table
.filter(books::title.eq("Momo"))
.select(Book::as_select())
Expand Down Expand Up @@ -117,7 +115,7 @@ fn one_to_n_relations(conn: &mut PgConnection) -> Result<(), Box<dyn Error + Sen
Ok(())
}

fn m_to_n_relations(conn: &mut PgConnection) -> Result<(), Box<dyn Error + Send + Sync>> {
fn m_to_n_relations(conn: &mut PgConnection) -> DbResult<()> {
let astrid_lindgren = authors::table
.filter(authors::name.eq("Astrid Lindgren"))
.select(Author::as_select())
Expand Down Expand Up @@ -172,7 +170,7 @@ fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
Ok(())
}

fn setup_data(conn: &mut PgConnection) -> Result<(), Box<dyn Error + Send + Sync>> {
fn setup_data(conn: &mut PgConnection) -> DbResult<()> {
// create a book
let momo = new_book(conn, "Momo")?;

Expand Down
24 changes: 10 additions & 14 deletions examples/sqlite/relations/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub mod schema;
use crate::model::*;
use crate::schema::*;

type DbResult<T> = Result<T, Box<dyn Error + Send + Sync>>;

fn establish_connection() -> SqliteConnection {
dotenv().ok();

Expand All @@ -18,21 +20,15 @@ fn establish_connection() -> SqliteConnection {
.unwrap_or_else(|_| panic!("Error connecting to {database_url}"))
}

fn new_author(
conn: &mut SqliteConnection,
name: &str,
) -> Result<Author, Box<dyn Error + Send + Sync>> {
fn new_author(conn: &mut SqliteConnection, name: &str) -> DbResult<Author> {
let author = diesel::insert_into(authors::table)
.values(authors::name.eq(name))
.returning(Author::as_returning())
.get_result(conn)?;
Ok(author)
}

fn new_book(
conn: &mut SqliteConnection,
title: &str,
) -> Result<Book, Box<dyn Error + Send + Sync>> {
fn new_book(conn: &mut SqliteConnection, title: &str) -> DbResult<Book> {
let book = diesel::insert_into(books::table)
.values(books::title.eq(title))
.returning(Book::as_returning())
Expand All @@ -44,7 +40,7 @@ fn new_books_author(
conn: &mut SqliteConnection,
book_id: i32,
author_id: i32,
) -> Result<BookAuthor, Box<dyn Error + Send + Sync>> {
) -> DbResult<BookAuthor> {
let book_author = diesel::insert_into(books_authors::table)
.values((
books_authors::book_id.eq(book_id),
Expand All @@ -60,7 +56,7 @@ fn new_page(
page_number: i32,
content: &str,
book_id: i32,
) -> Result<Page, Box<dyn Error + Send + Sync>> {
) -> DbResult<Page> {
let page = diesel::insert_into(pages::table)
.values((
pages::page_number.eq(page_number),
Expand All @@ -72,7 +68,7 @@ fn new_page(
Ok(page)
}

fn joins(conn: &mut SqliteConnection) -> Result<(), Box<dyn Error + Send + Sync>> {
fn joins(conn: &mut SqliteConnection) -> DbResult<()> {
let page_with_book = pages::table
.inner_join(books::table)
.filter(books::title.eq("Momo"))
Expand All @@ -90,7 +86,7 @@ fn joins(conn: &mut SqliteConnection) -> Result<(), Box<dyn Error + Send + Sync>
Ok(())
}

fn one_to_n_relations(conn: &mut SqliteConnection) -> Result<(), Box<dyn Error + Send + Sync>> {
fn one_to_n_relations(conn: &mut SqliteConnection) -> DbResult<()> {
let momo = books::table
.filter(books::title.eq("Momo"))
.select(Book::as_select())
Expand Down Expand Up @@ -123,7 +119,7 @@ fn one_to_n_relations(conn: &mut SqliteConnection) -> Result<(), Box<dyn Error +
Ok(())
}

fn m_to_n_relations(conn: &mut SqliteConnection) -> Result<(), Box<dyn Error + Send + Sync>> {
fn m_to_n_relations(conn: &mut SqliteConnection) -> DbResult<()> {
let astrid_lindgren = authors::table
.filter(authors::name.eq("Astrid Lindgren"))
.select(Author::as_select())
Expand Down Expand Up @@ -178,7 +174,7 @@ fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
Ok(())
}

fn setup_data(conn: &mut SqliteConnection) -> Result<(), Box<dyn Error + Send + Sync>> {
fn setup_data(conn: &mut SqliteConnection) -> DbResult<()> {
// create a book
let momo = new_book(conn, "Momo")?;

Expand Down

0 comments on commit 19c0405

Please sign in to comment.