| 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.io.ByteArrayOutputStream; |
| 9 |
|
| 10 |
import org.h2.engine.SessionLocal; |
| 11 |
import org.h2.expression.Expression; |
| 12 |
import org.h2.expression.ExpressionWithFlags; |
| 13 |
import org.h2.expression.Format; |
| 14 |
import org.h2.expression.OperationN; |
| 15 |
import org.h2.expression.Subquery; |
| 16 |
import org.h2.expression.TypedValueExpression; |
| 17 |
import org.h2.message.DbException; |
| 18 |
import org.h2.util.json.JsonConstructorUtils; |
| 19 |
import org.h2.value.TypeInfo; |
| 20 |
import org.h2.value.Value; |
| 21 |
import org.h2.value.ValueJson; |
| 22 |
import org.h2.value.ValueNull; |
| 23 |
|
| 24 |
/** |
| 25 |
* JSON constructor function. |
| 26 |
*/ |
| 27 |
public final class JsonConstructorFunction extends OperationN implements ExpressionWithFlags, NamedExpression { |
| 28 |
|
| 29 |
private final boolean array; |
| 30 |
|
| 31 |
private int flags; |
| 32 |
|
| 33 |
/** |
| 34 |
* Creates a new instance of JSON constructor function. |
| 35 |
* |
| 36 |
* @param array |
| 37 |
* {@code false} for {@code JSON_OBJECT}, {@code true} for |
| 38 |
* {@code JSON_ARRAY}. |
| 39 |
*/ |
| 40 |
public JsonConstructorFunction(boolean array) { |
| 41 |
super(new Expression[4]); |
| 42 |
this.array = array; |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
public void setFlags(int flags) { |
| 47 |
this.flags = flags; |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public int getFlags() { |
| 52 |
return flags; |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public Value getValue(SessionLocal session) { |
| 57 |
return array ? jsonArray(session, args) : jsonObject(session, args); |
| 58 |
} |
| 59 |
|
| 60 |
private Value jsonObject(SessionLocal session, Expression[] args) { |
| 61 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 62 |
baos.write('{'); |
| 63 |
for (int i = 0, l = args.length; i < l;) { |
| 64 |
String name = args[i++].getValue(session).getString(); |
| 65 |
if (name == null) { |
| 66 |
throw DbException.getInvalidValueException("JSON_OBJECT key", "NULL"); |
| 67 |
} |
| 68 |
Value value = args[i++].getValue(session); |
| 69 |
if (value == ValueNull.INSTANCE) { |
| 70 |
if ((flags & JsonConstructorUtils.JSON_ABSENT_ON_NULL) != 0) { |
| 71 |
continue; |
| 72 |
} else { |
| 73 |
value = ValueJson.NULL; |
| 74 |
} |
| 75 |
} |
| 76 |
JsonConstructorUtils.jsonObjectAppend(baos, name, value); |
| 77 |
} |
| 78 |
return JsonConstructorUtils.jsonObjectFinish(baos, flags); |
| 79 |
} |
| 80 |
|
| 81 |
private Value jsonArray(SessionLocal session, Expression[] args) { |
| 82 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 83 |
baos.write('['); |
| 84 |
int l = args.length; |
| 85 |
evaluate: { |
| 86 |
if (l == 1) { |
| 87 |
Expression arg0 = args[0]; |
| 88 |
if (arg0 instanceof Subquery) { |
| 89 |
Subquery q = (Subquery) arg0; |
| 90 |
for (Value value : q.getAllRows(session)) { |
| 91 |
JsonConstructorUtils.jsonArrayAppend(baos, value, flags); |
| 92 |
} |
| 93 |
break evaluate; |
| 94 |
} else if (arg0 instanceof Format) { |
| 95 |
Format format = (Format) arg0; |
| 96 |
arg0 = format.getSubexpression(0); |
| 97 |
if (arg0 instanceof Subquery) { |
| 98 |
Subquery q = (Subquery) arg0; |
| 99 |
for (Value value : q.getAllRows(session)) { |
| 100 |
JsonConstructorUtils.jsonArrayAppend(baos, format.getValue(value), flags); |
| 101 |
} |
| 102 |
break evaluate; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
for (int i = 0; i < l;) { |
| 107 |
JsonConstructorUtils.jsonArrayAppend(baos, args[i++].getValue(session), flags); |
| 108 |
} |
| 109 |
} |
| 110 |
baos.write(']'); |
| 111 |
return ValueJson.getInternal(baos.toByteArray()); |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public Expression optimize(SessionLocal session) { |
| 116 |
boolean allConst = optimizeArguments(session, true); |
| 117 |
type = TypeInfo.TYPE_JSON; |
| 118 |
if (allConst) { |
| 119 |
return TypedValueExpression.getTypedIfNull(getValue(session), type); |
| 120 |
} |
| 121 |
return this; |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 126 |
builder.append(getName()).append('('); |
| 127 |
if (array) { |
| 128 |
writeExpressions(builder, args, sqlFlags); |
| 129 |
} else { |
| 130 |
for (int i = 0, l = args.length; i < l;) { |
| 131 |
if (i > 0) { |
| 132 |
builder.append(", "); |
| 133 |
} |
| 134 |
args[i++].getUnenclosedSQL(builder, sqlFlags).append(": "); |
| 135 |
args[i++].getUnenclosedSQL(builder, sqlFlags); |
| 136 |
} |
| 137 |
} |
| 138 |
return getJsonFunctionFlagsSQL(builder, flags, array).append(')'); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Appends flags of a JSON function to the specified string builder. |
| 143 |
* |
| 144 |
* @param builder |
| 145 |
* string builder to append to |
| 146 |
* @param flags |
| 147 |
* flags to append |
| 148 |
* @param forArray |
| 149 |
* whether the function is an array function |
| 150 |
* @return the specified string builder |
| 151 |
*/ |
| 152 |
public static StringBuilder getJsonFunctionFlagsSQL(StringBuilder builder, int flags, boolean forArray) { |
| 153 |
if ((flags & JsonConstructorUtils.JSON_ABSENT_ON_NULL) != 0) { |
| 154 |
if (!forArray) { |
| 155 |
builder.append(" ABSENT ON NULL"); |
| 156 |
} |
| 157 |
} else if (forArray) { |
| 158 |
builder.append(" NULL ON NULL"); |
| 159 |
} |
| 160 |
if (!forArray && (flags & JsonConstructorUtils.JSON_WITH_UNIQUE_KEYS) != 0) { |
| 161 |
builder.append(" WITH UNIQUE KEYS"); |
| 162 |
} |
| 163 |
return builder; |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public String getName() { |
| 168 |
return array ? "JSON_ARRAY" : "JSON_OBJECT"; |
| 169 |
} |
| 170 |
|
| 171 |
} |