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

Quick Search    Search Deep

Source code: jbreport/data/Query.java


1   /*
2    * $Id: Query.java,v 1.1.1.1 2000/08/31 13:14:30 grantfin Exp $
3    *
4    * jbReport - A reporting library for Java
5    * Copyright (C) 2000 Grant Finnemore <grantfin@users.sourceforge.net>
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   */
21  package jbreport.data;
22  
23  import java.sql.ResultSet;
24  import java.sql.SQLException;
25  import java.sql.Statement;
26  import java.util.ArrayList;
27  import java.util.Collections;
28  import java.util.List;
29  import java.util.Iterator;
30  import org.xml.sax.Attributes;
31  
32  import jbreport.Constants;
33  import jbreport.ReportElement;
34  import jbreport.ReportException;
35  import jbreport.core.AbstractReportElement;
36  import jbreport.core.ReportVisitor;
37  import jbreport.core.ReportVisitorState;
38  import jbreport.core.RepositoryImpl;
39  import jbreport.core.XMLHandler;
40  import jbreport.core.XMLParsingElement;
41  
42  /**
43   * This provides for a named query to be captured from an xml definition. It
44   * is executed against a predefined datasource.
45   *
46   * @author Grant Finnemore
47   * @version $Revision: 1.1.1.1 $
48   */
49  public
50  class Query extends AbstractReportElement implements XMLParsingElement {
51  
52     /** The name of the datasource that will be used to execute this query */
53     private String datasourceName;
54  
55     /** The bound parameters available for this query */
56     private List params;
57  
58     //
59     // Constructors
60     //
61  
62     //
63     // Methods overloaded from AbstractReportElement
64     //
65  
66     public void accept(ReportVisitor visitor, ReportVisitorState state) 
67        throws ReportException {
68        visitor.visitQuery(this, state);
69     }
70  
71     public List getList(String property) {
72        List result = Collections.EMPTY_LIST;
73        if(Constants.BOUND_PARAMS.equals(property)) {
74           if(params != null) {
75              result = Collections.unmodifiableList(params);
76           }
77        }
78        else {
79           result = super.getList(property);
80        }
81        return result;
82     }
83  
84     public void xmlInitialize(String localName, Attributes attributes) 
85        throws ReportException {
86  
87        super.xmlInitialize(localName, attributes);
88        datasourceName = attributes.getValue("datasource");
89     }
90  
91     //
92     // Implementation of the XMLParsingElement interface
93     //
94  
95     public boolean xmlCanParse() {
96        return true;
97     }
98     
99     public void xmlParse(XMLHandler handler, String localName, 
100                         Attributes attributes) {
101       if("param".equals(localName)) {
102          if(params == null) {
103             params = new ArrayList();
104          }
105          String value = attributes.getValue("name");
106          assertNotNull(value, "name");
107          params.add(value);
108       }
109       else {
110          throw new IllegalArgumentException("localName is " + localName);
111       }
112    }
113 
114    public void xmlCData(XMLHandler handler, String cdata) {
115 
116    }
117 
118    //
119    // Action methods
120    //
121 
122    /**
123     * Execute the query and return the appropriate resultset
124     *
125     * @param elem needed to do any variable substitutions in the query
126     */
127    public QueryResult executeQuery(ReportElement elem) throws ReportException {
128       Datasource datasource = 
129          ((RepositoryImpl)getRepository()).fetchDatasource(datasourceName);
130       try {
131          Statement statement = datasource.getConnection().createStatement();
132          String query = parseQuery(cdata, elem);
133          return new ResultSetAdapter(statement.executeQuery(query));
134       }
135       catch(SQLException e) {
136          throw new ReportException(e);
137       }
138    }
139 
140    //
141    // Implementation methods
142    //
143    
144    /**
145     * This will do the appropriate substitutions on the query string.
146     */
147    private String parseQuery(String query, ReportElement elem) {
148       StringBuffer buf = new StringBuffer(query);
149       if(params != null) {
150          for(Iterator it = params.iterator(); it.hasNext();) {
151             String param = (String)it.next();
152             String value = elem.getBoundParamValue(param);
153             assertNotNull(value, "value");
154             int idx = buf.toString().indexOf(":" + param);
155             if(idx >= 0) {
156                buf.replace(idx, idx + param.length() + 1, value);
157             }
158          }
159       }
160       return buf.toString();
161    }
162    
163 }