Skip to content

Commit

Permalink
Improvements and packages update, Release 4.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagoloureiro committed May 6, 2023
1 parent a9598bc commit e6f6ace
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 62 deletions.
16 changes: 8 additions & 8 deletions Dapper.Crud.Tests/Dapper.Crud.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,28 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.2" />
<PackageReference Include="xunit">
<Version>2.4.1</Version>
<Version>2.4.2</Version>
</PackageReference>
<PackageReference Include="xunit.abstractions">
<Version>2.0.3</Version>
</PackageReference>
<PackageReference Include="xunit.analyzers">
<Version>0.10.0</Version>
<Version>1.1.0</Version>
</PackageReference>
<PackageReference Include="xunit.assert">
<Version>2.4.1</Version>
<Version>2.4.2</Version>
</PackageReference>
<PackageReference Include="xunit.core">
<Version>2.4.1</Version>
<Version>2.4.2</Version>
</PackageReference>
<PackageReference Include="xunit.extensibility.core">
<Version>2.4.1</Version>
<Version>2.4.2</Version>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio">
<Version>2.4.1</Version>
<Version>2.4.5</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
6 changes: 3 additions & 3 deletions Dapper.Crud.VSExtension/CreateCrudPackage.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Threading;
using EnvDTE;
using EnvDTE80;
using Task = System.Threading.Tasks.Task;

