Skip to content

Commit

Permalink
1.0.0-beta-sun-build34 Version:
Browse files Browse the repository at this point in the history
      Fix issue 21: #21
      Fix issue 25: #25
  • Loading branch information
hherzl committed Apr 19, 2023
1 parent 1b04cc8 commit 3756443
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
24 changes: 23 additions & 1 deletion CatFactory.Tests/DbDataReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ namespace CatFactory.Tests
{
public class DbDataReaderTests
{
private const string ConnectionString = "server=(local); database=master; integrated security=yes; TrustServerCertificate=True;";

[Fact]
public async Task TestGetClassDefinitionFromDbDataReaderAsync()
{
using var connection = new SqlConnection("server=(local); database=master; integrated security=yes; TrustServerCertificate=True;");
using var connection = new SqlConnection(ConnectionString);

await connection.OpenAsync();

Expand All @@ -27,5 +29,25 @@ public async Task TestGetClassDefinitionFromDbDataReaderAsync()

Assert.True(classDefinition.Properties.Count > 0);
}

[Fact]
public async Task TestGetRecordDefinitionFromDbDataReaderAsync()
{
using var connection = new SqlConnection(ConnectionString);

await connection.OpenAsync();

using var command = connection.CreateCommand();

command.Connection = connection;
command.CommandType = CommandType.Text;
command.CommandText = " select top 1 * from sys.tables ";

using var dataReader = await command.ExecuteReaderAsync();

var recordDefinition = dataReader.GetRecordDefinition();

Assert.True(recordDefinition.Properties.Count > 0);
}
}
}
1 change: 1 addition & 0 deletions CatFactory/CatFactory.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<PackageReleaseNotes>
1.0.0-beta-sun-build34 Version:

Fix issue 21: https://github.com/hherzl/CatFactory/issues/21
Fix issue 25: https://github.com/hherzl/CatFactory/issues/25
</PackageReleaseNotes>
<PackageLicenseUrl>https://github.com/hherzl/CatFactory/blob/master/LICENSE</PackageLicenseUrl>
Expand Down
35 changes: 35 additions & 0 deletions CatFactory/ObjectOrientedProgramming/DbDataReaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,40 @@ public static ClassDefinition GetClassDefinition(this DbDataReader dataReader, s

return definition;
}

/// <summary>
/// Gets a class definition from <see cref="DbDataReader"/> implementation
/// </summary>
/// <param name="dataReader">Implementation of <see cref="DbDataReader"/> class</param>
/// <param name="name">Class name</param>
/// <param name="namingConvention">Implementation of <see cref="ICodeNamingConvention"/> interface</param>
/// <returns></returns>
public static RecordDefinition GetRecordDefinition(this DbDataReader dataReader, string name = null, ICodeNamingConvention namingConvention = null)
{
var definition = new RecordDefinition
{
AccessModifier = AccessModifier.Public,
Name = name ?? "Record1"
};

if (dataReader.FieldCount > 0)
{
for (var i = 0; i < dataReader.FieldCount; i++)
{
var propertyType = dataReader.GetFieldType(i);
var propertyName = namingConvention == null ? dataReader.GetName(i) : namingConvention.GetPropertyName(dataReader.GetName(i));

definition.Properties.Add(new PropertyDefinition
{
AccessModifier = AccessModifier.Public,
Type = propertyType.Name,
Name = propertyName,
IsAutomatic = true
});
}
}

return definition;
}
}
}

0 comments on commit 3756443

Please sign in to comment.