|
| 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