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.io.Serializable;
00028 import java.sql.SQLException;
00029 import java.util.ArrayList;
00030 import java.util.StringTokenizer;
00031
00032 import org.objectweb.cjdbc.common.sql.schema.DatabaseColumn;
00033 import org.objectweb.cjdbc.common.sql.schema.DatabaseSchema;
00034 import org.objectweb.cjdbc.common.sql.schema.DatabaseTable;
00035 import org.objectweb.cjdbc.common.sql.schema.TableColumn;
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 public class CreateRequest extends AbstractWriteRequest implements Serializable
00050 {
00051
00052 private transient DatabaseTable table = null;
00053
00054
00055
00056
00057
00058 private transient ArrayList fromTables = null;
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 public CreateRequest(String sqlQuery, boolean escapeProcessing, int timeout,
00079 String lineSeparator, DatabaseSchema schema, int granularity,
00080 boolean isCaseSensitive) throws SQLException
00081 {
00082 this(sqlQuery, escapeProcessing, timeout, lineSeparator);
00083 parse(schema, granularity, isCaseSensitive);
00084 }
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 public CreateRequest(String sqlQuery, boolean escapeProcessing, int timeout,
00102 String lineSeparator)
00103 {
00104 super(sqlQuery, escapeProcessing, timeout, lineSeparator);
00105 cacheable = RequestType.UNCACHEABLE;
00106 isParsed = false;
00107 }
00108
00109
00110
00111
00112
00113 public void parse(DatabaseSchema schema, int granularity,
00114 boolean isCaseSensitive) throws SQLException
00115 {
00116 if (granularity == ParsingGranularities.NO_PARSING)
00117 {
00118 isParsed = true;
00119 return;
00120 }
00121
00122 String originalSQL = this.trimCarriageReturn();
00123 String sql = originalSQL.toLowerCase();
00124
00125
00126 int tableIdx = sql.indexOf("table");
00127 sql = sql.substring(tableIdx + 5).trim();
00128
00129
00130 int selectIdx = sql.indexOf("select");
00131 if (selectIdx != -1 && sql.charAt(selectIdx + 6) != ' ')
00132 selectIdx = -1;
00133
00134 if (isCaseSensitive)
00135 sql = originalSQL.substring(originalSQL.length() - sql.length());
00136
00137 if (selectIdx != -1)
00138 {
00139
00140 int nextSpaceIdx = sql.indexOf(" ");
00141 tableName = sql.substring(0, nextSpaceIdx).trim();
00142 table = new DatabaseTable(tableName);
00143
00144 sql = sql.substring(selectIdx).trim();
00145 SelectRequest select = new SelectRequest(sql, false, 60,
00146 getLineSeparator());
00147 select.parse(schema, granularity, isCaseSensitive);
00148 fromTables = select.getFrom();
00149 if (granularity > ParsingGranularities.TABLE)
00150 {
00151 columns = select.getSelect();
00152 int size = columns.size();
00153 for (int i = 0; i < size; i++)
00154 {
00155 TableColumn tc = (TableColumn) columns.get(i);
00156 table.addColumn(new DatabaseColumn(tc.getColumnName(), false));
00157 }
00158 }
00159 }
00160 else
00161 {
00162
00163
00164 int openParenthesisIdx = sql.indexOf("(");
00165 int closeParenthesisIdx = sql.lastIndexOf(")");
00166 if ((openParenthesisIdx == -1) && (closeParenthesisIdx == -1))
00167 {
00168
00169 table = new DatabaseTable(sql.trim());
00170 if (granularity > ParsingGranularities.TABLE)
00171 columns = new ArrayList();
00172 return;
00173 }
00174 else if ((openParenthesisIdx == -1) || (closeParenthesisIdx == -1)
00175 || (openParenthesisIdx > closeParenthesisIdx))
00176 {
00177 throw new SQLException("Syntax error in this CREATE statement: '"
00178 + sqlQuery + "'");
00179 }
00180 else
00181 {
00182 tableName = sql.substring(0, openParenthesisIdx).trim();
00183 }
00184 table = new DatabaseTable(tableName);
00185
00186
00187 if (granularity > ParsingGranularities.TABLE)
00188 {
00189 columns = new ArrayList();
00190 sql = sql.substring(openParenthesisIdx + 1, closeParenthesisIdx).trim();
00191 StringTokenizer columnTokens = new StringTokenizer(sql, ",");
00192 String word;
00193 String lowercaseWord;
00194 StringTokenizer wordTokens = null;
00195 String token;
00196 DatabaseColumn col = null;
00197
00198 while (columnTokens.hasMoreTokens())
00199 {
00200 token = columnTokens.nextToken().trim();
00201
00202
00203
00204
00205 if ((token.indexOf("(") != -1) && (token.indexOf(")") == -1))
00206 {
00207 if (columnTokens.hasMoreTokens())
00208 token = token + "," + columnTokens.nextToken().trim();
00209 else
00210 {
00211 tableName = null;
00212 columns = null;
00213 throw new SQLException("Syntax error in this CREATE statement: '"
00214 + sqlQuery + "'");
00215 }
00216 }
00217
00218
00219
00220 wordTokens = new StringTokenizer(token, " ");
00221 word = wordTokens.nextToken().trim();
00222 lowercaseWord = word.toLowerCase();
00223
00224
00225
00226 if (!lowercaseWord.equals("constraint")
00227 && !lowercaseWord.equals("index")
00228 && !lowercaseWord.equals("check"))
00229 {
00230 String columnName;
00231 boolean isUnique = false;
00232
00233 if (lowercaseWord.equals("primary")
00234 || lowercaseWord.startsWith("unique"))
00235 {
00236
00237
00238 openParenthesisIdx = token.indexOf("(");
00239 closeParenthesisIdx = token.indexOf(")");
00240 if ((openParenthesisIdx == -1) || (closeParenthesisIdx == -1)
00241 || (openParenthesisIdx > closeParenthesisIdx))
00242 {
00243 tableName = null;
00244 columns = null;
00245 throw new SQLException(
00246 "Syntax error in this CREATE statement: '" + sqlQuery + "'");
00247 }
00248
00249 columnName = token.substring(openParenthesisIdx + 1,
00250 closeParenthesisIdx).trim();
00251
00252 int comma;
00253 while ((comma = columnName.indexOf(',')) != -1)
00254 {
00255 String col1 = columnName.substring(0, comma).trim();
00256 col = table.getColumn(col1);
00257 if (col == null)
00258 {
00259 tableName = null;
00260 columns = null;
00261 throw new SQLException(
00262 "Syntax error in this CREATE statement: '" + sqlQuery
00263 + "'");
00264 }
00265 else
00266 col.setIsUnique(true);
00267 columnName = columnName.substring(comma + 1);
00268 }
00269
00270
00271 col = table.getColumn(columnName);
00272
00273
00274
00275
00276 if (col == null)
00277 {
00278 tableName = null;
00279 columns = null;
00280 throw new SQLException(
00281 "Syntax error in this CREATE statement: '" + sqlQuery + "'");
00282 }
00283 else
00284 col.setIsUnique(true);
00285 }
00286 else
00287 {
00288
00289 columnName = word;
00290
00291 if (!wordTokens.hasMoreTokens())
00292 {
00293
00294 tableName = null;
00295 columns = null;
00296 throw new SQLException(
00297 "Syntax error in this CREATE statement: '" + sqlQuery + "'");
00298 }
00299
00300
00301 do
00302 {
00303 word = wordTokens.nextToken().trim().toLowerCase();
00304 if (word.equals("primary") || word.startsWith("unique"))
00305 {
00306
00307 isUnique = true;
00308 break;
00309 }
00310 }
00311 while (wordTokens.hasMoreTokens());
00312
00313
00314
00315 columns.add(new TableColumn(tableName, columnName));
00316 table.addColumn(new DatabaseColumn(columnName, isUnique));
00317 }
00318 }
00319 }
00320 }
00321 }
00322 isParsed = true;
00323 }
00324
00325
00326
00327
00328 public void cloneParsing(AbstractRequest request)
00329 {
00330 if (!request.isParsed())
00331 return;
00332 CreateRequest createRequest = (CreateRequest) request;
00333 cloneTableNameAndColumns((AbstractWriteRequest) request);
00334 table = createRequest.getDatabaseTable();
00335 fromTables = createRequest.getFromTables();
00336 isParsed = true;
00337 }
00338
00339
00340
00341
00342
00343 public boolean isCreate()
00344 {
00345 return true;
00346 }
00347
00348
00349
00350
00351
00352 public boolean isInsert()
00353 {
00354 return false;
00355 }
00356
00357
00358
00359
00360
00361 public boolean isUpdate()
00362 {
00363 return false;
00364 }
00365
00366
00367
00368
00369
00370 public boolean isDelete()
00371 {
00372 return false;
00373 }
00374
00375
00376
00377
00378
00379 public boolean isDrop()
00380 {
00381 return false;
00382 }
00383
00384
00385
00386
00387
00388
00389 public DatabaseTable getDatabaseTable()
00390 {
00391 return table;
00392 }
00393
00394
00395
00396
00397
00398
00399
00400 public ArrayList getFromTables()
00401 {
00402 return fromTables;
00403 }
00404
00405
00406
00407
00408 public void debug()
00409 {
00410 super.debug();
00411 if (tableName != null)
00412 System.out.println("Created table: " + tableName);
00413 else
00414 System.out.println("No information about created table");
00415
00416 if (columns != null)
00417 {
00418 System.out.println("Created columns:");
00419 for (int i = 0; i < columns.size(); i++)
00420 System.out.println(" "
00421 + ((TableColumn) columns.get(i)).getColumnName());
00422 }
00423 else
00424 System.out.println("No information about created columns");
00425
00426 System.out.println();
00427 }
00428
00429
00430
00431
00432 public boolean isAlter()
00433 {
00434 return false;
00435 }
00436 }