Skip to content

Commit

Permalink
优化sql解析格式化,修复特殊关键字未进行格式化造成的错误bug;更新版本号为1.0.3.6;
Browse files Browse the repository at this point in the history
  • Loading branch information
zqlovejyc committed Mar 23, 2020
1 parent f90481b commit 43349d9
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 132 deletions.
198 changes: 99 additions & 99 deletions SQLBuilder.Core.UnitTest/SelectTest.cs

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions SQLBuilder.Core/Expression/ConstantExpressionResolve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public override SqlPack GroupBy(ConstantExpression expression, SqlPack sqlPack)
sqlPack.SetTableAlias(tableName);
var tableAlias = sqlPack.GetTableAlias(tableName);
if (!string.IsNullOrEmpty(tableAlias)) tableAlias += ".";
sqlPack += tableAlias + expression.Value.ToString() + ",";
sqlPack += tableAlias + sqlPack.GetColumnName(expression.Value.ToString()) + ",";
return sqlPack;
}

Expand All @@ -91,7 +91,10 @@ public override SqlPack OrderBy(ConstantExpression expression, SqlPack sqlPack,
sqlPack.SetTableAlias(tableName);
var tableAlias = sqlPack.GetTableAlias(tableName);
if (!string.IsNullOrEmpty(tableAlias)) tableAlias += ".";
sqlPack += tableAlias + expression.Value.ToString();
var field = expression.Value.ToString();
if (!field.ToUpper().Contains(" ASC") && !field.ToUpper().Contains(" DESC"))
field = sqlPack.GetColumnName(field);
sqlPack += tableAlias + field;
if (orders?.Length > 0)
sqlPack += $" { (orders[0] == OrderType.Descending ? "DESC" : "ASC")}";
return sqlPack;
Expand Down
10 changes: 5 additions & 5 deletions SQLBuilder.Core/Expression/MemberExpressionResolve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public override SqlPack Select(MemberExpression expression, SqlPack sqlPack)
sqlPack.SetTableAlias(tableName);
string tableAlias = sqlPack.GetTableAlias(tableName);
if (!string.IsNullOrEmpty(tableAlias)) tableAlias += ".";
sqlPack.SelectFields.Add(tableAlias + sqlPack.GetColumnInfo(expression.Member.DeclaringType, expression.Member, string.IsNullOrEmpty(tableAlias)).columnName);
sqlPack.SelectFields.Add(tableAlias + sqlPack.GetColumnInfo(expression.Member.DeclaringType, expression.Member).columnName);
return sqlPack;
}

Expand All @@ -149,7 +149,7 @@ public override SqlPack Join(MemberExpression expression, SqlPack sqlPack)
sqlPack.SetTableAlias(tableName);
string tableAlias = sqlPack.GetTableAlias(tableName);
if (!string.IsNullOrEmpty(tableAlias)) tableAlias += ".";
sqlPack += tableAlias + sqlPack.GetColumnInfo(expression.Member.DeclaringType, expression.Member, string.IsNullOrEmpty(tableAlias)).columnName;
sqlPack += tableAlias + sqlPack.GetColumnInfo(expression.Member.DeclaringType, expression.Member).columnName;
return sqlPack;
}

