Debian Codegraph @

src/main/org/h2/expression/function/XMLFunction.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.api.ErrorCode;
9 import org.h2.engine.SessionLocal;
10 import org.h2.expression.Expression;
11 import org.h2.expression.TypedValueExpression;
12 import org.h2.message.DbException;
13 import org.h2.util.StringUtils;
14 import org.h2.value.TypeInfo;
15 import org.h2.value.Value;
16 import org.h2.value.ValueNull;
17 import org.h2.value.ValueVarchar;
18
19 /**
20 * An XML function.
21 */
22 public final class XMLFunction extends FunctionN {
23
24 /**
25 * XMLATTR() (non-standard).
26 */
27 public static final int XMLATTR = 0;
28
29 /**
30 * XMLCDATA() (non-standard).
31 */
32 public static final int XMLCDATA = XMLATTR + 1;
33
34 /**
35 * XMLCOMMENT() (non-standard).
36 */
37 public static final int XMLCOMMENT = XMLCDATA + 1;
38
39 /**
40 * XMLNODE() (non-standard).
41 */
42 public static final int XMLNODE = XMLCOMMENT + 1;
43
44 /**
45 * XMLSTARTDOC() (non-standard).
46 */
47 public static final int XMLSTARTDOC = XMLNODE + 1;
48
49 /**
50 * XMLTEXT() (non-standard).
51 */
52 public static final int XMLTEXT = XMLSTARTDOC + 1;
53
54 private static final String[] NAMES = { //
55 "XMLATTR", "XMLCDATA", "XMLCOMMENT", "XMLNODE", "XMLSTARTDOC", "XMLTEXT" //
56 };
57
58 private final int function;
59
60 public XMLFunction(int function) {
61 super(new Expression[4]);
62 this.function = function;
63 }
64
65 @Override
66 public Value getValue(SessionLocal session) {
67 switch (function) {
68 case XMLNODE:
69 return xmlNode(session);
70 case XMLSTARTDOC:
71 return ValueVarchar.get(StringUtils.xmlStartDoc(), session);
72 default:
73 return super.getValue(session);
74 }
75 }
76
77 private Value xmlNode(SessionLocal session) {
78 Value v1 = args[0].getValue(session);
79 if (v1 == ValueNull.INSTANCE) {
80 return ValueNull.INSTANCE;
81 }
82 int length = args.length;
83 String attr = length >= 2 ? args[1].getValue(session).getString() : null;
84 String content = length >= 3 ? args[2].getValue(session).getString() : null;
85 boolean indent;
86 if (length >= 4) {
87 Value v4 = args[3].getValue(session);
88 if (v4 == ValueNull.INSTANCE) {
89 return ValueNull.INSTANCE;
90 }
91 indent = v4.getBoolean();
92 } else {
93 indent = true;
94 }
95 return ValueVarchar.get(StringUtils.xmlNode(v1.getString(), attr, content, indent), session);
96 }
97
98 @Override
99 protected Value getValue(SessionLocal session, Value v1, Value v2, Value v3) {
100 switch (function) {
101 case XMLATTR:
102 v1 = ValueVarchar.get(StringUtils.xmlAttr(v1.getString(), v2.getString()), session);
103 break;
104 case XMLCDATA:
105 v1 = ValueVarchar.get(StringUtils.xmlCData(v1.getString()), session);
106 break;
107 case XMLCOMMENT:
108 v1 = ValueVarchar.get(StringUtils.xmlComment(v1.getString()), session);
109 break;
110 case XMLTEXT:
111 v1 = ValueVarchar.get(StringUtils.xmlText(v1.getString(), v2 != null && v2.getBoolean()), session);
112 break;
113 default:
114 throw DbException.getInternalError("function=" + function);
115 }
116 return v1;
117 }
118
119 @Override
120 public Expression optimize(SessionLocal session) {
121 boolean allConst = optimizeArguments(session, true);
122 int min, max;
123 switch (function) {
124 case XMLATTR:
125 max = min = 2;
126 break;
127 case XMLNODE:
128 min = 1;
129 max = 4;
130 break;
131 case XMLCDATA:
132 case XMLCOMMENT:
133 max = min = 1;
134 break;
135 case XMLSTARTDOC:
136 max = min = 0;
137 break;
138 case XMLTEXT:
139 min = 1;
140 max = 2;
141 break;
142 default:
143 throw DbException.getInternalError("function=" + function);
144 }
145 int len = args.length;
146 if (len < min || len > max) {
147 throw DbException.get(ErrorCode.INVALID_PARAMETER_COUNT_2, getName(), min + ".." + max);
148 }
149 type = TypeInfo.TYPE_VARCHAR;
150 if (allConst) {
151 return TypedValueExpression.getTypedIfNull(getValue(session), type);
152 }
153 return this;
154 }
155
156 @Override
157 public String getName() {
158 return NAMES[function];
159 }
160
161 }