KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > db > ModelRDB


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4 */

5
6 package com.hp.hpl.jena.db;
7
8
9 import com.hp.hpl.jena.rdf.model.impl.*;
10 import com.hp.hpl.jena.rdf.model.*;
11 import com.hp.hpl.jena.util.iterator.ExtendedIterator;
12 import com.hp.hpl.jena.db.impl.DBQueryHandler;
13 import com.hp.hpl.jena.enhanced.*;
14 import com.hp.hpl.jena.graph.*;
15
16 /** A persistent relational database implemention of the RDF API.
17  *
18  * <p>This implementation of the RDF API stores all its data in a relational database.</p>
19  * <p> To construct a persistent RDB Model, first load the jdbc connector for
20  * your database - in this example, mysql:</p>
21  *
22  * <code>
23  * Class.forName("com.mysql.jdbc.Driver");
24  * </code>
25  * <p> Then create a connection to the database: </p>
26  *
27  * <code>
28  * IDBConnection conn = new DBConnection("jdbc:mysql://localhost/test", "test", "", "MySQL");
29  * </code>
30  *
31  * <p> Now, using that connection, you can construct Models in the database:</p>
32  * <code>
33  * Model m = ModelRDB.createModel(conn);
34  * </code>
35  *
36  * @author csayers (based on ModelMem written by bwm and the Jena 1 version of Model RDB by der.)
37  * @version $Revision: 1.17 $
38  */

