SqlService support XElement

This commit is contained in:
sunkaixuan 2022-05-14 17:23:35 +08:00
parent 80940c1878
commit 4141c1cc37
4 changed files with 52 additions and 3 deletions

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace OrmTest
{
@ -78,8 +79,21 @@ namespace OrmTest
Db.CodeFirst.InitTables<UNITCOdEFIRST131>();
Db.CodeFirst.InitTables<UnitTableUserName>();
db.CodeFirst.InitTables<UnitTablename>();
db.CodeFirst.InitTables<UnitXml>();
db.Insertable(new UnitXml()
{
name= XElement.Parse("<xml>aa</xml>")
}).ExecuteCommand();
var list= db.Queryable<UnitXml>().ToList();
}
public class UnitXml
{
[SugarColumn(ColumnDataType ="xml")]
public XElement name { get; set; }
[SugarColumn(IsNullable =true)]
public string name2 { get; set; }
}
public class UnitCodeFirst131
{
public int Id { get; set; }

View File

@ -6,6 +6,7 @@ using System.Data;
using System.Reflection;
using System.Reflection.Emit;
using System.Text.RegularExpressions;
using System.Xml.Linq;
namespace SqlSugar
{
@ -41,6 +42,7 @@ namespace SqlSugar
private static readonly MethodInfo getdatetimeoffset = typeof(IDataRecordExtensions).GetMethod("Getdatetimeoffset");
private static readonly MethodInfo getdatetimeoffsetDate = typeof(IDataRecordExtensions).GetMethod("GetdatetimeoffsetDate");
private static readonly MethodInfo getStringGuid = typeof(IDataRecordExtensions).GetMethod("GetStringGuid");
private static readonly MethodInfo getXelement = typeof(IDataRecordExtensions).GetMethod("GetXelement");
private static readonly MethodInfo getConvertStringGuid = typeof(IDataRecordExtensions).GetMethod("GetConvertStringGuid");
private static readonly MethodInfo getEnum = typeof(IDataRecordExtensions).GetMethod("GetEnum");
private static readonly MethodInfo getConvertString = typeof(IDataRecordExtensions).GetMethod("GetConvertString");
@ -182,6 +184,21 @@ namespace SqlSugar
generator.Emit(OpCodes.Callvirt, columnInfo.PropertyInfo.GetSetMethod(true));
generator.MarkLabel(endIfLabel);
}
else if (columnInfo.UnderType == typeof(XElement))
{
int i = DataRecord.GetOrdinal(fieldName);
Label endIfLabel = generator.DefineLabel();
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldc_I4, i);
generator.Emit(OpCodes.Callvirt, isDBNullMethod);
generator.Emit(OpCodes.Brtrue, endIfLabel);
generator.Emit(OpCodes.Ldloc, result);
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldc_I4, i);
BindMethod(generator, columnInfo, i);
generator.Emit(OpCodes.Callvirt, columnInfo.PropertyInfo.GetSetMethod(true));
generator.MarkLabel(endIfLabel);
}
}
private void BindField(ILGenerator generator, LocalBuilder result, EntityColumnInfo columnInfo, string fieldName)
{
@ -285,6 +302,10 @@ namespace SqlSugar
{
method = isNullableType ? getConvertStringGuid : getStringGuid;
}
else if (bindProperyTypeName == "xelement")
{
method = getXelement;
}
break;
case CSharpDataType.DateTime:
CheckType(bind.DateThrow, bindProperyTypeName, validPropertyName, propertyName);

View File

@ -3,12 +3,19 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml.Linq;
namespace SqlSugar
{
public static partial class IDataRecordExtensions
{
#region Common Extensions
public static XElement GetXelement(this IDataRecord dr, int i)
{
var result = XElement.Parse(dr.GetString(i).ToString());
return result;
}
public static Guid GetStringGuid(this IDataRecord dr, int i)
{
var result = Guid.Parse(dr.GetValue(i).ToString());

View File

@ -6,6 +6,8 @@ using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace SqlSugar
{
public class SqlServerProvider : AdoProvider
@ -131,10 +133,15 @@ namespace SqlSugar
sqlParameter.Value = parameter.Value;
sqlParameter.DbType = parameter.DbType;
var isTime = parameter.DbType == System.Data.DbType.Time;
if (isTime)
if (isTime)
{
sqlParameter.SqlDbType = SqlDbType.Time;
sqlParameter.Value=DateTime.Parse(parameter.Value?.ToString()).TimeOfDay;
sqlParameter.SqlDbType = SqlDbType.Time;
sqlParameter.Value = DateTime.Parse(parameter.Value?.ToString()).TimeOfDay;
}
else if (parameter.Value!=null&&parameter.Value is XElement)
{
sqlParameter.SqlDbType = SqlDbType.Xml;
sqlParameter.Value= (parameter.Value as XElement).ToString();
}
if (sqlParameter.Value!=null&& sqlParameter.Value != DBNull.Value && sqlParameter.DbType == System.Data.DbType.DateTime)
{