Skip to content

Support mysql type #1

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
Nov 21, 2023
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
11 changes: 11 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 Expand Up @@ -376,6 +378,15 @@ func sqlalchemySQL(s, engine string) string {
s = strings.ReplaceAll(s, ":", `\\:`)
if engine == "postgresql" {
return postgresPlaceholderRegexp.ReplaceAllString(s, ":p$1")
} else if engine == "mysql" {
// All "?" in string s in string s are replaced with ":p1", ":p2", ... in that order
parts := strings.Split(s, "?")
for i := range parts {
if i != 0 {
parts[i] = fmt.Sprintf(":p%d%s", i, parts[i])
}
}
return strings.Join(parts, "")
}
return s
}
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"

}
}