From 20d1e08133dba08cb885558968d6c65fdc616260 Mon Sep 17 00:00:00 2001 From: lukaseder Date: Tue, 30 Jan 2018 12:31:00 +0100 Subject: [PATCH] [#7116] Add Field.collate(Collation) and collate(String) to support COLLATE clauses --- jOOQ/src/main/java/org/jooq/Field.java | 23 ++++++ .../java/org/jooq/impl/AbstractField.java | 16 ++++ .../java/org/jooq/impl/CollatedField.java | 79 +++++++++++++++++++ .../src/main/java/org/jooq/impl/Keywords.java | 2 + 4 files changed, 120 insertions(+) create mode 100644 jOOQ/src/main/java/org/jooq/impl/CollatedField.java diff --git a/jOOQ/src/main/java/org/jooq/Field.java b/jOOQ/src/main/java/org/jooq/Field.java index 3362dc7333..b78151826d 100644 --- a/jOOQ/src/main/java/org/jooq/Field.java +++ b/jOOQ/src/main/java/org/jooq/Field.java @@ -108,6 +108,7 @@ public interface Field extends SelectField, GroupField, OrderField, Fie * expressions return the empty string "" here, never * null. */ + @Override String getComment(); /** @@ -3324,6 +3325,28 @@ public interface Field extends SelectField, GroupField, OrderField, Fie @Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES }) Field ascii(); + /** + * Apply a collation operator to this column expression. + * + * @see DSL#collation(String) + */ + @Support({ HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + Field collate(String collation); + + /** + * Apply a collation operator to this column expression. + * + * @see DSL#collation(Name) + */ + @Support({ HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + Field collate(Name collation); + + /** + * Apply a collation operator to this column expression. + */ + @Support({ HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) + Field 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 diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractField.java b/jOOQ/src/main/java/org/jooq/impl/AbstractField.java index b44e132e20..f9d1da8cde 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractField.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractField.java @@ -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 extends AbstractNamed implements Field { return DSL.ascii(varchar()); } + @Override + public final Field collate(String collation) { + return collate(DSL.collation(collation)); + } + + @Override + public final Field collate(Name collation) { + return collate(DSL.collation(collation)); + } + + @Override + public final Field collate(Collation collation) { + return new CollatedField(this, collation); + } + @Override @Deprecated public final Field concat(Field... fields) { diff --git a/jOOQ/src/main/java/org/jooq/impl/CollatedField.java b/jOOQ/src/main/java/org/jooq/impl/CollatedField.java new file mode 100644 index 0000000000..253393c681 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/CollatedField.java @@ -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 { + + 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 binding(Field field) { + return field.getType() == String.class ? (Binding) field.getBinding() : SQLDataType.VARCHAR.getBinding(); + } + + @SuppressWarnings("unchecked") + private static final DataType type(Field field) { + return field.getType() == String.class ? (DataType) field.getDataType() : SQLDataType.VARCHAR; + } + + @Override + public final void accept(Context ctx) { + ctx.sql("((").visit(field).sql(") ").visit(K_COLLATE).sql(' ').visit(collation).sql(')'); + + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/Keywords.java b/jOOQ/src/main/java/org/jooq/impl/Keywords.java index 4b0dad041c..87d80db906 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Keywords.java +++ b/jOOQ/src/main/java/org/jooq/impl/Keywords.java @@ -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");