39 public class ModelRDB extends ModelCom implements Model {
40     
41     protected GraphRDB m_graphRDB = null;
42
43     /**
44      * Construct a model which is stored persistently in a Relational DataBase
45      *
46      * If a model already exists in the database, then it is opened, otherwise
47      * a new model with default name and formatting is inserted and opened.
48      * @param dbcon a Connection specifying the database connection
49      * @deprecated Since Jena 2.0, this call is not recommended -
50      * in the short-term use ModelRDB.open or ModelRDB.createModel;
51      * in the longer-term use factory methods to construct persistent models.
52      */

53      public ModelRDB( IDBConnection dbcon) throws RDFRDBException {
54         this(BuiltinPersonalities.model, new GraphRDB(dbcon, null, null, !dbcon.containsDefaultModel()));
55      }
56
57
58     /**
59      * Construct a model which is stored persistently in a Relational DataBase
60      *
61      * If a model with the specified identifier already exists in the
62      * database, then it is opened, otherwise a new model with default
63      * formatting is inserted and opened.
64      * @param dbcon a Connection specifying the database connection
65      * @param modelID is the identifier of an RDF model within the database.
66      * The modelID "DEFAULT" is reserved and may not be used for user models.
67      * @deprecated Since Jena 2.0, this call is not recommended -
68      * in the short-term use ModelRDB.open or ModelRDB.createModel;
69      * in the longer-term use factory methods to construct persistent models.
70      */

71      public ModelRDB( IDBConnection dbcon, String JavaDoc modelID) throws RDFRDBException {
72         this(BuiltinPersonalities.model, new GraphRDB(dbcon, modelID, null, !dbcon.containsDefaultModel()));
73      }
74
75
76     /**
77      * A model which is stored persistently in a Relational DataBase
78      *
79      * Most applications should not call the constructor - use
80      * ModelRDB.createModel (to create a new model) or
81      * ModelRDB.open (to open an exising model).
82      *
83      * @param p the GraphPersonality of the resulting Model
84      * @param graph a GraphRDB to be exposed through the model interface
85      *
86      * @since Jena 2.0
87      */

88     public ModelRDB(GraphPersonality p, GraphRDB graph) throws RDFRDBException {
89         super( graph, p);
90         m_graphRDB = graph;
91     }
92     
93     public ModelRDB( GraphRDB graph ) {
94         super( graph );
95         m_graphRDB = graph;
96     }
97
98     /**
99      * Open the default model from an existing rdf database. The layout and
100      * datatype type information will be dynamically loaded from the database.
101      *
102      * @param dbcon an IDBConnection specifying the database connection
103      */

104     public static ModelRDB open(IDBConnection dbcon) throws RDFRDBException {
105         return open(dbcon, null);
106     }
107
108     /**
109      * Open an existing rdf database. The layout and datatype type information
110      * will be dynamically loaded from the database.
111      * Will throw an RDFDBException if the database does not seem to formated.
112      * @param dbcon a IDBConnection specifying the database connection
113      * @param name the name of the RDF model to open
114      */

115     public static ModelRDB open(IDBConnection dbcon, String JavaDoc name) throws RDFRDBException {
116         GraphRDB graph = new GraphRDB(dbcon, name, null, GraphRDB.OPTIMIZE_AND_HIDE_ONLY_FULL_REIFICATIONS ,false);
117         return new ModelRDB(BuiltinPersonalities.model, graph);
118     }
119
120     /**
121      * Create a new default model on an existing database.
122      * Will format the database if it has not already been formatted.
123      * @param dbcon a DBConnection specifying the database connection
124      * @since Jena 2.0
125      */

126     public static ModelRDB createModel(IDBConnection dbcon) throws RDFRDBException {
127         return createModel(dbcon, null, getDefaultModelProperties(dbcon));
128     }
129
130     /**
131      * Create a new model on an existing database.
132      * Will format the database if it has not already been formatted.
133      *
134      * <p>
135      * Use the properties to optionally customize the model - this
136      * won't change the results you see when using the model interface,
137      * but it may alter the speed with which you get them or the space
138      * required by the underlying database.</p>
139      *
140      * <p>
141      * The properties must form a complete and consistent set.
142      * The easist way to get a complete and consistent set is to call
143      * getDefaultModelProperties, modify it, and then use that as an argument
144      * in the call.</p>
145      *
146      * @param dbcon a DBConnection specifying the database connection
147      * @param modelProperties a Model containing customization properties
148      * @since Jena 2.0
149      */

150     public static ModelRDB createModel(IDBConnection dbcon, Model modelProperties) throws RDFRDBException {
151         return createModel(dbcon, null, modelProperties);
152     }
153
154     /**
155      * Create a new model on an existing database.
156      * Will format the database if it has not already been formatted.
157      * @param dbcon a DBConnectionI specifying the database connection
158      * @param name the name to give the newly created model.
159      * The name "DEFAULT" is reserved and may not be used for user models.
160      */

161     public static ModelRDB createModel(IDBConnection dbcon, String JavaDoc name) throws RDFRDBException {
162         return createModel(dbcon, name, getDefaultModelProperties(dbcon));
163     }
164
165     /**
166      * Create a new model on an existing database.
167      * Will format the database if it has not already been formatted.
168      *
169      * <p>
170      * Use the properties to optionally customize the model - this
171      * won't change the results you see when using the model interface,
172      * but it may alter the speed with which you get them or the space
173      * required by the underlying database.</p>
174      *
175      * <p>
176      * The properties must form a complete and consistent set.
177      * The easist way to get a complete and consistent set is to call
178      * getDefaultModelProperties, modify it, and then use that as an argument
179      * in the call.</p>
180      *
181      * @param dbcon a DBConnection specifying the database connection
182      * @param name the name to give the newly created model.
183      * The name "DEFAULT" is reserved and may not be used for user models.
184      * @param modelProperties a Model containing customization properties
185      * @since Jena 2.0
186      */

187     public static ModelRDB createModel(IDBConnection dbcon, String JavaDoc name, Model modelProperties) throws RDFRDBException {
188         
189         GraphRDB graph;
190         if( modelProperties != null )
191             graph = new GraphRDB(dbcon, name, modelProperties.getGraph(), GraphRDB.OPTIMIZE_AND_HIDE_ONLY_FULL_REIFICATIONS ,true);
192         else
193             graph = new GraphRDB(dbcon, name, null, GraphRDB.OPTIMIZE_AND_HIDE_ONLY_FULL_REIFICATIONS, true);
194         return new ModelRDB(BuiltinPersonalities.model, graph);
195     }
196
197     /**
198      * Create a new database suitable for storing RDF data. In fact the database has
199      * to exist since jdbc can't create an empty database from a vacuum but it can be empty
200      * and this call will format it with appropriate tables and stored procedures.
201      * <p>
202      * The appropriate RDF-RDB driver to use is assumed to be the class Driver<DatabaseType><LayoutType>.
203      * If that can't be found it defaults to looking for a property file in /etc/Driver<DatabaseType><LayoutType>.config
204      * and uses that to determine the driver class and parameters.</p>
205      *
206      * @param dbcon a DBConnection specifying the database connection
207      * @param layoutType the name of the layout style to use. Currently one of:
208      * "Generic", "Hash", "MMGeneric", "MMHash", "Proc", "ThinProc".
209      * @param databaseType the name of the database type. Currently one of:
210      * "Interbase" "Postgresql" "Mysql" "Oracle". This may seem a little redundant
211      * given that the jdbc uri implicitly contains this information but there is no
212      * standard way of extracting this (esp. if the user connects via a bridge).
213      * @deprecated Since Jena 2.0 this call is no longer needed - it is preferable
214      * to specify the database type when constructing the DBConnection and to modify
215      * the layout by using the properties in the DBConnection. Then use the
216      * call ModelRDB.createModel(IDBConnection)
217      */

218     public static ModelRDB create(IDBConnection dbcon, String JavaDoc layoutType, String JavaDoc databaseType) throws RDFRDBException {
219         dbcon.setDatabaseType(databaseType);
220         return createModel(dbcon, null, getDefaultModelProperties(dbcon));
221     }
222
223     /**
224      * Create a new database suitable for storing RDF data. In fact the database has
225      * to exist since jdbc can't create an empty database from a vacuum but it can be empty
226      * and this call will format it with appropriate tables and stored procedures.
227      * <p>
228      * Uses a default layout format which is able to support multiple models in a single database.
229      * </p>
230      * @param dbcon a DBConnectionI specifying the database connection
231      * @param databaseType the name of the database type. Currently one of:
232      * "Interbase" "Postgresql" "Mysql" "Oracle". This may seem a little redundant
233      * given that the jdbc uri implicitly contains this information but there is no
234      * standard way of extracting this (esp. if the user connects via a bridge).
235      * @deprecated Since Jena 2.0 this call is no longer needed - it is preferable to
236      * specify the database type when constructing the DBConnection. Then use the call
237      * ModelRDB.createModel(IDBConnection)
238      */

239
240     public static ModelRDB create(IDBConnection dbcon, String JavaDoc databaseType) throws RDFRDBException {
241         dbcon.setDatabaseType(databaseType);
242         return createModel(dbcon, null, getDefaultModelProperties(dbcon));
243     }
244
245     /**
246      * Returns a Jena Model containing model-specific properties.
247      * These describe the optimization/layout for this model in the database.
248      *
249      * The returned Model is a copy, modifying it will have no
250      * immediate effect on the database.
251      *
252      *
253      * @since Jena 2.0
254      */

255     
256     public Model getModelProperties() {
257         Model m = ModelFactory.createDefaultModel();
258         Graph g = m.getGraph();
259         ExtendedIterator it = m_graphRDB.getPropertyTriples();
260         while (it.hasNext()) g.add( (Triple) it.next());
261         return m;
262     }
263     
264     /**
265      * Retrieve a default set of model customization properties
266      *
267      * The returned default set of properties is suitable for use in a call to
268      * ModelRDB.create(..., modelProperties);
269      *
270      * @param dbcon a DBConnectionI specifying the database connection
271      * @return Model containing default properties
272      */

273     
274     public static Model getDefaultModelProperties( IDBConnection dbcon ) {
275         return dbcon.getDefaultModelProperties();
276     }
277     
278     /**
279      * List the names of all models stored in the database
280      * @return ExtendedIterator over the model names.
281      */

282      
283      public static ExtendedIterator listModels(IDBConnection dbcon) throws RDFRDBException {
284         return dbcon.getAllModelNames();
285      }
286
287     /** Close the Model and free up resources held.
288      *
289      * <p>Not all implementations of Model require this method to be called. But
290      * some do, so in general its best to call it when done with the object,
291      * rather than leave it to the finalizer.</p>
292      */

293     public void close() {
294         m_graphRDB.close();
295     }
296     
297     /**
298      * Remove all traces of this particular Model from the database.
299      */

300     public void remove() throws RDFRDBException {
301         m_graphRDB.remove();
302     }
303      
304     /**
305      * A convenience function to return the connection
306      */

307     public IDBConnection getConnection() {
308         return m_graphRDB.getConnection();
309     }
310     
311     /**
312      * Remove all the statements from the database which are associated with just this model.
313      * This no longer reformats the database (which makes it safer and useful for multi-model
314      * databases) but means that it is not guaranteed to garbage collect the resource table.
315      * @deprecated Since Jena 2.0 this call is not recommended (it's name
316      * is misleading) - to clear an entire database use DBConnection.cleanDB,
317      * to remove just this Model use Model.remove().
318      */

319      public void clear() throws RDFRDBException {
320         remove();
321      }
322
323     /**
324      * Remove a named model from an existing multi-model database.
325      * Will throw an RDFDBException if the database layout does not support
326      * multiple models or if the database does not seem to formated.
327      * @param dbcon a DBConnectionI specifying the database connection
328      * @param name the name to give the newly created model
329      * @deprecated Since Jena 2.0, to remove a model use the ModelRDB.remove()
330      */

331     public static void deleteModel(IDBConnection dbcon, String JavaDoc name) throws RDFRDBException {
332         ModelRDB modelToDelete = ModelRDB.open(dbcon, name);
333         modelToDelete.remove();
334     }
335
336     /**
337      * Loads all the statements for this model into an in-memory model.
338      * @return a ModelMem containing the whole of the RDB model
339      * @deprecated Since Jena 2.0, this call is not recommended. Instead use
340      * the soon-to-be-released bulk-load functions.
341      */

342     public Model loadAll() {
343         Model m = ModelFactory.createDefaultModel();
344         for (StmtIterator i = this.listStatements(); i.hasNext(); ) {
345             m.add( i.nextStatement() );
346         }
347         return m;
348     }
349     
350     /**
351     * Get the value of DoDuplicateCheck
352     * @return bool boolean
353     */

354     public boolean getDoDuplicateCheck() {
355         return m_graphRDB.getDoDuplicateCheck();
356     }
357     /**
358     * Set the value of DoDuplicateCheck.
359     * @param bool boolean
360     */

361     public void setDoDuplicateCheck(boolean bool) {
362         m_graphRDB.setDoDuplicateCheck(bool);
363     }
364     
365     /**
366      * Set the value of DoFastpath.
367      * @param val boolean
368      */

369     public void setDoFastpath ( boolean val ) {
370         ((DBQueryHandler)m_graphRDB.queryHandler()).setDoFastpath(val);
371     }
372     
373     /**
374      * Get the value of DoFastpath.
375      * @return boolean
376      */

377     public boolean getDoFastpath () {
378         return ((DBQueryHandler)m_graphRDB.queryHandler()).getDoFastpath();
379     }
380
381     /**
382      * Set the value of QueryOnlyAsserted.
383      * @param opt boolean
384      */

385     public void setQueryOnlyAsserted ( boolean opt ) {
386         ((DBQueryHandler)m_graphRDB.queryHandler()).setQueryOnlyAsserted(opt);
387     }
388
389     /**
390      * Get the value of QueryOnlyAsserted.
391      * @return boolean
392      */

393     public boolean getQueryOnlyAsserted() {
394         return ((DBQueryHandler)m_graphRDB.queryHandler()).getQueryOnlyAsserted();
395     }
396
397     /**
398      * Set the value of QueryOnlyReified.
399      * @param opt boolean
400      */

401     public void setQueryOnlyReified ( boolean opt ) {
402         ((DBQueryHandler)m_graphRDB.queryHandler()).setQueryOnlyReified(opt);
403     }
404
405     /**
406      * Get the value of QueryOnlyReified.
407      * @return boolean
408      */

409     public boolean getQueryOnlyReified() {
410         return ((DBQueryHandler)m_graphRDB.queryHandler()).getQueryOnlyReified();
411     }
412
413     /**
414      * Set the value of QueryFullReified.
415      * @param opt boolean
416      */

417     public void setQueryFullReified ( boolean opt ) {
418         ((DBQueryHandler)m_graphRDB.queryHandler()).setQueryFullReified(opt);
419     }
420
421     /**
422      * Get the value of QueryFullReified.
423      * @return boolean
424      */

425     public boolean getQueryFullReified() {
426         return ((DBQueryHandler)m_graphRDB.queryHandler()).getQueryFullReified();
427     }
428     
429     /**
430      * Set the value of DoImplicitJoin.
431      * @param val boolean
432      */

433     public void setDoImplicitJoin ( boolean val ) {
434         ((DBQueryHandler)m_graphRDB.queryHandler()).setDoImplicitJoin(val);
435     }
436
437     
438 }
439
440 /*
441     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
442     All rights reserved.
443
444     Redistribution and use in source and binary forms, with or without
445     modification, are permitted provided that the following conditions
446     are met:
447
448     1. Redistributions of source code must retain the above copyright
449        notice, this list of conditions and the following disclaimer.
450
451     2. Redistributions in binary form must reproduce the above copyright
452        notice, this list of conditions and the following disclaimer in the
453        documentation and/or other materials provided with the distribution.
454
455     3. The name of the author may not be used to endorse or promote products
456        derived from this software without specific prior written permission.
457
458     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
459     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
460     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
461     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
462     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
463     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
464     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
465     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
466     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
467     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
468 */

469
Popular Tags