Skip to content

Commit

Permalink
Add count method
Browse files Browse the repository at this point in the history
  • Loading branch information
kant2002 committed Nov 27, 2024
1 parent b8be291 commit fc98a4e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
58 changes: 58 additions & 0 deletions SqlMarshal.Tests/CrudGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,64 @@ partial class C
return result;
}
}
}";
Assert.AreEqual(expectedOutput, output);
}

[TestMethod]
public void Count_int_Sync()
{
string source = @"
#nullable enable
namespace Foo
{
class TestEntity
{
public int Id { get; set; }
public string Name { get; set; }
}
[Repository(typeof(TestEntity))]
partial class C
{
private DbConnection connection;
public partial int Count();
}
}";
string output = this.GetGeneratedOutput(source, NullableContextOptions.Disable);

Assert.IsNotNull(output);

var expectedOutput = @"// <auto-generated>
// Code generated by Stored Procedures Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is
// regenerated.
// </auto-generated>
#nullable enable
#pragma warning disable 1591
namespace Foo
{
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
partial class C
{
public partial int Count()
{
var connection = this.connection;
using var command = connection.CreateCommand();
var sqlQuery = @""SELECT COUNT(1) FROM TestEntity"";
command.CommandText = sqlQuery;
var result = command.ExecuteScalar();
return (int)result!;
}
}
}";
Assert.AreEqual(expectedOutput, output);
}
Expand Down
8 changes: 8 additions & 0 deletions SqlMarshal/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,14 @@ private void MapResults(
return builder.ToString();
}

if (canonicalOperationName == "Count")
{
var builder = new StringBuilder();
builder.Append("SELECT COUNT(1) FROM ");
builder.Append(entityType.Name);
return builder.ToString();
}

return null;
}

Expand Down

0 comments on commit fc98a4e

Please sign in to comment.