Open opens a Postgres driver.
(_ context.Context, _ storepb.Engine, config db.ConnectionConfig)
| 61 | |
| 62 | // Open opens a Postgres driver. |
| 63 | func (d *Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionConfig) (db.Driver, error) { |
| 64 | pgxConnConfig, err := getCockroachConnectionConfig(config) |
| 65 | if err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | |
| 69 | if config.DataSource.GetSshHost() != "" { |
| 70 | sshClient, err := util.GetSSHClient(config.DataSource) |
| 71 | if err != nil { |
| 72 | return nil, err |
| 73 | } |
| 74 | d.sshClient = sshClient |
| 75 | |
| 76 | pgxConnConfig.DialFunc = func(_ context.Context, network, addr string) (net.Conn, error) { |
| 77 | conn, err := sshClient.Dial(network, addr) |
| 78 | if err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | return &util.NoDeadlineConn{Conn: conn}, nil |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | d.databaseName = config.ConnectionContext.DatabaseName |
| 86 | if config.ConnectionContext.DatabaseName != "" { |
| 87 | pgxConnConfig.Database = config.ConnectionContext.DatabaseName |
| 88 | } else if config.DataSource.GetDatabase() != "" { |
| 89 | pgxConnConfig.Database = config.DataSource.GetDatabase() |
| 90 | } else { |
| 91 | pgxConnConfig.Database = "postgres" |
| 92 | } |
| 93 | d.config = config |
| 94 | |
| 95 | d.connectionString = stdlib.RegisterConnConfig(pgxConnConfig) |
| 96 | db, err := sql.Open(driverName, d.connectionString) |
| 97 | if err != nil { |
| 98 | return nil, err |
| 99 | } |
| 100 | d.db = db |
| 101 | d.connectionCtx = config.ConnectionContext |
| 102 | return d, nil |
| 103 | } |
| 104 | |
| 105 | // getRoutingIDFromCockroachCloudURL returns the routing ID from the Cockroach Cloud URL, returns empty string if not found. |
| 106 | func getRoutingIDFromCockroachCloudURL(host string) string { |
nothing calls this directly
no test coverage detected