qobjectdb.java
来自「cwbbs 云网论坛源码」· Java 代码 · 共 1,053 行 · 第 1/3 页
JAVA
1,053 行
package com.cloudwebsoft.framework.base;import java.sql.ResultSet;import java.sql.SQLException;import java.util.*;import cn.js.fan.db.*;import cn.js.fan.util.*;import cn.js.fan.web.SkinUtil;import com.cloudwebsoft.framework.db.Connection;import com.cloudwebsoft.framework.db.JdbcTemplate;import com.cloudwebsoft.framework.util.LogUtil;import java.sql.PreparedStatement;import java.sql.ResultSetMetaData;import cn.js.fan.web.Global;public abstract class QObjectDb implements java.io.Serializable { public QDBTable table; public String cacheGroup; public String cacheGroupCount; public boolean isInitFromConfig = true; public PrimaryKey primaryKey; public boolean loaded = false; public ResultRecord resultRecord; public QObjectDb() { init(); } public void init() { initDB(); if (isInitFromConfig) initFromConfig(); cacheGroup = getClass().getName(); cacheGroupCount = cacheGroup + "_count"; } public void initDB() { } public void initFromConfig() { QDBConfig dc = new QDBConfig(); table = dc.getQDBTable(getClass().getName()); if (table == null) { throw new IllegalArgumentException( "initFromConfig2: cann't find table '" + getClass().getName() + "' defination in config file."); } this.primaryKey = (PrimaryKey) table.getPrimaryKey().clone(); this.objCachable = table.isObjCachable(); this.listCachable = table.isListCachable(); } public QDBTable getTable() { return table; } public String getCacheKey(PrimaryKey pk) { return table.getName() + "_" + pk.toString(); } public QObjectDb getQObjectDb(Object primaryKeyValue) { PrimaryKey pk = new PrimaryKey(); pk.setValue(primaryKeyValue); QObjectDb obj = null; if (objCachable) { try { obj = (QObjectDb) QCache.getInstance().getFromGroup(getCacheKey( pk), cacheGroup); } catch (Exception e) { LogUtil.getLog(this.getClass()).error("getQObject:" + e.getMessage()); } if (obj == null) { obj = getQObjectDbRaw(primaryKeyValue); if (obj != null && obj.isLoaded()) { try { QCache.getInstance().putInGroup(getCacheKey(pk), cacheGroup, obj); } catch (Exception e) { LogUtil.getLog(this.getClass()).error( "getQObject2: key=" + getCacheKey(primaryKey) + " group=" + cacheGroup + " " + e.getMessage()); LogUtil.getLog(this.getClass()).error(StrUtil.trace(e)); } } } } else { obj = getQObjectDbRaw(primaryKeyValue); } return obj; } public QObjectDb getQObjectDbRaw(Object primaryKeyValue) { QObjectDb obj = null; try { obj = (QObjectDb) getClass().newInstance(); } catch (Exception e) { LogUtil.getLog(getClass()).error("getQObjectDbRaw:" + e.getMessage()); } obj.primaryKey.setValue(primaryKeyValue); try { obj.load(); } catch (SQLException e) { LogUtil.getLog(getClass()).error("getQObjectDbRaw2:" + StrUtil.trace(e)); LogUtil.getLog(getClass()).error("getQObjectDbRaw2:primaryKey=" + primaryKey); } if (!obj.isLoaded()) obj = null; return obj; } public boolean initQObject(Object[] loadParams) throws SQLException { return initQObject(new JdbcTemplate(), table.getQueryLoad(), loadParams); } public boolean initQObject(JdbcTemplate jt, String sql, Object[] objectParams) throws SQLException { int colCount = 0; HashMap mapIndex = new HashMap(); ResultSet rs = null; Vector result = null; Connection connection = jt.getConnection(); PreparedStatement ps = null; try { ps = connection.prepareStatement(sql); jt.fillPreparedStatement(ps, objectParams); rs = connection.executePreQuery(); if (rs == null) { return false; } else { ResultSetMetaData rm = rs.getMetaData(); colCount = rm.getColumnCount(); result = new Vector(); for (int i = 1; i <= colCount; i++) { mapIndex.put(rm.getColumnName(i).toUpperCase(), new Integer(i)); result.addElement(new Object()); } resultRecord = new ResultRecord(result, mapIndex); } } catch (SQLException e) { throw e; } finally { if (rs != null) { rs.close(); rs = null; } if (ps!=null) { ps.close(); ps = null; } if (connection!=null) { connection.close(); connection = null; } } return true; } public boolean load() throws SQLException { JdbcTemplate jt = new JdbcTemplate(new Connection(table.getConnName())); ResultIterator ri = null; Object[] params = primaryKey.toObjectArray(); ri = jt.executeQuery(table.getQueryLoad(), params); if (ri.hasNext()) { resultRecord = (ResultRecord) ri.next(); loaded = true; return true; } return false; } public boolean create() throws ResKeyException, ErrMsgException { String[] fields = getFieldsFromQueryCreate(); int len = fields.length; Object[] params = new Object[len]; for (int i = 0; i < len; i++) { params[i] = resultRecord.get(fields[i]); } return create(new JdbcTemplate(new Connection(table.getConnName())), params); } public boolean create(ParamChecker paramChecker) throws ResKeyException, ErrMsgException { return create(new JdbcTemplate(new Connection(table.getConnName())), paramChecker); } public boolean create(JdbcTemplate jt, ParamChecker paramChecker) throws ResKeyException, ErrMsgException { String[] fields = getFieldsFromQueryCreate(); int len = fields.length; Object[] params = new Object[len]; for (int i = 0; i < len; i++) { params[i] = paramChecker.getValue(fields[i]); } return create(jt, params); } public boolean create(JdbcTemplate jt, Object[] params) throws ResKeyException { boolean re = false; try { String[] fields = getFieldsFromQueryCreate(); int len = fields.length; if (params.length != len) { throw new IllegalArgumentException("Params array's lenth is " + params.length + ", but create sql's fields number is " + len); } HashMap mapIndex = new HashMap(); Vector row = new Vector(); for (int i = 0; i < len; i++) { mapIndex.put(fields[i].toUpperCase(), new Integer(i + 1)); row.addElement(params[i]); } resultRecord = new ResultRecord(row, mapIndex); re = jt.executeUpdate(table.getQueryCreate(), params) == 1; } catch (SQLException e) { e.printStackTrace(); throw new ResKeyException(SkinUtil.ERR_DB); } if (re) { refreshCreate(); } return re; } public long getQObjectCount(String sql) { return getQObjectCount(sql, ""); } public long getQObjectCount(String sql, String groupName) { String query = cn.js.fan.db.SQLFilter.getCountSql(sql); Long count = null; if (listCachable) { try { count = (Long) QCache.getInstance().getFromGroup(query, cacheGroupCount+groupName); } catch (Exception e) { LogUtil.getLog(this.getClass()).error(e.getMessage()); } if (count != null) { return count.longValue(); } } long docCount = 0; Connection conn = new Connection(table.getConnName()); ResultSet rs = null; try { rs = conn.executeQuery(query); if (rs.next()) docCount = rs.getLong(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; } } if (listCachable) { try { QCache.getInstance().putInGroup(query, cacheGroupCount, new Long(docCount)); } catch (Exception e) { LogUtil.getLog(this.getClass()).error(e.getMessage()); } } return docCount; } public void refresh(PrimaryKey pk) { if (!objCachable) return; try { if (!cacheGroup.equals("")) { QCache.getInstance().remove(getCacheKey(pk), cacheGroup); } else { QCache.getInstance().remove(getCacheKey(pk)); } } catch (Exception e) { LogUtil.getLog(this.getClass()).error("refresh:" + e.getMessage()); } } public void refreshList() { refreshList(""); } public void refreshList(String groupName) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?