get schema

This commit is contained in:
Takayuki Maeda 2021-08-07 00:41:38 +09:00
parent fff9b1aa02
commit cbd33b1659
2 changed files with 27 additions and 36 deletions

View File

@ -19,6 +19,12 @@ host = "localhost"
port = 3306 port = 3306
database = "employees" database = "employees"
[[conn]]
type = "postgres"
user = "postgres"
host = "localhost"
port = 5432
[[conn]] [[conn]]
type = "postgres" type = "postgres"
user = "postgres" user = "postgres"

View File

@ -32,31 +32,15 @@ impl Pool for PostgresPool {
for db in databases { for db in databases {
list.push(Database::new( list.push(Database::new(
db.clone(), db.clone(),
vec![Schema { self.get_tables(db.clone()).await?,
name: "schema".to_string(),
tables: vec![Table {
name: "table".to_string(),
create_time: None,
update_time: None,
engine: None,
schema: Some("schema".to_string()),
}],
}
.into()],
// get_tables(db.clone(), &self.pool)
// .await?
// .into_iter()
// .map(|table| table.into())
// .collect(),
)) ))
} }
Ok(list) Ok(list)
} }
async fn get_tables(&self, database: String) -> anyhow::Result<Vec<Child>> { async fn get_tables(&self, database: String) -> anyhow::Result<Vec<Child>> {
let mut rows = sqlx::query( let mut rows =
"SELECT * FROM information_schema.tables WHERE table_schema='public' and table_catalog = $1", sqlx::query("SELECT * FROM information_schema.tables WHERE table_catalog = $1")
)
.bind(database) .bind(database)
.fetch(&self.pool); .fetch(&self.pool);
let mut tables = Vec::new(); let mut tables = Vec::new();
@ -70,20 +54,21 @@ impl Pool for PostgresPool {
}) })
} }
let mut schemas = vec![]; let mut schemas = vec![];
for (key, group) in &tables.iter().group_by(|t| { for (key, group) in &tables
t.schema .iter()
.as_ref() .sorted_by(|a, b| Ord::cmp(&b.schema, &a.schema))
.map(|schema| schema.to_string()) .group_by(|t| t.schema.as_ref())
.unwrap_or_else(|| "".to_string()) {
}) { if let Some(key) = key {
schemas.push( schemas.push(
Schema { Schema {
name: key, name: key.to_string(),
tables: group.cloned().collect(), tables: group.cloned().collect(),
} }
.into(), .into(),
) )
} }
}
Ok(schemas) Ok(schemas)
} }
@ -100,7 +85,7 @@ impl Pool for PostgresPool {
database = database.name, database = database.name,
table = table.name, table = table.name,
filter = filter, filter = filter,
table_schema = "public", table_schema = table.schema.clone().unwrap_or("public".to_string()),
page = page, page = page,
limit = RECORDS_LIMIT_PER_PAGE limit = RECORDS_LIMIT_PER_PAGE
) )
@ -109,7 +94,7 @@ impl Pool for PostgresPool {
r#"SELECT * FROM "{database}"."{table_schema}"."{table}" limit {limit} offset {page}"#, r#"SELECT * FROM "{database}"."{table_schema}"."{table}" limit {limit} offset {page}"#,
database = database.name, database = database.name,
table = table.name, table = table.name,
table_schema = "public", table_schema = table.schema.clone().unwrap_or("public".to_string()),
page = page, page = page,
limit = RECORDS_LIMIT_PER_PAGE limit = RECORDS_LIMIT_PER_PAGE
) )