Ejemplo n.º 1
0
        public static IList CreateMultiQuery(List <string> queryString, out string msgException)
        {
            IList result = null;

            lock (lockObj)
            {
                ISession session = NHibernateHelper.GetCurrentSession();
                msgException = string.Empty;
                try
                {
                    IMultiQuery mq = session.CreateMultiQuery();
                    foreach (string query in queryString)
                    {
                        IQuery q = session.CreateQuery(query);
                        mq.Add(q);
                    }
                    result = mq.List();
                }
                catch (Exception ex)
                {
                    msgException = ex.StackTrace;
                    LogHelper.Log(ex);
                }
                return(result);
            }
        }
Ejemplo n.º 2
0
            /// <summary>
            /// Executes all queries in this batch, adding the (token, result-set) pair to the map.
            /// </summary>
            /// <param name="resultMap"></param>
            internal void Execute(IDictionary <object, object> resultMap)
            {
                var results = _multiQuery.List();

                for (var i = 0; i < results.Count; i++)
                {
                    resultMap.Add(_tokens[i], results[i]);
                }
            }
Ejemplo n.º 3
0
 public void NH_1085_WillGiveReasonableErrorIfBadParameterName()
 {
     using (ISession s = sessions.OpenSession())
     {
         IMultiQuery MultiQuery = s.CreateMultiQuery()
                                  .Add("from Item i where i.Id in (:ids)")
                                  .Add("from Item i where i.Id in (:ids2)");
         var e = Assert.Throws <QueryException>(() => MultiQuery.List());
         Assert.That(e.Message, Is.EqualTo("Not all named parameters have been set: ['ids'] [from Item i where i.Id in (:ids)]"));
     }
 }
 public void NH_1085_WillGiveReasonableErrorIfBadParameterName()
 {
     using (ISession s = sessions.OpenSession())
     {
         IMultiQuery multiQuery = s.CreateMultiQuery()
                                  .Add(s.CreateSQLQuery("select * from ITEM where Id in (:ids)").AddEntity(typeof(Item)))
                                  .Add(s.CreateSQLQuery("select * from ITEM where Id in (:ids2)").AddEntity(typeof(Item)));
         var e = Assert.Throws <QueryException>(() => multiQuery.List());
         Assert.That(e.Message, Is.EqualTo("Not all named parameters have been set: ['ids'] [select * from ITEM where Id in (:ids)]"));
     }
 }
Ejemplo n.º 5
0
 public void DoesntThrowExceptionWhenHqlQueryIsGiven()
 {
     using (ISession session = OpenSession())
         using (ITransaction tx = session.BeginTransaction())
         {
             IQuery      sqlQuery = session.CreateQuery("from Document");
             IMultiQuery q        = session
                                    .CreateMultiQuery()
                                    .Add(sqlQuery);
             q.List();
         }
 }
Ejemplo n.º 6
0
 public void NH_1085_WillIgnoreParametersIfDoesNotAppearInQuery()
 {
     using (ISession s = sessions.OpenSession())
     {
         IMultiQuery MultiQuery = s.CreateMultiQuery()
                                  .Add("from Item i where i.Id in (:ids)")
                                  .Add("from Item i where i.Id in (:ids2)")
                                  .SetParameterList("ids", new int[] { 50 })
                                  .SetParameterList("ids2", new int[] { 50 });
         MultiQuery.List();
     }
 }
 public void NH_1085_WillIgnoreParametersIfDoesNotAppearInQuery()
 {
     using (ISession s = sessions.OpenSession())
     {
         IMultiQuery multiQuery = s.CreateMultiQuery()
                                  .Add(s.CreateSQLQuery("select * from ITEM where Id in (:ids)").AddEntity(typeof(Item)))
                                  .Add(s.CreateSQLQuery("select * from ITEM where Id in (:ids2)").AddEntity(typeof(Item)))
                                  .SetParameterList("ids", new[] { 50 })
                                  .SetParameterList("ids2", new[] { 50 });
         multiQuery.List();
     }
 }
Ejemplo n.º 8
0
        public void AliasToBeanTransformerShouldApplyCorrectlyToMultiQuery()
        {
            using (var s = OpenSession())
                using (var t = s.BeginTransaction())
                {
                    IMultiQuery multiQuery = s.CreateMultiQuery()
                                             .Add(s.CreateQuery("select entity.Id as EntityId from Entity entity")
                                                  .SetResultTransformer(Transformers.AliasToBean(typeof(EntityDTO)))
                                                  );

                    IList results = null;
                    Assert.That(() => results = multiQuery.List(), Throws.Nothing);
                    var elementOfFirstResult = ((IList)results[0])[0];
                    Assert.That(elementOfFirstResult, Is.TypeOf <EntityDTO>().And.Property("EntityId").EqualTo(1));
                }
        }
