fix: postgres to mcp-server tools: describeTable fail (#2687)

This commit is contained in:
lvshui
2025-07-30 21:18:56 +08:00
committed by GitHub
parent bd1101d711
commit 32b5c89c17

View File

@@ -3,6 +3,7 @@ package gorm
import ( import (
"context" "context"
"fmt" "fmt"
"strings"
"sync/atomic" "sync/atomic"
"time" "time"
@@ -160,7 +161,6 @@ func (c *DBClient) handleSQLError(err error) error {
// DescribeTable Get the structure of a specific table. // DescribeTable Get the structure of a specific table.
func (c *DBClient) DescribeTable(table string) ([]map[string]interface{}, error) { func (c *DBClient) DescribeTable(table string) ([]map[string]interface{}, error) {
var sql string var sql string
var args []string
switch c.dbType { switch c.dbType {
case MYSQL: case MYSQL:
sql = ` sql = `
@@ -175,7 +175,7 @@ func (c *DBClient) DescribeTable(table string) ([]map[string]interface{}, error)
from information_schema.columns from information_schema.columns
where table_schema = database() and table_name = ? where table_schema = database() and table_name = ?
` `
args = []string{table} return c.Query(sql, table)
case POSTGRES: case POSTGRES:
sql = ` sql = `
@@ -184,20 +184,21 @@ func (c *DBClient) DescribeTable(table string) ([]map[string]interface{}, error)
data_type as column_type, data_type as column_type,
is_nullable, is_nullable,
case case
when column_default like 'nextval%%' then 'auto_increment' when column_default like 'nextval%' then 'auto_increment'
when column_default is not null then 'default' when column_default is not null then 'default'
else '' else ''
end as column_key, end as column_key,
column_default, column_default,
case case
when column_default like 'nextval%%' then 'auto_increment' when column_default like 'nextval%' then 'auto_increment'
else '' else ''
end as extra, end as extra,
col_description((select oid from pg_class where relname = ?), ordinal_position) as column_comment col_description((select oid from pg_class where relname = ?), ordinal_position) as column_comment
from information_schema.columns from information_schema.columns
where table_name = ? where table_name = ?
` `
args = []string{table, table} lowerTable := strings.ToLower(table)
return c.Query(sql, lowerTable, lowerTable)
case CLICKHOUSE: case CLICKHOUSE:
sql = ` sql = `
@@ -212,7 +213,7 @@ func (c *DBClient) DescribeTable(table string) ([]map[string]interface{}, error)
from system.columns from system.columns
where database = currentDatabase() and table = ? where database = currentDatabase() and table = ?
` `
args = []string{table} return c.Query(sql, table)
case SQLITE: case SQLITE:
sql = ` sql = `
@@ -226,13 +227,11 @@ func (c *DBClient) DescribeTable(table string) ([]map[string]interface{}, error)
'' as column_comment '' as column_comment
from pragma_table_info(?) from pragma_table_info(?)
` `
args = []string{table} return c.Query(sql, table)
default: default:
return nil, fmt.Errorf("unsupported database type: %s", c.dbType) return nil, fmt.Errorf("unsupported database type: %s", c.dbType)
} }
return c.Query(sql, args)
} }
// ListTables List all tables in the connected database. // ListTables List all tables in the connected database.