Search in sources :

Example 1 with BracketExpr

use of org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.BracketExpr 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 BracketExpr

use of org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.BracketExpr 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 3 with BracketExpr

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

the class SqlParser method parseSingleStatement.

/**
 * return {@link SingleStatement} or {@link BracketExpr}
 */
private IToken parseSingleStatement(List<IToken> list, ParseContext ctx) {
    ParseStep lock = ctx.checkAndAdd("SingleStatement", list);
    if (lock == null) {
        return null;
    }
    try {
        // brackets
        ArrayList<IToken> backup = new ArrayList<IToken>(list);
        BracketExpr be = parseBracketExpr(list, ctx);
        if (be != null) {
            BracketExpr tmp = be;
            while (tmp != null) {
                // open, token, close
                IToken ch = tmp.getChildren().get(1);
                if (ch instanceof BracketExpr) {
                    tmp = (BracketExpr) ch;
                } else if (ch instanceof Statement) {
                    return be;
                } else if (ch instanceof SingleStatement) {
                    return be;
                } else {
                    tmp = null;
                }
            }
            // restore
            list.clear();
            list.addAll(backup);
            return null;
        }
        // 
        // Part+
        Part p;
        SingleStatement ss = new SingleStatement();
        if ((p = parsePart(list, ctx, true)) != null) {
            // ok
            ss.addChild(p);
        } else {
            return null;
        }
        while ((p = parsePart(list, ctx, false)) != null) {
            ss.addChild(p);
        }
        if (ss.getChildren().size() == 0) {
            return null;
        }
        return ss;
    } 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) Part(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Part) ArrayList(java.util.ArrayList) SingleStatement(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.SingleStatement) BracketExpr(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.BracketExpr)

Example 4 with BracketExpr

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

the class SqlParser method parseBracketExpr.

@SuppressWarnings("squid:S2583")
private BracketExpr parseBracketExpr(List<IToken> list, ParseContext ctx) {
    // BracketExpr = OpenBracketToken (Statement | ListExpr) CloseBracketToken
    ParseStep lock = ctx.checkAndAdd("BracketExpr", list);
    if (lock == null) {
        return null;
    }
    try {
        IToken open = null;
        IToken t = null;
        IToken close = null;
        ArrayList<IToken> backup = new ArrayList<IToken>(list);
        if ((open = removeToken(list, OpenBracketToken.class)) != null && ((t = parseStatement(list, ctx)) != null || (t = parseListExpr(list, ctx)) != null || t == null) && (close = removeToken(list, CloseBracketToken.class)) != null) {
        // NOSONAR
        // ok
        } else {
            // restore incomplete
            list.clear();
            list.addAll(backup);
            return null;
        }
        BracketExpr e = new BracketExpr();
        e.addChild(open);
        if (t != null) {
            e.addChild(t);
        }
        e.addChild(close);
        return e;
    } finally {
        ctx.remove(lock);
    }
}
Also used : IToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken) ArrayList(java.util.ArrayList) BracketExpr(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.BracketExpr) OpenBracketToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.OpenBracketToken)

Example 5 with BracketExpr

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

the class SqlParser method parseFunExpr.

private FunExpr parseFunExpr(List<IToken> list, ParseContext ctx) {
    // Name BracketExpr
    ParseStep lock = ctx.checkAndAdd("FunExpr", list);
    if (lock == null) {
        return null;
    }
    try {
        Name nm = null;
        BracketExpr be = null;
        if ((nm = removeToken(list, Name.class)) != null && (be = parseBracketExpr(list, ctx)) != null) {
        // ok
        } else {
            // restore incomplete
            if (nm != null) {
                list.add(0, nm);
            }
            return null;
        }
        FunExpr e = new FunExpr();
        e.addChild(nm);
        e.addChild(be);
        return e;
    } finally {
        ctx.remove(lock);
    }
}
Also used : BracketExpr(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.BracketExpr) FunExpr(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.FunExpr) Name(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Name)

Aggregations

BracketExpr (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.BracketExpr)5 IToken (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken)4 SingleStatement (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.SingleStatement)3 Statement (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Statement)3 ArrayList (java.util.ArrayList)2 Unparsed (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Unparsed)2 FunExpr (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.FunExpr)1 Name (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Name)1 OpenBracketToken (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.OpenBracketToken)1 Part (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Part)1 UnionToken (org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.UnionToken)1