namespace Dapper.Crud.VSExtension
Expand Down
45 changes: 14 additions & 31 deletions Dapper.Crud.VSExtension/Dapper.Crud.VSExtension.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -338,38 +338,33 @@
<Version>2.2.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform">
<Version>4.1.0-preview1</Version>
<Version>4.1.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.Net.Compilers">
<Version>4.2.0</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Net.Compilers.Toolset">
<Version>4.2.0</Version>
<Version>4.5.0</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.CoreUtility" Version="16.8.239" />
<PackageReference Include="Microsoft.VisualStudio.Imaging" Version="16.7.30329.63" />
<PackageReference Include="Microsoft.VisualStudio.OLE.Interop" Version="16.7.30328.74" />
<PackageReference Include="Microsoft.VisualStudio.CoreUtility" Version="17.5.279" />
<PackageReference Include="Microsoft.VisualStudio.Imaging" Version="17.5.33428.366" />
<PackageReference Include="Microsoft.VisualStudio.OLE.Interop" Version="17.5.33428.366" />
<PackageReference Include="Microsoft.VisualStudio.SDK.EmbedInteropTypes" Version="15.0.36" />
<PackageReference Include="Microsoft.VisualStudio.Shell.15.0">
<Version>16.7.30329.88</Version>
<Version>17.5.33428.388</Version>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Shell.Framework" Version="16.7.30329.88" />
<PackageReference Include="Microsoft.VisualStudio.Shell.Interop" Version="16.7.30328.74" />
<PackageReference Include="Microsoft.VisualStudio.Shell.Interop.10.0" Version="16.7.30328.74" />
<PackageReference Include="Microsoft.VisualStudio.Shell.Interop.11.0" Version="16.7.30328.74" />
<PackageReference Include="Microsoft.VisualStudio.Shell.Interop.12.0" Version="16.7.30328.74" />
<PackageReference Include="Microsoft.VisualStudio.Shell.Interop.8.0" Version="16.7.30328.74" />
<PackageReference Include="Microsoft.VisualStudio.Shell.Interop.9.0" Version="16.7.30328.74" />
<PackageReference Include="Microsoft.VisualStudio.TextManager.Interop" Version="16.7.30328.74" />
<PackageReference Include="Microsoft.VisualStudio.TextManager.Interop.8.0" Version="16.7.30328.74" />
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="16.8.55" />
<PackageReference Include="Microsoft.VisualStudio.Utilities" Version="16.7.30329.38" />
<PackageReference Include="Microsoft.VisualStudio.Validation" Version="16.8.33" />
<PackageReference Include="Microsoft.VSSDK.BuildTools" Version="17.0.5232">
<PackageReference Include="Microsoft.VisualStudio.Shell.Framework" Version="17.5.33428.388" />
<PackageReference Include="Microsoft.VisualStudio.Shell.Interop" Version="17.5.33428.366" />
<PackageReference Include="Microsoft.VisualStudio.TextManager.Interop" Version="17.5.33428.366" />
<PackageReference Include="Microsoft.VisualStudio.TextManager.Interop.8.0" Version="17.5.33428.366" />
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="17.5.22" />
<PackageReference Include="Microsoft.VisualStudio.Utilities" Version="17.5.33428.366" />
<PackageReference Include="Microsoft.VisualStudio.Validation" Version="17.6.11" />
<PackageReference Include="Microsoft.VSSDK.BuildTools" Version="17.5.4074">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down Expand Up @@ -400,18 +395,6 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Reference Include="EnvDTE, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<EmbedInteropTypes>False</EmbedInteropTypes>
</Reference>
<Reference Include="EnvDTE100, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<EmbedInteropTypes>False</EmbedInteropTypes>
</Reference>
<Reference Include="EnvDTE80, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<EmbedInteropTypes>False</EmbedInteropTypes>
</Reference>
<Reference Include="EnvDTE90, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<EmbedInteropTypes>False</EmbedInteropTypes>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.VisualStudio.CommandBars, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<EmbedInteropTypes>False</EmbedInteropTypes>
Expand Down
4 changes: 3 additions & 1 deletion Dapper.Crud.VSExtension/Helpers/ClassGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ public static class ClassGenerator
{
public static string GenerateClassBody(string model, bool interfaceEnabled)
{
var space = " ";
model = FixClassName(model);
var sb = new StringBuilder();
sb.AppendLine(interfaceEnabled
? $"public class {model}Repository : I{model}Repository"
: $"public class {model}Repository");
sb.AppendLine("{");

sb.AppendLine(space + "private readonly string _connstring;");
sb.AppendLine();
return sb.ToString();
}

Expand Down
53 changes: 43 additions & 10 deletions Dapper.Crud.VSExtension/Helpers/DapperGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public static string SelectJoin(List<string> models, List<IList<PropertyInfo>> p
sb.AppendLine(space + "// Select");
sb.AppendLine(space + $"List<{models[0]}> ret;");
if (awaitUsing)
sb.AppendLine(space + "await using (var db = new SqlConnection(connstring))");
sb.AppendLine(space + "await using (var db = new SqlConnection(_connstring))");
else
sb.AppendLine(space + "using (var db = new SqlConnection(connstring))"); sb.AppendLine(space + "{");
sb.AppendLine(space + "using (var db = new SqlConnection(_connstring))"); sb.AppendLine(space + "{");
sb.AppendLine(space + $" string sql = @\"SELECT {prop} FROM [{models[0]}] D0");

for (int i = 1; i < arrProp.Length; i++)
Expand Down Expand Up @@ -62,9 +62,9 @@ public static string Select(string model, IList<PropertyInfo> properties, bool g

sb.AppendLine(space + "// Select");
if (awaitUsing)
sb.AppendLine(space + "await using (var db = new SqlConnection(connstring))");
sb.AppendLine(space + "await using (var db = new SqlConnection(_connstring))");
else
sb.AppendLine(space + "using (var db = new SqlConnection(connstring))");
sb.AppendLine(space + "using (var db = new SqlConnection(_connstring))");

sb.AppendLine(space + "{");
sb.AppendLine(space + $" string sql = @\"SELECT {prop} FROM [{model}]\";");
Expand All @@ -80,6 +80,39 @@ public static string Select(string model, IList<PropertyInfo> properties, bool g
return sb.ToString();
}

public static string SelectById(string model, IList<PropertyInfo> properties, bool generateMethod, bool generateClass, bool async, bool awaitUsing)
{
model = FixClassName(model);
var space = "";

if (generateMethod)
space = " ";
if (generateClass)
space += " ";

var sb = new StringBuilder();
var prop = GenerateProperties(properties, false);

sb.AppendLine(space + "// Select");
if (awaitUsing)
sb.AppendLine(space + "await using (var db = new SqlConnection(_connstring))");
else
sb.AppendLine(space + "using (var db = new SqlConnection(_connstring))");

sb.AppendLine(space + "{");
sb.AppendLine(space + $" string sql = @\"SELECT {prop} FROM [{model}] WHERE {properties[0].Name} = @id\";");
sb.AppendLine("");

if (async)
sb.AppendLine(space + $" return await db.QueryFirstOrDefaultAsync<{model}>(sql, new {{ id }}, commandType: CommandType.Text);");
else
sb.AppendLine(space + $" return db.QueryFirstOrDefault<{model}>(sql, new {{ id }}, commandType: CommandType.Text);");

sb.AppendLine(space + "}");

return sb.ToString();
}

private static string FixClassName(string model)
{
if (model.Contains("\\"))
Expand Down Expand Up @@ -111,9 +144,9 @@ public static string Insert(string model, IList<PropertyInfo> properties, bool g

sb.AppendLine(space + "// Insert");
if (awaitUsing)
sb.AppendLine(space + "await using (var db = new SqlConnection(connstring))");
sb.AppendLine(space + "await using (var db = new SqlConnection(_connstring))");
else
sb.AppendLine(space + "using (var db = new SqlConnection(connstring))"); sb.AppendLine(space + "{");
sb.AppendLine(space + "using (var db = new SqlConnection(_connstring))"); sb.AppendLine(space + "{");

if (insertedId)
sb.AppendLine(space + $" string sql = @\"INSERT INTO [{model}] ({prop}) VALUES ({propAt});select @@IDENTITY;\";");
Expand Down Expand Up @@ -171,9 +204,9 @@ public static string Update(string model, IList<PropertyInfo> properties, bool g

sb.AppendLine(space + "// Update");
if (awaitUsing)
sb.AppendLine(space + "await using (var db = new SqlConnection(connstring))");
sb.AppendLine(space + "await using (var db = new SqlConnection(_connstring))");
else
sb.AppendLine(space + "using (var db = new SqlConnection(connstring))"); sb.AppendLine(space + "{");
sb.AppendLine(space + "using (var db = new SqlConnection(_connstring))"); sb.AppendLine(space + "{");

sb.AppendLine(space + $" string sql = @\"UPDATE [{model}] SET {GenerateUpdateValues(properties)} WHERE {propId.Name} = @{propId.Name}\";");
sb.AppendLine("");
Expand Down Expand Up @@ -211,9 +244,9 @@ public static string Delete(string model, IList<PropertyInfo> properties, bool g

sb.AppendLine(space + "// Delete");
if (awaitUsing)
sb.AppendLine(space + "await using (var db = new SqlConnection(connstring))");
sb.AppendLine(space + "await using (var db = new SqlConnection(_connstring))");
else
sb.AppendLine(space + "using (var db = new SqlConnection(connstring))"); sb.AppendLine(space + "{");
sb.AppendLine(space + "using (var db = new SqlConnection(_connstring))"); sb.AppendLine(space + "{");

sb.AppendLine(space + $" string sql = @\"DELETE FROM [{model}] WHERE {properties[0].Name} = @{properties[0].Name}\";");
sb.AppendLine("");
Expand Down
14 changes: 14 additions & 0 deletions Dapper.Crud.VSExtension/Helpers/InterfaceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ public static string GenerateSelect(string model, bool async)
return sb.ToString();
}

public static string GenerateSelectById(string model, bool async)
{
model = FixClassName(model);

var sb = new StringBuilder();

if (async)
sb.AppendLine($" Task<{model}> Select{model}ById(int id);");
else
sb.AppendLine($" {model} Select{model}ById(int id);");

return sb.ToString();
}

public static string GenerateInsert(string model, bool async, bool insertedId)
{
model = FixClassName(model);
Expand Down
22 changes: 22 additions & 0 deletions Dapper.Crud.VSExtension/Helpers/MethodGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ public static string GenerateSelect(string content, string model, bool generateC
return sb.ToString();
}

public static string GenerateSelectById(string content, string model, bool generateClass, bool async)
{
var space = "";
model = FixClassName(model);

if (generateClass)
space = " ";

var sb = new StringBuilder();

if (async)
sb.AppendLine(space + $"public async Task<{model}> Select{model}ById(int id)");
else
sb.AppendLine(space + $"public {model} Select{model}ById(int id)");

sb.AppendLine(space + "{");
sb.Append(content);
sb.AppendLine(space + "}");

return sb.ToString();
}

public static string GenerateInsert(string content, string model, bool generateClass, bool async, bool insertedId)
{
var space = "";
Expand Down
19 changes: 16 additions & 3 deletions Dapper.Crud.VSExtension/frmExtension.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e6f6ace

Please sign in to comment.