Skip to content

Commit

Permalink
feat: mysql support sqlc-dev#24
Browse files Browse the repository at this point in the history
  • Loading branch information
zztkm committed Oct 24, 2023
1 parent 6246d8b commit 2249174
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ func pyInnerType(req *plugin.CodeGenRequest, col *plugin.Column) string {
switch req.Settings.Engine {
case "postgresql":
return postgresType(req, col)
case "mysql":
return mysqlType(req, col)
default:
log.Println("unsupported engine type")
return "Any"
Expand Down
72 changes: 72 additions & 0 deletions internal/mysql_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package python

import (
"log"

"buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
"github.com/sqlc-dev/sqlc-go/sdk"
)

func mysqlType(req *plugin.CodeGenRequest, col *plugin.Column) string {
columnType := sdk.DataType(col.Type)

switch columnType {

case "varchar", "text", "char", "tinytext", "mediumtext", "longtext":
return "str"

case "tinyint":
if col.Length == 1 {
return "bool"
} else {
return "int"
}

case "int", "integer", "smallint", "mediumint", "year":
return "int"

case "bigint":
return "int"

case "blob", "binary", "varbinary", "tinyblob", "mediumblob", "longblob":
// TODO: Proper blob support
return "Any"

case "double", "double precision", "real", "float":
return "float"

case "decimal", "dec", "fixed":
return "string"

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

case "date", "timestamp", "datetime", "time":
return "datetime.date"

case "boolean", "bool":
return "bool"

case "json":
return "Any"

case "any":
return "Any"

default:
for _, schema := range req.Catalog.Schemas {
for _, enum := range schema.Enums {
if columnType == enum.Name {
if schema.Name == req.Catalog.DefaultSchema {
return "models." + modelName(enum.Name, req.Settings)
}
return "models." + modelName(schema.Name+"_"+enum.Name, req.Settings)
}
}
}
log.Printf("Unknown MySQL type: %s\n", columnType)
return "Any"

}
}

0 comments on commit 2249174

Please sign in to comment.