Quadcap Embeddable Database

com/quadcap/sql/AddConstraint.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 add a constraint to a table. 00054 * 00055 * @author Stan Bailes 00056 */ 00057 public class AddConstraint extends LogStep implements Externalizable { 00058 transient Table table; 00059 00060 Constraint constraint; 00061 String tableName = null; 00062 boolean global; 00063 00064 /** 00065 * Default constructor 00066 */ 00067 public AddConstraint() {} 00068 00069 /** 00070 * Explicit constructor: {table, constraint, global flag} 00071 */ 00072 public AddConstraint(Session session, Table table, Constraint constraint, 00073 boolean global) { 00074 super(session); 00075 this.table = table; 00076 this.constraint = constraint; 00077 table.nameConstraint(constraint); 00078 this.global = global; 00079 this.tableName = table.getName(); 00080 } 00081 00082 /** 00083 * Constructor for a non-global constraint 00084 */ 00085 public AddConstraint(Session session, Table table, Constraint constraint) { 00086 this(session, table, constraint, false); 00087 } 00088 00089 /** 00090 * Lazy table accessor 00091 */ 00092 Table getTable(Database db) throws IOException { 00093 if (table == null) { 00094 table = (Table)db.getRelation(tableName); 00095 } 00096 return table; 00097 } 00098 00099 /** 00100 * LogStep.undo(): delete the constraint; perform any constraint-specified 00101 * cleanup, and remove the constraint from the table. 00102 */ 00103 public void undo(Session session) throws IOException, SQLException { 00104 Database db = session.getDatabase(); 00105 getTable(db); 00106 constraint = table.getConstraint(constraint.getName()); 00107 // Generally, we will have a constraint here. But if things have 00108 // already gone badly, the constraint might not have actually been 00109 // added. So don't freak out if you can't find it. 00110 if (constraint != null) { 00111 table.deleteConstraint(constraint.getName()); 00112 constraint.undoAdd(session); 00113 db.updateRelation(table); 00114 db.deleteIndex(constraint.getName()); 00115 } 00116 } 00117 00118 /** 00119 * LogStep.redo(): add the constrtaint, perform any constraint-specific 00120 * initialization, and update the table with the new constraint 00121 */ 00122 public void redo(Session session) throws IOException, SQLException { 00123 Database db = session.getDatabase(); 00124 getTable(db); 00125 table.addConstraint(constraint); 00126 constraint.add(session); 00127 db.updateRelation(table); 00128 db.addIndex(constraint.getName(), tableName); 00129 } 00130 00131 /** 00132 * LogStep.prepare(): A good constraint prepares his table, anyway 00133 */ 00134 public void prepare(Session session) throws IOException, SQLException { 00135 constraint.setTable(getTable(session.getDatabase())); 00136 } 00137 00138 /** 00139 * Read me from a stream 00140 */ 00141 public void readExternal(ObjectInput in) 00142 throws IOException, ClassNotFoundException 00143 { 00144 super.readExternal(in); 00145 this.constraint = (Constraint)in.readObject(); 00146 this.tableName = (String)in.readObject(); 00147 this.global = in.read() == 1; 00148 } 00149 00150 /** 00151 * Write me to a stream 00152 */ 00153 public void writeExternal(ObjectOutput out) throws IOException { 00154 super.writeExternal(out); 00155 out.writeObject(constraint); 00156 out.writeObject(tableName); 00157 out.write(global ? 1 : 0); 00158 } 00159 00160 /** 00161 * My class's Extern object 00162 */ 00163 static Extern extern; 00164 public void setExtern(Extern extern) { AddConstraint.extern = extern; } 00165 public Extern getExtern() { return extern; } 00166 00167 //#ifdef DEBUG 00168 public String toString() { 00169 StringBuffer sb = new StringBuffer(super.toString()); 00170 sb.append(" AddConstraint("); 00171 sb.append(tableName); 00172 sb.append(','); 00173 sb.append(constraint == null ? "null" : constraint.toString()); 00174 return sb.toString(); 00175 } 00176 //#endif 00177 00178 }