From a0275077bbbb531bd0c79e6fc0f26a6b4ac510dd Mon Sep 17 00:00:00 2001
From: zqlovejyc <943620963@qq.com>
Date: Wed, 23 Jun 2021 11:20:30 +0800
Subject: [PATCH] =?UTF-8?q?Table=E3=80=81Key=E3=80=81Column=E7=89=B9?=
=?UTF-8?q?=E6=80=A7=E6=96=B0=E5=A2=9EFormat=E5=B1=9E=E6=80=A7=EF=BC=8C?=
=?UTF-8?q?=E7=94=A8=E4=BA=8E=E6=9F=90=E4=BA=9B=E4=B8=8E=E6=95=B0=E6=8D=AE?=
=?UTF-8?q?=E5=BA=93=E5=85=B3=E9=94=AE=E5=AD=97=E9=87=8D=E5=90=8D=E7=9A=84?=
=?UTF-8?q?=E8=A1=A8=E5=90=8D=E3=80=81=E5=88=97=E5=90=8D=E5=8D=95=E7=8B=AC?=
=?UTF-8?q?=E8=BF=9B=E8=A1=8C=E6=A0=BC=E5=BC=8F=E5=8C=96=EF=BC=9B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
SQLBuilder.Core.UnitTest/Models/TestTable.cs | 21 ++++++
SQLBuilder.Core.UnitTest/SelectTest.cs | 15 ++++
SQLBuilder.Core/Attributes/ColumnAttribute.cs | 5 ++
SQLBuilder.Core/Attributes/KeyAttribute.cs | 5 ++
SQLBuilder.Core/Attributes/TableAttribute.cs | 5 ++
SQLBuilder.Core/Entry/SqlBuilderCore.cs | 4 +-
SQLBuilder.Core/Entry/SqlWrapper.cs | 70 +++++++++++++------
7 files changed, 102 insertions(+), 23 deletions(-)
create mode 100644 SQLBuilder.Core.UnitTest/Models/TestTable.cs
diff --git a/SQLBuilder.Core.UnitTest/Models/TestTable.cs b/SQLBuilder.Core.UnitTest/Models/TestTable.cs
new file mode 100644
index 0000000..8f1b1ff
--- /dev/null
+++ b/SQLBuilder.Core.UnitTest/Models/TestTable.cs
@@ -0,0 +1,21 @@
+using SQLBuilder.Core.Attributes;
+
+namespace SQLBuilder.Core.UnitTest
+{
+ [Table("Base_Test", Format = true)]
+ public class TestTable
+ {
+ [Key]
+ public int Id { get; set; }
+
+ public string Name { get; set; }
+
+ [Column(Format = true)]
+ public string Group { get; set; }
+
+ [Column(Format = true)]
+ public string Order { get; set; }
+
+ public string OtherName { get; set; }
+ }
+}
diff --git a/SQLBuilder.Core.UnitTest/SelectTest.cs b/SQLBuilder.Core.UnitTest/SelectTest.cs
index 6afcc28..b6a2081 100644
--- a/SQLBuilder.Core.UnitTest/SelectTest.cs
+++ b/SQLBuilder.Core.UnitTest/SelectTest.cs
@@ -3137,6 +3137,21 @@ public void Test_Select_113()
Assert.AreEqual("SELECT x.Name AS TeacherName,y.Name AS ClassName FROM Base_Teacher AS x INNER JOIN Base_Class AS y ON x.ClassId = y.Id WHERE x.Type = @p__1 AND x.Name IS NOT NULL", builder.Sql);
Assert.AreEqual(1, builder.Parameters.Count);
}
+
+ ///
+ /// 查询114
+ ///
+ [TestMethod]
+ public void Test_Select_114()
+ {
+ var builder = SqlBuilder
+ .Select(x =>
+ new { x.Id, x.Name, x.OtherName, OrderName = x.Order, GroupName = x.Group })
+ .Where(x => x.Id == 1);
+
+ Assert.AreEqual("SELECT x.Id,x.Name,x.OtherName,x.[Order] AS OrderName,x.[Group] AS GroupName FROM [Base_Test] AS x WHERE x.Id = @p__1", builder.Sql);
+ Assert.AreEqual(1, builder.Parameters.Count);
+ }
#endregion
#region Page
diff --git a/SQLBuilder.Core/Attributes/ColumnAttribute.cs b/SQLBuilder.Core/Attributes/ColumnAttribute.cs
index 0103314..e79f540 100644
--- a/SQLBuilder.Core/Attributes/ColumnAttribute.cs
+++ b/SQLBuilder.Core/Attributes/ColumnAttribute.cs
@@ -46,5 +46,10 @@ public class ColumnAttribute : Attribute
/// 更新是否有效
///
public bool Update { get; set; } = true;
+
+ ///
+ /// 是否启用格式化,用于全局不启用格式化时,该列名为数据库关键字,此时需要单独启用格式化
+ ///
+ public bool Format { get; set; }
}
}
diff --git a/SQLBuilder.Core/Attributes/KeyAttribute.cs b/SQLBuilder.Core/Attributes/KeyAttribute.cs
index 1c2b109..617d69e 100644
--- a/SQLBuilder.Core/Attributes/KeyAttribute.cs
+++ b/SQLBuilder.Core/Attributes/KeyAttribute.cs
@@ -41,5 +41,10 @@ public class KeyAttribute : Attribute
/// 是否自增列
///
public bool Identity { get; set; }
+
+ ///
+ /// 是否启用格式化,用于全局不启用格式化时,该列名为数据库关键字,此时需要单独启用格式化
+ ///
+ public bool Format { get; set; }
}
}
diff --git a/SQLBuilder.Core/Attributes/TableAttribute.cs b/SQLBuilder.Core/Attributes/TableAttribute.cs
index 1670aff..1b7ace2 100644
--- a/SQLBuilder.Core/Attributes/TableAttribute.cs
+++ b/SQLBuilder.Core/Attributes/TableAttribute.cs
@@ -41,5 +41,10 @@ public class TableAttribute : Attribute
/// 数据库模式
///
public string Schema { get; set; }
+
+ ///
+ /// 是否启用格式化,用于全局不启用格式化时,该表名为数据库关键字,此时需要单独启用格式化
+ ///
+ public bool Format { get; set; }
}
}
diff --git a/SQLBuilder.Core/Entry/SqlBuilderCore.cs b/SQLBuilder.Core/Entry/SqlBuilderCore.cs
index 8734b64..40fbf34 100644
--- a/SQLBuilder.Core/Entry/SqlBuilderCore.cs
+++ b/SQLBuilder.Core/Entry/SqlBuilderCore.cs
@@ -4574,7 +4574,7 @@ public SqlBuilderCore Page(int pageSize, int pageIndex, string orderField, st
if (sql.IsNotNullOrEmpty())
{
this.sqlWrapper.DbParameters.Clear();
- if (parameters != null)
+ if (parameters.IsNotNullOrEmpty())
this.sqlWrapper.DbParameters = parameters;
}
@@ -4649,7 +4649,7 @@ public SqlBuilderCore PageByWith(int pageSize, int pageIndex, string orderFie
if (sql.IsNotNullOrEmpty())
{
this.sqlWrapper.DbParameters.Clear();
- if (parameters != null)
+ if (parameters.IsNotNullOrEmpty())
this.sqlWrapper.DbParameters = parameters;
}
diff --git a/SQLBuilder.Core/Entry/SqlWrapper.cs b/SQLBuilder.Core/Entry/SqlWrapper.cs
index fb514ce..3bc86dd 100644
--- a/SQLBuilder.Core/Entry/SqlWrapper.cs
+++ b/SQLBuilder.Core/Entry/SqlWrapper.cs
@@ -21,7 +21,6 @@
using System.Linq;
using System.Reflection;
using System.Text;
-using System.Text.RegularExpressions;
using System.ComponentModel.DataAnnotations.Schema;
using SQLBuilder.Core.Extensions;
using SQLBuilder.Core.Enums;
@@ -51,7 +50,12 @@ public class SqlWrapper
///
/// 表别名字典
///
- private readonly Dictionary aliasDictionary;
+ private readonly Dictionary _aliasDictionary;
+
+ ///
+ /// 格式化列名缓存,多用于以数据库关键字命名的列
+ ///
+ private readonly List _formatColumns;
#endregion
#region Public Property
@@ -165,11 +169,12 @@ public string FormatTemplate
///
public SqlWrapper()
{
- this.Sql = new StringBuilder();
- this.SelectFields = new List();
- this.DbParameters = new Dictionary();
- this.aliasDictionary = new Dictionary();
- this.JoinTypes = new List();
+ this.Sql = new();
+ this.SelectFields = new();
+ this.DbParameters = new();
+ this.JoinTypes = new();
+ this._aliasDictionary = new();
+ this._formatColumns = new();
}
#endregion
@@ -405,7 +410,8 @@ public void Clear()
this.Sql.Clear();
this.DbParameters.Clear();
this.SelectFields.Clear();
- this.aliasDictionary.Clear();
+ this._aliasDictionary.Clear();
+ this._formatColumns.Clear();
}
#endregion
@@ -493,13 +499,13 @@ public void AddDbParameter(object parameterValue, string parameterKey = null)
else if (parameterKey.IsNullOrEmpty())
{
var name = this.DbParameterPrefix + "p__" + (this.DbParameters.Count + 1);
- this.DbParameters.Add(name, parameterValue);
+ this.DbParameters.TryAdd(name, parameterValue);
this.Sql.Append(name);
}
else
{
var name = this.DbParameterPrefix + parameterKey;
- this.DbParameters.Add(name, parameterValue);
+ this.DbParameters.TryAdd(name, parameterValue);
this.Sql.Append(name);
}
}
@@ -519,9 +525,9 @@ public bool SetTableAlias(string tableName, string tableAlias = null)
tableAlias = this.GetFormatName(tableAlias);
- if (!this.aliasDictionary.Keys.Contains(tableAlias))
+ if (!this._aliasDictionary.Keys.Contains(tableAlias))
{
- this.aliasDictionary.Add(tableAlias, tableName);
+ this._aliasDictionary.Add(tableAlias, tableName);
return true;
}
@@ -545,13 +551,13 @@ public string GetTableAlias(string tableName, string tableAlias = null)
tableAlias = this.GetFormatName(tableAlias);
//表别名+表名 同时满足
- if (aliasDictionary.Keys.Contains(tableAlias) && aliasDictionary[tableAlias] == tableName)
+ if (_aliasDictionary.Keys.Contains(tableAlias) && _aliasDictionary[tableAlias] == tableName)
return tableAlias;
}
//根据表名获取别名
- if (aliasDictionary.Values.Contains(tableName))
- return aliasDictionary.FirstOrDefault(x => x.Value == tableName).Key;
+ if (_aliasDictionary.Values.Contains(tableName))
+ return _aliasDictionary.FirstOrDefault(x => x.Value == tableName).Key;
}
return string.Empty;
}
@@ -561,11 +567,12 @@ public string GetTableAlias(string tableName, string tableAlias = null)
///
/// 获取格式化名称
///
- ///
+ /// 待格式化的原始名称
+ /// 是否强制指定格式化
///
- public string GetFormatName(string name)
+ public string GetFormatName(string name, bool format = false)
{
- if (this.IsEnableFormat &&
+ if ((format || this.IsEnableFormat) &&
!name.IsNullOrEmpty() &&
!name.StartsWith("[") &&
!name.StartsWith("`") &&
@@ -585,13 +592,14 @@ public string GetFormatName(string name)
public string GetTableName(Type type)
{
var tableName = this.GetFormatName(type.Name);
+
if (type.GetFirstOrDefaultAttribute() is CusTableAttribute cta)
{
if (cta.Name.IsNotNullOrEmpty())
- tableName = this.GetFormatName(cta.Name);
+ tableName = this.GetFormatName(cta.Name, cta.Format);
if (cta.Schema.IsNotNullOrEmpty())
- tableName = $"{this.GetFormatName(cta.Schema)}.{tableName}";
+ tableName = $"{this.GetFormatName(cta.Schema, cta.Format)}.{tableName}";
}
else if (type.GetFirstOrDefaultAttribute() is SysTableAttribute sta)
{
@@ -601,6 +609,7 @@ public string GetTableName(Type type)
if (sta.Schema.IsNotNullOrEmpty())
tableName = $"{this.GetFormatName(sta.Schema)}.{tableName}";
}
+
return tableName;
}
#endregion
@@ -611,7 +620,12 @@ public string GetTableName(Type type)
///
/// 列名
///
- public string GetColumnName(string columnName) => this.GetFormatName(columnName);
+ public string GetColumnName(string columnName)
+ {
+ var format = _formatColumns.Any(x => x.EqualIgnoreCase(columnName));
+
+ return this.GetFormatName(columnName, format);
+ }
#endregion
#region GetColumnInfo
@@ -626,6 +640,7 @@ public string GetTableName(Type type)
string columnName = null;
var isInsert = true;
var isUpdate = true;
+ var format = false;
var props = type.GetProperties();
//判断列是否包含Column特性
@@ -641,6 +656,7 @@ public string GetTableName(Type type)
columnName = cca.Name;
isInsert = cca.Insert;
isUpdate = cca.Update;
+ format = cca.Format;
}
else if (member.GetFirstOrDefaultAttribute() is SysColumnAttribute sca)
columnName = sca.Name;
@@ -654,6 +670,7 @@ public string GetTableName(Type type)
columnName = cus.Name;
isInsert = cus.Insert;
isUpdate = cus.Update;
+ format = cus.Format;
}
else if (p.GetFirstOrDefaultAttribute() is SysColumnAttribute sys)
columnName = sys.Name;
@@ -670,6 +687,7 @@ public string GetTableName(Type type)
if (member.GetFirstOrDefaultAttribute() is CusKeyAttribute cka)
{
isUpdate = false;
+ format = cka.Format;
if (cka.Identity)
isInsert = false;
@@ -696,6 +714,7 @@ public string GetTableName(Type type)
if (p.GetFirstOrDefaultAttribute() is CusKeyAttribute cus)
{
isUpdate = false;
+ format = cus.Format;
if (cus.Identity)
isInsert = false;
@@ -718,6 +737,9 @@ public string GetTableName(Type type)
}
}
+ if (format)
+ _formatColumns.Add(columnName);
+
return (this.GetColumnName(columnName), isInsert, isUpdate);
}
#endregion
@@ -749,9 +771,15 @@ public string GetTableName(Type type)
string keyName = null;
if (property?.GetFirstOrDefaultAttribute() is CusKeyAttribute cka)
+ {
keyName = cka.Name ?? propertyName;
+ if (cka.Format)
+ _formatColumns.Add(keyName);
+ }
else if (property?.GetFirstOrDefaultAttribute() is SysKeyAttribute ska)
+ {
keyName = propertyName;
+ }
result.Add((this.GetColumnName(keyName), propertyName));
}