private void AppendRowForBcp(DataTable tableSchema, RowChange rowChange, Func <TableColumn, bool> filter) { var row = tableSchema.NewRow(); for (var i = 0; i < _tableChanges.ColumnNames.Length; i++) { var columnName = _tableChanges.ColumnNames[i]; var column = _tableChanges.ColumnsByName[columnName]; if (!filter(column)) { continue; } if (null == rowChange[i] && column.IsKey) { continue; } var columnValue = rowChange[i] ?? _tableChanges.GetDefaultValueForColumn(rowChange, column) ?? DBNull.Value; row[columnName] = columnValue; } _bcpDataRows.Add(row); }
public RowChange NewRowChanges() { var rowChanges = new RowChange(this); RowChanges.Add(rowChanges); InitKeys(rowChanges); return(rowChanges); }
private void AddRowChangeToTableVariable(RowChange rowChange, int rowChangeIndex) { _builder.AppendLine(); _builder.Append("INSERT {0} VALUES (".Fi(GetTableVariableName())); AddColumnValues(rowChange, c => c.IsKey); _builder.AppendFormat(", {0})", rowChangeIndex); if (_providerType == DatabaseProviderType.MySql) { _builder.Append(";"); } }
private void AppendInsertIntoTableVariable(RowChange rowChange, Func <TableColumn, bool> filter) { _builder.AppendLine(); _builder.Append("INSERT {0} VALUES (".FormatInvariant(GetTableVariableName())); AddColumnValues(rowChange, filter); _builder.Append(")"); if (_providerType == DatabaseProviderType.MySql) { _builder.Append(";"); } }
private bool MatchesUpdateColumnMap(RowChange rowChange) { for (var i = 0; i < _columnMap.Length; i++) { if (_columnMap[i] != IsUpdateColumn(rowChange, i)) { return(false); } } return(true); }
private void InstallUpdateColumnMap(RowChange rowChange) { var cntColumns = _tableChanges.ColumnNames.Length; if (null == _columnMap) { _columnMap = new bool[cntColumns]; } _hasUpdateColumns = false; for (var i = 0; i < cntColumns; i++) { _columnMap[i] = IsUpdateColumn(rowChange, i); _hasUpdateColumns |= _columnMap[i]; } }
public object GetDefaultValueForColumn(RowChange r, TableColumn c) { foreach (var g in DefaultColumnValueGenerators) { var v = g(r, c); if (v != null) { return(v); } } var sb = new StringBuilder(r.TableChanges.Columns.Length * 50); foreach (var columnName in r.TableChanges.ColumnNames) { sb.AppendFormat("{0} = {1}, ", columnName, r[columnName]); } sb.Length -= 2; throw new AssertionViolationException("Cannot generate default value for column {0} of type {1} in table {2}. Row values are: {3}".Fi( c.Name, c.Type.Name, r.TableChanges.TableName, sb)); }
private void AddColumnValues(RowChange rowChange, Func <TableColumn, bool> filter) { for (int i = 0; i < _tableChanges.ColumnNames.Length; i++) { var columnName = _tableChanges.ColumnNames[i]; var column = _tableChanges.ColumnsByName[columnName]; if (!filter(column)) { continue; } if (null == rowChange[i] && column.IsKey) { continue; } var columnValue = rowChange[i] ?? _tableChanges.GetDefaultValueForColumn(rowChange, column) ?? DBNull.Value; _builder.AppendFormat("{0}, ", columnValue.ToSql()); } _builder.Length -= 2; }
private void QueueRowForUpdate(RowChange rowChange) { if (!_hasUpdateColumns) { return; } Func <TableColumn, bool> filter = c => _columnMap[c.Index] || c.IsKey; if (0 == _builder.Length) { AddSetNoCount(); AddTableVariable(false, filter); } AppendInsertIntoTableVariable(rowChange, filter); if (_builder.Length < MaxSqlStatementLength) { return; } RunUpdateQuery(); }
private bool IsUpdateColumn(RowChange rowChange, int i) { return(!_tableChanges.Columns[i].IsKey); }