def labels(self): """ The full set of labels to apply to the merged node. :rtype: :class:`py2neo.LabelSet` """ l = LabelSet(self.__labels) l.update(self.__node.labels) return l
def __init__(self, primary_label, primary_key=None, primary_value=None): CypherTask.__init__(self) self.__node = Node(primary_label) if primary_key is not None: self.__node.properties[primary_key] = CypherParameter("V", primary_value) self.__labels = LabelSet() self.__properties = PropertySet() self.__return = False
class MergeNode(CypherTask): """ :class:`.CypherTask` for `merging <http://neo4j.com/docs/stable/query-merge.html>`_ nodes. :: >>> from py2neo import Graph >>> graph = Graph() >>> tx = graph.cypher.begin() >>> tx.append(MergeNode("Person", "name", "Alice")) >>> tx.commit() | a ---+----------------------- 1 | (n170 {name:"Alice"}) """ def __init__(self, primary_label, primary_key=None, primary_value=None): CypherTask.__init__(self) self.__node = Node(primary_label) if primary_key is not None: self.__node.properties[primary_key] = CypherParameter( "V", primary_value) self.__labels = LabelSet() self.__properties = PropertySet() self.__return = False @property def labels(self): """ The full set of labels to apply to the merged node. :rtype: :class:`py2neo.LabelSet` """ l = LabelSet(self.__labels) l.update(self.__node.labels) return l @property def properties(self): """ The full set of properties to apply to the merged node. :rtype: :class:`py2neo.PropertySet` """ p = PropertySet(self.__properties) if self.primary_key: p[self.primary_key] = self.primary_value return p @property def primary_label(self): """ The label on which to merge. """ return list(self.__node.labels)[0] @property def primary_key(self): """ The property key on which to merge. """ try: return list(self.__node.properties.keys())[0] except IndexError: return None @property def primary_value(self): """ The property value on which to merge. """ try: return list(self.__node.properties.values())[0].value except IndexError: return None def set(self, *labels, **properties): """ Extra labels and properties to apply to the node. >>> merge = MergeNode("Person", "name", "Bob").set("Employee", employee_id=1234) """ self.__labels.update(labels) self.__properties.update(properties) return self def with_return(self): """ Include a RETURN clause in the statement. """ self.__return = True return self @property def statement(self): """ The full Cypher statement. """ string = StringIO() writer = CypherWriter(string) writer.write_literal("MERGE ") if self.__labels or self.__properties or self.__return: node_name = "a" else: node_name = None writer.write_node(self.__node, node_name) if self.__labels: writer.write_literal(" SET a") for label in self.__labels: writer.write_label(label) if self.__properties: writer.write_literal(" SET a={P}") if self.__return: writer.write_literal(" RETURN a") return string.getvalue() @property def parameters(self): """ Dictionary of parameters. """ parameters = {} if self.__node.properties: parameters["V"] = self.primary_value if self.__properties: parameters["P"] = self.properties return parameters
class MergeNode(CypherTask): """ :class:`.CypherTask` for `merging <http://neo4j.com/docs/stable/query-merge.html>`_ nodes. :: >>> from py2neo import Graph >>> graph = Graph() >>> tx = graph.cypher.begin() >>> tx.append(MergeNode("Person", "name", "Alice")) >>> tx.commit() | a ---+----------------------- 1 | (n170 {name:"Alice"}) """ def __init__(self, primary_label, primary_key=None, primary_value=None): CypherTask.__init__(self) self.__node = Node(primary_label) if primary_key is not None: self.__node.properties[primary_key] = CypherParameter("V", primary_value) self.__labels = LabelSet() self.__properties = PropertySet() self.__return = False @property def labels(self): """ The full set of labels to apply to the merged node. :rtype: :class:`py2neo.LabelSet` """ l = LabelSet(self.__labels) l.update(self.__node.labels) return l @property def properties(self): """ The full set of properties to apply to the merged node. :rtype: :class:`py2neo.PropertySet` """ p = PropertySet(self.__properties) if self.primary_key: p[self.primary_key] = self.primary_value return p @property def primary_label(self): """ The label on which to merge. """ return list(self.__node.labels)[0] @property def primary_key(self): """ The property key on which to merge. """ try: return list(self.__node.properties.keys())[0] except IndexError: return None @property def primary_value(self): """ The property value on which to merge. """ try: return list(self.__node.properties.values())[0].value except IndexError: return None def set(self, *labels, **properties): """ Extra labels and properties to apply to the node. >>> merge = MergeNode("Person", "name", "Bob").set("Employee", employee_id=1234) """ self.__labels.update(labels) self.__properties.update(properties) return self def with_return(self): """ Include a RETURN clause in the statement. """ self.__return = True return self @property def statement(self): """ The full Cypher statement. """ string = StringIO() writer = CypherWriter(string) writer.write_literal("MERGE ") if self.__labels or self.__properties or self.__return: node_name = "a" else: node_name = None writer.write_node(self.__node, node_name) if self.__labels: writer.write_literal(" SET a") for label in self.__labels: writer.write_label(label) if self.__properties: writer.write_literal(" SET a={P}") if self.__return: writer.write_literal(" RETURN a") return string.getvalue() @property def parameters(self): """ Dictionary of parameters. """ parameters = {} if self.__node.properties: parameters["V"] = self.primary_value if self.__properties: parameters["P"] = self.properties return parameters
def __init__(self, node, labels): Job.__init__(self, "PUT", Target(node, "labels"), list(LabelSet(labels)))