init lserp cs 5.0

This commit is contained in:
cyf
2026-07-10 15:25:05 +08:00
commit 90f3fda86a
3799 changed files with 976868 additions and 0 deletions
@@ -0,0 +1,22 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace Zhaizj.Framework.ORM.Operation {
internal class ConditionInfo {
public Type Type { get; set; }
public String SelectedItem { get; set; }
public String WhereString { get; set; }
public Dictionary<String,Object> Parameters { get; set; }
public int Count { get; set; }
public String Sql { get; set; }
public ObjectInfo State { get; set; }
}
}
@@ -0,0 +1,47 @@
using System;
using System.Data;
using Zhaizj.Framework;
using Zhaizj.Framework.Data;
using Zhaizj.Framework.Log;
namespace Zhaizj.Framework.ORM.Operation {
internal class CountOperation {
private static readonly ILog logger = LogManager.GetLogger( typeof( CountOperation ) );
public static int Count( Type t ) {
return Count( t, "" );
}
public static int Count( Type t, String condition ) {
return Count( condition, Entity.GetInfo( t ) );
}
private static int Count( String condition, EntityInfo entityInfo ) {
String countSql;
int result = 0;
SqlBuilder builder = new SqlBuilder( entityInfo );
if (strUtil.IsNullOrEmpty( condition )) {
countSql = String.Format( "select count(*) from {0}", entityInfo.TableName );
}
else {
countSql = builder.GetCountSql( condition );
}
logger.Info( LoggerUtil.SqlPrefix+"[Count(String condition) Sql]:" + countSql );
IDbCommand command = DataFactory.GetCommand( countSql, DbContext.getConnection( entityInfo ) );
try {
result = cvt.ToInt( command.ExecuteScalar() );
}
catch (Exception exception) {
logger.Error( exception.Message );
throw exception;
}
return result;
}
}
}
@@ -0,0 +1,103 @@
using System;
using System.Data;
using Zhaizj.Framework;
using Zhaizj.Framework.Data;
using Zhaizj.Framework.Log;
using System.Collections.Generic;
using Zhaizj.Framework.ORM.Caching;
namespace Zhaizj.Framework.ORM.Operation {
internal class DeleteOperation {
private static readonly ILog logger = LogManager.GetLogger( typeof( DeleteOperation ) );
public static int Delete( IEntity obj ) {
if (obj == null) throw new ArgumentNullException();
return Delete( obj.Id, obj, Entity.GetInfo( obj ) );
}
public static int Delete( Type t, int id ) {
IEntity obj = FindByIdOperation.FindById( id, new ObjectInfo( t ) );
if (obj != null) {
return Delete( obj );
}
return 0;
}
public static int Delete( int id, IEntity obj, EntityInfo entityInfo ) {
List<IInterceptor> ilist = MappingClass.Instance.InterceptorList;
for (int i = 0; i < ilist.Count; i++) {
ilist[i].BeforDelete( obj );
}
int rowAffected = 0;
rowAffected += deleteSingle( id, entityInfo );
if (entityInfo.ChildEntityList.Count > 0) {
foreach (EntityInfo info in entityInfo.ChildEntityList) {
rowAffected += deleteSingle( id, MappingClass.Instance.ClassList[info.Type.FullName] as EntityInfo );
}
}
if (entityInfo.Parent != null) {
IEntity objP = Entity.New( entityInfo.Parent.Type.FullName );
rowAffected += deleteSingle( id, Entity.GetInfo( objP ) );
}
for (int i = 0; i < ilist.Count; i++) {
ilist[i].AfterDelete( obj );
}
CacheUtil.CheckCountCache( "delete", obj, entityInfo );
return rowAffected;
}
public static int DeleteBatch( String condition, EntityInfo entityInfo ) {
if (strUtil.IsNullOrEmpty( condition )) {
return 0;
}
String deleteSql = new SqlBuilder( entityInfo ).GetDeleteSql( condition );
logger.Info(LoggerUtil.SqlPrefix+ "delete sql : " + deleteSql );
List<IInterceptor> ilist = MappingClass.Instance.InterceptorList;
for (int i = 0; i < ilist.Count; i++) {
ilist[i].BeforDeleteBatch( entityInfo.Type, condition );
}
IDbCommand cmd = DataFactory.GetCommand( deleteSql, DbContext.getConnection( entityInfo ) );
int rowAffected = cmd.ExecuteNonQuery();
logger.Info( "delete : " + rowAffected + " records affected" );
for (int i = 0; i < ilist.Count; i++) {
ilist[i].AfterDeleteBatch( entityInfo.Type, condition );
}
// update cache timestamp
CacheTime.updateTable( entityInfo.Type );
return rowAffected;
}
private static int Delete( Type t, String deleteCondition ) {
return DeleteBatch( deleteCondition, MappingClass.Instance.ClassList[t.FullName] as EntityInfo );
}
private static int deleteSingle( int id, EntityInfo entityInfo ) {
IDbCommand command = DataFactory.GetCommand( getDeleteSql( id, entityInfo ), DbContext.getConnection( entityInfo ) );
return command.ExecuteNonQuery();
}
private static String getDeleteSql( int id, EntityInfo entityInfo ) {
String str = String.Format( "delete from {0} where Id={1}", entityInfo.TableName, id );
logger.Info( "Delete(int id): " + str );
return str;
}
}
}
@@ -0,0 +1,104 @@
using System;
using System.Collections;
using Zhaizj.Framework;
using Zhaizj.Framework.Log;
using System.Data;
namespace Zhaizj.Framework.ORM.Operation
{
internal class FindAllOperation
{
private static readonly ILog logger = LogManager.GetLogger(typeof(FindAllOperation));
public static IList FindAll(ObjectInfo state)
{
IList parentResults = findAllPrivate(state);
if (state.EntityInfo.ChildEntityList.Count > 0)
{
return findAllFromChild(parentResults, state);
}
return parentResults;
}
public static DataTable FindDt(ObjectInfo state, string orderby)
{
String sql = "select * from " + state.EntityInfo.TableName + " order by " + orderby;
logger.Info(LoggerUtil.SqlPrefix + "[" + state.EntityInfo.Name + "_FindDt]" + sql);
return EntityPropertyUtil.FindDt(state, sql);
}
public static DataTable FindDt(ObjectInfo state)
{
String sql = "select * from " + state.EntityInfo.TableName;
logger.Info(LoggerUtil.SqlPrefix + "[" + state.EntityInfo.Name + "_FindDt]" + sql);
return EntityPropertyUtil.FindDt(state, sql);
}
public static int findMaxId(ObjectInfo state)
{
String sql = "select max(id) as id from " + state.EntityInfo.TableName;
logger.Info(LoggerUtil.SqlPrefix + "[" + state.EntityInfo.Name + "_FindDt]" + sql);
return EntityPropertyUtil.FindMaxId(state, sql);
}
public static DataTable FindDt(String condtion, ObjectInfo state, string orderby)
{
String sql = "select * from " + state.EntityInfo.TableName + " where " + condtion + " order by " + orderby;
logger.Info(LoggerUtil.SqlPrefix + "[" + state.EntityInfo.Name + "_FindDt]" + sql);
return EntityPropertyUtil.FindDt(state, sql);
}
public static DataTable FindDt(string fields, String condtion, ObjectInfo state, string orderby)
{
String sql = "select " + fields + " from " + state.EntityInfo.TableName + " where " + condtion + " order by " + orderby;
logger.Info(LoggerUtil.SqlPrefix + "[" + state.EntityInfo.Name + "_FindDt]" + sql);
return EntityPropertyUtil.FindDt(state, sql);
}
private static IList findAllPrivate(ObjectInfo state)
{
String sql = "select * from " + state.EntityInfo.TableName;
logger.Info(LoggerUtil.SqlPrefix + "[" + state.EntityInfo.Name + "_FindAll]" + sql);
return EntityPropertyUtil.FindList(state, sql);
}
private static IList findAllFromChild(IList parents, ObjectInfo state)
{
ArrayList results = new ArrayList();
foreach (EntityInfo info in state.EntityInfo.ChildEntityList)
{
ObjectInfo childState = new ObjectInfo(info);
childState.includeAll();
IList children = ObjectDB.FindAll(childState);
for (int i = 0; i < children.Count; i++)
{
IEntity child = children[i] as IEntity;
// state
//child.state.Order = state.Order;
results.Add(child);
parents.RemoveAt(Query.getIndexOfObject(parents, child));
}
}
if (parents.Count > 0) results.AddRange(parents);
results.Sort();
return results;
}
}
}
@@ -0,0 +1,136 @@
using System;
using System.Collections;
using System.Data;
using Zhaizj.Framework.Data;
using Zhaizj.Framework.Members.Interface;
using Zhaizj.Framework.Web;
using Zhaizj.Framework.ORM.Utils;
using Zhaizj.Framework.ORM.Caching;
namespace Zhaizj.Framework.ORM.Operation {
internal class FindByIdOperation {
private static readonly ILog logger = LogManager.GetLogger( typeof( FindByIdOperation ) );
public static IEntity FindById( int id, ObjectInfo state ) {
if (id < 0) return null;
IEntity result = null;
if (state.IsFindChild)
result = findByIdFromChild( id, state.EntityInfo );
if (result != null) return result;
if (state.Includer.EntityPropertyList == null)
state.Includer.IncludeAll();
return findById_Private( id, state );
}
private static IEntity findById_Private( int id, ObjectInfo state ) {
if (id < 0) return null;
IEntity result = null;
SqlBuilder sh = new SqlBuilder( state.EntityInfo );
processIncluder( state.Includer );
String sql = sh.GetFindById( id, state.Includer.SelectedProperty );
IDbCommand cmd = DataFactory.GetCommand( sql, DbContext.getConnection( state.EntityInfo ) );
IList list = new ArrayList();
IDataReader rd = null;
try {
rd = cmd.ExecuteReader();
while (rd.Read()) {
list.Add( FillUtil.Populate( rd, state ) );
}
}
catch (Exception ex) {
logger.Error( ex.Message );
logger.Error( ex.StackTrace );
throw ex;
}
finally {
OrmHelper.CloseDataReader( rd );
}
if (list.Count > 0) result = list[0] as IEntity;
result = setEntityProperty( result, id, state );
return result;
}
// TODO: performance
private static IEntity findByIdFromChild( int id, EntityInfo entityInfo ) {
foreach (EntityInfo ei in entityInfo.ChildEntityList) {
IEntity result = ObjectDB.FindById( id, new ObjectInfo( ei ) );
if (result != null) return result;
}
return null;
}
// select is prior to include
private static void processIncluder( Includer includer ) {
if (strUtil.IsNullOrEmpty( includer.SelectedProperty )) return;
if (includer.SelectedProperty.Equals( "*" ))
includer.IncludeAll();
else
includer.Include( SqlBuilder.GetIncludeProperty( includer.SelectedProperty ) );
}
private static IEntity setEntityProperty( IEntity obj, int id, ObjectInfo state ) {
if (obj == null)
return null;
IList entityPropertyList = state.EntityInfo.EntityPropertyList;
foreach (EntityPropertyInfo ep in entityPropertyList) {
if (!isPropertyInIncluder( ep, state.Includer.EntityPropertyList )) continue;
IEntity propertyValue = obj.get( ep.Name ) as IEntity;
if (propertyValue == null) continue;
int pid = propertyValue.Id;
if (pid <= 0) continue;
IEntity cachedValue = ObjectPool.FindOne( ep.Type, pid );
if (cachedValue == null) {
propertyValue = ObjectDB.FindById( pid, new ObjectInfo( ep.Type ) );
ObjectPool.Add( propertyValue );
}
else {
propertyValue = cachedValue;
}
ValueSetter.setEntityByCheckNull( obj, ep, propertyValue, pid );
}
return obj;
}
private static Boolean isPropertyInIncluder( EntityPropertyInfo p, IList _includeEntityPropertyList ) {
if (_includeEntityPropertyList == null) return false;
if (_includeEntityPropertyList.Count == 0) return false;
foreach (EntityPropertyInfo _include_ep in _includeEntityPropertyList) {
if (_include_ep.Name.Equals( p.Name )) return true;
}
return false;
}
}
}
@@ -0,0 +1,68 @@
using Zhaizj.Framework;
using Zhaizj.Framework.Data;
using System;
using System.Collections;
using System.Data;
using System.Collections.Generic;
using Zhaizj.Framework.ORM.Caching;
namespace Zhaizj.Framework.ORM.Operation {
internal class FindByOperation {
private static readonly ILog logger = LogManager.GetLogger( typeof( FindByOperation ) );
public static Query Find( String condition, ObjectInfo state ) {
Includer includer = state.Includer;
if (strUtil.HasText( includer.SelectedProperty ) && (includer.SelectedProperty != "*")) {
Query query = new Query( condition, state );
return query.select( includer.SelectedProperty );
}
return new Query( condition, state );
}
internal static IList Find( ConditionInfo condition ) {
IList results = ObjectPool.FindByQuery( condition );
if (results != null) return results;
if ("*".Equals( condition.SelectedItem )) {
condition.State.includeAll();
}
else {
condition.State.include( SqlBuilder.GetIncludeProperty( condition.SelectedItem ) );
}
IList includeEntityPropertyList = condition.State.Includer.EntityPropertyList;
IDbCommand cmd = DataFactory.GetCommand( condition.Sql, DbContext.getConnection( condition.State.EntityInfo ) );
foreach (String key in condition.Parameters.Keys) {
DataFactory.SetParameter( cmd, key, condition.Parameters[key] );
}
Hashtable hashtable = new Hashtable();
IDataReader record = null;
results = new ArrayList();
try {
record = cmd.ExecuteReader();
while (record.Read()) {
EntityPropertyUtil.Fill_EntityProperty_Ids( record, includeEntityPropertyList, ref hashtable );
results.Add( FillUtil.Populate( record, condition.State ) );
}
}
catch (Exception ex) {
logger.Error( ex.Message );
logger.Error( ex.StackTrace );
throw ex;
}
finally {
OrmHelper.CloseDataReader( record );
}
if (results.Count == 0) return results;
return EntityPropertyUtil.setEntityProperty( condition.State, results, hashtable );
}
}
}
@@ -0,0 +1,49 @@
using System;
using System.Collections;
using System.Data;
using System.Web;
using Zhaizj.Framework;
using Zhaizj.Framework.Data;
using Zhaizj.Framework.Web;
namespace Zhaizj.Framework.ORM.Operation {
internal class FindPageOperation {
private static readonly ILog logger = LogManager.GetLogger( typeof( FindPageOperation ) );
public static IList FindPage( ObjectInfo state ) {
return FindPage( state, "" );
}
public static IList FindPage( ObjectInfo state, String queryString ) {
// see: Zhaizj.Framework/Zhaizj.Framework/Web/Mvc/Routes/RouteTool.cs, line 211
if (state.Pager.getCurrent() <= 0) {
int page = CurrentRequest.getCurrentPage();
state.Pager.setCurrent( page );
}
if ( queryString != null && queryString.ToLower().StartsWith( "order " )) {
queryString = " " + queryString;
}
PageCondition pc = new PageCondition();
pc.ConditionStr = queryString;
pc.Property = state.Includer.SelectedProperty;
pc.CurrentPage = state.Pager.getCurrent();
pc.Size = state.Pager.getSize();
pc.OrderStr = state.Order;
pc.Pager = state.Pager;
String sql = new SqlBuilder( state.EntityInfo ).GetPageSql( pc );
return EntityPropertyUtil.FindList( state, sql );
}
}
}
@@ -0,0 +1,60 @@
using System;
using System.Collections;
using System.Data;
using System.Text;
using Zhaizj.Framework.Data;
namespace Zhaizj.Framework.ORM.Operation {
internal class FindRelationOperation {
private static readonly ILog logger = LogManager.GetLogger( typeof( FindRelationOperation ) );
public static IList FindDataOther( Type throughType, Type t, String order, int id ) {
// 1029
ObjectInfo state = new ObjectInfo( throughType );
String relationPropertyName = state.EntityInfo.GetRelationPropertyName( t );
EntityPropertyInfo info = state.EntityInfo.FindRelationProperty( t );
state.Order = order;
state.include( info.Name );
return ObjectDB.Find( state, relationPropertyName + ".Id=" + id ).listChildren( info.Name );
}
public static String GetSameTypeIds( Type throughType, Type t, int id ) {
// 1029
ObjectInfo state = new ObjectInfo( throughType );
String relationPropertyName = state.EntityInfo.GetRelationPropertyName( t );
EntityPropertyInfo info = state.EntityInfo.FindRelationProperty( t );
String ids = ObjectDB.Find(state, relationPropertyName + ".Id=" + id ).get( info.Name + ".Id" );
EntityPropertyInfo property = state.EntityInfo.GetProperty( relationPropertyName );
String sql = String.Format( "select distinct {0} from {1} where {2} in ({3}) and {0}<>{4}", property.ColumnName, state.EntityInfo.TableName, info.ColumnName, ids, id );
IDbCommand command = DataFactory.GetCommand( sql, DbContext.getConnection( state.EntityInfo ) );
IDataReader rd = null;
StringBuilder builder = new StringBuilder();
try {
rd = command.ExecuteReader();
while (rd.Read()) {
builder.Append( rd[0] );
builder.Append( "," );
}
}
catch (Exception exception) {
logger.Error( exception.Message );
throw exception;
}
finally {
OrmHelper.CloseDataReader( rd );
}
return builder.ToString().TrimEnd( ',' );
}
}
}
@@ -0,0 +1,22 @@
using System;
using System.Collections;
using System.Text;
namespace Zhaizj.Framework.ORM.Finder
{
internal abstract class FinderBase
{
protected ObjectInfo state;
public FinderBase()
{
}
public FinderBase(ObjectInfo state)
{
this.state = state;
}
public abstract IList Find();
}
}
@@ -0,0 +1,119 @@
using System;
using System.Collections.Generic;
using System.Data;
using Zhaizj.Framework.Data;
using Zhaizj.Framework.Log;
using Zhaizj.Framework.ORM.Caching;
namespace Zhaizj.Framework.ORM.Operation
{
internal class InsertOperation
{
private static readonly ILog logger = LogManager.GetLogger(typeof(InsertOperation));
public static Result Insert(IEntity obj)
{
if (obj == null) throw new ArgumentNullException();
Result result = Insert(obj, Entity.GetInfo(obj), true);
return result;
}
private static Result Insert(IEntity obj, EntityInfo entityInfo, Boolean isInsertParent)
{
Result result = Validator.Validate(obj, "insert");
if (result.HasErrors) return result;
if (isInsertParent && entityInfo.Parent != null)
{
IEntity objP = Entity.New(entityInfo.Type.BaseType.FullName);
List<EntityPropertyInfo> eplist = Entity.GetInfo(objP).SavedPropertyList;
foreach (EntityPropertyInfo info in eplist)
{
if (info.IsList) continue;
objP.set(info.Name, obj.get(info.Name));
}
ObjectDB.Insert(objP);
obj.Id = objP.Id;
}
List<IInterceptor> ilist = MappingClass.Instance.InterceptorList;
for (int i = 0; i < ilist.Count; i++)
{
ilist[i].BeforInsert(obj);
}
IDbCommand cmd = DataFactory.GetCommand(getInsertSql(entityInfo), DbContext.getConnection(entityInfo));
OrmHelper.SetParameters(cmd, "insert", obj, entityInfo);
//obj.Id = executeCmd( cmd, entityInfo );// CommandUtil.ExecuteCmd( cmd, entityInfo );
int retval = executeCmd(cmd, entityInfo, ref obj);
for (int i = 0; i < ilist.Count; i++)
{
ilist[i].AfterInsert(obj);
}
CacheUtil.CheckCountCache("insert", obj, entityInfo);
result.Info = obj;
CacheTime.updateTable(obj.GetType());
return result;
}
private static int executeCmd(IDbCommand cmd, EntityInfo entityInfo, ref IEntity obj)
{
// 读取操作结果
int retval = cmd.ExecuteNonQuery();
// 是否采用自增编号
if (DbConfig.Instance.IsAutoId)
{
String sqlId = String.Format("select id from {0} order by id desc", entityInfo.TableName);
sqlId = entityInfo.Dialect.GetLimit(sqlId, 1);
//logger.Info( "get id sql:" + sqlId );
cmd.CommandText = sqlId;
//return cvt.ToInt(cmd.ExecuteScalar());
obj.Id = cvt.ToInt(cmd.ExecuteScalar());
}
return retval;
}
public static Result InsertSelf(IEntity obj)
{
return Insert(obj, Entity.GetInfo(obj), false);
}
private static String getInsertSql(EntityInfo entityInfo)
{
String str = "insert into " + entityInfo.TableName + " (";
String fStr = "";
String vStr = ") values(";
for (int i = 0; i < entityInfo.SavedPropertyList.Count; i++)
{
EntityPropertyInfo info = entityInfo.SavedPropertyList[i];
if ((
( /**/!DbConfig.Instance.IsAutoId || !(info.Name.ToLower() == "id") || (entityInfo.Parent != null))
&& info.SaveToDB)
&& (!info.IsList && !info.IsList)
)
{
String col = info.ColumnName ?? "";
fStr = fStr + "[" + col + "]" + ", ";
vStr = vStr + entityInfo.Dialect.GetParameter(info.ColumnName) + ", ";
}
}
fStr = fStr.Trim().TrimEnd(',');
vStr = vStr.Trim().TrimEnd(',');
str = str + fStr + vStr + ")";
logger.Info(LoggerUtil.SqlPrefix + entityInfo.Name + " InsertSql" + str);
return str;
}
}
}
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Zhaizj.Framework.Data;
namespace Zhaizj.Framework.ORM.Operation {
internal class PageCondition {
private static readonly ILog logger = LogManager.GetLogger( typeof( PageCondition ) );
public String ConditionStr { get; set; }
public String Property { get; set; }
public int CurrentPage { get; set; }
public int Size { get; set; }
public String OrderStr { get; set; }
public ObjectPage Pager { get; set; }
public int beginCount( String countSql, ObjectPage pager, EntityInfo entityInfo ) {
String commandText = countSql;
logger.Info( "[Page Record Count] " + commandText );
IDbCommand command = DataFactory.GetCommand( commandText, DbContext.getConnection( entityInfo ) );
pager.RecordCount = cvt.ToInt( command.ExecuteScalar() );
pager.computePageCount();
pager.resetCurrent();
return pager.getCurrent();
}
}
}
@@ -0,0 +1,189 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Zhaizj.Framework.Data;
using Zhaizj.Framework.Log;
using Zhaizj.Framework.ORM.Caching;
namespace Zhaizj.Framework.ORM.Operation {
internal class UpdateOperation {
private static readonly ILog logger = LogManager.GetLogger( typeof( UpdateOperation ) );
public static Result Update( IEntity obj ) {
if (obj == null) throw new ArgumentNullException();
EntityInfo ei = Entity.GetInfo( obj );
return Update( obj, ei );
}
private static Result Update( IEntity obj, EntityInfo entityInfo ) {
Result result = Validator.Validate( obj, "update" );
if (result.IsValid == false) return result;
List<IInterceptor> ilist = MappingClass.Instance.InterceptorList;
for (int i = 0; i < ilist.Count; i++) {
ilist[i].BeforUpdate( obj );
}
updateSingle( obj, entityInfo );
if (entityInfo.Parent != null) {
IEntity objParent = Entity.New( entityInfo.Parent.Type.FullName );
setParentValueFromChild( objParent, obj );
updateSingle( objParent, Entity.GetInfo( objParent ) );
}
CacheUtil.CheckCountCache( "insert", obj, entityInfo );
for (int i = 0; i < ilist.Count; i++) {
ilist[i].AfterUpdate( obj );
}
result.Info = obj;
// update cache timestamp
CacheTime.updateTable( entityInfo.Type );
return result;
}
public static void Update( IEntity obj, String[] arrPropertyName ) {
if (obj == null) throw new ArgumentNullException();
Update( obj, arrPropertyName, Entity.GetInfo( obj ) );
}
public static void Update( IEntity obj, String[] arrPropertyName, EntityInfo entityInfo ) {
if (obj == null) throw new ArgumentNullException();
StringBuilder builder = new StringBuilder( String.Empty );
builder.Append( "update " );
builder.Append( entityInfo.TableName );
builder.Append( " set " );
for (int i = 0; i < arrPropertyName.Length; i++) {
String columnName = entityInfo.GetColumnName( arrPropertyName[i] );
builder.Append( columnName );
builder.Append( "=" );
builder.Append( entityInfo.Dialect.GetParameter( columnName ) );
builder.Append( "," );
}
builder.Append( " where Id=" );
builder.Append( entityInfo.Dialect.GetParameter( "Id" ) );
String commandText = builder.ToString().Replace( ", where", " where" );
logger.Info( LoggerUtil.SqlPrefix+entityInfo.Name + "[" + entityInfo.TableName + "] Update Sql" + commandText );
List<IInterceptor> ilist = MappingClass.Instance.InterceptorList;
for (int i = 0; i < ilist.Count; i++) {
ilist[i].BeforUpdate( obj );
}
IDbCommand cmd = DataFactory.GetCommand( commandText, DbContext.getConnection( entityInfo ) );
for (int i = 0; i < arrPropertyName.Length; i++) {
Object parameterValue = obj.get( arrPropertyName[i] );
String columnName = entityInfo.GetColumnName( arrPropertyName[i] );
DataFactory.SetParameter( cmd, columnName, parameterValue );
}
int id = obj.Id;
DataFactory.SetParameter( cmd, "Id", id );
cmd.ExecuteNonQuery();
for (int i = 0; i < ilist.Count; i++) {
ilist[i].AfterUpdate( obj );
}
// update cache timestamp
CacheTime.updateTable( entityInfo.Type );
CacheUtil.CheckCountCache( "insert", obj, entityInfo );
}
public static void UpdateBatch( IEntity obj, String action, String condition ) {
if (obj == null) throw new ArgumentNullException();
UpdateBatch( obj, action, condition, Entity.GetInfo( obj ) );
}
private static int UpdateBatch( IEntity obj, String action, String condition, EntityInfo entityInfo ) {
if (obj == null) throw new ArgumentNullException();
if (strUtil.IsNullOrEmpty( action )) {
logger.Info( "no sql is executed : action String is empty" );
return 0;
}
//action = action.Trim().ToLower();
//if (!action.StartsWith( "set" )) {
// action = "set " + action;
//}
// http://www.Zhaizj.Framework.com/rubywu
// http://www.Zhaizj.Framework.com/Forum1/Topic/1706
if (!action.Trim().ToLower().StartsWith( "set" )) {
action = "set " + action;
}
condition = condition.Trim().ToLower();
if (!condition.StartsWith( "where" )) {
condition = "where " + condition;
}
String sql = String.Format( "update {0} {1} {2}", entityInfo.TableName, action, condition );
logger.Info(LoggerUtil.SqlPrefix+ "update sql : " + sql );
List<IInterceptor> ilist = MappingClass.Instance.InterceptorList;
for (int i = 0; i < ilist.Count; i++) {
ilist[i].BeforUpdateBatch( obj.GetType(), action, condition );
}
IDbCommand cmd = DataFactory.GetCommand( sql, DbContext.getConnection( entityInfo ) );
int rowAffected = cmd.ExecuteNonQuery();
logger.Info( "update : " + rowAffected + " records affected" );
for (int i = 0; i < ilist.Count; i++) {
ilist[i].AfterUpdateBatch( obj.GetType(), action, condition );
}
// update cache timestamp
CacheTime.updateTable( entityInfo.Type );
return rowAffected;
}
private static void updateSingle( IEntity obj, EntityInfo entityInfo ) {
IDbCommand cmd = DataFactory.GetCommand( getUpdateSql( entityInfo ), DbContext.getConnection( entityInfo ) );
OrmHelper.SetParameters( cmd, "update", obj, entityInfo );
DataFactory.SetParameter( cmd, "Id", obj.Id );
cmd.ExecuteNonQuery();
}
private static String getUpdateSql( EntityInfo entityInfo ) {
StringBuilder builder = new StringBuilder( "update " );
builder.Append( entityInfo.TableName );
builder.Append( " set " );
for (int i = 0; i < entityInfo.SavedPropertyList.Count; i++) {
EntityPropertyInfo info = entityInfo.SavedPropertyList[i];
if (((info.Name.ToLower() != "id") && info.SaveToDB) && !info.IsList) {
builder.Append( info.ColumnName );
builder.Append( "=" );
builder.Append( entityInfo.Dialect.GetParameter( info.ColumnName ) );
builder.Append( ", " );
}
}
builder.Remove( builder.Length - 2, 2 );
builder.Append( " where Id=" );
builder.Append( entityInfo.Dialect.GetParameter( "Id" ) );
logger.Info( LoggerUtil.SqlPrefix+" [update sql] " + builder.ToString() );
return builder.ToString();
}
private static void setParentValueFromChild( IEntity objParent, IEntity objChild ) {
List<EntityPropertyInfo> eplist = Entity.GetInfo( objParent ).SavedPropertyList;
foreach (EntityPropertyInfo info in eplist) {
objParent.set( info.Name, objChild.get( info.Name ) );
}
}
}
}