docs: split quickstart into mysql+tokio, mysql+async-std, and mysql+blocking

This commit is contained in:
Ryan Leckey 2021-01-10 19:52:50 -08:00
parent 8562dba0dc
commit e1e6f594c1
No known key found for this signature in database
GPG Key ID: F8AA68C235AB08C9
10 changed files with 172 additions and 60 deletions

58
Cargo.lock generated
View File

@ -1115,17 +1115,6 @@ dependencies = [
"tokio",
]
[[package]]
name = "sqlx-example-quickstart"
version = "0.0.0"
dependencies = [
"anyhow",
"async-std",
"env_logger",
"log",
"sqlx",
]
[[package]]
name = "sqlx-mysql"
version = "0.6.0-pre"
@ -1150,6 +1139,38 @@ dependencies = [
"url",
]
[[package]]
name = "sqlx_example_quickstart_mysql_async-std"
version = "0.0.0"
dependencies = [
"anyhow",
"async-std",
"env_logger",
"log",
"sqlx",
]
[[package]]
name = "sqlx_example_quickstart_mysql_blocking"
version = "0.0.0"
dependencies = [
"anyhow",
"env_logger",
"log",
"sqlx",
]
[[package]]
name = "sqlx_example_quickstart_mysql_tokio"
version = "0.0.0"
dependencies = [
"anyhow",
"env_logger",
"log",
"sqlx",
"tokio",
]
[[package]]
name = "subtle"
version = "2.4.0"
@ -1250,15 +1271,30 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d258221f566b6c803c7b4714abadc080172b272090cdc5e244a6d4dd13c3a6bd"
dependencies = [
"autocfg 1.0.1",
"bytes",
"libc",
"memchr",
"mio",
"num_cpus",
"once_cell",
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
"tokio-macros",
"winapi",
]
[[package]]
name = "tokio-macros"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42517d2975ca3114b22a16192634e8241dc5cc1f130be194645970cc1c371494"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "typenum"
version = "1.12.0"

View File

@ -4,5 +4,9 @@ members = [
"sqlx-core",
"sqlx-mysql",
"sqlx",
"examples/quickstart"
# examples
"examples/quickstart/mysql+blocking",
"examples/quickstart/mysql+async-std",
"examples/quickstart/mysql+tokio"
]

View File

@ -1,18 +0,0 @@
[package]
name = "sqlx-example-quickstart"
version = "0.0.0"
license = "MIT OR Apache-2.0"
edition = "2018"
authors = [
"LaunchBadge <contact@launchbadge.com>"
]
[dependencies]
#actix-web = "3.3.2"
anyhow = "1.0.36"
#async-std = { version = "1.8.0", features = ["attributes"] }
tokio = { version = "1.0.1", features = ["rt", "rt-multi-thread", "macros"] }
log = "0.4"
env_logger = "0.8.2"
#sqlx = { path = "../../sqlx", features = ["tokio", "mysql"] }
sqlx = { path = "../../sqlx", features = ["blocking", "tokio", "mysql"] }

View File

@ -0,0 +1,15 @@
[package]
name = "sqlx_example_quickstart_mysql_async-std"
version = "0.0.0"
license = "MIT OR Apache-2.0"
edition = "2018"
authors = [
"LaunchBadge <contact@launchbadge.com>"
]
[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", "mysql"] }

View File

@ -0,0 +1,26 @@
use sqlx::mysql::{MySqlConnectOptions, MySqlConnection};
use sqlx::prelude::*;
#[async_std::main]
async fn main() -> anyhow::Result<()> {
env_logger::try_init()?;
// parse the connection URL
let mut conn: MySqlConnection = MySqlConnectOptions::parse("mysql://root@localhost")?
// set a password (perhaps from somewhere else than the rest of the URL)
.password("password")
// connect to the database (asynchronously)
.connect()
.await?;
// 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(())
}

View File

@ -0,0 +1,14 @@
[package]
name = "sqlx_example_quickstart_mysql_blocking"
version = "0.0.0"
license = "MIT OR Apache-2.0"
edition = "2018"
authors = [
"LaunchBadge <contact@launchbadge.com>"
]
[dependencies]
anyhow = "1.0.36"
log = "0.4"
env_logger = "0.8.2"
sqlx = { path = "../../../sqlx", features = [ "blocking", "mysql"] }

View File

@ -0,0 +1,24 @@
use sqlx::mysql::{MySqlConnectOptions, MySqlConnection};
use sqlx::prelude::*;
fn main() -> anyhow::Result<()> {
env_logger::try_init()?;
// parse the connection URL
let mut conn: MySqlConnection = MySqlConnectOptions::parse("mysql://root@localhost")?
// set a password (perhaps from somewhere else than the rest of the URL)
.password("password")
// connect to the database (blocking)
.connect()?;
// ping, this makes sure the server is still there
// hopefully it is we did just connect to it
conn.ping()?;
// 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()?;
Ok(())
}

View File

@ -0,0 +1,15 @@
[package]
name = "sqlx_example_quickstart_mysql_tokio"
version = "0.0.0"
license = "MIT OR Apache-2.0"
edition = "2018"
authors = [
"LaunchBadge <contact@launchbadge.com>"
]
[dependencies]
anyhow = "1.0.36"
tokio = { version = "1", features = ["full"] }
log = "0.4"
env_logger = "0.8.2"
sqlx = { path = "../../../sqlx", features = [ "tokio", "mysql"] }

View File

@ -0,0 +1,26 @@
use sqlx::mysql::{MySqlConnectOptions, MySqlConnection};
use sqlx::prelude::*;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::try_init()?;
// parse the connection URL
let mut conn: MySqlConnection = MySqlConnectOptions::parse("mysql://root@localhost")?
// set a password (perhaps from somewhere else than the rest of the URL)
.password("password")
// connect to the database (asynchronously)
.connect()
.await?;
// 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(())
}

View File

@ -1,30 +0,0 @@
// use sqlx::prelude::*;
use sqlx::blocking::{prelude::*, Blocking};
use sqlx::mysql::MySqlConnection;
// #[tokio::main]
// async fn main() -> anyhow::Result<()> {
// env_logger::try_init()?;
//
// // connect to the database
// let mut conn = <MySqlConnection>::connect("mysql://root:password@localhost").await?;
//
// // ping, say HAI
// conn.ping().await?;
//
// // , and now close the connection explicitly
// conn.close().await?;
//
// Ok(())
// }
fn main() -> anyhow::Result<()> {
env_logger::try_init()?;
let mut conn = <MySqlConnection<Blocking>>::connect("mysql://root:password@localhost")?;
conn.ping()?;
conn.close()?;
Ok(())
}