From 9935abd1a2ca4bf75d65b9a16e4bdb5639c774a8 Mon Sep 17 00:00:00 2001 From: Morten Christensen Date: Fri, 7 Dec 2012 12:23:03 -0100 Subject: [PATCH] Implementing Insert-builder with fluent syntax --- .../Model/InsertionDataDefinition.cs | 9 +++++ .../Expressions/InsertDataExpression.cs | 24 +++++++++++++ .../Syntax/Insert/IInsertBuilder.cs | 4 +-- .../Syntax/Insert/IInsertDataSyntax.cs | 7 ++++ .../Migrations/Syntax/Insert/InsertBuilder.cs | 11 +++++- .../Syntax/Insert/InsertDataBuilder.cs | 36 +++++++++++++++++++ src/Umbraco.Core/Umbraco.Core.csproj | 10 ++++++ 7 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 src/Umbraco.Core/Persistence/Migrations/Model/InsertionDataDefinition.cs create mode 100644 src/Umbraco.Core/Persistence/Migrations/Syntax/Insert/Expressions/InsertDataExpression.cs create mode 100644 src/Umbraco.Core/Persistence/Migrations/Syntax/Insert/IInsertDataSyntax.cs create mode 100644 src/Umbraco.Core/Persistence/Migrations/Syntax/Insert/InsertDataBuilder.cs diff --git a/src/Umbraco.Core/Persistence/Migrations/Model/InsertionDataDefinition.cs b/src/Umbraco.Core/Persistence/Migrations/Model/InsertionDataDefinition.cs new file mode 100644 index 0000000000..23a45a149b --- /dev/null +++ b/src/Umbraco.Core/Persistence/Migrations/Model/InsertionDataDefinition.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace Umbraco.Core.Persistence.Migrations.Model +{ + public class InsertionDataDefinition : List> + { + + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Migrations/Syntax/Insert/Expressions/InsertDataExpression.cs b/src/Umbraco.Core/Persistence/Migrations/Syntax/Insert/Expressions/InsertDataExpression.cs new file mode 100644 index 0000000000..33148be7cc --- /dev/null +++ b/src/Umbraco.Core/Persistence/Migrations/Syntax/Insert/Expressions/InsertDataExpression.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; +using Umbraco.Core.Persistence.Migrations.Model; + +namespace Umbraco.Core.Persistence.Migrations.Syntax.Insert.Expressions +{ + public class InsertDataExpression : IMigrationExpression + { + private readonly List _rows = new List(); + public string SchemaName { get; set; } + public string TableName { get; set; } + + public List Rows + { + get { return _rows; } + } + + public override string ToString() + { + //TODO implement the use of sql syntax provider? + + return string.Empty; + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Migrations/Syntax/Insert/IInsertBuilder.cs b/src/Umbraco.Core/Persistence/Migrations/Syntax/Insert/IInsertBuilder.cs index 7a8eeeb8ba..f57d28e2f4 100644 --- a/src/Umbraco.Core/Persistence/Migrations/Syntax/Insert/IInsertBuilder.cs +++ b/src/Umbraco.Core/Persistence/Migrations/Syntax/Insert/IInsertBuilder.cs @@ -1,7 +1,7 @@ namespace Umbraco.Core.Persistence.Migrations.Syntax.Insert { - public interface IInsertBuilder + public interface IInsertBuilder : IFluentSyntax { - + IInsertDataSyntax IntoTable(string tableName); } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Migrations/Syntax/Insert/IInsertDataSyntax.cs b/src/Umbraco.Core/Persistence/Migrations/Syntax/Insert/IInsertDataSyntax.cs new file mode 100644 index 0000000000..fb0585f0e6 --- /dev/null +++ b/src/Umbraco.Core/Persistence/Migrations/Syntax/Insert/IInsertDataSyntax.cs @@ -0,0 +1,7 @@ +namespace Umbraco.Core.Persistence.Migrations.Syntax.Insert +{ + public interface IInsertDataSyntax : IFluentSyntax + { + IInsertDataSyntax Row(object dataAsAnonymousType); + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Migrations/Syntax/Insert/InsertBuilder.cs b/src/Umbraco.Core/Persistence/Migrations/Syntax/Insert/InsertBuilder.cs index 12f046e56b..889c3e4008 100644 --- a/src/Umbraco.Core/Persistence/Migrations/Syntax/Insert/InsertBuilder.cs +++ b/src/Umbraco.Core/Persistence/Migrations/Syntax/Insert/InsertBuilder.cs @@ -1,4 +1,6 @@ -namespace Umbraco.Core.Persistence.Migrations.Syntax.Insert +using Umbraco.Core.Persistence.Migrations.Syntax.Insert.Expressions; + +namespace Umbraco.Core.Persistence.Migrations.Syntax.Insert { public class InsertBuilder : IInsertBuilder { @@ -8,5 +10,12 @@ { _context = context; } + + public IInsertDataSyntax IntoTable(string tableName) + { + var expression = new InsertDataExpression { TableName = tableName }; + _context.Expressions.Add(expression); + return new InsertDataBuilder(expression); + } } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Migrations/Syntax/Insert/InsertDataBuilder.cs b/src/Umbraco.Core/Persistence/Migrations/Syntax/Insert/InsertDataBuilder.cs new file mode 100644 index 0000000000..9e65b842ed --- /dev/null +++ b/src/Umbraco.Core/Persistence/Migrations/Syntax/Insert/InsertDataBuilder.cs @@ -0,0 +1,36 @@ +using System.Collections.Generic; +using System.ComponentModel; +using Umbraco.Core.Persistence.Migrations.Model; +using Umbraco.Core.Persistence.Migrations.Syntax.Insert.Expressions; + +namespace Umbraco.Core.Persistence.Migrations.Syntax.Insert +{ + public class InsertDataBuilder : IInsertDataSyntax + { + private readonly InsertDataExpression _expression; + + public InsertDataBuilder(InsertDataExpression expression) + { + _expression = expression; + } + + public IInsertDataSyntax Row(object dataAsAnonymousType) + { + _expression.Rows.Add(GetData(dataAsAnonymousType)); + return this; + } + + private static InsertionDataDefinition GetData(object dataAsAnonymousType) + { + var data = new InsertionDataDefinition(); + var properties = TypeDescriptor.GetProperties(dataAsAnonymousType); + + foreach (PropertyDescriptor property in properties) + { + data.Add(new KeyValuePair(property.Name, property.GetValue(dataAsAnonymousType))); + } + + return data; + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 2dd1c4279f..65e1145173 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -229,6 +229,7 @@ + @@ -290,8 +291,11 @@ + + + @@ -595,6 +599,12 @@ + + + + + +