Beispiel #1
0
def type_to_ql_typeref(t: s_obj.Object,
                       *,
                       _name=None,
                       ctx: context.ContextLevel) -> qlast.TypeName:
    if t.is_any():
        result = qlast.TypeName(name=_name, maintype=qlast.AnyType())
    elif t.is_anytuple():
        result = qlast.TypeName(name=_name, maintype=qlast.AnyTuple())
    elif not isinstance(t, s_abc.Collection):
        result = qlast.TypeName(name=_name,
                                maintype=qlast.ObjectRef(
                                    module=t.get_name(ctx.env.schema).module,
                                    name=t.get_name(ctx.env.schema).name))
    elif isinstance(t, s_abc.Tuple) and t.named:
        result = qlast.TypeName(
            name=_name,
            maintype=qlast.ObjectRef(name=t.schema_name),
            subtypes=[
                type_to_ql_typeref(st, _name=sn, ctx=ctx)
                for sn, st in t.iter_subtypes(ctx.env.schema)
            ])
    else:
        result = qlast.TypeName(name=_name,
                                maintype=qlast.ObjectRef(name=t.schema_name),
                                subtypes=[
                                    type_to_ql_typeref(st, ctx=ctx)
                                    for st in t.get_subtypes(ctx.env.schema)
                                ])

    return result
Beispiel #2
0
def pg_type_from_object(schema: s_schema.Schema,
                        obj: s_obj.Object,
                        persistent_tuples: bool = False) -> Tuple[str, ...]:

    if isinstance(obj, s_scalars.ScalarType):
        return pg_type_from_scalar(schema, obj)

    elif obj.is_type() and obj.is_anytuple(schema):
        return ('record', )

    elif isinstance(obj, s_abc.Tuple):
        if persistent_tuples:
            return common.get_tuple_backend_name(obj.id, catenate=False)
        else:
            return ('record', )

    elif isinstance(obj, s_abc.Array):
        if obj.is_polymorphic(schema):
            return ('anyarray', )
        else:
            tp = pg_type_from_object(schema,
                                     obj.get_subtypes(schema)[0],
                                     persistent_tuples=persistent_tuples)
            return pg_type_array(tp)

    elif isinstance(obj, s_objtypes.ObjectType):
        return ('uuid', )

    elif obj.is_type() and obj.is_any(schema):
        return ('anyelement', )

    else:
        raise ValueError(f'could not determine PG type for {obj!r}')