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.

144 lines
3.8 KiB

  1. // Copyright 2016 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "database/sql"
  7. "github.com/go-xorm/core"
  8. )
  9. func (session *Session) query(sqlStr string, paramStr ...interface{}) (resultsSlice []map[string][]byte, err error) {
  10. session.queryPreprocess(&sqlStr, paramStr...)
  11. if session.IsAutoCommit {
  12. return session.innerQuery2(sqlStr, paramStr...)
  13. }
  14. return session.txQuery(session.Tx, sqlStr, paramStr...)
  15. }
  16. func (session *Session) txQuery(tx *core.Tx, sqlStr string, params ...interface{}) (resultsSlice []map[string][]byte, err error) {
  17. rows, err := tx.Query(sqlStr, params...)
  18. if err != nil {
  19. return nil, err
  20. }
  21. defer rows.Close()
  22. return rows2maps(rows)
  23. }
  24. func (session *Session) innerQuery(sqlStr string, params ...interface{}) (*core.Stmt, *core.Rows, error) {
  25. var callback func() (*core.Stmt, *core.Rows, error)
  26. if session.prepareStmt {
  27. callback = func() (*core.Stmt, *core.Rows, error) {
  28. stmt, err := session.doPrepare(sqlStr)
  29. if err != nil {
  30. return nil, nil, err
  31. }
  32. rows, err := stmt.Query(params...)
  33. if err != nil {
  34. return nil, nil, err
  35. }
  36. return stmt, rows, nil
  37. }
  38. } else {
  39. callback = func() (*core.Stmt, *core.Rows, error) {
  40. rows, err := session.DB().Query(sqlStr, params...)
  41. if err != nil {
  42. return nil, nil, err
  43. }
  44. return nil, rows, err
  45. }
  46. }
  47. stmt, rows, err := session.Engine.logSQLQueryTime(sqlStr, params, callback)
  48. if err != nil {
  49. return nil, nil, err
  50. }
  51. return stmt, rows, nil
  52. }
  53. func (session *Session) innerQuery2(sqlStr string, params ...interface{}) ([]map[string][]byte, error) {
  54. _, rows, err := session.innerQuery(sqlStr, params...)
  55. if rows != nil {
  56. defer rows.Close()
  57. }
  58. if err != nil {
  59. return nil, err
  60. }
  61. return rows2maps(rows)
  62. }
  63. // Query a raw sql and return records as []map[string][]byte
  64. func (session *Session) Query(sqlStr string, paramStr ...interface{}) (resultsSlice []map[string][]byte, err error) {
  65. defer session.resetStatement()
  66. if session.IsAutoClose {
  67. defer session.Close()
  68. }
  69. return session.query(sqlStr, paramStr...)
  70. }
  71. // =============================
  72. // for string
  73. // =============================
  74. func (session *Session) query2(sqlStr string, paramStr ...interface{}) (resultsSlice []map[string]string, err error) {
  75. session.queryPreprocess(&sqlStr, paramStr...)
  76. if session.IsAutoCommit {
  77. return query2(session.DB(), sqlStr, paramStr...)
  78. }
  79. return txQuery2(session.Tx, sqlStr, paramStr...)
  80. }
  81. // Execute sql
  82. func (session *Session) innerExec(sqlStr string, args ...interface{}) (sql.Result, error) {
  83. if session.prepareStmt {
  84. stmt, err := session.doPrepare(sqlStr)
  85. if err != nil {
  86. return nil, err
  87. }
  88. res, err := stmt.Exec(args...)
  89. if err != nil {
  90. return nil, err
  91. }
  92. return res, nil
  93. }
  94. return session.DB().Exec(sqlStr, args...)
  95. }
  96. func (session *Session) exec(sqlStr string, args ...interface{}) (sql.Result, error) {
  97. for _, filter := range session.Engine.dialect.Filters() {
  98. // TODO: for table name, it's no need to RefTable
  99. sqlStr = filter.Do(sqlStr, session.Engine.dialect, session.Statement.RefTable)
  100. }
  101. session.saveLastSQL(sqlStr, args...)
  102. return session.Engine.logSQLExecutionTime(sqlStr, args, func() (sql.Result, error) {
  103. if session.IsAutoCommit {
  104. // FIXME: oci8 can not auto commit (github.com/mattn/go-oci8)
  105. if session.Engine.dialect.DBType() == core.ORACLE {
  106. session.Begin()
  107. r, err := session.Tx.Exec(sqlStr, args...)
  108. session.Commit()
  109. return r, err
  110. }
  111. return session.innerExec(sqlStr, args...)
  112. }
  113. return session.Tx.Exec(sqlStr, args...)
  114. })
  115. }
  116. // Exec raw sql
  117. func (session *Session) Exec(sqlStr string, args ...interface{}) (sql.Result, error) {
  118. defer session.resetStatement()
  119. if session.IsAutoClose {
  120. defer session.Close()
  121. }
  122. return session.exec(sqlStr, args...)
  123. }