1 21 package oracle.toplink.essentials.platform.database.oracle; 23 24 import java.util.*; 25 import java.io.*; 26 import java.sql.*; 27 import oracle.toplink.essentials.exceptions.*; 28 import oracle.toplink.essentials.queryframework.*; 29 import oracle.toplink.essentials.internal.helper.*; 30 import oracle.toplink.essentials.expressions.*; 31 import oracle.toplink.essentials.internal.expressions.*; 32 import oracle.toplink.essentials.internal.databaseaccess.*; 33 import oracle.toplink.essentials.internal.sessions.AbstractSession; 34 35 44 public class OraclePlatform extends oracle.toplink.essentials.platform.database.DatabasePlatform { 45 46 50 public boolean allowsSizeInProcedureArguments() { 51 return false; 52 } 53 54 59 protected void appendByteArray(byte[] bytes, Writer writer) throws IOException { 60 if (usesNativeSQL()) { 61 writer.write('\''); 62 Helper.writeHexString(bytes, writer); 63 writer.write('\''); 64 } else { 65 super.appendByteArray(bytes, writer); 66 } 67 } 68 69 74 protected void appendDate(java.sql.Date date, Writer writer) throws IOException { 75 if (usesNativeSQL()) { 76 writer.write("to_date('"); 77 writer.write(Helper.printDate(date)); 78 writer.write("','yyyy-mm-dd')"); 79 } else { 80 super.appendDate(date, writer); 81 } 82 } 83 84 89 protected void appendTime(java.sql.Time time, Writer writer) throws IOException { 90 if (usesNativeSQL()) { 91 writer.write("to_date('"); 92 writer.write(Helper.printTime(time)); 93 writer.write("','hh24:mi:ss')"); 94 } else { 95 super.appendTime(time, writer); 96 } 97 } 98 99 104 protected void appendTimestamp(java.sql.Timestamp timestamp, Writer writer) throws IOException { 105 if (usesNativeSQL()) { 106 writer.write("to_date('"); 107 writer.write(Helper.printTimestampWithoutNanos(timestamp)); 108 writer.write("','yyyy-mm-dd hh24:mi:ss')"); 109 } else { 110 super.appendTimestamp(timestamp, writer); 111 } 112 } 113 114 119 protected void appendCalendar(Calendar calendar, Writer writer) throws IOException { 120 if (usesNativeSQL()) { 121 writer.write("to_date('"); 122 writer.write(Helper.printCalendarWithoutNanos(calendar)); 123 writer.write("','yyyy-mm-dd hh24:mi:ss')"); 124 } else { 125 super.appendCalendar(calendar, writer); 126 } 127 } 128 129 133 public ExpressionOperator atan2Operator() { 134 return ExpressionOperator.simpleTwoArgumentFunction(ExpressionOperator.Atan2, "ATAN2"); 135 } 136 137 protected Hashtable buildFieldTypes() { 138 Hashtable fieldTypeMapping; 139 140 fieldTypeMapping = new Hashtable(); 141 fieldTypeMapping.put(Boolean .class, new FieldTypeDefinition("NUMBER(1) default 0", false)); 142 143 fieldTypeMapping.put(Integer .class, new FieldTypeDefinition("NUMBER", 10)); 144 fieldTypeMapping.put(Long .class, new FieldTypeDefinition("NUMBER", 19)); 145 fieldTypeMapping.put(Float .class, new FieldTypeDefinition("NUMBER", 19, 4)); 146 fieldTypeMapping.put(Double .class, new FieldTypeDefinition("NUMBER", 19, 4)); 147 fieldTypeMapping.put(Short .class, new FieldTypeDefinition("NUMBER", 5)); 148 fieldTypeMapping.put(Byte .class, new FieldTypeDefinition("NUMBER", 3)); 149 fieldTypeMapping.put(java.math.BigInteger .class, new FieldTypeDefinition("NUMBER", 38)); 150 fieldTypeMapping.put(java.math.BigDecimal .class, new FieldTypeDefinition("NUMBER", 38).setLimits(38, -38, 38)); 151 fieldTypeMapping.put(Number .class, new FieldTypeDefinition("NUMBER", 38).setLimits(38, -38, 38)); 152 153 fieldTypeMapping.put(String .class, new FieldTypeDefinition("VARCHAR2", 255)); 154 fieldTypeMapping.put(Character .class, new FieldTypeDefinition("CHAR", 1)); 155 156 fieldTypeMapping.put(Byte [].class, new FieldTypeDefinition("LONG RAW", false)); 157 fieldTypeMapping.put(Character [].class, new FieldTypeDefinition("LONG", false)); 158 fieldTypeMapping.put(byte[].class, new FieldTypeDefinition("LONG RAW", false)); 159 fieldTypeMapping.put(char[].class, new FieldTypeDefinition("LONG", false)); 160 fieldTypeMapping.put(java.sql.Blob .class, new FieldTypeDefinition("BLOB", false)); 161 fieldTypeMapping.put(java.sql.Clob .class, new FieldTypeDefinition("CLOB", false)); 162 163 fieldTypeMapping.put(java.sql.Date .class, new FieldTypeDefinition("DATE", false)); 164 fieldTypeMapping.put(java.sql.Time .class, new FieldTypeDefinition("TIMESTAMP", false)); 165 fieldTypeMapping.put(java.sql.Timestamp .class, new FieldTypeDefinition("TIMESTAMP", false)); 166 167 return fieldTypeMapping; 168 } 169 170 174 public DatabaseCall buildCallWithReturning(SQLCall sqlCall, Vector returnFields) { 175 SQLCall call = new SQLCall(); 176 call.setParameters(sqlCall.getParameters()); 177 call.setParameterTypes(sqlCall.getParameterTypes()); 178 179 Writer writer = new CharArrayWriter(200); 180 try { 181 writer.write("BEGIN "); 182 writer.write(sqlCall.getSQLString()); 183 writer.write(" RETURNING "); 184 185 for (int i = 0; i < returnFields.size(); i++) { 186 DatabaseField field = (DatabaseField)returnFields.elementAt(i); 187 writer.write(field.getName()); 188 if ((i + 1) < returnFields.size()) { 189 writer.write(", "); 190 } 191 } 192 193 writer.write(" INTO "); 194 195 for (int i = 0; i < returnFields.size(); i++) { 196 DatabaseField field = (DatabaseField)returnFields.elementAt(i); 197 call.appendOut(writer, field); 198 if ((i + 1) < returnFields.size()) { 199 writer.write(", "); 200 } 201 } 202 203 writer.write("; END;"); 204 205 call.setQueryString(writer.toString()); 206 207 } catch (IOException exception) { 208 throw ValidationException.fileError(exception); 209 } 210 211 return call; 212 } 213 214 220 public boolean canBuildCallWithReturning() { 221 return true; 222 } 223 224 228 public String getAssignmentString() { 229 return ":= "; 230 } 231 232 236 public String getBatchBeginString() { 237 return "BEGIN "; 238 } 239 240 244 public String getBatchEndString() { 245 return "END;"; 246 } 247 248 253 public String getJDBCOuterJoinString() { 254 return "{'oj "; 255 } 256 257 262 public int getMaxFieldNameSize() { 263 return 30; 264 } 265 266 272 public Vector getNativeTableInfo(String table, String creator, AbstractSession session) { 273 String query = "SELECT * FROM ALL_TABLES WHERE OWNER NOT IN ('SYS', 'SYSTEM')"; 274 if (table != null) { 275 if (table.indexOf('%') != -1) { 276 query = query + " AND TABLE_NAME LIKE " + table; 277 } else { 278 query = query + " AND TABLE_NAME = " + table; 279 } 280 } 281 if (creator != null) { 282 if (creator.indexOf('%') != -1) { 283 query = query + " AND OWNER LIKE " + creator; 284 } else { 285 query = query + " AND OWNER = " + creator; 286 } 287 } 288 return session.executeSelectingCall(new oracle.toplink.essentials.queryframework.SQLCall(query)); 289 } 290 291 295 public String getProcedureArgumentSetter() { 296 return "=>"; 297 } 298 299 303 public String getProcedureCallHeader() { 304 return "BEGIN "; 305 } 306 307 311 public String getProcedureCallTail() { 312 return "; END;"; 313 } 314 315 318 public String getSelectForUpdateString() { 319 return " FOR UPDATE"; 320 } 321 322 325 public String getStoredProcedureParameterPrefix() { 326 return "P_"; 327 } 328 329 332 public String getStoredProcedureTerminationToken() { 333 return ""; 334 } 335 336 343 public ValueReadQuery getSystemChangeNumberQuery() { 344 ValueReadQuery sCNQuery = new ValueReadQuery(); 345 sCNQuery.setSQLString("SELECT DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER FROM DUAL"); 346 return sCNQuery; 347 } 348 349 354 public ValueReadQuery getTimestampQuery() { 355 if (timestampQuery == null) { 356 timestampQuery = new ValueReadQuery(); 357 timestampQuery.setSQLString("SELECT SYSDATE FROM DUAL"); 358 } 359 return timestampQuery; 360 } 361 362 368 public Object getObjectFromResultSet(ResultSet resultSet, int columnNumber, int type) throws java.sql.SQLException { 369 if ((type == Types.TIMESTAMP) || (type == Types.DATE)) { 371 return resultSet.getTimestamp(columnNumber); 372 } else { 373 return resultSet.getObject(columnNumber); 374 } 375 } 376 377 381 protected void initializePlatformOperators() { 382 super.initializePlatformOperators(); 383 addOperator(operatorOuterJoin()); 384 addOperator(logOperator()); 385 addOperator(ExpressionOperator.simpleTwoArgumentFunction(ExpressionOperator.Concat, "CONCAT")); 386 addOperator(todayOperator()); 387 addOperator(currentDateOperator()); 388 addOperator(ExpressionOperator.truncateDate()); 389 addOperator(ExpressionOperator.newTime()); 390 addOperator(ExpressionOperator.ifNull()); 391 addOperator(atan2Operator()); 392 addOperator(ExpressionOperator.oracleDateName()); 393 addOperator(operatorLocate()); 394 addOperator(operatorLocate2()); 395 } 396 397 public boolean isOracle() { 398 return true; 399 } 400 401 405 protected ExpressionOperator logOperator() { 406 ExpressionOperator result = new ExpressionOperator(); 407 result.setSelector(ExpressionOperator.Log); 408 Vector v = new Vector(2); 409 v.addElement("LOG("); 410 v.addElement(", 10)"); 411 result.printsAs(v); 412 result.bePrefix(); 413 result.setNodeClass(FunctionExpression.class); 414 return result; 415 416 } 417 418 424 public Hashtable maximumNumericValues() { 425 Hashtable values = new Hashtable(); 426 427 values.put(Integer .class, new Integer (Integer.MAX_VALUE)); 428 values.put(Long .class, new Long (Long.MAX_VALUE)); 429 values.put(Double .class, new Double ((double)9.9999E125)); 430 values.put(Short .class, new Short (Short.MAX_VALUE)); 431 values.put(Byte .class, new Byte (Byte.MAX_VALUE)); 432 values.put(Float .class, new Float (Float.MAX_VALUE)); 433 values.put(java.math.BigInteger .class, new java.math.BigInteger ("0")); 434 values.put(java.math.BigDecimal .class, new java.math.BigDecimal (new java.math.BigInteger ("0"), 38)); 435 return values; 436 } 437 438 444 public Hashtable minimumNumericValues() { 445 Hashtable values = new Hashtable(); 446 447 values.put(Integer .class, new Integer (Integer.MIN_VALUE)); 448 values.put(Long .class, new Long (Long.MIN_VALUE)); 449 values.put(Double .class, new Double ((double)-1E-129)); 450 values.put(Short .class, new Short (Short.MIN_VALUE)); 451 values.put(Byte .class, new Byte (Byte.MIN_VALUE)); 452 values.put(Float .class, new Float (Float.MIN_VALUE)); 453 values.put(java.math.BigInteger .class, new java.math.BigInteger ("0")); 454 values.put(java.math.BigDecimal .class, new java.math.BigDecimal (new java.math.BigInteger ("0"), 38)); 455 return values; 456 } 457 458 464 public ValueReadQuery buildSelectQueryForNativeSequence(String seqName, Integer size) { 465 return new ValueReadQuery("SELECT " + getQualifiedSequenceName(seqName) + ".NEXTVAL FROM DUAL"); 466 } 467 468 472 protected String getQualifiedSequenceName(String seqName) { 473 if (getTableQualifier().equals("")) { 474 return seqName; 475 } else { 476 return getTableQualifier() + "." + seqName; 477 } 478 } 479 480 484 protected ExpressionOperator operatorOuterJoin() { 485 ExpressionOperator result = new ExpressionOperator(); 486 result.setSelector(ExpressionOperator.EqualOuterJoin); 487 Vector v = new Vector(2); 488 v.addElement(" (+) = "); 489 result.printsAs(v); 490 result.bePostfix(); 491 result.setNodeClass(RelationExpression.class); 492 return result; 493 494 } 495 496 500 protected ExpressionOperator operatorLocate() { 501 ExpressionOperator result = new ExpressionOperator(); 502 result.setSelector(ExpressionOperator.Locate); 503 Vector v = new Vector(3); 504 v.addElement("INSTR("); 505 v.addElement(", "); 506 v.addElement(")"); 507 result.printsAs(v); 508 result.bePrefix(); 509 result.setNodeClass(RelationExpression.class); 510 return result; 511 } 512 513 517 protected ExpressionOperator operatorLocate2() { 518 ExpressionOperator result = new ExpressionOperator(); 519 result.setSelector(ExpressionOperator.Locate2); 520 Vector v = new Vector(4); 521 v.addElement("INSTR("); 522 v.addElement(", "); 523 v.addElement(", "); 524 v.addElement(")"); 525 result.printsAs(v); 526 result.bePrefix(); 527 result.setNodeClass(RelationExpression.class); 528 return result; 529 } 530 531 535 public void printFieldNullClause(Writer writer) throws ValidationException { 536 try { 537 writer.write(" NULL"); 538 } catch (IOException ioException) { 539 throw ValidationException.fileError(ioException); 540 } 541 } 542 543 547 public String serverTimestampString() { 548 return "SYSDATE"; 549 } 550 551 555 public boolean shouldPrintOuterJoinInWhereClause() { 556 return true; 557 } 558 559 563 public boolean shouldUseJDBCOuterJoinSyntax() { 564 return false; 565 } 566 567 572 public boolean supportsNativeSequenceNumbers() { 573 return true; 574 } 575 576 579 public boolean supportsStoredFunctions() { 580 return true; 581 } 582 583 588 public boolean supportsSelectForUpdateNoWait() { 589 return true; 590 } 591 592 596 protected ExpressionOperator todayOperator() { 597 return ExpressionOperator.simpleFunctionNoParentheses(ExpressionOperator.Today, "SYSDATE"); 598 } 599 600 protected ExpressionOperator currentDateOperator() { 601 return ExpressionOperator.simpleFunctionNoParentheses(ExpressionOperator.currentDate, "TO_DATE(CURRENT_DATE)"); 602 } 603 604 } 605 | Popular Tags |