public List <T> GetNextResultSet <T>()
        {
            if (!InitConnection())
            {
                if (!sqlDataReader.NextResult())
                {
                    throw new InvalidOperationException("There is no result set left");
                }
            }

            var fillVisitor = new FillVisitor(
                reader: sqlDataReader,
                db: db,
                objectFillerFactory: new ObjectFillerFactory());
            var resultSet = new List <T>();

            var configuration = GlobalDbConfiguration.GetConfigurationOrEmpty(typeof(T));
            var entityFilter  = configuration.EntityFilter;
            var queryLogger   = GlobalDbConfiguration.QueryLogger;

            while (sqlDataReader.Read())
            {
                var entity = fillVisitor.Fill <T>();

                queryLogger.IncrementLoadedElementCount(increment: 1);

                if (entityFilter.DoReturnEntity(db.Settings, entity))
                {
                    EmitValueLoaded(entity);
                    resultSet.Add(entity);
                }
            }

            return(resultSet);
        }
 protected override void ModifyInternalState(FillVisitor visitor)
 {
     SendIdChanging();
     _StockGroupID.Load(visitor.GetInt32());
     SendIdChanged();
     _StockGroupName.Load(visitor.GetValue <System.String>());
     _LastEditedBy.Load(visitor.GetInt32());
     _ValidFrom.Load(visitor.GetDateTime());
     _ValidTo.Load(visitor.GetDateTime());
     this._db = visitor.Db;
     isLoaded = true;
 }
 protected override void ModifyInternalState(FillVisitor visitor)
 {
     SendIdChanging();
     _StockItemStockGroupID.Load(visitor.GetInt32());
     SendIdChanged();
     _StockItemID.Load(visitor.GetInt32());
     _StockGroupID.Load(visitor.GetInt32());
     _LastEditedBy.Load(visitor.GetInt32());
     _LastEditedWhen.Load(visitor.GetDateTime());
     this._db = visitor.Db;
     isLoaded = true;
 }
示例#4
0
        public static object Fill(Type type, object entity, FillVisitor fillVisitor)
        {
            var helper = GetHelperOrNull(type);

            if (helper != null)
            {
                return(helper.Fill(entity, fillVisitor));
            }

            entity = fillVisitor.Fill(type);

            return(entity);
        }
示例#5
0
 protected override void ModifyInternalState(FillVisitor visitor)
 {
     SendIdChanging();
     _ColdRoomTemperatureID.Load(visitor.GetValue <System.Int64>());
     SendIdChanged();
     _ColdRoomSensorNumber.Load(visitor.GetInt32());
     _RecordedWhen.Load(visitor.GetDateTime());
     _Temperature.Load(visitor.GetDecimal());
     _ValidFrom.Load(visitor.GetDateTime());
     _ValidTo.Load(visitor.GetDateTime());
     this._db = visitor.Db;
     isLoaded = true;
 }
 protected override void ModifyInternalState(FillVisitor visitor)
 {
     SendIdChanging();
     _CityID.Load(visitor.GetInt32());
     SendIdChanged();
     _CityName.Load(visitor.GetValue <System.String>());
     _StateProvinceID.Load(visitor.GetInt32());
     _LatestRecordedPopulation.Load(visitor.GetValue <System.Int64>());
     _LastEditedBy.Load(visitor.GetInt32());
     _ValidFrom.Load(visitor.GetDateTime());
     _ValidTo.Load(visitor.GetDateTime());
     this._db = visitor.Db;
     isLoaded = true;
 }
