commonSqlDialect.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2022 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. }