00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package org.objectweb.cjdbc.common.sql;
00026
00027 import java.sql.SQLException;
00028 import java.util.ArrayList;
00029
00030 import org.objectweb.cjdbc.common.sql.schema.DatabaseColumn;
00031 import org.objectweb.cjdbc.common.sql.schema.DatabaseSchema;
00032 import org.objectweb.cjdbc.common.sql.schema.DatabaseTable;
00033 import org.objectweb.cjdbc.common.sql.schema.TableColumn;
00034
00035
00036
00037
00038
00039
00040
00041 public class AlterRequest extends AbstractWriteRequest
00042 {
00043
00044
00045 private transient DatabaseTable table = null;
00046
00047
00048 private transient DatabaseColumn column = null;
00049
00050 private transient boolean isDrop = false;
00051 private transient boolean isAdd = false;
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 public AlterRequest(String sqlQuery, boolean escapeProcessing, int timeout,
00069 String lineSeparator)
00070 {
00071 super(sqlQuery, escapeProcessing, timeout, lineSeparator);
00072 }
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 public AlterRequest(String sqlQuery, boolean escapeProcessing, int timeout,
00093 String lineSeparator, DatabaseSchema schema, int granularity,
00094 boolean isCaseSensitive) throws SQLException
00095 {
00096 this(sqlQuery, escapeProcessing, timeout, lineSeparator);
00097 parse(schema, granularity, isCaseSensitive);
00098 }
00099
00100
00101
00102
00103 public boolean isReadRequest()
00104 {
00105 return false;
00106 }
00107
00108
00109
00110
00111 public boolean isWriteRequest()
00112 {
00113 return true;
00114 }
00115
00116
00117
00118
00119 public boolean isUnknownRequest()
00120 {
00121 return false;
00122 }
00123
00124
00125
00126
00127
00128 public void parse(DatabaseSchema schema, int granularity,
00129 boolean isCaseSensitive) throws SQLException
00130 {
00131
00132
00133
00134
00135
00136 if (granularity == ParsingGranularities.NO_PARSING)
00137 {
00138 isParsed = true;
00139 return;
00140 }
00141
00142 String originalSQL = this.trimCarriageReturn();
00143 String sql = originalSQL.toLowerCase();
00144
00145
00146 int tableIdx = sql.indexOf("table");
00147 if (tableIdx == -1)
00148 throw new SQLException(
00149 "Malformed Alter Request. Should start with [ALTER TABLE]");
00150 sql = sql.substring(tableIdx + 5).trim();
00151
00152
00153 int addIdx = sql.indexOf(" add ");
00154
00155
00156 int dropIdx = sql.indexOf(" drop ");
00157
00158 if (addIdx != -1)
00159 isAdd = true;
00160 if (dropIdx != -1)
00161 isDrop = true;
00162
00163 if (!isAdd && !isDrop)
00164 throw new SQLException(
00165 "Malformed Alter Request. No drop or add condition");
00166
00167 if (isCaseSensitive)
00168 sql = originalSQL.substring(originalSQL.length() - sql.length());
00169
00170 int index = (isAdd) ? addIdx : dropIdx;
00171
00172 tableName = sql.substring(0, index).trim();
00173 table = new DatabaseTable(tableName);
00174
00175
00176 if (granularity > ParsingGranularities.TABLE)
00177 {
00178
00179
00180 int subsIndex = index + 6 + 2;
00181
00182
00183 if (isAdd)
00184 subsIndex += 3;
00185 else
00186
00187 subsIndex += 4;
00188
00189 columns = new ArrayList();
00190 sql = sql.substring(subsIndex).trim();
00191
00192 if (isAdd)
00193 {
00194 int colIndex = sql.indexOf(' ');
00195 String colName = sql.substring(0, colIndex);
00196
00197 int uniqueIndex = sql.toLowerCase().indexOf("unique");
00198 int primary = sql.toLowerCase().indexOf("primary");
00199 if (uniqueIndex != -1 || primary != -1)
00200 column = new DatabaseColumn(colName, true);
00201 else
00202 column = new DatabaseColumn(colName, false);
00203 columns.add(new TableColumn(tableName, colName));
00204 }
00205 else if (isDrop)
00206 {
00207 String colName = sql.trim();
00208 column = schema.getTable(tableName).getColumn(colName);
00209 columns.add(new TableColumn(tableName, colName));
00210 }
00211 }
00212 isParsed = true;
00213 }
00214
00215
00216
00217
00218 public void cloneParsing(AbstractRequest request)
00219 {
00220 if (!request.isParsed())
00221 return;
00222 AlterRequest alterRequest = (AlterRequest) request;
00223 cloneTableNameAndColumns((AbstractWriteRequest) request);
00224 table = alterRequest.getDatabaseTable();
00225 column = alterRequest.getColumn();
00226 isParsed = true;
00227 }
00228
00229
00230
00231
00232 public boolean isAlter()
00233 {
00234 return true;
00235 }
00236
00237
00238
00239
00240 public boolean isCreate()
00241 {
00242 return false;
00243 }
00244
00245
00246
00247
00248 public boolean isDelete()
00249 {
00250 return false;
00251 }
00252
00253
00254
00255
00256 public boolean isDrop()
00257 {
00258 return false;
00259 }
00260
00261
00262
00263
00264 public boolean isInsert()
00265 {
00266 return false;
00267 }
00268
00269
00270
00271
00272 public boolean isUpdate()
00273 {
00274 return false;
00275 }
00276
00277
00278
00279
00280
00281
00282 public DatabaseTable getDatabaseTable()
00283 {
00284 return table;
00285 }
00286
00287
00288
00289
00290
00291 public DatabaseColumn getColumn()
00292 {
00293 return column;
00294 }
00295
00296
00297
00298
00299
00300 public boolean isAdd()
00301 {
00302 return isAdd;
00303 }
00304 }