Skip to content

Commit

Permalink
fix: fails to initialize postgres session
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhengYa-0110 committed Jan 14, 2025
1 parent 5df11c8 commit 94224f1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
29 changes: 14 additions & 15 deletions server/controller/db/metadb/common/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func GetConnector(cfg config.Config, useDatabase bool, timeout uint32, multiStat
case config.MetaDBTypeMySQL:
return getMySQLConnector(cfg, useDatabase, timeout, multiStatements)
case config.MetaDBTypePostgreSQL:
return getPostgreSQLConnector(cfg, useDatabase, timeout, multiStatements)
return getPostgreSQLConnector(cfg, useDatabase, timeout)
default:
return nil, fmt.Errorf("unsupported database type: %s", cfg.Type)
}
Expand Down Expand Up @@ -87,24 +87,23 @@ func getMySQLConnector(cfg config.Config, useDatabase bool, timeout uint32, mult
return connector, nil
}

func getPostgreSQLConnector(cfg config.Config, useDatabase bool, timeout uint32, multiStatements bool) (driver.Connector, error) {
var database string
var schema string
if useDatabase {
database = cfg.Database
schema = cfg.Schema
}

func getPostgreSQLConnector(cfg config.Config, useDatabase bool, timeout uint32) (driver.Connector, error) {
connStr := "user=" + cfg.UserName +
" password=" + cfg.UserPassword +
" host=" + cfg.Host +
" port=" + fmt.Sprintf("%d", cfg.Port) +
" dbname=" + database +
" search_path=" + schema +
" connect_timeout=" + fmt.Sprintf("%d", int(timeout)) +
" sslmode=disable" +
" client_encoding=UTF8"

var database string
if useDatabase {
database = cfg.Database
connStr += " dbname=" + database +
" search_path=" + cfg.Schema
}

log.Infof("postgres connection string: %s", connStr)
connector, err := postgres_driver.NewConnector(connStr)
if err != nil {
log.Error("get database(%s) connector failed with error: %v", database, err.Error())
Expand Down Expand Up @@ -149,16 +148,16 @@ func InitSession(cfg config.Config, connector driver.Connector) (*gorm.DB, error
func getDialector(cfg config.Config, connector driver.Connector) gorm.Dialector {
switch cfg.Type {
case config.MetaDBTypeMySQL:
return getMySQLDialector(cfg, connector)
return getMySQLDialector(connector)
case config.MetaDBTypePostgreSQL:
return getPostgresDialector(cfg, connector)
return getPostgresDialector(connector)
default:
log.Errorf("unsupported database type: %s", cfg.Type)
return nil
}
}

func getMySQLDialector(cfg config.Config, conn driver.Connector) gorm.Dialector {
func getMySQLDialector(conn driver.Connector) gorm.Dialector {
return mysql.New(mysql.Config{
Conn: sql.OpenDB(conn),
DefaultStringSize: 256, // string 类型字段的默认长度
Expand All @@ -169,7 +168,7 @@ func getMySQLDialector(cfg config.Config, conn driver.Connector) gorm.Dialector
})
}

func getPostgresDialector(cfg config.Config, conn driver.Connector) gorm.Dialector {
func getPostgresDialector(conn driver.Connector) gorm.Dialector {
return postgres.New(postgres.Config{
Conn: sql.OpenDB(conn),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (f *SqlFmt) SelectDatabase() string {
}

func (f *SqlFmt) SelectTable(tableName string) string {
return fmt.Sprintf("SELECT tablename FROM pg_catalog.pg_tables WHERE datname='%s' AND schemaname='%s' AND tablename='%s'", f.cfg.Database, f.cfg.Schema, tableName)
return fmt.Sprintf("SELECT table_name FROM information_schema.tables WHERE table_catalog='%s' AND table_schema='%s' AND table_name='%s'", f.cfg.Database, f.cfg.Schema, tableName)
}

func (f *SqlFmt) SelectColumn(tableName, columnName string) string {
Expand Down

0 comments on commit 94224f1

Please sign in to comment.