-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
database.go
379 lines (343 loc) · 9.53 KB
/
database.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package trdsql
import (
"context"
"database/sql"
"errors"
"fmt"
"io"
"log"
"strings"
)
var (
// ErrNoTransaction is returned if SQL is executed when a transaction has not started.
// SQL must be executed within a transaction.
ErrNoTransaction = errors.New("transaction has not been started")
// ErrNilReader is returned by Set reader of the specified file is nil error.
ErrNilReader = errors.New("nil reader")
// ErrInvalidNames is returned by Set if invalid names(number of columns is 0).
ErrInvalidNames = errors.New("invalid names")
// ErrInvalidTypes is returned by Set if invalid column types (does not match the number of column names).
ErrInvalidTypes = errors.New("invalid types")
// ErrNoStatement is returned by no SQL statement.
ErrNoStatement = errors.New("no SQL statement")
)
// DB represents database information.
type DB struct {
// driver holds the sql driver as a string.
driver string
// dsn holds dsn of sql as a character string.
dsn string
// quote is the quote character(s) that varies depending on the sql driver.
// PostgreSQL is ("), sqlite3 and mysql is (`).
quote string
// maxBulk is the maximum number of bundles for bulk insert.
// The number of columns x rows is less than maxBulk.
maxBulk int
// *sql.DB represents the database connection.
*sql.DB
// Tx represents a database transaction.
Tx *sql.Tx
// importCount represents the number that is incremented to identify imported files.
importCount int
}
// Disconnect is disconnect the database.
func (db *DB) Disconnect() error {
return db.Close()
}
// CreateTable is create a (temporary) table in the database.
// The arguments are the table name, column name, column type, and temporary flag.
func (db *DB) CreateTable(tableName string, columnNames []string, columnTypes []string, isTemporary bool) error {
return db.CreateTableContext(context.Background(), tableName, columnNames, columnTypes, isTemporary)
}
// CreateTableContext is create a (temporary) table in the database.
// The arguments are the table name, column name, column type, and temporary flag.
func (db *DB) CreateTableContext(ctx context.Context, tableName string, columnNames []string, columnTypes []string, isTemporary bool) error {
if db.Tx == nil {
return ErrNoTransaction
}
if len(columnNames) == 0 {
return ErrInvalidNames
}
if len(columnNames) != len(columnTypes) {
return ErrInvalidTypes
}
query := db.queryCreateTable(tableName, columnNames, columnTypes, isTemporary)
debug.Printf(query)
_, err := db.Tx.ExecContext(ctx, query)
return err
}
func (db *DB) queryCreateTable(tableName string, columnNames []string, columnTypes []string, isTemporary bool) string {
var buf strings.Builder
if isTemporary {
buf.WriteString("CREATE TEMPORARY TABLE ")
} else {
buf.WriteString("CREATE TABLE ")
}
buf.WriteString(tableName)
buf.WriteString(" ( ")
buf.WriteString(db.QuotedName(columnNames[0]))
buf.WriteString(" ")
buf.WriteString(columnTypes[0])
for i, columnName := range columnNames[1:] {
buf.WriteString(", ")
buf.WriteString(db.QuotedName(columnName))
buf.WriteString(" ")
buf.WriteString(columnTypes[i+1])
}
buf.WriteString(" );")
return buf.String()
}
// importTable represents the table data to be imported.
type importTable struct {
tableName string
columns []string
row []any
maxCap int
lastCount int
count int
}
// Import is imports data into a table.
func (db *DB) Import(tableName string, columnNames []string, reader Reader) error {
return db.ImportContext(context.Background(), tableName, columnNames, reader)
}
// ImportContext is imports data into a table.
func (db *DB) ImportContext(ctx context.Context, tableName string, columnNames []string, reader Reader) error {
if db.Tx == nil {
return ErrNoTransaction
}
if reader == nil {
return ErrNilReader
}
columns := make([]string, len(columnNames))
for i := range columnNames {
columns[i] = db.QuotedName(columnNames[i])
}
row := make([]any, len(columnNames))
table := &importTable{
tableName: tableName,
columns: columns,
row: row,
lastCount: 0,
count: 0,
}
if db.driver == "postgres" {
return db.copyImport(ctx, table, reader)
}
return db.insertImport(ctx, table, reader)
}
// copyImport adds rows to a table with the COPY clause (PostgreSQL only).
func (db *DB) copyImport(ctx context.Context, table *importTable, reader Reader) error {
query := queryCopy(table)
debug.Printf(query)
stmt, err := db.Tx.PrepareContext(ctx, query)
if err != nil {
return fmt.Errorf("COPY prepare: %w", err)
}
defer db.stmtClose(stmt)
preReadRows := reader.PreReadRow()
for _, row := range preReadRows {
if row == nil {
break
}
if _, err = stmt.ExecContext(ctx, row...); err != nil {
return err
}
}
for {
table.row, err = reader.ReadRow(table.row)
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return fmt.Errorf("COPY read: %w", err)
}
// Skip when empty read.
if len(table.row) == 0 {
continue
}
if _, err = stmt.ExecContext(ctx, table.row...); err != nil {
return err
}
}
_, err = stmt.ExecContext(ctx)
return err
}
// queryCopy constructs a SQL COPY statement.
func queryCopy(table *importTable) string {
var buf strings.Builder
buf.WriteString("COPY ")
buf.WriteString(table.tableName)
buf.WriteString(" (")
buf.WriteString(table.columns[0])
for _, column := range table.columns[1:] {
buf.WriteString(", ")
buf.WriteString(column)
}
buf.WriteString(") FROM STDIN;")
return buf.String()
}
// insertImport adds a row to a table with an INSERT clause.
// Insert multiple rows by bulk insert.
func (db *DB) insertImport(ctx context.Context, table *importTable, reader Reader) error {
var err error
var stmt *sql.Stmt
defer db.stmtClose(stmt)
if len(table.row) > db.maxBulk {
table.maxCap = len(table.row)
} else {
table.maxCap = (db.maxBulk / len(table.row)) * len(table.row)
}
bulk := make([]any, 0, table.maxCap)
preRows := reader.PreReadRow()
preRowNum := len(preRows)
preCount := 0
for eof := false; !eof; {
if preCount < preRowNum {
// PreRead
for preCount < preRowNum {
row := preRows[preCount]
bulk = append(bulk, row...)
table.count++
preCount++
if (table.count * len(table.row)) > table.maxCap {
break
}
}
} else {
// Read
bulk, err = bulkPush(ctx, table, reader, bulk)
if err != nil {
if !errors.Is(err, io.EOF) {
return fmt.Errorf("bulk read: %w", err)
}
eof = true
if len(bulk) == 0 {
return nil
}
}
}
stmt, err = db.bulkStmtOpen(ctx, table, stmt)
if err != nil {
return err
}
if _, err := stmt.ExecContext(ctx, bulk...); err != nil {
return err
}
bulk = bulk[:0]
table.count = 0
}
return nil
}
func (db *DB) stmtClose(stmt *sql.Stmt) {
if stmt == nil {
return
}
if err := stmt.Close(); err != nil {
log.Printf("ERROR: stmtClose:%s", err)
}
}
func bulkPush(ctx context.Context, table *importTable, input Reader, bulk []any) ([]any, error) {
for (table.count * len(table.row)) < table.maxCap {
row, err := input.ReadRow(table.row)
if err != nil {
return bulk, err
}
// Skip when empty read.
if len(row) == 0 {
continue
}
bulk = append(bulk, row...)
table.count++
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
}
return bulk, nil
}
func (db *DB) bulkStmtOpen(ctx context.Context, table *importTable, stmt *sql.Stmt) (*sql.Stmt, error) {
if table.lastCount == table.count {
return stmt, nil
}
db.stmtClose(stmt)
stmt, err := db.insertPrepare(ctx, table)
if err != nil {
return nil, err
}
table.lastCount = table.count
return stmt, nil
}
func (db *DB) insertPrepare(ctx context.Context, table *importTable) (*sql.Stmt, error) {
query := queryInsert(table)
debug.Printf(query)
stmt, err := db.Tx.PrepareContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("INSERT Prepare: %s:%w", query, err)
}
return stmt, nil
}
// queryInsert constructs a SQL INSERT statement.
func queryInsert(table *importTable) string {
var buf strings.Builder
buf.WriteString("INSERT INTO ")
buf.WriteString(table.tableName)
buf.WriteString(" (")
buf.WriteString(table.columns[0])
for _, column := range table.columns[1:] {
buf.WriteString(", ")
buf.WriteString(column)
}
buf.WriteString(") VALUES ")
buf.WriteString("(")
buf.WriteString("?")
for i := 1; i < len(table.columns); i++ {
buf.WriteString(",?")
}
buf.WriteString(")")
for i := 1; i < table.count; i++ {
buf.WriteString(",(")
buf.WriteString("?")
for j := 1; j < len(table.columns); j++ {
buf.WriteString(",?")
}
buf.WriteString(")")
}
buf.WriteString(";")
return buf.String()
}
// QuotedName returns the table name quoted.
// Returns as is, if already quoted.
func (db *DB) QuotedName(orgName string) string {
if orgName == "" {
return ""
}
if orgName[0] == db.quote[0] {
return orgName
}
var buf strings.Builder
buf.WriteString(db.quote)
buf.WriteString(orgName)
buf.WriteString(db.quote)
return buf.String()
}
// Select is executes SQL select statements.
func (db *DB) Select(query string) (*sql.Rows, error) {
return db.SelectContext(context.Background(), query)
}
// SelectContext is executes SQL select statements with context.
// SelectContext is a wrapper for QueryContext.
func (db *DB) SelectContext(ctx context.Context, query string) (*sql.Rows, error) {
rows, err := db.Tx.QueryContext(ctx, query)
if err != nil {
return rows, fmt.Errorf("%w [%s]", err, query)
}
return rows, nil
}
func (db *DB) OtherExecContext(ctx context.Context, query string) error {
_, err := db.Tx.ExecContext(ctx, query)
if err != nil {
return fmt.Errorf("%w [%s]", err, query)
}
return nil
}