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

ac.simons.neo4j.migrations.core.QueryPrecondition Maven / Gradle / Ivy

There is a newer version: 2.15.1
Show newest version
/*
 * Copyright 2020-2022 the original author or authors.
 *
 * 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
 *
 *      https://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 ac.simons.neo4j.migrations.core;

import java.util.Locale;
import java.util.Optional;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.neo4j.driver.Session;

/**
 * @author Gerrit Meier
 * @author Michael J. Simons
 * @since 1.5.0
 */
final class QueryPrecondition extends AbstractPrecondition implements Precondition {

	private static final Pattern CONDITION_PATTERN = Pattern.compile(
		"(?i)^.*?(? in (target|schema))? (?.++)?$");

	/**
	 * Enum to specify in which database the query should be executed
	 */
	enum Database {

		/**
		 * Inside the database being migrated.
		 */
		TARGET,
		/**
		 * Inside the schema database.
		 */
		SCHEMA,
	}

	/**
	 * Checks if the {@code hint} is matched by the {@link #CONDITION_PATTERN} and if so, tries to build a factory  for
	 * a corresponding precondition.
	 *
	 * @param hint The complete hint
	 * @return A factory for a precondition or an empty optional if this factory doesn't match the hint
	 */
	static Optional> tryToParse(String hint) {

		Matcher matcher = CONDITION_PATTERN.matcher(hint);
		if (!matcher.matches()) {
			return Optional.empty();
		}
		String query = matcher.group("query");
		if (query == null || query.trim().length() == 0) {
			throw new IllegalArgumentException(
					String.format(
							"Wrong Cypher precondition %s. Usage: ` [in ] q' `.",
							Precondition.formattedHint(hint)));
		}
		Database database;
		if (matcher.group("database") != null) {
			database = Database.valueOf(matcher.group(2).toUpperCase(Locale.ROOT));
		} else {
			database = Database.TARGET;
		}
		return Optional.of(type -> new QueryPrecondition(type, query.trim(), database));
	}

	private final Database database;

	private final String query;

	private QueryPrecondition(Type type, String query, Database database) {
		super(type);
		this.query = query;
		this.database = database;
	}

	@Override
	public boolean isMet(MigrationContext migrationContext) {
		try (Session session = database == Database.SCHEMA ? migrationContext.getSchemaSession() :
			migrationContext.getSession()) {
			return session.readTransaction(tx -> tx.run(query).single().get(0).asBoolean());
		}
	}

	String getQuery() {
		return query;
	}

	Database getDatabase() {
		return database;
	}

	@Override
	public String toString() {
		return String.format("// %s in %s q'%s", getType().keyword(), getDatabase(), query);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy