00001
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
00048 public class CreateRequest extends AbstractWriteRequest implements Serializable
00049 {
00051 private transient DatabaseTable
table = null;
00052
00057 private transient ArrayList
fromTables = null;
00058
00077 public CreateRequest(String sqlQuery,
boolean escapeProcessing,
int timeout,
00078 String lineSeparator,
DatabaseSchema schema,
int granularity,
00079
boolean isCaseSensitive)
throws SQLException
00080 {
00081
this(sqlQuery, escapeProcessing, timeout, lineSeparator);
00082
parse(schema, granularity, isCaseSensitive);
00083 }
00084
00100 public CreateRequest(String sqlQuery,
boolean escapeProcessing,
int timeout,
00101 String lineSeparator)
00102 {
00103 super(sqlQuery, escapeProcessing, timeout, lineSeparator);
00104 cacheable =
RequestType.UNCACHEABLE;
00105 isParsed =
false;
00106 }
00107
00112 public void parse(
DatabaseSchema schema,
int granularity,
00113
boolean isCaseSensitive)
throws SQLException
00114 {
00115
if (granularity ==
ParsingGranularities.NO_PARSING)
00116 {
00117 isParsed =
true;
00118
return;
00119 }
00120
00121 String originalSQL =
this.trimCarriageReturn();
00122 String sql = originalSQL.toLowerCase();
00123
00124
00125
int tableIdx = sql.indexOf(
"table");
00126 sql = sql.substring(tableIdx + 5).trim();
00127
00128
00129
int selectIdx = sql.indexOf(
"select");
00130
00131
if (isCaseSensitive)
00132 sql = originalSQL.substring(originalSQL.length() - sql.length());
00133
00134
if (selectIdx != -1)
00135 {
00136
00137
int nextSpaceIdx = sql.indexOf(
" ");
00138 tableName = sql.substring(0, nextSpaceIdx).trim();
00139
table =
new DatabaseTable(tableName);
00140
00141 sql = sql.substring(selectIdx).trim();
00142
SelectRequest select =
new SelectRequest(sql,
false, 60,
00143
getLineSeparator());
00144 select.
parse(schema, granularity, isCaseSensitive);
00145
fromTables = select.
getFrom();
00146
if (granularity >
ParsingGranularities.TABLE)
00147 {
00148 columns = select.
getSelect();
00149 }
00150
00151 }
00152
else
00153 {
00154
00155
00156
int openParenthesisIdx = sql.indexOf(
"(");
00157
int closeParenthesisIdx = sql.lastIndexOf(
")");
00158
if ((openParenthesisIdx == -1) && (closeParenthesisIdx == -1))
00159 {
00160
00161
table =
new DatabaseTable(sql.trim());
00162
if (granularity >
ParsingGranularities.TABLE)
00163 {
00164 columns =
new ArrayList();
00165 }
00166
return;
00167 }
00168
else if ((openParenthesisIdx == -1) || (closeParenthesisIdx == -1)
00169 || (openParenthesisIdx > closeParenthesisIdx))
00170 {
00171
throw new SQLException(
"Syntax error in this CREATE statement: '"
00172 + sqlQuery +
"'");
00173 }
00174
else
00175 {
00176 tableName = sql.substring(0, openParenthesisIdx).trim();
00177 }
00178
table =
new DatabaseTable(tableName);
00179
00180
00181
if (granularity >
ParsingGranularities.TABLE)
00182 {
00183 columns =
new ArrayList();
00184 sql = sql.substring(openParenthesisIdx + 1, closeParenthesisIdx).trim();
00185 StringTokenizer columnTokens =
new StringTokenizer(sql,
",");
00186 String word;
00187 String lowercaseWord;
00188 StringTokenizer wordTokens = null;
00189 String token;
00190
DatabaseColumn col = null;
00191
00192
while (columnTokens.hasMoreTokens())
00193 {
00194 token = columnTokens.nextToken().trim();
00195
00196
00197
00198
00199
if ((token.indexOf(
"(") != -1) && (token.indexOf(
")") == -1))
00200 {
00201
if (columnTokens.hasMoreTokens())
00202 token = token +
"," + columnTokens.nextToken().trim();
00203
else
00204 {
00205 tableName = null;
00206 columns = null;
00207
throw new SQLException(
"Syntax error in this CREATE statement: '"
00208 + sqlQuery +
"'");
00209 }
00210 }
00211
00212
00213
00214 wordTokens =
new StringTokenizer(token,
" ");
00215 word = wordTokens.nextToken().trim();
00216 lowercaseWord = word.toLowerCase();
00217
00218
00219
00220
if (!lowercaseWord.equals(
"constraint")
00221 && !lowercaseWord.equals(
"index")
00222 && !lowercaseWord.equals(
"check"))
00223 {
00224 String columnName;
00225
boolean isUnique =
false;
00226
00227
if (lowercaseWord.equals(
"primary")
00228 || lowercaseWord.startsWith(
"unique"))
00229 {
00230
00231
00232 openParenthesisIdx = token.indexOf(
"(");
00233 closeParenthesisIdx = token.indexOf(
")");
00234
if ((openParenthesisIdx == -1) || (closeParenthesisIdx == -1)
00235 || (openParenthesisIdx > closeParenthesisIdx))
00236 {
00237 tableName = null;
00238 columns = null;
00239
throw new SQLException(
00240
"Syntax error in this CREATE statement: '" + sqlQuery +
"'");
00241 }
00242
00243 columnName = token.substring(openParenthesisIdx + 1,
00244 closeParenthesisIdx).trim();
00245
00246
00247 col =
table.getColumn(columnName);
00248
00249
00250
00251
if (col == null)
00252 {
00253 tableName = null;
00254 columns = null;
00255
throw new SQLException(
00256
"Syntax error in this CREATE statement: '" + sqlQuery +
"'");
00257 }
00258
else
00259 col.
setIsUnique(
true);
00260 }
00261
else
00262 {
00263
00264 columnName = word;
00265
00266
if (!wordTokens.hasMoreTokens())
00267 {
00268
00269 tableName = null;
00270 columns = null;
00271
throw new SQLException(
00272
"Syntax error in this CREATE statement: '" + sqlQuery +
"'");
00273 }
00274
00275
00276
do
00277 {
00278 word = wordTokens.nextToken().trim().toLowerCase();
00279
if (word.equals(
"primary") || word.startsWith(
"unique"))
00280 {
00281
00282 isUnique =
true;
00283
break;
00284 }
00285 }
00286
while (wordTokens.hasMoreTokens());
00287
00288
00289
00290 columns.add(
new TableColumn(tableName, columnName));
00291
table.addColumn(
new DatabaseColumn(columnName, isUnique));
00292 }
00293 }
00294 }
00295 }
00296 }
00297 isParsed =
true;
00298 }
00299
00303 public void cloneParsing(
AbstractRequest request)
00304 {
00305
if (!request.
isParsed())
00306
return;
00307
CreateRequest createRequest = (
CreateRequest) request;
00308
cloneTableNameAndColumns((
AbstractWriteRequest) request);
00309
table = createRequest.
getDatabaseTable();
00310
fromTables = createRequest.
getFromTables();
00311 isParsed =
true;
00312 }
00313
00318 public boolean isCreate()
00319 {
00320
return true;
00321 }
00322
00327 public boolean isInsert()
00328 {
00329
return false;
00330 }
00331
00336 public boolean isUpdate()
00337 {
00338
return false;
00339 }
00340
00345 public boolean isDelete()
00346 {
00347
return false;
00348 }
00349
00354 public boolean isDrop()
00355 {
00356
return false;
00357 }
00358
00364 public DatabaseTable
getDatabaseTable()
00365 {
00366
return table;
00367 }
00368
00375 public ArrayList
getFromTables()
00376 {
00377
return fromTables;
00378 }
00379
00383 public void debug()
00384 {
00385 super.debug();
00386
if (tableName != null)
00387 System.out.println(
"Created table: " + tableName);
00388
else
00389 System.out.println(
"No information about created table");
00390
00391
if (columns != null)
00392 {
00393 System.out.println(
"Created columns:");
00394
for (
int i = 0; i < columns.size(); i++)
00395 System.out.println(
" "
00396 + ((
TableColumn) columns.get(i)).getColumnName());
00397 }
00398
else
00399 System.out.println(
"No information about created columns");
00400
00401 System.out.println();
00402 }
00406 public boolean isAlter()
00407 {
00408
return false;
00409 }
00410 }