예제 #1
0
파일: DbTable`T.cs 프로젝트: tofx/data
        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);
        }
예제 #2
0
파일: DbClient.cs 프로젝트: tofx/data
        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);
        }
예제 #3
0
        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);
        }