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

Commit c223df4

Browse files
committed
Add support for wildcards in Select(fields) API
1 parent f70b658 commit c223df4

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ public string SelectInto<TModel>()
161161
}
162162

163163
var sbSelect = new StringBuilder();
164-
var selectDef = typeof(TModel).GetModelDefinition();
165164

165+
var selectDef = typeof(TModel).GetModelDefinition();
166166
var orderedDefs = tableDefs;
167167
if (selectDef != modelDef && tableDefs.Contains(selectDef))
168168
{

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,38 @@ public virtual SqlExpression<T> Select(string[] fields)
130130
if (fields == null || fields.Length == 0)
131131
return Select(string.Empty);
132132

133+
var allTableDefs = new List<ModelDefinition> { modelDef };
134+
allTableDefs.AddRange(tableDefs);
135+
136+
var fieldsList = new List<string>();
133137
var sb = new StringBuilder();
134138
foreach (var field in fields)
135139
{
140+
if (string.IsNullOrEmpty(field))
141+
continue;
142+
143+
if (field.EndsWith(".*"))
144+
{
145+
var tableName = field.Substring(0, field.Length - 2);
146+
var tableDef = allTableDefs.FirstOrDefault(x => string.Equals(x.Name, tableName, StringComparison.OrdinalIgnoreCase));
147+
if (tableDef != null)
148+
{
149+
foreach (var fieldDef in tableDef.FieldDefinitionsArray)
150+
{
151+
var qualifiedField = DialectProvider.GetQuotedColumnName(tableDef, fieldDef);
152+
153+
if (sb.Length > 0)
154+
sb.Append(", ");
155+
156+
sb.Append(qualifiedField);
157+
fieldsList.Add(fieldDef.Name);
158+
}
159+
}
160+
continue;
161+
}
162+
163+
fieldsList.Add(field); //Could be non-matching referenced property
164+
136165
var match = FirstMatchingField(field);
137166
if (match == null)
138167
continue;
@@ -146,7 +175,7 @@ public virtual SqlExpression<T> Select(string[] fields)
146175
}
147176

148177
UnsafeSelect(sb.ToString());
149-
OnlyFields = new HashSet<string>(fields, StringComparer.OrdinalIgnoreCase);
178+
OnlyFields = new HashSet<string>(fieldsList, StringComparer.OrdinalIgnoreCase);
150179

151180
return this;
152181
}

tests/ServiceStack.OrmLite.Tests/AutoQueryTests.cs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class Employee
5959
[References(typeof(Department))]
6060
public int DepartmentId { get; set; }
6161

62-
[DataAnnotations.Ignore]
62+
[Reference]
6363
public Department Department { get; set; }
6464
}
6565

@@ -227,6 +227,50 @@ public void Does_only_populate_Select_fields()
227227
}
228228
}
229229

230+
[Test]
231+
public void Does_only_populate_Select_fields_wildcard()
232+
{
233+
using (var db = OpenDbConnection())
234+
{
235+
db.DropTable<Employee>();
236+
db.DropTable<Department>();
237+
db.CreateTable<Department>();
238+
db.CreateTable<Employee>();
239+
240+
db.InsertAll(SeedDepartments);
241+
db.InsertAll(SeedEmployees);
242+
243+
var q = db.From<Employee>()
244+
.Join<Department>()
245+
.Select(new[] { "departmentid", "employee.*" });
246+
247+
Assert.That(q.OnlyFields, Is.EquivalentTo(new[] {
248+
"departmentid", "Id", "FirstName", "LastName"
249+
}));
250+
251+
var results = db.Select(q);
252+
Assert.That(results.All(x => x.Id >= 0));
253+
Assert.That(results.All(x => x.DepartmentId >= 10));
254+
Assert.That(results.All(x => x.FirstName != null));
255+
Assert.That(results.All(x => x.LastName != null));
256+
257+
q = db.From<Employee>()
258+
.Join<Department>()
259+
.Select(new[] { "departmentid", "department", "department.*" });
260+
261+
Assert.That(q.OnlyFields, Is.EquivalentTo(new[] {
262+
"departmentid", "department", "Id", "Name"
263+
}));
264+
265+
results = db.LoadSelect(q, include:q.OnlyFields);
266+
267+
Assert.That(results.All(x => x.Id > 0 && x.Id < 10));
268+
Assert.That(results.All(x => x.DepartmentId >= 10));
269+
Assert.That(results.All(x => x.Department.Id >= 10));
270+
Assert.That(results.All(x => x.Department.Name != null));
271+
}
272+
}
273+
230274
[Test]
231275
public void Does_only_populate_LoadSelect_fields()
232276
{

0 commit comments

Comments
 (0)