00001
package com.quadcap.sql;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.IOException;
00042
00043
import java.util.Enumeration;
00044
00045
import java.sql.SQLException;
00046
00047
import com.quadcap.util.Debug;
00048
00049
00050
00051
00052
00053
00054 public class StmtDropConstraint implements Stmt {
00055 String
tableName;
00056 String
constraintName;
00057 boolean restrict;
00058
00059 public StmtDropConstraint(String tableName, String constraintName,
00060
boolean restrict) {
00061
this.tableName = tableName;
00062
this.constraintName = constraintName;
00063
this.restrict = restrict;
00064 }
00065
00066 public void execute(
Session session)
throws IOException, SQLException {
00067 session.getTableWriteLock(
"#Schema");
00068 session.getTableWriteLock(
tableName);
00069
Database db = session.getDatabase();
00070
Table table = (
Table)db.
getRelation(
tableName);
00071
if (table == null) {
00072
throw new SQLException(
"table not found: " +
tableName);
00073 }
00074
Constraint constraint = table.
getConstraint(
constraintName);
00075
if (constraint == null) {
00076
throw new SQLException(
"constraint not found: " +
constraintName);
00077 }
00078
if (
restrict) {
00079
if (constraint instanceof
UniqueConstraint) {
00080
UniqueConstraint uc = (
UniqueConstraint)constraint;
00081 Enumeration en = uc.
getExportConstraints();
00082
if (en.hasMoreElements()) {
00083
throw new SQLException(
"Can't drop unique/primary key " +
00084
"because of referencing foreign " +
00085
"key constraints");
00086 }
00087 }
00088 }
else {
00089
if (constraint instanceof
UniqueConstraint) {
00090
UniqueConstraint uc = (
UniqueConstraint)constraint;
00091 Enumeration en = uc.
getExportConstraints();
00092
while (en.hasMoreElements()) {
00093 String name = (String)en.nextElement().toString();
00094
ExportedKeyConstraint ec =
00095 (
ExportedKeyConstraint)table.
getConstraint(name);
00096 session.doStep(
new DeleteConstraint(session, table, ec));
00097
DeleteConstraint.deleteForeign(session, ec);
00098 }
00099 }
00100 }
00101
00102
00103
00104
00105
if (constraint instanceof
IndexConstraint) {
00106
int icnt = 0;
00107
int num = table.
getNumConstraints();
00108
for (
int i = 0; i < num; i++) {
00109
Constraint c = table.
getConstraint(i);
00110
if (c instanceof
IndexConstraint) icnt++;
00111 }
00112
if (icnt == 1) {
00113
DefaultTableConstraint con =
new DefaultTableConstraint();
00114 con.
setTable(table);
00115 table.
nameConstraint(con);
00116 session.doStep(
new AddConstraint(session, table, con));
00117 }
00118 }
00119 session.doStep(
new DeleteConstraint(session, table, constraint));
00120
DeleteConstraint.deleteForeign(session, constraint);
00121
00122 }
00123
00124 }