Skip to content

Commit

Permalink
Manage index on view
Browse files Browse the repository at this point in the history
  • Loading branch information
AMagistroni committed Sep 24, 2021
1 parent c9be18c commit cfa8267
Show file tree
Hide file tree
Showing 17 changed files with 8,295 additions and 8,081 deletions.
6 changes: 3 additions & 3 deletions SqlSchemaCompare.Core/Common/RelatedDbObjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace SqlSchemaCompare.Core.Common
{
public class RelatedDbObjectsConfiguration
{
public static List<List<DbObjectType>> RelatedDbObjects = new()
private static List<List<DbObjectType>> RelatedDbObjects = new()
{
new List<DbObjectType> { DbObjectType.Function },
new List<DbObjectType> { DbObjectType.StoreProcedure },
new List<DbObjectType> { DbObjectType.Table, DbObjectType.TableDefaultContraint, DbObjectType.TableForeignKeyContraint, DbObjectType.TablePrimaryKeyContraint, DbObjectType.Column, DbObjectType.Index },
new List<DbObjectType> { DbObjectType.Table, DbObjectType.TableDefaultContraint, DbObjectType.TableForeignKeyContraint, DbObjectType.TablePrimaryKeyContraint, DbObjectType.Column, DbObjectType.Index, DbObjectType.TableSet },
new List<DbObjectType> { DbObjectType.User, DbObjectType.Role, DbObjectType.Member },
new List<DbObjectType> { DbObjectType.View },
new List<DbObjectType> { DbObjectType.View, DbObjectType.Index },
new List<DbObjectType> { DbObjectType.Schema },
new List<DbObjectType> { DbObjectType.Trigger, DbObjectType.EnableTrigger },
new List<DbObjectType> { DbObjectType.Type },
Expand Down
1 change: 1 addition & 0 deletions SqlSchemaCompare.Core/DbStructures/DbObjectType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public enum DbObjectType
TableDefaultContraint,
TableForeignKeyContraint,
TablePrimaryKeyContraint,
TableSet,
Column,
EnableTrigger,
Database,
Expand Down
7 changes: 7 additions & 0 deletions SqlSchemaCompare.Core/DbStructures/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,18 @@ public class TablePrimaryKeyConstraint : TableConstraint
public override DbObjectType DbObjectType => DbObjectType.TablePrimaryKeyContraint;
}

public class TableSet : DbObject
{
public override DbObjectType DbObjectType => DbObjectType.TableSet;
}

public IList<Column> Columns { get; } = new List<Column>();
public IList<TableConstraint> Constraints { get; } = new List<TableConstraint>();
public IList<TableSet> TableSetList { get; } = new List<TableSet>();
public IList<Index> Indexes { get; } = new List<Index>();
public void AdColumns(Column Colum) => Columns.Add(Colum);
public void AddConstraint(TableConstraint tableConstraint) => Constraints.Add(tableConstraint);
public void AddSet(TableSet tableSet) => TableSetList.Add(tableSet);
public void AddIndex(Index index) => Indexes.Add(index);
public bool PrimaryKeyDefinedInsideCreateTable { get; set; }
}
Expand Down
6 changes: 5 additions & 1 deletion SqlSchemaCompare.Core/DbStructures/View.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
namespace SqlSchemaCompare.Core.DbStructures
using System.Collections.Generic;

namespace SqlSchemaCompare.Core.DbStructures
{
public class View: DbObject
{
public override DbObjectType DbObjectType => DbObjectType.View;
public string Body { get; init; }
public IList<Index> Indexes { get; } = new List<Index>();
public void AddIndex(Index index) => Indexes.Add(index);
}
}
13 changes: 12 additions & 1 deletion SqlSchemaCompare.Core/TSql/Factory/FactoryBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using SqlSchemaCompare.Core.DbStructures;
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using SqlSchemaCompare.Core.DbStructures;
using System;

namespace SqlSchemaCompare.Core.TSql.Factory
Expand All @@ -14,5 +16,14 @@ protected Operation GetOperation(string operation)
return Operation.Alter;
else throw new NotImplementedException();
}

protected string GetSqlWithoutGOStatement(ParserRuleContext context, ICharStream stream)
{
var sql = stream.GetText(new Interval(context.start.StartIndex, context.stop.StopIndex)).Trim();
if (sql.ToUpper().EndsWith("GO"))
sql = sql[0..^2].Trim();

return sql;
}
}
}
3 changes: 1 addition & 2 deletions SqlSchemaCompare.Core/TSql/Factory/TSqlFunctionFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using SqlSchemaCompare.Core.Common;
using SqlSchemaCompare.Core.DbStructures;

