-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnector.go
More file actions
79 lines (68 loc) · 1.86 KB
/
Copy pathconnector.go
File metadata and controls
79 lines (68 loc) · 1.86 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package quackdriver
import (
"context"
"database/sql/driver"
"runtime"
"github.com/zzir/quackdriver/internal/localserver"
"github.com/zzir/quackdriver/internal/message"
"github.com/zzir/quackdriver/internal/transport"
)
// clientVersion is sent in the ConnectionRequest. Bump when a server expects
// a newer string for compatibility checks.
const clientVersion = "v1.5.3"
// Connector is a parsed DSN that can produce new Conn values.
type Connector struct {
uri *transport.URI
localServer *localserver.Server
}
func newConnector(dsn string) (*Connector, error) {
u, err := transport.ParseURI(dsn)
if err != nil {
return nil, err
}
c := &Connector{uri: u}
if u.IsLocal {
srv, err := localserver.Acquire(u.DBPath, u.Token, u.Timeout)
if err != nil {
return nil, err
}
u.Host = "127.0.0.1"
u.Port = srv.Port()
u.Token = srv.Token()
c.localServer = srv
}
return c, nil
}
// Close releases resources held by this Connector. For local-mode
// Connectors, this decrements the local server's reference count and
// may stop the DuckDB process.
func (c *Connector) Close() error {
if c.localServer != nil {
return c.localServer.Release()
}
return nil
}
// Connect opens a Quack session by issuing a ConnectionRequest.
func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
return c.connect(ctx)
}
// Driver returns the underlying Driver value.
func (c *Connector) Driver() driver.Driver { return Driver{} }
func (c *Connector) connect(ctx context.Context) (*Conn, error) {
if ctx == nil {
ctx = context.Background()
}
t := transport.New(c.uri)
conn := &Conn{
transport: t,
token: c.uri.Token,
}
if err := conn.handshake(ctx); err != nil {
return nil, err
}
return conn, nil
}
func defaultPlatform() string {
return "go-" + runtime.GOOS + "-" + runtime.GOARCH
}
var _ message.Body = message.ConnectionRequest{}