Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "All About Updates" sqlite examples #3655

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 12 additions & 0 deletions examples/sqlite/all_about_updates/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
217 changes: 217 additions & 0 deletions examples/sqlite/all_about_updates/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<usize> {
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<usize> {
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<usize> {
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<usize> {
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::<Sqlite, _>(&diesel::update(posts).set(visit_count.eq(visit_count + 1)))
.to_string()
);
}

pub fn hide_everything(conn: &mut SqliteConnection) -> QueryResult<usize> {
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::<Sqlite, _>(&query).to_string()
);
}

pub fn update_from_post_fields(post: &Post, conn: &mut SqliteConnection) -> QueryResult<usize> {
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<usize> {
#[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::<Sqlite, _>(&query).to_string()
);
}