Skip to content

Commit

Permalink
fix json and csv output for dateonly and timespan
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed May 14, 2024
1 parent 9250633 commit 049a95c
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 130 deletions.
44 changes: 41 additions & 3 deletions src/FluentCommand.Csv/CsvCommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using CsvHelper;
using CsvHelper.Configuration;

using FluentCommand.Extensions;

using Microsoft.IO;

namespace FluentCommand;
Expand Down Expand Up @@ -257,17 +259,53 @@ private static void WriteValue(IDataReader reader, CsvWriter writer, int index)
return;
}

#if NET6_0_OR_GREATER
if (type == typeof(DateOnly))
{
var value = reader.GetValue<DateOnly>(index);
var formatted = value.ToString("yyyy'-'MM'-'dd", CultureInfo.InvariantCulture);

writer.WriteField(formatted);
return;
}

if (type == typeof(TimeOnly))
{
var value = reader.GetValue<TimeOnly>(index);
string formatted = value.Second == 0 && value.Millisecond == 0
? value.ToString("HH':'mm", CultureInfo.InvariantCulture)
: value.ToString(CultureInfo.InvariantCulture);

writer.WriteField(formatted);
return;
}
#endif

if (type == typeof(TimeSpan))
{
var value = reader.GetDateTime(index);
writer.WriteField(value);
var value = reader.GetValue<TimeSpan>(index);
string formatted = value.Seconds == 0 && value.Milliseconds == 0
? value.ToString(@"hh\:mm", CultureInfo.InvariantCulture)
: value.ToString();

writer.WriteField(formatted);
return;
}

if (type == typeof(DateTime))
{
var value = reader.GetDateTime(index);
writer.WriteField(value);
var dataType = reader.GetDataTypeName(index).ToLowerInvariant();

if (string.Equals(dataType, "date", StringComparison.OrdinalIgnoreCase))
{
var formattedDate = value.ToString("yyyy'-'MM'-'dd", CultureInfo.InvariantCulture);
writer.WriteField(formattedDate);
}
else
{
writer.WriteField(value);
}
return;
}

Expand Down
47 changes: 43 additions & 4 deletions src/FluentCommand.Json/JsonCommandExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System;
using System.Buffers;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Text;
using System.Text.Json;

using FluentCommand.Extensions;

using Microsoft.IO;

namespace FluentCommand;
Expand Down Expand Up @@ -173,7 +177,6 @@ private static void WriteValue(IDataReader reader, Utf8JsonWriter writer, int in
}

var type = reader.GetFieldType(index);

