This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
model.go
96 lines (81 loc) · 2.6 KB
/
model.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
package gomodel
type (
// Model represent a database model mapping to a table
Model interface {
// Table return database table name that the model mapped
Table() string
// Vals store values of fields to given slice, the slice has length,
// just setup value by index. The value order MUST same as fields defined
// in strcuture
Vals(fields uint64, vals []interface{})
// Ptrs is similar to Vals, but for field pointers
Ptrs(fields uint64, ptrs []interface{})
}
// Columner is a optional interface for Model, if Model implements this interface,
// it's no need to parse Model info with reflection
Columner interface {
// Columns return all column names for this Model
Columns() []string
}
// Nocacher is a optional interface for Model, if Model implements this interface,
// and NoCache method return true, it will not allocate memory to store
// sql, stmt, columns for this Model, all sqls, stmts must be stored in DB instance.
//
// If Nocache, the only methods to get Stmt are DB.StmtById and Tx.PrepareById,
// implements it only when you actually know what are you do.
Nocacher interface {
Nocache() bool
}
)
func NumFields(n uint64) int {
n -= (n >> 1) & 0x5555555555555555
n = (n>>2)&0x3333333333333333 + n&0x3333333333333333
n += n >> 4
n &= 0x0f0f0f0f0f0f0f0f
n *= 0x0101010101010101
return int(n >> 56)
}
func Fields(fields ...uint64) uint64 {
var f uint64
for _, field := range fields {
f |= field
}
return f
}
// NumFieldsExcp create fieldset except given fields
func NumFieldsExcp(numField uint64, fields ...uint64) uint64 {
return AllFieldsExcp(1<<numField-1, fields...)
}
// AllFieldsExcp create fieldset except given fields
func AllFieldsExcp(allFields uint64, fields ...uint64) uint64 {
return allFields & ^Fields(fields...)
}
func AllFieldsOrSome(allFields uint64, fields ...uint64) uint64 {
f := Fields(fields...)
if f != 0 {
return f
}
return allFields
}
// FieldVals will extract values of fields from model, and concat with remains
// arguments
func FieldVals(model Model, fields uint64, args ...interface{}) []interface{} {
c, l := NumFields(fields), len(args)
vals := make([]interface{}, c+l)
model.Vals(fields, vals)
for l = l - 1; l >= 0; l-- {
vals[c+l] = args[l]
}
return vals
}
// FieldPtrs is similar to FieldVals, but for field pointers.
// FieldPtrs only used for query operations such as One, Limit, All.
func FieldPtrs(model Model, fields uint64, args ...interface{}) []interface{} {
c, l := NumFields(fields), len(args)
ptrs := make([]interface{}, c+l)
model.Ptrs(fields, ptrs)
for l = l - 1; l >= 0; l-- {
ptrs[c+l] = args[l]
}
return ptrs
}