-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.go
52 lines (44 loc) · 1.36 KB
/
connector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package immusql
import (
"context"
"database/sql/driver"
"github.com/codenotary/immudb/pkg/client"
driverClient "github.com/tauu/immusql/client"
"github.com/tauu/immusql/embedded"
)
// connector opens connections to a preconfigured immudb instance.
type connector struct {
config dsnConfig
driver ImmudbDriver
}
// -- Connector interface --
// Connect establishes a new connection to an immudb instance.
func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
if c.config.Embedded {
return c.openEmbedded(ctx)
}
return c.openClient(ctx)
}
// Driver returns the driver used by the connector.
func (c *connector) Driver() *ImmudbDriver {
return &c.driver
}
// openClient opens an immuclient connection to an immudb.
func (c *connector) openClient(ctx context.Context) (driver.Conn, error) {
// Assemble options for connecting to immudb.
options := client.DefaultOptions().
WithAddress(c.config.Host).
WithPort(c.config.Port).
WithDatabase(c.config.Name)
// Set username and password.
if c.config.User != "" {
options = options.WithUsername(c.config.User).
WithPassword(c.config.Pass)
}
return driverClient.Open(ctx, options)
}
// openEmbedded creates an embedded immudb engine.
func (c *connector) openEmbedded(ctx context.Context) (driver.Conn, error) {
// Open an engine for it.
return embedded.Open(ctx, c.config.Path, c.config.Name)
}