generated from alecodes/base-template
feat: add discover command
This commit is contained in:
parent
48a582eb7e
commit
fc0a9b4206
6 changed files with 261 additions and 12 deletions
37
src/sql.rs
37
src/sql.rs
|
|
@ -1,4 +1,6 @@
|
|||
use clap::ValueEnum;
|
||||
use sea_schema::postgres::discovery::SchemaDiscovery;
|
||||
use sqlx::PgPool;
|
||||
use url::Url;
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
|
|
@ -10,12 +12,12 @@ pub enum Database {
|
|||
Postgres,
|
||||
}
|
||||
|
||||
pub async fn handle_query(url: String, query: &String) -> Result<()> {
|
||||
let parse = Url::parse(&url)?;
|
||||
async fn get_connector(url: &String) -> Result<(postgres::PgConnector, String)> {
|
||||
let parse = Url::parse(url)?;
|
||||
let scheme = parse.scheme();
|
||||
|
||||
let connector = match scheme {
|
||||
"postgresql" => postgres::PgConnector::new(url).await?,
|
||||
"postgresql" => postgres::PgConnector::new(url.to_owned()).await?,
|
||||
&_ => {
|
||||
return Err(Error::Generic(format!(
|
||||
"Database `{}` is not supported",
|
||||
|
|
@ -23,6 +25,11 @@ pub async fn handle_query(url: String, query: &String) -> Result<()> {
|
|||
)))
|
||||
}
|
||||
};
|
||||
Ok((connector, scheme.to_owned()))
|
||||
}
|
||||
|
||||
pub async fn handle_query(url: String, query: &String) -> Result<()> {
|
||||
let (connector, _) = get_connector(&url).await?;
|
||||
|
||||
let rows = connector.query(query).await?;
|
||||
|
||||
|
|
@ -32,3 +39,27 @@ pub async fn handle_query(url: String, query: &String) -> Result<()> {
|
|||
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
pub async fn discover_scheme(url: String, schema: Option<String>) -> Result<()> {
|
||||
let (_, db_name) = get_connector(&url).await?;
|
||||
|
||||
let schema_discovery = match db_name.as_str() {
|
||||
"postgresql" => {
|
||||
let schema = schema.unwrap_or("public".to_owned());
|
||||
let pool = PgPool::connect(url.as_str()).await?;
|
||||
SchemaDiscovery::new(pool, schema.as_str())
|
||||
}
|
||||
&_ => {
|
||||
return Err(Error::Generic(format!(
|
||||
"Database `{}` is not supported",
|
||||
db_name
|
||||
)))
|
||||
}
|
||||
};
|
||||
|
||||
let schema = schema_discovery.discover().await?;
|
||||
|
||||
println!("{:#?}", schema);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue