public static <BaseClass, Element> IFilter<BaseClass, Element> makeFilter(CommonTree tree) {
    String tokenText = getTokenText(tree);
    if (tokenText == null) return null;
    char firstChar = tokenText.charAt(0);
    if (AND.equals(tokenText)) {
      CommonTree child0 = (CommonTree) tree.getChild(0);
      CommonTree child1 = (CommonTree) tree.getChild(1);
      IFilter<BaseClass, Element> filter0 = makeFilter(child0);
      IFilter<BaseClass, Element> filter1 = makeFilter(child1);
      return new AndFilter<BaseClass, Element>(filter0, filter1);
    } else if (OR.equals(tokenText)) {
      CommonTree child0 = (CommonTree) tree.getChild(0);
      CommonTree child1 = (CommonTree) tree.getChild(1);
      IFilter<BaseClass, Element> filter0 = makeFilter(child0);
      IFilter<BaseClass, Element> filter1 = makeFilter(child1);
      return new OrFilter<BaseClass, Element>(filter0, filter1);
    } else if (NOT.equals(tokenText)) {
      CommonTree child0 = (CommonTree) tree.getChild(0);
      IFilter<BaseClass, Element> filter0 = makeFilter(child0);
      return new NotFilter<BaseClass, Element>(filter0);
    } else if (CONSTRAINT.equals(tokenText)) {
      CommonTree child0 = (CommonTree) tree.getChild(0);
      CommonTree child1 = null;
      if (tree.getChildCount() == 1) {
        return makeFilter(getTokenText(child0));
      } else if (tree.getChildCount() == 2) {
        child1 = (CommonTree) tree.getChild(1);
        return makeFilter(getTokenText(child0), parseCronstraintParameterTree(child1));
      }
      return null;
    } else {

    }
    return null;
  }
  private int getElementOffset(CommonTree tree) {
    switch (tree.getType()) {
      case ANTLRParser.MODE:
      case ANTLRParser.ASSIGN:
      case ANTLRParser.RULE:
        if (tree.getChildCount() > 0 && tree.getChild(0) instanceof CommonTree) {
          CommonTree child = (CommonTree) tree.getChild(0);
          if (child.getToken() instanceof CommonToken) {
            CommonToken token = (CommonToken) child.getToken();
            return token.getStartIndex();
          }
        }

        break;

      case ANTLRParser.ID:
        break;

      default:
        throw new UnsupportedOperationException();
    }

    if (tree.getToken() instanceof CommonToken) {
      return ((CommonToken) tree.getToken()).getStartIndex();
    }

    return 0;
  }
 private AssignNode constructAssignNode(CommonTree tree) {
   if (!checkNode(tree)) {
     return null;
   }
   IdNode id = constructIdNode((CommonTree) tree.getChild(0));
   ExprNode expr = constructExprNode((CommonTree) tree.getChild(1));
   return new AssignNode(id, expr, tree.getLine());
 }
 private BinaryFunctionNode constructBinFuncNode(BinaryFunction binFunc, CommonTree tree) {
   if (!checkNode(tree)) {
     return null;
   }
   IdNode id = constructIdNode((CommonTree) tree.getChild(0));
   ExprNode expr = constructExprNode((CommonTree) tree.getChild(1));
   return new BinaryFunctionNode(binFunc, id, expr, tree.getLine());
 }
  public WhileStatement(CommonTree MyTree, Model MyModel) {
    setLine(MyTree.getLine());
    this.MyModel = MyModel;

    Comparison = ExpressionFactory.getExpression((CommonTree) MyTree.getChild(0), MyModel);
    if (MyTree.getChildCount() > 1) {
      for (int i = 1; i < MyTree.getChildCount(); i++)
        Statements.add(StatementFactory.getStatement((CommonTree) MyTree.getChild(i), MyModel));
    }
  }
  private static String getVariableName(CommonTree t, StringBuilder sb) {
    if (t != null) {
      for (int i = 0; i < t.getChildCount(); i++) {
        sb.append(t.getChild(i).toString());
        getVariableName((CommonTree) t.getChild(i), sb);
      }
    }

    return sb.toString();
  }
