1
0
mirror of https://github.com/actix/examples synced 2024-11-23 14:31:07 +01:00

Update db/basic, db/sqlx_todo and db/r2d2 to v4 (#506)

This commit is contained in:
Luca Palmieri 2022-01-29 16:25:46 +00:00 committed by GitHub
parent 6e3ae0fd19
commit 8b5de155bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 225 additions and 165 deletions

301
Cargo.lock generated
View File

@ -22,7 +22,7 @@ dependencies = [
"actix_derive", "actix_derive",
"bitflags", "bitflags",
"bytes 0.5.6", "bytes 0.5.6",
"crossbeam-channel", "crossbeam-channel 0.4.4",
"derive_more", "derive_more",
"futures-channel", "futures-channel",
"futures-util", "futures-util",
@ -1033,7 +1033,7 @@ dependencies = [
"async-graphql-derive", "async-graphql-derive",
"async-graphql-parser", "async-graphql-parser",
"async-graphql-value", "async-graphql-value",
"async-stream 0.3.2", "async-stream",
"async-trait", "async-trait",
"bytes 1.1.0", "bytes 1.1.0",
"fnv", "fnv",
@ -1157,35 +1157,6 @@ dependencies = [
"event-listener", "event-listener",
] ]
[[package]]
name = "async-native-tls"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e9e7a929bd34c68a82d58a4de7f86fffdaf97fb2af850162a7bb19dd7269b33"
dependencies = [
"async-std",
"native-tls",
"thiserror",
"url",
]
[[package]]
name = "async-process"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83137067e3a2a6a06d67168e49e68a0957d215410473a740cea95a2425c0b7c6"
dependencies = [
"async-io",
"blocking",
"cfg-if 1.0.0",
"event-listener",
"futures-lite",
"libc",
"once_cell",
"signal-hook",
"winapi 0.3.9",
]
[[package]] [[package]]
name = "async-std" name = "async-std"
version = "1.10.0" version = "1.10.0"
@ -1196,7 +1167,6 @@ dependencies = [
"async-global-executor", "async-global-executor",
"async-io", "async-io",
"async-lock", "async-lock",
"async-process",
"crossbeam-utils 0.8.6", "crossbeam-utils 0.8.6",
"futures-channel", "futures-channel",
"futures-core", "futures-core",
@ -1214,37 +1184,16 @@ dependencies = [
"wasm-bindgen-futures", "wasm-bindgen-futures",
] ]
[[package]]
name = "async-stream"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22068c0c19514942eefcfd4daf8976ef1aad84e61539f95cd200c35202f80af5"
dependencies = [
"async-stream-impl 0.2.1",
"futures-core",
]
[[package]] [[package]]
name = "async-stream" name = "async-stream"
version = "0.3.2" version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "171374e7e3b2504e0e5236e3b59260560f9fe94bfe9ac39ba5e4e929c5590625" checksum = "171374e7e3b2504e0e5236e3b59260560f9fe94bfe9ac39ba5e4e929c5590625"
dependencies = [ dependencies = [
"async-stream-impl 0.3.2", "async-stream-impl",
"futures-core", "futures-core",
] ]
[[package]]
name = "async-stream-impl"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25f9db3b38af870bf7e5cc649167533b493928e50744e2c30ae350230b414670"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "async-stream-impl" name = "async-stream-impl"
version = "0.3.2" version = "0.3.2"
@ -1287,8 +1236,8 @@ dependencies = [
name = "async_db" name = "async_db"
version = "2.0.0" version = "2.0.0"
dependencies = [ dependencies = [
"actix-web 3.3.3", "actix-web 4.0.0-beta.21",
"env_logger 0.8.4", "env_logger 0.9.0",
"failure", "failure",
"futures", "futures",
"num_cpus", "num_cpus",
@ -1329,6 +1278,15 @@ dependencies = [
"tokio-postgres", "tokio-postgres",
] ]
[[package]]
name = "atoi"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "616896e05fc0e2649463a93a15183c6a16bf03413a7af88ef1285ddedfa9cda5"
dependencies = [
"num-traits 0.2.14",
]
[[package]] [[package]]
name = "atomic-waker" name = "atomic-waker"
version = "1.0.0" version = "1.0.0"
@ -1576,7 +1534,7 @@ dependencies = [
"rand 0.7.3", "rand 0.7.3",
"serde 1.0.136", "serde 1.0.136",
"serde_json", "serde_json",
"uuid", "uuid 0.8.2",
] ]
[[package]] [[package]]
@ -1595,7 +1553,7 @@ dependencies = [
"serde 1.0.136", "serde 1.0.136",
"serde_bytes", "serde_bytes",
"serde_json", "serde_json",
"uuid", "uuid 0.8.2",
] ]
[[package]] [[package]]
@ -1969,6 +1927,21 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcb25d077389e53838a8158c8e99174c5a9d902dee4904320db714f3c653ffba" checksum = "dcb25d077389e53838a8158c8e99174c5a9d902dee4904320db714f3c653ffba"
[[package]]
name = "crc"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49fc9a695bca7f35f5f4c15cddc84415f66a74ea78eef08e90c5024f2b540e23"
dependencies = [
"crc-catalog",
]
[[package]]
name = "crc-catalog"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccaeedb56da03b09f598226e25e80088cb4cd25f316e6e4df7d695f0feeb1403"
[[package]] [[package]]
name = "crc32fast" name = "crc32fast"
version = "1.3.1" version = "1.3.1"
@ -1988,6 +1961,16 @@ dependencies = [
"maybe-uninit", "maybe-uninit",
] ]
[[package]]
name = "crossbeam-channel"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e54ea8bc3fb1ee042f5aace6e3c6e025d3874866da222930f70ce62aceba0bfa"
dependencies = [
"cfg-if 1.0.0",
"crossbeam-utils 0.8.6",
]
[[package]] [[package]]
name = "crossbeam-queue" name = "crossbeam-queue"
version = "0.2.3" version = "0.2.3"
@ -1999,6 +1982,16 @@ dependencies = [
"maybe-uninit", "maybe-uninit",
] ]
[[package]]
name = "crossbeam-queue"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b979d76c9fcb84dffc80a73f7290da0f83e4c95773494674cb44b76d13a7a110"
dependencies = [
"cfg-if 1.0.0",
"crossbeam-utils 0.8.6",
]
[[package]] [[package]]
name = "crossbeam-utils" name = "crossbeam-utils"
version = "0.7.2" version = "0.7.2"
@ -2172,7 +2165,7 @@ checksum = "4aaff9a7a1de9893f4004fa08527b31cb2ae4121c44e053cf53f29203c73bd23"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"config", "config",
"crossbeam-queue", "crossbeam-queue 0.2.3",
"num_cpus", "num_cpus",
"serde 1.0.136", "serde 1.0.136",
"tokio 0.2.25", "tokio 0.2.25",
@ -2248,7 +2241,7 @@ dependencies = [
"libsqlite3-sys", "libsqlite3-sys",
"pq-sys", "pq-sys",
"r2d2", "r2d2",
"uuid", "uuid 0.8.2",
] ]
[[package]] [[package]]
@ -2264,7 +2257,7 @@ dependencies = [
"futures", "futures",
"serde 1.0.136", "serde 1.0.136",
"serde_json", "serde_json",
"uuid", "uuid 0.8.2",
] ]
[[package]] [[package]]
@ -2382,19 +2375,6 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "env_logger"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
dependencies = [
"atty",
"humantime 1.3.0",
"log",
"regex",
"termcolor",
]
[[package]] [[package]]
name = "env_logger" name = "env_logger"
version = "0.8.4" version = "0.8.4"
@ -2402,7 +2382,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3" checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3"
dependencies = [ dependencies = [
"atty", "atty",
"humantime 2.1.0", "humantime",
"log", "log",
"regex", "regex",
"termcolor", "termcolor",
@ -2415,7 +2395,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3" checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3"
dependencies = [ dependencies = [
"atty", "atty",
"humantime 2.1.0", "humantime",
"log", "log",
"regex", "regex",
"termcolor", "termcolor",
@ -2623,6 +2603,17 @@ dependencies = [
"futures-util", "futures-util",
] ]
[[package]]
name = "futures-intrusive"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62007592ac46aa7c2b6416f7deb9a8a8f63a01e0f1d6e1787d5630170db2b63e"
dependencies = [
"futures-core",
"lock_api",
"parking_lot",
]
[[package]] [[package]]
name = "futures-io" name = "futures-io"
version = "0.3.19" version = "0.3.19"
@ -2856,6 +2847,18 @@ name = "hashbrown"
version = "0.11.2" version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
dependencies = [
"ahash 0.7.6",
]
[[package]]
name = "hashlink"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7249a3129cbc1ffccd74857f81464a323a152173cdb134e0fd81bc803b29facf"
dependencies = [
"hashbrown",
]
[[package]] [[package]]
name = "heck" name = "heck"
@ -3011,15 +3014,6 @@ version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02296996cb8796d7c6e3bc2d9211b7802812d36999a51bb754123ead7d37d026" checksum = "02296996cb8796d7c6e3bc2d9211b7802812d36999a51bb754123ead7d37d026"
[[package]]
name = "humantime"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
dependencies = [
"quick-error 1.2.3",
]
[[package]] [[package]]
name = "humantime" name = "humantime"
version = "2.1.0" version = "2.1.0"
@ -3281,7 +3275,7 @@ dependencies = [
"smartstring", "smartstring",
"static_assertions 1.1.0", "static_assertions 1.1.0",
"url", "url",
"uuid", "uuid 0.8.2",
] ]
[[package]] [[package]]
@ -3298,7 +3292,7 @@ dependencies = [
"r2d2_mysql", "r2d2_mysql",
"serde 1.0.136", "serde 1.0.136",
"serde_json", "serde_json",
"uuid", "uuid 0.8.2",
] ]
[[package]] [[package]]
@ -3409,9 +3403,9 @@ checksum = "565dbd88872dbe4cc8a46e527f26483c1d1f7afa6b884a3bd6cd893d4f98da74"
[[package]] [[package]]
name = "libsqlite3-sys" name = "libsqlite3-sys"
version = "0.17.3" version = "0.22.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56d90181c2904c287e5390186be820e5ef311a3c62edebb7d6ca3d6a48ce041d" checksum = "290b64917f8b0cb885d9de0f9959fe1f775d7fa12f1da2db9001c1c8ab60f89d"
dependencies = [ dependencies = [
"cc", "cc",
"pkg-config", "pkg-config",
@ -3774,7 +3768,7 @@ dependencies = [
"trust-dns-proto 0.20.3", "trust-dns-proto 0.20.3",
"trust-dns-resolver 0.20.3", "trust-dns-resolver 0.20.3",
"typed-builder", "typed-builder",
"uuid", "uuid 0.8.2",
"version_check 0.9.4", "version_check 0.9.4",
"webpki", "webpki",
"webpki-roots 0.21.1", "webpki-roots 0.21.1",
@ -3806,7 +3800,7 @@ dependencies = [
"actix-web 3.3.3", "actix-web 3.3.3",
"futures-util", "futures-util",
"sanitize-filename", "sanitize-filename",
"uuid", "uuid 0.8.2",
] ]
[[package]] [[package]]
@ -3875,7 +3869,7 @@ dependencies = [
"sha2 0.8.2", "sha2 0.8.2",
"time 0.1.44", "time 0.1.44",
"twox-hash", "twox-hash",
"uuid", "uuid 0.8.2",
] ]
[[package]] [[package]]
@ -4569,12 +4563,12 @@ dependencies = [
name = "r2d2-example" name = "r2d2-example"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix-web 3.3.3", "actix-web 4.0.0-beta.21",
"env_logger 0.8.4", "env_logger 0.9.0",
"r2d2", "r2d2",
"r2d2_sqlite", "r2d2_sqlite",
"rusqlite", "rusqlite",
"uuid", "uuid 1.0.0-alpha.1",
] ]
[[package]] [[package]]
@ -4589,9 +4583,9 @@ dependencies = [
[[package]] [[package]]
name = "r2d2_sqlite" name = "r2d2_sqlite"
version = "0.14.0" version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c4689dcbe44341a3388a696a056cbb96124f7e70a1fbc9cd6a7bb6bab543e3b" checksum = "9d24607049214c5e42d3df53ac1d8a23c34cc6a5eefe3122acb2c72174719959"
dependencies = [ dependencies = [
"r2d2", "r2d2",
"rusqlite", "rusqlite",
@ -4978,17 +4972,17 @@ dependencies = [
[[package]] [[package]]
name = "rusqlite" name = "rusqlite"
version = "0.21.0" version = "0.25.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64a656821bb6317a84b257737b7934f79c0dbb7eb694710475908280ebad3e64" checksum = "5c4b1eaf239b47034fb450ee9cdedd7d0226571689d8823030c4b6c2cb407152"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"fallible-iterator", "fallible-iterator",
"fallible-streaming-iterator", "fallible-streaming-iterator",
"hashlink",
"libsqlite3-sys", "libsqlite3-sys",
"lru-cache",
"memchr", "memchr",
"time 0.1.44", "smallvec",
] ]
[[package]] [[package]]
@ -5416,16 +5410,6 @@ dependencies = [
"tokio 1.16.1", "tokio 1.16.1",
] ]
[[package]]
name = "signal-hook"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "647c97df271007dcea485bb74ffdb57f2e683f1306c854f468a0c244badabf2d"
dependencies = [
"libc",
"signal-hook-registry",
]
[[package]] [[package]]
name = "signal-hook-registry" name = "signal-hook-registry"
version = "1.4.0" version = "1.4.0"
@ -5454,7 +5438,7 @@ dependencies = [
"serde_json", "serde_json",
"sparkpost", "sparkpost",
"time 0.2.27", "time 0.2.27",
"uuid", "uuid 0.8.2",
] ]
[[package]] [[package]]
@ -5552,9 +5536,9 @@ dependencies = [
[[package]] [[package]]
name = "sqlx" name = "sqlx"
version = "0.3.5" version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8974cacd80085fbe49e778708d660dec6fb351604dc34c3905b26efb2803b038" checksum = "7911b0031a0247af40095838002999c7a52fba29d9739e93326e71a5a1bc9d43"
dependencies = [ dependencies = [
"sqlx-core", "sqlx-core",
"sqlx-macros", "sqlx-macros",
@ -5562,55 +5546,88 @@ dependencies = [
[[package]] [[package]]
name = "sqlx-core" name = "sqlx-core"
version = "0.3.5" version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88ac5a436f941c42eac509471a730df5c3c58e1450e68cd39afedbd948206273" checksum = "aec89bfaca8f7737439bad16d52b07f1ccd0730520d3bf6ae9d069fe4b641fb1"
dependencies = [ dependencies = [
"async-native-tls", "ahash 0.7.6",
"async-std", "atoi",
"async-stream 0.2.1",
"bitflags", "bitflags",
"byteorder", "byteorder",
"crossbeam-queue", "bytes 1.1.0",
"crossbeam-utils 0.7.2", "crc",
"crossbeam-channel 0.5.2",
"crossbeam-queue 0.3.3",
"crossbeam-utils 0.8.6",
"either",
"futures-channel", "futures-channel",
"futures-core", "futures-core",
"futures-intrusive",
"futures-util", "futures-util",
"hashlink",
"hex", "hex",
"indexmap",
"itoa 0.4.8",
"libc", "libc",
"libsqlite3-sys", "libsqlite3-sys",
"log", "log",
"memchr", "memchr",
"once_cell",
"parking_lot",
"percent-encoding", "percent-encoding",
"rustls 0.19.1",
"sha2 0.9.9",
"smallvec",
"sqlformat", "sqlformat",
"sqlx-rt",
"stringprep",
"thiserror",
"tokio-stream",
"url", "url",
"webpki",
"webpki-roots 0.21.1",
"whoami",
] ]
[[package]] [[package]]
name = "sqlx-macros" name = "sqlx-macros"
version = "0.3.5" version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de2ae78b783af5922d811b14665a5a3755e531c3087bb805cf24cf71f15e6780" checksum = "584866c833511b1a152e87a7ee20dee2739746f60c858b3c5209150bc4b466f5"
dependencies = [ dependencies = [
"async-std",
"dotenv", "dotenv",
"futures", "either",
"heck", "heck",
"once_cell",
"proc-macro2", "proc-macro2",
"quote", "quote",
"sha2 0.9.9",
"sqlx-core", "sqlx-core",
"sqlx-rt",
"syn", "syn",
"url", "url",
] ]
[[package]]
name = "sqlx-rt"
version = "0.5.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8061cbaa91ee75041514f67a09398c65a64efed72c90151ecd47593bad53da99"
dependencies = [
"actix-rt 2.6.0",
"once_cell",
"tokio 1.16.1",
"tokio-rustls 0.22.0",
]
[[package]] [[package]]
name = "sqlx_todo" name = "sqlx_todo"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix-web 3.3.3", "actix-web 4.0.0-beta.21",
"anyhow", "anyhow",
"dotenv", "dotenv",
"env_logger 0.7.1", "env_logger 0.9.0",
"futures", "futures",
"log", "log",
"serde 1.0.136", "serde 1.0.136",
@ -6187,6 +6204,17 @@ dependencies = [
"webpki", "webpki",
] ]
[[package]]
name = "tokio-stream"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50145484efff8818b5ccd256697f36863f587da82cf8b409c53adf1e840798e3"
dependencies = [
"futures-core",
"pin-project-lite 0.2.8",
"tokio 1.16.1",
]
[[package]] [[package]]
name = "tokio-tls" name = "tokio-tls"
version = "0.3.1" version = "0.3.1"
@ -6580,6 +6608,15 @@ dependencies = [
"serde 1.0.136", "serde 1.0.136",
] ]
[[package]]
name = "uuid"
version = "1.0.0-alpha.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb3ab47baa004111b323696c6eaa2752e7356f7f77cf6b6dc7a2087368ce1ca4"
dependencies = [
"getrandom 0.2.4",
]
[[package]] [[package]]
name = "v_escape" name = "v_escape"
version = "0.13.2" version = "0.13.2"
@ -6973,6 +7010,16 @@ dependencies = [
"cc", "cc",
] ]
[[package]]
name = "whoami"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "524b58fa5a20a2fb3014dd6358b70e6579692a56ef6fce928834e488f42f65e8"
dependencies = [
"wasm-bindgen",
"web-sys",
]
[[package]] [[package]]
name = "widestring" name = "widestring"
version = "0.4.3" version = "0.4.3"

View File

@ -2,16 +2,16 @@
name = "async_db" name = "async_db"
version = "2.0.0" version = "2.0.0"
authors = ["Darin Gordon <dkcdkg@gmail.com>"] authors = ["Darin Gordon <dkcdkg@gmail.com>"]
edition = "2018" edition = "2021"
[dependencies] [dependencies]
actix-web = "3" actix-web = "4.0.0-beta.21"
env_logger = "0.8" env_logger = "0.9.0"
failure = "0.1.7" failure = "0.1.7"
futures = "0.3.1" futures = "0.3.1"
num_cpus = "1.13" num_cpus = "1.13"
r2d2 = "0.8.2" r2d2 = "0.8.2"
r2d2_sqlite = "0.14" r2d2_sqlite = "0.18.0"
rusqlite = "0.21" rusqlite = "0.25.4"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"

View File

@ -1,7 +1,6 @@
use actix_web::{web, Error as AWError}; use actix_web::{error::InternalError, http::StatusCode, web};
use failure::Error; use failure::Error;
use futures::{Future, TryFutureExt}; use rusqlite::Statement;
use rusqlite::{Statement, NO_PARAMS};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{thread::sleep, time::Duration}; use std::{thread::sleep, time::Duration};
@ -23,10 +22,10 @@ pub enum Queries {
GetTopTenColdestMonths, GetTopTenColdestMonths,
} }
pub fn execute( pub async fn execute(
pool: &Pool, pool: &Pool,
query: Queries, query: Queries,
) -> impl Future<Output = Result<Vec<WeatherAgg>, AWError>> { ) -> Result<Vec<WeatherAgg>, InternalError<Error>> {
let pool = pool.clone(); let pool = pool.clone();
web::block(move || { web::block(move || {
// simulate an expensive query, see comments at top of main.rs // simulate an expensive query, see comments at top of main.rs
@ -40,7 +39,9 @@ pub fn execute(
}; };
result.map_err(Error::from) result.map_err(Error::from)
}) })
.map_err(AWError::from) .await
.unwrap()
.map_err(|e| InternalError::new(e, StatusCode::INTERNAL_SERVER_ERROR))
} }
fn get_hottest_years(conn: Connection) -> WeatherAggResult { fn get_hottest_years(conn: Connection) -> WeatherAggResult {
@ -73,7 +74,7 @@ fn get_coldest_years(conn: Connection) -> WeatherAggResult {
fn get_rows_as_annual_agg(mut statement: Statement) -> WeatherAggResult { fn get_rows_as_annual_agg(mut statement: Statement) -> WeatherAggResult {
statement statement
.query_map(NO_PARAMS, |row| { .query_map([], |row| {
Ok(WeatherAgg::AnnualAgg { Ok(WeatherAgg::AnnualAgg {
year: row.get(0)?, year: row.get(0)?,
total: row.get(1)?, total: row.get(1)?,
@ -112,7 +113,7 @@ fn get_coldest_months(conn: Connection) -> WeatherAggResult {
fn get_rows_as_month_agg(mut statement: Statement) -> WeatherAggResult { fn get_rows_as_month_agg(mut statement: Statement) -> WeatherAggResult {
statement statement
.query_map(NO_PARAMS, |row| { .query_map([], |row| {
Ok(WeatherAgg::MonthAgg { Ok(WeatherAgg::MonthAgg {
year: row.get(0)?, year: row.get(0)?,
month: row.get(1)?, month: row.get(1)?,

View File

@ -61,7 +61,7 @@ async fn main() -> io::Result<()> {
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
// store db pool as Data object // store db pool as Data object
.data(pool.clone()) .app_data(web::Data::new(pool.clone()))
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.service( .service(
web::resource("/asyncio_weather").route(web::get().to(asyncio_weather)), web::resource("/asyncio_weather").route(web::get().to(asyncio_weather)),

View File

@ -2,14 +2,14 @@
name = "r2d2-example" name = "r2d2-example"
version = "1.0.0" version = "1.0.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
edition = "2018" edition = "2021"
[dependencies] [dependencies]
actix-web = "3" actix-web = "4.0.0-beta.21"
env_logger = "0.8" env_logger = "0.9.0"
uuid = { version = "0.8", features = ["v4"] } uuid = { version = "1.0.0-alpha.1", features = ["v4"] }
r2d2 = "0.8" r2d2 = "0.8"
r2d2_sqlite = "0.14" r2d2_sqlite = "0.18.0"
rusqlite = "0.21" rusqlite = "0.25.4"

View File

@ -1,7 +1,10 @@
//! Actix web r2d2 example //! Actix web r2d2 example
use std::io; use std::io;
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer}; use actix_web::{
error::InternalError, http::StatusCode, middleware, web, App, Error, HttpResponse,
HttpServer,
};
use r2d2::Pool; use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager; use r2d2_sqlite::SqliteConnectionManager;
@ -26,8 +29,9 @@ async fn index(
}) })
}) })
.await .await
.unwrap()
.map(|user| HttpResponse::Ok().json(user)) .map(|user| HttpResponse::Ok().json(user))
.map_err(|_| HttpResponse::InternalServerError())?; .map_err(|e| InternalError::new(e, StatusCode::INTERNAL_SERVER_ERROR))?;
Ok(res) Ok(res)
} }
@ -43,7 +47,7 @@ async fn main() -> io::Result<()> {
// start http server // start http server
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.data(pool.clone()) // <- store db pool in app state .app_data(web::Data::new(pool.clone())) // <- store db pool in app state
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.route("/{name}", web::get().to(index)) .route("/{name}", web::get().to(index))
}) })

View File

@ -2,17 +2,17 @@
name = "sqlx_todo" name = "sqlx_todo"
version = "0.1.0" version = "0.1.0"
authors = ["Milan Zivkovic <zivkovic.milan@gmail.com>"] authors = ["Milan Zivkovic <zivkovic.milan@gmail.com>"]
edition = "2018" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
actix-web = "3" actix-web = "4.0.0-beta.21"
serde = "1.0.106" serde = "1.0.106"
serde_json = "1.0.51" serde_json = "1.0.51"
sqlx = { version = "0.3", features = ["sqlite"] } sqlx = { version = "0.5.9", features = ["sqlite", "runtime-actix-rustls"] }
dotenv = "0.15.0" dotenv = "0.15.0"
env_logger = "0.7.1" env_logger = "0.9.0"
log = "0.4.8" log = "0.4.8"
anyhow = "1.0.28" anyhow = "1.0.28"
futures = "0.3.13" futures = "0.3.13"

View File

@ -19,7 +19,12 @@ $ cd database_interactions/sqlx_todo
## Set up the database ## Set up the database
* Create new database using `schema.sql` * Create new database:
```bash
./setup_db.sh
```
* Copy `.env.example` into `.env` and adjust `DATABASE_URL` to match your SQLite address, if needed * Copy `.env.example` into `.env` and adjust `DATABASE_URL` to match your SQLite address, if needed
```sh ```sh
@ -35,4 +40,4 @@ To run the application execute:
cargo run cargo run
``` ```
By default application will be available on `http://localhost:8080`. If you wish to change address or port you can do it inside the `.env` file By default the application will be available on `http://localhost:8080`. If you wish to change address or port you can do it inside the `.env` file

View File

@ -0,0 +1,2 @@
#!/usr/bin/env bash
sqlite3 test.db < schema.sql

View File

@ -40,7 +40,7 @@ async fn main() -> Result<()> {
.expect("PORT should be a u16"); .expect("PORT should be a u16");
info!("using sqlite database at: {}", &database_url); info!("using sqlite database at: {}", &database_url);
let db_pool = SqlitePool::new(&database_url).await?; let db_pool = SqlitePool::connect(&database_url).await?;
// startup connection+schema check // startup connection+schema check
sqlx::query!("SELECT * FROM todos") sqlx::query!("SELECT * FROM todos")
@ -51,7 +51,7 @@ async fn main() -> Result<()> {
let server = HttpServer::new(move || { let server = HttpServer::new(move || {
App::new() App::new()
// pass database pool to application so we can access it inside handlers // pass database pool to application so we can access it inside handlers
.data(db_pool.clone()) .app_data(web::Data::new(db_pool.clone()))
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.route("/", web::get().to(index)) .route("/", web::get().to(index))
.configure(todo::init) // init todo routes .configure(todo::init) // init todo routes

View File

@ -1,4 +1,4 @@
use actix_web::{Error, HttpRequest, HttpResponse, Responder}; use actix_web::{body::BoxBody, HttpRequest, HttpResponse, Responder};
use anyhow::Result; use anyhow::Result;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sqlx::sqlite::SqliteRow; use sqlx::sqlite::SqliteRow;
@ -14,17 +14,16 @@ pub struct TodoRequest {
// this struct will be used to represent database record // this struct will be used to represent database record
#[derive(Serialize, FromRow)] #[derive(Serialize, FromRow)]
pub struct Todo { pub struct Todo {
pub id: i32, pub id: i64,
pub description: String, pub description: String,
pub done: bool, pub done: bool,
} }
// implementation of Actix Responder for Todo struct so we can return Todo from action handler // implementation of Actix Responder for Todo struct so we can return Todo from action handler
impl Responder for Todo { impl Responder for Todo {
type Error = Error; type Body = BoxBody;
type Future = HttpResponse;
fn respond_to(self, _req: &HttpRequest) -> Self::Future { fn respond_to(self, _req: &HttpRequest) -> HttpResponse {
// create response and set content type // create response and set content type
HttpResponse::Ok().json(&self) HttpResponse::Ok().json(&self)
} }
@ -130,7 +129,8 @@ impl Todo {
id, id,
) )
.execute(&mut tx) .execute(&mut tx)
.await?; .await?
.rows_affected();
if n == 0 { if n == 0 {
return Ok(None); return Ok(None);
@ -168,7 +168,8 @@ impl Todo {
id, id,
) )
.execute(&mut tx) .execute(&mut tx)
.await?; .await?
.rows_affected();
tx.commit().await?; tx.commit().await?;
Ok(n_deleted) Ok(n_deleted)