Public Member Functions | |
CreateRequest (String sqlQuery, boolean escapeProcessing, int timeout, String lineSeparator, DatabaseSchema schema, int granularity, boolean isCaseSensitive) throws SQLException | |
CreateRequest (String sqlQuery, boolean escapeProcessing, int timeout, String lineSeparator) | |
void | parse (DatabaseSchema schema, int granularity, boolean isCaseSensitive) throws SQLException |
void | cloneParsing (AbstractRequest request) |
boolean | isCreate () |
boolean | isInsert () |
boolean | isUpdate () |
boolean | isDelete () |
boolean | isDrop () |
DatabaseTable | getDatabaseTable () |
ArrayList | getFromTables () |
void | debug () |
boolean | isAlter () |
CreateRequest
is a SQL request of the following syntax:
CREATE [TEMPORARY] TABLE table-name [(column-name column-type [,column-name colum-type]* [,table-constraint-definition]*)]
Definition at line 49 of file CreateRequest.java.
|
Creates a new If the syntax is incorrect an exception is thrown.
Definition at line 78 of file CreateRequest.java. References org.objectweb.cjdbc.common.sql.CreateRequest.parse(). 00081 { 00082 this(sqlQuery, escapeProcessing, timeout, lineSeparator); 00083 parse(schema, granularity, isCaseSensitive); 00084 }
|
|
Creates a new The request is not parsed but it can be done later by a call to parse(DatabaseSchema, int, boolean).
Definition at line 101 of file CreateRequest.java. 00103 { 00104 super(sqlQuery, escapeProcessing, timeout, lineSeparator); 00105 cacheable = RequestType.UNCACHEABLE; 00106 isParsed = false; 00107 }
|
|
Implements org.objectweb.cjdbc.common.sql.AbstractRequest. Definition at line 328 of file CreateRequest.java. References org.objectweb.cjdbc.common.sql.AbstractWriteRequest.cloneTableNameAndColumns(), org.objectweb.cjdbc.common.sql.CreateRequest.getDatabaseTable(), org.objectweb.cjdbc.common.sql.CreateRequest.getFromTables(), and org.objectweb.cjdbc.common.sql.AbstractRequest.isParsed. 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 }
|
|
Displays some debugging information about this request. Reimplemented from org.objectweb.cjdbc.common.sql.AbstractRequest. Definition at line 408 of file CreateRequest.java. 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 }
|
|
Gets the database table created by this statement.
Definition at line 389 of file CreateRequest.java. Referenced by org.objectweb.cjdbc.common.sql.CreateRequest.cloneParsing(). 00390 {
00391 return table;
00392 }
|
|
Returns the list of tables used to fill the created table in case of create query containing a select.
Definition at line 400 of file CreateRequest.java. Referenced by org.objectweb.cjdbc.common.sql.CreateRequest.cloneParsing(). 00401 {
00402 return fromTables;
00403 }
|
|
Implements org.objectweb.cjdbc.common.sql.AbstractWriteRequest. Definition at line 432 of file CreateRequest.java. 00433 { 00434 return false; 00435 }
|
|
Implements org.objectweb.cjdbc.common.sql.AbstractWriteRequest. Definition at line 343 of file CreateRequest.java. 00344 { 00345 return true; 00346 }
|
|
Implements org.objectweb.cjdbc.common.sql.AbstractWriteRequest. Definition at line 370 of file CreateRequest.java. 00371 { 00372 return false; 00373 }
|
|
Implements org.objectweb.cjdbc.common.sql.AbstractWriteRequest. Definition at line 379 of file CreateRequest.java. 00380 { 00381 return false; 00382 }
|
|
Implements org.objectweb.cjdbc.common.sql.AbstractWriteRequest. Definition at line 352 of file CreateRequest.java. 00353 { 00354 return false; 00355 }
|
|
Implements org.objectweb.cjdbc.common.sql.AbstractWriteRequest. Definition at line 361 of file CreateRequest.java. 00362 { 00363 return false; 00364 }
|
|
Implements org.objectweb.cjdbc.common.sql.AbstractRequest. Definition at line 113 of file CreateRequest.java. References org.objectweb.cjdbc.common.sql.schema.TableColumn.getColumnName(), org.objectweb.cjdbc.common.sql.SelectRequest.getFrom(), org.objectweb.cjdbc.common.sql.AbstractRequest.getLineSeparator(), org.objectweb.cjdbc.common.sql.SelectRequest.getSelect(), org.objectweb.cjdbc.common.sql.SelectRequest.parse(), and org.objectweb.cjdbc.common.sql.schema.DatabaseColumn.setIsUnique(). Referenced by org.objectweb.cjdbc.common.sql.CreateRequest.CreateRequest(). 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 // Strip 'create [temporary] table ' 00126 int tableIdx = sql.indexOf("table"); 00127 sql = sql.substring(tableIdx + 5).trim(); 00128 00129 // Does the query contain a select? 00130 int selectIdx = sql.indexOf("select"); 00131 if (selectIdx != -1 && sql.charAt(selectIdx + 6) != ' ') 00132 selectIdx = -1; 00133 00134 if (isCaseSensitive) // Reverse to the original case 00135 sql = originalSQL.substring(originalSQL.length() - sql.length()); 00136 00137 if (selectIdx != -1) 00138 { 00139 // Get the table on which CREATE occurs 00140 int nextSpaceIdx = sql.indexOf(" "); 00141 tableName = sql.substring(0, nextSpaceIdx).trim(); 00142 table = new DatabaseTable(tableName); 00143 // Parse the select 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 { // Update the columns and add them to the table 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 // Get the table on which CREATE occurs 00163 // Look for the parenthesis 00164 int openParenthesisIdx = sql.indexOf("("); 00165 int closeParenthesisIdx = sql.lastIndexOf(")"); 00166 if ((openParenthesisIdx == -1) && (closeParenthesisIdx == -1)) 00167 { 00168 // no parenthesis found 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 // Get the column names 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 // work around to prevent bug: if the request contains for example: 00203 // INDEX foo (col1,col2) 00204 // we have to merge the 2 tokens: 'INDEX foo (col1' and 'col2)' 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 // First word of the line: either a column name or 00219 // a table constraint definition 00220 wordTokens = new StringTokenizer(token, " "); 00221 word = wordTokens.nextToken().trim(); 00222 lowercaseWord = word.toLowerCase(); 00223 00224 // If it's a constraint, index or check keyword do not do anything 00225 // else parse the line 00226 if (!lowercaseWord.equals("constraint") 00227 && !lowercaseWord.equals("index") 00228 && !lowercaseWord.equals("check")) 00229 { 00230 String columnName; 00231 boolean isUnique = false; 00232 // Check for primary key or unique constraint 00233 if (lowercaseWord.equals("primary") 00234 || lowercaseWord.startsWith("unique")) 00235 { 00236 00237 // Get the name of the column 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 // Set this column to unique 00271 col = table.getColumn(columnName); 00272 00273 // Test first if dbTable contains this column. This can fail with 00274 // some invalid request, for example: 00275 // CREATE TABLE categories(id INT4, name TEXT, PRIMARY KEY((id)) 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 // It's a column name 00289 columnName = word; 00290 00291 if (!wordTokens.hasMoreTokens()) 00292 { 00293 // at least type declaration is required 00294 tableName = null; 00295 columns = null; 00296 throw new SQLException( 00297 "Syntax error in this CREATE statement: '" + sqlQuery + "'"); 00298 } 00299 00300 // Check for primary key or unique constraints 00301 do 00302 { 00303 word = wordTokens.nextToken().trim().toLowerCase(); 00304 if (word.equals("primary") || word.startsWith("unique")) 00305 { 00306 // Create the column as unique 00307 isUnique = true; 00308 break; 00309 } 00310 } 00311 while (wordTokens.hasMoreTokens()); 00312 00313 // Add the column to the parsed columns list and 00314 // to the create DatabaseTable 00315 columns.add(new TableColumn(tableName, columnName)); 00316 table.addColumn(new DatabaseColumn(columnName, isUnique)); 00317 } 00318 } 00319 } 00320 } 00321 } 00322 isParsed = true; 00323 }
|