sourceStmt.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2021 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. }
  77. func (o Options) node() {}
  78. type ShowStreamsStatement struct {
  79. Statement
  80. }
  81. type DescribeStreamStatement struct {
  82. Name string
  83. Statement
  84. }
  85. type ExplainStreamStatement struct {
  86. Name string
  87. Statement
  88. }
  89. type DropStreamStatement struct {
  90. Name string
  91. Statement
  92. }
  93. func (dss *DescribeStreamStatement) GetName() string { return dss.Name }
  94. func (ess *ExplainStreamStatement) GetName() string { return ess.Name }
  95. func (dss *DropStreamStatement) GetName() string { return dss.Name }
  96. type ShowTablesStatement struct {
  97. Statement
  98. }
  99. type DescribeTableStatement struct {
  100. Name string
  101. Statement
  102. }
  103. type ExplainTableStatement struct {
  104. Name string
  105. Statement
  106. }
  107. type DropTableStatement struct {
  108. Name string
  109. Statement
  110. }
  111. func (dss *DescribeTableStatement) GetName() string { return dss.Name }
  112. func (ess *ExplainTableStatement) GetName() string { return ess.Name }
  113. func (dss *DropTableStatement) GetName() string { return dss.Name }
  114. func printFieldTypeForJson(ft FieldType) (result interface{}) {
  115. r, q := doPrintFieldTypeForJson(ft)
  116. if q {
  117. return r
  118. } else {
  119. return json.RawMessage(r)
  120. }
  121. }
  122. func doPrintFieldTypeForJson(ft FieldType) (result string, isLiteral bool) {
  123. switch t := ft.(type) {
  124. case *BasicType:
  125. return t.Type.String(), true
  126. case *ArrayType:
  127. var (
  128. fieldType string
  129. q bool
  130. )
  131. if t.FieldType != nil {
  132. fieldType, q = doPrintFieldTypeForJson(t.FieldType)
  133. } else {
  134. fieldType, q = t.Type.String(), true
  135. }
  136. if q {
  137. result = fmt.Sprintf(`{"Type":"array","ElementType":"%s"}`, fieldType)
  138. } else {
  139. result = fmt.Sprintf(`{"Type":"array","ElementType":%s}`, fieldType)
  140. }
  141. case *RecType:
  142. result = `{"Type":"struct","Fields":[`
  143. isFirst := true
  144. for _, f := range t.StreamFields {
  145. if isFirst {
  146. isFirst = false
  147. } else {
  148. result += ","
  149. }
  150. fieldType, q := doPrintFieldTypeForJson(f.FieldType)
  151. if q {
  152. result = fmt.Sprintf(`%s{"FieldType":"%s","Name":"%s"}`, result, fieldType, f.Name)
  153. } else {
  154. result = fmt.Sprintf(`%s{"FieldType":%s,"Name":"%s"}`, result, fieldType, f.Name)
  155. }
  156. }
  157. result += `]}`
  158. }
  159. return result, false
  160. }