Ejemplo n.º 1
0
		public void SubclassCollection()
		{
			ISession s = OpenSession();
			SubMulti sm = new SubMulti();
			SubMulti sm1 = new SubMulti();
			SubMulti sm2 = new SubMulti();
			sm.Children = new List<SubMulti> {sm1, sm2};
			sm.MoreChildren = new List<SubMulti> {sm1, sm2};
			sm.ExtraProp = "foo";
			sm1.Parent = sm;
			sm2.Parent = sm;
			object id = s.Save(sm);
			s.Save(sm1);
			s.Save(sm2);
			s.Flush();
			s.Close();

			sessions.Evict(typeof(SubMulti));

			s = OpenSession();
			// TODO: I don't understand why h2.0.3/h2.1 issues a select statement here

			Assert.AreEqual(2,
							s.CreateQuery(
								"select s from SubMulti as sm join sm.Children as s where s.Amount>-1 and s.Name is null").List().
								Count);
			Assert.AreEqual(2, s.CreateQuery("select elements(sm.Children) from SubMulti as sm").List().Count);
			Assert.AreEqual(1,
							s.CreateQuery(
								"select distinct sm from SubMulti as sm join sm.Children as s where s.Amount>-1 and s.Name is null")
								.List().Count);
			sm = (SubMulti) s.Load(typeof(SubMulti), id);
			Assert.AreEqual(2, sm.Children.Count);

			ICollection filterColl =
				s.CreateFilter(sm.MoreChildren, "select count(*) where this.Amount>-1 and this.Name is null").List();
			foreach (object obj in filterColl)
			{
				Assert.AreEqual(2, obj);
				// only want the first one
				break;
			}
			Assert.AreEqual("FOO", sm.Derived, "should have uppercased the column in a formula");

			IEnumerator enumer =
				s.CreateQuery("select distinct s from s in class SubMulti where s.MoreChildren[1].Amount < 1.0").Enumerable().
					GetEnumerator();
			Assert.IsTrue(enumer.MoveNext());
			Assert.AreSame(sm, enumer.Current);
			Assert.AreEqual(2, sm.MoreChildren.Count);
			s.Delete(sm);

			foreach (object obj in sm.Children)
			{
				s.Delete(obj);
			}
			s.Flush();
			s.Close();
		}
Ejemplo n.º 2
0
		public void MultiTable()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			Multi multi = new Multi();
			multi.ExtraProp = "extra";
			multi.Name = "name";
			Top simp = new Top();
			simp.Date = DateTime.Now;
			simp.Name = "simp";
			object mid;
			object sid;
			if (Dialect is MsSql2000Dialect)
			{
				mid = s.Save(multi);
				sid = s.Save(simp);
			}
			else
			{
				mid = 123L;
				sid = 1234L;
				s.Save(multi, mid);
				s.Save(simp, sid);
			}
			SubMulti sm = new SubMulti();
			sm.Amount = 66.5f;
			object smid;
			if (Dialect is MsSql2000Dialect)
			{
				smid = s.Save(sm);
			}
			else
			{
				smid = 2L;
				s.Save(sm, smid);
			}
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			multi.ExtraProp = multi.ExtraProp + "2";
			multi.Name = "new name";
			s.Update(multi, mid);
			simp.Name = "new name";
			s.Update(simp, sid);
			sm.Amount = 456.7f;
			s.Update(sm, smid);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			multi = (Multi) s.Load(typeof(Multi), mid);
			Assert.AreEqual("extra2", multi.ExtraProp);
			multi.ExtraProp = multi.ExtraProp + "3";
			Assert.AreEqual("new name", multi.Name);
			multi.Name = "newer name";
			sm = (SubMulti) s.Load(typeof(SubMulti), smid);
			Assert.AreEqual(456.7f, sm.Amount);
			sm.Amount = 23423f;
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			multi = (Multi) s.Load(typeof(Top), mid);
			simp = (Top) s.Load(typeof(Top), sid);
			Assert.IsFalse(simp is Multi);
			Assert.AreEqual("extra23", multi.ExtraProp);
			Assert.AreEqual("newer name", multi.Name);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			IEnumerator enumer = s.CreateQuery("select\n\ns from s in class Top where s.Count>0").Enumerable().GetEnumerator();
			bool foundSimp = false;
			bool foundMulti = false;
			bool foundSubMulti = false;
			while (enumer.MoveNext())
			{
				object o = enumer.Current;
				if ((o is Top) && !(o is Multi)) foundSimp = true;
				if ((o is Multi) && !(o is SubMulti)) foundMulti = true;
				if (o is SubMulti) foundSubMulti = true;
			}
			Assert.IsTrue(foundSimp);
			Assert.IsTrue(foundMulti);
			Assert.IsTrue(foundSubMulti);

			s.CreateQuery("from m in class Multi where m.Count>0 and m.ExtraProp is not null").List();
			s.CreateQuery("from m in class Top where m.Count>0 and m.Name is not null").List();
			s.CreateQuery("from m in class Lower where m.Other is not null").List();
			s.CreateQuery("from m in class Multi where m.Other.id = 1").List();
			s.CreateQuery("from m in class SubMulti where m.Amount > 0.0").List();

			Assert.AreEqual(2, s.CreateQuery("from m in class Multi").List().Count);

			//if( !(dialect is Dialect.HSQLDialect) ) 
			//{
			Assert.AreEqual(1, s.CreateQuery("from m in class Multi where m.class = SubMulti").List().Count);
			Assert.AreEqual(1, s.CreateQuery("from m in class Top where m.class = Multi").List().Count);
			//}

			Assert.AreEqual(3, s.CreateQuery("from s in class Top").List().Count);
			Assert.AreEqual(0, s.CreateQuery("from ls in class Lower").List().Count);
			Assert.AreEqual(1, s.CreateQuery("from sm in class SubMulti").List().Count);

			s.CreateQuery("from ls in class Lower, s in elements(ls.Bag) where s.id is not null").List();
			s.CreateQuery("from ls in class Lower, s in elements(ls.Set) where s.id is not null").List();
			s.CreateQuery("from sm in class SubMulti where exists elements(sm.Children)").List();

			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			if (TestDialect.SupportsSelectForUpdateOnOuterJoin)
				multi = (Multi)s.Load(typeof(Top), mid, LockMode.Upgrade);
			simp = (Top) s.Load(typeof(Top), sid);
			s.Lock(simp, LockMode.UpgradeNoWait);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			s.Update(multi, mid);
			s.Delete(multi);
			Assert.AreEqual(2, s.Delete("from s in class Top"));
			t.Commit();
			s.Close();
		}
