1 7 8 package com.hp.hpl.jena.db; 9 10 import java.sql.*; 11 12 import com.hp.hpl.jena.db.impl.*; 13 import com.hp.hpl.jena.graph.*; 14 import com.hp.hpl.jena.rdf.model.*; 15 import com.hp.hpl.jena.util.iterator.*; 16 17 24 25 public class DBConnection implements IDBConnection { 26 27 28 protected Connection m_connection; 29 30 31 protected String m_url; 32 33 34 protected String m_user; 35 36 37 protected String m_password; 38 39 43 protected String m_databaseType = null; 44 45 46 protected IRDBDriver m_driver = null; 47 48 49 64 public DBConnection(String url, String user, String password) { 65 this( url, user, password, null); 66 } 67 68 83 public DBConnection(String url, String user, String password, String databaseType) { 84 m_url = url; 85 m_user = user; 86 m_password = password; 87 setDatabaseType(databaseType); 88 } 89 90 98 public DBConnection(Connection connection) { 99 this(connection, null); 100 } 101 102 110 public DBConnection(Connection connection, String databaseType) { 111 m_connection = connection; 112 setDatabaseType(databaseType); 113 } 114 115 116 119 public Connection getConnection() throws SQLException { 120 if (m_connection == null) { 121 if (m_url != null) { 122 m_connection = 123 DriverManager.getConnection(m_url, m_user, m_password); 124 m_connection.setAutoCommit(true); 125 } 126 } 127 return m_connection; 128 } 129 130 133 public void close() throws SQLException { 134 if( m_driver != null ) { 135 m_driver.close(); 136 m_driver = null; 137 } 138 if (m_connection != null) { 139 m_connection.close(); 140 m_connection = null; 141 } 142 } 143 144 147 public void cleanDB() throws SQLException { 148 if (m_driver == null) 149 m_driver = getDriver(); 150 m_driver.cleanDB(); 151 } 152 153 156 public boolean isFormatOK() { 157 if( m_driver == null ) 161 m_driver = getDriver(); 162 return m_driver.isDBFormatOK(); 163 } 167 168 171 public void setDatabaseProperties(Model dbProperties) throws RDFRDBException { 172 if (m_driver == null) 173 m_driver = getDriver(); 174 m_driver.setDatabaseProperties( dbProperties.getGraph()); 175 } 176 177 180 public Model getDatabaseProperties() throws RDFRDBException { 181 if (m_driver == null) 182 m_driver = getDriver(); 183 Model resultModel = ModelFactory.createDefaultModel(); 184 copySpecializedGraphToModel( m_driver.getSystemSpecializedGraph(true), 185 resultModel, 186 Triple.createMatch( null, null, null )); 187 return resultModel; 188 } 189 190 193 public Model getDefaultModelProperties() throws RDFRDBException { 194 if (m_driver == null) 195 m_driver = getDriver(); 196 DBPropGraph defaultProps = m_driver.getDefaultModelProperties(); 197 Model resultModel = ModelFactory.createDefaultModel(); 198 copySpecializedGraphToModel( m_driver.getSystemSpecializedGraph(true), 199 resultModel, 200 Triple.createMatch(defaultProps.getNode(), null, null)); 201 return resultModel; 202 } 203 204 207 public ExtendedIterator getAllModelNames() throws RDFRDBException { 208 if (m_driver == null) 209 m_driver = getDriver(); 210 SpecializedGraph sg = m_driver.getSystemSpecializedGraph(false); 211 ExtendedIterator it; 212 if ( sg == null ) 213 it = new ResultSetIterator(); 214 else { 215 DBPropDatabase dbprops = new DBPropDatabase(sg); 216 it = dbprops.getAllGraphNames(); 217 } 218 return it; 219 } 220 221 224 public boolean containsModel(String name) throws RDFRDBException { 225 boolean res = false; 226 if (m_driver == null) 227 m_driver = getDriver(); 228 SpecializedGraph sg = m_driver.getSystemSpecializedGraph(false); 229 if ( sg != null ) { 230 DBPropGraph g = DBPropGraph.findPropGraphByName(sg,name); 231 res = g == null ? false : g.isDBPropGraphOk(name); 232 } 233 return res; 234 } 235 236 239 public boolean containsDefaultModel() throws RDFRDBException { 240 return containsModel(GraphRDB.DEFAULT); 241 } 242 243 250 static void copySpecializedGraphToModel( SpecializedGraph fromGraph, Model toModel, TripleMatch filter) throws RDFRDBException { 251 Graph toGraph = toModel.getGraph(); 252 SpecializedGraph.CompletionFlag complete = new SpecializedGraph.CompletionFlag(); 253 ExtendedIterator it = fromGraph.find( filter, complete); 254 while(it.hasNext()) 255 toGraph.add((Triple)(it.next())); 256 it.close(); 257 } 258 259 262 public void setDatabaseType( String databaseType ) { 263 if (databaseType != null) { 264 if (databaseType.compareToIgnoreCase("mysql") == 0) { 265 m_databaseType = "MySQL"; 266 } else { 267 m_databaseType = databaseType; 268 } 269 } 270 271 } 272 273 276 public String getDatabaseType() { return m_databaseType; } 277 278 281 public IRDBDriver getDriver() throws RDFRDBException { 282 try { 283 if (m_connection == null) 284 getConnection(); 285 286 if (m_driver == null) { 287 if (m_databaseType == null) { 289 throw new RDFRDBException("Error - attempt to call DBConnection.getDriver before setting the database type"); 291 } 292 m_driver = (IRDBDriver) (Class.forName("com.hp.hpl.jena.db.impl.Driver_" + m_databaseType).newInstance()); 293 m_driver.setConnection( this ); 294 } 295 } catch (Exception e) { 296 throw new RDFRDBException("Failure to instantiate DB Driver:"+ m_databaseType+ " "+ e.toString()); 298 } 299 300 return m_driver; 301 } 302 303 306 public void setDriver(IRDBDriver driver) { 307 m_driver = driver; 308 } 309 310 319 public IRDBDriver getDriver(String layout, String database) throws RDFRDBException { 320 setDatabaseType(database); 322 return getDriver(); 323 } 324 325 } 326 327 353 | Popular Tags |