diff --git a/Dapper.Crud.Tests/Dapper.Crud.Tests.csproj b/Dapper.Crud.Tests/Dapper.Crud.Tests.csproj index 314cd9a..869c6d2 100644 --- a/Dapper.Crud.Tests/Dapper.Crud.Tests.csproj +++ b/Dapper.Crud.Tests/Dapper.Crud.Tests.csproj @@ -38,28 +38,28 @@ 4 - - + + - 2.4.1 + 2.4.2 2.0.3 - 0.10.0 + 1.1.0 - 2.4.1 + 2.4.2 - 2.4.1 + 2.4.2 - 2.4.1 + 2.4.2 - 2.4.1 + 2.4.5 runtime; build; native; contentfiles; analyzers all diff --git a/Dapper.Crud.VSExtension/CreateCrudPackage.cs b/Dapper.Crud.VSExtension/CreateCrudPackage.cs index bb73d64..ab58c70 100644 --- a/Dapper.Crud.VSExtension/CreateCrudPackage.cs +++ b/Dapper.Crud.VSExtension/CreateCrudPackage.cs @@ -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 diff --git a/Dapper.Crud.VSExtension/Dapper.Crud.VSExtension.csproj b/Dapper.Crud.VSExtension/Dapper.Crud.VSExtension.csproj index 4cb93a9..92af393 100644 --- a/Dapper.Crud.VSExtension/Dapper.Crud.VSExtension.csproj +++ b/Dapper.Crud.VSExtension/Dapper.Crud.VSExtension.csproj @@ -338,7 +338,7 @@ 2.2.0 - 4.1.0-preview1 + 4.1.0 4.2.0 @@ -346,30 +346,25 @@ all - 4.2.0 + 4.5.0 runtime; build; native; contentfiles; analyzers; buildtransitive all - - - + + + - 16.7.30329.88 + 17.5.33428.388 - - - - - - - - - - - - - + + + + + + + + runtime; build; native; contentfiles; analyzers all @@ -400,18 +395,6 @@ - - False - - - False - - - False - - - False - False diff --git a/Dapper.Crud.VSExtension/Helpers/ClassGenerator.cs b/Dapper.Crud.VSExtension/Helpers/ClassGenerator.cs index f4d0d77..92fd485 100644 --- a/Dapper.Crud.VSExtension/Helpers/ClassGenerator.cs +++ b/Dapper.Crud.VSExtension/Helpers/ClassGenerator.cs @@ -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(); } diff --git a/Dapper.Crud.VSExtension/Helpers/DapperGenerator.cs b/Dapper.Crud.VSExtension/Helpers/DapperGenerator.cs index 931dd88..6592fc7 100644 --- a/Dapper.Crud.VSExtension/Helpers/DapperGenerator.cs +++ b/Dapper.Crud.VSExtension/Helpers/DapperGenerator.cs @@ -29,9 +29,9 @@ public static string SelectJoin(List models, List> 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++) @@ -62,9 +62,9 @@ public static string Select(string model, IList 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}]\";"); @@ -80,6 +80,39 @@ public static string Select(string model, IList properties, bool g return sb.ToString(); } + public static string SelectById(string model, IList 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("\\")) @@ -111,9 +144,9 @@ public static string Insert(string model, IList 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;\";"); @@ -171,9 +204,9 @@ public static string Update(string model, IList 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(""); @@ -211,9 +244,9 @@ public static string Delete(string model, IList 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(""); diff --git a/Dapper.Crud.VSExtension/Helpers/InterfaceGenerator.cs b/Dapper.Crud.VSExtension/Helpers/InterfaceGenerator.cs index 75d02de..4322edc 100644 --- a/Dapper.Crud.VSExtension/Helpers/InterfaceGenerator.cs +++ b/Dapper.Crud.VSExtension/Helpers/InterfaceGenerator.cs @@ -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); diff --git a/Dapper.Crud.VSExtension/Helpers/MethodGenerator.cs b/Dapper.Crud.VSExtension/Helpers/MethodGenerator.cs index a057c17..ab9f7f1 100644 --- a/Dapper.Crud.VSExtension/Helpers/MethodGenerator.cs +++ b/Dapper.Crud.VSExtension/Helpers/MethodGenerator.cs @@ -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 = ""; diff --git a/Dapper.Crud.VSExtension/frmExtension.Designer.cs b/Dapper.Crud.VSExtension/frmExtension.Designer.cs index cb8add0..e9a2766 100644 --- a/Dapper.Crud.VSExtension/frmExtension.Designer.cs +++ b/Dapper.Crud.VSExtension/frmExtension.Designer.cs @@ -57,6 +57,7 @@ private void InitializeComponent() this.linkLabel1 = new System.Windows.Forms.LinkLabel(); this.btnChangeMode = new System.Windows.Forms.Button(); this.chkReturnIdentity = new System.Windows.Forms.CheckBox(); + this.SelectAllClass = new System.Windows.Forms.Button(); this.gBox.SuspendLayout(); this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.picLoader)).BeginInit(); @@ -221,7 +222,7 @@ private void InitializeComponent() // // btnSelectAll // - this.btnSelectAll.Location = new System.Drawing.Point(119, 0); + this.btnSelectAll.Location = new System.Drawing.Point(119, -1); this.btnSelectAll.Name = "btnSelectAll"; this.btnSelectAll.Size = new System.Drawing.Size(75, 23); this.btnSelectAll.TabIndex = 17; @@ -236,7 +237,7 @@ private void InitializeComponent() this.groupBox1.Controls.Add(this.chkClass); this.groupBox1.Controls.Add(this.chkGenerateMethod); this.groupBox1.Controls.Add(this.chkInterface); - this.groupBox1.Location = new System.Drawing.Point(12, 261); + this.groupBox1.Location = new System.Drawing.Point(12, 258); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(200, 136); this.groupBox1.TabIndex = 16; @@ -351,11 +352,22 @@ private void InitializeComponent() this.chkReturnIdentity.Text = "InsertedId"; this.chkReturnIdentity.UseVisualStyleBackColor = true; // + // SelectAllClass + // + this.SelectAllClass.Location = new System.Drawing.Point(131, 257); + this.SelectAllClass.Name = "SelectAllClass"; + this.SelectAllClass.Size = new System.Drawing.Size(75, 23); + this.SelectAllClass.TabIndex = 18; + this.SelectAllClass.Text = "Select All"; + this.SelectAllClass.UseVisualStyleBackColor = true; + this.SelectAllClass.Click += new System.EventHandler(this.SelectAllClass_Click); + // // frmExtension // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1281, 730); + this.Controls.Add(this.SelectAllClass); this.Controls.Add(this.chkReturnIdentity); this.Controls.Add(this.btnChangeMode); this.Controls.Add(this.linkLabel1); @@ -377,7 +389,7 @@ private void InitializeComponent() this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "frmExtension"; - this.Text = "Dapper Extension 3.0.14"; + this.Text = "Dapper Extension 4.0.1"; this.gBox.ResumeLayout(false); this.gBox.PerformLayout(); this.groupBox1.ResumeLayout(false); @@ -418,5 +430,6 @@ private void InitializeComponent() private System.Windows.Forms.Button btnChangeMode; private System.Windows.Forms.CheckBox chkReturnIdentity; private System.Windows.Forms.CheckBox chkAwaitUsing; + private System.Windows.Forms.Button SelectAllClass; } } \ No newline at end of file diff --git a/Dapper.Crud.VSExtension/frmExtension.cs b/Dapper.Crud.VSExtension/frmExtension.cs index 5f87cb2..4ca44ab 100644 --- a/Dapper.Crud.VSExtension/frmExtension.cs +++ b/Dapper.Crud.VSExtension/frmExtension.cs @@ -79,9 +79,17 @@ private void GenerateCrud() output += ClassGenerator.GenerateClassBody(model, chkInterface.Checked); if (chkSelect.Checked) + { output += MethodGenerator.GenerateSelect( - DapperGenerator.Select(model, properties, chkGenerateMethod.Checked, chkClass.Checked, chkAsync.Checked, chkAwaitUsing.Checked), + DapperGenerator.Select(model, properties, chkGenerateMethod.Checked, chkClass.Checked, + chkAsync.Checked, chkAwaitUsing.Checked), + model, chkClass.Checked, chkAsync.Checked); + + output += MethodGenerator.GenerateSelectById( + DapperGenerator.SelectById(model, properties, chkGenerateMethod.Checked, chkClass.Checked, + chkAsync.Checked, chkAwaitUsing.Checked), model, chkClass.Checked, chkAsync.Checked); + } if (chkInsert.Checked) output += MethodGenerator.GenerateInsert( @@ -110,8 +118,19 @@ private void GenerateCrud() if (chkGenerateMethod.Checked) { if (chkSelect.Checked) + { + txtOutput.Text += + MethodGenerator.GenerateSelect( + DapperGenerator.Select(model, properties, chkGenerateMethod.Checked, + chkClass.Checked, chkAsync.Checked, chkAwaitUsing.Checked), model, + chkClass.Checked, chkAsync.Checked); + txtOutput.Text += - MethodGenerator.GenerateSelect(DapperGenerator.Select(model, properties, chkGenerateMethod.Checked, chkClass.Checked, chkAsync.Checked, chkAwaitUsing.Checked), model, chkClass.Checked, chkAsync.Checked); + MethodGenerator.GenerateSelectById( + DapperGenerator.Select(model, properties, chkGenerateMethod.Checked, + chkClass.Checked, chkAsync.Checked, chkAwaitUsing.Checked), model, + chkClass.Checked, chkAsync.Checked); + } if (chkInsert.Checked) txtOutput.Text += @@ -146,8 +165,10 @@ private void GenerateCrud() output = InterfaceGenerator.GenerateInterfaceBody(model); if (chkSelect.Checked) + { output += InterfaceGenerator.GenerateSelect(model, chkAsync.Checked); - + output += InterfaceGenerator.GenerateSelectById(model, chkAsync.Checked); + } if (chkInsert.Checked) output += InterfaceGenerator.GenerateInsert(model, chkAsync.Checked, chkReturnIdentity.Checked); @@ -404,5 +425,14 @@ private void btnChangeMode_Click(object sender, EventArgs e) btnChangeMode.Text = _darkMode ? "White mode" : "Dark mode"; _darkMode = !_darkMode; } + + private void SelectAllClass_Click(object sender, EventArgs e) + { + chkClass.Checked = true; + chkGenerateMethod.Checked = true; + chkAsync.Checked = true; + chkInterface.Checked = true; + chkAwaitUsing.Checked = true; + } } } \ No newline at end of file diff --git a/Dapper.Crud.VSExtension/source.extension.vsixmanifest b/Dapper.Crud.VSExtension/source.extension.vsixmanifest index 2045172..7b81385 100644 --- a/Dapper.Crud.VSExtension/source.extension.vsixmanifest +++ b/Dapper.Crud.VSExtension/source.extension.vsixmanifest @@ -1,7 +1,7 @@  - + Dapper Crud Generator Generate CRUD easily with Dapper from your existing Models https://github.com/thiagoloureiro/Dapper.Crud.Extension @@ -14,8 +14,6 @@ amd64 - -