stmtx.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2021-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 xsql
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "strings"
  19. "github.com/lf-edge/ekuiper/pkg/ast"
  20. "github.com/lf-edge/ekuiper/pkg/cast"
  21. "github.com/lf-edge/ekuiper/pkg/errorx"
  22. "github.com/lf-edge/ekuiper/pkg/kv"
  23. )
  24. func GetStreams(stmt *ast.SelectStatement) (result []string) {
  25. if stmt == nil {
  26. return nil
  27. }
  28. // TODO sources must be a stream
  29. for _, source := range stmt.Sources {
  30. if s, ok := source.(*ast.Table); ok {
  31. result = append(result, s.Name)
  32. }
  33. }
  34. for _, join := range stmt.Joins {
  35. result = append(result, join.Name)
  36. }
  37. return
  38. }
  39. func GetStatementFromSql(sql string) (*ast.SelectStatement, error) {
  40. parser := NewParser(strings.NewReader(sql))
  41. if stmt, err := Language.Parse(parser); err != nil {
  42. return nil, fmt.Errorf("Parse SQL %s error: %s.", sql, err)
  43. } else {
  44. if r, ok := stmt.(*ast.SelectStatement); !ok {
  45. return nil, fmt.Errorf("SQL %s is not a select statement.", sql)
  46. } else {
  47. return r, nil
  48. }
  49. }
  50. }
  51. type StreamInfo struct {
  52. StreamType ast.StreamType `json:"streamType"`
  53. StreamKind string `json:"streamKind"`
  54. Statement string `json:"statement"`
  55. }
  56. func GetDataSourceStatement(m kv.KeyValue, name string) (*StreamInfo, error) {
  57. var (
  58. v string
  59. vs = &StreamInfo{}
  60. )
  61. if ok, _ := m.Get(name, &v); ok {
  62. if err := json.Unmarshal(cast.StringToBytes(v), vs); err != nil {
  63. return nil, fmt.Errorf("error unmarshall %s, the data in db may be corrupted", name)
  64. } else {
  65. return vs, nil
  66. }
  67. }
  68. return nil, errorx.NewWithCode(errorx.NOT_FOUND, fmt.Sprintf("%s is not found", name))
  69. }
  70. func GetDataSource(m kv.KeyValue, name string) (stmt *ast.StreamStmt, err error) {
  71. info, err := GetDataSourceStatement(m, name)
  72. if err != nil {
  73. return nil, err
  74. }
  75. parser := NewParser(strings.NewReader(info.Statement))
  76. stream, err := Language.Parse(parser)
  77. stmt, ok := stream.(*ast.StreamStmt)
  78. if !ok {
  79. err = fmt.Errorf("Error resolving the stream %s, the data in db may be corrupted.", name)
  80. }
  81. return
  82. }