MCPcopy Create free account
hub / github.com/google/go-cloud / OpenPostgresURL

Method OpenPostgresURL

postgres/gcppostgres/gcppostgres.go:100–134  ·  view source on GitHub ↗

OpenPostgresURL opens a new GCP database connection wrapped with OpenTelemetry instrumentation.

(ctx context.Context, u *url.URL)

Source from the content-addressed store, hash-verified

98
99// OpenPostgresURL opens a new GCP database connection wrapped with OpenTelemetry instrumentation.
100func (uo *URLOpener) OpenPostgresURL(ctx context.Context, u *url.URL) (*sql.DB, error) {
101 if uo.CertSource == nil {
102 return nil, fmt.Errorf("gcppostgres: URLOpener CertSource is nil")
103 }
104 instance, dbName, err := instanceFromURL(u)
105 if err != nil {
106 return nil, fmt.Errorf("gcppostgres: open %v: %v", u, err)
107 }
108
109 query := u.Query()
110 for k := range query {
111 // Only permit parameters that do not conflict with other behavior.
112 if k == "sslmode" || k == "sslcert" || k == "sslkey" || k == "sslrootcert" {
113 return nil, fmt.Errorf("gcppostgres: open: extra parameter %s not allowed", k)
114 }
115 }
116 query.Set("sslmode", "disable")
117
118 u2 := new(url.URL)
119 *u2 = *u
120 u2.Scheme = "postgres"
121 u2.Host = "cloudsql"
122 u2.Path = "/" + dbName
123 u2.RawQuery = query.Encode()
124 db := sql.OpenDB(connector{
125 client: &proxy.Client{
126 Port: 3307,
127 Certs: uo.CertSource,
128 },
129 instance: instance,
130 pqConn: u2.String(),
131 traceOpts: append([]otelsql.Option(nil), uo.TraceOpts...),
132 })
133 return db, nil
134}
135
136func instanceFromURL(u *url.URL) (instance, db string, _ error) {
137 path := u.Host + u.Path // everything after scheme but before query or fragment

Callers

nothing calls this directly

Calls 5

QueryMethod · 0.80
instanceFromURLFunction · 0.70
StringMethod · 0.65
SetMethod · 0.45
EncodeMethod · 0.45

Tested by

no test coverage detected