Ejemplo n.º 1
0
  public Connection connect() throws TechnicalException {

    Connection connection = null;
    String connectionString = null;
    try {
      if (databaseParameter.getRdbs().isMySql()) {
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        connectionString =
            "jdbc:mysql://"
                + databaseParameter.getDatabaseHost()
                + ":"
                + databaseParameter.getDatabasePort()
                + "/"
                + databaseParameter.getDatabaseName();
      } else if (databaseParameter.getRdbs().isPostgreSql()) {
        DriverManager.registerDriver(new org.postgresql.Driver());
        connectionString =
            "jdbc:postgresql://"
                + databaseParameter.getDatabaseHost()
                + ":"
                + databaseParameter.getDatabasePort()
                + "/"
                + databaseParameter.getDatabaseName();
      } else if (databaseParameter.getRdbs().isOracle()) {
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        connectionString =
            "jdbc:oracle:thin:@"
                + databaseParameter.getDatabaseHost()
                + ":"
                + databaseParameter.getDatabasePort()
                + ":"
                + databaseParameter.getDatabaseName();
      }

      connection =
          DriverManager.getConnection(
              connectionString,
              databaseParameter.getDatabaseUser(),
              databaseParameter.getDatabasePassword());

      if (databaseParameter.getRdbs().isPostgreSql()) {
        connection.setAutoCommit(
            false); // Need to explicitely set auto-commit to false (for streaming results)
      }
    } catch (SQLException e) {
      throw new TechnicalException(connectionString, e);
    }
    return connection;
  }
Ejemplo n.º 2
0
 public Rdbs getRdbs() {
   return (databaseParameter == null ? null : databaseParameter.getRdbs());
 }
