|
| 1 | +package dolphin |
| 2 | + |
| 3 | +import ( |
| 4 | + pcast "github.com/pingcap/parser/ast" |
| 5 | + "github.com/pingcap/parser/types" |
| 6 | + |
| 7 | + "github.com/kyleconroy/sqlc/internal/sql/ast" |
| 8 | + "github.com/kyleconroy/sqlc/internal/sql/ast/pg" |
| 9 | +) |
| 10 | + |
| 11 | +func convertAlterTableStmt(n *pcast.AlterTableStmt) ast.Node { |
| 12 | + alt := &ast.AlterTableStmt{ |
| 13 | + Table: parseTableName(n.Table), |
| 14 | + Cmds: &ast.List{}, |
| 15 | + } |
| 16 | + for _, spec := range n.Specs { |
| 17 | + switch spec.Tp { |
| 18 | + case pcast.AlterTableAddColumns: |
| 19 | + for _, def := range spec.NewColumns { |
| 20 | + name := def.Name.String() |
| 21 | + alt.Cmds.Items = append(alt.Cmds.Items, &ast.AlterTableCmd{ |
| 22 | + Name: &name, |
| 23 | + Subtype: ast.AT_AddColumn, |
| 24 | + Def: &ast.ColumnDef{ |
| 25 | + Colname: def.Name.String(), |
| 26 | + TypeName: &ast.TypeName{Name: types.TypeStr(def.Tp.Tp)}, |
| 27 | + IsNotNull: isNotNull(def), |
| 28 | + }, |
| 29 | + }) |
| 30 | + } |
| 31 | + |
| 32 | + case pcast.AlterTableDropColumn: |
| 33 | + name := spec.OldColumnName.String() |
| 34 | + alt.Cmds.Items = append(alt.Cmds.Items, &ast.AlterTableCmd{ |
| 35 | + Name: &name, |
| 36 | + Subtype: ast.AT_DropColumn, |
| 37 | + // MissingOk: spec.IfExists, |
| 38 | + }) |
| 39 | + |
| 40 | + case pcast.AlterTableChangeColumn: |
| 41 | + // spew.Dump("change column", spec) |
| 42 | + |
| 43 | + case pcast.AlterTableModifyColumn: |
| 44 | + // spew.Dump("modify column", spec) |
| 45 | + |
| 46 | + case pcast.AlterTableAlterColumn: |
| 47 | + // spew.Dump("alter column", spec) |
| 48 | + |
| 49 | + case pcast.AlterTableAddConstraint: |
| 50 | + // spew.Dump("add const", spec) |
| 51 | + |
| 52 | + default: |
| 53 | + continue |
| 54 | + } |
| 55 | + } |
| 56 | + return alt |
| 57 | +} |
| 58 | + |
| 59 | +func convertCreateTableStmt(n *pcast.CreateTableStmt) ast.Node { |
| 60 | + create := &ast.CreateTableStmt{ |
| 61 | + Name: parseTableName(n.Table), |
| 62 | + IfNotExists: n.IfNotExists, |
| 63 | + } |
| 64 | + for _, def := range n.Cols { |
| 65 | + create.Cols = append(create.Cols, &ast.ColumnDef{ |
| 66 | + Colname: def.Name.String(), |
| 67 | + TypeName: &ast.TypeName{Name: types.TypeStr(def.Tp.Tp)}, |
| 68 | + IsNotNull: isNotNull(def), |
| 69 | + }) |
| 70 | + } |
| 71 | + return create |
| 72 | +} |
| 73 | + |
| 74 | +func convertDropTableStmt(n *pcast.DropTableStmt) ast.Node { |
| 75 | + drop := &ast.DropTableStmt{IfExists: n.IfExists} |
| 76 | + for _, name := range n.Tables { |
| 77 | + drop.Tables = append(drop.Tables, parseTableName(name)) |
| 78 | + } |
| 79 | + return drop |
| 80 | +} |
| 81 | + |
| 82 | +func convertSelectStmt(n *pcast.SelectStmt) ast.Node { |
| 83 | + var tables []ast.Node |
| 84 | + visit(n.From, func(n pcast.Node) { |
| 85 | + name, ok := n.(*pcast.TableName) |
| 86 | + if !ok { |
| 87 | + return |
| 88 | + } |
| 89 | + tables = append(tables, parseTableName(name)) |
| 90 | + }) |
| 91 | + var cols []ast.Node |
| 92 | + visit(n.Fields, func(n pcast.Node) { |
| 93 | + col, ok := n.(*pcast.ColumnName) |
| 94 | + if !ok { |
| 95 | + return |
| 96 | + } |
| 97 | + cols = append(cols, &ast.ResTarget{ |
| 98 | + Val: &ast.ColumnRef{ |
| 99 | + Name: col.Name.String(), |
| 100 | + }, |
| 101 | + }) |
| 102 | + }) |
| 103 | + return &pg.SelectStmt{ |
| 104 | + FromClause: &ast.List{Items: tables}, |
| 105 | + TargetList: &ast.List{Items: cols}, |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func convert(node pcast.Node) ast.Node { |
| 110 | + switch n := node.(type) { |
| 111 | + |
| 112 | + case *pcast.AlterTableStmt: |
| 113 | + return convertAlterTableStmt(n) |
| 114 | + |
| 115 | + case *pcast.CreateTableStmt: |
| 116 | + return convertCreateTableStmt(n) |
| 117 | + |
| 118 | + case *pcast.DropTableStmt: |
| 119 | + return convertDropTableStmt(n) |
| 120 | + |
| 121 | + case *pcast.SelectStmt: |
| 122 | + return convertSelectStmt(n) |
| 123 | + |
| 124 | + default: |
| 125 | + return &ast.TODO{} |
| 126 | + } |
| 127 | +} |
0 commit comments