/// <summary> /// 从DataSet 中创建可以进行数据库操作的DBCommand。 /// </summary> /// <param name="db"></param> /// <param name="baseRule"></param> /// <param name="dsData"></param> /// <param name="dataInDocType"></param> /// <returns></returns> public IList <EntityDbCommandInfo> CreateCmdsFromDataSet(Database db, IBaseRule baseRule, DataSet dsData, object foreingKeyValue, object dataInDocType) { List <EntityDbCommandInfo> cmds = new List <EntityDbCommandInfo>(); ObjectDataMappingAttribute mappingAtt = AttributeConfigHelper.Instance.GetObjectDataMappingAttribute(dataInDocType); MB.Orm.Enums.OperationType opts = MB.Orm.Enums.OperationType.Insert | MB.Orm.Enums.OperationType.Update; if (mappingAtt.DeleteNotInIds) { opts = opts | MB.Orm.Enums.OperationType.DeleteNotIn; } else { opts = opts | MB.Orm.Enums.OperationType.Delete; } MB.Orm.Persistence.PersistenceManagerHelper persistenceManager = MB.Orm.Persistence.PersistenceManagerHelper.NewInstance; Dictionary <MB.Orm.Enums.OperationType, MB.Orm.DbSql.SqlString[]> sqlLib = MB.Orm.Mapping.Xml.SqlConfigHelper.Instance.GetObjectStandardEditSql(mappingAtt.MappingXmlFileName, opts); DataRow[] drsData = dsData.Tables[0].Select(); if (sqlLib.ContainsKey(OperationType.DeleteNotIn) && (foreingKeyValue != null && foreingKeyValue != System.DBNull.Value)) { EntityDbCommandInfo[] cmdDeletes = persistenceManager.CreateDeleteNotInDbCommand(db, sqlLib[OperationType.DeleteNotIn], drsData, mappingAtt.KeyName, foreingKeyValue); cmds.AddRange(cmdDeletes); } StringBuilder sqlB = new StringBuilder(); foreach (DataRow dr in drsData) { //空行跳过不进行处理 bool b = MB.Orm.Common.ValueValidated.Instance.RowIsNull(dr); if (b) { continue; } switch (dr.RowState) { case System.Data.DataRowState.Added: //增加 if (!sqlLib.ContainsKey(OperationType.Insert)) { continue; } if (!string.IsNullOrEmpty(mappingAtt.ForeingKeyName) && foreingKeyValue != null && dr.Table.Columns.Contains(mappingAtt.ForeingKeyName) && (dr[mappingAtt.ForeingKeyName] == System.DBNull.Value)) { dr[mappingAtt.ForeingKeyName] = foreingKeyValue; } DbCommand[] cmdAdds = persistenceManager.CreateDbCommandByDataRow(db, sqlLib[OperationType.Insert], dr); foreach (DbCommand ad in cmdAdds) { cmds.Add(new EntityDbCommandInfo(dr, ad)); } // cmds.AddRange(cmdAdds); break; case System.Data.DataRowState.Modified: //修改 if (!sqlLib.ContainsKey(OperationType.Update)) { continue; } DbCommand[] cmdUpdates = persistenceManager.CreateDbCommandByDataRow(db, sqlLib[OperationType.Update], dr); foreach (DbCommand ud in cmdUpdates) { cmds.Add(new EntityDbCommandInfo(dr, ud)); } // cmds.AddRange(cmdUpdates); break; case System.Data.DataRowState.Deleted: //删除为直接删除,所以这里不进行处理。 break; case System.Data.DataRowState.Detached: default: break; } } return(cmds); }
/// <summary> /// 从ObjectDataList 创建可进行数据库操作的DbCommand。 /// </summary> /// <param name="baseRule">指定操作的业务类(为空将根据集合中的实体类型中的配置信息来自动创建DBCommand)。</param> /// <param name="db">数据库操作DB.</param> /// <param name="dataList">数据</param> /// <returns></returns> public IList <EntityDbCommandInfo> CreateCmdsFromObjectDataList(IBaseRule baseRule, Database db, ObjectDataList dataList) { RuleSettingAttribute mappingAtt = null; if (baseRule != null) { mappingAtt = AttributeConfigHelper.Instance.GetRuleSettingAtt(baseRule); } if (mappingAtt != null && mappingAtt.BaseDataType == null) { throw new MB.Util.APPException("业务类:" + baseRule.GetType().FullName + " 必须要先配置主表数据在单据中的类型值。", MB.Util.APPMessageType.SysErrInfo); } //获取存储变化的数据对象。 IEnumerable <ObjectDataInfo> vals = dataList.GetCanSaveAndOrder(); //必须要进行排序以获取主表的键值 List <EntityDbCommandInfo> cmds = new List <EntityDbCommandInfo>(); object mainKeyValue = null; bool currentIsMain = false; MB.Orm.Persistence.PersistenceManagerHelper persistenceManager = MB.Orm.Persistence.PersistenceManagerHelper.NewInstance; foreach (ObjectDataInfo dataInfo in vals) { if (mappingAtt != null && (int)dataInfo.DataInDocType == (int)mappingAtt.BaseDataType)//获取基本对象的主键值。 { if (mappingAtt.GenerateKeyModel == GenerateKeyModel.OnDataSave) { //设置对象的主键 MB.Orm.Persistence.EntityIdentityHelper.NewInstance.FillEntityIdentity(dataInfo.ObjectData); } mainKeyValue = GetObjectKeyValue(baseRule, dataInfo); currentIsMain = true; } else { currentIsMain = false; } object data = dataInfo.ObjectData; //MB.Orm.Enums.OperationType operationType = ConvertDataStateToOperationType(dataInfo.DataState); //if (operationType == MB.Orm.Enums.OperationType.None) // continue; if (data == null) { continue; } if (data.GetType().IsSubclassOf(typeof(MB.Orm.Common.BaseModel))) { DbCommand[] acmd = null; if (string.IsNullOrEmpty(dataInfo.ExecuteXmlCfgSqlName)) { MB.Orm.Enums.OperationType operationType = ConvertEntityStateToOperationType((data as MB.Orm.Common.BaseModel).EntityState); if (operationType == MB.Orm.Enums.OperationType.None) { continue; } acmd = persistenceManager.GetDbCommand(db, data as MB.Orm.Common.BaseModel, operationType, dataInfo.SavePropertys); } else { acmd = persistenceManager.GetDbCommand(db, data as MB.Orm.Common.BaseModel, dataInfo.ExecuteXmlCfgSqlName); } //如果存在多个Command 上只需要记录一次主体对象就可以,避免多次进行引用约束判断 bool hasRegister = false; foreach (DbCommand d in acmd) { EntityDbCommandInfo ecmd = new EntityDbCommandInfo(data, d); if (!hasRegister) { ecmd.IsMainEntity = currentIsMain; hasRegister = true; } //目前只对主体数据进行删除的约束标记。 if (ecmd.IsMainEntity && dataInfo.DataState == ObjectDataState.Deleted) { ecmd.OperationType = OperationType.Delete; } ecmd.IsPartPropertyUpdate = (dataInfo.SavePropertys != null && dataInfo.SavePropertys.Length > 0); cmds.Add(ecmd); } } else if (data.GetType().IsSubclassOf(typeof(System.Data.DataSet))) { if (mainKeyValue == null) { throw new MB.Util.APPException("在批量存储对象明细前必须先获取主表键值!", MB.Util.APPMessageType.SysErrInfo); } if (baseRule == null) { throw new MB.Util.APPException("通过CreateCmdsFromObjectDataList 执行DataSet时,业务对象不能为空", Util.APPMessageType.SysErrInfo); } IList <EntityDbCommandInfo> tempCmds = CreateCmdsFromDataSet(db, baseRule, data as DataSet, mainKeyValue, dataInfo.DataInDocType); foreach (EntityDbCommandInfo tCmd in tempCmds) { cmds.Add(tCmd); } } else { throw new MB.RuleBase.Exceptions.DataTypeUnSupportException(data.GetType().FullName); } } return(cmds); }