Ejemplo n.º 3
0
		public void MultiTableGeneratedId()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			Multi multi = new Multi();
			multi.ExtraProp = "extra";
			multi.Name = "name";
			Top simp = new Top();
			simp.Date = DateTime.Now;
			simp.Name = "simp";
			object multiId = s.Save(multi);
			object simpId = s.Save(simp);
			SubMulti sm = new SubMulti();
			sm.Amount = 66.5f;
			object smId = s.Save(sm);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			multi.ExtraProp += "2";
			multi.Name = "new name";
			s.Update(multi, multiId);
			simp.Name = "new name";
			s.Update(simp, simpId);
			sm.Amount = 456.7f;
			s.Update(sm, smId);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			multi = (Multi) s.Load(typeof(Multi), multiId);
			Assert.AreEqual("extra2", multi.ExtraProp);
			multi.ExtraProp += "3";
			Assert.AreEqual("new name", multi.Name);
			multi.Name = "newer name";
			sm = (SubMulti) s.Load(typeof(SubMulti), smId);
			Assert.AreEqual(456.7f, sm.Amount);
			sm.Amount = 23423f;
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			multi = (Multi) s.Load(typeof(Top), multiId);
			simp = (Top) s.Load(typeof(Top), simpId);
			Assert.IsFalse(simp is Multi);
			// Can't see the point of this test since the variable is declared as Multi!
			//Assert.IsTrue( multi is Multi );
			Assert.AreEqual("extra23", multi.ExtraProp);
			Assert.AreEqual("newer name", multi.Name);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			IEnumerable enumer = s.CreateQuery("select\n\ns from s in class Top where s.Count>0").Enumerable();
			bool foundSimp = false;
			bool foundMulti = false;
			bool foundSubMulti = false;

			foreach (object obj in enumer)
			{
				if ((obj is Top) && !(obj is Multi)) foundSimp = true;
				if ((obj is Multi) && !(obj is SubMulti)) foundMulti = true;
				if (obj is SubMulti) foundSubMulti = true;
			}
			Assert.IsTrue(foundSimp);
			Assert.IsTrue(foundMulti);
			Assert.IsTrue(foundSubMulti);

			s.CreateQuery("from m in class Multi where m.Count>0 and m.ExtraProp is not null").List();
			s.CreateQuery("from m in class Top where m.Count>0 and m.Name is not null").List();
			s.CreateQuery("from m in class Lower where m.Other is not null").List();
			s.CreateQuery("from m in class Multi where m.Other.id = 1").List();
			s.CreateQuery("from m in class SubMulti where m.Amount > 0.0").List();

			Assert.AreEqual(2, s.CreateQuery("from m in class Multi").List().Count);
			Assert.AreEqual(3, s.CreateQuery("from s in class Top").List().Count);
			Assert.AreEqual(0, s.CreateQuery("from s in class Lower").List().Count);
			Assert.AreEqual(1, s.CreateQuery("from sm in class SubMulti").List().Count);

			s.CreateQuery("from ls in class Lower, s in elements(ls.Bag) where s.id is not null").List();
			s.CreateQuery("from sm in class SubMulti where exists elements(sm.Children)").List();
			
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			if (TestDialect.SupportsSelectForUpdateOnOuterJoin)
				multi = (Multi) s.Load(typeof(Top), multiId, LockMode.Upgrade);
			simp = (Top) s.Load(typeof(Top), simpId);
			s.Lock(simp, LockMode.UpgradeNoWait);
			t.Commit();
			s.Close();

			s = OpenSession();
			t = s.BeginTransaction();
			s.Update(multi, multiId);
			s.Delete(multi);
			Assert.AreEqual(2, s.Delete("from s in class Top"));
			t.Commit();
			s.Close();
		}
Ejemplo n.º 4
0
		public void Constraints()
		{
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			SubMulti sm = new SubMulti();
			sm.Amount = 66.5f;
			if (Dialect is MsSql2000Dialect)
			{
				s.Save(sm);
			}
			else
			{
				s.Save(sm, (long) 2);
			}
			t.Commit();
			s.Close();

			s = OpenSession();
			s.Delete("from sm in class SubMulti");
			t = s.BeginTransaction();
			t.Commit();
			s.Close();
		}