-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
importer_buffer.go
48 lines (43 loc) · 1.2 KB
/
importer_buffer.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
package trdsql
import (
"context"
"io"
)
// BufferImporter a structure that includes tableName and Reader.
type BufferImporter struct {
Reader
tableName string
}
// NewBufferImporter returns trdsql BufferImporter.
func NewBufferImporter(tableName string, r io.Reader, options ...ReadOpt) (*BufferImporter, error) {
readOpts := NewReadOpts(options...)
readOpts.realFormat = readOpts.InFormat
reader, err := NewReader(r, readOpts)
if err != nil {
return nil, err
}
return &BufferImporter{
tableName: tableName,
Reader: reader,
}, nil
}
// Import is a method to import from Reader in BufferImporter.
func (i *BufferImporter) Import(db *DB, query string) (string, error) {
ctx := context.Background()
return i.ImportContext(ctx, db, query)
}
// ImportContext is a method to import from Reader in BufferImporter.
func (i *BufferImporter) ImportContext(ctx context.Context, db *DB, query string) (string, error) {
names, err := i.Names()
if err != nil {
return query, err
}
types, err := i.Types()
if err != nil {
return query, err
}
if err := db.CreateTable(i.tableName, names, types, true); err != nil {
return query, err
}
return query, db.ImportContext(ctx, i.tableName, names, i.Reader)
}