public override Expression GetUpdateExpression(IMappedEntity entity, Expression instance, LambdaExpression updateCheck, LambdaExpression selector) { var IdentifiableAlias = new IdentifiableAlias(); var table = new IdentifiableExpression(IdentifiableAlias, entity, ((OTypeMapping)this.Mapping).GetTableName(entity)); var where = this.GetIdentityCheck(table, entity, instance); if (updateCheck != null) { Expression typeProjector = this.GetEntityExpression(table, entity); Expression pred = DbExpressionReplacer.Replace(updateCheck.Body, updateCheck.Parameters[0], typeProjector); where = where.And(pred); } var assignments = this.GetFieldAssignments(table, instance, entity, (e, m) => ((OTypeMapping)this.Mapping).IsUpdatable(e, m)); Expression update = new UpdateCommandExpression(table, where, assignments); if (selector != null) { return CreateUpdateCommand(entity, instance, selector, where, assignments); } else { return update; } }
public override void SaveBucketImport(TrelloCaseEntityBucket bucket, IMappedEntity existing, String operation) { MappedCase obj = bucket.Case; Case impl = cbapi.Put <Case>(obj.Local, obj.LocalID); bucket.Case.AddLocal(impl, impl.SyncID, impl.SyncTime); UpdateStatus(obj, operation); }
public override void MapBucketImport(TrelloCaseEntityBucket bucket, IMappedEntity existing) { MappedCase obj = bucket.Case; CardData data = obj.Extern; Case impl = obj.Local = new Case(); //Product impl.Subject = data.Name.ValueField(); impl.Description = data.Description.ValueField(); }
public override Expression GetInsertExpression(IMappedEntity entity, Expression instance, LambdaExpression selector) { var IdentifiableAlias = new IdentifiableAlias(); var table = new IdentifiableExpression(IdentifiableAlias, entity, ((OTypeMapping)this.Mapping).GetTableName(entity)); var assignments = this.GetFieldAssignments(table, instance, entity, (e, m) => !((OTypeMapping)this.Mapping).IsGenerated(e, m)); if (selector != null) { return CreateInsertCommand(entity, instance, selector, assignments); } return new InsertCommandExpression(table, assignments); }
public override EntityExpression GetEntityExpression(Expression root, IMappedEntity entity) { // must be some complex type constructed from multiple Fields var assignments = new List<EntityAssignment>(); foreach (MemberInfo mi in this.mapping.GetMappedMembers(entity)) { if (!this.mapping.IsAssociationRelationship(entity, mi)) { Expression me = this.GetMemberExpression(root, entity, mi); if (me != null) { assignments.Add(new EntityAssignment(mi, me)); } } } return new EntityExpression(entity, BuildEntityExpression(entity, assignments)); }
public override Expression GetDeleteExpression(IMappedEntity entity, Expression instance, LambdaExpression deleteCheck) { IdentifiableExpression table = new IdentifiableExpression(new IdentifiableAlias(), entity, this.mapping.GetTableName(entity)); Expression where = null; if (instance != null) { where = this.GetIdentityCheck(table, entity, instance); } if (deleteCheck != null) { Expression row = this.GetEntityExpression(table, entity); Expression pred = DbExpressionReplacer.Replace(deleteCheck.Body, deleteCheck.Parameters[0], row); where = (where != null) ? where.And(pred) : pred; } return new DeleteCommandExpression(table, where); }
protected override Expression BuildEntityExpression(IMappedEntity entity, IList<EntityAssignment> assignments) { if (entity.EntityType.Equals(typeof(IIdentifiable)) || entity.EntityType.Equals(typeof(IThing))) { return base.BuildEntityExpression(new OMappingEntity(typeof(_Thing), _repoType), assignments); // switch it to a constructable type } else if (entity.EntityType.Equals(typeof(IAssociation))) { return base.BuildEntityExpression(new OMappingEntity(typeof(_Association), _repoType), assignments); // switch it to a constructable type } else return base.BuildEntityExpression(entity, assignments); }
/// <summary> /// Determines if a property can be part of an update operation /// </summary> /// <param name="entity"></param> /// <param name="member"></param> /// <returns></returns> public virtual bool IsUpdatable(IMappedEntity entity, MemberInfo member) { return !this.IsPrimaryKey(entity, member); }
public abstract bool IsRelationshipSource(IMappedEntity entity, MemberInfo member);
/// <summary> /// Determines if a property represents or is part of the entities unique identity (often primary key) /// </summary> /// <param name="member"></param> /// <returns></returns> public override bool IsPrimaryKey(IMappedEntity entity, MemberInfo member) { return false; }
/// <summary> /// Deterimines is a property is mapped onto a Field or relationship /// </summary> /// <param name="member"></param> /// <returns></returns> public virtual bool IsMapped(IMappedEntity entity, MemberInfo member) { return true; }
protected virtual Expression GetUpdateResult(IMappedEntity entity, Expression instance, LambdaExpression selector) { var tq = this.GetQueryExpression(entity); Expression where = this.GetIdentityCheck(tq.Select, entity, instance); Expression selection = DbExpressionReplacer.Replace(selector.Body, selector.Parameters[0], tq.Projector); IdentifiableAlias newAlias = new IdentifiableAlias(); var pc = FieldProjector.ProjectFields(this.translator.Linguist.Language, selection, null, newAlias, tq.Select.Alias); return new ProjectionExpression( new SelectExpression(newAlias, pc.Fields, tq.Select, where), pc.Projector, Aggregator.GetAggregator(selector.Body.Type, typeof(IEnumerable<>).MakeGenericType(selector.Body.Type)) ); }
/// <summary> /// The name of the corresponding database table /// </summary> /// <param name="rowType"></param> /// <returns></returns> public virtual string GetTableName(IMappedEntity entity) { return entity.StorageClass; }
/// <summary> /// The query language specific type for the Field /// </summary> /// <param name="member"></param> /// <returns></returns> public virtual StorageType GetFieldType(IMappedEntity entity, MemberInfo member) { string dbType = this.mapping.GetFieldDbType(entity, member); if (dbType != null) { return this.translator.Linguist.Language.TypeSystem.Parse(dbType); } return this.translator.Linguist.Language.TypeSystem.GetStorageType(TypeHelper.GetMemberType(member)); }
private bool IsNullRelationshipAssignment(IMappedEntity entity, EntityAssignment assignment) { if (this.mapping.IsRelationship(entity, assignment.Member)) { var cex = assignment.Expression as ConstantExpression; if (cex != null && cex.Value == null) return true; } return false; }
protected virtual Expression GetIdentityCheck(Expression root, IMappedEntity entity, Expression instance) { return this.mapping.GetMappedMembers(entity) .Where(m => this.mapping.IsPrimaryKey(entity, m)) .Select(m => this.GetMemberExpression(root, entity, m).Equal(Expression.MakeMemberAccess(instance, m))) .Aggregate((x, y) => x.And(y)); }
/// <summary> /// Determines if a property is mapped onto a Field /// </summary> /// <param name="member"></param> /// <returns></returns> public virtual bool IsField(IMappedEntity entity, MemberInfo member) { //return this.mapping.IsMapped(entity, member) && this.translator.Linguist.Language.IsScalar(TypeHelper.GetMemberType(member)); return this.IsMapped(entity, member); }
/// <summary> /// The type of the entity on the other side of the relationship /// </summary> /// <param name="member"></param> /// <returns></returns> public virtual IMappedEntity GetRelatedEntity(IMappedEntity entity, MemberInfo member) { Type relatedType = TypeHelper.GetElementType(TypeHelper.GetMemberType(member)); return this.GetEntity(relatedType, entity.RepositoryType); }
protected virtual Expression GetInsertResult(IMappedEntity entity, Expression instance, LambdaExpression selector, Dictionary<MemberInfo, Expression> map) { var IdentifiableAlias = new IdentifiableAlias(); var tex = new IdentifiableExpression(IdentifiableAlias, entity, this.mapping.GetTableName(entity)); var aggregator = Aggregator.GetAggregator(selector.Body.Type, typeof(IEnumerable<>).MakeGenericType(selector.Body.Type)); Expression where; DeclarationCommand genIdCommand = null; var generatedIds = this.mapping.GetMappedMembers(entity).Where(m => this.mapping.IsPrimaryKey(entity, m) && this.mapping.IsGenerated(entity, m)).ToList(); if (generatedIds.Count > 0) { if (map == null || !generatedIds.Any(m => map.ContainsKey(m))) { var localMap = new Dictionary<MemberInfo, Expression>(); genIdCommand = this.GetGeneratedIdCommand(entity, generatedIds.ToList(), localMap); map = localMap; } // is this just a retrieval of one generated id member? var mex = selector.Body as MemberExpression; if (mex != null && this.mapping.IsPrimaryKey(entity, mex.Member) && this.mapping.IsGenerated(entity, mex.Member)) { if (genIdCommand != null) { // just use the select from the genIdCommand return new ProjectionExpression( genIdCommand.Source, new FieldExpression(mex.Type, genIdCommand.Variables[0].QueryType, genIdCommand.Source.Alias, genIdCommand.Source.Fields[0].Name), aggregator ); } else { IdentifiableAlias alias = new IdentifiableAlias(); var colType = this.GetFieldType(entity, mex.Member); return new ProjectionExpression( new SelectExpression(alias, new[] { new FieldDeclaration("", map[mex.Member], colType) }, null, null), new FieldExpression(TypeHelper.GetMemberType(mex.Member), colType, alias, ""), aggregator ); } } where = generatedIds.Select((m, i) => this.GetMemberExpression(tex, entity, m).Equal(map[m]) ).Aggregate((x, y) => x.And(y)); } else { where = this.GetIdentityCheck(tex, entity, instance); } Expression typeProjector = this.GetEntityExpression(tex, entity); Expression selection = DbExpressionReplacer.Replace(selector.Body, selector.Parameters[0], typeProjector); IdentifiableAlias newAlias = new IdentifiableAlias(); var pc = FieldProjector.ProjectFields(this.translator.Linguist.Language, selection, null, newAlias, IdentifiableAlias); var pe = new ProjectionExpression( new SelectExpression(newAlias, pc.Fields, tex, where), pc.Projector, aggregator ); if (genIdCommand != null) { return new BlockCommandExpression(genIdCommand, pe); } return pe; }
/// <summary> /// Determines if the property is an assocation relationship. /// </summary> /// <param name="entity"></param> /// <param name="member"></param> /// <returns></returns> public virtual bool IsAssociationRelationship(IMappedEntity entity, MemberInfo member) { if (member.MemberType == MemberTypes.Field) return IsAssociationType(((FieldInfo)member).FieldType); else return IsAssociationType(((PropertyInfo)member).PropertyType); }
public override void MapBucketExport(TrelloCaseEntityBucket bucket, IMappedEntity existing) { }
/// <summary> /// Determines if a property is generated on the server during insert /// </summary> /// <param name="member"></param> /// <returns></returns> public virtual bool IsGenerated(IMappedEntity entity, MemberInfo member) { return false; }
public override void SaveBucketExport(TrelloCaseEntityBucket bucket, IMappedEntity existing, String operation) { }
public override bool IsModified(IMappedEntity entity, object instance, object original) { foreach (var mi in this.GetMappedMembers(entity)) { if (this.IsField(entity, mi)) { if (!object.Equals(mi.GetValue(instance), mi.GetValue(original))) return true; } } return false; }
public override IEnumerable<IdentifiableInfo> GetDependingEntities(IMappedEntity entity, object instance) { foreach (var mi in this.GetMappedMembers(entity)) { if (this.IsRelationship(entity, mi) && this.IsRelationshipTarget(entity, mi)) { IMappedEntity relatedEntity = this.GetRelatedEntity(entity, mi); var value = mi.GetValue(instance); if (value != null) { var list = value as IList; if (list != null) { foreach (var item in list) { if (item != null) { yield return new IdentifiableInfo(item, relatedEntity); } } } else { yield return new IdentifiableInfo(value, relatedEntity); } } } } }
public override bool IsRelationship(IMappedEntity entity, MemberInfo member) { return this.IsAssociationRelationship(entity, member); }
/// <summary> /// The type declaration for the Field in the provider's syntax /// </summary> /// <param name="entity"></param> /// <param name="member"></param> /// <returns>a string representing the type declaration or null</returns> public virtual string GetFieldDbType(IMappedEntity entity, MemberInfo member) { return null; }
public abstract bool IsRelationshipTarget(IMappedEntity entity, MemberInfo member);
/// <summary> /// The name of the corresponding table Field /// </summary> /// <param name="member"></param> /// <returns></returns> public virtual string GetFieldName(IMappedEntity entity, MemberInfo member) { return member.Name; }
/// <summary> /// Returns the key members on this side of the association /// </summary> /// <param name="entity"></param> /// <param name="member"></param> /// <returns></returns> public virtual IEnumerable<MemberInfo> GetAssociationKeyMembers(IMappedEntity entity, MemberInfo member) { return new MemberInfo[] { }; }
/// <summary> /// A sequence of all the mapped members /// </summary> /// <param name="rowType"></param> /// <returns></returns> public override IEnumerable<MemberInfo> GetMappedMembers(IMappedEntity entity) { //Type type = entity.ElementType.IsInterface ? entity.EntityType : entity.ElementType; Type type = entity.EntityType; HashSet<MemberInfo> members = new HashSet<MemberInfo>(type.GetFields().Cast<MemberInfo>().Where(m => this.IsMapped(entity, m))); members.UnionWith(type.GetProperties().Cast<MemberInfo>().Where(m => this.IsMapped(entity, m))); return members.OrderBy(m => m.Name); }
private OUpdateCommandExpression CreateUpdateCommand(IMappedEntity entity, Expression instance, LambdaExpression selector, Expression where, IEnumerable<FieldAssignment> assignments) { var identifiableAlias = new IdentifiableAlias(); var tex = new IdentifiableExpression(identifiableAlias, entity, ((OTypeMapping)this.Mapping).GetTableName(entity)); Expression typeProjector = this.GetEntityExpression(tex, entity); Expression selection = DbExpressionReplacer.Replace(selector.Body, selector.Parameters[0], typeProjector); IdentifiableAlias newAlias = new IdentifiableAlias(); var pc = FieldProjector.ProjectFields(this.Translator.Linguist.Language, selection, null, newAlias, identifiableAlias); return new OUpdateCommandExpression(tex, where, assignments, pc.Projector); }
public override object GetPrimaryKey(IMappedEntity entity, object instance) { object firstKey = null; List<object> keys = null; foreach (var mi in this.GetPrimaryKeyMembers(entity)) { if (firstKey == null) { firstKey = mi.GetValue(instance); } else { if (keys == null) { keys = new List<object>(); keys.Add(firstKey); } keys.Add(mi.GetValue(instance)); } } if (keys != null) { return new CompoundKey(keys.ToArray()); } return firstKey; }
public override Expression GetPrimaryKeyQuery(IMappedEntity entity, Expression source, Expression[] keys) { // make predicate ParameterExpression p = Expression.Parameter(entity.EntityType, "p"); Expression pred = null; var idMembers = this.GetPrimaryKeyMembers(entity).ToList(); if (idMembers.Count != keys.Length) { throw new InvalidOperationException("Incorrect number of primary key values"); } for (int i = 0, n = keys.Length; i < n; i++) { MemberInfo mem = idMembers[i]; Type memberType = TypeHelper.GetMemberType(mem); if (keys[i] != null && TypeHelper.GetNonNullableType(keys[i].Type) != TypeHelper.GetNonNullableType(memberType)) { throw new InvalidOperationException("Primary key value is wrong type"); } Expression eq = Expression.MakeMemberAccess(p, mem).Equal(keys[i]); pred = (pred == null) ? eq : pred.And(eq); } var predLambda = Expression.Lambda(pred, p); return Expression.Call(typeof(Queryable), "SingleOrDefault", new Type[] { entity.EntityType }, source, predLambda); }
public ConcreteEntityWithDependancyInjection(IMappedEntity typeToResolve) { }