Expand Down Expand Up @@ -177,7 +177,7 @@ public override SqlPack Where(MemberExpression expression, SqlPack sqlPack)
sqlPack.SetTableAlias(tableName);
var tableAlias = sqlPack.GetTableAlias(tableName);
if (!string.IsNullOrEmpty(tableAlias)) tableAlias += ".";
sqlPack += tableAlias + sqlPack.GetColumnInfo(expression.Member.DeclaringType, expression.Member, string.IsNullOrEmpty(tableAlias)).columnName;
sqlPack += tableAlias + sqlPack.GetColumnInfo(expression.Member.DeclaringType, expression.Member).columnName;
//字段是bool类型
if (expression.NodeType == ExpressionType.MemberAccess && expression.Type.GetCoreType() == typeof(bool))
{
Expand Down Expand Up @@ -243,7 +243,7 @@ public override SqlPack GroupBy(MemberExpression expression, SqlPack sqlPack)
if (!string.IsNullOrEmpty(tableAlias)) tableAlias += ".";
if (expression.Expression.NodeType == ExpressionType.Parameter)
{
sqlPack += tableAlias + sqlPack.GetColumnInfo(expression.Member.DeclaringType, expression.Member, false).columnName + ",";
sqlPack += tableAlias + sqlPack.GetColumnInfo(expression.Member.DeclaringType, expression.Member).columnName + ",";
}
if (expression.Expression.NodeType == ExpressionType.Constant)
{
Expand Down Expand Up @@ -303,7 +303,7 @@ public override SqlPack OrderBy(MemberExpression expression, SqlPack sqlPack, pa
if (!string.IsNullOrEmpty(tableAlias)) tableAlias += ".";
if (expression.Expression.NodeType == ExpressionType.Parameter)
{
sqlPack += tableAlias + sqlPack.GetColumnInfo(expression.Member.DeclaringType, expression.Member, false).columnName;
sqlPack += tableAlias + sqlPack.GetColumnInfo(expression.Member.DeclaringType, expression.Member).columnName;
if (orders?.Length > 0)
sqlPack += $" { (orders[0] == OrderType.Descending ? "DESC" : "ASC")}";
}
Expand Down
10 changes: 5 additions & 5 deletions SQLBuilder.Core/SQLBuilder.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyVersion>1.0.3.5</AssemblyVersion>
<AssemblyVersion>1.0.3.6</AssemblyVersion>
<Authors>张强</Authors>
<Version>1.0.3.5</Version>
<Version>1.0.3.6</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>Expression表达式转换为SQL语句,支持SqlServer、MySQL、Oracle、SQLite、PostgreSQL</Description>
<Description>Expression表达式转换为SQL语句,支持SqlServer、MySQL、Oracle、SQLite、PostgreSQL;基于Dapper实现了不同数据库对应的数据仓储Repository;</Description>
<Copyright>Copyright © 2018-2020 , 张强 [email protected]</Copyright>
<PackageId>Zq.SQLBuilder.Core</PackageId>
<PackageReleaseNotes>1.修复部分情况下Contains方法解析异常bug;</PackageReleaseNotes>
<FileVersion>1.0.3.5</FileVersion>
<PackageReleaseNotes>1.优化sql解析格式化,修复特殊关键字未进行格式化造成的错误bug;</PackageReleaseNotes>
<FileVersion>1.0.3.6</FileVersion>
<PackageProjectUrl>https://github.com/zqlovejyc/SQLBuilder.Core</PackageProjectUrl>
</PropertyGroup>

Expand Down
4 changes: 4 additions & 0 deletions SQLBuilder.Core/SqlBuilderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,8 @@ public SqlBuilderCore<T> OrderBy(Expression<Func<T, object>> expression, params
public SqlBuilderCore<T> Page(int pageSize, int pageIndex, string orderField, string sql = null, Dictionary<string, object> parameters = null)
{
var sb = new StringBuilder();
if (!orderField.ToUpper().Contains(" ASC") && !orderField.ToUpper().Contains(" DESC"))
orderField = this._sqlPack.GetColumnName(orderField);
if (!string.IsNullOrEmpty(sql))
{
this._sqlPack.DbParams.Clear();
Expand Down Expand Up @@ -858,6 +860,8 @@ public SqlBuilderCore<T> Page(int pageSize, int pageIndex, string orderField, st
public SqlBuilderCore<T> PageByWith(int pageSize, int pageIndex, string orderField, string sql = null, Dictionary<string, object> parameters = null)
{
var sb = new StringBuilder();
if (!orderField.ToUpper().Contains(" ASC") && !orderField.ToUpper().Contains(" DESC"))
orderField = this._sqlPack.GetColumnName(orderField);
if (!string.IsNullOrEmpty(sql))
{
this._sqlPack.DbParams.Clear();
Expand Down
58 changes: 37 additions & 21 deletions SQLBuilder.Core/SqlPack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,61 +281,77 @@ public string GetTableAlias(string tableName)
}
#endregion

#region GetFormatName
/// <summary>
/// GetFormatName
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public string GetFormatName(string name)
{
if (
name?.StartsWith("[") == false &&
name?.StartsWith("`") == false &&
name?.StartsWith("\"") == false)
{
name = string.Format(this.FormatTempl, name);
}
return name;
}
#endregion

#region GetTableName
/// <summary>
/// GetTableName
/// </summary>
/// <param name="type">类型</param>
/// <param name="isFormat">是否格式化</param>
/// <returns>string</returns>
public string GetTableName(Type type, bool isFormat = true)
public string GetTableName(Type type)
{
var tableName = isFormat || this.DatabaseType == DatabaseType.PostgreSQL ?
string.Format(this.FormatTempl, type.Name) :
type.Name;
var tableName = this.GetFormatName(type.Name);
if (type.GetCustomAttributes(typeof(CusTableAttribute), false).FirstOrDefault() is CusTableAttribute cta)
{
if (!string.IsNullOrEmpty(cta.Name))
{
tableName = isFormat || this.DatabaseType == DatabaseType.PostgreSQL ?
string.Format(this.FormatTempl, cta.Name) :
cta.Name;
tableName = this.GetFormatName(cta.Name);
}
if (!string.IsNullOrEmpty(cta.Schema))
{
tableName = isFormat || this.DatabaseType == DatabaseType.PostgreSQL ?
$"{string.Format(this.FormatTempl, cta.Schema)}.{tableName}" :
$"{cta.Schema}.{tableName}";
tableName = $"{this.GetFormatName(cta.Schema)}.{tableName}";
}
}
else if (type.GetCustomAttributes(typeof(SysTableAttribute), false).FirstOrDefault() is SysTableAttribute sta)
{
if (!string.IsNullOrEmpty(sta.Name))
{
tableName = isFormat || this.DatabaseType == DatabaseType.PostgreSQL ?
string.Format(this.FormatTempl, sta.Name) :
sta.Name;
tableName = this.GetFormatName(sta.Name);
}
if (!string.IsNullOrEmpty(sta.Schema))
{
tableName = isFormat || this.DatabaseType == DatabaseType.PostgreSQL ?
$"{string.Format(this.FormatTempl, sta.Schema)}.{tableName}" :
$"{sta.Schema}.{tableName}";
tableName = $"{this.GetFormatName(sta.Schema)}.{tableName}";
}
}
return tableName;
}
#endregion

#region GetColumnName
/// <summary>
/// GetFormatColumnName
/// </summary>
/// <param name="columnName">列名</param>
/// <returns></returns>
public string GetColumnName(string columnName) => this.GetFormatName(columnName);
#endregion

#region GetColumnInfo
/// <summary>
/// GetColumnInfo
/// </summary>
/// <param name="type">类型</param>
/// <param name="member">成员</param>
/// <param name="isFormat">是否格式化</param>
/// <returns>Tuple</returns>
public (string columnName, bool isInsert, bool isUpdate) GetColumnInfo(Type type, MemberInfo member, bool isFormat = true)
public (string columnName, bool isInsert, bool isUpdate) GetColumnInfo(Type type, MemberInfo member)
{
string columnName = null;
var isInsert = true;
Expand Down Expand Up @@ -385,7 +401,7 @@ public string GetTableName(Type type, bool isFormat = true)
{
isUpdate = false;
}
return ((isFormat || this.DatabaseType == DatabaseType.PostgreSQL ? this.GetFormatColumnName(columnName) : columnName), isInsert, isUpdate);
return (this.GetColumnName(columnName), isInsert, isUpdate);
}
#endregion

Expand Down Expand Up @@ -424,7 +440,7 @@ public string GetTableName(Type type, bool isFormat = true)
{
keyName = propertyName;
}
result.Add(((isFormat || this.DatabaseType == DatabaseType.PostgreSQL ? this.GetFormatColumnName(keyName) : keyName), propertyName));
result.Add((this.GetColumnName(keyName), propertyName));
}
}
return result;
Expand Down

0 comments on commit 43349d9

Please sign in to comment.