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.

cache.go 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 statements
  5. import (
  6. "fmt"
  7. "strings"
  8. "xorm.io/xorm/internal/utils"
  9. "xorm.io/xorm/schemas"
  10. )
  11. func (statement *Statement) ConvertIDSQL(sqlStr string) string {
  12. if statement.RefTable != nil {
  13. cols := statement.RefTable.PKColumns()
  14. if len(cols) == 0 {
  15. return ""
  16. }
  17. colstrs := statement.joinColumns(cols, false)
  18. sqls := utils.SplitNNoCase(sqlStr, " from ", 2)
  19. if len(sqls) != 2 {
  20. return ""
  21. }
  22. var top string
  23. pLimitN := statement.LimitN
  24. if pLimitN != nil && statement.dialect.URI().DBType == schemas.MSSQL {
  25. top = fmt.Sprintf("TOP %d ", *pLimitN)
  26. }
  27. newsql := fmt.Sprintf("SELECT %s%s FROM %v", top, colstrs, sqls[1])
  28. return newsql
  29. }
  30. return ""
  31. }
  32. func (statement *Statement) ConvertUpdateSQL(sqlStr string) (string, string) {
  33. if statement.RefTable == nil || len(statement.RefTable.PrimaryKeys) != 1 {
  34. return "", ""
  35. }
  36. colstrs := statement.joinColumns(statement.RefTable.PKColumns(), true)
  37. sqls := utils.SplitNNoCase(sqlStr, "where", 2)
  38. if len(sqls) != 2 {
  39. if len(sqls) == 1 {
  40. return sqls[0], fmt.Sprintf("SELECT %v FROM %v",
  41. colstrs, statement.quote(statement.TableName()))
  42. }
  43. return "", ""
  44. }
  45. var whereStr = sqls[1]
  46. // TODO: for postgres only, if any other database?
  47. var paraStr string
  48. if statement.dialect.URI().DBType == schemas.POSTGRES {
  49. paraStr = "$"
  50. } else if statement.dialect.URI().DBType == schemas.MSSQL {
  51. paraStr = ":"
  52. }
  53. if paraStr != "" {
  54. if strings.Contains(sqls[1], paraStr) {
  55. dollers := strings.Split(sqls[1], paraStr)
  56. whereStr = dollers[0]
  57. for i, c := range dollers[1:] {
  58. ccs := strings.SplitN(c, " ", 2)
  59. whereStr += fmt.Sprintf(paraStr+"%v %v", i+1, ccs[1])
  60. }
  61. }
  62. }
  63. return sqls[0], fmt.Sprintf("SELECT %v FROM %v WHERE %v",
  64. colstrs, statement.quote(statement.TableName()),
  65. whereStr)
  66. }