Permalink
Browse files

Add overloads for UpdateOnly accepting SQL whereExpression + db params

  • Loading branch information...
mythz committed Dec 3, 2018
1 parent 4c92f48 commit a92aebff07d08a7dfad562ab48bb9a7512538813
@@ -58,6 +58,18 @@ internal static Task<int> UpdateOnlyAsync<T>(this IDbCommand dbCmd, T model, Sql
return cmd.ExecNonQueryAsync(token);
}

internal static Task<int> UpdateOnlyAsync<T>(this IDbCommand dbCmd,
Expression<Func<T>> updateFields,
string whereExpression,
IEnumerable<IDbDataParameter> sqlParams,
Action<IDbCommand> commandFilter,
CancellationToken token)
{
var cmd = dbCmd.InitUpdateOnly(updateFields, whereExpression, sqlParams);
commandFilter?.Invoke(cmd);
return cmd.ExecNonQueryAsync(token);
}

public static Task<int> UpdateAddAsync<T>(this IDbCommand dbCmd,
Expression<Func<T>> updateFields,
SqlExpression<T> q,
@@ -88,6 +88,32 @@ internal static IDbCommand InitUpdateOnly<T>(this IDbCommand dbCmd, Expression<F

return dbCmd;
}

internal static int UpdateOnly<T>(this IDbCommand dbCmd,
Expression<Func<T>> updateFields,
string whereExpression,
IEnumerable<IDbDataParameter> dbParams,
Action<IDbCommand> commandFilter = null)
{
var cmd = dbCmd.InitUpdateOnly(updateFields, whereExpression, dbParams);
commandFilter?.Invoke(cmd);
return cmd.ExecNonQuery();
}

internal static IDbCommand InitUpdateOnly<T>(this IDbCommand dbCmd, Expression<Func<T>> updateFields, string whereExpression, IEnumerable<IDbDataParameter> sqlParams)
{
if (updateFields == null)
throw new ArgumentNullException(nameof(updateFields));

OrmLiteConfig.UpdateFilter?.Invoke(dbCmd, updateFields.EvalFactoryFn());

dbCmd.SetParameters(sqlParams);

var updateFieldValues = updateFields.AssignedValues();
dbCmd.GetDialectProvider().PrepareUpdateRowStatement<T>(dbCmd, updateFieldValues, whereExpression);

return dbCmd;
}

public static int UpdateAdd<T>(this IDbCommand dbCmd,
Expression<Func<T>> updateFields,
@@ -55,6 +55,22 @@ public static int UpdateOnly<T>(this IDbConnection dbConn, T model, SqlExpressio
return dbConn.Exec(dbCmd => dbCmd.UpdateOnly(updateFields, q, commandFilter));
}

/// <summary>
/// Update only fields in the specified expression that matches the where condition (if any), E.g:
///
/// var q = db.From&gt;Person&lt;().Where(p => p.LastName == "Hendrix");
/// db.UpdateOnly(() => new Person { FirstName = "JJ" }, q.WhereExpression, q.Params);
/// UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("LastName" = 'Hendrix')
/// </summary>
public static int UpdateOnly<T>(this IDbConnection dbConn,
Expression<Func<T>> updateFields,
string whereExpression,
IEnumerable<IDbDataParameter> sqlParams,
Action<IDbCommand> commandFilter = null)
{
return dbConn.Exec(dbCmd => dbCmd.UpdateOnly(updateFields, whereExpression, sqlParams, commandFilter));
}

/// <summary>
/// Update record, updating only fields specified in updateOnly that matches the where condition (if any), E.g:
///
@@ -64,6 +64,23 @@ public static class OrmLiteWriteExpressionsApiAsync
return dbConn.Exec(dbCmd => dbCmd.UpdateOnlyAsync(updateFields, q, commandFilter, token));
}

/// <summary>
/// Update record, updating only fields specified in updateOnly that matches the where condition (if any), E.g:
///
/// var q = db.From&gt;Person&lt;().Where(p => p.LastName == "Hendrix");
/// db.UpdateOnlyAsync(() => new Person { FirstName = "JJ" }, q.WhereExpression, q.Params);
/// UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("LastName" = 'Hendrix')
/// </summary>
public static Task<int> UpdateOnlyAsync<T>(this IDbConnection dbConn,
Expression<Func<T>> updateFields,
string whereExpression,
IEnumerable<IDbDataParameter> sqlParams,
Action<IDbCommand> commandFilter = null,
CancellationToken token = default(CancellationToken))
{
return dbConn.Exec(dbCmd => dbCmd.UpdateOnlyAsync(updateFields, whereExpression, sqlParams, commandFilter, token));
}

/// <summary>
/// Update record, updating only fields specified in updateOnly that matches the where condition (if any), E.g:
///
@@ -159,33 +159,90 @@ public void Can_UpdateOnly_multiple_columns()
[Test]
public void Supports_different_ways_to_UpdateOnly()
{
void Reset(IDbConnection db)
{
db.DeleteAll<Person>();
db.Insert(new Person {Id = 1, FirstName = "OriginalFirst", LastName = "OriginalLast", Age = 100});
}

using (var db = OpenDbConnection())
{
db.DropAndCreateTable<Person>();
db.Insert(new Person { Id = 1, FirstName = "OriginalFirst", LastName = "OriginalLast", Age = 100 });
db.DropAndCreateTable<Person>();

Reset(db);
db.UpdateOnly(() => new Person { FirstName = "UpdatedFirst", Age = 27 });
var row = db.Select<Person>().First();
Assert.That(row, Is.EqualTo(new Person(1, "UpdatedFirst", "OriginalLast", 27)));

db.DeleteAll<Person>();
db.Insert(new Person { Id = 1, FirstName = "OriginalFirst", LastName = "OriginalLast", Age = 100 });

Reset(db);
db.UpdateOnly(new Person { FirstName = "UpdatedFirst", Age = 27 }, p => p.FirstName);
row = db.Select<Person>().First();
Assert.That(row, Is.EqualTo(new Person(1, "UpdatedFirst", "OriginalLast", 100)));

db.DeleteAll<Person>();
db.Insert(new Person { Id = 1, FirstName = "OriginalFirst", LastName = "OriginalLast", Age = 100 });

Reset(db);
db.UpdateOnly(new Person { FirstName = "UpdatedFirst", Age = 27 }, p => new { p.FirstName, p.Age });
row = db.Select<Person>().First();
Assert.That(row, Is.EqualTo(new Person(1, "UpdatedFirst", "OriginalLast", 27)));

Reset(db);
db.UpdateOnly(new Person { FirstName = "UpdatedFirst", Age = 27 }, new[] { "FirstName", "Age" });
row = db.Select<Person>().First();
Assert.That(row, Is.EqualTo(new Person(1, "UpdatedFirst", "OriginalLast", 27)));

Reset(db);
db.UpdateOnly(() => new Person { FirstName = "UpdatedFirst", Age = 27 }, db.From<Person>().Where(x => x.Age == 100));
row = db.Select<Person>().First();
Assert.That(row, Is.EqualTo(new Person(1, "UpdatedFirst", "OriginalLast", 27)));

Reset(db);
var q = db.From<Person>().Where(x => x.Age == 100);
db.UpdateOnly(() => new Person { FirstName = "UpdatedFirst", Age = 27 }, q.WhereExpression, q.Params);
row = db.Select<Person>().First();
Assert.That(row, Is.EqualTo(new Person(1, "UpdatedFirst", "OriginalLast", 27)));
}
}

[Test]
public async Task Supports_different_ways_to_UpdateOnly_Async()
{
void Reset(IDbConnection db)
{
db.DeleteAll<Person>();
db.Insert(new Person { Id = 1, FirstName = "OriginalFirst", LastName = "OriginalLast", Age = 100 });
db.Insert(new Person {Id = 1, FirstName = "OriginalFirst", LastName = "OriginalLast", Age = 100});
}

db.UpdateOnly(new Person { FirstName = "UpdatedFirst", Age = 27 }, new[] { "FirstName", "Age" });
using (var db = OpenDbConnection())
{
db.DropAndCreateTable<Person>();

Reset(db);
await db.UpdateOnlyAsync(() => new Person { FirstName = "UpdatedFirst", Age = 27 });
var row = db.Select<Person>().First();
Assert.That(row, Is.EqualTo(new Person(1, "UpdatedFirst", "OriginalLast", 27)));

Reset(db);
await db.UpdateOnlyAsync(new Person { FirstName = "UpdatedFirst", Age = 27 }, p => p.FirstName);
row = db.Select<Person>().First();
Assert.That(row, Is.EqualTo(new Person(1, "UpdatedFirst", "OriginalLast", 100)));

Reset(db);
await db.UpdateOnlyAsync(new Person { FirstName = "UpdatedFirst", Age = 27 }, p => new { p.FirstName, p.Age });
row = db.Select<Person>().First();
Assert.That(row, Is.EqualTo(new Person(1, "UpdatedFirst", "OriginalLast", 27)));

Reset(db);
await db.UpdateOnlyAsync(new Person { FirstName = "UpdatedFirst", Age = 27 }, new[] { "FirstName", "Age" });
row = db.Select<Person>().First();
Assert.That(row, Is.EqualTo(new Person(1, "UpdatedFirst", "OriginalLast", 27)));

Reset(db);
await db.UpdateOnlyAsync(() => new Person { FirstName = "UpdatedFirst", Age = 27 }, db.From<Person>().Where(x => x.Age == 100));
row = db.Select<Person>().First();
Assert.That(row, Is.EqualTo(new Person(1, "UpdatedFirst", "OriginalLast", 27)));

Reset(db);
var q = db.From<Person>().Where(x => x.Age == 100);
await db.UpdateOnlyAsync(() => new Person { FirstName = "UpdatedFirst", Age = 27 }, q.WhereExpression, q.Params);
row = db.Select<Person>().First();
Assert.That(row, Is.EqualTo(new Person(1, "UpdatedFirst", "OriginalLast", 27)));
}

0 comments on commit a92aebf

Please sign in to comment.