KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > table > model > sql > SimpleSqlTableDataSource


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.contrib.table.model.sql;
16
17 import java.sql.Connection JavaDoc;
18 import java.sql.ResultSet JavaDoc;
19 import java.sql.SQLException JavaDoc;
20 import java.sql.Statement JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.tapestry.contrib.table.model.ITablePagingState;
25 import org.apache.tapestry.contrib.table.model.ITableSortingState;
26 import org.apache.tapestry.contrib.table.model.simple.SimpleTableState;
27
28 /**
29  *
30  * @author mindbridge
31  */

32 public class SimpleSqlTableDataSource implements ISqlTableDataSource
33 {
34     private static final Log LOG =
35         LogFactory.getLog(SimpleSqlTableDataSource.class);
36
37     private ISqlConnectionSource m_objConnSource;
38     private String JavaDoc m_strTableName;
39     private String JavaDoc m_strWhereClause;
40
41     public SimpleSqlTableDataSource(
42         ISqlConnectionSource objConnSource,
43         String JavaDoc strTableName)
44     {
45         this(objConnSource, strTableName, null);
46     }
47
48     public SimpleSqlTableDataSource(
49         ISqlConnectionSource objConnSource,
50         String JavaDoc strTableName,
51         String JavaDoc strWhereClause)
52     {
53         setConnSource(objConnSource);
54         setTableName(strTableName);
55         setWhereClause(strWhereClause);
56     }
57
58     /**
59      * @see org.apache.tapestry.contrib.table.model.sql.ISqlTableDataSource#getRowCount()
60      */

61     public int getRowCount() throws SQLException JavaDoc
62     {
63         String JavaDoc strQuery = generateCountQuery();
64         LOG.trace("Invoking query to count rows: " + strQuery);
65
66         Connection JavaDoc objConn = getConnSource().obtainConnection();
67         try
68         {
69             Statement JavaDoc objStmt = objConn.createStatement();
70             try
71             {
72                 ResultSet JavaDoc objRS = objStmt.executeQuery(strQuery);
73                 objRS.next();
74                 return objRS.getInt(1);
75             }
76             finally
77             {
78                 objStmt.close();
79             }
80         }
81         finally
82         {
83             getConnSource().returnConnection(objConn);
84         }
85     }
86
87     /**
88      * @see org.apache.tapestry.contrib.table.model.sql.ISqlTableDataSource#getCurrentRows(SqlTableColumnModel, SimpleTableState)
89      */

90     public ResultSet JavaDoc getCurrentRows(
91         SqlTableColumnModel objColumnModel,
92         SimpleTableState objState)
93         throws SQLException JavaDoc
94     {
95         String JavaDoc strQuery = generateDataQuery(objColumnModel, objState);
96         LOG.trace("Invoking query to load current rows: " + strQuery);
97
98         Connection JavaDoc objConn = getConnSource().obtainConnection();
99         Statement JavaDoc objStmt = objConn.createStatement();
100         return objStmt.executeQuery(strQuery);
101     }
102
103     /**
104      * @see org.apache.tapestry.contrib.table.model.sql.ISqlTableDataSource#closeResultSet(ResultSet)
105      */

106     public void closeResultSet(ResultSet JavaDoc objResultSet)
107     {
108         try
109         {
110             Statement JavaDoc objStmt = objResultSet.getStatement();
111             Connection JavaDoc objConn = objStmt.getConnection();
112             try
113             {
114                 objResultSet.close();
115                 objStmt.close();
116             }
117             catch (SQLException JavaDoc e)
118             {
119                 // ignore
120
}
121             getConnSource().returnConnection(objConn);
122         }
123         catch (SQLException JavaDoc e)
124         {
125             LOG.warn("Error while closing the result set", e);
126         }
127     }
128
129     protected String JavaDoc quoteObjectName(String JavaDoc strObject)
130     {
131         return strObject;
132     }
133
134     /**
135      * Returns the tableName.
136      * @return String
137      */

138     public String JavaDoc getTableName()
139     {
140         return m_strTableName;
141     }
142
143     /**
144      * Sets the tableName.
145      * @param tableName The tableName to set
146      */

147     public void setTableName(String JavaDoc tableName)
148     {
149         m_strTableName = tableName;
150     }
151
152     /**
153      * Returns the connSource.
154      * @return ISqlConnectionSource
155      */

156     public ISqlConnectionSource getConnSource()
157     {
158         return m_objConnSource;
159     }
160
161     /**
162      * Sets the connSource.
163      * @param connSource The connSource to set
164      */

165     public void setConnSource(ISqlConnectionSource connSource)
166     {
167         m_objConnSource = connSource;
168     }
169
170     /**
171      * Returns the whereClause.
172      * @return String
173      */

174     public String JavaDoc getWhereClause()
175     {
176         return m_strWhereClause;
177     }
178
179     /**
180      * Sets the whereClause.
181      * @param whereClause The whereClause to set
182      */

183     public void setWhereClause(String JavaDoc whereClause)
184     {
185         m_strWhereClause = whereClause;
186     }
187
188     protected String JavaDoc generateColumnList(SqlTableColumnModel objColumnModel)
189     {
190         // build the column selection
191
StringBuffer JavaDoc objColumnBuf = new StringBuffer JavaDoc();
192         for (int i = 0; i < objColumnModel.getColumnCount(); i++)
193         {
194             SqlTableColumn objColumn = objColumnModel.getSqlColumn(i);
195             if (i > 0)
196                 objColumnBuf.append(", ");
197             objColumnBuf.append(quoteObjectName(objColumn.getColumnName()));
198         }
199
200         return objColumnBuf.toString();
201     }
202
203     protected String JavaDoc generateWhereClause()
204     {
205         String JavaDoc strWhereClause = getWhereClause();
206         if (strWhereClause == null || strWhereClause.equals(""))
207             return "";
208         return "WHERE " + strWhereClause + " ";
209     }
210
211     protected String JavaDoc generateOrderByClause(ITableSortingState objSortingState)
212     {
213         // build the sorting clause
214
StringBuffer JavaDoc objSortingBuf = new StringBuffer JavaDoc();
215         if (objSortingState.getSortColumn() != null)
216         {
217             objSortingBuf.append("ORDER BY ");
218             objSortingBuf.append(objSortingState.getSortColumn());
219             if (objSortingState.getSortOrder()
220                 == ITableSortingState.SORT_ASCENDING)
221                 objSortingBuf.append(" ASC ");
222             else
223                 objSortingBuf.append(" DESC ");
224         }
225
226         return objSortingBuf.toString();
227     }
228
229     protected String JavaDoc generateLimitClause(ITablePagingState objPagingState)
230     {
231         int nPageSize = objPagingState.getPageSize();
232         int nStart = objPagingState.getCurrentPage() * nPageSize;
233         String JavaDoc strPagingBuf = "LIMIT " + nPageSize + " OFFSET " + nStart + " ";
234         return strPagingBuf;
235     }
236
237     protected String JavaDoc generateDataQuery(
238         SqlTableColumnModel objColumnModel,
239         SimpleTableState objState)
240     {
241         String JavaDoc strQuery =
242             "SELECT "
243                 + generateColumnList(objColumnModel)
244                 + " FROM "
245                 + getTableName()
246                 + " "
247                 + generateWhereClause()
248                 + generateOrderByClause(objState.getSortingState())
249                 + generateLimitClause(objState.getPagingState());
250
251         return strQuery;
252     }
253
254     protected String JavaDoc generateCountQuery()
255     {
256         String JavaDoc strQuery =
257             "SELECT COUNT(*) FROM "
258                 + getTableName()
259                 + " "
260                 + generateWhereClause();
261
262         return strQuery;
263     }
264
265 }
266
Popular Tags