memgraph/include/query_engine/code_generator/handlers/create.hpp
2016-08-10 09:39:02 +01:00

86 lines
3.3 KiB
C++

#pragma once
#include "query_engine/code_generator/handlers/includes.hpp"
auto create_query_action =
[](CypherStateData &cypher_data,
const QueryActionData &action_data) -> std::string {
std::string code = "";
for (auto const &kv : action_data.actions) {
if (kv.second == ClauseAction::CreateNode) {
// create node
auto &name = kv.first;
code += code_line(code::create_vertex, name);
// update properties
code += update_properties(action_data, name);
// update labels
auto entity_data = action_data.get_entity_property(name);
for (auto &label : entity_data.tags) {
code += code_line(code::create_label, label);
code += code_line(code::add_label, name, label);
}
// mark node as created
cypher_data.node_created(name);
}
if (kv.second == ClauseAction::CreateRelationship) {
// create relationship
auto name = kv.first;
code += code_line(code::create_edge, name);
// update properties
code += update_properties(action_data, name);
// update tag
auto entity_data = action_data.get_entity_property(name);
for (auto &tag : entity_data.tags) {
code += code_line(code::find_type, tag);
code += code_line(code::set_type, name, tag);
}
// find start and end node
auto &relationships_data = action_data.relationship_data;
if (relationships_data.find(name) == relationships_data.end())
throw CodeGenerationError("Unable to find data for: " + name);
auto &relationship_data = relationships_data.at(name);
auto left_node = relationship_data.nodes.first;
auto right_node = relationship_data.nodes.second;
// TODO: If node isn't already matched or created it has to be
// created here. It is not possible for now.
if (cypher_data.status(left_node) != EntityStatus::Matched) {
throw SemanticError("Create Relationship: node " + left_node +
" can't be found");
}
if (cypher_data.status(right_node) != EntityStatus::Matched) {
throw SemanticError("Create Relationship: node " + right_node +
" can't be found");
}
// define direction
if (relationship_data.direction == Direction::Right) {
code += code_line(code::node_out, left_node, name);
code += code_line(code::node_in, right_node, name);
code += code_line(code::edge_from, name, left_node);
code += code_line(code::edge_to, name, right_node);
} else if (relationship_data.direction == Direction::Left) {
code += code_line(code::node_out, right_node, name);
code += code_line(code::node_in, left_node, name);
code += code_line(code::edge_from, name, right_node);
code += code_line(code::edge_to, name, left_node);
}
// mark relationship as created
cypher_data.relationship_created(name);
}
}
return code;
};