| 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.value; |
| 7 |
|
| 8 |
import org.h2.api.ErrorCode; |
| 9 |
import org.h2.engine.CastDataProvider; |
| 10 |
import org.h2.engine.Constants; |
| 11 |
import org.h2.message.DbException; |
| 12 |
import org.h2.result.SimpleResult; |
| 13 |
|
| 14 |
/** |
| 15 |
* Row value. |
| 16 |
*/ |
| 17 |
public final class ValueRow extends ValueCollectionBase { |
| 18 |
|
| 19 |
/** |
| 20 |
* Empty row. |
| 21 |
*/ |
| 22 |
public static final ValueRow EMPTY = get(Value.EMPTY_VALUES); |
| 23 |
|
| 24 |
private TypeInfo type; |
| 25 |
|
| 26 |
private ValueRow(TypeInfo type, Value[] list) { |
| 27 |
super(list); |
| 28 |
int degree = list.length; |
| 29 |
if (degree > Constants.MAX_COLUMNS) { |
| 30 |
throw DbException.get(ErrorCode.TOO_MANY_COLUMNS_1, "" + Constants.MAX_COLUMNS); |
| 31 |
} |
| 32 |
if (type != null) { |
| 33 |
if (type.getValueType() != ROW || ((ExtTypeInfoRow) type.getExtTypeInfo()).getFields().size() != degree) { |
| 34 |
throw DbException.getInternalError(); |
| 35 |
} |
| 36 |
this.type = type; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Get or create a row value for the given value array. |
| 42 |
* Do not clone the data. |
| 43 |
* |
| 44 |
* @param list the value array |
| 45 |
* @return the value |
| 46 |
*/ |
| 47 |
public static ValueRow get(Value[] list) { |
| 48 |
return new ValueRow(null, list); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get or create a typed row value for the given value array. |
| 53 |
* Do not clone the data. |
| 54 |
* |
| 55 |
* @param extTypeInfo the extended data type information |
| 56 |
* @param list the value array |
| 57 |
* @return the value |
| 58 |
*/ |
| 59 |
public static ValueRow get(ExtTypeInfoRow extTypeInfo, Value[] list) { |
| 60 |
return new ValueRow(new TypeInfo(ROW, -1, -1, extTypeInfo), list); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Get or create a typed row value for the given value array. |
| 65 |
* Do not clone the data. |
| 66 |
* |
| 67 |
* @param typeInfo the data type information |
| 68 |
* @param list the value array |
| 69 |
* @return the value |
| 70 |
*/ |
| 71 |
public static ValueRow get(TypeInfo typeInfo, Value[] list) { |
| 72 |
return new ValueRow(typeInfo, list); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public TypeInfo getType() { |
| 77 |
TypeInfo type = this.type; |
| 78 |
if (type == null) { |
| 79 |
this.type = type = TypeInfo.getTypeInfo(Value.ROW, 0, 0, new ExtTypeInfoRow(values)); |
| 80 |
} |
| 81 |
return type; |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public int getValueType() { |
| 86 |
return ROW; |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public String getString() { |
| 91 |
StringBuilder builder = new StringBuilder("ROW ("); |
| 92 |
for (int i = 0; i < values.length; i++) { |
| 93 |
if (i > 0) { |
| 94 |
builder.append(", "); |
| 95 |
} |
| 96 |
builder.append(values[i].getString()); |
| 97 |
} |
| 98 |
return builder.append(')').toString(); |
| 99 |
} |
| 100 |
|
| 101 |
public SimpleResult getResult() { |
| 102 |
SimpleResult result = new SimpleResult(); |
| 103 |
for (int i = 0, l = values.length; i < l;) { |
| 104 |
Value v = values[i++]; |
| 105 |
result.addColumn("C" + i, v.getType()); |
| 106 |
} |
| 107 |
result.addRow(values); |
| 108 |
return result; |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public int compareTypeSafe(Value o, CompareMode mode, CastDataProvider provider) { |
| 113 |
ValueRow v = (ValueRow) o; |
| 114 |
if (values == v.values) { |
| 115 |
return 0; |
| 116 |
} |
| 117 |
int len = values.length; |
| 118 |
if (len != v.values.length) { |
| 119 |
throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH); |
| 120 |
} |
| 121 |
for (int i = 0; i < len; i++) { |
| 122 |
Value v1 = values[i]; |
| 123 |
Value v2 = v.values[i]; |
| 124 |
int comp = v1.compareTo(v2, provider, mode); |
| 125 |
if (comp != 0) { |
| 126 |
return comp; |
| 127 |
} |
| 128 |
} |
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public StringBuilder getSQL(StringBuilder builder, int sqlFlags) { |
| 134 |
builder.append("ROW ("); |
| 135 |
int length = values.length; |
| 136 |
for (int i = 0; i < length; i++) { |
| 137 |
if (i > 0) { |
| 138 |
builder.append(", "); |
| 139 |
} |
| 140 |
values[i].getSQL(builder, sqlFlags); |
| 141 |
} |
| 142 |
return builder.append(')'); |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public boolean equals(Object other) { |
| 147 |
if (!(other instanceof ValueRow)) { |
| 148 |
return false; |
| 149 |
} |
| 150 |
ValueRow v = (ValueRow) other; |
| 151 |
if (values == v.values) { |
| 152 |
return true; |
| 153 |
} |
| 154 |
int len = values.length; |
| 155 |
if (len != v.values.length) { |
| 156 |
return false; |
| 157 |
} |
| 158 |
for (int i = 0; i < len; i++) { |
| 159 |
if (!values[i].equals(v.values[i])) { |
| 160 |
return false; |
| 161 |
} |
| 162 |
} |
| 163 |
return true; |
| 164 |
} |
| 165 |
|
| 166 |
} |