use of org.umlg.sqlg.structure.topology.PropertyColumn 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);
}
}
}
use of org.umlg.sqlg.structure.topology.PropertyColumn in project sqlg by pietermartin.
the class SqlgElement method removeGlobalUniqueIndex.
private void removeGlobalUniqueIndex() {
Map<String, PropertyColumn> properties = this.sqlgGraph.getTopology().getPropertiesWithGlobalUniqueIndexFor(this.getSchemaTablePrefixed());
for (PropertyColumn propertyColumn : properties.values()) {
for (GlobalUniqueIndex globalUniqueIndex : propertyColumn.getGlobalUniqueIndices()) {
StringBuilder sql = new StringBuilder("DELETE FROM ");
sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(Schema.GLOBAL_UNIQUE_INDEX_SCHEMA));
sql.append(".");
sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(VERTEX_PREFIX + globalUniqueIndex.getName()));
sql.append(" WHERE ");
sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes("recordId"));
sql.append(" = ? AND ");
sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes("property"));
sql.append(" = ?");
if (this.sqlgGraph.getSqlDialect().needsSemicolon()) {
sql.append(";");
}
if (logger.isDebugEnabled()) {
logger.debug(sql.toString());
}
Connection conn = this.sqlgGraph.tx().getConnection();
try (PreparedStatement preparedStatement = conn.prepareStatement(sql.toString())) {
preparedStatement.setString(1, this.id().toString());
preparedStatement.setString(2, propertyColumn.getName());
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
use of org.umlg.sqlg.structure.topology.PropertyColumn in project sqlg by pietermartin.
the class SqlgElement method updateRow.
private void updateRow(String key, Object value) {
boolean elementInInsertedCache = false;
if (this.sqlgGraph.getSqlDialect().supportsBatchMode() && this.sqlgGraph.tx().isInBatchMode()) {
elementInInsertedCache = this.sqlgGraph.tx().getBatchManager().updateProperty(this, key, value);
}
if (!elementInInsertedCache) {
Object oldValue = this.property(key).orElse(null);
if (oldValue != null && oldValue.equals(value)) {
return;
}
// TODO needs optimizing, firing this all the time when there probably are no GlobalUniqueIndexes is a tad dum.
// GlobalUniqueIndex
Map<String, PropertyColumn> properties;
if (this instanceof Vertex) {
properties = this.sqlgGraph.getTopology().getSchema(this.schema).orElseThrow(() -> new IllegalStateException(String.format("Schema %s not found", this.schema))).getVertexLabel(this.table).orElseThrow(() -> new IllegalStateException(String.format("VertexLabel %s not found", this.table))).getProperties();
} else {
properties = this.sqlgGraph.getTopology().getSchema(this.schema).orElseThrow(() -> new IllegalStateException(String.format("Schema %s not found", this.schema))).getEdgeLabel(this.table).orElseThrow(() -> new IllegalStateException(String.format("EdgeLabel %s not found", this.table))).getProperties();
}
// sync up the keyValueMap with its PropertyColumn
PropertyColumn propertyColumn = properties.get(key);
Pair<PropertyColumn, Object> propertyColumnObjectPair = Pair.of(propertyColumn, value);
for (GlobalUniqueIndex globalUniqueIndex : propertyColumn.getGlobalUniqueIndices()) {
SqlgElement.updateGlobalUniqueIndex(this.sqlgGraph, globalUniqueIndex, this.recordId, propertyColumnObjectPair);
}
String tableName = (this instanceof Vertex ? VERTEX_PREFIX : EDGE_PREFIX) + this.table;
StringBuilder sql = new StringBuilder("UPDATE ");
sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(this.schema));
sql.append(".");
sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(tableName));
sql.append(" SET ");
sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(key));
sql.append(" = ?");
// some data types require several columns in the db, make sure to update them all
PropertyType pt = PropertyType.from(value);
String[] postfixes = this.sqlgGraph.getSqlDialect().propertyTypeToSqlDefinition(pt);
if (postfixes != null && postfixes.length > 1) {
for (int i = 1; i < postfixes.length; i++) {
sql.append(",");
sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(key + pt.getPostFixes()[i - 1]));
sql.append(" = ?");
}
}
sql.append(" WHERE ");
sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes("ID"));
sql.append(" = ?");
if (this.sqlgGraph.getSqlDialect().needsSemicolon()) {
sql.append(";");
}
if (logger.isDebugEnabled()) {
logger.debug(sql.toString());
}
Connection conn = this.sqlgGraph.tx().getConnection();
try (PreparedStatement preparedStatement = conn.prepareStatement(sql.toString())) {
Map<String, Object> keyValue = new HashMap<>();
keyValue.put(key, value);
// the index of the id column in the statement depend on how many columns we had to use to store that data type
int idx = setKeyValuesAsParameter(this.sqlgGraph, 1, preparedStatement, keyValue);
preparedStatement.setLong(idx, ((RecordId) this.id()).getId());
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
// Cache the properties
this.properties.put(key, value);
}
Aggregations