-
Notifications
You must be signed in to change notification settings - Fork 0
/
inserter.go
32 lines (27 loc) · 1.05 KB
/
inserter.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
package dalgo2sql
import (
"context"
"github.com/dal-go/dalgo/dal"
)
func (dtb *database) Insert(ctx context.Context, record dal.Record, opts ...dal.InsertOption) error {
return insertSingle(ctx, dtb.options, record, dtb.db.ExecContext)
}
func (t transaction) Insert(ctx context.Context, record dal.Record, opts ...dal.InsertOption) error {
return insertSingle(ctx, t.sqlOptions, record, t.tx.ExecContext)
}
func insertSingle(ctx context.Context, options Options, record dal.Record, exec statementExecutor, opts ...dal.InsertOption) error {
query := buildSingleRecordQuery(insert, options, record)
if _, err := exec(ctx, query.text, query.args...); err != nil {
return err
}
return nil
}
// InsertMulti inserts multiple records in a single transaction at once. TODO: Implement batched multi-insert
func (t transaction) InsertMulti(ctx context.Context, records []dal.Record, opts ...dal.InsertOption) error {
for _, record := range records {
if err := insertSingle(ctx, t.sqlOptions, record, t.tx.ExecContext); err != nil {
return err
}
}
return nil
}