commonSqlDialect.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2022-2023 EMQ Technologies Co., Ltd.
  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 sqlgen
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/pkg/cast"
  18. )
  19. type CommonQueryGenerator struct {
  20. *InternalSqlQueryCfg
  21. }
  22. func (q *CommonQueryGenerator) quoteIdentifier(identifier string) string {
  23. return "'" + identifier + "'"
  24. }
  25. func (q *CommonQueryGenerator) getSelect() string {
  26. return "select * from " + q.Table + " "
  27. }
  28. func (q *CommonQueryGenerator) getCondition() (string, error) {
  29. var val string
  30. if q.IndexField != "" {
  31. if q.IndexFieldType == DATETIME_TYPE && q.DateTimeFormat != "" {
  32. t, err := cast.InterfaceToTime(q.IndexValue, q.DateTimeFormat)
  33. if err != nil {
  34. err = fmt.Errorf("SqlQueryStatement InterfaceToTime datetime convert got error %v", err)
  35. return "", err
  36. }
  37. val, err = cast.FormatTime(t, q.DateTimeFormat)
  38. if err != nil {
  39. err = fmt.Errorf("SqlQueryStatement FormatTime datetime convert got error %v", err)
  40. return "", err
  41. }
  42. } else {
  43. val = fmt.Sprintf("%v", q.IndexValue)
  44. }
  45. return "where " + q.IndexField + " > " + q.quoteIdentifier(val) + " ", nil
  46. }
  47. return "", nil
  48. }
  49. func (q *CommonQueryGenerator) getOrderby() string {
  50. if q.IndexField != "" {
  51. return "order by " + q.quoteIdentifier(q.IndexField) + " ASC "
  52. }
  53. return ""
  54. }
  55. func (q *CommonQueryGenerator) getLimit() string {
  56. if q.Limit != 0 {
  57. return fmt.Sprintf("limit %d", q.Limit)
  58. }
  59. return ""
  60. }
  61. func NewCommonSqlQuery(cfg *InternalSqlQueryCfg) SqlQueryGenerator {
  62. in := &CommonQueryGenerator{
  63. InternalSqlQueryCfg: cfg,
  64. }
  65. return in
  66. }
  67. func (q *CommonQueryGenerator) SqlQueryStatement() (string, error) {
  68. con, err := q.getCondition()
  69. if err != nil {
  70. return "", err
  71. }
  72. return q.getSelect() + con + q.getOrderby() + q.getLimit(), nil
  73. }
  74. func (q *CommonQueryGenerator) UpdateMaxIndexValue(row map[string]interface{}) {
  75. if q.IndexField != "" {
  76. v, found := row[q.IndexField]
  77. if !found {
  78. return
  79. }
  80. q.IndexValue = v
  81. }
  82. }
  83. type OracleQueryGenerate struct {
  84. *CommonQueryGenerator
  85. }
  86. func NewOracleQueryGenerate(cfg *InternalSqlQueryCfg) SqlQueryGenerator {
  87. return &OracleQueryGenerate{
  88. CommonQueryGenerator: &CommonQueryGenerator{
  89. InternalSqlQueryCfg: cfg,
  90. },
  91. }
  92. }
  93. func (q *OracleQueryGenerate) SqlQueryStatement() (string, error) {
  94. con, err := q.getCondition()
  95. if err != nil {
  96. return "", err
  97. }
  98. query := q.getSelect() + con + q.getOrderby()
  99. if q.Limit != 0 {
  100. return fmt.Sprintf("select * from (%s) where rownum <= %v", query, q.Limit), nil
  101. }
  102. return query, nil
  103. }
  104. func (q *OracleQueryGenerate) UpdateMaxIndexValue(row map[string]interface{}) {
  105. q.CommonQueryGenerator.UpdateMaxIndexValue(row)
  106. }