Beispiel #1
0
        private void GetResultsFromDatabase(IList results)
        {
            bool statsEnabled = session.Factory.Statistics.IsStatisticsEnabled;
            var  stopWatch    = new Stopwatch();

            if (statsEnabled)
            {
                stopWatch.Start();
            }
            int rowCount = 0;

            using (
                IDbCommand command =
                    session.Batcher.PrepareCommand(CommandType.Text, sqlString, types.ToArray()))
            {
                BindParameters(command);
                ArrayList[]          hydratedObjects     = new ArrayList[loaders.Count];
                List <EntityKey[]>[] subselectResultKeys = new List <EntityKey[]> [loaders.Count];
                bool[]      createSubselects             = new bool[loaders.Count];
                IDataReader reader = session.Batcher.ExecuteReader(command);
                try
                {
                    for (int i = 0; i < loaders.Count; i++)
                    {
                        CriteriaLoader loader     = loaders[i];
                        int            entitySpan = loader.EntityPersisters.Length;
                        hydratedObjects[i] = entitySpan == 0 ? null : new ArrayList(entitySpan);
                        EntityKey[]     keys            = new EntityKey[entitySpan];
                        QueryParameters queryParameters = parameters[i];
                        IList           tmpResults;
                        if (resultCollectionGenericType[i] == typeof(object))
                        {
                            tmpResults = new ArrayList();
                        }
                        else
                        {
                            tmpResults = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(resultCollectionGenericType[i]));
                        }

                        RowSelection selection = parameters[i].RowSelection;
                        createSubselects[i]    = loader.IsSubselectLoadingEnabled;
                        subselectResultKeys[i] = createSubselects[i] ? new List <EntityKey[]>() : null;
                        int maxRows = Loader.Loader.HasMaxRows(selection) ? selection.MaxRows : int.MaxValue;
                        if (!dialect.SupportsLimitOffset || !NHibernate.Loader.Loader.UseLimit(selection, dialect))
                        {
                            Loader.Loader.Advance(reader, selection);
                        }
                        int count;
                        for (count = 0; count < maxRows && reader.Read(); count++)
                        {
                            rowCount++;

                            object o =
                                loader.GetRowFromResultSet(reader, session, queryParameters, loader.GetLockModes(queryParameters.LockModes),
                                                           null, hydratedObjects[i], keys, false);
                            if (createSubselects[i])
                            {
                                subselectResultKeys[i].Add(keys);
                                keys = new EntityKey[entitySpan];                                 //can't reuse in this case
                            }
                            tmpResults.Add(o);
                        }
                        results.Add(tmpResults);
                        reader.NextResult();
                    }
                }
                catch (Exception e)
                {
                    log.Error("Error executing multi criteria : [" + command.CommandText + "]");
                    throw new HibernateException("Error executing multi criteria : [" + command.CommandText + "]", e);
                }
                finally
                {
                    session.Batcher.CloseCommand(command, reader);
                }
                for (int i = 0; i < loaders.Count; i++)
                {
                    CriteriaLoader loader = loaders[i];
                    loader.InitializeEntitiesAndCollections(hydratedObjects[i], reader, session, false);

                    if (createSubselects[i])
                    {
                        loader.CreateSubselects(subselectResultKeys[i], parameters[i], session);
                    }
                }
            }
            if (statsEnabled)
            {
                stopWatch.Stop();
                session.Factory.StatisticsImplementor.QueryExecuted(string.Format("{0} queries (MultiCriteria)", loaders.Count), rowCount, stopWatch.Elapsed);
            }
        }
