📄 tableworks.java
字号:
table = tn; } table.database.schemaManager.removeIndexName(indexname, table.getName()); table.database.schemaManager.recompileViews(table); } /** * * @param column * @param colindex * @throws HsqlException */ void retypeColumn(Column column, int colindex) throws HsqlException { 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); 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 pkNameRemove = null; 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(colName); if (table.getPrimaryKey().length == 1 && table.getPrimaryKey()[0] == colindex) { table.checkDropIndex(table.getIndex(0).getName().name, null, true); pkNameRemove = table.getIndex(0).getName(); tn = table.moveDefinitionPK(null, 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); if (constNameRemove != null) { tn.removeConstraint(constNameRemove.name); } 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 (pkNameRemove != null) { table.database.schemaManager.removeConstraintName( pkNameRemove.name, table.getName()); } if (constNameRemove != null) { table.database.schemaManager.removeConstraintName( constNameRemove.name, table.getName()); } } /** * * @param column * @param colindex * @throws HsqlException */ void addColumn(Column column, int colindex) throws HsqlException { HsqlName pkNameAdd = null; 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()) { pkNameAdd = tn.makeSysPKName(); tn = tn.moveDefinitionPK(pkNameAdd, 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 (pkNameAdd != null) { 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 (name.equals(table.getIndexes()[0].getName().name)) { ctype = Constraint.PRIMARY_KEY; } else if (c == null) { throw Trace.error(Trace.CONSTRAINT_NOT_FOUND, Trace.TableWorks_dropConstraint, new Object[] { name, table.getName().name }); } else { ctype = c.getType(); } if (ctype == Constraint.MAIN) { throw Trace.error(Trace.DROP_SYSTEM_CONSTRAINT); } if (ctype == Constraint.PRIMARY_KEY) { addOrDropPrimaryKey(null, null, false); } 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); int refIndex = table.getConstraintIndex(c.getFkName()); 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(oldCol.columnName.name); checkConvertColDataType(oldCol, newCol); retypeColumn(newCol, colindex); } void checkConvertColDataType(Column oldCol, Column newCol) throws HsqlException { int colindex = table.getColumnNr(oldCol.columnName.name); RowIterator it = table.rowIterator(null); while (it.hasNext()) { Row row = it.next(); Object o = row.getData()[colindex]; Column.convertObject(session, o, newCol.getType(), newCol.getSize(), newCol.getScale()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -