Quadcap Embeddable Database

com/quadcap/sql/AlterColumn.java

Go to the documentation of this file.
00001 package com.quadcap.sql; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.Externalizable; 00042 import java.io.IOException; 00043 import java.io.ObjectInput; 00044 import java.io.ObjectOutput; 00045 00046 import java.sql.SQLException; 00047 00048 import com.quadcap.sql.io.Extern; 00049 00050 import com.quadcap.util.Debug; 00051 00052 /** 00053 * Log Step to alter the definition of a table column. 00054 * 00055 * @author Stan Bailes 00056 */ 00057 public class AlterColumn extends LogStep implements Externalizable { 00058 transient Table table; 00059 transient Column column; 00060 00061 String columnName = null; 00062 String tableName = null; 00063 Expression oldDefault = null; 00064 Expression newDefault = null; 00065 00066 /** 00067 * Default constructor 00068 */ 00069 public AlterColumn() {} 00070 00071 /** 00072 * Explicit constructor from table, column name and new default value 00073 */ 00074 public AlterColumn(Session session, Table table, 00075 String columnName, Expression newDefault) { 00076 super(session); 00077 this.table = table; 00078 this.tableName = table.getName(); 00079 this.columnName = columnName; 00080 this.newDefault = newDefault; 00081 } 00082 00083 Table getTable(Database db) throws IOException { 00084 if (table == null) { 00085 table = (Table)db.getRelation(tableName); 00086 } 00087 return table; 00088 } 00089 00090 public void undo(Session session) throws IOException, SQLException { 00091 Database db = session.getDatabase(); 00092 getTable(db); 00093 column = table.getColumn(columnName); 00094 column.setDefault(oldDefault); 00095 db.updateRelation(table); 00096 } 00097 00098 public void redo(Session session) throws IOException, SQLException { 00099 Database db = session.getDatabase(); 00100 getTable(db); 00101 column.setDefault(newDefault); 00102 db.updateRelation(table); 00103 } 00104 00105 public void prepare(Session session) throws IOException, SQLException { 00106 column = table.getColumn(columnName); 00107 if (oldDefault == null) { 00108 oldDefault = column.getDefault(); 00109 } 00110 } 00111 00112 public void readExternal(ObjectInput in) 00113 throws IOException, ClassNotFoundException 00114 { 00115 super.readExternal(in); 00116 this.columnName = (String)in.readObject(); 00117 this.tableName = (String)in.readObject(); 00118 this.oldDefault = (Expression)in.readObject(); 00119 this.newDefault = (Expression)in.readObject(); 00120 } 00121 00122 public void writeExternal(ObjectOutput out) throws IOException { 00123 super.writeExternal(out); 00124 out.writeObject(columnName); 00125 out.writeObject(tableName); 00126 out.writeObject(oldDefault); 00127 out.writeObject(newDefault); 00128 } 00129 00130 //#ifdef DEBUG 00131 public String toString() { 00132 StringBuffer sb = new StringBuffer(super.toString()); 00133 sb.append(" AlterColumn("); 00134 sb.append(tableName); 00135 sb.append(','); 00136 sb.append(columnName); 00137 sb.append(" set default " + newDefault); 00138 sb.append(")"); 00139 return sb.toString(); 00140 } 00141 //#endif 00142 00143 static Extern extern = null; 00144 public Extern getExtern() { return extern; } 00145 public void setExtern(Extern extern) { AlterColumn.extern = extern; } 00146 }