KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > db > impl > DBQueryStage


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: DBQueryStage.java,v 1.9 2005/02/21 12:02:50 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.db.impl;
8
9 import java.sql.PreparedStatement JavaDoc;
10 import java.sql.ResultSet JavaDoc;
11 import java.util.List JavaDoc;
12
13 import com.hp.hpl.jena.db.GraphRDB;
14 import com.hp.hpl.jena.db.IDBConnection;
15 import com.hp.hpl.jena.graph.*;
16 import com.hp.hpl.jena.graph.query.BufferPipe;
17 import com.hp.hpl.jena.graph.query.Domain;
18 import com.hp.hpl.jena.graph.query.ExpressionSet;
19 import com.hp.hpl.jena.graph.query.Pipe;
20 import com.hp.hpl.jena.graph.query.Stage;
21 import com.hp.hpl.jena.shared.JenaException;
22
23 /**
24     @author hedgehog
25 */

26
27 public class DBQueryStage extends Stage
28     {
29     protected Graph graph;
30     protected DBQuery compiled;
31             
32     public DBQueryStage( GraphRDB graph, SpecializedGraph sg,
33                     List JavaDoc varList, List JavaDoc dbPat, ExpressionSet constraints )
34         {
35         this.graph = graph;
36         this.compiled = compile (sg, varList, dbPat, constraints);
37         }
38
39     protected DBQuery compile( SpecializedGraph sg, List JavaDoc varList, List JavaDoc dbPat, ExpressionSet constraints )
40         { return compile( compiler, sg, varList, dbPat, constraints ); }
41         
42     protected DBQuery compile( DBQueryStageCompiler compiler, SpecializedGraph sg,
43                 List JavaDoc varList, List JavaDoc dbPat, ExpressionSet constraints )
44         {
45         return DBQueryStageCompiler.compile( compiler, (DBQueryHandler) graph.queryHandler(),
46                     sg, varList, dbPat, constraints );
47         }
48                  
49     private static final DBQueryStageCompiler compiler = new DBQueryStageCompiler();
50         
51     protected void run(Pipe source, Pipe sink) {
52         PreparedStatement JavaDoc ps = null;
53         Domain current;
54         Domain useme;
55         IDBConnection conn;
56         if ( !compiled.isEmpty ) try {
57             conn = compiled.driver.getConnection();
58             ps = conn.getConnection().prepareStatement(compiled.stmt);
59          } catch (Exception JavaDoc e) {
60              throw new JenaException("Query prepare failed: " + e);
61          }
62
63         if ( ps != null) while (source.hasNext()) {
64             current = source.get();
65             setArgs(current, ps);
66 // System.out.println(compiled.stmt);
67

68             try {
69                 ResultSetIterator it = new ResultSetIterator();
70                 ps.execute();
71                 ResultSet JavaDoc rs = ps.getResultSet();
72                 it.reset(rs, ps, null, null);
73                 while (it.hasNext()) {
74                     useme = current.copy();
75                     List JavaDoc row = (List JavaDoc) it.next();
76                     for(int i=0;i<compiled.resList.length;i++) {
77                         int j = compiled.resList[i];
78                         String JavaDoc o = (String JavaDoc) row.get(i);
79                         Node n = compiled.driver.RDBStringToNode(o);
80                         useme.setElement(j,n);
81                     }
82                     sink.put(useme);
83                 }
84                 it.close();
85             } catch (Exception JavaDoc e) {
86                 throw new JenaException("Query execute failed: " + e);
87             }
88         }
89         if ( ps != null ) try {
90             ps.close();
91         } catch (Exception JavaDoc e) {
92             throw new JenaException("Close on repared stmt failed: " + e);
93         }
94         sink.close();
95     }
96         
97     protected void setArgs ( Domain args, PreparedStatement JavaDoc ps ) {
98         int i, ix;
99         String JavaDoc val;
100         Node arg;
101         try {
102             for ( i=0;i<compiled.argCnt;i++) {
103                 ix = ((Integer JavaDoc)compiled.argIndex.get(i)).intValue();
104                 arg = (Node) args.get(ix);
105                 if ( arg == null ) throw new JenaException("Null query argument");
106                 val = compiled.driver.nodeToRDBString(arg,false);
107                 ps.setString(i+1, val);
108             }
109         } catch (Exception JavaDoc e) {
110             throw new JenaException("Bad query argument: " + e);
111         }
112
113     }
114
115     public Pipe deliver( final Pipe result )
116         {
117         final Pipe stream = previous.deliver( new BufferPipe() );
118         new Thread JavaDoc() { public void run() { DBQueryStage.this.run( stream, result ); } } .start();
119         return result;
120         }
121       
122     }
123
124 /*
125     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
126     All rights reserved.
127
128     Redistribution and use in source and binary forms, with or without
129     modification, are permitted provided that the following conditions
130     are met:
131
132     1. Redistributions of source code must retain the above copyright
133        notice, this list of conditions and the following disclaimer.
134
135     2. Redistributions in binary form must reproduce the above copyright
136        notice, this list of conditions and the following disclaimer in the
137        documentation and/or other materials provided with the distribution.
138
139     3. The name of the author may not be used to endorse or promote products
140        derived from this software without specific prior written permission.
141
142     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
143     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
144     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
145     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
146     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
147     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
148     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
149     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
150     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
151     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
152 */

153
Popular Tags