📄 table.java
字号:
c.core.check = condition; // this workaround is here to stop LIKE optimisation (for proper scripting) condition.setLikeOptimised(); Select s = Expression.getCheckSelect(session, this, condition); c.core.checkFilter = s.tFilter[0]; c.core.checkFilter.setAsCheckFilter(); c.core.mainTable = this; } /** * Used for drop column. */ void checkColumnInCheckConstraint(String colname) throws HsqlException { for (int i = 0, size = constraintList.length; i < size; i++) { Constraint c = constraintList[i]; if (c.constType == Constraint.CHECK) { if (c.hasColumn(this, colname)) { throw Trace.error(Trace.COLUMN_IS_REFERENCED, c.getName()); } } } } /** * Used for retype column. Checks whether column is in an FK or is * referenced by a FK * @param colIndex index */ void checkColumnInFKConstraint(int colIndex) throws HsqlException { for (int i = 0, size = constraintList.length; i < size; i++) { Constraint c = constraintList[i]; if (c.hasColumn(colIndex) && (c.getType() == Constraint.MAIN || c.getType() == Constraint.FOREIGN_KEY)) { throw Trace.error(Trace.COLUMN_IS_REFERENCED, c.getName().name); } } } /** * Used for column defaults and nullability. Checks whether column is in an FK. * @param colIndex index of column * @param refOnly only check FK columns, not referenced columns */ void checkColumnInFKConstraint(int colIndex, int actionType) throws HsqlException { for (int i = 0, size = constraintList.length; i < size; i++) { Constraint c = constraintList[i]; if (c.hasColumn(colIndex)) { if (c.getType() == Constraint.FOREIGN_KEY && (actionType == c.getUpdateAction() || actionType == c.getDeleteAction())) { throw Trace.error(Trace.COLUMN_IS_REFERENCED, c.getName().name); } } } } /** * Used for rename column. */ private void renameColumnInCheckConstraints(String oldname, String newname, boolean isquoted) throws HsqlException { for (int i = 0, size = constraintList.length; i < size; i++) { Constraint c = constraintList[i]; if (c.constType == Constraint.CHECK) { Expression.Collector coll = new Expression.Collector(); coll.addAll(c.core.check, Expression.COLUMN); Iterator it = coll.iterator(); for (; it.hasNext(); ) { Expression e = (Expression) it.next(); if (e.getColumnName() == oldname) { e.setColumnName(newname, isquoted); } } } } } /** * Used for drop column. */ private void renameTableInCheckConstraints(Session session, String oldname, String newname) throws HsqlException { for (int i = 0, size = constraintList.length; i < size; i++) { Constraint c = constraintList[i]; if (c.constType == Constraint.CHECK) { Expression.Collector coll = new Expression.Collector(); coll.addAll(c.core.check, Expression.COLUMN); Iterator it = coll.iterator(); for (; it.hasNext(); ) { Expression e = (Expression) it.next(); if (e.getTableName() == oldname) { e.setTableName(newname); } } } } recompileCheckConstraints(session); } /** * Returns the count of user defined columns. */ public int getColumnCount() { return columnCount; } /** * Returns the count of indexes on this table. */ public int getIndexCount() { return indexList.length; } /** * Returns the identity column or null. */ int getIdentityColumn() { return identityColumn; } /** * Returns the index of given column name or throws if not found */ int getColumnNr(String c) throws HsqlException { int i = findColumn(c); if (i == -1) { throw Trace.error(Trace.COLUMN_NOT_FOUND, c); } return i; } /** * Returns the index of given column name or -1 if not found. */ int findColumn(String c) { int index = columnList.getIndex(c); return index; } /** * Returns the primary index (user defined or system defined) */ public Index getPrimaryIndex() { return getIndex(0); } /** * Return the user defined primary key column indexes, or empty array for system PK's. */ public int[] getPrimaryKey() { return primaryKeyCols; } public int[] getPrimaryKeyTypes() { return primaryKeyTypes; } public boolean hasPrimaryKey() { return !(primaryKeyCols.length == 0); } int[] getBestRowIdentifiers() { return bestRowIdentifierCols; } boolean isBestRowIdentifiersStrict() { return bestRowIdentifierStrict; } /** * This method is called whenever there is a change to table structure and * serves two porposes: (a) to reset the best set of columns that identify * the rows of the table (b) to reset the best index that can be used * to find rows of the table given a column value. * * (a) gives most weight to a primary key index, followed by a unique * address with the lowest count of nullable columns. Otherwise there is * no best row identifier. * * (b) finds for each column an index with a corresponding first column. * It uses any type of visible index and accepts the first one (it doesn't * make any difference to performance). * * bestIndex is the user defined, primary key, the first unique index, or * the first non-unique index. NULL if there is no user-defined index. * */ void setBestRowIdentifiers() { int[] briCols = null; int briColsCount = 0; boolean isStrict = false; int nNullCount = 0; // ignore if called prior to completion of primary key construction if (colNullable == null) { return; } bestIndex = null; bestIndexForColumn = new int[columnList.size()]; ArrayUtil.fillArray(bestIndexForColumn, -1); for (int i = 0; i < indexList.length; i++) { Index index = indexList[i]; int[] cols = index.getColumns(); int colsCount = index.getVisibleColumns(); if (i == 0) { // ignore system primary keys if (hasPrimaryKey()) { isStrict = true; } else { continue; } } if (bestIndexForColumn[cols[0]] == -1) { bestIndexForColumn[cols[0]] = i; } if (!index.isUnique()) { if (bestIndex == null) { bestIndex = index; } continue; } int nnullc = 0; for (int j = 0; j < colsCount; j++) { if (!colNullable[cols[j]]) { nnullc++; } } if (bestIndex != null) { bestIndex = index; } if (nnullc == colsCount) { if (briCols == null || briColsCount != nNullCount || colsCount < briColsCount) { // nothing found before || // found but has null columns || // found but has more columns than this index briCols = cols; briColsCount = colsCount; nNullCount = colsCount; isStrict = true; } continue; } else if (isStrict) { continue; } else if (briCols == null || colsCount < briColsCount || nnullc > nNullCount) { // nothing found before || // found but has more columns than this index|| // found but has fewer not null columns than this index briCols = cols; briColsCount = colsCount; nNullCount = nnullc; } } // remove rowID column from bestRowIdentiferCols bestRowIdentifierCols = briCols == null || briColsCount == briCols.length ? briCols : ArrayUtil .arraySlice(briCols, 0, briColsCount); bestRowIdentifierStrict = isStrict; if (hasPrimaryKey()) { bestIndex = getPrimaryIndex(); } } /** * Sets the SQL default value for a columm. */ void setDefaultExpression(int columnIndex, Expression def) { Column column = getColumn(columnIndex); column.setDefaultExpression(def); colDefaults[columnIndex] = column.getDefaultExpression(); resetDefaultsFlag(); } /** * sets the flag for the presence of any default expression */ void resetDefaultsFlag() { hasDefaultValues = false; for (int i = 0; i < columnCount; i++) { hasDefaultValues = hasDefaultValues || colDefaults[i] != null; } } DataFileCache getCache() { return cache; } /** * Used in TableFilter to get an index for the column. * An index is created automatically for system tables or subqueries. */ Index getIndexForColumn(Session session, int column) { int i = bestIndexForColumn[column]; if (i == -1 && (tableType == Table.SYSTEM_SUBQUERY || tableType == Table.SYSTEM_TABLE)) { try { HsqlName indexName = database.nameManager.newAutoName("IDX"); createIndex(session, new int[]{ column }, indexName, false, false, false); i = bestIndexForColumn[column]; } catch (Exception e) {} } return i == -1 ? null : getIndex(i); } /** * Used for TableFilter to get an index for the columns */ Index getIndexForColumns(boolean[] columnCheck) { Index indexChoice = null; int colCount = 0; for (int i = 0; i < indexList.length; i++) { Index index = indexList[i]; boolean result = ArrayUtil.containsAllTrueElements(columnCheck, index.colCheck); if (result && index.getVisibleColumns() > colCount) { colCount = index.getVisibleColumns(); indexChoice = index; } } return indexChoice; } /** * Finds an existing index for a foreign key column group */ Index getIndexForColumns(int[] col, boolean unique) throws HsqlException { for (int i = 0, count = getIndexCount(); i < count; i++) { Index currentindex = getIndex(i); int[] indexcol = currentindex.getColumns(); if (ArrayUtil.haveEqualArrays(indexcol, col, col.length)) { if (!unique || currentindex.isUnique()) { return currentindex; } } } return null; } /** * Return the list of file pointers to root nodes for this table's * indexes. */ public int[] getIndexRootsArray() { int[] roots = new int[getIndexCount()]; for (int i = 0; i < getIndexCount(); i++) { roots[i] = indexList[i].getRoot(); } return roots; } /** * Returns the string consisting of file pointers to roots of indexes
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -