| 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 org.h2.api.ErrorCode; |
| 9 |
import org.h2.command.CommandInterface; |
| 10 |
import org.h2.command.ddl.SchemaCommand; |
| 11 |
import org.h2.engine.Right; |
| 12 |
import org.h2.engine.SessionLocal; |
| 13 |
import org.h2.message.DbException; |
| 14 |
import org.h2.schema.Schema; |
| 15 |
import org.h2.table.Table; |
| 16 |
|
| 17 |
/** |
| 18 |
* This class represents the statement |
| 19 |
* ALTER TABLE SET |
| 20 |
*/ |
| 21 |
public class AlterTableSet extends SchemaCommand { |
| 22 |
|
| 23 |
private boolean ifTableExists; |
| 24 |
private String tableName; |
| 25 |
private final int type; |
| 26 |
|
| 27 |
private final boolean value; |
| 28 |
private boolean checkExisting; |
| 29 |
|
| 30 |
public AlterTableSet(SessionLocal session, Schema schema, int type, boolean value) { |
| 31 |
super(session, schema); |
| 32 |
this.type = type; |
| 33 |
this.value = value; |
| 34 |
} |
| 35 |
|
| 36 |
public void setCheckExisting(boolean b) { |
| 37 |
this.checkExisting = b; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public boolean isTransactional() { |
| 42 |
return true; |
| 43 |
} |
| 44 |
|
| 45 |
public void setIfTableExists(boolean b) { |
| 46 |
this.ifTableExists = b; |
| 47 |
} |
| 48 |
|
| 49 |
public void setTableName(String tableName) { |
| 50 |
this.tableName = tableName; |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public long update() { |
| 55 |
Table table = getSchema().resolveTableOrView(session, tableName); |
| 56 |
if (table == null) { |
| 57 |
if (ifTableExists) { |
| 58 |
return 0; |
| 59 |
} |
| 60 |
throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableName); |
| 61 |
} |
| 62 |
session.getUser().checkTableRight(table, Right.SCHEMA_OWNER); |
| 63 |
table.lock(session, Table.EXCLUSIVE_LOCK); |
| 64 |
switch (type) { |
| 65 |
case CommandInterface.ALTER_TABLE_SET_REFERENTIAL_INTEGRITY: |
| 66 |
table.setCheckForeignKeyConstraints(session, value, value ? |
| 67 |
checkExisting : false); |
| 68 |
break; |
| 69 |
default: |
| 70 |
throw DbException.getInternalError("type="+type); |
| 71 |
} |
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public int getType() { |
| 77 |
return type; |
| 78 |
} |
| 79 |
|
| 80 |
} |