Skip to content

Commit

Permalink
Refactor update schema manager. Add order by db object
Browse files Browse the repository at this point in the history
  • Loading branch information
AMagistroni committed Sep 2, 2021
1 parent 97d3765 commit 8ae1f67
Show file tree
Hide file tree
Showing 20 changed files with 267 additions and 320 deletions.
3 changes: 1 addition & 2 deletions SqlSchemaCompare.Core/DbStructures/Operation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ public enum Operation
Alter,
Drop,
Enabled,
Disabled,
Add
Disabled
}
}
9 changes: 2 additions & 7 deletions SqlSchemaCompare.Core/DbStructures/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@ public class Column : DbObject {
public override DbObjectType DbObjectType => DbObjectType.Column;
}
public class TableConstraint : DbObject
{
public enum TableConstraintType
{
DefaultWithName,
DefaultWithoutName
}
{
public override DbObjectType DbObjectType => DbObjectType.TableContraint;
public TableConstraintType ConstraintType { get; set; }
public string ColumnNameForeighKey { get; set; }
}
public IList<Column> Columns { get; } = new List<Column>();
public IList<TableConstraint> Constraints { get; } = new List<TableConstraint>();
Expand Down
2 changes: 1 addition & 1 deletion SqlSchemaCompare.Core/TSql/Factory/TSqlTableFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Table.TableConstraint CreateAlterTable(ParserRuleContext context)
Sql = alterTableContext.Start.InputStream.GetText(new Interval(alterTableContext.start.StartIndex, alterTableContext.stop.StopIndex)),
Name = name,
ParentName = tableName,
ConstraintType = name == string.Empty ? Table.TableConstraint.TableConstraintType.DefaultWithoutName : Table.TableConstraint.TableConstraintType.DefaultWithName
ColumnNameForeighKey = alterTableContext.fk?.GetText()
};
}
}
Expand Down
48 changes: 13 additions & 35 deletions SqlSchemaCompare.Core/TSql/TSqlResultProcessDbObject.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,29 @@
using SqlSchemaCompare.Core.DbStructures;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace SqlSchemaCompare.Core.TSql
{
public class TSqlResultProcessDbObject
{

public StringBuilder UpdateSchemaStringBuild { get; set; } = new();
public List<DbObject> ToDrop { get; private set; } = new();
public List<DbObject> ToAlter { get; private set; } = new();
public List<DbObject> ToCreate { get; private set; } = new();
public List<DbObject> ToEnable { get; private set; } = new();
public List<DbObject> ToDisable { get; private set; } = new();

public void AddToAlter<T>(IList<T> dbObjects) where T: DbObject
{
ToAlter.AddRange(dbObjects);
}
public void AddToCreate<T>(IList<T> dbObjects) where T : DbObject
{
ToCreate.AddRange(dbObjects);
}
public void AddToDrop<T>(IList<T> dbObjects) where T : DbObject
public class OperationOnDbObject
{
ToDrop.AddRange(dbObjects);
public DbObject DbObject { get; init; }
public Operation Operation { get; init; }
}

public void AddToAlter<T>(T dbObjects) where T : DbObject
{
ToAlter.Add(dbObjects);
}
public void AddToCreate<T>(T dbObjects) where T : DbObject
public List<OperationOnDbObject> OperationsOnDbObject { get; private set; } = new();
public void AddOperation<T>(DbObject dbObjects, Operation operation) where T : DbObject
{
ToCreate.Add(dbObjects);
OperationsOnDbObject.Add(new OperationOnDbObject { DbObject = dbObjects, Operation = operation } );
}
public void AddToDrop<T>(T dbObjects) where T : DbObject
public void AddOperation<T>(IList<T> dbObjects, Operation operation) where T : DbObject
{
ToDrop.Add(dbObjects);
}
public void AddToEnable<T>(T dbObjects) where T : DbObject
{
ToEnable.Add(dbObjects);
}
public void AddToDisable<T>(T dbObjects) where T : DbObject
dbObjects.ToList().ForEach(x => AddOperation<T>(x, operation));
}

public IEnumerable<DbObject> GetDbObject(DbObjectType dbObjectType, Operation operation)
{
ToDisable.Add(dbObjects);
return OperationsOnDbObject.Where(x => x.DbObject.DbObjectType == dbObjectType && x.Operation == operation).Select(x => x.DbObject);
}
}
}
57 changes: 21 additions & 36 deletions SqlSchemaCompare.Core/TSql/TSqlSchemaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,24 @@ public class TSqlSchemaBuilder : ISchemaBuilder
{
public string Build(DbObject dbObject, Operation operation)
{
switch (dbObject.DbObjectType)
return dbObject.DbObjectType switch
{
case DbObjectType.Table:
return BuildCreateDropTable(dbObject as Table, operation);
case DbObjectType.TableContraint:
return BuildTableConstraint(dbObject as Table.TableConstraint, operation);
case DbObjectType.Column:
return BuildAlterTableElement("COLUMN", dbObject as Table.Column, operation);
case DbObjectType.View:
return BuildView(dbObject as View, operation);
case DbObjectType.StoreProcedure:
return BuildStoreProcedure(dbObject as StoreProcedure, operation);
case DbObjectType.Function:
return BuildFunction(dbObject as Function, operation);
case DbObjectType.Schema:
return BuildCreateDrop(dbObject as Schema, "SCHEMA", operation);
case DbObjectType.Trigger:
return BuildTrigger(dbObject as Trigger, operation);
case DbObjectType.User:
return BuildUser(dbObject as User, operation);
case DbObjectType.Role:
return BuildCreateDrop(dbObject as Role, "ROLE", operation);
case DbObjectType.Type:
return BuildCreateDrop(dbObject as TypeDbObject, "TYPE", operation);
case DbObjectType.Index:
return BuildIndex(dbObject as Index, operation);
case DbObjectType.Member:
return BuildMember(dbObject as Member, operation);
case DbObjectType.EnableTrigger:
return BuildEnableTrigger(dbObject as Trigger.EnabledDbObject, operation);
default:
throw new NotImplementedException();
}
DbObjectType.Table => BuildCreateDropTable(dbObject as Table, operation),
DbObjectType.TableContraint => BuildTableConstraint(dbObject as Table.TableConstraint, operation),
DbObjectType.Column => BuildColumn(dbObject as Table.Column, operation),
DbObjectType.View => BuildView(dbObject as View, operation),
DbObjectType.StoreProcedure => BuildStoreProcedure(dbObject as StoreProcedure, operation),
DbObjectType.Function => BuildFunction(dbObject as Function, operation),
DbObjectType.Schema => BuildCreateDrop(dbObject as Schema, "SCHEMA", operation),
DbObjectType.Trigger => BuildTrigger(dbObject as Trigger, operation),
DbObjectType.User => BuildUser(dbObject as User, operation),
DbObjectType.Role => BuildCreateDrop(dbObject as Role, "ROLE", operation),
DbObjectType.Type => BuildCreateDrop(dbObject as TypeDbObject, "TYPE", operation),
DbObjectType.Index => BuildIndex(dbObject as Index, operation),
DbObjectType.Member => BuildMember(dbObject as Member, operation),
DbObjectType.EnableTrigger => BuildEnableTrigger(dbObject as Trigger.EnabledDbObject, operation),
_ => throw new NotImplementedException(),
};
}

public string GetStartCommentInLine()
Expand Down Expand Up @@ -82,13 +67,13 @@ private string BuildMember(Member member, Operation operation)
};
}

private string BuildAlterTableElement(String itemName, DbObject dbObject, Operation operation)
private string BuildColumn(DbObject dbObject, Operation operation)
{
return operation switch
{
Operation.Create => $"ALTER TABLE {dbObject.ParentName} ADD {itemName} {dbObject.Sql}",
Operation.Drop => $"ALTER TABLE {dbObject.ParentName} DROP {itemName} {dbObject.Name}",
Operation.Alter => $"ALTER TABLE {dbObject.ParentName} ALTER {itemName} {dbObject.Sql}",
Operation.Create => $"ALTER TABLE {dbObject.ParentName} ADD {dbObject.Sql}",
Operation.Drop => $"ALTER TABLE {dbObject.ParentName} DROP COLUMN {dbObject.Name}",
Operation.Alter => $"ALTER TABLE {dbObject.ParentName} ALTER COLUMN {dbObject.Sql}",
_ => throw new NotSupportedException("Alter not supported on schema"),
};
}
Expand Down
Loading

0 comments on commit 8ae1f67

Please sign in to comment.