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.

118 lines
2.4KB

  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. default:
  61. if _, err := w.WriteString(fmt.Sprintf("%v", arg)); err != nil {
  62. return err
  63. }
  64. }
  65. if i != len(exprs.args)-1 {
  66. if _, err := w.WriteString(","); err != nil {
  67. return err
  68. }
  69. }
  70. }
  71. return nil
  72. }
  73. func (exprs *exprParams) writeNameArgs(w *builder.BytesWriter) error {
  74. for i, colName := range exprs.colNames {
  75. if _, err := w.WriteString(colName); err != nil {
  76. return err
  77. }
  78. if _, err := w.WriteString("="); err != nil {
  79. return err
  80. }
  81. switch arg := exprs.args[i].(type) {
  82. case *builder.Builder:
  83. if _, err := w.WriteString("("); err != nil {
  84. return err
  85. }
  86. if err := arg.WriteTo(w); err != nil {
  87. return err
  88. }
  89. if _, err := w.WriteString("("); err != nil {
  90. return err
  91. }
  92. default:
  93. w.Append(exprs.args[i])
  94. }
  95. if i+1 != len(exprs.colNames) {
  96. if _, err := w.WriteString(","); err != nil {
  97. return err
  98. }
  99. }
  100. }
  101. return nil
  102. }