Skip to content

Commit

Permalink
- 增加 表达式树函数解析 byte[] Length;#505
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Oct 30, 2020
1 parent b86676d commit 454eb8c
Show file tree
Hide file tree
Showing 19 changed files with 369 additions and 20 deletions.
80 changes: 80 additions & 0 deletions FreeSql.Tests/FreeSql.Tests/FreeSql.Tests.xml

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

52 changes: 52 additions & 0 deletions FreeSql.Tests/FreeSql.Tests/Issues/505.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using FreeSql.DataAnnotations;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using Xunit;

namespace FreeSql.Tests.Issues
{
public class _505
{
[Fact]
public void ByteLengthTest()
{
TestLocal(g.sqlserver);
TestLocal(g.mysql);
TestLocal(g.pgsql);
TestLocal(g.oracle);
TestLocal(g.sqlite);
TestLocal(g.firebird);
TestLocal(g.dameng);
//TestLocal(g.kingbaseES); //
//TestLocal(g.shentong); // OCTET_LENGTH(xx) 返回结果32,值不符合
//TestLocal(g.msaccess); //lenb(xx) 返回结果 15,值不符合

void TestLocal(IFreeSql fsql)
{
var byteArray = Encoding.UTF8.GetBytes("我是中国人");
fsql.Delete<Model505>().Where("1=1").ExecuteAffrows();
fsql.Insert(new Model505 { ByteLength = byteArray.Length, ByteArray = byteArray }).ExecuteAffrows();

var item = fsql.Select<Model505>()
//.Where(x => x.ByteArray.Length == x.ByteLength)
.First(a => new { item = a, length = a.ByteArray.Length });

Assert.NotNull(item);
Assert.Equal(Encoding.UTF8.GetString(byteArray), Encoding.UTF8.GetString(item.item.ByteArray));
Assert.Equal(byteArray.Length, item.item.ByteLength);
Assert.Equal(byteArray.Length, item.length);
}
}
public class Model505
{
public Guid id { get; set; }
public byte[] ByteArray { get; set; }
public int ByteLength { get; set; }
}

}
}
163 changes: 163 additions & 0 deletions FreeSql.Tests/FreeSql.Tests/Issues/507.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
using FreeSql.DataAnnotations;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using Xunit;

