public QueryableCommand(IFolkeConnection connection, MappedClass mappedClass, string sql, params object[] parameters) { Connection = connection; Parameters = parameters; MappedClass = mappedClass; Sql = sql; }
/// <summary>A factory for a MappedClass instance</summary> /// <param name="fieldAliases">The fields that have been selected in the query and that should fill the class properties</param> /// <param name="type">The mapping of the type</param> /// <param name="selectedTable">The table of the fields to map or null if the object must be created empty</param> /// <returns>The mapping from the database query to the instancied object</returns> public static MappedClass MapClass(IList <SelectedField> fieldAliases, TypeMapping type, SelectedTable selectedTable) { if (fieldAliases == null) { return(null); } var mappedClass = new MappedClass(); var idProperty = type.Key; mappedClass.constructor = type.Type.GetTypeInfo().GetConstructor(Type.EmptyTypes); if (idProperty != null) { var selectedField = fieldAliases.SingleOrDefault(f => f.Field.Table == selectedTable && f.Field.Column == idProperty); mappedClass.primaryKeyField = new MappedField { SelectedField = selectedField, PropertyMapping = idProperty }; } foreach (var columnPair in type.Columns) { var propertyMapping = columnPair.Value; if (idProperty != null && propertyMapping == idProperty) { continue; } var fieldInfo = fieldAliases.SingleOrDefault(f => f.Field.Table == selectedTable && f.Field.Column == propertyMapping); bool isForeign = propertyMapping.Reference != null; if (fieldInfo != null || isForeign) { var mappedField = new MappedField { PropertyMapping = propertyMapping, SelectedField = fieldInfo }; if (isForeign) { if (selectedTable != null && selectedTable.Children.ContainsKey(propertyMapping)) { mappedField.MappedClass = MapClass(fieldAliases, propertyMapping.Reference, selectedTable.Children[propertyMapping]); } else { // The table has not been selected but the class must be created anyway with empty fields (or it may come from cache) mappedField.MappedClass = MapClass(fieldAliases, propertyMapping.Reference, null); } } mappedClass.fields.Add(mappedField); } } MapCollections(type, mappedClass); return(mappedClass); }
private static void MapCollections(TypeMapping type, MappedClass mappedClass) { if (type.Collections.Any()) { mappedClass.collections = new List <MappedCollection>(); foreach (var collection in type.Collections.Values) { mappedClass.collections.Add(collection); } } }