Search in sources :

Example 1 with IToken

use of org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken in project scout.rt by eclipse.

the class SqlFormatter method formatStatement.

private void formatStatement(Statement stm, FormatContext ctx) {
    if (stm != null) {
        for (IToken t : stm.getChildren()) {
            if (t instanceof Statement) {
                formatStatement((Statement) t, ctx);
            } else if (t instanceof SingleStatement) {
                formatSingleStatement((SingleStatement) t, ctx);
            } else if (t instanceof BracketExpr) {
                formatBracketExpr((BracketExpr) t, ctx);
            } else if (t instanceof UnionToken) {
                formatDefault(t, ctx);
                ctx.println();
            } else if (t instanceof Unparsed) {
                ctx.println("*** UNPARSED ***");
                formatDefault(t, ctx);
                ctx.println();
            } else {
                formatDefault(t, ctx);
            }
        }
    }
}
Also used : IToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken) Statement(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Statement) SingleStatement(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.SingleStatement) Unparsed(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Unparsed) SingleStatement(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.SingleStatement) UnionToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.UnionToken) BracketExpr(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.BracketExpr)

Example 2 with IToken

use of org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken in project scout.rt by eclipse.

the class SqlFormatter method formatBracketExpr.

private void formatBracketExpr(BracketExpr stm, FormatContext ctx) {
    boolean multiline = isMultiline(stm);
    for (IToken t : stm.getChildren()) {
        if (t instanceof OpenBracketToken) {
            formatDefault(t, ctx);
            if (multiline) {
                ctx.println();
                ctx.in();
            }
        } else if (t instanceof CloseBracketToken) {
            if (multiline) {
                ctx.out();
            }
            formatDefault(t, ctx);
        } else if (t instanceof Statement) {
            formatStatement((Statement) t, ctx);
        } else if (t instanceof SingleStatement) {
            formatSingleStatement((SingleStatement) t, ctx);
        } else if (t instanceof ListExpr) {
            formatListExpr((ListExpr) t, multiline, ctx);
        } else {
            formatDefault(t, ctx);
        }
    }
}
Also used : CloseBracketToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.CloseBracketToken) IToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken) Statement(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Statement) SingleStatement(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.SingleStatement) SingleStatement(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.SingleStatement) ListExpr(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.ListExpr) OpenBracketToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.OpenBracketToken)

Example 3 with IToken

use of org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken in project scout.rt by eclipse.

the class SqlParser method parseMathExpr.

private MathExpr parseMathExpr(List<IToken> list, ParseContext ctx) {
    // _simpleExpr (BinaryOp _simpleExpr)*
    ParseStep lock = ctx.checkAndAdd("MathExpr", list);
    if (lock == null) {
        return null;
    }
    try {
        IToken se = parseSimpleExpr(list, ctx);
        if (se == null) {
            return null;
        }
        MathExpr me = new MathExpr();
        me.addChild(se);
        MathOp mo;
        while ((mo = removeToken(list, MathOp.class)) != null && (se = parseSimpleExpr(list, ctx)) != null) {
            me.addChild(mo);
            me.addChild(se);
        }
        // restore incomplete
        if (mo != null) {
            list.add(0, mo);
        }
        return me;
    } finally {
        ctx.remove(lock);
    }
}
Also used : IToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken) MathOp(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.MathOp) MathExpr(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.MathExpr)

Example 4 with IToken

use of org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken in project scout.rt by eclipse.

the class SqlParser method parse.

public Statement parse(String s) {
    ParseContext ctx = new ParseContext();
    List<IToken> list = tokenize(s, ctx);
    Statement stm = parseStatement(list, ctx);
    // sometimes sql is wrapped into brackets
    if (stm == null) {
        ctx = new ParseContext();
        list = tokenize(s, ctx);
        BracketExpr be = parseBracketExpr(list, ctx);
        if (be != null) {
            for (IToken t : be.getChildren()) {
                if (t instanceof Statement) {
                    stm = (Statement) t;
                    break;
                }
            }
        }
    }
    if (list.size() > 0) {
        Unparsed up = new Unparsed();
        up.setText(flatten(list));
        if (stm == null) {
            stm = new Statement();
        }
        stm.addChild(up);
    }
    return stm;
}
Also used : IToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken) Statement(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Statement) SingleStatement(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.SingleStatement) Unparsed(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Unparsed) BracketExpr(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.BracketExpr)

Example 5 with IToken

use of org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken in project scout.rt by eclipse.

the class SqlParser method parseStatement.

@SuppressWarnings("squid:S2583")
private Statement parseStatement(List<IToken> list, ParseContext ctx) {
    // SingleStatement (UnionToken SingleStatement)*
    ParseStep lock = ctx.checkAndAdd("Statement", list);
    if (lock == null) {
        return null;
    }
    try {
        IToken ss = parseSingleStatement(list, ctx);
        if (ss == null) {
            return null;
        }
        Statement s = new Statement();
        s.addChild(ss);
        UnionToken u;
        IToken nexts = null;
        while ((u = removeToken(list, UnionToken.class)) != null && (nexts = parseSingleStatement(list, ctx)) != null) {
            s.addChild(u);
            s.addChild(nexts);
        }
        // restore incomplete
        if (u != null && nexts == null) {
            // NOSONAR
            list.add(0, u);
        }
        return s;
    } finally {
        ctx.remove(lock);
    }
}
Also used : IToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken) Statement(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Statement) SingleStatement(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.SingleStatement) UnionToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.UnionToken)

Aggregations

IToken (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken)16 MathOp (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.MathOp)5 SingleStatement (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.SingleStatement)5 Statement (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Statement)5 ArrayList (java.util.ArrayList)4 BracketExpr (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.BracketExpr)4 OpenBracketToken (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.OpenBracketToken)3 UnaryPrefix (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.UnaryPrefix)3 UnionToken (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.UnionToken)3 AndOp (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.AndOp)2 Atom (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Atom)2 CloseBracketToken (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.CloseBracketToken)2 Comment (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Comment)2 ListExpr (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.ListExpr)2 Name (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Name)2 OuterJoinToken (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.OuterJoinToken)2 Part (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Part)2 PartToken (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.PartToken)2 Raw (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Raw)2 Unparsed (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Unparsed)2