Exemplo n.º 1
0
 /// <summary>
 /// Construtor padrão.
 /// </summary>
 /// <param name="baseRecord"></param>
 /// <param name="translator">
 /// Instancia do tradutor de acesso ao valor do campo.
 /// Armazena o nome do campo e a posição do campo no registro.
 /// </param>
 internal GDADataRecordEx(IDataRecord baseRecord, TranslatorDataInfoCollection translator) : base(baseRecord, translator)
 {
 }
Exemplo n.º 2
0
        /// <summary>
        /// Recupera os dados do objeto submetido com base na consulta fornecida.
        /// </summary>
        /// <param name="session">Sess�o utilizada para a execu��o do comando.</param>
        /// <param name="objData">Objeto onde os valores ser�o atribuidos.</param>
        /// <param name="sqlQuery">Consulta para a recupera��o dos dados.</param>
        /// <param name="parameters">Parametros utilizados na consulta.</param>
        /// <returns>Objecto com os valores da recupera��o j� atribu�dos.</returns>
        public Model RecoverData(GDASession session, Model objData, string sqlQuery, params GDAParameter[] parameters)
        {
            IDbConnection conn    = CreateConnection(session);
            IDbCommand    cmd     = CreateCommand(session, conn);
            string        newName = null;

            if (parameters != null)
            {
                for (int i = 0; i < parameters.Length; i++)
                {
                    try
                    {
                        newName = (parameters[i].ParameterName[0] != '?' ? parameters[i].ParameterName : UserProvider.ParameterPrefix + parameters[i].ParameterName.Substring(1) + UserProvider.ParameterSuffix);
                    }
                    catch (Exception ex)
                    {
                        throw new GDAException("Error on make parameter name '" + parameters[i].ParameterName + "'.", ex);
                    }
                    sqlQuery = sqlQuery.Replace(parameters[i].ParameterName, newName);
                    cmd.Parameters.Add(GDA.Helper.GDAHelper.ConvertGDAParameter(cmd, parameters[i], UserProvider));
                }
            }
            cmd.CommandText = sqlQuery;
            if (session == null && conn.State != ConnectionState.Open)
            {
                try
                {
                    conn.Open();
                }
                catch (Exception ex)
                {
                    throw new GDAException(ex);
                }
                GDAConnectionManager.NotifyConnectionOpened(conn);
            }
            IDataReader dReader = null;

            try
            {
                SendMessageDebugTrace("CommandText: " + cmd.CommandText);
                using (var executionHandler = Diagnostics.GDATrace.CreateExecutionHandler(cmd))
                    try
                    {
                        dReader = cmd.ExecuteReader();
                    }
                    catch (Exception ex)
                    {
                        ex = new GDAException(ex);
                        executionHandler.Fail(ex);
                        throw ex;
                    }
                if (dReader.Read())
                {
                    var mapping = new TranslatorDataInfoCollection(MappingManager.GetMappers <Model>(null, null));
                    mapping.ProcessFieldsPositions(dReader);
                    IDataRecord record = dReader;
                    RecoverValueOfResult(ref record, mapping, ref objData, false);
                }
                else
                {
                    throw new ItemNotFoundException("Item not found with submited parameters.");
                }
            }
            finally
            {
                if (dReader != null)
                {
                    dReader.Close();
                }
                if (session == null)
                {
                    try
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                    catch
                    {
                        SendMessageDebugTrace("Error close connection.");
                    }
                }
            }
            return(objData);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Recupera os valores do resultado e preenche o objeto submetido.
        /// </summary>
        /// <param name="dReader">DataReader contendo os dados.</param>
        /// <param name="recoverDataInfos">Lista dos campos a serem carregados.</param>
        /// <param name="objItem">Objeto que será preenchido.</param>
        /// <param name="implementIObjectDataRecord">Identifica se o tipo implementa o <see cref="IObjectDataRecord"/>.</param>
        /// <exception cref="GDAColumnNotFoundException"></exception>
        /// <exception cref="GDAException"></exception>
        public static void RecoverValueOfResult(ref IDataRecord dReader, TranslatorDataInfoCollection recoverDataInfos, ref object objItem, bool implementIObjectDataRecord)
        {
            if (objItem == null)
            {
                return;
            }
            foreach (TranslatorDataInfo rdi in recoverDataInfos)
            {
                if (rdi.FieldPosition < 0)
                {
                    continue;
                }
                object value;
                try
                {
                    value = dReader[rdi.FieldPosition];
                }
                catch (KeyNotFoundException)
                {
                    throw new GDAColumnNotFoundException(rdi.FieldName, "");
                }
                catch (Exception ex)
                {
                    throw new GDAException("Error to recover value of field: " + rdi.FieldName + "; Exception: " + ex.Message, ex);
                }
                if (value == DBNull.Value)
                {
                    value = null;
                }
                var sourceTypeName = value != null?value.GetType().Name : "null";

                if (rdi.PathLength > 0)
                {
                    try
                    {
                        rdi.SetValue(objItem, ConvertValue(value, rdi.Property.PropertyType));
                    }
                    catch (Exception ex)
                    {
                        if (value != null)
                        {
                            throw new GDAException(String.Format("Error to convert type {0} to type {1} of field:{2}.", sourceTypeName, rdi.Property.PropertyType.Name, rdi.FieldName), ex);
                        }
                        else
                        {
                            throw new GDAException(String.Format("Error to convert type {0} to null", rdi.Property.PropertyType.Name), ex);
                        }
                    }
                }
            }
            if (implementIObjectDataRecord)
            {
                var objetDataRecord = (IObjectDataRecord)objItem;
                if (objetDataRecord.LoadMappedsRecordFields)
                {
                    for (int i = 0; i < dReader.FieldCount; i++)
                    {
                        var value = dReader[i];
                        objetDataRecord.InsertRecordField(dReader.GetName(i), value == DBNull.Value ? null : value);
                    }
                }
                else
                {
                    for (int i = 0; i < dReader.FieldCount; i++)
                    {
                        var fieldName = dReader.GetName(i);
                        if (recoverDataInfos.FindIndex(f => string.Compare(f.FieldName, fieldName, true) == 0) < 0)
                        {
                            var value = dReader[i];
                            objetDataRecord.InsertRecordField(fieldName, value == DBNull.Value ? null : value);
                        }
                    }
                }
            }
        }