Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[Contrib][T4] Fix detection of Guid DefaultValue

  • Loading branch information...
commit 8defc3ca4eef3fe1be5f8230244d57384edc74d2 1 parent 571ef31
@spaccabit authored
View
11 src/FluentMigrator.T4/CodeGenerator.cs
@@ -48,6 +48,7 @@ public string GetCurrentTimeStamp()
public string GetColumnDefaultValue(Column col)
{
string sysType = string.Format("\"{0}\"", col.DefaultValue);
+ var guid = Guid.Empty;
switch (col.PropertyType)
{
case System.Data.DbType.Currency:
@@ -64,7 +65,13 @@ public string GetColumnDefaultValue(Column col)
sysType = col.DefaultValue.Replace("'", "").Replace("\"", "");
break;
case System.Data.DbType.Guid:
- sysType = string.Format("\"{0}\"", col.DefaultValue);
+ if (col.DefaultValue.IsGuid(out guid))
+ {
+ if (guid == Guid.Empty)
+ sysType = "Guid.Empty";
+ else
+ sysType = string.Format("new System.Guid(\"{0}\")", guid);
+ }
break;
case System.Data.DbType.DateTime:
case System.Data.DbType.DateTime2:
@@ -293,7 +300,7 @@ public string GetMigrationTypeFunctionForType(Column col)
sysType = "AsInt64()";
break;
case System.Data.DbType.Single:
- sysType ="AsFloat()";
+ sysType = "AsFloat()";
break;
case System.Data.DbType.String:
sysType = string.Format("AsString({0})", sizeStr);
View
100 src/FluentMigrator.T4/Extensions/StringExtension.cs
@@ -0,0 +1,100 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Text.RegularExpressions;
+
+namespace System
+{
+ static class StringExtension
+ {
+ // using System.Text.RegularExpressions;
+
+ /// <summary>
+ /// Regular expression built for C# on: sab, dic 1, 2012, 11.00.55
+ /// Using Expresso Version: 3.0.3634, http://www.ultrapico.com
+ ///
+ /// check guid
+ ///
+ /// A description of the regular expression:
+ ///
+ /// Match expression but don't capture it. [\'], between 0 and 1 repetitions
+ /// Literal '
+ /// [1]: A numbered capture group. [[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}]
+ /// [0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}
+ /// Any character in this class: [0-9a-fA-F], exactly 8 repetitions
+ /// Literal -
+ /// Any character in this class: [0-9a-fA-F], exactly 4 repetitions
+ /// Literal -
+ /// Any character in this class: [0-9a-fA-F], exactly 4 repetitions
+ /// Literal -
+ /// Any character in this class: [0-9a-fA-F], exactly 4 repetitions
+ /// Literal -
+ /// Any character in this class: [0-9a-fA-F], exactly 12 repetitions
+ /// Match expression but don't capture it. [\'], between 0 and 1 repetitions
+ /// Literal '
+ ///
+ ///
+ /// </summary>
+ public static Regex isGuid = new Regex(
+ "(?:\\'){0,1}([0-9a-fA-F]{8}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4" +
+ "}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{12})(?:\\'){0,1}",
+ RegexOptions.Multiline
+ | RegexOptions.Compiled
+ );
+
+
+
+ //// Replace the matched text in the InputText using the replacement pattern
+ // string result = isGuid.Replace(InputText,isGuidReplace);
+
+ //// Split the InputText wherever the regex matches
+ // string[] results = isGuid.Split(InputText);
+
+ //// Capture the first Match, if any, in the InputText
+ // Match m = isGuid.Match(InputText);
+
+ //// Capture all Matches in the InputText
+ // MatchCollection ms = isGuid.Matches(InputText);
+
+ //// Test to see if there is a match in the InputText
+ // bool IsMatch = isGuid.IsMatch(InputText);
+
+ //// Get the names of all the named and numbered capture groups
+ // string[] GroupNames = isGuid.GetGroupNames();
+
+ //// Get the numbers of all the named and numbered capture groups
+ // int[] GroupNumbers = isGuid.GetGroupNumbers();
+
+
+
+
+
+ internal static bool IsGuid(this string candidate, out Guid output)
+ {
+ output = Guid.Empty;
+ if (string.IsNullOrEmpty(candidate))
+ return false;
+
+ bool isValid = false;
+
+ if (candidate != null)
+ {
+
+
+ var m = isGuid.Match(candidate);
+ if (m != null && m.Success)
+ {
+
+ output = new Guid(string.Format("{{{0}}}", m.Groups[1].Value));
+ isValid = true;
+
+ }
+
+ }
+
+ return isValid;
+
+ }
+ }
+}
View
1  src/FluentMigrator.T4/FluentMigrator.T4.csproj
@@ -43,6 +43,7 @@
<ItemGroup>
<Compile Include="DelegateTextWriter.cs" />
<Compile Include="Extensions\DataReaderExtension.cs" />
+ <Compile Include="Extensions\StringExtension.cs" />
<Compile Include="Models\Column.cs" />
<Compile Include="Models\ForeignKey.cs" />
<Compile Include="Models\IndexColumn.cs" />
Please sign in to comment.
Something went wrong with that request. Please try again.