Skip to content

Commit

Permalink
feat: add basic parse for sql
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Oct 28, 2022
1 parent af27c35 commit b8f9e22
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions fkl_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ indexmap = "1.9.1"

serde = { version = "1", features = ["derive"] }

url = "2.3.1"

[dev-dependencies]
mockall = "0.11.2"
92 changes: 92 additions & 0 deletions fkl_parser/src/mir/implementation/datasource.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum Datasource {
MySql(MySqlDatasource),
Postgres(PostgresDatasource),
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct MySqlDatasource {
pub host: String,
pub port: u16,
pub username: String,
pub password: String,
pub database: String,
}


#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PostgresDatasource {
pub host: String,
pub port: u16,
pub username: String,
pub password: String,
pub database: String,
}

fn parse_database_url(url: &str) -> Result<Datasource, String> {
let url = url::Url::parse(url).map_err(|e| e.to_string())?;
let scheme = url.scheme();
let host = url.host_str().ok_or("host is required")?.to_string();
let port = url.port().ok_or("port is required")?;
let username = url.username().to_string();
let password = url.password().unwrap_or("").to_string();
let database = url.path().trim_start_matches('/').to_string();

match scheme {
"mysql" => Ok(Datasource::MySql(MySqlDatasource {
host,
port,
username,
password,
database,
})),
"postgresql" => Ok(Datasource::Postgres(PostgresDatasource {
host,
port,
username,
password,
database,
})),
_ => Err(format!("unsupported scheme: {}", scheme)),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_database_url() {
let url = "mysql://username:password@localhost:3306/database";
let datasource = parse_database_url(url).unwrap();
assert_eq!(
datasource,
Datasource::MySql(MySqlDatasource {
host: "localhost".to_string(),
port: 3306,
username: "username".to_string(),
password: "password".to_string(),
database: "database".to_string(),
})
);
}

#[test]
fn test_parse_normal_postgres() {
let url = "postgresql://localhost:5432/yourdb";
let datasource = parse_database_url(url).unwrap();

assert_eq!(
datasource,
Datasource::Postgres(PostgresDatasource {
host: "localhost".to_string(),
port: 5432,
username: "".to_string(),
password: "".to_string(),
database: "yourdb".to_string(),
})
);
}
}
1 change: 1 addition & 0 deletions fkl_parser/src/mir/implementation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod http_api_impl;
pub mod http_impl;
pub mod implementation;
pub mod authorization;
pub mod datasource;

pub use validation::*;
pub use http_impl::*;
Expand Down

0 comments on commit b8f9e22

Please sign in to comment.