convert.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package vitess
  2. import (
  3. "fmt"
  4. "vitess.io/vitess/go/vt/sqlparser"
  5. "github.com/kyleconroy/sqlc/internal/sql/ast"
  6. "github.com/kyleconroy/sqlc/internal/sql/ast/pg"
  7. )
  8. func convertAliasedExpr(n *sqlparser.AliasedExpr) *pg.ResTarget {
  9. var name string
  10. if !n.As.IsEmpty() {
  11. name = n.As.String()
  12. }
  13. return &pg.ResTarget{
  14. Name: &name,
  15. Val: convert(n.Expr),
  16. }
  17. }
  18. func convertColName(n *sqlparser.ColName) *pg.ColumnRef {
  19. // TODO: Add table name if necessary
  20. return &pg.ColumnRef{
  21. Fields: &ast.List{
  22. Items: []ast.Node{
  23. &pg.String{
  24. Str: n.Name.String(),
  25. },
  26. },
  27. },
  28. }
  29. }
  30. func convertColumnType(n sqlparser.ColumnType) *ast.TypeName {
  31. return &ast.TypeName{
  32. Name: n.Type,
  33. }
  34. }
  35. func convertDDL(n *sqlparser.DDL) ast.Node {
  36. switch n.Action {
  37. case sqlparser.AddAutoIncStr:
  38. case sqlparser.AddColVindexStr:
  39. case sqlparser.AddSequenceStr:
  40. case sqlparser.AddVschemaTableStr:
  41. case sqlparser.AlterStr:
  42. case sqlparser.CreateStr:
  43. create := &ast.CreateTableStmt{
  44. Name: convertTableName(n.Table),
  45. IfNotExists: !n.IfExists,
  46. }
  47. if n.TableSpec == nil {
  48. return create
  49. }
  50. for _, def := range n.TableSpec.Columns {
  51. create.Cols = append(create.Cols, &ast.ColumnDef{
  52. Colname: def.Name.String(),
  53. TypeName: convertColumnType(def.Type),
  54. IsNotNull: bool(def.Type.NotNull),
  55. })
  56. }
  57. return create
  58. case sqlparser.CreateVindexStr:
  59. case sqlparser.DropColVindexStr:
  60. case sqlparser.DropStr:
  61. case sqlparser.DropVindexStr:
  62. case sqlparser.DropVschemaTableStr:
  63. case sqlparser.FlushStr:
  64. case sqlparser.RenameStr:
  65. case sqlparser.TruncateStr:
  66. default:
  67. panic("unknown DDL action: " + n.Action)
  68. }
  69. return &ast.TODO{}
  70. }
  71. func convertFuncExpr(n *sqlparser.FuncExpr) *ast.FuncCall {
  72. // TODO: Populate additional field names
  73. return &ast.FuncCall{
  74. Func: &ast.FuncName{
  75. Name: n.Name.String(),
  76. },
  77. Funcname: &ast.List{
  78. Items: []ast.Node{
  79. &pg.String{Str: n.Name.String()},
  80. },
  81. },
  82. Args: convertSelectExprs(n.Exprs),
  83. }
  84. }
  85. func convertSelectExprs(n sqlparser.SelectExprs) *ast.List {
  86. exprs := make([]ast.Node, len(n))
  87. for i := range n {
  88. exprs[i] = convert(n[i])
  89. }
  90. return &ast.List{Items: exprs}
  91. }
  92. func convertSelectStmt(n *sqlparser.Select) *pg.SelectStmt {
  93. return &pg.SelectStmt{
  94. TargetList: convertSelectExprs(n.SelectExprs),
  95. FromClause: convertTableExprs(n.From),
  96. }
  97. }
  98. func convertStarExpr(n *sqlparser.StarExpr) *pg.ResTarget {
  99. return &pg.ResTarget{
  100. Val: &pg.ColumnRef{
  101. Fields: &ast.List{
  102. Items: []ast.Node{
  103. &pg.A_Star{},
  104. },
  105. },
  106. },
  107. }
  108. }
  109. func convertTableExprs(n sqlparser.TableExprs) *ast.List {
  110. var tables []ast.Node
  111. err := sqlparser.Walk(func(n sqlparser.SQLNode) (bool, error) {
  112. table, ok := n.(sqlparser.TableName)
  113. if !ok {
  114. return true, nil
  115. }
  116. schema := table.Qualifier.String()
  117. rel := table.Name.String()
  118. tables = append(tables, &pg.RangeVar{
  119. Schemaname: &schema,
  120. Relname: &rel,
  121. })
  122. return false, nil
  123. }, n)
  124. if err != nil {
  125. panic(err)
  126. }
  127. return &ast.List{Items: tables}
  128. }
  129. func convertTableName(n sqlparser.TableName) *ast.TableName {
  130. return &ast.TableName{
  131. Schema: n.Qualifier.String(),
  132. Name: n.Name.String(),
  133. }
  134. }
  135. func convert(node sqlparser.SQLNode) ast.Node {
  136. switch n := node.(type) {
  137. case *sqlparser.AliasedExpr:
  138. return convertAliasedExpr(n)
  139. case *sqlparser.ColName:
  140. return convertColName(n)
  141. case *sqlparser.DDL:
  142. return convertDDL(n)
  143. case *sqlparser.Select:
  144. return convertSelectStmt(n)
  145. case *sqlparser.StarExpr:
  146. return convertStarExpr(n)
  147. case *sqlparser.Insert:
  148. return &ast.TODO{}
  149. case *sqlparser.Update:
  150. return &ast.TODO{}
  151. case *sqlparser.Delete:
  152. return &ast.TODO{}
  153. default:
  154. fmt.Printf("%T\n", n)
  155. return &ast.TODO{}
  156. }
  157. }