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

955 lines
20 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package dbquery
  2. import (
  3. "database/sql"
  4. "log"
  5. "strconv"
  6. "errors"
  7. "strings"
  8. "time"
  9. "git.tetele.net/tgo/helper"
  10. _ "github.com/go-sql-driver/mysql"
  11. )
  12. /**
  13. * 创建数据
  14. */
  15. func Insert(dbName, table string, data map[string]string) (int64, error) {
  16. var insertId int64
  17. var err error
  18. if dbName == "" && table == "" {
  19. return insertId, errors.New("没有数据表")
  20. }
  21. if strings.Contains(table, "select ") {
  22. dbName = table
  23. } else {
  24. dbName = getTableName(dbName, table)
  25. }
  26. if len(data) < 1 {
  27. return insertId, errors.New("没有要写入的数据")
  28. }
  29. keyList := make([]string, len(data))
  30. keyStr := make([]string, len(data))
  31. valueList := make([]interface{}, len(data))
  32. var i int = 0
  33. for key, value := range data {
  34. keyList[i] = key
  35. keyStr[i] = "?"
  36. valueList[i] = value
  37. i++
  38. }
  39. result, err := DB.Exec("insert into "+dbName+" ("+strings.Join(keyList, ",")+") value("+strings.Join(keyStr, ",")+")", valueList...)
  40. if err != nil {
  41. log.Println("ERROR|插入", dbName, "数据失败,", err)
  42. return insertId, err
  43. } else {
  44. insertId, _ = result.LastInsertId()
  45. time.Sleep(time.Second * 2)
  46. return insertId, nil
  47. }
  48. }
  49. /**
  50. * 修改数据
  51. */
  52. func Update(dbName, table string, data map[string]string, where map[string]string) (int64, error) {
  53. var rowsAffected int64
  54. var err error
  55. if dbName == "" && table == "" {
  56. return rowsAffected, errors.New("没有数据表")
  57. }
  58. if strings.Contains(table, "select ") {
  59. dbName = table
  60. } else {
  61. dbName = getTableName(dbName, table)
  62. }
  63. if len(data) < 1 {
  64. return rowsAffected, errors.New("同有更新的数据")
  65. }
  66. if len(where) < 1 {
  67. return rowsAffected, errors.New("没有更新条件")
  68. }
  69. keyList := make([]string, len(data))
  70. valueList := make([]interface{}, len(data), len(data)+len(where))
  71. whereStr := make([]string, len(where))
  72. var i int = 0
  73. empty := false
  74. for key, value := range data {
  75. keyList[i] = key + "=?"
  76. valueList[i] = value
  77. i++
  78. }
  79. i = 0
  80. for key, value := range where {
  81. if value == "" {
  82. empty = true
  83. break
  84. }
  85. whereStr[i] = key + "=?"
  86. valueList = append(valueList, value)
  87. i++
  88. }
  89. if empty {
  90. log.Println("ERROR|修改数据表", dbName, "时条件中有空数据,条件:", where, "数据:", data)
  91. return rowsAffected, errors.New("条件中有空数据")
  92. }
  93. result, err := DB.Exec("update "+dbName+" set "+strings.Join(keyList, " , ")+" where "+strings.Join(whereStr, " and "), valueList...)
  94. if err != nil {
  95. log.Println("ERROR|修改", dbName, "数据失败,", err)
  96. return rowsAffected, err
  97. } else {
  98. rowsAffected, _ = result.RowsAffected()
  99. return rowsAffected, nil
  100. }
  101. }
  102. /**
  103. * 删除数据
  104. * @param count 删除数量
  105. */
  106. func Delete(dbName, table string, data map[string]string, del_count ...string) (int64, error) {
  107. var count int64
  108. var err error
  109. if dbName == "" && table == "" {
  110. return count, errors.New("没有数据表")
  111. }
  112. if strings.Contains(table, "select ") {
  113. dbName = table
  114. } else {
  115. dbName = getTableName(dbName, table)
  116. }
  117. if len(data) < 1 {
  118. return count, errors.New("没有要删除的数据")
  119. }
  120. keyList := make([]string, len(data))
  121. valueList := make([]interface{}, len(data))
  122. var i int = 0
  123. empty := false
  124. for key, value := range data {
  125. if value == "" {
  126. empty = true
  127. break
  128. }
  129. keyList[i] = key + "=?"
  130. valueList[i] = value
  131. i++
  132. }
  133. if empty {
  134. log.Println("ERROR|删除数据表", dbName, "时条件中有空数据,条件:", data)
  135. return count, errors.New("条件中有空数据")
  136. }
  137. var limitStr string = ""
  138. if len(del_count) > 0 {
  139. limitStr = " limit " + del_count[0]
  140. }
  141. result, err := DB.Exec("delete from "+dbName+" where "+strings.Join(keyList, " and ")+limitStr, valueList...)
  142. if err != nil {
  143. log.Println("ERROR|删除", dbName, "数据失败,", err)
  144. return count, err
  145. } else {
  146. count, _ = result.RowsAffected()
  147. return count, nil
  148. }
  149. }
  150. /**
  151. * 查找一条记录
  152. * @param dbName 数据表名
  153. * @param title 查询字段名
  154. */
  155. func GetData(dbName, table string, title string, where map[string]string, limit map[string]string) (int, map[string]string, error) {
  156. var count int = 0
  157. info := make(map[string]string)
  158. if dbName == "" && table == "" {
  159. return count, info, errors.New("没有数据表")
  160. }
  161. dbName = getTableName(dbName, table)
  162. if len(title) < 1 {
  163. return count, info, errors.New("没有指定查询内容")
  164. }
  165. var limitStr string = ""
  166. if limit != nil && len(limit) > 0 {
  167. var from string = "0" //开始
  168. if _, ok := limit["order"]; ok {
  169. limitStr += " order by " + limit["order"]
  170. }
  171. if _, ok := limit["from"]; ok {
  172. from = limit["from"]
  173. }
  174. limitStr += " limit " + from + ",1"
  175. } else {
  176. limitStr = " limit 1"
  177. }
  178. if len(where) < 1 {
  179. return count, info, errors.New("Query condition is empty")
  180. }
  181. keyList := make([]string, len(where))
  182. valueList := make([]interface{}, len(where))
  183. var i int = 0
  184. empty := false
  185. for key, value := range where {
  186. if value == "" {
  187. empty = true
  188. break
  189. }
  190. keyList[i] = key + " = ? "
  191. valueList[i] = value
  192. i++
  193. }
  194. if empty {
  195. return count, info, errors.New("Query condition is empty")
  196. }
  197. var rows *sql.Rows
  198. var err error
  199. var queryNum int = 0
  200. for queryNum < 3 { //如发生错误,继续查询3次,防止数据库连接断开问题
  201. rows, err = DB.Query("SELECT "+title+" FROM "+dbName+" where "+strings.Join(keyList, " and ")+" "+limitStr, valueList...)
  202. if err == nil {
  203. break
  204. } else {
  205. log.Println("ERROR", "Query from", dbName, "failed,", err, "| SELECT "+title+" FROM "+dbName+" where "+strings.Join(keyList, " and ")+" "+limitStr)
  206. time.Sleep(time.Millisecond * 500)
  207. }
  208. queryNum++
  209. }
  210. defer rows.Close()
  211. if err != nil {
  212. return count, info, err
  213. }
  214. columns, _ := rows.Columns()
  215. scanArgs := make([]interface{}, len(columns))
  216. values := make([]interface{}, len(columns))
  217. for i := range values {
  218. scanArgs[i] = &values[i]
  219. }
  220. var index string
  221. var rowerr error
  222. for rows.Next() {
  223. rowerr = rows.Scan(scanArgs...)
  224. if rowerr == nil {
  225. for i, col := range values {
  226. if col != nil {
  227. index = helper.StrFirstToUpper(columns[i])
  228. info[index] = helper.ToString(col)
  229. }
  230. }
  231. count++
  232. } else {
  233. log.Println("ERROR", "rows scan error", rowerr, dbName, keyList, valueList)
  234. }
  235. }
  236. if rowerr != nil {
  237. return count, info, rowerr
  238. }
  239. return count, info, nil
  240. }
  241. /**
  242. * 查找一条记录
  243. * @param dbName 数据表名
  244. * @param title 查询字段名
  245. */
  246. func GetRow(dbName, table_name, alias string, titles string, join [][]string, where, where_or []string, valueList []interface{}, orderby, groupby string, debug bool) (int, map[string]string, error) {
  247. var count int = 0
  248. info := make(map[string]string)
  249. if dbName == "" && table_name == "" {
  250. return count, info, errors.New("没有数据表")
  251. }
  252. table := ""
  253. if strings.Contains(table_name, "select ") {
  254. table = table_name
  255. } else {
  256. table = getTableName(dbName, table_name)
  257. }
  258. var sql_str, title string
  259. if titles != "" {
  260. title = titles
  261. } else {
  262. title = "*"
  263. }
  264. sql_str = helper.StringJoin("/*slave*/ select ", title)
  265. if alias != "" {
  266. table = helper.StringJoin(table, " as ", alias)
  267. }
  268. sql_str = helper.StringJoin(sql_str, " from ", table)
  269. if len(join) > 0 {
  270. for _, joinitem := range join {
  271. if len(joinitem) < 2 {
  272. continue
  273. }
  274. if len(joinitem) == 3 {
  275. sql_str = helper.StringJoin(sql_str, " ", joinitem[2], " join ", getTableName(dbName, joinitem[0]), " on ", joinitem[1])
  276. } else { //默认左连接
  277. sql_str = helper.StringJoin(sql_str, " left join ", getTableName(dbName, joinitem[0]), " on ", joinitem[1])
  278. }
  279. }
  280. }
  281. if len(where) > 0 || len(where_or) > 0 {
  282. sql_str = helper.StringJoin(sql_str, " where ")
  283. }
  284. if len(where) > 0 {
  285. sql_str = helper.StringJoin(sql_str, " (", strings.Join(where, " and "), " ) ")
  286. }
  287. if len(where_or) > 0 {
  288. if len(where) > 0 {
  289. sql_str = helper.StringJoin(sql_str, " or ", strings.Join(where_or, " or "))
  290. } else {
  291. sql_str = helper.StringJoin(sql_str, strings.Join(where_or, " or "))
  292. }
  293. }
  294. if groupby != "" {
  295. sql_str = helper.StringJoin(sql_str, " group by ", groupby)
  296. }
  297. if orderby != "" {
  298. sql_str = helper.StringJoin(sql_str, " order by ", orderby)
  299. }
  300. if debug {
  301. log.Println("query sql:", sql_str, valueList)
  302. }
  303. condition_len := 0 //所有条件数
  304. for _, ch2 := range sql_str {
  305. if string(ch2) == "?" {
  306. condition_len++
  307. }
  308. }
  309. if condition_len != len(valueList) {
  310. return 0, nil, errors.New("参数错误,条件值错误")
  311. }
  312. var rows *sql.Rows
  313. var err error
  314. var queryNum int = 0
  315. sql_str = helper.StringJoin(sql_str, " limit 1")
  316. var db *sql.DB
  317. if SLAVER_DB != nil {
  318. db = SLAVER_DB
  319. } else {
  320. db = DB
  321. }
  322. for queryNum < 2 { //如发生错误,继续查询2次,防止数据库连接断开问题
  323. rows, err = db.Query(sql_str, valueList...)
  324. if err == nil {
  325. break
  326. } else {
  327. log.Println(err)
  328. time.Sleep(time.Millisecond * 500)
  329. }
  330. queryNum++
  331. }
  332. if err != nil {
  333. rows.Close()
  334. return count, info, err
  335. }
  336. columns, _ := rows.Columns()
  337. scanArgs := make([]interface{}, len(columns))
  338. values := make([]interface{}, len(columns))
  339. for i := range values {
  340. scanArgs[i] = &values[i]
  341. }
  342. var index string
  343. var rowerr error
  344. for rows.Next() {
  345. rowerr = rows.Scan(scanArgs...)
  346. if rowerr == nil {
  347. for i, col := range values {
  348. if col != nil {
  349. index = helper.StrFirstToUpper(columns[i])
  350. info[index] = helper.ToString(col)
  351. }
  352. }
  353. count++
  354. } else {
  355. log.Println("ERROR", rowerr)
  356. }
  357. }
  358. rows.Close()
  359. if rowerr != nil {
  360. return count, info, rowerr
  361. }
  362. return count, info, nil
  363. }
  364. /**
  365. * 查找多条记录
  366. * @param dbName 数据表名
  367. * @param title 查询字段名
  368. */
  369. func FetchRows(dbName, table_name, alias string, titles string, join [][]string, where, where_or []string, valueList []interface{}, orderby, groupby string, page int, page_size int, debug bool) (int, []map[string]string, error) {
  370. var count int = 0
  371. list := make([]map[string]string, 0)
  372. if dbName == "" && table_name == "" {
  373. return count, list, errors.New("没有数据表")
  374. }
  375. table := ""
  376. if strings.Contains(table_name, "select ") {
  377. table = table_name
  378. } else {
  379. table = getTableName(dbName, table_name)
  380. }
  381. var sql_str, title string
  382. if titles != "" {
  383. title = titles
  384. } else {
  385. title = "*"
  386. }
  387. sql_str = helper.StringJoin("/*slave*/ select ", title)
  388. if alias != "" {
  389. table = helper.StringJoin(table, " as ", alias)
  390. }
  391. sql_str = helper.StringJoin(sql_str, " from ", table)
  392. if len(join) > 0 {
  393. for _, joinitem := range join {
  394. if len(joinitem) < 2 {
  395. continue
  396. }
  397. if len(joinitem) == 3 {
  398. sql_str = helper.StringJoin(sql_str, " ", joinitem[2], " join ", getTableName(dbName, joinitem[0]), " on ", joinitem[1])
  399. } else { //默认左连接
  400. sql_str = helper.StringJoin(sql_str, " left join ", getTableName(dbName, joinitem[0]), " on ", joinitem[1])
  401. }
  402. }
  403. }
  404. if len(where) > 0 || len(where_or) > 0 {
  405. sql_str = helper.StringJoin(sql_str, " where ")
  406. }
  407. if len(where) > 0 {
  408. sql_str = helper.StringJoin(sql_str, " (", strings.Join(where, " and "), " ) ")
  409. }
  410. if len(where_or) > 0 {
  411. if len(where) > 0 {
  412. sql_str = helper.StringJoin(sql_str, " or ", strings.Join(where_or, " or "))
  413. } else {
  414. sql_str = helper.StringJoin(sql_str, strings.Join(where_or, " or "))
  415. }
  416. }
  417. if groupby != "" {
  418. sql_str = helper.StringJoin(sql_str, " group by ", groupby)
  419. }
  420. if orderby != "" {
  421. sql_str = helper.StringJoin(sql_str, " order by ", orderby)
  422. }
  423. if page > 0 || page_size > 0 {
  424. if page < 1 {
  425. page = 1
  426. }
  427. if page_size < 1 {
  428. page_size = 10
  429. }
  430. from := strconv.Itoa((page - 1) * page_size)
  431. offset := strconv.Itoa(page_size)
  432. if from != "" && offset != "" {
  433. sql_str = helper.StringJoin(sql_str, " limit ", from, " , ", offset)
  434. }
  435. }
  436. if debug {
  437. log.Println("query sql:", sql_str, valueList)
  438. }
  439. condition_len := 0 //所有条件数
  440. for _, ch2 := range sql_str {
  441. if string(ch2) == "?" {
  442. condition_len++
  443. }
  444. }
  445. if condition_len != len(valueList) {
  446. return 0, list, errors.New("参数错误,条件值错误")
  447. }
  448. var db *sql.DB
  449. if SLAVER_DB != nil {
  450. db = SLAVER_DB
  451. } else {
  452. db = DB
  453. }
  454. var rows *sql.Rows
  455. var err error
  456. var queryNum int = 0
  457. for queryNum < 2 { //如发生错误,继续查询2次,防止数据库连接断开问题
  458. rows, err = db.Query(sql_str, valueList...)
  459. if err == nil {
  460. break
  461. } else {
  462. log.Println(err)
  463. time.Sleep(time.Millisecond * 500)
  464. }
  465. queryNum++
  466. }
  467. if err != nil {
  468. rows.Close()
  469. return 0, list, err
  470. }
  471. columns, _ := rows.Columns()
  472. scanArgs := make([]interface{}, len(columns))
  473. values := make([]interface{}, len(columns))
  474. for i := range values {
  475. scanArgs[i] = &values[i]
  476. }
  477. var index string
  478. var rowerr error
  479. var info map[string]string
  480. for rows.Next() {
  481. rowerr = rows.Scan(scanArgs...)
  482. info = make(map[string]string)
  483. if rowerr == nil {
  484. for i, col := range values {
  485. if col != nil {
  486. index = helper.StrFirstToUpper(columns[i])
  487. info[index] = helper.ToString(col)
  488. }
  489. }
  490. count++
  491. } else {
  492. log.Println("ERROR", rowerr)
  493. }
  494. if len(info) > 0 {
  495. list = append(list, info)
  496. }
  497. }
  498. rows.Close()
  499. return count, list, nil
  500. }
  501. func GetInfo(dbName, table string, title string, where map[string]string) (map[string]string, error) {
  502. count, info, gzErr := GetData(dbName, table, title, where, nil)
  503. if gzErr != nil {
  504. return info, gzErr
  505. } else {
  506. if count < 1 {
  507. return info, errors.New("No data")
  508. }
  509. return info, nil
  510. }
  511. }
  512. /**
  513. * 查询列表
  514. * 2018/04/19
  515. */
  516. func GetList(dbName, table string, title string, where map[string]string, limit map[string]string) ([]map[string]string, error) {
  517. var list []map[string]string
  518. if dbName == "" && table == "" {
  519. return list, errors.New("没有数据表")
  520. }
  521. if strings.Contains(table, "select ") {
  522. dbName = table
  523. } else {
  524. dbName = getTableName(dbName, table)
  525. }
  526. var rows *sql.Rows
  527. var err error
  528. var queryNum int = 0
  529. var limitStr string = ""
  530. if len(limit) > 0 {
  531. var offset string = "0" //偏移量,个数
  532. var from string = "" //开始
  533. if _, ok := limit["order"]; ok {
  534. limitStr += " order by " + limit["order"]
  535. }
  536. if _, ok := limit["offset"]; ok {
  537. offset = limit["offset"]
  538. }
  539. if _, ok := limit["from"]; ok {
  540. from = limit["from"]
  541. }
  542. if offset != "0" && from != "" {
  543. limitStr += " limit " + from + "," + offset
  544. }
  545. }
  546. if len(where) > 0 {
  547. valueList := make([]interface{}, len(where))
  548. whereStr := make([]string, len(where))
  549. i := 0
  550. var keys []string
  551. for key, value := range where {
  552. key = strings.Trim(key, " ")
  553. value = strings.Trim(value, " ")
  554. if value == "" || key == "" {
  555. continue
  556. // return list, errors.New("Query condition is empty")
  557. }
  558. if strings.Contains(key, " ") {
  559. //key中包含空格,判断是否不等于条件
  560. keys = strings.Split(key, " ")
  561. if helper.IsInStringArray(judg(), strings.Trim(keys[1], " ")) {
  562. whereStr[i] = strings.Trim(keys[0], " ") + " " + strings.Trim(keys[1], " ") + " ?"
  563. }
  564. } else {
  565. whereStr[i] = key + " = ?"
  566. valueList[i] = value
  567. }
  568. i++
  569. }
  570. if len(whereStr) == 0 || len(valueList) == 0 {
  571. return list, errors.New("Query condition is empty")
  572. }
  573. for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
  574. rows, err = DB.Query("select "+title+" from "+dbName+" where "+strings.Join(whereStr, " and ")+" "+limitStr, valueList...)
  575. if err == nil {
  576. break
  577. } else {
  578. time.Sleep(time.Millisecond * 500)
  579. }
  580. queryNum++
  581. }
  582. } else {
  583. for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
  584. rows, err = DB.Query("select " + title + " from " + dbName + " " + limitStr)
  585. if err == nil {
  586. break
  587. } else {
  588. time.Sleep(time.Millisecond * 500)
  589. }
  590. queryNum++
  591. }
  592. }
  593. if err != nil {
  594. return list, err
  595. }
  596. defer rows.Close()
  597. columns, _ := rows.Columns()
  598. scanArgs := make([]interface{}, len(columns))
  599. values := make([]interface{}, len(columns))
  600. for i := range values {
  601. scanArgs[i] = &values[i]
  602. }
  603. var record map[string]string
  604. var index string
  605. for rows.Next() {
  606. //将行数据保存到record字典
  607. err = rows.Scan(scanArgs...)
  608. record = make(map[string]string)
  609. for i, col := range values {
  610. if col != nil {
  611. index = helper.StrFirstToUpper(columns[i])
  612. record[index] = helper.ToString(col)
  613. }
  614. }
  615. list = append(list, record)
  616. }
  617. return list, nil
  618. }
  619. /**
  620. * 查询总数
  621. * 2018/04/19
  622. */
  623. func GetTotal(dbName, table string, args ...string) (total int) {
  624. if dbName == "" && table == "" {
  625. return
  626. }
  627. if strings.Contains(table, "select ") {
  628. dbName = table
  629. } else {
  630. dbName = getTableName(dbName, table)
  631. }
  632. var title string = "*"
  633. if len(args) > 0 {
  634. title = args[0]
  635. }
  636. var rows *sql.Rows
  637. var err error
  638. var queryNum int = 0
  639. for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
  640. rows, err = DB.Query("select count(" + title + ") as count from " + dbName + " limit 1")
  641. if err == nil {
  642. break
  643. } else {
  644. time.Sleep(time.Millisecond * 500)
  645. }
  646. queryNum++
  647. }
  648. if err != nil {
  649. log.Println("ERROR|get", dbName, "total error", err)
  650. return
  651. }
  652. defer rows.Close()
  653. var count int
  654. for rows.Next() {
  655. //将行数据保存到record字典
  656. err = rows.Scan(&count)
  657. if err != nil {
  658. log.Println("ERROR|get", dbName, "total error", err)
  659. } else {
  660. total = count
  661. }
  662. }
  663. return
  664. }
  665. /**
  666. * 查询总数
  667. * 2020/06/04
  668. */
  669. func GetCount(dbName, table string, where map[string]string, args ...string) (total int) {
  670. if dbName == "" && table == "" {
  671. return
  672. }
  673. if strings.Contains(table, "select ") {
  674. dbName = table
  675. } else {
  676. dbName = getTableName(dbName, table)
  677. }
  678. var title string = "*"
  679. if len(title) > 0 {
  680. title = args[0]
  681. }
  682. var rows *sql.Rows
  683. var err error
  684. var queryNum int = 0
  685. if len(where) > 0 {
  686. valueList := make([]interface{}, len(where))
  687. whereStr := make([]string, len(where))
  688. i := 0
  689. var keys []string
  690. for key, value := range where {
  691. key = strings.Trim(key, " ")
  692. value = strings.Trim(value, " ")
  693. if value == "" || key == "" {
  694. continue
  695. // return list, errors.New("Query condition is empty")
  696. }
  697. if strings.Contains(key, " ") {
  698. //key中包含空格,判断是否不等于条件
  699. keys = strings.Split(key, " ")
  700. if helper.IsInStringArray(judg(), strings.Trim(keys[1], " ")) {
  701. whereStr[i] = strings.Trim(keys[0], " ") + " " + strings.Trim(keys[1], " ") + " ?"
  702. }
  703. } else {
  704. whereStr[i] = key + " = ?"
  705. valueList[i] = value
  706. }
  707. i++
  708. }
  709. for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
  710. rows, err = DB.Query("select count("+title+") as count from "+dbName+" where "+strings.Join(whereStr, " and ")+" limit 1", valueList...)
  711. if err == nil {
  712. break
  713. } else {
  714. time.Sleep(time.Millisecond * 500)
  715. }
  716. queryNum++
  717. }
  718. } else {
  719. for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
  720. rows, err = DB.Query("select count(" + title + ") as count from " + dbName + " limit 1")
  721. if err == nil {
  722. break
  723. } else {
  724. time.Sleep(time.Millisecond * 500)
  725. }
  726. queryNum++
  727. }
  728. }
  729. if err != nil {
  730. log.Println("ERROR|get", dbName, "count error", err)
  731. return
  732. }
  733. defer rows.Close()
  734. var count int
  735. for rows.Next() {
  736. //将行数据保存到record字典
  737. err = rows.Scan(&count)
  738. if err != nil {
  739. log.Println("ERROR|get", dbName, "count error", err)
  740. } else {
  741. total = count
  742. }
  743. }
  744. return
  745. }
  746. func DoQuery(args ...interface{}) ([]map[string]string, error) {
  747. var list []map[string]string
  748. if len(args) < 1 {
  749. return list, errors.New("Query condition is empty")
  750. }
  751. queryStr := helper.ToString(args[0])
  752. if queryStr == "" {
  753. return list, errors.New("Query condition is empty")
  754. }
  755. var rows *sql.Rows
  756. var err error
  757. var queryNum int = 0
  758. for queryNum < 3 { //如发生错误,继续查询5次,防止数据库连接断开问题
  759. if len(args) > 1 {
  760. rows, err = DB.Query(queryStr, args[1:]...) //strings.Join(args[1:], ",")
  761. if err != nil {
  762. log.Println("ERROR|DoQuery error:", err)
  763. }
  764. } else {
  765. rows, err = DB.Query(queryStr)
  766. if err != nil {
  767. log.Println("ERROR|DoQuery error:", err)
  768. }
  769. }
  770. if err == nil {
  771. break
  772. } else {
  773. time.Sleep(time.Millisecond * 500)
  774. }
  775. queryNum++
  776. }
  777. if err != nil {
  778. return list, err
  779. }
  780. defer rows.Close()
  781. columns, _ := rows.Columns()
  782. scanArgs := make([]interface{}, len(columns))
  783. values := make([]interface{}, len(columns))
  784. for i := range values {
  785. scanArgs[i] = &values[i]
  786. }
  787. var record map[string]string
  788. var index string
  789. for rows.Next() {
  790. //将行数据保存到record字典
  791. err = rows.Scan(scanArgs...)
  792. record = make(map[string]string)
  793. for i, col := range values {
  794. if col != nil {
  795. index = helper.StrFirstToUpper(columns[i])
  796. record[index] = helper.ToString(col)
  797. }
  798. }
  799. list = append(list, record)
  800. }
  801. return list, nil
  802. }