|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
SimpleSqlTableDataSource.java | 0% | 0% | 0% | 0% |
|
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;
|
|
18 |
import java.sql.ResultSet;
|
|
19 |
import java.sql.SQLException;
|
|
20 |
import java.sql.Statement;
|
|
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 m_strTableName;
|
|
39 |
private String m_strWhereClause;
|
|
40 |
|
|
41 | 0 |
public SimpleSqlTableDataSource(
|
42 |
ISqlConnectionSource objConnSource, |
|
43 |
String strTableName) |
|
44 |
{ |
|
45 | 0 |
this(objConnSource, strTableName, null); |
46 |
} |
|
47 |
|
|
48 | 0 |
public SimpleSqlTableDataSource(
|
49 |
ISqlConnectionSource objConnSource, |
|
50 |
String strTableName, |
|
51 |
String strWhereClause) |
|
52 |
{ |
|
53 | 0 |
setConnSource(objConnSource); |
54 | 0 |
setTableName(strTableName); |
55 | 0 |
setWhereClause(strWhereClause); |
56 |
} |
|
57 |
|
|
58 |
/**
|
|
59 |
* @see org.apache.tapestry.contrib.table.model.sql.ISqlTableDataSource#getRowCount()
|
|
60 |
*/
|
|
61 | 0 |
public int getRowCount() throws SQLException |
62 |
{ |
|
63 | 0 |
String strQuery = generateCountQuery(); |
64 | 0 |
LOG.trace("Invoking query to count rows: " + strQuery);
|
65 |
|
|
66 | 0 |
Connection objConn = getConnSource().obtainConnection(); |
67 | 0 |
try
|
68 |
{ |
|
69 | 0 |
Statement objStmt = objConn.createStatement(); |
70 | 0 |
try
|
71 |
{ |
|
72 | 0 |
ResultSet objRS = objStmt.executeQuery(strQuery); |
73 | 0 |
objRS.next(); |
74 | 0 |
return objRS.getInt(1);
|
75 |
} |
|
76 |
finally
|
|
77 |
{ |
|
78 | 0 |
objStmt.close(); |
79 |
} |
|
80 |
} |
|
81 |
finally
|
|
82 |
{ |
|
83 | 0 |
getConnSource().returnConnection(objConn); |
84 |
} |
|
85 |
} |
|
86 |
|
|
87 |
/**
|
|
88 |
* @see org.apache.tapestry.contrib.table.model.sql.ISqlTableDataSource#getCurrentRows(SqlTableColumnModel, SimpleTableState)
|
|
89 |
*/
|
|
90 | 0 |
public ResultSet getCurrentRows(
|
91 |
SqlTableColumnModel objColumnModel, |
|
92 |
SimpleTableState objState) |
|
93 |
throws SQLException
|
|
94 |
{ |
|
95 | 0 |
String strQuery = generateDataQuery(objColumnModel, objState); |
96 | 0 |
LOG.trace("Invoking query to load current rows: " + strQuery);
|
97 |
|
|
98 | 0 |
Connection objConn = getConnSource().obtainConnection(); |
99 | 0 |
Statement objStmt = objConn.createStatement(); |
100 | 0 |
return objStmt.executeQuery(strQuery);
|
101 |
} |
|
102 |
|
|
103 |
/**
|
|
104 |
* @see org.apache.tapestry.contrib.table.model.sql.ISqlTableDataSource#closeResultSet(ResultSet)
|
|
105 |
*/
|
|
106 | 0 |
public void closeResultSet(ResultSet objResultSet) |
107 |
{ |
|
108 | 0 |
try
|
109 |
{ |
|
110 | 0 |
Statement objStmt = objResultSet.getStatement(); |
111 | 0 |
Connection objConn = objStmt.getConnection(); |
112 | 0 |
try
|
113 |
{ |
|
114 | 0 |
objResultSet.close(); |
115 | 0 |
objStmt.close(); |
116 |
} |
|
117 |
catch (SQLException e)
|
|
118 |
{ |
|
119 |
// ignore
|
|
120 |
} |
|
121 | 0 |
getConnSource().returnConnection(objConn); |
122 |
} |
|
123 |
catch (SQLException e)
|
|
124 |
{ |
|
125 | 0 |
LOG.warn("Error while closing the result set", e);
|
126 |
} |
|
127 |
} |
|
128 |
|
|
129 | 0 |
protected String quoteObjectName(String strObject)
|
130 |
{ |
|
131 | 0 |
return strObject;
|
132 |
} |
|
133 |
|
|
134 |
/**
|
|
135 |
* Returns the tableName.
|
|
136 |
* @return String
|
|
137 |
*/
|
|
138 | 0 |
public String getTableName()
|
139 |
{ |
|
140 | 0 |
return m_strTableName;
|
141 |
} |
|
142 |
|
|
143 |
/**
|
|
144 |
* Sets the tableName.
|
|
145 |
* @param tableName The tableName to set
|
|
146 |
*/
|
|
147 | 0 |
public void setTableName(String tableName) |
148 |
{ |
|
149 | 0 |
m_strTableName = tableName; |
150 |
} |
|
151 |
|
|
152 |
/**
|
|
153 |
* Returns the connSource.
|
|
154 |
* @return ISqlConnectionSource
|
|
155 |
*/
|
|
156 | 0 |
public ISqlConnectionSource getConnSource()
|
157 |
{ |
|
158 | 0 |
return m_objConnSource;
|
159 |
} |
|
160 |
|
|
161 |
/**
|
|
162 |
* Sets the connSource.
|
|
163 |
* @param connSource The connSource to set
|
|
164 |
*/
|
|
165 | 0 |
public void setConnSource(ISqlConnectionSource connSource) |
166 |
{ |
|
167 | 0 |
m_objConnSource = connSource; |
168 |
} |
|
169 |
|
|
170 |
/**
|
|
171 |
* Returns the whereClause.
|
|
172 |
* @return String
|
|
173 |
*/
|
|
174 | 0 |
public String getWhereClause()
|
175 |
{ |
|
176 | 0 |
return m_strWhereClause;
|
177 |
} |
|
178 |
|
|
179 |
/**
|
|
180 |
* Sets the whereClause.
|
|
181 |
* @param whereClause The whereClause to set
|
|
182 |
*/
|
|
183 | 0 |
public void setWhereClause(String whereClause) |
184 |
{ |
|
185 | 0 |
m_strWhereClause = whereClause; |
186 |
} |
|
187 |
|
|
188 | 0 |
protected String generateColumnList(SqlTableColumnModel objColumnModel)
|
189 |
{ |
|
190 |
// build the column selection
|
|
191 | 0 |
StringBuffer objColumnBuf = new StringBuffer();
|
192 | 0 |
for (int i = 0; i < objColumnModel.getColumnCount(); i++) |
193 |
{ |
|
194 | 0 |
SqlTableColumn objColumn = objColumnModel.getSqlColumn(i); |
195 | 0 |
if (i > 0)
|
196 | 0 |
objColumnBuf.append(", ");
|
197 | 0 |
objColumnBuf.append(quoteObjectName(objColumn.getColumnName())); |
198 |
} |
|
199 |
|
|
200 | 0 |
return objColumnBuf.toString();
|
201 |
} |
|
202 |
|
|
203 | 0 |
protected String generateWhereClause()
|
204 |
{ |
|
205 | 0 |
String strWhereClause = getWhereClause(); |
206 | 0 |
if (strWhereClause == null || strWhereClause.equals("")) |
207 | 0 |
return ""; |
208 | 0 |
return "WHERE " + strWhereClause + " "; |
209 |
} |
|
210 |
|
|
211 | 0 |
protected String generateOrderByClause(ITableSortingState objSortingState)
|
212 |
{ |
|
213 |
// build the sorting clause
|
|
214 | 0 |
StringBuffer objSortingBuf = new StringBuffer();
|
215 | 0 |
if (objSortingState.getSortColumn() != null) |
216 |
{ |
|
217 | 0 |
objSortingBuf.append("ORDER BY ");
|
218 | 0 |
objSortingBuf.append(objSortingState.getSortColumn()); |
219 | 0 |
if (objSortingState.getSortOrder()
|
220 |
== ITableSortingState.SORT_ASCENDING) |
|
221 | 0 |
objSortingBuf.append(" ASC ");
|
222 |
else
|
|
223 | 0 |
objSortingBuf.append(" DESC ");
|
224 |
} |
|
225 |
|
|
226 | 0 |
return objSortingBuf.toString();
|
227 |
} |
|
228 |
|
|
229 | 0 |
protected String generateLimitClause(ITablePagingState objPagingState)
|
230 |
{ |
|
231 | 0 |
int nPageSize = objPagingState.getPageSize();
|
232 | 0 |
int nStart = objPagingState.getCurrentPage() * nPageSize;
|
233 | 0 |
String strPagingBuf = "LIMIT " + nPageSize + " OFFSET " + nStart + " "; |
234 | 0 |
return strPagingBuf;
|
235 |
} |
|
236 |
|
|
237 | 0 |
protected String generateDataQuery(
|
238 |
SqlTableColumnModel objColumnModel, |
|
239 |
SimpleTableState objState) |
|
240 |
{ |
|
241 | 0 |
String strQuery = |
242 |
"SELECT "
|
|
243 |
+ generateColumnList(objColumnModel) |
|
244 |
+ " FROM "
|
|
245 |
+ getTableName() |
|
246 |
+ " "
|
|
247 |
+ generateWhereClause() |
|
248 |
+ generateOrderByClause(objState.getSortingState()) |
|
249 |
+ generateLimitClause(objState.getPagingState()); |
|
250 |
|
|
251 | 0 |
return strQuery;
|
252 |
} |
|
253 |
|
|
254 | 0 |
protected String generateCountQuery()
|
255 |
{ |
|
256 | 0 |
String strQuery = |
257 |
"SELECT COUNT(*) FROM "
|
|
258 |
+ getTableName() |
|
259 |
+ " "
|
|
260 |
+ generateWhereClause(); |
|
261 |
|
|
262 | 0 |
return strQuery;
|
263 |
} |
|
264 |
|
|
265 |
} |
|
266 |
|
|