📄 result.java
字号:
/* Copyright (c) 1995-2000, The Hypersonic SQL Group. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of the Hypersonic SQL Group nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE HYPERSONIC SQL GROUP, * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * This software consists of voluntary contributions made by many individuals * on behalf of the Hypersonic SQL Group. * * * For work added by the HSQL Development Group: * * Copyright (c) 2001-2005, The HSQL Development Group * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of the HSQL Development Group nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG, * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package org.hsqldb;import java.io.DataInput;import java.io.IOException;import java.io.OutputStream;import java.util.NoSuchElementException;import org.hsqldb.lib.Iterator;import org.hsqldb.rowio.RowInputBinary;import org.hsqldb.rowio.RowOutputBinary;// fredt@users 20020130 - patch 1.7.0 by fredt// to ensure consistency of r.rTail r.iSize in all operations// methods for set operations moved here from Select.java// tony_lai@users 20020820 - patch 595073 - duplicated exception msg// fredt@users 20030801 - patch 1.7.2 - separate metadata and polymophic serialisation// boucherb@users 200307/8 - various, in support of fred's work over the same time period/** * The primary unit of comunication between Connection, Server and Session * objects. * * An HSQLDB Result object encapsulates all requests (such as to alter or * query session settings, to allocate and execute statements, etc.) and all * responses (such as exception indications, update counts, result sets and * result set metadata). It also implements the HSQL wire protocol for * comunicating all such requests and responses across the network. * * Extensively rewritten and extended in successive versions of HSQLDB. * * @author Thomas Mueller (Hypersonic SQL Group) * @version 1.8.0 * @since Hypersonic SQL */public class Result { // record list public Record rRoot; private Record rTail; private int size; // transient - number of significant columns private int significantColumns; // type of result public int mode;// boolean isMulti; // database ID int databaseID; // session ID int sessionID; // user / password or error strings String mainString; String subString; // database name String subSubString; // the exception if this is an error private Throwable exception; // prepared statement id / error vendor code int statementID; // max rows (out) or update count (in) int updateCount; public ResultMetaData metaData; /** A Result object's metadata */ public static class ResultMetaData { // always resolved public String[] colLabels; public String[] tableNames; public String[] colNames; public boolean[] isLabelQuoted; public int[] colTypes; public int[] colSizes; public int[] colScales; // extra attrs, sometimes resolved public String[] catalogNames; public String[] schemaNames; public int[] colNullable; public boolean[] isIdentity; public boolean[] isWritable; public int[] paramMode; // It's possible to do better than java.lang.Object // for type OTHER if the expression generating the value // is of type FUNCTION. This applies to result set columns // whose value is the result of a SQL function call and // especially to the arguments and return value of a CALL public String[] classNames; boolean isParameterDescription; ResultMetaData() {} ResultMetaData(int n) { prepareData(n); } /** * Method declaration * * @param columns */ private void prepareData(int columns) { colLabels = new String[columns]; tableNames = new String[columns]; colNames = new String[columns]; isLabelQuoted = new boolean[columns]; colTypes = new int[columns]; colSizes = new int[columns]; colScales = new int[columns]; catalogNames = new String[columns]; schemaNames = new String[columns]; colNullable = new int[columns]; isIdentity = new boolean[columns]; isWritable = new boolean[columns]; classNames = new String[columns]; } public int[] getParameterTypes() { return colTypes; } boolean isTableColumn(int i) { return tableNames[i] != null && tableNames[i].length() > 0 && colNames[i] != null && colNames[i].length() > 0; } private void decodeTableColumnAttrs(int in, int i) { colNullable[i] = in & 0x0000000f; isIdentity[i] = (in & 0x00000010) != 0; isWritable[i] = (in & 0x00000020) != 0; } private void writeTableColumnAttrs(RowOutputBinary out, int i) throws IOException, HsqlException { // HSQLDB also ignores precision and scale for all types except // XXXCHAR, for which it may (or may not) perform some trimming/padding. // All in all, it's currently meaningless (indeed misleading) to // transmit and report the values, as the data typically will // not be constrained accordingly.// switch(colType[i]) {// // As early as SQL 92, these are allowed to have a scale.// // However, DatabaseCommandInterpreter.processCreateColumn// // does not currently handle this correctly and will assign// // a precision instead of a scale if TIME(s) or TIMESTAMP(s)// // is specified// case Types.TIME :// case Types.TIMESTAMP :// out.writeIntData(colScale[i]);// break;// case Types.DECIMAL :// case Types.NUMERIC : {// out.writeIntData(colScale[i]);// } // fall through// // Apparently, SQL 92 specifies that FLOAT can have// // a declared precision, which is typically the number of// // bits (not binary digits). In any case, this is somewhat// // meaningless under HSQLDB/Java, in that we use java.lang.Double// // to represent SQL FLOAT// case Types.FLOAT :// // It's legal to declare precision for these, although HSQLDB// // currently does not use it to constrain values// case Types.BINARY :// case Types.VARBINARY :// case Types.LONGVARBINARY :// // possibly, but not universally acted upon (trimmming/padding)// case Types.CHAR :// case Types.VARCHAR :// case Types.LONGVARCHAR : {// out.writeIntData(colSize[i]);// }// } out.writeIntData(encodeTableColumnAttrs(i)); out.writeString(catalogNames[i] == null ? "" : catalogNames[i]); out.writeString(schemaNames[i] == null ? "" : schemaNames[i]); } private int encodeTableColumnAttrs(int i) { int out = colNullable[i]; // always between 0x00 and 0x02 if (isIdentity[i]) { out |= 0x00000010; } if (isWritable[i]) { out |= 0x00000020; } return out; } private void readTableColumnAttrs(RowInputBinary in, int i) throws IOException, HsqlException { decodeTableColumnAttrs(in.readIntData(), i); catalogNames[i] = in.readString(); schemaNames[i] = in.readString(); } ResultMetaData(RowInputBinary in, int mode) throws HsqlException, IOException { int l = in.readIntData(); prepareData(l); if (mode == ResultConstants.PARAM_META_DATA) { isParameterDescription = true; paramMode = new int[l]; } for (int i = 0; i < l; i++) { colTypes[i] = in.readType(); // fredt - 1.8.0 added colSizes[i] = in.readIntData(); colScales[i] = in.readIntData(); colLabels[i] = in.readString(); tableNames[i] = in.readString(); colNames[i] = in.readString(); classNames[i] = in.readString(); if (isTableColumn(i)) { readTableColumnAttrs(in, i); } if (mode == ResultConstants.PARAM_META_DATA) { paramMode[i] = in.readIntData(); } } } void write(RowOutputBinary out, int colCount) throws HsqlException, IOException { out.writeIntData(colCount); for (int i = 0; i < colCount; i++) { out.writeType(colTypes[i]); // fredt - 1.8.0 added out.writeIntData(colSizes[i]); out.writeIntData(colScales[i]); out.writeString(colLabels[i] == null ? "" : colLabels[i]); out.writeString(tableNames[i] == null ? "" : tableNames[i]); out.writeString(colNames[i] == null ? "" : colNames[i]); out.writeString(classNames[i] == null ? "" : classNames[i]); if (isTableColumn(i)) { writeTableColumnAttrs(out, i); } if (isParameterDescription) { out.writeIntData(paramMode[i]); } } } } /** * General constructor */ public Result(int type) { mode = type;/* if (type == ResultConstants.MULTI) { isMulti = true; }*/ if (type == ResultConstants.DATA || type == ResultConstants.PARAM_META_DATA || type == ResultConstants.SQLEXECUTE || type == ResultConstants.SETSESSIONATTR) { metaData = new ResultMetaData(); } } Result(ResultMetaData md) { mode = ResultConstants.DATA; significantColumns = md.colTypes.length; metaData = md; }// fredt@users 20020221 - patch 513005 by sqlbob@users (RMP) /** * Constructor for errors * * @param error error message * @param state sql state * @param code vendor code */ Result(String error, String state, int code) { mode = ResultConstants.ERROR; mainString = error; subString = state; statementID = code; subSubString = ""; } /** * Only used with DATA and PARAM_META_DATA results * * @param columns */ Result(int type, int columns) { metaData = new ResultMetaData(); metaData.prepareData(columns); if (type == ResultConstants.PARAM_META_DATA) { metaData.isParameterDescription = true; metaData.paramMode = new int[columns]; } mode = type; significantColumns = columns; } /** * For BATCHEXECUTE and BATCHEXECDIRECT */ public Result(int type, int[] types, int id) { mode = type; metaData = new ResultMetaData(); metaData.colTypes = types; significantColumns = types.length; statementID = id; } /** * Constructor declaration * * @param in * @exception HsqlException Description of the Exception */ Result(RowInputBinary in) throws HsqlException { try { mode = in.readIntData(); if (mode == ResultConstants.MULTI) { readMultiResult(in); return; } databaseID = in.readIntData(); sessionID = in.readIntData(); switch (mode) { case ResultConstants.GETSESSIONATTR : case ResultConstants.SQLDISCONNECT : case ResultConstants.SQLSTARTTRAN : case ResultConstants.HSQLRESETSESSION : break; case ResultConstants.SQLPREPARE : setStatementType(in.readIntData()); mainString = in.readString(); break; case ResultConstants.PREPARE_ACK : case ResultConstants.SQLFREESTMT : statementID = in.readIntData(); break; case ResultConstants.SQLEXECDIRECT : updateCount = in.readIntData(); statementID = in.readIntData(); mainString = in.readString(); break; case ResultConstants.ERROR : case ResultConstants.SQLCONNECT : mainString = in.readString(); subString = in.readString(); subSubString = in.readString(); statementID = in.readIntData();// throw Trace.getError(string, code); break; case ResultConstants.UPDATECOUNT : updateCount = in.readIntData(); break; case ResultConstants.SQLENDTRAN : { int type = in.readIntData(); setEndTranType(type); // endtran type switch (type) { case ResultConstants.SAVEPOINT_NAME_RELEASE : case ResultConstants.SAVEPOINT_NAME_ROLLBACK : mainString = in.readString(); // savepoint name } break; } case ResultConstants.BATCHEXECUTE : case ResultConstants.BATCHEXECDIRECT : case ResultConstants.SQLEXECUTE : case ResultConstants.SETSESSIONATTR : { updateCount = in.readIntData(); statementID = in.readIntData(); int l = in.readIntData(); metaData = new ResultMetaData(l); significantColumns = l; for (int i = 0; i < l; i++) { metaData.colTypes[i] = in.readType(); } int count = in.readIntData(); while (count-- > 0) { add(in.readData(metaData.colTypes)); } break; } case ResultConstants.DATA : case ResultConstants.PARAM_META_DATA : { metaData = new ResultMetaData(in, mode);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -