|
| 1 | +package golang |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/sqlc-dev/sqlc/internal/codegen/golang/opts" |
| 8 | + "github.com/sqlc-dev/sqlc/internal/codegen/sdk" |
| 9 | + "github.com/sqlc-dev/sqlc/internal/debug" |
| 10 | + "github.com/sqlc-dev/sqlc/internal/plugin" |
| 11 | +) |
| 12 | + |
| 13 | +func clickhouseType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string { |
| 14 | + dt := strings.ToLower(sdk.DataType(col.Type)) |
| 15 | + notNull := col.NotNull || col.IsArray |
| 16 | + emitPointersForNull := options.EmitPointersForNullTypes |
| 17 | + |
| 18 | + // Handle Nullable wrapper |
| 19 | + if strings.HasPrefix(dt, "nullable(") && strings.HasSuffix(dt, ")") { |
| 20 | + dt = dt[9 : len(dt)-1] |
| 21 | + notNull = false |
| 22 | + } |
| 23 | + |
| 24 | + // Handle LowCardinality wrapper |
| 25 | + if strings.HasPrefix(dt, "lowcardinality(") && strings.HasSuffix(dt, ")") { |
| 26 | + dt = dt[15 : len(dt)-1] |
| 27 | + } |
| 28 | + |
| 29 | + switch dt { |
| 30 | + // Integer types |
| 31 | + case "int8": |
| 32 | + if notNull { |
| 33 | + return "int8" |
| 34 | + } |
| 35 | + if emitPointersForNull { |
| 36 | + return "*int8" |
| 37 | + } |
| 38 | + return "sql.NullInt16" // No sql.NullInt8, use Int16 |
| 39 | + |
| 40 | + case "int16": |
| 41 | + if notNull { |
| 42 | + return "int16" |
| 43 | + } |
| 44 | + if emitPointersForNull { |
| 45 | + return "*int16" |
| 46 | + } |
| 47 | + return "sql.NullInt16" |
| 48 | + |
| 49 | + case "int32": |
| 50 | + if notNull { |
| 51 | + return "int32" |
| 52 | + } |
| 53 | + if emitPointersForNull { |
| 54 | + return "*int32" |
| 55 | + } |
| 56 | + return "sql.NullInt32" |
| 57 | + |
| 58 | + case "int64": |
| 59 | + if notNull { |
| 60 | + return "int64" |
| 61 | + } |
| 62 | + if emitPointersForNull { |
| 63 | + return "*int64" |
| 64 | + } |
| 65 | + return "sql.NullInt64" |
| 66 | + |
| 67 | + case "uint8": |
| 68 | + if notNull { |
| 69 | + return "uint8" |
| 70 | + } |
| 71 | + if emitPointersForNull { |
| 72 | + return "*uint8" |
| 73 | + } |
| 74 | + return "sql.NullInt16" // No sql.NullUint8 |
| 75 | + |
| 76 | + case "uint16": |
| 77 | + if notNull { |
| 78 | + return "uint16" |
| 79 | + } |
| 80 | + if emitPointersForNull { |
| 81 | + return "*uint16" |
| 82 | + } |
| 83 | + return "sql.NullInt32" // No sql.NullUint16 |
| 84 | + |
| 85 | + case "uint32": |
| 86 | + if notNull { |
| 87 | + return "uint32" |
| 88 | + } |
| 89 | + if emitPointersForNull { |
| 90 | + return "*uint32" |
| 91 | + } |
| 92 | + return "sql.NullInt64" // No sql.NullUint32 |
| 93 | + |
| 94 | + case "uint64": |
| 95 | + if notNull { |
| 96 | + return "uint64" |
| 97 | + } |
| 98 | + if emitPointersForNull { |
| 99 | + return "*uint64" |
| 100 | + } |
| 101 | + // Note: uint64 doesn't fit in sql.NullInt64 for large values |
| 102 | + return "sql.NullInt64" |
| 103 | + |
| 104 | + // Float types |
| 105 | + case "float32": |
| 106 | + if notNull { |
| 107 | + return "float32" |
| 108 | + } |
| 109 | + if emitPointersForNull { |
| 110 | + return "*float32" |
| 111 | + } |
| 112 | + return "sql.NullFloat64" |
| 113 | + |
| 114 | + case "float64": |
| 115 | + if notNull { |
| 116 | + return "float64" |
| 117 | + } |
| 118 | + if emitPointersForNull { |
| 119 | + return "*float64" |
| 120 | + } |
| 121 | + return "sql.NullFloat64" |
| 122 | + |
| 123 | + // String types |
| 124 | + case "string": |
| 125 | + if notNull { |
| 126 | + return "string" |
| 127 | + } |
| 128 | + if emitPointersForNull { |
| 129 | + return "*string" |
| 130 | + } |
| 131 | + return "sql.NullString" |
| 132 | + |
| 133 | + // Boolean type |
| 134 | + case "bool", "boolean": |
| 135 | + if notNull { |
| 136 | + return "bool" |
| 137 | + } |
| 138 | + if emitPointersForNull { |
| 139 | + return "*bool" |
| 140 | + } |
| 141 | + return "sql.NullBool" |
| 142 | + |
| 143 | + // Date and time types |
| 144 | + case "date", "date32": |
| 145 | + if notNull { |
| 146 | + return "time.Time" |
| 147 | + } |
| 148 | + if emitPointersForNull { |
| 149 | + return "*time.Time" |
| 150 | + } |
| 151 | + return "sql.NullTime" |
| 152 | + |
| 153 | + case "datetime", "datetime64": |
| 154 | + if notNull { |
| 155 | + return "time.Time" |
| 156 | + } |
| 157 | + if emitPointersForNull { |
| 158 | + return "*time.Time" |
| 159 | + } |
| 160 | + return "sql.NullTime" |
| 161 | + |
| 162 | + // UUID type |
| 163 | + case "uuid": |
| 164 | + if notNull { |
| 165 | + return "uuid.UUID" |
| 166 | + } |
| 167 | + if emitPointersForNull { |
| 168 | + return "*uuid.UUID" |
| 169 | + } |
| 170 | + return "uuid.NullUUID" |
| 171 | + |
| 172 | + // JSON type |
| 173 | + case "json": |
| 174 | + return "json.RawMessage" |
| 175 | + |
| 176 | + // Any type (for unknown types) |
| 177 | + case "any": |
| 178 | + return "interface{}" |
| 179 | + |
| 180 | + default: |
| 181 | + // Handle FixedString(N) |
| 182 | + if strings.HasPrefix(dt, "fixedstring") { |
| 183 | + if notNull { |
| 184 | + return "string" |
| 185 | + } |
| 186 | + if emitPointersForNull { |
| 187 | + return "*string" |
| 188 | + } |
| 189 | + return "sql.NullString" |
| 190 | + } |
| 191 | + |
| 192 | + // Handle Decimal types |
| 193 | + if strings.HasPrefix(dt, "decimal") { |
| 194 | + if notNull { |
| 195 | + return "float64" |
| 196 | + } |
| 197 | + if emitPointersForNull { |
| 198 | + return "*float64" |
| 199 | + } |
| 200 | + return "sql.NullFloat64" |
| 201 | + } |
| 202 | + |
| 203 | + // Handle Array types |
| 204 | + if strings.HasPrefix(dt, "array(") && strings.HasSuffix(dt, ")") { |
| 205 | + innerType := dt[6 : len(dt)-1] |
| 206 | + innerCol := &plugin.Column{ |
| 207 | + Type: &plugin.Identifier{Name: innerType}, |
| 208 | + NotNull: true, |
| 209 | + } |
| 210 | + return "[]" + clickhouseType(req, options, innerCol) |
| 211 | + } |
| 212 | + |
| 213 | + // Handle Enum types |
| 214 | + if strings.HasPrefix(dt, "enum8") || strings.HasPrefix(dt, "enum16") { |
| 215 | + if notNull { |
| 216 | + return "string" |
| 217 | + } |
| 218 | + if emitPointersForNull { |
| 219 | + return "*string" |
| 220 | + } |
| 221 | + return "sql.NullString" |
| 222 | + } |
| 223 | + |
| 224 | + // Handle Map types |
| 225 | + if strings.HasPrefix(dt, "map(") { |
| 226 | + return "map[string]interface{}" |
| 227 | + } |
| 228 | + |
| 229 | + // Handle Tuple types |
| 230 | + if strings.HasPrefix(dt, "tuple(") { |
| 231 | + return "interface{}" |
| 232 | + } |
| 233 | + |
| 234 | + if debug.Active { |
| 235 | + log.Printf("unknown ClickHouse type: %s\n", dt) |
| 236 | + } |
| 237 | + |
| 238 | + return "interface{}" |
| 239 | + } |
| 240 | +} |
0 commit comments