001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on 24/05/2004 / 12:04:11
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.dao.sqlserver;
044:
045: import java.sql.PreparedStatement;
046: import java.sql.ResultSet;
047: import java.sql.SQLException;
048: import java.util.ArrayList;
049: import java.util.List;
050:
051: import net.jforum.JForumExecutionContext;
052: import net.jforum.entities.Post;
053: import net.jforum.exceptions.DatabaseException;
054: import net.jforum.util.DbUtils;
055: import net.jforum.util.preferences.SystemGlobals;
056: import net.jforum.repository.ForumRepository;
057:
058: import org.apache.log4j.Logger;
059:
060: /**
061: * @author Andre de Andrade da Silva - andre.de.andrade@gmail.com
062: * @author Dirk Rasmussen - d.rasmussen@bevis.de (2007/02/19, modifs for MS SqlServer 2005)
063: * @see WEB-INF\config\database\sqlserver\sqlserver.sql (2007/02/19, MS SqlServer 2005 specific version!)
064: * @version $Id: SqlServerPostDAO.java,v 1.12 2007/03/03 18:33:45 rafaelsteil Exp $
065: */
066: public class SqlServerPostDAO extends
067: net.jforum.dao.generic.GenericPostDAO {
068: private static final Logger logger = Logger
069: .getLogger(SqlServerPostDAO.class);
070:
071: /**
072: * @see net.jforum.dao.PostDAO#selectById(int)
073: */
074: public Post selectById(int postId) {
075: PreparedStatement p = null;
076: ResultSet rs = null;
077: String sqlStmnt = SystemGlobals.getSql("PostModel.selectById");
078: if (logger.isDebugEnabled()) {
079: logger.debug("selectById(" + postId + ")..., sqlStmnt="
080: + sqlStmnt);
081: }
082:
083: try {
084: p = JForumExecutionContext.getConnection()
085: .prepareStatement(sqlStmnt,
086: ResultSet.TYPE_SCROLL_INSENSITIVE,
087: ResultSet.CONCUR_UPDATABLE);
088: p.setInt(1, postId);
089:
090: rs = p.executeQuery();
091:
092: Post post = new Post();
093:
094: if (rs.next()) {
095: post = this .makePost(rs);
096: }
097:
098: return post;
099: } catch (SQLException e) {
100: logger.error(sqlStmnt, e);
101: throw new DatabaseException(e);
102: } finally {
103: DbUtils.close(rs, p);
104: }
105: }
106:
107: /**
108: * @see net.jforum.dao.PostDAO#selectAllByTopicByLimit(int, int, int)
109: */
110: public List selectAllByTopicByLimit(int topicId, int startFrom,
111: int count) {
112: List l = new ArrayList();
113:
114: PreparedStatement p = null;
115: ResultSet rs = null;
116: String sqlStmnt = SystemGlobals
117: .getSql("PostModel.selectAllByTopicByLimit");
118: if (logger.isDebugEnabled()) {
119: logger.debug("selectAllByTopicByLimit(" + topicId + ","
120: + startFrom + "," + count + ")..., sqlStmnt="
121: + sqlStmnt);
122: }
123:
124: try {
125: p = JForumExecutionContext.getConnection()
126: .prepareStatement(sqlStmnt,
127: ResultSet.TYPE_SCROLL_INSENSITIVE,
128: ResultSet.CONCUR_UPDATABLE);
129:
130: p.setInt(1, topicId);
131: p.setInt(2, startFrom);
132: p.setInt(3, startFrom + count);
133:
134: rs = p.executeQuery();
135:
136: while (rs.next()) {
137: l.add(this .makePost(rs));
138: }
139:
140: return l;
141: } catch (SQLException e) {
142: logger.error(sqlStmnt, e);
143: throw new DatabaseException(e);
144: } finally {
145: DbUtils.close(rs, p);
146: }
147: }
148:
149: /**
150: * @see net.jforum.dao.PostDAO#selectByUserByLimit(int, int, int)
151: */
152: public List selectByUserByLimit(int userId, int startFrom, int count) {
153: PreparedStatement p = null;
154: ResultSet rs = null;
155: String sqlStmnt = SystemGlobals
156: .getSql("PostModel.selectByUserByLimit");
157: sqlStmnt = sqlStmnt.replaceAll(":fids:", ForumRepository
158: .getListAllowedForums());
159: if (logger.isDebugEnabled()) {
160: logger.debug("selectByUserByLimit(" + userId + ","
161: + startFrom + "," + count + ")..., sqlStmnt="
162: + sqlStmnt);
163: }
164:
165: try {
166: p = JForumExecutionContext.getConnection()
167: .prepareStatement(sqlStmnt);
168:
169: p.setInt(1, userId);
170: p.setInt(2, startFrom);
171: p.setInt(3, count);
172:
173: rs = p.executeQuery();
174: List l = new ArrayList();
175:
176: while (rs.next()) {
177: l.add(this .makePost(rs));
178: }
179:
180: return l;
181: } catch (SQLException e) {
182: throw new DatabaseException(e);
183: } finally {
184: DbUtils.close(rs, p);
185: }
186: }
187:
188: }
|