Skip to content

Commit

Permalink
Drop and create related foreign key when drop and create primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
AMagistroni committed Oct 4, 2021
1 parent 42d5361 commit f22f876
Show file tree
Hide file tree
Showing 30 changed files with 4,065 additions and 3,975 deletions.
4 changes: 2 additions & 2 deletions SqlSchemaCompare.Core/CaseChangingCharStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace SqlSchemaCompare.Core
{
public class CaseChangingCharStream : ICharStream
{
private ICharStream stream;
private bool upper;
private readonly ICharStream stream;
private readonly bool upper;

/// <summary>
/// Constructs a new CaseChangingCharStream wrapping the given <paramref name="stream"/> forcing
Expand Down
17 changes: 9 additions & 8 deletions SqlSchemaCompare.Core/DbStructures/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class TableDefaultConstraint : TableConstraint
public class TableForeignKeyConstraint : TableConstraint
{
public override DbObjectType DbObjectType => DbObjectType.TableForeignKeyContraint;
public string TableIdentifierPrimaryKey { get; set; }
}

public class TablePrimaryKeyConstraint : TableConstraint
Expand All @@ -43,15 +44,15 @@ public class TableSet : DbObject
public void AddIndex(Index index) => Indexes.Add(index);
public bool PrimaryKeyDefinedInsideCreateTable { get; set; }
}
}

public abstract class TableConstraint : DbObject
{
public Table Table { get; set; }
public IEnumerable<string> ColumnNames { get; init; }

public void SetTable(Table table)
public abstract class TableConstraint : DbObject
{
Table = table;
public Table Table { get; set; }
public IEnumerable<string> ColumnNames { get; init; }

public void SetTable(Table table)
{
Table = table;
}
}
}
4 changes: 2 additions & 2 deletions SqlSchemaCompare.Core/TSql/Factory/FactoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ protected Operation GetOperation(string operation)
return Operation.Create;
else if (operation == "ALTER")
return Operation.Alter;
else throw new NotImplementedException();
else throw new NotSupportedException();
}

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"))
if (sql.EndsWith("GO", StringComparison.OrdinalIgnoreCase))
sql = sql[0..^2].Trim();

