public OrmQueryDetailParser(String oql) { this.parser = new SimpleTextParser(oql); }
public int findWordLower(String lowerMatch, int afterPos) { this.pos = afterPos; return findWordLower(lowerMatch); }
/** * Match the current and the next word. */ public boolean isMatch(String lowerMatch, String nextWordMatch) { if (isMatch(lowerMatch)) { String nw = peekNextWord(); if (nw != null) { nw = nw.toLowerCase(); return nw.equals(nextWordMatch); } } return false; }
private void moveToEndOfWord() { char c = chars[pos]; boolean isOperator = isOperator(c); for (; pos < eof; pos++) { c = chars[pos]; if (isWordTerminator(c, isOperator)) { return; } } }
private String nextWordInternal() { trimLeadingWhitespace(); if (pos >= eof) { return null; } int start = pos; if (chars[pos] == '(') { moveToClose(); } else { moveToEndOfWord(); } return oql.substring(start, pos); }
private void readLimit() { try { String maxLimit = parser.nextWord(); maxRows = Integer.parseInt(maxLimit); String offsetKeyword = parser.nextWord(); if (offsetKeyword != null) { if (!parser.isMatch("offset")) { throw new PersistenceException("expected offset keyword but got " + parser.getWord()); } String firstRowLimit = parser.nextWord(); firstRow = Integer.parseInt(firstRowLimit); parser.nextWord(); } } catch (NumberFormatException e) { String msg = "Expected an integer for maxRows or firstRows in limit offset clause"; throw new PersistenceException(msg, e); } }
private void readOrderBy() { // read the by parser.nextWord(); StringBuilder sb = new StringBuilder(); while (parser.nextWord() != null) { if (parser.isMatch("limit")) { break; } else { String w = parser.getWord(); if (!w.startsWith("(")) { sb.append(" "); } sb.append(w); } } rawOrderBy = sb.toString().trim(); if (!parser.isFinished()) { readLimit(); } }
private void process() { if (isFetch()) { OrmQueryProperties props = readFindFetch(); detail.putFetchPath(props); } else if (parser.isMatch("where")) { readWhere(); } else if (parser.isMatch("order", "by")) { readOrderBy(); } else if (parser.isMatch("limit")) { readLimit(); } else { throw new PersistenceException("Query expected 'fetch', 'where','order by' or 'limit' keyword but got [" + parser.getWord() + "] \r " + parser.getOql()); } }
private void parseSqlFindKeywords(boolean allKeywords) { selectPos = textParser.findWordLower("select"); if (selectPos == -1) { String msg = "Error parsing sql, can not find SELECT keyword in:"; String possibleDistinct = textParser.nextWord(); if ("distinct".equals(possibleDistinct)) { distinctPos = textParser.getPos() - 8; fromPos = textParser.findWordLower("from"); if (fromPos == -1) { String msg = "Error parsing sql, can not find FROM keyword in:"; wherePos = textParser.findWordLower("where"); if (wherePos == -1) { groupByPos = textParser.findWordLower("group", fromPos + 5); } else { groupByPos = textParser.findWordLower("group"); havingPos = textParser.findWordLower("having"); orderByPos = textParser.findWordLower("order", startOrderBy);
private void processInitial() { if (parser.isMatch("select")) { readSelect(); } else if (parser.isMatch("find")) { OrmQueryProperties props = readFindFetch(); detail.setBase(props); } else { process(); } while (!parser.isFinished()) { process(); } }
private OrmQueryProperties readFindFetch() { boolean readAlias = false; String props = null; String path = parser.nextWord(); String token; while ((token = parser.nextWord()) != null) { if (!readAlias && parser.isMatch("as")) { // next token is alias parser.nextWord(); readAlias = true; } else if ('(' == token.charAt(0)) { props = token; parser.nextWord(); break; } else if (isFindFetchEnd()) { break; } else if (!readAlias) { readAlias = true; } else { throw new PersistenceException("Expected (props) or new 'fetch' 'where' but got " + token); } } if (props != null) { props = props.substring(1, props.length() - 1); } return new OrmQueryProperties(path, props); }
private boolean isWordTerminator(char c, boolean isOperator) { if (Character.isWhitespace(c)) { return true; } if (isOperator(c)) { return !isOperator; } if (c == '('){ return true; } return isOperator; }
private void readLimit() { try { String maxLimit = parser.nextWord(); maxRows = Integer.parseInt(maxLimit); String offsetKeyword = parser.nextWord(); if (offsetKeyword != null) { if (!parser.isMatch("offset")) { throw new PersistenceException("expected offset keyword but got " + parser.getWord()); } String firstRowLimit = parser.nextWord(); firstRow = Integer.parseInt(firstRowLimit); parser.nextWord(); } } catch (NumberFormatException e) { String msg = "Expected an integer for maxRows or firstRows in limit offset clause"; throw new PersistenceException(msg, e); } }
private void readOrderBy() { // read the by parser.nextWord(); StringBuilder sb = new StringBuilder(); while (parser.nextWord() != null) { if (parser.isMatch("limit")) { break; } else { String w = parser.getWord(); if (!w.startsWith("(")) { sb.append(" "); } sb.append(w); } } rawOrderBy = sb.toString().trim(); if (!parser.isFinished()) { readLimit(); } }
private String nextWordInternal() { trimLeadingWhitespace(); if (pos >= eof) { return null; } int start = pos; if (chars[pos] == '(') { moveToClose(); } else { moveToEndOfWord(); } return oql.substring(start, pos); }
private void process() { if (isFetch()) { OrmQueryProperties props = readFindFetch(); detail.putFetchPath(props); } else if (parser.isMatch("where")) { readWhere(); } else if (parser.isMatch("order", "by")) { readOrderBy(); } else if (parser.isMatch("limit")) { readLimit(); } else { throw new PersistenceException("Query expected 'fetch', 'where','order by' or 'limit' keyword but got [" + parser.getWord() + "] \r " + parser.getOql()); } }
private void parseSqlFindKeywords(boolean allKeywords) { selectPos = textParser.findWordLower("select"); if (selectPos == -1) { String msg = "Error parsing sql, can not find SELECT keyword in:"; String possibleDistinct = textParser.nextWord(); if ("distinct".equals(possibleDistinct)) { distinctPos = textParser.getPos() - 8; fromPos = textParser.findWordLower("from"); if (fromPos == -1) { String msg = "Error parsing sql, can not find FROM keyword in:"; wherePos = textParser.findWordLower("where"); if (wherePos == -1) { groupByPos = textParser.findWordLower("group", fromPos + 5); } else { groupByPos = textParser.findWordLower("group"); havingPos = textParser.findWordLower("having"); orderByPos = textParser.findWordLower("order", startOrderBy); if (orderByPos > 1) { orderByStmtPos = 2 + textParser.findWordLower("by", orderByPos);
private void moveToEndOfWord() { char c = chars[pos]; boolean isOperator = isOperator(c); for (; pos < eof; pos++) { c = chars[pos]; if (isWordTerminator(c, isOperator)) { return; } } }