View Javadoc

1   /*
2    ORDI - Ontology Repository and Data Integration
3   
4    Copyright (c) 2004-2007, OntoText Lab. / SIRMA
5   
6    This library is free software; you can redistribute it and/or modify it under
7    the terms of the GNU Lesser General Public License as published by the Free
8    Software Foundation; either version 2.1 of the License, or (at your option)
9    any later version.
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13   details.
14   You should have received a copy of the GNU Lesser General Public License along
15   with this library; if not, write to the Free Software Foundation, Inc.,
16   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17   */
18  package com.ontotext.ordi.mapper.rdbms;
19  
20  import java.sql.PreparedStatement;
21  import java.sql.ResultSet;
22  import java.sql.SQLException;
23  
24  import org.apache.log4j.Logger;
25  import org.openrdf.model.Resource;
26  import org.openrdf.model.URI;
27  import org.openrdf.model.Value;
28  
29  import com.ontotext.ordi.exception.ORDIRuntimeException;
30  import com.ontotext.ordi.mapper.model.PredicatePattern;
31  import com.ontotext.ordi.mapper.model.RDFResultSet;
32  import com.ontotext.ordi.mapper.processor.RDFResultManager;
33  
34  public abstract class JdbcRDFResultSetBase implements RDFResultSet {
35  
36      protected PredicatePattern predicatePattern;
37      protected PreparedStatement statement;
38      protected ResultSet result;
39      protected RDFResultManager manager;
40      private static Logger logger = Logger.getLogger(JdbcRDFResultSetBase.class);
41  
42      /**
43       * 
44       * @param statement
45       * @param predicatePattern
46       * @param manager
47       *            may be null if the
48       */
49      public JdbcRDFResultSetBase(PreparedStatement statement,
50              PredicatePattern predicatePattern, RDFResultManager manager) {
51          if (statement == null || predicatePattern == null) {
52              throw new IllegalArgumentException();
53          }
54          this.predicatePattern = predicatePattern;
55          this.statement = statement;
56          if (manager != null) {
57              this.manager = manager;
58              this.manager.register(this);
59          }
60      }
61  
62      public void run() {
63          RDFResultManager.threadCount++;
64          try {
65              statement.setFetchSize(10000);
66              result = statement.executeQuery();
67          } catch (SQLException e) {
68              logger.error(String.format("Error while executing %s!", statement));
69              logger.error(e);
70          } finally {
71              if (manager != null)
72                  manager.registerComplete(this);
73              RDFResultManager.threadCount--;
74          }
75      }
76  
77      public void close() {
78          try {
79              if (result != null) {
80                  result.close();
81              }
82          } catch (SQLException e) {
83              throw new ORDIRuntimeException("Error while closing!", e);
84          }
85      }
86  
87      public URI getNamedGraph() {
88          return predicatePattern.getNamedGraph();
89      }
90  
91      public URI getPredicate() {
92          return predicatePattern.getPredicate();
93      }
94  
95      public abstract Resource getSubject();
96  
97      public abstract Value getObject();
98  
99      public abstract URI[] getTriplesets();
100 
101     public abstract boolean next();
102 }