1 21 package oracle.toplink.essentials.internal.databaseaccess; 23 24 import java.io.*; 25 import java.util.*; 26 import oracle.toplink.essentials.exceptions.*; 27 import oracle.toplink.essentials.queryframework.*; 28 import oracle.toplink.essentials.internal.helper.*; 29 import oracle.toplink.essentials.expressions.*; 30 import oracle.toplink.essentials.sequencing.*; 31 import oracle.toplink.essentials.internal.sessions.AbstractSession; 32 import oracle.toplink.essentials.sessions.DatabaseSession; 33 34 45 public class DatasourcePlatform implements Platform { 46 47 48 protected String tableQualifier; 49 50 51 protected transient ConversionManager conversionManager; 52 53 54 protected ValueReadQuery timestampQuery; 55 56 57 protected transient Map platformOperators; 58 59 60 protected Hashtable dataTypesConvertedFromAClass; 61 62 63 protected Hashtable dataTypesConvertedToAClass; 64 65 66 protected Sequence defaultSequence; 67 68 69 protected Map sequences; 70 71 public DatasourcePlatform() { 72 this.tableQualifier = ""; 73 } 74 75 protected void addOperator(ExpressionOperator operator) { 76 platformOperators.put(new Integer (operator.getSelector()), operator); 77 } 78 79 83 public void appendParameter(Call call, Writer writer, Object parameter) { 84 String parameterValue = (String )getConversionManager().convertObject(parameter, ClassConstants.STRING); 85 if (parameterValue == null) { 86 parameterValue = ""; 87 } 88 try { 89 writer.write(parameterValue); 90 } catch (IOException exception) { 91 throw ValidationException.fileError(exception); 92 } 93 } 94 95 98 public Object getCustomModifyValueForCall(Call call, Object value, DatabaseField field, boolean shouldBind) { 99 return value; 100 } 101 102 110 public boolean shouldUseCustomModifyForCall(DatabaseField field) { 111 return false; 112 } 113 114 public Object clone() { 115 try { 116 DatasourcePlatform clone = (DatasourcePlatform)super.clone(); 117 clone.sequencesAfterCloneCleanup(); 118 return clone; 119 } catch (CloneNotSupportedException exception) { 120 ; } 122 123 return null; 124 } 125 126 protected void sequencesAfterCloneCleanup() { 127 Sequence defaultSequenceClone = null; 128 if (hasDefaultSequence()) { 129 defaultSequenceClone = (Sequence)getDefaultSequence().clone(); 130 setDefaultSequence(defaultSequenceClone); 131 } 132 HashMap sequencesDeepClone = null; 133 if (getSequences() != null) { 134 sequencesDeepClone = new HashMap(getSequences().size()); 135 Iterator it = getSequences().values().iterator(); 136 while (it.hasNext()) { 137 Sequence sequence = (Sequence)it.next(); 138 if ((defaultSequenceClone != null) && (sequence == getDefaultSequence())) { 139 sequencesDeepClone.put(defaultSequenceClone.getName(), defaultSequenceClone); 140 } else { 141 Sequence sequenceClone = (Sequence)sequence.clone(); 142 if (sequenceClone instanceof DefaultSequence) { 143 if (!((DefaultSequence)sequenceClone).hasPreallocationSize()) { 144 continue; 145 } 146 } 147 sequencesDeepClone.put(sequenceClone.getName(), sequenceClone); 148 } 149 } 150 this.setSequences(sequencesDeepClone); 151 } 152 } 153 154 162 public Object convertObject(Object sourceObject, Class javaClass) throws ConversionException { 163 return getConversionManager().convertObject(sourceObject, javaClass); 164 } 165 166 169 public void copyInto(Platform platform) { 170 if (!(platform instanceof DatasourcePlatform)) { 171 return; 172 } 173 DatasourcePlatform datasourcePlatform = (DatasourcePlatform)platform; 174 datasourcePlatform.setTableQualifier(getTableQualifier()); 175 datasourcePlatform.setTimestampQuery(this.timestampQuery); 176 datasourcePlatform.setConversionManager(getConversionManager()); 177 if (hasDefaultSequence()) { 178 datasourcePlatform.setDefaultSequence(getDefaultSequence()); 179 } 180 datasourcePlatform.setSequences(getSequences()); 181 } 182 183 186 public ConversionManager getConversionManager() { 187 if (conversionManager == null) { 189 conversionManager = (ConversionManager)ConversionManager.getDefaultManager().clone(); 191 } 192 return conversionManager; 193 } 194 195 198 public void setConversionManager(ConversionManager conversionManager) { 199 this.conversionManager = conversionManager; 200 } 201 202 205 public ExpressionOperator getOperator(int selector) { 206 return (ExpressionOperator)getPlatformOperators().get(new Integer (selector)); 207 } 208 209 212 public synchronized Map getPlatformOperators() { 213 if (platformOperators == null) { 214 initializePlatformOperators(); 215 } 216 return platformOperators; 217 } 218 219 public int getSequencePreallocationSize() { 220 return getDefaultSequence().getPreallocationSize(); 221 } 222 223 227 public String getTableQualifier() { 228 return tableQualifier; 229 } 230 231 234 public java.sql.Timestamp getTimestampFromServer(AbstractSession session, String sessionName) { 235 if (getTimestampQuery() == null) { 236 return new java.sql.Timestamp (System.currentTimeMillis()); 237 } else { 238 getTimestampQuery().setSessionName(sessionName); 239 return (java.sql.Timestamp )session.executeQuery(getTimestampQuery()); 240 } 241 } 242 243 248 public ValueReadQuery getTimestampQuery() { 249 return timestampQuery; 250 } 251 252 255 protected void initializePlatformOperators() { 256 this.platformOperators = new HashMap(); 257 258 addOperator(ExpressionOperator.equalOuterJoin()); 260 261 addOperator(ExpressionOperator.toUpperCase()); 263 addOperator(ExpressionOperator.toLowerCase()); 264 addOperator(ExpressionOperator.chr()); 265 addOperator(ExpressionOperator.concat()); 266 addOperator(ExpressionOperator.hexToRaw()); 267 addOperator(ExpressionOperator.initcap()); 268 addOperator(ExpressionOperator.instring()); 269 addOperator(ExpressionOperator.soundex()); 270 addOperator(ExpressionOperator.leftPad()); 271 addOperator(ExpressionOperator.leftTrim()); 272 addOperator(ExpressionOperator.leftTrim2()); 273 addOperator(ExpressionOperator.replace()); 274 addOperator(ExpressionOperator.rightPad()); 275 addOperator(ExpressionOperator.rightTrim()); 276 addOperator(ExpressionOperator.rightTrim2()); 277 addOperator(ExpressionOperator.substring()); 278 addOperator(ExpressionOperator.toNumber()); 279 addOperator(ExpressionOperator.toChar()); 280 addOperator(ExpressionOperator.toCharWithFormat()); 281 addOperator(ExpressionOperator.translate()); 282 addOperator(ExpressionOperator.trim()); 283 addOperator(ExpressionOperator.trim2()); 284 addOperator(ExpressionOperator.ascii()); 285 addOperator(ExpressionOperator.length()); 286 addOperator(ExpressionOperator.locate()); 287 addOperator(ExpressionOperator.locate2()); 288 289 addOperator(ExpressionOperator.addMonths()); 291 addOperator(ExpressionOperator.dateToString()); 292 addOperator(ExpressionOperator.lastDay()); 293 addOperator(ExpressionOperator.monthsBetween()); 294 addOperator(ExpressionOperator.nextDay()); 295 addOperator(ExpressionOperator.roundDate()); 296 addOperator(ExpressionOperator.toDate()); 297 addOperator(ExpressionOperator.today()); 298 addOperator(ExpressionOperator.currentDate()); 299 300 addOperator(ExpressionOperator.simpleMath(ExpressionOperator.Add, "+")); 302 addOperator(ExpressionOperator.simpleMath(ExpressionOperator.Subtract, "-")); 303 addOperator(ExpressionOperator.simpleMath(ExpressionOperator.Multiply, "*")); 304 addOperator(ExpressionOperator.simpleMath(ExpressionOperator.Divide, "/")); 305 306 addOperator(ExpressionOperator.ceil()); 307 addOperator(ExpressionOperator.cos()); 308 addOperator(ExpressionOperator.cosh()); 309 addOperator(ExpressionOperator.abs()); 310 addOperator(ExpressionOperator.acos()); 311 addOperator(ExpressionOperator.asin()); 312 addOperator(ExpressionOperator.atan()); 313 addOperator(ExpressionOperator.exp()); 314 addOperator(ExpressionOperator.sqrt()); 315 addOperator(ExpressionOperator.floor()); 316 addOperator(ExpressionOperator.ln()); 317 addOperator(ExpressionOperator.log()); 318 addOperator(ExpressionOperator.mod()); 319 addOperator(ExpressionOperator.power()); 320 addOperator(ExpressionOperator.round()); 321 addOperator(ExpressionOperator.sign()); 322 addOperator(ExpressionOperator.sin()); 323 addOperator(ExpressionOperator.sinh()); 324 addOperator(ExpressionOperator.tan()); 325 addOperator(ExpressionOperator.tanh()); 326 addOperator(ExpressionOperator.trunc()); 327 addOperator(ExpressionOperator.greatest()); 328 addOperator(ExpressionOperator.least()); 329 330 addOperator(ExpressionOperator.deref()); 332 addOperator(ExpressionOperator.ref()); 333 addOperator(ExpressionOperator.refToHex()); 334 addOperator(ExpressionOperator.value()); 335 } 336 337 public boolean isAccess() { 338 return false; 339 } 340 341 public boolean isAttunity() { 342 return false; 343 } 344 345 public boolean isCloudscape() { 346 return false; 347 } 348 349 public boolean isDerby() { 350 return false; 351 } 352 353 public boolean isDB2() { 354 return false; 355 } 356 357 public boolean isDBase() { 358 return false; 359 } 360 361 public boolean isHSQL() { 362 return false; 363 } 364 365 public boolean isInformix() { 366 return false; 367 } 368 369 public boolean isMySQL() { 370 return false; 371 } 372 373 public boolean isODBC() { 374 return false; 375 } 376 377 public boolean isOracle() { 378 return false; 379 } 380 381 public boolean isPointBase() { 382 return false; 383 } 384 385 public boolean isSQLAnywhere() { 386 return false; 387 } 388 389 public boolean isSQLServer() { 390 return false; 391 } 392 393 public boolean isSybase() { 394 return false; 395 } 396 397 public boolean isTimesTen() { 398 return false; 399 } 400 401 public boolean isPostgreSQL() { 402 return false; 403 } 404 405 409 public void setTableQualifier(String qualifier) { 410 tableQualifier = qualifier; 411 } 412 413 417 public void setTimestampQuery(ValueReadQuery tsQuery) { 418 timestampQuery = tsQuery; 419 } 420 421 public String toString() { 422 return Helper.getShortClassName(this.getClass()); 423 } 424 425 431 public Vector getDataTypesConvertedFrom(Class javaClass) { 432 return getConversionManager().getDataTypesConvertedFrom(javaClass); 433 } 434 435 441 public Vector getDataTypesConvertedTo(Class javaClass) { 442 return getConversionManager().getDataTypesConvertedTo(javaClass); 443 } 444 445 448 public Sequence getDefaultSequence() { 449 if (!hasDefaultSequence()) { 450 setDefaultSequence(createPlatformDefaultSequence()); 451 } 452 return defaultSequence; 453 } 454 455 458 public boolean hasDefaultSequence() { 459 return defaultSequence != null; 460 } 461 462 466 public void setDefaultSequence(Sequence sequence) { 467 if (sequence instanceof DefaultSequence) { 468 Sequence platformDefaultSequence = createPlatformDefaultSequence(); 469 if (platformDefaultSequence != null) { 470 platformDefaultSequence.setName(sequence.getName()); 471 if (((DefaultSequence)sequence).hasPreallocationSize()) { 472 platformDefaultSequence.setPreallocationSize(sequence.getPreallocationSize()); 473 } 474 } 475 defaultSequence = platformDefaultSequence; 476 } else { 477 defaultSequence = sequence; 478 } 479 } 480 481 484 public void addSequence(Sequence sequence) { 485 if (getSequences() == null) { 486 createSequences(); 487 } 488 getSequences().put(sequence.getName(), sequence); 489 } 490 491 494 public Sequence getSequence(String seqName) { 495 if (seqName == null) { 496 return getDefaultSequence(); 497 } else { 498 if (getSequences() != null) { 499 return (Sequence)getSequences().get(seqName); 500 } else { 501 return null; 502 } 503 } 504 } 505 506 510 protected Sequence createPlatformDefaultSequence() { 511 throw ValidationException.createPlatformDefaultSequenceUndefined(Helper.getShortClassName(this)); 512 } 513 514 protected synchronized void createSequences() { 515 if (getSequences() == null) { 516 setSequences(new HashMap()); 517 } 518 } 519 520 524 public Sequence removeSequence(String seqName) { 525 if (getSequences() != null) { 526 return (Sequence)getSequences().remove(seqName); 527 } else { 528 return null; 529 } 530 } 531 532 535 public void removeAllSequences() { 536 sequences = null; 537 } 538 539 543 public Map getSequences() { 544 return sequences; 545 } 546 547 551 public Map getSequencesToWrite() { 552 if ((getSequences() == null) || getSequences().isEmpty()) { 553 return null; 554 } 555 Map sequencesToWrite = new HashMap(); 556 Iterator it = getSequences().values().iterator(); 557 while (it.hasNext()) { 558 Sequence sequence = (Sequence)it.next(); 559 if (!(sequence instanceof DefaultSequence) || ((DefaultSequence)sequence).hasPreallocationSize()) { 560 sequencesToWrite.put(sequence.getName(), sequence); 561 } 562 } 563 return sequencesToWrite; 564 } 565 566 570 public Sequence getDefaultSequenceToWrite() { 571 if (usesPlatformDefaultSequence()) { 572 return null; 573 } else { 574 return getDefaultSequence(); 575 } 576 } 577 578 582 public void setSequences(Map sequences) { 583 this.sequences = sequences; 584 } 585 586 590 public boolean usesPlatformDefaultSequence() { 591 if (!hasDefaultSequence()) { 592 return true; 593 } else { 594 return getDefaultSequence().equals(createPlatformDefaultSequence()); 595 } 596 } 597 598 604 public void platformSpecificSequencingInitialization(DatabaseSession session) { 605 } 606 } 607 | Popular Tags |