1 6 7 package com.hp.hpl.jena.db.impl; 8 9 import java.sql.PreparedStatement ; 10 import java.sql.ResultSet ; 11 import java.util.List ; 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 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 varList, List 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 varList, List dbPat, ExpressionSet constraints ) 40 { return compile( compiler, sg, varList, dbPat, constraints ); } 41 42 protected DBQuery compile( DBQueryStageCompiler compiler, SpecializedGraph sg, 43 List varList, List 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 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 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 68 try { 69 ResultSetIterator it = new ResultSetIterator(); 70 ps.execute(); 71 ResultSet rs = ps.getResultSet(); 72 it.reset(rs, ps, null, null); 73 while (it.hasNext()) { 74 useme = current.copy(); 75 List row = (List ) it.next(); 76 for(int i=0;i<compiled.resList.length;i++) { 77 int j = compiled.resList[i]; 78 String o = (String ) 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 e) { 86 throw new JenaException("Query execute failed: " + e); 87 } 88 } 89 if ( ps != null ) try { 90 ps.close(); 91 } catch (Exception e) { 92 throw new JenaException("Close on repared stmt failed: " + e); 93 } 94 sink.close(); 95 } 96 97 protected void setArgs ( Domain args, PreparedStatement ps ) { 98 int i, ix; 99 String val; 100 Node arg; 101 try { 102 for ( i=0;i<compiled.argCnt;i++) { 103 ix = ((Integer )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 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 () { public void run() { DBQueryStage.this.run( stream, result ); } } .start(); 119 return result; 120 } 121 122 } 123 124 153 | Popular Tags |