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.BufferedInputStream;
042: import java.io.BufferedOutputStream;
043: import java.io.Externalizable;
044: import java.io.IOException;
045: import java.io.ObjectInput;
046: import java.io.ObjectOutput;
047:
048: import java.sql.SQLException;
049:
050: import com.quadcap.sql.file.BlockFile;
051: import com.quadcap.sql.file.ByteUtil;
052: import com.quadcap.sql.file.PageManager;
053: import com.quadcap.sql.file.RandomAccess;
054: import com.quadcap.sql.file.RandomAccessInputStream;
055: import com.quadcap.sql.file.RandomAccessOutputStream;
056:
057: import com.quadcap.sql.io.ObjectInputStream;
058: import com.quadcap.sql.io.ObjectOutputStream;
059: import com.quadcap.sql.io.Extern;
060:
061: import com.quadcap.sql.index.Btree;
062:
063: import com.quadcap.sql.types.Value;
064: import com.quadcap.sql.types.ValueNull;
065:
066: import com.quadcap.io.CountedInputStream;
067:
068: import com.quadcap.util.Debug;
069:
070: /**
071: * Log step to add a column to a table, supplying default values as necessary.
072: *
073: * @author Stan Bailes
074: */
075: public class DropColumn extends LogStep implements Externalizable {
076: transient Table table;
077:
078: Column column;
079: String tableName = null;
080:
081: /**
082: * Default constructor
083: */
084: public DropColumn() {
085: }
086:
087: /**
088: * Explicit constructor from table and column
089: */
090: public DropColumn(Session session, Table table, Column column) {
091: super (session);
092: this .table = table;
093: this .tableName = table.getName();
094: this .column = column;
095: }
096:
097: /**
098: * Lazy table accessor
099: */
100: Table getTable(Database db) throws IOException {
101: if (table == null) {
102: table = (Table) db.getRelation(tableName);
103: }
104: return table;
105: }
106:
107: /**
108: * LogStep.redo(), our job is to remove the column definition from the
109: * schema.
110: */
111: public void redo(Session session) throws IOException, SQLException {
112: Database db = session.getDatabase();
113: getTable(db);
114: Column c = table.getColumn(column.getName());
115: table.deleteColumn(c.getColumn());
116: db.updateRelation(table);
117: }
118:
119: /**
120: * LogStep.undo(), our job is to update the table's definition to
121: * include the new column
122: */
123: public void undo(Session session) throws IOException, SQLException {
124: Database db = session.getDatabase();
125: getTable(db);
126: table.addColumn(column, column.getColumn());
127: db.updateRelation(table);
128: }
129:
130: /**
131: * I'm ready
132: */
133: public void prepare(Session session) throws IOException,
134: SQLException {
135: }
136:
137: /**
138: * Read me from a stream
139: */
140: public void readExternal(ObjectInput in) throws IOException,
141: ClassNotFoundException {
142: super .readExternal(in);
143: this .column = (Column) in.readObject();
144: this .tableName = (String) in.readObject();
145: }
146:
147: /**
148: * Write me to a stream
149: */
150: public void writeExternal(ObjectOutput out) throws IOException {
151: super .writeExternal(out);
152: out.writeObject(column);
153: out.writeObject(tableName);
154: }
155:
156: /**
157: * Return a string representation for display/debugging purposes
158: */
159: //#ifdef DEBUG
160: public String toString() {
161: StringBuffer sb = new StringBuffer(super .toString());
162: sb.append(" DropColumn(");
163: sb.append(tableName);
164: sb.append(',');
165: sb.append(column.toString());
166: return sb.toString();
167: }
168:
169: //#endif
170:
171: /**
172: * My class's Extern object (see com.quadcap.sql.io.Extern)
173: */
174: static Extern extern;
175:
176: public void setExtern(Extern extern) {
177: DropColumn.extern = extern;
178: }
179:
180: public Extern getExtern() {
181: return extern;
182: }
183: }
|