Debian Codegraph @

src/main/org/h2/expression/function/CurrentDateTimeValueFunction.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.expression.function;
7
8 import org.h2.engine.SessionLocal;
9 import org.h2.expression.ExpressionVisitor;
10 import org.h2.expression.Operation0;
11 import org.h2.value.TypeInfo;
12 import org.h2.value.Value;
13 import org.h2.value.ValueTime;
14 import org.h2.value.ValueTimestamp;
15
16 /**
17 * Current datetime value function.
18 */
19 public final class CurrentDateTimeValueFunction extends Operation0 implements NamedExpression {
20
21 /**
22 * The function "CURRENT_DATE"
23 */
24 public static final int CURRENT_DATE = 0;
25
26 /**
27 * The function "CURRENT_TIME"
28 */
29 public static final int CURRENT_TIME = 1;
30
31 /**
32 * The function "LOCALTIME"
33 */
34 public static final int LOCALTIME = 2;
35
36 /**
37 * The function "CURRENT_TIMESTAMP"
38 */
39 public static final int CURRENT_TIMESTAMP = 3;
40
41 /**
42 * The function "LOCALTIMESTAMP"
43 */
44 public static final int LOCALTIMESTAMP = 4;
45
46 private static final int[] TYPES = { Value.DATE, Value.TIME_TZ, Value.TIME, Value.TIMESTAMP_TZ, Value.TIMESTAMP };
47
48 private static final String[] NAMES = { "CURRENT_DATE", "CURRENT_TIME", "LOCALTIME", "CURRENT_TIMESTAMP",
49 "LOCALTIMESTAMP" };
50
51 /**
52 * Get the name for this function id.
53 *
54 * @param function the function id
55 * @return the name
56 */
57 public static String getName(int function) {
58 return NAMES[function];
59 }
60
61 private final int function, scale;
62
63 private final TypeInfo type;
64
65 public CurrentDateTimeValueFunction(int function, int scale) {
66 this.function = function;
67 this.scale = scale;
68 if (scale < 0) {
69 scale = function >= CURRENT_TIMESTAMP ? ValueTimestamp.DEFAULT_SCALE : ValueTime.DEFAULT_SCALE;
70 }
71 type = TypeInfo.getTypeInfo(TYPES[function], 0L, scale, null);
72 }
73
74 @Override
75 public Value getValue(SessionLocal session) {
76 return session.currentTimestamp().castTo(type, session);
77 }
78
79 @Override
80 public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) {
81 builder.append(getName());
82 if (scale >= 0) {
83 builder.append('(').append(scale).append(')');
84 }
85 return builder;
86 }
87
88 @Override
89 public boolean isEverything(ExpressionVisitor visitor) {
90 switch (visitor.getType()) {
91 case ExpressionVisitor.DETERMINISTIC:
92 return false;
93 }
94 return true;
95 }
96
97 @Override
98 public TypeInfo getType() {
99 return type;
100 }
101
102 @Override
103 public int getCost() {
104 return 1;
105 }
106
107 @Override
108 public String getName() {
109 return NAMES[function];
110 }
111
112 }