-
Notifications
You must be signed in to change notification settings - Fork 2
/
datawrap_utils.go
131 lines (97 loc) · 2.37 KB
/
datawrap_utils.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
// Title:
//
// Description:
//
// Author:black
//
// Createtime:2014-01-06 18:01
//
// Version:1.0
//
// 修改历史:版本号 修改日期 修改人 修改说明
//
// 1.0 2014-01-06 18:01 black 创建文档
package lessgo
import (
"database/sql"
"github.com/hjqhezgh/commonlib"
"math"
)
//获取数据库总页数
func GetTotalPage(pageSize int, db *sql.DB, sql string, params []interface{}) (totalPage, totalNum int, err error) {
rows, err := db.Query(sql, params...)
if err != nil {
Log.Error(err.Error())
return 0, 0, err
}
if rows.Next() {
err := rows.Scan(&totalNum)
if err != nil {
Log.Error(err.Error())
return 0, 0, err
}
}
totalPage = int(math.Ceil(float64(totalNum) / float64(pageSize)))
return totalPage, totalNum, nil
}
func GetFillObjectPage(db *sql.DB, sql string, currPageNo, pageSize, totalNum int, params []interface{}) (*commonlib.TraditionPage, error) {
rows, err := db.Query(sql, params...)
if err != nil {
Log.Error(err.Error())
return nil, err
}
objects := []interface{}{}
columns, err := rows.Columns()
if err != nil {
Log.Error(err.Error())
return nil, err
}
for rows.Next() {
model := new(Model)
fillObjects := []interface{}{}
fillObjects = append(fillObjects, &model.Id)
for index, column := range columns {
if index > 0 { //第一个列必须是id
prop := new(Prop)
prop.Name = column
prop.Value = ""
fillObjects = append(fillObjects, &prop.Value)
model.Props = append(model.Props, prop)
}
}
err = commonlib.PutRecord(rows, fillObjects...)
if err != nil {
Log.Error(err.Error())
return nil, err
}
objects = append(objects, model)
}
return commonlib.BulidTraditionPage(currPageNo, pageSize, totalNum, objects), nil
}
func GetDataMap(rows *sql.Rows) (map[string]string, error) {
type TmpString struct {
Value string
}
dataMap := make(map[string]string)
columns, err := rows.Columns()
if err != nil {
Log.Error(err.Error())
return nil, err
}
objects := []*TmpString{}
tmpString := []interface{}{}
for i := 0; i < len(columns); i++ {
var tmp = new(TmpString)
objects = append(objects, tmp)
tmpString = append(tmpString, &tmp.Value)
}
err = commonlib.PutRecord(rows, tmpString...)
if err != nil {
Log.Error(err.Error())
return nil, err
}
for index, column := range columns {
dataMap[column] = objects[index].Value
}
return dataMap, nil
}