sourceStmt.go 3.2 KB

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