Skip to content

Commit

Permalink
Create use database from destination create database
Browse files Browse the repository at this point in the history
  • Loading branch information
AMagistroni committed Aug 31, 2021
1 parent 95dedeb commit 6c432ae
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 1 addition & 1 deletion SqlSchemaCompare.Core/DbStructures/DbObjectType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public enum DbObjectType
TableContraint,
Column,
EnableTrigger,
UseDatabase,
Database,
Other
}
}
19 changes: 19 additions & 0 deletions SqlSchemaCompare.Core/TSql/Factory/TSqlDatabaseFactory.cs
Original file line number Diff line number Diff line change
@@ -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()
};
}
}
}
15 changes: 0 additions & 15 deletions SqlSchemaCompare.Core/TSql/Factory/TSqlUseDatabaseFactory.cs

This file was deleted.

10 changes: 5 additions & 5 deletions SqlSchemaCompare.Core/TSql/TSqlParserUpdateListener.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Type> DDLParserRule = new List<Type>()
{ typeof(Cfl_statementContext) };
Expand All @@ -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<DbObject> DbObjects { get; } = new();
public override void ExitAlter_database([NotNull] Alter_databaseContext context)
Expand Down Expand Up @@ -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)
{
Expand Down
8 changes: 4 additions & 4 deletions SqlSchemaCompare.Core/UpdateSchemaManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public string UpdateSchema(IEnumerable<DbObject> sourceObjects, IEnumerable<DbOb
ProcessRole(sourceObjects, destinationObjects, resultProcessDbObject);
ProcessMember(sourceObjects, destinationObjects, resultProcessDbObject);
ProcessSchema(sourceObjects, destinationObjects, Operation.Create, resultProcessDbObject);
ProcessTable(sourceObjects, destinationObjects, resultProcessDbObject);
ProcessGenericDbObject<StoreProcedure>(sourceObjects, destinationObjects, resultProcessDbObject, DbObjectType.StoreProcedure);
ProcessGenericDbObject<Function>(sourceObjects, destinationObjects, resultProcessDbObject, DbObjectType.Function);
ProcessGenericDbObject<View>(sourceObjects, destinationObjects, resultProcessDbObject, DbObjectType.View);
ProcessTrigger(sourceObjects, destinationObjects, resultProcessDbObject);
ProcessTable(sourceObjects, destinationObjects, resultProcessDbObject);
ProcessTrigger(sourceObjects, destinationObjects, resultProcessDbObject);
ProcessDbObjectWithoutAlter<TypeDbObject>(sourceObjects, destinationObjects, resultProcessDbObject, DbObjectType.Type);
ProcdessIndex(sourceObjects, destinationObjects, resultProcessDbObject);
ProcessSchema(sourceObjects, destinationObjects, Operation.Drop, resultProcessDbObject);
Expand All @@ -43,11 +43,11 @@ public string UpdateSchema(IEnumerable<DbObject> sourceObjects, IEnumerable<DbOb
}
else
{
var destinationDb = destinationObjects.OfType<UseDbObject>();
var destinationDb = destinationObjects.OfType<Database>();
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}";
Expand Down
2 changes: 1 addition & 1 deletion SqlSchemaCompare.Test/TSql/IndexTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 4 additions & 0 deletions SqlSchemaCompare.WindowsForm/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6c432ae

Please sign in to comment.