-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
81 lines (74 loc) · 1.78 KB
/
client.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
package ssql
import (
"context"
"database/sql"
_ "github.com/lib/pq"
)
type Options struct {
// Struct tag to look for "sql" field names in.
Tag string
// If not set (0), logging will be disabled
LogLevel int
// If not set, the default logger will be used
Logger *Logger
// Main default pagination sort config. It can be overriden
// by Find query options.
MainSortField string
MainSortDirection string
}
// Client provides common methods for using SQL.
type Client struct {
db *sql.DB
tag string
mainSortField string
mainSortDirection string
l *log
}
// NewClient creates a new SQL client using the database connection and give options.
func NewClient(ctx context.Context, db *sql.DB, options *Options) (*Client, error) {
tag := "sql"
var logger Logger = &defaultLogger{}
logLevel := 0
if options != nil {
if options.Tag != "" {
tag = options.Tag
}
if options.Logger != nil {
logger = *options.Logger
}
if options.LogLevel != 0 {
logLevel = options.LogLevel
}
}
ret := Client{
db: db,
tag: tag,
mainSortField: options.MainSortField,
mainSortDirection: options.MainSortDirection,
l: &log{
logger: &logger,
level: logLevel,
},
}
ret.l.info("SQL client created successfully")
return &ret, nil
}
const (
// Used by query options and the API.
DirectionDesc = "DESC"
DirectionAsc = "ASC"
OPEqual = "="
OPNotEqual = "<>"
OPGreater = ">"
OPLess = "<"
OPGreatorOrEqual = ">="
OPLessOrEqual = "<="
OPLogicalIn = "IN"
// Only used internally.
opLogicalAnd = "AND"
// OPLogicalOr = "OR"
// OPLogicalNot = "NOT"
// OPLogicalLike = "LIKE"
// OPLogicalExists = "EXISTS"
// OPLogicalBetween = "BETWEEN"
)