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

937 lines
21 KiB

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