25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(oracle): add GB18030 charset fallback
t8y2bc3748334383
diff --git a/agents/drivers/oracle-go/go.mod b/agents/drivers/oracle-go/go.modindex 569ba62d6a..9d4188c2cf 100644--- a/agents/drivers/oracle-go/go.mod+++ b/agents/drivers/oracle-go/go.mod@@ -3,3 +3,5 @@ module github.com/t8y2/dbx/agents/drivers/oracle-gogo 1.22require github.com/sijms/go-ora/v2 v2.9.0++require golang.org/x/text v0.22.0diff --git a/agents/drivers/oracle-go/go.sum b/agents/drivers/oracle-go/go.sumindex c8b6880459..a0fd27bc80 100644--- a/agents/drivers/oracle-go/go.sum+++ b/agents/drivers/oracle-go/go.sum@@ -1,2 +1,4 @@github.com/sijms/go-ora/v2 v2.9.0 h1:+iQbUeTeCOFMb5BsOMgUhV8KWyrv9yjKpcK4x7+MFrg=github.com/sijms/go-ora/v2 v2.9.0/go.mod h1:QgFInVi3ZWyqAiJwzBQA+nbKYKH77tdp1PYoCqhR2dU=+golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=+golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=diff --git a/agents/drivers/oracle-go/main.go b/agents/drivers/oracle-go/main.goindex 0c1d24d817..ea5d134533 100644--- a/agents/drivers/oracle-go/main.go+++ b/agents/drivers/oracle-go/main.go@@ -18,15 +18,23 @@ import (_ "github.com/sijms/go-ora/v2"go_ora "github.com/sijms/go-ora/v2"+ "github.com/sijms/go-ora/v2/converters"+ "golang.org/x/text/encoding/simplifiedchinese"+ "golang.org/x/text/transform")const protocolVersion = 1const defaultMaxRows = 1000+const oracleCharsetZHS32GB18030 = 854var (- oraclePlSQLBlockStartRegexp = regexp.MustCompile(`(?is)^\s*(?:DECLARE|BEGIN|CREATE\s+(?:OR\s+REPLACE\s+)?(?:(?:EDITIONABLE|NONEDITIONABLE)\s+)?(?:FUNCTION|PROCEDURE|TRIGGER|PACKAGE(?:\s+BODY)?|TYPE(?:\s+BODY)?))\b`)- oraclePlSQLBlockEndRegexp = regexp.MustCompile(`(?is)\bEND\s*;\s*$`)- oracleNamedPlSQLBlockEndRegexp = regexp.MustCompile(`(?is)\bEND\s+([A-Z0-9_$#]+)\s*;\s*$`)+ oraclePlSQLBlockStartRegexp = regexp.MustCompile(`(?is)^\s*(?:DECLARE|BEGIN|CREATE\s+(?:OR\s+REPLACE\s+)?(?:(?:EDITIONABLE|NONEDITIONABLE)\s+)?(?:FUNCTION|PROCEDURE|TRIGGER|PACKAGE(?:\s+BODY)?|TYPE(?:\s+BODY)?))\b`)+ oraclePlSQLBlockEndRegexp = regexp.MustCompile(`(?is)\bEND\s*;\s*$`)+ oracleNamedPlSQLBlockEndRegexp = regexp.MustCompile(`(?is)\bEND\s+([A-Z0-9_$#]+)\s*;\s*$`)+ oracleUnsupportedServerCharsetRegexp = regexp.MustCompile(`server use charset with id: ([0-9]+).*not supported by the driver`)+ oracleStringConverters = map[int]converters.IStringConverter{+ oracleCharsetZHS32GB18030: oracleGB18030Converter{},+ })const oracleListDatabasesSQL = `@@ -335,16 +343,11 @@ func (s *server) dispatch(method string, params map[string]json.RawMessage) (anyif err := decodeParams(params, &cp); err != nil {return nil, false, err}- db, err := openDB(cp)+ db, err := openAndPingDB(cp, 5*time.Second)if err != nil {return nil, false, err}defer db.Close()- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)- defer cancel()- if err := db.PingContext(ctx); err != nil {- return nil, false, err- }return map[string]bool{"ok": true}, false, nilcase "list_databases":result, err := s.listDatabases()@@ -442,16 +445,12 @@ func (s *server) dispatch(method string, params map[string]json.RawMessage) (anyfunc (s *server) connect(params connectParams) error {_ = s.disconnect()- db, err := openDB(params)+ db, err := openAndPingDB(params, 15*time.Second)if err != nil {return err}ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)defer cancel()- if err := db.PingContext(ctx); err != nil {- db.Close()- return err- }if _, err := db.ExecContext(ctx, "ALTER SESSION SET NLS_LANGUAGE='AMERICAN'"); err != nil {db.Close()return err@@ -472,10 +471,22 @@ func (s *server) disconnect() error {}func openDB(params connectParams) (*sql.DB, error) {+ return openDBWithStringConverter(params, nil)+}++func openDBWithStringConverter(params connectParams, stringConverter converters.IStringConverter) (*sql.DB, error) {dsn := buildDSN(params)- db, err := sql.Open("oracle", dsn)- if err != nil {- return nil, err+ var db *sql.DB+ if stringConverter == nil {+ var err error+ db, err = sql.Open("oracle", dsn)+ if err != nil {+ return nil, err+ }+ } else {+ connector := go_ora.NewConnector(dsn)+ go_ora.SetStringConverter(connector, stringConverter, nil)+ db = sql.OpenDB(connector)}db.SetMaxOpenConns(4)db.SetMaxIdleConns(1)@@ -483,6 +494,87 @@ func openDB(params connectParams) (*sql.DB, error) {return db, nil}+func openAndPingDB(params connectParams, timeout time.Duration) (*sql.DB, error) {+ db, err := openDB(params)+ if err != nil {+ return nil, err+ }+ if err := pingDB(db, timeout); err != nil {+ db.Close()+ stringConverter, ok := oracleStringConverterForUnsupportedCharsetError(err)+ if !ok {+ return nil, err+ }+ // Retry only charsets with explicit converters. Guessing a converter+ // can make the connection succeed while silently corrupting text.+ db, err = openDBWithStringConverter(params, stringConverter)+ if err != nil {+ return nil, err+ }+ if retryErr := pingDB(db, timeout); retryErr != nil {+ db.Close()+ return nil, retryErr+ }+ }+ return db, nil+}++func pingDB(db *sql.DB, timeout time.Duration) error {+ ctx, cancel := context.WithTimeout(context.Background(), timeout)+ defer cancel()+ return db.PingContext(ctx)+}++func oracleStringConverterForUnsupportedCharsetError(err error) (converters.IStringConverter, bool) {+ charsetID, ok := unsupportedOracleServerCharsetID(err)+ if !ok {+ return nil, false+ }+ stringConverter, ok := oracleStringConverters[charsetID]+ return stringConverter, ok+}++func unsupportedOracleServerCharsetID(err error) (int, bool) {+ if err == nil {+ return 0, false+ }+ match := oracleUnsupportedServerCharsetRegexp.FindStringSubmatch(err.Error())+ if len(match) != 2 {+ return 0, false+ }+ charsetID, parseErr := strconv.Atoi(match[1])+ if parseErr != nil {+ return 0, false+ }+ return charsetID, true+}++type oracleGB18030Converter struct{}++func (oracleGB18030Converter) Encode(input string) []byte {+ output, _, err := transform.String(simplifiedchinese.GB18030.NewEncoder(), input)+ if err != nil {+ return []byte(input)+ }+ return []byte(output)+}++func (oracleGB18030Converter) Decode(input []byte) string {+ output, _, err := transform.Bytes(simplifiedchinese.GB18030.NewDecoder(), input)+ if err != nil {+ return string(input)+ }+ return string(output)+}++func (oracleGB18030Converter) GetLangID() int {+ return oracleCharsetZHS32GB18030+}++func (oracleGB18030Converter) Clone() converters.IStringConverter {+ return oracleGB18030Converter{}+}+func buildDSN(params connectParams) string {connectionString := strings.TrimSpace(params.ConnectionString)if strings.HasPrefix(strings.ToLower(connectionString), "oracle://") {diff --git a/agents/drivers/oracle-go/main_test.go b/agents/drivers/oracle-go/main_test.goindex b4ceb9a2af..d9364bc819 100644--- a/agents/drivers/oracle-go/main_test.go+++ b/agents/drivers/oracle-go/main_test.go@@ -364,6 +364,47 @@ func TestBuildDSNAddsSysDbaOption(t *testing.T) {}}+func TestOracleGB18030ConverterRoundTrip(t *testing.T) {+ converter := oracleGB18030Converter{}+ input := "DBX \u4e2d\u6587 \U00020000"++ encoded := converter.Encode(input)+ if string(encoded) == input {+ t.Fatalf("GB18030 converter should encode non-ASCII text away from UTF-8 bytes")+ }+ if decoded := converter.Decode(encoded); decoded != input {+ t.Fatalf("GB18030 round trip = %q, want %q", decoded, input)+ }+ if converter.GetLangID() != oracleCharsetZHS32GB18030 {+ t.Fatalf("GB18030 converter lang id = %d, want %d", converter.GetLangID(), oracleCharsetZHS32GB18030)+ }+ if clone := converter.Clone(); clone.GetLangID() != oracleCharsetZHS32GB18030 {+ t.Fatalf("GB18030 converter clone lang id = %d, want %d", clone.GetLangID(), oracleCharsetZHS32GB18030)+ }+}++func TestOracleStringConverterForUnsupportedCharsetError(t *testing.T) {+ err := errors.New("the server use charset with id: 854 which is not supported by the driver")+ converter, ok := oracleStringConverterForUnsupportedCharsetError(err)+ if !ok {+ t.Fatalf("expected GB18030 server charset error to have a converter")+ }+ if converter.GetLangID() != oracleCharsetZHS32GB18030 {+ t.Fatalf("converter lang id = %d, want %d", converter.GetLangID(), oracleCharsetZHS32GB18030)+ }+ ncharsetErr := errors.New("the server use ncharset with id: 854 which is not supported by the driver")+ if _, ok := oracleStringConverterForUnsupportedCharsetError(ncharsetErr); ok {+ t.Fatalf("ncharset errors should not have a server charset converter")+ }+ otherCharsetErr := errors.New("the server use charset with id: 852 which is not supported by the driver")+ if charsetID, ok := unsupportedOracleServerCharsetID(otherCharsetErr); !ok || charsetID != 852 {+ t.Fatalf("other server charset should still be parsed, got id=%d ok=%v", charsetID, ok)+ }+ if _, ok := oracleStringConverterForUnsupportedCharsetError(otherCharsetErr); ok {+ t.Fatalf("unknown charset ids should not get a guessed converter")+ }+}+func TestListDatabasesSQLUsesUserDictionaryInsteadOfObjectDictionary(t *testing.T) {sqlText := strings.ToUpper(oracleListDatabasesSQL)