org.firebirdsql.jdbc.metadata.Clause Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jaybird Show documentation
Show all versions of jaybird Show documentation
JDBC Driver for the Firebird RDBMS
The newest version!
/*
* Firebird Open Source JDBC Driver
*
* Distributable under LGPL license.
* You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* LGPL License for more details.
*
* This file was created by members of the firebird development team.
* All individual contributions remain the Copyright (C) of those
* individuals. Contributors to this file are either listed here or
* can be obtained from a source control history command.
*
* All rights reserved.
*/
package org.firebirdsql.jdbc.metadata;
import org.firebirdsql.util.InternalApi;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import static java.util.stream.Collectors.joining;
/**
* Condition clause for constructing metadata query conditions.
*
* @author Mark Rotteveel
* @since 4.0
*/
@InternalApi
public final class Clause {
private final String condition;
private final String value;
/**
* Creates a metadata conditional clause.
*
* @param columnName
* Column name or expression resulting in a string value
* @param pattern
* Metadata pattern
*/
public Clause(String columnName, String pattern) {
this(columnName, MetadataPattern.compile(pattern));
}
private Clause(String columnName, MetadataPattern metadataPattern) {
condition = metadataPattern.renderCondition(columnName);
value = metadataPattern.getConditionValue();
}
/**
* Creates an equals clause.
*
* @param columnName
* Column name or expression resulting in a string value
* @param value
* value for equals condition
* @return clause for a SQL equals ({@code =}) condition
*/
static Clause equalsClause(String columnName, String value) {
return new Clause(columnName, MetadataPattern.equalsCondition(value));
}
/**
* Creates an {@code IS NULL} clause.
*
* @param columnName
* column name or expression resulting in a null value
* @return clause for SQL {@code IS NULL} condition
*/
static Clause isNullClause(String columnName) {
return new Clause(columnName, MetadataPattern.isNullCondition());
}
/**
* @return The condition for this clause suffixed with {@code " and "}, or empty string if the condition is empty
*/
public String getCondition() {
return getCondition(true);
}
/**
* @return The condition for this clause suffixed with {@code " and "} when {@code includeAnd} is {@code true}, or
* empty string if the condition is empty
*/
public String getCondition(boolean includeAnd) {
if (includeAnd) {
return getCondition("", " and ");
}
return getPlainCondition();
}
/**
* Condition rendered with prefix and suffix.
*
* @param prefix Prefix
* @param suffix Suffix
* @return The condition for this clause prefixed with {@code prefix} and suffixed with {@code suffix}, or empty
* string if the condition is empty
*/
public String getCondition(String prefix, String suffix) {
if (emptyCondition()) {
return "";
}
if (Objects.equals("", prefix) && Objects.equals("", suffix)) {
return condition;
}
return prefix + condition + suffix;
}
private String getPlainCondition() {
if (emptyCondition()) {
return "";
}
return condition;
}
public String getValue() {
return value;
}
public boolean hasCondition() {
return !emptyCondition();
}
private boolean emptyCondition() {
return condition.isEmpty();
}
private boolean hasValue() {
return value != null;
}
public static boolean anyCondition(Clause clause1, Clause clause2) {
return clause1.hasCondition() || clause2.hasCondition();
}
public static boolean anyCondition(Clause... clauses) {
for (Clause clause : clauses) {
if (clause.hasCondition()) {
return true;
}
}
return false;
}
public static boolean anyCondition(List clauses) {
for (Clause clause : clauses) {
if (clause.hasCondition()) {
return true;
}
}
return false;
}
private static boolean anyParameter(Clause clause1, Clause clause2) {
return clause1.hasCondition() && clause1.hasValue() || clause2.hasCondition() && clause2.hasValue();
}
public static List parameters(Clause clause1) {
if (clause1.hasCondition() && clause1.hasValue()) {
return Collections.singletonList(clause1.getValue());
}
return Collections.emptyList();
}
public static List parameters(Clause clause1, Clause clause2) {
if (!anyParameter(clause1, clause2)) {
return Collections.emptyList();
}
List list = new ArrayList<>(2);
if (clause1.hasCondition() && clause1.hasValue()) {
list.add(clause1.getValue());
}
if (clause2.hasCondition() && clause2.hasValue()) {
list.add(clause2.getValue());
}
return list;
}
public static List parameters(Clause... clauses) {
return parameters(Arrays.asList(clauses));
}
public static List parameters(List clauses) {
List list = new ArrayList<>(clauses.size());
for (Clause clause : clauses) {
if (clause.hasCondition() && clause.hasValue()) {
list.add(clause.getValue());
}
}
return list;
}
public static String conjunction(Clause... clauses) {
return conjunction(Arrays.asList(clauses));
}
public static String conjunction(List clauses) {
return clauses.stream()
.filter(Clause::hasCondition)
.map(clause -> clause.getCondition(false))
.collect(joining("\nand "));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy