LoadType inspects the database for typeName and produces a [pgtype.Type] suitable for registration. typeName must be the name of a type where the underlying type(s) is already understood by pgx. It is for derived types. In particular, typeName must be one of the following: - An array type name of a
(ctx context.Context, typeName string)
| 1288 | // - A range type name where the element type is already registered. |
| 1289 | // - A multirange type name where the element type is already registered. |
| 1290 | func (c *Conn) LoadType(ctx context.Context, typeName string) (*pgtype.Type, error) { |
| 1291 | var oid uint32 |
| 1292 | |
| 1293 | err := c.QueryRow(ctx, "select $1::text::regtype::oid;", typeName).Scan(&oid) |
| 1294 | if err != nil { |
| 1295 | return nil, err |
| 1296 | } |
| 1297 | |
| 1298 | var typtype string |
| 1299 | var typbasetype uint32 |
| 1300 | |
| 1301 | err = c.QueryRow(ctx, "select typtype::text, typbasetype from pg_type where oid=$1", oid).Scan(&typtype, &typbasetype) |
| 1302 | if err != nil { |
| 1303 | return nil, err |
| 1304 | } |
| 1305 | |
| 1306 | switch typtype { |
| 1307 | case "b": // array |
| 1308 | elementOID, err := c.getArrayElementOID(ctx, oid) |
| 1309 | if err != nil { |
| 1310 | return nil, err |
| 1311 | } |
| 1312 | |
| 1313 | dt, ok := c.TypeMap().TypeForOID(elementOID) |
| 1314 | if !ok { |
| 1315 | return nil, errors.New("array element OID not registered") |
| 1316 | } |
| 1317 | |
| 1318 | return &pgtype.Type{Name: typeName, OID: oid, Codec: &pgtype.ArrayCodec{ElementType: dt}}, nil |
| 1319 | case "c": // composite |
| 1320 | fields, err := c.getCompositeFields(ctx, oid) |
| 1321 | if err != nil { |
| 1322 | return nil, err |
| 1323 | } |
| 1324 | |
| 1325 | return &pgtype.Type{Name: typeName, OID: oid, Codec: &pgtype.CompositeCodec{Fields: fields}}, nil |
| 1326 | case "d": // domain |
| 1327 | dt, ok := c.TypeMap().TypeForOID(typbasetype) |
| 1328 | if !ok { |
| 1329 | return nil, errors.New("domain base type OID not registered") |
| 1330 | } |
| 1331 | |
| 1332 | return &pgtype.Type{Name: typeName, OID: oid, Codec: dt.Codec}, nil |
| 1333 | case "e": // enum |
| 1334 | return &pgtype.Type{Name: typeName, OID: oid, Codec: &pgtype.EnumCodec{}}, nil |
| 1335 | case "r": // range |
| 1336 | elementOID, err := c.getRangeElementOID(ctx, oid) |
| 1337 | if err != nil { |
| 1338 | return nil, err |
| 1339 | } |
| 1340 | |
| 1341 | dt, ok := c.TypeMap().TypeForOID(elementOID) |
| 1342 | if !ok { |
| 1343 | return nil, errors.New("range element OID not registered") |
| 1344 | } |
| 1345 | |
| 1346 | return &pgtype.Type{Name: typeName, OID: oid, Codec: &pgtype.RangeCodec{ElementType: dt}}, nil |
| 1347 | case "m": // multirange |