/// <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); }
/// <summary> /// Cria o enumerador. /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> protected IEnumerator <T> CreateEnumerator <T>() where T : GDADataRecord { _enumeratorCreated = true; if (_session == null && _connection.State != ConnectionState.Open) { try { _connection.Open(); } catch (Exception ex) { try { _connection.Dispose(); } catch { } _connection = null; throw new GDAException(ex); } GDAConnectionManager.NotifyConnectionOpened(_connection); } IDataReader dReader = null; try { StringBuilder sb = new StringBuilder(); sb.AppendFormat("GDADataRecordCursor: {0}\r\n", _command.CommandText); if (_command.Parameters.Count > 0) { sb.Append("--> Parameters:"); foreach (IDataParameter parameter in _command.Parameters) { if (parameter.Value is byte[]) { sb.Append("\r\n").Append(parameter.ParameterName).Append(" = byte[]"); } else { sb.Append("\r\n").Append(parameter.ParameterName).Append(" = ").Append(parameter.Value == null ? "NULL" : '"' + parameter.Value.ToString() + '"'); } } } SendMessageDebugTrace(sb.ToString()); using (var executionHandler = Diagnostics.GDATrace.CreateExecutionHandler(_command)) try { dReader = _command.ExecuteReader(); } catch (Exception ex) { ex = new GDAException(ex); executionHandler.Fail(ex); throw ex; } if (!_mapFieldsLoaded) { _mapFields = OnLoadTranslator(dReader); _mapFieldsLoaded = true; } int startPage = _startPage; int countPageSize = 0; while (dReader.Read()) { if (countPageSize == 0 && _startProcess != null) { _startProcess(this, EventArgs.Empty); } if (_usingPaging && !_provider.SupportSQLCommandLimit && startPage < _pageSize) { startPage++; continue; } yield return(CreateDataRecord <T>(dReader, _mapFields)); countPageSize++; if (_usingPaging && !_provider.SupportSQLCommandLimit && countPageSize >= _pageSize) { break; } } } finally { try { if (dReader != null) { dReader.Close(); dReader.Dispose(); } if (_command != null) { _command.Dispose(); } _command = null; } finally { if (_session == null) { try { _connection.Close(); _connection.Dispose(); } catch { SendMessageDebugTrace("Error close connection."); } } } } }
/// <summary> /// Recupera os valores da propriedade que se encontra no resultado da consulta. /// </summary> /// <param name="session">Sess�o para execu��o do comando.</param> /// <param name="query">Comando da consulta que ser� executada.</param> /// <param name="propertyName">Nome da propriedade que ser� recuperada.</param> /// <returns>Valores da propriedade encontrada.</returns> public IEnumerable <GDAPropertyValue> GetValues(GDASession session, IQuery query, string propertyName) { if (string.IsNullOrEmpty(propertyName)) { throw new ArgumentNullException("propertyName"); } if (query == null) { query = new Query(); } query.ReturnTypeQuery = typeof(Model); QueryReturnInfo returnInfo = query.BuildResultInfo <Model>(this.Configuration); List <Mapper> listAttr = returnInfo.RecoverProperties; string sqlQuery = returnInfo.CommandText; IDbConnection conn = CreateConnection(session); IDbCommand cmd = CreateCommand(session, conn); cmd.CommandType = CommandType.Text; bool usingPaging = false; if (query.SkipCount > 0 || query.TakeCount > 0) { usingPaging = true; } if (listAttr.Count == 0) { throw new GDAException("Not found properties mappers to model {0}.", typeof(Model).FullName); } if (returnInfo.Parameters != null) { for (int i = 0; i < returnInfo.Parameters.Count; i++) { try { string newName = (returnInfo.Parameters[i].ParameterName[0] != '?' ? returnInfo.Parameters[i].ParameterName : UserProvider.ParameterPrefix + returnInfo.Parameters[i].ParameterName.Substring(1) + UserProvider.ParameterSuffix); sqlQuery = sqlQuery.Replace(returnInfo.Parameters[i].ParameterName, newName); } catch (Exception ex) { throw new GDAException("Error on make parameter name '" + returnInfo.Parameters[i].ParameterName + "'.", ex); } cmd.Parameters.Add(GDA.Helper.GDAHelper.ConvertGDAParameter(cmd, returnInfo.Parameters[i], UserProvider)); } } if (usingPaging) { sqlQuery = UserProvider.SQLCommandLimit(listAttr, sqlQuery, query.SkipCount, query.TakeCount); } cmd.CommandText = sqlQuery; var property = listAttr.Find(delegate(Mapper m) { return(m.PropertyMapperName == propertyName); }); if (property == null) { throw new GDAException("Property {0} not found in {1}.", propertyName, typeof(Model).FullName); } IDataReader dReader = null; if (session == null) { try { conn.Open(); } catch (Exception ex) { throw new GDAException(ex); } GDAConnectionManager.NotifyConnectionOpened(conn); } 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.FieldCount == 0) { throw new GDAException("The query not return any field."); } var propertyExists = false; while (dReader.Read()) { var i = new GDADataRecord(dReader, null); if (!propertyExists && i.GetOrdinal(property.Name) < 0) { yield break; } propertyExists = true; yield return(new GDAPropertyValue(i.GetValue(property.Name), true)); } } finally { if (session == null) { try { conn.Close(); } catch { SendMessageDebugTrace("Error close connection."); } } if (dReader != null) { dReader.Close(); } } }
/// <summary> /// Recupera o Enumerator dos dados. /// </summary> /// <returns></returns> public IEnumerator <Model> GetEnumerator() { _enumeratorCreated = true; if (_session == null && _connection.State != ConnectionState.Open) { try { _connection.Open(); } catch (Exception ex) { try { _connection.Dispose(); } catch { } _connection = null; throw new GDAException(ex); } GDAConnectionManager.NotifyConnectionOpened(_connection); } IDataReader dReader = null; try { SendMessageDebugTrace("GDACursor: " + _command.CommandText); using (var executionHandler = Diagnostics.GDATrace.CreateExecutionHandler(_command)) try { dReader = _command.ExecuteReader(); } catch (Exception ex) { ex = new GDAException(ex); executionHandler.Fail(ex); throw ex; } if (dReader == null) { throw new InvalidOperationException(string.Format("Execute Reader result from IDbCommand \"{0}\" couldn't be null.", _command.GetType().FullName)); } _translatorDataInfo.ProcessFieldsPositions(dReader); int startPage = _startPage; int countPageSize = 0; while (dReader.Read()) { if (countPageSize == 0 && _startProcess != null) { _startProcess(this, EventArgs.Empty); } if (_usingPaging && !_provider.SupportSQLCommandLimit && startPage < _pageSize) { startPage++; continue; } if (!_isLoadValues) { Model objItem = new Model(); IDataRecord record = dReader; PersistenceObject <Model> .RecoverValueOfResult(ref record, _translatorDataInfo, ref objItem, false); yield return(objItem); } else { if (dReader[0] == DBNull.Value) { yield return(default(Model)); } else { yield return((Model)dReader[0]); } } countPageSize++; if (_usingPaging && !_provider.SupportSQLCommandLimit && countPageSize >= _pageSize) { break; } } } finally { try { if (dReader != null) { dReader.Close(); dReader.Dispose(); } _command.Dispose(); _command = null; } finally { if (_session == null) { try { _connection.Close(); _connection.Dispose(); _connection = null; } catch { SendMessageDebugTrace("Error close connection."); } } } } }