Example #1
0
        /// <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);
        }
Example #2
0
        /// <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);
        }
Example #3
0
        /// <summary>
        /// Returns the id string associated with the given entity, generating it if needed.
        /// Returns null if it cannot be generated if needed or, optionally, raises an exception.
        /// Optionally captures the state in a new record if it did not exist previously.
        /// </summary>
        string LocateId(MetaEntity meta, bool raise, bool captureRecord)
        {
            var str = meta.CollectionId;

            if (str != null)
            {
                return(str);
            }

            var obj = meta.Entity;

            if (obj == null)
            {
                throw new InvalidOperationException("MetaEntity '{0}' is invalid.".FormatWith(meta));
            }

            var record = meta.Record; if (record == null)

            {
                record = new Core.Concrete.Record(_Master.Schema);
                _Master.WriteRecord(obj, record);
            }
            str = GenerateId(record, raise);

            if (meta.Record == null)
            {
                if (captureRecord)
                {
                    meta.Record = record;
                }
                else
                {
                    record.Dispose();
                }
            }

            return(str);
        }
Example #4
0
        /// <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);
        }