Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在 ISelect 中对 byte[] 的长度进行 == 比较时出现异常 #505

Closed
alexinea opened this issue Oct 28, 2020 · 3 comments
Closed

在 ISelect 中对 byte[] 的长度进行 == 比较时出现异常 #505

alexinea opened this issue Oct 28, 2020 · 3 comments
Labels
bug Something isn't working

Comments

@alexinea
Copy link
Member

alexinea commented Oct 28, 2020

情况说明:

0、当前使用的版本号是 1.10.1

1、我在数据库(假设是 SqlServer)中预设两个字段,一个字段名叫 ByteArray 类型为 varbinary(2000)、另一个字段名叫 ByteLength 类型为 int

2、假设我有以下模型:

public class Model
{
    public byte[] ByteArray {get; set;}
    public int ByteLength {get; set;}
}

3、我在检索数据时,使用如下伪代码:

var fsql = ...;

var val = fsql.Select<Model>()
           .Where(x=>x.State == 1)
           .Where(x=>x.ByteArray.Length == x.ByteLength)
           .ToList();

4、执行上述操作后抛出异常:

“=”附近有语法错误。

5、StackTrace 信息:

   at FreeSql.Internal.CommonProvider.Select0Provider`2.ToListAfPrivate(String sql, GetAllFieldExpressionTreeInfo af, ReadAnonymousTypeOtherInfo[] otherData)
   at FreeSql.Internal.CommonProvider.Select0Provider`2.ToListPrivate(GetAllFieldExpressionTreeInfo af, ReadAnonymousTypeOtherInfo[] otherData)
   at FreeSql.Internal.CommonProvider.Select0Provider`2.ToList(Boolean includeNestedMembers)
   at FreeSql.Internal.CommonProvider.Select1Provider`1.ToList(Boolean includeNestedMembers)

6、所生成的 SQL 语句样本(片段):

SELECT TOP 100 a.[ByteArray], a.[ByteLength] 
FROM [ModelTable] a 
WHERE ( = a.[Length]) 
@2881099
Copy link
Collaborator

2881099 commented Oct 28, 2020

byte[] 的方法确实没有做解析,貌似各数据库之间不通用?

临时方案,用自定义方法解析解决:

[ExpressionCall]
public static class DbFunc {
  static ThreadLocal<ExpressionCallContext> context = new ThreadLocal<ExpressionCallContext>();

  public static DateTime SqlLength(this byte[] that)
  {
    var up = context.Value;
    if (up.DataType == FreeSql.DataType.SqlServer)
      up.Result = $"len({up.ParsedContent["that"]})"; //不确定是不是用 len 
    return that;
  }
}

@alexinea
Copy link
Member Author

是的,每个都不太一样,比如 SqlServer 使用的是 DATALENGTH(<fieldname>),MySql 好像是 char_length(<fieldname>),pgsql 好像是 OCTET_LENGTH(<fieldname>)

2881099 added a commit that referenced this issue Nov 2, 2020
@luoyunchong luoyunchong added the bug Something isn't working label Nov 3, 2020
@alexinea
Copy link
Member Author

已可以通过 someThingBytes.Length == givenLength 这种方式来生成 WHERE 子句了,多谢大侠。

2881099 added a commit that referenced this issue Nov 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants