Quadcap Embeddable Database

com/quadcap/sql/StmtAddColumn.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.IOException; 00042 00043 import java.util.Vector; 00044 00045 import java.sql.SQLException; 00046 00047 import com.quadcap.sql.file.BlockFile; 00048 import com.quadcap.sql.file.ByteUtil; 00049 00050 import com.quadcap.sql.index.Btree; 00051 import com.quadcap.sql.index.BCursor; 00052 00053 import com.quadcap.sql.types.Value; 00054 import com.quadcap.sql.types.ValueLong; 00055 import com.quadcap.sql.types.ValueNull; 00056 00057 import com.quadcap.util.Debug; 00058 00059 /** 00060 * Implementation of SQL <b>ADD COLUMN</b> statement. 00061 * 00062 * @author Stan Bailes 00063 */ 00064 public class StmtAddColumn implements Stmt { 00065 String tableName; 00066 Column column; 00067 String aname; 00068 00069 /** 00070 * Explicit constructor from table name and Column 00071 */ 00072 public StmtAddColumn(String tableName, Column column, String aname) { 00073 this.tableName = tableName; 00074 this.column = column; 00075 this.aname = aname; 00076 } 00077 00078 /** 00079 * Stmt.execute() 00080 * 00081 * For ADD COLUMN, we need to update the table definition, of course. 00082 * But then, we need to update *all* of the rows! 00083 */ 00084 public void execute(Session session) throws IOException, SQLException { 00085 // First, acquire the schema and table locks 00086 Database db = session.getDatabase(); 00087 session.getTableWriteLock("#Schema"); 00088 session.getTableWriteLock(tableName); 00089 00090 // Make a cursor from the original schema 00091 Table table = (Table)db.getRelation(tableName); 00092 if (table == null) { 00093 throw new SQLException("Table not found: " + tableName); 00094 } 00095 00096 if (table.getColumnIndex(column.getName()) >= 1) { 00097 throw new SQLException("Add Column: a column named '" + 00098 column.getName() + 00099 "' already exists int table '" + 00100 tableName + "'"); 00101 } 00102 00103 int newpos = table.getColumnCount() + 1; 00104 if (aname != null) { 00105 if (aname.equals("")) { 00106 newpos = 1; 00107 } else { 00108 newpos = table.getColumnIndex(aname) + 1; 00109 if (newpos <= 0) { 00110 throw new SQLException("Bad column name: " + aname); 00111 } 00112 } 00113 } 00114 column.setColumn(newpos); 00115 00116 // Update every row in the table with the new column value 00117 BCursor bc = null; 00118 try { 00119 long id = 0; 00120 BlockFile file = db.getFile(); 00121 Expression defaultExpr = column.getDefault(); 00122 Value columnDefault = 00123 defaultExpr == null ? ValueNull.valueNull 00124 : defaultExpr.getValue(session, null); 00125 00126 IndexConstraint ic = table.getAnyIndex(session); 00127 Btree index = ic.getIndex(db); 00128 bc = index.getCursor(false); 00129 LazyRow row1 = new LazyRow(table.getColumnCount()); 00130 Row row2 = new Row(table.getColumnCount() + 1); 00131 while (bc.next()) { 00132 long rowId = bc.getValAsLong(); 00133 db.getRow(rowId, row1, false); 00134 int offset = 0; 00135 for (int i = 1; i <= row2.size(); i++) { 00136 if (i == newpos) { 00137 if (column.isAutoIncrement()) { 00138 columnDefault = new ValueLong(++id); 00139 } 00140 row2.set(i, columnDefault); 00141 offset = 1; 00142 } else { 00143 row2.set(i, row1.item(i - offset)); 00144 } 00145 } 00146 UpdateRow update = null; 00147 if (db.inMemory()) { 00148 update = new UpdateRow(session, rowId, row1, row2); 00149 row2 = new Row(table.getColumnCount() + 1); 00150 row1 = new LazyRow(table.getColumnCount()); 00151 } else { 00152 byte[] oldRowBytes = row1.getBytes(); 00153 byte[] rowBytes = LazyRow.writeRow(session, null, row2); 00154 update = new UpdateRow(session, table, rowId, 00155 oldRowBytes, rowBytes); 00156 } 00157 session.doStep(update); 00158 } 00159 if (column.isAutoIncrement()) { 00160 session.doStep(new AutoNumberStep(session, table, id+1)); 00161 } 00162 } finally { 00163 if (bc != null) bc.release(); 00164 } 00165 00166 // This changes the table schema 00167 session.doStep(new AddColumn(session, table, column, newpos)); 00168 00169 Vector cv = column.getConstraints(); 00170 if (cv != null) for (int i = 0; i < cv.size(); i++) { 00171 Constraint constraint = (Constraint)cv.elementAt(i); 00172 session.doStep(new AddConstraint(session, table, constraint)); 00173 } 00174 00175 table.resetColumnConstraints(); 00176 } 00177 }