session_raw.go 4.7 KB

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