-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
68 lines (57 loc) · 1.52 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"net/url"
"strings"
"github.com/actiontech/sqle/sqle/driver"
adaptor "github.com/actiontech/sqle/sqle/pkg/driver"
_ "github.com/denisenkom/go-mssqldb"
)
type MssqlDialector struct {}
func (d *MssqlDialector) Dialect(dsn *driver.DSN) (string, string) {
u := &url.URL{
Scheme: "sqlserver",
User: url.UserPassword(dsn.User, dsn.Password),
Host: fmt.Sprintf("%s:%s", dsn.Host, dsn.Port),
}
if dsn.DatabaseName != "" {
query := url.Values{}
query.Add("database", dsn.DatabaseName)
u.RawQuery = query.Encode()
}
return u.Scheme, u.String()
}
func (d *MssqlDialector) String() string {
return "SQL Server"
}
func (d *MssqlDialector) ShowDatabaseSQL() string {
return "select name from sys.databases where name not in ('master','tempdb','model','msdb','distribution')"
}
var version string
var printVersion = flag.Bool("version", false, "Print version & exit")
func main() {
flag.Parse()
if *printVersion {
fmt.Println(version)
return
}
plugin := adaptor.NewAdaptor(&MssqlDialector{})
ruleDQL1 := &driver.Rule{
Name: "ms_dql_1",
Desc: "禁止不带where条件的查询",
Category: "DQL规范",
Level: driver.RuleLevelError,
}
ruleDQL1Handler := func(ctx context.Context, rule *driver.Rule, sql string) (string, error) {
lowerSql := strings.ToLower(sql)
if strings.Contains(lowerSql, "select") &&
!strings.Contains(lowerSql,"where") {
return rule.Desc, nil
}
return "", nil
}
plugin.AddRule(ruleDQL1, ruleDQL1Handler)
plugin.Serve()
}