📄 tableworks.java
字号:
} table.database.schemaManager.checkColumnIsInView(table, table.getColumn(colindex).columnName.name); table.checkColumnInCheckConstraint( table.getColumn(colindex).columnName.name); int[] dropIndexes = null; Table tn = table.moveDefinition(dropIndexes, column, colindex, 0); tn.moveData(session, table, colindex, 0); tn.updateConstraintsTables(session, table, colindex, 0); int i = table.database.schemaManager.getTableIndex(table); table.database.schemaManager.setTable(i, tn); table = tn; table.database.schemaManager.recompileViews(table); } /** * * @param colIndex * @throws HsqlException */ void dropColumn(int colIndex) throws HsqlException { HsqlName constNameRemove = null; if (table.isText() &&!table.isEmpty(session)) { throw Trace.error(Trace.OPERATION_NOT_SUPPORTED); } table.database.schemaManager.checkColumnIsInView(table, table.getColumn(colIndex).columnName.name); table.checkColumnInCheckConstraint( table.getColumn(colIndex).columnName.name); Table tn = table; int[] dropIndexes = null; String colName = tn.getColumn(colIndex).columnName.name; tn.checkColumnInFKConstraint(colIndex); if (table.getPrimaryKey().length == 1 && table.getPrimaryKey()[0] == colIndex) { table.checkDropIndex(table.getIndex(0).getName().name, null, true); constNameRemove = table.constraintList[0].getName(); tn = table.moveDefinitionPK(null, false); } Constraint c = tn.getUniqueConstraintForColumns(new int[]{ colIndex }); if (c != null) { Index idx = c.getMainIndex(); dropIndexes = new int[]{ tn.getIndexIndex(idx.getName().name) }; constNameRemove = c.getName(); } tn = tn.moveDefinition(dropIndexes, null, colIndex, -1); tn.moveData(session, table, colIndex, -1); tn.updateConstraintsTables(session, table, colIndex, -1); int i = table.database.schemaManager.getTableIndex(table); table.database.schemaManager.setTable(i, tn); table = tn; table.database.schemaManager.recompileViews(table); if (constNameRemove != null) { table.removeConstraint(constNameRemove.name); table.database.schemaManager.removeConstraintName( constNameRemove.name, table.getName()); } } /** * * @param column * @param colIndex * @throws HsqlException */ void addColumn(Column column, int colIndex) throws HsqlException { if (table.isText() &&!table.isEmpty(session)) { throw Trace.error(Trace.OPERATION_NOT_SUPPORTED); } Table tn = table; tn = tn.moveDefinition(null, column, colIndex, 1); if (column.isPrimaryKey()) { tn = tn.moveDefinitionPK(new int[]{ colIndex }, true); } tn.moveData(session, table, colIndex, 1); tn.updateConstraintsTables(session, table, colIndex, 1); int i = table.database.schemaManager.getTableIndex(table); table.database.schemaManager.setTable(i, tn); table = tn; table.database.schemaManager.recompileViews(table); if (column.isPrimaryKey()) { HsqlName pkNameAdd = tn.makeSysPKName(); Constraint newconstraint = new Constraint(pkNameAdd, table, table.getPrimaryIndex(), Constraint.PRIMARY_KEY); table.addConstraint(newconstraint); table.database.schemaManager.registerConstraintName( pkNameAdd.name, table.getName()); } } /** * Drop a named constraint * */ void dropConstraint(String name) throws HsqlException { Constraint c = table.getConstraint(name); int ctype; if (c == null) { throw Trace.error(Trace.CONSTRAINT_NOT_FOUND, Trace.TableWorks_dropConstraint, new Object[] { name, table.getName().name }); } ctype = c.getType(); if (ctype == Constraint.MAIN) { throw Trace.error(Trace.DROP_SYSTEM_CONSTRAINT); } if (ctype == Constraint.PRIMARY_KEY) { addOrDropPrimaryKey(null, false); table.removeConstraint(name); } else if (ctype == Constraint.FOREIGN_KEY) { dropFKConstraint(c); } else if (ctype == Constraint.UNIQUE) { HashSet cset = new HashSet(); cset.add(c); // throw if the index for unique constraint is shared table.checkDropIndex(c.getMainIndex().getName().name, cset, false); // all is well if dropIndex throws for lack of resources dropIndex(c.getMainIndex().getName().name); table.removeConstraint(name); } else if (ctype == Constraint.CHECK) { table.removeConstraint(name); } table.database.schemaManager.removeConstraintName(name, table.getName()); } void dropFKConstraint(Constraint c) throws HsqlException { // drop the reference index, which is automatic and unused elsewhere Index constIndex = c.getRefIndex(); // all is well if dropIndex throws for lack of resources dropIndex(constIndex.getName().name); Table mainTable = c.getMain(); // MAIN constraint was created after REF, so delete first mainTable.removeConstraint(c.getPkName()); table.removeConstraint(c.getFkName()); } void reTypeColumn(Column oldCol, Column newCol) throws HsqlException { boolean notallowed = false; int oldtype = oldCol.getType(); int newtype = newCol.getType(); switch (newtype) { case Types.BINARY : case Types.VARBINARY : case Types.LONGVARBINARY : case Types.OTHER : case Types.JAVA_OBJECT : notallowed = !(newtype == oldtype || table.isEmpty(session)); } switch (oldtype) { case Types.BINARY : case Types.VARBINARY : case Types.LONGVARBINARY : case Types.OTHER : case Types.JAVA_OBJECT : notallowed = !(newtype == oldtype || table.isEmpty(session)); break; case Types.TINYINT : case Types.SMALLINT : case Types.INTEGER : case Types.BIGINT : case Types.REAL : case Types.FLOAT : case Types.DOUBLE : case Types.NUMERIC : case Types.DECIMAL : switch (newtype) { case Types.DATE : case Types.TIME : case Types.TIMESTAMP : notallowed = !table.isEmpty(session); default : } break; case Types.DATE : case Types.TIME : case Types.TIMESTAMP : switch (newtype) { case Types.TINYINT : case Types.SMALLINT : case Types.INTEGER : case Types.BIGINT : case Types.REAL : case Types.FLOAT : case Types.DOUBLE : case Types.NUMERIC : case Types.DECIMAL : notallowed = !table.isEmpty(session); default : } break; } if (notallowed) { throw Trace.error(Trace.INVALID_CONVERSION); } int colIndex = table.getColumnNr(oldCol.columnName.name); if (table.getPrimaryKey().length > 1) { // if there is a multi-column PK, do not change the PK attributes if (newCol.isIdentity()) { throw Trace.error(Trace.SECOND_PRIMARY_KEY); } newCol.setPrimaryKey(oldCol.isPrimaryKey()); if (ArrayUtil.find(table.getPrimaryKey(), colIndex) != -1) { newCol.setNullable(false); } } else if (table.hasPrimaryKey()) { if (oldCol.isPrimaryKey()) { newCol.setPrimaryKey(true); newCol.setNullable(false); } else if (newCol.isPrimaryKey()) { throw Trace.error(Trace.SECOND_PRIMARY_KEY); } } else if (newCol.isPrimaryKey()) { throw Trace.error(Trace.PRIMARY_KEY_NOT_ALLOWED); } // apply and return if only metadata change is required if (newtype == oldtype && oldCol.isNullable() == newCol.isNullable() && oldCol.getScale() == newCol.getScale() && oldCol.isIdentity() == newCol.isIdentity() && oldCol.identityIncrement == newCol.identityIncrement && (oldCol.getSize() == newCol.getSize() || (oldCol.getSize() < newCol.getSize() && (oldtype == Types.VARCHAR || oldtype == Types.DECIMAL || oldtype == Types.NUMERIC)))) { // size of some types may be increased with this command // default expressions can change oldCol.setType(newCol); oldCol.setDefaultExpression(newCol.getDefaultExpression()); table.setColumnTypeVars(colIndex); table.resetDefaultsFlag(); return; } table.database.schemaManager.checkColumnIsInView(table, table.getColumn(colIndex).columnName.name); table.checkColumnInCheckConstraint( table.getColumn(colIndex).columnName.name); table.checkColumnInFKConstraint(colIndex); checkConvertColDataType(oldCol, newCol); retypeColumn(newCol, colIndex); } void checkConvertColDataType(Column oldCol, Column newCol) throws HsqlException { int colIndex = table.getColumnNr(oldCol.columnName.name); RowIterator it = table.getPrimaryIndex().firstRow(session); while (it.hasNext()) { Row row = it.next(); Object o = row.getData()[colIndex]; Column.convertObject(session, o, newCol.getType(), newCol.getSize(), newCol.getScale()); } } /** * performs the work for changing the nullability of a column */ void setColNullability(Column column, boolean nullable) throws HsqlException { int colIndex = table.getColumnNr(column.columnName.name); if (nullable) { if (column.isPrimaryKey()) { throw Trace.error(Trace.TRY_TO_INSERT_NULL); } table.checkColumnInFKConstraint(colIndex, Constraint.SET_NULL); } else { RowIterator it = table.getPrimaryIndex().firstRow(session); while (it.hasNext()) { Row row = it.next(); Object o = row.getData()[colIndex]; if (o == null) { throw Trace.error(Trace.TRY_TO_INSERT_NULL); } } } column.setNullable(nullable); table.setColumnTypeVars(colIndex); } /** * performs the work for changing the default value of a column */ void setColDefaultExpression(int colIndex, Expression def) throws HsqlException { if (def == null) { table.checkColumnInFKConstraint(colIndex, Constraint.SET_DEFAULT); } table.setDefaultExpression(colIndex, def); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -