Skip to content

Commit

Permalink
Support Guid ToString in SqLite, add test for mssql and sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
jogibear9988 committed Jan 27, 2024
1 parent 19cafcd commit da110d4
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions Source/LinqToDB/Linq/Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ public void SetInfo(MappingSchema mappingSchema)
{ MT<uint? >(() => ((uint?) 0) .ToString()!), N(() => L<uint?, string>((uint? p0) => p0!.Value.ToString() )) },
{ MT<ulong? >(() => ((ulong?) 0) .ToString()!), N(() => L<ulong?, string>((ulong? p0) => p0!.Value.ToString() )) },

{ MT<Guid? >(() => (Guid.Empty) .ToString()!), N(() => L<Guid?, string>((Guid? p0) => Sql.GuidToString(p0)!)) },

// handle all other as default
{ MT<object>(() => ((object)0).ToString()!), N(() => L<object, string>((object p0) => Sql.ConvertTo<string>.From(p0) )) },
Expand Down
18 changes: 18 additions & 0 deletions Source/LinqToDB/Sql/Sql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,24 @@ public static Guid NewGuid()
return Guid.NewGuid();
}

sealed class GuidToStringBuilder : IExtensionCallBuilder
{
public void Build(ISqExtensionBuilder builder)
{
var para = builder.GetExpression(0);

builder.ResultExpression = PseudoFunctions.MakeConvert(SqlDataType.String, SqlDataType.Guid, para);
}
}

[CLSCompliant(false)]
[Expression(PN.SQLite, "(substr(hex({0}), 7, 2) || substr(hex({0}), 5, 2) || substr(hex({0}), 3, 2) || substr(hex({0}), 1, 2) || '-' || substr(hex({0}), 11, 2) || substr(hex({0}), 9, 2) || '-' || substr(hex({0}), 15, 2) || substr(hex({0}), 13, 2) || '-' || substr(hex({0}), 17, 4) || '-' || substr(hex({0}), 21, 12))", IsNullable = IsNullableType.IfAnyParameterNullable)]
[Extension("", BuilderType = typeof(GuidToStringBuilder))]
public static string? GuidToString(Guid? value)
{
return value == null ? null : value.ToString();
}

#endregion

#region Convert Functions
Expand Down
62 changes: 62 additions & 0 deletions Tests/Linq/UserTests/Issue4295Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Linq;
using FluentAssertions;
using LinqToDB;
using LinqToDB.Mapping;
using NUnit.Framework;
using System.Linq.Dynamic.Core;
using System.Linq.Dynamic.Core.CustomTypeProviders;
using System.Collections.Generic;
using System;
using LinqToDB.Data;

namespace Tests.UserTests
{
[TestFixture]
public class Issue4295Tests : TestBase
{
[Table]
public class InfeedAdvicePositionDTO
{
[Column] public Guid Id { get; set; }
}

[Test]
public void TestGuidToString([IncludeDataSources(TestProvName.AllSQLite, TestProvName.AllSqlServer)] string context)
{
using (var db = GetDataContext(context))
using (db.CreateLocalTable<InfeedAdvicePositionDTO>())
{
var a = new InfeedAdvicePositionDTO() { Id = Guid.Parse("193AE7F4-5309-4EEE-A746-27B28C7E30F3") };
db.Insert(a);

var qryA = from infeed in db.GetTable<InfeedAdvicePositionDTO>()
where infeed.Id.ToString().ToLower().Contains("7f4-53")
select infeed;

var lA = qryA.ToList();
Assert.AreEqual(1, lA.Count);

var qryB = from infeed in db.GetTable<InfeedAdvicePositionDTO>()
where infeed.Id.ToString().ToLower().StartsWith("193ae")
select infeed;

var lB = qryB.ToList();
Assert.AreEqual(1, lB.Count);

var qryC = from infeed in db.GetTable<InfeedAdvicePositionDTO>()
where infeed.Id.ToString().ToLower().Contains("8f4-53")
select infeed;

var lC = qryC.ToList();
Assert.AreEqual(0, lC.Count);

var qryD = from infeed in db.GetTable<InfeedAdvicePositionDTO>()
where infeed.Id.ToString().ToLower().StartsWith("293ae")
select infeed;

var lD = qryD.ToList();
Assert.AreEqual(0, lD.Count);
}
}
}
}

0 comments on commit da110d4

Please sign in to comment.