`

chx 学习jForum笔记十八 jForum与ms sqlserver

阅读更多

jForum的原生数据库是mysql,但对sqlserver也应是支持的。

我当时安装的时候是从网上下载了一个sqlserver2000的补丁包,完成了安装。在此向提供此包的高手表示致敬。

在附件中即是我当时下载的那个补丁包。

下面要说的是其实sqlserver2000与其他的SQL不同之处在于没有LIMIT ?,?语句,而应该使用TOP ?语句。

因此,最终在我的sqlserver2000.sql文件中的内容为:

# ####################################
# @author ??? (original coding)
# @author Dirk Rasmussen - d.rasmussen@bevis.de (modifies for MS SQL Server 2005)
# @author Andowson Chang - http://www.andowson.com (fix for MS SQL Server 2000)
# @version $Id$
# ####################################

# #############
# UserModel
# #############						
UserModel.selectAllByLimit = SELECT TOP %d \
	user_email, user_id, user_posts, user_regdate, username, deleted, user_karma, user_from, user_website, user_viewemail \
	FROM jforum_users \
	ORDER BY user_id ASC

UserModel.selectAllByGroup = SELECT TOP %d user_email, u.user_id, user_posts, user_regdate, username, deleted, user_karma, user_from, user_website, user_viewemail \
    FROM jforum_users u, jforum_user_groups ug WHERE u.user_id = ug.user_id AND ug.group_id = ? \
    ORDER BY u.user_id

UserModel.lastUserRegistered = SELECT top 1 user_id, username FROM jforum_users ORDER BY user_regdate DESC

UserModel.selectById = SELECT u.*, \
    (SELECT COUNT(1) FROM jforum_privmsgs pm \
    WHERE pm.privmsgs_to_userid = u.user_id \
    AND pm.privmsgs_type = 1) AS private_messages \
    FROM jforum_users u \
    WHERE u.user_id = ?
	
# #############
# PostModel
# #############
PostModel.selectLatestByForumForRSS = SELECT TOP %d \
    p.topic_id, p.topic_id, p.post_id, p.forum_id, pt.post_subject AS subject, pt.post_text, p.post_time, p.user_id, u.username \
	FROM jforum_topics t, jforum_posts p, jforum_posts_text pt, jforum_users u \
	WHERE p.post_id = t.topic_last_post_id \
	AND p.topic_id = t.topic_id \
	AND p.user_id = u.user_id \
	AND p.post_id = pt.post_id \
	AND p.need_moderate = 0 \
	AND t.forum_id = ? \
	ORDER BY t.topic_last_post_id DESC

PostModel.selectLatestForRSS = SELECT TOP %d \
    t.topic_id, t.topic_title AS subject, p.post_id, t.forum_id, pt.post_text, p.post_time, p.user_id, u.username \
	FROM jforum_topics t, jforum_posts p, jforum_posts_text pt, jforum_users u \
	WHERE p.post_id = t.topic_last_post_id \
	AND p.topic_id = t.topic_id \
	AND p.user_id = u.user_id \
	AND p.post_id = pt.post_id \
	AND p.need_moderate = 0  \
	ORDER BY topic_last_post_id DESC
	
PostModel.selectHotForRSS = SELECT TOP %d \
    t.topic_id, t.topic_title AS subject, p.post_id, t.forum_id, pt.post_text, p.post_time, p.user_id, u.username \
	FROM jforum_topics t, jforum_posts p, jforum_posts_text pt, jforum_users u \
	WHERE p.post_id = t.topic_last_post_id \
	AND p.topic_id = t.topic_id \
	AND p.user_id = u.user_id \
	AND p.post_id = pt.post_id \
	AND p.need_moderate = 0  \
	ORDER BY topic_views DESC

PostModel.selectAllByTopicByLimit = SELECT TOP %d \
	p.post_id, topic_id, forum_id, p.user_id, post_time, poster_ip, enable_bbcode, p.attach, \
	enable_html, enable_smilies, enable_sig, post_edit_time, post_edit_count, status, pt.post_subject, pt.post_text, username, p.need_moderate \
	FROM jforum_posts p, jforum_posts_text pt, jforum_users u \
	WHERE p.post_id = pt.post_id \
	AND topic_id = ? \
	AND p.user_id = u.user_id \
	AND p.need_moderate = 0 
	ORDER BY post_time ASC

PostModel.selectByUserByLimit = SELECT TOP %d \
    p.post_id, topic_id, forum_id, p.user_id, post_time, poster_ip, enable_bbcode, p.attach, \
	enable_html, enable_smilies, enable_sig, post_edit_time, post_edit_count, status, pt.post_subject, pt.post_text, username, p.need_moderate \
	FROM jforum_posts p, jforum_posts_text pt, jforum_users u \
	WHERE p.post_id = pt.post_id \
	AND p.user_id = u.user_id \
	AND p.user_id = ? \
	AND p.need_moderate = 0 \
	AND forum_id IN(:fids:) \
	ORDER BY p.post_id DESC

# #############
# TopicModel
# #############
TopicModel.selectAllByForumByLimit =  SELECT TOP %d \
    t.*, p.user_id AS last_user_id, p.post_time, (SELECT SUM(p.attach) \
        FROM jforum_posts p \
        WHERE p.topic_id = t.topic_id \
        AND p.need_moderate = 0) AS attach \
	FROM jforum_topics t, jforum_posts p \
	WHERE (t.forum_id = ? OR t.topic_moved_id = ?) \
	AND p.post_id = t.topic_last_post_id \
	AND p.need_moderate = 0 \
	ORDER BY t.topic_type DESC, t.topic_last_post_id DESC

TopicModel.selectRecentTopicsByLimit = SELECT TOP %d \
	t.*, p.user_id AS last_user_id, p.post_time, (SELECT SUM(p.attach) \
        FROM jforum_posts p \
        WHERE p.topic_id = t.topic_id \
        AND p.need_moderate = 0) AS attach \
	FROM jforum_topics t, jforum_posts p \
	WHERE p.post_id = t.topic_last_post_id \
	AND p.need_moderate = 0  \
	ORDER BY t.topic_last_post_id DESC

TopicModel.selectHottestTopicsByLimit = SELECT TOP %d \
  t.*, p.user_id AS last_user_id, p.post_time, (SELECT SUM(p.attach) \
        FROM jforum_posts p \
        WHERE p.topic_id = t.topic_id \
        AND p.need_moderate = 0) AS attach \
  FROM jforum_topics t, jforum_posts p \
  WHERE p.post_id = t.topic_last_post_id \
  AND p.need_moderate = 0 \
  ORDER BY topic_views DESC

TopicModel.selectByUserByLimit = SELECT TOP %d \
    t.*, p.user_id AS last_user_id, p.post_time, (SELECT SUM(p.attach) \
        FROM jforum_posts p \
        WHERE p.topic_id = t.topic_id \
        AND p.need_moderate = 0) AS attach \
	FROM jforum_topics t, jforum_posts p \
	WHERE p.post_id = t.topic_last_post_id \
	AND t.user_id = ? \
	AND p.need_moderate = 0 \
	AND t.forum_id IN(:fids:) \
	ORDER BY t.topic_last_post_id DESC

# ################
# AttachmentModel
# ################
AttachmentModel.selectTopDownloadsByLimit = SELECT TOP %d f.forum_id, f.forum_name, t.topic_id, t.topic_title, ad.attach_id, ad.real_filename, ad.filesize, ad.download_count \
    FROM jforum_forums f, jforum_posts p, jforum_topics t, jforum_attach a, jforum_attach_desc ad \
    WHERE p.topic_id = t.topic_id AND p.forum_id = f.forum_id and p.post_id = a.post_id \
    AND a.attach_id = ad.attach_id AND a.privmsgs_id = 0 AND ad.download_count > 0 \
    ORDER BY ad.download_count DESC 


# ############
# SearchModel
# ############
SearchModel.firstPostIdByDate = SELECT TOP 1 post_id FROM jforum_posts WHERE post_time > ?
SearchModel.lastPostIdByDate = SELECT TOP 1 post_id FROM jforum_posts WHERE post_time < ? ORDER BY post_id DESC

# ################
# ModerationLog
# ################
ModerationLog.selectAll = SELECT TOP %d l.*, u.username, u2.username AS poster_username FROM jforum_moderation_log l LEFT JOIN jforum_users u2 ON u2.user_id = l.post_user_id LEFT JOIN jforum_users u ON l.user_id = u.user_id ORDER BY log_id DESC

 

我自己添加了最后的几句。

其中ModerationLog那句影响的是“管理日志 ”功能。光在这个里面添加SQL语句还不行,程序会自动调用generModerationLogDAO();中的相应语句导致SQL报错。为此需要在SqlServer2000DataAccessDriver.java中增加以下语句:

private static ModerationLogDAO moderationLogDao = new SqlServer2000ModerationLogDAO();
public net.jforum.dao.ModerationLogDAO newModerationLogDAO()
{
   return moderationLogDao;
}

 

而对于search那两句,则不光要进行上述类似的操作,还需要在/dao/sqlserver中新增一个名为SqlServer2000LuceneDAO.java的文件。文件内容为:

package net.jforum.dao.sqlserver;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.GenericLuceneDAO;
import net.jforum.exceptions.DatabaseException;
import net.jforum.util.DbUtils;
import net.jforum.util.preferences.SystemGlobals;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

/**
 * @author Rafael Steil
 * @version $Id: GenericLuceneDAO.java,v 1.12 2007/10/13 13:46:21 rafaelsteil Exp $
 */
public class SqlServer2000LuceneDAO extends GenericLuceneDAO {

    /**
     * @see net.jforum.dao.LuceneDAO#firstPostIdByDate(java.util.Date)
     */
    public int firstPostIdByDate(Date date) {
        return this.getPostIdByDate(date, SystemGlobals.getSql("SearchModel.firstPostIdByDate"));
    }

    /**
     * @see net.jforum.dao.LuceneDAO#lastPostIdByDate(java.util.Date)
     */
    public int lastPostIdByDate(Date date) {
        return this.getPostIdByDate(date, SystemGlobals.getSql("SearchModel.lastPostIdByDate"));
    }

    private int getPostIdByDate(Date date, String query) {
        int postId = 0;

        PreparedStatement p = null;
        ResultSet rs = null;

        try {
            p = JForumExecutionContext.getConnection().prepareStatement(query);

            p.setTimestamp(1, new Timestamp(date.getTime()));

            rs = p.executeQuery();

            if (rs.next()) {
                postId = rs.getInt(1);
            }
        }
        catch (SQLException e) {
            throw new DatabaseException(e);
        }
        finally {
            DbUtils.close(rs, p);
        }

        return postId;
    }

}
 

至于attachment那句,与search类似,需要新增SqlServer2000AttachmentDAO.java.内容为:

package net.jforum.dao.sqlserver;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.GenericAttachmentDAO;
import net.jforum.entities.TopDownloadInfo;
import net.jforum.exceptions.DatabaseException;
import net.jforum.util.DbUtils;
import net.jforum.util.preferences.SystemGlobals;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Rafael Steil
 * @version $Id: GenericAttachmentDAO.java,v 1.11 2006/08/23 02:13:41 rafaelsteil Exp $
 */
public class SqlServer2000AttachmentDAO extends GenericAttachmentDAO {

    public List<TopDownloadInfo> selectTopDownloads(final int limit) {
        final List<TopDownloadInfo> list = new ArrayList<TopDownloadInfo>();
        PreparedStatement pstmt = null;
        ResultSet resultSet = null;
        String sql = SystemGlobals.getSql("AttachmentModel.selectTopDownloadsByLimit");
        sql = String.format(sql, limit);
        try {
            pstmt = JForumExecutionContext.getConnection().prepareStatement(sql);

            resultSet = pstmt.executeQuery();
            while (resultSet.next()) {
                TopDownloadInfo tdi = new TopDownloadInfo();
                tdi.setForumId(resultSet.getInt("forum_id"));
                tdi.setForumName(resultSet.getString("forum_name"));
                tdi.setTopicId(resultSet.getInt("topic_id"));
                tdi.setTopicTitle(resultSet.getString("topic_title"));
                tdi.setAttachId(resultSet.getInt("attach_id"));
                tdi.setRealFilename(resultSet.getString("real_filename"));
                tdi.setFilesize(resultSet.getLong("filesize"));
                tdi.setDownloadCount(resultSet.getInt("download_count"));
                list.add(tdi);
            }
        } catch (SQLException e) {
            throw new DatabaseException(e);
        } finally {
            DbUtils.close(resultSet, pstmt);
        }
        return list;
    }
}

 这两个文件,都是从相应的generic中复制过来进行一定的修改即可。主要就是下面这句:

sql = String.format(sql, limit);

 

另外,非常重要的是要注意web服务器上jforum\WEB-INF\config\jforum-custom.conf这个文件。

这个文件是在INSTALL之后自动生成的。除了有数据库相应的地址、端口、用户名、密码等内容之外下面这三句也许都起着一定的作用。

 

dao.driver=net.jforum.dao.sqlserver.SqlServer2000DataAccessDriver
database.connection.driver=net.sourceforge.jtds.jdbc.Driver
database.driver.name=sqlserver2000

  特别是database.driver.name=sqlserver2000这句,会导致jforum从服务器的jofrum\WEB-INF\config\database\sqlserver2000\中去取相应的内容。我的服务器上这个目录包含以下文件:

sqlserver2000.properties

sqlserver2000.sql

sqlserver2000_data_dump.sql

sqlserver2000_db_struct.sql

如果没有,请从jofrum\WEB-INF\config\database\sqlserver\目录中复制过来,并注意修改相应的文件名。

 

分享到:
评论

相关推荐

    chx99个人主页源码_chx99.zip

    【标题】"chx99个人主页源码_chx99.zip" 提供的是一个名为 chx99 的个人主页的源代码。源码通常包含了网站设计、布局、功能实现等核心部分,对于开发者而言是了解网站工作原理、学习前端技术或者进行二次开发的重要...

    CHX TNF JNK之间的关系

    标题与描述中的关键词“CHX TNF JNK”揭示了在细胞凋亡机制中的相互作用,尤其是关于人类肝癌细胞对肿瘤坏死因子α(TNFα)诱导的凋亡反应。这一研究聚焦于理解CHX(环己亚胺)、TNFα以及JNK(c-Jun N端激酶)三者...

    chx2095中文 英文资料

    根据给定的文件信息,我们可以提炼出以下关于CHX2095的详细IT知识: ### CHX2095概述 **CHX2095**是一款由United Monolithic Semiconductors S.A.S.制造的频率倍增器集成电路(IC),其工作频率范围在7.5至30GHz...

    CHX-I防火墙官方教程-附带翻译终稿.pdf

    CHX-I防火墙官方教程-附带翻译终稿.pdf

    CHX

    此外,对于初学者来说,阅读和理解CHX的源代码也是一个很好的学习C语言和理解十六进制编辑器工作原理的机会。 总的来说,CHX 是一款实用的终端工具,它的轻量级设计和高度可定制性使其成为开发人员和系统管理员的...

    [CMS程序]chx99个人主页源码_chx99(ASP.NET源码).rar

    总之,通过对【CMS程序]chx99个人主页源码_chx99(ASP.NET源码)的深入研究,我们可以学习到ASP.NET Web Forms的开发技术,数据绑定,用户认证,配置管理,以及前端交互等多个方面,这将对提升我们的ASP.NET开发技能...

    HA-SoftEther-CHX虚拟专网

    HA-SoftEther-CHX虚拟专网HA-SoftEther-CHX虚拟专网HA-SoftEther-CHX虚拟专网HA-SoftEther-CHX虚拟专网

    chx99个人主页源码

    【标题】"chx99个人主页源码"是一个针对个人使用的网站源代码,它提供了构建个人在线展示空间的基础框架。个人主页源码通常包含了HTML、CSS、JavaScript等前端技术,有时也会涉及到PHP、Node.js等后端语言,用于实现...

    CHX612.zip

    这篇文章将详细讲解与"CHX612.zip"压缩包相关的知识点,主要涉及HX612触摸IC、STC15W408AS微控制器以及相关编程与接口技术。 首先,我们关注的是HX612,这是一款触摸传感器集成电路。不同于常见的I2C接口,HX612...

    ASP.NET-[CMS程序]chx99个人主页源码.zip

    1. **数据库设计**:源码可能包含与MySQL、SQL Server或SQLite等数据库相关的文件,如`.sql`脚本,用于创建站点所需的表结构和数据。 2. **后端逻辑**:这部分主要是处理用户请求、执行业务逻辑和数据库操作的代码...

    Chx.rar_最优化_最优化方法_梯度优化_梯度法

    与普通的梯度下降法相比,共轭梯度法在迭代过程中可以更快地收敛到最优解,因为它考虑了梯度的共轭性质,避免了过多的正交化步骤。 压缩包内的“Chx.txt”和“www.pudn.com.txt”可能是相关的文档或资料。"Chx.txt...

    ASP.NET源码——[CMS程序]chx99个人主页源码.zip

    【ASP.NET技术详解】 ASP.NET是由微软公司推出的用于构建Web应用程序的框架,它基于.NET Framework,为开发者提供...通过分析源码,我们可以学习到如何利用ASP.NET的各种特性来设计和实现一个功能完备的个人主页系统。

    CHX-3大手臂大法兰.SLDDRW

    CHX-3大手臂大法兰.SLDDRW

    [CMS程序]chx99个人主页源码_chx99.zip源码ASP.NET网站源码打包下载

    [CMS程序]chx99个人主页源码_chx99.zip源码ASP.NET网站源码打包下载[CMS程序]chx99个人主页源码_chx99.zip源码ASP.NET网站源码打包下载[CMS程序]chx99个人主页源码_chx99.zip源码ASP.NET网站源码打包下载[CMS程序]...

    \HA-SoftEther-CHX虚拟网卡及教程

    使得系统把此软件识别成一块网卡,有了这个东西,只要可以访问外网,都可以连接到虚拟HUB上与其他电脑组成局域网,在此虚拟的局域网上能进行所有物理存在的局域网上的操作,可以相互访问,可以联网玩游戏。...

    程序源码 chx99个人主页源码_chx99(ASP.NET源码).rar

    免责声明:资料部分来源于合法的互联网渠道收集和整理,部分自己学习积累成果,供大家学习参考与交流。收取的费用仅用于收集和整理资料耗费时间的酬劳。 本人尊重原创作者或出版方,资料版权归原作者或出版方所有,...

    CHX Hyper eXpressor-开源

    CHX Hyper eXpressor 是一个开源项目,旨在为C++开发者提供类似JSP(Java Server Pages)的功能,即在C++代码中嵌入HTML来创建动态网页内容。这个项目的核心是一个源代码转换器,它能够将CHX语法的源代码转化为可...

    CHX-I防火墙官方教程-附带翻译借鉴.pdf

    CHX-I防火墙官方教程-附带翻译借鉴.pdf

Global site tag (gtag.js) - Google Analytics