-
Notifications
You must be signed in to change notification settings - Fork 20
UPDATE
Signature:
QueryInfo QB.Update<T>(
T obj,
string whereSql = null,
TParamType whereParam = null,
Predicate<string> propFilter = null,
string tableName = null,
Dictionary <string, object> dict = null
)
Assume we have the following class:
// contains "SequentialGuid" and quiery builder "QB"
using SecurityDriven.TinyORM.Helpers;
public class Person
{
public string Name { get; set; } = "[Default]";
public Guid Id { get; set; } = SequentialGuid.NewSequentialGuid();
public DateTime BirthDate { get; set;} = System.Data.SqlTypes.SqlDateTime.MinValue.Value;
}
Let's generate SQL
to update a Person with a specific Id (cf9fad7a-9775-28b9-7693-11e6ea3b1484
).
var p = new Person()
{
Id = new Guid("cf9fad7a-9775-28b9-7693-11e6ea3b1484"),
Name = "John",
BirthDate = new DateTime(1975,03,17)
};
p.Dump(); // shows nicely in LinqPad - comment out otherwise
// [Person]
// Name : "John"
// Id : cf9fad7a-9775-28b9-7693-11e6ea3b1484
// BirthDate : 1975-03-`7 00:00:00
var query = QB.Update(p);
query.Dump(); // shows nicely in LinqPad - comment out otherwise
// [QueryInfo]
// [SQL] : "UPDATE [Person] SET [Name]=@@Name,[Id]=@@Id,[BirthDate]=@@BirthDate WHERE Id=@w@Id"
// [ParameterMap] : [Key : Value]
// "@@Name" : "John"
// "@@Id" : cf9fad7a-9775-28b9-7693-11e6ea3b1484
// "@@BirthDate" : 1975-03-17 00:00:00
// "@@w@Id" : cf9fad7a-9775-28b9-7693-11e6ea3b1484
await db.QueryAsync(query); // executing the built query
In the example above the p
entity is being updated without any filter. In order to prevent accidental undesired mass-update of every row, QB.Update()
will automatically create a WHERE
filter on Id
property of the entity. If Id
property does not exist and no filter is provided, QB.Update()
will throw an ArgumentException
("whereSql" is empty and object does not contain "Id" property.
). In order to explicitly update every row, provide a 1=1
or any other true filter condition.
Updating only the Name
:
var query = QB.Update(p, propFilter: c => c == "Name");
query.Dump(); // shows nicely in LinqPad - comment out otherwise
// [QueryInfo]
// [SQL] : "UPDATE [Person] SET [Name]=@@Name WHERE Id=@w@Id"
// [ParameterMap] : [Key : Value]
// "@@Name" : "John"
// "@@w@Id" : cf9fad7a-9775-28b9-7693-11e6ea3b1484
Updating everything except Id
and Name
:
var query = QB.Update(p, propFilter: c => !(c == "Name" || c == "Id"));
query.Dump(); // shows nicely in LinqPad - comment out otherwise
// [QueryInfo]
// [SQL] : "UPDATE [Person] SET [BirthDate]=@@BirthDate WHERE Id=@w@Id"
// [ParameterMap] : [Key : Value]
// "@@BirthDate" : 1975-03-17 00:00:00
// "@@w@Id" : cf9fad7a-9775-28b9-7693-11e6ea3b1484
You can also provide a custom name-value property set via dict
parameter. If dict
is non-null, it will be used instead of properties on the obj
instance:
var query = QB.Update(
obj: default(object),
tableName: "Some name".AsSqlName(),
dict: new Dictionary<string, object>()
{
{ "Id", 123 },
{ "Name", "Stan" }
});
query.Dump();
// [QueryInfo]
// [SQL] : "UPDATE [Some name] SET [Id]=@@Id,[Name]=@@Name WHERE Id=@w@Id"
// [ParameterMap] : [Key : Value]
// "@@Id" : 123
// "@@Name" : "Stan"
// "@@w@Id" : 123
Copyright (c) 2016-2022 Stan Drapkin