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

Commit

Permalink
Add support for case-insensitive Select(fields)
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Mar 17, 2016
1 parent eb3eb80 commit 234fa52
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/ServiceStack.OrmLite/Expressions/SqlExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public abstract partial class SqlExpression<T> : ISqlExpression, IHasUntypedSqlE
private string groupBy = string.Empty;
private string havingExpression;
private string orderBy = string.Empty;
private string[] onlyFields = null;
private HashSet<string> onlyFields = null;

public List<string> UpdateFields { get; set; }
public List<string> InsertFields { get; set; }
Expand Down Expand Up @@ -72,7 +72,7 @@ protected virtual SqlExpression<T> CopyTo(SqlExpression<T> to)
to.groupBy = groupBy;
to.havingExpression = havingExpression;
to.orderBy = orderBy;
to.onlyFields = onlyFields != null ? (string[])onlyFields.Clone() : null;
to.onlyFields = onlyFields != null ? new HashSet<string>(onlyFields) : null;
to.UpdateFields = UpdateFields;
to.InsertFields = InsertFields;
to.modelDef = modelDef;
Expand Down Expand Up @@ -146,7 +146,7 @@ public virtual SqlExpression<T> Select(string[] fields)
}

UnsafeSelect(sb.ToString());
onlyFields = fields;
onlyFields = new HashSet<string>(fields, StringComparer.OrdinalIgnoreCase);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ public void Can_select_partial_list_of_fields()
}
}

[Test]
public void Can_select_partial_list_of_fields_case_insensitive()
{
using (var db = OpenDbConnection())
{
db.DropAndCreateTable<Person>();
db.InsertAll(Person.Rockstars);

var results = db.Select(db.From<Person>().Select(new[] { "id", "firstname", "age" }));

db.GetLastSql().Print();

Assert.That(results.All(x => x.Id > 0));
Assert.That(results.All(x => x.FirstName != null));
Assert.That(results.All(x => x.LastName == null));
Assert.That(results.Any(x => x.Age > 0));
}
}

[Test]
public void Does_ignore_invalid_fields()
{
Expand Down Expand Up @@ -106,7 +125,7 @@ public void Can_select_fields_from_joined_table()
db.InsertAll(RockstarAlbum.SeedAlbums);

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

var results = db.Select<PersonWithAlbum>(q);
Expand All @@ -121,5 +140,32 @@ public void Can_select_fields_from_joined_table()
Assert.That(results.All(x => x.RockstarAlbumName != null));
}
}

[Test]
public void Can_select_fields_from_joined_table_case_insensitive()
{
using (var db = OpenDbConnection())
{
db.DropAndCreateTable<Person>();
db.InsertAll(Person.Rockstars);
db.DropAndCreateTable<RockstarAlbum>();
db.InsertAll(RockstarAlbum.SeedAlbums);

var q = db.From<Person>()
.Join<RockstarAlbum>((p, a) => p.Id == a.RockstarId)
.Select(new[] { "id", "firstname", "age", "rockstaralbumname" });

var results = db.Select<PersonWithAlbum>(q);

db.GetLastSql().Print();

Assert.That(results.All(x => x.Id > 0));
Assert.That(results.All(x => x.FirstName != null));
Assert.That(results.All(x => x.LastName == null));
Assert.That(results.All(x => x.Age > 0));
Assert.That(results.All(x => x.RockstarId == 0));
Assert.That(results.All(x => x.RockstarAlbumName != null));
}
}
}
}

0 comments on commit 234fa52

Please sign in to comment.