def _cmd_tree_from_ast( cls, schema: s_schema.Schema, astnode: qlast.DDLOperation, context: sd.CommandContext, ) -> referencing.AlterReferencedInheritingObject[Link]: cmd = super()._cmd_tree_from_ast(schema, astnode, context) assert isinstance(cmd, pointers.PointerCommand) if isinstance(astnode, qlast.CreateConcreteLink): cmd._process_create_or_alter_ast(schema, astnode, context) else: expr_cmd = qlast.get_ddl_field_command(astnode, 'expr') if expr_cmd is not None: expr = expr_cmd.value if expr is not None: qlcompiler.normalize(expr, schema=schema, modaliases=context.modaliases) target_ref = pointers.ComputableRef(expr) cmd.set_attribute_value( 'target', target_ref, source_context=expr.context, ) cmd.discard_attribute('expr') assert isinstance(cmd, referencing.AlterReferencedInheritingObject) return cmd
def from_ast( cls: Type[Expression], qltree: qlast_.Expr, schema: s_schema.Schema, modaliases: Optional[Mapping[Optional[str], str]] = None, localnames: AbstractSet[str] = frozenset(), *, as_fragment: bool = False, ) -> Expression: if modaliases is None: modaliases = {} if not as_fragment: qlcompiler.normalize( qltree, schema=schema, modaliases=modaliases, localnames=localnames ) norm_text = qlcodegen.generate_source(qltree, pretty=False) return cls( text=norm_text, _qlast=qltree, )
def _cmd_tree_from_ast( cls, schema: s_schema.Schema, astnode: qlast.DDLOperation, context: sd.CommandContext, ) -> sd.Command: cmd = super()._cmd_tree_from_ast(schema, astnode, context) assert isinstance(astnode, qlast.CreateGlobal) assert isinstance(cmd, GlobalCommand) if astnode.is_required is not None: cmd.set_attribute_value( 'required', astnode.is_required, source_context=astnode.context, ) if astnode.cardinality is not None: cmd.set_attribute_value( 'cardinality', astnode.cardinality, source_context=astnode.context, ) assert astnode.target is not None target_ref: Union[s_types.TypeShell[s_types.Type], s_pointers.ComputableRef] if isinstance(astnode.target, qlast.TypeExpr): type_ref = utils.ast_to_type_shell( astnode.target, metaclass=s_types.Type, modaliases=context.modaliases, schema=schema, ) cmd.set_attribute_value( 'target', type_ref, source_context=astnode.target.context, ) else: # computable qlcompiler.normalize(astnode.target, schema=schema, modaliases=context.modaliases) cmd.set_attribute_value( 'expr', s_expr.Expression.from_ast( astnode.target, schema, context.modaliases, context.localnames, ), ) if (cmd.has_attribute_value('expr') and cmd.has_attribute_value('target')): raise errors.UnsupportedFeatureError( "cannot specify a type and an expression for a global", context=astnode.context, ) return cmd