| 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 |
import java.util.HashSet; |
| 10 |
import org.h2.api.ErrorCode; |
| 11 |
import org.h2.command.CommandContainer; |
| 12 |
import org.h2.command.CommandInterface; |
| 13 |
import org.h2.command.Parser; |
| 14 |
import org.h2.command.Prepared; |
| 15 |
import org.h2.constraint.Constraint; |
| 16 |
import org.h2.constraint.ConstraintReferential; |
| 17 |
import org.h2.constraint.ConstraintUnique; |
| 18 |
import org.h2.engine.Constants; |
| 19 |
import org.h2.engine.Database; |
| 20 |
import org.h2.engine.DbObject; |
| 21 |
import org.h2.engine.Right; |
| 22 |
import org.h2.engine.SessionLocal; |
| 23 |
import org.h2.expression.Expression; |
| 24 |
import org.h2.expression.ExpressionVisitor; |
| 25 |
import org.h2.index.Index; |
| 26 |
import org.h2.index.IndexType; |
| 27 |
import org.h2.message.DbException; |
| 28 |
import org.h2.result.ResultInterface; |
| 29 |
import org.h2.result.SearchRow; |
| 30 |
import org.h2.schema.Schema; |
| 31 |
import org.h2.schema.SchemaObject; |
| 32 |
import org.h2.schema.Sequence; |
| 33 |
import org.h2.schema.TriggerObject; |
| 34 |
import org.h2.table.Column; |
| 35 |
import org.h2.table.Table; |
| 36 |
import org.h2.table.TableBase; |
| 37 |
import org.h2.table.TableView; |
| 38 |
import org.h2.util.HasSQL; |
| 39 |
import org.h2.util.Utils; |
| 40 |
|
| 41 |
/** |
| 42 |
* This class represents the statements |
| 43 |
* ALTER TABLE ADD, |
| 44 |
* ALTER TABLE ADD IF NOT EXISTS, |
| 45 |
* ALTER TABLE ALTER COLUMN, |
| 46 |
* ALTER TABLE ALTER COLUMN SELECTIVITY, |
| 47 |
* ALTER TABLE ALTER COLUMN SET DEFAULT, |
| 48 |
* ALTER TABLE ALTER COLUMN DROP DEFAULT, |
| 49 |
* ALTER TABLE ALTER COLUMN DROP EXPRESSION, |
| 50 |
* ALTER TABLE ALTER COLUMN SET NULL, |
| 51 |
* ALTER TABLE ALTER COLUMN DROP NULL, |
| 52 |
* ALTER TABLE ALTER COLUMN SET VISIBLE, |
| 53 |
* ALTER TABLE ALTER COLUMN SET INVISIBLE, |
| 54 |
* ALTER TABLE DROP COLUMN |
| 55 |
*/ |
| 56 |
public class AlterTableAlterColumn extends CommandWithColumns { |
| 57 |
|
| 58 |
private String tableName; |
| 59 |
private Column oldColumn; |
| 60 |
private Column newColumn; |
| 61 |
private int type; |
| 62 |
/** |
| 63 |
* Default or on update expression. |
| 64 |
*/ |
| 65 |
private Expression defaultExpression; |
| 66 |
private Expression newSelectivity; |
| 67 |
private Expression usingExpression; |
| 68 |
private boolean addFirst; |
| 69 |
private String addBefore; |
| 70 |
private String addAfter; |
| 71 |
private boolean ifTableExists; |
| 72 |
private boolean ifNotExists; |
| 73 |
private ArrayList<Column> columnsToAdd; |
| 74 |
private ArrayList<Column> columnsToRemove; |
| 75 |
private boolean booleanFlag; |
| 76 |
|
| 77 |
public AlterTableAlterColumn(SessionLocal session, Schema schema) { |
| 78 |
super(session, schema); |
| 79 |
} |
| 80 |
|
| 81 |
public void setIfTableExists(boolean b) { |
| 82 |
ifTableExists = b; |
| 83 |
} |
| 84 |
|
| 85 |
public void setTableName(String tableName) { |
| 86 |
this.tableName = tableName; |
| 87 |
} |
| 88 |
|
| 89 |
public void setOldColumn(Column oldColumn) { |
| 90 |
this.oldColumn = oldColumn; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Add the column as the first column of the table. |
| 95 |
*/ |
| 96 |
public void setAddFirst() { |
| 97 |
addFirst = true; |
| 98 |
} |
| 99 |
|
| 100 |
public void setAddBefore(String before) { |
| 101 |
this.addBefore = before; |
| 102 |
} |
| 103 |
|
| 104 |
public void setAddAfter(String after) { |
| 105 |
this.addAfter = after; |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public long update() { |
| 110 |
Database db = getDatabase(); |
| 111 |
Table table = getSchema().resolveTableOrView(session, tableName); |
| 112 |
if (table == null) { |
| 113 |
if (ifTableExists) { |
| 114 |
return 0; |
| 115 |
} |
| 116 |
throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableName); |
| 117 |
} |
| 118 |
session.getUser().checkTableRight(table, Right.SCHEMA_OWNER); |
| 119 |
table.checkSupportAlter(); |
| 120 |
table.lock(session, Table.EXCLUSIVE_LOCK); |
| 121 |
if (newColumn != null) { |
| 122 |
checkDefaultReferencesTable(table, newColumn.getDefaultExpression()); |
| 123 |
checkClustering(newColumn); |
| 124 |
} |
| 125 |
if (columnsToAdd != null) { |
| 126 |
for (Column column : columnsToAdd) { |
| 127 |
checkDefaultReferencesTable(table, column.getDefaultExpression()); |
| 128 |
checkClustering(column); |
| 129 |
} |
| 130 |
} |
| 131 |
switch (type) { |
| 132 |
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_NOT_NULL: { |
| 133 |
if (oldColumn == null || !oldColumn.isNullable()) { |
| 134 |
// no change |
| 135 |
break; |
| 136 |
} |
| 137 |
checkNoNullValues(table); |
| 138 |
oldColumn.setNullable(false); |
| 139 |
db.updateMeta(session, table); |
| 140 |
break; |
| 141 |
} |
| 142 |
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_DROP_NOT_NULL: { |
| 143 |
if (oldColumn == null || oldColumn.isNullable()) { |
| 144 |
// no change |
| 145 |
break; |
| 146 |
} |
| 147 |
checkNullable(table); |
| 148 |
oldColumn.setNullable(true); |
| 149 |
db.updateMeta(session, table); |
| 150 |
break; |
| 151 |
} |
| 152 |
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT: |
| 153 |
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_DROP_EXPRESSION: { |
| 154 |
if (oldColumn == null) { |
| 155 |
break; |
| 156 |
} |
| 157 |
if (oldColumn.isIdentity()) { |
| 158 |
break; |
| 159 |
} |
| 160 |
if (defaultExpression != null) { |
| 161 |
if (oldColumn.isGenerated()) { |
| 162 |
break; |
| 163 |
} |
| 164 |
checkDefaultReferencesTable(table, defaultExpression); |
| 165 |
oldColumn.setDefaultExpression(session, defaultExpression); |
| 166 |
} else { |
| 167 |
if (type == CommandInterface.ALTER_TABLE_ALTER_COLUMN_DROP_EXPRESSION != oldColumn.isGenerated()) { |
| 168 |
break; |
| 169 |
} |
| 170 |
oldColumn.setDefaultExpression(session, null); |
| 171 |
} |
| 172 |
db.updateMeta(session, table); |
| 173 |
break; |
| 174 |
} |
| 175 |
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_DROP_IDENTITY: { |
| 176 |
if (oldColumn == null) { |
| 177 |
break; |
| 178 |
} |
| 179 |
Sequence sequence = oldColumn.getSequence(); |
| 180 |
if (sequence == null) { |
| 181 |
break; |
| 182 |
} |
| 183 |
oldColumn.setSequence(null, false); |
| 184 |
removeSequence(table, sequence); |
| 185 |
db.updateMeta(session, table); |
| 186 |
break; |
| 187 |
} |
| 188 |
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_ON_UPDATE: { |
| 189 |
if (oldColumn == null) { |
| 190 |
break; |
| 191 |
} |
| 192 |
if (defaultExpression != null) { |
| 193 |
if (oldColumn.isIdentity() || oldColumn.isGenerated()) { |
| 194 |
break; |
| 195 |
} |
| 196 |
checkDefaultReferencesTable(table, defaultExpression); |
| 197 |
oldColumn.setOnUpdateExpression(session, defaultExpression); |
| 198 |
} else { |
| 199 |
oldColumn.setOnUpdateExpression(session, null); |
| 200 |
} |
| 201 |
db.updateMeta(session, table); |
| 202 |
break; |
| 203 |
} |
| 204 |
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_CHANGE_TYPE: { |
| 205 |
if (oldColumn == null) { |
| 206 |
break; |
| 207 |
} |
| 208 |
// if the change is only increasing the precision, then we don't |
| 209 |
// need to copy the table because the length is only a constraint, |
| 210 |
// and does not affect the storage structure. |
| 211 |
if (oldColumn.isWideningConversion(newColumn) && usingExpression == null) { |
| 212 |
convertIdentityColumn(table, newColumn); |
| 213 |
oldColumn.copy(newColumn); |
| 214 |
db.updateMeta(session, table); |
| 215 |
} else { |
| 216 |
oldColumn.setSequence(null, false); |
| 217 |
oldColumn.setDefaultExpression(session, null); |
| 218 |
if (oldColumn.isNullable() && !newColumn.isNullable()) { |
| 219 |
checkNoNullValues(table); |
| 220 |
} else if (!oldColumn.isNullable() && newColumn.isNullable()) { |
| 221 |
checkNullable(table); |
| 222 |
} |
| 223 |
if (oldColumn.getVisible() ^ newColumn.getVisible()) { |
| 224 |
oldColumn.setVisible(newColumn.getVisible()); |
| 225 |
} |
| 226 |
convertIdentityColumn(table, newColumn); |
| 227 |
copyData(table, null, true); |
| 228 |
} |
| 229 |
table.setModified(); |
| 230 |
break; |
| 231 |
} |
| 232 |
case CommandInterface.ALTER_TABLE_ADD_COLUMN: { |
| 233 |
// ifNotExists only supported for single column add |
| 234 |
if (ifNotExists && columnsToAdd != null && columnsToAdd.size() == 1 && |
| 235 |
table.doesColumnExist(columnsToAdd.get(0).getName())) { |
| 236 |
break; |
| 237 |
} |
| 238 |
ArrayList<Sequence> sequences = generateSequences(columnsToAdd, false); |
| 239 |
if (columnsToAdd != null) { |
| 240 |
changePrimaryKeysToNotNull(columnsToAdd); |
| 241 |
} |
| 242 |
copyData(table, sequences, true); |
| 243 |
break; |
| 244 |
} |
| 245 |
case CommandInterface.ALTER_TABLE_DROP_COLUMN: { |
| 246 |
if (table.getColumns().length - columnsToRemove.size() < 1) { |
| 247 |
throw DbException.get(ErrorCode.CANNOT_DROP_LAST_COLUMN, columnsToRemove.get(0).getTraceSQL()); |
| 248 |
} |
| 249 |
table.dropMultipleColumnsConstraintsAndIndexes(session, columnsToRemove); |
| 250 |
copyData(table, null, false); |
| 251 |
break; |
| 252 |
} |
| 253 |
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_SELECTIVITY: { |
| 254 |
if (oldColumn == null) { |
| 255 |
break; |
| 256 |
} |
| 257 |
int value = newSelectivity.optimize(session).getValue(session).getInt(); |
| 258 |
oldColumn.setSelectivity(value); |
| 259 |
db.updateMeta(session, table); |
| 260 |
break; |
| 261 |
} |
| 262 |
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_VISIBILITY: |
| 263 |
if (oldColumn == null) { |
| 264 |
break; |
| 265 |
} |
| 266 |
if (oldColumn.getVisible() != booleanFlag) { |
| 267 |
oldColumn.setVisible(booleanFlag); |
| 268 |
table.setModified(); |
| 269 |
db.updateMeta(session, table); |
| 270 |
} |
| 271 |
break; |
| 272 |
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT_ON_NULL: |
| 273 |
if (oldColumn == null) { |
| 274 |
break; |
| 275 |
} |
| 276 |
if (oldColumn.isDefaultOnNull() != booleanFlag) { |
| 277 |
oldColumn.setDefaultOnNull(booleanFlag); |
| 278 |
table.setModified(); |
| 279 |
db.updateMeta(session, table); |
| 280 |
} |
| 281 |
break; |
| 282 |
default: |
| 283 |
throw DbException.getInternalError("type=" + type); |
| 284 |
} |
| 285 |
return 0; |
| 286 |
} |
| 287 |
|
| 288 |
private static void checkDefaultReferencesTable(Table table, Expression defaultExpression) { |
| 289 |
if (defaultExpression == null) { |
| 290 |
return; |
| 291 |
} |
| 292 |
HashSet<DbObject> dependencies = new HashSet<>(); |
| 293 |
ExpressionVisitor visitor = ExpressionVisitor |
| 294 |
.getDependenciesVisitor(dependencies); |
| 295 |
defaultExpression.isEverything(visitor); |
| 296 |
if (dependencies.contains(table)) { |
| 297 |
throw DbException.get(ErrorCode.COLUMN_IS_REFERENCED_1, defaultExpression.getTraceSQL()); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
private void checkClustering(Column c) { |
| 302 |
if (!Constants.CLUSTERING_DISABLED |
| 303 |
.equals(getDatabase().getCluster()) |
| 304 |
&& c.hasIdentityOptions()) { |
| 305 |
throw DbException.getUnsupportedException( |
| 306 |
"CLUSTERING && identity columns"); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
private void convertIdentityColumn(Table table, Column c) { |
| 311 |
if (c.hasIdentityOptions()) { |
| 312 |
if (c.isPrimaryKey()) { |
| 313 |
addConstraintCommand( |
| 314 |
Parser.newPrimaryKeyConstraintCommand(session, table.getSchema(), table.getName(), c)); |
| 315 |
} |
| 316 |
int objId = getObjectId(); |
| 317 |
c.initializeSequence(session, getSchema(), objId, table.isTemporary()); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
private void removeSequence(Table table, Sequence sequence) { |
| 322 |
if (sequence != null) { |
| 323 |
table.removeSequence(sequence); |
| 324 |
sequence.setBelongsToTable(false); |
| 325 |
Database db = getDatabase(); |
| 326 |
db.removeSchemaObject(session, sequence); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
private void copyData(Table table, ArrayList<Sequence> sequences, boolean createConstraints) { |
| 331 |
if (table.isTemporary()) { |
| 332 |
throw DbException.getUnsupportedException("TEMP TABLE"); |
| 333 |
} |
| 334 |
Database db = getDatabase(); |
| 335 |
String baseName = table.getName(); |
| 336 |
String tempName = db.getTempTableName(baseName, session); |
| 337 |
Column[] columns = table.getColumns(); |
| 338 |
ArrayList<Column> newColumns = new ArrayList<>(columns.length); |
| 339 |
Table newTable = cloneTableStructure(table, columns, db, tempName, newColumns); |
| 340 |
if (sequences != null) { |
| 341 |
for (Sequence sequence : sequences) { |
| 342 |
table.addSequence(sequence); |
| 343 |
} |
| 344 |
} |
| 345 |
try { |
| 346 |
// check if a view would become invalid |
| 347 |
// (because the column to drop is referenced or so) |
| 348 |
checkViews(table, newTable); |
| 349 |
} catch (DbException e) { |
| 350 |
StringBuilder builder = new StringBuilder("DROP TABLE "); |
| 351 |
newTable.getSQL(builder, HasSQL.DEFAULT_SQL_FLAGS); |
| 352 |
execute(builder.toString()); |
| 353 |
throw e; |
| 354 |
} |
| 355 |
String tableName = table.getName(); |
| 356 |
ArrayList<TableView> dependentViews = new ArrayList<>(table.getDependentViews()); |
| 357 |
for (TableView view : dependentViews) { |
| 358 |
table.removeDependentView(view); |
| 359 |
} |
| 360 |
StringBuilder builder = new StringBuilder("DROP TABLE "); |
| 361 |
table.getSQL(builder, HasSQL.DEFAULT_SQL_FLAGS).append(" IGNORE"); |
| 362 |
execute(builder.toString()); |
| 363 |
db.renameSchemaObject(session, newTable, tableName); |
| 364 |
for (DbObject child : newTable.getChildren()) { |
| 365 |
if (child instanceof Sequence) { |
| 366 |
continue; |
| 367 |
} |
| 368 |
String name = child.getName(); |
| 369 |
if (name == null || child.getCreateSQL() == null) { |
| 370 |
continue; |
| 371 |
} |
| 372 |
if (name.startsWith(tempName + "_")) { |
| 373 |
name = name.substring(tempName.length() + 1); |
| 374 |
SchemaObject so = (SchemaObject) child; |
| 375 |
if (so instanceof Constraint) { |
| 376 |
if (so.getSchema().findConstraint(session, name) != null) { |
| 377 |
name = so.getSchema().getUniqueConstraintName(session, newTable); |
| 378 |
} |
| 379 |
} else if (so instanceof Index) { |
| 380 |
if (so.getSchema().findIndex(session, name) != null) { |
| 381 |
name = so.getSchema().getUniqueIndexName(session, newTable, name); |
| 382 |
} |
| 383 |
} |
| 384 |
db.renameSchemaObject(session, so, name); |
| 385 |
} |
| 386 |
} |
| 387 |
if (createConstraints) { |
| 388 |
createConstraints(); |
| 389 |
} |
| 390 |
for (TableView view : dependentViews) { |
| 391 |
String sql = view.getCreateSQL(true, true); |
| 392 |
execute(sql); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
private Table cloneTableStructure(Table table, Column[] columns, Database db, |
| 397 |
String tempName, ArrayList<Column> newColumns) { |
| 398 |
for (Column col : columns) { |
| 399 |
newColumns.add(col.getClone()); |
| 400 |
} |
| 401 |
switch (type) { |
| 402 |
case CommandInterface.ALTER_TABLE_DROP_COLUMN: |
| 403 |
for (Column removeCol : columnsToRemove) { |
| 404 |
Column foundCol = null; |
| 405 |
for (Column newCol : newColumns) { |
| 406 |
if (newCol.getName().equals(removeCol.getName())) { |
| 407 |
foundCol = newCol; |
| 408 |
break; |
| 409 |
} |
| 410 |
} |
| 411 |
if (foundCol == null) { |
| 412 |
throw DbException.getInternalError(removeCol.getCreateSQL()); |
| 413 |
} |
| 414 |
newColumns.remove(foundCol); |
| 415 |
} |
| 416 |
break; |
| 417 |
case CommandInterface.ALTER_TABLE_ADD_COLUMN: { |
| 418 |
int position; |
| 419 |
if (addFirst) { |
| 420 |
position = 0; |
| 421 |
} else if (addBefore != null) { |
| 422 |
position = table.getColumn(addBefore).getColumnId(); |
| 423 |
} else if (addAfter != null) { |
| 424 |
position = table.getColumn(addAfter).getColumnId() + 1; |
| 425 |
} else { |
| 426 |
position = columns.length; |
| 427 |
} |
| 428 |
if (columnsToAdd != null) { |
| 429 |
for (Column column : columnsToAdd) { |
| 430 |
newColumns.add(position++, column); |
| 431 |
} |
| 432 |
} |
| 433 |
break; |
| 434 |
} |
| 435 |
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_CHANGE_TYPE: |
| 436 |
newColumns.set(oldColumn.getColumnId(), newColumn); |
| 437 |
} |
| 438 |
|
| 439 |
// create a table object in order to get the SQL statement |
| 440 |
// can't just use this table, because most column objects are 'shared' |
| 441 |
// with the old table |
| 442 |
// still need a new id because using 0 would mean: the new table tries |
| 443 |
// to use the rows of the table 0 (the meta table) |
| 444 |
int id = db.allocateObjectId(); |
| 445 |
CreateTableData data = new CreateTableData(); |
| 446 |
data.tableName = tempName; |
| 447 |
data.id = id; |
| 448 |
data.columns = newColumns; |
| 449 |
data.temporary = table.isTemporary(); |
| 450 |
data.persistData = table.isPersistData(); |
| 451 |
data.persistIndexes = table.isPersistIndexes(); |
| 452 |
data.isHidden = table.isHidden(); |
| 453 |
data.session = session; |
| 454 |
Table newTable = getSchema().createTable(data); |
| 455 |
newTable.setComment(table.getComment()); |
| 456 |
String newTableSQL = newTable.getCreateSQLForMeta(); |
| 457 |
StringBuilder columnNames = new StringBuilder(); |
| 458 |
StringBuilder columnValues = new StringBuilder(); |
| 459 |
for (Column nc : newColumns) { |
| 460 |
if (nc.isGenerated()) { |
| 461 |
continue; |
| 462 |
} |
| 463 |
switch (type) { |
| 464 |
case CommandInterface.ALTER_TABLE_ADD_COLUMN: |
| 465 |
if (columnsToAdd != null && columnsToAdd.contains(nc)) { |
| 466 |
if (usingExpression != null) { |
| 467 |
usingExpression.getUnenclosedSQL(addColumn(nc, columnNames, columnValues), |
| 468 |
HasSQL.DEFAULT_SQL_FLAGS); |
| 469 |
} |
| 470 |
continue; |
| 471 |
} |
| 472 |
break; |
| 473 |
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_CHANGE_TYPE: |
| 474 |
if (nc.equals(newColumn) && usingExpression != null) { |
| 475 |
usingExpression.getUnenclosedSQL(addColumn(nc, columnNames, columnValues), |
| 476 |
HasSQL.DEFAULT_SQL_FLAGS); |
| 477 |
continue; |
| 478 |
} |
| 479 |
} |
| 480 |
nc.getSQL(addColumn(nc, columnNames, columnValues), HasSQL.DEFAULT_SQL_FLAGS); |
| 481 |
} |
| 482 |
String newTableName = newTable.getName(); |
| 483 |
Schema newTableSchema = newTable.getSchema(); |
| 484 |
newTable.removeChildrenAndResources(session); |
| 485 |
|
| 486 |
execute(newTableSQL); |
| 487 |
newTable = newTableSchema.getTableOrView(session, newTableName); |
| 488 |
ArrayList<String> children = Utils.newSmallArrayList(); |
| 489 |
ArrayList<String> triggers = Utils.newSmallArrayList(); |
| 490 |
boolean hasDelegateIndex = false; |
| 491 |
for (DbObject child : table.getChildren()) { |
| 492 |
if (child instanceof Sequence) { |
| 493 |
continue; |
| 494 |
} else if (child instanceof Index) { |
| 495 |
Index idx = (Index) child; |
| 496 |
if (idx.getIndexType().getBelongsToConstraint()) { |
| 497 |
continue; |
| 498 |
} |
| 499 |
} |
| 500 |
String createSQL = child.getCreateSQL(); |
| 501 |
if (createSQL == null) { |
| 502 |
continue; |
| 503 |
} |
| 504 |
if (child instanceof TableView) { |
| 505 |
continue; |
| 506 |
} else if (child.getType() == DbObject.TABLE_OR_VIEW) { |
| 507 |
throw DbException.getInternalError(); |
| 508 |
} |
| 509 |
String quotedName = Parser.quoteIdentifier(tempName + "_" + child.getName(), HasSQL.DEFAULT_SQL_FLAGS); |
| 510 |
String sql = null; |
| 511 |
if (child instanceof ConstraintReferential) { |
| 512 |
ConstraintReferential r = (ConstraintReferential) child; |
| 513 |
if (r.getTable() != table) { |
| 514 |
sql = r.getCreateSQLForCopy(r.getTable(), newTable, quotedName, false); |
| 515 |
} |
| 516 |
} |
| 517 |
if (sql == null) { |
| 518 |
sql = child.getCreateSQLForCopy(newTable, quotedName); |
| 519 |
} |
| 520 |
if (sql != null) { |
| 521 |
if (child instanceof TriggerObject) { |
| 522 |
triggers.add(sql); |
| 523 |
} else { |
| 524 |
if (!hasDelegateIndex) { |
| 525 |
Index index = null; |
| 526 |
if (child instanceof ConstraintUnique) { |
| 527 |
ConstraintUnique constraint = (ConstraintUnique) child; |
| 528 |
if (constraint.getConstraintType() == Constraint.Type.PRIMARY_KEY) { |
| 529 |
index = constraint.getIndex(); |
| 530 |
} |
| 531 |
} else if (child instanceof Index) { |
| 532 |
index = (Index) child; |
| 533 |
} |
| 534 |
if (index != null |
| 535 |
&& TableBase.getMainIndexColumn(index.getIndexType(), index.getIndexColumns()) |
| 536 |
!= SearchRow.ROWID_INDEX) { |
| 537 |
execute(sql); |
| 538 |
hasDelegateIndex = true; |
| 539 |
continue; |
| 540 |
} |
| 541 |
} |
| 542 |
children.add(sql); |
| 543 |
} |
| 544 |
} |
| 545 |
} |
| 546 |
StringBuilder builder = newTable.getSQL(new StringBuilder(128).append("INSERT INTO "), // |
| 547 |
HasSQL.DEFAULT_SQL_FLAGS) |
| 548 |
.append('(').append(columnNames).append(") OVERRIDING SYSTEM VALUE SELECT "); |
| 549 |
if (columnValues.length() == 0) { |
| 550 |
// special case: insert into test select * from |
| 551 |
builder.append('*'); |
| 552 |
} else { |
| 553 |
builder.append(columnValues); |
| 554 |
} |
| 555 |
table.getSQL(builder.append(" FROM "), HasSQL.DEFAULT_SQL_FLAGS); |
| 556 |
try { |
| 557 |
execute(builder.toString()); |
| 558 |
} catch (Throwable t) { |
| 559 |
// data was not inserted due to data conversion error or some |
| 560 |
// unexpected reason |
| 561 |
builder = new StringBuilder("DROP TABLE "); |
| 562 |
newTable.getSQL(builder, HasSQL.DEFAULT_SQL_FLAGS); |
| 563 |
execute(builder.toString()); |
| 564 |
throw t; |
| 565 |
} |
| 566 |
for (String sql : children) { |
| 567 |
execute(sql); |
| 568 |
} |
| 569 |
table.setModified(); |
| 570 |
// remove the sequences from the columns (except dropped columns) |
| 571 |
// otherwise the sequence is dropped if the table is dropped |
| 572 |
for (Column col : newColumns) { |
| 573 |
Sequence seq = col.getSequence(); |
| 574 |
if (seq != null) { |
| 575 |
table.removeSequence(seq); |
| 576 |
col.setSequence(null, false); |
| 577 |
} |
| 578 |
} |
| 579 |
for (String sql : triggers) { |
| 580 |
execute(sql); |
| 581 |
} |
| 582 |
return newTable; |
| 583 |
} |
| 584 |
|
| 585 |
private static StringBuilder addColumn(Column column, StringBuilder columnNames, StringBuilder columnValues) { |
| 586 |
if (columnNames.length() > 0) { |
| 587 |
columnNames.append(", "); |
| 588 |
} |
| 589 |
column.getSQL(columnNames, HasSQL.DEFAULT_SQL_FLAGS); |
| 590 |
if (columnValues.length() > 0) { |
| 591 |
columnValues.append(", "); |
| 592 |
} |
| 593 |
return columnValues; |
| 594 |
} |
| 595 |
|
| 596 |
/** |
| 597 |
* Check that all views and other dependent objects. |
| 598 |
*/ |
| 599 |
private void checkViews(SchemaObject sourceTable, SchemaObject newTable) { |
| 600 |
String sourceTableName = sourceTable.getName(); |
| 601 |
String newTableName = newTable.getName(); |
| 602 |
Database db = sourceTable.getDatabase(); |
| 603 |
// save the real table under a temporary name |
| 604 |
String temp = db.getTempTableName(sourceTableName, session); |
| 605 |
db.renameSchemaObject(session, sourceTable, temp); |
| 606 |
try { |
| 607 |
// have our new table impersonate the target table |
| 608 |
db.renameSchemaObject(session, newTable, sourceTableName); |
| 609 |
checkViewsAreValid(sourceTable); |
| 610 |
} finally { |
| 611 |
// always put the source tables back with their proper names |
| 612 |
try { |
| 613 |
db.renameSchemaObject(session, newTable, newTableName); |
| 614 |
} finally { |
| 615 |
db.renameSchemaObject(session, sourceTable, sourceTableName); |
| 616 |
} |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
/** |
| 621 |
* Check that a table or view is still valid. |
| 622 |
* |
| 623 |
* @param tableOrView the table or view to check |
| 624 |
*/ |
| 625 |
private void checkViewsAreValid(DbObject tableOrView) { |
| 626 |
for (DbObject view : tableOrView.getChildren()) { |
| 627 |
if (view instanceof TableView) { |
| 628 |
String sql = ((TableView) view).getQuerySQL(); |
| 629 |
// check if the query is still valid |
| 630 |
// do not execute, not even with limit 1, because that could |
| 631 |
// have side effects or take a very long time |
| 632 |
try { |
| 633 |
session.prepare(sql); |
| 634 |
} catch (DbException e) { |
| 635 |
throw DbException.get(ErrorCode.COLUMN_IS_REFERENCED_1, e, view.getTraceSQL()); |
| 636 |
} |
| 637 |
checkViewsAreValid(view); |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
private void execute(String sql) { |
| 643 |
Prepared command = session.prepare(sql); |
| 644 |
CommandContainer commandContainer = new CommandContainer(session, sql, command); |
| 645 |
commandContainer.executeUpdate(null); |
| 646 |
} |
| 647 |
|
| 648 |
private void checkNullable(Table table) { |
| 649 |
if (oldColumn.isIdentity()) { |
| 650 |
throw DbException.get(ErrorCode.COLUMN_MUST_NOT_BE_NULLABLE_1, oldColumn.getName()); |
| 651 |
} |
| 652 |
for (Index index : table.getIndexes()) { |
| 653 |
if (index.getColumnIndex(oldColumn) < 0) { |
| 654 |
continue; |
| 655 |
} |
| 656 |
IndexType indexType = index.getIndexType(); |
| 657 |
if (indexType.isPrimaryKey()) { |
| 658 |
throw DbException.get(ErrorCode.COLUMN_MUST_NOT_BE_NULLABLE_1, oldColumn.getName()); |
| 659 |
} |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
private void checkNoNullValues(Table table) { |
| 664 |
StringBuilder builder = new StringBuilder("SELECT COUNT(*) FROM "); |
| 665 |
table.getSQL(builder, HasSQL.DEFAULT_SQL_FLAGS).append(" WHERE "); |
| 666 |
oldColumn.getSQL(builder, HasSQL.DEFAULT_SQL_FLAGS).append(" IS NULL"); |
| 667 |
String sql = builder.toString(); |
| 668 |
Prepared command = session.prepare(sql); |
| 669 |
ResultInterface result = command.query(0); |
| 670 |
result.next(); |
| 671 |
if (result.currentRow()[0].getInt() > 0) { |
| 672 |
throw DbException.get(ErrorCode.COLUMN_CONTAINS_NULL_VALUES_1, oldColumn.getTraceSQL()); |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
public void setType(int type) { |
| 677 |
this.type = type; |
| 678 |
} |
| 679 |
|
| 680 |
public void setSelectivity(Expression selectivity) { |
| 681 |
newSelectivity = selectivity; |
| 682 |
} |
| 683 |
|
| 684 |
/** |
| 685 |
* Set default or on update expression. |
| 686 |
* |
| 687 |
* @param defaultExpression default or on update expression |
| 688 |
*/ |
| 689 |
public void setDefaultExpression(Expression defaultExpression) { |
| 690 |
this.defaultExpression = defaultExpression; |
| 691 |
} |
| 692 |
|
| 693 |
/** |
| 694 |
* Set using expression. |
| 695 |
* |
| 696 |
* @param usingExpression using expression |
| 697 |
*/ |
| 698 |
public void setUsingExpression(Expression usingExpression) { |
| 699 |
this.usingExpression = usingExpression; |
| 700 |
} |
| 701 |
|
| 702 |
public void setNewColumn(Column newColumn) { |
| 703 |
this.newColumn = newColumn; |
| 704 |
} |
| 705 |
|
| 706 |
@Override |
| 707 |
public int getType() { |
| 708 |
return type; |
| 709 |
} |
| 710 |
|
| 711 |
public void setIfNotExists(boolean ifNotExists) { |
| 712 |
this.ifNotExists = ifNotExists; |
| 713 |
} |
| 714 |
|
| 715 |
@Override |
| 716 |
public void addColumn(Column column) { |
| 717 |
if (columnsToAdd == null) { |
| 718 |
columnsToAdd = new ArrayList<>(); |
| 719 |
} |
| 720 |
columnsToAdd.add(column); |
| 721 |
} |
| 722 |
|
| 723 |
public void setColumnsToRemove(ArrayList<Column> columnsToRemove) { |
| 724 |
this.columnsToRemove = columnsToRemove; |
| 725 |
} |
| 726 |
|
| 727 |
public void setBooleanFlag(boolean booleanFlag) { |
| 728 |
this.booleanFlag = booleanFlag; |
| 729 |
} |
| 730 |
} |