def merge_command(self, variable=None): properties = self._add_unique_constraint_index( flatten(self.entity.toDict())) return NodeCommands.merge_command_for_type(variable, entity_type=type( self.entity).__name__, **properties)
def search(self, keys, sep=None, flatten_node=False): dictionary = flatten(self.toDict()) if flatten_node else self.toDict() if flatten_node: sep, _ = get_defaults(sep, None) search_keys = sep.join(list(keys)) if isinstance( keys, MutableSequence) or type(keys) in (set, tuple) else keys return { k: dictionary[k] for k in dictionary.keys() if k.startswith(search_keys) } else: try: search_keys = list(keys) if isinstance( keys, MutableSequence) or type(keys) in (set, tuple) else [ keys ] keys_array = [ "[{key}]".format( key=str(k) if not type(k) == str else '"{key}"'.format( key=k)) for k in search_keys ] result = {} for key in reversed(search_keys): if len(result) == 0: result[key] = eval("dictionary" + "".join(keys_array)) else: result = {key: result} return result except KeyError: return None
def match_command_for_type(node_type=None, variable=None, excluded=None, **properties): props = {k: v for k, v in properties.items() if v is not None} if node_type is None and "__type" in props.keys() and len( str(props["__type"])) > 0: node_type = str(props["__type"]) if node_type is not None: variable_name = node_type[0].lower() if variable is None else str( variable) else: variable_name = str(variable) if variable is not None else "n" excluded = [] if excluded is None else excluded query_props = CypherCommands.properties_values_map( excluded=excluded, property_prefix=NodeCommands.property_prefix, **flatten(props)) if node_type is not None: return "MATCH ({name}:{type} {properties})".format( name=variable_name, type=node_type, properties=query_props) else: return "MATCH ({name} {properties})".format(name=variable_name, properties=query_props)
def match_command(self, variable=None, excluded=None, node_type=None): excluded = list(excluded) if isinstance( excluded, MutableSequence) or type(excluded) in (set, tuple) else [] excluded = list(set(NodeCommands.excluded_properties + excluded)) node_type = type( self.entity).__name__ if node_type is None else str(node_type) key, value = self._get_unique_constraint_index() # if there is an unique index on the node we use it for retrieval if key is not None and value is not None: properties = {key: value} else: properties = flatten(self.entity.toDict()) return NodeCommands.match_command_for_type(variable=variable, excluded=excluded, node_type=node_type, **properties)
def merge_command_for_type(variable=None, entity_type=None, **properties): return NodeCommands._create_command("MERGE", variable, node_type=entity_type, **flatten(properties))
def where_statement(self, boolean_operator=None, excluded=None): return NodeCommands.where_statement_for_type( boolean_operator=boolean_operator, excluded=excluded, **flatten(self.entity.toDict()))
def _create_command(verb, variable=None, entity_type=None, **properties): node1_var = "n1" node2_var = "n2" node1_type_name = properties["__node_1"]["__type"] node2_type_name = properties["__node_1"]["__type"] variable = variable if variable is not None else "r" def get_query_fields(node_type_name, props): if node_type_name in EntityClassGenerator.class_registry.keys(): node_type = EntityClassGenerator.class_registry[ node1_type_name] keys = [ key for key, value in node_type.get_properties_mapping().items() if value in node_type.get_unique_constraint_fields() ] unique_constraint_value = ":".join([node_type_name] + [ str(props[key]) if props[key] is not None else "" for key in keys ]) unique_constraint_name = NodeBaseClass.serialize( IndexesSupport.get_unique_constraint_name(node_type)) return {unique_constraint_name: unique_constraint_value} return props match1 = NodeCommands.match_command_for_type( variable=node1_var, node_type=node1_type_name, excluded=["__type"], **get_query_fields(node1_type_name, properties["__node_1"]["__object"])) match2 = NodeCommands.match_command_for_type( variable=node2_var, node_type=node2_type_name, excluded=["__type"], **get_query_fields(node2_type_name, properties["__node_2"]["__object"])) generate_match_cmd = match1 + "," + match2.replace("MATCH", "") if entity_type is not None: properties["__rel_type"] = entity_type properties_data = { k: v for k, v in properties.items() if k not in ["__direction", "__rel_type", "__node_1", "__node_2"] } relationship_props = CypherCommands.properties_values_map( **{ k: v for k, v in flatten(properties_data).items() if str(v).upper() != "NONE" }) definition = variable + (":" + str(properties["__rel_type"]) if "__rel_type" in properties.keys() else "") connector = RelationshipCommands._cypher_direction_command( properties["__direction"]).format( relationship=" ".join([definition, relationship_props])) create_command = "{verb} ({node1}){connector}({node2})".format( verb=verb, node1=node1_var, node2=node2_var, connector=connector) return_command = "RETURN " + ", ".join( [variable, node1_var, node2_var]) return " ".join([generate_match_cmd, create_command, return_command])