def _apply_field_ast(self, context, node, op): objtype = context.get(LinkSourceCommandContext) if op.property == 'is_derived': pass elif op.property == 'spectargets': if op.new_value: node.targets = [ qlast.ObjectRef(name=t.classname.name, module=t.classname.module) for t in op.new_value ] elif op.property == 'default': self._encode_default(context, node, op) elif op.property == 'required': node.is_required = op.new_value elif op.property == 'source': pass elif op.property == 'search': if op.new_value: v = qlast.Constant(value=str(op.new_value.weight)) self._set_attribute_ast(context, node, 'search_weight', v) elif op.property == 'target' and objtype: if not node.targets: t = op.new_value node.targets = [utils.typeref_to_ast(t)] else: super()._apply_field_ast(context, node, op)
def _apply_field_ast(self, context, node, op): if op.property == 'spectargets': if op.new_value: node.commands.append(qlast.AlterTarget( targets=[ qlast.ObjectRef(name=t.classname.name, module=t.classname.module) for t in op.new_value ] )) elif op.property == 'target': if op.new_value: node.commands.append(qlast.AlterTarget( targets=[ qlast.ObjectRef( name=op.new_value.classname.name, module=op.new_value.classname.module) ] )) elif op.property == 'source': pass elif op.property == 'search': if op.new_value: v = qlast.Constant(value=str(op.new_value.weight)) self._set_attribute_ast(context, node, 'search_weight', v) else: self._drop_attribute_ast(context, node, 'search_weight') else: super()._apply_field_ast(context, node, op)
def _get_ast(self, context): value = self.new_value new_value_empty = \ (value is None or (isinstance(value, collections.Container) and not value)) old_value_empty = \ (self.old_value is None or (isinstance(self.old_value, collections.Container) and not self.old_value)) if new_value_empty and not old_value_empty: op = qlast.DropAttributeValue( name=qlast.ObjectRef(module='', name=self.property)) return op if new_value_empty and old_value_empty: return if isinstance(value, s_expr.ExpressionText): value = edgeql.parse(str(value)) elif utils.is_nontrivial_container(value): value = qlast.Tuple( elements=[qlast.Constant(value=el) for el in value]) elif isinstance(value, nlang.WordCombination): forms = value.as_dict() if len(forms) > 1: items = [] for k, v in forms.items(): items.append( (qlast.Constant(value=k), qlast.Constant(value=v))) value = qlast.Mapping(items=items) else: value = qlast.Constant(value=str(value)) else: value = qlast.Constant(value=value) as_expr = isinstance(value, qlast.ExpressionText) op = qlast.CreateAttributeValue(name=qlast.ObjectRef( module='', name=self.property), value=value, as_expr=as_expr) return op
def _encode_default(self, context, node, op): if op.new_value: expr = op.new_value if not isinstance(expr, sexpr.ExpressionText): expr_t = qlast.SelectQuery( result=qlast.Constant(value=expr) ) expr = edgeql.generate_source(expr_t, pretty=False) op.new_value = sexpr.ExpressionText(expr) super()._apply_field_ast(context, node, op)
def _apply_field_ast(self, context, node, op): if op.property == 'value': node.value = qlast.Constant(value=op.new_value) elif op.property == 'is_derived': pass elif op.property == 'attribute': pass elif op.property == 'subject': pass else: super()._apply_field_ast(context, node, op)
def computable_ptr_set(rptr: irast.Pointer, *, unnest_fence: bool = False, ctx: context.ContextLevel) -> irast.Set: """Return ir.Set for a pointer defined as a computable.""" ptrcls = rptr.ptrcls # Must use an entirely separate context, as the computable # expression is totally independent from the surrounding query. subctx = stmtctx.init_context(schema=ctx.schema) self_ = rptr.source source_scls = self_.scls # process_view() may generate computable pointer expressions # in the form "self.linkname". To prevent infinite recursion, # self must resolve to view base type, NOT the view type itself. if source_scls.is_view(): self_ = copy.copy(self_) self_.scls = source_scls.peel_view() self_.shape = [] subctx.anchors[qlast.Source] = self_ subctx.aliases = ctx.aliases subctx.stmt = ctx.stmt subctx.view_scls = ptrcls.target subctx.view_rptr = context.ViewRPtr(source_scls, ptrcls, rptr=rptr) subctx.toplevel_stmt = ctx.toplevel_stmt subctx.path_scope = ctx.path_scope subctx.class_shapes = ctx.class_shapes.copy() subctx.all_sets = ctx.all_sets subctx.path_scope_map = ctx.path_scope_map subctx.scope_id_ctr = ctx.scope_id_ctr if ptrcls.is_link_property(): source_path_id = rptr.source.path_id.ptr_path() else: source_path_id = rptr.target.path_id.src_path() path_id = source_path_id.extend(ptrcls, s_pointers.PointerDirection.Outbound, ptrcls.target) subctx.path_scope.contain_path(path_id) try: qlexpr, qlctx = ctx.source_map[ptrcls] except KeyError: if not ptrcls.default: raise ValueError( f'{ptrcls.shortname!r} is not a computable pointer') if isinstance(ptrcls.default, s_expr.ExpressionText): qlexpr = astutils.ensure_qlstmt(qlparser.parse(ptrcls.default)) else: qlexpr = qlast.Constant(value=ptrcls.default) qlctx = None else: subctx.modaliases = qlctx.modaliases.copy() subctx.aliased_views = qlctx.aliased_views.new_child() if source_scls.is_view(): subctx.aliased_views[self_.scls.name] = None subctx.source_map = qlctx.source_map.copy() subctx.view_nodes = qlctx.view_nodes.copy() subctx.view_sets = qlctx.view_sets.copy() subctx.view_map = qlctx.view_map.new_child() subctx.singletons = qlctx.singletons.copy() subctx.class_shapes = qlctx.class_shapes.copy() subctx.path_id_namespce = qlctx.path_id_namespace if qlctx is None: # This is a schema-level computable expression, put all # class refs into a separate namespace. subctx.path_id_namespace = (subctx.aliases.get('ns'), ) else: subns = subctx.pending_stmt_path_id_namespace = \ {irast.WeakNamespace(ctx.aliases.get('ns'))} self_view = ctx.view_sets.get(self_.scls) if self_view: if self_view.path_id.namespace: subns.update(self_view.path_id.namespace) inner_path_id = self_view.path_id.merge_namespace( subctx.path_id_namespace + tuple(subns)) else: if self_.path_id.namespace: subns.update(self_.path_id.namespace) inner_path_id = pathctx.get_path_id( self_.scls, ctx=subctx).merge_namespace(subns) remapped_source = new_set_from_set(rptr.source, ctx=subctx) remapped_source.path_id = \ remapped_source.path_id.merge_namespace(subns) subctx.view_map[inner_path_id] = remapped_source if isinstance(qlexpr, qlast.Statement) and unnest_fence: subctx.stmt_metadata[qlexpr] = context.StatementMetadata( is_unnest_fence=True) comp_ir_set = dispatch.compile(qlexpr, ctx=subctx) comp_ir_set.scls = ptrcls.target comp_ir_set.path_id = path_id comp_ir_set.rptr = rptr rptr.target = comp_ir_set return comp_ir_set
def visit_Literal(self, node): return qlast.Constant(value=node.value)
def _apply_fields_ast(self, context, node): super()._apply_fields_ast(context, node) for op in self(sd.AlterObjectProperty): if op.property == 'value': node.value = qlast.Constant(value=op.new_value)
def reduce_FALSE(self, *kids): self.val = qlast.Constant(value=False)
def reduce_TRUE(self, *kids): self.val = qlast.Constant(value=True)
def reduce_SCONST(self, *kids): self.val = qlast.Constant(value=str(kids[0].string))
def reduce_FCONST(self, *kids): self.val = qlast.Constant(value=float(kids[0].val))
def reduce_COLONGT_NL_INDENT_RawString_NL_DEDENT(self, *kids): text = kids[3].val.value text = textwrap.dedent(text).strip() self.val = qlast.Constant(value=text)