Skip to content

Support SELECT * in MySQL #529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions internal/compiler/go_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ func (r *Result) goInnerType(col *Column, settings config.CombinedSettings) stri
}
}

// TODO: Extend the engine interface to handle types
switch settings.Package.Engine {
case config.EngineMySQL, config.EngineXDolphin:
return r.mysqlType(col, settings)
case config.EnginePostgreSQL:
return r.postgresType(col, settings)
case config.EngineXLemon:
return r.postgresType(col, settings)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why wouldn't you have this in the same case statement as the line above?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have left a comment; calling postgresType for SQLite is a temporary solution. SQLite offers a very small number of built in types and should not use the same mapping.

default:
return "interface{}"
}
}

func (r *Result) postgresType(col *Column, settings config.CombinedSettings) string {
columnType := col.DataType
notNull := col.NotNull || col.IsArray

switch columnType {
case "serial", "pg_catalog.serial4":
if notNull {
Expand Down Expand Up @@ -186,3 +203,65 @@ func (r *Result) goInnerType(col *Column, settings config.CombinedSettings) stri
return "interface{}"
}
}

func (r *Result) mysqlType(col *Column, settings config.CombinedSettings) string {
columnType := col.DataType
notNull := col.NotNull || col.IsArray

switch columnType {

case "varchar", "text", "char", "tinytext", "mediumtext", "longtext":
if notNull {
return "string"
}
return "sql.NullString"

case "int", "integer", "smallint", "mediumint", "year":
if notNull {
return "int32"
}
return "sql.NullInt32"

case "bigint":
if notNull {
return "int64"
}
return "sql.NullInt64"

case "blob", "binary", "varbinary", "tinyblob", "mediumblob", "longblob":
return "[]byte"

case "double", "double precision", "real":
if notNull {
return "float64"
}
return "sql.NullFloat64"

case "decimal", "dec", "fixed":
if notNull {
return "string"
}
return "sql.NullString"

case "enum":
// TODO: Proper Enum support
return "string"

case "date", "timestamp", "datetime", "time":
if notNull {
return "time.Time"
}
return "sql.NullTime"

case "boolean", "bool", "tinyint":
if notNull {
return "bool"
}
return "sql.NullBool"

default:
log.Printf("unknown MySQL type: %s\n", columnType)
return "interface{}"

}
}
77 changes: 60 additions & 17 deletions internal/dolphin/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ func convertCreateTableStmt(n *pcast.CreateTableStmt) ast.Node {
IfNotExists: n.IfNotExists,
}
for _, def := range n.Cols {
var vals *ast.List
if len(def.Tp.Elems) > 0 {
vals = &ast.List{}
for i := range def.Tp.Elems {
vals.Items = append(vals.Items, &ast.String{
Str: def.Tp.Elems[i],
})
}
}
create.Cols = append(create.Cols, &ast.ColumnDef{
Colname: def.Name.String(),
TypeName: &ast.TypeName{Name: types.TypeStr(def.Tp.Tp)},
Expand All @@ -79,30 +88,64 @@ func convertDropTableStmt(n *pcast.DropTableStmt) ast.Node {
return drop
}

func convertSelectStmt(n *pcast.SelectStmt) ast.Node {
func convertFieldList(n *pcast.FieldList) *ast.List {
fields := make([]ast.Node, len(n.Fields))
for i := range n.Fields {
fields[i] = convertSelectField(n.Fields[i])
}
return &ast.List{Items: fields}
}

func convertSelectField(n *pcast.SelectField) *pg.ResTarget {
var val ast.Node
if n.WildCard != nil {
val = convertWildCardField(n.WildCard)
} else {
val = convert(n.Expr)
}
var name *string
if n.AsName.O != "" {
name = &n.AsName.O
}
return &pg.ResTarget{
// TODO: Populate Indirection field
Name: name,
Val: val,
Location: n.Offset,
}
}

func convertSelectStmt(n *pcast.SelectStmt) *pg.SelectStmt {
return &pg.SelectStmt{
TargetList: convertFieldList(n.Fields),
FromClause: convertTableRefsClause(n.From),
}
}

func convertTableRefsClause(n *pcast.TableRefsClause) *ast.List {
var tables []ast.Node
visit(n.From, func(n pcast.Node) {
visit(n, func(n pcast.Node) {
name, ok := n.(*pcast.TableName)
if !ok {
return
}
tables = append(tables, parseTableName(name))
})
var cols []ast.Node
visit(n.Fields, func(n pcast.Node) {
col, ok := n.(*pcast.ColumnName)
if !ok {
return
}
cols = append(cols, &ast.ResTarget{
Val: &ast.ColumnRef{
Name: col.Name.String(),
},
schema := name.Schema.String()
rel := name.Name.String()
tables = append(tables, &pg.RangeVar{
Schemaname: &schema,
Relname: &rel,
})
})
return &pg.SelectStmt{
FromClause: &ast.List{Items: tables},
TargetList: &ast.List{Items: cols},
return &ast.List{Items: tables}
}

func convertWildCardField(n *pcast.WildCardField) *pg.ColumnRef {
return &pg.ColumnRef{
Fields: &ast.List{
Items: []ast.Node{
&pg.A_Star{},
},
},
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/dolphin/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
Raw: &ast.RawStmt{
Stmt: out,
StmtLocation: loc,
StmtLen: len(text),
StmtLen: len(text) - 1, // Subtract one to remove semicolon
},
})
}
Expand Down
3 changes: 3 additions & 0 deletions internal/endtoend/testdata/dolphin_select_star/endtoend.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"experimental_parser_only": true
}
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/dolphin_select_star/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions internal/endtoend/testdata/dolphin_select_star/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions internal/endtoend/testdata/dolphin_select_star/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/endtoend/testdata/dolphin_select_star/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* name: GetAll :many */
SELECT * FROM users;
6 changes: 6 additions & 0 deletions internal/endtoend/testdata/dolphin_select_star/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE users (
id integer NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name varchar(255) NOT NULL,
last_name varchar(255),
age integer NOT NULL
) ENGINE=InnoDB;
13 changes: 13 additions & 0 deletions internal/endtoend/testdata/dolphin_select_star/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "1",
"packages": [
{
"name": "querytest",
"path": "go",
"schema": "schema.sql",
"queries": "query.sql",
"engine": "_dolphin",
"emit_json_tags": true
}
]
}
18 changes: 14 additions & 4 deletions internal/source/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,24 @@ func StripComments(sql string) (string, []string, error) {
s := bufio.NewScanner(strings.NewReader(strings.TrimSpace(sql)))
var lines, comments []string
for s.Scan() {
if strings.HasPrefix(s.Text(), "-- name:") {
t := s.Text()
if strings.HasPrefix(t, "-- name:") {
continue
}
if strings.HasPrefix(s.Text(), "--") {
comments = append(comments, strings.TrimPrefix(s.Text(), "--"))
if strings.HasPrefix(t, "/* name:") && strings.HasSuffix(t, "*/") {
continue
}
lines = append(lines, s.Text())
if strings.HasPrefix(t, "--") {
comments = append(comments, strings.TrimPrefix(t, "--"))
continue
}
if strings.HasPrefix(t, "/*") && strings.HasSuffix(t, "*/") {
t = strings.TrimPrefix(t, "/*")
t = strings.TrimSuffix(t, "*/")
comments = append(comments, t)
continue
}
lines = append(lines, t)
}
return strings.Join(lines, "\n"), comments, s.Err()
}
1 change: 1 addition & 0 deletions internal/sql/ast/column_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type ColumnDef struct {
TypeName *TypeName
IsNotNull bool
IsArray bool
Vals *List
}

func (n *ColumnDef) Pos() int {
Expand Down