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

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