1 7 8 package com.hp.hpl.jena.db.impl; 9 10 import java.io.InputStream ; 11 import java.io.OutputStream ; 12 import java.io.StringBufferInputStream ; 13 import java.io.UnsupportedEncodingException ; 14 import java.sql.PreparedStatement ; 15 import java.sql.ResultSet ; 16 import java.sql.SQLException ; 17 import java.sql.Statement ; 18 import java.util.Iterator ; 19 20 import com.hp.hpl.jena.db.IDBConnection; 21 import com.hp.hpl.jena.db.RDFRDBException; 22 23 29 30 40 public class Driver_Oracle extends DriverRDB { 41 42 44 public interface BLOB extends java.sql.Blob { 45 OutputStream getBinaryOutputStream(); 46 int getBufferSize(); 47 boolean isOpen(); 48 void close(); 49 } 50 51 private interface OracleResultSet extends ResultSet { 52 BLOB getBLOB(int i); 53 } 54 55 56 57 58 61 public Driver_Oracle( ){ 62 super(); 63 64 String myPackageName = this.getClass().getPackage().getName(); 65 66 DATABASE_TYPE = "Oracle"; 67 DRIVER_NAME = "oracle.jdbc.driver.OracleDriver"; 68 69 ID_SQL_TYPE = "INTEGER"; 70 URI_COMPRESS = false; 71 INDEX_KEY_LENGTH_MAX = INDEX_KEY_LENGTH = 4000; 72 LONG_OBJECT_LENGTH_MAX = LONG_OBJECT_LENGTH = 250; 73 TABLE_NAME_LENGTH_MAX = 30; 74 76 IS_XACT_DB = true; 77 PRE_ALLOCATE_ID = true; 78 SKIP_DUPLICATE_CHECK = false; 79 SQL_FILE = "etc/oracle.sql"; 80 81 m_psetClassName = myPackageName + ".PSet_TripleStore_RDB"; 82 m_psetReifierClassName = myPackageName + ".PSet_ReifStore_RDB"; 83 84 m_lsetClassName = myPackageName + ".SpecializedGraph_TripleStore_RDB"; 85 m_lsetReifierClassName = myPackageName + ".SpecializedGraphReifier_RDB"; 86 87 88 QUOTE_CHAR = '\''; 89 90 DB_NAMES_TO_UPPER = true; 91 setTableNames(TABLE_NAME_PREFIX); 92 } 93 94 97 public void setConnection( IDBConnection dbcon ) { 98 m_dbcon = dbcon; 99 110 try { 111 m_sql = new SQLCache(SQL_FILE, null, dbcon, ID_SQL_TYPE); 114 } catch (Exception e) { 115 e.printStackTrace( System.err ); 116 logger.error("Unable to set connection for Driver:", e); 117 } 118 } 119 120 124 public int graphIdAlloc ( String graphName ) { 125 DBIDInt result = null; 126 int dbid = 0; 127 try { 128 String op = "insertGraph"; 129 dbid = getInsertID(GRAPH_TABLE); 130 PreparedStatement ps = m_sql.getPreparedSQLStatement(op,GRAPH_TABLE); 131 ps.setInt(1,dbid); 132 ps.setString(2,graphName); 133 ps.executeUpdate(); 134 m_sql.returnPreparedSQLStatement(ps); 135 } catch (SQLException e) { 136 throw new RDFRDBException("Failed to get last inserted ID: " + e); 137 } 138 return dbid; 139 } 140 141 145 public void graphIdDealloc ( int graphId ) { 146 DBIDInt result = null; 147 try { 148 String op = "deleteGraph"; 149 PreparedStatement ps = m_sql.getPreparedSQLStatement(op,GRAPH_TABLE); 150 ps.setInt(1,graphId); 151 ps.executeUpdate(); 152 m_sql.returnPreparedSQLStatement(ps); 153 } catch (SQLException e) { 154 throw new RDFRDBException("Failed to delete graph ID: " + e); 155 } 156 return; 157 } 158 159 public int getInsertID ( String tableName ) { 160 DBIDInt result = null; 161 try { 162 String op = "getInsertID"; 163 PreparedStatement ps = m_sql.getPreparedSQLStatement(op,tableName); 164 ResultSet rs = ps.executeQuery(); 165 if (rs.next()) { 166 result = wrapDBID(rs.getObject(1)); 167 } else 168 throw new RDFRDBException("No insert ID"); 169 m_sql.returnPreparedSQLStatement(ps); 170 } catch (SQLException e) { 171 throw new RDFRDBException("Failed to insert ID: " + e); 172 } 173 return result.getIntID(); 174 } 175 176 177 184 protected void getTblParams ( String [] param ) { 185 String objColType; 186 187 if ( LONG_OBJECT_LENGTH > 4000 ) 189 throw new RDFRDBException("Long object length specified (" + LONG_OBJECT_LENGTH + 190 ") exceeds maximum sane length of 4000."); 191 if ( INDEX_KEY_LENGTH > 4000 ) 192 throw new RDFRDBException("Index key length specified (" + INDEX_KEY_LENGTH + 193 ") exceeds maximum sane length of 4000."); 194 195 objColType = "VARCHAR2(" + LONG_OBJECT_LENGTH + ")"; 196 STRINGS_TRIMMED = false; 197 param[0] = objColType; 198 199 String headColType = "VARCHAR2(" + INDEX_KEY_LENGTH + ")"; 201 param[1] = headColType; 202 param[2] = TABLE_NAME_PREFIX; 203 } 204 205 213 214 protected String [] getCreateTableParams( int graphId, boolean isReif ) { 215 String [] parms = new String [3]; 216 String [] res = new String [2]; 217 218 getTblParams (parms); 219 int tblCnt = getTableCount(graphId); 220 res[0] = genTableName(graphId,tblCnt,isReif); 221 res[1] = parms[0]; 222 return res; 223 } 224 225 228 protected String [] getDbInitTablesParams() { 229 String [] res = new String [3]; 230 231 getTblParams (res); 232 EOS_LEN = EOS.length(); 233 234 return res; 235 } 236 237 242 public DBIDInt addRDBLongObject(RDBLongObject lobj, String table) throws RDFRDBException { 243 DBIDInt longObjID = null; 244 try { 245 int argi = 1; 246 boolean save = m_dbcon.getConnection().getAutoCommit(); 247 248 String opname = (lobj.tail.length() > 0) ? "insertLongObjectEmptyTail" : "insertLongObject"; 249 PreparedStatement ps = m_sql.getPreparedSQLStatement(opname, table); 250 int dbid = 0; if ( PRE_ALLOCATE_ID ) { 252 dbid = getInsertID(table); 253 ps.setInt(argi++,dbid); 254 longObjID = wrapDBID(new Integer (dbid)); 255 } 256 ps.setString(argi++, lobj.head); 257 if ( lobj.tail.length() > 0 ) { 258 ps.setLong(argi++, lobj.hash); 259 } else { 260 ps.setNull(argi++,java.sql.Types.BIGINT); 261 } 262 ps.executeUpdate(); 263 m_sql.returnPreparedSQLStatement(ps); 264 265 if ( lobj.tail.length() > 0) { 266 if (! xactOp(xactIsActive)) { 267 m_dbcon.getConnection().setAutoCommit(false); 268 } 269 opname = "getEmptyBLOB"; 270 String cmd = m_sql.getSQLStatement(opname, table, longObjID.getID().toString()); 271 Statement lobStmt = m_sql.getConnection().createStatement(); 272 ResultSet lrs = lobStmt.executeQuery(cmd); 273 lrs.next(); 274 275 BLOB blob = ((OracleResultSet) lrs).getBLOB(1); 276 OutputStream outstream = blob.getBinaryOutputStream(); 277 int size = blob.getBufferSize(); 278 279 int length = -1; 280 InputStream instream = new StringBufferInputStream (lobj.tail); 281 282 byte[] buffer = new byte[size]; 284 285 while ((length = instream.read(buffer)) != -1) 286 outstream.write(buffer,0,length); 287 288 if (blob.isOpen()) 289 blob.close(); 290 instream.close(); 291 outstream.close(); 292 lobStmt.close(); 293 if (! xactOp(xactIsActive)) { 294 m_dbcon.getConnection().setAutoCommit(save); 295 } 296 } 297 298 if ( !PRE_ALLOCATE_ID ) { 299 dbid = getInsertID(table); 300 longObjID = wrapDBID(new Integer (dbid)); 301 } 302 } catch (Exception e1) { 303 System.out.println("Problem on long object (l=" + lobj.head + ") " + e1 ); 304 throw new RDFRDBException("Failed to add long object ", e1); 306 } 307 return longObjID; 308 } 309 310 311 312 315 protected RDBLongObject IDtoLongObject ( int dbid, String table ) { 316 RDBLongObject res = null; 317 try { 318 String opName = "getLongObject"; 319 PreparedStatement ps = m_sql.getPreparedSQLStatement(opName, table); 320 ps.setInt(1,dbid); 321 OracleResultSet rs = (OracleResultSet) ps.executeQuery(); 322 if (rs.next()) { 323 res = new RDBLongObject(); 324 res.head = rs.getString(1); 325 BLOB blob = rs.getBLOB(2); 326 327 if (blob != null) { 328 int len = (int)blob.length(); 329 byte[] data = blob.getBytes(1,len); 330 res.tail = new String (data, "UTF-8"); 331 } else { 332 res.tail = ""; 333 } 334 } 335 rs.close(); 336 m_sql.returnPreparedSQLStatement(ps); 337 338 } catch (SQLException e1) { 339 throw new RDFRDBException("Failed to retrieve long object (SQL Exception): ", e1); 341 } catch (UnsupportedEncodingException e2) { 342 throw new RDFRDBException("Failed to retrieve long object (UnsupportedEncoding): ", e2); 343 } 344 return res; 345 } 346 347 351 public void clearSequences() { 352 Iterator seqIt = getSequences().iterator(); 353 while (seqIt.hasNext()) { 354 removeSequence((String )seqIt.next()); 355 } 356 } 357 358 public String genSQLStringMatchLHS_IC(String var) { 359 return "UPPER(" + var + ")"; 360 } 361 362 public String genSQLStringMatchRHS_IC(String strToMatch) { 363 return "UPPER(" + strToMatch + ")"; 364 } 365 366 public String stringMatchEscapeChar() { return "\\"; } 367 368 public String genSQLStringMatchEscape() { 369 return " " + genSQLEscapeKW() + " '" + stringMatchEscapeChar() + "'"; 370 } 371 372 373 374 375 376 377 378 379 380 381 } 382 383 409 | Popular Tags |