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

📄 bufferrow.java

📁 mysql5.0 JDBC 驱动 放在glassfish或者tomcat的lib文件夹下就可以了
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 Copyright  2007 MySQL AB, 2008 Sun Microsystems

 This program is free software; you can redistribute it and/or modify
 it under the terms of version 2 of the GNU General Public License as 
 published by the Free Software Foundation.

 There are special exceptions to the terms and conditions of the GPL 
 as it is applied to this software. View the full text of the 
 exception in file EXCEPTIONS-CONNECTOR-J in the directory of this 
 software distribution.

 This program 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.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 */
package com.mysql.jdbc;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.TimeZone;

/**
 * A RowHolder implementation that holds one row packet (which is re-used by the
 * driver, and thus saves memory allocations), and tries when possible to avoid
 * allocations to break out the results as individual byte[]s.
 * 
 * (this isn't possible when doing things like reading floating point values).
 * 
 * @version $Id: $
 */
public class BufferRow extends ResultSetRow {
	private Buffer rowFromServer;

	/**
	 * The beginning of the row packet
	 */
	private int homePosition = 0;
	
	/**
	 * The home position before the is-null bitmask for server-side
	 * prepared statement result sets
	 */
	private int preNullBitmaskHomePosition = 0;

	/**
	 * The last-requested index, used as an optimization, if you ask for the
	 * same index, we won't seek to find it. If you ask for an index that is >
	 * than the last one requested, we start seeking from the last requested
	 * index.
	 */
	private int lastRequestedIndex = -1;

	/**
	 * The position of the last-requested index, optimization in concert with
	 * lastRequestedIndex.
	 */
	private int lastRequestedPos;

	/**
	 * The metadata of the fields of this result set.
	 */
	private Field[] metadata;

	/**
	 * Is this a row from a server-side prepared statement? If so, they're
	 * encoded differently, so we have different ways of finding where each
	 * column is, and unpacking them.
	 */
	private boolean isBinaryEncoded;

	/**
	 * If binary-encoded, the NULL status of each column is at the beginning of
	 * the row, so we
	 */
	private boolean[] isNull;

	private List openStreams;

	public BufferRow(Buffer buf, Field[] fields, boolean isBinaryEncoded)
			throws SQLException {
		this.rowFromServer = buf;
		this.metadata = fields;
		this.isBinaryEncoded = isBinaryEncoded;
		this.homePosition = this.rowFromServer.getPosition();
		this.preNullBitmaskHomePosition = this.homePosition;

		if (fields != null) {
			setMetadata(fields);
		}
	}

	public synchronized void closeOpenStreams() {
		if (this.openStreams != null) {
			// This would've looked slicker in a "for" loop
			// but we want to skip over streams that fail to
			// close (they probably won't ever)
			// to be more robust and close everything we _can_

			Iterator iter = this.openStreams.iterator();

			while (iter.hasNext()) {

				try {
					((InputStream) iter.next()).close();
				} catch (IOException e) {
					// ignore - it can't really happen in this case
				}
			}

			this.openStreams.clear();
		}
	}

	private int findAndSeekToOffset(int index) throws SQLException {
		if (!this.isBinaryEncoded) {

			if (index == 0) {
				this.lastRequestedIndex = 0;
				this.lastRequestedPos = this.homePosition;
				this.rowFromServer.setPosition(this.homePosition);

				return 0;
			}

			if (index == this.lastRequestedIndex) {
				this.rowFromServer.setPosition(this.lastRequestedPos);

				return this.lastRequestedPos;
			}

			int startingIndex = 0;

			if (index > this.lastRequestedIndex) {
				if (this.lastRequestedIndex >= 0) {
					startingIndex = this.lastRequestedIndex;
				} else {
					startingIndex = 0;
				}
				
				this.rowFromServer.setPosition(this.lastRequestedPos);
			} else {
				this.rowFromServer.setPosition(this.homePosition);
			}

			for (int i = startingIndex; i < index; i++) {
				this.rowFromServer.fastSkipLenByteArray();
			}

			this.lastRequestedIndex = index;
			this.lastRequestedPos = this.rowFromServer.getPosition();

			return this.lastRequestedPos;
		}

		return findAndSeekToOffsetForBinaryEncoding(index);
	}

