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

939 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 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. builder.WriteString(sql)
  325. boo := false
  326. for _, joinitem := range this.join {
  327. if len(joinitem) < 2 {
  328. continue
  329. }
  330. builder.WriteString(" ")
  331. if len(joinitem) >= 3 {
  332. builder.WriteString(joinitem[2])
  333. } else {
  334. builder.WriteString("left")
  335. }
  336. builder.WriteString(" join ")
  337. if withSql != "" || strings.Contains(joinitem[0], "select ") || strings.HasPrefix(joinitem[0], "(") {
  338. builder.WriteString(joinitem[0])
  339. } else {
  340. builder.WriteString(getTableName(this.dbname, joinitem[0]))
  341. }
  342. builder.WriteString(" on ")
  343. builder.WriteString(joinitem[1])
  344. boo = true
  345. }
  346. if boo {
  347. sql = builder.String()
  348. }
  349. }
  350. if len(this.where) > 0 || len(this.where_or) > 0 {
  351. sql = helper.StringJoin(sql, " where ")
  352. }
  353. if len(this.where) > 0 {
  354. sql = helper.StringJoin(sql, " (", strings.Join(this.where, " and "), " ) ")
  355. }
  356. if len(this.where_or) > 0 {
  357. if len(this.where) > 0 {
  358. sql = helper.StringJoin(sql, " or ", strings.Join(this.where_or, " or "))
  359. } else {
  360. sql = helper.StringJoin(sql, strings.Join(this.where_or, " or "))
  361. }
  362. }
  363. if this.groupby != "" {
  364. sql = helper.StringJoin(sql, " group by ", this.groupby)
  365. }
  366. if this.having != "" {
  367. sql = helper.StringJoin(sql, " having ", this.having)
  368. }
  369. if this.orderby != "" {
  370. sql = helper.StringJoin(sql, " order by ", this.orderby)
  371. }
  372. if this.dbtype == "mysql" && (this.page > 0 || this.page_size > 0) {
  373. if this.page < 1 {
  374. this.page = 1
  375. }
  376. if this.page_size < 1 {
  377. this.page_size = 10
  378. }
  379. from := strconv.Itoa((this.page - 1) * this.page_size)
  380. offset := strconv.Itoa(this.page_size)
  381. if from != "" && offset != "" {
  382. sql = helper.StringJoin(sql, " limit ", from, " , ", offset)
  383. }
  384. }
  385. if this.debug {
  386. log.Println("query sql:", sql, this.value)
  387. }
  388. condition_len := 0 //所有条件数
  389. for _, ch2 := range sql {
  390. if string(ch2) == "?" {
  391. condition_len++
  392. }
  393. }
  394. if condition_len != len(this.value) {
  395. return nil, errors.New("参数错误,条件值错误")
  396. }
  397. return map[string]interface{}{
  398. "sql": sql,
  399. "value": this.value,
  400. }, nil
  401. }
  402. // 拼查询sql
  403. func (this *Query) QueryStmt() error {
  404. res := map[string]interface{}{}
  405. res, err = this.BuildSelectSql()
  406. if err != nil {
  407. return err
  408. }
  409. sql := helper.ToStr(res["sql"])
  410. if SLAVER_DB != nil {
  411. this.conn = SLAVER_DB
  412. }
  413. // else {
  414. // this.conn = DB
  415. // }
  416. if this.conn == nil {
  417. this.conn = DB
  418. }
  419. stmt, err = this.conn.Prepare(sql)
  420. if err != nil {
  421. return err
  422. }
  423. this.stmt = stmt
  424. return nil
  425. }
  426. // 拼更新sql
  427. func (this *Query) UpdateStmt() error {
  428. if this.dbname == "" && this.table == "" {
  429. return errors.New("参数错误,没有数据表")
  430. }
  431. if len(this.where) < 1 {
  432. return errors.New("参数错误,缺少条件")
  433. }
  434. dbName := getTableName(this.dbname, this.table, this.dbtype)
  435. var sql string
  436. sql = helper.StringJoin("update ", dbName, " set ", strings.Join(this.data, " , "))
  437. sql = helper.StringJoin(sql, " where ", strings.Join(this.where, " and "))
  438. if this.debug {
  439. log.Println("update sql:", sql, this.value)
  440. }
  441. condition_len := 0 //所有条件数
  442. for _, ch2 := range sql {
  443. if string(ch2) == "?" {
  444. condition_len++
  445. }
  446. }
  447. if condition_len != len(this.value) {
  448. return errors.New("参数错误,条件值错误")
  449. }
  450. if this.conn == nil {
  451. this.conn = DB
  452. }
  453. stmt, err = this.conn.Prepare(sql)
  454. if err != nil {
  455. return err
  456. }
  457. this.stmt = stmt
  458. return nil
  459. }
  460. // 拼批量存在更新不存在插入sql
  461. func (this *Query) UpdateAllStmt() error {
  462. if this.dbname == "" && this.table == "" {
  463. return errors.New("参数错误,没有数据表")
  464. }
  465. dbName := getTableName(this.dbname, this.table)
  466. var sql string
  467. var dataSql []string //一组用到的占位字符
  468. var valSql []string //占位字符组
  469. var updSql []string //更新字段的sql
  470. var updFieldLen = len(this.upd_field) //需要更新的字段数量,为0时更新除id外添加值
  471. dataLen := len(this.save_data)
  472. if dataLen > 0 {
  473. //批量操作
  474. this.data = this.data[0:0]
  475. this.value = this.value[0:0]
  476. var dataSqlText string //占位字符组
  477. for i := 0; i < dataLen; i++ {
  478. if i == 0 {
  479. //第一组时分配变量空间
  480. fieldLen := len(this.save_data[i])
  481. this.data = make([]string, 0, fieldLen)
  482. dataSql = make([]string, 0, fieldLen)
  483. this.value = make([]interface{}, 0, fieldLen*dataLen)
  484. valSql = make([]string, 0, dataLen)
  485. switch updFieldLen {
  486. case 0:
  487. //预览创建数据的长度
  488. updSql = make([]string, 0, fieldLen)
  489. default:
  490. //按照需要更新字段数长度
  491. updSql = make([]string, 0, updFieldLen)
  492. for _, k := range this.upd_field {
  493. updSql = append(updSql, k+"=values("+k+")") //存储需要更新的字段
  494. }
  495. }
  496. for k := range this.save_data[i] {
  497. this.data = append(this.data, k) //存储添加的字段
  498. dataSql = append(dataSql, "?") //存储需要的占位符
  499. if updFieldLen == 0 && k != "id" {
  500. updSql = append(updSql, k+"=values("+k+")") //存储需要更新的字段
  501. }
  502. }
  503. dataSqlText = strings.Join(dataSql, ",") //组成每组占位字符格式
  504. }
  505. for j := 0; j < len(this.data); j++ {
  506. this.value = append(this.value, this.save_data[i][this.data[j]]) //存储值
  507. }
  508. valSql = append(valSql, "("+dataSqlText+")") //组成占位字符组
  509. }
  510. } else {
  511. //添加一条(原理同上)
  512. fieldLen := len(this.data)
  513. dataSql = make([]string, 0, fieldLen)
  514. valSql = make([]string, 0, 1)
  515. switch updFieldLen {
  516. case 0:
  517. updSql = make([]string, 0, fieldLen)
  518. default:
  519. updSql = make([]string, 0, updFieldLen)
  520. for _, k := range this.upd_field {
  521. updSql = append(updSql, k+"=values("+k+")")
  522. }
  523. }
  524. for i := 0; i < fieldLen; i++ {
  525. dataSql = append(dataSql, "?")
  526. if updFieldLen == 0 && this.data[i] != "id" {
  527. updSql = append(updSql, this.data[i]+"=values("+this.data[i]+")")
  528. }
  529. }
  530. if updFieldLen > 0 {
  531. for _, k := range this.upd_field {
  532. updSql = append(updSql, k+"=values("+k+")")
  533. }
  534. }
  535. valSql = append(valSql, "("+strings.Join(dataSql, " , ")+")")
  536. }
  537. if len(this.data) == 0 {
  538. return errors.New("参数错误,没有字段值")
  539. }
  540. if len(this.value) == 0 {
  541. return errors.New("参数错误,条件值错误")
  542. }
  543. setText := " values "
  544. if len(valSql) > 1 {
  545. setText = " value "
  546. }
  547. sql = helper.StringJoin("insert into ", dbName, " (", strings.Join(this.data, " , "), ")", setText, strings.Join(valSql, ","), " ON DUPLICATE KEY UPDATE ", strings.Join(updSql, " , "))
  548. if this.debug {
  549. log.Println("insert on duplicate key update sql:", sql, this.value)
  550. }
  551. conditionLen := 0 //所有条件数
  552. for _, ch2 := range sql {
  553. if string(ch2) == "?" {
  554. conditionLen++
  555. }
  556. }
  557. if conditionLen != len(this.value) {
  558. return errors.New("参数错误,条件值数量不匹配")
  559. }
  560. if this.conn == nil {
  561. this.conn = DB
  562. }
  563. stmt, err = this.conn.Prepare(sql)
  564. if err != nil {
  565. return err
  566. }
  567. this.stmt = stmt
  568. return nil
  569. }
  570. // 拼批量插入sql
  571. func (this *Query) CreateAllStmt() error {
  572. if this.dbname == "" && this.table == "" {
  573. return errors.New("参数错误,没有数据表")
  574. }
  575. dbName := getTableName(this.dbname, this.table)
  576. var sql string
  577. var dataSql []string //一组用到的占位字符
  578. var valSql []string //占位字符组
  579. dataLen := len(this.save_data)
  580. if dataLen > 0 {
  581. //清空字段和值
  582. this.data = this.data[0:0]
  583. this.value = this.value[0:0]
  584. var dataSqlText string //占位字符组
  585. for i := 0; i < dataLen; i++ {
  586. if i == 0 {
  587. //第一组时分配变量空间
  588. fieldLen := len(this.save_data[i])
  589. this.data = make([]string, 0, fieldLen)
  590. dataSql = make([]string, 0, fieldLen)
  591. this.value = make([]interface{}, 0, fieldLen*dataLen)
  592. valSql = make([]string, 0, dataLen)
  593. for k := range this.save_data[i] {
  594. this.data = append(this.data, k) //存储字段
  595. dataSql = append(dataSql, "?") //存储需要的占位符
  596. }
  597. dataSqlText = strings.Join(dataSql, ",") //组成每组占位字符格式
  598. }
  599. for j := 0; j < len(this.data); j++ {
  600. this.value = append(this.value, this.save_data[i][this.data[j]]) //存储值
  601. }
  602. valSql = append(valSql, "("+dataSqlText+")") //组成占位字符组
  603. }
  604. } else {
  605. //添加一条(原理同上)
  606. fieldLen := len(this.data)
  607. dataSql = make([]string, 0, fieldLen)
  608. for i := 0; i < fieldLen; i++ {
  609. dataSql = append(dataSql, "?")
  610. }
  611. valSql = make([]string, 0, 1)
  612. valSql = append(valSql, "("+strings.Join(dataSql, " , ")+")")
  613. }
  614. if len(this.data) == 0 {
  615. return errors.New("参数错误,字段名错误")
  616. }
  617. if len(this.value) == 0 {
  618. return errors.New("参数错误,条件值错误")
  619. }
  620. //通过sql关键字优化批量操作和单个操作效率
  621. setText := " values "
  622. if len(valSql) > 1 {
  623. setText = " value "
  624. }
  625. sql = helper.StringJoin("insert into ", dbName, " (", strings.Join(this.data, " , "), ")", setText, strings.Join(valSql, ","))
  626. if this.debug {
  627. log.Println("insert sql:", sql, this.value)
  628. }
  629. conditionLen := 0 //所有条件数
  630. for _, ch2 := range sql {
  631. if string(ch2) == "?" {
  632. conditionLen++
  633. }
  634. }
  635. if conditionLen != len(this.value) {
  636. return errors.New("参数错误,条件值数量不匹配")
  637. }
  638. if this.conn == nil {
  639. this.conn = DB
  640. }
  641. stmt, err = this.conn.Prepare(sql)
  642. if err != nil {
  643. return err
  644. }
  645. this.stmt = stmt
  646. return nil
  647. }
  648. // 拼插入sql
  649. func (this *Query) CreateStmt() error {
  650. if this.dbname == "" && this.table == "" {
  651. return errors.New("参数错误,没有数据表")
  652. }
  653. dbName := getTableName(this.dbname, this.table, this.dbtype)
  654. var sql string
  655. sql = helper.StringJoin("insert into ", dbName, " set ", strings.Join(this.data, " , "))
  656. if this.debug {
  657. log.Println("insert sql:", sql, this.value)
  658. }
  659. condition_len := 0 //所有条件数
  660. for _, ch2 := range sql {
  661. if string(ch2) == "?" {
  662. condition_len++
  663. }
  664. }
  665. if condition_len != len(this.value) {
  666. return errors.New("参数错误,条件值错误")
  667. }
  668. if this.conn == nil {
  669. this.conn = DB
  670. }
  671. stmt, err = this.conn.Prepare(sql)
  672. if err != nil {
  673. return err
  674. }
  675. this.stmt = stmt
  676. return nil
  677. }
  678. // 拼删除sql
  679. func (this *Query) DeleteStmt() error {
  680. if this.dbname == "" && this.table == "" {
  681. return errors.New("参数错误,没有数据表")
  682. }
  683. if len(this.where) < 1 {
  684. return errors.New("参数错误,缺少条件")
  685. }
  686. dbName := getTableName(this.dbname, this.table, this.dbtype)
  687. var sql string
  688. sql = helper.StringJoin("delete from ", dbName, " where ", strings.Join(this.where, " and "))
  689. if this.page_size > 0 {
  690. sql = helper.StringJoin(sql, " limit ", strconv.Itoa(this.page_size))
  691. }
  692. if this.debug {
  693. log.Println("delete sql:", sql, this.value)
  694. }
  695. condition_len := 0 //所有条件数
  696. for _, ch2 := range sql {
  697. if string(ch2) == "?" {
  698. condition_len++
  699. }
  700. }
  701. if condition_len != len(this.value) {
  702. return errors.New("参数错误,条件值错误")
  703. }
  704. if this.conn == nil {
  705. this.conn = DB
  706. }
  707. stmt, err = this.conn.Prepare(sql)
  708. if err != nil {
  709. return err
  710. }
  711. this.stmt = stmt
  712. return nil
  713. }
  714. /**
  715. * 执行查询列表
  716. * return list error
  717. */
  718. func (this *Query) Select() ([]map[string]string, error) {
  719. _, rows, err := FetchRows(this.dbname, this.table, this.alias, this.title, this.with, this.join,
  720. this.where, this.where_or, this.value, this.orderby, this.groupby, this.having, this.page, this.page_size, this.debug)
  721. return rows, err
  722. }
  723. /**
  724. * 执行查询多条数据
  725. * return row error
  726. * 2022/01/05
  727. */
  728. func (this *Query) List() ([]map[string]string, error) {
  729. err := this.QueryStmt()
  730. if err != nil {
  731. return []map[string]string{}, err
  732. }
  733. if this.stmt == nil {
  734. return []map[string]string{}, errors.New("缺少必要参数")
  735. }
  736. return StmtForQueryList(this.stmt, this.value)
  737. }
  738. /**
  739. * 执行查询一条数据
  740. * return row error
  741. */
  742. func (this *Query) Find() (map[string]string, error) {
  743. _, row, err := GetRow(this.dbname, this.table, this.alias, this.title, this.with, this.join,
  744. this.where, this.where_or, this.value, this.orderby, this.groupby, this.having, this.debug)
  745. return row, err
  746. }
  747. /**
  748. * 执行查询一条数据
  749. * return row error
  750. * 2022/01/05
  751. */
  752. func (this *Query) Get() (map[string]string, error) {
  753. this.page = 1
  754. this.page_size = 1
  755. err := this.QueryStmt()
  756. if err != nil {
  757. return map[string]string{}, err
  758. }
  759. if this.stmt == nil {
  760. return nil, errors.New("缺少必要参数")
  761. }
  762. return StmtForQueryRow(this.stmt, this.value)
  763. }
  764. /**
  765. * 执行更新
  766. * return is_updated error
  767. */
  768. func (this *Query) Update() (int64, error) {
  769. err := this.UpdateStmt()
  770. if err != nil {
  771. return 0, err
  772. }
  773. return StmtForUpdateExec(this.stmt, this.value)
  774. }
  775. // 批量更新
  776. func (this *Query) UpdateAll() (int64, error) {
  777. err := this.UpdateAllStmt()
  778. if err != nil {
  779. return 0, err
  780. }
  781. return StmtForUpdateExec(this.stmt, this.value)
  782. }
  783. /**
  784. * 执行删除
  785. * return is_delete error
  786. */
  787. func (this *Query) Delete() (int64, error) {
  788. err := this.DeleteStmt()
  789. if err != nil {
  790. return 0, err
  791. }
  792. return StmtForUpdateExec(this.stmt, this.value)
  793. }
  794. /**
  795. * 执行写入
  796. * return is_insert error
  797. */
  798. func (this *Query) Create() (int64, error) {
  799. err := this.CreateStmt()
  800. if err != nil {
  801. return 0, err
  802. }
  803. return StmtForInsertExec(this.stmt, this.value)
  804. }
  805. func (this *Query) CreateAll() (int64, error) {
  806. err := this.CreateAllStmt()
  807. if err != nil {
  808. return 0, err
  809. }
  810. return StmtForInsertExec(this.stmt, this.value)
  811. }