| 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: Jason Brittain (jason.brittain at gmail.com) |
| 5 |
*/ |
| 6 |
package org.h2.mode; |
| 7 |
|
| 8 |
import java.text.SimpleDateFormat; |
| 9 |
import java.util.Date; |
| 10 |
import java.util.HashMap; |
| 11 |
import java.util.Locale; |
| 12 |
|
| 13 |
import org.h2.api.ErrorCode; |
| 14 |
import org.h2.engine.SessionLocal; |
| 15 |
import org.h2.expression.Expression; |
| 16 |
import org.h2.expression.ValueExpression; |
| 17 |
import org.h2.message.DbException; |
| 18 |
import org.h2.util.DateTimeUtils; |
| 19 |
import org.h2.util.StringUtils; |
| 20 |
import org.h2.value.TypeInfo; |
| 21 |
import org.h2.value.Value; |
| 22 |
import org.h2.value.ValueBigint; |
| 23 |
import org.h2.value.ValueInteger; |
| 24 |
import org.h2.value.ValueNull; |
| 25 |
import org.h2.value.ValueTimestamp; |
| 26 |
import org.h2.value.ValueTimestampTimeZone; |
| 27 |
import org.h2.value.ValueVarchar; |
| 28 |
|
| 29 |
/** |
| 30 |
* This class implements some MySQL-specific functions. |
| 31 |
* |
| 32 |
* @author Jason Brittain |
| 33 |
* @author Thomas Mueller |
| 34 |
*/ |
| 35 |
public final class FunctionsMySQL extends ModeFunction { |
| 36 |
|
| 37 |
private static final int UNIX_TIMESTAMP = 1001, FROM_UNIXTIME = 1002, DATE = 1003, LAST_INSERT_ID = 1004; |
| 38 |
|
| 39 |
private static final HashMap<String, FunctionInfo> FUNCTIONS = new HashMap<>(); |
| 40 |
|
| 41 |
static { |
| 42 |
FUNCTIONS.put("UNIX_TIMESTAMP", |
| 43 |
new FunctionInfo("UNIX_TIMESTAMP", UNIX_TIMESTAMP, VAR_ARGS, Value.INTEGER, true, false)); |
| 44 |
FUNCTIONS.put("FROM_UNIXTIME", |
| 45 |
new FunctionInfo("FROM_UNIXTIME", FROM_UNIXTIME, VAR_ARGS, Value.VARCHAR, true, true)); |
| 46 |
FUNCTIONS.put("DATE", new FunctionInfo("DATE", DATE, 1, Value.DATE, true, true)); |
| 47 |
FUNCTIONS.put("LAST_INSERT_ID", |
| 48 |
new FunctionInfo("LAST_INSERT_ID", LAST_INSERT_ID, VAR_ARGS, Value.BIGINT, false, false)); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* The date format of a MySQL formatted date/time. |
| 53 |
* Example: 2008-09-25 08:40:59 |
| 54 |
*/ |
| 55 |
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; |
| 56 |
|
| 57 |
/** |
| 58 |
* Format replacements for MySQL date formats. |
| 59 |
* See |
| 60 |
* https://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format |
| 61 |
*/ |
| 62 |
private static final String[] FORMAT_REPLACE = { |
| 63 |
"%a", "EEE", |
| 64 |
"%b", "MMM", |
| 65 |
"%c", "MM", |
| 66 |
"%d", "dd", |
| 67 |
"%e", "d", |
| 68 |
"%H", "HH", |
| 69 |
"%h", "hh", |
| 70 |
"%I", "hh", |
| 71 |
"%i", "mm", |
| 72 |
"%j", "DDD", |
| 73 |
"%k", "H", |
| 74 |
"%l", "h", |
| 75 |
"%M", "MMMM", |
| 76 |
"%m", "MM", |
| 77 |
"%p", "a", |
| 78 |
"%r", "hh:mm:ss a", |
| 79 |
"%S", "ss", |
| 80 |
"%s", "ss", |
| 81 |
"%T", "HH:mm:ss", |
| 82 |
"%W", "EEEE", |
| 83 |
"%w", "F", |
| 84 |
"%Y", "yyyy", |
| 85 |
"%y", "yy", |
| 86 |
"%%", "%", |
| 87 |
}; |
| 88 |
|
| 89 |
/** |
| 90 |
* Get the seconds since 1970-01-01 00:00:00 UTC of the given timestamp. |
| 91 |
* See |
| 92 |
* https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_unix-timestamp |
| 93 |
* |
| 94 |
* @param session the session |
| 95 |
* @param value the timestamp |
| 96 |
* @return the timestamp in seconds since EPOCH |
| 97 |
*/ |
| 98 |
public static int unixTimestamp(SessionLocal session, Value value) { |
| 99 |
long seconds; |
| 100 |
if (value instanceof ValueTimestampTimeZone) { |
| 101 |
ValueTimestampTimeZone t = (ValueTimestampTimeZone) value; |
| 102 |
long timeNanos = t.getTimeNanos(); |
| 103 |
seconds = DateTimeUtils.absoluteDayFromDateValue(t.getDateValue()) * DateTimeUtils.SECONDS_PER_DAY |
| 104 |
+ timeNanos / DateTimeUtils.NANOS_PER_SECOND - t.getTimeZoneOffsetSeconds(); |
| 105 |
} else { |
| 106 |
ValueTimestamp t = (ValueTimestamp) value.convertTo(TypeInfo.TYPE_TIMESTAMP, session); |
| 107 |
long timeNanos = t.getTimeNanos(); |
| 108 |
seconds = session.currentTimeZone().getEpochSecondsFromLocal(t.getDateValue(), timeNanos); |
| 109 |
} |
| 110 |
return (int) seconds; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* See |
| 115 |
* https://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime |
| 116 |
* |
| 117 |
* @param seconds The current timestamp in seconds. |
| 118 |
* @return a formatted date/time String in the format "yyyy-MM-dd HH:mm:ss". |
| 119 |
*/ |
| 120 |
public static String fromUnixTime(int seconds) { |
| 121 |
SimpleDateFormat formatter = new SimpleDateFormat(DATE_TIME_FORMAT, |
| 122 |
Locale.ENGLISH); |
| 123 |
return formatter.format(new Date(seconds * 1_000L)); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* See |
| 128 |
* https://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime |
| 129 |
* |
| 130 |
* @param seconds The current timestamp in seconds. |
| 131 |
* @param format The format of the date/time String to return. |
| 132 |
* @return a formatted date/time String in the given format. |
| 133 |
*/ |
| 134 |
public static String fromUnixTime(int seconds, String format) { |
| 135 |
format = convertToSimpleDateFormat(format); |
| 136 |
SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.ENGLISH); |
| 137 |
return formatter.format(new Date(seconds * 1_000L)); |
| 138 |
} |
| 139 |
|
| 140 |
private static String convertToSimpleDateFormat(String format) { |
| 141 |
String[] replace = FORMAT_REPLACE; |
| 142 |
for (int i = 0; i < replace.length; i += 2) { |
| 143 |
format = StringUtils.replaceAll(format, replace[i], replace[i + 1]); |
| 144 |
} |
| 145 |
return format; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Returns mode-specific function for a given name, or {@code null}. |
| 150 |
* |
| 151 |
* @param upperName |
| 152 |
* the upper-case name of a function |
| 153 |
* @return the function with specified name or {@code null} |
| 154 |
*/ |
| 155 |
public static FunctionsMySQL getFunction(String upperName) { |
| 156 |
FunctionInfo info = FUNCTIONS.get(upperName); |
| 157 |
return info != null ? new FunctionsMySQL(info) : null; |
| 158 |
} |
| 159 |
|
| 160 |
FunctionsMySQL(FunctionInfo info) { |
| 161 |
super(info); |
| 162 |
} |
| 163 |
|
| 164 |
@Override |
| 165 |
protected void checkParameterCount(int len) { |
| 166 |
int min, max; |
| 167 |
switch (info.type) { |
| 168 |
case UNIX_TIMESTAMP: |
| 169 |
min = 0; |
| 170 |
max = 1; |
| 171 |
break; |
| 172 |
case FROM_UNIXTIME: |
| 173 |
min = 1; |
| 174 |
max = 2; |
| 175 |
break; |
| 176 |
case DATE: |
| 177 |
min = 1; |
| 178 |
max = 1; |
| 179 |
break; |
| 180 |
case LAST_INSERT_ID: |
| 181 |
min = 0; |
| 182 |
max = 1; |
| 183 |
break; |
| 184 |
default: |
| 185 |
throw DbException.getInternalError("type=" + info.type); |
| 186 |
} |
| 187 |
if (len < min || len > max) { |
| 188 |
throw DbException.get(ErrorCode.INVALID_PARAMETER_COUNT_2, info.name, min + ".." + max); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
@Override |
| 193 |
public Expression optimize(SessionLocal session) { |
| 194 |
boolean allConst = optimizeArguments(session); |
| 195 |
type = TypeInfo.getTypeInfo(info.returnDataType); |
| 196 |
if (allConst) { |
| 197 |
return ValueExpression.get(getValue(session)); |
| 198 |
} |
| 199 |
return this; |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
public Value getValue(SessionLocal session) { |
| 204 |
Value[] values = getArgumentsValues(session, args); |
| 205 |
if (values == null) { |
| 206 |
return ValueNull.INSTANCE; |
| 207 |
} |
| 208 |
Value v0 = getNullOrValue(session, args, values, 0); |
| 209 |
Value v1 = getNullOrValue(session, args, values, 1); |
| 210 |
Value result; |
| 211 |
switch (info.type) { |
| 212 |
case UNIX_TIMESTAMP: |
| 213 |
result = ValueInteger.get(unixTimestamp(session, v0 == null ? session.currentTimestamp() : v0)); |
| 214 |
break; |
| 215 |
case FROM_UNIXTIME: |
| 216 |
result = ValueVarchar.get( |
| 217 |
v1 == null ? fromUnixTime(v0.getInt()) : fromUnixTime(v0.getInt(), v1.getString())); |
| 218 |
break; |
| 219 |
case DATE: |
| 220 |
switch (v0.getValueType()) { |
| 221 |
case Value.DATE: |
| 222 |
result = v0; |
| 223 |
break; |
| 224 |
default: |
| 225 |
try { |
| 226 |
v0 = v0.convertTo(TypeInfo.TYPE_TIMESTAMP, session); |
| 227 |
} catch (DbException ex) { |
| 228 |
result = ValueNull.INSTANCE; |
| 229 |
break; |
| 230 |
} |
| 231 |
//$FALL-THROUGH$ |
| 232 |
case Value.TIMESTAMP: |
| 233 |
case Value.TIMESTAMP_TZ: |
| 234 |
result = v0.convertToDate(session); |
| 235 |
} |
| 236 |
break; |
| 237 |
case LAST_INSERT_ID: |
| 238 |
if (args.length == 0) { |
| 239 |
result = session.getLastIdentity(); |
| 240 |
if (result == ValueNull.INSTANCE) { |
| 241 |
result = ValueBigint.get(0L); |
| 242 |
} else { |
| 243 |
result = result.convertToBigint(null); |
| 244 |
} |
| 245 |
} else { |
| 246 |
result = v0; |
| 247 |
if (result == ValueNull.INSTANCE) { |
| 248 |
session.setLastIdentity(ValueNull.INSTANCE); |
| 249 |
} else { |
| 250 |
session.setLastIdentity(result = result.convertToBigint(null)); |
| 251 |
} |
| 252 |
} |
| 253 |
break; |
| 254 |
default: |
| 255 |
throw DbException.getInternalError("type=" + info.type); |
| 256 |
} |
| 257 |
return result; |
| 258 |
} |
| 259 |
|
| 260 |
} |