diff --git a/SqlSchemaCompare.Core/DbStructures/UseDbObject.cs b/SqlSchemaCompare.Core/DbStructures/Database.cs similarity index 69% rename from SqlSchemaCompare.Core/DbStructures/UseDbObject.cs rename to SqlSchemaCompare.Core/DbStructures/Database.cs index 8f20912..9ecaf10 100644 --- a/SqlSchemaCompare.Core/DbStructures/UseDbObject.cs +++ b/SqlSchemaCompare.Core/DbStructures/Database.cs @@ -1,7 +1,7 @@ namespace SqlSchemaCompare.Core.DbStructures { - public class UseDbObject : DbObject + public class Database : DbObject { - public override DbObjectType DbObjectType => DbObjectType.UseDatabase; + public override DbObjectType DbObjectType => DbObjectType.Database; } } diff --git a/SqlSchemaCompare.Core/DbStructures/DbObjectType.cs b/SqlSchemaCompare.Core/DbStructures/DbObjectType.cs index ec202af..6300a55 100644 --- a/SqlSchemaCompare.Core/DbStructures/DbObjectType.cs +++ b/SqlSchemaCompare.Core/DbStructures/DbObjectType.cs @@ -16,7 +16,7 @@ public enum DbObjectType TableContraint, Column, EnableTrigger, - UseDatabase, + Database, Other } } diff --git a/SqlSchemaCompare.Core/TSql/Factory/TSqlDatabaseFactory.cs b/SqlSchemaCompare.Core/TSql/Factory/TSqlDatabaseFactory.cs new file mode 100644 index 0000000..f2e9971 --- /dev/null +++ b/SqlSchemaCompare.Core/TSql/Factory/TSqlDatabaseFactory.cs @@ -0,0 +1,19 @@ +using Antlr4.Runtime; +using Antlr4.Runtime.Misc; +using SqlSchemaCompare.Core.Common; +using SqlSchemaCompare.Core.DbStructures; + +namespace SqlSchemaCompare.Core.TSql.Factory +{ + public class TSqlDatabaseFactory : FactoryBase, IFactory + { + public DbObject Create(ParserRuleContext context, ICharStream stream) + { + var databaseContext = context as TSqlParser.Create_databaseContext; + return new Database { + Sql = context.Start.InputStream.GetText(new Interval(context.start.StartIndex, context.stop.StopIndex)), + Name = databaseContext.database.GetText() + }; + } + } +} diff --git a/SqlSchemaCompare.Core/TSql/Factory/TSqlUseDatabaseFactory.cs b/SqlSchemaCompare.Core/TSql/Factory/TSqlUseDatabaseFactory.cs deleted file mode 100644 index 791471b..0000000 --- a/SqlSchemaCompare.Core/TSql/Factory/TSqlUseDatabaseFactory.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Antlr4.Runtime; -using Antlr4.Runtime.Misc; -using SqlSchemaCompare.Core.Common; -using SqlSchemaCompare.Core.DbStructures; - -namespace SqlSchemaCompare.Core.TSql.Factory -{ - public class TSqlUseDatabaseFactory : FactoryBase, IFactory - { - public DbObject Create(ParserRuleContext context, ICharStream stream) - { - return new UseDbObject { Sql = context.Start.InputStream.GetText(new Interval(context.start.StartIndex, context.stop.StopIndex)) }; - } - } -} diff --git a/SqlSchemaCompare.Core/TSql/TSqlParserUpdateListener.cs b/SqlSchemaCompare.Core/TSql/TSqlParserUpdateListener.cs index 01c75c8..e155ab6 100644 --- a/SqlSchemaCompare.Core/TSql/TSqlParserUpdateListener.cs +++ b/SqlSchemaCompare.Core/TSql/TSqlParserUpdateListener.cs @@ -1,5 +1,6 @@ using Antlr4.Runtime; using Antlr4.Runtime.Misc; +using Antlr4.Runtime.Tree; using SqlSchemaCompare.Core.DbStructures; using SqlSchemaCompare.Core.TSql.Factory; using System; @@ -24,7 +25,7 @@ public sealed class TSqlParserUpdateListener : TSqlParserBaseListener private readonly TSqlIndexFactory _indexFactory; private readonly TSqlMemberFactory _memberFactory; private readonly TSqlSimpleDbObjectFactory _simpleDbObjectFactory; - private readonly TSqlUseDatabaseFactory _useDatabaseFactory; + private readonly TSqlDatabaseFactory _databaseFactory; private readonly IList DDLParserRule = new List() { typeof(Cfl_statementContext) }; @@ -43,7 +44,7 @@ public TSqlParserUpdateListener(ICharStream stream) _indexFactory = new TSqlIndexFactory(); _memberFactory = new TSqlMemberFactory(); _simpleDbObjectFactory = new TSqlSimpleDbObjectFactory(); - _useDatabaseFactory = new TSqlUseDatabaseFactory(); + _databaseFactory = new TSqlDatabaseFactory(); } public List DbObjects { get; } = new(); public override void ExitAlter_database([NotNull] Alter_databaseContext context) @@ -140,10 +141,9 @@ public override void ExitCreate_sequence([NotNull] Create_sequenceContext contex { DbObjects.Add(_simpleDbObjectFactory.Create(context, _stream)); } - - public override void ExitUse_statement([NotNull] TSqlParser.Use_statementContext context) + public override void ExitCreate_database([NotNull] TSqlParser.Create_databaseContext context) { - DbObjects.Add(_useDatabaseFactory.Create(context, _stream)); + DbObjects.Add(_databaseFactory.Create(context, _stream)); } private bool ObjectInsideDDL(RuleContext context) { diff --git a/SqlSchemaCompare.Core/UpdateSchemaManager.cs b/SqlSchemaCompare.Core/UpdateSchemaManager.cs index 97d28ed..40b44a3 100644 --- a/SqlSchemaCompare.Core/UpdateSchemaManager.cs +++ b/SqlSchemaCompare.Core/UpdateSchemaManager.cs @@ -28,11 +28,11 @@ public string UpdateSchema(IEnumerable sourceObjects, IEnumerable(sourceObjects, destinationObjects, resultProcessDbObject, DbObjectType.StoreProcedure); ProcessGenericDbObject(sourceObjects, destinationObjects, resultProcessDbObject, DbObjectType.Function); ProcessGenericDbObject(sourceObjects, destinationObjects, resultProcessDbObject, DbObjectType.View); - ProcessTrigger(sourceObjects, destinationObjects, resultProcessDbObject); - ProcessTable(sourceObjects, destinationObjects, resultProcessDbObject); + ProcessTrigger(sourceObjects, destinationObjects, resultProcessDbObject); ProcessDbObjectWithoutAlter(sourceObjects, destinationObjects, resultProcessDbObject, DbObjectType.Type); ProcdessIndex(sourceObjects, destinationObjects, resultProcessDbObject); ProcessSchema(sourceObjects, destinationObjects, Operation.Drop, resultProcessDbObject); @@ -43,11 +43,11 @@ public string UpdateSchema(IEnumerable sourceObjects, IEnumerable(); + var destinationDb = destinationObjects.OfType(); StringBuilder useDb = new(); if (destinationDb.Any()) { - useDb.AppendLine(destinationDb.First().Sql); + useDb.AppendLine(_schemaBuilder.BuildUse(destinationDb.First().Name)); useDb.AppendLine(_schemaBuilder.BuildSeparator()); } return $"{useDb}{resultProcessDbObject.UpdateSchemaStringBuild}"; diff --git a/SqlSchemaCompare.Test/TSql/IndexTest.cs b/SqlSchemaCompare.Test/TSql/IndexTest.cs index 32a2aed..01d820a 100644 --- a/SqlSchemaCompare.Test/TSql/IndexTest.cs +++ b/SqlSchemaCompare.Test/TSql/IndexTest.cs @@ -101,7 +101,7 @@ public void UpdateSchemaDropDbObject() const string origin = ""; const string destination = -@"USE [dbName] +@"CREATE DATABASE [dbName] GO CREATE NONCLUSTERED INDEX [indexName] ON [dbo].[table] diff --git a/SqlSchemaCompare.WindowsForm/MainForm.cs b/SqlSchemaCompare.WindowsForm/MainForm.cs index 3ca36a7..a8b3eca 100644 --- a/SqlSchemaCompare.WindowsForm/MainForm.cs +++ b/SqlSchemaCompare.WindowsForm/MainForm.cs @@ -267,6 +267,10 @@ private void BtnSwapOriginDestination_Click(object sender, EventArgs e) var swap = txtOriginSchema.Text; txtOriginSchema.Text = txtDestinationSchema.Text; txtDestinationSchema.Text = swap; + + formSettings.OriginSchema = txtOriginSchema.Text; + formSettings.DestinationSchema = txtDestinationSchema.Text; + formSettings.Save(); } private void BtnLoadSchema_Click(object sender, EventArgs e)