001: package com.quadcap.sql;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.IOException;
042:
043: import java.util.Enumeration;
044:
045: import java.sql.SQLException;
046:
047: import com.quadcap.util.Debug;
048:
049: /**
050: * Implementation of the SQL <B>DROP CONSTRAINT</B> statement.
051: *
052: * @author Stan Bailes
053: */
054: public class StmtDropConstraint implements Stmt {
055: String tableName;
056: String constraintName;
057: boolean restrict;
058:
059: public StmtDropConstraint(String tableName, String constraintName,
060: boolean restrict) {
061: this .tableName = tableName;
062: this .constraintName = constraintName;
063: this .restrict = restrict;
064: }
065:
066: public void execute(Session session) throws IOException,
067: SQLException {
068: session.getTableWriteLock("#Schema");
069: session.getTableWriteLock(tableName);
070: Database db = session.getDatabase();
071: Table table = (Table) db.getRelation(tableName);
072: if (table == null) {
073: throw new SQLException("table not found: " + tableName);
074: }
075: Constraint constraint = table.getConstraint(constraintName);
076: if (constraint == null) {
077: throw new SQLException("constraint not found: "
078: + constraintName);
079: }
080: if (restrict) {
081: if (constraint instanceof UniqueConstraint) {
082: UniqueConstraint uc = (UniqueConstraint) constraint;
083: Enumeration en = uc.getExportConstraints();
084: if (en.hasMoreElements()) {
085: throw new SQLException(
086: "Can't drop unique/primary key "
087: + "because of referencing foreign "
088: + "key constraints");
089: }
090: }
091: } else {
092: if (constraint instanceof UniqueConstraint) {
093: UniqueConstraint uc = (UniqueConstraint) constraint;
094: Enumeration en = uc.getExportConstraints();
095: while (en.hasMoreElements()) {
096: String name = (String) en.nextElement().toString();
097: ExportedKeyConstraint ec = (ExportedKeyConstraint) table
098: .getConstraint(name);
099: session.doStep(new DeleteConstraint(session, table,
100: ec));
101: DeleteConstraint.deleteForeign(session, ec);
102: }
103: }
104: }
105:
106: // --- if we're about to delete the last unique/primary key constraint,
107: // --- we have to create a DefaultTableConstraint so there's some
108: // --- kind of index.
109: if (constraint instanceof IndexConstraint) {
110: int icnt = 0;
111: int num = table.getNumConstraints();
112: for (int i = 0; i < num; i++) {
113: Constraint c = table.getConstraint(i);
114: if (c instanceof IndexConstraint)
115: icnt++;
116: }
117: if (icnt == 1) {
118: DefaultTableConstraint con = new DefaultTableConstraint();
119: con.setTable(table);
120: table.nameConstraint(con);
121: session.doStep(new AddConstraint(session, table, con));
122: }
123: }
124: session
125: .doStep(new DeleteConstraint(session, table, constraint));
126: DeleteConstraint.deleteForeign(session, constraint);
127:
128: }
129:
130: }
|