-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathSQLFunctionsTest.cs
221 lines (184 loc) · 7.21 KB
/
SQLFunctionsTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.Collections;
using NHibernate.DomainModel;
using NUnit.Framework;
namespace NHibernate.Test
{
/// <summary>
/// Summary description for SQLFunctionsTest.
/// </summary>
[TestFixture]
public class SQLFunctionsTest : TestCase
{
[SetUp]
public void SetUp()
{
ExportSchema( new string[] { "Simple.hbm.xml",
"Blobber.hbm.xml"
} );
}
[Test]
public void SetProperties()
{
ISession s = sessions.OpenSession();
ITransaction t = s.BeginTransaction();
Simple simple = new Simple();
simple.Name = "Simple 1";
s.Save(simple, (long)10);
IQuery q = s.CreateQuery("from s in class Simple where s.Name=:Name and s.Count=:Count");
q.SetProperties(simple);
Assert.AreEqual( simple, q.List()[0]);
s.Delete(simple);
t.Commit();
s.Close();
}
[Test]
public void NothingToUpdate()
{
ISession s = sessions.OpenSession();
ITransaction t = s.BeginTransaction();
Simple simple = new Simple();
simple.Name = "Simple 1";
s.Save( simple, (long)10 );
t.Commit();
s.Close();
s = sessions.OpenSession();
t = s.BeginTransaction();
s.Update( simple, (long)10 );
t.Commit();
s.Close();
s = sessions.OpenSession();
t = s.BeginTransaction();
s.Update( simple, (long)10 );
s.Delete(simple);
t.Commit();
s.Close();
}
[Test]
public void SQLFunctions()
{
ISession s = sessions.OpenSession();
ITransaction t = s.BeginTransaction();
Simple simple = new Simple();
simple.Name = "Simple 1";
s.Save( simple, (long)10 );
/*
* TODO: once DB2Dialect is implemented uncomment this
if( dialect is Dialect.DB2Dialect )
{
s.Find("from s in class Simple where repeat('foo', 3) = 'foofoofoo'");
s.Find("from s in class Simple where repeat(s.Name, 3) = 'foofoofoo'");
s.Find("from s in class Simple where repeat( lower(s.Name), 3 + (1-1) / 2) = 'foofoofoo'");
}
*/
Assert.AreEqual( 1, s.Find("from s in class Simple where upper(s.Name) = 'SIMPLE 1'").Count );
Assert.AreEqual( 1, s.Find("from s in class Simple where not( upper(s.Name)='yada' or 1=2 or 'foo'='bar' or not('foo'='foo') or 'foo' like 'bar')").Count );
if( !(dialect is Dialect.MySQLDialect) && !(dialect is Dialect.SybaseDialect) && !(dialect is Dialect.MsSql2000Dialect) )
{
// Dialect.MckoiDialect and Dialect.InterbaseDialect also included
// My Sql has a funny concatenation operator
Assert.AreEqual( 1, s.Find("from s in class Simple where lower(s.Name || ' foo')='simple 1 foo'").Count );
}
if( (dialect is Dialect.SybaseDialect) )
{
Assert.AreEqual( 1, s.Find("from s in class Simple where lower( concat(s.Name, ' foo') ) = 'simple 1 foo'").Count );
}
if( (dialect is Dialect.MsSql2000Dialect) )
{
Assert.AreEqual( 1, s.Find("from s in class Simple where lower( s.Name + ' foo' ) = 'simple 1 foo'").Count );
}
/*
* TODO: uncomment if MckoiDialect is ever implemented
if( (dialect is Dialect.MckoiDialect) )
{
Assert.AreEqual( 1, s.Find("from s in class Simple where lower( concat(s.Name, ' foo') ) = 'simple 1 foo'").Count );
}
*/
Simple other = new Simple();
other.Name = "Simple 2";
other.Count = 12;
simple.Other = other;
s.Save( other, (long)20 );
Assert.AreEqual( 1, s.Find("from s in class Simple where upper( s.Other.Name )='SIMPLE 2'").Count );
Assert.AreEqual( 0, s.Find("from s in class Simple where not (upper(s.Other.Name)='SIMPLE 2')").Count );
Assert.AreEqual( 1, s.Find("select distinct s from s in class Simple where ( ( s.Other.Count + 3) = (15*2)/2 and s.Count = 69) or ( (s.Other.Count + 2) / 7 ) = 2").Count );
Assert.AreEqual( 1, s.Find("select s from s in class Simple where ( ( s.Other.Count + 3) = (15*2)/2 and s.Count = 69) or ( (s.Other.Count + 2) / 7 ) = 2 order by s.Other.Count").Count );
Simple min = new Simple();
min.Count = -1;
s.Save( min, (long)30 );
// && !(dialect is Dialect.HSQLDialect)
// MySql has no subqueries
if( !(dialect is Dialect.MySQLDialect) )
{
Assert.AreEqual( 2, s.Find("from s in class Simple where s.Count > ( select min(sim.Count) from sim in class NHibernate.DomainModel.Simple )").Count );
t.Commit();
t = s.BeginTransaction();
Assert.AreEqual( 2, s.Find("from s in class Simple where s = some( select sim from sim in class NHibernate.DomainModel.Simple where sim.Count>=0) and s.Count >= 0").Count );
Assert.AreEqual( 1, s.Find("from s in class Simple where s = some( select sim from sim in class NHibernate.DomainModel.Simple where sim.Other.Count=s.Other.Count ) and s.Other.Count > 0").Count );
}
IEnumerator enumer = s.Enumerable("select sum(s.Count) from s in class Simple group by s.Count having sum(s.Count) > 10 ").GetEnumerator();
Assert.IsTrue( enumer.MoveNext() );
Assert.AreEqual(12, (Int32)enumer.Current );
Assert.IsFalse( enumer.MoveNext() );
if( !(dialect is Dialect.MySQLDialect) )
{
enumer = s.Enumerable("select s.Count from s in class Simple group by s.Count having s.Count = 12").GetEnumerator();
Assert.IsTrue( enumer.MoveNext() );
}
enumer = s.Enumerable("select s.id, s.Count, count(t), max(t.Date) from s in class Simple, t in class Simple where s.Count = t.Count group by s.id, s.Count order by s.Count").GetEnumerator();
IQuery q = s.CreateQuery("from s in class Simple");
q.SetMaxResults(10);
Assert.AreEqual( 3, q.List().Count );
q = s.CreateQuery("from s in class Simple");
q.SetMaxResults(1);
Assert.AreEqual( 1, q.List().Count );
q = s.CreateQuery("from s in class Simple");
Assert.AreEqual( 3, q.List().Count );
q = s.CreateQuery("from s in class Simple where s.Name = ?");
q.SetString(0, "Simple 1");
Assert.AreEqual( 1, q.List().Count );
q = s.CreateQuery("from s in class Simple where s.Name = ? and upper(s.Name) = ?");
q.SetString( 1, "SIMPLE 1" );
q.SetString( 0, "Simple 1" );
q.SetFirstResult(0);
Assert.IsTrue( q.Enumerable().GetEnumerator().MoveNext() );
q = s.CreateQuery("from s in class Simple where s.Name = :foo and upper(s.Name) = :bar or s.Count=:count or s.Count=:count + 1");
q.SetParameter("bar", "SIMPLE 1");
q.SetString("foo", "Simple 1");
q.SetInt32("count", 69);
q.SetFirstResult(0);
Assert.IsTrue( q.Enumerable().GetEnumerator().MoveNext() );
q = s.CreateQuery("select s.id from s in class Simple");
q.SetFirstResult(1);
q.SetMaxResults(2);
IEnumerable enumerable = q.Enumerable();
int i=0;
foreach( object obj in enumerable )
{
Assert.IsTrue( obj is Int64 );
i++;
}
Assert.AreEqual( 2, i );
q = s.CreateQuery("select all s, s.Other from s in class Simple where s = :s");
q.SetParameter("s", simple);
Assert.AreEqual( 1, q.List().Count );
q = s.CreateQuery("from s in class Simple where s.Name in (:name_list) and s.Count > :count");
IList list = new ArrayList(2);
list.Add("Simple 1");
list.Add("foo");
q.SetParameterList( "name_list", list );
q.SetParameter( "count", (int)-1 );
Assert.AreEqual( 1, q.List().Count );
s.Delete(other);
s.Delete(simple);
s.Delete(min);
t.Commit();
s.Close();
}
[Test]
[Ignore("BLOB/CLOB not implmented like h2.0.3 - http://jira.nhibernate.org:8080/browse/NH-19")]
public void BlobClob()
{
}
}
}