| 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.expression.function; |
| 7 |
|
| 8 |
import java.text.DateFormatSymbols; |
| 9 |
import java.util.Locale; |
| 10 |
|
| 11 |
import org.h2.engine.SessionLocal; |
| 12 |
import org.h2.expression.Expression; |
| 13 |
import org.h2.expression.TypedValueExpression; |
| 14 |
import org.h2.message.DbException; |
| 15 |
import org.h2.util.DateTimeUtils; |
| 16 |
import org.h2.value.TypeInfo; |
| 17 |
import org.h2.value.Value; |
| 18 |
import org.h2.value.ValueNull; |
| 19 |
import org.h2.value.ValueVarchar; |
| 20 |
|
| 21 |
/** |
| 22 |
* A DAYNAME() or MONTHNAME() function. |
| 23 |
*/ |
| 24 |
public final class DayMonthNameFunction extends Function1 { |
| 25 |
|
| 26 |
/** |
| 27 |
* DAYNAME() (non-standard). |
| 28 |
*/ |
| 29 |
public static final int DAYNAME = 0; |
| 30 |
|
| 31 |
/** |
| 32 |
* MONTHNAME() (non-standard). |
| 33 |
*/ |
| 34 |
public static final int MONTHNAME = DAYNAME + 1; |
| 35 |
|
| 36 |
private static final String[] NAMES = { // |
| 37 |
"DAYNAME", "MONTHNAME" // |
| 38 |
}; |
| 39 |
|
| 40 |
/** |
| 41 |
* English names of months and week days. |
| 42 |
*/ |
| 43 |
private static volatile String[][] MONTHS_AND_WEEKS; |
| 44 |
|
| 45 |
private final int function; |
| 46 |
|
| 47 |
public DayMonthNameFunction(Expression arg, int function) { |
| 48 |
super(arg); |
| 49 |
this.function = function; |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public Value getValue(SessionLocal session) { |
| 54 |
Value v = arg.getValue(session); |
| 55 |
if (v == ValueNull.INSTANCE) { |
| 56 |
return ValueNull.INSTANCE; |
| 57 |
} |
| 58 |
long dateValue = DateTimeUtils.dateAndTimeFromValue(v, session)[0]; |
| 59 |
String result; |
| 60 |
switch (function) { |
| 61 |
case DAYNAME: |
| 62 |
result = getMonthsAndWeeks(1)[DateTimeUtils.getDayOfWeek(dateValue, 0)]; |
| 63 |
break; |
| 64 |
case MONTHNAME: |
| 65 |
result = getMonthsAndWeeks(0)[DateTimeUtils.monthFromDateValue(dateValue) - 1]; |
| 66 |
break; |
| 67 |
default: |
| 68 |
throw DbException.getInternalError("function=" + function); |
| 69 |
} |
| 70 |
return ValueVarchar.get(result, session); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Return names of month or weeks. |
| 75 |
* |
| 76 |
* @param field |
| 77 |
* 0 for months, 1 for weekdays |
| 78 |
* @return names of month or weeks |
| 79 |
*/ |
| 80 |
private static String[] getMonthsAndWeeks(int field) { |
| 81 |
String[][] result = MONTHS_AND_WEEKS; |
| 82 |
if (result == null) { |
| 83 |
result = new String[2][]; |
| 84 |
DateFormatSymbols dfs = DateFormatSymbols.getInstance(Locale.ENGLISH); |
| 85 |
result[0] = dfs.getMonths(); |
| 86 |
result[1] = dfs.getWeekdays(); |
| 87 |
MONTHS_AND_WEEKS = result; |
| 88 |
} |
| 89 |
return result[field]; |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public Expression optimize(SessionLocal session) { |
| 94 |
arg = arg.optimize(session); |
| 95 |
type = TypeInfo.getTypeInfo(Value.VARCHAR, 20, 0, null); |
| 96 |
if (arg.isConstant()) { |
| 97 |
return TypedValueExpression.getTypedIfNull(getValue(session), type); |
| 98 |
} |
| 99 |
return this; |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public String getName() { |
| 104 |
return NAMES[function]; |
| 105 |
} |
| 106 |
|
| 107 |
} |