Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select() support for TimeSpan. Not complete LINQ support yet. Closes … #3418

Merged
merged 1 commit into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/LinqTests/Acceptance/querying_against_timespan.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using JasperFx.Core;
using Marten;
using Marten.Testing.Documents;
using Marten.Testing.Harness;
using Shouldly;

namespace LinqTests.Acceptance;

public class querying_against_timespan : IntegrationContext
{
public querying_against_timespan(DefaultStoreFixture fixture) : base(fixture)
{
}

[Fact]
public async Task select_to_time_span()
{
var span = TimeSpan.Parse("-20154.01:12:32");

var targets = Target.GenerateRandomData(10).ToArray();
var first = targets.First();
await theStore.BulkInsertDocumentsAsync(targets);

var results = await theSession.Query<Target>().Where(x => x.Id == first.Id).Select(x => x.HowLong)
.ToListAsync();
results.Single().ShouldBe(first.HowLong);
}
}
4 changes: 4 additions & 0 deletions src/Marten.Testing/Documents/Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public static Target Random(bool deep = false)
target.Double = _random.NextDouble();
target.Long = _random.Next() * 10000;

target.HowLong = TimeSpan.FromSeconds(target.Long);

target.Date = DateTime.Today.AddDays(_random.Next(-10000, 10000));

if (value > 15)
Expand Down Expand Up @@ -185,6 +187,8 @@ public Target()
public List<string> StringList { get; set; }

public Guid[] GuidArray { get; set; }

public TimeSpan HowLong { get; set; }
}

public class Address
Expand Down
1 change: 1 addition & 0 deletions src/Marten/Linq/Members/EnumAsIntegerMember.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
using System;
using System.Linq.Expressions;
using System.Reflection;
using Marten.Exceptions;
Expand Down
22 changes: 22 additions & 0 deletions src/Marten/Linq/Selectors/TimeSpanSelector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#nullable enable
using System;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;

namespace Marten.Linq.Selectors;

internal class TimeSpanSelector: ISelector<TimeSpan>
{
public TimeSpan Resolve(DbDataReader reader)
{
var text = reader.GetString(0);
return TimeSpan.Parse(text);
}

public async Task<TimeSpan> ResolveAsync(DbDataReader reader, CancellationToken token)
{
var text = await reader.GetFieldValueAsync<string>(0, token).ConfigureAwait(false);
return TimeSpan.Parse(text);
}
}
4 changes: 3 additions & 1 deletion src/Marten/Linq/SqlGeneration/DataSelectClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@ public string[] SelectFields()

public ISelector BuildSelector(IMartenSession session)
{
if (typeof(T) == typeof(TimeSpan)) return new TimeSpanSelector();

return new SerializationSelector<T>(session.Serializer);
}

public IQueryHandler<TResult> BuildHandler<TResult>(IMartenSession session, ISqlFragment statement,
ISqlFragment currentStatement)
{
var selector = new SerializationSelector<T>(session.Serializer);
var selector = typeof(T) == typeof(TimeSpan) ? (ISelector<T>)new TimeSpanSelector() : new SerializationSelector<T>(session.Serializer);

return LinqQueryParser.BuildHandler<T, TResult>(selector, statement);
}
Expand Down
Loading