Beispiel #7
0
 private String getPrimaryKeyFieldName(CommonTree whereNode) {
   String result = null;
   CommonTree operation = (CommonTree) whereNode.getChild(0);
   if (MySQL51Parser.EQUALS == operation.getType()) {
     result = operation.getChild(0).getChild(0).getText();
   } else {
     throw new ClusterJUserException("Cannot find primary key in WHERE clause.");
   }
   return result;
 }
Beispiel #8
0
 /**
  * @param ast
  * @return
  */
 private boolean isCreateVirtualTable(CommonTree ast) {
   final CommonTree optionsNode = (CommonTree) ast.getChild(0);
   for (int i = 0; i < optionsNode.getChildCount(); i++) {
     CommonTree optionNode = (CommonTree) optionsNode.getChild(i);
     if ("virtual".equalsIgnoreCase(optionNode.getText())) {
       return true;
     }
   }
   return false;
 }
 public SqlJetBindParameter(CommonTree ast) {
   if ("bind_name".equalsIgnoreCase(ast.getText())) {
     position = null;
     name = ast.getChild(0).getText();
   } else {
     assert "bind".equalsIgnoreCase(ast.getText());
     position = (ast.getChildCount() > 0) ? Integer.valueOf(ast.getChild(0).getText()) : null;
     name = null;
   }
 }
Beispiel #10
0
  public SqlJetViewDef(String sql, CommonTree ast) {
    CommonTree optionsNode = (CommonTree) ast.getChild(0);

    sqlStatement = sql;
    temporary = SqlJetTableDef.hasOption(optionsNode, "temporary");
    ifNotExists = SqlJetTableDef.hasOption(optionsNode, "exists");

    CommonTree nameNode = (CommonTree) ast.getChild(1);
    name = nameNode.getText();
    databaseName = nameNode.getChildCount() > 0 ? nameNode.getChild(0).getText() : null;
  }
  /*
   * def test_variable_middle
   *   template = Liquid::Template.parse("  {{funk}}  ")
   *   assert_equal 3, template.root.nodelist.size
   *   assert_equal String, template.root.nodelist[0].class
   *   assert_equal Variable, template.root.nodelist[1].class
   *   assert_equal String, template.root.nodelist[2].class
   * end
   */
  @Test
  public void variableMiddleTest() throws RecognitionException {

    CommonTree root = Template.parse("  {{funk}}  ").getAST();

    assertThat(root.getChildCount(), is(3));

    assertThat(root.getChild(0).getType(), is(LiquidLexer.PLAIN));
    assertThat(root.getChild(1).getChild(0).getType(), is(LiquidLexer.LOOKUP));
    assertThat(root.getChild(2).getType(), is(LiquidLexer.PLAIN));
  }
  /*
   * def test_with_block
   *   template = Liquid::Template.parse("  {% comment %} {% endcomment %} ")
   *   assert_equal [String, Comment, String], block_types(template.root.nodelist)
   *   assert_equal 3, template.root.nodelist.size
   * end
   */
  @Test
  public void blockTest() throws RecognitionException {

    CommonTree root = Template.parse("  {% comment %} {% endcomment %} ").getAST();

    assertThat(root.getChildCount(), is(3));

    assertThat(root.getChild(0).getType(), is(LiquidLexer.PLAIN));
    assertThat(root.getChild(1).getType(), is(LiquidLexer.COMMENT));
    assertThat(root.getChild(2).getType(), is(LiquidLexer.PLAIN));
  }
 private Pair<BExprNode, IdNode> constructChangeStatement(CommonTree tree) {
   if (!checkNode(tree)) {
     return null;
   }
   CommonTree bexpr = (CommonTree) tree.getChild(0);
   BExprNode bExprNode = null;
   if (bexpr.getToken().getType() != BACONParser.OTHERWISE) {
     bExprNode = constructBExprNode((CommonTree) tree.getChild(0));
   }
   return new Pair<BExprNode, IdNode>(bExprNode, constructIdNode((CommonTree) tree.getChild(1)));
 }
 public static UIQueryASTPredicate newPredicateFromAST(CommonTree step) {
   // TODO Auto-generated method stub
   if (step.getChildCount() != 3) {
     throw new IllegalStateException(
         "Bad Predicate query: " + step + ". Expected form {getter RELATION value}.");
   }
   CommonTree prop = (CommonTree) step.getChild(0);
   CommonTree rel = (CommonTree) step.getChild(1);
   CommonTree val = (CommonTree) step.getChild(2);
   return new UIQueryASTPredicate(
       prop.getText(), UIQueryASTPredicate.parseRelation(rel), UIQueryUtils.parseValue(val));
 }
 /*
  * Each of these are individual rules for constructing different types of ASTreeNodes
  */
 private RuleSequenceNode constructRuleSeqNode(CommonTree tree) {
   if (checkNode(tree)) {
     if (tree.getChildCount() == 1) {
       RuleNode rule = constructRuleNode((CommonTree) tree.getChild(0));
       return new RuleSequenceNode(rule);
     }
     CommonTree ruleNameNode = (CommonTree) tree.getChild(0);
     String ruleName = ruleNameNode.getToken().getText();
     RuleNode rule = constructRuleNode((CommonTree) tree.getChild(1));
     return new RuleSequenceNode(ruleName, rule);
   }
   return null;
 }
 private void setExceptionClass(CommonTree tree) {
   if (tree != null) {
     for (int index = 0; index < tree.getChildCount(); index++) {
       if (tree.getType() == typeIdentifierNode) {
         this.exceptionClass = this.parserUniquename((CommonTree) tree);
       } else if (tree.getType() == 156) {
         this.exceptionClass = this.parserUniquename((CommonTree) tree.getChild(0));
       } else {
         setExceptionClass((CommonTree) tree.getChild(index));
       }
     }
   }
 }
 private String packageClassPath(CommonTree tree) {
   String path = "";
   int totalElements = tree.getChildCount();
   for (int iterator = 0; iterator < totalElements; iterator++) {
     if (tree.getChild(iterator).getType() == JavaParser.DOT) {
       path += packageClassPath((CommonTree) tree.getChild(iterator));
     } else {
       path += !path.equals("") ? "." : "";
       path += tree.getChild(iterator).getText();
     }
   }
   return path;
 }
