stmtx.go 2.4 KB

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