00001
package com.quadcap.sql;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
00061
00062
00063
00064 public class StmtAddColumn implements Stmt {
00065 String
tableName;
00066 Column column;
00067 String
aname;
00068
00069
00070
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
00080
00081
00082
00083
00084 public void execute(
Session session)
throws IOException, SQLException {
00085
00086
Database db = session.getDatabase();
00087 session.getTableWriteLock(
"#Schema");
00088 session.getTableWriteLock(
tableName);
00089
00090
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
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
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 }