Skip to content

Commit ef89c92

Browse files
authored
Merge pull request #1 from zztkm/support-mysql-type
Support mysql type
2 parents 34be140 + d3d1c06 commit ef89c92

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

internal/gen.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ func pyInnerType(req *plugin.GenerateRequest, col *plugin.Column) string {
193193
switch req.Settings.Engine {
194194
case "postgresql":
195195
return postgresType(req, col)
196+
case "mysql":
197+
return mysqlType(req, col)
196198
default:
197199
log.Println("unsupported engine type")
198200
return "Any"
@@ -359,6 +361,15 @@ func sqlalchemySQL(s, engine string) string {
359361
s = strings.ReplaceAll(s, ":", `\\:`)
360362
if engine == "postgresql" {
361363
return postgresPlaceholderRegexp.ReplaceAllString(s, ":p$1")
364+
} else if engine == "mysql" {
365+
// All "?" in string s in string s are replaced with ":p1", ":p2", ... in that order
366+
parts := strings.Split(s, "?")
367+
for i := range parts {
368+
if i != 0 {
369+
parts[i] = fmt.Sprintf(":p%d%s", i, parts[i])
370+
}
371+
}
372+
return strings.Join(parts, "")
362373
}
363374
return s
364375
}

internal/mysql_type.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package python
2+
3+
import (
4+
"log"
5+
6+
"buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
7+
"github.com/sqlc-dev/sqlc-go/sdk"
8+
)
9+
10+
func mysqlType(req *plugin.CodeGenRequest, col *plugin.Column) string {
11+
columnType := sdk.DataType(col.Type)
12+
13+
switch columnType {
14+
15+
case "varchar", "text", "char", "tinytext", "mediumtext", "longtext":
16+
return "str"
17+
18+
case "tinyint":
19+
if col.Length == 1 {
20+
return "bool"
21+
} else {
22+
return "int"
23+
}
24+
25+
case "int", "integer", "smallint", "mediumint", "year":
26+
return "int"
27+
28+
case "bigint":
29+
return "int"
30+
31+
case "blob", "binary", "varbinary", "tinyblob", "mediumblob", "longblob":
32+
// TODO: Proper blob support
33+
return "Any"
34+
35+
case "double", "double precision", "real", "float":
36+
return "float"
37+
38+
case "decimal", "dec", "fixed":
39+
return "string"
40+
41+
case "enum":
42+
// TODO: Proper Enum support
43+
return "string"
44+
45+
case "date", "timestamp", "datetime", "time":
46+
return "datetime.date"
47+
48+
case "boolean", "bool":
49+
return "bool"
50+
51+
case "json":
52+
return "Any"
53+
54+
case "any":
55+
return "Any"
56+
57+
default:
58+
for _, schema := range req.Catalog.Schemas {
59+
for _, enum := range schema.Enums {
60+
if columnType == enum.Name {
61+
if schema.Name == req.Catalog.DefaultSchema {
62+
return "models." + modelName(enum.Name, req.Settings)
63+
}
64+
return "models." + modelName(schema.Name+"_"+enum.Name, req.Settings)
65+
}
66+
}
67+
}
68+
log.Printf("Unknown MySQL type: %s\n", columnType)
69+
return "Any"
70+
71+
}
72+
}

0 commit comments

Comments
 (0)