Skip to content

Commit

Permalink
refactor generator
Browse files Browse the repository at this point in the history
- removed GenerateDataReaderAttribute use TableAttribute
- make FluentCommand.Dapper, FluentCommand.EntityFactory and FluentCommand.Batch obsolete
- changed generated extension methods to be Query<User> instead of QueryUser
  • Loading branch information
pwelter34 committed Aug 16, 2023
1 parent 54b663a commit 7251390
Show file tree
Hide file tree
Showing 31 changed files with 192 additions and 322 deletions.
23 changes: 0 additions & 23 deletions FluentCommand.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentCommand.SQLite.Tests"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentCommand.SqlServer.Tests", "test\FluentCommand.SqlServer.Tests\FluentCommand.SqlServer.Tests.csproj", "{5DE2495F-EC9E-49C5-8211-5ACB44A780A8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentCommand.Batch", "src\FluentCommand.Batch\FluentCommand.Batch.csproj", "{9DEC5471-EEFE-48EC-8D7A-BEE501087F38}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentCommand.Batch.Tests", "test\FluentCommand.Batch.Tests\FluentCommand.Batch.Tests.csproj", "{CCC34DD0-4706-44CB-AC03-A0E607F449A2}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{38D98603-4878-4CFA-ACF5-5BEC1218B149}"
Expand All @@ -29,19 +27,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentCommand.Tests", "test
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentCommand.PostgreSQL.Tests", "test\FluentCommand.PostgreSQL.Tests\FluentCommand.PostgreSQL.Tests.csproj", "{CFCFEA34-ABFA-490E-85BF-A7DC12392245}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentCommand.EntityFactory", "src\FluentCommand.EntityFactory\FluentCommand.EntityFactory.csproj", "{F05DE8E6-1CBB-4304-8D41-68D9BB5DF6B6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentCommand.SqlServer", "src\FluentCommand.SqlServer\FluentCommand.SqlServer.csproj", "{536C4B33-C1D8-495B-A282-97F525BF994D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{2BE1C80C-1E25-4143-8319-0217575B2560}"
ProjectSection(SolutionItems) = preProject
README.md = README.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentCommand.Json", "src\FluentCommand.Json\FluentCommand.Json.csproj", "{5DC02823-0A42-4C2D-8E49-C0BEA9CAE97A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentCommand.Dapper", "src\FluentCommand.Dapper\FluentCommand.Dapper.csproj", "{47EE3C3A-178C-4AE5-8A54-F3791FA76E08}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentCommand.Generators", "src\FluentCommand.Generators\FluentCommand.Generators.csproj", "{43DCCD08-D585-4905-95A9-120D300C512A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentCommand.Generators.Tests", "test\FluentCommand.Generators.Tests\FluentCommand.Generators.Tests.csproj", "{00F44ED1-561E-43DC-ABC1-A78411FFC6F3}"
Expand All @@ -68,10 +57,6 @@ Global
{5DE2495F-EC9E-49C5-8211-5ACB44A780A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5DE2495F-EC9E-49C5-8211-5ACB44A780A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5DE2495F-EC9E-49C5-8211-5ACB44A780A8}.Release|Any CPU.Build.0 = Release|Any CPU
{9DEC5471-EEFE-48EC-8D7A-BEE501087F38}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9DEC5471-EEFE-48EC-8D7A-BEE501087F38}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9DEC5471-EEFE-48EC-8D7A-BEE501087F38}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9DEC5471-EEFE-48EC-8D7A-BEE501087F38}.Release|Any CPU.Build.0 = Release|Any CPU
{CCC34DD0-4706-44CB-AC03-A0E607F449A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CCC34DD0-4706-44CB-AC03-A0E607F449A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CCC34DD0-4706-44CB-AC03-A0E607F449A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -84,10 +69,6 @@ Global
{CFCFEA34-ABFA-490E-85BF-A7DC12392245}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CFCFEA34-ABFA-490E-85BF-A7DC12392245}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CFCFEA34-ABFA-490E-85BF-A7DC12392245}.Release|Any CPU.Build.0 = Release|Any CPU
{F05DE8E6-1CBB-4304-8D41-68D9BB5DF6B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F05DE8E6-1CBB-4304-8D41-68D9BB5DF6B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F05DE8E6-1CBB-4304-8D41-68D9BB5DF6B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F05DE8E6-1CBB-4304-8D41-68D9BB5DF6B6}.Release|Any CPU.Build.0 = Release|Any CPU
{536C4B33-C1D8-495B-A282-97F525BF994D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{536C4B33-C1D8-495B-A282-97F525BF994D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{536C4B33-C1D8-495B-A282-97F525BF994D}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -96,10 +77,6 @@ Global
{5DC02823-0A42-4C2D-8E49-C0BEA9CAE97A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5DC02823-0A42-4C2D-8E49-C0BEA9CAE97A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5DC02823-0A42-4C2D-8E49-C0BEA9CAE97A}.Release|Any CPU.Build.0 = Release|Any CPU
{47EE3C3A-178C-4AE5-8A54-F3791FA76E08}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{47EE3C3A-178C-4AE5-8A54-F3791FA76E08}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47EE3C3A-178C-4AE5-8A54-F3791FA76E08}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47EE3C3A-178C-4AE5-8A54-F3791FA76E08}.Release|Any CPU.Build.0 = Release|Any CPU
{43DCCD08-D585-4905-95A9-120D300C512A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{43DCCD08-D585-4905-95A9-120D300C512A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{43DCCD08-D585-4905-95A9-120D300C512A}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
34 changes: 13 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Fluent Wrapper for DbCommand.
| Package | Version |
| :--- | :--- |
| [FluentCommand](https://www.nuget.org/packages/FluentCommand/) | [![FluentCommand](https://img.shields.io/nuget/v/FluentCommand.svg)](https://www.nuget.org/packages/FluentCommand/) |
| [FluentCommand.Dapper](https://www.nuget.org/packages/FluentCommand.Dapper/) | [![FluentCommand.Dapper](https://img.shields.io/nuget/v/FluentCommand.Dapper.svg)](https://www.nuget.org/packages/FluentCommand.Dapper/) |
| [FluentCommand.SqlServer](https://www.nuget.org/packages/FluentCommand.SqlServer/) | [![FluentCommand.SqlServer](https://img.shields.io/nuget/v/FluentCommand.SqlServer.svg)](https://www.nuget.org/packages/FluentCommand.SqlServer/) |
| [FluentCommand.Json](https://www.nuget.org/packages/FluentCommand.Json/) | [![FluentCommand.Json](https://img.shields.io/nuget/v/FluentCommand.Json.svg)](https://www.nuget.org/packages/FluentCommand.Json/) |

Expand Down Expand Up @@ -133,23 +132,6 @@ session
priorities = q.Query<Priority>().ToList();
});
```
## Dapper

PM> Install-Package FluentCommand.Dapper

Use Dapper to materialize data reader to entities

```c#

string email = "kara.thrace@battlestar.com";
string sql = "select * from [User] where EmailAddress = @EmailAddress";

var session = configuration.CreateSession();
var user = await session
.Sql(sql)
.Parameter("@EmailAddress", email)
.QuerySingleAsync<User>();
```

## Query Builder

Expand All @@ -165,7 +147,7 @@ var user = await session
.Select<User>() // table name comes from type
.Where(p => p.EmailAddress, email)
)
.QuerySingleAsync();
.QuerySingleAsync<User>();
```

Count query
Expand Down Expand Up @@ -231,10 +213,9 @@ var deleteId = await session

### Source Generator

The project supports generating a DbDataReader from a class via an attribute. Add the `GenerateDataReaderAttribute` to a class to generate the needed extension methods.
The project supports generating a DbDataReader from a class via an attribute. Add the `TableAttribute` to a class to generate the needed extension methods.

```c#
[GenerateDataReader]
[Table("Status", Schema = "dbo")]
public class Status
{
Expand All @@ -258,6 +239,17 @@ public class Status
}
```

Extension methods are generated to materialize data command to entities

```c#
string email = "kara.thrace@battlestar.com";
string sql = "select * from [User] where EmailAddress = @EmailAddress";
var session = configuration.CreateSession();
var user = await session
.Sql(sql)
.Parameter("@EmailAddress", email)
.QuerySingleAsync<User>();
```


## SQL Server Features
Expand Down
4 changes: 2 additions & 2 deletions src/FluentCommand.Generators/DataReaderFactoryGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class DataReaderFactoryGenerator : IIncrementalGenerator
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var provider = context.SyntaxProvider.ForAttributeWithMetadataName(
fullyQualifiedMetadataName: "FluentCommand.GenerateDataReaderAttribute",
fullyQualifiedMetadataName: "System.ComponentModel.DataAnnotations.Schema.TableAttribute",
predicate: SyntacticPredicate,
transform: SemanticTransform
)
Expand Down Expand Up @@ -51,7 +51,7 @@ private static void Execute(SourceProductionContext context, EntityClass entityC

var source = DataReaderFactoryWriter.Generate(entityClass);

context.AddSource($"{qualifiedName}.DataReaderFactory.g.cs", source);
context.AddSource($"{qualifiedName}DataReaderExtensions.g.cs", source);
}

private static bool SyntacticPredicate(SyntaxNode syntaxNode, CancellationToken cancellationToken)
Expand Down
88 changes: 55 additions & 33 deletions src/FluentCommand.Generators/DataReaderFactoryWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ namespace FluentCommand.Generators;

public static class DataReaderFactoryWriter
{
#if DEBUG
private static int _counter = 0;
#endif

public static string Generate(EntityClass entityClass)
{
if (entityClass == null)
Expand All @@ -22,11 +18,6 @@ public static string Generate(EntityClass entityClass)
.AppendLine("using global::FluentCommand.Extensions;")
.AppendLine();

#if DEBUG
// used to track re-writes for performance
codeBuilder.AppendLine($"// Counter: {Interlocked.Increment(ref _counter)}");
#endif

codeBuilder
.Append("namespace ")
.AppendLine(entityClass.EntityNamespace)
Expand All @@ -50,7 +41,7 @@ public static string Generate(EntityClass entityClass)
.AppendLine("[global::System.Diagnostics.DebuggerStepThroughAttribute]")
.Append("public static partial class ")
.Append(entityClass.EntityName)
.AppendLine("DataReaderFactoryExtensions")
.AppendLine("DataReaderExtensions")
.AppendLine("{")
.IncrementIndent();

Expand Down Expand Up @@ -98,26 +89,35 @@ private static void WriteQuerySingleEntityTask(IndentedStringBuilder codeBuilder
.Append(".")
.Append(entity.EntityName)
.Append("> QuerySingle")
.Append(entity.EntityName)
.AppendLine("Async(")
.AppendLine("Async<TEntity>(")
.IncrementIndent()
.AppendLine("this global::FluentCommand.IDataQueryAsync dataQuery,")
.AppendLine("global::System.Threading.CancellationToken cancellationToken = default)")
.Append("=> dataQuery.QuerySingleAsync<")
.Append("where TEntity : ")
.Append(entity.EntityNamespace)
.Append(".")
.AppendLine(entity.EntityName)
.DecrementIndent()
.AppendLine("{")
.IncrementIndent()
.Append("return dataQuery.QuerySingleAsync<")
.Append(entity.EntityNamespace)
.Append(".")
.Append(entity.EntityName)
.AppendLine(">(")
.IncrementIndent()
.Append("factory: ")
.Append(entity.EntityName)
.Append("DataReaderFactoryExtensions.")
.Append("DataReaderExtensions.")
.Append(entity.EntityName)
.AppendLine("Factory,")
.AppendLine("commandBehavior: global::System.Data.CommandBehavior.SequentialAccess | global::System.Data.CommandBehavior.SingleResult | global::System.Data.CommandBehavior.SingleRow,")
.AppendLine("commandBehavior: global::System.Data.CommandBehavior.SequentialAccess |")
.AppendLine(" global::System.Data.CommandBehavior.SingleResult |")
.AppendLine(" global::System.Data.CommandBehavior.SingleRow,")
.AppendLine("cancellationToken: cancellationToken);")
.DecrementIndent()
.DecrementIndent()
.AppendLine("}")
.AppendLine();
}

Expand Down Expand Up @@ -145,27 +145,34 @@ private static void WriteQueryEntityTask(IndentedStringBuilder codeBuilder, Enti
.Append(entity.EntityNamespace)
.Append(".")
.Append(entity.EntityName)
.Append(">> Query")
.Append(entity.EntityName)
.AppendLine("Async(")
.AppendLine(">> QueryAsync<TEntity>(")
.IncrementIndent()
.AppendLine("this global::FluentCommand.IDataQueryAsync dataQuery,")
.AppendLine("global::System.Threading.CancellationToken cancellationToken = default)")
.Append("=> dataQuery.QueryAsync<")
.Append("where TEntity : ")
.Append(entity.EntityNamespace)
.Append(".")
.AppendLine(entity.EntityName)
.DecrementIndent()
.AppendLine("{")
.IncrementIndent()
.Append("return dataQuery.QueryAsync<")
.Append(entity.EntityNamespace)
.Append(".")
.Append(entity.EntityName)
.AppendLine(">(")
.IncrementIndent()
.Append("factory: ")
.Append(entity.EntityName)
.Append("DataReaderFactoryExtensions.")
.Append("DataReaderExtensions.")
.Append(entity.EntityName)
.AppendLine("Factory,")
.AppendLine("commandBehavior: global::System.Data.CommandBehavior.SequentialAccess | global::System.Data.CommandBehavior.SingleResult,")
.AppendLine("commandBehavior: global::System.Data.CommandBehavior.SequentialAccess |")
.AppendLine(" global::System.Data.CommandBehavior.SingleResult,")
.AppendLine("cancellationToken: cancellationToken);")
.DecrementIndent()
.DecrementIndent()
.AppendLine("}")
.AppendLine();
}

Expand All @@ -192,25 +199,33 @@ private static void WriteQuerySingleEntity(IndentedStringBuilder codeBuilder, En
.Append(entity.EntityNamespace)
.Append(".")
.Append(entity.EntityName)
.Append(" QuerySingle")
.Append(entity.EntityName)
.AppendLine("(")
.AppendLine(" QuerySingle<TEntity>(")
.IncrementIndent()
.AppendLine("this global::FluentCommand.IDataQuery dataQuery)")
.Append("=> dataQuery.QuerySingle<")
.Append("where TEntity : ")
.Append(entity.EntityNamespace)
.Append(".")
.AppendLine(entity.EntityName)
.DecrementIndent()
.AppendLine("{")
.IncrementIndent()
.Append("return dataQuery.QuerySingle<")
.Append(entity.EntityNamespace)
.Append(".")
.Append(entity.EntityName)
.AppendLine(">(")
.IncrementIndent()
.Append("factory: ")
.Append(entity.EntityName)
.Append("DataReaderFactoryExtensions.")
.Append("DataReaderExtensions.")
.Append(entity.EntityName)
.AppendLine("Factory,")
.AppendLine("commandBehavior: global::System.Data.CommandBehavior.SequentialAccess | global::System.Data.CommandBehavior.SingleResult | global::System.Data.CommandBehavior.SingleRow);")
.AppendLine("commandBehavior: global::System.Data.CommandBehavior.SequentialAccess |")
.AppendLine(" global::System.Data.CommandBehavior.SingleResult |")
.AppendLine(" global::System.Data.CommandBehavior.SingleRow);")
.DecrementIndent()
.DecrementIndent()
.AppendLine("}")
.AppendLine();
}

Expand All @@ -237,25 +252,32 @@ private static void WriteQueryEntity(IndentedStringBuilder codeBuilder, EntityCl
.Append(entity.EntityNamespace)
.Append(".")
.Append(entity.EntityName)
.Append("> Query")
.Append(entity.EntityName)
.AppendLine("(")
.AppendLine("> Query<TEntity>(")
.IncrementIndent()
.AppendLine("this global::FluentCommand.IDataQuery dataQuery)")
.Append("=> dataQuery.Query<")
.Append("where TEntity : ")
.Append(entity.EntityNamespace)
.Append(".")
.AppendLine(entity.EntityName)
.DecrementIndent()
.AppendLine("{")
.IncrementIndent()
.Append("return dataQuery.Query<")
.Append(entity.EntityNamespace)
.Append(".")
.Append(entity.EntityName)
.AppendLine(">(")
.IncrementIndent()
.Append("factory: ")
.Append(entity.EntityName)
.Append("DataReaderFactoryExtensions.")
.Append("DataReaderExtensions.")
.Append(entity.EntityName)
.AppendLine("Factory,")
.AppendLine("commandBehavior: global::System.Data.CommandBehavior.SequentialAccess | global::System.Data.CommandBehavior.SingleResult);")
.AppendLine("commandBehavior: global::System.Data.CommandBehavior.SequentialAccess |")
.AppendLine(" global::System.Data.CommandBehavior.SingleResult);")
.DecrementIndent()
.DecrementIndent()
.AppendLine("}")
.AppendLine();
}

Expand Down

0 comments on commit 7251390

Please sign in to comment.