-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
196 lines (170 loc) · 4.77 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
package dalgo2sql
import (
"context"
"database/sql"
"fmt"
"github.com/dal-go/dalgo/dal"
)
// Field defines field
type Field struct {
Name string
}
func (v Field) String() string {
return v.Name
}
// RecordsetType defines type of a database recordset
type RecordsetType = int
const (
// Table identifies a table in a database
Table RecordsetType = iota
// View identifies a view in a database
View
// StoredProcedure identifies a stored procedure in a database
StoredProcedure
)
// Recordset hold recordset settings
type Recordset struct {
name string
t RecordsetType
primaryKey []dal.FieldRef // Primary keys by table name
}
func (v *Recordset) Name() string {
return v.name
}
func (v *Recordset) PrimaryKey() []dal.FieldRef {
if v == nil {
return nil
}
pk := make([]dal.FieldRef, len(v.primaryKey))
copy(pk, v.primaryKey)
return pk
}
func (v *Recordset) PrimaryKeyFieldNames() []string {
pk := make([]string, len(v.primaryKey))
for i, f := range v.primaryKey {
pk[i] = f.Name
}
return pk
}
func NewRecordset(name string, t RecordsetType, primaryKey []dal.FieldRef) *Recordset {
return &Recordset{
name: name,
t: t,
primaryKey: primaryKey,
}
}
func (v *Recordset) Type() RecordsetType {
return v.t
}
var _ dal.DB = (*database)(nil)
type database struct {
id string
db *sql.DB
options Options
onlyReadWriteTx bool
}
//func (dtb *database) Connect(ctx context.Context) (dal.Connection, error) {
// return connection{database: dtb}, nil
//}
func (dtb *database) ID() string {
return dtb.id
}
func (dtb *database) Adapter() dal.Adapter {
return dal.NewAdapter("dalgo2sql", Version)
}
func (dtb *database) QueryReader(c context.Context, query dal.Query) (dal.Reader, error) {
//TODO implement me
panic("implement me")
}
func (dtb *database) QueryAllRecords(ctx context.Context, query dal.Query) (records []dal.Record, err error) {
//TODO implement me
panic("implement me")
}
// Options provides database sqlOptions for DALgo - // TODO: document why & how to use
type Options struct {
PrimaryKey []string
Recordsets map[string]*Recordset
}
func (o Options) GetRecordsetByKey(key *dal.Key) *Recordset {
rsName := getRecordsetName(key)
return o.Recordsets[rsName]
}
func (o Options) PrimaryKeyFieldNames(key *dal.Key) (primaryKey []string) {
rs := o.GetRecordsetByKey(key)
if pk := rs.PrimaryKey(); len(pk) > 0 {
primaryKey = make([]string, len(pk))
for i, f := range pk {
primaryKey[i] = f.Name
}
return
}
return nil
}
func (dtb *database) RunReadonlyTransaction(ctx context.Context, f dal.ROTxWorker, options ...dal.TransactionOption) error {
dalgoTxOptions := dal.NewTransactionOptions(append(options, dal.TxWithReadonly())...)
var sqlTxOptions sql.TxOptions
if dalgoTxOptions.IsReadonly() {
sqlTxOptions.ReadOnly = !dtb.onlyReadWriteTx
} else {
return fmt.Errorf("attemt to run readonly transation without readonly option")
}
dbTx, err := dtb.db.BeginTx(ctx, &sqlTxOptions)
if err != nil {
if err.Error() == "sql: driver does not support read-only transactions" {
dtb.onlyReadWriteTx = true
sqlTxOptions.ReadOnly = false
dbTx, err = dtb.db.BeginTx(ctx, &sqlTxOptions)
}
if err != nil {
return fmt.Errorf("failed to begin transaction: %w", err)
}
}
if dbTx == nil {
return fmt.Errorf("sql driver returned nil transaction")
}
if err = f(ctx, transaction{tx: dbTx, sqlOptions: dtb.options}); err != nil {
if rollbackErr := dbTx.Rollback(); rollbackErr != nil {
return dal.NewRollbackError(rollbackErr, err)
}
return err
}
if err := dbTx.Commit(); err != nil {
return fmt.Errorf("failed to commit transaction: %w", err)
}
return nil
}
func (dtb *database) RunReadwriteTransaction(ctx context.Context, f dal.RWTxWorker, options ...dal.TransactionOption) error {
dalgoTxOptions := dal.NewTransactionOptions(options...)
sqlTxOptions := sql.TxOptions{}
if dalgoTxOptions.IsReadonly() {
return fmt.Errorf("attemt to run readwrite transation with readonly=true option")
}
dbTx, err := dtb.db.BeginTx(ctx, &sqlTxOptions)
if err != nil {
return err
}
if err = f(ctx, readwriteTransaction{tx: dbTx, sqlOptions: dtb.options}); err != nil {
if rollbackErr := dbTx.Rollback(); rollbackErr != nil {
return dal.NewRollbackError(rollbackErr, err)
}
return err
}
if err := dbTx.Commit(); err != nil {
return fmt.Errorf("failed to commit transaction: %w", err)
}
return nil
}
func (dtb *database) Select(ctx context.Context, query dal.Query) (dal.Reader, error) {
panic("implement me")
}
var _ dal.DB = (*database)(nil)
// NewDatabase creates a new instance of DALgo adapter to SQL database
func NewDatabase(db *sql.DB, options Options) dal.DB {
if db == nil {
panic("db is a required parameter, got nil")
}
return &database{
db: db,
options: options,
}
}