forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSqlClientDriverFixture.cs
215 lines (190 loc) · 7.44 KB
/
SqlClientDriverFixture.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
using System;
using System.Collections;
using System.Data;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Engine;
using NHibernate.SqlTypes;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;
namespace NHibernate.Test.DriverTest
{
public class MultiTypeEntity
{
public virtual int Id { get; set; }
public virtual string StringProp { get; set; }
public virtual string AnsiStringProp { get; set; }
public virtual decimal Decimal { get; set; }
public virtual decimal DecimalHighScale { get; set; }
public virtual decimal Currency { get; set; }
public virtual double Double { get; set; }
public virtual float Float { get; set; }
public virtual byte[] BinaryBlob { get; set; }
public virtual byte[] Binary { get; set; }
public virtual string StringClob { get; set; }
public virtual DateTime DateTimeProp { get; set; } = DateTime.Now;
}
[TestFixture]
public class SqlClientDriverFixture : TestCase
{
protected override string MappingsAssembly => "NHibernate.Test";
protected override string[] Mappings => new[] { "DriverTest.MultiTypeEntity.hbm.xml" };
protected override bool AppliesTo(Dialect.Dialect dialect)
{
return dialect is MsSql2008Dialect;
}
protected override bool AppliesTo(ISessionFactoryImplementor factory)
{
return factory.ConnectionProvider.Driver is SqlClientDriver;
}
protected override void OnTearDown()
{
base.OnTearDown();
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
s.CreateQuery("delete from MultiTypeEntity").ExecuteUpdate();
t.Commit();
}
}
[Test]
public void Crud()
{
// Should use default dimension for CRUD op because the mapping does not
// have dimensions specified.
object savedId;
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
savedId = s.Save(
new MultiTypeEntity
{
StringProp = "a",
StringClob = "a",
BinaryBlob = new byte[] { 1, 2, 3 },
Binary = new byte[] { 4, 5, 6 },
Currency = 123.4m,
Double = 123.5d,
Decimal = 789.5m,
DecimalHighScale = 1234567890.0123456789m
});
t.Commit();
}
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var m = s.Get<MultiTypeEntity>(savedId);
Assert.That(m.StringProp, Is.EqualTo("a"), "StringProp");
Assert.That(m.StringClob, Is.EqualTo("a"), "StringClob");
Assert.That(m.BinaryBlob, Is.EqualTo(new byte[] { 1, 2, 3 }), "BinaryBlob");
Assert.That(m.Binary, Is.EqualTo(new byte[] { 4, 5, 6 }), "BinaryBlob");
Assert.That(m.Currency, Is.EqualTo(123.4m), "Currency");
Assert.That(m.Double, Is.EqualTo(123.5d).Within(0.0001d), "Double");
Assert.That(m.Decimal, Is.EqualTo(789.5m), "Decimal");
Assert.That(m.DecimalHighScale, Is.EqualTo(1234567890.0123456789m), "DecimalHighScale");
m.StringProp = "b";
m.StringClob = "b";
m.BinaryBlob = new byte[] { 4, 5, 6 };
m.Binary = new byte[] { 7, 8, 9 };
m.Currency = 456.78m;
m.Double = 987.6d;
m.Decimal = 1323456.45m;
m.DecimalHighScale = 9876543210.0123456789m;
t.Commit();
}
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var m = s.Load<MultiTypeEntity>(savedId);
Assert.That(m.StringProp, Is.EqualTo("b"), "StringProp");
Assert.That(m.StringClob, Is.EqualTo("b"), "StringClob");
Assert.That(m.BinaryBlob, Is.EqualTo(new byte[] { 4, 5, 6 }), "BinaryBlob");
Assert.That(m.Binary, Is.EqualTo(new byte[] { 7, 8, 9 }), "BinaryBlob");
Assert.That(m.Currency, Is.EqualTo(456.78m), "Currency");
Assert.That(m.Double, Is.EqualTo(987.6d).Within(0.0001d), "Double");
Assert.That(m.Decimal, Is.EqualTo(1323456.45m), "Decimal");
Assert.That(m.DecimalHighScale, Is.EqualTo(9876543210.0123456789m), "DecimalHighScale");
t.Commit();
}
}
[Test]
public void QueryPlansAreReused()
{
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
// clear the existing plan cache
s.CreateSQLQuery("DBCC FREEPROCCACHE").ExecuteUpdate();
t.Commit();
}
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
var countPlansCommand = s.CreateSQLQuery("SELECT COUNT(*) FROM sys.dm_exec_cached_plans");
var beforeCount = countPlansCommand.UniqueResult<int>();
var insertCount = 10;
for (var i = 0; i < insertCount; i++)
{
s.Save(new MultiTypeEntity() { StringProp = new string('x', i + 1) });
s.Flush();
}
var afterCount = countPlansCommand.UniqueResult<int>();
Assert.That(
afterCount - beforeCount,
Is.LessThan(insertCount - 1),
$"Excessive query plans created: before={beforeCount} after={afterCount}");
t.Rollback();
}
}
[Test]
public void DefaultPrecisionScale()
{
const byte defaultPrecision = 29;
const byte defaultScale = 10;
var driver = Sfi.ConnectionProvider.Driver;
try
{
using (var cmd = new System.Data.SqlClient.SqlCommand())
{
var p = driver.GenerateParameter(cmd, "p", SqlTypeFactory.Decimal);
Assert.That(p.Precision, Is.EqualTo(defaultPrecision), "no defaults");
Assert.That(p.Scale, Is.EqualTo(defaultScale), "no defaults");
p = driver.GenerateParameter(cmd, "p", SqlTypeFactory.GetSqlType(DbType.Decimal, 24, 11));
Assert.That(p.Precision, Is.EqualTo(24), "explicit without defaults");
Assert.That(p.Scale, Is.EqualTo(11), "explicit without defaults");
var configuration = TestConfigurationHelper.GetDefaultConfiguration();
configuration.SetProperty(Environment.QueryDefaultCastPrecision, "26");
driver.Configure(configuration.Properties);
p = driver.GenerateParameter(cmd, "p", SqlTypeFactory.Decimal);
Assert.That(p.Precision, Is.EqualTo(26), "default precision 26");
Assert.That(p.Scale, Is.EqualTo(defaultScale), "default precision 26");
p = driver.GenerateParameter(cmd, "p", SqlTypeFactory.GetSqlType(DbType.Decimal, 24, 11));
Assert.That(p.Precision, Is.EqualTo(24), "explicit 24 with default precision 26");
Assert.That(p.Scale, Is.EqualTo(11), "explicit 24 with default precision 26");
configuration.Properties.Remove(Environment.QueryDefaultCastPrecision);
configuration.SetProperty(Environment.QueryDefaultCastScale, "8");
driver.Configure(configuration.Properties);
p = driver.GenerateParameter(cmd, "p", SqlTypeFactory.Decimal);
Assert.That(p.Precision, Is.EqualTo(defaultPrecision), "default scale 8");
Assert.That(p.Scale, Is.EqualTo(8), "default scale 8");
p = driver.GenerateParameter(cmd, "p", SqlTypeFactory.GetSqlType(DbType.Decimal, 24, 11));
Assert.That(p.Precision, Is.EqualTo(24), "explicit with default scale 8");
Assert.That(p.Scale, Is.EqualTo(11), "explicit with default scale 8");
configuration.SetProperty(Environment.QueryDefaultCastPrecision, "34");
configuration.SetProperty(Environment.QueryDefaultCastScale, "15");
driver.Configure(configuration.Properties);
p = driver.GenerateParameter(cmd, "p", SqlTypeFactory.Decimal);
Assert.That(p.Precision, Is.EqualTo(34), "default 34,15");
Assert.That(p.Scale, Is.EqualTo(15), "default 34,15");
p = driver.GenerateParameter(cmd, "p", SqlTypeFactory.GetSqlType(DbType.Decimal, 24, 11));
Assert.That(p.Precision, Is.EqualTo(24), "explicit with default 34,15");
Assert.That(p.Scale, Is.EqualTo(11), "explicit with default 34,15");
}
}
finally
{
driver.Configure(cfg.GetDerivedProperties());
}
}
}
}