/// <summary> /// Adds actual table fields in a row to select list of a query.</summary> /// <param name="query"> /// Query to select fields into (required).</param> /// <param name="row"> /// Row with fields to be selected (required).</param> /// <param name="exclude"> /// Fields to be excluded (optional).</param> public static SqlQuery SelectTableFields(this SqlQuery query, Row row, params Field[] exclude) { if (query == null) { throw new ArgumentNullException("query"); } if (row == null) { throw new ArgumentNullException("row"); } HashSet <Field> excludeFields = (exclude != null && exclude.Length > 0) ? new HashSet <Field>(exclude) : null; var fields = row.GetFields(); for (int i = 0; i < row.FieldCount; i++) { Field field = fields[i]; if (EntityFieldExtensions.IsTableField(field)) { if (excludeFields == null || !excludeFields.Contains(field)) { query.Select(field); } } } return(query); }
public static Field GetNameField(this Row row, bool force = false) { var nameRow = row as INameRow; if (nameRow != null) { return(nameRow.NameField); } var nameProperty = row.GetFields().GetFieldsByAttribute <NamePropertyAttribute>(); if (nameProperty.Length > 0) { if (nameProperty.Length > 1) { throw new Exception(string.Format( "Row type {0} has multiple fields with [NameProperty] attribute!", row.GetType().FullName)); } return(nameProperty[0]); } if (force) { throw new Exception(string.Format( "Row type {0} doesn't have a field with [NameProperty] attribute and doesn't implement INameRow!", row.GetType().FullName)); } return(null); }
public static List<ReportColumn> EntityTypeToList(Row instance) { var list = new List<ReportColumn>(); foreach (var field in instance.GetFields()) { list.Add(FromField(field)); } return list; }
public static SqlQuery EnsureAllForeignJoins(this SqlQuery query, Row row) { foreach (var field in row.GetFields()) { if ((field.Flags & FieldFlags.Foreign) == FieldFlags.Foreign) { query.EnsureJoinOf(field); } } return(query); }
public static void ApplyDefaultValues(this Row row) { foreach (var field in row.GetFields()) { var value = field.DefaultValue; if (value != null) { field.AsObject(row, field.ConvertValue(value, CultureInfo.InvariantCulture)); } } }
public static void SetEnumFormatters(IDictionary <string, ValueFormatter> formatters, Row row) { foreach (var field in row.GetFields()) { var f = field as Int32Field; if (f != null && f.EnumType != null) { formatters[f.Name] = Enum(f.EnumType); } } }
/// <summary> /// Gets a dictionary of table fields (e.g. not a foreign or calculated field) in a row.</summary> /// <param name="row"> /// The row to return dictionary of table fields</param> /// <returns> /// A dictionary of table fields in which field objects are keys.</returns> public static IEnumerable <Field> EnumerateTableFields(this Row row) { var fields = row.GetFields(); for (int i = 0; i < fields.Count; i++) { Field field = fields[i]; if (field.IsTableField()) { yield return(field); } } }
/// <summary> /// Gets a dictionary of table fields (e.g. not a foreign or calculated field) in a row.</summary> /// <param name="row"> /// The row to return dictionary of table fields</param> /// <returns> /// A dictionary of table fields in which field objects are keys.</returns> public static HashSet <Field> GetTableFields(this Row row) { HashSet <Field> tableFields = new HashSet <Field>(); var fields = row.GetFields(); for (int i = 0; i < fields.Count; i++) { Field field = fields[i]; if (IsTableField(field)) { tableFields.Add(field); } } return(tableFields); }
/// <summary> /// Adds all field values in a row to where clause with equality operator and auto named parameters /// (field name prefixed with '@').</summary> /// <param field="row"> /// The row with modified field values to be added to the where clause (key row). Must be in TrackAssignments mode, /// or an exception is raised.</param> /// <returns> /// Object itself.</returns> public static T WhereEqual <T>(this T self, Row row) where T : IFilterableQuery { if (row == null) { throw new ArgumentNullException("row"); } if (!row.TrackAssignments) { throw new ArgumentException("row must be in TrackAssignments mode to determine modified fields."); } foreach (var field in row.GetFields()) { if (row.IsAssigned(field)) { self.Where(new Criteria(field) == self.AddParam(field.AsObject(row))); } } return(self); }
/// <summary> /// Sets all field values in a row with auto named parameters (field name prefixed with '@').</summary> /// <param field="row"> /// The row with modified field values. Must be in TrackAssignments mode, or an exception is raised.</param> /// <returns> /// Object itself.</returns> public static T Set <T>(this T self, Row row, IField exclude = null) where T : ISetFieldByStatement { if (row == null) { throw new ArgumentNullException("row"); } if (!row.TrackAssignments) { throw new ArgumentException("row must be in TrackAssignments mode to determine modified fields."); } foreach (var field in row.GetFields()) { if (!ReferenceEquals(field, exclude) && row.IsAssigned(field)) { self.Set((IField)field, field.AsObject(row)); } } return(self); }
public bool ActivateFor(Row row) { var attrs = row.GetType().GetCustomAttributes<UpdatableExtensionAttribute>(); if (attrs == null || !attrs.Any()) return false; var sourceByExpression = row.GetFields().ToLookup(x => BracketLocator.ReplaceBrackets(x.Expression.TrimToEmpty(), BracketRemoverDialect.Instance)); this.infoList = attrs.Select(attr => { var info = new RelationInfo(); info.Attr = attr; var rowType = attr.RowType; if (rowType.GetIsAbstract() || !typeof(Row).IsAssignableFrom(rowType)) { throw new ArgumentException(String.Format( "Row type '{1}' has an ExtensionRelation attribute " + "but its specified extension row type '{0}' is not a valid row class!", rowType.FullName, row.GetType().FullName)); } info.RowFactory = FastReflection.DelegateForConstructor<Row>(rowType); info.ListHandlerFactory = FastReflection.DelegateForConstructor<IListRequestProcessor>( typeof(ListRequestHandler<>).MakeGenericType(rowType)); info.SaveHandlerFactory = FastReflection.DelegateForConstructor<ISaveRequestProcessor>( typeof(SaveRequestHandler<>).MakeGenericType(rowType)); info.SaveRequestFactory = FastReflection.DelegateForConstructor<ISaveRequest>( typeof(SaveRequest<>).MakeGenericType(rowType)); info.DeleteHandlerFactory = FastReflection.DelegateForConstructor<IDeleteRequestProcessor>( typeof(DeleteRequestHandler<>).MakeGenericType(rowType)); var thisKey = attr.ThisKey; if (string.IsNullOrEmpty(thisKey)) { if (!(row is IIdRow)) { throw new ArgumentException(String.Format( "Row type '{0}' has an ExtensionRelation attribute " + "but its ThisKey is not specified!", row.GetType().FullName)); } info.ThisKeyField = (Field)(((IIdRow)row).IdField); } else { info.ThisKeyField = row.FindFieldByPropertyName(attr.ThisKey) ?? row.FindField(attr.ThisKey); if (ReferenceEquals(info.ThisKeyField, null)) throw new ArgumentException(String.Format("Field '{0}' doesn't exist in row of type '{1}'." + "This field is specified for an ExtensionRelation attribute", attr.ThisKey, row.GetType().FullName)); } var ext = info.RowFactory(); var otherKey = attr.OtherKey; if (string.IsNullOrEmpty(otherKey)) { info.OtherKeyField = ext.FindField(info.ThisKeyField.Name); if (ReferenceEquals(info.OtherKeyField, null) && ext is IIdRow) info.OtherKeyField = (Field)(((IIdRow)row).IdField); if (ReferenceEquals(info.OtherKeyField, null)) throw new ArgumentException(String.Format( "Row type '{1}' has an ExtensionRelation attribute " + "but its OtherKey is not specified!", row.GetType().FullName)); } else { info.OtherKeyField = ext.FindFieldByPropertyName(attr.OtherKey) ?? ext.FindField(attr.OtherKey); if (ReferenceEquals(info.OtherKeyField, null)) throw new ArgumentException(String.Format("Field '{0}' doesn't exist in row of type '{1}'." + "This field is specified for an ExtensionRelation attribute on '{2}'", attr.OtherKey, ext.GetType().FullName, row.GetType().FullName)); } if (!string.IsNullOrEmpty(attr.FilterField)) { info.FilterField = ext.FindFieldByPropertyName(attr.FilterField) ?? ext.FindField(attr.FilterField); if (ReferenceEquals(info.FilterField, null)) throw new ArgumentException(String.Format("Field '{0}' doesn't exist in row of type '{1}'." + "This field is specified as FilterField for an ExtensionRelation attribute on '{2}'", attr.OtherKey, ext.GetType().FullName, row.GetType().FullName)); info.FilterValue = info.FilterField.ConvertValue(attr.FilterValue, CultureInfo.InvariantCulture); } if (!string.IsNullOrEmpty(attr.PresenceField)) { info.PresenceField = ext.FindFieldByPropertyName(attr.PresenceField) ?? ext.FindField(attr.PresenceField); if (ReferenceEquals(info.PresenceField, null)) throw new ArgumentException(String.Format("Field '{0}' doesn't exist in row of type '{1}'." + "This field is specified as PresenceField as an ExtensionRelation attribute.", attr.PresenceField, row.GetType().FullName)); info.PresenceValue = attr.PresenceValue; } var extFields = ext.GetFields(); var alias = attr.Alias; var aliasPrefix = attr.Alias + "_"; var joinByKey = new HashSet<string>(extFields.Joins.Keys, StringComparer.OrdinalIgnoreCase); Func<string, string> mapAlias = x => { if (x == "t0" || x == "T0") return alias; if (!joinByKey.Contains(x)) return x; return aliasPrefix + x; }; Func<string, string> mapExpression = x => { if (x == null) return null; return JoinAliasLocator.ReplaceAliases(x, mapAlias); }; info.Mappings = new List<Tuple<Field, Field>>(); foreach (var field in extFields) { if (ReferenceEquals(info.OtherKeyField, field)) continue; if (ReferenceEquals(info.FilterField, field)) continue; var expression = field.Expression.TrimToEmpty(); if (string.IsNullOrEmpty(expression)) continue; expression = mapExpression(expression); expression = BracketLocator.ReplaceBrackets(expression, BracketRemoverDialect.Instance); var match = sourceByExpression[expression].FirstOrDefault(); if (ReferenceEquals(null, match)) continue; if (match.IsTableField()) continue; if (ReferenceEquals(info.ThisKeyField, match)) continue; if (field.GetType() != match.GetType()) throw new ArgumentException(String.Format( "Row type '{0}' has an ExtensionRelation attribute to '{1}'." + "Their '{2}' and '{3}' fields are matched but they have different types ({4} and {5})!", row.GetType().FullName, ext.GetType().FullName, field.PropertyName ?? field.Name, match.PropertyName ?? match.Name, field.GetType().Name, match.GetType().Name)); info.Mappings.Add(new Tuple<Field, Field>(match, field)); } if (info.Mappings.Count == 0) throw new ArgumentException(String.Format( "Row type '{0}' has an ExtensionRelation attribute " + "but no view fields could be matched to extension row '{1}'!", row.GetType().FullName, ext.GetType().FullName)); return info; }).ToList(); return true; }