1 24 25 package org.objectweb.cjdbc.common.sql; 26 27 import java.io.IOException ; 28 import java.io.Serializable ; 29 import java.sql.SQLException ; 30 import java.util.ArrayList ; 31 import java.util.StringTokenizer ; 32 33 import org.objectweb.cjdbc.common.sql.schema.DatabaseColumn; 34 import org.objectweb.cjdbc.common.sql.schema.DatabaseSchema; 35 import org.objectweb.cjdbc.common.sql.schema.DatabaseTable; 36 import org.objectweb.cjdbc.common.sql.schema.TableColumn; 37 import org.objectweb.cjdbc.common.stream.CJDBCInputStream; 38 39 51 public class CreateRequest extends AbstractWriteRequest implements Serializable 52 { 53 private static final long serialVersionUID = 2953585153643414325L; 54 55 56 private transient DatabaseTable table = null; 57 58 62 private transient ArrayList fromTables = null; 63 64 73 private boolean alterDatabaseSchema = true; 74 75 84 private boolean alterDefinitions = true; 85 86 104 public CreateRequest(String sqlQuery, boolean escapeProcessing, int timeout, 105 String lineSeparator, DatabaseSchema schema, int granularity, 106 boolean isCaseSensitive) throws SQLException 107 { 108 this(sqlQuery, escapeProcessing, timeout, lineSeparator); 109 parse(schema, granularity, isCaseSensitive); 110 } 111 112 127 public CreateRequest(String sqlQuery, boolean escapeProcessing, int timeout, 128 String lineSeparator) 129 { 130 super(sqlQuery, escapeProcessing, timeout, lineSeparator, 131 RequestType.CREATE); 132 } 133 134 137 public CreateRequest(CJDBCInputStream in) throws IOException 138 { 139 super(in, RequestType.CREATE); 140 } 141 142 146 public void parse(DatabaseSchema schema, int granularity, 147 boolean isCaseSensitive) throws SQLException 148 { 149 if (granularity == ParsingGranularities.NO_PARSING) 150 { 151 isParsed = true; 152 return; 153 } 154 155 String originalSQL = this.trimCarriageReturnAndTabs(); 156 String sql = originalSQL.toLowerCase(); 157 158 sql = sql.substring("create".length()).trim(); 160 161 if (sql.startsWith("database") || sql.startsWith("index") 163 || sql.startsWith("unique") || sql.startsWith("role")) 164 { alterDatabaseSchema = false; 167 alterDefinitions = false; 168 return; 169 } 170 if (sql.startsWith("function") || sql.startsWith("method") 171 || sql.startsWith("procedure") || sql.startsWith("trigger") 172 || sql.startsWith("type")) 173 { alterDatabaseSchema = false; 176 alterDefinitions = true; 177 return; 178 } 179 if (sql.startsWith("schema") || sql.startsWith("view")) 180 { 181 alterDatabaseSchema = true; 182 alterDefinitions = false; 183 return; 184 } 185 186 int tableIdx = sql.indexOf("table"); 188 if (tableIdx < 0) 189 throw new SQLException ("Unsupported CREATE statement: '" + sqlQuery + "'"); 190 191 195 sql = sql.substring(tableIdx + 5).trim(); 197 198 int selectIdx = sql.indexOf("select"); 200 if (selectIdx != -1 && sql.charAt(selectIdx + 6) != ' ') 201 selectIdx = -1; 202 203 if (isCaseSensitive) sql = originalSQL.substring(originalSQL.length() - sql.length()); 205 206 if (selectIdx != -1) 207 { 208 int nextSpaceIdx = sql.indexOf(" "); 210 tableName = sql.substring(0, nextSpaceIdx).trim(); 211 table = new DatabaseTable(tableName); 212 sql = sql.substring(selectIdx).trim(); 214 SelectRequest select = new SelectRequest(sql, false, 60, 215 getLineSeparator()); 216 select.parse(schema, granularity, isCaseSensitive); 217 fromTables = select.getFrom(); 218 if (granularity > ParsingGranularities.TABLE) 219 { columns = select.getSelect(); 221 int size = columns.size(); 222 for (int i = 0; i < size; i++) 223 { 224 TableColumn tc = (TableColumn) columns.get(i); 225 table.addColumn(new DatabaseColumn(tc.getColumnName(), false)); 226 } 227 } 228 } 229 else 230 { 231 int openParenthesisIdx = sql.indexOf("("); 234 int closeParenthesisIdx = sql.lastIndexOf(")"); 235 if ((openParenthesisIdx == -1) && (closeParenthesisIdx == -1)) 236 { 237 table = new DatabaseTable(sql.trim()); 239 if (granularity > ParsingGranularities.TABLE) 240 columns = new ArrayList (); 241 return; 242 } 243 else if ((openParenthesisIdx == -1) || (closeParenthesisIdx == -1) 244 || (openParenthesisIdx > closeParenthesisIdx)) 245 { 246 throw new SQLException ("Syntax error in this CREATE statement: '" 247 + sqlQuery + "'"); 248 } 249 else 250 { 251 tableName = sql.substring(0, openParenthesisIdx).trim(); 252 } 253 table = new DatabaseTable(tableName); 254 255 if (granularity > ParsingGranularities.TABLE) 257 { 258 columns = new ArrayList (); 259 sql = sql.substring(openParenthesisIdx + 1, closeParenthesisIdx).trim(); 260 StringTokenizer columnTokens = new StringTokenizer (sql, ","); 261 String word; 262 String lowercaseWord; 263 StringTokenizer wordTokens = null; 264 String token; 265 DatabaseColumn col = null; 266 267 while (columnTokens.hasMoreTokens()) 268 { 269 token = columnTokens.nextToken().trim(); 270 271 if ((token.indexOf("(") != -1) && (token.indexOf(")") == -1)) 275 { 276 if (columnTokens.hasMoreTokens()) 277 token = token + "," + columnTokens.nextToken().trim(); 278 else 279 { 280 tableName = null; 281 columns = null; 282 throw new SQLException ("Syntax error in this CREATE statement: '" 283 + sqlQuery + "'"); 284 } 285 } 286 287 wordTokens = new StringTokenizer (token, " "); 290 word = wordTokens.nextToken().trim(); 291 lowercaseWord = word.toLowerCase(); 292 293 if (!lowercaseWord.equals("constraint") 296 && !lowercaseWord.equals("index") 297 && !lowercaseWord.equals("check")) 298 { 299 String columnName; 300 boolean isUnique = false; 301 if (lowercaseWord.equals("primary") 303 || lowercaseWord.startsWith("unique")) 304 { 305 306 openParenthesisIdx = token.indexOf("("); 308 closeParenthesisIdx = token.indexOf(")"); 309 if ((openParenthesisIdx == -1) || (closeParenthesisIdx == -1) 310 || (openParenthesisIdx > closeParenthesisIdx)) 311 { 312 tableName = null; 313 columns = null; 314 throw new SQLException ( 315 "Syntax error in this CREATE statement: '" + sqlQuery + "'"); 316 } 317 318 columnName = token.substring(openParenthesisIdx + 1, 319 closeParenthesisIdx).trim(); 320 321 int comma; 322 while ((comma = columnName.indexOf(',')) != -1) 323 { 324 String col1 = columnName.substring(0, comma).trim(); 325 col = table.getColumn(col1); 326 if (col == null) 327 { 328 tableName = null; 329 columns = null; 330 throw new SQLException ( 331 "Syntax error in this CREATE statement: '" + sqlQuery 332 + "'"); 333 } 334 else 335 col.setIsUnique(true); 336 columnName = columnName.substring(comma + 1); 337 } 338 339 col = table.getColumn(columnName); 341 342 if (col == null) 346 { 347 tableName = null; 348 columns = null; 349 throw new SQLException ( 350 "Syntax error in this CREATE statement: '" + sqlQuery + "'"); 351 } 352 else 353 col.setIsUnique(true); 354 } 355 else 356 { 357 columnName = word; 359 360 if (!wordTokens.hasMoreTokens()) 361 { 362 tableName = null; 364 columns = null; 365 throw new SQLException ( 366 "Syntax error in this CREATE statement: '" + sqlQuery + "'"); 367 } 368 369 do 371 { 372 word = wordTokens.nextToken().trim().toLowerCase(); 373 if (word.equals("primary") || word.startsWith("unique")) 374 { 375 isUnique = true; 377 break; 378 } 379 } 380 while (wordTokens.hasMoreTokens()); 381 382 columns.add(new TableColumn(tableName, columnName)); 385 table.addColumn(new DatabaseColumn(columnName, isUnique)); 386 } 387 } 388 } 389 } 390 } 391 isParsed = true; 392 } 393 394 402 public boolean altersDatabaseSchema() 403 { 404 return alterDatabaseSchema; 405 } 406 407 415 public boolean altersDefinitions() 416 { 417 return alterDefinitions; 418 } 419 420 423 public void cloneParsing(AbstractRequest request) 424 { 425 if (!request.isParsed()) 426 return; 427 CreateRequest createRequest = (CreateRequest) request; 428 cloneTableNameAndColumns((AbstractWriteRequest) request); 429 table = createRequest.getDatabaseTable(); 430 fromTables = createRequest.getFromTables(); 431 isParsed = true; 432 } 433 434 440 public DatabaseTable getDatabaseTable() 441 { 442 return table; 443 } 444 445 451 public ArrayList getFromTables() 452 { 453 return fromTables; 454 } 455 456 459 public boolean needsMacroProcessing() 460 { 461 return false; 462 } 463 464 467 public boolean returnsResultSet() 468 { 469 return false; 470 } 471 472 475 public void debug() 476 { 477 super.debug(); 478 if (tableName != null) 479 System.out.println("Created table: " + tableName); 480 else 481 System.out.println("No information about created table"); 482 483 if (columns != null) 484 { 485 System.out.println("Created columns:"); 486 for (int i = 0; i < columns.size(); i++) 487 System.out.println(" " 488 + ((TableColumn) columns.get(i)).getColumnName()); 489 } 490 else 491 System.out.println("No information about created columns"); 492 493 System.out.println(); 494 } 495 496 } | Popular Tags |