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

961 lines
21 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, having 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 having != "" {
  298. sql_str = helper.StringJoin(sql_str, " having ", having)
  299. }
  300. if orderby != "" {
  301. sql_str = helper.StringJoin(sql_str, " order by ", orderby)
  302. }
  303. if debug {
  304. log.Println("query sql:", sql_str, valueList)
  305. }
  306. condition_len := 0 //所有条件数
  307. for _, ch2 := range sql_str {
  308. if string(ch2) == "?" {
  309. condition_len++
  310. }
  311. }
  312. if condition_len != len(valueList) {
  313. return 0, nil, errors.New("参数错误,条件值错误")
  314. }
  315. var rows *sql.Rows
  316. var err error
  317. var queryNum int = 0
  318. sql_str = helper.StringJoin(sql_str, " limit 1")
  319. var db *sql.DB
  320. if SLAVER_DB != nil {
  321. db = SLAVER_DB
  322. } else {
  323. db = DB
  324. }
  325. for queryNum < 2 { //如发生错误,继续查询2次,防止数据库连接断开问题
  326. rows, err = db.Query(sql_str, valueList...)
  327. if err == nil {
  328. break
  329. } else {
  330. log.Println(err)
  331. time.Sleep(time.Millisecond * 500)
  332. }
  333. queryNum++
  334. }
  335. if err != nil {
  336. rows.Close()
  337. return count, info, err
  338. }
  339. columns, _ := rows.Columns()
  340. scanArgs := make([]interface{}, len(columns))
  341. values := make([]interface{}, len(columns))
  342. for i := range values {
  343. scanArgs[i] = &values[i]
  344. }
  345. var index string
  346. var rowerr error
  347. for rows.Next() {
  348. rowerr = rows.Scan(scanArgs...)
  349. if rowerr == nil {
  350. for i, col := range values {
  351. if col != nil {
  352. index = helper.StrFirstToUpper(columns[i])
  353. info[index] = helper.ToString(col)
  354. }
  355. }
  356. count++
  357. } else {
  358. log.Println("ERROR", rowerr)
  359. }
  360. }
  361. rows.Close()
  362. if rowerr != nil {
  363. return count, info, rowerr
  364. }
  365. return count, info, nil
  366. }
  367. /**
  368. * 查找多条记录
  369. * @param dbName 数据表名
  370. * @param title 查询字段名
  371. */
  372. func FetchRows(dbName, table_name, alias string, titles string, join [][]string, where, where_or []string, valueList []interface{}, orderby, groupby, having string, page int, page_size int, debug bool) (int, []map[string]string, error) {
  373. var count int = 0
  374. list := make([]map[string]string, 0)
  375. if dbName == "" && table_name == "" {
  376. return count, list, errors.New("没有数据表")
  377. }
  378. table := ""
  379. if strings.Contains(table_name, "select ") {
  380. table = table_name
  381. } else {
  382. table = getTableName(dbName, table_name)
  383. }
  384. var sql_str, title string
  385. if titles != "" {
  386. title = titles
  387. } else {
  388. title = "*"
  389. }
  390. sql_str = helper.StringJoin("/*slave*/ select ", title)
  391. if alias != "" {
  392. table = helper.StringJoin(table, " as ", alias)
  393. }
  394. sql_str = helper.StringJoin(sql_str, " from ", table)
  395. if len(join) > 0 {
  396. for _, joinitem := range join {
  397. if len(joinitem) < 2 {
  398. continue
  399. }
  400. if len(joinitem) == 3 {
  401. sql_str = helper.StringJoin(sql_str, " ", joinitem[2], " join ", getTableName(dbName, joinitem[0]), " on ", joinitem[1])
  402. } else { //默认左连接
  403. sql_str = helper.StringJoin(sql_str, " left join ", getTableName(dbName, joinitem[0]), " on ", joinitem[1])
  404. }
  405. }
  406. }
  407. if len(where) > 0 || len(where_or) > 0 {
  408. sql_str = helper.StringJoin(sql_str, " where ")
  409. }
  410. if len(where) > 0 {
  411. sql_str = helper.StringJoin(sql_str, " (", strings.Join(where, " and "), " ) ")
  412. }
  413. if len(where_or) > 0 {
  414. if len(where) > 0 {
  415. sql_str = helper.StringJoin(sql_str, " or ", strings.Join(where_or, " or "))
  416. } else {
  417. sql_str = helper.StringJoin(sql_str, strings.Join(where_or, " or "))
  418. }
  419. }
  420. if groupby != "" {
  421. sql_str = helper.StringJoin(sql_str, " group by ", groupby)
  422. }
  423. if having != "" {
  424. sql_str = helper.StringJoin(sql_str, " HAVING ", having)
  425. }
  426. if orderby != "" {
  427. sql_str = helper.StringJoin(sql_str, " order by ", orderby)
  428. }
  429. if page > 0 || page_size > 0 {
  430. if page < 1 {
  431. page = 1
  432. }
  433. if page_size < 1 {
  434. page_size = 10
  435. }
  436. from := strconv.Itoa((page - 1) * page_size)
  437. offset := strconv.Itoa(page_size)
  438. if from != "" && offset != "" {
  439. sql_str = helper.StringJoin(sql_str, " limit ", from, " , ", offset)
  440. }
  441. }
  442. if debug {
  443. log.Println("query sql:", sql_str, valueList)
  444. }
  445. condition_len := 0 //所有条件数
  446. for _, ch2 := range sql_str {
  447. if string(ch2) == "?" {
  448. condition_len++
  449. }
  450. }
  451. if condition_len != len(valueList) {
  452. return 0, list, errors.New("参数错误,条件值错误")
  453. }
  454. var db *sql.DB
  455. if SLAVER_DB != nil {
  456. db = SLAVER_DB
  457. } else {
  458. db = DB
  459. }
  460. var rows *sql.Rows
  461. var err error
  462. var queryNum int = 0
  463. for queryNum < 2 { //如发生错误,继续查询2次,防止数据库连接断开问题
  464. rows, err = db.Query(sql_str, valueList...)
  465. if err == nil {
  466. break
  467. } else {
  468. log.Println(err)
  469. time.Sleep(time.Millisecond * 500)
  470. }
  471. queryNum++
  472. }
  473. if err != nil {
  474. rows.Close()
  475. return 0, list, err
  476. }
  477. columns, _ := rows.Columns()
  478. scanArgs := make([]interface{}, len(columns))
  479. values := make([]interface{}, len(columns))
  480. for i := range values {
  481. scanArgs[i] = &values[i]
  482. }
  483. var index string
  484. var rowerr error
  485. var info map[string]string
  486. for rows.Next() {
  487. rowerr = rows.Scan(scanArgs...)
  488. info = make(map[string]string)
  489. if rowerr == nil {
  490. for i, col := range values {
  491. if col != nil {
  492. index = helper.StrFirstToUpper(columns[i])
  493. info[index] = helper.ToString(col)
  494. }
  495. }
  496. count++
  497. } else {
  498. log.Println("ERROR", rowerr)
  499. }
  500. if len(info) > 0 {
  501. list = append(list, info)
  502. }
  503. }
  504. rows.Close()
  505. return count, list, nil
  506. }
  507. func GetInfo(dbName, table string, title string, where map[string]string) (map[string]string, error) {
  508. count, info, gzErr := GetData(dbName, table, title, where, nil)
  509. if gzErr != nil {
  510. return info, gzErr
  511. } else {
  512. if count < 1 {
  513. return info, errors.New("No data")
  514. }
  515. return info, nil
  516. }
  517. }
  518. /**
  519. * 查询列表
  520. * 2018/04/19
  521. */
  522. func GetList(dbName, table string, title string, where map[string]string, limit map[string]string) ([]map[string]string, error) {
  523. var list []map[string]string
  524. if dbName == "" && table == "" {
  525. return list, errors.New("没有数据表")
  526. }
  527. if strings.Contains(table, "select ") {
  528. dbName = table
  529. } else {
  530. dbName = getTableName(dbName, table)
  531. }
  532. var rows *sql.Rows
  533. var err error
  534. var queryNum int = 0
  535. var limitStr string = ""
  536. if len(limit) > 0 {
  537. var offset string = "0" //偏移量,个数
  538. var from string = "" //开始
  539. if _, ok := limit["order"]; ok {
  540. limitStr += " order by " + limit["order"]
  541. }
  542. if _, ok := limit["offset"]; ok {
  543. offset = limit["offset"]
  544. }
  545. if _, ok := limit["from"]; ok {
  546. from = limit["from"]
  547. }
  548. if offset != "0" && from != "" {
  549. limitStr += " limit " + from + "," + offset
  550. }
  551. }
  552. if len(where) > 0 {
  553. valueList := make([]interface{}, len(where))
  554. whereStr := make([]string, len(where))
  555. i := 0
  556. var keys []string
  557. for key, value := range where {
  558. key = strings.Trim(key, " ")
  559. value = strings.Trim(value, " ")
  560. if value == "" || key == "" {
  561. continue
  562. // return list, errors.New("Query condition is empty")
  563. }
  564. if strings.Contains(key, " ") {
  565. //key中包含空格,判断是否不等于条件
  566. keys = strings.Split(key, " ")
  567. if helper.IsInStringArray(judg(), strings.Trim(keys[1], " ")) {
  568. whereStr[i] = strings.Trim(keys[0], " ") + " " + strings.Trim(keys[1], " ") + " ?"
  569. }
  570. } else {
  571. whereStr[i] = key + " = ?"
  572. valueList[i] = value
  573. }
  574. i++
  575. }
  576. if len(whereStr) == 0 || len(valueList) == 0 {
  577. return list, errors.New("Query condition is empty")
  578. }
  579. for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
  580. rows, err = DB.Query("select "+title+" from "+dbName+" where "+strings.Join(whereStr, " and ")+" "+limitStr, valueList...)
  581. if err == nil {
  582. break
  583. } else {
  584. time.Sleep(time.Millisecond * 500)
  585. }
  586. queryNum++
  587. }
  588. } else {
  589. for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
  590. rows, err = DB.Query("select " + title + " from " + dbName + " " + limitStr)
  591. if err == nil {
  592. break
  593. } else {
  594. time.Sleep(time.Millisecond * 500)
  595. }
  596. queryNum++
  597. }
  598. }
  599. if err != nil {
  600. return list, err
  601. }
  602. defer rows.Close()
  603. columns, _ := rows.Columns()
  604. scanArgs := make([]interface{}, len(columns))
  605. values := make([]interface{}, len(columns))
  606. for i := range values {
  607. scanArgs[i] = &values[i]
  608. }
  609. var record map[string]string
  610. var index string
  611. for rows.Next() {
  612. //将行数据保存到record字典
  613. err = rows.Scan(scanArgs...)
  614. record = make(map[string]string)
  615. for i, col := range values {
  616. if col != nil {
  617. index = helper.StrFirstToUpper(columns[i])
  618. record[index] = helper.ToString(col)
  619. }
  620. }
  621. list = append(list, record)
  622. }
  623. return list, nil
  624. }
  625. /**
  626. * 查询总数
  627. * 2018/04/19
  628. */
  629. func GetTotal(dbName, table string, args ...string) (total int) {
  630. if dbName == "" && table == "" {
  631. return
  632. }
  633. if strings.Contains(table, "select ") {
  634. dbName = table
  635. } else {
  636. dbName = getTableName(dbName, table)
  637. }
  638. var title string = "*"
  639. if len(args) > 0 {
  640. title = args[0]
  641. }
  642. var rows *sql.Rows
  643. var err error
  644. var queryNum int = 0
  645. for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
  646. rows, err = DB.Query("select count(" + title + ") as count from " + dbName + " limit 1")
  647. if err == nil {
  648. break
  649. } else {
  650. time.Sleep(time.Millisecond * 500)
  651. }
  652. queryNum++
  653. }
  654. if err != nil {
  655. log.Println("ERROR|get", dbName, "total error", err)
  656. return
  657. }
  658. defer rows.Close()
  659. var count int
  660. for rows.Next() {
  661. //将行数据保存到record字典
  662. err = rows.Scan(&count)
  663. if err != nil {
  664. log.Println("ERROR|get", dbName, "total error", err)
  665. } else {
  666. total = count
  667. }
  668. }
  669. return
  670. }
  671. /**
  672. * 查询总数
  673. * 2020/06/04
  674. */
  675. func GetCount(dbName, table string, where map[string]string, args ...string) (total int) {
  676. if dbName == "" && table == "" {
  677. return
  678. }
  679. if strings.Contains(table, "select ") {
  680. dbName = table
  681. } else {
  682. dbName = getTableName(dbName, table)
  683. }
  684. var title string = "*"
  685. if len(title) > 0 {
  686. title = args[0]
  687. }
  688. var rows *sql.Rows
  689. var err error
  690. var queryNum int = 0
  691. if len(where) > 0 {
  692. valueList := make([]interface{}, len(where))
  693. whereStr := make([]string, len(where))
  694. i := 0
  695. var keys []string
  696. for key, value := range where {
  697. key = strings.Trim(key, " ")
  698. value = strings.Trim(value, " ")
  699. if value == "" || key == "" {
  700. continue
  701. // return list, errors.New("Query condition is empty")
  702. }
  703. if strings.Contains(key, " ") {
  704. //key中包含空格,判断是否不等于条件
  705. keys = strings.Split(key, " ")
  706. if helper.IsInStringArray(judg(), strings.Trim(keys[1], " ")) {
  707. whereStr[i] = strings.Trim(keys[0], " ") + " " + strings.Trim(keys[1], " ") + " ?"
  708. }
  709. } else {
  710. whereStr[i] = key + " = ?"
  711. valueList[i] = value
  712. }
  713. i++
  714. }
  715. for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
  716. rows, err = DB.Query("select count("+title+") as count from "+dbName+" where "+strings.Join(whereStr, " and ")+" limit 1", valueList...)
  717. if err == nil {
  718. break
  719. } else {
  720. time.Sleep(time.Millisecond * 500)
  721. }
  722. queryNum++
  723. }
  724. } else {
  725. for queryNum < 5 { //如发生错误,继续查询5次,防止数据库连接断开问题
  726. rows, err = DB.Query("select count(" + title + ") as count from " + dbName + " limit 1")
  727. if err == nil {
  728. break
  729. } else {
  730. time.Sleep(time.Millisecond * 500)
  731. }
  732. queryNum++
  733. }
  734. }
  735. if err != nil {
  736. log.Println("ERROR|get", dbName, "count error", err)
  737. return
  738. }
  739. defer rows.Close()
  740. var count int
  741. for rows.Next() {
  742. //将行数据保存到record字典
  743. err = rows.Scan(&count)
  744. if err != nil {
  745. log.Println("ERROR|get", dbName, "count error", err)
  746. } else {
  747. total = count
  748. }
  749. }
  750. return
  751. }
  752. func DoQuery(args ...interface{}) ([]map[string]string, error) {
  753. var list []map[string]string
  754. if len(args) < 1 {
  755. return list, errors.New("Query condition is empty")
  756. }
  757. queryStr := helper.ToString(args[0])
  758. if queryStr == "" {
  759. return list, errors.New("Query condition is empty")
  760. }
  761. var rows *sql.Rows
  762. var err error
  763. var queryNum int = 0
  764. for queryNum < 3 { //如发生错误,继续查询5次,防止数据库连接断开问题
  765. if len(args) > 1 {
  766. rows, err = DB.Query(queryStr, args[1:]...) //strings.Join(args[1:], ",")
  767. if err != nil {
  768. log.Println("ERROR|DoQuery error:", err)
  769. }
  770. } else {
  771. rows, err = DB.Query(queryStr)
  772. if err != nil {
  773. log.Println("ERROR|DoQuery error:", err)
  774. }
  775. }
  776. if err == nil {
  777. break
  778. } else {
  779. time.Sleep(time.Millisecond * 500)
  780. }
  781. queryNum++
  782. }
  783. if err != nil {
  784. return list, err
  785. }
  786. defer rows.Close()
  787. columns, _ := rows.Columns()
  788. scanArgs := make([]interface{}, len(columns))
  789. values := make([]interface{}, len(columns))
  790. for i := range values {
  791. scanArgs[i] = &values[i]
  792. }
  793. var record map[string]string
  794. var index string
  795. for rows.Next() {
  796. //将行数据保存到record字典
  797. err = rows.Scan(scanArgs...)
  798. record = make(map[string]string)
  799. for i, col := range values {
  800. if col != nil {
  801. index = helper.StrFirstToUpper(columns[i])
  802. record[index] = helper.ToString(col)
  803. }
  804. }
  805. list = append(list, record)
  806. }
  807. return list, nil
  808. }