/// <summary>
/// Performs an SQL update
/// </summary>
/// <param name="tableName">The name of the table to update</param>
/// <param name="primaryKeyName">The name of the primary key column of the table</param>
/// <param name="poco">The POCO object that specifies the column values to be updated</param>
/// <param name="primaryKeyValue">The primary key of the record to be updated</param>
/// <returns>The number of affected records</returns>
public int Update(string tableName, string primaryKeyName, object poco, object primaryKeyValue)
{
if (string.IsNullOrEmpty(tableName))
throw new ArgumentNullException("tableName"); if (string.IsNullOrEmpty(primaryKeyName))
throw new ArgumentNullException("primaryKeyName"); if (poco == null)
throw new ArgumentNullException("poco"); return ExecuteUpdate(tableName, primaryKeyName, poco, primaryKeyValue, null);
} /// <summary>
/// Performs an SQL update
/// </summary>
/// <param name="tableName">The name of the table to update</param>
/// <param name="primaryKeyName">The name of the primary key column of the table</param>
/// <param name="poco">The POCO object that specifies the column values to be updated</param>
/// <param name="primaryKeyValue">The primary key of the record to be updated</param>
/// <param name="columns">The column names of the columns to be updated, or null for all</param>
/// <returns>The number of affected rows</returns>
public int Update(string tableName, string primaryKeyName, object poco, object primaryKeyValue, IEnumerable<string> columns)
{
if (string.IsNullOrEmpty(tableName))
throw new ArgumentNullException("tableName"); if (string.IsNullOrEmpty(primaryKeyName))
throw new ArgumentNullException("primaryKeyName"); if (poco == null)
throw new ArgumentNullException("poco"); return ExecuteUpdate(tableName, primaryKeyName, poco, primaryKeyValue, columns);
} /// <summary>
/// Performs an SQL update
/// </summary>
/// <param name="tableName">The name of the table to update</param>
/// <param name="primaryKeyName">The name of the primary key column of the table</param>
/// <param name="poco">The POCO object that specifies the column values to be updated</param>
/// <returns>The number of affected rows</returns>
public int Update(string tableName, string primaryKeyName, object poco)
{
return Update(tableName, primaryKeyName, poco, null);
} /// <summary>
/// Performs an SQL update
/// </summary>
/// <param name="tableName">The name of the table to update</param>
/// <param name="primaryKeyName">The name of the primary key column of the table</param>
/// <param name="poco">The POCO object that specifies the column values to be updated</param>
/// <param name="columns">The column names of the columns to be updated, or null for all</param>
/// <returns>The number of affected rows</returns>
public int Update(string tableName, string primaryKeyName, object poco, IEnumerable<string> columns)
{
if (string.IsNullOrEmpty(tableName))
throw new ArgumentNullException("tableName"); if (string.IsNullOrEmpty(primaryKeyName))
throw new ArgumentNullException("primaryKeyName"); if (poco == null)
throw new ArgumentNullException("poco"); return ExecuteUpdate(tableName, primaryKeyName, poco, null, columns);
} /// <summary>
/// Performs an SQL update
/// </summary>
/// <param name="poco">The POCO object that specifies the column values to be updated</param>
/// <param name="columns">The column names of the columns to be updated, or null for all</param>
/// <returns>The number of affected rows</returns>
public int Update(object poco, IEnumerable<string> columns)
{
return Update(poco, null, columns);
} /// <summary>
/// Performs an SQL update
/// </summary>
/// <param name="poco">The POCO object that specifies the column values to be updated</param>
/// <returns>The number of affected rows</returns>
public int Update(object poco)
{
return Update(poco, null, null);
} /// <summary>
/// Performs an SQL update
/// </summary>
/// <param name="poco">The POCO object that specifies the column values to be updated</param>
/// <param name="primaryKeyValue">The primary key of the record to be updated</param>
/// <returns>The number of affected rows</returns>
public int Update(object poco, object primaryKeyValue)
{
return Update(poco, primaryKeyValue, null);
} /// <summary>
/// Performs an SQL update
/// </summary>
/// <param name="poco">The POCO object that specifies the column values to be updated</param>
/// <param name="primaryKeyValue">The primary key of the record to be updated</param>
/// <param name="columns">The column names of the columns to be updated, or null for all</param>
/// <returns>The number of affected rows</returns>
public int Update(object poco, object primaryKeyValue, IEnumerable<string> columns)
{
if (poco == null)
throw new ArgumentNullException("poco"); var pd = PocoData.ForType(poco.GetType(), _defaultMapper);
return ExecuteUpdate(pd.TableInfo.TableName, pd.TableInfo.PrimaryKey, poco, primaryKeyValue, columns);
} /// <summary>
/// Performs an SQL update
/// </summary>
/// <typeparam name="T">The POCO class who's attributes specify the name of the table to update</typeparam>
/// <param name="sql">The SQL update and condition clause (ie: everything after "UPDATE tablename"</param>
/// <param name="args">Arguments to any embedded parameters in the SQL</param>
/// <returns>The number of affected rows</returns>
public int Update<T>(string sql, params object[] args)
{
if (string.IsNullOrEmpty(sql))
throw new ArgumentNullException("sql"); var pd = PocoData.ForType(typeof(T), _defaultMapper);
return Execute(string.Format("UPDATE {0} {1}", _provider.EscapeTableName(pd.TableInfo.TableName), sql), args);
} /// <summary>
/// Performs an SQL update
/// </summary>
/// <typeparam name="T">The POCO class who's attributes specify the name of the table to update</typeparam>
/// <param name="sql">
/// An SQL builder object representing the SQL update and condition clause (ie: everything after "UPDATE
/// tablename"
/// </param>
/// <returns>The number of affected rows</returns>
public int Update<T>(Sql sql)
{
if (sql == null)
throw new ArgumentNullException("sql"); var pd = PocoData.ForType(typeof(T), _defaultMapper);
return Execute(new Sql(string.Format("UPDATE {0}", _provider.EscapeTableName(pd.TableInfo.TableName))).Append(sql));
} private int ExecuteUpdate(string tableName, string primaryKeyName, object poco, object primaryKeyValue, IEnumerable<string> columns)
{
try
{
OpenSharedConnection();
try
{
using (var cmd = CreateCommand(_sharedConnection, ""))
{
var sb = new StringBuilder();
var index = ;
var pd = PocoData.ForObject(poco, primaryKeyName, _defaultMapper);
if (columns == null)
{
foreach (var i in pd.Columns)
{
// Don't update the primary key, but grab the value if we don't have it
if (string.Compare(i.Key, primaryKeyName, true) == )
{
if (primaryKeyValue == null)
primaryKeyValue = i.Value.GetValue(poco);
continue;
} // Dont update result only columns
if (i.Value.ResultColumn)
continue; // Build the sql
if (index > )
sb.Append(", ");
sb.AppendFormat("{0} = {1}{2}", _provider.EscapeSqlIdentifier(i.Key), _paramPrefix, index++); // Store the parameter in the command
AddParam(cmd, i.Value.GetValue(poco), i.Value.PropertyInfo);
}
}
else
{
foreach (var colname in columns)
{
var pc = pd.Columns[colname]; // Build the sql
if (index > )
sb.Append(", ");
sb.AppendFormat("{0} = {1}{2}", _provider.EscapeSqlIdentifier(colname), _paramPrefix, index++); // Store the parameter in the command
AddParam(cmd, pc.GetValue(poco), pc.PropertyInfo);
} // Grab primary key value
if (primaryKeyValue == null)
{
var pc = pd.Columns[primaryKeyName];
primaryKeyValue = pc.GetValue(poco);
}
} // Find the property info for the primary key
PropertyInfo pkpi = null;
if (primaryKeyName != null)
{
PocoColumn col;
pkpi = pd.Columns.TryGetValue(primaryKeyName, out col)
? col.PropertyInfo
: new { Id = primaryKeyValue }.GetType().GetProperties()[];
} cmd.CommandText = string.Format("UPDATE {0} SET {1} WHERE {2} = {3}{4}",
_provider.EscapeTableName(tableName), sb.ToString(), _provider.EscapeSqlIdentifier(primaryKeyName), _paramPrefix, index++);
AddParam(cmd, primaryKeyValue, pkpi); DoPreExecute(cmd); // Do it
var retv = cmd.ExecuteNonQuery();
OnExecutedCommand(cmd);
return retv;
}
}
finally
{
CloseSharedConnection();
}
}
catch (Exception x)
{
if (OnException(x))
throw;
return -;
}
}