SqlServer support for xml path

This commit is contained in:
sunkaixuan 2022-08-03 20:42:40 +08:00
parent 111deccb8a
commit 8875d74111
4 changed files with 26 additions and 0 deletions

View File

@ -561,5 +561,9 @@ namespace SqlSugar
{
return null;
}
public virtual string GetForXmlPath()
{
return null;
}
}
}

View File

@ -88,5 +88,6 @@ namespace SqlSugar
string Desc(MethodCallExpressionModel model);
string Stuff(MethodCallExpressionModel model);
string GetDateString(string dateValue,string format);
string GetForXmlPath();
}
}

View File

@ -19,6 +19,7 @@ namespace SqlSugar
private ExpressionContext context = null;
private string subKey = "$SubAs:";
private bool hasWhere;
private bool isXmlPath = false;
public SubResolve(MethodCallExpression expression, ExpressionContext context, Expression oppsiteExpression)
{
this.context = context;
@ -124,6 +125,14 @@ namespace SqlSugar
{
sql = string.Join(UtilConstants.Space, sqlItems);
}
if (isXmlPath)
{
var xmlPath = context.DbMehtods.GetForXmlPath();
if (xmlPath.HasValue())
{
sql = sql + xmlPath;
}
}
return this.context.DbMehtods.Pack(sql);
}
@ -185,6 +194,10 @@ namespace SqlSugar
{
item = items.First(s => s is SubAndIF);
}
else if (item is SubSelectStringJoin)
{
isXmlPath = true;
}
item.Context = this.context;
item.Expression = exp;

View File

@ -20,6 +20,14 @@ namespace SqlSugar
}
public partial class SqlServerMethod : DefaultDbMethod, IDbMethods
{
public override string GetForXmlPath()
{
return " FOR XML PATH('')),1,len(N','),'') ";
}
public override string GetStringJoinSelector(string result, string separator)
{
return $"stuff((SELECT cast(N'{separator}' as nvarchar(max)) + cast({result} as nvarchar(max))";
}
public override string DateValue(MethodCallExpressionModel model)
{
var parameter = model.Args[0];