-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.go
269 lines (224 loc) · 5.78 KB
/
driver.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package cfd1
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"io"
"net/url"
"sync"
)
func init() {
sql.Register("cfd1", &d1Driver{})
}
// d1Driver implements a database/sql driver for D1.
type d1Driver struct {
mu sync.Mutex
clientFactory func(cfg *config) (CFD1Client, error)
}
// Open returns a new connection to the database.
func (d *d1Driver) Open(name string) (driver.Conn, error) {
connector, err := d.OpenConnector(name)
if err != nil {
return nil, err
}
return connector.Connect(context.Background())
}
// OpenConnector implements driver.DriverContext interface.
func (d *d1Driver) OpenConnector(name string) (driver.Connector, error) {
cfg, err := parseDSN(name)
if err != nil {
return nil, err
}
return &connector{
driver: d,
cfg: cfg,
}, nil
}
// Connector implements driver.Connector interface.
type connector struct {
driver *d1Driver
cfg *config
}
func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
client, err := c.driver.createClient(c.cfg)
if err != nil {
return nil, err
}
h, err := client.GetHandle(ctx, c.cfg.DatabaseNameOrUUID)
if err != nil {
return nil, err
}
newConn := &conn{
handle: h,
}
return newConn, nil
}
func (c *connector) Driver() driver.Driver {
return c.driver
}
// createClient creates a new CFD1Client instance.
func (d *d1Driver) createClient(cfg *config) (CFD1Client, error) {
if d.clientFactory != nil {
return d.clientFactory(cfg)
}
return NewClient(cfg.AccountID, cfg.APIToken), nil
}
type config struct {
AccountID string
APIToken string
DatabaseNameOrUUID string
}
func parseDSN(dsn string) (*config, error) {
cfg := &config{}
u, err := url.Parse(dsn)
if err != nil {
return nil, fmt.Errorf("invalid DSN: %v", err)
}
// Extract account_id and api_token from user info
if u.User != nil {
cfg.AccountID = u.User.Username()
cfg.APIToken, _ = u.User.Password()
}
// Extract database_id from host
cfg.DatabaseNameOrUUID = u.Host
// Validate the config
if cfg.AccountID == "" {
return nil, errors.New("account_id (username) is required in the DSN")
}
if cfg.APIToken == "" {
return nil, errors.New("api_token (password) is required in the DSN")
}
if cfg.DatabaseNameOrUUID == "" {
return nil, errors.New("database_id (host) is required in the DSN")
}
return cfg, nil
}
type conn struct {
handle *Handle
}
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) Close() error {
// no-op
return nil
}
func (c *conn) Begin() (driver.Tx, error) {
return nil, driver.ErrSkip
}
func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
return nil, driver.ErrSkip
}
// Implement ExecerContext interface
func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
if !c.IsValid() {
return nil, driver.ErrBadConn
}
params := namedValuesToAny(args)
_, err := c.handle.Query(ctx, query, params...)
if err != nil {
return nil, err
}
return &driverResult{
rowsAffected: int64(c.handle.LastMeta().RowsWritten),
lastInsertID: int64(c.handle.LastRowID()),
}, nil
}
// Implement QueryerContext interface
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
if !c.IsValid() {
return nil, driver.ErrBadConn
}
params := namedValuesToAny(args)
result, err := c.handle.Query(ctx, query, params...)
if err != nil {
return nil, err
}
columns := make([]string, 0, len(result[0]))
for col := range result[0] {
columns = append(columns, col)
}
return &rows{
columns: columns,
rows: result,
}, nil
}
// Implement Pinger interface
func (c *conn) Ping(ctx context.Context) error {
return c.handle.Ping(ctx)
}
func (c *conn) ResetSession(ctx context.Context) error {
// We don't keep connections open so this is a no-op
return nil
}
func (c *conn) IsValid() bool {
return c.handle != nil
}
type stmt struct {
conn *conn
query string
}
func (s *stmt) Close() error {
return nil
}
func (s *stmt) NumInput() int {
return -1
}
func (s *stmt) Exec(args []driver.Value) (driver.Result, error) {
return s.ExecContext(context.Background(), valuesToNamedValues(args))
}
func (s *stmt) Query(args []driver.Value) (driver.Rows, error) {
return s.QueryContext(context.Background(), valuesToNamedValues(args))
}
func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
return s.conn.ExecContext(ctx, s.query, args)
}
func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
return s.conn.QueryContext(ctx, s.query, args)
}
type rows struct {
columns []string
rows []map[string]any
current int
}
func (r *rows) Columns() []string { return r.columns }
func (r *rows) Close() error { return nil }
func (r *rows) Next(dest []driver.Value) error {
if r.current >= len(r.rows) {
return io.EOF
}
row := r.rows[r.current]
for i, col := range r.columns {
dest[i] = row[col]
}
r.current++
return nil
}
type driverResult struct {
lastInsertID int64
rowsAffected int64
}
func (r *driverResult) LastInsertId() (int64, error) { return r.lastInsertID, nil }
func (r *driverResult) RowsAffected() (int64, error) { return r.rowsAffected, nil }
func valuesToNamedValues(vals []driver.Value) []driver.NamedValue {
nvs := make([]driver.NamedValue, len(vals))
for i, v := range vals {
nvs[i] = driver.NamedValue{Ordinal: i + 1, Value: v}
}
return nvs
}
func namedValuesToAny(nvs []driver.NamedValue) []any {
params := make([]any, len(nvs))
for i, nv := range nvs {
params[i] = nv.Value
}
return params
}