Ejemplo n.º 9
0
        public void AliasToBeanTransformerShouldApplyCorrectlyToMultiQuery()
        {
            using (var s = OpenSession())
                using (var t = s.BeginTransaction())
                {
                    IMultiQuery multiQuery = s.CreateMultiQuery()
                                             .Add(s.CreateQuery("select entity.Id as EntityId from Entity entity")
                                                  .SetResultTransformer(Transformers.AliasToBean(typeof(EntityDTO)))
                                                  );

                    IList results = null;
                    Executing.This(() => results = multiQuery.List()).Should().NotThrow();
                    var elementOfFirstResult = ((IList)results[0])[0];
                    elementOfFirstResult.Should().Be.OfType <EntityDTO>().And.ValueOf.EntityId.Should().Be(1);
                }
        }
Ejemplo n.º 10
0
 private void DoMutiQueryAndAssert()
 {
     using (ISession s = OpenSession())
     {
         IMultiQuery MultiQuery = s.CreateMultiQuery()
                                  .Add(s.CreateQuery("from Item i where i.Id > ?")
                                       .SetInt32(0, 50)
                                       .SetFirstResult(10))
                                  .Add(s.CreateQuery("select count(*) from Item i where i.Id > ?")
                                       .SetInt32(0, 50));
         MultiQuery.SetCacheable(true);
         IList results = MultiQuery.List();
         IList items   = (IList)results[0];
         Assert.AreEqual(89, items.Count);
         long count = (long)((IList)results[1])[0];
         Assert.AreEqual(99L, count);
     }
 }
        public void TwoMultiQueriesWithDifferentPagingGetDifferentResultsWhenUsingCachedQueries()
        {
            CreateItems();
            using (ISession s = OpenSession())
            {
                IMultiQuery multiQuery = s.CreateMultiQuery()
                                         .Add(s.CreateQuery("from Item i where i.Id > ?")
                                              .SetInt32(0, 50)
                                              .SetFirstResult(10))
                                         .Add(s.CreateSQLQuery("select count(*) as count from ITEM where Id > ?").AddScalar("count", NHibernateUtil.Int64)
                                              .SetInt32(0, 50));

                multiQuery.SetCacheable(true);
                IList results = multiQuery.List();
                IList items   = (IList)results[0];
                Assert.AreEqual(89, items.Count);
                long count = (long)((IList)results[1])[0];
                Assert.AreEqual(99L, count);
            }

            using (ISession s = OpenSession())
            {
                IMultiQuery multiQuery = s.CreateMultiQuery()
                                         .Add(s.CreateSQLQuery("select * from ITEM where Id > ?").AddEntity(typeof(Item))
                                              .SetInt32(0, 50)
                                              .SetFirstResult(20))
                                         .Add(s.CreateQuery("select count(*) from Item i where i.Id > ?")
                                              .SetInt32(0, 50));
                multiQuery.SetCacheable(true);
                IList results = multiQuery.List();
                IList items   = (IList)results[0];
                Assert.AreEqual(79, items.Count,
                                "Should have gotten different result here, because the paging is different");
                long count = (long)((IList)results[1])[0];
                Assert.AreEqual(99L, count);
            }

            RemoveAllItems();
        }
        /// <summary>
        /// Executes the specified query and return the results
        /// </summary>
        /// <param name="session">The session to execute the query in.</param>
        /// <returns>an array of results, one for each query added</returns>
        public object Execute(ISession session)
        {
            // create a multi-query
            IMultiQuery multiQuery = session.CreateMultiQuery();

            foreach (ActiveRecordBaseQuery arQuery in _queryList)
            {
                // rule: if a query implements InternalExecute, it will fail
                // (we are depending on the ability to call CreateQuery()
                //  and get a self-contained query object)
                if ((arQuery is CountQuery) || (arQuery is ActiveRecordCriteriaQuery))
                {
                    throw new ActiveRecordException("Criteria Based queries are not supported!");
                }
                // add the executable IQuery to our multi-query
                arQuery.AddQuery(session, multiQuery);
            }
            // execute multiquery
            object resultSetArray = multiQuery.List();

            return(resultSetArray);
        }
Ejemplo n.º 13
0
        public void CanGetMultiQueryFromSecondLevelCache()
        {
            CreateItems();
            //set the query in the cache
            DoMutiQueryAndAssert();

            Hashtable cacheHashtable  = GetHashTableUsedAsQueryCache();
            IList     cachedListEntry = (IList) new ArrayList(cacheHashtable.Values)[0];
            IList     cachedQuery     = (IList)cachedListEntry[1];

            IList firstQueryResults = (IList)cachedQuery[0];

            firstQueryResults.Clear();
            firstQueryResults.Add(3);
            firstQueryResults.Add(4);

            IList secondQueryResults = (IList)cachedQuery[1];

            secondQueryResults[0] = 2L;

            using (ISession s = sessions.OpenSession())
            {
                IMultiQuery MultiQuery = s.CreateMultiQuery()
                                         .Add(s.CreateQuery("from Item i where i.Id > ?")
                                              .SetInt32(0, 50)
                                              .SetFirstResult(10))
                                         .Add(s.CreateQuery("select count(*) from Item i where i.Id > ?")
                                              .SetInt32(0, 50));
                MultiQuery.SetCacheable(true);
                IList results = MultiQuery.List();
                IList items   = (IList)results[0];
                Assert.AreEqual(2, items.Count);
                long count = (long)((IList)results[1])[0];
                Assert.AreEqual(2L, count);
            }

            RemoveAllItems();
        }