Skip to content

Commit

Permalink
#27 fix sql generator for db
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiCorbilla committed Oct 18, 2022
1 parent d669f12 commit 8164ec7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
32 changes: 31 additions & 1 deletion table.lib.tests/SqlInsertTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using Dapper;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using table.runner;

namespace table.lib.tests
Expand Down Expand Up @@ -31,7 +35,33 @@ public void TestNullGeneration()
var lines = s.Split(Environment.NewLine);
Assert.Multiple(() =>
{
Assert.That(lines[0], Is.EqualTo("INSERT INTO TestClass (Field1,Field2,Field3,Field4,Field5,Field6) VALUES (321121,NULL,2121.32,1,'1970-01-01',34.43);"));
Assert.That(lines[0], Is.EqualTo("INSERT INTO TestClass (Field1,Field2,Field3,Field4,Field5,Field6) VALUES (NULL,NULL,NULL,NULL,NULL,NULL);"));
});
}

[Test]
public void TestNullFromDBGeneration()
{
IEnumerable<IDictionary<string, object>> table;
using (var connection = new SqlConnection(@"Data Source=DESKTOP-TTUSQLJ\SQLEXPRESS;Initial Catalog=store;Integrated Security=True"))
{
connection.Open();
const string data = @"
SELECT PhoneNumber, LockoutEnd
FROM [store].[dbo].[AspNetUsers]
";
table = connection.Query(data) as IEnumerable<IDictionary<string, object>>;
}

var enumerable = table as IDictionary<string, object>[] ??
(table ?? throw new InvalidOperationException()).ToArray();


var s = DbTable.Add(enumerable).ToSqlInsertString();
var lines = s.Split(Environment.NewLine);
Assert.Multiple(() =>
{
Assert.That(lines[0], Is.EqualTo("INSERT INTO Table1 (PhoneNumber,LockoutEnd) VALUES (NULL,NULL);"));
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion table.lib/DbTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public string ToSqlInsertString()
DateTime time => "'" + time.ToString("yyyy-MM-dd") + "'",
decimal value1 => value1.ToString("#0.0###"),
double value1 => value1.ToString("#0.0###"),
_ => (obj != null ? obj.ToString().ToSql() : "")
_ => (obj != null ? obj.ToString().ToSql() : "NULL")
};
s += $"{p},";
}
Expand Down
8 changes: 4 additions & 4 deletions table.lib/table.lib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>table.lib</RootNamespace>
<Version>1.12.0</Version>
<Version>1.13.0</Version>
<Copyright>Jordi Corbilla</Copyright>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<RepositoryUrl>https://github.com/JordiCorbilla/table.lib</RepositoryUrl>
<PackageReleaseNotes>v1.12
<PackageReleaseNotes>v1.13
- Upgrade to .net 6</PackageReleaseNotes>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Description>Simple c# (.NET 6) table library that renders any List&lt;T&gt; or Dictionary&lt;TV, T&gt; into a nicely formatted markdown, csv, html, specflow or console table, allowing for extra formats. It also supports dynamic returns from Dapper as IEnumerable&lt;IDictionary&lt;string, object&gt;&gt; via DBTable object.</Description>
<AssemblyVersion>1.12.0.0</AssemblyVersion>
<FileVersion>1.12.0.0</FileVersion>
<AssemblyVersion>1.13.0.0</AssemblyVersion>
<FileVersion>1.13.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions table.runner/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public static List<TestClass> GetNullOutput()
{
new TestClass
{
Field1 = 321121, Field2 = null, Field3 = 2121.32m, Field4 = true,
Field5 = new DateTime(1970, 1, 1), Field6 = 34.43
Field1 = null, Field2 = null, Field3 = null, Field4 = null,
Field5 = null, Field6 = null
}
};
return list;
Expand Down
12 changes: 6 additions & 6 deletions table.runner/TestClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ namespace table.runner
{
public class TestClass
{
public int Field1 { get; set; }
public string Field2 { get; set; }
public decimal Field3 { get; set; }
public bool Field4 { get; set; }
public DateTime Field5 { get; set; }
public double Field6 { get; set; }
public int? Field1 { get; set; }
public string? Field2 { get; set; }
public decimal? Field3 { get; set; }
public bool? Field4 { get; set; }
public DateTime? Field5 { get; set; }
public double? Field6 { get; set; }
}
}

0 comments on commit 8164ec7

Please sign in to comment.