示例#7
0
        private IReadOnlyList <object> Load(Type type, string sql, params object[] values)
        {
            List <object> entities = new List <object>();

            using (var sqlConnection = GetConnection())
            {
                using (var sqlCommand = new DbSqlCommand(sql))
                {
                    sqlCommand.Connection = sqlConnection.SqlConnection;

                    QueryHelpers.AddSqlParameters(sqlCommand, values);
                    sqlConnection.Open();

                    using (SqlDataReader reader = sqlCommand.ExecuteReader(CommandBehavior.SequentialAccess))
                    {
                        var fillVisitor = new FillVisitor(
                            reader: reader,
                            db: this,
                            objectFillerFactory: new ObjectFillerFactory());
                        while (fillVisitor.Read())
                        {
                            object entity = QueryHelpers.Fill(type, entity: null, fillVisitor: fillVisitor);
                            entities.Add(entity);
                        }
                    }
                }
            }

            var entitiesToReturn = new List <object>(entities.Count);

            var configuration = GlobalDbConfiguration.GetConfigurationOrEmpty(type);
            var entityFilter  = configuration.EntityFilter;
            var queryLogger   = GlobalDbConfiguration.QueryLogger;

            foreach (object entity in entities)
            {
                queryLogger.IncrementLoadedElementCount(increment: 1);
                if (entityFilter.DoReturnEntity(Settings, entity))
                {
                    entitiesToReturn.Add(entity);
                }
            }

            OnEntitiesLoaded(entitiesToReturn
                             .Where(e => e is DbEntity)
                             .Select(e => e as DbEntity)
                             .ToList());

            return(entitiesToReturn);
        }
示例#8
0
 protected override void ModifyInternalState(FillVisitor visitor)
 {
     SendIdChanging();
     _VehicleTemperatureID.Load(visitor.GetValue <System.Int64>());
     SendIdChanged();
     _VehicleRegistration.Load(visitor.GetValue <System.String>());
     _ChillerSensorNumber.Load(visitor.GetInt32());
     _RecordedWhen.Load(visitor.GetDateTime());
     _Temperature.Load(visitor.GetDecimal());
     _FullSensorData.Load(visitor.GetValue <System.String>());
     _IsCompressed.Load(visitor.GetBoolean());
     _CompressedSensorData.Load(visitor.GetBinary());
     this._db = visitor.Db;
     isLoaded = true;
 }
示例#9
0
 protected override void ModifyInternalState(FillVisitor visitor)
 {
     SendIdChanging();
     _StockItemID.Load(visitor.GetInt32());
     SendIdChanged();
     _QuantityOnHand.Load(visitor.GetInt32());
     _BinLocation.Load(visitor.GetValue <System.String>());
     _LastStocktakeQuantity.Load(visitor.GetInt32());
     _LastCostPrice.Load(visitor.GetDecimal());
     _ReorderLevel.Load(visitor.GetInt32());
     _TargetStockLevel.Load(visitor.GetInt32());
     _LastEditedBy.Load(visitor.GetInt32());
     _LastEditedWhen.Load(visitor.GetDateTime());
     this._db = visitor.Db;
     isLoaded = true;
 }
示例#10
0
 protected override void ModifyInternalState(FillVisitor visitor)
 {
     SendIdChanging();
     _StockItemTransactionID.Load(visitor.GetInt32());
     SendIdChanged();
     _StockItemID.Load(visitor.GetInt32());
     _TransactionTypeID.Load(visitor.GetInt32());
     _CustomerID.Load(visitor.GetInt32());
     _InvoiceID.Load(visitor.GetInt32());
     _SupplierID.Load(visitor.GetInt32());
     _PurchaseOrderID.Load(visitor.GetInt32());
     _TransactionOccurredWhen.Load(visitor.GetDateTime());
     _Quantity.Load(visitor.GetDecimal());
     _LastEditedBy.Load(visitor.GetInt32());
     _LastEditedWhen.Load(visitor.GetDateTime());
     this._db = visitor.Db;
     isLoaded = true;
 }
示例#11
0
        public TElement Fill(TElement entity, FillVisitor fillVisitor)
        {
            if (fillVisitor.IsDBNull())
            {
                fillVisitor.Skip(ColumnsInSelectStatement.Length);
            }
            else
            {
                if (entity == null)
                {
                    entity = new TElement();
                }

                ((IDbEntityInternal)entity).ModifyInternalState(fillVisitor);
            }

            return(entity);
        }
