/
item.go
187 lines (149 loc) · 3.72 KB
/
item.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright 2020 The SealABC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package simpleSQLDatabase
import (
"reflect"
"strconv"
)
type ITable interface {
Name() string
Columns() []string
ColumnsForInsert() []string
FieldForInsert() []string
FieldsToColumns([]string) []string
NewRows() interface{}
}
type BasicTable struct {
columns []string
columnsForInsert []string
insertField []string
Instance interface{}
}
func (b BasicTable) Name() string {
return ""
}
func (b BasicTable) Columns() (list []string) {
if b.Instance == nil {
return
}
if len(b.columns) == 0 {
list, _ = ColumnsFromTag(b.Instance, false, nil)
b.columns = list
}
return b.columns
}
func (b BasicTable) FieldsToColumns(fields []string) []string {
list, _ := ColumnsFromTag(b.Instance, false, fields)
return list
}
func (b BasicTable) ColumnsForInsert() (list []string) {
if b.Instance == nil {
return
}
if len(b.columnsForInsert) == 0 {
list, _ = ColumnsFromTag(b.Instance, true, nil)
b.columnsForInsert = list
}
return b.columnsForInsert
}
func (b BasicTable) FieldForInsert() (fields []string){
if b.Instance == nil {
return
}
if len(b.insertField) == 0 {
fields, _ = FieldsFromLocalTable(b.Instance, true)
b.insertField = fields
}
return b.insertField
}
type IRows interface {
Table() ITable
Count() int
DataForInsert() []interface{}
Data(requiredColumns []string) []interface{}
}
type BasicRows struct {
Rows []interface{}
Instance IRows
}
func (b BasicRows) Count() int {
return len(b.Rows)
}
func (b BasicRows) Data(requiredField []string) (data []interface{}) {
if requiredField == nil {
return
}
for _, d := range b.Rows {
list := StringDataFromRow(d, requiredField)
data = append(data, list...)
}
return
}
func (b BasicRows) DataForInsert() (data []interface{}) {
if b.Instance == nil {
return
}
table := b.Instance.Table()
if table == nil {
return
}
fieldForInsert := table.FieldForInsert()
for _, d := range b.Rows {
list := StringDataFromRow(d, fieldForInsert)
data = append(data, list...)
}
return
}
type SimplePagingQueryParam struct {
Page int64
Count int64
Table string
RowType interface{}
Condition string
ConditionArgs []interface{}
}
func (s *SimplePagingQueryParam) PageFromString(page string) {
s.Page = 0
if page == "" {
return
}
p, err := strconv.ParseInt(page, 10, 64)
if err != nil {
return
}
s.Page = p
}
type SimplePagingQueryResult struct {
Total uint64
Rows interface{}
}
func NewRowsInstance(base interface{}) (rows interface{}) {
rowsType := reflect.TypeOf(base)
newRows := reflect.New(rowsType).Elem()
if !newRows.CanAddr() {
return
}
field := newRows.FieldByName("Instance")
if !field.IsValid() {
return
}
if !field.CanSet() {
return
}
field.Set(newRows.Addr())
return newRows.Interface()
}