Debian Codegraph @

src/main/org/h2/expression/Format.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;
7
8 import org.h2.engine.SessionLocal;
9 import org.h2.value.TypeInfo;
10 import org.h2.value.Value;
11 import org.h2.value.ValueJson;
12
13 /**
14 * A format clause such as FORMAT JSON.
15 */
16 public final class Format extends Operation1 {
17
18 /**
19 * Supported formats.
20 */
21 public enum FormatEnum {
22 /**
23 * JSON.
24 */
25 JSON;
26 }
27
28 private final FormatEnum format;
29
30 public Format(Expression arg, FormatEnum format) {
31 super(arg);
32 this.format = format;
33 }
34
35 @Override
36 public Value getValue(SessionLocal session) {
37 return getValue(arg.getValue(session));
38 }
39
40 /**
41 * Returns the value with applied format.
42 *
43 * @param value
44 * the value
45 * @return the value with applied format
46 */
47 public Value getValue(Value value) {
48 switch (value.getValueType()) {
49 case Value.NULL:
50 return ValueJson.NULL;
51 case Value.VARCHAR:
52 case Value.VARCHAR_IGNORECASE:
53 case Value.CHAR:
54 case Value.CLOB:
55 return ValueJson.fromJson(value.getString());
56 default:
57 return value.convertTo(TypeInfo.TYPE_JSON);
58 }
59 }
60
61 @Override
62 public Expression optimize(SessionLocal session) {
63 arg = arg.optimize(session);
64 if (arg.isConstant()) {
65 return ValueExpression.get(getValue(session));
66 }
67 if (arg instanceof Format && format == ((Format) arg).format) {
68 return arg;
69 }
70 type = TypeInfo.TYPE_JSON;
71 return this;
72 }
73
74 @Override
75 public boolean isIdentity() {
76 return arg.isIdentity();
77 }
78
79 @Override
80 public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) {
81 return arg.getSQL(builder, sqlFlags, AUTO_PARENTHESES).append(" FORMAT ").append(format.name());
82 }
83
84 @Override
85 public int getNullable() {
86 return arg.getNullable();
87 }
88
89 @Override
90 public String getTableName() {
91 return arg.getTableName();
92 }
93
94 @Override
95 public String getColumnName(SessionLocal session, int columnIndex) {
96 return arg.getColumnName(session, columnIndex);
97 }
98
99 }