if (type == typeof(string))
{
var value = reader.GetString(index);
Expand Down Expand Up @@ -237,17 +240,53 @@ private static void WriteValue(IDataReader reader, Utf8JsonWriter writer, int in
return;
}

#if NET6_0_OR_GREATER
if (type == typeof(DateOnly))
{
var value = reader.GetValue<DateOnly>(index);
var formatted = value.ToString("yyyy'-'MM'-'dd", CultureInfo.InvariantCulture);

writer.WriteStringValue(formatted);
return;
}

if (type == typeof(TimeOnly))
{
var value = reader.GetValue<TimeOnly>(index);
string formatted = value.Second == 0 && value.Millisecond == 0
? value.ToString("HH':'mm", CultureInfo.InvariantCulture)
: value.ToString("HH':'mm':'ss.FFFFFFF", CultureInfo.InvariantCulture);

writer.WriteStringValue(formatted);
return;
}
#endif

if (type == typeof(TimeSpan))
{
var value = reader.GetDateTime(index);
writer.WriteStringValue(value);
var value = reader.GetValue<TimeSpan>(index);
string formatted = value.Seconds == 0 && value.Milliseconds == 0
? value.ToString(@"hh\:mm", CultureInfo.InvariantCulture)
: value.ToString();

writer.WriteStringValue(formatted);
return;
}

if (type == typeof(DateTime))
{
var value = reader.GetDateTime(index);
writer.WriteStringValue(value);
var dataType = reader.GetDataTypeName(index).ToLowerInvariant();

if (string.Equals(dataType, "date", StringComparison.OrdinalIgnoreCase))
{
var formattedDate = value.ToString("yyyy'-'MM'-'dd", CultureInfo.InvariantCulture);
writer.WriteStringValue(formattedDate);
}
else
{
writer.WriteStringValue(value);
}
return;
}

Expand Down
4 changes: 3 additions & 1 deletion test/FluentCommand.Entities/DataType.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace FluentCommand.Entities;

[Table("DataType", Schema = "dbo")]
public class DataType
{
public int Id { get; set; }
[Key]
public long Id { get; set; }

public string Name { get; set; }

Expand Down
4 changes: 2 additions & 2 deletions test/FluentCommand.SqlServer.Tests/DataMergeGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public async System.Threading.Tasks.Task BuildMergeDataTypeTests()
column.IsKey = true;
column.CanUpdate = false;

var users = new List<DataType>
var items = new List<DataType>
{
new() {
Id = 1,
Expand Down Expand Up @@ -214,7 +214,7 @@ public async System.Threading.Tasks.Task BuildMergeDataTypeTests()
}
};

var listDataReader = new ListDataReader<DataType>(users);
var listDataReader = new ListDataReader<DataType>(items);

var mergeDataStatement = DataMergeGenerator.BuildMerge(definition, listDataReader);
mergeDataStatement.Should().NotBeNullOrEmpty();
Expand Down
54 changes: 54 additions & 0 deletions test/FluentCommand.SqlServer.Tests/JsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,58 @@ public async Task QuerySelectAsync()

json.Should().NotBeNull();
}

[Fact]
public async Task QueryDataTypeSelectAsync()
{
var session = Services.GetRequiredService<IDataSession>();
session.Should().NotBeNull();

var item = new DataType()
{
Id = DateTime.Now.Ticks,
Name = "Test1",
Boolean = false,
Short = 2,
Long = 200,
Float = 200.20F,
Double = 300.35,
Decimal = 456.12M,
DateTime = new DateTime(2024, 5, 1, 8, 0, 0),
DateTimeOffset = new DateTimeOffset(2024, 5, 1, 8, 0, 0, TimeSpan.FromHours(-6)),
Guid = Guid.Empty,
TimeSpan = TimeSpan.FromHours(1),
DateOnly = new DateOnly(2022, 12, 1),
TimeOnly = new TimeOnly(1, 30, 0),
BooleanNull = false,
ShortNull = 2,
LongNull = 200,
FloatNull = 200.20F,
DoubleNull = 300.35,
DecimalNull = 456.12M,
DateTimeNull = new DateTime(2024, 4, 1, 8, 0, 0),
DateTimeOffsetNull = new DateTimeOffset(2024, 4, 1, 8, 0, 0, TimeSpan.FromHours(-6)),
GuidNull = Guid.Empty,
TimeSpanNull = TimeSpan.FromHours(1),
DateOnlyNull = new DateOnly(2022, 12, 1),
TimeOnlyNull = new TimeOnly(1, 30, 0),
};

await session
.Sql(builder => builder
.Insert<DataType>()
.Values(item)
)
.ExecuteAsync();

var json = await session
.Sql(builder => builder
.Select<DataType>()
.OrderBy(p => p.Id)
.Limit(0, 1000)
)
.QueryJsonAsync();

json.Should().NotBeNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ CREATE TABLE [dbo].[UserRole] (

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DataType]') AND type in (N'U'))
CREATE TABLE [dbo].[DataType] (
[Id] int NOT NULL,
[Id] bigint NOT NULL,
[Name] nvarchar(100) NOT NULL,
[Boolean] bit NOT NULL,
[Short] smallint NOT NULL,
Expand Down Expand Up @@ -176,7 +176,7 @@ CREATE TABLE [dbo].[DataType] (
);

-- Types
CREATE TYPE [dbo].[UserImportType] AS TABLE
CREATE TYPE [dbo].[UserImportType] AS TABLE
(
[EmailAddress] [nvarchar](256) NOT NULL,
[DisplayName] [nvarchar](256) NOT NULL,
Expand Down Expand Up @@ -249,5 +249,5 @@ CREATE INDEX [IX_UserLogin_UserId]
ON [dbo].[UserLogin] ([UserId]);

-- change tracking
ALTER TABLE [dbo].[User]
ALTER TABLE [dbo].[User]
ENABLE CHANGE_TRACKING WITH (TRACK_COLUMNS_UPDATED = OFF);
Loading

0 comments on commit 049a95c

Please sign in to comment.