private void Initialize(NonSelectColumnArgument column, DataType dataTypeDef)
        {
            IsNullable = null;
            CheckNullAndThrow(Arg(() => column, column));
            TryThrow(dataTypeDef.Exception);
            Prev.GetPrev <BeginTableChainer>().SetHasColumns();

            Build = (buildContext, buildArgs) =>
            {
                var sql = Text.GenerateSql(300);
                sql.AppendLine();
                sql.Append(Text.TwoSpaces);

                if (!(Prev is BeginTableChainer && !((BeginTableChainer)Prev).IsDesignedByType))
                {
                    sql.Append(Text.Comma);
                }

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

                TryThrow(column.Exception);

                if (Identity != null)
                {
                    sql.S().Append(Text.Identity)
                    .EncloseLeft()
                    .Append(Identity.Seed)
                    .AppendComma()
                    .Append(Identity.Increment)
                    .EncloseRight();
                }

                if (IsNullable == true)
                {
                    sql.S().Append(Text.Null);
                }
                else if (IsNullable == false)
                {
                    sql.S().Append(Text.NotNull);
                }

                if (Check != null)
                {
                    sql.S().Append(Text.Check)
                    .EncloseLeft().Append(Check.Build(buildContext, buildArgs)).EncloseRight();
                    TryThrow(buildContext);
                }

                if (Default != null)
                {
                    sql.S().Append(Text.Default)
                    .EncloseLeft().Append(Default.Build(buildContext, buildArgs)).EncloseRight();
                    TryThrow(buildContext);
                }

                return(sql.ToString());
            };
        }
Пример #2
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());
            };
        }
Пример #3
0
        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);
        }