📄 table.java
字号:
* plus the next identity value (hidden or user defined). This is used * with CACHED tables. */ String getIndexRoots() { String roots = StringUtil.getList(getIndexRootsArray(), " ", ""); StringBuffer s = new StringBuffer(roots); s.append(' '); s.append(identitySequence.peek()); return s.toString(); } /** * Sets the index roots of a cached/text table to specified file * pointers. If a * file pointer is -1 then the particular index root is null. A null index * root signifies an empty table. Accordingly, all index roots should be * null or all should be a valid file pointer/reference. */ public void setIndexRoots(int[] roots) throws HsqlException { Trace.check(isCached, Trace.TABLE_NOT_FOUND); for (int i = 0; i < getIndexCount(); i++) { int p = roots[i]; Row r = null; if (p != -1) { r = (CachedRow) rowStore.get(p); } Node f = null; if (r != null) { f = r.getNode(i); } indexList[i].setRoot(null, f); } } /** * Sets the index roots and next identity. */ void setIndexRoots(String s) throws HsqlException { // the user may try to set this; this is not only internal problem Trace.check(isCached, Trace.TABLE_NOT_FOUND); Tokenizer t = new Tokenizer(s); int[] roots = new int[getIndexCount()]; for (int i = 0; i < getIndexCount(); i++) { int v = t.getInt(); roots[i] = v; } setIndexRoots(roots); long v = t.getBigint(); identitySequence.reset(v); } /** * Shortcut for creating system table PK's. */ void createPrimaryKey(int[] cols) throws HsqlException { createPrimaryKey(null, cols, false); } /** * Shortcut for creating default PK's. */ void createPrimaryKey() throws HsqlException { createPrimaryKey(null, null, false); } /** * Creates a single or multi-column primary key and index. sets the * colTypes array. Finalises the creation of the table. (fredt@users) */// tony_lai@users 20020820 - patch 595099 void createPrimaryKey(HsqlName indexName, int[] columns, boolean columnsNotNull) throws HsqlException { if (primaryKeyCols != null) { Trace.doAssert(false, "Table.createPrimaryKey(column)"); } if (columns == null) { columns = new int[0]; } else { for (int i = 0; i < columns.length; i++) { if (columnsNotNull) { getColumn(columns[i]).setNullable(false); } getColumn(columns[i]).setPrimaryKey(true); } } primaryKeyCols = columns; colTypes = new int[columnCount]; colDefaults = new Expression[columnCount]; colSizes = new int[columnCount]; colScales = new int[columnCount]; colNullable = new boolean[columnCount]; defaultColumnMap = new int[columnCount]; for (int i = 0; i < columnCount; i++) { setColumnTypeVars(i); } primaryKeyTypes = new int[primaryKeyCols.length]; ArrayUtil.copyColumnValues(colTypes, primaryKeyCols, primaryKeyTypes); primaryKeyColsSequence = new int[primaryKeyCols.length]; ArrayUtil.fillSequence(primaryKeyColsSequence); resetDefaultsFlag(); // tony_lai@users 20020820 - patch 595099 HsqlName name = indexName != null ? indexName : database.nameManager.newAutoName( "IDX"); createPrimaryIndex(columns, name); setBestRowIdentifiers(); } void setColumnTypeVars(int i) { Column column = getColumn(i); colTypes[i] = column.getType(); colSizes[i] = column.getSize(); colScales[i] = column.getScale(); colNullable[i] = column.isNullable(); defaultColumnMap[i] = i; if (column.isIdentity()) { identitySequence.reset(column.identityStart, column.identityIncrement); } colDefaults[i] = column.getDefaultExpression(); } HsqlName makeSysPKName() throws HsqlException { return database.nameManager.newAutoName("PK"); } void createPrimaryIndex(int[] pkcols, HsqlName name) throws HsqlException { int[] pkcoltypes = new int[pkcols.length]; for (int j = 0; j < pkcols.length; j++) { pkcoltypes[j] = colTypes[pkcols[j]]; } Index newindex = new Index(database, name, this, pkcols, pkcoltypes, true, true, true, false, pkcols, pkcoltypes, isTemp); addIndex(newindex); } /** * Create new index taking into account removal or addition of a column * to the table. */ private Index createAdjustedIndex(Index index, int colindex, int adjust) throws HsqlException { int[] indexcolumns = (int[]) ArrayUtil.resizeArray(index.getColumns(), index.getVisibleColumns()); int[] colarr = ArrayUtil.toAdjustedColumnArray(indexcolumns, colindex, adjust); // if a column to remove is one of the Index columns if (colarr.length != index.getVisibleColumns()) { return null; } return createIndexStructure(colarr, index.getName(), index.isUnique(), index.isConstraint, index.isForward); } /** * Create new memory-resident index. For MEMORY and TEXT tables. */ Index createIndex(Session session, int[] column, HsqlName name, boolean unique, boolean constraint, boolean forward) throws HsqlException { int newindexNo = createIndexStructureGetNo(column, name, unique, constraint, forward); Index newindex = indexList[newindexNo]; Index primaryindex = getPrimaryIndex(); RowIterator it = primaryindex.firstRow(session); int rowCount = 0; HsqlException error = null; try { while (it.hasNext()) { Row row = it.next(); Node backnode = row.getNode(newindexNo - 1); Node newnode = Node.newNode(row, newindexNo, this); newnode.nNext = backnode.nNext; backnode.nNext = newnode; // count before inserting rowCount++; newindex.insert(session, row, newindexNo); } return newindex; } catch (java.lang.OutOfMemoryError e) { error = Trace.error(Trace.OUT_OF_MEMORY); } catch (HsqlException e) { error = e; } // backtrack on error // rowCount rows have been modified it = primaryindex.firstRow(session); for (int i = 0; i < rowCount; i++) { Row row = it.next(); Node backnode = row.getNode(0); int j = newindexNo; while (--j > 0) { backnode = backnode.nNext; } backnode.nNext = backnode.nNext.nNext; } indexList = (Index[]) ArrayUtil.toAdjustedArray(indexList, null, newindexNo, -1); setBestRowIdentifiers(); throw error; } /** * Creates the internal structures for an index. */ Index createIndexStructure(int[] columns, HsqlName name, boolean unique, boolean constraint, boolean forward) throws HsqlException { int i = createIndexStructureGetNo(columns, name, unique, constraint, forward); return indexList[i]; } int createIndexStructureGetNo(int[] column, HsqlName name, boolean unique, boolean constraint, boolean forward) throws HsqlException { if (primaryKeyCols == null) { Trace.doAssert(false, "createIndex"); } int s = column.length; int[] col = new int[s]; int[] type = new int[s]; for (int j = 0; j < s; j++) { col[j] = column[j]; type[j] = colTypes[col[j]]; } int[] pkcols = getPrimaryKey(); int[] pktypes = getPrimaryKeyTypes(); Index newindex = new Index(database, name, this, col, type, false, unique, constraint, forward, pkcols, pktypes, isTemp); int indexNo = addIndex(newindex); setBestRowIdentifiers(); return indexNo; } private int addIndex(Index index) { int i = 0; for (; i < indexList.length; i++) { Index current = indexList[i]; int order = index.getIndexOrderValue() - current.getIndexOrderValue(); if (order < 0) { break; } } indexList = (Index[]) ArrayUtil.toAdjustedArray(indexList, index, i, 1); return i; } /** * returns false if the table has to be recreated in order to add / drop * indexes. Only CACHED tables return false. */ boolean isIndexingMutable() { return !isIndexCached(); } /** * Checks for use of a named index in table constraints, * while ignorring a given set of constraints. * @throws HsqlException if index is used in a constraint */ void checkDropIndex(String indexname, HashSet ignore, boolean dropPK) throws HsqlException { Index index = this.getIndex(indexname); if (index == null) { throw Trace.error(Trace.INDEX_NOT_FOUND, indexname); } if (!dropPK && index.equals(getIndex(0))) { throw Trace.error(Trace.DROP_PRIMARY_KEY, indexname); } for (int i = 0, size = constraintList.length; i < size; i++) { Constraint c = constraintList[i]; if (ignore != null && ignore.contains(c)) { continue; } if (c.isIndexFK(index)) { throw Trace.error(Trace.DROP_FK_INDEX, indexname); } if (c.isIndexUnique(index)) { throw Trace.error(Trace.SYSTEM_INDEX, indexname); } } return; } /** * Returns true if the table has any rows at all. */ public boolean isEmpty(Session session) { if (getIndexCount() == 0) { return true; } return getIndex(0).isEmpty(session); } /** * Returns direct mapping array. */ int[] getColumnMap() { return defaultColumnMap; } /** * Returns empty mapping array. */ int[] getNewColumnMap() { return new int[columnCount]; } /** * Returns empty boolean array. */ boolean[] getNewColumnCheckList() { return new boolean[columnCount]; } /** * Returns empty Object array for a new row. */ public Object[] getEmptyRowData() { return new Object[columnCount]; } /** * Returns array for a new row with SQL DEFAULT value for each column n * where exists[n] is false. This provides default values only where * required and avoids evaluating these values where they will be * overwritten. */ Object[] getNewRowData(Session session, boolean[] exists) throws HsqlException { Object[] data = new Object[columnCount]; int i; if (exists != null && hasDefaultValues) { for (i = 0; i < columnCount; i++) { Expression def = colDefaults[i]; if (exists[i] == false && def != null) { data[i] = def.getValue(session, colTypes[i]); } } } return data; } /** * Performs Table structure modification and changes to the index nodes * to remove a given index from a MEMORY or TEXT table. Not for PK index.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -