Debian Codegraph @

src/main/org/h2/table/DataChangeDeltaTable.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.table;
7
8 import org.h2.command.dml.DataChangeStatement;
9 import org.h2.engine.SessionLocal;
10 import org.h2.expression.Expression;
11 import org.h2.expression.ExpressionColumn;
12 import org.h2.result.LocalResult;
13 import org.h2.result.ResultInterface;
14 import org.h2.result.ResultTarget;
15 import org.h2.result.Row;
16 import org.h2.schema.Schema;
17
18 /**
19 * A data change delta table.
20 */
21 public class DataChangeDeltaTable extends VirtualConstructedTable {
22
23 /**
24 * Result option.
25 */
26 public enum ResultOption {
27
28 /**
29 * OLD row.
30 */
31 OLD,
32
33 /**
34 * NEW row with evaluated default expressions, but before triggers.
35 */
36 NEW,
37
38 /**
39 * FINAL rows after triggers.
40 */
41 FINAL;
42
43 }
44
45 /**
46 * Collects final row for INSERT operations.
47 *
48 * @param session
49 * the session
50 * @param table
51 * the table
52 * @param deltaChangeCollector
53 * target result
54 * @param deltaChangeCollectionMode
55 * collection mode
56 * @param newRow
57 * the inserted row
58 */
59 public static void collectInsertedFinalRow(SessionLocal session, Table table, ResultTarget deltaChangeCollector,
60 ResultOption deltaChangeCollectionMode, Row newRow) {
61 if (session.getMode().takeInsertedIdentity) {
62 Column column = table.getIdentityColumn();
63 if (column != null) {
64 session.setLastIdentity(newRow.getValue(column.getColumnId()));
65 }
66 }
67 if (deltaChangeCollectionMode == ResultOption.FINAL) {
68 deltaChangeCollector.addRow(newRow.getValueList());
69 }
70 }
71
72 private final DataChangeStatement statement;
73
74 private final ResultOption resultOption;
75
76 private final Expression[] expressions;
77
78 public DataChangeDeltaTable(Schema schema, SessionLocal session, DataChangeStatement statement,
79 ResultOption resultOption) {
80 super(schema, 0, statement.getStatementName());
81 this.statement = statement;
82 this.resultOption = resultOption;
83 Table table = statement.getTable();
84 Column[] tableColumns = table.getColumns();
85 int columnCount = tableColumns.length;
86 Column[] c = new Column[columnCount];
87 for (int i = 0; i < columnCount; i++) {
88 c[i] = tableColumns[i].getClone();
89 }
90 setColumns(c);
91 Expression[] expressions = new Expression[columnCount];
92 String tableName = getName();
93 for (int i = 0; i < columnCount; i++) {
94 expressions[i] = new ExpressionColumn(database, null, tableName, c[i].getName());
95 }
96 this.expressions = expressions;
97 }
98
99 @Override
100 public boolean canGetRowCount(SessionLocal session) {
101 return false;
102 }
103
104 @Override
105 public long getRowCount(SessionLocal session) {
106 return Long.MAX_VALUE;
107 }
108
109 @Override
110 public long getRowCountApproximation(SessionLocal session) {
111 return Long.MAX_VALUE;
112 }
113
114 @Override
115 public ResultInterface getResult(SessionLocal session) {
116 statement.prepare();
117 int columnCount = expressions.length;
118 LocalResult result = new LocalResult(session, expressions, columnCount, columnCount);
119 result.setForDataChangeDeltaTable();
120 statement.update(result, resultOption);
121 return result;
122 }
123
124 @Override
125 public StringBuilder getSQL(StringBuilder builder, int sqlFlags) {
126 return builder.append(resultOption.name()).append(" TABLE (").append(statement.getSQL()).append(')');
127 }
128
129 @Override
130 public boolean isDeterministic() {
131 return false;
132 }
133
134 }