public override Expression GetDeleteExpression(MappingEntity entity, Expression instance, LambdaExpression deleteCheck)
        {
            var commands = new List <Expression>();
            var tables   = this.mapping.GetTables(entity);

            if (tables.Count < 2)
            {
                return(base.GetDeleteExpression(entity, instance, deleteCheck));
            }

            foreach (var table in this.GetDependencyOrderedTables(entity).Reverse())
            {
                var tex = new DbTableExpression(new TableAlias(), entity, this.mapping.GetTableName(table));
                var where = this.GetIdentityCheck(tex, entity, instance);

                commands.Add(new DbDeleteCommand(tex, where));
            }

            var block = new DbBlockCommand(commands) as Expression;

            if (deleteCheck != null)
            {
                return(new DbIFCommand(this.GetEntityStateTest(entity, instance, deleteCheck), block, null));
            }

            return(block);
        }
        protected override Expression VisitTable(DbTableExpression table)
        {
            var newAlias = new TableAlias();

            this.map[table.Alias] = newAlias;

            return(new DbTableExpression(newAlias, table.Entity, table.Name));
        }
        protected DbInsertCommand UpdateInsert(DbInsertCommand insert, DbTableExpression table, IEnumerable <DbColumnAssignment> assignments)
        {
            if (table != insert.Table || assignments != insert.Assignments)
            {
                return(new DbInsertCommand(table, assignments));
            }

            return(insert);
        }
        protected DbDeleteCommand UpdateDelete(DbDeleteCommand delete, DbTableExpression table, Expression where)
        {
            if (table != delete.Table || where != delete.Where)
            {
                return(new DbDeleteCommand(table, where));
            }

            return(delete);
        }
        protected DbUpdateCommand UpdateUpdate(DbUpdateCommand update, DbTableExpression table, Expression where, IEnumerable <DbColumnAssignment> assignments)
        {
            if (table != update.Table || where != update.Where || assignments != update.Assignments)
            {
                return(new DbUpdateCommand(table, where, assignments));
            }

            return(update);
        }
        private DbCommandExpression GetDependentGeneratedVariableDeclaration(MappingEntity entity, MappingTable table, List <MemberInfo> members, Expression instance, Dictionary <MemberInfo, Expression> map)
        {
            var genIdCommand = null as DbDeclarationCommand;
            var generatedIds = this.mapping.GetMappedMembers(entity).Where(m => this.mapping.IsPrimaryKey(entity, m) && this.mapping.IsGenerated(entity, m)).ToList();

            if (generatedIds.Count > 0)
            {
                genIdCommand = this.GetGeneratedIdCommand(entity, members, map);

                if (members.Count == generatedIds.Count)
                {
                    return(genIdCommand);
                }
            }

            members = members.Except(generatedIds).ToList();

            var tableAlias = new TableAlias();
            var tex        = new DbTableExpression(tableAlias, entity, this.mapping.GetTableName(table));

            var where = null as Expression;

            if (generatedIds.Count > 0)
            {
                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);
            }

            var selectAlias = new TableAlias();
            var columns     = new List <DbColumnDeclaration>();
            var variables   = new List <DbVariableDeclaration>();

            foreach (var mi in members)
            {
                var col = (DbColumnExpression)this.GetMemberExpression(tex, entity, mi);

                columns.Add(new DbColumnDeclaration(this.mapping.GetColumnName(entity, mi), col, col.QueryType));

                var vcol = new DbColumnExpression(col.Type, col.QueryType, selectAlias, col.Name);

                variables.Add(new DbVariableDeclaration(mi.Name, col.QueryType, vcol));
                map.Add(mi, new DbVariableExpression(mi.Name, col.Type, col.QueryType));
            }

            var genMembersCommand = new DbDeclarationCommand(variables, new DbSelectExpression(selectAlias, columns, tex, where));

            if (genIdCommand != null)
            {
                return(new DbBlockCommand(genIdCommand, genMembersCommand));
            }

            return(genMembersCommand);
        }
        public override Expression GetUpdateExpression(MappingEntity entity, Expression instance, LambdaExpression updateCheck, LambdaExpression selector, Expression exp)
        {
            var tableAlias = new TableAlias();
            var table      = new DbTableExpression(tableAlias, entity, this.mapping.GetTableName(entity));

            var where = this.GetIdentityCheck(table, entity, instance);

            if (updateCheck != null)
            {
                var typeProjector = this.GetEntityExpression(table, entity);
                var pred          = DbExpressionReplacer.Replace(updateCheck.Body, updateCheck.Parameters[0], typeProjector);

                if (pred != null)
                {
                    where = where.And(pred);
                }
            }

            var assignments = this.GetColumnAssignments(table, instance, entity, (e, m) => this.mapping.IsUpdatable(e, m));

            var update = new DbUpdateCommand(table, where, assignments);

            if (selector != null)
            {
                return(new DbBlockCommand
                       (
                           update,
                           new DbIFCommand
                           (
                               this.translator.Linguist.Language.GetRowsAffectedExpression(update).GreaterThan(Expression.Constant(0)),
                               this.GetUpdateResult(entity, instance, selector),
                               exp
                           )
                       ));
            }
            else if (exp != null)
            {
                return(new DbBlockCommand
                       (
                           update,
                           new DbIFCommand
                           (
                               this.translator.Linguist.Language.GetRowsAffectedExpression(update).LessThanOrEqual(Expression.Constant(0)),
                               exp,
                               null
                           )
                       ));
            }
            else
            {
                return(update);
            }
        }
        internal override DbProjectionExpression GetQueryExpression(MappingEntity entity)
        {
            var tableAlias  = new TableAlias();
            var selectAlias = new TableAlias();
            var table       = new DbTableExpression(tableAlias, entity, this.mapping.GetTableName(entity));

            var projector = this.GetEntityExpression(table, entity) as Expression;
            var pc        = DbColumnProjector.ProjectColumns(this.translator.Linguist.Language, projector, null, selectAlias, tableAlias);

            var proj = new DbProjectionExpression(new DbSelectExpression(selectAlias, pc.Columns, table, null), pc.Projector);

            return(this.Translator.Police.ApplyPolicy(proj, entity.ElementType.GetTypeInfo()) as DbProjectionExpression);
        }
        public override Expression GetUpdateExpression(MappingEntity entity, Expression instance, LambdaExpression updateCheck, LambdaExpression selector, Expression exp)
        {
            var tables = this.mapping.GetTables(entity);

            if (tables.Count < 2)
            {
                return(base.GetUpdateExpression(entity, instance, updateCheck, selector, exp));
            }

            var commands = new List <Expression>();

            foreach (var table in this.GetDependencyOrderedTables(entity))
            {
                var tex         = new DbTableExpression(new TableAlias(), entity, this.mapping.GetTableName(table));
                var assignments = this.GetColumnAssignments(tex, instance, entity, (e, m) => this.mapping.GetAlias(e, m) == this.mapping.GetAlias(table) && this.mapping.IsUpdatable(e, m), null);
                var where = this.GetIdentityCheck(tex, entity, instance);

                commands.Add(new DbUpdateCommand(tex, where, assignments));
            }

            if (selector != null)
            {
                commands.Add(new DbIFCommand
                             (
                                 this.Translator.Linguist.Language.GetRowsAffectedExpression(commands[commands.Count - 1]).GreaterThan(Expression.Constant(0)),
                                 this.GetUpdateResult(entity, instance, selector), exp)
                             );
            }
            else if (exp != null)
            {
                commands.Add(new DbIFCommand
                             (
                                 this.Translator.Linguist.Language.GetRowsAffectedExpression(commands[commands.Count - 1]).LessThanOrEqual(Expression.Constant(0)),
                                 exp,
                                 null
                             ));
            }

            var block = new DbBlockCommand(commands) as Expression;

            if (updateCheck != null)
            {
                return(new DbIFCommand(this.GetEntityStateTest(entity, instance, updateCheck), block, null));
            }

            return(block);
        }
        public override Expression GetInsertExpression(MappingEntity entity, Expression instance, LambdaExpression selector)
        {
            var tableAlias  = new TableAlias();
            var table       = new DbTableExpression(tableAlias, entity, this.mapping.GetTableName(entity));
            var assignments = this.GetColumnAssignments(table, instance, entity, (e, m) => !(mapping.IsGenerated(e, m) || mapping.IsReadOnly(e, m)));

            if (selector != null)
            {
                return(new DbBlockCommand
                       (
                           new DbInsertCommand(table, assignments),
                           this.GetInsertResult(entity, instance, selector, null)
                       ));
            }

            return(new DbInsertCommand(table, assignments));
        }
        public override Expression GetInsertExpression(MappingEntity entity, Expression instance, LambdaExpression selector)
        {
            var tables = this.mapping.GetTables(entity);

            if (tables.Count < 2)
            {
                return(base.GetInsertExpression(entity, instance, selector));
            }

            var commands = new List <Expression>();

            var map    = this.GetDependentGeneratedColumns(entity);
            var vexMap = new Dictionary <MemberInfo, Expression>();

            foreach (var table in this.GetDependencyOrderedTables(entity))
            {
                var tableAlias  = new TableAlias();
                var tex         = new DbTableExpression(tableAlias, entity, this.mapping.GetTableName(table));
                var assignments = this.GetColumnAssignments
                                  (
                    tex, instance, entity,
                    (e, m) => this.mapping.GetAlias(e, m) == this.mapping.GetAlias(table) && !this.mapping.IsGenerated(e, m),
                    vexMap
                                  );
                var totalAssignments = assignments.Concat(this.GetRelatedColumnAssignments(tex, entity, table, vexMap));

                commands.Add(new DbInsertCommand(tex, totalAssignments));

                if (map.TryGetValue(this.mapping.GetAlias(table), out List <MemberInfo> members))
                {
                    var d = this.GetDependentGeneratedVariableDeclaration(entity, table, members, instance, vexMap);

                    commands.Add(d);
                }
            }

            if (selector != null)
            {
                commands.Add(this.GetInsertResult(entity, instance, selector, vexMap));
            }

            return(new DbBlockCommand(commands));
        }
        private Expression GetIdentityCheck(DbTableExpression root, MappingEntity entity, Expression instance, MappingTable table)
        {
            if (this.mapping.IsExtensionTable(table))
            {
                var keyColNames    = this.mapping.GetExtensionKeyColumnNames(table).ToArray();
                var relatedMembers = this.mapping.GetExtensionRelatedMembers(table).ToArray();
                var where = null as Expression;

                for (int i = 0, n = keyColNames.Length; i < n; i++)
                {
                    var relatedMember = relatedMembers[i];
                    var cex           = new DbColumnExpression(TypeHelper.GetMemberType(relatedMember), this.GetColumnType(entity, relatedMember), root.Alias, keyColNames[n]);
                    var nex           = this.GetMemberExpression(instance, entity, relatedMember);
                    var eq            = cex.Equal(nex);

                    where = (where != null) ? where.And(eq) : where;
                }

                return(where);
            }

            return(base.GetIdentityCheck(root, entity, instance));
        }
        public override Expression GetDeleteExpression(MappingEntity entity, Expression instance, LambdaExpression deleteCheck)
        {
            var table = new DbTableExpression(new TableAlias(), entity, this.mapping.GetTableName(entity));

            var where = null as Expression;

            if (instance != null)
            {
                where = this.GetIdentityCheck(table, entity, instance);
            }

            if (deleteCheck != null)
            {
                var row  = this.GetEntityExpression(table, entity);
                var pred = DbExpressionReplacer.Replace(deleteCheck.Body, deleteCheck.Parameters[0], row);

                if (pred != null)
                {
                    where = (where != null) ? where.And(pred) : pred;
                }
            }

            return(new DbDeleteCommand(table, where));
        }
        internal override DbProjectionExpression GetQueryExpression(MappingEntity entity)
        {
            var tables = this.mapping.GetTables(entity);

            if (tables.Count <= 1)
            {
                return(base.GetQueryExpression(entity));
            }

            var aliases   = new Dictionary <string, TableAlias>();
            var rootTable = tables.Single(ta => !this.mapping.IsExtensionTable(ta));
            var tex       = new DbTableExpression(new TableAlias(), entity, this.mapping.GetTableName(rootTable));

            aliases.Add(this.mapping.GetAlias(rootTable), tex.Alias);

            var source = tex as Expression;

            foreach (var table in tables.Where(t => this.mapping.IsExtensionTable(t)))
            {
                var joinedTableAlias = new TableAlias();
                var extensionAlias   = this.mapping.GetAlias(table);

                aliases.Add(extensionAlias, joinedTableAlias);

                var keyColumns     = this.mapping.GetExtensionKeyColumnNames(table).ToList();
                var relatedMembers = this.mapping.GetExtensionRelatedMembers(table).ToList();
                var relatedAlias   = this.mapping.GetExtensionRelatedAlias(table);

                aliases.TryGetValue(relatedAlias, out TableAlias relatedTableAlias);

                var joinedTex = new DbTableExpression(joinedTableAlias, entity, this.mapping.GetTableName(table));
                var cond      = null as Expression;

                for (int i = 0, n = keyColumns.Count; i < n; i++)
                {
                    var memberType    = TypeHelper.GetMemberType(relatedMembers[i]);
                    var colType       = this.GetColumnType(entity, relatedMembers[i]);
                    var relatedColumn = new DbColumnExpression(memberType, colType, relatedTableAlias, this.mapping.GetColumnName(entity, relatedMembers[i]));
                    var joinedColumn  = new DbColumnExpression(memberType, colType, joinedTableAlias, keyColumns[i]);
                    var eq            = joinedColumn.Equal(relatedColumn);

                    cond = (cond != null) ? cond.And(eq) : eq;
                }

                source = new DbJoinExpression(JoinType.SingletonLeftOuter, source, joinedTex, cond);
            }

            var columns = new List <DbColumnDeclaration>();

            this.GetColumns(entity, aliases, columns);

            var root            = new DbSelectExpression(new TableAlias(), columns, source, null);
            var existingAliases = aliases.Values.ToArray();

            var projector   = this.GetEntityExpression(root, entity) as Expression;
            var selectAlias = new TableAlias();
            var pc          = DbColumnProjector.ProjectColumns(this.Translator.Linguist.Language, projector, null, selectAlias, root.Alias);
            var proj        = new DbProjectionExpression
                              (
                new DbSelectExpression(selectAlias, pc.Columns, root, null),
                pc.Projector
                              );

            return(this.Translator.Police.ApplyPolicy(proj, entity.ElementType.GetTypeInfo()) as DbProjectionExpression);
        }
 protected virtual bool CompareTable(DbTableExpression a, DbTableExpression b)
 {
     return(a.Name == b.Name);
 }
 protected virtual Expression VisitTable(DbTableExpression table)
 {
     return(table);
 }
