sourceStmt.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 ast
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. )
  19. const (
  20. TypeStream StreamType = iota
  21. TypeTable
  22. )
  23. var StreamTypeMap = map[StreamType]string{
  24. TypeStream: "stream",
  25. TypeTable: "table",
  26. }
  27. type StreamType int
  28. type StreamStmt struct {
  29. Name StreamName
  30. StreamFields StreamFields
  31. Options *Options
  32. StreamType StreamType //default to TypeStream
  33. Statement
  34. }
  35. type StreamField struct {
  36. Name string
  37. FieldType
  38. }
  39. func (u *StreamField) MarshalJSON() ([]byte, error) {
  40. return json.Marshal(&struct {
  41. FieldType interface{}
  42. Name string
  43. }{
  44. FieldType: printFieldTypeForJson(u.FieldType),
  45. Name: u.Name,
  46. })
  47. }
  48. type StreamFields []StreamField
  49. type FieldType interface {
  50. fieldType()
  51. }
  52. type BasicType struct {
  53. Type DataType
  54. FieldType
  55. }
  56. type ArrayType struct {
  57. Type DataType
  58. FieldType
  59. }
  60. type RecType struct {
  61. StreamFields StreamFields
  62. FieldType
  63. }
  64. // The stream AST tree
  65. type Options struct {
  66. DATASOURCE string
  67. KEY string
  68. FORMAT string
  69. CONF_KEY string
  70. TYPE string
  71. STRICT_VALIDATION bool
  72. TIMESTAMP string
  73. TIMESTAMP_FORMAT string
  74. RETAIN_SIZE int
  75. SHARED bool
  76. SCHEMAID string
  77. }
  78. func (o Options) node() {}
  79. type ShowStreamsStatement struct {
  80. Statement
  81. }
  82. type DescribeStreamStatement struct {
  83. Name string
  84. Statement
  85. }
  86. type ExplainStreamStatement struct {
  87. Name string
  88. Statement
  89. }
  90. type DropStreamStatement struct {
  91. Name string
  92. Statement
  93. }
  94. func (dss *DescribeStreamStatement) GetName() string { return dss.Name }
  95. func (ess *ExplainStreamStatement) GetName() string { return ess.Name }
  96. func (dss *DropStreamStatement) GetName() string { return dss.Name }
  97. type ShowTablesStatement struct {
  98. Statement
  99. }
  100. type DescribeTableStatement struct {
  101. Name string
  102. Statement
  103. }
  104. type ExplainTableStatement struct {
  105. Name string
  106. Statement
  107. }
  108. type DropTableStatement struct {
  109. Name string
  110. Statement
  111. }
  112. func (dss *DescribeTableStatement) GetName() string { return dss.Name }
  113. func (ess *ExplainTableStatement) GetName() string { return ess.Name }
  114. func (dss *DropTableStatement) GetName() string { return dss.Name }
  115. func printFieldTypeForJson(ft FieldType) (result interface{}) {
  116. r, q := doPrintFieldTypeForJson(ft)
  117. if q {
  118. return r
  119. } else {
  120. return json.RawMessage(r)
  121. }
  122. }
  123. func doPrintFieldTypeForJson(ft FieldType) (result string, isLiteral bool) {
  124. switch t := ft.(type) {
  125. case *BasicType:
  126. return t.Type.String(), true
  127. case *ArrayType:
  128. var (
  129. fieldType string
  130. q bool
  131. )
  132. if t.FieldType != nil {
  133. fieldType, q = doPrintFieldTypeForJson(t.FieldType)
  134. } else {
  135. fieldType, q = t.Type.String(), true
  136. }
  137. if q {
  138. result = fmt.Sprintf(`{"Type":"array","ElementType":"%s"}`, fieldType)
  139. } else {
  140. result = fmt.Sprintf(`{"Type":"array","ElementType":%s}`, fieldType)
  141. }
  142. case *RecType:
  143. result = `{"Type":"struct","Fields":[`
  144. isFirst := true
  145. for _, f := range t.StreamFields {
  146. if isFirst {
  147. isFirst = false
  148. } else {
  149. result += ","
  150. }
  151. fieldType, q := doPrintFieldTypeForJson(f.FieldType)
  152. if q {
  153. result = fmt.Sprintf(`%s{"FieldType":"%s","Name":"%s"}`, result, fieldType, f.Name)
  154. } else {
  155. result = fmt.Sprintf(`%s{"FieldType":%s,"Name":"%s"}`, result, fieldType, f.Name)
  156. }
  157. }
  158. result += `]}`
  159. }
  160. return result, false
  161. }