-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
98 lines (82 loc) · 2.22 KB
/
conn.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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package bigquery
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"cloud.google.com/go/bigquery"
)
var (
_ driver.Conn = (*conn)(nil)
_ driver.Pinger = (*conn)(nil)
_ driver.Validator = (*conn)(nil)
_ driver.ConnBeginTx = (*conn)(nil)
_ driver.ConnPrepareContext = (*conn)(nil)
_ driver.ExecerContext = (*conn)(nil)
_ driver.QueryerContext = (*conn)(nil)
_ driver.NamedValueChecker = (*conn)(nil)
)
type conn struct {
client *bigquery.Client
config Config
sessionID string
closed bool
options
}
func (c *conn) Ping(ctx context.Context) error {
if _, err := c.client.Dataset(c.config.Dataset).Metadata(ctx); err != nil {
return err
}
return nil
}
func (c *conn) IsValid() bool {
// TODO: Return false if session has ended
// (can connection be broken in any other way?)
return true
}
func (c *conn) Prepare(query string) (driver.Stmt, error) {
return c.PrepareContext(context.Background(), query)
}
func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
return &stmt{
conn: c,
query: query,
}, nil
}
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
statement := &stmt{
conn: c,
query: query,
}
return statement.QueryContext(ctx, args)
}
func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
statement := &stmt{
conn: c,
query: query,
}
return statement.ExecContext(ctx, args)
}
func (c *conn) Begin() (driver.Tx, error) {
return c.BeginTx(context.Background(), driver.TxOptions{})
}
func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
if level := sql.IsolationLevel(opts.Isolation); level != sql.LevelDefault {
return nil, fmt.Errorf("invalid isolation level (only sql.LevelDefault is supported): %s", level)
}
if opts.ReadOnly {
return nil, errors.New("read-only transactions not supported")
}
if _, err := c.ExecContext(ctx, "BEGIN TRANSACTION", nil); err != nil {
return nil, err
}
return &tx{conn: c}, nil
}
func (c *conn) Close() error {
if c.closed {
return nil
}
c.closed = true
return c.client.Close()
}