⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectdb.java

📁 云网论坛CWBBS 源码,内容丰富,学习,参考,教学的好资料,具体见内说明,
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.cloudwebsoft.framework.base;

import org.apache.log4j.Logger;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.Iterator;
import java.util.HashMap;
import com.cloudwebsoft.framework.db.Connection;
import com.cloudwebsoft.framework.db.JdbcTemplate;
import cn.js.fan.util.ErrMsgException;
import cn.js.fan.util.ResKeyException;
import cn.js.fan.db.PrimaryKey;
import cn.js.fan.db.ListResult;
import cn.js.fan.web.Global;
import cn.js.fan.resource.Constant;
import cn.js.fan.db.KeyUnit;
import cn.js.fan.db.SQLFilter;

public abstract class ObjectDb implements IObjectDb {
    public static final PrimaryKey[] EMPTY_BLOCK = new PrimaryKey[0];

    public String connname = "";
    public transient Logger logger = null;
    public String QUERY_LOAD;
    public String QUERY_DEL;
    public String QUERY_SAVE;
    public String QUERY_CREATE;
    public String QUERY_LIST;

    public boolean isInitFromConfigDB = true;

    protected String tableName = "";

    public PrimaryKey primaryKey;
    public ObjectCache objectCache;

    public ObjectDb() {
        init();
    }

    public void init() {
        logger = Logger.getLogger(this.getClass().getName());
        connname = Global.defaultDB;
        if (connname.equals(""))
            logger.info(Constant.DB_NAME_NOT_FOUND);

        initDB();

        initFromConfigDB();
    }

    /**
     * 当从缓存中取出后,用以初始化transient的变量
     * @return Logger
     */
    public void renew() {
        if (logger==null) {
            logger = Logger.getLogger(this.getClass().getName());
        }
        if (objectCache!=null) {
            objectCache.renew();
        }
    }

    public void initDB() {
        isInitFromConfigDB = false;
    }

    public void initFromConfigDB() {
        if (!isInitFromConfigDB)
            return;
        DBConfig dc = new DBConfig();
        DBTable dt = dc.getDBTable(this.getClass().getName());
        if (dt == null) {
            logger.info(this + " cann't find table defination in config file.");
            return;
        }
        this.tableName = dt.getName();
        this.primaryKey = (PrimaryKey)dt.getPrimaryKey().clone();

        this.QUERY_CREATE = dt.getQueryCreate();
        this.QUERY_DEL = dt.getQueryDel();
        this.QUERY_LIST = dt.getQueryList();
        this.QUERY_LOAD = dt.getQueryLoad();
        this.QUERY_SAVE = dt.getQuerySave();
        this.objectCache = dt.getObjectCache(this);

        this.objectCache.setObjCachable(dt.isObjCachable());
        this.objectCache.setListCachable(dt.isListCachable());
    }

    /**
     *
     * @param sql String
     * @param startIndex int
     * @return Object[] 存放的是对应于主键的值
     */
    // abstract public Object[] getObjectBlock(String sql, int startIndex);
    public Object[] getObjectBlock(String query, int startIndex) {
        return objectCache.getObjectBlock(query, startIndex);
    }

    public IObjectDb getObjectDb(Object primaryKeyValue) {
        // logger.info(this + " userName=" + primaryKeyValue);
        primaryKey.setValue(primaryKeyValue);
        return objectCache.getObjectDb(primaryKey);
    }

    public ObjectBlockIterator getObjects(String query,
                                         int startIndex,
                                         int endIndex) {
        // 可能取得的infoBlock中的元素的顺序号小于endIndex
        Object[] blockValues = getObjectBlock(query, startIndex);
        // for (int i=0; i<blockValues.length; i++)
        //     logger.info("getObjects i=" + i + " " + blockValues[i]);
        return new ObjectBlockIterator(this, blockValues, query,
                                    startIndex, endIndex);
    }

    public boolean isLoaded() {
        return loaded;
    }

    public void setLoaded(boolean loaded) {
        this.loaded = loaded;
    }

    public int getBlockSize() {
        return blockSize;
    }

    public PrimaryKey getPrimaryKey() {
        return primaryKey;
    }

    abstract public boolean create(JdbcTemplate jt) throws ErrMsgException, ResKeyException ;
    abstract public void load(JdbcTemplate jt) throws ErrMsgException, ResKeyException;
    abstract public boolean save(JdbcTemplate jt) throws ErrMsgException, ResKeyException;
    abstract public boolean del(JdbcTemplate jt) throws ErrMsgException, ResKeyException;

    /**
     * 从数据库中取得对象
     * @param objKey Object
     * @return Object
     */
    abstract public IObjectDb getObjectRaw(PrimaryKey pk);

    public int getObjectCount(String sql) {
        return objectCache.getObjectCount(sql);
    }

    public boolean loaded = false;

    public Vector list() {
        return list(QUERY_LIST);
    }

    /**
     * 全部的记录列表
     * @return Vector
     */
    public Vector list(String QUERY_LIST) {
        int total = 0;
        ResultSet rs = null;
        Vector result = new Vector();
        Connection conn = new Connection(connname);
        try {
            // 取得总记录条数
            total = getObjectCount(QUERY_LIST);
            conn.prepareStatement(QUERY_LIST);
            if (total != 0)
                // sets the limit of the maximum nuber of rows in a ResultSet object
                conn.setMaxRows(total); // 尽量减少内存的使用
            rs = conn.executePreQuery();
            if (rs == null) {
                return result;
            } else {
                // defines the number of rows that will be read from the database when the ResultSet needs more rows
                rs.setFetchSize(total); // rs一次从POOL中所获取的记录数
                if (rs.absolute(1) == false) {
                    return result;
                }
                do {
                    if (primaryKey.getType()==PrimaryKey.TYPE_INT)
                        result.addElement(getObjectDb(new Integer(rs.getInt(1))));
                    else if (primaryKey.getType()==PrimaryKey.TYPE_STRING)
                        result.addElement(getObjectDb(rs.getString(1)));
                    else if (primaryKey.getType()==PrimaryKey.TYPE_LONG)
                        result.addElement(getObjectDb(new Long(rs.getLong(1))));
                    else if (primaryKey.getType() == PrimaryKey.TYPE_COMPOUND) {
                        HashMap keys = ((PrimaryKey)primaryKey.clone()).getKeys();
                        Iterator ir = keys.keySet().iterator();
                        while (ir.hasNext()) {
                            String keyName = (String) ir.next();
                            KeyUnit ku = (KeyUnit) keys.get(keyName);
                            if (ku.getType() == primaryKey.TYPE_INT) {
                                ku.setValue(new Integer(rs.getInt(ku.getOrders() + 1)));
                            } else if (ku.getType() == primaryKey.TYPE_LONG ) {
                                ku.setValue(new Long(rs.getLong(ku.getOrders() + 1)));
                            } else {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -