1 21 package oracle.toplink.essentials.platform.database; 23 24 import java.io.*; 25 import java.util.*; 26 import oracle.toplink.essentials.exceptions.*; 27 import oracle.toplink.essentials.expressions.*; 28 import oracle.toplink.essentials.internal.expressions.*; 29 import oracle.toplink.essentials.internal.helper.*; 30 import oracle.toplink.essentials.internal.databaseaccess.*; 31 import oracle.toplink.essentials.queryframework.*; 32 import oracle.toplink.essentials.internal.sessions.AbstractSession; 33 34 42 public class SQLServerPlatform extends oracle.toplink.essentials.platform.database.DatabasePlatform { 43 44 48 protected void appendByteArray(byte[] bytes, Writer writer) throws IOException { 49 if (usesNativeSQL() && (!usesByteArrayBinding())) { 50 writer.write("0x"); 51 Helper.writeHexString(bytes, writer); 52 } else { 53 super.appendByteArray(bytes, writer); 54 } 55 } 56 57 62 protected void appendDate(java.sql.Date date, Writer writer) throws IOException { 63 if (usesNativeSQL()) { 64 writer.write("'"); 65 writer.write(Helper.printDate(date)); 66 writer.write("'"); 67 } else { 68 super.appendDate(date, writer); 69 } 70 } 71 72 76 protected void appendSybaseTimestamp(java.sql.Timestamp timestamp, Writer writer) throws IOException { 77 writer.write("'"); 78 writer.write(Helper.printTimestampWithoutNanos(timestamp)); 79 writer.write(':'); 80 81 String nanoString = Integer.toString(timestamp.getNanos()); 84 int numberOfZeros = 0; 85 for (int num = Math.min(9 - nanoString.length(), 3); num > 0; num--) { 86 writer.write('0'); 87 numberOfZeros++; 88 } 89 if ((nanoString.length() + numberOfZeros) > 3) { 90 nanoString = nanoString.substring(0, (3 - numberOfZeros)); 91 } 92 writer.write(nanoString); 93 writer.write("'"); 94 } 95 96 100 protected void appendSybaseCalendar(Calendar calendar, Writer writer) throws IOException { 101 writer.write("'"); 102 writer.write(Helper.printCalendar(calendar)); 103 writer.write("'"); 104 } 105 106 111 protected void appendTime(java.sql.Time time, Writer writer) throws IOException { 112 if (usesNativeSQL()) { 113 writer.write("'"); 114 writer.write(Helper.printTime(time)); 115 writer.write("'"); 116 } else { 117 super.appendTime(time, writer); 118 } 119 } 120 121 126 protected void appendTimestamp(java.sql.Timestamp timestamp, Writer writer) throws IOException { 127 if (usesNativeSQL()) { 128 appendSybaseTimestamp(timestamp, writer); 129 } else { 130 super.appendTimestamp(timestamp, writer); 131 } 132 } 133 134 139 protected void appendCalendar(Calendar calendar, Writer writer) throws IOException { 140 if (usesNativeSQL()) { 141 appendSybaseCalendar(calendar, writer); 142 } else { 143 super.appendCalendar(calendar, writer); 144 } 145 } 146 147 protected Hashtable buildFieldTypes() { 148 Hashtable fieldTypeMapping; 149 150 fieldTypeMapping = new Hashtable(); 151 fieldTypeMapping.put(Boolean .class, new FieldTypeDefinition("BIT default 0", false)); 152 153 fieldTypeMapping.put(Integer .class, new FieldTypeDefinition("INTEGER", false)); 154 fieldTypeMapping.put(Long .class, new FieldTypeDefinition("NUMERIC", 19)); 155 fieldTypeMapping.put(Float .class, new FieldTypeDefinition("FLOAT(16)", false)); 156 fieldTypeMapping.put(Double .class, new FieldTypeDefinition("FLOAT(32)", false)); 157 fieldTypeMapping.put(Short .class, new FieldTypeDefinition("SMALLINT", false)); 158 fieldTypeMapping.put(Byte .class, new FieldTypeDefinition("SMALLINT", false)); 159 fieldTypeMapping.put(java.math.BigInteger .class, new FieldTypeDefinition("NUMERIC", 28)); 160 fieldTypeMapping.put(java.math.BigDecimal .class, new FieldTypeDefinition("NUMERIC", 28).setLimits(28, -19, 19)); 161 fieldTypeMapping.put(Number .class, new FieldTypeDefinition("NUMERIC", 28).setLimits(28, -19, 19)); 162 163 fieldTypeMapping.put(String .class, new FieldTypeDefinition("VARCHAR", 255)); 164 fieldTypeMapping.put(Character .class, new FieldTypeDefinition("CHAR", 1)); 165 166 fieldTypeMapping.put(Byte [].class, new FieldTypeDefinition("IMAGE", false)); 167 fieldTypeMapping.put(Character [].class, new FieldTypeDefinition("TEXT", false)); 168 fieldTypeMapping.put(byte[].class, new FieldTypeDefinition("IMAGE", false)); 169 fieldTypeMapping.put(char[].class, new FieldTypeDefinition("TEXT", false)); 170 fieldTypeMapping.put(java.sql.Blob .class, new FieldTypeDefinition("IMAGE", false)); 171 fieldTypeMapping.put(java.sql.Clob .class, new FieldTypeDefinition("TEXT", false)); 172 173 fieldTypeMapping.put(java.sql.Date .class, new FieldTypeDefinition("DATETIME", false)); 174 fieldTypeMapping.put(java.sql.Time .class, new FieldTypeDefinition("DATETIME", false)); 175 fieldTypeMapping.put(java.sql.Timestamp .class, new FieldTypeDefinition("DATETIME", false)); 176 177 return fieldTypeMapping; 178 } 179 180 184 public ValueReadQuery buildSelectQueryForNativeSequence() { 185 ValueReadQuery selectQuery = new ValueReadQuery(); 186 StringWriter writer = new StringWriter(); 187 writer.write("SELECT @@IDENTITY"); 188 selectQuery.setSQLString(writer.toString()); 189 return selectQuery; 190 } 191 192 196 public String getBatchDelimiterString() { 197 return ""; 198 } 199 200 205 public String getCreationInOutputProcedureToken() { 206 return getInOutputProcedureToken(); 207 } 208 209 214 public String getCreationOutputProcedureToken() { 215 return "OUTPUT"; 216 } 217 218 223 public String getInOutputProcedureToken() { 224 return "OUT"; 225 } 226 227 232 public int getMaxFieldNameSize() { 233 return 22; 234 } 235 236 242 public Vector getNativeTableInfo(String table, String creator, AbstractSession session) { 243 String query = "SELECT * FROM sysobjects WHERE table_type <> 'SYSTEM_TABLE'"; 245 if (table != null) { 246 if (table.indexOf('%') != -1) { 247 query = query + " AND table_name LIKE " + table; 248 } else { 249 query = query + " AND table_name = " + table; 250 } 251 } 252 if (creator != null) { 253 if (creator.indexOf('%') != -1) { 254 query = query + " AND table_owner LIKE " + creator; 255 } else { 256 query = query + " AND table_owner = " + creator; 257 } 258 } 259 return session.executeSelectingCall(new SQLCall(query)); 260 } 261 262 267 public String getOutputProcedureToken() { 268 return ""; 269 } 270 271 275 public String getProcedureArgumentString() { 276 return "@"; 277 } 278 279 282 public String getSelectForUpdateString() { 283 return " FOR UPDATE"; 284 } 285 286 290 public String getProcedureCallHeader() { 291 return "EXECUTE "; 292 } 293 294 297 public String getStoredProcedureParameterPrefix() { 298 return "@"; 299 } 300 301 305 public String getStoredProcedureTerminationToken() { 306 return " go"; 307 } 308 309 314 public ValueReadQuery getTimestampQuery() { 315 if (timestampQuery == null) { 316 timestampQuery = new ValueReadQuery(); 317 timestampQuery.setSQLString("SELECT GETDATE()"); 318 } 319 return timestampQuery; 320 } 321 322 326 protected void initializePlatformOperators() { 327 super.initializePlatformOperators(); 328 addOperator(operatorOuterJoin()); 329 addOperator(ExpressionOperator.simpleFunction(ExpressionOperator.Today, "GETDATE")); 330 addOperator(ExpressionOperator.simpleFunction(ExpressionOperator.currentDate, "GETDATE")); 333 addOperator(ExpressionOperator.simpleFunction(ExpressionOperator.Length, "CHAR_LENGTH")); 334 addOperator(ExpressionOperator.simpleThreeArgumentFunction(ExpressionOperator.Substring, "SUBSTRING")); 335 addOperator(ExpressionOperator.addDate()); 336 addOperator(ExpressionOperator.dateName()); 337 addOperator(ExpressionOperator.datePart()); 338 addOperator(ExpressionOperator.dateDifference()); 339 addOperator(ExpressionOperator.difference()); 340 addOperator(ExpressionOperator.charIndex()); 341 addOperator(ExpressionOperator.charLength()); 342 addOperator(ExpressionOperator.reverse()); 343 addOperator(ExpressionOperator.replicate()); 344 addOperator(ExpressionOperator.right()); 345 addOperator(ExpressionOperator.cot()); 346 addOperator(ExpressionOperator.sybaseAtan2Operator()); 347 addOperator(ExpressionOperator.sybaseAddMonthsOperator()); 348 addOperator(ExpressionOperator.sybaseInStringOperator()); 349 addOperator(ExpressionOperator.simpleTwoArgumentFunction(ExpressionOperator.Nvl, "ISNULL")); 351 addOperator(ExpressionOperator.sybaseToNumberOperator()); 353 addOperator(ExpressionOperator.sybaseToDateToStringOperator()); 354 addOperator(ExpressionOperator.sybaseToDateOperator()); 355 addOperator(ExpressionOperator.sybaseToCharOperator()); 356 addOperator(ExpressionOperator.sybaseLocateOperator()); 357 addOperator(locate2Operator()); 358 addOperator(ExpressionOperator.simpleFunction(ExpressionOperator.Ceil, "CEILING")); 359 addOperator(ExpressionOperator.simpleFunction(ExpressionOperator.Length, "LEN")); 360 addOperator(modOperator()); 361 } 362 363 public boolean isSQLServer() { 364 return true; 365 } 366 367 373 public Hashtable maximumNumericValues() { 374 Hashtable values = new Hashtable(); 375 376 values.put(Integer .class, new Integer (Integer.MAX_VALUE)); 377 values.put(Long .class, new Long (Long.MAX_VALUE)); 378 values.put(Double .class, new Double (0)); 379 values.put(Short .class, new Short (Short.MAX_VALUE)); 380 values.put(Byte .class, new Byte (Byte.MAX_VALUE)); 381 values.put(Float .class, new Float (0)); 382 values.put(java.math.BigInteger .class, new java.math.BigInteger ("9999999999999999999999999999")); 383 values.put(java.math.BigDecimal .class, new java.math.BigDecimal ("999999999.9999999999999999999")); 384 return values; 385 } 386 387 393 public Hashtable minimumNumericValues() { 394 Hashtable values = new Hashtable(); 395 396 values.put(Integer .class, new Integer (Integer.MIN_VALUE)); 397 values.put(Long .class, new Long (Long.MIN_VALUE)); 398 values.put(Double .class, new Double ((double)-9)); 399 values.put(Short .class, new Short (Short.MIN_VALUE)); 400 values.put(Byte .class, new Byte (Byte.MIN_VALUE)); 401 values.put(Float .class, new Float ((float)-9)); 402 values.put(java.math.BigInteger .class, new java.math.BigInteger ("-9999999999999999999999999999")); 403 values.put(java.math.BigDecimal .class, new java.math.BigDecimal ("-999999999.9999999999999999999")); 404 return values; 405 } 406 407 411 public ExpressionOperator modOperator() { 412 ExpressionOperator result = new ExpressionOperator(); 413 result.setSelector(ExpressionOperator.Mod); 414 Vector v = new Vector(); 415 v.addElement(" % "); 416 result.printsAs(v); 417 result.bePostfix(); 418 result.setNodeClass(oracle.toplink.essentials.internal.expressions.FunctionExpression.class); 419 return result; 420 } 421 422 426 protected ExpressionOperator operatorOuterJoin() { 427 ExpressionOperator result = new ExpressionOperator(); 428 result.setSelector(ExpressionOperator.EqualOuterJoin); 429 Vector v = new Vector(); 430 v.addElement(" =* "); 431 result.printsAs(v); 432 result.bePostfix(); 433 result.setNodeClass(RelationExpression.class); 434 return result; 435 } 436 437 441 public static ExpressionOperator locate2Operator() { 442 ExpressionOperator result = ExpressionOperator.simpleThreeArgumentFunction(ExpressionOperator.Locate2, "CHARINDEX"); 443 int[] argumentIndices = new int[3]; 444 argumentIndices[0] = 1; 445 argumentIndices[1] = 0; 446 argumentIndices[2] = 2; 447 result.setArgumentIndices(argumentIndices); 448 return result; 449 } 450 451 452 453 457 public void printFieldIdentityClause(Writer writer) throws ValidationException { 458 try { 459 writer.write(" IDENTITY"); 460 } catch (IOException ioException) { 461 throw ValidationException.fileError(ioException); 462 } 463 } 464 465 469 public void printFieldNullClause(Writer writer) throws ValidationException { 470 try { 471 writer.write(" NULL"); 472 } catch (IOException ioException) { 473 throw ValidationException.fileError(ioException); 474 } 475 } 476 477 481 public boolean requiresProcedureCallBrackets() { 482 return false; 483 } 484 485 489 public boolean requiresProcedureCallOuputToken() { 490 return true; 491 } 492 493 498 public boolean shouldPrintInOutputTokenBeforeType() { 499 return false; 500 } 501 502 506 public boolean shouldPrintOuterJoinInWhereClause() { 507 return false; 508 } 509 510 515 public boolean shouldPrintOutputTokenBeforeType() { 516 return false; 517 } 518 519 525 public boolean shouldNativeSequenceAcquireValueAfterInsert() { 526 return true; 527 } 528 529 533 public boolean shouldUseJDBCOuterJoinSyntax() { 534 return false; 535 } 536 537 542 public boolean supportsNativeSequenceNumbers() { 543 return true; 544 } 545 546 549 public boolean supportsLocalTempTables() { 550 return true; 551 } 552 553 556 protected String getCreateTempTableSqlPrefix() { 557 return "CREATE TABLE "; 558 } 559 560 563 public DatabaseTable getTempTableForTable(DatabaseTable table) { 564 return new DatabaseTable("#" + table.getName(), table.getTableQualifier()); 565 } 566 567 570 public void writeUpdateOriginalFromTempTableSql(Writer writer, DatabaseTable table, 571 Collection pkFields, 572 Collection assignedFields) throws IOException 573 { 574 writer.write("UPDATE "); 575 String tableName = table.getQualifiedName(); 576 writer.write(tableName); 577 String tempTableName = getTempTableForTable(table).getQualifiedName(); 578 writeAutoAssignmentSetClause(writer, null, tempTableName, assignedFields); 579 writer.write(" FROM "); 580 writer.write(tableName); 581 writer.write(", "); 582 writer.write(tempTableName); 583 writeAutoJoinWhereClause(writer, tableName, tempTableName, pkFields); 584 } 585 } 586 | Popular Tags |