Skip to content

Commit

Permalink
Merge v1 into master (v2)
Browse files Browse the repository at this point in the history
Ignored v1 nuspec changes.

* origin/v1:
  version bump v1.9.0
  make transaction optional
  tests - don't drop non-existent db in setup
  removed unused 'using's
  code-doc - IDbHandler
  code-doc - remove misleading info about defaults
  code formatting
  typo fix - description sql parameter

Conflicts:
	ef-enum-to-lookup.nuspec
  • Loading branch information
timabell committed Aug 18, 2015
2 parents 05c7bc6 + 820e1c1 commit 4b5e905
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 14 deletions.
9 changes: 7 additions & 2 deletions EfEnumToLookup/LookupGenerator/EnumToLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

/// <summary>
/// Makes up for a missing feature in Entity Framework 6.1
Expand All @@ -33,6 +31,7 @@ public EnumToLookup()
NameFieldLength = 255;
TableNamePrefix = "Enum_";
_enumParser = new EnumParser { SplitWords = true };
UseTransaction = true;
}

/// <summary>
Expand Down Expand Up @@ -65,6 +64,11 @@ public bool SplitWords
/// </summary>
public string TableNameSuffix { get; set; }

/// <summary>
/// Whether to run the changes inside a database transaction.
/// </summary>
public bool UseTransaction { get; set; }

/// <summary>
/// Create any missing lookup tables,
/// enforce values in the lookup tables
Expand Down Expand Up @@ -112,6 +116,7 @@ private IDbHandler GetDbHandler()
NameFieldLength = NameFieldLength,
TableNamePrefix = TableNamePrefix,
TableNameSuffix = TableNameSuffix,
UseTransaction = UseTransaction,
};
return dbHandler;
}
Expand Down
8 changes: 7 additions & 1 deletion EfEnumToLookup/LookupGenerator/IDbHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ internal interface IDbHandler
/// </summary>
string TableNameSuffix { get; set; }

/// <summary>
/// Whether to run the changes inside a database transaction.
/// </summary>
bool UseTransaction { get; set; }

/// <summary>
/// Make the required changes to the database.
/// </summary>
Expand All @@ -36,7 +41,8 @@ internal interface IDbHandler
/// Generates the migration SQL needed to update the database to match
/// the enums in the current model.
/// </summary>
/// <returns></returns>
/// <param name="model">Details of lookups and foreign keys to add/update</param>
/// <returns>The generated SQL script</returns>
string GenerateMigrationSql(LookupDbModel model);
}
}
5 changes: 5 additions & 0 deletions EfEnumToLookup/LookupGenerator/IEnumToLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ public interface IEnumToLookup
/// Defaults to "Enum_" set to null or "" to not have any prefix.
/// </summary>
string TableNamePrefix { get; set; }

/// <summary>
/// Whether to run the changes inside a database transaction.
/// </summary>
bool UseTransaction { get; set; }
}
}
34 changes: 28 additions & 6 deletions EfEnumToLookup/LookupGenerator/SqlServerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,47 @@ class SqlServerHandler : IDbHandler
{
/// <summary>
/// The size of the Name field that will be added to the generated lookup tables.
/// Adjust to suit your data if required, defaults to 255.
/// Adjust to suit your data if required.
/// </summary>
public int NameFieldLength { get; set; }

/// <summary>
/// Prefix to add to all the generated tables to separate help group them together
/// and make them stand out as different from other tables.
/// Defaults to "Enum_" set to null or "" to not have any prefix.
/// </summary>
public string TableNamePrefix { get; set; }

/// <summary>
/// Suffix to add to all the generated tables to separate help group them together
/// and make them stand out as different from other tables.
/// Defaults to "" set to null or "" to not have any suffix.
/// </summary>
public string TableNameSuffix { get; set; }


/// <summary>
/// Whether to run the changes inside a database transaction.
/// </summary>
public bool UseTransaction { get; set; }


/// <summary>
/// Make the required changes to the database.
/// </summary>
/// <param name="model">Details of lookups and foreign keys to add/update</param>
/// <param name="runSql">A callback providing a means to execute sql against the
/// server. (Or possibly write it to a file for later use.</param>
public void Apply(LookupDbModel model, Action<string, IEnumerable<SqlParameter>> runSql)
{
var sql = BuildSql(model);
runSql(sql, null);
}

/// <summary>
/// Generates the migration SQL needed to update the database to match
/// the enums in the current model.
/// </summary>
/// <param name="model">Details of lookups and foreign keys to add/update</param>
/// <returns>The generated SQL script</returns>
public string GenerateMigrationSql(LookupDbModel model)
{
return BuildSql(model);
Expand All @@ -43,12 +59,18 @@ private string BuildSql(LookupDbModel model)
{
var sql = new StringBuilder();
sql.AppendLine("set nocount on;");
sql.AppendLine("set xact_abort on; -- rollback on error");
sql.AppendLine("begin tran;");
if (UseTransaction)
{
sql.AppendLine("set xact_abort on; -- rollback on error");
sql.AppendLine("begin tran;");
}
sql.AppendLine(CreateTables(model.Lookups));
sql.AppendLine(PopulateLookups(model.Lookups));
sql.AppendLine(AddForeignKeys(model.References));
sql.AppendLine("commit;");
if (UseTransaction)
{
sql.AppendLine("commit;");
}
return sql.ToString();
}

Expand Down
5 changes: 4 additions & 1 deletion EfEnumToLookupTests/Tests/TestConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public void SetUp()
// Using setup rather than teardown to make it easier to inspect the database after running a test.
using (var context = new MagicContext())
{
context.Database.Delete();
if (context.Database.Exists())
{
context.Database.Delete();
}
}
}

Expand Down
9 changes: 6 additions & 3 deletions EfEnumToLookupTests/Tests/TestStuff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ public void SetUp()
// Using setup rather than teardown to make it easier to inspect the database after running a test.
using (var context = new MagicContext())
{
context.Database.Delete();
if (context.Database.Exists())
{
context.Database.Delete();
}
}

Database.SetInitializer(new TestInitializer(new EnumToLookup()));
Expand Down Expand Up @@ -63,9 +66,9 @@ public void UsesDescriptionAttribute()
{
using (var context = new MagicContext())
{
const string sql = "select @desciption = name from Enum_Importance where id = @id";
const string sql = "select @description = name from Enum_Importance where id = @id";
var idParam = new SqlParameter("id", (int)Importance.NotBovverd);
var outParam = new SqlParameter("desciption", SqlDbType.NVarChar, 255) { Direction = ParameterDirection.Output };
var outParam = new SqlParameter("description", SqlDbType.NVarChar, 255) { Direction = ParameterDirection.Output };
context.Database.ExecuteSqlCommand(sql, idParam, outParam);
var actualName = outParam.Value;
Assert.AreEqual(Constants.BovveredDisplay, actualName);
Expand Down
3 changes: 2 additions & 1 deletion ExampleUsage/EnumExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public void ExampleOfUsingApply()
}
}

[Test] public void ExampleOfGeneratingSql()
[Test]
public void ExampleOfGeneratingSql()
{
using (var context = new MyDbContext())
{
Expand Down

0 comments on commit 4b5e905

Please sign in to comment.