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

📄 qobjectdb.java

📁 云网论坛CWBBS 源码,内容丰富,学习,参考,教学的好资料,具体见内说明,
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                        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 {
                                ku.setValue(rs.getString(ku.getOrders() + 1));
                            }
                        }
                        result.addElement(getQObjectDb(keys));
                    }
                } while (rs.next());
            }
        } catch (SQLException e) {
            LogUtil.getLog(this.getClass()).error("list: " + e.getMessage());
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {}
                rs = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }
        return result;
    }

    /**
     * 2007.1.7 添加,用于OA中sms_receive_record的处理
     * @param jt JdbcTemplate
     * @param sql String
     * @param params Object[]
     * @return Vector
     */
    public Vector list(JdbcTemplate jt, String sql, Object[] params) {
        int total = 0;
        Vector result = new Vector();
        String connName = jt.getConnection().connName; // Connection(Global.defaultDB);

        try {
            // 取得总记录条数
            String countsql = SQLFilter.getCountSql(sql);
            ResultIterator ri = jt.executeQuery(countsql, params);
            if (ri.hasNext()) {
                ResultRecord rr = (ResultRecord) ri.next();
                total = rr.getInt(1);
            }

            jt = new JdbcTemplate(new Connection(connName));

            if (total != 0)
                // sets the limit of the maximum nuber of rows in a ResultSet object
                jt.getConnection().setMaxRows(total); // 尽量减少内存的使用
            ri = jt.executeQuery(sql, params);
            while (ri.hasNext()) {
                ResultRecord rr = (ResultRecord) ri.next();
                if (primaryKey.getType() == PrimaryKey.TYPE_INT)
                    result.addElement(getQObjectDb(new Integer(rr.getInt(1))));
                else if (primaryKey.getType() == PrimaryKey.TYPE_STRING)
                    result.addElement(getQObjectDb(rr.getString(1)));
                else if (primaryKey.getType() == PrimaryKey.TYPE_LONG)
                    result.addElement(getQObjectDb(new Long(rr.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(rr.getInt(ku.getOrders() + 1)));
                        } else if (ku.getType() == primaryKey.TYPE_LONG) {
                            ku.setValue(new Long(rr.getLong(ku.getOrders() + 1)));
                        } else {
                            ku.setValue(rr.getString(ku.getOrders() + 1));
                        }
                        // System.out.println(getClass() + " ku type=" + ku.getType() + " orders=" + ku.getOrders() + " value=" + ku.getValue());
                    }
                    result.addElement(getQObjectDb(keys));
                }
            }
        } catch (SQLException e) {
            LogUtil.getLog(this.getClass()).error("list: " + e.getMessage());
        }
        return result;
    }

    /**
     * 取得startIndex至endIndex-1个对象列表
     * @param query String
     * @param startIndex int
     * @param endIndex int
     * @return QObjectBlockIterator
     */
    public QObjectBlockIterator getQObjects(String query,
                                            int startIndex,
                                            int endIndex) {
        // 可能取得的infoBlock中的元素的顺序号小于endIndex
        Object[] blockValues = getQObjectBlock(query, startIndex);
        // LogUtil.getLog(getClass()).info("getObjects blockValues.lendth=" + blockValues.length);

        // for (int i=0; i<blockValues.length; i++)
        //    LogUtil.getLog(getClass()).info("getObjects i=" + i + " " + blockValues[i]);
        return new QObjectBlockIterator(this, blockValues, query,
                                        startIndex, endIndex);
    }

    public Object[] getQObjectBlock(String sql, int startIndex) {
        // First, discover what block number the results will be in.
        int blockSize = table.getBlockSize();
        int blockID = startIndex / blockSize;
        int blockStart = blockID * blockSize;

        // 取得根据主键的查询语句
        // String pk = primaryKey.getName();
        // String query = "select " + pk + " " + SQLFilter.getFromSql(sql); // 当为联合查询时,此句中的pk会带来问题,因为缺少表的别名作为前缀 2006.6.9
        // String query = "select " + objectDb.getTableName() + "." + pk + " " + SQLFilter.getFromSql(sql); // 加表名作为前缀在oracle中也不行
        String query = sql;

        // 缓存所用的key
        String key = query + blockID;

        Object[] objArray = null;
        if (listCachable) {
            try {
                objArray = (Object[]) QCache.getInstance().getFromGroup(key,
                        cacheGroup);
            } catch (Exception e) {
                LogUtil.getLog(this.getClass()).error("getQObjectBlock:" +
                        e.getMessage());
            }
        }
        //If already in cache, return the block.
        if (objArray != null) {
            /**
             * The actual block may be smaller than THREAD_BLOCK_SIZE. If that's
             * the case, it means two things:
             *  1) We're at the end boundary of all the results.
             *  2) If the start index is greater than the length of the current
             *     block, than there aren't really any results to return.
             */
            Object[] objkeys = objArray;
            //当startIndex过大时
            if (startIndex >= blockStart + objkeys.length) {
                // Return an empty array
                return ObjectDb.EMPTY_BLOCK;
            } else {
                return objkeys;
            }
        }
        // Otherwise, we have to load up the block from the database.
        else {
            Vector block = new Vector();
            ResultSet rs = null;
            Connection conn = new Connection(table.getConnName());
            try {
                // Set the maxium number of rows to end at the end of this block.
                conn.setMaxRows(blockSize * (blockID + 1));
                rs = conn.executeQuery(query);
                // LogUtil.getLog(getClass()).info("getQObjectBlock2 query=" + query);
                // Grab THREAD_BLOCK_ROWS rows at a time.
                conn.setFetchSize(blockSize);
                // Many JDBC drivers don't implement scrollable cursors the real
                // way, but instead load all results into memory. Looping through
                // the results ourselves is more efficient.
                for (int i = 0; i < blockStart; i++) {
                    rs.next();
                }
                // LogUtil.getLog(getClass()).info("getQObjectBlock2 blockStart=" + blockStart);

                // Keep reading results until the result set is exaughsted or
                // we come to the end of the block.
                int count = 0;
                while (rs.next() && count < blockSize) {
                    // LogUtil.getLog(getClass()).info("getQObjectBlock2 blockStart=" + blockStart);

                    // 如果不是复合主键
                    if (primaryKey.getKeyCount() == 1) {
                        if (primaryKey.getType() == primaryKey.TYPE_INT)
                            block.addElement(new Integer(rs.getInt(1)));
                        if (primaryKey.getType() == primaryKey.TYPE_STRING)
                            block.addElement(rs.getString(1));
                        if (primaryKey.getType() == primaryKey.TYPE_LONG)
                            block.addElement(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 {
                                ku.setValue(rs.getString(ku.getOrders() + 1));
                            }
                        }

                        block.addElement(keys);
                    }
                    count++;
                }
            } catch (SQLException sqle) {
                LogUtil.getLog(this.getClass()).error("getQObjectBlock2:" +
                        sqle.getMessage());
            } finally {
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (Exception e) {}
                    rs = null;
                }
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            }
            int len = block.size();

            // LogUtil.getLog(getClass()).info("getQObjectBlock block.size=" + len);

            Object[] objkeys = new Object[len];
            for (int i = 0; i < len; i++) {
                objkeys[i] = block.elementAt(i);
            }

            if (listCachable) {
                // Add the thread block to cache
                try {
                    QCache.getInstance().putInGroup(key, cacheGroup, objkeys);
                } catch (Exception e) {
                    LogUtil.getLog(this.getClass()).error("getQObjectBlock3:" +
                            e.getMessage());
                }
            }

            /**
             * The actual block may be smaller than THREAD_BLOCK_SIZE. If that's
             * the case, it means two things:
             *  1) We're at the end boundary of all the results.
             *  2) If the start index is greater than the length of the current
             *     block, than there aren't really any results to return.
             */
            if (startIndex >= blockStart + objkeys.length) {
                // Return an empty array
                return ObjectDb.EMPTY_BLOCK;
            } else {
                return objkeys;
            }
        }
    }

    public String getString(String field) {
        return resultRecord.getString(field);
    }

    public boolean getBoolean(String field) {
        return resultRecord.getBoolean(field);
    }

    public int getInt(String field) {
        return resultRecord.getInt(field);
    }

    public java.util.Date getDate(String field) {
        return resultRecord.getDate(field);
    }

    public long getLong(String field) {
        return resultRecord.getLong(field);
    }

    public double getDouble(String field) {
        return resultRecord.getDouble(field);
    }

    public Object get(String field) {
        return resultRecord.get(field);
    }

    public void set(String field, Object value) {
        resultRecord.set(field, value);
    }

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

    public void setObjCachable(boolean objCachable) {
        this.objCachable = objCachable;
    }

    public void setListCachable(boolean listCachable) {
        this.listCachable = listCachable;
    }

    public boolean isLoaded() {
        return loaded;
    }

    public ResultRecord getResultRecord() {
        return resultRecord;
    }

    public boolean isObjCachable() {
        return objCachable;
    }

    public boolean isListCachable() {
        return listCachable;
    }

    public boolean objCachable = true;
    public boolean listCachable = true;

}

⌨️ 快捷键说明

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