Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: nl/aidministrator/rdf/ral/rdbms/SQLValueIterator.java


1   /*  Sesame - Storage and Querying architecture for RDF and RDF Schema
2    *  Copyright (C) 2002 Aidministrator Nederland b.v.
3    *
4    *  Contact: 
5    *  Aidministrator Nederland b.v.
6    *  Julianaplein 14b 
7    *  3817 CS Amersfoort
8    *  The Netherlands
9    *  tel. +31(0)33 4659987
10   *  fax. +31(0)33 4659987
11   *  sesame@aidministrator.nl
12   *
13   *   http://www.aidministrator.nl/
14   *  
15   *  This library is free software; you can redistribute it and/or
16   *  modify it under the terms of the GNU Lesser General Public
17   *  License as published by the Free Software Foundation; either
18   *  version 2.1 of the License, or (at your option) any later version.
19   *
20   *  This library is distributed in the hope that it will be useful,
21   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   *  Lesser General Public License for more details.
24   *
25   *  You should have received a copy of the GNU Lesser General Public
26   *  License along with this library; if not, write to the Free Software
27   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28   */
29  
30  package nl.aidministrator.rdf.ral.rdbms;
31  
32  import nl.aidministrator.rdf.ral.ResourceIterator;
33  import nl.aidministrator.rdf.sail.model.*;
34  import java.util.NoSuchElementException;
35  import java.sql.*;
36  
37  /**
38   * A ResourceIterator that executes an SQL query. SQLValueIterator is an
39   * abstract class, no instances of SQLValueIterator can be created. A subclass
40   * of SQLValueIterator is defined that either iterates rdfs:Resources or
41   * rdfs:Literals. This subclass must implement getResult(), the implementation of
42   * getResult() must call the proper contructor of either rdfs:Resource or
43   * rdfs:Literal.
44   *
45   * @author Peter van 't Hof
46   * @version 1.3, 12/13/01
47   */
48  
49  public abstract class SQLValueIterator implements ResourceIterator {
50  
51  /*----------+
52  | Variables |
53  +----------*/
54  
55    /**
56     * ResultSet for the query. */
57    protected ResultSet _resultSet;
58  
59    /**
60     * SQL Statement that was used to generate the ResultSet. It cannot be closed
61     * until all results have been read from the ResultSet.
62     */
63    protected java.sql.Statement _statement;
64    /**
65     * Connection to the database. */
66    protected Connection _databaseCon;
67    /**
68     * Query to execute. */
69    protected String _query;
70    /**
71     * Flag indicating whether there are any more results. */
72    protected boolean _hasNext;
73  
74  /*-------------+
75  | Constructors |
76  +-------------*/
77  
78    /**
79     * Constructor.
80     *
81     * @param databaseCon connection to the repository
82     * @param query to execute
83     */
84    public SQLValueIterator(Connection databaseCon, String query) {
85      _databaseCon = databaseCon;
86      _query = query;
87      _execQuery();
88  
89      if (!_hasNext) {  // No result for this query, close Connection, the java.sql.Statement and its ResultSet.
90        close();
91      }
92    }
93  
94  /*--------+
95  | Methods |
96  +--------*/
97  
98    private void _execQuery() {
99      try {
100        _statement = _databaseCon.createStatement();
101       _resultSet = _statement.executeQuery(_query);
102 
103       _hasNext = _resultSet.next();  // Check if the new ResultSet contains any values.
104     }
105     catch (SQLException e) {
106        // FIXME: should throw a RalException
107 
108       System.err.println("SQLException in _execQuery()...");
109       close();
110     }
111   }
112 
113   public boolean hasNext() {
114     return _hasNext;
115   }
116 
117   public Value next() {
118     if (_hasNext) {
119       Value result = null;
120       try {
121         result = getResult();
122 
123         _hasNext = _resultSet.next();
124 
125         if (!_hasNext) {  // No more results for this query.
126           close();
127         }
128       }
129       catch (SQLException e) {
130         // FIXME: should throw a RalException
131 
132         System.err.println("SQLException in next()...");
133         close();
134       }
135       return result;
136     }
137     else {
138       throw new NoSuchElementException("No more values...");
139     }
140   }
141 
142   public void close() {
143     try {
144       if (_resultSet != null) {
145         _resultSet.close();
146       }
147       if (_statement != null) {
148         _statement.close();
149       }
150       _databaseCon.close();
151     }
152     catch (SQLException e) {
153       // FIXME: should throw a RalException
154 
155       System.err.println("SQLException in close()...");
156     }
157     _hasNext = false;  // No more results are returned.
158   }
159 
160   /**
161    * Called by the garbage collector on SQLValueIterator when garbage
162    * collection determines that there are no more references to the
163    * SQLValueIterator.
164    */
165   protected void finalize() {
166     close();
167   }
168 
169   /**
170    * Gets the result from the ResultSet. Must be implemented by
171    * SQLResourceIterator or SQLLiteralIterator in order to call the proper
172    * constructor of either Resource or Literal.
173    *
174    * @return result from ResultSet
175    */
176   protected abstract Value getResult()
177     throws SQLException;
178 }