-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(postgres/pgxv4): add support for postgres driver (#61)
Related to #3.
- Loading branch information
Showing
4 changed files
with
219 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package pgxv4 provides a Cloud SQL Postgres driver that uses pgx v4. | ||
package pgxv4 | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"database/sql/driver" | ||
"net" | ||
|
||
"cloud.google.com/go/cloudsqlconn" | ||
"github.com/jackc/pgx/v4" | ||
"github.com/jackc/pgx/v4/stdlib" | ||
) | ||
|
||
// RegisterDriver registers a Postgres driver that uses the cloudsqlconn.Dialer | ||
// configured with the provided options. The choice of name is entirely up to | ||
// the caller and may be used to distinguish between multiple registrations of | ||
// differently configured Dialers. | ||
// Note: The underlying driver uses the latest version of pgx. | ||
func RegisterDriver(name string, opts ...cloudsqlconn.Option) { | ||
sql.Register(name, &pgDriver{ | ||
opts: opts, | ||
}) | ||
} | ||
|
||
type dialerConn struct { | ||
driver.Conn | ||
dialer *cloudsqlconn.Dialer | ||
} | ||
|
||
func (c *dialerConn) Close() error { | ||
c.dialer.Close() | ||
return c.Conn.Close() | ||
} | ||
|
||
type pgDriver struct { | ||
opts []cloudsqlconn.Option | ||
} | ||
|
||
// Open accepts a keyword/value formatted connection string and returns a | ||
// connection to the database using cloudsqlconn.Dialer. The Cloud SQL instance | ||
// connection name should be specified in the host field. For example: | ||
// | ||
// "host=my-project:us-central1:my-db-instance user=myuser password=mypass" | ||
func (p *pgDriver) Open(name string) (driver.Conn, error) { | ||
config, err := pgx.ParseConfig(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
instConnName := config.Config.Host // Extract instance connection name | ||
config.Config.Host = "localhost" // Replace it with a default value | ||
d, err := cloudsqlconn.NewDialer(context.Background(), p.opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
config.DialFunc = func(ctx context.Context, _, _ string) (net.Conn, error) { | ||
return d.Dial(ctx, instConnName) | ||
} | ||
dbURI := stdlib.RegisterConnConfig(config) | ||
conn, err := stdlib.GetDefaultDriver().Open(dbURI) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &dialerConn{Conn: conn, dialer: d}, nil | ||
} |
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,44 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package pgxv4_test | ||
|
||
import ( | ||
"database/sql" | ||
"log" | ||
"time" | ||
|
||
"cloud.google.com/go/cloudsqlconn" | ||
"cloud.google.com/go/cloudsqlconn/postgres/pgxv4" | ||
) | ||
|
||
// Example shows how to use cloudsqlpostgres dialer | ||
func ExamplePostgresConnection() { | ||
// Note that sslmode=disable is required it does not mean that the connection | ||
// is unencrypted. All connections via the proxy are completely encrypted. | ||
pgxv4.RegisterDriver("cloudsql-postgres", cloudsqlconn.WithIAMAuthN()) | ||
db, err := sql.Open( | ||
"cloudsql-postgres", | ||
"host=project:region:instance user=postgres dbname=postgres password=password sslmode=disable", | ||
) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer db.Close() | ||
var now time.Time | ||
if err := db.QueryRow("SELECT NOW()").Scan(&now); err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Println(now) | ||
} |