-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathrelation.go
153 lines (128 loc) · 2.94 KB
/
relation.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
package ar
import (
"database/sql"
"fmt"
"reflect"
"strings"
"time"
"unicode"
"github.com/monochromegane/argen/query"
"github.com/monochromegane/goban"
)
type Relation struct {
*query.Select
db *sql.DB
logger *Logger
}
func NewRelation(db *sql.DB, logger *Logger) *Relation {
return &Relation{
Select: &query.Select{},
db: db,
logger: logger,
}
}
func (r *Relation) Table(table string) *Relation {
r.Select.Table(table)
return r
}
func (r *Relation) Columns(columns ...string) *Relation {
r.Select.Columns(columns...)
return r
}
func (r *Relation) GetColumns() []string {
return r.Select.GetColumns()
}
func (r *Relation) Where(cond string, args ...interface{}) *Relation {
r.Select.Where(cond, args...)
return r
}
func (r *Relation) And(cond string, args ...interface{}) *Relation {
return r.Where(cond, args...)
}
func (r *Relation) OrderBy(column, order string) *Relation {
r.Select.OrderBy(column, order)
return r
}
func (r *Relation) Limit(limit int) *Relation {
r.Select.Limit(limit)
return r
}
func (r *Relation) Offset(offset int) *Relation {
r.Select.Offset(offset)
return r
}
func (r *Relation) GroupBy(group string, groups ...string) *Relation {
r.Select.GroupBy(group, groups...)
return r
}
func (r *Relation) Having(cond string, args ...interface{}) *Relation {
r.Select.Having(cond, args...)
return r
}
func (r *Relation) Build() (string, []interface{}) {
return r.Select.Build()
}
func (r *Relation) Count(column ...string) int {
c := "*"
if len(column) > 0 {
c = column[0]
}
var count int
if err := r.Columns(fmt.Sprintf("COUNT(%s)", c)).QueryRow(&count); err != nil {
return 0
}
return count
}
func (r *Relation) Exists() bool {
var one int
if err := r.Columns("1").Limit(1).QueryRow(&one); err == sql.ErrNoRows {
return false
}
return true
}
func (r *Relation) Explain() error {
r.Select.Explain()
rows, err := r.Query()
if err != nil {
return err
}
defer rows.Close()
columns, _ := rows.Columns()
var values [][]string
for rows.Next() {
vals := make([]string, len(columns))
ptrs := make([]interface{}, len(columns))
for i := range vals {
ptrs[i] = &vals[i]
}
rows.Scan(ptrs...)
values = append(values, vals)
}
goban.Render(columns, values)
return nil
}
func (r *Relation) Query() (*sql.Rows, error) {
q, b := r.Build()
defer r.log(time.Now(), q, b...)
return r.db.Query(q, b...)
}
func (r *Relation) QueryRow(dest ...interface{}) error {
q, b := r.Build()
defer r.log(time.Now(), q, b...)
return r.db.QueryRow(q, b...).Scan(dest...)
}
func (r *Relation) log(t time.Time, sql string, args ...interface{}) {
r.logger.Print(time.Now().Sub(t), sql, args)
}
func IsZero(v interface{}) bool {
return reflect.ValueOf(v).Interface() == reflect.Zero(reflect.TypeOf(v)).Interface()
}
func ToCamelCase(s string) string {
var camel string
for _, split := range strings.Split(s, "_") {
c := []rune(split)
c[0] = unicode.ToUpper(c[0])
camel += string(c)
}
return camel
}