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

886 lines
20 KiB

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