Skip to content

Commit

Permalink
Table、Key、Column特性新增Format属性,用于某些与数据库关键字重名的表名、列名单独进行格式化;
Browse files Browse the repository at this point in the history
  • Loading branch information
zqlovejyc committed Jun 23, 2021
1 parent f728fbd commit a027507
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 23 deletions.
21 changes: 21 additions & 0 deletions SQLBuilder.Core.UnitTest/Models/TestTable.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
15 changes: 15 additions & 0 deletions SQLBuilder.Core.UnitTest/SelectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
/// 查询114
/// </summary>
[TestMethod]
public void Test_Select_114()
{
var builder = SqlBuilder
.Select<TestTable>(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
Expand Down
5 changes: 5 additions & 0 deletions SQLBuilder.Core/Attributes/ColumnAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,10 @@ public class ColumnAttribute : Attribute
/// 更新是否有效
/// </summary>
public bool Update { get; set; } = true;

/// <summary>
/// 是否启用格式化,用于全局不启用格式化时,该列名为数据库关键字,此时需要单独启用格式化
/// </summary>
public bool Format { get; set; }
}
}
5 changes: 5 additions & 0 deletions SQLBuilder.Core/Attributes/KeyAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,10 @@ public class KeyAttribute : Attribute
/// 是否自增列
/// </summary>
public bool Identity { get; set; }

/// <summary>
/// 是否启用格式化,用于全局不启用格式化时,该列名为数据库关键字,此时需要单独启用格式化
/// </summary>
public bool Format { get; set; }
}
}
5 changes: 5 additions & 0 deletions SQLBuilder.Core/Attributes/TableAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,10 @@ public class TableAttribute : Attribute
/// 数据库模式
/// </summary>
public string Schema { get; set; }

/// <summary>
/// 是否启用格式化,用于全局不启用格式化时,该表名为数据库关键字,此时需要单独启用格式化
/// </summary>
public bool Format { get; set; }
}
}
4 changes: 2 additions & 2 deletions SQLBuilder.Core/Entry/SqlBuilderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4574,7 +4574,7 @@ public SqlBuilderCore<T> 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;
}

Expand Down Expand Up @@ -4649,7 +4649,7 @@ public SqlBuilderCore<T> PageByWith(int pageSize, int pageIndex, string orderFie
if (sql.IsNotNullOrEmpty())
{
this.sqlWrapper.DbParameters.Clear();
if (parameters != null)
if (parameters.IsNotNullOrEmpty())
this.sqlWrapper.DbParameters = parameters;
}

Expand Down
70 changes: 49 additions & 21 deletions SQLBuilder.Core/Entry/SqlWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,7 +50,12 @@ public class SqlWrapper
/// <summary>
/// 表别名字典
/// </summary>
private readonly Dictionary<string, string> aliasDictionary;
private readonly Dictionary<string, string> _aliasDictionary;

/// <summary>
/// 格式化列名缓存,多用于以数据库关键字命名的列
/// </summary>
private readonly List<string> _formatColumns;
#endregion

#region Public Property
Expand Down Expand Up @@ -165,11 +169,12 @@ public string FormatTemplate
/// </summary>
public SqlWrapper()
{
this.Sql = new StringBuilder();
this.SelectFields = new List<string>();
this.DbParameters = new Dictionary<string, object>();
this.aliasDictionary = new Dictionary<string, string>();
this.JoinTypes = new List<Type>();
this.Sql = new();
this.SelectFields = new();
this.DbParameters = new();
this.JoinTypes = new();
this._aliasDictionary = new();
this._formatColumns = new();
}
#endregion

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -561,11 +567,12 @@ public string GetTableAlias(string tableName, string tableAlias = null)
/// <summary>
/// 获取格式化名称
/// </summary>
/// <param name="name"></param>
/// <param name="name">待格式化的原始名称</param>
/// <param name="format">是否强制指定格式化</param>
/// <returns></returns>
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("`") &&
Expand All @@ -585,13 +592,14 @@ public string GetFormatName(string name)
public string GetTableName(Type type)
{
var tableName = this.GetFormatName(type.Name);

if (type.GetFirstOrDefaultAttribute<CusTableAttribute>() 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<SysTableAttribute>() is SysTableAttribute sta)
{
Expand All @@ -601,6 +609,7 @@ public string GetTableName(Type type)
if (sta.Schema.IsNotNullOrEmpty())
tableName = $"{this.GetFormatName(sta.Schema)}.{tableName}";
}

return tableName;
}
#endregion
Expand All @@ -611,7 +620,12 @@ public string GetTableName(Type type)
/// </summary>
/// <param name="columnName">列名</param>
/// <returns></returns>
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
Expand All @@ -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特性
Expand All @@ -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<SysColumnAttribute>() is SysColumnAttribute sca)
columnName = sca.Name;
Expand All @@ -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<SysColumnAttribute>() is SysColumnAttribute sys)
columnName = sys.Name;
Expand All @@ -670,6 +687,7 @@ public string GetTableName(Type type)
if (member.GetFirstOrDefaultAttribute<CusKeyAttribute>() is CusKeyAttribute cka)
{
isUpdate = false;
format = cka.Format;

if (cka.Identity)
isInsert = false;
Expand All @@ -696,6 +714,7 @@ public string GetTableName(Type type)
if (p.GetFirstOrDefaultAttribute<CusKeyAttribute>() is CusKeyAttribute cus)
{
isUpdate = false;
format = cus.Format;

if (cus.Identity)
isInsert = false;
Expand All @@ -718,6 +737,9 @@ public string GetTableName(Type type)
}
}

if (format)
_formatColumns.Add(columnName);

return (this.GetColumnName(columnName), isInsert, isUpdate);
}
#endregion
Expand Down Expand Up @@ -749,9 +771,15 @@ public string GetTableName(Type type)
string keyName = null;

if (property?.GetFirstOrDefaultAttribute<CusKeyAttribute>() is CusKeyAttribute cka)
{
keyName = cka.Name ?? propertyName;
if (cka.Format)
_formatColumns.Add(keyName);
}
else if (property?.GetFirstOrDefaultAttribute<SysKeyAttribute>() is SysKeyAttribute ska)
{
keyName = propertyName;
}

result.Add((this.GetColumnName(keyName), propertyName));
}
Expand Down

0 comments on commit a027507

Please sign in to comment.