private object GetDbQueryAggregationValue(DbQueryAggregateMode queryMode, Expression <Func <TModel, object> > columnSpecifier = null, Expression <Func <TModel, object> > specifierExpression = null, Expression <Func <TModel, object> > groupByExpression = null) { var queryStrategyProvider = _container.Resolve <IDbQueryStrategyProvider>(); var filterExpressions = new List <DbQueryWhereClause <TModel> >(); var groupByExpressions = new List <DbQueryGroupByClause <TModel> >(); if (specifierExpression != null) { filterExpressions.Add(new DbQueryWhereClause <TModel>() { Clause = specifierExpression, Operator = DbQueryConditionOperators.And }); } if (groupByExpression != null) { groupByExpressions.Add(new DbQueryGroupByClause <TModel>() { Clause = groupByExpression }); } var strategy = queryStrategyProvider.GetSelectAggregationStrategy( ModelStrategy, queryMode, filterExpressions, new DbQueryColumnClause <TModel>() { ColumExpression = columnSpecifier }, groupByExpressions); var sql = strategy.GetDbQueryScript(); var dbparams = strategy.GetDbParameters().ToList(); _serviceProvider.Open(); object value; try { value = _serviceProvider.QueryGetScalar(sql, dbparams); } catch (Exception exception) { throw new DbOperationException("ERROR_SQL_EXECUTION_FAILED", exception, sql, dbparams); } finally { _serviceProvider.Close(); } return(value); }
public object ExecuteScalar(string sql, object queryParams = null) { var autoClose = !(_provider.IsConnectionOpen()); if (autoClose) { _provider.Open(); } var dbParams = GetQueryDataParameters(queryParams); var scalar = _provider.QueryGetScalar(sql, dbParams); if (autoClose) { _provider.Close(); } return(scalar); }
private object GetDbQueryAggregationValue( DbQueryAggregateMode queryMode, Expression <Func <TModel, object> > columnSpecifier, Expression <Func <TModel, object> > conditionExpression = null) { if (queryMode != DbQueryAggregateMode.Any && queryMode != DbQueryAggregateMode.Count && queryMode != DbQueryAggregateMode.CountBig) { ParameterChecker.NotNull(columnSpecifier); } IDbQueryStrategy strategy; var filterExpressions = new List <DbQueryWhereClause <TModel> >(); if (conditionExpression != null) { filterExpressions.Add(new DbQueryWhereClause <TModel>() { Clause = conditionExpression, Operator = DbQueryConditionOperators.And }); } if (columnSpecifier == null) { strategy = _queryStrategyProvider.GetSelectAggregationStrategy( _queryTable.ModelStrategy, queryMode, (filterExpressions.Any()) ? filterExpressions : _whereClauses ); } else { var columnExpression = new DbQueryColumnClause <TModel>() { ColumExpression = columnSpecifier }; strategy = _queryStrategyProvider.GetSelectAggregationStrategy( _queryTable.ModelStrategy, queryMode, (filterExpressions.Any()) ? filterExpressions : _whereClauses, columnExpression ); } var sql = strategy.GetDbQueryScript(); var dbparams = strategy.GetDbParameters(); _serviceProvider.Open(); object value; try { value = _serviceProvider.QueryGetScalar(sql, dbparams); } catch (Exception exception) { throw new DbOperationException("ERROR_SQL_EXECUTION_FAILED", exception, sql, dbparams); } finally { _serviceProvider.Close(); } return(value); }