mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-12-29 21:00:54 +00:00
docs: split quickstart into mysql+tokio, mysql+async-std, and mysql+blocking
This commit is contained in:
parent
8562dba0dc
commit
e1e6f594c1
58
Cargo.lock
generated
58
Cargo.lock
generated
@ -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"
|
||||
|
||||
@ -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"
|
||||
]
|
||||
|
||||
@ -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"] }
|
||||
15
examples/quickstart/mysql+async-std/Cargo.toml
Normal file
15
examples/quickstart/mysql+async-std/Cargo.toml
Normal 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"] }
|
||||
26
examples/quickstart/mysql+async-std/src/main.rs
Normal file
26
examples/quickstart/mysql+async-std/src/main.rs
Normal 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(())
|
||||
}
|
||||
14
examples/quickstart/mysql+blocking/Cargo.toml
Normal file
14
examples/quickstart/mysql+blocking/Cargo.toml
Normal 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"] }
|
||||
24
examples/quickstart/mysql+blocking/src/main.rs
Normal file
24
examples/quickstart/mysql+blocking/src/main.rs
Normal 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(())
|
||||
}
|
||||
15
examples/quickstart/mysql+tokio/Cargo.toml
Normal file
15
examples/quickstart/mysql+tokio/Cargo.toml
Normal 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"] }
|
||||
26
examples/quickstart/mysql+tokio/src/main.rs
Normal file
26
examples/quickstart/mysql+tokio/src/main.rs
Normal 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(())
|
||||
}
|
||||
@ -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(())
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user