📄 table.java
字号:
return getPrimaryIndex(); } for (int i = 0, size = constraintList.length; i < size; i++) { Constraint c = constraintList[i]; if (c.getType() != Constraint.UNIQUE) { continue; } if (ArrayUtil.areEqual(c.getMainColumns(), col, col.length, true)) { return c.getMainIndex(); } } return null; } /** * Returns any foreign key constraint equivalent to the column sets */ Constraint getConstraintForColumns(Table tablemain, int[] colmain, int[] colref) { for (int i = 0, size = constraintList.length; i < size; i++) { Constraint c = constraintList[i]; if (c.isEquivalent(tablemain, colmain, this, colref)) { return c; } } return null; } /** * Returns any unique constraint equivalent to the column set */ Constraint getUniqueConstraintForColumns(int[] cols) { for (int i = 0, size = constraintList.length; i < size; i++) { Constraint c = constraintList[i]; if (c.isEquivalent(cols, Constraint.UNIQUE)) { return c; } } return null; } /** * Returns any unique Constraint using this index * * @param index * @return */ Constraint getUniqueOrPKConstraintForIndex(Index index) { for (int i = 0, size = constraintList.length; i < size; i++) { Constraint c = constraintList[i]; if (c.getMainIndex() == index && (c.getType() == Constraint.UNIQUE || c.getType() == Constraint.PRIMARY_KEY)) { return c; } } return null; } /** * Returns the next constraint of a given type * * @param from * @param type * @return */ int getNextConstraintIndex(int from, int type) { for (int i = from, size = constraintList.length; i < size; i++) { Constraint c = constraintList[i]; if (c.getType() == type) { return i; } } return -1; }// fredt@users 20020220 - patch 475199 - duplicate column /** * Performs the table level checks and adds a column to the table at the * DDL level. Only used at table creation, not at alter column. */ void addColumn(Column column) throws HsqlException { if (findColumn(column.columnName.name) >= 0) { throw Trace.error(Trace.COLUMN_ALREADY_EXISTS); } if (column.isIdentity()) { Trace.check( column.getType() == Types.INTEGER || column.getType() == Types.BIGINT, Trace.WRONG_DATA_TYPE, column.columnName.name); Trace.check(identityColumn == -1, Trace.SECOND_PRIMARY_KEY, column.columnName.name); identityColumn = columnCount; } if (primaryKeyCols != null) { Trace.doAssert(false, "Table.addColumn"); } columnList.add(column.columnName.name, column); columnCount++; } /** * Add a set of columns based on a ResultMetaData */ void addColumns(Result.ResultMetaData metadata, int count) throws HsqlException { for (int i = 0; i < count; i++) { Column column = new Column( database.nameManager.newHsqlName( metadata.colLabels[i], metadata.isLabelQuoted[i]), true, metadata.colTypes[i], metadata.colSizes[i], metadata.colScales[i], false, null); addColumn(column); } } /** * Adds a set of columns based on a compiled Select */ void addColumns(Select select) throws HsqlException { int colCount = select.iResultLen; for (int i = 0; i < colCount; i++) { Expression e = select.exprColumns[i]; Column column = new Column( database.nameManager.newHsqlName( e.getAlias(), e.isAliasQuoted()), true, e.getDataType(), e.getColumnSize(), e.getColumnScale(), false, null); addColumn(column); } } /** * Returns the HsqlName object fo the table */ public HsqlName getName() { return tableName; } public int getId() { return tableName.hashCode(); } /** * Changes table name. Used by 'alter table rename to'. * Essential to use the existing HsqlName as this is is referenced by * intances of Constraint etc. */ void rename(Session session, String newname, boolean isquoted) throws HsqlException { String oldname = tableName.name; tableName.rename(newname, isquoted); if (HsqlName.isReservedIndexName(getPrimaryIndex().getName().name)) { getPrimaryIndex().getName().rename("SYS_PK", newname, isquoted); } renameTableInCheckConstraints(session, oldname, newname); } /** * Returns total column counts, including hidden ones. */ int getInternalColumnCount() { return columnCount; } /** * returns a basic duplicate of the table without the data structures. */ protected Table duplicate() throws HsqlException { Table t = (new Table(database, tableName, tableType)); return t; } /** * Match two columns arrays for length and type of columns * * @param col column array from this Table * @param other the other Table object * @param othercol column array from the other Table * @throws HsqlException if there is a mismatch */ void checkColumnsMatch(int[] col, Table other, int[] othercol) throws HsqlException { if (col.length != othercol.length) { throw Trace.error(Trace.COLUMN_COUNT_DOES_NOT_MATCH); } for (int i = 0; i < col.length; i++) { // integrity check - should not throw in normal operation if (col[i] >= columnCount || othercol[i] >= other.columnCount) { throw Trace.error(Trace.COLUMN_COUNT_DOES_NOT_MATCH); } if (getColumn(col[i]).getType() != other.getColumn(othercol[i]).getType()) { throw Trace.error(Trace.COLUMN_TYPE_MISMATCH); } } }// fredt@users 20020405 - patch 1.7.0 by fredt - DROP and CREATE INDEX bug /** * Constraints that need removing are removed outside this method.<br> * removeIndex is the index of an index to be removed, in which case * no change is made to columns <br> * When withoutindex is null, adjust {-1 | 0 | +1} indicates if a * column is {removed | replaced | added} * */ Table moveDefinition(int[] removeIndex, Column newColumn, int colIndex, int adjust) throws HsqlException { Table tn = duplicate(); // loop beyond the end in order to be able to add a column to the end // of the list for (int i = 0; i < columnCount + 1; i++) { if (i == colIndex) { if (adjust == 0) { if (newColumn != null) { tn.addColumn(newColumn); continue; } } else if (adjust > 0) { tn.addColumn(newColumn); } else if (adjust < 0) { continue; } } if (i == columnCount) { break; } tn.addColumn(getColumn(i)); } // treat it the same as new table creation and int[] primarykey = primaryKeyCols.length == 0 ? null : primaryKeyCols; if (primarykey != null) { int[] newpk = ArrayUtil.toAdjustedColumnArray(primarykey, colIndex, adjust); if (primarykey.length != newpk.length) { throw Trace.error(Trace.DROP_PRIMARY_KEY); } else { primarykey = newpk; } } tn.createPrimaryKey(getIndex(0).getName(), primarykey, false); tn.constraintList = constraintList; Index idx = null; if (removeIndex != null) { idx = getIndex(removeIndex, colIndex); } if (idx != null) { if (idx.isConstraint()) { throw Trace.error(Trace.COLUMN_IS_IN_CONSTRAINT); } else { throw Trace.error(Trace.COLUMN_IS_IN_INDEX); } } for (int i = 1; i < indexList.length; i++) { if (removeIndex != null && ArrayUtil.find(removeIndex, i) != -1) { continue; } tn.createAdjustedIndex(indexList[i], colIndex, adjust); } tn.triggerLists = triggerLists; return tn; } Index getIndex(int[] exclude, int colIndex) { for (int i = 1; i < indexList.length; i++) { if (exclude != null && ArrayUtil.find(exclude, i) != -1) { continue; } Index idx = indexList[i]; int[] cols = idx.getColumns(); if (ArrayUtil.find(cols, colIndex) != -1) { return idx; } } return null; } private void copyIndexes(Table tn, int removeIndex, int colIndex, int adjust) throws HsqlException { for (int i = 1; i < getIndexCount(); i++) { Index idx = indexList[i]; if (removeIndex == i) { continue; } Index newidx = tn.createAdjustedIndex(idx, colIndex, adjust); if (newidx == null) { // column to remove is part of an index throw Trace.error(Trace.COLUMN_IS_IN_INDEX); } } } /** * cols == null means drop */ Table moveDefinitionPK(int[] pkCols, boolean withIdentity) throws HsqlException { // some checks if ((hasPrimaryKey() && pkCols != null) || (!hasPrimaryKey() && pkCols == null)) { throw Trace.error(Trace.DROP_PRIMARY_KEY); } Table tn = duplicate(); for (int i = 0; i < columnCount; i++) { tn.addColumn(getColumn(i).duplicate(withIdentity)); } tn.createPrimaryKey(getIndex(0).getName(), pkCols, true); tn.constraintList = constraintList; for (int i = 1; i < getIndexCount(); i++) { Index idx = getIndex(i); tn.createAdjustedIndex(idx, -1, 0); } tn.triggerLists = triggerLists; return tn; } /** * Updates the constraint and replaces references to the old table with * the new one, adjusting column index arrays by the given amount. */ void updateConstraintsTables(Session session, Table old, int colindex, int adjust) throws HsqlException { for (int i = 0, size = constraintList.length; i < size; i++) { Constraint c = constraintList[i]; c.replaceTable(old, this, colindex, adjust); if (c.constType == Constraint.CHECK) { recompileCheckConstraint(session, c); } } } private void recompileCheckConstraints(Session session) throws HsqlException { for (int i = 0, size = constraintList.length; i < size; i++) { Constraint c = constraintList[i]; if (c.constType == Constraint.CHECK) { recompileCheckConstraint(session, c); } } } /** * Used after adding columns or indexes to the table. */ private void recompileCheckConstraint(Session session, Constraint c) throws HsqlException { String ddl = c.core.check.getDDL(); Tokenizer tokenizer = new Tokenizer(ddl); Parser parser = new Parser(session, database, tokenizer); Expression condition = parser.parseExpression();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -