-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
176 lines (150 loc) · 4.17 KB
/
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
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
package QueryHelper
import (
"context"
"fmt"
"github.com/jmoiron/sqlx"
"github.com/xwb1989/sqlparser"
"regexp"
"strings"
)
var (
// List of reserved keywords
reservedKeywords = []string{
"ADD", "ALL", "ALTER", "AND", "ANY", "AS", "ASC", "BACKUP", "BETWEEN",
"CASE", "CHECK", "COLUMN", "CONSTRAINT", "CREATE", "DATABASE", "DEFAULT",
"DELETE", "DESC", "DISTINCT", "DROP", "EXEC", "EXISTS", "FOREIGN", "FROM",
"FULL", "GROUP", "HAVING", "IN", "INDEX", "INNER", "INSERT", "IS", "JOIN",
"KEY", "LEFT", "LIKE", "LIMIT", "NOT", "NULL", "OR", "ORDER", "OUTER",
"PRIMARY", "PROCEDURE", "RIGHT", "ROWNUM", "SELECT", "SET", "TABLE", "TOP",
"TRUNCATE", "UNION", "UNIQUE", "UPDATE", "VALUES", "VIEW", "WHERE",
}
// Regex patterns for invalid column names
patterns = []string{
`^\d+.*`, // Starts with a number
`.*\s+.*`, // Contains space
`.*[-\.@].*`, // Contains dash, dot, or at-sign
}
)
type DBOptions struct {
NoLock bool
ReadPast bool
}
func (o *DBOptions) EnableNoLock() *DBOptions {
o.NoLock = true
o.ReadPast = false
return o
}
func (o *DBOptions) EnableReadPast() *DBOptions {
o.NoLock = false
o.ReadPast = true
return o
}
type DB interface {
Ping(ctx context.Context) error
CreateTable(ctx context.Context, dataset, table string, columns map[string]Column) error
QueryContext(ctx context.Context, query string, options *DBOptions, args interface{}) (DBRow, error)
ExecContext(ctx context.Context, query string, args interface{}) error
RawQueryContext(ctx context.Context, query string, options *DBOptions, args ...interface{}) (DBRow, error)
GetTableIndexes(database, tableName string) ([]IndexInfo, error)
GetTableDefinition(database string, tableName string) ([]ColumnInfo, error)
Close()
GetDataset(ds string) string
Version() string
}
type DBRow interface {
Next() bool
StructScan(i interface{}) error
Scan(i ...any) error
Close() error
}
var _ DBRow = &sqlx.Rows{}
type MockDB struct {
tables map[string]*mockTable
mockData map[string]map[string]*mockData
prefix string
}
func (m MockDB) Version() string {
return "debug"
}
func (m MockDB) GetTableIndexes(database, tableName string) ([]IndexInfo, error) {
return nil, nil
}
func (m MockDB) GetTableDefinition(database string, tableName string) ([]ColumnInfo, error) {
return nil, nil
}
func (m MockDB) RawQueryContext(ctx context.Context, query string, options *DBOptions, args ...interface{}) (DBRow, error) {
return nil, nil
}
func (m MockDB) GetDataset(ds string) string {
if len(m.prefix) > 0 {
return m.prefix + ds
}
return ds
}
type mockTable struct {
name string
dataset string
columns map[string]Column
}
type mockData struct {
//name string
//dataset string
//columns map[string]Column
}
func NewMockDB() *MockDB {
return &MockDB{
tables: map[string]*mockTable{},
}
}
func (m MockDB) Ping(ctx context.Context) error {
return nil
}
func (m MockDB) CreateTable(ctx context.Context, dataset, table string, columns map[string]Column) error {
m.tables[fmt.Sprintf("%s.%s", dataset, table)] = &mockTable{
name: table,
dataset: dataset,
columns: columns,
}
for _, col := range columns {
if !isValidColumnName(col.Name) {
return fmt.Errorf("column name %s is not valid", col.Name)
}
}
return nil
}
func (m MockDB) QueryContext(ctx context.Context, query string, options *DBOptions, args interface{}) (DBRow, error) {
if valid, err := isSQLValid(query); err != nil && !valid {
return nil, fmt.Errorf("invalid query %s: %v", query, err)
}
return nil, nil
}
func (m MockDB) ExecContext(ctx context.Context, query string, args interface{}) error {
if valid, err := isSQLValid(query); err != nil && !valid {
return fmt.Errorf("invalid query %s: %v", query, err)
}
return nil
}
func (m MockDB) Close() {
}
var _ DB = MockDB{}
func isSQLValid(sql string) (bool, error) {
_, err := sqlparser.Parse(sql)
if err != nil {
return false, err
}
return true, nil
}
func isValidColumnName(columnName string) bool {
for _, keyword := range reservedKeywords {
if strings.ToUpper(columnName) == keyword {
return false
}
}
for _, pattern := range patterns {
matched, _ := regexp.MatchString(pattern, columnName)
if matched {
return false
}
}
return true
}