예제 #1
0
        public TSQLMergeClause Parse(ITSQLTokenizer tokenizer)
        {
            TSQLMergeClause merge = new TSQLMergeClause();

            if (!tokenizer.Current.IsKeyword(TSQLKeywords.MERGE))
            {
                throw new InvalidOperationException("MERGE expected.");
            }

            merge.Tokens.Add(tokenizer.Current);

            // can contain TOP()

            TSQLSubqueryHelper.ReadUntilStop(
                tokenizer,
                merge,
                new List <TSQLFutureKeywords>()
            {
                TSQLFutureKeywords.OUTPUT,
                TSQLFutureKeywords.USING
            },
                new List <TSQLKeywords>()
            {
                TSQLKeywords.INTO,
                TSQLKeywords.AS,
                TSQLKeywords.ON,
                TSQLKeywords.WHEN
            },
                lookForStatementStarts: true);

            return(merge);
        }
        public TSQLInsertClause Parse(ITSQLTokenizer tokenizer)
        {
            TSQLInsertClause insert = new TSQLInsertClause();

            if (!tokenizer.Current.IsKeyword(TSQLKeywords.INSERT))
            {
                throw new InvalidOperationException("INSERT expected.");
            }

            insert.Tokens.Add(tokenizer.Current);

            TSQLSubqueryHelper.ReadUntilStop(
                tokenizer,
                insert,
                new List <TSQLFutureKeywords>()
            {
                TSQLFutureKeywords.OUTPUT
            },
                new List <TSQLKeywords>()
            {
                TSQLKeywords.SELECT,
                TSQLKeywords.EXECUTE,
                TSQLKeywords.VALUES,
                TSQLKeywords.DEFAULT
            },
                lookForStatementStarts: false);

            return(insert);
        }
예제 #3
0
        public TSQLOnClause Parse(ITSQLTokenizer tokenizer)
        {
            TSQLOnClause on = new TSQLOnClause();

            if (!tokenizer.Current.IsKeyword(TSQLKeywords.ON))
            {
                throw new InvalidOperationException("ON expected.");
            }

            on.Tokens.Add(tokenizer.Current);

            // TODO: tighten logic to handle tables named OUTPUT, but still handle ON usage in MERGE

            TSQLSubqueryHelper.ReadUntilStop(
                tokenizer,
                on,
                new List <TSQLFutureKeywords>()
            {
                TSQLFutureKeywords.OUTPUT,
                TSQLFutureKeywords.USING
            },
                new List <TSQLKeywords>()
            {
                TSQLKeywords.INNER,
                TSQLKeywords.OUTER,
                TSQLKeywords.JOIN,
                TSQLKeywords.WHEN
            },
                lookForStatementStarts: true);

            return(on);
        }
        public TSQLGroupByClause Parse(ITSQLTokenizer tokenizer)
        {
            TSQLGroupByClause groupBy = new TSQLGroupByClause();

            if (!tokenizer.Current.IsKeyword(TSQLKeywords.GROUP))
            {
                throw new InvalidOperationException("GROUP expected.");
            }

            groupBy.Tokens.Add(tokenizer.Current);

            // subqueries

            TSQLSubqueryHelper.ReadUntilStop(
                tokenizer,
                groupBy,
                new List <TSQLFutureKeywords>()
            {
            },
                new List <TSQLKeywords>()
            {
                TSQLKeywords.HAVING,
                TSQLKeywords.UNION,
                TSQLKeywords.EXCEPT,
                TSQLKeywords.INTERSECT,
                TSQLKeywords.ORDER,
                TSQLKeywords.FOR,
                TSQLKeywords.OPTION
            },
                lookForStatementStarts: true);

            return(groupBy);
        }
        public TSQLHavingClause Parse(ITSQLTokenizer tokenizer)
        {
            TSQLHavingClause having = new TSQLHavingClause();

            if (!tokenizer.Current.IsKeyword(TSQLKeywords.HAVING))
            {
                throw new InvalidOperationException("HAVING expected.");
            }

            having.Tokens.Add(tokenizer.Current);

            // subqueries

            TSQLSubqueryHelper.ReadUntilStop(
                tokenizer,
                having,
                new List <TSQLFutureKeywords>()
            {
            },
                new List <TSQLKeywords>()
            {
                TSQLKeywords.ORDER,
                TSQLKeywords.UNION,
                TSQLKeywords.EXCEPT,
                TSQLKeywords.INTERSECT,
                TSQLKeywords.FOR,
                TSQLKeywords.OPTION
            },
                lookForStatementStarts: true);

            return(having);
        }
        public TSQLWhenClause Parse(ITSQLTokenizer tokenizer)
        {
            TSQLWhenClause when = new TSQLWhenClause();

            if (!tokenizer.Current.IsKeyword(TSQLKeywords.WHEN))
            {
                throw new InvalidOperationException("WHEN expected.");
            }

            when.Tokens.Add(tokenizer.Current);

            // we don't have to worry about accidentally running into the next statement.

            // https://docs.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql
            // The MERGE statement requires a semicolon (;) as a statement terminator.
            // Error 10713 is raised when a MERGE statement is run without the terminator.

            TSQLSubqueryHelper.ReadUntilStop(
                tokenizer,
                when,
                new List <TSQLFutureKeywords>()
            {
                TSQLFutureKeywords.OUTPUT
            },
                new List <TSQLKeywords>()
            {
                TSQLKeywords.WHEN,
                TSQLKeywords.OPTION
            },
                lookForStatementStarts: false);

            return(when);
        }
        public TSQLOrderByClause Parse(ITSQLTokenizer tokenizer)
        {
            TSQLOrderByClause orderBy = new TSQLOrderByClause();

            if (!tokenizer.Current.IsKeyword(TSQLKeywords.ORDER))
            {
                throw new InvalidOperationException("ORDER expected.");
            }

            orderBy.Tokens.Add(tokenizer.Current);

            // subqueries

            TSQLSubqueryHelper.ReadUntilStop(
                tokenizer,
                orderBy,
                new List <TSQLFutureKeywords>()
            {
            },
                new List <TSQLKeywords>()
            {
                TSQLKeywords.FOR,
                TSQLKeywords.OPTION
            },
                lookForStatementStarts: true);

            return(orderBy);
        }
        public TSQLDeleteClause Parse(ITSQLTokenizer tokenizer)
        {
            TSQLDeleteClause delete = new TSQLDeleteClause();

            if (!tokenizer.Current.IsKeyword(TSQLKeywords.DELETE))
            {
                throw new InvalidOperationException("DELETE expected.");
            }

            delete.Tokens.Add(tokenizer.Current);

            TSQLSubqueryHelper.ReadUntilStop(
                tokenizer,
                delete,
                new List <TSQLFutureKeywords>()
            {
                TSQLFutureKeywords.OUTPUT
            },
                new List <TSQLKeywords>()
            {
                TSQLKeywords.FROM,
                TSQLKeywords.WHERE,
                TSQLKeywords.OPTION
            },
                lookForStatementStarts: true);

            return(delete);
        }
