Ejemplo n.º 1
0
        /// <summary>
        /// Recupera os dados do objeto submetido tendo como base os valores
        /// da chave contidos no objeto submetido.
        /// </summary>
        /// <param name="session">Sess�o utilizada para a execu��o do comando.</param>
        /// <param name="objData">Objeto contendo os dados das chaves.</param>
        /// <returns>Model com os dados recuperados.</returns>
        public Model RecoverData(GDASession session, Model objData)
        {
            string        sqlParam   = "";
            StringBuilder buf        = new StringBuilder("SELECT ");
            List <Mapper> properties = Keys;

            if (properties.Count == 0)
            {
                throw new GDAException("In model {0} not found keys for to recover data.", objData.GetType().FullName);
            }
            DirectionParameter[] dp = new DirectionParameter[] {
                DirectionParameter.Input,
                DirectionParameter.InputOutput,
                DirectionParameter.OutputOnlyInsert
            };
            List <Mapper> columns = MappingManager.GetMappers <Model>(null, dp);

            foreach (Mapper column in columns)
            {
                buf.Append(UserProvider.QuoteExpression(column.Name)).Append(",");
            }
            buf.Remove(buf.Length - 1, 1);
            buf.Append(" FROM ").Append(SystemTableName).Append(" ");
            GDAParameter[] parameters = new GDAParameter[properties.Count];
            int            i          = 0;

            foreach (Mapper mapper in properties)
            {
                if (sqlParam != "")
                {
                    sqlParam += " AND ";
                }
                parameters[i] = new GDAParameter(UserProvider.ParameterPrefix + mapper.Name + UserProvider.ParameterSuffix, typeof(Model).GetProperty(mapper.PropertyMapper.Name).GetValue(objData, null));
                sqlParam     += UserProvider.QuoteExpression(mapper.Name) + "=" + parameters[i].ParameterName;
                i++;
            }
            buf.Append("WHERE ").Append(sqlParam);
            return(RecoverData(session, objData, buf.ToString(), parameters));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Recupera as propriedades volatéis.
        /// </summary>
        /// <param name="actions"></param>
        /// <param name="result"></param>
        /// <param name="transaction">Transação usada na execução.</param>
        protected override void RetrievePersistenceVolatileProperties(PersistenceAction[] actions, PersistenceActionResult[] result, IPersistenceTransactionExecuter transaction)
        {
            for (int actionIndex = 0; actionIndex < actions.Length; actionIndex++)
            {
                if (actions[actionIndex].Type == PersistenceActionType.Insert || actions[actionIndex].Type == PersistenceActionType.Update)
                {
                    var action             = actions[actionIndex];
                    var actionResult       = result[actionIndex];
                    var typeMetadata       = TypeSchema.GetTypeMetadata(action.EntityFullName);
                    var volatileProperties = typeMetadata.GetVolatileProperties().ToArray();
                    if (!typeMetadata.IsVersioned && volatileProperties.Length == 0)
                    {
                        continue;
                    }
                    var commandText = new StringBuilder();
                    var keysIndex   = new List <Tuple <int, IPropertyMetadata> >();
                    for (int i = 0; i < action.Parameters.Count; i++)
                    {
                        var propertyMetadata = typeMetadata[action.Parameters[i].Name];
                        if (propertyMetadata.ParameterType == Data.Schema.PersistenceParameterType.IdentityKey || propertyMetadata.ParameterType == Data.Schema.PersistenceParameterType.Key)
                        {
                            keysIndex.Add(new Tuple <int, IPropertyMetadata>(i, propertyMetadata));
                        }
                    }
                    if (keysIndex.Count == 0)
                    {
                        continue;
                    }
                    var  parameters = new GDA.Collections.GDAParameterCollection();
                    bool isFirst    = true;
                    commandText.Append("SELECT ");
                    var selectCommandText = new StringBuilder();
                    if (typeMetadata.IsVersioned)
                    {
                        selectCommandText.Append("CAST(ORA_ROWSCN AS NUMBER(18,0)) AS ").AppendFormat("\"{0}\"", Query.DataAccessConstants.RowVersionPropertyName);
                        isFirst = false;
                    }
                    foreach (var property in typeMetadata.GetVolatileProperties())
                    {
                        if (!isFirst)
                        {
                            selectCommandText.Append(", ");
                        }
                        isFirst = false;
                        if (typeMetadata.IsVersioned && property.Name == Query.DataAccessConstants.RowVersionPropertyName)
                        {
                            continue;
                        }
                        selectCommandText.AppendFormat("\"{0}\" AS \"{1}\"", property.ColumnName.ToUpper(), property.Name);
                    }
                    var tableNameTranslatedName = (Query.Database.TranslatedTableName)Translator.GetName(new EntityInfo(action.EntityFullName));
                    isFirst = true;
                    commandText.Append(selectCommandText).Append(" FROM ").AppendFormat("\"{0}\".\"{1}\"", tableNameTranslatedName.Schema, tableNameTranslatedName.Name).Append(" WHERE ");
                    var paramaterCount = 1;
                    foreach (var i in keysIndex)
                    {
                        if (!isFirst)
                        {
                            commandText.Append(" AND ");
                        }
                        isFirst = false;
                        var whereParameter = new GDA.GDAParameter("?" + (paramaterCount++), action.Parameters[i.Item1].Value);
                        parameters.Add(whereParameter);
                        commandText.AppendFormat("\"{0}\"", i.Item2.ColumnName.ToUpper()).Append("=").Append(whereParameter.ParameterName);
                    }
                    var transaction2     = (PersistenceTransactionExecuter)transaction;
                    var da               = new DataAccess(transaction2.Transaction.ProviderConfiguration);
                    var resultParameters = new List <PersistenceParameter>();
                    using (var enumerator = da.LoadResult(transaction2.Transaction, commandText.ToString(), parameters.ToArray()).GetEnumerator())
                    {
                        if (enumerator.MoveNext())
                        {
                            var record = enumerator.Current;
                            for (var i = 0; i < record.FieldCount; i++)
                            {
                                var    parameterName = record.GetName(i);
                                object value         = null;
                                if (record.BaseDataRecord.GetDataTypeName(i) == "TimeStampTZ")
                                {
                                                                        #if UNMANAGED
                                    // Recupera o valor do Oracle
                                    var oracleTimeStampTZ = ((global::Oracle.DataAccess.Client.OracleDataReader)record.BaseDataRecord)
                                                            .GetOracleTimeStampTZ(i);
#else
                                    var oracleTimeStampTZ = ((global::Oracle.ManagedDataAccess.Client.OracleDataReader)record.BaseDataRecord).GetOracleTimeStampTZ(i);
                                                                        #endif
                                    value = new DateTimeOffset((DateTime)oracleTimeStampTZ, TimeSpan.Parse(oracleTimeStampTZ.TimeZone == "+00:00" ? "00:00" : oracleTimeStampTZ.TimeZone));
                                }
                                else
                                {
                                    value = record.GetValue(i);
                                }
                                resultParameters.Add(new PersistenceParameter(parameterName, value));
                                if (typeMetadata.IsVersioned && StringComparer.InvariantCultureIgnoreCase.Equals(Query.DataAccessConstants.RowVersionPropertyName, parameterName))
                                {
                                    try
                                    {
                                        if (value is DBNull || value == null)
                                        {
                                            actionResult.RowVersion = 0;
                                        }
                                        else
                                        {
                                            actionResult.RowVersion = Convert.ToInt64(value);
                                        }
                                    }
                                    catch
                                    {
                                    }
                                }
                            }
                        }
                    }
                    if (actionResult.Parameters != null)
                    {
                        foreach (var i in actionResult.Parameters)
                        {
                            if (!resultParameters.Any(f => StringComparer.InvariantCultureIgnoreCase.Equals(f.Name, i.Name)))
                            {
                                resultParameters.Add(i);
                            }
                        }
                    }
                    actionResult.Parameters = resultParameters.ToArray();
                }
            }
        }