session_raw.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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{}) ([]map[string][]byte, 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{}) ([]map[string][]byte, 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 runs a raw sql and return records as []map[string][]byte
  64. func (session *Session) Query(sqlStr string, paramStr ...interface{}) ([]map[string][]byte, error) {
  65. defer session.resetStatement()
  66. if session.IsAutoClose {
  67. defer session.Close()
  68. }
  69. return session.query(sqlStr, paramStr...)
  70. }
  71. // QueryString runs a raw sql and return records as []map[string]string
  72. func (session *Session) QueryString(sqlStr string, args ...interface{}) ([]map[string]string, error) {
  73. defer session.resetStatement()
  74. if session.IsAutoClose {
  75. defer session.Close()
  76. }
  77. session.queryPreprocess(&sqlStr, args...)
  78. if session.IsAutoCommit {
  79. return query2(session.DB(), sqlStr, args...)
  80. }
  81. return txQuery2(session.Tx, sqlStr, args...)
  82. }
  83. // Execute sql
  84. func (session *Session) innerExec(sqlStr string, args ...interface{}) (sql.Result, error) {
  85. if session.prepareStmt {
  86. stmt, err := session.doPrepare(sqlStr)
  87. if err != nil {
  88. return nil, err
  89. }
  90. res, err := stmt.Exec(args...)
  91. if err != nil {
  92. return nil, err
  93. }
  94. return res, nil
  95. }
  96. return session.DB().Exec(sqlStr, args...)
  97. }
  98. func (session *Session) exec(sqlStr string, args ...interface{}) (sql.Result, error) {
  99. for _, filter := range session.Engine.dialect.Filters() {
  100. // TODO: for table name, it's no need to RefTable
  101. sqlStr = filter.Do(sqlStr, session.Engine.dialect, session.Statement.RefTable)
  102. }
  103. session.saveLastSQL(sqlStr, args...)
  104. return session.Engine.logSQLExecutionTime(sqlStr, args, func() (sql.Result, error) {
  105. if session.IsAutoCommit {
  106. // FIXME: oci8 can not auto commit (github.com/mattn/go-oci8)
  107. if session.Engine.dialect.DBType() == core.ORACLE {
  108. session.Begin()
  109. r, err := session.Tx.Exec(sqlStr, args...)
  110. session.Commit()
  111. return r, err
  112. }
  113. return session.innerExec(sqlStr, args...)
  114. }
  115. return session.Tx.Exec(sqlStr, args...)
  116. })
  117. }
  118. // Exec raw sql
  119. func (session *Session) Exec(sqlStr string, args ...interface{}) (sql.Result, error) {
  120. defer session.resetStatement()
  121. if session.IsAutoClose {
  122. defer session.Close()
  123. }
  124. return session.exec(sqlStr, args...)
  125. }