/
sql_table.go
129 lines (111 loc) · 3.28 KB
/
sql_table.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
package mdb
import (
"fmt"
"github.com/mabetle/mcore"
)
// RemoveTableRow removes table row with id.
func (s Sql) RemoveTableRow(table string, id interface{}) error {
sql := GetRemoveRowSql(table)
_, err := s.Exec(sql, id)
CheckErrorWithSucceedMsg(err, "Remove row succeed. Table:%s ID:%v", table, id)
return err
}
// IsTableHasID checks if table has specific id
func (s Sql) IsTableHasID(table string, id interface{}) bool {
sql := GetIsHasIDSql(table)
return s.IsQueryHasRows(sql)
}
// IsTableQueryHasRows checks table query has rows
func (s Sql) IsTableQueryHasRows(table, query string, args ...interface{}) (r bool) {
sql := "select count(*) from " + table + " "
sql = BuildWhereQuery(sql, query)
return s.IsQueryHasRows(sql, args...)
}
// IsTableHasRowsByColumns checks table has
// example IsTableHasRowsByColumns(table, []string{"OrgName","Flag"}, "一局")
// TODO check
func (s Sql) IsTableHasRowsByColumns(table string, columns []string, values ...interface{}) (b bool) {
if !IsValidTableName(table) {
return
}
where := s.BuildColumnsWhere(columns)
q := fmt.Sprintf("select count(*) from %s %s", table, where)
rows, _ := s.QueryForInt(q, values...)
return rows > 0
}
// ClearTable clear table rows
func (s Sql) ClearTable(table string) error {
sql := GetClearTableSql(table)
_, err := s.Exec(sql)
return err
}
// ClearTables clears tables rows one by one
func (s Sql) ClearTables(tables ...string) error {
errs := mcore.NewErrors()
for _, v := range tables {
e := s.ClearTable(v)
errs.Record(e)
}
return errs.Error()
}
// DropTable drops table
func (s Sql) DropTable(table string) error {
sql := GetDropTableSql(table)
_, err := s.Exec(sql)
return err
}
// DropTables drops tables
func (s Sql) DropTables(tables ...string) error {
errs := mcore.NewErrors()
for _, v := range tables {
e := s.DropTable(v)
errs.Record(e)
}
return errs.Error()
}
// CountTableRows returns table rows num.
func (s Sql) CountTableRows(table string) (int64, error) {
sql := GetCountRowsSql(table)
return s.QueryForInt(sql)
}
// IsExistTable checks table exists.
func (s Sql) IsExistTable(table string) bool {
q := "select count(*) from " + table
_, err := s.Query(q)
return logger.CheckError(err)
}
// IsExistColumn checks table column exists.
func (s Sql) IsExistColumn(table, column string) (r bool) {
columns, err := s.GetTableColumns(table)
if err != nil {
return false
}
return mcore.String(column).IsInArrayIgnoreCase(columns)
}
// CountTableColumns returns table rows number.
func (s Sql) CountTableColumns(table string) int {
cols, err := s.GetTableColumns(table)
if err != nil {
return -1
}
return len(cols)
}
// GetTableColumns returns table column names
func (s Sql) GetTableColumns(table string) ([]string, error) {
sql := GetCountColumnsSql(table)
return s.GetQueryColumns(sql)
}
// IsTableHasColumn equals to IsExistColumn
func (s Sql) IsTableHasColumn(table string, column string) bool {
return s.IsExistColumn(table, column)
}
// GetTableRowsJSONData gets table rows JSON data
func (s Sql) GetTableRowsJSONData(table string) map[string]string {
sql := GetSelectAllSql(table)
return s.QueryForJSONData(sql)
}
// GetTableRowsMap
func (s Sql) GetTableRowsMap(table string) ([]map[string]interface{}, []string, error) {
q := GetSelectAllSql(table)
return s.QueryForMaps(q)
}