001: /*
002: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: *
005: *
006: */
007:
008: package com.hp.hpl.jena.db;
009:
010: import java.sql.*;
011:
012: import com.hp.hpl.jena.db.impl.*;
013: import com.hp.hpl.jena.graph.*;
014: import com.hp.hpl.jena.rdf.model.*;
015: import com.hp.hpl.jena.util.iterator.*;
016:
017: /**
018: * Encapsulate the specification of a jdbc connection.
019: * This is mostly used to simplify the calling pattern for ModelRDB factory methods.
020: *
021: * @author csayers (based in part on the jena 1 implementation by der).
022: * @version $Revision: 1.19 $
023: */
024:
025: public class DBConnection implements IDBConnection {
026:
027: /** The jdbc connection being wrapped up */
028: protected Connection m_connection;
029:
030: /** The url for the connection, may be null if the connection was passed in pre-opened */
031: protected String m_url;
032:
033: /** The user name for the connection, may be null if the connection was passed in pre-opened */
034: protected String m_user;
035:
036: /** The password for the connection, may be null if the connection was passed in pre-opened */
037: protected String m_password;
038:
039: /** The database type: "Oracle", "mySQL, etc...
040: * This is new in Jena2 - for compatability with older code we allow this to
041: * be left unspecified at the loss of some jena2 functionality.
042: */
043: protected String m_databaseType = null;
044:
045: /** Driver to connect to this database */
046: protected IRDBDriver m_driver = null;
047:
048: /**
049: * Create a connection specification based on jdbc address and
050: * appropriate authentication information.
051: * @param url the jdbc url for the database, note that the format of this
052: * is database dependent and that the appropriate jdbc driver will need to
053: * be specified via the standard pattern
054: * <pre>
055: * Class.forName("my.sql.driver");
056: * </pre>
057: * @param user the user name to log on with
058: * @param password the password corresponding to this user
059: * @deprecated As of Jena 2.0, it is recommended to use one of the DBConnection
060: * constructors which takes a database type as an argument. (The DBConnection can
061: * operate more efficiently if it knows the database type).
062: */
063: public DBConnection(String url, String user, String password) {
064: this (url, user, password, null);
065: }
066:
067: /**
068: * Create a connection specification based on jdbc address and
069: * appropriate authentication information.
070: * @param url the jdbc url for the database, note that the format of this
071: * is database dependent and that the appropriate jdbc driver will need to
072: * be specified via the standard pattern
073: * <pre>
074: * Class.forName("my.sql.driver");
075: * </pre>
076: * @param user the user name to log on with
077: * @param password the password corresponding to this user
078: * @param databaseType the type of database to which we are connecting.
079: *
080: * @since Jena 2.0
081: */
082: public DBConnection(String url, String user, String password,
083: String databaseType) {
084: m_url = url;
085: m_user = user;
086: m_password = password;
087: setDatabaseType(databaseType);
088: }
089:
090: /**
091: * Create a connection specification that just wraps up an existing database
092: * connection.
093: * @param connection the open jdbc connection to use
094: * @deprecated As of Jena 2.0, it is recommended to use one of the DBConnection
095: * constructors which takes a database type as an argument. (The DBConnection can
096: * operate more efficiently if it knows the database type).
097: */
098: public DBConnection(Connection connection) {
099: this (connection, null);
100: }
101:
102: /**
103: * Create a connection specification that just wraps up an existing database
104: * connection.
105: * @param connection the open jdbc connection to use
106: * @param databaseType the type of database to which we are connecting.
107: *
108: * @since Jena 2.0
109: */
110: public DBConnection(Connection connection, String databaseType) {
111: m_connection = connection;
112: setDatabaseType(databaseType);
113: }
114:
115: /* (non-Javadoc)
116: * @see com.hp.hpl.jena.db.IDBConnection#getConnection()
117: */
118: public Connection getConnection() throws SQLException {
119: if (m_connection == null) {
120: if (m_url != null) {
121: m_connection = DriverManager.getConnection(m_url,
122: m_user, m_password);
123: m_connection.setAutoCommit(true);
124: }
125: }
126: return m_connection;
127: }
128:
129: /* (non-Javadoc)
130: * @see com.hp.hpl.jena.db.IDBConnection#close()
131: */
132: public void close() throws SQLException {
133: if (m_driver != null) {
134: m_driver.close();
135: m_driver = null;
136: }
137: if (m_connection != null) {
138: m_connection.close();
139: m_connection = null;
140: }
141: }
142:
143: /* (non-Javadoc)
144: * @see com.hp.hpl.jena.db.IDBConnection#cleanDB()
145: */
146: public void cleanDB() throws SQLException {
147: if (m_driver == null)
148: m_driver = getDriver();
149: m_driver.cleanDB();
150: }
151:
152: /* (non-Javadoc)
153: * @see com.hp.hpl.jena.db.IDBConnection#isFormatOK()
154: */
155: public boolean isFormatOK() {
156: // Removed exception trap, an exception might be a connection
157: // failure on a well formated database - der 24/7/04
158: // try {
159: if (m_driver == null)
160: m_driver = getDriver();
161: return m_driver.isDBFormatOK();
162: // } catch (Exception e) {
163: // return false;
164: // }
165: }
166:
167: /* (non-Javadoc)
168: * @see com.hp.hpl.jena.db.IDBConnection#setDatabaseProperties(com.hp.hpl.jena.rdf.model.Model)
169: */
170: public void setDatabaseProperties(Model dbProperties)
171: throws RDFRDBException {
172: if (m_driver == null)
173: m_driver = getDriver();
174: m_driver.setDatabaseProperties(dbProperties.getGraph());
175: }
176:
177: /* (non-Javadoc)
178: * @see com.hp.hpl.jena.db.IDBConnection#getDatabaseProperties()
179: */
180: public Model getDatabaseProperties() throws RDFRDBException {
181: if (m_driver == null)
182: m_driver = getDriver();
183: Model resultModel = ModelFactory.createDefaultModel();
184: copySpecializedGraphToModel(m_driver
185: .getSystemSpecializedGraph(true), resultModel,
186: Triple.ANY);
187: return resultModel;
188: }
189:
190: /* (non-Javadoc)
191: * @see com.hp.hpl.jena.db.IDBConnection#getDefaultModelProperties()
192: */
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
199: .getSystemSpecializedGraph(true), resultModel, Triple
200: .createMatch(defaultProps.getNode(), null, null));
201: return resultModel;
202: }
203:
204: /* (non-Javadoc)
205: * @see com.hp.hpl.jena.db.IDBConnection#getAllModelNames()
206: */
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: /* (non-Javadoc)
222: * @see com.hp.hpl.jena.db.IDBConnection#containsModel(java.lang.String)
223: */
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: /* (non-Javadoc)
237: * @see com.hp.hpl.jena.db.IDBConnection#containsDefaultModel()
238: */
239: public boolean containsDefaultModel() throws RDFRDBException {
240: return containsModel(GraphRDB.DEFAULT);
241: }
242:
243: /**
244: * Copy the contents of a specialized graph to a new Model.
245: *
246: * This has package scope - for internal use only.
247: *
248: * @since Jena 2.0
249: */
250: static void copySpecializedGraphToModel(SpecializedGraph fromGraph,
251: Model toModel, TripleMatch filter) throws RDFRDBException {
252: Graph toGraph = toModel.getGraph();
253: SpecializedGraph.CompletionFlag complete = new SpecializedGraph.CompletionFlag();
254: ExtendedIterator it = fromGraph.find(filter, complete);
255: while (it.hasNext())
256: toGraph.add((Triple) (it.next()));
257: it.close();
258: }
259:
260: /* (non-Javadoc)
261: * @see com.hp.hpl.jena.db.IDBConnection#setDatabaseType(java.lang.String)
262: */
263: public void setDatabaseType(String databaseType) {
264: if (databaseType != null) {
265: if (databaseType.compareToIgnoreCase("mysql") == 0) {
266: m_databaseType = "MySQL";
267: } else {
268: m_databaseType = databaseType;
269: }
270: }
271:
272: }
273:
274: /* (non-Javadoc)
275: * @see com.hp.hpl.jena.db.IDBConnection#getDatabaseType()
276: */
277: public String getDatabaseType() {
278: return m_databaseType;
279: }
280:
281: /* (non-Javadoc)
282: * @see com.hp.hpl.jena.db.IDBConnection#getDriver()
283: */
284: public IRDBDriver getDriver() throws RDFRDBException {
285: try {
286: if (m_connection == null)
287: getConnection();
288:
289: if (m_driver == null) {
290: // need to look for a suitable driver
291: if (m_databaseType == null) {
292: // without knowing the database type there's not much we can do.
293: throw new RDFRDBException(
294: "Error - attempt to call DBConnection.getDriver before setting the database type");
295: }
296: m_driver = (IRDBDriver) (Class
297: .forName("com.hp.hpl.jena.db.impl.Driver_"
298: + m_databaseType).newInstance());
299: m_driver.setConnection(this );
300: }
301: } catch (Exception e) {
302: // e.printStackTrace( System.err );
303: throw new RDFRDBException(
304: "Failure to instantiate DB Driver:"
305: + m_databaseType + " " + e.toString(), e);
306: }
307:
308: return m_driver;
309: }
310:
311: /* (non-Javadoc)
312: * @see com.hp.hpl.jena.db.IDBConnection#setDriver(com.hp.hpl.jena.db.impl.IRDBDriver)
313: */
314: public void setDriver(IRDBDriver driver) {
315: m_driver = driver;
316: }
317:
318: /**
319: * Helper function to locate and instantiate the driver class corresponding
320: * to a given layout and database name
321: * Throws an RDFRDBexception if the driver can't be instantiated
322: * @deprecated As of Jena 2.0 this call should not be used. Instead specify the database type
323: * when constructing a DBConnection and then pass that connection to the GraphRDB. There is
324: * no longer any need for applications to interact with the IRDBDriver. To customize the
325: * database configuration/layout use the formatDB(propertyModel) call.
326: */
327: public IRDBDriver getDriver(String layout, String database)
328: throws RDFRDBException {
329: // the layout is not supported in Jena2 - ignore this parameter
330: setDatabaseType(database);
331: return getDriver();
332: }
333:
334: }
335:
336: /*
337: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
338: * All rights reserved.
339: *
340: * Redistribution and use in source and binary forms, with or without
341: * modification, are permitted provided that the following conditions
342: * are met:
343: * 1. Redistributions of source code must retain the above copyright
344: * notice, this list of conditions and the following disclaimer.
345: * 2. Redistributions in binary form must reproduce the above copyright
346: * notice, this list of conditions and the following disclaimer in the
347: * documentation and/or other materials provided with the distribution.
348: * 3. The name of the author may not be used to endorse or promote products
349: * derived from this software without specific prior written permission.
350:
351: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
352: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
353: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
354: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
355: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
356: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
357: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
358: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
359: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
360: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
361: */
|