-
Notifications
You must be signed in to change notification settings - Fork 0
/
datatable.go
executable file
·260 lines (234 loc) · 6.14 KB
/
datatable.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package database
import (
"fmt"
"strings"
utl "github.com/tdanic/libcore/utility"
)
type (
/*DataTable : maneja un crud completo y genera script automaticos*/
DataTable struct {
table string
rows []StData
index []string
}
)
/*NewDataTable : Crea un datable correctamente*/
func NewDataTable(table string, rows []StData, index []string) DataTable {
var data DataTable
data.SetTable(table)
data.AddIndexs(index...)
data.AddRows(rows...)
return data
}
/*ValidRow : valida si tiene filas llenadas*/
func (p *DataTable) ValidRow() bool {
return utl.ReturnIf(len(p.rows) > 0, true, false).(bool)
}
/*GetCols : Obtiene el nombre de las columnas de cada fila*/
func (p *DataTable) GetCols() ([]string, error) {
var cols []string
item, err := p.GetRow(1)
if err != nil {
return cols, err
}
cols = item.KeyColum()
if len(cols) <= 0 {
return cols, utl.Msj.GetError("DT04")
}
return cols, nil
}
/*GetIndex : Obtiene el nombre de los indices*/
func (p *DataTable) GetIndex() []string {
return p.index
}
/*GetTable : Obtiene el nombre de la tabla*/
func (p *DataTable) GetTable() string {
return p.table
}
/*SetTable : Modifica el nombre de la tabla*/
func (p *DataTable) SetTable(table string) {
p.table = strings.ToUpper(utl.Trim(table))
}
/*LenRows : Obtienen la cantidad de fila*/
func (p *DataTable) LenRows() int {
return len(p.rows)
}
/*LenIndex : Obtienen la cantidad de llaves primarias para update o delete*/
func (p *DataTable) LenIndex() int {
return len(p.index)
}
/*AddIndexs : agrega varias claves primarias*/
func (p *DataTable) AddIndexs(cols ...string) error {
for _, col := range cols {
err := p.AddIndex(col)
if err != nil {
return err
}
}
return nil
}
/*AddRows : Agrega varias fila */
func (p *DataTable) AddRows(rows ...StData) {
for _, item := range rows {
p.AddRow(item)
}
}
/*AddRow : Agrega una fila */
func (p *DataTable) AddRow(row StData) {
p.rows = append(p.rows, row.UpperKey())
}
/*AddIndex : agrega una llave para los delete o update*/
func (p *DataTable) AddIndex(col string) error {
col = strings.ToUpper(col)
item, err := p.GetRow(1)
if err != nil {
return err
}
if !item.ValidColum(col) {
return utl.Msj.GetError("DT02")
}
p.index = append(p.index, col)
return nil
}
/*GetRow : Obtiene una fila X por el numero de fila*/
func (p *DataTable) GetRow(row int) (StData, error) {
if p.LenRows() < row {
return StData{}, utl.Msj.GetError("DT01")
}
return p.rows[(row - 1)], nil
}
/*GetRows : Obtiene todas las fila*/
func (p *DataTable) GetRows() []StData {
return p.rows
}
/*GenSQL : genera acciones de base de datos mediante los siguientes comando INSERT,UPDATE,DELETE*/
func (p *DataTable) GenSQL(accion string) ([]StQuery, error) {
switch accion {
case INSERT:
return p.GenInserts()
case DELETE:
return p.GenDeletes()
case UPDATE:
return p.GenUpdates()
default:
return nil, utl.Msj.GetError("DT07")
}
}
/*GenInserts : genera insert masivos para modificaciones de base de datos*/
func (p *DataTable) GenInserts() ([]StQuery, error) {
var queries []StQuery
clone := *p
sqltemp, err := sqldinamic(clone, INSERT)
if err != nil {
return queries, err
}
for _, quirie := range p.GetRows() {
queries = append(queries, StQuery{
Querie: sqltemp,
Args: quirie,
})
}
return queries, nil
}
/*GenDeletes : genera los delete masivos para modificaciones de base de datos*/
func (p *DataTable) GenDeletes() ([]StQuery, error) {
var queries []StQuery
clone := *p
sqltemp, err := sqldinamic(clone, DELETE)
if err != nil {
return queries, err
}
cols, _ := p.GetCols()
colsnew := utl.FilterExcl(cols, clone.GetIndex())
for _, quirie := range p.GetRows() {
queries = append(queries, StQuery{
Querie: sqltemp,
Args: quirie.Filter(colsnew...),
})
}
return queries, nil
}
/*GenUpdates : genera los update masivos para modificaciones de base de datos de ante mano tener que colocar indices*/
func (p *DataTable) GenUpdates() ([]StQuery, error) {
var queries []StQuery
clone := *p
sqltemp, err := sqldinamic(clone, UPDATE)
if err != nil {
return queries, err
}
for _, quirie := range p.GetRows() {
queries = append(queries, StQuery{
Querie: sqltemp,
Args: quirie,
})
}
return queries, nil
}
/*sqldinamic : genera los sql temporales para los crud*/
func sqldinamic(data DataTable, acc string) (string, error) {
table := utl.Trim(data.GetTable())
if table == "" {
return "", utl.Msj.GetError("DT03")
}
cols, err := data.GetCols()
if err != nil {
return "", err
}
if !utl.ValidDuplidArrayStr(cols) {
return "", utl.Msj.GetError("DT05")
}
if utl.InStr(acc, UPDATE, DELETE) && data.LenIndex() <= 0 {
return "", utl.Msj.GetError("DT06")
}
switch acc {
case INSERT:
sqltmp := sqlinsert(table, cols)
return sqltmp, nil
case UPDATE:
sqltmp := sqlupdate(table, cols, data.GetIndex())
return sqltmp, nil
case DELETE:
sqltmp := sqldelete(table, data.GetIndex())
return sqltmp, nil
default:
return "", nil
}
}
func sqldelete(table string, indices []string) string {
sqltmp := fmt.Sprintf("DELETE FROM %s", table)
sqltmp = sqlConditional(sqltmp, indices)
return sqltmp
}
func sqlupdate(table string, cols []string, indices []string) string {
sqltmp := fmt.Sprintf("UPDATE %s SET", table)
values := utl.FilterExcl(cols, indices)
for i, item := range values {
ind := (len(values) - 1)
sqltmp = fmt.Sprintf("%s %s = :%s%s", sqltmp, item, item, utl.ReturnIf(i == ind, "", " ,").(string))
}
sqltmp = sqlConditional(sqltmp, indices)
return sqltmp
}
func sqlConditional(sqltmp string, indices []string) string {
sqltmp = fmt.Sprintf("%s WHERE ", sqltmp)
for i, item := range indices {
ind := (len(indices) - 1)
sqltmp = fmt.Sprintf("%s %s = :%s%s", sqltmp, item, item, utl.ReturnIf(i == ind, "", " AND").(string))
}
return sqltmp
}
func sqlinsert(table string, cols []string) string {
sqltmp := fmt.Sprintf("INSERT INTO %s (", table)
for i, item := range cols {
if i == (len(cols) - 1) {
sqltmp = fmt.Sprintf("%s%s) VALUES(", sqltmp, item)
} else {
sqltmp = fmt.Sprintf("%s%s,", sqltmp, item)
}
}
for i, item := range cols {
ind := (len(cols) - 1)
sqltmp = fmt.Sprintf("%s:%s%s", sqltmp, item, utl.ReturnIf(i == ind, ")", ",").(string))
}
return sqltmp
}