/// <inheritdoc/> public void HandleColumnMapping(object sender, ColumnMappingEventArgs e) { if (FieldName != null && String.Compare(FieldName, e.TargetFieldName, StringComparison.OrdinalIgnoreCase) != 0) { return; } if (RecordType != null && e.TargetType != RecordType) { return; } if (FieldType != null) { var member = ClassPropInfo.GetMembersForType(e.TargetType) .Where(p => String.Compare(p.Name, e.TargetFieldName, StringComparison.OrdinalIgnoreCase) == 0) .FirstOrDefault(); if (member == null) { return; } } e.SerializationMode = SerializationMode; }
/// <summary> /// Provides the default mapping logic. /// </summary> /// <param name="sender">The ColumnMapping object that has generated the event.</param> /// <param name="e">The ColumnMappingEventArgs to process.</param> private void DefaultMappingHandler(object sender, ColumnMappingEventArgs e) { if (e.Reader != null) { e.TargetFieldName = e.Reader.GetName(e.FieldIndex); } else if (e.Parameters != null) { e.TargetFieldName = e.Parameters[e.FieldIndex].ParameterName; } else { throw new InvalidOperationException("DefaultMappingHandler requires either a Reader or Parameters list."); } }
/// <summary> /// Handles a column mapping event. /// </summary> /// <param name="sender">The ColumnMapping object that has generated the event.</param> /// <param name="e">The ColumnMappingEventArgs to process.</param> public void HandleColumnMapping(object sender, ColumnMappingEventArgs e) { if (e == null) { throw new ArgumentNullException("e"); } // if we aren't mapping the current type, just continue if (e.TargetType != typeof(T) && !e.TargetType.IsSubclassOf(typeof(T))) { return; } // perform a replacement on the target field name e.TargetFieldName = _regex.Replace(e.TargetFieldName, _replacement); }
/// <inheritdoc/> void IRecordStructure.MapColumn(ColumnMappingEventArgs e) { if (ColumnOverrides == null) { return; } var match = ColumnOverrides.SingleOrDefault( t => (t.TargetType == null || t.TargetType == e.TargetType) && String.Compare(t.ColumnName, e.TargetFieldName, StringComparison.OrdinalIgnoreCase) == 0); if (match != null) { e.TargetFieldName = match.FieldName; } }
/// <summary> /// Initialize the serializer on the given mapping. /// </summary> /// <param name="mapping">The mapping being evaluated.</param> /// <param name="reader">The reader being evaluated.</param> /// <param name="command">The command being evaluated.</param> /// <param name="parameters">The parameters being evaluated.</param> /// <param name="i">The index of the parameter being evaluated.</param> private static void InitializeMappingSerializer(ColumnMappingEventArgs mapping, IDataReader reader, IDbCommand command, IList <IDataParameter> parameters, int i) { // if the provider knows that this is an xml field, then automatically use that if ((command != null && InsightDbProvider.For(command).IsXmlParameter(command, parameters[i])) || (reader != null && InsightDbProvider.For(reader).IsXmlColumn(command, reader.GetSchemaTable(), i))) { mapping.SerializationMode = SerializationMode.Xml; mapping.Serializer = typeof(XmlObjectSerializer); } else { mapping.SerializationMode = mapping.SerializationMode ?? mapping.ClassPropInfo.SerializationMode; switch (mapping.SerializationMode) { default: case SerializationMode.Default: mapping.Serializer = ColumnMapping.DefaultObjectSerializer; break; case SerializationMode.Xml: mapping.Serializer = typeof(XmlObjectSerializer); break; case SerializationMode.Json: mapping.Serializer = JsonObjectSerializer.SerializerType; break; case SerializationMode.ToString: mapping.Serializer = typeof(ToStringObjectSerializer); break; case SerializationMode.Custom: mapping.Serializer = mapping.Serializer ?? mapping.ClassPropInfo.Serializer ?? ColumnMapping.DefaultObjectSerializer; break; } } // if we allow atomic types to get a different serializer, then there are certain situations where we can't figure out the right thing to do if (mapping.SerializationMode != SerializationMode.Default && TypeHelper.IsAtomicType(mapping.ClassPropInfo.MemberType) && mapping.ClassPropInfo.MemberType != typeof(string)) { throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Atomic types cannot have a column serializer: {0}.{1}", mapping.ClassPropInfo.Type.Name, mapping.ClassPropInfo.Name)); } }
/// <inheritdoc/> public void HandleColumnMapping(object sender, ColumnMappingEventArgs e) { if (FieldName != null && String.Compare(FieldName, e.TargetFieldName, StringComparison.OrdinalIgnoreCase) != 0) return; if (RecordType != null && e.TargetType != RecordType) return; if (FieldType != null) { var member = ClassPropInfo.GetMembersForType(e.TargetType) .Where(p => String.Compare(p.Name, e.TargetFieldName, StringComparison.OrdinalIgnoreCase) == 0) .FirstOrDefault(); if (member == null) return; } e.SerializationMode = SerializationMode; }
/// <summary> /// Creates the list of property setters for a reader. /// </summary> /// <param name="type">The type of object to map to.</param> /// <param name="reader">The reader to read.</param> /// <param name="command">The command that is currently being mapped.</param> /// <param name="parameters">The list of parameters used in the mapping operation.</param> /// <param name="structure">The structure of the record being read.</param> /// <param name="startColumn">The index of the first column to map.</param> /// <param name="columnCount">The number of columns to map.</param> /// <param name="uniqueMatches">True to only return the first match per field, false to return all matches per field.</param> /// <returns>An array of setters.</returns> internal ColumnMappingEventArgs[] CreateMapping( Type type, IDataReader reader, IDbCommand command, IList <IDataParameter> parameters, IRecordStructure structure, int startColumn, int columnCount, bool uniqueMatches) { ColumnMappingEventArgs[] mapping = new ColumnMappingEventArgs[columnCount]; // convert the list of names into a list of set reflections // clone the methods list, since we are only going to use each setter once (i.e. if you return two ID columns, we will only use the first one) // Also, we want to do a case-insensitive lookup of the property, so convert the dictionary to an uppercase dictionary var setMethods = new Dictionary <string, ClassPropInfo>(ClassPropInfo.GetMappingForType(type), StringComparer.OrdinalIgnoreCase); List <IDataParameter> readOnlyParameters = null; if (parameters != null) { readOnlyParameters = new List <IDataParameter>(parameters.OfType <IDataParameter>().ToList()); } // find all of the mappings for (int i = 0; i < columnCount; i++) { // generate an event var e = new ColumnMappingEventArgs() { TargetType = type, Reader = reader, FieldIndex = i + startColumn, Parameters = readOnlyParameters, }; if (command != null) { e.CommandText = command.CommandText; e.CommandType = command.CommandType; } if (reader != null) { e.ColumnName = reader.GetSchemaTable().Rows[e.FieldIndex]["ColumnName"].ToString(); } lock (_lock) { _mappings(null, e); } // if no mapping was returned, then skip the column if (e.Canceled || String.IsNullOrEmpty(e.TargetFieldName.Trim())) { continue; } // if a column mapping override was specified, then attempt an override if (structure != null) { structure.MapColumn(e); } // get the target property based on the result string targetFieldName = e.TargetFieldName; // first see if there is a wildcard column, if not, then look up the field name ClassPropInfo setter; if (setMethods.TryGetValue("*", out setter) || setMethods.TryGetValue(targetFieldName, out setter)) { mapping[i] = e; e.ClassPropInfo = setter; InitializeMappingSerializer(e, reader, command, parameters, i); // remove the name from the list so we can only use it once if (uniqueMatches) { setMethods.Remove(setter.ColumnName); } } } return(mapping); }