| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.mode; |
| 7 |
|
| 8 |
import org.h2.api.ErrorCode; |
| 9 |
import org.h2.engine.Database; |
| 10 |
import org.h2.engine.Mode.ModeEnum; |
| 11 |
import org.h2.engine.SessionLocal; |
| 12 |
import org.h2.expression.Expression; |
| 13 |
import org.h2.expression.ExpressionVisitor; |
| 14 |
import org.h2.expression.function.CurrentDateTimeValueFunction; |
| 15 |
import org.h2.expression.function.FunctionN; |
| 16 |
import org.h2.message.DbException; |
| 17 |
import org.h2.value.Value; |
| 18 |
import org.h2.value.ValueNull; |
| 19 |
|
| 20 |
/** |
| 21 |
* Base class for mode-specific functions. |
| 22 |
*/ |
| 23 |
public abstract class ModeFunction extends FunctionN { |
| 24 |
|
| 25 |
/** |
| 26 |
* Constant for variable number of arguments. |
| 27 |
*/ |
| 28 |
protected static final int VAR_ARGS = -1; |
| 29 |
|
| 30 |
/** |
| 31 |
* The information about this function. |
| 32 |
*/ |
| 33 |
protected final FunctionInfo info; |
| 34 |
|
| 35 |
/** |
| 36 |
* Get an instance of the given function for this database. |
| 37 |
* If no function with this name is found, null is returned. |
| 38 |
* |
| 39 |
* @param database the database |
| 40 |
* @param name the upper case function name |
| 41 |
* @return the function object or null |
| 42 |
*/ |
| 43 |
public static ModeFunction getFunction(Database database, String name) { |
| 44 |
ModeEnum modeEnum = database.getMode().getEnum(); |
| 45 |
if (modeEnum != ModeEnum.REGULAR) { |
| 46 |
return getCompatibilityModeFunction(name, modeEnum); |
| 47 |
} |
| 48 |
return null; |
| 49 |
} |
| 50 |
|
| 51 |
private static ModeFunction getCompatibilityModeFunction(String name, ModeEnum modeEnum) { |
| 52 |
switch (modeEnum) { |
| 53 |
case LEGACY: |
| 54 |
return FunctionsLegacy.getFunction(name); |
| 55 |
case DB2: |
| 56 |
case Derby: |
| 57 |
return FunctionsDB2Derby.getFunction(name); |
| 58 |
case MSSQLServer: |
| 59 |
return FunctionsMSSQLServer.getFunction(name); |
| 60 |
case MariaDB: |
| 61 |
case MySQL: |
| 62 |
return FunctionsMySQL.getFunction(name); |
| 63 |
case Oracle: |
| 64 |
return FunctionsOracle.getFunction(name); |
| 65 |
case PostgreSQL: |
| 66 |
return FunctionsPostgreSQL.getFunction(name); |
| 67 |
default: |
| 68 |
return null; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get an instance of the given function without parentheses for this |
| 74 |
* database. If no function with this name is found, null is returned. |
| 75 |
* |
| 76 |
* @param database the database |
| 77 |
* @param name the upper case function name |
| 78 |
* @param scale the scale, or {@code -1} |
| 79 |
* @return the function object or null |
| 80 |
*/ |
| 81 |
@SuppressWarnings("incomplete-switch") |
| 82 |
public static Expression getCompatibilityDateTimeValueFunction(Database database, String name, int scale) { |
| 83 |
switch (name) { |
| 84 |
case "SYSDATE": |
| 85 |
switch (database.getMode().getEnum()) { |
| 86 |
case LEGACY: |
| 87 |
case HSQLDB: |
| 88 |
case Oracle: |
| 89 |
return new CompatibilityDateTimeValueFunction(CompatibilityDateTimeValueFunction.SYSDATE, -1); |
| 90 |
} |
| 91 |
break; |
| 92 |
case "SYSTIMESTAMP": |
| 93 |
switch (database.getMode().getEnum()) { |
| 94 |
case LEGACY: |
| 95 |
case Oracle: |
| 96 |
return new CompatibilityDateTimeValueFunction(CompatibilityDateTimeValueFunction.SYSTIMESTAMP, scale); |
| 97 |
} |
| 98 |
break; |
| 99 |
case "TODAY": |
| 100 |
switch (database.getMode().getEnum()) { |
| 101 |
case LEGACY: |
| 102 |
case HSQLDB: |
| 103 |
return new CurrentDateTimeValueFunction(CurrentDateTimeValueFunction.CURRENT_DATE, scale); |
| 104 |
} |
| 105 |
break; |
| 106 |
} |
| 107 |
return null; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Creates a new instance of function. |
| 112 |
* |
| 113 |
* @param info function information |
| 114 |
*/ |
| 115 |
ModeFunction(FunctionInfo info) { |
| 116 |
super(new Expression[info.parameterCount != VAR_ARGS ? info.parameterCount : 4]); |
| 117 |
this.info = info; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get value transformed by expression, or null if i is out of range or |
| 122 |
* the input value is null. |
| 123 |
* |
| 124 |
* @param session database session |
| 125 |
* @param args expressions |
| 126 |
* @param values array of input values |
| 127 |
* @param i index of value of transform |
| 128 |
* @return value or null |
| 129 |
*/ |
| 130 |
static Value getNullOrValue(SessionLocal session, Expression[] args, |
| 131 |
Value[] values, int i) { |
| 132 |
if (i >= args.length) { |
| 133 |
return null; |
| 134 |
} |
| 135 |
Value v = values[i]; |
| 136 |
if (v == null) { |
| 137 |
Expression e = args[i]; |
| 138 |
if (e == null) { |
| 139 |
return null; |
| 140 |
} |
| 141 |
v = values[i] = e.getValue(session); |
| 142 |
} |
| 143 |
return v; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Gets values of arguments and checks them for NULL values if function |
| 148 |
* returns NULL on NULL argument. |
| 149 |
* |
| 150 |
* @param session |
| 151 |
* the session |
| 152 |
* @param args |
| 153 |
* the arguments |
| 154 |
* @return the values, or {@code null} if function should return NULL due to |
| 155 |
* NULL argument |
| 156 |
*/ |
| 157 |
final Value[] getArgumentsValues(SessionLocal session, Expression[] args) { |
| 158 |
Value[] values = new Value[args.length]; |
| 159 |
if (info.nullIfParameterIsNull) { |
| 160 |
for (int i = 0, l = args.length; i < l; i++) { |
| 161 |
Value v = args[i].getValue(session); |
| 162 |
if (v == ValueNull.INSTANCE) { |
| 163 |
return null; |
| 164 |
} |
| 165 |
values[i] = v; |
| 166 |
} |
| 167 |
} |
| 168 |
return values; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Check if the parameter count is correct. |
| 173 |
* |
| 174 |
* @param len the number of parameters set |
| 175 |
* @throws DbException if the parameter count is incorrect |
| 176 |
*/ |
| 177 |
void checkParameterCount(int len) { |
| 178 |
throw DbException.getInternalError("type=" + info.type); |
| 179 |
} |
| 180 |
|
| 181 |
@Override |
| 182 |
public void doneWithParameters() { |
| 183 |
int count = info.parameterCount; |
| 184 |
if (count == VAR_ARGS) { |
| 185 |
checkParameterCount(argsCount); |
| 186 |
super.doneWithParameters(); |
| 187 |
} else if (count != argsCount) { |
| 188 |
throw DbException.get(ErrorCode.INVALID_PARAMETER_COUNT_2, info.name, Integer.toString(argsCount)); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Optimizes arguments. |
| 194 |
* |
| 195 |
* @param session |
| 196 |
* the session |
| 197 |
* @return whether all arguments are constants and function is deterministic |
| 198 |
*/ |
| 199 |
final boolean optimizeArguments(SessionLocal session) { |
| 200 |
return optimizeArguments(session, info.deterministic); |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public String getName() { |
| 205 |
return info.name; |
| 206 |
} |
| 207 |
|
| 208 |
@Override |
| 209 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 210 |
if (!super.isEverything(visitor)) { |
| 211 |
return false; |
| 212 |
} |
| 213 |
switch (visitor.getType()) { |
| 214 |
case ExpressionVisitor.DETERMINISTIC: |
| 215 |
case ExpressionVisitor.QUERY_COMPARABLE: |
| 216 |
case ExpressionVisitor.READONLY: |
| 217 |
return info.deterministic; |
| 218 |
default: |
| 219 |
return true; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
} |