All Downloads are FREE. Search and download functionalities are using the official Maven repository.

apoc.export.cypher.formatter.AbstractCypherFormatter Maven / Gradle / Ivy

There is a newer version: 5.25.1
Show newest version
/*
 * Copyright (c) "Neo4j"
 * Neo4j Sweden AB [http://neo4j.com]
 *
 * This file is part of Neo4j.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package apoc.export.cypher.formatter;

import static apoc.export.cypher.formatter.CypherFormatterUtils.Q_UNIQUE_ID_LABEL;
import static apoc.export.cypher.formatter.CypherFormatterUtils.Q_UNIQUE_ID_REL;
import static apoc.export.cypher.formatter.CypherFormatterUtils.UNIQUE_ID_PROP;
import static apoc.export.cypher.formatter.CypherFormatterUtils.isUniqueRelationship;
import static apoc.export.cypher.formatter.CypherFormatterUtils.simpleKeyValue;

import apoc.export.util.ExportConfig;
import apoc.export.util.ExportFormat;
import apoc.export.util.Reporter;
import apoc.util.Util;
import java.io.PrintWriter;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.commons.lang3.StringUtils;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.schema.ConstraintType;

/**
 * @author AgileLARUS
 *
 * @since 16-06-2017
 */
abstract class AbstractCypherFormatter implements CypherFormatter {

    private static final String STATEMENT_CONSTRAINTS = "CREATE CONSTRAINT %s%s FOR (node:%s) REQUIRE (%s) %s;";
    private static final String STATEMENT_CONSTRAINTS_REL =
            "CREATE CONSTRAINT %s%s FOR ()-[rel:%s]-() REQUIRE (%s) %s;";
    private static final String STATEMENT_DROP_CONSTRAINTS = "DROP CONSTRAINT %s;";

    private static final String STATEMENT_NODE_FULLTEXT_IDX = "CREATE FULLTEXT INDEX %s FOR (n:%s) ON EACH [%s];";
    private static final String STATEMENT_REL_FULLTEXT_IDX =
            "CREATE FULLTEXT INDEX %s FOR ()-[rel:%s]-() ON EACH [%s];";
    public static final String PROPERTY_QUOTING_FORMAT = "%s.`%s`";
    private static final String ID_REL_KEY = "id";

    @Override
    public String statementForCleanUpNodes(int batchSize) {
        return "MATCH (n:" + Q_UNIQUE_ID_LABEL + ") " + " WITH n LIMIT "
                + batchSize + " REMOVE n:"
                + Q_UNIQUE_ID_LABEL + " REMOVE n." + Util.quote(UNIQUE_ID_PROP) + ";";
    }

    @Override
    public String statementForCleanUpRelationships(int batchSize) {
        return "MATCH ()-[r]->() WHERE r." + Q_UNIQUE_ID_REL + " IS NOT NULL"
                + " WITH r LIMIT " + batchSize
                + " REMOVE r." + Q_UNIQUE_ID_REL + ";";
    }

    @Override
    public String statementForNodeIndex(
            String indexType, String label, Iterable keys, boolean ifNotExists, String idxName) {
        return String.format(
                "CREATE %s INDEX%s%s FOR (n:%s) ON (%s);",
                indexType, idxName, getIfNotExists(ifNotExists), Util.quote(label), getPropertiesQuoted(keys, "n."));
    }

    @Override
    public String statementForIndexRelationship(
            String indexType, String type, Iterable keys, boolean ifNotExists, String idxName) {
        return String.format(
                "CREATE %s INDEX%s%s FOR ()-[rel:%s]-() ON (%s);",
                indexType, idxName, getIfNotExists(ifNotExists), Util.quote(type), getPropertiesQuoted(keys, "rel."));
    }

    @Override
    public String statementForNodeFullTextIndex(String name, Iterable




© 2015 - 2024 Weber Informatics LLC | Privacy Policy