示例#12
0
 protected override void ModifyInternalState(FillVisitor visitor)
 {
     SendIdChanging();
     _SystemParameterID.Load(visitor.GetInt32());
     SendIdChanged();
     _DeliveryAddressLine1.Load(visitor.GetValue <System.String>());
     _DeliveryAddressLine2.Load(visitor.GetValue <System.String>());
     _DeliveryCityID.Load(visitor.GetInt32());
     _DeliveryPostalCode.Load(visitor.GetValue <System.String>());
     _PostalAddressLine1.Load(visitor.GetValue <System.String>());
     _PostalAddressLine2.Load(visitor.GetValue <System.String>());
     _PostalCityID.Load(visitor.GetInt32());
     _PostalPostalCode.Load(visitor.GetValue <System.String>());
     _ApplicationSettings.Load(visitor.GetValue <System.String>());
     _LastEditedBy.Load(visitor.GetInt32());
     _LastEditedWhen.Load(visitor.GetDateTime());
     this._db = visitor.Db;
     isLoaded = true;
 }
示例#13
0
 protected override void ModifyInternalState(FillVisitor visitor)
 {
     SendIdChanging();
     _PurchaseOrderLineID.Load(visitor.GetInt32());
     SendIdChanged();
     _PurchaseOrderID.Load(visitor.GetInt32());
     _StockItemID.Load(visitor.GetInt32());
     _OrderedOuters.Load(visitor.GetInt32());
     _Description.Load(visitor.GetValue <System.String>());
     _ReceivedOuters.Load(visitor.GetInt32());
     _PackageTypeID.Load(visitor.GetInt32());
     _ExpectedUnitPricePerOuter.Load(visitor.GetDecimal());
     _LastReceiptDate.Load(visitor.GetDateTime());
     _IsOrderLineFinalized.Load(visitor.GetBoolean());
     _LastEditedBy.Load(visitor.GetInt32());
     _LastEditedWhen.Load(visitor.GetDateTime());
     this._db = visitor.Db;
     isLoaded = true;
 }
