This repository has been archived by the owner on Jun 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcoscms.go
907 lines (825 loc) · 22.5 KB
/
coscms.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
package xorm
import (
stdSQL "database/sql"
"fmt"
"log"
"reflect"
"strconv"
"strings"
"time"
"github.com/coscms/xorm/core"
)
type SelectRows []*ResultSet
func (this SelectRows) GetRow() (result *ResultSet) {
if len(this) > 0 {
result = this[0]
}
return
}
func (this SelectRows) GetOne() (result string) {
results := this.GetRow()
if results != nil {
result = results.GetString(0)
}
return
}
// =====================================
// 定义ResultSet
// =====================================
func NewResultSet() *ResultSet {
return &ResultSet{
Fields: make([]string, 0),
Values: make([]*reflect.Value, 0),
NameIndex: make(map[string]int),
Length: 0,
}
}
type ResultSet struct {
Fields []string
Values []*reflect.Value
NameIndex map[string]int
Length int
}
func (r *ResultSet) Get(index int) interface{} {
if index >= r.Length {
return nil
}
return (*r.Values[index]).Interface()
}
func (r *ResultSet) GetByName(name string) interface{} {
if index, ok := r.NameIndex[name]; ok {
return r.Get(index)
}
return nil
}
func (r *ResultSet) GetString(index int) string {
if index >= r.Length {
return ""
}
str, err := reflect2value(r.Values[index])
if err != nil {
log.Println(err)
}
return str
}
func (r *ResultSet) GetStringByName(name string) string {
if index, ok := r.NameIndex[name]; ok {
return r.GetString(index)
}
return ""
}
func (r *ResultSet) GetInt64(index int) int64 {
v := r.Get(index)
if v == nil {
return 0
}
if val, ok := v.(int64); ok {
return val
}
if val, ok := v.(float64); ok {
return int64(val)
}
if val, ok := v.(int); ok {
return int64(val)
}
return 0
}
func (r *ResultSet) GetInt64ByName(name string) int64 {
if index, ok := r.NameIndex[name]; ok {
return r.GetInt64(index)
}
return 0
}
func (r *ResultSet) GetInt(index int) int {
return int(r.GetInt64(index))
}
func (r *ResultSet) GetIntByName(name string) int {
return int(r.GetInt64ByName(name))
}
func (r *ResultSet) GetFloat64(index int) float64 {
v := r.Get(index)
if v == nil {
return 0
}
if val, ok := v.([]uint8); ok {
r, _ := strconv.ParseFloat(string(val), 64)
return r
}
if val, ok := v.(float64); ok {
return val
}
if val, ok := v.(float32); ok {
return float64(val)
}
return 0
}
func (r *ResultSet) GetFloat64ByName(name string) float64 {
if index, ok := r.NameIndex[name]; ok {
return r.GetFloat64(index)
}
return 0
}
func (r *ResultSet) GetFloat32(index int) float32 {
return float32(r.GetFloat64(index))
}
func (r *ResultSet) GetFloat32ByName(name string) float32 {
return float32(r.GetFloat64ByName(name))
}
func (r *ResultSet) GetBool(index int) bool {
v := r.Get(index)
if v == nil {
return false
}
if val, ok := v.(bool); ok {
return val
}
return false
}
func (r *ResultSet) GetBoolByName(name string) bool {
if index, ok := r.NameIndex[name]; ok {
return r.GetBool(index)
}
return false
}
func (r *ResultSet) GetTime(index int) time.Time {
var t time.Time
if v := r.Get(index); v != nil {
t, _ = v.(time.Time)
}
return t
}
func (r *ResultSet) GetTimeByName(name string) time.Time {
var t time.Time
if index, ok := r.NameIndex[name]; ok {
t = r.GetTime(index)
}
return t
}
func (r *ResultSet) Set(index int, value interface{}) bool {
if index >= r.Length {
return false
}
rawValue := reflect.Indirect(reflect.ValueOf(value))
r.Values[index] = &rawValue
return true
}
func (r *ResultSet) SetByName(name string, value interface{}) bool {
if index, ok := r.NameIndex[name]; ok {
return r.Set(index, value)
}
r.NameIndex[name] = len(r.Values)
r.Fields = append(r.Fields, name)
rawValue := reflect.Indirect(reflect.ValueOf(value))
r.Values = append(r.Values, &rawValue)
r.Length = len(r.Values)
return true
}
// =====================================
// 增加Session结构体中的方法
// =====================================
func (session *Session) QueryStr(sqlStr string, params ...interface{}) ([]map[string]string, error) {
defer session.resetStatement()
if session.IsAutoClose {
defer session.Close()
}
return session.queryStr(sqlStr, params...)
}
func (session *Session) QueryRaw(sqlStr string, params ...interface{}) ([]map[string]interface{}, error) {
defer session.resetStatement()
if session.IsAutoClose {
defer session.Close()
}
return session.queryInterface(sqlStr, params...)
}
/**
* Exec a raw sql and return records as []*ResultSet
* @param string SQL
* @param ...interface{} params
* @return []*ResultSet,error
* @author AdamShen ([email protected])
*/
func (session *Session) Q(sqlStr string, params ...interface{}) (resultsSlice []*ResultSet, err error) {
defer session.resetStatement()
if session.IsAutoClose {
defer session.Close()
}
resultsSlice = make([]*ResultSet, 0)
rows, err := session.queryRows(sqlStr, params...)
if rows != nil {
if err == nil {
resultsSlice, err = rows2ResultSetSlice(rows)
}
rows.Close()
}
return
}
/**
* 逐行执行回调函数
* @param func(*core.Rows) callback callback func
* @param string sqlStr SQL
* @param ...interface{} params params
* @return error
* @author AdamShen ([email protected])
* @example
* QCallback(func(rows *core.Rows){
* if err := rows.Scan(bean); err != nil {
* return
* }
* //.....
* },"SELECT * FROM shop WHERE type=?","vip")
*/
func (session *Session) QCallback(callback func(*core.Rows, []string), sqlStr string, params ...interface{}) (err error) {
defer session.resetStatement()
if session.IsAutoClose {
defer session.Close()
}
rows, err := session.queryRows(sqlStr, params...)
if rows != nil {
defer rows.Close()
if err == nil {
var fields []string
fields, err = rows.Columns()
if err != nil {
return err
}
for rows.Next() {
callback(rows, fields)
}
}
}
return
}
func (session *Session) queryInterface(sqlStr string, params ...interface{}) (result []map[string]interface{}, err error) {
rows, err := session.queryRows(sqlStr, params...)
if err != nil {
return nil, err
}
result, err = rows2MapInterface(rows)
rows.Close()
return
}
func (session *Session) queryRows(sqlStr string, params ...interface{}) (rows *core.Rows, err error) {
session.queryPreprocess(&sqlStr, params...)
if session.IsAutoCommit {
return session.innerQueryRows(session.DB(), sqlStr, params...)
}
return session.txQueryRows(session.Tx, sqlStr, params...)
}
func (session *Session) txQueryRows(tx *core.Tx, sqlStr string, params ...interface{}) (rows *core.Rows, err error) {
rows, err = tx.Query(sqlStr, params...)
if err != nil {
return nil, err
}
return
}
func (session *Session) innerQueryRows(db *core.DB, sqlStr string, params ...interface{}) (rows *core.Rows, err error) {
stmt, rows, err := session.Engine.logSQLQueryTime(sqlStr, params, func() (*core.Stmt, *core.Rows, error) {
stmt, err := db.Prepare(sqlStr)
if err != nil {
return stmt, nil, err
}
rows, err := stmt.Query(params...)
return stmt, rows, err
})
if stmt != nil {
stmt.Close()
}
if err != nil {
return nil, err
}
return
}
// =====================================
// 增加Engine结构体中的方法
// =====================================
func (this *Engine) QueryStr(sql string, params ...interface{}) []map[string]string {
session := this.NewSession()
defer session.Close()
result, err := session.QueryStr(sql, params...)
if err != nil {
this.TLogger.Base.Error(err)
}
return result
}
func (this *Engine) QueryRaw(sql string, params ...interface{}) []map[string]interface{} {
session := this.NewSession()
defer session.Close()
result, err := session.QueryRaw(sql, params...)
if err != nil {
this.TLogger.Base.Error(err)
}
return result
}
// =======================
// 原生SQL查询
// =======================
func (this *Engine) RawQuery(sql string, params ...interface{}) (resultsSlice []*ResultSet, err error) {
session := this.NewSession()
defer session.Close()
resultsSlice, err = session.Q(sql, params...)
return
}
func (this *Engine) RawQueryCallback(callback func(*core.Rows, []string), sql string, params ...interface{}) (err error) {
session := this.NewSession()
defer session.Close()
err = session.QCallback(callback, sql, params...)
return
}
/**
* 查询键值对
*/
func (this *Engine) RawQueryKv(key string, val string, sql string, params ...interface{}) map[string]string {
results := make(map[string]string, 0)
err := this.RawQueryCallback(func(rows *core.Rows, fields []string) {
result := make(map[string]string)
StrRowProcessing(rows, fields, func(data string, index int, fieldName string) {
result[fieldName] = data
})
if k, ok := result[key]; ok {
if v, ok := result[val]; ok {
results[k] = v
}
}
}, sql, params...)
if err != nil {
this.TLogger.Base.Error(err)
}
return results
}
func (this *Engine) RawQueryAllKvs(key string, sql string, params ...interface{}) map[string][]map[string]string {
results := make(map[string][]map[string]string, 0)
err := this.RawQueryCallback(func(rows *core.Rows, fields []string) {
result := make(map[string]string)
StrRowProcessing(rows, fields, func(data string, index int, fieldName string) {
result[fieldName] = data
})
if k, ok := result[key]; ok {
if _, ok := results[k]; !ok {
results[k] = make([]map[string]string, 0)
}
results[k] = append(results[k], result)
}
}, sql, params...)
if err != nil {
this.TLogger.Base.Error(err)
}
return results
}
// -----------------------
// ResultSet结果
// -----------------------
func (this *Engine) GetRows(sql string, params ...interface{}) []*ResultSet {
sql = this.ReplaceTablePrefix(sql)
result, err := this.RawQuery(sql, params...)
if err != nil {
this.TLogger.Base.Error(err)
}
return result
}
func (this *Engine) GetRow(sql string, params ...interface{}) (result *ResultSet) {
sql = this.ReplaceTablePrefix(sql)
results, err := this.RawQuery(sql+" LIMIT 1", params...)
if err != nil {
this.TLogger.Base.Error(err)
return
}
if len(results) > 0 {
result = results[0]
}
return
}
func (this *Engine) GetOne(sql string, params ...interface{}) (result string) {
results := this.GetRow(sql, params...)
if results != nil {
result = results.GetString(0)
}
return
}
// RawSelect("*","member","id=?",1)
// RawSelect("*","member","status=? AND sex=?",1,1)
// RawSelect("*","`~member` a,`~order` b","a.status=? AND b.status=?",1,1)
func (this *Engine) RawSelect(fields string, table string, where string, params ...interface{}) SelectRows {
if fields == "" {
fields = "*"
} else {
fields = this.ReplaceTablePrefix(fields)
}
sql := `SELECT ` + fields + ` FROM ` + this.fullTableName(table) + ` WHERE ` + this.ReplaceTablePrefix(where)
if len(params) == 1 {
switch params[0].(type) {
case []interface{}:
return this.GetRows(sql, params[0].([]interface{})...)
}
}
return SelectRows(this.GetRows(sql, params...))
}
// -----------------------
// map结果
// -----------------------
func (this *Engine) RawFetchAll(fields string, table string, where string, params ...interface{}) []map[string]string {
if fields == "" {
fields = "*"
} else {
fields = this.ReplaceTablePrefix(fields)
}
sql := `SELECT ` + fields + ` FROM ` + this.fullTableName(table) + ` WHERE ` + this.ReplaceTablePrefix(where)
if len(params) == 1 {
switch params[0].(type) {
case []interface{}:
return this.RawQueryStr(sql, params[0].([]interface{})...)
}
}
return this.RawQueryStr(sql, params...)
}
func (this *Engine) RawFetch(fields string, table string, where string, params ...interface{}) (result map[string]string) {
if fields == "" {
fields = "*"
} else {
fields = this.ReplaceTablePrefix(fields)
}
sql := `SELECT ` + fields + ` FROM ` + this.fullTableName(table) + ` WHERE ` + this.ReplaceTablePrefix(where) + ` LIMIT 1`
if len(params) == 1 {
switch params[0].(type) {
case []interface{}:
results := this.RawQueryStr(sql, params[0].([]interface{})...)
if len(results) > 0 {
result = results[0]
}
return
}
}
results := this.RawQueryStr(sql, params...)
if len(results) > 0 {
result = results[0]
}
return
}
/**
* 查询基于指定字段值为键名的map
*/
func (this *Engine) RawQueryKvs(key string, sql string, params ...interface{}) map[string]map[string]string {
if key == "" {
key = "id"
}
results := make(map[string]map[string]string, 0)
err := this.RawQueryCallback(func(rows *core.Rows, fields []string) {
result := make(map[string]string)
StrRowProcessing(rows, fields, func(data string, index int, fieldName string) {
result[fieldName] = data
})
if k, ok := result[key]; ok {
results[k] = result
}
}, sql, params...)
if err != nil {
this.TLogger.Base.Error(err)
}
return results
}
/**
* RawQueryStr 查询[]map[string]string
*/
func (this *Engine) RawQueryStr(sql string, params ...interface{}) []map[string]string {
var results []map[string]string
err := this.RawQueryCallback(func(rows *core.Rows, fields []string) {
result := make(map[string]string)
StrRowProcessing(rows, fields, func(data string, index int, fieldName string) {
result[fieldName] = data
})
results = append(results, result)
}, sql, params...)
if err != nil {
this.TLogger.Base.Error(err)
}
return results
}
// -----------------------
// 写操作
// -----------------------
//RawInsert insert
func (this *Engine) RawInsert(table string, sets map[string]interface{}) (lastId int64, err error) {
var (
fields string
values string
params []interface{}
delim string
)
for k, v := range sets {
fields += delim + this.Quote(k)
values += delim + "?"
params = append(params, v)
delim = ","
}
sql := `INSERT INTO ` + this.fullTableName(table) + ` (` + fields + `) VALUES (` + values + `)`
return this.RawExec(sql, true, params...)
}
func (this *Engine) RawOnlyInsert(table string, sets map[string]interface{}) (stdSQL.Result, error) {
var (
fields string
values string
params []interface{}
delim string
)
for k, v := range sets {
fields += delim + this.Quote(k)
values += delim + "?"
params = append(params, v)
delim = ","
}
sql := `INSERT INTO ` + this.fullTableName(table) + ` (` + fields + `) VALUES (` + values + `)`
return this.RawExecr(sql, params...)
}
func (this *Engine) RawBatchInsert(table string, multiSets []map[string]interface{}) (stdSQL.Result, error) {
var (
fields string
values string
params []interface{}
length int
delim string
)
keyIdx := map[string]int{}
for i, sets := range multiSets {
var innerDelim string
values += delim + "("
if i == 0 {
idx := 0
for k, v := range sets {
keyIdx[k] = idx
fields += innerDelim + this.Quote(k)
values += innerDelim + "?"
params = append(params, v)
innerDelim = ","
idx++
}
length = idx
} else {
innerParams := make([]interface{}, length)
for k, idx := range keyIdx {
v, _ := sets[k]
values += innerDelim + "?"
innerParams[idx] = v
innerDelim = ","
}
params = append(params, innerParams...)
}
values += ")"
delim = ","
}
if values == `` {
return nil, nil
}
sql := `INSERT INTO ` + this.fullTableName(table) + ` (` + fields + `) VALUES ` + values
return this.RawExecr(sql, params...)
}
func (this *Engine) RawReplace(table string, sets map[string]interface{}) (int64, error) {
var (
fields string
values string
params []interface{}
delim string
)
for k, v := range sets {
fields += delim + this.Quote(k)
values += delim + "?"
params = append(params, v)
delim = ","
}
sql := `REPLACE INTO ` + this.fullTableName(table) + ` (` + fields + `) VALUES (` + values + `)`
return this.RawExec(sql, false, params...)
}
func (this *Engine) RawUpdate(table string, sets map[string]interface{}, where string, args ...interface{}) (int64, error) {
var (
set string
params []interface{}
delim string
)
for k, v := range sets {
set += delim + this.Quote(k) + "=?"
params = append(params, v)
delim = ","
}
if len(args) > 0 {
isAloneSlice := false
if len(args) == 1 {
switch args[0].(type) {
case []interface{}:
params = append(params, args[0].([]interface{})...)
isAloneSlice = true
}
}
if !isAloneSlice {
for _, v := range args {
params = append(params, v)
}
}
}
sql := `UPDATE ` + this.fullTableName(table) + ` SET ` + set + ` WHERE ` + where
return this.RawExec(sql, false, params...)
}
func (this *Engine) RawDelete(table string, where string, params ...interface{}) (int64, error) {
sql := `DELETE FROM ` + this.fullTableName(table) + ` WHERE ` + where
if len(params) == 1 {
switch params[0].(type) {
case []interface{}:
return this.RawExec(sql, false, params[0].([]interface{})...)
}
}
return this.RawExec(sql, false, params...)
}
func (this *Engine) RawExec(sql string, retId bool, params ...interface{}) (affected int64, err error) {
var result stdSQL.Result
result, err = this.Exec(sql, params...)
if err == nil {
if retId {
affected, err = result.LastInsertId()
} else {
affected, err = result.RowsAffected()
}
if err != nil {
this.TLogger.Base.Error(err)
}
} else {
this.TLogger.Base.Error(err)
}
return
}
func (this *Engine) RawExecr(sql string, params ...interface{}) (result stdSQL.Result, err error) {
result, err = this.Exec(sql, params...)
if err != nil {
this.TLogger.Base.Error(err)
}
return
}
func (this *Engine) ReplaceTablePrefix(sql string) (r string) {
r = strings.Replace(sql, "~", this.TablePrefix, -1)
return
}
func (this *Engine) TableName(table string) string {
return this.TablePrefix + table + this.TableSuffix
}
func (this *Engine) fullTableName(table string) string {
if table[0] != '`' && table[0] != '~' {
table = this.Quote(this.TableName(table))
}
table = this.ReplaceTablePrefix(table)
return table
}
func (this *Engine) QuoteValue(s string) string {
return "'" + AddSlashes(s) + "'"
}
func (this *Engine) QuoteKey(s string) string {
return this.Quote(s)
}
// =====================================
// 函数
// =====================================
func rows2ResultSetSlice(rows *core.Rows) (resultsSlice []*ResultSet, err error) {
fields, err := rows.Columns()
if err != nil {
return nil, err
}
for rows.Next() {
result, err := row2ResultSet(rows, fields)
if err != nil {
return nil, err
}
resultsSlice = append(resultsSlice, result)
}
return resultsSlice, nil
}
func row2ResultSet(rows *core.Rows, fields []string) (result *ResultSet, err error) {
//result := make(map[string]string)
result = NewResultSet()
err = RowProcessing(rows, fields, func(rawValue *reflect.Value, index int, fieldName string) error {
//if row is null then ignore
if (*rawValue).Interface() == nil {
return nil
}
result.NameIndex[fieldName] = len(result.Fields)
result.Fields = append(result.Fields, fieldName)
result.Values = append(result.Values, rawValue)
return nil
})
result.Length = len(result.Values)
return result, err
}
func row2Interface(rows *core.Rows, fields []string) (result map[string]interface{}, err error) {
result = make(map[string]interface{})
err = RowProcessing(rows, fields, func(rawValue *reflect.Value, index int, fieldName string) error {
//if row is null then ignore
if (*rawValue).Interface() == nil {
return nil
}
result[fieldName] = (*rawValue).Interface()
return nil
})
return
}
func rows2MapInterface(rows *core.Rows) (result []map[string]interface{}, err error) {
fields, err := rows.Columns()
if err != nil {
return nil, err
}
for rows.Next() {
ret, err := row2Interface(rows, fields)
if err != nil {
return nil, err
}
result = append(result, ret)
}
return
}
//获取一行中每一列字符串数据
func StrRowProcessing(rows *core.Rows, fields []string, fn func(data string, index int, fieldName string)) (err error) {
return RowProcessing(rows, fields, func(rawValue *reflect.Value, index int, fieldName string) error {
//if row is null then ignore
if (*rawValue).Interface() == nil {
return nil
}
if data, err := value2String(rawValue); err == nil {
fn(data, index, fieldName)
} else {
return err
}
return nil
})
}
//获取一行中每一列reflect.Value数据
func RowProcessing(rows *core.Rows, fields []string, fn func(data *reflect.Value, index int, fieldName string) error) (err error) {
length := len(fields)
scanResultContainers := make([]interface{}, length)
for i := 0; i < length; i++ {
var resultContainer interface{}
scanResultContainers[i] = &resultContainer
}
if err := rows.Scan(scanResultContainers...); err != nil {
return err
}
for ii, key := range fields {
rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
fn(&rawValue, ii, key)
}
return nil
}
//根据core.Rows来查询结果
func getResultSliceByRows(rows *core.Rows, erre error) (resultsSlice []map[string][]byte, err error) {
resultsSlice = make([]map[string][]byte, 0)
if rows != nil {
if erre == nil {
resultsSlice, err = rows2maps(rows)
}
rows.Close()
}
return
}
//替换sql中的占位符
func BuildSqlResult(sqlStr string, args interface{}) string {
if args, ok := args.([]interface{}); ok {
for _, v := range args {
val := ""
switch v.(type) {
case []interface{}:
vals := v.([]interface{})
delim := ""
for _, v := range vals {
rv := fmt.Sprintf("%v", v)
rv = AddSlashes(rv)
val += delim + "'" + rv + "'"
delim = ","
}
val = strings.Replace(val, "'", `\'`, -1)
default:
val = fmt.Sprintf("%v", v)
val = AddSlashes(val)
val = "'" + val + "'"
}
sqlStr = strings.Replace(sqlStr, "?", val, 1)
}
}
//fmt.Printf("%v\n", sqlStr)
return sqlStr
}
func AddSlashes(s string, args ...rune) string {
b := []rune{'\''}
if len(args) > 0 {
b = append(b, args...)
}
return AddCSlashes(s, b...)
}
func AddCSlashes(s string, b ...rune) string {
r := []rune{}
for _, v := range []rune(s) {
if v == '\\' {
r = append(r, '\\')
} else {
for _, f := range b {
if v == f {
r = append(r, '\\')
break
}
}
}
r = append(r, v)
}
return string(r)
}