private DbCommand BuildInsertCommand(DataTableMapping mappings, DataRow dataRow)
{
DbCommand command = InitializeCommand(InsertCommand);
StringBuilder builder = new StringBuilder();
int parameterCount = 0;
string nextSeparator = SpaceLeftParenthesis;
Debug.Assert(!string.IsNullOrEmpty(_quotedBaseTableName), "no table name");
builder.Append(InsertInto);
builder.Append(QuotedBaseTableName);
// search for the columns in that base table, to be the column clause
DbSchemaRow[] schemaRows = _dbSchemaRows;
string[] parameterName = new string[schemaRows.Length];
for (int i = 0; i < schemaRows.Length; ++i)
{
DbSchemaRow row = schemaRows[i];
if ((null == row) || (0 == row.BaseColumnName.Length) || !IncludeInInsertValues(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 insert
// statement or not
if ((null != mappings) && (null != dataRow))
{
DataColumn dataColumn = GetDataColumn(sourceColumn, mappings, dataRow);
if (null == dataColumn)
continue;
// Don't bother inserting if the column is readonly in both the data
// set and the back end.
if (row.IsReadOnly && dataColumn.ReadOnly)
continue;
currentValue = GetColumnValue(dataRow, dataColumn, DataRowVersion.Current);
// If the value is null, and the column doesn't support nulls, then
// the user is requesting the server-specified default value, so don't
// include it in the set-list.
if (!row.AllowDBNull && (null == currentValue || Convert.IsDBNull(currentValue)))
continue;
}
builder.Append(nextSeparator);
nextSeparator = Comma;
builder.Append(QuotedColumn(row.BaseColumnName));
parameterName[parameterCount] = CreateParameterForValue(
command,
GetBaseParameterName(i),
sourceColumn,
DataRowVersion.Current,
parameterCount,
currentValue,
row, StatementType.Insert, false
);
parameterCount++;
}
if (0 == parameterCount)
builder.Append(DefaultValues);
else
{
builder.Append(RightParenthesis);
builder.Append(Values);
builder.Append(LeftParenthesis);
builder.Append(parameterName[0]);
for (int i = 1; i < parameterCount; ++i)
{
builder.Append(Comma);
builder.Append(parameterName[i]);
}
builder.Append(RightParenthesis);
}
command.CommandText = builder.ToString();
RemoveExtraParameters(command, parameterCount);
InsertCommand = command;
return command;
}