query.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package db
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "genBrief/common/log"
  6. "strings"
  7. )
  8. type Query struct {
  9. db *sql.DB
  10. table string
  11. attr string
  12. where string
  13. from uint32
  14. to uint32
  15. order string
  16. quote string
  17. args []interface{}
  18. rows *sql.Rows
  19. }
  20. func New(table string) *Query {
  21. quote := "`"
  22. if strings.Contains(table, " ") {
  23. quote = ""
  24. }
  25. return &Query{db: p1.Db, table: table, quote: quote, args: []interface{}{}}
  26. }
  27. func (sl *Query) LeftJoin(format string, args ...interface{}) *Query {
  28. sl.table += " LEFT JOIN " + format
  29. sl.args = append(sl.args, args...)
  30. sl.quote = ""
  31. return sl
  32. }
  33. func (sl *Query) Match(attrs, value string) *Query {
  34. sl.init_where()
  35. //sl.where += " MATCH(" + attrs + ") AGAINST(?)"
  36. //sl.where += " MATCH(" + attrs + ") AGAINST(? IN BOOLEAN MODE)"
  37. sl.where += " MATCH(" + attrs + ") AGAINST(? IN NATURAL LANGUAGE MODE)"
  38. sl.args = append(sl.args, value)
  39. return sl
  40. }
  41. func (sl *Query) Locate(attrs, value string) *Query {
  42. sl.init_where()
  43. sl.where += fmt.Sprintf(" LOCATE(?,%v)>0 ", attrs)
  44. sl.args = append(sl.args, value)
  45. return sl
  46. }
  47. func (sl *Query) Attr(attr string) *Query {
  48. sl.attr = attr
  49. return sl
  50. }
  51. func (sl *Query) init_where() {
  52. if sl.where == "" {
  53. sl.where = " where "
  54. } else {
  55. sl.where += " and "
  56. }
  57. }
  58. func (sl *Query) Where(attr string, value interface{}) *Query {
  59. sl.init_where()
  60. sl.where += sl.quote + attr + sl.quote + "=?"
  61. sl.args = append(sl.args, value)
  62. return sl
  63. }
  64. func (sl *Query) WhereF(attr string, value ...interface{}) *Query {
  65. sl.init_where()
  66. sl.where += attr
  67. sl.args = append(sl.args, value...)
  68. return sl
  69. }
  70. func (sl *Query) Like(attr string, value interface{}) *Query {
  71. sl.init_where()
  72. sl.where += sl.quote + attr + sl.quote + " like ?"
  73. sl.args = append(sl.args, "%"+value.(string)+"%")
  74. return sl
  75. }
  76. func (sl *Query) In(attr string, param ...interface{}) *Query {
  77. if len(param) < 1 {
  78. return sl
  79. }
  80. sl.init_where()
  81. what := "?"
  82. for i := 1; i < len(param); i++ {
  83. what += ",?"
  84. }
  85. sl.where += sl.quote + attr + sl.quote + " in (" + what + ")"
  86. sl.args = append(sl.args, param...)
  87. return sl
  88. }
  89. func (sl *Query) Order(order string) *Query {
  90. sl.order = order
  91. return sl
  92. }
  93. func (sl *Query) Limit(from, to uint32) *Query {
  94. sl.from = from
  95. sl.to = to
  96. return sl
  97. }
  98. func (sl *Query) GetRow() *sql.Row {
  99. //sqlString := "select " + sl.attr + " from " + sl.table + sl.where + " " + sl.order + " limit 1"
  100. sqlString := fmt.Sprintf("SELECT %v FROM %v %v %v LIMIT 1", sl.attr, sl.table, sl.where, sl.order)
  101. row := sl.db.QueryRow(sqlString, sl.args...)
  102. return row
  103. }
  104. func (sl *Query) GetAll() (tableData []map[string]interface{}, err error) {
  105. //sqlString := "select " + sl.attr + " from " + sl.table + sl.where + sl.order
  106. sqlString := fmt.Sprintf("SELECT %v FROM %v %v %v", sl.attr, sl.table, sl.where, sl.order)
  107. // limit 语句
  108. if sl.to > sl.from {
  109. sqlString += " limit ?,?"
  110. sl.args = append(sl.args, sl.from, sl.to)
  111. }
  112. var rows *sql.Rows
  113. if rows, err = sl.db.Query(sqlString, sl.args...); err != nil {
  114. log.Info("getData", err.Error(), sqlString)
  115. return
  116. }
  117. if tableData, err = FetchData(rows); err != nil {
  118. log.Info("getData error", err.Error(), sqlString)
  119. }
  120. return
  121. }
  122. func (sl *Query) GetOne() (data map[string]interface{}, err error) {
  123. //sqlString := "select " + sl.attr + " from " + sl.table + sl.where + sl.order
  124. sqlString := fmt.Sprintf("SELECT %v FROM %v %v %v", sl.attr, sl.table, sl.where, sl.order)
  125. // limit 语句
  126. sqlString += " limit 1"
  127. rows, err := sl.db.Query(sqlString, sl.args...)
  128. if err != nil {
  129. log.Info("getData", err.Error(), sqlString)
  130. return
  131. }
  132. return FetchOne(rows)
  133. }
  134. func (sl *Query) GetWithTotal() (tableData []map[string]interface{}, total int) {
  135. sqltotal := "select count(1) from " + sl.table + sl.where
  136. log.Debugf("sqltotal:%v", sqltotal)
  137. err := sl.db.QueryRow(sqltotal, sl.args...).Scan(&total)
  138. if err != nil {
  139. log.Info("gettotal", err.Error(), sqltotal)
  140. return nil, 0
  141. }
  142. //sqlString := "select " + sl.attr + " from " + sl.table + sl.where + sl.order + " limit ?,?"
  143. sqlString := fmt.Sprintf("SELECT %v FROM %v %v %v LIMIT ?,?", sl.attr, sl.table, sl.where, sl.order)
  144. sl.args = append(sl.args, sl.from, sl.to)
  145. log.Debugf("sqlString:%v", sqlString)
  146. rows, err := sl.db.Query(sqlString, sl.args...)
  147. if err != nil {
  148. log.Info("getData", err.Error(), sqlString)
  149. return nil, 0
  150. }
  151. tableData, _ = FetchData(rows)
  152. return tableData, total
  153. }
  154. func SqlOne(sqlString string, args ...interface{}) (map[string]interface{}, error) {
  155. rows, err := Pool().Db.Query(sqlString, args...)
  156. if err != nil {
  157. log.Warnf("SqlOne[%s] err:%v", sqlString, err)
  158. return nil, err
  159. }
  160. return FetchOne(rows)
  161. }
  162. func SqlAll(sqlString string, args ...interface{}) []map[string]interface{} {
  163. tableData := make([]map[string]interface{}, 0)
  164. rows, err := Pool().Db.Query(sqlString, args...)
  165. if err != nil {
  166. log.Warnf("SqlAll[%s] err:%v", sqlString, err)
  167. return tableData
  168. }
  169. tableData, err = FetchData(rows)
  170. if err != nil {
  171. log.Warnf("SqlAll[%s] err:%v", sqlString, err)
  172. return tableData
  173. }
  174. return tableData
  175. }