use of org.neo4j.ogm.request.Statement in project neo4j-ogm by neo4j.
the class CompilerTest method createSimpleRelationshipWithIllegalCharactersBetweenObjects.
// DATAGRAPH-589
@Test
public void createSimpleRelationshipWithIllegalCharactersBetweenObjects() {
Artist theBeatles = new Artist("The Beatles");
Album please = new Album("Please Please Me");
theBeatles.getAlbums().add(please);
please.setArtist(theBeatles);
Compiler compiler = mapAndCompile(theBeatles, -1);
List<Statement> statements = compiler.createNodesStatements();
assertThat(statements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row CREATE (n:`l'artiste`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type", "UNWIND $rows as row CREATE (n:`l'album`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
for (Statement statement : statements) {
List rows = (List) statement.getParameters().get("rows");
assertThat(rows).hasSize(1);
}
statements = compiler.createRelationshipsStatements();
assertThat(statements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`HAS-ALBUM`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
for (Statement statement : statements) {
List rows = (List) statement.getParameters().get("rows");
assertThat(rows).hasSize(1);
}
}
use of org.neo4j.ogm.request.Statement in project neo4j-ogm by neo4j.
the class CompilerTest method createIncomingRelationWhenSpecified.
// DATAGRAPH-594
@Test
public void createIncomingRelationWhenSpecified() {
Mortal adam = new Mortal("Adam");
Mortal vince = new Mortal("Vince");
adam.getKnownBy().add(vince);
Compiler compiler = mapAndCompile(adam, -1);
List<Statement> statements = compiler.createNodesStatements();
assertThat(statements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row CREATE (n:`Mortal`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
for (Statement statement : statements) {
List rows = (List) statement.getParameters().get("rows");
assertThat(rows).hasSize(2);
}
statements = compiler.createRelationshipsStatements();
assertThat(statements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`KNOWN_BY`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
List rows = (List) statements.get(0).getParameters().get("rows");
assertThat(rows).hasSize(1);
Map row = (Map) rows.get(0);
assertThat(row.get("startNodeId")).isEqualTo(mappingContext.nativeId(vince));
assertThat(row.get("endNodeId")).isEqualTo(mappingContext.nativeId(adam));
}
use of org.neo4j.ogm.request.Statement in project neo4j-ogm by neo4j.
the class CompilerTest method shouldCorrectlyPersistObjectGraphsSeveralLevelsDeep.
@Test
public void shouldCorrectlyPersistObjectGraphsSeveralLevelsDeep() {
Student sheila = new Student();
sheila.setName("Sheila Smythe");
Student gary = new Student();
gary.setName("Gary Jones");
Student winston = new Student();
winston.setName("Winston Charles");
Course physics = new Course();
physics.setName("GCSE Physics");
physics.setStudents(Arrays.asList(gary, sheila));
Course maths = new Course();
maths.setName("A-Level Mathematics");
maths.setStudents(Arrays.asList(sheila, winston));
Teacher teacher = new Teacher();
teacher.setName("Mrs Kapoor");
teacher.setCourses(Arrays.asList(physics, maths));
// Save teacher
Compiler compiler = mapAndCompile(teacher, -1);
List<Statement> createNodesStatements = compiler.createNodesStatements();
assertThat(createNodesStatements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row CREATE (n:`Teacher`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type", "UNWIND $rows as row CREATE (n:`Course`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type", "UNWIND $rows as row CREATE (n:`DomainObject`:`Student`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
for (Statement statement : createNodesStatements) {
List rows = (List) statement.getParameters().get("rows");
if (statement.getStatement().contains("Teacher")) {
assertThat(rows).hasSize(1);
}
if (statement.getStatement().contains("Student")) {
assertThat(rows).hasSize(3);
}
if (statement.getStatement().contains("Course")) {
assertThat(rows).hasSize(2);
}
}
List<Statement> createRelsStatements = compiler.createRelationshipsStatements();
assertThat(createRelsStatements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`COURSES`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type", "UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`STUDENTS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
for (Statement statement : createRelsStatements) {
List rows = (List) statement.getParameters().get("rows");
if (statement.getStatement().contains("STUDENTS")) {
assertThat(rows).hasSize(4);
}
if (statement.getStatement().contains("COURSES")) {
assertThat(rows).hasSize(2);
}
}
}
use of org.neo4j.ogm.request.Statement in project neo4j-ogm by neo4j.
the class RequestExecutor method executeStatements.
private void executeStatements(CompileContext context, List<ReferenceMapping> entityReferenceMappings, List<ReferenceMapping> relReferenceMappings, List<Statement> statements) {
if (statements.size() > 0) {
List<Statement> noCheckStatements = new ArrayList<>();
for (Statement statement : statements) {
if (statement.optimisticLockingConfig().isPresent()) {
DefaultRequest request = new DefaultRequest(statement);
try (Response<RowModel> response = session.requestHandler().execute(request)) {
List<RowModel> rowModels = response.toList();
session.optimisticLockingChecker().checkResultsCount(rowModels, statement);
registerEntityIds(context, rowModels, entityReferenceMappings, relReferenceMappings);
}
} else {
noCheckStatements.add(statement);
}
}
DefaultRequest defaultRequest = new DefaultRequest();
defaultRequest.setStatements(noCheckStatements);
try (Response<RowModel> response = session.requestHandler().execute(defaultRequest)) {
registerEntityIds(context, response.toList(), entityReferenceMappings, relReferenceMappings);
}
}
}
use of org.neo4j.ogm.request.Statement in project neo4j-ogm by neo4j.
the class EmbeddedRequest method execute.
@Override
public Response<RowModel> execute(DefaultRequest query) {
// TODO this is a hack to get the embedded driver to work with executing multiple statements
final List<RowModel> rowmodels = new ArrayList<>();
String[] columns = null;
for (Statement statement : query.getStatements()) {
Result result = executeRequest(statement);
if (columns == null) {
columns = result.columns().toArray(new String[result.columns().size()]);
}
RowModelResponse rowModelResponse = new RowModelResponse(result, entityAdapter);
RowModel model;
while ((model = rowModelResponse.next()) != null) {
rowmodels.add(model);
}
result.close();
}
final String[] finalColumns = columns;
return new Response<>() {
int currentRow = 0;
@Override
public RowModel next() {
if (currentRow < rowmodels.size()) {
return rowmodels.get(currentRow++);
}
return null;
}
@Override
public void close() {
}
@Override
public String[] columns() {
return finalColumns;
}
};
}
Aggregations