001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.backend.list.helper;
016:
017: import java.util.List;
018: import javax.sql.DataSource;
019: import org.araneaframework.backend.list.model.ListQuery;
020:
021: /**
022: * @author <a href="mailto:rein@araneaframework.org">Rein Raudjärv</a>
023: */
024: public class HSqlListSqlHelper extends ListSqlHelper {
025:
026: private static final String SELECT_PREFIX = "SELECT ";
027:
028: protected SqlStatement statement = new SqlStatement();
029:
030: protected String countSqlQuery = null;
031:
032: public HSqlListSqlHelper(DataSource dataSource, ListQuery query) {
033: super (dataSource, query);
034: }
035:
036: public HSqlListSqlHelper(DataSource dataSource) {
037: super (dataSource);
038: }
039:
040: public HSqlListSqlHelper(ListQuery query) {
041: super (query);
042: }
043:
044: public HSqlListSqlHelper() {
045: super ();
046: }
047:
048: protected SqlStatement getCountSqlStatement() {
049: if (this .countSqlQuery != null) {
050: return new SqlStatement(this .countSqlQuery, this .statement
051: .getParams());
052: }
053: String temp = new StringBuffer("SELECT COUNT(*) FROM (")
054: .append(this .statement.getQuery()).append(")")
055: .toString();
056: return new SqlStatement(temp, this .statement.getParams());
057: }
058:
059: protected SqlStatement getRangeSqlStatement() {
060: if (!this .statement.getQuery().toUpperCase().startsWith(
061: SELECT_PREFIX)) {
062: throw new RuntimeException(
063: "SQL query must start with SELECT");
064: }
065:
066: SqlStatement result;
067:
068: if (isShowAll()) {
069: result = (SqlStatement) this .statement.clone();
070: } else {
071: StringBuffer query = new StringBuffer();
072: query.append("SELECT LIMIT ? ? ");
073: query.append(this .statement.getQuery().substring(
074: SELECT_PREFIX.length()));
075:
076: result = new SqlStatement(query.toString());
077: result.addParam(itemRangeStart);
078: result.addParam(itemRangeCount);
079: result.addAllParams(this .statement.getParams());
080: }
081:
082: return result;
083: }
084:
085: protected boolean isShowAll() {
086: return (itemRangeStart == null || itemRangeStart.longValue() == 0)
087: && (itemRangeCount == null || itemRangeCount
088: .longValue() == Long.MAX_VALUE);
089: }
090:
091: /**
092: * Sets the SQL query that will be used to retrieve the item range from the
093: * list and count the items. The SQL query must start with SELECT expression
094: * withoud the word "SELECT".
095: *
096: * @param sqlQuery
097: * the SQL query that will be used to retrieve the item range
098: * from the list and count the items.
099: */
100: public void setSqlQuery(String sqlQuery) {
101: this .statement.setQuery(sqlQuery);
102: }
103:
104: /**
105: * Sets the SQL query used to count the items in the database.
106: *
107: * @param countSqlQuery
108: * the SQL query used to count the items in the database.
109: */
110: public void setCountSqlQuery(String countSqlQuery) {
111: this .countSqlQuery = countSqlQuery;
112: }
113:
114: /**
115: * Adds a <code>NULL</code> <code>PreparedStatement</code> parameter for
116: * later setting.
117: *
118: * @param valueType
119: * the type of the NULL value.
120: */
121: public void addNullParam(int valueType) {
122: this .statement.addNullParam(valueType);
123: }
124:
125: /**
126: * Adds a <code>PreparedStatement</code> parameter for later setting.
127: *
128: * @param param
129: * a <code>PreparedStatement</code> parameter.
130: */
131: public void addStatementParam(Object param) {
132: this .statement.addParam(param);
133: }
134:
135: /**
136: * Adds <code>PreparedStatement</code> parameters for later setting.
137: *
138: * @param params
139: * <code>PreparedStatement</code> parameters.
140: */
141: public void addStatementParams(List params) {
142: this.statement.addAllParams(params);
143: }
144: }
|