Files
lserp_cs_5.0/插件库/Zhaizj.Framework/ORM/Utils/FillUtil.cs
T
2026-07-10 15:25:05 +08:00

79 lines
2.3 KiB
C#

using System;
using System.Collections;
using System.Data;
using Zhaizj.Framework.ORM.Caching;
namespace Zhaizj.Framework.ORM {
internal class FillUtil {
private static readonly ILog logger = LogManager.GetLogger( typeof( FillUtil ) );
public static Object Populate( IDataRecord rd, ObjectInfo state ) {
IEntity obj = Entity.New( state.EntityInfo.Type.FullName );
// state
//obj.state.Order = state.Order;
for (int i = 0; i < rd.FieldCount; i++) {
Object fdvalue = rd[i];
if (fdvalue == null || fdvalue == DBNull.Value) continue;
EntityPropertyInfo ep = state.EntityInfo.GetPropertyByColumn( rd.GetName( i ) );
if (ep == null) continue;
try {
if (ep.IsEntity || ep.IsAbstractEntity) {
setEntityPropertyValueById( obj, state, ep, rd.GetInt32( i ) );
}
else {
ep.SetValue( obj, getReaderValue( fdvalue, ep.Type ) );
}
}
catch (Exception ex) {
logger.Error( ex.Message + "=" + ep.Name + "_" + ep.Type );
logger.Error( ex.StackTrace );
throw ex;
}
}
return obj;
}
private static void setEntityPropertyValueById( IEntity obj, ObjectInfo state, EntityPropertyInfo property, int pid ) {
if (!property.IsAbstractEntity) {
IEntity objValue = Entity.New( property.Type.FullName );
objValue.Id = pid;
// state
//objValue.state = new ObjectInfo( property.Type ).Copy( state );
IEntity objCache = ObjectPool.FindOne( property.Type, objValue.Id );
if (objCache != null) {
objValue = objCache;
}
obj.set( property.Name, objValue );
}
}
private static Object getReaderValue( Object rdValue, Type ptype ) {
if (ptype == typeof( decimal ))
return Convert.ToDecimal( rdValue );
if (ptype == typeof( int ))
return Convert.ToInt32( rdValue );
return rdValue;
}
}
}