Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle https://xorm.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
2.5KB

  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. "strings"
  8. "xorm.io/builder"
  9. )
  10. type ErrUnsupportedExprType struct {
  11. tp string
  12. }
  13. func (err ErrUnsupportedExprType) Error() string {
  14. return fmt.Sprintf("Unsupported expression type: %v", err.tp)
  15. }
  16. type exprParam struct {
  17. colName string
  18. arg interface{}
  19. }
  20. type exprParams struct {
  21. colNames []string
  22. args []interface{}
  23. }
  24. func (exprs *exprParams) Len() int {
  25. return len(exprs.colNames)
  26. }
  27. func (exprs *exprParams) addParam(colName string, arg interface{}) {
  28. exprs.colNames = append(exprs.colNames, colName)
  29. exprs.args = append(exprs.args, arg)
  30. }
  31. func (exprs *exprParams) isColExist(colName string) bool {
  32. for _, name := range exprs.colNames {
  33. if strings.EqualFold(trimQuote(name), trimQuote(colName)) {
  34. return true
  35. }
  36. }
  37. return false
  38. }
  39. func (exprs *exprParams) getByName(colName string) (exprParam, bool) {
  40. for i, name := range exprs.colNames {
  41. if strings.EqualFold(name, colName) {
  42. return exprParam{name, exprs.args[i]}, true
  43. }
  44. }
  45. return exprParam{}, false
  46. }
  47. func (exprs *exprParams) writeArgs(w *builder.BytesWriter) error {
  48. for i, expr := range exprs.args {
  49. switch arg := expr.(type) {
  50. case *builder.Builder:
  51. if _, err := w.WriteString("("); err != nil {
  52. return err
  53. }
  54. if err := arg.WriteTo(w); err != nil {
  55. return err
  56. }
  57. if _, err := w.WriteString(")"); err != nil {
  58. return err
  59. }
  60. case string:
  61. if arg == "" {
  62. arg = "''"
  63. }
  64. if _, err := w.WriteString(fmt.Sprintf("%v", arg)); err != nil {
  65. return err
  66. }
  67. default:
  68. if _, err := w.WriteString("?"); err != nil {
  69. return err
  70. }
  71. w.Append(arg)
  72. }
  73. if i != len(exprs.args)-1 {
  74. if _, err := w.WriteString(","); err != nil {
  75. return err
  76. }
  77. }
  78. }
  79. return nil
  80. }
  81. func (exprs *exprParams) writeNameArgs(w *builder.BytesWriter) error {
  82. for i, colName := range exprs.colNames {
  83. if _, err := w.WriteString(colName); err != nil {
  84. return err
  85. }
  86. if _, err := w.WriteString("="); err != nil {
  87. return err
  88. }
  89. switch arg := exprs.args[i].(type) {
  90. case *builder.Builder:
  91. if _, err := w.WriteString("("); err != nil {
  92. return err
  93. }
  94. if err := arg.WriteTo(w); err != nil {
  95. return err
  96. }
  97. if _, err := w.WriteString("("); err != nil {
  98. return err
  99. }
  100. default:
  101. w.Append(exprs.args[i])
  102. }
  103. if i+1 != len(exprs.colNames) {
  104. if _, err := w.WriteString(","); err != nil {
  105. return err
  106. }
  107. }
  108. }
  109. return nil
  110. }