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

Commit 63d3619

Browse files
committed
pass in sql expression params in async parameterized queries
1 parent 55083b8 commit 63d3619

File tree

5 files changed

+53
-53
lines changed

5 files changed

+53
-53
lines changed

src/ServiceStack.OrmLite/Async/OrmLiteWriteCommandExtensionsAsync.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ internal static class OrmLiteWriteCommandExtensionsAsync
1818
{
1919
private static readonly ILog Log = LogManager.GetLogger(typeof(OrmLiteWriteCommandExtensionsAsync));
2020

21+
internal static Task<int> ExecuteSqlAsync(this IDbCommand dbCmd, string sql, IEnumerable<IDbDataParameter> sqlParams, CancellationToken token)
22+
{
23+
return dbCmd.SetParameters(sqlParams).ExecuteSqlAsync(sql, token);
24+
}
25+
2126
internal static Task<int> ExecuteSqlAsync(this IDbCommand dbCmd, string sql, CancellationToken token)
2227
{
2328
dbCmd.CommandText = sql;

src/ServiceStack.OrmLite/Async/ReadExpressionCommandExtensionsAsync.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,52 +70,52 @@ internal static Task<T> SingleAsync<T>(this IDbCommand dbCmd, SqlExpression<T> e
7070

7171
public static Task<TKey> ScalarAsync<T, TKey>(this IDbCommand dbCmd, Expression<Func<T, TKey>> field, CancellationToken token)
7272
{
73-
var ev = dbCmd.GetDialectProvider().SqlExpression<T>();
74-
ev.Select(field);
75-
var sql = ev.SelectInto<T>();
76-
return dbCmd.ScalarAsync<TKey>(sql, token);
73+
var q = dbCmd.GetDialectProvider().SqlExpression<T>();
74+
q.Select(field);
75+
var sql = q.SelectInto<T>();
76+
return dbCmd.ScalarAsync<TKey>(sql, q.Params, token);
7777
}
7878

7979
internal static Task<TKey> ScalarAsync<T, TKey>(this IDbCommand dbCmd,
8080
Expression<Func<T, TKey>> field, Expression<Func<T, bool>> predicate, CancellationToken token)
8181
{
82-
var ev = dbCmd.GetDialectProvider().SqlExpression<T>();
83-
ev.Select(field).Where(predicate);
84-
string sql = ev.SelectInto<T>();
85-
return dbCmd.ScalarAsync<TKey>(sql, token);
82+
var q = dbCmd.GetDialectProvider().SqlExpression<T>();
83+
q.Select(field).Where(predicate);
84+
string sql = q.SelectInto<T>();
85+
return dbCmd.ScalarAsync<TKey>(sql, q.Params, token);
8686
}
8787

8888
internal static Task<long> CountAsync<T>(this IDbCommand dbCmd, CancellationToken token)
8989
{
90-
var expression = dbCmd.GetDialectProvider().SqlExpression<T>();
91-
var sql = expression.ToCountStatement();
92-
return GetCountAsync(dbCmd, sql, token);
90+
var q = dbCmd.GetDialectProvider().SqlExpression<T>();
91+
var sql = q.ToCountStatement();
92+
return GetCountAsync(dbCmd, sql, q.Params, token);
9393
}
9494

9595
internal static Task<long> CountAsync<T>(this IDbCommand dbCmd, Func<SqlExpression<T>, SqlExpression<T>> expression, CancellationToken token)
9696
{
97-
var expr = dbCmd.GetDialectProvider().SqlExpression<T>();
98-
var sql = expression(expr).ToCountStatement();
99-
return GetCountAsync(dbCmd, sql, token);
97+
var q = dbCmd.GetDialectProvider().SqlExpression<T>();
98+
var sql = expression(q).ToCountStatement();
99+
return GetCountAsync(dbCmd, sql, q.Params, token);
100100
}
101101

102-
internal static Task<long> CountAsync<T>(this IDbCommand dbCmd, SqlExpression<T> expression, CancellationToken token)
102+
internal static Task<long> CountAsync<T>(this IDbCommand dbCmd, SqlExpression<T> q, CancellationToken token)
103103
{
104-
var sql = expression.ToCountStatement();
105-
return GetCountAsync(dbCmd, sql, token);
104+
var sql = q.ToCountStatement();
105+
return GetCountAsync(dbCmd, sql, q.Params, token);
106106
}
107107

108108
internal static Task<long> CountAsync<T>(this IDbCommand dbCmd, Expression<Func<T, bool>> predicate, CancellationToken token)
109109
{
110-
var ev = dbCmd.GetDialectProvider().SqlExpression<T>();
111-
ev.Where(predicate);
112-
var sql = ev.ToCountStatement();
113-
return GetCountAsync(dbCmd, sql, token);
110+
var q = dbCmd.GetDialectProvider().SqlExpression<T>();
111+
q.Where(predicate);
112+
var sql = q.ToCountStatement();
113+
return GetCountAsync(dbCmd, sql, q.Params, token);
114114
}
115115

116-
internal static Task<long> GetCountAsync(this IDbCommand dbCmd, string sql, CancellationToken token)
116+
internal static Task<long> GetCountAsync(this IDbCommand dbCmd, string sql, IEnumerable<IDbDataParameter> sqlParams, CancellationToken token)
117117
{
118-
return dbCmd.ColumnAsync<long>(sql, token).Then(x => x.Sum());
118+
return dbCmd.ColumnAsync<long>(sql, sqlParams, token).Then(x => x.Sum());
119119
}
120120

121121
internal static Task<long> RowCountAsync<T>(this IDbCommand dbCmd, SqlExpression<T> expression, CancellationToken token)

src/ServiceStack.OrmLite/Async/WriteExpressionCommandExtensionsAsync.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal static Task<int> UpdateOnlyAsync<T>(this IDbCommand dbCmd, T model, Fun
1717
internal static Task<int> UpdateOnlyAsync<T>(this IDbCommand dbCmd, T model, SqlExpression<T> onlyFields, CancellationToken token)
1818
{
1919
var sql = dbCmd.UpdateOnlySql(model, onlyFields);
20-
return dbCmd.ExecuteSqlAsync(sql, token);
20+
return dbCmd.ExecuteSqlAsync(sql, onlyFields.Params, token);
2121
}
2222

2323
internal static Task<int> UpdateOnlyAsync<T, TKey>(this IDbCommand dbCmd, T obj,
@@ -42,7 +42,7 @@ internal static Task<int> UpdateNonDefaultsAsync<T>(this IDbCommand dbCmd, T ite
4242
var q = dbCmd.GetDialectProvider().SqlExpression<T>();
4343
q.Where(obj);
4444
var sql = q.ToUpdateStatement(item, excludeDefaults: true);
45-
return dbCmd.ExecuteSqlAsync(sql, token);
45+
return dbCmd.ExecuteSqlAsync(sql, q.Params, token);
4646
}
4747

4848
internal static Task<int> UpdateAsync<T>(this IDbCommand dbCmd, T item, Expression<Func<T, bool>> expression, CancellationToken token)
@@ -53,13 +53,16 @@ internal static Task<int> UpdateAsync<T>(this IDbCommand dbCmd, T item, Expressi
5353
var q = dbCmd.GetDialectProvider().SqlExpression<T>();
5454
q.Where(expression);
5555
var sql = q.ToUpdateStatement(item);
56-
return dbCmd.ExecuteSqlAsync(sql, token);
56+
return dbCmd.ExecuteSqlAsync(sql, q.Params, token);
5757
}
5858

5959
internal static Task<int> UpdateAsync<T>(this IDbCommand dbCmd, object updateOnly, Expression<Func<T, bool>> where, CancellationToken token)
6060
{
61-
var updateSql = WriteExpressionCommandExtensions.UpdateSql(dbCmd.GetDialectProvider(), updateOnly, where);
62-
return dbCmd.ExecuteSqlAsync(updateSql, token);
61+
var q = dbCmd.GetDialectProvider().SqlExpression<T>();
62+
var whereSql = q.Where(@where).WhereExpression;
63+
var updateSql = WriteExpressionCommandExtensions.UpdateSql<T>(dbCmd.GetDialectProvider(), updateOnly, whereSql);
64+
65+
return dbCmd.ExecuteSqlAsync(updateSql, q.Params, token);
6366
}
6467

6568
internal static Task<int> UpdateFmtAsync<T>(this IDbCommand dbCmd, string set, string where, CancellationToken token)
@@ -89,20 +92,20 @@ internal static Task InsertOnlyAsync<T>(this IDbCommand dbCmd, T obj, SqlExpress
8992

9093
internal static Task<int> DeleteAsync<T>(this IDbCommand dbCmd, Expression<Func<T, bool>> where, CancellationToken token)
9194
{
92-
var ev = dbCmd.GetDialectProvider().SqlExpression<T>();
93-
ev.Where(where);
94-
return dbCmd.DeleteAsync(ev, token);
95+
var q = dbCmd.GetDialectProvider().SqlExpression<T>();
96+
q.Where(where);
97+
return dbCmd.DeleteAsync(q, token);
9598
}
9699

97100
internal static Task<int> DeleteAsync<T>(this IDbCommand dbCmd, Func<SqlExpression<T>, SqlExpression<T>> where, CancellationToken token)
98101
{
99102
return dbCmd.DeleteAsync(where(dbCmd.GetDialectProvider().SqlExpression<T>()), token);
100103
}
101104

102-
internal static Task<int> DeleteAsync<T>(this IDbCommand dbCmd, SqlExpression<T> where, CancellationToken token)
105+
internal static Task<int> DeleteAsync<T>(this IDbCommand dbCmd, SqlExpression<T> q, CancellationToken token)
103106
{
104-
var sql = where.ToDeleteRowStatement();
105-
return dbCmd.ExecuteSqlAsync(sql, token);
107+
var sql = q.ToDeleteRowStatement();
108+
return dbCmd.ExecuteSqlAsync(sql, q.Params, token);
106109
}
107110

108111
internal static Task<int> DeleteFmtAsync<T>(this IDbCommand dbCmd, string where, CancellationToken token)

src/ServiceStack.OrmLite/Expressions/ReadExpressionCommandExtensions.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,19 @@ internal static T Single<T>(this IDbCommand dbCmd, SqlExpression<T> expression)
6868

6969
public static TKey Scalar<T, TKey>(this IDbCommand dbCmd, Expression<Func<T, TKey>> field)
7070
{
71-
var ev = dbCmd.GetDialectProvider().SqlExpression<T>();
72-
ev.Select(field);
73-
var sql = ev.SelectInto<T>();
74-
return dbCmd.Scalar<TKey>(sql);
71+
var q = dbCmd.GetDialectProvider().SqlExpression<T>();
72+
q.Select(field);
73+
var sql = q.SelectInto<T>();
74+
return dbCmd.Scalar<TKey>(sql, q.Params);
7575
}
7676

7777
internal static TKey Scalar<T, TKey>(this IDbCommand dbCmd,
7878
Expression<Func<T, TKey>> field, Expression<Func<T, bool>> predicate)
7979
{
80-
var ev = dbCmd.GetDialectProvider().SqlExpression<T>();
81-
ev.Select(field).Where(predicate);
82-
string sql = ev.SelectInto<T>();
83-
return dbCmd.Scalar<TKey>(sql, ev.Params);
80+
var q = dbCmd.GetDialectProvider().SqlExpression<T>();
81+
q.Select(field).Where(predicate);
82+
string sql = q.SelectInto<T>();
83+
return dbCmd.Scalar<TKey>(sql, q.Params);
8484
}
8585

8686
internal static long Count<T>(this IDbCommand dbCmd)

src/ServiceStack.OrmLite/Expressions/WriteExpressionCommandExtensions.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ public static int Update<T>(this IDbCommand dbCmd, T item, Expression<Func<T, bo
7272

7373
public static int Update<T>(this IDbCommand dbCmd, object updateOnly, Expression<Func<T, bool>> where = null)
7474
{
75-
var ev = dbCmd.GetDialectProvider().SqlExpression<T>();
76-
var whereSql = ev.Where(where).WhereExpression;
75+
var q = dbCmd.GetDialectProvider().SqlExpression<T>();
76+
var whereSql = q.Where(where).WhereExpression;
7777
var updateSql = UpdateSql<T>(dbCmd.GetDialectProvider(), updateOnly, whereSql);
7878

79-
return dbCmd.ExecuteSql(updateSql, ev.Params);
79+
return dbCmd.ExecuteSql(updateSql, q.Params);
8080
}
8181

8282
internal static string UpdateSql<T>(IOrmLiteDialectProvider dialectProvider, object updateOnly, string whereSql)
@@ -104,14 +104,6 @@ internal static string UpdateSql<T>(IOrmLiteDialectProvider dialectProvider, obj
104104
return updateSql;
105105
}
106106

107-
internal static string UpdateSql<T>(IOrmLiteDialectProvider dialectProvider, object updateOnly, Expression<Func<T, bool>> @where)
108-
{
109-
var ev = dialectProvider.SqlExpression<T>();
110-
var whereSql = ev.Where(@where).WhereExpression;
111-
112-
return UpdateSql<T>(dialectProvider, updateOnly, whereSql);
113-
}
114-
115107
public static int UpdateFmt<T>(this IDbCommand dbCmd, string set = null, string where = null)
116108
{
117109
return dbCmd.UpdateFmt(typeof(T).GetModelDefinition().ModelName, set, where);

0 commit comments

Comments
 (0)