Ejemplo n.º 1
0
        /// <summary>
        /// 使用临时表形式,生成删除若干数据对象(多个主键)语句,
        /// 要求数据库拥有<see cref="EDbCapable.TemporaryTable"/>
        /// 和<see cref="EDbCapable.ModifyJoin"/>两个特性。
        /// </summary>
        /// <param name="context">生成上下文。</param>
        /// <returns>语句片段。</returns>
        protected virtual SqlFragment GenerateForInsertTempTable(GenerateContext context)
        {
            var data        = (GenerateDataForInsert)context.Data;
            var members     = data.GetTables().SelectMany(a => a.Members);
            var createtable = new CreateTempTableFragment(context, members);
            var temptable   = createtable.Table;

            var insert   = new InsertValueFragment(context, temptable, data.CommitObject, data.Items);
            var mainunit = (CommitKeyUnit)data.MainUnit;

            mainunit.Keys.ForEach(member => GenerateForInsertMembers(insert, member));
            data.CommitObject.Parent = insert;
            foreach (var unit in data.GetUnits())
            {
                unit.Members.ForEach(member => GenerateForInsertMembers(insert, member));
            }
            var block = new BlockFragment(context, createtable, insert);

            InsertFragment mainInsert = null;

            foreach (var unit in data.GetUnits().OfType <CommitKeyUnit>())
            {
                mainInsert = context.Insert(temptable, unit);
                block.Add(mainInsert);
            }
            GenerateForInsertReturnStatement(context, block, mainInsert.Target, temptable);
            return(block);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 使用临时表形式,生成插入单个数据对象语句,对象的主键是标识列,
        /// 要求数据库拥有<see cref="EDbCapable.TemporaryTable"/>
        /// 和<see cref="EDbCapable.ModifyJoin"/>两个特性。
        /// </summary>
        /// <param name="context">生成上下文。</param>
        /// <returns>语句片段。</returns>
        protected virtual SqlFragment GenerateForInsertIdentityTempTable(GenerateContext context)
        {
            var data     = (GenerateDataForInsert)context.Data;
            var mainunit = (CommitIdentityUnit)data.MainUnit;
            var members  = data.GetTables().SelectMany(a => a.Members).ToList();
            var rowindex = new ColumnMetadata(data.Table, SupportMembers.DbMembers.CustomRowIndex,
                                              members.Select(a => a.Name).Unique("RowIndex"));

            members.Add(rowindex);

            var createtable = new CreateTempTableFragment(context, members);
            var temptable   = createtable.Table;

            var insert = new InsertValueFragment(context, temptable, data.CommitObject, data.Items);

            data.CommitObject.Parent = insert;
            foreach (var unit in data.GetUnits())
            {
                unit.Members.ForEach(member => GenerateForInsertMembers(insert, member));
            }
            insert.SetValue(mainunit.Identity.Metadata, new SimpleFragment(context, "0"));
            var rowindexfragment = new RowIndexFragment(context);

            insert.SetValue(rowindex, rowindexfragment);


            TableFragment whileTarget = null;
            BlockFragment block       = null;

            if (context.Feature.HasCapable(EDbCapable.ExternalCompoundStatement))
            {
                var index         = new CreateVariableFragment(context, typeof(int));
                var whilefragment = GenerateForInsertIdentityMultipleWhile(context, mainunit, index,
                                                                           rowindex, createtable.Table, rowindexfragment, out whileTarget);
                block = new BlockFragment(context, index, createtable, insert,
                                          context.Assign(index.Name, context.StatementString("1")), whilefragment);
            }
            else
            {
                var repeatfragment = GenerateForInsertIdentityMultipleRepeat(context, mainunit,
                                                                             rowindex, createtable.Table, rowindexfragment, data, out whileTarget);
                block = new BlockFragment(context, createtable, insert, repeatfragment);
            }

            if (data.SubUnits != null)
            {
                foreach (var unit in data.SubUnits)
                {
                    block.Add(context.Insert(createtable.Table, unit));
                }
            }
            GenerateForInsertReturnStatement(context, block, whileTarget, createtable.Table);
            return(block);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 表达式生成删除语句。
        /// </summary>
        /// <param name="context">生成上下文。</param>
        /// <param name="content">生成表达式。</param>
        /// <returns>语句片段。</returns>
        protected virtual SqlFragment GenerateForDeleteStatement(GenerateContext context, DbExpression content)
        {
            var data   = (StatementContent)context.Data;
            var item   = data.ItemEpxression;
            var source = CreateSource(context, content) as QueryBaseFragment;

            var table = data.Table;

            if (table.InheritSets.Length == 0)
            {
                var target = (TableFragment)GetSource(context, item);
                target.Name = context.ConvertName(data.TargetName);
                var delete = new DeleteFragment(context, target)
                {
                    Where = source.Where,
                    Take  = source.Take
                };
                delete.AddSource(source.Sources.Where(a => a != target));
                return(delete);
            }
            else
            {
                var createtable = new CreateTempTableFragment(context, table.Keys);
                var insert      = new InsertFragment(context, createtable.Table, source);
                insert.Members.AddRange(createtable.Table.Members);
                foreach (var key in table.Keys)
                {
                    RetrievalMember(context, source, new DbMemberExpression(key.Member, item), null, false);
                }
                var block = new BlockFragment(context, createtable, insert);
                foreach (var subtable in table.InheritSets.Concat(new TableMetadata[] { table }).Reverse())
                {
                    var current   = new TableFragment(context, subtable);
                    var delete    = new DeleteFragment(context, current);
                    var temptable = new TemporaryTableFragment(context, table.Keys, createtable.Table.Name);
                    current.Join(temptable, table.Keys);
                    delete.AddSource(temptable);
                    block.Add(delete);
                }
                return(block);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 使用临时表形式,生成删除多个复合关系的语句,
        /// 要求数据库拥有<see cref="EDbCapable.TemporaryTable"/>
        /// 和<see cref="EDbCapable.ModifyJoin"/>两个特性。
        /// </summary>
        /// <param name="context">生成上下文。</param>
        /// <returns>语句片段。</returns>
        protected virtual SqlFragment GenerateForRelationDeleteTempTable(GenerateContext context)
        {
            var data            = (RelationContent)context.Data;
            var composite       = (CompositeNavigateMetadata)data.Items.Navigate;
            var createtemptable = new CreateTempTableFragment(context,
                                                              composite.Pairs.Select(a => a.ForeignKey).Concat(composite.CompositePairs.Select(a => a.ForeignKey)));

            var insert = new InsertValueFragment(context, createtemptable.Table, data.CommitObject, data.Items);

            composite.Pairs.ForEach(pair => insert.SetValue(pair.ForeignKey, data.CommitObject.GetMember(pair.PrincipalKey)));
            composite.CompositePairs.ForEach(pair => insert.SetValue(pair.ForeignKey, data.CommitObject.GetMember(pair.PrincipalKey)));

            var temptable = createtemptable.Table;
            var target    = new TableFragment(context, data.Table);
            var delete    = new DeleteFragment(context, target);

            delete.AddSource(temptable);
            target.Join(temptable, composite.Pairs.Select(a => a.ForeignKey).Concat(composite.CompositePairs.Select(a => a.ForeignKey)));
            return(new BlockFragment(context, createtemptable, insert, delete));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 生成用于插入的语句片段。
        /// </summary>
        /// <param name="context">生成上下文。</param>
        /// <param name="content">插入表达式。</param>
        /// <returns>生成结果。</returns>
        protected virtual SqlFragment GenerateForInsertStatement(GenerateContext context, DbExpression content)
        {
            var data     = (StatementContent)context.Data;
            var newitem  = (DbNewExpression)data.ItemEpxression;
            var source   = CreateSource(context, content) as QueryBaseFragment;
            var metadata = data.Table;

            if (metadata.InheritSets.Length == 0)
            {
                var target = new TableFragment(context, metadata, data.TargetName);
                var insert = new InsertFragment(context, target, source);
                GenerateForInsertStatementMembers(context, newitem, source, insert, member => metadata.Members[member]);
                return(insert);
            }
            else
            {
                var allsource   = metadata.InheritSets.Concat(new TableMetadata[] { metadata });
                var allmembers  = allsource.SelectMany(a => a.Members).ToDictionary(a => a.Member, a => a);
                var createtable = new CreateTempTableFragment(context, newitem.Members.Keys.OfType <PropertyInfo>().Select(a => allmembers[a]));
                var insert      = new InsertFragment(context, createtable.Table, source);
                GenerateForInsertStatementMembers(context, newitem, source, insert, member => allmembers[member]);
                var block = new BlockFragment(context, createtable, insert);
                var query = from a in newitem.Members.Keys.OfType <PropertyInfo>().Select(m => allmembers[m])
                            group a by a.Table into g
                            select new { g.Key, Members = g };
                var prefixMembers = query.First().Members.Where(a => a.IsKey);
                foreach (var g in query)
                {
                    IEnumerable <ColumnMetadata> currentMembers = g.Members;
                    if (g.Key.InheritSets.Length > 0)
                    {
                        currentMembers = prefixMembers.Concat(g.Members);
                    }
                    var current = new TemporaryTableFragment(context, currentMembers, createtable.Table.Name);

                    block.Add(context.Insert(current, g.Key, currentMembers));
                }
                return(block);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 使用临时表形式,生成更新关系语句,
        /// 要求数据库拥有<see cref="EDbCapable.TemporaryTable"/>
        /// 和<see cref="EDbCapable.ModifyJoin"/>两个特性。
        /// </summary>
        /// <param name="context">生成上下文。</param>
        /// <returns>语句片段。</returns>
        protected virtual SqlFragment GenerateForRelationUpdateTempTable(GenerateContext context)
        {
            var data            = (RelationContent)context.Data;
            var metadata        = data.Items.Navigate;
            var block           = new BlockFragment(context);
            var createtemptable = new CreateTempTableFragment(context,
                                                              data.IsAddRelation ? data.Source.Keys.Concat(metadata.Pairs.Select(a => a.ForeignKey)) : data.Source.Keys);
            var insert = new InsertValueFragment(context, createtemptable.Table, data.CommitObject, data.Items);

            data.Source.Keys.ForEach(key => insert.SetValue(key));
            if (data.IsAddRelation)
            {
                metadata.Pairs.ForEach(
                    pair => insert.SetValue(pair.ForeignKey, data.CommitObject.GetMember(pair.PrincipalKey)));
            }
            var temptable = createtemptable.Table;
            var update    = new UpdateFragment(context, data.Table);

            update.AddSource(update.Target, temptable);
            if (data.IsAddRelation)
            {
                foreach (var pair in metadata.Pairs)
                {
                    update.SetValue(pair.ForeignKey, temptable.GetMember(pair.ForeignKey));
                }
            }
            else
            {
                var nullfargment = new SimpleFragment(context, "NULL");
                foreach (var pair in metadata.Pairs)
                {
                    update.SetValue(pair.ForeignKey, nullfargment);
                }
            }
            update.Target.Join(temptable, data.Table.Keys);
            return(new BlockFragment(context, createtemptable, insert, update));
        }