예제 #9
0
        public TSQLUpdateClause Parse(ITSQLTokenizer tokenizer)
        {
            TSQLUpdateClause update = new TSQLUpdateClause();

            if (!tokenizer.Current.IsKeyword(TSQLKeywords.UPDATE))
            {
                throw new InvalidOperationException("UPDATE expected.");
            }

            update.Tokens.Add(tokenizer.Current);

            TSQLSubqueryHelper.ReadUntilStop(
                tokenizer,
                update,
                new List <TSQLFutureKeywords>()
            {
            },
                new List <TSQLKeywords>()
            {
                TSQLKeywords.SET
            },
                lookForStatementStarts: false);

            return(update);
        }
예제 #10
0
        public TSQLSelectClause Parse(ITSQLTokenizer tokenizer)
        {
            TSQLSelectClause select = new TSQLSelectClause();

            if (!tokenizer.Current.IsKeyword(TSQLKeywords.SELECT))
            {
                throw new InvalidOperationException("SELECT expected.");
            }

            select.Tokens.Add(tokenizer.Current);

            // can contain ALL, DISTINCT, TOP, PERCENT, WITH TIES, AS

            // ends with FROM, semicolon, or keyword other than those listed above, when used outside of parens

            // recursively walk down and back up parens

            TSQLSubqueryHelper.ReadUntilStop(
                tokenizer,
                select,
                new List <TSQLFutureKeywords>()
            {
            },
                new List <TSQLKeywords>()
            {
                TSQLKeywords.INTO,
                TSQLKeywords.FROM,
                TSQLKeywords.WHERE,
                TSQLKeywords.GROUP,
                TSQLKeywords.HAVING,
                TSQLKeywords.ORDER,
                TSQLKeywords.UNION,
                TSQLKeywords.EXCEPT,
                TSQLKeywords.INTERSECT,
                TSQLKeywords.FOR,
                TSQLKeywords.OPTION
            },
                lookForStatementStarts: true);

            return(select);
        }
        public TSQLFromClause Parse(ITSQLTokenizer tokenizer)
        {
            TSQLFromClause from = new TSQLFromClause();

            if (!tokenizer.Current.IsKeyword(TSQLKeywords.FROM))
            {
                throw new InvalidOperationException("FROM expected.");
            }

            from.Tokens.Add(tokenizer.Current);

            // derived tables
            // TVF

            TSQLSubqueryHelper.ReadUntilStop(
                tokenizer,
                from,
                new List <TSQLFutureKeywords>()
            {
            },
                new List <TSQLKeywords>()
            {
                TSQLKeywords.WHERE,
                TSQLKeywords.GROUP,
                TSQLKeywords.HAVING,
                TSQLKeywords.ORDER,
                TSQLKeywords.UNION,
                TSQLKeywords.EXCEPT,
                TSQLKeywords.INTERSECT,
                TSQLKeywords.FOR,
                TSQLKeywords.OPTION
            },
                lookForStatementStarts: true);

            return(from);
        }