From 012ef6a4455faa3e031b65d50206a3045ec192c2 Mon Sep 17 00:00:00 2001 From: Gernot Pokorny Date: Fri, 9 Jun 2023 02:53:25 +0200 Subject: [PATCH] Added "All About Updates" sqlite examples I copy & pasted the Postgres examples and adapted them to sqlite. Note: The tests do not look ideal to me --- Cargo.toml | 1 + examples/sqlite/all_about_updates/Cargo.toml | 12 + examples/sqlite/all_about_updates/src/lib.rs | 217 +++++++++++++++++++ 3 files changed, 230 insertions(+) create mode 100644 examples/sqlite/all_about_updates/Cargo.toml create mode 100644 examples/sqlite/all_about_updates/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 2263bd36e189..57eba31f30c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ members = [ "examples/postgres/custom_types", "examples/postgres/relations", "examples/sqlite/all_about_inserts", + "examples/sqlite/all_about_updates", "examples/sqlite/getting_started_step_1", "examples/sqlite/getting_started_step_2", "examples/sqlite/getting_started_step_3", diff --git a/examples/sqlite/all_about_updates/Cargo.toml b/examples/sqlite/all_about_updates/Cargo.toml new file mode 100644 index 000000000000..31a006782ad0 --- /dev/null +++ b/examples/sqlite/all_about_updates/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "all_about_updates_sqlite" +version = "0.1.0" +edition = "2018" +publish = false + +[dependencies] +chrono = "0.4.26" +diesel = { version = "2.1.0", path = "../../../diesel", features = ["sqlite", "chrono"] } + +[lib] +doc = false diff --git a/examples/sqlite/all_about_updates/src/lib.rs b/examples/sqlite/all_about_updates/src/lib.rs new file mode 100644 index 000000000000..6fb3d40e01c8 --- /dev/null +++ b/examples/sqlite/all_about_updates/src/lib.rs @@ -0,0 +1,217 @@ +use chrono::NaiveDateTime; +#[cfg(test)] +use chrono::Utc; + +#[cfg(test)] +use diesel::debug_query; +use diesel::prelude::*; +#[cfg(test)] +use diesel::sqlite::Sqlite; + +table! { + posts { + id -> BigInt, + title -> Text, + body -> Text, + draft -> Bool, + publish_at -> Timestamp, + visit_count -> Integer, + } +} + +#[derive(Queryable, Identifiable, AsChangeset)] +pub struct Post { + pub id: i64, + pub title: String, + pub body: String, + pub draft: bool, + pub publish_at: NaiveDateTime, + pub visit_count: i32, +} + +pub fn publish_all_posts(conn: &mut SqliteConnection) -> QueryResult { + use crate::posts::dsl::*; + + diesel::update(posts).set(draft.eq(false)).execute(conn) +} + +#[test] +fn examine_sql_from_publish_all_posts() { + use crate::posts::dsl::*; + + assert_eq!( + "UPDATE `posts` SET `draft` = ? -- binds: [false]", + debug_query(&diesel::update(posts).set(draft.eq(false))).to_string() + ); +} + +pub fn publish_pending_posts(conn: &mut SqliteConnection) -> QueryResult { + use crate::posts::dsl::*; + use diesel::dsl::now; + + diesel::update(posts) + .filter(publish_at.lt(now)) + .set(draft.eq(false)) + .execute(conn) +} + +#[test] +fn examine_sql_from_publish_pending_posts() { + use crate::posts::dsl::*; + use diesel::dsl::now; + + let query = diesel::update(posts) + .filter(publish_at.lt(now)) + .set(draft.eq(false)); + assert_eq!( + "UPDATE `posts` SET `draft` = ? \ + WHERE (`posts`.`publish_at` < CURRENT_TIMESTAMP) \ + -- binds: [false]", + debug_query(&query).to_string() + ); +} + +pub fn publish_post(post: &Post, conn: &mut SqliteConnection) -> QueryResult { + diesel::update(post) + .set(posts::draft.eq(false)) + .execute(conn) +} + +#[test] +fn examine_sql_from_publish_post() { + let now = Utc::now().naive_utc(); + + let post = Post { + id: 1, + title: "".into(), + body: "".into(), + draft: false, + publish_at: now, + visit_count: 0, + }; + assert_eq!( + "UPDATE `posts` SET `draft` = ? WHERE (`posts`.`id` = ?) \ + -- binds: [false, 1]", + debug_query(&diesel::update(&post).set(posts::draft.eq(false))).to_string() + ); +} + +pub fn increment_visit_counts(conn: &mut SqliteConnection) -> QueryResult { + use crate::posts::dsl::*; + + diesel::update(posts) + .set(visit_count.eq(visit_count + 1)) + .execute(conn) +} + +#[test] +fn examine_sql_from_increment_visit_counts() { + use crate::posts::dsl::*; + + assert_eq!( + "UPDATE `posts` SET `visit_count` = (`posts`.`visit_count` + ?) \ + -- binds: [1]", + debug_query::(&diesel::update(posts).set(visit_count.eq(visit_count + 1))) + .to_string() + ); +} + +pub fn hide_everything(conn: &mut SqliteConnection) -> QueryResult { + use crate::posts::dsl::*; + + diesel::update(posts) + .set(( + title.eq("[REDACTED]"), + body.eq("This post has been classified"), + )) + .execute(conn) +} + +#[test] +fn examine_sql_from_hide_everything() { + use crate::posts::dsl::*; + + let query = diesel::update(posts).set(( + title.eq("[REDACTED]"), + body.eq("This post has been classified"), + )); + assert_eq!( + "UPDATE `posts` SET `title` = ?, `body` = ? \ + -- binds: [\"[REDACTED]\", \"This post has been classified\"]", + debug_query::(&query).to_string() + ); +} + +pub fn update_from_post_fields(post: &Post, conn: &mut SqliteConnection) -> QueryResult { + diesel::update(posts::table).set(post).execute(conn) +} + +#[test] +fn examine_sql_from_update_post_fields() { + let now = Utc::now().naive_utc(); + + let post = Post { + id: 1, + title: "".into(), + body: "".into(), + draft: false, + publish_at: now, + visit_count: 0, + }; + let sql = format!( + "UPDATE `posts` SET \ + `title` = ?, \ + `body` = ?, \ + `draft` = ?, \ + `publish_at` = ?, \ + `visit_count` = ? \ + -- binds: [\ + \"\", \ + \"\", \ + false, \ + {now:?}, \ + 0\ + ]" + ); + assert_eq!( + sql, + debug_query(&diesel::update(posts::table).set(&post)).to_string() + ); +} + +pub fn update_with_option(conn: &mut SqliteConnection) -> QueryResult { + #[derive(AsChangeset)] + #[diesel(table_name = posts)] + struct PostForm<'a> { + title: Option<&'a str>, + body: Option<&'a str>, + } + + diesel::update(posts::table) + .set(&PostForm { + title: None, + body: Some("My new post"), + }) + .execute(conn) +} + +#[test] +fn examine_sql_from_update_with_option() { + #[derive(AsChangeset)] + #[diesel(table_name = posts)] + struct PostForm<'a> { + title: Option<&'a str>, + body: Option<&'a str>, + } + + let post_form = PostForm { + title: None, + body: Some("My new post"), + }; + let query = diesel::update(posts::table).set(&post_form); + assert_eq!( + "UPDATE `posts` SET `body` = ? \ + -- binds: [\"My new post\"]", + debug_query::(&query).to_string() + ); +}