DevExpress - 将 "sort string"(json)、"filter string" 转换为 sql
DevExpress - Convert "sort string"(json), "filter string" to sql
我使用带有特殊数据源的 devextreme 网格 https://js.devexpress.com/Demos/WidgetsGallery/Demo/DataGrid/CustomDataSource/React/Light/
参数"sort","filter"成功传给后端
在我的后端,我捕获了值:
* sort -> [{"selector": "Id", "desc": true}]
* filter -> ["RoleName", "contains", "test"]
如何转换 :
* sort -> ORDER BY Id DESC
* filter -> RoleName LIKE '%test%'
注意事项:
这只是实际情况中的一个简单示例,可能是字符串太复杂以至于我不想创建自己的函数。
devextreme 有一个官方 github,它从后端处理流程。但 "raw sql" 仅存在于 php 版本中。必须手动转换为c#
https://github.com/DevExpress/DevExtreme.AspNet.Data
https://github.com/DevExpress/DevExtreme-PHP-Data
将 PHP 转换为 C#
private string _GetSimpleSqlExpr(object[] expression)
{
string result = "";
int itemsCount = expression.Length;
string fieldName = expression[0].ToString();
if (itemsCount == 2)
{
var val = expression[1];
result = String.Format("{0} = {1}", fieldName, val);
}
else if (itemsCount == 3)
{
string clause = expression[1].ToString();
var val = expression[2];
string pattern = "";
switch (clause)
{
case "=":
case "<>":
case ">":
case ">=":
case "<":
case "<=":
if (val.GetType().Name == "Int64")
{
pattern = "{0} {1} {2}";
}
else
{
pattern = "{0} {1} '{2}'";
}
break;
case "startswith":
pattern = "{0} {1} '{2}%'";
clause = " LIKE ";
break;
case "endswith":
pattern = "{0} {1} '%{2}'";
clause = " LIKE ";
break;
case "contains":
pattern = "{0} {1} '%{2}%'";
clause = " LIKE ";
break;
case "notcontains":
pattern = "{0} {1} '%{2}%'";
clause = String.Format("{0} {1}", " NOT ", " LIKE ");
break;
default:
clause = "";
break;
}
result = string.Format(pattern, fieldName, clause, val);
}
return result;
}
public string GetSqlExprByArray(object[] expression)
{
string result = "(";
Boolean prevItemWasArray = false;
int index = 0;
foreach (var item in expression)
{
Console.WriteLine(item.GetType().Name);
if(item.GetType().Name=="String")
{
prevItemWasArray = false;
if(index==0)
{
if(item.ToString()=="!")
{
result += " NOT ";
continue;
}
result += _GetSimpleSqlExpr(expression);
break;
}
string strItem = item.ToString().ToUpper();
if (strItem == "AND" || strItem == "OR")
{
result += string.Format(" {0} ", strItem);
}
continue;
}
if (item.GetType().Name == "JArray")
{
if (prevItemWasArray)
{
result += string.Format(" {0} ", "AND");
}
result += GetSqlExprByArray(((JArray)item).ToObject<object[]>());
prevItemWasArray = true;
}
index++;
}
result += ")";
return result;
}
我使用带有特殊数据源的 devextreme 网格 https://js.devexpress.com/Demos/WidgetsGallery/Demo/DataGrid/CustomDataSource/React/Light/ 参数"sort","filter"成功传给后端
在我的后端,我捕获了值:
* sort -> [{"selector": "Id", "desc": true}]
* filter -> ["RoleName", "contains", "test"]
如何转换 :
* sort -> ORDER BY Id DESC
* filter -> RoleName LIKE '%test%'
注意事项: 这只是实际情况中的一个简单示例,可能是字符串太复杂以至于我不想创建自己的函数。
devextreme 有一个官方 github,它从后端处理流程。但 "raw sql" 仅存在于 php 版本中。必须手动转换为c#
https://github.com/DevExpress/DevExtreme.AspNet.Data https://github.com/DevExpress/DevExtreme-PHP-Data
将 PHP 转换为 C#
private string _GetSimpleSqlExpr(object[] expression)
{
string result = "";
int itemsCount = expression.Length;
string fieldName = expression[0].ToString();
if (itemsCount == 2)
{
var val = expression[1];
result = String.Format("{0} = {1}", fieldName, val);
}
else if (itemsCount == 3)
{
string clause = expression[1].ToString();
var val = expression[2];
string pattern = "";
switch (clause)
{
case "=":
case "<>":
case ">":
case ">=":
case "<":
case "<=":
if (val.GetType().Name == "Int64")
{
pattern = "{0} {1} {2}";
}
else
{
pattern = "{0} {1} '{2}'";
}
break;
case "startswith":
pattern = "{0} {1} '{2}%'";
clause = " LIKE ";
break;
case "endswith":
pattern = "{0} {1} '%{2}'";
clause = " LIKE ";
break;
case "contains":
pattern = "{0} {1} '%{2}%'";
clause = " LIKE ";
break;
case "notcontains":
pattern = "{0} {1} '%{2}%'";
clause = String.Format("{0} {1}", " NOT ", " LIKE ");
break;
default:
clause = "";
break;
}
result = string.Format(pattern, fieldName, clause, val);
}
return result;
}
public string GetSqlExprByArray(object[] expression)
{
string result = "(";
Boolean prevItemWasArray = false;
int index = 0;
foreach (var item in expression)
{
Console.WriteLine(item.GetType().Name);
if(item.GetType().Name=="String")
{
prevItemWasArray = false;
if(index==0)
{
if(item.ToString()=="!")
{
result += " NOT ";
continue;
}
result += _GetSimpleSqlExpr(expression);
break;
}
string strItem = item.ToString().ToUpper();
if (strItem == "AND" || strItem == "OR")
{
result += string.Format(" {0} ", strItem);
}
continue;
}
if (item.GetType().Name == "JArray")
{
if (prevItemWasArray)
{
result += string.Format(" {0} ", "AND");
}
result += GetSqlExprByArray(((JArray)item).ToObject<object[]>());
prevItemWasArray = true;
}
index++;
}
result += ")";
return result;
}