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

Quick Search    Search Deep

Source code: nl/aidministrator/rdf/ral/ordbms/SqlNamespaceIterator.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.ordbms;
31  
32  import nl.aidministrator.rdf.ral.*;
33  import nl.aidministrator.rdf.sail.model.*;
34  import nl.aidministrator.rdf.util.*;
35  import java.util.*;
36  import java.sql.*;
37  
38  /**
39   * A NamespaceIterator that executes an SQL query. The ResultSet should
40   * contain at least two columns. The value of these columns is expected
41   * to represent the <em>namespace prefix</em> and the <em>namespace name</em>
42   * respectively.
43   **/
44  public class SqlNamespaceIterator implements NamespaceIterator {
45  
46  /*-----------------------------------------+
47  | Variables                                |
48  +-----------------------------------------*/
49  
50    /**
51     * ResultSet for the query.
52     **/
53    protected ResultSet _resultSet;
54  
55    /**
56     * SQL statement that was used to generate the ResultSet, but which
57     * cannot be closed until all results have been read from the
58     * ResultSet.
59     **/
60    protected java.sql.Statement _statement;
61  
62    /**
63     * Connection to the database.
64     **/
65    protected Connection _dbConn;
66  
67    /**
68     * The query to execute.
69     **/
70    protected String _query;
71  
72    /**
73     * Flag indicating whether there are any more results.
74     **/
75    protected boolean _hasNext;
76    
77    /** Namespace prefix. **/
78    protected String _prefix;
79  
80    /** Namespace name. **/
81    protected String _name;
82  
83  /*-----------------------------------------+
84  | Constructors                             |
85  +-----------------------------------------*/
86  
87    /**
88     * Calls the supplied query and returns its results one by one.
89     * The query result should contain (at least) three columns. The
90     * first is expected to be the <em>subject</em>, the second the
91     * <em>predicate</em>, * and the third the <em>object</em>.
92     **/
93    public SqlNamespaceIterator(Connection dbConn, String query) {
94      _dbConn = dbConn;
95      _query = query;
96      _execQuery();
97    }
98  
99  /*-----------------------------------------+
100 | Methods                                  |
101 +-----------------------------------------*/
102 
103   private void _execQuery() {
104     try {
105       // Create statement
106       _statement = _dbConn.createStatement();
107 
108       // Execute query
109       _resultSet = _statement.executeQuery(_query);
110 
111       // Check if the new ResultSet contains any values.
112       _hasNext = _resultSet.next();
113 
114       if (!_hasNext) {
115         // No result for this query
116         close();
117       }
118     }
119     catch (SQLException e) {
120       System.err.println("SqlException: " + e.getMessage());
121       e.printStackTrace();
122       _hasNext = false;
123     }
124   }
125 
126   /**
127    * Checks whether there are any more results available.
128    **/
129   public boolean hasNext() {
130     return _hasNext;
131   }
132 
133   /**
134    * Gets the next result.
135    *
136    * @exception NoSuchElementException If there are no more results
137    * available.
138    **/
139   public void next() {
140     if (_hasNext) {
141       try {
142         _prefix = _resultSet.getString(1);
143         _name = _resultSet.getString(2);
144 
145         _hasNext = _resultSet.next();
146 
147         if (!_hasNext) {
148           // No result for this query
149           close();
150         }
151       }
152       catch (SQLException e) {
153         System.err.println("SqlException: " + e.getMessage());
154         e.printStackTrace();
155         _hasNext = false;
156       }
157     }
158     else {
159       throw new NoSuchElementException("No more statements");
160     }
161   }
162 
163   /**
164    * Gets the namespace prefix.
165    **/
166   public String getPrefix() {
167     return _prefix;
168   }
169 
170   /**
171    * Gets the namespace name.
172    **/
173   public String getName() {
174     return _name;
175   }
176 
177   public void close() {
178     if (_dbConn != null) {
179       _hasNext = false;
180 
181       try {
182         if (_resultSet != null) {
183           _resultSet.close();
184         }
185         if (_statement != null) {
186           _statement.close();
187         }
188         _dbConn.close();
189       }
190       catch (SQLException e) {
191               System.err.println("SqlException: " + e.getMessage());
192               e.printStackTrace();
193           }
194 
195       _dbConn = null;
196     }
197   }
198 
199   protected void finalize() {
200         close();
201     }
202 }