KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
42
43 import java.util.List JavaDoc;
44
45 import java.sql.SQLException JavaDoc;
46
47 import com.quadcap.sql.file.BlockFile;
48 import com.quadcap.sql.file.ByteUtil;
49
50 import com.quadcap.sql.index.BCursor;
51 import com.quadcap.sql.index.Btree;
52
53 import com.quadcap.sql.types.Value;
54 import com.quadcap.sql.types.ValueNull;
55
56 import com.quadcap.util.Debug;
57
58 /**
59  * Implementation of SQL <b>ALTER TABLE DROP COLUMN</b> statement.
60  *
61  * @author Stan Bailes
62  */

63 public class StmtDropColumn implements Stmt {
64     String JavaDoc tableName;
65     String JavaDoc colName;
66
67     /**
68      * Explicit constructor from table name and column name
69      */

70     public StmtDropColumn(String JavaDoc tableName, String JavaDoc colName) {
71     this.tableName = tableName;
72     this.colName = colName;
73     }
74
75     /**
76      * Delete any constraints which reference the specified column.
77      * Reset all other constraint column maps.
78      *
79      * @param col one-based column number
80      */

81     public void deleteColumnConstraints(Session session, Table table, int col)
82         throws SQLException JavaDoc, IOException JavaDoc
83     {
84         int num = table.getNumConstraints();
85
86         // Copy constraints for stable enumeration
87
Constraint[] tmpC = new Constraint[num];
88         for (int i = 0; i < num; i++) {
89         tmpC[i] = table.getConstraint(i);
90         }
91         for (int i = 0; i < num; i++) {
92         Constraint con = tmpC[i];
93         int[] cols = con.getColumns();
94         boolean match = false;
95         for (int j = 0; j < cols.length && !match; j++) {
96         match = cols[j] == col;
97         }
98         if (match) {
99         session.doStep(new DeleteConstraint(session, table, con));
100         } else {
101                 con.resetColumns();
102             }
103     }
104     }
105
106     /**
107      * Stmt.execute()
108      *
109      * For DROP COLUMN, we need to update the table definition, of course.
110      * But then, we need to update *all* of the rows!
111      */

112     public void execute(Session session) throws IOException JavaDoc, SQLException JavaDoc {
113         // First, acquire the schema and table locks
114
Database db = session.getDatabase();
115         session.getTableWriteLock("#Schema");
116     session.getTableWriteLock(tableName);
117
118     Table table = (Table)db.getRelation(tableName);
119         //#ifdef DEBUG
120
if (table == null) {
121             throw new SQLException JavaDoc("No table: " + tableName);
122         }
123         //#endif
124
Column column = table.getColumn(colName);
125
126         // Drop all the column's constraints first
127
List JavaDoc constraints = column.getConstraints();
128         if (constraints != null) {
129             for (int i = 0; i < constraints.size(); i++) {
130                 Constraint constraint = (Constraint)constraints.get(i);
131                 session.doStep(new DeleteConstraint(session, table,
132                                                     constraint));
133             }
134         }
135         deleteColumnConstraints(session, table, column.getColumn());
136         
137         // Remove the column from the table schema
138
session.doStep(new DropColumn(session, table, column));
139
140         // Update every row in the table with the new column value
141
BCursor bc = null;
142         try {
143             BlockFile file = db.getFile();
144             Expression defaultExpr = column.getDefault();
145             Value columnDefault =
146                 defaultExpr == null
147                 ? ValueNull.valueNull
148                 : defaultExpr.getValue(session, null);
149             
150             IndexConstraint ic = table.getAnyIndex(session);
151             Btree index = ic.getIndex(db);
152             bc = index.getCursor();
153             LazyRow row1 = new LazyRow(table.getColumnCount() + 1);
154             Row row2 = new Row(table.getColumnCount());
155             final int dc = column.getColumn();
156             while (bc.next()) {
157                 long rowId = bc.getValAsLong();
158                 db.getRow(rowId, row1, false);
159                 int del = 0;
160                 for (int i = 1; i <= row1.size(); i++) {
161                     if (i == dc) del++;
162                     else row2.set(i - del, row1.item(i));
163                 }
164                 UpdateRow update = null;
165                 if (db.inMemory()) {
166                     update = new UpdateRow(session, rowId, row1, row2);
167                     row1 = new LazyRow(table.getColumnCount() + 1);
168                     row2 = new Row(table.getColumnCount());
169                 } else {
170                     byte[] oldRowBytes = row1.getBytes();
171                     byte[] rowBytes = LazyRow.writeRow(session, null, row2);
172                     update = new UpdateRow(session, table, rowId,
173                                            oldRowBytes, rowBytes);
174                 }
175                 session.doStep(update);
176             }
177     } finally {
178             if (bc != null) bc.release();
179         }
180     }
181 }
182
Popular Tags