statement_args.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2019 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. "fmt"
  7. "reflect"
  8. "strings"
  9. "time"
  10. "xorm.io/builder"
  11. "xorm.io/core"
  12. )
  13. func quoteNeeded(a interface{}) bool {
  14. switch a.(type) {
  15. case int, int8, int16, int32, int64:
  16. return false
  17. case uint, uint8, uint16, uint32, uint64:
  18. return false
  19. case float32, float64:
  20. return false
  21. case bool:
  22. return false
  23. case string:
  24. return true
  25. case time.Time, *time.Time:
  26. return true
  27. case builder.Builder, *builder.Builder:
  28. return false
  29. }
  30. t := reflect.TypeOf(a)
  31. switch t.Kind() {
  32. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  33. return false
  34. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  35. return false
  36. case reflect.Float32, reflect.Float64:
  37. return false
  38. case reflect.Bool:
  39. return false
  40. case reflect.String:
  41. return true
  42. }
  43. return true
  44. }
  45. func convertStringSingleQuote(arg string) string {
  46. return "'" + strings.Replace(arg, "'", "''", -1) + "'"
  47. }
  48. func convertString(arg string) string {
  49. var buf strings.Builder
  50. buf.WriteRune('\'')
  51. for _, c := range arg {
  52. if c == '\\' || c == '\'' {
  53. buf.WriteRune('\\')
  54. }
  55. buf.WriteRune(c)
  56. }
  57. buf.WriteRune('\'')
  58. return buf.String()
  59. }
  60. func convertArg(arg interface{}, convertFunc func(string) string) string {
  61. if quoteNeeded(arg) {
  62. argv := fmt.Sprintf("%v", arg)
  63. return convertFunc(argv)
  64. }
  65. return fmt.Sprintf("%v", arg)
  66. }
  67. const insertSelectPlaceHolder = true
  68. func (statement *Statement) writeArg(w *builder.BytesWriter, arg interface{}) error {
  69. switch argv := arg.(type) {
  70. case bool:
  71. if statement.Engine.dialect.DBType() == core.MSSQL {
  72. if argv {
  73. if _, err := w.WriteString("1"); err != nil {
  74. return err
  75. }
  76. } else {
  77. if _, err := w.WriteString("0"); err != nil {
  78. return err
  79. }
  80. }
  81. } else {
  82. if argv {
  83. if _, err := w.WriteString("true"); err != nil {
  84. return err
  85. }
  86. } else {
  87. if _, err := w.WriteString("false"); err != nil {
  88. return err
  89. }
  90. }
  91. }
  92. case *builder.Builder:
  93. if _, err := w.WriteString("("); err != nil {
  94. return err
  95. }
  96. if err := argv.WriteTo(w); err != nil {
  97. return err
  98. }
  99. if _, err := w.WriteString(")"); err != nil {
  100. return err
  101. }
  102. default:
  103. if insertSelectPlaceHolder {
  104. if err := w.WriteByte('?'); err != nil {
  105. return err
  106. }
  107. w.Append(arg)
  108. } else {
  109. var convertFunc = convertStringSingleQuote
  110. if statement.Engine.dialect.DBType() == core.MYSQL {
  111. convertFunc = convertString
  112. }
  113. if _, err := w.WriteString(convertArg(arg, convertFunc)); err != nil {
  114. return err
  115. }
  116. }
  117. }
  118. return nil
  119. }
  120. func (statement *Statement) writeArgs(w *builder.BytesWriter, args []interface{}) error {
  121. for i, arg := range args {
  122. if err := statement.writeArg(w, arg); err != nil {
  123. return err
  124. }
  125. if i+1 != len(args) {
  126. if _, err := w.WriteString(","); err != nil {
  127. return err
  128. }
  129. }
  130. }
  131. return nil
  132. }
  133. func writeStrings(w *builder.BytesWriter, cols []string, leftQuote, rightQuote string) error {
  134. for i, colName := range cols {
  135. if len(leftQuote) > 0 && colName[0] != '`' {
  136. if _, err := w.WriteString(leftQuote); err != nil {
  137. return err
  138. }
  139. }
  140. if _, err := w.WriteString(colName); err != nil {
  141. return err
  142. }
  143. if len(rightQuote) > 0 && colName[len(colName)-1] != '`' {
  144. if _, err := w.WriteString(rightQuote); err != nil {
  145. return err
  146. }
  147. }
  148. if i+1 != len(cols) {
  149. if _, err := w.WriteString(","); err != nil {
  150. return err
  151. }
  152. }
  153. }
  154. return nil
  155. }