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 OracleListSqlHelper extends ListSqlHelper {
025:
026: protected SqlStatement statement = new SqlStatement();
027:
028: protected String countSqlQuery = null;
029:
030: public OracleListSqlHelper(DataSource dataSource, ListQuery query) {
031: super (dataSource, query);
032: }
033:
034: public OracleListSqlHelper(DataSource dataSource) {
035: super (dataSource);
036: }
037:
038: public OracleListSqlHelper(ListQuery query) {
039: super (query);
040: }
041:
042: public OracleListSqlHelper() {
043: super ();
044: }
045:
046: protected SqlStatement getCountSqlStatement() {
047: if (this .countSqlQuery != null) {
048: return new SqlStatement(this .countSqlQuery, this .statement
049: .getParams());
050: }
051: String temp = new StringBuffer("SELECT COUNT(*) FROM (")
052: .append(this .statement.getQuery()).append(")")
053: .toString();
054: return new SqlStatement(temp, this .statement.getParams());
055: }
056:
057: protected SqlStatement getRangeSqlStatement() {
058: StringBuffer sb = new StringBuffer();
059: sb.append("SELECT * FROM ("
060: + "SELECT rownum listRowNum, listItemData.* FROM (");
061: sb.append(this .statement.getQuery());
062: sb.append(") listItemData" + ") WHERE listRowNum >= ?");
063: if (this .itemRangeCount != null) {
064: sb.append(" AND listRowNum <= ?");
065: }
066:
067: SqlStatement temp = new SqlStatement(sb.toString());
068: temp.addAllParams(this .statement.getParams());
069: temp.addParam(new Long(this .itemRangeStart.longValue() + 1));
070: if (this .itemRangeCount != null) {
071: temp.addParam(new Long(this .itemRangeStart.longValue()
072: + this .itemRangeCount.longValue()));
073: }
074: return temp;
075: }
076:
077: /**
078: * Sets the SQL query that will be used to retrieve the item range from the
079: * list and count the items. The SQL query must start with SELECT expression
080: * including the word "SELECT".
081: *
082: * @param sqlQuery
083: * the SQL query that will be used to retrieve the item range
084: * from the list and count the items.
085: */
086: public void setSqlQuery(String sqlQuery) {
087: this .statement.setQuery(sqlQuery);
088: }
089:
090: /**
091: * Sets the SQL query used to count the items in the database.
092: *
093: * @param countSqlQuery
094: * the SQL query used to count the items in the database.
095: */
096: public void setCountSqlQuery(String countSqlQuery) {
097: this .countSqlQuery = countSqlQuery;
098: }
099:
100: /**
101: * Adds a <code>NULL</code> <code>PreparedStatement</code> parameter for
102: * later setting.
103: *
104: * @param valueType
105: * the type of the NULL value.
106: */
107: public void addNullParam(int valueType) {
108: this .statement.addNullParam(valueType);
109: }
110:
111: /**
112: * Adds a <code>PreparedStatement</code> parameter for later setting.
113: *
114: * @param param
115: * a <code>PreparedStatement</code> parameter.
116: */
117: public void addStatementParam(Object param) {
118: this .statement.addParam(param);
119: }
120:
121: /**
122: * Adds <code>PreparedStatement</code> parameters for later setting.
123: *
124: * @param params
125: * <code>PreparedStatement</code> parameters.
126: */
127: public void addStatementParams(List params) {
128: this.statement.addAllParams(params);
129: }
130: }
|