-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix postgresql 'name', and 'oid' data types by switching to a driver
that handles them properly
- Loading branch information
1 parent
1989a58
commit 85dda55
Showing
9 changed files
with
211 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package postgresql | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"net" | ||
"net/url" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/jackc/pgx" | ||
"github.com/jackc/pgx/stdlib" | ||
) | ||
|
||
// pulled from lib/pq | ||
// ParseURL no longer needs to be used by clients of this library since supplying a URL as a | ||
// connection string to sql.Open() is now supported: | ||
// | ||
// sql.Open("postgres", "postgres://bob:[email protected]:5432/mydb?sslmode=verify-full") | ||
// | ||
// It remains exported here for backwards-compatibility. | ||
// | ||
// ParseURL converts a url to a connection string for driver.Open. | ||
// Example: | ||
// | ||
// "postgres://bob:[email protected]:5432/mydb?sslmode=verify-full" | ||
// | ||
// converts to: | ||
// | ||
// "user=bob password=secret host=1.2.3.4 port=5432 dbname=mydb sslmode=verify-full" | ||
// | ||
// A minimal example: | ||
// | ||
// "postgres://" | ||
// | ||
// This will be blank, causing driver.Open to use all of the defaults | ||
func ParseURL(uri string) (string, error) { | ||
u, err := url.Parse(uri) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if u.Scheme != "postgres" && u.Scheme != "postgresql" { | ||
return "", fmt.Errorf("invalid connection protocol: %s", u.Scheme) | ||
} | ||
|
||
var kvs []string | ||
escaper := strings.NewReplacer(` `, `\ `, `'`, `\'`, `\`, `\\`) | ||
accrue := func(k, v string) { | ||
if v != "" { | ||
kvs = append(kvs, k+"="+escaper.Replace(v)) | ||
} | ||
} | ||
|
||
if u.User != nil { | ||
v := u.User.Username() | ||
accrue("user", v) | ||
|
||
v, _ = u.User.Password() | ||
accrue("password", v) | ||
} | ||
|
||
if host, port, err := net.SplitHostPort(u.Host); err != nil { | ||
accrue("host", u.Host) | ||
} else { | ||
accrue("host", host) | ||
accrue("port", port) | ||
} | ||
|
||
if u.Path != "" { | ||
accrue("dbname", u.Path[1:]) | ||
} | ||
|
||
q := u.Query() | ||
for k := range q { | ||
accrue(k, q.Get(k)) | ||
} | ||
|
||
sort.Strings(kvs) // Makes testing easier (not a performance concern) | ||
return strings.Join(kvs, " "), nil | ||
} | ||
|
||
func Connect(address string) (*sql.DB, error) { | ||
if strings.HasPrefix(address, "postgres://") || strings.HasPrefix(address, "postgresql://") { | ||
return sql.Open("pgx", address) | ||
} | ||
|
||
config, err := pgx.ParseDSN(address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pool, err := pgx.NewConnPool(pgx.ConnPoolConfig{ConnConfig: config}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return stdlib.OpenFromConnPool(pool) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.