1 package com.quadcap.sql; 2 3 40 41 import java.io.IOException ; 42 43 import java.util.Vector ; 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.Btree; 51 import com.quadcap.sql.index.BCursor; 52 53 import com.quadcap.sql.types.Value; 54 import com.quadcap.sql.types.ValueLong; 55 import com.quadcap.sql.types.ValueNull; 56 57 import com.quadcap.util.Debug; 58 59 64 public class StmtAddColumn implements Stmt { 65 String tableName; 66 Column column; 67 String aname; 68 69 72 public StmtAddColumn(String tableName, Column column, String aname) { 73 this.tableName = tableName; 74 this.column = column; 75 this.aname = aname; 76 } 77 78 84 public void execute(Session session) throws IOException , SQLException { 85 Database db = session.getDatabase(); 87 session.getTableWriteLock("#Schema"); 88 session.getTableWriteLock(tableName); 89 90 Table table = (Table)db.getRelation(tableName); 92 if (table == null) { 93 throw new SQLException ("Table not found: " + tableName); 94 } 95 96 if (table.getColumnIndex(column.getName()) >= 1) { 97 throw new SQLException ("Add Column: a column named '" + 98 column.getName() + 99 "' already exists int table '" + 100 tableName + "'"); 101 } 102 103 int newpos = table.getColumnCount() + 1; 104 if (aname != null) { 105 if (aname.equals("")) { 106 newpos = 1; 107 } else { 108 newpos = table.getColumnIndex(aname) + 1; 109 if (newpos <= 0) { 110 throw new SQLException ("Bad column name: " + aname); 111 } 112 } 113 } 114 column.setColumn(newpos); 115 116 BCursor bc = null; 118 try { 119 long id = 0; 120 BlockFile file = db.getFile(); 121 Expression defaultExpr = column.getDefault(); 122 Value columnDefault = 123 defaultExpr == null ? ValueNull.valueNull 124 : defaultExpr.getValue(session, null); 125 126 IndexConstraint ic = table.getAnyIndex(session); 127 Btree index = ic.getIndex(db); 128 bc = index.getCursor(false); 129 LazyRow row1 = new LazyRow(table.getColumnCount()); 130 Row row2 = new Row(table.getColumnCount() + 1); 131 while (bc.next()) { 132 long rowId = bc.getValAsLong(); 133 db.getRow(rowId, row1, false); 134 int offset = 0; 135 for (int i = 1; i <= row2.size(); i++) { 136 if (i == newpos) { 137 if (column.isAutoIncrement()) { 138 columnDefault = new ValueLong(++id); 139 } 140 row2.set(i, columnDefault); 141 offset = 1; 142 } else { 143 row2.set(i, row1.item(i - offset)); 144 } 145 } 146 UpdateRow update = null; 147 if (db.inMemory()) { 148 update = new UpdateRow(session, rowId, row1, row2); 149 row2 = new Row(table.getColumnCount() + 1); 150 row1 = new LazyRow(table.getColumnCount()); 151 } else { 152 byte[] oldRowBytes = row1.getBytes(); 153 byte[] rowBytes = LazyRow.writeRow(session, null, row2); 154 update = new UpdateRow(session, table, rowId, 155 oldRowBytes, rowBytes); 156 } 157 session.doStep(update); 158 } 159 if (column.isAutoIncrement()) { 160 session.doStep(new AutoNumberStep(session, table, id+1)); 161 } 162 } finally { 163 if (bc != null) bc.release(); 164 } 165 166 session.doStep(new AddColumn(session, table, column, newpos)); 168 169 Vector cv = column.getConstraints(); 170 if (cv != null) for (int i = 0; i < cv.size(); i++) { 171 Constraint constraint = (Constraint)cv.elementAt(i); 172 session.doStep(new AddConstraint(session, table, constraint)); 173 } 174 175 table.resetColumnConstraints(); 176 } 177 } 178 | Popular Tags |