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

Commit da6e430

Browse files
committed
Add API for returning Count of all Rows
1 parent d329d33 commit da6e430

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/ServiceStack.OrmLite/Expressions/ReadConnectionExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,21 @@ public static long Count<T>(this IDbConnection dbConn)
218218
return dbConn.Exec(dbCmd => dbCmd.Count(expression));
219219
}
220220

221+
/// <summary>
222+
/// Return the number of rows returned by the supplied expression
223+
/// </summary>
224+
public static long RowCount<T>(this IDbConnection dbConn, SqlExpression<T> expression)
225+
{
226+
return dbConn.Exec(dbCmd => dbCmd.RowCount(expression));
227+
}
228+
229+
/// <summary>
230+
/// Return the number of rows returned by the supplied sql
231+
/// </summary>
232+
public static long RowCount(this IDbConnection dbConn, string sql)
233+
{
234+
return dbConn.Exec(dbCmd => dbCmd.RowCount(sql));
235+
}
221236

222237
/// <summary>
223238
/// Returns results with references from using a LINQ Expression. E.g:

src/ServiceStack.OrmLite/Expressions/ReadExtensions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,25 @@ internal static long GetCount(this IDbCommand dbCmd, string sql)
121121
return dbCmd.Column<long>(sql).Sum();
122122
}
123123

124+
internal static long RowCount<T>(this IDbCommand dbCmd, SqlExpression<T> expression)
125+
{
126+
var sql = expression.Clone().Select("1").ToSelectStatement();
127+
return RowCount(dbCmd, sql);
128+
}
129+
130+
internal static long RowCount(this IDbCommand dbCmd, string sql)
131+
{
132+
var rowCount = 0;
133+
using (var reader = dbCmd.ExecReader(sql))
134+
{
135+
while (reader.Read())
136+
{
137+
rowCount++;
138+
}
139+
}
140+
return rowCount;
141+
}
142+
124143
internal static List<T> LoadSelect<T>(this IDbCommand dbCmd, Func<SqlExpression<T>, SqlExpression<T>> expression)
125144
{
126145
var expr = OrmLiteConfig.DialectProvider.SqlExpression<T>();

0 commit comments

Comments
 (0)