stmtx.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. StreamKind string `json:"streamKind"`
  53. Statement string `json:"statement"`
  54. }
  55. func GetDataSourceStatement(m kv.KeyValue, name string) (*StreamInfo, error) {
  56. var (
  57. v string
  58. vs = &StreamInfo{}
  59. )
  60. if ok, _ := m.Get(name, &v); ok {
  61. if err := json.Unmarshal([]byte(v), vs); err != nil {
  62. return nil, fmt.Errorf("error unmarshall %s, the data in db may be corrupted", name)
  63. } else {
  64. return vs, nil
  65. }
  66. }
  67. return nil, errorx.NewWithCode(errorx.NOT_FOUND, fmt.Sprintf("%s is not found", name))
  68. }
  69. func GetDataSource(m kv.KeyValue, name string) (stmt *ast.StreamStmt, err error) {
  70. info, err := GetDataSourceStatement(m, name)
  71. if err != nil {
  72. return nil, err
  73. }
  74. parser := NewParser(strings.NewReader(info.Statement))
  75. stream, err := Language.Parse(parser)
  76. stmt, ok := stream.(*ast.StreamStmt)
  77. if !ok {
  78. err = fmt.Errorf("Error resolving the stream %s, the data in db may be corrupted.", name)
  79. }
  80. return
  81. }