Beispiel #18
0
  // Execute SET statement
  private void executeSet(CommonTree ast)
      throws TException, InvalidRequestException, UnavailableException, TimedOutException,
          UnsupportedEncodingException {
    if (!CliMain.isConnected()) return;

    assert (ast.getChildCount() == 2) : "serious parsing error (this is a bug).";

    CommonTree columnFamilySpec = (CommonTree) ast.getChild(0);
    if (!(columnFamilySpec.getType() == CliParser.NODE_COLUMN_ACCESS)) return;

    String tableName = CliCompiler.getTableName(columnFamilySpec);
    String key = CliCompiler.getKey(columnFamilySpec);
    String columnFamily = CliCompiler.getColumnFamily(columnFamilySpec);
    int columnSpecCnt = CliCompiler.numColumnSpecifiers(columnFamilySpec);
    String value = CliUtils.unescapeSQLString(ast.getChild(1).getText());

    byte[] superColumnName = null;
    byte[] columnName = null;

    // table.cf['key']
    if (columnSpecCnt == 0) {
      css_.err.println("No column name specified, (type 'help' or '?' for help on syntax).");
      return;
    }
    // table.cf['key']['column'] = 'value'
    else if (columnSpecCnt == 1) {
      // get the column name
      columnName = CliCompiler.getColumn(columnFamilySpec, 0).getBytes("UTF-8");
    }
    // table.cf['key']['super_column']['column'] = 'value'
    else {
      assert (columnSpecCnt == 2) : "serious parsing error (this is a bug).";

      // get the super column and column names
      superColumnName = CliCompiler.getColumn(columnFamilySpec, 0).getBytes("UTF-8");
      columnName = CliCompiler.getColumn(columnFamilySpec, 1).getBytes("UTF-8");
    }

    // do the insert
    thriftClient_.insert(
        tableName,
        key,
        createColumnPath(columnFamily, superColumnName, columnName),
        value.getBytes(),
        timestampMicros(),
        ConsistencyLevel.ONE);

    css_.out.println("Value inserted.");
  }
  /*
   * def test_variable_many_embedded_fragments
   *   template = Liquid::Template.parse("  {{funk}} {{so}} {{brother}} ")
   *   assert_equal 7, template.root.nodelist.size
   *   assert_equal [String, Variable, String, Variable, String, Variable, String],
   *                block_types(template.root.nodelist)
   * end
   */
  @Test
  public void variableManyEmbeddedFragmentsTest() throws RecognitionException {

    CommonTree root = Template.parse("  {{funk}} {{so}} {{brother}} ").getAST();

    assertThat(root.getChildCount(), is(7));

    assertThat(root.getChild(0).getType(), is(LiquidLexer.PLAIN));
    assertThat(root.getChild(1).getChild(0).getType(), is(LiquidLexer.LOOKUP));
    assertThat(root.getChild(2).getType(), is(LiquidLexer.PLAIN));
    assertThat(root.getChild(3).getChild(0).getType(), is(LiquidLexer.LOOKUP));
    assertThat(root.getChild(4).getType(), is(LiquidLexer.PLAIN));
    assertThat(root.getChild(5).getChild(0).getType(), is(LiquidLexer.LOOKUP));
    assertThat(root.getChild(6).getType(), is(LiquidLexer.PLAIN));
  }
