Skip to content

Commit

Permalink
feat: add basic connection to database
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Oct 28, 2022
1 parent 78ed983 commit f9eee49
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 16 deletions.
3 changes: 1 addition & 2 deletions docs/samples/impl.fkl
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ impl UserCreated {

env Local {
datasource {
url: "postgresql://localhost:5432/yourdb"
// two different types?
url: "mysql://localhost:5432/yourdb"
driver: postgresql
host: "localhost"
port: 5432
Expand Down
4 changes: 3 additions & 1 deletion fkl_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ sqlx = { version = "0.6", features = [
# microsoft sql server
#tiberius = { version = "0.11" }
#oracle = { version = "0.5" }

futures = "0.3"
tokio = { version = "1", features = ["full"] }

async-trait = "0.1.58"

[dev-dependencies]
trycmd = "0.14.0"

Expand Down
16 changes: 14 additions & 2 deletions fkl_cli/src/builtin/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::path::PathBuf;
use std::process;

use log::error;
use log::{error, info};

use fkl_parser::mir::LayeredArchitecture;
use fkl_parser::mir::{Datasource, Environment, LayeredArchitecture};

use crate::datasource::mysql_connector::MysqlConnector;
use crate::exec::LayeredGuardingExec;

pub mod endpoint_runner;
Expand All @@ -20,3 +21,14 @@ pub fn guarding_runner(root: PathBuf, layered: &LayeredArchitecture) {
process::exit(0x0100);
}
}

pub(crate) async fn test_connection_runner(env: &Environment) {
info!("test connection: {:?}", env);
match &env.datasources[0] {
Datasource::MySql(mysql) => {
MysqlConnector::new(mysql.clone()).test_connection().await;
}

Datasource::Postgres(_) => {}
}
}
2 changes: 2 additions & 0 deletions fkl_cli/src/datasource/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub mod mysql_connector;
use async_trait::async_trait;

#[async_trait]
pub trait DatasourceConnector {
fn test_connection(&self) -> bool;
}
32 changes: 22 additions & 10 deletions fkl_cli/src/datasource/mysql_connector.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::time::Duration;

use log::error;
use sqlx::mysql::MySqlPoolOptions;

use fkl_parser::mir::MySqlDatasource;
use crate::datasource::DatasourceConnector;

pub struct MysqlConnector {
config: MySqlDatasource,
Expand All @@ -14,12 +17,21 @@ impl MysqlConnector {
}
}

// impl DatasourceConnector for MysqlConnector {
// fn test_connection(&self) -> bool {
// let pool = MySqlPoolOptions::new()
// .max_connections(5)
// .connect("postgres://postgres:password@localhost/test").await?;
//
//
// }
// }
impl MysqlConnector {
pub(crate) async fn test_connection(&self) -> bool {
let options = MySqlPoolOptions::new();

match options
.max_connections(5)
.max_lifetime(Duration::from_secs(10 * 60))
.connect(&self.config.url()).await {
Ok(_) => {
true
}
Err(err) => {
error!("error: {:?}", err);
false
}
}
}
}
3 changes: 2 additions & 1 deletion fkl_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ async fn main() {
builtin::guarding_runner(root, &layered);
}
RunFuncName::TestConnection => {

let env = mir.envs;
builtin::test_connection_runner( &env[0]).await;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions fkl_parser/src/mir/implementation/datasource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl Datasource {
}
}

#[allow(dead_code)]
fn url(&self) -> String {
match self {
Datasource::MySql(config) => MySqlDatasource::url(config),
Expand Down

0 comments on commit f9eee49

Please sign in to comment.