数据库操作
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.

328 lines
7.3 KiB

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