Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Open
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
12 changes: 11 additions & 1 deletion 12 internal/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,10 @@ func pyInnerType(req *plugin.GenerateRequest, 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")
log.Printf("unsupported engine type: %s\n", req.Settings.Engine)
return "Any"
}
}
Expand Down Expand Up @@ -360,6 +362,14 @@ func sqlalchemySQL(s, engine string) string {
if engine == "postgresql" {
return postgresPlaceholderRegexp.ReplaceAllString(s, ":p$1")
}
if engine == "mysql" {
// Convert MySQL ? placeholders to named parameters for SQLAlchemy compatibility
i := 1
for strings.Contains(s, "?") {
s = strings.Replace(s, "?", fmt.Sprintf(":p%d", i), 1)
i++
}
}
return s
}

Expand Down
53 changes: 53 additions & 0 deletions 53 internal/mysql_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package python

import (
"log"

"github.com/sqlc-dev/plugin-sdk-go/plugin"
"github.com/sqlc-dev/plugin-sdk-go/sdk"
)

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

switch columnType {
case "tinyint", "smallint", "mediumint", "int", "integer", "bigint":
return "int"
case "float", "double", "real":
return "float"
case "decimal", "numeric":
return "decimal.Decimal"
case "bit", "boolean", "bool":
return "bool"
case "json":
return "Any"
case "binary", "varbinary", "blob", "tinyblob", "mediumblob", "longblob":
return "memoryview"
case "date":
return "datetime.date"
case "time":
return "datetime.time"
case "datetime", "timestamp":
return "datetime.datetime"
case "char", "varchar", "text", "tinytext", "mediumtext", "longtext":
return "str"
case "enum", "set":
return "str"
default:
for _, schema := range req.Catalog.Schemas {
if schema.Name == "information_schema" || schema.Name == "mysql" {
continue
}
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"
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.