Skip to content

Commit

Permalink
Default time value (#27)
Browse files Browse the repository at this point in the history
* Handling default values for time data type
  • Loading branch information
carl-berg authored Feb 21, 2023
1 parent 4ea286d commit 83ff7ec
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
8 changes: 8 additions & 0 deletions DataDude.Tests/Core/Scripts/Migrations/1.0.0/01.Schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ CREATE TABLE Test_Geography_Data(
)
GO

CREATE TABLE Test_DateTime_Data(
Id INT IDENTITY PRIMARY KEY,
SomeDate DATE NOT NULL,
SomeTime TIME NOT NULL,
SomeDateTime DATETIME NOT NULL,
)
GO

CREATE TRIGGER People.EmployeeUpdatedAt ON People.Employee AFTER UPDATE AS BEGIN
UPDATE People.Employee
SET People.Employee.UpdatedAt = GETDATE()
Expand Down
12 changes: 10 additions & 2 deletions DataDude.Tests/Inserts/InstructionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,7 @@ public async Task Can_Insert_Geography_Data()
using var connection = Fixture.CreateNewConnection();
var position = SqlGeography.Point(50, 11, 4326);

var dude = new Dude();
await dude
await new Dude()
.Insert("Test_Geography_Data",
new { Position = position },
new { Position = (SqlGeography)null })
Expand All @@ -368,8 +367,17 @@ await dude
positions => positions[1].ShouldSatisfyAllConditions(
position => position.Lat.ShouldBeNull(),
position => position.Long.ShouldBeNull()));
}

[Fact]
public async Task Can_Insert_Default_Date_Times()
{
using var connection = Fixture.CreateNewConnection();

await new Dude().Insert("Test_DateTime_Data").Go(connection);

var rows = await connection.QueryAsync<dynamic>("SELECT SomeDate, SomeTime, SomeDateTime FROM Test_DateTime_Data");
rows.ShouldHaveSingleItem();
}
}
}
2 changes: 1 addition & 1 deletion DataDude/DataDude.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
<Version>0.8.0-preview.3</Version>
<Version>0.8.0-preview.4</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand Down
15 changes: 6 additions & 9 deletions DataDude/Instructions/Insert/ValueProviders/DateValueProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ public class DateValueProvider : ValueProvider
{
protected override ColumnValue? GetDefaultValue(ColumnInformation column, ColumnValue value)
{
if (column.DataType is "date" or "datetime" or "datetime2")
return column.DataType switch
{
return new ColumnValue(new DateTime(1753, 1, 1, 12, 0, 0));
}
else if (column.DataType is "datetimeoffset")
{
return new ColumnValue(new DateTimeOffset(1753, 1, 1, 12, 0, 0, TimeSpan.Zero));
}

return null;
"date" or "datetime" or "datetime2" => new ColumnValue(new DateTime(1753, 1, 1, 12, 0, 0)),
"datetimeoffset" => new ColumnValue(new DateTimeOffset(1753, 1, 1, 12, 0, 0, TimeSpan.Zero)),
"time" => new ColumnValue(TimeSpan.Zero),
_ => null
};
}
}
}

0 comments on commit 83ff7ec

Please sign in to comment.