示例#14
0
 protected override void ModifyInternalState(FillVisitor visitor)
 {
     SendIdChanging();
     _PurchaseOrderID.Load(visitor.GetInt32());
     SendIdChanged();
     _SupplierID.Load(visitor.GetInt32());
     _OrderDate.Load(visitor.GetDateTime());
     _DeliveryMethodID.Load(visitor.GetInt32());
     _ContactPersonID.Load(visitor.GetInt32());
     _ExpectedDeliveryDate.Load(visitor.GetDateTime());
     _SupplierReference.Load(visitor.GetValue <System.String>());
     _IsOrderFinalized.Load(visitor.GetBoolean());
     _Comments.Load(visitor.GetValue <System.String>());
     _InternalComments.Load(visitor.GetValue <System.String>());
     _LastEditedBy.Load(visitor.GetInt32());
     _LastEditedWhen.Load(visitor.GetDateTime());
     this._db = visitor.Db;
     isLoaded = true;
 }
 protected override void ModifyInternalState(FillVisitor visitor)
 {
     SendIdChanging();
     _OrderLineID.Load(visitor.GetInt32());
     SendIdChanged();
     _OrderID.Load(visitor.GetInt32());
     _StockItemID.Load(visitor.GetInt32());
     _Description.Load(visitor.GetValue <System.String>());
     _PackageTypeID.Load(visitor.GetInt32());
     _Quantity.Load(visitor.GetInt32());
     _UnitPrice.Load(visitor.GetDecimal());
     _TaxRate.Load(visitor.GetDecimal());
     _PickedQuantity.Load(visitor.GetInt32());
     _PickingCompletedWhen.Load(visitor.GetDateTime());
     _LastEditedBy.Load(visitor.GetInt32());
     _LastEditedWhen.Load(visitor.GetDateTime());
     this._db = visitor.Db;
     isLoaded = true;
 }
 protected override void ModifyInternalState(FillVisitor visitor)
 {
     SendIdChanging();
     _CountryID.Load(visitor.GetInt32());
     SendIdChanged();
     _CountryName.Load(visitor.GetValue <System.String>());
     _FormalName.Load(visitor.GetValue <System.String>());
     _IsoAlpha3Code.Load(visitor.GetValue <System.String>());
     _IsoNumericCode.Load(visitor.GetInt32());
     _CountryType.Load(visitor.GetValue <System.String>());
     _LatestRecordedPopulation.Load(visitor.GetValue <System.Int64>());
     _Continent.Load(visitor.GetValue <System.String>());
     _Region.Load(visitor.GetValue <System.String>());
     _Subregion.Load(visitor.GetValue <System.String>());
     _LastEditedBy.Load(visitor.GetInt32());
     _ValidFrom.Load(visitor.GetDateTime());
     _ValidTo.Load(visitor.GetDateTime());
     this._db = visitor.Db;
     isLoaded = true;
 }
 protected override void ModifyInternalState(FillVisitor visitor)
 {
     SendIdChanging();
     _SpecialDealID.Load(visitor.GetInt32());
     SendIdChanged();
     _StockItemID.Load(visitor.GetInt32());
     _CustomerID.Load(visitor.GetInt32());
     _BuyingGroupID.Load(visitor.GetInt32());
     _CustomerCategoryID.Load(visitor.GetInt32());
     _StockGroupID.Load(visitor.GetInt32());
     _DealDescription.Load(visitor.GetValue <System.String>());
     _StartDate.Load(visitor.GetDateTime());
     _EndDate.Load(visitor.GetDateTime());
     _DiscountAmount.Load(visitor.GetDecimal());
     _DiscountPercentage.Load(visitor.GetDecimal());
     _UnitPrice.Load(visitor.GetDecimal());
     _LastEditedBy.Load(visitor.GetInt32());
     _LastEditedWhen.Load(visitor.GetDateTime());
     this._db = visitor.Db;
     isLoaded = true;
 }
示例#18
0
        public static T Fill <T>(T entity, FillVisitor fillVisitor)
        {
            var helper = GetHelperOrNull <T>();

            if (helper != null)
            {
                return(helper.Fill(entity, fillVisitor));
            }

            entity = fillVisitor.Fill <T>();
            var dbEntity = entity as IDbEntityInternal;

            if (dbEntity != null)
            {
                dbEntity.ModifyInternalState(new InsertSetVisitor(doReset: true));

                dbEntity.SetDb(fillVisitor.DbInternal);
            }

            return(entity);
        }
示例#19
0
 protected override void ModifyInternalState(FillVisitor visitor)
 {
     SendIdChanging();
     _CustomerTransactionID.Load(visitor.GetInt32());
     SendIdChanged();
     _CustomerID.Load(visitor.GetInt32());
     _TransactionTypeID.Load(visitor.GetInt32());
     _InvoiceID.Load(visitor.GetInt32());
     _PaymentMethodID.Load(visitor.GetInt32());
     _TransactionDate.Load(visitor.GetDateTime());
     _AmountExcludingTax.Load(visitor.GetDecimal());
     _TaxAmount.Load(visitor.GetDecimal());
     _TransactionAmount.Load(visitor.GetDecimal());
     _OutstandingBalance.Load(visitor.GetDecimal());
     _FinalizationDate.Load(visitor.GetDateTime());
     _IsFinalized.Load(visitor.GetBoolean());
     _LastEditedBy.Load(visitor.GetInt32());
     _LastEditedWhen.Load(visitor.GetDateTime());
     this._db = visitor.Db;
     isLoaded = true;
 }
