📄 compiledstatement.java
字号:
/* 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 org.hsqldb.HsqlNameManager.HsqlName;/** * A simple structure class for holding the products of * statement compilation for later execution. * * @author boucherb@users * @version 1.7.2 * @since 1.7.2 */// fredt@users 20040404 - patch 1.7.2 - fixed type resolution for parameters// boucherb@users 200404xx - patch 1.7.2 - changed parameter naming scheme for SQLCI client usability/support// fredt@users 20050609 - 1.8.0 - fixed EXPLAIN PLAN by implementing describe(Session)final class CompiledStatement { static final String PCOL_PREFIX = "@p"; static final String RETURN_COLUMN_NAME = "@p0"; static final int UNKNOWN = 0; // enumeration of allowable CompiledStatement types static final int INSERT_VALUES = 1; static final int INSERT_SELECT = 2; static final int UPDATE = 3; static final int DELETE = 4; static final int SELECT = 5; static final int SELECT_INTO = 6; static final int CALL = 7; // enumeration of catagories static final int DML = 7; static final int DQL = 8; static final int DDL = 9; /** id in CompiledStatementManager */ int id; /** false when cleared */ boolean isValid = true; /** target table for INSERT_XXX, UPDATE and DELETE */ Table targetTable; /** table filter for UPDATE and DELETE */ TableFilter targetFilter; /** condition expression for UPDATE and DELETE */ Expression condition; /** column map for INSERT_XXX, UPDATE */ int[] columnMap; /** Column value Expressions for INSERT_VALUES and UPDATE. */ Expression[] columnValues; /** * Flags indicating which columns' values will/will not be * explicitly set. */ boolean[] checkColumns; /** Expression to be evaluated when this is a CALL statement. */ Expression expression; /** * Select to be evaluated when this is an INSERT_SELECT or * SELECT statement */ Select select; /** * Parse-order array of Expression objects, all of iType == PARAM , * involved in some way in any INSERT_XXX, UPDATE, DELETE, SELECT or * CALL CompiledStatement */ Expression[] parameters; /** * int[] contains type of each parameter */ int[] paramTypes; /** * Subqueries inverse parse depth order */ SubQuery[] subqueries; /** * The type of this CompiledStatement. <p> * * One of: <p> * * <ol> * <li>UNKNOWN * <li>INSERT_VALUES * <li>INSERT_SELECT * <li>UPDATE * <li>DELETE * <li>SELECT * <li>CALL * <li>DDL * </ol> */ int type; /** * The SQL string that produced this compiled statement */ String sql; /** * The default schema name used to resolve names in the sql */ final HsqlName schemaHsqlName; /** * Creates a new instance of CompiledStatement for DDL * */ CompiledStatement(HsqlName schema) { parameters = new Expression[0]; paramTypes = new int[0]; subqueries = new SubQuery[0]; type = DDL; schemaHsqlName = schema; } /** * Initializes this as a DELETE statement * * @param targetFilter * @param deleteCondition * @param parameters */ CompiledStatement(Session session, Database database, HsqlName schema, TableFilter targetFilter, Expression deleteCondition, SubQuery[] subqueries, Expression[] params) throws HsqlException { schemaHsqlName = schema; this.targetFilter = targetFilter; targetTable = targetFilter.filterTable; if (deleteCondition != null) { condition = new Expression(deleteCondition); condition.resolveTables(targetFilter); condition.resolveTypes(session); targetFilter.setConditions(session, condition); } setParameters(params); setSubqueries(subqueries); type = DELETE; } /** * Instantiate this as an UPDATE statement. * * @param targetTable * @param columnMap * @param columnValues * @param updateCondition * @param params */ CompiledStatement(Session session, Database database, HsqlName schema, TableFilter targetFilter, int[] columnMap, Expression[] columnValues, Expression updateCondition, SubQuery[] subqueries, Expression[] params) throws HsqlException { schemaHsqlName = schema; this.targetFilter = targetFilter; targetTable = targetFilter.filterTable; this.columnMap = columnMap; this.columnValues = columnValues; for (int i = 0; i < columnValues.length; i++) { Expression cve = columnValues[i]; if (cve.isParam()) { cve.setTableColumnAttributes(targetTable, columnMap[i]); } else { cve.resolveTables(targetFilter); cve.resolveTypes(session); } } if (updateCondition != null) { condition = new Expression(updateCondition); condition.resolveTables(targetFilter); condition.resolveTypes(session); targetFilter.setConditions(session, condition); } setParameters(params); setSubqueries(subqueries); type = UPDATE; } /** * Instantiate this as an INSERT_VALUES statement. * * @param targetTable * @param columnMap * @param columnValues * @param checkColumns * @param params */ CompiledStatement(HsqlName schema, Table targetTable, int[] columnMap, Expression[] columnValues, boolean[] checkColumns, SubQuery[] subqueries, Expression[] params) throws HsqlException { schemaHsqlName = schema; this.targetTable = targetTable; this.columnMap = columnMap; this.checkColumns = checkColumns; this.columnValues = columnValues; for (int i = 0; i < columnValues.length; i++) { Expression cve = columnValues[i]; // If its not a param, it's already been resolved in // Parser.getColumnValueExpressions if (cve.isParam()) { cve.setTableColumnAttributes(targetTable, columnMap[i]); } } setParameters(params); setSubqueries(subqueries); type = INSERT_VALUES; } /** * Instantiate this as an INSERT_SELECT statement. * * @param targetTable * @param columnMap * @param checkColumns * @param select * @param params */ CompiledStatement(Session session, Database database, HsqlName schema, Table targetTable, int[] columnMap, boolean[] checkColumns, Select select, SubQuery[] subqueries, Expression[] params) throws HsqlException { schemaHsqlName = schema; this.targetTable = targetTable; this.columnMap = columnMap; this.checkColumns = checkColumns; this.select = select; // resolve any parameters in SELECT resolveInsertParameterTypes(); // set select result metadata etc. select.prepareResult(session); setParameters(params); setSubqueries(subqueries); type = INSERT_SELECT; } /** * Instantiate this as a SELECT statement. * * @param select * @param params */ CompiledStatement(Session session, Database database, HsqlName schema, Select select, SubQuery[] subqueries, Expression[] params) throws HsqlException { schemaHsqlName = schema; this.select = select; // resolve any parameters in SELECT as VARCHAR for (int i = 0; i < select.iResultLen; i++) { Expression colexpr = select.exprColumns[i]; if (colexpr.getDataType() == Types.NULL) { colexpr.setDataType(Types.VARCHAR); } } // set select result metadata etc. select.prepareResult(session); setParameters(params); setSubqueries(subqueries); type = SELECT; } /** * Instantiate this as a CALL statement. * * @param expression * @param params */ CompiledStatement(Session session, Database database, HsqlName schema, Expression expression, SubQuery[] subqueries, Expression[] params) throws HsqlException { schemaHsqlName = schema; this.expression = expression; expression.resolveTypes(session); expression.paramMode = Expression.PARAM_OUT; setParameters(params); setSubqueries(subqueries); type = CALL; } /** * For parameters in INSERT_VALUES and INSERT_SELECT lists */ private void resolveInsertParameterTypes() { for (int i = 0; i < select.iResultLen; i++) { Expression colexpr = select.exprColumns[i]; if (colexpr.getDataType() == Types.NULL) { Column col = targetTable.getColumn(columnMap[i]); colexpr.setDataType(col.getType()); } } } private void setParameters(Expression[] params) { this.parameters = params; int[] types = new int[parameters.length]; for (int i = 0; i < parameters.length; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -