/// <summary> /// Executa uma procedure no banco de dados. /// </summary> /// <param name="action">Informação da ação de persistência.</param> /// <param name="transaction">Transação de persistência.</param> /// <returns>Retorna resultado da ação de persistência.</returns> protected virtual PersistenceActionResult ExecuteProcedureCommand(PersistenceAction action, IPersistenceTransactionExecuter transaction) { var trans = (PersistenceTransactionExecuter)transaction; var outputParametersIndexes = new List <int>(); var procedure = CreateProcedure(action); if (action.Parameters != null) { for (int i = 0; i < action.Parameters.Count; i++) { var paramDirection = (System.Data.ParameterDirection)((int)action.Parameters[i].Direction); procedure.AddParameter(new GDAParameter(action.Parameters[i].Name, action.Parameters[i].Value, paramDirection) { Size = action.Parameters[i].Size }); if (action.Parameters[i].Direction != Colosoft.Query.ParameterDirection.Input) { outputParametersIndexes.Add(i); } } } var da = new DataAccess(trans.Transaction.ProviderConfiguration); object result = null; try { result = da.ExecuteScalar(trans.Transaction, procedure); } catch (GDAException ex) { Exception ex2 = ex; if (ex.InnerException is System.Data.Common.DbException || ex.InnerException is DataException) { ex2 = ex.InnerException; } ex2 = new DataException(ResourceMessageFormatter.Create(() => Properties.Resources.Exception_ExecuteDatabaseCommand, ex2.Message).Format(), ex2); action.NotifyError(ex2); return(new PersistenceActionResult { Success = false, AffectedRows = -1, FailureMessage = ex2.Message }); } foreach (int index in outputParametersIndexes) { action.Parameters[index].Value = procedure[index]; } action.NotifyExecution(); var presult = new PersistenceActionResult() { ActionId = action.ActionId, Parameters = action.Parameters.ToArray(), Result = result, Success = true }; return(presult); }
/// <summary> /// Executa o comando de exclusão. /// </summary> /// <param name="commandText"></param> /// <param name="action"></param> /// <param name="transaction"></param> /// <param name="primaryKeyMappings"></param> /// <returns></returns> protected override PersistenceActionResult ExecuteDeleteCommand(string commandText, PersistenceAction action, IPersistenceTransactionExecuter transaction, Dictionary <int, int> primaryKeyMappings) { var trans = (PersistenceTransactionExecuter)transaction; var typeMetadata = TypeSchema.GetTypeMetadata(action.EntityFullName); bool hasRowVersion = typeMetadata.IsVersioned && (action.Conditional == null); int parameterCount = (action.Conditional != null ? action.Conditional.Count() : 0) + action.Parameters.Count; if (hasRowVersion) { parameterCount++; } var parameters = new GDAParameter[parameterCount]; var index = 0; if (action.Conditional == null) { for (int i = 0; i < action.Parameters.Count; i++) { var propertyMetadata = typeMetadata[action.Parameters[i].Name]; if (propertyMetadata == null) { parameters[i] = CreateParameter('?' + action.Parameters[i].Name, action.Parameters[i].Value); } else { parameters[i] = CreateParameter('?' + propertyMetadata.ColumnName, action.Parameters[i].Value); } } index = action.Parameters.Count; } else { foreach (var param in action.Conditional) { parameters[index] = CreateParameter(param.Name, param.Value); index++; } foreach (var param in action.Parameters) { parameters[index] = CreateParameter(param.Name, param.Value); index++; } } if (hasRowVersion) { parameters[index] = CreateParameter('?' + DataAccessConstants.RowVersionPropertyName, action.RowVersion, 0, (System.Data.ParameterDirection)((int)Colosoft.Query.ParameterDirection.InputOutput)); } var da = new DataAccess(trans.Transaction.ProviderConfiguration); int affectedRows = 0; try { affectedRows = da.ExecuteCommand(trans.Transaction, CommandType.Text, action.CommandTimeout, commandText, parameters); } catch (GDAException ex) { Exception ex2 = ex; if (ex.InnerException is System.Data.Common.DbException || ex.InnerException is DataException) { ex2 = ex.InnerException; } ex2 = new DataException(ResourceMessageFormatter.Create(() => Properties.Resources.Exception_ExecuteDatabaseCommand, ex2.Message).Format(), ex2); action.NotifyError(ex2); return(new PersistenceActionResult { Success = false, AffectedRows = -1, FailureMessage = ex2.Message }); } if (affectedRows == 0 && hasRowVersion) { return new PersistenceActionResult { Success = false, AffectedRows = -1, FailureMessage = ResourceMessageFormatter.Create(() => Properties.Resources.Exception_RowsNotAffected, typeMetadata.FullName, action.RowVersion).Format() } } ; action.NotifyExecution(); var actionResult = new PersistenceActionResult() { Success = true, AffectedRows = affectedRows, Parameters = action.Parameters.ToArray(), ActionId = action.ActionId, RowVersion = (hasRowVersion) ? (long)action.RowVersion : 0 }; return(actionResult); } }
/// <summary> /// Executa um comando de delete no banco de dados. /// </summary> /// <param name="commandText">Texto do comando a ser executado.</param> /// <param name="action">Informação da ação de persistência.</param> /// <param name="transaction">Transação do comando.</param> /// <param name="primaryKeyMappings">Dicionário que mapeia ids virtuais para reais.</param> /// <returns>Retorna resultado da ação de persistência.</returns> protected virtual PersistenceActionResult ExecuteDeleteCommand(string commandText, PersistenceAction action, IPersistenceTransactionExecuter transaction, Dictionary <int, int> primaryKeyMappings) { var trans = (PersistenceTransactionExecuter)transaction; var typeMetadata = TypeSchema.GetTypeMetadata(action.EntityFullName); bool hasRowVersion = typeMetadata.IsVersioned && (action.Conditional == null); var actionParameters = action.Parameters.Where(f => !(f.Value is PropertyReference || f.Value is ExpressionParameter || f.Value is QueryInfo || f.Value is Colosoft.Query.Queryable)); var conditionalParameters = action.Conditional != null?action.Conditional.Where(f => !(f.Value is PropertyReference || f.Value is ExpressionParameter || f.Value is QueryInfo || f.Value is Colosoft.Query.Queryable) && !actionParameters.Any(x => x.Name == f.Name)).ToArray() : new QueryParameter[0]; int parameterCount = conditionalParameters.Where(f => !actionParameters.Any(x => x.Name == f.Name)).Count() + actionParameters.Count(); if (hasRowVersion) { parameterCount++; } var parameters = new GDAParameter[parameterCount]; var index = 0; if (action.Conditional == null) { foreach (var actionParameter in actionParameters) { var propertyMetadata = typeMetadata[actionParameter.Name]; if (propertyMetadata == null) { parameters[index++] = CreateParameter(!string.IsNullOrEmpty(actionParameter.Name) && actionParameter.Name.StartsWith("?") ? actionParameter.Name : '?' + actionParameter.Name, actionParameter.Value); } else { parameters[index++] = CreateParameter('?' + propertyMetadata.Name, actionParameter.Value); } } } else { foreach (var param in conditionalParameters.Where(f => !actionParameters.Any(x => x.Name == f.Name))) { parameters[index] = CreateParameter(param.Name, param.Value); index++; } foreach (var param in actionParameters) { parameters[index] = CreateParameter(param.Name, param.Value); index++; } } if (hasRowVersion) { parameters[index] = CreateParameter('?' + DataAccessConstants.RowVersionPropertyName, action.RowVersion, 0, (System.Data.ParameterDirection)((int)Colosoft.Query.ParameterDirection.InputOutput)); } var da = new DataAccess(trans.Transaction.ProviderConfiguration); int affectedRows = 0; try { affectedRows = da.ExecuteCommand(trans.Transaction, CommandType.Text, action.CommandTimeout, commandText, parameters); } catch (GDAException ex) { Exception ex2 = ex; if (ex.InnerException is System.Data.Common.DbException || ex.InnerException is DataException) { ex2 = ex.InnerException; } ex2 = new DataException(ResourceMessageFormatter.Create(() => Properties.Resources.Exception_ExecuteDatabaseCommand, ex2.Message).Format(), ex2); action.NotifyError(ex2); return(new PersistenceActionResult { Success = false, AffectedRows = -1, FailureMessage = ex2.Message }); } if (affectedRows == 0 && hasRowVersion) { return new PersistenceActionResult { Success = false, AffectedRows = -1, FailureMessage = ResourceMessageFormatter.Create(() => Properties.Resources.Exception_RowsNotAffected, typeMetadata.FullName, action.RowVersion).Format() } } ; action.NotifyExecution(); var actionResult = new PersistenceActionResult() { Success = true, AffectedRows = affectedRows, Parameters = action.Parameters.ToArray(), ActionId = action.ActionId, RowVersion = (hasRowVersion) ? (long)action.RowVersion : 0 }; return(actionResult); }
/// <summary> /// Executa um comando de insert no banco de dados. /// </summary> /// <param name="commandText">Texto do comando a ser executado.</param> /// <param name="action">Informação da ação de persistência.</param> /// <param name="transaction">Transação do comando.</param> /// <param name="primaryKeyMappings">Dicionário que mapeia ids virtuais para reais.</param> /// <returns>Retorna resultado da ação de persistência.</returns> protected virtual PersistenceActionResult ExecuteInsertCommand(string commandText, PersistenceAction action, IPersistenceTransactionExecuter transaction, Dictionary <int, int> primaryKeyMappings) { var trans = (PersistenceTransactionExecuter)transaction; var typeMetadata = TypeSchema.GetTypeMetadata(action.EntityFullName); bool hasRowVersion = typeMetadata.IsVersioned; var onlyResultProperties = typeMetadata.GetVolatileProperties().Where(f => action.Parameters.Count(g => g.Name == f.Name) == 0); int onlyResultCount = onlyResultProperties.Count(); int parameterCount = action.Parameters.Count + onlyResultCount; if (hasRowVersion) { parameterCount++; } var resultParameters = new PersistenceParameter[action.Parameters.Count + onlyResultCount]; var parameters = new GDAParameter[parameterCount + onlyResultCount]; for (int i = 0; i < action.Parameters.Count; i++) { parameters[i] = CreateParameter('?' + action.Parameters[i].Name, action.Parameters[i].Value); if (action.Parameters[i].DbType != DbType.AnsiString) { parameters[i].DbType = action.Parameters[i].DbType; } resultParameters[i] = action.Parameters[i]; } int index = action.Parameters.Count; foreach (var property in onlyResultProperties) { parameters[index] = CreateParameter('?' + property.Name, "", 0, System.Data.ParameterDirection.InputOutput); resultParameters[index] = new PersistenceParameter(property.Name, ""); index++; } if (hasRowVersion) { parameters[index] = CreateParameter('?' + DataAccessConstants.RowVersionPropertyName, action.RowVersion, 0, System.Data.ParameterDirection.Output); } int affectedRows = 0; try { var da = new DataAccess(trans.Transaction.ProviderConfiguration); affectedRows = da.ExecuteCommand(trans.Transaction, CommandType.Text, action.CommandTimeout, commandText, parameters); } catch (Exception ex) { Exception ex2 = ex; if (ex.InnerException is System.Data.Common.DbException || ex.InnerException is DataException) { ex2 = ex.InnerException; } ex = new DataException(ResourceMessageFormatter.Create(() => Properties.Resources.Exception_ExecuteDatabaseCommand, ex2.Message).Format(), ex2); action.NotifyError(ex); return(new PersistenceActionResult() { Success = false, AffectedRows = affectedRows, ActionId = action.ActionId, FailureMessage = ex.Message, RowVersion = (hasRowVersion) ? (long)parameters[parameters.Length - 1].Value : 0 }); } var keyRepository = GetKeyRepository(trans.ProviderName); if (keyRepository.IsPosCommand(action.EntityFullName)) { IPropertyMetadata identityMetadata = typeMetadata.GetKeyProperties().FirstOrDefault(); if (identityMetadata != null && identityMetadata.ParameterType == Schema.PersistenceParameterType.IdentityKey) { for (int i = 0; i < parameters.Length; i++) { if (action.Parameters[i].Name == identityMetadata.Name) { int identityValue = 0; var key = keyRepository.GetPrimaryKey(transaction, typeMetadata.FullName); if (key is int) { identityValue = (int)key; } else if (key is uint) { identityValue = (int)(uint)key; } else { identityValue = Convert.ToInt32(key); } parameters[i].Value = identityValue; var virtualIdValue = action.Parameters[i].Value; int virtualId = 0; if (virtualIdValue is int) { virtualId = (int)virtualIdValue; } else if (virtualIdValue is uint) { virtualId = (int)(uint)virtualIdValue; } else { virtualId = Convert.ToInt32(virtualIdValue); } action.Parameters[i].Value = identityValue; primaryKeyMappings.Add(virtualId, identityValue); break; } } } } action.NotifyExecution(); return(new PersistenceActionResult() { Success = true, AffectedRows = affectedRows, Parameters = resultParameters, ActionId = action.ActionId, RowVersion = (hasRowVersion) ? (long)parameters[parameters.Length - 1].Value : 0 }); }