Search in sources :

Example 11 with PropertyColumn

use of org.umlg.sqlg.structure.topology.PropertyColumn in project sqlg by pietermartin.

the class SqlgVertex method load.

@Override
protected void load() {
    // new vertexes have no id, impossible to load, but then all its properties are already cached.
    if ((this.properties.isEmpty() && !this.sqlgGraph.tx().isInBatchMode()) || (this.properties.isEmpty() && this.sqlgGraph.getSqlDialect().supportsBatchMode() && this.sqlgGraph.tx().isInBatchMode() && !this.sqlgGraph.tx().getBatchManager().vertexIsCached(this))) {
        if (this.sqlgGraph.getSqlDialect().supportsBatchMode() && this.sqlgGraph.tx().isOpen() && 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") VertexLabel vertexLabel = this.sqlgGraph.getTopology().getSchema(this.schema).get().getVertexLabel(this.table).get();
        StringBuilder sql = new StringBuilder("SELECT\n\t");
        sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes("ID"));
        for (PropertyColumn propertyColumn : vertexLabel.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));
                }
            }
        }
        sql.append("\nFROM\n\t");
        sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(this.schema));
        sql.append(".");
        sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(VERTEX_PREFIX + this.table));
        sql.append("\nWHERE\n\t");
        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);
            } else {
                throw new IllegalStateException(String.format("Vertex with label %s and id %d does not exist.", new Object[] { this.schema + "." + this.table, this.recordId.getId() }));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel)

Example 12 with PropertyColumn

use of org.umlg.sqlg.structure.topology.PropertyColumn in project sqlg by pietermartin.

the class TestSchemaEagerCreation method testVertexEdgeHasSameName.

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
public void testVertexEdgeHasSameName() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name1", "halo1");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name2", "halo2");
    a1.addEdge("A", b2, "name3", "halo3");
    this.sqlgGraph.tx().commit();
    Map<String, PropertyColumn> properties = this.sqlgGraph.getTopology().getPublicSchema().getVertexLabel("A").get().getProperties();
    assertTrue(properties.containsKey("name1"));
    properties = this.sqlgGraph.getTopology().getPublicSchema().getVertexLabel("B").get().getProperties();
    assertTrue(properties.containsKey("name2"));
    properties = this.sqlgGraph.getTopology().getPublicSchema().getEdgeLabel("A").get().getProperties();
    assertTrue(properties.containsKey("name3"));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 13 with PropertyColumn

use of org.umlg.sqlg.structure.topology.PropertyColumn in project sqlg by pietermartin.

the class BaseSqlDialect method flushVertexCache.

