コード例 #1
0
        internal UpdateChainer(Chainer prev, string alias)
            : base(prev)
        {
            Query.SetUpdate(this);
            alias = base.TryGetTableAlias(alias);
            var aliasDelimited = Filter.Delimit(alias);

            Build = (buildContext, buildArgs) =>
            {
                var sql    = Text.GenerateSql(200);
                var select = GetPrev <SelectChainer>();
                TakeTop(select);
                select.SkipBuild = true;

                sql.NewLine(BuildTop(buildContext, buildArgs))
                .Append(aliasDelimited).S();

                var columnsObject = Prev.GetPrev <ColumnsChainer>();
                columnsObject.Build(buildContext, buildArgs);
                columnsObject.SkipBuild = true;

                var values = ProcessValueArrayInliner(buildContext, buildArgs, select.Columns);
                if (values == null)
                {
                    values = select.Columns;
                }

                var columns = ProcessValueArrayInliner(buildContext, buildArgs, Columns);
                if (columns == null)
                {
                    columns = Columns;
                }

                sql.NewLine(Text.Set).S();
                var i = 0;
                foreach (var column in columns)
                {
                    if (i > 0)
                    {
                        sql.NewLineIndent(Text.Comma).S();
                    }

                    sql.Append(aliasDelimited)
                    .Append(Text.Dot)
                    .Append(column.Build(buildContext, buildArgs))
                    .Append(Text._Equal_)
                    .Append(values[i].Build(buildContext, buildArgs)).S();

                    ++i;
                }

                OutputChainer.TryAppendOutput(this, sql, buildContext, buildArgs);

                return(sql.ToString());
            };
        }
コード例 #2
0
ファイル: InsertChainer.cs プロジェクト: amosvoron/querytalk
        private StringBuilder BuildHeader(BuildContext buildContext, BuildArgs buildArgs, Table table)
        {
            var sql = Text.GenerateSql(200);

            sql.NewLine(BuildTop(buildContext, buildArgs))
            .Append(table.Build(buildContext, buildArgs)).S();

            Column[] columns       = null;
            var      columnsObject = Prev.GetPrev <ColumnsChainer>();

            if (columnsObject != null)
            {
                columnsObject.Build(buildContext, buildArgs);

                // Do not build the ColumnsChainer object (it has been built already) .
                // This is not actually not needed because the Query does not control ColumnsChainer object.
                // However we set this flag here to make it clear and explicit.
                columnsObject.SkipBuild = true;

                // handle column inliner in .ColumnInto method passed as collection
                columns = ProcessValueArrayInliner(buildContext, buildArgs, Columns);
            }

            if (columns == null)
            {
                columns = Columns;
            }

            // build inline columns
            if (columns != null)
            {
                sql.NewLine(Text.LeftBracket);

                int i = 0;
                foreach (var column in columns)
                {
                    CheckNull(Arg(() => column, column));
                    if (chainException != null)
                    {
                        chainException.Arguments = String.Format("{0} = null{1}   columns = {2}",
                                                                 chainException.Arguments, Environment.NewLine, ToString());
                        throw chainException;
                    }

                    if (i++ > 0)
                    {
                        sql.Append(Text.Comma).S();
                    }

                    sql.Append(column.Build(buildContext, buildArgs));

                    TryThrow(buildContext);
                }

                sql.Append(Text.RightBracket)
                .TrimEnd();
            }

            OutputChainer.TryAppendOutput(this, sql, buildContext, buildArgs);

            return(sql);
        }