📄 table.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.IOException;import org.hsqldb.HsqlNameManager.HsqlName;import org.hsqldb.index.RowIterator;import org.hsqldb.lib.ArrayUtil;import org.hsqldb.lib.HashMappedList;import org.hsqldb.lib.HashSet;import org.hsqldb.lib.HsqlArrayList;import org.hsqldb.lib.Iterator;import org.hsqldb.lib.StringUtil;import org.hsqldb.persist.CachedObject;import org.hsqldb.persist.DataFileCache;import org.hsqldb.persist.PersistentStore;import org.hsqldb.rowio.RowInputInterface;import org.hsqldb.store.ValuePool;// fredt@users 20020130 - patch 491987 by jimbag@users - made optional// fredt@users 20020405 - patch 1.7.0 by fredt - quoted identifiers// for sql standard quoted identifiers for column and table names and aliases// applied to different places// fredt@users 20020225 - patch 1.7.0 - restructuring// some methods moved from Database.java, some rewritten// changes to several methods// fredt@users 20020225 - patch 1.7.0 - ON DELETE CASCADE// fredt@users 20020225 - patch 1.7.0 - named constraints// boucherb@users 20020225 - patch 1.7.0 - multi-column primary keys// fredt@users 20020221 - patch 513005 by sqlbob@users (RMP)// tony_lai@users 20020820 - patch 595099 - user defined PK name// tony_lai@users 20020820 - patch 595172 - drop constraint fix// kloska@users 20021030 - patch 1.7.2 - ON UPDATE CASCADE | SET NULL | SET DEFAULT// kloska@users 20021112 - patch 1.7.2 - ON DELETE SET NULL | SET DEFAULT// fredt@users 20021210 - patch 1.7.2 - better ADD / DROP INDEX for non-CACHED tables// fredt@users 20030901 - patch 1.7.2 - allow multiple nulls for UNIQUE columns// fredt@users 20030901 - patch 1.7.2 - reworked IDENTITY support// achnettest@users 20040130 - patch 878288 - bug fix for new indexes in memory tables by Arne Christensen// boucherb@users 20040327 - doc 1.7.2 - javadoc updates// boucherb@users 200404xx - patch 1.7.2 - proper uri for getCatalogName// fredt@users 20050000 - 1.8.0 updates in several areas// fredt@users 20050220 - patch 1.8.0 enforcement of DECIMAL precision/scale/** * Holds the data structures and methods for creation of a database table. * * * Extensively rewritten and extended in successive versions of HSQLDB. * * @author Thomas Mueller (Hypersonic SQL Group) * @version 1.8.0 * @since Hypersonic SQL */public class Table extends BaseTable { // types of table public static final int SYSTEM_TABLE = 0; public static final int SYSTEM_SUBQUERY = 1; public static final int TEMP_TABLE = 2; public static final int MEMORY_TABLE = 3; public static final int CACHED_TABLE = 4; public static final int TEMP_TEXT_TABLE = 5; public static final int TEXT_TABLE = 6; public static final int VIEW = 7;// boucherb@users - for future implementation of SQL standard INFORMATION_SCHEMA static final int SYSTEM_VIEW = 8; // main properties// boucherb@users - access changed in support of metadata 1.7.2 public HashMappedList columnList; // columns in table private int[] primaryKeyCols; // column numbers for primary key private int[] primaryKeyTypes; // types for primary key private int[] primaryKeyColsSequence; // {0,1,2,...} int[] bestRowIdentifierCols; // column set for best index boolean bestRowIdentifierStrict; // true if it has no nullable column int[] bestIndexForColumn; // index of the 'best' index for each column Index bestIndex; // the best index overall - null if there is no user-defined index int identityColumn; // -1 means no such row NumberSequence identitySequence; // next value of identity column NumberSequence rowIdSequence; // next value of optional rowid// ----------------------------------------------------------------------- Constraint[] constraintList; // constrainst for the table HsqlArrayList[] triggerLists; // array of trigger lists private int[] colTypes; // fredt - types of columns private int[] colSizes; // fredt - copy of SIZE values for columns private int[] colScales; // fredt - copy of SCALE values for columns private boolean[] colNullable; // fredt - modified copy of isNullable() values private Expression[] colDefaults; // fredt - expressions of DEFAULT values private int[] defaultColumnMap; // fred - holding 0,1,2,3,... private boolean hasDefaultValues; //fredt - shortcut for above boolean sqlEnforceSize; // inherited from the database - // properties for subclasses protected int columnCount; // inclusive the hidden primary key public Database database; protected DataFileCache cache; protected HsqlName tableName; // SQL name private int tableType; protected boolean isReadOnly; protected boolean isTemp; protected boolean isCached; protected boolean isText; protected boolean isMemory; private boolean isView; protected boolean isLogged; protected int indexType; // fredt - type of index used protected boolean onCommitPreserve; // for temp tables // PersistentStore rowStore; Index[] indexList; // vIndex(0) is the primary key index /** * Constructor * * @param db * @param name * @param type * @param sessionid * @exception HsqlException */ Table(Database db, HsqlName name, int type) throws HsqlException { database = db; sqlEnforceSize = db.sqlEnforceStrictSize; identitySequence = new NumberSequence(null, 0, 1, Types.BIGINT); rowIdSequence = new NumberSequence(null, 0, 1, Types.BIGINT); switch (type) { case SYSTEM_SUBQUERY : isTemp = true; isMemory = true; case SYSTEM_TABLE : isMemory = true; break; case CACHED_TABLE : if (DatabaseURL.isFileBasedDatabaseType(db.getType())) { cache = db.logger.getCache(); isCached = true; isLogged = !database.isFilesReadOnly(); indexType = Index.DISK_INDEX; rowStore = new RowStore(); break; } type = MEMORY_TABLE; case MEMORY_TABLE : isMemory = true; isLogged = !database.isFilesReadOnly(); break; case TEMP_TABLE : isMemory = true; isTemp = true; break; case TEMP_TEXT_TABLE : if (!DatabaseURL.isFileBasedDatabaseType(db.getType())) { throw Trace.error(Trace.DATABASE_IS_MEMORY_ONLY); } isTemp = true; isText = true; isReadOnly = true; indexType = Index.POINTER_INDEX; rowStore = new RowStore(); break; case TEXT_TABLE : if (!DatabaseURL.isFileBasedDatabaseType(db.getType())) { throw Trace.error(Trace.DATABASE_IS_MEMORY_ONLY); } isText = true; indexType = Index.POINTER_INDEX; rowStore = new RowStore(); break; case VIEW : case SYSTEM_VIEW : isView = true; break; } // type may have changed above for CACHED tables tableType = type; tableName = name; primaryKeyCols = null; primaryKeyTypes = null; identityColumn = -1; columnList = new HashMappedList(); indexList = new Index[0]; constraintList = new Constraint[0]; triggerLists = new HsqlArrayList[TriggerDef.NUM_TRIGS];// ----------------------------------------------------------------------------// akede@users - 1.7.2 patch Files readonly // Changing the mode of the table if necessary if (db.isFilesReadOnly() && isFileBased()) { this.isReadOnly = true; }// ---------------------------------------------------------------------------- } boolean equals(Session session, String name) {/* if (isTemp && (session != null && session.getId() != ownerSessionId)) { return false; }*/ return (tableName.name.equals(name)); } boolean equals(String name) { return (tableName.name.equals(name)); } boolean equals(HsqlName name) { return (tableName.equals(name)); } public final boolean isText() { return isText; } public final boolean isTemp() { return isTemp; } public final boolean isReadOnly() { return isReadOnly; } final boolean isView() { return isView; } final int getIndexType() { return indexType; } public final int getTableType() { return tableType; } public final boolean isDataReadOnly() { return isReadOnly; } /** * Used by INSERT, DELETE, UPDATE operations */ void checkDataReadOnly() throws HsqlException { if (isReadOnly) { throw Trace.error(Trace.DATA_IS_READONLY); } }// ----------------------------------------------------------------------------// akede@users - 1.7.2 patch Files readonly void setDataReadOnly(boolean value) throws HsqlException { // Changing the Read-Only mode for the table is only allowed if the // the database can realize it. if (!value && database.isFilesReadOnly() && isFileBased()) { throw Trace.error(Trace.DATA_IS_READONLY); } isReadOnly = value; } /** * Text or Cached Tables are normally file based */ boolean isFileBased() { return isCached || isText; } /** * For text tables */ protected void setDataSource(Session s, String source, boolean isDesc, boolean newFile) throws HsqlException { throw (Trace.error(Trace.TABLE_NOT_FOUND)); } /** * For text tables */ protected String getDataSource() { return null; } /** * For text tables. */ protected boolean isDescDataSource() { return false; } /** * For text tables. */ public void setHeader(String header) throws HsqlException { throw Trace.error(Trace.TEXT_TABLE_HEADER); } /** * For text tables. */ public String getHeader() { return null; } /** * Adds a constraint. */ void addConstraint(Constraint c) { constraintList = (Constraint[]) ArrayUtil.toAdjustedArray(constraintList, c, constraintList.length, 1); } /** * Adds a constraint. */ void addPKConstraint(Constraint c) { constraintList = (Constraint[]) ArrayUtil.toAdjustedArray(constraintList, c, 0, 1); } /** * Returns the list of constraints. */ Constraint[] getConstraints() { return constraintList; } /** * Returns the primary constraint. */ Constraint getPrimaryConstraint() { return primaryKeyCols.length == 0 ? null : constraintList[0]; }/** @todo fredt - this can be improved to ignore order of columns in * multi-column indexes */ /** * Returns the index supporting a constraint with the given column signature. * Only Unique constraints are considered. */ Index getUniqueConstraintIndexForColumns(int[] col) { if (ArrayUtil.areEqual(getPrimaryIndex().getColumns(), col, col.length, true)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -