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

863 lines
19 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. func (this *Query) Data(data string) *Query {
  135. this.data = append(this.data, data)
  136. return this
  137. }
  138. func (this *Query) Datas(datas []string) *Query {
  139. this.data = append(this.data, datas...)
  140. return this
  141. }
  142. func (this *Query) Debug(debug bool) *Query {
  143. this.debug = debug
  144. return this
  145. }
  146. /*
  147. * 清理上次查询
  148. */
  149. func (this *Query) Clean() *Query {
  150. this.title = ""
  151. this.where = this.where[0:0]
  152. this.where_or = this.where_or[0:0]
  153. this.join = this.join[0:0]
  154. this.data = this.data[0:0]
  155. this.value = this.value[0:0]
  156. this.orderby = ""
  157. this.groupby = ""
  158. this.page = 0
  159. this.page_size = 0
  160. this.save_data = this.save_data[0:0]
  161. this.upd_field = this.upd_field[0:0]
  162. this.having = ""
  163. return this
  164. }
  165. // 获取表格信息
  166. func (this *Query) GetTableInfo(table string) (map[string]interface{}, error) {
  167. field := []string{
  168. "COLUMN_NAME", //字段名
  169. "COLUMN_DEFAULT", //默认值
  170. "DATA_TYPE", //数据类型
  171. "COLUMN_TYPE", //数据类型+长度
  172. "COLUMN_COMMENT", //备注
  173. "IS_NULLABLE", //是否为空
  174. }
  175. sql := "select `" + strings.Join(field, "`,`") + "` from information_schema.COLUMNS where table_name = ? and table_schema = ?"
  176. if this.conn == nil {
  177. this.conn = DB
  178. }
  179. stmtSql, err := this.conn.Prepare(sql)
  180. if err != nil {
  181. return nil, err
  182. }
  183. list, err := StmtForQueryList(stmtSql, []interface{}{table, this.dbname})
  184. if err != nil {
  185. return nil, err
  186. }
  187. rows := make([]interface{}, 0, len(list))
  188. fieldName := make([]string, 0, len(list))
  189. for _, item := range list {
  190. info := map[string]interface{}{
  191. "name": "",
  192. "column_type": "",
  193. "is_null": true,
  194. "data_type": "",
  195. "comment": "",
  196. "default": "",
  197. }
  198. for _, k := range field {
  199. index := helper.StrFirstToUpper(k)
  200. if v, ok := item[index]; ok {
  201. switch k {
  202. case "COLUMN_NAME":
  203. info["name"] = v
  204. case "COLUMN_DEFAULT":
  205. info["default"] = v
  206. case "DATA_TYPE":
  207. info["data_type"] = v
  208. case "COLUMN_TYPE":
  209. info["column_type"] = helper.ToInt64(v)
  210. case "COLUMN_COMMENT":
  211. info["comment"] = helper.ToInt64(v)
  212. case "IS_NULLABLE":
  213. if v == "NO" {
  214. info["is_null"] = false
  215. }
  216. }
  217. }
  218. }
  219. name := helper.ToStr(info["name"])
  220. if name != "" {
  221. rows = append(rows, info)
  222. fieldName = append(fieldName, name)
  223. }
  224. }
  225. return map[string]interface{}{
  226. "field": fieldName,
  227. "list": rows,
  228. }, nil
  229. }
  230. // 构造子查询
  231. func (this *Query) BuildSelectSql() (map[string]interface{}, error) {
  232. if this.dbname == "" && this.table == "" {
  233. return nil, errors.New("参数错误,没有数据表")
  234. }
  235. var table = ""
  236. if strings.Contains(this.table, "select ") {
  237. table = this.table
  238. } else {
  239. table = getTableName(this.dbname, this.table, this.dbtype)
  240. }
  241. // var err error
  242. var sql, title string
  243. if this.title != "" {
  244. title = this.title
  245. } else {
  246. title = "*"
  247. }
  248. if this.dbtype == "mssql" {
  249. if this.page_size > 0 {
  250. sql = helper.StringJoin("select top ", helper.ToStr(this.page_size), " ")
  251. } else {
  252. sql = "select "
  253. }
  254. } else {
  255. if DB_PROVIDER == "TencentDB" {
  256. sql = "/*slave*/ select "
  257. } else {
  258. sql = "select "
  259. }
  260. }
  261. sql = helper.StringJoin(sql, title)
  262. if this.alias != "" {
  263. table = helper.StringJoin(table, " as ", this.alias)
  264. }
  265. sql = helper.StringJoin(sql, " from ", table)
  266. if len(this.join) > 0 {
  267. for _, joinitem := range this.join {
  268. if len(joinitem) < 2 {
  269. continue
  270. }
  271. if len(joinitem) == 3 {
  272. sql = helper.StringJoin(sql, " ", joinitem[2], " join ", getTableName(this.dbname, joinitem[0], this.dbtype), " on ", joinitem[1])
  273. } else { //默认左连接
  274. sql = helper.StringJoin(sql, " left join ", getTableName(this.dbname, joinitem[0], this.dbtype), " on ", joinitem[1])
  275. }
  276. }
  277. }
  278. if len(this.where) > 0 || len(this.where_or) > 0 {
  279. sql = helper.StringJoin(sql, " where ")
  280. }
  281. if len(this.where) > 0 {
  282. sql = helper.StringJoin(sql, " (", strings.Join(this.where, " and "), " ) ")
  283. }
  284. if len(this.where_or) > 0 {
  285. if len(this.where) > 0 {
  286. sql = helper.StringJoin(sql, " or ", strings.Join(this.where_or, " or "))
  287. } else {
  288. sql = helper.StringJoin(sql, strings.Join(this.where_or, " or "))
  289. }
  290. }
  291. if this.groupby != "" {
  292. sql = helper.StringJoin(sql, " group by ", this.groupby)
  293. }
  294. if this.having != "" {
  295. sql = helper.StringJoin(sql, " having ", this.having)
  296. }
  297. if this.orderby != "" {
  298. sql = helper.StringJoin(sql, " order by ", this.orderby)
  299. }
  300. if this.dbtype == "mysql" && (this.page > 0 || this.page_size > 0) {
  301. if this.page < 1 {
  302. this.page = 1
  303. }
  304. if this.page_size < 1 {
  305. this.page_size = 10
  306. }
  307. from := strconv.Itoa((this.page - 1) * this.page_size)
  308. offset := strconv.Itoa(this.page_size)
  309. if from != "" && offset != "" {
  310. sql = helper.StringJoin(sql, " limit ", from, " , ", offset)
  311. }
  312. }
  313. if this.debug {
  314. log.Println("query sql:", sql, this.value)
  315. }
  316. condition_len := 0 //所有条件数
  317. for _, ch2 := range sql {
  318. if string(ch2) == "?" {
  319. condition_len++
  320. }
  321. }
  322. if condition_len != len(this.value) {
  323. return nil, errors.New("参数错误,条件值错误")
  324. }
  325. return map[string]interface{}{
  326. "sql": sql,
  327. "value": this.value,
  328. }, nil
  329. }
  330. // 拼查询sql
  331. func (this *Query) QueryStmt() error {
  332. res := map[string]interface{}{}
  333. res, err = this.BuildSelectSql()
  334. if err != nil {
  335. return err
  336. }
  337. sql := helper.ToStr(res["sql"])
  338. if SLAVER_DB != nil {
  339. this.conn = SLAVER_DB
  340. }
  341. // else {
  342. // this.conn = DB
  343. // }
  344. if this.conn == nil {
  345. this.conn = DB
  346. }
  347. stmt, err = this.conn.Prepare(sql)
  348. if err != nil {
  349. return err
  350. }
  351. this.stmt = stmt
  352. return nil
  353. }
  354. // 拼更新sql
  355. func (this *Query) UpdateStmt() error {
  356. if this.dbname == "" && this.table == "" {
  357. return errors.New("参数错误,没有数据表")
  358. }
  359. if len(this.where) < 1 {
  360. return errors.New("参数错误,缺少条件")
  361. }
  362. dbName := getTableName(this.dbname, this.table, this.dbtype)
  363. var sql string
  364. sql = helper.StringJoin("update ", dbName, " set ", strings.Join(this.data, " , "))
  365. sql = helper.StringJoin(sql, " where ", strings.Join(this.where, " and "))
  366. if this.debug {
  367. log.Println("update sql:", sql, this.value)
  368. }
  369. condition_len := 0 //所有条件数
  370. for _, ch2 := range sql {
  371. if string(ch2) == "?" {
  372. condition_len++
  373. }
  374. }
  375. if condition_len != len(this.value) {
  376. return errors.New("参数错误,条件值错误")
  377. }
  378. if this.conn == nil {
  379. this.conn = DB
  380. }
  381. stmt, err = this.conn.Prepare(sql)
  382. if err != nil {
  383. return err
  384. }
  385. this.stmt = stmt
  386. return nil
  387. }
  388. // 拼批量存在更新不存在插入sql
  389. func (this *Query) UpdateAllStmt() error {
  390. if this.dbname == "" && this.table == "" {
  391. return errors.New("参数错误,没有数据表")
  392. }
  393. dbName := getTableName(this.dbname, this.table)
  394. var sql string
  395. var dataSql []string //一组用到的占位字符
  396. var valSql []string //占位字符组
  397. var updSql []string //更新字段的sql
  398. var updFieldLen = len(this.upd_field) //需要更新的字段数量,为0时更新除id外添加值
  399. dataLen := len(this.save_data)
  400. if dataLen > 0 {
  401. //批量操作
  402. this.data = this.data[0:0]
  403. this.value = this.value[0:0]
  404. var dataSqlText string //占位字符组
  405. for i := 0; i < dataLen; i++ {
  406. if i == 0 {
  407. //第一组时分配变量空间
  408. fieldLen := len(this.save_data[i])
  409. this.data = make([]string, 0, fieldLen)
  410. dataSql = make([]string, 0, fieldLen)
  411. this.value = make([]interface{}, 0, fieldLen*dataLen)
  412. valSql = make([]string, 0, dataLen)
  413. switch updFieldLen {
  414. case 0:
  415. //预览创建数据的长度
  416. updSql = make([]string, 0, fieldLen)
  417. default:
  418. //按照需要更新字段数长度
  419. updSql = make([]string, 0, updFieldLen)
  420. for _, k := range this.upd_field {
  421. updSql = append(updSql, k+"=values("+k+")") //存储需要更新的字段
  422. }
  423. }
  424. for k := range this.save_data[i] {
  425. this.data = append(this.data, k) //存储添加的字段
  426. dataSql = append(dataSql, "?") //存储需要的占位符
  427. if updFieldLen == 0 && k != "id" {
  428. updSql = append(updSql, k+"=values("+k+")") //存储需要更新的字段
  429. }
  430. }
  431. dataSqlText = strings.Join(dataSql, ",") //组成每组占位字符格式
  432. }
  433. for j := 0; j < len(this.data); j++ {
  434. this.value = append(this.value, this.save_data[i][this.data[j]]) //存储值
  435. }
  436. valSql = append(valSql, "("+dataSqlText+")") //组成占位字符组
  437. }
  438. } else {
  439. //添加一条(原理同上)
  440. fieldLen := len(this.data)
  441. dataSql = make([]string, 0, fieldLen)
  442. valSql = make([]string, 0, 1)
  443. switch updFieldLen {
  444. case 0:
  445. updSql = make([]string, 0, fieldLen)
  446. default:
  447. updSql = make([]string, 0, updFieldLen)
  448. for _, k := range this.upd_field {
  449. updSql = append(updSql, k+"=values("+k+")")
  450. }
  451. }
  452. for i := 0; i < fieldLen; i++ {
  453. dataSql = append(dataSql, "?")
  454. if updFieldLen == 0 && this.data[i] != "id" {
  455. updSql = append(updSql, this.data[i]+"=values("+this.data[i]+")")
  456. }
  457. }
  458. if updFieldLen > 0 {
  459. for _, k := range this.upd_field {
  460. updSql = append(updSql, k+"=values("+k+")")
  461. }
  462. }
  463. valSql = append(valSql, "("+strings.Join(dataSql, " , ")+")")
  464. }
  465. if len(this.data) == 0 {
  466. return errors.New("参数错误,没有字段值")
  467. }
  468. if len(this.value) == 0 {
  469. return errors.New("参数错误,条件值错误")
  470. }
  471. setText := " values "
  472. if len(valSql) > 1 {
  473. setText = " value "
  474. }
  475. sql = helper.StringJoin("insert into ", dbName, " (", strings.Join(this.data, " , "), ")", setText, strings.Join(valSql, ","), " ON DUPLICATE KEY UPDATE ", strings.Join(updSql, " , "))
  476. if this.debug {
  477. log.Println("insert on duplicate key update sql:", sql, this.value)
  478. }
  479. conditionLen := 0 //所有条件数
  480. for _, ch2 := range sql {
  481. if string(ch2) == "?" {
  482. conditionLen++
  483. }
  484. }
  485. if conditionLen != len(this.value) {
  486. return errors.New("参数错误,条件值数量不匹配")
  487. }
  488. if this.conn == nil {
  489. this.conn = DB
  490. }
  491. stmt, err = this.conn.Prepare(sql)
  492. if err != nil {
  493. return err
  494. }
  495. this.stmt = stmt
  496. return nil
  497. }
  498. // 拼批量插入sql
  499. func (this *Query) CreateAllStmt() error {
  500. if this.dbname == "" && this.table == "" {
  501. return errors.New("参数错误,没有数据表")
  502. }
  503. dbName := getTableName(this.dbname, this.table)
  504. var sql string
  505. var dataSql []string //一组用到的占位字符
  506. var valSql []string //占位字符组
  507. dataLen := len(this.save_data)
  508. if dataLen > 0 {
  509. //清空字段和值
  510. this.data = this.data[0:0]
  511. this.value = this.value[0:0]
  512. var dataSqlText string //占位字符组
  513. for i := 0; i < dataLen; i++ {
  514. if i == 0 {
  515. //第一组时分配变量空间
  516. fieldLen := len(this.save_data[i])
  517. this.data = make([]string, 0, fieldLen)
  518. dataSql = make([]string, 0, fieldLen)
  519. this.value = make([]interface{}, 0, fieldLen*dataLen)
  520. valSql = make([]string, 0, dataLen)
  521. for k := range this.save_data[i] {
  522. this.data = append(this.data, k) //存储字段
  523. dataSql = append(dataSql, "?") //存储需要的占位符
  524. }
  525. dataSqlText = strings.Join(dataSql, ",") //组成每组占位字符格式
  526. }
  527. for j := 0; j < len(this.data); j++ {
  528. this.value = append(this.value, this.save_data[i][this.data[j]]) //存储值
  529. }
  530. valSql = append(valSql, "("+dataSqlText+")") //组成占位字符组
  531. }
  532. } else {
  533. //添加一条(原理同上)
  534. fieldLen := len(this.data)
  535. dataSql = make([]string, 0, fieldLen)
  536. for i := 0; i < fieldLen; i++ {
  537. dataSql = append(dataSql, "?")
  538. }
  539. valSql = make([]string, 0, 1)
  540. valSql = append(valSql, "("+strings.Join(dataSql, " , ")+")")
  541. }
  542. if len(this.data) == 0 {
  543. return errors.New("参数错误,字段名错误")
  544. }
  545. if len(this.value) == 0 {
  546. return errors.New("参数错误,条件值错误")
  547. }
  548. //通过sql关键字优化批量操作和单个操作效率
  549. setText := " values "
  550. if len(valSql) > 1 {
  551. setText = " value "
  552. }
  553. sql = helper.StringJoin("insert into ", dbName, " (", strings.Join(this.data, " , "), ")", setText, strings.Join(valSql, ","))
  554. if this.debug {
  555. log.Println("insert sql:", sql, this.value)
  556. }
  557. conditionLen := 0 //所有条件数
  558. for _, ch2 := range sql {
  559. if string(ch2) == "?" {
  560. conditionLen++
  561. }
  562. }
  563. if conditionLen != len(this.value) {
  564. return errors.New("参数错误,条件值数量不匹配")
  565. }
  566. if this.conn == nil {
  567. this.conn = DB
  568. }
  569. stmt, err = this.conn.Prepare(sql)
  570. if err != nil {
  571. return err
  572. }
  573. this.stmt = stmt
  574. return nil
  575. }
  576. // 拼插入sql
  577. func (this *Query) CreateStmt() error {
  578. if this.dbname == "" && this.table == "" {
  579. return errors.New("参数错误,没有数据表")
  580. }
  581. dbName := getTableName(this.dbname, this.table, this.dbtype)
  582. var sql string
  583. sql = helper.StringJoin("insert into ", dbName, " set ", strings.Join(this.data, " , "))
  584. if this.debug {
  585. log.Println("insert sql:", sql, this.value)
  586. }
  587. condition_len := 0 //所有条件数
  588. for _, ch2 := range sql {
  589. if string(ch2) == "?" {
  590. condition_len++
  591. }
  592. }
  593. if condition_len != len(this.value) {
  594. return errors.New("参数错误,条件值错误")
  595. }
  596. if this.conn == nil {
  597. this.conn = DB
  598. }
  599. stmt, err = this.conn.Prepare(sql)
  600. if err != nil {
  601. return err
  602. }
  603. this.stmt = stmt
  604. return nil
  605. }
  606. // 拼删除sql
  607. func (this *Query) DeleteStmt() error {
  608. if this.dbname == "" && this.table == "" {
  609. return errors.New("参数错误,没有数据表")
  610. }
  611. if len(this.where) < 1 {
  612. return errors.New("参数错误,缺少条件")
  613. }
  614. dbName := getTableName(this.dbname, this.table, this.dbtype)
  615. var sql string
  616. sql = helper.StringJoin("delete from ", dbName, " where ", strings.Join(this.where, " and "))
  617. if this.page_size > 0 {
  618. sql = helper.StringJoin(sql, " limit ", strconv.Itoa(this.page_size))
  619. }
  620. if this.debug {
  621. log.Println("delete sql:", sql, this.value)
  622. }
  623. condition_len := 0 //所有条件数
  624. for _, ch2 := range sql {
  625. if string(ch2) == "?" {
  626. condition_len++
  627. }
  628. }
  629. if condition_len != len(this.value) {
  630. return errors.New("参数错误,条件值错误")
  631. }
  632. if this.conn == nil {
  633. this.conn = DB
  634. }
  635. stmt, err = this.conn.Prepare(sql)
  636. if err != nil {
  637. return err
  638. }
  639. this.stmt = stmt
  640. return nil
  641. }
  642. /**
  643. * 执行查询列表
  644. * return list error
  645. */
  646. func (this *Query) Select() ([]map[string]string, error) {
  647. _, rows, err := FetchRows(this.dbname, this.table, this.alias, this.title, this.join,
  648. this.where, this.where_or, this.value, this.orderby, this.groupby, this.having, this.page, this.page_size, this.debug)
  649. return rows, err
  650. }
  651. /**
  652. * 执行查询多条数据
  653. * return row error
  654. * 2022/01/05
  655. */
  656. func (this *Query) List() ([]map[string]string, error) {
  657. err := this.QueryStmt()
  658. if err != nil {
  659. return []map[string]string{}, err
  660. }
  661. if this.stmt == nil {
  662. return []map[string]string{}, errors.New("缺少必要参数")
  663. }
  664. return StmtForQueryList(this.stmt, this.value)
  665. }
  666. /**
  667. * 执行查询一条数据
  668. * return row error
  669. */
  670. func (this *Query) Find() (map[string]string, error) {
  671. _, row, err := GetRow(this.dbname, this.table, this.alias, this.title, this.join,
  672. this.where, this.where_or, this.value, this.orderby, this.groupby, this.having, this.debug)
  673. return row, err
  674. }
  675. /**
  676. * 执行查询一条数据
  677. * return row error
  678. * 2022/01/05
  679. */
  680. func (this *Query) Get() (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 *Query) 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. // 批量更新
  704. func (this *Query) 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 *Query) 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 *Query) 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. func (this *Query) CreateAll() (int64, error) {
  734. err := this.CreateAllStmt()
  735. if err != nil {
  736. return 0, err
  737. }
  738. return StmtForInsertExec(this.stmt, this.value)
  739. }