init lserp cs 5.0
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
using Zhaizj.Framework;
|
||||
using Zhaizj.Framework.Data;
|
||||
using Zhaizj.Framework.Reflection;
|
||||
using Zhaizj.Framework.ORM.Caching;
|
||||
|
||||
namespace Zhaizj.Framework.ORM {
|
||||
|
||||
internal class CacheUtil {
|
||||
|
||||
private static readonly ILog logger = LogManager.GetLogger( typeof( CacheUtil ) );
|
||||
|
||||
public static void CheckCountCache( String action, IEntity obj, EntityInfo entityInfo ) {
|
||||
|
||||
for (int i = 0; i < entityInfo.SavedPropertyList.Count; i++) {
|
||||
IEntity container = null;
|
||||
String propertyName = null;
|
||||
EntityPropertyInfo info = entityInfo.SavedPropertyList[i];
|
||||
if ((info.Name != "Id") && !(info.Name == "OID")) {
|
||||
ICacheAttribute attribute = ReflectionUtil.GetAttribute( info.Property, typeof( CacheCountAttribute ) ) as ICacheAttribute;
|
||||
if (attribute != null) {
|
||||
container = ReflectionUtil.GetPropertyValue( obj, info.Name ) as IEntity;
|
||||
propertyName = attribute.TargetPropertyName.Replace( " ", "" );
|
||||
}
|
||||
if (container != null) {
|
||||
String columnName = Entity.GetInfo( container ).GetColumnName( propertyName );
|
||||
setCountCacheBySql( container, columnName, action );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void setCountCacheBySql( IEntity container, String columName, String action ) {
|
||||
|
||||
EntityInfo et = Entity.GetInfo( container );
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.Append( "update " );
|
||||
builder.Append( et.TableName );
|
||||
builder.Append( " set " );
|
||||
builder.Append( columName );
|
||||
builder.Append( "=" );
|
||||
builder.Append( columName );
|
||||
if (action.ToLower() == "insert") {
|
||||
builder.Append( "+1" );
|
||||
}
|
||||
else {
|
||||
builder.Append( "-1" );
|
||||
}
|
||||
builder.AppendFormat( " where id={0}", container.Id );
|
||||
DataFactory.GetCommand( builder.ToString(), DbContext.getConnection( et ) ).ExecuteNonQuery();
|
||||
logger.Debug( "set counter cache : " + builder.ToString() );
|
||||
|
||||
CacheTime.updateTable( et.Type );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,240 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Data;
|
||||
using Zhaizj.Framework;
|
||||
using Zhaizj.Framework.Data;
|
||||
using System.Collections.Generic;
|
||||
using Zhaizj.Framework.Members.Interface;
|
||||
using Zhaizj.Framework.Web;
|
||||
using Zhaizj.Framework.ORM.Utils;
|
||||
using Zhaizj.Framework.ORM.Caching;
|
||||
|
||||
namespace Zhaizj.Framework.ORM
|
||||
{
|
||||
|
||||
|
||||
internal class EntityPropertyUtil
|
||||
{
|
||||
|
||||
private static readonly ILog logger = LogManager.GetLogger(typeof(EntityPropertyUtil));
|
||||
|
||||
public static void Fill_EntityProperty_Ids(IDataRecord record, IList _includeEntityPropertyList, ref Hashtable _entityProperty_ids)
|
||||
{
|
||||
|
||||
if (_includeEntityPropertyList == null) return;
|
||||
|
||||
foreach (EntityPropertyInfo info in _includeEntityPropertyList)
|
||||
{
|
||||
|
||||
|
||||
String columnName = info.ColumnName;
|
||||
|
||||
if (record[columnName] == DBNull.Value) continue;
|
||||
|
||||
if (_entityProperty_ids[columnName] == null)
|
||||
{
|
||||
_entityProperty_ids[columnName] = record[columnName] + ",";
|
||||
}
|
||||
else
|
||||
{
|
||||
String ids = _entityProperty_ids[columnName].ToString();
|
||||
String tempId = "," + record[columnName] + ",";
|
||||
if (("," + ids).IndexOf(tempId) < 0)
|
||||
{
|
||||
_entityProperty_ids[columnName] = ids + record[columnName] + ",";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static IList FindList(ObjectInfo state, String sql)
|
||||
{
|
||||
IList results = new ArrayList();
|
||||
IDataReader rd = null;
|
||||
Hashtable hashtable = new Hashtable();
|
||||
IDbCommand command = DataFactory.GetCommand(sql, DbContext.getConnection(state.EntityInfo));
|
||||
try
|
||||
{
|
||||
rd = command.ExecuteReader();
|
||||
while (rd.Read())
|
||||
{
|
||||
IDataRecord record = rd;
|
||||
Fill_EntityProperty_Ids(record, state.Includer.EntityPropertyList, ref hashtable);
|
||||
|
||||
int id = Convert.ToInt32(record["Id"]);
|
||||
Object cache = ObjectPool.FindOne(state.EntityInfo.Type, id);
|
||||
|
||||
if (cache != null)
|
||||
{
|
||||
results.Add(cache);
|
||||
}
|
||||
else
|
||||
{
|
||||
results.Add(FillUtil.Populate(record, state));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error("sql=>" + sql);
|
||||
logger.Error(ex.Message);
|
||||
logger.Error(ex.StackTrace);
|
||||
throw ex;
|
||||
}
|
||||
finally
|
||||
{
|
||||
OrmHelper.CloseDataReader(rd);
|
||||
}
|
||||
|
||||
|
||||
if (results.Count == 0)
|
||||
{
|
||||
return results;
|
||||
}
|
||||
return setEntityProperty(state, results, hashtable);
|
||||
}
|
||||
|
||||
public static DataTable FindDt(ObjectInfo state, String sql)
|
||||
{
|
||||
DataTable results = new DataTable();
|
||||
IDbDataAdapter adapter = DataFactory.GetAdapter(sql, DbContext.getConnection(state.EntityInfo));
|
||||
try
|
||||
{
|
||||
DataSet ds = new DataSet(state.EntityInfo.TableName);
|
||||
adapter.Fill(ds);
|
||||
results = ds.Tables[0];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error("sql=>" + sql);
|
||||
logger.Error(ex.Message);
|
||||
logger.Error(ex.StackTrace);
|
||||
throw ex;
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
public static int FindMaxId(ObjectInfo state, String sql)
|
||||
{
|
||||
int results = 0;
|
||||
IDbCommand command = DataFactory.GetCommand(sql, DbContext.getConnection(state.EntityInfo));
|
||||
try
|
||||
{
|
||||
object obj = command.ExecuteScalar();
|
||||
if (!string.IsNullOrEmpty(obj + ""))
|
||||
results = Convert.ToInt32(obj);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error("sql=>" + sql);
|
||||
logger.Error(ex.Message);
|
||||
logger.Error(ex.StackTrace);
|
||||
throw ex;
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
public static IList setEntityProperty(ObjectInfo state, IList results, Hashtable _entityProperty_ids)
|
||||
{
|
||||
Hashtable selectColumn = null;
|
||||
if (strUtil.HasText(state.Includer.SelectedProperty) && (state.Includer.SelectedProperty != "*"))
|
||||
{
|
||||
selectColumn = SqlBuilder.GetIncludeSelectColumn(state.Includer.SelectedProperty, state.EntityInfo.EntityPropertyList);
|
||||
}
|
||||
else
|
||||
{
|
||||
selectColumn = Includer.getSelectColumnFromInclude(state.Includer.EntityPropertyList);
|
||||
}
|
||||
Hashtable propertyList = getEntityPropertyList(state.Includer.EntityPropertyList, _entityProperty_ids, selectColumn, state.Order);
|
||||
fillPropertyObjectToResultList(ref results, state.EntityInfo.Type, state.Includer.EntityPropertyList, propertyList);
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
public static Hashtable getEntityPropertyList(IList _includeEntityPropertyList, Hashtable _ep_ids, Hashtable selectColumn, String order)
|
||||
{
|
||||
Hashtable hashtable = new Hashtable();
|
||||
|
||||
if (_includeEntityPropertyList == null) return hashtable;
|
||||
|
||||
foreach (EntityPropertyInfo info in _includeEntityPropertyList)
|
||||
{
|
||||
String epIds = "";
|
||||
String[] arrIds = new String[] { };
|
||||
if (_ep_ids[info.ColumnName] != null)
|
||||
arrIds = _ep_ids[info.ColumnName].ToString().Trim().TrimEnd(',').Split(',');
|
||||
|
||||
foreach (String strId in arrIds)
|
||||
{
|
||||
IEntity cacheObj = ObjectPool.FindOne(info.EntityInfo.Type, cvt.ToInt(strId));
|
||||
if (cacheObj != null)
|
||||
{
|
||||
hashtable[getPropertyObjectKey(info.Name, cacheObj.Id)] = cacheObj;
|
||||
}
|
||||
else
|
||||
{
|
||||
epIds = epIds + strId + ",";
|
||||
}
|
||||
}
|
||||
epIds = epIds.TrimEnd(',');
|
||||
|
||||
if (strUtil.IsNullOrEmpty(epIds)) continue;
|
||||
|
||||
String col = selectColumn[info.Name].ToString().Trim().TrimEnd(',');
|
||||
if (string.Compare(col, "Id", true) != 0)
|
||||
{
|
||||
if (!col.Equals("*") && (("," + col + ",").ToLower().IndexOf(",id,") < 0))
|
||||
{
|
||||
col = "Id," + col;
|
||||
}
|
||||
|
||||
ObjectInfo cstate = new ObjectInfo(info.EntityInfo);
|
||||
cstate.IsFindChild = true;
|
||||
IList list = ObjectDB.Find(cstate, String.Format("Id in ({0})", epIds)).list();
|
||||
|
||||
foreach (IEntity objItem in list)
|
||||
{
|
||||
hashtable[getPropertyObjectKey(info.Name, objItem.Id)] = objItem;
|
||||
ObjectPool.Add(objItem);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return hashtable;
|
||||
}
|
||||
|
||||
public static void fillPropertyObjectToResultList(ref IList resultList, Type t, IList _includeEntityPropertyList, Hashtable _ep_obj_list)
|
||||
{
|
||||
|
||||
if (_includeEntityPropertyList == null) return;
|
||||
|
||||
foreach (IEntity obj in resultList)
|
||||
{
|
||||
|
||||
foreach (EntityPropertyInfo ep in _includeEntityPropertyList)
|
||||
{
|
||||
|
||||
IEntity objValue = obj.get(ep.Name) as IEntity;
|
||||
if (objValue == null) continue;
|
||||
|
||||
String key = getPropertyObjectKey(ep.Name, objValue.Id);
|
||||
Object propertyValue = _ep_obj_list[key];
|
||||
|
||||
ValueSetter.setEntityByCheckNull(obj, ep, propertyValue, objValue.Id);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static String getPropertyObjectKey(String propertyName, int objId)
|
||||
{
|
||||
return (propertyName + "_" + objId);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using Zhaizj.Framework.Data;
|
||||
|
||||
namespace Zhaizj.Framework.ORM {
|
||||
|
||||
internal class OrmHelper {
|
||||
|
||||
internal static void CloseDataReader( IDataReader rd ) {
|
||||
|
||||
if (rd == null) return;
|
||||
if (rd.IsClosed) return;
|
||||
|
||||
rd.Close();
|
||||
}
|
||||
|
||||
internal static void SetParameters( IDbCommand cmd, String action, IEntity obj, EntityInfo entityInfo ) {
|
||||
|
||||
for (int i = 0; i < entityInfo.SavedPropertyList.Count; i++) {
|
||||
|
||||
EntityPropertyInfo info = entityInfo.SavedPropertyList[i];
|
||||
|
||||
if (isContinue( action, info, entityInfo )) continue;
|
||||
|
||||
|
||||
Object paramVal = obj.get( info.Name );
|
||||
if (paramVal == null && info.DefaultAttribute != null) {
|
||||
paramVal = info.DefaultAttribute.Value;
|
||||
}
|
||||
|
||||
if (paramVal == null) {
|
||||
setDefaultValue( cmd, info, entityInfo );
|
||||
}
|
||||
else if (info.Type.IsSubclassOf( typeof( IEntity ) ) || MappingClass.Instance.ClassList.Contains( info.Type.FullName )) {
|
||||
setEntityId( cmd, info, paramVal );
|
||||
}
|
||||
else {
|
||||
paramVal = DataFactory.SetParameter( cmd, info.ColumnName, paramVal );
|
||||
obj.set( info.Name, paramVal );
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private static Boolean isContinue( String action, EntityPropertyInfo info, EntityInfo entityInfo ) {
|
||||
|
||||
if (info.SaveToDB == false) return true;
|
||||
if (info.IsList) return true;
|
||||
|
||||
if (info.Name.Equals( "Id" )) {
|
||||
|
||||
if (action.Equals( "update" )) return true;
|
||||
|
||||
// Ôö¼ÓʵÌå¼üÀàÐÍʶ±ð
|
||||
if (/**/DbConfig.Instance.IsAutoId && action.Equals("insert")
|
||||
&& entityInfo.Parent == null
|
||||
)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private static void setDefaultValue( IDbCommand cmd, EntityPropertyInfo info, EntityInfo entityInfo ) {
|
||||
|
||||
if (MappingClass.Instance.ClassList.Contains( info.Type.FullName )) {
|
||||
DataFactory.SetParameter( cmd, info.ColumnName, -1 );
|
||||
}
|
||||
else if (info.Type == typeof( DateTime )) {
|
||||
if (entityInfo.DbType == DatabaseType.Access) {
|
||||
DataFactory.SetParameter( cmd, info.ColumnName, DateTime.Now.ToString() );
|
||||
}
|
||||
else {
|
||||
DataFactory.SetParameter( cmd, info.ColumnName, DateTime.Now );
|
||||
}
|
||||
}
|
||||
else {
|
||||
DataFactory.SetParameter( cmd, info.ColumnName, "" );
|
||||
}
|
||||
}
|
||||
|
||||
private static void setEntityId( IDbCommand cmd, EntityPropertyInfo info, Object paramVal ) {
|
||||
int id = ((IEntity)paramVal).Id;
|
||||
DataFactory.SetParameter( cmd, info.ColumnName, id );
|
||||
}
|
||||
|
||||
public static Boolean IsEntityBase( Type t ) {
|
||||
return t.FullName.IndexOf( "Zhaizj.Framework.ObjectBase" ) >= 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Zhaizj.Framework.ORM.Utils {
|
||||
|
||||
internal class SqlUtil {
|
||||
|
||||
|
||||
public static List<String[]> getEntityProperties( String condition ) {
|
||||
|
||||
List<String[]> result = new List<String[]>();
|
||||
|
||||
String[] arrItem = condition.Split( ' ' );
|
||||
foreach (String str in arrItem) {
|
||||
if (str.IndexOf( '.' ) < 0) continue;
|
||||
|
||||
String[] arr = getPropertyItems( str );
|
||||
result.Add( arr );
|
||||
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public static String[] getPropertyItems( String str ) {
|
||||
|
||||
String[] arr = new string[2];
|
||||
|
||||
String key = "";
|
||||
String val = "";
|
||||
Boolean valBegin = false;
|
||||
char[] arrChar = str.ToCharArray();
|
||||
foreach (char ch in arrChar) {
|
||||
|
||||
if( ch == ' ' ) continue;
|
||||
|
||||
if (ch == '.') {
|
||||
valBegin = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (valBegin == false) {
|
||||
|
||||
if (isAbcAndNumber( ch ) == false) {
|
||||
key = "";
|
||||
continue;
|
||||
}
|
||||
key += ch;
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
if (isAbcAndNumber( ch ) == false) {
|
||||
break;
|
||||
}
|
||||
|
||||
val += ch;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
arr[0] = key;
|
||||
arr[1] = val;
|
||||
|
||||
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
private static Boolean isAbcAndNumber( char ch ) {
|
||||
return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".IndexOf( ch ) >= 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Zhaizj.Framework.Web;
|
||||
using Zhaizj.Framework.Members.Interface;
|
||||
|
||||
namespace Zhaizj.Framework.ORM.Utils {
|
||||
|
||||
internal class ValueSetter {
|
||||
|
||||
private static readonly ILog logger = LogManager.GetLogger( typeof( ValueSetter ) );
|
||||
|
||||
/// <summary>
|
||||
/// 1.5新增,针对已删除用户应用 null object 模式
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <param name="ep"></param>
|
||||
/// <param name="propertyValue"></param>
|
||||
public static void setEntityByCheckNull( IEntity obj, EntityPropertyInfo ep, Object propertyValue, int realUserId ) {
|
||||
|
||||
|
||||
if (propertyValue == null && rft.IsInterface( ep.Type, typeof( IUser ) )) {
|
||||
|
||||
IEntity user = getNullUser( realUserId );
|
||||
obj.set( ep.Name, user );
|
||||
}
|
||||
else {
|
||||
obj.set( ep.Name, propertyValue );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static IEntity getNullUser( int realUserId ) {
|
||||
|
||||
IUserFactory userFactory = CurrentRequest.getItem( "_user_factory" ) as IUserFactory;
|
||||
IEntity user = userFactory.NullUser() as IEntity;
|
||||
if (user != null) {
|
||||
user.set( "RealId", realUserId );
|
||||
}
|
||||
return user;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user