From 4b14717dde753ce2a43483aef2a35a04a73aa44a Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Fri, 24 Apr 2020 23:28:29 +0200 Subject: [PATCH] [jOOQ/jOOQ#8495] Avoid unnecessary array copying --- .../java/org/jooq/impl/AbstractField.java | 33 ++- .../java/org/jooq/impl/AbstractRecord.java | 5 + jOOQ/src/main/java/org/jooq/impl/Alias.java | 4 +- .../main/java/org/jooq/impl/BatchSingle.java | 8 +- jOOQ/src/main/java/org/jooq/impl/DSL.java | 35 ++- .../java/org/jooq/impl/DefaultDSLContext.java | 22 +- .../main/java/org/jooq/impl/InCondition.java | 43 ++- .../java/org/jooq/impl/InsertQueryImpl.java | 5 +- .../main/java/org/jooq/impl/RecordImplN.java | 7 +- .../java/org/jooq/impl/SelectQueryImpl.java | 18 +- jOOQ/src/main/java/org/jooq/impl/Tools.java | 262 +++--------------- .../java/org/jooq/impl/UnqualifiedName.java | 2 +- 12 files changed, 138 insertions(+), 306 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractField.java b/jOOQ/src/main/java/org/jooq/impl/AbstractField.java index a2883c0080..e0005bc376 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractField.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractField.java @@ -56,6 +56,7 @@ import static org.jooq.Comparator.NOT_SIMILAR_TO; import static org.jooq.Comparator.SIMILAR_TO; import static org.jooq.impl.DSL.inline; import static org.jooq.impl.DSL.nullSafe; +import static org.jooq.impl.DSL.nullSafeList; import static org.jooq.impl.DSL.val; import static org.jooq.impl.ExpressionOperator.ADD; import static org.jooq.impl.ExpressionOperator.DIVIDE; @@ -64,8 +65,6 @@ import static org.jooq.impl.ExpressionOperator.SUBTRACT; import static org.jooq.impl.Tools.EMPTY_FIELD; import static org.jooq.impl.Tools.EMPTY_STRING; import static org.jooq.impl.Tools.castIfNeeded; -import static org.jooq.tools.Convert.FALSE_VALUES; -import static org.jooq.tools.Convert.TRUE_VALUES; import java.math.BigDecimal; import java.sql.Timestamp; @@ -73,6 +72,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.function.Function; @@ -102,6 +102,7 @@ import org.jooq.SortField; import org.jooq.SortOrder; import org.jooq.WindowIgnoreNullsStep; import org.jooq.WindowPartitionByStep; +import org.jooq.tools.Convert; /** * @author Lukas Eder @@ -111,8 +112,10 @@ abstract class AbstractField extends AbstractTypedNamed implements Field> TRUE_VALUES = Tools.inline(Convert.TRUE_VALUES.toArray(EMPTY_STRING)); + private static final List> FALSE_VALUES = Tools.inline(Convert.FALSE_VALUES.toArray(EMPTY_STRING)); AbstractField(Name name, DataType type) { this(name, type, null); @@ -683,7 +686,7 @@ abstract class AbstractField extends AbstractTypedNamed implements Field type = getType(); if (type == String.class) - return ((Field) this).in(Tools.inline(TRUE_VALUES.toArray(EMPTY_STRING))); + return ((Field) this).in(TRUE_VALUES); else if (Number.class.isAssignableFrom(type)) return ((Field) this).equal(inline((Number) getDataType().convert(1))); else if (Boolean.class.isAssignableFrom(type)) @@ -698,13 +701,13 @@ abstract class AbstractField extends AbstractTypedNamed implements Field type = getType(); if (type == String.class) - return ((Field) this).in(Tools.inline(FALSE_VALUES.toArray(EMPTY_STRING))); + return ((Field) this).in(FALSE_VALUES); else if (Number.class.isAssignableFrom(type)) return ((Field) this).equal(inline((Number) getDataType().convert(0))); else if (Boolean.class.isAssignableFrom(type)) return ((Field) this).equal(inline(false, (DataType) getDataType())); else - return castIfNeeded(this, String.class).in(Tools.inline(FALSE_VALUES.toArray(EMPTY_STRING))); + return castIfNeeded(this, String.class).in(FALSE_VALUES); } @Override @@ -963,12 +966,12 @@ abstract class AbstractField extends AbstractTypedNamed implements Field) values[0]); - return in(Tools.fields(values, this).toArray(EMPTY_FIELD)); + return new InCondition<>(this, Tools.fields(values, this), IN); } @Override public final Condition in(Field... values) { - return new InCondition<>(this, nullSafe(values), IN); + return new InCondition<>(this, nullSafeList(values), IN); } @Override @@ -1004,12 +1007,12 @@ abstract class AbstractField extends AbstractTypedNamed implements Field) values[0]); - return notIn(Tools.fields(values, this).toArray(EMPTY_FIELD)); + return new InCondition<>(this, Tools.fields(values, this), NOT_IN); } @Override public final Condition notIn(Field... values) { - return new InCondition<>(this, nullSafe(values), NOT_IN); + return new InCondition<>(this, nullSafeList(values), NOT_IN); } @Override @@ -1949,7 +1952,7 @@ abstract class AbstractField extends AbstractTypedNamed implements Field concat(String... values) { - return DSL.concat(Tools.combine(this, Tools.fields(values).toArray(EMPTY_FIELD))); + return DSL.concat(Tools.combine(this, Tools.fieldsArray(values))); } @Override @@ -2015,7 +2018,7 @@ abstract class AbstractField extends AbstractTypedNamed implements Field greatest(T... others) { - return DSL.greatest(this, Tools.fields(others).toArray(EMPTY_FIELD)); + return DSL.greatest(this, Tools.fieldsArray(others)); } @Override @@ -2028,7 +2031,7 @@ abstract class AbstractField extends AbstractTypedNamed implements Field least(T... others) { - return DSL.least(this, Tools.fields(others).toArray(EMPTY_FIELD)); + return DSL.least(this, Tools.fieldsArray(others)); } @Override @@ -2094,7 +2097,7 @@ abstract class AbstractField extends AbstractTypedNamed implements Field> fields) { this(Tools.row0(fields.toArray(EMPTY_FIELD))); } diff --git a/jOOQ/src/main/java/org/jooq/impl/Alias.java b/jOOQ/src/main/java/org/jooq/impl/Alias.java index 417d93c013..84cc327036 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Alias.java +++ b/jOOQ/src/main/java/org/jooq/impl/Alias.java @@ -333,9 +333,7 @@ final class Alias extends AbstractQueryPart { } private final void toSQLDerivedColumnList(Context ctx) { - ctx.sql(" (") - .visit(wrap(fieldAliases).indentSize(0)) - .sql(')'); + ctx.sql(" (").visit(wrap(fieldAliases).indentSize(0)).sql(')'); } @Override diff --git a/jOOQ/src/main/java/org/jooq/impl/BatchSingle.java b/jOOQ/src/main/java/org/jooq/impl/BatchSingle.java index 79dd6f7204..9b40a30e07 100644 --- a/jOOQ/src/main/java/org/jooq/impl/BatchSingle.java +++ b/jOOQ/src/main/java/org/jooq/impl/BatchSingle.java @@ -39,7 +39,6 @@ package org.jooq.impl; import static org.jooq.conf.ParamType.INLINED; import static org.jooq.conf.SettingsTools.executeStaticStatements; -import static org.jooq.impl.Tools.dataTypes; import static org.jooq.impl.Tools.fields; import static org.jooq.impl.Tools.visitAll; @@ -54,7 +53,6 @@ import java.util.Map.Entry; import org.jooq.BatchBindStep; import org.jooq.Configuration; -import org.jooq.DataType; import org.jooq.ExecuteContext; import org.jooq.ExecuteListener; import org.jooq.Param; @@ -197,8 +195,6 @@ final class BatchSingle extends AbstractBatch implements BatchBindStep { for (int i = 0; it.hasNext(); i++) params[i] = it.next().getValue(); - DataType[] paramTypes = dataTypes(params); - try { // [#8968] Keep start() event inside of lifecycle management listener.start(ctx); @@ -225,8 +221,8 @@ final class BatchSingle extends AbstractBatch implements BatchBindStep { // [#3547] The original query may have no Params specified - e.g. when it was constructed with // plain SQL. In that case, infer the bind value type directly from the bind value visitAll(new DefaultBindContext(configuration, ctx.statement()), - (paramTypes.length > 0) - ? fields(bindValues, paramTypes) + (params.length > 0) + ? fields(bindValues, params) : fields(bindValues)); listener.bindEnd(ctx); diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 1f29ee0be3..bc28971b8f 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -118,6 +118,7 @@ import java.time.LocalTime; import java.time.OffsetDateTime; import java.time.OffsetTime; import java.time.temporal.Temporal; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; @@ -12687,7 +12688,7 @@ public class DSL { */ @Support public static Field choose(int index, T... values) { - return choose(val(index), (Field[]) Tools.fields(values).toArray(EMPTY_FIELD)); + return choose(val(index), Tools.fieldsArray(values)); } /** @@ -12704,7 +12705,7 @@ public class DSL { */ @Support public static Field choose(Field index, T... values) { - return choose(index, (Field[]) Tools.fields(values).toArray(EMPTY_FIELD)); + return choose(index, Tools.fieldsArray(values)); } /** @@ -12839,7 +12840,7 @@ public class DSL { */ @Support public static Field decode(T value, T search, Z result, Object... more) { - return decode(Tools.field(value), Tools.field(search), Tools.field(result), Tools.fields(more).toArray(EMPTY_FIELD)); + return decode(Tools.field(value), Tools.field(search), Tools.field(result), Tools.fieldsArray(more)); } /** @@ -13178,7 +13179,7 @@ public class DSL { */ @Support public static Field coalesce(T value, T... values) { - return coalesce0(Tools.field(value), Tools.fields(values).toArray(EMPTY_FIELD)); + return coalesce0(Tools.field(value), Tools.fieldsArray(values)); } /** @@ -14215,7 +14216,7 @@ public class DSL { */ @Support public static Field concat(String... values) { - return concat(Tools.fields(values).toArray(EMPTY_FIELD)); + return concat(Tools.fieldsArray(values)); } /** @@ -17427,7 +17428,7 @@ public class DSL { */ @Support public static Field greatest(T value, T... values) { - return greatest(Tools.field(value), Tools.fields(values).toArray(EMPTY_FIELD)); + return greatest(Tools.field(value), Tools.fieldsArray(values)); } /** @@ -17457,7 +17458,7 @@ public class DSL { */ @Support public static Field least(T value, T... values) { - return least(Tools.field(value), Tools.fields(values).toArray(EMPTY_FIELD)); + return least(Tools.field(value), Tools.fieldsArray(values)); } /** @@ -22472,7 +22473,7 @@ public class DSL { */ @Support public static RowN row(Object... values) { - return row(Tools.fields(values).toArray(EMPTY_FIELD)); + return row(Tools.fieldsArray(values)); } @@ -23582,10 +23583,22 @@ public class DSL { return EMPTY_FIELD; Field[] result = new Field[fields.length]; - - for (int i = 0; i < fields.length; i++) { + for (int i = 0; i < fields.length; i++) result[i] = nullSafe(fields[i]); - } + + return result; + } + + /** + * Null-safety of a field. + */ + protected static List> nullSafeList(Field... fields) { + if (fields == null) + return asList(EMPTY_FIELD); + + List> result = new ArrayList<>(fields.length); + for (Field f : fields) + result.add(nullSafe(f)); return result; } diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index 397aa1b04b..5547e5c5bf 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -1342,9 +1342,8 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri ResultSetMetaData meta = rs.getMetaData(); int columns = meta.getColumnCount(); - for (int i = 0; i < types.length && i < columns; i++) { + for (int i = 0; i < types.length && i < columns; i++) fields[i] = field(meta.getColumnLabel(i + 1), types[i]); - } return fetchLazy(rs, fields); } @@ -1548,23 +1547,24 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri return new ResultImpl<>(configuration()); } else { - List> fields = new ArrayList<>(strings.get(0).length); - int firstRow = header ? 1 : 0; + String[] firstRow = strings.get(0); + Field[] fields = new Field[firstRow.length]; + int firstRowIndex = header ? 1 : 0; if (header) - for (String name : strings.get(0)) - fields.add(field(name(name), String.class)); + for (int i = 0; i < fields.length; i++) + fields[i] = field(name(firstRow[i]), String.class); else - for (int i = 0; i < strings.get(0).length; i++) - fields.add(field(name("COL" + (i + 1)), String.class)); + for (int i = 0; i < fields.length; i++) + fields[i] = field(name("COL" + (i + 1)), String.class); Result result = new ResultImpl<>(configuration(), fields); - if (strings.size() > firstRow) { - for (String[] values : strings.subList(firstRow, strings.size())) { + if (strings.size() > firstRowIndex) { + for (String[] values : strings.subList(firstRowIndex, strings.size())) { RecordImplN record = new RecordImplN(fields); - for (int i = 0; i < Math.min(values.length, fields.size()); i++) { + for (int i = 0; i < Math.min(values.length, fields.length); i++) { record.values[i] = values[i]; record.originals[i] = values[i]; } diff --git a/jOOQ/src/main/java/org/jooq/impl/InCondition.java b/jOOQ/src/main/java/org/jooq/impl/InCondition.java index 3e47db3dc1..d3c2dda75c 100644 --- a/jOOQ/src/main/java/org/jooq/impl/InCondition.java +++ b/jOOQ/src/main/java/org/jooq/impl/InCondition.java @@ -83,7 +83,6 @@ import static org.jooq.impl.Tools.isEmbeddable; import static org.jooq.tools.StringUtils.defaultIfNull; import java.util.AbstractList; -import java.util.Arrays; import java.util.List; import java.util.Set; @@ -99,18 +98,18 @@ import org.jooq.SQLDialect; */ final class InCondition extends AbstractCondition { - private static final long serialVersionUID = -1653924248576930761L; - private static final int IN_LIMIT = 1000; - private static final Clause[] CLAUSES_IN = { CONDITION, CONDITION_IN }; - private static final Clause[] CLAUSES_IN_NOT = { CONDITION, CONDITION_NOT_IN }; - private static final Set REQUIRES_IN_LIMIT = SQLDialect.supportedBy(FIREBIRD); - private static final Set NO_SUPPORT_EMPTY_LISTS = SQLDialect.supportedBy(CUBRID, DERBY, FIREBIRD, HSQLDB, MARIADB, MYSQL, POSTGRES); + private static final long serialVersionUID = -1653924248576930761L; + private static final int IN_LIMIT = 1000; + private static final Clause[] CLAUSES_IN = { CONDITION, CONDITION_IN }; + private static final Clause[] CLAUSES_IN_NOT = { CONDITION, CONDITION_NOT_IN }; + private static final Set REQUIRES_IN_LIMIT = SQLDialect.supportedBy(FIREBIRD); + private static final Set NO_SUPPORT_EMPTY_LISTS = SQLDialect.supportedBy(CUBRID, DERBY, FIREBIRD, HSQLDB, MARIADB, MYSQL, POSTGRES); - private final Field field; - private final Field[] values; - private final Comparator comparator; + private final Field field; + private final List> values; + private final Comparator comparator; - InCondition(Field field, Field[] values, Comparator comparator) { + InCondition(Field field, List> values, Comparator comparator) { this.field = field; this.values = values; this.comparator = comparator; @@ -133,24 +132,22 @@ final class InCondition extends AbstractCondition { } private final RowN[] rows() { - RowN[] result = new RowN[values.length]; + RowN[] result = new RowN[values.size()]; - for (int i = 0; i < values.length; i++) - result[i] = row(embeddedFields(values[i])); + for (int i = 0; i < result.length; i++) + result[i] = row(embeddedFields(values.get(i))); return result; } private final void accept0(Context ctx) { - List> list = Arrays.asList(values); - - if (list.size() == 0 && NO_SUPPORT_EMPTY_LISTS.contains(ctx.family())) { + if (values.size() == 0 && NO_SUPPORT_EMPTY_LISTS.contains(ctx.family())) { if (comparator == IN) ctx.visit(falseCondition()); else ctx.visit(trueCondition()); } - else if (list.size() > IN_LIMIT) { + else if (values.size() > IN_LIMIT) { // [#798] Oracle and some other dialects can only hold 1000 values // in an IN (...) clause switch (ctx.family()) { @@ -165,7 +162,7 @@ final class InCondition extends AbstractCondition { .formatIndentStart() .formatNewLine(); - for (int i = 0; i < list.size(); i += IN_LIMIT) { + for (int i = 0; i < values.size(); i += IN_LIMIT) { if (i > 0) { // [#1515] The connector depends on the IN / NOT IN @@ -182,7 +179,7 @@ final class InCondition extends AbstractCondition { } } - toSQLSubValues(ctx, padded(ctx, list.subList(i, Math.min(i + IN_LIMIT, list.size())))); + toSQLSubValues(ctx, padded(ctx, values.subList(i, Math.min(i + IN_LIMIT, values.size())))); } ctx.formatIndentEnd() @@ -193,13 +190,13 @@ final class InCondition extends AbstractCondition { // Most dialects can handle larger lists default: { - toSQLSubValues(ctx, list); + toSQLSubValues(ctx, values); break; } } } else { - toSQLSubValues(ctx, padded(ctx, list)); + toSQLSubValues(ctx, padded(ctx, values)); } } @@ -215,7 +212,7 @@ final class InCondition extends AbstractCondition { /** * Render the SQL for a sub-set of the IN clause's values */ - private void toSQLSubValues(Context ctx, List> subValues) { + private void toSQLSubValues(Context ctx, List> subValues) { ctx.visit(field) .sql(' ') .visit(comparator.toKeyword()) diff --git a/jOOQ/src/main/java/org/jooq/impl/InsertQueryImpl.java b/jOOQ/src/main/java/org/jooq/impl/InsertQueryImpl.java index 842c4547bb..7f307e0801 100644 --- a/jOOQ/src/main/java/org/jooq/impl/InsertQueryImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/InsertQueryImpl.java @@ -80,7 +80,6 @@ import static org.jooq.impl.QueryPartListView.wrap; import static org.jooq.impl.Tools.EMPTY_FIELD; import static org.jooq.impl.Tools.aliasedFields; import static org.jooq.impl.Tools.fieldNameStrings; -import static org.jooq.impl.Tools.fieldNames; import static org.jooq.impl.Tools.BooleanDataKey.DATA_CONSTRAINT_REFERENCE; import static org.jooq.impl.Tools.BooleanDataKey.DATA_INSERT_SELECT_WITHOUT_INSERT_COLUMN_LIST; import static org.jooq.impl.Tools.DataKey.DATA_ON_DUPLICATE_KEY_WHERE; @@ -752,11 +751,9 @@ final class InsertQueryImpl extends AbstractStoreQuery impl // re-used. Select rows = null; - Name[] aliases = fieldNames(insertMaps.fields().toArray(EMPTY_FIELD)); - for (Map, Field> map : insertMaps.maps()) { Select row = - select(aliasedFields(map.values().toArray(EMPTY_FIELD), aliases)) + select(aliasedFields(map.values().toArray(EMPTY_FIELD))) .whereNotExists( selectOne() .from(table()) diff --git a/jOOQ/src/main/java/org/jooq/impl/RecordImplN.java b/jOOQ/src/main/java/org/jooq/impl/RecordImplN.java index 1dea4995b3..b43a1390c4 100644 --- a/jOOQ/src/main/java/org/jooq/impl/RecordImplN.java +++ b/jOOQ/src/main/java/org/jooq/impl/RecordImplN.java @@ -57,11 +57,16 @@ class RecordImplN extends AbstractRecord implements InternalRecord { * Generated UID */ private static final long serialVersionUID = -2201346180421463830L; - + RecordImplN(Field... values) { super(values); } + /** + * @deprecated - 3.14.0 - [#8495] - Prevent the array copy and call + * {@link #RecordImplN(Field...)} instead. + */ + @Deprecated RecordImplN(Collection> fields) { super(fields); } diff --git a/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java b/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java index dab460a466..f1b46b0c97 100644 --- a/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/SelectQueryImpl.java @@ -1198,18 +1198,12 @@ final class SelectQueryImpl extends AbstractResultQuery imp // AUTHOR.ID, BOOK.ID, BOOK.TITLE final Field[] originalFields = Tools.fieldArray(getSelect()); - // ID, ID, TITLE - final Name[] originalNames = Tools.fieldNames(originalFields); - - // v1, v2, v3 - final Name[] alternativeNames = Tools.fieldNames(originalFields.length); - // AUTHOR.ID as v1, BOOK.ID as v2, BOOK.TITLE as v3 // Enforce x.* or just * if we have no known field names (e.g. when plain SQL tables are involved) final Field[] alternativeFields = Tools.combine( - alternativeNames.length == 0 + originalFields.length == 0 ? new Field[] { DSL.field("*") } - : Tools.aliasedFields(originalFields, alternativeNames), + : Tools.aliasedFields(originalFields), null ); @@ -1257,7 +1251,7 @@ final class SelectQueryImpl extends AbstractResultQuery imp }.as("rn"); // v1 as ID, v2 as ID, v3 as TITLE - final Field[] unaliasedFields = Tools.aliasedFields(Tools.fields(originalFields.length), originalNames); + final Field[] unaliasedFields = Tools.unaliasedFields(originalFields); ctx.visit(K_SELECT).separatorRequired(true) .declareFields(true) @@ -1374,12 +1368,6 @@ final class SelectQueryImpl extends AbstractResultQuery imp - - - - - - diff --git a/jOOQ/src/main/java/org/jooq/impl/Tools.java b/jOOQ/src/main/java/org/jooq/impl/Tools.java index 29e3b143bd..b8a1f27000 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Tools.java +++ b/jOOQ/src/main/java/org/jooq/impl/Tools.java @@ -246,7 +246,6 @@ import org.jooq.Result; import org.jooq.ResultOrRows; import org.jooq.Results; import org.jooq.Row; -import org.jooq.RowN; import org.jooq.SQLDialect; import org.jooq.Schema; import org.jooq.Scope; @@ -295,7 +294,6 @@ final class Tools { // ------------------------------------------------------------------------ static final byte[] EMPTY_BYTE = {}; - static final Class[] EMPTY_CLASS = {}; static final Clause[] EMPTY_CLAUSE = {}; static final Collection[] EMPTY_COLLECTION = {}; static final CommonTableExpression[] EMPTY_COMMON_TABLE_EXPRESSION = {}; @@ -309,9 +307,6 @@ final class Tools { static final QueryPart[] EMPTY_QUERYPART = {}; static final Record[] EMPTY_RECORD = {}; static final Row[] EMPTY_ROW = {}; - static final RowN[] EMPTY_ROWN = {}; - static final Schema[] EMPTY_SCHEMA = {}; - static final SelectFieldOrAsterisk[] EMPTY_SELECT_FIELD_OR_ASTERISK = {}; static final SortField[] EMPTY_SORTFIELD = {}; static final String[] EMPTY_STRING = {}; static final Table[] EMPTY_TABLE = {}; @@ -1173,82 +1168,6 @@ final class Tools { // XXX: Data-type related methods // ------------------------------------------------------------------------ - /** - * Useful conversion method - */ - static final Class[] types(Field[] fields) { - return types(dataTypes(fields)); - } - - /** - * Useful conversion method - */ - static final Class[] types(DataType[] types) { - if (types == null) - return null; - - Class[] result = new Class[types.length]; - - for (int i = 0; i < types.length; i++) { - if (types[i] != null) { - result[i] = types[i].getType(); - } - else { - result[i] = Object.class; - } - } - - return result; - } - - /** - * Useful conversion method - */ - static final Class[] types(Object[] values) { - if (values == null) - return null; - - Class[] result = new Class[values.length]; - - for (int i = 0; i < values.length; i++) { - if (values[i] instanceof Field) { - result[i] = ((Field) values[i]).getType(); - } - else if (values[i] != null) { - result[i] = values[i].getClass(); - } - else { - result[i] = Object.class; - } - } - - return result; - } - - /** - * Useful conversion method - */ - static final DataType[] dataTypes(Field[] fields) { - if (fields == null) - return null; - - DataType[] result = new DataType[fields.length]; - - for (int i = 0; i < fields.length; i++) { - if (fields[i] != null) { - result[i] = fields[i].getDataType(); - } - else { - result[i] = getDataType(Object.class); - } - } - - return result; - } - - /** - * Useful conversion method - */ static final DataType[] dataTypes(Class[] types) { if (types == null) return null; @@ -1264,13 +1183,6 @@ final class Tools { return result; } - /** - * Useful conversion method - */ - static final DataType[] dataTypes(Object[] values) { - return dataTypes(types(values)); - } - // ------------------------------------------------------------------------ // XXX: General utility methods // ------------------------------------------------------------------------ @@ -1310,11 +1222,19 @@ final class Tools { return result; } + static final String fieldNameString(int index) { + return "v" + index; + } + + static final Name fieldName(int index) { + return name(fieldNameString(index)); + } + static final Name[] fieldNames(int length) { Name[] result = new Name[length]; for (int i = 0; i < length; i++) - result[i] = name("v" + i); + result[i] = fieldName(i); return result; } @@ -1323,7 +1243,7 @@ final class Tools { String[] result = new String[length]; for (int i = 0; i < length; i++) - result[i] = "v" + i; + result[i] = fieldNameString(i); return result; } @@ -1371,10 +1291,9 @@ final class Tools { @SuppressWarnings("unchecked") static final Field[] fields(int length, DataType type) { Field[] result = new Field[length]; - Name[] names = fieldNames(length); for (int i = 0; i < length; i++) - result[i] = DSL.field(name(names[i]), type); + result[i] = DSL.field(fieldName(i), type); return result; } @@ -1403,28 +1322,45 @@ final class Tools { return result; } - static final Field[] aliasedFields(Field[] fields, Name[] aliases) { + static final Field[] unaliasedFields(Field[] fields) { if (fields == null) return null; Field[] result = new Field[fields.length]; for (int i = 0; i < fields.length; i++) - result[i] = fields[i].as(aliases[i]); + result[i] = DSL.field(fieldName(i), fields[i].getDataType()).as(fields[i]); return result; } - static final Field[] fieldsByName(Collection fieldNames) { - return fieldsByName(null, fieldNames.toArray(EMPTY_STRING)); + static final Field[] aliasedFields(Field[] fields) { + if (fields == null) + return null; + + Field[] result = new Field[fields.length]; + + for (int i = 0; i < fields.length; i++) + result[i] = fields[i].as(fieldName(i)); + + return result; } static final Field[] fieldsByName(String[] fieldNames) { return fieldsByName(null, fieldNames); } - static final Field[] fieldsByName(String tableName, Collection fieldNames) { - return fieldsByName(tableName, fieldNames.toArray(EMPTY_STRING)); + static final Field[] fieldsByName(Name tableName, int length) { + Field[] result = new Field[length]; + + if (tableName == null) + for (int i = 0; i < length; i++) + result[i] = DSL.field(fieldName(i)); + else + for (int i = 0; i < length; i++) + result[i] = DSL.field(name(tableName, fieldName(i))); + + return result; } static final Field[] fieldsByName(Name tableName, Name[] fieldNames) { @@ -1724,14 +1660,6 @@ final class Tools { return DSL.field(name); } - /** - * Be sure that a given object is a field. - * - * @param value The argument object - * @param field The field to take the bind value type from - * @return The argument object itself, if it is a {@link Field}, or a bind - * value created from the argument object. - */ @SuppressWarnings("unchecked") static final Field field(Object value, Field field) { @@ -1747,14 +1675,6 @@ final class Tools { return val(value, field); } - /** - * Be sure that a given object is a field. - * - * @param value The argument object - * @param type The type to take the bind value type from - * @return The argument object itself, if it is a {@link Field}, or a bind - * value created from the argument object. - */ @SuppressWarnings("unchecked") static final Field field(Object value, Class type) { @@ -1770,14 +1690,6 @@ final class Tools { return val(value, type); } - /** - * Be sure that a given object is a field. - * - * @param value The argument object - * @param type The type to take the bind value type from - * @return The argument object itself, if it is a {@link Field}, or a bind - * value created from the argument object. - */ @SuppressWarnings("unchecked") static final Field field(Object value, DataType type) { @@ -1793,53 +1705,28 @@ final class Tools { return val(value, type); } - /** - * Be sure that a given set of objects are fields. - * - * @param values The argument objects - * @return The argument objects themselves, if they are {@link Field}s, or a bind - * values created from the argument objects. - */ static final List> fields(T[] values) { if (values == null) return new ArrayList<>(); List> result = new ArrayList<>(values.length); - for (int i = 0; i < values.length; i++) result.add(field(values[i])); return result; } - /** - * Be sure that a given set of objects are fields. - * - * @param values The argument objects - * @param field The field to take the bind value types from - * @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, Field field) { if (values == null || field == null) return new ArrayList<>(); List> result = new ArrayList<>(values.length); - for (int i = 0; i < values.length; i++) result.add(field(values[i], field)); return result; } - /** - * Be sure that a given set of objects are fields. - * - * @param values The argument objects - * @param fields The fields to take the bind value types from - * @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, Field[] fields) { if (values == null || fields == null) return new ArrayList<>(); @@ -1853,14 +1740,6 @@ final class Tools { return result; } - /** - * Be sure that a given set of objects are fields. - * - * @param values The argument objects - * @param fields The fields to take the bind value types from - * @return The argument objects themselves, if they are {@link Field}s, or a bind - * values created from the argument objects. - */ static final Field[] fieldsArray(Object[] values, Field[] fields) { if (values == null || fields == null) return EMPTY_FIELD; @@ -1874,34 +1753,17 @@ final class Tools { return result; } - /** - * Be sure that a given set of objects are fields. - * - * @param values The argument objects - * @param type The type to take the bind value types from - * @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, Class type) { if (values == null || type == null) return new ArrayList<>(); List> result = new ArrayList<>(values.length); - for (int i = 0; i < values.length; i++) result.add(field(values[i], type)); return result; } - /** - * Be sure that a given set of objects are fields. - * - * @param values The argument objects - * @param types The types to take the bind value types from - * @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, Class[] types) { if (values == null || types == null) return new ArrayList<>(); @@ -1915,54 +1777,28 @@ final class Tools { return result; } - /** - * Be sure that a given set of objects are fields. - * - * @param values The argument objects - * @param type The type to take the bind value types from - * @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, DataType type) { if (values == null || type == null) return new ArrayList<>(); List> result = new ArrayList<>(values.length); - for (Object value : values) result.add(field(value, type)); return result; } - /** - * Be sure that a given set of objects are fields. - * - * @param values The argument objects - * @param type The type to take the bind value types from - * @return The argument objects themselves, if they are {@link Field}s, or a bind - * values created from the argument objects. - */ static final List> fields(Collection values, DataType type) { if (values == null || type == null) return new ArrayList<>(); List> result = new ArrayList<>(values.size()); - for (Object value : values) result.add(field(value, type)); return result; } - /** - * Be sure that a given set of objects are fields. - * - * @param values The argument objects - * @param types The types to take the bind value types from - * @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, DataType[] types) { if (values == null || types == null) return new ArrayList<>(); @@ -1976,14 +1812,18 @@ final class Tools { return result; } - /** - * Be sure that a given set of objects are fields. - * - * @param values The argument objects - * @param types The types to take the bind value types from - * @return The argument objects themselves, if they are {@link Field}s, or a bind - * values created from the argument objects. - */ + @SuppressWarnings("unchecked") + static final Field[] fieldsArray(T[] values) { + if (values == null) + return (Field[]) EMPTY_FIELD; + + Field[] result = new Field[values.length]; + for (int i = 0; i < values.length; i++) + result[i] = field(values[i]); + + return result; + } + static final Field[] fieldsArray(Object[] values, DataType[] types) { if (values == null || types == null) return EMPTY_FIELD; @@ -2002,7 +1842,6 @@ final class Tools { return new ArrayList<>(); List> result = new ArrayList<>(values.length); - for (int i = 0; i < values.length; i++) result.add(DSL.inline(values[i])); @@ -2015,7 +1854,6 @@ final class Tools { return (Field[]) EMPTY_FIELD; Field[] result = new Field[fieldIndexes.length]; - for (int i = 0; i < fieldIndexes.length; i++) result[i] = DSL.inline(fieldIndexes[i]); @@ -2100,14 +1938,6 @@ final class Tools { return result; } - /** - * Create a new array - */ - @SafeVarargs - static final T[] array(T... array) { - return array; - } - /** * Reverse an array. */ diff --git a/jOOQ/src/main/java/org/jooq/impl/UnqualifiedName.java b/jOOQ/src/main/java/org/jooq/impl/UnqualifiedName.java index aaf47fde4c..e72ecf8bd5 100644 --- a/jOOQ/src/main/java/org/jooq/impl/UnqualifiedName.java +++ b/jOOQ/src/main/java/org/jooq/impl/UnqualifiedName.java @@ -60,7 +60,7 @@ final class UnqualifiedName extends AbstractName { private static final long serialVersionUID = 8562325639223483938L; private final String name; - private final Quoted quoted; + private final Quoted quoted; UnqualifiedName(String name) { this(name, DEFAULT);