protected internal IEnumerable <object> GetRelationObjects(QuerySpec filter, TableSchema.Relation parentRelation, object parentObject) { var relations = Schema.BuildPreloadRelationSet(); var objects = from o in DataProvider.GetObjects(filter.Native, Schema) select Ir.WithLoadedRelations(Schema.UpdateObject(Activator.CreateInstance(Schema.ObjectType), o), relations); if (parentRelation?.ReverseRelation != null) { objects = from o in objects select parentRelation.ReverseRelation.SetField(o, parentObject); } if (filter.Code != null) { objects = from o in objects where filter.Code.IsFilterMatch(o) select o; } objects = objects.Select(o => { Fire_ObjectRead(o); return(o); }); return(objects); }
private Task _LoadRelationsAsync(object obj, IEnumerable <LambdaExpression> relationsToLoad) { return(Task.Run(() => { TableSchema parentSchema = GetSchema(obj.GetType()); Ir.LoadRelations(obj, LambdaRelationFinder.FindRelations(relationsToLoad, parentSchema)); })); }
internal T Load(T obj, object key, params Expression <Func <T, object> >[] relationsToLoad) { if (key == null) { throw new ArgumentNullException(nameof(key)); } Dictionary <string, object> primaryKey; var keyTypeInspector = key.GetType().Inspector(); if (keyTypeInspector.Is(TypeFlags.Numeric | TypeFlags.Enum | TypeFlags.String)) { if (Schema.PrimaryKeys.Length != 1) { throw new Exception($"Invalid key for {typeof (T)}"); } primaryKey = new Dictionary <string, object>() { { Schema.PrimaryKeys[0].MappedName, key } }; } else { primaryKey = Schema.PrimaryKeys.ToDictionary(pk => pk.MappedName, pk => GetFieldOrPropertyValue(key, pk.FieldName)); } var serializedEntity = DataProvider.ReadObject(primaryKey, Schema); if (serializedEntity == null) { return(default(T)); } Schema.UpdateObject(obj, serializedEntity); var relations = LambdaRelationFinder.FindRelations(relationsToLoad, Schema); if (Schema.DatasetRelations != null) { if (relations == null) { relations = new HashSet <TableSchema.Relation>(Schema.DatasetRelations); } else { relations.UnionWith(Schema.DatasetRelations); } } return(Ir.WithLoadedRelations(obj, relations)); }
internal object LoadRelation(object parentObject) { var localFieldValue = LocalField.GetField(parentObject); var foreignRepository = ForeignSchema.Repository; if (IsToOne) { var serializedForeignObject = localFieldValue == null ? null : foreignRepository.DataProvider.ReadObject(new Dictionary <string, object> { { ForeignField.MappedName, localFieldValue } }, ForeignSchema); var relationObject = serializedForeignObject != null?Ir.WithLoadedRelations(ForeignSchema.UpdateObject(Activator.CreateInstance(FieldType), serializedForeignObject), ForeignSchema.DatasetRelations) : null; if (ReverseRelation != null) { relationObject = ReverseRelation.SetField(relationObject, parentObject); } return(relationObject); } if (RelationType == RelationType.OneToMany) { var parameter = Expression.Parameter(ElementType, "x"); Expression exp1 = Expression.MakeMemberAccess(parameter, ForeignField.FieldInfo); Expression exp2 = Expression.Constant(localFieldValue, LocalField.FieldType); if (exp2.Type != exp1.Type) { exp2 = Expression.Convert(exp2, exp1.Type); } var filter = new FilterSpec(Expression.Lambda(Expression.Equal(exp1, exp2), parameter)); if (IsDataSet) { return(Activator.CreateInstance(typeof(DataSet <>).MakeGenericType(ElementType), ForeignSchema.Repository, filter, this, parentObject)); } else { return(CreateCollection(foreignRepository.GetRelationObjects(foreignRepository.CreateQuerySpec(filter), this, parentObject))); } } return(null); }
public static bool Delete <T>(this T entity) where T : IEntity { return(Ir.DataSet <T>().Delete(entity)); }
public static T Load <T>(this T obj, object key, params Expression <Func <T, object> >[] relationsToLoad) where T : IEntity { return(Ir.DataSet <T>().Load(obj, key, relationsToLoad)); }
public static bool Update <T>(this T entity, params Expression <Func <T, object> >[] relationsToSave) where T : IEntity { return(Ir.DataSet <T>().Update(entity, relationsToSave)); }
public static bool Insert <T>(this T entity, params Expression <Func <T, object> >[] relationsToSave) where T : IEntity { return(Ir.DataSet <T>().Insert(entity, deferSave: null, relationsToSave: relationsToSave)); }
private void FindFields() { var indexedFields = Ir.CreateEmptyList(new { IndexName = "", Position = 0, SortOrder = SortOrder.Ascending, Field = (Field)null, Unique = false }); var fieldList = new List <Field>(); var mappableFields = ObjectType.Inspector().GetFieldsAndProperties(BindingFlags.Instance | BindingFlags.Public).Where(field => _mappableTypes.Any(f => f(field.Type.Inspector()))).ToArray(); bool hasExplicitPrimaryKey = mappableFields.Any(f => f.HasAttribute <Column.PrimaryKeyAttribute>()); bool hasExplicitIndexes = mappableFields.Any(f => f.HasAttribute <Column.IndexedAttribute>()); foreach (var field in mappableFields) { var fieldInspector = field; if (fieldInspector.HasAttribute <Column.IgnoreAttribute>() || (fieldInspector.IsProperty && !fieldInspector.IsWritePublic)) { continue; } var schemaField = new Field(field.MemberInfo); var fieldPropertiesFromConvention = Ir.Config.NamingConvention.GetFieldProperties(this, schemaField); if (fieldPropertiesFromConvention.MappedTo != null) { schemaField.MappedName = fieldPropertiesFromConvention.MappedTo; } if (fieldInspector.HasAttribute <Column.NameAttribute>()) { schemaField.MappedName = fieldInspector.GetAttribute <Column.NameAttribute>().Name; } if (fieldInspector.HasAttribute <Column.SizeAttribute>()) { schemaField.ColumnSize = fieldInspector.GetAttribute <Column.SizeAttribute>().Size; schemaField.ColumnScale = fieldInspector.GetAttribute <Column.SizeAttribute>().Scale; } else if (field.Type == typeof(string)) { schemaField.ColumnSize = fieldInspector.HasAttribute <Column.LargeTextAttribute>() ? int.MaxValue : 50; } else if (field.Type.Inspector().Is(TypeFlags.Decimal)) { schemaField.ColumnSize = 10; schemaField.ColumnScale = 5; } if (fieldInspector.HasAttribute <Column.PrimaryKeyAttribute>()) { var pkAttribute = fieldInspector.GetAttribute <Column.PrimaryKeyAttribute>(); schemaField.UpdateFlags(FieldFlags.PrimaryKey, true); schemaField.UpdateFlags(FieldFlags.AutoIncrement, pkAttribute.AutoIncrement); } else if (!hasExplicitPrimaryKey && (fieldPropertiesFromConvention.PrimaryKey ?? false) && !fieldInspector.HasAttribute <Column.NoPrimaryKeyAttribute>()) { schemaField.UpdateFlags(FieldFlags.PrimaryKey, true); schemaField.UpdateFlags(FieldFlags.AutoIncrement, fieldPropertiesFromConvention.AutoIncrement); } schemaField.UpdateFlags(FieldFlags.Nullable, fieldPropertiesFromConvention.Null); if (fieldInspector.HasAttribute <Column.NotNullAttribute>()) { schemaField.UpdateFlags(FieldFlags.Nullable, false); } if (fieldInspector.HasAttribute <Column.NullAttribute>()) { schemaField.UpdateFlags(FieldFlags.Nullable, true); } if (fieldInspector.HasAttribute <Column.ReadOnlyAttribute>()) { schemaField.UpdateFlags(FieldFlags.ReadOnly, true); } if (schemaField.PrimaryKey) { schemaField.UpdateFlags(FieldFlags.Nullable, false); } if (fieldInspector.HasAttribute <Column.IndexedAttribute>() || ((fieldPropertiesFromConvention.Indexed ?? false) && !fieldInspector.HasAttribute <Column.NotIndexedAttribute>())) { var indexAttributes = fieldInspector.GetAttributes <Column.IndexedAttribute>(); if (indexAttributes.Length == 0 && !hasExplicitIndexes) { indexAttributes = new[] { new Column.IndexedAttribute() } } ; foreach (var indexAttribute in indexAttributes) { indexedFields.Add(new { IndexName = indexAttribute.IndexName ?? MappedName + schemaField.MappedName, Position = indexAttribute.Position, SortOrder = indexAttribute.Descending ? SortOrder.Descending : SortOrder.Ascending, Field = schemaField, Unique = indexAttribute.Unique }); } } FieldsByFieldName[schemaField.FieldName] = schemaField; FieldsByMappedName[schemaField.MappedName] = schemaField; fieldList.Add(schemaField); } Indexes = indexedFields .ToLookup(indexField => indexField.IndexName) .Select(item => new Index { Name = item.Key, FieldsWithOrder = item.OrderBy(f => f.Position).Select(f => new Tuple <Field, SortOrder>(f.Field, f.SortOrder)).ToArray(), Unique = item.All(arg => arg.Unique) }) .ToArray(); Fields = fieldList.ToArray(); WriteFields = fieldList.Where(f => !f.ColumnReadOnly && !f.AutoIncrement).ToArray(); PrimaryKeys = Fields.Where(f => f.PrimaryKey).ToArray(); IncrementKey = Fields.FirstOrDefault(f => f.AutoIncrement); }
private void _LoadRelations(object obj, IEnumerable <LambdaExpression> relationsToLoad /*, TableSchema parentSchema*/) { TableSchema parentSchema = GetSchema(obj.GetType()); Ir.LoadRelations(obj, LambdaRelationFinder.FindRelations(relationsToLoad, parentSchema)); }
public object ObjectWithRelations(object o) { return(Ir.WithLoadedRelations(o, Relations)); }