/
connect.go
174 lines (148 loc) · 4.39 KB
/
connect.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
package audit
import (
"context"
"database/sql"
"fmt"
"log"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
"go.mongodb.org/mongo-driver/mongo"
)
var (
MysqlCreate = "CREATE TABLE IF NOT EXISTS %s (id bigint unsigned auto_increment primary key, actor_id bigint unsigned null, table_row_id bigint unsigned null,table_name varchar(255) null,action varchar(10) null,old_values longtext collate utf8mb4_bin null,new_values longtext collate utf8mb4_bin null,http_method varchar(11) null,url text null,ip_address text null,user_agent text null,created_at datetime null,constraint new_values check (json_valid(new_values)),constraint old_values check (json_valid(old_values)));"
MysqlInsert = "INSERT INTO %s (actor_id, table_row_id, table_name, action, old_values, new_values, http_method, url, ip_address, user_agent, created_at) VALUES(?,?,?,?,?,?,?,?,?,?,?)"
MysqlSelect = "SELECT * FROM %s WHERE %v %s ?"
PostgresCreate = "CREATE TABLE IF NOT EXISTS %s (id bigserial constraint audits_pk primary key, actor_id bigserial, table_row_id bigserial, table_name text, action varchar(11), old_values json, new_values json, http_method varchar(11), url text, ip_address text, user_agent text, created_at timestamp with time zone);"
PostgresInsert = "INSERT INTO %s (actor_id, table_row_id, table_name, action, old_values, new_values, http_method, url, ip_address, user_agent, created_at) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)"
PostgresSelect = "SELECT * FROM %s WHERE %v %s $1" // todo: support IN operator
)
type Auditor struct {
auditTableName string
tableException []string
store
}
type query struct {
insert string
create string
selectStmt string
}
type store struct {
dbType string
sql *sql.DB
mongo *mongo.Client
query
parser *Parser
internal *sql.DB
}
var (
ErrInvalidQuery = fmt.Errorf("invalid query")
ErrDriverNotSupported = fmt.Errorf("driver is not supported")
)
const (
MysqlDB string = "mysql"
PostgresDB string = "postgres"
MongoDB string = "mongo"
)
func (a *store) newPostgresAuditor(internal *sql.DB, db *sql.DB) error {
_, err := db.ExecContext(context.Background(), a.query.create)
if err != nil {
return err
}
a.dbType = PostgresDB
a.parser = NewParser(a.dbType)
postgres := &PostgresParser{
internal: internal,
db: db,
query: a.query,
}
a.parser.PostgresParser = postgres
return err
}
func (a *store) newMysqlAuditor(internal *sql.DB, db *sql.DB) error {
_, err := internal.ExecContext(context.Background(), a.query.create)
if err != nil {
return err
}
a.dbType = MysqlDB
a.parser = NewParser(a.dbType)
mysql := &MysqlParser{
internal: internal,
db: db,
query: a.query,
}
a.parser.MysqlParser = mysql
return err
}
//func (a *store) newMongoAuditor(kv *mongo.Client) error {
// a.dbType = MongoDB
// a.mongo = kv
//
// return nil
//}
type DBOption func(*Auditor)
func (a *Auditor) SetDB(opts ...DBOption) error {
for _, opt := range opts {
opt(a)
}
switch a.store.dbType {
case MysqlDB:
fallthrough
case PostgresDB:
return nil
default:
return ErrDriverNotSupported
}
}
func (a *Auditor) GetTableName(query string) (tableName string, err error) {
if a.store.parser == nil {
return "", nil
}
switch a.store.dbType {
case "mysql":
return a.parser.MysqlParser.getTableName(query)
case "postgres":
return a.parser.PostgresParser.getTableName(query)
default:
return "", ErrDriverNotSupported
}
}
func Postgres(db *sql.DB, dsn string) DBOption {
return func(a *Auditor) {
internal, err := sql.Open("postgres", dsn)
if err != nil {
log.Fatal(err)
}
a.store.internal = internal
q := query{
insert: fmt.Sprintf(PostgresInsert, a.auditTableName),
create: fmt.Sprintf(PostgresCreate, a.auditTableName),
selectStmt: PostgresSelect,
}
a.store.query = q
a.store.sql = db
err = a.newPostgresAuditor(a.store.internal, a.store.sql)
if err != nil {
log.Fatal(err)
}
}
}
func MySql(db *sql.DB, dsn string) DBOption {
return func(a *Auditor) {
internal, err := sql.Open("mysql", dsn)
if err != nil {
log.Fatal(err)
}
a.store.internal = internal
q := query{
insert: fmt.Sprintf(MysqlInsert, a.auditTableName),
create: fmt.Sprintf(MysqlCreate, a.auditTableName),
selectStmt: MysqlSelect,
}
a.store.query = q
a.store.sql = db
err = a.newMysqlAuditor(a.store.internal, a.store.sql)
if err != nil {
log.Fatal(err)
}
}
}