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

Commit 19e7c31

Browse files
committed
Add missing Expression overloads for db.Exists<T>, impl currently uses Count > 0
1 parent b56a561 commit 19e7c31

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/ServiceStack.OrmLite/OrmLiteReadConnectionExtensions.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Data;
55
using System.Linq;
6+
using System.Linq.Expressions;
67

78
namespace ServiceStack.OrmLite
89
{
@@ -291,6 +292,32 @@ public static Dictionary<K, V> DictionaryFmt<K, V>(this IDbConnection dbConn, st
291292
return dbConn.Exec(dbCmd => dbCmd.DictionaryFmt<K, V>(sqlFormat, sqlParams));
292293
}
293294

295+
/// <summary>
296+
/// Returns true if the Query returns any records that match the LINQ expression, E.g:
297+
/// <para>db.Exists&lt;Person&gt;(x =&gt; x.Age &lt; 50)</para>
298+
/// </summary>
299+
public static bool Exists<T>(this IDbConnection dbConn, Expression<Func<T, bool>> expression)
300+
{
301+
return dbConn.Exec(dbCmd => dbCmd.Count(expression)) > 0;
302+
}
303+
304+
/// <summary>
305+
/// Returns true if the Query returns any records that match the SqlExpression lambda, E.g:
306+
/// <para>db.Exists&lt;Person&gt;(q =&gt; q.Where(x =&gt; x.Age &lt; 50))</para>
307+
/// </summary>
308+
public static bool Exists<T>(this IDbConnection dbConn, Func<SqlExpression<T>, SqlExpression<T>> expression)
309+
{
310+
return dbConn.Exec(dbCmd => dbCmd.Count(expression)) > 0;
311+
}
312+
313+
/// <summary>
314+
/// Returns true if the Query returns any records that match the supplied SqlExpression, E.g:
315+
/// <para>db.Exists(db.SqlExpression&lt;Person&gt;().Where(x =&gt; x.Age &lt; 50))</para>
316+
/// </summary>
317+
public static bool Exists<T>(this IDbConnection dbConn, SqlExpression<T> expression)
318+
{
319+
return dbConn.Exec(dbCmd => dbCmd.Count(expression)) > 0;
320+
}
294321
/// <summary>
295322
/// Returns true if the Query returns any records, using an SqlFormat query. E.g:
296323
/// <para>db.Exists&lt;Person&gt;(new { Age = 42 })</para>

tests/ServiceStack.OrmLite.Tests/ApiSqlServerTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ public void API_SqlServer_Examples()
163163
db.DictionaryFmt<int, string>("SELECT Id, LastName FROM Person WHERE Age < {0}", 50);
164164
Assert.That(db.GetLastSql(), Is.EqualTo("SELECT Id, LastName FROM Person WHERE Age < 50"));
165165

166+
db.Exists<Person>(x => x.Age < 50);
167+
Assert.That(db.GetLastSql(), Is.EqualTo("SELECT COUNT(*) FROM \"Person\" WHERE (\"Age\" < 50)"));
168+
169+
db.Exists(db.SqlExpression<Person>().Where(x => x.Age < 50));
170+
Assert.That(db.GetLastSql(), Is.EqualTo("SELECT COUNT(*) FROM \"Person\" WHERE (\"Age\" < 50)"));
171+
166172
db.Exists<Person>(new { Age = 42 });
167173
Assert.That(db.GetLastSql(), Is.EqualTo("SELECT \"Id\", \"FirstName\", \"LastName\", \"Age\" FROM \"Person\" WHERE \"Age\" = @Age"));
168174

tests/ServiceStack.OrmLite.Tests/ApiSqliteTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ public void API_Sqlite_Examples()
164164
db.DictionaryFmt<int, string>("SELECT Id, LastName FROM Person WHERE Age < {0}", 50);
165165
Assert.That(db.GetLastSql(), Is.EqualTo("SELECT Id, LastName FROM Person WHERE Age < 50"));
166166

167+
db.Exists<Person>(x => x.Age < 50);
168+
Assert.That(db.GetLastSql(), Is.EqualTo("SELECT COUNT(*) FROM \"Person\" WHERE (\"Age\" < 50)"));
169+
170+
db.Exists(db.SqlExpression<Person>().Where(x => x.Age < 50));
171+
Assert.That(db.GetLastSql(), Is.EqualTo("SELECT COUNT(*) FROM \"Person\" WHERE (\"Age\" < 50)"));
172+
167173
db.Exists<Person>(new { Age = 42 });
168174
Assert.That(db.GetLastSql(), Is.EqualTo("SELECT \"Id\", \"FirstName\", \"LastName\", \"Age\" FROM \"Person\" WHERE \"Age\" = @Age"));
169175

0 commit comments

Comments
 (0)