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.Externalizable;
042: import java.io.IOException;
043: import java.io.ObjectInput;
044: import java.io.ObjectOutput;
045:
046: import java.sql.SQLException;
047:
048: import com.quadcap.sql.io.Extern;
049:
050: import com.quadcap.util.Debug;
051:
052: /**
053: * Log Step to alter the definition of a table column.
054: *
055: * @author Stan Bailes
056: */
057: public class AlterColumn extends LogStep implements Externalizable {
058: transient Table table;
059: transient Column column;
060:
061: String columnName = null;
062: String tableName = null;
063: Expression oldDefault = null;
064: Expression newDefault = null;
065:
066: /**
067: * Default constructor
068: */
069: public AlterColumn() {
070: }
071:
072: /**
073: * Explicit constructor from table, column name and new default value
074: */
075: public AlterColumn(Session session, Table table, String columnName,
076: Expression newDefault) {
077: super (session);
078: this .table = table;
079: this .tableName = table.getName();
080: this .columnName = columnName;
081: this .newDefault = newDefault;
082: }
083:
084: Table getTable(Database db) throws IOException {
085: if (table == null) {
086: table = (Table) db.getRelation(tableName);
087: }
088: return table;
089: }
090:
091: public void undo(Session session) throws IOException, SQLException {
092: Database db = session.getDatabase();
093: getTable(db);
094: column = table.getColumn(columnName);
095: column.setDefault(oldDefault);
096: db.updateRelation(table);
097: }
098:
099: public void redo(Session session) throws IOException, SQLException {
100: Database db = session.getDatabase();
101: getTable(db);
102: column.setDefault(newDefault);
103: db.updateRelation(table);
104: }
105:
106: public void prepare(Session session) throws IOException,
107: SQLException {
108: column = table.getColumn(columnName);
109: if (oldDefault == null) {
110: oldDefault = column.getDefault();
111: }
112: }
113:
114: public void readExternal(ObjectInput in) throws IOException,
115: ClassNotFoundException {
116: super .readExternal(in);
117: this .columnName = (String) in.readObject();
118: this .tableName = (String) in.readObject();
119: this .oldDefault = (Expression) in.readObject();
120: this .newDefault = (Expression) in.readObject();
121: }
122:
123: public void writeExternal(ObjectOutput out) throws IOException {
124: super .writeExternal(out);
125: out.writeObject(columnName);
126: out.writeObject(tableName);
127: out.writeObject(oldDefault);
128: out.writeObject(newDefault);
129: }
130:
131: //#ifdef DEBUG
132: public String toString() {
133: StringBuffer sb = new StringBuffer(super .toString());
134: sb.append(" AlterColumn(");
135: sb.append(tableName);
136: sb.append(',');
137: sb.append(columnName);
138: sb.append(" set default " + newDefault);
139: sb.append(")");
140: return sb.toString();
141: }
142:
143: //#endif
144:
145: static Extern extern = null;
146:
147: public Extern getExtern() {
148: return extern;
149: }
150:
151: public void setExtern(Extern extern) {
152: AlterColumn.extern = extern;
153: }
154: }
|