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

Commit 2ea3893

Browse files
committed
Added support for NonQuery to get affected rows
- Added support for free text sql statements with optional params which allows the called to get the number of affected rows. This should be usable across all databases since it's using the standard ExecuteNonQuery from IDbCommand. - Added unit tests for the functionality
1 parent b526862 commit 2ea3893

File tree

4 files changed

+463
-2
lines changed

4 files changed

+463
-2
lines changed

src/ServiceStack.OrmLite/OrmLiteReadConnectionExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,21 @@ public static List<T> Query<T>(this IDbConnection dbConn, string sql, Dictionary
160160
return dbConn.Exec(dbCmd => dbCmd.Query<T>(sql, dict));
161161
}
162162

163+
public static int NonQuery(this IDbConnection dbConn, string sql)
164+
{
165+
return dbConn.Exec(dbCmd => dbCmd.NonQuery(sql));
166+
}
167+
168+
public static int NonQuery(this IDbConnection dbConn, string sql, object anonType)
169+
{
170+
return dbConn.Exec(dbCmd => dbCmd.NonQuery(sql, anonType));
171+
}
172+
173+
public static int NonQuery(this IDbConnection dbConn, string sql, Dictionary<string, object> dict)
174+
{
175+
return dbConn.Exec(dbCmd => dbCmd.NonQuery(sql, dict));
176+
}
177+
163178
public static T QueryScalar<T>(this IDbConnection dbConn, object anonType)
164179
{
165180
return dbConn.Exec(dbCmd => dbCmd.QueryScalar<T>(anonType));

src/ServiceStack.OrmLite/OrmLiteReadExtensions.cs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,36 @@ private static void SetParameters<T>(this IDbCommand dbCmd, object anonType, boo
253253
p.Value = value ?? DBNull.Value;
254254
dbCmd.Parameters.Add(p);
255255
}
256-
}
256+
}
257+
258+
private static void SetParameters(this IDbCommand dbCmd, object anonType, bool excludeNulls)
259+
{
260+
dbCmd.Parameters.Clear();
261+
lastQueryType = null;
262+
if (anonType == null)
263+
return;
264+
265+
var pis = anonType.GetType().GetSerializableProperties();
266+
267+
foreach (var pi in pis)
268+
{
269+
var mi = pi.GetGetMethod();
270+
if (mi == null)
271+
continue;
272+
273+
var value = mi.Invoke(anonType, new object[0]);
274+
if (excludeNulls && value == null)
275+
continue;
276+
277+
var p = dbCmd.CreateParameter();
278+
279+
p.ParameterName = pi.Name;
280+
p.DbType = OrmLiteConfig.DialectProvider.GetColumnDbType(pi.PropertyType);
281+
p.Direction = ParameterDirection.Input;
282+
p.Value = value ?? DBNull.Value;
283+
dbCmd.Parameters.Add(p);
284+
}
285+
}
257286

258287
private static void SetParameters(this IDbCommand dbCmd, Dictionary<string,object> dict, bool excludeNulls)
259288
{
@@ -393,7 +422,25 @@ internal static List<T> Query<T>(this IDbCommand dbCmd, string sql, Dictionary<s
393422
return IsScalar<T>()
394423
? dbReader.GetFirstColumn<T>()
395424
: dbReader.ConvertToList<T>();
396-
}
425+
}
426+
427+
internal static int NonQuery(this IDbCommand dbCmd, string sql, object anonType = null)
428+
{
429+
if (anonType != null)
430+
dbCmd.SetParameters(anonType, excludeNulls: false);
431+
dbCmd.CommandText = sql;
432+
433+
return dbCmd.ExecuteNonQuery();
434+
}
435+
436+
internal static int NonQuery(this IDbCommand dbCmd, string sql, Dictionary<string, object> dict)
437+
{
438+
if (dict != null)
439+
dbCmd.SetParameters(dict, excludeNulls: false);
440+
dbCmd.CommandText = sql;
441+
442+
return dbCmd.ExecuteNonQuery();
443+
}
397444

398445
internal static T QueryScalar<T>(this IDbCommand dbCmd, object anonType)
399446
{

0 commit comments

Comments
 (0)