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

925 lines
20 KiB

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