| 1 19 package org.openharmonise.commons.dsi.impl; 20 21 22 import java.io.*; 23 24 import java.sql.*; 25 26 import java.util.*; 27 28 import org.openharmonise.commons.dsi.*; 29 import org.openharmonise.commons.dsi.ddl.*; 30 import org.openharmonise.commons.dsi.dml.*; 31 32 33 39 public class DataStoreInterfaceOracle extends AbstractDataStoreInterface { 40 41 static { 43 DB_DATEFORMAT = "MM-dd-yyyy HH:mm:ss"; 44 } 45 46 49 public DataStoreInterfaceOracle() { 50 } 51 52 59 public DataStoreInterfaceOracle(int nConnectionType) 60 throws DataStoreException { 61 super(nConnectionType); 62 } 63 64 65 66 69 public int getSequenceNextValue(String sSeqName) throws DataStoreException, 70 SQLException { 71 Connection conn = null; 72 ResultSet rs = null; 73 Statement stmt = null; 74 String sSql = null; 75 int nSeq = -1; 76 77 conn = getConnection(); 78 stmt = conn.createStatement(); 79 80 sSql = "select " + sSeqName + ".NEXTVAL from dual"; 81 82 rs = stmt.executeQuery(sSql); 83 84 if (rs.next()) { 85 nSeq = rs.getInt(1); 86 } else { 87 throw new DataStoreException("Sequence [" + sSeqName + 88 "] not found."); 89 } 90 91 if (rs != null) { 92 rs.close(); 93 } 94 95 if (stmt != null) { 96 stmt.close(); 97 } 98 99 if (isPooledConnection() && (conn != null)) { 100 this.closeConnection(conn); 101 } 102 103 return nSeq; 104 } 105 106 107 110 public void insertClob(String sTable, String sColumn, String sClob, 111 String sCondition) throws DataStoreException { 112 Connection conn = null; 113 Statement stmt = null; 114 ResultSet rs = null; 115 116 if ((sCondition == null) || (sCondition.length() == 0)) { 117 throw new DataStoreException("Missing CLOB condition"); 118 } 119 120 StringBuffer sSql = new StringBuffer (); 121 122 try { 123 conn = getConnection(); 124 stmt = conn.createStatement(); 125 126 sSql.append("update "); 127 sSql.append(sTable); 128 sSql.append(" set "); 129 sSql.append(sColumn); 130 sSql.append("="); 131 sSql.append(""); 132 sSql.append("empty_clob()"); 133 sSql.append(" where "); 134 sSql.append(sCondition); 135 136 stmt.executeUpdate(sSql.toString()); 137 138 sSql.setLength(0); 139 140 conn.setAutoCommit(false); 141 142 143 sSql.append("select "); 145 sSql.append(sColumn); 146 sSql.append(" from "); 147 sSql.append(sTable); 148 sSql.append(" where "); 149 sSql.append(sCondition); 150 sSql.append(" for update"); 151 152 rs = stmt.executeQuery(sSql.toString()); 153 154 Clob clob = null; 155 156 if (rs.next()) { 157 clob = rs.getClob(1); 158 } 159 160 Writer outstream = clob.setCharacterStream(0); 162 163 try { 164 outstream.write(addEscapeChars(sClob)); 165 outstream.close(); 166 } catch (IOException e) { 167 throw new DataStoreException("Error writing CLOB to database: " + 168 e.getMessage()); 169 } 170 171 conn.commit(); 172 conn.setAutoCommit(true); 173 174 if (isPooledConnection() && (conn != null)) { 175 this.closeConnection(conn); 176 } 177 } catch (SQLException e) { 178 throw new DataStoreException("SQLException: " + e.getMessage()); 179 } 180 } 181 182 183 186 public void updateClob(String sTable, String sColumn, String sClob, 187 String sCondition) throws DataStoreException { 188 Connection conn = null; 189 Statement stmt = null; 190 ResultSet rs = null; 191 192 if ((sCondition == null) || (sCondition.length() == 0)) { 193 throw new DataStoreException("Missing CLOB condition"); 194 } 195 196 StringBuffer sSql = new StringBuffer (); 197 198 try { 199 conn = getConnection(); 200 stmt = conn.createStatement(); 201 202 conn.setAutoCommit(false); 203 204 205 sSql.append("select "); 207 sSql.append(sColumn); 208 sSql.append(" from "); 209 sSql.append(sTable); 210 sSql.append(" where "); 211 sSql.append(sCondition); 212 213 sSql.append(" for update"); 214 215 rs = stmt.executeQuery(sSql.toString()); 216 217 Clob clob = null; 218 219 if (rs.next()) { 220 clob = rs.getClob(1); 221 } 222 223 Writer outstream = clob.setCharacterStream(0); 225 226 try { 227 outstream.write(addEscapeChars(sClob)); 228 outstream.close(); 229 } catch (IOException e) { 230 throw new DataStoreException("Error writing CLOB to database: " + 231 e.getMessage()); 232 } 233 234 conn.commit(); 235 conn.setAutoCommit(true); 236 237 if (stmt != null) { 238 stmt.close(); 239 } 240 241 if (rs != null) { 242 rs.close(); 243 } 244 245 if (isPooledConnection() && (conn != null)) { 246 this.closeConnection(conn); 247 } 248 } catch (SQLException e) { 249 throw new DataStoreException("SQLException: " + e.getMessage()); 250 } 251 } 252 253 256 public String getClob(String sTable, String sColumn, String sCondition) 257 throws DataStoreException { 258 Connection conn = null; 259 Statement stmt = null; 260 ResultSet rs = null; 261 String sReturn = null; 262 263 if ((sCondition == null) || (sCondition.length() == 0)) { 264 throw new DataStoreException("Missing CLOB condition"); 265 } 266 267 StringBuffer sSql = new StringBuffer (); 268 269 try { 270 conn = getConnection(); 271 272 conn.setAutoCommit(false); 273 274 stmt = conn.createStatement(); 275 276 sSql.append("select "); 277 sSql.append(sColumn); 278 sSql.append(" from "); 279 sSql.append(sTable); 280 sSql.append(" where "); 281 sSql.append(sCondition); 282 283 rs = stmt.executeQuery(sSql.toString()); 284 285 if (rs.next()) { 286 287 Clob clob = rs.getClob(1); 288 289 try { 290 sReturn = Clob2String(clob); 291 } catch (Exception e) { 292 throw new DataStoreException( 293 "Error converting CLOB to a String: " + 294 e.getMessage()); 295 } 296 } 297 298 conn.commit(); 299 conn.setAutoCommit(true); 300 301 if (stmt != null) { 302 stmt.close(); 303 } 304 305 if (rs != null) { 306 rs.close(); 307 } 308 309 if (isPooledConnection() && (conn != null)) { 310 this.closeConnection(conn); 311 } 312 } catch (SQLException e) { 313 throw new DataStoreException("SQLException: " + e.getMessage()); 314 } 315 316 return sReturn; 317 } 318 319 320 327 private String Clob2String(Clob clob) throws DataStoreException { 328 StringBuffer strbuf = new StringBuffer (); 329 String sMattString = ""; 330 331 try { 332 char[] buffer = new char[(int) clob.length()]; 334 335 long mattLong = 1; 336 337 sMattString = clob.getSubString(mattLong, (int) clob.length()); 338 339 340 } catch (SQLException e) { 341 throw new DataStoreException("SQLException: " + e.getMessage()); 342 } 343 344 return sMattString; 345 } 346 347 348 351 protected String addEscapeChars(String sOldString) { 352 int marker = -1; 353 int lastmarker = 0; 354 int quotemarker = -1; 355 356 if (sOldString == null) { 357 return ""; 358 } 359 360 StringBuffer sBuf = new StringBuffer (); 361 362 quotemarker = sOldString.indexOf("'"); 363 364 if (quotemarker >= 0) { 365 marker = quotemarker; 366 } 367 368 if (marker < 0) { 369 return sOldString; 370 } else { 371 while (marker >= 0) { 372 sBuf.append(sOldString.substring(lastmarker, marker)); 374 sBuf.append("'"); 375 376 377 quotemarker = -1; 379 lastmarker = marker; 380 381 quotemarker = sOldString.indexOf("'", marker + 1); 382 383 if (quotemarker >= 0) { 384 marker = quotemarker; 385 } else { 386 marker = -1; 387 } 388 } 389 390 sBuf.append(sOldString.substring(lastmarker)); 391 392 return (sBuf.toString()); 393 } 394 } 395 396 399 public String getDateDataType() { 400 return "DATE"; 401 } 402 403 406 public String getCLOBDataType() { 407 return "CLOB"; 408 } 409 410 413 public String getBooleanDataType() { 414 return "SMALLINT"; 415 } 416 417 420 public List getTableList() throws DataStoreException { 421 Vector tables = new Vector(); 422 ResultSet rs = null; 423 424 try { 425 rs = executeQuery( 426 "select table_name from user_tables where table_name not like '%$%' order by table_name"); 427 428 while (rs.next()) { 429 tables.add(rs.getString(1).trim()); 430 } 431 432 rs.close(); 433 } catch (SQLException e) { 434 throw new DataStoreException(e); 435 } 436 437 return tables; 438 } 439 440 441 444 public List getSequenceList() throws DataStoreException { 445 Vector seqs = new Vector(); 446 ResultSet rs = null; 447 448 try { 449 rs = executeQuery( 450 "select sequence_name from seq order by sequence_name"); 451 452 while (rs.next()) { 453 seqs.add(rs.getString(1).trim()); 454 } 455 456 rs.close(); 457 } catch (SQLException e) { 458 throw new DataStoreException(e); 459 } 460 461 return seqs; 462 } 463 464 465 468 protected String getDateAsSQL(String date) { 469 StringBuffer sSql = new StringBuffer (); 472 473 sSql.append(" TO_DATE('"); 474 sSql.append(date); 475 sSql.append("','MM-DD-YYYY HH24:MI:SS' )"); 476 477 return sSql.toString(); 478 } 479 480 483 public String getJoinCondition(ColumnRef ref1, ColumnRef ref2, 484 boolean bIsOuter) { 485 StringBuffer sSql = new StringBuffer (); 486 487 sSql.append(ref1.getFullRef()); 488 489 if (bIsOuter == true) { 490 sSql.append("(+)"); 491 } 492 493 sSql.append("="); 494 495 sSql.append(ref2.getFullRef()); 496 497 return sSql.toString(); 498 } 499 500 503 protected String getFunction(Function func) throws DataStoreException { 504 throw new DataStoreException("Not implemented"); 505 } 506 507 510 public void createTable(TableDefinition tblDef) throws DataStoreException { 511 throw new UnsupportedOperationException ("Method not implemented"); 512 } 513 } | Popular Tags |