1 package com.quadcap.sql; 2 3 40 41 import java.io.IOException ; 42 43 import java.util.List ; 44 45 import java.sql.SQLException ; 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 63 public class StmtDropColumn implements Stmt { 64 String tableName; 65 String colName; 66 67 70 public StmtDropColumn(String tableName, String colName) { 71 this.tableName = tableName; 72 this.colName = colName; 73 } 74 75 81 public void deleteColumnConstraints(Session session, Table table, int col) 82 throws SQLException , IOException 83 { 84 int num = table.getNumConstraints(); 85 86 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 112 public void execute(Session session) throws IOException , SQLException { 113 Database db = session.getDatabase(); 115 session.getTableWriteLock("#Schema"); 116 session.getTableWriteLock(tableName); 117 118 Table table = (Table)db.getRelation(tableName); 119 if (table == null) { 121 throw new SQLException ("No table: " + tableName); 122 } 123 Column column = table.getColumn(colName); 125 126 List 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 session.doStep(new DropColumn(session, table, column)); 139 140 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 |