use of org.umlg.sqlg.structure.SchemaTable in project sqlg by pietermartin.
the class CockroachdbDialect method deleteEdges.
private void deleteEdges(SqlgGraph sqlgGraph, SchemaTable schemaTable, List<SqlgVertex> subVertices, Set<SchemaTable> labels, boolean inDirection) {
for (SchemaTable inLabel : labels) {
StringBuilder sql = new StringBuilder();
sql.append("DELETE FROM ");
sql.append(maybeWrapInQoutes(inLabel.getSchema()));
sql.append(".");
sql.append(maybeWrapInQoutes(inLabel.getTable()));
sql.append(" WHERE ");
sql.append(maybeWrapInQoutes(schemaTable.toString() + (inDirection ? Topology.IN_VERTEX_COLUMN_END : Topology.OUT_VERTEX_COLUMN_END)));
sql.append(" IN (");
int count = 1;
for (Vertex vertexToDelete : subVertices) {
sql.append("?");
if (count++ < subVertices.size()) {
sql.append(",");
}
}
sql.append(")");
if (sqlgGraph.getSqlDialect().needsSemicolon()) {
sql.append(";");
}
if (logger.isDebugEnabled()) {
logger.debug(sql.toString());
}
Connection conn = sqlgGraph.tx().getConnection();
try (PreparedStatement preparedStatement = conn.prepareStatement(sql.toString())) {
count = 1;
for (Vertex vertexToDelete : subVertices) {
preparedStatement.setLong(count++, ((RecordId) vertexToDelete.id()).getId());
}
int deleted = preparedStatement.executeUpdate();
if (logger.isDebugEnabled()) {
logger.debug("Deleted " + deleted + " edges from " + inLabel.toString());
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
use of org.umlg.sqlg.structure.SchemaTable in project sqlg by pietermartin.
the class TestCaptureSchemaTableEdges method testLoadTableLabels.
@Test
public void testLoadTableLabels() throws Exception {
Vertex person1 = this.sqlgGraph.addVertex(T.label, "Person", "name", "pieter");
Vertex car1 = this.sqlgGraph.addVertex(T.label, "Car", "name", "bmw");
person1.addEdge("drives", car1);
Vertex car2 = this.sqlgGraph.addVertex(T.label, "Car", "name", "toyota");
person1.addEdge("drives", car2);
Vertex bmw = this.sqlgGraph.addVertex(T.label, "Model", "name", "bmw");
car1.addEdge("model", bmw);
Vertex toyota = this.sqlgGraph.addVertex(T.label, "Model", "name", "toyota");
car2.addEdge("model", toyota);
this.sqlgGraph.tx().commit();
this.sqlgGraph.close();
try (SqlgGraph sqlgGraph = SqlgGraph.open(configuration)) {
Map<SchemaTable, Pair<Set<SchemaTable>, Set<SchemaTable>>> localTabels = sqlgGraph.getTopology().getTableLabels();
assertTrue(localTabels.containsKey(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "V_Person")));
assertTrue(localTabels.containsKey(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "V_Car")));
assertTrue(localTabels.containsKey(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "V_Model")));
Pair<Set<SchemaTable>, Set<SchemaTable>> person = localTabels.get(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "V_Person"));
assertEquals(0, person.getLeft().size());
assertEquals(1, person.getRight().size());
assertEquals(sqlgGraph.getSqlDialect().getPublicSchema() + ".E_drives", person.getRight().iterator().next().toString());
Pair<Set<SchemaTable>, Set<SchemaTable>> car = localTabels.get(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "V_Car"));
assertEquals(1, car.getLeft().size());
assertEquals(1, car.getRight().size());
assertEquals(sqlgGraph.getSqlDialect().getPublicSchema() + ".E_drives", car.getLeft().iterator().next().toString());
Pair<Set<SchemaTable>, Set<SchemaTable>> model = localTabels.get(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "V_Model"));
assertEquals(1, model.getLeft().size());
assertEquals(0, model.getRight().size());
assertEquals(sqlgGraph.getSqlDialect().getPublicSchema() + ".E_model", model.getLeft().iterator().next().toString());
}
}
use of org.umlg.sqlg.structure.SchemaTable in project sqlg by pietermartin.
the class TestCaptureSchemaTableEdges method testCaptureSchemaTableLabels.
@Test
public void testCaptureSchemaTableLabels() {
Vertex person1 = this.sqlgGraph.addVertex(T.label, "Person", "name", "pieter");
Vertex car1 = this.sqlgGraph.addVertex(T.label, "Car", "name", "bmw");
person1.addEdge("drives", car1);
Vertex car2 = this.sqlgGraph.addVertex(T.label, "Car", "name", "toyota");
person1.addEdge("drives", car2);
Vertex bmw = this.sqlgGraph.addVertex(T.label, "Model", "name", "bmw");
car1.addEdge("model", bmw);
Vertex toyota = this.sqlgGraph.addVertex(T.label, "Model", "name", "toyota");
car2.addEdge("model", toyota);
this.sqlgGraph.tx().commit();
Map<SchemaTable, Pair<Set<SchemaTable>, Set<SchemaTable>>> localTabels = this.sqlgGraph.getTopology().getTableLabels();
assertTrue(localTabels.containsKey(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "V_Person")));
assertTrue(localTabels.containsKey(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "V_Car")));
assertTrue(localTabels.containsKey(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "V_Model")));
Pair<Set<SchemaTable>, Set<SchemaTable>> person = localTabels.get(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "V_Person"));
assertEquals(0, person.getLeft().size());
assertEquals(1, person.getRight().size());
assertEquals(this.sqlgGraph.getSqlDialect().getPublicSchema() + ".E_drives", person.getRight().iterator().next().toString());
Pair<Set<SchemaTable>, Set<SchemaTable>> car = localTabels.get(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "V_Car"));
assertEquals(1, car.getLeft().size());
assertEquals(1, car.getRight().size());
assertEquals(this.sqlgGraph.getSqlDialect().getPublicSchema() + ".E_drives", car.getLeft().iterator().next().toString());
Pair<Set<SchemaTable>, Set<SchemaTable>> model = localTabels.get(SchemaTable.of(this.sqlgGraph.getSqlDialect().getPublicSchema(), "V_Model"));
assertEquals(1, model.getLeft().size());
assertEquals(0, model.getRight().size());
assertEquals(this.sqlgGraph.getSqlDialect().getPublicSchema() + ".E_model", model.getLeft().iterator().next().toString());
}
use of org.umlg.sqlg.structure.SchemaTable in project sqlg by pietermartin.
the class EdgeLabel method ensureEdgeVertexLabelExist.
public void ensureEdgeVertexLabelExist(Direction direction, VertexLabel vertexLabel) {
// if the direction is OUT then the vertexLabel must be in the same schema as the edgeLabel (this)
if (direction == Direction.OUT) {
Preconditions.checkState(vertexLabel.getSchema().equals(getSchema()), "For Direction.OUT the VertexLabel must be in the same schema as the edge. Found %s and %s", vertexLabel.getSchema().getName(), getSchema().getName());
}
SchemaTable foreignKey = SchemaTable.of(vertexLabel.getSchema().getName(), vertexLabel.getLabel() + (direction == Direction.IN ? Topology.IN_VERTEX_COLUMN_END : Topology.OUT_VERTEX_COLUMN_END));
if (!foreignKeysContains(direction, vertexLabel)) {
// Make sure the current thread/transaction owns the lock
Schema schema = this.getSchema();
schema.getTopology().lock();
if (!foreignKeysContains(direction, vertexLabel)) {
TopologyManager.addLabelToEdge(this.sqlgGraph, this.getSchema().getName(), EDGE_PREFIX + getLabel(), direction == Direction.IN, foreignKey);
if (direction == Direction.IN) {
this.uncommittedInVertexLabels.add(vertexLabel);
vertexLabel.addToUncommittedInEdgeLabels(schema, this);
} else {
this.uncommittedOutVertexLabels.add(vertexLabel);
vertexLabel.addToUncommittedOutEdgeLabels(schema, this);
}
SchemaTable vertexSchemaTable = SchemaTable.of(vertexLabel.getSchema().getName(), vertexLabel.getLabel());
addEdgeForeignKey(schema.getName(), EDGE_PREFIX + getLabel(), foreignKey, vertexSchemaTable);
this.getSchema().getTopology().fire(this, vertexSchemaTable.toString(), TopologyChangeAction.ADD_IN_VERTEX_LABELTO_EDGE);
}
}
}
Aggregations