public void Delete(TModel model) { var queryStrategy = _queryStrategyProvider.GetDeleteStrategy <TModel>(_queryTable.ModelStrategy); var sql = queryStrategy.GetDbQueryScript(); var dbparams = queryStrategy.GetDbParameters(); DbModelBindingHelper.BindingModelToParameters( model, _queryTable.ModelStrategy.PropertyStrategies, dbparams); try { _serviceProvider.Open(); if (_serviceProvider.Execute(sql, dbparams) == 0) { throw new DbOperationException("ERROR_SQL_EXECUTION_FAILED", sql, dbparams); } } catch (Exception exception) { throw new DbOperationException("ERROR_SQL_EXECUTION_FAILED", exception, sql, dbparams); } finally { _serviceProvider.Close(); } }
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); } }
public IEnumerable <dynamic> InvokeGet(TModel model) { var parameters = BindingModelPropertyToParameter( model, _strategy.GetModelPropertyBindings(), _strategy.GetDbParameters()).ToList(); try { _serviceProvider.Open(); var reader = _serviceProvider.RunStoredProcGetReader(_strategy.GetDbProcName(), parameters); var items = new List <dynamic>(); var records = DbModelBindingHelper.GetDataRecords(reader); foreach (var record in records) { dynamic item = new ExpandoObject(); var itemProps = (IDictionary <string, object>)item; for (var i = 0; i < record.FieldCount; i++) { try { itemProps.Add(record.GetName(i).Trim(), reader.GetValue(i)); } catch (IndexOutOfRangeException) { itemProps.Add(record.GetName(i).Trim(), DBNull.Value); } } items.Add(item); } reader.Close(); return(items); } catch (Exception exception) { throw new DbOperationException( "ERROR_SQL_PROCEDURE_EXECUTION_FAILED", exception, _strategy.GetDbProcName(), parameters); } finally { _serviceProvider.Close(); } }
public IEnumerator <TModel> GetEnumerator() { var queryStrategy = _queryStrategyProvider.GetSelectStrategy( _queryTable.ModelStrategy, _skipCount, _takeCount, _whereClauses, _orderbyClauses); var sql = queryStrategy.GetDbQueryScript(); var dbparams = queryStrategy.GetDbParameters(); IDataReader reader = null; var items = new List <TModel>(); _serviceProvider.Open(); try { reader = _serviceProvider.QueryGetReader(sql, dbparams); if (_skipCount == 0 && _takeCount == 0) { var records = DbModelBindingHelper.GetDataRecords(reader); items.AddRange( records.Select(record => DbModelBindingHelper.BindingRecordToModel <TModel>( record, _queryTable.ModelStrategy.PropertyStrategies)) ); } else { while (reader.Read()) { items.Add(DbModelBindingHelper.BindingRecordToModel <TModel>( reader, _queryTable.ModelStrategy.PropertyStrategies)); } } } catch (Exception exception) { throw new DbOperationException("ERROR_SQL_EXECUTION_FAILED", exception, sql, dbparams); } finally { reader?.Close(); _serviceProvider.Close(); } return(items.GetEnumerator()); }
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); }
private void ExecuteModelDbOperation(IDbQueryStrategy strategy, IEnumerable <TModel> models) { var exceptions = new Dictionary <int, Exception>(); _serviceProvider.Open(); _serviceProvider.BeginTransaction(); var modelIdx = 0; foreach (var model in models) { try { var dbparams = DbModelBindingHelper.BindingModelToParameters( model, ModelStrategy.PropertyStrategies, strategy.GetDbParameters()); if (_serviceProvider.Execute(strategy.GetDbQueryScript(), dbparams) == 0) { throw new DbChangeNoAffectException(); } } catch (Exception ex) { exceptions.Add(modelIdx, ex); } modelIdx++; } if (!exceptions.Any()) { _serviceProvider.Commit(); _serviceProvider.Close(); } else { _serviceProvider.Rollback(); _serviceProvider.Close(); throw new DbAggregateException(exceptions); } }
public IEnumerable <dynamic> ExecuteQuery(string sql, object queryParams = null) { var autoClose = !(_provider.IsConnectionOpen()); if (autoClose) { _provider.Open(); } var dbParams = GetQueryDataParameters(queryParams); var reader = _provider.QueryGetReader(sql, dbParams); var records = DbModelBindingHelper.GetDataRecords(reader); var result = FormatDataRecordToDynamicModel(records); reader.Close(); if (autoClose) { _provider.Close(); } return(result); }
private TModel GetDataModelByPosition(QueryItemPosition position, long dataIndex = 0) { var queryStrategy = _queryStrategyProvider.GetSelectStrategy( _queryTable.ModelStrategy, _skipCount, _takeCount, _whereClauses, _orderbyClauses); var sql = queryStrategy.GetDbQueryScript(); var dbparams = queryStrategy.GetDbParameters().ToList(); IDataReader reader = null; TModel model = null; var singleError = false; _serviceProvider.Open(); try { reader = _serviceProvider.QueryGetReader(sql, dbparams); var dataAvailable = false; IDataRecord record = null; var idx = 0; switch (position) { case QueryItemPosition.Single: while (reader.Read()) { if (dataAvailable) { break; } if (idx > 1) { singleError = true; break; } idx++; dataAvailable = true; model = DbModelBindingHelper.BindingRecordToModel <TModel>( reader, _queryTable.ModelStrategy.PropertyStrategies); } break; case QueryItemPosition.ElementAt: idx = -1; while (reader.Read()) { idx++; if (idx == dataIndex) { record = reader; model = DbModelBindingHelper.BindingRecordToModel <TModel>( record, _queryTable.ModelStrategy.PropertyStrategies); break; } } break; case QueryItemPosition.First: reader.Read(); record = reader; model = DbModelBindingHelper.BindingRecordToModel <TModel>( record, _queryTable.ModelStrategy.PropertyStrategies); break; case QueryItemPosition.Last: while (reader.Read()) { record = reader; model = DbModelBindingHelper.BindingRecordToModel <TModel>( record, _queryTable.ModelStrategy.PropertyStrategies); } break; default: throw new NotSupportedException(); } } catch (Exception exception) { throw new DbOperationException("ERROR_SQL_EXECUTION_FAILED", exception, sql, dbparams); } finally { reader?.Close(); _serviceProvider.Close(); } if (singleError) { throw new InvalidOperationException("ERROR_OUT_OF_SINGLE_RECORD"); } return(model); }