Search in sources :

Example 1 with SchemaTableTree

use of org.umlg.sqlg.sql.parse.SchemaTableTree in project sqlg by pietermartin.

the class SqlgSqlExecutor method executeDropQuery.

public static void executeDropQuery(SqlgGraph sqlgGraph, SchemaTableTree rootSchemaTableTree, LinkedList<SchemaTableTree> distinctQueryStack) {
    List<Triple<DROP_QUERY, String, SchemaTable>> sqls = rootSchemaTableTree.constructDropSql(distinctQueryStack);
    for (Triple<DROP_QUERY, String, SchemaTable> sqlPair : sqls) {
        DROP_QUERY dropQuery = sqlPair.getLeft();
        String sql = sqlPair.getMiddle();
        SchemaTable deletedSchemaTable = sqlPair.getRight();
        switch(dropQuery) {
            case ALTER:
                executeDropQuery(sqlgGraph, sql, new LinkedList<>(), deletedSchemaTable);
                break;
            case EDGE:
                LinkedList<SchemaTableTree> tmp = new LinkedList<>(distinctQueryStack);
                tmp.removeLast();
                executeDropQuery(sqlgGraph, sql, tmp, deletedSchemaTable);
                break;
            case NORMAL:
                executeDropQuery(sqlgGraph, sql, distinctQueryStack, deletedSchemaTable);
                break;
            case TRUNCATE:
                executeDropQuery(sqlgGraph, sql, new LinkedList<>(), deletedSchemaTable);
                break;
            default:
                throw new IllegalStateException("Unknown DROP_QUERY " + dropQuery.toString());
        }
    }
}
Also used : Triple(org.apache.commons.lang3.tuple.Triple) SchemaTableTree(org.umlg.sqlg.sql.parse.SchemaTableTree) SchemaTable(org.umlg.sqlg.structure.SchemaTable) LinkedList(java.util.LinkedList)

Example 2 with SchemaTableTree

use of org.umlg.sqlg.sql.parse.SchemaTableTree in project sqlg by pietermartin.

the class SqlgVertexStep method parseForStrategy.

private SchemaTableTree parseForStrategy(SchemaTable schemaTable) {
    this.isForMultipleQueries = false;
    Preconditions.checkState(this.replacedSteps.size() > 1, "There must be at least one replacedStep");
    Preconditions.checkState(this.replacedSteps.get(1).isVertexStep() || this.replacedSteps.get(1).isEdgeVertexStep() || this.replacedSteps.get(1).isEdgeOtherVertexStep(), "The first step must a VertexStep, EdgeVertexStep, EdgeOtherVertexStep or GraphStep, found " + this.replacedSteps.get(1).getStep().getClass().toString());
    SchemaTableTree rootSchemaTableTree = null;
    try {
        rootSchemaTableTree = this.sqlgGraph.getGremlinParser().parse(schemaTable, this.replacedSteps);
        // Regular
        List<LinkedList<SchemaTableTree>> distinctQueries = rootSchemaTableTree.constructDistinctQueries();
        // Optional
        List<Pair<LinkedList<SchemaTableTree>, Set<SchemaTableTree>>> leftJoinResult = new ArrayList<>();
        SchemaTableTree.constructDistinctOptionalQueries(rootSchemaTableTree, leftJoinResult);
        // Emit
        List<LinkedList<SchemaTableTree>> leftJoinResultEmit = new ArrayList<>();
        SchemaTableTree.constructDistinctEmitBeforeQueries(rootSchemaTableTree, leftJoinResultEmit);
        this.isForMultipleQueries = (distinctQueries.size() + leftJoinResult.size() + leftJoinResultEmit.size()) > 1;
        return rootSchemaTableTree;
    } finally {
        if (rootSchemaTableTree != null) {
            rootSchemaTableTree.resetColumnAliasMaps();
        }
    }
}
Also used : SchemaTableTree(org.umlg.sqlg.sql.parse.SchemaTableTree) Pair(org.apache.commons.lang3.tuple.Pair)

Example 3 with SchemaTableTree

use of org.umlg.sqlg.sql.parse.SchemaTableTree in project sqlg by pietermartin.

the class SqlgGraphStep method parseForStrategy.