return sql;
Expand Down
2 changes: 1 addition & 1 deletion SqlSchemaCompare.Core/TSql/Factory/TSqlDatabaseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class TSqlDatabaseFactory : FactoryBase, IFactory
public DbObject Create(ParserRuleContext context, ICharStream stream)
{
var databaseContext = context as TSqlParser.Create_databaseContext;
return new Database {
return new Database {
Sql = context.Start.InputStream.GetText(new Interval(context.start.StartIndex, context.stop.StopIndex)),
Name = databaseContext.database.GetText()
};
Expand Down
31 changes: 16 additions & 15 deletions SqlSchemaCompare.Core/TSql/Factory/TSqlTableFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public DbObject Create(ParserRuleContext context, ICharStream stream)
return table;
}

private Table.Column CreateColumn(TSqlParser.Column_def_table_constraintContext columnTree, Table table)
private Column CreateColumn(TSqlParser.Column_def_table_constraintContext columnTree, Table table)
{
var columnDefinition = columnTree.column_definition();

return new Table.Column()
return new Column()
{
Sql = columnTree.Start.InputStream.GetText(new Interval(columnTree.start.StartIndex, columnTree.stop.StopIndex)),
Name = columnDefinition.id_()[0].GetText(),
Expand All @@ -52,9 +52,9 @@ private Table.Column CreateColumn(TSqlParser.Column_def_table_constraintContext
};
}

private Table.TablePrimaryKeyConstraint CreatePrimaryKeyConstraint(TSqlParser.Table_constraintContext constraintContext, ICharStream stream, Table table)
private TablePrimaryKeyConstraint CreatePrimaryKeyConstraint(TSqlParser.Table_constraintContext constraintContext, ICharStream stream, Table table)
{
return new Table.TablePrimaryKeyConstraint
return new TablePrimaryKeyConstraint
{
Sql = stream.GetText(new Interval(constraintContext.start.StartIndex, constraintContext.stop.StopIndex)),
Name = constraintContext.constraint?.GetText(),
Expand All @@ -77,34 +77,35 @@ internal DbObject CreateAlterTable(ParserRuleContext context)
return new TableSet
{
Sql = alterTableContext.Start.InputStream.GetText(new Interval(alterTableContext.start.StartIndex, alterTableContext.stop.StopIndex)),
Name = alterTableContext.constraint != null ? alterTableContext.constraint.GetText() : default,
Name = (alterTableContext.constraint?.GetText()),
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();
throw new NotSupportedException();
}

private Table.TableForeignKeyConstraint CreateForeignKeyConstraint(TSqlParser.Alter_tableContext alterTableContext)
{
return new Table.TableForeignKeyConstraint
private TableForeignKeyConstraint CreateForeignKeyConstraint(TSqlParser.Alter_tableContext alterTableContext)
{
return new TableForeignKeyConstraint
{
Sql = alterTableContext.Start.InputStream.GetText(new Interval(alterTableContext.start.StartIndex, alterTableContext.stop.StopIndex)),
Name = alterTableContext.constraint != null ? alterTableContext.constraint.GetText() : default,
Name = (alterTableContext.constraint?.GetText()),
ParentName = alterTableContext.children[2].GetText(),
ColumnNames = new List<string> { alterTableContext.fk?.GetText() }
ColumnNames = new List<string> { alterTableContext.fk?.GetText() },
TableIdentifierPrimaryKey = alterTableContext.REFERENCES() != null ? alterTableContext.table_name()[1].GetText() : string.Empty
};
}

private Table.TableDefaultConstraint CreateDefaultConstraint(TSqlParser.Alter_tableContext alterTableContext)
private TableDefaultConstraint CreateDefaultConstraint(TSqlParser.Alter_tableContext alterTableContext)
{
if (alterTableContext.column_def_table_constraints() is not null)
{
var constraint = ((TSqlParser.Column_def_table_constraintContext)alterTableContext.column_def_table_constraints().children[0]).table_constraint();
return new Table.TableDefaultConstraint
return new TableDefaultConstraint
{
Sql = alterTableContext.Start.InputStream.GetText(new Interval(alterTableContext.start.StartIndex, alterTableContext.stop.StopIndex)),
Name = constraint.CONSTRAINT() != null ? constraint.id_()[0].GetText() : string.Empty,
Expand All @@ -115,7 +116,7 @@ private Table.TableDefaultConstraint CreateDefaultConstraint(TSqlParser.Alter_ta
}
else
{
return new Table.TableDefaultConstraint
return new TableDefaultConstraint
{
Sql = alterTableContext.Start.InputStream.GetText(new Interval(alterTableContext.start.StartIndex, alterTableContext.stop.StopIndex)),
ParentName = alterTableContext.children[2].GetText(),
Expand Down
3 changes: 1 addition & 2 deletions SqlSchemaCompare.Core/TSql/Factory/TSqlTypeCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Antlr4.Runtime.Misc;
using SqlSchemaCompare.Core.Common;
using SqlSchemaCompare.Core.DbStructures;
using System.Collections.Generic;

namespace SqlSchemaCompare.Core.TSql.Factory
{
Expand All @@ -11,7 +10,7 @@ public class TSqlTypeCreator : FactoryBase, IFactory
public DbObject Create(ParserRuleContext context, ICharStream stream)
{
var contextTable = context as TSqlParser.Create_typeContext;

return new TypeDbObject
{
Sql = stream.GetText(new Interval(context.start.StartIndex, context.stop.StopIndex)),
Expand Down
11 changes: 4 additions & 7 deletions SqlSchemaCompare.Core/TSql/Factory/TSqlViewFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,16 @@ public DbObject Create(ParserRuleContext context, ICharStream stream)
{
var viewContext = context as TSqlParser.Create_viewContext;
var bodyContext = viewContext.select_statement_standalone();
if (bodyContext.start.StartIndex < bodyContext.stop.StopIndex)
{
return new View()
return bodyContext.start.StartIndex < bodyContext.stop.StopIndex
? new View()
{
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)),
Operation = GetOperation(viewContext.GetChild(0).GetText())
};
}
else
return null;
}
: null;
}
}
}
Loading

0 comments on commit f22f876

Please sign in to comment.