Beispiel #20
0
 private String getCoreSQL(RuleReturnScope parsedSQL) {
   final CommonTree ast = (CommonTree) parsedSQL.getTree();
   final CommonToken nameToken = (CommonToken) ((CommonTree) ast.getChild(1)).getToken();
   final CharStream inputStream = nameToken.getInputStream();
   final CommonToken stopToken = (CommonToken) parsedSQL.getStop();
   return inputStream.substring(nameToken.getStartIndex(), stopToken.getStopIndex());
 }
 private VOpNode constructVBOpNode(VOperator vOp, CommonTree tree) {
   if (checkNode(tree)) {
     ExprNode expr = constructExprNode((CommonTree) tree.getChild(0));
     return new VOpNode(vOp, expr, tree.getLine());
   }
   return null;
 }
 private VBOpNode constructVBOpNode(VBoolOperator vBOp, CommonTree tree) {
   if (!checkNode(tree)) {
     return null;
   }
   BExprNode bExpr = constructBExprNode((CommonTree) tree.getChild(0));
   return new VBOpNode(vBOp, bExpr, tree.getLine());
 }
Beispiel #23
0
  private void executeCount(CommonTree ast)
      throws TException, InvalidRequestException, UnavailableException, TimedOutException,
          UnsupportedEncodingException {
    if (!CliMain.isConnected()) return;

    int childCount = ast.getChildCount();
    assert (childCount == 1);

    CommonTree columnFamilySpec = (CommonTree) ast.getChild(0);
    if (!(columnFamilySpec.getType() == CliParser.NODE_COLUMN_ACCESS)) return;

    String tableName = CliCompiler.getTableName(columnFamilySpec);
    String key = CliCompiler.getKey(columnFamilySpec);
    String columnFamily = CliCompiler.getColumnFamily(columnFamilySpec);
    int columnSpecCnt = CliCompiler.numColumnSpecifiers(columnFamilySpec);

    ColumnParent colParent;

    if (columnSpecCnt == 0) {
      colParent = createColumnParent(columnFamily, null);
    } else {
      assert (columnSpecCnt == 1);
      colParent =
          createColumnParent(
              columnFamily, CliCompiler.getColumn(columnFamilySpec, 0).getBytes("UTF-8"));
    }

    int count = thriftClient_.get_count(tableName, key, colParent, ConsistencyLevel.ONE);
    css_.out.printf("%d columns\n", count);
  }
Beispiel #24
0
  // process a statement of the form: describe table <tablename>
  private void executeDescribeTable(CommonTree ast) throws TException {
    if (!CliMain.isConnected()) return;

    // Get table name
    int childCount = ast.getChildCount();
    assert (childCount == 1);

    String tableName = ast.getChild(0).getText();

    if (tableName == null) {
      css_.out.println("Keyspace argument required");
      return;
    }

    // Describe and display
    Map<String, Map<String, String>> columnFamiliesMap;
    try {
      columnFamiliesMap = thriftClient_.describe_keyspace(tableName);
      for (String columnFamilyName : columnFamiliesMap.keySet()) {
        Map<String, String> columnMap = columnFamiliesMap.get(columnFamilyName);
        String desc = columnMap.get("Desc");
        String columnFamilyType = columnMap.get("Type");
        String sort = columnMap.get("CompareWith");
        String flushperiod = columnMap.get("FlushPeriodInMinutes");
        css_.out.println(desc);
        css_.out.println("Column Family Type: " + columnFamilyType);
        css_.out.println("Column Sorted By: " + sort);
        css_.out.println("flush period: " + flushperiod + " minutes");
        css_.out.println("------");
      }
    } catch (NotFoundException e) {
      css_.out.println("Keyspace " + tableName + " could not be found.");
    }
  }
