Exemple #1
0
        protected SqlCaseClause(SqlParserState state, out AmpElement error)
        {
            Kind = SqlKind.SqlCaseClause;
            if (!state.IsKind(SqlKind.CaseToken))
            {
                error = SqlParseError.Construct(state);
                return;
            }

            CaseToken = state.CurrentToken;
            state.Read();

            if (!state.IsKind(SqlKind.WhenToken, SqlKind.ElseToken, SqlKind.EndToken))
            {
                if (!SqlParser.TryParse <SqlExpression>(state, out var caseExpr))
                {
                    error = SqlParseError.Construct(state);
                    return;
                }
                CaseExpression = caseExpr;
            }

            List <SqlWhenClause> whens = new List <SqlWhenClause>();

            while (state.IsKind(SqlKind.WhenToken))
            {
                SqlToken whenToken = state.CurrentToken;
                state.Read();

                if (!SqlParser.TryParse <SqlExpression>(state, out var whenExpr))
                {
                    error = SqlParseError.Construct(state);
                    return;
                }

                if (!state.IsKind(SqlKind.ThenToken))
                {
                    error = SqlParseError.Construct(state);
                    return;
                }
                SqlToken thenToken = state.CurrentToken;
                state.Read();


                if (!SqlParser.TryParse <SqlExpression>(state, out var thenExpr))
                {
                    error = SqlParseError.Construct(state);
                    return;
                }

                whens.Add(new SqlWhenClause(whenToken, whenExpr, thenToken, thenExpr));
            }
            Whens = SqlSyntaxList <SqlWhenClause> .FromItems(whens);

            if (state.IsKind(SqlKind.ElseToken))
            {
                ElseToken = state.CurrentToken;
                state.Read();

                if (!SqlParser.TryParse <SqlExpression>(state, out var elseExpr))
                {
                    error = SqlParseError.Construct(state);
                    return;
                }
                ElseExpression = elseExpr;
            }

            if (state.IsKind(SqlKind.EndToken))
            {
                EndToken = state.CurrentToken;
                state.Read();
                error = null;
            }
            else
            {
                error = SqlParseError.Construct(state);
            }
        }
Exemple #2
0
        protected SqlSelect(SqlParserState state, out AmpElement error)
        {
            Kind = SqlKind.SelectSyntax;

            if (state.IsKind(SqlKind.WithToken))
            {
                if (!SqlParser.TryParse <SqlWithClause>(state, out var wp))
                {
                    error = state.Error;
                    return;
                }
                WithPart = wp;
            }

            if (state.IsKind(SqlKind.SelectToken))
            {
                SelectToken = (SqlToken)state.Current;
                state.Read();
            }

            if (state.IsKind(SqlKind.DistinctToken))
            {
                DistinctAllModifier = state.CurrentToken;
                state.Read();
            }
            else if (state.IsKind(SqlKind.AllToken))
            {
                DistinctAllModifier = state.CurrentToken;
                state.Read();
            }

            if (SqlParser.TryParse <SqlCommaSeparatedTokenList <SqlSelectColumn> >(state, out var cols))
            {
                ResultColumns = cols;
            }
            else
            {
                error = state.Error;
                return;
            }

            if (state.IsKind(SqlKind.FromToken))
            {
                if (!SqlParser.TryParse <SqlFromClause>(state, out var from))
                {
                    error = state.Error;
                    return;
                }
                FromClause = from;
            }

            List <SqlJoinClause> joins = new List <SqlJoinClause>();

            while (state.IsKind(SqlKind.JoinToken, SqlKind.OuterJoinToken, SqlKind.LeftToken, SqlKind.RightToken, SqlKind.InnerToken, SqlKind.CrossToken))
            {
                if (!SqlParser.TryParse <SqlJoinClause>(state, out var joinClause))
                {
                    error = state.Error;
                    return;
                }
                joins.Add(joinClause);
            }

            if (joins.Count > 0)
            {
                JoinClauses = SqlSyntaxList <SqlJoinClause> .FromItems(joins);
            }

            if (state.IsKind(SqlKind.WhereToken))
            {
                if (!SqlParser.TryParse <SqlWhereClause>(state, out var where))
                {
                    error = state.Error;
                    return;
                }
                WhereClause = where;
            }

            // GROUP BY
            // COMPOUND
            // ORDER BY


            if (state.IsKind(SqlKind.FetchToken))
            {
                if (!SqlParser.TryParse <SqlLimitClause>(state, out var limit))
                {
                    error = state.Error;
                    return;
                }
                LimitClause = limit;
            }


            error = null;
        }