Skip to content

Commit

Permalink
Add generation of DeleteAll, FindById and DeleteById methods for repo…
Browse files Browse the repository at this point in the history
…sitory
  • Loading branch information
kant2002 committed Nov 30, 2024
1 parent fc98a4e commit 622c72f
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 13 deletions.
203 changes: 203 additions & 0 deletions SqlMarshal.Tests/CrudGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,85 @@ partial class C
Assert.AreEqual(expectedOutput, output);
}

[TestMethod]
public void FindById_IList_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 TestEntity? FindById(int id);
}
}";
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 Foo.TestEntity? FindById(int id)
{
var connection = this.connection;
using var command = connection.CreateCommand();
var idParameter = command.CreateParameter();
idParameter.ParameterName = ""@id"";
idParameter.Value = id;
var parameters = new DbParameter[]
{
idParameter,
};
var sqlQuery = @""SELECT Id, Name FROM TestEntity WHERE Id = @id"";
command.CommandText = sqlQuery;
command.Parameters.AddRange(parameters);
using var reader = command.ExecuteReader(System.Data.CommandBehavior.SingleResult | System.Data.CommandBehavior.SingleRow);
if (!reader.Read())
{
return null;
}
var result = new TestEntity();
var value_0 = reader.GetValue(0);
result.Id = (int)value_0;
var value_1 = reader.GetValue(1);
result.Name = value_1 == DBNull.Value ? (string?)null : (string)value_1;
reader.Close();
return result;
}
}
}";
Assert.AreEqual(expectedOutput, output);
}

[TestMethod]
public void Count_int_Sync()
{
Expand Down Expand Up @@ -136,6 +215,130 @@ public partial int Count()
return (int)result!;
}
}
}";
Assert.AreEqual(expectedOutput, output);
}

[TestMethod]
public void DeleteAll_IList_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 void DeleteAll();
}
}";
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 void DeleteAll()
{
var connection = this.connection;
using var command = connection.CreateCommand();
var sqlQuery = @""DELETE FROM TestEntity"";
command.CommandText = sqlQuery;
command.ExecuteNonQuery();
}
}
}";
Assert.AreEqual(expectedOutput, output);
}

[TestMethod]
public void DeleteById_IList_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 void DeleteById(int id);
}
}";
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 void DeleteById(int id)
{
var connection = this.connection;
using var command = connection.CreateCommand();
var idParameter = command.CreateParameter();
idParameter.ParameterName = ""@id"";
idParameter.Value = id;
var parameters = new DbParameter[]
{
idParameter,
};
var sqlQuery = @""DELETE FROM TestEntity WHERE Id = @id"";
command.CommandText = sqlQuery;
command.Parameters.AddRange(parameters);
command.ExecuteNonQuery();
}
}
}";
Assert.AreEqual(expectedOutput, output);
}
Expand Down
7 changes: 5 additions & 2 deletions SqlMarshal/ClassGenerationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ public ClassGenerationContext(
List<IMethodSymbol> methods,
ISymbol attributeSymbol,
ISymbol repositoryAttributeSymbol,
NullableContextOptions nullableContextOptions)
GeneratorExecutionContext context)
{
this.ClassSymbol = classSymbol;
this.Methods = methods.Select(_ => new MethodGenerationContext(this, _)).ToList();
this.AttributeSymbol = attributeSymbol;
this.RepositoryAttributeSymbol = repositoryAttributeSymbol;
this.NullableContextOptions = nullableContextOptions;
this.GeneratorExecutionContext = context;
this.NullableContextOptions = context.Compilation.Options.NullableContextOptions;

this.ConnectionField = GetConnectionField(classSymbol);
this.DbContextField = GetContextField(classSymbol);
Expand All @@ -38,6 +39,8 @@ public ClassGenerationContext(

public ISymbol RepositoryAttributeSymbol { get; }

public GeneratorExecutionContext GeneratorExecutionContext { get; }

public NullableContextOptions NullableContextOptions { get; }

public bool HasNullableAnnotations => this.NullableContextOptions != NullableContextOptions.Disable;
Expand Down
7 changes: 7 additions & 0 deletions SqlMarshal/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
namespace SqlMarshal;

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Linq;

internal static class Extensions
{
Expand Down Expand Up @@ -174,4 +176,9 @@ internal static ITypeSymbol UnwrapListItem(ITypeSymbol returnType)

return returnType;
}

internal static IPropertySymbol? FindIdMember(this ITypeSymbol returnType)
{
return returnType.GetMembers().OfType<IPropertySymbol>().FirstOrDefault(_ => _.Name == "Id");
}
}
Loading

0 comments on commit 622c72f

Please sign in to comment.