@Override
public void flushVertexCache(SqlgGraph sqlgGraph, Map<SchemaTable, Pair<SortedSet<String>, Map<SqlgVertex, Map<String, Object>>>> vertexCache) {
    for (Map.Entry<SchemaTable, Pair<SortedSet<String>, Map<SqlgVertex, Map<String, Object>>>> entry : vertexCache.entrySet()) {
        SchemaTable schemaTable = entry.getKey();
        Pair<SortedSet<String>, Map<SqlgVertex, Map<String, Object>>> vertices = entry.getValue();
        SortedSet<String> columns = vertices.getLeft();
        Map<SqlgVertex, Map<String, Object>> rows = vertices.getRight();
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO ");
        if (!schemaTable.isTemporary() || sqlgGraph.getSqlDialect().needsTemporaryTableSchema()) {
            sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(schemaTable.getSchema()));
            sql.append(".");
        }
        if (!schemaTable.isTemporary() || !sqlgGraph.getSqlDialect().needsTemporaryTablePrefix()) {
            sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(VERTEX_PREFIX + schemaTable.getTable()));
        } else {
            sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(sqlgGraph.getSqlDialect().temporaryTablePrefix() + VERTEX_PREFIX + schemaTable.getTable()));
        }
        Map<String, PropertyColumn> propertyColumns = null;
        Map<String, PropertyType> properties = null;
        if (!schemaTable.isTemporary()) {
            propertyColumns = sqlgGraph.getTopology().getSchema(schemaTable.getSchema()).orElseThrow(() -> new IllegalStateException(String.format("Schema %s not found", schemaTable.getSchema()))).getVertexLabel(schemaTable.getTable()).orElseThrow(() -> new IllegalStateException(String.format("VertexLabel %s not found", schemaTable.getTable()))).getProperties();
        } else {
            properties = sqlgGraph.getTopology().getPublicSchema().getTemporaryTable(VERTEX_PREFIX + schemaTable.getTable());
        }
        if (!columns.isEmpty()) {
            Map<String, PropertyType> propertyTypeMap = new HashMap<>();
            for (String column : columns) {
                if (!schemaTable.isTemporary()) {
                    PropertyColumn propertyColumn = propertyColumns.get(column);
                    propertyTypeMap.put(column, propertyColumn.getPropertyType());
                } else {
                    propertyTypeMap.put(column, properties.get(column));
                }
            }
            sql.append(" (");
            int i = 1;
            // noinspection Duplicates
            for (String column : columns) {
                PropertyType propertyType = propertyTypeMap.get(column);
                String[] sqlDefinitions = sqlgGraph.getSqlDialect().propertyTypeToSqlDefinition(propertyType);
                int count = 1;
                for (@SuppressWarnings("unused") String sqlDefinition : sqlDefinitions) {
                    if (count > 1) {
                        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(column + propertyType.getPostFixes()[count - 2]));
                    } else {
                        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(column));
                    }
                    if (count++ < sqlDefinitions.length) {
                        sql.append(",");
                    }
                }
                if (i++ < columns.size()) {
                    sql.append(", ");
                }
            }
            sql.append(") VALUES ( ");
            i = 1;
            // noinspection Duplicates
            for (String column : columns) {
                PropertyType propertyType = propertyTypeMap.get(column);
                String[] sqlDefinitions = sqlgGraph.getSqlDialect().propertyTypeToSqlDefinition(propertyType);
                int count = 1;
                // noinspection Duplicates
                for (@SuppressWarnings("unused") String sqlDefinition : sqlDefinitions) {
                    if (count > 1) {
                        sql.append("?");
                    } else {
                        sql.append("?");
                    }
                    if (count++ < sqlDefinitions.length) {
                        sql.append(",");
                    }
                }
                if (i++ < columns.size()) {
                    sql.append(", ");
                }
            }
            sql.append(")");
        } else {
            sql.append(sqlgGraph.getSqlDialect().sqlInsertEmptyValues());
        }
        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(), Statement.RETURN_GENERATED_KEYS)) {
            List<SqlgVertex> sqlgVertices = new ArrayList<>();
            for (Map.Entry<SqlgVertex, Map<String, Object>> rowEntry : rows.entrySet()) {
                int i = 1;
                SqlgVertex sqlgVertex = rowEntry.getKey();
                sqlgVertices.add(sqlgVertex);
                if (!columns.isEmpty()) {
                    Map<String, Object> parameterValueMap = rowEntry.getValue();
                    List<Pair<PropertyType, Object>> typeAndValues = new ArrayList<>();
                    for (String column : columns) {
                        if (!schemaTable.isTemporary()) {
                            PropertyColumn propertyColumn = propertyColumns.get(column);
                            typeAndValues.add(Pair.of(propertyColumn.getPropertyType(), parameterValueMap.get(column)));
                        } else {
                            typeAndValues.add(Pair.of(properties.get(column), parameterValueMap.get(column)));
                        }
                    }
                    SqlgUtil.setKeyValuesAsParameterUsingPropertyColumn(sqlgGraph, true, i, preparedStatement, typeAndValues);
                }
                preparedStatement.addBatch();
            }
            preparedStatement.executeBatch();
            ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
            int i = 0;
            while (generatedKeys.next()) {
                sqlgVertices.get(i++).setInternalPrimaryKey(RecordId.from(schemaTable, generatedKeys.getLong(1)));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) Pair(org.apache.commons.lang3.tuple.Pair)

Example 14 with PropertyColumn

use of org.umlg.sqlg.structure.topology.PropertyColumn in project sqlg by pietermartin.

the class BaseSqlDialect method flushEdgePropertyCache.

@Override
public void flushEdgePropertyCache(SqlgGraph sqlgGraph, Map<SchemaTable, Pair<SortedSet<String>, Map<SqlgEdge, Map<String, Object>>>> edgePropertyCache) {
    for (Map.Entry<SchemaTable, Pair<SortedSet<String>, Map<SqlgEdge, Map<String, Object>>>> entry : edgePropertyCache.entrySet()) {
        SchemaTable schemaTable = entry.getKey();
        Pair<SortedSet<String>, Map<SqlgEdge, Map<String, Object>>> edges = entry.getValue();
        SortedSet<String> columns = edges.getLeft();
        Map<SqlgEdge, Map<String, Object>> rows = edges.getRight();
        StringBuilder sql = new StringBuilder();
        sql.append("UPDATE ");
        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(schemaTable.getSchema()));
        sql.append(".");
        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(EDGE_PREFIX + schemaTable.getTable()));
        sql.append(" SET ");
        Map<String, PropertyColumn> propertyColumns = sqlgGraph.getTopology().getSchema(schemaTable.getSchema()).orElseThrow(() -> new IllegalStateException(String.format("Schema %s not found", schemaTable.getSchema()))).getEdgeLabel(schemaTable.getTable()).orElseThrow(() -> new IllegalStateException(String.format("EdgeLabel %s not found", schemaTable.getTable()))).getProperties();
        if (!columns.isEmpty()) {
            Map<String, PropertyType> propertyTypeMap = new HashMap<>();
            for (String column : columns) {
                PropertyColumn propertyColumn = propertyColumns.get(column);
                propertyTypeMap.put(column, propertyColumn.getPropertyType());
            }
            sql.append(" ");
            int i = 1;
            // noinspection Duplicates
            for (String column : columns) {
                PropertyType propertyType = propertyTypeMap.get(column);
                String[] sqlDefinitions = sqlgGraph.getSqlDialect().propertyTypeToSqlDefinition(propertyType);
                int count = 1;
                for (@SuppressWarnings("unused") String sqlDefinition : sqlDefinitions) {
                    if (count > 1) {
                        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(column + propertyType.getPostFixes()[count - 2]));
                        sql.append(" = ?");
                    } else {
                        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(column));
                        sql.append(" = ?");
                    }
                    if (count++ < sqlDefinitions.length) {
                        sql.append(",");
                    }
                }
                if (i++ < columns.size()) {
                    sql.append(", ");
                }
            }
        }
        sql.append(" WHERE ");
        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(Topology.ID));
        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())) {
            for (Map.Entry<SqlgEdge, Map<String, Object>> rowEntry : rows.entrySet()) {
                int i = 1;
                SqlgEdge sqlgEdge = rowEntry.getKey();
                if (!columns.isEmpty()) {
                    Map<String, Object> parameterValueMap = rowEntry.getValue();
                    List<Pair<PropertyType, Object>> typeAndValues = new ArrayList<>();
                    for (String column : columns) {
                        PropertyColumn propertyColumn = propertyColumns.get(column);
                        Object value = parameterValueMap.get(column);
                        if (value == null) {
                            // if the value is not present update it to what is currently is.
                            if (sqlgEdge.property(column).isPresent()) {
                                value = sqlgEdge.value(column);
                            } else {
                                value = null;
                            }
                        }
                        typeAndValues.add(Pair.of(propertyColumn.getPropertyType(), value));
                    }
                    i = SqlgUtil.setKeyValuesAsParameterUsingPropertyColumn(sqlgGraph, true, i, preparedStatement, typeAndValues);
                    preparedStatement.setLong(i, ((RecordId) sqlgEdge.id()).getId());
                }
                preparedStatement.addBatch();
            }
            preparedStatement.executeBatch();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) Pair(org.apache.commons.lang3.tuple.Pair)