Beispiel #2
0
        private void GetResultsFromDatabase(IList results)
        {
            Stopwatch stopWatch = null;

            if (session.Factory.Statistics.IsStatisticsEnabled)
            {
                stopWatch = Stopwatch.StartNew();
            }
            int rowCount     = 0;
            var cacheBatcher = new CacheBatcher(session);

            try
            {
                using (var reader = resultSetsCommand.GetReader(_timeout))
                {
                    var hydratedObjects = new List <object> [loaders.Count];
                    List <EntityKey[]>[] subselectResultKeys = new List <EntityKey[]> [loaders.Count];
                    bool[] createSubselects = new bool[loaders.Count];
                    for (int i = 0; i < loaders.Count; i++)
                    {
                        CriteriaLoader loader     = loaders[i];
                        int            entitySpan = loader.EntityPersisters.Length;
                        hydratedObjects[i] = entitySpan == 0 ? null : new List <object>(entitySpan);
                        EntityKey[]     keys            = new EntityKey[entitySpan];
                        QueryParameters queryParameters = parameters[i];
                        IList           tmpResults      = new List <object>();

                        RowSelection selection = parameters[i].RowSelection;
                        createSubselects[i]    = loader.IsSubselectLoadingEnabled;
                        subselectResultKeys[i] = createSubselects[i] ? new List <EntityKey[]>() : null;
                        int maxRows = Loader.Loader.HasMaxRows(selection) ? selection.MaxRows : int.MaxValue;
                        if (!dialect.SupportsLimitOffset || !loader.UseLimit(selection, dialect))
                        {
                            Loader.Loader.Advance(reader, selection);
                        }
                        int count;
                        for (count = 0; count < maxRows && reader.Read(); count++)
                        {
                            rowCount++;

                            object o =
                                loader.GetRowFromResultSet(reader, session, queryParameters, loader.GetLockModes(queryParameters.LockModes),
                                                           null, hydratedObjects[i], keys, true, null, null,
                                                           (persister, data) => cacheBatcher.AddToBatch(persister, data));
                            if (createSubselects[i])
                            {
                                subselectResultKeys[i].Add(keys);
                                keys = new EntityKey[entitySpan];                                 //can't reuse in this case
                            }
                            tmpResults.Add(o);
                        }

                        results.Add(tmpResults);
                        reader.NextResult();
                    }

                    for (int i = 0; i < loaders.Count; i++)
                    {
                        CriteriaLoader loader = loaders[i];
                        loader.InitializeEntitiesAndCollections(hydratedObjects[i], reader, session, session.DefaultReadOnly, cacheBatcher);

                        if (createSubselects[i])
                        {
                            loader.CreateSubselects(subselectResultKeys[i], parameters[i], session);
                        }
                    }

                    cacheBatcher.ExecuteBatch();
                }
            }
            catch (Exception sqle)
            {
                log.Error(sqle, "Failed to execute multi criteria: [{0}]", resultSetsCommand.Sql);
                throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle, "Failed to execute multi criteria", resultSetsCommand.Sql);
            }
            if (stopWatch != null)
            {
                stopWatch.Stop();
                session.Factory.StatisticsImplementor.QueryExecuted(string.Format("{0} queries (MultiCriteria)", loaders.Count), rowCount, stopWatch.Elapsed);
            }
        }
        private async Task GetResultsFromDatabaseAsync(IList results, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            bool statsEnabled = session.Factory.Statistics.IsStatisticsEnabled;
            var  stopWatch    = new Stopwatch();

            if (statsEnabled)
            {
                stopWatch.Start();
            }
            int rowCount = 0;

            try
            {
                using (var reader = await(resultSetsCommand.GetReaderAsync(null, cancellationToken)).ConfigureAwait(false))
                {
                    var hydratedObjects = new List <object> [loaders.Count];
                    List <EntityKey[]>[] subselectResultKeys = new List <EntityKey[]> [loaders.Count];
                    bool[] createSubselects = new bool[loaders.Count];
                    for (int i = 0; i < loaders.Count; i++)
                    {
                        CriteriaLoader loader     = loaders[i];
                        int            entitySpan = loader.EntityPersisters.Length;
                        hydratedObjects[i] = entitySpan == 0 ? null : new List <object>(entitySpan);
                        EntityKey[]     keys            = new EntityKey[entitySpan];
                        QueryParameters queryParameters = parameters[i];
                        IList           tmpResults      = new List <object>();

                        RowSelection selection = parameters[i].RowSelection;
                        createSubselects[i]    = loader.IsSubselectLoadingEnabled;
                        subselectResultKeys[i] = createSubselects[i] ? new List <EntityKey[]>() : null;
                        int maxRows = Loader.Loader.HasMaxRows(selection) ? selection.MaxRows : int.MaxValue;
                        if (!dialect.SupportsLimitOffset || !loader.UseLimit(selection, dialect))
                        {
                            await(Loader.Loader.AdvanceAsync(reader, selection, cancellationToken)).ConfigureAwait(false);
                        }
                        int count;
                        for (count = 0; count < maxRows && await(reader.ReadAsync(cancellationToken)).ConfigureAwait(false); count++)
                        {
                            rowCount++;

                            object o =
                                await(loader.GetRowFromResultSetAsync(reader, session, queryParameters, loader.GetLockModes(queryParameters.LockModes),
                                                                      null, hydratedObjects[i], keys, true, cancellationToken)).ConfigureAwait(false);
                            if (createSubselects[i])
                            {
                                subselectResultKeys[i].Add(keys);
                                keys = new EntityKey[entitySpan];                                 //can't reuse in this case
                            }
                            tmpResults.Add(o);
                        }

                        results.Add(tmpResults);
                        await(reader.NextResultAsync(cancellationToken)).ConfigureAwait(false);
                    }

                    for (int i = 0; i < loaders.Count; i++)
                    {
                        CriteriaLoader loader = loaders[i];
                        await(loader.InitializeEntitiesAndCollectionsAsync(hydratedObjects[i], reader, session, session.DefaultReadOnly, cancellationToken)).ConfigureAwait(false);

                        if (createSubselects[i])
                        {
                            loader.CreateSubselects(subselectResultKeys[i], parameters[i], session);
                        }
                    }
                }
            }
            catch (Exception sqle)
            {
                var message = string.Format("Failed to execute multi criteria: [{0}]", resultSetsCommand.Sql);
                log.Error(message, sqle);
                throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle, "Failed to execute multi criteria", resultSetsCommand.Sql);
            }
            if (statsEnabled)
            {
                stopWatch.Stop();
                session.Factory.StatisticsImplementor.QueryExecuted(string.Format("{0} queries (MultiCriteria)", loaders.Count), rowCount, stopWatch.Elapsed);
            }
        }