Expand All @@ -12,7 +11,7 @@ public DbObject Create(ParserRuleContext context, ICharStream stream)
var functionContext = context as TSqlParser.Create_or_alter_functionContext;
return new Function()
{
Sql = stream.GetText(new Interval(functionContext.start.StartIndex, functionContext.stop.StopIndex)),
Sql = GetSqlWithoutGOStatement(context, stream),
Name = functionContext.func_proc_name_schema().procedure.GetText(),
Schema = functionContext.func_proc_name_schema().schema.GetText(),
Operation = GetOperation(functionContext.GetChild(0).GetText())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using SqlSchemaCompare.Core.Common;
using SqlSchemaCompare.Core.DbStructures;

Expand All @@ -12,7 +11,7 @@ public DbObject Create(ParserRuleContext context, ICharStream stream)
var storeProcedureContext = context as TSqlParser.Create_or_alter_procedureContext;
return new StoreProcedure()
{
Sql = stream.GetText(new Interval(storeProcedureContext.start.StartIndex, storeProcedureContext.stop.StopIndex)),
Sql = GetSqlWithoutGOStatement(context, stream),
Name = storeProcedureContext.func_proc_name_schema().procedure.GetText(),
Schema = storeProcedureContext.func_proc_name_schema().schema.GetText(),
Operation = GetOperation(storeProcedureContext.GetChild(0).GetText())
Expand Down
44 changes: 33 additions & 11 deletions SqlSchemaCompare.Core/TSql/Factory/TSqlTableFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using static SqlSchemaCompare.Core.DbStructures.Table;

namespace SqlSchemaCompare.Core.TSql.Factory
{
Expand Down Expand Up @@ -63,17 +64,26 @@ private Table.TablePrimaryKeyConstraint CreatePrimaryKeyConstraint(TSqlParser.Ta
};
}

internal TableConstraint CreateAlterTable(ParserRuleContext context)
internal DbObject CreateAlterTable(ParserRuleContext context)
{
var alterTableContext = context as TSqlParser.Alter_tableContext;

if ((alterTableContext.fk != null) || (alterTableContext.constraint != null))
{
return CreateForeignKeyConstraint(alterTableContext);
}
else if (alterTableContext.column_def_table_constraints() != null) {
return CreateDefaultConstraint(alterTableContext);
else if (alterTableContext.SET() is not null)
{
return new TableSet
{
Sql = alterTableContext.Start.InputStream.GetText(new Interval(alterTableContext.start.StartIndex, alterTableContext.stop.StopIndex)),
Name = alterTableContext.constraint != null ? alterTableContext.constraint.GetText() : default,
ParentName = alterTableContext.children[2].GetText(),
};
}
else if (alterTableContext.column_def_table_constraints() != null || alterTableContext.CHECK() != null || alterTableContext.NOCHECK() != null) {
return CreateDefaultConstraint(alterTableContext);
}

throw new NotImplementedException();
}
Expand All @@ -91,15 +101,27 @@ private Table.TableForeignKeyConstraint CreateForeignKeyConstraint(TSqlParser.Al

private Table.TableDefaultConstraint CreateDefaultConstraint(TSqlParser.Alter_tableContext alterTableContext)
{
var constraint = ((TSqlParser.Column_def_table_constraintContext)alterTableContext.column_def_table_constraints().children[0]).table_constraint();
return new Table.TableDefaultConstraint
if (alterTableContext.column_def_table_constraints() is not null)
{
Sql = alterTableContext.Start.InputStream.GetText(new Interval(alterTableContext.start.StartIndex, alterTableContext.stop.StopIndex)),
Name = constraint.CONSTRAINT() != null ? constraint.id_()[0].GetText() : string.Empty,
ParentName = alterTableContext.children[2].GetText(),
ColumnNames = new List<string> { constraint.forColumn.GetText() },
Value = constraint.DEFAULT() != null ? constraint.default_value_column.GetText() : string.Empty
};
var constraint = ((TSqlParser.Column_def_table_constraintContext)alterTableContext.column_def_table_constraints().children[0]).table_constraint();
return new Table.TableDefaultConstraint
{
Sql = alterTableContext.Start.InputStream.GetText(new Interval(alterTableContext.start.StartIndex, alterTableContext.stop.StopIndex)),
Name = constraint.CONSTRAINT() != null ? constraint.id_()[0].GetText() : string.Empty,
ParentName = alterTableContext.children[2].GetText(),
ColumnNames = new List<string> { constraint.forColumn.GetText() },
Value = constraint.DEFAULT() != null ? constraint.default_value_column.GetText() : string.Empty
};
}
else
{
return new Table.TableDefaultConstraint
{
Sql = alterTableContext.Start.InputStream.GetText(new Interval(alterTableContext.start.StartIndex, alterTableContext.stop.StopIndex)),
ParentName = alterTableContext.children[2].GetText(),
Value = alterTableContext.search_condition().GetText()
};
}
}
}
}
2 changes: 1 addition & 1 deletion SqlSchemaCompare.Core/TSql/Factory/TSqlTriggerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public DbObject Create(ParserRuleContext context, ICharStream stream)
var triggerContext = context as TSqlParser.Create_or_alter_triggerContext;
return new Trigger()
{
Sql = stream.GetText(new Interval(triggerContext.start.StartIndex, triggerContext.stop.StopIndex)),
Sql = GetSqlWithoutGOStatement(context, stream),
Name = triggerContext.create_or_alter_ddl_trigger().simple_name().GetText(),
Schema = string.Empty,
Operation = GetOperation(triggerContext.GetChild(0).GetChild(0).GetText())
Expand Down
4 changes: 1 addition & 3 deletions SqlSchemaCompare.Core/TSql/Factory/TSqlViewFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ public DbObject Create(ParserRuleContext context, ICharStream stream)
{
return new View()
{
Sql = viewContext.stop.Text == "GO"
? stream.GetText(new Interval(viewContext.start.StartIndex, viewContext.stop.StopIndex - 4))
: stream.GetText(new Interval(viewContext.start.StartIndex, viewContext.stop.StopIndex)),
Sql = GetSqlWithoutGOStatement(context, stream),
Name = viewContext.simple_name().name.GetText(),
Schema = viewContext.simple_name().schema.GetText(),
Body = stream.GetText(new Interval(bodyContext.start.StartIndex, bodyContext.stop.StopIndex)),
Expand Down
Loading

0 comments on commit cfa8267

Please sign in to comment.