protected IList <T> RetornaListaDados <T>(TipoSql sql, object obj = null) { try { if (obj != null) { AdicionaParametro(obj, sql.ToString()); } else { AdicionaParametro(null, sql.ToString()); } ExecCommands con = new ExecCommands(); return(con.RetornaLista <T>(procedure, parametros, conectionString)); } catch (Exception ex) { Common.TratarLogErro(ex); return(null); } }
/// <summary> /// Retorna o número de registros de um SQL de seleção. /// Usado em conjunto com o método LoadDataWithSortExpression, ou após chamar SetInfoPaging. /// </summary> protected int GetCountWithInfoPaging(GDASession session, string sqlSelecionar, TipoSql tipoSql, params GDAParameter[] parameters) { string sql; // Recupera as cláusulas FROM e GROUP BY e verifica se há um WHERE no SQL bool temWhere; string groupBy, from = From(sqlSelecionar, out temWhere, out groupBy); int indexLimit = sqlSelecionar.ToLower().LastIndexOf(" limit "); int indexFrom = sqlSelecionar.ToLower().IndexOf(from.ToLower()); if ((indexLimit > indexFrom && _pageSize > 0) || !String.IsNullOrEmpty(groupBy)) { if (indexLimit > indexFrom && _pageSize > 0) { // Altera o LIMIT do SQL para considerar o número de páginas sqlSelecionar = sqlSelecionar.Substring(0, indexLimit) + " limit " + (tipoSql == TipoSql.Otimizado ? 0 : _startRow) + "," + (_pageSize * NUMERO_PAGINAS) + (sqlSelecionar.IndexOf(")", indexLimit) > -1 ? sqlSelecionar.Substring(sqlSelecionar.IndexOf(")", indexLimit)) : String.Empty); } // Faz a contagem de registros através de uma subquery sql = "select count(*) + " + _startRow + @" from (" + sqlSelecionar + ") as temp"; } else if (tipoSql == TipoSql.Otimizado && !String.IsNullOrEmpty(groupBy)) { // Recupera a tabela principal e seu alias string alias, tabela = GetTabelaFrom(from, GetAliasTabela(sqlSelecionar), null, out alias); // Recupera os campos que compõe a chave primária da tabela bool chaveComposta; string campo = GetCampoChave(session, tabela, out chaveComposta, alias); // Faz a contagem das chaves distintas do resultado sql = sqlSelecionar.Substring(indexFrom); sql = sql.Remove(sql.Length - groupBy.Length); sql = "select count(distinct " + campo + ") " + sql; } else { // Apenas faz a contagem dos registros que forem encontrados sql = "select count(*) " + sqlSelecionar.Substring(indexFrom); } return(ExecuteScalar <int>(session, sql, parameters)); }
/// <summary> /// Retorna o número de registros de um SQL de seleção. /// Usado em conjunto com o método LoadDataWithSortExpression, ou após chamar SetInfoPaging. /// </summary> protected int GetCountWithInfoPaging(string sqlSelecionar, TipoSql tipoSql, params GDAParameter[] parameters) { return(GetCountWithInfoPaging(null, sqlSelecionar, tipoSql, parameters)); }