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 Substring SQL Expression to SQL Server
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Jul 29, 2015
1 parent 33b737a commit 695114e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 16 deletions.
9 changes: 8 additions & 1 deletion src/ServiceStack.OrmLite.SqlServer/SqlServerExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,12 @@ public override string ToUpdateStatement(T item, bool excludeDefaults = false)
return string.Format("UPDATE {0} SET {1} {2}",
base.DialectProvider.GetQuotedTableName(ModelDef), setFields, WhereExpression);
}
}

public override string GetSubstringSql(object quotedColumn, int startIndex, int? length = null)
{
return length != null
? string.Format("substring({0}, {1}, {2})", quotedColumn, startIndex, length.Value)
: string.Format("substring({0}, {1}, LEN({0}) - {1} + 1)", quotedColumn, startIndex );
}
}
}
22 changes: 13 additions & 9 deletions src/ServiceStack.OrmLite/Expressions/SqlExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1644,26 +1644,30 @@ protected virtual object VisitColumnAccessMethod(MethodCallExpression m)
}
break;
case "Substring":
var startIndex = Int32.Parse(args[0].ToString()) + 1;
var startIndex = int.Parse(args[0].ToString()) + 1;
if (args.Count == 2)
{
var length = Int32.Parse(args[1].ToString());
statement = string.Format("substring({0} from {1} for {2})",
quotedColName,
startIndex,
length);
var length = int.Parse(args[1].ToString());
statement = GetSubstringSql(quotedColName, startIndex, length);
}
else
statement = string.Format("substring({0} from {1})",
quotedColName,
startIndex);
{
statement = GetSubstringSql(quotedColName, startIndex);
}
break;
default:
throw new NotSupportedException();
}
return new PartialSqlString(statement);
}

public virtual string GetSubstringSql(object quotedColumn, int startIndex, int? length = null)
{
return length != null
? string.Format("substring({0} from {1} for {2})", quotedColumn, startIndex, length.Value)
: string.Format("substring({0} from {1})", quotedColumn, startIndex);
}

public IDbDataParameter CreateParam(string name,
object value = null,
ParameterDirection direction = ParameterDirection.Input,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Linq;
using NUnit.Framework;
using ServiceStack.OrmLite.Tests.Shared;
using ServiceStack.Text;

namespace ServiceStack.OrmLite.Tests.Expression
{
public class SelectFieldExpressionTests
: OrmLiteTestBase
{
[Test]
public void Can_Select_Substring()
{
using (var db = OpenDbConnection())
{
db.DropAndCreateTable<Person>();
db.InsertAll(Person.Rockstars);

var results = db.Select<Person>(x => x.FirstName.Substring(1, 2) == "im");

results.PrintDump();

var expected = Person.Rockstars.Where(x => x.FirstName.Substring(1, 2) == "im").ToList();

Assert.That(results.Count, Is.EqualTo(expected.Count));
Assert.That(results, Is.EquivalentTo(expected));

results = db.Select<Person>(x => x.FirstName.Substring(1) == "im");
results.PrintDump();

expected = Person.Rockstars.Where(x => x.FirstName.Substring(1) == "im").ToList();
Assert.That(results.Count, Is.EqualTo(expected.Count));
Assert.That(results, Is.EquivalentTo(expected));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
<Compile Include="Expression\FromExpressionTests.cs" />
<Compile Include="Expression\GenericTableExpressions.cs" />
<Compile Include="Expression\MethodExpressionTests.cs" />
<Compile Include="Expression\SelectFieldExpressionTests.cs" />
<Compile Include="Expression\SqlExpressionParamTests.cs" />
<Compile Include="Expression\SqlExpressionTests.cs" />
<Compile Include="Expression\SelectExpressionTests.cs" />
Expand Down
40 changes: 34 additions & 6 deletions tests/ServiceStack.OrmLite.Tests/Shared/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ namespace ServiceStack.OrmLite.Tests.Shared
public class Person
{
public static Person[] Rockstars = new[] {
new Person(1, "Jimi", "Hendrix", 27),
new Person(2, "Janis", "Joplin", 27),
new Person(3, "Jim", "Morrisson", 27),
new Person(4, "Kurt", "Cobain", 27),
new Person(5, "Elvis", "Presley", 42),
new Person(6, "Michael", "Jackson", 50),
new Person(1, "Jimi", "Hendrix", 27),
new Person(2, "Janis", "Joplin", 27),
new Person(3, "Jim", "Morrisson", 27),
new Person(4, "Kurt", "Cobain", 27),
new Person(5, "Elvis", "Presley", 42),
new Person(6, "Michael", "Jackson", 50),
};

public int Id { get; set; }
Expand All @@ -26,6 +26,34 @@ public Person(int id, string firstName, string lastName, int age)
LastName = lastName;
Age = age;
}

protected bool Equals(Person other)
{
return Id == other.Id &&
string.Equals(FirstName, other.FirstName) &&
string.Equals(LastName, other.LastName) &&
Age == other.Age;
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Person)obj);
}

public override int GetHashCode()
{
unchecked
{
var hashCode = Id;
hashCode = (hashCode * 397) ^ (FirstName != null ? FirstName.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (LastName != null ? LastName.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ Age;
return hashCode;
}
}
}

public class PersonWithAutoId
Expand Down

0 comments on commit 695114e

Please sign in to comment.