Add json queryable

This commit is contained in:
sunkaixuan
2022-09-12 06:43:44 +08:00
parent 8fb74caab5
commit 45c49fbcd6
5 changed files with 143 additions and 0 deletions

View File

@@ -605,5 +605,30 @@ namespace SqlSugar
{
return null;
}
public virtual string JsonField(MethodCallExpressionModel model)
{
throw new NotImplementedException("Current database no support");
}
public virtual string JsonContainsFieldName(MethodCallExpressionModel model)
{
throw new NotImplementedException("Current database no support");
}
public virtual string JsonArrayLength(MethodCallExpressionModel model)
{
throw new NotImplementedException("Current database no support");
}
public virtual string JsonParse(MethodCallExpressionModel model)
{
throw new NotImplementedException("Current database no support");
}
public string JsonLike(MethodCallExpressionModel model)
{
model.Args[0].MemberName = ToString(model);
return Contains(model);
}
}
}

View File

@@ -92,5 +92,10 @@ namespace SqlSugar
string Exists(MethodCallExpressionModel model);
string GetDateString(string dateValue,string format);
string GetForXmlPath();
string JsonField(MethodCallExpressionModel model);
string JsonContainsFieldName(MethodCallExpressionModel model);
string JsonArrayLength(MethodCallExpressionModel model);
string JsonParse(MethodCallExpressionModel model);
string JsonLike(MethodCallExpressionModel model);
}
}

View File

@@ -21,6 +21,49 @@ namespace SqlSugar
{
throw new NotSupportedException("Can only be used in expressions");
}
public static string JsonField(object json,string fieldName)
{
throw new NotSupportedException("Can only be used in expressions");
}
public static string JsonField(object json, string fieldName,string includeFieldName)
{
throw new NotSupportedException("Can only be used in expressions");
}
public static string JsonField(object json, string fieldName, string includeFieldName, string ThenIncludeFieldName)
{
throw new NotSupportedException("Can only be used in expressions");
}
public static string JsonField(object json, string fieldName, string includeFieldName, string ThenIncludeFieldName, string ThenIncludeFieldName2)
{
throw new NotSupportedException("Can only be used in expressions");
}
public static string JsonField(object json, string fieldName, string includeFieldName, string ThenIncludeFieldName, string ThenIncludeFieldName2, string ThenIncludeFieldName3)
{
throw new NotSupportedException("Can only be used in expressions");
}
public static bool JsonContainsFieldName(object json, string fieldName)
{
throw new NotSupportedException("Can only be used in expressions");
}
public static int JsonArrayLength(object json)
{
throw new NotSupportedException("Can only be used in expressions");
}
public static string JsonParse(object json)
{
throw new NotSupportedException("Can only be used in expressions");
}
public static bool JsonLike(object json,string likeStr)
{
throw new NotSupportedException("Can only be used in expressions");
}
public static T Desc<T>(T value)
{
throw new NotSupportedException("Can only be used in expressions");

View File

@@ -1028,6 +1028,17 @@ namespace SqlSugar
this.Context.Parameters.AddRange(sqlObj.Value);
}
return this.Context.DbMehtods.Exists(model);
case "JsonField":
return this.Context.DbMehtods.JsonField(model);
case "JsonArrayLength":
return this.Context.DbMehtods.JsonArrayLength(model);
case "JsonContainsFieldName":
return this.Context.DbMehtods.JsonContainsFieldName(model);
case "JsonParse":
return this.Context.DbMehtods.JsonParse(model);
case "JsonLike":
return this.Context.DbMehtods.JsonLike(model);
default:
break;
}

View File

@@ -384,5 +384,64 @@ namespace SqlSugar
{
return "( " + fieldName + "=true )";
}
public override string JsonField(MethodCallExpressionModel model)
{
var parameter = model.Args[0];
var parameter1 = model.Args[1];
//var parameter2 = model.Args[2];
//var parameter3= model.Args[3];
var result= GetJson(parameter.MemberName, parameter1.MemberName, model.Args.Count()==2);
if (model.Args.Count > 2)
{
result = GetJson(result, model.Args[2].MemberName, model.Args.Count() == 3);
}
if (model.Args.Count > 3)
{
result = GetJson(result, model.Args[3].MemberName, model.Args.Count() == 4);
}
if (model.Args.Count > 4)
{
result = GetJson(result, model.Args[4].MemberName, model.Args.Count() == 5);
}
if (model.Args.Count > 5)
{
result = GetJson(result, model.Args[5].MemberName, model.Args.Count() == 6);
}
return result;
}
public override string JsonContainsFieldName(MethodCallExpressionModel model)
{
var parameter = model.Args[0];
var parameter1 = model.Args[1];
return $"({parameter.MemberName}::jsonb ?{parameter1.MemberName})";
}
private string GetJson(object memberName1, object memberName2,bool isLast)
{
if (isLast)
{
return $"({memberName1}::json->>{memberName2})";
}
else
{
return $"({memberName1}->{memberName2})";
}
}
public override string JsonArrayLength(MethodCallExpressionModel model)
{
var parameter = model.Args[0];
//var parameter1 = model.Args[1];
return $" json_array_length({parameter.MemberName}::json) ";
}
public override string JsonParse(MethodCallExpressionModel model)
{
var parameter = model.Args[0];
//var parameter1 = model.Args[1];
return $" ({parameter.MemberName}::json) ";
}
}
}