| 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.ddl; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
|
| 10 |
import org.h2.api.ErrorCode; |
| 11 |
import org.h2.command.CommandInterface; |
| 12 |
import org.h2.engine.Constants; |
| 13 |
import org.h2.engine.SessionLocal; |
| 14 |
import org.h2.message.DbException; |
| 15 |
import org.h2.schema.Schema; |
| 16 |
import org.h2.schema.Sequence; |
| 17 |
import org.h2.table.Column; |
| 18 |
import org.h2.table.IndexColumn; |
| 19 |
|
| 20 |
public abstract class CommandWithColumns extends SchemaCommand { |
| 21 |
|
| 22 |
private ArrayList<DefineCommand> constraintCommands; |
| 23 |
|
| 24 |
private AlterTableAddConstraint primaryKey; |
| 25 |
|
| 26 |
protected CommandWithColumns(SessionLocal session, Schema schema) { |
| 27 |
super(session, schema); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Add a column to this table. |
| 32 |
* |
| 33 |
* @param column |
| 34 |
* the column to add |
| 35 |
*/ |
| 36 |
public abstract void addColumn(Column column); |
| 37 |
|
| 38 |
/** |
| 39 |
* Add a constraint statement to this statement. The primary key definition is |
| 40 |
* one possible constraint statement. |
| 41 |
* |
| 42 |
* @param command |
| 43 |
* the statement to add |
| 44 |
*/ |
| 45 |
public void addConstraintCommand(DefineCommand command) { |
| 46 |
if (!(command instanceof CreateIndex)) { |
| 47 |
AlterTableAddConstraint con = (AlterTableAddConstraint) command; |
| 48 |
if (con.getType() == CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY) { |
| 49 |
if (setPrimaryKey(con)) { |
| 50 |
return; |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
getConstraintCommands().add(command); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* For the given list of columns, disable "nullable" for those columns that |
| 59 |
* are primary key columns. |
| 60 |
* |
| 61 |
* @param columns the list of columns |
| 62 |
*/ |
| 63 |
protected void changePrimaryKeysToNotNull(ArrayList<Column> columns) { |
| 64 |
if (primaryKey != null) { |
| 65 |
IndexColumn[] pkColumns = primaryKey.getIndexColumns(); |
| 66 |
for (Column c : columns) { |
| 67 |
for (IndexColumn idxCol : pkColumns) { |
| 68 |
if (c.getName().equals(idxCol.columnName)) { |
| 69 |
c.setNullable(false); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Create the constraints. |
| 78 |
*/ |
| 79 |
protected void createConstraints() { |
| 80 |
if (constraintCommands != null) { |
| 81 |
for (DefineCommand command : constraintCommands) { |
| 82 |
command.setTransactional(transactional); |
| 83 |
command.update(); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* For the given list of columns, create sequences for identity |
| 90 |
* columns (if needed), and then get the list of all sequences of the |
| 91 |
* columns. |
| 92 |
* |
| 93 |
* @param columns the columns |
| 94 |
* @param temporary whether generated sequences should be temporary |
| 95 |
* @return the list of sequences (may be empty) |
| 96 |
*/ |
| 97 |
protected ArrayList<Sequence> generateSequences(ArrayList<Column> columns, boolean temporary) { |
| 98 |
ArrayList<Sequence> sequences = new ArrayList<>(columns == null ? 0 : columns.size()); |
| 99 |
if (columns != null) { |
| 100 |
for (Column c : columns) { |
| 101 |
if (c.hasIdentityOptions()) { |
| 102 |
int objId = getDatabase().allocateObjectId(); |
| 103 |
c.initializeSequence(session, getSchema(), objId, temporary); |
| 104 |
if (!Constants.CLUSTERING_DISABLED.equals(getDatabase().getCluster())) { |
| 105 |
throw DbException.getUnsupportedException("CLUSTERING && identity columns"); |
| 106 |
} |
| 107 |
} |
| 108 |
Sequence seq = c.getSequence(); |
| 109 |
if (seq != null) { |
| 110 |
sequences.add(seq); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
return sequences; |
| 115 |
} |
| 116 |
|
| 117 |
private ArrayList<DefineCommand> getConstraintCommands() { |
| 118 |
if (constraintCommands == null) { |
| 119 |
constraintCommands = new ArrayList<>(); |
| 120 |
} |
| 121 |
return constraintCommands; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Set the primary key, but also check if a primary key with different |
| 126 |
* columns is already defined. |
| 127 |
* <p> |
| 128 |
* If an unnamed primary key with the same columns is already defined it is |
| 129 |
* removed from the list of constraints and this method returns |
| 130 |
* {@code false}. |
| 131 |
* </p> |
| 132 |
* |
| 133 |
* @param primaryKey |
| 134 |
* the primary key |
| 135 |
* @return whether another primary key with the same columns was already set |
| 136 |
* and the specified primary key should be ignored |
| 137 |
*/ |
| 138 |
private boolean setPrimaryKey(AlterTableAddConstraint primaryKey) { |
| 139 |
if (this.primaryKey != null) { |
| 140 |
IndexColumn[] oldColumns = this.primaryKey.getIndexColumns(); |
| 141 |
IndexColumn[] newColumns = primaryKey.getIndexColumns(); |
| 142 |
int len = newColumns.length; |
| 143 |
if (len != oldColumns.length) { |
| 144 |
throw DbException.get(ErrorCode.SECOND_PRIMARY_KEY); |
| 145 |
} |
| 146 |
for (int i = 0; i < len; i++) { |
| 147 |
if (!newColumns[i].columnName.equals(oldColumns[i].columnName)) { |
| 148 |
throw DbException.get(ErrorCode.SECOND_PRIMARY_KEY); |
| 149 |
} |
| 150 |
} |
| 151 |
if (this.primaryKey.getConstraintName() != null) { |
| 152 |
return true; |
| 153 |
} |
| 154 |
// Remove unnamed primary key |
| 155 |
constraintCommands.remove(this.primaryKey); |
| 156 |
} |
| 157 |
this.primaryKey = primaryKey; |
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
public AlterTableAddConstraint getPrimaryKey() { |
| 162 |
return primaryKey; |
| 163 |
} |
| 164 |
|
| 165 |
} |