Debian Codegraph @

src/main/org/h2/mode/FunctionsMSSQLServer.java

raw
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 java.util.HashMap;
9
10 import org.h2.api.ErrorCode;
11 import org.h2.engine.SessionLocal;
12 import org.h2.expression.Expression;
13 import org.h2.expression.TypedValueExpression;
14 import org.h2.expression.function.CoalesceFunction;
15 import org.h2.expression.function.CurrentDateTimeValueFunction;
16 import org.h2.expression.function.RandFunction;
17 import org.h2.expression.function.StringFunction;
18 import org.h2.message.DbException;
19 import org.h2.value.TypeInfo;
20 import org.h2.value.Value;
21 import org.h2.value.ValueBigint;
22 import org.h2.value.ValueNull;
23
24 /**
25 * Functions for {@link org.h2.engine.Mode.ModeEnum#MSSQLServer} compatibility
26 * mode.
27 */
28 public final class FunctionsMSSQLServer extends ModeFunction {
29
30 private static final HashMap<String, FunctionInfo> FUNCTIONS = new HashMap<>();
31
32 private static final int CHARINDEX = 4001;
33
34 private static final int GETDATE = CHARINDEX + 1;
35
36 private static final int ISNULL = GETDATE + 1;
37
38 private static final int LEN = ISNULL + 1;
39
40 private static final int NEWID = LEN + 1;
41
42 private static final int NEWSEQUENTIALID = NEWID + 1;
43
44 private static final int SCOPE_IDENTITY = NEWSEQUENTIALID + 1;
45
46 private static final TypeInfo SCOPE_IDENTITY_TYPE = TypeInfo.getTypeInfo(Value.NUMERIC, 38, 0, null);
47
48 static {
49 FUNCTIONS.put("CHARINDEX", new FunctionInfo("CHARINDEX", CHARINDEX, VAR_ARGS, Value.INTEGER, true, true));
50 FUNCTIONS.put("GETDATE", new FunctionInfo("GETDATE", GETDATE, 0, Value.TIMESTAMP, false, true));
51 FUNCTIONS.put("LEN", new FunctionInfo("LEN", LEN, 1, Value.INTEGER, true, true));
52 FUNCTIONS.put("NEWID", new FunctionInfo("NEWID", NEWID, 0, Value.UUID, true, false));
53 FUNCTIONS.put("NEWSEQUENTIALID",
54 new FunctionInfo("NEWSEQUENTIALID", NEWSEQUENTIALID, 0, Value.UUID, true, false));
55 FUNCTIONS.put("ISNULL", new FunctionInfo("ISNULL", ISNULL, 2, Value.NULL, false, true));
56 FUNCTIONS.put("SCOPE_IDENTITY",
57 new FunctionInfo("SCOPE_IDENTITY", SCOPE_IDENTITY, 0, Value.NUMERIC, true, false));
58 }
59
60 /**
61 * Returns mode-specific function for a given name, or {@code null}.
62 *
63 * @param upperName
64 * the upper-case name of a function
65 * @return the function with specified name or {@code null}
66 */
67 public static FunctionsMSSQLServer getFunction(String upperName) {
68 FunctionInfo info = FUNCTIONS.get(upperName);
69 if (info != null) {
70 return new FunctionsMSSQLServer(info);
71 }
72 return null;
73 }
74
75 private FunctionsMSSQLServer(FunctionInfo info) {
76 super(info);
77 }
78
79 @Override
80 protected void checkParameterCount(int len) {
81 int min, max;
82 switch (info.type) {
83 case CHARINDEX:
84 min = 2;
85 max = 3;
86 break;
87 default:
88 throw DbException.getInternalError("type=" + info.type);
89 }
90 if (len < min || len > max) {
91 throw DbException.get(ErrorCode.INVALID_PARAMETER_COUNT_2, info.name, min + ".." + max);
92 }
93 }
94
95 @Override
96 public Value getValue(SessionLocal session) {
97 Value[] values = getArgumentsValues(session, args);
98 if (values == null) {
99 return ValueNull.INSTANCE;
100 }
101 Value v0 = getNullOrValue(session, args, values, 0);
102 switch (info.type) {
103 case LEN: {
104 long len;
105 if (v0.getValueType() == Value.CHAR) {
106 String s = v0.getString();
107 int l = s.length();
108 while (l > 0 && s.charAt(l - 1) == ' ') {
109 l--;
110 }
111 len = l;
112 } else {
113 len = v0.charLength();
114 }
115 return ValueBigint.get(len);
116 }
117 case SCOPE_IDENTITY:
118 return session.getLastIdentity().convertTo(type);
119 default:
120 throw DbException.getInternalError("type=" + info.type);
121 }
122 }
123
124 @Override
125 public Expression optimize(SessionLocal session) {
126 switch (info.type) {
127 case CHARINDEX:
128 return new StringFunction(args, StringFunction.LOCATE).optimize(session);
129 case GETDATE:
130 return new CurrentDateTimeValueFunction(CurrentDateTimeValueFunction.LOCALTIMESTAMP, 3).optimize(session);
131 case ISNULL:
132 return new CoalesceFunction(CoalesceFunction.COALESCE, args).optimize(session);
133 case NEWID:
134 case NEWSEQUENTIALID:
135 return new RandFunction(null, RandFunction.RANDOM_UUID).optimize(session);
136 case SCOPE_IDENTITY:
137 type = SCOPE_IDENTITY_TYPE;
138 break;
139 default:
140 type = TypeInfo.getTypeInfo(info.returnDataType);
141 if (optimizeArguments(session)) {
142 return TypedValueExpression.getTypedIfNull(getValue(session), type);
143 }
144 }
145 return this;
146 }
147
148 }