private DbCommand BuildUpdateCommand(DataTableMapping mappings, DataRow dataRow)
{
DbCommand command = InitializeCommand(UpdateCommand);
StringBuilder builder = new StringBuilder();
string nextSeparator = Set;
int parameterCount = 0;
Debug.Assert(!string.IsNullOrEmpty(_quotedBaseTableName), "no table name");
builder.Append(Update);
builder.Append(QuotedBaseTableName);
// search for the columns in that base table, to build the set clause
DbSchemaRow[] schemaRows = _dbSchemaRows;
for (int i = 0; i < schemaRows.Length; ++i)
{
DbSchemaRow row = schemaRows[i];
if ((null == row) || (0 == row.BaseColumnName.Length) || !IncludeInUpdateSet(row))
continue;
object currentValue = null;
string sourceColumn = _sourceColumnNames[i];
// If we're building a statement for a specific row, then check the
// values to see whether the column should be included in the update
// statement or not
if ((null != mappings) && (null != dataRow))
{
DataColumn dataColumn = GetDataColumn(sourceColumn, mappings, dataRow);
if (null == dataColumn)
continue;
// Don't bother updating if the column is readonly in both the data
// set and the back end.
if (row.IsReadOnly && dataColumn.ReadOnly)
continue;
// Unless specifically directed to do so, we will not automatically update
// a column with it's original value, which means that we must determine
// whether the value has changed locally, before we send it up.
currentValue = GetColumnValue(dataRow, dataColumn, DataRowVersion.Current);
if (!SetAllValues)
{
object originalValue = GetColumnValue(dataRow, dataColumn, DataRowVersion.Original);
if ((originalValue == currentValue)
|| ((null != originalValue) && originalValue.Equals(currentValue)))
{
continue;
}
}
}
builder.Append(nextSeparator);
nextSeparator = Comma;
builder.Append(QuotedColumn(row.BaseColumnName));
builder.Append(Equal);
builder.Append(
CreateParameterForValue(
command,
GetBaseParameterName(i),
sourceColumn,
DataRowVersion.Current,
parameterCount,
currentValue,
row, StatementType.Update, false
)
);
parameterCount++;
}
// It is an error to attempt an update when there's nothing to update;
bool skipRow = (0 == parameterCount);
parameterCount = BuildWhereClause(mappings, dataRow, builder, command, parameterCount, true);
command.CommandText = builder.ToString();
RemoveExtraParameters(command, parameterCount);
UpdateCommand = command;
return (skipRow) ? null : command;
}