001: /**
002: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the latest version of the GNU Lesser General
006: * Public License as published by the Free Software Foundation;
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program (LICENSE.txt); if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: */package org.jamwiki.db;
017:
018: import java.text.MessageFormat;
019: import java.util.Properties;
020: import org.jamwiki.Environment;
021: import org.jamwiki.utils.Pagination;
022: import org.jamwiki.utils.Utilities;
023: import org.jamwiki.utils.WikiLogger;
024:
025: /**
026: * DB2/400-specific implementation of the QueryHandler interface. This class implements
027: * DB2/400-specific methods for instances where DB2/400 does not support the default
028: * ASCII SQL syntax.
029: */
030: public class DB2400QueryHandler extends AnsiQueryHandler {
031:
032: private static final WikiLogger logger = WikiLogger
033: .getLogger(DB2400QueryHandler.class.getName());
034: private static final String SQL_PROPERTY_FILE_NAME = "sql.db2400.properties";
035: private static Properties props = null;
036: private static Properties defaults = null;
037:
038: /**
039: *
040: */
041: protected DB2400QueryHandler() {
042: defaults = Environment
043: .loadProperties(AnsiQueryHandler.SQL_PROPERTY_FILE_NAME);
044: props = Environment.loadProperties(SQL_PROPERTY_FILE_NAME,
045: defaults);
046: super .init(props);
047: }
048:
049: /**
050: * DB2/400 will not allow query parameters such as "fetch ? rows only", so
051: * this method provides a way of formatting the query limits without using
052: * query parameters.
053: *
054: * @param sql The SQL statement, with the last result parameter specified as
055: * {0} and the total number of rows parameter specified as {1}.
056: * @param pagination A Pagination object that specifies the number of results
057: * and starting result offset for the result set to be retrieved.
058: * @return A formatted SQL string.
059: */
060: private static String formatStatement(String sql,
061: Pagination pagination) {
062: try {
063: Object[] objects = { new Integer(pagination.getEnd()),
064: new Integer(pagination.getNumResults()) };
065: return MessageFormat.format(sql, objects);
066: } catch (Exception e) {
067: logger.warning("Unable to format " + sql + " with values "
068: + pagination.getEnd() + " / "
069: + pagination.getNumResults(), e);
070: return null;
071: }
072: }
073:
074: /**
075: *
076: */
077: public WikiResultSet getCategories(int virtualWikiId,
078: Pagination pagination) throws Exception {
079: String sql = formatStatement(STATEMENT_SELECT_CATEGORIES,
080: pagination);
081: WikiPreparedStatement stmt = new WikiPreparedStatement(sql);
082: stmt.setInt(1, virtualWikiId);
083: return stmt.executeQuery();
084: }
085:
086: /**
087: *
088: */
089: public WikiResultSet getRecentChanges(String virtualWiki,
090: Pagination pagination, boolean descending) throws Exception {
091: String sql = formatStatement(STATEMENT_SELECT_RECENT_CHANGES,
092: pagination);
093: WikiPreparedStatement stmt = new WikiPreparedStatement(sql);
094: stmt.setString(1, virtualWiki);
095: // FIXME - sort order ignored
096: return stmt.executeQuery();
097: }
098:
099: /**
100: *
101: */
102: public WikiResultSet getRecentChanges(int topicId,
103: Pagination pagination, boolean descending) throws Exception {
104: String sql = formatStatement(
105: STATEMENT_SELECT_RECENT_CHANGES_TOPIC, pagination);
106: WikiPreparedStatement stmt = new WikiPreparedStatement(sql);
107: stmt.setInt(1, topicId);
108: // FIXME - sort order ignored
109: return stmt.executeQuery();
110: }
111:
112: /**
113: *
114: */
115: public WikiResultSet getTopicsAdmin(int virtualWikiId,
116: Pagination pagination) throws Exception {
117: String sql = formatStatement(STATEMENT_SELECT_TOPICS_ADMIN,
118: pagination);
119: WikiPreparedStatement stmt = new WikiPreparedStatement(sql);
120: stmt.setInt(1, virtualWikiId);
121: return stmt.executeQuery();
122: }
123:
124: /**
125: *
126: */
127: public WikiResultSet getUserContributions(String virtualWiki,
128: String userString, Pagination pagination, boolean descending)
129: throws Exception {
130: String sql = null;
131: if (Utilities.isIpAddress(userString)) {
132: sql = formatStatement(
133: STATEMENT_SELECT_WIKI_USER_CHANGES_ANONYMOUS,
134: pagination);
135: } else {
136: sql = formatStatement(
137: STATEMENT_SELECT_WIKI_USER_CHANGES_LOGIN,
138: pagination);
139: }
140: WikiPreparedStatement stmt = new WikiPreparedStatement(sql);
141: stmt.setString(1, virtualWiki);
142: stmt.setString(2, userString);
143: // FIXME - sort order ignored
144: return stmt.executeQuery();
145: }
146:
147: /**
148: *
149: */
150: public WikiResultSet getWatchlist(int virtualWikiId, int userId,
151: Pagination pagination) throws Exception {
152: String sql = formatStatement(
153: STATEMENT_SELECT_WATCHLIST_CHANGES, pagination);
154: WikiPreparedStatement stmt = new WikiPreparedStatement(sql);
155: stmt.setInt(1, virtualWikiId);
156: stmt.setInt(2, userId);
157: return stmt.executeQuery();
158: }
159:
160: /**
161: *
162: */
163: public WikiResultSet lookupTopicByType(int virtualWikiId,
164: int topicType, Pagination pagination) throws Exception {
165: String sql = formatStatement(STATEMENT_SELECT_TOPIC_BY_TYPE,
166: pagination);
167: WikiPreparedStatement stmt = new WikiPreparedStatement(sql);
168: stmt.setInt(1, virtualWikiId);
169: stmt.setInt(2, topicType);
170: return stmt.executeQuery();
171: }
172:
173: /**
174: *
175: */
176: public WikiResultSet lookupWikiUsers(Pagination pagination)
177: throws Exception {
178: String sql = formatStatement(STATEMENT_SELECT_WIKI_USERS,
179: pagination);
180: WikiPreparedStatement stmt = new WikiPreparedStatement(sql);
181: return stmt.executeQuery();
182: }
183: }
|