public Set<SchemaTableTree> parseForStrategy() {
    this.isForMultipleQueries = false;
    Preconditions.checkState(this.replacedSteps.size() > 0, "There must be at least one replacedStep");
    Preconditions.checkState(this.replacedSteps.get(0).isGraphStep(), "The first step must a SqlgGraphStep");
    Set<SchemaTableTree> rootSchemaTableTrees = this.sqlgGraph.getGremlinParser().parse(this.replacedStepTree);
    if (rootSchemaTableTrees.size() > 1) {
        this.isForMultipleQueries = true;
        for (SchemaTableTree rootSchemaTableTree : rootSchemaTableTrees) {
            rootSchemaTableTree.resetColumnAliasMaps();
        }
    } else {
        for (SchemaTableTree rootSchemaTableTree : rootSchemaTableTrees) {
            try {
                // TODO this really sucks, constructsql should not query, but alas it does for P.within and temp table jol
                if (this.sqlgGraph.tx().isOpen() && this.sqlgGraph.getSqlDialect().supportsBatchMode() && this.sqlgGraph.tx().getBatchManager().isStreaming()) {
                    throw new IllegalStateException("streaming is in progress, first flush or commit before querying.");
                }
                // Regular
                List<LinkedList<SchemaTableTree>> distinctQueries = rootSchemaTableTree.constructDistinctQueries();
                this.isForMultipleQueries = distinctQueries.size() > 1;
                if (this.isForMultipleQueries) {
                    break;
                }
            } finally {
                rootSchemaTableTree.resetColumnAliasMaps();
            }
        }
    }
    return rootSchemaTableTrees;
}
Also used : SchemaTableTree(org.umlg.sqlg.sql.parse.SchemaTableTree)

Example 4 with SchemaTableTree

use of org.umlg.sqlg.sql.parse.SchemaTableTree in project sqlg by pietermartin.

the class SqlgUtil method loadLabeledElements.

/**
 * Loads all labeled or emitted elements.
 *
 * @param sqlgGraph
 * @param resultSet
 * @param subQueryStack
 * @param lastQueryStack
 * @param lastElementIdCountMap
 * @return
 * @throws SQLException
 */
private static <E extends SqlgElement> List<Emit<E>> loadLabeledElements(SqlgGraph sqlgGraph, final ResultSet resultSet, LinkedList<SchemaTableTree> subQueryStack, boolean lastQueryStack, Map<String, Integer> lastElementIdCountMap, boolean forParent) throws SQLException {
    List<Emit<E>> result = new ArrayList<>();
    int count = 1;
    for (SchemaTableTree schemaTableTree : subQueryStack) {
        if (!schemaTableTree.getLabels().isEmpty()) {
            String idProperty = schemaTableTree.labeledAliasId();
            Integer columnCount = lastElementIdCountMap.get(idProperty);
            Long id = resultSet.getLong(columnCount);
            if (!resultSet.wasNull()) {
                E sqlgElement;
                if (schemaTableTree.getSchemaTable().isVertexTable()) {
                    String rawLabel = schemaTableTree.getSchemaTable().getTable().substring(VERTEX_PREFIX.length());
                    sqlgElement = (E) SqlgVertex.of(sqlgGraph, id, schemaTableTree.getSchemaTable().getSchema(), rawLabel);
                } else {
                    String rawLabel = schemaTableTree.getSchemaTable().getTable().substring(EDGE_PREFIX.length());
                    sqlgElement = (E) new SqlgEdge(sqlgGraph, id, schemaTableTree.getSchemaTable().getSchema(), rawLabel);
                }
                schemaTableTree.loadProperty(resultSet, sqlgElement);
                // Elements that come before the last element in the path must not get the labels.
                if (schemaTableTree.isEmit() && !lastQueryStack) {
                    if (forParent) {
                        result.add(new Emit<>(resultSet.getLong(1), sqlgElement, Collections.emptySet(), schemaTableTree.getStepDepth(), schemaTableTree.getSqlgComparatorHolder()));
                    } else {
                        result.add(new Emit<>(sqlgElement, Collections.emptySet(), schemaTableTree.getStepDepth(), schemaTableTree.getSqlgComparatorHolder()));
                    }
                } else if (schemaTableTree.isEmit() && lastQueryStack && (count != subQueryStack.size())) {
                    if (forParent) {
                        result.add(new Emit<>(resultSet.getLong(1), sqlgElement, Collections.emptySet(), schemaTableTree.getStepDepth(), schemaTableTree.getSqlgComparatorHolder()));
                    } else {
                        result.add(new Emit<>(sqlgElement, Collections.emptySet(), schemaTableTree.getStepDepth(), schemaTableTree.getSqlgComparatorHolder()));
                    }
                } else {
                    if (forParent) {
                        result.add(new Emit<>(resultSet.getLong(1), sqlgElement, schemaTableTree.getRealLabels(), schemaTableTree.getStepDepth(), schemaTableTree.getSqlgComparatorHolder()));
                    } else {
                        result.add(new Emit<>(sqlgElement, schemaTableTree.getRealLabels(), schemaTableTree.getStepDepth(), schemaTableTree.getSqlgComparatorHolder()));
                    }
                }
            }
        }
        count++;
    }
    return result;
}
Also used : Emit(org.umlg.sqlg.strategy.Emit) SchemaTableTree(org.umlg.sqlg.sql.parse.SchemaTableTree)

