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

875 lines
20 KiB

2 years ago
2 years ago
2 years ago
2 years 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. if strings.Contains(this.table, "select ") || strings.HasPrefix(this.table, "(") {
  207. table = this.table
  208. } else {
  209. table = getTableName(this.dbname, this.table)
  210. }
  211. var sql, title string
  212. if this.title != "" {
  213. title = this.title
  214. } else {
  215. title = "*"
  216. }
  217. withSql := ""
  218. if len(this.with) > 0 {
  219. var builder strings.Builder
  220. builder.WriteString("WITH ")
  221. boo := false
  222. for k, v := range this.with {
  223. if len(v) < 2 {
  224. continue
  225. }
  226. if k != 0 {
  227. builder.WriteString(", ")
  228. }
  229. builder.WriteString(v[1])
  230. builder.WriteString(" as (")
  231. builder.WriteString(v[0])
  232. builder.WriteString(")")
  233. boo = true
  234. }
  235. if boo {
  236. builder.WriteString(" ")
  237. withSql = builder.String()
  238. }
  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. for _, joinitem := range this.join {
  247. if len(joinitem) < 2 {
  248. continue
  249. }
  250. if len(joinitem) == 3 {
  251. sql = helper.StringJoin(sql, " ", joinitem[2], " join ", getTableName(this.dbname, joinitem[0]), " on ", joinitem[1])
  252. } else { //默认左连接
  253. sql = helper.StringJoin(sql, " left join ", getTableName(this.dbname, joinitem[0]), " on ", joinitem[1])
  254. }
  255. }
  256. }
  257. if len(this.where) > 0 || len(this.where_or) > 0 {
  258. sql = helper.StringJoin(sql, " where ")
  259. }
  260. if len(this.where) > 0 {
  261. sql = helper.StringJoin(sql, " (", strings.Join(this.where, " and "), " ) ")
  262. }
  263. if len(this.where_or) > 0 {
  264. if len(this.where) > 0 {
  265. sql = helper.StringJoin(sql, " or ", strings.Join(this.where_or, " or "))
  266. } else {
  267. sql = helper.StringJoin(sql, strings.Join(this.where_or, " or "))
  268. }
  269. }
  270. if this.groupby != "" {
  271. sql = helper.StringJoin(sql, " group by ", this.groupby)
  272. }
  273. if this.having != "" {
  274. sql = helper.StringJoin(sql, " having ", this.having)
  275. }
  276. if this.orderby != "" {
  277. sql = helper.StringJoin(sql, " order by ", this.orderby)
  278. }
  279. if this.page > 0 || this.page_size > 0 {
  280. if this.page < 1 {
  281. this.page = 1
  282. }
  283. if this.page_size < 1 {
  284. this.page_size = 10
  285. }
  286. from := strconv.Itoa((this.page - 1) * this.page_size)
  287. offset := strconv.Itoa(this.page_size)
  288. if from != "" && offset != "" {
  289. sql = helper.StringJoin(sql, " limit ", from, " , ", offset)
  290. }
  291. }
  292. if this.debug {
  293. log.Println("query sql:", sql, this.value)
  294. }
  295. condition_len := 0 //所有条件数
  296. for _, ch2 := range sql {
  297. if string(ch2) == "?" {
  298. condition_len++
  299. }
  300. }
  301. if condition_len != len(this.value) {
  302. return nil, errors.New("参数错误,条件值错误")
  303. }
  304. return map[string]interface{}{
  305. "sql": sql,
  306. "value": this.value,
  307. }, nil
  308. }
  309. // 获取表格信息
  310. func (this *TxQuery) GetTableInfo(table string) (map[string]interface{}, error) {
  311. field := []string{
  312. "COLUMN_NAME", //字段名
  313. "COLUMN_DEFAULT", //默认值
  314. "DATA_TYPE", //数据类型
  315. "COLUMN_TYPE", //数据类型+长度
  316. "COLUMN_COMMENT", //备注
  317. "IS_NULLABLE", //是否为空
  318. }
  319. sql := "select `" + strings.Join(field, "`,`") + "` from information_schema.COLUMNS where table_name = ? and table_schema = ?"
  320. stmtSql, err := this.tx.Prepare(sql)
  321. if err != nil {
  322. return nil, err
  323. }
  324. list, err := StmtForQueryList(stmtSql, []interface{}{table, this.dbname})
  325. if err != nil {
  326. return nil, err
  327. }
  328. rows := make([]interface{}, 0, len(list))
  329. fieldName := make([]string, 0, len(list))
  330. for _, item := range list {
  331. info := map[string]interface{}{
  332. "name": "",
  333. "column_type": "",
  334. "is_null": true,
  335. "data_type": "",
  336. "comment": "",
  337. "default": "",
  338. }
  339. for _, k := range field {
  340. index := helper.StrFirstToUpper(k)
  341. if v, ok := item[index]; ok {
  342. switch k {
  343. case "COLUMN_NAME":
  344. info["name"] = v
  345. case "COLUMN_DEFAULT":
  346. info["default"] = v
  347. case "DATA_TYPE":
  348. info["data_type"] = v
  349. case "COLUMN_TYPE":
  350. info["column_type"] = helper.ToInt64(v)
  351. case "COLUMN_COMMENT":
  352. info["comment"] = helper.ToInt64(v)
  353. case "IS_NULLABLE":
  354. if v == "NO" {
  355. info["is_null"] = false
  356. }
  357. }
  358. }
  359. }
  360. name := helper.ToStr(info["name"])
  361. if name != "" {
  362. rows = append(rows, info)
  363. fieldName = append(fieldName, name)
  364. }
  365. }
  366. return map[string]interface{}{
  367. "field": fieldName,
  368. "list": rows,
  369. }, nil
  370. }
  371. // 拼查询sql
  372. func (this *TxQuery) QueryStmt() error {
  373. res := map[string]interface{}{}
  374. res, err = this.BuildSelectSql()
  375. if err != nil {
  376. return err
  377. }
  378. sql := helper.ToStr(res["sql"])
  379. stmt, err = this.tx.Prepare(sql + " FOR UPDATE")
  380. if err != nil {
  381. return err
  382. }
  383. this.stmt = stmt
  384. return nil
  385. }
  386. // 拼更新sql
  387. func (this *TxQuery) UpdateStmt() error {
  388. if this.dbname == "" && this.table == "" {
  389. return errors.New("参数错误,没有数据表")
  390. }
  391. if len(this.where) < 1 {
  392. return errors.New("参数错误,缺少条件")
  393. }
  394. dbName := getTableName(this.dbname, this.table)
  395. var sql string
  396. sql = helper.StringJoin("update ", dbName, " set ", strings.Join(this.data, " , "))
  397. sql = helper.StringJoin(sql, " where ", strings.Join(this.where, " and "))
  398. if this.debug {
  399. log.Println("update sql:", sql, this.value)
  400. }
  401. condition_len := 0 //所有条件数
  402. for _, ch2 := range sql {
  403. if string(ch2) == "?" {
  404. condition_len++
  405. }
  406. }
  407. if condition_len != len(this.value) {
  408. return errors.New("参数错误,条件值错误")
  409. }
  410. stmt, err = this.tx.Prepare(sql)
  411. if err != nil {
  412. return err
  413. }
  414. this.stmt = stmt
  415. return nil
  416. }
  417. // 拼批量存在更新不存在插入sql
  418. func (this *TxQuery) UpdateAllStmt() error {
  419. if this.dbname == "" && this.table == "" {
  420. return errors.New("参数错误,没有数据表")
  421. }
  422. dbName := getTableName(this.dbname, this.table)
  423. var sql string
  424. var dataSql []string //一组用到的占位字符
  425. var valSql []string //占位字符组
  426. var updSql []string //更新字段的sql
  427. var updFieldLen = len(this.upd_field) //需要更新的字段数量,为0时更新除id外添加值
  428. dataLen := len(this.save_data)
  429. if dataLen > 0 {
  430. //批量操作
  431. this.data = this.data[0:0]
  432. this.value = this.value[0:0]
  433. var dataSqlText string //占位字符组
  434. for i := 0; i < dataLen; i++ {
  435. if i == 0 {
  436. //第一组时分配变量空间
  437. fieldLen := len(this.save_data[i])
  438. this.data = make([]string, 0, fieldLen)
  439. dataSql = make([]string, 0, fieldLen)
  440. this.value = make([]interface{}, 0, fieldLen*dataLen)
  441. valSql = make([]string, 0, dataLen)
  442. switch updFieldLen {
  443. case 0:
  444. //预览创建数据的长度
  445. updSql = make([]string, 0, fieldLen)
  446. default:
  447. //按照需要更新字段数长度
  448. updSql = make([]string, 0, updFieldLen)
  449. for _, k := range this.upd_field {
  450. updSql = append(updSql, k+"=values("+k+")") //存储需要更新的字段
  451. }
  452. }
  453. for k := range this.save_data[i] {
  454. this.data = append(this.data, k) //存储添加的字段
  455. dataSql = append(dataSql, "?") //存储需要的占位符
  456. if updFieldLen == 0 && k != "id" {
  457. updSql = append(updSql, k+"=values("+k+")") //存储需要更新的字段
  458. }
  459. }
  460. dataSqlText = strings.Join(dataSql, ",") //组成每组占位字符格式
  461. }
  462. for j := 0; j < len(this.data); j++ {
  463. this.value = append(this.value, this.save_data[i][this.data[j]]) //存储值
  464. }
  465. valSql = append(valSql, "("+dataSqlText+")") //组成占位字符组
  466. }
  467. } else {
  468. //添加一条(原理同上)
  469. fieldLen := len(this.data)
  470. dataSql = make([]string, 0, fieldLen)
  471. valSql = make([]string, 0, 1)
  472. switch updFieldLen {
  473. case 0:
  474. updSql = make([]string, 0, fieldLen)
  475. default:
  476. updSql = make([]string, 0, updFieldLen)
  477. for _, k := range this.upd_field {
  478. updSql = append(updSql, k+"=values("+k+")")
  479. }
  480. }
  481. for i := 0; i < fieldLen; i++ {
  482. dataSql = append(dataSql, "?")
  483. if updFieldLen == 0 && this.data[i] != "id" {
  484. updSql = append(updSql, this.data[i]+"=values("+this.data[i]+")")
  485. }
  486. }
  487. if updFieldLen > 0 {
  488. for _, k := range this.upd_field {
  489. updSql = append(updSql, k+"=values("+k+")")
  490. }
  491. }
  492. valSql = append(valSql, "("+strings.Join(dataSql, " , ")+")")
  493. }
  494. if len(this.data) == 0 {
  495. return errors.New("参数错误,没有字段值")
  496. }
  497. if len(this.value) == 0 {
  498. return errors.New("参数错误,条件值错误")
  499. }
  500. setText := " values "
  501. if len(valSql) > 1 {
  502. setText = " value "
  503. }
  504. sql = helper.StringJoin("insert into ", dbName, " (", strings.Join(this.data, " , "), ")", setText, strings.Join(valSql, ","), " ON DUPLICATE KEY UPDATE ", strings.Join(updSql, " , "))
  505. if this.debug {
  506. log.Println("insert on duplicate key update sql:", sql, this.value)
  507. }
  508. conditionLen := 0 //所有条件数
  509. for _, ch2 := range sql {
  510. if string(ch2) == "?" {
  511. conditionLen++
  512. }
  513. }
  514. if conditionLen != len(this.value) {
  515. return errors.New("参数错误,条件值数量不匹配")
  516. }
  517. stmt, err = this.tx.Prepare(sql)
  518. if err != nil {
  519. return err
  520. }
  521. this.stmt = stmt
  522. return nil
  523. }
  524. // 拼插入sql
  525. func (this *TxQuery) CreateStmt() error {
  526. if this.dbname == "" && this.table == "" {
  527. return errors.New("参数错误,没有数据表")
  528. }
  529. dbName := getTableName(this.dbname, this.table)
  530. var sql string
  531. sql = helper.StringJoin("insert into ", dbName, " set ", strings.Join(this.data, " , "))
  532. if this.debug {
  533. log.Println("insert sql:", sql, this.value)
  534. }
  535. condition_len := 0 //所有条件数
  536. for _, ch2 := range sql {
  537. if string(ch2) == "?" {
  538. condition_len++
  539. }
  540. }
  541. if condition_len != len(this.value) {
  542. return errors.New("参数错误,条件值错误")
  543. }
  544. stmt, err = this.tx.Prepare(sql)
  545. if err != nil {
  546. return err
  547. }
  548. this.stmt = stmt
  549. return nil
  550. }
  551. // 拼批量插入sql
  552. func (this *TxQuery) CreateAllStmt() error {
  553. if this.dbname == "" && this.table == "" {
  554. return errors.New("参数错误,没有数据表")
  555. }
  556. dbName := getTableName(this.dbname, this.table)
  557. var sql string
  558. var dataSql []string //一组用到的占位字符
  559. var valSql []string //占位字符组
  560. dataLen := len(this.save_data)
  561. if dataLen > 0 {
  562. //清空字段和值
  563. this.data = this.data[0:0]
  564. this.value = this.value[0:0]
  565. var dataSqlText string //占位字符组
  566. for i := 0; i < dataLen; i++ {
  567. if i == 0 {
  568. //第一组时分配变量空间
  569. fieldLen := len(this.save_data[i])
  570. this.data = make([]string, 0, fieldLen)
  571. dataSql = make([]string, 0, fieldLen)
  572. this.value = make([]interface{}, 0, fieldLen*dataLen)
  573. valSql = make([]string, 0, dataLen)
  574. for k := range this.save_data[i] {
  575. this.data = append(this.data, k) //存储字段
  576. dataSql = append(dataSql, "?") //存储需要的占位符
  577. }
  578. dataSqlText = strings.Join(dataSql, ",") //组成每组占位字符格式
  579. }
  580. for j := 0; j < len(this.data); j++ {
  581. this.value = append(this.value, this.save_data[i][this.data[j]]) //存储值
  582. }
  583. valSql = append(valSql, "("+dataSqlText+")") //组成占位字符组
  584. }
  585. } else {
  586. //添加一条(原理同上)
  587. fieldLen := len(this.data)
  588. dataSql = make([]string, 0, fieldLen)
  589. for i := 0; i < fieldLen; i++ {
  590. dataSql = append(dataSql, "?")
  591. }
  592. valSql = make([]string, 0, 1)
  593. valSql = append(valSql, "("+strings.Join(dataSql, " , ")+")")
  594. }
  595. if len(this.data) == 0 {
  596. return errors.New("参数错误,字段名错误")
  597. }
  598. if len(this.value) == 0 {
  599. return errors.New("参数错误,条件值错误")
  600. }
  601. //通过sql关键字优化批量操作和单个操作效率
  602. setText := " values "
  603. if len(valSql) > 1 {
  604. setText = " value "
  605. }
  606. sql = helper.StringJoin("insert into ", dbName, " (", strings.Join(this.data, " , "), ")", setText, strings.Join(valSql, ","))
  607. if len(this.value) == 0 {
  608. return errors.New("参数错误,条件值错误")
  609. }
  610. if this.debug {
  611. log.Println("insert sql:", sql, this.value)
  612. }
  613. conditionLen := 0 //所有条件数
  614. for _, ch2 := range sql {
  615. if string(ch2) == "?" {
  616. conditionLen++
  617. }
  618. }
  619. if conditionLen != len(this.value) {
  620. return errors.New("参数错误,条件值数量不匹配")
  621. }
  622. stmt, err = this.tx.Prepare(sql)
  623. if err != nil {
  624. return err
  625. }
  626. this.stmt = stmt
  627. return nil
  628. }
  629. // 拼删除sql
  630. func (this *TxQuery) DeleteStmt() error {
  631. if this.dbname == "" && this.table == "" {
  632. return errors.New("参数错误,没有数据表")
  633. }
  634. if len(this.where) < 1 {
  635. return errors.New("参数错误,缺少条件")
  636. }
  637. dbName := getTableName(this.dbname, this.table)
  638. var sql string
  639. sql = helper.StringJoin("delete from ", dbName, " where ", strings.Join(this.where, " and "))
  640. if this.page_size > 0 {
  641. sql = helper.StringJoin(sql, " limit ", strconv.Itoa(this.page_size))
  642. }
  643. if this.debug {
  644. log.Println("delete sql:", sql, this.value)
  645. }
  646. condition_len := 0 //所有条件数
  647. for _, ch2 := range sql {
  648. if string(ch2) == "?" {
  649. condition_len++
  650. }
  651. }
  652. if condition_len != len(this.value) {
  653. return errors.New("参数错误,条件值错误")
  654. }
  655. stmt, err = this.tx.Prepare(sql)
  656. if err != nil {
  657. return err
  658. }
  659. this.stmt = stmt
  660. return nil
  661. }
  662. /**
  663. * 执行查询列表
  664. * return list error
  665. */
  666. func (this *TxQuery) Select() ([]map[string]string, error) {
  667. err := this.QueryStmt()
  668. if err != nil {
  669. return []map[string]string{}, err
  670. }
  671. if this.stmt == nil {
  672. return []map[string]string{}, errors.New("缺少必要参数")
  673. }
  674. return StmtForQueryList(this.stmt, this.value)
  675. }
  676. /**
  677. * 执行查询一条数据
  678. * return row error
  679. */
  680. func (this *TxQuery) Find() (map[string]string, error) {
  681. this.page = 1
  682. this.page_size = 1
  683. err := this.QueryStmt()
  684. if err != nil {
  685. return map[string]string{}, err
  686. }
  687. if this.stmt == nil {
  688. return nil, errors.New("缺少必要参数")
  689. }
  690. return StmtForQueryRow(this.stmt, this.value)
  691. }
  692. /**
  693. * 执行更新
  694. * return is_updated error
  695. */
  696. func (this *TxQuery) Update() (int64, error) {
  697. err := this.UpdateStmt()
  698. if err != nil {
  699. return 0, err
  700. }
  701. return StmtForUpdateExec(this.stmt, this.value)
  702. }
  703. // UpdateAll 批量更新(根据唯一键判断存在则更新,不存在则创建)
  704. func (this *TxQuery) UpdateAll() (int64, error) {
  705. err := this.UpdateAllStmt()
  706. if err != nil {
  707. return 0, err
  708. }
  709. return StmtForUpdateExec(this.stmt, this.value)
  710. }
  711. /**
  712. * 执行删除
  713. * return is_delete error
  714. */
  715. func (this *TxQuery) Delete() (int64, error) {
  716. err := this.DeleteStmt()
  717. if err != nil {
  718. return 0, err
  719. }
  720. return StmtForUpdateExec(this.stmt, this.value)
  721. }
  722. /**
  723. * 执行写入
  724. * return is_insert error
  725. */
  726. func (this *TxQuery) Create() (int64, error) {
  727. err := this.CreateStmt()
  728. if err != nil {
  729. return 0, err
  730. }
  731. return StmtForInsertExec(this.stmt, this.value)
  732. }
  733. /**
  734. * 执行批量写入
  735. * return is_insert error
  736. */
  737. func (this *TxQuery) CreateAll() (int64, error) {
  738. err := this.CreateAllStmt()
  739. if err != nil {
  740. return 0, err
  741. }
  742. return StmtForInsertExec(this.stmt, this.value)
  743. }
  744. /**
  745. * 提交
  746. */
  747. func (this *TxQuery) Commit() error {
  748. return this.tx.Commit()
  749. }
  750. /**
  751. * 回滚
  752. */
  753. func (this *TxQuery) Rollback() error {
  754. return this.tx.Rollback()
  755. }