Skip to content

Commit b77c8df

Browse files
committed
A bunch of broken tests
1 parent f4e076b commit b77c8df

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

internal/dolphin/convert.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package dolphin
22

33
import (
44
pcast "github.com/pingcap/parser/ast"
5+
"github.com/pingcap/parser/types"
56

67
"github.com/kyleconroy/sqlc/internal/sql/ast"
8+
"github.com/kyleconroy/sqlc/internal/sql/ast/pg"
79
)
810

911
func convertAlterTableStmt(n *pcast.AlterTableStmt) ast.Node {
@@ -20,9 +22,9 @@ func convertAlterTableStmt(n *pcast.AlterTableStmt) ast.Node {
2022
Name: &name,
2123
Subtype: ast.AT_AddColumn,
2224
Def: &ast.ColumnDef{
23-
Colname: def.Name.String(),
24-
// TODO: Use def.Tp to generate type name
25-
TypeName: &ast.TypeName{Name: "text"},
25+
Colname: def.Name.String(),
26+
TypeName: &ast.TypeName{Name: types.TypeStr(def.Tp.Tp)},
27+
IsNotNull: isNotNull(def),
2628
},
2729
})
2830
}
@@ -61,9 +63,9 @@ func convertCreateTableStmt(n *pcast.CreateTableStmt) ast.Node {
6163
}
6264
for _, def := range n.Cols {
6365
create.Cols = append(create.Cols, &ast.ColumnDef{
64-
Colname: def.Name.String(),
65-
// TODO: Use n.Tp to generate type name
66-
TypeName: &ast.TypeName{Name: "text"},
66+
Colname: def.Name.String(),
67+
TypeName: &ast.TypeName{Name: types.TypeStr(def.Tp.Tp)},
68+
IsNotNull: isNotNull(def),
6769
})
6870
}
6971
return create
@@ -78,7 +80,6 @@ func convertDropTableStmt(n *pcast.DropTableStmt) ast.Node {
7880
}
7981

8082
func convertSelectStmt(n *pcast.SelectStmt) ast.Node {
81-
sel := &ast.SelectStmt{}
8283
var tables []ast.Node
8384
visit(n.From, func(n pcast.Node) {
8485
name, ok := n.(*pcast.TableName)
@@ -99,9 +100,10 @@ func convertSelectStmt(n *pcast.SelectStmt) ast.Node {
99100
},
100101
})
101102
})
102-
sel.From = &ast.List{Items: tables}
103-
sel.Fields = &ast.List{Items: cols}
104-
return sel
103+
return &pg.SelectStmt{
104+
FromClause: &ast.List{Items: tables},
105+
TargetList: &ast.List{Items: cols},
106+
}
105107
}
106108

107109
func convert(node pcast.Node) ast.Node {

internal/dolphin/parse.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dolphin
33
import (
44
"io"
55
"io/ioutil"
6+
"strings"
67

78
"github.com/pingcap/parser"
89
_ "github.com/pingcap/tidb/types/parser_driver"
@@ -33,10 +34,17 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
3334
if _, ok := out.(*ast.TODO); ok {
3435
continue
3536
}
37+
38+
// TODO: Attach the text directly to the ast.Statement node
39+
text := stmtNodes[i].Text()
40+
loc := strings.Index(string(blob), text)
41+
3642
stmts = append(stmts, ast.Statement{
37-
Raw: &ast.RawStmt{Stmt: out},
38-
// TODO: StmtLocation
39-
// TODO: StmtLen
43+
Raw: &ast.RawStmt{
44+
Stmt: out,
45+
StmtLocation: loc,
46+
StmtLen: len(text),
47+
},
4048
})
4149
}
4250
return stmts, nil

internal/dolphin/utils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,12 @@ func parseTableName(n *pcast.TableName) *ast.TableName {
7070
Name: n.Name.String(),
7171
}
7272
}
73+
74+
func isNotNull(n *pcast.ColumnDef) bool {
75+
for i := range n.Options {
76+
if n.Options[i].Tp == pcast.ColumnOptionNotNull {
77+
return true
78+
}
79+
}
80+
return false
81+
}

0 commit comments

Comments
 (0)