From b228df981de45e81c0fe2d2025dc8ebce382f327 Mon Sep 17 00:00:00 2001 From: lukaseder Date: Tue, 8 Sep 2015 20:25:59 +0200 Subject: [PATCH] [#4006] Add Field array(Field...) --- jOOQ/src/main/java/org/jooq/impl/Array.java | 99 +++++++++++++++++++++ jOOQ/src/main/java/org/jooq/impl/DSL.java | 84 +++++++++++++++++ jOOQ/src/main/java/org/jooq/impl/Utils.java | 10 +-- 3 files changed, 187 insertions(+), 6 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/impl/Array.java diff --git a/jOOQ/src/main/java/org/jooq/impl/Array.java b/jOOQ/src/main/java/org/jooq/impl/Array.java new file mode 100644 index 0000000000..05c9d60f75 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/Array.java @@ -0,0 +1,99 @@ +/** + * Copyright (c) 2009-2015, Data Geekery GmbH (http://www.datageekery.com) + * All rights reserved. + * + * 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.SQLDialect.POSTGRES; + +import java.util.Collection; + +import org.jooq.Context; +import org.jooq.DataType; +import org.jooq.Field; +import org.jooq.Record; + +/** + * @author Lukas Eder + */ +class Array extends AbstractField { + + /** + * Generated UID + */ + private static final long serialVersionUID = -6629785423729163857L; + + private final Fields fields; + + Array(Collection> fields) { + super("array", type(fields)); + + this.fields = new Fields(fields); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + private static DataType type(Collection> fields) { + if (fields == null || fields.isEmpty()) + return (DataType) SQLDataType.OTHER.getArrayDataType(); + else + return fields.iterator().next().getDataType().getArrayDataType(); + } + + @Override + public final void accept(Context ctx) { + switch (ctx.family()) { + case H2: + ctx.sql('(').visit(fields).sql(')'); + break; + + case HSQLDB: + case POSTGRES: + default: + ctx.keyword("array") + .sql('[') + .visit(fields) + .sql(']'); + + if (fields.fields.length == 0 && ctx.family() == POSTGRES) + ctx.sql("::").keyword("int[]"); + + break; + } + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 632bf4ba6f..ef8000fbcb 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -11272,6 +11272,90 @@ public class DSL { return new Function(Term.ARRAY_AGG, field.getDataType().getArrayDataType(), nullSafe(field)); } + /** + * Create an array literal. + *

+ * This translates to the following databases and syntaxes: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
SQLDialectJavaSQL
{@link SQLDialect#H2}array(1, 2)(1, 2)
{@link SQLDialect#HSQLDB}, {@link SQLDialect#POSTGRES}array(1, 2)array[1, 2]
+ */ + @Support({ H2, HSQLDB, POSTGRES }) + public static Field array(T... values) { + return array(Utils.fields(values)); + } + + /** + * Create an array literal. + *

+ * This translates to the following databases and syntaxes: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
SQLDialectJavaSQL
{@link SQLDialect#H2}array(1, 2)(1, 2)
{@link SQLDialect#HSQLDB}, {@link SQLDialect#POSTGRES}array(1, 2)array[1, 2]
+ */ + /* [java-8] */ + @SafeVarargs + /* [/java-8] */ + @Support({ H2, HSQLDB, POSTGRES }) + public static Field array(Field... fields) { + return array(Arrays.asList(fields)); + } + + /** + * Create an array literal. + *

+ * This translates to the following databases and syntaxes: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
SQLDialectJavaSQL
{@link SQLDialect#H2}array(1, 2)(1, 2)
{@link SQLDialect#HSQLDB}, {@link SQLDialect#POSTGRES}array(1, 2)array[1, 2]
+ */ + @Support({ H2, HSQLDB, POSTGRES }) + public static Field array(Collection> fields) { + return new Array(fields); + } + /** * Get the max value over a field: max(field). */ diff --git a/jOOQ/src/main/java/org/jooq/impl/Utils.java b/jOOQ/src/main/java/org/jooq/impl/Utils.java index 0b8203e468..29350a560f 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Utils.java +++ b/jOOQ/src/main/java/org/jooq/impl/Utils.java @@ -866,14 +866,12 @@ final class Utils { * @return The argument objects themselves, if they are {@link Field}s, or a bind * values created from the argument objects. */ - static final List> fields(Object[] values) { - List> result = new ArrayList>(); + static final List> fields(T[] values) { + List> result = new ArrayList>(); - if (values != null) { - for (Object value : values) { + if (values != null) + for (T value : values) result.add(field(value)); - } - } return result; }