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

Commit 234fa52

Browse files
committed
Add support for case-insensitive Select(fields)
1 parent eb3eb80 commit 234fa52

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public abstract partial class SqlExpression<T> : ISqlExpression, IHasUntypedSqlE
2121
private string groupBy = string.Empty;
2222
private string havingExpression;
2323
private string orderBy = string.Empty;
24-
private string[] onlyFields = null;
24+
private HashSet<string> onlyFields = null;
2525

2626
public List<string> UpdateFields { get; set; }
2727
public List<string> InsertFields { get; set; }
@@ -72,7 +72,7 @@ protected virtual SqlExpression<T> CopyTo(SqlExpression<T> to)
7272
to.groupBy = groupBy;
7373
to.havingExpression = havingExpression;
7474
to.orderBy = orderBy;
75-
to.onlyFields = onlyFields != null ? (string[])onlyFields.Clone() : null;
75+
to.onlyFields = onlyFields != null ? new HashSet<string>(onlyFields) : null;
7676
to.UpdateFields = UpdateFields;
7777
to.InsertFields = InsertFields;
7878
to.modelDef = modelDef;
@@ -146,7 +146,7 @@ public virtual SqlExpression<T> Select(string[] fields)
146146
}
147147

148148
UnsafeSelect(sb.ToString());
149-
onlyFields = fields;
149+
onlyFields = new HashSet<string>(fields, StringComparer.OrdinalIgnoreCase);
150150

151151
return this;
152152
}

tests/ServiceStack.OrmLite.Tests/Expression/SelectFieldExpressionTests.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,25 @@ public void Can_select_partial_list_of_fields()
7676
}
7777
}
7878

79+
[Test]
80+
public void Can_select_partial_list_of_fields_case_insensitive()
81+
{
82+
using (var db = OpenDbConnection())
83+
{
84+
db.DropAndCreateTable<Person>();
85+
db.InsertAll(Person.Rockstars);
86+
87+
var results = db.Select(db.From<Person>().Select(new[] { "id", "firstname", "age" }));
88+
89+
db.GetLastSql().Print();
90+
91+
Assert.That(results.All(x => x.Id > 0));
92+
Assert.That(results.All(x => x.FirstName != null));
93+
Assert.That(results.All(x => x.LastName == null));
94+
Assert.That(results.Any(x => x.Age > 0));
95+
}
96+
}
97+
7998
[Test]
8099
public void Does_ignore_invalid_fields()
81100
{
@@ -106,7 +125,7 @@ public void Can_select_fields_from_joined_table()
106125
db.InsertAll(RockstarAlbum.SeedAlbums);
107126

108127
var q = db.From<Person>()
109-
.Join<RockstarAlbum>((p,a) => p.Id == a.RockstarId)
128+
.Join<RockstarAlbum>((p, a) => p.Id == a.RockstarId)
110129
.Select(new[] { "Id", "FirstName", "Age", "RockstarAlbumName" });
111130

112131
var results = db.Select<PersonWithAlbum>(q);
@@ -121,5 +140,32 @@ public void Can_select_fields_from_joined_table()
121140
Assert.That(results.All(x => x.RockstarAlbumName != null));
122141
}
123142
}
143+
144+
[Test]
145+
public void Can_select_fields_from_joined_table_case_insensitive()
146+
{
147+
using (var db = OpenDbConnection())
148+
{
149+
db.DropAndCreateTable<Person>();
150+
db.InsertAll(Person.Rockstars);
151+
db.DropAndCreateTable<RockstarAlbum>();
152+
db.InsertAll(RockstarAlbum.SeedAlbums);
153+
154+
var q = db.From<Person>()
155+
.Join<RockstarAlbum>((p, a) => p.Id == a.RockstarId)
156+
.Select(new[] { "id", "firstname", "age", "rockstaralbumname" });
157+
158+
var results = db.Select<PersonWithAlbum>(q);
159+
160+
db.GetLastSql().Print();
161+
162+
Assert.That(results.All(x => x.Id > 0));
163+
Assert.That(results.All(x => x.FirstName != null));
164+
Assert.That(results.All(x => x.LastName == null));
165+
Assert.That(results.All(x => x.Age > 0));
166+
Assert.That(results.All(x => x.RockstarId == 0));
167+
Assert.That(results.All(x => x.RockstarAlbumName != null));
168+
}
169+
}
124170
}
125171
}

0 commit comments

Comments
 (0)