-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsingledb.go
318 lines (273 loc) · 7.6 KB
/
singledb.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package duckdbreplicator
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"io"
"log/slog"
"net/url"
"os"
"strings"
"sync"
"github.com/XSAM/otelsql"
"github.com/jmoiron/sqlx"
"github.com/marcboeker/go-duckdb"
"go.opentelemetry.io/otel/attribute"
)
type singledb struct {
db *sqlx.DB
writeMU *sync.Mutex // limits write queries to one at a time. Does not block read queries.
logger *slog.Logger
}
type SingleDBOptions struct {
DSN string
Clean bool
InitQueries []string
Logger *slog.Logger
}
var _ DB = &singledb{}
// NewSingleDB creates a new DB that writes to and reads from a single DuckDB database.
// This is useful for testing only.
func NewSingleDB(ctx context.Context, opts *SingleDBOptions) (DB, error) {
if opts.Clean {
u, err := url.Parse(opts.DSN)
if err != nil {
return nil, err
}
if u.Path != "" {
err = os.Remove(u.Path)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
}
}
connector, err := duckdb.NewConnector(opts.DSN, func(execer driver.ExecerContext) error {
for _, qry := range opts.InitQueries {
_, err := execer.ExecContext(context.Background(), qry, nil)
if err != nil && strings.Contains(err.Error(), "Failed to download extension") {
// Retry using another mirror. Based on: https://github.com/duckdb/duckdb/issues/9378
_, err = execer.ExecContext(context.Background(), qry+" FROM 'http://nightly-extensions.duckdb.org'", nil)
}
if err != nil {
return err
}
}
return nil
})
if err != nil {
if strings.Contains(err.Error(), "Symbol not found") {
fmt.Printf("Your version of macOS is not supported. Please upgrade to the latest major release of macOS. See this link for details: https://support.apple.com/en-in/macos/upgrade")
os.Exit(1)
}
return nil, err
}
db := sqlx.NewDb(otelsql.OpenDB(connector), "duckdb")
err = otelsql.RegisterDBStatsMetrics(db.DB, otelsql.WithAttributes(attribute.String("db.system", "duckdb")))
if err != nil {
db.Close()
return nil, fmt.Errorf("registering db stats metrics: %w", err)
}
err = db.PingContext(context.Background())
if err != nil {
db.Close()
return nil, err
}
if opts.Logger == nil {
opts.Logger = slog.New(slog.NewJSONHandler(io.Discard, nil))
}
return &singledb{
db: db,
writeMU: &sync.Mutex{},
logger: opts.Logger,
}, nil
}
// Close implements DB.
func (s *singledb) Close() error {
return s.db.Close()
}
// AcquireReadConnection implements DB.
func (s *singledb) AcquireReadConnection(ctx context.Context) (Conn, func() error, error) {
conn, err := s.db.Connx(ctx)
if err != nil {
return nil, nil, err
}
return &singledbConn{
Conn: conn,
db: s,
}, conn.Close, nil
}
func (s *singledb) AcquireWriteConnection(ctx context.Context) (Conn, func() error, error) {
s.writeMU.Lock()
c, err := s.db.Connx(ctx)
if err != nil {
s.writeMU.Unlock()
return nil, nil, err
}
return &singledbConn{
Conn: c,
db: s,
}, func() error {
err := c.Close()
s.writeMU.Unlock()
return err
}, nil
}
// CreateTableAsSelect implements DB.
func (s *singledb) CreateTableAsSelect(ctx context.Context, name, uery string, opts *CreateTableOptions) error {
s.writeMU.Lock()
defer s.writeMU.Unlock()
conn, err := s.db.Connx(ctx)
if err != nil {
return err
}
return s.createTableAsSelect(ctx, conn, name, uery, opts)
}
func (s *singledb) createTableAsSelect(ctx context.Context, conn *sqlx.Conn, name, query string, opts *CreateTableOptions) error {
var typ string
if opts != nil && opts.View {
typ = "VIEW"
} else {
typ = "TABLE"
}
_, err := conn.ExecContext(ctx, fmt.Sprintf("CREATE OR REPLACE %s %s AS (%s\n)", typ, safeSQLName(name), query))
return err
}
// DropTable implements DB.
func (s *singledb) DropTable(ctx context.Context, name string) error {
s.writeMU.Lock()
defer s.writeMU.Unlock()
conn, err := s.db.Connx(ctx)
if err != nil {
return err
}
return s.dropTable(ctx, conn, name)
}
func (s *singledb) dropTable(ctx context.Context, conn *sqlx.Conn, name string) error {
view, err := isView(ctx, conn, name)
if err != nil {
return err
}
var typ string
if view {
typ = "VIEW"
} else {
typ = "TABLE"
}
_, err = conn.ExecContext(ctx, fmt.Sprintf("DROP %s %s", typ, safeSQLName(name)))
return err
}
// InsertTableAsSelect implements DB.
func (s *singledb) InsertTableAsSelect(ctx context.Context, name, query string, opts *InsertTableOptions) error {
if opts == nil {
opts = &InsertTableOptions{
Strategy: IncrementalStrategyAppend,
}
}
s.writeMU.Lock()
defer s.writeMU.Unlock()
conn, err := s.db.Connx(ctx)
if err != nil {
return err
}
if opts == nil {
opts = &InsertTableOptions{
Strategy: IncrementalStrategyAppend,
}
}
return execIncrementalInsert(ctx, conn, safeSQLName(name), query, opts)
}
// RenameTable implements DB.
func (s *singledb) RenameTable(ctx context.Context, oldName, newName string) error {
s.writeMU.Lock()
defer s.writeMU.Unlock()
conn, err := s.db.Connx(ctx)
if err != nil {
return err
}
return s.renameTable(ctx, conn, oldName, newName)
}
func (s *singledb) renameTable(ctx context.Context, conn *sqlx.Conn, oldName, newName string) error {
view, err := isView(ctx, conn, oldName)
if err != nil {
return err
}
var typ string
if view {
typ = "VIEW"
} else {
typ = "TABLE"
}
newNameIsView, err := isView(ctx, conn, newName)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
return err
}
// The newName does not exist.
_, err = conn.ExecContext(ctx, fmt.Sprintf("ALTER %s %s RENAME TO %s", typ, safeSQLName(oldName), safeSQLName(newName)))
return err
}
// The newName is already occupied.
var existingTyp string
if newNameIsView {
existingTyp = "VIEW"
} else {
existingTyp = "TABLE"
}
_, err = conn.ExecContext(ctx, fmt.Sprintf("DROP %s IF EXISTS %s", existingTyp, safeSQLName(newName)))
if err != nil {
return err
}
_, err = conn.ExecContext(ctx, fmt.Sprintf("ALTER %s %s RENAME TO %s", typ, safeSQLName(oldName), safeSQLName(newName)))
return err
}
// AddTableColumn implements DB.
func (s *singledb) AddTableColumn(ctx context.Context, tableName, columnName, typ string) error {
s.writeMU.Lock()
defer s.writeMU.Unlock()
conn, err := s.db.Connx(ctx)
if err != nil {
return err
}
return s.addTableColumn(ctx, conn, tableName, columnName, typ)
}
func (s *singledb) addTableColumn(ctx context.Context, conn *sqlx.Conn, tableName, columnName, typ string) error {
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s %s", safeSQLString(tableName), safeSQLName(columnName), typ))
return err
}
// AlterTableColumn implements DB.
func (s *singledb) AlterTableColumn(ctx context.Context, tableName, columnName, newType string) error {
s.writeMU.Lock()
defer s.writeMU.Unlock()
conn, err := s.db.Connx(ctx)
if err != nil {
return err
}
return s.alterTableColumn(ctx, conn, tableName, columnName, newType)
}
func (s *singledb) alterTableColumn(ctx context.Context, conn *sqlx.Conn, tableName, columnName, newType string) error {
_, err := conn.ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s TYPE %s", safeSQLName(tableName), safeSQLName(columnName), newType))
return err
}
// TODO :: fix by calling pragma_database_size
func (s *singledb) Size() int64 {
return 0
}
func isView(ctx context.Context, conn *sqlx.Conn, name string) (bool, error) {
var view bool
err := conn.QueryRowxContext(ctx, `
SELECT
UPPER(table_type) = 'VIEW'
FROM
information_schema.tables
WHERE
table_catalog = current_database()
AND table_schema = 'main'
AND LOWER(table_name) = LOWER(?)
`, name).Scan(&view)
if err != nil {
return false, err
}
return view, nil
}