Exemplo n.º 1
0
        private void ExecuteModelDbOperation(IDbQueryStrategy strategy, TModel model = null)
        {
            Exception exception = null;
            var       dbparams  = (model == null)
                ? new List <IDbDataParameter>()
                : DbModelBindingHelper.BindingModelToParameters(model, ModelStrategy.PropertyStrategies, strategy.GetDbParameters());

            _serviceProvider.Open();

            try
            {
                if (_serviceProvider.Execute(strategy.GetDbQueryScript(), dbparams) == 0)
                {
                    throw new DbChangeNoAffectException();
                }
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            _serviceProvider.Close();

            if (exception != null)
            {
                throw new DbAggregateException(exception);
            }
        }
Exemplo n.º 2
0
Arquivo: DbTable.cs Projeto: tofx/data
        public void Empty()
        {
            var queryStrategyProvider = _container.Resolve <IDbQueryStrategyProvider>();
            var strategy = queryStrategyProvider.GetEmptyStrategy(_modelStrategy);

            _serviceProvider.Open();
            _serviceProvider.Execute(strategy.GetDbQueryScript(), new List <IDbDataParameter>());
            _serviceProvider.Close();
        }
Exemplo n.º 3
0
        public int Execute(string sql, object dataParams = null)
        {
            var autoClose = !(_provider.IsConnectionOpen());

            if (autoClose)
            {
                _provider.Open();
            }

            var dbParams     = GetQueryDataParameters(dataParams);
            var affactedRows = _provider.Execute(sql, dbParams);

            if (autoClose)
            {
                _provider.Close();
            }

            return(affactedRows);
        }
Exemplo n.º 4
0
        public int Delete(IEnumerable <TModel> models, bool throwIfAnyModelFailed = true)
        {
            int success = 0;

            var queryStrategy = _queryStrategyProvider.GetDeleteStrategy <TModel>(_queryTable.ModelStrategy);
            var sql           = queryStrategy.GetDbQueryScript();
            var dbparams      = queryStrategy.GetDbParameters();
            var exceptions    = new List <DbOperationException>();

            _serviceProvider.Open();

            foreach (var model in models)
            {
                DbModelBindingHelper.BindingModelToParameters(
                    model, _queryTable.ModelStrategy.PropertyStrategies, dbparams);

                try
                {
                    if (_serviceProvider.Execute(sql, dbparams) > 0)
                    {
                        success++;
                    }
                    else
                    {
                        exceptions.Add(new DbOperationException("ERROR_SQL_EXECUTION_FAILED", sql, dbparams));
                    }
                }
                catch (Exception ex)
                {
                    exceptions.Add(new DbOperationException("ERROR_SQL_EXECUTION_FAILED", ex, sql, dbparams));
                }
            }

            _serviceProvider.Close();

            if (throwIfAnyModelFailed)
            {
                throw new DbMultipleOperationsException(exceptions);
            }

            return(success);
        }