feat(postgres): support schema comments
This commit is contained in:
1 parent
ded3fdde32
commit
23edbe391f
20 files changed
+315
-22
No files matched your search
@@ -25,7 +25,7 @@ use crate::types::{
|
||||
ColumnInfo, CompletionAssistantCandidate, CompletionAssistantCandidateKind, CompletionAssistantMatchMode,
|
||||
CompletionAssistantObjectKind, CompletionAssistantRequest, CompletionAssistantResponse, DatabaseInfo,
|
||||
ForeignKeyInfo, FunctionInfo, IndexInfo, ObjectInfo, ObjectStatistics, OwnerInfo, QueryResult, RuleInfo,
|
||||
SequenceInfo, TableInfo, TriggerInfo,
|
||||
SchemaInfo, SequenceInfo, TableInfo, TriggerInfo,
|
||||
};
|
||||
|
||||
fn pg_temporal_to_json_value(row: &Row, idx: usize) -> Option<serde_json::Value> {
|
||||
@@ -1479,10 +1479,19 @@ pub async fn list_object_statistics(pool: &Pool, schema: &str) -> Result<Vec<Obj
|
||||
}
|
||||
|
||||
pub async fn list_schemas(pool: &Pool) -> Result<Vec<String>, String> {
|
||||
Ok(list_schema_infos(pool).await?.into_iter().map(|schema| schema.name).collect())
|
||||
}
|
||||
|
||||
pub async fn list_schema_infos(pool: &Pool) -> Result<Vec<SchemaInfo>, String> {
|
||||
let client = pool.get().await.map_err(|e| e.to_string())?;
|
||||
let stmt = client
|
||||
.prepare_cached(
|
||||
"SELECT n.nspname AS schema_name FROM pg_catalog.pg_namespace n \
|
||||
"SELECT n.nspname AS schema_name, d.description AS schema_comment \
|
||||
FROM pg_catalog.pg_namespace n \
|
||||
LEFT JOIN pg_catalog.pg_description d \
|
||||
ON d.objoid = n.oid \
|
||||
AND d.objsubid = 0 \
|
||||
AND d.classoid = 'pg_namespace'::regclass \
|
||||
WHERE n.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast') \
|
||||
AND n.nspname NOT LIKE 'pg_toast_temp_%' \
|
||||
AND n.nspname NOT LIKE 'pg_temp_%' \
|
||||
@@ -1492,7 +1501,13 @@ pub async fn list_schemas(pool: &Pool) -> Result<Vec<String>, String> {
|
||||
.map_err(|e| e.to_string())?;
|
||||
let rows = client.query(&stmt, &[]).await.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(rows.iter().map(|row| row.get::<_, String>(0)).collect())
|
||||
Ok(rows
|
||||
.iter()
|
||||
.map(|row| SchemaInfo {
|
||||
name: row.get::<_, String>(0),
|
||||
comment: row.try_get::<_, Option<String>>(1).ok().flatten(),
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
const POSTGRES_COLUMNS_SQL: &str = "SELECT a.attname AS column_name, \
|
||||
|
||||
@@ -590,6 +590,34 @@ pub async fn list_schemas_core(state: &AppState, connection_id: &str, database:
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn list_schema_infos_core(
|
||||
state: &AppState,
|
||||
connection_id: &str,
|
||||
database: &str,
|
||||
) -> Result<Vec<db::SchemaInfo>, String> {
|
||||
retry_metadata_connection(state, connection_id, Some(database), || {
|
||||
list_schema_infos_once(state, connection_id, database)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
async fn list_schema_infos_once(
|
||||
state: &AppState,
|
||||
connection_id: &str,
|
||||
database: &str,
|
||||
) -> Result<Vec<db::SchemaInfo>, String> {
|
||||
let pool_key = state.get_or_create_pool(connection_id, Some(database)).await?;
|
||||
{
|
||||
let connections = state.connections.read().await;
|
||||
if let Some(PoolKind::Postgres(pool)) = connections.get(&pool_key) {
|
||||
return db::postgres::list_schema_infos(pool).await;
|
||||
}
|
||||
}
|
||||
|
||||
let schemas = list_schemas_once(state, connection_id, database).await?;
|
||||
Ok(schemas.into_iter().map(|name| db::SchemaInfo { name, comment: None }).collect())
|
||||
}
|
||||
|
||||
async fn list_schemas_once(state: &AppState, connection_id: &str, database: &str) -> Result<Vec<String>, String> {
|
||||
let pool_key = state.get_or_create_pool(connection_id, Some(database)).await?;
|
||||
let db_config = connection_config(state, connection_id).await;
|
||||
|
||||
@@ -5,6 +5,12 @@ pub struct DatabaseInfo {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SchemaInfo {
|
||||
pub name: String,
|
||||
pub comment: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct LinkedServerInfo {
|
||||
pub name: String,
|
||||
|
||||
Reference in new issue
Block a user