示例#20
0
 protected override void ModifyInternalState(FillVisitor visitor)
 {
     SendIdChanging();
     _OrderID.Load(visitor.GetInt32());
     SendIdChanged();
     _CustomerID.Load(visitor.GetInt32());
     _SalespersonPersonID.Load(visitor.GetInt32());
     _PickedByPersonID.Load(visitor.GetInt32());
     _ContactPersonID.Load(visitor.GetInt32());
     _BackorderOrderID.Load(visitor.GetInt32());
     _OrderDate.Load(visitor.GetDateTime());
     _ExpectedDeliveryDate.Load(visitor.GetDateTime());
     _CustomerPurchaseOrderNumber.Load(visitor.GetValue <System.String>());
     _IsUndersupplyBackordered.Load(visitor.GetBoolean());
     _Comments.Load(visitor.GetValue <System.String>());
     _DeliveryInstructions.Load(visitor.GetValue <System.String>());
     _InternalComments.Load(visitor.GetValue <System.String>());
     _PickingCompletedWhen.Load(visitor.GetDateTime());
     _LastEditedBy.Load(visitor.GetInt32());
     _LastEditedWhen.Load(visitor.GetDateTime());
     this._db = visitor.Db;
     isLoaded = true;
 }
示例#21
0
        private void EnsureElementsAreLoaded()
        {
            if (cachedElements != null)
            {
                return;
            }

            lock (cachedElementsLock)
            {
                if (cachedElements != null)
                {
                    return;
                }

                var cachedTypedElements = new List <TElement>();
                List <(TElement, List <object>)> loadedData = SqlCommands.ExecuteSqlCommand(() =>
                {
                    using (var sqlConnection = Db.GetConnection())
                    {
                        using (var sqlCommand = GetSqlCommand())
                        {
                            QueryHelpers.AddSqlParameters(sqlCommand, parameters);

                            // AppContext.StartRequestTiming(sql, "OpenConnection");
                            sqlConnection.Open();
                            // AppContext.StopRequestTiming(sql, "OpenConnection");

                            sqlCommand.Connection = sqlConnection.SqlConnection;
                            // sqlCommand.Transaction = sqlConnection.SqlConnection.BeginTransaction(Db.Settings().IsolationLevel);

                            if (sqlCommand.CommandType == CommandType.StoredProcedure)
                            {
                                SqlParameter returnValue = SqlParameterUtils.Create("@ReturnValue", TypeToDbType <TElement>());
                                returnValue.Direction    = ParameterDirection.ReturnValue;
                                sqlCommand.Parameters.Add(returnValue);
                                sqlCommand.ExecuteNonQuery();

                                TElement entity = (TElement)sqlCommand.Parameters["@ReturnValue"].Value;
                                cachedTypedElements.Add(entity);

                                sqlCommand.Parameters.Clear();
                                return(new List <(TElement, List <object>)>());
                            }
                            else
                            {
                                // AppContext.StartRequestTiming(sql, "ExecuteReader");

                                var loadedDataFromReader = new List <(TElement, List <object>)>();
                                using (SqlDataReader reader = sqlCommand.ExecuteReader(CommandBehavior.SequentialAccess))
                                {
                                    // AppContext.StopRequestTiming(sql, "ExecuteReader");
                                    // AppContext.StartRequestTiming(sql, "FillVisitor");

                                    var fillVisitor = new FillVisitor(
                                        reader: reader,
                                        db: Db,
                                        objectFillerFactory: new ObjectFillerFactory());
                                    // fillVisitor.SQL = sql;

                                    // AppContext.StopRequestTiming(sql, "FillVisitor");
                                    // AppContext.StartRequestTiming(sql, "Read");
                                    int k = 0;
                                    while (fillVisitor.Read())
                                    {
                                        // AppContext.StopRequestTiming(sql, "Read");
                                        // AppContext.StartRequestTiming(sql, "Fill", k > 0);

                                        TElement entity = QueryHelpers.Fill(default(TElement), fillVisitor);
                                        var dbEntity    = entity as DbEntity;
                                        if (dbEntity != null)
                                        {
                                            ((IDbEntityInternal)dbEntity).SetAllowSettingColumns(false);
                                        }

                                        if (entity is IId)
                                        {
                                            entity = (TElement)Db.LoadedEntityCache.GetOrAdd(typeof(TElement), "Id", ((IId)entity).Id, entity);
                                        }

                                        // AppContext.StopRequestTiming(sql, "Fill");

                                        // First fetch all entities from the db and then return them
                                        // Otherwise we have problems, if someone calls AnyDb() first (AnyDb() just requests the first entity)
                                        // Anyway we improve performance if someone calls the query two times: The second time, we don't query the
                                        // database but return the cached elements

                                        List <object> subEntities = new List <object>();
                                        if (fillVisitor.HasNext && !fillVisitor.IsDBNull())
                                        {
                                            subEntities.OfType <IDbEntityInternal>().ForEach(e => e.SetAllowSettingColumns(true));
                                            // AppContext.StopRequestTiming(sql, "subEntities");
                                        }

                                        k++;
                                        loadedDataFromReader.Add((entity, subEntities));
                                        // CachedElements.Add(entity);
                                    }

                                    // AppContext.StopRequestTiming(sql, "Read");
                                }

                                sqlCommand.Parameters.Clear();
                                return(loadedDataFromReader);
                            }
                        }
                    }
                });

                // Connect the items to one another (i.e. if Person.Customer was called, set Person.Customer to the recently loaded entity)
                // First emit EmitValueLoadedBeforeRightsCheck(dbEntity); for all entities before checking the rights, because if only do it within the while loop below just before Db.ReturnEntity for each dbEntity,
                // in GetOwnerMandatorId() of i.e. User (which is called from Db.ReturnEntity()), if we call Person.Customer.MandatorId, Customer is loaded from the DB, because Person.Customer was not set yet
                loadedData.Select(e => e.Item1).OfType <DbEntity>().ForEach(e => EmitValueLoadedBeforeRightsCheck(e));
                // Do this after the sql connection above has been closed
                // Otherwise we get this exception:
                // System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Transactions.TransactionAbortedException: The transaction has aborted. ---> System.Transactions.TransactionPromotionException: Failure while attempting to promote transaction. ---> System.Data.SqlClient.SqlException: There is already an open DataReader associated with this Command which must

                var entityFilter = GlobalDbConfiguration.GetConfigurationOrEmpty(typeof(TElement)).EntityFilter;
                for (int i = 0; i < loadedData.Count; i++)
                {
                    var entity         = loadedData[i].Item1;
                    var joinedEntities = loadedData[i].Item2;

                    GlobalDbConfiguration.QueryLogger.IncrementLoadedElementCount(increment: 1);
                    // Count the joined entities too
                    GlobalDbConfiguration.QueryLogger.IncrementLoadedElementCount(increment: joinedEntities?.Count ?? 0);

                    var dbEntity = entity as DbEntity;
                    if (dbEntity != null)
                    {
                        ((IDbEntityInternal)dbEntity).SetAllowSettingColumns(allowSettingColumns: true);
                    }

                    if (entityFilter.DoReturnEntity(Db.Settings, entity, joinedEntities))
                    {
                        EmitValueLoaded(dbEntity);
                        cachedTypedElements.Add(entity);
                    }
                    else
                    {
                        EmitValueRemoved(dbEntity);
                    }
                }

                cachedElements = cachedTypedElements.AsReadOnly();
            }
        }
示例#22
0
 protected override void ModifyInternalState(FillVisitor visitor)
 {
 }
示例#23
0
 public object Fill(object _obj, FillVisitor fillVisitor)
 {
     return(Fill(_obj as TElement, fillVisitor));
 }
示例#24
0
 void IDbEntityInternal.ModifyInternalState(FillVisitor visitor) => ModifyInternalState(visitor);
示例#25
0
 protected abstract void ModifyInternalState(FillVisitor visitor);
示例#26
0
 protected override void ModifyInternalState(FillVisitor visitor)
 {
     throw new NotImplementedException();
 }