提交 5b022f39 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Remove dependency between Mode and Function

上级 2a7c3604
......@@ -10,8 +10,6 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
import java.util.regex.Pattern;
import org.h2.expression.Function;
import org.h2.expression.FunctionInfo;
import org.h2.util.StringUtils;
import org.h2.value.DataType;
import org.h2.value.Value;
......@@ -206,8 +204,6 @@ public class Mode {
*/
public HashMap<String, DataType> typeByNameMap = new HashMap<>();
public HashMap<String, FunctionInfo> functionAliases;
private final String name;
private final ModeEnum modeEnum;
......@@ -273,9 +269,6 @@ public class Mode {
dt.sqlType = Types.NUMERIC;
dt.name = "SMALLMONEY";
mode.typeByNameMap.put("SMALLMONEY", dt);
HashMap<String, FunctionInfo> functions = new HashMap<>();
copyFunction(functions, "RANDOM_UUID", "NEWID");
mode.functionAliases = functions;
add(mode);
mode = new Mode(ModeEnum.MySQL);
......@@ -349,10 +342,6 @@ public class 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) {
this.name = modeEnum.name();
this.modeEnum = modeEnum;
......
......@@ -28,6 +28,7 @@ import org.h2.engine.Database;
import org.h2.engine.Mode;
import org.h2.engine.Session;
import org.h2.message.DbException;
import org.h2.mode.FunctionsMSSQLServer;
import org.h2.schema.Schema;
import org.h2.schema.Sequence;
import org.h2.security.BlockCipher;
......@@ -474,7 +475,13 @@ public class Function extends Expression implements FunctionCall {
addFunction("VALUES", VALUES, 1, Value.NULL, false, true, false);
}
protected Function(Database database, FunctionInfo info) {
/**
* Creates a new instance of function.
*
* @param database database
* @param info function information
*/
public Function(Database database, FunctionInfo info) {
this.database = database;
this.info = info;
if (info.parameterCount == VAR_ARGS) {
......@@ -521,12 +528,10 @@ public class Function extends Expression implements FunctionCall {
}
FunctionInfo info = FUNCTIONS.get(name);
if (info == null) {
HashMap<String, FunctionInfo> aliases = database.getMode().functionAliases;
if (aliases == null) {
return null;
}
info = aliases.get(name);
if (info == null) {
switch (database.getMode().getEnum()) {
case MSSQLServer:
return FunctionsMSSQLServer.getFunction(database, name);
default:
return null;
}
}
......
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.mode;
import java.util.HashMap;
import org.h2.expression.Function;
import org.h2.expression.FunctionInfo;
/**
* Base class for mode-specific functions.
*/
abstract class FunctionsBase {
/**
* Copy a standard function to a mode functions with a different name.
*
* @param functions
* mode functions
* @param stdName
* the name of the standard function
* @param newName
* the name of the mode-specific function
*/
static void copyFunction(HashMap<String, FunctionInfo> functions, String stdName, String newName) {
functions.put(newName, new FunctionInfo(Function.getFunctionInfo(stdName), newName));
}
}
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.mode;
import java.util.HashMap;
import org.h2.engine.Database;
import org.h2.expression.Function;
import org.h2.expression.FunctionInfo;
/**
* Functions for {@link org.h2.engine.Mode.ModeEnum#MSSQLServer} compatibility
* mode.
*/
public final class FunctionsMSSQLServer extends FunctionsBase {
private static final HashMap<String, FunctionInfo> FUNCTIONS = new HashMap<>();
static {
copyFunction(FUNCTIONS, "RANDOM_UUID", "NEWID");
}
/**
* Returns mode-specific function for a given name, or {@code null}.
*
* @param database
* the database
* @param upperName
* the upper-case name of a function
* @return the function with specified name or {@code null}
*/
public static Function getFunction(Database database, String upperName) {
FunctionInfo info = FUNCTIONS.get(upperName);
return info != null ? new Function(database, info) : null;
}
private FunctionsMSSQLServer() {
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论