	private int findAndSeekToOffsetForBinaryEncoding(int index)
			throws SQLException {
		if (index == 0) {
			this.lastRequestedIndex = 0;
			this.lastRequestedPos = this.homePosition;
			this.rowFromServer.setPosition(this.homePosition);
			
			return 0;
		}

		if (index == this.lastRequestedIndex) {
			this.rowFromServer.setPosition(this.lastRequestedPos);

			return this.lastRequestedPos;
		}

		int startingIndex = 0;

		if (index > this.lastRequestedIndex) {
			if (this.lastRequestedIndex >= 0) {
				startingIndex = this.lastRequestedIndex;
			} else {
				// First-time "scan"
				startingIndex = 0;
				this.lastRequestedPos = this.homePosition;
			}
			
			this.rowFromServer.setPosition(this.lastRequestedPos);
		} else {
			this.rowFromServer.setPosition(this.homePosition);
		}

		for (int i = startingIndex; i < index; i++) {
			if (this.isNull[i]) {
				continue;
			}

			int curPosition = this.rowFromServer.getPosition();

			switch (this.metadata[i].getMysqlType()) {
			case MysqlDefs.FIELD_TYPE_NULL:
				break; // for dummy binds

			case MysqlDefs.FIELD_TYPE_TINY:

				this.rowFromServer.setPosition(curPosition + 1);
				break;

			case MysqlDefs.FIELD_TYPE_SHORT:
			case MysqlDefs.FIELD_TYPE_YEAR:
				this.rowFromServer.setPosition(curPosition + 2);

				break;
			case MysqlDefs.FIELD_TYPE_LONG:
			case MysqlDefs.FIELD_TYPE_INT24:
				this.rowFromServer.setPosition(curPosition + 4);

				break;
			case MysqlDefs.FIELD_TYPE_LONGLONG:
				this.rowFromServer.setPosition(curPosition + 8);

				break;
			case MysqlDefs.FIELD_TYPE_FLOAT:
				this.rowFromServer.setPosition(curPosition + 4);

				break;
			case MysqlDefs.FIELD_TYPE_DOUBLE:
				this.rowFromServer.setPosition(curPosition + 8);

				break;
			case MysqlDefs.FIELD_TYPE_TIME:
				this.rowFromServer.fastSkipLenByteArray();

				break;
			case MysqlDefs.FIELD_TYPE_DATE:

				this.rowFromServer.fastSkipLenByteArray();

				break;
			case MysqlDefs.FIELD_TYPE_DATETIME:
			case MysqlDefs.FIELD_TYPE_TIMESTAMP:
				this.rowFromServer.fastSkipLenByteArray();

				break;
			case MysqlDefs.FIELD_TYPE_TINY_BLOB:
			case MysqlDefs.FIELD_TYPE_MEDIUM_BLOB:
			case MysqlDefs.FIELD_TYPE_LONG_BLOB:
			case MysqlDefs.FIELD_TYPE_BLOB:
			case MysqlDefs.FIELD_TYPE_VAR_STRING:
			case MysqlDefs.FIELD_TYPE_VARCHAR:
			case MysqlDefs.FIELD_TYPE_STRING:
			case MysqlDefs.FIELD_TYPE_DECIMAL:
			case MysqlDefs.FIELD_TYPE_NEW_DECIMAL:
			case MysqlDefs.FIELD_TYPE_GEOMETRY:
			case MysqlDefs.FIELD_TYPE_BIT:
				this.rowFromServer.fastSkipLenByteArray();

				break;

			default:
				throw SQLError.createSQLException(Messages
						.getString("MysqlIO.97") //$NON-NLS-1$
						+ this.metadata[i].getMysqlType()
						+ Messages.getString("MysqlIO.98")
						+ (i + 1)
						+ Messages.getString("MysqlIO.99") //$NON-NLS-1$ //$NON-NLS-2$
						+ this.metadata.length
						+ Messages.getString("MysqlIO.100"), //$NON-NLS-1$
						SQLError.SQL_STATE_GENERAL_ERROR);
			}
		}

		this.lastRequestedIndex = index;
		this.lastRequestedPos = this.rowFromServer.getPosition();

		return this.lastRequestedPos;
	}

