Open opens a Spanner driver. It must connect to a specific database. If database isn't provided, part of the driver cannot function.
(ctx context.Context, _ storepb.Engine, config db.ConnectionConfig)
| 63 | // Open opens a Spanner driver. It must connect to a specific database. |
| 64 | // If database isn't provided, part of the driver cannot function. |
| 65 | func (d *Driver) Open(ctx context.Context, _ storepb.Engine, config db.ConnectionConfig) (db.Driver, error) { |
| 66 | if config.DataSource.Host == "" { |
| 67 | return nil, errors.New("host cannot be empty") |
| 68 | } |
| 69 | d.config = config |
| 70 | d.connCtx = config.ConnectionContext |
| 71 | |
| 72 | var o []option.ClientOption |
| 73 | if gcpCredential := config.DataSource.GetGcpCredential(); gcpCredential != nil { |
| 74 | credOption, err := util.GCPCredentialOption([]byte(gcpCredential.Content)) |
| 75 | if err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | o = append(o, credOption) |
| 79 | } |
| 80 | if config.ConnectionContext.DatabaseName != "" { |
| 81 | d.databaseName = config.ConnectionContext.DatabaseName |
| 82 | dsn := getDSN(d.config.DataSource.Host, config.ConnectionContext.DatabaseName) |
| 83 | client, err := spanner.NewClient( |
| 84 | ctx, |
| 85 | dsn, |
| 86 | o..., |
| 87 | ) |
| 88 | if err != nil { |
| 89 | return nil, err |
| 90 | } |
| 91 | d.client = client |
| 92 | } |
| 93 | |
| 94 | dbClient, err := spannerdb.NewDatabaseAdminClient(ctx, o...) |
| 95 | if err != nil { |
| 96 | return nil, err |
| 97 | } |
| 98 | |
| 99 | d.dbClient = dbClient |
| 100 | return d, nil |
| 101 | } |
| 102 | |
| 103 | // Close closes the driver. |
| 104 | func (d *Driver) Close(_ context.Context) error { |
nothing calls this directly
no test coverage detected