private static Object convertNode(CommonTree node, DatabaseDialect dialect) { switch (node.getType()) { case SQLLexer.CREATE_TABLE: return convertCreateTable(node, dialect); case SQLLexer.DROP_TABLE: return convertDropTable(node); case SQLLexer.ALTER_TABLE: return convertAlterTable(node); case SQLLexer.CREATE_SEQUENCE: return convertCreateSequence(node); case SQLLexer.DROP_SEQUENCE: return convertDropSequence(node); case SQLLexer.CREATE_INDEX: return convertCreateIndex(node); case SQLLexer.COMMENT_TABLE: return convertTableComment(node); case SQLLexer.COMMENT_COLUMN: return convertColumnComment(node); } if (node.isNil()) { List<Object> nodes = convertNodes(getChildNodes(node), dialect); return nodes.toArray(); } throw new ParseException("Unknown token type", "'" + node.getText() + "'"); }
/** * Used by BetweenClauseTest, validates the clause * @param theQuery Query * @param propName String * @param firstValue String * @param secondValue String */ private void betweenChecks(Query theQuery, final String propName, final String firstValue, final String secondValue) { assertNotNull(theQuery); CommonTree tree = theQuery.getTree(); assertNotNull(tree); assertEquals(WhereClauseParser.BETWEEN, tree.getType()); assertEquals(WhereClauseParser.PROPERTYNAME, tree.getChild(0).getType()); assertTrue(propName.equals(tree.getChild(0).getText())); assertEquals(WhereClauseParser.PROPERTYVALUE, tree.getChild(1).getType()); assertEquals(WhereClauseParser.PROPERTYVALUE, tree.getChild(2).getType()); QueryHelper.walk(theQuery, new WalkerCallbackAdapter(){ @Override public void between(String property, String firstVal, String secondVal, boolean negated) { assertTrue("Property name should be "+propName,propName.equals(property)); assertTrue("First value should be "+firstValue,firstValue.equals(firstVal)); assertTrue("Second value should be "+secondValue,secondValue.equals(secondVal)); } }); }
private static void convertColumnSpec(CommonTree node, DBTable table) { String columnName = convertString(childAt(0, node)); String columnTypeName; Integer size = null; Integer fractionDigits = null; int detailOffset = 2; columnTypeName = convertString(childAt(1, node)); if (node.getChildCount() > 2 && childAt(2, node).getType() == SQLLexer.SIZE) { detailOffset++; CommonTree sizeNode = childAt(2, node); size = convertInteger(childAt(0, sizeNode)); if (sizeNode.getChildCount() > 1) { CommonTree subNode2 = childAt(1, sizeNode); if (subNode2.getType() == SQLLexer.INT) { fractionDigits = convertInteger(subNode2); } else { // TODO v1.0 support (n BYTE) / (n CHAR) } } } DBColumn column = new DBColumn(columnName, table, DBDataType.getInstance(columnTypeName), size, fractionDigits); table.addColumn(column); for (int i = detailOffset; i < node.getChildCount(); i++) convertColumnDetail(childAt(i, node), column); }
/** * Used by the matchesClauseTest * @param theQuery Query * @param propName String * @param propVal String */ private void matchesChecks(Query theQuery, final String propName, final String propVal) { assertNotNull(theQuery); CommonTree tree = theQuery.getTree(); assertNotNull(tree); assertEquals(WhereClauseParser.PROPERTYNAME, tree.getChild(0).getType()); assertTrue(propName.equals(tree.getChild(0).getText())); assertEquals(WhereClauseParser.PROPERTYVALUE, tree.getChild(1).getType()); QueryHelper.walk(theQuery, new WalkerCallbackAdapter(){ @Override public void matches(String propertyName, String propertyValue, boolean negated) { assertTrue("Property name should be "+propName,propName.equals(propertyName)); assertTrue("Property value should be "+propVal,propVal.equals(propertyValue)); } }); }
static private Constraint buildFTSTest(CommonTree argNode, QueryModelFactory factory, FunctionEvaluationContext functionEvaluationContext, Selector selector, Map<String, Column> columnMap, String defaultField) { CommonTree testNode = argNode; switch (testNode.getType()) { case CMIS_FTSParser.DISJUNCTION: case CMIS_FTSParser.CONJUNCTION: return buildFTSConnective(testNode, factory, functionEvaluationContext, selector, columnMap, defaultField); case CMIS_FTSParser.TERM: return buildTerm(testNode, factory, functionEvaluationContext, selector, columnMap); case CMIS_FTSParser.PHRASE: return buildPhrase(testNode, factory, functionEvaluationContext, selector, columnMap); default: throw new FTSQueryException("Unsupported FTS option " + testNode.getText()); } }
/** * Use by the inClauseTest * @param theQuery Query * @param propName String * @param values String... */ private void inChecks(Query theQuery, final String propName, final String... values) { assertNotNull(theQuery); CommonTree tree = theQuery.getTree(); assertNotNull(tree); assertEquals(WhereClauseParser.IN, tree.getType()); assertEquals(WhereClauseParser.PROPERTYNAME, tree.getChild(0).getType()); assertTrue(propName.equals(tree.getChild(0).getText())); QueryHelper.walk(theQuery, new WalkerCallbackAdapter(){ @Override public void in(String property, boolean negated, String... propertyValues) { assertTrue("Property name should be "+propName,propName.equals(property)); for (int i = 0; i < values.length; i++) { assertTrue("Value must match:"+values[i],values[i].equals(propertyValues[i])); } } }); }
public static void recursivelyFixWindowFucntion( CommonTree srcTree) { if (srcTree.getChildCount() == 0) { return; } // process node String text = srcTree.getText(); System.out.println("Currently processed node==> " + text); if (text.equals(PARTITION_BY)) { return; } else if (text.equals(ORDER_BY)) { if (srcTree.getChildCount() == 0) { if (srcTree.getParent() != null) { // found a matching node srcTree.getParent().deleteChild(srcTree.getChildIndex()); srcTree.getParent().freshenParentAndChildIndexes(); } } return; } // process children for (Tree t : children(srcTree)) { recursivelyFixWindowFucntion((CommonTree)t); } }
private static Boolean findAggFuncRecursively( CommonTree tree, List<String> aggregateFuncs) { Boolean ret = false; if (tree.getText().equals(FUNCTION_CALL)) { // function String funcName = tree.getChild(0).getChild(0).getText(); //FUNCTION_CALL->QNAME->xxx if (aggregateFuncs.contains(funcName.toLowerCase())) { return true; } } for (Tree t : children(tree)) { ret = findAggFuncRecursively((CommonTree)t, aggregateFuncs); if (ret == true) { return true; } } return false; }
public static void patchTreeByTreesGroupBy(CommonTree srcTree, CommonTree targetTree) { //System.out.println("Text = " + srcTree.getText() + " ChildCount = " + srcTree.getChildCount() + " Type = " + srcTree.getType()); if (srcTree.getChildCount() == 0) { return; } // process node if (srcTree.getText().equals(QUERY_SPEC_NODE)) { // patch targetTree.setParent(srcTree); srcTree.addChild(targetTree); return; } // process children for (Tree t : children(srcTree)) { patchTreeByTreesGroupBy((CommonTree)t, targetTree); } return; }
private static void gatherTypes(CommonTree srcTree, Map<String, Integer> typesMap) { if (srcTree.getChildCount() == 0) { return; } // process node if (!typesMap.containsKey(srcTree.getText())) { typesMap.put(srcTree.getText(), new Integer(srcTree.getType())); } // process children for (Tree subTree : children(srcTree)) { gatherTypes((CommonTree)subTree, typesMap); } return; }
public static CommonTree createGroupByNode(List<String> names) { CommonTree groupByNode = createGenericNode(GENERIC_NODE); groupByNode.token.setText(GROUP_BY_NODE); //groupByNode.token.setType(80); groupByNode.token.setType(findType(GROUP_BY_NODE)); groupByNode.deleteChild(0); for (String name : names) { CommonTree genericNode = createGenericNode(name); genericNode.setParent(groupByNode); groupByNode.addChild(genericNode); } return groupByNode; }
public static void updateTreeByString(CommonTree tree, String target, String goal) { if (tree.getChildCount() == 0) { // leaf if (tree.getText().equals(target)) { //modifyText(tree, goal+'.'+target); modifyText(tree, goal); //System.out.println("Found 1!!!"); } return; } // process node if (tree.getText().equals(target)) { modifyText(tree, goal+'.'+target); //System.out.println("Found 2!!!"); } // process children for (Tree t : children(tree)) { updateTreeByString((CommonTree)t, target, goal); } return; }
public static void printExpression(String sql) { println(sql.trim()); println(""); System.out.println("EXP Printing CommonTree toString..."); CommonTree tree = VeroSqlParser.parseExpression(sql); println(TreePrinter.treeToString(tree)); println(""); System.out.println("EXP Printing AST toString..."); Expression expression = VeroSqlParser.createExpression(tree); println(expression.toString()); println(""); System.out.println("EXP Printing Format sql toString..."); // TODO: support formatting all statement types println(FormatterFactory.getSqlFormatter().formatSql(expression)); println(""); println(repeat("=", 60)); println(""); }
public static void printStatement(String sql) { println(sql.trim()); println(""); System.out.println("STATE Printing CommonTree toString..."); CommonTree tree = VeroSqlParser.parseStatement(sql); println(TreePrinter.treeToString(tree)); println(""); System.out.println("STATE Printing AST toString..."); Statement statement = VeroSqlParser.createStatement(tree); println(statement.toString()); println(""); System.out.println("STATE Printing Format sql toString..."); // TODO: support formatting all statement types if (statement instanceof Query) { println(FormatterFactory.getSqlFormatter().formatSql(statement)); println(""); } println(repeat("=", 60)); println(""); }
/** Creates a qualified name tree by extending an existing one * with an additional fragment in front. * @param init token holding the additional level text * @param subTree the existing tree; may be {@code null}, in which case * the result is calculated using {@link #toQualName(Token,Token)} with first * argument {@code null} */ CommonTree toQualName(Token init, CtrlTree subTree) { CtrlTree result; if (subTree == null || this.namespace.hasErrors()) { result = toQualName(null, init); } else { Token subTop = subTree.getToken(); QualName qualName = subTree.getQualName() .nest(getText(init)); CommonToken top = new CommonToken(subTop.getType(), qualName.toString()); top.setLine(init.getLine()); top.setTokenIndex(init.getTokenIndex()); result = new CtrlTree(top); result.setQualName(qualName); result.addChild(subTree.getChild(0)); } return result; }
private static boolean hasOptionalFieldReference(CommonTree node) { switch (node.getType()) { case FTSParser.TERM: case FTSParser.EXACT_TERM: case FTSParser.PHRASE: case FTSParser.EXACT_PHRASE: case FTSParser.SYNONYM: case FTSParser.PROXIMITY: case FTSParser.RANGE: return true; default: return false; } }
/** * @param orNode CommonTree * @param factory QueryModelFactory * @param functionEvaluationContext FunctionEvaluationContext * @param selectors Map<String, Selector> * @param columnMap HashMap<String, Column> * @return Constraint */ private Constraint buildDisjunction(CommonTree orNode, QueryModelFactory factory, FunctionEvaluationContext functionEvaluationContext, Map<String, Selector> selectors, HashMap<String, Column> columnMap) { List<Constraint> constraints = new ArrayList<Constraint>(orNode.getChildCount()); for (int i = 0; i < orNode.getChildCount(); i++) { CommonTree andNode = (CommonTree) orNode.getChild(i); Constraint constraint = buildConjunction(andNode, factory, functionEvaluationContext, selectors, columnMap); constraints.add(constraint); } if (constraints.size() == 1) { return constraints.get(0); } else { return factory.createDisjunction(constraints); } }
/** * @param andNode CommonTree * @param factory QueryModelFactory * @param functionEvaluationContext FunctionEvaluationContext * @param selectors Map<String, Selector> * @param columnMap HashMap<String, Column> * @return Constraint */ private Constraint buildConjunction(CommonTree andNode, QueryModelFactory factory, FunctionEvaluationContext functionEvaluationContext, Map<String, Selector> selectors, HashMap<String, Column> columnMap) { List<Constraint> constraints = new ArrayList<Constraint>(andNode.getChildCount()); for (int i = 0; i < andNode.getChildCount(); i++) { CommonTree notNode = (CommonTree) andNode.getChild(i); Constraint constraint = buildNegation(notNode, factory, functionEvaluationContext, selectors, columnMap); constraints.add(constraint); } if (constraints.size() == 1 && constraints.get(0).getOccur() != Occur.EXCLUDE) { return constraints.get(0); } else { return factory.createConjunction(constraints); } }
/** * @param notNode CommonTree * @param factory QueryModelFactory * @param functionEvaluationContext FunctionEvaluationContext * @param selectors Map<String, Selector> * @param columnMap HashMap<String, Column> * @return Constraint */ private Constraint buildNegation(CommonTree notNode, QueryModelFactory factory, FunctionEvaluationContext functionEvaluationContext, Map<String, Selector> selectors, HashMap<String, Column> columnMap) { if (notNode.getType() == CMISParser.NEGATION) { Constraint constraint = buildTest((CommonTree) notNode.getChild(0), factory, functionEvaluationContext, selectors, columnMap); constraint.setOccur(Occur.EXCLUDE); return constraint; } else { return buildTest(notNode, factory, functionEvaluationContext, selectors, columnMap); } }
private JavaVMOption<?> extractOption(CommandLineParser.vmOptions_return options_return) { CommonTree root = (CommonTree) options_return.getTree(); if (root instanceof JavaVMOption<?>) { return (JavaVMOption<?>) root; } else if (root != null) { return (JavaVMOption<?>) root.getChildren().get(0); } return null; }
@SuppressWarnings("rawtypes") private static Expression convertExpressionNode(CommonTree node) { switch (node.getType()) { case SQLLexer.OR: return convertOr(node); case SQLLexer.AND: return convertAnd(node); case SQLLexer.XOR: return convertXor(node); case SQLLexer.EQ: return convertEq(node); case SQLLexer.BANGEQ: return convertBangEq(node); case SQLLexer.LTGT: return convertBangEq(node); // <> case SQLLexer.GT: return convertGt(node); case SQLLexer.GE: return convertGe(node); case SQLLexer.LT: return convertLt(node); case SQLLexer.LE: return convertLe(node); case SQLLexer.IS: return convertIs(node); case SQLLexer.NOT: return convertNot(node); case SQLLexer.NULL: return convertNull(node); case SQLLexer.LIKE: return convertLike(node); case SQLLexer.IN: return convertIn(node); case SQLLexer.BETWEEN: return convertBetween(node); case SQLLexer.PLUS: return convertPlus(node); case SQLLexer.SUB: return convertSub(node); case SQLLexer.STAR: return convertStar(node); case SQLLexer.SLASH: return convertSlash(node); case SQLLexer.PERCENT: return convertPercent(node); case SQLLexer.BARBAR: return convertBarBar(node); case SQLLexer.INVOCATION: return convertInvocation(node); case SQLLexer.QUOTED_NAME: return convertQuotedName(node); case SQLLexer.IDENTIFIER: return convertIdentifier(node); case SQLLexer.STRING: return convertStringToExpression(node); case SQLLexer.INT: return convertInt(node); default: throw new ParseException("Unknown token type (" + node.getType() + ")", "'" + node.getText() + "'"); } }
@SuppressWarnings("unchecked") private static Expression<Boolean> convertOr(CommonTree node) { ConditionalOrExpression result = new ConditionalOrExpression("OR"); for (CommonTree childNode : getChildNodes(node)) result.addTerm(convertExpressionNode(childNode)); return result; }
@SuppressWarnings("unchecked") private static Expression<Boolean> convertAnd(CommonTree node) { ConditionalAndExpression result = new ConditionalAndExpression("AND"); for (CommonTree childNode : getChildNodes(node)) result.addTerm(convertExpressionNode(childNode)); return result; }
private static Expression<?> convertLike(CommonTree node) { Expression<?> valueEx = convertExpressionNode(childAt(0, node)); CommonTree child1 = childAt(1, node); boolean not = (child1.getType() == SQLLexer.NOT); int collectionIndex = (not ? 2 : 1); Expression<?> refEx = convertExpressionNode(childAt(collectionIndex, node)); Expression<?> result = new LikeExpression(valueEx, refEx); if (not) result = new LogicalComplementExpression(result); return result; }
private static Expression<?> convertIn(CommonTree node) { Expression<?> valueEx = convertExpressionNode(childAt(0, node)); CommonTree child1 = childAt(1, node); boolean not = (child1.getType() == SQLLexer.NOT); int collectionIndex = (not ? 2 : 1); Expression<? extends Collection<?>> collEx = convertValueList(childAt(collectionIndex, node)); Expression<?> result = new ValueCollectionContainsExpression("IN", valueEx, collEx); if (not) result = new LogicalComplementExpression(result); return result; }
@SuppressWarnings("unchecked") private static Expression<?> convertPlus(CommonTree node) { SumExpression result = new SumExpression(); for (CommonTree child : getChildNodes(node)) result.addTerm(convertExpressionNode(child)); return result; }
@SuppressWarnings("unchecked") private static Expression<?> convertSub(CommonTree node) { if (node.getChildCount() == 1) { return new UnaryMinusExpression<Object>(convertExpressionNode(childAt(0, node))); } else { SubtractionExpression result = new SubtractionExpression(); for (CommonTree child : getChildNodes(node)) result.addTerm(convertExpressionNode(child)); return result; } }
@SuppressWarnings("unchecked") private static Expression<?> convertStar(CommonTree node) { MultiplicationExpression result = new MultiplicationExpression(); for (CommonTree child : getChildNodes(node)) result.addTerm(convertExpressionNode(child)); return result; }
@SuppressWarnings("unchecked") private static Expression<?> convertSlash(CommonTree node) { DivisionExpression result = new DivisionExpression(); for (CommonTree child : getChildNodes(node)) result.addTerm(convertExpressionNode(child)); return result; }
@SuppressWarnings("rawtypes") private static Expression<?>[] convertArguments(CommonTree node) { ArrayBuilder<Expression> result = new ArrayBuilder<Expression>(Expression.class); for (CommonTree child : getChildNodes(node)) result.add(convertExpressionNode(child)); return result.toArray(); }
static private Constraint buildTerm(CommonTree testNode, QueryModelFactory factory, FunctionEvaluationContext functionEvaluationContext, Selector selector, Map<String, Column> columnMap) { String functionName = FTSTerm.NAME; Function function = factory.getFunction(functionName); Map<String, Argument> functionArguments = new LinkedHashMap<String, Argument>(); LiteralArgument larg = factory.createLiteralArgument(FTSTerm.ARG_TERM, DataTypeDefinition.TEXT, getText(testNode.getChild(0))); functionArguments.put(larg.getName(), larg); larg = factory.createLiteralArgument(FTSTerm.ARG_TOKENISATION_MODE, DataTypeDefinition.ANY, AnalysisMode.DEFAULT); functionArguments.put(larg.getName(), larg); return factory.createFunctionalConstraint(function, functionArguments); }
static private Constraint buildPhrase(CommonTree testNode, QueryModelFactory factory, FunctionEvaluationContext functionEvaluationContext, Selector selector, Map<String, Column> columnMap) { String functionName = FTSPhrase.NAME; Function function = factory.getFunction(functionName); Map<String, Argument> functionArguments = new LinkedHashMap<String, Argument>(); LiteralArgument larg = factory.createLiteralArgument(FTSPhrase.ARG_PHRASE, DataTypeDefinition.TEXT, getText(testNode.getChild(0))); functionArguments.put(larg.getName(), larg); larg = factory.createLiteralArgument(FTSPhrase.ARG_TOKENISATION_MODE, DataTypeDefinition.ANY, AnalysisMode.DEFAULT); functionArguments.put(larg.getName(), larg); return factory.createFunctionalConstraint(function, functionArguments); }
private static DBTable convertCreateTable(CommonTree node, DatabaseDialect dialect) { String tableName = convertString(childAt(0, node)); DBTable table = new DBTable(tableName); convertTableDetails(childAt(1, node), table, dialect); // TODO v1.0 parse ora_configs return table; }
public static CommonTree compileSelectClause(String selectParam) throws RecognitionException { //lexer splits input into tokens ANTLRStringStream input = new ANTLRStringStream(selectParam); TokenStream tokens = new CommonTokenStream(new WhereClauseLexer(input)); //parser generates abstract syntax tree WhereClauseParser parser = new WhereClauseParser(tokens); WhereClauseParser.selectClause_return ret = parser.selectClause(); //acquire parse result CommonTree ast = (CommonTree) ret.getTree(); if (logger.isDebugEnabled()) print(ast, 0); return ast; }
/** * Takes the "where" parameter and turns it into a Java Object that can be used for querying * * @param whereParam String * @return Query a parsed version of the where clause, represented in Java */ default Query getWhereClause(String whereParam) throws InvalidQueryException { if (whereParam == null) return QueryImpl.EMPTY; try { CommonTree whereTree = WhereCompiler.compileWhereClause(whereParam); if (whereTree instanceof CommonErrorNode) { rpeLogger().debug("Error parsing the WHERE clause " + whereTree); throw new InvalidQueryException(whereTree); } return new QueryImpl(whereTree); } catch (RewriteCardinalityException re) { //Catch any error so it doesn't get thrown up the stack rpeLogger().info("Unhandled Error parsing the WHERE clause: " + re); } catch (RecognitionException e) { whereParam += ", " + WhereCompiler.resolveMessage(e); rpeLogger().info("Error parsing the WHERE clause: " + whereParam); } //Default to throw out an invalid query throw new InvalidQueryException(whereParam); }
/** * Walks a query with a callback for each operation * @param query the query * @param callback a callback */ public static void walk(Query query, WalkerCallback callback) { CommonTree tree = query.getTree(); if (tree != null) { LinkedList<Tree> stack = new LinkedList<Tree>(); stack.push(tree); callbackTree(tree, callback, false); } }
private static void convertColumnDetail(CommonTree node, DBColumn column) { switch (node.getType()) { case SQLLexer.NOT : column.setNullable(false); break; case SQLLexer.DEFAULT : column.setNullable(false); break; default: throw new ParseException("Unknown column detail token type", String.valueOf(node.getText()), node.getLine(), node.getCharPositionInLine()); } }
@Test public void testTotal() throws RecognitionException, IOException { CommonTree t = (CommonTree)parse(sampleCompany).getTree(); CommonTreeNodeStream nodes = new CommonTreeNodeStream(t); Total total = new Total(nodes); total.company(); assertEquals(399747, total.total, 0); }
public static CommonTree compileWhereClause(String expression) throws RecognitionException { //lexer splits input into tokens ANTLRStringStream input = new ANTLRStringStream(expression); TokenStream tokens = new CommonTokenStream(new WhereClauseLexer(input)); //parser generates abstract syntax tree WhereClauseParser parser = new WhereClauseParser(tokens); WhereClauseParser.whereclause_return ret = parser.whereclause(); //acquire parse result CommonTree ast = (CommonTree) ret.getTree(); if (logger.isDebugEnabled()) print(ast, 0); return ast; }
private static void printTree(CommonTree t, int indent) { if ( t != null ) { indent(indent); System.out.println(t.toString()); for (int i = 0; i < t.getChildCount(); i++ ) { printTree((CommonTree)t.getChild(i), indent+1); } } }