KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > StmtRenameTable


1 package com.quadcap.sql;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.Externalizable JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.ObjectInput JavaDoc;
44 import java.io.ObjectOutput JavaDoc;
45
46 import java.util.ArrayList JavaDoc;
47 import java.util.Enumeration JavaDoc;
48 import java.util.HashMap JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.Map JavaDoc;
51
52 import java.sql.SQLException JavaDoc;
53
54 import com.quadcap.sql.file.BlockFile;
55 import com.quadcap.sql.file.ByteUtil;
56
57 import com.quadcap.sql.index.Btree;
58 import com.quadcap.sql.index.BCursor;
59
60 import com.quadcap.sql.io.Extern;
61
62 import com.quadcap.util.Debug;
63
64 /**
65  * Implementation of the SQL <b>ALTER TABLE RENAME TO</b> statement.
66  *
67  * @author Stan Bailes
68  */

69
70 public class StmtRenameTable extends LogStep implements Stmt, Externalizable JavaDoc {
71     String JavaDoc oldName;
72     String JavaDoc newName;
73     Relation r;
74
75     public StmtRenameTable() {}
76     
77     public StmtRenameTable(String JavaDoc oldName, String JavaDoc newName) {
78     this.oldName = oldName;
79         this.newName = newName;
80     }
81
82     public void execute(Session session) throws IOException JavaDoc, SQLException JavaDoc {
83     session.getTableWriteLock("#Schema");
84     session.getTableWriteLock(oldName);
85     session.getTableWriteLock(newName);
86         session.doStep(this);
87     }
88
89     public void redo(Session session) throws IOException JavaDoc, SQLException JavaDoc {
90         rename(session, oldName, newName);
91     }
92
93     public void undo(Session session) throws IOException JavaDoc, SQLException JavaDoc {
94         rename(session, newName, oldName);
95     }
96
97     public void rename(Session session, String JavaDoc oldN, String JavaDoc newN)
98         throws IOException JavaDoc, SQLException JavaDoc
99     {
100     Database db = session.getDatabase();
101         Relation oldR = db.getRelation(oldN);
102     if (oldR == null) {
103         throw new SQLException JavaDoc("no such table: " + oldN, "42000");
104     }
105         db.renameRelation(oldR, newN);
106         Relation newR = db.getRelation(newN);
107         if (newR == null) {
108             throw new SQLException JavaDoc("rename failed!: " + newN);
109         }
110             
111     Enumeration JavaDoc views = db.getViews(newN);
112         while (views.hasMoreElements()) {
113             String JavaDoc view1 = views.nextElement().toString();
114             renameView(session, view1, oldN, newN);
115         }
116
117         if (newR instanceof Table) {
118             Map JavaDoc referencingTables = new HashMap JavaDoc();
119             Table t = (Table)newR;
120             int num = t.getNumConstraints();
121             for (int i = 0; i < num; i++) {
122                 Constraint c = t.getConstraint(i);
123                 if (c instanceof ForeignKeyConstraint) {
124                     ForeignKeyConstraint fc = (ForeignKeyConstraint)c;
125                     String JavaDoc fTable = fc.getFTableName();
126                     referencingTables.put(fTable, "");
127                 }
128             }
129             if (referencingTables.size() > 0) {
130                 Iterator JavaDoc iter = referencingTables.keySet().iterator();
131                 while (iter.hasNext()) {
132                     renameForeignKeys(session, iter.next().toString(), oldN, newN);
133                 }
134                 db.updateRelation(newR);
135             }
136     }
137     }
138     
139     void renameForeignKeys(Session session, String JavaDoc tableName, String JavaDoc oldN,
140                            String JavaDoc newN)
141         throws IOException JavaDoc, SQLException JavaDoc
142     {
143         session.getTableReadLock(tableName);
144         Database db = session.getDatabase();
145         Table t = (Table)db.getRelation(tableName);
146         int num = t.getNumConstraints();
147         for (int i = 0; i < num; i++) {
148             Constraint c = t.getConstraint(i);
149             if (c instanceof ForeignKeyConstraint) {
150                 ForeignKeyConstraint fc = (ForeignKeyConstraint)c;
151                 String JavaDoc fTable = fc.getFTableName();
152                 if (fTable.equals(oldN)) {
153                     fc.setFTableName(newN);
154                     fc.resetColumns();
155                 }
156             }
157         }
158         db.updateRelation(t);
159     }
160
161     class RenameTableVisitor implements ExpressionVisitor {
162         String JavaDoc oldN;
163         String JavaDoc newN;
164         RenameTableVisitor(String JavaDoc oldN, String JavaDoc newN) {
165             this.oldN = oldN;
166             this.newN = newN;
167         }
168         public void visit(Expression ex) {
169             if (ex instanceof SelectFromTable) {
170                 SelectFromTable sf = (SelectFromTable)ex;
171                 String JavaDoc tableName = sf.getTableName();
172                 if (tableName.equals(oldN)) {
173                     sf.setTableName(newN);
174                 }
175             } else if (ex instanceof SelectExpression) {
176                 Iterator JavaDoc iter = ((SelectExpression)ex).getFrom().iterator();
177                 while (iter.hasNext()) {
178                     visit((Expression)iter.next());
179                 }
180             }
181             ex.visitSubExpressions(this);
182         }
183     }
184     
185     void renameView(Session session, String JavaDoc viewName, String JavaDoc oldN,
186                     String JavaDoc newN)
187         throws IOException JavaDoc, SQLException JavaDoc
188     {
189
190         session.getTableReadLock(viewName);
191
192         Database db = session.getDatabase();
193         View v = (View)db.getRelation(viewName);
194         Expression viewEx = v.getViewExpression();
195         RenameTableVisitor visitor = new RenameTableVisitor(oldN, newN);
196         visitor.visit(viewEx);
197         db.updateRelation(v);
198     }
199
200     public void prepare(Session session) throws IOException JavaDoc, SQLException JavaDoc {
201     }
202
203     public void readExternal(ObjectInput JavaDoc in)
204     throws IOException JavaDoc, ClassNotFoundException JavaDoc
205     {
206     oldName = (String JavaDoc)in.readObject();
207     newName = (String JavaDoc)in.readObject();
208     }
209     
210     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
211     out.writeObject(oldName);
212     out.writeObject(newName);
213     }
214
215     //#ifdef DEBUG
216
/**
217      * Return a displayable representation for debugging
218      */

219     public String JavaDoc toString() {
220     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(super.toString());
221     sb.append(" RenameTable(");
222         sb.append(oldName);
223         sb.append(" to ");
224         sb.append(newName);
225     sb.append(')');
226     return sb.toString();
227     }
228     //#endif
229

230     static Extern extern;
231     public void setExtern(Extern extern) { StmtRenameTable.extern = extern; }
232     public Extern getExtern() { return extern; }
233 }
234
Popular Tags