This commit is contained in:
Lukas Eder 2020-12-04 17:55:01 +01:00
parent a95431170d
commit e137636a9e
9 changed files with 66 additions and 42 deletions

View File

@ -38,6 +38,7 @@
package org.jooq.impl;
import static org.jooq.impl.Names.N_COALESCE;
import static org.jooq.impl.SQLDataType.OTHER;
import static org.jooq.impl.Tools.anyNotNull;
import org.jooq.Context;
@ -57,7 +58,7 @@ final class Coalesce<T> extends AbstractField<T> {
@SuppressWarnings({ "unchecked", "rawtypes" })
Coalesce(Field<?>[] fields) {
super(N_COALESCE, (DataType) anyNotNull(fields));
super(N_COALESCE, anyNotNull((DataType) OTHER, fields[0], fields));
this.fields = (Field[]) fields;
}

View File

@ -15732,7 +15732,7 @@ public class DSL {
@NotNull
@Support
public static Field<String> substring(Field<String> field, int startingPosition) {
return substring(Tools.nullSafe(field), Tools.field(startingPosition));
return substring(field, Tools.field(startingPosition));
}
/**
@ -15745,7 +15745,7 @@ public class DSL {
@NotNull
@Support
public static Field<String> substring(Field<String> field, Field<? extends Number> startingPosition) {
return new Substring(Tools.nullSafe(field), Tools.nullSafe(startingPosition));
return new Substring(field, startingPosition);
}
/**
@ -15756,7 +15756,7 @@ public class DSL {
@NotNull
@Support
public static Field<String> substring(Field<String> field, int startingPosition, int length) {
return substring(Tools.nullSafe(field), Tools.field(startingPosition), Tools.field(length));
return substring(field, Tools.field(startingPosition), Tools.field(length));
}
/**
@ -15769,7 +15769,7 @@ public class DSL {
@NotNull
@Support
public static Field<String> substring(Field<String> field, Field<? extends Number> startingPosition, Field<? extends Number> length) {
return new Substring(Tools.nullSafe(field), Tools.nullSafe(startingPosition), Tools.nullSafe(length));
return new Substring(field, startingPosition, Tools.nullSafe(length));
}
/**
@ -15780,7 +15780,7 @@ public class DSL {
@NotNull
@Support
public static Field<String> mid(Field<String> field, int startingPosition, int length) {
return substring(Tools.nullSafe(field), Tools.field(startingPosition), Tools.field(length));
return substring(field, Tools.field(startingPosition), Tools.field(length));
}
/**
@ -15793,7 +15793,7 @@ public class DSL {
@NotNull
@Support
public static Field<String> mid(Field<String> field, Field<? extends Number> startingPosition, Field<? extends Number> length) {
return substring(Tools.nullSafe(field), Tools.nullSafe(startingPosition), Tools.nullSafe(length));
return substring(field, startingPosition, length);
}
/**

View File

@ -39,6 +39,7 @@ package org.jooq.impl;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.Names.N_LEFT;
import static org.jooq.impl.SQLDataType.VARCHAR;
import static org.jooq.impl.Tools.allNotNull;
import org.jooq.Context;
@ -58,7 +59,7 @@ final class Left extends AbstractField<String> {
private Field<? extends Number> length;
Left(Field<String> field, Field<? extends Number> length) {
super(N_LEFT, allNotNull(field, length));
super(N_LEFT, allNotNull(VARCHAR, field, length));
this.field = field;
this.length = length;

View File

@ -60,7 +60,7 @@ final class Nvl<T> extends AbstractField<T> {
private final Field<T> arg2;
Nvl(Field<T> arg1, Field<T> arg2) {
super(N_NVL, Tools.anyNotNull(arg1, arg2));
super(N_NVL, Tools.anyNotNull(arg1.getDataType(), arg1, arg2));
this.arg1 = arg1;
this.arg2 = arg2;

View File

@ -58,7 +58,7 @@ final class Nvl2<T> extends AbstractField<T> {
private final Field<T> arg3;
Nvl2(Field<?> arg1, Field<T> arg2, Field<T> arg3) {
super(N_NVL2, !arg1.getDataType().nullable() ? arg2.getDataType() : Tools.allNotNull(arg2, arg3));
super(N_NVL2, !arg1.getDataType().nullable() ? arg2.getDataType() : Tools.allNotNull(arg2.getDataType(), arg2, arg3));
this.arg1 = arg1;
this.arg2 = arg2;

View File

@ -43,6 +43,7 @@ import static org.jooq.impl.Names.N_REPEAT;
import static org.jooq.impl.Names.N_REPLACE;
import static org.jooq.impl.Names.N_REPLICATE;
import static org.jooq.impl.Names.N_ZEROBLOB;
import static org.jooq.impl.SQLDataType.VARCHAR;
import static org.jooq.impl.Tools.allNotNull;
import org.jooq.Context;
@ -62,7 +63,7 @@ final class Repeat extends AbstractField<String> {
private final Field<? extends Number> count;
Repeat(Field<String> string, Field<? extends Number> count) {
super(N_REPEAT, allNotNull(string, count));
super(N_REPEAT, allNotNull(VARCHAR, string, count));
this.string = string;
this.count = count;

View File

@ -41,6 +41,7 @@ import static org.jooq.impl.DSL.one;
import static org.jooq.impl.Internal.iadd;
import static org.jooq.impl.Internal.isub;
import static org.jooq.impl.Names.N_RIGHT;
import static org.jooq.impl.SQLDataType.VARCHAR;
import static org.jooq.impl.Tools.allNotNull;
import org.jooq.Context;
@ -60,7 +61,7 @@ final class Right extends AbstractField<String> {
private Field<? extends Number> length;
Right(Field<String> field, Field<? extends Number> length) {
super(N_RIGHT, allNotNull(field, length));
super(N_RIGHT, allNotNull(VARCHAR, field, length));
this.field = field;
this.length = length;

View File

@ -43,9 +43,15 @@ import static org.jooq.impl.Keywords.K_FROM;
import static org.jooq.impl.Names.N_MID;
import static org.jooq.impl.Names.N_SUBSTR;
import static org.jooq.impl.Names.N_SUBSTRING;
import static org.jooq.impl.SQLDataType.INTEGER;
import static org.jooq.impl.SQLDataType.VARCHAR;
import static org.jooq.impl.Tools.allNotNull;
import static org.jooq.impl.Tools.convertVal;
import static org.jooq.impl.Tools.nullSafe;
import static org.jooq.impl.Tools.nullableIf;
import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.Name;
@ -57,22 +63,26 @@ final class Substring extends AbstractField<String> {
/**
* Generated UID
*/
private static final long serialVersionUID = -7273879239726265322L;
private static final long serialVersionUID = -7273879239726265322L;
private final Field<String> field;
private final Field<? extends Number> startingPosition;
private final Field<? extends Number> length;
private final Field<String> field;
private final Field<? extends Number> startingPosition;
private final Field<? extends Number> length;
Substring(Field<String> field, Field<? extends Number> startingPosition) {
this(field, startingPosition, null);
super(N_SUBSTRING, allNotNull(VARCHAR, field, startingPosition));
this.field = nullableIf(false, nullSafe(field, VARCHAR));
this.startingPosition = nullableIf(false, nullSafe(startingPosition, INTEGER));
this.length = null;
}
Substring(Field<String> field, Field<? extends Number> startingPosition, Field<? extends Number> length) {
super(N_SUBSTRING, VARCHAR);
super(N_SUBSTRING, allNotNull(VARCHAR, field, startingPosition, length));
this.field = field;
this.startingPosition = startingPosition;
this.length = length;
this.field = nullableIf(false, nullSafe(field, VARCHAR));
this.startingPosition = nullableIf(false, nullSafe(startingPosition, INTEGER));
this.length = nullableIf(false, nullSafe(length, INTEGER));
}
@Override

View File

@ -301,6 +301,8 @@ import org.jooq.types.UInteger;
import org.jooq.types.ULong;
import org.jooq.types.UShort;
import org.jetbrains.annotations.NotNull;
/**
* General internal jOOQ utilities
*
@ -5897,54 +5899,57 @@ final class Tools {
return result;
}
static final <T> DataType<T> allNotNull(Field<T> f1, Field<?> f2) {
DataType<T> result = f1.getDataType();
static final DataType<?> dataType(Field<?> field) {
return field == null ? OTHER : field.getDataType();
}
static final <T> DataType<T> allNotNull(DataType<T> defaultType, Field<T> f1, Field<?> f2) {
if (f1 == null)
return defaultType.null_();
DataType<T> result = f1.getDataType();
if (result.nullable())
return result;
else if (f2.getDataType().nullable())
else if (dataType(f2).nullable())
return result.null_();
else
return result;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
static final DataType<?> allNotNull(Field<?>... fields) {
if (fields == null || fields.length == 0)
return OTHER;
static final <T> DataType<T> allNotNull(DataType<T> defaultType, Field<T> f1, Field<?>... fields) {
if (f1 == null)
return defaultType.null_();
DataType<?> result = fields[0].getDataType();
DataType<T> result = f1.getDataType();
if (result.nullable())
return result;
for (int i = 1; i < fields.length; i++)
if (fields[i].getDataType().nullable())
for (int i = 0; i < fields.length; i++)
if (dataType(fields[i]).nullable())
return result.null_();
return result;
}
static final <T> DataType<T> anyNotNull(Field<T> f1, Field<?> f2) {
DataType<T> result = f1.getDataType();
static final <T> DataType<T> anyNotNull(DataType<T> defaultType, Field<T> f1, Field<?> f2) {
DataType<T> result = f1 == null ? defaultType : f1.getDataType();
if (!result.nullable())
return result;
else if (!f2.getDataType().nullable())
else if (!dataType(f2).nullable())
return result.notNull();
else
return result;
}
static final DataType<?> anyNotNull(Field<?>... fields) {
if (fields == null || fields.length == 0)
return OTHER;
static final <T> DataType<T> anyNotNull(DataType<T> defaultType, Field<T> f1, Field<?>... fields) {
DataType<T> result = f1 == null ? defaultType : f1.getDataType();
DataType<?> result = fields[0].getDataType();
if (!result.nullable())
return result;
for (int i = 1; i < fields.length; i++)
if (!fields[i].getDataType().nullable())
for (int i = 0; i < fields.length; i++)
if (!dataType(fields[i]).nullable())
return result.notNull();
return result;
@ -5958,8 +5963,13 @@ final class Tools {
static final <T> Field<T> nullSafe(Field<T> field, DataType<?> type) {
return field == null
? (Field<T>) DSL.val((T) null, type)
: field instanceof Val
? (Field<T>) ((Val<T>) field).convertTo(type)
: convertVal(field, type);
}
@SuppressWarnings("unchecked")
static final <T> Field<T> convertVal(Field<T> field, DataType<?> type) {
return isVal(field)
? (Field<T>) extractVal(field).convertTo(type)
: field;
}