数据库操作
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
6.8 KiB

3 years ago
  1. /**
  2. * DB transaction
  3. */
  4. package dbquery
  5. import (
  6. "database/sql"
  7. "errors"
  8. "log"
  9. "strings"
  10. "time"
  11. )
  12. /**
  13. * 创建数据
  14. */
  15. func TxInsert(tx *sql.Tx, dbname, table string, data map[string]string) (int64, error) {
  16. var insertId int64
  17. var err error
  18. if dbname == "" && table == "" {
  19. return 0, errors.New("参数错误,没有数据表")
  20. }
  21. dbName := getTableName(dbname, table)
  22. if len(data) < 1 {
  23. return 0, errors.New("参数错误,没有要写入的数据")
  24. }
  25. keyList := make([]string, len(data))
  26. keyStr := make([]string, len(data))
  27. valueList := make([]interface{}, len(data))
  28. var i int = 0
  29. for key, value := range data {
  30. keyList[i] = key
  31. keyStr[i] = "?"
  32. valueList[i] = value
  33. i++
  34. }
  35. result, err := tx.Exec("insert into "+dbName+" ("+strings.Join(keyList, ",")+") value("+strings.Join(keyStr, ",")+")", valueList...)
  36. if err != nil {
  37. log.Println("ERROR", "insert into ", dbName, "error:", err)
  38. return insertId, err
  39. } else {
  40. insertId, _ = result.LastInsertId()
  41. time.Sleep(time.Second * 2)
  42. return insertId, nil
  43. }
  44. }
  45. /**
  46. * 准备写入
  47. * return Stmt error
  48. */
  49. func TxPreInsert(tx *sql.Tx, dbname, table string, data map[string]interface{}) (int64, error) {
  50. if dbname == "" && table == "" {
  51. return 0, errors.New("params error,no db or table")
  52. }
  53. dbName := getTableName(dbname, table)
  54. if len(data) < 1 {
  55. return 0, errors.New("params error,no data to insert")
  56. }
  57. var err error
  58. var stmt *sql.Stmt
  59. var field []string = make([]string, len(data))
  60. var valuelist []interface{} = make([]interface{}, len(data))
  61. var i int = 0
  62. for key, item := range data {
  63. field[i] = key + "=?"
  64. valuelist[i] = item
  65. i++
  66. }
  67. sql := "insert into " + dbName + " set " + strings.Join(field, " , ")
  68. stmt, err = tx.Prepare(sql)
  69. if err != nil {
  70. log.Println("insert prepare error:", sql, err)
  71. return 0, errors.New("insert prepare error:" + err.Error())
  72. }
  73. result, err := stmt.Exec(valuelist...)
  74. if err != nil {
  75. log.Println("insert exec error:", sql, valuelist, err)
  76. return 0, errors.New("insert exec error:" + err.Error())
  77. }
  78. insertId, _ := result.LastInsertId()
  79. return insertId, nil
  80. }
  81. /**
  82. * 修改数据
  83. */
  84. func TxUpdate(tx *sql.Tx, dbname, table string, data map[string]string, where map[string]string) (int64, error) {
  85. var rowsAffected int64
  86. var err error
  87. if dbname == "" && table == "" {
  88. return rowsAffected, errors.New("参数错误,没有数据表")
  89. }
  90. dbName := getTableName(dbname, table)
  91. if len(data) < 1 {
  92. return rowsAffected, errors.New("参数错误,没有要写入的数据")
  93. }
  94. if len(where) < 1 {
  95. return rowsAffected, errors.New("参数错误,没有修改条件")
  96. }
  97. keyList := make([]string, len(data))
  98. valueList := make([]interface{}, len(data), len(data)+len(where))
  99. whereStr := make([]string, len(where))
  100. var i int = 0
  101. empty := false
  102. for key, value := range data {
  103. keyList[i] = key + "=?"
  104. valueList[i] = value
  105. i++
  106. }
  107. i = 0
  108. for key, value := range where {
  109. if value == "" {
  110. empty = true
  111. break
  112. }
  113. whereStr[i] = key + "=?"
  114. valueList = append(valueList, value)
  115. i++
  116. }
  117. if empty {
  118. log.Println("ERROR", "update", dbName, "error, params empty")
  119. return rowsAffected, errors.New("params empty")
  120. }
  121. result, err := tx.Exec("update "+dbName+" set "+strings.Join(keyList, " , ")+" where "+strings.Join(whereStr, " and "), valueList...)
  122. if err != nil {
  123. log.Println("ERROR", "update", dbName, "error:", err)
  124. return rowsAffected, err
  125. } else {
  126. rowsAffected, _ = result.RowsAffected()
  127. return rowsAffected, nil
  128. }
  129. }
  130. /**
  131. * 准备更新
  132. * return Stmt error
  133. */
  134. func TxPreUpdate(tx *sql.Tx, dbname, table string, data []string, where []string, valuelist []interface{}) (int64, error) {
  135. if dbname == "" && table == "" {
  136. return 0, errors.New("params error,no db or table")
  137. }
  138. dbName := getTableName(dbname, table)
  139. if len(where) < 1 {
  140. return 0, errors.New("params error, no data for update")
  141. }
  142. var err error
  143. var stmt *sql.Stmt
  144. sql := "update " + dbName + " set " + strings.Join(data, " , ") + " where " + strings.Join(where, " and ")
  145. stmt, err = tx.Prepare(sql)
  146. if err != nil {
  147. log.Println("update prepare error:", sql, err)
  148. return 0, errors.New("update prepare error:" + err.Error())
  149. }
  150. res, err := stmt.Exec(valuelist...)
  151. if err != nil {
  152. log.Println("update exec error:", sql, valuelist, err)
  153. return 0, errors.New("update exec error:" + err.Error())
  154. }
  155. return res.RowsAffected()
  156. }
  157. /**
  158. * 删除数据
  159. * @param count 删除数量
  160. */
  161. func TxDelete(tx *sql.Tx, dbname, table string, where map[string]string, del_count ...string) (int64, error) {
  162. var count int64
  163. var err error
  164. if dbname == "" && table == "" {
  165. return count, errors.New("参数错误,没有数据表")
  166. }
  167. dbName := getTableName(dbname, table)
  168. if len(where) < 1 {
  169. return count, errors.New("参数错误,没有删除条件")
  170. }
  171. keyList := make([]string, len(where))
  172. valueList := make([]interface{}, len(where))
  173. var i int = 0
  174. empty := false
  175. for key, value := range where {
  176. if value == "" {
  177. empty = true
  178. break
  179. }
  180. keyList[i] = key + "=?"
  181. valueList[i] = value
  182. i++
  183. }
  184. if empty {
  185. log.Println("ERROR", "delete from", dbName, "error, where:", where)
  186. return count, errors.New("params empty")
  187. }
  188. var limitStr string = ""
  189. if len(del_count) > 0 {
  190. limitStr = " limit " + del_count[0]
  191. }
  192. result, err := tx.Exec("delete from "+dbName+" where "+strings.Join(keyList, " and ")+limitStr, valueList...)
  193. if err != nil {
  194. log.Println("ERROR", "delete from", dbName, "error:", err)
  195. return count, err
  196. } else {
  197. count, _ = result.RowsAffected()
  198. return count, nil
  199. }
  200. }
  201. /**
  202. * 准备查询
  203. * return Stmt error
  204. */
  205. func TxForRead(tx *sql.Tx, dbName, table string, title string, where []string) (*sql.Stmt, error) {
  206. if dbName == "" && table == "" {
  207. return nil, errors.New("参数错误,没有数据表")
  208. }
  209. dbName = getTableName(dbName, table)
  210. if len(title) < 1 {
  211. return nil, errors.New("没有要查询内容")
  212. }
  213. var stmt *sql.Stmt
  214. var err error
  215. if len(where) > 0 {
  216. // log.Println("SELECT " + title + " FROM " + dbName + " where " + strings.Join(where, " and ") + " FOR UPDATE")
  217. stmt, err = tx.Prepare("SELECT " + title + " FROM " + dbName + " where " + strings.Join(where, " and ") + " FOR UPDATE")
  218. } else {
  219. // log.Println("SELECT " + title + " FROM " + dbName + " FOR UPDATE")
  220. stmt, err = tx.Prepare("SELECT " + title + " FROM " + dbName + " FOR UPDATE")
  221. }
  222. return stmt, err
  223. }
  224. /**
  225. * 使用db prepare方式查询单条数据
  226. * @param dbName
  227. * @param title 查询的字段名
  228. * @param where 查询条件
  229. * @param valuelist 查询的条件值
  230. * @param limit 查询排序
  231. * GZ
  232. * 2020/05/19
  233. */
  234. func TxGetData(tx *sql.Tx, dbName string, table string, title string, where []string, valuelist []interface{}) (map[string]string, error) {
  235. stmt, err := TxForRead(tx, dbName, table, title, where)
  236. if err != nil {
  237. return nil, err
  238. }
  239. defer stmt.Close()
  240. return StmtForQueryRow(stmt, valuelist)
  241. }