add few dbs
This commit is contained in:
46
Cargo.toml
46
Cargo.toml
@@ -2,7 +2,49 @@
|
||||
name = "test-sqlx"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
authors = ["Bassem Girgis <brgirgis@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
# logging
|
||||
log = { version = "^0.4", features = [
|
||||
"max_level_debug",
|
||||
"release_max_level_warn",
|
||||
] }
|
||||
fern = "^0.5"
|
||||
|
||||
# sqlx
|
||||
sqlx = { version = "0.5.6", features = [
|
||||
# tokio + rustls
|
||||
"runtime-tokio-rustls",
|
||||
# PostgresSQL
|
||||
"postgres",
|
||||
"uuid",
|
||||
"json",
|
||||
# MySQL and MariaDB
|
||||
"mysql",
|
||||
# MS SQL
|
||||
"mssql",
|
||||
# SQLite
|
||||
"sqlite",
|
||||
# misc
|
||||
"macros",
|
||||
"chrono",
|
||||
"tls",
|
||||
] }
|
||||
|
||||
tokio = { version = "1", features = [
|
||||
"rt-multi-thread",
|
||||
"time",
|
||||
"fs",
|
||||
"macros",
|
||||
"net",
|
||||
] }
|
||||
|
||||
# sqlx = { version = "0.5.6", features = [
|
||||
# "postgres",
|
||||
# "runtime-async-std-rustls",
|
||||
# ] }
|
||||
# async-std = { version = "1.6", features = ["attributes"] }
|
||||
|
||||
# other
|
||||
chrono = "^0.4"
|
||||
|
||||
@@ -15,13 +15,15 @@ CREATE TABLE events
|
||||
|
||||
CREATE
|
||||
USER
|
||||
'demo_user'@'localhost'
|
||||
'demo_user'@'%'
|
||||
IDENTIFIED BY
|
||||
'password';
|
||||
'demo_pass';
|
||||
|
||||
GRANT
|
||||
ALL
|
||||
ALL privileges
|
||||
ON
|
||||
demo.*
|
||||
TO
|
||||
'demo_user'@'localhost';
|
||||
'demo_user'@'%';
|
||||
|
||||
FLUSH PRIVILEGES;
|
||||
|
||||
@@ -15,13 +15,15 @@ CREATE TABLE events
|
||||
|
||||
CREATE
|
||||
USER
|
||||
'demo_user'@'localhost'
|
||||
'demo_user'@'%'
|
||||
IDENTIFIED BY
|
||||
'password';
|
||||
'demo_pass';
|
||||
|
||||
GRANT
|
||||
ALL
|
||||
ALL privileges
|
||||
ON
|
||||
demo.*
|
||||
TO
|
||||
'demo_user'@'localhost';
|
||||
'demo_user'@'%';
|
||||
|
||||
FLUSH PRIVILEGES;
|
||||
|
||||
109
src/main.rs
109
src/main.rs
@@ -1,3 +1,108 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use log::info;
|
||||
|
||||
use sqlx::{
|
||||
mysql::MySqlPoolOptions,
|
||||
postgres::PgPoolOptions,
|
||||
};
|
||||
|
||||
fn setup_logger() -> Result<(), fern::InitError> {
|
||||
fern::Dispatch::new()
|
||||
.format(|out, message, record| {
|
||||
out.finish(format_args!(
|
||||
"{}[{}][{}] {}",
|
||||
chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"),
|
||||
record.target(),
|
||||
record.level(),
|
||||
message
|
||||
))
|
||||
})
|
||||
.level(log::LevelFilter::Debug)
|
||||
.chain(std::io::stdout())
|
||||
.chain(fern::log_file("output.log")?)
|
||||
.apply()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn check_maria_db() -> Result<(), sqlx::Error> {
|
||||
let pool = MySqlPoolOptions::new()
|
||||
.max_connections(5)
|
||||
.connect("mysql://demo_user:demo_pass@localhost:8084/demo")
|
||||
.await?;
|
||||
|
||||
// Make a simple query to return the given parameter
|
||||
let row = sqlx::query("SELECT * from events")
|
||||
.fetch_all(&pool)
|
||||
.await?;
|
||||
|
||||
info!("Received {:?}", row);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn check_mysql() -> Result<(), sqlx::Error> {
|
||||
let pool = MySqlPoolOptions::new()
|
||||
.max_connections(5)
|
||||
.connect("mysql://demo_user:demo_pass@localhost:8086/demo")
|
||||
.await?;
|
||||
|
||||
// Make a simple query to return the given parameter
|
||||
let row = sqlx::query("SELECT * from events")
|
||||
.fetch_all(&pool)
|
||||
.await?;
|
||||
|
||||
info!("Received {:?}", row);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn check_postgres() -> Result<(), sqlx::Error> {
|
||||
// Create a connection pool
|
||||
// for MySQL, use MySqlPoolOptions::new()
|
||||
// for SQLite, use SqlitePoolOptions::new()
|
||||
// etc.
|
||||
let pool = PgPoolOptions::new()
|
||||
.max_connections(5)
|
||||
.connect("postgres://demo_user:demo_pass@localhost:8087/demo")
|
||||
.await?;
|
||||
|
||||
// Make a simple query to return the given parameter
|
||||
let row: (i64,) = sqlx::query_as("SELECT $1")
|
||||
.bind(150_i64)
|
||||
.fetch_one(&pool)
|
||||
.await?;
|
||||
|
||||
assert_eq!(row.0, 150);
|
||||
|
||||
info!("Received {}", row.0);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
//#[async_std::main]
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), sqlx::Error> {
|
||||
setup_logger().unwrap();
|
||||
|
||||
let ts = vec![
|
||||
tokio::spawn(async move {
|
||||
check_postgres().await.unwrap();
|
||||
}),
|
||||
tokio::spawn(async move {
|
||||
check_mysql().await.unwrap();
|
||||
}),
|
||||
// tokio::spawn(async move {
|
||||
// check_postgres().await.unwrap();
|
||||
// }),
|
||||
tokio::spawn(async move {
|
||||
check_maria_db().await.unwrap();
|
||||
}),
|
||||
];
|
||||
|
||||
info!("---- Spawned all ----");
|
||||
|
||||
for t in ts {
|
||||
t.await.unwrap();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user