| 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.command.dml; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
import java.util.Arrays; |
| 10 |
|
| 11 |
import org.h2.api.ErrorCode; |
| 12 |
import org.h2.engine.Constants; |
| 13 |
import org.h2.engine.SessionLocal; |
| 14 |
import org.h2.expression.Expression; |
| 15 |
import org.h2.expression.ExpressionList; |
| 16 |
import org.h2.expression.ExpressionVisitor; |
| 17 |
import org.h2.expression.Parameter; |
| 18 |
import org.h2.expression.ValueExpression; |
| 19 |
import org.h2.message.DbException; |
| 20 |
import org.h2.result.LocalResult; |
| 21 |
import org.h2.result.ResultTarget; |
| 22 |
import org.h2.result.Row; |
| 23 |
import org.h2.table.Column; |
| 24 |
import org.h2.table.ColumnResolver; |
| 25 |
import org.h2.table.DataChangeDeltaTable.ResultOption; |
| 26 |
import org.h2.table.Table; |
| 27 |
import org.h2.util.HasSQL; |
| 28 |
import org.h2.value.Value; |
| 29 |
import org.h2.value.ValueArray; |
| 30 |
import org.h2.value.ValueNull; |
| 31 |
|
| 32 |
/** |
| 33 |
* Set clause list. |
| 34 |
*/ |
| 35 |
public final class SetClauseList implements HasSQL { |
| 36 |
|
| 37 |
private final Table table; |
| 38 |
|
| 39 |
private final UpdateAction[] actions; |
| 40 |
|
| 41 |
private boolean onUpdate; |
| 42 |
|
| 43 |
public SetClauseList(Table table) { |
| 44 |
this.table = table; |
| 45 |
actions = new UpdateAction[table.getColumns().length]; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Add a single column. |
| 50 |
* |
| 51 |
* @param column the column |
| 52 |
* @param arrayIndexes |
| 53 |
* non-empty array of indexes for array element assignment, or |
| 54 |
* {@code null} for simple assignment |
| 55 |
* @param expression the expression |
| 56 |
*/ |
| 57 |
public void addSingle(Column column, Expression[] arrayIndexes, Expression expression) { |
| 58 |
int id = column.getColumnId(); |
| 59 |
if (actions[id] != null) { |
| 60 |
throw DbException.get(ErrorCode.DUPLICATE_COLUMN_NAME_1, column.getName()); |
| 61 |
} |
| 62 |
if (expression != ValueExpression.DEFAULT) { |
| 63 |
actions[id] = new SetSimple(arrayIndexes, expression); |
| 64 |
if (expression instanceof Parameter) { |
| 65 |
((Parameter) expression).setColumn(column); |
| 66 |
} |
| 67 |
} else { |
| 68 |
actions[id] = SetClauseList.UpdateAction.SET_DEFAULT; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Add multiple columns. |
| 74 |
* |
| 75 |
* @param columns the columns |
| 76 |
* @param allIndexes |
| 77 |
* list of non-empty arrays of indexes for array element |
| 78 |
* assignments, or {@code null} values for simple assignments |
| 79 |
* @param expression the expression (e.g. an expression list) |
| 80 |
*/ |
| 81 |
public void addMultiple(ArrayList<Column> columns, ArrayList<Expression[]> allIndexes, Expression expression) { |
| 82 |
int columnCount = columns.size(); |
| 83 |
if (expression instanceof ExpressionList) { |
| 84 |
ExpressionList expressions = (ExpressionList) expression; |
| 85 |
if (!expressions.isArray()) { |
| 86 |
if (columnCount != expressions.getSubexpressionCount()) { |
| 87 |
throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH); |
| 88 |
} |
| 89 |
for (int i = 0; i < columnCount; i++) { |
| 90 |
addSingle(columns.get(i), allIndexes.get(i), expressions.getSubexpression(i)); |
| 91 |
} |
| 92 |
return; |
| 93 |
} |
| 94 |
} |
| 95 |
if (columnCount == 1) { |
| 96 |
// Row value special case |
| 97 |
addSingle(columns.get(0), allIndexes.get(0), expression); |
| 98 |
} else { |
| 99 |
int[] cols = new int[columnCount]; |
| 100 |
RowExpression row = new RowExpression(expression, cols); |
| 101 |
int minId = table.getColumns().length - 1, maxId = 0; |
| 102 |
for (int i = 0; i < columnCount; i++) { |
| 103 |
int id = columns.get(i).getColumnId(); |
| 104 |
if (id < minId) { |
| 105 |
minId = id; |
| 106 |
} |
| 107 |
if (id > maxId) { |
| 108 |
maxId = id; |
| 109 |
} |
| 110 |
} |
| 111 |
for (int i = 0; i < columnCount; i++) { |
| 112 |
Column column = columns.get(i); |
| 113 |
int id = column.getColumnId(); |
| 114 |
cols[i] = id; |
| 115 |
if (actions[id] != null) { |
| 116 |
throw DbException.get(ErrorCode.DUPLICATE_COLUMN_NAME_1, column.getName()); |
| 117 |
} |
| 118 |
actions[id] = new SetMultiple(allIndexes.get(i), row, i, id == minId, id == maxId); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
boolean prepareUpdate(Table table, SessionLocal session, ResultTarget deltaChangeCollector, |
| 124 |
ResultOption deltaChangeCollectionMode, LocalResult rows, Row oldRow, |
| 125 |
boolean updateToCurrentValuesReturnsZero) { |
| 126 |
Column[] columns = table.getColumns(); |
| 127 |
int columnCount = columns.length; |
| 128 |
Row newRow = table.getTemplateRow(); |
| 129 |
for (int i = 0; i < columnCount; i++) { |
| 130 |
UpdateAction action = actions[i]; |
| 131 |
Column column = columns[i]; |
| 132 |
Value oldValue = oldRow.getValue(i), newValue; |
| 133 |
if (action == null || action == UpdateAction.ON_UPDATE) { |
| 134 |
newValue = column.isGenerated() ? null : oldValue; |
| 135 |
} else if (action == UpdateAction.SET_DEFAULT) { |
| 136 |
newValue = !column.isIdentity() ? null : oldValue; |
| 137 |
} else { |
| 138 |
newValue = action.update(session, oldValue); |
| 139 |
if (newValue == ValueNull.INSTANCE && column.isDefaultOnNull()) { |
| 140 |
newValue = !column.isIdentity() ? null : oldValue; |
| 141 |
} else if (column.isGeneratedAlways()) { |
| 142 |
throw DbException.get(ErrorCode.GENERATED_COLUMN_CANNOT_BE_ASSIGNED_1, |
| 143 |
column.getSQLWithTable(new StringBuilder(), TRACE_SQL_FLAGS).toString()); |
| 144 |
} |
| 145 |
} |
| 146 |
newRow.setValue(i, newValue); |
| 147 |
} |
| 148 |
newRow.setKey(oldRow.getKey()); |
| 149 |
table.convertUpdateRow(session, newRow, false); |
| 150 |
boolean result = true; |
| 151 |
if (onUpdate) { |
| 152 |
if (!oldRow.hasSameValues(newRow)) { |
| 153 |
for (int i = 0; i < columnCount; i++) { |
| 154 |
if (actions[i] == UpdateAction.ON_UPDATE) { |
| 155 |
newRow.setValue(i, columns[i].getEffectiveOnUpdateExpression().getValue(session)); |
| 156 |
} else if (columns[i].isGenerated()) { |
| 157 |
newRow.setValue(i, null); |
| 158 |
} |
| 159 |
} |
| 160 |
// Convert on update expressions and reevaluate |
| 161 |
// generated columns |
| 162 |
table.convertUpdateRow(session, newRow, false); |
| 163 |
} else if (updateToCurrentValuesReturnsZero) { |
| 164 |
result = false; |
| 165 |
} |
| 166 |
} else if (updateToCurrentValuesReturnsZero && oldRow.hasSameValues(newRow)) { |
| 167 |
result = false; |
| 168 |
} |
| 169 |
if (deltaChangeCollectionMode == ResultOption.OLD) { |
| 170 |
deltaChangeCollector.addRow(oldRow.getValueList()); |
| 171 |
} else if (deltaChangeCollectionMode == ResultOption.NEW) { |
| 172 |
deltaChangeCollector.addRow(newRow.getValueList().clone()); |
| 173 |
} |
| 174 |
if (!table.fireRow() || !table.fireBeforeRow(session, oldRow, newRow)) { |
| 175 |
rows.addRowForTable(oldRow); |
| 176 |
rows.addRowForTable(newRow); |
| 177 |
} |
| 178 |
if (deltaChangeCollectionMode == ResultOption.FINAL) { |
| 179 |
deltaChangeCollector.addRow(newRow.getValueList()); |
| 180 |
} |
| 181 |
return result; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Check if this expression and all sub-expressions can fulfill a criteria. |
| 186 |
* If any part returns false, the result is false. |
| 187 |
* |
| 188 |
* @param visitor |
| 189 |
* the visitor |
| 190 |
* @return if the criteria can be fulfilled |
| 191 |
*/ |
| 192 |
boolean isEverything(ExpressionVisitor visitor) { |
| 193 |
for (UpdateAction action : actions) { |
| 194 |
if (action != null) { |
| 195 |
if (!action.isEverything(visitor)) { |
| 196 |
return false; |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
return true; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Map the columns and optimize expressions. |
| 205 |
* |
| 206 |
* @param session |
| 207 |
* the session |
| 208 |
* @param resolver1 |
| 209 |
* the first column resolver |
| 210 |
* @param resolver2 |
| 211 |
* the second column resolver, or {@code null} |
| 212 |
*/ |
| 213 |
void mapAndOptimize(SessionLocal session, ColumnResolver resolver1, ColumnResolver resolver2) { |
| 214 |
Column[] columns = table.getColumns(); |
| 215 |
boolean onUpdate = false; |
| 216 |
for (int i = 0; i < actions.length; i++) { |
| 217 |
UpdateAction action = actions[i]; |
| 218 |
if (action != null) { |
| 219 |
action.mapAndOptimize(session, resolver1, resolver2); |
| 220 |
} else { |
| 221 |
Column column = columns[i]; |
| 222 |
if (column.getEffectiveOnUpdateExpression() != null) { |
| 223 |
actions[i] = UpdateAction.ON_UPDATE; |
| 224 |
onUpdate = true; |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
this.onUpdate = onUpdate; |
| 229 |
} |
| 230 |
|
| 231 |
@Override |
| 232 |
public StringBuilder getSQL(StringBuilder builder, int sqlFlags) { |
| 233 |
Column[] columns = table.getColumns(); |
| 234 |
builder.append("\nSET\n "); |
| 235 |
boolean f = false; |
| 236 |
for (int i = 0; i < actions.length; i++) { |
| 237 |
UpdateAction action = actions[i]; |
| 238 |
if (action != null && action != UpdateAction.ON_UPDATE) { |
| 239 |
if (action.getClass() == SetMultiple.class) { |
| 240 |
SetMultiple multiple = (SetMultiple) action; |
| 241 |
if (multiple.first) { |
| 242 |
if (f) { |
| 243 |
builder.append(",\n "); |
| 244 |
} |
| 245 |
f = true; |
| 246 |
RowExpression r = multiple.row; |
| 247 |
builder.append('('); |
| 248 |
int[] cols = r.columns; |
| 249 |
for (int j = 0, l = cols.length; j < l; j++) { |
| 250 |
if (j > 0) { |
| 251 |
builder.append(", "); |
| 252 |
} |
| 253 |
columns[cols[j]].getSQL(builder, sqlFlags); |
| 254 |
} |
| 255 |
r.expression.getUnenclosedSQL(builder.append(") = "), sqlFlags); |
| 256 |
} |
| 257 |
} else { |
| 258 |
if (f) { |
| 259 |
builder.append(",\n "); |
| 260 |
} |
| 261 |
f = true; |
| 262 |
Column column = columns[i]; |
| 263 |
if (action != UpdateAction.SET_DEFAULT) { |
| 264 |
action.getSQL(builder, sqlFlags, column); |
| 265 |
} else { |
| 266 |
column.getSQL(builder, sqlFlags).append(" = DEFAULT"); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
return builder; |
| 272 |
} |
| 273 |
|
| 274 |
private static class UpdateAction { |
| 275 |
|
| 276 |
static UpdateAction ON_UPDATE = new UpdateAction(); |
| 277 |
|
| 278 |
static UpdateAction SET_DEFAULT = new UpdateAction(); |
| 279 |
|
| 280 |
UpdateAction() { |
| 281 |
} |
| 282 |
|
| 283 |
Value update(SessionLocal session, Value oldValue) { |
| 284 |
throw DbException.getInternalError(); |
| 285 |
} |
| 286 |
|
| 287 |
boolean isEverything(ExpressionVisitor visitor) { |
| 288 |
return true; |
| 289 |
} |
| 290 |
|
| 291 |
void mapAndOptimize(SessionLocal session, ColumnResolver resolver1, ColumnResolver resolver2) { |
| 292 |
// Do nothing |
| 293 |
} |
| 294 |
|
| 295 |
void getSQL(StringBuilder builder, int sqlFlags, Column column) { |
| 296 |
throw DbException.getInternalError(); |
| 297 |
} |
| 298 |
|
| 299 |
} |
| 300 |
|
| 301 |
private static abstract class SetAction extends UpdateAction { |
| 302 |
|
| 303 |
private final Expression[] arrayIndexes; |
| 304 |
|
| 305 |
SetAction(Expression[] arrayIndexes) { |
| 306 |
this.arrayIndexes = arrayIndexes; |
| 307 |
} |
| 308 |
|
| 309 |
@Override |
| 310 |
boolean isEverything(ExpressionVisitor visitor) { |
| 311 |
if (arrayIndexes != null) { |
| 312 |
for (Expression e : arrayIndexes) { |
| 313 |
if (!e.isEverything(visitor)) { |
| 314 |
return false; |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
return true; |
| 319 |
} |
| 320 |
|
| 321 |
@Override |
| 322 |
void mapAndOptimize(SessionLocal session, ColumnResolver resolver1, ColumnResolver resolver2) { |
| 323 |
if (arrayIndexes != null) { |
| 324 |
for (int i = 0, l = arrayIndexes.length; i < l; i++) { |
| 325 |
Expression e = arrayIndexes[i]; |
| 326 |
e.mapColumns(resolver1, 0, Expression.MAP_INITIAL); |
| 327 |
if (resolver2 != null) { |
| 328 |
e.mapColumns(resolver2, 0, Expression.MAP_INITIAL); |
| 329 |
} |
| 330 |
arrayIndexes[i] = e.optimize(session); |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
@Override |
| 336 |
final Value update(SessionLocal session, Value oldValue) { |
| 337 |
Value newValue = update(session); |
| 338 |
if (arrayIndexes != null) { |
| 339 |
newValue = updateArray(session, oldValue, newValue, 0); |
| 340 |
} |
| 341 |
return newValue; |
| 342 |
} |
| 343 |
|
| 344 |
private Value updateArray(SessionLocal session, Value oldValue, Value newValue, int indexNumber) { |
| 345 |
int index = arrayIndexes[indexNumber++].getValue(session).getInt(); |
| 346 |
int cardinality = Constants.MAX_ARRAY_CARDINALITY; |
| 347 |
if (index < 0 || index > cardinality) { |
| 348 |
throw DbException.get(ErrorCode.ARRAY_ELEMENT_ERROR_2, Integer.toString(index), |
| 349 |
"1.." + cardinality); |
| 350 |
} |
| 351 |
Value[] values; |
| 352 |
if (oldValue == null) { |
| 353 |
values = new Value[index]; |
| 354 |
for (int i = 0; i < index - 1; i++) { |
| 355 |
values[i] = ValueNull.INSTANCE; |
| 356 |
} |
| 357 |
} else if (oldValue == ValueNull.INSTANCE) { |
| 358 |
throw DbException.get(ErrorCode.NULL_VALUE_IN_ARRAY_TARGET); |
| 359 |
} else if (oldValue.getValueType() != Value.ARRAY) { |
| 360 |
throw DbException.get(ErrorCode.ARRAY_ELEMENT_ERROR_2, oldValue.getType().getTraceSQL(), |
| 361 |
"ARRAY"); |
| 362 |
} else { |
| 363 |
values = ((ValueArray) oldValue).getList(); |
| 364 |
int length = values.length; |
| 365 |
if (index <= length) { |
| 366 |
values = values.clone(); |
| 367 |
} else { |
| 368 |
values = Arrays.copyOf(values, index); |
| 369 |
for (int i = length; i < index - 1; i++) { |
| 370 |
values[i] = ValueNull.INSTANCE; |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
values[index - 1] = indexNumber == arrayIndexes.length ? newValue |
| 375 |
: updateArray(session, values[index - 1], newValue, indexNumber); |
| 376 |
return ValueArray.get(values, session); |
| 377 |
} |
| 378 |
|
| 379 |
abstract Value update(SessionLocal session); |
| 380 |
|
| 381 |
} |
| 382 |
|
| 383 |
private static final class SetSimple extends SetAction { |
| 384 |
|
| 385 |
private Expression expression; |
| 386 |
|
| 387 |
SetSimple(Expression[] arrayIndexes, Expression expression) { |
| 388 |
super(arrayIndexes); |
| 389 |
this.expression = expression; |
| 390 |
} |
| 391 |
|
| 392 |
@Override |
| 393 |
Value update(SessionLocal session) { |
| 394 |
return expression.getValue(session); |
| 395 |
} |
| 396 |
|
| 397 |
@Override |
| 398 |
boolean isEverything(ExpressionVisitor visitor) { |
| 399 |
return super.isEverything(visitor) && expression.isEverything(visitor); |
| 400 |
} |
| 401 |
|
| 402 |
@Override |
| 403 |
void mapAndOptimize(SessionLocal session, ColumnResolver resolver1, ColumnResolver resolver2) { |
| 404 |
super.mapAndOptimize(session, resolver1, resolver2); |
| 405 |
expression.mapColumns(resolver1, 0, Expression.MAP_INITIAL); |
| 406 |
if (resolver2 != null) { |
| 407 |
expression.mapColumns(resolver2, 0, Expression.MAP_INITIAL); |
| 408 |
} |
| 409 |
expression = expression.optimize(session); |
| 410 |
} |
| 411 |
|
| 412 |
@Override |
| 413 |
void getSQL(StringBuilder builder, int sqlFlags, Column column) { |
| 414 |
expression.getUnenclosedSQL(column.getSQL(builder, sqlFlags).append(" = "), sqlFlags); |
| 415 |
} |
| 416 |
|
| 417 |
} |
| 418 |
|
| 419 |
private static final class RowExpression { |
| 420 |
|
| 421 |
Expression expression; |
| 422 |
|
| 423 |
final int[] columns; |
| 424 |
|
| 425 |
Value[] values; |
| 426 |
|
| 427 |
RowExpression(Expression expression, int[] columns) { |
| 428 |
this.expression = expression; |
| 429 |
this.columns = columns; |
| 430 |
} |
| 431 |
|
| 432 |
boolean isEverything(ExpressionVisitor visitor) { |
| 433 |
return expression.isEverything(visitor); |
| 434 |
} |
| 435 |
|
| 436 |
void mapAndOptimize(SessionLocal session, ColumnResolver resolver1, ColumnResolver resolver2) { |
| 437 |
expression.mapColumns(resolver1, 0, Expression.MAP_INITIAL); |
| 438 |
if (resolver2 != null) { |
| 439 |
expression.mapColumns(resolver2, 0, Expression.MAP_INITIAL); |
| 440 |
} |
| 441 |
expression = expression.optimize(session); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
private static final class SetMultiple extends SetAction { |
| 446 |
|
| 447 |
final RowExpression row; |
| 448 |
|
| 449 |
private final int position; |
| 450 |
|
| 451 |
boolean first; |
| 452 |
|
| 453 |
private boolean last; |
| 454 |
|
| 455 |
SetMultiple(Expression[] arrayIndexes, RowExpression row, int position, boolean first, boolean last) { |
| 456 |
super(arrayIndexes); |
| 457 |
this.row = row; |
| 458 |
this.position = position; |
| 459 |
this.first = first; |
| 460 |
this.last = last; |
| 461 |
} |
| 462 |
|
| 463 |
@Override |
| 464 |
Value update(SessionLocal session) { |
| 465 |
Value[] v; |
| 466 |
if (first) { |
| 467 |
Value value = row.expression.getValue(session); |
| 468 |
if (value == ValueNull.INSTANCE) { |
| 469 |
throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, "NULL to assigned row value"); |
| 470 |
} |
| 471 |
row.values = v = value.convertToAnyRow().getList(); |
| 472 |
if (v.length != row.columns.length) { |
| 473 |
throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH); |
| 474 |
} |
| 475 |
} else { |
| 476 |
v = row.values; |
| 477 |
if (last) { |
| 478 |
row.values = null; |
| 479 |
} |
| 480 |
} |
| 481 |
return v[position]; |
| 482 |
} |
| 483 |
|
| 484 |
@Override |
| 485 |
boolean isEverything(ExpressionVisitor visitor) { |
| 486 |
return super.isEverything(visitor) && (!first || row.isEverything(visitor)); |
| 487 |
} |
| 488 |
|
| 489 |
@Override |
| 490 |
void mapAndOptimize(SessionLocal session, ColumnResolver resolver1, ColumnResolver resolver2) { |
| 491 |
super.mapAndOptimize(session, resolver1, resolver2); |
| 492 |
if (first) { |
| 493 |
row.mapAndOptimize(session, resolver1, resolver2); |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
} |
| 498 |
|
| 499 |
} |