Example #1
0
    private boolean isUserId(SQLExpr x) {
      if (x instanceof SQLIdentifierExpr) {
        if ("user".equals(defaultTableName) && "uid".equals(((SQLIdentifierExpr) x).getName())) {
          return true;
        }

        return false;
      }

      if (x instanceof SQLPropertyExpr) {
        SQLPropertyExpr propExpr = (SQLPropertyExpr) x;

        String columnName = propExpr.getName();

        if (!"uid".equals(columnName)) {
          return false;
        }

        if (propExpr.getOwner() instanceof SQLIdentifierExpr) {
          String ownerName = ((SQLIdentifierExpr) propExpr.getOwner()).getName();
          if ("user".equals(ownerName) || "user".equals(tableAlias.get(ownerName))) {
            return true;
          }
        }
      }
      return false;
    }
 public void setTable(SQLName table) {
   if (table instanceof SQLPropertyExpr) {
     SQLPropertyExpr propExpr = (SQLPropertyExpr) table;
     this.setDatabase((SQLName) propExpr.getOwner());
     this.table = new SQLIdentifierExpr(propExpr.getName());
     return;
   }
   this.table = table;
 }
Example #3
0
  private String convert(String sql, List<Object> parameters) {
    SQLStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> stmtList = parser.parseStatementList(); //

    SQLStatement first = (SQLStatement) stmtList.get(0);

    MyVisitor visitor = new MyVisitor();
    first.accept(visitor);

    if (visitor.getVariantList().size() > 0) {
      SQLExpr firstVar = visitor.getVariantList().get(0);

      int userId;
      if (firstVar instanceof SQLVariantRefExpr) {
        int varIndex = (Integer) firstVar.getAttribute("varIndex");
        userId = (Integer) parameters.get(varIndex);
      } else {
        userId = ((SQLNumericLiteralExpr) firstVar).getNumber().intValue();
      }

      String tableName;
      if (userId == 1) {
        tableName = "user_1";
      } else {
        tableName = "user_x";
      }

      for (SQLExprTableSource tableSource : visitor.getTableSourceList()) {
        SQLExpr expr = tableSource.getExpr();
        if (expr instanceof SQLIdentifierExpr) {
          SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
          String ident = identExpr.getName();

          if (ident.equals("user")) {
            identExpr.setName(tableName);
          }
        } else if (expr instanceof SQLPropertyExpr) {
          SQLPropertyExpr proExpr = (SQLPropertyExpr) expr;
          String ident = proExpr.getName();

          if (ident.equals("user")) {
            proExpr.setName(tableName);
          }
        }
      }
    }

    String realSql = SQLUtils.toOracleString(first);
    return realSql;
  }