Example 5 with SchemaTableTree

use of org.umlg.sqlg.sql.parse.SchemaTableTree in project sqlg by pietermartin.

the class MSSqlServerDialect method drop.

@Override
public List<Triple<SqlgSqlExecutor.DROP_QUERY, String, SchemaTable>> drop(SqlgGraph sqlgGraph, String leafElementsToDelete, Optional<String> edgesToDelete, LinkedList<SchemaTableTree> distinctQueryStack) {
    List<Triple<SqlgSqlExecutor.DROP_QUERY, String, SchemaTable>> sqls = new ArrayList<>();
    SchemaTableTree last = distinctQueryStack.getLast();
    SchemaTableTree lastEdge = null;
    // if the leaf elements are vertices then we need to delete its in and out edges.
    boolean isVertex = last.getSchemaTable().isVertexTable();
    VertexLabel lastVertexLabel = null;
    if (isVertex) {
        Optional<Schema> schemaOptional = sqlgGraph.getTopology().getSchema(last.getSchemaTable().getSchema());
        Preconditions.checkState(schemaOptional.isPresent(), "BUG: %s not found in the topology.", last.getSchemaTable().getSchema());
        Schema schema = schemaOptional.get();
        Optional<VertexLabel> vertexLabelOptional = schema.getVertexLabel(last.getSchemaTable().withOutPrefix().getTable());
        Preconditions.checkState(vertexLabelOptional.isPresent(), "BUG: %s not found in the topology.", last.getSchemaTable().withOutPrefix().getTable());
        lastVertexLabel = vertexLabelOptional.get();
    }
    boolean queryTraversesEdge = isVertex && (distinctQueryStack.size() > 1);
    EdgeLabel lastEdgeLabel = null;
    if (queryTraversesEdge) {
        lastEdge = distinctQueryStack.get(distinctQueryStack.size() - 2);
        Optional<Schema> edgeSchema = sqlgGraph.getTopology().getSchema(lastEdge.getSchemaTable().getSchema());
        Preconditions.checkState(edgeSchema.isPresent(), "BUG: %s not found in the topology.", lastEdge.getSchemaTable().getSchema());
        Optional<EdgeLabel> edgeLabelOptional = edgeSchema.get().getEdgeLabel(lastEdge.getSchemaTable().withOutPrefix().getTable());
        Preconditions.checkState(edgeLabelOptional.isPresent(), "BUG: %s not found in the topology.", lastEdge.getSchemaTable().getTable());
        lastEdgeLabel = edgeLabelOptional.get();
    }
    if (isVertex) {
        // First delete all edges except for this edge traversed to get to the vertices.
        StringBuilder sb;
        for (Map.Entry<String, EdgeLabel> edgeLabelEntry : lastVertexLabel.getOutEdgeLabels().entrySet()) {
            EdgeLabel edgeLabel = edgeLabelEntry.getValue();
            if (lastEdgeLabel == null || !edgeLabel.equals(lastEdgeLabel)) {
                // Delete
                sb = new StringBuilder();
                sb.append("DELETE FROM ");
                sb.append(maybeWrapInQoutes(edgeLabel.getSchema().getName()));
                sb.append(".");
                sb.append(maybeWrapInQoutes(Topology.EDGE_PREFIX + edgeLabel.getName()));
                sb.append("\nWHERE ");
                sb.append(maybeWrapInQoutes(lastVertexLabel.getSchema().getName() + "." + lastVertexLabel.getName() + Topology.OUT_VERTEX_COLUMN_END));
                sb.append(" IN(");
                sb.append(leafElementsToDelete);
                sb.append(")");
                sqls.add(Triple.of(SqlgSqlExecutor.DROP_QUERY.NORMAL, sb.toString(), SchemaTable.of(edgeLabel.getSchema().getName(), Topology.EDGE_PREFIX + edgeLabel.getName())));
            }
        }
        for (Map.Entry<String, EdgeLabel> edgeLabelEntry : lastVertexLabel.getInEdgeLabels().entrySet()) {
            EdgeLabel edgeLabel = edgeLabelEntry.getValue();
            if (lastEdgeLabel == null || !edgeLabel.equals(lastEdgeLabel)) {
                // Delete
                sb = new StringBuilder();
                sb.append("DELETE FROM ");
                sb.append(maybeWrapInQoutes(edgeLabel.getSchema().getName()));
                sb.append(".");
                sb.append(maybeWrapInQoutes(Topology.EDGE_PREFIX + edgeLabel.getName()));
                sb.append("\nWHERE ");
                sb.append(maybeWrapInQoutes(lastVertexLabel.getSchema().getName() + "." + lastVertexLabel.getName() + Topology.IN_VERTEX_COLUMN_END));
                sb.append(" IN\n\t(");
                sb.append(leafElementsToDelete);
                sb.append(")");
                sqls.add(Triple.of(SqlgSqlExecutor.DROP_QUERY.NORMAL, sb.toString(), SchemaTable.of(edgeLabel.getSchema().getName(), Topology.EDGE_PREFIX + edgeLabel.getName())));
            }
        }
    }
    // Need to defer foreign key constraint checks.
    if (queryTraversesEdge) {
        String edgeTableName = (maybeWrapInQoutes(lastEdge.getSchemaTable().getSchema())) + "." + maybeWrapInQoutes(lastEdge.getSchemaTable().getTable());
        sqls.add(Triple.of(SqlgSqlExecutor.DROP_QUERY.ALTER, this.sqlToTurnOffReferentialConstraintCheck(edgeTableName), lastEdge.getSchemaTable()));
    }
    // Delete the leaf vertices, if there are foreign keys then its been deferred.
    StringBuilder sb = new StringBuilder();
    sb.append("DELETE FROM ");
    sb.append(maybeWrapInQoutes(last.getSchemaTable().getSchema()));
    sb.append(".");
    sb.append(maybeWrapInQoutes(last.getSchemaTable().getTable()));
    sb.append("\nWHERE \"ID\" IN (");
    sb.append(leafElementsToDelete);
    sb.append(")");
    sqls.add(Triple.of(SqlgSqlExecutor.DROP_QUERY.NORMAL, sb.toString(), last.getSchemaTable()));
    if (queryTraversesEdge) {
        sb = new StringBuilder();
        sb.append("DELETE FROM ");
        sb.append(maybeWrapInQoutes(lastEdge.getSchemaTable().getSchema()));
        sb.append(".");
        sb.append(maybeWrapInQoutes(lastEdge.getSchemaTable().getTable()));
        sb.append("\nWHERE \"ID\" IN (\n\t");
        sb.append(edgesToDelete.get());
        sb.append(")");
        sqls.add(Triple.of(SqlgSqlExecutor.DROP_QUERY.EDGE, sb.toString(), lastEdge.getSchemaTable()));
    }
    // Enable the foreign key constraint
    if (queryTraversesEdge) {
        String edgeTableName = (maybeWrapInQoutes(lastEdge.getSchemaTable().getSchema())) + "." + maybeWrapInQoutes(lastEdge.getSchemaTable().getTable());
        sqls.add(Triple.of(SqlgSqlExecutor.DROP_QUERY.ALTER, this.sqlToTurnOnReferentialConstraintCheck(edgeTableName), null));
    }
    return sqls;
}
Also used : Triple(org.apache.commons.lang3.tuple.Triple) SqlgSqlExecutor(org.umlg.sqlg.strategy.SqlgSqlExecutor) SchemaTableTree(org.umlg.sqlg.sql.parse.SchemaTableTree)

Aggregations

SchemaTableTree (org.umlg.sqlg.sql.parse.SchemaTableTree)11 Triple (org.apache.commons.lang3.tuple.Triple)4 SqlgSqlExecutor (org.umlg.sqlg.strategy.SqlgSqlExecutor)3 Emit (org.umlg.sqlg.strategy.Emit)2 LinkedList (java.util.LinkedList)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1 Pair (org.apache.commons.lang3.tuple.Pair)1 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)1 AndOrHasContainer (org.umlg.sqlg.sql.parse.AndOrHasContainer)1 WhereClause (org.umlg.sqlg.sql.parse.WhereClause)1 SchemaTable (org.umlg.sqlg.structure.SchemaTable)1 EdgeLabel (org.umlg.sqlg.structure.topology.EdgeLabel)1 Schema (org.umlg.sqlg.structure.topology.Schema)1 VertexLabel (org.umlg.sqlg.structure.topology.VertexLabel)1