简体   繁体   English

表达式树中的sql C#

[英]sql from expression trees C#

I basically have a generic class called DynamicServices. 我基本上有一个称为DynamicServices的通用类。 The dynamicServices will look like the following dynamicServices将如下所示

class DynamicEntityServices<T>{
     T GetById(Guid Id) { // get from db using ado.net dal 
     List<T> Search(SearchCondition searchCondition) { // filtering the data from the dal
}

Now, that I have a json spec file [basically a json schema file that defines the dynamic entity and its property / types 现在,我有了一个json规范文件[基本上是一个json模式文件,它定义了动态实体及其属性/类型

During runtime, I want the consumer of my service to build and send me a search condition. 在运行时,我希望服务的使用者构建并向我发送搜索条件。

I currently have my SQLDAL implementation like the following 我目前有如下的SQLDAL实现

class DynamicEntityDAL<T>{
    List<T> Search(SearchCondition condition){
      StringBuilder query = new StringBuilder();
      foreach(var field in condition.Fields){
         query.Append(" "+field.Key+" like '%"+field.Value+"%' ")

and something similar to the above, I know that the above process is right because there are practical limitations of this. 和上面的类似,我知道上面的过程是正确的,因为这样做有实际的局限性。 Please note that I would like to actually get an expression sent in from the caller of my dynamic service in the search condition and then I will construct the query as like below 请注意,我实际上希望在搜索条件下从动态服务的调用者发送一个表达式,然后按如下所示构造查询

var filterExpr = new Expression()...
var paramterExpr = new ParameterExpression("categorystatus",true); // not the right ones, but will look similar to this.
var condition = new SearchCondition{ SearchExpression = filterExpr };

In DAL, I can try to convert the above to SQL using some expression visitor like
`string filterClause = ConvertToSql(condition.SearchExpression);`

I can read and implement things, but as my json schema files are dynamic, I cannot use EF. 我可以阅读和实现内容,但是由于json模式文件是动态的,因此无法使用EF。 Kindly suggest the right approach so that my library can get the expression from the caller of the service and also get it translated to T-SQL and query from db. 请提出正确的方法,以便我的库可以从服务的调用者获取表达式,还可以将其转换为T-SQL并从db查询。

Assuming you have a piece of JSON that represents the expression for a WHERE clause of a user query, then here are the steps of converting that to SQL: 假设您有一块JSON代表用户查询的WHERE子句的表达式,那么这里是将其转换为SQL的步骤:

  1. Convert JSON string to a Hierarchal object. 将JSON字符串转换为Hierarchal对象。
  2. Convert the Hierarchal object to SQL. 将Hierarchal对象转换为SQL。

Assuming you already have (1) done (it looks like you can create Expression 's, presumably from the JSON), then you just need to write something that recursively traverses the Hierarchal object and converts it to SQL, depending on the type of Expression . 假设您已经完成了(1)(似乎可以从JSON创建Expression的对象),那么您只需要编写一些内容即可,就可以根据Expression的类型递归地遍历Hierarchal对象并将其转换为SQL。 。

You're going to probably need some notion of an explicit schema (tables and columns) somewhere to dynamically compute your JOIN 's, unless this is all in one table/view. 您可能需要在某个地方需要一些显式架构(表和列)的概念来动态计算您的JOIN ,除非全部都在一个表/视图中。 Additionally, if your schema's foreign key relationships creates a cyclic graph then you'll need to define relationships, in addition to tables and columns (which will help you specify which type of join unless you can assume INNER is always okay). 另外,如果您架构的外键关系创建了一个循环图,那么除了表和列之外,您还需要定义关系(这将帮助您指定哪种连接类型,除非您可以假设INNER总是可以的)。 This will also help you when you want to change the SELECT columns and ORDER BY columns, when/if that becomes a need. 当/如果需要更改SELECT列和ORDER BY列时,这也将为您提供帮助。

This is coming from someone who has written similar things multiple times in the past: 这来自过去多次写过​​类似文章的人:

  • This is very time-consuming and non-trivially to do correctly; 这是非常耗时且不容易做到的。 it's better to find something that already does something similar enough to what you want, instead of writing this from scratch. 最好找到已经完成了与所需功能类似的功能,而不是从头开始编写。
  • Leverage as many existing frameworks as possible if there isn't something already out there for you that works. 如果没有适合您的东西,请尽可能多地利用现有框架。 For example, reusing Linq Expression s instead of rolling your own, which it looks like you've done that. 例如,重用Linq Expression而不是自己滚动,看起来已经完成了。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM