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

📄 table.java

📁 Java写的含有一个jdbc驱动的小型数据库数据库引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Table.java
 *
 * Copyright (c) 2001, 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 THE REGENTS 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 package is based on HypersonicSQL, originally developed by Thomas Mueller.
 *
 */
package org.hsqldb;

import java.sql.SQLException;
import java.util.Vector;

/**
 * Class declaration
 *
 *
 * @version 1.0.0.1
 */
class Table {
    private String   sName;
    private Vector   vColumn;
    private Vector   vIndex;		 // vIndex(0) is always the primary key index
    private int      iVisibleColumns;    // table may contain a hidden primary key
    private int      iColumnCount;    // inclusive the (maybe hidden) primary key
    private int      iPrimaryKey;
    private boolean  bCached;
    private Database dDatabase;
    private Log      lLog;
    private int      iIndexCount;
    private int      iIdentityColumn;    // -1 means no such row
    private int      iIdentityId;
    private Vector   vConstraint;
    private int      iConstraintCount;
    Cache	     cCache;
    Vector	     vTrigs[];

    /**
     * Constructor declaration
     *
     *
     * @param db
     * @param log
     * @param name
     * @param cached
     */
    Table(Database db, boolean log, String name, boolean cached) {
	dDatabase = db;
	lLog = log ? db.getLog() : null;

	if (cached) {
	    cCache = lLog.cCache;
	    bCached = true;
	}

	sName = name;
	iPrimaryKey = -1;
	iIdentityColumn = -1;
	vColumn = new Vector();
	vIndex = new Vector();
	vConstraint = new Vector();
	vTrigs = new Vector[TriggerDef.numTrigs()];

	for (int vi = 0; vi < TriggerDef.numTrigs(); vi++) {
	    vTrigs[vi] = new Vector();
	}
    }

    /**
     * Method declaration
     *
     *
     * @param c
     */
    void addConstraint(Constraint c) {
	vConstraint.addElement(c);

	iConstraintCount++;
    }

    /**
     * Method declaration
     *
     *
     * @return
     */
    Vector getConstraints() {
	return vConstraint;
    }

    /**
     * Method declaration
     *
     *
     * @param name
     * @param type
     *
     * @throws SQLException
     */
    void addColumn(String name, int type) throws SQLException {
	addColumn(name, type, true, false);
    }

    /**
     * Method declaration
     *
     *
     * @param c
     *
     * @throws SQLException
     */
    void addColumn(Column c) throws SQLException {
	addColumn(c.sName, c.iType, c.isNullable(), c.isIdentity());
    }

    /**
     * Method declaration
     *
     *
     * @param name
     * @param type
     * @param nullable
     * @param identity
     *
     * @throws SQLException
     */
    void addColumn(String name, int type, boolean nullable,
		   boolean identity) throws SQLException {
	if (identity) {
	    Trace.check(type == Column.INTEGER, Trace.WRONG_DATA_TYPE, name);
	    Trace.check(iIdentityColumn == -1, Trace.SECOND_PRIMARY_KEY,
			name);

	    iIdentityColumn = iColumnCount;
	}

	Trace.assert(iPrimaryKey == -1, "Table.addColumn");
	vColumn.addElement(new Column(name, nullable, type, identity));

	iColumnCount++;
    }

    /**
     * Method declaration
     *
     *
     * @param result
     *
     * @throws SQLException
     */
    void addColumns(Result result) throws SQLException {
	for (int i = 0; i < result.getColumnCount(); i++) {
	    addColumn(result.sLabel[i], result.iType[i], true, false);
	}
    }

    /**
     * Method declaration
     *
     *
     * @return
     */
    String getName() {
	return sName;
    }

    /**
     * Method declaration
     *
     *
     * @return
     */
    int getInternalColumnCount() {

	// todo: this is a temporary solution;
	// the the hidden column is not really required
	return iColumnCount;
    }

    /**
     * Method declaration
     *
     *
     * @param withoutindex
     *
     * @return
     *
     * @throws SQLException
     */
    Table moveDefinition(String withoutindex) throws SQLException {
	Table tn = new Table(dDatabase, true, getName(), isCached());

	for (int i = 0; i < getColumnCount(); i++) {
	    tn.addColumn(getColumn(i));
	}

	// todo: there should be nothing special with the primary key!
	if (iVisibleColumns < iColumnCount) {
	    tn.createPrimaryKey();
	} else {
	    tn.createPrimaryKey(getPrimaryIndex().getColumns()[0]);
	}

	Index idx = null;

	while (true) {
	    idx = getNextIndex(idx);

	    if (idx == null) {
		break;
	    }

	    if (withoutindex != null && idx.getName().equals(withoutindex)) {
		continue;
	    }

	    if (idx == getPrimaryIndex()) {
		continue;
	    }

	    tn.createIndex(idx);
	}

	for (int i = 0; i < iConstraintCount; i++) {
	    Constraint c = (Constraint) vConstraint.elementAt(i);

	    c.replaceTable(this, tn);
	}

	tn.vConstraint = vConstraint;

	return tn;
    }

