Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 84410b0

Browse files
authored
Merge pull request #571 from OlegNadymov/master
Unit tests for using GetQuotedColumnName instead of DialectProvider.GetQuotedColumnName inside SqlExpression
2 parents 620a900 + 301519d commit 84410b0

File tree

1 file changed

+189
-38
lines changed

1 file changed

+189
-38
lines changed
Lines changed: 189 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,61 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Data;
4+
using System.Linq;
35
using System.Linq.Expressions;
46
using NUnit.Framework;
7+
using ServiceStack.DataAnnotations;
58
using ServiceStack.OrmLite.Sqlite;
69

710

811
namespace ServiceStack.OrmLite.Tests
912
{
10-
public class Waybill
13+
public class ObjectBase
1114
{
15+
[PrimaryKey]
1216
public int Id { get; set; }
17+
}
1318

19+
public class WaybillBase : ObjectBase
20+
{
1421
public int Number { get; set; }
1522

1623
public string Name { get; set; }
1724

25+
[DataAnnotations.Ignore]
1826
public string VirtProperty => "WaybillVirtPropertyValue";
1927

28+
[DataAnnotations.Ignore]
2029
public string VirtProperty2 => "WaybillVirtPropertyValue2";
2130

31+
[DataAnnotations.Ignore]
2232
public bool BoolVirtProperty => false;
2333
}
2434

35+
public class WaybillIn : WaybillBase
36+
{
37+
public DateTime DateBegin { get; set; }
38+
39+
public DateTime DateEnd { get; set; }
40+
41+
public string Note { get; set; }
42+
}
43+
44+
/// <summary>
45+
/// Class only for creating the table and population it with data.
46+
/// </summary>
47+
[Alias(nameof(WaybillIn))]
48+
public class SeparateWaybillIn
49+
{
50+
public int Id { get; set; }
51+
52+
public DateTime DateBegin { get; set; }
53+
54+
public DateTime DateEnd { get; set; }
55+
56+
public string Note { get; set; }
57+
}
58+
2559
public class CustomSqlServerDialectProvider : SqliteOrmLiteDialectProvider
2660
{
2761
public override SqlExpression<T> SqlExpression<T>()
@@ -39,18 +73,63 @@ public CustomSqlExpression(IOrmLiteDialectProvider dialectProvider) : base(diale
3973

4074
protected override Object GetMemberExpression(MemberExpression m)
4175
{
42-
if (m.Member.DeclaringType == typeof(Waybill))
76+
if (m.Member.DeclaringType == typeof(WaybillBase))
4377
{
44-
if (m.Member.Name == nameof(Waybill.VirtProperty))
78+
if (m.Member.Name == nameof(WaybillBase.VirtProperty))
4579
return "WaybillVirtPropertyValue";
46-
if (m.Member.Name == nameof(Waybill.VirtProperty2))
80+
if (m.Member.Name == nameof(WaybillBase.VirtProperty2))
4781
return "WaybillVirtPropertyValue2";
48-
if (m.Member.Name == nameof(Waybill.BoolVirtProperty))
82+
if (m.Member.Name == nameof(WaybillBase.BoolVirtProperty))
4983
return false;
5084
}
5185

5286
return base.GetMemberExpression(m);
5387
}
88+
89+
protected override string GetQuotedColumnName(ModelDefinition tableDef, String memberName)
90+
{
91+
if (useFieldName)
92+
{
93+
var actualTableDefForMember = GetCurrentTableDef(tableDef, memberName);
94+
if (tableDef.ModelName != actualTableDefForMember.ModelName)
95+
{
96+
CreateHierarchyJoin(actualTableDefForMember, tableDef);
97+
}
98+
99+
return base.GetQuotedColumnName(actualTableDefForMember, memberName);
100+
}
101+
102+
return base.GetQuotedColumnName(tableDef, memberName);
103+
}
104+
105+
protected virtual void CreateHierarchyJoin(ModelDefinition actualHierarchyTableDef, ModelDefinition mainHierarchyTableDef)
106+
{
107+
}
108+
109+
private ModelDefinition GetCurrentTableDef(ModelDefinition tableDef, string memberName)
110+
{
111+
var curType = tableDef.ModelType;
112+
var nonInheritedProperties = GetCurrentPropertiesWithoutBase(tableDef);
113+
while (curType != null && !nonInheritedProperties.Contains(memberName))
114+
{
115+
curType = curType.BaseType;
116+
nonInheritedProperties = GetCurrentPropertiesWithoutBase(curType?.GetModelMetadata());
117+
}
118+
119+
return curType?.GetModelMetadata() ?? tableDef;
120+
}
121+
122+
protected virtual List<string> GetCurrentPropertiesWithoutBase(ModelDefinition currentModelDef)
123+
{
124+
if (currentModelDef == null) return null;
125+
126+
var baseType = currentModelDef.ModelType;
127+
var res = new List<string> { currentModelDef.PrimaryKey.Name };
128+
129+
res.AddRange(baseType.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly).Select(a => a.Name));
130+
131+
return res;
132+
}
54133
}
55134

56135
[TestFixture]
@@ -70,12 +149,19 @@ public void Setup()
70149
{
71150
using (var db = OpenDbConnection())
72151
{
73-
db.DropTable<Waybill>();
152+
db.DropTable<WaybillBase>();
153+
154+
db.CreateTable<WaybillBase>();
155+
db.Insert(new WaybillBase {Id = 1, Number = 100, Name = "first"});
156+
db.Insert(new WaybillBase {Id = 2, Number = 200, Name = "second"});
157+
db.Insert(new WaybillBase {Id = 3, Number = 300, Name = "third"});
74158

75-
db.CreateTable<Waybill>();
76-
db.Insert(new Waybill {Id = 1, Number = 100, Name = "first"});
77-
db.Insert(new Waybill {Id = 2, Number = 200, Name = "second"});
78-
db.Insert(new Waybill {Id = 3, Number = 300, Name = "third"});
159+
db.DropTable<SeparateWaybillIn>();
160+
161+
db.CreateTable<SeparateWaybillIn>();
162+
db.Insert(new SeparateWaybillIn { Id = 1, DateBegin = DateTime.Parse("2014-01-01"), DateEnd = DateTime.Parse("2014-01-03"), Note = "firstNote"});
163+
db.Insert(new SeparateWaybillIn { Id = 2, DateBegin = DateTime.Parse("2015-01-01"), DateEnd = DateTime.Parse("2015-01-03"), Note = "secondNote" });
164+
db.Insert(new SeparateWaybillIn { Id = 3, DateBegin = DateTime.Parse("2016-01-01"), DateEnd = DateTime.Parse("2016-01-03"), Note = "thirdNote" });
79165
}
80166
Db = OpenDbConnection();
81167
}
@@ -89,131 +175,196 @@ public void TearDown()
89175
[Test]
90176
public void Can_Where_using_constant_filter1()
91177
{
92-
System.Linq.Expressions.Expression<Func<Waybill, bool>> filter = x => x.VirtProperty == "WaybillVirtPropertyValue";
93-
var q = Db.From<Waybill>().Where(filter);
178+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.VirtProperty == "WaybillVirtPropertyValue";
179+
var q = Db.From<WaybillBase>().Where(filter);
94180
var target = Db.Select(q);
95181
Assert.AreEqual(3, target.Count);
96182
}
97183

98184
[Test]
99185
public void Can_Where_using_constant_filter2()
100186
{
101-
System.Linq.Expressions.Expression<Func<Waybill, bool>> filter = x => x.VirtProperty == "Any";
102-
var q = Db.From<Waybill>().Where(filter);
187+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.VirtProperty == "Any";
188+
var q = Db.From<WaybillBase>().Where(filter);
103189
var target = Db.Select(q);
104190
Assert.AreEqual(0, target.Count);
105191
}
106192

107193
[Test]
108194
public void Can_Where_using_constant_filter3()
109195
{
110-
System.Linq.Expressions.Expression<Func<Waybill, bool>> filter = x => x.VirtProperty != "WaybillVirtPropertyValue";
111-
var q = Db.From<Waybill>().Where(filter);
196+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.VirtProperty != "WaybillVirtPropertyValue";
197+
var q = Db.From<WaybillBase>().Where(filter);
112198
var target = Db.Select(q);
113199
Assert.AreEqual(0, target.Count);
114200
}
115201

116202
[Test]
117203
public void Can_Where_using_constant_filter4()
118204
{
119-
System.Linq.Expressions.Expression<Func<Waybill, bool>> filter = x => x.VirtProperty != "Any";
120-
var q = Db.From<Waybill>().Where(filter);
205+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.VirtProperty != "Any";
206+
var q = Db.From<WaybillBase>().Where(filter);
121207
var target = Db.Select(q);
122208
Assert.AreEqual(3, target.Count);
123209
}
124210

125211
[Test]
126212
public void Can_Where_using_constant_filter5()
127213
{
128-
System.Linq.Expressions.Expression<Func<Waybill, bool>> filter = x => x.VirtProperty == "WaybillVirtPropertyValue" || x.VirtProperty2 == "WaybillVirtPropertyValue2";
129-
var q = Db.From<Waybill>().Where(filter);
214+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.VirtProperty == "WaybillVirtPropertyValue" || x.VirtProperty2 == "WaybillVirtPropertyValue2";
215+
var q = Db.From<WaybillBase>().Where(filter);
130216
var target = Db.Select(q);
131217
Assert.AreEqual(3, target.Count);
132218
}
133219

134220
[Test]
135221
public void Can_Where_using_constant_filter6()
136222
{
137-
System.Linq.Expressions.Expression<Func<Waybill, bool>> filter = x => x.VirtProperty == "WaybillVirtPropertyValue" && x.VirtProperty2 == "WaybillVirtPropertyValue2";
138-
var q = Db.From<Waybill>().Where(filter);
223+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.VirtProperty == "WaybillVirtPropertyValue" && x.VirtProperty2 == "WaybillVirtPropertyValue2";
224+
var q = Db.From<WaybillBase>().Where(filter);
139225
var target = Db.Select(q);
140226
Assert.AreEqual(3, target.Count);
141227
}
142228

143229
[Test]
144230
public void Can_Where_using_constant_filter7()
145231
{
146-
System.Linq.Expressions.Expression<Func<Waybill, bool>> filter = x => x.VirtProperty == "WaybillVirtPropertyValue" || x.VirtProperty2 == "Any";
147-
var q = Db.From<Waybill>().Where(filter);
232+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.VirtProperty == "WaybillVirtPropertyValue" || x.VirtProperty2 == "Any";
233+
var q = Db.From<WaybillBase>().Where(filter);
148234
var target = Db.Select(q);
149235
Assert.AreEqual(3, target.Count);
150236
}
151237

152238
[Test]
153239
public void Can_Where_using_constant_filter8()
154240
{
155-
System.Linq.Expressions.Expression<Func<Waybill, bool>> filter = x => x.VirtProperty == "WaybillVirtPropertyValue" && x.VirtProperty2 == "Any";
156-
var q = Db.From<Waybill>().Where(filter);
241+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.VirtProperty == "WaybillVirtPropertyValue" && x.VirtProperty2 == "Any";
242+
var q = Db.From<WaybillBase>().Where(filter);
157243
var target = Db.Select(q);
158244
Assert.AreEqual(0, target.Count);
159245
}
160246

161247
[Test]
162248
public void Can_Where_using_constant_filter9()
163249
{
164-
System.Linq.Expressions.Expression<Func<Waybill, bool>> filter = x => x.BoolVirtProperty;
165-
var q = Db.From<Waybill>().Where(filter);
250+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.BoolVirtProperty;
251+
var q = Db.From<WaybillBase>().Where(filter);
166252
var target = Db.Select(q);
167253
Assert.AreEqual(0, target.Count);
168254
}
169255

170256
[Test]
171257
public void Can_Where_using_constant_filter10()
172258
{
173-
System.Linq.Expressions.Expression<Func<Waybill, bool>> filter = x => !x.BoolVirtProperty;
174-
var q = Db.From<Waybill>().Where(filter);
259+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => !x.BoolVirtProperty;
260+
var q = Db.From<WaybillBase>().Where(filter);
175261
var target = Db.Select(q);
176262
Assert.AreEqual(3, target.Count);
177263
}
178264

179265
[Test]
180266
public void Can_Where_using_constant_filter11()
181267
{
182-
System.Linq.Expressions.Expression<Func<Waybill, bool>> filter = x => x.BoolVirtProperty && x.VirtProperty == "WaybillVirtPropertyValue";
183-
var q = Db.From<Waybill>().Where(filter);
268+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.BoolVirtProperty && x.VirtProperty == "WaybillVirtPropertyValue";
269+
var q = Db.From<WaybillBase>().Where(filter);
184270
var target = Db.Select(q);
185271
Assert.AreEqual(0, target.Count);
186272
}
187273

188274
[Test]
189275
public void Can_Where_using_constant_filter12()
190276
{
191-
System.Linq.Expressions.Expression<Func<Waybill, bool>> filter = x => !x.BoolVirtProperty || x.VirtProperty == "WaybillVirtPropertyValue";
192-
var q = Db.From<Waybill>().Where(filter);
277+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => !x.BoolVirtProperty || x.VirtProperty == "WaybillVirtPropertyValue";
278+
var q = Db.From<WaybillBase>().Where(filter);
193279
var target = Db.Select(q);
194280
Assert.AreEqual(3, target.Count);
195281
}
196282

197283
[Test]
198284
public void Can_Where_using_constant_filter13()
199285
{
200-
System.Linq.Expressions.Expression<Func<Waybill, bool>> filter = x => !x.BoolVirtProperty &&
286+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => !x.BoolVirtProperty &&
201287
x.VirtProperty == "WaybillVirtPropertyValue" &&
202288
x.Number == 100;
203-
var q = Db.From<Waybill>().Where(filter);
289+
var q = Db.From<WaybillBase>().Where(filter);
204290
var target = Db.Select(q);
205291
Assert.AreEqual(1, target.Count);
206292
}
207293

208294
[Test]
209295
public void Can_Where_using_constant_filter14()
210296
{
211-
System.Linq.Expressions.Expression<Func<Waybill, bool>> filter = x => x.Number == 100 &&
297+
System.Linq.Expressions.Expression<Func<WaybillBase, bool>> filter = x => x.Number == 100 &&
212298
(x.BoolVirtProperty ||
213299
x.VirtProperty == "WaybillVirtPropertyValue");
214-
var q = Db.From<Waybill>().Where(filter);
300+
var q = Db.From<WaybillBase>().Where(filter);
215301
var target = Db.Select(q);
216302
Assert.AreEqual(1, target.Count);
217303
}
304+
305+
[Test]
306+
public void Can_Select_hierarchy_classes1()
307+
{
308+
var q1 = Db.From<WaybillIn>();
309+
q1.PrefixFieldWithTableName = true;
310+
q1.Select(x => new {x.Name, x.Number});
311+
q1.SelectInto<WaybillIn>();
312+
var sql1 = q1.SelectExpression;
313+
314+
var q2 = Db.From<WaybillBase>();
315+
q2.PrefixFieldWithTableName = true;
316+
q2.Select(x => new {x.Name, x.Number});
317+
q2.SelectInto<WaybillIn>();
318+
var sql2 = q2.SelectExpression;
319+
320+
Assert.AreEqual(sql1, sql2);
321+
}
322+
323+
[Test]
324+
public void Can_Select_hierarchy_classes2()
325+
{
326+
var q = Db.From<WaybillIn>();
327+
q.PrefixFieldWithTableName = true;
328+
q.Join<WaybillBase>((x, y) => x.Id == y.Id);
329+
q.Where(x => x.Name == "first" && x.Note == "firstNote");
330+
var target = Db.Select(q);
331+
332+
Assert.AreEqual(1, target.Count);
333+
334+
var obj = target[0];
335+
Assert.AreEqual(DateTime.Parse("2014-01-01"), obj.DateBegin);
336+
}
337+
338+
[Test]
339+
public void Can_Select_hierarchy_classes3()
340+
{
341+
var q = Db.From<WaybillIn>();
342+
q.PrefixFieldWithTableName = true;
343+
q.Join<WaybillBase>((x, y) => x.Id == y.Id);
344+
q.Where(x => x.Name == "first" && x.Note == "firstNote");
345+
q.Select(new [] {nameof(WaybillBase.Number)});
346+
var target = Db.Column<int>(q);
347+
348+
Assert.AreEqual(1, target.Count);
349+
350+
var obj = target[0];
351+
Assert.AreEqual(100, obj);
352+
}
353+
354+
[Test]
355+
public void Can_Select_hierarchy_classes4()
356+
{
357+
var q = Db.From<WaybillIn>();
358+
q.PrefixFieldWithTableName = true;
359+
q.Join<WaybillBase>((x, y) => x.Id == y.Id);
360+
q.Where(x => x.Name == "first" && x.Note == "firstNote");
361+
q.OrderByFields(nameof(WaybillBase.Number));
362+
var target = Db.Select(q);
363+
364+
Assert.AreEqual(1, target.Count);
365+
366+
var obj = target[0];
367+
Assert.AreEqual(DateTime.Parse("2014-01-01"), obj.DateBegin);
368+
}
218369
}
219370
}

0 commit comments

Comments
 (0)