selectresult.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,222 行 · 第 1/2 页
JAVA
1,222 行
/* * 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.db.sql;import com.caucho.db.store.BlobInputStream;import com.caucho.db.store.Store;import com.caucho.db.table.Column;import com.caucho.db.table.TableIterator;import com.caucho.sql.SQLExceptionWrapper;import com.caucho.util.CharBuffer;import com.caucho.util.FreeList;import com.caucho.util.IntArray;import com.caucho.util.L10N;import com.caucho.util.QDate;import com.caucho.vfs.TempBuffer;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.sql.Blob;import java.sql.Clob;import java.sql.SQLException;public class SelectResult { private static final L10N L = new L10N(SelectResult.class); private static final FreeList<SelectResult> _freeList = new FreeList<SelectResult>(32); private static final int SIZE = TempBuffer.SIZE; private static QDate _date = new QDate(); private CharBuffer _cb = new CharBuffer(); private byte []_blob = new byte[128]; private Expr []_exprs; private Store []_stores = new Store[32]; private TableIterator []_rows = new TableIterator[16]; private Order _order; private IntArray _orderIndex; private TempBuffer []_tempBuffers = new TempBuffer[128]; private byte [][]_buffers = new byte[128][]; private int _length; private int _rowCount; private int _row; private int _offset; private int _rowOffset; private int _columnOffset; private int _column; private boolean _wasNull; private SelectResult() { } public static SelectResult create(Expr []exprs, Order order) { SelectResult rs = _freeList.allocate(); if (rs == null) rs = new SelectResult(); rs.init(exprs, order); return rs; } /** * Initialize the iterator. */ TableIterator []initRows(FromItem []fromItems) { if (_rows.length < fromItems.length) _rows = new TableIterator[fromItems.length]; for (int i = 0; i < fromItems.length; i++) { if (_rows[i] == null) _rows[i] = new TableIterator(); _rows[i].init(fromItems[i].getTable()); } return _rows; } /** * Initialize based on the exprs. */ private void init(Expr []exprs, Order order) { _exprs = exprs; _order = order; if (order != null) _orderIndex = new IntArray(); if (_stores.length < _exprs.length) { _stores = new Store[exprs.length]; } for (int i = 0; i < exprs.length; i++) _stores[i] = exprs[i].getTable(); _length = 0; _rowCount = 0; } void initRead() throws SQLException { if (_order != null) _order.sort(this, _orderIndex); _row = -1; _offset = 0; _column = 0; _rowOffset = 0; _columnOffset = 0; } /** * Moves to the next row, returning true if the row has data. */ public boolean next() throws SQLException { if (++_row < _rowCount) { if (_orderIndex != null) { _offset = _orderIndex.get(_row); } else if (_row != 0) { _offset = _columnOffset; skipColumns(_exprs.length - _column); } _column = 0; _rowOffset = _offset; _columnOffset = _rowOffset; return true; } else return false; } /** * Returns the expressions. */ public Expr []getExprs() { return _exprs; } /** * Returns the column index with the given name. */ public int findColumnIndex(String name) throws SQLException { for (int i = 0; i < _exprs.length; i++) { if (_exprs[i].getName().equals(name)) return i + 1; } throw new SQLException(L.l("column `{0}' does not exist.", name)); } /** * Returns the string value of the given index. */ public String getString(int index) throws SQLException { _wasNull = false; setColumn(index); int type = read(); switch (type) { case Column.NONE: _wasNull = true; return null; case Column.SHORT: { int value = (short) ((read() << 8) + (read())); return String.valueOf(value); } case Column.INT: { int value = ((read() << 24) + (read() << 16) + (read() << 8) + (read())); return String.valueOf(value); } case Column.LONG: { long value = (((long) read() << 56) + ((long) read() << 48) + ((long) read() << 40) + ((long) read() << 32) + ((long) read() << 24) + ((long) read() << 16) + ((long) read() << 8) + ((long) read())); return String.valueOf(value); } case Column.DOUBLE: { long value = (((long) read() << 56) + ((long) read() << 48) + ((long) read() << 40) + ((long) read() << 32) + ((long) read() << 24) + ((long) read() << 16) + ((long) read() << 8) + ((long) read())); return String.valueOf(Double.longBitsToDouble(value)); } case Column.DATE: { long value = (((long) read() << 56) + ((long) read() << 48) + ((long) read() << 40) + ((long) read() << 32) + ((long) read() << 24) + ((long) read() << 16) + ((long) read() << 8) + ((long) read())); return QDate.formatISO8601(value); } case Column.VARCHAR: return readString(); case Column.BLOB: return readBlobString(); case Column.BINARY: { int len = read(); byte []bytes = new byte[len]; read(bytes, 0, len); return toHex(bytes); } default: throw new RuntimeException("unknown column type:" + type + " column:" + index); } } /** * Returns the string value of the given index. */ public byte [] getBytes(int index) throws SQLException { _wasNull = false; setColumn(index); int type = read(); switch (type) { case Column.NONE: _wasNull = true; return null; case Column.BINARY: { int len = read(); byte []bytes = new byte[len]; read(bytes, 0, len); return bytes; } case Column.BLOB: return readBlobBytes(); default: throw new RuntimeException("unknown column type:" + type + " column:" + index); } } /** * Returns the integer value of the column. */ public int getInt(int index) throws SQLException { _wasNull = false; setColumn(index); int type = read(); switch (type) { case Column.NONE: _wasNull = true; return 0; case Column.SHORT: { int value = (short) ((read() << 8) + (read())); return value; } case Column.INT: { int value = ((read() << 24) + (read() << 16) + (read() << 8) + (read())); return value; } case Column.LONG: case Column.DATE: { long value = (((long) read() << 56) + ((long) read() << 48) + ((long) read() << 40) + ((long) read() << 32) + ((long) read() << 24) + ((long) read() << 16) + ((long) read() << 8) + ((long) read())); return (int) value; } case Column.DOUBLE: { long value = (((long) read() << 56) + ((long) read() << 48) + ((long) read() << 40) + ((long) read() << 32) + ((long) read() << 24) + ((long) read() << 16) + ((long) read() << 8) + ((long) read())); return (int) Double.longBitsToDouble(value); } case Column.VARCHAR: return Integer.parseInt(readString()); case Column.BLOB: return Integer.parseInt(readBlobString()); default: return 0; } } /** * Returns the long value of the column. */ public long getLong(int index) throws SQLException { _wasNull = false; setColumn(index); int type = read(); switch (type) { case Column.NONE: _wasNull = true; return 0; case Column.SHORT: { int value = (short) ((read() << 8) + (read())); return value; } case Column.INT: { int value = ((read() << 24) + (read() << 16) + (read() << 8) + (read())); return value; } case Column.LONG: case Column.DATE: { long value = (((long) read() << 56) + ((long) read() << 48) + ((long) read() << 40) + ((long) read() << 32) + ((long) read() << 24) + ((long) read() << 16) + ((long) read() << 8) + ((long) read())); return value; } case Column.DOUBLE: { long value = (((long) read() << 56) + ((long) read() << 48) + ((long) read() << 40) + ((long) read() << 32) + ((long) read() << 24) + ((long) read() << 16) + ((long) read() << 8) + ((long) read())); return (long) Double.longBitsToDouble(value); } case Column.VARCHAR: return Long.parseLong(readString()); case Column.BLOB: return Long.parseLong(readBlobString()); default: return 0; } } /** * Returns a double value from this column. */ public double getDouble(int index) throws SQLException { _wasNull = false; setColumn(index); int type = read(); switch (type) { case Column.NONE: _wasNull = true; return 0; case Column.SHORT: { int value = (short) ((read() << 8) + (read())); return value; } case Column.INT: { int value = ((read() << 24) + (read() << 16) + (read() << 8) + (read())); return value; } case Column.LONG: case Column.DATE: { long value = (((long) read() << 56) + ((long) read() << 48) + ((long) read() << 40) + ((long) read() << 32) + ((long) read() << 24) + ((long) read() << 16) + ((long) read() << 8) + ((long) read())); return value; } case Column.DOUBLE: { long value = (((long) read() << 56) + ((long) read() << 48) + ((long) read() << 40) + ((long) read() << 32) + ((long) read() << 24) + ((long) read() << 16) + ((long) read() << 8) + ((long) read())); return Double.longBitsToDouble(value); } case Column.VARCHAR: return Double.parseDouble(readString()); case Column.BLOB: return Double.parseDouble(readBlobString()); default: return 0; } } public long getDate(int index) throws SQLException { _wasNull = false; setColumn(index); int type = read(); switch (type) { case Column.NONE: _wasNull = true; return 0; case Column.LONG: case Column.DATE: { long value = (((long) read() << 56) + ((long) read() << 48) + ((long) read() << 40) + ((long) read() << 32) + ((long) read() << 24) + ((long) read() << 16) + ((long) read() << 8) + ((long) read())); return value; } case Column.VARCHAR: { String value = readString(); synchronized (_date) { try { return _date.parseDate(value); } catch (Exception e) { throw new SQLExceptionWrapper(e); } } } case Column.BLOB: { String value = readBlobString(); synchronized (_date) { try { return _date.parseDate(value); } catch (Exception e) { throw new SQLExceptionWrapper(e); } } } default: throw new SQLException("unknown type: " + type); } } /**
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?