Skip to content

Commit

Permalink
Merge pull request #211 from yflau/main
Browse files Browse the repository at this point in the history
add postgres dsn
  • Loading branch information
trheyi authored Jul 12, 2024
2 parents 8f1a475 + 4a00ad2 commit d661a65
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions connector/database/xun.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ func (x *Xun) getDSN(i int) (string, error) {
return x.mysqlDSN(i)
case "sqlite3":
return x.sqlite3DSN(i)
case "postgres":
return x.postgresDSN(i)
}

return "", fmt.Errorf("the driver %s does not support", x.Driver)
Expand Down Expand Up @@ -261,6 +263,38 @@ func (x *Xun) mysqlDSN(i int) (string, error) {
return dsn, nil
}

func (x *Xun) postgresDSN(i int) (string, error) {

if x.Options.DB == "" {
return "", fmt.Errorf("options.db is required")
}

if len(x.Options.Hosts) == 0 {
return "", fmt.Errorf("options.hosts is required")
}

host := x.Options.Hosts[i]
if host.Host == "" {
return "", fmt.Errorf("hosts.%d.host is required", i)
}

if host.Port == "" {
host.Port = "5432"
}

if host.User == "" {
return "", fmt.Errorf("hosts.%d.user is required", i)
}

if host.Pass == "" {
return "", fmt.Errorf("hosts.%d.pass is required", i)
}

dsn := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", host.User, host.Pass, host.Host, host.Port, x.Options.DB)

return dsn, nil
}

// Setting get the connection setting
func (x *Xun) Setting() map[string]interface{} {
return map[string]interface{}{
Expand Down

0 comments on commit d661a65

Please sign in to comment.