objectdb.java

来自「cwbbs 云网论坛源码」· Java 代码 · 共 467 行 · 第 1/2 页

JAVA
467
字号
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();    }        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());    }        public int getObjectCountRaw(String query) {                        int docCount = 0;        Connection conn = new Connection(connname);        ResultSet rs = null;        try {            rs = conn.executeQuery(query);            if (rs.next()) {                docCount = rs.getInt(1);            }        } catch (SQLException e) {            e.printStackTrace();        } finally {            if (rs != null) {                try {                    rs.close();                } catch (Exception e) {}                rs = null;            }            if (conn != null) {                conn.close();                conn = null;            }        }        return docCount;    }            public Object[] getObjectBlock(String query, String groupKey,                                   int startIndex) {        return objectCache.getObjectBlock(query, groupKey, startIndex);    }    public IObjectDb getObjectDb(Object primaryKeyValue) {        PrimaryKey pk = (PrimaryKey)primaryKey.clone();        pk.setValue(primaryKeyValue);        return objectCache.getObjectDb(pk);    }        public ObjectBlockIterator getObjects(String query, String groupKey,                                          int startIndex,                                          int endIndex) {                Object[] blockValues = getObjectBlock(query, groupKey, startIndex);                                return new ObjectBlockIterator(this, blockValues, query, groupKey,                                       startIndex, endIndex);    }    public ObjectBlockIterator getObjects(String query,                                          int startIndex,                                          int endIndex) {        return getObjects(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;        abstract public IObjectDb getObjectRaw(PrimaryKey pk);    public int getObjectCount(String sql) {        return objectCache.getObjectCount(sql, "");    }    public int getObjectCount(String sql, String groupName) {        return objectCache.getObjectCount(sql, groupName);    }    public boolean loaded = false;    public Vector list() {        return list(QUERY_LIST);    }        public Vector list(String QUERY_LIST) {        ResultSet rs = null;        int total = 0;        Vector result = new Vector();        Connection conn = new Connection(connname);        try {                        String countsql = SQLFilter.getCountSql(QUERY_LIST);            rs = conn.executeQuery(countsql);            if (rs != null && rs.next()) {                total = rs.getInt(1);            }            if (rs != null) {                rs.close();                rs = null;            }            conn.prepareStatement(QUERY_LIST);            if (total != 0) {                                conn.setMaxRows(total);             }            rs = conn.executePreQuery();            if (rs == null) {                return result;            } else {                                rs.setFetchSize(total);                 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) {

⌨️ 快捷键说明

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