def _entity_by_id_builder(self, entity, id_val, add_labels=False): qv = entity.query_variable if not qv: qv = VM.set_query_var(entity) _id = VM.get_next(entity, 'id') _id = Param(_id, id_val) node_kwargs = {} if add_labels: node_kwargs['labels'] = entity.labels if isinstance(entity, Relationship): node = __.node(**node_kwargs).relationship(qv).node() else: node = __.node(qv, **node_kwargs) where = __.ID(qv) == _id self.matches.append(node) self.wheres.append(where) self.returns.append(entity.query_variable) return self
def create_node(self, entity): mapper = get_mapper(entity) has_unique = len(mapper.unique_properties()) > 0 props = self._properties(entity, has_unique) node = __.node(entity.query_variable, labels=entity.labels, **props) # if the entity has unique properties, we will build a MERGE statement # that looks like: # # MERGE (keanu:Person { name: 'Keanu Reeves' }) # ON CREATE SET keanu.created = timestamp(), kenau.name = 'name # ON MATCH SET keanu.created = timestamp(), kenau.name = 'name # RETURN keanu # # if it doesnt have unique properties, it will build a CREATE statement # that looks like: # # CREATE (keanu:Person { name: 'Keanu Reeves' }) return keanu if has_unique: full_props = self._properties(entity) self.merges.append(node) for field, value in full_props.items(): stmt = getattr(__, entity.query_variable).property(field)._ stmt == value self.on_create_sets.append(stmt) self.on_match_sets.append(stmt) else: self.creates.append(node) self.returns.append(entity.query_variable) return self
def _node_by_id(self, entity): qv = entity.query_variable if not qv: qv = VM.set_query_var(entity) _id = VM.get_next(entity, 'id') _id = Param(_id, entity.id) return __.node(qv).WHERE(__.ID(qv) == _id)
def delete_node(self, entity): _id = VM.get_next(entity, 'id') _id = Param(_id, entity.id) match = __.node(entity.query_variable) match.WHERE(__.ID(entity.query_variable) == _id) self.matches.append(match) self.deletes.append(entity.query_variable) return self
def delete_relationship(self, entity): _id = VM.get_next(entity, 'id') _id = Param(_id, entity.id) match = __.node() match.rel(entity.query_variable, labels=entity.labels) match.node() match.WHERE(__.ID(entity.query_variable) == _id) self.matches.append(match) self.deletes.append(entity.query_variable) return self
def test_can_clone_nested_pypher(self): p = Pypher() d = Pypher() e = Pypher() e.CAND(1, 2, 3, 4, 5, __.test.this.out.CONDITIONAL(9, 9, 8, __.node(6))) d.id(123).raw(e) p.a.b.c.d.node(d == 122) c = p.clone() x = str(p) y = str(c) self.assertEqual(x, y) self.assertEqual(len(p.bound_params), len(c.bound_params)) self.assertTrue(id(p.bound_params) == id(c.bound_params))
def test_can_assign_variable(self): p = Pypher() p.MATCH.p.assign(__.node('n').rel_out().node('m')) exp = 'MATCH p = (n)-->(m)' self.assertEqual(str(p), exp)