public void RowsCountTransforming()
 {
     DetachedQuery dq = new DetachedQuery("from Foo f where f.Name like :p1");
     dq.SetString("p1", "%1_");
     IRowsCounter rc = QueryRowsCounter.Transforming(dq);
     using (ISession s = OpenSession())
     {
         Assert.AreEqual(5, rc.GetRowsCount(s));
     }
 }
Ejemplo n.º 2
0
        public void WithSelfCounter()
        {
            DetachedQuery dq = new DetachedQuery("from Foo f where f.Name like :p1");
            dq.SetString("p1", "%1_");
            IPaginable<Foo> fp = new PaginableRowsCounterQuery<Foo>(LastOpenedSession, dq);

            Paginator<Foo> ptor = new Paginator<Foo>(2, fp);
            Assert.IsTrue(ptor.AutoCalcPages);
            Assert.AreEqual(3, ptor.LastPageNumber);
            // check page 2
            IList<Foo> lpage = ptor.GetPage(2);
            Assert.AreEqual(2, lpage.Count);
            Assert.AreEqual(2, ptor.CurrentPageNumber);
            Assert.AreEqual("N12", lpage[0].Name);
            Assert.AreEqual("N13", lpage[1].Name);
        }
 public void PaginableRowsCount()
 {
     DetachedQuery dq = new DetachedQuery("from Foo f where f.Name like :p1");
     dq.SetString("p1", "N_");
     using (ISession s = OpenSession())
     {
         IPaginable<Foo> fp = new PaginableRowsCounterQuery<Foo>(LastOpenedSession, dq);
         IList<Foo> l = fp.GetPage(5, 1);
         Assert.AreEqual(5, l.Count);
         Assert.AreEqual(10, ((IRowsCounter) fp).GetRowsCount(s));
     }
 }
		public void PerformanceDiffSimplyQuery()
		{

			DateTime sDQStart = DateTime.Now;
			DetachedQuery dq = new DetachedQuery("from Foo f where f.Name=:pn and f.Description=:pd");
			dq.SetString("pn", "N2").SetString("pd", "D2");
			using (ISession s = OpenSession())
			{
				IQuery q = dq.GetExecutableQuery(s);
			}
			DateTime sDQStop = DateTime.Now;

			DateTime sQStart = DateTime.Now;
			using (ISession s = OpenSession())
			{
				IQuery q = s.CreateQuery("from Foo f where f.Name=:pn and f.Description=:pd").SetString("pn", "N2").SetString("pd", "D2");
			}
			DateTime sQStop = DateTime.Now;


			Console.WriteLine("DetachedQueryCycle={0} QueryCycl={1}  Diff={2}", sDQStop - sDQStart, sQStop - sQStart,
												(sDQStop - sDQStart) - (sQStop - sQStart));
		}
		public void Serializable()
		{
			DetachedQuery dq = new DetachedQuery("from Foo f where f.Name=:pn and f.Description=:pd");
			dq.SetString("pn", "N2").SetString("pd", "D2");
			byte[] bytes = SerializationHelper.Serialize(dq);

			DetachedQuery dqs = (DetachedQuery)SerializationHelper.Deserialize(bytes);

			using (ISession s = OpenSession())
			{
				dqs.GetExecutableQuery(s).List();
			}
		}
		public void ExecutableQuery()
		{
			// Simply fetch
			IDetachedQuery dq = new DetachedQuery("from Foo");
			using (ISession s = OpenSession())
			{
				IQuery q = dq.GetExecutableQuery(s);
				IList l = q.List();
				Assert.AreEqual(totalFoo, l.Count);
			}

			// With Typed Parameters
			dq = new DetachedQuery("from Foo f where f.Name=:pn and f.Description=:pd");
			dq.SetString("pn", "N2").SetString("pd", "D2");
			using (ISession s = OpenSession())
			{
				IQuery q = dq.GetExecutableQuery(s);
				IList<Foo> l = q.List<Foo>();
				Assert.AreEqual(1, l.Count);
				Assert.AreEqual("N2", l[0].Name);
				Assert.AreEqual("D2", l[0].Description);
			}

			// With UnTyped Parameters
			dq = new DetachedQuery("from Foo f where f.Name=:pn and f.Description=:pd");
			dq.SetParameter("pn", "N2").SetParameter("pd", "D2");
			using (ISession s = OpenSession())
			{
				IQuery q = dq.GetExecutableQuery(s);
				IList<Foo> l = q.List<Foo>();
				Assert.AreEqual(1, l.Count);
				Assert.AreEqual("N2", l[0].Name);
				Assert.AreEqual("D2", l[0].Description);
			}

			// With UnTyped Parameter List
			dq = new DetachedQuery("from Foo f where f.IntValue in (:pn)");
			dq.SetParameterList("pn", new int[] { 2, 3 });
			using (ISession s = OpenSession())
			{
				IQuery q = dq.GetExecutableQuery(s);
				IList<Foo> l = q.List<Foo>();
				Assert.That(l.Count, Is.EqualTo(2));

				Assert.That(l.Contains(new Foo("N2", "D2")), Is.True);
				Assert.That(l.Contains(new Foo("N3", "D3")), Is.True);
			}

			// Pagination
			dq = new DetachedQuery("from Foo f order by f.IntValue");
			dq.SetFirstResult(0).SetMaxResults(2);
			using (ISession s = OpenSession())
			{
				IQuery q = dq.GetExecutableQuery(s);
				IList<Foo> l = q.List<Foo>();
				Assert.AreEqual(2, l.Count);
				Assert.AreEqual("N0", l[0].Name);
				Assert.AreEqual("N1", l[1].Name);
			}
			dq.SetFirstResult(2).SetMaxResults(1);
			using (ISession s = OpenSession())
			{
				IQuery q = dq.GetExecutableQuery(s);
				IList<Foo> l = q.List<Foo>();
				Assert.AreEqual(1, l.Count);
				Assert.AreEqual("N2", l[0].Name);
			}
		}