debug.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package sqlchemy
  15. import (
  16. "fmt"
  17. "strings"
  18. "time"
  19. "yunion.io/x/log"
  20. )
  21. var (
  22. // DEBUG_SQLCHEMY is a global constant that indicates turn on SQL debug
  23. DEBUG_SQLCHEMY = false
  24. )
  25. func sqlDebug(key, sqlstr string, variables []interface{}) {
  26. sqlstr = _sqlDebug(sqlstr, variables)
  27. if key == "" {
  28. key = "SQuery"
  29. }
  30. log.Debugln(key, sqlstr)
  31. }
  32. func _sqlDebug(sqlstr string, variables []interface{}) string {
  33. return SQLPrintf(sqlstr, variables)
  34. }
  35. func SQLPrintf(sqlstr string, variables []interface{}) string {
  36. for _, v := range variables {
  37. switch vv := v.(type) {
  38. case bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
  39. sqlstr = strings.Replace(sqlstr, "?", fmt.Sprintf("%v", v), 1)
  40. case string:
  41. sqlstr = strings.Replace(sqlstr, "?", fmt.Sprintf("'%s'", v), 1)
  42. case time.Time:
  43. sqlstr = strings.Replace(sqlstr, "?", fmt.Sprintf("'%s'", vv.UTC().Format(time.DateTime)), 1)
  44. default:
  45. sqlstr = strings.Replace(sqlstr, "?", fmt.Sprintf("'%v'", v), 1)
  46. }
  47. }
  48. return sqlstr
  49. }
  50. // DebugQuery show the full query string for debug
  51. func (tq *SQuery) DebugQuery() {
  52. tq.DebugQuery2("")
  53. }
  54. // DebugQuery show the full query string for debug
  55. func (tq *SQuery) DebugQuery2(key string) {
  56. sqlstr := tq.String()
  57. vars := tq.Variables()
  58. sqlDebug(key, sqlstr, vars)
  59. }
  60. func (tq *SQuery) DebugString() string {
  61. return _sqlDebug(tq.String(), tq.Variables())
  62. }
  63. // DebugQuery show the full query string for a subquery for debug
  64. func (sqf *SSubQuery) DebugQuery2(key string) {
  65. sqlstr := sqf.Expression()
  66. vars := sqf.query.Variables()
  67. sqlDebug(key, sqlstr, vars)
  68. }
  69. // DebugQuery show the full query string for a subquery for debug
  70. func (sqf *SSubQuery) DebugQuery() {
  71. sqf.DebugQuery2("")
  72. }
  73. // DebugInsert does insert with debug mode on
  74. func (t *STableSpec) DebugInsert(dt interface{}) error {
  75. return t.insert(dt, false, true)
  76. }
  77. // DebugInsertOrUpdate does insertOrUpdate with debug mode on
  78. func (t *STableSpec) DebugInsertOrUpdate(dt interface{}) error {
  79. return t.insert(dt, true, true)
  80. }
  81. // DebugUpdateFields does update with debug mode on
  82. func (t *STableSpec) DebugUpdateFields(dt interface{}, fields map[string]interface{}) error {
  83. return t.updateFields(dt, fields, true)
  84. }