From 9cc80af5e4fdd830391598267aadc276cb0d1013 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Sat, 20 Mar 2021 00:16:52 -0700 Subject: [PATCH] feat(examples): add initial postgres+async-std quickstart --- Cargo.lock | 2 + examples/Cargo.lock | 40 +++++++++++++++++++ examples/Cargo.toml | 3 +- .../quickstart/postgres+async-std/Cargo.toml | 15 +++++++ .../quickstart/postgres+async-std/src/main.rs | 36 +++++++++++++++++ 5 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 examples/quickstart/postgres+async-std/Cargo.toml create mode 100644 examples/quickstart/postgres+async-std/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 5d3b9da8..2771a0fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1068,6 +1068,7 @@ dependencies = [ "futures-util", "sqlx-core", "sqlx-mysql", + "sqlx-postgres", ] [[package]] @@ -1122,6 +1123,7 @@ version = "0.6.0-pre" dependencies = [ "anyhow", "atoi", + "base64", "bitflags", "bytes", "bytestring", diff --git a/examples/Cargo.lock b/examples/Cargo.lock index 29bb183a..bd2ebd81 100644 --- a/examples/Cargo.lock +++ b/examples/Cargo.lock @@ -153,6 +153,15 @@ version = "4.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e91831deabf0d6d7ec49552e489aed63b7456a7a3c46cff62adad428110b0af0" +[[package]] +name = "atoi" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "616896e05fc0e2649463a93a15183c6a16bf03413a7af88ef1285ddedfa9cda5" +dependencies = [ + "num-traits", +] + [[package]] name = "atomic-waker" version = "1.0.0" @@ -987,6 +996,7 @@ dependencies = [ "futures-util", "sqlx-core", "sqlx-mysql", + "sqlx-postgres", ] [[package]] @@ -1028,6 +1038,25 @@ dependencies = [ "url", ] +[[package]] +name = "sqlx-postgres" +version = "0.6.0-pre" +dependencies = [ + "atoi", + "base64", + "bitflags", + "bytes", + "bytestring", + "either", + "futures-io", + "futures-util", + "log", + "memchr", + "percent-encoding", + "sqlx-core", + "url", +] + [[package]] name = "sqlx_example_quickstart_mysql_async-std" version = "0.0.0" @@ -1060,6 +1089,17 @@ dependencies = [ "tokio", ] +[[package]] +name = "sqlx_example_quickstart_postgres_async-std" +version = "0.0.0" +dependencies = [ + "anyhow", + "async-std", + "env_logger", + "log", + "sqlx", +] + [[package]] name = "subtle" version = "2.4.0" diff --git a/examples/Cargo.toml b/examples/Cargo.toml index dff0f644..e3a0d56b 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -2,5 +2,6 @@ members = [ "quickstart/mysql+blocking", "quickstart/mysql+async-std", - "quickstart/mysql+tokio" + "quickstart/mysql+tokio", + "quickstart/postgres+async-std", ] diff --git a/examples/quickstart/postgres+async-std/Cargo.toml b/examples/quickstart/postgres+async-std/Cargo.toml new file mode 100644 index 00000000..0beb18e1 --- /dev/null +++ b/examples/quickstart/postgres+async-std/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "sqlx_example_quickstart_postgres_async-std" +version = "0.0.0" +license = "MIT OR Apache-2.0" +edition = "2018" +authors = [ + "LaunchBadge " +] + +[dependencies] +anyhow = "1.0.36" +async-std = { version = "1.8.0", features = ["attributes"] } +log = "0.4" +env_logger = "0.8.2" +sqlx = { path = "../../../sqlx", features = [ "async-std", "postgres-async" ] } diff --git a/examples/quickstart/postgres+async-std/src/main.rs b/examples/quickstart/postgres+async-std/src/main.rs new file mode 100644 index 00000000..afa6f2b4 --- /dev/null +++ b/examples/quickstart/postgres+async-std/src/main.rs @@ -0,0 +1,36 @@ +use sqlx::postgres::{PgConnectOptions, PgConnection}; + +#[async_std::main] +async fn main() -> anyhow::Result<()> { + env_logger::try_init()?; + + // start by parsing the connection URL (typically from an environment variable) + let mut conn: PgConnection = PgConnectOptions::parse("postgres://postgres@localhost")? + // set a password (perhaps from somewhere else than the rest of the URL) + .password("password") + // connect to the database (blocking) + .connect() + .await?; + + // the following are equivalent to the above: + + // let mut conn = PgConnection::::connect("mysql://root:password@localhost").await?; + // let mut conn = ::connect("mysql://root:password@localhost").await?; + // let mut conn = PgConnectOptions::::new().username("root").password("password").connect().await?; + // let mut conn = ::new().username("root").password("password").connect().await?; + + // the <...> syntax is an escape into the type syntax + // when writing a *type*, Rust allows default type parameters + // as opposed to writing a *path* where it does not (yet) + + // ping, this makes sure the server is still there + // hopefully it is – we did just connect to it + conn.ping().await?; + + // close the connection explicitly + // this kindly informs the database server that we'll be terminating + // while not strictly required, the server will dispose of connection resources faster + conn.close().await?; + + Ok(()) +}