use of org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Statement 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);
}
}
}
}
use of org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Statement 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);
}
}
}
use of org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Statement 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;
}
use of org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Statement 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);
}
}
use of org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Statement in project scout.rt by eclipse.
the class SqlFormatter method wellform.
public String wellform(String s, int tabSize) {
Statement stm = new SqlParser().parse(s);
FormatContext ctx = new FormatContext(tabSize);
formatStatement(stm, ctx);
return ctx.getBuffer().trim();
}
Aggregations