[#7116] Add Field.collate(Collation) and collate(String) to support COLLATE clauses

This commit is contained in:
lukaseder 2018-01-30 12:31:00 +01:00
parent 6ae89a844f
commit 20d1e08133
4 changed files with 120 additions and 0 deletions

View File

@ -108,6 +108,7 @@ public interface Field<T> extends SelectField<T>, GroupField, OrderField<T>, Fie
* expressions return the empty string <code>""</code> here, never
* <code>null</code>.
*/
@Override
String getComment();
/**
@ -3324,6 +3325,28 @@ public interface Field<T> extends SelectField<T>, GroupField, OrderField<T>, Fie
@Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
Field<Integer> ascii();
/**
* Apply a collation operator to this column expression.
*
* @see DSL#collation(String)
*/
@Support({ HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
Field<String> collate(String collation);
/**
* Apply a collation operator to this column expression.
*
* @see DSL#collation(Name)
*/
@Support({ HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
Field<String> collate(Name collation);
/**
* Apply a collation operator to this column expression.
*/
@Support({ HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
Field<String> collate(Collation collation);
/**
* This method is part of the pre-2.0 API. This API is maintained for
* backwards-compatibility. It may be removed in the future. Consider using

View File

@ -80,6 +80,7 @@ import org.jooq.Binding;
import org.jooq.CaseValueStep;
import org.jooq.CaseWhenStep;
import org.jooq.Clause;
import org.jooq.Collation;
import org.jooq.Comment;
import org.jooq.Comparator;
import org.jooq.Condition;
@ -1901,6 +1902,21 @@ abstract class AbstractField<T> extends AbstractNamed implements Field<T> {
return DSL.ascii(varchar());
}
@Override
public final Field<String> collate(String collation) {
return collate(DSL.collation(collation));
}
@Override
public final Field<String> collate(Name collation) {
return collate(DSL.collation(collation));
}
@Override
public final Field<String> collate(Collation collation) {
return new CollatedField(this, collation);
}
@Override
@Deprecated
public final Field<String> concat(Field<?>... fields) {

View File

@ -0,0 +1,79 @@
/*
* 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.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import static org.jooq.impl.Keywords.K_COLLATE;
import org.jooq.Binding;
import org.jooq.Collation;
import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
/**
* @author Lukas Eder
*/
final class CollatedField extends AbstractField<String> {
private static final long serialVersionUID = -3996395622492844215L;
private final Field<?> field;
private final Collation collation;
CollatedField(Field<?> field, Collation collation) {
super(field.getQualifiedName(), type(field), DSL.comment(field.getComment()), binding(field));
this.field = field;
this.collation = collation;
}
@SuppressWarnings("unchecked")
private static final Binding<?, String> binding(Field<?> field) {
return field.getType() == String.class ? (Binding<?, String>) field.getBinding() : SQLDataType.VARCHAR.getBinding();
}
@SuppressWarnings("unchecked")
private static final DataType<String> type(Field<?> field) {
return field.getType() == String.class ? (DataType<String>) field.getDataType() : SQLDataType.VARCHAR;
}
@Override
public final void accept(Context<?> ctx) {
ctx.sql("((").visit(field).sql(") ").visit(K_COLLATE).sql(' ').visit(collation).sql(')');
}
}

View File

@ -77,6 +77,8 @@ final class Keywords {
static final Keyword K_CHANGE_COLUMN = keyword("change column");
static final Keyword K_CHECK = keyword("check");
static final Keyword K_COALESCE = keyword("coalesce");
static final Keyword K_COLLATE = keyword("collate");
static final Keyword K_COLLATION = keyword("collation");
static final Keyword K_COLUMN = keyword("column");
static final Keyword K_COLUMNS = keyword("columns");
static final Keyword K_COMMENT = keyword("comment");