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