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

892 lines
20 KiB

2 years ago
2 years ago
2 years ago
1 month ago
1 month ago
1 month ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. package dbquery
  2. /**
  3. * 事务操作
  4. */
  5. import (
  6. "database/sql"
  7. "errors"
  8. "log"
  9. "strconv"
  10. "strings"
  11. "git.tetele.net/tgo/helper"
  12. )
  13. type TxQuery struct {
  14. dbname string
  15. table string
  16. alias string
  17. title string
  18. where []string
  19. where_or []string
  20. join [][]string //[["tablea as a","a.id=b.id","left"]]
  21. data []string
  22. value []interface{}
  23. save_data []map[string]interface{} //批量操作的数据[["title":"a","num":1,],["title":"a","num":1,]]
  24. upd_field []string // 批量更新时需要更新的字段,为空时按除id外的字段进行更新
  25. orderby string
  26. groupby string
  27. having string
  28. page int
  29. page_size int
  30. stmt *sql.Stmt
  31. conn *sql.DB
  32. tx *sql.Tx
  33. debug bool
  34. with [][]string //[[临时表的sql语句,临时表的名称]]
  35. }
  36. func NewTxQuery(t ...string) *TxQuery {
  37. var conn_type *sql.DB = DB
  38. if len(t) > 0 {
  39. switch t[0] {
  40. case "mysql":
  41. conn_type = DB
  42. case "mssql": //sql server
  43. conn_type = MSDB_CONN
  44. }
  45. }
  46. tx, err := conn_type.Begin()
  47. if err != nil {
  48. log.Println("start tx begin error", err)
  49. }
  50. return &TxQuery{
  51. conn: conn_type,
  52. tx: tx,
  53. }
  54. }
  55. func (this *TxQuery) Conn(conn *sql.DB) *TxQuery {
  56. this.conn = conn
  57. return this
  58. }
  59. func (this *TxQuery) Db(dbname string) *TxQuery {
  60. this.dbname = dbname
  61. return this
  62. }
  63. func (this *TxQuery) Table(tablename string) *TxQuery {
  64. this.table = tablename
  65. return this
  66. }
  67. func (this *TxQuery) Alias(tablename string) *TxQuery {
  68. this.alias = tablename
  69. return this
  70. }
  71. func (this *TxQuery) Title(title string) *TxQuery {
  72. this.title = title
  73. return this
  74. }
  75. func (this *TxQuery) Page(page int) *TxQuery {
  76. this.page = page
  77. return this
  78. }
  79. func (this *TxQuery) PageSize(page_num int) *TxQuery {
  80. this.page_size = page_num
  81. return this
  82. }
  83. func (this *TxQuery) Orderby(orderby string) *TxQuery {
  84. this.orderby = orderby
  85. return this
  86. }
  87. func (this *TxQuery) Groupby(groupby string) *TxQuery {
  88. this.groupby = groupby
  89. return this
  90. }
  91. func (this *TxQuery) Having(having string) *TxQuery {
  92. this.having = having
  93. return this
  94. }
  95. func (this *TxQuery) Where(where string) *TxQuery {
  96. this.where = append(this.where, where)
  97. return this
  98. }
  99. func (this *TxQuery) With(with []string) *TxQuery {
  100. this.with = append(this.with, with)
  101. return this
  102. }
  103. func (this *TxQuery) Withs(withs [][]string) *TxQuery {
  104. this.with = append(this.with, withs...)
  105. return this
  106. }
  107. func (this *TxQuery) Wheres(wheres []string) *TxQuery {
  108. if len(wheres) > 0 {
  109. this.where = append(this.where, wheres...)
  110. }
  111. return this
  112. }
  113. func (this *TxQuery) WhereOr(where string) *TxQuery {
  114. this.where_or = append(this.where_or, where)
  115. return this
  116. }
  117. func (this *TxQuery) Value(value interface{}) *TxQuery {
  118. this.value = append(this.value, value)
  119. return this
  120. }
  121. func (this *TxQuery) SaveData(value map[string]interface{}) *TxQuery {
  122. this.save_data = append(this.save_data, value)
  123. return this
  124. }
  125. func (this *TxQuery) SaveDatas(value []map[string]interface{}) *TxQuery {
  126. this.save_data = append(this.save_data, value...)
  127. return this
  128. }
  129. func (this *TxQuery) UpdField(value string) *TxQuery {
  130. this.upd_field = append(this.upd_field, value)
  131. return this
  132. }
  133. func (this *TxQuery) UpdFields(value []string) *TxQuery {
  134. this.upd_field = append(this.upd_field, value...)
  135. return this
  136. }
  137. func (this *TxQuery) Values(values []interface{}) *TxQuery {
  138. this.value = append(this.value, values...)
  139. return this
  140. }
  141. func (this *TxQuery) Join(join []string) *TxQuery {
  142. this.join = append(this.join, join)
  143. return this
  144. }
  145. /**
  146. * 左连接
  147. * 2023/08/10
  148. * gz
  149. */
  150. func (this *TxQuery) LeftJoin(table_name string, condition string) *TxQuery {
  151. this.join = append(this.join, []string{table_name, condition, "left"})
  152. return this
  153. }
  154. /**
  155. * 右连接
  156. * 2023/08/10
  157. * gz
  158. */
  159. func (this *TxQuery) RightJoin(table_name string, condition string) *TxQuery {
  160. this.join = append(this.join, []string{table_name, condition, "right"})
  161. return this
  162. }
  163. func (this *TxQuery) Data(data string) *TxQuery {
  164. this.data = append(this.data, data)
  165. return this
  166. }
  167. func (this *TxQuery) Datas(datas []string) *TxQuery {
  168. this.data = append(this.data, datas...)
  169. return this
  170. }
  171. func (this *TxQuery) Debug(debug bool) *TxQuery {
  172. this.debug = debug
  173. return this
  174. }
  175. /*
  176. * 清理上次查询
  177. */
  178. func (this *TxQuery) Clean() *TxQuery {
  179. this.title = ""
  180. this.where = this.where[0:0]
  181. this.where_or = this.where_or[0:0]
  182. this.join = this.join[0:0]
  183. this.data = this.data[0:0]
  184. this.value = this.value[0:0]
  185. this.orderby = ""
  186. this.groupby = ""
  187. this.page = 0
  188. this.page_size = 0
  189. this.save_data = this.save_data[0:0]
  190. this.upd_field = this.upd_field[0:0]
  191. this.having = ""
  192. this.alias = ""
  193. this.with = this.with[0:0]
  194. return this
  195. }
  196. // 返回表名
  197. func (this *TxQuery) GetTableName(table string) string {
  198. return getTableName(this.dbname, table)
  199. }
  200. // 构造子查询
  201. func (this *TxQuery) BuildSelectSql() (map[string]interface{}, error) {
  202. if this.dbname == "" && this.table == "" {
  203. return nil, errors.New("参数错误,没有数据表")
  204. }
  205. var table = ""
  206. withSql := ""
  207. if len(this.with) > 0 {
  208. var builder strings.Builder
  209. builder.WriteString("WITH ")
  210. boo := false
  211. for k, v := range this.with {
  212. if len(v) < 2 {
  213. continue
  214. }
  215. if k != 0 {
  216. builder.WriteString(", ")
  217. }
  218. builder.WriteString(v[1])
  219. builder.WriteString(" as (")
  220. builder.WriteString(v[0])
  221. builder.WriteString(")")
  222. boo = true
  223. }
  224. if boo {
  225. builder.WriteString(" ")
  226. withSql = builder.String()
  227. }
  228. }
  229. if withSql != "" || strings.Contains(this.table, "select ") || strings.HasPrefix(this.table, "(") {
  230. table = this.table
  231. } else {
  232. table = getTableName(this.dbname, this.table)
  233. }
  234. var sql, title string
  235. if this.title != "" {
  236. title = this.title
  237. } else {
  238. title = "*"
  239. }
  240. sql = helper.StringJoin(withSql, "select ", title)
  241. if this.alias != "" {
  242. table = helper.StringJoin(table, " as ", this.alias)
  243. }
  244. sql = helper.StringJoin(sql, " from ", table)
  245. if len(this.join) > 0 {
  246. var builder strings.Builder
  247. builder.WriteString(sql)
  248. boo := false
  249. for _, joinitem := range this.join {
  250. if len(joinitem) < 2 {
  251. continue
  252. }
  253. builder.WriteString(" ")
  254. if len(joinitem) >= 3 {
  255. builder.WriteString(joinitem[2])
  256. } else {
  257. builder.WriteString("left")
  258. }
  259. builder.WriteString(" join ")
  260. if withSql != "" || strings.Contains(joinitem[0], "select ") || strings.HasPrefix(joinitem[0], "(") {
  261. builder.WriteString(joinitem[0])
  262. } else {
  263. builder.WriteString(getTableName(this.dbname, joinitem[0]))
  264. }
  265. builder.WriteString(" on ")
  266. builder.WriteString(joinitem[1])
  267. boo = true
  268. }
  269. if boo {
  270. sql = builder.String()
  271. }
  272. }
  273. if len(this.where) > 0 || len(this.where_or) > 0 {
  274. sql = helper.StringJoin(sql, " where ")
  275. }
  276. if len(this.where) > 0 {
  277. sql = helper.StringJoin(sql, " (", strings.Join(this.where, " and "), " ) ")
  278. }
  279. if len(this.where_or) > 0 {
  280. if len(this.where) > 0 {
  281. sql = helper.StringJoin(sql, " or ", strings.Join(this.where_or, " or "))
  282. } else {
  283. sql = helper.StringJoin(sql, strings.Join(this.where_or, " or "))
  284. }
  285. }
  286. if this.groupby != "" {
  287. sql = helper.StringJoin(sql, " group by ", this.groupby)
  288. }
  289. if this.having != "" {
  290. sql = helper.StringJoin(sql, " having ", this.having)
  291. }
  292. if this.orderby != "" {
  293. sql = helper.StringJoin(sql, " order by ", this.orderby)
  294. }
  295. if this.page > 0 || this.page_size > 0 {
  296. if this.page < 1 {
  297. this.page = 1
  298. }
  299. if this.page_size < 1 {
  300. this.page_size = 10
  301. }
  302. from := strconv.Itoa((this.page - 1) * this.page_size)
  303. offset := strconv.Itoa(this.page_size)
  304. if from != "" && offset != "" {
  305. sql = helper.StringJoin(sql, " limit ", from, " , ", offset)
  306. }
  307. }
  308. if this.debug {
  309. log.Println("query sql:", sql, this.value)
  310. }
  311. condition_len := 0 //所有条件数
  312. for _, ch2 := range sql {
  313. if string(ch2) == "?" {
  314. condition_len++
  315. }
  316. }
  317. if condition_len != len(this.value) {
  318. return nil, errors.New("参数错误,条件值错误")
  319. }
  320. return map[string]interface{}{
  321. "sql": sql,
  322. "value": this.value,
  323. }, nil
  324. }
  325. // 获取表格信息
  326. func (this *TxQuery) GetTableInfo(table string) (map[string]interface{}, error) {
  327. field := []string{
  328. "COLUMN_NAME", //字段名
  329. "COLUMN_DEFAULT", //默认值
  330. "DATA_TYPE", //数据类型
  331. "COLUMN_TYPE", //数据类型+长度
  332. "COLUMN_COMMENT", //备注
  333. "IS_NULLABLE", //是否为空
  334. }
  335. sql := "select `" + strings.Join(field, "`,`") + "` from information_schema.COLUMNS where table_name = ? and table_schema = ?"
  336. stmtSql, err := this.tx.Prepare(sql)
  337. if err != nil {
  338. return nil, err
  339. }
  340. list, err := StmtForQueryList(stmtSql, []interface{}{table, this.dbname})
  341. if err != nil {
  342. return nil, err
  343. }
  344. rows := make([]interface{}, 0, len(list))
  345. fieldName := make([]string, 0, len(list))
  346. for _, item := range list {
  347. info := map[string]interface{}{
  348. "name": "",
  349. "column_type": "",
  350. "is_null": true,
  351. "data_type": "",
  352. "comment": "",
  353. "default": "",
  354. }
  355. for _, k := range field {
  356. index := helper.StrFirstToUpper(k)
  357. if v, ok := item[index]; ok {
  358. switch k {
  359. case "COLUMN_NAME":
  360. info["name"] = v
  361. case "COLUMN_DEFAULT":
  362. info["default"] = v
  363. case "DATA_TYPE":
  364. info["data_type"] = v
  365. case "COLUMN_TYPE":
  366. info["column_type"] = helper.ToInt64(v)
  367. case "COLUMN_COMMENT":
  368. info["comment"] = helper.ToInt64(v)
  369. case "IS_NULLABLE":
  370. if v == "NO" {
  371. info["is_null"] = false
  372. }
  373. }
  374. }
  375. }
  376. name := helper.ToStr(info["name"])
  377. if name != "" {
  378. rows = append(rows, info)
  379. fieldName = append(fieldName, name)
  380. }
  381. }
  382. return map[string]interface{}{
  383. "field": fieldName,
  384. "list": rows,
  385. }, nil
  386. }
  387. // 拼查询sql
  388. func (this *TxQuery) QueryStmt() error {
  389. res := map[string]interface{}{}
  390. res, err = this.BuildSelectSql()
  391. if err != nil {
  392. return err
  393. }
  394. sql := helper.ToStr(res["sql"])
  395. stmt, err = this.tx.Prepare(sql + " FOR UPDATE")
  396. if err != nil {
  397. return err
  398. }
  399. this.stmt = stmt
  400. return nil
  401. }
  402. // 拼更新sql
  403. func (this *TxQuery) UpdateStmt() error {
  404. if this.dbname == "" && this.table == "" {
  405. return errors.New("参数错误,没有数据表")
  406. }
  407. if len(this.where) < 1 {
  408. return errors.New("参数错误,缺少条件")
  409. }
  410. dbName := getTableName(this.dbname, this.table)
  411. var sql string
  412. sql = helper.StringJoin("update ", dbName, " set ", strings.Join(this.data, " , "))
  413. sql = helper.StringJoin(sql, " where ", strings.Join(this.where, " and "))
  414. if this.debug {
  415. log.Println("update sql:", sql, this.value)
  416. }
  417. condition_len := 0 //所有条件数
  418. for _, ch2 := range sql {
  419. if string(ch2) == "?" {
  420. condition_len++
  421. }
  422. }
  423. if condition_len != len(this.value) {
  424. return errors.New("参数错误,条件值错误")
  425. }
  426. stmt, err = this.tx.Prepare(sql)
  427. if err != nil {
  428. return err
  429. }
  430. this.stmt = stmt
  431. return nil
  432. }
  433. // 拼批量存在更新不存在插入sql
  434. func (this *TxQuery) UpdateAllStmt() error {
  435. if this.dbname == "" && this.table == "" {
  436. return errors.New("参数错误,没有数据表")
  437. }
  438. dbName := getTableName(this.dbname, this.table)
  439. var sql string
  440. var dataSql []string //一组用到的占位字符
  441. var valSql []string //占位字符组
  442. var updSql []string //更新字段的sql
  443. var updFieldLen = len(this.upd_field) //需要更新的字段数量,为0时更新除id外添加值
  444. dataLen := len(this.save_data)
  445. if dataLen > 0 {
  446. //批量操作
  447. this.data = this.data[0:0]
  448. this.value = this.value[0:0]
  449. var dataSqlText string //占位字符组
  450. for i := 0; i < dataLen; i++ {
  451. if i == 0 {
  452. //第一组时分配变量空间
  453. fieldLen := len(this.save_data[i])
  454. this.data = make([]string, 0, fieldLen)
  455. dataSql = make([]string, 0, fieldLen)
  456. this.value = make([]interface{}, 0, fieldLen*dataLen)
  457. valSql = make([]string, 0, dataLen)
  458. switch updFieldLen {
  459. case 0:
  460. //预览创建数据的长度
  461. updSql = make([]string, 0, fieldLen)
  462. default:
  463. //按照需要更新字段数长度
  464. updSql = make([]string, 0, updFieldLen)
  465. for _, k := range this.upd_field {
  466. updSql = append(updSql, k+"=values("+k+")") //存储需要更新的字段
  467. }
  468. }
  469. for k := range this.save_data[i] {
  470. this.data = append(this.data, k) //存储添加的字段
  471. dataSql = append(dataSql, "?") //存储需要的占位符
  472. if updFieldLen == 0 && k != "id" {
  473. updSql = append(updSql, k+"=values("+k+")") //存储需要更新的字段
  474. }
  475. }
  476. dataSqlText = strings.Join(dataSql, ",") //组成每组占位字符格式
  477. }
  478. for j := 0; j < len(this.data); j++ {
  479. this.value = append(this.value, this.save_data[i][this.data[j]]) //存储值
  480. }
  481. valSql = append(valSql, "("+dataSqlText+")") //组成占位字符组
  482. }
  483. } else {
  484. //添加一条(原理同上)
  485. fieldLen := len(this.data)
  486. dataSql = make([]string, 0, fieldLen)
  487. valSql = make([]string, 0, 1)
  488. switch updFieldLen {
  489. case 0:
  490. updSql = make([]string, 0, fieldLen)
  491. default:
  492. updSql = make([]string, 0, updFieldLen)
  493. for _, k := range this.upd_field {
  494. updSql = append(updSql, k+"=values("+k+")")
  495. }
  496. }
  497. for i := 0; i < fieldLen; i++ {
  498. dataSql = append(dataSql, "?")
  499. if updFieldLen == 0 && this.data[i] != "id" {
  500. updSql = append(updSql, this.data[i]+"=values("+this.data[i]+")")
  501. }
  502. }
  503. if updFieldLen > 0 {
  504. for _, k := range this.upd_field {
  505. updSql = append(updSql, k+"=values("+k+")")
  506. }
  507. }
  508. valSql = append(valSql, "("+strings.Join(dataSql, " , ")+")")
  509. }
  510. if len(this.data) == 0 {
  511. return errors.New("参数错误,没有字段值")
  512. }
  513. if len(this.value) == 0 {
  514. return errors.New("参数错误,条件值错误")
  515. }
  516. setText := " values "
  517. if len(valSql) > 1 {
  518. setText = " value "
  519. }
  520. sql = helper.StringJoin("insert into ", dbName, " (", strings.Join(this.data, " , "), ")", setText, strings.Join(valSql, ","), " ON DUPLICATE KEY UPDATE ", strings.Join(updSql, " , "))
  521. if this.debug {
  522. log.Println("insert on duplicate key update sql:", sql, this.value)
  523. }
  524. conditionLen := 0 //所有条件数
  525. for _, ch2 := range sql {
  526. if string(ch2) == "?" {
  527. conditionLen++
  528. }
  529. }
  530. if conditionLen != len(this.value) {
  531. return errors.New("参数错误,条件值数量不匹配")
  532. }
  533. stmt, err = this.tx.Prepare(sql)
  534. if err != nil {
  535. return err
  536. }
  537. this.stmt = stmt
  538. return nil
  539. }
  540. // 拼插入sql
  541. func (this *TxQuery) CreateStmt() error {
  542. if this.dbname == "" && this.table == "" {
  543. return errors.New("参数错误,没有数据表")
  544. }
  545. dbName := getTableName(this.dbname, this.table)
  546. var sql string
  547. sql = helper.StringJoin("insert into ", dbName, " set ", strings.Join(this.data, " , "))
  548. if this.debug {
  549. log.Println("insert sql:", sql, this.value)
  550. }
  551. condition_len := 0 //所有条件数
  552. for _, ch2 := range sql {
  553. if string(ch2) == "?" {
  554. condition_len++
  555. }
  556. }
  557. if condition_len != len(this.value) {
  558. return errors.New("参数错误,条件值错误")
  559. }
  560. stmt, err = this.tx.Prepare(sql)
  561. if err != nil {
  562. return err
  563. }
  564. this.stmt = stmt
  565. return nil
  566. }
  567. // 拼批量插入sql
  568. func (this *TxQuery) CreateAllStmt() error {
  569. if this.dbname == "" && this.table == "" {
  570. return errors.New("参数错误,没有数据表")
  571. }
  572. dbName := getTableName(this.dbname, this.table)
  573. var sql string
  574. var dataSql []string //一组用到的占位字符
  575. var valSql []string //占位字符组
  576. dataLen := len(this.save_data)
  577. if dataLen > 0 {
  578. //清空字段和值
  579. this.data = this.data[0:0]
  580. this.value = this.value[0:0]
  581. var dataSqlText string //占位字符组
  582. for i := 0; i < dataLen; i++ {
  583. if i == 0 {
  584. //第一组时分配变量空间
  585. fieldLen := len(this.save_data[i])
  586. this.data = make([]string, 0, fieldLen)
  587. dataSql = make([]string, 0, fieldLen)
  588. this.value = make([]interface{}, 0, fieldLen*dataLen)
  589. valSql = make([]string, 0, dataLen)
  590. for k := range this.save_data[i] {
  591. this.data = append(this.data, k) //存储字段
  592. dataSql = append(dataSql, "?") //存储需要的占位符
  593. }
  594. dataSqlText = strings.Join(dataSql, ",") //组成每组占位字符格式
  595. }
  596. for j := 0; j < len(this.data); j++ {
  597. this.value = append(this.value, this.save_data[i][this.data[j]]) //存储值
  598. }
  599. valSql = append(valSql, "("+dataSqlText+")") //组成占位字符组
  600. }
  601. } else {
  602. //添加一条(原理同上)
  603. fieldLen := len(this.data)
  604. dataSql = make([]string, 0, fieldLen)
  605. for i := 0; i < fieldLen; i++ {
  606. dataSql = append(dataSql, "?")
  607. }
  608. valSql = make([]string, 0, 1)
  609. valSql = append(valSql, "("+strings.Join(dataSql, " , ")+")")
  610. }
  611. if len(this.data) == 0 {
  612. return errors.New("参数错误,字段名错误")
  613. }
  614. if len(this.value) == 0 {
  615. return errors.New("参数错误,条件值错误")
  616. }
  617. //通过sql关键字优化批量操作和单个操作效率
  618. setText := " values "
  619. if len(valSql) > 1 {
  620. setText = " value "
  621. }
  622. sql = helper.StringJoin("insert into ", dbName, " (", strings.Join(this.data, " , "), ")", setText, strings.Join(valSql, ","))
  623. if len(this.value) == 0 {
  624. return errors.New("参数错误,条件值错误")
  625. }
  626. if this.debug {
  627. log.Println("insert sql:", sql, this.value)
  628. }
  629. conditionLen := 0 //所有条件数
  630. for _, ch2 := range sql {
  631. if string(ch2) == "?" {
  632. conditionLen++
  633. }
  634. }
  635. if conditionLen != len(this.value) {
  636. return errors.New("参数错误,条件值数量不匹配")
  637. }
  638. stmt, err = this.tx.Prepare(sql)
  639. if err != nil {
  640. return err
  641. }
  642. this.stmt = stmt
  643. return nil
  644. }
  645. // 拼删除sql
  646. func (this *TxQuery) DeleteStmt() error {
  647. if this.dbname == "" && this.table == "" {
  648. return errors.New("参数错误,没有数据表")
  649. }
  650. if len(this.where) < 1 {
  651. return errors.New("参数错误,缺少条件")
  652. }
  653. dbName := getTableName(this.dbname, this.table)
  654. var sql string
  655. sql = helper.StringJoin("delete from ", dbName, " where ", strings.Join(this.where, " and "))
  656. if this.page_size > 0 {
  657. sql = helper.StringJoin(sql, " limit ", strconv.Itoa(this.page_size))
  658. }
  659. if this.debug {
  660. log.Println("delete sql:", sql, this.value)
  661. }
  662. condition_len := 0 //所有条件数
  663. for _, ch2 := range sql {
  664. if string(ch2) == "?" {
  665. condition_len++
  666. }
  667. }
  668. if condition_len != len(this.value) {
  669. return errors.New("参数错误,条件值错误")
  670. }
  671. stmt, err = this.tx.Prepare(sql)
  672. if err != nil {
  673. return err
  674. }
  675. this.stmt = stmt
  676. return nil
  677. }
  678. /**
  679. * 执行查询列表
  680. * return list error
  681. */
  682. func (this *TxQuery) Select() ([]map[string]string, error) {
  683. err := this.QueryStmt()
  684. if err != nil {
  685. return []map[string]string{}, err
  686. }
  687. if this.stmt == nil {
  688. return []map[string]string{}, errors.New("缺少必要参数")
  689. }
  690. return StmtForQueryList(this.stmt, this.value)
  691. }
  692. /**
  693. * 执行查询一条数据
  694. * return row error
  695. */
  696. func (this *TxQuery) Find() (map[string]string, error) {
  697. this.page = 1
  698. this.page_size = 1
  699. err := this.QueryStmt()
  700. if err != nil {
  701. return map[string]string{}, err
  702. }
  703. if this.stmt == nil {
  704. return nil, errors.New("缺少必要参数")
  705. }
  706. return StmtForQueryRow(this.stmt, this.value)
  707. }
  708. /**
  709. * 执行更新
  710. * return is_updated error
  711. */
  712. func (this *TxQuery) Update() (int64, error) {
  713. err := this.UpdateStmt()
  714. if err != nil {
  715. return 0, err
  716. }
  717. return StmtForUpdateExec(this.stmt, this.value)
  718. }
  719. // UpdateAll 批量更新(根据唯一键判断存在则更新,不存在则创建)
  720. func (this *TxQuery) UpdateAll() (int64, error) {
  721. err := this.UpdateAllStmt()
  722. if err != nil {
  723. return 0, err
  724. }
  725. return StmtForUpdateExec(this.stmt, this.value)
  726. }
  727. /**
  728. * 执行删除
  729. * return is_delete error
  730. */
  731. func (this *TxQuery) Delete() (int64, error) {
  732. err := this.DeleteStmt()
  733. if err != nil {
  734. return 0, err
  735. }
  736. return StmtForUpdateExec(this.stmt, this.value)
  737. }
  738. /**
  739. * 执行写入
  740. * return is_insert error
  741. */
  742. func (this *TxQuery) Create() (int64, error) {
  743. err := this.CreateStmt()
  744. if err != nil {
  745. return 0, err
  746. }
  747. return StmtForInsertExec(this.stmt, this.value)
  748. }
  749. /**
  750. * 执行批量写入
  751. * return is_insert error
  752. */
  753. func (this *TxQuery) CreateAll() (int64, error) {
  754. err := this.CreateAllStmt()
  755. if err != nil {
  756. return 0, err
  757. }
  758. return StmtForInsertExec(this.stmt, this.value)
  759. }
  760. /**
  761. * 提交
  762. */
  763. func (this *TxQuery) Commit() error {
  764. return this.tx.Commit()
  765. }
  766. /**
  767. * 回滚
  768. */
  769. func (this *TxQuery) Rollback() error {
  770. return this.tx.Rollback()
  771. }