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

Commit 3181b0c

Browse files
committed
Give SelectInto<TModel> highest priority for field resolution if TModel is apart of JOIN list
1 parent 3e1ffa5 commit 3181b0c

File tree

2 files changed

+177
-19
lines changed

2 files changed

+177
-19
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.Join.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,20 @@ public string SelectInto<TModel>()
140140

141141
var sbSelect = new StringBuilder();
142142
var selectDef = typeof(TModel).GetModelDefinition();
143+
144+
var orderedDefs = tableDefs;
145+
if (selectDef != modelDef && tableDefs.Contains(selectDef))
146+
{
147+
orderedDefs = tableDefs.ToList(); //clone
148+
orderedDefs.Remove(selectDef);
149+
orderedDefs.Insert(0, selectDef);
150+
}
151+
143152
foreach (var fieldDef in selectDef.FieldDefinitions)
144153
{
145154
var found = false;
146155

147-
foreach (var tableDef in tableDefs)
156+
foreach (var tableDef in orderedDefs)
148157
{
149158
foreach (var tableFieldDef in tableDef.FieldDefinitions)
150159
{
@@ -168,7 +177,7 @@ public string SelectInto<TModel>()
168177
if (!found)
169178
{
170179
// Add support for auto mapping `{Table}{Field}` convention
171-
foreach (var tableDef in tableDefs)
180+
foreach (var tableDef in orderedDefs)
172181
{
173182
var matchingField = tableDef.FieldDefinitions
174183
.FirstOrDefault(x =>

tests/ServiceStack.OrmLite.Tests/Issues/SchemaTests.cs

Lines changed: 166 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using NUnit.Framework;
1+
using System;
2+
using NUnit.Framework;
23
using ServiceStack.DataAnnotations;
4+
using ServiceStack.Model;
35
using ServiceStack.Text;
46

57
namespace ServiceStack.OrmLite.Tests.Issues
@@ -75,16 +77,13 @@ public void Can_query_with_Schema_and_alias_attributes()
7577
db.DropAndCreateTable<Page>();
7678

7779
db.Save(new Page {
78-
ReportSectionId = 1,
7980
SectionId = 1,
8081
}, references: true);
8182
db.Save(new Page {
82-
ReportSectionId = 2,
8383
SectionId = 2,
8484
}, references: true);
8585
db.Save(new Section {
8686
Id = 1,
87-
ReportSectionId = 1,
8887
Name = "Name1",
8988
ReportId = 15,
9089
}, references: true);
@@ -102,34 +101,184 @@ public void Can_query_with_Schema_and_alias_attributes()
102101
Assert.That(results[0].Name, Is.EqualTo("Name1"));
103102
}
104103
}
104+
105+
[Test]
106+
public void Does_complex_query_using_Schemas_with_LeftJoins()
107+
{
108+
using (var db = OpenDbConnection())
109+
{
110+
db.DropAndCreateTable<Editable>();
111+
db.DropAndCreateTable<EditableRevision>();
112+
db.DropAndCreateTable<LogEntry>();
113+
db.DropAndCreateTable<Report>();
114+
db.DropAndCreateTable<Page>();
115+
db.DropAndCreateTable<Section>();
116+
117+
var q = db.From<Section>()
118+
.Join<Section, Page>((s, p) => s.Id == p.SectionId)
119+
.Join<Page, Editable>((p, e) => p.Id == e.PageId)
120+
.Where<Section, Page>((s, p) => s.ReportId == 15 && p.Index == 24);
121+
122+
var result = db.Select<Editable>(q);
123+
124+
db.GetLastSql().Print();
125+
}
126+
}
127+
}
128+
129+
[Alias("Editables")]
130+
[Schema("MicroSite")]
131+
public partial class Editable : IHasId<int>
132+
{
133+
public string Content { get; set; }
134+
135+
[Alias("EditableID")]
136+
[AutoIncrement]
137+
[PrimaryKey]
138+
public int Id { get; set; }
139+
140+
[Required]
141+
public int Index { get; set; }
142+
143+
[Alias("ReportPageID")]
144+
[Required]
145+
public int PageId { get; set; }
146+
147+
public string Styles { get; set; }
148+
149+
[Alias("Type")]
150+
[Required]
151+
public int TypeId { get; set; }
152+
}
153+
154+
[Alias("EditableRevisions")]
155+
[Schema("MicroSite")]
156+
public partial class EditableRevision : IHasId<int>
157+
{
158+
public string Content { get; set; }
159+
160+
[Required]
161+
public DateTime Date { get; set; }
162+
163+
[Alias("EditableID")]
164+
[Required]
165+
public int EditableId { get; set; }
166+
167+
[Required]
168+
public int EmployeeId { get; set; }
169+
170+
[Alias("EditableRevisionsID")]
171+
[AutoIncrement]
172+
[PrimaryKey]
173+
public int Id { get; set; }
174+
175+
public string Reason { get; set; }
176+
public string Styles { get; set; }
105177
}
106178

107-
[Schema("Schema")]
108-
[Alias("PageAlias")]
109-
public class Page
179+
[Alias("LogEntries")]
180+
[Schema("MicroSite")]
181+
public class LogEntry : IHasId<int>
110182
{
183+
[Required]
184+
public DateTime Date { get; set; }
185+
186+
[Alias("LogEntriesID")]
111187
[AutoIncrement]
188+
[PrimaryKey]
112189
public int Id { get; set; }
113190

114-
[Alias("ReportSectionIdAlias")]
115-
public int ReportSectionId { get; set; }
191+
[Required]
192+
public int KlasId { get; set; }
193+
194+
[Required]
195+
public int PageTrackerId { get; set; }
196+
197+
[Alias("ReportID")]
198+
[Required]
199+
public int ReportId { get; set; }
200+
201+
[Alias("ReportPageID")]
202+
[Required]
203+
public int PageId { get; set; }
204+
205+
public string RequestUrl { get; set; }
206+
207+
[Alias("Type")]
208+
[Required]
209+
public int TypeId { get; set; }
210+
}
211+
212+
[Alias("ReportPages")]
213+
[Schema("MicroSite")]
214+
public partial class Page : IHasId<int>
215+
{
216+
[Required]
217+
public int AccessLevel { get; set; }
218+
219+
[Required]
220+
public int AssignedEmployeeId { get; set; }
221+
222+
[Required]
223+
public bool Cover { get; set; }
224+
225+
[Required]
226+
public bool Deleted { get; set; }
227+
228+
[Required]
229+
public bool Disabled { get; set; }
230+
231+
[Alias("ReportPageID")]
232+
[AutoIncrement]
233+
[PrimaryKey]
234+
public int Id { get; set; }
116235

117-
[Alias("SectionIdAlias")]
236+
[Required]
237+
public int Index { get; set; }
238+
239+
public string Name { get; set; }
240+
241+
[Alias("ReportSectionID")]
242+
[Required]
118243
public int SectionId { get; set; }
244+
245+
public string Template { get; set; }
119246
}
120247

121-
[Schema("Schema")]
122-
[Alias("SectionAlias")]
123-
public class Section
248+
[Alias("Reports")]
249+
[Schema("MicroSite")]
250+
public partial class Report : IHasId<int>
124251
{
252+
[Required]
253+
public int DefaultAccessLevel { get; set; }
254+
255+
[Required]
256+
public bool Deleted { get; set; }
257+
258+
public string Description { get; set; }
259+
260+
[Alias("ReportID")]
261+
[AutoIncrement]
262+
[PrimaryKey]
125263
public int Id { get; set; }
126264

127-
[Alias("ReportSectionIdAlias")]
128-
public int ReportSectionId { get; set; }
265+
public string Name { get; set; }
266+
}
267+
268+
[Alias("ReportSections")]
269+
[Schema("MicroSite")]
270+
public class Section : IHasId<int>
271+
{
272+
[Alias("ReportSectionID")]
273+
[AutoIncrement]
274+
[PrimaryKey]
275+
public int Id { get; set; }
129276

130-
[Alias("NameAlias")]
131277
public string Name { get; set; }
132-
[Alias("ReportIdAlias")]
278+
279+
[Alias("ReportID")]
280+
[Required]
133281
public int ReportId { get; set; }
134282
}
283+
135284
}

0 commit comments

Comments
 (0)