Beispiel #25
0
  /**
   * @param sql
   * @param alterTableDef
   * @return
   * @throws SqlJetException
   */
  private String getTableAlteredSql(String sql, SqlJetAlterTableDef alterTableDef)
      throws SqlJetException {

    final RuleReturnScope parsedSQL = parseTable(sql);
    final CommonTree ast = (CommonTree) parsedSQL.getTree();
    final CommonToken nameToken = (CommonToken) ((CommonTree) ast.getChild(1)).getToken();
    final CharStream inputStream = nameToken.getInputStream();
    final CommonToken stopToken = (CommonToken) parsedSQL.getStop();

    final StringBuilder b = new StringBuilder();

    if (alterTableDef.getNewTableName() != null) {
      b.append(inputStream.substring(0, nameToken.getStartIndex() - 1));
      b.append(getAlterTableName(alterTableDef));
      b.append(inputStream.substring(nameToken.getStopIndex() + 1, stopToken.getStopIndex()));
    } else if (alterTableDef.getNewColumnDef() != null) {
      b.append(inputStream.substring(0, stopToken.getStartIndex() - 1));
      b.append(",").append(getAlterTableName(alterTableDef));
      b.append(inputStream.substring(stopToken.getStartIndex(), stopToken.getStopIndex()));
    } else {
      throw new SqlJetException("Wrong ALTER TABLE statement");
    }

    return b.toString();
  }
 private BinaryPrimitiveNode constructBinPrimNode(BinaryPrimitive prim, CommonTree tree) {
   if (checkNode(tree)) {
     ExprNode lExpr = constructExprNode((CommonTree) tree.getChild(0));
     ExprNode rExpr = constructExprNode((CommonTree) tree.getChild(1));
     return new BinaryPrimitiveNode(prim, lExpr, rExpr, tree.getLine());
   }
   return null;
 }
 private BinOpNode constructBinOpNode(MathematicalOperator op, CommonTree tree) {
   if (checkNode(tree)) {
     ExprNode lExpr = constructExprNode((CommonTree) tree.getChild(0));
     ExprNode rExpr = constructExprNode((CommonTree) tree.getChild(1));
     return new BinOpNode(op, lExpr, rExpr, tree.getLine());
   }
   return null;
 }
 private BooleanBinOpNode constructBooleanBinOpNode(BooleanBinOperator binOp, CommonTree tree) {
   if (!checkNode(tree)) {
     return null;
   }
   BExprNode lBExpr = constructBExprNode((CommonTree) tree.getChild(0));
   BExprNode rBExpr = constructBExprNode((CommonTree) tree.getChild(1));
   return new BooleanBinOpNode(binOp, lBExpr, rBExpr, tree.getLine());
 }
 private BooleanComparitorNode constructBooleanCompNode(ComparisonOperator comp, CommonTree tree) {
   if (!checkNode(tree)) {
     return null;
   }
   ExprNode lExpr = constructExprNode((CommonTree) tree.getChild(0));
   ExprNode rExpr = constructExprNode((CommonTree) tree.getChild(1));
   return new BooleanComparitorNode(comp, lExpr, rExpr, tree.getLine());
 }
Beispiel #30
0
 /**
  * @param alterTableDef
  * @return
  */
 private String getAlterTableName(SqlJetAlterTableDef alterTableDef) {
   final ParserRuleReturnScope parsedSql = alterTableDef.getParsedSql();
   final CommonTree ast = (CommonTree) parsedSql.getTree();
   final CommonToken stopToken = (CommonToken) parsedSql.getStop();
   final CommonToken nameToken =
       (CommonToken) ((CommonTree) ast.getChild(ast.getChildCount() - 1)).getToken();
   final CharStream inputStream = nameToken.getInputStream();
   return inputStream.substring(nameToken.getStartIndex(), stopToken.getStopIndex());
 }