/// <summary> /// Initializes a new instance. /// </summary> internal ChangeEntry(ChangeType changeType, IUberMap map, object entity) { UberMap = map; ChangeType = changeType; Entity = entity; MetaEntity = MetaEntity.Locate(entity); }
/// <summary> /// Disposes this instance. /// </summary> internal void Dispose() { try { if (_Items != null) { Clear(); } } catch { } _Items = null; _Master = null; }
/// <summary> /// Locates the map registered to manage the entities of the given type, or tries to /// create a new weak map otherwise if possible. Returns null if no map is found and /// no weak map can be created. /// </summary> /// <param name="type">The type of the entities managed by the map to locate.</param> /// <param name="table">A dynamic lambda expression that resolves into the name of the /// primary table, used when creating a weak map. If null the a number of suitable /// names are tried based upon the name of the type.</param> /// <returns>The requested map, or null.</returns> internal IUberMap LocateUberMap(Type type, Func <dynamic, object> table = null) { if (IsDisposed) { throw new ObjectDisposedException(this.ToString()); } if (type == null) { throw new ArgumentNullException("type", "Type cannot be null."); } lock (ProxyGenerator.ProxyLock) { var holder = ProxyGenerator.ProxyHolders.Find(type); if (holder != null) { type = holder.ProxyType.BaseType; } } IUberMap map = null; lock (MasterLock) { map = _UberMaps.Find(type); if (map == null) { var generic = typeof(DataMap <>); var concrete = generic.MakeGenericType(new Type[] { type }); var cons = concrete.GetConstructor(new Type[] { typeof(DataRepository), typeof(string) }); if (table != null) { var name = DynamicInfo.ParseName(table); map = (IUberMap)cons.Invoke(new object[] { this, name }); map.IsWeakMap = false; } else if (WeakMapsEnabled) { var name = Uber.FindTableName(Link, type.Name); if (name != null) { map = (IUberMap)cons.Invoke(new object[] { this, name }); map.IsWeakMap = true; } } } } return(map); }
/// <summary> /// Returns a list containing the dependencies of the given entity whose mode match the /// one given. /// </summary> internal static List <object> GetDependencies(this MetaEntity meta, IUberMap map, MemberDependencyMode mode) { var list = new List <object>(); var entity = meta.Entity; if (entity != null && map != null && !map.IsDisposed) { foreach (var member in (IEnumerable <IUberMember>)map.Members) { if (member.DependencyMode != mode) { continue; } if (!member.ElementInfo.CanRead) { continue; } var source = member.ElementInfo.GetValue(entity); if (source == null) { continue; } var type = source.GetType(); if (!type.IsListAlike()) { if (type.IsClass) { list.Add(source); } } else { type = type.ListAlikeMemberType(); if (type.IsClass) { var iter = source as IEnumerable; foreach (var item in iter) { if (item != null) { list.Add(item); } } } } } } return(list); }
/// <summary> /// Generates a delete core command for the given entity, or returns null if such /// command cannot be generated for whatever reasons. /// </summary> internal static IDeleteCommand GenerateDeleteCommand(this IUberMap map, object entity) { if (entity == null) { return(null); } if (map == null || map.IsDisposed || !map.IsValidated) { return(null); } IDeleteCommand cmd = null; MetaEntity meta = MetaEntity.Locate(entity, create: true); if (meta.Record == null) { var record = new Core.Concrete.Record(map.Schema); map.WriteRecord(entity, record); meta.Record = record; } var id = map.ExtractId(meta.Record); if (id != null) { cmd = map.Link.Engine.CreateDeleteCommand(map.Link, x => map.Table); if (map.Discriminator != null) { cmd.Where(map.Discriminator); } var tag = new DynamicNode.Argument("x"); for (int i = 0; i < id.Count; i++) { var left = new DynamicNode.GetMember(tag, id.Schema[i].ColumnName); var bin = new DynamicNode.Binary(left, ExpressionType.Equal, id[i]); cmd.Where(x => bin); left.Dispose(); bin.Dispose(); } tag.Dispose(); id.Dispose(); } return(cmd); }
/// <summary> /// Generates an insert core command for the given entity, or returns null if such /// command cannot be generated for whatever reasons. /// </summary> internal static IInsertCommand GenerateInsertCommand(this IUberMap map, object entity) { if (entity == null) { return(null); } if (map == null || map.IsDisposed || !map.IsValidated) { return(null); } IInsertCommand cmd = null; int num = map.Schema.Count(x => !x.IsReadOnlyColumn); if (num != 0) { cmd = map.Link.Engine.CreateInsertCommand(map.Link, x => map.Table); var tag = new DynamicNode.Argument("x"); var rec = new Core.Concrete.Record(map.Schema); map.WriteRecord(entity, rec); for (int i = 0; i < rec.Count; i++) { if (rec.Schema[i].IsReadOnlyColumn) { continue; } var node = new DynamicNode.SetMember(tag, rec.Schema[i].ColumnName, rec[i]); cmd.Columns(x => node); node.Dispose(); } tag.Dispose(); rec.Dispose(); } return(cmd); }
/// <summary> /// Initializes a new instance. /// </summary> internal MetaEntityCollection(IUberMap map) { _Master = map; }
/// <summary> /// Generates an update core command for the given entity, or returns null if such /// command cannot be generated for whatever reasons. /// </summary> internal static IUpdateCommand GenerateUpdateCommand(this IUberMap map, object entity) { if (entity == null) { return(null); } if (map == null || map.IsDisposed || !map.IsValidated) { return(null); } IUpdateCommand cmd = null; MetaEntity meta = MetaEntity.Locate(entity, create: true); if (meta.Record == null) { var record = new Core.Concrete.Record(map.Schema); map.WriteRecord(entity, record); meta.Record = record; } var changes = meta.GetRecordChanges(); if (changes == null) { return(null); } var num = changes.Schema.Count(x => !x.IsReadOnlyColumn); if (num != 0) { var id = map.ExtractId(meta.Record); if (id != null) { cmd = map.Link.Engine.CreateUpdateCommand(map.Link, x => map.Table); if (map.Discriminator != null) { cmd.Where(map.Discriminator); } var tag = new DynamicNode.Argument("x"); for (int i = 0; i < id.Count; i++) { var left = new DynamicNode.GetMember(tag, id.Schema[i].ColumnName); var bin = new DynamicNode.Binary(left, ExpressionType.Equal, id[i]); cmd.Where(x => bin); left.Dispose(); bin.Dispose(); } for (int i = 0; i < changes.Count; i++) { if (changes.Schema[i].IsReadOnlyColumn) { continue; } var node = new DynamicNode.SetMember(tag, changes.Schema[i].ColumnName, changes[i]); cmd.Columns(x => node); node.Dispose(); } tag.Dispose(); id.Dispose(); } } changes.Dispose(disposeSchema: true); return(cmd); }