Ejemplo n.º 1
0
        public IExpression Parse(TextSpan token, IParser parser)
        {
            var permissionPrincipal = new[]
            {
                SqlToken.CONNECT,
                SqlToken.Delete,
                SqlToken.Insert,
                SqlToken.Select,
                SqlToken.Update,
                SqlToken.Execute,
                SqlToken.Exec,
                SqlToken.Alter
            };

            var permissionList = parser.Scanner.ConsumeToStringListByDelimiter(SqlToken.Comma, permissionPrincipal)
                                 .ToList();

            if (permissionList.Count == 0)
            {
                var permissionPrincipalStr = string.Join(",", permissionPrincipal);
                ThrowHelper.ThrowParseException(parser, $"Expect one of {permissionPrincipalStr}.");
            }

            SqlCodeExpr onObjectId = null;

            if (parser.Scanner.Match(SqlToken.ON))
            {
                //onObjectId = parser.ConsumeAny(SqlToken.Object, SqlToken.SqlIdentifier, SqlToken.Identifier) as SqlCodeExpr;
                onObjectId = parser.ParseExpIgnoreComment();
            }

            parser.Scanner.Consume(SqlToken.To);

            var targetList = parser.ConsumeByDelimiter(SqlToken.Comma, () =>
            {
                return(parser.ParseExp() as SqlCodeExpr);
            }).ToList();


            SqlCodeExpr asDbo = null;

            if (parser.Scanner.Match(SqlToken.As))
            {
                asDbo = parser.ConsumeObjectId();
            }

            var isSemicolon = parser.MatchToken(SqlToken.Semicolon);

            return(new GrantSqlCodeExpr
            {
                PermissionList = permissionList,
                OnObjectId = onObjectId,
                TargetList = targetList,
                AsDbo = asDbo,
                IsSemicolon = isSemicolon
            });
        }
Ejemplo n.º 2
0
        public IExpression Parse(TextSpan token, IParser parser)
        {
            var intoStr = string.Empty;

            if (parser.Scanner.TryConsume(SqlToken.Into, out var intoToken))
            {
                intoStr = parser.Scanner.GetSpanString(intoToken);
            }

            var tableName = parser.ConsumeTableName();

            var withExpr = parser.ParsePrefix(SqlToken.With);

            var columnList = GetColumnsList(parser);

            if (parser.Scanner.TryConsumeAny(out var execSpan, SqlToken.Exec, SqlToken.Execute))
            {
                var execExpr = parser.PrefixParse(execSpan) as SqlCodeExpr;
                return(new InsertIntoFromSqlCodeExpr
                {
                    Table = tableName,
                    ColumnsList = columnList,
                    WithExpr = withExpr,
                    SelectFromExpr = execExpr,
                });
            }

            var outputList = parser.ParseOutputListExpr();
            var outputInto = parser.ParseInto_TableExpr();


            var hasGroup = parser.MatchToken(SqlToken.LParen);

            if (parser.Scanner.TryConsume(SqlToken.Select, out var selectToken))
            {
                var selectExpr = new SelectParselet().Parse(selectToken, parser) as SqlCodeExpr;
                if (hasGroup)
                {
                    parser.MatchToken(SqlToken.RParen);
                    selectExpr = new GroupSqlCodeExpr
                    {
                        InnerExpr = selectExpr
                    };
                }

                return(new InsertIntoFromSqlCodeExpr
                {
                    Table = tableName,
                    ColumnsList = columnList,
                    OutputList = outputList,
                    OutputIntoExpr = outputInto,
                    SelectFromExpr = selectExpr,
                });
            }


            parser.Scanner.Consume(SqlToken.Values);
            var valuesList = parser.ConsumeByDelimiter(SqlToken.Comma, () =>
            {
                parser.Scanner.Consume(SqlToken.LParen);
                var values = new List <SqlCodeExpr>();
                do
                {
                    var expr = parser.ParseExpIgnoreComment();
                    values.Add(expr);
                } while (parser.Scanner.Match(SqlToken.Comma));

                parser.Scanner.Consume(SqlToken.RParen);

                return(new ExprListSqlCodeExpr
                {
                    Items = values.ToList()
                });
            }).ToList();

            return(new InsertSqlCodeExpr
            {
                IntoStr = intoStr,
                TableName = tableName,
                Columns = columnList,
                WithExpr = withExpr,
                ValuesList = valuesList
            });
        }