diff --git a/DubUrl.Adomd/Querying/DaxValueFormatter.cs b/DubUrl.Adomd/Querying/DaxValueFormatter.cs index f8264864..db450ced 100644 --- a/DubUrl.Adomd/Querying/DaxValueFormatter.cs +++ b/DubUrl.Adomd/Querying/DaxValueFormatter.cs @@ -14,7 +14,10 @@ internal class DaxValueFormatter : BaseValueFormatter public DaxValueFormatter() { With(new BooleanFormatter()); - With(new DoubleQuotedValueFormatter()); + With(new GuidFormatter()); + With(new DoubleQuotedValueFormatter()); + With(new DoubleQuotedValueFormatter()); + With(new DoubleQuotedValueFormatter()); With(new FunctionFormatter("VALUE", new DaxDateFormatter())); With(new FunctionFormatter("VALUE", new DaxTimeFormatter())); With(new FunctionFormatter("VALUE", new DaxTimestampFormatter())); diff --git a/DubUrl.Core/Querying/Dialects/Formatters/DoubleQuotedValueFormatter.cs b/DubUrl.Core/Querying/Dialects/Formatters/DoubleQuotedValueFormatter.cs index 9cfafa82..780257ef 100644 --- a/DubUrl.Core/Querying/Dialects/Formatters/DoubleQuotedValueFormatter.cs +++ b/DubUrl.Core/Querying/Dialects/Formatters/DoubleQuotedValueFormatter.cs @@ -6,10 +6,17 @@ namespace DubUrl.Querying.Dialects.Formatters; -public class DoubleQuotedValueFormatter : IValueFormatter +public class DoubleQuotedValueFormatter : IValueFormatter, IValueFormatter { public string Format(string value) => $"\"{value}\""; + public string Format(char value) + => $"\"{value}\""; public string Format(object obj) - => obj is string value ? Format(value) : throw new Exception(); + => obj switch + { + char c => Format(c), + string str => Format(str), + _ => throw new Exception() + }; } diff --git a/DubUrl.Core/Querying/Dialects/Formatters/GuidFormatter.cs b/DubUrl.Core/Querying/Dialects/Formatters/GuidFormatter.cs new file mode 100644 index 00000000..5aabbc1a --- /dev/null +++ b/DubUrl.Core/Querying/Dialects/Formatters/GuidFormatter.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DubUrl.Querying.Dialects.Formatters; +public class GuidFormatter : IValueFormatter +{ + public string Format(Guid value) + => $"'{value}'"; + + public string Format(object obj) + => obj is Guid value + ? Format(value) + : obj is string str + ? Format(str) + : throw new Exception(); + + public virtual string Format(string value) + => Guid.TryParse(value, out var guid) ? Format(guid) : throw new Exception(); +} diff --git a/DubUrl.Core/Querying/Dialects/Formatters/SimpleQuotedValueFormatter.cs b/DubUrl.Core/Querying/Dialects/Formatters/SimpleQuotedValueFormatter.cs index 34c9aa1e..e993bdd8 100644 --- a/DubUrl.Core/Querying/Dialects/Formatters/SimpleQuotedValueFormatter.cs +++ b/DubUrl.Core/Querying/Dialects/Formatters/SimpleQuotedValueFormatter.cs @@ -6,10 +6,17 @@ namespace DubUrl.Querying.Dialects.Formatters; -public class SimpleQuotedValueFormatter : IValueFormatter +public class SimpleQuotedValueFormatter : IValueFormatter, IValueFormatter { public string Format(string value) => $"'{value}'"; + public string Format(char value) + => $"'{value}'"; public string Format(object obj) - => obj is string value ? Format(value) : throw new Exception(); + => obj switch + { + char c => Format(c), + string str => Format(str), + _ => throw new Exception() + }; } diff --git a/DubUrl.Core/Querying/Dialects/Renderers/ValueFormatter.cs b/DubUrl.Core/Querying/Dialects/Renderers/ValueFormatter.cs index 8fe51803..82815a85 100644 --- a/DubUrl.Core/Querying/Dialects/Renderers/ValueFormatter.cs +++ b/DubUrl.Core/Querying/Dialects/Renderers/ValueFormatter.cs @@ -14,7 +14,9 @@ public class ValueFormatter : BaseValueFormatter public ValueFormatter() { With(new BooleanFormatter()); - With(new SimpleQuotedValueFormatter()); + With(new GuidFormatter()); + With(new SimpleQuotedValueFormatter()); + With(new SimpleQuotedValueFormatter()); With(new PrefixFormatter("DATE", new DateFormatter())); With(new PrefixFormatter("TIME", new TimeFormatter())); With(new PrefixFormatter("TIMESTAMP", new TimestampFormatter())); diff --git a/DubUrl.Testing/Querying/Dialects/Formatters/FormattersTest.cs b/DubUrl.Testing/Querying/Dialects/Formatters/FormattersTest.cs index 1df15180..907eb6fc 100644 --- a/DubUrl.Testing/Querying/Dialects/Formatters/FormattersTest.cs +++ b/DubUrl.Testing/Querying/Dialects/Formatters/FormattersTest.cs @@ -133,4 +133,15 @@ public void PrefixFormatter_Format_Match(DateTime value, string expected) public void SimpleQuotedValueFormatter_Format_Match(string value, string expected) => Assert.That(new SimpleQuotedValueFormatter().Format(value), Is.EqualTo(expected)); -} \ No newline at end of file + [Test] + [TestCase("36f1d158-1fa1-11ed-ba36-c8cb9e32df8e", "'36f1d158-1fa1-11ed-ba36-c8cb9e32df8e'")] + [TestCase("36F1D158-1FA1-11ED-BA36-C8CB9e32DF8E", "'36f1d158-1fa1-11ed-ba36-c8cb9e32df8e'")] + public void GuidFormatter_Guid_Match(Guid value, string expected) + => Assert.That(new GuidFormatter().Format(value), Is.EqualTo($"'{value}'")); + + [Test] + [TestCase("36f1d158-1fa1-11ed-ba36-c8cb9e32df8e", "'36f1d158-1fa1-11ed-ba36-c8cb9e32df8e'")] + [TestCase("36F1D158-1FA1-11ED-BA36-C8CB9e32DF8E", "'36f1d158-1fa1-11ed-ba36-c8cb9e32df8e'")] + public void GuidFormatter_String_Match(string value, string expected) + => Assert.That(new GuidFormatter().Format(value), Is.EqualTo(expected)); +}