001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: DBQueryStage.java,v 1.17 2008/01/02 12:08:23 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.db.impl;
008:
009: import java.sql.PreparedStatement;
010: import java.sql.ResultSet;
011: import java.sql.SQLException;
012: import java.util.List;
013:
014: import com.hp.hpl.jena.db.GraphRDB;
015: import com.hp.hpl.jena.db.IDBConnection;
016: import com.hp.hpl.jena.db.RDFRDBException;
017: import com.hp.hpl.jena.graph.*;
018: import com.hp.hpl.jena.graph.query.BufferPipe;
019: import com.hp.hpl.jena.graph.query.Domain;
020: import com.hp.hpl.jena.graph.query.ExpressionSet;
021: import com.hp.hpl.jena.graph.query.Pipe;
022: import com.hp.hpl.jena.graph.query.Stage;
023: import com.hp.hpl.jena.shared.JenaException;
024:
025: /**
026: @author hedgehog
027: */
028:
029: public class DBQueryStage extends Stage {
030: protected Graph graph;
031: protected DBQuery compiled;
032:
033: public DBQueryStage(GraphRDB graph, SpecializedGraph sg,
034: List varList, List dbPat, ExpressionSet constraints) {
035: this .graph = graph;
036: this .compiled = compile(sg, varList, dbPat, constraints);
037: // System.err.println( " " + this.compiled.stmt.toString().replaceAll( " AND ", "\n AND " ).replaceAll( " Where ", "\n Where " ).replaceAll( " From ", "\n From " ) );
038: }
039:
040: protected DBQuery compile(SpecializedGraph sg, List varList,
041: List dbPat, ExpressionSet constraints) {
042: return compile(compiler, sg, varList, dbPat, constraints);
043: }
044:
045: protected DBQuery compile(DBQueryStageCompiler compiler,
046: SpecializedGraph sg, List varList, List dbPat,
047: ExpressionSet constraints) {
048: return DBQueryStageCompiler.compile(compiler,
049: (DBQueryHandler) graph.queryHandler(), sg, varList,
050: dbPat, constraints);
051: }
052:
053: private static final DBQueryStageCompiler compiler = new DBQueryStageCompiler();
054:
055: protected void run(Pipe source, Pipe sink) {
056: PreparedStatement ps = null;
057: try {
058: if (!compiled.isEmpty)
059: ps = getPreparedStatement();
060:
061: if (ps != null)
062: while (source.hasNext())
063: extendSourceBinding(source.get(), sink, ps);
064: } finally {
065: if (ps != null)
066: closePreparedStatement(ps);
067: if (sink != null)
068: sink.close();
069: }
070: }
071:
072: private void extendSourceBinding(Domain current, Pipe sink,
073: PreparedStatement ps) {
074: ResultSet rs = null;
075: ResultSetIterator it = null;
076: setArgs(current, ps);
077: // System.out.println( ">> " + compiled.stmt.toString().replaceAll( " AND ", "\n AND " ) );
078: try {
079: it = new ResultSetIterator();
080: ps.execute();
081: rs = ps.getResultSet();
082: it.reset(rs, ps);
083: while (it.hasNext()) {
084: Domain useme = current.copy();
085: List row = (List) it.next();
086: for (int i = 0; i < compiled.resList.length; i++) {
087: int j = compiled.resList[i];
088: String o = (String) row.get(i);
089: Node n = compiled.driver.RDBStringToNode(o);
090: useme.setElement(j, n);
091: }
092: sink.put(useme);
093: }
094: } catch (Exception e) {
095: throw new JenaException("Query execute failed: " + e);
096: } finally {
097: if (it != null)
098: it.close();
099: if (rs != null)
100: closeResultSet(rs);
101: }
102: }
103:
104: private void closePreparedStatement(PreparedStatement ps) {
105: try {
106: ps.close();
107: } catch (Exception e) {
108: throw new JenaException("Close on prepared stmt failed: "
109: + e);
110: }
111: }
112:
113: private void closeResultSet(ResultSet rs) {
114: try {
115: rs.close();
116: } catch (SQLException e) {
117: throw new RDFRDBException(
118: "Failed to get last inserted ID: " + e);
119: }
120: }
121:
122: private PreparedStatement getPreparedStatement() {
123: try {
124: IDBConnection conn = compiled.driver.getConnection();
125: return conn.getConnection().prepareStatement(compiled.stmt);
126: } catch (Exception e) {
127: throw new JenaException("Query prepare failed: " + e);
128: }
129: }
130:
131: protected void setArgs(Domain args, PreparedStatement ps) {
132: try {
133: for (int i = 0; i < compiled.argCnt; i++) {
134: int ix = ((Integer) compiled.argIndex.get(i))
135: .intValue();
136: Node arg = (Node) args.get(ix);
137: if (arg == null)
138: throw new JenaException("Null query argument");
139: String val = compiled.driver
140: .nodeToRDBString(arg, false);
141: ps.setString(i + 1, val);
142: }
143: } catch (SQLException e) {
144: throw new JenaException("Bad query argument", e);
145: }
146:
147: }
148:
149: public Pipe deliver(final Pipe result) {
150: final Pipe stream = previous.deliver(new BufferPipe());
151: new Thread() {
152: public void run() {
153: DBQueryStage.this .run(stream, result);
154: }
155: }.start();
156: return result;
157: }
158:
159: }
160:
161: /*
162: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
163: All rights reserved.
164:
165: Redistribution and use in source and binary forms, with or without
166: modification, are permitted provided that the following conditions
167: are met:
168:
169: 1. Redistributions of source code must retain the above copyright
170: notice, this list of conditions and the following disclaimer.
171:
172: 2. Redistributions in binary form must reproduce the above copyright
173: notice, this list of conditions and the following disclaimer in the
174: documentation and/or other materials provided with the distribution.
175:
176: 3. The name of the author may not be used to endorse or promote products
177: derived from this software without specific prior written permission.
178:
179: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
180: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
182: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
183: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
184: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
185: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
186: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
187: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
188: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
189: */
|