|
| 1 | +package python |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + |
| 6 | + "github.com/sqlc-dev/plugin-sdk-go/plugin" |
| 7 | + "github.com/sqlc-dev/plugin-sdk-go/sdk" |
| 8 | +) |
| 9 | + |
| 10 | +func mysqlType(req *plugin.GenerateRequest, col *plugin.Column) string { |
| 11 | + columnType := sdk.DataType(col.Type) |
| 12 | + |
| 13 | + switch columnType { |
| 14 | + case "tinyint", "smallint", "mediumint", "int", "integer", "bigint": |
| 15 | + return "int" |
| 16 | + case "float", "double", "real": |
| 17 | + return "float" |
| 18 | + case "decimal", "numeric": |
| 19 | + return "decimal.Decimal" |
| 20 | + case "bit", "boolean", "bool": |
| 21 | + return "bool" |
| 22 | + case "json": |
| 23 | + return "Any" |
| 24 | + case "binary", "varbinary", "blob", "tinyblob", "mediumblob", "longblob": |
| 25 | + return "memoryview" |
| 26 | + case "date": |
| 27 | + return "datetime.date" |
| 28 | + case "time": |
| 29 | + return "datetime.time" |
| 30 | + case "datetime", "timestamp": |
| 31 | + return "datetime.datetime" |
| 32 | + case "char", "varchar", "text", "tinytext", "mediumtext", "longtext": |
| 33 | + return "str" |
| 34 | + case "enum", "set": |
| 35 | + return "str" |
| 36 | + default: |
| 37 | + for _, schema := range req.Catalog.Schemas { |
| 38 | + if schema.Name == "information_schema" || schema.Name == "mysql" { |
| 39 | + continue |
| 40 | + } |
| 41 | + for _, enum := range schema.Enums { |
| 42 | + if columnType == enum.Name { |
| 43 | + if schema.Name == req.Catalog.DefaultSchema { |
| 44 | + return "models." + modelName(enum.Name, req.Settings) |
| 45 | + } |
| 46 | + return "models." + modelName(schema.Name+"_"+enum.Name, req.Settings) |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + log.Printf("unknown MySQL type: %s\n", columnType) |
| 51 | + return "Any" |
| 52 | + } |
| 53 | +} |
0 commit comments