protected void AddColumns(params Expression <Func <TEntity, object> >[] members) { foreach (var member in members) { if (member.Body is NewExpression newExpression) { foreach (var arg in newExpression.Arguments) { ColumnMappings.Add(Expression.Lambda <Func <TEntity, object> >( Expression.Convert(arg, typeof(object)), member.Parameters)); } ; } else { var memberExpression = (member.Body is UnaryExpression unaryExpression) ? unaryExpression.Operand as MemberExpression : member.Body as MemberExpression; if (memberExpression == null) { throw new ArgumentException($"Invalid expression in parameter list: '{member}'", nameof(members)); } ColumnMappings.Add(MakeColumnExpression <TEntity>(memberExpression.Member)); } } }
public TableData(DataContext context, DataTypeMappingElement mappingConfig) { TableName = mappingConfig.TableName; ObjectType = mappingConfig.ObjectType; DataTable schemaTable = context.SchemaTables[mappingConfig]; if (!string.IsNullOrEmpty(mappingConfig.IDColumn)) { DataRow idColumnRow = schemaTable.Select(string.Format(COLUMN_NAME_FILTER, mappingConfig.IDColumn)).FirstOrDefault(); if (idColumnRow != null) { KeyColumn = mappingConfig.IDColumn; } } else { KeyColumn = Constants.DEFAULT_ID_COLUMN; } CacheData = mappingConfig.CacheData; schemaTable.Rows.Cast <DataRow>().ToList().ForEach(columnDataRow => { string columnName = columnDataRow[COLUMN_NAME_COL].ToString(); PropertyMappingElement columnPropMapping = mappingConfig.PropertyMappings.FirstOrDefault(propMapping => string.Compare(propMapping.ColumnName, columnName, true) == 0); if (columnPropMapping != null) { PropertyInfo propInfo = mappingConfig.ObjectType.GetProperty(columnPropMapping.PropertyName); if (propInfo != null) { ColumnMappings.Add(columnName, propInfo); } } else { string columNameWithoutUnderscores = columnName.Replace("_", ""); PropertyInfo propInfo = mappingConfig.ObjectType.GetProperties().FirstOrDefault(propMatch => string.Compare(propMatch.Name, columNameWithoutUnderscores, true) == 0); if (propInfo != null) { ColumnMappings.Add(columnName, propInfo); } } }); MapChildTables(context, mappingConfig); // TODO: throw invalid column mapping exceptions here }
internal void InitializeColumnMappings(Dictionary <string, ForeignKeyColumnInfo> fks) { foreach (KeyValuePair <string, ForeignKeyColumnInfo> kvp in fks) { string fkTableName = kvp.Value.ReferencedTableName; if (string.IsNullOrEmpty(fkTableName)) { continue; } if (ColumnMappings.ContainsKey(fkTableName)) { continue; } Dictionary <string, Column> dicCols = GetColumnsFromTable(fkTableName, WizardForm.Connection); List <ColumnValidation> myColValidations = ValidationsGrid.GetColumnValidationList(fkTableName, dicCols, null); ColumnMappings.Add(fkTableName, myColValidations.ToDictionary(p => { return(p.Name); })); } }
private async ValueTask WriteToServerAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) #endif { var tableName = DestinationTableName ?? throw new InvalidOperationException("DestinationTableName must be set before calling WriteToServer"); var bulkLoader = new MySqlBulkLoader(m_connection) { CharacterSet = "utf8mb4", EscapeCharacter = '\\', FieldQuotationCharacter = '\0', FieldTerminator = "\t", LinePrefix = null, LineTerminator = "\n", Local = true, NumberOfLinesToSkip = 0, Source = this, TableName = QuoteIdentifier(tableName), Timeout = BulkCopyTimeout, }; var closeConnection = false; if (m_connection.State != ConnectionState.Open) { m_connection.Open(); closeConnection = true; } // if no user-supplied column mappings, compute them from the destination schema if (ColumnMappings.Count == 0) { using var cmd = new MySqlCommand("select * from " + QuoteIdentifier(tableName) + ";", m_connection, m_transaction); using var reader = (MySqlDataReader) await cmd.ExecuteReaderAsync(CommandBehavior.SchemaOnly, ioBehavior, cancellationToken).ConfigureAwait(false); var schema = reader.GetColumnSchema(); for (var i = 0; i < schema.Count; i++) { if (schema[i].DataTypeName == "BIT") { ColumnMappings.Add(new MySqlBulkCopyColumnMapping(i, $"@col{i}", $"`{reader.GetName(i)}` = CAST(@col{i} AS UNSIGNED)")); } else if (schema[i].DataTypeName == "YEAR") { // the current code can't distinguish between 0 = 0000 and 0 = 2000 throw new NotSupportedException("'YEAR' columns are not supported by MySqlBulkLoader."); } else { var type = schema[i].DataType; if (type == typeof(byte[]) || (type == typeof(Guid) && (m_connection.GuidFormat == MySqlGuidFormat.Binary16 || m_connection.GuidFormat == MySqlGuidFormat.LittleEndianBinary16 || m_connection.GuidFormat == MySqlGuidFormat.TimeSwapBinary16))) { ColumnMappings.Add(new MySqlBulkCopyColumnMapping(i, $"@col{i}", $"`{reader.GetName(i)}` = UNHEX(@col{i})")); } else { ColumnMappings.Add(new MySqlBulkCopyColumnMapping(i, reader.GetName(i))); } } } } // set columns and expressions from the column mappings for (var i = 0; i < m_valuesEnumerator !.FieldCount; i++) { var columnMapping = ColumnMappings.FirstOrDefault(x => x.SourceOrdinal == i); if (columnMapping is null) { bulkLoader.Columns.Add("@`\uE002\bignore`"); } else { if (columnMapping.DestinationColumn.Length == 0) { throw new InvalidOperationException("MySqlBulkCopyColumnMapping.DestinationName is not set."); } if (columnMapping.DestinationColumn[0] == '@') { bulkLoader.Columns.Add(columnMapping.DestinationColumn); } else { bulkLoader.Columns.Add(QuoteIdentifier(columnMapping.DestinationColumn)); } if (columnMapping.Expression is object) { bulkLoader.Expressions.Add(columnMapping.Expression); } } } await bulkLoader.LoadAsync(ioBehavior, cancellationToken).ConfigureAwait(false); if (closeConnection) { m_connection.Close(); } #if !NETSTANDARD2_1 && !NETCOREAPP3_0 return(default);