public void ChangeTable(EntityDB.IDatabaseService database, string oldTableName, string newTableName, EJ.DBColumn[] addColumns, EJ.DBColumn[] changed_columns, EJ.DBColumn[] deletedColumns, Func <List <EJ.DBColumn> > getColumnsFunc, IndexInfo[] _indexInfos) { List <EJ.DBColumn> changedColumns = new List <EJ.DBColumn>(changed_columns); oldTableName = oldTableName.ToLower(); newTableName = newTableName.ToLower(); List <IndexInfo> indexInfos = new List <IndexInfo>(_indexInfos); //先判断表明是否更改 if (oldTableName != newTableName) { //更改表名 database.ExecSqlString($"alter table \"{oldTableName}\" RENAME TO \"{newTableName}\""); } var needToDels = checkIfIdxChanged(database, newTableName, indexInfos); foreach (var column in deletedColumns) { ChangeColumnHandler.HandleDelete(database, newTableName, column); } foreach (string delIndexName in needToDels) { dropTableIndex(database, newTableName, delIndexName); } //将取消主键的列放前面处理 if (true) { var column = changedColumns.FirstOrDefault(m => m.BackupChangedProperties["IsPKID"] != null && (bool)m.BackupChangedProperties["IsPKID"].OriginalValue == true); if (column != null && column.IsPKID == false) { changedColumns.Remove(column); changedColumns.Insert(0, column); } } foreach (var column in changedColumns) { ChangeColumnHandler.HandleChange(database, newTableName, column); } foreach (var column in addColumns) { ChangeColumnHandler.HandleNewColumn(database, newTableName, column); } foreach (var config in indexInfos) { createIndex(database, newTableName, config); } }
public void ChangeTable(EntityDB.IDatabaseService database, string oldTableName, string newTableName, EJ.DBColumn[] addColumns, EJ.DBColumn[] changed_columns, EJ.DBColumn[] deletedColumns, Func <List <EJ.DBColumn> > getColumnsFunc , IndexInfo[] indexInfos) { List <EJ.DBColumn> changedColumns = new List <EJ.DBColumn>(changed_columns); oldTableName = oldTableName.ToLower(); newTableName = newTableName.ToLower(); //先判断表明是否更改 if (oldTableName != newTableName) { //更改表名 database.ExecSqlString("EXEC sp_rename '" + oldTableName + "', '" + newTableName + "'"); } //获取那个索引存在了 var existIndexed = checkIfIdxChanged(database, newTableName, indexInfos); dropTableIndex(database, newTableName, existIndexed.ToArray()); foreach (var column in deletedColumns) { ChangeColumnHandler.HandleDelete(database, newTableName, column); } //将取消主键的列放前面处理 if (true) { var column = changedColumns.FirstOrDefault(m => m.BackupChangedProperties["IsPKID"] != null && (bool)m.BackupChangedProperties["IsPKID"].OriginalValue == true); if (column != null && column.IsPKID == false) { changedColumns.Remove(column); changedColumns.Insert(0, column); } } foreach (var column in changedColumns) { ChangeColumnHandler.HandleChange(database, newTableName, column); } foreach (var column in addColumns) { ChangeColumnHandler.HandleNewColumn(database, newTableName, column); } if (indexInfos != null && indexInfos.Length > 0) { foreach (var c in indexInfos) { if (existIndexed.Contains(c)) { continue; } createIndex(database, newTableName, c); } } }