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.Vector;
044:
045: import java.sql.SQLException;
046:
047: import com.quadcap.util.Debug;
048:
049: /**
050: * Implementation of the SQL <B>CREATE TABLE</B> statement.
051: *
052: * @author Stan Bailes
053: */
054:
055: public class StmtCreateTable implements Stmt {
056: Table table;
057: Vector constraints;
058: boolean index = false;
059:
060: /**
061: * Default constructor
062: */
063: public StmtCreateTable() {
064: }
065:
066: /**
067: * Constructor from table and constraints
068: */
069: public StmtCreateTable(Table table, Vector constraints) {
070: this .table = table;
071: this .constraints = constraints;
072: }
073:
074: /**
075: * Helper to perform the operations necessary to add the specified
076: * constraints
077: */
078: void addConstraints(Session session, Vector constraints)
079: throws IOException, SQLException {
080: Database db = session.getDatabase();
081: for (int j = 0; j < constraints.size(); j++) {
082: Constraint con = (Constraint) constraints.elementAt(j);
083: if (con instanceof UniqueConstraint) {
084: index = true;
085: }
086: session.doStep(new AddConstraint(session, table, con));
087: }
088: }
089:
090: /**
091: * Add the table and the constraints
092: */
093: public void execute(Session session) throws IOException,
094: SQLException {
095: session.getTableWriteLock("#Schema");
096: table.setUnderConstruction(true);
097: try {
098: session.doStep(new AddTable(session, table));
099: for (int i = 1; i <= table.getColumnCount(); i++) {
100: Vector v = table.getColumn(i).getConstraints();
101: if (v != null) {
102: addConstraints(session, v);
103: }
104: }
105: addConstraints(session, constraints);
106: if (!index) {
107: Vector v = new Vector();
108: v.addElement(new DefaultTableConstraint());
109: addConstraints(session, v);
110: }
111: } finally {
112: table.setUnderConstruction(false);
113: }
114: }
115: }
|