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

Commit 8b84fbc

Browse files
committed
Add support for selecting and ordering CustomSelect fields
1 parent d01df15 commit 8b84fbc

File tree

5 files changed

+80
-19
lines changed

5 files changed

+80
-19
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ public string SelectInto<TModel>()
297297
{
298298
sbSelect.Append(GetQuotedColumnName(tableDef, tableFieldDef.Name));
299299

300-
if (tableFieldDef.Alias != null)
300+
if (tableFieldDef.RequiresAlias)
301301
sbSelect.Append(" AS ").Append(SqlColumn(fieldDef.Name));
302302
}
303303
else

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ internal virtual SqlExpression<T> Select(string[] fields, bool distinct)
192192
foreach (var fieldDef in tableDef.FieldDefinitionsArray)
193193
{
194194
var qualifiedField = GetQuotedColumnName(tableDef, fieldDef.Name);
195+
if (fieldDef.CustomSelect != null)
196+
qualifiedField += " AS " + fieldDef.Name;
195197

196198
if (sb.Length > 0)
197199
sb.Append(", ");
@@ -200,21 +202,25 @@ internal virtual SqlExpression<T> Select(string[] fields, bool distinct)
200202
fieldsList.Add(fieldDef.Name);
201203
}
202204
}
203-
continue;
204205
}
205-
206-
fieldsList.Add(field); //Could be non-matching referenced property
207-
208-
var match = FirstMatchingField(field);
209-
if (match == null)
210-
continue;
211-
212-
var qualifiedName = GetQuotedColumnName(match.Item1, match.Item2.Name);
213-
214-
if (sb.Length > 0)
215-
sb.Append(", ");
216-
217-
sb.Append(qualifiedName);
206+
else
207+
{
208+
fieldsList.Add(field); //Could be non-matching referenced property
209+
210+
var match = FirstMatchingField(field);
211+
if (match == null)
212+
continue;
213+
214+
var fieldDef = match.Item2;
215+
var qualifiedName = GetQuotedColumnName(match.Item1, fieldDef.Name);
216+
if (fieldDef.CustomSelect != null)
217+
qualifiedName += " AS " + fieldDef.Name;
218+
219+
if (sb.Length > 0)
220+
sb.Append(", ");
221+
222+
sb.Append(qualifiedName);
223+
}
218224
}
219225

220226
UnsafeSelect(StringBuilderCache.ReturnAndFree(sb), distinct:distinct);
@@ -2135,7 +2141,10 @@ protected virtual string GetQuotedColumnName(ModelDefinition tableDef, string me
21352141
tableDef = this.ModelDef;
21362142
}
21372143

2138-
var includePrefix = PrefixFieldWithTableName && fd?.CustomSelect == null && !tableDef.ModelType.IsInterface;
2144+
if (fd?.CustomSelect != null)
2145+
return fd.CustomSelect;
2146+
2147+
var includePrefix = PrefixFieldWithTableName && !tableDef.ModelType.IsInterface;
21392148
return includePrefix
21402149
? DialectProvider.GetQuotedColumnName(tableDef, fieldName)
21412150
: DialectProvider.GetQuotedColumnName(fieldName);

src/ServiceStack.OrmLite/FieldDefinition.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class FieldDefinition
5757
public string CheckConstraint { get; set; }
5858

5959
public bool IsUniqueConstraint { get; set; }
60-
60+
6161
public ForeignKeyConstraint ForeignKey { get; set; }
6262

6363
public GetMemberDelegate GetValueFn { get; set; }
@@ -90,6 +90,8 @@ public string GetQuotedValue(object fromInstance, IOrmLiteDialectProvider dialec
9090

9191
public string CustomSelect { get; set; }
9292

93+
public bool RequiresAlias => Alias != null || CustomSelect != null;
94+
9395
public string BelongToModelName { get; set; }
9496

9597
public bool IsReference { get; set; }

tests/ServiceStack.OrmLite.Tests/AutoQueryTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using NUnit.Framework;
6+
using NUnit.Framework.Internal;
67
using ServiceStack.DataAnnotations;
78
using ServiceStack.Text;
89

@@ -483,7 +484,56 @@ public void Can_query_Rockstars_with_Distinct_Fields()
483484
Assert.That(results.Count(x => x.FirstName == "Michael"), Is.EqualTo(1));
484485
}
485486
}
487+
488+
public class FeatureRequest
489+
{
490+
public int Id { get; set; }
491+
public int Up { get; set; }
492+
public int Down { get; set; }
493+
[CustomSelect("up - down")]
494+
public int Points { get; set; }
495+
}
496+
497+
[Test]
498+
public void Can_select_custom_select_column()
499+
{
500+
using (var db = OpenDbConnection())
501+
{
502+
db.DropAndCreateTable<FeatureRequest>();
486503

504+
3.Times(i => db.Insert(new FeatureRequest {Id = i + 1, Up = (i + 1) * 2, Down = i + 1}));
505+
506+
var q = db.From<FeatureRequest>()
507+
.Select(new[]{ "id", "up", "down", "points" });
508+
509+
var results = db.Select(q);
510+
511+
results.PrintDump();
512+
513+
Assert.That(results.Map(x => x.Points), Is.EqualTo(new[]{ 1, 2, 3 }));
514+
}
515+
}
516+
517+
[Test]
518+
public void Can_order_by_custom_select_column()
519+
{
520+
using (var db = OpenDbConnection())
521+
{
522+
db.DropAndCreateTable<FeatureRequest>();
523+
524+
3.Times(i => db.Insert(new FeatureRequest {Id = i + 1, Up = (i + 1) * 2, Down = i + 1}));
525+
526+
var q = db.From<FeatureRequest>()
527+
.Select(new[]{ "id", "up", "down", "points" })
528+
.OrderByDescending(x => x.Points);
529+
530+
var results = db.Select(q);
531+
532+
results.PrintDump();
533+
534+
Assert.That(results.Map(x => x.Points), Is.EqualTo(new[]{ 3, 2, 1 }));
535+
}
536+
}
487537
}
488538

489539
}

tests/ServiceStack.OrmLite.Tests/CustomSqlExpressionTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ protected override Object GetMemberExpression(MemberExpression m)
125125
return base.GetMemberExpression(m);
126126
}
127127

128-
protected override string GetQuotedColumnName(ModelDefinition tableDef, String memberName)
128+
protected override string GetQuotedColumnName(ModelDefinition tableDef, string memberName)
129129
{
130130
if (useFieldName)
131131
{
@@ -151,7 +151,7 @@ private ModelDefinition GetCurrentTableDef(ModelDefinition tableDef, string memb
151151
var nonInheritedProperties = GetCurrentPropertiesWithoutBase(tableDef);
152152
while (curType != null && !nonInheritedProperties.Contains(memberName))
153153
{
154-
curType = curType.BaseType();
154+
curType = curType.BaseType;
155155
nonInheritedProperties = GetCurrentPropertiesWithoutBase(curType?.GetModelMetadata());
156156
}
157157

0 commit comments

Comments
 (0)