/// <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 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); }