session_raw.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. "reflect"
  8. "time"
  9. "xorm.io/builder"
  10. "xorm.io/core"
  11. )
  12. func (session *Session) queryPreprocess(sqlStr *string, paramStr ...interface{}) {
  13. for _, filter := range session.engine.dialect.Filters() {
  14. *sqlStr = filter.Do(*sqlStr, session.engine.dialect, session.statement.RefTable)
  15. }
  16. session.lastSQL = *sqlStr
  17. session.lastSQLArgs = paramStr
  18. }
  19. func (session *Session) queryRows(sqlStr string, args ...interface{}) (*core.Rows, error) {
  20. defer session.resetStatement()
  21. session.queryPreprocess(&sqlStr, args...)
  22. if session.engine.showSQL {
  23. if session.engine.showExecTime {
  24. b4ExecTime := time.Now()
  25. defer func() {
  26. execDuration := time.Since(b4ExecTime)
  27. if len(args) > 0 {
  28. session.engine.logger.Infof("[SQL] %s %#v - took: %v", sqlStr, args, execDuration)
  29. } else {
  30. session.engine.logger.Infof("[SQL] %s - took: %v", sqlStr, execDuration)
  31. }
  32. }()
  33. } else {
  34. if len(args) > 0 {
  35. session.engine.logger.Infof("[SQL] %v %#v", sqlStr, args)
  36. } else {
  37. session.engine.logger.Infof("[SQL] %v", sqlStr)
  38. }
  39. }
  40. }
  41. if session.isAutoCommit {
  42. var db *core.DB
  43. if session.sessionType == groupSession {
  44. db = session.engine.engineGroup.Slave().DB()
  45. } else {
  46. db = session.DB()
  47. }
  48. if session.prepareStmt {
  49. // don't clear stmt since session will cache them
  50. stmt, err := session.doPrepare(db, sqlStr)
  51. if err != nil {
  52. return nil, err
  53. }
  54. rows, err := stmt.QueryContext(session.ctx, args...)
  55. if err != nil {
  56. return nil, err
  57. }
  58. return rows, nil
  59. }
  60. rows, err := db.QueryContext(session.ctx, sqlStr, args...)
  61. if err != nil {
  62. return nil, err
  63. }
  64. return rows, nil
  65. }
  66. rows, err := session.tx.QueryContext(session.ctx, sqlStr, args...)
  67. if err != nil {
  68. return nil, err
  69. }
  70. return rows, nil
  71. }
  72. func (session *Session) queryRow(sqlStr string, args ...interface{}) *core.Row {
  73. return core.NewRow(session.queryRows(sqlStr, args...))
  74. }
  75. func value2Bytes(rawValue *reflect.Value) ([]byte, error) {
  76. str, err := value2String(rawValue)
  77. if err != nil {
  78. return nil, err
  79. }
  80. return []byte(str), nil
  81. }
  82. func row2map(rows *core.Rows, fields []string) (resultsMap map[string][]byte, err error) {
  83. result := make(map[string][]byte)
  84. scanResultContainers := make([]interface{}, len(fields))
  85. for i := 0; i < len(fields); i++ {
  86. var scanResultContainer interface{}
  87. scanResultContainers[i] = &scanResultContainer
  88. }
  89. if err := rows.Scan(scanResultContainers...); err != nil {
  90. return nil, err
  91. }
  92. for ii, key := range fields {
  93. rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
  94. //if row is null then ignore
  95. if rawValue.Interface() == nil {
  96. result[key] = []byte{}
  97. continue
  98. }
  99. if data, err := value2Bytes(&rawValue); err == nil {
  100. result[key] = data
  101. } else {
  102. return nil, err // !nashtsai! REVIEW, should return err or just error log?
  103. }
  104. }
  105. return result, nil
  106. }
  107. func rows2maps(rows *core.Rows) (resultsSlice []map[string][]byte, err error) {
  108. fields, err := rows.Columns()
  109. if err != nil {
  110. return nil, err
  111. }
  112. for rows.Next() {
  113. result, err := row2map(rows, fields)
  114. if err != nil {
  115. return nil, err
  116. }
  117. resultsSlice = append(resultsSlice, result)
  118. }
  119. return resultsSlice, nil
  120. }
  121. func (session *Session) queryBytes(sqlStr string, args ...interface{}) ([]map[string][]byte, error) {
  122. rows, err := session.queryRows(sqlStr, args...)
  123. if err != nil {
  124. return nil, err
  125. }
  126. defer rows.Close()
  127. return rows2maps(rows)
  128. }
  129. func (session *Session) exec(sqlStr string, args ...interface{}) (sql.Result, error) {
  130. defer session.resetStatement()
  131. session.queryPreprocess(&sqlStr, args...)
  132. if session.engine.showSQL {
  133. if session.engine.showExecTime {
  134. b4ExecTime := time.Now()
  135. defer func() {
  136. execDuration := time.Since(b4ExecTime)
  137. if len(args) > 0 {
  138. session.engine.logger.Infof("[SQL] %s %#v - took: %v", sqlStr, args, execDuration)
  139. } else {
  140. session.engine.logger.Infof("[SQL] %s - took: %v", sqlStr, execDuration)
  141. }
  142. }()
  143. } else {
  144. if len(args) > 0 {
  145. session.engine.logger.Infof("[SQL] %v %#v", sqlStr, args)
  146. } else {
  147. session.engine.logger.Infof("[SQL] %v", sqlStr)
  148. }
  149. }
  150. }
  151. if !session.isAutoCommit {
  152. return session.tx.ExecContext(session.ctx, sqlStr, args...)
  153. }
  154. if session.prepareStmt {
  155. stmt, err := session.doPrepare(session.DB(), sqlStr)
  156. if err != nil {
  157. return nil, err
  158. }
  159. res, err := stmt.ExecContext(session.ctx, args...)
  160. if err != nil {
  161. return nil, err
  162. }
  163. return res, nil
  164. }
  165. return session.DB().ExecContext(session.ctx, sqlStr, args...)
  166. }
  167. func convertSQLOrArgs(sqlOrArgs ...interface{}) (string, []interface{}, error) {
  168. switch sqlOrArgs[0].(type) {
  169. case string:
  170. return sqlOrArgs[0].(string), sqlOrArgs[1:], nil
  171. case *builder.Builder:
  172. return sqlOrArgs[0].(*builder.Builder).ToSQL()
  173. case builder.Builder:
  174. bd := sqlOrArgs[0].(builder.Builder)
  175. return bd.ToSQL()
  176. }
  177. return "", nil, ErrUnSupportedType
  178. }
  179. // Exec raw sql
  180. func (session *Session) Exec(sqlOrArgs ...interface{}) (sql.Result, error) {
  181. if session.isAutoClose {
  182. defer session.Close()
  183. }
  184. if len(sqlOrArgs) == 0 {
  185. return nil, ErrUnSupportedType
  186. }
  187. sqlStr, args, err := convertSQLOrArgs(sqlOrArgs...)
  188. if err != nil {
  189. return nil, err
  190. }
  191. return session.exec(sqlStr, args...)
  192. }