final long rowId = tableDef.getRowId();
final SqlJetTableDef alterDef = new SqlJetTableDef(newTableName, null, tableDef.isTemporary(), false, columns,
tableDef.getConstraints(), page, rowId);
final ISqlJetBtreeSchemaTable schemaTable = openSchemaTable(true);
try {
schemaTable.lock();
try {
if (!schemaTable.goToRow(rowId)) {
throw new SqlJetException(SqlJetErrorCode.CORRUPT);
}
final String typeField = schemaTable.getTypeField();
final String nameField = schemaTable.getNameField();
final String tableField = schemaTable.getTableField();
final int pageField = schemaTable.getPageField();
if (null == typeField || !TABLE_TYPE.equals(typeField)) {
throw new SqlJetException(SqlJetErrorCode.CORRUPT);
}
if (null == nameField || !tableName.equals(nameField)) {
throw new SqlJetException(SqlJetErrorCode.CORRUPT);
}
if (null == tableField || !tableName.equals(tableField)) {
throw new SqlJetException(SqlJetErrorCode.CORRUPT);
}
if (0 == pageField || pageField != page) {
throw new SqlJetException(SqlJetErrorCode.CORRUPT);
}
final String alteredSql = getTableAlteredSql(schemaTable.getSqlField(), alterTableDef);
db.getOptions().changeSchemaVersion();
schemaTable.insertRecord(TABLE_TYPE, newTableName, newTableName, page, alteredSql);
if (renameTable && !tableName.equals(newTableName)) {
renameTablesIndices(schemaTable, tableName, newTableName, getAlterTableName(alterTableDef));
}
tableDefs.remove(tableName);
tableDefs.put(newTableName, alterDef);
return alterDef;
} finally {
schemaTable.unlock();
}
} finally {
schemaTable.close();
}
}