Exemple #17
0
 public DbUpdateCommand(DbTableExpression table, Expression where, IEnumerable <DbColumnAssignment> assignments) : base(DbExpressionType.Update, typeof(int))
 {
     this.Table       = table;
     this.Where       = where;
     this.Assignments = assignments.ToReadOnly();
 }
Exemple #18
0
 public DbDeleteCommand(DbTableExpression table, Expression where) : base(DbExpressionType.Delete, typeof(int))
 {
     this.Table = table;
     this.Where = where;
 }
        protected virtual Expression GetInsertResult(MappingEntity entity, Expression instance, LambdaExpression selector, Dictionary <MemberInfo, Expression> map)
        {
            var tableAlias = new TableAlias();
            var tex        = new DbTableExpression(tableAlias, entity, this.mapping.GetTableName(entity));
            var aggregator = DbAggregator.GetAggregator(selector.Body.Type, typeof(IEnumerable <>).MakeGenericType(selector.Body.Type));

            var where = null as Expression;
            var genIdCommand = null as DbDeclarationCommand;
            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;
                }

                if (selector.Body is MemberExpression mex && this.mapping.IsPrimaryKey(entity, mex.Member) && this.mapping.IsGenerated(entity, mex.Member))
                {
                    if (genIdCommand != null)
                    {
                        return(new DbProjectionExpression
                               (
                                   genIdCommand.Source,
                                   new DbColumnExpression(mex.Type, genIdCommand.Variables[0].QueryType, genIdCommand.Source.Alias, genIdCommand.Source.Columns[0].Name),
                                   aggregator
                               ));
                    }
                    else
                    {
                        var alias   = new TableAlias();
                        var colType = this.GetColumnType(entity, mex.Member);

                        return(new DbProjectionExpression
                               (
                                   new DbSelectExpression(alias, new[] { new DbColumnDeclaration(string.Empty, map[mex.Member], colType) }, null, null),
                                   new DbColumnExpression(TypeHelper.GetMemberType(mex.Member), colType, alias, string.Empty),
                                   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);
            }

            var typeProjector = this.GetEntityExpression(tex, entity);
            var selection     = DbExpressionReplacer.Replace(selector.Body, selector.Parameters[0], typeProjector);
            var newAlias      = new TableAlias();
            var pc            = DbColumnProjector.ProjectColumns(this.translator.Linguist.Language, selection, null, newAlias, tableAlias);
            var pe            = new DbProjectionExpression
                                (
                new DbSelectExpression(newAlias, pc.Columns, tex, where),
                pc.Projector,
                aggregator
                                );

            if (genIdCommand != null)
            {
                return(new DbBlockCommand(genIdCommand, pe));
            }

            return(pe);
        }
Exemple #20
0
 public DbInsertCommand(DbTableExpression table, IEnumerable <DbColumnAssignment> assignments) : base(DbExpressionType.Insert, typeof(int))
 {
     this.Table       = table;
     this.Assignments = assignments.ToReadOnly();
 }
Exemple #21
0
        protected override Expression VisitTable(DbTableExpression table)
        {
            this.aliases.Add(table.Alias);

            return(table);
        }