Ejemplo n.º 3
0
  public QueryThread(
      int totalThreads,
      int queryThreadIndex,
      Integer rows,
      int batchSize,
      boolean distinct,
      boolean isLinkIndex,
      boolean isSuperIndex,
      DatabaseParameter databaseParameter,
      String tableName,
      List<String> leftJoinFields,
      List<String> rightJoinFields,
      List<String> resultFields)
      throws TechnicalException {
    super();
    this.queryThreadIndex = queryThreadIndex;
    this.first = queryThreadIndex == 0;
    this.last = queryThreadIndex == totalThreads - 1;

    this.rows = rows;
    this.batchSize = batchSize;
    this.distinct = distinct;
    this.isLinkIndex = isLinkIndex;
    this.isSuperIndex = isSuperIndex;
    this.databaseParameter = databaseParameter;
    this.databaseName = databaseParameter.getDatabaseName();

    this.isMySql = databaseParameter.getRdbs().isMySql();
    this.isOracle = databaseParameter.getRdbs().isOracle();
    this.isPostgreSql = databaseParameter.getRdbs().isPostgreSql();

    this.tableName = tableName;
    this.leftJoinFields = leftJoinFields;
    this.rightJoinFields = rightJoinFields;
    this.resultFields = resultFields;

    this.leftJoinFieldFullNames = new ArrayList<String>();
    this.aliasedLeftJoinFieldFullNames = new ArrayList<String>();
    for (int i = 0; i < leftJoinFields.size(); i++) {
      this.leftJoinFieldFullNames.add(
          this.databaseName + "." + this.tableName + "." + leftJoinFields.get(i));
      this.aliasedLeftJoinFieldFullNames.add(
          QueryRunnerPrototypeConstants.TABLE_ALIAS + "." + leftJoinFields.get(i));
    }
    this.rightJoinFieldFullNames = new ArrayList<String>();
    this.aliasedRightJoinFieldFullNames = new ArrayList<String>();
    for (int i = 0; i < rightJoinFields.size(); i++) {
      this.rightJoinFieldFullNames.add(
          this.databaseName + "." + this.tableName + "." + rightJoinFields.get(i));
      this.aliasedRightJoinFieldFullNames.add(
          QueryRunnerPrototypeConstants.TABLE_ALIAS + "." + rightJoinFields.get(i));
    }
    this.resultFieldFullNames = new ArrayList<String>();
    this.aliasedResultFieldFullNames = new ArrayList<String>();
    for (int i = 0; i < resultFields.size(); i++) {
      this.resultFieldFullNames.add(
          this.databaseName + "." + this.tableName + "." + resultFields.get(i));
      this.aliasedResultFieldFullNames.add(
          QueryRunnerPrototypeConstants.TABLE_ALIAS + "." + resultFields.get(i));
    }

    this.totalFields = 0;
    headers = new ArrayList<String>();
    StringBuffer queryTemplateSb = new StringBuffer();
    queryTemplateSb.append("select ");
    for (int i = 0; i < this.aliasedLeftJoinFieldFullNames.size(); i++) {
      queryTemplateSb.append(
          (i == 0 ? "" : ",") + this.aliasedLeftJoinFieldFullNames.get(i)
          // + " as " + (QueryRunnerPrototypeConstants.COLUMN_ALIAS_PREFIX + i)
          );
      headers.add(this.leftJoinFieldFullNames.get(i));
      this.totalFields++;
    }
    for (int i = 0; i < this.aliasedRightJoinFieldFullNames.size(); i++) {
      queryTemplateSb.append(
          (i == 0 ? (this.aliasedLeftJoinFieldFullNames.size() == 0 ? "" : ",") : ",")
              + this.aliasedRightJoinFieldFullNames.get(i));
      headers.add(this.rightJoinFieldFullNames.get(i));
      this.totalFields++;
    }
    for (int i = 0; i < this.aliasedResultFieldFullNames.size(); i++) {
      String resultField = this.aliasedResultFieldFullNames.get(i);
      // if (!this.leftJoinFieldFullNames.contains(resultField) &&
      // !this.rightJoinFieldFullNames.contains(resultField)) {
      queryTemplateSb.append(
          (i == 0
                  ? (this.aliasedLeftJoinFieldFullNames.size() == 0
                          && this.aliasedRightJoinFieldFullNames.size() == 0
                      ? ""
                      : ",")
                  : ",")
              + resultField);
      headers.add(this.resultFieldFullNames.get(i));
      this.totalFields++;
      // }
    }

    queryTemplateSb.append(" from ");
    queryTemplateSb.append(
        (this.isMySql ? databaseName + "." : "")
            + // that doesn't seem to work with postgres
            tableName
            + (!this.isOracle ? " as" : "")
            + " "
            + QueryRunnerPrototypeConstants.TABLE_ALIAS);

    this.queryTemplate = queryTemplateSb.toString();

    this.launchQueryAverageTime = new Average();
    this.fetchingQueryAverageTime = new Average();
    this.fetchingBatchAverageTime = new Average();
    this.intermediaryResultsProcessingAverageTime = new Average();

    this.statusList = new ArrayList<Status>();
    this.readyList = new ArrayList<Status>();

    this.done = new WaitableBoolean(false);
    this.firstFinalResults =
        new WaitableBoolean(false); // always except for last that will provide them
    this.someFinalResults =
        new WaitableBoolean(false); // always except for last that will provide them
    this.unsynchronizedFirstFinalResults = false;
    this.unsynchronizedSomeFinalResults = false;

    this.cancelThread = new WaitableBoolean(false); // until told differently
    this.activeQuery = new WaitableBoolean(false);

    this.readyList.add(Status.READY); // simulate a signal for all threads to notify they are ready
  }
Ejemplo n.º 4
0
  public void connect(String databaseName) throws TechnicalException {

    this.databaseParameter.setDatabaseName(databaseName);

    try {
      String connectionString = null;
      if (databaseParameter.getRdbs().isMySql()) {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        connectionString =
            "jdbc:mysql://"
                + databaseParameter.getDatabaseHost()
                + ":"
                + databaseParameter.getDatabasePort()
                + "/"
                + databaseParameter.getDatabaseName();
      } else if (databaseParameter.getRdbs().isPostgreSql()) {
        connectionString =
            "jdbc:postgresql://"
                + databaseParameter.getDatabaseHost()
                + ":"
                + databaseParameter.getDatabasePort()
                + "/"
                + databaseParameter.getDatabaseName();
      } else if (databaseParameter.getRdbs().isOracle()) {
        connectionString =
            "jdbc:oracle:thin:@"
                + databaseParameter.getDatabaseHost()
                + ":"
                + databaseParameter.getDatabasePort()
                + ":"
                + databaseParameter.getDatabaseName();
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); // Need to load driver
      }

      connection =
          DriverManager.getConnection(
              connectionString,
              databaseParameter.getDatabaseUser(),
              databaseParameter.getDatabasePassword());

    } catch (SQLException e) {
      throw new TechnicalException(e);
    } catch (InstantiationException e) {
      throw new TechnicalException(e);
    } catch (IllegalAccessException e) {
      throw new TechnicalException(e);
    } catch (ClassNotFoundException e) {
      throw new TechnicalException(e);
    }
  }