GoDuckDBTypeNameToPostgresType parses a type name reported by the go-duckdb driver into a corresponding pgtype.Type with its precision and scale (if applicable). Unknown types are fallback to text. TODO(fan): Make this function more rigorous for nested types.
(name string)
| 255 | // GoDuckDBTypeNameToPostgresType parses a type name reported by the go-duckdb driver |
| 256 | // into a corresponding pgtype.Type with its precision and scale (if applicable). |
| 257 | // Unknown types are fallback to text. |
| 258 | // |
| 259 | // TODO(fan): Make this function more rigorous for nested types. |
| 260 | func GoDuckDBTypeNameToPostgresType(name string) (pt *pgtype.Type, precision, scale int32, fallback bool, err error) { |
| 261 | var list bool |
| 262 | if strings.HasSuffix(name, "[]") { |
| 263 | // LIST type |
| 264 | // Ref: logicalTypeNameList in go-duckdb |
| 265 | name = strings.TrimSuffix(name, "[]") |
| 266 | list = true |
| 267 | } |
| 268 | |
| 269 | if strings.HasPrefix(name, "DECIMAL") { |
| 270 | // Scan precision and scale from the type name |
| 271 | // Ref: logicalTypeNameDecimal in go-duckdb |
| 272 | if _, err = fmt.Sscanf(name, "DECIMAL(%d,%d)", &precision, &scale); err != nil { |
| 273 | return nil, 0, 0, false, err |
| 274 | } |
| 275 | name = "DECIMAL" |
| 276 | } |
| 277 | pgTypeName, ok := duckdbTypeNameToPostgresTypeName[name] |
| 278 | if !ok { |
| 279 | pgTypeName, fallback = "text", true // Default to text if it is an unknown type |
| 280 | } |
| 281 | if list { |
| 282 | // the pgx/pgtype package prefixes array type names with an underscore: |
| 283 | // https://github.com/jackc/pgx/blob/master/pgtype/pgtype_default.go |
| 284 | pgTypeName = `_` + pgTypeName |
| 285 | } |
| 286 | pt, ok = DefaultTypeMap.TypeForName(pgTypeName) |
| 287 | if !ok { |
| 288 | pt, ok = DefaultTypeMap.TypeForName("text") |
| 289 | fallback = true |
| 290 | } |
| 291 | if !ok { |
| 292 | return nil, 0, 0, fallback, fmt.Errorf("unsupported type %s", name) |
| 293 | } |
| 294 | return |
| 295 | } |
no outgoing calls
no test coverage detected