From e97968054d4914a193da9abd5b65ba8854c9bb2d Mon Sep 17 00:00:00 2001 From: James Pace Date: Wed, 14 Feb 2024 22:10:34 -0500 Subject: [PATCH] Finish examples. --- Cargo.lock | 138 ---------------------------------------------------- src/main.rs | 43 +++++++++++----- 2 files changed, 32 insertions(+), 149 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 70372a7..982c7ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -36,21 +36,6 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" -[[package]] -name = "android-tzdata" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" - -[[package]] -name = "android_system_properties" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" -dependencies = [ - "libc", -] - [[package]] name = "anyhow" version = "1.0.79" @@ -133,12 +118,6 @@ dependencies = [ "generic-array", ] -[[package]] -name = "bumpalo" -version = "3.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" - [[package]] name = "byteorder" version = "1.5.0" @@ -166,30 +145,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "chrono" -version = "0.4.34" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5bc015644b92d5890fab7489e49d21f879d5c990186827d42ec511919404f38b" -dependencies = [ - "android-tzdata", - "iana-time-zone", - "num-traits", - "windows-targets 0.52.0", -] - [[package]] name = "const-oid" version = "0.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" -[[package]] -name = "core-foundation-sys" -version = "0.8.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" - [[package]] name = "cpufeatures" version = "0.2.12" @@ -508,29 +469,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "iana-time-zone" -version = "0.1.60" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" -dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "wasm-bindgen", - "windows-core", -] - -[[package]] -name = "iana-time-zone-haiku" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" -dependencies = [ - "cc", -] - [[package]] name = "idna" version = "0.5.0" @@ -566,15 +504,6 @@ version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" -[[package]] -name = "js-sys" -version = "0.3.68" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "406cda4b368d531c842222cf9d2600a9a4acce8d29423695379c6868a143a9ee" -dependencies = [ - "wasm-bindgen", -] - [[package]] name = "lazy_static" version = "1.4.0" @@ -1117,7 +1046,6 @@ dependencies = [ "atoi", "byteorder", "bytes", - "chrono", "crc", "crossbeam-queue", "dotenvy", @@ -1210,7 +1138,6 @@ dependencies = [ "bitflags 2.4.2", "byteorder", "bytes", - "chrono", "crc", "digest", "dotenvy", @@ -1252,7 +1179,6 @@ dependencies = [ "base64", "bitflags 2.4.2", "byteorder", - "chrono", "crc", "dotenvy", "etcetera", @@ -1289,7 +1215,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "210976b7d948c7ba9fced8ca835b11cbb2d677c59c79de41ac0d397e14547490" dependencies = [ "atoi", - "chrono", "flume", "futures-channel", "futures-core", @@ -1539,75 +1464,12 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" -[[package]] -name = "wasm-bindgen" -version = "0.2.91" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1e124130aee3fb58c5bdd6b639a0509486b0338acaaae0c84a5124b0f588b7f" -dependencies = [ - "cfg-if", - "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.91" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9e7e1900c352b609c8488ad12639a311045f40a35491fb69ba8c12f758af70b" -dependencies = [ - "bumpalo", - "log", - "once_cell", - "proc-macro2", - "quote", - "syn 2.0.48", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.91" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b30af9e2d358182b5c7449424f017eba305ed32a7010509ede96cdc4696c46ed" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.91" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "642f325be6301eb8107a83d12a8ac6c1e1c54345a7ef1a9261962dfefda09e66" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.48", - "wasm-bindgen-backend", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.91" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838" - [[package]] name = "whoami" version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22fc3756b8a9133049b26c7f61ab35416c130e8c09b660f5b3958b446f52cc50" -[[package]] -name = "windows-core" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" -dependencies = [ - "windows-targets 0.52.0", -] - [[package]] name = "windows-sys" version = "0.48.0" diff --git a/src/main.rs b/src/main.rs index b4994b3..143a717 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,47 +1,68 @@ use serde::{Deserialize, Serialize}; +use sqlx::{types::Json, Row}; #[tokio::main] async fn main() -> anyhow::Result<()> { // Connect to the database. - let pool = sqlx::postgres::PgPool::connect("postgresql://postgres:devpassword@localhost/postgres").await?; + let pool = + sqlx::postgres::PgPool::connect("postgresql://postgres:devpassword@localhost/postgres") + .await?; // Make the table. let _ = sqlx::query( - "CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY AUTOINCREMENT, + "CREATE TABLE IF NOT EXISTS test (id SERIAL PRIMARY KEY, name TEXT, doc JSONB - );" - ).execute(&pool).await?; + );", + ) + .execute(&pool) + .await?; // Insert some entries into db. // Add James. let james_doc = Doc { age: 30, fav_color: "blue".to_owned(), - fav_movie: "Iron Man".to_owned() + fav_movie: "Iron Man".to_owned(), }; let james_doc_as_str = serde_json::to_string(&james_doc)?; - let james_add_query = format!(r#"INSERT INTO test (name, doc) VALUES('James', '{}');"#, james_doc_as_str); + let james_add_query = format!( + r#"INSERT INTO test (name, doc) VALUES('James', '{}');"#, + james_doc_as_str + ); let _ = sqlx::query(&james_add_query).execute(&pool).await?; // Add Dave. let dave_doc = Doc { age: 25, fav_color: "yellow".to_owned(), - fav_movie: "Spiderman".to_owned() + fav_movie: "Spiderman".to_owned(), }; let dave_doc_as_str = serde_json::to_string(&dave_doc)?; - let dave_add_query = format!(r#"INSERT INTO test (name, doc) VALUES('Dave', '{}');"#, dave_doc_as_str); + let dave_add_query = format!( + r#"INSERT INTO test (name, doc) VALUES('Dave', '{}');"#, + dave_doc_as_str + ); let _ = sqlx::query(&dave_add_query).execute(&pool).await?; // Get james favorite color. + let fav_color_query = r#"SELECT doc->>'fav_color' FROM test WHERE name = 'James';"#; + let fav_color_result = sqlx::query(&fav_color_query).fetch_one(&pool).await?; + let james_fav_color = fav_color_result.try_get::(0)?; + println!("James' Fav Color: {}", james_fav_color); // Get name based on movie. + let spiderman_query = r#"SELECT name FROM test WHERE doc->>'fav_movie' = 'Spiderman';"#; + let spiderman_result = sqlx::query(&spiderman_query).fetch_one(&pool).await?; + let spiderman_name = spiderman_result.try_get::(0)?; + println!("{} likes Spiderman.", spiderman_name); // Get james's doc. - // Row based on color. - + let james_query = r#"SELECT doc FROM test where name = 'James'"#; + let james_result = sqlx::query(&james_query).fetch_one(&pool).await?; + let james_doc = james_result.try_get::, usize>(0)?.0; + println!("James Doc is: {:?}", james_doc); Ok(()) } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Debug)] struct Doc { age: u32, fav_color: String,