resultsetimpl.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,011 行 · 第 1/3 页
JAVA
2,011 行
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.amber.query;import com.caucho.amber.entity.AmberEntityHome;import com.caucho.amber.entity.Entity;import com.caucho.amber.entity.EntityItem;import com.caucho.amber.expr.AmberExpr;import com.caucho.amber.expr.LoadEntityExpr;import com.caucho.amber.manager.AmberConnection;import com.caucho.amber.type.EntityType;import com.caucho.util.L10N;import java.io.InputStream;import java.io.Reader;import java.math.BigDecimal;import java.net.URL;import java.sql.*;import java.util.ArrayList;import java.util.Calendar;import java.util.Map;import java.util.logging.Level;import java.util.logging.Logger;/** * The JDBC statement implementation. */public class ResultSetImpl implements ResultSet { private static final Logger log = Logger.getLogger(ResultSetImpl.class.getName()); private static final L10N L = new L10N(ResultSetImpl.class); public static final int CACHE_CHUNK_SIZE = 64; private UserQuery _userQuery; private ResultSet _rs; private ArrayList<FromItem> _fromList; private ArrayList<AmberExpr> _resultList; private Map<AmberExpr, String> _joinFetchMap; private AmberConnection _session; private QueryCacheKey _cacheKey; private ResultSetCacheChunk _cacheChunk; private ResultSetMetaData _cacheMetaData; private boolean _isCache; private int _firstResult; private int _maxResults = Integer.MAX_VALUE / 2; private int _row; private int _numberOfLoadingColumns = 1; public ResultSetImpl() { } /** * Returns the join fetch map. */ public Map<AmberExpr, String> getJoinFetchMap() { return _joinFetchMap; } /** * Sets the user query */ public void setUserQuery(UserQuery userQuery) { _userQuery = userQuery; } /** * Sets the result set. */ public void setResultSet(ResultSet rs) throws SQLException { _rs = rs; if (rs != null) _cacheMetaData = rs.getMetaData(); } /** * Sets the result set and meta data. */ public void setResultSet(ResultSet rs, ResultSetMetaData metaData) { _rs = rs; _cacheMetaData = metaData; } /** * Sets the query. */ public void setQuery(SelectQuery query) { _fromList = query.getFromList(); _resultList = query.getResultList(); _joinFetchMap = query.getJoinFetchMap(); } /** * Sets the session. */ public void setSession(AmberConnection aConn) { _session = aConn; } /** * Sets the first cache chunk */ public void setCacheChunk(ResultSetCacheChunk cacheChunk, ResultSetMetaData metaData) { _cacheChunk = cacheChunk; _cacheMetaData = metaData; _isCache = true; } /** * Sets the first result. */ public void setFirstResult(int first) { _firstResult = first; } /** * Sets the max result. */ public void setMaxResults(int max) { if (max < 0) _maxResults = Integer.MAX_VALUE / 2; else _maxResults = max; } /** * Fills the cache chunk. */ public void fillCacheChunk(ResultSetCacheChunk cacheChunk) throws SQLException { int size = CACHE_CHUNK_SIZE; int maxSize = Integer.MAX_VALUE / 2; int i = 0; ResultSetCacheChunk tail = cacheChunk; // max length of the cached value for (; maxSize-- > 0; i++) { if (_rs.next()) { if (size <= i) { i = 0; ResultSetCacheChunk next = new ResultSetCacheChunk(tail); tail.setNext(next); tail = next; } tail.newRow(); int len = _resultList.size(); int offset = 0; for (int j = 0; j < len; j++) { int index = getColumn(j + 1); AmberExpr expr = _resultList.get(j); if (expr instanceof LoadEntityExpr) { LoadEntityExpr entityExpr = (LoadEntityExpr) expr; Object obj = entityExpr.getCacheObject(_session, _rs, index + offset, _joinFetchMap); tail.setValue(i, j, obj); // jpa/11z1 offset += entityExpr.getIndex(); } else { Object obj = expr.getCacheObject(_session, _rs, index + offset); tail.setValue(i, j, obj); } } } else { tail.setLast(true); return; } } /* if (! _rs.next()) { tail.setLast(true); } */ } /** * Initialize */ public void init() throws SQLException { _numberOfLoadingColumns = 1; while (_row < _firstResult && next()) { } } public void setRow(int row) { _row = row; } /** * Returns the current row number. */ public int getRow() throws SQLException { return _row; } /** * Returns true before the first row. */ public boolean isBeforeFirst() throws SQLException { return _rs.isBeforeFirst(); } /** * Returns true if this is the first row. */ public boolean isFirst() throws SQLException { return _rs.isFirst(); } /** * Returns true if this is the last row. */ public boolean isLast() throws SQLException { return _rs.isLast(); } /** * Returns true if this is after the last row. */ public boolean isAfterLast() throws SQLException { return _rs.isAfterLast(); } /** * Returns the statement for the result. */ public java.sql.Statement getStatement() throws SQLException { return _rs.getStatement(); } /** * Returns the metadata. */ public java.sql.ResultSetMetaData getMetaData() throws SQLException { if (_rs == null) return _cacheMetaData; else { _cacheMetaData = _rs.getMetaData(); return _cacheMetaData; } } /** * Returns the warnings. */ public SQLWarning getWarnings() throws SQLException { return _rs.getWarnings(); } /** * Clears the warnings. */ public void clearWarnings() throws SQLException { _rs.clearWarnings(); } /** * Returns the cursor name. */ public String getCursorName() throws SQLException { return _rs.getCursorName(); } /** * Sets the fetch size. */ public void setFetchSize(int size) throws SQLException { _rs.setFetchSize(size); } /** * Gets the fetch size. */ public int getFetchSize() throws SQLException { return _rs.getFetchSize(); } /** * Gets the fetch direction. */ public int getFetchDirection() throws SQLException { return _rs.getFetchDirection(); } /** * Sets the fetch direction. */ public void setFetchDirection(int dir) throws SQLException { _rs.setFetchDirection(dir); } /** * Gets the concurrency. */ public int getConcurrency() throws SQLException { return _rs.getConcurrency(); } /** * Returns the next row. */ public boolean next() throws SQLException { if (_firstResult + _maxResults <= _row) return false; int row = _row++; ResultSetCacheChunk cacheChunk = _cacheChunk; if (cacheChunk == null) return _rs != null ? _rs.next() : false; else if (row < cacheChunk.getRowCount()) { return true; } else { ResultSetCacheChunk next = cacheChunk.getNext(); if (next != null) { _cacheChunk = next; return true; } _isCache = false; _cacheChunk = null; // jpa/1433 /* if (cacheChunk.isLast()) { _maxResults = 0; return false; } else */ if (_rs != null) { return _rs.next(); } /* else if (_userQuery != null) { _rs = _userQuery.executeQuery(row, -1); _cacheMetaData = _rs.getMetaData(); return _rs.next(); } */ else { return false; } } } /** * Returns the previous row. */ public boolean previous() throws SQLException { if (_row <= _firstResult) return false; _row--; return _rs.previous(); } /** * Move relative. */ public boolean relative(int delta) throws SQLException { return _rs.relative(delta); } /** * Move absolute. */ public boolean absolute(int delta) throws SQLException { return _rs.absolute(delta); } /** * Moves before the first row. */ public void beforeFirst() throws SQLException { _rs.beforeFirst(); } /** * Move to first */ public boolean first() throws SQLException { return _rs.first(); } /** * Move to last */ public boolean last() throws SQLException { return _rs.last(); } /** * Moves after the last row. */ public void afterLast() throws SQLException { _rs.afterLast(); } /** * Returns true if the last column read was null. */ public boolean wasNull() throws SQLException { return _rs.wasNull(); } /** * Returns the type of the last column. */ public int getType() throws SQLException { return _rs.getType(); } /** * Returns the external column id corresponding to the column name. */ public int findColumn(String columnName) throws SQLException { throw new UnsupportedOperationException(); } /** * Returns the boolean value for the column. */ public boolean getBoolean(String columnName) throws SQLException { int column = getColumn(columnName); if (_cacheChunk != null) return _cacheChunk.getBoolean(_row - 1, column - 1); else return _rs.getBoolean(column); } /** * Returns the boolean value for the column. */ public boolean getBoolean(int column) throws SQLException { if (_cacheChunk != null) return _cacheChunk.getBoolean(_row - 1, column - 1); else return _rs.getBoolean(column); } /** * Returns the byte value for the column. */ public byte getByte(String columnName) throws SQLException { int column = getColumn(columnName); if (_cacheChunk != null) return _cacheChunk.getByte(_row - 1, column - 1); else return _rs.getByte(column); } /** * Returns the byte value for the column. */ public byte getByte(int column) throws SQLException { if (_cacheChunk != null) return _cacheChunk.getByte(_row - 1, column - 1); else return _rs.getByte(column); } /** * Returns the short value for the column. */ public short getShort(String columnName) throws SQLException { int column = getColumn(columnName); if (_cacheChunk != null) return _cacheChunk.getShort(_row - 1, column - 1); else return _rs.getShort(column); } /** * Returns the short value for the column. */ public short getShort(int column) throws SQLException { if (_cacheChunk != null) return _cacheChunk.getShort(_row - 1, column - 1); else return _rs.getShort(column); } /** * Returns the int value for the column. */ public int getInt(String columnName) throws SQLException { int column = getColumn(columnName); if (_cacheChunk != null) return _cacheChunk.getInt(_row - 1, column - 1); else return _rs.getInt(column); } /** * Returns the int value for the column. */ public int getInt(int column) throws SQLException { if (_cacheChunk != null) return _cacheChunk.getInt(_row - 1, column - 1); else return _rs.getInt(column); } /** * Returns the long value for the column. */ public long getLong(String columnName) throws SQLException { int column = getColumn(columnName); if (_cacheChunk != null) return _cacheChunk.getLong(_row - 1, column - 1); else return _rs.getLong(column); } /** * Returns the long value for the column. */ public long getLong(int column) throws SQLException { if (_cacheChunk != null) return _cacheChunk.getLong(_row - 1, column - 1); else return _rs.getLong(column);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?