Beispiel #1
0
        private SqlFragment GenerateForInsertIdentityMultipleRepeat(GenerateContext context, CommitIdentityUnit mainunit, ColumnMetadata rowindex,
                                                                    TemporaryTableFragment temporaryTable, RowIndexFragment rowindexfragment, GenerateDataForInsert data, out TableFragment whileTarget)
        {
            var repeatfragment = new RepeatBlockFragment(context, data.Items);
            var block          = repeatfragment.Block;
            var whileInsert    = context.Insert(temporaryTable, mainunit.Table,
                                                mainunit.Members.Where(a => a.ValueType != ECommitValueType.Database).Select(a => a.Metadata));
            var whileSelect = (QueryBaseFragment)whileInsert.Query;

            whileSelect.Where = context.Equal(temporaryTable.GetMember(rowindex), new RowIndexFragment(context));
            block.Add(whileInsert);
            var whileUpdate = new UpdateFragment(context, temporaryTable);
            var getidentity = whileUpdate.CreateExpression(
                context.Translate(Expression.Call(null, SupportMembers.DbFunctions.GetIdentity)));

            whileUpdate.SetValue(mainunit.Identity.Metadata, getidentity);
            whileUpdate.Where = context.Equal(temporaryTable.GetMember(rowindex), new RowIndexFragment(context));
            block.Add(whileUpdate);
            whileTarget = (TableFragment)whileInsert.Target;
            return(repeatfragment);
        }
Beispiel #2
0
        private WhileFragment GenerateForInsertIdentityMultipleWhile(GenerateContext context, CommitIdentityUnit mainunit, CreateVariableFragment index,
                                                                     ColumnMetadata rowindex, TemporaryTableFragment temporaryTable, RowIndexFragment rowindexfragment, out TableFragment whileTarget)
        {
            var whilefragment = new WhileFragment(context, context.LessThan(index.Name, rowindexfragment));
            var block         = whilefragment.Block;
            var whileInsert   = context.Insert(temporaryTable, mainunit.Table,
                                               mainunit.Members.Where(a => a.ValueType != ECommitValueType.Database).Select(a => a.Metadata));
            var whileSelect = (QueryBaseFragment)whileInsert.Query;

            whileSelect.Where = context.Equal(temporaryTable.GetMember(rowindex), index.Name);
            block.Add(whileInsert);
            var whileUpdate = new UpdateFragment(context, temporaryTable);
            var getidentity = whileUpdate.CreateExpression(
                context.Translate(Expression.Call(null, SupportMembers.DbFunctions.GetIdentity)));

            whileUpdate.SetValue(mainunit.Identity.Metadata, getidentity);
            whileUpdate.Where = context.Equal(temporaryTable.GetMember(rowindex), index.Name);
            block.Add(whileUpdate);
            block.Add(context.Assign(index.Name, context.Add(index.Name, context.StatementString("1"))));
            whileTarget = (TableFragment)whileInsert.Target;
            return(whilefragment);
        }
Beispiel #3
0
        /// <summary>
        /// 生成插入单个数据对象语句,对象的主键是标识列。
        /// </summary>
        /// <param name="context">生成上下文。</param>
        /// <returns>语句片段。</returns>
        protected virtual SqlFragment GenerateForInsertIdentitySingle(GenerateContext context)
        {
            var getidentity = context.Translate(Expression.Call(null, SupportMembers.DbFunctions.GetIdentity));

            var data           = (GenerateDataForInsert)context.Data;
            var identityunit   = (CommitIdentityUnit)data.MainUnit;
            var keyVars        = new Dictionary <ColumnMetadata, VariableFragment>();
            var identityCreate = new CreateVariableFragment(context, identityunit.Identity.Metadata.Member.PropertyType);

            keyVars.Add(identityunit.Identity.Metadata, identityCreate.Name);
            var block = new BlockFragment(context);

            if (!context.Feature.HasCapable(EDbCapable.ImplicitDeclareVariable))
            {
                block.Add(identityCreate);
            }
            var name       = data.TargetName;
            var maintarget = new TableFragment(context, identityunit.Table, name);

            {
                var insert = new InsertValueFragment(context, maintarget, data.CommitObject, data.Items);
                data.CommitObject.Parent = insert;
                identityunit.Members.ForEach(member =>
                {
                    if (member.Metadata.IsKey && member.ValueType == ECommitValueType.Expression)
                    {
                        var expMember = (CommitExpressionMember)member;
                        var exp       = insert.CreateExpression(expMember.Expression);
                        var expVar    = new CreateVariableFragment(context, member.Metadata.Member.PropertyType);
                        block.Add(expVar);
                        block.Add(context.Assign(expVar.Name, exp));
                        keyVars.Add(member.Metadata, expVar.Name);
                    }
                    else
                    {
                        GenerateForInsertMembers(insert, member);
                    }
                });
                block.Add(insert);
                block.Add(context.Assign(identityCreate.Name, insert.CreateExpression(getidentity)));
            }
            if (data.SubUnits != null)
            {
                foreach (var unit in data.SubUnits)
                {
                    var target = new TableFragment(context, unit.Table);
                    var insert = new InsertValueFragment(context, target, data.CommitObject, data.Items);
                    data.CommitObject.Parent = insert;
                    unit.Keys.ForEach(member =>
                    {
                        if (keyVars.TryGetValue(member.Metadata, out VariableFragment value))
                        {
                            insert.SetValue(member.Metadata, value);
                        }
                        else
                        {
                            GenerateForInsertMembers(insert, member);
                        }
                    });
                    unit.Members.ForEach(member => GenerateForInsertMembers(insert, member));
                    block.Add(insert);
                }
            }
            if (data.ReturnMembers.Any())
            {
                ISourceFragment target = maintarget;
                if (data.SubUnits != null)
                {
                    target = new InheritFragment(context, data.Table);
                }
                var select = new SelectFragment(context, target);
                select.Members.AddRange(data.ReturnMembers.Select(a => target.GetMember(a.Metadata)));
                select.Where = data.Table.Keys.Select(key => context.Equal(target.GetMember(key),
                                                                           keyVars.ContainsKey(key) ? (IExpressionFragment)keyVars[key] : data.CommitObject.GetMember(key))).Merge();
                block.Add(select);
            }
            return(block);
        }