Example 15 with PropertyColumn

use of org.umlg.sqlg.structure.topology.PropertyColumn in project sqlg by pietermartin.

the class BaseSqlDialect method flushEdgeCache.

@Override
public void flushEdgeCache(SqlgGraph sqlgGraph, Map<MetaEdge, Pair<SortedSet<String>, Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>>>> edgeCache) {
    for (MetaEdge metaEdge : edgeCache.keySet()) {
        Pair<SortedSet<String>, Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>>> triples = edgeCache.get(metaEdge);
        Map<String, PropertyType> propertyTypeMap = sqlgGraph.getTopology().getTableFor(metaEdge.getSchemaTable().withPrefix(EDGE_PREFIX));
        SortedSet<String> columns = triples.getLeft();
        Map<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>> rows = triples.getRight();
        StringBuilder sql = new StringBuilder("INSERT INTO ");
        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(metaEdge.getSchemaTable().getSchema()));
        sql.append(".");
        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(EDGE_PREFIX + metaEdge.getSchemaTable().getTable()));
        sql.append(" (");
        Map<String, PropertyColumn> propertyColumns = sqlgGraph.getTopology().getSchema(metaEdge.getSchemaTable().getSchema()).orElseThrow(() -> new IllegalStateException(String.format("Schema %s not found", metaEdge.getSchemaTable().getSchema()))).getEdgeLabel(metaEdge.getSchemaTable().getTable()).orElseThrow(() -> new IllegalStateException(String.format("EdgeLabel %s not found", metaEdge.getSchemaTable().getTable()))).getProperties();
        int i = 1;
        for (String column : columns) {
            PropertyType propertyType = propertyTypeMap.get(column);
            String[] sqlDefinitions = sqlgGraph.getSqlDialect().propertyTypeToSqlDefinition(propertyType);
            int count = 1;
            for (@SuppressWarnings("unused") String sqlDefinition : sqlDefinitions) {
                if (count > 1) {
                    sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(column + propertyType.getPostFixes()[count - 2]));
                } else {
                    sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(column));
                }
                if (count++ < sqlDefinitions.length) {
                    sql.append(",");
                }
            }
            if (i++ < columns.size()) {
                sql.append(", ");
            }
        }
        if (!columns.isEmpty()) {
            sql.append(", ");
        }
        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(metaEdge.getOutLabel() + OUT_VERTEX_COLUMN_END));
        sql.append(", ");
        sql.append(sqlgGraph.getSqlDialect().maybeWrapInQoutes(metaEdge.getInLabel() + IN_VERTEX_COLUMN_END));
        sql.append(") VALUES (");
        i = 1;
        for (String column : columns) {
            PropertyType propertyType = propertyTypeMap.get(column);
            String[] sqlDefinitions = sqlgGraph.getSqlDialect().propertyTypeToSqlDefinition(propertyType);
            int count = 1;
            // noinspection Duplicates
            for (@SuppressWarnings("unused") String sqlDefinition : sqlDefinitions) {
                if (count > 1) {
                    sql.append("?");
                } else {
                    sql.append("?");
                }
                if (count++ < sqlDefinitions.length) {
                    sql.append(",");
                }
            }
            if (i++ < columns.size()) {
                sql.append(", ");
            }
        }
        if (!columns.isEmpty()) {
            sql.append(", ");
        }
        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(), Statement.RETURN_GENERATED_KEYS)) {
            List<SqlgEdge> sqlgEdges = new ArrayList<>();
            for (Map.Entry<SqlgEdge, Triple<SqlgVertex, SqlgVertex, Map<String, Object>>> rowEntry : rows.entrySet()) {
                i = 1;
                SqlgEdge sqlgEdge = rowEntry.getKey();
                sqlgEdges.add(sqlgEdge);
                Triple<SqlgVertex, SqlgVertex, Map<String, Object>> parameterValueMap = rowEntry.getValue();
                List<Pair<PropertyType, Object>> typeAndValues = new ArrayList<>();
                for (String column : columns) {
                    PropertyColumn propertyColumn = propertyColumns.get(column);
                    typeAndValues.add(Pair.of(propertyColumn.getPropertyType(), parameterValueMap.getRight().get(column)));
                }
                i = SqlgUtil.setKeyValuesAsParameterUsingPropertyColumn(sqlgGraph, true, i, preparedStatement, typeAndValues);
                preparedStatement.setLong(i++, ((RecordId) parameterValueMap.getLeft().id()).getId());
                preparedStatement.setLong(i, ((RecordId) parameterValueMap.getMiddle().id()).getId());
                preparedStatement.addBatch();
            }
            preparedStatement.executeBatch();
            ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
            i = 0;
            while (generatedKeys.next()) {
                sqlgEdges.get(i++).setInternalPrimaryKey(RecordId.from(metaEdge.getSchemaTable(), generatedKeys.getLong(1)));
            }
        // insertGlobalUniqueIndex(keyValueMap, propertyColumns);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) Pair(org.apache.commons.lang3.tuple.Pair) Triple(org.apache.commons.lang3.tuple.Triple)

Aggregations

PropertyColumn (org.umlg.sqlg.structure.topology.PropertyColumn)23 GlobalUniqueIndex (org.umlg.sqlg.structure.topology.GlobalUniqueIndex)10 VertexLabel (org.umlg.sqlg.structure.topology.VertexLabel)8 Pair (org.apache.commons.lang3.tuple.Pair)7 Test (org.junit.Test)7 BaseTest (org.umlg.sqlg.test.BaseTest)7 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)5 PropertyVetoException (java.beans.PropertyVetoException)4 IOException (java.io.IOException)4 Schema (org.umlg.sqlg.structure.topology.Schema)3 Triple (org.apache.commons.lang3.tuple.Triple)2 EdgeLabel (org.umlg.sqlg.structure.topology.EdgeLabel)2 SecureRandom (java.security.SecureRandom)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Edge (org.apache.tinkerpop.gremlin.structure.Edge)1 PropertyType (org.umlg.sqlg.structure.PropertyType)1