fix(questdb): handle materialized views and unify table_type naming
* fix(questdb): handle materialized views * fix(test): fix rust test problems
This commit is contained in:
11 files changed
+112
-56
No files matched your search
@@ -939,7 +939,7 @@
|
||||
"fieldLineage": false,
|
||||
"sqlExplain": true,
|
||||
"userAdmin": false,
|
||||
"driverManagement": true
|
||||
"driverManagement": false
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
@@ -946,7 +946,7 @@ pub async fn list_tables_filtered(
|
||||
fn postgres_tables_sql() -> &'static str {
|
||||
"SELECT c.relname AS table_name, \
|
||||
CASE c.relkind WHEN 'r' THEN 'BASE TABLE' WHEN 'v' THEN 'VIEW' \
|
||||
WHEN 'm' THEN 'MATERIALIZED VIEW' WHEN 'f' THEN 'FOREIGN TABLE' \
|
||||
WHEN 'm' THEN 'MATERIALIZED_VIEW' WHEN 'f' THEN 'FOREIGN TABLE' \
|
||||
WHEN 'p' THEN 'BASE TABLE' END AS table_type, \
|
||||
obj_description(c.oid) AS table_comment, \
|
||||
CASE WHEN pc.relkind = 'p' THEN pn.nspname ELSE NULL END AS parent_schema, \
|
||||
@@ -984,7 +984,7 @@ fn list_objects_sql(include_timestamps: bool) -> &'static str {
|
||||
return "SELECT c.relname AS object_name, \
|
||||
CASE c.relkind \
|
||||
WHEN 'v' THEN 'VIEW' \
|
||||
WHEN 'm' THEN 'VIEW' \
|
||||
WHEN 'm' THEN 'MATERIALIZED_VIEW' \
|
||||
WHEN 'S' THEN 'SEQUENCE' \
|
||||
ELSE 'TABLE' \
|
||||
END AS object_type, \
|
||||
@@ -1026,7 +1026,7 @@ fn list_objects_sql(include_timestamps: bool) -> &'static str {
|
||||
"SELECT c.relname AS object_name, \
|
||||
CASE c.relkind \
|
||||
WHEN 'v' THEN 'VIEW' \
|
||||
WHEN 'm' THEN 'VIEW' \
|
||||
WHEN 'm' THEN 'MATERIALIZED_VIEW' \
|
||||
WHEN 'S' THEN 'SEQUENCE' \
|
||||
ELSE 'TABLE' \
|
||||
END AS object_type, \
|
||||
@@ -1678,7 +1678,7 @@ pub async fn list_owners(pool: &Pool, schema: &str) -> Result<Vec<OwnerInfo>, St
|
||||
let object_type = match relkind.as_str() {
|
||||
"r" => "TABLE",
|
||||
"v" => "VIEW",
|
||||
"m" => "MATERIALIZED VIEW",
|
||||
"m" => "MATERIALIZED_VIEW",
|
||||
"S" => "SEQUENCE",
|
||||
"f" => "FOREIGN TABLE",
|
||||
"p" => "PARTITIONED TABLE",
|
||||
@@ -2098,7 +2098,7 @@ mod tests {
|
||||
assert!(sql.contains("$1"));
|
||||
assert!(sql.contains("BASE TABLE"));
|
||||
assert!(sql.contains("VIEW"));
|
||||
assert!(sql.contains("MATERIALIZED VIEW"));
|
||||
assert!(sql.contains("MATERIALIZED_VIEW"));
|
||||
assert!(sql.contains("FOREIGN TABLE"));
|
||||
}
|
||||
|
||||
|
||||
@@ -21,23 +21,34 @@ pub async fn list_objects(pool: &Pool, schema: &str) -> Result<Vec<ObjectInfo>,
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// try query `table`, `view` and `materialized view` using statement supported by the newer version.
|
||||
/// if there is an error, rollback to the previous version of the statement
|
||||
pub async fn list_tables(pool: &Pool, _schema: &str) -> Result<Vec<TableInfo>, String> {
|
||||
match list_tables_new_version(pool, _schema).await {
|
||||
Ok(ddl) => Ok(ddl),
|
||||
Err(_) => list_tables_older_version(pool, _schema).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn list_tables_new_version(pool: &Pool, _schema: &str) -> Result<Vec<TableInfo>, String> {
|
||||
let client = pool.get().await.map_err(|e| e.to_string())?;
|
||||
|
||||
let stmt = client.prepare_cached(questdb_tables_sql()).await.map_err(|e| e.to_string())?;
|
||||
let stmt = client.prepare_cached(questdb_tables_sql_new_version()).await.map_err(|e| e.to_string())?;
|
||||
let rows = client.query(&stmt, &[]).await.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(rows
|
||||
.iter()
|
||||
.map(|row| {
|
||||
let table_type_col = row.get::<_, String>(1);
|
||||
let table_type = if table_type_col.eq_ignore_ascii_case("T") { "TABLE" } else { "VIEW" };
|
||||
let comment =
|
||||
if table_type_col.eq_ignore_ascii_case("M") { Some("Materialized".to_string()) } else { None };
|
||||
let table_type = match table_type_col.as_ref() {
|
||||
"V" => "VIEW",
|
||||
"M" => "MATERIALIZED_VIEW",
|
||||
_ => "TABLE",
|
||||
};
|
||||
TableInfo {
|
||||
name: row.get::<_, String>(0),
|
||||
table_type: table_type.to_string(),
|
||||
comment,
|
||||
comment: None,
|
||||
parent_schema: None,
|
||||
parent_name: None,
|
||||
}
|
||||
@@ -45,10 +56,36 @@ pub async fn list_tables(pool: &Pool, _schema: &str) -> Result<Vec<TableInfo>, S
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn questdb_tables_sql() -> &'static str {
|
||||
fn questdb_tables_sql_new_version() -> &'static str {
|
||||
"SELECT table_name, table_type FROM tables"
|
||||
}
|
||||
|
||||
async fn list_tables_older_version(pool: &Pool, _schema: &str) -> Result<Vec<TableInfo>, String> {
|
||||
let client = pool.get().await.map_err(|e| e.to_string())?;
|
||||
|
||||
let stmt = client.prepare_cached(questdb_tables_sql_older_version()).await.map_err(|e| e.to_string())?;
|
||||
let rows = client.query(&stmt, &[]).await.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(rows
|
||||
.iter()
|
||||
.map(|row| {
|
||||
let mat_view_col = row.get::<_, bool>(1);
|
||||
let table_type = if mat_view_col { "MATERIALIZED_VIEW" } else { "TABLE" };
|
||||
TableInfo {
|
||||
name: row.get::<_, String>(0),
|
||||
table_type: table_type.to_string(),
|
||||
comment: None,
|
||||
parent_schema: None,
|
||||
parent_name: None,
|
||||
}
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn questdb_tables_sql_older_version() -> &'static str {
|
||||
"SELECT table_name, matView FROM tables"
|
||||
}
|
||||
|
||||
pub async fn get_columns(pool: &Pool, _schema: &str, table: &str) -> Result<Vec<ColumnInfo>, String> {
|
||||
let client = pool.get().await.map_err(|e| e.to_string())?;
|
||||
let sql = format!("SHOW COLUMNS FROM {}", quote_table_identifier(Some(DatabaseType::Questdb), table));
|
||||
@@ -109,9 +146,9 @@ pub async fn questdb_object_source(pool: &Pool, name: &str) -> Result<String, St
|
||||
}
|
||||
|
||||
pub async fn questdb_table_or_view_ddl(pool: &Pool, table_or_view: &str) -> Result<String, String> {
|
||||
match questdb_table_ddl(pool, table_or_view).await {
|
||||
match questdb_view_ddl(pool, table_or_view).await {
|
||||
Ok(ddl) => Ok(ddl),
|
||||
Err(_) => questdb_view_ddl(pool, table_or_view).await,
|
||||
Err(_) => questdb_table_ddl(pool, table_or_view).await,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -466,7 +466,7 @@ fn object_type_keyword(object_type: DatabaseObjectType) -> &'static str {
|
||||
match object_type {
|
||||
DatabaseObjectType::Table => "TABLE",
|
||||
DatabaseObjectType::View => "VIEW",
|
||||
DatabaseObjectType::MaterializedView => "MATERIALIZED VIEW",
|
||||
DatabaseObjectType::MaterializedView => "MATERIALIZED_VIEW",
|
||||
DatabaseObjectType::Procedure => "PROCEDURE",
|
||||
DatabaseObjectType::Function => "FUNCTION",
|
||||
}
|
||||
|
||||
@@ -236,7 +236,7 @@ mod tests {
|
||||
},
|
||||
db::ObjectInfo {
|
||||
name: "active_orders".to_string(),
|
||||
object_type: "MATERIALIZED VIEW".to_string(),
|
||||
object_type: "MATERIALIZED_VIEW".to_string(),
|
||||
schema: None,
|
||||
comment: None,
|
||||
created_at: None,
|
||||
|
||||
Reference in new issue
Block a user