namespace FreeSql.Tests.Issues
{
public class _507
{
[Fact]
public void SelectTest()
{
var fsql = g.sqlite;
var _bodyAuditRepository = fsql.GetRepository<BodyAuditEntity>();

var sql = _bodyAuditRepository.Select
.Include(a => a.ClassInfo)
.Count(out var total)
.OrderBy(true, a => a.ClassInfo.period)
.OrderBy(true, a => a.ClassInfo.className)
.Page(2, 10)
.ToList();
}
public class BodyAuditEntity
{
public string Id { get; set; }

/// <summary>
/// 班期标识-BodyScheduleEntity班期主键
/// </summary>
[Column(Name = "ClassId", StringLength = 70, Position = 2)]
public string ClassId { get; set; }

/// <summary>
/// 姓名
/// </summary>
[Column(Name = "Name", StringLength = 20, Position = 3)]
public string Name { get; set; }



/// <summary>
/// 出生日期
/// </summary>
[Column(Name = "Birthday", StringLength = 20, Position = 5)]
public string Birthday { get; set; }

/// <summary>
/// 身份证
/// </summary>
[Column(Name = "IdCard", StringLength = 20, Position = 6)]
public string IdCard { get; set; }

/// <summary>
/// 联系电话
/// </summary>
[Column(Name = "TelPhone", StringLength = 15, Position = 7)]
public string TelPhone { get; set; }

/// <summary>
/// 学校名称
/// </summary>
[Column(Name = "SchoolName", StringLength = 50, Position = 8)]
public string SchoolName { get; set; }

/// <summary>
/// 初潮年龄
/// </summary>
[Column(Name = "StuCCNL", StringLength = 20, Position = 9)]
public int? StuCCNL { get; set; }

/// <summary>
/// 联系地址
/// </summary>
[Column(Name = "Address", StringLength = 100, Position = 10)]
public string Address { get; set; }

/// <summary>
/// 审核人标识
/// </summary>
[Column(Name = "CheckUserId", StringLength = 70)]
public string CheckUserId { get; set; }

/// <summary>
/// 审核人
/// </summary>
[Column(Name = "CheckUserName", StringLength = 20)]
public string CheckUserName { get; set; }

/// <summary>
/// 审核时间
/// </summary>
[Column(Name = "CheckTime")]
public DateTime? CheckTime { get; set; }

/// <summary>
/// 拒绝原因
/// </summary>
[Column(Name = "RefuseReason", StringLength = 100)]
public string RefuseReason { get; set; }

/// <summary>
/// 采集时间
/// </summary>
[Column(Name = "CollectionDate")]
public DateTime? CollectionDate { get; set; }

/// <summary>
/// 附件标识(多附件)
/// </summary>
[Column(Name = "AttachmentId", StringLength = 500)]
public string AttachmentId { get; set; }

/// <summary>
/// 性别
/// </summary>
[Column(Name = "Sex", StringLength = 2, Position = 4)]
public string Sex { get; set; }

#region 导航属性
/// <summary>
/// 班期信息
/// </summary>
[Navigate(nameof(ClassId))]
public virtual BodyScheduleEntity ClassInfo { get; set; }
#endregion
}
public class BodyScheduleEntity
{
public string Id { get; set; }

[Column(Name = "period")]
public int? period { get; set; }

[Column(Name = "maxQuota")]
public int? maxQuota { get; set; }

[Column(Name = "allDays")]
public int? allDays { get; set; }

[Column(Name = "schoolId", StringLength = 64)]
public string bodySchoolId { get; set; }

[Column(Name = "ownership", StringLength = 64)]
public string ownership { get; set; }


[Column(Name = "className", StringLength = 20)]
public string className { get; set; }

[Column(Name = "sex", StringLength = 10)]
public string sex { get; set; }

[Column(Name = "classType", StringLength = 20)]
public string classType { get; set; }
}

}
}
4 changes: 4 additions & 0 deletions Providers/FreeSql.Provider.Dameng/DamengExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
Func<Expression, string> getExp = exparg => ExpressionLambdaToSql(exparg, tsc);
switch (exp.NodeType)
{
case ExpressionType.ArrayLength:
var arrOper = (exp as UnaryExpression)?.Operand;
if (arrOper.Type == typeof(byte[])) return $"lengthb({getExp(arrOper)})";
break;
case ExpressionType.Convert:
var operandExp = (exp as UnaryExpression)?.Operand;
var gentype = exp.Type.NullableTypeOrThis();
Expand Down
4 changes: 4 additions & 0 deletions Providers/FreeSql.Provider.Firebird/FirebirdExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
Func<Expression, string> getExp = exparg => ExpressionLambdaToSql(exparg, tsc);
switch (exp.NodeType)
{
case ExpressionType.ArrayLength:
var arrOper = (exp as UnaryExpression)?.Operand;
if (arrOper.Type == typeof(byte[])) return $"octet_length({getExp(arrOper)})";
break;
case ExpressionType.Convert:
var operandExp = (exp as UnaryExpression)?.Operand;
var gentype = exp.Type.NullableTypeOrThis();
Expand Down
10 changes: 6 additions & 4 deletions Providers/FreeSql.Provider.KingbaseES/KingbaseESExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
Func<Expression, string> getExp = exparg => ExpressionLambdaToSql(exparg, tsc);
switch (exp.NodeType)
{
case ExpressionType.ArrayLength:
var arrOper = (exp as UnaryExpression)?.Operand;
var arrOperExp = getExp(arrOper);
if (arrOperExp.StartsWith("(") || arrOperExp.EndsWith(")")) return $"array_length(array[{arrOperExp.TrimStart('(').TrimEnd(')')}],1)";
if (arrOper.Type == typeof(byte[])) return $"octet_length({getExp(arrOper)})";
return $"case when {arrOperExp} is null then 0 else array_length({arrOperExp},1) end";
case ExpressionType.Convert:
var operandExp = (exp as UnaryExpression)?.Operand;
var gentype = exp.Type.NullableTypeOrThis();
Expand All @@ -45,10 +51,6 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
}
}
break;
case ExpressionType.ArrayLength:
var arrOperExp = getExp((exp as UnaryExpression).Operand);
if (arrOperExp.StartsWith("(") || arrOperExp.EndsWith(")")) return $"array_length(array[{arrOperExp.TrimStart('(').TrimEnd(')')}],1)";
return $"case when {arrOperExp} is null then 0 else array_length({arrOperExp},1) end";
case ExpressionType.Call:
var callExp = exp as MethodCallExpression;

Expand Down
4 changes: 4 additions & 0 deletions Providers/FreeSql.Provider.MsAccess/MsAccessExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
Func<Expression, string> getExp = exparg => ExpressionLambdaToSql(exparg, tsc);
switch (exp.NodeType)
{
//case ExpressionType.ArrayLength:
// var arrOper = (exp as UnaryExpression)?.Operand;
// if (arrOper.Type == typeof(byte[])) return $"lenb({getExp(arrOper)})"; #505
// break;
case ExpressionType.Convert:
var operandExp = (exp as UnaryExpression)?.Operand;
var gentype = exp.Type.NullableTypeOrThis();
Expand Down
4 changes: 4 additions & 0 deletions Providers/FreeSql.Provider.MySql/MySqlExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
Func<Expression, string> getExp = exparg => ExpressionLambdaToSql(exparg, tsc);
switch (exp.NodeType)
{
case ExpressionType.ArrayLength:
var arrOper = (exp as UnaryExpression)?.Operand;
if (arrOper.Type == typeof(byte[])) return $"octet_length({getExp(arrOper)})";
break;
case ExpressionType.Convert:
var operandExp = (exp as UnaryExpression)?.Operand;
var gentype = exp.Type.NullableTypeOrThis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
Func<Expression, string> getExp = exparg => ExpressionLambdaToSql(exparg, tsc);
switch (exp.NodeType)
{
case ExpressionType.ArrayLength:
var arrOper = (exp as UnaryExpression)?.Operand;
if (arrOper.Type == typeof(byte[])) return $"lengthb({getExp(arrOper)})";
break;
case ExpressionType.Convert:
var operandExp = (exp as UnaryExpression)?.Operand;
var gentype = exp.Type.NullableTypeOrThis();
Expand Down
Loading

0 comments on commit 454eb8c

Please sign in to comment.