show user defined types

pull/35/head
Takayuki Maeda 3 years ago
parent e1ae7d0f9a
commit c9deca0087

22
Cargo.lock generated

@ -567,13 +567,19 @@ dependencies = [
"ahash 0.4.7",
]
[[package]]
name = "hashbrown"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]]
name = "hashlink"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d99cf782f0dc4372d26846bec3de7804ceb5df083c2d4462c0b8d2330e894fa8"
dependencies = [
"hashbrown",
"hashbrown 0.9.1",
]
[[package]]
@ -621,6 +627,16 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "indexmap"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc633605454125dec4b66843673f01c7df2b89479b32e0ed634e43a91cff62a5"
dependencies = [
"autocfg 1.0.1",
"hashbrown 0.11.2",
]
[[package]]
name = "instant"
version = "0.1.9"
@ -1306,6 +1322,7 @@ version = "1.0.64"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79"
dependencies = [
"indexmap",
"itoa",
"ryu",
"serde",
@ -1448,6 +1465,8 @@ dependencies = [
"rsa",
"rust_decimal",
"rustls",
"serde",
"serde_json",
"sha-1",
"sha2",
"smallvec",
@ -1475,6 +1494,7 @@ dependencies = [
"lazy_static",
"proc-macro2",
"quote",
"serde_json",
"sha2",
"sqlx-core",
"sqlx-rt",

@ -19,7 +19,7 @@ tui = { version = "0.14.0", features = ["crossterm"], default-features = false }
crossterm = "0.19"
anyhow = "1.0.38"
unicode-width = "0.1"
sqlx = { version = "0.4.1", features = ["mysql", "postgres", "chrono", "runtime-tokio-rustls", "decimal"] }
sqlx = { version = "0.4.1", features = ["mysql", "postgres", "chrono", "runtime-tokio-rustls", "decimal", "json"] }
chrono = "0.4"
tokio = { version = "0.2.22", features = ["full"] }
futures = "0.3.5"

@ -79,7 +79,7 @@ impl Pool for PostgresPool {
page: u16,
filter: Option<String>,
) -> anyhow::Result<(Vec<String>, Vec<Vec<String>>)> {
let query = if let Some(filter) = filter {
let query = if let Some(filter) = filter.as_ref() {
format!(
r#"SELECT * FROM "{database}""{table_schema}"."{table}" WHERE {filter} LIMIT {page}, {limit}"#,
database = database.name,
@ -102,6 +102,7 @@ impl Pool for PostgresPool {
let mut rows = sqlx::query(query.as_str()).fetch(&self.pool);
let mut headers = vec![];
let mut records = vec![];
let mut json_records = None;
while let Some(row) = rows.try_next().await? {
headers = row
.columns()
@ -110,7 +111,29 @@ impl Pool for PostgresPool {
.collect();
let mut new_row = vec![];
for column in row.columns() {
new_row.push(convert_column_value_to_string(&row, column)?)
match convert_column_value_to_string(&row, column) {
Ok(v) => new_row.push(v),
Err(_) => {
if json_records.is_none() {
json_records = Some(
self.get_json_records(database, table, page, filter.clone())
.await?,
);
}
if let Some(json_records) = &json_records {
match json_records
.get(records.len())
.unwrap()
.get(column.name())
.unwrap()
{
serde_json::Value::String(v) => new_row.push(v.to_string()),
serde_json::Value::Null => new_row.push("NULL".to_string()),
_ => (),
}
}
}
}
}
records.push(new_row)
}
@ -153,6 +176,40 @@ impl Pool for PostgresPool {
}
}
impl PostgresPool {
async fn get_json_records(
&self,
database: &Database,
table: &Table,
page: u16,
filter: Option<String>,
) -> anyhow::Result<Vec<serde_json::Value>> {
let query = if let Some(filter) = filter {
format!(
r#"SELECT to_json({table}.*) FROM "{database}""{table_schema}"."{table}" WHERE {filter} LIMIT {page}, {limit}"#,
database = database.name,
table = table.name,
filter = filter,
table_schema = table.schema.clone().unwrap_or_else(|| "public".to_string()),
page = page,
limit = RECORDS_LIMIT_PER_PAGE
)
} else {
format!(
r#"SELECT to_json({table}.*) FROM "{database}"."{table_schema}"."{table}" limit {limit} offset {page}"#,
database = database.name,
table = table.name,
table_schema = table.schema.clone().unwrap_or_else(|| "public".to_string()),
page = page,
limit = RECORDS_LIMIT_PER_PAGE
)
};
let json: Vec<(serde_json::Value,)> =
sqlx::query_as(query.as_str()).fetch_all(&self.pool).await?;
Ok(json.iter().map(|v| v.clone().0).collect())
}
}
fn convert_column_value_to_string(row: &PgRow, column: &PgColumn) -> anyhow::Result<String> {
let column_name = column.name();
match column.type_info().clone().name() {

Loading…
Cancel
Save