use of org.umlg.sqlg.structure.topology.VertexLabel in project sqlg by pietermartin.
the class SqlgEdge method load.
// TODO this needs optimizing, an edge created in the transaction need not go to the db to load itself again
@Override
protected void load() {
// recordId can be null when in batchMode
if (this.recordId != null && this.properties.isEmpty()) {
this.sqlgGraph.tx().readWrite();
if (this.sqlgGraph.getSqlDialect().supportsBatchMode() && this.sqlgGraph.tx().getBatchManager().isStreaming()) {
throw new IllegalStateException("streaming is in progress, first flush or commit before querying.");
}
// Generate the columns to prevent 'ERROR: cached plan must not change result type" error'
// This happens when the schema changes after the statement is prepared.
@SuppressWarnings("OptionalGetWithoutIsPresent") EdgeLabel edgeLabel = this.sqlgGraph.getTopology().getSchema(this.schema).get().getEdgeLabel(this.table).get();
StringBuilder sql = new StringBuilder("SELECT\n\t");
sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes("ID"));
for (PropertyColumn propertyColumn : edgeLabel.getProperties().values()) {
sql.append(", ");
sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(propertyColumn.getName()));
// additional columns for time zone, etc.
String[] ps = propertyColumn.getPropertyType().getPostFixes();
if (ps != null) {
for (String p : propertyColumn.getPropertyType().getPostFixes()) {
sql.append(", ");
sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(propertyColumn.getName() + p));
}
}
}
for (VertexLabel vertexLabel : edgeLabel.getOutVertexLabels()) {
sql.append(", ");
sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(vertexLabel.getSchema().getName() + "." + vertexLabel.getName() + Topology.OUT_VERTEX_COLUMN_END));
}
for (VertexLabel vertexLabel : edgeLabel.getInVertexLabels()) {
sql.append(", ");
sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(vertexLabel.getSchema().getName() + "." + vertexLabel.getName() + Topology.IN_VERTEX_COLUMN_END));
}
sql.append("\nFROM\n\t");
sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(this.schema));
sql.append(".");
sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(EDGE_PREFIX + this.table));
sql.append(" WHERE ");
sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes("ID"));
sql.append(" = ?");
if (this.sqlgGraph.getSqlDialect().needsSemicolon()) {
sql.append(";");
}
Connection conn = this.sqlgGraph.tx().getConnection();
if (logger.isDebugEnabled()) {
logger.debug(sql.toString());
}
try (PreparedStatement preparedStatement = conn.prepareStatement(sql.toString())) {
preparedStatement.setLong(1, this.recordId.getId());
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
loadResultSet(resultSet);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
Aggregations