Skip to content

Commit

Permalink
Handling default values (including null) for most sql server data typ…
Browse files Browse the repository at this point in the history
…es (#29)
  • Loading branch information
carl-berg authored Mar 11, 2023
1 parent 83ff7ec commit 49411c9
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 15 deletions.
49 changes: 49 additions & 0 deletions DataDude.Tests/Inserts/InstructionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,5 +379,54 @@ public async Task Can_Insert_Default_Date_Times()
var rows = await connection.QueryAsync<dynamic>("SELECT SomeDate, SomeTime, SomeDateTime FROM Test_DateTime_Data");
rows.ShouldHaveSingleItem();
}

[Theory]
[InlineData("bigint")]
[InlineData("bit")]
[InlineData("decimal")]
[InlineData("int")]
[InlineData("money")]
[InlineData("numeric")]
[InlineData("smallint")]
[InlineData("smallmoney")]
[InlineData("tinyint")]
[InlineData("float")]
[InlineData("real")]
[InlineData("date")]
[InlineData("datetime2")]
[InlineData("datetime")]
[InlineData("datetimeoffset")]
[InlineData("smalldatetime")]
[InlineData("time")]
[InlineData("char")]
[InlineData("text")]
[InlineData("varchar")]
[InlineData("nchar")]
[InlineData("ntext")]
[InlineData("nvarchar")]
[InlineData("binary")]
[InlineData("varbinary")]
[InlineData("image")]
[InlineData("uniqueidentifier")]
public async Task Can_Insert_Default_Values(string dataType)
{
using var connection = Fixture.CreateNewConnection();

var tableName = $"Test_{dataType}";
await connection.ExecuteAsync($"" +
$"""
CREATE TABLE [{tableName}]
(
Id INT PRIMARY KEY IDENTITY,
Value {dataType} NOT NULL,
NullableValue {dataType} NULL
)
""");

await new Dude().Insert(tableName).Go(connection);

var rows = await connection.QueryAsync<dynamic>($"SELECT * FROM [{tableName}]");
rows.ShouldHaveSingleItem();
}
}
}
4 changes: 2 additions & 2 deletions DataDude/DataDude.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>9.0</LangVersion>
<LangVersion>11.0</LangVersion>
<Nullable>enable</Nullable>
<Version>0.8.0-preview.4</Version>
<Version>0.8.0-preview.5</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand Down
28 changes: 19 additions & 9 deletions DataDude/DataDudeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,35 @@ public DataDudeContext(ISchemaLoader schemaLoader)
public static IDictionary<string, DbType> TypeMappings { get; } = new Dictionary<string, DbType>
{
["bit"] = DbType.Boolean,
["bigint"] = DbType.Int64,
["binary"] = DbType.Binary,
["char"] = DbType.String,
["date"] = DbType.Date,
["datetime"] = DbType.DateTime,
["datetime2"] = DbType.DateTime2,
["datetimeoffset"] = DbType.DateTimeOffset,
["decimal"] = DbType.Decimal,
["numeric"] = DbType.Decimal,
["float"] = DbType.Double,
["real"] = DbType.Double,
["uniqueidentifier"] = DbType.Guid,
["smallint"] = DbType.Int16,
["geography"] = DbType.String,
["int"] = DbType.Int32,
["bigint"] = DbType.Int64,
["variant"] = DbType.Object,
["varbinary"] = DbType.Binary,
["varchar"] = DbType.String,
["image"] = DbType.Binary,
["nchar"] = DbType.String,
["ntext"] = DbType.String,
["nvarchar"] = DbType.String,
["numeric"] = DbType.Decimal,
["money"] = DbType.Decimal,
["real"] = DbType.Double,
["smalldatetime"] = DbType.DateTime,
["smallint"] = DbType.Int16,
["smallmoney"] = DbType.Decimal,
["text"] = DbType.String,
["geography"] = DbType.String,
["tinyint"] = DbType.Int16,
["time"] = DbType.Time,
["timestamp"] = DbType.Binary,
["uniqueidentifier"] = DbType.Guid,
["varbinary"] = DbType.Binary,
["varchar"] = DbType.String,
["variant"] = DbType.Object,
};

public ISchemaLoader SchemaLoader { get; }
Expand Down
1 change: 1 addition & 0 deletions DataDude/Instructions/Insert/InsertContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public InsertContext(DataDudeContext context)
new DateValueProvider(),
new BoolValueProvider(),
new VersionValueProvider(),
new GuidValueProvider()
};

public IList<IInsertRowHandler> InsertRowHandlers { get; } = new List<IInsertRowHandler>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class BinaryValueProvider : ValueProvider
{
protected override ColumnValue? GetDefaultValue(ColumnInformation column, ColumnValue value)
{
if (column.DataType is "varbinary")
if (column.DataType is "binary" or "image" or "varbinary")
{
return new ColumnValue(new byte[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class DateValueProvider : ValueProvider
return column.DataType switch
{
"date" or "datetime" or "datetime2" => new ColumnValue(new DateTime(1753, 1, 1, 12, 0, 0)),
"smalldatetime" => new ColumnValue(new DateTime(1900, 1, 1, 12, 0, 0)),
"datetimeoffset" => new ColumnValue(new DateTimeOffset(1753, 1, 1, 12, 0, 0, TimeSpan.Zero)),
"time" => new ColumnValue(TimeSpan.Zero),
_ => null
Expand Down
18 changes: 18 additions & 0 deletions DataDude/Instructions/Insert/ValueProviders/GuidValueProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using DataDude.Schema;

namespace DataDude.Instructions.Insert.ValueProviders
{
public class GuidValueProvider : ValueProvider
{
protected override ColumnValue? GetDefaultValue(ColumnInformation column, ColumnValue value)
{
if (column.DataType is "uniqueidentifier")
{
return new ColumnValue(Guid.NewGuid());
}

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class NumericValueProvider : ValueProvider
{
protected override ColumnValue? GetDefaultValue(ColumnInformation column, ColumnValue value)
{
if (column.DataType is "int" or "smallint" or "bigint" or "decimal" or "numeric" or "real" or "float")
if (column.DataType is "int" or "smallint" or "tinyint" or "bigint" or "decimal" or "numeric" or "money" or "smallmoney" or "real" or "float")
{
return new ColumnValue(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class StringValueProvider : ValueProvider
{
protected override ColumnValue? GetDefaultValue(ColumnInformation column, ColumnValue value)
{
if (column.DataType is "varchar" or "nvarchar" or "text")
if (column.DataType is "char" or "varchar" or "nchar" or "ntext" or "nvarchar" or "text")
{
return new ColumnValue(string.Empty);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class VersionValueProvider : ValueProvider
{
protected override ColumnValue? GetDefaultValue(ColumnInformation column, ColumnValue value)
{
if (column.DataType is "timestamp")
if (column.DataType is "timestamp" or "rowversion")
{
return ColumnValue.Ignore;
}
Expand Down

0 comments on commit 49411c9

Please sign in to comment.