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

Commit 1ab3479

Browse files
committed
Fix RowCountAsync impls
1 parent 163d1b4 commit 1ab3479

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/ServiceStack.OrmLite/Async/ReadExpressionCommandExtensionsAsync.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,16 @@ internal static Task<long> GetCountAsync(this IDbCommand dbCmd, string sql, IEnu
161161

162162
internal static Task<long> RowCountAsync<T>(this IDbCommand dbCmd, SqlExpression<T> expression, CancellationToken token)
163163
{
164-
var sql = $"SELECT COUNT(*) FROM ({expression.ToSelectStatement()}) AS COUNT";
165-
return dbCmd.ScalarAsync<long>(sql, token);
164+
var countExpr = expression.Clone().OrderBy();
165+
return dbCmd.ScalarAsync<long>(dbCmd.GetDialectProvider().ToRowCountStatement(countExpr.ToSelectStatement()), expression.Params, token);
166166
}
167167

168-
internal static Task<long> RowCountAsync(this IDbCommand dbCmd, string sql, CancellationToken token)
168+
internal static Task<long> RowCountAsync(this IDbCommand dbCmd, string sql, object anonType, CancellationToken token)
169169
{
170-
return dbCmd.ScalarAsync<long>($"SELECT COUNT(*) FROM ({sql}) AS COUNT", token);
170+
if (anonType != null)
171+
dbCmd.SetParameters(anonType.ToObjectDictionary(), excludeDefaults: false, sql:ref sql);
172+
173+
return dbCmd.ScalarAsync<long>(dbCmd.GetDialectProvider().ToRowCountStatement(sql), token);
171174
}
172175

173176
internal static Task<List<T>> LoadSelectAsync<T>(this IDbCommand dbCmd, SqlExpression<T> expression = null, string[] include = null, CancellationToken token = default(CancellationToken))

src/ServiceStack.OrmLite/OrmLiteReadExpressionsApiAsync.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ public static Task<TKey> ScalarAsync<T, TKey>(this IDbConnection dbConn,
151151
/// <summary>
152152
/// Return the number of rows returned by the supplied sql
153153
/// </summary>
154-
public static Task<long> RowCountAsync(this IDbConnection dbConn, string sql, CancellationToken token = default(CancellationToken))
154+
public static Task<long> RowCountAsync(this IDbConnection dbConn, string sql, object anonType = null, CancellationToken token = default(CancellationToken))
155155
{
156-
return dbConn.Exec(dbCmd => dbCmd.RowCountAsync(sql, token));
156+
return dbConn.Exec(dbCmd => dbCmd.RowCountAsync(sql, anonType, token));
157157
}
158158

159159
/// <summary>

tests/ServiceStack.OrmLite.Tests/Issues/LRAIssues.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Data;
44
using System.Linq;
5+
using System.Threading.Tasks;
56
using NUnit.Framework;
67
using ServiceStack.DataAnnotations;
78
using ServiceStack.Model;
@@ -113,6 +114,20 @@ public void Does_select_aliases_on_custom_expressions()
113114
Assert.That(lastStatement, Does.Contain("as statowarning"));
114115
}
115116
}
117+
118+
[Test]
119+
public async Task Does_RowCount_LRARichiesta_Async()
120+
{
121+
using (var db = OpenDbConnection())
122+
{
123+
db.DropAndCreateTable<LRARichiesta>();
124+
125+
var q = db.From<LRARichiesta>()
126+
.Where(x => x.Id > 0);
127+
128+
var result = await db.RowCountAsync(q);
129+
}
130+
}
116131

117132
private static void CreateTables(IDbConnection db)
118133
{

0 commit comments

Comments
 (0)