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

Commit 300fbd8

Browse files
committed
Merge remote-tracking branch 'upstream/master' into ContainsNullableSupport
2 parents b38379c + 1649afb commit 300fbd8

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

src/ServiceStack.OrmLite/Expressions/ReadExpressionCommandExtensions.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,22 @@ internal static long RowCount<T>(this IDbCommand dbCmd, SqlExpression<T> express
154154
{
155155
//ORDER BY throws when used in subselects in SQL Server. Removing OrderBy() clause since it doesn't impact results
156156
var countExpr = expression.Clone().OrderBy();
157-
return dbCmd.Scalar<long>(dbCmd.GetDialectProvider().ToRowCountStatement(countExpr.ToSelectStatement()));
157+
return dbCmd.Scalar<long>(dbCmd.GetDialectProvider().ToRowCountStatement(countExpr.ToSelectStatement()), expression.Params);
158158
}
159159

160-
internal static long RowCount(this IDbCommand dbCmd, string sql)
160+
internal static long RowCount(this IDbCommand dbCmd, string sql, object anonType)
161161
{
162+
if (anonType != null)
163+
dbCmd.SetParameters(anonType.ToObjectDictionary(), excludeDefaults: false);
164+
162165
return dbCmd.Scalar<long>(dbCmd.GetDialectProvider().ToRowCountStatement(sql));
163166
}
164167

168+
internal static long RowCount(this IDbCommand dbCmd, string sql, IEnumerable<IDbDataParameter> sqlParams)
169+
{
170+
return dbCmd.SetParameters(sqlParams).Scalar<long>(dbCmd.GetDialectProvider().ToRowCountStatement(sql));
171+
}
172+
165173
internal static List<T> LoadSelect<T>(this IDbCommand dbCmd, SqlExpression<T> expression = null, IEnumerable<string> include = null)
166174
{
167175
return dbCmd.LoadListWithReferences<T, T>(expression, include);

src/ServiceStack.OrmLite/OrmLiteReadExpressionsApi.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,17 @@ public static long RowCount<T>(this IDbConnection dbConn, SqlExpression<T> expre
238238
/// <summary>
239239
/// Return the number of rows returned by the supplied sql
240240
/// </summary>
241-
public static long RowCount(this IDbConnection dbConn, string sql)
241+
public static long RowCount(this IDbConnection dbConn, string sql, object anonType = null)
242242
{
243-
return dbConn.Exec(dbCmd => dbCmd.RowCount(sql));
243+
return dbConn.Exec(dbCmd => dbCmd.RowCount(sql, anonType));
244+
}
245+
246+
/// <summary>
247+
/// Return the number of rows returned by the supplied sql and db params
248+
/// </summary>
249+
public static long RowCount(this IDbConnection dbConn, string sql, IEnumerable<IDbDataParameter> sqlParams)
250+
{
251+
return dbConn.Exec(dbCmd => dbCmd.RowCount(sql, sqlParams));
244252
}
245253

246254
/// <summary>

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,40 @@ public void Can_do_ToCountStatement_with_SqlExpression_if_expression_has_groupby
371371
}
372372
}
373373

374+
[Test]
375+
public void Can_select_RowCount_with_db_params()
376+
{
377+
using (var db = OpenDbConnection())
378+
{
379+
db.DropAndCreateTable<LetterFrequency>();
380+
381+
db.Insert(new LetterFrequency { Letter = "A" });
382+
db.Insert(new LetterFrequency { Letter = "A" });
383+
db.Insert(new LetterFrequency { Letter = "A" });
384+
db.Insert(new LetterFrequency { Letter = "B" });
385+
db.Insert(new LetterFrequency { Letter = "B" });
386+
db.Insert(new LetterFrequency { Letter = "B" });
387+
db.Insert(new LetterFrequency { Letter = "B" });
388+
389+
var query = db.From<LetterFrequency>()
390+
.Where(x => x.Letter == "B")
391+
.Select(x => x.Letter);
392+
393+
var rowCount = db.RowCount(query);
394+
db.GetLastSql().Print();
395+
Assert.That(rowCount, Is.EqualTo(4));
396+
397+
var table = typeof(LetterFrequency).Name.SqlTable();
398+
399+
rowCount = db.RowCount("SELECT * FROM {0} WHERE Letter = @p1".Fmt(table), new { p1 = "B" });
400+
Assert.That(rowCount, Is.EqualTo(4));
401+
402+
rowCount = db.RowCount("SELECT * FROM {0} WHERE Letter = @p1".Fmt(table),
403+
new[] { db.CreateParam("p1", "B") });
404+
Assert.That(rowCount, Is.EqualTo(4));
405+
}
406+
}
407+
374408
[Test]
375409
public void Can_get_RowCount_if_expression_has_OrderBy()
376410
{

0 commit comments

Comments
 (0)