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

Quick Search    Search Deep

Source code: com/dghda/kent/reports/SQLQueryMapping.java


1   /* Copyright (C) 2001 Duane Griffin <duanegriffin@users.sourceforge.net>
2      This file is part of Kent.
3   
4      Kent is free software; you can redistribute it and/or
5      modify it under the terms of the GNU General Public License as
6      published by the Free Software Foundation; either version 2 of the
7      License, or (at your option) any later version.
8   
9      Kent is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12     General Public License for more details.
13  
14     You should have received a copy of the GNU General Public
15     License along with Kent; see the file COPYING.  If not,
16     write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17     Boston, MA 02111-1307, USA.
18  */
19  
20  package com.dghda.kent.reports;
21  
22  import java.sql.*;
23  import java.util.*;
24  
25  import com.dghda.kent.*;
26  
27  /** A class which maps SQL query results to a data set. */
28  public class SQLQueryMapping {
29    
30    /** Create a new mapping using the given SQL statement. */
31    public SQLQueryMapping (String name, PreparedStatement stmt) {
32      m_Name = name;
33      m_Query = stmt;
34    }
35    
36    /** Adds the given mapping from the query result to report data. */
37    public void addMapping (String sqlColumn, String dataColumn) {
38      m_Mappings.put (sqlColumn, dataColumn);
39    }
40    
41    /** Gets the SQL query that will be used to query the DB. */
42    public PreparedStatement getSQLQuery() {
43      return m_Query;
44    }
45    
46    /**
47       Executes the query, and constructs a data set from the results.
48    */
49    public ReportData.DataSet execute() throws SQLException {
50      
51      // Prepare column mappings
52      int columnCount = m_Mappings.size();
53      int [] sqlColumn = new int[columnCount];
54      String [] dataColumn = new String[columnCount];
55      
56      // Run the query
57      ResultSet data = m_Query.executeQuery();
58      
59      // Populate column mappings
60      columnCount = 0;
61      Iterator i = m_Mappings.entrySet().iterator();
62      while (i.hasNext()) {
63        Map.Entry entry = (Map.Entry) i.next();
64        sqlColumn[columnCount] = data.findColumn ((String) entry.getKey());
65        dataColumn[columnCount] = (String) entry.getValue();
66        ++columnCount;
67      }
68      
69      // Prepare & populate the data set
70      ReportData.DataSet result = new ReportData.DataSet (m_Name, dataColumn);
71      while (data.next()) {
72        String [] row = new String[sqlColumn.length];
73        for (int index = 0; index != sqlColumn.length; ++index)
74          row[index] = data.getString (sqlColumn[index]);
75        result.addRow (row);
76      }
77      
78      return result;
79    }
80    
81    private String m_Name;
82    private PreparedStatement m_Query;
83    private TreeMap m_Mappings = new TreeMap();
84  }