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

890 lines
20 KiB

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