-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsqlabble.go
More file actions
92 lines (78 loc) · 2.16 KB
/
sqlabble.go
File metadata and controls
92 lines (78 loc) · 2.16 KB
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
package sqlabble
import (
"database/sql"
"github.com/sqlabble/sqlabble/builder"
"github.com/sqlabble/sqlabble/stmt"
)
// Methods exported to make statements.
var (
CreateTable = stmt.NewCreateTable
CreateTableIfNotExists = stmt.NewCreateTableIfNotExists
Select = stmt.NewSelect
SelectDistinct = stmt.NewSelectDistinct
InsertInto = stmt.NewInsertInto
Update = stmt.NewUpdate
Delete = stmt.NewDelete
SimpleCase = stmt.NewSimpleCase
SimpleWhen = stmt.NewSimpleWhen
SearchedCase = stmt.NewSearchedCase
SearchedWhen = stmt.NewSearchedWhen
Column = stmt.NewColumn
Table = stmt.NewTable
Val = stmt.NewVal
Vals = stmt.NewVals
Subquery = stmt.NewSubquery
Assign = stmt.NewAssign
And = stmt.NewAnd
Or = stmt.NewOr
Not = stmt.NewNot
Exists = stmt.NewExists
NotExists = stmt.NewNotExists
Union = stmt.NewUnion
UnionAll = stmt.NewUnionAll
Intersect = stmt.NewIntersect
IntersectAll = stmt.NewIntersectAll
Except = stmt.NewExcept
ExceptAll = stmt.NewExceptAll
Wildcard = stmt.NewWildcard()
Count = stmt.NewCount
Max = stmt.NewMax
Add = stmt.NewAdd
Sub = stmt.NewSub
Mul = stmt.NewMul
Div = stmt.NewDiv
IntegerDiv = stmt.NewIntegerDiv
Mod = stmt.NewMod
IFNULL = stmt.NewIFNULL
)
// Session wraps sql.DB
type Session struct {
DBInterface
Builder *builder.Builder
}
// DBInterface *sql.DB or *sql.Tx
type DBInterface interface {
Query(query string, args ...interface{}) (*sql.Rows, error)
QueryRow(query string, args ...interface{}) *sql.Row
Exec(query string, args ...interface{}) (sql.Result, error)
}
// NewSession create a Session instance
func NewSession(builderName string, db DBInterface) (*Session, error) {
var b *builder.Builder
switch builderName {
case "standard":
b = &builder.Standard
case "standard_indented":
b = &builder.StandardIndented
case "mysql":
b = &builder.MySQL
case "mysql_indented":
b = &builder.MySQLIndented
default:
return nil, builder.NewErrBuilderNotSupported(builderName)
}
return &Session{
DBInterface: db,
Builder: b,
}, nil
}