Пример #1
0
 private boolean validateColumnsRecurse(TExpression condition, TJoinList joins) {
   if (condition.getLeftOperand().getLeftOperand() == null
       && condition.getRightOperand().getLeftOperand() == null) {
     return validateColumns(condition.getLeftOperand().toString(), joins)
         .equals(validateColumns(condition.getRightOperand().toString(), joins));
   } else {
     return (validateColumnsRecurse(condition.getLeftOperand(), joins)
         && validateColumnsRecurse(condition.getRightOperand(), joins));
   }
 }
Пример #2
0
  public void selectCommand(String query) {
    int invalid = 0;
    String details = "Querytype:select\n";
    TGSqlParser tgSqlParser = new TGSqlParser(EDbVendor.dbvoracle);
    tgSqlParser.sqltext = query;
    tgSqlParser.parse();
    TSelectSqlStatement tSelectSqlStatement =
        (TSelectSqlStatement) tgSqlParser.sqlstatements.get(0);

    try {
      // Find number of tables in join. Invalid if no tables specified
      int noTabs = tSelectSqlStatement.joins.size();
      if (noTabs > 0) {
        if (!validateTables(tSelectSqlStatement)) // check for existing tables and its columns
        throw new NullPointerException();
        details = details + "Tablename:" + tSelectSqlStatement.joins.getJoin(0);
        for (int i = 1; i < noTabs; i++) {
          details = details + "," + tSelectSqlStatement.joins.getJoin(i);
        }
        details = details + "\n";
      }
      // find columns involved in select query

      int noCols = tSelectSqlStatement.getResultColumnList().size();
      String columnList = "";
      if (noCols > 0) {
        String column = tSelectSqlStatement.getResultColumnList().getResultColumn(0).toString();
        details = details + "Columns:";
        if (column.equals("*")) {
          columnList = getColumnName(tSelectSqlStatement.joins);
          details = details + columnList;
        } else {
          columnList =
              tSelectSqlStatement
                  .getResultColumnList()
                  .getResultColumn(0)
                  .toString()
                  .replace("(", "")
                  .replace(")", "");
          for (int i = 1; i < noCols; i++) {
            columnList =
                columnList
                    + ","
                    + tSelectSqlStatement
                        .getResultColumnList()
                        .getResultColumn(i)
                        .toString()
                        .replace("(", "")
                        .replace(")", "");
          }
          details = details + columnList;
        }
        details = details + "\n";
      }

      // Evaluate for distinct
      TSelectDistinct distinctClause = tSelectSqlStatement.getSelectDistinct();
      if (distinctClause != null) {
        details = details + "Distinct:" + columnList + "\n";
      } else details = details + "Distinct:NA\n";

      // Evaluate where clause
      TWhereClause wClause = tSelectSqlStatement.getWhereClause();
      if (wClause != null) {
        // System.out.println(wClause.getCondition().getLeftOperand());
        if (!validateColumnsRecurse(wClause.getCondition(), tSelectSqlStatement.joins))
          throw new NullPointerException();
        details = details + "Condition:" + wClause.getCondition().toString() + "\n";
      } else details = details + "Condition:NA\n";

      // evaluate order by
      TOrderBy orderBy = tSelectSqlStatement.getOrderbyClause();
      if (orderBy != null) {
        String result = validateColumns(orderBy.getItems().toString(), tSelectSqlStatement.joins);
        if (result.equals("int") || result.equals("invalid")) throw new NullPointerException();
        details = details + "OrderBy:" + orderBy.getItems().toString() + "\n";
      } else details = details + "OrderBy:NA\n";

      // evaluate group by
      TGroupBy groupBy = tSelectSqlStatement.getGroupByClause();
      if (groupBy != null) {
        String result = validateColumns(groupBy.getItems().toString(), tSelectSqlStatement.joins);
        if (result.equals("int") || result.equals("invalid")) throw new NullPointerException();
        details = details + "GroupBy:" + groupBy.getItems().toString() + "\n";

        // evaluate having clause {having occurs only with group by}
        TExpression havingClause = groupBy.getHavingClause();
        if (havingClause != null) {
          // result=validateColumns(havingClause.getLeftOperand().toString(),tSelectSqlStatement.joins);
          if (!validateColumnsRecurse(havingClause, tSelectSqlStatement.joins))
            throw new NullPointerException();
          details = details + "Having:" + havingClause.toString() + "\n";
        } else {
          details = details + "Having:NA\n";
        }
      } else details = details + "GroupBy:NA\nHaving:NA\n";

      System.out.print(details);
    } catch (NullPointerException e) {
      System.out.println("Query Invalid");
      // e.printStackTrace();
    }
  }