提交 d071f831 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add NEWID() as alias to RANDOM_UUID() in SQL Server compatibility mode

上级 5e5e6b00
...@@ -10,6 +10,8 @@ import java.util.Collections; ...@@ -10,6 +10,8 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.h2.expression.Function;
import org.h2.expression.FunctionInfo;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
import org.h2.value.DataType; import org.h2.value.DataType;
import org.h2.value.Value; import org.h2.value.Value;
...@@ -204,6 +206,8 @@ public class Mode { ...@@ -204,6 +206,8 @@ public class Mode {
*/ */
public HashMap<String, DataType> typeByNameMap = new HashMap<>(); public HashMap<String, DataType> typeByNameMap = new HashMap<>();
public HashMap<String, FunctionInfo> functionAliases;
private final String name; private final String name;
private final ModeEnum modeEnum; private final ModeEnum modeEnum;
...@@ -269,6 +273,9 @@ public class Mode { ...@@ -269,6 +273,9 @@ public class Mode {
dt.sqlType = Types.NUMERIC; dt.sqlType = Types.NUMERIC;
dt.name = "SMALLMONEY"; dt.name = "SMALLMONEY";
mode.typeByNameMap.put("SMALLMONEY", dt); mode.typeByNameMap.put("SMALLMONEY", dt);
HashMap<String, FunctionInfo> functions = new HashMap<>();
copyFunction(functions, "RANDOM_UUID", "NEWID");
mode.functionAliases = functions;
add(mode); add(mode);
mode = new Mode(ModeEnum.MySQL); mode = new Mode(ModeEnum.MySQL);
...@@ -342,6 +349,10 @@ public class Mode { ...@@ -342,6 +349,10 @@ public class Mode {
add(mode); add(mode);
} }
static void copyFunction(HashMap<String, FunctionInfo> functions, String stdName, String newName) {
functions.put(newName, new FunctionInfo(Function.getFunctionInfo(stdName), newName));
}
private Mode(ModeEnum modeEnum) { private Mode(ModeEnum modeEnum) {
this.name = modeEnum.name(); this.name = modeEnum.name();
this.modeEnum = modeEnum; this.modeEnum = modeEnum;
......
...@@ -487,15 +487,8 @@ public class Function extends Expression implements FunctionCall { ...@@ -487,15 +487,8 @@ public class Function extends Expression implements FunctionCall {
private static void addFunction(String name, int type, int parameterCount, private static void addFunction(String name, int type, int parameterCount,
int returnDataType, boolean nullIfParameterIsNull, boolean deterministic, int returnDataType, boolean nullIfParameterIsNull, boolean deterministic,
boolean bufferResultSetToLocalTemp) { boolean bufferResultSetToLocalTemp) {
FunctionInfo info = new FunctionInfo(); FUNCTIONS.put(name, new FunctionInfo(name, type, parameterCount, returnDataType, nullIfParameterIsNull,
info.name = name; deterministic, bufferResultSetToLocalTemp));
info.type = type;
info.parameterCount = parameterCount;
info.returnDataType = returnDataType;
info.nullIfParameterIsNull = nullIfParameterIsNull;
info.deterministic = deterministic;
info.bufferResultSetToLocalTemp = bufferResultSetToLocalTemp;
FUNCTIONS.put(name, info);
} }
private static void addFunctionNotDeterministic(String name, int type, private static void addFunctionNotDeterministic(String name, int type,
...@@ -528,8 +521,15 @@ public class Function extends Expression implements FunctionCall { ...@@ -528,8 +521,15 @@ public class Function extends Expression implements FunctionCall {
} }
FunctionInfo info = FUNCTIONS.get(name); FunctionInfo info = FUNCTIONS.get(name);
if (info == null) { if (info == null) {
HashMap<String, FunctionInfo> aliases = database.getMode().functionAliases;
if (aliases == null) {
return null; return null;
} }
info = aliases.get(name);
if (info == null) {
return null;
}
}
switch (info.type) { switch (info.type) {
case TABLE: case TABLE:
case TABLE_DISTINCT: case TABLE_DISTINCT:
...@@ -539,6 +539,16 @@ public class Function extends Expression implements FunctionCall { ...@@ -539,6 +539,16 @@ public class Function extends Expression implements FunctionCall {
} }
} }
/**
* Returns function information for the specified function name.
*
* @param upperName the function name in upper case
* @return the function information or {@code null}
*/
public static FunctionInfo getFunctionInfo(String upperName) {
return FUNCTIONS.get(upperName);
}
/** /**
* Set the parameter expression at the given index. * Set the parameter expression at the given index.
* *
......
...@@ -8,41 +8,91 @@ package org.h2.expression; ...@@ -8,41 +8,91 @@ package org.h2.expression;
/** /**
* This class contains information about a built-in function. * This class contains information about a built-in function.
*/ */
class FunctionInfo { public final class FunctionInfo {
/** /**
* The name of the function. * The name of the function.
*/ */
String name; final String name;
/** /**
* The function type. * The function type.
*/ */
int type; final int type;
/** /**
* The data type of the return value. * The number of parameters.
*/ */
int returnDataType; final int parameterCount;
/** /**
* The number of parameters. * The data type of the return value.
*/ */
int parameterCount; final int returnDataType;
/** /**
* If the result of the function is NULL if any of the parameters is NULL. * If the result of the function is NULL if any of the parameters is NULL.
*/ */
boolean nullIfParameterIsNull; final boolean nullIfParameterIsNull;
/** /**
* If this function always returns the same value for the same parameters. * If this function always returns the same value for the same parameters.
*/ */
boolean deterministic; final boolean deterministic;
/** /**
* Should the return value ResultSet be buffered in a local temporary file? * Should the return value ResultSet be buffered in a local temporary file?
*/ */
boolean bufferResultSetToLocalTemp = true; final boolean bufferResultSetToLocalTemp;
/**
* Creates new instance of built-in function information.
*
* @param name
* the name of the function
* @param type
* the function type
* @param parameterCount
* the number of parameters
* @param returnDataType
* the data type of the return value
* @param nullIfParameterIsNull
* if the result of the function is NULL if any of the parameters
* is NULL
* @param deterministic
* if this function always returns the same value for the same
* parameters
* @param bufferResultSetToLocalTemp
* should the return value ResultSet be buffered in a local
* temporary file?
*/
FunctionInfo(String name, int type, int parameterCount, int returnDataType, boolean nullIfParameterIsNull,
boolean deterministic, boolean bufferResultSetToLocalTemp) {
this.name = name;
this.type = type;
this.parameterCount = parameterCount;
this.returnDataType = returnDataType;
this.nullIfParameterIsNull = nullIfParameterIsNull;
this.deterministic = deterministic;
this.bufferResultSetToLocalTemp = bufferResultSetToLocalTemp;
}
/**
* Creates a copy of built-in function information with a different name.
*
* @param source
* the source information
* @param name
* the new name
*/
public FunctionInfo(FunctionInfo source, String name) {
this.name = name;
type = source.type;
returnDataType = source.returnDataType;
parameterCount = source.parameterCount;
nullIfParameterIsNull = source.nullIfParameterIsNull;
deterministic = source.deterministic;
bufferResultSetToLocalTemp = source.bufferResultSetToLocalTemp;
}
} }
...@@ -2,3 +2,21 @@ ...@@ -2,3 +2,21 @@
-- and the EPL 1.0 (http://h2database.com/html/license.html). -- and the EPL 1.0 (http://h2database.com/html/license.html).
-- Initial Developer: H2 Group -- Initial Developer: H2 Group
-- --
SELECT LENGTH(CAST(RANDOM_UUID() AS VARCHAR));
>> 36
SELECT RANDOM_UUID() = RANDOM_UUID();
>> FALSE
SELECT NEWID();
> exception FUNCTION_NOT_FOUND_1
SET MODE MSSQLServer;
> ok
SELECT LENGTH(CAST(NEWID() AS VARCHAR));
>> 36
SET MODE Regular;
> ok
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论