001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.mandarax.sql;
019:
020: import java.sql.Connection;
021: import java.sql.SQLException;
022: import java.util.List;
023: import java.util.Map;
024: import org.mandarax.util.logging.LogCategories;
025: import org.mandarax.kernel.Clause;
026: import org.mandarax.kernel.ClauseSetException;
027: import org.mandarax.kernel.Fact;
028: import org.mandarax.kernel.LogicFactory;
029: import org.mandarax.kernel.Term;
030:
031: /**
032: * Clause set iterator for SQL clause sets.
033: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
034: * @version 3.4 <7 March 05>
035: * @since 1.5
036: */
037: class SQLClauseIterator extends
038: org.mandarax.util.AbstractClauseIterator implements
039: LogCategories {
040:
041: private SQLPredicate predicate;
042: private java.sql.ResultSet rs = null;
043: private Fact nextFact = null;
044: private LogicFactory logFact = LogicFactory.getDefaultFactory();
045: private Class[] struct = null;
046: private SQLTypeMapping typeMapping = null;
047: private Map map = null;
048: private List cache = null;
049: private boolean closeConnection = false;
050: private Connection connection = null;
051:
052: /**
053: * Constructor.
054: * @param p the predicate
055: * @param queryResult the result set
056: * @param c the cache list to collect facts - if null, nothing will be cached
057: * @param closeConnection whether to close the connection at the end
058: * @param con the connection used
059: */
060: SQLClauseIterator(SQLPredicate p, java.sql.ResultSet queryResult,
061: List c, boolean closeConnection, Connection con) {
062: super ();
063: predicate = p;
064: rs = queryResult;
065: struct = predicate.getStructure();
066: typeMapping = predicate.getTypeMapping();
067: map = (typeMapping == null) ? null : typeMapping
068: .getTypeMapping();
069: cache = c;
070: this .closeConnection = closeConnection;
071: this .connection = con;
072: }
073:
074: /**
075: * Indicates whether there are more clauses.
076: * @return true if there are more clauses, false otherwise
077: * @throws ClauseSetException
078: */
079: public boolean hasMoreClauses() throws ClauseSetException {
080: try {
081: boolean hasNext = rs.next();
082: if (hasNext) {
083: Term[] terms = new Term[struct.length];
084: for (int i = 0; i < struct.length; i++) {
085: if (typeMapping == null) {
086: terms[i] = logFact.createConstantTerm(rs
087: .getObject(i + 1));
088: } else {
089: terms[i] = logFact
090: .createConstantTerm(typeMapping
091: .postMap((map == null) ? rs
092: .getObject(i + 1) : rs
093: .getObject(i + 1, map)));
094: }
095: }
096: nextFact = logFact.createFact(predicate, terms);
097: if (cache != null) {
098: cache.add(nextFact);
099: }
100: return true;
101: } else {
102: nextFact = null;
103: return false;
104: }
105: } catch (SQLException x) {
106: nextFact = null;
107: throw new ClauseSetException(
108: "Exception building facts from records", x);
109: }
110: }
111:
112: /**
113: * Get the next clause.
114: * @return the next clause
115: * @throws ClauseSetException
116: */
117: public Clause nextClause() throws ClauseSetException {
118: return nextFact;
119: }
120:
121: /**
122: * Close the iterator and release all resources.
123: */
124: public void close() {
125: try {
126: if (rs != null && closeConnection) {
127: rs.close();
128: if (LOG_SQL.isDebugEnabled())
129: LOG_SQL
130: .debug("JDBC connection released by SQLClauseIterator "
131: + this );
132: }
133: } catch (SQLException x) {
134: LOG_SQL
135: .error(
136: "Exception closing result set in SQL clause iterator",
137: x);
138: }
139: try {
140: if (connection != null && closeConnection)
141: connection.close();
142: } catch (SQLException x) {
143: LOG_SQL
144: .error(
145: "Exception closing connection in SQL clause iterator",
146: x);
147: }
148: }
149: }
|