-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlx_db.go
143 lines (118 loc) · 3.08 KB
/
sqlx_db.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
package goquery
import (
"database/sql"
"errors"
"fmt"
"reflect"
"github.com/georgysavva/scany/sqlscan"
"github.com/jmoiron/sqlx"
)
type SqlRows struct {
rows *sql.Rows
rowScanner *sqlscan.RowScanner
}
func (s *SqlRows) Columns() ([]string, error) {
return s.rows.Columns()
}
func (s *SqlRows) ColumnTypes() ([]reflect.Type, error) {
sts, err := s.rows.ColumnTypes()
if err != nil {
return nil, err
}
t := make([]reflect.Type, len(sts))
for i := range sts {
t[i] = sts[i].ScanType()
}
return t, nil
}
func (s *SqlRows) Next() bool {
return s.rows.Next()
}
func (s *SqlRows) Scan(dest ...interface{}) error {
return s.rows.Scan(dest...)
}
func (s *SqlRows) ScanStruct(dest interface{}) error {
if s.rowScanner == nil {
s.rowScanner = sqlscan.NewRowScanner(s.rows)
}
return s.rowScanner.Scan(dest)
}
func (s *SqlRows) Close() error {
return s.rows.Close()
}
type SqlxDb struct {
db *sqlx.DB
dialect DbDialect
}
func getDialect(driver string) (DbDialect, error) {
switch driver {
case "pgx":
return pgDialect, nil
case "godror":
return oracleDialect, nil
default:
return DbDialect{}, errors.New(fmt.Sprintf("Unsupported DB Driver: %s", driver))
}
}
func NewSqlxConnection(config *RdbmsConfig) (SqlxDb, error) {
dialect, err := getDialect(config.DbDriver)
if err != nil {
return SqlxDb{}, err
}
dburl := dialect.Url(config)
con, err := sqlx.Connect(config.DbDriver, dburl)
return SqlxDb{con, dialect}, err
}
func (sdb *SqlxDb) querier(tx *Tx) sqlx.Queryer {
if tx != nil {
return tx.SqlXTx()
}
return sdb.db
}
func (sdb *SqlxDb) Connection() interface{} {
return sdb.db
}
func (sdb *SqlxDb) Select(dest interface{}, tx *Tx, stmt string, params ...interface{}) error {
if len(params) == 0 {
return sqlx.Select(sdb.querier(tx), dest, stmt)
}
return sqlx.Select(sdb.querier(tx), dest, stmt, params...)
}
func (sdb *SqlxDb) Get(dest interface{}, tx *Tx, stmt string, params ...interface{}) error {
if len(params) == 0 {
return sqlx.Get(sdb.querier(tx), dest, stmt)
}
return sqlx.Get(sdb.querier(tx), dest, stmt, params...)
}
func (sdb *SqlxDb) Query(tx *Tx, stmt string, params ...interface{}) (Rows, error) {
rows, err := sdb.db.Query(stmt, params...)
return &SqlRows{rows, nil}, err
}
func (sdb *SqlxDb) Exec(tx *Tx, stmt string, params ...interface{}) error {
res, err := sdb.db.Exec(stmt, params...)
//@TODO what to do with result?
fmt.Println(res)
return err
}
func (sdb *SqlxDb) MustExec(tx *Tx, stmt string, params ...interface{}) {
res := sdb.db.MustExec(stmt, params...)
//@TODO what to do with result?
fmt.Println(res)
}
func (sdb *SqlxDb) Batch() (Batch, error) {
return nil, errors.New("batch operations are not supported by the sqlx driver")
}
func (sdb *SqlxDb) SendBatch(batch Batch) BatchResult {
return nil
}
func (sdb *SqlxDb) InsertStmt(ds DataSet) (string, error) {
return ToInsert(ds, sdb.dialect)
}
func (sdb *SqlxDb) Insert(ds DataSet, rec interface{}, tx *Tx) error {
//pdb.db.Exec(context.Background(),stmt,
return nil
}
func (sdb *SqlxDb) Transaction() (Tx, error) {
tx, err := sdb.db.Beginx()
return Tx{tx}, err
}