	public synchronized InputStream getBinaryInputStream(int columnIndex)
			throws SQLException {
		if (this.isBinaryEncoded) {
			if (isNull(columnIndex)) {
				return null;
			}
		}
		
		findAndSeekToOffset(columnIndex);

		long length = this.rowFromServer.readFieldLength();
		
		int offset = this.rowFromServer.getPosition();

		if (length == Buffer.NULL_LENGTH) {
			return null;
		}

		InputStream stream = new ByteArrayInputStream(this.rowFromServer
				.getByteBuffer(), offset, (int) length);

		if (this.openStreams == null) {
			this.openStreams = new LinkedList();
		}

		return stream;
	}

	public byte[] getColumnValue(int index) throws SQLException {
		findAndSeekToOffset(index);

		if (!this.isBinaryEncoded) {
			return this.rowFromServer.readLenByteArray(0);
		}

		if (this.isNull[index]) {
			return null;
		}

		switch (this.metadata[index].getMysqlType()) {
		case MysqlDefs.FIELD_TYPE_NULL:
			return null;

		case MysqlDefs.FIELD_TYPE_TINY:
			return new byte[] { this.rowFromServer.readByte() };

		case MysqlDefs.FIELD_TYPE_SHORT:
		case MysqlDefs.FIELD_TYPE_YEAR:
			return this.rowFromServer.getBytes(2);

		case MysqlDefs.FIELD_TYPE_LONG:
		case MysqlDefs.FIELD_TYPE_INT24:
			return this.rowFromServer.getBytes(4);

		case MysqlDefs.FIELD_TYPE_LONGLONG:
			return this.rowFromServer.getBytes(8);

		case MysqlDefs.FIELD_TYPE_FLOAT:
			return this.rowFromServer.getBytes(4);

		case MysqlDefs.FIELD_TYPE_DOUBLE:
			return this.rowFromServer.getBytes(8);

		case MysqlDefs.FIELD_TYPE_TIME:
		case MysqlDefs.FIELD_TYPE_DATE:
		case MysqlDefs.FIELD_TYPE_DATETIME:
		case MysqlDefs.FIELD_TYPE_TIMESTAMP:
		case MysqlDefs.FIELD_TYPE_TINY_BLOB:
		case MysqlDefs.FIELD_TYPE_MEDIUM_BLOB:
		case MysqlDefs.FIELD_TYPE_LONG_BLOB:
		case MysqlDefs.FIELD_TYPE_BLOB:
		case MysqlDefs.FIELD_TYPE_VAR_STRING:
		case MysqlDefs.FIELD_TYPE_VARCHAR:
		case MysqlDefs.FIELD_TYPE_STRING:
		case MysqlDefs.FIELD_TYPE_DECIMAL:
		case MysqlDefs.FIELD_TYPE_NEW_DECIMAL:
		case MysqlDefs.FIELD_TYPE_GEOMETRY:
		case MysqlDefs.FIELD_TYPE_BIT:
			return this.rowFromServer.readLenByteArray(0);

		default:
			throw SQLError.createSQLException(Messages.getString("MysqlIO.97") //$NON-NLS-1$
					+ this.metadata[index].getMysqlType()

⌨️ 快捷键说明

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