    /**
     * Method declaration
     *
     *
     * @return
     */
    int getColumnCount() {
	return iVisibleColumns;
    }

    /**
     * Method declaration
     *
     *
     * @return
     */
    int getIndexCount() {
	return iIndexCount;
    }

    /**
     * Method declaration
     *
     *
     * @return
     */
    int getIdentityColumn() {
	return iIdentityColumn;
    }

    /**
     * Method declaration
     *
     *
     * @param c
     *
     * @return
     *
     * @throws SQLException
     */
    int getColumnNr(String c) throws SQLException {
	int i = searchColumn(c);

	if (i == -1) {
	    throw Trace.error(Trace.COLUMN_NOT_FOUND, c);
	}

	return i;
    }

    /**
     * Method declaration
     *
     *
     * @param c
     *
     * @return
     */
    int searchColumn(String c) {
	for (int i = 0; i < iColumnCount; i++) {
	    if (c.equals(((Column) vColumn.elementAt(i)).sName)) {
		return i;
	    }
	}

	return -1;
    }

    /**
     * Method declaration
     *
     *
     * @param i
     *
     * @return
     */
    String getColumnName(int i) {
	return getColumn(i).sName;
    }

    /**
     * Method declaration
     *
     *
     * @param i
     *
     * @return
     */
    int getColumnType(int i) {
	return getColumn(i).iType;
    }

    /**
     * Method declaration
     *
     *
     * @param i
     *
     * @return
     */
    boolean getColumnIsNullable(int i) {
	return getColumn(i).isNullable();
    }

    /**
     * Method declaration
     *
     *
     * @return
     *
     * @throws SQLException
     */
    Index getPrimaryIndex() throws SQLException {
	if (iPrimaryKey == -1) {
	    return null;
	}

	return getIndex(0);
    }

    /**
     * Method declaration
     *
     *
     * @param column
     *
     * @return
     *
     * @throws SQLException
     */
    Index getIndexForColumn(int column) throws SQLException {
	for (int i = 0; i < iIndexCount; i++) {
	    Index h = getIndex(i);

	    if (h.getColumns()[0] == column) {
		return h;
	    }
	}

	return null;
    }

    /**
     * Method declaration
     *
     *
     * @param col
     *
     * @return
     *
     * @throws SQLException
     */
    Index getIndexForColumns(int col[]) throws SQLException {
	for (int i = 0; i < iIndexCount; i++) {
	    Index h = getIndex(i);
	    int   icol[] = h.getColumns();
	    int   j = 0;

	    for (; j < col.length; j++) {
		if (j >= icol.length) {
		    break;
		}

		if (icol[j] != col[j]) {
		    break;
		}
	    }

	    if (j == col.length) {
		return h;
	    }
	}

	return null;
    }

    /**
     * Method declaration
     *
     *
     * @return
     *
     * @throws SQLException
     */
    String getIndexRoots() throws SQLException {
	Trace.assert(bCached, "Table.getIndexRootData");

	String s = "";

	for (int i = 0; i < iIndexCount; i++) {
	    Node f = getIndex(i).getRoot();

	    if (f != null) {
		s = s + f.getKey() + " ";
	    } else {
		s = s + "-1 ";
	    }
	}

	s += iIdentityId;

	return s;
    }

    /**
     * Method declaration
     *
     *
     * @param s
     *
     * @throws SQLException
     */
    void setIndexRoots(String s) throws SQLException {

	// the user may try to set this; this is not only internal problem
	Trace.check(bCached, Trace.TABLE_NOT_FOUND);

	int j = 0;

	for (int i = 0; i < iIndexCount; i++) {
	    int n = s.indexOf(' ', j);
	    int p = Integer.parseInt(s.substring(j, n));

	    if (p != -1) {
		Row  r = cCache.getRow(p, this);
		Node f = r.getNode(i);

		getIndex(i).setRoot(f);
	    }

	    j = n + 1;
	}

	iIdentityId = Integer.parseInt(s.substring(j));
    }

    /**
     * Method declaration
     *
     *
     * @param index
     *
     * @return
     */
    Index getNextIndex(Index index) {
	int i = 0;

	if (index != null) {
	    for (; i < iIndexCount && getIndex(i) != index; i++);

	    i++;
	}

	if (i < iIndexCount) {
	    return getIndex(i);
	}

	return null;    // no more indexes
    }

    /**
     * Method declaration
     *
     *
     * @param i
     *
     * @return
     */
    int getType(int i) {
	return getColumn(i).iType;
    }

    /**
     * Method declaration
     *
     *
     * @param column
     *
     * @throws SQLException
     */
    void createPrimaryKey(int column) throws SQLException {
	Trace.assert(iPrimaryKey == -1, "Table.createPrimaryKey(column)");

	iVisibleColumns = iColumnCount;
	iPrimaryKey = column;

	int col[] = {
	    column

⌨️ 快捷键说明

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