myql操作
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.

456 lines
9.9 KiB

  1. package tencentdb
  2. import (
  3. "database/sql"
  4. "errors"
  5. "log"
  6. "strings"
  7. )
  8. /**
  9. * 准备查询
  10. * return Stmt error
  11. */
  12. func StmtForRead(dbName, table string, title string, where []string, limit map[string]string) (*sql.Stmt, error) {
  13. if dbName == "" && table == "" {
  14. return nil, errors.New("参数错误,没有数据表")
  15. }
  16. dbName = getTableName(dbName, table)
  17. if len(title) < 1 {
  18. return nil, errors.New("没有要查询内容")
  19. }
  20. var limitStr string = ""
  21. if limit != nil && len(limit) > 0 {
  22. var from, offset string = "", "" //开始
  23. if _, ok := limit["order"]; ok {
  24. limitStr += " order by " + limit["order"]
  25. }
  26. if _, ok := limit["from"]; ok {
  27. from = limit["from"]
  28. }
  29. if _, ok := limit["offset"]; ok {
  30. offset = limit["offset"]
  31. }
  32. if from != "" && offset != "" {
  33. limitStr += " limit " + from + "," + offset
  34. }
  35. }
  36. var stmt *sql.Stmt
  37. var err error
  38. if len(where) > 0 {
  39. // log.Println("SELECT " + title + " FROM " + dbName + " where " + strings.Join(where, " and ") + limitStr)
  40. stmt, err = DB.Prepare("SELECT " + title + " FROM " + dbName + " where " + strings.Join(where, " and ") + limitStr)
  41. } else {
  42. // log.Println("SELECT " + title + " FROM " + dbName + limitStr)
  43. stmt, err = DB.Prepare("SELECT " + title + " FROM " + dbName + limitStr)
  44. }
  45. return stmt, err
  46. }
  47. func CloseStmt(stmt *sql.Stmt) error {
  48. return stmt.Close()
  49. }
  50. /**
  51. * 执行查询列表
  52. * return list error
  53. */
  54. func StmtForQueryList(stmt *sql.Stmt, valuelist []interface{}) ([]map[string]string, error) {
  55. if stmt == nil {
  56. return nil, errors.New("缺少必要参数")
  57. }
  58. // log.Println(valuelist...)
  59. rows, err := stmt.Query(valuelist...)
  60. defer stmt.Close()
  61. if err != nil {
  62. if stmt != nil {
  63. stmt.Close()
  64. }
  65. return nil, err
  66. }
  67. columns, _ := rows.Columns()
  68. scanArgs := make([]interface{}, len(columns))
  69. values := make([]interface{}, len(columns))
  70. for i := range values {
  71. scanArgs[i] = &values[i]
  72. }
  73. var list []map[string]string
  74. var index string
  75. var rowerr error
  76. info := make(map[string]string)
  77. for rows.Next() {
  78. rowerr = rows.Scan(scanArgs...)
  79. info = make(map[string]string)
  80. if rowerr == nil {
  81. for i, col := range values {
  82. if col != nil {
  83. index = StrFirstToUpper(columns[i])
  84. info[index] = ToString(col)
  85. }
  86. }
  87. } else {
  88. log.Println("rows scan error", rowerr)
  89. }
  90. if len(info) > 0 {
  91. list = append(list, info)
  92. }
  93. }
  94. return list, nil
  95. }
  96. /**
  97. * 执行查询一条数据
  98. * return row error
  99. */
  100. func StmtForQueryRow(stmt *sql.Stmt, valuelist []interface{}) (map[string]string, error) {
  101. if stmt == nil || len(valuelist) < 1 {
  102. return nil, errors.New("缺少必要参数")
  103. }
  104. rows, err := stmt.Query(valuelist...)
  105. defer stmt.Close()
  106. if err != nil {
  107. if stmt != nil {
  108. stmt.Close()
  109. }
  110. return nil, err
  111. }
  112. columns, _ := rows.Columns()
  113. scanArgs := make([]interface{}, len(columns))
  114. values := make([]interface{}, len(columns))
  115. for i := range values {
  116. scanArgs[i] = &values[i]
  117. }
  118. var index string
  119. var rowerr error
  120. info := make(map[string]string)
  121. for rows.Next() {
  122. rowerr = rows.Scan(scanArgs...)
  123. if rowerr == nil {
  124. for i, col := range values {
  125. if col != nil {
  126. index = StrFirstToUpper(columns[i])
  127. info[index] = ToString(col)
  128. }
  129. }
  130. } else {
  131. log.Println("rows scan error", rowerr)
  132. }
  133. }
  134. if rowerr != nil {
  135. return info, errors.New("数据出错")
  136. }
  137. return info, nil
  138. }
  139. /**
  140. * 准备更新
  141. * return Stmt error
  142. */
  143. func StmtForUpdate(dbName, table string, data []string, where []string) (*sql.Stmt, error) {
  144. if dbName == "" && table == "" {
  145. return nil, errors.New("参数错误,没有数据表")
  146. }
  147. dbName = getTableName(dbName, table)
  148. if len(where) < 1 {
  149. return nil, errors.New("参数错误,没有更新条件")
  150. }
  151. var stmt *sql.Stmt
  152. var err error
  153. stmt, err = DB.Prepare("update " + dbName + " set " + strings.Join(data, " , ") + " where " + strings.Join(where, " and "))
  154. return stmt, err
  155. }
  156. /**
  157. * 执行更新
  158. * return is_updated error
  159. */
  160. func StmtForUpdateExec(stmt *sql.Stmt, valuelist []interface{}) (int64, error) {
  161. res, err := stmt.Exec(valuelist...)
  162. if err != nil {
  163. return 0, errors.New("更新失败:" + err.Error())
  164. }
  165. return res.RowsAffected()
  166. }
  167. /**
  168. * 准备写入
  169. * return Stmt error
  170. */
  171. func StmtForInsert(dbName, table string, data []string) (*sql.Stmt, error) {
  172. if dbName == "" && table == "" {
  173. return nil, errors.New("参数错误,没有数据表")
  174. }
  175. dbName = getTableName(dbName, table)
  176. if len(data) < 1 {
  177. return nil, errors.New("参数错误,没有要写入的数据")
  178. }
  179. var stmt *sql.Stmt
  180. var err error
  181. stmt, err = DB.Prepare("insert into " + dbName + " set " + strings.Join(data, " , "))
  182. return stmt, err
  183. }
  184. /**
  185. * 执行写入
  186. * @return lastId error
  187. */
  188. func StmtForInsertExec(stmt *sql.Stmt, valuelist []interface{}) (int64, error) {
  189. res, err := stmt.Exec(valuelist...)
  190. if err != nil {
  191. return 0, errors.New("创建失败:" + err.Error())
  192. }
  193. return res.LastInsertId()
  194. }
  195. /**
  196. * 使用db prepare方式查询列表
  197. * @param dbName
  198. * @param title 查询的字段名
  199. * @param where 查询条件
  200. * @param valuelist 查询的条件值
  201. * @param limit 查询排序
  202. * @param page 查询范围可传两个值 pageNum,pageSize
  203. * GZ
  204. * 2020/05/19
  205. */
  206. func GetListByStmt(dbName string, table string, title string, where []string, valuelist []interface{}, limit map[string]string, page ...int) ([]map[string]string, error) {
  207. if len(page) > 0 {
  208. pageNum, pageSize := page[0], 10
  209. if len(page) > 1 {
  210. pageSize = page[1]
  211. }
  212. limit["from"], limit["offset"] = GetPage(pageNum, pageSize)
  213. }
  214. stmt, err := StmtForRead(dbName, table, title, where, limit)
  215. if err != nil {
  216. return nil, err
  217. }
  218. defer stmt.Close()
  219. return StmtForQueryList(stmt, valuelist)
  220. }
  221. /**
  222. * 使用db prepare方式查询单条数据
  223. * @param dbName
  224. * @param title 查询的字段名
  225. * @param where 查询条件
  226. * @param valuelist 查询的条件值
  227. * @param limit 查询排序
  228. * GZ
  229. * 2020/05/19
  230. */
  231. func GetDataByStmt(dbName string, table string, title string, where []string, valuelist []interface{}, limit map[string]string) (map[string]string, error) {
  232. stmt, err := StmtForRead(dbName, table, title, where, limit)
  233. if err != nil {
  234. return nil, err
  235. }
  236. defer stmt.Close()
  237. return StmtForQueryRow(stmt, valuelist)
  238. }
  239. /**
  240. * 使用db prepare修改数据
  241. * @param dbName
  242. * @param title 查询的字段名
  243. * @param where 查询条件
  244. * @param valuelist 查询的条件值
  245. * @param limit 查询排序
  246. * GZ
  247. * 2020/05/19
  248. */
  249. func UpdateByStmt(dbName string, table string, data []string, where []string, valuelist []interface{}) (int64, error) {
  250. stmt, err := StmtForUpdate(dbName, table, data, where)
  251. if err != nil {
  252. return 0, err
  253. }
  254. defer stmt.Close()
  255. return StmtForUpdateExec(stmt, valuelist)
  256. }
  257. /**
  258. * 使用db prepare写入数据
  259. * @param dbName
  260. * @param table 表名
  261. * @param data 写入的字段
  262. * @param valuelist 写入的值
  263. * GZ
  264. * 2020/08/06
  265. */
  266. func InsertByStmt(dbName string, table string, data []string, valuelist []interface{}) (int64, error) {
  267. stmt, err := StmtForInsert(dbName, table, data)
  268. if err != nil {
  269. return 0, err
  270. }
  271. defer stmt.Close()
  272. return StmtForInsertExec(stmt, valuelist)
  273. }
  274. /**
  275. * 自定义查询
  276. * return Stmt error
  277. */
  278. func StmtForQuery(querysql string) (*sql.Stmt, error) {
  279. if querysql == "" {
  280. return nil, errors.New("参数错误,没有数据表")
  281. }
  282. var stmt *sql.Stmt
  283. var err error
  284. stmt, err = DB.Prepare(querysql)
  285. return stmt, err
  286. }
  287. /**
  288. * 执行自定义查询
  289. * @return lastId error
  290. */
  291. func QueryByStmt(sql string, valuelist []interface{}) ([]map[string]string, error) {
  292. stmt, err := StmtForQuery(sql)
  293. if err != nil {
  294. return nil, err
  295. }
  296. defer stmt.Close()
  297. return StmtForQueryList(stmt, valuelist)
  298. }
  299. /**
  300. * 联表查询
  301. * @param dbName
  302. * @param tableA 表一
  303. * @param tableA_alias 表一别名
  304. * @param tableB 表二
  305. * @param tableB_alias 表二别名
  306. * @param join 联表方式
  307. * @param join_on 联表字段
  308. * @param title 查询的字段名
  309. * @param where 查询条件
  310. * @param valuelist 查询的条件值
  311. * @param limit 查询排序
  312. * @param page 查询范围可传两个值 pageNum,pageSize
  313. * GZ
  314. * 2020/11/23
  315. */
  316. func GetJoinListByStmt(dbName string, tableA, tableA_alias string, tableB, tableB_alias string, join_type, join_on string, title string, where []string, valuelist []interface{}, limit map[string]string, page ...int) ([]map[string]string, error) {
  317. if len(page) > 0 {
  318. pageNum, pageSize := page[0], 10
  319. if len(page) > 1 {
  320. pageSize = page[1]
  321. }
  322. limit["from"], limit["offset"] = GetPage(pageNum, pageSize)
  323. }
  324. if tableA_alias != "" {
  325. tableA = StringJoin(dbName, ".", tableA, " as ", tableA_alias)
  326. }
  327. if tableB_alias != "" {
  328. tableB = StringJoin(dbName, ".", tableB, " as ", tableB_alias)
  329. }
  330. table := StringJoin(tableA, " ", join_type, " join ", tableB, " on ", join_on)
  331. stmt, err := StmtForRead(dbName, table, title, where, limit)
  332. if err != nil {
  333. return nil, err
  334. }
  335. defer stmt.Close()
  336. return StmtForQueryList(stmt, valuelist)
  337. }
  338. /**
  339. * 左联表查询
  340. * @param dbName
  341. * @param tableA 表一
  342. * @param tableB 表二
  343. * @param join_on 联表字段
  344. * @param title 查询的字段名
  345. * @param where 查询条件
  346. * @param valuelist 查询的条件值
  347. * @param limit 查询排序
  348. * @param page 查询范围可传两个值 pageNum,pageSize
  349. * GZ
  350. * 2021/1/27
  351. */
  352. func LeftJoinListByStmt(dbName string, tableA, tableB string, join_on string, title string, where []string, valuelist []interface{}, limit map[string]string, page ...int) ([]map[string]string, error) {
  353. if len(page) > 0 {
  354. pageNum, pageSize := page[0], 10
  355. if len(page) > 1 {
  356. pageSize = page[1]
  357. }
  358. limit["from"], limit["offset"] = GetPage(pageNum, pageSize)
  359. }
  360. tableA = getTableName(dbName, tableA)
  361. tableB = getTableName(dbName, tableB)
  362. table := StringJoin(tableA, " left join ", tableB, " on ", join_on)
  363. stmt, err := StmtForRead(dbName, table, title, where, limit)
  364. if err != nil {
  365. return nil, err
  366. }
  367. defer stmt.Close()
  368. return StmtForQueryList(stmt, valuelist)
  369. }