From 141bd42fb4aa64c2518dafb5d66fbb7d83c4aeff Mon Sep 17 00:00:00 2001 From: Bassem Girgis Date: Wed, 18 Aug 2021 03:56:12 -0500 Subject: [PATCH] add few dbs --- Cargo.toml | 46 ++++++++++++++++++- db/mariadb/init.sql | 10 ++-- db/mysql/init.sql | 10 ++-- src/main.rs | 109 +++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 163 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4599132..9c53d1d 100644 --- a/Cargo.toml +++ b/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 "] [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" diff --git a/db/mariadb/init.sql b/db/mariadb/init.sql index e115d49..20258c9 100644 --- a/db/mariadb/init.sql +++ b/db/mariadb/init.sql @@ -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; diff --git a/db/mysql/init.sql b/db/mysql/init.sql index 55568f6..e975603 100644 --- a/db/mysql/init.sql +++ b/db/mysql/init.sql @@ -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; diff --git a/src/main.rs b/src/main.rs index e7a11a9..94c4183 100644 --- a/src/main.rs +++ b/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(()) }